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.
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
31 * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
44 #include <sys/mount.h>
45 #include <sys/mntent.h>
46 #include <sys/mnttab.h>
48 #include <sys/debug.h>
56 #include <libzfs_core.h>
59 #include "zfs_namecheck.h"
61 #include "zfs_fletcher.h"
62 #include "libzfs_impl.h"
64 #include <sys/zio_checksum.h>
65 #include <sys/dsl_crypt.h>
67 #include <sys/socket.h>
70 /* in libzfs_dataset.c */
71 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
73 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
74 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **, int,
75 uint64_t *, const char *, nvlist_t
*);
76 static int guid_to_name(libzfs_handle_t
*, const char *,
77 uint64_t, boolean_t
, char *);
79 static const zio_cksum_t zero_cksum
= { { 0 } };
81 typedef struct dedup_arg
{
84 libzfs_handle_t
*dedup_hdl
;
87 typedef struct progress_arg
{
90 boolean_t pa_parsable
;
93 typedef struct dataref
{
99 typedef struct dedup_entry
{
100 struct dedup_entry
*dde_next
;
101 zio_cksum_t dde_chksum
;
106 #define MAX_DDT_PHYSMEM_PERCENT 20
107 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
109 typedef struct dedup_table
{
110 dedup_entry_t
**dedup_hash_array
;
111 umem_cache_t
*ddecache
;
112 uint64_t max_ddt_size
; /* max dedup table size in bytes */
113 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
120 high_order_bit(uint64_t n
)
124 for (count
= 0; n
!= 0; count
++)
130 ssread(void *buf
, size_t len
, FILE *stream
)
134 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
141 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
142 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
146 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
147 if (ddt
->ddt_full
== B_FALSE
) {
148 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
149 "Dedup table full. Deduplication will continue "
150 "with existing table entries"));
151 ddt
->ddt_full
= B_TRUE
;
156 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
158 assert(*ddepp
== NULL
);
159 dde
->dde_next
= NULL
;
160 dde
->dde_chksum
= *cs
;
161 dde
->dde_prop
= prop
;
164 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
170 * Using the specified dedup table, do a lookup for an entry with
171 * the checksum cs. If found, return the block's reference info
172 * in *dr. Otherwise, insert a new entry in the dedup table, using
173 * the reference information specified by *dr.
175 * return value: true - entry was found
176 * false - entry was not found
179 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
180 uint64_t prop
, dataref_t
*dr
)
183 dedup_entry_t
**ddepp
;
185 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
187 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
188 ddepp
= &((*ddepp
)->dde_next
)) {
189 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
190 (*ddepp
)->dde_prop
== prop
) {
191 *dr
= (*ddepp
)->dde_ref
;
195 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
200 dump_record(dmu_replay_record_t
*drr
, void *payload
, int payload_len
,
201 zio_cksum_t
*zc
, int outfd
)
203 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
204 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
205 fletcher_4_incremental_native(drr
,
206 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
207 if (drr
->drr_type
!= DRR_BEGIN
) {
208 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
209 drr_checksum
.drr_checksum
));
210 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
212 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
213 sizeof (zio_cksum_t
), zc
);
214 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
216 if (payload_len
!= 0) {
217 fletcher_4_incremental_native(payload
, payload_len
, zc
);
218 if (write(outfd
, payload
, payload_len
) == -1)
225 * This function is started in a separate thread when the dedup option
226 * has been requested. The main send thread determines the list of
227 * snapshots to be included in the send stream and makes the ioctl calls
228 * for each one. But instead of having the ioctl send the output to the
229 * the output fd specified by the caller of zfs_send()), the
230 * ioctl is told to direct the output to a pipe, which is read by the
231 * alternate thread running THIS function. This function does the
233 * 1. building a dedup table (the DDT)
234 * 2. doing checksums on each data block and inserting a record in the DDT
235 * 3. looking for matching checksums, and
236 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
237 * a duplicate block is found.
238 * The output of this function then goes to the output fd requested
239 * by the caller of zfs_send().
244 dedup_arg_t
*dda
= arg
;
245 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
246 dmu_replay_record_t thedrr
= { 0 };
247 dmu_replay_record_t
*drr
= &thedrr
;
251 zio_cksum_t stream_cksum
;
255 ddt
.max_ddt_size
= SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20;
257 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
259 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
) / 100,
260 SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20);
263 numbuckets
= ddt
.max_ddt_size
/ (sizeof (dedup_entry_t
));
266 * numbuckets must be a power of 2. Increase number to
267 * a power of 2 if necessary.
269 if (!ISP2(numbuckets
))
270 numbuckets
= 1ULL << high_order_bit(numbuckets
);
272 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
273 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
274 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
275 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
276 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
277 ddt
.ddt_full
= B_FALSE
;
279 outfd
= dda
->outputfd
;
280 ofp
= fdopen(dda
->inputfd
, "r");
281 while (ssread(drr
, sizeof (*drr
), ofp
) != 0) {
284 * kernel filled in checksum, we are going to write same
285 * record, but need to regenerate checksum.
287 if (drr
->drr_type
!= DRR_BEGIN
) {
288 bzero(&drr
->drr_u
.drr_checksum
.drr_checksum
,
289 sizeof (drr
->drr_u
.drr_checksum
.drr_checksum
));
292 switch (drr
->drr_type
) {
295 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
298 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
300 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
302 /* set the DEDUP feature flag for this stream */
303 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
304 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
305 DMU_BACKUP_FEATURE_DEDUPPROPS
);
306 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
308 if (drr
->drr_payloadlen
!= 0) {
309 sz
= drr
->drr_payloadlen
;
311 if (sz
> SPA_MAXBLOCKSIZE
) {
312 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
313 SPA_MAXBLOCKSIZE
, sz
);
315 (void) ssread(buf
, sz
, ofp
);
319 if (dump_record(drr
, buf
, sz
, &stream_cksum
,
327 struct drr_end
*drre
= &drr
->drr_u
.drr_end
;
328 /* use the recalculated checksum */
329 drre
->drr_checksum
= stream_cksum
;
330 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
338 struct drr_object
*drro
= &drr
->drr_u
.drr_object
;
339 if (drro
->drr_bonuslen
> 0) {
341 DRR_OBJECT_PAYLOAD_SIZE(drro
), ofp
);
343 if (dump_record(drr
, buf
, DRR_OBJECT_PAYLOAD_SIZE(drro
),
344 &stream_cksum
, outfd
) != 0)
351 struct drr_spill
*drrs
= &drr
->drr_u
.drr_spill
;
352 (void) ssread(buf
, DRR_SPILL_PAYLOAD_SIZE(drrs
), ofp
);
353 if (dump_record(drr
, buf
, DRR_SPILL_PAYLOAD_SIZE(drrs
),
354 &stream_cksum
, outfd
) != 0)
359 case DRR_FREEOBJECTS
:
361 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
369 struct drr_write
*drrw
= &drr
->drr_u
.drr_write
;
371 uint64_t payload_size
;
373 payload_size
= DRR_WRITE_PAYLOAD_SIZE(drrw
);
374 (void) ssread(buf
, payload_size
, ofp
);
377 * Use the existing checksum if it's dedup-capable,
378 * else calculate a SHA256 checksum for it.
381 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
383 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_flags
)) {
385 zio_cksum_t tmpsha256
;
387 SHA2Init(SHA256
, &ctx
);
388 SHA2Update(&ctx
, buf
, payload_size
);
389 SHA2Final(&tmpsha256
, &ctx
);
391 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
392 BE_64(tmpsha256
.zc_word
[0]);
393 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
394 BE_64(tmpsha256
.zc_word
[1]);
395 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
396 BE_64(tmpsha256
.zc_word
[2]);
397 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
398 BE_64(tmpsha256
.zc_word
[3]);
399 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
400 drrw
->drr_flags
|= DRR_CHECKSUM_DEDUP
;
403 dataref
.ref_guid
= drrw
->drr_toguid
;
404 dataref
.ref_object
= drrw
->drr_object
;
405 dataref
.ref_offset
= drrw
->drr_offset
;
407 if (ddt_update(dda
->dedup_hdl
, &ddt
,
408 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
410 dmu_replay_record_t wbr_drr
= {0};
411 struct drr_write_byref
*wbr_drrr
=
412 &wbr_drr
.drr_u
.drr_write_byref
;
414 /* block already present in stream */
415 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
417 wbr_drrr
->drr_object
= drrw
->drr_object
;
418 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
419 wbr_drrr
->drr_length
= drrw
->drr_logical_size
;
420 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
421 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
422 wbr_drrr
->drr_refobject
=
424 wbr_drrr
->drr_refoffset
=
427 wbr_drrr
->drr_checksumtype
=
428 drrw
->drr_checksumtype
;
429 wbr_drrr
->drr_flags
= drrw
->drr_flags
;
430 wbr_drrr
->drr_key
.ddk_cksum
=
431 drrw
->drr_key
.ddk_cksum
;
432 wbr_drrr
->drr_key
.ddk_prop
=
433 drrw
->drr_key
.ddk_prop
;
435 if (dump_record(&wbr_drr
, NULL
, 0,
436 &stream_cksum
, outfd
) != 0)
439 /* block not previously seen */
440 if (dump_record(drr
, buf
, payload_size
,
441 &stream_cksum
, outfd
) != 0)
447 case DRR_WRITE_EMBEDDED
:
449 struct drr_write_embedded
*drrwe
=
450 &drr
->drr_u
.drr_write_embedded
;
452 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
453 if (dump_record(drr
, buf
,
454 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
455 &stream_cksum
, outfd
) != 0)
462 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
468 case DRR_OBJECT_RANGE
:
470 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
477 (void) fprintf(stderr
, "INVALID record type 0x%x\n",
479 /* should never happen, so assert */
484 umem_cache_destroy(ddt
.ddecache
);
485 free(ddt
.dedup_hash_array
);
493 * Routines for dealing with the AVL tree of fs-nvlists
495 typedef struct fsavl_node
{
503 fsavl_compare(const void *arg1
, const void *arg2
)
505 const fsavl_node_t
*fn1
= (const fsavl_node_t
*)arg1
;
506 const fsavl_node_t
*fn2
= (const fsavl_node_t
*)arg2
;
508 return (AVL_CMP(fn1
->fn_guid
, fn2
->fn_guid
));
512 * Given the GUID of a snapshot, find its containing filesystem and
516 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
518 fsavl_node_t fn_find
;
521 fn_find
.fn_guid
= snapguid
;
523 fn
= avl_find(avl
, &fn_find
, NULL
);
526 *snapname
= fn
->fn_snapname
;
527 return (fn
->fn_nvfs
);
533 fsavl_destroy(avl_tree_t
*avl
)
542 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
549 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
552 fsavl_create(nvlist_t
*fss
)
555 nvpair_t
*fselem
= NULL
;
557 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
560 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
561 offsetof(fsavl_node_t
, fn_node
));
563 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
564 nvlist_t
*nvfs
, *snaps
;
565 nvpair_t
*snapelem
= NULL
;
567 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
568 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
571 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
575 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
576 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
577 fsavl_destroy(fsavl
);
581 fn
->fn_snapname
= nvpair_name(snapelem
);
585 * Note: if there are multiple snaps with the
586 * same GUID, we ignore all but one.
588 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
599 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
601 typedef struct send_data
{
603 * assigned inside every recursive call,
604 * restored from *_save on return:
606 * guid of fromsnap snapshot in parent dataset
607 * txg of fromsnap snapshot in current dataset
608 * txg of tosnap snapshot in current dataset
611 uint64_t parent_fromsnap_guid
;
612 uint64_t fromsnap_txg
;
615 /* the nvlists get accumulated during depth-first traversal */
616 nvlist_t
*parent_snaps
;
620 /* send-receive configuration, does not change during traversal */
622 const char *fromsnap
;
632 * The header nvlist is of the following format:
635 * "fromsnap" -> string (if incremental)
639 * "name" -> string (full name; for debugging)
640 * "parentfromsnap" -> number (guid of fromsnap in parent)
642 * "props" -> { name -> value (only if set here) }
643 * "snaps" -> { name (lastname) -> number (guid) }
644 * "snapprops" -> { name (lastname) -> { name -> value } }
646 * "origin" -> number (guid) (if clone)
647 * "is_encroot" -> boolean
648 * "sent" -> boolean (not on-disk)
657 send_iterate_prop(zfs_handle_t
*zhp
, boolean_t received_only
, nvlist_t
*nv
);
660 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
662 send_data_t
*sd
= arg
;
663 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
664 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
667 boolean_t isfromsnap
, istosnap
, istosnapwithnofrom
;
669 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
670 isfromsnap
= (sd
->fromsnap
!= NULL
&&
671 strcmp(sd
->fromsnap
, snapname
) == 0);
672 istosnap
= (sd
->tosnap
!= NULL
&& (strcmp(sd
->tosnap
, snapname
) == 0));
673 istosnapwithnofrom
= (istosnap
&& sd
->fromsnap
== NULL
);
675 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
677 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
678 "skipping snapshot %s because it was created "
679 "after the destination snapshot (%s)\n"),
680 zhp
->zfs_name
, sd
->tosnap
);
686 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
688 * NB: if there is no fromsnap here (it's a newly created fs in
689 * an incremental replication), we will substitute the tosnap.
691 if (isfromsnap
|| (sd
->parent_fromsnap_guid
== 0 && istosnap
)) {
692 sd
->parent_fromsnap_guid
= guid
;
695 if (!sd
->recursive
) {
696 if (!sd
->seenfrom
&& isfromsnap
) {
697 sd
->seenfrom
= B_TRUE
;
702 if ((sd
->seento
|| !sd
->seenfrom
) && !istosnapwithnofrom
) {
711 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
712 send_iterate_prop(zhp
, sd
->backup
, nv
);
713 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
721 send_iterate_prop(zfs_handle_t
*zhp
, boolean_t received_only
, nvlist_t
*nv
)
723 nvlist_t
*props
= NULL
;
724 nvpair_t
*elem
= NULL
;
727 props
= zfs_get_recvd_props(zhp
);
729 props
= zhp
->zfs_props
;
731 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
732 char *propname
= nvpair_name(elem
);
733 zfs_prop_t prop
= zfs_name_to_prop(propname
);
736 if (!zfs_prop_user(propname
)) {
738 * Realistically, this should never happen. However,
739 * we want the ability to add DSL properties without
740 * needing to make incompatible version changes. We
741 * need to ignore unknown properties to allow older
742 * software to still send datasets containing these
743 * properties, with the unknown properties elided.
745 if (prop
== ZPROP_INVAL
)
748 if (zfs_prop_readonly(prop
))
752 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
753 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
754 prop
== ZFS_PROP_REFQUOTA
||
755 prop
== ZFS_PROP_REFRESERVATION
) {
758 verify(nvlist_lookup_uint64(propnv
,
759 ZPROP_VALUE
, &value
) == 0);
760 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
763 * May have no source before SPA_VERSION_RECVD_PROPS,
764 * but is still modifiable.
766 if (nvlist_lookup_string(propnv
,
767 ZPROP_SOURCE
, &source
) == 0) {
768 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
770 ZPROP_SOURCE_VAL_RECVD
) != 0))
775 if (nvlist_lookup_string(propnv
,
776 ZPROP_SOURCE
, &source
) != 0)
778 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
779 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
783 if (zfs_prop_user(propname
) ||
784 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
786 verify(nvlist_lookup_string(propnv
,
787 ZPROP_VALUE
, &value
) == 0);
788 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
791 verify(nvlist_lookup_uint64(propnv
,
792 ZPROP_VALUE
, &value
) == 0);
793 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
799 * returns snapshot creation txg
800 * and returns 0 if the snapshot does not exist
803 get_snap_txg(libzfs_handle_t
*hdl
, const char *fs
, const char *snap
)
805 char name
[ZFS_MAX_DATASET_NAME_LEN
];
808 if (fs
== NULL
|| fs
[0] == '\0' || snap
== NULL
|| snap
[0] == '\0')
811 (void) snprintf(name
, sizeof (name
), "%s@%s", fs
, snap
);
812 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_SNAPSHOT
)) {
813 zfs_handle_t
*zhp
= zfs_open(hdl
, name
, ZFS_TYPE_SNAPSHOT
);
815 txg
= zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
);
824 * recursively generate nvlists describing datasets. See comment
825 * for the data structure send_data_t above for description of contents
829 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
831 send_data_t
*sd
= arg
;
832 nvlist_t
*nvfs
= NULL
, *nv
= NULL
;
834 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
835 uint64_t fromsnap_txg_save
= sd
->fromsnap_txg
;
836 uint64_t tosnap_txg_save
= sd
->tosnap_txg
;
837 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
838 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
839 uint64_t fromsnap_txg
, tosnap_txg
;
842 fromsnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->fromsnap
);
843 if (fromsnap_txg
!= 0)
844 sd
->fromsnap_txg
= fromsnap_txg
;
846 tosnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->tosnap
);
848 sd
->tosnap_txg
= tosnap_txg
;
851 * on the send side, if the current dataset does not have tosnap,
852 * perform two additional checks:
854 * - skip sending the current dataset if it was created later than
856 * - return error if the current dataset was created earlier than
859 if (sd
->tosnap
!= NULL
&& tosnap_txg
== 0) {
860 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
862 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
863 "skipping dataset %s: snapshot %s does "
864 "not exist\n"), zhp
->zfs_name
, sd
->tosnap
);
867 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
868 "cannot send %s@%s%s: snapshot %s@%s does not "
869 "exist\n"), sd
->fsname
, sd
->tosnap
, sd
->recursive
?
870 dgettext(TEXT_DOMAIN
, " recursively") : "",
871 zhp
->zfs_name
, sd
->tosnap
);
877 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
878 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
879 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
880 sd
->parent_fromsnap_guid
));
882 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
883 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
884 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
885 if (origin
== NULL
) {
889 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
890 origin
->zfs_dmustats
.dds_guid
));
895 /* iterate over props */
896 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
897 send_iterate_prop(zhp
, sd
->backup
, nv
);
899 if (zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
) {
902 /* determine if this dataset is an encryption root */
903 if (zfs_crypto_get_encryption_root(zhp
, &encroot
, NULL
) != 0) {
909 VERIFY(0 == nvlist_add_boolean(nvfs
, "is_encroot"));
912 * Encrypted datasets can only be sent with properties if
913 * the raw flag is specified because the receive side doesn't
914 * currently have a mechanism for recursively asking the user
915 * for new encryption parameters.
918 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
919 "cannot send %s@%s: encrypted dataset %s may not "
920 "be sent with properties without the raw flag\n"),
921 sd
->fsname
, sd
->tosnap
, zhp
->zfs_name
);
928 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
930 /* iterate over snaps, and set sd->parent_fromsnap_guid */
931 sd
->parent_fromsnap_guid
= 0;
932 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
933 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
934 (void) zfs_iter_snapshots_sorted(zhp
, send_iterate_snap
, sd
);
935 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
936 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
937 nvlist_free(sd
->parent_snaps
);
938 nvlist_free(sd
->snapprops
);
940 /* add this fs to nvlist */
941 (void) snprintf(guidstring
, sizeof (guidstring
),
942 "0x%llx", (longlong_t
)guid
);
943 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
945 /* iterate over children */
947 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
950 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
951 sd
->fromsnap_txg
= fromsnap_txg_save
;
952 sd
->tosnap_txg
= tosnap_txg_save
;
961 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
962 const char *tosnap
, boolean_t recursive
, boolean_t raw
, boolean_t verbose
,
963 boolean_t backup
, nvlist_t
**nvlp
, avl_tree_t
**avlp
)
966 send_data_t sd
= { 0 };
969 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
971 return (EZFS_BADTYPE
);
973 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
975 sd
.fromsnap
= fromsnap
;
977 sd
.recursive
= recursive
;
979 sd
.verbose
= verbose
;
982 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
990 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
1001 * Routines specific to "zfs send"
1003 typedef struct send_dump_data
{
1004 /* these are all just the short snapname (the part after the @) */
1005 const char *fromsnap
;
1007 char prevsnap
[ZFS_MAX_DATASET_NAME_LEN
];
1008 uint64_t prevsnap_obj
;
1009 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
1010 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
, std_out
;
1011 boolean_t large_block
, compress
, raw
;
1015 nvlist_t
*snapholds
;
1017 snapfilter_cb_t
*filter_cb
;
1018 void *filter_cb_arg
;
1020 char holdtag
[ZFS_MAX_DATASET_NAME_LEN
];
1026 zfs_send_space(zfs_handle_t
*zhp
, const char *snapname
, const char *from
,
1027 enum lzc_send_flags flags
, uint64_t *spacep
)
1029 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1032 assert(snapname
!= NULL
);
1033 error
= lzc_send_space(snapname
, from
, flags
, spacep
);
1037 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1038 "warning: cannot estimate space for '%s'"), snapname
);
1042 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1043 "not an earlier snapshot from the same fs"));
1044 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1047 if (zfs_dataset_exists(hdl
, snapname
,
1048 ZFS_TYPE_SNAPSHOT
)) {
1049 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1050 "incremental source (%s) does not exist"),
1053 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1067 zfs_error_aux(hdl
, strerror(error
));
1068 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1071 return (zfs_standard_error(hdl
, error
, errbuf
));
1079 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1080 * NULL) to the file descriptor specified by outfd.
1083 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
1084 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
1087 zfs_cmd_t zc
= {"\0"};
1088 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1091 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1092 assert(fromsnap_obj
== 0 || !fromorigin
);
1094 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1095 zc
.zc_cookie
= outfd
;
1096 zc
.zc_obj
= fromorigin
;
1097 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1098 zc
.zc_fromobj
= fromsnap_obj
;
1099 zc
.zc_flags
= flags
;
1101 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
1102 if (fromsnap
&& fromsnap
[0] != '\0') {
1103 VERIFY(0 == nvlist_add_string(thisdbg
,
1104 "fromsnap", fromsnap
));
1107 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
1109 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1110 "warning: cannot send '%s'"), zhp
->zfs_name
);
1112 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
1114 VERIFY(0 == nvlist_add_nvlist(debugnv
,
1115 zhp
->zfs_name
, thisdbg
));
1117 nvlist_free(thisdbg
);
1121 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1122 "not an earlier snapshot from the same fs"));
1123 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1126 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1127 "source key must be loaded"));
1128 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
1131 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
1132 ZFS_TYPE_SNAPSHOT
)) {
1133 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1134 "incremental source (@%s) does not exist"),
1137 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1150 zfs_error_aux(hdl
, strerror(errno
));
1151 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1154 return (zfs_standard_error(hdl
, errno
, errbuf
));
1159 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
1160 nvlist_free(thisdbg
);
1166 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
1168 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1171 * zfs_send() only sets snapholds for sends that need them,
1172 * e.g. replication and doall.
1174 if (sdd
->snapholds
== NULL
)
1177 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1181 send_progress_thread(void *arg
)
1183 progress_arg_t
*pa
= arg
;
1184 zfs_cmd_t zc
= {"\0"};
1185 zfs_handle_t
*zhp
= pa
->pa_zhp
;
1186 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1187 unsigned long long bytes
;
1192 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1194 if (!pa
->pa_parsable
)
1195 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1198 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1203 zc
.zc_cookie
= pa
->pa_fd
;
1204 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1205 return ((void *)-1);
1209 bytes
= zc
.zc_cookie
;
1211 if (pa
->pa_parsable
) {
1212 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1213 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1214 bytes
, zhp
->zfs_name
);
1216 zfs_nicebytes(bytes
, buf
, sizeof (buf
));
1217 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1218 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1219 buf
, zhp
->zfs_name
);
1225 send_print_verbose(FILE *fout
, const char *tosnap
, const char *fromsnap
,
1226 uint64_t size
, boolean_t parsable
)
1229 if (fromsnap
!= NULL
) {
1230 (void) fprintf(fout
, "incremental\t%s\t%s",
1233 (void) fprintf(fout
, "full\t%s",
1237 if (fromsnap
!= NULL
) {
1238 if (strchr(fromsnap
, '@') == NULL
&&
1239 strchr(fromsnap
, '#') == NULL
) {
1240 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1241 "send from @%s to %s"),
1244 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1245 "send from %s to %s"),
1249 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1256 (void) fprintf(fout
, "\t%llu",
1258 } else if (size
!= 0) {
1260 zfs_nicebytes(size
, buf
, sizeof (buf
));
1261 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1262 " estimated size is %s"), buf
);
1264 (void) fprintf(fout
, "\n");
1268 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1270 send_dump_data_t
*sdd
= arg
;
1271 progress_arg_t pa
= { 0 };
1274 enum lzc_send_flags flags
= 0;
1276 boolean_t isfromsnap
, istosnap
, fromorigin
;
1277 boolean_t exclude
= B_FALSE
;
1278 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1281 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1282 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1283 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1285 if (!sdd
->seenfrom
&& isfromsnap
) {
1286 gather_holds(zhp
, sdd
);
1287 sdd
->seenfrom
= B_TRUE
;
1288 (void) strlcpy(sdd
->prevsnap
, thissnap
,
1289 sizeof (sdd
->prevsnap
));
1290 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1295 if (sdd
->seento
|| !sdd
->seenfrom
) {
1300 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1302 sdd
->seento
= B_TRUE
;
1304 if (sdd
->large_block
)
1305 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1306 if (sdd
->embed_data
)
1307 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1309 flags
|= LZC_SEND_FLAG_COMPRESS
;
1311 flags
|= LZC_SEND_FLAG_RAW
;
1313 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1314 if (sdd
->replicate
) {
1316 nvlist_t
*snapprops
;
1318 * Filter out all intermediate snapshots except origin
1319 * snapshots needed to replicate clones.
1321 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1322 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1324 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1325 "snapprops", &snapprops
));
1326 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1327 thissnap
, &snapprops
));
1328 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1335 * If a filter function exists, call it to determine whether
1336 * this snapshot will be sent.
1338 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1339 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1341 * This snapshot is filtered out. Don't send it, and don't
1342 * set prevsnap_obj, so it will be as if this snapshot didn't
1343 * exist, and the next accepted snapshot will be sent as
1344 * an incremental from the last accepted one, or as the
1345 * first (and full) snapshot in the case of a replication,
1346 * non-incremental send.
1352 gather_holds(zhp
, sdd
);
1353 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1354 (sdd
->fromorigin
|| sdd
->replicate
);
1358 char fromds
[ZFS_MAX_DATASET_NAME_LEN
];
1360 if (sdd
->prevsnap
[0] != '\0') {
1361 (void) strlcpy(fromds
, zhp
->zfs_name
, sizeof (fromds
));
1362 *(strchr(fromds
, '@') + 1) = '\0';
1363 (void) strlcat(fromds
, sdd
->prevsnap
, sizeof (fromds
));
1365 if (zfs_send_space(zhp
, zhp
->zfs_name
,
1366 sdd
->prevsnap
[0] ? fromds
: NULL
, flags
, &size
) != 0) {
1367 size
= 0; /* cannot estimate send space */
1369 send_print_verbose(fout
, zhp
->zfs_name
,
1370 sdd
->prevsnap
[0] ? sdd
->prevsnap
: NULL
,
1371 size
, sdd
->parsable
);
1378 * If progress reporting is requested, spawn a new thread to
1379 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1381 if (sdd
->progress
) {
1383 pa
.pa_fd
= sdd
->outfd
;
1384 pa
.pa_parsable
= sdd
->parsable
;
1386 if ((err
= pthread_create(&tid
, NULL
,
1387 send_progress_thread
, &pa
)) != 0) {
1393 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1394 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1396 if (sdd
->progress
) {
1397 (void) pthread_cancel(tid
);
1398 (void) pthread_join(tid
, NULL
);
1402 (void) strcpy(sdd
->prevsnap
, thissnap
);
1403 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1409 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1412 send_dump_data_t
*sdd
= arg
;
1413 boolean_t missingfrom
= B_FALSE
;
1414 zfs_cmd_t zc
= {"\0"};
1416 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1417 zhp
->zfs_name
, sdd
->tosnap
);
1418 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1419 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1420 "WARNING: could not send %s@%s: does not exist\n"),
1421 zhp
->zfs_name
, sdd
->tosnap
);
1426 if (sdd
->replicate
&& sdd
->fromsnap
) {
1428 * If this fs does not have fromsnap, and we're doing
1429 * recursive, we need to send a full stream from the
1430 * beginning (or an incremental from the origin if this
1431 * is a clone). If we're doing non-recursive, then let
1432 * them get the error.
1434 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1435 zhp
->zfs_name
, sdd
->fromsnap
);
1436 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1437 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1438 missingfrom
= B_TRUE
;
1442 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1443 sdd
->prevsnap_obj
= 0;
1444 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1445 sdd
->seenfrom
= B_TRUE
;
1447 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1448 if (!sdd
->seenfrom
) {
1449 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1450 "WARNING: could not send %s@%s:\n"
1451 "incremental source (%s@%s) does not exist\n"),
1452 zhp
->zfs_name
, sdd
->tosnap
,
1453 zhp
->zfs_name
, sdd
->fromsnap
);
1455 } else if (!sdd
->seento
) {
1456 if (sdd
->fromsnap
) {
1457 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1458 "WARNING: could not send %s@%s:\n"
1459 "incremental source (%s@%s) "
1460 "is not earlier than it\n"),
1461 zhp
->zfs_name
, sdd
->tosnap
,
1462 zhp
->zfs_name
, sdd
->fromsnap
);
1464 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1466 "could not send %s@%s: does not exist\n"),
1467 zhp
->zfs_name
, sdd
->tosnap
);
1476 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1478 send_dump_data_t
*sdd
= arg
;
1480 boolean_t needagain
, progress
;
1482 if (!sdd
->replicate
)
1483 return (dump_filesystem(rzhp
, sdd
));
1485 /* Mark the clone origin snapshots. */
1486 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1487 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1489 uint64_t origin_guid
= 0;
1491 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1492 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1493 if (origin_guid
!= 0) {
1495 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1496 origin_guid
, &snapname
);
1497 if (origin_nv
!= NULL
) {
1498 nvlist_t
*snapprops
;
1499 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1500 "snapprops", &snapprops
));
1501 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1502 snapname
, &snapprops
));
1503 VERIFY(0 == nvlist_add_boolean(
1504 snapprops
, "is_clone_origin"));
1509 needagain
= progress
= B_FALSE
;
1510 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1511 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1512 nvlist_t
*fslist
, *parent_nv
;
1516 uint64_t origin_guid
= 0;
1517 uint64_t parent_guid
= 0;
1519 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1520 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1523 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1524 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1525 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1528 if (parent_guid
!= 0) {
1529 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1530 if (!nvlist_exists(parent_nv
, "sent")) {
1531 /* parent has not been sent; skip this one */
1537 if (origin_guid
!= 0) {
1538 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1540 if (origin_nv
!= NULL
&&
1541 !nvlist_exists(origin_nv
, "sent")) {
1543 * origin has not been sent yet;
1551 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1554 err
= dump_filesystem(zhp
, sdd
);
1555 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1566 /* clean out the sent flags in case we reuse this fss */
1567 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1568 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1571 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1572 (void) nvlist_remove_all(fslist
, "sent");
1579 zfs_send_resume_token_to_nvlist(libzfs_handle_t
*hdl
, const char *token
)
1581 unsigned int version
;
1583 unsigned long long checksum
, packed_len
;
1586 * Decode token header, which is:
1587 * <token version>-<checksum of payload>-<uncompressed payload length>
1588 * Note that the only supported token version is 1.
1590 nread
= sscanf(token
, "%u-%llx-%llx-",
1591 &version
, &checksum
, &packed_len
);
1593 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1594 "resume token is corrupt (invalid format)"));
1598 if (version
!= ZFS_SEND_RESUME_TOKEN_VERSION
) {
1599 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1600 "resume token is corrupt (invalid version %u)"),
1605 /* convert hexadecimal representation to binary */
1606 token
= strrchr(token
, '-') + 1;
1607 int len
= strlen(token
) / 2;
1608 unsigned char *compressed
= zfs_alloc(hdl
, len
);
1609 for (i
= 0; i
< len
; i
++) {
1610 nread
= sscanf(token
+ i
* 2, "%2hhx", compressed
+ i
);
1613 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1614 "resume token is corrupt "
1615 "(payload is not hex-encoded)"));
1620 /* verify checksum */
1622 fletcher_4_native_varsize(compressed
, len
, &cksum
);
1623 if (cksum
.zc_word
[0] != checksum
) {
1625 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1626 "resume token is corrupt (incorrect checksum)"));
1631 void *packed
= zfs_alloc(hdl
, packed_len
);
1632 uLongf packed_len_long
= packed_len
;
1633 if (uncompress(packed
, &packed_len_long
, compressed
, len
) != Z_OK
||
1634 packed_len_long
!= packed_len
) {
1637 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1638 "resume token is corrupt (decompression failed)"));
1644 int error
= nvlist_unpack(packed
, packed_len
, &nv
, KM_SLEEP
);
1648 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1649 "resume token is corrupt (nvlist_unpack failed)"));
1656 zfs_send_resume(libzfs_handle_t
*hdl
, sendflags_t
*flags
, int outfd
,
1657 const char *resume_token
)
1661 char *fromname
= NULL
;
1662 uint64_t resumeobj
, resumeoff
, toguid
, fromguid
, bytes
;
1665 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1666 enum lzc_send_flags lzc_flags
= 0;
1667 FILE *fout
= (flags
->verbose
&& flags
->dryrun
) ? stdout
: stderr
;
1669 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1670 "cannot resume send"));
1672 nvlist_t
*resume_nvl
=
1673 zfs_send_resume_token_to_nvlist(hdl
, resume_token
);
1674 if (resume_nvl
== NULL
) {
1676 * zfs_error_aux has already been set by
1677 * zfs_send_resume_token_to_nvlist
1679 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1681 if (flags
->verbose
) {
1682 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1683 "resume token contents:\n"));
1684 nvlist_print(fout
, resume_nvl
);
1687 if (nvlist_lookup_string(resume_nvl
, "toname", &toname
) != 0 ||
1688 nvlist_lookup_uint64(resume_nvl
, "object", &resumeobj
) != 0 ||
1689 nvlist_lookup_uint64(resume_nvl
, "offset", &resumeoff
) != 0 ||
1690 nvlist_lookup_uint64(resume_nvl
, "bytes", &bytes
) != 0 ||
1691 nvlist_lookup_uint64(resume_nvl
, "toguid", &toguid
) != 0) {
1692 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1693 "resume token is corrupt"));
1694 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1697 (void) nvlist_lookup_uint64(resume_nvl
, "fromguid", &fromguid
);
1699 if (flags
->largeblock
|| nvlist_exists(resume_nvl
, "largeblockok"))
1700 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1701 if (flags
->embed_data
|| nvlist_exists(resume_nvl
, "embedok"))
1702 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1703 if (flags
->compress
|| nvlist_exists(resume_nvl
, "compressok"))
1704 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
1705 if (flags
->raw
|| nvlist_exists(resume_nvl
, "rawok"))
1706 lzc_flags
|= LZC_SEND_FLAG_RAW
;
1708 if (guid_to_name(hdl
, toname
, toguid
, B_FALSE
, name
) != 0) {
1709 if (zfs_dataset_exists(hdl
, toname
, ZFS_TYPE_DATASET
)) {
1710 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1711 "'%s' is no longer the same snapshot used in "
1712 "the initial send"), toname
);
1714 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1715 "'%s' used in the initial send no longer exists"),
1718 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1720 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1722 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1723 "unable to access '%s'"), name
);
1724 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1727 if (fromguid
!= 0) {
1728 if (guid_to_name(hdl
, toname
, fromguid
, B_TRUE
, name
) != 0) {
1729 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1730 "incremental source %#llx no longer exists"),
1731 (longlong_t
)fromguid
);
1732 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1737 if (flags
->verbose
) {
1739 error
= lzc_send_space(zhp
->zfs_name
, fromname
,
1742 size
= MAX(0, (int64_t)(size
- bytes
));
1743 send_print_verbose(fout
, zhp
->zfs_name
, fromname
,
1744 size
, flags
->parsable
);
1747 if (!flags
->dryrun
) {
1748 progress_arg_t pa
= { 0 };
1751 * If progress reporting is requested, spawn a new thread to
1752 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1754 if (flags
->progress
) {
1757 pa
.pa_parsable
= flags
->parsable
;
1759 error
= pthread_create(&tid
, NULL
,
1760 send_progress_thread
, &pa
);
1767 error
= lzc_send_resume(zhp
->zfs_name
, fromname
, outfd
,
1768 lzc_flags
, resumeobj
, resumeoff
);
1770 if (flags
->progress
) {
1771 (void) pthread_cancel(tid
);
1772 (void) pthread_join(tid
, NULL
);
1776 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1777 "warning: cannot send '%s'"), zhp
->zfs_name
);
1785 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1786 "source key must be loaded"));
1787 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
1802 zfs_error_aux(hdl
, strerror(errno
));
1803 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1806 return (zfs_standard_error(hdl
, errno
, errbuf
));
1817 * Generate a send stream for the dataset identified by the argument zhp.
1819 * The content of the send stream is the snapshot identified by
1820 * 'tosnap'. Incremental streams are requested in two ways:
1821 * - from the snapshot identified by "fromsnap" (if non-null) or
1822 * - from the origin of the dataset identified by zhp, which must
1823 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1826 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1827 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1828 * if "replicate" is set. If "doall" is set, dump all the intermediate
1829 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1830 * case too. If "props" is set, send properties.
1833 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1834 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1835 void *cb_arg
, nvlist_t
**debugnvp
)
1838 send_dump_data_t sdd
= { 0 };
1840 nvlist_t
*fss
= NULL
;
1841 avl_tree_t
*fsavl
= NULL
;
1842 static uint64_t holdseq
;
1846 dedup_arg_t dda
= { 0 };
1847 int featureflags
= 0;
1850 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1851 "cannot send '%s'"), zhp
->zfs_name
);
1853 if (fromsnap
&& fromsnap
[0] == '\0') {
1854 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1855 "zero-length incremental source"));
1856 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1859 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1861 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1862 if (version
>= ZPL_VERSION_SA
) {
1863 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1868 * Start the dedup thread if this is a dedup stream. We do not bother
1869 * doing this if this a raw send of an encrypted dataset with dedup off
1870 * because normal encrypted blocks won't dedup.
1872 if (flags
->dedup
&& !flags
->dryrun
&& !(flags
->raw
&&
1873 zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
&&
1874 zfs_prop_get_int(zhp
, ZFS_PROP_DEDUP
) == ZIO_CHECKSUM_OFF
)) {
1875 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1876 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1877 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
)) != 0) {
1878 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1879 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1882 dda
.outputfd
= outfd
;
1883 dda
.inputfd
= pipefd
[1];
1884 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1885 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
)) != 0) {
1886 (void) close(pipefd
[0]);
1887 (void) close(pipefd
[1]);
1888 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1889 return (zfs_error(zhp
->zfs_hdl
,
1890 EZFS_THREADCREATEFAILED
, errbuf
));
1894 if (flags
->replicate
|| flags
->doall
|| flags
->props
|| flags
->backup
) {
1895 dmu_replay_record_t drr
= { 0 };
1896 char *packbuf
= NULL
;
1900 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
1902 if (flags
->replicate
|| flags
->props
|| flags
->backup
) {
1905 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1907 VERIFY(0 == nvlist_add_string(hdrnv
,
1908 "fromsnap", fromsnap
));
1910 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1911 if (!flags
->replicate
) {
1912 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1916 VERIFY(0 == nvlist_add_boolean(hdrnv
, "raw"));
1919 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1920 fromsnap
, tosnap
, flags
->replicate
, flags
->raw
,
1921 flags
->verbose
, flags
->backup
, &fss
, &fsavl
);
1924 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1925 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1935 if (!flags
->dryrun
) {
1936 /* write first begin record */
1937 drr
.drr_type
= DRR_BEGIN
;
1938 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1939 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1940 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1941 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1942 drr_versioninfo
, featureflags
);
1943 if (snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1944 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1945 "%s@%s", zhp
->zfs_name
, tosnap
) >=
1946 sizeof (drr
.drr_u
.drr_begin
.drr_toname
)) {
1950 drr
.drr_payloadlen
= buflen
;
1952 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, outfd
);
1957 /* write end record */
1958 bzero(&drr
, sizeof (drr
));
1959 drr
.drr_type
= DRR_END
;
1960 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1961 err
= write(outfd
, &drr
, sizeof (drr
));
1971 /* dump each stream */
1972 sdd
.fromsnap
= fromsnap
;
1973 sdd
.tosnap
= tosnap
;
1975 sdd
.outfd
= pipefd
[0];
1978 sdd
.replicate
= flags
->replicate
;
1979 sdd
.doall
= flags
->doall
;
1980 sdd
.fromorigin
= flags
->fromorigin
;
1983 sdd
.verbose
= flags
->verbose
;
1984 sdd
.parsable
= flags
->parsable
;
1985 sdd
.progress
= flags
->progress
;
1986 sdd
.dryrun
= flags
->dryrun
;
1987 sdd
.large_block
= flags
->largeblock
;
1988 sdd
.embed_data
= flags
->embed_data
;
1989 sdd
.compress
= flags
->compress
;
1990 sdd
.raw
= flags
->raw
;
1991 sdd
.filter_cb
= filter_func
;
1992 sdd
.filter_cb_arg
= cb_arg
;
1994 sdd
.debugnv
= *debugnvp
;
1995 if (sdd
.verbose
&& sdd
.dryrun
)
1996 sdd
.std_out
= B_TRUE
;
1997 fout
= sdd
.std_out
? stdout
: stderr
;
2000 * Some flags require that we place user holds on the datasets that are
2001 * being sent so they don't get destroyed during the send. We can skip
2002 * this step if the pool is imported read-only since the datasets cannot
2005 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
2006 ZPOOL_PROP_READONLY
, NULL
) &&
2007 zfs_spa_version(zhp
, &spa_version
) == 0 &&
2008 spa_version
>= SPA_VERSION_USERREFS
&&
2009 (flags
->doall
|| flags
->replicate
)) {
2011 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
2012 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
2013 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
2014 if (sdd
.cleanup_fd
< 0) {
2018 sdd
.snapholds
= fnvlist_alloc();
2020 sdd
.cleanup_fd
= -1;
2021 sdd
.snapholds
= NULL
;
2023 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
2025 * Do a verbose no-op dry run to get all the verbose output
2026 * or to gather snapshot hold's before generating any data,
2027 * then do a non-verbose real run to generate the streams.
2029 sdd
.dryrun
= B_TRUE
;
2030 err
= dump_filesystems(zhp
, &sdd
);
2035 if (flags
->verbose
) {
2036 if (flags
->parsable
) {
2037 (void) fprintf(fout
, "size\t%llu\n",
2038 (longlong_t
)sdd
.size
);
2041 zfs_nicebytes(sdd
.size
, buf
, sizeof (buf
));
2042 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
2043 "total estimated size is %s\n"), buf
);
2047 /* Ensure no snaps found is treated as an error. */
2053 /* Skip the second run if dryrun was requested. */
2057 if (sdd
.snapholds
!= NULL
) {
2058 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
2062 fnvlist_free(sdd
.snapholds
);
2063 sdd
.snapholds
= NULL
;
2066 sdd
.dryrun
= B_FALSE
;
2067 sdd
.verbose
= B_FALSE
;
2070 err
= dump_filesystems(zhp
, &sdd
);
2071 fsavl_destroy(fsavl
);
2074 /* Ensure no snaps found is treated as an error. */
2075 if (err
== 0 && !sdd
.seento
)
2080 (void) pthread_cancel(tid
);
2081 (void) close(pipefd
[0]);
2082 (void) pthread_join(tid
, NULL
);
2085 if (sdd
.cleanup_fd
!= -1) {
2086 VERIFY(0 == close(sdd
.cleanup_fd
));
2087 sdd
.cleanup_fd
= -1;
2090 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
2091 flags
->props
|| flags
->backup
)) {
2093 * write final end record. NB: want to do this even if
2094 * there was some error, because it might not be totally
2097 dmu_replay_record_t drr
= { 0 };
2098 drr
.drr_type
= DRR_END
;
2099 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
2100 return (zfs_standard_error(zhp
->zfs_hdl
,
2105 return (err
|| sdd
.err
);
2108 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
2110 fsavl_destroy(fsavl
);
2112 fnvlist_free(sdd
.snapholds
);
2114 if (sdd
.cleanup_fd
!= -1)
2115 VERIFY(0 == close(sdd
.cleanup_fd
));
2117 (void) pthread_cancel(tid
);
2118 (void) close(pipefd
[0]);
2119 (void) pthread_join(tid
, NULL
);
2125 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
, sendflags_t flags
)
2128 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2129 enum lzc_send_flags lzc_flags
= 0;
2130 FILE *fout
= (flags
.verbose
&& flags
.dryrun
) ? stdout
: stderr
;
2133 if (flags
.largeblock
)
2134 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
2135 if (flags
.embed_data
)
2136 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
2138 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
2140 lzc_flags
|= LZC_SEND_FLAG_RAW
;
2142 if (flags
.verbose
) {
2144 err
= lzc_send_space(zhp
->zfs_name
, from
, lzc_flags
, &size
);
2146 send_print_verbose(fout
, zhp
->zfs_name
, from
, size
,
2149 (void) fprintf(stderr
, "Cannot estimate send size: "
2150 "%s\n", strerror(errno
));
2157 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2158 "warning: cannot send '%s'"), zhp
->zfs_name
);
2160 err
= lzc_send(zhp
->zfs_name
, from
, fd
, lzc_flags
);
2164 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2165 "not an earlier snapshot from the same fs"));
2166 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
2170 if (lzc_exists(zhp
->zfs_name
)) {
2171 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2172 "incremental source (%s) does not exist"),
2175 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2178 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2179 "dataset key must be loaded"));
2180 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
2183 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2184 "target is busy; if a filesystem, "
2185 "it must not be mounted"));
2186 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
2199 zfs_error_aux(hdl
, strerror(errno
));
2200 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
2203 return (zfs_standard_error(hdl
, errno
, errbuf
));
2210 * Routines specific to "zfs recv"
2214 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
2215 boolean_t byteswap
, zio_cksum_t
*zc
)
2221 assert(ilen
<= SPA_MAXBLOCKSIZE
);
2224 rv
= read(fd
, cp
, len
);
2229 if (rv
< 0 || len
!= 0) {
2230 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2231 "failed to read from stream"));
2232 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
2233 "cannot receive")));
2238 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
2240 fletcher_4_incremental_native(buf
, ilen
, zc
);
2246 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
2247 boolean_t byteswap
, zio_cksum_t
*zc
)
2252 buf
= zfs_alloc(hdl
, len
);
2256 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
2262 err
= nvlist_unpack(buf
, len
, nvp
, 0);
2265 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2266 "stream (malformed nvlist)"));
2273 * Returns the grand origin (origin of origin of origin...) of a given handle.
2274 * If this dataset is not a clone, it simply returns a copy of the original
2277 static zfs_handle_t
*
2278 recv_open_grand_origin(zfs_handle_t
*zhp
)
2280 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
2282 zfs_handle_t
*ozhp
= zfs_handle_dup(zhp
);
2284 while (ozhp
!= NULL
) {
2285 if (zfs_prop_get(ozhp
, ZFS_PROP_ORIGIN
, origin
,
2286 sizeof (origin
), &src
, NULL
, 0, B_FALSE
) != 0)
2289 (void) zfs_close(ozhp
);
2290 ozhp
= zfs_open(zhp
->zfs_hdl
, origin
, ZFS_TYPE_FILESYSTEM
);
2297 recv_rename_impl(zfs_handle_t
*zhp
, const char *name
, const char *newname
)
2300 zfs_handle_t
*ozhp
= NULL
;
2303 * Attempt to rename the dataset. If it fails with EACCES we have
2304 * attempted to rename the dataset outside of its encryption root.
2305 * Force the dataset to become an encryption root and try again.
2307 err
= lzc_rename(name
, newname
);
2308 if (err
== EACCES
) {
2309 ozhp
= recv_open_grand_origin(zhp
);
2315 err
= lzc_change_key(ozhp
->zfs_name
, DCP_CMD_FORCE_NEW_KEY
,
2320 err
= lzc_rename(name
, newname
);
2330 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
2331 int baselen
, char *newname
, recvflags_t
*flags
)
2335 prop_changelist_t
*clp
= NULL
;
2336 zfs_handle_t
*zhp
= NULL
;
2338 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2343 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2344 flags
->force
? MS_FORCE
: 0);
2349 err
= changelist_prefix(clp
);
2354 (void) strcpy(newname
, tryname
);
2355 if (flags
->verbose
) {
2356 (void) printf("attempting rename %s to %s\n",
2359 err
= recv_rename_impl(zhp
, name
, newname
);
2361 changelist_rename(clp
, name
, tryname
);
2366 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
2369 (void) snprintf(newname
, ZFS_MAX_DATASET_NAME_LEN
,
2370 "%.*srecv-%u-%u", baselen
, name
, getpid(), seq
);
2372 if (flags
->verbose
) {
2373 (void) printf("failed - trying rename %s to %s\n",
2376 err
= recv_rename_impl(zhp
, name
, newname
);
2378 changelist_rename(clp
, name
, newname
);
2379 if (err
&& flags
->verbose
) {
2380 (void) printf("failed (%u) - "
2381 "will try again on next pass\n", errno
);
2384 } else if (flags
->verbose
) {
2386 (void) printf("success\n");
2388 (void) printf("failed (%u)\n", errno
);
2391 (void) changelist_postfix(clp
);
2395 changelist_free(clp
);
2403 recv_promote(libzfs_handle_t
*hdl
, const char *fsname
,
2404 const char *origin_fsname
, recvflags_t
*flags
)
2407 zfs_cmd_t zc
= {"\0"};
2408 zfs_handle_t
*zhp
= NULL
, *ozhp
= NULL
;
2411 (void) printf("promoting %s\n", fsname
);
2413 (void) strlcpy(zc
.zc_value
, origin_fsname
, sizeof (zc
.zc_value
));
2414 (void) strlcpy(zc
.zc_name
, fsname
, sizeof (zc
.zc_name
));
2417 * Attempt to promote the dataset. If it fails with EACCES the
2418 * promotion would cause this dataset to leave its encryption root.
2419 * Force the origin to become an encryption root and try again.
2421 err
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2422 if (err
== EACCES
) {
2423 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_DATASET
);
2429 ozhp
= recv_open_grand_origin(zhp
);
2435 err
= lzc_change_key(ozhp
->zfs_name
, DCP_CMD_FORCE_NEW_KEY
,
2440 err
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2453 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
2454 char *newname
, recvflags_t
*flags
)
2457 prop_changelist_t
*clp
;
2459 boolean_t defer
= B_FALSE
;
2462 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2465 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2466 flags
->force
? MS_FORCE
: 0);
2467 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2468 zfs_spa_version(zhp
, &spa_version
) == 0 &&
2469 spa_version
>= SPA_VERSION_USERREFS
)
2474 err
= changelist_prefix(clp
);
2479 (void) printf("attempting destroy %s\n", name
);
2480 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
) {
2481 nvlist_t
*nv
= fnvlist_alloc();
2482 fnvlist_add_boolean(nv
, name
);
2483 err
= lzc_destroy_snaps(nv
, defer
, NULL
);
2486 err
= lzc_destroy(name
);
2490 (void) printf("success\n");
2491 changelist_remove(clp
, name
);
2494 (void) changelist_postfix(clp
);
2495 changelist_free(clp
);
2498 * Deferred destroy might destroy the snapshot or only mark it to be
2499 * destroyed later, and it returns success in either case.
2501 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
2502 ZFS_TYPE_SNAPSHOT
))) {
2503 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
2509 typedef struct guid_to_name_data
{
2511 boolean_t bookmark_ok
;
2514 } guid_to_name_data_t
;
2517 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
2519 guid_to_name_data_t
*gtnd
= arg
;
2523 if (gtnd
->skip
!= NULL
&&
2524 (slash
= strrchr(zhp
->zfs_name
, '/')) != NULL
&&
2525 strcmp(slash
+ 1, gtnd
->skip
) == 0) {
2530 if (zfs_prop_get_int(zhp
, ZFS_PROP_GUID
) == gtnd
->guid
) {
2531 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
2536 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
2537 if (err
!= EEXIST
&& gtnd
->bookmark_ok
)
2538 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, gtnd
);
2544 * Attempt to find the local dataset associated with this guid. In the case of
2545 * multiple matches, we attempt to find the "best" match by searching
2546 * progressively larger portions of the hierarchy. This allows one to send a
2547 * tree of datasets individually and guarantee that we will find the source
2548 * guid within that hierarchy, even if there are multiple matches elsewhere.
2551 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
2552 boolean_t bookmark_ok
, char *name
)
2554 char pname
[ZFS_MAX_DATASET_NAME_LEN
];
2555 guid_to_name_data_t gtnd
;
2558 gtnd
.bookmark_ok
= bookmark_ok
;
2563 * Search progressively larger portions of the hierarchy, starting
2564 * with the filesystem specified by 'parent'. This will
2565 * select the "most local" version of the origin snapshot in the case
2566 * that there are multiple matching snapshots in the system.
2568 (void) strlcpy(pname
, parent
, sizeof (pname
));
2569 char *cp
= strrchr(pname
, '@');
2571 cp
= strchr(pname
, '\0');
2572 for (; cp
!= NULL
; cp
= strrchr(pname
, '/')) {
2573 /* Chop off the last component and open the parent */
2575 zfs_handle_t
*zhp
= make_dataset_handle(hdl
, pname
);
2579 int err
= guid_to_name_cb(zfs_handle_dup(zhp
), >nd
);
2581 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
2582 if (err
!= EEXIST
&& bookmark_ok
)
2583 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, >nd
);
2589 * Remember the last portion of the dataset so we skip it next
2590 * time through (as we've already searched that portion of the
2593 gtnd
.skip
= strrchr(pname
, '/') + 1;
2600 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2601 * guid1 is after guid2.
2604 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
2605 uint64_t guid1
, uint64_t guid2
)
2608 char *fsname
= NULL
, *snapname
= NULL
;
2609 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2611 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2612 uint64_t create1
, create2
;
2619 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2620 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2621 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2622 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2623 if (guid1hdl
== NULL
)
2626 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2627 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2628 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2629 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2630 if (guid2hdl
== NULL
) {
2631 zfs_close(guid1hdl
);
2635 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2636 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2638 if (create1
< create2
)
2640 else if (create1
> create2
)
2645 zfs_close(guid1hdl
);
2646 zfs_close(guid2hdl
);
2652 * This function reestablishes the heirarchy of encryption roots after a
2653 * recursive incremental receive has completed. This must be done after the
2654 * second call to recv_incremental_replication() has renamed and promoted all
2655 * sent datasets to their final locations in the dataset heriarchy.
2658 recv_fix_encryption_heirarchy(libzfs_handle_t
*hdl
, const char *destname
,
2659 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
)
2662 nvpair_t
*fselem
= NULL
;
2663 nvlist_t
*stream_fss
;
2665 char top_zfs
[ZFS_MAX_DATASET_NAME_LEN
];
2667 (void) strcpy(top_zfs
, destname
);
2668 cp
= strrchr(top_zfs
, '@');
2672 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss", &stream_fss
));
2674 while ((fselem
= nvlist_next_nvpair(stream_fss
, fselem
)) != NULL
) {
2675 zfs_handle_t
*zhp
= NULL
;
2677 nvlist_t
*snaps
, *props
, *stream_nvfs
= NULL
;
2678 nvpair_t
*snapel
= NULL
;
2679 boolean_t is_encroot
, is_clone
, stream_encroot
;
2681 char *stream_keylocation
= NULL
;
2682 char keylocation
[MAXNAMELEN
];
2683 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
2685 keylocation
[0] = '\0';
2686 VERIFY(0 == nvpair_value_nvlist(fselem
, &stream_nvfs
));
2687 VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs
, "snaps", &snaps
));
2688 VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs
, "props", &props
));
2689 stream_encroot
= nvlist_exists(stream_nvfs
, "is_encroot");
2691 /* find a snapshot from the stream that exists locally */
2693 while ((snapel
= nvlist_next_nvpair(snaps
, snapel
)) != NULL
) {
2696 VERIFY(0 == nvpair_value_uint64(snapel
, &guid
));
2697 err
= guid_to_name(hdl
, destname
, guid
, B_FALSE
,
2706 cp
= strchr(fsname
, '@');
2710 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_DATASET
);
2716 crypt
= zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
2717 is_clone
= zhp
->zfs_dmustats
.dds_origin
[0] != '\0';
2718 (void) zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
2720 /* we don't need to do anything for unencrypted filesystems */
2721 if (crypt
== ZIO_CRYPT_OFF
) {
2727 * If the dataset is flagged as an encryption root, was not
2728 * received as a clone and is not currently an encryption root,
2729 * force it to become one. Fixup the keylocation if necessary.
2731 if (stream_encroot
) {
2732 if (!is_clone
&& !is_encroot
) {
2733 err
= lzc_change_key(fsname
,
2734 DCP_CMD_FORCE_NEW_KEY
, NULL
, NULL
, 0);
2741 VERIFY(0 == nvlist_lookup_string(props
,
2742 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
2743 &stream_keylocation
));
2746 * Refresh the properties in case the call to
2747 * lzc_change_key() changed the value.
2749 zfs_refresh_properties(zhp
);
2750 err
= zfs_prop_get(zhp
, ZFS_PROP_KEYLOCATION
,
2751 keylocation
, sizeof (keylocation
), NULL
, NULL
,
2758 if (strcmp(keylocation
, stream_keylocation
) != 0) {
2759 err
= zfs_prop_set(zhp
,
2760 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
2761 stream_keylocation
);
2770 * If the dataset is not flagged as an encryption root and is
2771 * currently an encryption root, force it to inherit from its
2772 * parent. The root of a raw send should never be
2775 if (!stream_encroot
&& is_encroot
&&
2776 strcmp(top_zfs
, fsname
) != 0) {
2777 err
= lzc_change_key(fsname
, DCP_CMD_FORCE_INHERIT
,
2795 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2796 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2799 nvlist_t
*local_nv
, *deleted
= NULL
;
2800 avl_tree_t
*local_avl
;
2801 nvpair_t
*fselem
, *nextfselem
;
2803 char newname
[ZFS_MAX_DATASET_NAME_LEN
];
2806 boolean_t needagain
, progress
, recursive
;
2809 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2811 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2818 needagain
= progress
= B_FALSE
;
2820 VERIFY(0 == nvlist_alloc(&deleted
, NV_UNIQUE_NAME
, 0));
2822 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2823 recursive
, B_TRUE
, B_FALSE
, B_FALSE
, &local_nv
, &local_avl
)) != 0)
2827 * Process deletes and renames
2829 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2830 fselem
; fselem
= nextfselem
) {
2831 nvlist_t
*nvfs
, *snaps
;
2832 nvlist_t
*stream_nvfs
= NULL
;
2833 nvpair_t
*snapelem
, *nextsnapelem
;
2834 uint64_t fromguid
= 0;
2835 uint64_t originguid
= 0;
2836 uint64_t stream_originguid
= 0;
2837 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2838 char *fsname
, *stream_fsname
;
2840 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2842 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2843 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2844 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2845 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2846 &parent_fromsnap_guid
));
2847 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2850 * First find the stream's fs, so we can check for
2851 * a different origin (due to "zfs promote")
2853 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2854 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2857 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2858 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2860 if (stream_nvfs
!= NULL
)
2864 /* check for promote */
2865 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2866 &stream_originguid
);
2867 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2868 switch (created_before(hdl
, local_avl
,
2869 stream_originguid
, originguid
)) {
2872 nvlist_t
*origin_nvfs
;
2873 char *origin_fsname
;
2875 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2877 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2878 "name", &origin_fsname
));
2879 error
= recv_promote(hdl
, fsname
, origin_fsname
,
2888 fsavl_destroy(local_avl
);
2889 nvlist_free(local_nv
);
2893 * We had/have the wrong origin, therefore our
2894 * list of snapshots is wrong. Need to handle
2895 * them on the next pass.
2901 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2902 snapelem
; snapelem
= nextsnapelem
) {
2904 char *stream_snapname
;
2905 nvlist_t
*found
, *props
;
2907 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2909 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2910 found
= fsavl_find(stream_avl
, thisguid
,
2913 /* check for delete */
2914 if (found
== NULL
) {
2915 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2920 (void) snprintf(name
, sizeof (name
), "%s@%s",
2921 fsname
, nvpair_name(snapelem
));
2923 error
= recv_destroy(hdl
, name
,
2924 strlen(fsname
)+1, newname
, flags
);
2929 sprintf(guidname
, "%llu",
2930 (u_longlong_t
)thisguid
);
2931 nvlist_add_boolean(deleted
, guidname
);
2935 stream_nvfs
= found
;
2937 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2938 &props
) && 0 == nvlist_lookup_nvlist(props
,
2939 stream_snapname
, &props
)) {
2940 zfs_cmd_t zc
= {"\0"};
2942 zc
.zc_cookie
= B_TRUE
; /* received */
2943 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2944 "%s@%s", fsname
, nvpair_name(snapelem
));
2945 if (zcmd_write_src_nvlist(hdl
, &zc
,
2947 (void) zfs_ioctl(hdl
,
2948 ZFS_IOC_SET_PROP
, &zc
);
2949 zcmd_free_nvlists(&zc
);
2953 /* check for different snapname */
2954 if (strcmp(nvpair_name(snapelem
),
2955 stream_snapname
) != 0) {
2956 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2957 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2959 (void) snprintf(name
, sizeof (name
), "%s@%s",
2960 fsname
, nvpair_name(snapelem
));
2961 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2962 fsname
, stream_snapname
);
2964 error
= recv_rename(hdl
, name
, tryname
,
2965 strlen(fsname
)+1, newname
, flags
);
2972 if (strcmp(stream_snapname
, fromsnap
) == 0)
2973 fromguid
= thisguid
;
2976 /* check for delete */
2977 if (stream_nvfs
== NULL
) {
2981 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2987 sprintf(guidname
, "%llu",
2988 (u_longlong_t
)parent_fromsnap_guid
);
2989 nvlist_add_boolean(deleted
, guidname
);
2993 if (fromguid
== 0) {
2994 if (flags
->verbose
) {
2995 (void) printf("local fs %s does not have "
2996 "fromsnap (%s in stream); must have "
2997 "been deleted locally; ignoring\n",
3003 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
3004 "name", &stream_fsname
));
3005 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
3006 "parentfromsnap", &stream_parent_fromsnap_guid
));
3008 s1
= strrchr(fsname
, '/');
3009 s2
= strrchr(stream_fsname
, '/');
3012 * Check if we're going to rename based on parent guid change
3013 * and the current parent guid was also deleted. If it was then
3014 * rename will fail and is likely unneeded, so avoid this and
3015 * force an early retry to determine the new
3016 * parent_fromsnap_guid.
3018 if (stream_parent_fromsnap_guid
!= 0 &&
3019 parent_fromsnap_guid
!= 0 &&
3020 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
3021 sprintf(guidname
, "%llu",
3022 (u_longlong_t
)parent_fromsnap_guid
);
3023 if (nvlist_exists(deleted
, guidname
)) {
3031 * Check for rename. If the exact receive path is specified, it
3032 * does not count as a rename, but we still need to check the
3033 * datasets beneath it.
3035 if ((stream_parent_fromsnap_guid
!= 0 &&
3036 parent_fromsnap_guid
!= 0 &&
3037 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
3038 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
3039 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
3041 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
3043 parent
= fsavl_find(local_avl
,
3044 stream_parent_fromsnap_guid
, NULL
);
3046 * NB: parent might not be found if we used the
3047 * tosnap for stream_parent_fromsnap_guid,
3048 * because the parent is a newly-created fs;
3049 * we'll be able to rename it after we recv the
3052 if (parent
!= NULL
) {
3055 VERIFY(0 == nvlist_lookup_string(parent
, "name",
3057 (void) snprintf(tryname
, sizeof (tryname
),
3058 "%s%s", pname
, strrchr(stream_fsname
, '/'));
3061 if (flags
->verbose
) {
3062 (void) printf("local fs %s new parent "
3063 "not found\n", fsname
);
3069 error
= recv_rename(hdl
, fsname
, tryname
,
3070 strlen(tofs
)+1, newname
, flags
);
3072 if (renamed
!= NULL
&& newname
[0] != '\0') {
3073 VERIFY(0 == nvlist_add_boolean(renamed
,
3085 fsavl_destroy(local_avl
);
3086 nvlist_free(local_nv
);
3087 nvlist_free(deleted
);
3089 if (needagain
&& progress
) {
3090 /* do another pass to fix up temporary names */
3092 (void) printf("another pass:\n");
3096 return (needagain
|| error
!= 0);
3100 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
3101 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
3102 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
,
3105 nvlist_t
*stream_nv
= NULL
;
3106 avl_tree_t
*stream_avl
= NULL
;
3107 char *fromsnap
= NULL
;
3108 char *sendsnap
= NULL
;
3110 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
3111 char sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
3113 dmu_replay_record_t drre
;
3115 boolean_t anyerr
= B_FALSE
;
3116 boolean_t softerr
= B_FALSE
;
3117 boolean_t recursive
, raw
;
3119 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3122 assert(drr
->drr_type
== DRR_BEGIN
);
3123 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
3124 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
3125 DMU_COMPOUNDSTREAM
);
3128 * Read in the nvlist from the stream.
3130 if (drr
->drr_payloadlen
!= 0) {
3131 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
3132 &stream_nv
, flags
->byteswap
, zc
);
3134 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3139 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
3141 raw
= (nvlist_lookup_boolean(stream_nv
, "raw") == 0);
3143 if (recursive
&& strchr(destname
, '@')) {
3144 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3145 "cannot specify snapshot name for multi-snapshot stream"));
3146 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3151 * Read in the end record and verify checksum.
3153 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
3154 flags
->byteswap
, NULL
)))
3156 if (flags
->byteswap
) {
3157 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
3158 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
3159 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
3160 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
3161 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
3162 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
3163 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
3164 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
3165 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
3167 if (drre
.drr_type
!= DRR_END
) {
3168 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3171 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
3172 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3173 "incorrect header checksum"));
3174 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3178 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
3180 if (drr
->drr_payloadlen
!= 0) {
3181 nvlist_t
*stream_fss
;
3183 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
3185 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
3186 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3187 "couldn't allocate avl tree"));
3188 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
3192 if (fromsnap
!= NULL
&& recursive
) {
3193 nvlist_t
*renamed
= NULL
;
3194 nvpair_t
*pair
= NULL
;
3196 (void) strlcpy(tofs
, destname
, sizeof (tofs
));
3197 if (flags
->isprefix
) {
3198 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
3201 if (flags
->istail
) {
3202 cp
= strrchr(drrb
->drr_toname
, '/');
3204 (void) strlcat(tofs
, "/",
3208 i
= (cp
- drrb
->drr_toname
);
3211 i
= strcspn(drrb
->drr_toname
, "/@");
3213 /* zfs_receive_one() will create_parents() */
3214 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
3216 *strchr(tofs
, '@') = '\0';
3219 if (!flags
->dryrun
&& !flags
->nomount
) {
3220 VERIFY(0 == nvlist_alloc(&renamed
,
3221 NV_UNIQUE_NAME
, 0));
3224 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
3225 stream_nv
, stream_avl
, renamed
);
3227 /* Unmount renamed filesystems before receiving. */
3228 while ((pair
= nvlist_next_nvpair(renamed
,
3231 prop_changelist_t
*clp
= NULL
;
3233 zhp
= zfs_open(hdl
, nvpair_name(pair
),
3234 ZFS_TYPE_FILESYSTEM
);
3236 clp
= changelist_gather(zhp
,
3237 ZFS_PROP_MOUNTPOINT
, 0, 0);
3241 changelist_prefix(clp
);
3242 changelist_free(clp
);
3247 nvlist_free(renamed
);
3252 * Get the fs specified by the first path in the stream (the top level
3253 * specified by 'zfs send') and pass it to each invocation of
3254 * zfs_receive_one().
3256 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
3258 if ((cp
= strchr(sendfs
, '@')) != NULL
) {
3261 * Find the "sendsnap", the final snapshot in a replication
3262 * stream. zfs_receive_one() handles certain errors
3263 * differently, depending on if the contained stream is the
3266 sendsnap
= (cp
+ 1);
3269 /* Finally, receive each contained stream */
3272 * we should figure out if it has a recoverable
3273 * error, in which case do a recv_skip() and drive on.
3274 * Note, if we fail due to already having this guid,
3275 * zfs_receive_one() will take care of it (ie,
3276 * recv_skip() and return 0).
3278 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
3279 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
3280 action_handlep
, sendsnap
, cmdprops
);
3281 if (error
== ENODATA
) {
3286 } while (error
== 0);
3288 if (drr
->drr_payloadlen
!= 0 && recursive
&& fromsnap
!= NULL
) {
3290 * Now that we have the fs's they sent us, try the
3293 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
3294 stream_nv
, stream_avl
, NULL
);
3297 if (raw
&& softerr
== 0) {
3298 softerr
= recv_fix_encryption_heirarchy(hdl
, destname
,
3299 stream_nv
, stream_avl
);
3303 fsavl_destroy(stream_avl
);
3304 nvlist_free(stream_nv
);
3313 trunc_prop_errs(int truncated
)
3315 ASSERT(truncated
!= 0);
3318 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
3319 "1 more property could not be set\n"));
3321 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
3322 "%d more properties could not be set\n"), truncated
);
3326 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
3328 dmu_replay_record_t
*drr
;
3329 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
3332 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3333 "cannot receive:"));
3335 /* XXX would be great to use lseek if possible... */
3338 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
3339 byteswap
, NULL
) == 0) {
3341 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
3343 switch (drr
->drr_type
) {
3345 if (drr
->drr_payloadlen
!= 0) {
3346 (void) recv_read(hdl
, fd
, buf
,
3347 drr
->drr_payloadlen
, B_FALSE
, NULL
);
3357 drr
->drr_u
.drr_object
.drr_bonuslen
=
3358 BSWAP_32(drr
->drr_u
.drr_object
.
3361 (void) recv_read(hdl
, fd
, buf
,
3362 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
3368 drr
->drr_u
.drr_write
.drr_logical_size
=
3370 drr
->drr_u
.drr_write
.drr_logical_size
);
3371 drr
->drr_u
.drr_write
.drr_compressed_size
=
3373 drr
->drr_u
.drr_write
.drr_compressed_size
);
3375 uint64_t payload_size
=
3376 DRR_WRITE_PAYLOAD_SIZE(&drr
->drr_u
.drr_write
);
3377 (void) recv_read(hdl
, fd
, buf
,
3378 payload_size
, B_FALSE
, NULL
);
3382 drr
->drr_u
.drr_spill
.drr_length
=
3383 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
3385 (void) recv_read(hdl
, fd
, buf
,
3386 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
3388 case DRR_WRITE_EMBEDDED
:
3390 drr
->drr_u
.drr_write_embedded
.drr_psize
=
3391 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
3394 (void) recv_read(hdl
, fd
, buf
,
3395 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
3398 case DRR_WRITE_BYREF
:
3399 case DRR_FREEOBJECTS
:
3404 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3405 "invalid record type"));
3407 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3416 recv_ecksum_set_aux(libzfs_handle_t
*hdl
, const char *target_snap
,
3417 boolean_t resumable
)
3419 char target_fs
[ZFS_MAX_DATASET_NAME_LEN
];
3421 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3422 "checksum mismatch or incomplete stream"));
3426 (void) strlcpy(target_fs
, target_snap
, sizeof (target_fs
));
3427 *strchr(target_fs
, '@') = '\0';
3428 zfs_handle_t
*zhp
= zfs_open(hdl
, target_fs
,
3429 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3433 char token_buf
[ZFS_MAXPROPLEN
];
3434 int error
= zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
3435 token_buf
, sizeof (token_buf
),
3436 NULL
, NULL
, 0, B_TRUE
);
3438 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3439 "checksum mismatch or incomplete stream.\n"
3440 "Partially received snapshot is saved.\n"
3441 "A resuming stream can be generated on the sending "
3442 "system by running:\n"
3450 * Prepare a new nvlist of properties that are to override (-o) or be excluded
3451 * (-x) from the received dataset
3452 * recvprops: received properties from the send stream
3453 * cmdprops: raw input properties from command line
3454 * origprops: properties, both locally-set and received, currently set on the
3455 * target dataset if it exists, NULL otherwise.
3456 * oxprops: valid output override (-o) and excluded (-x) properties
3459 zfs_setup_cmdline_props(libzfs_handle_t
*hdl
, zfs_type_t type
,
3460 char *fsname
, boolean_t zoned
, boolean_t recursive
, boolean_t newfs
,
3461 boolean_t raw
, boolean_t toplevel
, nvlist_t
*recvprops
, nvlist_t
*cmdprops
,
3462 nvlist_t
*origprops
, nvlist_t
**oxprops
, uint8_t **wkeydata_out
,
3463 uint_t
*wkeylen_out
, const char *errbuf
)
3466 nvlist_t
*oprops
, *voprops
;
3467 zfs_handle_t
*zhp
= NULL
;
3468 zpool_handle_t
*zpool_hdl
= NULL
;
3471 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
3473 if (nvlist_empty(cmdprops
))
3474 return (0); /* No properties to override or exclude */
3476 *oxprops
= fnvlist_alloc();
3477 oprops
= fnvlist_alloc();
3479 strlcpy(namebuf
, fsname
, ZFS_MAX_DATASET_NAME_LEN
);
3482 * Get our dataset handle. The target dataset may not exist yet.
3484 if (zfs_dataset_exists(hdl
, namebuf
, ZFS_TYPE_DATASET
)) {
3485 zhp
= zfs_open(hdl
, namebuf
, ZFS_TYPE_DATASET
);
3492 /* open the zpool handle */
3493 cp
= strchr(namebuf
, '/');
3496 zpool_hdl
= zpool_open(hdl
, namebuf
);
3497 if (zpool_hdl
== NULL
) {
3502 /* restore namebuf to match fsname for later use */
3507 * first iteration: process excluded (-x) properties now and gather
3508 * added (-o) properties to be later processed by zfs_valid_proplist()
3511 while ((nvp
= nvlist_next_nvpair(cmdprops
, nvp
)) != NULL
) {
3512 const char *name
= nvpair_name(nvp
);
3513 zfs_prop_t prop
= zfs_name_to_prop(name
);
3515 /* "origin" is processed separately, don't handle it here */
3516 if (prop
== ZFS_PROP_ORIGIN
)
3520 * we're trying to override or exclude a property that does not
3521 * make sense for this type of dataset, but we don't want to
3522 * fail if the receive is recursive: this comes in handy when
3523 * the send stream contains, for instance, a child ZVOL and
3524 * we're trying to receive it with "-o atime=on"
3526 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
) &&
3527 !zfs_prop_user(name
)) {
3530 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3531 "property '%s' does not apply to datasets of this "
3533 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
3537 /* raw streams can't override encryption properties */
3538 if ((zfs_prop_encryption_key_param(prop
) ||
3539 prop
== ZFS_PROP_ENCRYPTION
) && (raw
|| !newfs
)) {
3540 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3541 "encryption property '%s' cannot "
3542 "be set or excluded for raw or incremental "
3544 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
3548 switch (nvpair_type(nvp
)) {
3549 case DATA_TYPE_BOOLEAN
: /* -x property */
3551 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
3552 * a property: this is done by forcing an explicit
3553 * inherit on the destination so the effective value is
3554 * not the one we received from the send stream.
3555 * We do this only if the property is not already
3556 * locally-set, in which case its value will take
3557 * priority over the received anyway.
3559 if (nvlist_exists(origprops
, name
)) {
3562 attrs
= fnvlist_lookup_nvlist(origprops
, name
);
3563 if (strcmp(fnvlist_lookup_string(attrs
,
3564 ZPROP_SOURCE
), ZPROP_SOURCE_VAL_RECVD
) != 0)
3568 * We can't force an explicit inherit on non-inheritable
3569 * properties: if we're asked to exclude this kind of
3570 * values we remove them from "recvprops" input nvlist.
3572 if (!zfs_prop_inheritable(prop
) &&
3573 !zfs_prop_user(name
) && /* can be inherited too */
3574 nvlist_exists(recvprops
, name
))
3575 fnvlist_remove(recvprops
, name
);
3577 fnvlist_add_nvpair(*oxprops
, nvp
);
3579 case DATA_TYPE_STRING
: /* -o property=value */
3580 fnvlist_add_nvpair(oprops
, nvp
);
3583 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3584 "property '%s' must be a string or boolean"), name
);
3585 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
3591 /* convert override strings properties to native */
3592 if ((voprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_DATASET
,
3593 oprops
, zoned
, zhp
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
3594 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
3599 * zfs_crypto_create() requires the parent name. Get it
3600 * by truncating the fsname copy stored in namebuf.
3602 cp
= strrchr(namebuf
, '/');
3606 if (!raw
&& zfs_crypto_create(hdl
, namebuf
, voprops
, NULL
,
3607 B_FALSE
, wkeydata_out
, wkeylen_out
) != 0) {
3608 fnvlist_free(voprops
);
3609 ret
= zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
3613 /* second pass: process "-o" properties */
3614 fnvlist_merge(*oxprops
, voprops
);
3615 fnvlist_free(voprops
);
3617 /* override props on child dataset are inherited */
3619 while ((nvp
= nvlist_next_nvpair(oprops
, nvp
)) != NULL
) {
3620 const char *name
= nvpair_name(nvp
);
3621 fnvlist_add_boolean(*oxprops
, name
);
3628 if (zpool_hdl
!= NULL
)
3629 zpool_close(zpool_hdl
);
3630 fnvlist_free(oprops
);
3635 * Restores a backup of tosnap from the file descriptor specified by infd.
3638 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
3639 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
3640 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
3641 avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3642 uint64_t *action_handlep
, const char *finalsnap
, nvlist_t
*cmdprops
)
3645 int ioctl_err
, ioctl_errno
, err
;
3647 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
3649 const char *chopprefix
;
3650 boolean_t newfs
= B_FALSE
;
3651 boolean_t stream_wantsnewfs
;
3652 boolean_t newprops
= B_FALSE
;
3653 uint64_t read_bytes
= 0;
3654 uint64_t errflags
= 0;
3655 uint64_t parent_snapguid
= 0;
3656 prop_changelist_t
*clp
= NULL
;
3657 nvlist_t
*snapprops_nvlist
= NULL
;
3658 zprop_errflags_t prop_errflags
;
3659 nvlist_t
*prop_errors
= NULL
;
3660 boolean_t recursive
;
3661 char *snapname
= NULL
;
3662 char destsnap
[MAXPATHLEN
* 2];
3663 char origin
[MAXNAMELEN
];
3664 char name
[MAXPATHLEN
];
3665 char tmp_keylocation
[MAXNAMELEN
];
3666 nvlist_t
*rcvprops
= NULL
; /* props received from the send stream */
3667 nvlist_t
*oxprops
= NULL
; /* override (-o) and exclude (-x) props */
3668 nvlist_t
*origprops
= NULL
; /* original props (if destination exists) */
3670 boolean_t toplevel
= B_FALSE
;
3671 boolean_t zoned
= B_FALSE
;
3672 boolean_t hastoken
= B_FALSE
;
3673 uint8_t *wkeydata
= NULL
;
3676 begin_time
= time(NULL
);
3677 bzero(origin
, MAXNAMELEN
);
3678 bzero(tmp_keylocation
, MAXNAMELEN
);
3680 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3683 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
3686 if (stream_avl
!= NULL
) {
3687 char *keylocation
= NULL
;
3688 nvlist_t
*lookup
= NULL
;
3689 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
3692 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
3694 err
= nvlist_lookup_nvlist(fs
, "props", &rcvprops
);
3696 VERIFY(0 == nvlist_alloc(&rcvprops
, NV_UNIQUE_NAME
, 0));
3701 * The keylocation property may only be set on encryption roots,
3702 * but this dataset might not become an encryption root until
3703 * recv_fix_encryption_heirarchy() is called. That function
3704 * will fixup the keylocation anyway, so we temporarily unset
3705 * the keylocation for now to avoid any errors from the receive
3708 err
= nvlist_lookup_string(rcvprops
,
3709 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &keylocation
);
3711 strcpy(tmp_keylocation
, keylocation
);
3712 (void) nvlist_remove_all(rcvprops
,
3713 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
));
3716 if (flags
->canmountoff
) {
3717 VERIFY(0 == nvlist_add_uint64(rcvprops
,
3718 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
3720 if (0 == nvlist_lookup_nvlist(fs
, "snapprops", &lookup
)) {
3721 VERIFY(0 == nvlist_lookup_nvlist(lookup
,
3722 snapname
, &snapprops_nvlist
));
3729 * Determine how much of the snapshot name stored in the stream
3730 * we are going to tack on to the name they specified on the
3731 * command line, and how much we are going to chop off.
3733 * If they specified a snapshot, chop the entire name stored in
3736 if (flags
->istail
) {
3738 * A filesystem was specified with -e. We want to tack on only
3739 * the tail of the sent snapshot path.
3741 if (strchr(tosnap
, '@')) {
3742 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3743 "argument - snapshot not allowed with -e"));
3744 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3748 chopprefix
= strrchr(sendfs
, '/');
3750 if (chopprefix
== NULL
) {
3752 * The tail is the poolname, so we need to
3753 * prepend a path separator.
3755 int len
= strlen(drrb
->drr_toname
);
3756 cp
= malloc(len
+ 2);
3758 (void) strcpy(&cp
[1], drrb
->drr_toname
);
3761 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
3763 } else if (flags
->isprefix
) {
3765 * A filesystem was specified with -d. We want to tack on
3766 * everything but the first element of the sent snapshot path
3767 * (all but the pool name).
3769 if (strchr(tosnap
, '@')) {
3770 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3771 "argument - snapshot not allowed with -d"));
3772 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3776 chopprefix
= strchr(drrb
->drr_toname
, '/');
3777 if (chopprefix
== NULL
)
3778 chopprefix
= strchr(drrb
->drr_toname
, '@');
3779 } else if (strchr(tosnap
, '@') == NULL
) {
3781 * If a filesystem was specified without -d or -e, we want to
3782 * tack on everything after the fs specified by 'zfs send'.
3784 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
3786 /* A snapshot was specified as an exact path (no -d or -e). */
3788 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3789 "cannot specify snapshot name for multi-snapshot "
3791 err
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3794 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
3797 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
3798 ASSERT(chopprefix
> drrb
->drr_toname
|| strchr(sendfs
, '/') == NULL
);
3799 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
) ||
3800 strchr(sendfs
, '/') == NULL
);
3801 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
3802 chopprefix
[0] == '\0');
3805 * Determine name of destination snapshot.
3807 (void) strlcpy(destsnap
, tosnap
, sizeof (destsnap
));
3808 (void) strlcat(destsnap
, chopprefix
, sizeof (destsnap
));
3810 if (!zfs_name_valid(destsnap
, ZFS_TYPE_SNAPSHOT
)) {
3811 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3816 * Determine the name of the origin snapshot.
3819 (void) strlcpy(origin
, originsnap
, sizeof (origin
));
3821 (void) printf("using provided clone origin %s\n",
3823 } else if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
3824 if (guid_to_name(hdl
, destsnap
,
3825 drrb
->drr_fromguid
, B_FALSE
, origin
) != 0) {
3826 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3827 "local origin for clone %s does not exist"),
3829 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
3833 (void) printf("found clone origin %s\n", origin
);
3836 boolean_t resuming
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
3837 DMU_BACKUP_FEATURE_RESUMING
;
3838 boolean_t raw
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
3839 DMU_BACKUP_FEATURE_RAW
;
3840 boolean_t embedded
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
3841 DMU_BACKUP_FEATURE_EMBED_DATA
;
3842 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
3843 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
) && !resuming
;
3845 if (stream_wantsnewfs
) {
3847 * if the parent fs does not exist, look for it based on
3848 * the parent snap GUID
3850 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3851 "cannot receive new filesystem stream"));
3853 (void) strcpy(name
, destsnap
);
3854 cp
= strrchr(name
, '/');
3858 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3859 char suffix
[ZFS_MAX_DATASET_NAME_LEN
];
3860 (void) strcpy(suffix
, strrchr(destsnap
, '/'));
3861 if (guid_to_name(hdl
, name
, parent_snapguid
,
3862 B_FALSE
, destsnap
) == 0) {
3863 *strchr(destsnap
, '@') = '\0';
3864 (void) strcat(destsnap
, suffix
);
3869 * if the fs does not exist, look for it based on the
3872 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3873 "cannot receive incremental stream"));
3875 (void) strcpy(name
, destsnap
);
3876 *strchr(name
, '@') = '\0';
3879 * If the exact receive path was specified and this is the
3880 * topmost path in the stream, then if the fs does not exist we
3881 * should look no further.
3883 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
3884 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
3885 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3886 char snap
[ZFS_MAX_DATASET_NAME_LEN
];
3887 (void) strcpy(snap
, strchr(destsnap
, '@'));
3888 if (guid_to_name(hdl
, name
, drrb
->drr_fromguid
,
3889 B_FALSE
, destsnap
) == 0) {
3890 *strchr(destsnap
, '@') = '\0';
3891 (void) strcat(destsnap
, snap
);
3896 (void) strcpy(name
, destsnap
);
3897 *strchr(name
, '@') = '\0';
3899 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3900 zfs_cmd_t zc
= {"\0"};
3902 boolean_t encrypted
;
3904 (void) strcpy(zc
.zc_name
, name
);
3907 * Destination fs exists. It must be one of these cases:
3908 * - an incremental send stream
3909 * - the stream specifies a new fs (full stream or clone)
3910 * and they want us to blow away the existing fs (and
3911 * have therefore specified -F and removed any snapshots)
3912 * - we are resuming a failed receive.
3914 if (stream_wantsnewfs
) {
3915 if (!flags
->force
) {
3916 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3917 "destination '%s' exists\n"
3918 "must specify -F to overwrite it"), name
);
3919 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3922 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
3924 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3925 "destination has snapshots (eg. %s)\n"
3926 "must destroy them to overwrite it"),
3928 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3933 if ((zhp
= zfs_open(hdl
, name
,
3934 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
3939 if (stream_wantsnewfs
&&
3940 zhp
->zfs_dmustats
.dds_origin
[0]) {
3942 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3943 "destination '%s' is a clone\n"
3944 "must destroy it to overwrite it"), name
);
3945 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3950 * Raw sends can not be performed as an incremental on top
3951 * of existing unencryppted datasets. zfs recv -F cant be
3952 * used to blow away an existing encrypted filesystem. This
3953 * is because it would require the dsl dir to point to the
3954 * new key (or lack of a key) and the old key at the same
3955 * time. The -F flag may still be used for deleting
3956 * intermediate snapshots that would otherwise prevent the
3957 * receive from working.
3959 encrypted
= zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) !=
3961 if (!stream_wantsnewfs
&& !encrypted
&& raw
) {
3963 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3964 "cannot perform raw receive on top of "
3965 "existing unencrypted dataset"));
3966 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3970 if (stream_wantsnewfs
&& flags
->force
&&
3971 ((raw
&& !encrypted
) || encrypted
)) {
3973 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3974 "zfs receive -F cannot be used to destroy an "
3975 "encrypted filesystem or overwrite an "
3976 "unencrypted one with an encrypted one"));
3977 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3981 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
3982 stream_wantsnewfs
) {
3983 /* We can't do online recv in this case */
3984 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
3990 if (changelist_prefix(clp
) != 0) {
3991 changelist_free(clp
);
3999 * If we are resuming a newfs, set newfs here so that we will
4000 * mount it if the recv succeeds this time. We can tell
4001 * that it was a newfs on the first recv because the fs
4002 * itself will be inconsistent (if the fs existed when we
4003 * did the first recv, we would have received it into
4006 if (resuming
&& zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
))
4009 /* we want to know if we're zoned when validating -o|-x props */
4010 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
4012 /* may need this info later, get it now we have zhp around */
4013 if (zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
, NULL
, 0,
4014 NULL
, NULL
, 0, B_TRUE
) == 0)
4017 /* gather existing properties on destination */
4018 origprops
= fnvlist_alloc();
4019 fnvlist_merge(origprops
, zhp
->zfs_props
);
4020 fnvlist_merge(origprops
, zhp
->zfs_user_props
);
4027 * Destination filesystem does not exist. Therefore we better
4028 * be creating a new filesystem (either from a full backup, or
4029 * a clone). It would therefore be invalid if the user
4030 * specified only the pool name (i.e. if the destination name
4031 * contained no slash character).
4033 cp
= strrchr(name
, '/');
4035 if (!stream_wantsnewfs
|| cp
== NULL
) {
4036 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4037 "destination '%s' does not exist"), name
);
4038 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4043 * Trim off the final dataset component so we perform the
4044 * recvbackup ioctl to the filesystems's parent.
4048 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
4049 create_parents(hdl
, destsnap
, strlen(tosnap
)) != 0) {
4050 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
4055 * It is invalid to receive a properties stream that was
4056 * unencrypted on the send side as a child of an encrypted
4057 * parent. Technically there is nothing preventing this, but
4058 * it would mean that the encryption=off property which is
4059 * locally set on the send side would not be received correctly.
4060 * We can infer encryption=off if the stream is not raw and
4061 * properties were included since the send side will only ever
4062 * send the encryption property in a raw nvlist header. This
4063 * check will be avoided if the user specifically overrides
4064 * the encryption property on the command line.
4066 if (!raw
&& rcvprops
!= NULL
&&
4067 !nvlist_exists(cmdprops
,
4068 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
))) {
4071 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
4073 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
4077 crypt
= zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
4080 if (crypt
!= ZIO_CRYPT_OFF
) {
4081 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4082 "parent '%s' must not be encrypted to "
4083 "receive unenecrypted property"), name
);
4084 err
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
4093 if (flags
->verbose
) {
4094 (void) printf("%s %s stream of %s into %s\n",
4095 flags
->dryrun
? "would receive" : "receiving",
4096 drrb
->drr_fromguid
? "incremental" : "full",
4097 drrb
->drr_toname
, destsnap
);
4098 (void) fflush(stdout
);
4101 if (flags
->dryrun
) {
4102 err
= recv_skip(hdl
, infd
, flags
->byteswap
);
4106 if (top_zfs
&& (*top_zfs
== NULL
|| strcmp(*top_zfs
, name
) == 0))
4108 if (drrb
->drr_type
== DMU_OST_ZVOL
) {
4109 type
= ZFS_TYPE_VOLUME
;
4110 } else if (drrb
->drr_type
== DMU_OST_ZFS
) {
4111 type
= ZFS_TYPE_FILESYSTEM
;
4113 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4114 "invalid record type: 0x%d"), drrb
->drr_type
);
4115 err
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
4118 if ((err
= zfs_setup_cmdline_props(hdl
, type
, name
, zoned
, recursive
,
4119 stream_wantsnewfs
, raw
, toplevel
, rcvprops
, cmdprops
, origprops
,
4120 &oxprops
, &wkeydata
, &wkeylen
, errbuf
)) != 0)
4123 err
= ioctl_err
= lzc_receive_with_cmdprops(destsnap
, rcvprops
,
4124 oxprops
, wkeydata
, wkeylen
, origin
, flags
->force
, flags
->resumable
,
4125 raw
, infd
, drr_noswap
, cleanup_fd
, &read_bytes
, &errflags
,
4126 action_handlep
, &prop_errors
);
4127 ioctl_errno
= ioctl_err
;
4128 prop_errflags
= errflags
;
4131 nvpair_t
*prop_err
= NULL
;
4133 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
4134 prop_err
)) != NULL
) {
4139 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
4140 (void) nvpair_value_int32(prop_err
, &intval
);
4141 if (strcmp(nvpair_name(prop_err
),
4142 ZPROP_N_MORE_ERRORS
) == 0) {
4143 trunc_prop_errs(intval
);
4145 } else if (snapname
== NULL
|| finalsnap
== NULL
||
4146 strcmp(finalsnap
, snapname
) == 0 ||
4147 strcmp(nvpair_name(prop_err
),
4148 zfs_prop_to_name(ZFS_PROP_REFQUOTA
)) != 0) {
4150 * Skip the special case of, for example,
4151 * "refquota", errors on intermediate
4152 * snapshots leading up to a final one.
4153 * That's why we have all of the checks above.
4155 * See zfs_ioctl.c's extract_delay_props() for
4156 * a list of props which can fail on
4157 * intermediate snapshots, but shouldn't
4158 * affect the overall receive.
4160 (void) snprintf(tbuf
, sizeof (tbuf
),
4161 dgettext(TEXT_DOMAIN
,
4162 "cannot receive %s property on %s"),
4163 nvpair_name(prop_err
), name
);
4164 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
4169 if (err
== 0 && snapprops_nvlist
) {
4170 zfs_cmd_t zc
= {"\0"};
4172 (void) strcpy(zc
.zc_name
, destsnap
);
4173 zc
.zc_cookie
= B_TRUE
; /* received */
4174 if (zcmd_write_src_nvlist(hdl
, &zc
, snapprops_nvlist
) == 0) {
4175 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
4176 zcmd_free_nvlists(&zc
);
4180 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
4182 * It may be that this snapshot already exists,
4183 * in which case we want to consume & ignore it
4184 * rather than failing.
4186 avl_tree_t
*local_avl
;
4187 nvlist_t
*local_nv
, *fs
;
4188 cp
= strchr(destsnap
, '@');
4191 * XXX Do this faster by just iterating over snaps in
4192 * this fs. Also if zc_value does not exist, we will
4193 * get a strange "does not exist" error message.
4196 if (gather_nvlist(hdl
, destsnap
, NULL
, NULL
, B_FALSE
, B_TRUE
,
4197 B_FALSE
, B_FALSE
, &local_nv
, &local_avl
) == 0) {
4199 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
4200 fsavl_destroy(local_avl
);
4201 nvlist_free(local_nv
);
4204 if (flags
->verbose
) {
4205 (void) printf("snap %s already exists; "
4206 "ignoring\n", destsnap
);
4208 err
= ioctl_err
= recv_skip(hdl
, infd
,
4215 if (ioctl_err
!= 0) {
4216 switch (ioctl_errno
) {
4218 cp
= strchr(destsnap
, '@');
4220 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4221 "most recent snapshot of %s does not\n"
4222 "match incremental source"), destsnap
);
4223 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
4227 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4228 "destination %s has been modified\n"
4229 "since most recent snapshot"), name
);
4230 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
4233 if (raw
&& stream_wantsnewfs
) {
4234 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4235 "failed to create encryption key"));
4236 } else if (raw
&& !stream_wantsnewfs
) {
4237 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4238 "encryption key does not match "
4241 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4242 "inherited key must be loaded"));
4244 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4247 cp
= strchr(destsnap
, '@');
4249 /* it's the containing fs that exists */
4252 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4253 "destination already exists"));
4254 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
4255 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
4260 if (flags
->resumable
)
4261 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4262 "kernel modules must be upgraded to "
4263 "receive this stream."));
4264 if (embedded
&& !raw
)
4265 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4266 "incompatible embedded data stream "
4267 "feature with encrypted receive."));
4268 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
4271 recv_ecksum_set_aux(hdl
, destsnap
, flags
->resumable
);
4272 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
4275 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4276 "pool must be upgraded to receive this stream."));
4277 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
4280 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4281 "destination %s space quota exceeded."), name
);
4282 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
4286 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4287 "destination %s contains "
4288 "partially-complete state from "
4289 "\"zfs receive -s\"."), name
);
4290 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
4295 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
4300 * Mount the target filesystem (if created). Also mount any
4301 * children of the target filesystem if we did a replication
4302 * receive (indicated by stream_avl being non-NULL).
4304 cp
= strchr(destsnap
, '@');
4305 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
4309 h
= zfs_open(hdl
, destsnap
,
4310 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4312 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
4314 } else if (newfs
|| stream_avl
) {
4316 * Track the first/top of hierarchy fs,
4317 * for mounting and sharing later.
4319 if (top_zfs
&& *top_zfs
== NULL
)
4320 *top_zfs
= zfs_strdup(hdl
, destsnap
);
4328 if (!flags
->nomount
)
4329 err
|= changelist_postfix(clp
);
4330 changelist_free(clp
);
4333 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
4334 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
4335 "failed to clear unreceived properties on %s"), name
);
4336 (void) fprintf(stderr
, "\n");
4338 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
4339 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
4340 "failed to restore original properties on %s"), name
);
4341 (void) fprintf(stderr
, "\n");
4344 if (err
|| ioctl_err
) {
4349 if (flags
->verbose
) {
4352 uint64_t bytes
= read_bytes
;
4353 time_t delta
= time(NULL
) - begin_time
;
4356 zfs_nicebytes(bytes
, buf1
, sizeof (buf1
));
4357 zfs_nicebytes(bytes
/delta
, buf2
, sizeof (buf1
));
4359 (void) printf("received %s stream in %lu seconds (%s/sec)\n",
4365 if (prop_errors
!= NULL
)
4366 nvlist_free(prop_errors
);
4368 if (tmp_keylocation
[0] != '\0') {
4369 VERIFY(0 == nvlist_add_string(rcvprops
,
4370 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), tmp_keylocation
));
4374 nvlist_free(rcvprops
);
4376 nvlist_free(oxprops
);
4377 nvlist_free(origprops
);
4383 * Check properties we were asked to override (both -o|-x)
4386 zfs_receive_checkprops(libzfs_handle_t
*hdl
, nvlist_t
*props
,
4394 while ((nvp
= nvlist_next_nvpair(props
, nvp
)) != NULL
) {
4395 name
= nvpair_name(nvp
);
4396 prop
= zfs_name_to_prop(name
);
4398 if (prop
== ZPROP_INVAL
) {
4399 if (!zfs_prop_user(name
)) {
4400 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4401 "invalid property '%s'"), name
);
4407 * "origin" is readonly but is used to receive datasets as
4408 * clones so we don't raise an error here
4410 if (prop
== ZFS_PROP_ORIGIN
)
4413 /* encryption params have their own verification later */
4414 if (prop
== ZFS_PROP_ENCRYPTION
||
4415 zfs_prop_encryption_key_param(prop
))
4419 * cannot override readonly, set-once and other specific
4420 * settable properties
4422 if (zfs_prop_readonly(prop
) || prop
== ZFS_PROP_VERSION
||
4423 prop
== ZFS_PROP_VOLSIZE
) {
4424 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4425 "invalid property '%s'"), name
);
4434 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
4435 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
4436 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
4437 uint64_t *action_handlep
, const char *finalsnap
, nvlist_t
*cmdprops
)
4440 dmu_replay_record_t drr
, drr_noswap
;
4441 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
4443 zio_cksum_t zcksum
= { { 0 } };
4444 uint64_t featureflags
;
4447 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4450 /* check cmdline props, raise an error if they cannot be received */
4451 if (!zfs_receive_checkprops(hdl
, cmdprops
, errbuf
)) {
4452 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
4455 if (flags
->isprefix
&&
4456 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
4457 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
4458 "(%s) does not exist"), tosnap
);
4459 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
4462 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
4463 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
4464 "(%s) does not exist"), originsnap
);
4465 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
4468 /* read in the BEGIN record */
4469 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
4473 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
4474 /* It's the double end record at the end of a package */
4478 /* the kernel needs the non-byteswapped begin record */
4481 flags
->byteswap
= B_FALSE
;
4482 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
4484 * We computed the checksum in the wrong byteorder in
4485 * recv_read() above; do it again correctly.
4487 bzero(&zcksum
, sizeof (zio_cksum_t
));
4488 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
4489 flags
->byteswap
= B_TRUE
;
4491 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
4492 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
4493 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
4494 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
4495 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
4496 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
4497 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
4498 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
4499 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
4502 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
4503 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
4504 "stream (bad magic number)"));
4505 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
4508 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
4509 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
4511 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
4512 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
4513 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4514 "stream has unsupported feature, feature flags = %lx"),
4516 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
4519 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
4520 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
4521 "stream (bad snapshot name)"));
4522 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
4525 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
4526 char nonpackage_sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
4527 if (sendfs
== NULL
) {
4529 * We were not called from zfs_receive_package(). Get
4530 * the fs specified by 'zfs send'.
4533 (void) strlcpy(nonpackage_sendfs
,
4534 drr
.drr_u
.drr_begin
.drr_toname
,
4535 sizeof (nonpackage_sendfs
));
4536 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
4538 sendfs
= nonpackage_sendfs
;
4539 VERIFY(finalsnap
== NULL
);
4541 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
4542 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
4543 cleanup_fd
, action_handlep
, finalsnap
, cmdprops
));
4545 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
4546 DMU_COMPOUNDSTREAM
);
4547 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
4548 &zcksum
, top_zfs
, cleanup_fd
, action_handlep
, cmdprops
));
4553 * Restores a backup of tosnap from the file descriptor specified by infd.
4554 * Return 0 on total success, -2 if some things couldn't be
4555 * destroyed/renamed/promoted, -1 if some things couldn't be received.
4556 * (-1 will override -2, if -1 and the resumable flag was specified the
4557 * transfer can be resumed if the sending side supports it).
4560 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
4561 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
4563 char *top_zfs
= NULL
;
4566 uint64_t action_handle
= 0;
4568 char *originsnap
= NULL
;
4571 * The only way fstat can fail is if we do not have a valid file
4574 if (fstat(infd
, &sb
) == -1) {
4580 #ifndef F_SETPIPE_SZ
4581 #define F_SETPIPE_SZ (F_SETLEASE + 7)
4582 #endif /* F_SETPIPE_SZ */
4584 #ifndef F_GETPIPE_SZ
4585 #define F_GETPIPE_SZ (F_GETLEASE + 7)
4586 #endif /* F_GETPIPE_SZ */
4589 * It is not uncommon for gigabytes to be processed in zfs receive.
4590 * Speculatively increase the buffer size via Linux-specific fcntl()
4593 if (S_ISFIFO(sb
.st_mode
)) {
4594 FILE *procf
= fopen("/proc/sys/fs/pipe-max-size", "r");
4596 if (procf
!= NULL
) {
4597 unsigned long max_psize
;
4599 if (fscanf(procf
, "%lu", &max_psize
) > 0) {
4600 cur_psize
= fcntl(infd
, F_GETPIPE_SZ
);
4601 if (cur_psize
> 0 &&
4602 max_psize
> (unsigned long) cur_psize
)
4603 (void) fcntl(infd
, F_SETPIPE_SZ
,
4609 #endif /* __linux__ */
4612 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
4613 if (err
&& err
!= ENOENT
)
4617 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
4618 VERIFY(cleanup_fd
>= 0);
4620 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
4621 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
, NULL
, props
);
4623 VERIFY(0 == close(cleanup_fd
));
4625 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
4626 zfs_handle_t
*zhp
= NULL
;
4627 prop_changelist_t
*clp
= NULL
;
4629 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
4631 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
4632 CL_GATHER_MOUNT_ALWAYS
, 0);
4635 /* mount and share received datasets */
4636 err
= changelist_postfix(clp
);
4637 changelist_free(clp
);
4640 if (zhp
== NULL
|| clp
== NULL
|| err
)