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 https://opensource.org/licenses/CDDL-1.0.
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, 2020 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) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
32 * Copyright (c) 2019 Datto Inc.
45 #include <sys/mount.h>
46 #include <sys/mntent.h>
47 #include <sys/mnttab.h>
49 #include <sys/debug.h>
56 #include <libzfs_core.h>
59 #include "zfs_namecheck.h"
61 #include "zfs_fletcher.h"
62 #include "libzfs_impl.h"
65 #include <sys/zio_checksum.h>
66 #include <sys/dsl_crypt.h>
68 #include <sys/socket.h>
71 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
72 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **,
73 const char *, nvlist_t
*);
74 static int guid_to_name_redact_snaps(libzfs_handle_t
*hdl
, const char *parent
,
75 uint64_t guid
, boolean_t bookmark_ok
, uint64_t *redact_snap_guids
,
76 uint64_t num_redact_snaps
, char *name
);
77 static int guid_to_name(libzfs_handle_t
*, const char *,
78 uint64_t, boolean_t
, char *);
80 typedef struct progress_arg
{
83 boolean_t pa_parsable
;
84 boolean_t pa_estimate
;
87 boolean_t pa_progress
;
92 dump_record(dmu_replay_record_t
*drr
, void *payload
, size_t payload_len
,
93 zio_cksum_t
*zc
, int outfd
)
95 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
96 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
97 fletcher_4_incremental_native(drr
,
98 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
99 if (drr
->drr_type
!= DRR_BEGIN
) {
100 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
101 drr_checksum
.drr_checksum
));
102 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
104 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
105 sizeof (zio_cksum_t
), zc
);
106 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
108 if (payload_len
!= 0) {
109 fletcher_4_incremental_native(payload
, payload_len
, zc
);
110 if (write(outfd
, payload
, payload_len
) == -1)
117 * Routines for dealing with the AVL tree of fs-nvlists
119 typedef struct fsavl_node
{
127 fsavl_compare(const void *arg1
, const void *arg2
)
129 const fsavl_node_t
*fn1
= (const fsavl_node_t
*)arg1
;
130 const fsavl_node_t
*fn2
= (const fsavl_node_t
*)arg2
;
132 return (TREE_CMP(fn1
->fn_guid
, fn2
->fn_guid
));
136 * Given the GUID of a snapshot, find its containing filesystem and
140 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
142 fsavl_node_t fn_find
;
145 fn_find
.fn_guid
= snapguid
;
147 fn
= avl_find(avl
, &fn_find
, NULL
);
150 *snapname
= fn
->fn_snapname
;
151 return (fn
->fn_nvfs
);
157 fsavl_destroy(avl_tree_t
*avl
)
166 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
173 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
176 fsavl_create(nvlist_t
*fss
)
179 nvpair_t
*fselem
= NULL
;
181 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
184 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
185 offsetof(fsavl_node_t
, fn_node
));
187 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
188 nvlist_t
*nvfs
, *snaps
;
189 nvpair_t
*snapelem
= NULL
;
191 nvfs
= fnvpair_value_nvlist(fselem
);
192 snaps
= fnvlist_lookup_nvlist(nvfs
, "snaps");
195 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
198 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
199 fsavl_destroy(fsavl
);
203 fn
->fn_snapname
= nvpair_name(snapelem
);
204 fn
->fn_guid
= fnvpair_value_uint64(snapelem
);
207 * Note: if there are multiple snaps with the
208 * same GUID, we ignore all but one.
210 avl_index_t where
= 0;
211 if (avl_find(fsavl
, fn
, &where
) == NULL
)
212 avl_insert(fsavl
, fn
, where
);
222 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
224 typedef struct send_data
{
226 * assigned inside every recursive call,
227 * restored from *_save on return:
229 * guid of fromsnap snapshot in parent dataset
230 * txg of fromsnap snapshot in current dataset
231 * txg of tosnap snapshot in current dataset
234 uint64_t parent_fromsnap_guid
;
235 uint64_t fromsnap_txg
;
238 /* the nvlists get accumulated during depth-first traversal */
239 nvlist_t
*parent_snaps
;
242 nvlist_t
*snapholds
; /* user holds */
244 /* send-receive configuration, does not change during traversal */
246 const char *fromsnap
;
252 boolean_t skipmissing
;
257 boolean_t holds
; /* were holds requested with send -h */
261 * The header nvlist is of the following format:
264 * "fromsnap" -> string (if incremental)
268 * "name" -> string (full name; for debugging)
269 * "parentfromsnap" -> number (guid of fromsnap in parent)
271 * "props" -> { name -> value (only if set here) }
272 * "snaps" -> { name (lastname) -> number (guid) }
273 * "snapprops" -> { name (lastname) -> { name -> value } }
274 * "snapholds" -> { name (lastname) -> { holdname -> crtime } }
276 * "origin" -> number (guid) (if clone)
277 * "is_encroot" -> boolean
278 * "sent" -> boolean (not on-disk)
287 send_iterate_prop(zfs_handle_t
*zhp
, boolean_t received_only
, nvlist_t
*nv
);
290 * Collect guid, valid props, optionally holds, etc. of a snapshot.
291 * This interface is intended for use as a zfs_iter_snapshots_sorted visitor.
294 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
296 send_data_t
*sd
= arg
;
297 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
298 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
299 boolean_t isfromsnap
, istosnap
, istosnapwithnofrom
;
301 const char *from
= sd
->fromsnap
;
302 const char *to
= sd
->tosnap
;
304 snapname
= strrchr(zhp
->zfs_name
, '@');
305 assert(snapname
!= NULL
);
308 isfromsnap
= (from
!= NULL
&& strcmp(from
, snapname
) == 0);
309 istosnap
= (to
!= NULL
&& strcmp(to
, snapname
) == 0);
310 istosnapwithnofrom
= (istosnap
&& from
== NULL
);
312 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
314 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
315 "skipping snapshot %s because it was created "
316 "after the destination snapshot (%s)\n"),
323 fnvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
);
326 * NB: if there is no fromsnap here (it's a newly created fs in
327 * an incremental replication), we will substitute the tosnap.
329 if (isfromsnap
|| (sd
->parent_fromsnap_guid
== 0 && istosnap
))
330 sd
->parent_fromsnap_guid
= guid
;
332 if (!sd
->recursive
) {
334 * To allow a doall stream to work properly
335 * with a NULL fromsnap
337 if (sd
->doall
&& from
== NULL
&& !sd
->seenfrom
)
338 sd
->seenfrom
= B_TRUE
;
340 if (!sd
->seenfrom
&& isfromsnap
) {
341 sd
->seenfrom
= B_TRUE
;
346 if ((sd
->seento
|| !sd
->seenfrom
) && !istosnapwithnofrom
) {
355 nvlist_t
*nv
= fnvlist_alloc();
356 send_iterate_prop(zhp
, sd
->backup
, nv
);
357 fnvlist_add_nvlist(sd
->snapprops
, snapname
, nv
);
362 if (lzc_get_holds(zhp
->zfs_name
, &holds
) == 0) {
363 fnvlist_add_nvlist(sd
->snapholds
, snapname
, holds
);
373 * Collect all valid props from the handle snap into an nvlist.
376 send_iterate_prop(zfs_handle_t
*zhp
, boolean_t received_only
, nvlist_t
*nv
)
381 props
= zfs_get_recvd_props(zhp
);
383 props
= zhp
->zfs_props
;
385 nvpair_t
*elem
= NULL
;
386 while ((elem
= nvlist_next_nvpair(props
, elem
)) != NULL
) {
387 char *propname
= nvpair_name(elem
);
388 zfs_prop_t prop
= zfs_name_to_prop(propname
);
390 if (!zfs_prop_user(propname
)) {
392 * Realistically, this should never happen. However,
393 * we want the ability to add DSL properties without
394 * needing to make incompatible version changes. We
395 * need to ignore unknown properties to allow older
396 * software to still send datasets containing these
397 * properties, with the unknown properties elided.
399 if (prop
== ZPROP_INVAL
)
402 if (zfs_prop_readonly(prop
))
406 nvlist_t
*propnv
= fnvpair_value_nvlist(elem
);
408 boolean_t isspacelimit
= (prop
== ZFS_PROP_QUOTA
||
409 prop
== ZFS_PROP_RESERVATION
||
410 prop
== ZFS_PROP_REFQUOTA
||
411 prop
== ZFS_PROP_REFRESERVATION
);
412 if (isspacelimit
&& zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
416 if (nvlist_lookup_string(propnv
, ZPROP_SOURCE
, &source
) == 0) {
417 if (strcmp(source
, zhp
->zfs_name
) != 0 &&
418 strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0)
422 * May have no source before SPA_VERSION_RECVD_PROPS,
423 * but is still modifiable.
429 if (zfs_prop_user(propname
) ||
430 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
432 value
= fnvlist_lookup_string(propnv
, ZPROP_VALUE
);
433 fnvlist_add_string(nv
, propname
, value
);
436 value
= fnvlist_lookup_uint64(propnv
, ZPROP_VALUE
);
437 fnvlist_add_uint64(nv
, propname
, value
);
443 * returns snapshot guid
444 * and returns 0 if the snapshot does not exist
447 get_snap_guid(libzfs_handle_t
*hdl
, const char *fs
, const char *snap
)
449 char name
[MAXPATHLEN
+ 1];
452 if (fs
== NULL
|| fs
[0] == '\0' || snap
== NULL
|| snap
[0] == '\0')
455 (void) snprintf(name
, sizeof (name
), "%s@%s", fs
, snap
);
456 zfs_handle_t
*zhp
= zfs_open(hdl
, name
, ZFS_TYPE_SNAPSHOT
);
458 guid
= zfs_prop_get_int(zhp
, ZFS_PROP_GUID
);
466 * returns snapshot creation txg
467 * and returns 0 if the snapshot does not exist
470 get_snap_txg(libzfs_handle_t
*hdl
, const char *fs
, const char *snap
)
472 char name
[ZFS_MAX_DATASET_NAME_LEN
];
475 if (fs
== NULL
|| fs
[0] == '\0' || snap
== NULL
|| snap
[0] == '\0')
478 (void) snprintf(name
, sizeof (name
), "%s@%s", fs
, snap
);
479 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_SNAPSHOT
)) {
480 zfs_handle_t
*zhp
= zfs_open(hdl
, name
, ZFS_TYPE_SNAPSHOT
);
482 txg
= zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
);
491 * Recursively generate nvlists describing datasets. See comment
492 * for the data structure send_data_t above for description of contents
496 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
498 send_data_t
*sd
= arg
;
499 nvlist_t
*nvfs
= NULL
, *nv
= NULL
;
501 uint64_t min_txg
= 0, max_txg
= 0;
502 uint64_t txg
= zhp
->zfs_dmustats
.dds_creation_txg
;
503 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
504 uint64_t fromsnap_txg
, tosnap_txg
;
507 /* These fields are restored on return from a recursive call. */
508 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
509 uint64_t fromsnap_txg_save
= sd
->fromsnap_txg
;
510 uint64_t tosnap_txg_save
= sd
->tosnap_txg
;
512 fromsnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->fromsnap
);
513 if (fromsnap_txg
!= 0)
514 sd
->fromsnap_txg
= fromsnap_txg
;
516 tosnap_txg
= get_snap_txg(zhp
->zfs_hdl
, zhp
->zfs_name
, sd
->tosnap
);
518 sd
->tosnap_txg
= tosnap_txg
;
521 * On the send side, if the current dataset does not have tosnap,
522 * perform two additional checks:
524 * - Skip sending the current dataset if it was created later than
526 * - Return error if the current dataset was created earlier than
527 * the parent tosnap, unless --skip-missing specified. Then
528 * just print a warning.
530 if (sd
->tosnap
!= NULL
&& tosnap_txg
== 0) {
531 if (sd
->tosnap_txg
!= 0 && txg
> sd
->tosnap_txg
) {
533 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
534 "skipping dataset %s: snapshot %s does "
535 "not exist\n"), zhp
->zfs_name
, sd
->tosnap
);
537 } else if (sd
->skipmissing
) {
538 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
539 "WARNING: skipping dataset %s and its children:"
540 " snapshot %s does not exist\n"),
541 zhp
->zfs_name
, sd
->tosnap
);
543 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
544 "cannot send %s@%s%s: snapshot %s@%s does not "
545 "exist\n"), sd
->fsname
, sd
->tosnap
, sd
->recursive
?
546 dgettext(TEXT_DOMAIN
, " recursively") : "",
547 zhp
->zfs_name
, sd
->tosnap
);
553 nvfs
= fnvlist_alloc();
554 fnvlist_add_string(nvfs
, "name", zhp
->zfs_name
);
555 fnvlist_add_uint64(nvfs
, "parentfromsnap", sd
->parent_fromsnap_guid
);
557 if (zhp
->zfs_dmustats
.dds_origin
[0] != '\0') {
558 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
559 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
560 if (origin
== NULL
) {
564 fnvlist_add_uint64(nvfs
, "origin",
565 origin
->zfs_dmustats
.dds_guid
);
569 /* Iterate over props. */
570 if (sd
->props
|| sd
->backup
|| sd
->recursive
) {
571 nv
= fnvlist_alloc();
572 send_iterate_prop(zhp
, sd
->backup
, nv
);
573 fnvlist_add_nvlist(nvfs
, "props", nv
);
575 if (zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) != ZIO_CRYPT_OFF
) {
578 /* Determine if this dataset is an encryption root. */
579 if (zfs_crypto_get_encryption_root(zhp
, &encroot
, NULL
) != 0) {
585 fnvlist_add_boolean(nvfs
, "is_encroot");
588 * Encrypted datasets can only be sent with properties if
589 * the raw flag is specified because the receive side doesn't
590 * currently have a mechanism for recursively asking the user
591 * for new encryption parameters.
594 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
595 "cannot send %s@%s: encrypted dataset %s may not "
596 "be sent with properties without the raw flag\n"),
597 sd
->fsname
, sd
->tosnap
, zhp
->zfs_name
);
605 * Iterate over snaps, and set sd->parent_fromsnap_guid.
607 * If this is a "doall" send, a replicate send or we're just trying
608 * to gather a list of previous snapshots, iterate through all the
609 * snaps in the txg range. Otherwise just look at the one we're
612 sd
->parent_fromsnap_guid
= 0;
613 sd
->parent_snaps
= fnvlist_alloc();
614 sd
->snapprops
= fnvlist_alloc();
616 sd
->snapholds
= fnvlist_alloc();
617 if (sd
->doall
|| sd
->replicate
|| sd
->tosnap
== NULL
) {
618 if (!sd
->replicate
&& fromsnap_txg
!= 0)
619 min_txg
= fromsnap_txg
;
620 if (!sd
->replicate
&& tosnap_txg
!= 0)
621 max_txg
= tosnap_txg
;
622 (void) zfs_iter_snapshots_sorted(zhp
, 0, send_iterate_snap
, sd
,
625 char snapname
[MAXPATHLEN
] = { 0 };
628 (void) snprintf(snapname
, sizeof (snapname
), "%s@%s",
629 zhp
->zfs_name
, sd
->tosnap
);
630 if (sd
->fromsnap
!= NULL
)
631 sd
->seenfrom
= B_TRUE
;
632 snap
= zfs_open(zhp
->zfs_hdl
, snapname
, ZFS_TYPE_SNAPSHOT
);
634 (void) send_iterate_snap(snap
, sd
);
637 fnvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
);
638 fnvlist_free(sd
->parent_snaps
);
639 fnvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
);
640 fnvlist_free(sd
->snapprops
);
642 fnvlist_add_nvlist(nvfs
, "snapholds", sd
->snapholds
);
643 fnvlist_free(sd
->snapholds
);
646 /* Do not allow the size of the properties list to exceed the limit */
647 if ((fnvlist_size(nvfs
) + fnvlist_size(sd
->fss
)) >
648 zhp
->zfs_hdl
->libzfs_max_nvlist
) {
649 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
650 "warning: cannot send %s@%s: the size of the list of "
651 "snapshots and properties is too large to be received "
653 "Select a smaller number of snapshots to send.\n"),
654 zhp
->zfs_name
, sd
->tosnap
);
658 /* Add this fs to nvlist. */
659 (void) snprintf(guidstring
, sizeof (guidstring
),
660 "0x%llx", (longlong_t
)guid
);
661 fnvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
);
663 /* Iterate over children. */
665 rv
= zfs_iter_filesystems(zhp
, 0, send_iterate_fs
, sd
);
668 /* Restore saved fields. */
669 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
670 sd
->fromsnap_txg
= fromsnap_txg_save
;
671 sd
->tosnap_txg
= tosnap_txg_save
;
681 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
682 const char *tosnap
, boolean_t recursive
, boolean_t raw
, boolean_t doall
,
683 boolean_t replicate
, boolean_t skipmissing
, boolean_t verbose
,
684 boolean_t backup
, boolean_t holds
, boolean_t props
, nvlist_t
**nvlp
,
688 send_data_t sd
= { 0 };
691 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
693 return (EZFS_BADTYPE
);
695 sd
.fss
= fnvlist_alloc();
697 sd
.fromsnap
= fromsnap
;
699 sd
.recursive
= recursive
;
702 sd
.replicate
= replicate
;
703 sd
.skipmissing
= skipmissing
;
704 sd
.verbose
= verbose
;
709 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
710 fnvlist_free(sd
.fss
);
717 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
718 fnvlist_free(sd
.fss
);
728 * Routines specific to "zfs send"
730 typedef struct send_dump_data
{
731 /* these are all just the short snapname (the part after the @) */
732 const char *fromsnap
;
734 char prevsnap
[ZFS_MAX_DATASET_NAME_LEN
];
735 uint64_t prevsnap_obj
;
736 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
737 boolean_t dryrun
, parsable
, progress
, embed_data
, std_out
;
738 boolean_t large_block
, compress
, raw
, holds
;
739 boolean_t progressastitle
;
745 snapfilter_cb_t
*filter_cb
;
748 char holdtag
[ZFS_MAX_DATASET_NAME_LEN
];
755 zfs_send_space(zfs_handle_t
*zhp
, const char *snapname
, const char *from
,
756 enum lzc_send_flags flags
, uint64_t *spacep
)
758 assert(snapname
!= NULL
);
760 int error
= lzc_send_space(snapname
, from
, flags
, spacep
);
764 char errbuf
[ERRBUFLEN
];
765 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
766 "warning: cannot estimate space for '%s'"), snapname
);
768 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
771 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
772 "not an earlier snapshot from the same fs"));
773 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
776 if (zfs_dataset_exists(hdl
, snapname
,
777 ZFS_TYPE_SNAPSHOT
)) {
778 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
779 "incremental source (%s) does not exist"),
782 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
796 zfs_error_aux(hdl
, "%s", strerror(error
));
797 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
800 return (zfs_standard_error(hdl
, error
, errbuf
));
805 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
806 * NULL) to the file descriptor specified by outfd.
809 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
810 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
813 zfs_cmd_t zc
= {"\0"};
814 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
817 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
818 assert(fromsnap_obj
== 0 || !fromorigin
);
820 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
821 zc
.zc_cookie
= outfd
;
822 zc
.zc_obj
= fromorigin
;
823 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
824 zc
.zc_fromobj
= fromsnap_obj
;
827 if (debugnv
!= NULL
) {
828 thisdbg
= fnvlist_alloc();
829 if (fromsnap
!= NULL
&& fromsnap
[0] != '\0')
830 fnvlist_add_string(thisdbg
, "fromsnap", fromsnap
);
833 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
834 char errbuf
[ERRBUFLEN
];
837 (void) snprintf(errbuf
, sizeof (errbuf
), "%s '%s'",
838 dgettext(TEXT_DOMAIN
, "warning: cannot send"),
841 if (debugnv
!= NULL
) {
842 fnvlist_add_uint64(thisdbg
, "error", error
);
843 fnvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
);
844 fnvlist_free(thisdbg
);
849 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
850 "not an earlier snapshot from the same fs"));
851 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
854 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
855 "source key must be loaded"));
856 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
859 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
860 ZFS_TYPE_SNAPSHOT
)) {
861 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
862 "incremental source (@%s) does not exist"),
865 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
879 zfs_error_aux(hdl
, "%s", strerror(errno
));
880 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
883 return (zfs_standard_error(hdl
, errno
, errbuf
));
887 if (debugnv
!= NULL
) {
888 fnvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
);
889 fnvlist_free(thisdbg
);
896 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
898 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
901 * zfs_send() only sets snapholds for sends that need them,
902 * e.g. replication and doall.
904 if (sdd
->snapholds
== NULL
)
907 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
911 zfs_send_progress(zfs_handle_t
*zhp
, int fd
, uint64_t *bytes_written
,
912 uint64_t *blocks_visited
)
914 zfs_cmd_t zc
= {"\0"};
916 if (bytes_written
!= NULL
)
918 if (blocks_visited
!= NULL
)
920 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
922 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
924 if (bytes_written
!= NULL
)
925 *bytes_written
= zc
.zc_cookie
;
926 if (blocks_visited
!= NULL
)
927 *blocks_visited
= zc
.zc_objset_type
;
932 send_progress_thread(void *arg
)
934 progress_arg_t
*pa
= arg
;
935 zfs_handle_t
*zhp
= pa
->pa_zhp
;
938 uint64_t total
= pa
->pa_size
/ 100;
944 if (!pa
->pa_parsable
&& pa
->pa_progress
) {
945 (void) fprintf(stderr
,
946 "TIME %s %sSNAPSHOT %s\n",
947 pa
->pa_estimate
? "BYTES" : " SENT",
948 pa
->pa_verbosity
>= 2 ? " BLOCKS " : "",
953 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
957 if ((err
= zfs_send_progress(zhp
, pa
->pa_fd
, &bytes
,
959 if (err
== EINTR
|| err
== ENOENT
)
961 return ((void *)(uintptr_t)err
);
965 localtime_r(&t
, &tm
);
967 if (pa
->pa_astitle
) {
971 zfs_nicenum(bytes
, buf_bytes
, sizeof (buf_bytes
));
972 zfs_nicenum(pa
->pa_size
, buf_size
, sizeof (buf_size
));
973 pct
= (total
> 0) ? bytes
/ total
: 100;
974 zfs_setproctitle("sending %s (%d%%: %s/%s)",
975 zhp
->zfs_name
, MIN(pct
, 100), buf_bytes
, buf_size
);
978 if (pa
->pa_verbosity
>= 2 && pa
->pa_parsable
) {
979 (void) fprintf(stderr
,
980 "%02d:%02d:%02d\t%llu\t%llu\t%s\n",
981 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
982 (u_longlong_t
)bytes
, (u_longlong_t
)blocks
,
984 } else if (pa
->pa_verbosity
>= 2) {
985 zfs_nicenum(bytes
, buf
, sizeof (buf
));
986 (void) fprintf(stderr
,
987 "%02d:%02d:%02d %5s %8llu %s\n",
988 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
989 buf
, (u_longlong_t
)blocks
, zhp
->zfs_name
);
990 } else if (pa
->pa_parsable
) {
991 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
992 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
993 (u_longlong_t
)bytes
, zhp
->zfs_name
);
994 } else if (pa
->pa_progress
) {
995 zfs_nicebytes(bytes
, buf
, sizeof (buf
));
996 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
997 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
,
1004 send_progress_thread_exit(libzfs_handle_t
*hdl
, pthread_t ptid
)
1006 void *status
= NULL
;
1007 (void) pthread_cancel(ptid
);
1008 (void) pthread_join(ptid
, &status
);
1009 int error
= (int)(uintptr_t)status
;
1010 if (error
!= 0 && status
!= PTHREAD_CANCELED
)
1011 return (zfs_standard_error(hdl
, error
,
1012 dgettext(TEXT_DOMAIN
, "progress thread exited nonzero")));
1018 send_print_verbose(FILE *fout
, const char *tosnap
, const char *fromsnap
,
1019 uint64_t size
, boolean_t parsable
)
1022 if (fromsnap
!= NULL
) {
1023 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1024 "incremental\t%s\t%s"), fromsnap
, tosnap
);
1027 * Workaround for GCC 12+ with UBSan enabled deficencies.
1029 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1030 * below as violating -Wformat-overflow.
1032 #if defined(__GNUC__) && !defined(__clang__) && \
1033 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1034 #pragma GCC diagnostic push
1035 #pragma GCC diagnostic ignored "-Wformat-overflow"
1037 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1038 "full\t%s"), tosnap
);
1039 #if defined(__GNUC__) && !defined(__clang__) && \
1040 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1041 #pragma GCC diagnostic pop
1044 (void) fprintf(fout
, "\t%llu", (longlong_t
)size
);
1046 if (fromsnap
!= NULL
) {
1047 if (strchr(fromsnap
, '@') == NULL
&&
1048 strchr(fromsnap
, '#') == NULL
) {
1049 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1050 "send from @%s to %s"), fromsnap
, tosnap
);
1052 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1053 "send from %s to %s"), fromsnap
, tosnap
);
1056 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1057 "full send of %s"), tosnap
);
1061 zfs_nicebytes(size
, buf
, sizeof (buf
));
1063 * Workaround for GCC 12+ with UBSan enabled deficencies.
1065 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1066 * below as violating -Wformat-overflow.
1068 #if defined(__GNUC__) && !defined(__clang__) && \
1069 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1070 #pragma GCC diagnostic push
1071 #pragma GCC diagnostic ignored "-Wformat-overflow"
1073 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1074 " estimated size is %s"), buf
);
1075 #if defined(__GNUC__) && !defined(__clang__) && \
1076 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1077 #pragma GCC diagnostic pop
1081 (void) fprintf(fout
, "\n");
1085 * Send a single filesystem snapshot, updating the send dump data.
1086 * This interface is intended for use as a zfs_iter_snapshots_sorted visitor.
1089 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1091 send_dump_data_t
*sdd
= arg
;
1092 progress_arg_t pa
= { 0 };
1095 enum lzc_send_flags flags
= 0;
1097 boolean_t isfromsnap
, istosnap
, fromorigin
;
1098 boolean_t exclude
= B_FALSE
;
1099 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1102 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1103 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1104 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1106 if (!sdd
->seenfrom
&& isfromsnap
) {
1107 gather_holds(zhp
, sdd
);
1108 sdd
->seenfrom
= B_TRUE
;
1109 (void) strlcpy(sdd
->prevsnap
, thissnap
, sizeof (sdd
->prevsnap
));
1110 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1115 if (sdd
->seento
|| !sdd
->seenfrom
) {
1120 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1122 sdd
->seento
= B_TRUE
;
1124 if (sdd
->large_block
)
1125 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1126 if (sdd
->embed_data
)
1127 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1129 flags
|= LZC_SEND_FLAG_COMPRESS
;
1131 flags
|= LZC_SEND_FLAG_RAW
;
1133 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1134 if (sdd
->replicate
) {
1136 nvlist_t
*snapprops
;
1138 * Filter out all intermediate snapshots except origin
1139 * snapshots needed to replicate clones.
1141 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1142 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1145 snapprops
= fnvlist_lookup_nvlist(nvfs
,
1147 snapprops
= fnvlist_lookup_nvlist(snapprops
,
1149 exclude
= !nvlist_exists(snapprops
,
1158 * If a filter function exists, call it to determine whether
1159 * this snapshot will be sent.
1161 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1162 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1164 * This snapshot is filtered out. Don't send it, and don't
1165 * set prevsnap_obj, so it will be as if this snapshot didn't
1166 * exist, and the next accepted snapshot will be sent as
1167 * an incremental from the last accepted one, or as the
1168 * first (and full) snapshot in the case of a replication,
1169 * non-incremental send.
1175 gather_holds(zhp
, sdd
);
1176 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1177 (sdd
->fromorigin
|| sdd
->replicate
);
1179 if (sdd
->verbosity
!= 0) {
1181 char fromds
[ZFS_MAX_DATASET_NAME_LEN
];
1183 if (sdd
->prevsnap
[0] != '\0') {
1184 (void) strlcpy(fromds
, zhp
->zfs_name
, sizeof (fromds
));
1185 *(strchr(fromds
, '@') + 1) = '\0';
1186 (void) strlcat(fromds
, sdd
->prevsnap
, sizeof (fromds
));
1188 if (zfs_send_space(zhp
, zhp
->zfs_name
,
1189 sdd
->prevsnap
[0] ? fromds
: NULL
, flags
, &size
) == 0) {
1190 send_print_verbose(fout
, zhp
->zfs_name
,
1191 sdd
->prevsnap
[0] ? sdd
->prevsnap
: NULL
,
1192 size
, sdd
->parsable
);
1199 * If progress reporting is requested, spawn a new thread to
1200 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1202 if (sdd
->progress
|| sdd
->progressastitle
) {
1204 pa
.pa_fd
= sdd
->outfd
;
1205 pa
.pa_parsable
= sdd
->parsable
;
1206 pa
.pa_estimate
= B_FALSE
;
1207 pa
.pa_verbosity
= sdd
->verbosity
;
1208 pa
.pa_size
= sdd
->size
;
1209 pa
.pa_astitle
= sdd
->progressastitle
;
1210 pa
.pa_progress
= sdd
->progress
;
1212 if ((err
= pthread_create(&tid
, NULL
,
1213 send_progress_thread
, &pa
)) != 0) {
1219 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1220 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1222 if ((sdd
->progress
|| sdd
->progressastitle
) &&
1223 send_progress_thread_exit(zhp
->zfs_hdl
, tid
))
1227 (void) strlcpy(sdd
->prevsnap
, thissnap
, sizeof (sdd
->prevsnap
));
1228 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1234 * Send all snapshots for a filesystem, updating the send dump data.
1237 dump_filesystem(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
1240 boolean_t missingfrom
= B_FALSE
;
1241 zfs_cmd_t zc
= {"\0"};
1242 uint64_t min_txg
= 0, max_txg
= 0;
1245 * Make sure the tosnap exists.
1247 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1248 zhp
->zfs_name
, sdd
->tosnap
);
1249 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1250 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1251 "WARNING: could not send %s@%s: does not exist\n"),
1252 zhp
->zfs_name
, sdd
->tosnap
);
1258 * If this fs does not have fromsnap, and we're doing
1259 * recursive, we need to send a full stream from the
1260 * beginning (or an incremental from the origin if this
1261 * is a clone). If we're doing non-recursive, then let
1262 * them get the error.
1264 if (sdd
->replicate
&& sdd
->fromsnap
) {
1266 * Make sure the fromsnap exists.
1268 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1269 zhp
->zfs_name
, sdd
->fromsnap
);
1270 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0)
1271 missingfrom
= B_TRUE
;
1274 sdd
->seenfrom
= sdd
->seento
= B_FALSE
;
1275 sdd
->prevsnap
[0] = '\0';
1276 sdd
->prevsnap_obj
= 0;
1277 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1278 sdd
->seenfrom
= B_TRUE
;
1281 * Iterate through all snapshots and process the ones we will be
1282 * sending. If we only have a "from" and "to" snapshot to deal
1283 * with, we can avoid iterating through all the other snapshots.
1285 if (sdd
->doall
|| sdd
->replicate
|| sdd
->tosnap
== NULL
) {
1286 if (!sdd
->replicate
) {
1287 if (sdd
->fromsnap
!= NULL
) {
1288 min_txg
= get_snap_txg(zhp
->zfs_hdl
,
1289 zhp
->zfs_name
, sdd
->fromsnap
);
1291 if (sdd
->tosnap
!= NULL
) {
1292 max_txg
= get_snap_txg(zhp
->zfs_hdl
,
1293 zhp
->zfs_name
, sdd
->tosnap
);
1296 rv
= zfs_iter_snapshots_sorted(zhp
, 0, dump_snapshot
, sdd
,
1299 char snapname
[MAXPATHLEN
] = { 0 };
1302 /* Dump fromsnap. */
1303 if (!sdd
->seenfrom
) {
1304 (void) snprintf(snapname
, sizeof (snapname
),
1305 "%s@%s", zhp
->zfs_name
, sdd
->fromsnap
);
1306 snap
= zfs_open(zhp
->zfs_hdl
, snapname
,
1309 rv
= dump_snapshot(snap
, sdd
);
1316 (void) snprintf(snapname
, sizeof (snapname
),
1317 "%s@%s", zhp
->zfs_name
, sdd
->tosnap
);
1318 snap
= zfs_open(zhp
->zfs_hdl
, snapname
,
1321 rv
= dump_snapshot(snap
, sdd
);
1327 if (!sdd
->seenfrom
) {
1328 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1329 "WARNING: could not send %s@%s:\n"
1330 "incremental source (%s@%s) does not exist\n"),
1331 zhp
->zfs_name
, sdd
->tosnap
,
1332 zhp
->zfs_name
, sdd
->fromsnap
);
1334 } else if (!sdd
->seento
) {
1335 if (sdd
->fromsnap
) {
1336 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1337 "WARNING: could not send %s@%s:\n"
1338 "incremental source (%s@%s) "
1339 "is not earlier than it\n"),
1340 zhp
->zfs_name
, sdd
->tosnap
,
1341 zhp
->zfs_name
, sdd
->fromsnap
);
1343 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1345 "could not send %s@%s: does not exist\n"),
1346 zhp
->zfs_name
, sdd
->tosnap
);
1355 * Send all snapshots for all filesystems in sdd.
1358 dump_filesystems(zfs_handle_t
*rzhp
, send_dump_data_t
*sdd
)
1361 boolean_t needagain
, progress
;
1363 if (!sdd
->replicate
)
1364 return (dump_filesystem(rzhp
, sdd
));
1366 /* Mark the clone origin snapshots. */
1367 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1368 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1370 uint64_t origin_guid
= 0;
1372 nvfs
= fnvpair_value_nvlist(fspair
);
1373 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1374 if (origin_guid
!= 0) {
1376 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1377 origin_guid
, &snapname
);
1378 if (origin_nv
!= NULL
) {
1379 nvlist_t
*snapprops
;
1380 snapprops
= fnvlist_lookup_nvlist(origin_nv
,
1382 snapprops
= fnvlist_lookup_nvlist(snapprops
,
1384 fnvlist_add_boolean(snapprops
,
1390 needagain
= progress
= B_FALSE
;
1391 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1392 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1393 nvlist_t
*fslist
, *parent_nv
;
1397 uint64_t origin_guid
= 0;
1398 uint64_t parent_guid
= 0;
1400 fslist
= fnvpair_value_nvlist(fspair
);
1401 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1404 fsname
= fnvlist_lookup_string(fslist
, "name");
1405 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1406 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1409 if (parent_guid
!= 0) {
1410 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1411 if (!nvlist_exists(parent_nv
, "sent")) {
1412 /* Parent has not been sent; skip this one. */
1418 if (origin_guid
!= 0) {
1419 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1421 if (origin_nv
!= NULL
&&
1422 !nvlist_exists(origin_nv
, "sent")) {
1424 * Origin has not been sent yet;
1432 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1435 err
= dump_filesystem(zhp
, sdd
);
1436 fnvlist_add_boolean(fslist
, "sent");
1447 /* Clean out the sent flags in case we reuse this fss. */
1448 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1449 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1452 fslist
= fnvpair_value_nvlist(fspair
);
1453 (void) nvlist_remove_all(fslist
, "sent");
1460 zfs_send_resume_token_to_nvlist(libzfs_handle_t
*hdl
, const char *token
)
1462 unsigned int version
;
1464 unsigned long long checksum
, packed_len
;
1467 * Decode token header, which is:
1468 * <token version>-<checksum of payload>-<uncompressed payload length>
1469 * Note that the only supported token version is 1.
1471 nread
= sscanf(token
, "%u-%llx-%llx-",
1472 &version
, &checksum
, &packed_len
);
1474 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1475 "resume token is corrupt (invalid format)"));
1479 if (version
!= ZFS_SEND_RESUME_TOKEN_VERSION
) {
1480 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1481 "resume token is corrupt (invalid version %u)"),
1486 /* Convert hexadecimal representation to binary. */
1487 token
= strrchr(token
, '-') + 1;
1488 int len
= strlen(token
) / 2;
1489 unsigned char *compressed
= zfs_alloc(hdl
, len
);
1490 for (i
= 0; i
< len
; i
++) {
1491 nread
= sscanf(token
+ i
* 2, "%2hhx", compressed
+ i
);
1494 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1495 "resume token is corrupt "
1496 "(payload is not hex-encoded)"));
1501 /* Verify checksum. */
1503 fletcher_4_native_varsize(compressed
, len
, &cksum
);
1504 if (cksum
.zc_word
[0] != checksum
) {
1506 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1507 "resume token is corrupt (incorrect checksum)"));
1512 void *packed
= zfs_alloc(hdl
, packed_len
);
1513 uLongf packed_len_long
= packed_len
;
1514 if (uncompress(packed
, &packed_len_long
, compressed
, len
) != Z_OK
||
1515 packed_len_long
!= packed_len
) {
1518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1519 "resume token is corrupt (decompression failed)"));
1523 /* Unpack nvlist. */
1525 int error
= nvlist_unpack(packed
, packed_len
, &nv
, KM_SLEEP
);
1529 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1530 "resume token is corrupt (nvlist_unpack failed)"));
1536 static enum lzc_send_flags
1537 lzc_flags_from_sendflags(const sendflags_t
*flags
)
1539 enum lzc_send_flags lzc_flags
= 0;
1541 if (flags
->largeblock
)
1542 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1543 if (flags
->embed_data
)
1544 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1545 if (flags
->compress
)
1546 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
1548 lzc_flags
|= LZC_SEND_FLAG_RAW
;
1550 lzc_flags
|= LZC_SEND_FLAG_SAVED
;
1556 estimate_size(zfs_handle_t
*zhp
, const char *from
, int fd
, sendflags_t
*flags
,
1557 uint64_t resumeobj
, uint64_t resumeoff
, uint64_t bytes
,
1558 const char *redactbook
, char *errbuf
, uint64_t *sizep
)
1561 FILE *fout
= flags
->dryrun
? stdout
: stderr
;
1562 progress_arg_t pa
= { 0 };
1566 if (flags
->progress
|| flags
->progressastitle
) {
1569 pa
.pa_parsable
= flags
->parsable
;
1570 pa
.pa_estimate
= B_TRUE
;
1571 pa
.pa_verbosity
= flags
->verbosity
;
1573 err
= pthread_create(&ptid
, NULL
,
1574 send_progress_thread
, &pa
);
1576 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(errno
));
1577 return (zfs_error(zhp
->zfs_hdl
,
1578 EZFS_THREADCREATEFAILED
, errbuf
));
1582 err
= lzc_send_space_resume_redacted(zhp
->zfs_name
, from
,
1583 lzc_flags_from_sendflags(flags
), resumeobj
, resumeoff
, bytes
,
1584 redactbook
, fd
, &size
);
1587 if ((flags
->progress
|| flags
->progressastitle
) &&
1588 send_progress_thread_exit(zhp
->zfs_hdl
, ptid
))
1591 if (!flags
->progress
&& !flags
->parsable
)
1595 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(err
));
1596 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADBACKUP
,
1599 send_print_verbose(fout
, zhp
->zfs_name
, from
, size
,
1602 if (flags
->parsable
) {
1603 (void) fprintf(fout
, "size\t%llu\n", (longlong_t
)size
);
1606 zfs_nicenum(size
, buf
, sizeof (buf
));
1607 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1608 "total estimated size is %s\n"), buf
);
1614 redact_snaps_contains(const uint64_t *snaps
, uint64_t num_snaps
, uint64_t guid
)
1616 for (int i
= 0; i
< num_snaps
; i
++) {
1617 if (snaps
[i
] == guid
)
1624 redact_snaps_equal(const uint64_t *snaps1
, uint64_t num_snaps1
,
1625 const uint64_t *snaps2
, uint64_t num_snaps2
)
1627 if (num_snaps1
!= num_snaps2
)
1629 for (int i
= 0; i
< num_snaps1
; i
++) {
1630 if (!redact_snaps_contains(snaps2
, num_snaps2
, snaps1
[i
]))
1637 get_bookmarks(const char *path
, nvlist_t
**bmarksp
)
1639 nvlist_t
*props
= fnvlist_alloc();
1642 fnvlist_add_boolean(props
, "redact_complete");
1643 fnvlist_add_boolean(props
, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
));
1644 error
= lzc_get_bookmarks(path
, props
, bmarksp
);
1645 fnvlist_free(props
);
1650 find_redact_pair(nvlist_t
*bmarks
, const uint64_t *redact_snap_guids
,
1651 int num_redact_snaps
)
1655 for (pair
= nvlist_next_nvpair(bmarks
, NULL
); pair
;
1656 pair
= nvlist_next_nvpair(bmarks
, pair
)) {
1658 nvlist_t
*bmark
= fnvpair_value_nvlist(pair
);
1659 nvlist_t
*vallist
= fnvlist_lookup_nvlist(bmark
,
1660 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
));
1662 uint64_t *bmarksnaps
= fnvlist_lookup_uint64_array(vallist
,
1664 if (redact_snaps_equal(redact_snap_guids
,
1665 num_redact_snaps
, bmarksnaps
, len
)) {
1673 get_redact_complete(nvpair_t
*pair
)
1675 nvlist_t
*bmark
= fnvpair_value_nvlist(pair
);
1676 nvlist_t
*vallist
= fnvlist_lookup_nvlist(bmark
, "redact_complete");
1677 boolean_t complete
= fnvlist_lookup_boolean_value(vallist
,
1684 * Check that the list of redaction snapshots in the bookmark matches the send
1685 * we're resuming, and return whether or not it's complete.
1687 * Note that the caller needs to free the contents of *bookname with free() if
1688 * this function returns successfully.
1691 find_redact_book(libzfs_handle_t
*hdl
, const char *path
,
1692 const uint64_t *redact_snap_guids
, int num_redact_snaps
,
1695 char errbuf
[ERRBUFLEN
];
1698 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1699 "cannot resume send"));
1701 int error
= get_bookmarks(path
, &bmarks
);
1703 if (error
== ESRCH
) {
1704 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1705 "nonexistent redaction bookmark provided"));
1706 } else if (error
== ENOENT
) {
1707 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1708 "dataset to be sent no longer exists"));
1710 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1711 "unknown error: %s"), strerror(error
));
1713 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1715 nvpair_t
*pair
= find_redact_pair(bmarks
, redact_snap_guids
,
1718 fnvlist_free(bmarks
);
1719 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1720 "no appropriate redaction bookmark exists"));
1721 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1723 boolean_t complete
= get_redact_complete(pair
);
1725 fnvlist_free(bmarks
);
1726 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1727 "incomplete redaction bookmark provided"));
1728 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
1730 *bookname
= strndup(nvpair_name(pair
), ZFS_MAX_DATASET_NAME_LEN
);
1731 ASSERT3P(*bookname
, !=, NULL
);
1732 fnvlist_free(bmarks
);
1736 static enum lzc_send_flags
1737 lzc_flags_from_resume_nvl(nvlist_t
*resume_nvl
)
1739 enum lzc_send_flags lzc_flags
= 0;
1741 if (nvlist_exists(resume_nvl
, "largeblockok"))
1742 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1743 if (nvlist_exists(resume_nvl
, "embedok"))
1744 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1745 if (nvlist_exists(resume_nvl
, "compressok"))
1746 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
1747 if (nvlist_exists(resume_nvl
, "rawok"))
1748 lzc_flags
|= LZC_SEND_FLAG_RAW
;
1749 if (nvlist_exists(resume_nvl
, "savedok"))
1750 lzc_flags
|= LZC_SEND_FLAG_SAVED
;
1756 zfs_send_resume_impl_cb_impl(libzfs_handle_t
*hdl
, sendflags_t
*flags
,
1757 int outfd
, nvlist_t
*resume_nvl
)
1759 char errbuf
[ERRBUFLEN
];
1761 char *fromname
= NULL
;
1762 uint64_t resumeobj
, resumeoff
, toguid
, fromguid
, bytes
;
1765 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1766 FILE *fout
= (flags
->verbosity
> 0 && flags
->dryrun
) ? stdout
: stderr
;
1767 uint64_t *redact_snap_guids
= NULL
;
1768 int num_redact_snaps
= 0;
1769 char *redact_book
= NULL
;
1772 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1773 "cannot resume send"));
1775 if (flags
->verbosity
!= 0) {
1776 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1777 "resume token contents:\n"));
1778 nvlist_print(fout
, resume_nvl
);
1781 if (nvlist_lookup_string(resume_nvl
, "toname", &toname
) != 0 ||
1782 nvlist_lookup_uint64(resume_nvl
, "object", &resumeobj
) != 0 ||
1783 nvlist_lookup_uint64(resume_nvl
, "offset", &resumeoff
) != 0 ||
1784 nvlist_lookup_uint64(resume_nvl
, "bytes", &bytes
) != 0 ||
1785 nvlist_lookup_uint64(resume_nvl
, "toguid", &toguid
) != 0) {
1786 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1787 "resume token is corrupt"));
1788 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1791 (void) nvlist_lookup_uint64(resume_nvl
, "fromguid", &fromguid
);
1794 (void) strlcpy(name
, toname
, sizeof (name
));
1796 error
= guid_to_name(hdl
, toname
, toguid
, B_FALSE
, name
);
1798 if (zfs_dataset_exists(hdl
, toname
, ZFS_TYPE_DATASET
)) {
1799 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1800 "'%s' is no longer the same snapshot "
1801 "used in the initial send"), toname
);
1803 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1804 "'%s' used in the initial send no "
1805 "longer exists"), toname
);
1807 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1811 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1813 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1814 "unable to access '%s'"), name
);
1815 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1818 if (nvlist_lookup_uint64_array(resume_nvl
, "book_redact_snaps",
1819 &redact_snap_guids
, (uint_t
*)&num_redact_snaps
) != 0) {
1820 num_redact_snaps
= -1;
1823 if (fromguid
!= 0) {
1824 if (guid_to_name_redact_snaps(hdl
, toname
, fromguid
, B_TRUE
,
1825 redact_snap_guids
, num_redact_snaps
, name
) != 0) {
1826 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1827 "incremental source %#llx no longer exists"),
1828 (longlong_t
)fromguid
);
1829 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1834 redact_snap_guids
= NULL
;
1836 if (nvlist_lookup_uint64_array(resume_nvl
,
1837 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
), &redact_snap_guids
,
1838 (uint_t
*)&num_redact_snaps
) == 0) {
1839 char path
[ZFS_MAX_DATASET_NAME_LEN
];
1841 (void) strlcpy(path
, toname
, sizeof (path
));
1842 char *at
= strchr(path
, '@');
1843 ASSERT3P(at
, !=, NULL
);
1847 if ((error
= find_redact_book(hdl
, path
, redact_snap_guids
,
1848 num_redact_snaps
, &redact_book
)) != 0) {
1853 enum lzc_send_flags lzc_flags
= lzc_flags_from_sendflags(flags
) |
1854 lzc_flags_from_resume_nvl(resume_nvl
);
1856 if (flags
->verbosity
!= 0 || flags
->progressastitle
) {
1858 * Some of these may have come from the resume token, set them
1859 * here for size estimate purposes.
1861 sendflags_t tmpflags
= *flags
;
1862 if (lzc_flags
& LZC_SEND_FLAG_LARGE_BLOCK
)
1863 tmpflags
.largeblock
= B_TRUE
;
1864 if (lzc_flags
& LZC_SEND_FLAG_COMPRESS
)
1865 tmpflags
.compress
= B_TRUE
;
1866 if (lzc_flags
& LZC_SEND_FLAG_EMBED_DATA
)
1867 tmpflags
.embed_data
= B_TRUE
;
1868 if (lzc_flags
& LZC_SEND_FLAG_RAW
)
1869 tmpflags
.raw
= B_TRUE
;
1870 if (lzc_flags
& LZC_SEND_FLAG_SAVED
)
1871 tmpflags
.saved
= B_TRUE
;
1872 error
= estimate_size(zhp
, fromname
, outfd
, &tmpflags
,
1873 resumeobj
, resumeoff
, bytes
, redact_book
, errbuf
, &size
);
1876 if (!flags
->dryrun
) {
1877 progress_arg_t pa
= { 0 };
1880 * If progress reporting is requested, spawn a new thread to
1881 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1883 if (flags
->progress
|| flags
->progressastitle
) {
1886 pa
.pa_parsable
= flags
->parsable
;
1887 pa
.pa_estimate
= B_FALSE
;
1888 pa
.pa_verbosity
= flags
->verbosity
;
1890 pa
.pa_astitle
= flags
->progressastitle
;
1891 pa
.pa_progress
= flags
->progress
;
1893 error
= pthread_create(&tid
, NULL
,
1894 send_progress_thread
, &pa
);
1896 if (redact_book
!= NULL
)
1903 error
= lzc_send_resume_redacted(zhp
->zfs_name
, fromname
, outfd
,
1904 lzc_flags
, resumeobj
, resumeoff
, redact_book
);
1905 if (redact_book
!= NULL
)
1908 if ((flags
->progressastitle
|| flags
->progress
) &&
1909 send_progress_thread_exit(hdl
, tid
)) {
1914 char errbuf
[ERRBUFLEN
];
1915 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1916 "warning: cannot send '%s'"), zhp
->zfs_name
);
1924 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1925 "source key must be loaded"));
1926 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
1928 if (lzc_exists(zhp
->zfs_name
)) {
1929 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1930 "incremental source could not be found"));
1932 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1947 zfs_error_aux(hdl
, "%s", strerror(errno
));
1948 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1951 return (zfs_standard_error(hdl
, errno
, errbuf
));
1954 if (redact_book
!= NULL
)
1963 struct zfs_send_resume_impl
{
1964 libzfs_handle_t
*hdl
;
1966 nvlist_t
*resume_nvl
;
1970 zfs_send_resume_impl_cb(int outfd
, void *arg
)
1972 struct zfs_send_resume_impl
*zsri
= arg
;
1973 return (zfs_send_resume_impl_cb_impl(zsri
->hdl
, zsri
->flags
, outfd
,
1978 zfs_send_resume_impl(libzfs_handle_t
*hdl
, sendflags_t
*flags
, int outfd
,
1979 nvlist_t
*resume_nvl
)
1981 struct zfs_send_resume_impl zsri
= {
1984 .resume_nvl
= resume_nvl
,
1986 return (lzc_send_wrapper(zfs_send_resume_impl_cb
, outfd
, &zsri
));
1990 zfs_send_resume(libzfs_handle_t
*hdl
, sendflags_t
*flags
, int outfd
,
1991 const char *resume_token
)
1994 char errbuf
[ERRBUFLEN
];
1995 nvlist_t
*resume_nvl
;
1997 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1998 "cannot resume send"));
2000 resume_nvl
= zfs_send_resume_token_to_nvlist(hdl
, resume_token
);
2001 if (resume_nvl
== NULL
) {
2003 * zfs_error_aux has already been set by
2004 * zfs_send_resume_token_to_nvlist()
2006 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
2009 ret
= zfs_send_resume_impl(hdl
, flags
, outfd
, resume_nvl
);
2010 fnvlist_free(resume_nvl
);
2016 zfs_send_saved(zfs_handle_t
*zhp
, sendflags_t
*flags
, int outfd
,
2017 const char *resume_token
)
2020 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2021 nvlist_t
*saved_nvl
= NULL
, *resume_nvl
= NULL
;
2022 uint64_t saved_guid
= 0, resume_guid
= 0;
2023 uint64_t obj
= 0, off
= 0, bytes
= 0;
2024 char token_buf
[ZFS_MAXPROPLEN
];
2025 char errbuf
[ERRBUFLEN
];
2027 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2028 "saved send failed"));
2030 ret
= zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
2031 token_buf
, sizeof (token_buf
), NULL
, NULL
, 0, B_TRUE
);
2035 saved_nvl
= zfs_send_resume_token_to_nvlist(hdl
, token_buf
);
2036 if (saved_nvl
== NULL
) {
2038 * zfs_error_aux has already been set by
2039 * zfs_send_resume_token_to_nvlist()
2041 ret
= zfs_error(hdl
, EZFS_FAULT
, errbuf
);
2046 * If a resume token is provided we use the object and offset
2047 * from that instead of the default, which starts from the
2050 if (resume_token
!= NULL
) {
2051 resume_nvl
= zfs_send_resume_token_to_nvlist(hdl
,
2053 if (resume_nvl
== NULL
) {
2054 ret
= zfs_error(hdl
, EZFS_FAULT
, errbuf
);
2058 if (nvlist_lookup_uint64(resume_nvl
, "object", &obj
) != 0 ||
2059 nvlist_lookup_uint64(resume_nvl
, "offset", &off
) != 0 ||
2060 nvlist_lookup_uint64(resume_nvl
, "bytes", &bytes
) != 0 ||
2061 nvlist_lookup_uint64(resume_nvl
, "toguid",
2062 &resume_guid
) != 0) {
2063 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2064 "provided resume token is corrupt"));
2065 ret
= zfs_error(hdl
, EZFS_FAULT
, errbuf
);
2069 if (nvlist_lookup_uint64(saved_nvl
, "toguid",
2071 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2072 "dataset's resume token is corrupt"));
2073 ret
= zfs_error(hdl
, EZFS_FAULT
, errbuf
);
2077 if (resume_guid
!= saved_guid
) {
2078 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2079 "provided resume token does not match dataset"));
2080 ret
= zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
);
2085 (void) nvlist_remove_all(saved_nvl
, "object");
2086 fnvlist_add_uint64(saved_nvl
, "object", obj
);
2088 (void) nvlist_remove_all(saved_nvl
, "offset");
2089 fnvlist_add_uint64(saved_nvl
, "offset", off
);
2091 (void) nvlist_remove_all(saved_nvl
, "bytes");
2092 fnvlist_add_uint64(saved_nvl
, "bytes", bytes
);
2094 (void) nvlist_remove_all(saved_nvl
, "toname");
2095 fnvlist_add_string(saved_nvl
, "toname", zhp
->zfs_name
);
2097 ret
= zfs_send_resume_impl(hdl
, flags
, outfd
, saved_nvl
);
2100 fnvlist_free(saved_nvl
);
2101 fnvlist_free(resume_nvl
);
2106 * This function informs the target system that the recursive send is complete.
2107 * The record is also expected in the case of a send -p.
2110 send_conclusion_record(int fd
, zio_cksum_t
*zc
)
2112 dmu_replay_record_t drr
= { 0 };
2113 drr
.drr_type
= DRR_END
;
2115 drr
.drr_u
.drr_end
.drr_checksum
= *zc
;
2116 if (write(fd
, &drr
, sizeof (drr
)) == -1) {
2123 * This function is responsible for sending the records that contain the
2124 * necessary information for the target system's libzfs to be able to set the
2125 * properties of the filesystem being received, or to be able to prepare for
2126 * a recursive receive.
2128 * The "zhp" argument is the handle of the snapshot we are sending
2129 * (the "tosnap"). The "from" argument is the short snapshot name (the part
2130 * after the @) of the incremental source.
2133 send_prelim_records(zfs_handle_t
*zhp
, const char *from
, int fd
,
2134 boolean_t gather_props
, boolean_t recursive
, boolean_t verbose
,
2135 boolean_t dryrun
, boolean_t raw
, boolean_t replicate
, boolean_t skipmissing
,
2136 boolean_t backup
, boolean_t holds
, boolean_t props
, boolean_t doall
,
2137 nvlist_t
**fssp
, avl_tree_t
**fsavlp
)
2140 char *packbuf
= NULL
;
2142 zio_cksum_t zc
= { {0} };
2143 int featureflags
= 0;
2144 /* name of filesystem/volume that contains snapshot we are sending */
2145 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
2146 /* short name of snap we are sending */
2147 const char *tosnap
= "";
2149 char errbuf
[ERRBUFLEN
];
2150 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2151 "warning: cannot send '%s'"), zhp
->zfs_name
);
2152 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&& zfs_prop_get_int(zhp
,
2153 ZFS_PROP_VERSION
) >= ZPL_VERSION_SA
) {
2154 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
2158 featureflags
|= DMU_BACKUP_FEATURE_HOLDS
;
2160 (void) strlcpy(tofs
, zhp
->zfs_name
, ZFS_MAX_DATASET_NAME_LEN
);
2161 char *at
= strchr(tofs
, '@');
2168 nvlist_t
*hdrnv
= fnvlist_alloc();
2169 nvlist_t
*fss
= NULL
;
2172 fnvlist_add_string(hdrnv
, "fromsnap", from
);
2173 fnvlist_add_string(hdrnv
, "tosnap", tosnap
);
2175 fnvlist_add_boolean(hdrnv
, "not_recursive");
2178 fnvlist_add_boolean(hdrnv
, "raw");
2181 if (gather_nvlist(zhp
->zfs_hdl
, tofs
,
2182 from
, tosnap
, recursive
, raw
, doall
, replicate
, skipmissing
,
2183 verbose
, backup
, holds
, props
, &fss
, fsavlp
) != 0) {
2184 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADBACKUP
,
2188 * Do not allow the size of the properties list to exceed
2191 if ((fnvlist_size(fss
) + fnvlist_size(hdrnv
)) >
2192 zhp
->zfs_hdl
->libzfs_max_nvlist
) {
2193 (void) snprintf(errbuf
, sizeof (errbuf
),
2194 dgettext(TEXT_DOMAIN
, "warning: cannot send '%s': "
2195 "the size of the list of snapshots and properties "
2196 "is too large to be received successfully.\n"
2197 "Select a smaller number of snapshots to send.\n"),
2199 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOSPC
,
2202 fnvlist_add_nvlist(hdrnv
, "fss", fss
);
2203 VERIFY0(nvlist_pack(hdrnv
, &packbuf
, &buflen
, NV_ENCODE_XDR
,
2210 fnvlist_free(hdrnv
);
2214 dmu_replay_record_t drr
= { 0 };
2215 /* write first begin record */
2216 drr
.drr_type
= DRR_BEGIN
;
2217 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
2218 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
2219 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
2220 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
2221 drr_versioninfo
, featureflags
);
2222 if (snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
2223 sizeof (drr
.drr_u
.drr_begin
.drr_toname
), "%s@%s", tofs
,
2224 tosnap
) >= sizeof (drr
.drr_u
.drr_begin
.drr_toname
)) {
2225 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADBACKUP
,
2228 drr
.drr_payloadlen
= buflen
;
2230 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, fd
);
2233 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(err
));
2234 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADBACKUP
,
2237 err
= send_conclusion_record(fd
, &zc
);
2239 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(err
));
2240 return (zfs_error(zhp
->zfs_hdl
, EZFS_BADBACKUP
,
2248 * Generate a send stream. The "zhp" argument is the filesystem/volume
2249 * that contains the snapshot to send. The "fromsnap" argument is the
2250 * short name (the part after the '@') of the snapshot that is the
2251 * incremental source to send from (if non-NULL). The "tosnap" argument
2252 * is the short name of the snapshot to send.
2254 * The content of the send stream is the snapshot identified by
2255 * 'tosnap'. Incremental streams are requested in two ways:
2256 * - from the snapshot identified by "fromsnap" (if non-null) or
2257 * - from the origin of the dataset identified by zhp, which must
2258 * be a clone. In this case, "fromsnap" is null and "fromorigin"
2261 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
2262 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
2263 * if "replicate" is set. If "doall" is set, dump all the intermediate
2264 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
2265 * case too. If "props" is set, send properties.
2267 * Pre-wrapped (cf. lzc_send_wrapper()).
2270 zfs_send_cb_impl(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
2271 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
2272 void *cb_arg
, nvlist_t
**debugnvp
)
2274 char errbuf
[ERRBUFLEN
];
2275 send_dump_data_t sdd
= { 0 };
2277 nvlist_t
*fss
= NULL
;
2278 avl_tree_t
*fsavl
= NULL
;
2279 static uint64_t holdseq
;
2283 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2284 "cannot send '%s'"), zhp
->zfs_name
);
2286 if (fromsnap
&& fromsnap
[0] == '\0') {
2287 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
2288 "zero-length incremental source"));
2289 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
2293 char full_fromsnap_name
[ZFS_MAX_DATASET_NAME_LEN
];
2294 if (snprintf(full_fromsnap_name
, sizeof (full_fromsnap_name
),
2295 "%s@%s", zhp
->zfs_name
, fromsnap
) >=
2296 sizeof (full_fromsnap_name
)) {
2300 zfs_handle_t
*fromsnapn
= zfs_open(zhp
->zfs_hdl
,
2301 full_fromsnap_name
, ZFS_TYPE_SNAPSHOT
);
2302 if (fromsnapn
== NULL
) {
2306 zfs_close(fromsnapn
);
2309 if (flags
->replicate
|| flags
->doall
|| flags
->props
||
2310 flags
->holds
|| flags
->backup
) {
2311 char full_tosnap_name
[ZFS_MAX_DATASET_NAME_LEN
];
2312 if (snprintf(full_tosnap_name
, sizeof (full_tosnap_name
),
2313 "%s@%s", zhp
->zfs_name
, tosnap
) >=
2314 sizeof (full_tosnap_name
)) {
2318 zfs_handle_t
*tosnap
= zfs_open(zhp
->zfs_hdl
,
2319 full_tosnap_name
, ZFS_TYPE_SNAPSHOT
);
2320 if (tosnap
== NULL
) {
2324 err
= send_prelim_records(tosnap
, fromsnap
, outfd
,
2325 flags
->replicate
|| flags
->props
|| flags
->holds
,
2326 flags
->replicate
, flags
->verbosity
> 0, flags
->dryrun
,
2327 flags
->raw
, flags
->replicate
, flags
->skipmissing
,
2328 flags
->backup
, flags
->holds
, flags
->props
, flags
->doall
,
2335 /* dump each stream */
2336 sdd
.fromsnap
= fromsnap
;
2337 sdd
.tosnap
= tosnap
;
2339 sdd
.replicate
= flags
->replicate
;
2340 sdd
.doall
= flags
->doall
;
2341 sdd
.fromorigin
= flags
->fromorigin
;
2344 sdd
.verbosity
= flags
->verbosity
;
2345 sdd
.parsable
= flags
->parsable
;
2346 sdd
.progress
= flags
->progress
;
2347 sdd
.progressastitle
= flags
->progressastitle
;
2348 sdd
.dryrun
= flags
->dryrun
;
2349 sdd
.large_block
= flags
->largeblock
;
2350 sdd
.embed_data
= flags
->embed_data
;
2351 sdd
.compress
= flags
->compress
;
2352 sdd
.raw
= flags
->raw
;
2353 sdd
.holds
= flags
->holds
;
2354 sdd
.filter_cb
= filter_func
;
2355 sdd
.filter_cb_arg
= cb_arg
;
2357 sdd
.debugnv
= *debugnvp
;
2358 if (sdd
.verbosity
!= 0 && sdd
.dryrun
)
2359 sdd
.std_out
= B_TRUE
;
2360 fout
= sdd
.std_out
? stdout
: stderr
;
2363 * Some flags require that we place user holds on the datasets that are
2364 * being sent so they don't get destroyed during the send. We can skip
2365 * this step if the pool is imported read-only since the datasets cannot
2368 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
2369 ZPOOL_PROP_READONLY
, NULL
) &&
2370 zfs_spa_version(zhp
, &spa_version
) == 0 &&
2371 spa_version
>= SPA_VERSION_USERREFS
&&
2372 (flags
->doall
|| flags
->replicate
)) {
2374 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
2375 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
2376 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
| O_CLOEXEC
);
2377 if (sdd
.cleanup_fd
< 0) {
2381 sdd
.snapholds
= fnvlist_alloc();
2383 sdd
.cleanup_fd
= -1;
2384 sdd
.snapholds
= NULL
;
2387 if (flags
->verbosity
!= 0 || sdd
.snapholds
!= NULL
) {
2389 * Do a verbose no-op dry run to get all the verbose output
2390 * or to gather snapshot hold's before generating any data,
2391 * then do a non-verbose real run to generate the streams.
2393 sdd
.dryrun
= B_TRUE
;
2394 err
= dump_filesystems(zhp
, &sdd
);
2399 if (flags
->verbosity
!= 0) {
2400 if (flags
->parsable
) {
2401 (void) fprintf(fout
, "size\t%llu\n",
2402 (longlong_t
)sdd
.size
);
2405 zfs_nicebytes(sdd
.size
, buf
, sizeof (buf
));
2406 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
2407 "total estimated size is %s\n"), buf
);
2411 /* Ensure no snaps found is treated as an error. */
2417 /* Skip the second run if dryrun was requested. */
2421 if (sdd
.snapholds
!= NULL
) {
2422 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
2426 fnvlist_free(sdd
.snapholds
);
2427 sdd
.snapholds
= NULL
;
2430 sdd
.dryrun
= B_FALSE
;
2434 err
= dump_filesystems(zhp
, &sdd
);
2435 fsavl_destroy(fsavl
);
2438 /* Ensure no snaps found is treated as an error. */
2439 if (err
== 0 && !sdd
.seento
)
2442 if (sdd
.cleanup_fd
!= -1) {
2443 VERIFY(0 == close(sdd
.cleanup_fd
));
2444 sdd
.cleanup_fd
= -1;
2447 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
2448 flags
->props
|| flags
->backup
|| flags
->holds
)) {
2450 * write final end record. NB: want to do this even if
2451 * there was some error, because it might not be totally
2454 int err2
= send_conclusion_record(outfd
, NULL
);
2456 return (zfs_standard_error(zhp
->zfs_hdl
, err2
, errbuf
));
2459 return (err
|| sdd
.err
);
2462 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
2464 fsavl_destroy(fsavl
);
2466 fnvlist_free(sdd
.snapholds
);
2468 if (sdd
.cleanup_fd
!= -1)
2469 VERIFY(0 == close(sdd
.cleanup_fd
));
2475 const char *fromsnap
;
2478 snapfilter_cb_t
*filter_func
;
2480 nvlist_t
**debugnvp
;
2484 zfs_send_cb(int outfd
, void *arg
)
2486 struct zfs_send
*zs
= arg
;
2487 return (zfs_send_cb_impl(zs
->zhp
, zs
->fromsnap
, zs
->tosnap
, zs
->flags
,
2488 outfd
, zs
->filter_func
, zs
->cb_arg
, zs
->debugnvp
));
2492 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
2493 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
2494 void *cb_arg
, nvlist_t
**debugnvp
)
2496 struct zfs_send arg
= {
2498 .fromsnap
= fromsnap
,
2501 .filter_func
= filter_func
,
2503 .debugnvp
= debugnvp
,
2505 return (lzc_send_wrapper(zfs_send_cb
, outfd
, &arg
));
2509 static zfs_handle_t
*
2510 name_to_dir_handle(libzfs_handle_t
*hdl
, const char *snapname
)
2512 char dirname
[ZFS_MAX_DATASET_NAME_LEN
];
2513 (void) strlcpy(dirname
, snapname
, ZFS_MAX_DATASET_NAME_LEN
);
2514 char *c
= strchr(dirname
, '@');
2517 return (zfs_open(hdl
, dirname
, ZFS_TYPE_DATASET
));
2521 * Returns B_TRUE if earlier is an earlier snapshot in later's timeline; either
2522 * an earlier snapshot in the same filesystem, or a snapshot before later's
2523 * origin, or it's origin's origin, etc.
2526 snapshot_is_before(zfs_handle_t
*earlier
, zfs_handle_t
*later
)
2529 uint64_t later_txg
=
2530 (later
->zfs_type
== ZFS_TYPE_FILESYSTEM
||
2531 later
->zfs_type
== ZFS_TYPE_VOLUME
?
2532 UINT64_MAX
: zfs_prop_get_int(later
, ZFS_PROP_CREATETXG
));
2533 uint64_t earlier_txg
= zfs_prop_get_int(earlier
, ZFS_PROP_CREATETXG
);
2535 if (earlier_txg
>= later_txg
)
2538 zfs_handle_t
*earlier_dir
= name_to_dir_handle(earlier
->zfs_hdl
,
2540 zfs_handle_t
*later_dir
= name_to_dir_handle(later
->zfs_hdl
,
2543 if (strcmp(earlier_dir
->zfs_name
, later_dir
->zfs_name
) == 0) {
2544 zfs_close(earlier_dir
);
2545 zfs_close(later_dir
);
2549 char clonename
[ZFS_MAX_DATASET_NAME_LEN
];
2550 if (zfs_prop_get(later_dir
, ZFS_PROP_ORIGIN
, clonename
,
2551 ZFS_MAX_DATASET_NAME_LEN
, NULL
, NULL
, 0, B_TRUE
) != 0) {
2552 zfs_close(earlier_dir
);
2553 zfs_close(later_dir
);
2557 zfs_handle_t
*origin
= zfs_open(earlier
->zfs_hdl
, clonename
,
2559 uint64_t origin_txg
= zfs_prop_get_int(origin
, ZFS_PROP_CREATETXG
);
2562 * If "earlier" is exactly the origin, then
2563 * snapshot_is_before(earlier, origin) will return false (because
2564 * they're the same).
2566 if (origin_txg
== earlier_txg
&&
2567 strcmp(origin
->zfs_name
, earlier
->zfs_name
) == 0) {
2568 zfs_close(earlier_dir
);
2569 zfs_close(later_dir
);
2573 zfs_close(earlier_dir
);
2574 zfs_close(later_dir
);
2576 ret
= snapshot_is_before(earlier
, origin
);
2582 * The "zhp" argument is the handle of the dataset to send (typically a
2583 * snapshot). The "from" argument is the full name of the snapshot or
2584 * bookmark that is the incremental source.
2586 * Pre-wrapped (cf. lzc_send_wrapper()).
2589 zfs_send_one_cb_impl(zfs_handle_t
*zhp
, const char *from
, int fd
,
2590 sendflags_t
*flags
, const char *redactbook
)
2593 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
2594 char *name
= zhp
->zfs_name
;
2596 progress_arg_t pa
= { 0 };
2599 char errbuf
[ERRBUFLEN
];
2600 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2601 "warning: cannot send '%s'"), name
);
2603 if (from
!= NULL
&& strchr(from
, '@')) {
2604 zfs_handle_t
*from_zhp
= zfs_open(hdl
, from
,
2606 if (from_zhp
== NULL
)
2608 if (!snapshot_is_before(from_zhp
, zhp
)) {
2609 zfs_close(from_zhp
);
2610 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2611 "not an earlier snapshot from the same fs"));
2612 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
2614 zfs_close(from_zhp
);
2617 if (redactbook
!= NULL
) {
2618 char bookname
[ZFS_MAX_DATASET_NAME_LEN
];
2619 nvlist_t
*redact_snaps
;
2620 zfs_handle_t
*book_zhp
;
2624 pound
= strchr(redactbook
, '#');
2626 redactbook
= pound
+ 1;
2627 at
= strchr(name
, '@');
2629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2630 "cannot do a redacted send to a filesystem"));
2631 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
2633 dsnamelen
= at
- name
;
2634 if (snprintf(bookname
, sizeof (bookname
), "%.*s#%s",
2635 dsnamelen
, name
, redactbook
)
2636 >= sizeof (bookname
)) {
2637 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2638 "invalid bookmark name"));
2639 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2641 book_zhp
= zfs_open(hdl
, bookname
, ZFS_TYPE_BOOKMARK
);
2642 if (book_zhp
== NULL
)
2644 if (nvlist_lookup_nvlist(book_zhp
->zfs_props
,
2645 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
),
2646 &redact_snaps
) != 0 || redact_snaps
== NULL
) {
2647 zfs_close(book_zhp
);
2648 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2649 "not a redaction bookmark"));
2650 return (zfs_error(hdl
, EZFS_BADTYPE
, errbuf
));
2652 zfs_close(book_zhp
);
2656 * Send fs properties
2658 if (flags
->props
|| flags
->holds
|| flags
->backup
) {
2660 * Note: the header generated by send_prelim_records()
2661 * assumes that the incremental source is in the same
2662 * filesystem/volume as the target (which is a requirement
2663 * when doing "zfs send -R"). But that isn't always the
2664 * case here (e.g. send from snap in origin, or send from
2665 * bookmark). We pass from=NULL, which will omit this
2666 * information from the prelim records; it isn't used
2667 * when receiving this type of stream.
2669 err
= send_prelim_records(zhp
, NULL
, fd
, B_TRUE
, B_FALSE
,
2670 flags
->verbosity
> 0, flags
->dryrun
, flags
->raw
,
2671 flags
->replicate
, B_FALSE
, flags
->backup
, flags
->holds
,
2672 flags
->props
, flags
->doall
, NULL
, NULL
);
2678 * Perform size estimate if verbose was specified.
2680 if (flags
->verbosity
!= 0 || flags
->progressastitle
) {
2681 err
= estimate_size(zhp
, from
, fd
, flags
, 0, 0, 0, redactbook
,
2691 * If progress reporting is requested, spawn a new thread to poll
2692 * ZFS_IOC_SEND_PROGRESS at a regular interval.
2694 if (flags
->progress
|| flags
->progressastitle
) {
2697 pa
.pa_parsable
= flags
->parsable
;
2698 pa
.pa_estimate
= B_FALSE
;
2699 pa
.pa_verbosity
= flags
->verbosity
;
2701 pa
.pa_astitle
= flags
->progressastitle
;
2702 pa
.pa_progress
= flags
->progress
;
2704 err
= pthread_create(&ptid
, NULL
,
2705 send_progress_thread
, &pa
);
2707 zfs_error_aux(zhp
->zfs_hdl
, "%s", strerror(errno
));
2708 return (zfs_error(zhp
->zfs_hdl
,
2709 EZFS_THREADCREATEFAILED
, errbuf
));
2713 err
= lzc_send_redacted(name
, from
, fd
,
2714 lzc_flags_from_sendflags(flags
), redactbook
);
2716 if ((flags
->progress
|| flags
->progressastitle
) &&
2717 send_progress_thread_exit(hdl
, ptid
))
2720 if (err
== 0 && (flags
->props
|| flags
->holds
|| flags
->backup
)) {
2721 /* Write the final end record. */
2722 err
= send_conclusion_record(fd
, NULL
);
2724 return (zfs_standard_error(hdl
, err
, errbuf
));
2729 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2730 "not an earlier snapshot from the same fs"));
2731 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
2735 if (lzc_exists(name
)) {
2736 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2737 "incremental source (%s) does not exist"),
2740 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2743 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2744 "dataset key must be loaded"));
2745 return (zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
));
2748 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2749 "target is busy; if a filesystem, "
2750 "it must not be mounted"));
2751 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
2765 zfs_error_aux(hdl
, "%s", strerror(errno
));
2766 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
2769 return (zfs_standard_error(hdl
, errno
, errbuf
));
2775 struct zfs_send_one
{
2779 const char *redactbook
;
2783 zfs_send_one_cb(int fd
, void *arg
)
2785 struct zfs_send_one
*zso
= arg
;
2786 return (zfs_send_one_cb_impl(zso
->zhp
, zso
->from
, fd
, zso
->flags
,
2791 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
, sendflags_t
*flags
,
2792 const char *redactbook
)
2794 struct zfs_send_one zso
= {
2798 .redactbook
= redactbook
,
2800 return (lzc_send_wrapper(zfs_send_one_cb
, fd
, &zso
));
2804 * Routines specific to "zfs recv"
2808 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
2809 boolean_t byteswap
, zio_cksum_t
*zc
)
2816 rv
= read(fd
, cp
, len
);
2821 if (rv
< 0 || len
!= 0) {
2822 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2823 "failed to read from stream"));
2824 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
2825 "cannot receive")));
2830 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
2832 fletcher_4_incremental_native(buf
, ilen
, zc
);
2838 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
2839 boolean_t byteswap
, zio_cksum_t
*zc
)
2844 buf
= zfs_alloc(hdl
, len
);
2846 if (len
> hdl
->libzfs_max_nvlist
) {
2847 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "nvlist too large"));
2852 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
2858 err
= nvlist_unpack(buf
, len
, nvp
, 0);
2861 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2862 "stream (malformed nvlist)"));
2869 * Returns the grand origin (origin of origin of origin...) of a given handle.
2870 * If this dataset is not a clone, it simply returns a copy of the original
2873 static zfs_handle_t
*
2874 recv_open_grand_origin(zfs_handle_t
*zhp
)
2876 char origin
[ZFS_MAX_DATASET_NAME_LEN
];
2878 zfs_handle_t
*ozhp
= zfs_handle_dup(zhp
);
2880 while (ozhp
!= NULL
) {
2881 if (zfs_prop_get(ozhp
, ZFS_PROP_ORIGIN
, origin
,
2882 sizeof (origin
), &src
, NULL
, 0, B_FALSE
) != 0)
2885 (void) zfs_close(ozhp
);
2886 ozhp
= zfs_open(zhp
->zfs_hdl
, origin
, ZFS_TYPE_FILESYSTEM
);
2893 recv_rename_impl(zfs_handle_t
*zhp
, const char *name
, const char *newname
)
2896 zfs_handle_t
*ozhp
= NULL
;
2899 * Attempt to rename the dataset. If it fails with EACCES we have
2900 * attempted to rename the dataset outside of its encryption root.
2901 * Force the dataset to become an encryption root and try again.
2903 err
= lzc_rename(name
, newname
);
2904 if (err
== EACCES
) {
2905 ozhp
= recv_open_grand_origin(zhp
);
2911 err
= lzc_change_key(ozhp
->zfs_name
, DCP_CMD_FORCE_NEW_KEY
,
2916 err
= lzc_rename(name
, newname
);
2926 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
2927 int baselen
, char *newname
, recvflags_t
*flags
)
2931 prop_changelist_t
*clp
= NULL
;
2932 zfs_handle_t
*zhp
= NULL
;
2934 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2939 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2940 flags
->force
? MS_FORCE
: 0);
2945 err
= changelist_prefix(clp
);
2950 (void) strlcpy(newname
, tryname
, ZFS_MAX_DATASET_NAME_LEN
);
2951 if (flags
->verbose
) {
2952 (void) printf("attempting rename %s to %s\n",
2955 err
= recv_rename_impl(zhp
, name
, newname
);
2957 changelist_rename(clp
, name
, tryname
);
2962 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
2965 (void) snprintf(newname
, ZFS_MAX_DATASET_NAME_LEN
,
2966 "%.*srecv-%u-%u", baselen
, name
, getpid(), seq
);
2968 if (flags
->verbose
) {
2969 (void) printf("failed - trying rename %s to %s\n",
2972 err
= recv_rename_impl(zhp
, name
, newname
);
2974 changelist_rename(clp
, name
, newname
);
2975 if (err
&& flags
->verbose
) {
2976 (void) printf("failed (%u) - "
2977 "will try again on next pass\n", errno
);
2980 } else if (flags
->verbose
) {
2982 (void) printf("success\n");
2984 (void) printf("failed (%u)\n", errno
);
2987 (void) changelist_postfix(clp
);
2991 changelist_free(clp
);
2999 recv_promote(libzfs_handle_t
*hdl
, const char *fsname
,
3000 const char *origin_fsname
, recvflags_t
*flags
)
3003 zfs_cmd_t zc
= {"\0"};
3004 zfs_handle_t
*zhp
= NULL
, *ozhp
= NULL
;
3007 (void) printf("promoting %s\n", fsname
);
3009 (void) strlcpy(zc
.zc_value
, origin_fsname
, sizeof (zc
.zc_value
));
3010 (void) strlcpy(zc
.zc_name
, fsname
, sizeof (zc
.zc_name
));
3013 * Attempt to promote the dataset. If it fails with EACCES the
3014 * promotion would cause this dataset to leave its encryption root.
3015 * Force the origin to become an encryption root and try again.
3017 err
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3018 if (err
== EACCES
) {
3019 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_DATASET
);
3025 ozhp
= recv_open_grand_origin(zhp
);
3031 err
= lzc_change_key(ozhp
->zfs_name
, DCP_CMD_FORCE_NEW_KEY
,
3036 err
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
3049 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
3050 char *newname
, recvflags_t
*flags
)
3053 prop_changelist_t
*clp
;
3055 boolean_t defer
= B_FALSE
;
3058 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
3061 zfs_type_t type
= zfs_get_type(zhp
);
3062 if (type
== ZFS_TYPE_SNAPSHOT
&&
3063 zfs_spa_version(zhp
, &spa_version
) == 0 &&
3064 spa_version
>= SPA_VERSION_USERREFS
)
3066 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
3067 flags
->force
? MS_FORCE
: 0);
3072 err
= changelist_prefix(clp
);
3077 (void) printf("attempting destroy %s\n", name
);
3078 if (type
== ZFS_TYPE_SNAPSHOT
) {
3079 nvlist_t
*nv
= fnvlist_alloc();
3080 fnvlist_add_boolean(nv
, name
);
3081 err
= lzc_destroy_snaps(nv
, defer
, NULL
);
3084 err
= lzc_destroy(name
);
3088 (void) printf("success\n");
3089 changelist_remove(clp
, name
);
3092 (void) changelist_postfix(clp
);
3093 changelist_free(clp
);
3096 * Deferred destroy might destroy the snapshot or only mark it to be
3097 * destroyed later, and it returns success in either case.
3099 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
3100 ZFS_TYPE_SNAPSHOT
))) {
3101 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
3107 typedef struct guid_to_name_data
{
3109 boolean_t bookmark_ok
;
3112 uint64_t *redact_snap_guids
;
3113 uint64_t num_redact_snaps
;
3114 } guid_to_name_data_t
;
3117 redact_snaps_match(zfs_handle_t
*zhp
, guid_to_name_data_t
*gtnd
)
3119 uint64_t *bmark_snaps
;
3120 uint_t bmark_num_snaps
;
3122 if (zhp
->zfs_type
!= ZFS_TYPE_BOOKMARK
)
3125 nvl
= fnvlist_lookup_nvlist(zhp
->zfs_props
,
3126 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS
));
3127 bmark_snaps
= fnvlist_lookup_uint64_array(nvl
, ZPROP_VALUE
,
3129 if (bmark_num_snaps
!= gtnd
->num_redact_snaps
)
3132 for (; i
< bmark_num_snaps
; i
++) {
3134 for (; j
< bmark_num_snaps
; j
++) {
3135 if (bmark_snaps
[i
] == gtnd
->redact_snap_guids
[j
])
3138 if (j
== bmark_num_snaps
)
3141 return (i
== bmark_num_snaps
);
3145 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
3147 guid_to_name_data_t
*gtnd
= arg
;
3151 if (gtnd
->skip
!= NULL
&&
3152 (slash
= strrchr(zhp
->zfs_name
, '/')) != NULL
&&
3153 strcmp(slash
+ 1, gtnd
->skip
) == 0) {
3158 if (zfs_prop_get_int(zhp
, ZFS_PROP_GUID
) == gtnd
->guid
&&
3159 (gtnd
->num_redact_snaps
== -1 || redact_snaps_match(zhp
, gtnd
))) {
3160 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
3165 err
= zfs_iter_children(zhp
, 0, guid_to_name_cb
, gtnd
);
3166 if (err
!= EEXIST
&& gtnd
->bookmark_ok
)
3167 err
= zfs_iter_bookmarks(zhp
, 0, guid_to_name_cb
, gtnd
);
3173 * Attempt to find the local dataset associated with this guid. In the case of
3174 * multiple matches, we attempt to find the "best" match by searching
3175 * progressively larger portions of the hierarchy. This allows one to send a
3176 * tree of datasets individually and guarantee that we will find the source
3177 * guid within that hierarchy, even if there are multiple matches elsewhere.
3179 * If num_redact_snaps is not -1, we attempt to find a redaction bookmark with
3180 * the specified number of redaction snapshots. If num_redact_snaps isn't 0 or
3181 * -1, then redact_snap_guids will be an array of the guids of the snapshots the
3182 * redaction bookmark was created with. If num_redact_snaps is -1, then we will
3183 * attempt to find a snapshot or bookmark (if bookmark_ok is passed) with the
3184 * given guid. Note that a redaction bookmark can be returned if
3185 * num_redact_snaps == -1.
3188 guid_to_name_redact_snaps(libzfs_handle_t
*hdl
, const char *parent
,
3189 uint64_t guid
, boolean_t bookmark_ok
, uint64_t *redact_snap_guids
,
3190 uint64_t num_redact_snaps
, char *name
)
3192 char pname
[ZFS_MAX_DATASET_NAME_LEN
];
3193 guid_to_name_data_t gtnd
;
3196 gtnd
.bookmark_ok
= bookmark_ok
;
3199 gtnd
.redact_snap_guids
= redact_snap_guids
;
3200 gtnd
.num_redact_snaps
= num_redact_snaps
;
3203 * Search progressively larger portions of the hierarchy, starting
3204 * with the filesystem specified by 'parent'. This will
3205 * select the "most local" version of the origin snapshot in the case
3206 * that there are multiple matching snapshots in the system.
3208 (void) strlcpy(pname
, parent
, sizeof (pname
));
3209 char *cp
= strrchr(pname
, '@');
3211 cp
= strchr(pname
, '\0');
3212 for (; cp
!= NULL
; cp
= strrchr(pname
, '/')) {
3213 /* Chop off the last component and open the parent */
3215 zfs_handle_t
*zhp
= make_dataset_handle(hdl
, pname
);
3219 int err
= guid_to_name_cb(zfs_handle_dup(zhp
), >nd
);
3221 err
= zfs_iter_children(zhp
, 0, guid_to_name_cb
, >nd
);
3222 if (err
!= EEXIST
&& bookmark_ok
)
3223 err
= zfs_iter_bookmarks(zhp
, 0, guid_to_name_cb
,
3230 * Remember the last portion of the dataset so we skip it next
3231 * time through (as we've already searched that portion of the
3234 gtnd
.skip
= strrchr(pname
, '/') + 1;
3241 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
3242 boolean_t bookmark_ok
, char *name
)
3244 return (guid_to_name_redact_snaps(hdl
, parent
, guid
, bookmark_ok
, NULL
,
3249 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
3250 * guid1 is after guid2.
3253 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
3254 uint64_t guid1
, uint64_t guid2
)
3257 char *fsname
= NULL
, *snapname
= NULL
;
3258 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
3260 zfs_handle_t
*guid1hdl
, *guid2hdl
;
3261 uint64_t create1
, create2
;
3268 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
3269 fsname
= fnvlist_lookup_string(nvfs
, "name");
3270 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
3271 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
3272 if (guid1hdl
== NULL
)
3275 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
3276 fsname
= fnvlist_lookup_string(nvfs
, "name");
3277 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
3278 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
3279 if (guid2hdl
== NULL
) {
3280 zfs_close(guid1hdl
);
3284 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
3285 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
3287 if (create1
< create2
)
3289 else if (create1
> create2
)
3294 zfs_close(guid1hdl
);
3295 zfs_close(guid2hdl
);
3301 * This function reestablishes the hierarchy of encryption roots after a
3302 * recursive incremental receive has completed. This must be done after the
3303 * second call to recv_incremental_replication() has renamed and promoted all
3304 * sent datasets to their final locations in the dataset hierarchy.
3307 recv_fix_encryption_hierarchy(libzfs_handle_t
*hdl
, const char *top_zfs
,
3308 nvlist_t
*stream_nv
)
3311 nvpair_t
*fselem
= NULL
;
3312 nvlist_t
*stream_fss
;
3314 stream_fss
= fnvlist_lookup_nvlist(stream_nv
, "fss");
3316 while ((fselem
= nvlist_next_nvpair(stream_fss
, fselem
)) != NULL
) {
3317 zfs_handle_t
*zhp
= NULL
;
3319 nvlist_t
*snaps
, *props
, *stream_nvfs
= NULL
;
3320 nvpair_t
*snapel
= NULL
;
3321 boolean_t is_encroot
, is_clone
, stream_encroot
;
3323 char *stream_keylocation
= NULL
;
3324 char keylocation
[MAXNAMELEN
];
3325 char fsname
[ZFS_MAX_DATASET_NAME_LEN
];
3327 keylocation
[0] = '\0';
3328 stream_nvfs
= fnvpair_value_nvlist(fselem
);
3329 snaps
= fnvlist_lookup_nvlist(stream_nvfs
, "snaps");
3330 props
= fnvlist_lookup_nvlist(stream_nvfs
, "props");
3331 stream_encroot
= nvlist_exists(stream_nvfs
, "is_encroot");
3333 /* find a snapshot from the stream that exists locally */
3335 while ((snapel
= nvlist_next_nvpair(snaps
, snapel
)) != NULL
) {
3338 guid
= fnvpair_value_uint64(snapel
);
3339 err
= guid_to_name(hdl
, top_zfs
, guid
, B_FALSE
,
3348 cp
= strchr(fsname
, '@');
3352 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_DATASET
);
3358 crypt
= zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
);
3359 is_clone
= zhp
->zfs_dmustats
.dds_origin
[0] != '\0';
3360 (void) zfs_crypto_get_encryption_root(zhp
, &is_encroot
, NULL
);
3362 /* we don't need to do anything for unencrypted datasets */
3363 if (crypt
== ZIO_CRYPT_OFF
) {
3369 * If the dataset is flagged as an encryption root, was not
3370 * received as a clone and is not currently an encryption root,
3371 * force it to become one. Fixup the keylocation if necessary.
3373 if (stream_encroot
) {
3374 if (!is_clone
&& !is_encroot
) {
3375 err
= lzc_change_key(fsname
,
3376 DCP_CMD_FORCE_NEW_KEY
, NULL
, NULL
, 0);
3383 stream_keylocation
= fnvlist_lookup_string(props
,
3384 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
));
3387 * Refresh the properties in case the call to
3388 * lzc_change_key() changed the value.
3390 zfs_refresh_properties(zhp
);
3391 err
= zfs_prop_get(zhp
, ZFS_PROP_KEYLOCATION
,
3392 keylocation
, sizeof (keylocation
), NULL
, NULL
,
3399 if (strcmp(keylocation
, stream_keylocation
) != 0) {
3400 err
= zfs_prop_set(zhp
,
3401 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
),
3402 stream_keylocation
);
3411 * If the dataset is not flagged as an encryption root and is
3412 * currently an encryption root, force it to inherit from its
3413 * parent. The root of a raw send should never be
3416 if (!stream_encroot
&& is_encroot
&&
3417 strcmp(top_zfs
, fsname
) != 0) {
3418 err
= lzc_change_key(fsname
, DCP_CMD_FORCE_INHERIT
,
3436 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
3437 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
3440 nvlist_t
*local_nv
, *deleted
= NULL
;
3441 avl_tree_t
*local_avl
;
3442 nvpair_t
*fselem
, *nextfselem
;
3444 char newname
[ZFS_MAX_DATASET_NAME_LEN
];
3447 boolean_t needagain
, progress
, recursive
;
3450 fromsnap
= fnvlist_lookup_string(stream_nv
, "fromsnap");
3452 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
3459 needagain
= progress
= B_FALSE
;
3461 deleted
= fnvlist_alloc();
3463 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
3464 recursive
, B_TRUE
, B_FALSE
, recursive
, B_FALSE
, B_FALSE
, B_FALSE
,
3465 B_FALSE
, B_TRUE
, &local_nv
, &local_avl
)) != 0)
3469 * Process deletes and renames
3471 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
3472 fselem
; fselem
= nextfselem
) {
3473 nvlist_t
*nvfs
, *snaps
;
3474 nvlist_t
*stream_nvfs
= NULL
;
3475 nvpair_t
*snapelem
, *nextsnapelem
;
3476 uint64_t fromguid
= 0;
3477 uint64_t originguid
= 0;
3478 uint64_t stream_originguid
= 0;
3479 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
3480 char *fsname
, *stream_fsname
;
3482 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
3484 nvfs
= fnvpair_value_nvlist(fselem
);
3485 snaps
= fnvlist_lookup_nvlist(nvfs
, "snaps");
3486 fsname
= fnvlist_lookup_string(nvfs
, "name");
3487 parent_fromsnap_guid
= fnvlist_lookup_uint64(nvfs
,
3489 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
3492 * First find the stream's fs, so we can check for
3493 * a different origin (due to "zfs promote")
3495 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
3496 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
3499 thisguid
= fnvpair_value_uint64(snapelem
);
3500 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
3502 if (stream_nvfs
!= NULL
)
3506 /* check for promote */
3507 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
3508 &stream_originguid
);
3509 if (stream_nvfs
&& originguid
!= stream_originguid
) {
3510 switch (created_before(hdl
, local_avl
,
3511 stream_originguid
, originguid
)) {
3514 nvlist_t
*origin_nvfs
;
3515 char *origin_fsname
;
3517 origin_nvfs
= fsavl_find(local_avl
, originguid
,
3519 origin_fsname
= fnvlist_lookup_string(
3520 origin_nvfs
, "name");
3521 error
= recv_promote(hdl
, fsname
, origin_fsname
,
3530 fsavl_destroy(local_avl
);
3531 fnvlist_free(local_nv
);
3535 * We had/have the wrong origin, therefore our
3536 * list of snapshots is wrong. Need to handle
3537 * them on the next pass.
3543 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
3544 snapelem
; snapelem
= nextsnapelem
) {
3546 char *stream_snapname
;
3547 nvlist_t
*found
, *props
;
3549 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
3551 thisguid
= fnvpair_value_uint64(snapelem
);
3552 found
= fsavl_find(stream_avl
, thisguid
,
3555 /* check for delete */
3556 if (found
== NULL
) {
3557 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3562 (void) snprintf(name
, sizeof (name
), "%s@%s",
3563 fsname
, nvpair_name(snapelem
));
3565 error
= recv_destroy(hdl
, name
,
3566 strlen(fsname
)+1, newname
, flags
);
3571 sprintf(guidname
, "%llu",
3572 (u_longlong_t
)thisguid
);
3573 nvlist_add_boolean(deleted
, guidname
);
3577 stream_nvfs
= found
;
3579 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
3580 &props
) && 0 == nvlist_lookup_nvlist(props
,
3581 stream_snapname
, &props
)) {
3582 zfs_cmd_t zc
= {"\0"};
3584 zc
.zc_cookie
= B_TRUE
; /* received */
3585 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
3586 "%s@%s", fsname
, nvpair_name(snapelem
));
3587 zcmd_write_src_nvlist(hdl
, &zc
, props
);
3588 (void) zfs_ioctl(hdl
,
3589 ZFS_IOC_SET_PROP
, &zc
);
3590 zcmd_free_nvlists(&zc
);
3593 /* check for different snapname */
3594 if (strcmp(nvpair_name(snapelem
),
3595 stream_snapname
) != 0) {
3596 char name
[ZFS_MAX_DATASET_NAME_LEN
];
3597 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
3599 (void) snprintf(name
, sizeof (name
), "%s@%s",
3600 fsname
, nvpair_name(snapelem
));
3601 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
3602 fsname
, stream_snapname
);
3604 error
= recv_rename(hdl
, name
, tryname
,
3605 strlen(fsname
)+1, newname
, flags
);
3612 if (strcmp(stream_snapname
, fromsnap
) == 0)
3613 fromguid
= thisguid
;
3616 /* check for delete */
3617 if (stream_nvfs
== NULL
) {
3621 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
3627 sprintf(guidname
, "%llu",
3628 (u_longlong_t
)parent_fromsnap_guid
);
3629 nvlist_add_boolean(deleted
, guidname
);
3633 if (fromguid
== 0) {
3634 if (flags
->verbose
) {
3635 (void) printf("local fs %s does not have "
3636 "fromsnap (%s in stream); must have "
3637 "been deleted locally; ignoring\n",
3643 stream_fsname
= fnvlist_lookup_string(stream_nvfs
, "name");
3644 stream_parent_fromsnap_guid
= fnvlist_lookup_uint64(
3645 stream_nvfs
, "parentfromsnap");
3647 s1
= strrchr(fsname
, '/');
3648 s2
= strrchr(stream_fsname
, '/');
3651 * Check if we're going to rename based on parent guid change
3652 * and the current parent guid was also deleted. If it was then
3653 * rename will fail and is likely unneeded, so avoid this and
3654 * force an early retry to determine the new
3655 * parent_fromsnap_guid.
3657 if (stream_parent_fromsnap_guid
!= 0 &&
3658 parent_fromsnap_guid
!= 0 &&
3659 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
3660 sprintf(guidname
, "%llu",
3661 (u_longlong_t
)parent_fromsnap_guid
);
3662 if (nvlist_exists(deleted
, guidname
)) {
3670 * Check for rename. If the exact receive path is specified, it
3671 * does not count as a rename, but we still need to check the
3672 * datasets beneath it.
3674 if ((stream_parent_fromsnap_guid
!= 0 &&
3675 parent_fromsnap_guid
!= 0 &&
3676 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
3677 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
3678 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
3680 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
3682 parent
= fsavl_find(local_avl
,
3683 stream_parent_fromsnap_guid
, NULL
);
3685 * NB: parent might not be found if we used the
3686 * tosnap for stream_parent_fromsnap_guid,
3687 * because the parent is a newly-created fs;
3688 * we'll be able to rename it after we recv the
3691 if (parent
!= NULL
) {
3694 pname
= fnvlist_lookup_string(parent
, "name");
3695 (void) snprintf(tryname
, sizeof (tryname
),
3696 "%s%s", pname
, strrchr(stream_fsname
, '/'));
3699 if (flags
->verbose
) {
3700 (void) printf("local fs %s new parent "
3701 "not found\n", fsname
);
3707 error
= recv_rename(hdl
, fsname
, tryname
,
3708 strlen(tofs
)+1, newname
, flags
);
3710 if (renamed
!= NULL
&& newname
[0] != '\0') {
3711 fnvlist_add_boolean(renamed
, newname
);
3722 fsavl_destroy(local_avl
);
3723 fnvlist_free(local_nv
);
3724 fnvlist_free(deleted
);
3726 if (needagain
&& progress
) {
3727 /* do another pass to fix up temporary names */
3729 (void) printf("another pass:\n");
3733 return (needagain
|| error
!= 0);
3737 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
3738 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
3739 char **top_zfs
, nvlist_t
*cmdprops
)
3741 nvlist_t
*stream_nv
= NULL
;
3742 avl_tree_t
*stream_avl
= NULL
;
3743 char *fromsnap
= NULL
;
3744 char *sendsnap
= NULL
;
3746 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
3747 char sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
3748 char errbuf
[ERRBUFLEN
];
3749 dmu_replay_record_t drre
;
3751 boolean_t anyerr
= B_FALSE
;
3752 boolean_t softerr
= B_FALSE
;
3753 boolean_t recursive
, raw
;
3755 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3758 assert(drr
->drr_type
== DRR_BEGIN
);
3759 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
3760 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
3761 DMU_COMPOUNDSTREAM
);
3764 * Read in the nvlist from the stream.
3766 if (drr
->drr_payloadlen
!= 0) {
3767 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
3768 &stream_nv
, flags
->byteswap
, zc
);
3770 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3775 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
3777 raw
= (nvlist_lookup_boolean(stream_nv
, "raw") == 0);
3779 if (recursive
&& strchr(destname
, '@')) {
3780 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3781 "cannot specify snapshot name for multi-snapshot stream"));
3782 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3787 * Read in the end record and verify checksum.
3789 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
3790 flags
->byteswap
, NULL
)))
3792 if (flags
->byteswap
) {
3793 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
3794 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
3795 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
3796 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
3797 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
3798 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
3799 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
3800 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
3801 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
3803 if (drre
.drr_type
!= DRR_END
) {
3804 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3807 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
3808 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3809 "incorrect header checksum"));
3810 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3814 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
3816 if (drr
->drr_payloadlen
!= 0) {
3817 nvlist_t
*stream_fss
;
3819 stream_fss
= fnvlist_lookup_nvlist(stream_nv
, "fss");
3820 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
3821 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3822 "couldn't allocate avl tree"));
3823 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
3827 if (fromsnap
!= NULL
&& recursive
) {
3828 nvlist_t
*renamed
= NULL
;
3829 nvpair_t
*pair
= NULL
;
3831 (void) strlcpy(tofs
, destname
, sizeof (tofs
));
3832 if (flags
->isprefix
) {
3833 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
3836 if (flags
->istail
) {
3837 cp
= strrchr(drrb
->drr_toname
, '/');
3839 (void) strlcat(tofs
, "/",
3843 i
= (cp
- drrb
->drr_toname
);
3846 i
= strcspn(drrb
->drr_toname
, "/@");
3848 /* zfs_receive_one() will create_parents() */
3849 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
3851 *strchr(tofs
, '@') = '\0';
3854 if (!flags
->dryrun
&& !flags
->nomount
) {
3855 renamed
= fnvlist_alloc();
3858 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
3859 stream_nv
, stream_avl
, renamed
);
3861 /* Unmount renamed filesystems before receiving. */
3862 while ((pair
= nvlist_next_nvpair(renamed
,
3865 prop_changelist_t
*clp
= NULL
;
3867 zhp
= zfs_open(hdl
, nvpair_name(pair
),
3868 ZFS_TYPE_FILESYSTEM
);
3870 clp
= changelist_gather(zhp
,
3871 ZFS_PROP_MOUNTPOINT
, 0,
3872 flags
->forceunmount
? MS_FORCE
: 0);
3876 changelist_prefix(clp
);
3877 changelist_free(clp
);
3882 fnvlist_free(renamed
);
3887 * Get the fs specified by the first path in the stream (the top level
3888 * specified by 'zfs send') and pass it to each invocation of
3889 * zfs_receive_one().
3891 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
3893 if ((cp
= strchr(sendfs
, '@')) != NULL
) {
3896 * Find the "sendsnap", the final snapshot in a replication
3897 * stream. zfs_receive_one() handles certain errors
3898 * differently, depending on if the contained stream is the
3901 sendsnap
= (cp
+ 1);
3904 /* Finally, receive each contained stream */
3907 * we should figure out if it has a recoverable
3908 * error, in which case do a recv_skip() and drive on.
3909 * Note, if we fail due to already having this guid,
3910 * zfs_receive_one() will take care of it (ie,
3911 * recv_skip() and return 0).
3913 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
3914 sendfs
, stream_nv
, stream_avl
, top_zfs
, sendsnap
, cmdprops
);
3915 if (error
== ENODATA
) {
3920 } while (error
== 0);
3922 if (drr
->drr_payloadlen
!= 0 && recursive
&& fromsnap
!= NULL
) {
3924 * Now that we have the fs's they sent us, try the
3927 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
3928 stream_nv
, stream_avl
, NULL
);
3931 if (raw
&& softerr
== 0 && *top_zfs
!= NULL
) {
3932 softerr
= recv_fix_encryption_hierarchy(hdl
, *top_zfs
,
3937 fsavl_destroy(stream_avl
);
3938 fnvlist_free(stream_nv
);
3947 trunc_prop_errs(int truncated
)
3949 ASSERT(truncated
!= 0);
3952 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
3953 "1 more property could not be set\n"));
3955 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
3956 "%d more properties could not be set\n"), truncated
);
3960 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
3962 dmu_replay_record_t
*drr
;
3963 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
3964 uint64_t payload_size
;
3965 char errbuf
[ERRBUFLEN
];
3967 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3970 /* XXX would be great to use lseek if possible... */
3973 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
3974 byteswap
, NULL
) == 0) {
3976 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
3978 switch (drr
->drr_type
) {
3980 if (drr
->drr_payloadlen
!= 0) {
3981 (void) recv_read(hdl
, fd
, buf
,
3982 drr
->drr_payloadlen
, B_FALSE
, NULL
);
3992 drr
->drr_u
.drr_object
.drr_bonuslen
=
3993 BSWAP_32(drr
->drr_u
.drr_object
.
3995 drr
->drr_u
.drr_object
.drr_raw_bonuslen
=
3996 BSWAP_32(drr
->drr_u
.drr_object
.
4001 DRR_OBJECT_PAYLOAD_SIZE(&drr
->drr_u
.drr_object
);
4002 (void) recv_read(hdl
, fd
, buf
, payload_size
,
4008 drr
->drr_u
.drr_write
.drr_logical_size
=
4010 drr
->drr_u
.drr_write
.drr_logical_size
);
4011 drr
->drr_u
.drr_write
.drr_compressed_size
=
4013 drr
->drr_u
.drr_write
.drr_compressed_size
);
4016 DRR_WRITE_PAYLOAD_SIZE(&drr
->drr_u
.drr_write
);
4017 assert(payload_size
<= SPA_MAXBLOCKSIZE
);
4018 (void) recv_read(hdl
, fd
, buf
,
4019 payload_size
, B_FALSE
, NULL
);
4023 drr
->drr_u
.drr_spill
.drr_length
=
4024 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
4025 drr
->drr_u
.drr_spill
.drr_compressed_size
=
4026 BSWAP_64(drr
->drr_u
.drr_spill
.
4027 drr_compressed_size
);
4031 DRR_SPILL_PAYLOAD_SIZE(&drr
->drr_u
.drr_spill
);
4032 (void) recv_read(hdl
, fd
, buf
, payload_size
,
4035 case DRR_WRITE_EMBEDDED
:
4037 drr
->drr_u
.drr_write_embedded
.drr_psize
=
4038 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
4041 (void) recv_read(hdl
, fd
, buf
,
4042 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
4045 case DRR_OBJECT_RANGE
:
4046 case DRR_WRITE_BYREF
:
4047 case DRR_FREEOBJECTS
:
4052 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4053 "invalid record type"));
4055 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
4064 recv_ecksum_set_aux(libzfs_handle_t
*hdl
, const char *target_snap
,
4065 boolean_t resumable
, boolean_t checksum
)
4067 char target_fs
[ZFS_MAX_DATASET_NAME_LEN
];
4069 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, (checksum
?
4070 "checksum mismatch" : "incomplete stream")));
4074 (void) strlcpy(target_fs
, target_snap
, sizeof (target_fs
));
4075 *strchr(target_fs
, '@') = '\0';
4076 zfs_handle_t
*zhp
= zfs_open(hdl
, target_fs
,
4077 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
4081 char token_buf
[ZFS_MAXPROPLEN
];
4082 int error
= zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
4083 token_buf
, sizeof (token_buf
),
4084 NULL
, NULL
, 0, B_TRUE
);
4086 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4087 "checksum mismatch or incomplete stream.\n"
4088 "Partially received snapshot is saved.\n"
4089 "A resuming stream can be generated on the sending "
4090 "system by running:\n"
4098 * Prepare a new nvlist of properties that are to override (-o) or be excluded
4099 * (-x) from the received dataset
4100 * recvprops: received properties from the send stream
4101 * cmdprops: raw input properties from command line
4102 * origprops: properties, both locally-set and received, currently set on the
4103 * target dataset if it exists, NULL otherwise.
4104 * oxprops: valid output override (-o) and excluded (-x) properties
4107 zfs_setup_cmdline_props(libzfs_handle_t
*hdl
, zfs_type_t type
,
4108 char *fsname
, boolean_t zoned
, boolean_t recursive
, boolean_t newfs
,
4109 boolean_t raw
, boolean_t toplevel
, nvlist_t
*recvprops
, nvlist_t
*cmdprops
,
4110 nvlist_t
*origprops
, nvlist_t
**oxprops
, uint8_t **wkeydata_out
,
4111 uint_t
*wkeylen_out
, const char *errbuf
)
4114 nvlist_t
*oprops
, *voprops
;
4115 zfs_handle_t
*zhp
= NULL
;
4116 zpool_handle_t
*zpool_hdl
= NULL
;
4119 char namebuf
[ZFS_MAX_DATASET_NAME_LEN
];
4121 if (nvlist_empty(cmdprops
))
4122 return (0); /* No properties to override or exclude */
4124 *oxprops
= fnvlist_alloc();
4125 oprops
= fnvlist_alloc();
4127 strlcpy(namebuf
, fsname
, ZFS_MAX_DATASET_NAME_LEN
);
4130 * Get our dataset handle. The target dataset may not exist yet.
4132 if (zfs_dataset_exists(hdl
, namebuf
, ZFS_TYPE_DATASET
)) {
4133 zhp
= zfs_open(hdl
, namebuf
, ZFS_TYPE_DATASET
);
4140 /* open the zpool handle */
4141 cp
= strchr(namebuf
, '/');
4144 zpool_hdl
= zpool_open(hdl
, namebuf
);
4145 if (zpool_hdl
== NULL
) {
4150 /* restore namebuf to match fsname for later use */
4155 * first iteration: process excluded (-x) properties now and gather
4156 * added (-o) properties to be later processed by zfs_valid_proplist()
4159 while ((nvp
= nvlist_next_nvpair(cmdprops
, nvp
)) != NULL
) {
4160 const char *name
= nvpair_name(nvp
);
4161 zfs_prop_t prop
= zfs_name_to_prop(name
);
4164 * It turns out, if we don't normalize "aliased" names
4165 * e.g. compress= against the "real" names (e.g. compression)
4166 * here, then setting/excluding them does not work as
4169 * But since user-defined properties wouldn't have a valid
4170 * mapping here, we do this conditional dance.
4172 const char *newname
= name
;
4173 if (prop
>= ZFS_PROP_TYPE
)
4174 newname
= zfs_prop_to_name(prop
);
4176 /* "origin" is processed separately, don't handle it here */
4177 if (prop
== ZFS_PROP_ORIGIN
)
4180 /* raw streams can't override encryption properties */
4181 if ((zfs_prop_encryption_key_param(prop
) ||
4182 prop
== ZFS_PROP_ENCRYPTION
) && raw
) {
4183 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4184 "encryption property '%s' cannot "
4185 "be set or excluded for raw streams."), name
);
4186 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
4191 * For plain replicated send, we can ignore encryption
4192 * properties other than first stream
4194 if ((zfs_prop_encryption_key_param(prop
) || prop
==
4195 ZFS_PROP_ENCRYPTION
) && !newfs
&& recursive
&& !raw
) {
4199 /* incremental streams can only exclude encryption properties */
4200 if ((zfs_prop_encryption_key_param(prop
) ||
4201 prop
== ZFS_PROP_ENCRYPTION
) && !newfs
&&
4202 nvpair_type(nvp
) != DATA_TYPE_BOOLEAN
) {
4203 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4204 "encryption property '%s' cannot "
4205 "be set for incremental streams."), name
);
4206 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
4210 switch (nvpair_type(nvp
)) {
4211 case DATA_TYPE_BOOLEAN
: /* -x property */
4213 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
4214 * a property: this is done by forcing an explicit
4215 * inherit on the destination so the effective value is
4216 * not the one we received from the send stream.
4218 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
) &&
4219 !zfs_prop_user(name
)) {
4220 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
4221 "Warning: %s: property '%s' does not "
4222 "apply to datasets of this type\n"),
4227 * We do this only if the property is not already
4228 * locally-set, in which case its value will take
4229 * priority over the received anyway.
4231 if (nvlist_exists(origprops
, newname
)) {
4233 char *source
= NULL
;
4235 attrs
= fnvlist_lookup_nvlist(origprops
,
4237 if (nvlist_lookup_string(attrs
,
4238 ZPROP_SOURCE
, &source
) == 0 &&
4239 strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0)
4243 * We can't force an explicit inherit on non-inheritable
4244 * properties: if we're asked to exclude this kind of
4245 * values we remove them from "recvprops" input nvlist.
4247 if (!zfs_prop_user(name
) && /* can be inherited too */
4248 !zfs_prop_inheritable(prop
) &&
4249 nvlist_exists(recvprops
, newname
))
4250 fnvlist_remove(recvprops
, newname
);
4252 fnvlist_add_boolean(*oxprops
, newname
);
4254 case DATA_TYPE_STRING
: /* -o property=value */
4256 * we're trying to override a property that does not
4257 * make sense for this type of dataset, but we don't
4258 * want to fail if the receive is recursive: this comes
4259 * in handy when the send stream contains, for
4260 * instance, a child ZVOL and we're trying to receive
4261 * it with "-o atime=on"
4263 if (!zfs_prop_valid_for_type(prop
, type
, B_FALSE
) &&
4264 !zfs_prop_user(name
)) {
4267 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4268 "property '%s' does not apply to datasets "
4269 "of this type"), name
);
4270 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
4273 fnvlist_add_string(oprops
, newname
,
4274 fnvpair_value_string(nvp
));
4277 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4278 "property '%s' must be a string or boolean"), name
);
4279 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
4285 /* convert override strings properties to native */
4286 if ((voprops
= zfs_valid_proplist(hdl
, ZFS_TYPE_DATASET
,
4287 oprops
, zoned
, zhp
, zpool_hdl
, B_FALSE
, errbuf
)) == NULL
) {
4288 ret
= zfs_error(hdl
, EZFS_BADPROP
, errbuf
);
4293 * zfs_crypto_create() requires the parent name. Get it
4294 * by truncating the fsname copy stored in namebuf.
4296 cp
= strrchr(namebuf
, '/');
4300 if (!raw
&& !(!newfs
&& recursive
) &&
4301 zfs_crypto_create(hdl
, namebuf
, voprops
, NULL
,
4302 B_FALSE
, wkeydata_out
, wkeylen_out
) != 0) {
4303 fnvlist_free(voprops
);
4304 ret
= zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
4308 /* second pass: process "-o" properties */
4309 fnvlist_merge(*oxprops
, voprops
);
4310 fnvlist_free(voprops
);
4312 /* override props on child dataset are inherited */
4314 while ((nvp
= nvlist_next_nvpair(oprops
, nvp
)) != NULL
) {
4315 const char *name
= nvpair_name(nvp
);
4316 fnvlist_add_boolean(*oxprops
, name
);
4323 if (zpool_hdl
!= NULL
)
4324 zpool_close(zpool_hdl
);
4325 fnvlist_free(oprops
);
4330 * Restores a backup of tosnap from the file descriptor specified by infd.
4333 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
4334 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
4335 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
4336 avl_tree_t
*stream_avl
, char **top_zfs
,
4337 const char *finalsnap
, nvlist_t
*cmdprops
)
4339 struct timespec begin_time
;
4340 int ioctl_err
, ioctl_errno
, err
;
4342 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
4343 char errbuf
[ERRBUFLEN
];
4344 const char *chopprefix
;
4345 boolean_t newfs
= B_FALSE
;
4346 boolean_t stream_wantsnewfs
, stream_resumingnewfs
;
4347 boolean_t newprops
= B_FALSE
;
4348 uint64_t read_bytes
= 0;
4349 uint64_t errflags
= 0;
4350 uint64_t parent_snapguid
= 0;
4351 prop_changelist_t
*clp
= NULL
;
4352 nvlist_t
*snapprops_nvlist
= NULL
;
4353 nvlist_t
*snapholds_nvlist
= NULL
;
4354 zprop_errflags_t prop_errflags
;
4355 nvlist_t
*prop_errors
= NULL
;
4356 boolean_t recursive
;
4357 char *snapname
= NULL
;
4358 char destsnap
[MAXPATHLEN
* 2];
4359 char origin
[MAXNAMELEN
] = {0};
4360 char name
[MAXPATHLEN
];
4361 char tmp_keylocation
[MAXNAMELEN
] = {0};
4362 nvlist_t
*rcvprops
= NULL
; /* props received from the send stream */
4363 nvlist_t
*oxprops
= NULL
; /* override (-o) and exclude (-x) props */
4364 nvlist_t
*origprops
= NULL
; /* original props (if destination exists) */
4365 zfs_type_t type
= ZFS_TYPE_INVALID
;
4366 boolean_t toplevel
= B_FALSE
;
4367 boolean_t zoned
= B_FALSE
;
4368 boolean_t hastoken
= B_FALSE
;
4370 uint8_t *wkeydata
= NULL
;
4373 #ifndef CLOCK_MONOTONIC_RAW
4374 #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
4376 clock_gettime(CLOCK_MONOTONIC_RAW
, &begin_time
);
4378 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4381 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
4384 /* Did the user request holds be skipped via zfs recv -k? */
4385 boolean_t holds
= flags
->holds
&& !flags
->skipholds
;
4387 if (stream_avl
!= NULL
) {
4388 char *keylocation
= NULL
;
4389 nvlist_t
*lookup
= NULL
;
4390 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
4393 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
4395 err
= nvlist_lookup_nvlist(fs
, "props", &rcvprops
);
4397 rcvprops
= fnvlist_alloc();
4402 * The keylocation property may only be set on encryption roots,
4403 * but this dataset might not become an encryption root until
4404 * recv_fix_encryption_hierarchy() is called. That function
4405 * will fixup the keylocation anyway, so we temporarily unset
4406 * the keylocation for now to avoid any errors from the receive
4409 err
= nvlist_lookup_string(rcvprops
,
4410 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), &keylocation
);
4412 strlcpy(tmp_keylocation
, keylocation
, MAXNAMELEN
);
4413 (void) nvlist_remove_all(rcvprops
,
4414 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
));
4417 if (flags
->canmountoff
) {
4418 fnvlist_add_uint64(rcvprops
,
4419 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0);
4420 } else if (newprops
) { /* nothing in rcvprops, eliminate it */
4421 fnvlist_free(rcvprops
);
4425 if (0 == nvlist_lookup_nvlist(fs
, "snapprops", &lookup
)) {
4426 snapprops_nvlist
= fnvlist_lookup_nvlist(lookup
,
4430 if (0 == nvlist_lookup_nvlist(fs
, "snapholds",
4432 snapholds_nvlist
= fnvlist_lookup_nvlist(
4441 * Determine how much of the snapshot name stored in the stream
4442 * we are going to tack on to the name they specified on the
4443 * command line, and how much we are going to chop off.
4445 * If they specified a snapshot, chop the entire name stored in
4448 if (flags
->istail
) {
4450 * A filesystem was specified with -e. We want to tack on only
4451 * the tail of the sent snapshot path.
4453 if (strchr(tosnap
, '@')) {
4454 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
4455 "argument - snapshot not allowed with -e"));
4456 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
4460 chopprefix
= strrchr(sendfs
, '/');
4462 if (chopprefix
== NULL
) {
4464 * The tail is the poolname, so we need to
4465 * prepend a path separator.
4467 int len
= strlen(drrb
->drr_toname
);
4468 cp
= umem_alloc(len
+ 2, UMEM_NOFAIL
);
4470 (void) strcpy(&cp
[1], drrb
->drr_toname
);
4473 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
4475 } else if (flags
->isprefix
) {
4477 * A filesystem was specified with -d. We want to tack on
4478 * everything but the first element of the sent snapshot path
4479 * (all but the pool name).
4481 if (strchr(tosnap
, '@')) {
4482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
4483 "argument - snapshot not allowed with -d"));
4484 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
4488 chopprefix
= strchr(drrb
->drr_toname
, '/');
4489 if (chopprefix
== NULL
)
4490 chopprefix
= strchr(drrb
->drr_toname
, '@');
4491 } else if (strchr(tosnap
, '@') == NULL
) {
4493 * If a filesystem was specified without -d or -e, we want to
4494 * tack on everything after the fs specified by 'zfs send'.
4496 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
4498 /* A snapshot was specified as an exact path (no -d or -e). */
4500 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4501 "cannot specify snapshot name for multi-snapshot "
4503 err
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
4506 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
4509 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
4510 ASSERT(chopprefix
> drrb
->drr_toname
|| strchr(sendfs
, '/') == NULL
);
4511 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
) ||
4512 strchr(sendfs
, '/') == NULL
);
4513 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
4514 chopprefix
[0] == '\0');
4517 * Determine name of destination snapshot.
4519 (void) strlcpy(destsnap
, tosnap
, sizeof (destsnap
));
4520 (void) strlcat(destsnap
, chopprefix
, sizeof (destsnap
));
4522 umem_free(cp
, strlen(cp
) + 1);
4523 if (!zfs_name_valid(destsnap
, ZFS_TYPE_SNAPSHOT
)) {
4524 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
4529 * Determine the name of the origin snapshot.
4532 (void) strlcpy(origin
, originsnap
, sizeof (origin
));
4534 (void) printf("using provided clone origin %s\n",
4536 } else if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
4537 if (guid_to_name(hdl
, destsnap
,
4538 drrb
->drr_fromguid
, B_FALSE
, origin
) != 0) {
4539 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4540 "local origin for clone %s does not exist"),
4542 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4546 (void) printf("found clone origin %s\n", origin
);
4549 if ((DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
4550 DMU_BACKUP_FEATURE_DEDUP
)) {
4551 (void) fprintf(stderr
,
4552 gettext("ERROR: \"zfs receive\" no longer supports "
4553 "deduplicated send streams. Use\n"
4554 "the \"zstream redup\" command to convert this stream "
4556 "non-deduplicated stream.\n"));
4557 err
= zfs_error(hdl
, EZFS_NOTSUP
, errbuf
);
4561 boolean_t resuming
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
4562 DMU_BACKUP_FEATURE_RESUMING
;
4563 boolean_t raw
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
4564 DMU_BACKUP_FEATURE_RAW
;
4565 boolean_t embedded
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
4566 DMU_BACKUP_FEATURE_EMBED_DATA
;
4567 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
4568 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
) && !resuming
;
4569 stream_resumingnewfs
= (drrb
->drr_fromguid
== 0 ||
4570 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
) && resuming
;
4572 if (stream_wantsnewfs
) {
4574 * if the parent fs does not exist, look for it based on
4575 * the parent snap GUID
4577 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
4578 "cannot receive new filesystem stream"));
4580 (void) strlcpy(name
, destsnap
, sizeof (name
));
4581 cp
= strrchr(name
, '/');
4585 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
4586 char suffix
[ZFS_MAX_DATASET_NAME_LEN
];
4587 (void) strlcpy(suffix
, strrchr(destsnap
, '/'),
4589 if (guid_to_name(hdl
, name
, parent_snapguid
,
4590 B_FALSE
, destsnap
) == 0) {
4591 *strchr(destsnap
, '@') = '\0';
4592 (void) strlcat(destsnap
, suffix
,
4598 * If the fs does not exist, look for it based on the
4602 (void) snprintf(errbuf
, sizeof (errbuf
),
4603 dgettext(TEXT_DOMAIN
,
4604 "cannot receive resume stream"));
4606 (void) snprintf(errbuf
, sizeof (errbuf
),
4607 dgettext(TEXT_DOMAIN
,
4608 "cannot receive incremental stream"));
4611 (void) strlcpy(name
, destsnap
, sizeof (name
));
4612 *strchr(name
, '@') = '\0';
4615 * If the exact receive path was specified and this is the
4616 * topmost path in the stream, then if the fs does not exist we
4617 * should look no further.
4619 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
4620 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
4621 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
4622 char snap
[ZFS_MAX_DATASET_NAME_LEN
];
4623 (void) strlcpy(snap
, strchr(destsnap
, '@'),
4625 if (guid_to_name(hdl
, name
, drrb
->drr_fromguid
,
4626 B_FALSE
, destsnap
) == 0) {
4627 *strchr(destsnap
, '@') = '\0';
4628 (void) strlcat(destsnap
, snap
,
4634 (void) strlcpy(name
, destsnap
, sizeof (name
));
4635 *strchr(name
, '@') = '\0';
4637 redacted
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
4638 DMU_BACKUP_FEATURE_REDACTED
;
4641 if (flags
->isprefix
|| flags
->istail
|| flags
->force
||
4642 flags
->canmountoff
|| flags
->resumable
|| flags
->nomount
||
4644 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4645 "corrective recv can not be used when combined with"
4647 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
4651 get_snap_guid(hdl
, name
, strchr(destsnap
, '@') + 1);
4653 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4654 "corrective recv must specify an existing snapshot"
4656 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
4658 } else if (guid
!= drrb
->drr_toguid
) {
4659 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4660 "local snapshot doesn't match the snapshot"
4661 " in the provided stream"));
4662 err
= zfs_error(hdl
, EZFS_WRONG_PARENT
, errbuf
);
4665 } else if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
4666 zfs_cmd_t zc
= {"\0"};
4667 zfs_handle_t
*zhp
= NULL
;
4668 boolean_t encrypted
;
4670 (void) strcpy(zc
.zc_name
, name
);
4673 * Destination fs exists. It must be one of these cases:
4674 * - an incremental send stream
4675 * - the stream specifies a new fs (full stream or clone)
4676 * and they want us to blow away the existing fs (and
4677 * have therefore specified -F and removed any snapshots)
4678 * - we are resuming a failed receive.
4680 if (stream_wantsnewfs
) {
4681 boolean_t is_volume
= drrb
->drr_type
== DMU_OST_ZVOL
;
4682 if (!flags
->force
) {
4683 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4684 "destination '%s' exists\n"
4685 "must specify -F to overwrite it"), name
);
4686 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4689 if (zfs_ioctl(hdl
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
4691 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4692 "destination has snapshots (eg. %s)\n"
4693 "must destroy them to overwrite it"),
4695 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4698 if (is_volume
&& strrchr(name
, '/') == NULL
) {
4699 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4700 "destination %s is the root dataset\n"
4701 "cannot overwrite with a ZVOL"),
4703 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4707 zfs_ioctl(hdl
, ZFS_IOC_DATASET_LIST_NEXT
,
4709 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4710 "destination has children (eg. %s)\n"
4711 "cannot overwrite with a ZVOL"),
4713 err
= zfs_error(hdl
, EZFS_WRONG_PARENT
, errbuf
);
4718 if ((zhp
= zfs_open(hdl
, name
,
4719 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
4725 * When receiving full/newfs on existing dataset, then it
4726 * should be done with "-F" flag. Its enforced for initial
4727 * receive in previous checks in this function.
4728 * Similarly, on resuming full/newfs recv on existing dataset,
4729 * it should be done with "-F" flag.
4731 * When dataset doesn't exist, then full/newfs recv is done on
4732 * newly created dataset and it's marked INCONSISTENT. But
4733 * When receiving on existing dataset, recv is first done on
4734 * %recv and its marked INCONSISTENT. Existing dataset is not
4735 * marked INCONSISTENT.
4736 * Resume of full/newfs receive with dataset not INCONSISTENT
4737 * indicates that its resuming newfs on existing dataset. So,
4738 * enforce "-F" flag in this case.
4740 if (stream_resumingnewfs
&&
4741 !zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
) &&
4744 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4745 "Resuming recv on existing destination '%s'\n"
4746 "must specify -F to overwrite it"), name
);
4747 err
= zfs_error(hdl
, EZFS_RESUME_EXISTS
, errbuf
);
4751 if (stream_wantsnewfs
&&
4752 zhp
->zfs_dmustats
.dds_origin
[0]) {
4754 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4755 "destination '%s' is a clone\n"
4756 "must destroy it to overwrite it"), name
);
4757 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
4762 * Raw sends can not be performed as an incremental on top
4763 * of existing unencrypted datasets. zfs recv -F can't be
4764 * used to blow away an existing encrypted filesystem. This
4765 * is because it would require the dsl dir to point to the
4766 * new key (or lack of a key) and the old key at the same
4767 * time. The -F flag may still be used for deleting
4768 * intermediate snapshots that would otherwise prevent the
4769 * receive from working.
4771 encrypted
= zfs_prop_get_int(zhp
, ZFS_PROP_ENCRYPTION
) !=
4773 if (!stream_wantsnewfs
&& !encrypted
&& raw
) {
4775 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4776 "cannot perform raw receive on top of "
4777 "existing unencrypted dataset"));
4778 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
4782 if (stream_wantsnewfs
&& flags
->force
&&
4783 ((raw
&& !encrypted
) || encrypted
)) {
4785 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4786 "zfs receive -F cannot be used to destroy an "
4787 "encrypted filesystem or overwrite an "
4788 "unencrypted one with an encrypted one"));
4789 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
4793 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
4794 (stream_wantsnewfs
|| stream_resumingnewfs
)) {
4795 /* We can't do online recv in this case */
4796 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
4797 flags
->forceunmount
? MS_FORCE
: 0);
4803 if (changelist_prefix(clp
) != 0) {
4804 changelist_free(clp
);
4812 * If we are resuming a newfs, set newfs here so that we will
4813 * mount it if the recv succeeds this time. We can tell
4814 * that it was a newfs on the first recv because the fs
4815 * itself will be inconsistent (if the fs existed when we
4816 * did the first recv, we would have received it into
4819 if (resuming
&& zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
))
4822 /* we want to know if we're zoned when validating -o|-x props */
4823 zoned
= zfs_prop_get_int(zhp
, ZFS_PROP_ZONED
);
4825 /* may need this info later, get it now we have zhp around */
4826 if (zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
, NULL
, 0,
4827 NULL
, NULL
, 0, B_TRUE
) == 0)
4830 /* gather existing properties on destination */
4831 origprops
= fnvlist_alloc();
4832 fnvlist_merge(origprops
, zhp
->zfs_props
);
4833 fnvlist_merge(origprops
, zhp
->zfs_user_props
);
4840 * Destination filesystem does not exist. Therefore we better
4841 * be creating a new filesystem (either from a full backup, or
4842 * a clone). It would therefore be invalid if the user
4843 * specified only the pool name (i.e. if the destination name
4844 * contained no slash character).
4846 cp
= strrchr(name
, '/');
4848 if (!stream_wantsnewfs
|| cp
== NULL
) {
4849 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4850 "destination '%s' does not exist"), name
);
4851 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
4856 * Trim off the final dataset component so we perform the
4857 * recvbackup ioctl to the filesystems's parent.
4861 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
4862 create_parents(hdl
, destsnap
, strlen(tosnap
)) != 0) {
4863 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
4867 /* validate parent */
4868 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
4870 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
4873 if (zfs_get_type(zhp
) != ZFS_TYPE_FILESYSTEM
) {
4874 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4875 "parent '%s' is not a filesystem"), name
);
4876 err
= zfs_error(hdl
, EZFS_WRONG_PARENT
, errbuf
);
4887 if (flags
->verbose
) {
4888 (void) printf("%s %s%s stream of %s into %s\n",
4889 flags
->dryrun
? "would receive" : "receiving",
4890 flags
->heal
? " corrective" : "",
4891 drrb
->drr_fromguid
? "incremental" : "full",
4892 drrb
->drr_toname
, destsnap
);
4893 (void) fflush(stdout
);
4897 * If this is the top-level dataset, record it so we can use it
4898 * for recursive operations later.
4900 if (top_zfs
!= NULL
&&
4901 (*top_zfs
== NULL
|| strcmp(*top_zfs
, name
) == 0)) {
4903 if (*top_zfs
== NULL
)
4904 *top_zfs
= zfs_strdup(hdl
, name
);
4907 if (drrb
->drr_type
== DMU_OST_ZVOL
) {
4908 type
= ZFS_TYPE_VOLUME
;
4909 } else if (drrb
->drr_type
== DMU_OST_ZFS
) {
4910 type
= ZFS_TYPE_FILESYSTEM
;
4912 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
4913 "invalid record type: 0x%d"), drrb
->drr_type
);
4914 err
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
4917 if ((err
= zfs_setup_cmdline_props(hdl
, type
, name
, zoned
, recursive
,
4918 stream_wantsnewfs
, raw
, toplevel
, rcvprops
, cmdprops
, origprops
,
4919 &oxprops
, &wkeydata
, &wkeylen
, errbuf
)) != 0)
4923 * When sending with properties (zfs send -p), the encryption property
4924 * is not included because it is a SETONCE property and therefore
4925 * treated as read only. However, we are always able to determine its
4926 * value because raw sends will include it in the DRR_BDEGIN payload
4927 * and non-raw sends with properties are not allowed for encrypted
4928 * datasets. Therefore, if this is a non-raw properties stream, we can
4929 * infer that the value should be ZIO_CRYPT_OFF and manually add that
4930 * to the received properties.
4932 if (stream_wantsnewfs
&& !raw
&& rcvprops
!= NULL
&&
4933 !nvlist_exists(cmdprops
, zfs_prop_to_name(ZFS_PROP_ENCRYPTION
))) {
4934 if (oxprops
== NULL
)
4935 oxprops
= fnvlist_alloc();
4936 fnvlist_add_uint64(oxprops
,
4937 zfs_prop_to_name(ZFS_PROP_ENCRYPTION
), ZIO_CRYPT_OFF
);
4940 if (flags
->dryrun
) {
4941 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
4944 * We have read the DRR_BEGIN record, but we have
4945 * not yet read the payload. For non-dryrun sends
4946 * this will be done by the kernel, so we must
4947 * emulate that here, before attempting to read
4950 err
= recv_read(hdl
, infd
, buf
, drr
->drr_payloadlen
,
4951 flags
->byteswap
, NULL
);
4956 err
= recv_skip(hdl
, infd
, flags
->byteswap
);
4961 err
= ioctl_err
= lzc_receive_with_heal(destsnap
, rcvprops
,
4962 oxprops
, wkeydata
, wkeylen
, origin
, flags
->force
,
4963 flags
->heal
, flags
->resumable
, raw
, infd
, drr_noswap
, -1,
4964 &read_bytes
, &errflags
, NULL
, &prop_errors
);
4966 err
= ioctl_err
= lzc_receive_with_cmdprops(destsnap
, rcvprops
,
4967 oxprops
, wkeydata
, wkeylen
, origin
, flags
->force
,
4968 flags
->resumable
, raw
, infd
, drr_noswap
, -1, &read_bytes
,
4969 &errflags
, NULL
, &prop_errors
);
4971 ioctl_errno
= ioctl_err
;
4972 prop_errflags
= errflags
;
4975 nvpair_t
*prop_err
= NULL
;
4977 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
4978 prop_err
)) != NULL
) {
4983 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
4984 (void) nvpair_value_int32(prop_err
, &intval
);
4985 if (strcmp(nvpair_name(prop_err
),
4986 ZPROP_N_MORE_ERRORS
) == 0) {
4987 trunc_prop_errs(intval
);
4989 } else if (snapname
== NULL
|| finalsnap
== NULL
||
4990 strcmp(finalsnap
, snapname
) == 0 ||
4991 strcmp(nvpair_name(prop_err
),
4992 zfs_prop_to_name(ZFS_PROP_REFQUOTA
)) != 0) {
4994 * Skip the special case of, for example,
4995 * "refquota", errors on intermediate
4996 * snapshots leading up to a final one.
4997 * That's why we have all of the checks above.
4999 * See zfs_ioctl.c's extract_delay_props() for
5000 * a list of props which can fail on
5001 * intermediate snapshots, but shouldn't
5002 * affect the overall receive.
5004 (void) snprintf(tbuf
, sizeof (tbuf
),
5005 dgettext(TEXT_DOMAIN
,
5006 "cannot receive %s property on %s"),
5007 nvpair_name(prop_err
), name
);
5008 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
5013 if (err
== 0 && snapprops_nvlist
) {
5014 zfs_cmd_t zc
= {"\0"};
5016 (void) strlcpy(zc
.zc_name
, destsnap
, sizeof (zc
.zc_name
));
5017 zc
.zc_cookie
= B_TRUE
; /* received */
5018 zcmd_write_src_nvlist(hdl
, &zc
, snapprops_nvlist
);
5019 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
5020 zcmd_free_nvlists(&zc
);
5022 if (err
== 0 && snapholds_nvlist
) {
5024 nvlist_t
*holds
, *errors
= NULL
;
5025 int cleanup_fd
= -1;
5027 VERIFY(0 == nvlist_alloc(&holds
, 0, KM_SLEEP
));
5028 for (pair
= nvlist_next_nvpair(snapholds_nvlist
, NULL
);
5030 pair
= nvlist_next_nvpair(snapholds_nvlist
, pair
)) {
5031 fnvlist_add_string(holds
, destsnap
, nvpair_name(pair
));
5033 (void) lzc_hold(holds
, cleanup_fd
, &errors
);
5034 fnvlist_free(snapholds_nvlist
);
5035 fnvlist_free(holds
);
5038 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
5040 * It may be that this snapshot already exists,
5041 * in which case we want to consume & ignore it
5042 * rather than failing.
5044 avl_tree_t
*local_avl
;
5045 nvlist_t
*local_nv
, *fs
;
5046 cp
= strchr(destsnap
, '@');
5049 * XXX Do this faster by just iterating over snaps in
5050 * this fs. Also if zc_value does not exist, we will
5051 * get a strange "does not exist" error message.
5054 if (gather_nvlist(hdl
, destsnap
, NULL
, NULL
, B_FALSE
, B_TRUE
,
5055 B_FALSE
, B_FALSE
, B_FALSE
, B_FALSE
, B_FALSE
, B_FALSE
,
5056 B_TRUE
, &local_nv
, &local_avl
) == 0) {
5058 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
5059 fsavl_destroy(local_avl
);
5060 fnvlist_free(local_nv
);
5063 if (flags
->verbose
) {
5064 (void) printf("snap %s already exists; "
5065 "ignoring\n", destsnap
);
5067 err
= ioctl_err
= recv_skip(hdl
, infd
,
5074 if (ioctl_err
!= 0) {
5075 switch (ioctl_errno
) {
5077 cp
= strchr(destsnap
, '@');
5079 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5080 "most recent snapshot of %s does not\n"
5081 "match incremental source"), destsnap
);
5082 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
5086 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5087 "destination %s has been modified\n"
5088 "since most recent snapshot"), name
);
5089 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
5093 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5094 "key must be loaded to do a non-raw "
5095 "corrective recv on an encrypted "
5097 } else if (raw
&& stream_wantsnewfs
) {
5098 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5099 "failed to create encryption key"));
5100 } else if (raw
&& !stream_wantsnewfs
) {
5101 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5102 "encryption key does not match "
5105 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5106 "inherited key must be loaded"));
5108 (void) zfs_error(hdl
, EZFS_CRYPTOFAILED
, errbuf
);
5111 cp
= strchr(destsnap
, '@');
5113 /* it's the containing fs that exists */
5116 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5117 "destination already exists"));
5118 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
5119 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
5124 if (embedded
&& !raw
) {
5125 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5126 "incompatible embedded data stream "
5127 "feature with encrypted receive."));
5128 } else if (flags
->resumable
) {
5129 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5130 "kernel modules must be upgraded to "
5131 "receive this stream."));
5133 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
5136 case ZFS_ERR_STREAM_TRUNCATED
:
5138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5139 "corrective receive was not able to "
5140 "reconstruct the data needed for "
5143 recv_ecksum_set_aux(hdl
, destsnap
,
5144 flags
->resumable
, ioctl_err
== ECKSUM
);
5145 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
5147 case ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH
:
5148 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5149 "incremental send stream requires -L "
5150 "(--large-block), to match previous receive."));
5151 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
5155 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5156 "stream is not compatible with the "
5157 "data in the pool."));
5159 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5160 "pool must be upgraded to receive this "
5162 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
5165 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5166 "destination %s space quota exceeded."), name
);
5167 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
5169 case ZFS_ERR_FROM_IVSET_GUID_MISSING
:
5170 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5171 "IV set guid missing. See errata %u at "
5172 "https://openzfs.github.io/openzfs-docs/msg/"
5174 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION
);
5175 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
5177 case ZFS_ERR_FROM_IVSET_GUID_MISMATCH
:
5178 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5179 "IV set guid mismatch. See the 'zfs receive' "
5180 "man page section\n discussing the limitations "
5181 "of raw encrypted send streams."));
5182 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
5184 case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING
:
5185 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5186 "Spill block flag missing for raw send.\n"
5187 "The zfs software on the sending system must "
5189 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
5191 case ZFS_ERR_RESUME_EXISTS
:
5192 cp
= strchr(destsnap
, '@');
5194 /* it's the containing fs that exists */
5197 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5198 "Resuming recv on existing dataset without force"));
5199 (void) zfs_error_fmt(hdl
, EZFS_RESUME_EXISTS
,
5200 dgettext(TEXT_DOMAIN
, "cannot resume recv %s"),
5205 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5206 "zfs receive required kernel memory allocation "
5207 "larger than the system can support. Please file "
5208 "an issue at the OpenZFS issue tracker:\n"
5209 "https://github.com/openzfs/zfs/issues/new"));
5210 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
5214 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5215 "destination %s contains "
5216 "partially-complete state from "
5217 "\"zfs receive -s\"."), name
);
5218 (void) zfs_error(hdl
, EZFS_BUSY
, errbuf
);
5223 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
5228 * Mount the target filesystem (if created). Also mount any
5229 * children of the target filesystem if we did a replication
5230 * receive (indicated by stream_avl being non-NULL).
5233 if (!flags
->nomount
)
5234 err
|= changelist_postfix(clp
);
5235 changelist_free(clp
);
5238 if ((newfs
|| stream_avl
) && type
== ZFS_TYPE_FILESYSTEM
&& !redacted
)
5239 flags
->domount
= B_TRUE
;
5241 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
5242 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
5243 "failed to clear unreceived properties on %s"), name
);
5244 (void) fprintf(stderr
, "\n");
5246 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
5247 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
5248 "failed to restore original properties on %s"), name
);
5249 (void) fprintf(stderr
, "\n");
5252 if (err
|| ioctl_err
) {
5257 if (flags
->verbose
) {
5260 uint64_t bytes
= read_bytes
;
5261 struct timespec delta
;
5262 clock_gettime(CLOCK_MONOTONIC_RAW
, &delta
);
5263 if (begin_time
.tv_nsec
> delta
.tv_nsec
) {
5265 1000000000 + delta
.tv_nsec
- begin_time
.tv_nsec
;
5268 delta
.tv_nsec
-= begin_time
.tv_nsec
;
5269 delta
.tv_sec
-= begin_time
.tv_sec
;
5270 if (delta
.tv_sec
== 0 && delta
.tv_nsec
== 0)
5272 double delta_f
= delta
.tv_sec
+ (delta
.tv_nsec
/ 1e9
);
5273 zfs_nicebytes(bytes
, buf1
, sizeof (buf1
));
5274 zfs_nicebytes(bytes
/ delta_f
, buf2
, sizeof (buf2
));
5276 (void) printf("received %s stream in %.2f seconds (%s/sec)\n",
5277 buf1
, delta_f
, buf2
);
5282 if (prop_errors
!= NULL
)
5283 fnvlist_free(prop_errors
);
5285 if (tmp_keylocation
[0] != '\0') {
5286 fnvlist_add_string(rcvprops
,
5287 zfs_prop_to_name(ZFS_PROP_KEYLOCATION
), tmp_keylocation
);
5291 fnvlist_free(rcvprops
);
5293 fnvlist_free(oxprops
);
5294 fnvlist_free(origprops
);
5300 * Check properties we were asked to override (both -o|-x)
5303 zfs_receive_checkprops(libzfs_handle_t
*hdl
, nvlist_t
*props
,
5306 nvpair_t
*nvp
= NULL
;
5310 while ((nvp
= nvlist_next_nvpair(props
, nvp
)) != NULL
) {
5311 name
= nvpair_name(nvp
);
5312 prop
= zfs_name_to_prop(name
);
5314 if (prop
== ZPROP_USERPROP
) {
5315 if (!zfs_prop_user(name
)) {
5316 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5317 "%s: invalid property '%s'"), errbuf
, name
);
5323 * "origin" is readonly but is used to receive datasets as
5324 * clones so we don't raise an error here
5326 if (prop
== ZFS_PROP_ORIGIN
)
5329 /* encryption params have their own verification later */
5330 if (prop
== ZFS_PROP_ENCRYPTION
||
5331 zfs_prop_encryption_key_param(prop
))
5335 * cannot override readonly, set-once and other specific
5336 * settable properties
5338 if (zfs_prop_readonly(prop
) || prop
== ZFS_PROP_VERSION
||
5339 prop
== ZFS_PROP_VOLSIZE
) {
5340 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5341 "%s: invalid property '%s'"), errbuf
, name
);
5350 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
5351 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
5352 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
,
5353 const char *finalsnap
, nvlist_t
*cmdprops
)
5356 dmu_replay_record_t drr
, drr_noswap
;
5357 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
5358 char errbuf
[ERRBUFLEN
];
5359 zio_cksum_t zcksum
= { { 0 } };
5360 uint64_t featureflags
;
5363 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
5366 /* check cmdline props, raise an error if they cannot be received */
5367 if (!zfs_receive_checkprops(hdl
, cmdprops
, errbuf
))
5368 return (zfs_error(hdl
, EZFS_BADPROP
, errbuf
));
5370 if (flags
->isprefix
&&
5371 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
5372 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
5373 "(%s) does not exist"), tosnap
);
5374 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
5377 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
5378 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
5379 "(%s) does not exist"), originsnap
);
5380 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
5383 /* read in the BEGIN record */
5384 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
5388 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
5389 /* It's the double end record at the end of a package */
5393 /* the kernel needs the non-byteswapped begin record */
5396 flags
->byteswap
= B_FALSE
;
5397 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
5399 * We computed the checksum in the wrong byteorder in
5400 * recv_read() above; do it again correctly.
5402 memset(&zcksum
, 0, sizeof (zio_cksum_t
));
5403 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
5404 flags
->byteswap
= B_TRUE
;
5406 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
5407 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
5408 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
5409 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
5410 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
5411 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
5412 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
5413 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
5414 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
5417 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
5418 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
5419 "stream (bad magic number)"));
5420 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
5423 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
5424 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
5426 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
5427 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
5429 * Let's be explicit about this one, since rather than
5430 * being a new feature we can't know, it's an old
5431 * feature we dropped.
5433 if (featureflags
& DMU_BACKUP_FEATURE_DEDUP
) {
5434 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5435 "stream has deprecated feature: dedup, try "
5436 "'zstream redup [send in a file] | zfs recv "
5439 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
5440 "stream has unsupported feature, feature flags = "
5441 "%llx (unknown flags = %llx)"),
5442 (u_longlong_t
)featureflags
,
5443 (u_longlong_t
)((featureflags
) &
5444 ~DMU_BACKUP_FEATURE_MASK
));
5446 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
5449 /* Holds feature is set once in the compound stream header. */
5450 if (featureflags
& DMU_BACKUP_FEATURE_HOLDS
)
5451 flags
->holds
= B_TRUE
;
5453 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
5454 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
5455 "stream (bad snapshot name)"));
5456 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
5459 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
5460 char nonpackage_sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
5461 if (sendfs
== NULL
) {
5463 * We were not called from zfs_receive_package(). Get
5464 * the fs specified by 'zfs send'.
5467 (void) strlcpy(nonpackage_sendfs
,
5468 drr
.drr_u
.drr_begin
.drr_toname
,
5469 sizeof (nonpackage_sendfs
));
5470 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
5472 sendfs
= nonpackage_sendfs
;
5473 VERIFY(finalsnap
== NULL
);
5475 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
5476 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
5477 finalsnap
, cmdprops
));
5479 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
5480 DMU_COMPOUNDSTREAM
);
5481 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
5482 &zcksum
, top_zfs
, cmdprops
));
5487 * Restores a backup of tosnap from the file descriptor specified by infd.
5488 * Return 0 on total success, -2 if some things couldn't be
5489 * destroyed/renamed/promoted, -1 if some things couldn't be received.
5490 * (-1 will override -2, if -1 and the resumable flag was specified the
5491 * transfer can be resumed if the sending side supports it).
5494 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
5495 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
5497 char *top_zfs
= NULL
;
5500 char *originsnap
= NULL
;
5503 * The only way fstat can fail is if we do not have a valid file
5506 if (fstat(infd
, &sb
) == -1) {
5512 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
5513 if (err
&& err
!= ENOENT
)
5517 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
5518 stream_avl
, &top_zfs
, NULL
, props
);
5520 if (err
== 0 && !flags
->nomount
&& flags
->domount
&& top_zfs
) {
5521 zfs_handle_t
*zhp
= NULL
;
5522 prop_changelist_t
*clp
= NULL
;
5524 zhp
= zfs_open(hdl
, top_zfs
,
5525 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
5530 if (zhp
->zfs_type
== ZFS_TYPE_VOLUME
) {
5535 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
5536 CL_GATHER_MOUNT_ALWAYS
,
5537 flags
->forceunmount
? MS_FORCE
: 0);
5544 /* mount and share received datasets */
5545 err
= changelist_postfix(clp
);
5546 changelist_free(clp
);