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. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
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/param.h>
46 #include <sys/mount.h>
52 #include <libzfs_core.h>
54 #include "zfs_namecheck.h"
56 #include "zfs_fletcher.h"
57 #include "libzfs_impl.h"
60 #include <sys/zio_checksum.h>
64 extern int zfs_ioctl_version;
67 /* in libzfs_dataset.c */
68 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
69 /* We need to use something for ENODATA. */
72 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
73 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
74 uint64_t *, const char *);
75 static int guid_to_name(libzfs_handle_t *, const char *,
76 uint64_t, boolean_t, char *);
78 static const zio_cksum_t zero_cksum = { 0 };
80 typedef struct dedup_arg {
83 libzfs_handle_t *dedup_hdl;
86 typedef struct progress_arg {
89 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 (void) 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 (void) fletcher_4_incremental_native(
214 &drr->drr_u.drr_checksum.drr_checksum, sizeof (zio_cksum_t), zc);
215 if (write(outfd, drr, sizeof (*drr)) == -1)
217 if (payload_len != 0) {
218 (void) 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;
248 dmu_replay_record_t *drr = &thedrr;
252 zio_cksum_t stream_cksum;
253 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
257 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
258 SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
260 numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
263 * numbuckets must be a power of 2. Increase number to
264 * a power of 2 if necessary.
266 if (!ISP2(numbuckets))
267 numbuckets = 1 << high_order_bit(numbuckets);
269 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
270 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
271 NULL, NULL, NULL, NULL, NULL, 0);
272 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
273 ddt.numhashbits = high_order_bit(numbuckets) - 1;
274 ddt.ddt_full = B_FALSE;
276 outfd = dda->outputfd;
277 ofp = fdopen(dda->inputfd, "r");
278 while (ssread(drr, sizeof (*drr), ofp) != 0) {
281 * kernel filled in checksum, we are going to write same
282 * record, but need to regenerate checksum.
284 if (drr->drr_type != DRR_BEGIN) {
285 bzero(&drr->drr_u.drr_checksum.drr_checksum,
286 sizeof (drr->drr_u.drr_checksum.drr_checksum));
289 switch (drr->drr_type) {
292 struct drr_begin *drrb = &drr->drr_u.drr_begin;
295 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
297 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
299 /* set the DEDUP feature flag for this stream */
300 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
301 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
302 DMU_BACKUP_FEATURE_DEDUPPROPS);
303 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
305 if (drr->drr_payloadlen != 0) {
306 sz = drr->drr_payloadlen;
308 if (sz > SPA_MAXBLOCKSIZE) {
309 buf = zfs_realloc(dda->dedup_hdl, buf,
310 SPA_MAXBLOCKSIZE, sz);
312 (void) ssread(buf, sz, ofp);
316 if (dump_record(drr, buf, sz, &stream_cksum,
324 struct drr_end *drre = &drr->drr_u.drr_end;
325 /* use the recalculated checksum */
326 drre->drr_checksum = stream_cksum;
327 if (dump_record(drr, NULL, 0, &stream_cksum,
335 struct drr_object *drro = &drr->drr_u.drr_object;
336 if (drro->drr_bonuslen > 0) {
338 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
341 if (dump_record(drr, buf,
342 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
343 &stream_cksum, outfd) != 0)
350 struct drr_spill *drrs = &drr->drr_u.drr_spill;
351 (void) ssread(buf, drrs->drr_length, ofp);
352 if (dump_record(drr, buf, drrs->drr_length,
353 &stream_cksum, outfd) != 0)
358 case DRR_FREEOBJECTS:
360 if (dump_record(drr, NULL, 0, &stream_cksum,
368 struct drr_write *drrw = &drr->drr_u.drr_write;
370 uint64_t payload_size;
372 payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
373 (void) ssread(buf, payload_size, ofp);
376 * Use the existing checksum if it's dedup-capable,
377 * else calculate a SHA256 checksum for it.
380 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
382 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
384 zio_cksum_t tmpsha256;
387 SHA256Update(&ctx, buf, payload_size);
388 SHA256Final(&tmpsha256, &ctx);
389 drrw->drr_key.ddk_cksum.zc_word[0] =
390 BE_64(tmpsha256.zc_word[0]);
391 drrw->drr_key.ddk_cksum.zc_word[1] =
392 BE_64(tmpsha256.zc_word[1]);
393 drrw->drr_key.ddk_cksum.zc_word[2] =
394 BE_64(tmpsha256.zc_word[2]);
395 drrw->drr_key.ddk_cksum.zc_word[3] =
396 BE_64(tmpsha256.zc_word[3]);
397 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
398 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
401 dataref.ref_guid = drrw->drr_toguid;
402 dataref.ref_object = drrw->drr_object;
403 dataref.ref_offset = drrw->drr_offset;
405 if (ddt_update(dda->dedup_hdl, &ddt,
406 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
408 dmu_replay_record_t wbr_drr = {0};
409 struct drr_write_byref *wbr_drrr =
410 &wbr_drr.drr_u.drr_write_byref;
412 /* block already present in stream */
413 wbr_drr.drr_type = DRR_WRITE_BYREF;
415 wbr_drrr->drr_object = drrw->drr_object;
416 wbr_drrr->drr_offset = drrw->drr_offset;
417 wbr_drrr->drr_length = drrw->drr_logical_size;
418 wbr_drrr->drr_toguid = drrw->drr_toguid;
419 wbr_drrr->drr_refguid = dataref.ref_guid;
420 wbr_drrr->drr_refobject =
422 wbr_drrr->drr_refoffset =
425 wbr_drrr->drr_checksumtype =
426 drrw->drr_checksumtype;
427 wbr_drrr->drr_checksumflags =
428 drrw->drr_checksumtype;
429 wbr_drrr->drr_key.ddk_cksum =
430 drrw->drr_key.ddk_cksum;
431 wbr_drrr->drr_key.ddk_prop =
432 drrw->drr_key.ddk_prop;
434 if (dump_record(&wbr_drr, NULL, 0,
435 &stream_cksum, outfd) != 0)
438 /* block not previously seen */
439 if (dump_record(drr, buf, payload_size,
440 &stream_cksum, outfd) != 0)
446 case DRR_WRITE_EMBEDDED:
448 struct drr_write_embedded *drrwe =
449 &drr->drr_u.drr_write_embedded;
451 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
452 if (dump_record(drr, buf,
453 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
454 &stream_cksum, outfd) != 0)
461 if (dump_record(drr, NULL, 0, &stream_cksum,
468 (void) fprintf(stderr, "INVALID record type 0x%x\n",
470 /* should never happen, so assert */
475 umem_cache_destroy(ddt.ddecache);
476 free(ddt.dedup_hash_array);
484 * Routines for dealing with the AVL tree of fs-nvlists
486 typedef struct fsavl_node {
494 fsavl_compare(const void *arg1, const void *arg2)
496 const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
497 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
499 return (AVL_CMP(fn1->fn_guid, fn2->fn_guid));
503 * Given the GUID of a snapshot, find its containing filesystem and
507 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
509 fsavl_node_t fn_find;
512 fn_find.fn_guid = snapguid;
514 fn = avl_find(avl, &fn_find, NULL);
517 *snapname = fn->fn_snapname;
518 return (fn->fn_nvfs);
524 fsavl_destroy(avl_tree_t *avl)
533 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
540 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
543 fsavl_create(nvlist_t *fss)
546 nvpair_t *fselem = NULL;
548 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
551 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
552 offsetof(fsavl_node_t, fn_node));
554 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
555 nvlist_t *nvfs, *snaps;
556 nvpair_t *snapelem = NULL;
558 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
559 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
562 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
566 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
567 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
568 fsavl_destroy(fsavl);
572 fn->fn_snapname = nvpair_name(snapelem);
576 * Note: if there are multiple snaps with the
577 * same GUID, we ignore all but one.
579 if (avl_find(fsavl, fn, NULL) == NULL)
590 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
592 typedef struct send_data {
594 * assigned inside every recursive call,
595 * restored from *_save on return:
597 * guid of fromsnap snapshot in parent dataset
598 * txg of fromsnap snapshot in current dataset
599 * txg of tosnap snapshot in current dataset
602 uint64_t parent_fromsnap_guid;
603 uint64_t fromsnap_txg;
606 /* the nvlists get accumulated during depth-first traversal */
607 nvlist_t *parent_snaps;
611 /* send-receive configuration, does not change during traversal */
613 const char *fromsnap;
620 * The header nvlist is of the following format:
623 * "fromsnap" -> string (if incremental)
627 * "name" -> string (full name; for debugging)
628 * "parentfromsnap" -> number (guid of fromsnap in parent)
630 * "props" -> { name -> value (only if set here) }
631 * "snaps" -> { name (lastname) -> number (guid) }
632 * "snapprops" -> { name (lastname) -> { name -> value } }
634 * "origin" -> number (guid) (if clone)
635 * "sent" -> boolean (not on-disk)
643 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
646 send_iterate_snap(zfs_handle_t *zhp, void *arg)
648 send_data_t *sd = arg;
649 uint64_t guid = zhp->zfs_dmustats.dds_guid;
650 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
654 snapname = strrchr(zhp->zfs_name, '@')+1;
656 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
658 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
659 "skipping snapshot %s because it was created "
660 "after the destination snapshot (%s)\n"),
661 zhp->zfs_name, sd->tosnap);
667 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
669 * NB: if there is no fromsnap here (it's a newly created fs in
670 * an incremental replication), we will substitute the tosnap.
672 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
673 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
674 strcmp(snapname, sd->tosnap) == 0)) {
675 sd->parent_fromsnap_guid = guid;
678 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
679 send_iterate_prop(zhp, nv);
680 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
688 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
690 nvpair_t *elem = NULL;
692 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
693 char *propname = nvpair_name(elem);
694 zfs_prop_t prop = zfs_name_to_prop(propname);
697 if (!zfs_prop_user(propname)) {
699 * Realistically, this should never happen. However,
700 * we want the ability to add DSL properties without
701 * needing to make incompatible version changes. We
702 * need to ignore unknown properties to allow older
703 * software to still send datasets containing these
704 * properties, with the unknown properties elided.
706 if (prop == ZPROP_INVAL)
709 if (zfs_prop_readonly(prop))
713 verify(nvpair_value_nvlist(elem, &propnv) == 0);
714 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
715 prop == ZFS_PROP_REFQUOTA ||
716 prop == ZFS_PROP_REFRESERVATION) {
719 verify(nvlist_lookup_uint64(propnv,
720 ZPROP_VALUE, &value) == 0);
721 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
724 * May have no source before SPA_VERSION_RECVD_PROPS,
725 * but is still modifiable.
727 if (nvlist_lookup_string(propnv,
728 ZPROP_SOURCE, &source) == 0) {
729 if ((strcmp(source, zhp->zfs_name) != 0) &&
731 ZPROP_SOURCE_VAL_RECVD) != 0))
736 if (nvlist_lookup_string(propnv,
737 ZPROP_SOURCE, &source) != 0)
739 if ((strcmp(source, zhp->zfs_name) != 0) &&
740 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
744 if (zfs_prop_user(propname) ||
745 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
747 verify(nvlist_lookup_string(propnv,
748 ZPROP_VALUE, &value) == 0);
749 VERIFY(0 == nvlist_add_string(nv, propname, value));
752 verify(nvlist_lookup_uint64(propnv,
753 ZPROP_VALUE, &value) == 0);
754 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
760 * returns snapshot creation txg
761 * and returns 0 if the snapshot does not exist
764 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
766 char name[ZFS_MAX_DATASET_NAME_LEN];
769 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
772 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
773 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
774 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
776 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
785 * recursively generate nvlists describing datasets. See comment
786 * for the data structure send_data_t above for description of contents
790 send_iterate_fs(zfs_handle_t *zhp, void *arg)
792 send_data_t *sd = arg;
795 uint64_t min_txg = 0, max_txg = 0;
796 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
797 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
798 uint64_t tosnap_txg_save = sd->tosnap_txg;
799 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
800 uint64_t guid = zhp->zfs_dmustats.dds_guid;
801 uint64_t fromsnap_txg, tosnap_txg;
804 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
805 if (fromsnap_txg != 0)
806 sd->fromsnap_txg = fromsnap_txg;
808 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
810 sd->tosnap_txg = tosnap_txg;
813 * on the send side, if the current dataset does not have tosnap,
814 * perform two additional checks:
816 * - skip sending the current dataset if it was created later than
818 * - return error if the current dataset was created earlier than
821 if (sd->tosnap != NULL && tosnap_txg == 0) {
822 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
824 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
825 "skipping dataset %s: snapshot %s does "
826 "not exist\n"), zhp->zfs_name, sd->tosnap);
829 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
830 "cannot send %s@%s%s: snapshot %s@%s does not "
831 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
832 dgettext(TEXT_DOMAIN, " recursively") : "",
833 zhp->zfs_name, sd->tosnap);
839 nvfs = fnvlist_alloc();
840 fnvlist_add_string(nvfs, "name", zhp->zfs_name);
841 fnvlist_add_uint64(nvfs, "parentfromsnap",
842 sd->parent_fromsnap_guid);
844 if (zhp->zfs_dmustats.dds_origin[0]) {
845 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
846 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
847 if (origin == NULL) {
851 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
852 origin->zfs_dmustats.dds_guid));
855 /* iterate over props */
856 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
857 send_iterate_prop(zhp, nv);
858 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
861 /* iterate over snaps, and set sd->parent_fromsnap_guid */
862 if (!sd->replicate && fromsnap_txg != 0)
863 min_txg = fromsnap_txg;
864 if (!sd->replicate && tosnap_txg != 0)
865 max_txg = tosnap_txg;
866 sd->parent_fromsnap_guid = 0;
867 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
868 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
869 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd,
871 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
872 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
873 fnvlist_free(sd->parent_snaps);
874 fnvlist_free(sd->snapprops);
876 /* add this fs to nvlist */
877 (void) snprintf(guidstring, sizeof (guidstring),
878 "0x%llx", (longlong_t)guid);
879 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
882 /* iterate over children */
884 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
887 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
888 sd->fromsnap_txg = fromsnap_txg_save;
889 sd->tosnap_txg = tosnap_txg_save;
896 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
897 const char *tosnap, boolean_t recursive, boolean_t verbose,
898 boolean_t replicate, nvlist_t **nvlp, avl_tree_t **avlp)
902 uint64_t min_txg = 0, max_txg = 0;
903 send_data_t sd = { 0 };
905 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
907 return (EZFS_BADTYPE);
909 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
911 sd.fromsnap = fromsnap;
913 sd.recursive = recursive;
914 sd.verbose = verbose;
915 sd.replicate = replicate;
917 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
925 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
936 * Routines specific to "zfs send"
938 typedef struct send_dump_data {
939 /* these are all just the short snapname (the part after the @) */
940 const char *fromsnap;
942 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
943 uint64_t prevsnap_obj;
944 boolean_t seenfrom, seento, replicate, doall, fromorigin;
945 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
946 boolean_t progressastitle;
947 boolean_t large_block, compress;
953 snapfilter_cb_t *filter_cb;
956 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
962 zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
963 enum lzc_send_flags flags, uint64_t *spacep)
965 libzfs_handle_t *hdl = zhp->zfs_hdl;
968 assert(snapname != NULL);
969 error = lzc_send_space(snapname, from, flags, spacep);
973 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
974 "warning: cannot estimate space for '%s'"), snapname);
978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
979 "not an earlier snapshot from the same fs"));
980 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
983 if (zfs_dataset_exists(hdl, snapname,
984 ZFS_TYPE_SNAPSHOT)) {
985 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
986 "incremental source (%s) does not exist"),
989 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1002 zfs_error_aux(hdl, strerror(error));
1003 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1006 return (zfs_standard_error(hdl, error, errbuf));
1014 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1015 * NULL) to the file descriptor specified by outfd.
1018 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
1019 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
1022 zfs_cmd_t zc = { 0 };
1023 libzfs_handle_t *hdl = zhp->zfs_hdl;
1026 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1027 assert(fromsnap_obj == 0 || !fromorigin);
1029 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1030 zc.zc_cookie = outfd;
1031 zc.zc_obj = fromorigin;
1032 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1033 zc.zc_fromobj = fromsnap_obj;
1034 zc.zc_flags = flags;
1036 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
1037 if (fromsnap && fromsnap[0] != '\0') {
1038 VERIFY(0 == nvlist_add_string(thisdbg,
1039 "fromsnap", fromsnap));
1042 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1044 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1045 "warning: cannot send '%s'"), zhp->zfs_name);
1047 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
1049 VERIFY(0 == nvlist_add_nvlist(debugnv,
1050 zhp->zfs_name, thisdbg));
1052 nvlist_free(thisdbg);
1056 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1057 "not an earlier snapshot from the same fs"));
1058 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1061 if (zfs_dataset_exists(hdl, zc.zc_name,
1062 ZFS_TYPE_SNAPSHOT)) {
1063 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1064 "incremental source (@%s) does not exist"),
1067 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1082 zfs_error_aux(hdl, strerror(errno));
1083 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1086 return (zfs_standard_error(hdl, errno, errbuf));
1091 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1092 nvlist_free(thisdbg);
1098 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1100 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1103 * zfs_send() only sets snapholds for sends that need them,
1104 * e.g. replication and doall.
1106 if (sdd->snapholds == NULL)
1109 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1113 send_progress_thread(void *arg)
1115 progress_arg_t *pa = arg;
1116 zfs_cmd_t zc = { 0 };
1117 zfs_handle_t *zhp = pa->pa_zhp;
1118 libzfs_handle_t *hdl = zhp->zfs_hdl;
1119 unsigned long long bytes, total;
1124 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1126 if (!pa->pa_parsable && !pa->pa_astitle)
1127 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1130 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1135 zc.zc_cookie = pa->pa_fd;
1136 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1137 return ((void *)-1);
1141 bytes = zc.zc_cookie;
1143 if (pa->pa_astitle) {
1145 if (pa->pa_size > bytes)
1146 pct = 100 * bytes / pa->pa_size;
1150 setproctitle("sending %s (%d%%: %llu/%llu)",
1151 zhp->zfs_name, pct, bytes, pa->pa_size);
1152 } else if (pa->pa_parsable) {
1153 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1154 tm->tm_hour, tm->tm_min, tm->tm_sec,
1155 bytes, zhp->zfs_name);
1157 zfs_nicenum(bytes, buf, sizeof (buf));
1158 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1159 tm->tm_hour, tm->tm_min, tm->tm_sec,
1160 buf, zhp->zfs_name);
1166 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1167 uint64_t size, boolean_t parsable)
1170 if (fromsnap != NULL) {
1171 (void) fprintf(fout, "incremental\t%s\t%s",
1174 (void) fprintf(fout, "full\t%s",
1178 if (fromsnap != NULL) {
1179 if (strchr(fromsnap, '@') == NULL &&
1180 strchr(fromsnap, '#') == NULL) {
1181 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1182 "send from @%s to %s"),
1185 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1186 "send from %s to %s"),
1190 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1197 (void) fprintf(fout, "\t%llu",
1199 } else if (size != 0) {
1201 zfs_nicenum(size, buf, sizeof (buf));
1202 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1203 " estimated size is %s"), buf);
1205 (void) fprintf(fout, "\n");
1209 dump_snapshot(zfs_handle_t *zhp, void *arg)
1211 send_dump_data_t *sdd = arg;
1212 progress_arg_t pa = { 0 };
1215 enum lzc_send_flags flags = 0;
1217 boolean_t isfromsnap, istosnap, fromorigin;
1218 boolean_t exclude = B_FALSE;
1219 FILE *fout = sdd->std_out ? stdout : stderr;
1222 thissnap = strchr(zhp->zfs_name, '@') + 1;
1223 isfromsnap = (sdd->fromsnap != NULL &&
1224 strcmp(sdd->fromsnap, thissnap) == 0);
1226 if (!sdd->seenfrom && isfromsnap) {
1227 gather_holds(zhp, sdd);
1228 sdd->seenfrom = B_TRUE;
1229 (void) strcpy(sdd->prevsnap, thissnap);
1230 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1235 if (sdd->seento || !sdd->seenfrom) {
1240 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1242 sdd->seento = B_TRUE;
1244 if (sdd->large_block)
1245 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1246 if (sdd->embed_data)
1247 flags |= LZC_SEND_FLAG_EMBED_DATA;
1249 flags |= LZC_SEND_FLAG_COMPRESS;
1251 if (!sdd->doall && !isfromsnap && !istosnap) {
1252 if (sdd->replicate) {
1254 nvlist_t *snapprops;
1256 * Filter out all intermediate snapshots except origin
1257 * snapshots needed to replicate clones.
1259 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1260 zhp->zfs_dmustats.dds_guid, &snapname);
1262 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1263 "snapprops", &snapprops));
1264 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1265 thissnap, &snapprops));
1266 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1273 * If a filter function exists, call it to determine whether
1274 * this snapshot will be sent.
1276 if (exclude || (sdd->filter_cb != NULL &&
1277 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1279 * This snapshot is filtered out. Don't send it, and don't
1280 * set prevsnap_obj, so it will be as if this snapshot didn't
1281 * exist, and the next accepted snapshot will be sent as
1282 * an incremental from the last accepted one, or as the
1283 * first (and full) snapshot in the case of a replication,
1284 * non-incremental send.
1290 gather_holds(zhp, sdd);
1291 fromorigin = sdd->prevsnap[0] == '\0' &&
1292 (sdd->fromorigin || sdd->replicate);
1294 if (sdd->verbose || sdd->progress) {
1296 char fromds[ZFS_MAX_DATASET_NAME_LEN];
1298 if (sdd->prevsnap[0] != '\0') {
1299 (void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1300 *(strchr(fromds, '@') + 1) = '\0';
1301 (void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1303 if (zfs_send_space(zhp, zhp->zfs_name,
1304 sdd->prevsnap[0] ? fromds : NULL, flags, &size) != 0) {
1305 size = 0; /* cannot estimate send space */
1307 send_print_verbose(fout, zhp->zfs_name,
1308 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1309 size, sdd->parsable);
1316 * If progress reporting is requested, spawn a new thread to
1317 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1319 if (sdd->progress) {
1321 pa.pa_fd = sdd->outfd;
1322 pa.pa_parsable = sdd->parsable;
1323 pa.pa_size = sdd->size;
1324 pa.pa_astitle = sdd->progressastitle;
1326 if ((err = pthread_create(&tid, NULL,
1327 send_progress_thread, &pa)) != 0) {
1333 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1334 fromorigin, sdd->outfd, flags, sdd->debugnv);
1336 if (sdd->progress) {
1337 (void) pthread_cancel(tid);
1338 (void) pthread_join(tid, NULL);
1342 (void) strcpy(sdd->prevsnap, thissnap);
1343 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1349 dump_filesystem(zfs_handle_t *zhp, void *arg)
1352 uint64_t min_txg = 0, max_txg = 0;
1353 send_dump_data_t *sdd = arg;
1354 boolean_t missingfrom = B_FALSE;
1355 zfs_cmd_t zc = { 0 };
1357 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1358 zhp->zfs_name, sdd->tosnap);
1359 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1360 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1361 "WARNING: could not send %s@%s: does not exist\n"),
1362 zhp->zfs_name, sdd->tosnap);
1367 if (sdd->replicate && sdd->fromsnap) {
1369 * If this fs does not have fromsnap, and we're doing
1370 * recursive, we need to send a full stream from the
1371 * beginning (or an incremental from the origin if this
1372 * is a clone). If we're doing non-recursive, then let
1373 * them get the error.
1375 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1376 zhp->zfs_name, sdd->fromsnap);
1377 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1378 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1379 missingfrom = B_TRUE;
1383 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1384 sdd->prevsnap_obj = 0;
1385 if (sdd->fromsnap == NULL || missingfrom)
1386 sdd->seenfrom = B_TRUE;
1388 if (!sdd->replicate && sdd->fromsnap != NULL)
1389 min_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name,
1391 if (!sdd->replicate && sdd->tosnap != NULL)
1392 max_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name,
1395 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg,
1397 if (!sdd->seenfrom) {
1398 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1399 "WARNING: could not send %s@%s:\n"
1400 "incremental source (%s@%s) does not exist\n"),
1401 zhp->zfs_name, sdd->tosnap,
1402 zhp->zfs_name, sdd->fromsnap);
1404 } else if (!sdd->seento) {
1405 if (sdd->fromsnap) {
1406 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1407 "WARNING: could not send %s@%s:\n"
1408 "incremental source (%s@%s) "
1409 "is not earlier than it\n"),
1410 zhp->zfs_name, sdd->tosnap,
1411 zhp->zfs_name, sdd->fromsnap);
1413 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1415 "could not send %s@%s: does not exist\n"),
1416 zhp->zfs_name, sdd->tosnap);
1425 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1427 send_dump_data_t *sdd = arg;
1429 boolean_t needagain, progress;
1431 if (!sdd->replicate)
1432 return (dump_filesystem(rzhp, sdd));
1434 /* Mark the clone origin snapshots. */
1435 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1436 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1438 uint64_t origin_guid = 0;
1440 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1441 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1442 if (origin_guid != 0) {
1444 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1445 origin_guid, &snapname);
1446 if (origin_nv != NULL) {
1447 nvlist_t *snapprops;
1448 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1449 "snapprops", &snapprops));
1450 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1451 snapname, &snapprops));
1452 VERIFY(0 == nvlist_add_boolean(
1453 snapprops, "is_clone_origin"));
1458 needagain = progress = B_FALSE;
1459 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1460 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1461 nvlist_t *fslist, *parent_nv;
1465 uint64_t origin_guid = 0;
1466 uint64_t parent_guid = 0;
1468 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1469 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1472 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1473 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1474 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1477 if (parent_guid != 0) {
1478 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1479 if (!nvlist_exists(parent_nv, "sent")) {
1480 /* parent has not been sent; skip this one */
1486 if (origin_guid != 0) {
1487 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1489 if (origin_nv != NULL &&
1490 !nvlist_exists(origin_nv, "sent")) {
1492 * origin has not been sent yet;
1500 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1503 err = dump_filesystem(zhp, sdd);
1504 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1515 /* clean out the sent flags in case we reuse this fss */
1516 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1517 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1520 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1521 (void) nvlist_remove_all(fslist, "sent");
1528 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1530 unsigned int version;
1532 unsigned long long checksum, packed_len;
1535 * Decode token header, which is:
1536 * <token version>-<checksum of payload>-<uncompressed payload length>
1537 * Note that the only supported token version is 1.
1539 nread = sscanf(token, "%u-%llx-%llx-",
1540 &version, &checksum, &packed_len);
1542 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1543 "resume token is corrupt (invalid format)"));
1547 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1549 "resume token is corrupt (invalid version %u)"),
1554 /* convert hexadecimal representation to binary */
1555 token = strrchr(token, '-') + 1;
1556 int len = strlen(token) / 2;
1557 unsigned char *compressed = zfs_alloc(hdl, len);
1558 for (int i = 0; i < len; i++) {
1559 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1562 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1563 "resume token is corrupt "
1564 "(payload is not hex-encoded)"));
1569 /* verify checksum */
1571 fletcher_4_native(compressed, len, NULL, &cksum);
1572 if (cksum.zc_word[0] != checksum) {
1574 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1575 "resume token is corrupt (incorrect checksum)"));
1580 void *packed = zfs_alloc(hdl, packed_len);
1581 uLongf packed_len_long = packed_len;
1582 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1583 packed_len_long != packed_len) {
1586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1587 "resume token is corrupt (decompression failed)"));
1593 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1597 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1598 "resume token is corrupt (nvlist_unpack failed)"));
1605 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1606 const char *resume_token)
1610 char *fromname = NULL;
1611 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1614 char name[ZFS_MAX_DATASET_NAME_LEN];
1615 enum lzc_send_flags lzc_flags = 0;
1617 FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr;
1619 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1620 "cannot resume send"));
1622 nvlist_t *resume_nvl =
1623 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1624 if (resume_nvl == NULL) {
1626 * zfs_error_aux has already been set by
1627 * zfs_send_resume_token_to_nvlist
1629 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1631 if (flags->verbose) {
1632 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1633 "resume token contents:\n"));
1634 nvlist_print(fout, resume_nvl);
1637 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1638 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1639 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1640 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1641 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1642 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1643 "resume token is corrupt"));
1644 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1647 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1649 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1650 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1651 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1652 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1653 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1654 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1656 if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1657 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1658 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1659 "'%s' is no longer the same snapshot used in "
1660 "the initial send"), toname);
1662 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1663 "'%s' used in the initial send no longer exists"),
1666 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1668 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1671 "unable to access '%s'"), name);
1672 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1675 if (fromguid != 0) {
1676 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1678 "incremental source %#llx no longer exists"),
1679 (longlong_t)fromguid);
1680 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1685 if (flags->progress || flags->verbose) {
1686 error = lzc_send_space(zhp->zfs_name, fromname,
1689 size = MAX(0, (int64_t)(size - bytes));
1691 if (flags->verbose) {
1692 send_print_verbose(fout, zhp->zfs_name, fromname,
1693 size, flags->parsable);
1696 if (!flags->dryrun) {
1697 progress_arg_t pa = { 0 };
1700 * If progress reporting is requested, spawn a new thread to
1701 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1703 if (flags->progress) {
1706 pa.pa_parsable = flags->parsable;
1708 pa.pa_astitle = flags->progressastitle;
1710 error = pthread_create(&tid, NULL,
1711 send_progress_thread, &pa);
1718 error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1719 lzc_flags, resumeobj, resumeoff);
1721 if (flags->progress) {
1722 (void) pthread_cancel(tid);
1723 (void) pthread_join(tid, NULL);
1727 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1728 "warning: cannot send '%s'"), zhp->zfs_name);
1750 zfs_error_aux(hdl, strerror(errno));
1751 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1754 return (zfs_standard_error(hdl, errno, errbuf));
1765 * Generate a send stream for the dataset identified by the argument zhp.
1767 * The content of the send stream is the snapshot identified by
1768 * 'tosnap'. Incremental streams are requested in two ways:
1769 * - from the snapshot identified by "fromsnap" (if non-null) or
1770 * - from the origin of the dataset identified by zhp, which must
1771 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1774 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1775 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1776 * if "replicate" is set. If "doall" is set, dump all the intermediate
1777 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1778 * case too. If "props" is set, send properties.
1781 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1782 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1783 void *cb_arg, nvlist_t **debugnvp)
1786 send_dump_data_t sdd = { 0 };
1788 nvlist_t *fss = NULL;
1789 avl_tree_t *fsavl = NULL;
1790 static uint64_t holdseq;
1794 dedup_arg_t dda = { 0 };
1795 int featureflags = 0;
1798 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1799 "cannot send '%s'"), zhp->zfs_name);
1801 if (fromsnap && fromsnap[0] == '\0') {
1802 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1803 "zero-length incremental source"));
1804 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1807 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1809 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1810 if (version >= ZPL_VERSION_SA) {
1811 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1815 if (flags->dedup && !flags->dryrun) {
1816 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1817 DMU_BACKUP_FEATURE_DEDUPPROPS);
1818 if ((err = pipe(pipefd)) != 0) {
1819 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1820 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1823 dda.outputfd = outfd;
1824 dda.inputfd = pipefd[1];
1825 dda.dedup_hdl = zhp->zfs_hdl;
1826 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1827 (void) close(pipefd[0]);
1828 (void) close(pipefd[1]);
1829 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1830 return (zfs_error(zhp->zfs_hdl,
1831 EZFS_THREADCREATEFAILED, errbuf));
1835 if (flags->replicate || flags->doall || flags->props) {
1836 dmu_replay_record_t drr = { 0 };
1837 char *packbuf = NULL;
1839 zio_cksum_t zc = { 0 };
1841 if (flags->replicate || flags->props) {
1844 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1846 VERIFY(0 == nvlist_add_string(hdrnv,
1847 "fromsnap", fromsnap));
1849 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1850 if (!flags->replicate) {
1851 VERIFY(0 == nvlist_add_boolean(hdrnv,
1855 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1856 fromsnap, tosnap, flags->replicate, flags->verbose,
1857 flags->replicate, &fss, &fsavl);
1860 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1861 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1871 if (!flags->dryrun) {
1872 /* write first begin record */
1873 drr.drr_type = DRR_BEGIN;
1874 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1875 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1876 drr_versioninfo, DMU_COMPOUNDSTREAM);
1877 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1878 drr_versioninfo, featureflags);
1879 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1880 sizeof (drr.drr_u.drr_begin.drr_toname),
1881 "%s@%s", zhp->zfs_name, tosnap);
1882 drr.drr_payloadlen = buflen;
1884 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1889 /* write end record */
1890 bzero(&drr, sizeof (drr));
1891 drr.drr_type = DRR_END;
1892 drr.drr_u.drr_end.drr_checksum = zc;
1893 err = write(outfd, &drr, sizeof (drr));
1903 /* dump each stream */
1904 sdd.fromsnap = fromsnap;
1905 sdd.tosnap = tosnap;
1907 sdd.outfd = pipefd[0];
1910 sdd.replicate = flags->replicate;
1911 sdd.doall = flags->doall;
1912 sdd.fromorigin = flags->fromorigin;
1915 sdd.verbose = flags->verbose;
1916 sdd.parsable = flags->parsable;
1917 sdd.progress = flags->progress;
1918 sdd.progressastitle = flags->progressastitle;
1919 sdd.dryrun = flags->dryrun;
1920 sdd.large_block = flags->largeblock;
1921 sdd.embed_data = flags->embed_data;
1922 sdd.compress = flags->compress;
1923 sdd.filter_cb = filter_func;
1924 sdd.filter_cb_arg = cb_arg;
1926 sdd.debugnv = *debugnvp;
1927 if (sdd.verbose && sdd.dryrun)
1928 sdd.std_out = B_TRUE;
1929 fout = sdd.std_out ? stdout : stderr;
1932 * Some flags require that we place user holds on the datasets that are
1933 * being sent so they don't get destroyed during the send. We can skip
1934 * this step if the pool is imported read-only since the datasets cannot
1937 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1938 ZPOOL_PROP_READONLY, NULL) &&
1939 zfs_spa_version(zhp, &spa_version) == 0 &&
1940 spa_version >= SPA_VERSION_USERREFS &&
1941 (flags->doall || flags->replicate)) {
1943 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1944 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1945 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1946 if (sdd.cleanup_fd < 0) {
1950 sdd.snapholds = fnvlist_alloc();
1952 sdd.cleanup_fd = -1;
1953 sdd.snapholds = NULL;
1955 if (flags->progress || flags->verbose || sdd.snapholds != NULL) {
1957 * Do a verbose no-op dry run to get all the verbose output
1958 * or to gather snapshot hold's before generating any data,
1959 * then do a non-verbose real run to generate the streams.
1961 sdd.dryrun = B_TRUE;
1962 err = dump_filesystems(zhp, &sdd);
1967 if (flags->verbose) {
1968 if (flags->parsable) {
1969 (void) fprintf(fout, "size\t%llu\n",
1970 (longlong_t)sdd.size);
1973 zfs_nicenum(sdd.size, buf, sizeof (buf));
1974 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1975 "total estimated size is %s\n"), buf);
1979 /* Ensure no snaps found is treated as an error. */
1985 /* Skip the second run if dryrun was requested. */
1989 if (sdd.snapholds != NULL) {
1990 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
1994 fnvlist_free(sdd.snapholds);
1995 sdd.snapholds = NULL;
1998 sdd.dryrun = B_FALSE;
1999 sdd.verbose = B_FALSE;
2002 err = dump_filesystems(zhp, &sdd);
2003 fsavl_destroy(fsavl);
2006 /* Ensure no snaps found is treated as an error. */
2007 if (err == 0 && !sdd.seento)
2012 (void) pthread_cancel(tid);
2013 (void) close(pipefd[0]);
2014 (void) pthread_join(tid, NULL);
2017 if (sdd.cleanup_fd != -1) {
2018 VERIFY(0 == close(sdd.cleanup_fd));
2019 sdd.cleanup_fd = -1;
2022 if (!flags->dryrun && (flags->replicate || flags->doall ||
2025 * write final end record. NB: want to do this even if
2026 * there was some error, because it might not be totally
2029 dmu_replay_record_t drr = { 0 };
2030 drr.drr_type = DRR_END;
2031 if (write(outfd, &drr, sizeof (drr)) == -1) {
2032 return (zfs_standard_error(zhp->zfs_hdl,
2037 return (err || sdd.err);
2040 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2042 fsavl_destroy(fsavl);
2044 fnvlist_free(sdd.snapholds);
2046 if (sdd.cleanup_fd != -1)
2047 VERIFY(0 == close(sdd.cleanup_fd));
2049 (void) pthread_cancel(tid);
2050 (void) close(pipefd[0]);
2051 (void) pthread_join(tid, NULL);
2057 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t flags)
2060 libzfs_handle_t *hdl = zhp->zfs_hdl;
2061 enum lzc_send_flags lzc_flags = 0;
2062 FILE *fout = (flags.verbose && flags.dryrun) ? stdout : stderr;
2065 if (flags.largeblock)
2066 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
2067 if (flags.embed_data)
2068 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
2070 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
2072 if (flags.verbose) {
2074 err = lzc_send_space(zhp->zfs_name, from, lzc_flags, &size);
2076 send_print_verbose(fout, zhp->zfs_name, from, size,
2078 if (flags.parsable) {
2079 (void) fprintf(fout, "size\t%llu\n",
2083 zfs_nicenum(size, buf, sizeof (buf));
2084 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
2085 "total estimated size is %s\n"), buf);
2088 (void) fprintf(stderr, "Cannot estimate send size: "
2089 "%s\n", strerror(errno));
2096 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2097 "warning: cannot send '%s'"), zhp->zfs_name);
2099 err = lzc_send(zhp->zfs_name, from, fd, lzc_flags);
2103 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2104 "not an earlier snapshot from the same fs"));
2105 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2109 if (lzc_exists(zhp->zfs_name)) {
2110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2111 "incremental source (%s) does not exist"),
2114 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2117 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2118 "target is busy; if a filesystem, "
2119 "it must not be mounted"));
2120 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2135 zfs_error_aux(hdl, strerror(errno));
2136 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2139 return (zfs_standard_error(hdl, errno, errbuf));
2146 * Routines specific to "zfs recv"
2150 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2151 boolean_t byteswap, zio_cksum_t *zc)
2157 assert(ilen <= SPA_MAXBLOCKSIZE);
2160 rv = read(fd, cp, len);
2165 if (rv < 0 || len != 0) {
2166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2167 "failed to read from stream"));
2168 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2169 "cannot receive")));
2174 (void) fletcher_4_incremental_byteswap(buf, ilen, zc);
2176 (void) fletcher_4_incremental_native(buf, ilen, zc);
2182 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2183 boolean_t byteswap, zio_cksum_t *zc)
2188 buf = zfs_alloc(hdl, len);
2192 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2198 err = nvlist_unpack(buf, len, nvp, 0);
2201 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2202 "stream (malformed nvlist)"));
2209 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2210 int baselen, char *newname, recvflags_t *flags)
2214 prop_changelist_t *clp;
2217 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2220 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2221 flags->force ? MS_FORCE : 0);
2225 err = changelist_prefix(clp);
2230 (void) strcpy(newname, tryname);
2231 if (flags->verbose) {
2232 (void) printf("attempting rename %s to %s\n",
2235 err = lzc_rename(name, newname);
2237 changelist_rename(clp, name, tryname);
2242 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2245 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2246 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2247 if (flags->verbose) {
2248 (void) printf("failed - trying rename %s to %s\n",
2251 err = lzc_rename(name, newname);
2253 changelist_rename(clp, name, newname);
2254 if (err && flags->verbose) {
2255 (void) printf("failed (%u) - "
2256 "will try again on next pass\n", errno);
2259 } else if (flags->verbose) {
2261 (void) printf("success\n");
2263 (void) printf("failed (%u)\n", errno);
2266 (void) changelist_postfix(clp);
2267 changelist_free(clp);
2273 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2274 char *newname, recvflags_t *flags)
2277 prop_changelist_t *clp;
2279 boolean_t defer = B_FALSE;
2282 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2285 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2286 flags->force ? MS_FORCE : 0);
2287 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2288 zfs_spa_version(zhp, &spa_version) == 0 &&
2289 spa_version >= SPA_VERSION_USERREFS)
2294 err = changelist_prefix(clp);
2299 (void) printf("attempting destroy %s\n", name);
2300 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2301 nvlist_t *nv = fnvlist_alloc();
2302 fnvlist_add_boolean(nv, name);
2303 err = lzc_destroy_snaps(nv, defer, NULL);
2306 err = lzc_destroy(name);
2310 (void) printf("success\n");
2311 changelist_remove(clp, name);
2314 (void) changelist_postfix(clp);
2315 changelist_free(clp);
2318 * Deferred destroy might destroy the snapshot or only mark it to be
2319 * destroyed later, and it returns success in either case.
2321 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2322 ZFS_TYPE_SNAPSHOT))) {
2323 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2329 typedef struct guid_to_name_data {
2331 boolean_t bookmark_ok;
2334 } guid_to_name_data_t;
2337 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2339 guid_to_name_data_t *gtnd = arg;
2343 if (gtnd->skip != NULL &&
2344 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2345 strcmp(slash + 1, gtnd->skip) == 0) {
2350 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2351 (void) strcpy(gtnd->name, zhp->zfs_name);
2356 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2357 if (err != EEXIST && gtnd->bookmark_ok)
2358 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2364 * Attempt to find the local dataset associated with this guid. In the case of
2365 * multiple matches, we attempt to find the "best" match by searching
2366 * progressively larger portions of the hierarchy. This allows one to send a
2367 * tree of datasets individually and guarantee that we will find the source
2368 * guid within that hierarchy, even if there are multiple matches elsewhere.
2371 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2372 boolean_t bookmark_ok, char *name)
2374 char pname[ZFS_MAX_DATASET_NAME_LEN];
2375 guid_to_name_data_t gtnd;
2378 gtnd.bookmark_ok = bookmark_ok;
2383 * Search progressively larger portions of the hierarchy, starting
2384 * with the filesystem specified by 'parent'. This will
2385 * select the "most local" version of the origin snapshot in the case
2386 * that there are multiple matching snapshots in the system.
2388 (void) strlcpy(pname, parent, sizeof (pname));
2389 char *cp = strrchr(pname, '@');
2391 cp = strchr(pname, '\0');
2392 for (; cp != NULL; cp = strrchr(pname, '/')) {
2393 /* Chop off the last component and open the parent */
2395 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2399 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
2401 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2402 if (err != EEXIST && bookmark_ok)
2403 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2409 * Remember the last portion of the dataset so we skip it next
2410 * time through (as we've already searched that portion of the
2413 gtnd.skip = strrchr(pname, '/') + 1;
2420 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2421 * guid1 is after guid2.
2424 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2425 uint64_t guid1, uint64_t guid2)
2428 char *fsname, *snapname;
2429 char buf[ZFS_MAX_DATASET_NAME_LEN];
2431 zfs_handle_t *guid1hdl, *guid2hdl;
2432 uint64_t create1, create2;
2439 nvfs = fsavl_find(avl, guid1, &snapname);
2440 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2441 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2442 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2443 if (guid1hdl == NULL)
2446 nvfs = fsavl_find(avl, guid2, &snapname);
2447 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2448 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2449 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2450 if (guid2hdl == NULL) {
2451 zfs_close(guid1hdl);
2455 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2456 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2458 if (create1 < create2)
2460 else if (create1 > create2)
2465 zfs_close(guid1hdl);
2466 zfs_close(guid2hdl);
2472 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2473 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2476 nvlist_t *local_nv, *deleted = NULL;
2477 avl_tree_t *local_avl;
2478 nvpair_t *fselem, *nextfselem;
2480 char newname[ZFS_MAX_DATASET_NAME_LEN];
2483 boolean_t needagain, progress, recursive;
2486 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2488 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2495 needagain = progress = B_FALSE;
2497 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2499 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2500 recursive, B_FALSE, B_FALSE, &local_nv, &local_avl)) != 0)
2504 * Process deletes and renames
2506 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2507 fselem; fselem = nextfselem) {
2508 nvlist_t *nvfs, *snaps;
2509 nvlist_t *stream_nvfs = NULL;
2510 nvpair_t *snapelem, *nextsnapelem;
2511 uint64_t fromguid = 0;
2512 uint64_t originguid = 0;
2513 uint64_t stream_originguid = 0;
2514 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2515 char *fsname, *stream_fsname;
2517 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2519 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2520 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2521 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2522 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2523 &parent_fromsnap_guid));
2524 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2527 * First find the stream's fs, so we can check for
2528 * a different origin (due to "zfs promote")
2530 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2531 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2534 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2535 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2537 if (stream_nvfs != NULL)
2541 /* check for promote */
2542 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2543 &stream_originguid);
2544 if (stream_nvfs && originguid != stream_originguid) {
2545 switch (created_before(hdl, local_avl,
2546 stream_originguid, originguid)) {
2549 zfs_cmd_t zc = { 0 };
2550 nvlist_t *origin_nvfs;
2551 char *origin_fsname;
2554 (void) printf("promoting %s\n", fsname);
2556 origin_nvfs = fsavl_find(local_avl, originguid,
2558 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2559 "name", &origin_fsname));
2560 (void) strlcpy(zc.zc_value, origin_fsname,
2561 sizeof (zc.zc_value));
2562 (void) strlcpy(zc.zc_name, fsname,
2563 sizeof (zc.zc_name));
2564 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2572 fsavl_destroy(local_avl);
2573 nvlist_free(local_nv);
2577 * We had/have the wrong origin, therefore our
2578 * list of snapshots is wrong. Need to handle
2579 * them on the next pass.
2585 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2586 snapelem; snapelem = nextsnapelem) {
2588 char *stream_snapname;
2589 nvlist_t *found, *props;
2591 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2593 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2594 found = fsavl_find(stream_avl, thisguid,
2597 /* check for delete */
2598 if (found == NULL) {
2599 char name[ZFS_MAX_DATASET_NAME_LEN];
2604 (void) snprintf(name, sizeof (name), "%s@%s",
2605 fsname, nvpair_name(snapelem));
2607 error = recv_destroy(hdl, name,
2608 strlen(fsname)+1, newname, flags);
2613 sprintf(guidname, "%" PRIu64, thisguid);
2614 nvlist_add_boolean(deleted, guidname);
2618 stream_nvfs = found;
2620 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2621 &props) && 0 == nvlist_lookup_nvlist(props,
2622 stream_snapname, &props)) {
2623 zfs_cmd_t zc = { 0 };
2625 zc.zc_cookie = B_TRUE; /* received */
2626 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2627 "%s@%s", fsname, nvpair_name(snapelem));
2628 if (zcmd_write_src_nvlist(hdl, &zc,
2630 (void) zfs_ioctl(hdl,
2631 ZFS_IOC_SET_PROP, &zc);
2632 zcmd_free_nvlists(&zc);
2636 /* check for different snapname */
2637 if (strcmp(nvpair_name(snapelem),
2638 stream_snapname) != 0) {
2639 char name[ZFS_MAX_DATASET_NAME_LEN];
2640 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2642 (void) snprintf(name, sizeof (name), "%s@%s",
2643 fsname, nvpair_name(snapelem));
2644 (void) snprintf(tryname, sizeof (name), "%s@%s",
2645 fsname, stream_snapname);
2647 error = recv_rename(hdl, name, tryname,
2648 strlen(fsname)+1, newname, flags);
2655 if (strcmp(stream_snapname, fromsnap) == 0)
2656 fromguid = thisguid;
2659 /* check for delete */
2660 if (stream_nvfs == NULL) {
2664 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2670 sprintf(guidname, "%" PRIu64, parent_fromsnap_guid);
2671 nvlist_add_boolean(deleted, guidname);
2675 if (fromguid == 0) {
2676 if (flags->verbose) {
2677 (void) printf("local fs %s does not have "
2678 "fromsnap (%s in stream); must have "
2679 "been deleted locally; ignoring\n",
2685 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2686 "name", &stream_fsname));
2687 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2688 "parentfromsnap", &stream_parent_fromsnap_guid));
2690 s1 = strrchr(fsname, '/');
2691 s2 = strrchr(stream_fsname, '/');
2694 * Check if we're going to rename based on parent guid change
2695 * and the current parent guid was also deleted. If it was then
2696 * rename will fail and is likely unneeded, so avoid this and
2697 * force an early retry to determine the new
2698 * parent_fromsnap_guid.
2700 if (stream_parent_fromsnap_guid != 0 &&
2701 parent_fromsnap_guid != 0 &&
2702 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
2703 sprintf(guidname, "%" PRIu64, parent_fromsnap_guid);
2704 if (nvlist_exists(deleted, guidname)) {
2712 * Check for rename. If the exact receive path is specified, it
2713 * does not count as a rename, but we still need to check the
2714 * datasets beneath it.
2716 if ((stream_parent_fromsnap_guid != 0 &&
2717 parent_fromsnap_guid != 0 &&
2718 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2719 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2720 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2722 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2724 parent = fsavl_find(local_avl,
2725 stream_parent_fromsnap_guid, NULL);
2727 * NB: parent might not be found if we used the
2728 * tosnap for stream_parent_fromsnap_guid,
2729 * because the parent is a newly-created fs;
2730 * we'll be able to rename it after we recv the
2733 if (parent != NULL) {
2736 VERIFY(0 == nvlist_lookup_string(parent, "name",
2738 (void) snprintf(tryname, sizeof (tryname),
2739 "%s%s", pname, strrchr(stream_fsname, '/'));
2742 if (flags->verbose) {
2743 (void) printf("local fs %s new parent "
2744 "not found\n", fsname);
2750 error = recv_rename(hdl, fsname, tryname,
2751 strlen(tofs)+1, newname, flags);
2753 if (renamed != NULL && newname[0] != '\0') {
2754 VERIFY(0 == nvlist_add_boolean(renamed,
2766 fsavl_destroy(local_avl);
2767 nvlist_free(local_nv);
2768 nvlist_free(deleted);
2770 if (needagain && progress) {
2771 /* do another pass to fix up temporary names */
2773 (void) printf("another pass:\n");
2781 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2782 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2783 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2785 nvlist_t *stream_nv = NULL;
2786 avl_tree_t *stream_avl = NULL;
2787 char *fromsnap = NULL;
2788 char *sendsnap = NULL;
2790 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2791 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
2793 dmu_replay_record_t drre;
2795 boolean_t anyerr = B_FALSE;
2796 boolean_t softerr = B_FALSE;
2797 boolean_t recursive;
2799 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2802 assert(drr->drr_type == DRR_BEGIN);
2803 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2804 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2805 DMU_COMPOUNDSTREAM);
2808 * Read in the nvlist from the stream.
2810 if (drr->drr_payloadlen != 0) {
2811 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2812 &stream_nv, flags->byteswap, zc);
2814 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2819 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2822 if (recursive && strchr(destname, '@')) {
2823 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2824 "cannot specify snapshot name for multi-snapshot stream"));
2825 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2830 * Read in the end record and verify checksum.
2832 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2833 flags->byteswap, NULL)))
2835 if (flags->byteswap) {
2836 drre.drr_type = BSWAP_32(drre.drr_type);
2837 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2838 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2839 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2840 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2841 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2842 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2843 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2844 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2846 if (drre.drr_type != DRR_END) {
2847 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2850 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2851 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2852 "incorrect header checksum"));
2853 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2857 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2859 if (drr->drr_payloadlen != 0) {
2860 nvlist_t *stream_fss;
2862 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2864 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2865 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2866 "couldn't allocate avl tree"));
2867 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2871 if (fromsnap != NULL && recursive) {
2872 nvlist_t *renamed = NULL;
2873 nvpair_t *pair = NULL;
2875 (void) strlcpy(tofs, destname, sizeof (tofs));
2876 if (flags->isprefix) {
2877 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2880 if (flags->istail) {
2881 cp = strrchr(drrb->drr_toname, '/');
2883 (void) strlcat(tofs, "/",
2887 i = (cp - drrb->drr_toname);
2890 i = strcspn(drrb->drr_toname, "/@");
2892 /* zfs_receive_one() will create_parents() */
2893 (void) strlcat(tofs, &drrb->drr_toname[i],
2895 *strchr(tofs, '@') = '\0';
2898 if (!flags->dryrun && !flags->nomount) {
2899 VERIFY(0 == nvlist_alloc(&renamed,
2900 NV_UNIQUE_NAME, 0));
2903 softerr = recv_incremental_replication(hdl, tofs, flags,
2904 stream_nv, stream_avl, renamed);
2906 /* Unmount renamed filesystems before receiving. */
2907 while ((pair = nvlist_next_nvpair(renamed,
2910 prop_changelist_t *clp = NULL;
2912 zhp = zfs_open(hdl, nvpair_name(pair),
2913 ZFS_TYPE_FILESYSTEM);
2915 clp = changelist_gather(zhp,
2916 ZFS_PROP_MOUNTPOINT, 0, 0);
2920 changelist_prefix(clp);
2921 changelist_free(clp);
2926 nvlist_free(renamed);
2931 * Get the fs specified by the first path in the stream (the top level
2932 * specified by 'zfs send') and pass it to each invocation of
2933 * zfs_receive_one().
2935 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2937 if ((cp = strchr(sendfs, '@')) != NULL) {
2940 * Find the "sendsnap", the final snapshot in a replication
2941 * stream. zfs_receive_one() handles certain errors
2942 * differently, depending on if the contained stream is the
2945 sendsnap = (cp + 1);
2948 /* Finally, receive each contained stream */
2951 * we should figure out if it has a recoverable
2952 * error, in which case do a recv_skip() and drive on.
2953 * Note, if we fail due to already having this guid,
2954 * zfs_receive_one() will take care of it (ie,
2955 * recv_skip() and return 0).
2957 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2958 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2959 action_handlep, sendsnap);
2960 if (error == ENODATA) {
2965 } while (error == 0);
2967 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
2969 * Now that we have the fs's they sent us, try the
2972 softerr = recv_incremental_replication(hdl, tofs, flags,
2973 stream_nv, stream_avl, NULL);
2977 fsavl_destroy(stream_avl);
2978 nvlist_free(stream_nv);
2987 trunc_prop_errs(int truncated)
2989 ASSERT(truncated != 0);
2992 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2993 "1 more property could not be set\n"));
2995 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2996 "%d more properties could not be set\n"), truncated);
3000 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3002 dmu_replay_record_t *drr;
3003 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
3006 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3007 "cannot receive:"));
3009 /* XXX would be great to use lseek if possible... */
3012 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3013 byteswap, NULL) == 0) {
3015 drr->drr_type = BSWAP_32(drr->drr_type);
3017 switch (drr->drr_type) {
3019 if (drr->drr_payloadlen != 0) {
3020 (void) recv_read(hdl, fd, buf,
3021 drr->drr_payloadlen, B_FALSE, NULL);
3031 drr->drr_u.drr_object.drr_bonuslen =
3032 BSWAP_32(drr->drr_u.drr_object.
3035 (void) recv_read(hdl, fd, buf,
3036 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
3042 drr->drr_u.drr_write.drr_logical_size =
3044 drr->drr_u.drr_write.drr_logical_size);
3045 drr->drr_u.drr_write.drr_compressed_size =
3047 drr->drr_u.drr_write.drr_compressed_size);
3049 uint64_t payload_size =
3050 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3051 (void) recv_read(hdl, fd, buf,
3052 payload_size, B_FALSE, NULL);
3056 drr->drr_u.drr_spill.drr_length =
3057 BSWAP_64(drr->drr_u.drr_spill.drr_length);
3059 (void) recv_read(hdl, fd, buf,
3060 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
3062 case DRR_WRITE_EMBEDDED:
3064 drr->drr_u.drr_write_embedded.drr_psize =
3065 BSWAP_32(drr->drr_u.drr_write_embedded.
3068 (void) recv_read(hdl, fd, buf,
3069 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3072 case DRR_WRITE_BYREF:
3073 case DRR_FREEOBJECTS:
3078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3079 "invalid record type"));
3080 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3089 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3090 boolean_t resumable)
3092 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3094 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3095 "checksum mismatch or incomplete stream"));
3099 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3100 *strchr(target_fs, '@') = '\0';
3101 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3102 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3106 char token_buf[ZFS_MAXPROPLEN];
3107 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3108 token_buf, sizeof (token_buf),
3109 NULL, NULL, 0, B_TRUE);
3111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3112 "checksum mismatch or incomplete stream.\n"
3113 "Partially received snapshot is saved.\n"
3114 "A resuming stream can be generated on the sending "
3115 "system by running:\n"
3123 * Restores a backup of tosnap from the file descriptor specified by infd.
3126 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3127 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3128 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3129 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3130 uint64_t *action_handlep, const char *finalsnap)
3132 zfs_cmd_t zc = { 0 };
3134 int ioctl_err, ioctl_errno, err;
3136 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3138 char prop_errbuf[1024];
3139 const char *chopprefix;
3140 boolean_t newfs = B_FALSE;
3141 boolean_t stream_wantsnewfs;
3142 uint64_t parent_snapguid = 0;
3143 prop_changelist_t *clp = NULL;
3144 nvlist_t *snapprops_nvlist = NULL;
3145 zprop_errflags_t prop_errflags;
3146 boolean_t recursive;
3147 char *snapname = NULL;
3149 begin_time = time(NULL);
3151 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3154 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3157 if (stream_avl != NULL) {
3158 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3163 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
3165 err = nvlist_lookup_nvlist(fs, "props", &props);
3167 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
3169 if (flags->canmountoff) {
3170 VERIFY(0 == nvlist_add_uint64(props,
3171 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3173 ret = zcmd_write_src_nvlist(hdl, &zc, props);
3177 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
3178 VERIFY(0 == nvlist_lookup_nvlist(props,
3179 snapname, &snapprops_nvlist));
3189 * Determine how much of the snapshot name stored in the stream
3190 * we are going to tack on to the name they specified on the
3191 * command line, and how much we are going to chop off.
3193 * If they specified a snapshot, chop the entire name stored in
3196 if (flags->istail) {
3198 * A filesystem was specified with -e. We want to tack on only
3199 * the tail of the sent snapshot path.
3201 if (strchr(tosnap, '@')) {
3202 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3203 "argument - snapshot not allowed with -e"));
3204 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3207 chopprefix = strrchr(sendfs, '/');
3209 if (chopprefix == NULL) {
3211 * The tail is the poolname, so we need to
3212 * prepend a path separator.
3214 int len = strlen(drrb->drr_toname);
3215 cp = malloc(len + 2);
3217 (void) strcpy(&cp[1], drrb->drr_toname);
3220 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3222 } else if (flags->isprefix) {
3224 * A filesystem was specified with -d. We want to tack on
3225 * everything but the first element of the sent snapshot path
3226 * (all but the pool name).
3228 if (strchr(tosnap, '@')) {
3229 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3230 "argument - snapshot not allowed with -d"));
3231 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3234 chopprefix = strchr(drrb->drr_toname, '/');
3235 if (chopprefix == NULL)
3236 chopprefix = strchr(drrb->drr_toname, '@');
3237 } else if (strchr(tosnap, '@') == NULL) {
3239 * If a filesystem was specified without -d or -e, we want to
3240 * tack on everything after the fs specified by 'zfs send'.
3242 chopprefix = drrb->drr_toname + strlen(sendfs);
3244 /* A snapshot was specified as an exact path (no -d or -e). */
3246 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3247 "cannot specify snapshot name for multi-snapshot "
3249 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3251 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3254 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3255 ASSERT(chopprefix > drrb->drr_toname);
3256 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
3257 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3258 chopprefix[0] == '\0');
3261 * Determine name of destination snapshot, store in zc_value.
3263 (void) strcpy(zc.zc_value, tosnap);
3264 (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
3266 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
3267 zfs_ioctl_version = get_zfs_ioctl_version();
3269 * For forward compatibility hide tosnap in zc_value
3271 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
3272 (void) strcpy(zc.zc_value + strlen(zc.zc_value) + 1, tosnap);
3275 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
3276 zcmd_free_nvlists(&zc);
3277 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3281 * Determine the name of the origin snapshot, store in zc_string.
3284 (void) strncpy(zc.zc_string, originsnap, sizeof (zc.zc_string));
3286 (void) printf("using provided clone origin %s\n",
3288 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3289 if (guid_to_name(hdl, zc.zc_value,
3290 drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3291 zcmd_free_nvlists(&zc);
3292 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3293 "local origin for clone %s does not exist"),
3295 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3298 (void) printf("found clone origin %s\n", zc.zc_string);
3301 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3302 DMU_BACKUP_FEATURE_RESUMING;
3303 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
3304 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3306 if (stream_wantsnewfs) {
3308 * if the parent fs does not exist, look for it based on
3309 * the parent snap GUID
3311 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3312 "cannot receive new filesystem stream"));
3314 (void) strcpy(zc.zc_name, zc.zc_value);
3315 cp = strrchr(zc.zc_name, '/');
3319 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3320 char suffix[ZFS_MAX_DATASET_NAME_LEN];
3321 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
3322 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
3323 B_FALSE, zc.zc_value) == 0) {
3324 *strchr(zc.zc_value, '@') = '\0';
3325 (void) strcat(zc.zc_value, suffix);
3330 * If the fs does not exist, look for it based on the
3334 (void) snprintf(errbuf, sizeof (errbuf),
3335 dgettext(TEXT_DOMAIN,
3336 "cannot receive resume stream"));
3338 (void) snprintf(errbuf, sizeof (errbuf),
3339 dgettext(TEXT_DOMAIN,
3340 "cannot receive incremental stream"));
3343 (void) strcpy(zc.zc_name, zc.zc_value);
3344 *strchr(zc.zc_name, '@') = '\0';
3347 * If the exact receive path was specified and this is the
3348 * topmost path in the stream, then if the fs does not exist we
3349 * should look no further.
3351 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
3352 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
3353 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3354 char snap[ZFS_MAX_DATASET_NAME_LEN];
3355 (void) strcpy(snap, strchr(zc.zc_value, '@'));
3356 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
3357 B_FALSE, zc.zc_value) == 0) {
3358 *strchr(zc.zc_value, '@') = '\0';
3359 (void) strcat(zc.zc_value, snap);
3364 (void) strcpy(zc.zc_name, zc.zc_value);
3365 *strchr(zc.zc_name, '@') = '\0';
3367 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3371 * Destination fs exists. It must be one of these cases:
3372 * - an incremental send stream
3373 * - the stream specifies a new fs (full stream or clone)
3374 * and they want us to blow away the existing fs (and
3375 * have therefore specified -F and removed any snapshots)
3376 * - we are resuming a failed receive.
3378 if (stream_wantsnewfs) {
3379 boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL;
3380 if (!flags->force) {
3381 zcmd_free_nvlists(&zc);
3382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3383 "destination '%s' exists\n"
3384 "must specify -F to overwrite it"),
3386 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3388 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3390 zcmd_free_nvlists(&zc);
3391 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3392 "destination has snapshots (eg. %s)\n"
3393 "must destroy them to overwrite it"),
3395 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3397 if (is_volume && strrchr(zc.zc_name, '/') == NULL) {
3398 zcmd_free_nvlists(&zc);
3399 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3400 "destination '%s' is the root dataset\n"
3401 "cannot overwrite with a ZVOL"),
3403 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3406 ioctl(hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT,
3408 zcmd_free_nvlists(&zc);
3409 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3410 "destination has children (eg. %s)\n"
3411 "cannot overwrite with a ZVOL"),
3413 return (zfs_error(hdl, EZFS_WRONG_PARENT,
3418 if ((zhp = zfs_open(hdl, zc.zc_name,
3419 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
3420 zcmd_free_nvlists(&zc);
3424 if (stream_wantsnewfs &&
3425 zhp->zfs_dmustats.dds_origin[0]) {
3426 zcmd_free_nvlists(&zc);
3428 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3429 "destination '%s' is a clone\n"
3430 "must destroy it to overwrite it"),
3432 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3435 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3436 (stream_wantsnewfs || resuming)) {
3437 /* We can't do online recv in this case */
3438 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3441 zcmd_free_nvlists(&zc);
3444 if (changelist_prefix(clp) != 0) {
3445 changelist_free(clp);
3447 zcmd_free_nvlists(&zc);
3453 * If we are resuming a newfs, set newfs here so that we will
3454 * mount it if the recv succeeds this time. We can tell
3455 * that it was a newfs on the first recv because the fs
3456 * itself will be inconsistent (if the fs existed when we
3457 * did the first recv, we would have received it into
3460 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
3468 * Destination filesystem does not exist. Therefore we better
3469 * be creating a new filesystem (either from a full backup, or
3470 * a clone). It would therefore be invalid if the user
3471 * specified only the pool name (i.e. if the destination name
3472 * contained no slash character).
3474 if (!stream_wantsnewfs ||
3475 (cp = strrchr(zc.zc_name, '/')) == NULL) {
3476 zcmd_free_nvlists(&zc);
3477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3478 "destination '%s' does not exist"), zc.zc_name);
3479 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3483 * Trim off the final dataset component so we perform the
3484 * recvbackup ioctl to the filesystems's parent.
3488 if (flags->isprefix && !flags->istail && !flags->dryrun &&
3489 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
3490 zcmd_free_nvlists(&zc);
3491 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3494 /* validate parent */
3495 zhp = zfs_open(hdl, zc.zc_name, ZFS_TYPE_DATASET);
3497 zcmd_free_nvlists(&zc);
3498 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3500 if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
3501 zcmd_free_nvlists(&zc);
3502 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3503 "parent '%s' is not a filesystem"), zc.zc_name);
3505 return (zfs_error(hdl, EZFS_WRONG_PARENT, errbuf));
3512 zc.zc_begin_record = *drr_noswap;
3513 zc.zc_cookie = infd;
3514 zc.zc_guid = flags->force;
3515 zc.zc_resumable = flags->resumable;
3516 if (flags->verbose) {
3517 (void) printf("%s %s stream of %s into %s\n",
3518 flags->dryrun ? "would receive" : "receiving",
3519 drrb->drr_fromguid ? "incremental" : "full",
3520 drrb->drr_toname, zc.zc_value);
3521 (void) fflush(stdout);
3524 if (flags->dryrun) {
3525 zcmd_free_nvlists(&zc);
3526 return (recv_skip(hdl, infd, flags->byteswap));
3529 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
3530 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
3531 zc.zc_cleanup_fd = cleanup_fd;
3532 zc.zc_action_handle = *action_handlep;
3534 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
3535 ioctl_errno = errno;
3536 prop_errflags = (zprop_errflags_t)zc.zc_obj;
3539 nvlist_t *prop_errors;
3540 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
3541 zc.zc_nvlist_dst_size, &prop_errors, 0));
3543 nvpair_t *prop_err = NULL;
3545 while ((prop_err = nvlist_next_nvpair(prop_errors,
3546 prop_err)) != NULL) {
3551 prop = zfs_name_to_prop(nvpair_name(prop_err));
3552 (void) nvpair_value_int32(prop_err, &intval);
3553 if (strcmp(nvpair_name(prop_err),
3554 ZPROP_N_MORE_ERRORS) == 0) {
3555 trunc_prop_errs(intval);
3557 } else if (snapname == NULL || finalsnap == NULL ||
3558 strcmp(finalsnap, snapname) == 0 ||
3559 strcmp(nvpair_name(prop_err),
3560 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
3562 * Skip the special case of, for example,
3563 * "refquota", errors on intermediate
3564 * snapshots leading up to a final one.
3565 * That's why we have all of the checks above.
3567 * See zfs_ioctl.c's extract_delay_props() for
3568 * a list of props which can fail on
3569 * intermediate snapshots, but shouldn't
3570 * affect the overall receive.
3572 (void) snprintf(tbuf, sizeof (tbuf),
3573 dgettext(TEXT_DOMAIN,
3574 "cannot receive %s property on %s"),
3575 nvpair_name(prop_err), zc.zc_name);
3576 zfs_setprop_error(hdl, prop, intval, tbuf);
3579 nvlist_free(prop_errors);
3582 zc.zc_nvlist_dst = 0;
3583 zc.zc_nvlist_dst_size = 0;
3584 zcmd_free_nvlists(&zc);
3586 if (err == 0 && snapprops_nvlist) {
3587 zfs_cmd_t zc2 = { 0 };
3589 (void) strcpy(zc2.zc_name, zc.zc_value);
3590 zc2.zc_cookie = B_TRUE; /* received */
3591 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
3592 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
3593 zcmd_free_nvlists(&zc2);
3597 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3599 * It may be that this snapshot already exists,
3600 * in which case we want to consume & ignore it
3601 * rather than failing.
3603 avl_tree_t *local_avl;
3604 nvlist_t *local_nv, *fs;
3605 cp = strchr(zc.zc_value, '@');
3608 * XXX Do this faster by just iterating over snaps in
3609 * this fs. Also if zc_value does not exist, we will
3610 * get a strange "does not exist" error message.
3613 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3614 B_FALSE, B_FALSE, &local_nv, &local_avl) == 0) {
3616 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
3617 fsavl_destroy(local_avl);
3618 nvlist_free(local_nv);
3621 if (flags->verbose) {
3622 (void) printf("snap %s already exists; "
3623 "ignoring\n", zc.zc_value);
3625 err = ioctl_err = recv_skip(hdl, infd,
3632 if (ioctl_err != 0) {
3633 switch (ioctl_errno) {
3635 cp = strchr(zc.zc_value, '@');
3637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3638 "most recent snapshot of %s does not\n"
3639 "match incremental source"), zc.zc_value);
3640 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3644 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3645 "destination %s has been modified\n"
3646 "since most recent snapshot"), zc.zc_name);
3647 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3650 cp = strchr(zc.zc_value, '@');
3652 /* it's the containing fs that exists */
3655 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3656 "destination already exists"));
3657 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
3658 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
3663 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3666 recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
3667 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3670 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3671 "pool must be upgraded to receive this stream."));
3672 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3675 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3676 "destination %s space quota exceeded"), zc.zc_name);
3677 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3680 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3685 * Mount the target filesystem (if created). Also mount any
3686 * children of the target filesystem if we did a replication
3687 * receive (indicated by stream_avl being non-NULL).
3689 cp = strchr(zc.zc_value, '@');
3690 if (cp && (ioctl_err == 0 || !newfs)) {
3694 h = zfs_open(hdl, zc.zc_value,
3695 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3697 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3699 } else if (newfs || stream_avl) {
3701 * Track the first/top of hierarchy fs,
3702 * for mounting and sharing later.
3704 if (top_zfs && *top_zfs == NULL)
3705 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3713 if (!flags->nomount)
3714 err |= changelist_postfix(clp);
3715 changelist_free(clp);
3718 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3719 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3720 "failed to clear unreceived properties on %s"),
3722 (void) fprintf(stderr, "\n");
3724 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3725 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3726 "failed to restore original properties on %s"),
3728 (void) fprintf(stderr, "\n");
3731 if (err || ioctl_err)
3734 *action_handlep = zc.zc_action_handle;
3736 if (flags->verbose) {
3739 uint64_t bytes = zc.zc_cookie;
3740 time_t delta = time(NULL) - begin_time;
3743 zfs_nicenum(bytes, buf1, sizeof (buf1));
3744 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3746 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3754 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3755 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
3756 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3757 uint64_t *action_handlep, const char *finalsnap)
3760 dmu_replay_record_t drr, drr_noswap;
3761 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3763 zio_cksum_t zcksum = { 0 };
3764 uint64_t featureflags;
3767 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3770 if (flags->isprefix &&
3771 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3772 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3773 "(%s) does not exist"), tosnap);
3774 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3777 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
3778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
3779 "(%s) does not exist"), originsnap);
3780 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3783 /* read in the BEGIN record */
3784 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3788 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3789 /* It's the double end record at the end of a package */
3793 /* the kernel needs the non-byteswapped begin record */
3796 flags->byteswap = B_FALSE;
3797 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3799 * We computed the checksum in the wrong byteorder in
3800 * recv_read() above; do it again correctly.
3802 bzero(&zcksum, sizeof (zio_cksum_t));
3803 (void) fletcher_4_incremental_byteswap(&drr,
3804 sizeof (drr), &zcksum);
3805 flags->byteswap = B_TRUE;
3807 drr.drr_type = BSWAP_32(drr.drr_type);
3808 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3809 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3810 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3811 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3812 drrb->drr_type = BSWAP_32(drrb->drr_type);
3813 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3814 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3815 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3818 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3819 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3820 "stream (bad magic number)"));
3821 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3824 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3825 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3827 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3828 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3829 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3830 "stream has unsupported feature, feature flags = %lx"),
3832 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3835 if (strchr(drrb->drr_toname, '@') == NULL) {
3836 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3837 "stream (bad snapshot name)"));
3838 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3841 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3842 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
3843 if (sendfs == NULL) {
3845 * We were not called from zfs_receive_package(). Get
3846 * the fs specified by 'zfs send'.
3849 (void) strlcpy(nonpackage_sendfs,
3850 drr.drr_u.drr_begin.drr_toname,
3851 sizeof (nonpackage_sendfs));
3852 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3854 sendfs = nonpackage_sendfs;
3855 VERIFY(finalsnap == NULL);
3857 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3858 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
3859 cleanup_fd, action_handlep, finalsnap));
3861 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3862 DMU_COMPOUNDSTREAM);
3863 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
3864 &zcksum, top_zfs, cleanup_fd, action_handlep));
3869 * Restores a backup of tosnap from the file descriptor specified by infd.
3870 * Return 0 on total success, -2 if some things couldn't be
3871 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3872 * (-1 will override -2, if -1 and the resumable flag was specified the
3873 * transfer can be resumed if the sending side supports it).
3876 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
3877 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
3879 char *top_zfs = NULL;
3882 uint64_t action_handle = 0;
3883 char *originsnap = NULL;
3885 err = nvlist_lookup_string(props, "origin", &originsnap);
3886 if (err && err != ENOENT)
3890 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3891 VERIFY(cleanup_fd >= 0);
3893 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
3894 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL);
3896 VERIFY(0 == close(cleanup_fd));
3898 if (err == 0 && !flags->nomount && top_zfs) {
3900 prop_changelist_t *clp;
3902 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3904 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3905 CL_GATHER_MOUNT_ALWAYS, 0);
3908 /* mount and share received datasets */
3909 err = changelist_postfix(clp);
3910 changelist_free(clp);
3913 if (zhp == NULL || clp == NULL || err)