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;
89 dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len,
90 zio_cksum_t *zc, int outfd)
92 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
93 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
94 fletcher_4_incremental_native(drr,
95 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
96 if (drr->drr_type != DRR_BEGIN) {
97 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
98 drr_checksum.drr_checksum));
99 drr->drr_u.drr_checksum.drr_checksum = *zc;
101 fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
102 sizeof (zio_cksum_t), zc);
103 if (write(outfd, drr, sizeof (*drr)) == -1)
105 if (payload_len != 0) {
106 fletcher_4_incremental_native(payload, payload_len, zc);
107 if (write(outfd, payload, payload_len) == -1)
114 * Routines for dealing with the AVL tree of fs-nvlists
116 typedef struct fsavl_node {
124 fsavl_compare(const void *arg1, const void *arg2)
126 const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
127 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
129 return (TREE_CMP(fn1->fn_guid, fn2->fn_guid));
133 * Given the GUID of a snapshot, find its containing filesystem and
137 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
139 fsavl_node_t fn_find;
142 fn_find.fn_guid = snapguid;
144 fn = avl_find(avl, &fn_find, NULL);
147 *snapname = fn->fn_snapname;
148 return (fn->fn_nvfs);
154 fsavl_destroy(avl_tree_t *avl)
163 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
170 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
173 fsavl_create(nvlist_t *fss)
176 nvpair_t *fselem = NULL;
178 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
181 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
182 offsetof(fsavl_node_t, fn_node));
184 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
185 nvlist_t *nvfs, *snaps;
186 nvpair_t *snapelem = NULL;
188 nvfs = fnvpair_value_nvlist(fselem);
189 snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
192 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
195 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
196 fsavl_destroy(fsavl);
200 fn->fn_snapname = nvpair_name(snapelem);
201 fn->fn_guid = fnvpair_value_uint64(snapelem);
204 * Note: if there are multiple snaps with the
205 * same GUID, we ignore all but one.
207 avl_index_t where = 0;
208 if (avl_find(fsavl, fn, &where) == NULL)
209 avl_insert(fsavl, fn, where);
219 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
221 typedef struct send_data {
223 * assigned inside every recursive call,
224 * restored from *_save on return:
226 * guid of fromsnap snapshot in parent dataset
227 * txg of fromsnap snapshot in current dataset
228 * txg of tosnap snapshot in current dataset
231 uint64_t parent_fromsnap_guid;
232 uint64_t fromsnap_txg;
235 /* the nvlists get accumulated during depth-first traversal */
236 nvlist_t *parent_snaps;
239 nvlist_t *snapholds; /* user holds */
241 /* send-receive configuration, does not change during traversal */
243 const char *fromsnap;
249 boolean_t skipmissing;
254 boolean_t holds; /* were holds requested with send -h */
258 * The header nvlist is of the following format:
261 * "fromsnap" -> string (if incremental)
265 * "name" -> string (full name; for debugging)
266 * "parentfromsnap" -> number (guid of fromsnap in parent)
268 * "props" -> { name -> value (only if set here) }
269 * "snaps" -> { name (lastname) -> number (guid) }
270 * "snapprops" -> { name (lastname) -> { name -> value } }
271 * "snapholds" -> { name (lastname) -> { holdname -> crtime } }
273 * "origin" -> number (guid) (if clone)
274 * "is_encroot" -> boolean
275 * "sent" -> boolean (not on-disk)
284 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
287 * Collect guid, valid props, optionally holds, etc. of a snapshot.
288 * This interface is intended for use as a zfs_iter_snapshots_sorted visitor.
291 send_iterate_snap(zfs_handle_t *zhp, void *arg)
293 send_data_t *sd = arg;
294 uint64_t guid = zhp->zfs_dmustats.dds_guid;
295 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
296 boolean_t isfromsnap, istosnap, istosnapwithnofrom;
298 const char *from = sd->fromsnap;
299 const char *to = sd->tosnap;
301 snapname = strrchr(zhp->zfs_name, '@');
302 assert(snapname != NULL);
305 isfromsnap = (from != NULL && strcmp(from, snapname) == 0);
306 istosnap = (to != NULL && strcmp(to, snapname) == 0);
307 istosnapwithnofrom = (istosnap && from == NULL);
309 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
311 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
312 "skipping snapshot %s because it was created "
313 "after the destination snapshot (%s)\n"),
320 fnvlist_add_uint64(sd->parent_snaps, snapname, guid);
323 * NB: if there is no fromsnap here (it's a newly created fs in
324 * an incremental replication), we will substitute the tosnap.
326 if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap))
327 sd->parent_fromsnap_guid = guid;
329 if (!sd->recursive) {
331 * To allow a doall stream to work properly
332 * with a NULL fromsnap
334 if (sd->doall && from == NULL && !sd->seenfrom)
335 sd->seenfrom = B_TRUE;
337 if (!sd->seenfrom && isfromsnap) {
338 sd->seenfrom = B_TRUE;
343 if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) {
352 nvlist_t *nv = fnvlist_alloc();
353 send_iterate_prop(zhp, sd->backup, nv);
354 fnvlist_add_nvlist(sd->snapprops, snapname, nv);
359 if (lzc_get_holds(zhp->zfs_name, &holds) == 0) {
360 fnvlist_add_nvlist(sd->snapholds, snapname, holds);
370 * Collect all valid props from the handle snap into an nvlist.
373 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
378 props = zfs_get_recvd_props(zhp);
380 props = zhp->zfs_props;
382 nvpair_t *elem = NULL;
383 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
384 char *propname = nvpair_name(elem);
385 zfs_prop_t prop = zfs_name_to_prop(propname);
387 if (!zfs_prop_user(propname)) {
389 * Realistically, this should never happen. However,
390 * we want the ability to add DSL properties without
391 * needing to make incompatible version changes. We
392 * need to ignore unknown properties to allow older
393 * software to still send datasets containing these
394 * properties, with the unknown properties elided.
396 if (prop == ZPROP_INVAL)
399 if (zfs_prop_readonly(prop))
403 nvlist_t *propnv = fnvpair_value_nvlist(elem);
405 boolean_t isspacelimit = (prop == ZFS_PROP_QUOTA ||
406 prop == ZFS_PROP_RESERVATION ||
407 prop == ZFS_PROP_REFQUOTA ||
408 prop == ZFS_PROP_REFRESERVATION);
409 if (isspacelimit && zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
413 if (nvlist_lookup_string(propnv, ZPROP_SOURCE, &source) == 0) {
414 if (strcmp(source, zhp->zfs_name) != 0 &&
415 strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
419 * May have no source before SPA_VERSION_RECVD_PROPS,
420 * but is still modifiable.
426 if (zfs_prop_user(propname) ||
427 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
429 value = fnvlist_lookup_string(propnv, ZPROP_VALUE);
430 fnvlist_add_string(nv, propname, value);
433 value = fnvlist_lookup_uint64(propnv, ZPROP_VALUE);
434 fnvlist_add_uint64(nv, propname, value);
440 * returns snapshot guid
441 * and returns 0 if the snapshot does not exist
444 get_snap_guid(libzfs_handle_t *hdl, const char *fs, const char *snap)
446 char name[MAXPATHLEN + 1];
449 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
452 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
453 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
455 guid = zfs_prop_get_int(zhp, ZFS_PROP_GUID);
463 * returns snapshot creation txg
464 * and returns 0 if the snapshot does not exist
467 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
469 char name[ZFS_MAX_DATASET_NAME_LEN];
472 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
475 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
476 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
477 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
479 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
488 * Recursively generate nvlists describing datasets. See comment
489 * for the data structure send_data_t above for description of contents
493 send_iterate_fs(zfs_handle_t *zhp, void *arg)
495 send_data_t *sd = arg;
496 nvlist_t *nvfs = NULL, *nv = NULL;
498 uint64_t min_txg = 0, max_txg = 0;
499 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
500 uint64_t guid = zhp->zfs_dmustats.dds_guid;
501 uint64_t fromsnap_txg, tosnap_txg;
504 /* These fields are restored on return from a recursive call. */
505 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
506 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
507 uint64_t tosnap_txg_save = sd->tosnap_txg;
509 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
510 if (fromsnap_txg != 0)
511 sd->fromsnap_txg = fromsnap_txg;
513 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
515 sd->tosnap_txg = tosnap_txg;
518 * On the send side, if the current dataset does not have tosnap,
519 * perform two additional checks:
521 * - Skip sending the current dataset if it was created later than
523 * - Return error if the current dataset was created earlier than
524 * the parent tosnap, unless --skip-missing specified. Then
525 * just print a warning.
527 if (sd->tosnap != NULL && tosnap_txg == 0) {
528 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
530 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
531 "skipping dataset %s: snapshot %s does "
532 "not exist\n"), zhp->zfs_name, sd->tosnap);
534 } else if (sd->skipmissing) {
535 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
536 "WARNING: skipping dataset %s and its children:"
537 " snapshot %s does not exist\n"),
538 zhp->zfs_name, sd->tosnap);
540 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
541 "cannot send %s@%s%s: snapshot %s@%s does not "
542 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
543 dgettext(TEXT_DOMAIN, " recursively") : "",
544 zhp->zfs_name, sd->tosnap);
550 nvfs = fnvlist_alloc();
551 fnvlist_add_string(nvfs, "name", zhp->zfs_name);
552 fnvlist_add_uint64(nvfs, "parentfromsnap", sd->parent_fromsnap_guid);
554 if (zhp->zfs_dmustats.dds_origin[0] != '\0') {
555 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
556 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
557 if (origin == NULL) {
561 fnvlist_add_uint64(nvfs, "origin",
562 origin->zfs_dmustats.dds_guid);
566 /* Iterate over props. */
567 if (sd->props || sd->backup || sd->recursive) {
568 nv = fnvlist_alloc();
569 send_iterate_prop(zhp, sd->backup, nv);
570 fnvlist_add_nvlist(nvfs, "props", nv);
572 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
575 /* Determine if this dataset is an encryption root. */
576 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
582 fnvlist_add_boolean(nvfs, "is_encroot");
585 * Encrypted datasets can only be sent with properties if
586 * the raw flag is specified because the receive side doesn't
587 * currently have a mechanism for recursively asking the user
588 * for new encryption parameters.
591 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
592 "cannot send %s@%s: encrypted dataset %s may not "
593 "be sent with properties without the raw flag\n"),
594 sd->fsname, sd->tosnap, zhp->zfs_name);
602 * Iterate over snaps, and set sd->parent_fromsnap_guid.
604 * If this is a "doall" send, a replicate send or we're just trying
605 * to gather a list of previous snapshots, iterate through all the
606 * snaps in the txg range. Otherwise just look at the one we're
609 sd->parent_fromsnap_guid = 0;
610 sd->parent_snaps = fnvlist_alloc();
611 sd->snapprops = fnvlist_alloc();
613 sd->snapholds = fnvlist_alloc();
614 if (sd->doall || sd->replicate || sd->tosnap == NULL) {
615 if (!sd->replicate && fromsnap_txg != 0)
616 min_txg = fromsnap_txg;
617 if (!sd->replicate && tosnap_txg != 0)
618 max_txg = tosnap_txg;
619 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd,
622 char snapname[MAXPATHLEN];
625 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
626 zhp->zfs_name, sd->tosnap);
627 if (sd->fromsnap != NULL)
628 sd->seenfrom = B_TRUE;
629 snap = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
631 (void) send_iterate_snap(snap, sd);
634 fnvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps);
635 fnvlist_free(sd->parent_snaps);
636 fnvlist_add_nvlist(nvfs, "snapprops", sd->snapprops);
637 fnvlist_free(sd->snapprops);
639 fnvlist_add_nvlist(nvfs, "snapholds", sd->snapholds);
640 fnvlist_free(sd->snapholds);
643 /* Do not allow the size of the properties list to exceed the limit */
644 if ((fnvlist_size(nvfs) + fnvlist_size(sd->fss)) >
645 zhp->zfs_hdl->libzfs_max_nvlist) {
646 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
647 "warning: cannot send %s@%s: the size of the list of "
648 "snapshots and properties is too large to be received "
650 "Select a smaller number of snapshots to send.\n"),
651 zhp->zfs_name, sd->tosnap);
655 /* Add this fs to nvlist. */
656 (void) snprintf(guidstring, sizeof (guidstring),
657 "0x%llx", (longlong_t)guid);
658 fnvlist_add_nvlist(sd->fss, guidstring, nvfs);
660 /* Iterate over children. */
662 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
665 /* Restore saved fields. */
666 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
667 sd->fromsnap_txg = fromsnap_txg_save;
668 sd->tosnap_txg = tosnap_txg_save;
678 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
679 const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t doall,
680 boolean_t replicate, boolean_t skipmissing, boolean_t verbose,
681 boolean_t backup, boolean_t holds, boolean_t props, nvlist_t **nvlp,
685 send_data_t sd = { 0 };
688 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
690 return (EZFS_BADTYPE);
692 sd.fss = fnvlist_alloc();
694 sd.fromsnap = fromsnap;
696 sd.recursive = recursive;
699 sd.replicate = replicate;
700 sd.skipmissing = skipmissing;
701 sd.verbose = verbose;
706 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
707 fnvlist_free(sd.fss);
714 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
715 fnvlist_free(sd.fss);
725 * Routines specific to "zfs send"
727 typedef struct send_dump_data {
728 /* these are all just the short snapname (the part after the @) */
729 const char *fromsnap;
731 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
732 uint64_t prevsnap_obj;
733 boolean_t seenfrom, seento, replicate, doall, fromorigin;
734 boolean_t dryrun, parsable, progress, embed_data, std_out;
735 boolean_t large_block, compress, raw, holds;
741 snapfilter_cb_t *filter_cb;
744 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
751 zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
752 enum lzc_send_flags flags, uint64_t *spacep)
754 assert(snapname != NULL);
756 int error = lzc_send_space(snapname, from, flags, spacep);
760 char errbuf[ERRBUFLEN];
761 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
762 "warning: cannot estimate space for '%s'"), snapname);
764 libzfs_handle_t *hdl = zhp->zfs_hdl;
767 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
768 "not an earlier snapshot from the same fs"));
769 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
772 if (zfs_dataset_exists(hdl, snapname,
773 ZFS_TYPE_SNAPSHOT)) {
774 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
775 "incremental source (%s) does not exist"),
778 return (zfs_error(hdl, EZFS_NOENT, errbuf));
792 zfs_error_aux(hdl, "%s", strerror(error));
793 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
796 return (zfs_standard_error(hdl, error, errbuf));
801 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
802 * NULL) to the file descriptor specified by outfd.
805 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
806 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
809 zfs_cmd_t zc = {"\0"};
810 libzfs_handle_t *hdl = zhp->zfs_hdl;
813 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
814 assert(fromsnap_obj == 0 || !fromorigin);
816 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
817 zc.zc_cookie = outfd;
818 zc.zc_obj = fromorigin;
819 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
820 zc.zc_fromobj = fromsnap_obj;
823 if (debugnv != NULL) {
824 thisdbg = fnvlist_alloc();
825 if (fromsnap != NULL && fromsnap[0] != '\0')
826 fnvlist_add_string(thisdbg, "fromsnap", fromsnap);
829 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
830 char errbuf[ERRBUFLEN];
833 (void) snprintf(errbuf, sizeof (errbuf), "%s '%s'",
834 dgettext(TEXT_DOMAIN, "warning: cannot send"),
837 if (debugnv != NULL) {
838 fnvlist_add_uint64(thisdbg, "error", error);
839 fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
840 fnvlist_free(thisdbg);
845 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
846 "not an earlier snapshot from the same fs"));
847 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
850 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
851 "source key must be loaded"));
852 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
855 if (zfs_dataset_exists(hdl, zc.zc_name,
856 ZFS_TYPE_SNAPSHOT)) {
857 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
858 "incremental source (@%s) does not exist"),
861 return (zfs_error(hdl, EZFS_NOENT, errbuf));
875 zfs_error_aux(hdl, "%s", strerror(errno));
876 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
879 return (zfs_standard_error(hdl, errno, errbuf));
883 if (debugnv != NULL) {
884 fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
885 fnvlist_free(thisdbg);
892 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
894 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
897 * zfs_send() only sets snapholds for sends that need them,
898 * e.g. replication and doall.
900 if (sdd->snapholds == NULL)
903 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
907 zfs_send_progress(zfs_handle_t *zhp, int fd, uint64_t *bytes_written,
908 uint64_t *blocks_visited)
910 zfs_cmd_t zc = {"\0"};
912 if (bytes_written != NULL)
914 if (blocks_visited != NULL)
916 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
918 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
920 if (bytes_written != NULL)
921 *bytes_written = zc.zc_cookie;
922 if (blocks_visited != NULL)
923 *blocks_visited = zc.zc_objset_type;
928 send_progress_thread(void *arg)
930 progress_arg_t *pa = arg;
931 zfs_handle_t *zhp = pa->pa_zhp;
939 if (!pa->pa_parsable) {
940 (void) fprintf(stderr,
941 "TIME %s %sSNAPSHOT %s\n",
942 pa->pa_estimate ? "BYTES" : " SENT",
943 pa->pa_verbosity >= 2 ? " BLOCKS " : "",
948 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
952 if ((err = zfs_send_progress(zhp, pa->pa_fd, &bytes,
954 if (err == EINTR || err == ENOENT)
956 return ((void *)(uintptr_t)err);
960 localtime_r(&t, &tm);
962 if (pa->pa_verbosity >= 2 && pa->pa_parsable) {
963 (void) fprintf(stderr,
964 "%02d:%02d:%02d\t%llu\t%llu\t%s\n",
965 tm.tm_hour, tm.tm_min, tm.tm_sec,
966 (u_longlong_t)bytes, (u_longlong_t)blocks,
968 } else if (pa->pa_verbosity >= 2) {
969 zfs_nicenum(bytes, buf, sizeof (buf));
970 (void) fprintf(stderr,
971 "%02d:%02d:%02d %5s %8llu %s\n",
972 tm.tm_hour, tm.tm_min, tm.tm_sec,
973 buf, (u_longlong_t)blocks, zhp->zfs_name);
974 } else if (pa->pa_parsable) {
975 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
976 tm.tm_hour, tm.tm_min, tm.tm_sec,
977 (u_longlong_t)bytes, zhp->zfs_name);
979 zfs_nicebytes(bytes, buf, sizeof (buf));
980 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
981 tm.tm_hour, tm.tm_min, tm.tm_sec,
988 send_progress_thread_exit(libzfs_handle_t *hdl, pthread_t ptid)
991 (void) pthread_cancel(ptid);
992 (void) pthread_join(ptid, &status);
993 int error = (int)(uintptr_t)status;
994 if (error != 0 && status != PTHREAD_CANCELED)
995 return (zfs_standard_error(hdl, error,
996 dgettext(TEXT_DOMAIN, "progress thread exited nonzero")));
1002 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1003 uint64_t size, boolean_t parsable)
1006 if (fromsnap != NULL) {
1007 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1008 "incremental\t%s\t%s"), fromsnap, tosnap);
1011 * Workaround for GCC 12+ with UBSan enabled deficencies.
1013 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1014 * below as violating -Wformat-overflow.
1016 #if defined(__GNUC__) && !defined(__clang__) && \
1017 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1018 #pragma GCC diagnostic push
1019 #pragma GCC diagnostic ignored "-Wformat-overflow"
1021 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1022 "full\t%s"), tosnap);
1023 #if defined(__GNUC__) && !defined(__clang__) && \
1024 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1025 #pragma GCC diagnostic pop
1028 (void) fprintf(fout, "\t%llu", (longlong_t)size);
1030 if (fromsnap != NULL) {
1031 if (strchr(fromsnap, '@') == NULL &&
1032 strchr(fromsnap, '#') == NULL) {
1033 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1034 "send from @%s to %s"), fromsnap, tosnap);
1036 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1037 "send from %s to %s"), fromsnap, tosnap);
1040 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1041 "full send of %s"), tosnap);
1045 zfs_nicebytes(size, buf, sizeof (buf));
1047 * Workaround for GCC 12+ with UBSan enabled deficencies.
1049 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1050 * below as violating -Wformat-overflow.
1052 #if defined(__GNUC__) && !defined(__clang__) && \
1053 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1054 #pragma GCC diagnostic push
1055 #pragma GCC diagnostic ignored "-Wformat-overflow"
1057 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1058 " estimated size is %s"), buf);
1059 #if defined(__GNUC__) && !defined(__clang__) && \
1060 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1061 #pragma GCC diagnostic pop
1065 (void) fprintf(fout, "\n");
1069 * Send a single filesystem snapshot, updating the send dump data.
1070 * This interface is intended for use as a zfs_iter_snapshots_sorted visitor.
1073 dump_snapshot(zfs_handle_t *zhp, void *arg)
1075 send_dump_data_t *sdd = arg;
1076 progress_arg_t pa = { 0 };
1079 enum lzc_send_flags flags = 0;
1081 boolean_t isfromsnap, istosnap, fromorigin;
1082 boolean_t exclude = B_FALSE;
1083 FILE *fout = sdd->std_out ? stdout : stderr;
1086 thissnap = strchr(zhp->zfs_name, '@') + 1;
1087 isfromsnap = (sdd->fromsnap != NULL &&
1088 strcmp(sdd->fromsnap, thissnap) == 0);
1090 if (!sdd->seenfrom && isfromsnap) {
1091 gather_holds(zhp, sdd);
1092 sdd->seenfrom = B_TRUE;
1093 (void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap));
1094 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1099 if (sdd->seento || !sdd->seenfrom) {
1104 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1106 sdd->seento = B_TRUE;
1108 if (sdd->large_block)
1109 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1110 if (sdd->embed_data)
1111 flags |= LZC_SEND_FLAG_EMBED_DATA;
1113 flags |= LZC_SEND_FLAG_COMPRESS;
1115 flags |= LZC_SEND_FLAG_RAW;
1117 if (!sdd->doall && !isfromsnap && !istosnap) {
1118 if (sdd->replicate) {
1120 nvlist_t *snapprops;
1122 * Filter out all intermediate snapshots except origin
1123 * snapshots needed to replicate clones.
1125 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1126 zhp->zfs_dmustats.dds_guid, &snapname);
1129 snapprops = fnvlist_lookup_nvlist(nvfs,
1131 snapprops = fnvlist_lookup_nvlist(snapprops,
1133 exclude = !nvlist_exists(snapprops,
1142 * If a filter function exists, call it to determine whether
1143 * this snapshot will be sent.
1145 if (exclude || (sdd->filter_cb != NULL &&
1146 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1148 * This snapshot is filtered out. Don't send it, and don't
1149 * set prevsnap_obj, so it will be as if this snapshot didn't
1150 * exist, and the next accepted snapshot will be sent as
1151 * an incremental from the last accepted one, or as the
1152 * first (and full) snapshot in the case of a replication,
1153 * non-incremental send.
1159 gather_holds(zhp, sdd);
1160 fromorigin = sdd->prevsnap[0] == '\0' &&
1161 (sdd->fromorigin || sdd->replicate);
1163 if (sdd->verbosity != 0) {
1165 char fromds[ZFS_MAX_DATASET_NAME_LEN];
1167 if (sdd->prevsnap[0] != '\0') {
1168 (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1169 *(strchr(fromds, '@') + 1) = '\0';
1170 (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1172 if (zfs_send_space(zhp, zhp->zfs_name,
1173 sdd->prevsnap[0] ? fromds : NULL, flags, &size) == 0) {
1174 send_print_verbose(fout, zhp->zfs_name,
1175 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1176 size, sdd->parsable);
1183 * If progress reporting is requested, spawn a new thread to
1184 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1186 if (sdd->progress) {
1188 pa.pa_fd = sdd->outfd;
1189 pa.pa_parsable = sdd->parsable;
1190 pa.pa_estimate = B_FALSE;
1191 pa.pa_verbosity = sdd->verbosity;
1193 if ((err = pthread_create(&tid, NULL,
1194 send_progress_thread, &pa)) != 0) {
1200 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1201 fromorigin, sdd->outfd, flags, sdd->debugnv);
1203 if (sdd->progress &&
1204 send_progress_thread_exit(zhp->zfs_hdl, tid))
1208 (void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap));
1209 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1215 * Send all snapshots for a filesystem, updating the send dump data.
1218 dump_filesystem(zfs_handle_t *zhp, send_dump_data_t *sdd)
1221 boolean_t missingfrom = B_FALSE;
1222 zfs_cmd_t zc = {"\0"};
1223 uint64_t min_txg = 0, max_txg = 0;
1226 * Make sure the tosnap exists.
1228 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1229 zhp->zfs_name, sdd->tosnap);
1230 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1231 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1232 "WARNING: could not send %s@%s: does not exist\n"),
1233 zhp->zfs_name, sdd->tosnap);
1239 * If this fs does not have fromsnap, and we're doing
1240 * recursive, we need to send a full stream from the
1241 * beginning (or an incremental from the origin if this
1242 * is a clone). If we're doing non-recursive, then let
1243 * them get the error.
1245 if (sdd->replicate && sdd->fromsnap) {
1247 * Make sure the fromsnap exists.
1249 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1250 zhp->zfs_name, sdd->fromsnap);
1251 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0)
1252 missingfrom = B_TRUE;
1255 sdd->seenfrom = sdd->seento = B_FALSE;
1256 sdd->prevsnap[0] = '\0';
1257 sdd->prevsnap_obj = 0;
1258 if (sdd->fromsnap == NULL || missingfrom)
1259 sdd->seenfrom = B_TRUE;
1262 * Iterate through all snapshots and process the ones we will be
1263 * sending. If we only have a "from" and "to" snapshot to deal
1264 * with, we can avoid iterating through all the other snapshots.
1266 if (sdd->doall || sdd->replicate || sdd->tosnap == NULL) {
1267 if (!sdd->replicate) {
1268 if (sdd->fromsnap != NULL) {
1269 min_txg = get_snap_txg(zhp->zfs_hdl,
1270 zhp->zfs_name, sdd->fromsnap);
1272 if (sdd->tosnap != NULL) {
1273 max_txg = get_snap_txg(zhp->zfs_hdl,
1274 zhp->zfs_name, sdd->tosnap);
1277 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, sdd,
1280 char snapname[MAXPATHLEN] = { 0 };
1283 /* Dump fromsnap. */
1284 if (!sdd->seenfrom) {
1285 (void) snprintf(snapname, sizeof (snapname),
1286 "%s@%s", zhp->zfs_name, sdd->fromsnap);
1287 snap = zfs_open(zhp->zfs_hdl, snapname,
1290 rv = dump_snapshot(snap, sdd);
1297 (void) snprintf(snapname, sizeof (snapname),
1298 "%s@%s", zhp->zfs_name, sdd->tosnap);
1299 snap = zfs_open(zhp->zfs_hdl, snapname,
1302 rv = dump_snapshot(snap, sdd);
1308 if (!sdd->seenfrom) {
1309 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1310 "WARNING: could not send %s@%s:\n"
1311 "incremental source (%s@%s) does not exist\n"),
1312 zhp->zfs_name, sdd->tosnap,
1313 zhp->zfs_name, sdd->fromsnap);
1315 } else if (!sdd->seento) {
1316 if (sdd->fromsnap) {
1317 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1318 "WARNING: could not send %s@%s:\n"
1319 "incremental source (%s@%s) "
1320 "is not earlier than it\n"),
1321 zhp->zfs_name, sdd->tosnap,
1322 zhp->zfs_name, sdd->fromsnap);
1324 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1326 "could not send %s@%s: does not exist\n"),
1327 zhp->zfs_name, sdd->tosnap);
1336 * Send all snapshots for all filesystems in sdd.
1339 dump_filesystems(zfs_handle_t *rzhp, send_dump_data_t *sdd)
1342 boolean_t needagain, progress;
1344 if (!sdd->replicate)
1345 return (dump_filesystem(rzhp, sdd));
1347 /* Mark the clone origin snapshots. */
1348 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1349 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1351 uint64_t origin_guid = 0;
1353 nvfs = fnvpair_value_nvlist(fspair);
1354 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1355 if (origin_guid != 0) {
1357 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1358 origin_guid, &snapname);
1359 if (origin_nv != NULL) {
1360 nvlist_t *snapprops;
1361 snapprops = fnvlist_lookup_nvlist(origin_nv,
1363 snapprops = fnvlist_lookup_nvlist(snapprops,
1365 fnvlist_add_boolean(snapprops,
1371 needagain = progress = B_FALSE;
1372 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1373 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1374 nvlist_t *fslist, *parent_nv;
1378 uint64_t origin_guid = 0;
1379 uint64_t parent_guid = 0;
1381 fslist = fnvpair_value_nvlist(fspair);
1382 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1385 fsname = fnvlist_lookup_string(fslist, "name");
1386 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1387 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1390 if (parent_guid != 0) {
1391 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1392 if (!nvlist_exists(parent_nv, "sent")) {
1393 /* Parent has not been sent; skip this one. */
1399 if (origin_guid != 0) {
1400 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1402 if (origin_nv != NULL &&
1403 !nvlist_exists(origin_nv, "sent")) {
1405 * Origin has not been sent yet;
1413 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1416 err = dump_filesystem(zhp, sdd);
1417 fnvlist_add_boolean(fslist, "sent");
1428 /* Clean out the sent flags in case we reuse this fss. */
1429 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1430 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1433 fslist = fnvpair_value_nvlist(fspair);
1434 (void) nvlist_remove_all(fslist, "sent");
1441 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1443 unsigned int version;
1445 unsigned long long checksum, packed_len;
1448 * Decode token header, which is:
1449 * <token version>-<checksum of payload>-<uncompressed payload length>
1450 * Note that the only supported token version is 1.
1452 nread = sscanf(token, "%u-%llx-%llx-",
1453 &version, &checksum, &packed_len);
1455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1456 "resume token is corrupt (invalid format)"));
1460 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1461 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1462 "resume token is corrupt (invalid version %u)"),
1467 /* Convert hexadecimal representation to binary. */
1468 token = strrchr(token, '-') + 1;
1469 int len = strlen(token) / 2;
1470 unsigned char *compressed = zfs_alloc(hdl, len);
1471 for (i = 0; i < len; i++) {
1472 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1475 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1476 "resume token is corrupt "
1477 "(payload is not hex-encoded)"));
1482 /* Verify checksum. */
1484 fletcher_4_native_varsize(compressed, len, &cksum);
1485 if (cksum.zc_word[0] != checksum) {
1487 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1488 "resume token is corrupt (incorrect checksum)"));
1493 void *packed = zfs_alloc(hdl, packed_len);
1494 uLongf packed_len_long = packed_len;
1495 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1496 packed_len_long != packed_len) {
1499 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1500 "resume token is corrupt (decompression failed)"));
1504 /* Unpack nvlist. */
1506 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1510 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1511 "resume token is corrupt (nvlist_unpack failed)"));
1517 static enum lzc_send_flags
1518 lzc_flags_from_sendflags(const sendflags_t *flags)
1520 enum lzc_send_flags lzc_flags = 0;
1522 if (flags->largeblock)
1523 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1524 if (flags->embed_data)
1525 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1526 if (flags->compress)
1527 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1529 lzc_flags |= LZC_SEND_FLAG_RAW;
1531 lzc_flags |= LZC_SEND_FLAG_SAVED;
1537 estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
1538 uint64_t resumeobj, uint64_t resumeoff, uint64_t bytes,
1539 const char *redactbook, char *errbuf)
1542 FILE *fout = flags->dryrun ? stdout : stderr;
1543 progress_arg_t pa = { 0 };
1547 if (flags->progress) {
1550 pa.pa_parsable = flags->parsable;
1551 pa.pa_estimate = B_TRUE;
1552 pa.pa_verbosity = flags->verbosity;
1554 err = pthread_create(&ptid, NULL,
1555 send_progress_thread, &pa);
1557 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
1558 return (zfs_error(zhp->zfs_hdl,
1559 EZFS_THREADCREATEFAILED, errbuf));
1563 err = lzc_send_space_resume_redacted(zhp->zfs_name, from,
1564 lzc_flags_from_sendflags(flags), resumeobj, resumeoff, bytes,
1565 redactbook, fd, &size);
1567 if (flags->progress && send_progress_thread_exit(zhp->zfs_hdl, ptid))
1571 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
1572 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
1575 send_print_verbose(fout, zhp->zfs_name, from, size,
1578 if (flags->parsable) {
1579 (void) fprintf(fout, "size\t%llu\n", (longlong_t)size);
1582 zfs_nicenum(size, buf, sizeof (buf));
1583 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1584 "total estimated size is %s\n"), buf);
1590 redact_snaps_contains(const uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
1592 for (int i = 0; i < num_snaps; i++) {
1593 if (snaps[i] == guid)
1600 redact_snaps_equal(const uint64_t *snaps1, uint64_t num_snaps1,
1601 const uint64_t *snaps2, uint64_t num_snaps2)
1603 if (num_snaps1 != num_snaps2)
1605 for (int i = 0; i < num_snaps1; i++) {
1606 if (!redact_snaps_contains(snaps2, num_snaps2, snaps1[i]))
1613 get_bookmarks(const char *path, nvlist_t **bmarksp)
1615 nvlist_t *props = fnvlist_alloc();
1618 fnvlist_add_boolean(props, "redact_complete");
1619 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1620 error = lzc_get_bookmarks(path, props, bmarksp);
1621 fnvlist_free(props);
1626 find_redact_pair(nvlist_t *bmarks, const uint64_t *redact_snap_guids,
1627 int num_redact_snaps)
1631 for (pair = nvlist_next_nvpair(bmarks, NULL); pair;
1632 pair = nvlist_next_nvpair(bmarks, pair)) {
1634 nvlist_t *bmark = fnvpair_value_nvlist(pair);
1635 nvlist_t *vallist = fnvlist_lookup_nvlist(bmark,
1636 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1638 uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist,
1640 if (redact_snaps_equal(redact_snap_guids,
1641 num_redact_snaps, bmarksnaps, len)) {
1649 get_redact_complete(nvpair_t *pair)
1651 nvlist_t *bmark = fnvpair_value_nvlist(pair);
1652 nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete");
1653 boolean_t complete = fnvlist_lookup_boolean_value(vallist,
1660 * Check that the list of redaction snapshots in the bookmark matches the send
1661 * we're resuming, and return whether or not it's complete.
1663 * Note that the caller needs to free the contents of *bookname with free() if
1664 * this function returns successfully.
1667 find_redact_book(libzfs_handle_t *hdl, const char *path,
1668 const uint64_t *redact_snap_guids, int num_redact_snaps,
1671 char errbuf[ERRBUFLEN];
1674 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1675 "cannot resume send"));
1677 int error = get_bookmarks(path, &bmarks);
1679 if (error == ESRCH) {
1680 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1681 "nonexistent redaction bookmark provided"));
1682 } else if (error == ENOENT) {
1683 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1684 "dataset to be sent no longer exists"));
1686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1687 "unknown error: %s"), strerror(error));
1689 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1691 nvpair_t *pair = find_redact_pair(bmarks, redact_snap_guids,
1694 fnvlist_free(bmarks);
1695 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1696 "no appropriate redaction bookmark exists"));
1697 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1699 boolean_t complete = get_redact_complete(pair);
1701 fnvlist_free(bmarks);
1702 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1703 "incomplete redaction bookmark provided"));
1704 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1706 *bookname = strndup(nvpair_name(pair), ZFS_MAX_DATASET_NAME_LEN);
1707 ASSERT3P(*bookname, !=, NULL);
1708 fnvlist_free(bmarks);
1712 static enum lzc_send_flags
1713 lzc_flags_from_resume_nvl(nvlist_t *resume_nvl)
1715 enum lzc_send_flags lzc_flags = 0;
1717 if (nvlist_exists(resume_nvl, "largeblockok"))
1718 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1719 if (nvlist_exists(resume_nvl, "embedok"))
1720 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1721 if (nvlist_exists(resume_nvl, "compressok"))
1722 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1723 if (nvlist_exists(resume_nvl, "rawok"))
1724 lzc_flags |= LZC_SEND_FLAG_RAW;
1725 if (nvlist_exists(resume_nvl, "savedok"))
1726 lzc_flags |= LZC_SEND_FLAG_SAVED;
1732 zfs_send_resume_impl_cb_impl(libzfs_handle_t *hdl, sendflags_t *flags,
1733 int outfd, nvlist_t *resume_nvl)
1735 char errbuf[ERRBUFLEN];
1737 char *fromname = NULL;
1738 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1741 char name[ZFS_MAX_DATASET_NAME_LEN];
1742 FILE *fout = (flags->verbosity > 0 && flags->dryrun) ? stdout : stderr;
1743 uint64_t *redact_snap_guids = NULL;
1744 int num_redact_snaps = 0;
1745 char *redact_book = NULL;
1747 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1748 "cannot resume send"));
1750 if (flags->verbosity != 0) {
1751 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1752 "resume token contents:\n"));
1753 nvlist_print(fout, resume_nvl);
1756 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1757 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1758 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1759 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1760 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1762 "resume token is corrupt"));
1763 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1766 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1769 (void) strlcpy(name, toname, sizeof (name));
1771 error = guid_to_name(hdl, toname, toguid, B_FALSE, name);
1773 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1774 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1775 "'%s' is no longer the same snapshot "
1776 "used in the initial send"), toname);
1778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1779 "'%s' used in the initial send no "
1780 "longer exists"), toname);
1782 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1786 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1788 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1789 "unable to access '%s'"), name);
1790 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1793 if (nvlist_lookup_uint64_array(resume_nvl, "book_redact_snaps",
1794 &redact_snap_guids, (uint_t *)&num_redact_snaps) != 0) {
1795 num_redact_snaps = -1;
1798 if (fromguid != 0) {
1799 if (guid_to_name_redact_snaps(hdl, toname, fromguid, B_TRUE,
1800 redact_snap_guids, num_redact_snaps, name) != 0) {
1801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1802 "incremental source %#llx no longer exists"),
1803 (longlong_t)fromguid);
1804 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1809 redact_snap_guids = NULL;
1811 if (nvlist_lookup_uint64_array(resume_nvl,
1812 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &redact_snap_guids,
1813 (uint_t *)&num_redact_snaps) == 0) {
1814 char path[ZFS_MAX_DATASET_NAME_LEN];
1816 (void) strlcpy(path, toname, sizeof (path));
1817 char *at = strchr(path, '@');
1818 ASSERT3P(at, !=, NULL);
1822 if ((error = find_redact_book(hdl, path, redact_snap_guids,
1823 num_redact_snaps, &redact_book)) != 0) {
1828 enum lzc_send_flags lzc_flags = lzc_flags_from_sendflags(flags) |
1829 lzc_flags_from_resume_nvl(resume_nvl);
1831 if (flags->verbosity != 0) {
1833 * Some of these may have come from the resume token, set them
1834 * here for size estimate purposes.
1836 sendflags_t tmpflags = *flags;
1837 if (lzc_flags & LZC_SEND_FLAG_LARGE_BLOCK)
1838 tmpflags.largeblock = B_TRUE;
1839 if (lzc_flags & LZC_SEND_FLAG_COMPRESS)
1840 tmpflags.compress = B_TRUE;
1841 if (lzc_flags & LZC_SEND_FLAG_EMBED_DATA)
1842 tmpflags.embed_data = B_TRUE;
1843 if (lzc_flags & LZC_SEND_FLAG_RAW)
1844 tmpflags.raw = B_TRUE;
1845 if (lzc_flags & LZC_SEND_FLAG_SAVED)
1846 tmpflags.saved = B_TRUE;
1847 error = estimate_size(zhp, fromname, outfd, &tmpflags,
1848 resumeobj, resumeoff, bytes, redact_book, errbuf);
1851 if (!flags->dryrun) {
1852 progress_arg_t pa = { 0 };
1855 * If progress reporting is requested, spawn a new thread to
1856 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1858 if (flags->progress) {
1861 pa.pa_parsable = flags->parsable;
1862 pa.pa_estimate = B_FALSE;
1863 pa.pa_verbosity = flags->verbosity;
1865 error = pthread_create(&tid, NULL,
1866 send_progress_thread, &pa);
1868 if (redact_book != NULL)
1875 error = lzc_send_resume_redacted(zhp->zfs_name, fromname, outfd,
1876 lzc_flags, resumeobj, resumeoff, redact_book);
1877 if (redact_book != NULL)
1880 if (flags->progress && send_progress_thread_exit(hdl, tid))
1883 char errbuf[ERRBUFLEN];
1884 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1885 "warning: cannot send '%s'"), zhp->zfs_name);
1893 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1894 "source key must be loaded"));
1895 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1897 if (lzc_exists(zhp->zfs_name)) {
1898 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1899 "incremental source could not be found"));
1901 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1916 zfs_error_aux(hdl, "%s", strerror(errno));
1917 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1920 return (zfs_standard_error(hdl, errno, errbuf));
1923 if (redact_book != NULL)
1932 struct zfs_send_resume_impl {
1933 libzfs_handle_t *hdl;
1935 nvlist_t *resume_nvl;
1939 zfs_send_resume_impl_cb(int outfd, void *arg)
1941 struct zfs_send_resume_impl *zsri = arg;
1942 return (zfs_send_resume_impl_cb_impl(zsri->hdl, zsri->flags, outfd,
1947 zfs_send_resume_impl(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1948 nvlist_t *resume_nvl)
1950 struct zfs_send_resume_impl zsri = {
1953 .resume_nvl = resume_nvl,
1955 return (lzc_send_wrapper(zfs_send_resume_impl_cb, outfd, &zsri));
1959 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1960 const char *resume_token)
1963 char errbuf[ERRBUFLEN];
1964 nvlist_t *resume_nvl;
1966 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1967 "cannot resume send"));
1969 resume_nvl = zfs_send_resume_token_to_nvlist(hdl, resume_token);
1970 if (resume_nvl == NULL) {
1972 * zfs_error_aux has already been set by
1973 * zfs_send_resume_token_to_nvlist()
1975 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1978 ret = zfs_send_resume_impl(hdl, flags, outfd, resume_nvl);
1979 fnvlist_free(resume_nvl);
1985 zfs_send_saved(zfs_handle_t *zhp, sendflags_t *flags, int outfd,
1986 const char *resume_token)
1989 libzfs_handle_t *hdl = zhp->zfs_hdl;
1990 nvlist_t *saved_nvl = NULL, *resume_nvl = NULL;
1991 uint64_t saved_guid = 0, resume_guid = 0;
1992 uint64_t obj = 0, off = 0, bytes = 0;
1993 char token_buf[ZFS_MAXPROPLEN];
1994 char errbuf[ERRBUFLEN];
1996 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1997 "saved send failed"));
1999 ret = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
2000 token_buf, sizeof (token_buf), NULL, NULL, 0, B_TRUE);
2004 saved_nvl = zfs_send_resume_token_to_nvlist(hdl, token_buf);
2005 if (saved_nvl == NULL) {
2007 * zfs_error_aux has already been set by
2008 * zfs_send_resume_token_to_nvlist()
2010 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2015 * If a resume token is provided we use the object and offset
2016 * from that instead of the default, which starts from the
2019 if (resume_token != NULL) {
2020 resume_nvl = zfs_send_resume_token_to_nvlist(hdl,
2022 if (resume_nvl == NULL) {
2023 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2027 if (nvlist_lookup_uint64(resume_nvl, "object", &obj) != 0 ||
2028 nvlist_lookup_uint64(resume_nvl, "offset", &off) != 0 ||
2029 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
2030 nvlist_lookup_uint64(resume_nvl, "toguid",
2031 &resume_guid) != 0) {
2032 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2033 "provided resume token is corrupt"));
2034 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2038 if (nvlist_lookup_uint64(saved_nvl, "toguid",
2040 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2041 "dataset's resume token is corrupt"));
2042 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2046 if (resume_guid != saved_guid) {
2047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2048 "provided resume token does not match dataset"));
2049 ret = zfs_error(hdl, EZFS_BADBACKUP, errbuf);
2054 (void) nvlist_remove_all(saved_nvl, "object");
2055 fnvlist_add_uint64(saved_nvl, "object", obj);
2057 (void) nvlist_remove_all(saved_nvl, "offset");
2058 fnvlist_add_uint64(saved_nvl, "offset", off);
2060 (void) nvlist_remove_all(saved_nvl, "bytes");
2061 fnvlist_add_uint64(saved_nvl, "bytes", bytes);
2063 (void) nvlist_remove_all(saved_nvl, "toname");
2064 fnvlist_add_string(saved_nvl, "toname", zhp->zfs_name);
2066 ret = zfs_send_resume_impl(hdl, flags, outfd, saved_nvl);
2069 fnvlist_free(saved_nvl);
2070 fnvlist_free(resume_nvl);
2075 * This function informs the target system that the recursive send is complete.
2076 * The record is also expected in the case of a send -p.
2079 send_conclusion_record(int fd, zio_cksum_t *zc)
2081 dmu_replay_record_t drr = { 0 };
2082 drr.drr_type = DRR_END;
2084 drr.drr_u.drr_end.drr_checksum = *zc;
2085 if (write(fd, &drr, sizeof (drr)) == -1) {
2092 * This function is responsible for sending the records that contain the
2093 * necessary information for the target system's libzfs to be able to set the
2094 * properties of the filesystem being received, or to be able to prepare for
2095 * a recursive receive.
2097 * The "zhp" argument is the handle of the snapshot we are sending
2098 * (the "tosnap"). The "from" argument is the short snapshot name (the part
2099 * after the @) of the incremental source.
2102 send_prelim_records(zfs_handle_t *zhp, const char *from, int fd,
2103 boolean_t gather_props, boolean_t recursive, boolean_t verbose,
2104 boolean_t dryrun, boolean_t raw, boolean_t replicate, boolean_t skipmissing,
2105 boolean_t backup, boolean_t holds, boolean_t props, boolean_t doall,
2106 nvlist_t **fssp, avl_tree_t **fsavlp)
2109 char *packbuf = NULL;
2111 zio_cksum_t zc = { {0} };
2112 int featureflags = 0;
2113 /* name of filesystem/volume that contains snapshot we are sending */
2114 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2115 /* short name of snap we are sending */
2116 const char *tosnap = "";
2118 char errbuf[ERRBUFLEN];
2119 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2120 "warning: cannot send '%s'"), zhp->zfs_name);
2121 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && zfs_prop_get_int(zhp,
2122 ZFS_PROP_VERSION) >= ZPL_VERSION_SA) {
2123 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
2127 featureflags |= DMU_BACKUP_FEATURE_HOLDS;
2129 (void) strlcpy(tofs, zhp->zfs_name, ZFS_MAX_DATASET_NAME_LEN);
2130 char *at = strchr(tofs, '@');
2137 nvlist_t *hdrnv = fnvlist_alloc();
2138 nvlist_t *fss = NULL;
2141 fnvlist_add_string(hdrnv, "fromsnap", from);
2142 fnvlist_add_string(hdrnv, "tosnap", tosnap);
2144 fnvlist_add_boolean(hdrnv, "not_recursive");
2147 fnvlist_add_boolean(hdrnv, "raw");
2150 if (gather_nvlist(zhp->zfs_hdl, tofs,
2151 from, tosnap, recursive, raw, doall, replicate, skipmissing,
2152 verbose, backup, holds, props, &fss, fsavlp) != 0) {
2153 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2157 * Do not allow the size of the properties list to exceed
2160 if ((fnvlist_size(fss) + fnvlist_size(hdrnv)) >
2161 zhp->zfs_hdl->libzfs_max_nvlist) {
2162 (void) snprintf(errbuf, sizeof (errbuf),
2163 dgettext(TEXT_DOMAIN, "warning: cannot send '%s': "
2164 "the size of the list of snapshots and properties "
2165 "is too large to be received successfully.\n"
2166 "Select a smaller number of snapshots to send.\n"),
2168 return (zfs_error(zhp->zfs_hdl, EZFS_NOSPC,
2171 fnvlist_add_nvlist(hdrnv, "fss", fss);
2172 VERIFY0(nvlist_pack(hdrnv, &packbuf, &buflen, NV_ENCODE_XDR,
2179 fnvlist_free(hdrnv);
2183 dmu_replay_record_t drr = { 0 };
2184 /* write first begin record */
2185 drr.drr_type = DRR_BEGIN;
2186 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
2187 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
2188 drr_versioninfo, DMU_COMPOUNDSTREAM);
2189 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
2190 drr_versioninfo, featureflags);
2191 if (snprintf(drr.drr_u.drr_begin.drr_toname,
2192 sizeof (drr.drr_u.drr_begin.drr_toname), "%s@%s", tofs,
2193 tosnap) >= sizeof (drr.drr_u.drr_begin.drr_toname)) {
2194 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2197 drr.drr_payloadlen = buflen;
2199 err = dump_record(&drr, packbuf, buflen, &zc, fd);
2202 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
2203 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2206 err = send_conclusion_record(fd, &zc);
2208 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
2209 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2217 * Generate a send stream. The "zhp" argument is the filesystem/volume
2218 * that contains the snapshot to send. The "fromsnap" argument is the
2219 * short name (the part after the '@') of the snapshot that is the
2220 * incremental source to send from (if non-NULL). The "tosnap" argument
2221 * is the short name of the snapshot to send.
2223 * The content of the send stream is the snapshot identified by
2224 * 'tosnap'. Incremental streams are requested in two ways:
2225 * - from the snapshot identified by "fromsnap" (if non-null) or
2226 * - from the origin of the dataset identified by zhp, which must
2227 * be a clone. In this case, "fromsnap" is null and "fromorigin"
2230 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
2231 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
2232 * if "replicate" is set. If "doall" is set, dump all the intermediate
2233 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
2234 * case too. If "props" is set, send properties.
2236 * Pre-wrapped (cf. lzc_send_wrapper()).
2239 zfs_send_cb_impl(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2240 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2241 void *cb_arg, nvlist_t **debugnvp)
2243 char errbuf[ERRBUFLEN];
2244 send_dump_data_t sdd = { 0 };
2246 nvlist_t *fss = NULL;
2247 avl_tree_t *fsavl = NULL;
2248 static uint64_t holdseq;
2252 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2253 "cannot send '%s'"), zhp->zfs_name);
2255 if (fromsnap && fromsnap[0] == '\0') {
2256 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2257 "zero-length incremental source"));
2258 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
2262 char full_fromsnap_name[ZFS_MAX_DATASET_NAME_LEN];
2263 if (snprintf(full_fromsnap_name, sizeof (full_fromsnap_name),
2264 "%s@%s", zhp->zfs_name, fromsnap) >=
2265 sizeof (full_fromsnap_name)) {
2269 zfs_handle_t *fromsnapn = zfs_open(zhp->zfs_hdl,
2270 full_fromsnap_name, ZFS_TYPE_SNAPSHOT);
2271 if (fromsnapn == NULL) {
2275 zfs_close(fromsnapn);
2278 if (flags->replicate || flags->doall || flags->props ||
2279 flags->holds || flags->backup) {
2280 char full_tosnap_name[ZFS_MAX_DATASET_NAME_LEN];
2281 if (snprintf(full_tosnap_name, sizeof (full_tosnap_name),
2282 "%s@%s", zhp->zfs_name, tosnap) >=
2283 sizeof (full_tosnap_name)) {
2287 zfs_handle_t *tosnap = zfs_open(zhp->zfs_hdl,
2288 full_tosnap_name, ZFS_TYPE_SNAPSHOT);
2289 if (tosnap == NULL) {
2293 err = send_prelim_records(tosnap, fromsnap, outfd,
2294 flags->replicate || flags->props || flags->holds,
2295 flags->replicate, flags->verbosity > 0, flags->dryrun,
2296 flags->raw, flags->replicate, flags->skipmissing,
2297 flags->backup, flags->holds, flags->props, flags->doall,
2304 /* dump each stream */
2305 sdd.fromsnap = fromsnap;
2306 sdd.tosnap = tosnap;
2308 sdd.replicate = flags->replicate;
2309 sdd.doall = flags->doall;
2310 sdd.fromorigin = flags->fromorigin;
2313 sdd.verbosity = flags->verbosity;
2314 sdd.parsable = flags->parsable;
2315 sdd.progress = flags->progress;
2316 sdd.dryrun = flags->dryrun;
2317 sdd.large_block = flags->largeblock;
2318 sdd.embed_data = flags->embed_data;
2319 sdd.compress = flags->compress;
2320 sdd.raw = flags->raw;
2321 sdd.holds = flags->holds;
2322 sdd.filter_cb = filter_func;
2323 sdd.filter_cb_arg = cb_arg;
2325 sdd.debugnv = *debugnvp;
2326 if (sdd.verbosity != 0 && sdd.dryrun)
2327 sdd.std_out = B_TRUE;
2328 fout = sdd.std_out ? stdout : stderr;
2331 * Some flags require that we place user holds on the datasets that are
2332 * being sent so they don't get destroyed during the send. We can skip
2333 * this step if the pool is imported read-only since the datasets cannot
2336 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2337 ZPOOL_PROP_READONLY, NULL) &&
2338 zfs_spa_version(zhp, &spa_version) == 0 &&
2339 spa_version >= SPA_VERSION_USERREFS &&
2340 (flags->doall || flags->replicate)) {
2342 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2343 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2344 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
2345 if (sdd.cleanup_fd < 0) {
2349 sdd.snapholds = fnvlist_alloc();
2351 sdd.cleanup_fd = -1;
2352 sdd.snapholds = NULL;
2355 if (flags->verbosity != 0 || sdd.snapholds != NULL) {
2357 * Do a verbose no-op dry run to get all the verbose output
2358 * or to gather snapshot hold's before generating any data,
2359 * then do a non-verbose real run to generate the streams.
2361 sdd.dryrun = B_TRUE;
2362 err = dump_filesystems(zhp, &sdd);
2367 if (flags->verbosity != 0) {
2368 if (flags->parsable) {
2369 (void) fprintf(fout, "size\t%llu\n",
2370 (longlong_t)sdd.size);
2373 zfs_nicebytes(sdd.size, buf, sizeof (buf));
2374 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
2375 "total estimated size is %s\n"), buf);
2379 /* Ensure no snaps found is treated as an error. */
2385 /* Skip the second run if dryrun was requested. */
2389 if (sdd.snapholds != NULL) {
2390 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2394 fnvlist_free(sdd.snapholds);
2395 sdd.snapholds = NULL;
2398 sdd.dryrun = B_FALSE;
2402 err = dump_filesystems(zhp, &sdd);
2403 fsavl_destroy(fsavl);
2406 /* Ensure no snaps found is treated as an error. */
2407 if (err == 0 && !sdd.seento)
2410 if (sdd.cleanup_fd != -1) {
2411 VERIFY(0 == close(sdd.cleanup_fd));
2412 sdd.cleanup_fd = -1;
2415 if (!flags->dryrun && (flags->replicate || flags->doall ||
2416 flags->props || flags->backup || flags->holds)) {
2418 * write final end record. NB: want to do this even if
2419 * there was some error, because it might not be totally
2422 int err2 = send_conclusion_record(outfd, NULL);
2424 return (zfs_standard_error(zhp->zfs_hdl, err2, errbuf));
2427 return (err || sdd.err);
2430 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2432 fsavl_destroy(fsavl);
2434 fnvlist_free(sdd.snapholds);
2436 if (sdd.cleanup_fd != -1)
2437 VERIFY(0 == close(sdd.cleanup_fd));
2443 const char *fromsnap;
2446 snapfilter_cb_t *filter_func;
2448 nvlist_t **debugnvp;
2452 zfs_send_cb(int outfd, void *arg)
2454 struct zfs_send *zs = arg;
2455 return (zfs_send_cb_impl(zs->zhp, zs->fromsnap, zs->tosnap, zs->flags,
2456 outfd, zs->filter_func, zs->cb_arg, zs->debugnvp));
2460 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2461 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2462 void *cb_arg, nvlist_t **debugnvp)
2464 struct zfs_send arg = {
2466 .fromsnap = fromsnap,
2469 .filter_func = filter_func,
2471 .debugnvp = debugnvp,
2473 return (lzc_send_wrapper(zfs_send_cb, outfd, &arg));
2477 static zfs_handle_t *
2478 name_to_dir_handle(libzfs_handle_t *hdl, const char *snapname)
2480 char dirname[ZFS_MAX_DATASET_NAME_LEN];
2481 (void) strlcpy(dirname, snapname, ZFS_MAX_DATASET_NAME_LEN);
2482 char *c = strchr(dirname, '@');
2485 return (zfs_open(hdl, dirname, ZFS_TYPE_DATASET));
2489 * Returns B_TRUE if earlier is an earlier snapshot in later's timeline; either
2490 * an earlier snapshot in the same filesystem, or a snapshot before later's
2491 * origin, or it's origin's origin, etc.
2494 snapshot_is_before(zfs_handle_t *earlier, zfs_handle_t *later)
2497 uint64_t later_txg =
2498 (later->zfs_type == ZFS_TYPE_FILESYSTEM ||
2499 later->zfs_type == ZFS_TYPE_VOLUME ?
2500 UINT64_MAX : zfs_prop_get_int(later, ZFS_PROP_CREATETXG));
2501 uint64_t earlier_txg = zfs_prop_get_int(earlier, ZFS_PROP_CREATETXG);
2503 if (earlier_txg >= later_txg)
2506 zfs_handle_t *earlier_dir = name_to_dir_handle(earlier->zfs_hdl,
2508 zfs_handle_t *later_dir = name_to_dir_handle(later->zfs_hdl,
2511 if (strcmp(earlier_dir->zfs_name, later_dir->zfs_name) == 0) {
2512 zfs_close(earlier_dir);
2513 zfs_close(later_dir);
2517 char clonename[ZFS_MAX_DATASET_NAME_LEN];
2518 if (zfs_prop_get(later_dir, ZFS_PROP_ORIGIN, clonename,
2519 ZFS_MAX_DATASET_NAME_LEN, NULL, NULL, 0, B_TRUE) != 0) {
2520 zfs_close(earlier_dir);
2521 zfs_close(later_dir);
2525 zfs_handle_t *origin = zfs_open(earlier->zfs_hdl, clonename,
2527 uint64_t origin_txg = zfs_prop_get_int(origin, ZFS_PROP_CREATETXG);
2530 * If "earlier" is exactly the origin, then
2531 * snapshot_is_before(earlier, origin) will return false (because
2532 * they're the same).
2534 if (origin_txg == earlier_txg &&
2535 strcmp(origin->zfs_name, earlier->zfs_name) == 0) {
2536 zfs_close(earlier_dir);
2537 zfs_close(later_dir);
2541 zfs_close(earlier_dir);
2542 zfs_close(later_dir);
2544 ret = snapshot_is_before(earlier, origin);
2550 * The "zhp" argument is the handle of the dataset to send (typically a
2551 * snapshot). The "from" argument is the full name of the snapshot or
2552 * bookmark that is the incremental source.
2554 * Pre-wrapped (cf. lzc_send_wrapper()).
2557 zfs_send_one_cb_impl(zfs_handle_t *zhp, const char *from, int fd,
2558 sendflags_t *flags, const char *redactbook)
2561 libzfs_handle_t *hdl = zhp->zfs_hdl;
2562 char *name = zhp->zfs_name;
2564 progress_arg_t pa = { 0 };
2566 char errbuf[ERRBUFLEN];
2567 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2568 "warning: cannot send '%s'"), name);
2570 if (from != NULL && strchr(from, '@')) {
2571 zfs_handle_t *from_zhp = zfs_open(hdl, from,
2573 if (from_zhp == NULL)
2575 if (!snapshot_is_before(from_zhp, zhp)) {
2576 zfs_close(from_zhp);
2577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2578 "not an earlier snapshot from the same fs"));
2579 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2581 zfs_close(from_zhp);
2584 if (redactbook != NULL) {
2585 char bookname[ZFS_MAX_DATASET_NAME_LEN];
2586 nvlist_t *redact_snaps;
2587 zfs_handle_t *book_zhp;
2591 pound = strchr(redactbook, '#');
2593 redactbook = pound + 1;
2594 at = strchr(name, '@');
2596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2597 "cannot do a redacted send to a filesystem"));
2598 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2600 dsnamelen = at - name;
2601 if (snprintf(bookname, sizeof (bookname), "%.*s#%s",
2602 dsnamelen, name, redactbook)
2603 >= sizeof (bookname)) {
2604 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2605 "invalid bookmark name"));
2606 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2608 book_zhp = zfs_open(hdl, bookname, ZFS_TYPE_BOOKMARK);
2609 if (book_zhp == NULL)
2611 if (nvlist_lookup_nvlist(book_zhp->zfs_props,
2612 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2613 &redact_snaps) != 0 || redact_snaps == NULL) {
2614 zfs_close(book_zhp);
2615 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2616 "not a redaction bookmark"));
2617 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2619 zfs_close(book_zhp);
2623 * Send fs properties
2625 if (flags->props || flags->holds || flags->backup) {
2627 * Note: the header generated by send_prelim_records()
2628 * assumes that the incremental source is in the same
2629 * filesystem/volume as the target (which is a requirement
2630 * when doing "zfs send -R"). But that isn't always the
2631 * case here (e.g. send from snap in origin, or send from
2632 * bookmark). We pass from=NULL, which will omit this
2633 * information from the prelim records; it isn't used
2634 * when receiving this type of stream.
2636 err = send_prelim_records(zhp, NULL, fd, B_TRUE, B_FALSE,
2637 flags->verbosity > 0, flags->dryrun, flags->raw,
2638 flags->replicate, B_FALSE, flags->backup, flags->holds,
2639 flags->props, flags->doall, NULL, NULL);
2645 * Perform size estimate if verbose was specified.
2647 if (flags->verbosity != 0) {
2648 err = estimate_size(zhp, from, fd, flags, 0, 0, 0, redactbook,
2658 * If progress reporting is requested, spawn a new thread to poll
2659 * ZFS_IOC_SEND_PROGRESS at a regular interval.
2661 if (flags->progress) {
2664 pa.pa_parsable = flags->parsable;
2665 pa.pa_estimate = B_FALSE;
2666 pa.pa_verbosity = flags->verbosity;
2668 err = pthread_create(&ptid, NULL,
2669 send_progress_thread, &pa);
2671 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
2672 return (zfs_error(zhp->zfs_hdl,
2673 EZFS_THREADCREATEFAILED, errbuf));
2677 err = lzc_send_redacted(name, from, fd,
2678 lzc_flags_from_sendflags(flags), redactbook);
2680 if (flags->progress && send_progress_thread_exit(hdl, ptid))
2683 if (err == 0 && (flags->props || flags->holds || flags->backup)) {
2684 /* Write the final end record. */
2685 err = send_conclusion_record(fd, NULL);
2687 return (zfs_standard_error(hdl, err, errbuf));
2692 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2693 "not an earlier snapshot from the same fs"));
2694 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2698 if (lzc_exists(name)) {
2699 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2700 "incremental source (%s) does not exist"),
2703 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2706 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2707 "dataset key must be loaded"));
2708 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2711 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2712 "target is busy; if a filesystem, "
2713 "it must not be mounted"));
2714 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2728 zfs_error_aux(hdl, "%s", strerror(errno));
2729 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2732 return (zfs_standard_error(hdl, errno, errbuf));
2738 struct zfs_send_one {
2742 const char *redactbook;
2746 zfs_send_one_cb(int fd, void *arg)
2748 struct zfs_send_one *zso = arg;
2749 return (zfs_send_one_cb_impl(zso->zhp, zso->from, fd, zso->flags,
2754 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
2755 const char *redactbook)
2757 struct zfs_send_one zso = {
2761 .redactbook = redactbook,
2763 return (lzc_send_wrapper(zfs_send_one_cb, fd, &zso));
2767 * Routines specific to "zfs recv"
2771 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2772 boolean_t byteswap, zio_cksum_t *zc)
2779 rv = read(fd, cp, len);
2784 if (rv < 0 || len != 0) {
2785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2786 "failed to read from stream"));
2787 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2788 "cannot receive")));
2793 fletcher_4_incremental_byteswap(buf, ilen, zc);
2795 fletcher_4_incremental_native(buf, ilen, zc);
2801 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2802 boolean_t byteswap, zio_cksum_t *zc)
2807 buf = zfs_alloc(hdl, len);
2809 if (len > hdl->libzfs_max_nvlist) {
2810 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "nvlist too large"));
2815 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2821 err = nvlist_unpack(buf, len, nvp, 0);
2824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2825 "stream (malformed nvlist)"));
2832 * Returns the grand origin (origin of origin of origin...) of a given handle.
2833 * If this dataset is not a clone, it simply returns a copy of the original
2836 static zfs_handle_t *
2837 recv_open_grand_origin(zfs_handle_t *zhp)
2839 char origin[ZFS_MAX_DATASET_NAME_LEN];
2841 zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2843 while (ozhp != NULL) {
2844 if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2845 sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2848 (void) zfs_close(ozhp);
2849 ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2856 recv_rename_impl(zfs_handle_t *zhp, const char *name, const char *newname)
2859 zfs_handle_t *ozhp = NULL;
2862 * Attempt to rename the dataset. If it fails with EACCES we have
2863 * attempted to rename the dataset outside of its encryption root.
2864 * Force the dataset to become an encryption root and try again.
2866 err = lzc_rename(name, newname);
2867 if (err == EACCES) {
2868 ozhp = recv_open_grand_origin(zhp);
2874 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2879 err = lzc_rename(name, newname);
2889 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2890 int baselen, char *newname, recvflags_t *flags)
2894 prop_changelist_t *clp = NULL;
2895 zfs_handle_t *zhp = NULL;
2897 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2902 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2903 flags->force ? MS_FORCE : 0);
2908 err = changelist_prefix(clp);
2913 (void) strlcpy(newname, tryname, ZFS_MAX_DATASET_NAME_LEN);
2914 if (flags->verbose) {
2915 (void) printf("attempting rename %s to %s\n",
2918 err = recv_rename_impl(zhp, name, newname);
2920 changelist_rename(clp, name, tryname);
2925 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2928 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2929 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2931 if (flags->verbose) {
2932 (void) printf("failed - trying rename %s to %s\n",
2935 err = recv_rename_impl(zhp, name, newname);
2937 changelist_rename(clp, name, newname);
2938 if (err && flags->verbose) {
2939 (void) printf("failed (%u) - "
2940 "will try again on next pass\n", errno);
2943 } else if (flags->verbose) {
2945 (void) printf("success\n");
2947 (void) printf("failed (%u)\n", errno);
2950 (void) changelist_postfix(clp);
2954 changelist_free(clp);
2962 recv_promote(libzfs_handle_t *hdl, const char *fsname,
2963 const char *origin_fsname, recvflags_t *flags)
2966 zfs_cmd_t zc = {"\0"};
2967 zfs_handle_t *zhp = NULL, *ozhp = NULL;
2970 (void) printf("promoting %s\n", fsname);
2972 (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
2973 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
2976 * Attempt to promote the dataset. If it fails with EACCES the
2977 * promotion would cause this dataset to leave its encryption root.
2978 * Force the origin to become an encryption root and try again.
2980 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2981 if (err == EACCES) {
2982 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2988 ozhp = recv_open_grand_origin(zhp);
2994 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2999 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3012 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
3013 char *newname, recvflags_t *flags)
3016 prop_changelist_t *clp;
3018 boolean_t defer = B_FALSE;
3021 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
3024 zfs_type_t type = zfs_get_type(zhp);
3025 if (type == ZFS_TYPE_SNAPSHOT &&
3026 zfs_spa_version(zhp, &spa_version) == 0 &&
3027 spa_version >= SPA_VERSION_USERREFS)
3029 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3030 flags->force ? MS_FORCE : 0);
3035 err = changelist_prefix(clp);
3040 (void) printf("attempting destroy %s\n", name);
3041 if (type == ZFS_TYPE_SNAPSHOT) {
3042 nvlist_t *nv = fnvlist_alloc();
3043 fnvlist_add_boolean(nv, name);
3044 err = lzc_destroy_snaps(nv, defer, NULL);
3047 err = lzc_destroy(name);
3051 (void) printf("success\n");
3052 changelist_remove(clp, name);
3055 (void) changelist_postfix(clp);
3056 changelist_free(clp);
3059 * Deferred destroy might destroy the snapshot or only mark it to be
3060 * destroyed later, and it returns success in either case.
3062 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
3063 ZFS_TYPE_SNAPSHOT))) {
3064 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
3070 typedef struct guid_to_name_data {
3072 boolean_t bookmark_ok;
3075 uint64_t *redact_snap_guids;
3076 uint64_t num_redact_snaps;
3077 } guid_to_name_data_t;
3080 redact_snaps_match(zfs_handle_t *zhp, guid_to_name_data_t *gtnd)
3082 uint64_t *bmark_snaps;
3083 uint_t bmark_num_snaps;
3085 if (zhp->zfs_type != ZFS_TYPE_BOOKMARK)
3088 nvl = fnvlist_lookup_nvlist(zhp->zfs_props,
3089 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
3090 bmark_snaps = fnvlist_lookup_uint64_array(nvl, ZPROP_VALUE,
3092 if (bmark_num_snaps != gtnd->num_redact_snaps)
3095 for (; i < bmark_num_snaps; i++) {
3097 for (; j < bmark_num_snaps; j++) {
3098 if (bmark_snaps[i] == gtnd->redact_snap_guids[j])
3101 if (j == bmark_num_snaps)
3104 return (i == bmark_num_snaps);
3108 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
3110 guid_to_name_data_t *gtnd = arg;
3114 if (gtnd->skip != NULL &&
3115 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
3116 strcmp(slash + 1, gtnd->skip) == 0) {
3121 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid &&
3122 (gtnd->num_redact_snaps == -1 || redact_snaps_match(zhp, gtnd))) {
3123 (void) strcpy(gtnd->name, zhp->zfs_name);
3128 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
3129 if (err != EEXIST && gtnd->bookmark_ok)
3130 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
3136 * Attempt to find the local dataset associated with this guid. In the case of
3137 * multiple matches, we attempt to find the "best" match by searching
3138 * progressively larger portions of the hierarchy. This allows one to send a
3139 * tree of datasets individually and guarantee that we will find the source
3140 * guid within that hierarchy, even if there are multiple matches elsewhere.
3142 * If num_redact_snaps is not -1, we attempt to find a redaction bookmark with
3143 * the specified number of redaction snapshots. If num_redact_snaps isn't 0 or
3144 * -1, then redact_snap_guids will be an array of the guids of the snapshots the
3145 * redaction bookmark was created with. If num_redact_snaps is -1, then we will
3146 * attempt to find a snapshot or bookmark (if bookmark_ok is passed) with the
3147 * given guid. Note that a redaction bookmark can be returned if
3148 * num_redact_snaps == -1.
3151 guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
3152 uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
3153 uint64_t num_redact_snaps, char *name)
3155 char pname[ZFS_MAX_DATASET_NAME_LEN];
3156 guid_to_name_data_t gtnd;
3159 gtnd.bookmark_ok = bookmark_ok;
3162 gtnd.redact_snap_guids = redact_snap_guids;
3163 gtnd.num_redact_snaps = num_redact_snaps;
3166 * Search progressively larger portions of the hierarchy, starting
3167 * with the filesystem specified by 'parent'. This will
3168 * select the "most local" version of the origin snapshot in the case
3169 * that there are multiple matching snapshots in the system.
3171 (void) strlcpy(pname, parent, sizeof (pname));
3172 char *cp = strrchr(pname, '@');
3174 cp = strchr(pname, '\0');
3175 for (; cp != NULL; cp = strrchr(pname, '/')) {
3176 /* Chop off the last component and open the parent */
3178 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
3182 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
3184 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
3185 if (err != EEXIST && bookmark_ok)
3186 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
3192 * Remember the last portion of the dataset so we skip it next
3193 * time through (as we've already searched that portion of the
3196 gtnd.skip = strrchr(pname, '/') + 1;
3203 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
3204 boolean_t bookmark_ok, char *name)
3206 return (guid_to_name_redact_snaps(hdl, parent, guid, bookmark_ok, NULL,
3211 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
3212 * guid1 is after guid2.
3215 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
3216 uint64_t guid1, uint64_t guid2)
3219 char *fsname = NULL, *snapname = NULL;
3220 char buf[ZFS_MAX_DATASET_NAME_LEN];
3222 zfs_handle_t *guid1hdl, *guid2hdl;
3223 uint64_t create1, create2;
3230 nvfs = fsavl_find(avl, guid1, &snapname);
3231 fsname = fnvlist_lookup_string(nvfs, "name");
3232 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3233 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3234 if (guid1hdl == NULL)
3237 nvfs = fsavl_find(avl, guid2, &snapname);
3238 fsname = fnvlist_lookup_string(nvfs, "name");
3239 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3240 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3241 if (guid2hdl == NULL) {
3242 zfs_close(guid1hdl);
3246 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
3247 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
3249 if (create1 < create2)
3251 else if (create1 > create2)
3256 zfs_close(guid1hdl);
3257 zfs_close(guid2hdl);
3263 * This function reestablishes the hierarchy of encryption roots after a
3264 * recursive incremental receive has completed. This must be done after the
3265 * second call to recv_incremental_replication() has renamed and promoted all
3266 * sent datasets to their final locations in the dataset hierarchy.
3269 recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
3270 nvlist_t *stream_nv)
3273 nvpair_t *fselem = NULL;
3274 nvlist_t *stream_fss;
3276 stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3278 while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3279 zfs_handle_t *zhp = NULL;
3281 nvlist_t *snaps, *props, *stream_nvfs = NULL;
3282 nvpair_t *snapel = NULL;
3283 boolean_t is_encroot, is_clone, stream_encroot;
3285 char *stream_keylocation = NULL;
3286 char keylocation[MAXNAMELEN];
3287 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3289 keylocation[0] = '\0';
3290 stream_nvfs = fnvpair_value_nvlist(fselem);
3291 snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps");
3292 props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3293 stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3295 /* find a snapshot from the stream that exists locally */
3297 while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
3300 guid = fnvpair_value_uint64(snapel);
3301 err = guid_to_name(hdl, top_zfs, guid, B_FALSE,
3310 cp = strchr(fsname, '@');
3314 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3320 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
3321 is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3322 (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3324 /* we don't need to do anything for unencrypted datasets */
3325 if (crypt == ZIO_CRYPT_OFF) {
3331 * If the dataset is flagged as an encryption root, was not
3332 * received as a clone and is not currently an encryption root,
3333 * force it to become one. Fixup the keylocation if necessary.
3335 if (stream_encroot) {
3336 if (!is_clone && !is_encroot) {
3337 err = lzc_change_key(fsname,
3338 DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
3345 stream_keylocation = fnvlist_lookup_string(props,
3346 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
3349 * Refresh the properties in case the call to
3350 * lzc_change_key() changed the value.
3352 zfs_refresh_properties(zhp);
3353 err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
3354 keylocation, sizeof (keylocation), NULL, NULL,
3361 if (strcmp(keylocation, stream_keylocation) != 0) {
3362 err = zfs_prop_set(zhp,
3363 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
3364 stream_keylocation);
3373 * If the dataset is not flagged as an encryption root and is
3374 * currently an encryption root, force it to inherit from its
3375 * parent. The root of a raw send should never be
3378 if (!stream_encroot && is_encroot &&
3379 strcmp(top_zfs, fsname) != 0) {
3380 err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
3398 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
3399 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3402 nvlist_t *local_nv, *deleted = NULL;
3403 avl_tree_t *local_avl;
3404 nvpair_t *fselem, *nextfselem;
3406 char newname[ZFS_MAX_DATASET_NAME_LEN];
3409 boolean_t needagain, progress, recursive;
3412 fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap");
3414 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3421 needagain = progress = B_FALSE;
3423 deleted = fnvlist_alloc();
3425 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
3426 recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE,
3427 B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
3431 * Process deletes and renames
3433 for (fselem = nvlist_next_nvpair(local_nv, NULL);
3434 fselem; fselem = nextfselem) {
3435 nvlist_t *nvfs, *snaps;
3436 nvlist_t *stream_nvfs = NULL;
3437 nvpair_t *snapelem, *nextsnapelem;
3438 uint64_t fromguid = 0;
3439 uint64_t originguid = 0;
3440 uint64_t stream_originguid = 0;
3441 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
3442 char *fsname, *stream_fsname;
3444 nextfselem = nvlist_next_nvpair(local_nv, fselem);
3446 nvfs = fnvpair_value_nvlist(fselem);
3447 snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
3448 fsname = fnvlist_lookup_string(nvfs, "name");
3449 parent_fromsnap_guid = fnvlist_lookup_uint64(nvfs,
3451 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
3454 * First find the stream's fs, so we can check for
3455 * a different origin (due to "zfs promote")
3457 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3458 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3461 thisguid = fnvpair_value_uint64(snapelem);
3462 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3464 if (stream_nvfs != NULL)
3468 /* check for promote */
3469 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
3470 &stream_originguid);
3471 if (stream_nvfs && originguid != stream_originguid) {
3472 switch (created_before(hdl, local_avl,
3473 stream_originguid, originguid)) {
3476 nvlist_t *origin_nvfs;
3477 char *origin_fsname;
3479 origin_nvfs = fsavl_find(local_avl, originguid,
3481 origin_fsname = fnvlist_lookup_string(
3482 origin_nvfs, "name");
3483 error = recv_promote(hdl, fsname, origin_fsname,
3492 fsavl_destroy(local_avl);
3493 fnvlist_free(local_nv);
3497 * We had/have the wrong origin, therefore our
3498 * list of snapshots is wrong. Need to handle
3499 * them on the next pass.
3505 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3506 snapelem; snapelem = nextsnapelem) {
3508 char *stream_snapname;
3509 nvlist_t *found, *props;
3511 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
3513 thisguid = fnvpair_value_uint64(snapelem);
3514 found = fsavl_find(stream_avl, thisguid,
3517 /* check for delete */
3518 if (found == NULL) {
3519 char name[ZFS_MAX_DATASET_NAME_LEN];
3524 (void) snprintf(name, sizeof (name), "%s@%s",
3525 fsname, nvpair_name(snapelem));
3527 error = recv_destroy(hdl, name,
3528 strlen(fsname)+1, newname, flags);
3533 sprintf(guidname, "%llu",
3534 (u_longlong_t)thisguid);
3535 nvlist_add_boolean(deleted, guidname);
3539 stream_nvfs = found;
3541 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
3542 &props) && 0 == nvlist_lookup_nvlist(props,
3543 stream_snapname, &props)) {
3544 zfs_cmd_t zc = {"\0"};
3546 zc.zc_cookie = B_TRUE; /* received */
3547 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
3548 "%s@%s", fsname, nvpair_name(snapelem));
3549 zcmd_write_src_nvlist(hdl, &zc, props);
3550 (void) zfs_ioctl(hdl,
3551 ZFS_IOC_SET_PROP, &zc);
3552 zcmd_free_nvlists(&zc);
3555 /* check for different snapname */
3556 if (strcmp(nvpair_name(snapelem),
3557 stream_snapname) != 0) {
3558 char name[ZFS_MAX_DATASET_NAME_LEN];
3559 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3561 (void) snprintf(name, sizeof (name), "%s@%s",
3562 fsname, nvpair_name(snapelem));
3563 (void) snprintf(tryname, sizeof (name), "%s@%s",
3564 fsname, stream_snapname);
3566 error = recv_rename(hdl, name, tryname,
3567 strlen(fsname)+1, newname, flags);
3574 if (strcmp(stream_snapname, fromsnap) == 0)
3575 fromguid = thisguid;
3578 /* check for delete */
3579 if (stream_nvfs == NULL) {
3583 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
3589 sprintf(guidname, "%llu",
3590 (u_longlong_t)parent_fromsnap_guid);
3591 nvlist_add_boolean(deleted, guidname);
3595 if (fromguid == 0) {
3596 if (flags->verbose) {
3597 (void) printf("local fs %s does not have "
3598 "fromsnap (%s in stream); must have "
3599 "been deleted locally; ignoring\n",
3605 stream_fsname = fnvlist_lookup_string(stream_nvfs, "name");
3606 stream_parent_fromsnap_guid = fnvlist_lookup_uint64(
3607 stream_nvfs, "parentfromsnap");
3609 s1 = strrchr(fsname, '/');
3610 s2 = strrchr(stream_fsname, '/');
3613 * Check if we're going to rename based on parent guid change
3614 * and the current parent guid was also deleted. If it was then
3615 * rename will fail and is likely unneeded, so avoid this and
3616 * force an early retry to determine the new
3617 * parent_fromsnap_guid.
3619 if (stream_parent_fromsnap_guid != 0 &&
3620 parent_fromsnap_guid != 0 &&
3621 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
3622 sprintf(guidname, "%llu",
3623 (u_longlong_t)parent_fromsnap_guid);
3624 if (nvlist_exists(deleted, guidname)) {
3632 * Check for rename. If the exact receive path is specified, it
3633 * does not count as a rename, but we still need to check the
3634 * datasets beneath it.
3636 if ((stream_parent_fromsnap_guid != 0 &&
3637 parent_fromsnap_guid != 0 &&
3638 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
3639 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3640 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3642 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3644 parent = fsavl_find(local_avl,
3645 stream_parent_fromsnap_guid, NULL);
3647 * NB: parent might not be found if we used the
3648 * tosnap for stream_parent_fromsnap_guid,
3649 * because the parent is a newly-created fs;
3650 * we'll be able to rename it after we recv the
3653 if (parent != NULL) {
3656 pname = fnvlist_lookup_string(parent, "name");
3657 (void) snprintf(tryname, sizeof (tryname),
3658 "%s%s", pname, strrchr(stream_fsname, '/'));
3661 if (flags->verbose) {
3662 (void) printf("local fs %s new parent "
3663 "not found\n", fsname);
3669 error = recv_rename(hdl, fsname, tryname,
3670 strlen(tofs)+1, newname, flags);
3672 if (renamed != NULL && newname[0] != '\0') {
3673 fnvlist_add_boolean(renamed, newname);
3684 fsavl_destroy(local_avl);
3685 fnvlist_free(local_nv);
3686 fnvlist_free(deleted);
3688 if (needagain && progress) {
3689 /* do another pass to fix up temporary names */
3691 (void) printf("another pass:\n");
3695 return (needagain || error != 0);
3699 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
3700 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3701 char **top_zfs, nvlist_t *cmdprops)
3703 nvlist_t *stream_nv = NULL;
3704 avl_tree_t *stream_avl = NULL;
3705 char *fromsnap = NULL;
3706 char *sendsnap = NULL;
3708 char tofs[ZFS_MAX_DATASET_NAME_LEN];
3709 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
3710 char errbuf[ERRBUFLEN];
3711 dmu_replay_record_t drre;
3713 boolean_t anyerr = B_FALSE;
3714 boolean_t softerr = B_FALSE;
3715 boolean_t recursive, raw;
3717 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3720 assert(drr->drr_type == DRR_BEGIN);
3721 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
3722 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3723 DMU_COMPOUNDSTREAM);
3726 * Read in the nvlist from the stream.
3728 if (drr->drr_payloadlen != 0) {
3729 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
3730 &stream_nv, flags->byteswap, zc);
3732 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3737 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3739 raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3741 if (recursive && strchr(destname, '@')) {
3742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3743 "cannot specify snapshot name for multi-snapshot stream"));
3744 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3749 * Read in the end record and verify checksum.
3751 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3752 flags->byteswap, NULL)))
3754 if (flags->byteswap) {
3755 drre.drr_type = BSWAP_32(drre.drr_type);
3756 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3757 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3758 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3759 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3760 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3761 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3762 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3763 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3765 if (drre.drr_type != DRR_END) {
3766 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3769 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3770 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3771 "incorrect header checksum"));
3772 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3776 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3778 if (drr->drr_payloadlen != 0) {
3779 nvlist_t *stream_fss;
3781 stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3782 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3783 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3784 "couldn't allocate avl tree"));
3785 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3789 if (fromsnap != NULL && recursive) {
3790 nvlist_t *renamed = NULL;
3791 nvpair_t *pair = NULL;
3793 (void) strlcpy(tofs, destname, sizeof (tofs));
3794 if (flags->isprefix) {
3795 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3798 if (flags->istail) {
3799 cp = strrchr(drrb->drr_toname, '/');
3801 (void) strlcat(tofs, "/",
3805 i = (cp - drrb->drr_toname);
3808 i = strcspn(drrb->drr_toname, "/@");
3810 /* zfs_receive_one() will create_parents() */
3811 (void) strlcat(tofs, &drrb->drr_toname[i],
3813 *strchr(tofs, '@') = '\0';
3816 if (!flags->dryrun && !flags->nomount) {
3817 renamed = fnvlist_alloc();
3820 softerr = recv_incremental_replication(hdl, tofs, flags,
3821 stream_nv, stream_avl, renamed);
3823 /* Unmount renamed filesystems before receiving. */
3824 while ((pair = nvlist_next_nvpair(renamed,
3827 prop_changelist_t *clp = NULL;
3829 zhp = zfs_open(hdl, nvpair_name(pair),
3830 ZFS_TYPE_FILESYSTEM);
3832 clp = changelist_gather(zhp,
3833 ZFS_PROP_MOUNTPOINT, 0,
3834 flags->forceunmount ? MS_FORCE : 0);
3838 changelist_prefix(clp);
3839 changelist_free(clp);
3844 fnvlist_free(renamed);
3849 * Get the fs specified by the first path in the stream (the top level
3850 * specified by 'zfs send') and pass it to each invocation of
3851 * zfs_receive_one().
3853 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
3855 if ((cp = strchr(sendfs, '@')) != NULL) {
3858 * Find the "sendsnap", the final snapshot in a replication
3859 * stream. zfs_receive_one() handles certain errors
3860 * differently, depending on if the contained stream is the
3863 sendsnap = (cp + 1);
3866 /* Finally, receive each contained stream */
3869 * we should figure out if it has a recoverable
3870 * error, in which case do a recv_skip() and drive on.
3871 * Note, if we fail due to already having this guid,
3872 * zfs_receive_one() will take care of it (ie,
3873 * recv_skip() and return 0).
3875 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3876 sendfs, stream_nv, stream_avl, top_zfs, sendsnap, cmdprops);
3877 if (error == ENODATA) {
3882 } while (error == 0);
3884 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
3886 * Now that we have the fs's they sent us, try the
3889 softerr = recv_incremental_replication(hdl, tofs, flags,
3890 stream_nv, stream_avl, NULL);
3893 if (raw && softerr == 0 && *top_zfs != NULL) {
3894 softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
3899 fsavl_destroy(stream_avl);
3900 fnvlist_free(stream_nv);
3909 trunc_prop_errs(int truncated)
3911 ASSERT(truncated != 0);
3914 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3915 "1 more property could not be set\n"));
3917 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3918 "%d more properties could not be set\n"), truncated);
3922 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3924 dmu_replay_record_t *drr;
3925 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
3926 uint64_t payload_size;
3927 char errbuf[ERRBUFLEN];
3929 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3932 /* XXX would be great to use lseek if possible... */
3935 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3936 byteswap, NULL) == 0) {
3938 drr->drr_type = BSWAP_32(drr->drr_type);
3940 switch (drr->drr_type) {
3942 if (drr->drr_payloadlen != 0) {
3943 (void) recv_read(hdl, fd, buf,
3944 drr->drr_payloadlen, B_FALSE, NULL);
3954 drr->drr_u.drr_object.drr_bonuslen =
3955 BSWAP_32(drr->drr_u.drr_object.
3957 drr->drr_u.drr_object.drr_raw_bonuslen =
3958 BSWAP_32(drr->drr_u.drr_object.
3963 DRR_OBJECT_PAYLOAD_SIZE(&drr->drr_u.drr_object);
3964 (void) recv_read(hdl, fd, buf, payload_size,
3970 drr->drr_u.drr_write.drr_logical_size =
3972 drr->drr_u.drr_write.drr_logical_size);
3973 drr->drr_u.drr_write.drr_compressed_size =
3975 drr->drr_u.drr_write.drr_compressed_size);
3978 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3979 assert(payload_size <= SPA_MAXBLOCKSIZE);
3980 (void) recv_read(hdl, fd, buf,
3981 payload_size, B_FALSE, NULL);
3985 drr->drr_u.drr_spill.drr_length =
3986 BSWAP_64(drr->drr_u.drr_spill.drr_length);
3987 drr->drr_u.drr_spill.drr_compressed_size =
3988 BSWAP_64(drr->drr_u.drr_spill.
3989 drr_compressed_size);
3993 DRR_SPILL_PAYLOAD_SIZE(&drr->drr_u.drr_spill);
3994 (void) recv_read(hdl, fd, buf, payload_size,
3997 case DRR_WRITE_EMBEDDED:
3999 drr->drr_u.drr_write_embedded.drr_psize =
4000 BSWAP_32(drr->drr_u.drr_write_embedded.
4003 (void) recv_read(hdl, fd, buf,
4004 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
4007 case DRR_OBJECT_RANGE:
4008 case DRR_WRITE_BYREF:
4009 case DRR_FREEOBJECTS:
4014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4015 "invalid record type"));
4017 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4026 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
4027 boolean_t resumable, boolean_t checksum)
4029 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
4031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, (checksum ?
4032 "checksum mismatch" : "incomplete stream")));
4036 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
4037 *strchr(target_fs, '@') = '\0';
4038 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
4039 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4043 char token_buf[ZFS_MAXPROPLEN];
4044 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4045 token_buf, sizeof (token_buf),
4046 NULL, NULL, 0, B_TRUE);
4048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4049 "checksum mismatch or incomplete stream.\n"
4050 "Partially received snapshot is saved.\n"
4051 "A resuming stream can be generated on the sending "
4052 "system by running:\n"
4060 * Prepare a new nvlist of properties that are to override (-o) or be excluded
4061 * (-x) from the received dataset
4062 * recvprops: received properties from the send stream
4063 * cmdprops: raw input properties from command line
4064 * origprops: properties, both locally-set and received, currently set on the
4065 * target dataset if it exists, NULL otherwise.
4066 * oxprops: valid output override (-o) and excluded (-x) properties
4069 zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
4070 char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
4071 boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
4072 nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
4073 uint_t *wkeylen_out, const char *errbuf)
4076 nvlist_t *oprops, *voprops;
4077 zfs_handle_t *zhp = NULL;
4078 zpool_handle_t *zpool_hdl = NULL;
4081 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4083 if (nvlist_empty(cmdprops))
4084 return (0); /* No properties to override or exclude */
4086 *oxprops = fnvlist_alloc();
4087 oprops = fnvlist_alloc();
4089 strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
4092 * Get our dataset handle. The target dataset may not exist yet.
4094 if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
4095 zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
4102 /* open the zpool handle */
4103 cp = strchr(namebuf, '/');
4106 zpool_hdl = zpool_open(hdl, namebuf);
4107 if (zpool_hdl == NULL) {
4112 /* restore namebuf to match fsname for later use */
4117 * first iteration: process excluded (-x) properties now and gather
4118 * added (-o) properties to be later processed by zfs_valid_proplist()
4121 while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
4122 const char *name = nvpair_name(nvp);
4123 zfs_prop_t prop = zfs_name_to_prop(name);
4126 * It turns out, if we don't normalize "aliased" names
4127 * e.g. compress= against the "real" names (e.g. compression)
4128 * here, then setting/excluding them does not work as
4131 * But since user-defined properties wouldn't have a valid
4132 * mapping here, we do this conditional dance.
4134 const char *newname = name;
4135 if (prop >= ZFS_PROP_TYPE)
4136 newname = zfs_prop_to_name(prop);
4138 /* "origin" is processed separately, don't handle it here */
4139 if (prop == ZFS_PROP_ORIGIN)
4142 /* raw streams can't override encryption properties */
4143 if ((zfs_prop_encryption_key_param(prop) ||
4144 prop == ZFS_PROP_ENCRYPTION) && raw) {
4145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4146 "encryption property '%s' cannot "
4147 "be set or excluded for raw streams."), name);
4148 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4152 /* incremental streams can only exclude encryption properties */
4153 if ((zfs_prop_encryption_key_param(prop) ||
4154 prop == ZFS_PROP_ENCRYPTION) && !newfs &&
4155 nvpair_type(nvp) != DATA_TYPE_BOOLEAN) {
4156 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4157 "encryption property '%s' cannot "
4158 "be set for incremental streams."), name);
4159 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4163 switch (nvpair_type(nvp)) {
4164 case DATA_TYPE_BOOLEAN: /* -x property */
4166 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
4167 * a property: this is done by forcing an explicit
4168 * inherit on the destination so the effective value is
4169 * not the one we received from the send stream.
4171 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
4172 !zfs_prop_user(name)) {
4173 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4174 "Warning: %s: property '%s' does not "
4175 "apply to datasets of this type\n"),
4180 * We do this only if the property is not already
4181 * locally-set, in which case its value will take
4182 * priority over the received anyway.
4184 if (nvlist_exists(origprops, newname)) {
4186 char *source = NULL;
4188 attrs = fnvlist_lookup_nvlist(origprops,
4190 if (nvlist_lookup_string(attrs,
4191 ZPROP_SOURCE, &source) == 0 &&
4192 strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
4196 * We can't force an explicit inherit on non-inheritable
4197 * properties: if we're asked to exclude this kind of
4198 * values we remove them from "recvprops" input nvlist.
4200 if (!zfs_prop_user(name) && /* can be inherited too */
4201 !zfs_prop_inheritable(prop) &&
4202 nvlist_exists(recvprops, newname))
4203 fnvlist_remove(recvprops, newname);
4205 fnvlist_add_boolean(*oxprops, newname);
4207 case DATA_TYPE_STRING: /* -o property=value */
4209 * we're trying to override a property that does not
4210 * make sense for this type of dataset, but we don't
4211 * want to fail if the receive is recursive: this comes
4212 * in handy when the send stream contains, for
4213 * instance, a child ZVOL and we're trying to receive
4214 * it with "-o atime=on"
4216 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
4217 !zfs_prop_user(name)) {
4220 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4221 "property '%s' does not apply to datasets "
4222 "of this type"), name);
4223 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4226 fnvlist_add_string(oprops, newname,
4227 fnvpair_value_string(nvp));
4230 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4231 "property '%s' must be a string or boolean"), name);
4232 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4238 /* convert override strings properties to native */
4239 if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
4240 oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4241 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4246 * zfs_crypto_create() requires the parent name. Get it
4247 * by truncating the fsname copy stored in namebuf.
4249 cp = strrchr(namebuf, '/');
4253 if (!raw && zfs_crypto_create(hdl, namebuf, voprops, NULL,
4254 B_FALSE, wkeydata_out, wkeylen_out) != 0) {
4255 fnvlist_free(voprops);
4256 ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4260 /* second pass: process "-o" properties */
4261 fnvlist_merge(*oxprops, voprops);
4262 fnvlist_free(voprops);
4264 /* override props on child dataset are inherited */
4266 while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
4267 const char *name = nvpair_name(nvp);
4268 fnvlist_add_boolean(*oxprops, name);
4275 if (zpool_hdl != NULL)
4276 zpool_close(zpool_hdl);
4277 fnvlist_free(oprops);
4282 * Restores a backup of tosnap from the file descriptor specified by infd.
4285 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
4286 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
4287 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
4288 avl_tree_t *stream_avl, char **top_zfs,
4289 const char *finalsnap, nvlist_t *cmdprops)
4291 struct timespec begin_time;
4292 int ioctl_err, ioctl_errno, err;
4294 struct drr_begin *drrb = &drr->drr_u.drr_begin;
4295 char errbuf[ERRBUFLEN];
4296 const char *chopprefix;
4297 boolean_t newfs = B_FALSE;
4298 boolean_t stream_wantsnewfs, stream_resumingnewfs;
4299 boolean_t newprops = B_FALSE;
4300 uint64_t read_bytes = 0;
4301 uint64_t errflags = 0;
4302 uint64_t parent_snapguid = 0;
4303 prop_changelist_t *clp = NULL;
4304 nvlist_t *snapprops_nvlist = NULL;
4305 nvlist_t *snapholds_nvlist = NULL;
4306 zprop_errflags_t prop_errflags;
4307 nvlist_t *prop_errors = NULL;
4308 boolean_t recursive;
4309 char *snapname = NULL;
4310 char destsnap[MAXPATHLEN * 2];
4311 char origin[MAXNAMELEN] = {0};
4312 char name[MAXPATHLEN];
4313 char tmp_keylocation[MAXNAMELEN] = {0};
4314 nvlist_t *rcvprops = NULL; /* props received from the send stream */
4315 nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
4316 nvlist_t *origprops = NULL; /* original props (if destination exists) */
4317 zfs_type_t type = ZFS_TYPE_INVALID;
4318 boolean_t toplevel = B_FALSE;
4319 boolean_t zoned = B_FALSE;
4320 boolean_t hastoken = B_FALSE;
4322 uint8_t *wkeydata = NULL;
4325 #ifndef CLOCK_MONOTONIC_RAW
4326 #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
4328 clock_gettime(CLOCK_MONOTONIC_RAW, &begin_time);
4330 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4333 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
4336 /* Did the user request holds be skipped via zfs recv -k? */
4337 boolean_t holds = flags->holds && !flags->skipholds;
4339 if (stream_avl != NULL) {
4340 char *keylocation = NULL;
4341 nvlist_t *lookup = NULL;
4342 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
4345 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
4347 err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
4349 rcvprops = fnvlist_alloc();
4354 * The keylocation property may only be set on encryption roots,
4355 * but this dataset might not become an encryption root until
4356 * recv_fix_encryption_hierarchy() is called. That function
4357 * will fixup the keylocation anyway, so we temporarily unset
4358 * the keylocation for now to avoid any errors from the receive
4361 err = nvlist_lookup_string(rcvprops,
4362 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
4364 strlcpy(tmp_keylocation, keylocation, MAXNAMELEN);
4365 (void) nvlist_remove_all(rcvprops,
4366 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
4369 if (flags->canmountoff) {
4370 fnvlist_add_uint64(rcvprops,
4371 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0);
4372 } else if (newprops) { /* nothing in rcvprops, eliminate it */
4373 fnvlist_free(rcvprops);
4377 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
4378 snapprops_nvlist = fnvlist_lookup_nvlist(lookup,
4382 if (0 == nvlist_lookup_nvlist(fs, "snapholds",
4384 snapholds_nvlist = fnvlist_lookup_nvlist(
4393 * Determine how much of the snapshot name stored in the stream
4394 * we are going to tack on to the name they specified on the
4395 * command line, and how much we are going to chop off.
4397 * If they specified a snapshot, chop the entire name stored in
4400 if (flags->istail) {
4402 * A filesystem was specified with -e. We want to tack on only
4403 * the tail of the sent snapshot path.
4405 if (strchr(tosnap, '@')) {
4406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4407 "argument - snapshot not allowed with -e"));
4408 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4412 chopprefix = strrchr(sendfs, '/');
4414 if (chopprefix == NULL) {
4416 * The tail is the poolname, so we need to
4417 * prepend a path separator.
4419 int len = strlen(drrb->drr_toname);
4420 cp = umem_alloc(len + 2, UMEM_NOFAIL);
4422 (void) strcpy(&cp[1], drrb->drr_toname);
4425 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
4427 } else if (flags->isprefix) {
4429 * A filesystem was specified with -d. We want to tack on
4430 * everything but the first element of the sent snapshot path
4431 * (all but the pool name).
4433 if (strchr(tosnap, '@')) {
4434 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4435 "argument - snapshot not allowed with -d"));
4436 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4440 chopprefix = strchr(drrb->drr_toname, '/');
4441 if (chopprefix == NULL)
4442 chopprefix = strchr(drrb->drr_toname, '@');
4443 } else if (strchr(tosnap, '@') == NULL) {
4445 * If a filesystem was specified without -d or -e, we want to
4446 * tack on everything after the fs specified by 'zfs send'.
4448 chopprefix = drrb->drr_toname + strlen(sendfs);
4450 /* A snapshot was specified as an exact path (no -d or -e). */
4452 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4453 "cannot specify snapshot name for multi-snapshot "
4455 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4458 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
4461 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
4462 ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
4463 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
4464 strchr(sendfs, '/') == NULL);
4465 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
4466 chopprefix[0] == '\0');
4469 * Determine name of destination snapshot.
4471 (void) strlcpy(destsnap, tosnap, sizeof (destsnap));
4472 (void) strlcat(destsnap, chopprefix, sizeof (destsnap));
4474 umem_free(cp, strlen(cp) + 1);
4475 if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
4476 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4481 * Determine the name of the origin snapshot.
4484 (void) strlcpy(origin, originsnap, sizeof (origin));
4486 (void) printf("using provided clone origin %s\n",
4488 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
4489 if (guid_to_name(hdl, destsnap,
4490 drrb->drr_fromguid, B_FALSE, origin) != 0) {
4491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4492 "local origin for clone %s does not exist"),
4494 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4498 (void) printf("found clone origin %s\n", origin);
4501 if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4502 DMU_BACKUP_FEATURE_DEDUP)) {
4503 (void) fprintf(stderr,
4504 gettext("ERROR: \"zfs receive\" no longer supports "
4505 "deduplicated send streams. Use\n"
4506 "the \"zstream redup\" command to convert this stream "
4508 "non-deduplicated stream.\n"));
4509 err = zfs_error(hdl, EZFS_NOTSUP, errbuf);
4513 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4514 DMU_BACKUP_FEATURE_RESUMING;
4515 boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4516 DMU_BACKUP_FEATURE_RAW;
4517 boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4518 DMU_BACKUP_FEATURE_EMBED_DATA;
4519 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
4520 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
4521 stream_resumingnewfs = (drrb->drr_fromguid == 0 ||
4522 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && resuming;
4524 if (stream_wantsnewfs) {
4526 * if the parent fs does not exist, look for it based on
4527 * the parent snap GUID
4529 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4530 "cannot receive new filesystem stream"));
4532 (void) strlcpy(name, destsnap, sizeof (name));
4533 cp = strrchr(name, '/');
4537 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4538 char suffix[ZFS_MAX_DATASET_NAME_LEN];
4539 (void) strlcpy(suffix, strrchr(destsnap, '/'),
4541 if (guid_to_name(hdl, name, parent_snapguid,
4542 B_FALSE, destsnap) == 0) {
4543 *strchr(destsnap, '@') = '\0';
4544 (void) strlcat(destsnap, suffix,
4545 sizeof (destsnap) - strlen(destsnap));
4550 * If the fs does not exist, look for it based on the
4554 (void) snprintf(errbuf, sizeof (errbuf),
4555 dgettext(TEXT_DOMAIN,
4556 "cannot receive resume stream"));
4558 (void) snprintf(errbuf, sizeof (errbuf),
4559 dgettext(TEXT_DOMAIN,
4560 "cannot receive incremental stream"));
4563 (void) strlcpy(name, destsnap, sizeof (name));
4564 *strchr(name, '@') = '\0';
4567 * If the exact receive path was specified and this is the
4568 * topmost path in the stream, then if the fs does not exist we
4569 * should look no further.
4571 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
4572 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
4573 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4574 char snap[ZFS_MAX_DATASET_NAME_LEN];
4575 (void) strlcpy(snap, strchr(destsnap, '@'),
4577 if (guid_to_name(hdl, name, drrb->drr_fromguid,
4578 B_FALSE, destsnap) == 0) {
4579 *strchr(destsnap, '@') = '\0';
4580 (void) strlcat(destsnap, snap,
4581 sizeof (destsnap) - strlen(destsnap));
4586 (void) strlcpy(name, destsnap, sizeof (name));
4587 *strchr(name, '@') = '\0';
4589 redacted = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4590 DMU_BACKUP_FEATURE_REDACTED;
4593 if (flags->isprefix || flags->istail || flags->force ||
4594 flags->canmountoff || flags->resumable || flags->nomount ||
4596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4597 "corrective recv can not be used when combined with"
4599 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4603 get_snap_guid(hdl, name, strchr(destsnap, '@') + 1);
4605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4606 "corrective recv must specify an existing snapshot"
4608 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4610 } else if (guid != drrb->drr_toguid) {
4611 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4612 "local snapshot doesn't match the snapshot"
4613 " in the provided stream"));
4614 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4617 } else if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4618 zfs_cmd_t zc = {"\0"};
4619 zfs_handle_t *zhp = NULL;
4620 boolean_t encrypted;
4622 (void) strcpy(zc.zc_name, name);
4625 * Destination fs exists. It must be one of these cases:
4626 * - an incremental send stream
4627 * - the stream specifies a new fs (full stream or clone)
4628 * and they want us to blow away the existing fs (and
4629 * have therefore specified -F and removed any snapshots)
4630 * - we are resuming a failed receive.
4632 if (stream_wantsnewfs) {
4633 boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL;
4634 if (!flags->force) {
4635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4636 "destination '%s' exists\n"
4637 "must specify -F to overwrite it"), name);
4638 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4641 if (zfs_ioctl(hdl, ZFS_IOC_SNAPSHOT_LIST_NEXT,
4643 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4644 "destination has snapshots (eg. %s)\n"
4645 "must destroy them to overwrite it"),
4647 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4650 if (is_volume && strrchr(name, '/') == NULL) {
4651 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4652 "destination %s is the root dataset\n"
4653 "cannot overwrite with a ZVOL"),
4655 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4659 zfs_ioctl(hdl, ZFS_IOC_DATASET_LIST_NEXT,
4661 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4662 "destination has children (eg. %s)\n"
4663 "cannot overwrite with a ZVOL"),
4665 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4670 if ((zhp = zfs_open(hdl, name,
4671 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
4677 * When receiving full/newfs on existing dataset, then it
4678 * should be done with "-F" flag. Its enforced for initial
4679 * receive in previous checks in this function.
4680 * Similarly, on resuming full/newfs recv on existing dataset,
4681 * it should be done with "-F" flag.
4683 * When dataset doesn't exist, then full/newfs recv is done on
4684 * newly created dataset and it's marked INCONSISTENT. But
4685 * When receiving on existing dataset, recv is first done on
4686 * %recv and its marked INCONSISTENT. Existing dataset is not
4687 * marked INCONSISTENT.
4688 * Resume of full/newfs receive with dataset not INCONSISTENT
4689 * indicates that its resuming newfs on existing dataset. So,
4690 * enforce "-F" flag in this case.
4692 if (stream_resumingnewfs &&
4693 !zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
4696 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4697 "Resuming recv on existing destination '%s'\n"
4698 "must specify -F to overwrite it"), name);
4699 err = zfs_error(hdl, EZFS_RESUME_EXISTS, errbuf);
4703 if (stream_wantsnewfs &&
4704 zhp->zfs_dmustats.dds_origin[0]) {
4706 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4707 "destination '%s' is a clone\n"
4708 "must destroy it to overwrite it"), name);
4709 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4714 * Raw sends can not be performed as an incremental on top
4715 * of existing unencrypted datasets. zfs recv -F can't be
4716 * used to blow away an existing encrypted filesystem. This
4717 * is because it would require the dsl dir to point to the
4718 * new key (or lack of a key) and the old key at the same
4719 * time. The -F flag may still be used for deleting
4720 * intermediate snapshots that would otherwise prevent the
4721 * receive from working.
4723 encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
4725 if (!stream_wantsnewfs && !encrypted && raw) {
4727 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4728 "cannot perform raw receive on top of "
4729 "existing unencrypted dataset"));
4730 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4734 if (stream_wantsnewfs && flags->force &&
4735 ((raw && !encrypted) || encrypted)) {
4737 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4738 "zfs receive -F cannot be used to destroy an "
4739 "encrypted filesystem or overwrite an "
4740 "unencrypted one with an encrypted one"));
4741 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4745 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4746 (stream_wantsnewfs || stream_resumingnewfs)) {
4747 /* We can't do online recv in this case */
4748 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4749 flags->forceunmount ? MS_FORCE : 0);
4755 if (changelist_prefix(clp) != 0) {
4756 changelist_free(clp);
4764 * If we are resuming a newfs, set newfs here so that we will
4765 * mount it if the recv succeeds this time. We can tell
4766 * that it was a newfs on the first recv because the fs
4767 * itself will be inconsistent (if the fs existed when we
4768 * did the first recv, we would have received it into
4771 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4774 /* we want to know if we're zoned when validating -o|-x props */
4775 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4777 /* may need this info later, get it now we have zhp around */
4778 if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4779 NULL, NULL, 0, B_TRUE) == 0)
4782 /* gather existing properties on destination */
4783 origprops = fnvlist_alloc();
4784 fnvlist_merge(origprops, zhp->zfs_props);
4785 fnvlist_merge(origprops, zhp->zfs_user_props);
4792 * Destination filesystem does not exist. Therefore we better
4793 * be creating a new filesystem (either from a full backup, or
4794 * a clone). It would therefore be invalid if the user
4795 * specified only the pool name (i.e. if the destination name
4796 * contained no slash character).
4798 cp = strrchr(name, '/');
4800 if (!stream_wantsnewfs || cp == NULL) {
4801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4802 "destination '%s' does not exist"), name);
4803 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4808 * Trim off the final dataset component so we perform the
4809 * recvbackup ioctl to the filesystems's parent.
4813 if (flags->isprefix && !flags->istail && !flags->dryrun &&
4814 create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4815 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4819 /* validate parent */
4820 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4822 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4825 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
4826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4827 "parent '%s' is not a filesystem"), name);
4828 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4839 if (flags->verbose) {
4840 (void) printf("%s %s%s stream of %s into %s\n",
4841 flags->dryrun ? "would receive" : "receiving",
4842 flags->heal ? " corrective" : "",
4843 drrb->drr_fromguid ? "incremental" : "full",
4844 drrb->drr_toname, destsnap);
4845 (void) fflush(stdout);
4849 * If this is the top-level dataset, record it so we can use it
4850 * for recursive operations later.
4852 if (top_zfs != NULL &&
4853 (*top_zfs == NULL || strcmp(*top_zfs, name) == 0)) {
4855 if (*top_zfs == NULL)
4856 *top_zfs = zfs_strdup(hdl, name);
4859 if (drrb->drr_type == DMU_OST_ZVOL) {
4860 type = ZFS_TYPE_VOLUME;
4861 } else if (drrb->drr_type == DMU_OST_ZFS) {
4862 type = ZFS_TYPE_FILESYSTEM;
4864 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4865 "invalid record type: 0x%d"), drrb->drr_type);
4866 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4869 if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4870 stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4871 &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
4875 * When sending with properties (zfs send -p), the encryption property
4876 * is not included because it is a SETONCE property and therefore
4877 * treated as read only. However, we are always able to determine its
4878 * value because raw sends will include it in the DRR_BDEGIN payload
4879 * and non-raw sends with properties are not allowed for encrypted
4880 * datasets. Therefore, if this is a non-raw properties stream, we can
4881 * infer that the value should be ZIO_CRYPT_OFF and manually add that
4882 * to the received properties.
4884 if (stream_wantsnewfs && !raw && rcvprops != NULL &&
4885 !nvlist_exists(cmdprops, zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
4886 if (oxprops == NULL)
4887 oxprops = fnvlist_alloc();
4888 fnvlist_add_uint64(oxprops,
4889 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), ZIO_CRYPT_OFF);
4892 if (flags->dryrun) {
4893 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
4896 * We have read the DRR_BEGIN record, but we have
4897 * not yet read the payload. For non-dryrun sends
4898 * this will be done by the kernel, so we must
4899 * emulate that here, before attempting to read
4902 err = recv_read(hdl, infd, buf, drr->drr_payloadlen,
4903 flags->byteswap, NULL);
4908 err = recv_skip(hdl, infd, flags->byteswap);
4913 err = ioctl_err = lzc_receive_with_heal(destsnap, rcvprops,
4914 oxprops, wkeydata, wkeylen, origin, flags->force,
4915 flags->heal, flags->resumable, raw, infd, drr_noswap, -1,
4916 &read_bytes, &errflags, NULL, &prop_errors);
4918 err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
4919 oxprops, wkeydata, wkeylen, origin, flags->force,
4920 flags->resumable, raw, infd, drr_noswap, -1, &read_bytes,
4921 &errflags, NULL, &prop_errors);
4923 ioctl_errno = ioctl_err;
4924 prop_errflags = errflags;
4927 nvpair_t *prop_err = NULL;
4929 while ((prop_err = nvlist_next_nvpair(prop_errors,
4930 prop_err)) != NULL) {
4935 prop = zfs_name_to_prop(nvpair_name(prop_err));
4936 (void) nvpair_value_int32(prop_err, &intval);
4937 if (strcmp(nvpair_name(prop_err),
4938 ZPROP_N_MORE_ERRORS) == 0) {
4939 trunc_prop_errs(intval);
4941 } else if (snapname == NULL || finalsnap == NULL ||
4942 strcmp(finalsnap, snapname) == 0 ||
4943 strcmp(nvpair_name(prop_err),
4944 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
4946 * Skip the special case of, for example,
4947 * "refquota", errors on intermediate
4948 * snapshots leading up to a final one.
4949 * That's why we have all of the checks above.
4951 * See zfs_ioctl.c's extract_delay_props() for
4952 * a list of props which can fail on
4953 * intermediate snapshots, but shouldn't
4954 * affect the overall receive.
4956 (void) snprintf(tbuf, sizeof (tbuf),
4957 dgettext(TEXT_DOMAIN,
4958 "cannot receive %s property on %s"),
4959 nvpair_name(prop_err), name);
4960 zfs_setprop_error(hdl, prop, intval, tbuf);
4965 if (err == 0 && snapprops_nvlist) {
4966 zfs_cmd_t zc = {"\0"};
4968 (void) strlcpy(zc.zc_name, destsnap, sizeof (zc.zc_name));
4969 zc.zc_cookie = B_TRUE; /* received */
4970 zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist);
4971 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
4972 zcmd_free_nvlists(&zc);
4974 if (err == 0 && snapholds_nvlist) {
4976 nvlist_t *holds, *errors = NULL;
4977 int cleanup_fd = -1;
4979 VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
4980 for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
4982 pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
4983 fnvlist_add_string(holds, destsnap, nvpair_name(pair));
4985 (void) lzc_hold(holds, cleanup_fd, &errors);
4986 fnvlist_free(snapholds_nvlist);
4987 fnvlist_free(holds);
4990 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
4992 * It may be that this snapshot already exists,
4993 * in which case we want to consume & ignore it
4994 * rather than failing.
4996 avl_tree_t *local_avl;
4997 nvlist_t *local_nv, *fs;
4998 cp = strchr(destsnap, '@');
5001 * XXX Do this faster by just iterating over snaps in
5002 * this fs. Also if zc_value does not exist, we will
5003 * get a strange "does not exist" error message.
5006 if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
5007 B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE,
5008 B_TRUE, &local_nv, &local_avl) == 0) {
5010 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
5011 fsavl_destroy(local_avl);
5012 fnvlist_free(local_nv);
5015 if (flags->verbose) {
5016 (void) printf("snap %s already exists; "
5017 "ignoring\n", destsnap);
5019 err = ioctl_err = recv_skip(hdl, infd,
5026 if (ioctl_err != 0) {
5027 switch (ioctl_errno) {
5029 cp = strchr(destsnap, '@');
5031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5032 "most recent snapshot of %s does not\n"
5033 "match incremental source"), destsnap);
5034 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
5038 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5039 "destination %s has been modified\n"
5040 "since most recent snapshot"), name);
5041 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
5045 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5046 "key must be loaded to do a non-raw "
5047 "corrective recv on an encrypted "
5049 } else if (raw && stream_wantsnewfs) {
5050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5051 "failed to create encryption key"));
5052 } else if (raw && !stream_wantsnewfs) {
5053 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5054 "encryption key does not match "
5057 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5058 "inherited key must be loaded"));
5060 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
5063 cp = strchr(destsnap, '@');
5065 /* it's the containing fs that exists */
5068 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5069 "destination already exists"));
5070 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
5071 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
5076 if (flags->resumable) {
5077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5078 "kernel modules must be upgraded to "
5079 "receive this stream."));
5080 } else if (embedded && !raw) {
5081 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5082 "incompatible embedded data stream "
5083 "feature with encrypted receive."));
5085 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5088 case ZFS_ERR_STREAM_TRUNCATED:
5090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5091 "corrective receive was not able to "
5092 "reconstruct the data needed for "
5095 recv_ecksum_set_aux(hdl, destsnap,
5096 flags->resumable, ioctl_err == ECKSUM);
5097 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5099 case ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH:
5100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5101 "incremental send stream requires -L "
5102 "(--large-block), to match previous receive."));
5103 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5107 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5108 "stream is not compatible with the "
5109 "data in the pool."));
5111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5112 "pool must be upgraded to receive this "
5114 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5118 "destination %s space quota exceeded."), name);
5119 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
5121 case ZFS_ERR_FROM_IVSET_GUID_MISSING:
5122 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5123 "IV set guid missing. See errata %u at "
5124 "https://openzfs.github.io/openzfs-docs/msg/"
5126 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
5127 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5129 case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
5130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5131 "IV set guid mismatch. See the 'zfs receive' "
5132 "man page section\n discussing the limitations "
5133 "of raw encrypted send streams."));
5134 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5136 case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING:
5137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5138 "Spill block flag missing for raw send.\n"
5139 "The zfs software on the sending system must "
5141 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5143 case ZFS_ERR_RESUME_EXISTS:
5144 cp = strchr(destsnap, '@');
5146 /* it's the containing fs that exists */
5149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5150 "Resuming recv on existing dataset without force"));
5151 (void) zfs_error_fmt(hdl, EZFS_RESUME_EXISTS,
5152 dgettext(TEXT_DOMAIN, "cannot resume recv %s"),
5158 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5159 "destination %s contains "
5160 "partially-complete state from "
5161 "\"zfs receive -s\"."), name);
5162 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
5167 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
5172 * Mount the target filesystem (if created). Also mount any
5173 * children of the target filesystem if we did a replication
5174 * receive (indicated by stream_avl being non-NULL).
5177 if (!flags->nomount)
5178 err |= changelist_postfix(clp);
5179 changelist_free(clp);
5182 if ((newfs || stream_avl) && type == ZFS_TYPE_FILESYSTEM && !redacted)
5183 flags->domount = B_TRUE;
5185 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
5186 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
5187 "failed to clear unreceived properties on %s"), name);
5188 (void) fprintf(stderr, "\n");
5190 if (prop_errflags & ZPROP_ERR_NORESTORE) {
5191 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
5192 "failed to restore original properties on %s"), name);
5193 (void) fprintf(stderr, "\n");
5196 if (err || ioctl_err) {
5201 if (flags->verbose) {
5204 uint64_t bytes = read_bytes;
5205 struct timespec delta;
5206 clock_gettime(CLOCK_MONOTONIC_RAW, &delta);
5207 if (begin_time.tv_nsec > delta.tv_nsec) {
5209 1000000000 + delta.tv_nsec - begin_time.tv_nsec;
5212 delta.tv_nsec -= begin_time.tv_nsec;
5213 delta.tv_sec -= begin_time.tv_sec;
5214 if (delta.tv_sec == 0 && delta.tv_nsec == 0)
5216 double delta_f = delta.tv_sec + (delta.tv_nsec / 1e9);
5217 zfs_nicebytes(bytes, buf1, sizeof (buf1));
5218 zfs_nicebytes(bytes / delta_f, buf2, sizeof (buf2));
5220 (void) printf("received %s stream in %.2f seconds (%s/sec)\n",
5221 buf1, delta_f, buf2);
5226 if (prop_errors != NULL)
5227 fnvlist_free(prop_errors);
5229 if (tmp_keylocation[0] != '\0') {
5230 fnvlist_add_string(rcvprops,
5231 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation);
5235 fnvlist_free(rcvprops);
5237 fnvlist_free(oxprops);
5238 fnvlist_free(origprops);
5244 * Check properties we were asked to override (both -o|-x)
5247 zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
5250 nvpair_t *nvp = NULL;
5254 while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
5255 name = nvpair_name(nvp);
5256 prop = zfs_name_to_prop(name);
5258 if (prop == ZPROP_USERPROP) {
5259 if (!zfs_prop_user(name)) {
5260 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5261 "%s: invalid property '%s'"), errbuf, name);
5267 * "origin" is readonly but is used to receive datasets as
5268 * clones so we don't raise an error here
5270 if (prop == ZFS_PROP_ORIGIN)
5273 /* encryption params have their own verification later */
5274 if (prop == ZFS_PROP_ENCRYPTION ||
5275 zfs_prop_encryption_key_param(prop))
5279 * cannot override readonly, set-once and other specific
5280 * settable properties
5282 if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
5283 prop == ZFS_PROP_VOLSIZE) {
5284 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5285 "%s: invalid property '%s'"), errbuf, name);
5294 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
5295 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
5296 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs,
5297 const char *finalsnap, nvlist_t *cmdprops)
5300 dmu_replay_record_t drr, drr_noswap;
5301 struct drr_begin *drrb = &drr.drr_u.drr_begin;
5302 char errbuf[ERRBUFLEN];
5303 zio_cksum_t zcksum = { { 0 } };
5304 uint64_t featureflags;
5307 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5310 /* check cmdline props, raise an error if they cannot be received */
5311 if (!zfs_receive_checkprops(hdl, cmdprops, errbuf))
5312 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
5314 if (flags->isprefix &&
5315 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
5316 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
5317 "(%s) does not exist"), tosnap);
5318 return (zfs_error(hdl, EZFS_NOENT, errbuf));
5321 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
5322 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
5323 "(%s) does not exist"), originsnap);
5324 return (zfs_error(hdl, EZFS_NOENT, errbuf));
5327 /* read in the BEGIN record */
5328 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
5332 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
5333 /* It's the double end record at the end of a package */
5337 /* the kernel needs the non-byteswapped begin record */
5340 flags->byteswap = B_FALSE;
5341 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
5343 * We computed the checksum in the wrong byteorder in
5344 * recv_read() above; do it again correctly.
5346 memset(&zcksum, 0, sizeof (zio_cksum_t));
5347 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
5348 flags->byteswap = B_TRUE;
5350 drr.drr_type = BSWAP_32(drr.drr_type);
5351 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
5352 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
5353 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
5354 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
5355 drrb->drr_type = BSWAP_32(drrb->drr_type);
5356 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
5357 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
5358 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
5361 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
5362 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5363 "stream (bad magic number)"));
5364 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5367 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
5368 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
5370 if (!DMU_STREAM_SUPPORTED(featureflags) ||
5371 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
5373 * Let's be explicit about this one, since rather than
5374 * being a new feature we can't know, it's an old
5375 * feature we dropped.
5377 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
5378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5379 "stream has deprecated feature: dedup, try "
5380 "'zstream redup [send in a file] | zfs recv "
5383 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5384 "stream has unsupported feature, feature flags = "
5385 "%llx (unknown flags = %llx)"),
5386 (u_longlong_t)featureflags,
5387 (u_longlong_t)((featureflags) &
5388 ~DMU_BACKUP_FEATURE_MASK));
5390 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5393 /* Holds feature is set once in the compound stream header. */
5394 if (featureflags & DMU_BACKUP_FEATURE_HOLDS)
5395 flags->holds = B_TRUE;
5397 if (strchr(drrb->drr_toname, '@') == NULL) {
5398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5399 "stream (bad snapshot name)"));
5400 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5403 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
5404 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
5405 if (sendfs == NULL) {
5407 * We were not called from zfs_receive_package(). Get
5408 * the fs specified by 'zfs send'.
5411 (void) strlcpy(nonpackage_sendfs,
5412 drr.drr_u.drr_begin.drr_toname,
5413 sizeof (nonpackage_sendfs));
5414 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
5416 sendfs = nonpackage_sendfs;
5417 VERIFY(finalsnap == NULL);
5419 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
5420 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
5421 finalsnap, cmdprops));
5423 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
5424 DMU_COMPOUNDSTREAM);
5425 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
5426 &zcksum, top_zfs, cmdprops));
5431 * Restores a backup of tosnap from the file descriptor specified by infd.
5432 * Return 0 on total success, -2 if some things couldn't be
5433 * destroyed/renamed/promoted, -1 if some things couldn't be received.
5434 * (-1 will override -2, if -1 and the resumable flag was specified the
5435 * transfer can be resumed if the sending side supports it).
5438 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
5439 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
5441 char *top_zfs = NULL;
5444 char *originsnap = NULL;
5447 * The only way fstat can fail is if we do not have a valid file
5450 if (fstat(infd, &sb) == -1) {
5456 err = nvlist_lookup_string(props, "origin", &originsnap);
5457 if (err && err != ENOENT)
5461 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
5462 stream_avl, &top_zfs, NULL, props);
5464 if (err == 0 && !flags->nomount && flags->domount && top_zfs) {
5465 zfs_handle_t *zhp = NULL;
5466 prop_changelist_t *clp = NULL;
5468 zhp = zfs_open(hdl, top_zfs,
5469 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5474 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
5479 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
5480 CL_GATHER_MOUNT_ALWAYS,
5481 flags->forceunmount ? MS_FORCE : 0);
5488 /* mount and share received datasets */
5489 err = changelist_postfix(clp);
5490 changelist_free(clp);