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. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
43 #include <sys/mount.h>
49 #include <libzfs_core.h>
51 #include "zfs_namecheck.h"
53 #include "zfs_fletcher.h"
54 #include "libzfs_impl.h"
57 #include <sys/zio_checksum.h>
60 /* in libzfs_dataset.c */
61 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
63 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
64 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **, int,
65 uint64_t *, const char *);
66 static int guid_to_name(libzfs_handle_t
*, const char *,
67 uint64_t, boolean_t
, char *);
69 static const zio_cksum_t zero_cksum
= { 0 };
71 typedef struct dedup_arg
{
74 libzfs_handle_t
*dedup_hdl
;
77 typedef struct progress_arg
{
80 boolean_t pa_parsable
;
83 typedef struct dataref
{
89 typedef struct dedup_entry
{
90 struct dedup_entry
*dde_next
;
91 zio_cksum_t dde_chksum
;
96 #define MAX_DDT_PHYSMEM_PERCENT 20
97 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
99 typedef struct dedup_table
{
100 dedup_entry_t
**dedup_hash_array
;
101 umem_cache_t
*ddecache
;
102 uint64_t max_ddt_size
; /* max dedup table size in bytes */
103 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
110 high_order_bit(uint64_t n
)
114 for (count
= 0; n
!= 0; count
++)
120 ssread(void *buf
, size_t len
, FILE *stream
)
124 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
131 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
132 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
136 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
137 if (ddt
->ddt_full
== B_FALSE
) {
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "Dedup table full. Deduplication will continue "
140 "with existing table entries"));
141 ddt
->ddt_full
= B_TRUE
;
146 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
148 assert(*ddepp
== NULL
);
149 dde
->dde_next
= NULL
;
150 dde
->dde_chksum
= *cs
;
151 dde
->dde_prop
= prop
;
154 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
160 * Using the specified dedup table, do a lookup for an entry with
161 * the checksum cs. If found, return the block's reference info
162 * in *dr. Otherwise, insert a new entry in the dedup table, using
163 * the reference information specified by *dr.
165 * return value: true - entry was found
166 * false - entry was not found
169 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
170 uint64_t prop
, dataref_t
*dr
)
173 dedup_entry_t
**ddepp
;
175 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
177 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
178 ddepp
= &((*ddepp
)->dde_next
)) {
179 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
180 (*ddepp
)->dde_prop
== prop
) {
181 *dr
= (*ddepp
)->dde_ref
;
185 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
190 dump_record(dmu_replay_record_t
*drr
, void *payload
, int payload_len
,
191 zio_cksum_t
*zc
, int outfd
)
193 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
194 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
195 fletcher_4_incremental_native(drr
,
196 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
197 if (drr
->drr_type
!= DRR_BEGIN
) {
198 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
199 drr_checksum
.drr_checksum
));
200 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
202 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
203 sizeof (zio_cksum_t
), zc
);
204 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
206 if (payload_len
!= 0) {
207 fletcher_4_incremental_native(payload
, payload_len
, zc
);
208 if (write(outfd
, payload
, payload_len
) == -1)
215 * This function is started in a separate thread when the dedup option
216 * has been requested. The main send thread determines the list of
217 * snapshots to be included in the send stream and makes the ioctl calls
218 * for each one. But instead of having the ioctl send the output to the
219 * the output fd specified by the caller of zfs_send()), the
220 * ioctl is told to direct the output to a pipe, which is read by the
221 * alternate thread running THIS function. This function does the
223 * 1. building a dedup table (the DDT)
224 * 2. doing checksums on each data block and inserting a record in the DDT
225 * 3. looking for matching checksums, and
226 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
227 * a duplicate block is found.
228 * The output of this function then goes to the output fd requested
229 * by the caller of zfs_send().
234 dedup_arg_t
*dda
= arg
;
235 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
236 dmu_replay_record_t thedrr
;
237 dmu_replay_record_t
*drr
= &thedrr
;
241 zio_cksum_t stream_cksum
;
242 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
246 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
) / 100,
247 SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20);
249 numbuckets
= ddt
.max_ddt_size
/ (sizeof (dedup_entry_t
));
252 * numbuckets must be a power of 2. Increase number to
253 * a power of 2 if necessary.
255 if (!ISP2(numbuckets
))
256 numbuckets
= 1 << high_order_bit(numbuckets
);
258 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
259 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
260 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
261 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
262 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
263 ddt
.ddt_full
= B_FALSE
;
265 outfd
= dda
->outputfd
;
266 ofp
= fdopen(dda
->inputfd
, "r");
267 while (ssread(drr
, sizeof (*drr
), ofp
) != 0) {
270 * kernel filled in checksum, we are going to write same
271 * record, but need to regenerate checksum.
273 if (drr
->drr_type
!= DRR_BEGIN
) {
274 bzero(&drr
->drr_u
.drr_checksum
.drr_checksum
,
275 sizeof (drr
->drr_u
.drr_checksum
.drr_checksum
));
278 switch (drr
->drr_type
) {
281 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
284 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
286 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
288 /* set the DEDUP feature flag for this stream */
289 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
290 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
291 DMU_BACKUP_FEATURE_DEDUPPROPS
);
292 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
294 if (drr
->drr_payloadlen
!= 0) {
295 sz
= drr
->drr_payloadlen
;
297 if (sz
> SPA_MAXBLOCKSIZE
) {
298 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
299 SPA_MAXBLOCKSIZE
, sz
);
301 (void) ssread(buf
, sz
, ofp
);
305 if (dump_record(drr
, buf
, sz
, &stream_cksum
,
313 struct drr_end
*drre
= &drr
->drr_u
.drr_end
;
314 /* use the recalculated checksum */
315 drre
->drr_checksum
= stream_cksum
;
316 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
324 struct drr_object
*drro
= &drr
->drr_u
.drr_object
;
325 if (drro
->drr_bonuslen
> 0) {
327 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
330 if (dump_record(drr
, buf
,
331 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
332 &stream_cksum
, outfd
) != 0)
339 struct drr_spill
*drrs
= &drr
->drr_u
.drr_spill
;
340 (void) ssread(buf
, drrs
->drr_length
, ofp
);
341 if (dump_record(drr
, buf
, drrs
->drr_length
,
342 &stream_cksum
, outfd
) != 0)
347 case DRR_FREEOBJECTS
:
349 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
357 struct drr_write
*drrw
= &drr
->drr_u
.drr_write
;
359 uint64_t payload_size
;
361 payload_size
= DRR_WRITE_PAYLOAD_SIZE(drrw
);
362 (void) ssread(buf
, payload_size
, ofp
);
365 * Use the existing checksum if it's dedup-capable,
366 * else calculate a SHA256 checksum for it.
369 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
371 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
373 zio_cksum_t tmpsha256
;
376 SHA256Update(&ctx
, buf
, payload_size
);
377 SHA256Final(&tmpsha256
, &ctx
);
378 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
379 BE_64(tmpsha256
.zc_word
[0]);
380 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
381 BE_64(tmpsha256
.zc_word
[1]);
382 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
383 BE_64(tmpsha256
.zc_word
[2]);
384 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
385 BE_64(tmpsha256
.zc_word
[3]);
386 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
387 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
390 dataref
.ref_guid
= drrw
->drr_toguid
;
391 dataref
.ref_object
= drrw
->drr_object
;
392 dataref
.ref_offset
= drrw
->drr_offset
;
394 if (ddt_update(dda
->dedup_hdl
, &ddt
,
395 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
397 dmu_replay_record_t wbr_drr
= {0};
398 struct drr_write_byref
*wbr_drrr
=
399 &wbr_drr
.drr_u
.drr_write_byref
;
401 /* block already present in stream */
402 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
404 wbr_drrr
->drr_object
= drrw
->drr_object
;
405 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
406 wbr_drrr
->drr_length
= drrw
->drr_logical_size
;
407 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
408 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
409 wbr_drrr
->drr_refobject
=
411 wbr_drrr
->drr_refoffset
=
414 wbr_drrr
->drr_checksumtype
=
415 drrw
->drr_checksumtype
;
416 wbr_drrr
->drr_checksumflags
=
417 drrw
->drr_checksumtype
;
418 wbr_drrr
->drr_key
.ddk_cksum
=
419 drrw
->drr_key
.ddk_cksum
;
420 wbr_drrr
->drr_key
.ddk_prop
=
421 drrw
->drr_key
.ddk_prop
;
423 if (dump_record(&wbr_drr
, NULL
, 0,
424 &stream_cksum
, outfd
) != 0)
427 /* block not previously seen */
428 if (dump_record(drr
, buf
, payload_size
,
429 &stream_cksum
, outfd
) != 0)
435 case DRR_WRITE_EMBEDDED
:
437 struct drr_write_embedded
*drrwe
=
438 &drr
->drr_u
.drr_write_embedded
;
440 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
441 if (dump_record(drr
, buf
,
442 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
443 &stream_cksum
, outfd
) != 0)
450 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
457 (void) fprintf(stderr
, "INVALID record type 0x%x\n",
459 /* should never happen, so assert */
464 umem_cache_destroy(ddt
.ddecache
);
465 free(ddt
.dedup_hash_array
);
473 * Routines for dealing with the AVL tree of fs-nvlists
475 typedef struct fsavl_node
{
483 fsavl_compare(const void *arg1
, const void *arg2
)
485 const fsavl_node_t
*fn1
= arg1
;
486 const fsavl_node_t
*fn2
= arg2
;
488 if (fn1
->fn_guid
> fn2
->fn_guid
)
490 else if (fn1
->fn_guid
< fn2
->fn_guid
)
497 * Given the GUID of a snapshot, find its containing filesystem and
501 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
503 fsavl_node_t fn_find
;
506 fn_find
.fn_guid
= snapguid
;
508 fn
= avl_find(avl
, &fn_find
, NULL
);
511 *snapname
= fn
->fn_snapname
;
512 return (fn
->fn_nvfs
);
518 fsavl_destroy(avl_tree_t
*avl
)
527 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
534 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
537 fsavl_create(nvlist_t
*fss
)
540 nvpair_t
*fselem
= NULL
;
542 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
545 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
546 offsetof(fsavl_node_t
, fn_node
));
548 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
549 nvlist_t
*nvfs
, *snaps
;
550 nvpair_t
*snapelem
= NULL
;
552 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
553 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
556 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
560 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
561 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
562 fsavl_destroy(fsavl
);
566 fn
->fn_snapname
= nvpair_name(snapelem
);
570 * Note: if there are multiple snaps with the
571 * same GUID, we ignore all but one.
573 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
584 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
586 typedef struct send_data
{
588 * assigned inside every recursive call,
589 * restored from *_save on return:
591 * guid of fromsnap snapshot in parent dataset
592 * txg of fromsnap snapshot in current dataset
593 * txg of tosnap snapshot in current dataset
596 uint64_t parent_fromsnap_guid
;
597 uint64_t fromsnap_txg
;
600 /* the nvlists get accumulated during depth-first traversal */
601 nvlist_t
*parent_snaps
;
605 /* send-receive configuration, does not change during traversal */
607 const char *fromsnap
;
613 * The header nvlist is of the following format:
616 * "fromsnap" -> string (if incremental)
620 * "name" -> string (full name; for debugging)
621 * "parentfromsnap" -> number (guid of fromsnap in parent)
623 * "props" -> { name -> value (only if set here) }
624 * "snaps" -> { name (lastname) -> number (guid) }
625 * "snapprops" -> { name (lastname) -> { name -> value } }
627 * "origin" -> number (guid) (if clone)
628 * "sent" -> boolean (not on-disk)
636 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
639 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
641 send_data_t
*sd
= arg
;
642 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
643 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
647 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
649 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
651 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
652 "skipping snapshot %s because it was created "
653 "after the destination snapshot (%s)\n"),
654 zhp
->zfs_name
, sd
->tosnap
);
660 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
662 * NB: if there is no fromsnap here (it's a newly created fs in
663 * an incremental replication), we will substitute the tosnap.
665 if ((sd
->fromsnap
&& strcmp(snapname
, sd
->fromsnap
) == 0) ||
666 (sd
->parent_fromsnap_guid
== 0 && sd
->tosnap
&&
667 strcmp(snapname
, sd
->tosnap
) == 0)) {
668 sd
->parent_fromsnap_guid
= guid
;
671 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
672 send_iterate_prop(zhp
, nv
);
673 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
681 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
683 nvpair_t
*elem
= NULL
;
685 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
686 char *propname
= nvpair_name(elem
);
687 zfs_prop_t prop
= zfs_name_to_prop(propname
);
690 if (!zfs_prop_user(propname
)) {
692 * Realistically, this should never happen. However,
693 * we want the ability to add DSL properties without
694 * needing to make incompatible version changes. We
695 * need to ignore unknown properties to allow older
696 * software to still send datasets containing these
697 * properties, with the unknown properties elided.
699 if (prop
== ZPROP_INVAL
)
702 if (zfs_prop_readonly(prop
))
706 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
707 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
708 prop
== ZFS_PROP_REFQUOTA
||
709 prop
== ZFS_PROP_REFRESERVATION
) {
712 verify(nvlist_lookup_uint64(propnv
,
713 ZPROP_VALUE
, &value
) == 0);
714 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
717 * May have no source before SPA_VERSION_RECVD_PROPS,
718 * but is still modifiable.
720 if (nvlist_lookup_string(propnv
,
721 ZPROP_SOURCE
, &source
) == 0) {
722 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
724 ZPROP_SOURCE_VAL_RECVD
) != 0))
729 if (nvlist_lookup_string(propnv
,
730 ZPROP_SOURCE
, &source
) != 0)
732 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
733 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
737 if (zfs_prop_user(propname
) ||
738 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
740 verify(nvlist_lookup_string(propnv
,
741 ZPROP_VALUE
, &value
) == 0);
742 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
745 verify(nvlist_lookup_uint64(propnv
,
746 ZPROP_VALUE
, &value
) == 0);
747 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
753 * returns snapshot creation txg
754 * and returns 0 if the snapshot does not exist
757 get_snap_txg(libzfs_handle_t
*hdl
, const char *fs
, const char *snap
)
759 char name
[ZFS_MAX_DATASET_NAME_LEN
];
762 if (fs
== NULL
|| fs
[0] == '\0' || snap
== NULL
|| snap
[0] == '\0')
765 (void) snprintf(name
, sizeof (name
), "%s@%s", fs
, snap
);
766 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_SNAPSHOT
)) {
767 zfs_handle_t
*zhp
= zfs_open(hdl
, name
, ZFS_TYPE_SNAPSHOT
);
769 txg
= zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
);
778 * recursively generate nvlists describing datasets. See comment
779 * for the data structure send_data_t above for description of contents
783 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
785 send_data_t
*sd
= arg
;
788 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
789 uint64_t fromsnap_txg_save
= sd
->fromsnap_txg
;
790 uint64_t tosnap_txg_save
= sd
->tosnap_txg
;
791 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
792 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
793 uint64_t fromsnap_txg
, tosnap_txg
;
796 fromsnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->fromsnap
);
797 if (fromsnap_txg
!= 0)
798 sd
->fromsnap_txg
= fromsnap_txg
;
800 tosnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->tosnap
);
802 sd
->tosnap_txg
= tosnap_txg
;
805 * on the send side, if the current dataset does not have tosnap,
806 * perform two additional checks:
808 * - skip sending the current dataset if it was created later than
810 * - return error if the current dataset was created earlier than
813 if (sd
->tosnap
!= NULL
&& tosnap_txg
== 0) {
814 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
816 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
817 "skipping dataset %s: snapshot %s does "
818 "not exist\n"), zhp
->zfs_name
, sd
->tosnap
);
821 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
822 "cannot send %s@%s%s: snapshot %s@%s does not "
823 "exist\n"), sd
->fsname
, sd
->tosnap
, sd
->recursive
?
824 dgettext(TEXT_DOMAIN
, " recursively") : "",
825 zhp
->zfs_name
, sd
->tosnap
);
831 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
832 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
833 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
834 sd
->parent_fromsnap_guid
));
836 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
837 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
838 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
839 if (origin
== NULL
) {
843 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
844 origin
->zfs_dmustats
.dds_guid
));
847 /* iterate over props */
848 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
849 send_iterate_prop(zhp
, nv
);
850 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
853 /* iterate over snaps, and set sd->parent_fromsnap_guid */
854 sd
->parent_fromsnap_guid
= 0;
855 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
856 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
857 (void) zfs_iter_snapshots(zhp
, B_FALSE
, send_iterate_snap
, sd
);
858 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
859 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
860 nvlist_free(sd
->parent_snaps
);
861 nvlist_free(sd
->snapprops
);
863 /* add this fs to nvlist */
864 (void) snprintf(guidstring
, sizeof (guidstring
),
865 "0x%llx", (longlong_t
)guid
);
866 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
869 /* iterate over children */
871 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
874 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
875 sd
->fromsnap_txg
= fromsnap_txg_save
;
876 sd
->tosnap_txg
= tosnap_txg_save
;
883 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
884 const char *tosnap
, boolean_t recursive
, boolean_t verbose
,
885 nvlist_t
**nvlp
, avl_tree_t
**avlp
)
888 send_data_t sd
= { 0 };
891 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
893 return (EZFS_BADTYPE
);
895 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
897 sd
.fromsnap
= fromsnap
;
899 sd
.recursive
= recursive
;
900 sd
.verbose
= verbose
;
902 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
910 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
921 * Routines specific to "zfs send"
923 typedef struct send_dump_data
{
924 /* these are all just the short snapname (the part after the @) */
925 const char *fromsnap
;
927 char prevsnap
[ZFS_MAX_DATASET_NAME_LEN
];
928 uint64_t prevsnap_obj
;
929 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
930 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
, std_out
;
931 boolean_t large_block
, compress
;
937 snapfilter_cb_t
*filter_cb
;
940 char holdtag
[ZFS_MAX_DATASET_NAME_LEN
];
946 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
947 boolean_t fromorigin
, enum lzc_send_flags flags
, uint64_t *sizep
)
949 zfs_cmd_t zc
= { 0 };
950 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
952 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
953 assert(fromsnap_obj
== 0 || !fromorigin
);
955 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
956 zc
.zc_obj
= fromorigin
;
957 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
958 zc
.zc_fromobj
= fromsnap_obj
;
959 zc
.zc_guid
= 1; /* estimate flag */
962 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
964 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
965 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
969 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
970 "not an earlier snapshot from the same fs"));
971 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
974 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
975 ZFS_TYPE_SNAPSHOT
)) {
976 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
977 "incremental source (@%s) does not exist"),
980 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
993 zfs_error_aux(hdl
, strerror(errno
));
994 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
997 return (zfs_standard_error(hdl
, errno
, errbuf
));
1001 *sizep
= zc
.zc_objset_type
;
1007 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1008 * NULL) to the file descriptor specified by outfd.
1011 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
1012 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
1015 zfs_cmd_t zc
= { 0 };
1016 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1019 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1020 assert(fromsnap_obj
== 0 || !fromorigin
);
1022 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1023 zc
.zc_cookie
= outfd
;
1024 zc
.zc_obj
= fromorigin
;
1025 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1026 zc
.zc_fromobj
= fromsnap_obj
;
1027 zc
.zc_flags
= flags
;
1029 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
1030 if (fromsnap
&& fromsnap
[0] != '\0') {
1031 VERIFY(0 == nvlist_add_string(thisdbg
,
1032 "fromsnap", fromsnap
));
1035 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
1037 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1038 "warning: cannot send '%s'"), zhp
->zfs_name
);
1040 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
1042 VERIFY(0 == nvlist_add_nvlist(debugnv
,
1043 zhp
->zfs_name
, thisdbg
));
1045 nvlist_free(thisdbg
);
1049 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1050 "not an earlier snapshot from the same fs"));
1051 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1054 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
1055 ZFS_TYPE_SNAPSHOT
)) {
1056 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1057 "incremental source (@%s) does not exist"),
1060 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1073 zfs_error_aux(hdl
, strerror(errno
));
1074 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1077 return (zfs_standard_error(hdl
, errno
, errbuf
));
1082 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
1083 nvlist_free(thisdbg
);
1089 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
1091 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1094 * zfs_send() only sets snapholds for sends that need them,
1095 * e.g. replication and doall.
1097 if (sdd
->snapholds
== NULL
)
1100 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1104 send_progress_thread(void *arg
)
1106 progress_arg_t
*pa
= arg
;
1107 zfs_cmd_t zc
= { 0 };
1108 zfs_handle_t
*zhp
= pa
->pa_zhp
;
1109 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1110 unsigned long long bytes
;
1115 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1117 if (!pa
->pa_parsable
)
1118 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1121 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1126 zc
.zc_cookie
= pa
->pa_fd
;
1127 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1128 return ((void *)-1);
1132 bytes
= zc
.zc_cookie
;
1134 if (pa
->pa_parsable
) {
1135 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1136 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1137 bytes
, zhp
->zfs_name
);
1139 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1140 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1141 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1142 buf
, zhp
->zfs_name
);
1148 send_print_verbose(FILE *fout
, const char *tosnap
, const char *fromsnap
,
1149 uint64_t size
, boolean_t parsable
)
1152 if (fromsnap
!= NULL
) {
1153 (void) fprintf(fout
, "incremental\t%s\t%s",
1156 (void) fprintf(fout
, "full\t%s",
1160 if (fromsnap
!= NULL
) {
1161 if (strchr(fromsnap
, '@') == NULL
&&
1162 strchr(fromsnap
, '#') == NULL
) {
1163 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1164 "send from @%s to %s"),
1167 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1168 "send from %s to %s"),
1172 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1180 (void) fprintf(fout
, "\t%llu",
1184 zfs_nicenum(size
, buf
, sizeof (buf
));
1185 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1186 " estimated size is %s"), buf
);
1189 (void) fprintf(fout
, "\n");
1193 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1195 send_dump_data_t
*sdd
= arg
;
1196 progress_arg_t pa
= { 0 };
1199 enum lzc_send_flags flags
= 0;
1201 boolean_t isfromsnap
, istosnap
, fromorigin
;
1202 boolean_t exclude
= B_FALSE
;
1203 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1206 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1207 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1208 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1210 if (!sdd
->seenfrom
&& isfromsnap
) {
1211 gather_holds(zhp
, sdd
);
1212 sdd
->seenfrom
= B_TRUE
;
1213 (void) strcpy(sdd
->prevsnap
, thissnap
);
1214 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1219 if (sdd
->seento
|| !sdd
->seenfrom
) {
1224 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1226 sdd
->seento
= B_TRUE
;
1228 if (sdd
->large_block
)
1229 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1230 if (sdd
->embed_data
)
1231 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1233 flags
|= LZC_SEND_FLAG_COMPRESS
;
1235 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1236 if (sdd
->replicate
) {
1238 nvlist_t
*snapprops
;
1240 * Filter out all intermediate snapshots except origin
1241 * snapshots needed to replicate clones.
1243 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1244 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1246 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1247 "snapprops", &snapprops
));
1248 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1249 thissnap
, &snapprops
));
1250 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1257 * If a filter function exists, call it to determine whether
1258 * this snapshot will be sent.
1260 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1261 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1263 * This snapshot is filtered out. Don't send it, and don't
1264 * set prevsnap_obj, so it will be as if this snapshot didn't
1265 * exist, and the next accepted snapshot will be sent as
1266 * an incremental from the last accepted one, or as the
1267 * first (and full) snapshot in the case of a replication,
1268 * non-incremental send.
1274 gather_holds(zhp
, sdd
);
1275 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1276 (sdd
->fromorigin
|| sdd
->replicate
);
1280 (void) estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1281 fromorigin
, flags
, &size
);
1283 send_print_verbose(fout
, zhp
->zfs_name
,
1284 sdd
->prevsnap
[0] ? sdd
->prevsnap
: NULL
,
1285 size
, sdd
->parsable
);
1291 * If progress reporting is requested, spawn a new thread to
1292 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1294 if (sdd
->progress
) {
1296 pa
.pa_fd
= sdd
->outfd
;
1297 pa
.pa_parsable
= sdd
->parsable
;
1299 if ((err
= pthread_create(&tid
, NULL
,
1300 send_progress_thread
, &pa
)) != 0) {
1306 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1307 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1309 if (sdd
->progress
) {
1310 (void) pthread_cancel(tid
);
1311 (void) pthread_join(tid
, NULL
);
1315 (void) strcpy(sdd
->prevsnap
, thissnap
);
1316 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1322 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1325 send_dump_data_t
*sdd
= arg
;
1326 boolean_t missingfrom
= B_FALSE
;
1327 zfs_cmd_t zc
= { 0 };
1329 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1330 zhp
->zfs_name
, sdd
->tosnap
);
1331 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1332 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1333 "WARNING: could not send %s@%s: does not exist\n"),
1334 zhp
->zfs_name
, sdd
->tosnap
);
1339 if (sdd
->replicate
&& sdd
->fromsnap
) {
1341 * If this fs does not have fromsnap, and we're doing
1342 * recursive, we need to send a full stream from the
1343 * beginning (or an incremental from the origin if this
1344 * is a clone). If we're doing non-recursive, then let
1345 * them get the error.
1347 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1348 zhp
->zfs_name
, sdd
->fromsnap
);
1349 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1350 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1351 missingfrom
= B_TRUE
;
1355 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1356 sdd
->prevsnap_obj
= 0;
1357 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1358 sdd
->seenfrom
= B_TRUE
;
1360 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1361 if (!sdd
->seenfrom
) {
1362 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1363 "WARNING: could not send %s@%s:\n"
1364 "incremental source (%s@%s) does not exist\n"),
1365 zhp
->zfs_name
, sdd
->tosnap
,
1366 zhp
->zfs_name
, sdd
->fromsnap
);
1368 } else if (!sdd
->seento
) {
1369 if (sdd
->fromsnap
) {
1370 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1371 "WARNING: could not send %s@%s:\n"
1372 "incremental source (%s@%s) "
1373 "is not earlier than it\n"),
1374 zhp
->zfs_name
, sdd
->tosnap
,
1375 zhp
->zfs_name
, sdd
->fromsnap
);
1377 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1379 "could not send %s@%s: does not exist\n"),
1380 zhp
->zfs_name
, sdd
->tosnap
);
1389 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1391 send_dump_data_t
*sdd
= arg
;
1393 boolean_t needagain
, progress
;
1395 if (!sdd
->replicate
)
1396 return (dump_filesystem(rzhp
, sdd
));
1398 /* Mark the clone origin snapshots. */
1399 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1400 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1402 uint64_t origin_guid
= 0;
1404 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1405 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1406 if (origin_guid
!= 0) {
1408 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1409 origin_guid
, &snapname
);
1410 if (origin_nv
!= NULL
) {
1411 nvlist_t
*snapprops
;
1412 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1413 "snapprops", &snapprops
));
1414 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1415 snapname
, &snapprops
));
1416 VERIFY(0 == nvlist_add_boolean(
1417 snapprops
, "is_clone_origin"));
1422 needagain
= progress
= B_FALSE
;
1423 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1424 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1425 nvlist_t
*fslist
, *parent_nv
;
1429 uint64_t origin_guid
= 0;
1430 uint64_t parent_guid
= 0;
1432 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1433 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1436 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1437 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1438 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1441 if (parent_guid
!= 0) {
1442 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1443 if (!nvlist_exists(parent_nv
, "sent")) {
1444 /* parent has not been sent; skip this one */
1450 if (origin_guid
!= 0) {
1451 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1453 if (origin_nv
!= NULL
&&
1454 !nvlist_exists(origin_nv
, "sent")) {
1456 * origin has not been sent yet;
1464 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1467 err
= dump_filesystem(zhp
, sdd
);
1468 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1479 /* clean out the sent flags in case we reuse this fss */
1480 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1481 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1484 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1485 (void) nvlist_remove_all(fslist
, "sent");
1492 zfs_send_resume_token_to_nvlist(libzfs_handle_t
*hdl
, const char *token
)
1494 unsigned int version
;
1496 unsigned long long checksum
, packed_len
;
1499 * Decode token header, which is:
1500 * <token version>-<checksum of payload>-<uncompressed payload length>
1501 * Note that the only supported token version is 1.
1503 nread
= sscanf(token
, "%u-%llx-%llx-",
1504 &version
, &checksum
, &packed_len
);
1506 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1507 "resume token is corrupt (invalid format)"));
1511 if (version
!= ZFS_SEND_RESUME_TOKEN_VERSION
) {
1512 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1513 "resume token is corrupt (invalid version %u)"),
1518 /* convert hexadecimal representation to binary */
1519 token
= strrchr(token
, '-') + 1;
1520 int len
= strlen(token
) / 2;
1521 unsigned char *compressed
= zfs_alloc(hdl
, len
);
1522 for (int i
= 0; i
< len
; i
++) {
1523 nread
= sscanf(token
+ i
* 2, "%2hhx", compressed
+ i
);
1526 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1527 "resume token is corrupt "
1528 "(payload is not hex-encoded)"));
1533 /* verify checksum */
1535 fletcher_4_native(compressed
, len
, NULL
, &cksum
);
1536 if (cksum
.zc_word
[0] != checksum
) {
1538 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1539 "resume token is corrupt (incorrect checksum)"));
1544 void *packed
= zfs_alloc(hdl
, packed_len
);
1545 uLongf packed_len_long
= packed_len
;
1546 if (uncompress(packed
, &packed_len_long
, compressed
, len
) != Z_OK
||
1547 packed_len_long
!= packed_len
) {
1550 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1551 "resume token is corrupt (decompression failed)"));
1557 int error
= nvlist_unpack(packed
, packed_len
, &nv
, KM_SLEEP
);
1561 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1562 "resume token is corrupt (nvlist_unpack failed)"));
1569 zfs_send_resume(libzfs_handle_t
*hdl
, sendflags_t
*flags
, int outfd
,
1570 const char *resume_token
)
1574 char *fromname
= NULL
;
1575 uint64_t resumeobj
, resumeoff
, toguid
, fromguid
, bytes
;
1578 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1579 enum lzc_send_flags lzc_flags
= 0;
1581 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1582 "cannot resume send"));
1584 nvlist_t
*resume_nvl
=
1585 zfs_send_resume_token_to_nvlist(hdl
, resume_token
);
1586 if (resume_nvl
== NULL
) {
1588 * zfs_error_aux has already been set by
1589 * zfs_send_resume_token_to_nvlist
1591 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1593 if (flags
->verbose
) {
1594 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1595 "resume token contents:\n"));
1596 nvlist_print(stderr
, resume_nvl
);
1599 if (nvlist_lookup_string(resume_nvl
, "toname", &toname
) != 0 ||
1600 nvlist_lookup_uint64(resume_nvl
, "object", &resumeobj
) != 0 ||
1601 nvlist_lookup_uint64(resume_nvl
, "offset", &resumeoff
) != 0 ||
1602 nvlist_lookup_uint64(resume_nvl
, "bytes", &bytes
) != 0 ||
1603 nvlist_lookup_uint64(resume_nvl
, "toguid", &toguid
) != 0) {
1604 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1605 "resume token is corrupt"));
1606 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1609 (void) nvlist_lookup_uint64(resume_nvl
, "fromguid", &fromguid
);
1611 if (flags
->largeblock
|| nvlist_exists(resume_nvl
, "largeblockok"))
1612 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1613 if (flags
->embed_data
|| nvlist_exists(resume_nvl
, "embedok"))
1614 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1615 if (flags
->compress
|| nvlist_exists(resume_nvl
, "compressok"))
1616 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
1618 if (guid_to_name(hdl
, toname
, toguid
, B_FALSE
, name
) != 0) {
1619 if (zfs_dataset_exists(hdl
, toname
, ZFS_TYPE_DATASET
)) {
1620 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1621 "'%s' is no longer the same snapshot used in "
1622 "the initial send"), toname
);
1624 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1625 "'%s' used in the initial send no longer exists"),
1628 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1630 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1632 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1633 "unable to access '%s'"), name
);
1634 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1637 if (fromguid
!= 0) {
1638 if (guid_to_name(hdl
, toname
, fromguid
, B_TRUE
, name
) != 0) {
1639 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1640 "incremental source %#llx no longer exists"),
1641 (longlong_t
)fromguid
);
1642 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1647 if (flags
->verbose
) {
1649 error
= lzc_send_space(zhp
->zfs_name
, fromname
,
1652 size
= MAX(0, (int64_t)(size
- bytes
));
1653 send_print_verbose(stderr
, zhp
->zfs_name
, fromname
,
1654 size
, flags
->parsable
);
1657 if (!flags
->dryrun
) {
1658 progress_arg_t pa
= { 0 };
1661 * If progress reporting is requested, spawn a new thread to
1662 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1664 if (flags
->progress
) {
1667 pa
.pa_parsable
= flags
->parsable
;
1669 error
= pthread_create(&tid
, NULL
,
1670 send_progress_thread
, &pa
);
1677 error
= lzc_send_resume(zhp
->zfs_name
, fromname
, outfd
,
1678 lzc_flags
, resumeobj
, resumeoff
);
1680 if (flags
->progress
) {
1681 (void) pthread_cancel(tid
);
1682 (void) pthread_join(tid
, NULL
);
1686 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1687 "warning: cannot send '%s'"), zhp
->zfs_name
);
1707 zfs_error_aux(hdl
, strerror(errno
));
1708 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1711 return (zfs_standard_error(hdl
, errno
, errbuf
));
1722 * Generate a send stream for the dataset identified by the argument zhp.
1724 * The content of the send stream is the snapshot identified by
1725 * 'tosnap'. Incremental streams are requested in two ways:
1726 * - from the snapshot identified by "fromsnap" (if non-null) or
1727 * - from the origin of the dataset identified by zhp, which must
1728 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1731 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1732 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1733 * if "replicate" is set. If "doall" is set, dump all the intermediate
1734 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1735 * case too. If "props" is set, send properties.
1738 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1739 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1740 void *cb_arg
, nvlist_t
**debugnvp
)
1743 send_dump_data_t sdd
= { 0 };
1745 nvlist_t
*fss
= NULL
;
1746 avl_tree_t
*fsavl
= NULL
;
1747 static uint64_t holdseq
;
1751 dedup_arg_t dda
= { 0 };
1752 int featureflags
= 0;
1755 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1756 "cannot send '%s'"), zhp
->zfs_name
);
1758 if (fromsnap
&& fromsnap
[0] == '\0') {
1759 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1760 "zero-length incremental source"));
1761 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1764 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1766 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1767 if (version
>= ZPL_VERSION_SA
) {
1768 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1772 if (flags
->dedup
&& !flags
->dryrun
) {
1773 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1774 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1775 if ((err
= pipe(pipefd
)) != 0) {
1776 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1777 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1780 dda
.outputfd
= outfd
;
1781 dda
.inputfd
= pipefd
[1];
1782 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1783 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
)) != 0) {
1784 (void) close(pipefd
[0]);
1785 (void) close(pipefd
[1]);
1786 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1787 return (zfs_error(zhp
->zfs_hdl
,
1788 EZFS_THREADCREATEFAILED
, errbuf
));
1792 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1793 dmu_replay_record_t drr
= { 0 };
1794 char *packbuf
= NULL
;
1796 zio_cksum_t zc
= { 0 };
1798 if (flags
->replicate
|| flags
->props
) {
1801 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1803 VERIFY(0 == nvlist_add_string(hdrnv
,
1804 "fromsnap", fromsnap
));
1806 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1807 if (!flags
->replicate
) {
1808 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1812 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1813 fromsnap
, tosnap
, flags
->replicate
, flags
->verbose
,
1817 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1818 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1828 if (!flags
->dryrun
) {
1829 /* write first begin record */
1830 drr
.drr_type
= DRR_BEGIN
;
1831 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1832 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1833 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1834 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1835 drr_versioninfo
, featureflags
);
1836 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1837 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1838 "%s@%s", zhp
->zfs_name
, tosnap
);
1839 drr
.drr_payloadlen
= buflen
;
1841 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, outfd
);
1846 /* write end record */
1847 bzero(&drr
, sizeof (drr
));
1848 drr
.drr_type
= DRR_END
;
1849 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1850 err
= write(outfd
, &drr
, sizeof (drr
));
1860 /* dump each stream */
1861 sdd
.fromsnap
= fromsnap
;
1862 sdd
.tosnap
= tosnap
;
1864 sdd
.outfd
= pipefd
[0];
1867 sdd
.replicate
= flags
->replicate
;
1868 sdd
.doall
= flags
->doall
;
1869 sdd
.fromorigin
= flags
->fromorigin
;
1872 sdd
.verbose
= flags
->verbose
;
1873 sdd
.parsable
= flags
->parsable
;
1874 sdd
.progress
= flags
->progress
;
1875 sdd
.dryrun
= flags
->dryrun
;
1876 sdd
.large_block
= flags
->largeblock
;
1877 sdd
.embed_data
= flags
->embed_data
;
1878 sdd
.compress
= flags
->compress
;
1879 sdd
.filter_cb
= filter_func
;
1880 sdd
.filter_cb_arg
= cb_arg
;
1882 sdd
.debugnv
= *debugnvp
;
1883 if (sdd
.verbose
&& sdd
.dryrun
)
1884 sdd
.std_out
= B_TRUE
;
1885 fout
= sdd
.std_out
? stdout
: stderr
;
1888 * Some flags require that we place user holds on the datasets that are
1889 * being sent so they don't get destroyed during the send. We can skip
1890 * this step if the pool is imported read-only since the datasets cannot
1893 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1894 ZPOOL_PROP_READONLY
, NULL
) &&
1895 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1896 spa_version
>= SPA_VERSION_USERREFS
&&
1897 (flags
->doall
|| flags
->replicate
)) {
1899 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1900 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1901 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
|O_EXCL
);
1902 if (sdd
.cleanup_fd
< 0) {
1906 sdd
.snapholds
= fnvlist_alloc();
1908 sdd
.cleanup_fd
= -1;
1909 sdd
.snapholds
= NULL
;
1911 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1913 * Do a verbose no-op dry run to get all the verbose output
1914 * or to gather snapshot hold's before generating any data,
1915 * then do a non-verbose real run to generate the streams.
1917 sdd
.dryrun
= B_TRUE
;
1918 err
= dump_filesystems(zhp
, &sdd
);
1923 if (flags
->verbose
) {
1924 if (flags
->parsable
) {
1925 (void) fprintf(fout
, "size\t%llu\n",
1926 (longlong_t
)sdd
.size
);
1929 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1930 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1931 "total estimated size is %s\n"), buf
);
1935 /* Ensure no snaps found is treated as an error. */
1941 /* Skip the second run if dryrun was requested. */
1945 if (sdd
.snapholds
!= NULL
) {
1946 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1950 fnvlist_free(sdd
.snapholds
);
1951 sdd
.snapholds
= NULL
;
1954 sdd
.dryrun
= B_FALSE
;
1955 sdd
.verbose
= B_FALSE
;
1958 err
= dump_filesystems(zhp
, &sdd
);
1959 fsavl_destroy(fsavl
);
1962 /* Ensure no snaps found is treated as an error. */
1963 if (err
== 0 && !sdd
.seento
)
1968 (void) pthread_cancel(tid
);
1969 (void) close(pipefd
[0]);
1970 (void) pthread_join(tid
, NULL
);
1973 if (sdd
.cleanup_fd
!= -1) {
1974 VERIFY(0 == close(sdd
.cleanup_fd
));
1975 sdd
.cleanup_fd
= -1;
1978 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1981 * write final end record. NB: want to do this even if
1982 * there was some error, because it might not be totally
1985 dmu_replay_record_t drr
= { 0 };
1986 drr
.drr_type
= DRR_END
;
1987 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1988 return (zfs_standard_error(zhp
->zfs_hdl
,
1993 return (err
|| sdd
.err
);
1996 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1998 fsavl_destroy(fsavl
);
2000 fnvlist_free(sdd
.snapholds
);
2002 if (sdd
.cleanup_fd
!= -1)
2003 VERIFY(0 == close(sdd
.cleanup_fd
));
2005 (void) pthread_cancel(tid
);
2006 (void) close(pipefd
[0]);
2007 (void) pthread_join(tid
, NULL
);
2013 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
2014 enum lzc_send_flags flags
)
2017 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2020 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2021 "warning: cannot send '%s'"), zhp
->zfs_name
);
2023 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
2027 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2028 "not an earlier snapshot from the same fs"));
2029 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
2033 if (lzc_exists(zhp
->zfs_name
)) {
2034 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2035 "incremental source (%s) does not exist"),
2038 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2042 "target is busy; if a filesystem, "
2043 "it must not be mounted"));
2044 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
2057 zfs_error_aux(hdl
, strerror(errno
));
2058 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
2061 return (zfs_standard_error(hdl
, errno
, errbuf
));
2068 * Routines specific to "zfs recv"
2072 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
2073 boolean_t byteswap
, zio_cksum_t
*zc
)
2079 assert(ilen
<= SPA_MAXBLOCKSIZE
);
2082 rv
= read(fd
, cp
, len
);
2087 if (rv
< 0 || len
!= 0) {
2088 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2089 "failed to read from stream"));
2090 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
2091 "cannot receive")));
2096 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
2098 fletcher_4_incremental_native(buf
, ilen
, zc
);
2104 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
2105 boolean_t byteswap
, zio_cksum_t
*zc
)
2110 buf
= zfs_alloc(hdl
, len
);
2114 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
2120 err
= nvlist_unpack(buf
, len
, nvp
, 0);
2123 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2124 "stream (malformed nvlist)"));
2131 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
2132 int baselen
, char *newname
, recvflags_t
*flags
)
2135 zfs_cmd_t zc
= { 0 };
2137 prop_changelist_t
*clp
;
2140 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2143 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2144 flags
->force
? MS_FORCE
: 0);
2148 err
= changelist_prefix(clp
);
2152 zc
.zc_objset_type
= DMU_OST_ZFS
;
2153 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2156 (void) strcpy(newname
, tryname
);
2158 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
2160 if (flags
->verbose
) {
2161 (void) printf("attempting rename %s to %s\n",
2162 zc
.zc_name
, zc
.zc_value
);
2164 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2166 changelist_rename(clp
, name
, tryname
);
2171 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
2174 (void) snprintf(newname
, ZFS_MAX_DATASET_NAME_LEN
,
2175 "%.*srecv-%u-%u", baselen
, name
, getpid(), seq
);
2176 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
2178 if (flags
->verbose
) {
2179 (void) printf("failed - trying rename %s to %s\n",
2180 zc
.zc_name
, zc
.zc_value
);
2182 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2184 changelist_rename(clp
, name
, newname
);
2185 if (err
&& flags
->verbose
) {
2186 (void) printf("failed (%u) - "
2187 "will try again on next pass\n", errno
);
2190 } else if (flags
->verbose
) {
2192 (void) printf("success\n");
2194 (void) printf("failed (%u)\n", errno
);
2197 (void) changelist_postfix(clp
);
2198 changelist_free(clp
);
2204 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
2205 char *newname
, recvflags_t
*flags
)
2207 zfs_cmd_t zc
= { 0 };
2209 prop_changelist_t
*clp
;
2211 boolean_t defer
= B_FALSE
;
2214 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2217 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2218 flags
->force
? MS_FORCE
: 0);
2219 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2220 zfs_spa_version(zhp
, &spa_version
) == 0 &&
2221 spa_version
>= SPA_VERSION_USERREFS
)
2226 err
= changelist_prefix(clp
);
2230 zc
.zc_objset_type
= DMU_OST_ZFS
;
2231 zc
.zc_defer_destroy
= defer
;
2232 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2235 (void) printf("attempting destroy %s\n", zc
.zc_name
);
2236 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
2239 (void) printf("success\n");
2240 changelist_remove(clp
, zc
.zc_name
);
2243 (void) changelist_postfix(clp
);
2244 changelist_free(clp
);
2247 * Deferred destroy might destroy the snapshot or only mark it to be
2248 * destroyed later, and it returns success in either case.
2250 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
2251 ZFS_TYPE_SNAPSHOT
))) {
2252 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
2258 typedef struct guid_to_name_data
{
2260 boolean_t bookmark_ok
;
2263 } guid_to_name_data_t
;
2266 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
2268 guid_to_name_data_t
*gtnd
= arg
;
2272 if (gtnd
->skip
!= NULL
&&
2273 (slash
= strrchr(zhp
->zfs_name
, '/')) != NULL
&&
2274 strcmp(slash
+ 1, gtnd
->skip
) == 0) {
2279 if (zfs_prop_get_int(zhp
, ZFS_PROP_GUID
) == gtnd
->guid
) {
2280 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
2285 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
2286 if (err
!= EEXIST
&& gtnd
->bookmark_ok
)
2287 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, gtnd
);
2293 * Attempt to find the local dataset associated with this guid. In the case of
2294 * multiple matches, we attempt to find the "best" match by searching
2295 * progressively larger portions of the hierarchy. This allows one to send a
2296 * tree of datasets individually and guarantee that we will find the source
2297 * guid within that hierarchy, even if there are multiple matches elsewhere.
2300 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
2301 boolean_t bookmark_ok
, char *name
)
2303 char pname
[ZFS_MAX_DATASET_NAME_LEN
];
2304 guid_to_name_data_t gtnd
;
2307 gtnd
.bookmark_ok
= bookmark_ok
;
2312 * Search progressively larger portions of the hierarchy, starting
2313 * with the filesystem specified by 'parent'. This will
2314 * select the "most local" version of the origin snapshot in the case
2315 * that there are multiple matching snapshots in the system.
2317 (void) strlcpy(pname
, parent
, sizeof (pname
));
2318 char *cp
= strrchr(pname
, '@');
2320 cp
= strchr(pname
, '\0');
2321 for (; cp
!= NULL
; cp
= strrchr(pname
, '/')) {
2322 /* Chop off the last component and open the parent */
2324 zfs_handle_t
*zhp
= make_dataset_handle(hdl
, pname
);
2328 int err
= guid_to_name_cb(zfs_handle_dup(zhp
), >nd
);
2330 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
2331 if (err
!= EEXIST
&& bookmark_ok
)
2332 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, >nd
);
2338 * Remember the last portion of the dataset so we skip it next
2339 * time through (as we've already searched that portion of the
2342 gtnd
.skip
= strrchr(pname
, '/') + 1;
2349 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2350 * guid1 is after guid2.
2353 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
2354 uint64_t guid1
, uint64_t guid2
)
2357 char *fsname
, *snapname
;
2358 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2360 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2361 uint64_t create1
, create2
;
2368 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2369 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2370 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2371 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2372 if (guid1hdl
== NULL
)
2375 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2376 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2377 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2378 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2379 if (guid2hdl
== NULL
) {
2380 zfs_close(guid1hdl
);
2384 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2385 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2387 if (create1
< create2
)
2389 else if (create1
> create2
)
2394 zfs_close(guid1hdl
);
2395 zfs_close(guid2hdl
);
2401 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2402 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2406 avl_tree_t
*local_avl
;
2407 nvpair_t
*fselem
, *nextfselem
;
2409 char newname
[ZFS_MAX_DATASET_NAME_LEN
];
2411 boolean_t needagain
, progress
, recursive
;
2414 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2416 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2423 needagain
= progress
= B_FALSE
;
2425 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2426 recursive
, B_FALSE
, &local_nv
, &local_avl
)) != 0)
2430 * Process deletes and renames
2432 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2433 fselem
; fselem
= nextfselem
) {
2434 nvlist_t
*nvfs
, *snaps
;
2435 nvlist_t
*stream_nvfs
= NULL
;
2436 nvpair_t
*snapelem
, *nextsnapelem
;
2437 uint64_t fromguid
= 0;
2438 uint64_t originguid
= 0;
2439 uint64_t stream_originguid
= 0;
2440 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2441 char *fsname
, *stream_fsname
;
2443 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2445 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2446 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2447 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2448 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2449 &parent_fromsnap_guid
));
2450 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2453 * First find the stream's fs, so we can check for
2454 * a different origin (due to "zfs promote")
2456 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2457 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2460 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2461 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2463 if (stream_nvfs
!= NULL
)
2467 /* check for promote */
2468 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2469 &stream_originguid
);
2470 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2471 switch (created_before(hdl
, local_avl
,
2472 stream_originguid
, originguid
)) {
2475 zfs_cmd_t zc
= { 0 };
2476 nvlist_t
*origin_nvfs
;
2477 char *origin_fsname
;
2480 (void) printf("promoting %s\n", fsname
);
2482 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2484 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2485 "name", &origin_fsname
));
2486 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2487 sizeof (zc
.zc_value
));
2488 (void) strlcpy(zc
.zc_name
, fsname
,
2489 sizeof (zc
.zc_name
));
2490 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2498 fsavl_destroy(local_avl
);
2499 nvlist_free(local_nv
);
2503 * We had/have the wrong origin, therefore our
2504 * list of snapshots is wrong. Need to handle
2505 * them on the next pass.
2511 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2512 snapelem
; snapelem
= nextsnapelem
) {
2514 char *stream_snapname
;
2515 nvlist_t
*found
, *props
;
2517 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2519 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2520 found
= fsavl_find(stream_avl
, thisguid
,
2523 /* check for delete */
2524 if (found
== NULL
) {
2525 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2530 (void) snprintf(name
, sizeof (name
), "%s@%s",
2531 fsname
, nvpair_name(snapelem
));
2533 error
= recv_destroy(hdl
, name
,
2534 strlen(fsname
)+1, newname
, flags
);
2542 stream_nvfs
= found
;
2544 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2545 &props
) && 0 == nvlist_lookup_nvlist(props
,
2546 stream_snapname
, &props
)) {
2547 zfs_cmd_t zc
= { 0 };
2549 zc
.zc_cookie
= B_TRUE
; /* received */
2550 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2551 "%s@%s", fsname
, nvpair_name(snapelem
));
2552 if (zcmd_write_src_nvlist(hdl
, &zc
,
2554 (void) zfs_ioctl(hdl
,
2555 ZFS_IOC_SET_PROP
, &zc
);
2556 zcmd_free_nvlists(&zc
);
2560 /* check for different snapname */
2561 if (strcmp(nvpair_name(snapelem
),
2562 stream_snapname
) != 0) {
2563 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2564 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2566 (void) snprintf(name
, sizeof (name
), "%s@%s",
2567 fsname
, nvpair_name(snapelem
));
2568 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2569 fsname
, stream_snapname
);
2571 error
= recv_rename(hdl
, name
, tryname
,
2572 strlen(fsname
)+1, newname
, flags
);
2579 if (strcmp(stream_snapname
, fromsnap
) == 0)
2580 fromguid
= thisguid
;
2583 /* check for delete */
2584 if (stream_nvfs
== NULL
) {
2588 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2597 if (fromguid
== 0) {
2598 if (flags
->verbose
) {
2599 (void) printf("local fs %s does not have "
2600 "fromsnap (%s in stream); must have "
2601 "been deleted locally; ignoring\n",
2607 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2608 "name", &stream_fsname
));
2609 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2610 "parentfromsnap", &stream_parent_fromsnap_guid
));
2612 s1
= strrchr(fsname
, '/');
2613 s2
= strrchr(stream_fsname
, '/');
2616 * Check for rename. If the exact receive path is specified, it
2617 * does not count as a rename, but we still need to check the
2618 * datasets beneath it.
2620 if ((stream_parent_fromsnap_guid
!= 0 &&
2621 parent_fromsnap_guid
!= 0 &&
2622 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2623 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2624 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2626 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2628 parent
= fsavl_find(local_avl
,
2629 stream_parent_fromsnap_guid
, NULL
);
2631 * NB: parent might not be found if we used the
2632 * tosnap for stream_parent_fromsnap_guid,
2633 * because the parent is a newly-created fs;
2634 * we'll be able to rename it after we recv the
2637 if (parent
!= NULL
) {
2640 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2642 (void) snprintf(tryname
, sizeof (tryname
),
2643 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2646 if (flags
->verbose
) {
2647 (void) printf("local fs %s new parent "
2648 "not found\n", fsname
);
2654 error
= recv_rename(hdl
, fsname
, tryname
,
2655 strlen(tofs
)+1, newname
, flags
);
2657 if (renamed
!= NULL
&& newname
[0] != '\0') {
2658 VERIFY(0 == nvlist_add_boolean(renamed
,
2669 fsavl_destroy(local_avl
);
2670 nvlist_free(local_nv
);
2672 if (needagain
&& progress
) {
2673 /* do another pass to fix up temporary names */
2675 (void) printf("another pass:\n");
2683 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2684 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2685 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2687 nvlist_t
*stream_nv
= NULL
;
2688 avl_tree_t
*stream_avl
= NULL
;
2689 char *fromsnap
= NULL
;
2690 char *sendsnap
= NULL
;
2692 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
2693 char sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
2695 dmu_replay_record_t drre
;
2697 boolean_t anyerr
= B_FALSE
;
2698 boolean_t softerr
= B_FALSE
;
2699 boolean_t recursive
;
2701 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2704 assert(drr
->drr_type
== DRR_BEGIN
);
2705 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2706 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2707 DMU_COMPOUNDSTREAM
);
2710 * Read in the nvlist from the stream.
2712 if (drr
->drr_payloadlen
!= 0) {
2713 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2714 &stream_nv
, flags
->byteswap
, zc
);
2716 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2721 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2724 if (recursive
&& strchr(destname
, '@')) {
2725 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2726 "cannot specify snapshot name for multi-snapshot stream"));
2727 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2732 * Read in the end record and verify checksum.
2734 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2735 flags
->byteswap
, NULL
)))
2737 if (flags
->byteswap
) {
2738 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2739 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2740 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2741 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2742 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2743 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2744 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2745 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2746 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2748 if (drre
.drr_type
!= DRR_END
) {
2749 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2752 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2753 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2754 "incorrect header checksum"));
2755 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2759 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2761 if (drr
->drr_payloadlen
!= 0) {
2762 nvlist_t
*stream_fss
;
2764 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2766 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2767 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2768 "couldn't allocate avl tree"));
2769 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2773 if (fromsnap
!= NULL
&& recursive
) {
2774 nvlist_t
*renamed
= NULL
;
2775 nvpair_t
*pair
= NULL
;
2777 (void) strlcpy(tofs
, destname
, sizeof (tofs
));
2778 if (flags
->isprefix
) {
2779 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2782 if (flags
->istail
) {
2783 cp
= strrchr(drrb
->drr_toname
, '/');
2785 (void) strlcat(tofs
, "/",
2789 i
= (cp
- drrb
->drr_toname
);
2792 i
= strcspn(drrb
->drr_toname
, "/@");
2794 /* zfs_receive_one() will create_parents() */
2795 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2797 *strchr(tofs
, '@') = '\0';
2800 if (!flags
->dryrun
&& !flags
->nomount
) {
2801 VERIFY(0 == nvlist_alloc(&renamed
,
2802 NV_UNIQUE_NAME
, 0));
2805 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2806 stream_nv
, stream_avl
, renamed
);
2808 /* Unmount renamed filesystems before receiving. */
2809 while ((pair
= nvlist_next_nvpair(renamed
,
2812 prop_changelist_t
*clp
= NULL
;
2814 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2815 ZFS_TYPE_FILESYSTEM
);
2817 clp
= changelist_gather(zhp
,
2818 ZFS_PROP_MOUNTPOINT
, 0, 0);
2822 changelist_prefix(clp
);
2823 changelist_free(clp
);
2828 nvlist_free(renamed
);
2833 * Get the fs specified by the first path in the stream (the top level
2834 * specified by 'zfs send') and pass it to each invocation of
2835 * zfs_receive_one().
2837 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2839 if ((cp
= strchr(sendfs
, '@')) != NULL
) {
2842 * Find the "sendsnap", the final snapshot in a replication
2843 * stream. zfs_receive_one() handles certain errors
2844 * differently, depending on if the contained stream is the
2847 sendsnap
= (cp
+ 1);
2850 /* Finally, receive each contained stream */
2853 * we should figure out if it has a recoverable
2854 * error, in which case do a recv_skip() and drive on.
2855 * Note, if we fail due to already having this guid,
2856 * zfs_receive_one() will take care of it (ie,
2857 * recv_skip() and return 0).
2859 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
2860 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2861 action_handlep
, sendsnap
);
2862 if (error
== ENODATA
) {
2867 } while (error
== 0);
2869 if (drr
->drr_payloadlen
!= 0 && recursive
&& fromsnap
!= NULL
) {
2871 * Now that we have the fs's they sent us, try the
2874 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2875 stream_nv
, stream_avl
, NULL
);
2879 fsavl_destroy(stream_avl
);
2880 nvlist_free(stream_nv
);
2889 trunc_prop_errs(int truncated
)
2891 ASSERT(truncated
!= 0);
2894 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2895 "1 more property could not be set\n"));
2897 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2898 "%d more properties could not be set\n"), truncated
);
2902 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2904 dmu_replay_record_t
*drr
;
2905 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2908 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2909 "cannot receive:"));
2911 /* XXX would be great to use lseek if possible... */
2914 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2915 byteswap
, NULL
) == 0) {
2917 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2919 switch (drr
->drr_type
) {
2921 if (drr
->drr_payloadlen
!= 0) {
2922 (void) recv_read(hdl
, fd
, buf
,
2923 drr
->drr_payloadlen
, B_FALSE
, NULL
);
2933 drr
->drr_u
.drr_object
.drr_bonuslen
=
2934 BSWAP_32(drr
->drr_u
.drr_object
.
2937 (void) recv_read(hdl
, fd
, buf
,
2938 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2944 drr
->drr_u
.drr_write
.drr_logical_size
=
2946 drr
->drr_u
.drr_write
.drr_logical_size
);
2947 drr
->drr_u
.drr_write
.drr_compressed_size
=
2949 drr
->drr_u
.drr_write
.drr_compressed_size
);
2951 uint64_t payload_size
=
2952 DRR_WRITE_PAYLOAD_SIZE(&drr
->drr_u
.drr_write
);
2953 (void) recv_read(hdl
, fd
, buf
,
2954 payload_size
, B_FALSE
, NULL
);
2958 drr
->drr_u
.drr_spill
.drr_length
=
2959 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2961 (void) recv_read(hdl
, fd
, buf
,
2962 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2964 case DRR_WRITE_EMBEDDED
:
2966 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2967 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2970 (void) recv_read(hdl
, fd
, buf
,
2971 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2974 case DRR_WRITE_BYREF
:
2975 case DRR_FREEOBJECTS
:
2980 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2981 "invalid record type"));
2982 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2991 recv_ecksum_set_aux(libzfs_handle_t
*hdl
, const char *target_snap
,
2992 boolean_t resumable
)
2994 char target_fs
[ZFS_MAX_DATASET_NAME_LEN
];
2996 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2997 "checksum mismatch or incomplete stream"));
3001 (void) strlcpy(target_fs
, target_snap
, sizeof (target_fs
));
3002 *strchr(target_fs
, '@') = '\0';
3003 zfs_handle_t
*zhp
= zfs_open(hdl
, target_fs
,
3004 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3008 char token_buf
[ZFS_MAXPROPLEN
];
3009 int error
= zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
3010 token_buf
, sizeof (token_buf
),
3011 NULL
, NULL
, 0, B_TRUE
);
3013 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3014 "checksum mismatch or incomplete stream.\n"
3015 "Partially received snapshot is saved.\n"
3016 "A resuming stream can be generated on the sending "
3017 "system by running:\n"
3025 * Restores a backup of tosnap from the file descriptor specified by infd.
3028 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
3029 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
3030 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
3031 avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3032 uint64_t *action_handlep
, const char *finalsnap
)
3034 zfs_cmd_t zc
= { 0 };
3036 int ioctl_err
, ioctl_errno
, err
;
3038 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
3040 char prop_errbuf
[1024];
3041 const char *chopprefix
;
3042 boolean_t newfs
= B_FALSE
;
3043 boolean_t stream_wantsnewfs
;
3044 uint64_t parent_snapguid
= 0;
3045 prop_changelist_t
*clp
= NULL
;
3046 nvlist_t
*snapprops_nvlist
= NULL
;
3047 zprop_errflags_t prop_errflags
;
3048 boolean_t recursive
;
3049 char *snapname
= NULL
;
3051 begin_time
= time(NULL
);
3053 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3056 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
3059 if (stream_avl
!= NULL
) {
3060 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
3065 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
3067 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
3069 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
3071 if (flags
->canmountoff
) {
3072 VERIFY(0 == nvlist_add_uint64(props
,
3073 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
3075 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
3079 if (0 == nvlist_lookup_nvlist(fs
, "snapprops", &props
)) {
3080 VERIFY(0 == nvlist_lookup_nvlist(props
,
3081 snapname
, &snapprops_nvlist
));
3091 * Determine how much of the snapshot name stored in the stream
3092 * we are going to tack on to the name they specified on the
3093 * command line, and how much we are going to chop off.
3095 * If they specified a snapshot, chop the entire name stored in
3098 if (flags
->istail
) {
3100 * A filesystem was specified with -e. We want to tack on only
3101 * the tail of the sent snapshot path.
3103 if (strchr(tosnap
, '@')) {
3104 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3105 "argument - snapshot not allowed with -e"));
3106 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3109 chopprefix
= strrchr(sendfs
, '/');
3111 if (chopprefix
== NULL
) {
3113 * The tail is the poolname, so we need to
3114 * prepend a path separator.
3116 int len
= strlen(drrb
->drr_toname
);
3117 cp
= malloc(len
+ 2);
3119 (void) strcpy(&cp
[1], drrb
->drr_toname
);
3122 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
3124 } else if (flags
->isprefix
) {
3126 * A filesystem was specified with -d. We want to tack on
3127 * everything but the first element of the sent snapshot path
3128 * (all but the pool name).
3130 if (strchr(tosnap
, '@')) {
3131 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3132 "argument - snapshot not allowed with -d"));
3133 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3136 chopprefix
= strchr(drrb
->drr_toname
, '/');
3137 if (chopprefix
== NULL
)
3138 chopprefix
= strchr(drrb
->drr_toname
, '@');
3139 } else if (strchr(tosnap
, '@') == NULL
) {
3141 * If a filesystem was specified without -d or -e, we want to
3142 * tack on everything after the fs specified by 'zfs send'.
3144 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
3146 /* A snapshot was specified as an exact path (no -d or -e). */
3148 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3149 "cannot specify snapshot name for multi-snapshot "
3151 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3153 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
3156 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
3157 ASSERT(chopprefix
> drrb
->drr_toname
);
3158 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
3159 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
3160 chopprefix
[0] == '\0');
3163 * Determine name of destination snapshot, store in zc_value.
3165 (void) strcpy(zc
.zc_value
, tosnap
);
3166 (void) strncat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
3168 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
3169 zcmd_free_nvlists(&zc
);
3170 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3174 * Determine the name of the origin snapshot, store in zc_string.
3177 (void) strncpy(zc
.zc_string
, originsnap
, sizeof (zc
.zc_string
));
3179 (void) printf("using provided clone origin %s\n",
3181 } else if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
3182 if (guid_to_name(hdl
, zc
.zc_value
,
3183 drrb
->drr_fromguid
, B_FALSE
, zc
.zc_string
) != 0) {
3184 zcmd_free_nvlists(&zc
);
3185 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3186 "local origin for clone %s does not exist"),
3188 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3191 (void) printf("found clone origin %s\n", zc
.zc_string
);
3194 boolean_t resuming
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
3195 DMU_BACKUP_FEATURE_RESUMING
;
3196 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
3197 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
) && !resuming
;
3199 if (stream_wantsnewfs
) {
3201 * if the parent fs does not exist, look for it based on
3202 * the parent snap GUID
3204 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3205 "cannot receive new filesystem stream"));
3207 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
3208 cp
= strrchr(zc
.zc_name
, '/');
3212 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
3213 char suffix
[ZFS_MAX_DATASET_NAME_LEN
];
3214 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
3215 if (guid_to_name(hdl
, zc
.zc_name
, parent_snapguid
,
3216 B_FALSE
, zc
.zc_value
) == 0) {
3217 *strchr(zc
.zc_value
, '@') = '\0';
3218 (void) strcat(zc
.zc_value
, suffix
);
3223 * if the fs does not exist, look for it based on the
3226 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3227 "cannot receive incremental stream"));
3229 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
3230 *strchr(zc
.zc_name
, '@') = '\0';
3233 * If the exact receive path was specified and this is the
3234 * topmost path in the stream, then if the fs does not exist we
3235 * should look no further.
3237 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
3238 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
3239 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
3240 char snap
[ZFS_MAX_DATASET_NAME_LEN
];
3241 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
3242 if (guid_to_name(hdl
, zc
.zc_name
, drrb
->drr_fromguid
,
3243 B_FALSE
, zc
.zc_value
) == 0) {
3244 *strchr(zc
.zc_value
, '@') = '\0';
3245 (void) strcat(zc
.zc_value
, snap
);
3250 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
3251 *strchr(zc
.zc_name
, '@') = '\0';
3253 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
3257 * Destination fs exists. It must be one of these cases:
3258 * - an incremental send stream
3259 * - the stream specifies a new fs (full stream or clone)
3260 * and they want us to blow away the existing fs (and
3261 * have therefore specified -F and removed any snapshots)
3262 * - we are resuming a failed receive.
3264 if (stream_wantsnewfs
) {
3265 if (!flags
->force
) {
3266 zcmd_free_nvlists(&zc
);
3267 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3268 "destination '%s' exists\n"
3269 "must specify -F to overwrite it"),
3271 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3273 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
3275 zcmd_free_nvlists(&zc
);
3276 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3277 "destination has snapshots (eg. %s)\n"
3278 "must destroy them to overwrite it"),
3280 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3284 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
3285 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
3286 zcmd_free_nvlists(&zc
);
3290 if (stream_wantsnewfs
&&
3291 zhp
->zfs_dmustats
.dds_origin
[0]) {
3292 zcmd_free_nvlists(&zc
);
3294 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3295 "destination '%s' is a clone\n"
3296 "must destroy it to overwrite it"),
3298 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3301 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
3302 stream_wantsnewfs
) {
3303 /* We can't do online recv in this case */
3304 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
3307 zcmd_free_nvlists(&zc
);
3310 if (changelist_prefix(clp
) != 0) {
3311 changelist_free(clp
);
3313 zcmd_free_nvlists(&zc
);
3319 * If we are resuming a newfs, set newfs here so that we will
3320 * mount it if the recv succeeds this time. We can tell
3321 * that it was a newfs on the first recv because the fs
3322 * itself will be inconsistent (if the fs existed when we
3323 * did the first recv, we would have received it into
3326 if (resuming
&& zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
))
3332 * Destination filesystem does not exist. Therefore we better
3333 * be creating a new filesystem (either from a full backup, or
3334 * a clone). It would therefore be invalid if the user
3335 * specified only the pool name (i.e. if the destination name
3336 * contained no slash character).
3338 if (!stream_wantsnewfs
||
3339 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
3340 zcmd_free_nvlists(&zc
);
3341 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3342 "destination '%s' does not exist"), zc
.zc_name
);
3343 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3347 * Trim off the final dataset component so we perform the
3348 * recvbackup ioctl to the filesystems's parent.
3352 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
3353 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
3354 zcmd_free_nvlists(&zc
);
3355 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
3361 zc
.zc_begin_record
= *drr_noswap
;
3362 zc
.zc_cookie
= infd
;
3363 zc
.zc_guid
= flags
->force
;
3364 zc
.zc_resumable
= flags
->resumable
;
3365 if (flags
->verbose
) {
3366 (void) printf("%s %s stream of %s into %s\n",
3367 flags
->dryrun
? "would receive" : "receiving",
3368 drrb
->drr_fromguid
? "incremental" : "full",
3369 drrb
->drr_toname
, zc
.zc_value
);
3370 (void) fflush(stdout
);
3373 if (flags
->dryrun
) {
3374 zcmd_free_nvlists(&zc
);
3375 return (recv_skip(hdl
, infd
, flags
->byteswap
));
3378 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
3379 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
3380 zc
.zc_cleanup_fd
= cleanup_fd
;
3381 zc
.zc_action_handle
= *action_handlep
;
3383 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
3384 ioctl_errno
= errno
;
3385 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
3388 nvlist_t
*prop_errors
;
3389 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
3390 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
3392 nvpair_t
*prop_err
= NULL
;
3394 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
3395 prop_err
)) != NULL
) {
3400 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
3401 (void) nvpair_value_int32(prop_err
, &intval
);
3402 if (strcmp(nvpair_name(prop_err
),
3403 ZPROP_N_MORE_ERRORS
) == 0) {
3404 trunc_prop_errs(intval
);
3406 } else if (snapname
== NULL
|| finalsnap
== NULL
||
3407 strcmp(finalsnap
, snapname
) == 0 ||
3408 strcmp(nvpair_name(prop_err
),
3409 zfs_prop_to_name(ZFS_PROP_REFQUOTA
)) != 0) {
3411 * Skip the special case of, for example,
3412 * "refquota", errors on intermediate
3413 * snapshots leading up to a final one.
3414 * That's why we have all of the checks above.
3416 * See zfs_ioctl.c's extract_delay_props() for
3417 * a list of props which can fail on
3418 * intermediate snapshots, but shouldn't
3419 * affect the overall receive.
3421 (void) snprintf(tbuf
, sizeof (tbuf
),
3422 dgettext(TEXT_DOMAIN
,
3423 "cannot receive %s property on %s"),
3424 nvpair_name(prop_err
), zc
.zc_name
);
3425 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3428 nvlist_free(prop_errors
);
3431 zc
.zc_nvlist_dst
= 0;
3432 zc
.zc_nvlist_dst_size
= 0;
3433 zcmd_free_nvlists(&zc
);
3435 if (err
== 0 && snapprops_nvlist
) {
3436 zfs_cmd_t zc2
= { 0 };
3438 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
3439 zc2
.zc_cookie
= B_TRUE
; /* received */
3440 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
3441 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
3442 zcmd_free_nvlists(&zc2
);
3446 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3448 * It may be that this snapshot already exists,
3449 * in which case we want to consume & ignore it
3450 * rather than failing.
3452 avl_tree_t
*local_avl
;
3453 nvlist_t
*local_nv
, *fs
;
3454 cp
= strchr(zc
.zc_value
, '@');
3457 * XXX Do this faster by just iterating over snaps in
3458 * this fs. Also if zc_value does not exist, we will
3459 * get a strange "does not exist" error message.
3462 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
3463 B_FALSE
, &local_nv
, &local_avl
) == 0) {
3465 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3466 fsavl_destroy(local_avl
);
3467 nvlist_free(local_nv
);
3470 if (flags
->verbose
) {
3471 (void) printf("snap %s already exists; "
3472 "ignoring\n", zc
.zc_value
);
3474 err
= ioctl_err
= recv_skip(hdl
, infd
,
3481 if (ioctl_err
!= 0) {
3482 switch (ioctl_errno
) {
3484 cp
= strchr(zc
.zc_value
, '@');
3486 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3487 "most recent snapshot of %s does not\n"
3488 "match incremental source"), zc
.zc_value
);
3489 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3493 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3494 "destination %s has been modified\n"
3495 "since most recent snapshot"), zc
.zc_name
);
3496 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3499 cp
= strchr(zc
.zc_value
, '@');
3501 /* it's the containing fs that exists */
3504 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3505 "destination already exists"));
3506 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3507 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3512 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3515 recv_ecksum_set_aux(hdl
, zc
.zc_value
, flags
->resumable
);
3516 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3519 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3520 "pool must be upgraded to receive this stream."));
3521 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3524 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3525 "destination %s space quota exceeded"), zc
.zc_name
);
3526 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3529 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3534 * Mount the target filesystem (if created). Also mount any
3535 * children of the target filesystem if we did a replication
3536 * receive (indicated by stream_avl being non-NULL).
3538 cp
= strchr(zc
.zc_value
, '@');
3539 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3543 h
= zfs_open(hdl
, zc
.zc_value
,
3544 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3546 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3548 } else if (newfs
|| stream_avl
) {
3550 * Track the first/top of hierarchy fs,
3551 * for mounting and sharing later.
3553 if (top_zfs
&& *top_zfs
== NULL
)
3554 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
3562 if (!flags
->nomount
)
3563 err
|= changelist_postfix(clp
);
3564 changelist_free(clp
);
3567 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3568 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3569 "failed to clear unreceived properties on %s"),
3571 (void) fprintf(stderr
, "\n");
3573 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3574 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3575 "failed to restore original properties on %s"),
3577 (void) fprintf(stderr
, "\n");
3580 if (err
|| ioctl_err
)
3583 *action_handlep
= zc
.zc_action_handle
;
3585 if (flags
->verbose
) {
3588 uint64_t bytes
= zc
.zc_cookie
;
3589 time_t delta
= time(NULL
) - begin_time
;
3592 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3593 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3595 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3603 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
3604 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
3605 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3606 uint64_t *action_handlep
, const char *finalsnap
)
3609 dmu_replay_record_t drr
, drr_noswap
;
3610 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3612 zio_cksum_t zcksum
= { 0 };
3613 uint64_t featureflags
;
3616 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3619 if (flags
->isprefix
&&
3620 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3621 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3622 "(%s) does not exist"), tosnap
);
3623 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3626 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
3627 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
3628 "(%s) does not exist"), originsnap
);
3629 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3632 /* read in the BEGIN record */
3633 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3637 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3638 /* It's the double end record at the end of a package */
3642 /* the kernel needs the non-byteswapped begin record */
3645 flags
->byteswap
= B_FALSE
;
3646 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3648 * We computed the checksum in the wrong byteorder in
3649 * recv_read() above; do it again correctly.
3651 bzero(&zcksum
, sizeof (zio_cksum_t
));
3652 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3653 flags
->byteswap
= B_TRUE
;
3655 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3656 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3657 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3658 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3659 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3660 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3661 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3662 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3663 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3666 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3667 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3668 "stream (bad magic number)"));
3669 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3672 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3673 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3675 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3676 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3677 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3678 "stream has unsupported feature, feature flags = %lx"),
3680 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3683 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3684 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3685 "stream (bad snapshot name)"));
3686 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3689 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3690 char nonpackage_sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
3691 if (sendfs
== NULL
) {
3693 * We were not called from zfs_receive_package(). Get
3694 * the fs specified by 'zfs send'.
3697 (void) strlcpy(nonpackage_sendfs
,
3698 drr
.drr_u
.drr_begin
.drr_toname
,
3699 sizeof (nonpackage_sendfs
));
3700 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3702 sendfs
= nonpackage_sendfs
;
3703 VERIFY(finalsnap
== NULL
);
3705 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
3706 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
3707 cleanup_fd
, action_handlep
, finalsnap
));
3709 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3710 DMU_COMPOUNDSTREAM
);
3711 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
3712 &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3717 * Restores a backup of tosnap from the file descriptor specified by infd.
3718 * Return 0 on total success, -2 if some things couldn't be
3719 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3720 * (-1 will override -2, if -1 and the resumable flag was specified the
3721 * transfer can be resumed if the sending side supports it).
3724 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
3725 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
3727 char *top_zfs
= NULL
;
3730 uint64_t action_handle
= 0;
3731 char *originsnap
= NULL
;
3733 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
3734 if (err
&& err
!= ENOENT
)
3738 cleanup_fd
= open(ZFS_DEV
, O_RDWR
|O_EXCL
);
3739 VERIFY(cleanup_fd
>= 0);
3741 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
3742 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
, NULL
);
3744 VERIFY(0 == close(cleanup_fd
));
3746 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3748 prop_changelist_t
*clp
;
3750 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3752 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3753 CL_GATHER_MOUNT_ALWAYS
, 0);
3756 /* mount and share received datasets */
3757 err
= changelist_postfix(clp
);
3758 changelist_free(clp
);
3761 if (zhp
== NULL
|| clp
== NULL
|| err
)