4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 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;
297 char *snapname = strrchr(zhp->zfs_name, '@') + 1;
298 const char *from = sd->fromsnap;
299 const char *to = sd->tosnap;
301 assert(snapname != (NULL + 1));
303 isfromsnap = (from != NULL && strcmp(from, snapname) == 0);
304 istosnap = (to != NULL && strcmp(to, snapname) == 0);
305 istosnapwithnofrom = (istosnap && from == NULL);
307 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
309 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
310 "skipping snapshot %s because it was created "
311 "after the destination snapshot (%s)\n"),
318 fnvlist_add_uint64(sd->parent_snaps, snapname, guid);
321 * NB: if there is no fromsnap here (it's a newly created fs in
322 * an incremental replication), we will substitute the tosnap.
324 if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap))
325 sd->parent_fromsnap_guid = guid;
327 if (!sd->recursive) {
329 * To allow a doall stream to work properly
330 * with a NULL fromsnap
332 if (sd->doall && from == NULL && !sd->seenfrom)
333 sd->seenfrom = B_TRUE;
335 if (!sd->seenfrom && isfromsnap) {
336 sd->seenfrom = B_TRUE;
341 if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) {
350 nvlist_t *nv = fnvlist_alloc();
351 send_iterate_prop(zhp, sd->backup, nv);
352 fnvlist_add_nvlist(sd->snapprops, snapname, nv);
357 if (lzc_get_holds(zhp->zfs_name, &holds) == 0) {
358 fnvlist_add_nvlist(sd->snapholds, snapname, holds);
368 * Collect all valid props from the handle snap into an nvlist.
371 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
376 props = zfs_get_recvd_props(zhp);
378 props = zhp->zfs_props;
380 nvpair_t *elem = NULL;
381 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
382 char *propname = nvpair_name(elem);
383 zfs_prop_t prop = zfs_name_to_prop(propname);
385 if (!zfs_prop_user(propname)) {
387 * Realistically, this should never happen. However,
388 * we want the ability to add DSL properties without
389 * needing to make incompatible version changes. We
390 * need to ignore unknown properties to allow older
391 * software to still send datasets containing these
392 * properties, with the unknown properties elided.
394 if (prop == ZPROP_INVAL)
397 if (zfs_prop_readonly(prop))
401 nvlist_t *propnv = fnvpair_value_nvlist(elem);
403 boolean_t isspacelimit = (prop == ZFS_PROP_QUOTA ||
404 prop == ZFS_PROP_RESERVATION ||
405 prop == ZFS_PROP_REFQUOTA ||
406 prop == ZFS_PROP_REFRESERVATION);
407 if (isspacelimit && zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
411 if (nvlist_lookup_string(propnv, ZPROP_SOURCE, &source) == 0) {
412 if (strcmp(source, zhp->zfs_name) != 0 &&
413 strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
417 * May have no source before SPA_VERSION_RECVD_PROPS,
418 * but is still modifiable.
424 if (zfs_prop_user(propname) ||
425 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
427 value = fnvlist_lookup_string(propnv, ZPROP_VALUE);
428 fnvlist_add_string(nv, propname, value);
431 value = fnvlist_lookup_uint64(propnv, ZPROP_VALUE);
432 fnvlist_add_uint64(nv, propname, value);
438 * returns snapshot creation txg
439 * and returns 0 if the snapshot does not exist
442 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
444 char name[ZFS_MAX_DATASET_NAME_LEN];
447 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
450 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
451 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
452 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
454 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
463 * Recursively generate nvlists describing datasets. See comment
464 * for the data structure send_data_t above for description of contents
468 send_iterate_fs(zfs_handle_t *zhp, void *arg)
470 send_data_t *sd = arg;
471 nvlist_t *nvfs = NULL, *nv = NULL;
473 uint64_t min_txg = 0, max_txg = 0;
474 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
475 uint64_t guid = zhp->zfs_dmustats.dds_guid;
476 uint64_t fromsnap_txg, tosnap_txg;
479 /* These fields are restored on return from a recursive call. */
480 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
481 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
482 uint64_t tosnap_txg_save = sd->tosnap_txg;
484 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
485 if (fromsnap_txg != 0)
486 sd->fromsnap_txg = fromsnap_txg;
488 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
490 sd->tosnap_txg = tosnap_txg;
493 * On the send side, if the current dataset does not have tosnap,
494 * perform two additional checks:
496 * - Skip sending the current dataset if it was created later than
498 * - Return error if the current dataset was created earlier than
499 * the parent tosnap, unless --skip-missing specified. Then
500 * just print a warning.
502 if (sd->tosnap != NULL && tosnap_txg == 0) {
503 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
505 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
506 "skipping dataset %s: snapshot %s does "
507 "not exist\n"), zhp->zfs_name, sd->tosnap);
509 } else if (sd->skipmissing) {
510 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
511 "WARNING: skipping dataset %s and its children:"
512 " snapshot %s does not exist\n"),
513 zhp->zfs_name, sd->tosnap);
515 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
516 "cannot send %s@%s%s: snapshot %s@%s does not "
517 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
518 dgettext(TEXT_DOMAIN, " recursively") : "",
519 zhp->zfs_name, sd->tosnap);
525 nvfs = fnvlist_alloc();
526 fnvlist_add_string(nvfs, "name", zhp->zfs_name);
527 fnvlist_add_uint64(nvfs, "parentfromsnap", sd->parent_fromsnap_guid);
529 if (zhp->zfs_dmustats.dds_origin[0] != '\0') {
530 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
531 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
532 if (origin == NULL) {
536 fnvlist_add_uint64(nvfs, "origin",
537 origin->zfs_dmustats.dds_guid);
541 /* Iterate over props. */
542 if (sd->props || sd->backup || sd->recursive) {
543 nv = fnvlist_alloc();
544 send_iterate_prop(zhp, sd->backup, nv);
545 fnvlist_add_nvlist(nvfs, "props", nv);
547 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
550 /* Determine if this dataset is an encryption root. */
551 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
557 fnvlist_add_boolean(nvfs, "is_encroot");
560 * Encrypted datasets can only be sent with properties if
561 * the raw flag is specified because the receive side doesn't
562 * currently have a mechanism for recursively asking the user
563 * for new encryption parameters.
566 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
567 "cannot send %s@%s: encrypted dataset %s may not "
568 "be sent with properties without the raw flag\n"),
569 sd->fsname, sd->tosnap, zhp->zfs_name);
577 * Iterate over snaps, and set sd->parent_fromsnap_guid.
579 * If this is a "doall" send, a replicate send or we're just trying
580 * to gather a list of previous snapshots, iterate through all the
581 * snaps in the txg range. Otherwise just look at the one we're
584 sd->parent_fromsnap_guid = 0;
585 sd->parent_snaps = fnvlist_alloc();
586 sd->snapprops = fnvlist_alloc();
588 sd->snapholds = fnvlist_alloc();
589 if (sd->doall || sd->replicate || sd->tosnap == NULL) {
590 if (!sd->replicate && fromsnap_txg != 0)
591 min_txg = fromsnap_txg;
592 if (!sd->replicate && tosnap_txg != 0)
593 max_txg = tosnap_txg;
594 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd,
597 char snapname[MAXPATHLEN];
600 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
601 zhp->zfs_name, sd->tosnap);
602 if (sd->fromsnap != NULL)
603 sd->seenfrom = B_TRUE;
604 snap = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
606 (void) send_iterate_snap(snap, sd);
609 fnvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps);
610 fnvlist_free(sd->parent_snaps);
611 fnvlist_add_nvlist(nvfs, "snapprops", sd->snapprops);
612 fnvlist_free(sd->snapprops);
614 fnvlist_add_nvlist(nvfs, "snapholds", sd->snapholds);
615 fnvlist_free(sd->snapholds);
618 /* Do not allow the size of the properties list to exceed the limit */
619 if ((fnvlist_size(nvfs) + fnvlist_size(sd->fss)) >
620 zhp->zfs_hdl->libzfs_max_nvlist) {
621 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
622 "warning: cannot send %s@%s: the size of the list of "
623 "snapshots and properties is too large to be received "
625 "Select a smaller number of snapshots to send.\n"),
626 zhp->zfs_name, sd->tosnap);
630 /* Add this fs to nvlist. */
631 (void) snprintf(guidstring, sizeof (guidstring),
632 "0x%llx", (longlong_t)guid);
633 fnvlist_add_nvlist(sd->fss, guidstring, nvfs);
635 /* Iterate over children. */
637 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
640 /* Restore saved fields. */
641 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
642 sd->fromsnap_txg = fromsnap_txg_save;
643 sd->tosnap_txg = tosnap_txg_save;
653 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
654 const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t doall,
655 boolean_t replicate, boolean_t skipmissing, boolean_t verbose,
656 boolean_t backup, boolean_t holds, boolean_t props, nvlist_t **nvlp,
660 send_data_t sd = { 0 };
663 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
665 return (EZFS_BADTYPE);
667 sd.fss = fnvlist_alloc();
669 sd.fromsnap = fromsnap;
671 sd.recursive = recursive;
674 sd.replicate = replicate;
675 sd.skipmissing = skipmissing;
676 sd.verbose = verbose;
681 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
682 fnvlist_free(sd.fss);
689 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
690 fnvlist_free(sd.fss);
700 * Routines specific to "zfs send"
702 typedef struct send_dump_data {
703 /* these are all just the short snapname (the part after the @) */
704 const char *fromsnap;
706 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
707 uint64_t prevsnap_obj;
708 boolean_t seenfrom, seento, replicate, doall, fromorigin;
709 boolean_t dryrun, parsable, progress, embed_data, std_out;
710 boolean_t large_block, compress, raw, holds;
716 snapfilter_cb_t *filter_cb;
719 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
726 zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
727 enum lzc_send_flags flags, uint64_t *spacep)
729 assert(snapname != NULL);
731 int error = lzc_send_space(snapname, from, flags, spacep);
736 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
737 "warning: cannot estimate space for '%s'"), snapname);
739 libzfs_handle_t *hdl = zhp->zfs_hdl;
742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
743 "not an earlier snapshot from the same fs"));
744 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
747 if (zfs_dataset_exists(hdl, snapname,
748 ZFS_TYPE_SNAPSHOT)) {
749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
750 "incremental source (%s) does not exist"),
753 return (zfs_error(hdl, EZFS_NOENT, errbuf));
767 zfs_error_aux(hdl, "%s", strerror(error));
768 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
771 return (zfs_standard_error(hdl, error, errbuf));
776 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
777 * NULL) to the file descriptor specified by outfd.
780 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
781 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
784 zfs_cmd_t zc = {"\0"};
785 libzfs_handle_t *hdl = zhp->zfs_hdl;
788 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
789 assert(fromsnap_obj == 0 || !fromorigin);
791 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
792 zc.zc_cookie = outfd;
793 zc.zc_obj = fromorigin;
794 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
795 zc.zc_fromobj = fromsnap_obj;
798 if (debugnv != NULL) {
799 thisdbg = fnvlist_alloc();
800 if (fromsnap != NULL && fromsnap[0] != '\0')
801 fnvlist_add_string(thisdbg, "fromsnap", fromsnap);
804 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
808 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
809 "warning: cannot send '%s'"), zhp->zfs_name);
811 if (debugnv != NULL) {
812 fnvlist_add_uint64(thisdbg, "error", error);
813 fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
814 fnvlist_free(thisdbg);
819 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
820 "not an earlier snapshot from the same fs"));
821 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
824 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
825 "source key must be loaded"));
826 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
829 if (zfs_dataset_exists(hdl, zc.zc_name,
830 ZFS_TYPE_SNAPSHOT)) {
831 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
832 "incremental source (@%s) does not exist"),
835 return (zfs_error(hdl, EZFS_NOENT, errbuf));
849 zfs_error_aux(hdl, "%s", strerror(errno));
850 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
853 return (zfs_standard_error(hdl, errno, errbuf));
857 if (debugnv != NULL) {
858 fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
859 fnvlist_free(thisdbg);
866 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
868 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
871 * zfs_send() only sets snapholds for sends that need them,
872 * e.g. replication and doall.
874 if (sdd->snapholds == NULL)
877 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
881 zfs_send_progress(zfs_handle_t *zhp, int fd, uint64_t *bytes_written,
882 uint64_t *blocks_visited)
884 zfs_cmd_t zc = {"\0"};
886 if (bytes_written != NULL)
888 if (blocks_visited != NULL)
890 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
892 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
894 if (bytes_written != NULL)
895 *bytes_written = zc.zc_cookie;
896 if (blocks_visited != NULL)
897 *blocks_visited = zc.zc_objset_type;
902 send_progress_thread(void *arg)
904 progress_arg_t *pa = arg;
905 zfs_handle_t *zhp = pa->pa_zhp;
913 if (!pa->pa_parsable) {
914 (void) fprintf(stderr,
915 "TIME %s %sSNAPSHOT %s\n",
916 pa->pa_estimate ? "BYTES" : " SENT",
917 pa->pa_verbosity >= 2 ? " BLOCKS " : "",
922 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
926 if ((err = zfs_send_progress(zhp, pa->pa_fd, &bytes,
928 if (err == EINTR || err == ENOENT)
930 return ((void *)(uintptr_t)err);
936 if (pa->pa_verbosity >= 2 && pa->pa_parsable) {
937 (void) fprintf(stderr,
938 "%02d:%02d:%02d\t%llu\t%llu\t%s\n",
939 tm->tm_hour, tm->tm_min, tm->tm_sec,
940 (u_longlong_t)bytes, (u_longlong_t)blocks,
942 } else if (pa->pa_verbosity >= 2) {
943 zfs_nicenum(bytes, buf, sizeof (buf));
944 (void) fprintf(stderr,
945 "%02d:%02d:%02d %5s %8llu %s\n",
946 tm->tm_hour, tm->tm_min, tm->tm_sec,
947 buf, (u_longlong_t)blocks, zhp->zfs_name);
948 } else if (pa->pa_parsable) {
949 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
950 tm->tm_hour, tm->tm_min, tm->tm_sec,
951 (u_longlong_t)bytes, zhp->zfs_name);
953 zfs_nicebytes(bytes, buf, sizeof (buf));
954 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
955 tm->tm_hour, tm->tm_min, tm->tm_sec,
962 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
963 uint64_t size, boolean_t parsable)
966 if (fromsnap != NULL) {
967 (void) fprintf(fout, "incremental\t%s\t%s",
970 (void) fprintf(fout, "full\t%s",
974 if (fromsnap != NULL) {
975 if (strchr(fromsnap, '@') == NULL &&
976 strchr(fromsnap, '#') == NULL) {
977 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
978 "send from @%s to %s"),
981 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
982 "send from %s to %s"),
986 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
993 (void) fprintf(fout, "\t%llu",
995 } else if (size != 0) {
997 zfs_nicebytes(size, buf, sizeof (buf));
998 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
999 " estimated size is %s"), buf);
1001 (void) fprintf(fout, "\n");
1005 dump_snapshot(zfs_handle_t *zhp, void *arg)
1007 send_dump_data_t *sdd = arg;
1008 progress_arg_t pa = { 0 };
1011 enum lzc_send_flags flags = 0;
1013 boolean_t isfromsnap, istosnap, fromorigin;
1014 boolean_t exclude = B_FALSE;
1015 FILE *fout = sdd->std_out ? stdout : stderr;
1018 thissnap = strchr(zhp->zfs_name, '@') + 1;
1019 isfromsnap = (sdd->fromsnap != NULL &&
1020 strcmp(sdd->fromsnap, thissnap) == 0);
1022 if (!sdd->seenfrom && isfromsnap) {
1023 gather_holds(zhp, sdd);
1024 sdd->seenfrom = B_TRUE;
1025 (void) strlcpy(sdd->prevsnap, thissnap,
1026 sizeof (sdd->prevsnap));
1027 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1032 if (sdd->seento || !sdd->seenfrom) {
1037 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1039 sdd->seento = B_TRUE;
1041 if (sdd->large_block)
1042 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1043 if (sdd->embed_data)
1044 flags |= LZC_SEND_FLAG_EMBED_DATA;
1046 flags |= LZC_SEND_FLAG_COMPRESS;
1048 flags |= LZC_SEND_FLAG_RAW;
1050 if (!sdd->doall && !isfromsnap && !istosnap) {
1051 if (sdd->replicate) {
1053 nvlist_t *snapprops;
1055 * Filter out all intermediate snapshots except origin
1056 * snapshots needed to replicate clones.
1058 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1059 zhp->zfs_dmustats.dds_guid, &snapname);
1062 snapprops = fnvlist_lookup_nvlist(nvfs,
1064 snapprops = fnvlist_lookup_nvlist(snapprops,
1066 exclude = !nvlist_exists(snapprops,
1075 * If a filter function exists, call it to determine whether
1076 * this snapshot will be sent.
1078 if (exclude || (sdd->filter_cb != NULL &&
1079 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1081 * This snapshot is filtered out. Don't send it, and don't
1082 * set prevsnap_obj, so it will be as if this snapshot didn't
1083 * exist, and the next accepted snapshot will be sent as
1084 * an incremental from the last accepted one, or as the
1085 * first (and full) snapshot in the case of a replication,
1086 * non-incremental send.
1092 gather_holds(zhp, sdd);
1093 fromorigin = sdd->prevsnap[0] == '\0' &&
1094 (sdd->fromorigin || sdd->replicate);
1096 if (sdd->verbosity != 0) {
1098 char fromds[ZFS_MAX_DATASET_NAME_LEN];
1100 if (sdd->prevsnap[0] != '\0') {
1101 (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1102 *(strchr(fromds, '@') + 1) = '\0';
1103 (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1105 if (zfs_send_space(zhp, zhp->zfs_name,
1106 sdd->prevsnap[0] ? fromds : NULL, flags, &size) != 0) {
1107 size = 0; /* cannot estimate send space */
1109 send_print_verbose(fout, zhp->zfs_name,
1110 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1111 size, sdd->parsable);
1118 * If progress reporting is requested, spawn a new thread to
1119 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1121 if (sdd->progress) {
1123 pa.pa_fd = sdd->outfd;
1124 pa.pa_parsable = sdd->parsable;
1125 pa.pa_estimate = B_FALSE;
1126 pa.pa_verbosity = sdd->verbosity;
1128 if ((err = pthread_create(&tid, NULL,
1129 send_progress_thread, &pa)) != 0) {
1135 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1136 fromorigin, sdd->outfd, flags, sdd->debugnv);
1138 if (sdd->progress) {
1139 void *status = NULL;
1140 (void) pthread_cancel(tid);
1141 (void) pthread_join(tid, &status);
1142 int error = (int)(uintptr_t)status;
1143 if (error != 0 && status != PTHREAD_CANCELED) {
1145 (void) snprintf(errbuf, sizeof (errbuf),
1146 dgettext(TEXT_DOMAIN,
1147 "progress thread exited nonzero"));
1148 return (zfs_standard_error(zhp->zfs_hdl, error,
1154 (void) strcpy(sdd->prevsnap, thissnap);
1155 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1161 dump_filesystem(zfs_handle_t *zhp, void *arg)
1164 send_dump_data_t *sdd = arg;
1165 boolean_t missingfrom = B_FALSE;
1166 zfs_cmd_t zc = {"\0"};
1167 uint64_t min_txg = 0, max_txg = 0;
1169 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1170 zhp->zfs_name, sdd->tosnap);
1171 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1172 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1173 "WARNING: could not send %s@%s: does not exist\n"),
1174 zhp->zfs_name, sdd->tosnap);
1179 if (sdd->replicate && sdd->fromsnap) {
1181 * If this fs does not have fromsnap, and we're doing
1182 * recursive, we need to send a full stream from the
1183 * beginning (or an incremental from the origin if this
1184 * is a clone). If we're doing non-recursive, then let
1185 * them get the error.
1187 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1188 zhp->zfs_name, sdd->fromsnap);
1189 if (zfs_ioctl(zhp->zfs_hdl,
1190 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1191 missingfrom = B_TRUE;
1195 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1196 sdd->prevsnap_obj = 0;
1197 if (sdd->fromsnap == NULL || missingfrom)
1198 sdd->seenfrom = B_TRUE;
1203 * Iterate through all snapshots and process the ones we will be
1204 * sending. If we only have a "from" and "to" snapshot to deal
1205 * with, we can avoid iterating through all the other snapshots.
1207 if (sdd->doall || sdd->replicate || sdd->tosnap == NULL) {
1208 if (!sdd->replicate && sdd->fromsnap != NULL)
1209 min_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name,
1211 if (!sdd->replicate && sdd->tosnap != NULL)
1212 max_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name,
1214 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg,
1217 char snapname[MAXPATHLEN] = { 0 };
1220 if (!sdd->seenfrom) {
1221 (void) snprintf(snapname, sizeof (snapname),
1222 "%s@%s", zhp->zfs_name, sdd->fromsnap);
1223 snap = zfs_open(zhp->zfs_hdl, snapname,
1226 rv = dump_snapshot(snap, sdd);
1232 (void) snprintf(snapname, sizeof (snapname),
1233 "%s@%s", zhp->zfs_name, sdd->tosnap);
1234 snap = zfs_open(zhp->zfs_hdl, snapname,
1237 rv = dump_snapshot(snap, sdd);
1243 if (!sdd->seenfrom) {
1244 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1245 "WARNING: could not send %s@%s:\n"
1246 "incremental source (%s@%s) does not exist\n"),
1247 zhp->zfs_name, sdd->tosnap,
1248 zhp->zfs_name, sdd->fromsnap);
1250 } else if (!sdd->seento) {
1251 if (sdd->fromsnap) {
1252 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1253 "WARNING: could not send %s@%s:\n"
1254 "incremental source (%s@%s) "
1255 "is not earlier than it\n"),
1256 zhp->zfs_name, sdd->tosnap,
1257 zhp->zfs_name, sdd->fromsnap);
1259 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1261 "could not send %s@%s: does not exist\n"),
1262 zhp->zfs_name, sdd->tosnap);
1271 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1273 send_dump_data_t *sdd = arg;
1275 boolean_t needagain, progress;
1277 if (!sdd->replicate)
1278 return (dump_filesystem(rzhp, sdd));
1280 /* Mark the clone origin snapshots. */
1281 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1282 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1284 uint64_t origin_guid = 0;
1286 nvfs = fnvpair_value_nvlist(fspair);
1287 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1288 if (origin_guid != 0) {
1290 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1291 origin_guid, &snapname);
1292 if (origin_nv != NULL) {
1293 nvlist_t *snapprops;
1294 snapprops = fnvlist_lookup_nvlist(origin_nv,
1296 snapprops = fnvlist_lookup_nvlist(snapprops,
1298 fnvlist_add_boolean(snapprops,
1304 needagain = progress = B_FALSE;
1305 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1306 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1307 nvlist_t *fslist, *parent_nv;
1311 uint64_t origin_guid = 0;
1312 uint64_t parent_guid = 0;
1314 fslist = fnvpair_value_nvlist(fspair);
1315 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1318 fsname = fnvlist_lookup_string(fslist, "name");
1319 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1320 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1323 if (parent_guid != 0) {
1324 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1325 if (!nvlist_exists(parent_nv, "sent")) {
1326 /* parent has not been sent; skip this one */
1332 if (origin_guid != 0) {
1333 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1335 if (origin_nv != NULL &&
1336 !nvlist_exists(origin_nv, "sent")) {
1338 * origin has not been sent yet;
1346 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1349 err = dump_filesystem(zhp, sdd);
1350 fnvlist_add_boolean(fslist, "sent");
1361 /* clean out the sent flags in case we reuse this fss */
1362 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1363 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1366 fslist = fnvpair_value_nvlist(fspair);
1367 (void) nvlist_remove_all(fslist, "sent");
1374 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1376 unsigned int version;
1378 unsigned long long checksum, packed_len;
1381 * Decode token header, which is:
1382 * <token version>-<checksum of payload>-<uncompressed payload length>
1383 * Note that the only supported token version is 1.
1385 nread = sscanf(token, "%u-%llx-%llx-",
1386 &version, &checksum, &packed_len);
1388 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1389 "resume token is corrupt (invalid format)"));
1393 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1394 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1395 "resume token is corrupt (invalid version %u)"),
1400 /* convert hexadecimal representation to binary */
1401 token = strrchr(token, '-') + 1;
1402 int len = strlen(token) / 2;
1403 unsigned char *compressed = zfs_alloc(hdl, len);
1404 for (i = 0; i < len; i++) {
1405 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1408 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1409 "resume token is corrupt "
1410 "(payload is not hex-encoded)"));
1415 /* verify checksum */
1417 fletcher_4_native_varsize(compressed, len, &cksum);
1418 if (cksum.zc_word[0] != checksum) {
1420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1421 "resume token is corrupt (incorrect checksum)"));
1426 void *packed = zfs_alloc(hdl, packed_len);
1427 uLongf packed_len_long = packed_len;
1428 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1429 packed_len_long != packed_len) {
1432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1433 "resume token is corrupt (decompression failed)"));
1439 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1444 "resume token is corrupt (nvlist_unpack failed)"));
1449 static enum lzc_send_flags
1450 lzc_flags_from_sendflags(const sendflags_t *flags)
1452 enum lzc_send_flags lzc_flags = 0;
1453 if (flags->largeblock)
1454 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1455 if (flags->embed_data)
1456 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1457 if (flags->compress)
1458 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1460 lzc_flags |= LZC_SEND_FLAG_RAW;
1462 lzc_flags |= LZC_SEND_FLAG_SAVED;
1467 estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
1468 uint64_t resumeobj, uint64_t resumeoff, uint64_t bytes,
1469 const char *redactbook, char *errbuf)
1472 FILE *fout = flags->dryrun ? stdout : stderr;
1473 progress_arg_t pa = { 0 };
1477 if (flags->progress) {
1480 pa.pa_parsable = flags->parsable;
1481 pa.pa_estimate = B_TRUE;
1482 pa.pa_verbosity = flags->verbosity;
1484 err = pthread_create(&ptid, NULL,
1485 send_progress_thread, &pa);
1487 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
1488 return (zfs_error(zhp->zfs_hdl,
1489 EZFS_THREADCREATEFAILED, errbuf));
1493 err = lzc_send_space_resume_redacted(zhp->zfs_name, from,
1494 lzc_flags_from_sendflags(flags), resumeobj, resumeoff, bytes,
1495 redactbook, fd, &size);
1497 if (flags->progress) {
1498 void *status = NULL;
1499 (void) pthread_cancel(ptid);
1500 (void) pthread_join(ptid, &status);
1501 int error = (int)(uintptr_t)status;
1502 if (error != 0 && status != PTHREAD_CANCELED) {
1504 (void) snprintf(errbuf, sizeof (errbuf),
1505 dgettext(TEXT_DOMAIN, "progress thread exited "
1507 return (zfs_standard_error(zhp->zfs_hdl, error,
1513 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
1514 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
1517 send_print_verbose(fout, zhp->zfs_name, from, size,
1520 if (flags->parsable) {
1521 (void) fprintf(fout, "size\t%llu\n", (longlong_t)size);
1524 zfs_nicenum(size, buf, sizeof (buf));
1525 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1526 "total estimated size is %s\n"), buf);
1532 redact_snaps_contains(const uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
1534 for (int i = 0; i < num_snaps; i++) {
1535 if (snaps[i] == guid)
1542 redact_snaps_equal(const uint64_t *snaps1, uint64_t num_snaps1,
1543 const uint64_t *snaps2, uint64_t num_snaps2)
1545 if (num_snaps1 != num_snaps2)
1547 for (int i = 0; i < num_snaps1; i++) {
1548 if (!redact_snaps_contains(snaps2, num_snaps2, snaps1[i]))
1555 * Check that the list of redaction snapshots in the bookmark matches the send
1556 * we're resuming, and return whether or not it's complete.
1558 * Note that the caller needs to free the contents of *bookname with free() if
1559 * this function returns successfully.
1562 find_redact_book(libzfs_handle_t *hdl, const char *path,
1563 const uint64_t *redact_snap_guids, int num_redact_snaps,
1568 nvlist_t *props = fnvlist_alloc();
1571 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1572 "cannot resume send"));
1574 fnvlist_add_boolean(props, "redact_complete");
1575 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1576 error = lzc_get_bookmarks(path, props, &bmarks);
1577 fnvlist_free(props);
1579 if (error == ESRCH) {
1580 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1581 "nonexistent redaction bookmark provided"));
1582 } else if (error == ENOENT) {
1583 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1584 "dataset to be sent no longer exists"));
1586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1587 "unknown error: %s"), strerror(error));
1589 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1592 for (pair = nvlist_next_nvpair(bmarks, NULL); pair;
1593 pair = nvlist_next_nvpair(bmarks, pair)) {
1595 nvlist_t *bmark = fnvpair_value_nvlist(pair);
1596 nvlist_t *vallist = fnvlist_lookup_nvlist(bmark,
1597 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1599 uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist,
1601 if (redact_snaps_equal(redact_snap_guids,
1602 num_redact_snaps, bmarksnaps, len)) {
1607 fnvlist_free(bmarks);
1608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1609 "no appropriate redaction bookmark exists"));
1610 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1612 char *name = nvpair_name(pair);
1613 nvlist_t *bmark = fnvpair_value_nvlist(pair);
1614 nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete");
1615 boolean_t complete = fnvlist_lookup_boolean_value(vallist,
1618 fnvlist_free(bmarks);
1619 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1620 "incomplete redaction bookmark provided"));
1621 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1623 *bookname = strndup(name, ZFS_MAX_DATASET_NAME_LEN);
1624 ASSERT3P(*bookname, !=, NULL);
1625 fnvlist_free(bmarks);
1630 zfs_send_resume_impl(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1631 nvlist_t *resume_nvl)
1635 char *fromname = NULL;
1636 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1639 char name[ZFS_MAX_DATASET_NAME_LEN];
1640 enum lzc_send_flags lzc_flags = 0;
1641 FILE *fout = (flags->verbosity > 0 && flags->dryrun) ? stdout : stderr;
1642 uint64_t *redact_snap_guids = NULL;
1643 int num_redact_snaps = 0;
1644 char *redact_book = NULL;
1646 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1647 "cannot resume send"));
1649 if (flags->verbosity != 0) {
1650 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1651 "resume token contents:\n"));
1652 nvlist_print(fout, resume_nvl);
1655 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1656 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1657 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1658 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1659 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1660 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1661 "resume token is corrupt"));
1662 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1665 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1667 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1668 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1669 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1670 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1671 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1672 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1673 if (flags->raw || nvlist_exists(resume_nvl, "rawok"))
1674 lzc_flags |= LZC_SEND_FLAG_RAW;
1675 if (flags->saved || nvlist_exists(resume_nvl, "savedok"))
1676 lzc_flags |= LZC_SEND_FLAG_SAVED;
1679 (void) strcpy(name, toname);
1681 error = guid_to_name(hdl, toname, toguid, B_FALSE, name);
1683 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1684 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1685 "'%s' is no longer the same snapshot "
1686 "used in the initial send"), toname);
1688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1689 "'%s' used in the initial send no "
1690 "longer exists"), toname);
1692 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1696 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1698 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1699 "unable to access '%s'"), name);
1700 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1703 if (nvlist_lookup_uint64_array(resume_nvl, "book_redact_snaps",
1704 &redact_snap_guids, (uint_t *)&num_redact_snaps) != 0) {
1705 num_redact_snaps = -1;
1708 if (fromguid != 0) {
1709 if (guid_to_name_redact_snaps(hdl, toname, fromguid, B_TRUE,
1710 redact_snap_guids, num_redact_snaps, name) != 0) {
1711 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1712 "incremental source %#llx no longer exists"),
1713 (longlong_t)fromguid);
1714 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1719 redact_snap_guids = NULL;
1721 if (nvlist_lookup_uint64_array(resume_nvl,
1722 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &redact_snap_guids,
1723 (uint_t *)&num_redact_snaps) == 0) {
1724 char path[ZFS_MAX_DATASET_NAME_LEN];
1726 (void) strlcpy(path, toname, sizeof (path));
1727 char *at = strchr(path, '@');
1728 ASSERT3P(at, !=, NULL);
1732 if ((error = find_redact_book(hdl, path, redact_snap_guids,
1733 num_redact_snaps, &redact_book)) != 0) {
1738 if (flags->verbosity != 0) {
1740 * Some of these may have come from the resume token, set them
1741 * here for size estimate purposes.
1743 sendflags_t tmpflags = *flags;
1744 if (lzc_flags & LZC_SEND_FLAG_LARGE_BLOCK)
1745 tmpflags.largeblock = B_TRUE;
1746 if (lzc_flags & LZC_SEND_FLAG_COMPRESS)
1747 tmpflags.compress = B_TRUE;
1748 if (lzc_flags & LZC_SEND_FLAG_EMBED_DATA)
1749 tmpflags.embed_data = B_TRUE;
1750 if (lzc_flags & LZC_SEND_FLAG_RAW)
1751 tmpflags.raw = B_TRUE;
1752 if (lzc_flags & LZC_SEND_FLAG_SAVED)
1753 tmpflags.saved = B_TRUE;
1754 error = estimate_size(zhp, fromname, outfd, &tmpflags,
1755 resumeobj, resumeoff, bytes, redact_book, errbuf);
1758 if (!flags->dryrun) {
1759 progress_arg_t pa = { 0 };
1762 * If progress reporting is requested, spawn a new thread to
1763 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1765 if (flags->progress) {
1768 pa.pa_parsable = flags->parsable;
1769 pa.pa_estimate = B_FALSE;
1770 pa.pa_verbosity = flags->verbosity;
1772 error = pthread_create(&tid, NULL,
1773 send_progress_thread, &pa);
1775 if (redact_book != NULL)
1782 error = lzc_send_resume_redacted(zhp->zfs_name, fromname, outfd,
1783 lzc_flags, resumeobj, resumeoff, redact_book);
1784 if (redact_book != NULL)
1787 if (flags->progress) {
1788 void *status = NULL;
1789 (void) pthread_cancel(tid);
1790 (void) pthread_join(tid, &status);
1791 int error = (int)(uintptr_t)status;
1792 if (error != 0 && status != PTHREAD_CANCELED) {
1794 (void) snprintf(errbuf, sizeof (errbuf),
1795 dgettext(TEXT_DOMAIN,
1796 "progress thread exited nonzero"));
1797 return (zfs_standard_error(hdl, error, errbuf));
1802 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1803 "warning: cannot send '%s'"), zhp->zfs_name);
1811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1812 "source key must be loaded"));
1813 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1815 if (lzc_exists(zhp->zfs_name)) {
1816 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1817 "incremental source could not be found"));
1819 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1834 zfs_error_aux(hdl, "%s", strerror(errno));
1835 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1838 return (zfs_standard_error(hdl, errno, errbuf));
1841 if (redact_book != NULL)
1851 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1852 const char *resume_token)
1856 nvlist_t *resume_nvl;
1858 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1859 "cannot resume send"));
1861 resume_nvl = zfs_send_resume_token_to_nvlist(hdl, resume_token);
1862 if (resume_nvl == NULL) {
1864 * zfs_error_aux has already been set by
1865 * zfs_send_resume_token_to_nvlist()
1867 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1870 ret = zfs_send_resume_impl(hdl, flags, outfd, resume_nvl);
1871 fnvlist_free(resume_nvl);
1877 zfs_send_saved(zfs_handle_t *zhp, sendflags_t *flags, int outfd,
1878 const char *resume_token)
1881 libzfs_handle_t *hdl = zhp->zfs_hdl;
1882 nvlist_t *saved_nvl = NULL, *resume_nvl = NULL;
1883 uint64_t saved_guid = 0, resume_guid = 0;
1884 uint64_t obj = 0, off = 0, bytes = 0;
1885 char token_buf[ZFS_MAXPROPLEN];
1888 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1889 "saved send failed"));
1891 ret = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1892 token_buf, sizeof (token_buf), NULL, NULL, 0, B_TRUE);
1896 saved_nvl = zfs_send_resume_token_to_nvlist(hdl, token_buf);
1897 if (saved_nvl == NULL) {
1899 * zfs_error_aux has already been set by
1900 * zfs_send_resume_token_to_nvlist()
1902 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
1907 * If a resume token is provided we use the object and offset
1908 * from that instead of the default, which starts from the
1911 if (resume_token != NULL) {
1912 resume_nvl = zfs_send_resume_token_to_nvlist(hdl,
1914 if (resume_nvl == NULL) {
1915 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
1919 if (nvlist_lookup_uint64(resume_nvl, "object", &obj) != 0 ||
1920 nvlist_lookup_uint64(resume_nvl, "offset", &off) != 0 ||
1921 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1922 nvlist_lookup_uint64(resume_nvl, "toguid",
1923 &resume_guid) != 0) {
1924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1925 "provided resume token is corrupt"));
1926 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
1930 if (nvlist_lookup_uint64(saved_nvl, "toguid",
1932 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1933 "dataset's resume token is corrupt"));
1934 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
1938 if (resume_guid != saved_guid) {
1939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1940 "provided resume token does not match dataset"));
1941 ret = zfs_error(hdl, EZFS_BADBACKUP, errbuf);
1946 (void) nvlist_remove_all(saved_nvl, "object");
1947 fnvlist_add_uint64(saved_nvl, "object", obj);
1949 (void) nvlist_remove_all(saved_nvl, "offset");
1950 fnvlist_add_uint64(saved_nvl, "offset", off);
1952 (void) nvlist_remove_all(saved_nvl, "bytes");
1953 fnvlist_add_uint64(saved_nvl, "bytes", bytes);
1955 (void) nvlist_remove_all(saved_nvl, "toname");
1956 fnvlist_add_string(saved_nvl, "toname", zhp->zfs_name);
1958 ret = zfs_send_resume_impl(hdl, flags, outfd, saved_nvl);
1961 fnvlist_free(saved_nvl);
1962 fnvlist_free(resume_nvl);
1967 * This function informs the target system that the recursive send is complete.
1968 * The record is also expected in the case of a send -p.
1971 send_conclusion_record(int fd, zio_cksum_t *zc)
1973 dmu_replay_record_t drr = { 0 };
1974 drr.drr_type = DRR_END;
1976 drr.drr_u.drr_end.drr_checksum = *zc;
1977 if (write(fd, &drr, sizeof (drr)) == -1) {
1984 * This function is responsible for sending the records that contain the
1985 * necessary information for the target system's libzfs to be able to set the
1986 * properties of the filesystem being received, or to be able to prepare for
1987 * a recursive receive.
1989 * The "zhp" argument is the handle of the snapshot we are sending
1990 * (the "tosnap"). The "from" argument is the short snapshot name (the part
1991 * after the @) of the incremental source.
1994 send_prelim_records(zfs_handle_t *zhp, const char *from, int fd,
1995 boolean_t gather_props, boolean_t recursive, boolean_t verbose,
1996 boolean_t dryrun, boolean_t raw, boolean_t replicate, boolean_t skipmissing,
1997 boolean_t backup, boolean_t holds, boolean_t props, boolean_t doall,
1998 nvlist_t **fssp, avl_tree_t **fsavlp)
2001 char *packbuf = NULL;
2003 zio_cksum_t zc = { {0} };
2004 int featureflags = 0;
2005 /* name of filesystem/volume that contains snapshot we are sending */
2006 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2007 /* short name of snap we are sending */
2011 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2012 "warning: cannot send '%s'"), zhp->zfs_name);
2013 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && zfs_prop_get_int(zhp,
2014 ZFS_PROP_VERSION) >= ZPL_VERSION_SA) {
2015 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
2019 featureflags |= DMU_BACKUP_FEATURE_HOLDS;
2021 (void) strlcpy(tofs, zhp->zfs_name, ZFS_MAX_DATASET_NAME_LEN);
2022 char *at = strchr(tofs, '@');
2029 nvlist_t *hdrnv = fnvlist_alloc();
2030 nvlist_t *fss = NULL;
2033 fnvlist_add_string(hdrnv, "fromsnap", from);
2034 fnvlist_add_string(hdrnv, "tosnap", tosnap);
2036 fnvlist_add_boolean(hdrnv, "not_recursive");
2039 fnvlist_add_boolean(hdrnv, "raw");
2042 if ((err = gather_nvlist(zhp->zfs_hdl, tofs,
2043 from, tosnap, recursive, raw, doall, replicate, skipmissing,
2044 verbose, backup, holds, props, &fss, fsavlp)) != 0) {
2045 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2049 * Do not allow the size of the properties list to exceed
2052 if ((fnvlist_size(fss) + fnvlist_size(hdrnv)) >
2053 zhp->zfs_hdl->libzfs_max_nvlist) {
2054 (void) snprintf(errbuf, sizeof (errbuf),
2055 dgettext(TEXT_DOMAIN, "warning: cannot send '%s': "
2056 "the size of the list of snapshots and properties "
2057 "is too large to be received successfully.\n"
2058 "Select a smaller number of snapshots to send.\n"),
2060 return (zfs_error(zhp->zfs_hdl, EZFS_NOSPC,
2063 fnvlist_add_nvlist(hdrnv, "fss", fss);
2064 VERIFY0(nvlist_pack(hdrnv, &packbuf, &buflen, NV_ENCODE_XDR,
2071 fnvlist_free(hdrnv);
2075 dmu_replay_record_t drr = { 0 };
2076 /* write first begin record */
2077 drr.drr_type = DRR_BEGIN;
2078 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
2079 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
2080 drr_versioninfo, DMU_COMPOUNDSTREAM);
2081 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
2082 drr_versioninfo, featureflags);
2083 if (snprintf(drr.drr_u.drr_begin.drr_toname,
2084 sizeof (drr.drr_u.drr_begin.drr_toname), "%s@%s", tofs,
2085 tosnap) >= sizeof (drr.drr_u.drr_begin.drr_toname)) {
2086 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2089 drr.drr_payloadlen = buflen;
2091 err = dump_record(&drr, packbuf, buflen, &zc, fd);
2094 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
2095 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2098 err = send_conclusion_record(fd, &zc);
2100 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
2101 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2109 * Generate a send stream. The "zhp" argument is the filesystem/volume
2110 * that contains the snapshot to send. The "fromsnap" argument is the
2111 * short name (the part after the '@') of the snapshot that is the
2112 * incremental source to send from (if non-NULL). The "tosnap" argument
2113 * is the short name of the snapshot to send.
2115 * The content of the send stream is the snapshot identified by
2116 * 'tosnap'. Incremental streams are requested in two ways:
2117 * - from the snapshot identified by "fromsnap" (if non-null) or
2118 * - from the origin of the dataset identified by zhp, which must
2119 * be a clone. In this case, "fromsnap" is null and "fromorigin"
2122 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
2123 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
2124 * if "replicate" is set. If "doall" is set, dump all the intermediate
2125 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
2126 * case too. If "props" is set, send properties.
2129 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2130 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2131 void *cb_arg, nvlist_t **debugnvp)
2134 send_dump_data_t sdd = { 0 };
2136 nvlist_t *fss = NULL;
2137 avl_tree_t *fsavl = NULL;
2138 static uint64_t holdseq;
2142 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2143 "cannot send '%s'"), zhp->zfs_name);
2145 if (fromsnap && fromsnap[0] == '\0') {
2146 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2147 "zero-length incremental source"));
2148 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
2152 char full_fromsnap_name[ZFS_MAX_DATASET_NAME_LEN];
2153 if (snprintf(full_fromsnap_name, sizeof (full_fromsnap_name),
2154 "%s@%s", zhp->zfs_name, fromsnap) >=
2155 sizeof (full_fromsnap_name)) {
2159 zfs_handle_t *fromsnapn = zfs_open(zhp->zfs_hdl,
2160 full_fromsnap_name, ZFS_TYPE_SNAPSHOT);
2161 if (fromsnapn == NULL) {
2165 zfs_close(fromsnapn);
2168 if (flags->replicate || flags->doall || flags->props ||
2169 flags->holds || flags->backup) {
2170 char full_tosnap_name[ZFS_MAX_DATASET_NAME_LEN];
2171 if (snprintf(full_tosnap_name, sizeof (full_tosnap_name),
2172 "%s@%s", zhp->zfs_name, tosnap) >=
2173 sizeof (full_tosnap_name)) {
2177 zfs_handle_t *tosnap = zfs_open(zhp->zfs_hdl,
2178 full_tosnap_name, ZFS_TYPE_SNAPSHOT);
2179 if (tosnap == NULL) {
2183 err = send_prelim_records(tosnap, fromsnap, outfd,
2184 flags->replicate || flags->props || flags->holds,
2185 flags->replicate, flags->verbosity > 0, flags->dryrun,
2186 flags->raw, flags->replicate, flags->skipmissing,
2187 flags->backup, flags->holds, flags->props, flags->doall,
2194 /* dump each stream */
2195 sdd.fromsnap = fromsnap;
2196 sdd.tosnap = tosnap;
2198 sdd.replicate = flags->replicate;
2199 sdd.doall = flags->doall;
2200 sdd.fromorigin = flags->fromorigin;
2203 sdd.verbosity = flags->verbosity;
2204 sdd.parsable = flags->parsable;
2205 sdd.progress = flags->progress;
2206 sdd.dryrun = flags->dryrun;
2207 sdd.large_block = flags->largeblock;
2208 sdd.embed_data = flags->embed_data;
2209 sdd.compress = flags->compress;
2210 sdd.raw = flags->raw;
2211 sdd.holds = flags->holds;
2212 sdd.filter_cb = filter_func;
2213 sdd.filter_cb_arg = cb_arg;
2215 sdd.debugnv = *debugnvp;
2216 if (sdd.verbosity != 0 && sdd.dryrun)
2217 sdd.std_out = B_TRUE;
2218 fout = sdd.std_out ? stdout : stderr;
2221 * Some flags require that we place user holds on the datasets that are
2222 * being sent so they don't get destroyed during the send. We can skip
2223 * this step if the pool is imported read-only since the datasets cannot
2226 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2227 ZPOOL_PROP_READONLY, NULL) &&
2228 zfs_spa_version(zhp, &spa_version) == 0 &&
2229 spa_version >= SPA_VERSION_USERREFS &&
2230 (flags->doall || flags->replicate)) {
2232 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2233 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2234 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
2235 if (sdd.cleanup_fd < 0) {
2239 sdd.snapholds = fnvlist_alloc();
2241 sdd.cleanup_fd = -1;
2242 sdd.snapholds = NULL;
2245 if (flags->verbosity != 0 || sdd.snapholds != NULL) {
2247 * Do a verbose no-op dry run to get all the verbose output
2248 * or to gather snapshot hold's before generating any data,
2249 * then do a non-verbose real run to generate the streams.
2251 sdd.dryrun = B_TRUE;
2252 err = dump_filesystems(zhp, &sdd);
2257 if (flags->verbosity != 0) {
2258 if (flags->parsable) {
2259 (void) fprintf(fout, "size\t%llu\n",
2260 (longlong_t)sdd.size);
2263 zfs_nicebytes(sdd.size, buf, sizeof (buf));
2264 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
2265 "total estimated size is %s\n"), buf);
2269 /* Ensure no snaps found is treated as an error. */
2275 /* Skip the second run if dryrun was requested. */
2279 if (sdd.snapholds != NULL) {
2280 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2284 fnvlist_free(sdd.snapholds);
2285 sdd.snapholds = NULL;
2288 sdd.dryrun = B_FALSE;
2292 err = dump_filesystems(zhp, &sdd);
2293 fsavl_destroy(fsavl);
2296 /* Ensure no snaps found is treated as an error. */
2297 if (err == 0 && !sdd.seento)
2300 if (sdd.cleanup_fd != -1) {
2301 VERIFY(0 == close(sdd.cleanup_fd));
2302 sdd.cleanup_fd = -1;
2305 if (!flags->dryrun && (flags->replicate || flags->doall ||
2306 flags->props || flags->backup || flags->holds)) {
2308 * write final end record. NB: want to do this even if
2309 * there was some error, because it might not be totally
2312 err = send_conclusion_record(outfd, NULL);
2314 return (zfs_standard_error(zhp->zfs_hdl, err, errbuf));
2317 return (err || sdd.err);
2320 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2322 fsavl_destroy(fsavl);
2324 fnvlist_free(sdd.snapholds);
2326 if (sdd.cleanup_fd != -1)
2327 VERIFY(0 == close(sdd.cleanup_fd));
2331 static zfs_handle_t *
2332 name_to_dir_handle(libzfs_handle_t *hdl, const char *snapname)
2334 char dirname[ZFS_MAX_DATASET_NAME_LEN];
2335 (void) strlcpy(dirname, snapname, ZFS_MAX_DATASET_NAME_LEN);
2336 char *c = strchr(dirname, '@');
2339 return (zfs_open(hdl, dirname, ZFS_TYPE_DATASET));
2343 * Returns B_TRUE if earlier is an earlier snapshot in later's timeline; either
2344 * an earlier snapshot in the same filesystem, or a snapshot before later's
2345 * origin, or it's origin's origin, etc.
2348 snapshot_is_before(zfs_handle_t *earlier, zfs_handle_t *later)
2351 uint64_t later_txg =
2352 (later->zfs_type == ZFS_TYPE_FILESYSTEM ||
2353 later->zfs_type == ZFS_TYPE_VOLUME ?
2354 UINT64_MAX : zfs_prop_get_int(later, ZFS_PROP_CREATETXG));
2355 uint64_t earlier_txg = zfs_prop_get_int(earlier, ZFS_PROP_CREATETXG);
2357 if (earlier_txg >= later_txg)
2360 zfs_handle_t *earlier_dir = name_to_dir_handle(earlier->zfs_hdl,
2362 zfs_handle_t *later_dir = name_to_dir_handle(later->zfs_hdl,
2365 if (strcmp(earlier_dir->zfs_name, later_dir->zfs_name) == 0) {
2366 zfs_close(earlier_dir);
2367 zfs_close(later_dir);
2371 char clonename[ZFS_MAX_DATASET_NAME_LEN];
2372 if (zfs_prop_get(later_dir, ZFS_PROP_ORIGIN, clonename,
2373 ZFS_MAX_DATASET_NAME_LEN, NULL, NULL, 0, B_TRUE) != 0) {
2374 zfs_close(earlier_dir);
2375 zfs_close(later_dir);
2379 zfs_handle_t *origin = zfs_open(earlier->zfs_hdl, clonename,
2381 uint64_t origin_txg = zfs_prop_get_int(origin, ZFS_PROP_CREATETXG);
2384 * If "earlier" is exactly the origin, then
2385 * snapshot_is_before(earlier, origin) will return false (because
2386 * they're the same).
2388 if (origin_txg == earlier_txg &&
2389 strcmp(origin->zfs_name, earlier->zfs_name) == 0) {
2390 zfs_close(earlier_dir);
2391 zfs_close(later_dir);
2395 zfs_close(earlier_dir);
2396 zfs_close(later_dir);
2398 ret = snapshot_is_before(earlier, origin);
2404 * The "zhp" argument is the handle of the dataset to send (typically a
2405 * snapshot). The "from" argument is the full name of the snapshot or
2406 * bookmark that is the incremental source.
2409 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
2410 const char *redactbook)
2413 libzfs_handle_t *hdl = zhp->zfs_hdl;
2414 char *name = zhp->zfs_name;
2416 progress_arg_t pa = { 0 };
2419 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2420 "warning: cannot send '%s'"), name);
2422 if (from != NULL && strchr(from, '@')) {
2423 zfs_handle_t *from_zhp = zfs_open(hdl, from,
2425 if (from_zhp == NULL)
2427 if (!snapshot_is_before(from_zhp, zhp)) {
2428 zfs_close(from_zhp);
2429 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2430 "not an earlier snapshot from the same fs"));
2431 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2433 zfs_close(from_zhp);
2436 if (redactbook != NULL) {
2437 char bookname[ZFS_MAX_DATASET_NAME_LEN];
2438 nvlist_t *redact_snaps;
2439 zfs_handle_t *book_zhp;
2443 pound = strchr(redactbook, '#');
2445 redactbook = pound + 1;
2446 at = strchr(name, '@');
2448 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2449 "cannot do a redacted send to a filesystem"));
2450 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2452 dsnamelen = at - name;
2453 if (snprintf(bookname, sizeof (bookname), "%.*s#%s",
2454 dsnamelen, name, redactbook)
2455 >= sizeof (bookname)) {
2456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2457 "invalid bookmark name"));
2458 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2460 book_zhp = zfs_open(hdl, bookname, ZFS_TYPE_BOOKMARK);
2461 if (book_zhp == NULL)
2463 if (nvlist_lookup_nvlist(book_zhp->zfs_props,
2464 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2465 &redact_snaps) != 0 || redact_snaps == NULL) {
2466 zfs_close(book_zhp);
2467 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2468 "not a redaction bookmark"));
2469 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2471 zfs_close(book_zhp);
2475 * Send fs properties
2477 if (flags->props || flags->holds || flags->backup) {
2479 * Note: the header generated by send_prelim_records()
2480 * assumes that the incremental source is in the same
2481 * filesystem/volume as the target (which is a requirement
2482 * when doing "zfs send -R"). But that isn't always the
2483 * case here (e.g. send from snap in origin, or send from
2484 * bookmark). We pass from=NULL, which will omit this
2485 * information from the prelim records; it isn't used
2486 * when receiving this type of stream.
2488 err = send_prelim_records(zhp, NULL, fd, B_TRUE, B_FALSE,
2489 flags->verbosity > 0, flags->dryrun, flags->raw,
2490 flags->replicate, B_FALSE, flags->backup, flags->holds,
2491 flags->props, flags->doall, NULL, NULL);
2497 * Perform size estimate if verbose was specified.
2499 if (flags->verbosity != 0) {
2500 err = estimate_size(zhp, from, fd, flags, 0, 0, 0, redactbook,
2510 * If progress reporting is requested, spawn a new thread to poll
2511 * ZFS_IOC_SEND_PROGRESS at a regular interval.
2513 if (flags->progress) {
2516 pa.pa_parsable = flags->parsable;
2517 pa.pa_estimate = B_FALSE;
2518 pa.pa_verbosity = flags->verbosity;
2520 err = pthread_create(&ptid, NULL,
2521 send_progress_thread, &pa);
2523 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
2524 return (zfs_error(zhp->zfs_hdl,
2525 EZFS_THREADCREATEFAILED, errbuf));
2529 err = lzc_send_redacted(name, from, fd,
2530 lzc_flags_from_sendflags(flags), redactbook);
2532 if (flags->progress) {
2533 void *status = NULL;
2535 (void) pthread_cancel(ptid);
2536 (void) pthread_join(ptid, &status);
2537 int error = (int)(uintptr_t)status;
2538 if (error != 0 && status != PTHREAD_CANCELED)
2539 return (zfs_standard_error_fmt(hdl, error,
2540 dgettext(TEXT_DOMAIN,
2541 "progress thread exited nonzero")));
2544 if (err == 0 && (flags->props || flags->holds || flags->backup)) {
2545 /* Write the final end record. */
2546 err = send_conclusion_record(fd, NULL);
2548 return (zfs_standard_error(hdl, err, errbuf));
2553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2554 "not an earlier snapshot from the same fs"));
2555 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2559 if (lzc_exists(name)) {
2560 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2561 "incremental source (%s) does not exist"),
2564 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2568 "dataset key must be loaded"));
2569 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2573 "target is busy; if a filesystem, "
2574 "it must not be mounted"));
2575 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2589 zfs_error_aux(hdl, "%s", strerror(errno));
2590 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2593 return (zfs_standard_error(hdl, errno, errbuf));
2600 * Routines specific to "zfs recv"
2604 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2605 boolean_t byteswap, zio_cksum_t *zc)
2612 rv = read(fd, cp, len);
2617 if (rv < 0 || len != 0) {
2618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2619 "failed to read from stream"));
2620 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2621 "cannot receive")));
2626 fletcher_4_incremental_byteswap(buf, ilen, zc);
2628 fletcher_4_incremental_native(buf, ilen, zc);
2634 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2635 boolean_t byteswap, zio_cksum_t *zc)
2640 buf = zfs_alloc(hdl, len);
2644 if (len > hdl->libzfs_max_nvlist) {
2645 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "nvlist too large"));
2650 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2656 err = nvlist_unpack(buf, len, nvp, 0);
2659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2660 "stream (malformed nvlist)"));
2667 * Returns the grand origin (origin of origin of origin...) of a given handle.
2668 * If this dataset is not a clone, it simply returns a copy of the original
2671 static zfs_handle_t *
2672 recv_open_grand_origin(zfs_handle_t *zhp)
2674 char origin[ZFS_MAX_DATASET_NAME_LEN];
2676 zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2678 while (ozhp != NULL) {
2679 if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2680 sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2683 (void) zfs_close(ozhp);
2684 ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2691 recv_rename_impl(zfs_handle_t *zhp, const char *name, const char *newname)
2694 zfs_handle_t *ozhp = NULL;
2697 * Attempt to rename the dataset. If it fails with EACCES we have
2698 * attempted to rename the dataset outside of its encryption root.
2699 * Force the dataset to become an encryption root and try again.
2701 err = lzc_rename(name, newname);
2702 if (err == EACCES) {
2703 ozhp = recv_open_grand_origin(zhp);
2709 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2714 err = lzc_rename(name, newname);
2724 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2725 int baselen, char *newname, recvflags_t *flags)
2729 prop_changelist_t *clp = NULL;
2730 zfs_handle_t *zhp = NULL;
2732 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2737 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2738 flags->force ? MS_FORCE : 0);
2743 err = changelist_prefix(clp);
2748 (void) strcpy(newname, tryname);
2749 if (flags->verbose) {
2750 (void) printf("attempting rename %s to %s\n",
2753 err = recv_rename_impl(zhp, name, newname);
2755 changelist_rename(clp, name, tryname);
2760 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2763 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2764 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2766 if (flags->verbose) {
2767 (void) printf("failed - trying rename %s to %s\n",
2770 err = recv_rename_impl(zhp, name, newname);
2772 changelist_rename(clp, name, newname);
2773 if (err && flags->verbose) {
2774 (void) printf("failed (%u) - "
2775 "will try again on next pass\n", errno);
2778 } else if (flags->verbose) {
2780 (void) printf("success\n");
2782 (void) printf("failed (%u)\n", errno);
2785 (void) changelist_postfix(clp);
2789 changelist_free(clp);
2797 recv_promote(libzfs_handle_t *hdl, const char *fsname,
2798 const char *origin_fsname, recvflags_t *flags)
2801 zfs_cmd_t zc = {"\0"};
2802 zfs_handle_t *zhp = NULL, *ozhp = NULL;
2805 (void) printf("promoting %s\n", fsname);
2807 (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
2808 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
2811 * Attempt to promote the dataset. If it fails with EACCES the
2812 * promotion would cause this dataset to leave its encryption root.
2813 * Force the origin to become an encryption root and try again.
2815 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2816 if (err == EACCES) {
2817 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2823 ozhp = recv_open_grand_origin(zhp);
2829 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2834 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2847 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2848 char *newname, recvflags_t *flags)
2851 prop_changelist_t *clp;
2853 boolean_t defer = B_FALSE;
2856 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2859 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2860 flags->force ? MS_FORCE : 0);
2861 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2862 zfs_spa_version(zhp, &spa_version) == 0 &&
2863 spa_version >= SPA_VERSION_USERREFS)
2868 err = changelist_prefix(clp);
2873 (void) printf("attempting destroy %s\n", name);
2874 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2875 nvlist_t *nv = fnvlist_alloc();
2876 fnvlist_add_boolean(nv, name);
2877 err = lzc_destroy_snaps(nv, defer, NULL);
2880 err = lzc_destroy(name);
2884 (void) printf("success\n");
2885 changelist_remove(clp, name);
2888 (void) changelist_postfix(clp);
2889 changelist_free(clp);
2892 * Deferred destroy might destroy the snapshot or only mark it to be
2893 * destroyed later, and it returns success in either case.
2895 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2896 ZFS_TYPE_SNAPSHOT))) {
2897 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2903 typedef struct guid_to_name_data {
2905 boolean_t bookmark_ok;
2908 uint64_t *redact_snap_guids;
2909 uint64_t num_redact_snaps;
2910 } guid_to_name_data_t;
2913 redact_snaps_match(zfs_handle_t *zhp, guid_to_name_data_t *gtnd)
2915 uint64_t *bmark_snaps;
2916 uint_t bmark_num_snaps;
2918 if (zhp->zfs_type != ZFS_TYPE_BOOKMARK)
2921 nvl = fnvlist_lookup_nvlist(zhp->zfs_props,
2922 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
2923 bmark_snaps = fnvlist_lookup_uint64_array(nvl, ZPROP_VALUE,
2925 if (bmark_num_snaps != gtnd->num_redact_snaps)
2928 for (; i < bmark_num_snaps; i++) {
2930 for (; j < bmark_num_snaps; j++) {
2931 if (bmark_snaps[i] == gtnd->redact_snap_guids[j])
2934 if (j == bmark_num_snaps)
2937 return (i == bmark_num_snaps);
2941 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2943 guid_to_name_data_t *gtnd = arg;
2947 if (gtnd->skip != NULL &&
2948 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2949 strcmp(slash + 1, gtnd->skip) == 0) {
2954 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid &&
2955 (gtnd->num_redact_snaps == -1 || redact_snaps_match(zhp, gtnd))) {
2956 (void) strcpy(gtnd->name, zhp->zfs_name);
2961 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2962 if (err != EEXIST && gtnd->bookmark_ok)
2963 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2969 * Attempt to find the local dataset associated with this guid. In the case of
2970 * multiple matches, we attempt to find the "best" match by searching
2971 * progressively larger portions of the hierarchy. This allows one to send a
2972 * tree of datasets individually and guarantee that we will find the source
2973 * guid within that hierarchy, even if there are multiple matches elsewhere.
2975 * If num_redact_snaps is not -1, we attempt to find a redaction bookmark with
2976 * the specified number of redaction snapshots. If num_redact_snaps isn't 0 or
2977 * -1, then redact_snap_guids will be an array of the guids of the snapshots the
2978 * redaction bookmark was created with. If num_redact_snaps is -1, then we will
2979 * attempt to find a snapshot or bookmark (if bookmark_ok is passed) with the
2980 * given guid. Note that a redaction bookmark can be returned if
2981 * num_redact_snaps == -1.
2984 guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
2985 uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
2986 uint64_t num_redact_snaps, char *name)
2988 char pname[ZFS_MAX_DATASET_NAME_LEN];
2989 guid_to_name_data_t gtnd;
2992 gtnd.bookmark_ok = bookmark_ok;
2995 gtnd.redact_snap_guids = redact_snap_guids;
2996 gtnd.num_redact_snaps = num_redact_snaps;
2999 * Search progressively larger portions of the hierarchy, starting
3000 * with the filesystem specified by 'parent'. This will
3001 * select the "most local" version of the origin snapshot in the case
3002 * that there are multiple matching snapshots in the system.
3004 (void) strlcpy(pname, parent, sizeof (pname));
3005 char *cp = strrchr(pname, '@');
3007 cp = strchr(pname, '\0');
3008 for (; cp != NULL; cp = strrchr(pname, '/')) {
3009 /* Chop off the last component and open the parent */
3011 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
3015 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
3017 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
3018 if (err != EEXIST && bookmark_ok)
3019 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
3025 * Remember the last portion of the dataset so we skip it next
3026 * time through (as we've already searched that portion of the
3029 gtnd.skip = strrchr(pname, '/') + 1;
3036 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
3037 boolean_t bookmark_ok, char *name)
3039 return (guid_to_name_redact_snaps(hdl, parent, guid, bookmark_ok, NULL,
3044 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
3045 * guid1 is after guid2.
3048 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
3049 uint64_t guid1, uint64_t guid2)
3052 char *fsname = NULL, *snapname = NULL;
3053 char buf[ZFS_MAX_DATASET_NAME_LEN];
3055 zfs_handle_t *guid1hdl, *guid2hdl;
3056 uint64_t create1, create2;
3063 nvfs = fsavl_find(avl, guid1, &snapname);
3064 fsname = fnvlist_lookup_string(nvfs, "name");
3065 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3066 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3067 if (guid1hdl == NULL)
3070 nvfs = fsavl_find(avl, guid2, &snapname);
3071 fsname = fnvlist_lookup_string(nvfs, "name");
3072 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3073 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3074 if (guid2hdl == NULL) {
3075 zfs_close(guid1hdl);
3079 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
3080 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
3082 if (create1 < create2)
3084 else if (create1 > create2)
3089 zfs_close(guid1hdl);
3090 zfs_close(guid2hdl);
3096 * This function reestablishes the hierarchy of encryption roots after a
3097 * recursive incremental receive has completed. This must be done after the
3098 * second call to recv_incremental_replication() has renamed and promoted all
3099 * sent datasets to their final locations in the dataset hierarchy.
3102 recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
3103 nvlist_t *stream_nv)
3106 nvpair_t *fselem = NULL;
3107 nvlist_t *stream_fss;
3109 stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3111 while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3112 zfs_handle_t *zhp = NULL;
3114 nvlist_t *snaps, *props, *stream_nvfs = NULL;
3115 nvpair_t *snapel = NULL;
3116 boolean_t is_encroot, is_clone, stream_encroot;
3118 char *stream_keylocation = NULL;
3119 char keylocation[MAXNAMELEN];
3120 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3122 keylocation[0] = '\0';
3123 stream_nvfs = fnvpair_value_nvlist(fselem);
3124 snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps");
3125 props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3126 stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3128 /* find a snapshot from the stream that exists locally */
3130 while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
3133 guid = fnvpair_value_uint64(snapel);
3134 err = guid_to_name(hdl, top_zfs, guid, B_FALSE,
3143 cp = strchr(fsname, '@');
3147 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3153 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
3154 is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3155 (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3157 /* we don't need to do anything for unencrypted datasets */
3158 if (crypt == ZIO_CRYPT_OFF) {
3164 * If the dataset is flagged as an encryption root, was not
3165 * received as a clone and is not currently an encryption root,
3166 * force it to become one. Fixup the keylocation if necessary.
3168 if (stream_encroot) {
3169 if (!is_clone && !is_encroot) {
3170 err = lzc_change_key(fsname,
3171 DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
3178 stream_keylocation = fnvlist_lookup_string(props,
3179 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
3182 * Refresh the properties in case the call to
3183 * lzc_change_key() changed the value.
3185 zfs_refresh_properties(zhp);
3186 err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
3187 keylocation, sizeof (keylocation), NULL, NULL,
3194 if (strcmp(keylocation, stream_keylocation) != 0) {
3195 err = zfs_prop_set(zhp,
3196 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
3197 stream_keylocation);
3206 * If the dataset is not flagged as an encryption root and is
3207 * currently an encryption root, force it to inherit from its
3208 * parent. The root of a raw send should never be
3211 if (!stream_encroot && is_encroot &&
3212 strcmp(top_zfs, fsname) != 0) {
3213 err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
3231 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
3232 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3235 nvlist_t *local_nv, *deleted = NULL;
3236 avl_tree_t *local_avl;
3237 nvpair_t *fselem, *nextfselem;
3239 char newname[ZFS_MAX_DATASET_NAME_LEN];
3242 boolean_t needagain, progress, recursive;
3245 fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap");
3247 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3254 needagain = progress = B_FALSE;
3256 deleted = fnvlist_alloc();
3258 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
3259 recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE,
3260 B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
3264 * Process deletes and renames
3266 for (fselem = nvlist_next_nvpair(local_nv, NULL);
3267 fselem; fselem = nextfselem) {
3268 nvlist_t *nvfs, *snaps;
3269 nvlist_t *stream_nvfs = NULL;
3270 nvpair_t *snapelem, *nextsnapelem;
3271 uint64_t fromguid = 0;
3272 uint64_t originguid = 0;
3273 uint64_t stream_originguid = 0;
3274 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
3275 char *fsname, *stream_fsname;
3277 nextfselem = nvlist_next_nvpair(local_nv, fselem);
3279 nvfs = fnvpair_value_nvlist(fselem);
3280 snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
3281 fsname = fnvlist_lookup_string(nvfs, "name");
3282 parent_fromsnap_guid = fnvlist_lookup_uint64(nvfs,
3284 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
3287 * First find the stream's fs, so we can check for
3288 * a different origin (due to "zfs promote")
3290 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3291 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3294 thisguid = fnvpair_value_uint64(snapelem);
3295 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3297 if (stream_nvfs != NULL)
3301 /* check for promote */
3302 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
3303 &stream_originguid);
3304 if (stream_nvfs && originguid != stream_originguid) {
3305 switch (created_before(hdl, local_avl,
3306 stream_originguid, originguid)) {
3309 nvlist_t *origin_nvfs;
3310 char *origin_fsname;
3312 origin_nvfs = fsavl_find(local_avl, originguid,
3314 origin_fsname = fnvlist_lookup_string(
3315 origin_nvfs, "name");
3316 error = recv_promote(hdl, fsname, origin_fsname,
3325 fsavl_destroy(local_avl);
3326 fnvlist_free(local_nv);
3330 * We had/have the wrong origin, therefore our
3331 * list of snapshots is wrong. Need to handle
3332 * them on the next pass.
3338 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3339 snapelem; snapelem = nextsnapelem) {
3341 char *stream_snapname;
3342 nvlist_t *found, *props;
3344 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
3346 thisguid = fnvpair_value_uint64(snapelem);
3347 found = fsavl_find(stream_avl, thisguid,
3350 /* check for delete */
3351 if (found == NULL) {
3352 char name[ZFS_MAX_DATASET_NAME_LEN];
3357 (void) snprintf(name, sizeof (name), "%s@%s",
3358 fsname, nvpair_name(snapelem));
3360 error = recv_destroy(hdl, name,
3361 strlen(fsname)+1, newname, flags);
3366 sprintf(guidname, "%llu",
3367 (u_longlong_t)thisguid);
3368 nvlist_add_boolean(deleted, guidname);
3372 stream_nvfs = found;
3374 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
3375 &props) && 0 == nvlist_lookup_nvlist(props,
3376 stream_snapname, &props)) {
3377 zfs_cmd_t zc = {"\0"};
3379 zc.zc_cookie = B_TRUE; /* received */
3380 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
3381 "%s@%s", fsname, nvpair_name(snapelem));
3382 if (zcmd_write_src_nvlist(hdl, &zc,
3384 (void) zfs_ioctl(hdl,
3385 ZFS_IOC_SET_PROP, &zc);
3386 zcmd_free_nvlists(&zc);
3390 /* check for different snapname */
3391 if (strcmp(nvpair_name(snapelem),
3392 stream_snapname) != 0) {
3393 char name[ZFS_MAX_DATASET_NAME_LEN];
3394 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3396 (void) snprintf(name, sizeof (name), "%s@%s",
3397 fsname, nvpair_name(snapelem));
3398 (void) snprintf(tryname, sizeof (name), "%s@%s",
3399 fsname, stream_snapname);
3401 error = recv_rename(hdl, name, tryname,
3402 strlen(fsname)+1, newname, flags);
3409 if (strcmp(stream_snapname, fromsnap) == 0)
3410 fromguid = thisguid;
3413 /* check for delete */
3414 if (stream_nvfs == NULL) {
3418 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
3424 sprintf(guidname, "%llu",
3425 (u_longlong_t)parent_fromsnap_guid);
3426 nvlist_add_boolean(deleted, guidname);
3430 if (fromguid == 0) {
3431 if (flags->verbose) {
3432 (void) printf("local fs %s does not have "
3433 "fromsnap (%s in stream); must have "
3434 "been deleted locally; ignoring\n",
3440 stream_fsname = fnvlist_lookup_string(stream_nvfs, "name");
3441 stream_parent_fromsnap_guid = fnvlist_lookup_uint64(
3442 stream_nvfs, "parentfromsnap");
3444 s1 = strrchr(fsname, '/');
3445 s2 = strrchr(stream_fsname, '/');
3448 * Check if we're going to rename based on parent guid change
3449 * and the current parent guid was also deleted. If it was then
3450 * rename will fail and is likely unneeded, so avoid this and
3451 * force an early retry to determine the new
3452 * parent_fromsnap_guid.
3454 if (stream_parent_fromsnap_guid != 0 &&
3455 parent_fromsnap_guid != 0 &&
3456 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
3457 sprintf(guidname, "%llu",
3458 (u_longlong_t)parent_fromsnap_guid);
3459 if (nvlist_exists(deleted, guidname)) {
3467 * Check for rename. If the exact receive path is specified, it
3468 * does not count as a rename, but we still need to check the
3469 * datasets beneath it.
3471 if ((stream_parent_fromsnap_guid != 0 &&
3472 parent_fromsnap_guid != 0 &&
3473 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
3474 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3475 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3477 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3479 parent = fsavl_find(local_avl,
3480 stream_parent_fromsnap_guid, NULL);
3482 * NB: parent might not be found if we used the
3483 * tosnap for stream_parent_fromsnap_guid,
3484 * because the parent is a newly-created fs;
3485 * we'll be able to rename it after we recv the
3488 if (parent != NULL) {
3491 pname = fnvlist_lookup_string(parent, "name");
3492 (void) snprintf(tryname, sizeof (tryname),
3493 "%s%s", pname, strrchr(stream_fsname, '/'));
3496 if (flags->verbose) {
3497 (void) printf("local fs %s new parent "
3498 "not found\n", fsname);
3504 error = recv_rename(hdl, fsname, tryname,
3505 strlen(tofs)+1, newname, flags);
3507 if (renamed != NULL && newname[0] != '\0') {
3508 fnvlist_add_boolean(renamed, newname);
3519 fsavl_destroy(local_avl);
3520 fnvlist_free(local_nv);
3521 fnvlist_free(deleted);
3523 if (needagain && progress) {
3524 /* do another pass to fix up temporary names */
3526 (void) printf("another pass:\n");
3530 return (needagain || error != 0);
3534 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
3535 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3536 char **top_zfs, nvlist_t *cmdprops)
3538 nvlist_t *stream_nv = NULL;
3539 avl_tree_t *stream_avl = NULL;
3540 char *fromsnap = NULL;
3541 char *sendsnap = NULL;
3543 char tofs[ZFS_MAX_DATASET_NAME_LEN];
3544 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
3546 dmu_replay_record_t drre;
3548 boolean_t anyerr = B_FALSE;
3549 boolean_t softerr = B_FALSE;
3550 boolean_t recursive, raw;
3552 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3555 assert(drr->drr_type == DRR_BEGIN);
3556 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
3557 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3558 DMU_COMPOUNDSTREAM);
3561 * Read in the nvlist from the stream.
3563 if (drr->drr_payloadlen != 0) {
3564 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
3565 &stream_nv, flags->byteswap, zc);
3567 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3572 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3574 raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3576 if (recursive && strchr(destname, '@')) {
3577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3578 "cannot specify snapshot name for multi-snapshot stream"));
3579 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3584 * Read in the end record and verify checksum.
3586 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3587 flags->byteswap, NULL)))
3589 if (flags->byteswap) {
3590 drre.drr_type = BSWAP_32(drre.drr_type);
3591 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3592 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3593 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3594 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3595 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3596 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3597 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3598 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3600 if (drre.drr_type != DRR_END) {
3601 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3604 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3606 "incorrect header checksum"));
3607 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3611 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3613 if (drr->drr_payloadlen != 0) {
3614 nvlist_t *stream_fss;
3616 stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3617 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3619 "couldn't allocate avl tree"));
3620 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3624 if (fromsnap != NULL && recursive) {
3625 nvlist_t *renamed = NULL;
3626 nvpair_t *pair = NULL;
3628 (void) strlcpy(tofs, destname, sizeof (tofs));
3629 if (flags->isprefix) {
3630 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3633 if (flags->istail) {
3634 cp = strrchr(drrb->drr_toname, '/');
3636 (void) strlcat(tofs, "/",
3640 i = (cp - drrb->drr_toname);
3643 i = strcspn(drrb->drr_toname, "/@");
3645 /* zfs_receive_one() will create_parents() */
3646 (void) strlcat(tofs, &drrb->drr_toname[i],
3648 *strchr(tofs, '@') = '\0';
3651 if (!flags->dryrun && !flags->nomount) {
3652 renamed = fnvlist_alloc();
3655 softerr = recv_incremental_replication(hdl, tofs, flags,
3656 stream_nv, stream_avl, renamed);
3658 /* Unmount renamed filesystems before receiving. */
3659 while ((pair = nvlist_next_nvpair(renamed,
3662 prop_changelist_t *clp = NULL;
3664 zhp = zfs_open(hdl, nvpair_name(pair),
3665 ZFS_TYPE_FILESYSTEM);
3667 clp = changelist_gather(zhp,
3668 ZFS_PROP_MOUNTPOINT, 0,
3669 flags->forceunmount ? MS_FORCE : 0);
3673 changelist_prefix(clp);
3674 changelist_free(clp);
3679 fnvlist_free(renamed);
3684 * Get the fs specified by the first path in the stream (the top level
3685 * specified by 'zfs send') and pass it to each invocation of
3686 * zfs_receive_one().
3688 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
3690 if ((cp = strchr(sendfs, '@')) != NULL) {
3693 * Find the "sendsnap", the final snapshot in a replication
3694 * stream. zfs_receive_one() handles certain errors
3695 * differently, depending on if the contained stream is the
3698 sendsnap = (cp + 1);
3701 /* Finally, receive each contained stream */
3704 * we should figure out if it has a recoverable
3705 * error, in which case do a recv_skip() and drive on.
3706 * Note, if we fail due to already having this guid,
3707 * zfs_receive_one() will take care of it (ie,
3708 * recv_skip() and return 0).
3710 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3711 sendfs, stream_nv, stream_avl, top_zfs, sendsnap, cmdprops);
3712 if (error == ENODATA) {
3717 } while (error == 0);
3719 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
3721 * Now that we have the fs's they sent us, try the
3724 softerr = recv_incremental_replication(hdl, tofs, flags,
3725 stream_nv, stream_avl, NULL);
3728 if (raw && softerr == 0 && *top_zfs != NULL) {
3729 softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
3734 fsavl_destroy(stream_avl);
3735 fnvlist_free(stream_nv);
3744 trunc_prop_errs(int truncated)
3746 ASSERT(truncated != 0);
3749 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3750 "1 more property could not be set\n"));
3752 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3753 "%d more properties could not be set\n"), truncated);
3757 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3759 dmu_replay_record_t *drr;
3760 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
3761 uint64_t payload_size;
3764 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3767 /* XXX would be great to use lseek if possible... */
3770 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3771 byteswap, NULL) == 0) {
3773 drr->drr_type = BSWAP_32(drr->drr_type);
3775 switch (drr->drr_type) {
3777 if (drr->drr_payloadlen != 0) {
3778 (void) recv_read(hdl, fd, buf,
3779 drr->drr_payloadlen, B_FALSE, NULL);
3789 drr->drr_u.drr_object.drr_bonuslen =
3790 BSWAP_32(drr->drr_u.drr_object.
3792 drr->drr_u.drr_object.drr_raw_bonuslen =
3793 BSWAP_32(drr->drr_u.drr_object.
3798 DRR_OBJECT_PAYLOAD_SIZE(&drr->drr_u.drr_object);
3799 (void) recv_read(hdl, fd, buf, payload_size,
3805 drr->drr_u.drr_write.drr_logical_size =
3807 drr->drr_u.drr_write.drr_logical_size);
3808 drr->drr_u.drr_write.drr_compressed_size =
3810 drr->drr_u.drr_write.drr_compressed_size);
3813 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3814 assert(payload_size <= SPA_MAXBLOCKSIZE);
3815 (void) recv_read(hdl, fd, buf,
3816 payload_size, B_FALSE, NULL);
3820 drr->drr_u.drr_spill.drr_length =
3821 BSWAP_64(drr->drr_u.drr_spill.drr_length);
3822 drr->drr_u.drr_spill.drr_compressed_size =
3823 BSWAP_64(drr->drr_u.drr_spill.
3824 drr_compressed_size);
3828 DRR_SPILL_PAYLOAD_SIZE(&drr->drr_u.drr_spill);
3829 (void) recv_read(hdl, fd, buf, payload_size,
3832 case DRR_WRITE_EMBEDDED:
3834 drr->drr_u.drr_write_embedded.drr_psize =
3835 BSWAP_32(drr->drr_u.drr_write_embedded.
3838 (void) recv_read(hdl, fd, buf,
3839 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3842 case DRR_OBJECT_RANGE:
3843 case DRR_WRITE_BYREF:
3844 case DRR_FREEOBJECTS:
3849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3850 "invalid record type"));
3852 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3861 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3862 boolean_t resumable, boolean_t checksum)
3864 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3866 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, (checksum ?
3867 "checksum mismatch" : "incomplete stream")));
3871 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3872 *strchr(target_fs, '@') = '\0';
3873 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3874 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3878 char token_buf[ZFS_MAXPROPLEN];
3879 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3880 token_buf, sizeof (token_buf),
3881 NULL, NULL, 0, B_TRUE);
3883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3884 "checksum mismatch or incomplete stream.\n"
3885 "Partially received snapshot is saved.\n"
3886 "A resuming stream can be generated on the sending "
3887 "system by running:\n"
3895 * Prepare a new nvlist of properties that are to override (-o) or be excluded
3896 * (-x) from the received dataset
3897 * recvprops: received properties from the send stream
3898 * cmdprops: raw input properties from command line
3899 * origprops: properties, both locally-set and received, currently set on the
3900 * target dataset if it exists, NULL otherwise.
3901 * oxprops: valid output override (-o) and excluded (-x) properties
3904 zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
3905 char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
3906 boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
3907 nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
3908 uint_t *wkeylen_out, const char *errbuf)
3911 nvlist_t *oprops, *voprops;
3912 zfs_handle_t *zhp = NULL;
3913 zpool_handle_t *zpool_hdl = NULL;
3916 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
3918 if (nvlist_empty(cmdprops))
3919 return (0); /* No properties to override or exclude */
3921 *oxprops = fnvlist_alloc();
3922 oprops = fnvlist_alloc();
3924 strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
3927 * Get our dataset handle. The target dataset may not exist yet.
3929 if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
3930 zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
3937 /* open the zpool handle */
3938 cp = strchr(namebuf, '/');
3941 zpool_hdl = zpool_open(hdl, namebuf);
3942 if (zpool_hdl == NULL) {
3947 /* restore namebuf to match fsname for later use */
3952 * first iteration: process excluded (-x) properties now and gather
3953 * added (-o) properties to be later processed by zfs_valid_proplist()
3956 while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
3957 const char *name = nvpair_name(nvp);
3958 zfs_prop_t prop = zfs_name_to_prop(name);
3961 * It turns out, if we don't normalize "aliased" names
3962 * e.g. compress= against the "real" names (e.g. compression)
3963 * here, then setting/excluding them does not work as
3966 * But since user-defined properties wouldn't have a valid
3967 * mapping here, we do this conditional dance.
3969 const char *newname = name;
3970 if (prop >= ZFS_PROP_TYPE)
3971 newname = zfs_prop_to_name(prop);
3973 /* "origin" is processed separately, don't handle it here */
3974 if (prop == ZFS_PROP_ORIGIN)
3977 /* raw streams can't override encryption properties */
3978 if ((zfs_prop_encryption_key_param(prop) ||
3979 prop == ZFS_PROP_ENCRYPTION) && raw) {
3980 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3981 "encryption property '%s' cannot "
3982 "be set or excluded for raw streams."), name);
3983 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3987 /* incremental streams can only exclude encryption properties */
3988 if ((zfs_prop_encryption_key_param(prop) ||
3989 prop == ZFS_PROP_ENCRYPTION) && !newfs &&
3990 nvpair_type(nvp) != DATA_TYPE_BOOLEAN) {
3991 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3992 "encryption property '%s' cannot "
3993 "be set for incremental streams."), name);
3994 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3998 switch (nvpair_type(nvp)) {
3999 case DATA_TYPE_BOOLEAN: /* -x property */
4001 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
4002 * a property: this is done by forcing an explicit
4003 * inherit on the destination so the effective value is
4004 * not the one we received from the send stream.
4006 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
4007 !zfs_prop_user(name)) {
4008 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4009 "Warning: %s: property '%s' does not "
4010 "apply to datasets of this type\n"),
4015 * We do this only if the property is not already
4016 * locally-set, in which case its value will take
4017 * priority over the received anyway.
4019 if (nvlist_exists(origprops, newname)) {
4021 char *source = NULL;
4023 attrs = fnvlist_lookup_nvlist(origprops,
4025 if (nvlist_lookup_string(attrs,
4026 ZPROP_SOURCE, &source) == 0 &&
4027 strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
4031 * We can't force an explicit inherit on non-inheritable
4032 * properties: if we're asked to exclude this kind of
4033 * values we remove them from "recvprops" input nvlist.
4035 if (!zfs_prop_inheritable(prop) &&
4036 !zfs_prop_user(name) && /* can be inherited too */
4037 nvlist_exists(recvprops, newname))
4038 fnvlist_remove(recvprops, newname);
4040 fnvlist_add_boolean(*oxprops, newname);
4042 case DATA_TYPE_STRING: /* -o property=value */
4044 * we're trying to override a property that does not
4045 * make sense for this type of dataset, but we don't
4046 * want to fail if the receive is recursive: this comes
4047 * in handy when the send stream contains, for
4048 * instance, a child ZVOL and we're trying to receive
4049 * it with "-o atime=on"
4051 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
4052 !zfs_prop_user(name)) {
4055 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4056 "property '%s' does not apply to datasets "
4057 "of this type"), name);
4058 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4061 fnvlist_add_string(oprops, newname,
4062 fnvpair_value_string(nvp));
4065 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4066 "property '%s' must be a string or boolean"), name);
4067 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4073 /* convert override strings properties to native */
4074 if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
4075 oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4076 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4081 * zfs_crypto_create() requires the parent name. Get it
4082 * by truncating the fsname copy stored in namebuf.
4084 cp = strrchr(namebuf, '/');
4088 if (!raw && zfs_crypto_create(hdl, namebuf, voprops, NULL,
4089 B_FALSE, wkeydata_out, wkeylen_out) != 0) {
4090 fnvlist_free(voprops);
4091 ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4095 /* second pass: process "-o" properties */
4096 fnvlist_merge(*oxprops, voprops);
4097 fnvlist_free(voprops);
4099 /* override props on child dataset are inherited */
4101 while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
4102 const char *name = nvpair_name(nvp);
4103 fnvlist_add_boolean(*oxprops, name);
4110 if (zpool_hdl != NULL)
4111 zpool_close(zpool_hdl);
4112 fnvlist_free(oprops);
4117 * Restores a backup of tosnap from the file descriptor specified by infd.
4120 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
4121 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
4122 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
4123 avl_tree_t *stream_avl, char **top_zfs,
4124 const char *finalsnap, nvlist_t *cmdprops)
4127 int ioctl_err, ioctl_errno, err;
4129 struct drr_begin *drrb = &drr->drr_u.drr_begin;
4131 const char *chopprefix;
4132 boolean_t newfs = B_FALSE;
4133 boolean_t stream_wantsnewfs, stream_resumingnewfs;
4134 boolean_t newprops = B_FALSE;
4135 uint64_t read_bytes = 0;
4136 uint64_t errflags = 0;
4137 uint64_t parent_snapguid = 0;
4138 prop_changelist_t *clp = NULL;
4139 nvlist_t *snapprops_nvlist = NULL;
4140 nvlist_t *snapholds_nvlist = NULL;
4141 zprop_errflags_t prop_errflags;
4142 nvlist_t *prop_errors = NULL;
4143 boolean_t recursive;
4144 char *snapname = NULL;
4145 char destsnap[MAXPATHLEN * 2];
4146 char origin[MAXNAMELEN];
4147 char name[MAXPATHLEN];
4148 char tmp_keylocation[MAXNAMELEN];
4149 nvlist_t *rcvprops = NULL; /* props received from the send stream */
4150 nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
4151 nvlist_t *origprops = NULL; /* original props (if destination exists) */
4153 boolean_t toplevel = B_FALSE;
4154 boolean_t zoned = B_FALSE;
4155 boolean_t hastoken = B_FALSE;
4157 uint8_t *wkeydata = NULL;
4160 begin_time = time(NULL);
4161 bzero(origin, MAXNAMELEN);
4162 bzero(tmp_keylocation, MAXNAMELEN);
4164 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4167 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
4170 /* Did the user request holds be skipped via zfs recv -k? */
4171 boolean_t holds = flags->holds && !flags->skipholds;
4173 if (stream_avl != NULL) {
4174 char *keylocation = NULL;
4175 nvlist_t *lookup = NULL;
4176 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
4179 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
4181 err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
4183 rcvprops = fnvlist_alloc();
4188 * The keylocation property may only be set on encryption roots,
4189 * but this dataset might not become an encryption root until
4190 * recv_fix_encryption_hierarchy() is called. That function
4191 * will fixup the keylocation anyway, so we temporarily unset
4192 * the keylocation for now to avoid any errors from the receive
4195 err = nvlist_lookup_string(rcvprops,
4196 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
4198 strcpy(tmp_keylocation, keylocation);
4199 (void) nvlist_remove_all(rcvprops,
4200 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
4203 if (flags->canmountoff) {
4204 fnvlist_add_uint64(rcvprops,
4205 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0);
4206 } else if (newprops) { /* nothing in rcvprops, eliminate it */
4207 fnvlist_free(rcvprops);
4211 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
4212 snapprops_nvlist = fnvlist_lookup_nvlist(lookup,
4216 if (0 == nvlist_lookup_nvlist(fs, "snapholds",
4218 snapholds_nvlist = fnvlist_lookup_nvlist(
4227 * Determine how much of the snapshot name stored in the stream
4228 * we are going to tack on to the name they specified on the
4229 * command line, and how much we are going to chop off.
4231 * If they specified a snapshot, chop the entire name stored in
4234 if (flags->istail) {
4236 * A filesystem was specified with -e. We want to tack on only
4237 * the tail of the sent snapshot path.
4239 if (strchr(tosnap, '@')) {
4240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4241 "argument - snapshot not allowed with -e"));
4242 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4246 chopprefix = strrchr(sendfs, '/');
4248 if (chopprefix == NULL) {
4250 * The tail is the poolname, so we need to
4251 * prepend a path separator.
4253 int len = strlen(drrb->drr_toname);
4254 cp = malloc(len + 2);
4256 (void) strcpy(&cp[1], drrb->drr_toname);
4259 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
4261 } else if (flags->isprefix) {
4263 * A filesystem was specified with -d. We want to tack on
4264 * everything but the first element of the sent snapshot path
4265 * (all but the pool name).
4267 if (strchr(tosnap, '@')) {
4268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4269 "argument - snapshot not allowed with -d"));
4270 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4274 chopprefix = strchr(drrb->drr_toname, '/');
4275 if (chopprefix == NULL)
4276 chopprefix = strchr(drrb->drr_toname, '@');
4277 } else if (strchr(tosnap, '@') == NULL) {
4279 * If a filesystem was specified without -d or -e, we want to
4280 * tack on everything after the fs specified by 'zfs send'.
4282 chopprefix = drrb->drr_toname + strlen(sendfs);
4284 /* A snapshot was specified as an exact path (no -d or -e). */
4286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4287 "cannot specify snapshot name for multi-snapshot "
4289 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4292 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
4295 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
4296 ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
4297 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
4298 strchr(sendfs, '/') == NULL);
4299 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
4300 chopprefix[0] == '\0');
4303 * Determine name of destination snapshot.
4305 (void) strlcpy(destsnap, tosnap, sizeof (destsnap));
4306 (void) strlcat(destsnap, chopprefix, sizeof (destsnap));
4308 if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
4309 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4314 * Determine the name of the origin snapshot.
4317 (void) strlcpy(origin, originsnap, sizeof (origin));
4319 (void) printf("using provided clone origin %s\n",
4321 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
4322 if (guid_to_name(hdl, destsnap,
4323 drrb->drr_fromguid, B_FALSE, origin) != 0) {
4324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4325 "local origin for clone %s does not exist"),
4327 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4331 (void) printf("found clone origin %s\n", origin);
4334 if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4335 DMU_BACKUP_FEATURE_DEDUP)) {
4336 (void) fprintf(stderr,
4337 gettext("ERROR: \"zfs receive\" no longer supports "
4338 "deduplicated send streams. Use\n"
4339 "the \"zstream redup\" command to convert this stream "
4341 "non-deduplicated stream.\n"));
4342 err = zfs_error(hdl, EZFS_NOTSUP, errbuf);
4346 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4347 DMU_BACKUP_FEATURE_RESUMING;
4348 boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4349 DMU_BACKUP_FEATURE_RAW;
4350 boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4351 DMU_BACKUP_FEATURE_EMBED_DATA;
4352 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
4353 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
4354 stream_resumingnewfs = (drrb->drr_fromguid == 0 ||
4355 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && resuming;
4357 if (stream_wantsnewfs) {
4359 * if the parent fs does not exist, look for it based on
4360 * the parent snap GUID
4362 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4363 "cannot receive new filesystem stream"));
4365 (void) strcpy(name, destsnap);
4366 cp = strrchr(name, '/');
4370 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4371 char suffix[ZFS_MAX_DATASET_NAME_LEN];
4372 (void) strcpy(suffix, strrchr(destsnap, '/'));
4373 if (guid_to_name(hdl, name, parent_snapguid,
4374 B_FALSE, destsnap) == 0) {
4375 *strchr(destsnap, '@') = '\0';
4376 (void) strcat(destsnap, suffix);
4381 * If the fs does not exist, look for it based on the
4385 (void) snprintf(errbuf, sizeof (errbuf),
4386 dgettext(TEXT_DOMAIN,
4387 "cannot receive resume stream"));
4389 (void) snprintf(errbuf, sizeof (errbuf),
4390 dgettext(TEXT_DOMAIN,
4391 "cannot receive incremental stream"));
4394 (void) strcpy(name, destsnap);
4395 *strchr(name, '@') = '\0';
4398 * If the exact receive path was specified and this is the
4399 * topmost path in the stream, then if the fs does not exist we
4400 * should look no further.
4402 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
4403 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
4404 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4405 char snap[ZFS_MAX_DATASET_NAME_LEN];
4406 (void) strcpy(snap, strchr(destsnap, '@'));
4407 if (guid_to_name(hdl, name, drrb->drr_fromguid,
4408 B_FALSE, destsnap) == 0) {
4409 *strchr(destsnap, '@') = '\0';
4410 (void) strcat(destsnap, snap);
4415 (void) strcpy(name, destsnap);
4416 *strchr(name, '@') = '\0';
4418 redacted = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4419 DMU_BACKUP_FEATURE_REDACTED;
4421 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4422 zfs_cmd_t zc = {"\0"};
4424 boolean_t encrypted;
4426 (void) strcpy(zc.zc_name, name);
4429 * Destination fs exists. It must be one of these cases:
4430 * - an incremental send stream
4431 * - the stream specifies a new fs (full stream or clone)
4432 * and they want us to blow away the existing fs (and
4433 * have therefore specified -F and removed any snapshots)
4434 * - we are resuming a failed receive.
4436 if (stream_wantsnewfs) {
4437 boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL;
4438 if (!flags->force) {
4439 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4440 "destination '%s' exists\n"
4441 "must specify -F to overwrite it"), name);
4442 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4445 if (zfs_ioctl(hdl, ZFS_IOC_SNAPSHOT_LIST_NEXT,
4447 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4448 "destination has snapshots (eg. %s)\n"
4449 "must destroy them to overwrite it"),
4451 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4454 if (is_volume && strrchr(name, '/') == NULL) {
4455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4456 "destination %s is the root dataset\n"
4457 "cannot overwrite with a ZVOL"),
4459 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4463 zfs_ioctl(hdl, ZFS_IOC_DATASET_LIST_NEXT,
4465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4466 "destination has children (eg. %s)\n"
4467 "cannot overwrite with a ZVOL"),
4469 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4474 if ((zhp = zfs_open(hdl, name,
4475 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
4480 if (stream_wantsnewfs &&
4481 zhp->zfs_dmustats.dds_origin[0]) {
4483 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4484 "destination '%s' is a clone\n"
4485 "must destroy it to overwrite it"), name);
4486 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4491 * Raw sends can not be performed as an incremental on top
4492 * of existing unencrypted datasets. zfs recv -F can't be
4493 * used to blow away an existing encrypted filesystem. This
4494 * is because it would require the dsl dir to point to the
4495 * new key (or lack of a key) and the old key at the same
4496 * time. The -F flag may still be used for deleting
4497 * intermediate snapshots that would otherwise prevent the
4498 * receive from working.
4500 encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
4502 if (!stream_wantsnewfs && !encrypted && raw) {
4504 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4505 "cannot perform raw receive on top of "
4506 "existing unencrypted dataset"));
4507 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4511 if (stream_wantsnewfs && flags->force &&
4512 ((raw && !encrypted) || encrypted)) {
4514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4515 "zfs receive -F cannot be used to destroy an "
4516 "encrypted filesystem or overwrite an "
4517 "unencrypted one with an encrypted one"));
4518 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4522 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4523 (stream_wantsnewfs || stream_resumingnewfs)) {
4524 /* We can't do online recv in this case */
4525 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4526 flags->forceunmount ? MS_FORCE : 0);
4532 if (changelist_prefix(clp) != 0) {
4533 changelist_free(clp);
4541 * If we are resuming a newfs, set newfs here so that we will
4542 * mount it if the recv succeeds this time. We can tell
4543 * that it was a newfs on the first recv because the fs
4544 * itself will be inconsistent (if the fs existed when we
4545 * did the first recv, we would have received it into
4548 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4551 /* we want to know if we're zoned when validating -o|-x props */
4552 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4554 /* may need this info later, get it now we have zhp around */
4555 if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4556 NULL, NULL, 0, B_TRUE) == 0)
4559 /* gather existing properties on destination */
4560 origprops = fnvlist_alloc();
4561 fnvlist_merge(origprops, zhp->zfs_props);
4562 fnvlist_merge(origprops, zhp->zfs_user_props);
4569 * Destination filesystem does not exist. Therefore we better
4570 * be creating a new filesystem (either from a full backup, or
4571 * a clone). It would therefore be invalid if the user
4572 * specified only the pool name (i.e. if the destination name
4573 * contained no slash character).
4575 cp = strrchr(name, '/');
4577 if (!stream_wantsnewfs || cp == NULL) {
4578 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4579 "destination '%s' does not exist"), name);
4580 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4585 * Trim off the final dataset component so we perform the
4586 * recvbackup ioctl to the filesystems's parent.
4590 if (flags->isprefix && !flags->istail && !flags->dryrun &&
4591 create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4592 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4596 /* validate parent */
4597 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4599 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4602 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
4603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4604 "parent '%s' is not a filesystem"), name);
4605 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4616 if (flags->verbose) {
4617 (void) printf("%s %s stream of %s into %s\n",
4618 flags->dryrun ? "would receive" : "receiving",
4619 drrb->drr_fromguid ? "incremental" : "full",
4620 drrb->drr_toname, destsnap);
4621 (void) fflush(stdout);
4625 * If this is the top-level dataset, record it so we can use it
4626 * for recursive operations later.
4628 if (top_zfs != NULL &&
4629 (*top_zfs == NULL || strcmp(*top_zfs, name) == 0)) {
4631 if (*top_zfs == NULL)
4632 *top_zfs = zfs_strdup(hdl, name);
4635 if (drrb->drr_type == DMU_OST_ZVOL) {
4636 type = ZFS_TYPE_VOLUME;
4637 } else if (drrb->drr_type == DMU_OST_ZFS) {
4638 type = ZFS_TYPE_FILESYSTEM;
4640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4641 "invalid record type: 0x%d"), drrb->drr_type);
4642 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4645 if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4646 stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4647 &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
4651 * When sending with properties (zfs send -p), the encryption property
4652 * is not included because it is a SETONCE property and therefore
4653 * treated as read only. However, we are always able to determine its
4654 * value because raw sends will include it in the DRR_BDEGIN payload
4655 * and non-raw sends with properties are not allowed for encrypted
4656 * datasets. Therefore, if this is a non-raw properties stream, we can
4657 * infer that the value should be ZIO_CRYPT_OFF and manually add that
4658 * to the received properties.
4660 if (stream_wantsnewfs && !raw && rcvprops != NULL &&
4661 !nvlist_exists(cmdprops, zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
4662 if (oxprops == NULL)
4663 oxprops = fnvlist_alloc();
4664 fnvlist_add_uint64(oxprops,
4665 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), ZIO_CRYPT_OFF);
4668 if (flags->dryrun) {
4669 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
4672 * We have read the DRR_BEGIN record, but we have
4673 * not yet read the payload. For non-dryrun sends
4674 * this will be done by the kernel, so we must
4675 * emulate that here, before attempting to read
4678 err = recv_read(hdl, infd, buf, drr->drr_payloadlen,
4679 flags->byteswap, NULL);
4684 err = recv_skip(hdl, infd, flags->byteswap);
4688 err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
4689 oxprops, wkeydata, wkeylen, origin, flags->force, flags->resumable,
4690 raw, infd, drr_noswap, -1, &read_bytes, &errflags,
4691 NULL, &prop_errors);
4692 ioctl_errno = ioctl_err;
4693 prop_errflags = errflags;
4696 nvpair_t *prop_err = NULL;
4698 while ((prop_err = nvlist_next_nvpair(prop_errors,
4699 prop_err)) != NULL) {
4704 prop = zfs_name_to_prop(nvpair_name(prop_err));
4705 (void) nvpair_value_int32(prop_err, &intval);
4706 if (strcmp(nvpair_name(prop_err),
4707 ZPROP_N_MORE_ERRORS) == 0) {
4708 trunc_prop_errs(intval);
4710 } else if (snapname == NULL || finalsnap == NULL ||
4711 strcmp(finalsnap, snapname) == 0 ||
4712 strcmp(nvpair_name(prop_err),
4713 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
4715 * Skip the special case of, for example,
4716 * "refquota", errors on intermediate
4717 * snapshots leading up to a final one.
4718 * That's why we have all of the checks above.
4720 * See zfs_ioctl.c's extract_delay_props() for
4721 * a list of props which can fail on
4722 * intermediate snapshots, but shouldn't
4723 * affect the overall receive.
4725 (void) snprintf(tbuf, sizeof (tbuf),
4726 dgettext(TEXT_DOMAIN,
4727 "cannot receive %s property on %s"),
4728 nvpair_name(prop_err), name);
4729 zfs_setprop_error(hdl, prop, intval, tbuf);
4734 if (err == 0 && snapprops_nvlist) {
4735 zfs_cmd_t zc = {"\0"};
4737 (void) strcpy(zc.zc_name, destsnap);
4738 zc.zc_cookie = B_TRUE; /* received */
4739 if (zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist) == 0) {
4740 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
4741 zcmd_free_nvlists(&zc);
4744 if (err == 0 && snapholds_nvlist) {
4746 nvlist_t *holds, *errors = NULL;
4747 int cleanup_fd = -1;
4749 VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
4750 for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
4752 pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
4753 fnvlist_add_string(holds, destsnap, nvpair_name(pair));
4755 (void) lzc_hold(holds, cleanup_fd, &errors);
4756 fnvlist_free(snapholds_nvlist);
4757 fnvlist_free(holds);
4760 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
4762 * It may be that this snapshot already exists,
4763 * in which case we want to consume & ignore it
4764 * rather than failing.
4766 avl_tree_t *local_avl;
4767 nvlist_t *local_nv, *fs;
4768 cp = strchr(destsnap, '@');
4771 * XXX Do this faster by just iterating over snaps in
4772 * this fs. Also if zc_value does not exist, we will
4773 * get a strange "does not exist" error message.
4776 if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
4777 B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE,
4778 B_TRUE, &local_nv, &local_avl) == 0) {
4780 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
4781 fsavl_destroy(local_avl);
4782 fnvlist_free(local_nv);
4785 if (flags->verbose) {
4786 (void) printf("snap %s already exists; "
4787 "ignoring\n", destsnap);
4789 err = ioctl_err = recv_skip(hdl, infd,
4796 if (ioctl_err != 0) {
4797 switch (ioctl_errno) {
4799 cp = strchr(destsnap, '@');
4801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4802 "most recent snapshot of %s does not\n"
4803 "match incremental source"), destsnap);
4804 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4809 "destination %s has been modified\n"
4810 "since most recent snapshot"), name);
4811 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4814 if (raw && stream_wantsnewfs) {
4815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4816 "failed to create encryption key"));
4817 } else if (raw && !stream_wantsnewfs) {
4818 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4819 "encryption key does not match "
4822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4823 "inherited key must be loaded"));
4825 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4828 cp = strchr(destsnap, '@');
4830 /* it's the containing fs that exists */
4833 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4834 "destination already exists"));
4835 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
4836 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
4841 if (flags->resumable) {
4842 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4843 "kernel modules must be upgraded to "
4844 "receive this stream."));
4845 } else if (embedded && !raw) {
4846 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4847 "incompatible embedded data stream "
4848 "feature with encrypted receive."));
4850 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4853 case ZFS_ERR_STREAM_TRUNCATED:
4854 recv_ecksum_set_aux(hdl, destsnap, flags->resumable,
4855 ioctl_err == ECKSUM);
4856 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4858 case ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH:
4859 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4860 "incremental send stream requires -L "
4861 "(--large-block), to match previous receive."));
4862 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4865 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4866 "pool must be upgraded to receive this stream."));
4867 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4870 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4871 "destination %s space quota exceeded."), name);
4872 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
4874 case ZFS_ERR_FROM_IVSET_GUID_MISSING:
4875 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4876 "IV set guid missing. See errata %u at "
4877 "https://openzfs.github.io/openzfs-docs/msg/"
4879 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
4880 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4882 case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
4883 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4884 "IV set guid mismatch. See the 'zfs receive' "
4885 "man page section\n discussing the limitations "
4886 "of raw encrypted send streams."));
4887 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4889 case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING:
4890 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4891 "Spill block flag missing for raw send.\n"
4892 "The zfs software on the sending system must "
4894 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4898 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4899 "destination %s contains "
4900 "partially-complete state from "
4901 "\"zfs receive -s\"."), name);
4902 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
4907 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
4912 * Mount the target filesystem (if created). Also mount any
4913 * children of the target filesystem if we did a replication
4914 * receive (indicated by stream_avl being non-NULL).
4917 if (!flags->nomount)
4918 err |= changelist_postfix(clp);
4919 changelist_free(clp);
4922 if ((newfs || stream_avl) && type == ZFS_TYPE_FILESYSTEM && !redacted)
4923 flags->domount = B_TRUE;
4925 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
4926 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4927 "failed to clear unreceived properties on %s"), name);
4928 (void) fprintf(stderr, "\n");
4930 if (prop_errflags & ZPROP_ERR_NORESTORE) {
4931 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4932 "failed to restore original properties on %s"), name);
4933 (void) fprintf(stderr, "\n");
4936 if (err || ioctl_err) {
4941 if (flags->verbose) {
4944 uint64_t bytes = read_bytes;
4945 time_t delta = time(NULL) - begin_time;
4948 zfs_nicebytes(bytes, buf1, sizeof (buf1));
4949 zfs_nicebytes(bytes/delta, buf2, sizeof (buf1));
4951 (void) printf("received %s stream in %lld seconds (%s/sec)\n",
4952 buf1, (longlong_t)delta, buf2);
4957 if (prop_errors != NULL)
4958 fnvlist_free(prop_errors);
4960 if (tmp_keylocation[0] != '\0') {
4961 fnvlist_add_string(rcvprops,
4962 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation);
4966 fnvlist_free(rcvprops);
4968 fnvlist_free(oxprops);
4969 fnvlist_free(origprops);
4975 * Check properties we were asked to override (both -o|-x)
4978 zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
4986 while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
4987 name = nvpair_name(nvp);
4988 prop = zfs_name_to_prop(name);
4990 if (prop == ZPROP_INVAL) {
4991 if (!zfs_prop_user(name)) {
4992 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4993 "%s: invalid property '%s'"), errbuf, name);
4999 * "origin" is readonly but is used to receive datasets as
5000 * clones so we don't raise an error here
5002 if (prop == ZFS_PROP_ORIGIN)
5005 /* encryption params have their own verification later */
5006 if (prop == ZFS_PROP_ENCRYPTION ||
5007 zfs_prop_encryption_key_param(prop))
5011 * cannot override readonly, set-once and other specific
5012 * settable properties
5014 if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
5015 prop == ZFS_PROP_VOLSIZE) {
5016 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5017 "%s: invalid property '%s'"), errbuf, name);
5026 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
5027 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
5028 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs,
5029 const char *finalsnap, nvlist_t *cmdprops)
5032 dmu_replay_record_t drr, drr_noswap;
5033 struct drr_begin *drrb = &drr.drr_u.drr_begin;
5035 zio_cksum_t zcksum = { { 0 } };
5036 uint64_t featureflags;
5039 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5042 /* check cmdline props, raise an error if they cannot be received */
5043 if (!zfs_receive_checkprops(hdl, cmdprops, errbuf))
5046 if (flags->isprefix &&
5047 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
5048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
5049 "(%s) does not exist"), tosnap);
5050 return (zfs_error(hdl, EZFS_NOENT, errbuf));
5053 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
5054 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
5055 "(%s) does not exist"), originsnap);
5056 return (zfs_error(hdl, EZFS_NOENT, errbuf));
5059 /* read in the BEGIN record */
5060 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
5064 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
5065 /* It's the double end record at the end of a package */
5069 /* the kernel needs the non-byteswapped begin record */
5072 flags->byteswap = B_FALSE;
5073 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
5075 * We computed the checksum in the wrong byteorder in
5076 * recv_read() above; do it again correctly.
5078 bzero(&zcksum, sizeof (zio_cksum_t));
5079 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
5080 flags->byteswap = B_TRUE;
5082 drr.drr_type = BSWAP_32(drr.drr_type);
5083 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
5084 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
5085 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
5086 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
5087 drrb->drr_type = BSWAP_32(drrb->drr_type);
5088 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
5089 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
5090 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
5093 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
5094 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5095 "stream (bad magic number)"));
5096 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5099 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
5100 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
5102 if (!DMU_STREAM_SUPPORTED(featureflags) ||
5103 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
5105 * Let's be explicit about this one, since rather than
5106 * being a new feature we can't know, it's an old
5107 * feature we dropped.
5109 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
5110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5111 "stream has deprecated feature: dedup, try "
5112 "'zstream redup [send in a file] | zfs recv "
5115 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5116 "stream has unsupported feature, feature flags = "
5117 "%llx (unknown flags = %llx)"),
5118 (u_longlong_t)featureflags,
5119 (u_longlong_t)((featureflags) &
5120 ~DMU_BACKUP_FEATURE_MASK));
5122 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5125 /* Holds feature is set once in the compound stream header. */
5126 if (featureflags & DMU_BACKUP_FEATURE_HOLDS)
5127 flags->holds = B_TRUE;
5129 if (strchr(drrb->drr_toname, '@') == NULL) {
5130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5131 "stream (bad snapshot name)"));
5132 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5135 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
5136 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
5137 if (sendfs == NULL) {
5139 * We were not called from zfs_receive_package(). Get
5140 * the fs specified by 'zfs send'.
5143 (void) strlcpy(nonpackage_sendfs,
5144 drr.drr_u.drr_begin.drr_toname,
5145 sizeof (nonpackage_sendfs));
5146 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
5148 sendfs = nonpackage_sendfs;
5149 VERIFY(finalsnap == NULL);
5151 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
5152 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
5153 finalsnap, cmdprops));
5155 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
5156 DMU_COMPOUNDSTREAM);
5157 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
5158 &zcksum, top_zfs, cmdprops));
5163 * Restores a backup of tosnap from the file descriptor specified by infd.
5164 * Return 0 on total success, -2 if some things couldn't be
5165 * destroyed/renamed/promoted, -1 if some things couldn't be received.
5166 * (-1 will override -2, if -1 and the resumable flag was specified the
5167 * transfer can be resumed if the sending side supports it).
5170 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
5171 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
5173 char *top_zfs = NULL;
5176 char *originsnap = NULL;
5179 * The only way fstat can fail is if we do not have a valid file
5182 if (fstat(infd, &sb) == -1) {
5188 * It is not uncommon for gigabytes to be processed in zfs receive.
5189 * Speculatively increase the buffer size if supported by the platform.
5191 if (S_ISFIFO(sb.st_mode))
5192 libzfs_set_pipe_max(infd);
5195 err = nvlist_lookup_string(props, "origin", &originsnap);
5196 if (err && err != ENOENT)
5200 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
5201 stream_avl, &top_zfs, NULL, props);
5203 if (err == 0 && !flags->nomount && flags->domount && top_zfs) {
5204 zfs_handle_t *zhp = NULL;
5205 prop_changelist_t *clp = NULL;
5207 zhp = zfs_open(hdl, top_zfs,
5208 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5213 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
5218 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
5219 CL_GATHER_MOUNT_ALWAYS,
5220 flags->forceunmount ? MS_FORCE : 0);
5227 /* mount and share received datasets */
5228 err = changelist_postfix(clp);
5229 changelist_free(clp);