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) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
32 * Copyright (c) 2019 Datto Inc.
45 #include <sys/mount.h>
46 #include <sys/mntent.h>
47 #include <sys/mnttab.h>
49 #include <sys/debug.h>
57 #include <libzfs_core.h>
60 #include "zfs_namecheck.h"
62 #include "zfs_fletcher.h"
63 #include "libzfs_impl.h"
65 #include <sys/zio_checksum.h>
66 #include <sys/dsl_crypt.h>
68 #include <sys/socket.h>
71 /* in libzfs_dataset.c */
72 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
74 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
75 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
76 uint64_t *, const char *, nvlist_t *);
77 static int guid_to_name(libzfs_handle_t *, const char *,
78 uint64_t, boolean_t, char *);
80 static const zio_cksum_t zero_cksum = { { 0 } };
82 typedef struct dedup_arg {
85 libzfs_handle_t *dedup_hdl;
88 typedef struct progress_arg {
91 boolean_t pa_parsable;
94 typedef struct dataref {
100 typedef struct dedup_entry {
101 struct dedup_entry *dde_next;
102 zio_cksum_t dde_chksum;
107 #define MAX_DDT_PHYSMEM_PERCENT 20
108 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
110 typedef struct dedup_table {
111 dedup_entry_t **dedup_hash_array;
112 umem_cache_t *ddecache;
113 uint64_t max_ddt_size; /* max dedup table size in bytes */
114 uint64_t cur_ddt_size; /* current dedup table size in bytes */
121 high_order_bit(uint64_t n)
125 for (count = 0; n != 0; count++)
131 ssread(void *buf, size_t len, FILE *stream)
135 if ((outlen = fread(buf, len, 1, stream)) == 0)
142 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
143 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
147 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
148 if (ddt->ddt_full == B_FALSE) {
149 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
150 "Dedup table full. Deduplication will continue "
151 "with existing table entries"));
152 ddt->ddt_full = B_TRUE;
157 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
159 assert(*ddepp == NULL);
160 dde->dde_next = NULL;
161 dde->dde_chksum = *cs;
162 dde->dde_prop = prop;
165 ddt->cur_ddt_size += sizeof (dedup_entry_t);
171 * Using the specified dedup table, do a lookup for an entry with
172 * the checksum cs. If found, return the block's reference info
173 * in *dr. Otherwise, insert a new entry in the dedup table, using
174 * the reference information specified by *dr.
176 * return value: true - entry was found
177 * false - entry was not found
180 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
181 uint64_t prop, dataref_t *dr)
184 dedup_entry_t **ddepp;
186 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
188 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
189 ddepp = &((*ddepp)->dde_next)) {
190 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
191 (*ddepp)->dde_prop == prop) {
192 *dr = (*ddepp)->dde_ref;
196 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
201 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
202 zio_cksum_t *zc, int outfd)
204 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
205 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
206 fletcher_4_incremental_native(drr,
207 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
208 if (drr->drr_type != DRR_BEGIN) {
209 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
210 drr_checksum.drr_checksum));
211 drr->drr_u.drr_checksum.drr_checksum = *zc;
213 fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
214 sizeof (zio_cksum_t), zc);
215 if (write(outfd, drr, sizeof (*drr)) == -1)
217 if (payload_len != 0) {
218 fletcher_4_incremental_native(payload, payload_len, zc);
219 if (write(outfd, payload, payload_len) == -1)
226 * This function is started in a separate thread when the dedup option
227 * has been requested. The main send thread determines the list of
228 * snapshots to be included in the send stream and makes the ioctl calls
229 * for each one. But instead of having the ioctl send the output to the
230 * the output fd specified by the caller of zfs_send()), the
231 * ioctl is told to direct the output to a pipe, which is read by the
232 * alternate thread running THIS function. This function does the
234 * 1. building a dedup table (the DDT)
235 * 2. doing checksums on each data block and inserting a record in the DDT
236 * 3. looking for matching checksums, and
237 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
238 * a duplicate block is found.
239 * The output of this function then goes to the output fd requested
240 * by the caller of zfs_send().
245 dedup_arg_t *dda = arg;
246 char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
247 dmu_replay_record_t thedrr = { 0 };
248 dmu_replay_record_t *drr = &thedrr;
252 zio_cksum_t stream_cksum;
256 ddt.max_ddt_size = SMALLEST_POSSIBLE_MAX_DDT_MB << 20;
258 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
260 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
261 SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
264 numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
267 * numbuckets must be a power of 2. Increase number to
268 * a power of 2 if necessary.
270 if (!ISP2(numbuckets))
271 numbuckets = 1ULL << high_order_bit(numbuckets);
273 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
274 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
275 NULL, NULL, NULL, NULL, NULL, 0);
276 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
277 ddt.numhashbits = high_order_bit(numbuckets) - 1;
278 ddt.ddt_full = B_FALSE;
280 outfd = dda->outputfd;
281 ofp = fdopen(dda->inputfd, "r");
282 while (ssread(drr, sizeof (*drr), ofp) != 0) {
285 * kernel filled in checksum, we are going to write same
286 * record, but need to regenerate checksum.
288 if (drr->drr_type != DRR_BEGIN) {
289 bzero(&drr->drr_u.drr_checksum.drr_checksum,
290 sizeof (drr->drr_u.drr_checksum.drr_checksum));
293 switch (drr->drr_type) {
296 struct drr_begin *drrb = &drr->drr_u.drr_begin;
299 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
301 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
303 /* set the DEDUP feature flag for this stream */
304 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
305 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
306 DMU_BACKUP_FEATURE_DEDUPPROPS);
307 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
309 if (drr->drr_payloadlen != 0) {
310 sz = drr->drr_payloadlen;
312 if (sz > SPA_MAXBLOCKSIZE) {
313 buf = zfs_realloc(dda->dedup_hdl, buf,
314 SPA_MAXBLOCKSIZE, sz);
316 (void) ssread(buf, sz, ofp);
320 if (dump_record(drr, buf, sz, &stream_cksum,
328 struct drr_end *drre = &drr->drr_u.drr_end;
329 /* use the recalculated checksum */
330 drre->drr_checksum = stream_cksum;
331 if (dump_record(drr, NULL, 0, &stream_cksum,
339 struct drr_object *drro = &drr->drr_u.drr_object;
340 if (drro->drr_bonuslen > 0) {
342 DRR_OBJECT_PAYLOAD_SIZE(drro), ofp);
344 if (dump_record(drr, buf, DRR_OBJECT_PAYLOAD_SIZE(drro),
345 &stream_cksum, outfd) != 0)
352 struct drr_spill *drrs = &drr->drr_u.drr_spill;
353 (void) ssread(buf, DRR_SPILL_PAYLOAD_SIZE(drrs), ofp);
354 if (dump_record(drr, buf, DRR_SPILL_PAYLOAD_SIZE(drrs),
355 &stream_cksum, outfd) != 0)
360 case DRR_FREEOBJECTS:
362 if (dump_record(drr, NULL, 0, &stream_cksum,
370 struct drr_write *drrw = &drr->drr_u.drr_write;
372 uint64_t payload_size;
374 payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
375 (void) ssread(buf, payload_size, ofp);
378 * Use the existing checksum if it's dedup-capable,
379 * else calculate a SHA256 checksum for it.
382 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
384 !DRR_IS_DEDUP_CAPABLE(drrw->drr_flags)) {
386 zio_cksum_t tmpsha256;
388 SHA2Init(SHA256, &ctx);
389 SHA2Update(&ctx, buf, payload_size);
390 SHA2Final(&tmpsha256, &ctx);
392 drrw->drr_key.ddk_cksum.zc_word[0] =
393 BE_64(tmpsha256.zc_word[0]);
394 drrw->drr_key.ddk_cksum.zc_word[1] =
395 BE_64(tmpsha256.zc_word[1]);
396 drrw->drr_key.ddk_cksum.zc_word[2] =
397 BE_64(tmpsha256.zc_word[2]);
398 drrw->drr_key.ddk_cksum.zc_word[3] =
399 BE_64(tmpsha256.zc_word[3]);
400 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
401 drrw->drr_flags |= DRR_CHECKSUM_DEDUP;
404 dataref.ref_guid = drrw->drr_toguid;
405 dataref.ref_object = drrw->drr_object;
406 dataref.ref_offset = drrw->drr_offset;
408 if (ddt_update(dda->dedup_hdl, &ddt,
409 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
411 dmu_replay_record_t wbr_drr = {0};
412 struct drr_write_byref *wbr_drrr =
413 &wbr_drr.drr_u.drr_write_byref;
415 /* block already present in stream */
416 wbr_drr.drr_type = DRR_WRITE_BYREF;
418 wbr_drrr->drr_object = drrw->drr_object;
419 wbr_drrr->drr_offset = drrw->drr_offset;
420 wbr_drrr->drr_length = drrw->drr_logical_size;
421 wbr_drrr->drr_toguid = drrw->drr_toguid;
422 wbr_drrr->drr_refguid = dataref.ref_guid;
423 wbr_drrr->drr_refobject =
425 wbr_drrr->drr_refoffset =
428 wbr_drrr->drr_checksumtype =
429 drrw->drr_checksumtype;
430 wbr_drrr->drr_flags = drrw->drr_flags;
431 wbr_drrr->drr_key.ddk_cksum =
432 drrw->drr_key.ddk_cksum;
433 wbr_drrr->drr_key.ddk_prop =
434 drrw->drr_key.ddk_prop;
436 if (dump_record(&wbr_drr, NULL, 0,
437 &stream_cksum, outfd) != 0)
440 /* block not previously seen */
441 if (dump_record(drr, buf, payload_size,
442 &stream_cksum, outfd) != 0)
448 case DRR_WRITE_EMBEDDED:
450 struct drr_write_embedded *drrwe =
451 &drr->drr_u.drr_write_embedded;
453 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
454 if (dump_record(drr, buf,
455 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
456 &stream_cksum, outfd) != 0)
463 if (dump_record(drr, NULL, 0, &stream_cksum,
469 case DRR_OBJECT_RANGE:
471 if (dump_record(drr, NULL, 0, &stream_cksum,
478 (void) fprintf(stderr, "INVALID record type 0x%x\n",
480 /* should never happen, so assert */
485 umem_cache_destroy(ddt.ddecache);
486 free(ddt.dedup_hash_array);
494 * Routines for dealing with the AVL tree of fs-nvlists
496 typedef struct fsavl_node {
504 fsavl_compare(const void *arg1, const void *arg2)
506 const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
507 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
509 return (AVL_CMP(fn1->fn_guid, fn2->fn_guid));
513 * Given the GUID of a snapshot, find its containing filesystem and
517 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
519 fsavl_node_t fn_find;
522 fn_find.fn_guid = snapguid;
524 fn = avl_find(avl, &fn_find, NULL);
527 *snapname = fn->fn_snapname;
528 return (fn->fn_nvfs);
534 fsavl_destroy(avl_tree_t *avl)
543 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
550 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
553 fsavl_create(nvlist_t *fss)
556 nvpair_t *fselem = NULL;
558 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
561 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
562 offsetof(fsavl_node_t, fn_node));
564 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
565 nvlist_t *nvfs, *snaps;
566 nvpair_t *snapelem = NULL;
568 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
569 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
572 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
576 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
577 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
578 fsavl_destroy(fsavl);
582 fn->fn_snapname = nvpair_name(snapelem);
586 * Note: if there are multiple snaps with the
587 * same GUID, we ignore all but one.
589 if (avl_find(fsavl, fn, NULL) == NULL)
600 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
602 typedef struct send_data {
604 * assigned inside every recursive call,
605 * restored from *_save on return:
607 * guid of fromsnap snapshot in parent dataset
608 * txg of fromsnap snapshot in current dataset
609 * txg of tosnap snapshot in current dataset
612 uint64_t parent_fromsnap_guid;
613 uint64_t fromsnap_txg;
616 /* the nvlists get accumulated during depth-first traversal */
617 nvlist_t *parent_snaps;
620 nvlist_t *snapholds; /* user holds */
622 /* send-receive configuration, does not change during traversal */
624 const char *fromsnap;
634 boolean_t holds; /* were holds requested with send -h */
638 * The header nvlist is of the following format:
641 * "fromsnap" -> string (if incremental)
645 * "name" -> string (full name; for debugging)
646 * "parentfromsnap" -> number (guid of fromsnap in parent)
648 * "props" -> { name -> value (only if set here) }
649 * "snaps" -> { name (lastname) -> number (guid) }
650 * "snapprops" -> { name (lastname) -> { name -> value } }
651 * "snapholds" -> { name (lastname) -> { holdname -> crtime } }
653 * "origin" -> number (guid) (if clone)
654 * "is_encroot" -> boolean
655 * "sent" -> boolean (not on-disk)
664 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
667 send_iterate_snap(zfs_handle_t *zhp, void *arg)
669 send_data_t *sd = arg;
670 uint64_t guid = zhp->zfs_dmustats.dds_guid;
671 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
674 boolean_t isfromsnap, istosnap, istosnapwithnofrom;
676 snapname = strrchr(zhp->zfs_name, '@')+1;
677 isfromsnap = (sd->fromsnap != NULL &&
678 strcmp(sd->fromsnap, snapname) == 0);
679 istosnap = (sd->tosnap != NULL && (strcmp(sd->tosnap, snapname) == 0));
680 istosnapwithnofrom = (istosnap && sd->fromsnap == NULL);
682 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
684 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
685 "skipping snapshot %s because it was created "
686 "after the destination snapshot (%s)\n"),
687 zhp->zfs_name, sd->tosnap);
693 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
695 * NB: if there is no fromsnap here (it's a newly created fs in
696 * an incremental replication), we will substitute the tosnap.
698 if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap)) {
699 sd->parent_fromsnap_guid = guid;
702 if (!sd->recursive) {
703 if (!sd->seenfrom && isfromsnap) {
704 sd->seenfrom = B_TRUE;
709 if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) {
718 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
719 send_iterate_prop(zhp, sd->backup, nv);
720 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
723 nvlist_t *holds = fnvlist_alloc();
724 int err = lzc_get_holds(zhp->zfs_name, &holds);
726 VERIFY(0 == nvlist_add_nvlist(sd->snapholds,
737 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
739 nvlist_t *props = NULL;
740 nvpair_t *elem = NULL;
743 props = zfs_get_recvd_props(zhp);
745 props = zhp->zfs_props;
747 while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
748 char *propname = nvpair_name(elem);
749 zfs_prop_t prop = zfs_name_to_prop(propname);
752 if (!zfs_prop_user(propname)) {
754 * Realistically, this should never happen. However,
755 * we want the ability to add DSL properties without
756 * needing to make incompatible version changes. We
757 * need to ignore unknown properties to allow older
758 * software to still send datasets containing these
759 * properties, with the unknown properties elided.
761 if (prop == ZPROP_INVAL)
764 if (zfs_prop_readonly(prop))
768 verify(nvpair_value_nvlist(elem, &propnv) == 0);
769 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
770 prop == ZFS_PROP_REFQUOTA ||
771 prop == ZFS_PROP_REFRESERVATION) {
774 verify(nvlist_lookup_uint64(propnv,
775 ZPROP_VALUE, &value) == 0);
776 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
779 * May have no source before SPA_VERSION_RECVD_PROPS,
780 * but is still modifiable.
782 if (nvlist_lookup_string(propnv,
783 ZPROP_SOURCE, &source) == 0) {
784 if ((strcmp(source, zhp->zfs_name) != 0) &&
786 ZPROP_SOURCE_VAL_RECVD) != 0))
791 if (nvlist_lookup_string(propnv,
792 ZPROP_SOURCE, &source) != 0)
794 if ((strcmp(source, zhp->zfs_name) != 0) &&
795 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
799 if (zfs_prop_user(propname) ||
800 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
802 verify(nvlist_lookup_string(propnv,
803 ZPROP_VALUE, &value) == 0);
804 VERIFY(0 == nvlist_add_string(nv, propname, value));
807 verify(nvlist_lookup_uint64(propnv,
808 ZPROP_VALUE, &value) == 0);
809 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
815 * returns snapshot creation txg
816 * and returns 0 if the snapshot does not exist
819 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
821 char name[ZFS_MAX_DATASET_NAME_LEN];
824 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
827 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
828 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
829 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
831 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
840 * recursively generate nvlists describing datasets. See comment
841 * for the data structure send_data_t above for description of contents
845 send_iterate_fs(zfs_handle_t *zhp, void *arg)
847 send_data_t *sd = arg;
848 nvlist_t *nvfs = NULL, *nv = NULL;
850 uint64_t min_txg = 0, max_txg = 0;
851 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
852 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
853 uint64_t tosnap_txg_save = sd->tosnap_txg;
854 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
855 uint64_t guid = zhp->zfs_dmustats.dds_guid;
856 uint64_t fromsnap_txg, tosnap_txg;
859 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
860 if (fromsnap_txg != 0)
861 sd->fromsnap_txg = fromsnap_txg;
863 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
865 sd->tosnap_txg = tosnap_txg;
868 * on the send side, if the current dataset does not have tosnap,
869 * perform two additional checks:
871 * - skip sending the current dataset if it was created later than
873 * - return error if the current dataset was created earlier than
876 if (sd->tosnap != NULL && tosnap_txg == 0) {
877 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
879 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
880 "skipping dataset %s: snapshot %s does "
881 "not exist\n"), zhp->zfs_name, sd->tosnap);
884 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
885 "cannot send %s@%s%s: snapshot %s@%s does not "
886 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
887 dgettext(TEXT_DOMAIN, " recursively") : "",
888 zhp->zfs_name, sd->tosnap);
894 nvfs = fnvlist_alloc();
895 fnvlist_add_string(nvfs, "name", zhp->zfs_name);
896 fnvlist_add_uint64(nvfs, "parentfromsnap",
897 sd->parent_fromsnap_guid);
899 if (zhp->zfs_dmustats.dds_origin[0]) {
900 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
901 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
902 if (origin == NULL) {
906 fnvlist_add_uint64(nvfs, "origin",
907 origin->zfs_dmustats.dds_guid);
912 /* iterate over props */
913 if (sd->props || sd->backup || sd->recursive) {
914 nv = fnvlist_alloc();
915 send_iterate_prop(zhp, sd->backup, nv);
917 if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
920 /* determine if this dataset is an encryption root */
921 if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
927 fnvlist_add_boolean(nvfs, "is_encroot");
930 * Encrypted datasets can only be sent with properties if
931 * the raw flag is specified because the receive side doesn't
932 * currently have a mechanism for recursively asking the user
933 * for new encryption parameters.
936 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
937 "cannot send %s@%s: encrypted dataset %s may not "
938 "be sent with properties without the raw flag\n"),
939 sd->fsname, sd->tosnap, zhp->zfs_name);
947 fnvlist_add_nvlist(nvfs, "props", nv);
949 /* iterate over snaps, and set sd->parent_fromsnap_guid */
950 sd->parent_fromsnap_guid = 0;
951 sd->parent_snaps = fnvlist_alloc();
952 sd->snapprops = fnvlist_alloc();
954 VERIFY(0 == nvlist_alloc(&sd->snapholds, NV_UNIQUE_NAME, 0));
958 * If this is a "doall" send, a replicate send or we're just trying
959 * to gather a list of previous snapshots, iterate through all the
960 * snaps in the txg range. Otherwise just look at the one we're
963 if (sd->doall || sd->replicate || sd->tosnap == NULL) {
964 if (!sd->replicate && fromsnap_txg != 0)
965 min_txg = fromsnap_txg;
966 if (!sd->replicate && tosnap_txg != 0)
967 max_txg = tosnap_txg;
968 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd,
971 char snapname[MAXPATHLEN] = { 0 };
974 (void) snprintf(snapname, sizeof (snapname) - 1, "%s@%s",
975 zhp->zfs_name, sd->tosnap);
976 if (sd->fromsnap != NULL)
977 sd->seenfrom = B_TRUE;
978 snap = zfs_open(zhp->zfs_hdl, snapname,
981 (void) send_iterate_snap(snap, sd);
984 fnvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps);
985 fnvlist_add_nvlist(nvfs, "snapprops", sd->snapprops);
987 fnvlist_add_nvlist(nvfs, "snapholds", sd->snapholds);
988 fnvlist_free(sd->parent_snaps);
989 fnvlist_free(sd->snapprops);
990 fnvlist_free(sd->snapholds);
992 /* add this fs to nvlist */
993 (void) snprintf(guidstring, sizeof (guidstring),
994 "0x%llx", (longlong_t)guid);
995 fnvlist_add_nvlist(sd->fss, guidstring, nvfs);
997 /* iterate over children */
999 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
1002 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
1003 sd->fromsnap_txg = fromsnap_txg_save;
1004 sd->tosnap_txg = tosnap_txg_save;
1013 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
1014 const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t doall,
1015 boolean_t replicate, boolean_t verbose, boolean_t backup, boolean_t holds,
1016 boolean_t props, nvlist_t **nvlp, avl_tree_t **avlp)
1019 send_data_t sd = { 0 };
1022 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1024 return (EZFS_BADTYPE);
1026 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
1028 sd.fromsnap = fromsnap;
1030 sd.recursive = recursive;
1033 sd.replicate = replicate;
1034 sd.verbose = verbose;
1039 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
1040 nvlist_free(sd.fss);
1047 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
1048 nvlist_free(sd.fss);
1050 return (EZFS_NOMEM);
1058 * Routines specific to "zfs send"
1060 typedef struct send_dump_data {
1061 /* these are all just the short snapname (the part after the @) */
1062 const char *fromsnap;
1064 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
1065 uint64_t prevsnap_obj;
1066 boolean_t seenfrom, seento, replicate, doall, fromorigin;
1067 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
1068 boolean_t large_block, compress, raw, holds;
1072 nvlist_t *snapholds;
1074 snapfilter_cb_t *filter_cb;
1075 void *filter_cb_arg;
1077 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
1083 zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
1084 enum lzc_send_flags flags, uint64_t *spacep)
1086 libzfs_handle_t *hdl = zhp->zfs_hdl;
1089 assert(snapname != NULL);
1090 error = lzc_send_space(snapname, from, flags, spacep);
1094 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1095 "warning: cannot estimate space for '%s'"), snapname);
1099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1100 "not an earlier snapshot from the same fs"));
1101 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1104 if (zfs_dataset_exists(hdl, snapname,
1105 ZFS_TYPE_SNAPSHOT)) {
1106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1107 "incremental source (%s) does not exist"),
1110 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1124 zfs_error_aux(hdl, strerror(error));
1125 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1128 return (zfs_standard_error(hdl, error, errbuf));
1136 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1137 * NULL) to the file descriptor specified by outfd.
1140 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
1141 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
1144 zfs_cmd_t zc = {"\0"};
1145 libzfs_handle_t *hdl = zhp->zfs_hdl;
1148 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1149 assert(fromsnap_obj == 0 || !fromorigin);
1151 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1152 zc.zc_cookie = outfd;
1153 zc.zc_obj = fromorigin;
1154 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1155 zc.zc_fromobj = fromsnap_obj;
1156 zc.zc_flags = flags;
1158 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
1159 if (fromsnap && fromsnap[0] != '\0') {
1160 VERIFY(0 == nvlist_add_string(thisdbg,
1161 "fromsnap", fromsnap));
1164 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1166 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1167 "warning: cannot send '%s'"), zhp->zfs_name);
1169 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
1171 VERIFY(0 == nvlist_add_nvlist(debugnv,
1172 zhp->zfs_name, thisdbg));
1174 nvlist_free(thisdbg);
1178 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1179 "not an earlier snapshot from the same fs"));
1180 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1184 "source key must be loaded"));
1185 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1188 if (zfs_dataset_exists(hdl, zc.zc_name,
1189 ZFS_TYPE_SNAPSHOT)) {
1190 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1191 "incremental source (@%s) does not exist"),
1194 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1207 zfs_error_aux(hdl, strerror(errno));
1208 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1211 return (zfs_standard_error(hdl, errno, errbuf));
1216 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1217 nvlist_free(thisdbg);
1223 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1225 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1228 * zfs_send() only sets snapholds for sends that need them,
1229 * e.g. replication and doall.
1231 if (sdd->snapholds == NULL)
1234 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1238 send_progress_thread(void *arg)
1240 progress_arg_t *pa = arg;
1241 zfs_cmd_t zc = {"\0"};
1242 zfs_handle_t *zhp = pa->pa_zhp;
1243 libzfs_handle_t *hdl = zhp->zfs_hdl;
1244 unsigned long long bytes;
1249 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1251 if (!pa->pa_parsable)
1252 (void) fprintf(stderr, "TIME SENT SNAPSHOT %s\n",
1256 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1261 zc.zc_cookie = pa->pa_fd;
1262 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1263 return ((void *)-1);
1267 bytes = zc.zc_cookie;
1269 if (pa->pa_parsable) {
1270 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1271 tm->tm_hour, tm->tm_min, tm->tm_sec,
1272 bytes, zhp->zfs_name);
1274 zfs_nicebytes(bytes, buf, sizeof (buf));
1275 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1276 tm->tm_hour, tm->tm_min, tm->tm_sec,
1277 buf, zhp->zfs_name);
1283 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1284 uint64_t size, boolean_t parsable)
1287 if (fromsnap != NULL) {
1288 (void) fprintf(fout, "incremental\t%s\t%s",
1291 (void) fprintf(fout, "full\t%s",
1295 if (fromsnap != NULL) {
1296 if (strchr(fromsnap, '@') == NULL &&
1297 strchr(fromsnap, '#') == NULL) {
1298 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1299 "send from @%s to %s"),
1302 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1303 "send from %s to %s"),
1307 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1314 (void) fprintf(fout, "\t%llu",
1316 } else if (size != 0) {
1318 zfs_nicebytes(size, buf, sizeof (buf));
1319 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1320 " estimated size is %s"), buf);
1322 (void) fprintf(fout, "\n");
1326 dump_snapshot(zfs_handle_t *zhp, void *arg)
1328 send_dump_data_t *sdd = arg;
1329 progress_arg_t pa = { 0 };
1332 enum lzc_send_flags flags = 0;
1334 boolean_t isfromsnap, istosnap, fromorigin;
1335 boolean_t exclude = B_FALSE;
1336 FILE *fout = sdd->std_out ? stdout : stderr;
1339 thissnap = strchr(zhp->zfs_name, '@') + 1;
1340 isfromsnap = (sdd->fromsnap != NULL &&
1341 strcmp(sdd->fromsnap, thissnap) == 0);
1343 if (!sdd->seenfrom && isfromsnap) {
1344 gather_holds(zhp, sdd);
1345 sdd->seenfrom = B_TRUE;
1346 (void) strlcpy(sdd->prevsnap, thissnap,
1347 sizeof (sdd->prevsnap));
1348 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1353 if (sdd->seento || !sdd->seenfrom) {
1358 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1360 sdd->seento = B_TRUE;
1362 if (sdd->large_block)
1363 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1364 if (sdd->embed_data)
1365 flags |= LZC_SEND_FLAG_EMBED_DATA;
1367 flags |= LZC_SEND_FLAG_COMPRESS;
1369 flags |= LZC_SEND_FLAG_RAW;
1371 if (!sdd->doall && !isfromsnap && !istosnap) {
1372 if (sdd->replicate) {
1374 nvlist_t *snapprops;
1376 * Filter out all intermediate snapshots except origin
1377 * snapshots needed to replicate clones.
1379 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1380 zhp->zfs_dmustats.dds_guid, &snapname);
1382 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1383 "snapprops", &snapprops));
1384 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1385 thissnap, &snapprops));
1386 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1393 * If a filter function exists, call it to determine whether
1394 * this snapshot will be sent.
1396 if (exclude || (sdd->filter_cb != NULL &&
1397 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1399 * This snapshot is filtered out. Don't send it, and don't
1400 * set prevsnap_obj, so it will be as if this snapshot didn't
1401 * exist, and the next accepted snapshot will be sent as
1402 * an incremental from the last accepted one, or as the
1403 * first (and full) snapshot in the case of a replication,
1404 * non-incremental send.
1410 gather_holds(zhp, sdd);
1411 fromorigin = sdd->prevsnap[0] == '\0' &&
1412 (sdd->fromorigin || sdd->replicate);
1416 char fromds[ZFS_MAX_DATASET_NAME_LEN];
1418 if (sdd->prevsnap[0] != '\0') {
1419 (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1420 *(strchr(fromds, '@') + 1) = '\0';
1421 (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1423 if (zfs_send_space(zhp, zhp->zfs_name,
1424 sdd->prevsnap[0] ? fromds : NULL, flags, &size) != 0) {
1425 size = 0; /* cannot estimate send space */
1427 send_print_verbose(fout, zhp->zfs_name,
1428 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1429 size, sdd->parsable);
1436 * If progress reporting is requested, spawn a new thread to
1437 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1439 if (sdd->progress) {
1441 pa.pa_fd = sdd->outfd;
1442 pa.pa_parsable = sdd->parsable;
1444 if ((err = pthread_create(&tid, NULL,
1445 send_progress_thread, &pa)) != 0) {
1451 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1452 fromorigin, sdd->outfd, flags, sdd->debugnv);
1454 if (sdd->progress) {
1455 (void) pthread_cancel(tid);
1456 (void) pthread_join(tid, NULL);
1460 (void) strcpy(sdd->prevsnap, thissnap);
1461 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1467 dump_filesystem(zfs_handle_t *zhp, void *arg)
1470 send_dump_data_t *sdd = arg;
1471 boolean_t missingfrom = B_FALSE;
1472 zfs_cmd_t zc = {"\0"};
1473 uint64_t min_txg = 0, max_txg = 0;
1475 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1476 zhp->zfs_name, sdd->tosnap);
1477 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1478 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1479 "WARNING: could not send %s@%s: does not exist\n"),
1480 zhp->zfs_name, sdd->tosnap);
1485 if (sdd->replicate && sdd->fromsnap) {
1487 * If this fs does not have fromsnap, and we're doing
1488 * recursive, we need to send a full stream from the
1489 * beginning (or an incremental from the origin if this
1490 * is a clone). If we're doing non-recursive, then let
1491 * them get the error.
1493 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1494 zhp->zfs_name, sdd->fromsnap);
1495 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1496 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1497 missingfrom = B_TRUE;
1501 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1502 sdd->prevsnap_obj = 0;
1503 if (sdd->fromsnap == NULL || missingfrom)
1504 sdd->seenfrom = B_TRUE;
1509 * Iterate through all snapshots and process the ones we will be
1510 * sending. If we only have a "from" and "to" snapshot to deal
1511 * with, we can avoid iterating through all the other snapshots.
1513 if (sdd->doall || sdd->replicate || sdd->tosnap == NULL) {
1514 if (!sdd->replicate && sdd->fromsnap != NULL)
1515 min_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name,
1517 if (!sdd->replicate && sdd->tosnap != NULL)
1518 max_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name,
1520 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg,
1523 char snapname[MAXPATHLEN] = { 0 };
1526 if (!sdd->seenfrom) {
1527 (void) snprintf(snapname, sizeof (snapname) - 1,
1528 "%s@%s", zhp->zfs_name, sdd->fromsnap);
1529 snap = zfs_open(zhp->zfs_hdl, snapname,
1532 rv = dump_snapshot(snap, sdd);
1538 (void) snprintf(snapname, sizeof (snapname) - 1,
1539 "%s@%s", zhp->zfs_name, sdd->tosnap);
1540 snap = zfs_open(zhp->zfs_hdl, snapname,
1543 rv = dump_snapshot(snap, sdd);
1549 if (!sdd->seenfrom) {
1550 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1551 "WARNING: could not send %s@%s:\n"
1552 "incremental source (%s@%s) does not exist\n"),
1553 zhp->zfs_name, sdd->tosnap,
1554 zhp->zfs_name, sdd->fromsnap);
1556 } else if (!sdd->seento) {
1557 if (sdd->fromsnap) {
1558 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1559 "WARNING: could not send %s@%s:\n"
1560 "incremental source (%s@%s) "
1561 "is not earlier than it\n"),
1562 zhp->zfs_name, sdd->tosnap,
1563 zhp->zfs_name, sdd->fromsnap);
1565 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1567 "could not send %s@%s: does not exist\n"),
1568 zhp->zfs_name, sdd->tosnap);
1577 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1579 send_dump_data_t *sdd = arg;
1581 boolean_t needagain, progress;
1583 if (!sdd->replicate)
1584 return (dump_filesystem(rzhp, sdd));
1586 /* Mark the clone origin snapshots. */
1587 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1588 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1590 uint64_t origin_guid = 0;
1592 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1593 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1594 if (origin_guid != 0) {
1596 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1597 origin_guid, &snapname);
1598 if (origin_nv != NULL) {
1599 nvlist_t *snapprops;
1600 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1601 "snapprops", &snapprops));
1602 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1603 snapname, &snapprops));
1604 VERIFY(0 == nvlist_add_boolean(
1605 snapprops, "is_clone_origin"));
1610 needagain = progress = B_FALSE;
1611 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1612 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1613 nvlist_t *fslist, *parent_nv;
1617 uint64_t origin_guid = 0;
1618 uint64_t parent_guid = 0;
1620 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1621 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1624 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1625 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1626 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1629 if (parent_guid != 0) {
1630 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1631 if (!nvlist_exists(parent_nv, "sent")) {
1632 /* parent has not been sent; skip this one */
1638 if (origin_guid != 0) {
1639 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1641 if (origin_nv != NULL &&
1642 !nvlist_exists(origin_nv, "sent")) {
1644 * origin has not been sent yet;
1652 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1655 err = dump_filesystem(zhp, sdd);
1656 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1667 /* clean out the sent flags in case we reuse this fss */
1668 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1669 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1672 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1673 (void) nvlist_remove_all(fslist, "sent");
1680 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1682 unsigned int version;
1684 unsigned long long checksum, packed_len;
1687 * Decode token header, which is:
1688 * <token version>-<checksum of payload>-<uncompressed payload length>
1689 * Note that the only supported token version is 1.
1691 nread = sscanf(token, "%u-%llx-%llx-",
1692 &version, &checksum, &packed_len);
1694 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1695 "resume token is corrupt (invalid format)"));
1699 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1700 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1701 "resume token is corrupt (invalid version %u)"),
1706 /* convert hexadecimal representation to binary */
1707 token = strrchr(token, '-') + 1;
1708 int len = strlen(token) / 2;
1709 unsigned char *compressed = zfs_alloc(hdl, len);
1710 for (i = 0; i < len; i++) {
1711 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1715 "resume token is corrupt "
1716 "(payload is not hex-encoded)"));
1721 /* verify checksum */
1723 fletcher_4_native_varsize(compressed, len, &cksum);
1724 if (cksum.zc_word[0] != checksum) {
1726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1727 "resume token is corrupt (incorrect checksum)"));
1732 void *packed = zfs_alloc(hdl, packed_len);
1733 uLongf packed_len_long = packed_len;
1734 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1735 packed_len_long != packed_len) {
1738 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1739 "resume token is corrupt (decompression failed)"));
1745 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1749 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1750 "resume token is corrupt (nvlist_unpack failed)"));
1757 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1758 const char *resume_token)
1762 char *fromname = NULL;
1763 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1766 char name[ZFS_MAX_DATASET_NAME_LEN];
1767 enum lzc_send_flags lzc_flags = 0;
1768 FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr;
1770 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1771 "cannot resume send"));
1773 nvlist_t *resume_nvl =
1774 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1775 if (resume_nvl == NULL) {
1777 * zfs_error_aux has already been set by
1778 * zfs_send_resume_token_to_nvlist
1780 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1782 if (flags->verbose) {
1783 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1784 "resume token contents:\n"));
1785 nvlist_print(fout, resume_nvl);
1788 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1789 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1790 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1791 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1792 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1793 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1794 "resume token is corrupt"));
1795 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1798 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1800 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1801 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1802 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1803 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1804 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1805 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1806 if (flags->raw || nvlist_exists(resume_nvl, "rawok"))
1807 lzc_flags |= LZC_SEND_FLAG_RAW;
1809 if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1810 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1811 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1812 "'%s' is no longer the same snapshot used in "
1813 "the initial send"), toname);
1815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1816 "'%s' used in the initial send no longer exists"),
1819 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1821 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1824 "unable to access '%s'"), name);
1825 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1828 if (fromguid != 0) {
1829 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1830 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1831 "incremental source %#llx no longer exists"),
1832 (longlong_t)fromguid);
1833 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1838 if (flags->verbose) {
1840 error = lzc_send_space(zhp->zfs_name, fromname,
1843 size = MAX(0, (int64_t)(size - bytes));
1844 send_print_verbose(fout, zhp->zfs_name, fromname,
1845 size, flags->parsable);
1848 if (!flags->dryrun) {
1849 progress_arg_t pa = { 0 };
1852 * If progress reporting is requested, spawn a new thread to
1853 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1855 if (flags->progress) {
1858 pa.pa_parsable = flags->parsable;
1860 error = pthread_create(&tid, NULL,
1861 send_progress_thread, &pa);
1868 error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1869 lzc_flags, resumeobj, resumeoff);
1871 if (flags->progress) {
1872 (void) pthread_cancel(tid);
1873 (void) pthread_join(tid, NULL);
1877 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1878 "warning: cannot send '%s'"), zhp->zfs_name);
1886 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1887 "source key must be loaded"));
1888 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1903 zfs_error_aux(hdl, strerror(errno));
1904 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1907 return (zfs_standard_error(hdl, errno, errbuf));
1918 * Generate a send stream for the dataset identified by the argument zhp.
1920 * The content of the send stream is the snapshot identified by
1921 * 'tosnap'. Incremental streams are requested in two ways:
1922 * - from the snapshot identified by "fromsnap" (if non-null) or
1923 * - from the origin of the dataset identified by zhp, which must
1924 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1927 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1928 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1929 * if "replicate" is set. If "doall" is set, dump all the intermediate
1930 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1931 * case too. If "props" is set, send properties.
1934 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1935 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1936 void *cb_arg, nvlist_t **debugnvp)
1939 send_dump_data_t sdd = { 0 };
1941 nvlist_t *fss = NULL;
1942 avl_tree_t *fsavl = NULL;
1943 static uint64_t holdseq;
1947 dedup_arg_t dda = { 0 };
1948 int featureflags = 0;
1951 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1952 "cannot send '%s'"), zhp->zfs_name);
1954 if (fromsnap && fromsnap[0] == '\0') {
1955 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1956 "zero-length incremental source"));
1957 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1960 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1962 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1963 if (version >= ZPL_VERSION_SA) {
1964 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1969 featureflags |= DMU_BACKUP_FEATURE_HOLDS;
1972 * Start the dedup thread if this is a dedup stream. We do not bother
1973 * doing this if this a raw send of an encrypted dataset with dedup off
1974 * because normal encrypted blocks won't dedup.
1976 if (flags->dedup && !flags->dryrun && !(flags->raw &&
1977 zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
1978 zfs_prop_get_int(zhp, ZFS_PROP_DEDUP) == ZIO_CHECKSUM_OFF)) {
1979 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1980 DMU_BACKUP_FEATURE_DEDUPPROPS);
1981 if ((err = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd)) != 0) {
1982 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1983 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1986 dda.outputfd = outfd;
1987 dda.inputfd = pipefd[1];
1988 dda.dedup_hdl = zhp->zfs_hdl;
1989 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1990 (void) close(pipefd[0]);
1991 (void) close(pipefd[1]);
1992 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1993 return (zfs_error(zhp->zfs_hdl,
1994 EZFS_THREADCREATEFAILED, errbuf));
1998 if (flags->replicate || flags->doall || flags->props ||
1999 flags->holds || flags->backup) {
2000 dmu_replay_record_t drr = { 0 };
2001 char *packbuf = NULL;
2005 ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
2007 if (flags->replicate || flags->props || flags->backup ||
2011 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
2013 VERIFY(0 == nvlist_add_string(hdrnv,
2014 "fromsnap", fromsnap));
2016 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
2017 if (!flags->replicate) {
2018 VERIFY(0 == nvlist_add_boolean(hdrnv,
2022 VERIFY(0 == nvlist_add_boolean(hdrnv, "raw"));
2025 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
2026 fromsnap, tosnap, flags->replicate, flags->raw,
2027 flags->doall, flags->replicate, flags->verbose,
2028 flags->backup, flags->holds, flags->props, &fss,
2032 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
2033 err = nvlist_pack(hdrnv, &packbuf, &buflen,
2043 if (!flags->dryrun) {
2044 /* write first begin record */
2045 drr.drr_type = DRR_BEGIN;
2046 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
2047 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
2048 drr_versioninfo, DMU_COMPOUNDSTREAM);
2049 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
2050 drr_versioninfo, featureflags);
2051 if (snprintf(drr.drr_u.drr_begin.drr_toname,
2052 sizeof (drr.drr_u.drr_begin.drr_toname),
2053 "%s@%s", zhp->zfs_name, tosnap) >=
2054 sizeof (drr.drr_u.drr_begin.drr_toname)) {
2058 drr.drr_payloadlen = buflen;
2060 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
2065 /* write end record */
2066 bzero(&drr, sizeof (drr));
2067 drr.drr_type = DRR_END;
2068 drr.drr_u.drr_end.drr_checksum = zc;
2069 err = write(outfd, &drr, sizeof (drr));
2079 /* dump each stream */
2080 sdd.fromsnap = fromsnap;
2081 sdd.tosnap = tosnap;
2083 sdd.outfd = pipefd[0];
2086 sdd.replicate = flags->replicate;
2087 sdd.doall = flags->doall;
2088 sdd.fromorigin = flags->fromorigin;
2091 sdd.verbose = flags->verbose;
2092 sdd.parsable = flags->parsable;
2093 sdd.progress = flags->progress;
2094 sdd.dryrun = flags->dryrun;
2095 sdd.large_block = flags->largeblock;
2096 sdd.embed_data = flags->embed_data;
2097 sdd.compress = flags->compress;
2098 sdd.raw = flags->raw;
2099 sdd.holds = flags->holds;
2100 sdd.filter_cb = filter_func;
2101 sdd.filter_cb_arg = cb_arg;
2103 sdd.debugnv = *debugnvp;
2104 if (sdd.verbose && sdd.dryrun)
2105 sdd.std_out = B_TRUE;
2106 fout = sdd.std_out ? stdout : stderr;
2109 * Some flags require that we place user holds on the datasets that are
2110 * being sent so they don't get destroyed during the send. We can skip
2111 * this step if the pool is imported read-only since the datasets cannot
2114 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2115 ZPOOL_PROP_READONLY, NULL) &&
2116 zfs_spa_version(zhp, &spa_version) == 0 &&
2117 spa_version >= SPA_VERSION_USERREFS &&
2118 (flags->doall || flags->replicate)) {
2120 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2121 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2122 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
2123 if (sdd.cleanup_fd < 0) {
2127 sdd.snapholds = fnvlist_alloc();
2129 sdd.cleanup_fd = -1;
2130 sdd.snapholds = NULL;
2133 if (flags->verbose || sdd.snapholds != NULL) {
2135 * Do a verbose no-op dry run to get all the verbose output
2136 * or to gather snapshot hold's before generating any data,
2137 * then do a non-verbose real run to generate the streams.
2139 sdd.dryrun = B_TRUE;
2140 err = dump_filesystems(zhp, &sdd);
2145 if (flags->verbose) {
2146 if (flags->parsable) {
2147 (void) fprintf(fout, "size\t%llu\n",
2148 (longlong_t)sdd.size);
2151 zfs_nicebytes(sdd.size, buf, sizeof (buf));
2152 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
2153 "total estimated size is %s\n"), buf);
2157 /* Ensure no snaps found is treated as an error. */
2163 /* Skip the second run if dryrun was requested. */
2167 if (sdd.snapholds != NULL) {
2168 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2172 fnvlist_free(sdd.snapholds);
2173 sdd.snapholds = NULL;
2176 sdd.dryrun = B_FALSE;
2177 sdd.verbose = B_FALSE;
2180 err = dump_filesystems(zhp, &sdd);
2181 fsavl_destroy(fsavl);
2184 /* Ensure no snaps found is treated as an error. */
2185 if (err == 0 && !sdd.seento)
2190 (void) pthread_cancel(tid);
2191 (void) close(pipefd[0]);
2192 (void) pthread_join(tid, NULL);
2195 if (sdd.cleanup_fd != -1) {
2196 VERIFY(0 == close(sdd.cleanup_fd));
2197 sdd.cleanup_fd = -1;
2200 if (!flags->dryrun && (flags->replicate || flags->doall ||
2201 flags->props || flags->backup || flags->holds)) {
2203 * write final end record. NB: want to do this even if
2204 * there was some error, because it might not be totally
2207 dmu_replay_record_t drr = { 0 };
2208 drr.drr_type = DRR_END;
2209 if (write(outfd, &drr, sizeof (drr)) == -1) {
2210 return (zfs_standard_error(zhp->zfs_hdl,
2215 return (err || sdd.err);
2218 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2220 fsavl_destroy(fsavl);
2222 fnvlist_free(sdd.snapholds);
2224 if (sdd.cleanup_fd != -1)
2225 VERIFY(0 == close(sdd.cleanup_fd));
2227 (void) pthread_cancel(tid);
2228 (void) close(pipefd[0]);
2229 (void) pthread_join(tid, NULL);
2235 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t flags)
2238 libzfs_handle_t *hdl = zhp->zfs_hdl;
2239 enum lzc_send_flags lzc_flags = 0;
2240 FILE *fout = (flags.verbose && flags.dryrun) ? stdout : stderr;
2243 if (flags.largeblock)
2244 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
2245 if (flags.embed_data)
2246 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
2248 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
2250 lzc_flags |= LZC_SEND_FLAG_RAW;
2252 if (flags.verbose) {
2254 err = lzc_send_space(zhp->zfs_name, from, lzc_flags, &size);
2256 send_print_verbose(fout, zhp->zfs_name, from, size,
2259 (void) fprintf(stderr, "Cannot estimate send size: "
2260 "%s\n", strerror(errno));
2267 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2268 "warning: cannot send '%s'"), zhp->zfs_name);
2270 err = lzc_send(zhp->zfs_name, from, fd, lzc_flags);
2274 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2275 "not an earlier snapshot from the same fs"));
2276 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2280 if (lzc_exists(zhp->zfs_name)) {
2281 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2282 "incremental source (%s) does not exist"),
2285 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2288 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2289 "dataset key must be loaded"));
2290 return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2293 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2294 "target is busy; if a filesystem, "
2295 "it must not be mounted"));
2296 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2309 zfs_error_aux(hdl, strerror(errno));
2310 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2313 return (zfs_standard_error(hdl, errno, errbuf));
2320 * Routines specific to "zfs recv"
2324 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2325 boolean_t byteswap, zio_cksum_t *zc)
2331 assert(ilen <= SPA_MAXBLOCKSIZE);
2334 rv = read(fd, cp, len);
2339 if (rv < 0 || len != 0) {
2340 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2341 "failed to read from stream"));
2342 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2343 "cannot receive")));
2348 fletcher_4_incremental_byteswap(buf, ilen, zc);
2350 fletcher_4_incremental_native(buf, ilen, zc);
2356 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2357 boolean_t byteswap, zio_cksum_t *zc)
2362 buf = zfs_alloc(hdl, len);
2366 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2372 err = nvlist_unpack(buf, len, nvp, 0);
2375 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2376 "stream (malformed nvlist)"));
2383 * Returns the grand origin (origin of origin of origin...) of a given handle.
2384 * If this dataset is not a clone, it simply returns a copy of the original
2387 static zfs_handle_t *
2388 recv_open_grand_origin(zfs_handle_t *zhp)
2390 char origin[ZFS_MAX_DATASET_NAME_LEN];
2392 zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2394 while (ozhp != NULL) {
2395 if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2396 sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2399 (void) zfs_close(ozhp);
2400 ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2407 recv_rename_impl(zfs_handle_t *zhp, const char *name, const char *newname)
2410 zfs_handle_t *ozhp = NULL;
2413 * Attempt to rename the dataset. If it fails with EACCES we have
2414 * attempted to rename the dataset outside of its encryption root.
2415 * Force the dataset to become an encryption root and try again.
2417 err = lzc_rename(name, newname);
2418 if (err == EACCES) {
2419 ozhp = recv_open_grand_origin(zhp);
2425 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2430 err = lzc_rename(name, newname);
2440 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2441 int baselen, char *newname, recvflags_t *flags)
2445 prop_changelist_t *clp = NULL;
2446 zfs_handle_t *zhp = NULL;
2448 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2453 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2454 flags->force ? MS_FORCE : 0);
2459 err = changelist_prefix(clp);
2464 (void) strcpy(newname, tryname);
2465 if (flags->verbose) {
2466 (void) printf("attempting rename %s to %s\n",
2469 err = recv_rename_impl(zhp, name, newname);
2471 changelist_rename(clp, name, tryname);
2476 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2479 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2480 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2482 if (flags->verbose) {
2483 (void) printf("failed - trying rename %s to %s\n",
2486 err = recv_rename_impl(zhp, name, newname);
2488 changelist_rename(clp, name, newname);
2489 if (err && flags->verbose) {
2490 (void) printf("failed (%u) - "
2491 "will try again on next pass\n", errno);
2494 } else if (flags->verbose) {
2496 (void) printf("success\n");
2498 (void) printf("failed (%u)\n", errno);
2501 (void) changelist_postfix(clp);
2505 changelist_free(clp);
2513 recv_promote(libzfs_handle_t *hdl, const char *fsname,
2514 const char *origin_fsname, recvflags_t *flags)
2517 zfs_cmd_t zc = {"\0"};
2518 zfs_handle_t *zhp = NULL, *ozhp = NULL;
2521 (void) printf("promoting %s\n", fsname);
2523 (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
2524 (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
2527 * Attempt to promote the dataset. If it fails with EACCES the
2528 * promotion would cause this dataset to leave its encryption root.
2529 * Force the origin to become an encryption root and try again.
2531 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2532 if (err == EACCES) {
2533 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2539 ozhp = recv_open_grand_origin(zhp);
2545 err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2550 err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2563 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2564 char *newname, recvflags_t *flags)
2567 prop_changelist_t *clp;
2569 boolean_t defer = B_FALSE;
2572 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2575 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2576 flags->force ? MS_FORCE : 0);
2577 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2578 zfs_spa_version(zhp, &spa_version) == 0 &&
2579 spa_version >= SPA_VERSION_USERREFS)
2584 err = changelist_prefix(clp);
2589 (void) printf("attempting destroy %s\n", name);
2590 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2591 nvlist_t *nv = fnvlist_alloc();
2592 fnvlist_add_boolean(nv, name);
2593 err = lzc_destroy_snaps(nv, defer, NULL);
2596 err = lzc_destroy(name);
2600 (void) printf("success\n");
2601 changelist_remove(clp, name);
2604 (void) changelist_postfix(clp);
2605 changelist_free(clp);
2608 * Deferred destroy might destroy the snapshot or only mark it to be
2609 * destroyed later, and it returns success in either case.
2611 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2612 ZFS_TYPE_SNAPSHOT))) {
2613 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2619 typedef struct guid_to_name_data {
2621 boolean_t bookmark_ok;
2624 } guid_to_name_data_t;
2627 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2629 guid_to_name_data_t *gtnd = arg;
2633 if (gtnd->skip != NULL &&
2634 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2635 strcmp(slash + 1, gtnd->skip) == 0) {
2640 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2641 (void) strcpy(gtnd->name, zhp->zfs_name);
2646 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2647 if (err != EEXIST && gtnd->bookmark_ok)
2648 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2654 * Attempt to find the local dataset associated with this guid. In the case of
2655 * multiple matches, we attempt to find the "best" match by searching
2656 * progressively larger portions of the hierarchy. This allows one to send a
2657 * tree of datasets individually and guarantee that we will find the source
2658 * guid within that hierarchy, even if there are multiple matches elsewhere.
2661 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2662 boolean_t bookmark_ok, char *name)
2664 char pname[ZFS_MAX_DATASET_NAME_LEN];
2665 guid_to_name_data_t gtnd;
2668 gtnd.bookmark_ok = bookmark_ok;
2673 * Search progressively larger portions of the hierarchy, starting
2674 * with the filesystem specified by 'parent'. This will
2675 * select the "most local" version of the origin snapshot in the case
2676 * that there are multiple matching snapshots in the system.
2678 (void) strlcpy(pname, parent, sizeof (pname));
2679 char *cp = strrchr(pname, '@');
2681 cp = strchr(pname, '\0');
2682 for (; cp != NULL; cp = strrchr(pname, '/')) {
2683 /* Chop off the last component and open the parent */
2685 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2689 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
2691 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2692 if (err != EEXIST && bookmark_ok)
2693 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2699 * Remember the last portion of the dataset so we skip it next
2700 * time through (as we've already searched that portion of the
2703 gtnd.skip = strrchr(pname, '/') + 1;
2710 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2711 * guid1 is after guid2.
2714 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2715 uint64_t guid1, uint64_t guid2)
2718 char *fsname = NULL, *snapname = NULL;
2719 char buf[ZFS_MAX_DATASET_NAME_LEN];
2721 zfs_handle_t *guid1hdl, *guid2hdl;
2722 uint64_t create1, create2;
2729 nvfs = fsavl_find(avl, guid1, &snapname);
2730 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2731 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2732 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2733 if (guid1hdl == NULL)
2736 nvfs = fsavl_find(avl, guid2, &snapname);
2737 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2738 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2739 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2740 if (guid2hdl == NULL) {
2741 zfs_close(guid1hdl);
2745 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2746 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2748 if (create1 < create2)
2750 else if (create1 > create2)
2755 zfs_close(guid1hdl);
2756 zfs_close(guid2hdl);
2762 * This function reestablishes the hierarchy of encryption roots after a
2763 * recursive incremental receive has completed. This must be done after the
2764 * second call to recv_incremental_replication() has renamed and promoted all
2765 * sent datasets to their final locations in the dataset hierarchy.
2768 recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *destname,
2769 nvlist_t *stream_nv, avl_tree_t *stream_avl)
2772 nvpair_t *fselem = NULL;
2773 nvlist_t *stream_fss;
2775 char top_zfs[ZFS_MAX_DATASET_NAME_LEN];
2777 (void) strcpy(top_zfs, destname);
2778 cp = strrchr(top_zfs, '@');
2782 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", &stream_fss));
2784 while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
2785 zfs_handle_t *zhp = NULL;
2787 nvlist_t *snaps, *props, *stream_nvfs = NULL;
2788 nvpair_t *snapel = NULL;
2789 boolean_t is_encroot, is_clone, stream_encroot;
2791 char *stream_keylocation = NULL;
2792 char keylocation[MAXNAMELEN];
2793 char fsname[ZFS_MAX_DATASET_NAME_LEN];
2795 keylocation[0] = '\0';
2796 VERIFY(0 == nvpair_value_nvlist(fselem, &stream_nvfs));
2797 VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "snaps", &snaps));
2798 VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "props", &props));
2799 stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
2801 /* find a snapshot from the stream that exists locally */
2803 while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
2806 VERIFY(0 == nvpair_value_uint64(snapel, &guid));
2807 err = guid_to_name(hdl, destname, guid, B_FALSE,
2816 cp = strchr(fsname, '@');
2820 zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2826 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
2827 is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
2828 (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
2830 /* we don't need to do anything for unencrypted filesystems */
2831 if (crypt == ZIO_CRYPT_OFF) {
2837 * If the dataset is flagged as an encryption root, was not
2838 * received as a clone and is not currently an encryption root,
2839 * force it to become one. Fixup the keylocation if necessary.
2841 if (stream_encroot) {
2842 if (!is_clone && !is_encroot) {
2843 err = lzc_change_key(fsname,
2844 DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
2851 VERIFY(0 == nvlist_lookup_string(props,
2852 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2853 &stream_keylocation));
2856 * Refresh the properties in case the call to
2857 * lzc_change_key() changed the value.
2859 zfs_refresh_properties(zhp);
2860 err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
2861 keylocation, sizeof (keylocation), NULL, NULL,
2868 if (strcmp(keylocation, stream_keylocation) != 0) {
2869 err = zfs_prop_set(zhp,
2870 zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2871 stream_keylocation);
2880 * If the dataset is not flagged as an encryption root and is
2881 * currently an encryption root, force it to inherit from its
2882 * parent. The root of a raw send should never be
2885 if (!stream_encroot && is_encroot &&
2886 strcmp(top_zfs, fsname) != 0) {
2887 err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
2905 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2906 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2909 nvlist_t *local_nv, *deleted = NULL;
2910 avl_tree_t *local_avl;
2911 nvpair_t *fselem, *nextfselem;
2913 char newname[ZFS_MAX_DATASET_NAME_LEN];
2916 boolean_t needagain, progress, recursive;
2919 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2921 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2928 needagain = progress = B_FALSE;
2930 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2932 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2933 recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE,
2934 B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
2938 * Process deletes and renames
2940 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2941 fselem; fselem = nextfselem) {
2942 nvlist_t *nvfs, *snaps;
2943 nvlist_t *stream_nvfs = NULL;
2944 nvpair_t *snapelem, *nextsnapelem;
2945 uint64_t fromguid = 0;
2946 uint64_t originguid = 0;
2947 uint64_t stream_originguid = 0;
2948 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2949 char *fsname, *stream_fsname;
2951 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2953 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2954 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2955 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2956 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2957 &parent_fromsnap_guid));
2958 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2961 * First find the stream's fs, so we can check for
2962 * a different origin (due to "zfs promote")
2964 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2965 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2968 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2969 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2971 if (stream_nvfs != NULL)
2975 /* check for promote */
2976 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2977 &stream_originguid);
2978 if (stream_nvfs && originguid != stream_originguid) {
2979 switch (created_before(hdl, local_avl,
2980 stream_originguid, originguid)) {
2983 nvlist_t *origin_nvfs;
2984 char *origin_fsname;
2986 origin_nvfs = fsavl_find(local_avl, originguid,
2988 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2989 "name", &origin_fsname));
2990 error = recv_promote(hdl, fsname, origin_fsname,
2999 fsavl_destroy(local_avl);
3000 nvlist_free(local_nv);
3004 * We had/have the wrong origin, therefore our
3005 * list of snapshots is wrong. Need to handle
3006 * them on the next pass.
3012 for (snapelem = nvlist_next_nvpair(snaps, NULL);
3013 snapelem; snapelem = nextsnapelem) {
3015 char *stream_snapname;
3016 nvlist_t *found, *props;
3018 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
3020 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
3021 found = fsavl_find(stream_avl, thisguid,
3024 /* check for delete */
3025 if (found == NULL) {
3026 char name[ZFS_MAX_DATASET_NAME_LEN];
3031 (void) snprintf(name, sizeof (name), "%s@%s",
3032 fsname, nvpair_name(snapelem));
3034 error = recv_destroy(hdl, name,
3035 strlen(fsname)+1, newname, flags);
3040 sprintf(guidname, "%llu",
3041 (u_longlong_t)thisguid);
3042 nvlist_add_boolean(deleted, guidname);
3046 stream_nvfs = found;
3048 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
3049 &props) && 0 == nvlist_lookup_nvlist(props,
3050 stream_snapname, &props)) {
3051 zfs_cmd_t zc = {"\0"};
3053 zc.zc_cookie = B_TRUE; /* received */
3054 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
3055 "%s@%s", fsname, nvpair_name(snapelem));
3056 if (zcmd_write_src_nvlist(hdl, &zc,
3058 (void) zfs_ioctl(hdl,
3059 ZFS_IOC_SET_PROP, &zc);
3060 zcmd_free_nvlists(&zc);
3064 /* check for different snapname */
3065 if (strcmp(nvpair_name(snapelem),
3066 stream_snapname) != 0) {
3067 char name[ZFS_MAX_DATASET_NAME_LEN];
3068 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3070 (void) snprintf(name, sizeof (name), "%s@%s",
3071 fsname, nvpair_name(snapelem));
3072 (void) snprintf(tryname, sizeof (name), "%s@%s",
3073 fsname, stream_snapname);
3075 error = recv_rename(hdl, name, tryname,
3076 strlen(fsname)+1, newname, flags);
3083 if (strcmp(stream_snapname, fromsnap) == 0)
3084 fromguid = thisguid;
3087 /* check for delete */
3088 if (stream_nvfs == NULL) {
3092 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
3098 sprintf(guidname, "%llu",
3099 (u_longlong_t)parent_fromsnap_guid);
3100 nvlist_add_boolean(deleted, guidname);
3104 if (fromguid == 0) {
3105 if (flags->verbose) {
3106 (void) printf("local fs %s does not have "
3107 "fromsnap (%s in stream); must have "
3108 "been deleted locally; ignoring\n",
3114 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
3115 "name", &stream_fsname));
3116 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
3117 "parentfromsnap", &stream_parent_fromsnap_guid));
3119 s1 = strrchr(fsname, '/');
3120 s2 = strrchr(stream_fsname, '/');
3123 * Check if we're going to rename based on parent guid change
3124 * and the current parent guid was also deleted. If it was then
3125 * rename will fail and is likely unneeded, so avoid this and
3126 * force an early retry to determine the new
3127 * parent_fromsnap_guid.
3129 if (stream_parent_fromsnap_guid != 0 &&
3130 parent_fromsnap_guid != 0 &&
3131 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
3132 sprintf(guidname, "%llu",
3133 (u_longlong_t)parent_fromsnap_guid);
3134 if (nvlist_exists(deleted, guidname)) {
3142 * Check for rename. If the exact receive path is specified, it
3143 * does not count as a rename, but we still need to check the
3144 * datasets beneath it.
3146 if ((stream_parent_fromsnap_guid != 0 &&
3147 parent_fromsnap_guid != 0 &&
3148 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
3149 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3150 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3152 char tryname[ZFS_MAX_DATASET_NAME_LEN];
3154 parent = fsavl_find(local_avl,
3155 stream_parent_fromsnap_guid, NULL);
3157 * NB: parent might not be found if we used the
3158 * tosnap for stream_parent_fromsnap_guid,
3159 * because the parent is a newly-created fs;
3160 * we'll be able to rename it after we recv the
3163 if (parent != NULL) {
3166 VERIFY(0 == nvlist_lookup_string(parent, "name",
3168 (void) snprintf(tryname, sizeof (tryname),
3169 "%s%s", pname, strrchr(stream_fsname, '/'));
3172 if (flags->verbose) {
3173 (void) printf("local fs %s new parent "
3174 "not found\n", fsname);
3180 error = recv_rename(hdl, fsname, tryname,
3181 strlen(tofs)+1, newname, flags);
3183 if (renamed != NULL && newname[0] != '\0') {
3184 VERIFY(0 == nvlist_add_boolean(renamed,
3196 fsavl_destroy(local_avl);
3197 nvlist_free(local_nv);
3198 nvlist_free(deleted);
3200 if (needagain && progress) {
3201 /* do another pass to fix up temporary names */
3203 (void) printf("another pass:\n");
3207 return (needagain || error != 0);
3211 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
3212 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3213 char **top_zfs, int cleanup_fd, uint64_t *action_handlep,
3216 nvlist_t *stream_nv = NULL;
3217 avl_tree_t *stream_avl = NULL;
3218 char *fromsnap = NULL;
3219 char *sendsnap = NULL;
3221 char tofs[ZFS_MAX_DATASET_NAME_LEN];
3222 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
3224 dmu_replay_record_t drre;
3226 boolean_t anyerr = B_FALSE;
3227 boolean_t softerr = B_FALSE;
3228 boolean_t recursive, raw;
3230 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3233 assert(drr->drr_type == DRR_BEGIN);
3234 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
3235 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3236 DMU_COMPOUNDSTREAM);
3239 * Read in the nvlist from the stream.
3241 if (drr->drr_payloadlen != 0) {
3242 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
3243 &stream_nv, flags->byteswap, zc);
3245 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3250 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3252 raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3254 if (recursive && strchr(destname, '@')) {
3255 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3256 "cannot specify snapshot name for multi-snapshot stream"));
3257 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3262 * Read in the end record and verify checksum.
3264 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3265 flags->byteswap, NULL)))
3267 if (flags->byteswap) {
3268 drre.drr_type = BSWAP_32(drre.drr_type);
3269 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3270 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3271 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3272 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3273 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3274 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3275 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3276 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3278 if (drre.drr_type != DRR_END) {
3279 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3282 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3283 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3284 "incorrect header checksum"));
3285 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3289 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3291 if (drr->drr_payloadlen != 0) {
3292 nvlist_t *stream_fss;
3294 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
3296 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3297 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3298 "couldn't allocate avl tree"));
3299 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3303 if (fromsnap != NULL && recursive) {
3304 nvlist_t *renamed = NULL;
3305 nvpair_t *pair = NULL;
3307 (void) strlcpy(tofs, destname, sizeof (tofs));
3308 if (flags->isprefix) {
3309 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3312 if (flags->istail) {
3313 cp = strrchr(drrb->drr_toname, '/');
3315 (void) strlcat(tofs, "/",
3319 i = (cp - drrb->drr_toname);
3322 i = strcspn(drrb->drr_toname, "/@");
3324 /* zfs_receive_one() will create_parents() */
3325 (void) strlcat(tofs, &drrb->drr_toname[i],
3327 *strchr(tofs, '@') = '\0';
3330 if (!flags->dryrun && !flags->nomount) {
3331 VERIFY(0 == nvlist_alloc(&renamed,
3332 NV_UNIQUE_NAME, 0));
3335 softerr = recv_incremental_replication(hdl, tofs, flags,
3336 stream_nv, stream_avl, renamed);
3338 /* Unmount renamed filesystems before receiving. */
3339 while ((pair = nvlist_next_nvpair(renamed,
3342 prop_changelist_t *clp = NULL;
3344 zhp = zfs_open(hdl, nvpair_name(pair),
3345 ZFS_TYPE_FILESYSTEM);
3347 clp = changelist_gather(zhp,
3348 ZFS_PROP_MOUNTPOINT, 0, 0);
3352 changelist_prefix(clp);
3353 changelist_free(clp);
3358 nvlist_free(renamed);
3363 * Get the fs specified by the first path in the stream (the top level
3364 * specified by 'zfs send') and pass it to each invocation of
3365 * zfs_receive_one().
3367 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
3369 if ((cp = strchr(sendfs, '@')) != NULL) {
3372 * Find the "sendsnap", the final snapshot in a replication
3373 * stream. zfs_receive_one() handles certain errors
3374 * differently, depending on if the contained stream is the
3377 sendsnap = (cp + 1);
3380 /* Finally, receive each contained stream */
3383 * we should figure out if it has a recoverable
3384 * error, in which case do a recv_skip() and drive on.
3385 * Note, if we fail due to already having this guid,
3386 * zfs_receive_one() will take care of it (ie,
3387 * recv_skip() and return 0).
3389 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3390 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
3391 action_handlep, sendsnap, cmdprops);
3392 if (error == ENODATA) {
3397 } while (error == 0);
3399 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
3401 * Now that we have the fs's they sent us, try the
3404 softerr = recv_incremental_replication(hdl, tofs, flags,
3405 stream_nv, stream_avl, NULL);
3408 if (raw && softerr == 0) {
3409 softerr = recv_fix_encryption_hierarchy(hdl, destname,
3410 stream_nv, stream_avl);
3414 fsavl_destroy(stream_avl);
3415 nvlist_free(stream_nv);
3424 trunc_prop_errs(int truncated)
3426 ASSERT(truncated != 0);
3429 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3430 "1 more property could not be set\n"));
3432 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3433 "%d more properties could not be set\n"), truncated);
3437 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3439 dmu_replay_record_t *drr;
3440 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
3443 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3444 "cannot receive:"));
3446 /* XXX would be great to use lseek if possible... */
3449 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3450 byteswap, NULL) == 0) {
3452 drr->drr_type = BSWAP_32(drr->drr_type);
3454 switch (drr->drr_type) {
3456 if (drr->drr_payloadlen != 0) {
3457 (void) recv_read(hdl, fd, buf,
3458 drr->drr_payloadlen, B_FALSE, NULL);
3468 drr->drr_u.drr_object.drr_bonuslen =
3469 BSWAP_32(drr->drr_u.drr_object.
3472 (void) recv_read(hdl, fd, buf,
3473 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
3479 drr->drr_u.drr_write.drr_logical_size =
3481 drr->drr_u.drr_write.drr_logical_size);
3482 drr->drr_u.drr_write.drr_compressed_size =
3484 drr->drr_u.drr_write.drr_compressed_size);
3486 uint64_t payload_size =
3487 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3488 (void) recv_read(hdl, fd, buf,
3489 payload_size, B_FALSE, NULL);
3493 drr->drr_u.drr_spill.drr_length =
3494 BSWAP_64(drr->drr_u.drr_spill.drr_length);
3496 (void) recv_read(hdl, fd, buf,
3497 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
3499 case DRR_WRITE_EMBEDDED:
3501 drr->drr_u.drr_write_embedded.drr_psize =
3502 BSWAP_32(drr->drr_u.drr_write_embedded.
3505 (void) recv_read(hdl, fd, buf,
3506 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3509 case DRR_WRITE_BYREF:
3510 case DRR_FREEOBJECTS:
3515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3516 "invalid record type"));
3518 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3527 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3528 boolean_t resumable)
3530 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3532 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3533 "checksum mismatch or incomplete stream"));
3537 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3538 *strchr(target_fs, '@') = '\0';
3539 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3540 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3544 char token_buf[ZFS_MAXPROPLEN];
3545 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3546 token_buf, sizeof (token_buf),
3547 NULL, NULL, 0, B_TRUE);
3549 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3550 "checksum mismatch or incomplete stream.\n"
3551 "Partially received snapshot is saved.\n"
3552 "A resuming stream can be generated on the sending "
3553 "system by running:\n"
3561 * Prepare a new nvlist of properties that are to override (-o) or be excluded
3562 * (-x) from the received dataset
3563 * recvprops: received properties from the send stream
3564 * cmdprops: raw input properties from command line
3565 * origprops: properties, both locally-set and received, currently set on the
3566 * target dataset if it exists, NULL otherwise.
3567 * oxprops: valid output override (-o) and excluded (-x) properties
3570 zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
3571 char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
3572 boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
3573 nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
3574 uint_t *wkeylen_out, const char *errbuf)
3577 nvlist_t *oprops, *voprops;
3578 zfs_handle_t *zhp = NULL;
3579 zpool_handle_t *zpool_hdl = NULL;
3582 char namebuf[ZFS_MAX_DATASET_NAME_LEN];
3584 if (nvlist_empty(cmdprops))
3585 return (0); /* No properties to override or exclude */
3587 *oxprops = fnvlist_alloc();
3588 oprops = fnvlist_alloc();
3590 strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
3593 * Get our dataset handle. The target dataset may not exist yet.
3595 if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
3596 zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
3603 /* open the zpool handle */
3604 cp = strchr(namebuf, '/');
3607 zpool_hdl = zpool_open(hdl, namebuf);
3608 if (zpool_hdl == NULL) {
3613 /* restore namebuf to match fsname for later use */
3618 * first iteration: process excluded (-x) properties now and gather
3619 * added (-o) properties to be later processed by zfs_valid_proplist()
3622 while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
3623 const char *name = nvpair_name(nvp);
3624 zfs_prop_t prop = zfs_name_to_prop(name);
3626 /* "origin" is processed separately, don't handle it here */
3627 if (prop == ZFS_PROP_ORIGIN)
3631 * we're trying to override or exclude a property that does not
3632 * make sense for this type of dataset, but we don't want to
3633 * fail if the receive is recursive: this comes in handy when
3634 * the send stream contains, for instance, a child ZVOL and
3635 * we're trying to receive it with "-o atime=on"
3637 if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
3638 !zfs_prop_user(name)) {
3641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3642 "property '%s' does not apply to datasets of this "
3644 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3648 /* raw streams can't override encryption properties */
3649 if ((zfs_prop_encryption_key_param(prop) ||
3650 prop == ZFS_PROP_ENCRYPTION) && (raw || !newfs)) {
3651 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3652 "encryption property '%s' cannot "
3653 "be set or excluded for raw or incremental "
3655 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3659 switch (nvpair_type(nvp)) {
3660 case DATA_TYPE_BOOLEAN: /* -x property */
3662 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
3663 * a property: this is done by forcing an explicit
3664 * inherit on the destination so the effective value is
3665 * not the one we received from the send stream.
3666 * We do this only if the property is not already
3667 * locally-set, in which case its value will take
3668 * priority over the received anyway.
3670 if (nvlist_exists(origprops, name)) {
3673 attrs = fnvlist_lookup_nvlist(origprops, name);
3674 if (strcmp(fnvlist_lookup_string(attrs,
3675 ZPROP_SOURCE), ZPROP_SOURCE_VAL_RECVD) != 0)
3679 * We can't force an explicit inherit on non-inheritable
3680 * properties: if we're asked to exclude this kind of
3681 * values we remove them from "recvprops" input nvlist.
3683 if (!zfs_prop_inheritable(prop) &&
3684 !zfs_prop_user(name) && /* can be inherited too */
3685 nvlist_exists(recvprops, name))
3686 fnvlist_remove(recvprops, name);
3688 fnvlist_add_nvpair(*oxprops, nvp);
3690 case DATA_TYPE_STRING: /* -o property=value */
3691 fnvlist_add_nvpair(oprops, nvp);
3694 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3695 "property '%s' must be a string or boolean"), name);
3696 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3702 /* convert override strings properties to native */
3703 if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
3704 oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
3705 ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3710 * zfs_crypto_create() requires the parent name. Get it
3711 * by truncating the fsname copy stored in namebuf.
3713 cp = strrchr(namebuf, '/');
3717 if (!raw && zfs_crypto_create(hdl, namebuf, voprops, NULL,
3718 B_FALSE, wkeydata_out, wkeylen_out) != 0) {
3719 fnvlist_free(voprops);
3720 ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
3724 /* second pass: process "-o" properties */
3725 fnvlist_merge(*oxprops, voprops);
3726 fnvlist_free(voprops);
3728 /* override props on child dataset are inherited */
3730 while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
3731 const char *name = nvpair_name(nvp);
3732 fnvlist_add_boolean(*oxprops, name);
3739 if (zpool_hdl != NULL)
3740 zpool_close(zpool_hdl);
3741 fnvlist_free(oprops);
3746 * Restores a backup of tosnap from the file descriptor specified by infd.
3749 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3750 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3751 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3752 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3753 uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops)
3756 int ioctl_err, ioctl_errno, err;
3758 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3760 const char *chopprefix;
3761 boolean_t newfs = B_FALSE;
3762 boolean_t stream_wantsnewfs;
3763 boolean_t newprops = B_FALSE;
3764 uint64_t read_bytes = 0;
3765 uint64_t errflags = 0;
3766 uint64_t parent_snapguid = 0;
3767 prop_changelist_t *clp = NULL;
3768 nvlist_t *snapprops_nvlist = NULL;
3769 nvlist_t *snapholds_nvlist = NULL;
3770 zprop_errflags_t prop_errflags;
3771 nvlist_t *prop_errors = NULL;
3772 boolean_t recursive;
3773 char *snapname = NULL;
3774 char destsnap[MAXPATHLEN * 2];
3775 char origin[MAXNAMELEN];
3776 char name[MAXPATHLEN];
3777 char tmp_keylocation[MAXNAMELEN];
3778 nvlist_t *rcvprops = NULL; /* props received from the send stream */
3779 nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
3780 nvlist_t *origprops = NULL; /* original props (if destination exists) */
3782 boolean_t toplevel = B_FALSE;
3783 boolean_t zoned = B_FALSE;
3784 boolean_t hastoken = B_FALSE;
3785 uint8_t *wkeydata = NULL;
3788 begin_time = time(NULL);
3789 bzero(origin, MAXNAMELEN);
3790 bzero(tmp_keylocation, MAXNAMELEN);
3792 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3795 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3798 /* Did the user request holds be skipped via zfs recv -k? */
3799 boolean_t holds = flags->holds && !flags->skipholds;
3801 if (stream_avl != NULL) {
3802 char *keylocation = NULL;
3803 nvlist_t *lookup = NULL;
3804 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3807 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
3809 err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
3811 VERIFY(0 == nvlist_alloc(&rcvprops, NV_UNIQUE_NAME, 0));
3816 * The keylocation property may only be set on encryption roots,
3817 * but this dataset might not become an encryption root until
3818 * recv_fix_encryption_hierarchy() is called. That function
3819 * will fixup the keylocation anyway, so we temporarily unset
3820 * the keylocation for now to avoid any errors from the receive
3823 err = nvlist_lookup_string(rcvprops,
3824 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
3826 strcpy(tmp_keylocation, keylocation);
3827 (void) nvlist_remove_all(rcvprops,
3828 zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
3831 if (flags->canmountoff) {
3832 VERIFY(0 == nvlist_add_uint64(rcvprops,
3833 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3834 } else if (newprops) { /* nothing in rcvprops, eliminate it */
3835 nvlist_free(rcvprops);
3839 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
3840 VERIFY(0 == nvlist_lookup_nvlist(lookup,
3841 snapname, &snapprops_nvlist));
3844 if (0 == nvlist_lookup_nvlist(fs, "snapholds",
3846 VERIFY(0 == nvlist_lookup_nvlist(lookup,
3847 snapname, &snapholds_nvlist));
3855 * Determine how much of the snapshot name stored in the stream
3856 * we are going to tack on to the name they specified on the
3857 * command line, and how much we are going to chop off.
3859 * If they specified a snapshot, chop the entire name stored in
3862 if (flags->istail) {
3864 * A filesystem was specified with -e. We want to tack on only
3865 * the tail of the sent snapshot path.
3867 if (strchr(tosnap, '@')) {
3868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3869 "argument - snapshot not allowed with -e"));
3870 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3874 chopprefix = strrchr(sendfs, '/');
3876 if (chopprefix == NULL) {
3878 * The tail is the poolname, so we need to
3879 * prepend a path separator.
3881 int len = strlen(drrb->drr_toname);
3882 cp = malloc(len + 2);
3884 (void) strcpy(&cp[1], drrb->drr_toname);
3887 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3889 } else if (flags->isprefix) {
3891 * A filesystem was specified with -d. We want to tack on
3892 * everything but the first element of the sent snapshot path
3893 * (all but the pool name).
3895 if (strchr(tosnap, '@')) {
3896 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3897 "argument - snapshot not allowed with -d"));
3898 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3902 chopprefix = strchr(drrb->drr_toname, '/');
3903 if (chopprefix == NULL)
3904 chopprefix = strchr(drrb->drr_toname, '@');
3905 } else if (strchr(tosnap, '@') == NULL) {
3907 * If a filesystem was specified without -d or -e, we want to
3908 * tack on everything after the fs specified by 'zfs send'.
3910 chopprefix = drrb->drr_toname + strlen(sendfs);
3912 /* A snapshot was specified as an exact path (no -d or -e). */
3914 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3915 "cannot specify snapshot name for multi-snapshot "
3917 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3920 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3923 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3924 ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
3925 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
3926 strchr(sendfs, '/') == NULL);
3927 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3928 chopprefix[0] == '\0');
3931 * Determine name of destination snapshot.
3933 (void) strlcpy(destsnap, tosnap, sizeof (destsnap));
3934 (void) strlcat(destsnap, chopprefix, sizeof (destsnap));
3936 if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
3937 err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3942 * Determine the name of the origin snapshot.
3945 (void) strlcpy(origin, originsnap, sizeof (origin));
3947 (void) printf("using provided clone origin %s\n",
3949 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3950 if (guid_to_name(hdl, destsnap,
3951 drrb->drr_fromguid, B_FALSE, origin) != 0) {
3952 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3953 "local origin for clone %s does not exist"),
3955 err = zfs_error(hdl, EZFS_NOENT, errbuf);
3959 (void) printf("found clone origin %s\n", origin);
3962 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3963 DMU_BACKUP_FEATURE_RESUMING;
3964 boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3965 DMU_BACKUP_FEATURE_RAW;
3966 boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3967 DMU_BACKUP_FEATURE_EMBED_DATA;
3968 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
3969 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3971 if (stream_wantsnewfs) {
3973 * if the parent fs does not exist, look for it based on
3974 * the parent snap GUID
3976 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3977 "cannot receive new filesystem stream"));
3979 (void) strcpy(name, destsnap);
3980 cp = strrchr(name, '/');
3984 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
3985 char suffix[ZFS_MAX_DATASET_NAME_LEN];
3986 (void) strcpy(suffix, strrchr(destsnap, '/'));
3987 if (guid_to_name(hdl, name, parent_snapguid,
3988 B_FALSE, destsnap) == 0) {
3989 *strchr(destsnap, '@') = '\0';
3990 (void) strcat(destsnap, suffix);
3995 * if the fs does not exist, look for it based on the
3998 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3999 "cannot receive incremental stream"));
4001 (void) strcpy(name, destsnap);
4002 *strchr(name, '@') = '\0';
4005 * If the exact receive path was specified and this is the
4006 * topmost path in the stream, then if the fs does not exist we
4007 * should look no further.
4009 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
4010 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
4011 !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4012 char snap[ZFS_MAX_DATASET_NAME_LEN];
4013 (void) strcpy(snap, strchr(destsnap, '@'));
4014 if (guid_to_name(hdl, name, drrb->drr_fromguid,
4015 B_FALSE, destsnap) == 0) {
4016 *strchr(destsnap, '@') = '\0';
4017 (void) strcat(destsnap, snap);
4022 (void) strcpy(name, destsnap);
4023 *strchr(name, '@') = '\0';
4025 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4026 zfs_cmd_t zc = {"\0"};
4028 boolean_t encrypted;
4030 (void) strcpy(zc.zc_name, name);
4033 * Destination fs exists. It must be one of these cases:
4034 * - an incremental send stream
4035 * - the stream specifies a new fs (full stream or clone)
4036 * and they want us to blow away the existing fs (and
4037 * have therefore specified -F and removed any snapshots)
4038 * - we are resuming a failed receive.
4040 if (stream_wantsnewfs) {
4041 boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL;
4042 if (!flags->force) {
4043 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4044 "destination '%s' exists\n"
4045 "must specify -F to overwrite it"), name);
4046 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4049 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
4051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4052 "destination has snapshots (eg. %s)\n"
4053 "must destroy them to overwrite it"),
4055 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4058 if (is_volume && strrchr(name, '/') == NULL) {
4059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4060 "destination %s is the root dataset\n"
4061 "cannot overwrite with a ZVOL"),
4063 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4067 ioctl(hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT,
4069 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4070 "destination has children (eg. %s)\n"
4071 "cannot overwrite with a ZVOL"),
4073 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4078 if ((zhp = zfs_open(hdl, name,
4079 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
4084 if (stream_wantsnewfs &&
4085 zhp->zfs_dmustats.dds_origin[0]) {
4087 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4088 "destination '%s' is a clone\n"
4089 "must destroy it to overwrite it"), name);
4090 err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4095 * Raw sends can not be performed as an incremental on top
4096 * of existing unencryppted datasets. zfs recv -F cant be
4097 * used to blow away an existing encrypted filesystem. This
4098 * is because it would require the dsl dir to point to the
4099 * new key (or lack of a key) and the old key at the same
4100 * time. The -F flag may still be used for deleting
4101 * intermediate snapshots that would otherwise prevent the
4102 * receive from working.
4104 encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
4106 if (!stream_wantsnewfs && !encrypted && raw) {
4108 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4109 "cannot perform raw receive on top of "
4110 "existing unencrypted dataset"));
4111 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4115 if (stream_wantsnewfs && flags->force &&
4116 ((raw && !encrypted) || encrypted)) {
4118 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4119 "zfs receive -F cannot be used to destroy an "
4120 "encrypted filesystem or overwrite an "
4121 "unencrypted one with an encrypted one"));
4122 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4126 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4127 stream_wantsnewfs) {
4128 /* We can't do online recv in this case */
4129 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
4135 if (changelist_prefix(clp) != 0) {
4136 changelist_free(clp);
4144 * If we are resuming a newfs, set newfs here so that we will
4145 * mount it if the recv succeeds this time. We can tell
4146 * that it was a newfs on the first recv because the fs
4147 * itself will be inconsistent (if the fs existed when we
4148 * did the first recv, we would have received it into
4151 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4154 /* we want to know if we're zoned when validating -o|-x props */
4155 zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4157 /* may need this info later, get it now we have zhp around */
4158 if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4159 NULL, NULL, 0, B_TRUE) == 0)
4162 /* gather existing properties on destination */
4163 origprops = fnvlist_alloc();
4164 fnvlist_merge(origprops, zhp->zfs_props);
4165 fnvlist_merge(origprops, zhp->zfs_user_props);
4172 * Destination filesystem does not exist. Therefore we better
4173 * be creating a new filesystem (either from a full backup, or
4174 * a clone). It would therefore be invalid if the user
4175 * specified only the pool name (i.e. if the destination name
4176 * contained no slash character).
4178 cp = strrchr(name, '/');
4180 if (!stream_wantsnewfs || cp == NULL) {
4181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4182 "destination '%s' does not exist"), name);
4183 err = zfs_error(hdl, EZFS_NOENT, errbuf);
4188 * Trim off the final dataset component so we perform the
4189 * recvbackup ioctl to the filesystems's parent.
4193 if (flags->isprefix && !flags->istail && !flags->dryrun &&
4194 create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4195 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4199 /* validate parent */
4200 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4202 err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4205 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
4206 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4207 "parent '%s' is not a filesystem"), name);
4208 err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4214 * It is invalid to receive a properties stream that was
4215 * unencrypted on the send side as a child of an encrypted
4216 * parent. Technically there is nothing preventing this, but
4217 * it would mean that the encryption=off property which is
4218 * locally set on the send side would not be received correctly.
4219 * We can infer encryption=off if the stream is not raw and
4220 * properties were included since the send side will only ever
4221 * send the encryption property in a raw nvlist header. This
4222 * check will be avoided if the user specifically overrides
4223 * the encryption property on the command line.
4225 if (!raw && rcvprops != NULL &&
4226 !nvlist_exists(cmdprops,
4227 zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
4230 crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
4232 if (crypt != ZIO_CRYPT_OFF) {
4233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4234 "parent '%s' must not be encrypted to "
4235 "receive unenecrypted property"), name);
4236 err = zfs_error(hdl, EZFS_BADPROP, errbuf);
4247 if (flags->verbose) {
4248 (void) printf("%s %s stream of %s into %s\n",
4249 flags->dryrun ? "would receive" : "receiving",
4250 drrb->drr_fromguid ? "incremental" : "full",
4251 drrb->drr_toname, destsnap);
4252 (void) fflush(stdout);
4255 if (flags->dryrun) {
4256 err = recv_skip(hdl, infd, flags->byteswap);
4260 if (top_zfs && (*top_zfs == NULL || strcmp(*top_zfs, name) == 0))
4262 if (drrb->drr_type == DMU_OST_ZVOL) {
4263 type = ZFS_TYPE_VOLUME;
4264 } else if (drrb->drr_type == DMU_OST_ZFS) {
4265 type = ZFS_TYPE_FILESYSTEM;
4267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4268 "invalid record type: 0x%d"), drrb->drr_type);
4269 err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4272 if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4273 stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4274 &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
4277 err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
4278 oxprops, wkeydata, wkeylen, origin, flags->force, flags->resumable,
4279 raw, infd, drr_noswap, cleanup_fd, &read_bytes, &errflags,
4280 action_handlep, &prop_errors);
4281 ioctl_errno = ioctl_err;
4282 prop_errflags = errflags;
4285 nvpair_t *prop_err = NULL;
4287 while ((prop_err = nvlist_next_nvpair(prop_errors,
4288 prop_err)) != NULL) {
4293 prop = zfs_name_to_prop(nvpair_name(prop_err));
4294 (void) nvpair_value_int32(prop_err, &intval);
4295 if (strcmp(nvpair_name(prop_err),
4296 ZPROP_N_MORE_ERRORS) == 0) {
4297 trunc_prop_errs(intval);
4299 } else if (snapname == NULL || finalsnap == NULL ||
4300 strcmp(finalsnap, snapname) == 0 ||
4301 strcmp(nvpair_name(prop_err),
4302 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
4304 * Skip the special case of, for example,
4305 * "refquota", errors on intermediate
4306 * snapshots leading up to a final one.
4307 * That's why we have all of the checks above.
4309 * See zfs_ioctl.c's extract_delay_props() for
4310 * a list of props which can fail on
4311 * intermediate snapshots, but shouldn't
4312 * affect the overall receive.
4314 (void) snprintf(tbuf, sizeof (tbuf),
4315 dgettext(TEXT_DOMAIN,
4316 "cannot receive %s property on %s"),
4317 nvpair_name(prop_err), name);
4318 zfs_setprop_error(hdl, prop, intval, tbuf);
4323 if (err == 0 && snapprops_nvlist) {
4324 zfs_cmd_t zc = {"\0"};
4326 (void) strcpy(zc.zc_name, destsnap);
4327 zc.zc_cookie = B_TRUE; /* received */
4328 if (zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist) == 0) {
4329 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
4330 zcmd_free_nvlists(&zc);
4333 if (err == 0 && snapholds_nvlist) {
4335 nvlist_t *holds, *errors = NULL;
4336 int cleanup_fd = -1;
4338 VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
4339 for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
4341 pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
4342 VERIFY(0 == nvlist_add_string(holds, destsnap,
4343 nvpair_name(pair)));
4345 (void) lzc_hold(holds, cleanup_fd, &errors);
4346 nvlist_free(snapholds_nvlist);
4350 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
4352 * It may be that this snapshot already exists,
4353 * in which case we want to consume & ignore it
4354 * rather than failing.
4356 avl_tree_t *local_avl;
4357 nvlist_t *local_nv, *fs;
4358 cp = strchr(destsnap, '@');
4361 * XXX Do this faster by just iterating over snaps in
4362 * this fs. Also if zc_value does not exist, we will
4363 * get a strange "does not exist" error message.
4366 if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
4367 B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_TRUE,
4368 &local_nv, &local_avl) == 0) {
4370 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
4371 fsavl_destroy(local_avl);
4372 nvlist_free(local_nv);
4375 if (flags->verbose) {
4376 (void) printf("snap %s already exists; "
4377 "ignoring\n", destsnap);
4379 err = ioctl_err = recv_skip(hdl, infd,
4386 if (ioctl_err != 0) {
4387 switch (ioctl_errno) {
4389 cp = strchr(destsnap, '@');
4391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4392 "most recent snapshot of %s does not\n"
4393 "match incremental source"), destsnap);
4394 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4399 "destination %s has been modified\n"
4400 "since most recent snapshot"), name);
4401 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4404 if (raw && stream_wantsnewfs) {
4405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4406 "failed to create encryption key"));
4407 } else if (raw && !stream_wantsnewfs) {
4408 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4409 "encryption key does not match "
4412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4413 "inherited key must be loaded"));
4415 (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4418 cp = strchr(destsnap, '@');
4420 /* it's the containing fs that exists */
4423 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4424 "destination already exists"));
4425 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
4426 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
4431 if (flags->resumable)
4432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4433 "kernel modules must be upgraded to "
4434 "receive this stream."));
4435 if (embedded && !raw)
4436 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4437 "incompatible embedded data stream "
4438 "feature with encrypted receive."));
4439 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4442 recv_ecksum_set_aux(hdl, destsnap, flags->resumable);
4443 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4447 "pool must be upgraded to receive this stream."));
4448 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4452 "destination %s space quota exceeded."), name);
4453 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
4455 case ZFS_ERR_FROM_IVSET_GUID_MISSING:
4456 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4457 "IV set guid missing. See errata %u at "
4458 "http://zfsonlinux.org/msg/ZFS-8000-ER."),
4459 ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
4460 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4462 case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
4463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4464 "IV set guid mismatch. See the 'zfs receive' "
4465 "man page section\n discussing the limitations "
4466 "of raw encrypted send streams."));
4467 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4471 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4472 "destination %s contains "
4473 "partially-complete state from "
4474 "\"zfs receive -s\"."), name);
4475 (void) zfs_error(hdl, EZFS_BUSY, errbuf);
4480 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
4485 * Mount the target filesystem (if created). Also mount any
4486 * children of the target filesystem if we did a replication
4487 * receive (indicated by stream_avl being non-NULL).
4489 cp = strchr(destsnap, '@');
4490 if (cp && (ioctl_err == 0 || !newfs)) {
4494 h = zfs_open(hdl, destsnap,
4495 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4497 if (h->zfs_type == ZFS_TYPE_VOLUME) {
4499 } else if (newfs || stream_avl) {
4501 * Track the first/top of hierarchy fs,
4502 * for mounting and sharing later.
4504 if (top_zfs && *top_zfs == NULL)
4505 *top_zfs = zfs_strdup(hdl, destsnap);
4513 if (!flags->nomount)
4514 err |= changelist_postfix(clp);
4515 changelist_free(clp);
4518 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
4519 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4520 "failed to clear unreceived properties on %s"), name);
4521 (void) fprintf(stderr, "\n");
4523 if (prop_errflags & ZPROP_ERR_NORESTORE) {
4524 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4525 "failed to restore original properties on %s"), name);
4526 (void) fprintf(stderr, "\n");
4529 if (err || ioctl_err) {
4534 if (flags->verbose) {
4537 uint64_t bytes = read_bytes;
4538 time_t delta = time(NULL) - begin_time;
4541 zfs_nicebytes(bytes, buf1, sizeof (buf1));
4542 zfs_nicebytes(bytes/delta, buf2, sizeof (buf1));
4544 (void) printf("received %s stream in %lu seconds (%s/sec)\n",
4550 if (prop_errors != NULL)
4551 nvlist_free(prop_errors);
4553 if (tmp_keylocation[0] != '\0') {
4554 VERIFY(0 == nvlist_add_string(rcvprops,
4555 zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation));
4559 nvlist_free(rcvprops);
4561 nvlist_free(oxprops);
4562 nvlist_free(origprops);
4568 * Check properties we were asked to override (both -o|-x)
4571 zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
4579 while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
4580 name = nvpair_name(nvp);
4581 prop = zfs_name_to_prop(name);
4583 if (prop == ZPROP_INVAL) {
4584 if (!zfs_prop_user(name)) {
4585 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4586 "invalid property '%s'"), name);
4592 * "origin" is readonly but is used to receive datasets as
4593 * clones so we don't raise an error here
4595 if (prop == ZFS_PROP_ORIGIN)
4598 /* encryption params have their own verification later */
4599 if (prop == ZFS_PROP_ENCRYPTION ||
4600 zfs_prop_encryption_key_param(prop))
4604 * cannot override readonly, set-once and other specific
4605 * settable properties
4607 if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
4608 prop == ZFS_PROP_VOLSIZE) {
4609 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4610 "invalid property '%s'"), name);
4619 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
4620 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
4621 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
4622 uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops)
4625 dmu_replay_record_t drr, drr_noswap;
4626 struct drr_begin *drrb = &drr.drr_u.drr_begin;
4628 zio_cksum_t zcksum = { { 0 } };
4629 uint64_t featureflags;
4632 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4635 /* check cmdline props, raise an error if they cannot be received */
4636 if (!zfs_receive_checkprops(hdl, cmdprops, errbuf)) {
4637 return (zfs_error(hdl, EZFS_BADPROP, errbuf));
4640 if (flags->isprefix &&
4641 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
4642 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
4643 "(%s) does not exist"), tosnap);
4644 return (zfs_error(hdl, EZFS_NOENT, errbuf));
4647 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
4648 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
4649 "(%s) does not exist"), originsnap);
4650 return (zfs_error(hdl, EZFS_NOENT, errbuf));
4653 /* read in the BEGIN record */
4654 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
4658 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
4659 /* It's the double end record at the end of a package */
4663 /* the kernel needs the non-byteswapped begin record */
4666 flags->byteswap = B_FALSE;
4667 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
4669 * We computed the checksum in the wrong byteorder in
4670 * recv_read() above; do it again correctly.
4672 bzero(&zcksum, sizeof (zio_cksum_t));
4673 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
4674 flags->byteswap = B_TRUE;
4676 drr.drr_type = BSWAP_32(drr.drr_type);
4677 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
4678 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
4679 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
4680 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
4681 drrb->drr_type = BSWAP_32(drrb->drr_type);
4682 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
4683 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
4684 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
4687 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
4688 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4689 "stream (bad magic number)"));
4690 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4693 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
4694 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
4696 if (!DMU_STREAM_SUPPORTED(featureflags) ||
4697 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
4698 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4699 "stream has unsupported feature, feature flags = %lx"),
4701 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4704 /* Holds feature is set once in the compound stream header. */
4705 boolean_t holds = (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4706 DMU_BACKUP_FEATURE_HOLDS);
4708 flags->holds = B_TRUE;
4710 if (strchr(drrb->drr_toname, '@') == NULL) {
4711 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4712 "stream (bad snapshot name)"));
4713 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4716 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
4717 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
4718 if (sendfs == NULL) {
4720 * We were not called from zfs_receive_package(). Get
4721 * the fs specified by 'zfs send'.
4724 (void) strlcpy(nonpackage_sendfs,
4725 drr.drr_u.drr_begin.drr_toname,
4726 sizeof (nonpackage_sendfs));
4727 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
4729 sendfs = nonpackage_sendfs;
4730 VERIFY(finalsnap == NULL);
4732 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
4733 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
4734 cleanup_fd, action_handlep, finalsnap, cmdprops));
4736 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
4737 DMU_COMPOUNDSTREAM);
4738 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
4739 &zcksum, top_zfs, cleanup_fd, action_handlep, cmdprops));
4744 * Restores a backup of tosnap from the file descriptor specified by infd.
4745 * Return 0 on total success, -2 if some things couldn't be
4746 * destroyed/renamed/promoted, -1 if some things couldn't be received.
4747 * (-1 will override -2, if -1 and the resumable flag was specified the
4748 * transfer can be resumed if the sending side supports it).
4751 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
4752 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
4754 char *top_zfs = NULL;
4757 uint64_t action_handle = 0;
4759 char *originsnap = NULL;
4762 * The only way fstat can fail is if we do not have a valid file
4765 if (fstat(infd, &sb) == -1) {
4771 #ifndef F_SETPIPE_SZ
4772 #define F_SETPIPE_SZ (F_SETLEASE + 7)
4773 #endif /* F_SETPIPE_SZ */
4775 #ifndef F_GETPIPE_SZ
4776 #define F_GETPIPE_SZ (F_GETLEASE + 7)
4777 #endif /* F_GETPIPE_SZ */
4780 * It is not uncommon for gigabytes to be processed in zfs receive.
4781 * Speculatively increase the buffer size via Linux-specific fcntl()
4784 if (S_ISFIFO(sb.st_mode)) {
4785 FILE *procf = fopen("/proc/sys/fs/pipe-max-size", "r");
4787 if (procf != NULL) {
4788 unsigned long max_psize;
4790 if (fscanf(procf, "%lu", &max_psize) > 0) {
4791 cur_psize = fcntl(infd, F_GETPIPE_SZ);
4792 if (cur_psize > 0 &&
4793 max_psize > (unsigned long) cur_psize)
4794 (void) fcntl(infd, F_SETPIPE_SZ,
4800 #endif /* __linux__ */
4803 err = nvlist_lookup_string(props, "origin", &originsnap);
4804 if (err && err != ENOENT)
4808 cleanup_fd = open(ZFS_DEV, O_RDWR);
4809 VERIFY(cleanup_fd >= 0);
4811 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
4812 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL, props);
4814 VERIFY(0 == close(cleanup_fd));
4816 if (err == 0 && !flags->nomount && top_zfs) {
4817 zfs_handle_t *zhp = NULL;
4818 prop_changelist_t *clp = NULL;
4820 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
4822 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
4823 CL_GATHER_MOUNT_ALWAYS, 0);
4826 /* mount and share received datasets */
4827 err = changelist_postfix(clp);
4828 changelist_free(clp);
4831 if (zhp == NULL || clp == NULL || err)