zdb: zero-pad checksum output follow up
[zfs.git] / lib / libzfs / libzfs_sendrecv.c
blob66a22e333663d3c5c1f5b7b87b061b2fd0eeb179
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
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>.
27 * All rights reserved
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.
35 #include <assert.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <libintl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43 #include <stddef.h>
44 #include <fcntl.h>
45 #include <sys/mount.h>
46 #include <sys/mntent.h>
47 #include <sys/mnttab.h>
48 #include <sys/avl.h>
49 #include <sys/debug.h>
50 #include <sys/stat.h>
51 #include <pthread.h>
52 #include <umem.h>
53 #include <time.h>
55 #include <libzfs.h>
56 #include <libzfs_core.h>
57 #include <libzutil.h>
59 #include "zfs_namecheck.h"
60 #include "zfs_prop.h"
61 #include "zfs_fletcher.h"
62 #include "libzfs_impl.h"
63 #include <cityhash.h>
64 #include <zlib.h>
65 #include <sys/zio_checksum.h>
66 #include <sys/dsl_crypt.h>
67 #include <sys/ddt.h>
68 #include <sys/socket.h>
69 #include <sys/sha2.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 {
81 zfs_handle_t *pa_zhp;
82 int pa_fd;
83 boolean_t pa_parsable;
84 boolean_t pa_estimate;
85 int pa_verbosity;
86 boolean_t pa_astitle;
87 boolean_t pa_progress;
88 uint64_t pa_size;
89 } progress_arg_t;
91 static int
92 dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len,
93 zio_cksum_t *zc, int outfd)
95 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
96 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
97 fletcher_4_incremental_native(drr,
98 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
99 if (drr->drr_type != DRR_BEGIN) {
100 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
101 drr_checksum.drr_checksum));
102 drr->drr_u.drr_checksum.drr_checksum = *zc;
104 fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
105 sizeof (zio_cksum_t), zc);
106 if (write(outfd, drr, sizeof (*drr)) == -1)
107 return (errno);
108 if (payload_len != 0) {
109 fletcher_4_incremental_native(payload, payload_len, zc);
110 if (write(outfd, payload, payload_len) == -1)
111 return (errno);
113 return (0);
117 * Routines for dealing with the AVL tree of fs-nvlists
119 typedef struct fsavl_node {
120 avl_node_t fn_node;
121 nvlist_t *fn_nvfs;
122 char *fn_snapname;
123 uint64_t fn_guid;
124 } fsavl_node_t;
126 static int
127 fsavl_compare(const void *arg1, const void *arg2)
129 const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
130 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
132 return (TREE_CMP(fn1->fn_guid, fn2->fn_guid));
136 * Given the GUID of a snapshot, find its containing filesystem and
137 * (optionally) name.
139 static nvlist_t *
140 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
142 fsavl_node_t fn_find;
143 fsavl_node_t *fn;
145 fn_find.fn_guid = snapguid;
147 fn = avl_find(avl, &fn_find, NULL);
148 if (fn) {
149 if (snapname)
150 *snapname = fn->fn_snapname;
151 return (fn->fn_nvfs);
153 return (NULL);
156 static void
157 fsavl_destroy(avl_tree_t *avl)
159 fsavl_node_t *fn;
160 void *cookie;
162 if (avl == NULL)
163 return;
165 cookie = NULL;
166 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
167 free(fn);
168 avl_destroy(avl);
169 free(avl);
173 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
175 static avl_tree_t *
176 fsavl_create(nvlist_t *fss)
178 avl_tree_t *fsavl;
179 nvpair_t *fselem = NULL;
181 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
182 return (NULL);
184 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
185 offsetof(fsavl_node_t, fn_node));
187 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
188 nvlist_t *nvfs, *snaps;
189 nvpair_t *snapelem = NULL;
191 nvfs = fnvpair_value_nvlist(fselem);
192 snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
194 while ((snapelem =
195 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
196 fsavl_node_t *fn;
198 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
199 fsavl_destroy(fsavl);
200 return (NULL);
202 fn->fn_nvfs = nvfs;
203 fn->fn_snapname = nvpair_name(snapelem);
204 fn->fn_guid = fnvpair_value_uint64(snapelem);
207 * Note: if there are multiple snaps with the
208 * same GUID, we ignore all but one.
210 avl_index_t where = 0;
211 if (avl_find(fsavl, fn, &where) == NULL)
212 avl_insert(fsavl, fn, where);
213 else
214 free(fn);
218 return (fsavl);
222 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
224 typedef struct send_data {
226 * assigned inside every recursive call,
227 * restored from *_save on return:
229 * guid of fromsnap snapshot in parent dataset
230 * txg of fromsnap snapshot in current dataset
231 * txg of tosnap snapshot in current dataset
234 uint64_t parent_fromsnap_guid;
235 uint64_t fromsnap_txg;
236 uint64_t tosnap_txg;
238 /* the nvlists get accumulated during depth-first traversal */
239 nvlist_t *parent_snaps;
240 nvlist_t *fss;
241 nvlist_t *snapprops;
242 nvlist_t *snapholds; /* user holds */
244 /* send-receive configuration, does not change during traversal */
245 const char *fsname;
246 const char *fromsnap;
247 const char *tosnap;
248 boolean_t recursive;
249 boolean_t raw;
250 boolean_t doall;
251 boolean_t replicate;
252 boolean_t skipmissing;
253 boolean_t verbose;
254 boolean_t backup;
255 boolean_t seenfrom;
256 boolean_t seento;
257 boolean_t holds; /* were holds requested with send -h */
258 boolean_t props;
261 * The header nvlist is of the following format:
263 * "tosnap" -> string
264 * "fromsnap" -> string (if incremental)
265 * "fss" -> {
266 * id -> {
268 * "name" -> string (full name; for debugging)
269 * "parentfromsnap" -> number (guid of fromsnap in parent)
271 * "props" -> { name -> value (only if set here) }
272 * "snaps" -> { name (lastname) -> number (guid) }
273 * "snapprops" -> { name (lastname) -> { name -> value } }
274 * "snapholds" -> { name (lastname) -> { holdname -> crtime } }
276 * "origin" -> number (guid) (if clone)
277 * "is_encroot" -> boolean
278 * "sent" -> boolean (not on-disk)
284 } send_data_t;
286 static void
287 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
290 * Collect guid, valid props, optionally holds, etc. of a snapshot.
291 * This interface is intended for use as a zfs_iter_snapshots_sorted visitor.
293 static int
294 send_iterate_snap(zfs_handle_t *zhp, void *arg)
296 send_data_t *sd = arg;
297 uint64_t guid = zhp->zfs_dmustats.dds_guid;
298 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
299 boolean_t isfromsnap, istosnap, istosnapwithnofrom;
300 char *snapname;
301 const char *from = sd->fromsnap;
302 const char *to = sd->tosnap;
304 snapname = strrchr(zhp->zfs_name, '@');
305 assert(snapname != NULL);
306 ++snapname;
308 isfromsnap = (from != NULL && strcmp(from, snapname) == 0);
309 istosnap = (to != NULL && strcmp(to, snapname) == 0);
310 istosnapwithnofrom = (istosnap && from == NULL);
312 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
313 if (sd->verbose) {
314 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
315 "skipping snapshot %s because it was created "
316 "after the destination snapshot (%s)\n"),
317 zhp->zfs_name, to);
319 zfs_close(zhp);
320 return (0);
323 fnvlist_add_uint64(sd->parent_snaps, snapname, guid);
326 * NB: if there is no fromsnap here (it's a newly created fs in
327 * an incremental replication), we will substitute the tosnap.
329 if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap))
330 sd->parent_fromsnap_guid = guid;
332 if (!sd->recursive) {
334 * To allow a doall stream to work properly
335 * with a NULL fromsnap
337 if (sd->doall && from == NULL && !sd->seenfrom)
338 sd->seenfrom = B_TRUE;
340 if (!sd->seenfrom && isfromsnap) {
341 sd->seenfrom = B_TRUE;
342 zfs_close(zhp);
343 return (0);
346 if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) {
347 zfs_close(zhp);
348 return (0);
351 if (istosnap)
352 sd->seento = B_TRUE;
355 nvlist_t *nv = fnvlist_alloc();
356 send_iterate_prop(zhp, sd->backup, nv);
357 fnvlist_add_nvlist(sd->snapprops, snapname, nv);
358 fnvlist_free(nv);
360 if (sd->holds) {
361 nvlist_t *holds;
362 if (lzc_get_holds(zhp->zfs_name, &holds) == 0) {
363 fnvlist_add_nvlist(sd->snapholds, snapname, holds);
364 fnvlist_free(holds);
368 zfs_close(zhp);
369 return (0);
373 * Collect all valid props from the handle snap into an nvlist.
375 static void
376 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
378 nvlist_t *props;
380 if (received_only)
381 props = zfs_get_recvd_props(zhp);
382 else
383 props = zhp->zfs_props;
385 nvpair_t *elem = NULL;
386 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
387 char *propname = nvpair_name(elem);
388 zfs_prop_t prop = zfs_name_to_prop(propname);
390 if (!zfs_prop_user(propname)) {
392 * Realistically, this should never happen. However,
393 * we want the ability to add DSL properties without
394 * needing to make incompatible version changes. We
395 * need to ignore unknown properties to allow older
396 * software to still send datasets containing these
397 * properties, with the unknown properties elided.
399 if (prop == ZPROP_INVAL)
400 continue;
402 if (zfs_prop_readonly(prop))
403 continue;
406 nvlist_t *propnv = fnvpair_value_nvlist(elem);
408 boolean_t isspacelimit = (prop == ZFS_PROP_QUOTA ||
409 prop == ZFS_PROP_RESERVATION ||
410 prop == ZFS_PROP_REFQUOTA ||
411 prop == ZFS_PROP_REFRESERVATION);
412 if (isspacelimit && zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
413 continue;
415 char *source;
416 if (nvlist_lookup_string(propnv, ZPROP_SOURCE, &source) == 0) {
417 if (strcmp(source, zhp->zfs_name) != 0 &&
418 strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
419 continue;
420 } else {
422 * May have no source before SPA_VERSION_RECVD_PROPS,
423 * but is still modifiable.
425 if (!isspacelimit)
426 continue;
429 if (zfs_prop_user(propname) ||
430 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
431 char *value;
432 value = fnvlist_lookup_string(propnv, ZPROP_VALUE);
433 fnvlist_add_string(nv, propname, value);
434 } else {
435 uint64_t value;
436 value = fnvlist_lookup_uint64(propnv, ZPROP_VALUE);
437 fnvlist_add_uint64(nv, propname, value);
443 * returns snapshot guid
444 * and returns 0 if the snapshot does not exist
446 static uint64_t
447 get_snap_guid(libzfs_handle_t *hdl, const char *fs, const char *snap)
449 char name[MAXPATHLEN + 1];
450 uint64_t guid = 0;
452 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
453 return (guid);
455 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
456 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
457 if (zhp != NULL) {
458 guid = zfs_prop_get_int(zhp, ZFS_PROP_GUID);
459 zfs_close(zhp);
462 return (guid);
466 * returns snapshot creation txg
467 * and returns 0 if the snapshot does not exist
469 static uint64_t
470 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
472 char name[ZFS_MAX_DATASET_NAME_LEN];
473 uint64_t txg = 0;
475 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
476 return (txg);
478 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
479 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
480 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
481 if (zhp != NULL) {
482 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
483 zfs_close(zhp);
487 return (txg);
491 * Recursively generate nvlists describing datasets. See comment
492 * for the data structure send_data_t above for description of contents
493 * of the nvlist.
495 static int
496 send_iterate_fs(zfs_handle_t *zhp, void *arg)
498 send_data_t *sd = arg;
499 nvlist_t *nvfs = NULL, *nv = NULL;
500 int rv = 0;
501 uint64_t min_txg = 0, max_txg = 0;
502 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
503 uint64_t guid = zhp->zfs_dmustats.dds_guid;
504 uint64_t fromsnap_txg, tosnap_txg;
505 char guidstring[64];
507 /* These fields are restored on return from a recursive call. */
508 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
509 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
510 uint64_t tosnap_txg_save = sd->tosnap_txg;
512 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
513 if (fromsnap_txg != 0)
514 sd->fromsnap_txg = fromsnap_txg;
516 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
517 if (tosnap_txg != 0)
518 sd->tosnap_txg = tosnap_txg;
521 * On the send side, if the current dataset does not have tosnap,
522 * perform two additional checks:
524 * - Skip sending the current dataset if it was created later than
525 * the parent tosnap.
526 * - Return error if the current dataset was created earlier than
527 * the parent tosnap, unless --skip-missing specified. Then
528 * just print a warning.
530 if (sd->tosnap != NULL && tosnap_txg == 0) {
531 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
532 if (sd->verbose) {
533 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
534 "skipping dataset %s: snapshot %s does "
535 "not exist\n"), zhp->zfs_name, sd->tosnap);
537 } else if (sd->skipmissing) {
538 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
539 "WARNING: skipping dataset %s and its children:"
540 " snapshot %s does not exist\n"),
541 zhp->zfs_name, sd->tosnap);
542 } else {
543 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
544 "cannot send %s@%s%s: snapshot %s@%s does not "
545 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
546 dgettext(TEXT_DOMAIN, " recursively") : "",
547 zhp->zfs_name, sd->tosnap);
548 rv = EZFS_NOENT;
550 goto out;
553 nvfs = fnvlist_alloc();
554 fnvlist_add_string(nvfs, "name", zhp->zfs_name);
555 fnvlist_add_uint64(nvfs, "parentfromsnap", sd->parent_fromsnap_guid);
557 if (zhp->zfs_dmustats.dds_origin[0] != '\0') {
558 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
559 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
560 if (origin == NULL) {
561 rv = -1;
562 goto out;
564 fnvlist_add_uint64(nvfs, "origin",
565 origin->zfs_dmustats.dds_guid);
566 zfs_close(origin);
569 /* Iterate over props. */
570 if (sd->props || sd->backup || sd->recursive) {
571 nv = fnvlist_alloc();
572 send_iterate_prop(zhp, sd->backup, nv);
573 fnvlist_add_nvlist(nvfs, "props", nv);
575 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
576 boolean_t encroot;
578 /* Determine if this dataset is an encryption root. */
579 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
580 rv = -1;
581 goto out;
584 if (encroot)
585 fnvlist_add_boolean(nvfs, "is_encroot");
588 * Encrypted datasets can only be sent with properties if
589 * the raw flag is specified because the receive side doesn't
590 * currently have a mechanism for recursively asking the user
591 * for new encryption parameters.
593 if (!sd->raw) {
594 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
595 "cannot send %s@%s: encrypted dataset %s may not "
596 "be sent with properties without the raw flag\n"),
597 sd->fsname, sd->tosnap, zhp->zfs_name);
598 rv = -1;
599 goto out;
605 * Iterate over snaps, and set sd->parent_fromsnap_guid.
607 * If this is a "doall" send, a replicate send or we're just trying
608 * to gather a list of previous snapshots, iterate through all the
609 * snaps in the txg range. Otherwise just look at the one we're
610 * interested in.
612 sd->parent_fromsnap_guid = 0;
613 sd->parent_snaps = fnvlist_alloc();
614 sd->snapprops = fnvlist_alloc();
615 if (sd->holds)
616 sd->snapholds = fnvlist_alloc();
617 if (sd->doall || sd->replicate || sd->tosnap == NULL) {
618 if (!sd->replicate && fromsnap_txg != 0)
619 min_txg = fromsnap_txg;
620 if (!sd->replicate && tosnap_txg != 0)
621 max_txg = tosnap_txg;
622 (void) zfs_iter_snapshots_sorted(zhp, 0, send_iterate_snap, sd,
623 min_txg, max_txg);
624 } else {
625 char snapname[MAXPATHLEN] = { 0 };
626 zfs_handle_t *snap;
628 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
629 zhp->zfs_name, sd->tosnap);
630 if (sd->fromsnap != NULL)
631 sd->seenfrom = B_TRUE;
632 snap = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
633 if (snap != NULL)
634 (void) send_iterate_snap(snap, sd);
637 fnvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps);
638 fnvlist_free(sd->parent_snaps);
639 fnvlist_add_nvlist(nvfs, "snapprops", sd->snapprops);
640 fnvlist_free(sd->snapprops);
641 if (sd->holds) {
642 fnvlist_add_nvlist(nvfs, "snapholds", sd->snapholds);
643 fnvlist_free(sd->snapholds);
646 /* Do not allow the size of the properties list to exceed the limit */
647 if ((fnvlist_size(nvfs) + fnvlist_size(sd->fss)) >
648 zhp->zfs_hdl->libzfs_max_nvlist) {
649 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
650 "warning: cannot send %s@%s: the size of the list of "
651 "snapshots and properties is too large to be received "
652 "successfully.\n"
653 "Select a smaller number of snapshots to send.\n"),
654 zhp->zfs_name, sd->tosnap);
655 rv = EZFS_NOSPC;
656 goto out;
658 /* Add this fs to nvlist. */
659 (void) snprintf(guidstring, sizeof (guidstring),
660 "0x%llx", (longlong_t)guid);
661 fnvlist_add_nvlist(sd->fss, guidstring, nvfs);
663 /* Iterate over children. */
664 if (sd->recursive)
665 rv = zfs_iter_filesystems(zhp, 0, send_iterate_fs, sd);
667 out:
668 /* Restore saved fields. */
669 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
670 sd->fromsnap_txg = fromsnap_txg_save;
671 sd->tosnap_txg = tosnap_txg_save;
673 fnvlist_free(nv);
674 fnvlist_free(nvfs);
676 zfs_close(zhp);
677 return (rv);
680 static int
681 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
682 const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t doall,
683 boolean_t replicate, boolean_t skipmissing, boolean_t verbose,
684 boolean_t backup, boolean_t holds, boolean_t props, nvlist_t **nvlp,
685 avl_tree_t **avlp)
687 zfs_handle_t *zhp;
688 send_data_t sd = { 0 };
689 int error;
691 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
692 if (zhp == NULL)
693 return (EZFS_BADTYPE);
695 sd.fss = fnvlist_alloc();
696 sd.fsname = fsname;
697 sd.fromsnap = fromsnap;
698 sd.tosnap = tosnap;
699 sd.recursive = recursive;
700 sd.raw = raw;
701 sd.doall = doall;
702 sd.replicate = replicate;
703 sd.skipmissing = skipmissing;
704 sd.verbose = verbose;
705 sd.backup = backup;
706 sd.holds = holds;
707 sd.props = props;
709 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
710 fnvlist_free(sd.fss);
711 if (avlp != NULL)
712 *avlp = NULL;
713 *nvlp = NULL;
714 return (error);
717 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
718 fnvlist_free(sd.fss);
719 *nvlp = NULL;
720 return (EZFS_NOMEM);
723 *nvlp = sd.fss;
724 return (0);
728 * Routines specific to "zfs send"
730 typedef struct send_dump_data {
731 /* these are all just the short snapname (the part after the @) */
732 const char *fromsnap;
733 const char *tosnap;
734 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
735 uint64_t prevsnap_obj;
736 boolean_t seenfrom, seento, replicate, doall, fromorigin;
737 boolean_t dryrun, parsable, progress, embed_data, std_out;
738 boolean_t large_block, compress, raw, holds;
739 boolean_t progressastitle;
740 int outfd;
741 boolean_t err;
742 nvlist_t *fss;
743 nvlist_t *snapholds;
744 avl_tree_t *fsavl;
745 snapfilter_cb_t *filter_cb;
746 void *filter_cb_arg;
747 nvlist_t *debugnv;
748 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
749 int cleanup_fd;
750 int verbosity;
751 uint64_t size;
752 } send_dump_data_t;
754 static int
755 zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
756 enum lzc_send_flags flags, uint64_t *spacep)
758 assert(snapname != NULL);
760 int error = lzc_send_space(snapname, from, flags, spacep);
761 if (error == 0)
762 return (0);
764 char errbuf[ERRBUFLEN];
765 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
766 "warning: cannot estimate space for '%s'"), snapname);
768 libzfs_handle_t *hdl = zhp->zfs_hdl;
769 switch (error) {
770 case EXDEV:
771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
772 "not an earlier snapshot from the same fs"));
773 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
775 case ENOENT:
776 if (zfs_dataset_exists(hdl, snapname,
777 ZFS_TYPE_SNAPSHOT)) {
778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
779 "incremental source (%s) does not exist"),
780 snapname);
782 return (zfs_error(hdl, EZFS_NOENT, errbuf));
784 case EDQUOT:
785 case EFBIG:
786 case EIO:
787 case ENOLINK:
788 case ENOSPC:
789 case ENOSTR:
790 case ENXIO:
791 case EPIPE:
792 case ERANGE:
793 case EFAULT:
794 case EROFS:
795 case EINVAL:
796 zfs_error_aux(hdl, "%s", strerror(error));
797 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
799 default:
800 return (zfs_standard_error(hdl, error, errbuf));
805 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
806 * NULL) to the file descriptor specified by outfd.
808 static int
809 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
810 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
811 nvlist_t *debugnv)
813 zfs_cmd_t zc = {"\0"};
814 libzfs_handle_t *hdl = zhp->zfs_hdl;
815 nvlist_t *thisdbg;
817 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
818 assert(fromsnap_obj == 0 || !fromorigin);
820 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
821 zc.zc_cookie = outfd;
822 zc.zc_obj = fromorigin;
823 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
824 zc.zc_fromobj = fromsnap_obj;
825 zc.zc_flags = flags;
827 if (debugnv != NULL) {
828 thisdbg = fnvlist_alloc();
829 if (fromsnap != NULL && fromsnap[0] != '\0')
830 fnvlist_add_string(thisdbg, "fromsnap", fromsnap);
833 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
834 char errbuf[ERRBUFLEN];
835 int error = errno;
837 (void) snprintf(errbuf, sizeof (errbuf), "%s '%s'",
838 dgettext(TEXT_DOMAIN, "warning: cannot send"),
839 zhp->zfs_name);
841 if (debugnv != NULL) {
842 fnvlist_add_uint64(thisdbg, "error", error);
843 fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
844 fnvlist_free(thisdbg);
847 switch (error) {
848 case EXDEV:
849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
850 "not an earlier snapshot from the same fs"));
851 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
853 case EACCES:
854 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
855 "source key must be loaded"));
856 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
858 case ENOENT:
859 if (zfs_dataset_exists(hdl, zc.zc_name,
860 ZFS_TYPE_SNAPSHOT)) {
861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
862 "incremental source (@%s) does not exist"),
863 zc.zc_value);
865 return (zfs_error(hdl, EZFS_NOENT, errbuf));
867 case EDQUOT:
868 case EFBIG:
869 case EIO:
870 case ENOLINK:
871 case ENOSPC:
872 case ENOSTR:
873 case ENXIO:
874 case EPIPE:
875 case ERANGE:
876 case EFAULT:
877 case EROFS:
878 case EINVAL:
879 zfs_error_aux(hdl, "%s", strerror(errno));
880 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
882 default:
883 return (zfs_standard_error(hdl, errno, errbuf));
887 if (debugnv != NULL) {
888 fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
889 fnvlist_free(thisdbg);
892 return (0);
895 static void
896 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
898 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
901 * zfs_send() only sets snapholds for sends that need them,
902 * e.g. replication and doall.
904 if (sdd->snapholds == NULL)
905 return;
907 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
911 zfs_send_progress(zfs_handle_t *zhp, int fd, uint64_t *bytes_written,
912 uint64_t *blocks_visited)
914 zfs_cmd_t zc = {"\0"};
916 if (bytes_written != NULL)
917 *bytes_written = 0;
918 if (blocks_visited != NULL)
919 *blocks_visited = 0;
920 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
921 zc.zc_cookie = fd;
922 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
923 return (errno);
924 if (bytes_written != NULL)
925 *bytes_written = zc.zc_cookie;
926 if (blocks_visited != NULL)
927 *blocks_visited = zc.zc_objset_type;
928 return (0);
931 static void *
932 send_progress_thread(void *arg)
934 progress_arg_t *pa = arg;
935 zfs_handle_t *zhp = pa->pa_zhp;
936 uint64_t bytes;
937 uint64_t blocks;
938 uint64_t total = pa->pa_size / 100;
939 char buf[16];
940 time_t t;
941 struct tm tm;
942 int err;
944 if (!pa->pa_parsable && pa->pa_progress) {
945 (void) fprintf(stderr,
946 "TIME %s %sSNAPSHOT %s\n",
947 pa->pa_estimate ? "BYTES" : " SENT",
948 pa->pa_verbosity >= 2 ? " BLOCKS " : "",
949 zhp->zfs_name);
953 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
955 for (;;) {
956 (void) sleep(1);
957 if ((err = zfs_send_progress(zhp, pa->pa_fd, &bytes,
958 &blocks)) != 0) {
959 if (err == EINTR || err == ENOENT)
960 return ((void *)0);
961 return ((void *)(uintptr_t)err);
964 (void) time(&t);
965 localtime_r(&t, &tm);
967 if (pa->pa_astitle) {
968 char buf_bytes[16];
969 char buf_size[16];
970 int pct;
971 zfs_nicenum(bytes, buf_bytes, sizeof (buf_bytes));
972 zfs_nicenum(pa->pa_size, buf_size, sizeof (buf_size));
973 pct = (total > 0) ? bytes / total : 100;
974 zfs_setproctitle("sending %s (%d%%: %s/%s)",
975 zhp->zfs_name, MIN(pct, 100), buf_bytes, buf_size);
978 if (pa->pa_verbosity >= 2 && pa->pa_parsable) {
979 (void) fprintf(stderr,
980 "%02d:%02d:%02d\t%llu\t%llu\t%s\n",
981 tm.tm_hour, tm.tm_min, tm.tm_sec,
982 (u_longlong_t)bytes, (u_longlong_t)blocks,
983 zhp->zfs_name);
984 } else if (pa->pa_verbosity >= 2) {
985 zfs_nicenum(bytes, buf, sizeof (buf));
986 (void) fprintf(stderr,
987 "%02d:%02d:%02d %5s %8llu %s\n",
988 tm.tm_hour, tm.tm_min, tm.tm_sec,
989 buf, (u_longlong_t)blocks, zhp->zfs_name);
990 } else if (pa->pa_parsable) {
991 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
992 tm.tm_hour, tm.tm_min, tm.tm_sec,
993 (u_longlong_t)bytes, zhp->zfs_name);
994 } else if (pa->pa_progress) {
995 zfs_nicebytes(bytes, buf, sizeof (buf));
996 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
997 tm.tm_hour, tm.tm_min, tm.tm_sec,
998 buf, zhp->zfs_name);
1003 static boolean_t
1004 send_progress_thread_exit(libzfs_handle_t *hdl, pthread_t ptid)
1006 void *status = NULL;
1007 (void) pthread_cancel(ptid);
1008 (void) pthread_join(ptid, &status);
1009 int error = (int)(uintptr_t)status;
1010 if (error != 0 && status != PTHREAD_CANCELED)
1011 return (zfs_standard_error(hdl, error,
1012 dgettext(TEXT_DOMAIN, "progress thread exited nonzero")));
1013 else
1014 return (B_FALSE);
1017 static void
1018 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1019 uint64_t size, boolean_t parsable)
1021 if (parsable) {
1022 if (fromsnap != NULL) {
1023 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1024 "incremental\t%s\t%s"), fromsnap, tosnap);
1025 } else {
1027 * Workaround for GCC 12+ with UBSan enabled deficencies.
1029 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1030 * below as violating -Wformat-overflow.
1032 #if defined(__GNUC__) && !defined(__clang__) && \
1033 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1034 #pragma GCC diagnostic push
1035 #pragma GCC diagnostic ignored "-Wformat-overflow"
1036 #endif
1037 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1038 "full\t%s"), tosnap);
1039 #if defined(__GNUC__) && !defined(__clang__) && \
1040 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1041 #pragma GCC diagnostic pop
1042 #endif
1044 (void) fprintf(fout, "\t%llu", (longlong_t)size);
1045 } else {
1046 if (fromsnap != NULL) {
1047 if (strchr(fromsnap, '@') == NULL &&
1048 strchr(fromsnap, '#') == NULL) {
1049 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1050 "send from @%s to %s"), fromsnap, tosnap);
1051 } else {
1052 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1053 "send from %s to %s"), fromsnap, tosnap);
1055 } else {
1056 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1057 "full send of %s"), tosnap);
1059 if (size != 0) {
1060 char buf[16];
1061 zfs_nicebytes(size, buf, sizeof (buf));
1063 * Workaround for GCC 12+ with UBSan enabled deficencies.
1065 * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1066 * below as violating -Wformat-overflow.
1068 #if defined(__GNUC__) && !defined(__clang__) && \
1069 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1070 #pragma GCC diagnostic push
1071 #pragma GCC diagnostic ignored "-Wformat-overflow"
1072 #endif
1073 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1074 " estimated size is %s"), buf);
1075 #if defined(__GNUC__) && !defined(__clang__) && \
1076 defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1077 #pragma GCC diagnostic pop
1078 #endif
1081 (void) fprintf(fout, "\n");
1085 * Send a single filesystem snapshot, updating the send dump data.
1086 * This interface is intended for use as a zfs_iter_snapshots_sorted visitor.
1088 static int
1089 dump_snapshot(zfs_handle_t *zhp, void *arg)
1091 send_dump_data_t *sdd = arg;
1092 progress_arg_t pa = { 0 };
1093 pthread_t tid;
1094 char *thissnap;
1095 enum lzc_send_flags flags = 0;
1096 int err;
1097 boolean_t isfromsnap, istosnap, fromorigin;
1098 boolean_t exclude = B_FALSE;
1099 FILE *fout = sdd->std_out ? stdout : stderr;
1101 err = 0;
1102 thissnap = strchr(zhp->zfs_name, '@') + 1;
1103 isfromsnap = (sdd->fromsnap != NULL &&
1104 strcmp(sdd->fromsnap, thissnap) == 0);
1106 if (!sdd->seenfrom && isfromsnap) {
1107 gather_holds(zhp, sdd);
1108 sdd->seenfrom = B_TRUE;
1109 (void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap));
1110 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1111 zfs_close(zhp);
1112 return (0);
1115 if (sdd->seento || !sdd->seenfrom) {
1116 zfs_close(zhp);
1117 return (0);
1120 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1121 if (istosnap)
1122 sdd->seento = B_TRUE;
1124 if (sdd->large_block)
1125 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1126 if (sdd->embed_data)
1127 flags |= LZC_SEND_FLAG_EMBED_DATA;
1128 if (sdd->compress)
1129 flags |= LZC_SEND_FLAG_COMPRESS;
1130 if (sdd->raw)
1131 flags |= LZC_SEND_FLAG_RAW;
1133 if (!sdd->doall && !isfromsnap && !istosnap) {
1134 if (sdd->replicate) {
1135 char *snapname;
1136 nvlist_t *snapprops;
1138 * Filter out all intermediate snapshots except origin
1139 * snapshots needed to replicate clones.
1141 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1142 zhp->zfs_dmustats.dds_guid, &snapname);
1144 if (nvfs != NULL) {
1145 snapprops = fnvlist_lookup_nvlist(nvfs,
1146 "snapprops");
1147 snapprops = fnvlist_lookup_nvlist(snapprops,
1148 thissnap);
1149 exclude = !nvlist_exists(snapprops,
1150 "is_clone_origin");
1152 } else {
1153 exclude = B_TRUE;
1158 * If a filter function exists, call it to determine whether
1159 * this snapshot will be sent.
1161 if (exclude || (sdd->filter_cb != NULL &&
1162 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1164 * This snapshot is filtered out. Don't send it, and don't
1165 * set prevsnap_obj, so it will be as if this snapshot didn't
1166 * exist, and the next accepted snapshot will be sent as
1167 * an incremental from the last accepted one, or as the
1168 * first (and full) snapshot in the case of a replication,
1169 * non-incremental send.
1171 zfs_close(zhp);
1172 return (0);
1175 gather_holds(zhp, sdd);
1176 fromorigin = sdd->prevsnap[0] == '\0' &&
1177 (sdd->fromorigin || sdd->replicate);
1179 if (sdd->verbosity != 0) {
1180 uint64_t size = 0;
1181 char fromds[ZFS_MAX_DATASET_NAME_LEN];
1183 if (sdd->prevsnap[0] != '\0') {
1184 (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1185 *(strchr(fromds, '@') + 1) = '\0';
1186 (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1188 if (zfs_send_space(zhp, zhp->zfs_name,
1189 sdd->prevsnap[0] ? fromds : NULL, flags, &size) == 0) {
1190 send_print_verbose(fout, zhp->zfs_name,
1191 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1192 size, sdd->parsable);
1193 sdd->size += size;
1197 if (!sdd->dryrun) {
1199 * If progress reporting is requested, spawn a new thread to
1200 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1202 if (sdd->progress || sdd->progressastitle) {
1203 pa.pa_zhp = zhp;
1204 pa.pa_fd = sdd->outfd;
1205 pa.pa_parsable = sdd->parsable;
1206 pa.pa_estimate = B_FALSE;
1207 pa.pa_verbosity = sdd->verbosity;
1208 pa.pa_size = sdd->size;
1209 pa.pa_astitle = sdd->progressastitle;
1210 pa.pa_progress = sdd->progress;
1212 if ((err = pthread_create(&tid, NULL,
1213 send_progress_thread, &pa)) != 0) {
1214 zfs_close(zhp);
1215 return (err);
1219 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1220 fromorigin, sdd->outfd, flags, sdd->debugnv);
1222 if ((sdd->progress || sdd->progressastitle) &&
1223 send_progress_thread_exit(zhp->zfs_hdl, tid))
1224 return (-1);
1227 (void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap));
1228 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1229 zfs_close(zhp);
1230 return (err);
1234 * Send all snapshots for a filesystem, updating the send dump data.
1236 static int
1237 dump_filesystem(zfs_handle_t *zhp, send_dump_data_t *sdd)
1239 int rv = 0;
1240 boolean_t missingfrom = B_FALSE;
1241 zfs_cmd_t zc = {"\0"};
1242 uint64_t min_txg = 0, max_txg = 0;
1245 * Make sure the tosnap exists.
1247 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1248 zhp->zfs_name, sdd->tosnap);
1249 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1250 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1251 "WARNING: could not send %s@%s: does not exist\n"),
1252 zhp->zfs_name, sdd->tosnap);
1253 sdd->err = B_TRUE;
1254 return (0);
1258 * If this fs does not have fromsnap, and we're doing
1259 * recursive, we need to send a full stream from the
1260 * beginning (or an incremental from the origin if this
1261 * is a clone). If we're doing non-recursive, then let
1262 * them get the error.
1264 if (sdd->replicate && sdd->fromsnap) {
1266 * Make sure the fromsnap exists.
1268 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1269 zhp->zfs_name, sdd->fromsnap);
1270 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0)
1271 missingfrom = B_TRUE;
1274 sdd->seenfrom = sdd->seento = B_FALSE;
1275 sdd->prevsnap[0] = '\0';
1276 sdd->prevsnap_obj = 0;
1277 if (sdd->fromsnap == NULL || missingfrom)
1278 sdd->seenfrom = B_TRUE;
1281 * Iterate through all snapshots and process the ones we will be
1282 * sending. If we only have a "from" and "to" snapshot to deal
1283 * with, we can avoid iterating through all the other snapshots.
1285 if (sdd->doall || sdd->replicate || sdd->tosnap == NULL) {
1286 if (!sdd->replicate) {
1287 if (sdd->fromsnap != NULL) {
1288 min_txg = get_snap_txg(zhp->zfs_hdl,
1289 zhp->zfs_name, sdd->fromsnap);
1291 if (sdd->tosnap != NULL) {
1292 max_txg = get_snap_txg(zhp->zfs_hdl,
1293 zhp->zfs_name, sdd->tosnap);
1296 rv = zfs_iter_snapshots_sorted(zhp, 0, dump_snapshot, sdd,
1297 min_txg, max_txg);
1298 } else {
1299 char snapname[MAXPATHLEN] = { 0 };
1300 zfs_handle_t *snap;
1302 /* Dump fromsnap. */
1303 if (!sdd->seenfrom) {
1304 (void) snprintf(snapname, sizeof (snapname),
1305 "%s@%s", zhp->zfs_name, sdd->fromsnap);
1306 snap = zfs_open(zhp->zfs_hdl, snapname,
1307 ZFS_TYPE_SNAPSHOT);
1308 if (snap != NULL)
1309 rv = dump_snapshot(snap, sdd);
1310 else
1311 rv = errno;
1314 /* Dump tosnap. */
1315 if (rv == 0) {
1316 (void) snprintf(snapname, sizeof (snapname),
1317 "%s@%s", zhp->zfs_name, sdd->tosnap);
1318 snap = zfs_open(zhp->zfs_hdl, snapname,
1319 ZFS_TYPE_SNAPSHOT);
1320 if (snap != NULL)
1321 rv = dump_snapshot(snap, sdd);
1322 else
1323 rv = errno;
1327 if (!sdd->seenfrom) {
1328 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1329 "WARNING: could not send %s@%s:\n"
1330 "incremental source (%s@%s) does not exist\n"),
1331 zhp->zfs_name, sdd->tosnap,
1332 zhp->zfs_name, sdd->fromsnap);
1333 sdd->err = B_TRUE;
1334 } else if (!sdd->seento) {
1335 if (sdd->fromsnap) {
1336 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1337 "WARNING: could not send %s@%s:\n"
1338 "incremental source (%s@%s) "
1339 "is not earlier than it\n"),
1340 zhp->zfs_name, sdd->tosnap,
1341 zhp->zfs_name, sdd->fromsnap);
1342 } else {
1343 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1344 "WARNING: "
1345 "could not send %s@%s: does not exist\n"),
1346 zhp->zfs_name, sdd->tosnap);
1348 sdd->err = B_TRUE;
1351 return (rv);
1355 * Send all snapshots for all filesystems in sdd.
1357 static int
1358 dump_filesystems(zfs_handle_t *rzhp, send_dump_data_t *sdd)
1360 nvpair_t *fspair;
1361 boolean_t needagain, progress;
1363 if (!sdd->replicate)
1364 return (dump_filesystem(rzhp, sdd));
1366 /* Mark the clone origin snapshots. */
1367 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1368 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1369 nvlist_t *nvfs;
1370 uint64_t origin_guid = 0;
1372 nvfs = fnvpair_value_nvlist(fspair);
1373 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1374 if (origin_guid != 0) {
1375 char *snapname;
1376 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1377 origin_guid, &snapname);
1378 if (origin_nv != NULL) {
1379 nvlist_t *snapprops;
1380 snapprops = fnvlist_lookup_nvlist(origin_nv,
1381 "snapprops");
1382 snapprops = fnvlist_lookup_nvlist(snapprops,
1383 snapname);
1384 fnvlist_add_boolean(snapprops,
1385 "is_clone_origin");
1389 again:
1390 needagain = progress = B_FALSE;
1391 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1392 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1393 nvlist_t *fslist, *parent_nv;
1394 char *fsname;
1395 zfs_handle_t *zhp;
1396 int err;
1397 uint64_t origin_guid = 0;
1398 uint64_t parent_guid = 0;
1400 fslist = fnvpair_value_nvlist(fspair);
1401 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1402 continue;
1404 fsname = fnvlist_lookup_string(fslist, "name");
1405 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1406 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1407 &parent_guid);
1409 if (parent_guid != 0) {
1410 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1411 if (!nvlist_exists(parent_nv, "sent")) {
1412 /* Parent has not been sent; skip this one. */
1413 needagain = B_TRUE;
1414 continue;
1418 if (origin_guid != 0) {
1419 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1420 origin_guid, NULL);
1421 if (origin_nv != NULL &&
1422 !nvlist_exists(origin_nv, "sent")) {
1424 * Origin has not been sent yet;
1425 * skip this clone.
1427 needagain = B_TRUE;
1428 continue;
1432 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1433 if (zhp == NULL)
1434 return (-1);
1435 err = dump_filesystem(zhp, sdd);
1436 fnvlist_add_boolean(fslist, "sent");
1437 progress = B_TRUE;
1438 zfs_close(zhp);
1439 if (err)
1440 return (err);
1442 if (needagain) {
1443 assert(progress);
1444 goto again;
1447 /* Clean out the sent flags in case we reuse this fss. */
1448 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1449 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1450 nvlist_t *fslist;
1452 fslist = fnvpair_value_nvlist(fspair);
1453 (void) nvlist_remove_all(fslist, "sent");
1456 return (0);
1459 nvlist_t *
1460 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1462 unsigned int version;
1463 int nread, i;
1464 unsigned long long checksum, packed_len;
1467 * Decode token header, which is:
1468 * <token version>-<checksum of payload>-<uncompressed payload length>
1469 * Note that the only supported token version is 1.
1471 nread = sscanf(token, "%u-%llx-%llx-",
1472 &version, &checksum, &packed_len);
1473 if (nread != 3) {
1474 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1475 "resume token is corrupt (invalid format)"));
1476 return (NULL);
1479 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1480 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1481 "resume token is corrupt (invalid version %u)"),
1482 version);
1483 return (NULL);
1486 /* Convert hexadecimal representation to binary. */
1487 token = strrchr(token, '-') + 1;
1488 int len = strlen(token) / 2;
1489 unsigned char *compressed = zfs_alloc(hdl, len);
1490 for (i = 0; i < len; i++) {
1491 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1492 if (nread != 1) {
1493 free(compressed);
1494 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1495 "resume token is corrupt "
1496 "(payload is not hex-encoded)"));
1497 return (NULL);
1501 /* Verify checksum. */
1502 zio_cksum_t cksum;
1503 fletcher_4_native_varsize(compressed, len, &cksum);
1504 if (cksum.zc_word[0] != checksum) {
1505 free(compressed);
1506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1507 "resume token is corrupt (incorrect checksum)"));
1508 return (NULL);
1511 /* Uncompress. */
1512 void *packed = zfs_alloc(hdl, packed_len);
1513 uLongf packed_len_long = packed_len;
1514 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1515 packed_len_long != packed_len) {
1516 free(packed);
1517 free(compressed);
1518 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1519 "resume token is corrupt (decompression failed)"));
1520 return (NULL);
1523 /* Unpack nvlist. */
1524 nvlist_t *nv;
1525 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1526 free(packed);
1527 free(compressed);
1528 if (error != 0) {
1529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1530 "resume token is corrupt (nvlist_unpack failed)"));
1531 return (NULL);
1533 return (nv);
1536 static enum lzc_send_flags
1537 lzc_flags_from_sendflags(const sendflags_t *flags)
1539 enum lzc_send_flags lzc_flags = 0;
1541 if (flags->largeblock)
1542 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1543 if (flags->embed_data)
1544 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1545 if (flags->compress)
1546 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1547 if (flags->raw)
1548 lzc_flags |= LZC_SEND_FLAG_RAW;
1549 if (flags->saved)
1550 lzc_flags |= LZC_SEND_FLAG_SAVED;
1552 return (lzc_flags);
1555 static int
1556 estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
1557 uint64_t resumeobj, uint64_t resumeoff, uint64_t bytes,
1558 const char *redactbook, char *errbuf, uint64_t *sizep)
1560 uint64_t size;
1561 FILE *fout = flags->dryrun ? stdout : stderr;
1562 progress_arg_t pa = { 0 };
1563 int err = 0;
1564 pthread_t ptid;
1566 if (flags->progress || flags->progressastitle) {
1567 pa.pa_zhp = zhp;
1568 pa.pa_fd = fd;
1569 pa.pa_parsable = flags->parsable;
1570 pa.pa_estimate = B_TRUE;
1571 pa.pa_verbosity = flags->verbosity;
1573 err = pthread_create(&ptid, NULL,
1574 send_progress_thread, &pa);
1575 if (err != 0) {
1576 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
1577 return (zfs_error(zhp->zfs_hdl,
1578 EZFS_THREADCREATEFAILED, errbuf));
1582 err = lzc_send_space_resume_redacted(zhp->zfs_name, from,
1583 lzc_flags_from_sendflags(flags), resumeobj, resumeoff, bytes,
1584 redactbook, fd, &size);
1585 *sizep = size;
1587 if ((flags->progress || flags->progressastitle) &&
1588 send_progress_thread_exit(zhp->zfs_hdl, ptid))
1589 return (-1);
1591 if (!flags->progress && !flags->parsable)
1592 return (err);
1594 if (err != 0) {
1595 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
1596 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
1597 errbuf));
1599 send_print_verbose(fout, zhp->zfs_name, from, size,
1600 flags->parsable);
1602 if (flags->parsable) {
1603 (void) fprintf(fout, "size\t%llu\n", (longlong_t)size);
1604 } else {
1605 char buf[16];
1606 zfs_nicenum(size, buf, sizeof (buf));
1607 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1608 "total estimated size is %s\n"), buf);
1610 return (0);
1613 static boolean_t
1614 redact_snaps_contains(const uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
1616 for (int i = 0; i < num_snaps; i++) {
1617 if (snaps[i] == guid)
1618 return (B_TRUE);
1620 return (B_FALSE);
1623 static boolean_t
1624 redact_snaps_equal(const uint64_t *snaps1, uint64_t num_snaps1,
1625 const uint64_t *snaps2, uint64_t num_snaps2)
1627 if (num_snaps1 != num_snaps2)
1628 return (B_FALSE);
1629 for (int i = 0; i < num_snaps1; i++) {
1630 if (!redact_snaps_contains(snaps2, num_snaps2, snaps1[i]))
1631 return (B_FALSE);
1633 return (B_TRUE);
1636 static int
1637 get_bookmarks(const char *path, nvlist_t **bmarksp)
1639 nvlist_t *props = fnvlist_alloc();
1640 int error;
1642 fnvlist_add_boolean(props, "redact_complete");
1643 fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1644 error = lzc_get_bookmarks(path, props, bmarksp);
1645 fnvlist_free(props);
1646 return (error);
1649 static nvpair_t *
1650 find_redact_pair(nvlist_t *bmarks, const uint64_t *redact_snap_guids,
1651 int num_redact_snaps)
1653 nvpair_t *pair;
1655 for (pair = nvlist_next_nvpair(bmarks, NULL); pair;
1656 pair = nvlist_next_nvpair(bmarks, pair)) {
1658 nvlist_t *bmark = fnvpair_value_nvlist(pair);
1659 nvlist_t *vallist = fnvlist_lookup_nvlist(bmark,
1660 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1661 uint_t len = 0;
1662 uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist,
1663 ZPROP_VALUE, &len);
1664 if (redact_snaps_equal(redact_snap_guids,
1665 num_redact_snaps, bmarksnaps, len)) {
1666 break;
1669 return (pair);
1672 static boolean_t
1673 get_redact_complete(nvpair_t *pair)
1675 nvlist_t *bmark = fnvpair_value_nvlist(pair);
1676 nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete");
1677 boolean_t complete = fnvlist_lookup_boolean_value(vallist,
1678 ZPROP_VALUE);
1680 return (complete);
1684 * Check that the list of redaction snapshots in the bookmark matches the send
1685 * we're resuming, and return whether or not it's complete.
1687 * Note that the caller needs to free the contents of *bookname with free() if
1688 * this function returns successfully.
1690 static int
1691 find_redact_book(libzfs_handle_t *hdl, const char *path,
1692 const uint64_t *redact_snap_guids, int num_redact_snaps,
1693 char **bookname)
1695 char errbuf[ERRBUFLEN];
1696 nvlist_t *bmarks;
1698 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1699 "cannot resume send"));
1701 int error = get_bookmarks(path, &bmarks);
1702 if (error != 0) {
1703 if (error == ESRCH) {
1704 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1705 "nonexistent redaction bookmark provided"));
1706 } else if (error == ENOENT) {
1707 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1708 "dataset to be sent no longer exists"));
1709 } else {
1710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1711 "unknown error: %s"), strerror(error));
1713 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1715 nvpair_t *pair = find_redact_pair(bmarks, redact_snap_guids,
1716 num_redact_snaps);
1717 if (pair == NULL) {
1718 fnvlist_free(bmarks);
1719 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1720 "no appropriate redaction bookmark exists"));
1721 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1723 boolean_t complete = get_redact_complete(pair);
1724 if (!complete) {
1725 fnvlist_free(bmarks);
1726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1727 "incomplete redaction bookmark provided"));
1728 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1730 *bookname = strndup(nvpair_name(pair), ZFS_MAX_DATASET_NAME_LEN);
1731 ASSERT3P(*bookname, !=, NULL);
1732 fnvlist_free(bmarks);
1733 return (0);
1736 static enum lzc_send_flags
1737 lzc_flags_from_resume_nvl(nvlist_t *resume_nvl)
1739 enum lzc_send_flags lzc_flags = 0;
1741 if (nvlist_exists(resume_nvl, "largeblockok"))
1742 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1743 if (nvlist_exists(resume_nvl, "embedok"))
1744 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1745 if (nvlist_exists(resume_nvl, "compressok"))
1746 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1747 if (nvlist_exists(resume_nvl, "rawok"))
1748 lzc_flags |= LZC_SEND_FLAG_RAW;
1749 if (nvlist_exists(resume_nvl, "savedok"))
1750 lzc_flags |= LZC_SEND_FLAG_SAVED;
1752 return (lzc_flags);
1755 static int
1756 zfs_send_resume_impl_cb_impl(libzfs_handle_t *hdl, sendflags_t *flags,
1757 int outfd, nvlist_t *resume_nvl)
1759 char errbuf[ERRBUFLEN];
1760 char *toname;
1761 char *fromname = NULL;
1762 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1763 zfs_handle_t *zhp;
1764 int error = 0;
1765 char name[ZFS_MAX_DATASET_NAME_LEN];
1766 FILE *fout = (flags->verbosity > 0 && flags->dryrun) ? stdout : stderr;
1767 uint64_t *redact_snap_guids = NULL;
1768 int num_redact_snaps = 0;
1769 char *redact_book = NULL;
1770 uint64_t size = 0;
1772 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1773 "cannot resume send"));
1775 if (flags->verbosity != 0) {
1776 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1777 "resume token contents:\n"));
1778 nvlist_print(fout, resume_nvl);
1781 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1782 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1783 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1784 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1785 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1786 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1787 "resume token is corrupt"));
1788 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1790 fromguid = 0;
1791 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1793 if (flags->saved) {
1794 (void) strlcpy(name, toname, sizeof (name));
1795 } else {
1796 error = guid_to_name(hdl, toname, toguid, B_FALSE, name);
1797 if (error != 0) {
1798 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1800 "'%s' is no longer the same snapshot "
1801 "used in the initial send"), toname);
1802 } else {
1803 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1804 "'%s' used in the initial send no "
1805 "longer exists"), toname);
1807 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1811 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1812 if (zhp == NULL) {
1813 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1814 "unable to access '%s'"), name);
1815 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1818 if (nvlist_lookup_uint64_array(resume_nvl, "book_redact_snaps",
1819 &redact_snap_guids, (uint_t *)&num_redact_snaps) != 0) {
1820 num_redact_snaps = -1;
1823 if (fromguid != 0) {
1824 if (guid_to_name_redact_snaps(hdl, toname, fromguid, B_TRUE,
1825 redact_snap_guids, num_redact_snaps, name) != 0) {
1826 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1827 "incremental source %#llx no longer exists"),
1828 (longlong_t)fromguid);
1829 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1831 fromname = name;
1834 redact_snap_guids = NULL;
1836 if (nvlist_lookup_uint64_array(resume_nvl,
1837 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &redact_snap_guids,
1838 (uint_t *)&num_redact_snaps) == 0) {
1839 char path[ZFS_MAX_DATASET_NAME_LEN];
1841 (void) strlcpy(path, toname, sizeof (path));
1842 char *at = strchr(path, '@');
1843 ASSERT3P(at, !=, NULL);
1845 *at = '\0';
1847 if ((error = find_redact_book(hdl, path, redact_snap_guids,
1848 num_redact_snaps, &redact_book)) != 0) {
1849 return (error);
1853 enum lzc_send_flags lzc_flags = lzc_flags_from_sendflags(flags) |
1854 lzc_flags_from_resume_nvl(resume_nvl);
1856 if (flags->verbosity != 0 || flags->progressastitle) {
1858 * Some of these may have come from the resume token, set them
1859 * here for size estimate purposes.
1861 sendflags_t tmpflags = *flags;
1862 if (lzc_flags & LZC_SEND_FLAG_LARGE_BLOCK)
1863 tmpflags.largeblock = B_TRUE;
1864 if (lzc_flags & LZC_SEND_FLAG_COMPRESS)
1865 tmpflags.compress = B_TRUE;
1866 if (lzc_flags & LZC_SEND_FLAG_EMBED_DATA)
1867 tmpflags.embed_data = B_TRUE;
1868 if (lzc_flags & LZC_SEND_FLAG_RAW)
1869 tmpflags.raw = B_TRUE;
1870 if (lzc_flags & LZC_SEND_FLAG_SAVED)
1871 tmpflags.saved = B_TRUE;
1872 error = estimate_size(zhp, fromname, outfd, &tmpflags,
1873 resumeobj, resumeoff, bytes, redact_book, errbuf, &size);
1876 if (!flags->dryrun) {
1877 progress_arg_t pa = { 0 };
1878 pthread_t tid;
1880 * If progress reporting is requested, spawn a new thread to
1881 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1883 if (flags->progress || flags->progressastitle) {
1884 pa.pa_zhp = zhp;
1885 pa.pa_fd = outfd;
1886 pa.pa_parsable = flags->parsable;
1887 pa.pa_estimate = B_FALSE;
1888 pa.pa_verbosity = flags->verbosity;
1889 pa.pa_size = size;
1890 pa.pa_astitle = flags->progressastitle;
1891 pa.pa_progress = flags->progress;
1893 error = pthread_create(&tid, NULL,
1894 send_progress_thread, &pa);
1895 if (error != 0) {
1896 if (redact_book != NULL)
1897 free(redact_book);
1898 zfs_close(zhp);
1899 return (error);
1903 error = lzc_send_resume_redacted(zhp->zfs_name, fromname, outfd,
1904 lzc_flags, resumeobj, resumeoff, redact_book);
1905 if (redact_book != NULL)
1906 free(redact_book);
1908 if ((flags->progressastitle || flags->progress) &&
1909 send_progress_thread_exit(hdl, tid)) {
1910 zfs_close(zhp);
1911 return (-1);
1914 char errbuf[ERRBUFLEN];
1915 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1916 "warning: cannot send '%s'"), zhp->zfs_name);
1918 zfs_close(zhp);
1920 switch (error) {
1921 case 0:
1922 return (0);
1923 case EACCES:
1924 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1925 "source key must be loaded"));
1926 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1927 case ESRCH:
1928 if (lzc_exists(zhp->zfs_name)) {
1929 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1930 "incremental source could not be found"));
1932 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1934 case EXDEV:
1935 case ENOENT:
1936 case EDQUOT:
1937 case EFBIG:
1938 case EIO:
1939 case ENOLINK:
1940 case ENOSPC:
1941 case ENOSTR:
1942 case ENXIO:
1943 case EPIPE:
1944 case ERANGE:
1945 case EFAULT:
1946 case EROFS:
1947 zfs_error_aux(hdl, "%s", strerror(errno));
1948 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1950 default:
1951 return (zfs_standard_error(hdl, errno, errbuf));
1953 } else {
1954 if (redact_book != NULL)
1955 free(redact_book);
1958 zfs_close(zhp);
1960 return (error);
1963 struct zfs_send_resume_impl {
1964 libzfs_handle_t *hdl;
1965 sendflags_t *flags;
1966 nvlist_t *resume_nvl;
1969 static int
1970 zfs_send_resume_impl_cb(int outfd, void *arg)
1972 struct zfs_send_resume_impl *zsri = arg;
1973 return (zfs_send_resume_impl_cb_impl(zsri->hdl, zsri->flags, outfd,
1974 zsri->resume_nvl));
1977 static int
1978 zfs_send_resume_impl(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1979 nvlist_t *resume_nvl)
1981 struct zfs_send_resume_impl zsri = {
1982 .hdl = hdl,
1983 .flags = flags,
1984 .resume_nvl = resume_nvl,
1986 return (lzc_send_wrapper(zfs_send_resume_impl_cb, outfd, &zsri));
1990 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1991 const char *resume_token)
1993 int ret;
1994 char errbuf[ERRBUFLEN];
1995 nvlist_t *resume_nvl;
1997 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1998 "cannot resume send"));
2000 resume_nvl = zfs_send_resume_token_to_nvlist(hdl, resume_token);
2001 if (resume_nvl == NULL) {
2003 * zfs_error_aux has already been set by
2004 * zfs_send_resume_token_to_nvlist()
2006 return (zfs_error(hdl, EZFS_FAULT, errbuf));
2009 ret = zfs_send_resume_impl(hdl, flags, outfd, resume_nvl);
2010 fnvlist_free(resume_nvl);
2012 return (ret);
2016 zfs_send_saved(zfs_handle_t *zhp, sendflags_t *flags, int outfd,
2017 const char *resume_token)
2019 int ret;
2020 libzfs_handle_t *hdl = zhp->zfs_hdl;
2021 nvlist_t *saved_nvl = NULL, *resume_nvl = NULL;
2022 uint64_t saved_guid = 0, resume_guid = 0;
2023 uint64_t obj = 0, off = 0, bytes = 0;
2024 char token_buf[ZFS_MAXPROPLEN];
2025 char errbuf[ERRBUFLEN];
2027 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2028 "saved send failed"));
2030 ret = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
2031 token_buf, sizeof (token_buf), NULL, NULL, 0, B_TRUE);
2032 if (ret != 0)
2033 goto out;
2035 saved_nvl = zfs_send_resume_token_to_nvlist(hdl, token_buf);
2036 if (saved_nvl == NULL) {
2038 * zfs_error_aux has already been set by
2039 * zfs_send_resume_token_to_nvlist()
2041 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2042 goto out;
2046 * If a resume token is provided we use the object and offset
2047 * from that instead of the default, which starts from the
2048 * beginning.
2050 if (resume_token != NULL) {
2051 resume_nvl = zfs_send_resume_token_to_nvlist(hdl,
2052 resume_token);
2053 if (resume_nvl == NULL) {
2054 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2055 goto out;
2058 if (nvlist_lookup_uint64(resume_nvl, "object", &obj) != 0 ||
2059 nvlist_lookup_uint64(resume_nvl, "offset", &off) != 0 ||
2060 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
2061 nvlist_lookup_uint64(resume_nvl, "toguid",
2062 &resume_guid) != 0) {
2063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2064 "provided resume token is corrupt"));
2065 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2066 goto out;
2069 if (nvlist_lookup_uint64(saved_nvl, "toguid",
2070 &saved_guid)) {
2071 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2072 "dataset's resume token is corrupt"));
2073 ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2074 goto out;
2077 if (resume_guid != saved_guid) {
2078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2079 "provided resume token does not match dataset"));
2080 ret = zfs_error(hdl, EZFS_BADBACKUP, errbuf);
2081 goto out;
2085 (void) nvlist_remove_all(saved_nvl, "object");
2086 fnvlist_add_uint64(saved_nvl, "object", obj);
2088 (void) nvlist_remove_all(saved_nvl, "offset");
2089 fnvlist_add_uint64(saved_nvl, "offset", off);
2091 (void) nvlist_remove_all(saved_nvl, "bytes");
2092 fnvlist_add_uint64(saved_nvl, "bytes", bytes);
2094 (void) nvlist_remove_all(saved_nvl, "toname");
2095 fnvlist_add_string(saved_nvl, "toname", zhp->zfs_name);
2097 ret = zfs_send_resume_impl(hdl, flags, outfd, saved_nvl);
2099 out:
2100 fnvlist_free(saved_nvl);
2101 fnvlist_free(resume_nvl);
2102 return (ret);
2106 * This function informs the target system that the recursive send is complete.
2107 * The record is also expected in the case of a send -p.
2109 static int
2110 send_conclusion_record(int fd, zio_cksum_t *zc)
2112 dmu_replay_record_t drr = { 0 };
2113 drr.drr_type = DRR_END;
2114 if (zc != NULL)
2115 drr.drr_u.drr_end.drr_checksum = *zc;
2116 if (write(fd, &drr, sizeof (drr)) == -1) {
2117 return (errno);
2119 return (0);
2123 * This function is responsible for sending the records that contain the
2124 * necessary information for the target system's libzfs to be able to set the
2125 * properties of the filesystem being received, or to be able to prepare for
2126 * a recursive receive.
2128 * The "zhp" argument is the handle of the snapshot we are sending
2129 * (the "tosnap"). The "from" argument is the short snapshot name (the part
2130 * after the @) of the incremental source.
2132 static int
2133 send_prelim_records(zfs_handle_t *zhp, const char *from, int fd,
2134 boolean_t gather_props, boolean_t recursive, boolean_t verbose,
2135 boolean_t dryrun, boolean_t raw, boolean_t replicate, boolean_t skipmissing,
2136 boolean_t backup, boolean_t holds, boolean_t props, boolean_t doall,
2137 nvlist_t **fssp, avl_tree_t **fsavlp)
2139 int err = 0;
2140 char *packbuf = NULL;
2141 size_t buflen = 0;
2142 zio_cksum_t zc = { {0} };
2143 int featureflags = 0;
2144 /* name of filesystem/volume that contains snapshot we are sending */
2145 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2146 /* short name of snap we are sending */
2147 const char *tosnap = "";
2149 char errbuf[ERRBUFLEN];
2150 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2151 "warning: cannot send '%s'"), zhp->zfs_name);
2152 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && zfs_prop_get_int(zhp,
2153 ZFS_PROP_VERSION) >= ZPL_VERSION_SA) {
2154 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
2157 if (holds)
2158 featureflags |= DMU_BACKUP_FEATURE_HOLDS;
2160 (void) strlcpy(tofs, zhp->zfs_name, ZFS_MAX_DATASET_NAME_LEN);
2161 char *at = strchr(tofs, '@');
2162 if (at != NULL) {
2163 *at = '\0';
2164 tosnap = at + 1;
2167 if (gather_props) {
2168 nvlist_t *hdrnv = fnvlist_alloc();
2169 nvlist_t *fss = NULL;
2171 if (from != NULL)
2172 fnvlist_add_string(hdrnv, "fromsnap", from);
2173 fnvlist_add_string(hdrnv, "tosnap", tosnap);
2174 if (!recursive)
2175 fnvlist_add_boolean(hdrnv, "not_recursive");
2177 if (raw) {
2178 fnvlist_add_boolean(hdrnv, "raw");
2181 if (gather_nvlist(zhp->zfs_hdl, tofs,
2182 from, tosnap, recursive, raw, doall, replicate, skipmissing,
2183 verbose, backup, holds, props, &fss, fsavlp) != 0) {
2184 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2185 errbuf));
2188 * Do not allow the size of the properties list to exceed
2189 * the limit
2191 if ((fnvlist_size(fss) + fnvlist_size(hdrnv)) >
2192 zhp->zfs_hdl->libzfs_max_nvlist) {
2193 (void) snprintf(errbuf, sizeof (errbuf),
2194 dgettext(TEXT_DOMAIN, "warning: cannot send '%s': "
2195 "the size of the list of snapshots and properties "
2196 "is too large to be received successfully.\n"
2197 "Select a smaller number of snapshots to send.\n"),
2198 zhp->zfs_name);
2199 return (zfs_error(zhp->zfs_hdl, EZFS_NOSPC,
2200 errbuf));
2202 fnvlist_add_nvlist(hdrnv, "fss", fss);
2203 VERIFY0(nvlist_pack(hdrnv, &packbuf, &buflen, NV_ENCODE_XDR,
2204 0));
2205 if (fssp != NULL) {
2206 *fssp = fss;
2207 } else {
2208 fnvlist_free(fss);
2210 fnvlist_free(hdrnv);
2213 if (!dryrun) {
2214 dmu_replay_record_t drr = { 0 };
2215 /* write first begin record */
2216 drr.drr_type = DRR_BEGIN;
2217 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
2218 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
2219 drr_versioninfo, DMU_COMPOUNDSTREAM);
2220 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
2221 drr_versioninfo, featureflags);
2222 if (snprintf(drr.drr_u.drr_begin.drr_toname,
2223 sizeof (drr.drr_u.drr_begin.drr_toname), "%s@%s", tofs,
2224 tosnap) >= sizeof (drr.drr_u.drr_begin.drr_toname)) {
2225 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2226 errbuf));
2228 drr.drr_payloadlen = buflen;
2230 err = dump_record(&drr, packbuf, buflen, &zc, fd);
2231 free(packbuf);
2232 if (err != 0) {
2233 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
2234 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2235 errbuf));
2237 err = send_conclusion_record(fd, &zc);
2238 if (err != 0) {
2239 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(err));
2240 return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2241 errbuf));
2244 return (0);
2248 * Generate a send stream. The "zhp" argument is the filesystem/volume
2249 * that contains the snapshot to send. The "fromsnap" argument is the
2250 * short name (the part after the '@') of the snapshot that is the
2251 * incremental source to send from (if non-NULL). The "tosnap" argument
2252 * is the short name of the snapshot to send.
2254 * The content of the send stream is the snapshot identified by
2255 * 'tosnap'. Incremental streams are requested in two ways:
2256 * - from the snapshot identified by "fromsnap" (if non-null) or
2257 * - from the origin of the dataset identified by zhp, which must
2258 * be a clone. In this case, "fromsnap" is null and "fromorigin"
2259 * is TRUE.
2261 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
2262 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
2263 * if "replicate" is set. If "doall" is set, dump all the intermediate
2264 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
2265 * case too. If "props" is set, send properties.
2267 * Pre-wrapped (cf. lzc_send_wrapper()).
2269 static int
2270 zfs_send_cb_impl(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2271 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2272 void *cb_arg, nvlist_t **debugnvp)
2274 char errbuf[ERRBUFLEN];
2275 send_dump_data_t sdd = { 0 };
2276 int err = 0;
2277 nvlist_t *fss = NULL;
2278 avl_tree_t *fsavl = NULL;
2279 static uint64_t holdseq;
2280 int spa_version;
2281 FILE *fout;
2283 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2284 "cannot send '%s'"), zhp->zfs_name);
2286 if (fromsnap && fromsnap[0] == '\0') {
2287 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2288 "zero-length incremental source"));
2289 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
2292 if (fromsnap) {
2293 char full_fromsnap_name[ZFS_MAX_DATASET_NAME_LEN];
2294 if (snprintf(full_fromsnap_name, sizeof (full_fromsnap_name),
2295 "%s@%s", zhp->zfs_name, fromsnap) >=
2296 sizeof (full_fromsnap_name)) {
2297 err = EINVAL;
2298 goto stderr_out;
2300 zfs_handle_t *fromsnapn = zfs_open(zhp->zfs_hdl,
2301 full_fromsnap_name, ZFS_TYPE_SNAPSHOT);
2302 if (fromsnapn == NULL) {
2303 err = -1;
2304 goto err_out;
2306 zfs_close(fromsnapn);
2309 if (flags->replicate || flags->doall || flags->props ||
2310 flags->holds || flags->backup) {
2311 char full_tosnap_name[ZFS_MAX_DATASET_NAME_LEN];
2312 if (snprintf(full_tosnap_name, sizeof (full_tosnap_name),
2313 "%s@%s", zhp->zfs_name, tosnap) >=
2314 sizeof (full_tosnap_name)) {
2315 err = EINVAL;
2316 goto stderr_out;
2318 zfs_handle_t *tosnap = zfs_open(zhp->zfs_hdl,
2319 full_tosnap_name, ZFS_TYPE_SNAPSHOT);
2320 if (tosnap == NULL) {
2321 err = -1;
2322 goto err_out;
2324 err = send_prelim_records(tosnap, fromsnap, outfd,
2325 flags->replicate || flags->props || flags->holds,
2326 flags->replicate, flags->verbosity > 0, flags->dryrun,
2327 flags->raw, flags->replicate, flags->skipmissing,
2328 flags->backup, flags->holds, flags->props, flags->doall,
2329 &fss, &fsavl);
2330 zfs_close(tosnap);
2331 if (err != 0)
2332 goto err_out;
2335 /* dump each stream */
2336 sdd.fromsnap = fromsnap;
2337 sdd.tosnap = tosnap;
2338 sdd.outfd = outfd;
2339 sdd.replicate = flags->replicate;
2340 sdd.doall = flags->doall;
2341 sdd.fromorigin = flags->fromorigin;
2342 sdd.fss = fss;
2343 sdd.fsavl = fsavl;
2344 sdd.verbosity = flags->verbosity;
2345 sdd.parsable = flags->parsable;
2346 sdd.progress = flags->progress;
2347 sdd.progressastitle = flags->progressastitle;
2348 sdd.dryrun = flags->dryrun;
2349 sdd.large_block = flags->largeblock;
2350 sdd.embed_data = flags->embed_data;
2351 sdd.compress = flags->compress;
2352 sdd.raw = flags->raw;
2353 sdd.holds = flags->holds;
2354 sdd.filter_cb = filter_func;
2355 sdd.filter_cb_arg = cb_arg;
2356 if (debugnvp)
2357 sdd.debugnv = *debugnvp;
2358 if (sdd.verbosity != 0 && sdd.dryrun)
2359 sdd.std_out = B_TRUE;
2360 fout = sdd.std_out ? stdout : stderr;
2363 * Some flags require that we place user holds on the datasets that are
2364 * being sent so they don't get destroyed during the send. We can skip
2365 * this step if the pool is imported read-only since the datasets cannot
2366 * be destroyed.
2368 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2369 ZPOOL_PROP_READONLY, NULL) &&
2370 zfs_spa_version(zhp, &spa_version) == 0 &&
2371 spa_version >= SPA_VERSION_USERREFS &&
2372 (flags->doall || flags->replicate)) {
2373 ++holdseq;
2374 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2375 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2376 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
2377 if (sdd.cleanup_fd < 0) {
2378 err = errno;
2379 goto stderr_out;
2381 sdd.snapholds = fnvlist_alloc();
2382 } else {
2383 sdd.cleanup_fd = -1;
2384 sdd.snapholds = NULL;
2387 if (flags->verbosity != 0 || sdd.snapholds != NULL) {
2389 * Do a verbose no-op dry run to get all the verbose output
2390 * or to gather snapshot hold's before generating any data,
2391 * then do a non-verbose real run to generate the streams.
2393 sdd.dryrun = B_TRUE;
2394 err = dump_filesystems(zhp, &sdd);
2396 if (err != 0)
2397 goto stderr_out;
2399 if (flags->verbosity != 0) {
2400 if (flags->parsable) {
2401 (void) fprintf(fout, "size\t%llu\n",
2402 (longlong_t)sdd.size);
2403 } else {
2404 char buf[16];
2405 zfs_nicebytes(sdd.size, buf, sizeof (buf));
2406 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
2407 "total estimated size is %s\n"), buf);
2411 /* Ensure no snaps found is treated as an error. */
2412 if (!sdd.seento) {
2413 err = ENOENT;
2414 goto err_out;
2417 /* Skip the second run if dryrun was requested. */
2418 if (flags->dryrun)
2419 goto err_out;
2421 if (sdd.snapholds != NULL) {
2422 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2423 if (err != 0)
2424 goto stderr_out;
2426 fnvlist_free(sdd.snapholds);
2427 sdd.snapholds = NULL;
2430 sdd.dryrun = B_FALSE;
2431 sdd.verbosity = 0;
2434 err = dump_filesystems(zhp, &sdd);
2435 fsavl_destroy(fsavl);
2436 fnvlist_free(fss);
2438 /* Ensure no snaps found is treated as an error. */
2439 if (err == 0 && !sdd.seento)
2440 err = ENOENT;
2442 if (sdd.cleanup_fd != -1) {
2443 VERIFY(0 == close(sdd.cleanup_fd));
2444 sdd.cleanup_fd = -1;
2447 if (!flags->dryrun && (flags->replicate || flags->doall ||
2448 flags->props || flags->backup || flags->holds)) {
2450 * write final end record. NB: want to do this even if
2451 * there was some error, because it might not be totally
2452 * failed.
2454 int err2 = send_conclusion_record(outfd, NULL);
2455 if (err2 != 0)
2456 return (zfs_standard_error(zhp->zfs_hdl, err2, errbuf));
2459 return (err || sdd.err);
2461 stderr_out:
2462 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2463 err_out:
2464 fsavl_destroy(fsavl);
2465 fnvlist_free(fss);
2466 fnvlist_free(sdd.snapholds);
2468 if (sdd.cleanup_fd != -1)
2469 VERIFY(0 == close(sdd.cleanup_fd));
2470 return (err);
2473 struct zfs_send {
2474 zfs_handle_t *zhp;
2475 const char *fromsnap;
2476 const char *tosnap;
2477 sendflags_t *flags;
2478 snapfilter_cb_t *filter_func;
2479 void *cb_arg;
2480 nvlist_t **debugnvp;
2483 static int
2484 zfs_send_cb(int outfd, void *arg)
2486 struct zfs_send *zs = arg;
2487 return (zfs_send_cb_impl(zs->zhp, zs->fromsnap, zs->tosnap, zs->flags,
2488 outfd, zs->filter_func, zs->cb_arg, zs->debugnvp));
2492 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2493 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2494 void *cb_arg, nvlist_t **debugnvp)
2496 struct zfs_send arg = {
2497 .zhp = zhp,
2498 .fromsnap = fromsnap,
2499 .tosnap = tosnap,
2500 .flags = flags,
2501 .filter_func = filter_func,
2502 .cb_arg = cb_arg,
2503 .debugnvp = debugnvp,
2505 return (lzc_send_wrapper(zfs_send_cb, outfd, &arg));
2509 static zfs_handle_t *
2510 name_to_dir_handle(libzfs_handle_t *hdl, const char *snapname)
2512 char dirname[ZFS_MAX_DATASET_NAME_LEN];
2513 (void) strlcpy(dirname, snapname, ZFS_MAX_DATASET_NAME_LEN);
2514 char *c = strchr(dirname, '@');
2515 if (c != NULL)
2516 *c = '\0';
2517 return (zfs_open(hdl, dirname, ZFS_TYPE_DATASET));
2521 * Returns B_TRUE if earlier is an earlier snapshot in later's timeline; either
2522 * an earlier snapshot in the same filesystem, or a snapshot before later's
2523 * origin, or it's origin's origin, etc.
2525 static boolean_t
2526 snapshot_is_before(zfs_handle_t *earlier, zfs_handle_t *later)
2528 boolean_t ret;
2529 uint64_t later_txg =
2530 (later->zfs_type == ZFS_TYPE_FILESYSTEM ||
2531 later->zfs_type == ZFS_TYPE_VOLUME ?
2532 UINT64_MAX : zfs_prop_get_int(later, ZFS_PROP_CREATETXG));
2533 uint64_t earlier_txg = zfs_prop_get_int(earlier, ZFS_PROP_CREATETXG);
2535 if (earlier_txg >= later_txg)
2536 return (B_FALSE);
2538 zfs_handle_t *earlier_dir = name_to_dir_handle(earlier->zfs_hdl,
2539 earlier->zfs_name);
2540 zfs_handle_t *later_dir = name_to_dir_handle(later->zfs_hdl,
2541 later->zfs_name);
2543 if (strcmp(earlier_dir->zfs_name, later_dir->zfs_name) == 0) {
2544 zfs_close(earlier_dir);
2545 zfs_close(later_dir);
2546 return (B_TRUE);
2549 char clonename[ZFS_MAX_DATASET_NAME_LEN];
2550 if (zfs_prop_get(later_dir, ZFS_PROP_ORIGIN, clonename,
2551 ZFS_MAX_DATASET_NAME_LEN, NULL, NULL, 0, B_TRUE) != 0) {
2552 zfs_close(earlier_dir);
2553 zfs_close(later_dir);
2554 return (B_FALSE);
2557 zfs_handle_t *origin = zfs_open(earlier->zfs_hdl, clonename,
2558 ZFS_TYPE_DATASET);
2559 uint64_t origin_txg = zfs_prop_get_int(origin, ZFS_PROP_CREATETXG);
2562 * If "earlier" is exactly the origin, then
2563 * snapshot_is_before(earlier, origin) will return false (because
2564 * they're the same).
2566 if (origin_txg == earlier_txg &&
2567 strcmp(origin->zfs_name, earlier->zfs_name) == 0) {
2568 zfs_close(earlier_dir);
2569 zfs_close(later_dir);
2570 zfs_close(origin);
2571 return (B_TRUE);
2573 zfs_close(earlier_dir);
2574 zfs_close(later_dir);
2576 ret = snapshot_is_before(earlier, origin);
2577 zfs_close(origin);
2578 return (ret);
2582 * The "zhp" argument is the handle of the dataset to send (typically a
2583 * snapshot). The "from" argument is the full name of the snapshot or
2584 * bookmark that is the incremental source.
2586 * Pre-wrapped (cf. lzc_send_wrapper()).
2588 static int
2589 zfs_send_one_cb_impl(zfs_handle_t *zhp, const char *from, int fd,
2590 sendflags_t *flags, const char *redactbook)
2592 int err;
2593 libzfs_handle_t *hdl = zhp->zfs_hdl;
2594 char *name = zhp->zfs_name;
2595 pthread_t ptid;
2596 progress_arg_t pa = { 0 };
2597 uint64_t size = 0;
2599 char errbuf[ERRBUFLEN];
2600 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2601 "warning: cannot send '%s'"), name);
2603 if (from != NULL && strchr(from, '@')) {
2604 zfs_handle_t *from_zhp = zfs_open(hdl, from,
2605 ZFS_TYPE_DATASET);
2606 if (from_zhp == NULL)
2607 return (-1);
2608 if (!snapshot_is_before(from_zhp, zhp)) {
2609 zfs_close(from_zhp);
2610 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2611 "not an earlier snapshot from the same fs"));
2612 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2614 zfs_close(from_zhp);
2617 if (redactbook != NULL) {
2618 char bookname[ZFS_MAX_DATASET_NAME_LEN];
2619 nvlist_t *redact_snaps;
2620 zfs_handle_t *book_zhp;
2621 char *at, *pound;
2622 int dsnamelen;
2624 pound = strchr(redactbook, '#');
2625 if (pound != NULL)
2626 redactbook = pound + 1;
2627 at = strchr(name, '@');
2628 if (at == NULL) {
2629 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2630 "cannot do a redacted send to a filesystem"));
2631 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2633 dsnamelen = at - name;
2634 if (snprintf(bookname, sizeof (bookname), "%.*s#%s",
2635 dsnamelen, name, redactbook)
2636 >= sizeof (bookname)) {
2637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2638 "invalid bookmark name"));
2639 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2641 book_zhp = zfs_open(hdl, bookname, ZFS_TYPE_BOOKMARK);
2642 if (book_zhp == NULL)
2643 return (-1);
2644 if (nvlist_lookup_nvlist(book_zhp->zfs_props,
2645 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2646 &redact_snaps) != 0 || redact_snaps == NULL) {
2647 zfs_close(book_zhp);
2648 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2649 "not a redaction bookmark"));
2650 return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2652 zfs_close(book_zhp);
2656 * Send fs properties
2658 if (flags->props || flags->holds || flags->backup) {
2660 * Note: the header generated by send_prelim_records()
2661 * assumes that the incremental source is in the same
2662 * filesystem/volume as the target (which is a requirement
2663 * when doing "zfs send -R"). But that isn't always the
2664 * case here (e.g. send from snap in origin, or send from
2665 * bookmark). We pass from=NULL, which will omit this
2666 * information from the prelim records; it isn't used
2667 * when receiving this type of stream.
2669 err = send_prelim_records(zhp, NULL, fd, B_TRUE, B_FALSE,
2670 flags->verbosity > 0, flags->dryrun, flags->raw,
2671 flags->replicate, B_FALSE, flags->backup, flags->holds,
2672 flags->props, flags->doall, NULL, NULL);
2673 if (err != 0)
2674 return (err);
2678 * Perform size estimate if verbose was specified.
2680 if (flags->verbosity != 0 || flags->progressastitle) {
2681 err = estimate_size(zhp, from, fd, flags, 0, 0, 0, redactbook,
2682 errbuf, &size);
2683 if (err != 0)
2684 return (err);
2687 if (flags->dryrun)
2688 return (0);
2691 * If progress reporting is requested, spawn a new thread to poll
2692 * ZFS_IOC_SEND_PROGRESS at a regular interval.
2694 if (flags->progress || flags->progressastitle) {
2695 pa.pa_zhp = zhp;
2696 pa.pa_fd = fd;
2697 pa.pa_parsable = flags->parsable;
2698 pa.pa_estimate = B_FALSE;
2699 pa.pa_verbosity = flags->verbosity;
2700 pa.pa_size = size;
2701 pa.pa_astitle = flags->progressastitle;
2702 pa.pa_progress = flags->progress;
2704 err = pthread_create(&ptid, NULL,
2705 send_progress_thread, &pa);
2706 if (err != 0) {
2707 zfs_error_aux(zhp->zfs_hdl, "%s", strerror(errno));
2708 return (zfs_error(zhp->zfs_hdl,
2709 EZFS_THREADCREATEFAILED, errbuf));
2713 err = lzc_send_redacted(name, from, fd,
2714 lzc_flags_from_sendflags(flags), redactbook);
2716 if ((flags->progress || flags->progressastitle) &&
2717 send_progress_thread_exit(hdl, ptid))
2718 return (-1);
2720 if (err == 0 && (flags->props || flags->holds || flags->backup)) {
2721 /* Write the final end record. */
2722 err = send_conclusion_record(fd, NULL);
2723 if (err != 0)
2724 return (zfs_standard_error(hdl, err, errbuf));
2726 if (err != 0) {
2727 switch (errno) {
2728 case EXDEV:
2729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2730 "not an earlier snapshot from the same fs"));
2731 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2733 case ENOENT:
2734 case ESRCH:
2735 if (lzc_exists(name)) {
2736 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2737 "incremental source (%s) does not exist"),
2738 from);
2740 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2742 case EACCES:
2743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2744 "dataset key must be loaded"));
2745 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2747 case EBUSY:
2748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2749 "target is busy; if a filesystem, "
2750 "it must not be mounted"));
2751 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2753 case EDQUOT:
2754 case EFAULT:
2755 case EFBIG:
2756 case EINVAL:
2757 case EIO:
2758 case ENOLINK:
2759 case ENOSPC:
2760 case ENOSTR:
2761 case ENXIO:
2762 case EPIPE:
2763 case ERANGE:
2764 case EROFS:
2765 zfs_error_aux(hdl, "%s", strerror(errno));
2766 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2768 default:
2769 return (zfs_standard_error(hdl, errno, errbuf));
2772 return (err != 0);
2775 struct zfs_send_one {
2776 zfs_handle_t *zhp;
2777 const char *from;
2778 sendflags_t *flags;
2779 const char *redactbook;
2782 static int
2783 zfs_send_one_cb(int fd, void *arg)
2785 struct zfs_send_one *zso = arg;
2786 return (zfs_send_one_cb_impl(zso->zhp, zso->from, fd, zso->flags,
2787 zso->redactbook));
2791 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
2792 const char *redactbook)
2794 struct zfs_send_one zso = {
2795 .zhp = zhp,
2796 .from = from,
2797 .flags = flags,
2798 .redactbook = redactbook,
2800 return (lzc_send_wrapper(zfs_send_one_cb, fd, &zso));
2804 * Routines specific to "zfs recv"
2807 static int
2808 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2809 boolean_t byteswap, zio_cksum_t *zc)
2811 char *cp = buf;
2812 int rv;
2813 int len = ilen;
2815 do {
2816 rv = read(fd, cp, len);
2817 cp += rv;
2818 len -= rv;
2819 } while (rv > 0);
2821 if (rv < 0 || len != 0) {
2822 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2823 "failed to read from stream"));
2824 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2825 "cannot receive")));
2828 if (zc) {
2829 if (byteswap)
2830 fletcher_4_incremental_byteswap(buf, ilen, zc);
2831 else
2832 fletcher_4_incremental_native(buf, ilen, zc);
2834 return (0);
2837 static int
2838 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2839 boolean_t byteswap, zio_cksum_t *zc)
2841 char *buf;
2842 int err;
2844 buf = zfs_alloc(hdl, len);
2846 if (len > hdl->libzfs_max_nvlist) {
2847 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "nvlist too large"));
2848 free(buf);
2849 return (ENOMEM);
2852 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2853 if (err != 0) {
2854 free(buf);
2855 return (err);
2858 err = nvlist_unpack(buf, len, nvp, 0);
2859 free(buf);
2860 if (err != 0) {
2861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2862 "stream (malformed nvlist)"));
2863 return (EINVAL);
2865 return (0);
2869 * Returns the grand origin (origin of origin of origin...) of a given handle.
2870 * If this dataset is not a clone, it simply returns a copy of the original
2871 * handle.
2873 static zfs_handle_t *
2874 recv_open_grand_origin(zfs_handle_t *zhp)
2876 char origin[ZFS_MAX_DATASET_NAME_LEN];
2877 zprop_source_t src;
2878 zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2880 while (ozhp != NULL) {
2881 if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2882 sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2883 break;
2885 (void) zfs_close(ozhp);
2886 ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2889 return (ozhp);
2892 static int
2893 recv_rename_impl(zfs_handle_t *zhp, const char *name, const char *newname)
2895 int err;
2896 zfs_handle_t *ozhp = NULL;
2899 * Attempt to rename the dataset. If it fails with EACCES we have
2900 * attempted to rename the dataset outside of its encryption root.
2901 * Force the dataset to become an encryption root and try again.
2903 err = lzc_rename(name, newname);
2904 if (err == EACCES) {
2905 ozhp = recv_open_grand_origin(zhp);
2906 if (ozhp == NULL) {
2907 err = ENOENT;
2908 goto out;
2911 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2912 NULL, NULL, 0);
2913 if (err != 0)
2914 goto out;
2916 err = lzc_rename(name, newname);
2919 out:
2920 if (ozhp != NULL)
2921 zfs_close(ozhp);
2922 return (err);
2925 static int
2926 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2927 int baselen, char *newname, recvflags_t *flags)
2929 static int seq;
2930 int err;
2931 prop_changelist_t *clp = NULL;
2932 zfs_handle_t *zhp = NULL;
2934 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2935 if (zhp == NULL) {
2936 err = -1;
2937 goto out;
2939 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2940 flags->force ? MS_FORCE : 0);
2941 if (clp == NULL) {
2942 err = -1;
2943 goto out;
2945 err = changelist_prefix(clp);
2946 if (err)
2947 goto out;
2949 if (tryname) {
2950 (void) strlcpy(newname, tryname, ZFS_MAX_DATASET_NAME_LEN);
2951 if (flags->verbose) {
2952 (void) printf("attempting rename %s to %s\n",
2953 name, newname);
2955 err = recv_rename_impl(zhp, name, newname);
2956 if (err == 0)
2957 changelist_rename(clp, name, tryname);
2958 } else {
2959 err = ENOENT;
2962 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2963 seq++;
2965 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2966 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2968 if (flags->verbose) {
2969 (void) printf("failed - trying rename %s to %s\n",
2970 name, newname);
2972 err = recv_rename_impl(zhp, name, newname);
2973 if (err == 0)
2974 changelist_rename(clp, name, newname);
2975 if (err && flags->verbose) {
2976 (void) printf("failed (%u) - "
2977 "will try again on next pass\n", errno);
2979 err = EAGAIN;
2980 } else if (flags->verbose) {
2981 if (err == 0)
2982 (void) printf("success\n");
2983 else
2984 (void) printf("failed (%u)\n", errno);
2987 (void) changelist_postfix(clp);
2989 out:
2990 if (clp != NULL)
2991 changelist_free(clp);
2992 if (zhp != NULL)
2993 zfs_close(zhp);
2995 return (err);
2998 static int
2999 recv_promote(libzfs_handle_t *hdl, const char *fsname,
3000 const char *origin_fsname, recvflags_t *flags)
3002 int err;
3003 zfs_cmd_t zc = {"\0"};
3004 zfs_handle_t *zhp = NULL, *ozhp = NULL;
3006 if (flags->verbose)
3007 (void) printf("promoting %s\n", fsname);
3009 (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
3010 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
3013 * Attempt to promote the dataset. If it fails with EACCES the
3014 * promotion would cause this dataset to leave its encryption root.
3015 * Force the origin to become an encryption root and try again.
3017 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3018 if (err == EACCES) {
3019 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3020 if (zhp == NULL) {
3021 err = -1;
3022 goto out;
3025 ozhp = recv_open_grand_origin(zhp);
3026 if (ozhp == NULL) {
3027 err = -1;
3028 goto out;
3031 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
3032 NULL, NULL, 0);
3033 if (err != 0)
3034 goto out;
3036 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3039 out:
3040 if (zhp != NULL)
3041 zfs_close(zhp);
3042 if (ozhp != NULL)
3043 zfs_close(ozhp);
3045 return (err);
3048 static int
3049 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
3050 char *newname, recvflags_t *flags)
3052 int err = 0;
3053 prop_changelist_t *clp;
3054 zfs_handle_t *zhp;
3055 boolean_t defer = B_FALSE;
3056 int spa_version;
3058 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
3059 if (zhp == NULL)
3060 return (-1);
3061 zfs_type_t type = zfs_get_type(zhp);
3062 if (type == ZFS_TYPE_SNAPSHOT &&
3063 zfs_spa_version(zhp, &spa_version) == 0 &&
3064 spa_version >= SPA_VERSION_USERREFS)
3065 defer = B_TRUE;
3066 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3067 flags->force ? MS_FORCE : 0);
3068 zfs_close(zhp);
3069 if (clp == NULL)
3070 return (-1);
3072 err = changelist_prefix(clp);
3073 if (err)
3074 return (err);
3076 if (flags->verbose)
3077 (void) printf("attempting destroy %s\n", name);
3078 if (type == ZFS_TYPE_SNAPSHOT) {
3079 nvlist_t *nv = fnvlist_alloc();
3080 fnvlist_add_boolean(nv, name);
3081 err = lzc_destroy_snaps(nv, defer, NULL);
3082 fnvlist_free(nv);
3083 } else {
3084 err = lzc_destroy(name);
3086 if (err == 0) {
3087 if (flags->verbose)
3088 (void) printf("success\n");
3089 changelist_remove(clp, name);
3092 (void) changelist_postfix(clp);
3093 changelist_free(clp);
3096 * Deferred destroy might destroy the snapshot or only mark it to be
3097 * destroyed later, and it returns success in either case.
3099 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
3100 ZFS_TYPE_SNAPSHOT))) {
3101 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
3104 return (err);
3107 typedef struct guid_to_name_data {
3108 uint64_t guid;
3109 boolean_t bookmark_ok;
3110 char *name;
3111 char *skip;
3112 uint64_t *redact_snap_guids;
3113 uint64_t num_redact_snaps;
3114 } guid_to_name_data_t;
3116 static boolean_t
3117 redact_snaps_match(zfs_handle_t *zhp, guid_to_name_data_t *gtnd)
3119 uint64_t *bmark_snaps;
3120 uint_t bmark_num_snaps;
3121 nvlist_t *nvl;
3122 if (zhp->zfs_type != ZFS_TYPE_BOOKMARK)
3123 return (B_FALSE);
3125 nvl = fnvlist_lookup_nvlist(zhp->zfs_props,
3126 zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
3127 bmark_snaps = fnvlist_lookup_uint64_array(nvl, ZPROP_VALUE,
3128 &bmark_num_snaps);
3129 if (bmark_num_snaps != gtnd->num_redact_snaps)
3130 return (B_FALSE);
3131 int i = 0;
3132 for (; i < bmark_num_snaps; i++) {
3133 int j = 0;
3134 for (; j < bmark_num_snaps; j++) {
3135 if (bmark_snaps[i] == gtnd->redact_snap_guids[j])
3136 break;
3138 if (j == bmark_num_snaps)
3139 break;
3141 return (i == bmark_num_snaps);
3144 static int
3145 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
3147 guid_to_name_data_t *gtnd = arg;
3148 const char *slash;
3149 int err;
3151 if (gtnd->skip != NULL &&
3152 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
3153 strcmp(slash + 1, gtnd->skip) == 0) {
3154 zfs_close(zhp);
3155 return (0);
3158 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid &&
3159 (gtnd->num_redact_snaps == -1 || redact_snaps_match(zhp, gtnd))) {
3160 (void) strcpy(gtnd->name, zhp->zfs_name);
3161 zfs_close(zhp);
3162 return (EEXIST);
3165 err = zfs_iter_children(zhp, 0, guid_to_name_cb, gtnd);
3166 if (err != EEXIST && gtnd->bookmark_ok)
3167 err = zfs_iter_bookmarks(zhp, 0, guid_to_name_cb, gtnd);
3168 zfs_close(zhp);
3169 return (err);
3173 * Attempt to find the local dataset associated with this guid. In the case of
3174 * multiple matches, we attempt to find the "best" match by searching
3175 * progressively larger portions of the hierarchy. This allows one to send a
3176 * tree of datasets individually and guarantee that we will find the source
3177 * guid within that hierarchy, even if there are multiple matches elsewhere.
3179 * If num_redact_snaps is not -1, we attempt to find a redaction bookmark with
3180 * the specified number of redaction snapshots. If num_redact_snaps isn't 0 or
3181 * -1, then redact_snap_guids will be an array of the guids of the snapshots the
3182 * redaction bookmark was created with. If num_redact_snaps is -1, then we will
3183 * attempt to find a snapshot or bookmark (if bookmark_ok is passed) with the
3184 * given guid. Note that a redaction bookmark can be returned if
3185 * num_redact_snaps == -1.
3187 static int
3188 guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
3189 uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
3190 uint64_t num_redact_snaps, char *name)
3192 char pname[ZFS_MAX_DATASET_NAME_LEN];
3193 guid_to_name_data_t gtnd;
3195 gtnd.guid = guid;
3196 gtnd.bookmark_ok = bookmark_ok;
3197 gtnd.name = name;
3198 gtnd.skip = NULL;
3199 gtnd.redact_snap_guids = redact_snap_guids;
3200 gtnd.num_redact_snaps = num_redact_snaps;
3203 * Search progressively larger portions of the hierarchy, starting
3204 * with the filesystem specified by 'parent'. This will
3205 * select the "most local" version of the origin snapshot in the case
3206 * that there are multiple matching snapshots in the system.
3208 (void) strlcpy(pname, parent, sizeof (pname));
3209 char *cp = strrchr(pname, '@');
3210 if (cp == NULL)
3211 cp = strchr(pname, '\0');
3212 for (; cp != NULL; cp = strrchr(pname, '/')) {
3213 /* Chop off the last component and open the parent */
3214 *cp = '\0';
3215 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
3217 if (zhp == NULL)
3218 continue;
3219 int err = guid_to_name_cb(zfs_handle_dup(zhp), &gtnd);
3220 if (err != EEXIST)
3221 err = zfs_iter_children(zhp, 0, guid_to_name_cb, &gtnd);
3222 if (err != EEXIST && bookmark_ok)
3223 err = zfs_iter_bookmarks(zhp, 0, guid_to_name_cb,
3224 &gtnd);
3225 zfs_close(zhp);
3226 if (err == EEXIST)
3227 return (0);
3230 * Remember the last portion of the dataset so we skip it next
3231 * time through (as we've already searched that portion of the
3232 * hierarchy).
3234 gtnd.skip = strrchr(pname, '/') + 1;
3237 return (ENOENT);
3240 static int
3241 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
3242 boolean_t bookmark_ok, char *name)
3244 return (guid_to_name_redact_snaps(hdl, parent, guid, bookmark_ok, NULL,
3245 -1, name));
3249 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
3250 * guid1 is after guid2.
3252 static int
3253 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
3254 uint64_t guid1, uint64_t guid2)
3256 nvlist_t *nvfs;
3257 char *fsname = NULL, *snapname = NULL;
3258 char buf[ZFS_MAX_DATASET_NAME_LEN];
3259 int rv;
3260 zfs_handle_t *guid1hdl, *guid2hdl;
3261 uint64_t create1, create2;
3263 if (guid2 == 0)
3264 return (0);
3265 if (guid1 == 0)
3266 return (1);
3268 nvfs = fsavl_find(avl, guid1, &snapname);
3269 fsname = fnvlist_lookup_string(nvfs, "name");
3270 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3271 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3272 if (guid1hdl == NULL)
3273 return (-1);
3275 nvfs = fsavl_find(avl, guid2, &snapname);
3276 fsname = fnvlist_lookup_string(nvfs, "name");
3277 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3278 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3279 if (guid2hdl == NULL) {
3280 zfs_close(guid1hdl);
3281 return (-1);
3284 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
3285 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
3287 if (create1 < create2)
3288 rv = -1;
3289 else if (create1 > create2)
3290 rv = +1;
3291 else
3292 rv = 0;
3294 zfs_close(guid1hdl);
3295 zfs_close(guid2hdl);
3297 return (rv);
3301 * This function reestablishes the hierarchy of encryption roots after a
3302 * recursive incremental receive has completed. This must be done after the
3303 * second call to recv_incremental_replication() has renamed and promoted all
3304 * sent datasets to their final locations in the dataset hierarchy.
3306 static int
3307 recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
3308 nvlist_t *stream_nv)
3310 int err;
3311 nvpair_t *fselem = NULL;
3312 nvlist_t *stream_fss;
3314 stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3316 while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3317 zfs_handle_t *zhp = NULL;
3318 uint64_t crypt;
3319 nvlist_t *snaps, *props, *stream_nvfs = NULL;
3320 nvpair_t *snapel = NULL;
3321 boolean_t is_encroot, is_clone, stream_encroot;
3322 char *cp;
3323 char *stream_keylocation = NULL;
3324 char keylocation[MAXNAMELEN];
3325 char fsname[ZFS_MAX_DATASET_NAME_LEN];
3327 keylocation[0] = '\0';
3328 stream_nvfs = fnvpair_value_nvlist(fselem);
3329 snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps");
3330 props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3331 stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3333 /* find a snapshot from the stream that exists locally */
3334 err = ENOENT;
3335 while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
3336 uint64_t guid;
3338 guid = fnvpair_value_uint64(snapel);
3339 err = guid_to_name(hdl, top_zfs, guid, B_FALSE,
3340 fsname);
3341 if (err == 0)
3342 break;
3345 if (err != 0)
3346 continue;
3348 cp = strchr(fsname, '@');
3349 if (cp != NULL)
3350 *cp = '\0';
3352 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3353 if (zhp == NULL) {
3354 err = ENOENT;
3355 goto error;
3358 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
3359 is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3360 (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3362 /* we don't need to do anything for unencrypted datasets */
3363 if (crypt == ZIO_CRYPT_OFF) {
3364 zfs_close(zhp);
3365 continue;
3369 * If the dataset is flagged as an encryption root, was not
3370 * received as a clone and is not currently an encryption root,
3371 * force it to become one. Fixup the keylocation if necessary.
3373 if (stream_encroot) {
3374 if (!is_clone && !is_encroot) {
3375 err = lzc_change_key(fsname,
3376 DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
3377 if (err != 0) {
3378 zfs_close(zhp);
3379 goto error;
3383 stream_keylocation = fnvlist_lookup_string(props,
3384 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
3387 * Refresh the properties in case the call to
3388 * lzc_change_key() changed the value.
3390 zfs_refresh_properties(zhp);
3391 err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
3392 keylocation, sizeof (keylocation), NULL, NULL,
3393 0, B_TRUE);
3394 if (err != 0) {
3395 zfs_close(zhp);
3396 goto error;
3399 if (strcmp(keylocation, stream_keylocation) != 0) {
3400 err = zfs_prop_set(zhp,
3401 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
3402 stream_keylocation);
3403 if (err != 0) {
3404 zfs_close(zhp);
3405 goto error;
3411 * If the dataset is not flagged as an encryption root and is
3412 * currently an encryption root, force it to inherit from its
3413 * parent. The root of a raw send should never be
3414 * force-inherited.
3416 if (!stream_encroot && is_encroot &&
3417 strcmp(top_zfs, fsname) != 0) {
3418 err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
3419 NULL, NULL, 0);
3420 if (err != 0) {
3421 zfs_close(zhp);
3422 goto error;
3426 zfs_close(zhp);
3429 return (0);
3431 error:
3432 return (err);
3435 static int
3436 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
3437 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3438 nvlist_t *renamed)
3440 nvlist_t *local_nv, *deleted = NULL;
3441 avl_tree_t *local_avl;
3442 nvpair_t *fselem, *nextfselem;
3443 char *fromsnap;
3444 char newname[ZFS_MAX_DATASET_NAME_LEN];
3445 char guidname[32];
3446 int error;
3447 boolean_t needagain, progress, recursive;
3448 char *s1, *s2;
3450 fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap");
3452 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3453 ENOENT);
3455 if (flags->dryrun)
3456 return (0);
3458 again:
3459 needagain = progress = B_FALSE;
3461 deleted = fnvlist_alloc();
3463 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
3464 recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE,
3465 B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
3466 return (error);
3469 * Process deletes and renames
3471 for (fselem = nvlist_next_nvpair(local_nv, NULL);
3472 fselem; fselem = nextfselem) {
3473 nvlist_t *nvfs, *snaps;
3474 nvlist_t *stream_nvfs = NULL;
3475 nvpair_t *snapelem, *nextsnapelem;
3476 uint64_t fromguid = 0;
3477 uint64_t originguid = 0;
3478 uint64_t stream_originguid = 0;
3479 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
3480 char *fsname, *stream_fsname;
3482 nextfselem = nvlist_next_nvpair(local_nv, fselem);
3484 nvfs = fnvpair_value_nvlist(fselem);
3485 snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
3486 fsname = fnvlist_lookup_string(nvfs, "name");
3487 parent_fromsnap_guid = fnvlist_lookup_uint64(nvfs,
3488 "parentfromsnap");
3489 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
3492 * First find the stream's fs, so we can check for
3493 * a different origin (due to "zfs promote")
3495 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3496 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3497 uint64_t thisguid;
3499 thisguid = fnvpair_value_uint64(snapelem);
3500 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3502 if (stream_nvfs != NULL)
3503 break;
3506 /* check for promote */
3507 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
3508 &stream_originguid);
3509 if (stream_nvfs && originguid != stream_originguid) {
3510 switch (created_before(hdl, local_avl,
3511 stream_originguid, originguid)) {
3512 case 1: {
3513 /* promote it! */
3514 nvlist_t *origin_nvfs;
3515 char *origin_fsname;
3517 origin_nvfs = fsavl_find(local_avl, originguid,
3518 NULL);
3519 origin_fsname = fnvlist_lookup_string(
3520 origin_nvfs, "name");
3521 error = recv_promote(hdl, fsname, origin_fsname,
3522 flags);
3523 if (error == 0)
3524 progress = B_TRUE;
3525 break;
3527 default:
3528 break;
3529 case -1:
3530 fsavl_destroy(local_avl);
3531 fnvlist_free(local_nv);
3532 return (-1);
3535 * We had/have the wrong origin, therefore our
3536 * list of snapshots is wrong. Need to handle
3537 * them on the next pass.
3539 needagain = B_TRUE;
3540 continue;
3543 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3544 snapelem; snapelem = nextsnapelem) {
3545 uint64_t thisguid;
3546 char *stream_snapname;
3547 nvlist_t *found, *props;
3549 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
3551 thisguid = fnvpair_value_uint64(snapelem);
3552 found = fsavl_find(stream_avl, thisguid,
3553 &stream_snapname);
3555 /* check for delete */
3556 if (found == NULL) {
3557 char name[ZFS_MAX_DATASET_NAME_LEN];
3559 if (!flags->force)
3560 continue;
3562 (void) snprintf(name, sizeof (name), "%s@%s",
3563 fsname, nvpair_name(snapelem));
3565 error = recv_destroy(hdl, name,
3566 strlen(fsname)+1, newname, flags);
3567 if (error)
3568 needagain = B_TRUE;
3569 else
3570 progress = B_TRUE;
3571 sprintf(guidname, "%llu",
3572 (u_longlong_t)thisguid);
3573 nvlist_add_boolean(deleted, guidname);
3574 continue;
3577 stream_nvfs = found;
3579 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
3580 &props) && 0 == nvlist_lookup_nvlist(props,
3581 stream_snapname, &props)) {
3582 zfs_cmd_t zc = {"\0"};
3584 zc.zc_cookie = B_TRUE; /* received */
3585 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
3586 "%s@%s", fsname, nvpair_name(snapelem));
3587 zcmd_write_src_nvlist(hdl, &zc, props);
3588 (void) zfs_ioctl(hdl,
3589 ZFS_IOC_SET_PROP, &zc);
3590 zcmd_free_nvlists(&zc);
3593 /* check for different snapname */
3594 if (strcmp(nvpair_name(snapelem),
3595 stream_snapname) != 0) {
3596 char name[ZFS_MAX_DATASET_NAME_LEN];
3597 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3599 (void) snprintf(name, sizeof (name), "%s@%s",
3600 fsname, nvpair_name(snapelem));
3601 (void) snprintf(tryname, sizeof (name), "%s@%s",
3602 fsname, stream_snapname);
3604 error = recv_rename(hdl, name, tryname,
3605 strlen(fsname)+1, newname, flags);
3606 if (error)
3607 needagain = B_TRUE;
3608 else
3609 progress = B_TRUE;
3612 if (strcmp(stream_snapname, fromsnap) == 0)
3613 fromguid = thisguid;
3616 /* check for delete */
3617 if (stream_nvfs == NULL) {
3618 if (!flags->force)
3619 continue;
3621 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
3622 newname, flags);
3623 if (error)
3624 needagain = B_TRUE;
3625 else
3626 progress = B_TRUE;
3627 sprintf(guidname, "%llu",
3628 (u_longlong_t)parent_fromsnap_guid);
3629 nvlist_add_boolean(deleted, guidname);
3630 continue;
3633 if (fromguid == 0) {
3634 if (flags->verbose) {
3635 (void) printf("local fs %s does not have "
3636 "fromsnap (%s in stream); must have "
3637 "been deleted locally; ignoring\n",
3638 fsname, fromsnap);
3640 continue;
3643 stream_fsname = fnvlist_lookup_string(stream_nvfs, "name");
3644 stream_parent_fromsnap_guid = fnvlist_lookup_uint64(
3645 stream_nvfs, "parentfromsnap");
3647 s1 = strrchr(fsname, '/');
3648 s2 = strrchr(stream_fsname, '/');
3651 * Check if we're going to rename based on parent guid change
3652 * and the current parent guid was also deleted. If it was then
3653 * rename will fail and is likely unneeded, so avoid this and
3654 * force an early retry to determine the new
3655 * parent_fromsnap_guid.
3657 if (stream_parent_fromsnap_guid != 0 &&
3658 parent_fromsnap_guid != 0 &&
3659 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
3660 sprintf(guidname, "%llu",
3661 (u_longlong_t)parent_fromsnap_guid);
3662 if (nvlist_exists(deleted, guidname)) {
3663 progress = B_TRUE;
3664 needagain = B_TRUE;
3665 goto doagain;
3670 * Check for rename. If the exact receive path is specified, it
3671 * does not count as a rename, but we still need to check the
3672 * datasets beneath it.
3674 if ((stream_parent_fromsnap_guid != 0 &&
3675 parent_fromsnap_guid != 0 &&
3676 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
3677 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3678 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3679 nvlist_t *parent;
3680 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3682 parent = fsavl_find(local_avl,
3683 stream_parent_fromsnap_guid, NULL);
3685 * NB: parent might not be found if we used the
3686 * tosnap for stream_parent_fromsnap_guid,
3687 * because the parent is a newly-created fs;
3688 * we'll be able to rename it after we recv the
3689 * new fs.
3691 if (parent != NULL) {
3692 char *pname;
3694 pname = fnvlist_lookup_string(parent, "name");
3695 (void) snprintf(tryname, sizeof (tryname),
3696 "%s%s", pname, strrchr(stream_fsname, '/'));
3697 } else {
3698 tryname[0] = '\0';
3699 if (flags->verbose) {
3700 (void) printf("local fs %s new parent "
3701 "not found\n", fsname);
3705 newname[0] = '\0';
3707 error = recv_rename(hdl, fsname, tryname,
3708 strlen(tofs)+1, newname, flags);
3710 if (renamed != NULL && newname[0] != '\0') {
3711 fnvlist_add_boolean(renamed, newname);
3714 if (error)
3715 needagain = B_TRUE;
3716 else
3717 progress = B_TRUE;
3721 doagain:
3722 fsavl_destroy(local_avl);
3723 fnvlist_free(local_nv);
3724 fnvlist_free(deleted);
3726 if (needagain && progress) {
3727 /* do another pass to fix up temporary names */
3728 if (flags->verbose)
3729 (void) printf("another pass:\n");
3730 goto again;
3733 return (needagain || error != 0);
3736 static int
3737 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
3738 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3739 char **top_zfs, nvlist_t *cmdprops)
3741 nvlist_t *stream_nv = NULL;
3742 avl_tree_t *stream_avl = NULL;
3743 char *fromsnap = NULL;
3744 char *sendsnap = NULL;
3745 char *cp;
3746 char tofs[ZFS_MAX_DATASET_NAME_LEN];
3747 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
3748 char errbuf[ERRBUFLEN];
3749 dmu_replay_record_t drre;
3750 int error;
3751 boolean_t anyerr = B_FALSE;
3752 boolean_t softerr = B_FALSE;
3753 boolean_t recursive, raw;
3755 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3756 "cannot receive"));
3758 assert(drr->drr_type == DRR_BEGIN);
3759 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
3760 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3761 DMU_COMPOUNDSTREAM);
3764 * Read in the nvlist from the stream.
3766 if (drr->drr_payloadlen != 0) {
3767 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
3768 &stream_nv, flags->byteswap, zc);
3769 if (error) {
3770 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3771 goto out;
3775 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3776 ENOENT);
3777 raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3779 if (recursive && strchr(destname, '@')) {
3780 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3781 "cannot specify snapshot name for multi-snapshot stream"));
3782 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3783 goto out;
3787 * Read in the end record and verify checksum.
3789 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3790 flags->byteswap, NULL)))
3791 goto out;
3792 if (flags->byteswap) {
3793 drre.drr_type = BSWAP_32(drre.drr_type);
3794 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3795 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3796 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3797 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3798 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3799 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3800 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3801 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3803 if (drre.drr_type != DRR_END) {
3804 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3805 goto out;
3807 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3808 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3809 "incorrect header checksum"));
3810 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3811 goto out;
3814 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3816 if (drr->drr_payloadlen != 0) {
3817 nvlist_t *stream_fss;
3819 stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3820 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3821 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3822 "couldn't allocate avl tree"));
3823 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3824 goto out;
3827 if (fromsnap != NULL && recursive) {
3828 nvlist_t *renamed = NULL;
3829 nvpair_t *pair = NULL;
3831 (void) strlcpy(tofs, destname, sizeof (tofs));
3832 if (flags->isprefix) {
3833 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3834 int i;
3836 if (flags->istail) {
3837 cp = strrchr(drrb->drr_toname, '/');
3838 if (cp == NULL) {
3839 (void) strlcat(tofs, "/",
3840 sizeof (tofs));
3841 i = 0;
3842 } else {
3843 i = (cp - drrb->drr_toname);
3845 } else {
3846 i = strcspn(drrb->drr_toname, "/@");
3848 /* zfs_receive_one() will create_parents() */
3849 (void) strlcat(tofs, &drrb->drr_toname[i],
3850 sizeof (tofs));
3851 *strchr(tofs, '@') = '\0';
3854 if (!flags->dryrun && !flags->nomount) {
3855 renamed = fnvlist_alloc();
3858 softerr = recv_incremental_replication(hdl, tofs, flags,
3859 stream_nv, stream_avl, renamed);
3861 /* Unmount renamed filesystems before receiving. */
3862 while ((pair = nvlist_next_nvpair(renamed,
3863 pair)) != NULL) {
3864 zfs_handle_t *zhp;
3865 prop_changelist_t *clp = NULL;
3867 zhp = zfs_open(hdl, nvpair_name(pair),
3868 ZFS_TYPE_FILESYSTEM);
3869 if (zhp != NULL) {
3870 clp = changelist_gather(zhp,
3871 ZFS_PROP_MOUNTPOINT, 0,
3872 flags->forceunmount ? MS_FORCE : 0);
3873 zfs_close(zhp);
3874 if (clp != NULL) {
3875 softerr |=
3876 changelist_prefix(clp);
3877 changelist_free(clp);
3882 fnvlist_free(renamed);
3887 * Get the fs specified by the first path in the stream (the top level
3888 * specified by 'zfs send') and pass it to each invocation of
3889 * zfs_receive_one().
3891 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
3892 sizeof (sendfs));
3893 if ((cp = strchr(sendfs, '@')) != NULL) {
3894 *cp = '\0';
3896 * Find the "sendsnap", the final snapshot in a replication
3897 * stream. zfs_receive_one() handles certain errors
3898 * differently, depending on if the contained stream is the
3899 * last one or not.
3901 sendsnap = (cp + 1);
3904 /* Finally, receive each contained stream */
3905 do {
3907 * we should figure out if it has a recoverable
3908 * error, in which case do a recv_skip() and drive on.
3909 * Note, if we fail due to already having this guid,
3910 * zfs_receive_one() will take care of it (ie,
3911 * recv_skip() and return 0).
3913 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3914 sendfs, stream_nv, stream_avl, top_zfs, sendsnap, cmdprops);
3915 if (error == ENODATA) {
3916 error = 0;
3917 break;
3919 anyerr |= error;
3920 } while (error == 0);
3922 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
3924 * Now that we have the fs's they sent us, try the
3925 * renames again.
3927 softerr = recv_incremental_replication(hdl, tofs, flags,
3928 stream_nv, stream_avl, NULL);
3931 if (raw && softerr == 0 && *top_zfs != NULL) {
3932 softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
3933 stream_nv);
3936 out:
3937 fsavl_destroy(stream_avl);
3938 fnvlist_free(stream_nv);
3939 if (softerr)
3940 error = -2;
3941 if (anyerr)
3942 error = -1;
3943 return (error);
3946 static void
3947 trunc_prop_errs(int truncated)
3949 ASSERT(truncated != 0);
3951 if (truncated == 1)
3952 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3953 "1 more property could not be set\n"));
3954 else
3955 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3956 "%d more properties could not be set\n"), truncated);
3959 static int
3960 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3962 dmu_replay_record_t *drr;
3963 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
3964 uint64_t payload_size;
3965 char errbuf[ERRBUFLEN];
3967 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3968 "cannot receive"));
3970 /* XXX would be great to use lseek if possible... */
3971 drr = buf;
3973 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3974 byteswap, NULL) == 0) {
3975 if (byteswap)
3976 drr->drr_type = BSWAP_32(drr->drr_type);
3978 switch (drr->drr_type) {
3979 case DRR_BEGIN:
3980 if (drr->drr_payloadlen != 0) {
3981 (void) recv_read(hdl, fd, buf,
3982 drr->drr_payloadlen, B_FALSE, NULL);
3984 break;
3986 case DRR_END:
3987 free(buf);
3988 return (0);
3990 case DRR_OBJECT:
3991 if (byteswap) {
3992 drr->drr_u.drr_object.drr_bonuslen =
3993 BSWAP_32(drr->drr_u.drr_object.
3994 drr_bonuslen);
3995 drr->drr_u.drr_object.drr_raw_bonuslen =
3996 BSWAP_32(drr->drr_u.drr_object.
3997 drr_raw_bonuslen);
4000 payload_size =
4001 DRR_OBJECT_PAYLOAD_SIZE(&drr->drr_u.drr_object);
4002 (void) recv_read(hdl, fd, buf, payload_size,
4003 B_FALSE, NULL);
4004 break;
4006 case DRR_WRITE:
4007 if (byteswap) {
4008 drr->drr_u.drr_write.drr_logical_size =
4009 BSWAP_64(
4010 drr->drr_u.drr_write.drr_logical_size);
4011 drr->drr_u.drr_write.drr_compressed_size =
4012 BSWAP_64(
4013 drr->drr_u.drr_write.drr_compressed_size);
4015 payload_size =
4016 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
4017 assert(payload_size <= SPA_MAXBLOCKSIZE);
4018 (void) recv_read(hdl, fd, buf,
4019 payload_size, B_FALSE, NULL);
4020 break;
4021 case DRR_SPILL:
4022 if (byteswap) {
4023 drr->drr_u.drr_spill.drr_length =
4024 BSWAP_64(drr->drr_u.drr_spill.drr_length);
4025 drr->drr_u.drr_spill.drr_compressed_size =
4026 BSWAP_64(drr->drr_u.drr_spill.
4027 drr_compressed_size);
4030 payload_size =
4031 DRR_SPILL_PAYLOAD_SIZE(&drr->drr_u.drr_spill);
4032 (void) recv_read(hdl, fd, buf, payload_size,
4033 B_FALSE, NULL);
4034 break;
4035 case DRR_WRITE_EMBEDDED:
4036 if (byteswap) {
4037 drr->drr_u.drr_write_embedded.drr_psize =
4038 BSWAP_32(drr->drr_u.drr_write_embedded.
4039 drr_psize);
4041 (void) recv_read(hdl, fd, buf,
4042 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
4043 8), B_FALSE, NULL);
4044 break;
4045 case DRR_OBJECT_RANGE:
4046 case DRR_WRITE_BYREF:
4047 case DRR_FREEOBJECTS:
4048 case DRR_FREE:
4049 break;
4051 default:
4052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4053 "invalid record type"));
4054 free(buf);
4055 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4059 free(buf);
4060 return (-1);
4063 static void
4064 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
4065 boolean_t resumable, boolean_t checksum)
4067 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
4069 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, (checksum ?
4070 "checksum mismatch" : "incomplete stream")));
4072 if (!resumable)
4073 return;
4074 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
4075 *strchr(target_fs, '@') = '\0';
4076 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
4077 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4078 if (zhp == NULL)
4079 return;
4081 char token_buf[ZFS_MAXPROPLEN];
4082 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4083 token_buf, sizeof (token_buf),
4084 NULL, NULL, 0, B_TRUE);
4085 if (error == 0) {
4086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4087 "checksum mismatch or incomplete stream.\n"
4088 "Partially received snapshot is saved.\n"
4089 "A resuming stream can be generated on the sending "
4090 "system by running:\n"
4091 " zfs send -t %s"),
4092 token_buf);
4094 zfs_close(zhp);
4098 * Prepare a new nvlist of properties that are to override (-o) or be excluded
4099 * (-x) from the received dataset
4100 * recvprops: received properties from the send stream
4101 * cmdprops: raw input properties from command line
4102 * origprops: properties, both locally-set and received, currently set on the
4103 * target dataset if it exists, NULL otherwise.
4104 * oxprops: valid output override (-o) and excluded (-x) properties
4106 static int
4107 zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
4108 char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
4109 boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
4110 nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
4111 uint_t *wkeylen_out, const char *errbuf)
4113 nvpair_t *nvp;
4114 nvlist_t *oprops, *voprops;
4115 zfs_handle_t *zhp = NULL;
4116 zpool_handle_t *zpool_hdl = NULL;
4117 char *cp;
4118 int ret = 0;
4119 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4121 if (nvlist_empty(cmdprops))
4122 return (0); /* No properties to override or exclude */
4124 *oxprops = fnvlist_alloc();
4125 oprops = fnvlist_alloc();
4127 strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
4130 * Get our dataset handle. The target dataset may not exist yet.
4132 if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
4133 zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
4134 if (zhp == NULL) {
4135 ret = -1;
4136 goto error;
4140 /* open the zpool handle */
4141 cp = strchr(namebuf, '/');
4142 if (cp != NULL)
4143 *cp = '\0';
4144 zpool_hdl = zpool_open(hdl, namebuf);
4145 if (zpool_hdl == NULL) {
4146 ret = -1;
4147 goto error;
4150 /* restore namebuf to match fsname for later use */
4151 if (cp != NULL)
4152 *cp = '/';
4155 * first iteration: process excluded (-x) properties now and gather
4156 * added (-o) properties to be later processed by zfs_valid_proplist()
4158 nvp = NULL;
4159 while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
4160 const char *name = nvpair_name(nvp);
4161 zfs_prop_t prop = zfs_name_to_prop(name);
4164 * It turns out, if we don't normalize "aliased" names
4165 * e.g. compress= against the "real" names (e.g. compression)
4166 * here, then setting/excluding them does not work as
4167 * intended.
4169 * But since user-defined properties wouldn't have a valid
4170 * mapping here, we do this conditional dance.
4172 const char *newname = name;
4173 if (prop >= ZFS_PROP_TYPE)
4174 newname = zfs_prop_to_name(prop);
4176 /* "origin" is processed separately, don't handle it here */
4177 if (prop == ZFS_PROP_ORIGIN)
4178 continue;
4180 /* raw streams can't override encryption properties */
4181 if ((zfs_prop_encryption_key_param(prop) ||
4182 prop == ZFS_PROP_ENCRYPTION) && raw) {
4183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4184 "encryption property '%s' cannot "
4185 "be set or excluded for raw streams."), name);
4186 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4187 goto error;
4191 * For plain replicated send, we can ignore encryption
4192 * properties other than first stream
4194 if ((zfs_prop_encryption_key_param(prop) || prop ==
4195 ZFS_PROP_ENCRYPTION) && !newfs && recursive && !raw) {
4196 continue;
4199 /* incremental streams can only exclude encryption properties */
4200 if ((zfs_prop_encryption_key_param(prop) ||
4201 prop == ZFS_PROP_ENCRYPTION) && !newfs &&
4202 nvpair_type(nvp) != DATA_TYPE_BOOLEAN) {
4203 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4204 "encryption property '%s' cannot "
4205 "be set for incremental streams."), name);
4206 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4207 goto error;
4210 switch (nvpair_type(nvp)) {
4211 case DATA_TYPE_BOOLEAN: /* -x property */
4213 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
4214 * a property: this is done by forcing an explicit
4215 * inherit on the destination so the effective value is
4216 * not the one we received from the send stream.
4218 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
4219 !zfs_prop_user(name)) {
4220 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4221 "Warning: %s: property '%s' does not "
4222 "apply to datasets of this type\n"),
4223 fsname, name);
4224 continue;
4227 * We do this only if the property is not already
4228 * locally-set, in which case its value will take
4229 * priority over the received anyway.
4231 if (nvlist_exists(origprops, newname)) {
4232 nvlist_t *attrs;
4233 char *source = NULL;
4235 attrs = fnvlist_lookup_nvlist(origprops,
4236 newname);
4237 if (nvlist_lookup_string(attrs,
4238 ZPROP_SOURCE, &source) == 0 &&
4239 strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
4240 continue;
4243 * We can't force an explicit inherit on non-inheritable
4244 * properties: if we're asked to exclude this kind of
4245 * values we remove them from "recvprops" input nvlist.
4247 if (!zfs_prop_user(name) && /* can be inherited too */
4248 !zfs_prop_inheritable(prop) &&
4249 nvlist_exists(recvprops, newname))
4250 fnvlist_remove(recvprops, newname);
4251 else
4252 fnvlist_add_boolean(*oxprops, newname);
4253 break;
4254 case DATA_TYPE_STRING: /* -o property=value */
4256 * we're trying to override a property that does not
4257 * make sense for this type of dataset, but we don't
4258 * want to fail if the receive is recursive: this comes
4259 * in handy when the send stream contains, for
4260 * instance, a child ZVOL and we're trying to receive
4261 * it with "-o atime=on"
4263 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
4264 !zfs_prop_user(name)) {
4265 if (recursive)
4266 continue;
4267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4268 "property '%s' does not apply to datasets "
4269 "of this type"), name);
4270 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4271 goto error;
4273 fnvlist_add_string(oprops, newname,
4274 fnvpair_value_string(nvp));
4275 break;
4276 default:
4277 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4278 "property '%s' must be a string or boolean"), name);
4279 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4280 goto error;
4284 if (toplevel) {
4285 /* convert override strings properties to native */
4286 if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
4287 oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4288 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4289 goto error;
4293 * zfs_crypto_create() requires the parent name. Get it
4294 * by truncating the fsname copy stored in namebuf.
4296 cp = strrchr(namebuf, '/');
4297 if (cp != NULL)
4298 *cp = '\0';
4300 if (!raw && !(!newfs && recursive) &&
4301 zfs_crypto_create(hdl, namebuf, voprops, NULL,
4302 B_FALSE, wkeydata_out, wkeylen_out) != 0) {
4303 fnvlist_free(voprops);
4304 ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4305 goto error;
4308 /* second pass: process "-o" properties */
4309 fnvlist_merge(*oxprops, voprops);
4310 fnvlist_free(voprops);
4311 } else {
4312 /* override props on child dataset are inherited */
4313 nvp = NULL;
4314 while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
4315 const char *name = nvpair_name(nvp);
4316 fnvlist_add_boolean(*oxprops, name);
4320 error:
4321 if (zhp != NULL)
4322 zfs_close(zhp);
4323 if (zpool_hdl != NULL)
4324 zpool_close(zpool_hdl);
4325 fnvlist_free(oprops);
4326 return (ret);
4330 * Restores a backup of tosnap from the file descriptor specified by infd.
4332 static int
4333 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
4334 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
4335 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
4336 avl_tree_t *stream_avl, char **top_zfs,
4337 const char *finalsnap, nvlist_t *cmdprops)
4339 struct timespec begin_time;
4340 int ioctl_err, ioctl_errno, err;
4341 char *cp;
4342 struct drr_begin *drrb = &drr->drr_u.drr_begin;
4343 char errbuf[ERRBUFLEN];
4344 const char *chopprefix;
4345 boolean_t newfs = B_FALSE;
4346 boolean_t stream_wantsnewfs, stream_resumingnewfs;
4347 boolean_t newprops = B_FALSE;
4348 uint64_t read_bytes = 0;
4349 uint64_t errflags = 0;
4350 uint64_t parent_snapguid = 0;
4351 prop_changelist_t *clp = NULL;
4352 nvlist_t *snapprops_nvlist = NULL;
4353 nvlist_t *snapholds_nvlist = NULL;
4354 zprop_errflags_t prop_errflags;
4355 nvlist_t *prop_errors = NULL;
4356 boolean_t recursive;
4357 char *snapname = NULL;
4358 char destsnap[MAXPATHLEN * 2];
4359 char origin[MAXNAMELEN] = {0};
4360 char name[MAXPATHLEN];
4361 char tmp_keylocation[MAXNAMELEN] = {0};
4362 nvlist_t *rcvprops = NULL; /* props received from the send stream */
4363 nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
4364 nvlist_t *origprops = NULL; /* original props (if destination exists) */
4365 zfs_type_t type = ZFS_TYPE_INVALID;
4366 boolean_t toplevel = B_FALSE;
4367 boolean_t zoned = B_FALSE;
4368 boolean_t hastoken = B_FALSE;
4369 boolean_t redacted;
4370 uint8_t *wkeydata = NULL;
4371 uint_t wkeylen = 0;
4373 #ifndef CLOCK_MONOTONIC_RAW
4374 #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
4375 #endif
4376 clock_gettime(CLOCK_MONOTONIC_RAW, &begin_time);
4378 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4379 "cannot receive"));
4381 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
4382 ENOENT);
4384 /* Did the user request holds be skipped via zfs recv -k? */
4385 boolean_t holds = flags->holds && !flags->skipholds;
4387 if (stream_avl != NULL) {
4388 char *keylocation = NULL;
4389 nvlist_t *lookup = NULL;
4390 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
4391 &snapname);
4393 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
4394 &parent_snapguid);
4395 err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
4396 if (err) {
4397 rcvprops = fnvlist_alloc();
4398 newprops = B_TRUE;
4402 * The keylocation property may only be set on encryption roots,
4403 * but this dataset might not become an encryption root until
4404 * recv_fix_encryption_hierarchy() is called. That function
4405 * will fixup the keylocation anyway, so we temporarily unset
4406 * the keylocation for now to avoid any errors from the receive
4407 * ioctl.
4409 err = nvlist_lookup_string(rcvprops,
4410 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
4411 if (err == 0) {
4412 strlcpy(tmp_keylocation, keylocation, MAXNAMELEN);
4413 (void) nvlist_remove_all(rcvprops,
4414 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
4417 if (flags->canmountoff) {
4418 fnvlist_add_uint64(rcvprops,
4419 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0);
4420 } else if (newprops) { /* nothing in rcvprops, eliminate it */
4421 fnvlist_free(rcvprops);
4422 rcvprops = NULL;
4423 newprops = B_FALSE;
4425 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
4426 snapprops_nvlist = fnvlist_lookup_nvlist(lookup,
4427 snapname);
4429 if (holds) {
4430 if (0 == nvlist_lookup_nvlist(fs, "snapholds",
4431 &lookup)) {
4432 snapholds_nvlist = fnvlist_lookup_nvlist(
4433 lookup, snapname);
4438 cp = NULL;
4441 * Determine how much of the snapshot name stored in the stream
4442 * we are going to tack on to the name they specified on the
4443 * command line, and how much we are going to chop off.
4445 * If they specified a snapshot, chop the entire name stored in
4446 * the stream.
4448 if (flags->istail) {
4450 * A filesystem was specified with -e. We want to tack on only
4451 * the tail of the sent snapshot path.
4453 if (strchr(tosnap, '@')) {
4454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4455 "argument - snapshot not allowed with -e"));
4456 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4457 goto out;
4460 chopprefix = strrchr(sendfs, '/');
4462 if (chopprefix == NULL) {
4464 * The tail is the poolname, so we need to
4465 * prepend a path separator.
4467 int len = strlen(drrb->drr_toname);
4468 cp = umem_alloc(len + 2, UMEM_NOFAIL);
4469 cp[0] = '/';
4470 (void) strcpy(&cp[1], drrb->drr_toname);
4471 chopprefix = cp;
4472 } else {
4473 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
4475 } else if (flags->isprefix) {
4477 * A filesystem was specified with -d. We want to tack on
4478 * everything but the first element of the sent snapshot path
4479 * (all but the pool name).
4481 if (strchr(tosnap, '@')) {
4482 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4483 "argument - snapshot not allowed with -d"));
4484 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4485 goto out;
4488 chopprefix = strchr(drrb->drr_toname, '/');
4489 if (chopprefix == NULL)
4490 chopprefix = strchr(drrb->drr_toname, '@');
4491 } else if (strchr(tosnap, '@') == NULL) {
4493 * If a filesystem was specified without -d or -e, we want to
4494 * tack on everything after the fs specified by 'zfs send'.
4496 chopprefix = drrb->drr_toname + strlen(sendfs);
4497 } else {
4498 /* A snapshot was specified as an exact path (no -d or -e). */
4499 if (recursive) {
4500 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4501 "cannot specify snapshot name for multi-snapshot "
4502 "stream"));
4503 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4504 goto out;
4506 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
4509 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
4510 ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
4511 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
4512 strchr(sendfs, '/') == NULL);
4513 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
4514 chopprefix[0] == '\0');
4517 * Determine name of destination snapshot.
4519 (void) strlcpy(destsnap, tosnap, sizeof (destsnap));
4520 (void) strlcat(destsnap, chopprefix, sizeof (destsnap));
4521 if (cp != NULL)
4522 umem_free(cp, strlen(cp) + 1);
4523 if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
4524 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4525 goto out;
4529 * Determine the name of the origin snapshot.
4531 if (originsnap) {
4532 (void) strlcpy(origin, originsnap, sizeof (origin));
4533 if (flags->verbose)
4534 (void) printf("using provided clone origin %s\n",
4535 origin);
4536 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
4537 if (guid_to_name(hdl, destsnap,
4538 drrb->drr_fromguid, B_FALSE, origin) != 0) {
4539 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4540 "local origin for clone %s does not exist"),
4541 destsnap);
4542 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4543 goto out;
4545 if (flags->verbose)
4546 (void) printf("found clone origin %s\n", origin);
4549 if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4550 DMU_BACKUP_FEATURE_DEDUP)) {
4551 (void) fprintf(stderr,
4552 gettext("ERROR: \"zfs receive\" no longer supports "
4553 "deduplicated send streams. Use\n"
4554 "the \"zstream redup\" command to convert this stream "
4555 "to a regular,\n"
4556 "non-deduplicated stream.\n"));
4557 err = zfs_error(hdl, EZFS_NOTSUP, errbuf);
4558 goto out;
4561 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4562 DMU_BACKUP_FEATURE_RESUMING;
4563 boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4564 DMU_BACKUP_FEATURE_RAW;
4565 boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4566 DMU_BACKUP_FEATURE_EMBED_DATA;
4567 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
4568 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
4569 stream_resumingnewfs = (drrb->drr_fromguid == 0 ||
4570 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && resuming;
4572 if (stream_wantsnewfs) {
4574 * if the parent fs does not exist, look for it based on
4575 * the parent snap GUID
4577 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4578 "cannot receive new filesystem stream"));
4580 (void) strlcpy(name, destsnap, sizeof (name));
4581 cp = strrchr(name, '/');
4582 if (cp)
4583 *cp = '\0';
4584 if (cp &&
4585 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4586 char suffix[ZFS_MAX_DATASET_NAME_LEN];
4587 (void) strlcpy(suffix, strrchr(destsnap, '/'),
4588 sizeof (suffix));
4589 if (guid_to_name(hdl, name, parent_snapguid,
4590 B_FALSE, destsnap) == 0) {
4591 *strchr(destsnap, '@') = '\0';
4592 (void) strlcat(destsnap, suffix,
4593 sizeof (destsnap));
4596 } else {
4598 * If the fs does not exist, look for it based on the
4599 * fromsnap GUID.
4601 if (resuming) {
4602 (void) snprintf(errbuf, sizeof (errbuf),
4603 dgettext(TEXT_DOMAIN,
4604 "cannot receive resume stream"));
4605 } else {
4606 (void) snprintf(errbuf, sizeof (errbuf),
4607 dgettext(TEXT_DOMAIN,
4608 "cannot receive incremental stream"));
4611 (void) strlcpy(name, destsnap, sizeof (name));
4612 *strchr(name, '@') = '\0';
4615 * If the exact receive path was specified and this is the
4616 * topmost path in the stream, then if the fs does not exist we
4617 * should look no further.
4619 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
4620 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
4621 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4622 char snap[ZFS_MAX_DATASET_NAME_LEN];
4623 (void) strlcpy(snap, strchr(destsnap, '@'),
4624 sizeof (snap));
4625 if (guid_to_name(hdl, name, drrb->drr_fromguid,
4626 B_FALSE, destsnap) == 0) {
4627 *strchr(destsnap, '@') = '\0';
4628 (void) strlcat(destsnap, snap,
4629 sizeof (destsnap));
4634 (void) strlcpy(name, destsnap, sizeof (name));
4635 *strchr(name, '@') = '\0';
4637 redacted = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4638 DMU_BACKUP_FEATURE_REDACTED;
4640 if (flags->heal) {
4641 if (flags->isprefix || flags->istail || flags->force ||
4642 flags->canmountoff || flags->resumable || flags->nomount ||
4643 flags->skipholds) {
4644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4645 "corrective recv can not be used when combined with"
4646 " this flag"));
4647 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4648 goto out;
4650 uint64_t guid =
4651 get_snap_guid(hdl, name, strchr(destsnap, '@') + 1);
4652 if (guid == 0) {
4653 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4654 "corrective recv must specify an existing snapshot"
4655 " to heal"));
4656 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4657 goto out;
4658 } else if (guid != drrb->drr_toguid) {
4659 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4660 "local snapshot doesn't match the snapshot"
4661 " in the provided stream"));
4662 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4663 goto out;
4665 } else if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4666 zfs_cmd_t zc = {"\0"};
4667 zfs_handle_t *zhp = NULL;
4668 boolean_t encrypted;
4670 (void) strcpy(zc.zc_name, name);
4673 * Destination fs exists. It must be one of these cases:
4674 * - an incremental send stream
4675 * - the stream specifies a new fs (full stream or clone)
4676 * and they want us to blow away the existing fs (and
4677 * have therefore specified -F and removed any snapshots)
4678 * - we are resuming a failed receive.
4680 if (stream_wantsnewfs) {
4681 boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL;
4682 if (!flags->force) {
4683 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4684 "destination '%s' exists\n"
4685 "must specify -F to overwrite it"), name);
4686 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4687 goto out;
4689 if (zfs_ioctl(hdl, ZFS_IOC_SNAPSHOT_LIST_NEXT,
4690 &zc) == 0) {
4691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4692 "destination has snapshots (eg. %s)\n"
4693 "must destroy them to overwrite it"),
4694 zc.zc_name);
4695 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4696 goto out;
4698 if (is_volume && strrchr(name, '/') == NULL) {
4699 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4700 "destination %s is the root dataset\n"
4701 "cannot overwrite with a ZVOL"),
4702 name);
4703 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4704 goto out;
4706 if (is_volume &&
4707 zfs_ioctl(hdl, ZFS_IOC_DATASET_LIST_NEXT,
4708 &zc) == 0) {
4709 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4710 "destination has children (eg. %s)\n"
4711 "cannot overwrite with a ZVOL"),
4712 zc.zc_name);
4713 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4714 goto out;
4718 if ((zhp = zfs_open(hdl, name,
4719 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
4720 err = -1;
4721 goto out;
4725 * When receiving full/newfs on existing dataset, then it
4726 * should be done with "-F" flag. Its enforced for initial
4727 * receive in previous checks in this function.
4728 * Similarly, on resuming full/newfs recv on existing dataset,
4729 * it should be done with "-F" flag.
4731 * When dataset doesn't exist, then full/newfs recv is done on
4732 * newly created dataset and it's marked INCONSISTENT. But
4733 * When receiving on existing dataset, recv is first done on
4734 * %recv and its marked INCONSISTENT. Existing dataset is not
4735 * marked INCONSISTENT.
4736 * Resume of full/newfs receive with dataset not INCONSISTENT
4737 * indicates that its resuming newfs on existing dataset. So,
4738 * enforce "-F" flag in this case.
4740 if (stream_resumingnewfs &&
4741 !zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
4742 !flags->force) {
4743 zfs_close(zhp);
4744 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4745 "Resuming recv on existing destination '%s'\n"
4746 "must specify -F to overwrite it"), name);
4747 err = zfs_error(hdl, EZFS_RESUME_EXISTS, errbuf);
4748 goto out;
4751 if (stream_wantsnewfs &&
4752 zhp->zfs_dmustats.dds_origin[0]) {
4753 zfs_close(zhp);
4754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4755 "destination '%s' is a clone\n"
4756 "must destroy it to overwrite it"), name);
4757 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4758 goto out;
4762 * Raw sends can not be performed as an incremental on top
4763 * of existing unencrypted datasets. zfs recv -F can't be
4764 * used to blow away an existing encrypted filesystem. This
4765 * is because it would require the dsl dir to point to the
4766 * new key (or lack of a key) and the old key at the same
4767 * time. The -F flag may still be used for deleting
4768 * intermediate snapshots that would otherwise prevent the
4769 * receive from working.
4771 encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
4772 ZIO_CRYPT_OFF;
4773 if (!stream_wantsnewfs && !encrypted && raw) {
4774 zfs_close(zhp);
4775 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4776 "cannot perform raw receive on top of "
4777 "existing unencrypted dataset"));
4778 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4779 goto out;
4782 if (stream_wantsnewfs && flags->force &&
4783 ((raw && !encrypted) || encrypted)) {
4784 zfs_close(zhp);
4785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4786 "zfs receive -F cannot be used to destroy an "
4787 "encrypted filesystem or overwrite an "
4788 "unencrypted one with an encrypted one"));
4789 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4790 goto out;
4793 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4794 (stream_wantsnewfs || stream_resumingnewfs)) {
4795 /* We can't do online recv in this case */
4796 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4797 flags->forceunmount ? MS_FORCE : 0);
4798 if (clp == NULL) {
4799 zfs_close(zhp);
4800 err = -1;
4801 goto out;
4803 if (changelist_prefix(clp) != 0) {
4804 changelist_free(clp);
4805 zfs_close(zhp);
4806 err = -1;
4807 goto out;
4812 * If we are resuming a newfs, set newfs here so that we will
4813 * mount it if the recv succeeds this time. We can tell
4814 * that it was a newfs on the first recv because the fs
4815 * itself will be inconsistent (if the fs existed when we
4816 * did the first recv, we would have received it into
4817 * .../%recv).
4819 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4820 newfs = B_TRUE;
4822 /* we want to know if we're zoned when validating -o|-x props */
4823 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4825 /* may need this info later, get it now we have zhp around */
4826 if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4827 NULL, NULL, 0, B_TRUE) == 0)
4828 hastoken = B_TRUE;
4830 /* gather existing properties on destination */
4831 origprops = fnvlist_alloc();
4832 fnvlist_merge(origprops, zhp->zfs_props);
4833 fnvlist_merge(origprops, zhp->zfs_user_props);
4835 zfs_close(zhp);
4836 } else {
4837 zfs_handle_t *zhp;
4840 * Destination filesystem does not exist. Therefore we better
4841 * be creating a new filesystem (either from a full backup, or
4842 * a clone). It would therefore be invalid if the user
4843 * specified only the pool name (i.e. if the destination name
4844 * contained no slash character).
4846 cp = strrchr(name, '/');
4848 if (!stream_wantsnewfs || cp == NULL) {
4849 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4850 "destination '%s' does not exist"), name);
4851 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4852 goto out;
4856 * Trim off the final dataset component so we perform the
4857 * recvbackup ioctl to the filesystems's parent.
4859 *cp = '\0';
4861 if (flags->isprefix && !flags->istail && !flags->dryrun &&
4862 create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4863 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4864 goto out;
4867 /* validate parent */
4868 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4869 if (zhp == NULL) {
4870 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4871 goto out;
4873 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
4874 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4875 "parent '%s' is not a filesystem"), name);
4876 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4877 zfs_close(zhp);
4878 goto out;
4881 zfs_close(zhp);
4883 newfs = B_TRUE;
4884 *cp = '/';
4887 if (flags->verbose) {
4888 (void) printf("%s %s%s stream of %s into %s\n",
4889 flags->dryrun ? "would receive" : "receiving",
4890 flags->heal ? " corrective" : "",
4891 drrb->drr_fromguid ? "incremental" : "full",
4892 drrb->drr_toname, destsnap);
4893 (void) fflush(stdout);
4897 * If this is the top-level dataset, record it so we can use it
4898 * for recursive operations later.
4900 if (top_zfs != NULL &&
4901 (*top_zfs == NULL || strcmp(*top_zfs, name) == 0)) {
4902 toplevel = B_TRUE;
4903 if (*top_zfs == NULL)
4904 *top_zfs = zfs_strdup(hdl, name);
4907 if (drrb->drr_type == DMU_OST_ZVOL) {
4908 type = ZFS_TYPE_VOLUME;
4909 } else if (drrb->drr_type == DMU_OST_ZFS) {
4910 type = ZFS_TYPE_FILESYSTEM;
4911 } else {
4912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4913 "invalid record type: 0x%d"), drrb->drr_type);
4914 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4915 goto out;
4917 if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4918 stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4919 &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
4920 goto out;
4923 * When sending with properties (zfs send -p), the encryption property
4924 * is not included because it is a SETONCE property and therefore
4925 * treated as read only. However, we are always able to determine its
4926 * value because raw sends will include it in the DRR_BDEGIN payload
4927 * and non-raw sends with properties are not allowed for encrypted
4928 * datasets. Therefore, if this is a non-raw properties stream, we can
4929 * infer that the value should be ZIO_CRYPT_OFF and manually add that
4930 * to the received properties.
4932 if (stream_wantsnewfs && !raw && rcvprops != NULL &&
4933 !nvlist_exists(cmdprops, zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
4934 if (oxprops == NULL)
4935 oxprops = fnvlist_alloc();
4936 fnvlist_add_uint64(oxprops,
4937 zfs_prop_to_name(ZFS_PROP_ENCRYPTION), ZIO_CRYPT_OFF);
4940 if (flags->dryrun) {
4941 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
4944 * We have read the DRR_BEGIN record, but we have
4945 * not yet read the payload. For non-dryrun sends
4946 * this will be done by the kernel, so we must
4947 * emulate that here, before attempting to read
4948 * more records.
4950 err = recv_read(hdl, infd, buf, drr->drr_payloadlen,
4951 flags->byteswap, NULL);
4952 free(buf);
4953 if (err != 0)
4954 goto out;
4956 err = recv_skip(hdl, infd, flags->byteswap);
4957 goto out;
4960 if (flags->heal) {
4961 err = ioctl_err = lzc_receive_with_heal(destsnap, rcvprops,
4962 oxprops, wkeydata, wkeylen, origin, flags->force,
4963 flags->heal, flags->resumable, raw, infd, drr_noswap, -1,
4964 &read_bytes, &errflags, NULL, &prop_errors);
4965 } else {
4966 err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
4967 oxprops, wkeydata, wkeylen, origin, flags->force,
4968 flags->resumable, raw, infd, drr_noswap, -1, &read_bytes,
4969 &errflags, NULL, &prop_errors);
4971 ioctl_errno = ioctl_err;
4972 prop_errflags = errflags;
4974 if (err == 0) {
4975 nvpair_t *prop_err = NULL;
4977 while ((prop_err = nvlist_next_nvpair(prop_errors,
4978 prop_err)) != NULL) {
4979 char tbuf[1024];
4980 zfs_prop_t prop;
4981 int intval;
4983 prop = zfs_name_to_prop(nvpair_name(prop_err));
4984 (void) nvpair_value_int32(prop_err, &intval);
4985 if (strcmp(nvpair_name(prop_err),
4986 ZPROP_N_MORE_ERRORS) == 0) {
4987 trunc_prop_errs(intval);
4988 break;
4989 } else if (snapname == NULL || finalsnap == NULL ||
4990 strcmp(finalsnap, snapname) == 0 ||
4991 strcmp(nvpair_name(prop_err),
4992 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
4994 * Skip the special case of, for example,
4995 * "refquota", errors on intermediate
4996 * snapshots leading up to a final one.
4997 * That's why we have all of the checks above.
4999 * See zfs_ioctl.c's extract_delay_props() for
5000 * a list of props which can fail on
5001 * intermediate snapshots, but shouldn't
5002 * affect the overall receive.
5004 (void) snprintf(tbuf, sizeof (tbuf),
5005 dgettext(TEXT_DOMAIN,
5006 "cannot receive %s property on %s"),
5007 nvpair_name(prop_err), name);
5008 zfs_setprop_error(hdl, prop, intval, tbuf);
5013 if (err == 0 && snapprops_nvlist) {
5014 zfs_cmd_t zc = {"\0"};
5016 (void) strlcpy(zc.zc_name, destsnap, sizeof (zc.zc_name));
5017 zc.zc_cookie = B_TRUE; /* received */
5018 zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist);
5019 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
5020 zcmd_free_nvlists(&zc);
5022 if (err == 0 && snapholds_nvlist) {
5023 nvpair_t *pair;
5024 nvlist_t *holds, *errors = NULL;
5025 int cleanup_fd = -1;
5027 VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
5028 for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
5029 pair != NULL;
5030 pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
5031 fnvlist_add_string(holds, destsnap, nvpair_name(pair));
5033 (void) lzc_hold(holds, cleanup_fd, &errors);
5034 fnvlist_free(snapholds_nvlist);
5035 fnvlist_free(holds);
5038 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
5040 * It may be that this snapshot already exists,
5041 * in which case we want to consume & ignore it
5042 * rather than failing.
5044 avl_tree_t *local_avl;
5045 nvlist_t *local_nv, *fs;
5046 cp = strchr(destsnap, '@');
5049 * XXX Do this faster by just iterating over snaps in
5050 * this fs. Also if zc_value does not exist, we will
5051 * get a strange "does not exist" error message.
5053 *cp = '\0';
5054 if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
5055 B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE,
5056 B_TRUE, &local_nv, &local_avl) == 0) {
5057 *cp = '@';
5058 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
5059 fsavl_destroy(local_avl);
5060 fnvlist_free(local_nv);
5062 if (fs != NULL) {
5063 if (flags->verbose) {
5064 (void) printf("snap %s already exists; "
5065 "ignoring\n", destsnap);
5067 err = ioctl_err = recv_skip(hdl, infd,
5068 flags->byteswap);
5071 *cp = '@';
5074 if (ioctl_err != 0) {
5075 switch (ioctl_errno) {
5076 case ENODEV:
5077 cp = strchr(destsnap, '@');
5078 *cp = '\0';
5079 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5080 "most recent snapshot of %s does not\n"
5081 "match incremental source"), destsnap);
5082 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
5083 *cp = '@';
5084 break;
5085 case ETXTBSY:
5086 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5087 "destination %s has been modified\n"
5088 "since most recent snapshot"), name);
5089 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
5090 break;
5091 case EACCES:
5092 if (flags->heal) {
5093 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5094 "key must be loaded to do a non-raw "
5095 "corrective recv on an encrypted "
5096 "dataset."));
5097 } else if (raw && stream_wantsnewfs) {
5098 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5099 "failed to create encryption key"));
5100 } else if (raw && !stream_wantsnewfs) {
5101 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5102 "encryption key does not match "
5103 "existing key"));
5104 } else {
5105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5106 "inherited key must be loaded"));
5108 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
5109 break;
5110 case EEXIST:
5111 cp = strchr(destsnap, '@');
5112 if (newfs) {
5113 /* it's the containing fs that exists */
5114 *cp = '\0';
5116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5117 "destination already exists"));
5118 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
5119 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
5120 destsnap);
5121 *cp = '@';
5122 break;
5123 case EINVAL:
5124 if (embedded && !raw) {
5125 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5126 "incompatible embedded data stream "
5127 "feature with encrypted receive."));
5128 } else if (flags->resumable) {
5129 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5130 "kernel modules must be upgraded to "
5131 "receive this stream."));
5133 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5134 break;
5135 case ECKSUM:
5136 case ZFS_ERR_STREAM_TRUNCATED:
5137 if (flags->heal)
5138 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5139 "corrective receive was not able to "
5140 "reconstruct the data needed for "
5141 "healing."));
5142 else
5143 recv_ecksum_set_aux(hdl, destsnap,
5144 flags->resumable, ioctl_err == ECKSUM);
5145 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5146 break;
5147 case ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH:
5148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5149 "incremental send stream requires -L "
5150 "(--large-block), to match previous receive."));
5151 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5152 break;
5153 case ENOTSUP:
5154 if (flags->heal)
5155 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5156 "stream is not compatible with the "
5157 "data in the pool."));
5158 else
5159 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5160 "pool must be upgraded to receive this "
5161 "stream."));
5162 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5163 break;
5164 case EDQUOT:
5165 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5166 "destination %s space quota exceeded."), name);
5167 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
5168 break;
5169 case ZFS_ERR_FROM_IVSET_GUID_MISSING:
5170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5171 "IV set guid missing. See errata %u at "
5172 "https://openzfs.github.io/openzfs-docs/msg/"
5173 "ZFS-8000-ER."),
5174 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
5175 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5176 break;
5177 case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
5178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5179 "IV set guid mismatch. See the 'zfs receive' "
5180 "man page section\n discussing the limitations "
5181 "of raw encrypted send streams."));
5182 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5183 break;
5184 case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING:
5185 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5186 "Spill block flag missing for raw send.\n"
5187 "The zfs software on the sending system must "
5188 "be updated."));
5189 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5190 break;
5191 case ZFS_ERR_RESUME_EXISTS:
5192 cp = strchr(destsnap, '@');
5193 if (newfs) {
5194 /* it's the containing fs that exists */
5195 *cp = '\0';
5197 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5198 "Resuming recv on existing dataset without force"));
5199 (void) zfs_error_fmt(hdl, EZFS_RESUME_EXISTS,
5200 dgettext(TEXT_DOMAIN, "cannot resume recv %s"),
5201 destsnap);
5202 *cp = '@';
5203 break;
5204 case E2BIG:
5205 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5206 "zfs receive required kernel memory allocation "
5207 "larger than the system can support. Please file "
5208 "an issue at the OpenZFS issue tracker:\n"
5209 "https://github.com/openzfs/zfs/issues/new"));
5210 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5211 break;
5212 case EBUSY:
5213 if (hastoken) {
5214 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5215 "destination %s contains "
5216 "partially-complete state from "
5217 "\"zfs receive -s\"."), name);
5218 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
5219 break;
5221 zfs_fallthrough;
5222 default:
5223 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
5228 * Mount the target filesystem (if created). Also mount any
5229 * children of the target filesystem if we did a replication
5230 * receive (indicated by stream_avl being non-NULL).
5232 if (clp) {
5233 if (!flags->nomount)
5234 err |= changelist_postfix(clp);
5235 changelist_free(clp);
5238 if ((newfs || stream_avl) && type == ZFS_TYPE_FILESYSTEM && !redacted)
5239 flags->domount = B_TRUE;
5241 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
5242 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
5243 "failed to clear unreceived properties on %s"), name);
5244 (void) fprintf(stderr, "\n");
5246 if (prop_errflags & ZPROP_ERR_NORESTORE) {
5247 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
5248 "failed to restore original properties on %s"), name);
5249 (void) fprintf(stderr, "\n");
5252 if (err || ioctl_err) {
5253 err = -1;
5254 goto out;
5257 if (flags->verbose) {
5258 char buf1[64];
5259 char buf2[64];
5260 uint64_t bytes = read_bytes;
5261 struct timespec delta;
5262 clock_gettime(CLOCK_MONOTONIC_RAW, &delta);
5263 if (begin_time.tv_nsec > delta.tv_nsec) {
5264 delta.tv_nsec =
5265 1000000000 + delta.tv_nsec - begin_time.tv_nsec;
5266 delta.tv_sec -= 1;
5267 } else
5268 delta.tv_nsec -= begin_time.tv_nsec;
5269 delta.tv_sec -= begin_time.tv_sec;
5270 if (delta.tv_sec == 0 && delta.tv_nsec == 0)
5271 delta.tv_nsec = 1;
5272 double delta_f = delta.tv_sec + (delta.tv_nsec / 1e9);
5273 zfs_nicebytes(bytes, buf1, sizeof (buf1));
5274 zfs_nicebytes(bytes / delta_f, buf2, sizeof (buf2));
5276 (void) printf("received %s stream in %.2f seconds (%s/sec)\n",
5277 buf1, delta_f, buf2);
5280 err = 0;
5281 out:
5282 if (prop_errors != NULL)
5283 fnvlist_free(prop_errors);
5285 if (tmp_keylocation[0] != '\0') {
5286 fnvlist_add_string(rcvprops,
5287 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation);
5290 if (newprops)
5291 fnvlist_free(rcvprops);
5293 fnvlist_free(oxprops);
5294 fnvlist_free(origprops);
5296 return (err);
5300 * Check properties we were asked to override (both -o|-x)
5302 static boolean_t
5303 zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
5304 const char *errbuf)
5306 nvpair_t *nvp = NULL;
5307 zfs_prop_t prop;
5308 const char *name;
5310 while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
5311 name = nvpair_name(nvp);
5312 prop = zfs_name_to_prop(name);
5314 if (prop == ZPROP_USERPROP) {
5315 if (!zfs_prop_user(name)) {
5316 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5317 "%s: invalid property '%s'"), errbuf, name);
5318 return (B_FALSE);
5320 continue;
5323 * "origin" is readonly but is used to receive datasets as
5324 * clones so we don't raise an error here
5326 if (prop == ZFS_PROP_ORIGIN)
5327 continue;
5329 /* encryption params have their own verification later */
5330 if (prop == ZFS_PROP_ENCRYPTION ||
5331 zfs_prop_encryption_key_param(prop))
5332 continue;
5335 * cannot override readonly, set-once and other specific
5336 * settable properties
5338 if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
5339 prop == ZFS_PROP_VOLSIZE) {
5340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5341 "%s: invalid property '%s'"), errbuf, name);
5342 return (B_FALSE);
5346 return (B_TRUE);
5349 static int
5350 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
5351 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
5352 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs,
5353 const char *finalsnap, nvlist_t *cmdprops)
5355 int err;
5356 dmu_replay_record_t drr, drr_noswap;
5357 struct drr_begin *drrb = &drr.drr_u.drr_begin;
5358 char errbuf[ERRBUFLEN];
5359 zio_cksum_t zcksum = { { 0 } };
5360 uint64_t featureflags;
5361 int hdrtype;
5363 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5364 "cannot receive"));
5366 /* check cmdline props, raise an error if they cannot be received */
5367 if (!zfs_receive_checkprops(hdl, cmdprops, errbuf))
5368 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
5370 if (flags->isprefix &&
5371 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
5372 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
5373 "(%s) does not exist"), tosnap);
5374 return (zfs_error(hdl, EZFS_NOENT, errbuf));
5376 if (originsnap &&
5377 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
5378 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
5379 "(%s) does not exist"), originsnap);
5380 return (zfs_error(hdl, EZFS_NOENT, errbuf));
5383 /* read in the BEGIN record */
5384 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
5385 &zcksum)))
5386 return (err);
5388 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
5389 /* It's the double end record at the end of a package */
5390 return (ENODATA);
5393 /* the kernel needs the non-byteswapped begin record */
5394 drr_noswap = drr;
5396 flags->byteswap = B_FALSE;
5397 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
5399 * We computed the checksum in the wrong byteorder in
5400 * recv_read() above; do it again correctly.
5402 memset(&zcksum, 0, sizeof (zio_cksum_t));
5403 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
5404 flags->byteswap = B_TRUE;
5406 drr.drr_type = BSWAP_32(drr.drr_type);
5407 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
5408 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
5409 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
5410 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
5411 drrb->drr_type = BSWAP_32(drrb->drr_type);
5412 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
5413 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
5414 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
5417 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
5418 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5419 "stream (bad magic number)"));
5420 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5423 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
5424 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
5426 if (!DMU_STREAM_SUPPORTED(featureflags) ||
5427 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
5429 * Let's be explicit about this one, since rather than
5430 * being a new feature we can't know, it's an old
5431 * feature we dropped.
5433 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
5434 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5435 "stream has deprecated feature: dedup, try "
5436 "'zstream redup [send in a file] | zfs recv "
5437 "[...]'"));
5438 } else {
5439 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5440 "stream has unsupported feature, feature flags = "
5441 "%llx (unknown flags = %llx)"),
5442 (u_longlong_t)featureflags,
5443 (u_longlong_t)((featureflags) &
5444 ~DMU_BACKUP_FEATURE_MASK));
5446 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5449 /* Holds feature is set once in the compound stream header. */
5450 if (featureflags & DMU_BACKUP_FEATURE_HOLDS)
5451 flags->holds = B_TRUE;
5453 if (strchr(drrb->drr_toname, '@') == NULL) {
5454 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5455 "stream (bad snapshot name)"));
5456 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5459 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
5460 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
5461 if (sendfs == NULL) {
5463 * We were not called from zfs_receive_package(). Get
5464 * the fs specified by 'zfs send'.
5466 char *cp;
5467 (void) strlcpy(nonpackage_sendfs,
5468 drr.drr_u.drr_begin.drr_toname,
5469 sizeof (nonpackage_sendfs));
5470 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
5471 *cp = '\0';
5472 sendfs = nonpackage_sendfs;
5473 VERIFY(finalsnap == NULL);
5475 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
5476 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
5477 finalsnap, cmdprops));
5478 } else {
5479 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
5480 DMU_COMPOUNDSTREAM);
5481 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
5482 &zcksum, top_zfs, cmdprops));
5487 * Restores a backup of tosnap from the file descriptor specified by infd.
5488 * Return 0 on total success, -2 if some things couldn't be
5489 * destroyed/renamed/promoted, -1 if some things couldn't be received.
5490 * (-1 will override -2, if -1 and the resumable flag was specified the
5491 * transfer can be resumed if the sending side supports it).
5494 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
5495 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
5497 char *top_zfs = NULL;
5498 int err;
5499 struct stat sb;
5500 char *originsnap = NULL;
5503 * The only way fstat can fail is if we do not have a valid file
5504 * descriptor.
5506 if (fstat(infd, &sb) == -1) {
5507 perror("fstat");
5508 return (-2);
5511 if (props) {
5512 err = nvlist_lookup_string(props, "origin", &originsnap);
5513 if (err && err != ENOENT)
5514 return (err);
5517 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
5518 stream_avl, &top_zfs, NULL, props);
5520 if (err == 0 && !flags->nomount && flags->domount && top_zfs) {
5521 zfs_handle_t *zhp = NULL;
5522 prop_changelist_t *clp = NULL;
5524 zhp = zfs_open(hdl, top_zfs,
5525 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5526 if (zhp == NULL) {
5527 err = -1;
5528 goto out;
5529 } else {
5530 if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
5531 zfs_close(zhp);
5532 goto out;
5535 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
5536 CL_GATHER_MOUNT_ALWAYS,
5537 flags->forceunmount ? MS_FORCE : 0);
5538 zfs_close(zhp);
5539 if (clp == NULL) {
5540 err = -1;
5541 goto out;
5544 /* mount and share received datasets */
5545 err = changelist_postfix(clp);
5546 changelist_free(clp);
5547 if (err != 0)
5548 err = -1;
5552 out:
5553 if (top_zfs)
5554 free(top_zfs);
5556 return (err);