4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
31 * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
44 #include <sys/mount.h>
45 #include <sys/mntent.h>
46 #include <sys/mnttab.h>
48 #include <sys/debug.h>
56 #include <libzfs_core.h>
59 #include "zfs_namecheck.h"
61 #include "zfs_fletcher.h"
62 #include "libzfs_impl.h"
64 #include <sys/zio_checksum.h>
65 #include <sys/dsl_crypt.h>
67 #include <sys/socket.h>
70 /* in libzfs_dataset.c */
71 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
73 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
74 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
75 uint64_t *, const char *, nvlist_t *);
76 static int guid_to_name(libzfs_handle_t *, const char *,
77 uint64_t, boolean_t, char *);
79 static const zio_cksum_t zero_cksum = { { 0 } };
81 typedef struct dedup_arg {
84 libzfs_handle_t *dedup_hdl;
87 typedef struct progress_arg {
90 boolean_t pa_parsable;
93 typedef struct dataref {
99 typedef struct dedup_entry {
100 struct dedup_entry *dde_next;
101 zio_cksum_t dde_chksum;
106 #define MAX_DDT_PHYSMEM_PERCENT 20
107 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
109 typedef struct dedup_table {
110 dedup_entry_t **dedup_hash_array;
111 umem_cache_t *ddecache;
112 uint64_t max_ddt_size; /* max dedup table size in bytes */
113 uint64_t cur_ddt_size; /* current dedup table size in bytes */
120 high_order_bit(uint64_t n)
124 for (count = 0; n != 0; count++)
130 ssread(void *buf, size_t len, FILE *stream)
134 if ((outlen = fread(buf, len, 1, stream)) == 0)
141 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
142 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
146 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
147 if (ddt->ddt_full == B_FALSE) {
148 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
149 "Dedup table full. Deduplication will continue "
150 "with existing table entries"));
151 ddt->ddt_full = B_TRUE;
156 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
158 assert(*ddepp == NULL);
159 dde->dde_next = NULL;
160 dde->dde_chksum = *cs;
161 dde->dde_prop = prop;
164 ddt->cur_ddt_size += sizeof (dedup_entry_t);
170 * Using the specified dedup table, do a lookup for an entry with
171 * the checksum cs. If found, return the block's reference info
172 * in *dr. Otherwise, insert a new entry in the dedup table, using
173 * the reference information specified by *dr.
175 * return value: true - entry was found
176 * false - entry was not found
179 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
180 uint64_t prop, dataref_t *dr)
183 dedup_entry_t **ddepp;
185 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
187 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
188 ddepp = &((*ddepp)->dde_next)) {
189 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
190 (*ddepp)->dde_prop == prop) {
191 *dr = (*ddepp)->dde_ref;
195 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
200 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
201 zio_cksum_t *zc, int outfd)
203 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
204 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
205 fletcher_4_incremental_native(drr,
206 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
207 if (drr->drr_type != DRR_BEGIN) {
208 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
209 drr_checksum.drr_checksum));
210 drr->drr_u.drr_checksum.drr_checksum = *zc;
212 fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
213 sizeof (zio_cksum_t), zc);
214 if (write(outfd, drr, sizeof (*drr)) == -1)
216 if (payload_len != 0) {
217 fletcher_4_incremental_native(payload, payload_len, zc);
218 if (write(outfd, payload, payload_len) == -1)
225 * This function is started in a separate thread when the dedup option
226 * has been requested. The main send thread determines the list of
227 * snapshots to be included in the send stream and makes the ioctl calls
228 * for each one. But instead of having the ioctl send the output to the
229 * the output fd specified by the caller of zfs_send()), the
230 * ioctl is told to direct the output to a pipe, which is read by the
231 * alternate thread running THIS function. This function does the
233 * 1. building a dedup table (the DDT)
234 * 2. doing checksums on each data block and inserting a record in the DDT
235 * 3. looking for matching checksums, and
236 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
237 * a duplicate block is found.
238 * The output of this function then goes to the output fd requested
239 * by the caller of zfs_send().
244 dedup_arg_t *dda = arg;
245 char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
246 dmu_replay_record_t thedrr = { 0 };
247 dmu_replay_record_t *drr = &thedrr;
251 zio_cksum_t stream_cksum;
255 ddt.max_ddt_size = SMALLEST_POSSIBLE_MAX_DDT_MB << 20;
257 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
259 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
260 SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
263 numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
266 * numbuckets must be a power of 2. Increase number to
267 * a power of 2 if necessary.
269 if (!ISP2(numbuckets))
270 numbuckets = 1ULL << high_order_bit(numbuckets);
272 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
273 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
274 NULL, NULL, NULL, NULL, NULL, 0);
275 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
276 ddt.numhashbits = high_order_bit(numbuckets) - 1;
277 ddt.ddt_full = B_FALSE;
279 outfd = dda->outputfd;
280 ofp = fdopen(dda->inputfd, "r");
281 while (ssread(drr, sizeof (*drr), ofp) != 0) {
284 * kernel filled in checksum, we are going to write same
285 * record, but need to regenerate checksum.
287 if (drr->drr_type != DRR_BEGIN) {
288 bzero(&drr->drr_u.drr_checksum.drr_checksum,
289 sizeof (drr->drr_u.drr_checksum.drr_checksum));
292 switch (drr->drr_type) {
295 struct drr_begin *drrb = &drr->drr_u.drr_begin;
298 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
300 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
302 /* set the DEDUP feature flag for this stream */
303 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
304 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
305 DMU_BACKUP_FEATURE_DEDUPPROPS);
306 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
308 if (drr->drr_payloadlen != 0) {
309 sz = drr->drr_payloadlen;
311 if (sz > SPA_MAXBLOCKSIZE) {
312 buf = zfs_realloc(dda->dedup_hdl, buf,
313 SPA_MAXBLOCKSIZE, sz);
315 (void) ssread(buf, sz, ofp);
319 if (dump_record(drr, buf, sz, &stream_cksum,
327 struct drr_end *drre = &drr->drr_u.drr_end;
328 /* use the recalculated checksum */
329 drre->drr_checksum = stream_cksum;
330 if (dump_record(drr, NULL, 0, &stream_cksum,
338 struct drr_object *drro = &drr->drr_u.drr_object;
339 if (drro->drr_bonuslen > 0) {
341 DRR_OBJECT_PAYLOAD_SIZE(drro), ofp);
343 if (dump_record(drr, buf, DRR_OBJECT_PAYLOAD_SIZE(drro),
344 &stream_cksum, outfd) != 0)
351 struct drr_spill *drrs = &drr->drr_u.drr_spill;
352 (void) ssread(buf, DRR_SPILL_PAYLOAD_SIZE(drrs), ofp);
353 if (dump_record(drr, buf, DRR_SPILL_PAYLOAD_SIZE(drrs),
354 &stream_cksum, outfd) != 0)
359 case DRR_FREEOBJECTS:
361 if (dump_record(drr, NULL, 0, &stream_cksum,
369 struct drr_write *drrw = &drr->drr_u.drr_write;
371 uint64_t payload_size;
373 payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
374 (void) ssread(buf, payload_size, ofp);
377 * Use the existing checksum if it's dedup-capable,
378 * else calculate a SHA256 checksum for it.
381 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
383 !DRR_IS_DEDUP_CAPABLE(drrw->drr_flags)) {
385 zio_cksum_t tmpsha256;
387 SHA2Init(SHA256, &ctx);
388 SHA2Update(&ctx, buf, payload_size);
389 SHA2Final(&tmpsha256, &ctx);
391 drrw->drr_key.ddk_cksum.zc_word[0] =
392 BE_64(tmpsha256.zc_word[0]);
393 drrw->drr_key.ddk_cksum.zc_word[1] =
394 BE_64(tmpsha256.zc_word[1]);
395 drrw->drr_key.ddk_cksum.zc_word[2] =
396 BE_64(tmpsha256.zc_word[2]);
397 drrw->drr_key.ddk_cksum.zc_word[3] =
398 BE_64(tmpsha256.zc_word[3]);
399 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
400 drrw->drr_flags |= DRR_CHECKSUM_DEDUP;
403 dataref.ref_guid = drrw->drr_toguid;
404 dataref.ref_object = drrw->drr_object;
405 dataref.ref_offset = drrw->drr_offset;
407 if (ddt_update(dda->dedup_hdl, &ddt,
408 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
410 dmu_replay_record_t wbr_drr = {0};
411 struct drr_write_byref *wbr_drrr =
412 &wbr_drr.drr_u.drr_write_byref;
414 /* block already present in stream */
415 wbr_drr.drr_type = DRR_WRITE_BYREF;
417 wbr_drrr->drr_object = drrw->drr_object;
418 wbr_drrr->drr_offset = drrw->drr_offset;
419 wbr_drrr->drr_length = drrw->drr_logical_size;
420 wbr_drrr->drr_toguid = drrw->drr_toguid;
421 wbr_drrr->drr_refguid = dataref.ref_guid;
422 wbr_drrr->drr_refobject =
424 wbr_drrr->drr_refoffset =
427 wbr_drrr->drr_checksumtype =
428 drrw->drr_checksumtype;
429 wbr_drrr->drr_flags = drrw->drr_flags;
430 wbr_drrr->drr_key.ddk_cksum =
431 drrw->drr_key.ddk_cksum;
432 wbr_drrr->drr_key.ddk_prop =
433 drrw->drr_key.ddk_prop;
435 if (dump_record(&wbr_drr, NULL, 0,
436 &stream_cksum, outfd) != 0)
439 /* block not previously seen */
440 if (dump_record(drr, buf, payload_size,
441 &stream_cksum, outfd) != 0)
447 case DRR_WRITE_EMBEDDED:
449 struct drr_write_embedded *drrwe =
450 &drr->drr_u.drr_write_embedded;
452 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
453 if (dump_record(drr, buf,
454 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
455 &stream_cksum, outfd) != 0)
462 if (dump_record(drr, NULL, 0, &stream_cksum,
468 case DRR_OBJECT_RANGE:
470 if (dump_record(drr, NULL, 0, &stream_cksum,
477 (void) fprintf(stderr, "INVALID record type 0x%x\n",
479 /* should never happen, so assert */
484 umem_cache_destroy(ddt.ddecache);
485 free(ddt.dedup_hash_array);
493 * Routines for dealing with the AVL tree of fs-nvlists
495 typedef struct fsavl_node {
503 fsavl_compare(const void *arg1, const void *arg2)
505 const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
506 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
508 return (AVL_CMP(fn1->fn_guid, fn2->fn_guid));
512 * Given the GUID of a snapshot, find its containing filesystem and
516 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
518 fsavl_node_t fn_find;
521 fn_find.fn_guid = snapguid;
523 fn = avl_find(avl, &fn_find, NULL);
526 *snapname = fn->fn_snapname;
527 return (fn->fn_nvfs);
533 fsavl_destroy(avl_tree_t *avl)
542 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
549 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
552 fsavl_create(nvlist_t *fss)
555 nvpair_t *fselem = NULL;
557 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
560 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
561 offsetof(fsavl_node_t, fn_node));
563 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
564 nvlist_t *nvfs, *snaps;
565 nvpair_t *snapelem = NULL;
567 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
568 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
571 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
575 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
576 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
577 fsavl_destroy(fsavl);
581 fn->fn_snapname = nvpair_name(snapelem);
585 * Note: if there are multiple snaps with the
586 * same GUID, we ignore all but one.
588 if (avl_find(fsavl, fn, NULL) == NULL)
599 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
601 typedef struct send_data {
603 * assigned inside every recursive call,
604 * restored from *_save on return:
606 * guid of fromsnap snapshot in parent dataset
607 * txg of fromsnap snapshot in current dataset
608 * txg of tosnap snapshot in current dataset
611 uint64_t parent_fromsnap_guid;
612 uint64_t fromsnap_txg;
615 /* the nvlists get accumulated during depth-first traversal */
616 nvlist_t *parent_snaps;
620 /* send-receive configuration, does not change during traversal */
622 const char *fromsnap;
632 * The header nvlist is of the following format:
635 * "fromsnap" -> string (if incremental)
639 * "name" -> string (full name; for debugging)
640 * "parentfromsnap" -> number (guid of fromsnap in parent)
642 * "props" -> { name -> value (only if set here) }
643 * "snaps" -> { name (lastname) -> number (guid) }
644 * "snapprops" -> { name (lastname) -> { name -> value } }
646 * "origin" -> number (guid) (if clone)
647 * "is_encroot" -> boolean
648 * "sent" -> boolean (not on-disk)
657 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
660 send_iterate_snap(zfs_handle_t *zhp, void *arg)
662 send_data_t *sd = arg;
663 uint64_t guid = zhp->zfs_dmustats.dds_guid;
664 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
667 boolean_t isfromsnap, istosnap, istosnapwithnofrom;
669 snapname = strrchr(zhp->zfs_name, '@')+1;
670 isfromsnap = (sd->fromsnap != NULL &&
671 strcmp(sd->fromsnap, snapname) == 0);
672 istosnap = (sd->tosnap != NULL && (strcmp(sd->tosnap, snapname) == 0));
673 istosnapwithnofrom = (istosnap && sd->fromsnap == NULL);
675 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
677 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
678 "skipping snapshot %s because it was created "
679 "after the destination snapshot (%s)\n"),
680 zhp->zfs_name, sd->tosnap);
686 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
688 * NB: if there is no fromsnap here (it's a newly created fs in
689 * an incremental replication), we will substitute the tosnap.
691 if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap)) {
692 sd->parent_fromsnap_guid = guid;
695 if (!sd->recursive) {
696 if (!sd->seenfrom && isfromsnap) {
697 sd->seenfrom = B_TRUE;
702 if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) {
711 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
712 send_iterate_prop(zhp, sd->backup, nv);
713 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
721 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
723 nvlist_t *props = NULL;
724 nvpair_t *elem = NULL;
727 props = zfs_get_recvd_props(zhp);
729 props = zhp->zfs_props;
731 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
732 char *propname = nvpair_name(elem);
733 zfs_prop_t prop = zfs_name_to_prop(propname);
736 if (!zfs_prop_user(propname)) {
738 * Realistically, this should never happen. However,
739 * we want the ability to add DSL properties without
740 * needing to make incompatible version changes. We
741 * need to ignore unknown properties to allow older
742 * software to still send datasets containing these
743 * properties, with the unknown properties elided.
745 if (prop == ZPROP_INVAL)
748 if (zfs_prop_readonly(prop))
752 verify(nvpair_value_nvlist(elem, &propnv) == 0);
753 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
754 prop == ZFS_PROP_REFQUOTA ||
755 prop == ZFS_PROP_REFRESERVATION) {
758 verify(nvlist_lookup_uint64(propnv,
759 ZPROP_VALUE, &value) == 0);
760 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
763 * May have no source before SPA_VERSION_RECVD_PROPS,
764 * but is still modifiable.
766 if (nvlist_lookup_string(propnv,
767 ZPROP_SOURCE, &source) == 0) {
768 if ((strcmp(source, zhp->zfs_name) != 0) &&
770 ZPROP_SOURCE_VAL_RECVD) != 0))
775 if (nvlist_lookup_string(propnv,
776 ZPROP_SOURCE, &source) != 0)
778 if ((strcmp(source, zhp->zfs_name) != 0) &&
779 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
783 if (zfs_prop_user(propname) ||
784 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
786 verify(nvlist_lookup_string(propnv,
787 ZPROP_VALUE, &value) == 0);
788 VERIFY(0 == nvlist_add_string(nv, propname, value));
791 verify(nvlist_lookup_uint64(propnv,
792 ZPROP_VALUE, &value) == 0);
793 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
799 * returns snapshot creation txg
800 * and returns 0 if the snapshot does not exist
803 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
805 char name[ZFS_MAX_DATASET_NAME_LEN];
808 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
811 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
812 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
813 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
815 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
824 * recursively generate nvlists describing datasets. See comment
825 * for the data structure send_data_t above for description of contents
829 send_iterate_fs(zfs_handle_t *zhp, void *arg)
831 send_data_t *sd = arg;
832 nvlist_t *nvfs = NULL, *nv = NULL;
834 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
835 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
836 uint64_t tosnap_txg_save = sd->tosnap_txg;
837 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
838 uint64_t guid = zhp->zfs_dmustats.dds_guid;
839 uint64_t fromsnap_txg, tosnap_txg;
842 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
843 if (fromsnap_txg != 0)
844 sd->fromsnap_txg = fromsnap_txg;
846 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
848 sd->tosnap_txg = tosnap_txg;
851 * on the send side, if the current dataset does not have tosnap,
852 * perform two additional checks:
854 * - skip sending the current dataset if it was created later than
856 * - return error if the current dataset was created earlier than
859 if (sd->tosnap != NULL && tosnap_txg == 0) {
860 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
862 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
863 "skipping dataset %s: snapshot %s does "
864 "not exist\n"), zhp->zfs_name, sd->tosnap);
867 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
868 "cannot send %s@%s%s: snapshot %s@%s does not "
869 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
870 dgettext(TEXT_DOMAIN, " recursively") : "",
871 zhp->zfs_name, sd->tosnap);
877 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
878 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
879 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
880 sd->parent_fromsnap_guid));
882 if (zhp->zfs_dmustats.dds_origin[0]) {
883 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
884 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
885 if (origin == NULL) {
889 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
890 origin->zfs_dmustats.dds_guid));
895 /* iterate over props */
896 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
897 send_iterate_prop(zhp, sd->backup, nv);
899 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
902 /* determine if this dataset is an encryption root */
903 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
909 VERIFY(0 == nvlist_add_boolean(nvfs, "is_encroot"));
912 * Encrypted datasets can only be sent with properties if
913 * the raw flag is specified because the receive side doesn't
914 * currently have a mechanism for recursively asking the user
915 * for new encryption parameters.
918 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
919 "cannot send %s@%s: encrypted dataset %s may not "
920 "be sent with properties without the raw flag\n"),
921 sd->fsname, sd->tosnap, zhp->zfs_name);
928 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
930 /* iterate over snaps, and set sd->parent_fromsnap_guid */
931 sd->parent_fromsnap_guid = 0;
932 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
933 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
934 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd);
935 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
936 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
937 nvlist_free(sd->parent_snaps);
938 nvlist_free(sd->snapprops);
940 /* add this fs to nvlist */
941 (void) snprintf(guidstring, sizeof (guidstring),
942 "0x%llx", (longlong_t)guid);
943 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
945 /* iterate over children */
947 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
950 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
951 sd->fromsnap_txg = fromsnap_txg_save;
952 sd->tosnap_txg = tosnap_txg_save;
961 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
962 const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t verbose,
963 boolean_t backup, nvlist_t **nvlp, avl_tree_t **avlp)
966 send_data_t sd = { 0 };
969 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
971 return (EZFS_BADTYPE);
973 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
975 sd.fromsnap = fromsnap;
977 sd.recursive = recursive;
979 sd.verbose = verbose;
982 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
990 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
1001 * Routines specific to "zfs send"
1003 typedef struct send_dump_data {
1004 /* these are all just the short snapname (the part after the @) */
1005 const char *fromsnap;
1007 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
1008 uint64_t prevsnap_obj;
1009 boolean_t seenfrom, seento, replicate, doall, fromorigin;
1010 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
1011 boolean_t large_block, compress, raw;
1015 nvlist_t *snapholds;
1017 snapfilter_cb_t *filter_cb;
1018 void *filter_cb_arg;
1020 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
1026 zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
1027 enum lzc_send_flags flags, uint64_t *spacep)
1029 libzfs_handle_t *hdl = zhp->zfs_hdl;
1032 assert(snapname != NULL);
1033 error = lzc_send_space(snapname, from, flags, spacep);
1037 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1038 "warning: cannot estimate space for '%s'"), snapname);
1042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1043 "not an earlier snapshot from the same fs"));
1044 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1047 if (zfs_dataset_exists(hdl, snapname,
1048 ZFS_TYPE_SNAPSHOT)) {
1049 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1050 "incremental source (%s) does not exist"),
1053 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1067 zfs_error_aux(hdl, strerror(error));
1068 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1071 return (zfs_standard_error(hdl, error, errbuf));
1079 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1080 * NULL) to the file descriptor specified by outfd.
1083 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
1084 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
1087 zfs_cmd_t zc = {"\0"};
1088 libzfs_handle_t *hdl = zhp->zfs_hdl;
1091 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1092 assert(fromsnap_obj == 0 || !fromorigin);
1094 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1095 zc.zc_cookie = outfd;
1096 zc.zc_obj = fromorigin;
1097 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1098 zc.zc_fromobj = fromsnap_obj;
1099 zc.zc_flags = flags;
1101 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
1102 if (fromsnap && fromsnap[0] != '\0') {
1103 VERIFY(0 == nvlist_add_string(thisdbg,
1104 "fromsnap", fromsnap));
1107 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1109 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1110 "warning: cannot send '%s'"), zhp->zfs_name);
1112 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
1114 VERIFY(0 == nvlist_add_nvlist(debugnv,
1115 zhp->zfs_name, thisdbg));
1117 nvlist_free(thisdbg);
1121 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1122 "not an earlier snapshot from the same fs"));
1123 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1126 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1127 "source key must be loaded"));
1128 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1131 if (zfs_dataset_exists(hdl, zc.zc_name,
1132 ZFS_TYPE_SNAPSHOT)) {
1133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1134 "incremental source (@%s) does not exist"),
1137 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1150 zfs_error_aux(hdl, strerror(errno));
1151 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1154 return (zfs_standard_error(hdl, errno, errbuf));
1159 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1160 nvlist_free(thisdbg);
1166 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1168 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1171 * zfs_send() only sets snapholds for sends that need them,
1172 * e.g. replication and doall.
1174 if (sdd->snapholds == NULL)
1177 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1181 send_progress_thread(void *arg)
1183 progress_arg_t *pa = arg;
1184 zfs_cmd_t zc = {"\0"};
1185 zfs_handle_t *zhp = pa->pa_zhp;
1186 libzfs_handle_t *hdl = zhp->zfs_hdl;
1187 unsigned long long bytes;
1192 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1194 if (!pa->pa_parsable)
1195 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1198 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1203 zc.zc_cookie = pa->pa_fd;
1204 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1205 return ((void *)-1);
1209 bytes = zc.zc_cookie;
1211 if (pa->pa_parsable) {
1212 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1213 tm->tm_hour, tm->tm_min, tm->tm_sec,
1214 bytes, zhp->zfs_name);
1216 zfs_nicebytes(bytes, buf, sizeof (buf));
1217 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1218 tm->tm_hour, tm->tm_min, tm->tm_sec,
1219 buf, zhp->zfs_name);
1225 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1226 uint64_t size, boolean_t parsable)
1229 if (fromsnap != NULL) {
1230 (void) fprintf(fout, "incremental\t%s\t%s",
1233 (void) fprintf(fout, "full\t%s",
1237 if (fromsnap != NULL) {
1238 if (strchr(fromsnap, '@') == NULL &&
1239 strchr(fromsnap, '#') == NULL) {
1240 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1241 "send from @%s to %s"),
1244 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1245 "send from %s to %s"),
1249 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1256 (void) fprintf(fout, "\t%llu",
1258 } else if (size != 0) {
1260 zfs_nicebytes(size, buf, sizeof (buf));
1261 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1262 " estimated size is %s"), buf);
1264 (void) fprintf(fout, "\n");
1268 dump_snapshot(zfs_handle_t *zhp, void *arg)
1270 send_dump_data_t *sdd = arg;
1271 progress_arg_t pa = { 0 };
1274 enum lzc_send_flags flags = 0;
1276 boolean_t isfromsnap, istosnap, fromorigin;
1277 boolean_t exclude = B_FALSE;
1278 FILE *fout = sdd->std_out ? stdout : stderr;
1281 thissnap = strchr(zhp->zfs_name, '@') + 1;
1282 isfromsnap = (sdd->fromsnap != NULL &&
1283 strcmp(sdd->fromsnap, thissnap) == 0);
1285 if (!sdd->seenfrom && isfromsnap) {
1286 gather_holds(zhp, sdd);
1287 sdd->seenfrom = B_TRUE;
1288 (void) strlcpy(sdd->prevsnap, thissnap,
1289 sizeof (sdd->prevsnap));
1290 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1295 if (sdd->seento || !sdd->seenfrom) {
1300 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1302 sdd->seento = B_TRUE;
1304 if (sdd->large_block)
1305 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1306 if (sdd->embed_data)
1307 flags |= LZC_SEND_FLAG_EMBED_DATA;
1309 flags |= LZC_SEND_FLAG_COMPRESS;
1311 flags |= LZC_SEND_FLAG_RAW;
1313 if (!sdd->doall && !isfromsnap && !istosnap) {
1314 if (sdd->replicate) {
1316 nvlist_t *snapprops;
1318 * Filter out all intermediate snapshots except origin
1319 * snapshots needed to replicate clones.
1321 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1322 zhp->zfs_dmustats.dds_guid, &snapname);
1324 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1325 "snapprops", &snapprops));
1326 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1327 thissnap, &snapprops));
1328 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1335 * If a filter function exists, call it to determine whether
1336 * this snapshot will be sent.
1338 if (exclude || (sdd->filter_cb != NULL &&
1339 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1341 * This snapshot is filtered out. Don't send it, and don't
1342 * set prevsnap_obj, so it will be as if this snapshot didn't
1343 * exist, and the next accepted snapshot will be sent as
1344 * an incremental from the last accepted one, or as the
1345 * first (and full) snapshot in the case of a replication,
1346 * non-incremental send.
1352 gather_holds(zhp, sdd);
1353 fromorigin = sdd->prevsnap[0] == '\0' &&
1354 (sdd->fromorigin || sdd->replicate);
1358 char fromds[ZFS_MAX_DATASET_NAME_LEN];
1360 if (sdd->prevsnap[0] != '\0') {
1361 (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1362 *(strchr(fromds, '@') + 1) = '\0';
1363 (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1365 if (zfs_send_space(zhp, zhp->zfs_name,
1366 sdd->prevsnap[0] ? fromds : NULL, flags, &size) != 0) {
1367 size = 0; /* cannot estimate send space */
1369 send_print_verbose(fout, zhp->zfs_name,
1370 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1371 size, sdd->parsable);
1378 * If progress reporting is requested, spawn a new thread to
1379 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1381 if (sdd->progress) {
1383 pa.pa_fd = sdd->outfd;
1384 pa.pa_parsable = sdd->parsable;
1386 if ((err = pthread_create(&tid, NULL,
1387 send_progress_thread, &pa)) != 0) {
1393 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1394 fromorigin, sdd->outfd, flags, sdd->debugnv);
1396 if (sdd->progress) {
1397 (void) pthread_cancel(tid);
1398 (void) pthread_join(tid, NULL);
1402 (void) strcpy(sdd->prevsnap, thissnap);
1403 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1409 dump_filesystem(zfs_handle_t *zhp, void *arg)
1412 send_dump_data_t *sdd = arg;
1413 boolean_t missingfrom = B_FALSE;
1414 zfs_cmd_t zc = {"\0"};
1416 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1417 zhp->zfs_name, sdd->tosnap);
1418 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1419 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1420 "WARNING: could not send %s@%s: does not exist\n"),
1421 zhp->zfs_name, sdd->tosnap);
1426 if (sdd->replicate && sdd->fromsnap) {
1428 * If this fs does not have fromsnap, and we're doing
1429 * recursive, we need to send a full stream from the
1430 * beginning (or an incremental from the origin if this
1431 * is a clone). If we're doing non-recursive, then let
1432 * them get the error.
1434 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1435 zhp->zfs_name, sdd->fromsnap);
1436 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1437 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1438 missingfrom = B_TRUE;
1442 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1443 sdd->prevsnap_obj = 0;
1444 if (sdd->fromsnap == NULL || missingfrom)
1445 sdd->seenfrom = B_TRUE;
1447 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1448 if (!sdd->seenfrom) {
1449 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1450 "WARNING: could not send %s@%s:\n"
1451 "incremental source (%s@%s) does not exist\n"),
1452 zhp->zfs_name, sdd->tosnap,
1453 zhp->zfs_name, sdd->fromsnap);
1455 } else if (!sdd->seento) {
1456 if (sdd->fromsnap) {
1457 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1458 "WARNING: could not send %s@%s:\n"
1459 "incremental source (%s@%s) "
1460 "is not earlier than it\n"),
1461 zhp->zfs_name, sdd->tosnap,
1462 zhp->zfs_name, sdd->fromsnap);
1464 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1466 "could not send %s@%s: does not exist\n"),
1467 zhp->zfs_name, sdd->tosnap);
1476 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1478 send_dump_data_t *sdd = arg;
1480 boolean_t needagain, progress;
1482 if (!sdd->replicate)
1483 return (dump_filesystem(rzhp, sdd));
1485 /* Mark the clone origin snapshots. */
1486 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1487 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1489 uint64_t origin_guid = 0;
1491 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1492 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1493 if (origin_guid != 0) {
1495 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1496 origin_guid, &snapname);
1497 if (origin_nv != NULL) {
1498 nvlist_t *snapprops;
1499 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1500 "snapprops", &snapprops));
1501 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1502 snapname, &snapprops));
1503 VERIFY(0 == nvlist_add_boolean(
1504 snapprops, "is_clone_origin"));
1509 needagain = progress = B_FALSE;
1510 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1511 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1512 nvlist_t *fslist, *parent_nv;
1516 uint64_t origin_guid = 0;
1517 uint64_t parent_guid = 0;
1519 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1520 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1523 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1524 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1525 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1528 if (parent_guid != 0) {
1529 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1530 if (!nvlist_exists(parent_nv, "sent")) {
1531 /* parent has not been sent; skip this one */
1537 if (origin_guid != 0) {
1538 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1540 if (origin_nv != NULL &&
1541 !nvlist_exists(origin_nv, "sent")) {
1543 * origin has not been sent yet;
1551 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1554 err = dump_filesystem(zhp, sdd);
1555 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1566 /* clean out the sent flags in case we reuse this fss */
1567 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1568 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1571 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1572 (void) nvlist_remove_all(fslist, "sent");
1579 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1581 unsigned int version;
1583 unsigned long long checksum, packed_len;
1586 * Decode token header, which is:
1587 * <token version>-<checksum of payload>-<uncompressed payload length>
1588 * Note that the only supported token version is 1.
1590 nread = sscanf(token, "%u-%llx-%llx-",
1591 &version, &checksum, &packed_len);
1593 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1594 "resume token is corrupt (invalid format)"));
1598 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1599 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1600 "resume token is corrupt (invalid version %u)"),
1605 /* convert hexadecimal representation to binary */
1606 token = strrchr(token, '-') + 1;
1607 int len = strlen(token) / 2;
1608 unsigned char *compressed = zfs_alloc(hdl, len);
1609 for (i = 0; i < len; i++) {
1610 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1613 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1614 "resume token is corrupt "
1615 "(payload is not hex-encoded)"));
1620 /* verify checksum */
1622 fletcher_4_native_varsize(compressed, len, &cksum);
1623 if (cksum.zc_word[0] != checksum) {
1625 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1626 "resume token is corrupt (incorrect checksum)"));
1631 void *packed = zfs_alloc(hdl, packed_len);
1632 uLongf packed_len_long = packed_len;
1633 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1634 packed_len_long != packed_len) {
1637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1638 "resume token is corrupt (decompression failed)"));
1644 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1648 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1649 "resume token is corrupt (nvlist_unpack failed)"));
1656 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1657 const char *resume_token)
1661 char *fromname = NULL;
1662 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1665 char name[ZFS_MAX_DATASET_NAME_LEN];
1666 enum lzc_send_flags lzc_flags = 0;
1667 FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr;
1669 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1670 "cannot resume send"));
1672 nvlist_t *resume_nvl =
1673 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1674 if (resume_nvl == NULL) {
1676 * zfs_error_aux has already been set by
1677 * zfs_send_resume_token_to_nvlist
1679 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1681 if (flags->verbose) {
1682 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1683 "resume token contents:\n"));
1684 nvlist_print(fout, resume_nvl);
1687 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1688 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1689 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1690 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1691 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1692 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1693 "resume token is corrupt"));
1694 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1697 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1699 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1700 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1701 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1702 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1703 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1704 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1705 if (flags->raw || nvlist_exists(resume_nvl, "rawok"))
1706 lzc_flags |= LZC_SEND_FLAG_RAW;
1708 if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1709 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1710 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1711 "'%s' is no longer the same snapshot used in "
1712 "the initial send"), toname);
1714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1715 "'%s' used in the initial send no longer exists"),
1718 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1720 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1722 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1723 "unable to access '%s'"), name);
1724 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1727 if (fromguid != 0) {
1728 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1730 "incremental source %#llx no longer exists"),
1731 (longlong_t)fromguid);
1732 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1737 if (flags->verbose) {
1739 error = lzc_send_space(zhp->zfs_name, fromname,
1742 size = MAX(0, (int64_t)(size - bytes));
1743 send_print_verbose(fout, zhp->zfs_name, fromname,
1744 size, flags->parsable);
1747 if (!flags->dryrun) {
1748 progress_arg_t pa = { 0 };
1751 * If progress reporting is requested, spawn a new thread to
1752 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1754 if (flags->progress) {
1757 pa.pa_parsable = flags->parsable;
1759 error = pthread_create(&tid, NULL,
1760 send_progress_thread, &pa);
1767 error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1768 lzc_flags, resumeobj, resumeoff);
1770 if (flags->progress) {
1771 (void) pthread_cancel(tid);
1772 (void) pthread_join(tid, NULL);
1776 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1777 "warning: cannot send '%s'"), zhp->zfs_name);
1785 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1786 "source key must be loaded"));
1787 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1802 zfs_error_aux(hdl, strerror(errno));
1803 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1806 return (zfs_standard_error(hdl, errno, errbuf));
1817 * Generate a send stream for the dataset identified by the argument zhp.
1819 * The content of the send stream is the snapshot identified by
1820 * 'tosnap'. Incremental streams are requested in two ways:
1821 * - from the snapshot identified by "fromsnap" (if non-null) or
1822 * - from the origin of the dataset identified by zhp, which must
1823 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1826 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1827 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1828 * if "replicate" is set. If "doall" is set, dump all the intermediate
1829 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1830 * case too. If "props" is set, send properties.
1833 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1834 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1835 void *cb_arg, nvlist_t **debugnvp)
1838 send_dump_data_t sdd = { 0 };
1840 nvlist_t *fss = NULL;
1841 avl_tree_t *fsavl = NULL;
1842 static uint64_t holdseq;
1846 dedup_arg_t dda = { 0 };
1847 int featureflags = 0;
1850 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1851 "cannot send '%s'"), zhp->zfs_name);
1853 if (fromsnap && fromsnap[0] == '\0') {
1854 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1855 "zero-length incremental source"));
1856 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1859 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1861 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1862 if (version >= ZPL_VERSION_SA) {
1863 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1868 * Start the dedup thread if this is a dedup stream. We do not bother
1869 * doing this if this a raw send of an encrypted dataset with dedup off
1870 * because normal encrypted blocks won't dedup.
1872 if (flags->dedup && !flags->dryrun && !(flags->raw &&
1873 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
1874 zfs_prop_get_int(zhp, ZFS_PROP_DEDUP) == ZIO_CHECKSUM_OFF)) {
1875 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1876 DMU_BACKUP_FEATURE_DEDUPPROPS);
1877 if ((err = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd)) != 0) {
1878 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1879 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1882 dda.outputfd = outfd;
1883 dda.inputfd = pipefd[1];
1884 dda.dedup_hdl = zhp->zfs_hdl;
1885 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1886 (void) close(pipefd[0]);
1887 (void) close(pipefd[1]);
1888 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1889 return (zfs_error(zhp->zfs_hdl,
1890 EZFS_THREADCREATEFAILED, errbuf));
1894 if (flags->replicate || flags->doall || flags->props || flags->backup) {
1895 dmu_replay_record_t drr = { 0 };
1896 char *packbuf = NULL;
1900 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
1902 if (flags->replicate || flags->props || flags->backup) {
1905 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1907 VERIFY(0 == nvlist_add_string(hdrnv,
1908 "fromsnap", fromsnap));
1910 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1911 if (!flags->replicate) {
1912 VERIFY(0 == nvlist_add_boolean(hdrnv,
1916 VERIFY(0 == nvlist_add_boolean(hdrnv, "raw"));
1919 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1920 fromsnap, tosnap, flags->replicate, flags->raw,
1921 flags->verbose, flags->backup, &fss, &fsavl);
1924 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1925 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1935 if (!flags->dryrun) {
1936 /* write first begin record */
1937 drr.drr_type = DRR_BEGIN;
1938 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1939 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1940 drr_versioninfo, DMU_COMPOUNDSTREAM);
1941 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1942 drr_versioninfo, featureflags);
1943 if (snprintf(drr.drr_u.drr_begin.drr_toname,
1944 sizeof (drr.drr_u.drr_begin.drr_toname),
1945 "%s@%s", zhp->zfs_name, tosnap) >=
1946 sizeof (drr.drr_u.drr_begin.drr_toname)) {
1950 drr.drr_payloadlen = buflen;
1952 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1957 /* write end record */
1958 bzero(&drr, sizeof (drr));
1959 drr.drr_type = DRR_END;
1960 drr.drr_u.drr_end.drr_checksum = zc;
1961 err = write(outfd, &drr, sizeof (drr));
1971 /* dump each stream */
1972 sdd.fromsnap = fromsnap;
1973 sdd.tosnap = tosnap;
1975 sdd.outfd = pipefd[0];
1978 sdd.replicate = flags->replicate;
1979 sdd.doall = flags->doall;
1980 sdd.fromorigin = flags->fromorigin;
1983 sdd.verbose = flags->verbose;
1984 sdd.parsable = flags->parsable;
1985 sdd.progress = flags->progress;
1986 sdd.dryrun = flags->dryrun;
1987 sdd.large_block = flags->largeblock;
1988 sdd.embed_data = flags->embed_data;
1989 sdd.compress = flags->compress;
1990 sdd.raw = flags->raw;
1991 sdd.filter_cb = filter_func;
1992 sdd.filter_cb_arg = cb_arg;
1994 sdd.debugnv = *debugnvp;
1995 if (sdd.verbose && sdd.dryrun)
1996 sdd.std_out = B_TRUE;
1997 fout = sdd.std_out ? stdout : stderr;
2000 * Some flags require that we place user holds on the datasets that are
2001 * being sent so they don't get destroyed during the send. We can skip
2002 * this step if the pool is imported read-only since the datasets cannot
2005 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2006 ZPOOL_PROP_READONLY, NULL) &&
2007 zfs_spa_version(zhp, &spa_version) == 0 &&
2008 spa_version >= SPA_VERSION_USERREFS &&
2009 (flags->doall || flags->replicate)) {
2011 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2012 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2013 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
2014 if (sdd.cleanup_fd < 0) {
2018 sdd.snapholds = fnvlist_alloc();
2020 sdd.cleanup_fd = -1;
2021 sdd.snapholds = NULL;
2023 if (flags->verbose || sdd.snapholds != NULL) {
2025 * Do a verbose no-op dry run to get all the verbose output
2026 * or to gather snapshot hold's before generating any data,
2027 * then do a non-verbose real run to generate the streams.
2029 sdd.dryrun = B_TRUE;
2030 err = dump_filesystems(zhp, &sdd);
2035 if (flags->verbose) {
2036 if (flags->parsable) {
2037 (void) fprintf(fout, "size\t%llu\n",
2038 (longlong_t)sdd.size);
2041 zfs_nicebytes(sdd.size, buf, sizeof (buf));
2042 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
2043 "total estimated size is %s\n"), buf);
2047 /* Ensure no snaps found is treated as an error. */
2053 /* Skip the second run if dryrun was requested. */
2057 if (sdd.snapholds != NULL) {
2058 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2062 fnvlist_free(sdd.snapholds);
2063 sdd.snapholds = NULL;
2066 sdd.dryrun = B_FALSE;
2067 sdd.verbose = B_FALSE;
2070 err = dump_filesystems(zhp, &sdd);
2071 fsavl_destroy(fsavl);
2074 /* Ensure no snaps found is treated as an error. */
2075 if (err == 0 && !sdd.seento)
2080 (void) pthread_cancel(tid);
2081 (void) close(pipefd[0]);
2082 (void) pthread_join(tid, NULL);
2085 if (sdd.cleanup_fd != -1) {
2086 VERIFY(0 == close(sdd.cleanup_fd));
2087 sdd.cleanup_fd = -1;
2090 if (!flags->dryrun && (flags->replicate || flags->doall ||
2091 flags->props || flags->backup)) {
2093 * write final end record. NB: want to do this even if
2094 * there was some error, because it might not be totally
2097 dmu_replay_record_t drr = { 0 };
2098 drr.drr_type = DRR_END;
2099 if (write(outfd, &drr, sizeof (drr)) == -1) {
2100 return (zfs_standard_error(zhp->zfs_hdl,
2105 return (err || sdd.err);
2108 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2110 fsavl_destroy(fsavl);
2112 fnvlist_free(sdd.snapholds);
2114 if (sdd.cleanup_fd != -1)
2115 VERIFY(0 == close(sdd.cleanup_fd));
2117 (void) pthread_cancel(tid);
2118 (void) close(pipefd[0]);
2119 (void) pthread_join(tid, NULL);
2125 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t flags)
2128 libzfs_handle_t *hdl = zhp->zfs_hdl;
2129 enum lzc_send_flags lzc_flags = 0;
2130 FILE *fout = (flags.verbose && flags.dryrun) ? stdout : stderr;
2133 if (flags.largeblock)
2134 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
2135 if (flags.embed_data)
2136 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
2138 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
2140 lzc_flags |= LZC_SEND_FLAG_RAW;
2142 if (flags.verbose) {
2144 err = lzc_send_space(zhp->zfs_name, from, lzc_flags, &size);
2146 send_print_verbose(fout, zhp->zfs_name, from, size,
2149 (void) fprintf(stderr, "Cannot estimate send size: "
2150 "%s\n", strerror(errno));
2157 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2158 "warning: cannot send '%s'"), zhp->zfs_name);
2160 err = lzc_send(zhp->zfs_name, from, fd, lzc_flags);
2164 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2165 "not an earlier snapshot from the same fs"));
2166 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2170 if (lzc_exists(zhp->zfs_name)) {
2171 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2172 "incremental source (%s) does not exist"),
2175 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2179 "dataset key must be loaded"));
2180 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2184 "target is busy; if a filesystem, "
2185 "it must not be mounted"));
2186 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2199 zfs_error_aux(hdl, strerror(errno));
2200 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2203 return (zfs_standard_error(hdl, errno, errbuf));
2210 * Routines specific to "zfs recv"
2214 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2215 boolean_t byteswap, zio_cksum_t *zc)
2221 assert(ilen <= SPA_MAXBLOCKSIZE);
2224 rv = read(fd, cp, len);
2229 if (rv < 0 || len != 0) {
2230 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2231 "failed to read from stream"));
2232 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2233 "cannot receive")));
2238 fletcher_4_incremental_byteswap(buf, ilen, zc);
2240 fletcher_4_incremental_native(buf, ilen, zc);
2246 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2247 boolean_t byteswap, zio_cksum_t *zc)
2252 buf = zfs_alloc(hdl, len);
2256 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2262 err = nvlist_unpack(buf, len, nvp, 0);
2265 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2266 "stream (malformed nvlist)"));
2273 * Returns the grand origin (origin of origin of origin...) of a given handle.
2274 * If this dataset is not a clone, it simply returns a copy of the original
2277 static zfs_handle_t *
2278 recv_open_grand_origin(zfs_handle_t *zhp)
2280 char origin[ZFS_MAX_DATASET_NAME_LEN];
2282 zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2284 while (ozhp != NULL) {
2285 if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2286 sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2289 (void) zfs_close(ozhp);
2290 ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2297 recv_rename_impl(zfs_handle_t *zhp, zfs_cmd_t *zc)
2300 zfs_handle_t *ozhp = NULL;
2303 * Attempt to rename the dataset. If it fails with EACCES we have
2304 * attempted to rename the dataset outside of its encryption root.
2305 * Force the dataset to become an encryption root and try again.
2307 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2308 if (err == EACCES) {
2309 ozhp = recv_open_grand_origin(zhp);
2315 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2320 err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2330 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2331 int baselen, char *newname, recvflags_t *flags)
2334 zfs_cmd_t zc = {"\0"};
2336 prop_changelist_t *clp = NULL;
2337 zfs_handle_t *zhp = NULL;
2339 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2344 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2345 flags->force ? MS_FORCE : 0);
2350 err = changelist_prefix(clp);
2354 zc.zc_objset_type = DMU_OST_ZFS;
2355 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2358 (void) strcpy(newname, tryname);
2360 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
2362 if (flags->verbose) {
2363 (void) printf("attempting rename %s to %s\n",
2364 zc.zc_name, zc.zc_value);
2366 err = recv_rename_impl(zhp, &zc);
2368 changelist_rename(clp, name, tryname);
2373 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2376 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2377 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2378 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
2380 if (flags->verbose) {
2381 (void) printf("failed - trying rename %s to %s\n",
2382 zc.zc_name, zc.zc_value);
2384 err = recv_rename_impl(zhp, &zc);
2386 changelist_rename(clp, name, newname);
2387 if (err && flags->verbose) {
2388 (void) printf("failed (%u) - "
2389 "will try again on next pass\n", errno);
2392 } else if (flags->verbose) {
2394 (void) printf("success\n");
2396 (void) printf("failed (%u)\n", errno);
2399 (void) changelist_postfix(clp);
2403 changelist_free(clp);
2411 recv_promote(libzfs_handle_t *hdl, const char *fsname,
2412 const char *origin_fsname, recvflags_t *flags)
2415 zfs_cmd_t zc = {"\0"};
2416 zfs_handle_t *zhp = NULL, *ozhp = NULL;
2419 (void) printf("promoting %s\n", fsname);
2421 (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
2422 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
2425 * Attempt to promote the dataset. If it fails with EACCES the
2426 * promotion would cause this dataset to leave its encryption root.
2427 * Force the origin to become an encryption root and try again.
2429 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2430 if (err == EACCES) {
2431 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2437 ozhp = recv_open_grand_origin(zhp);
2443 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2448 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2461 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2462 char *newname, recvflags_t *flags)
2464 zfs_cmd_t zc = {"\0"};
2466 prop_changelist_t *clp;
2468 boolean_t defer = B_FALSE;
2471 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2474 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2475 flags->force ? MS_FORCE : 0);
2476 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2477 zfs_spa_version(zhp, &spa_version) == 0 &&
2478 spa_version >= SPA_VERSION_USERREFS)
2483 err = changelist_prefix(clp);
2487 zc.zc_objset_type = DMU_OST_ZFS;
2488 zc.zc_defer_destroy = defer;
2489 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2492 (void) printf("attempting destroy %s\n", zc.zc_name);
2493 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
2496 (void) printf("success\n");
2497 changelist_remove(clp, zc.zc_name);
2500 (void) changelist_postfix(clp);
2501 changelist_free(clp);
2504 * Deferred destroy might destroy the snapshot or only mark it to be
2505 * destroyed later, and it returns success in either case.
2507 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2508 ZFS_TYPE_SNAPSHOT))) {
2509 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2515 typedef struct guid_to_name_data {
2517 boolean_t bookmark_ok;
2520 } guid_to_name_data_t;
2523 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2525 guid_to_name_data_t *gtnd = arg;
2529 if (gtnd->skip != NULL &&
2530 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2531 strcmp(slash + 1, gtnd->skip) == 0) {
2536 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2537 (void) strcpy(gtnd->name, zhp->zfs_name);
2542 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2543 if (err != EEXIST && gtnd->bookmark_ok)
2544 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2550 * Attempt to find the local dataset associated with this guid. In the case of
2551 * multiple matches, we attempt to find the "best" match by searching
2552 * progressively larger portions of the hierarchy. This allows one to send a
2553 * tree of datasets individually and guarantee that we will find the source
2554 * guid within that hierarchy, even if there are multiple matches elsewhere.
2557 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2558 boolean_t bookmark_ok, char *name)
2560 char pname[ZFS_MAX_DATASET_NAME_LEN];
2561 guid_to_name_data_t gtnd;
2564 gtnd.bookmark_ok = bookmark_ok;
2569 * Search progressively larger portions of the hierarchy, starting
2570 * with the filesystem specified by 'parent'. This will
2571 * select the "most local" version of the origin snapshot in the case
2572 * that there are multiple matching snapshots in the system.
2574 (void) strlcpy(pname, parent, sizeof (pname));
2575 char *cp = strrchr(pname, '@');
2577 cp = strchr(pname, '\0');
2578 for (; cp != NULL; cp = strrchr(pname, '/')) {
2579 /* Chop off the last component and open the parent */
2581 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2585 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
2587 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2588 if (err != EEXIST && bookmark_ok)
2589 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2595 * Remember the last portion of the dataset so we skip it next
2596 * time through (as we've already searched that portion of the
2599 gtnd.skip = strrchr(pname, '/') + 1;
2606 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2607 * guid1 is after guid2.
2610 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2611 uint64_t guid1, uint64_t guid2)
2614 char *fsname = NULL, *snapname = NULL;
2615 char buf[ZFS_MAX_DATASET_NAME_LEN];
2617 zfs_handle_t *guid1hdl, *guid2hdl;
2618 uint64_t create1, create2;
2625 nvfs = fsavl_find(avl, guid1, &snapname);
2626 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2627 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2628 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2629 if (guid1hdl == NULL)
2632 nvfs = fsavl_find(avl, guid2, &snapname);
2633 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2634 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2635 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2636 if (guid2hdl == NULL) {
2637 zfs_close(guid1hdl);
2641 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2642 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2644 if (create1 < create2)
2646 else if (create1 > create2)
2651 zfs_close(guid1hdl);
2652 zfs_close(guid2hdl);
2658 * This function reestablishes the heirarchy of encryption roots after a
2659 * recursive incremental receive has completed. This must be done after the
2660 * second call to recv_incremental_replication() has renamed and promoted all
2661 * sent datasets to their final locations in the dataset heriarchy.
2664 recv_fix_encryption_heirarchy(libzfs_handle_t *hdl, const char *destname,
2665 nvlist_t *stream_nv, avl_tree_t *stream_avl)
2668 nvpair_t *fselem = NULL;
2669 nvlist_t *stream_fss;
2671 char top_zfs[ZFS_MAX_DATASET_NAME_LEN];
2673 (void) strcpy(top_zfs, destname);
2674 cp = strrchr(top_zfs, '@');
2678 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", &stream_fss));
2680 while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
2681 zfs_handle_t *zhp = NULL;
2683 nvlist_t *snaps, *props, *stream_nvfs = NULL;
2684 nvpair_t *snapel = NULL;
2685 boolean_t is_encroot, is_clone, stream_encroot;
2687 char *stream_keylocation = NULL;
2688 char keylocation[MAXNAMELEN];
2689 char fsname[ZFS_MAX_DATASET_NAME_LEN];
2691 keylocation[0] = '\0';
2692 VERIFY(0 == nvpair_value_nvlist(fselem, &stream_nvfs));
2693 VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "snaps", &snaps));
2694 VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "props", &props));
2695 stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
2697 /* find a snapshot from the stream that exists locally */
2699 while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
2702 VERIFY(0 == nvpair_value_uint64(snapel, &guid));
2703 err = guid_to_name(hdl, destname, guid, B_FALSE,
2712 cp = strchr(fsname, '@');
2716 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2722 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
2723 is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
2724 (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
2726 /* we don't need to do anything for unencrypted filesystems */
2727 if (crypt == ZIO_CRYPT_OFF) {
2733 * If the dataset is flagged as an encryption root, was not
2734 * received as a clone and is not currently an encryption root,
2735 * force it to become one. Fixup the keylocation if necessary.
2737 if (stream_encroot) {
2738 if (!is_clone && !is_encroot) {
2739 err = lzc_change_key(fsname,
2740 DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
2747 VERIFY(0 == nvlist_lookup_string(props,
2748 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2749 &stream_keylocation));
2752 * Refresh the properties in case the call to
2753 * lzc_change_key() changed the value.
2755 zfs_refresh_properties(zhp);
2756 err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
2757 keylocation, sizeof (keylocation), NULL, NULL,
2764 if (strcmp(keylocation, stream_keylocation) != 0) {
2765 err = zfs_prop_set(zhp,
2766 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2767 stream_keylocation);
2776 * If the dataset is not flagged as an encryption root and is
2777 * currently an encryption root, force it to inherit from its
2778 * parent. The root of a raw send should never be
2781 if (!stream_encroot && is_encroot &&
2782 strcmp(top_zfs, fsname) != 0) {
2783 err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
2801 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2802 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2805 nvlist_t *local_nv, *deleted = NULL;
2806 avl_tree_t *local_avl;
2807 nvpair_t *fselem, *nextfselem;
2809 char newname[ZFS_MAX_DATASET_NAME_LEN];
2812 boolean_t needagain, progress, recursive;
2815 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2817 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2824 needagain = progress = B_FALSE;
2826 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2828 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2829 recursive, B_TRUE, B_FALSE, B_FALSE, &local_nv, &local_avl)) != 0)
2833 * Process deletes and renames
2835 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2836 fselem; fselem = nextfselem) {
2837 nvlist_t *nvfs, *snaps;
2838 nvlist_t *stream_nvfs = NULL;
2839 nvpair_t *snapelem, *nextsnapelem;
2840 uint64_t fromguid = 0;
2841 uint64_t originguid = 0;
2842 uint64_t stream_originguid = 0;
2843 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2844 char *fsname, *stream_fsname;
2846 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2848 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2849 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2850 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2851 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2852 &parent_fromsnap_guid));
2853 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2856 * First find the stream's fs, so we can check for
2857 * a different origin (due to "zfs promote")
2859 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2860 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2863 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2864 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2866 if (stream_nvfs != NULL)
2870 /* check for promote */
2871 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2872 &stream_originguid);
2873 if (stream_nvfs && originguid != stream_originguid) {
2874 switch (created_before(hdl, local_avl,
2875 stream_originguid, originguid)) {
2878 nvlist_t *origin_nvfs;
2879 char *origin_fsname;
2881 origin_nvfs = fsavl_find(local_avl, originguid,
2883 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2884 "name", &origin_fsname));
2885 error = recv_promote(hdl, fsname, origin_fsname,
2894 fsavl_destroy(local_avl);
2895 nvlist_free(local_nv);
2899 * We had/have the wrong origin, therefore our
2900 * list of snapshots is wrong. Need to handle
2901 * them on the next pass.
2907 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2908 snapelem; snapelem = nextsnapelem) {
2910 char *stream_snapname;
2911 nvlist_t *found, *props;
2913 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2915 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2916 found = fsavl_find(stream_avl, thisguid,
2919 /* check for delete */
2920 if (found == NULL) {
2921 char name[ZFS_MAX_DATASET_NAME_LEN];
2926 (void) snprintf(name, sizeof (name), "%s@%s",
2927 fsname, nvpair_name(snapelem));
2929 error = recv_destroy(hdl, name,
2930 strlen(fsname)+1, newname, flags);
2935 sprintf(guidname, "%llu",
2936 (u_longlong_t)thisguid);
2937 nvlist_add_boolean(deleted, guidname);
2941 stream_nvfs = found;
2943 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2944 &props) && 0 == nvlist_lookup_nvlist(props,
2945 stream_snapname, &props)) {
2946 zfs_cmd_t zc = {"\0"};
2948 zc.zc_cookie = B_TRUE; /* received */
2949 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2950 "%s@%s", fsname, nvpair_name(snapelem));
2951 if (zcmd_write_src_nvlist(hdl, &zc,
2953 (void) zfs_ioctl(hdl,
2954 ZFS_IOC_SET_PROP, &zc);
2955 zcmd_free_nvlists(&zc);
2959 /* check for different snapname */
2960 if (strcmp(nvpair_name(snapelem),
2961 stream_snapname) != 0) {
2962 char name[ZFS_MAX_DATASET_NAME_LEN];
2963 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2965 (void) snprintf(name, sizeof (name), "%s@%s",
2966 fsname, nvpair_name(snapelem));
2967 (void) snprintf(tryname, sizeof (name), "%s@%s",
2968 fsname, stream_snapname);
2970 error = recv_rename(hdl, name, tryname,
2971 strlen(fsname)+1, newname, flags);
2978 if (strcmp(stream_snapname, fromsnap) == 0)
2979 fromguid = thisguid;
2982 /* check for delete */
2983 if (stream_nvfs == NULL) {
2987 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2993 sprintf(guidname, "%llu",
2994 (u_longlong_t)parent_fromsnap_guid);
2995 nvlist_add_boolean(deleted, guidname);
2999 if (fromguid == 0) {
3000 if (flags->verbose) {
3001 (void) printf("local fs %s does not have "
3002 "fromsnap (%s in stream); must have "
3003 "been deleted locally; ignoring\n",
3009 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
3010 "name", &stream_fsname));
3011 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
3012 "parentfromsnap", &stream_parent_fromsnap_guid));
3014 s1 = strrchr(fsname, '/');
3015 s2 = strrchr(stream_fsname, '/');
3018 * Check if we're going to rename based on parent guid change
3019 * and the current parent guid was also deleted. If it was then
3020 * rename will fail and is likely unneeded, so avoid this and
3021 * force an early retry to determine the new
3022 * parent_fromsnap_guid.
3024 if (stream_parent_fromsnap_guid != 0 &&
3025 parent_fromsnap_guid != 0 &&
3026 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
3027 sprintf(guidname, "%llu",
3028 (u_longlong_t)parent_fromsnap_guid);
3029 if (nvlist_exists(deleted, guidname)) {
3037 * Check for rename. If the exact receive path is specified, it
3038 * does not count as a rename, but we still need to check the
3039 * datasets beneath it.
3041 if ((stream_parent_fromsnap_guid != 0 &&
3042 parent_fromsnap_guid != 0 &&
3043 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
3044 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3045 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3047 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3049 parent = fsavl_find(local_avl,
3050 stream_parent_fromsnap_guid, NULL);
3052 * NB: parent might not be found if we used the
3053 * tosnap for stream_parent_fromsnap_guid,
3054 * because the parent is a newly-created fs;
3055 * we'll be able to rename it after we recv the
3058 if (parent != NULL) {
3061 VERIFY(0 == nvlist_lookup_string(parent, "name",
3063 (void) snprintf(tryname, sizeof (tryname),
3064 "%s%s", pname, strrchr(stream_fsname, '/'));
3067 if (flags->verbose) {
3068 (void) printf("local fs %s new parent "
3069 "not found\n", fsname);
3075 error = recv_rename(hdl, fsname, tryname,
3076 strlen(tofs)+1, newname, flags);
3078 if (renamed != NULL && newname[0] != '\0') {
3079 VERIFY(0 == nvlist_add_boolean(renamed,
3091 fsavl_destroy(local_avl);
3092 nvlist_free(local_nv);
3093 nvlist_free(deleted);
3095 if (needagain && progress) {
3096 /* do another pass to fix up temporary names */
3098 (void) printf("another pass:\n");
3102 return (needagain || error != 0);
3106 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
3107 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3108 char **top_zfs, int cleanup_fd, uint64_t *action_handlep,
3111 nvlist_t *stream_nv = NULL;
3112 avl_tree_t *stream_avl = NULL;
3113 char *fromsnap = NULL;
3114 char *sendsnap = NULL;
3116 char tofs[ZFS_MAX_DATASET_NAME_LEN];
3117 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
3119 dmu_replay_record_t drre;
3121 boolean_t anyerr = B_FALSE;
3122 boolean_t softerr = B_FALSE;
3123 boolean_t recursive, raw;
3125 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3128 assert(drr->drr_type == DRR_BEGIN);
3129 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
3130 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3131 DMU_COMPOUNDSTREAM);
3134 * Read in the nvlist from the stream.
3136 if (drr->drr_payloadlen != 0) {
3137 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
3138 &stream_nv, flags->byteswap, zc);
3140 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3145 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3147 raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3149 if (recursive && strchr(destname, '@')) {
3150 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3151 "cannot specify snapshot name for multi-snapshot stream"));
3152 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3157 * Read in the end record and verify checksum.
3159 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3160 flags->byteswap, NULL)))
3162 if (flags->byteswap) {
3163 drre.drr_type = BSWAP_32(drre.drr_type);
3164 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3165 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3166 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3167 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3168 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3169 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3170 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3171 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3173 if (drre.drr_type != DRR_END) {
3174 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3177 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3179 "incorrect header checksum"));
3180 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3184 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3186 if (drr->drr_payloadlen != 0) {
3187 nvlist_t *stream_fss;
3189 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
3191 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3192 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3193 "couldn't allocate avl tree"));
3194 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3198 if (fromsnap != NULL && recursive) {
3199 nvlist_t *renamed = NULL;
3200 nvpair_t *pair = NULL;
3202 (void) strlcpy(tofs, destname, sizeof (tofs));
3203 if (flags->isprefix) {
3204 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3207 if (flags->istail) {
3208 cp = strrchr(drrb->drr_toname, '/');
3210 (void) strlcat(tofs, "/",
3214 i = (cp - drrb->drr_toname);
3217 i = strcspn(drrb->drr_toname, "/@");
3219 /* zfs_receive_one() will create_parents() */
3220 (void) strlcat(tofs, &drrb->drr_toname[i],
3222 *strchr(tofs, '@') = '\0';
3225 if (!flags->dryrun && !flags->nomount) {
3226 VERIFY(0 == nvlist_alloc(&renamed,
3227 NV_UNIQUE_NAME, 0));
3230 softerr = recv_incremental_replication(hdl, tofs, flags,
3231 stream_nv, stream_avl, renamed);
3233 /* Unmount renamed filesystems before receiving. */
3234 while ((pair = nvlist_next_nvpair(renamed,
3237 prop_changelist_t *clp = NULL;
3239 zhp = zfs_open(hdl, nvpair_name(pair),
3240 ZFS_TYPE_FILESYSTEM);
3242 clp = changelist_gather(zhp,
3243 ZFS_PROP_MOUNTPOINT, 0, 0);
3247 changelist_prefix(clp);
3248 changelist_free(clp);
3253 nvlist_free(renamed);
3258 * Get the fs specified by the first path in the stream (the top level
3259 * specified by 'zfs send') and pass it to each invocation of
3260 * zfs_receive_one().
3262 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
3264 if ((cp = strchr(sendfs, '@')) != NULL) {
3267 * Find the "sendsnap", the final snapshot in a replication
3268 * stream. zfs_receive_one() handles certain errors
3269 * differently, depending on if the contained stream is the
3272 sendsnap = (cp + 1);
3275 /* Finally, receive each contained stream */
3278 * we should figure out if it has a recoverable
3279 * error, in which case do a recv_skip() and drive on.
3280 * Note, if we fail due to already having this guid,
3281 * zfs_receive_one() will take care of it (ie,
3282 * recv_skip() and return 0).
3284 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3285 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
3286 action_handlep, sendsnap, cmdprops);
3287 if (error == ENODATA) {
3292 } while (error == 0);
3294 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
3296 * Now that we have the fs's they sent us, try the
3299 softerr = recv_incremental_replication(hdl, tofs, flags,
3300 stream_nv, stream_avl, NULL);
3303 if (raw && softerr == 0) {
3304 softerr = recv_fix_encryption_heirarchy(hdl, destname,
3305 stream_nv, stream_avl);
3309 fsavl_destroy(stream_avl);
3310 nvlist_free(stream_nv);
3319 trunc_prop_errs(int truncated)
3321 ASSERT(truncated != 0);
3324 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3325 "1 more property could not be set\n"));
3327 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3328 "%d more properties could not be set\n"), truncated);
3332 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3334 dmu_replay_record_t *drr;
3335 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
3338 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3339 "cannot receive:"));
3341 /* XXX would be great to use lseek if possible... */
3344 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3345 byteswap, NULL) == 0) {
3347 drr->drr_type = BSWAP_32(drr->drr_type);
3349 switch (drr->drr_type) {
3351 if (drr->drr_payloadlen != 0) {
3352 (void) recv_read(hdl, fd, buf,
3353 drr->drr_payloadlen, B_FALSE, NULL);
3363 drr->drr_u.drr_object.drr_bonuslen =
3364 BSWAP_32(drr->drr_u.drr_object.
3367 (void) recv_read(hdl, fd, buf,
3368 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
3374 drr->drr_u.drr_write.drr_logical_size =
3376 drr->drr_u.drr_write.drr_logical_size);
3377 drr->drr_u.drr_write.drr_compressed_size =
3379 drr->drr_u.drr_write.drr_compressed_size);
3381 uint64_t payload_size =
3382 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3383 (void) recv_read(hdl, fd, buf,
3384 payload_size, B_FALSE, NULL);
3388 drr->drr_u.drr_spill.drr_length =
3389 BSWAP_64(drr->drr_u.drr_spill.drr_length);
3391 (void) recv_read(hdl, fd, buf,
3392 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
3394 case DRR_WRITE_EMBEDDED:
3396 drr->drr_u.drr_write_embedded.drr_psize =
3397 BSWAP_32(drr->drr_u.drr_write_embedded.
3400 (void) recv_read(hdl, fd, buf,
3401 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3404 case DRR_WRITE_BYREF:
3405 case DRR_FREEOBJECTS:
3410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3411 "invalid record type"));
3413 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3422 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3423 boolean_t resumable)
3425 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3427 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3428 "checksum mismatch or incomplete stream"));
3432 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3433 *strchr(target_fs, '@') = '\0';
3434 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3435 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3439 char token_buf[ZFS_MAXPROPLEN];
3440 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3441 token_buf, sizeof (token_buf),
3442 NULL, NULL, 0, B_TRUE);
3444 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3445 "checksum mismatch or incomplete stream.\n"
3446 "Partially received snapshot is saved.\n"
3447 "A resuming stream can be generated on the sending "
3448 "system by running:\n"
3456 * Prepare a new nvlist of properties that are to override (-o) or be excluded
3457 * (-x) from the received dataset
3458 * recvprops: received properties from the send stream
3459 * cmdprops: raw input properties from command line
3460 * origprops: properties, both locally-set and received, currently set on the
3461 * target dataset if it exists, NULL otherwise.
3462 * oxprops: valid output override (-o) and excluded (-x) properties
3465 zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
3466 char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
3467 boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
3468 nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
3469 uint_t *wkeylen_out, const char *errbuf)
3472 nvlist_t *oprops, *voprops;
3473 zfs_handle_t *zhp = NULL;
3474 zpool_handle_t *zpool_hdl = NULL;
3477 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
3479 if (nvlist_empty(cmdprops))
3480 return (0); /* No properties to override or exclude */
3482 *oxprops = fnvlist_alloc();
3483 oprops = fnvlist_alloc();
3485 strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
3488 * Get our dataset handle. The target dataset may not exist yet.
3490 if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
3491 zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
3498 /* open the zpool handle */
3499 cp = strchr(namebuf, '/');
3502 zpool_hdl = zpool_open(hdl, namebuf);
3503 if (zpool_hdl == NULL) {
3508 /* restore namebuf to match fsname for later use */
3513 * first iteration: process excluded (-x) properties now and gather
3514 * added (-o) properties to be later processed by zfs_valid_proplist()
3517 while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
3518 const char *name = nvpair_name(nvp);
3519 zfs_prop_t prop = zfs_name_to_prop(name);
3521 /* "origin" is processed separately, don't handle it here */
3522 if (prop == ZFS_PROP_ORIGIN)
3526 * we're trying to override or exclude a property that does not
3527 * make sense for this type of dataset, but we don't want to
3528 * fail if the receive is recursive: this comes in handy when
3529 * the send stream contains, for instance, a child ZVOL and
3530 * we're trying to receive it with "-o atime=on"
3532 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
3533 !zfs_prop_user(name)) {
3536 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3537 "property '%s' does not apply to datasets of this "
3539 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3543 /* raw streams can't override encryption properties */
3544 if ((zfs_prop_encryption_key_param(prop) ||
3545 prop == ZFS_PROP_ENCRYPTION) && (raw || !newfs)) {
3546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3547 "encryption property '%s' cannot "
3548 "be set or excluded for raw or incremental "
3550 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3554 switch (nvpair_type(nvp)) {
3555 case DATA_TYPE_BOOLEAN: /* -x property */
3557 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
3558 * a property: this is done by forcing an explicit
3559 * inherit on the destination so the effective value is
3560 * not the one we received from the send stream.
3561 * We do this only if the property is not already
3562 * locally-set, in which case its value will take
3563 * priority over the received anyway.
3565 if (nvlist_exists(origprops, name)) {
3568 attrs = fnvlist_lookup_nvlist(origprops, name);
3569 if (strcmp(fnvlist_lookup_string(attrs,
3570 ZPROP_SOURCE), ZPROP_SOURCE_VAL_RECVD) != 0)
3574 * We can't force an explicit inherit on non-inheritable
3575 * properties: if we're asked to exclude this kind of
3576 * values we remove them from "recvprops" input nvlist.
3578 if (!zfs_prop_inheritable(prop) &&
3579 !zfs_prop_user(name) && /* can be inherited too */
3580 nvlist_exists(recvprops, name))
3581 fnvlist_remove(recvprops, name);
3583 fnvlist_add_nvpair(*oxprops, nvp);
3585 case DATA_TYPE_STRING: /* -o property=value */
3586 fnvlist_add_nvpair(oprops, nvp);
3589 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3590 "property '%s' must be a string or boolean"), name);
3591 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3597 /* convert override strings properties to native */
3598 if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
3599 oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
3600 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3605 * zfs_crypto_create() requires the parent name. Get it
3606 * by truncating the fsname copy stored in namebuf.
3608 cp = strrchr(namebuf, '/');
3612 if (!raw && zfs_crypto_create(hdl, namebuf, voprops, NULL,
3613 B_FALSE, wkeydata_out, wkeylen_out) != 0) {
3614 fnvlist_free(voprops);
3615 ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
3619 /* second pass: process "-o" properties */
3620 fnvlist_merge(*oxprops, voprops);
3621 fnvlist_free(voprops);
3623 /* override props on child dataset are inherited */
3625 while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
3626 const char *name = nvpair_name(nvp);
3627 fnvlist_add_boolean(*oxprops, name);
3634 if (zpool_hdl != NULL)
3635 zpool_close(zpool_hdl);
3636 fnvlist_free(oprops);
3641 * Restores a backup of tosnap from the file descriptor specified by infd.
3644 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3645 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3646 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3647 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3648 uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops)
3651 int ioctl_err, ioctl_errno, err;
3653 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3655 const char *chopprefix;
3656 boolean_t newfs = B_FALSE;
3657 boolean_t stream_wantsnewfs;
3658 boolean_t newprops = B_FALSE;
3659 uint64_t read_bytes = 0;
3660 uint64_t errflags = 0;
3661 uint64_t parent_snapguid = 0;
3662 prop_changelist_t *clp = NULL;
3663 nvlist_t *snapprops_nvlist = NULL;
3664 zprop_errflags_t prop_errflags;
3665 nvlist_t *prop_errors = NULL;
3666 boolean_t recursive;
3667 char *snapname = NULL;
3668 char destsnap[MAXPATHLEN * 2];
3669 char origin[MAXNAMELEN];
3670 char name[MAXPATHLEN];
3671 char tmp_keylocation[MAXNAMELEN];
3672 nvlist_t *rcvprops = NULL; /* props received from the send stream */
3673 nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
3674 nvlist_t *origprops = NULL; /* original props (if destination exists) */
3676 boolean_t toplevel = B_FALSE;
3677 boolean_t zoned = B_FALSE;
3678 boolean_t hastoken = B_FALSE;
3679 uint8_t *wkeydata = NULL;
3682 begin_time = time(NULL);
3683 bzero(origin, MAXNAMELEN);
3684 bzero(tmp_keylocation, MAXNAMELEN);
3686 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3689 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3692 if (stream_avl != NULL) {
3693 char *keylocation = NULL;
3694 nvlist_t *lookup = NULL;
3695 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3698 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
3700 err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
3702 VERIFY(0 == nvlist_alloc(&rcvprops, NV_UNIQUE_NAME, 0));
3707 * The keylocation property may only be set on encryption roots,
3708 * but this dataset might not become an encryption root until
3709 * recv_fix_encryption_heirarchy() is called. That function
3710 * will fixup the keylocation anyway, so we temporarily unset
3711 * the keylocation for now to avoid any errors from the receive
3714 err = nvlist_lookup_string(rcvprops,
3715 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
3717 strcpy(tmp_keylocation, keylocation);
3718 (void) nvlist_remove_all(rcvprops,
3719 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
3722 if (flags->canmountoff) {
3723 VERIFY(0 == nvlist_add_uint64(rcvprops,
3724 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3726 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
3727 VERIFY(0 == nvlist_lookup_nvlist(lookup,
3728 snapname, &snapprops_nvlist));
3735 * Determine how much of the snapshot name stored in the stream
3736 * we are going to tack on to the name they specified on the
3737 * command line, and how much we are going to chop off.
3739 * If they specified a snapshot, chop the entire name stored in
3742 if (flags->istail) {
3744 * A filesystem was specified with -e. We want to tack on only
3745 * the tail of the sent snapshot path.
3747 if (strchr(tosnap, '@')) {
3748 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3749 "argument - snapshot not allowed with -e"));
3750 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3754 chopprefix = strrchr(sendfs, '/');
3756 if (chopprefix == NULL) {
3758 * The tail is the poolname, so we need to
3759 * prepend a path separator.
3761 int len = strlen(drrb->drr_toname);
3762 cp = malloc(len + 2);
3764 (void) strcpy(&cp[1], drrb->drr_toname);
3767 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3769 } else if (flags->isprefix) {
3771 * A filesystem was specified with -d. We want to tack on
3772 * everything but the first element of the sent snapshot path
3773 * (all but the pool name).
3775 if (strchr(tosnap, '@')) {
3776 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3777 "argument - snapshot not allowed with -d"));
3778 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3782 chopprefix = strchr(drrb->drr_toname, '/');
3783 if (chopprefix == NULL)
3784 chopprefix = strchr(drrb->drr_toname, '@');
3785 } else if (strchr(tosnap, '@') == NULL) {
3787 * If a filesystem was specified without -d or -e, we want to
3788 * tack on everything after the fs specified by 'zfs send'.
3790 chopprefix = drrb->drr_toname + strlen(sendfs);
3792 /* A snapshot was specified as an exact path (no -d or -e). */
3794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3795 "cannot specify snapshot name for multi-snapshot "
3797 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3800 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3803 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3804 ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
3805 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
3806 strchr(sendfs, '/') == NULL);
3807 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3808 chopprefix[0] == '\0');
3811 * Determine name of destination snapshot.
3813 (void) strlcpy(destsnap, tosnap, sizeof (destsnap));
3814 (void) strlcat(destsnap, chopprefix, sizeof (destsnap));
3816 if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
3817 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3822 * Determine the name of the origin snapshot.
3825 (void) strlcpy(origin, originsnap, sizeof (origin));
3827 (void) printf("using provided clone origin %s\n",
3829 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3830 if (guid_to_name(hdl, destsnap,
3831 drrb->drr_fromguid, B_FALSE, origin) != 0) {
3832 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3833 "local origin for clone %s does not exist"),
3835 err = zfs_error(hdl, EZFS_NOENT, errbuf);
3839 (void) printf("found clone origin %s\n", origin);
3842 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3843 DMU_BACKUP_FEATURE_RESUMING;
3844 boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3845 DMU_BACKUP_FEATURE_RAW;
3846 boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3847 DMU_BACKUP_FEATURE_EMBED_DATA;
3848 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
3849 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3851 if (stream_wantsnewfs) {
3853 * if the parent fs does not exist, look for it based on
3854 * the parent snap GUID
3856 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3857 "cannot receive new filesystem stream"));
3859 (void) strcpy(name, destsnap);
3860 cp = strrchr(name, '/');
3864 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
3865 char suffix[ZFS_MAX_DATASET_NAME_LEN];
3866 (void) strcpy(suffix, strrchr(destsnap, '/'));
3867 if (guid_to_name(hdl, name, parent_snapguid,
3868 B_FALSE, destsnap) == 0) {
3869 *strchr(destsnap, '@') = '\0';
3870 (void) strcat(destsnap, suffix);
3875 * if the fs does not exist, look for it based on the
3878 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3879 "cannot receive incremental stream"));
3881 (void) strcpy(name, destsnap);
3882 *strchr(name, '@') = '\0';
3885 * If the exact receive path was specified and this is the
3886 * topmost path in the stream, then if the fs does not exist we
3887 * should look no further.
3889 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
3890 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
3891 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
3892 char snap[ZFS_MAX_DATASET_NAME_LEN];
3893 (void) strcpy(snap, strchr(destsnap, '@'));
3894 if (guid_to_name(hdl, name, drrb->drr_fromguid,
3895 B_FALSE, destsnap) == 0) {
3896 *strchr(destsnap, '@') = '\0';
3897 (void) strcat(destsnap, snap);
3902 (void) strcpy(name, destsnap);
3903 *strchr(name, '@') = '\0';
3905 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
3906 zfs_cmd_t zc = {"\0"};
3908 boolean_t encrypted;
3910 (void) strcpy(zc.zc_name, name);
3913 * Destination fs exists. It must be one of these cases:
3914 * - an incremental send stream
3915 * - the stream specifies a new fs (full stream or clone)
3916 * and they want us to blow away the existing fs (and
3917 * have therefore specified -F and removed any snapshots)
3918 * - we are resuming a failed receive.
3920 if (stream_wantsnewfs) {
3921 if (!flags->force) {
3922 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3923 "destination '%s' exists\n"
3924 "must specify -F to overwrite it"), name);
3925 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3928 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3930 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3931 "destination has snapshots (eg. %s)\n"
3932 "must destroy them to overwrite it"),
3934 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3939 if ((zhp = zfs_open(hdl, name,
3940 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
3945 if (stream_wantsnewfs &&
3946 zhp->zfs_dmustats.dds_origin[0]) {
3948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3949 "destination '%s' is a clone\n"
3950 "must destroy it to overwrite it"), name);
3951 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3956 * Raw sends can not be performed as an incremental on top
3957 * of existing unencryppted datasets. zfs recv -F cant be
3958 * used to blow away an existing encrypted filesystem. This
3959 * is because it would require the dsl dir to point to the
3960 * new key (or lack of a key) and the old key at the same
3961 * time. The -F flag may still be used for deleting
3962 * intermediate snapshots that would otherwise prevent the
3963 * receive from working.
3965 encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
3967 if (!stream_wantsnewfs && !encrypted && raw) {
3969 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3970 "cannot perform raw receive on top of "
3971 "existing unencrypted dataset"));
3972 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3976 if (stream_wantsnewfs && flags->force &&
3977 ((raw && !encrypted) || encrypted)) {
3979 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3980 "zfs receive -F cannot be used to destroy an "
3981 "encrypted filesystem or overwrite an "
3982 "unencrypted one with an encrypted one"));
3983 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3987 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3988 stream_wantsnewfs) {
3989 /* We can't do online recv in this case */
3990 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3996 if (changelist_prefix(clp) != 0) {
3997 changelist_free(clp);
4005 * If we are resuming a newfs, set newfs here so that we will
4006 * mount it if the recv succeeds this time. We can tell
4007 * that it was a newfs on the first recv because the fs
4008 * itself will be inconsistent (if the fs existed when we
4009 * did the first recv, we would have received it into
4012 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4015 /* we want to know if we're zoned when validating -o|-x props */
4016 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4018 /* may need this info later, get it now we have zhp around */
4019 if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4020 NULL, NULL, 0, B_TRUE) == 0)
4023 /* gather existing properties on destination */
4024 origprops = fnvlist_alloc();
4025 fnvlist_merge(origprops, zhp->zfs_props);
4026 fnvlist_merge(origprops, zhp->zfs_user_props);
4033 * Destination filesystem does not exist. Therefore we better
4034 * be creating a new filesystem (either from a full backup, or
4035 * a clone). It would therefore be invalid if the user
4036 * specified only the pool name (i.e. if the destination name
4037 * contained no slash character).
4039 cp = strrchr(name, '/');
4041 if (!stream_wantsnewfs || cp == NULL) {
4042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4043 "destination '%s' does not exist"), name);
4044 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4049 * Trim off the final dataset component so we perform the
4050 * recvbackup ioctl to the filesystems's parent.
4054 if (flags->isprefix && !flags->istail && !flags->dryrun &&
4055 create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4056 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4061 * It is invalid to receive a properties stream that was
4062 * unencrypted on the send side as a child of an encrypted
4063 * parent. Technically there is nothing preventing this, but
4064 * it would mean that the encryption=off property which is
4065 * locally set on the send side would not be received correctly.
4066 * We can infer encryption=off if the stream is not raw and
4067 * properties were included since the send side will only ever
4068 * send the encryption property in a raw nvlist header. This
4069 * check will be avoided if the user specifically overrides
4070 * the encryption property on the command line.
4072 if (!raw && rcvprops != NULL &&
4073 !nvlist_exists(cmdprops,
4074 zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
4077 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4079 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4083 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
4086 if (crypt != ZIO_CRYPT_OFF) {
4087 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4088 "parent '%s' must not be encrypted to "
4089 "receive unenecrypted property"), name);
4090 err = zfs_error(hdl, EZFS_BADPROP, errbuf);
4099 if (flags->verbose) {
4100 (void) printf("%s %s stream of %s into %s\n",
4101 flags->dryrun ? "would receive" : "receiving",
4102 drrb->drr_fromguid ? "incremental" : "full",
4103 drrb->drr_toname, destsnap);
4104 (void) fflush(stdout);
4107 if (flags->dryrun) {
4108 err = recv_skip(hdl, infd, flags->byteswap);
4112 if (top_zfs && (*top_zfs == NULL || strcmp(*top_zfs, name) == 0))
4114 if (drrb->drr_type == DMU_OST_ZVOL) {
4115 type = ZFS_TYPE_VOLUME;
4116 } else if (drrb->drr_type == DMU_OST_ZFS) {
4117 type = ZFS_TYPE_FILESYSTEM;
4119 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4120 "invalid record type: 0x%d"), drrb->drr_type);
4121 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4124 if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4125 stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4126 &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
4129 err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
4130 oxprops, wkeydata, wkeylen, origin, flags->force, flags->resumable,
4131 raw, infd, drr_noswap, cleanup_fd, &read_bytes, &errflags,
4132 action_handlep, &prop_errors);
4133 ioctl_errno = ioctl_err;
4134 prop_errflags = errflags;
4137 nvpair_t *prop_err = NULL;
4139 while ((prop_err = nvlist_next_nvpair(prop_errors,
4140 prop_err)) != NULL) {
4145 prop = zfs_name_to_prop(nvpair_name(prop_err));
4146 (void) nvpair_value_int32(prop_err, &intval);
4147 if (strcmp(nvpair_name(prop_err),
4148 ZPROP_N_MORE_ERRORS) == 0) {
4149 trunc_prop_errs(intval);
4151 } else if (snapname == NULL || finalsnap == NULL ||
4152 strcmp(finalsnap, snapname) == 0 ||
4153 strcmp(nvpair_name(prop_err),
4154 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
4156 * Skip the special case of, for example,
4157 * "refquota", errors on intermediate
4158 * snapshots leading up to a final one.
4159 * That's why we have all of the checks above.
4161 * See zfs_ioctl.c's extract_delay_props() for
4162 * a list of props which can fail on
4163 * intermediate snapshots, but shouldn't
4164 * affect the overall receive.
4166 (void) snprintf(tbuf, sizeof (tbuf),
4167 dgettext(TEXT_DOMAIN,
4168 "cannot receive %s property on %s"),
4169 nvpair_name(prop_err), name);
4170 zfs_setprop_error(hdl, prop, intval, tbuf);
4175 if (err == 0 && snapprops_nvlist) {
4176 zfs_cmd_t zc = {"\0"};
4178 (void) strcpy(zc.zc_name, destsnap);
4179 zc.zc_cookie = B_TRUE; /* received */
4180 if (zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist) == 0) {
4181 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
4182 zcmd_free_nvlists(&zc);
4186 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
4188 * It may be that this snapshot already exists,
4189 * in which case we want to consume & ignore it
4190 * rather than failing.
4192 avl_tree_t *local_avl;
4193 nvlist_t *local_nv, *fs;
4194 cp = strchr(destsnap, '@');
4197 * XXX Do this faster by just iterating over snaps in
4198 * this fs. Also if zc_value does not exist, we will
4199 * get a strange "does not exist" error message.
4202 if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
4203 B_FALSE, B_FALSE, &local_nv, &local_avl) == 0) {
4205 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
4206 fsavl_destroy(local_avl);
4207 nvlist_free(local_nv);
4210 if (flags->verbose) {
4211 (void) printf("snap %s already exists; "
4212 "ignoring\n", destsnap);
4214 err = ioctl_err = recv_skip(hdl, infd,
4221 if (ioctl_err != 0) {
4222 switch (ioctl_errno) {
4224 cp = strchr(destsnap, '@');
4226 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4227 "most recent snapshot of %s does not\n"
4228 "match incremental source"), destsnap);
4229 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4234 "destination %s has been modified\n"
4235 "since most recent snapshot"), name);
4236 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4239 if (raw && stream_wantsnewfs) {
4240 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4241 "failed to create encryption key"));
4242 } else if (raw && !stream_wantsnewfs) {
4243 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4244 "encryption key does not match "
4247 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4248 "inherited key must be loaded"));
4250 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4253 cp = strchr(destsnap, '@');
4255 /* it's the containing fs that exists */
4258 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4259 "destination already exists"));
4260 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
4261 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
4266 if (flags->resumable)
4267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4268 "kernel modules must be upgraded to "
4269 "receive this stream."));
4270 if (embedded && !raw)
4271 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4272 "incompatible embedded data stream "
4273 "feature with encrypted receive."));
4274 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4277 recv_ecksum_set_aux(hdl, destsnap, flags->resumable);
4278 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4281 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4282 "pool must be upgraded to receive this stream."));
4283 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4286 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4287 "destination %s space quota exceeded."), name);
4288 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
4292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4293 "destination %s contains "
4294 "partially-complete state from "
4295 "\"zfs receive -s\"."), name);
4296 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
4301 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
4306 * Mount the target filesystem (if created). Also mount any
4307 * children of the target filesystem if we did a replication
4308 * receive (indicated by stream_avl being non-NULL).
4310 cp = strchr(destsnap, '@');
4311 if (cp && (ioctl_err == 0 || !newfs)) {
4315 h = zfs_open(hdl, destsnap,
4316 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4318 if (h->zfs_type == ZFS_TYPE_VOLUME) {
4320 } else if (newfs || stream_avl) {
4322 * Track the first/top of hierarchy fs,
4323 * for mounting and sharing later.
4325 if (top_zfs && *top_zfs == NULL)
4326 *top_zfs = zfs_strdup(hdl, destsnap);
4334 if (!flags->nomount)
4335 err |= changelist_postfix(clp);
4336 changelist_free(clp);
4339 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
4340 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4341 "failed to clear unreceived properties on %s"), name);
4342 (void) fprintf(stderr, "\n");
4344 if (prop_errflags & ZPROP_ERR_NORESTORE) {
4345 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4346 "failed to restore original properties on %s"), name);
4347 (void) fprintf(stderr, "\n");
4350 if (err || ioctl_err) {
4355 if (flags->verbose) {
4358 uint64_t bytes = read_bytes;
4359 time_t delta = time(NULL) - begin_time;
4362 zfs_nicebytes(bytes, buf1, sizeof (buf1));
4363 zfs_nicebytes(bytes/delta, buf2, sizeof (buf1));
4365 (void) printf("received %s stream in %lu seconds (%s/sec)\n",
4371 if (prop_errors != NULL)
4372 nvlist_free(prop_errors);
4374 if (tmp_keylocation[0] != '\0') {
4375 VERIFY(0 == nvlist_add_string(rcvprops,
4376 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation));
4380 nvlist_free(rcvprops);
4382 nvlist_free(oxprops);
4383 nvlist_free(origprops);
4389 * Check properties we were asked to override (both -o|-x)
4392 zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
4400 while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
4401 name = nvpair_name(nvp);
4402 prop = zfs_name_to_prop(name);
4404 if (prop == ZPROP_INVAL) {
4405 if (!zfs_prop_user(name)) {
4406 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4407 "invalid property '%s'"), name);
4413 * "origin" is readonly but is used to receive datasets as
4414 * clones so we don't raise an error here
4416 if (prop == ZFS_PROP_ORIGIN)
4419 /* encryption params have their own verification later */
4420 if (prop == ZFS_PROP_ENCRYPTION ||
4421 zfs_prop_encryption_key_param(prop))
4425 * cannot override readonly, set-once and other specific
4426 * settable properties
4428 if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
4429 prop == ZFS_PROP_VOLSIZE) {
4430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4431 "invalid property '%s'"), name);
4440 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
4441 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
4442 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
4443 uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops)
4446 dmu_replay_record_t drr, drr_noswap;
4447 struct drr_begin *drrb = &drr.drr_u.drr_begin;
4449 zio_cksum_t zcksum = { { 0 } };
4450 uint64_t featureflags;
4453 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4456 /* check cmdline props, raise an error if they cannot be received */
4457 if (!zfs_receive_checkprops(hdl, cmdprops, errbuf)) {
4458 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
4461 if (flags->isprefix &&
4462 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
4463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
4464 "(%s) does not exist"), tosnap);
4465 return (zfs_error(hdl, EZFS_NOENT, errbuf));
4468 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
4469 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
4470 "(%s) does not exist"), originsnap);
4471 return (zfs_error(hdl, EZFS_NOENT, errbuf));
4474 /* read in the BEGIN record */
4475 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
4479 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
4480 /* It's the double end record at the end of a package */
4484 /* the kernel needs the non-byteswapped begin record */
4487 flags->byteswap = B_FALSE;
4488 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
4490 * We computed the checksum in the wrong byteorder in
4491 * recv_read() above; do it again correctly.
4493 bzero(&zcksum, sizeof (zio_cksum_t));
4494 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
4495 flags->byteswap = B_TRUE;
4497 drr.drr_type = BSWAP_32(drr.drr_type);
4498 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
4499 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
4500 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
4501 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
4502 drrb->drr_type = BSWAP_32(drrb->drr_type);
4503 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
4504 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
4505 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
4508 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
4509 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4510 "stream (bad magic number)"));
4511 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4514 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
4515 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
4517 if (!DMU_STREAM_SUPPORTED(featureflags) ||
4518 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
4519 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4520 "stream has unsupported feature, feature flags = %lx"),
4522 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4525 if (strchr(drrb->drr_toname, '@') == NULL) {
4526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4527 "stream (bad snapshot name)"));
4528 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4531 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
4532 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
4533 if (sendfs == NULL) {
4535 * We were not called from zfs_receive_package(). Get
4536 * the fs specified by 'zfs send'.
4539 (void) strlcpy(nonpackage_sendfs,
4540 drr.drr_u.drr_begin.drr_toname,
4541 sizeof (nonpackage_sendfs));
4542 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
4544 sendfs = nonpackage_sendfs;
4545 VERIFY(finalsnap == NULL);
4547 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
4548 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
4549 cleanup_fd, action_handlep, finalsnap, cmdprops));
4551 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
4552 DMU_COMPOUNDSTREAM);
4553 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
4554 &zcksum, top_zfs, cleanup_fd, action_handlep, cmdprops));
4559 * Restores a backup of tosnap from the file descriptor specified by infd.
4560 * Return 0 on total success, -2 if some things couldn't be
4561 * destroyed/renamed/promoted, -1 if some things couldn't be received.
4562 * (-1 will override -2, if -1 and the resumable flag was specified the
4563 * transfer can be resumed if the sending side supports it).
4566 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
4567 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
4569 char *top_zfs = NULL;
4572 uint64_t action_handle = 0;
4574 char *originsnap = NULL;
4577 * The only way fstat can fail is if we do not have a valid file
4580 if (fstat(infd, &sb) == -1) {
4586 #ifndef F_SETPIPE_SZ
4587 #define F_SETPIPE_SZ (F_SETLEASE + 7)
4588 #endif /* F_SETPIPE_SZ */
4590 #ifndef F_GETPIPE_SZ
4591 #define F_GETPIPE_SZ (F_GETLEASE + 7)
4592 #endif /* F_GETPIPE_SZ */
4595 * It is not uncommon for gigabytes to be processed in zfs receive.
4596 * Speculatively increase the buffer size via Linux-specific fcntl()
4599 if (S_ISFIFO(sb.st_mode)) {
4600 FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "r");
4602 if (procf != NULL) {
4603 unsigned long max_psize;
4605 if (fscanf(procf, "%lu", &max_psize) > 0) {
4606 cur_psize = fcntl(infd, F_GETPIPE_SZ);
4607 if (cur_psize > 0 &&
4608 max_psize > (unsigned long) cur_psize)
4609 (void) fcntl(infd, F_SETPIPE_SZ,
4615 #endif /* __linux__ */
4618 err = nvlist_lookup_string(props, "origin", &originsnap);
4619 if (err && err != ENOENT)
4623 cleanup_fd = open(ZFS_DEV, O_RDWR);
4624 VERIFY(cleanup_fd >= 0);
4626 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
4627 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL, props);
4629 VERIFY(0 == close(cleanup_fd));
4631 if (err == 0 && !flags->nomount && top_zfs) {
4632 zfs_handle_t *zhp = NULL;
4633 prop_changelist_t *clp = NULL;
4635 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
4637 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
4638 CL_GATHER_MOUNT_ALWAYS, 0);
4641 /* mount and share received datasets */
4642 err = changelist_postfix(clp);
4643 changelist_free(clp);
4646 if (zhp == NULL || clp == NULL || err)