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>
43 #include <sys/param.h>
44 #include <sys/mount.h>
50 #include <libzfs_core.h>
52 #include "zfs_namecheck.h"
54 #include "zfs_fletcher.h"
55 #include "libzfs_impl.h"
58 #include <sys/zio_checksum.h>
62 extern int zfs_ioctl_version;
65 /* in libzfs_dataset.c */
66 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
67 /* We need to use something for ENODATA. */
70 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
71 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
72 uint64_t *, const char *);
73 static int guid_to_name(libzfs_handle_t *, const char *,
74 uint64_t, boolean_t, char *);
76 static const zio_cksum_t zero_cksum = { 0 };
78 typedef struct dedup_arg {
81 libzfs_handle_t *dedup_hdl;
84 typedef struct progress_arg {
87 boolean_t pa_parsable;
92 typedef struct dataref {
98 typedef struct dedup_entry {
99 struct dedup_entry *dde_next;
100 zio_cksum_t dde_chksum;
105 #define MAX_DDT_PHYSMEM_PERCENT 20
106 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
108 typedef struct dedup_table {
109 dedup_entry_t **dedup_hash_array;
110 umem_cache_t *ddecache;
111 uint64_t max_ddt_size; /* max dedup table size in bytes */
112 uint64_t cur_ddt_size; /* current dedup table size in bytes */
119 high_order_bit(uint64_t n)
123 for (count = 0; n != 0; count++)
129 ssread(void *buf, size_t len, FILE *stream)
133 if ((outlen = fread(buf, len, 1, stream)) == 0)
140 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
141 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
145 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
146 if (ddt->ddt_full == B_FALSE) {
147 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
148 "Dedup table full. Deduplication will continue "
149 "with existing table entries"));
150 ddt->ddt_full = B_TRUE;
155 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
157 assert(*ddepp == NULL);
158 dde->dde_next = NULL;
159 dde->dde_chksum = *cs;
160 dde->dde_prop = prop;
163 ddt->cur_ddt_size += sizeof (dedup_entry_t);
169 * Using the specified dedup table, do a lookup for an entry with
170 * the checksum cs. If found, return the block's reference info
171 * in *dr. Otherwise, insert a new entry in the dedup table, using
172 * the reference information specified by *dr.
174 * return value: true - entry was found
175 * false - entry was not found
178 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
179 uint64_t prop, dataref_t *dr)
182 dedup_entry_t **ddepp;
184 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
186 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
187 ddepp = &((*ddepp)->dde_next)) {
188 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
189 (*ddepp)->dde_prop == prop) {
190 *dr = (*ddepp)->dde_ref;
194 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
199 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
200 zio_cksum_t *zc, int outfd)
202 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
203 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
204 (void) fletcher_4_incremental_native(drr,
205 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
206 if (drr->drr_type != DRR_BEGIN) {
207 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
208 drr_checksum.drr_checksum));
209 drr->drr_u.drr_checksum.drr_checksum = *zc;
211 (void) fletcher_4_incremental_native(
212 &drr->drr_u.drr_checksum.drr_checksum, sizeof (zio_cksum_t), zc);
213 if (write(outfd, drr, sizeof (*drr)) == -1)
215 if (payload_len != 0) {
216 (void) fletcher_4_incremental_native(payload, payload_len, zc);
217 if (write(outfd, payload, payload_len) == -1)
224 * This function is started in a separate thread when the dedup option
225 * has been requested. The main send thread determines the list of
226 * snapshots to be included in the send stream and makes the ioctl calls
227 * for each one. But instead of having the ioctl send the output to the
228 * the output fd specified by the caller of zfs_send()), the
229 * ioctl is told to direct the output to a pipe, which is read by the
230 * alternate thread running THIS function. This function does the
232 * 1. building a dedup table (the DDT)
233 * 2. doing checksums on each data block and inserting a record in the DDT
234 * 3. looking for matching checksums, and
235 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
236 * a duplicate block is found.
237 * The output of this function then goes to the output fd requested
238 * by the caller of zfs_send().
243 dedup_arg_t *dda = arg;
244 char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
245 dmu_replay_record_t thedrr;
246 dmu_replay_record_t *drr = &thedrr;
250 zio_cksum_t stream_cksum;
251 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
255 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
256 SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
258 numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
261 * numbuckets must be a power of 2. Increase number to
262 * a power of 2 if necessary.
264 if (!ISP2(numbuckets))
265 numbuckets = 1 << high_order_bit(numbuckets);
267 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
268 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
269 NULL, NULL, NULL, NULL, NULL, 0);
270 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
271 ddt.numhashbits = high_order_bit(numbuckets) - 1;
272 ddt.ddt_full = B_FALSE;
274 outfd = dda->outputfd;
275 ofp = fdopen(dda->inputfd, "r");
276 while (ssread(drr, sizeof (*drr), ofp) != 0) {
279 * kernel filled in checksum, we are going to write same
280 * record, but need to regenerate checksum.
282 if (drr->drr_type != DRR_BEGIN) {
283 bzero(&drr->drr_u.drr_checksum.drr_checksum,
284 sizeof (drr->drr_u.drr_checksum.drr_checksum));
287 switch (drr->drr_type) {
290 struct drr_begin *drrb = &drr->drr_u.drr_begin;
293 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
295 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
297 /* set the DEDUP feature flag for this stream */
298 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
299 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
300 DMU_BACKUP_FEATURE_DEDUPPROPS);
301 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
303 if (drr->drr_payloadlen != 0) {
304 sz = drr->drr_payloadlen;
306 if (sz > SPA_MAXBLOCKSIZE) {
307 buf = zfs_realloc(dda->dedup_hdl, buf,
308 SPA_MAXBLOCKSIZE, sz);
310 (void) ssread(buf, sz, ofp);
314 if (dump_record(drr, buf, sz, &stream_cksum,
322 struct drr_end *drre = &drr->drr_u.drr_end;
323 /* use the recalculated checksum */
324 drre->drr_checksum = stream_cksum;
325 if (dump_record(drr, NULL, 0, &stream_cksum,
333 struct drr_object *drro = &drr->drr_u.drr_object;
334 if (drro->drr_bonuslen > 0) {
336 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
339 if (dump_record(drr, buf,
340 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
341 &stream_cksum, outfd) != 0)
348 struct drr_spill *drrs = &drr->drr_u.drr_spill;
349 (void) ssread(buf, drrs->drr_length, ofp);
350 if (dump_record(drr, buf, drrs->drr_length,
351 &stream_cksum, outfd) != 0)
356 case DRR_FREEOBJECTS:
358 if (dump_record(drr, NULL, 0, &stream_cksum,
366 struct drr_write *drrw = &drr->drr_u.drr_write;
368 uint64_t payload_size;
370 payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
371 (void) ssread(buf, payload_size, ofp);
374 * Use the existing checksum if it's dedup-capable,
375 * else calculate a SHA256 checksum for it.
378 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
380 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
382 zio_cksum_t tmpsha256;
385 SHA256Update(&ctx, buf, payload_size);
386 SHA256Final(&tmpsha256, &ctx);
387 drrw->drr_key.ddk_cksum.zc_word[0] =
388 BE_64(tmpsha256.zc_word[0]);
389 drrw->drr_key.ddk_cksum.zc_word[1] =
390 BE_64(tmpsha256.zc_word[1]);
391 drrw->drr_key.ddk_cksum.zc_word[2] =
392 BE_64(tmpsha256.zc_word[2]);
393 drrw->drr_key.ddk_cksum.zc_word[3] =
394 BE_64(tmpsha256.zc_word[3]);
395 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
396 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
399 dataref.ref_guid = drrw->drr_toguid;
400 dataref.ref_object = drrw->drr_object;
401 dataref.ref_offset = drrw->drr_offset;
403 if (ddt_update(dda->dedup_hdl, &ddt,
404 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
406 dmu_replay_record_t wbr_drr = {0};
407 struct drr_write_byref *wbr_drrr =
408 &wbr_drr.drr_u.drr_write_byref;
410 /* block already present in stream */
411 wbr_drr.drr_type = DRR_WRITE_BYREF;
413 wbr_drrr->drr_object = drrw->drr_object;
414 wbr_drrr->drr_offset = drrw->drr_offset;
415 wbr_drrr->drr_length = drrw->drr_logical_size;
416 wbr_drrr->drr_toguid = drrw->drr_toguid;
417 wbr_drrr->drr_refguid = dataref.ref_guid;
418 wbr_drrr->drr_refobject =
420 wbr_drrr->drr_refoffset =
423 wbr_drrr->drr_checksumtype =
424 drrw->drr_checksumtype;
425 wbr_drrr->drr_checksumflags =
426 drrw->drr_checksumtype;
427 wbr_drrr->drr_key.ddk_cksum =
428 drrw->drr_key.ddk_cksum;
429 wbr_drrr->drr_key.ddk_prop =
430 drrw->drr_key.ddk_prop;
432 if (dump_record(&wbr_drr, NULL, 0,
433 &stream_cksum, outfd) != 0)
436 /* block not previously seen */
437 if (dump_record(drr, buf, payload_size,
438 &stream_cksum, outfd) != 0)
444 case DRR_WRITE_EMBEDDED:
446 struct drr_write_embedded *drrwe =
447 &drr->drr_u.drr_write_embedded;
449 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
450 if (dump_record(drr, buf,
451 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
452 &stream_cksum, outfd) != 0)
459 if (dump_record(drr, NULL, 0, &stream_cksum,
466 (void) fprintf(stderr, "INVALID record type 0x%x\n",
468 /* should never happen, so assert */
473 umem_cache_destroy(ddt.ddecache);
474 free(ddt.dedup_hash_array);
482 * Routines for dealing with the AVL tree of fs-nvlists
484 typedef struct fsavl_node {
492 fsavl_compare(const void *arg1, const void *arg2)
494 const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
495 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
497 return (AVL_CMP(fn1->fn_guid, fn2->fn_guid));
501 * Given the GUID of a snapshot, find its containing filesystem and
505 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
507 fsavl_node_t fn_find;
510 fn_find.fn_guid = snapguid;
512 fn = avl_find(avl, &fn_find, NULL);
515 *snapname = fn->fn_snapname;
516 return (fn->fn_nvfs);
522 fsavl_destroy(avl_tree_t *avl)
531 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
538 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
541 fsavl_create(nvlist_t *fss)
544 nvpair_t *fselem = NULL;
546 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
549 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
550 offsetof(fsavl_node_t, fn_node));
552 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
553 nvlist_t *nvfs, *snaps;
554 nvpair_t *snapelem = NULL;
556 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
557 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
560 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
564 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
565 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
566 fsavl_destroy(fsavl);
570 fn->fn_snapname = nvpair_name(snapelem);
574 * Note: if there are multiple snaps with the
575 * same GUID, we ignore all but one.
577 if (avl_find(fsavl, fn, NULL) == NULL)
588 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
590 typedef struct send_data {
592 * assigned inside every recursive call,
593 * restored from *_save on return:
595 * guid of fromsnap snapshot in parent dataset
596 * txg of fromsnap snapshot in current dataset
597 * txg of tosnap snapshot in current dataset
600 uint64_t parent_fromsnap_guid;
601 uint64_t fromsnap_txg;
604 /* the nvlists get accumulated during depth-first traversal */
605 nvlist_t *parent_snaps;
609 /* send-receive configuration, does not change during traversal */
611 const char *fromsnap;
617 * The header nvlist is of the following format:
620 * "fromsnap" -> string (if incremental)
624 * "name" -> string (full name; for debugging)
625 * "parentfromsnap" -> number (guid of fromsnap in parent)
627 * "props" -> { name -> value (only if set here) }
628 * "snaps" -> { name (lastname) -> number (guid) }
629 * "snapprops" -> { name (lastname) -> { name -> value } }
631 * "origin" -> number (guid) (if clone)
632 * "sent" -> boolean (not on-disk)
640 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
643 send_iterate_snap(zfs_handle_t *zhp, void *arg)
645 send_data_t *sd = arg;
646 uint64_t guid = zhp->zfs_dmustats.dds_guid;
647 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
651 snapname = strrchr(zhp->zfs_name, '@')+1;
653 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
655 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
656 "skipping snapshot %s because it was created "
657 "after the destination snapshot (%s)\n"),
658 zhp->zfs_name, sd->tosnap);
664 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
666 * NB: if there is no fromsnap here (it's a newly created fs in
667 * an incremental replication), we will substitute the tosnap.
669 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
670 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
671 strcmp(snapname, sd->tosnap) == 0)) {
672 sd->parent_fromsnap_guid = guid;
675 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
676 send_iterate_prop(zhp, nv);
677 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
685 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
687 nvpair_t *elem = NULL;
689 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
690 char *propname = nvpair_name(elem);
691 zfs_prop_t prop = zfs_name_to_prop(propname);
694 if (!zfs_prop_user(propname)) {
696 * Realistically, this should never happen. However,
697 * we want the ability to add DSL properties without
698 * needing to make incompatible version changes. We
699 * need to ignore unknown properties to allow older
700 * software to still send datasets containing these
701 * properties, with the unknown properties elided.
703 if (prop == ZPROP_INVAL)
706 if (zfs_prop_readonly(prop))
710 verify(nvpair_value_nvlist(elem, &propnv) == 0);
711 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
712 prop == ZFS_PROP_REFQUOTA ||
713 prop == ZFS_PROP_REFRESERVATION) {
716 verify(nvlist_lookup_uint64(propnv,
717 ZPROP_VALUE, &value) == 0);
718 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
721 * May have no source before SPA_VERSION_RECVD_PROPS,
722 * but is still modifiable.
724 if (nvlist_lookup_string(propnv,
725 ZPROP_SOURCE, &source) == 0) {
726 if ((strcmp(source, zhp->zfs_name) != 0) &&
728 ZPROP_SOURCE_VAL_RECVD) != 0))
733 if (nvlist_lookup_string(propnv,
734 ZPROP_SOURCE, &source) != 0)
736 if ((strcmp(source, zhp->zfs_name) != 0) &&
737 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
741 if (zfs_prop_user(propname) ||
742 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
744 verify(nvlist_lookup_string(propnv,
745 ZPROP_VALUE, &value) == 0);
746 VERIFY(0 == nvlist_add_string(nv, propname, value));
749 verify(nvlist_lookup_uint64(propnv,
750 ZPROP_VALUE, &value) == 0);
751 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
757 * returns snapshot creation txg
758 * and returns 0 if the snapshot does not exist
761 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
763 char name[ZFS_MAX_DATASET_NAME_LEN];
766 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
769 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
770 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
771 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
773 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
782 * recursively generate nvlists describing datasets. See comment
783 * for the data structure send_data_t above for description of contents
787 send_iterate_fs(zfs_handle_t *zhp, void *arg)
789 send_data_t *sd = arg;
792 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
793 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
794 uint64_t tosnap_txg_save = sd->tosnap_txg;
795 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
796 uint64_t guid = zhp->zfs_dmustats.dds_guid;
797 uint64_t fromsnap_txg, tosnap_txg;
800 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
801 if (fromsnap_txg != 0)
802 sd->fromsnap_txg = fromsnap_txg;
804 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
806 sd->tosnap_txg = tosnap_txg;
809 * on the send side, if the current dataset does not have tosnap,
810 * perform two additional checks:
812 * - skip sending the current dataset if it was created later than
814 * - return error if the current dataset was created earlier than
817 if (sd->tosnap != NULL && tosnap_txg == 0) {
818 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
820 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
821 "skipping dataset %s: snapshot %s does "
822 "not exist\n"), zhp->zfs_name, sd->tosnap);
825 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
826 "cannot send %s@%s%s: snapshot %s@%s does not "
827 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
828 dgettext(TEXT_DOMAIN, " recursively") : "",
829 zhp->zfs_name, sd->tosnap);
835 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
836 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
837 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
838 sd->parent_fromsnap_guid));
840 if (zhp->zfs_dmustats.dds_origin[0]) {
841 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
842 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
843 if (origin == NULL) {
847 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
848 origin->zfs_dmustats.dds_guid));
851 /* iterate over props */
852 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
853 send_iterate_prop(zhp, nv);
854 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
857 /* iterate over snaps, and set sd->parent_fromsnap_guid */
858 sd->parent_fromsnap_guid = 0;
859 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
860 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
861 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd);
862 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
863 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
864 nvlist_free(sd->parent_snaps);
865 nvlist_free(sd->snapprops);
867 /* add this fs to nvlist */
868 (void) snprintf(guidstring, sizeof (guidstring),
869 "0x%llx", (longlong_t)guid);
870 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
873 /* iterate over children */
875 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
878 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
879 sd->fromsnap_txg = fromsnap_txg_save;
880 sd->tosnap_txg = tosnap_txg_save;
887 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
888 const char *tosnap, boolean_t recursive, boolean_t verbose,
889 nvlist_t **nvlp, avl_tree_t **avlp)
892 send_data_t sd = { 0 };
895 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
897 return (EZFS_BADTYPE);
899 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
901 sd.fromsnap = fromsnap;
903 sd.recursive = recursive;
904 sd.verbose = verbose;
906 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
914 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
925 * Routines specific to "zfs send"
927 typedef struct send_dump_data {
928 /* these are all just the short snapname (the part after the @) */
929 const char *fromsnap;
931 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
932 uint64_t prevsnap_obj;
933 boolean_t seenfrom, seento, replicate, doall, fromorigin;
934 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
935 boolean_t progressastitle;
936 boolean_t large_block, compress;
942 snapfilter_cb_t *filter_cb;
945 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
951 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
952 boolean_t fromorigin, enum lzc_send_flags flags, uint64_t *sizep)
954 zfs_cmd_t zc = { 0 };
955 libzfs_handle_t *hdl = zhp->zfs_hdl;
957 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
958 assert(fromsnap_obj == 0 || !fromorigin);
960 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
961 zc.zc_obj = fromorigin;
962 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
963 zc.zc_fromobj = fromsnap_obj;
964 zc.zc_guid = 1; /* estimate flag */
967 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
969 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
970 "warning: cannot estimate space for '%s'"), zhp->zfs_name);
974 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
975 "not an earlier snapshot from the same fs"));
976 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
979 if (zfs_dataset_exists(hdl, zc.zc_name,
980 ZFS_TYPE_SNAPSHOT)) {
981 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
982 "incremental source (@%s) does not exist"),
985 return (zfs_error(hdl, EZFS_NOENT, errbuf));
997 zfs_error_aux(hdl, strerror(errno));
998 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1001 return (zfs_standard_error(hdl, errno, errbuf));
1005 *sizep = zc.zc_objset_type;
1011 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1012 * NULL) to the file descriptor specified by outfd.
1015 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
1016 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
1019 zfs_cmd_t zc = { 0 };
1020 libzfs_handle_t *hdl = zhp->zfs_hdl;
1023 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1024 assert(fromsnap_obj == 0 || !fromorigin);
1026 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1027 zc.zc_cookie = outfd;
1028 zc.zc_obj = fromorigin;
1029 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1030 zc.zc_fromobj = fromsnap_obj;
1031 zc.zc_flags = flags;
1033 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
1034 if (fromsnap && fromsnap[0] != '\0') {
1035 VERIFY(0 == nvlist_add_string(thisdbg,
1036 "fromsnap", fromsnap));
1039 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1041 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1042 "warning: cannot send '%s'"), zhp->zfs_name);
1044 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
1046 VERIFY(0 == nvlist_add_nvlist(debugnv,
1047 zhp->zfs_name, thisdbg));
1049 nvlist_free(thisdbg);
1053 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1054 "not an earlier snapshot from the same fs"));
1055 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1058 if (zfs_dataset_exists(hdl, zc.zc_name,
1059 ZFS_TYPE_SNAPSHOT)) {
1060 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1061 "incremental source (@%s) does not exist"),
1064 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1079 zfs_error_aux(hdl, strerror(errno));
1080 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1083 return (zfs_standard_error(hdl, errno, errbuf));
1088 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1089 nvlist_free(thisdbg);
1095 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1097 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1100 * zfs_send() only sets snapholds for sends that need them,
1101 * e.g. replication and doall.
1103 if (sdd->snapholds == NULL)
1106 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1110 send_progress_thread(void *arg)
1112 progress_arg_t *pa = arg;
1113 zfs_cmd_t zc = { 0 };
1114 zfs_handle_t *zhp = pa->pa_zhp;
1115 libzfs_handle_t *hdl = zhp->zfs_hdl;
1116 unsigned long long bytes, total;
1121 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1123 if (!pa->pa_parsable && !pa->pa_astitle)
1124 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1127 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1132 zc.zc_cookie = pa->pa_fd;
1133 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1134 return ((void *)-1);
1138 bytes = zc.zc_cookie;
1140 if (pa->pa_astitle) {
1142 if (pa->pa_size > bytes)
1143 pct = 100 * bytes / pa->pa_size;
1147 setproctitle("sending %s (%d%%: %llu/%llu)",
1148 zhp->zfs_name, pct, bytes, pa->pa_size);
1149 } else if (pa->pa_parsable) {
1150 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1151 tm->tm_hour, tm->tm_min, tm->tm_sec,
1152 bytes, zhp->zfs_name);
1154 zfs_nicenum(bytes, buf, sizeof (buf));
1155 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1156 tm->tm_hour, tm->tm_min, tm->tm_sec,
1157 buf, zhp->zfs_name);
1163 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1164 uint64_t size, boolean_t parsable)
1167 if (fromsnap != NULL) {
1168 (void) fprintf(fout, "incremental\t%s\t%s",
1171 (void) fprintf(fout, "full\t%s",
1175 if (fromsnap != NULL) {
1176 if (strchr(fromsnap, '@') == NULL &&
1177 strchr(fromsnap, '#') == NULL) {
1178 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1179 "send from @%s to %s"),
1182 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1183 "send from %s to %s"),
1187 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1194 (void) fprintf(fout, "\t%llu",
1196 } else if (size != 0) {
1198 zfs_nicenum(size, buf, sizeof (buf));
1199 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1200 " estimated size is %s"), buf);
1202 (void) fprintf(fout, "\n");
1206 dump_snapshot(zfs_handle_t *zhp, void *arg)
1208 send_dump_data_t *sdd = arg;
1209 progress_arg_t pa = { 0 };
1212 enum lzc_send_flags flags = 0;
1214 boolean_t isfromsnap, istosnap, fromorigin;
1215 boolean_t exclude = B_FALSE;
1216 FILE *fout = sdd->std_out ? stdout : stderr;
1220 thissnap = strchr(zhp->zfs_name, '@') + 1;
1221 isfromsnap = (sdd->fromsnap != NULL &&
1222 strcmp(sdd->fromsnap, thissnap) == 0);
1224 if (!sdd->seenfrom && isfromsnap) {
1225 gather_holds(zhp, sdd);
1226 sdd->seenfrom = B_TRUE;
1227 (void) strcpy(sdd->prevsnap, thissnap);
1228 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1233 if (sdd->seento || !sdd->seenfrom) {
1238 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1240 sdd->seento = B_TRUE;
1242 if (sdd->large_block)
1243 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1244 if (sdd->embed_data)
1245 flags |= LZC_SEND_FLAG_EMBED_DATA;
1247 flags |= LZC_SEND_FLAG_COMPRESS;
1249 if (!sdd->doall && !isfromsnap && !istosnap) {
1250 if (sdd->replicate) {
1252 nvlist_t *snapprops;
1254 * Filter out all intermediate snapshots except origin
1255 * snapshots needed to replicate clones.
1257 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1258 zhp->zfs_dmustats.dds_guid, &snapname);
1260 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1261 "snapprops", &snapprops));
1262 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1263 thissnap, &snapprops));
1264 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1271 * If a filter function exists, call it to determine whether
1272 * this snapshot will be sent.
1274 if (exclude || (sdd->filter_cb != NULL &&
1275 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1277 * This snapshot is filtered out. Don't send it, and don't
1278 * set prevsnap_obj, so it will be as if this snapshot didn't
1279 * exist, and the next accepted snapshot will be sent as
1280 * an incremental from the last accepted one, or as the
1281 * first (and full) snapshot in the case of a replication,
1282 * non-incremental send.
1288 gather_holds(zhp, sdd);
1289 fromorigin = sdd->prevsnap[0] == '\0' &&
1290 (sdd->fromorigin || sdd->replicate);
1292 if (sdd->verbose || sdd->progress) {
1293 (void) estimate_ioctl(zhp, sdd->prevsnap_obj,
1294 fromorigin, flags, &size);
1297 send_print_verbose(fout, zhp->zfs_name,
1298 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1299 size, sdd->parsable);
1304 * If progress reporting is requested, spawn a new thread to
1305 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1307 if (sdd->progress) {
1309 pa.pa_fd = sdd->outfd;
1310 pa.pa_parsable = sdd->parsable;
1311 pa.pa_size = sdd->size;
1312 pa.pa_astitle = sdd->progressastitle;
1314 if ((err = pthread_create(&tid, NULL,
1315 send_progress_thread, &pa)) != 0) {
1321 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1322 fromorigin, sdd->outfd, flags, sdd->debugnv);
1324 if (sdd->progress) {
1325 (void) pthread_cancel(tid);
1326 (void) pthread_join(tid, NULL);
1330 (void) strcpy(sdd->prevsnap, thissnap);
1331 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1337 dump_filesystem(zfs_handle_t *zhp, void *arg)
1340 send_dump_data_t *sdd = arg;
1341 boolean_t missingfrom = B_FALSE;
1342 zfs_cmd_t zc = { 0 };
1344 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1345 zhp->zfs_name, sdd->tosnap);
1346 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1347 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1348 "WARNING: could not send %s@%s: does not exist\n"),
1349 zhp->zfs_name, sdd->tosnap);
1354 if (sdd->replicate && sdd->fromsnap) {
1356 * If this fs does not have fromsnap, and we're doing
1357 * recursive, we need to send a full stream from the
1358 * beginning (or an incremental from the origin if this
1359 * is a clone). If we're doing non-recursive, then let
1360 * them get the error.
1362 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1363 zhp->zfs_name, sdd->fromsnap);
1364 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1365 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1366 missingfrom = B_TRUE;
1370 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1371 sdd->prevsnap_obj = 0;
1372 if (sdd->fromsnap == NULL || missingfrom)
1373 sdd->seenfrom = B_TRUE;
1375 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1376 if (!sdd->seenfrom) {
1377 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1378 "WARNING: could not send %s@%s:\n"
1379 "incremental source (%s@%s) does not exist\n"),
1380 zhp->zfs_name, sdd->tosnap,
1381 zhp->zfs_name, sdd->fromsnap);
1383 } else if (!sdd->seento) {
1384 if (sdd->fromsnap) {
1385 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1386 "WARNING: could not send %s@%s:\n"
1387 "incremental source (%s@%s) "
1388 "is not earlier than it\n"),
1389 zhp->zfs_name, sdd->tosnap,
1390 zhp->zfs_name, sdd->fromsnap);
1392 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1394 "could not send %s@%s: does not exist\n"),
1395 zhp->zfs_name, sdd->tosnap);
1404 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1406 send_dump_data_t *sdd = arg;
1408 boolean_t needagain, progress;
1410 if (!sdd->replicate)
1411 return (dump_filesystem(rzhp, sdd));
1413 /* Mark the clone origin snapshots. */
1414 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1415 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1417 uint64_t origin_guid = 0;
1419 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1420 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1421 if (origin_guid != 0) {
1423 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1424 origin_guid, &snapname);
1425 if (origin_nv != NULL) {
1426 nvlist_t *snapprops;
1427 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1428 "snapprops", &snapprops));
1429 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1430 snapname, &snapprops));
1431 VERIFY(0 == nvlist_add_boolean(
1432 snapprops, "is_clone_origin"));
1437 needagain = progress = B_FALSE;
1438 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1439 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1440 nvlist_t *fslist, *parent_nv;
1444 uint64_t origin_guid = 0;
1445 uint64_t parent_guid = 0;
1447 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1448 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1451 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1452 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1453 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1456 if (parent_guid != 0) {
1457 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1458 if (!nvlist_exists(parent_nv, "sent")) {
1459 /* parent has not been sent; skip this one */
1465 if (origin_guid != 0) {
1466 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1468 if (origin_nv != NULL &&
1469 !nvlist_exists(origin_nv, "sent")) {
1471 * origin has not been sent yet;
1479 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1482 err = dump_filesystem(zhp, sdd);
1483 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1494 /* clean out the sent flags in case we reuse this fss */
1495 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1496 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1499 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1500 (void) nvlist_remove_all(fslist, "sent");
1507 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1509 unsigned int version;
1511 unsigned long long checksum, packed_len;
1514 * Decode token header, which is:
1515 * <token version>-<checksum of payload>-<uncompressed payload length>
1516 * Note that the only supported token version is 1.
1518 nread = sscanf(token, "%u-%llx-%llx-",
1519 &version, &checksum, &packed_len);
1521 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1522 "resume token is corrupt (invalid format)"));
1526 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1528 "resume token is corrupt (invalid version %u)"),
1533 /* convert hexadecimal representation to binary */
1534 token = strrchr(token, '-') + 1;
1535 int len = strlen(token) / 2;
1536 unsigned char *compressed = zfs_alloc(hdl, len);
1537 for (int i = 0; i < len; i++) {
1538 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1542 "resume token is corrupt "
1543 "(payload is not hex-encoded)"));
1548 /* verify checksum */
1550 fletcher_4_native(compressed, len, NULL, &cksum);
1551 if (cksum.zc_word[0] != checksum) {
1553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1554 "resume token is corrupt (incorrect checksum)"));
1559 void *packed = zfs_alloc(hdl, packed_len);
1560 uLongf packed_len_long = packed_len;
1561 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1562 packed_len_long != packed_len) {
1565 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1566 "resume token is corrupt (decompression failed)"));
1572 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1576 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1577 "resume token is corrupt (nvlist_unpack failed)"));
1584 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1585 const char *resume_token)
1589 char *fromname = NULL;
1590 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1593 char name[ZFS_MAX_DATASET_NAME_LEN];
1594 enum lzc_send_flags lzc_flags = 0;
1596 FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr;
1598 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1599 "cannot resume send"));
1601 nvlist_t *resume_nvl =
1602 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1603 if (resume_nvl == NULL) {
1605 * zfs_error_aux has already been set by
1606 * zfs_send_resume_token_to_nvlist
1608 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1610 if (flags->verbose) {
1611 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1612 "resume token contents:\n"));
1613 nvlist_print(fout, resume_nvl);
1616 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1617 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1618 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1619 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1620 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1621 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1622 "resume token is corrupt"));
1623 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1626 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1628 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1629 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1630 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1631 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1632 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1633 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1635 if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1636 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1637 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1638 "'%s' is no longer the same snapshot used in "
1639 "the initial send"), toname);
1641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1642 "'%s' used in the initial send no longer exists"),
1645 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1647 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1649 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1650 "unable to access '%s'"), name);
1651 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1654 if (fromguid != 0) {
1655 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1656 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1657 "incremental source %#llx no longer exists"),
1658 (longlong_t)fromguid);
1659 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1664 if (flags->progress || flags->verbose) {
1665 error = lzc_send_space(zhp->zfs_name, fromname,
1668 size = MAX(0, (int64_t)(size - bytes));
1670 if (flags->verbose) {
1671 send_print_verbose(fout, zhp->zfs_name, fromname,
1672 size, flags->parsable);
1675 if (!flags->dryrun) {
1676 progress_arg_t pa = { 0 };
1679 * If progress reporting is requested, spawn a new thread to
1680 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1682 if (flags->progress) {
1685 pa.pa_parsable = flags->parsable;
1687 pa.pa_astitle = flags->progressastitle;
1689 error = pthread_create(&tid, NULL,
1690 send_progress_thread, &pa);
1697 error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1698 lzc_flags, resumeobj, resumeoff);
1700 if (flags->progress) {
1701 (void) pthread_cancel(tid);
1702 (void) pthread_join(tid, NULL);
1706 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1707 "warning: cannot send '%s'"), zhp->zfs_name);
1729 zfs_error_aux(hdl, strerror(errno));
1730 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1733 return (zfs_standard_error(hdl, errno, errbuf));
1744 * Generate a send stream for the dataset identified by the argument zhp.
1746 * The content of the send stream is the snapshot identified by
1747 * 'tosnap'. Incremental streams are requested in two ways:
1748 * - from the snapshot identified by "fromsnap" (if non-null) or
1749 * - from the origin of the dataset identified by zhp, which must
1750 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1753 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1754 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1755 * if "replicate" is set. If "doall" is set, dump all the intermediate
1756 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1757 * case too. If "props" is set, send properties.
1760 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1761 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1762 void *cb_arg, nvlist_t **debugnvp)
1765 send_dump_data_t sdd = { 0 };
1767 nvlist_t *fss = NULL;
1768 avl_tree_t *fsavl = NULL;
1769 static uint64_t holdseq;
1773 dedup_arg_t dda = { 0 };
1774 int featureflags = 0;
1777 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1778 "cannot send '%s'"), zhp->zfs_name);
1780 if (fromsnap && fromsnap[0] == '\0') {
1781 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1782 "zero-length incremental source"));
1783 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1786 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1788 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1789 if (version >= ZPL_VERSION_SA) {
1790 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1794 if (flags->dedup && !flags->dryrun) {
1795 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1796 DMU_BACKUP_FEATURE_DEDUPPROPS);
1797 if ((err = pipe(pipefd)) != 0) {
1798 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1799 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1802 dda.outputfd = outfd;
1803 dda.inputfd = pipefd[1];
1804 dda.dedup_hdl = zhp->zfs_hdl;
1805 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1806 (void) close(pipefd[0]);
1807 (void) close(pipefd[1]);
1808 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1809 return (zfs_error(zhp->zfs_hdl,
1810 EZFS_THREADCREATEFAILED, errbuf));
1814 if (flags->replicate || flags->doall || flags->props) {
1815 dmu_replay_record_t drr = { 0 };
1816 char *packbuf = NULL;
1818 zio_cksum_t zc = { 0 };
1820 if (flags->replicate || flags->props) {
1823 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1825 VERIFY(0 == nvlist_add_string(hdrnv,
1826 "fromsnap", fromsnap));
1828 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1829 if (!flags->replicate) {
1830 VERIFY(0 == nvlist_add_boolean(hdrnv,
1834 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1835 fromsnap, tosnap, flags->replicate, flags->verbose,
1839 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1840 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1850 if (!flags->dryrun) {
1851 /* write first begin record */
1852 drr.drr_type = DRR_BEGIN;
1853 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1854 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1855 drr_versioninfo, DMU_COMPOUNDSTREAM);
1856 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1857 drr_versioninfo, featureflags);
1858 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1859 sizeof (drr.drr_u.drr_begin.drr_toname),
1860 "%s@%s", zhp->zfs_name, tosnap);
1861 drr.drr_payloadlen = buflen;
1863 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1868 /* write end record */
1869 bzero(&drr, sizeof (drr));
1870 drr.drr_type = DRR_END;
1871 drr.drr_u.drr_end.drr_checksum = zc;
1872 err = write(outfd, &drr, sizeof (drr));
1882 /* dump each stream */
1883 sdd.fromsnap = fromsnap;
1884 sdd.tosnap = tosnap;
1886 sdd.outfd = pipefd[0];
1889 sdd.replicate = flags->replicate;
1890 sdd.doall = flags->doall;
1891 sdd.fromorigin = flags->fromorigin;
1894 sdd.verbose = flags->verbose;
1895 sdd.parsable = flags->parsable;
1896 sdd.progress = flags->progress;
1897 sdd.progressastitle = flags->progressastitle;
1898 sdd.dryrun = flags->dryrun;
1899 sdd.large_block = flags->largeblock;
1900 sdd.embed_data = flags->embed_data;
1901 sdd.compress = flags->compress;
1902 sdd.filter_cb = filter_func;
1903 sdd.filter_cb_arg = cb_arg;
1905 sdd.debugnv = *debugnvp;
1906 if (sdd.verbose && sdd.dryrun)
1907 sdd.std_out = B_TRUE;
1908 fout = sdd.std_out ? stdout : stderr;
1911 * Some flags require that we place user holds on the datasets that are
1912 * being sent so they don't get destroyed during the send. We can skip
1913 * this step if the pool is imported read-only since the datasets cannot
1916 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1917 ZPOOL_PROP_READONLY, NULL) &&
1918 zfs_spa_version(zhp, &spa_version) == 0 &&
1919 spa_version >= SPA_VERSION_USERREFS &&
1920 (flags->doall || flags->replicate)) {
1922 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1923 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1924 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1925 if (sdd.cleanup_fd < 0) {
1929 sdd.snapholds = fnvlist_alloc();
1931 sdd.cleanup_fd = -1;
1932 sdd.snapholds = NULL;
1934 if (flags->progress || flags->verbose || sdd.snapholds != NULL) {
1936 * Do a verbose no-op dry run to get all the verbose output
1937 * or to gather snapshot hold's before generating any data,
1938 * then do a non-verbose real run to generate the streams.
1940 sdd.dryrun = B_TRUE;
1941 err = dump_filesystems(zhp, &sdd);
1946 if (flags->verbose) {
1947 if (flags->parsable) {
1948 (void) fprintf(fout, "size\t%llu\n",
1949 (longlong_t)sdd.size);
1952 zfs_nicenum(sdd.size, buf, sizeof (buf));
1953 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1954 "total estimated size is %s\n"), buf);
1958 /* Ensure no snaps found is treated as an error. */
1964 /* Skip the second run if dryrun was requested. */
1968 if (sdd.snapholds != NULL) {
1969 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
1973 fnvlist_free(sdd.snapholds);
1974 sdd.snapholds = NULL;
1977 sdd.dryrun = B_FALSE;
1978 sdd.verbose = B_FALSE;
1981 err = dump_filesystems(zhp, &sdd);
1982 fsavl_destroy(fsavl);
1985 /* Ensure no snaps found is treated as an error. */
1986 if (err == 0 && !sdd.seento)
1991 (void) pthread_cancel(tid);
1992 (void) close(pipefd[0]);
1993 (void) pthread_join(tid, NULL);
1996 if (sdd.cleanup_fd != -1) {
1997 VERIFY(0 == close(sdd.cleanup_fd));
1998 sdd.cleanup_fd = -1;
2001 if (!flags->dryrun && (flags->replicate || flags->doall ||
2004 * write final end record. NB: want to do this even if
2005 * there was some error, because it might not be totally
2008 dmu_replay_record_t drr = { 0 };
2009 drr.drr_type = DRR_END;
2010 if (write(outfd, &drr, sizeof (drr)) == -1) {
2011 return (zfs_standard_error(zhp->zfs_hdl,
2016 return (err || sdd.err);
2019 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2021 fsavl_destroy(fsavl);
2023 fnvlist_free(sdd.snapholds);
2025 if (sdd.cleanup_fd != -1)
2026 VERIFY(0 == close(sdd.cleanup_fd));
2028 (void) pthread_cancel(tid);
2029 (void) close(pipefd[0]);
2030 (void) pthread_join(tid, NULL);
2036 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t flags)
2039 libzfs_handle_t *hdl = zhp->zfs_hdl;
2040 enum lzc_send_flags lzc_flags = 0;
2041 FILE *fout = (flags.verbose && flags.dryrun) ? stdout : stderr;
2044 if (flags.largeblock)
2045 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
2046 if (flags.embed_data)
2047 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
2049 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
2051 if (flags.verbose) {
2053 err = lzc_send_space(zhp->zfs_name, from, lzc_flags, &size);
2055 send_print_verbose(fout, zhp->zfs_name, from, size,
2057 if (flags.parsable) {
2058 (void) fprintf(fout, "size\t%llu\n",
2062 zfs_nicenum(size, buf, sizeof (buf));
2063 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
2064 "total estimated size is %s\n"), buf);
2067 (void) fprintf(stderr, "Cannot estimate send size: "
2068 "%s\n", strerror(errno));
2075 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2076 "warning: cannot send '%s'"), zhp->zfs_name);
2078 err = lzc_send(zhp->zfs_name, from, fd, lzc_flags);
2082 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2083 "not an earlier snapshot from the same fs"));
2084 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2088 if (lzc_exists(zhp->zfs_name)) {
2089 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2090 "incremental source (%s) does not exist"),
2093 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2096 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2097 "target is busy; if a filesystem, "
2098 "it must not be mounted"));
2099 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2114 zfs_error_aux(hdl, strerror(errno));
2115 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2118 return (zfs_standard_error(hdl, errno, errbuf));
2125 * Routines specific to "zfs recv"
2129 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2130 boolean_t byteswap, zio_cksum_t *zc)
2136 assert(ilen <= SPA_MAXBLOCKSIZE);
2139 rv = read(fd, cp, len);
2144 if (rv < 0 || len != 0) {
2145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2146 "failed to read from stream"));
2147 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2148 "cannot receive")));
2153 (void) fletcher_4_incremental_byteswap(buf, ilen, zc);
2155 (void) fletcher_4_incremental_native(buf, ilen, zc);
2161 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2162 boolean_t byteswap, zio_cksum_t *zc)
2167 buf = zfs_alloc(hdl, len);
2171 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2177 err = nvlist_unpack(buf, len, nvp, 0);
2180 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2181 "stream (malformed nvlist)"));
2188 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2189 int baselen, char *newname, recvflags_t *flags)
2193 prop_changelist_t *clp;
2196 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2199 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2200 flags->force ? MS_FORCE : 0);
2204 err = changelist_prefix(clp);
2209 (void) strcpy(newname, tryname);
2210 if (flags->verbose) {
2211 (void) printf("attempting rename %s to %s\n",
2214 err = lzc_rename(name, newname);
2216 changelist_rename(clp, name, tryname);
2221 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2224 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2225 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2226 if (flags->verbose) {
2227 (void) printf("failed - trying rename %s to %s\n",
2230 err = lzc_rename(name, newname);
2232 changelist_rename(clp, name, newname);
2233 if (err && flags->verbose) {
2234 (void) printf("failed (%u) - "
2235 "will try again on next pass\n", errno);
2238 } else if (flags->verbose) {
2240 (void) printf("success\n");
2242 (void) printf("failed (%u)\n", errno);
2245 (void) changelist_postfix(clp);
2246 changelist_free(clp);
2252 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2253 char *newname, recvflags_t *flags)
2256 prop_changelist_t *clp;
2258 boolean_t defer = B_FALSE;
2261 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2264 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2265 flags->force ? MS_FORCE : 0);
2266 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2267 zfs_spa_version(zhp, &spa_version) == 0 &&
2268 spa_version >= SPA_VERSION_USERREFS)
2273 err = changelist_prefix(clp);
2278 (void) printf("attempting destroy %s\n", name);
2279 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2280 nvlist_t *nv = fnvlist_alloc();
2281 fnvlist_add_boolean(nv, name);
2282 err = lzc_destroy_snaps(nv, defer, NULL);
2285 err = lzc_destroy(name);
2289 (void) printf("success\n");
2290 changelist_remove(clp, name);
2293 (void) changelist_postfix(clp);
2294 changelist_free(clp);
2297 * Deferred destroy might destroy the snapshot or only mark it to be
2298 * destroyed later, and it returns success in either case.
2300 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2301 ZFS_TYPE_SNAPSHOT))) {
2302 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2308 typedef struct guid_to_name_data {
2310 boolean_t bookmark_ok;
2313 } guid_to_name_data_t;
2316 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2318 guid_to_name_data_t *gtnd = arg;
2322 if (gtnd->skip != NULL &&
2323 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2324 strcmp(slash + 1, gtnd->skip) == 0) {
2329 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2330 (void) strcpy(gtnd->name, zhp->zfs_name);
2335 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2336 if (err != EEXIST && gtnd->bookmark_ok)
2337 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2343 * Attempt to find the local dataset associated with this guid. In the case of
2344 * multiple matches, we attempt to find the "best" match by searching
2345 * progressively larger portions of the hierarchy. This allows one to send a
2346 * tree of datasets individually and guarantee that we will find the source
2347 * guid within that hierarchy, even if there are multiple matches elsewhere.
2350 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2351 boolean_t bookmark_ok, char *name)
2353 char pname[ZFS_MAX_DATASET_NAME_LEN];
2354 guid_to_name_data_t gtnd;
2357 gtnd.bookmark_ok = bookmark_ok;
2362 * Search progressively larger portions of the hierarchy, starting
2363 * with the filesystem specified by 'parent'. This will
2364 * select the "most local" version of the origin snapshot in the case
2365 * that there are multiple matching snapshots in the system.
2367 (void) strlcpy(pname, parent, sizeof (pname));
2368 char *cp = strrchr(pname, '@');
2370 cp = strchr(pname, '\0');
2371 for (; cp != NULL; cp = strrchr(pname, '/')) {
2372 /* Chop off the last component and open the parent */
2374 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2378 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
2380 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2381 if (err != EEXIST && bookmark_ok)
2382 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2388 * Remember the last portion of the dataset so we skip it next
2389 * time through (as we've already searched that portion of the
2392 gtnd.skip = strrchr(pname, '/') + 1;
2399 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2400 * guid1 is after guid2.
2403 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2404 uint64_t guid1, uint64_t guid2)
2407 char *fsname, *snapname;
2408 char buf[ZFS_MAX_DATASET_NAME_LEN];
2410 zfs_handle_t *guid1hdl, *guid2hdl;
2411 uint64_t create1, create2;
2418 nvfs = fsavl_find(avl, guid1, &snapname);
2419 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2420 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2421 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2422 if (guid1hdl == NULL)
2425 nvfs = fsavl_find(avl, guid2, &snapname);
2426 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2427 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2428 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2429 if (guid2hdl == NULL) {
2430 zfs_close(guid1hdl);
2434 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2435 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2437 if (create1 < create2)
2439 else if (create1 > create2)
2444 zfs_close(guid1hdl);
2445 zfs_close(guid2hdl);
2451 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2452 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2455 nvlist_t *local_nv, *deleted = NULL;
2456 avl_tree_t *local_avl;
2457 nvpair_t *fselem, *nextfselem;
2459 char newname[ZFS_MAX_DATASET_NAME_LEN];
2462 boolean_t needagain, progress, recursive;
2465 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2467 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2474 needagain = progress = B_FALSE;
2476 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2478 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2479 recursive, B_FALSE, &local_nv, &local_avl)) != 0)
2483 * Process deletes and renames
2485 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2486 fselem; fselem = nextfselem) {
2487 nvlist_t *nvfs, *snaps;
2488 nvlist_t *stream_nvfs = NULL;
2489 nvpair_t *snapelem, *nextsnapelem;
2490 uint64_t fromguid = 0;
2491 uint64_t originguid = 0;
2492 uint64_t stream_originguid = 0;
2493 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2494 char *fsname, *stream_fsname;
2496 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2498 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2499 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2500 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2501 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2502 &parent_fromsnap_guid));
2503 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2506 * First find the stream's fs, so we can check for
2507 * a different origin (due to "zfs promote")
2509 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2510 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2513 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2514 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2516 if (stream_nvfs != NULL)
2520 /* check for promote */
2521 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2522 &stream_originguid);
2523 if (stream_nvfs && originguid != stream_originguid) {
2524 switch (created_before(hdl, local_avl,
2525 stream_originguid, originguid)) {
2528 zfs_cmd_t zc = { 0 };
2529 nvlist_t *origin_nvfs;
2530 char *origin_fsname;
2533 (void) printf("promoting %s\n", fsname);
2535 origin_nvfs = fsavl_find(local_avl, originguid,
2537 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2538 "name", &origin_fsname));
2539 (void) strlcpy(zc.zc_value, origin_fsname,
2540 sizeof (zc.zc_value));
2541 (void) strlcpy(zc.zc_name, fsname,
2542 sizeof (zc.zc_name));
2543 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2551 fsavl_destroy(local_avl);
2552 nvlist_free(local_nv);
2556 * We had/have the wrong origin, therefore our
2557 * list of snapshots is wrong. Need to handle
2558 * them on the next pass.
2564 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2565 snapelem; snapelem = nextsnapelem) {
2567 char *stream_snapname;
2568 nvlist_t *found, *props;
2570 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2572 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2573 found = fsavl_find(stream_avl, thisguid,
2576 /* check for delete */
2577 if (found == NULL) {
2578 char name[ZFS_MAX_DATASET_NAME_LEN];
2583 (void) snprintf(name, sizeof (name), "%s@%s",
2584 fsname, nvpair_name(snapelem));
2586 error = recv_destroy(hdl, name,
2587 strlen(fsname)+1, newname, flags);
2592 sprintf(guidname, "%" PRIu64, thisguid);
2593 nvlist_add_boolean(deleted, guidname);
2597 stream_nvfs = found;
2599 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2600 &props) && 0 == nvlist_lookup_nvlist(props,
2601 stream_snapname, &props)) {
2602 zfs_cmd_t zc = { 0 };
2604 zc.zc_cookie = B_TRUE; /* received */
2605 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2606 "%s@%s", fsname, nvpair_name(snapelem));
2607 if (zcmd_write_src_nvlist(hdl, &zc,
2609 (void) zfs_ioctl(hdl,
2610 ZFS_IOC_SET_PROP, &zc);
2611 zcmd_free_nvlists(&zc);
2615 /* check for different snapname */
2616 if (strcmp(nvpair_name(snapelem),
2617 stream_snapname) != 0) {
2618 char name[ZFS_MAX_DATASET_NAME_LEN];
2619 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2621 (void) snprintf(name, sizeof (name), "%s@%s",
2622 fsname, nvpair_name(snapelem));
2623 (void) snprintf(tryname, sizeof (name), "%s@%s",
2624 fsname, stream_snapname);
2626 error = recv_rename(hdl, name, tryname,
2627 strlen(fsname)+1, newname, flags);
2634 if (strcmp(stream_snapname, fromsnap) == 0)
2635 fromguid = thisguid;
2638 /* check for delete */
2639 if (stream_nvfs == NULL) {
2643 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2649 sprintf(guidname, "%" PRIu64, parent_fromsnap_guid);
2650 nvlist_add_boolean(deleted, guidname);
2654 if (fromguid == 0) {
2655 if (flags->verbose) {
2656 (void) printf("local fs %s does not have "
2657 "fromsnap (%s in stream); must have "
2658 "been deleted locally; ignoring\n",
2664 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2665 "name", &stream_fsname));
2666 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2667 "parentfromsnap", &stream_parent_fromsnap_guid));
2669 s1 = strrchr(fsname, '/');
2670 s2 = strrchr(stream_fsname, '/');
2673 * Check if we're going to rename based on parent guid change
2674 * and the current parent guid was also deleted. If it was then
2675 * rename will fail and is likely unneeded, so avoid this and
2676 * force an early retry to determine the new
2677 * parent_fromsnap_guid.
2679 if (stream_parent_fromsnap_guid != 0 &&
2680 parent_fromsnap_guid != 0 &&
2681 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
2682 sprintf(guidname, "%" PRIu64, parent_fromsnap_guid);
2683 if (nvlist_exists(deleted, guidname)) {
2691 * Check for rename. If the exact receive path is specified, it
2692 * does not count as a rename, but we still need to check the
2693 * datasets beneath it.
2695 if ((stream_parent_fromsnap_guid != 0 &&
2696 parent_fromsnap_guid != 0 &&
2697 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2698 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2699 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2701 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2703 parent = fsavl_find(local_avl,
2704 stream_parent_fromsnap_guid, NULL);
2706 * NB: parent might not be found if we used the
2707 * tosnap for stream_parent_fromsnap_guid,
2708 * because the parent is a newly-created fs;
2709 * we'll be able to rename it after we recv the
2712 if (parent != NULL) {
2715 VERIFY(0 == nvlist_lookup_string(parent, "name",
2717 (void) snprintf(tryname, sizeof (tryname),
2718 "%s%s", pname, strrchr(stream_fsname, '/'));
2721 if (flags->verbose) {
2722 (void) printf("local fs %s new parent "
2723 "not found\n", fsname);
2729 error = recv_rename(hdl, fsname, tryname,
2730 strlen(tofs)+1, newname, flags);
2732 if (renamed != NULL && newname[0] != '\0') {
2733 VERIFY(0 == nvlist_add_boolean(renamed,
2745 fsavl_destroy(local_avl);
2746 nvlist_free(local_nv);
2747 nvlist_free(deleted);
2749 if (needagain && progress) {
2750 /* do another pass to fix up temporary names */
2752 (void) printf("another pass:\n");
2760 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2761 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2762 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2764 nvlist_t *stream_nv = NULL;
2765 avl_tree_t *stream_avl = NULL;
2766 char *fromsnap = NULL;
2767 char *sendsnap = NULL;
2769 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2770 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
2772 dmu_replay_record_t drre;
2774 boolean_t anyerr = B_FALSE;
2775 boolean_t softerr = B_FALSE;
2776 boolean_t recursive;
2778 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2781 assert(drr->drr_type == DRR_BEGIN);
2782 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2783 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2784 DMU_COMPOUNDSTREAM);
2787 * Read in the nvlist from the stream.
2789 if (drr->drr_payloadlen != 0) {
2790 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2791 &stream_nv, flags->byteswap, zc);
2793 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2798 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2801 if (recursive && strchr(destname, '@')) {
2802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2803 "cannot specify snapshot name for multi-snapshot stream"));
2804 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2809 * Read in the end record and verify checksum.
2811 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2812 flags->byteswap, NULL)))
2814 if (flags->byteswap) {
2815 drre.drr_type = BSWAP_32(drre.drr_type);
2816 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2817 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2818 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2819 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2820 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2821 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2822 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2823 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2825 if (drre.drr_type != DRR_END) {
2826 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2829 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2830 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2831 "incorrect header checksum"));
2832 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2836 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2838 if (drr->drr_payloadlen != 0) {
2839 nvlist_t *stream_fss;
2841 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2843 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2844 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2845 "couldn't allocate avl tree"));
2846 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2850 if (fromsnap != NULL && recursive) {
2851 nvlist_t *renamed = NULL;
2852 nvpair_t *pair = NULL;
2854 (void) strlcpy(tofs, destname, sizeof (tofs));
2855 if (flags->isprefix) {
2856 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2859 if (flags->istail) {
2860 cp = strrchr(drrb->drr_toname, '/');
2862 (void) strlcat(tofs, "/",
2866 i = (cp - drrb->drr_toname);
2869 i = strcspn(drrb->drr_toname, "/@");
2871 /* zfs_receive_one() will create_parents() */
2872 (void) strlcat(tofs, &drrb->drr_toname[i],
2874 *strchr(tofs, '@') = '\0';
2877 if (!flags->dryrun && !flags->nomount) {
2878 VERIFY(0 == nvlist_alloc(&renamed,
2879 NV_UNIQUE_NAME, 0));
2882 softerr = recv_incremental_replication(hdl, tofs, flags,
2883 stream_nv, stream_avl, renamed);
2885 /* Unmount renamed filesystems before receiving. */
2886 while ((pair = nvlist_next_nvpair(renamed,
2889 prop_changelist_t *clp = NULL;
2891 zhp = zfs_open(hdl, nvpair_name(pair),
2892 ZFS_TYPE_FILESYSTEM);
2894 clp = changelist_gather(zhp,
2895 ZFS_PROP_MOUNTPOINT, 0, 0);
2899 changelist_prefix(clp);
2900 changelist_free(clp);
2905 nvlist_free(renamed);
2910 * Get the fs specified by the first path in the stream (the top level
2911 * specified by 'zfs send') and pass it to each invocation of
2912 * zfs_receive_one().
2914 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2916 if ((cp = strchr(sendfs, '@')) != NULL) {
2919 * Find the "sendsnap", the final snapshot in a replication
2920 * stream. zfs_receive_one() handles certain errors
2921 * differently, depending on if the contained stream is the
2924 sendsnap = (cp + 1);
2927 /* Finally, receive each contained stream */
2930 * we should figure out if it has a recoverable
2931 * error, in which case do a recv_skip() and drive on.
2932 * Note, if we fail due to already having this guid,
2933 * zfs_receive_one() will take care of it (ie,
2934 * recv_skip() and return 0).
2936 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2937 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2938 action_handlep, sendsnap);
2939 if (error == ENODATA) {
2944 } while (error == 0);
2946 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
2948 * Now that we have the fs's they sent us, try the
2951 softerr = recv_incremental_replication(hdl, tofs, flags,
2952 stream_nv, stream_avl, NULL);
2956 fsavl_destroy(stream_avl);
2957 nvlist_free(stream_nv);
2966 trunc_prop_errs(int truncated)
2968 ASSERT(truncated != 0);
2971 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2972 "1 more property could not be set\n"));
2974 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2975 "%d more properties could not be set\n"), truncated);
2979 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2981 dmu_replay_record_t *drr;
2982 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
2985 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2986 "cannot receive:"));
2988 /* XXX would be great to use lseek if possible... */
2991 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2992 byteswap, NULL) == 0) {
2994 drr->drr_type = BSWAP_32(drr->drr_type);
2996 switch (drr->drr_type) {
2998 if (drr->drr_payloadlen != 0) {
2999 (void) recv_read(hdl, fd, buf,
3000 drr->drr_payloadlen, B_FALSE, NULL);
3010 drr->drr_u.drr_object.drr_bonuslen =
3011 BSWAP_32(drr->drr_u.drr_object.
3014 (void) recv_read(hdl, fd, buf,
3015 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
3021 drr->drr_u.drr_write.drr_logical_size =
3023 drr->drr_u.drr_write.drr_logical_size);
3024 drr->drr_u.drr_write.drr_compressed_size =
3026 drr->drr_u.drr_write.drr_compressed_size);
3028 uint64_t payload_size =
3029 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3030 (void) recv_read(hdl, fd, buf,
3031 payload_size, B_FALSE, NULL);
3035 drr->drr_u.drr_spill.drr_length =
3036 BSWAP_64(drr->drr_u.drr_spill.drr_length);
3038 (void) recv_read(hdl, fd, buf,
3039 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
3041 case DRR_WRITE_EMBEDDED:
3043 drr->drr_u.drr_write_embedded.drr_psize =
3044 BSWAP_32(drr->drr_u.drr_write_embedded.
3047 (void) recv_read(hdl, fd, buf,
3048 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3051 case DRR_WRITE_BYREF:
3052 case DRR_FREEOBJECTS:
3057 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3058 "invalid record type"));
3059 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3068 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3069 boolean_t resumable)
3071 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3073 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3074 "checksum mismatch or incomplete stream"));
3078 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3079 *strchr(target_fs, '@') = '\0';
3080 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3081 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3085 char token_buf[ZFS_MAXPROPLEN];
3086 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3087 token_buf, sizeof (token_buf),
3088 NULL, NULL, 0, B_TRUE);
3090 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3091 "checksum mismatch or incomplete stream.\n"
3092 "Partially received snapshot is saved.\n"
3093 "A resuming stream can be generated on the sending "
3094 "system by running:\n"
3102 * Restores a backup of tosnap from the file descriptor specified by infd.
3105 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3106 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3107 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3108 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3109 uint64_t *action_handlep, const char *finalsnap)
3111 zfs_cmd_t zc = { 0 };
3113 int ioctl_err, ioctl_errno, err;
3115 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3117 char prop_errbuf[1024];
3118 const char *chopprefix;
3119 boolean_t newfs = B_FALSE;
3120 boolean_t stream_wantsnewfs;
3121 uint64_t parent_snapguid = 0;
3122 prop_changelist_t *clp = NULL;
3123 nvlist_t *snapprops_nvlist = NULL;
3124 zprop_errflags_t prop_errflags;
3125 boolean_t recursive;
3126 char *snapname = NULL;
3128 begin_time = time(NULL);
3130 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3133 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3136 if (stream_avl != NULL) {
3137 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3142 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
3144 err = nvlist_lookup_nvlist(fs, "props", &props);
3146 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
3148 if (flags->canmountoff) {
3149 VERIFY(0 == nvlist_add_uint64(props,
3150 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3152 ret = zcmd_write_src_nvlist(hdl, &zc, props);
3156 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
3157 VERIFY(0 == nvlist_lookup_nvlist(props,
3158 snapname, &snapprops_nvlist));
3168 * Determine how much of the snapshot name stored in the stream
3169 * we are going to tack on to the name they specified on the
3170 * command line, and how much we are going to chop off.
3172 * If they specified a snapshot, chop the entire name stored in
3175 if (flags->istail) {
3177 * A filesystem was specified with -e. We want to tack on only
3178 * the tail of the sent snapshot path.
3180 if (strchr(tosnap, '@')) {
3181 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3182 "argument - snapshot not allowed with -e"));
3183 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3186 chopprefix = strrchr(sendfs, '/');
3188 if (chopprefix == NULL) {
3190 * The tail is the poolname, so we need to
3191 * prepend a path separator.
3193 int len = strlen(drrb->drr_toname);
3194 cp = malloc(len + 2);
3196 (void) strcpy(&cp[1], drrb->drr_toname);
3199 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3201 } else if (flags->isprefix) {
3203 * A filesystem was specified with -d. We want to tack on
3204 * everything but the first element of the sent snapshot path
3205 * (all but the pool name).
3207 if (strchr(tosnap, '@')) {
3208 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3209 "argument - snapshot not allowed with -d"));
3210 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3213 chopprefix = strchr(drrb->drr_toname, '/');
3214 if (chopprefix == NULL)
3215 chopprefix = strchr(drrb->drr_toname, '@');
3216 } else if (strchr(tosnap, '@') == NULL) {
3218 * If a filesystem was specified without -d or -e, we want to
3219 * tack on everything after the fs specified by 'zfs send'.
3221 chopprefix = drrb->drr_toname + strlen(sendfs);
3223 /* A snapshot was specified as an exact path (no -d or -e). */
3225 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3226 "cannot specify snapshot name for multi-snapshot "
3228 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3230 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3233 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3234 ASSERT(chopprefix > drrb->drr_toname);
3235 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
3236 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3237 chopprefix[0] == '\0');
3240 * Determine name of destination snapshot, store in zc_value.
3242 (void) strcpy(zc.zc_value, tosnap);
3243 (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
3245 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
3246 zfs_ioctl_version = get_zfs_ioctl_version();
3248 * For forward compatibility hide tosnap in zc_value
3250 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
3251 (void) strcpy(zc.zc_value + strlen(zc.zc_value) + 1, tosnap);
3254 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
3255 zcmd_free_nvlists(&zc);
3256 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3260 * Determine the name of the origin snapshot, store in zc_string.
3263 (void) strncpy(zc.zc_string, originsnap, sizeof (zc.zc_string));
3265 (void) printf("using provided clone origin %s\n",
3267 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3268 if (guid_to_name(hdl, zc.zc_value,
3269 drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3270 zcmd_free_nvlists(&zc);
3271 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3272 "local origin for clone %s does not exist"),
3274 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3277 (void) printf("found clone origin %s\n", zc.zc_string);
3280 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3281 DMU_BACKUP_FEATURE_RESUMING;
3282 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
3283 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3285 if (stream_wantsnewfs) {
3287 * if the parent fs does not exist, look for it based on
3288 * the parent snap GUID
3290 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3291 "cannot receive new filesystem stream"));
3293 (void) strcpy(zc.zc_name, zc.zc_value);
3294 cp = strrchr(zc.zc_name, '/');
3298 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3299 char suffix[ZFS_MAX_DATASET_NAME_LEN];
3300 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
3301 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
3302 B_FALSE, zc.zc_value) == 0) {
3303 *strchr(zc.zc_value, '@') = '\0';
3304 (void) strcat(zc.zc_value, suffix);
3309 * If the fs does not exist, look for it based on the
3313 (void) snprintf(errbuf, sizeof (errbuf),
3314 dgettext(TEXT_DOMAIN,
3315 "cannot receive resume stream"));
3317 (void) snprintf(errbuf, sizeof (errbuf),
3318 dgettext(TEXT_DOMAIN,
3319 "cannot receive incremental stream"));
3322 (void) strcpy(zc.zc_name, zc.zc_value);
3323 *strchr(zc.zc_name, '@') = '\0';
3326 * If the exact receive path was specified and this is the
3327 * topmost path in the stream, then if the fs does not exist we
3328 * should look no further.
3330 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
3331 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
3332 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3333 char snap[ZFS_MAX_DATASET_NAME_LEN];
3334 (void) strcpy(snap, strchr(zc.zc_value, '@'));
3335 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
3336 B_FALSE, zc.zc_value) == 0) {
3337 *strchr(zc.zc_value, '@') = '\0';
3338 (void) strcat(zc.zc_value, snap);
3343 (void) strcpy(zc.zc_name, zc.zc_value);
3344 *strchr(zc.zc_name, '@') = '\0';
3346 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3350 * Destination fs exists. It must be one of these cases:
3351 * - an incremental send stream
3352 * - the stream specifies a new fs (full stream or clone)
3353 * and they want us to blow away the existing fs (and
3354 * have therefore specified -F and removed any snapshots)
3355 * - we are resuming a failed receive.
3357 if (stream_wantsnewfs) {
3358 if (!flags->force) {
3359 zcmd_free_nvlists(&zc);
3360 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3361 "destination '%s' exists\n"
3362 "must specify -F to overwrite it"),
3364 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3366 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3368 zcmd_free_nvlists(&zc);
3369 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3370 "destination has snapshots (eg. %s)\n"
3371 "must destroy them to overwrite it"),
3373 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3377 if ((zhp = zfs_open(hdl, zc.zc_name,
3378 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
3379 zcmd_free_nvlists(&zc);
3383 if (stream_wantsnewfs &&
3384 zhp->zfs_dmustats.dds_origin[0]) {
3385 zcmd_free_nvlists(&zc);
3387 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3388 "destination '%s' is a clone\n"
3389 "must destroy it to overwrite it"),
3391 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3394 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3395 stream_wantsnewfs) {
3396 /* We can't do online recv in this case */
3397 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3400 zcmd_free_nvlists(&zc);
3403 if (changelist_prefix(clp) != 0) {
3404 changelist_free(clp);
3406 zcmd_free_nvlists(&zc);
3412 * If we are resuming a newfs, set newfs here so that we will
3413 * mount it if the recv succeeds this time. We can tell
3414 * that it was a newfs on the first recv because the fs
3415 * itself will be inconsistent (if the fs existed when we
3416 * did the first recv, we would have received it into
3419 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
3425 * Destination filesystem does not exist. Therefore we better
3426 * be creating a new filesystem (either from a full backup, or
3427 * a clone). It would therefore be invalid if the user
3428 * specified only the pool name (i.e. if the destination name
3429 * contained no slash character).
3431 if (!stream_wantsnewfs ||
3432 (cp = strrchr(zc.zc_name, '/')) == NULL) {
3433 zcmd_free_nvlists(&zc);
3434 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3435 "destination '%s' does not exist"), zc.zc_name);
3436 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3440 * Trim off the final dataset component so we perform the
3441 * recvbackup ioctl to the filesystems's parent.
3445 if (flags->isprefix && !flags->istail && !flags->dryrun &&
3446 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
3447 zcmd_free_nvlists(&zc);
3448 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3454 zc.zc_begin_record = *drr_noswap;
3455 zc.zc_cookie = infd;
3456 zc.zc_guid = flags->force;
3457 zc.zc_resumable = flags->resumable;
3458 if (flags->verbose) {
3459 (void) printf("%s %s stream of %s into %s\n",
3460 flags->dryrun ? "would receive" : "receiving",
3461 drrb->drr_fromguid ? "incremental" : "full",
3462 drrb->drr_toname, zc.zc_value);
3463 (void) fflush(stdout);
3466 if (flags->dryrun) {
3467 zcmd_free_nvlists(&zc);
3468 return (recv_skip(hdl, infd, flags->byteswap));
3471 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
3472 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
3473 zc.zc_cleanup_fd = cleanup_fd;
3474 zc.zc_action_handle = *action_handlep;
3476 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
3477 ioctl_errno = errno;
3478 prop_errflags = (zprop_errflags_t)zc.zc_obj;
3481 nvlist_t *prop_errors;
3482 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
3483 zc.zc_nvlist_dst_size, &prop_errors, 0));
3485 nvpair_t *prop_err = NULL;
3487 while ((prop_err = nvlist_next_nvpair(prop_errors,
3488 prop_err)) != NULL) {
3493 prop = zfs_name_to_prop(nvpair_name(prop_err));
3494 (void) nvpair_value_int32(prop_err, &intval);
3495 if (strcmp(nvpair_name(prop_err),
3496 ZPROP_N_MORE_ERRORS) == 0) {
3497 trunc_prop_errs(intval);
3499 } else if (snapname == NULL || finalsnap == NULL ||
3500 strcmp(finalsnap, snapname) == 0 ||
3501 strcmp(nvpair_name(prop_err),
3502 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
3504 * Skip the special case of, for example,
3505 * "refquota", errors on intermediate
3506 * snapshots leading up to a final one.
3507 * That's why we have all of the checks above.
3509 * See zfs_ioctl.c's extract_delay_props() for
3510 * a list of props which can fail on
3511 * intermediate snapshots, but shouldn't
3512 * affect the overall receive.
3514 (void) snprintf(tbuf, sizeof (tbuf),
3515 dgettext(TEXT_DOMAIN,
3516 "cannot receive %s property on %s"),
3517 nvpair_name(prop_err), zc.zc_name);
3518 zfs_setprop_error(hdl, prop, intval, tbuf);
3521 nvlist_free(prop_errors);
3524 zc.zc_nvlist_dst = 0;
3525 zc.zc_nvlist_dst_size = 0;
3526 zcmd_free_nvlists(&zc);
3528 if (err == 0 && snapprops_nvlist) {
3529 zfs_cmd_t zc2 = { 0 };
3531 (void) strcpy(zc2.zc_name, zc.zc_value);
3532 zc2.zc_cookie = B_TRUE; /* received */
3533 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
3534 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
3535 zcmd_free_nvlists(&zc2);
3539 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3541 * It may be that this snapshot already exists,
3542 * in which case we want to consume & ignore it
3543 * rather than failing.
3545 avl_tree_t *local_avl;
3546 nvlist_t *local_nv, *fs;
3547 cp = strchr(zc.zc_value, '@');
3550 * XXX Do this faster by just iterating over snaps in
3551 * this fs. Also if zc_value does not exist, we will
3552 * get a strange "does not exist" error message.
3555 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3556 B_FALSE, &local_nv, &local_avl) == 0) {
3558 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
3559 fsavl_destroy(local_avl);
3560 nvlist_free(local_nv);
3563 if (flags->verbose) {
3564 (void) printf("snap %s already exists; "
3565 "ignoring\n", zc.zc_value);
3567 err = ioctl_err = recv_skip(hdl, infd,
3574 if (ioctl_err != 0) {
3575 switch (ioctl_errno) {
3577 cp = strchr(zc.zc_value, '@');
3579 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3580 "most recent snapshot of %s does not\n"
3581 "match incremental source"), zc.zc_value);
3582 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3587 "destination %s has been modified\n"
3588 "since most recent snapshot"), zc.zc_name);
3589 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3592 cp = strchr(zc.zc_value, '@');
3594 /* it's the containing fs that exists */
3597 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3598 "destination already exists"));
3599 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
3600 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
3605 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3608 recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
3609 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3613 "pool must be upgraded to receive this stream."));
3614 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3617 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3618 "destination %s space quota exceeded"), zc.zc_name);
3619 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3622 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3627 * Mount the target filesystem (if created). Also mount any
3628 * children of the target filesystem if we did a replication
3629 * receive (indicated by stream_avl being non-NULL).
3631 cp = strchr(zc.zc_value, '@');
3632 if (cp && (ioctl_err == 0 || !newfs)) {
3636 h = zfs_open(hdl, zc.zc_value,
3637 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3639 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3641 } else if (newfs || stream_avl) {
3643 * Track the first/top of hierarchy fs,
3644 * for mounting and sharing later.
3646 if (top_zfs && *top_zfs == NULL)
3647 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3655 if (!flags->nomount)
3656 err |= changelist_postfix(clp);
3657 changelist_free(clp);
3660 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3661 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3662 "failed to clear unreceived properties on %s"),
3664 (void) fprintf(stderr, "\n");
3666 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3667 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3668 "failed to restore original properties on %s"),
3670 (void) fprintf(stderr, "\n");
3673 if (err || ioctl_err)
3676 *action_handlep = zc.zc_action_handle;
3678 if (flags->verbose) {
3681 uint64_t bytes = zc.zc_cookie;
3682 time_t delta = time(NULL) - begin_time;
3685 zfs_nicenum(bytes, buf1, sizeof (buf1));
3686 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3688 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3696 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3697 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
3698 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3699 uint64_t *action_handlep, const char *finalsnap)
3702 dmu_replay_record_t drr, drr_noswap;
3703 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3705 zio_cksum_t zcksum = { 0 };
3706 uint64_t featureflags;
3709 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3712 if (flags->isprefix &&
3713 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3714 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3715 "(%s) does not exist"), tosnap);
3716 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3719 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
3720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
3721 "(%s) does not exist"), originsnap);
3722 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3725 /* read in the BEGIN record */
3726 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3730 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3731 /* It's the double end record at the end of a package */
3735 /* the kernel needs the non-byteswapped begin record */
3738 flags->byteswap = B_FALSE;
3739 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3741 * We computed the checksum in the wrong byteorder in
3742 * recv_read() above; do it again correctly.
3744 bzero(&zcksum, sizeof (zio_cksum_t));
3745 (void) fletcher_4_incremental_byteswap(&drr,
3746 sizeof (drr), &zcksum);
3747 flags->byteswap = B_TRUE;
3749 drr.drr_type = BSWAP_32(drr.drr_type);
3750 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3751 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3752 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3753 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3754 drrb->drr_type = BSWAP_32(drrb->drr_type);
3755 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3756 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3757 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3760 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3761 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3762 "stream (bad magic number)"));
3763 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3766 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3767 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3769 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3770 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3772 "stream has unsupported feature, feature flags = %lx"),
3774 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3777 if (strchr(drrb->drr_toname, '@') == NULL) {
3778 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3779 "stream (bad snapshot name)"));
3780 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3783 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3784 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
3785 if (sendfs == NULL) {
3787 * We were not called from zfs_receive_package(). Get
3788 * the fs specified by 'zfs send'.
3791 (void) strlcpy(nonpackage_sendfs,
3792 drr.drr_u.drr_begin.drr_toname,
3793 sizeof (nonpackage_sendfs));
3794 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3796 sendfs = nonpackage_sendfs;
3797 VERIFY(finalsnap == NULL);
3799 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3800 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
3801 cleanup_fd, action_handlep, finalsnap));
3803 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3804 DMU_COMPOUNDSTREAM);
3805 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
3806 &zcksum, top_zfs, cleanup_fd, action_handlep));
3811 * Restores a backup of tosnap from the file descriptor specified by infd.
3812 * Return 0 on total success, -2 if some things couldn't be
3813 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3814 * (-1 will override -2, if -1 and the resumable flag was specified the
3815 * transfer can be resumed if the sending side supports it).
3818 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
3819 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
3821 char *top_zfs = NULL;
3824 uint64_t action_handle = 0;
3825 char *originsnap = NULL;
3827 err = nvlist_lookup_string(props, "origin", &originsnap);
3828 if (err && err != ENOENT)
3832 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3833 VERIFY(cleanup_fd >= 0);
3835 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
3836 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL);
3838 VERIFY(0 == close(cleanup_fd));
3840 if (err == 0 && !flags->nomount && top_zfs) {
3842 prop_changelist_t *clp;
3844 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3846 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3847 CL_GATHER_MOUNT_ALWAYS, 0);
3850 /* mount and share received datasets */
3851 err = changelist_postfix(clp);
3852 changelist_free(clp);
3855 if (zhp == NULL || clp == NULL || err)