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;
90 typedef struct dataref {
96 typedef struct dedup_entry {
97 struct dedup_entry *dde_next;
98 zio_cksum_t dde_chksum;
103 #define MAX_DDT_PHYSMEM_PERCENT 20
104 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
106 typedef struct dedup_table {
107 dedup_entry_t **dedup_hash_array;
108 umem_cache_t *ddecache;
109 uint64_t max_ddt_size; /* max dedup table size in bytes */
110 uint64_t cur_ddt_size; /* current dedup table size in bytes */
117 high_order_bit(uint64_t n)
121 for (count = 0; n != 0; count++)
127 ssread(void *buf, size_t len, FILE *stream)
131 if ((outlen = fread(buf, len, 1, stream)) == 0)
138 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
139 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
143 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
144 if (ddt->ddt_full == B_FALSE) {
145 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
146 "Dedup table full. Deduplication will continue "
147 "with existing table entries"));
148 ddt->ddt_full = B_TRUE;
153 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
155 assert(*ddepp == NULL);
156 dde->dde_next = NULL;
157 dde->dde_chksum = *cs;
158 dde->dde_prop = prop;
161 ddt->cur_ddt_size += sizeof (dedup_entry_t);
167 * Using the specified dedup table, do a lookup for an entry with
168 * the checksum cs. If found, return the block's reference info
169 * in *dr. Otherwise, insert a new entry in the dedup table, using
170 * the reference information specified by *dr.
172 * return value: true - entry was found
173 * false - entry was not found
176 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
177 uint64_t prop, dataref_t *dr)
180 dedup_entry_t **ddepp;
182 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
184 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
185 ddepp = &((*ddepp)->dde_next)) {
186 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
187 (*ddepp)->dde_prop == prop) {
188 *dr = (*ddepp)->dde_ref;
192 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
197 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
198 zio_cksum_t *zc, int outfd)
200 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
201 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
202 (void) fletcher_4_incremental_native(drr,
203 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
204 if (drr->drr_type != DRR_BEGIN) {
205 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
206 drr_checksum.drr_checksum));
207 drr->drr_u.drr_checksum.drr_checksum = *zc;
209 (void) fletcher_4_incremental_native(
210 &drr->drr_u.drr_checksum.drr_checksum, sizeof (zio_cksum_t), zc);
211 if (write(outfd, drr, sizeof (*drr)) == -1)
213 if (payload_len != 0) {
214 (void) fletcher_4_incremental_native(payload, payload_len, zc);
215 if (write(outfd, payload, payload_len) == -1)
222 * This function is started in a separate thread when the dedup option
223 * has been requested. The main send thread determines the list of
224 * snapshots to be included in the send stream and makes the ioctl calls
225 * for each one. But instead of having the ioctl send the output to the
226 * the output fd specified by the caller of zfs_send()), the
227 * ioctl is told to direct the output to a pipe, which is read by the
228 * alternate thread running THIS function. This function does the
230 * 1. building a dedup table (the DDT)
231 * 2. doing checksums on each data block and inserting a record in the DDT
232 * 3. looking for matching checksums, and
233 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
234 * a duplicate block is found.
235 * The output of this function then goes to the output fd requested
236 * by the caller of zfs_send().
241 dedup_arg_t *dda = arg;
242 char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
243 dmu_replay_record_t thedrr;
244 dmu_replay_record_t *drr = &thedrr;
248 zio_cksum_t stream_cksum;
249 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
253 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
254 SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
256 numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
259 * numbuckets must be a power of 2. Increase number to
260 * a power of 2 if necessary.
262 if (!ISP2(numbuckets))
263 numbuckets = 1 << high_order_bit(numbuckets);
265 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
266 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
267 NULL, NULL, NULL, NULL, NULL, 0);
268 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
269 ddt.numhashbits = high_order_bit(numbuckets) - 1;
270 ddt.ddt_full = B_FALSE;
272 outfd = dda->outputfd;
273 ofp = fdopen(dda->inputfd, "r");
274 while (ssread(drr, sizeof (*drr), ofp) != 0) {
277 * kernel filled in checksum, we are going to write same
278 * record, but need to regenerate checksum.
280 if (drr->drr_type != DRR_BEGIN) {
281 bzero(&drr->drr_u.drr_checksum.drr_checksum,
282 sizeof (drr->drr_u.drr_checksum.drr_checksum));
285 switch (drr->drr_type) {
288 struct drr_begin *drrb = &drr->drr_u.drr_begin;
291 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
293 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
295 /* set the DEDUP feature flag for this stream */
296 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
297 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
298 DMU_BACKUP_FEATURE_DEDUPPROPS);
299 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
301 if (drr->drr_payloadlen != 0) {
302 sz = drr->drr_payloadlen;
304 if (sz > SPA_MAXBLOCKSIZE) {
305 buf = zfs_realloc(dda->dedup_hdl, buf,
306 SPA_MAXBLOCKSIZE, sz);
308 (void) ssread(buf, sz, ofp);
312 if (dump_record(drr, buf, sz, &stream_cksum,
320 struct drr_end *drre = &drr->drr_u.drr_end;
321 /* use the recalculated checksum */
322 drre->drr_checksum = stream_cksum;
323 if (dump_record(drr, NULL, 0, &stream_cksum,
331 struct drr_object *drro = &drr->drr_u.drr_object;
332 if (drro->drr_bonuslen > 0) {
334 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
337 if (dump_record(drr, buf,
338 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
339 &stream_cksum, outfd) != 0)
346 struct drr_spill *drrs = &drr->drr_u.drr_spill;
347 (void) ssread(buf, drrs->drr_length, ofp);
348 if (dump_record(drr, buf, drrs->drr_length,
349 &stream_cksum, outfd) != 0)
354 case DRR_FREEOBJECTS:
356 if (dump_record(drr, NULL, 0, &stream_cksum,
364 struct drr_write *drrw = &drr->drr_u.drr_write;
366 uint64_t payload_size;
368 payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
369 (void) ssread(buf, payload_size, ofp);
372 * Use the existing checksum if it's dedup-capable,
373 * else calculate a SHA256 checksum for it.
376 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
378 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
380 zio_cksum_t tmpsha256;
383 SHA256Update(&ctx, buf, payload_size);
384 SHA256Final(&tmpsha256, &ctx);
385 drrw->drr_key.ddk_cksum.zc_word[0] =
386 BE_64(tmpsha256.zc_word[0]);
387 drrw->drr_key.ddk_cksum.zc_word[1] =
388 BE_64(tmpsha256.zc_word[1]);
389 drrw->drr_key.ddk_cksum.zc_word[2] =
390 BE_64(tmpsha256.zc_word[2]);
391 drrw->drr_key.ddk_cksum.zc_word[3] =
392 BE_64(tmpsha256.zc_word[3]);
393 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
394 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
397 dataref.ref_guid = drrw->drr_toguid;
398 dataref.ref_object = drrw->drr_object;
399 dataref.ref_offset = drrw->drr_offset;
401 if (ddt_update(dda->dedup_hdl, &ddt,
402 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
404 dmu_replay_record_t wbr_drr = {0};
405 struct drr_write_byref *wbr_drrr =
406 &wbr_drr.drr_u.drr_write_byref;
408 /* block already present in stream */
409 wbr_drr.drr_type = DRR_WRITE_BYREF;
411 wbr_drrr->drr_object = drrw->drr_object;
412 wbr_drrr->drr_offset = drrw->drr_offset;
413 wbr_drrr->drr_length = drrw->drr_logical_size;
414 wbr_drrr->drr_toguid = drrw->drr_toguid;
415 wbr_drrr->drr_refguid = dataref.ref_guid;
416 wbr_drrr->drr_refobject =
418 wbr_drrr->drr_refoffset =
421 wbr_drrr->drr_checksumtype =
422 drrw->drr_checksumtype;
423 wbr_drrr->drr_checksumflags =
424 drrw->drr_checksumtype;
425 wbr_drrr->drr_key.ddk_cksum =
426 drrw->drr_key.ddk_cksum;
427 wbr_drrr->drr_key.ddk_prop =
428 drrw->drr_key.ddk_prop;
430 if (dump_record(&wbr_drr, NULL, 0,
431 &stream_cksum, outfd) != 0)
434 /* block not previously seen */
435 if (dump_record(drr, buf, payload_size,
436 &stream_cksum, outfd) != 0)
442 case DRR_WRITE_EMBEDDED:
444 struct drr_write_embedded *drrwe =
445 &drr->drr_u.drr_write_embedded;
447 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
448 if (dump_record(drr, buf,
449 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
450 &stream_cksum, outfd) != 0)
457 if (dump_record(drr, NULL, 0, &stream_cksum,
464 (void) fprintf(stderr, "INVALID record type 0x%x\n",
466 /* should never happen, so assert */
471 umem_cache_destroy(ddt.ddecache);
472 free(ddt.dedup_hash_array);
480 * Routines for dealing with the AVL tree of fs-nvlists
482 typedef struct fsavl_node {
490 fsavl_compare(const void *arg1, const void *arg2)
492 const fsavl_node_t *fn1 = arg1;
493 const fsavl_node_t *fn2 = arg2;
495 if (fn1->fn_guid > fn2->fn_guid)
497 else if (fn1->fn_guid < fn2->fn_guid)
504 * Given the GUID of a snapshot, find its containing filesystem and
508 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
510 fsavl_node_t fn_find;
513 fn_find.fn_guid = snapguid;
515 fn = avl_find(avl, &fn_find, NULL);
518 *snapname = fn->fn_snapname;
519 return (fn->fn_nvfs);
525 fsavl_destroy(avl_tree_t *avl)
534 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
541 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
544 fsavl_create(nvlist_t *fss)
547 nvpair_t *fselem = NULL;
549 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
552 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
553 offsetof(fsavl_node_t, fn_node));
555 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
556 nvlist_t *nvfs, *snaps;
557 nvpair_t *snapelem = NULL;
559 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
560 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
563 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
567 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
568 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
569 fsavl_destroy(fsavl);
573 fn->fn_snapname = nvpair_name(snapelem);
577 * Note: if there are multiple snaps with the
578 * same GUID, we ignore all but one.
580 if (avl_find(fsavl, fn, NULL) == NULL)
591 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
593 typedef struct send_data {
595 * assigned inside every recursive call,
596 * restored from *_save on return:
598 * guid of fromsnap snapshot in parent dataset
599 * txg of fromsnap snapshot in current dataset
600 * txg of tosnap snapshot in current dataset
603 uint64_t parent_fromsnap_guid;
604 uint64_t fromsnap_txg;
607 /* the nvlists get accumulated during depth-first traversal */
608 nvlist_t *parent_snaps;
612 /* send-receive configuration, does not change during traversal */
614 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 parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
796 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
797 uint64_t tosnap_txg_save = sd->tosnap_txg;
798 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
799 uint64_t guid = zhp->zfs_dmustats.dds_guid;
800 uint64_t fromsnap_txg, tosnap_txg;
803 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
804 if (fromsnap_txg != 0)
805 sd->fromsnap_txg = fromsnap_txg;
807 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
809 sd->tosnap_txg = tosnap_txg;
812 * on the send side, if the current dataset does not have tosnap,
813 * perform two additional checks:
815 * - skip sending the current dataset if it was created later than
817 * - return error if the current dataset was created earlier than
820 if (sd->tosnap != NULL && tosnap_txg == 0) {
821 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
823 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
824 "skipping dataset %s: snapshot %s does "
825 "not exist\n"), zhp->zfs_name, sd->tosnap);
828 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
829 "cannot send %s@%s%s: snapshot %s@%s does not "
830 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
831 dgettext(TEXT_DOMAIN, " recursively") : "",
832 zhp->zfs_name, sd->tosnap);
838 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
839 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
840 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
841 sd->parent_fromsnap_guid));
843 if (zhp->zfs_dmustats.dds_origin[0]) {
844 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
845 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
846 if (origin == NULL) {
850 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
851 origin->zfs_dmustats.dds_guid));
854 /* iterate over props */
855 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
856 send_iterate_prop(zhp, nv);
857 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
860 /* iterate over snaps, and set sd->parent_fromsnap_guid */
861 sd->parent_fromsnap_guid = 0;
862 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
863 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
864 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd);
865 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
866 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
867 nvlist_free(sd->parent_snaps);
868 nvlist_free(sd->snapprops);
870 /* add this fs to nvlist */
871 (void) snprintf(guidstring, sizeof (guidstring),
872 "0x%llx", (longlong_t)guid);
873 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
876 /* iterate over children */
878 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
881 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
882 sd->fromsnap_txg = fromsnap_txg_save;
883 sd->tosnap_txg = tosnap_txg_save;
890 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
891 const char *tosnap, boolean_t recursive, boolean_t verbose,
892 nvlist_t **nvlp, avl_tree_t **avlp)
895 send_data_t sd = { 0 };
898 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
900 return (EZFS_BADTYPE);
902 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
904 sd.fromsnap = fromsnap;
906 sd.recursive = recursive;
907 sd.verbose = verbose;
909 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
917 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
928 * Routines specific to "zfs send"
930 typedef struct send_dump_data {
931 /* these are all just the short snapname (the part after the @) */
932 const char *fromsnap;
934 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
935 uint64_t prevsnap_obj;
936 boolean_t seenfrom, seento, replicate, doall, fromorigin;
937 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
938 boolean_t large_block, compress;
944 snapfilter_cb_t *filter_cb;
947 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
953 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
954 boolean_t fromorigin, enum lzc_send_flags flags, uint64_t *sizep)
956 zfs_cmd_t zc = { 0 };
957 libzfs_handle_t *hdl = zhp->zfs_hdl;
959 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
960 assert(fromsnap_obj == 0 || !fromorigin);
962 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
963 zc.zc_obj = fromorigin;
964 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
965 zc.zc_fromobj = fromsnap_obj;
966 zc.zc_guid = 1; /* estimate flag */
969 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
971 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
972 "warning: cannot estimate space for '%s'"), zhp->zfs_name);
976 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
977 "not an earlier snapshot from the same fs"));
978 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
981 if (zfs_dataset_exists(hdl, zc.zc_name,
982 ZFS_TYPE_SNAPSHOT)) {
983 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
984 "incremental source (@%s) does not exist"),
987 return (zfs_error(hdl, EZFS_NOENT, errbuf));
999 zfs_error_aux(hdl, strerror(errno));
1000 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1003 return (zfs_standard_error(hdl, errno, errbuf));
1007 *sizep = zc.zc_objset_type;
1013 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1014 * NULL) to the file descriptor specified by outfd.
1017 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
1018 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
1021 zfs_cmd_t zc = { 0 };
1022 libzfs_handle_t *hdl = zhp->zfs_hdl;
1025 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1026 assert(fromsnap_obj == 0 || !fromorigin);
1028 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1029 zc.zc_cookie = outfd;
1030 zc.zc_obj = fromorigin;
1031 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1032 zc.zc_fromobj = fromsnap_obj;
1033 zc.zc_flags = flags;
1035 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
1036 if (fromsnap && fromsnap[0] != '\0') {
1037 VERIFY(0 == nvlist_add_string(thisdbg,
1038 "fromsnap", fromsnap));
1041 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1043 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1044 "warning: cannot send '%s'"), zhp->zfs_name);
1046 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
1048 VERIFY(0 == nvlist_add_nvlist(debugnv,
1049 zhp->zfs_name, thisdbg));
1051 nvlist_free(thisdbg);
1055 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1056 "not an earlier snapshot from the same fs"));
1057 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1060 if (zfs_dataset_exists(hdl, zc.zc_name,
1061 ZFS_TYPE_SNAPSHOT)) {
1062 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1063 "incremental source (@%s) does not exist"),
1066 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1081 zfs_error_aux(hdl, strerror(errno));
1082 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1085 return (zfs_standard_error(hdl, errno, errbuf));
1090 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1091 nvlist_free(thisdbg);
1097 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1099 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1102 * zfs_send() only sets snapholds for sends that need them,
1103 * e.g. replication and doall.
1105 if (sdd->snapholds == NULL)
1108 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1112 send_progress_thread(void *arg)
1114 progress_arg_t *pa = arg;
1115 zfs_cmd_t zc = { 0 };
1116 zfs_handle_t *zhp = pa->pa_zhp;
1117 libzfs_handle_t *hdl = zhp->zfs_hdl;
1118 unsigned long long bytes;
1123 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1125 if (!pa->pa_parsable)
1126 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1129 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1134 zc.zc_cookie = pa->pa_fd;
1135 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1136 return ((void *)-1);
1140 bytes = zc.zc_cookie;
1142 if (pa->pa_parsable) {
1143 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1144 tm->tm_hour, tm->tm_min, tm->tm_sec,
1145 bytes, zhp->zfs_name);
1147 zfs_nicenum(bytes, buf, sizeof (buf));
1148 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1149 tm->tm_hour, tm->tm_min, tm->tm_sec,
1150 buf, zhp->zfs_name);
1156 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1157 uint64_t size, boolean_t parsable)
1160 if (fromsnap != NULL) {
1161 (void) fprintf(fout, "incremental\t%s\t%s",
1164 (void) fprintf(fout, "full\t%s",
1168 if (fromsnap != NULL) {
1169 if (strchr(fromsnap, '@') == NULL &&
1170 strchr(fromsnap, '#') == NULL) {
1171 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1172 "send from @%s to %s"),
1175 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1176 "send from %s to %s"),
1180 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1188 (void) fprintf(fout, "\t%llu",
1192 zfs_nicenum(size, buf, sizeof (buf));
1193 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1194 " estimated size is %s"), buf);
1197 (void) fprintf(fout, "\n");
1201 dump_snapshot(zfs_handle_t *zhp, void *arg)
1203 send_dump_data_t *sdd = arg;
1204 progress_arg_t pa = { 0 };
1207 enum lzc_send_flags flags = 0;
1209 boolean_t isfromsnap, istosnap, fromorigin;
1210 boolean_t exclude = B_FALSE;
1211 FILE *fout = sdd->std_out ? stdout : stderr;
1214 thissnap = strchr(zhp->zfs_name, '@') + 1;
1215 isfromsnap = (sdd->fromsnap != NULL &&
1216 strcmp(sdd->fromsnap, thissnap) == 0);
1218 if (!sdd->seenfrom && isfromsnap) {
1219 gather_holds(zhp, sdd);
1220 sdd->seenfrom = B_TRUE;
1221 (void) strcpy(sdd->prevsnap, thissnap);
1222 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1227 if (sdd->seento || !sdd->seenfrom) {
1232 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1234 sdd->seento = B_TRUE;
1236 if (sdd->large_block)
1237 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1238 if (sdd->embed_data)
1239 flags |= LZC_SEND_FLAG_EMBED_DATA;
1241 flags |= LZC_SEND_FLAG_COMPRESS;
1243 if (!sdd->doall && !isfromsnap && !istosnap) {
1244 if (sdd->replicate) {
1246 nvlist_t *snapprops;
1248 * Filter out all intermediate snapshots except origin
1249 * snapshots needed to replicate clones.
1251 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1252 zhp->zfs_dmustats.dds_guid, &snapname);
1254 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1255 "snapprops", &snapprops));
1256 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1257 thissnap, &snapprops));
1258 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1265 * If a filter function exists, call it to determine whether
1266 * this snapshot will be sent.
1268 if (exclude || (sdd->filter_cb != NULL &&
1269 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1271 * This snapshot is filtered out. Don't send it, and don't
1272 * set prevsnap_obj, so it will be as if this snapshot didn't
1273 * exist, and the next accepted snapshot will be sent as
1274 * an incremental from the last accepted one, or as the
1275 * first (and full) snapshot in the case of a replication,
1276 * non-incremental send.
1282 gather_holds(zhp, sdd);
1283 fromorigin = sdd->prevsnap[0] == '\0' &&
1284 (sdd->fromorigin || sdd->replicate);
1288 (void) estimate_ioctl(zhp, sdd->prevsnap_obj,
1289 fromorigin, flags, &size);
1291 send_print_verbose(fout, zhp->zfs_name,
1292 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1293 size, sdd->parsable);
1299 * If progress reporting is requested, spawn a new thread to
1300 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1302 if (sdd->progress) {
1304 pa.pa_fd = sdd->outfd;
1305 pa.pa_parsable = sdd->parsable;
1307 if ((err = pthread_create(&tid, NULL,
1308 send_progress_thread, &pa)) != 0) {
1314 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1315 fromorigin, sdd->outfd, flags, sdd->debugnv);
1317 if (sdd->progress) {
1318 (void) pthread_cancel(tid);
1319 (void) pthread_join(tid, NULL);
1323 (void) strcpy(sdd->prevsnap, thissnap);
1324 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1330 dump_filesystem(zfs_handle_t *zhp, void *arg)
1333 send_dump_data_t *sdd = arg;
1334 boolean_t missingfrom = B_FALSE;
1335 zfs_cmd_t zc = { 0 };
1337 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1338 zhp->zfs_name, sdd->tosnap);
1339 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1340 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1341 "WARNING: could not send %s@%s: does not exist\n"),
1342 zhp->zfs_name, sdd->tosnap);
1347 if (sdd->replicate && sdd->fromsnap) {
1349 * If this fs does not have fromsnap, and we're doing
1350 * recursive, we need to send a full stream from the
1351 * beginning (or an incremental from the origin if this
1352 * is a clone). If we're doing non-recursive, then let
1353 * them get the error.
1355 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1356 zhp->zfs_name, sdd->fromsnap);
1357 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1358 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1359 missingfrom = B_TRUE;
1363 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1364 sdd->prevsnap_obj = 0;
1365 if (sdd->fromsnap == NULL || missingfrom)
1366 sdd->seenfrom = B_TRUE;
1368 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1369 if (!sdd->seenfrom) {
1370 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1371 "WARNING: could not send %s@%s:\n"
1372 "incremental source (%s@%s) does not exist\n"),
1373 zhp->zfs_name, sdd->tosnap,
1374 zhp->zfs_name, sdd->fromsnap);
1376 } else if (!sdd->seento) {
1377 if (sdd->fromsnap) {
1378 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1379 "WARNING: could not send %s@%s:\n"
1380 "incremental source (%s@%s) "
1381 "is not earlier than it\n"),
1382 zhp->zfs_name, sdd->tosnap,
1383 zhp->zfs_name, sdd->fromsnap);
1385 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1387 "could not send %s@%s: does not exist\n"),
1388 zhp->zfs_name, sdd->tosnap);
1397 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1399 send_dump_data_t *sdd = arg;
1401 boolean_t needagain, progress;
1403 if (!sdd->replicate)
1404 return (dump_filesystem(rzhp, sdd));
1406 /* Mark the clone origin snapshots. */
1407 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1408 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1410 uint64_t origin_guid = 0;
1412 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1413 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1414 if (origin_guid != 0) {
1416 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1417 origin_guid, &snapname);
1418 if (origin_nv != NULL) {
1419 nvlist_t *snapprops;
1420 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1421 "snapprops", &snapprops));
1422 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1423 snapname, &snapprops));
1424 VERIFY(0 == nvlist_add_boolean(
1425 snapprops, "is_clone_origin"));
1430 needagain = progress = B_FALSE;
1431 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1432 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1433 nvlist_t *fslist, *parent_nv;
1437 uint64_t origin_guid = 0;
1438 uint64_t parent_guid = 0;
1440 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1441 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1444 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1445 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1446 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1449 if (parent_guid != 0) {
1450 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1451 if (!nvlist_exists(parent_nv, "sent")) {
1452 /* parent has not been sent; skip this one */
1458 if (origin_guid != 0) {
1459 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1461 if (origin_nv != NULL &&
1462 !nvlist_exists(origin_nv, "sent")) {
1464 * origin has not been sent yet;
1472 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1475 err = dump_filesystem(zhp, sdd);
1476 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1487 /* clean out the sent flags in case we reuse this fss */
1488 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1489 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1492 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1493 (void) nvlist_remove_all(fslist, "sent");
1500 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1502 unsigned int version;
1504 unsigned long long checksum, packed_len;
1507 * Decode token header, which is:
1508 * <token version>-<checksum of payload>-<uncompressed payload length>
1509 * Note that the only supported token version is 1.
1511 nread = sscanf(token, "%u-%llx-%llx-",
1512 &version, &checksum, &packed_len);
1514 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1515 "resume token is corrupt (invalid format)"));
1519 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1521 "resume token is corrupt (invalid version %u)"),
1526 /* convert hexadecimal representation to binary */
1527 token = strrchr(token, '-') + 1;
1528 int len = strlen(token) / 2;
1529 unsigned char *compressed = zfs_alloc(hdl, len);
1530 for (int i = 0; i < len; i++) {
1531 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1535 "resume token is corrupt "
1536 "(payload is not hex-encoded)"));
1541 /* verify checksum */
1543 fletcher_4_native(compressed, len, NULL, &cksum);
1544 if (cksum.zc_word[0] != checksum) {
1546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1547 "resume token is corrupt (incorrect checksum)"));
1552 void *packed = zfs_alloc(hdl, packed_len);
1553 uLongf packed_len_long = packed_len;
1554 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1555 packed_len_long != packed_len) {
1558 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1559 "resume token is corrupt (decompression failed)"));
1565 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1569 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1570 "resume token is corrupt (nvlist_unpack failed)"));
1577 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1578 const char *resume_token)
1582 char *fromname = NULL;
1583 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1586 char name[ZFS_MAX_DATASET_NAME_LEN];
1587 enum lzc_send_flags lzc_flags = 0;
1589 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1590 "cannot resume send"));
1592 nvlist_t *resume_nvl =
1593 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1594 if (resume_nvl == NULL) {
1596 * zfs_error_aux has already been set by
1597 * zfs_send_resume_token_to_nvlist
1599 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1601 if (flags->verbose) {
1602 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1603 "resume token contents:\n"));
1604 nvlist_print(stderr, resume_nvl);
1607 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1608 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1609 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1610 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1611 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1612 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1613 "resume token is corrupt"));
1614 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1617 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1619 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1620 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1621 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1622 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1623 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1624 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1626 if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1627 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1629 "'%s' is no longer the same snapshot used in "
1630 "the initial send"), toname);
1632 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1633 "'%s' used in the initial send no longer exists"),
1636 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1638 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1641 "unable to access '%s'"), name);
1642 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1645 if (fromguid != 0) {
1646 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1647 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1648 "incremental source %#llx no longer exists"),
1649 (longlong_t)fromguid);
1650 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1655 if (flags->verbose) {
1657 error = lzc_send_space(zhp->zfs_name, fromname,
1660 size = MAX(0, (int64_t)(size - bytes));
1661 send_print_verbose(stderr, zhp->zfs_name, fromname,
1662 size, flags->parsable);
1665 if (!flags->dryrun) {
1666 progress_arg_t pa = { 0 };
1669 * If progress reporting is requested, spawn a new thread to
1670 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1672 if (flags->progress) {
1675 pa.pa_parsable = flags->parsable;
1677 error = pthread_create(&tid, NULL,
1678 send_progress_thread, &pa);
1685 error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1686 lzc_flags, resumeobj, resumeoff);
1688 if (flags->progress) {
1689 (void) pthread_cancel(tid);
1690 (void) pthread_join(tid, NULL);
1694 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1695 "warning: cannot send '%s'"), zhp->zfs_name);
1717 zfs_error_aux(hdl, strerror(errno));
1718 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1721 return (zfs_standard_error(hdl, errno, errbuf));
1732 * Generate a send stream for the dataset identified by the argument zhp.
1734 * The content of the send stream is the snapshot identified by
1735 * 'tosnap'. Incremental streams are requested in two ways:
1736 * - from the snapshot identified by "fromsnap" (if non-null) or
1737 * - from the origin of the dataset identified by zhp, which must
1738 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1741 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1742 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1743 * if "replicate" is set. If "doall" is set, dump all the intermediate
1744 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1745 * case too. If "props" is set, send properties.
1748 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1749 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1750 void *cb_arg, nvlist_t **debugnvp)
1753 send_dump_data_t sdd = { 0 };
1755 nvlist_t *fss = NULL;
1756 avl_tree_t *fsavl = NULL;
1757 static uint64_t holdseq;
1761 dedup_arg_t dda = { 0 };
1762 int featureflags = 0;
1765 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1766 "cannot send '%s'"), zhp->zfs_name);
1768 if (fromsnap && fromsnap[0] == '\0') {
1769 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1770 "zero-length incremental source"));
1771 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1774 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1776 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1777 if (version >= ZPL_VERSION_SA) {
1778 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1782 if (flags->dedup && !flags->dryrun) {
1783 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1784 DMU_BACKUP_FEATURE_DEDUPPROPS);
1785 if ((err = pipe(pipefd)) != 0) {
1786 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1787 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1790 dda.outputfd = outfd;
1791 dda.inputfd = pipefd[1];
1792 dda.dedup_hdl = zhp->zfs_hdl;
1793 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1794 (void) close(pipefd[0]);
1795 (void) close(pipefd[1]);
1796 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1797 return (zfs_error(zhp->zfs_hdl,
1798 EZFS_THREADCREATEFAILED, errbuf));
1802 if (flags->replicate || flags->doall || flags->props) {
1803 dmu_replay_record_t drr = { 0 };
1804 char *packbuf = NULL;
1806 zio_cksum_t zc = { 0 };
1808 if (flags->replicate || flags->props) {
1811 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1813 VERIFY(0 == nvlist_add_string(hdrnv,
1814 "fromsnap", fromsnap));
1816 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1817 if (!flags->replicate) {
1818 VERIFY(0 == nvlist_add_boolean(hdrnv,
1822 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1823 fromsnap, tosnap, flags->replicate, flags->verbose,
1827 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1828 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1838 if (!flags->dryrun) {
1839 /* write first begin record */
1840 drr.drr_type = DRR_BEGIN;
1841 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1842 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1843 drr_versioninfo, DMU_COMPOUNDSTREAM);
1844 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1845 drr_versioninfo, featureflags);
1846 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1847 sizeof (drr.drr_u.drr_begin.drr_toname),
1848 "%s@%s", zhp->zfs_name, tosnap);
1849 drr.drr_payloadlen = buflen;
1851 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1856 /* write end record */
1857 bzero(&drr, sizeof (drr));
1858 drr.drr_type = DRR_END;
1859 drr.drr_u.drr_end.drr_checksum = zc;
1860 err = write(outfd, &drr, sizeof (drr));
1870 /* dump each stream */
1871 sdd.fromsnap = fromsnap;
1872 sdd.tosnap = tosnap;
1874 sdd.outfd = pipefd[0];
1877 sdd.replicate = flags->replicate;
1878 sdd.doall = flags->doall;
1879 sdd.fromorigin = flags->fromorigin;
1882 sdd.verbose = flags->verbose;
1883 sdd.parsable = flags->parsable;
1884 sdd.progress = flags->progress;
1885 sdd.dryrun = flags->dryrun;
1886 sdd.large_block = flags->largeblock;
1887 sdd.embed_data = flags->embed_data;
1888 sdd.compress = flags->compress;
1889 sdd.filter_cb = filter_func;
1890 sdd.filter_cb_arg = cb_arg;
1892 sdd.debugnv = *debugnvp;
1893 if (sdd.verbose && sdd.dryrun)
1894 sdd.std_out = B_TRUE;
1895 fout = sdd.std_out ? stdout : stderr;
1898 * Some flags require that we place user holds on the datasets that are
1899 * being sent so they don't get destroyed during the send. We can skip
1900 * this step if the pool is imported read-only since the datasets cannot
1903 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1904 ZPOOL_PROP_READONLY, NULL) &&
1905 zfs_spa_version(zhp, &spa_version) == 0 &&
1906 spa_version >= SPA_VERSION_USERREFS &&
1907 (flags->doall || flags->replicate)) {
1909 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1910 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1911 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1912 if (sdd.cleanup_fd < 0) {
1916 sdd.snapholds = fnvlist_alloc();
1918 sdd.cleanup_fd = -1;
1919 sdd.snapholds = NULL;
1921 if (flags->verbose || sdd.snapholds != NULL) {
1923 * Do a verbose no-op dry run to get all the verbose output
1924 * or to gather snapshot hold's before generating any data,
1925 * then do a non-verbose real run to generate the streams.
1927 sdd.dryrun = B_TRUE;
1928 err = dump_filesystems(zhp, &sdd);
1933 if (flags->verbose) {
1934 if (flags->parsable) {
1935 (void) fprintf(fout, "size\t%llu\n",
1936 (longlong_t)sdd.size);
1939 zfs_nicenum(sdd.size, buf, sizeof (buf));
1940 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1941 "total estimated size is %s\n"), buf);
1945 /* Ensure no snaps found is treated as an error. */
1951 /* Skip the second run if dryrun was requested. */
1955 if (sdd.snapholds != NULL) {
1956 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
1960 fnvlist_free(sdd.snapholds);
1961 sdd.snapholds = NULL;
1964 sdd.dryrun = B_FALSE;
1965 sdd.verbose = B_FALSE;
1968 err = dump_filesystems(zhp, &sdd);
1969 fsavl_destroy(fsavl);
1972 /* Ensure no snaps found is treated as an error. */
1973 if (err == 0 && !sdd.seento)
1978 (void) pthread_cancel(tid);
1979 (void) close(pipefd[0]);
1980 (void) pthread_join(tid, NULL);
1983 if (sdd.cleanup_fd != -1) {
1984 VERIFY(0 == close(sdd.cleanup_fd));
1985 sdd.cleanup_fd = -1;
1988 if (!flags->dryrun && (flags->replicate || flags->doall ||
1991 * write final end record. NB: want to do this even if
1992 * there was some error, because it might not be totally
1995 dmu_replay_record_t drr = { 0 };
1996 drr.drr_type = DRR_END;
1997 if (write(outfd, &drr, sizeof (drr)) == -1) {
1998 return (zfs_standard_error(zhp->zfs_hdl,
2003 return (err || sdd.err);
2006 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2008 fsavl_destroy(fsavl);
2010 fnvlist_free(sdd.snapholds);
2012 if (sdd.cleanup_fd != -1)
2013 VERIFY(0 == close(sdd.cleanup_fd));
2015 (void) pthread_cancel(tid);
2016 (void) close(pipefd[0]);
2017 (void) pthread_join(tid, NULL);
2023 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
2024 enum lzc_send_flags flags)
2027 libzfs_handle_t *hdl = zhp->zfs_hdl;
2030 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2031 "warning: cannot send '%s'"), zhp->zfs_name);
2033 err = lzc_send(zhp->zfs_name, from, fd, flags);
2037 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2038 "not an earlier snapshot from the same fs"));
2039 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2043 if (lzc_exists(zhp->zfs_name)) {
2044 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2045 "incremental source (%s) does not exist"),
2048 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2051 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2052 "target is busy; if a filesystem, "
2053 "it must not be mounted"));
2054 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2069 zfs_error_aux(hdl, strerror(errno));
2070 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2073 return (zfs_standard_error(hdl, errno, errbuf));
2080 * Routines specific to "zfs recv"
2084 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2085 boolean_t byteswap, zio_cksum_t *zc)
2091 assert(ilen <= SPA_MAXBLOCKSIZE);
2094 rv = read(fd, cp, len);
2099 if (rv < 0 || len != 0) {
2100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2101 "failed to read from stream"));
2102 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2103 "cannot receive")));
2108 (void) fletcher_4_incremental_byteswap(buf, ilen, zc);
2110 (void) fletcher_4_incremental_native(buf, ilen, zc);
2116 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2117 boolean_t byteswap, zio_cksum_t *zc)
2122 buf = zfs_alloc(hdl, len);
2126 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2132 err = nvlist_unpack(buf, len, nvp, 0);
2135 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2136 "stream (malformed nvlist)"));
2143 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2144 int baselen, char *newname, recvflags_t *flags)
2147 zfs_cmd_t zc = { 0 };
2149 prop_changelist_t *clp;
2152 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2155 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2156 flags->force ? MS_FORCE : 0);
2160 err = changelist_prefix(clp);
2164 zc.zc_objset_type = DMU_OST_ZFS;
2165 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2168 (void) strcpy(newname, tryname);
2170 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
2172 if (flags->verbose) {
2173 (void) printf("attempting rename %s to %s\n",
2174 zc.zc_name, zc.zc_value);
2176 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2178 changelist_rename(clp, name, tryname);
2183 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2186 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2187 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2188 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
2190 if (flags->verbose) {
2191 (void) printf("failed - trying rename %s to %s\n",
2192 zc.zc_name, zc.zc_value);
2194 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2196 changelist_rename(clp, name, newname);
2197 if (err && flags->verbose) {
2198 (void) printf("failed (%u) - "
2199 "will try again on next pass\n", errno);
2202 } else if (flags->verbose) {
2204 (void) printf("success\n");
2206 (void) printf("failed (%u)\n", errno);
2209 (void) changelist_postfix(clp);
2210 changelist_free(clp);
2216 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2217 char *newname, recvflags_t *flags)
2219 zfs_cmd_t zc = { 0 };
2221 prop_changelist_t *clp;
2223 boolean_t defer = B_FALSE;
2226 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2229 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2230 flags->force ? MS_FORCE : 0);
2231 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2232 zfs_spa_version(zhp, &spa_version) == 0 &&
2233 spa_version >= SPA_VERSION_USERREFS)
2238 err = changelist_prefix(clp);
2242 zc.zc_objset_type = DMU_OST_ZFS;
2243 zc.zc_defer_destroy = defer;
2244 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2247 (void) printf("attempting destroy %s\n", zc.zc_name);
2248 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
2251 (void) printf("success\n");
2252 changelist_remove(clp, zc.zc_name);
2255 (void) changelist_postfix(clp);
2256 changelist_free(clp);
2259 * Deferred destroy might destroy the snapshot or only mark it to be
2260 * destroyed later, and it returns success in either case.
2262 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2263 ZFS_TYPE_SNAPSHOT))) {
2264 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2270 typedef struct guid_to_name_data {
2272 boolean_t bookmark_ok;
2275 } guid_to_name_data_t;
2278 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2280 guid_to_name_data_t *gtnd = arg;
2284 if (gtnd->skip != NULL &&
2285 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2286 strcmp(slash + 1, gtnd->skip) == 0) {
2291 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2292 (void) strcpy(gtnd->name, zhp->zfs_name);
2297 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2298 if (err != EEXIST && gtnd->bookmark_ok)
2299 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2305 * Attempt to find the local dataset associated with this guid. In the case of
2306 * multiple matches, we attempt to find the "best" match by searching
2307 * progressively larger portions of the hierarchy. This allows one to send a
2308 * tree of datasets individually and guarantee that we will find the source
2309 * guid within that hierarchy, even if there are multiple matches elsewhere.
2312 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2313 boolean_t bookmark_ok, char *name)
2315 char pname[ZFS_MAX_DATASET_NAME_LEN];
2316 guid_to_name_data_t gtnd;
2319 gtnd.bookmark_ok = bookmark_ok;
2324 * Search progressively larger portions of the hierarchy, starting
2325 * with the filesystem specified by 'parent'. This will
2326 * select the "most local" version of the origin snapshot in the case
2327 * that there are multiple matching snapshots in the system.
2329 (void) strlcpy(pname, parent, sizeof (pname));
2330 char *cp = strrchr(pname, '@');
2332 cp = strchr(pname, '\0');
2333 for (; cp != NULL; cp = strrchr(pname, '/')) {
2334 /* Chop off the last component and open the parent */
2336 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2340 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
2342 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2343 if (err != EEXIST && bookmark_ok)
2344 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2350 * Remember the last portion of the dataset so we skip it next
2351 * time through (as we've already searched that portion of the
2354 gtnd.skip = strrchr(pname, '/') + 1;
2361 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2362 * guid1 is after guid2.
2365 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2366 uint64_t guid1, uint64_t guid2)
2369 char *fsname, *snapname;
2370 char buf[ZFS_MAX_DATASET_NAME_LEN];
2372 zfs_handle_t *guid1hdl, *guid2hdl;
2373 uint64_t create1, create2;
2380 nvfs = fsavl_find(avl, guid1, &snapname);
2381 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2382 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2383 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2384 if (guid1hdl == NULL)
2387 nvfs = fsavl_find(avl, guid2, &snapname);
2388 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2389 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2390 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2391 if (guid2hdl == NULL) {
2392 zfs_close(guid1hdl);
2396 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2397 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2399 if (create1 < create2)
2401 else if (create1 > create2)
2406 zfs_close(guid1hdl);
2407 zfs_close(guid2hdl);
2413 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2414 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2417 nvlist_t *local_nv, *deleted = NULL;
2418 avl_tree_t *local_avl;
2419 nvpair_t *fselem, *nextfselem;
2421 char newname[ZFS_MAX_DATASET_NAME_LEN];
2424 boolean_t needagain, progress, recursive;
2427 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2429 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2436 needagain = progress = B_FALSE;
2438 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2440 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2441 recursive, B_FALSE, &local_nv, &local_avl)) != 0)
2445 * Process deletes and renames
2447 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2448 fselem; fselem = nextfselem) {
2449 nvlist_t *nvfs, *snaps;
2450 nvlist_t *stream_nvfs = NULL;
2451 nvpair_t *snapelem, *nextsnapelem;
2452 uint64_t fromguid = 0;
2453 uint64_t originguid = 0;
2454 uint64_t stream_originguid = 0;
2455 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2456 char *fsname, *stream_fsname;
2458 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2460 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2461 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2462 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2463 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2464 &parent_fromsnap_guid));
2465 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2468 * First find the stream's fs, so we can check for
2469 * a different origin (due to "zfs promote")
2471 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2472 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2475 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2476 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2478 if (stream_nvfs != NULL)
2482 /* check for promote */
2483 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2484 &stream_originguid);
2485 if (stream_nvfs && originguid != stream_originguid) {
2486 switch (created_before(hdl, local_avl,
2487 stream_originguid, originguid)) {
2490 zfs_cmd_t zc = { 0 };
2491 nvlist_t *origin_nvfs;
2492 char *origin_fsname;
2495 (void) printf("promoting %s\n", fsname);
2497 origin_nvfs = fsavl_find(local_avl, originguid,
2499 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2500 "name", &origin_fsname));
2501 (void) strlcpy(zc.zc_value, origin_fsname,
2502 sizeof (zc.zc_value));
2503 (void) strlcpy(zc.zc_name, fsname,
2504 sizeof (zc.zc_name));
2505 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2513 fsavl_destroy(local_avl);
2514 nvlist_free(local_nv);
2518 * We had/have the wrong origin, therefore our
2519 * list of snapshots is wrong. Need to handle
2520 * them on the next pass.
2526 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2527 snapelem; snapelem = nextsnapelem) {
2529 char *stream_snapname;
2530 nvlist_t *found, *props;
2532 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2534 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2535 found = fsavl_find(stream_avl, thisguid,
2538 /* check for delete */
2539 if (found == NULL) {
2540 char name[ZFS_MAX_DATASET_NAME_LEN];
2545 (void) snprintf(name, sizeof (name), "%s@%s",
2546 fsname, nvpair_name(snapelem));
2548 error = recv_destroy(hdl, name,
2549 strlen(fsname)+1, newname, flags);
2554 sprintf(guidname, "%lu", thisguid);
2555 nvlist_add_boolean(deleted, guidname);
2559 stream_nvfs = found;
2561 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2562 &props) && 0 == nvlist_lookup_nvlist(props,
2563 stream_snapname, &props)) {
2564 zfs_cmd_t zc = { 0 };
2566 zc.zc_cookie = B_TRUE; /* received */
2567 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2568 "%s@%s", fsname, nvpair_name(snapelem));
2569 if (zcmd_write_src_nvlist(hdl, &zc,
2571 (void) zfs_ioctl(hdl,
2572 ZFS_IOC_SET_PROP, &zc);
2573 zcmd_free_nvlists(&zc);
2577 /* check for different snapname */
2578 if (strcmp(nvpair_name(snapelem),
2579 stream_snapname) != 0) {
2580 char name[ZFS_MAX_DATASET_NAME_LEN];
2581 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2583 (void) snprintf(name, sizeof (name), "%s@%s",
2584 fsname, nvpair_name(snapelem));
2585 (void) snprintf(tryname, sizeof (name), "%s@%s",
2586 fsname, stream_snapname);
2588 error = recv_rename(hdl, name, tryname,
2589 strlen(fsname)+1, newname, flags);
2596 if (strcmp(stream_snapname, fromsnap) == 0)
2597 fromguid = thisguid;
2600 /* check for delete */
2601 if (stream_nvfs == NULL) {
2605 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2611 sprintf(guidname, "%lu", parent_fromsnap_guid);
2612 nvlist_add_boolean(deleted, guidname);
2616 if (fromguid == 0) {
2617 if (flags->verbose) {
2618 (void) printf("local fs %s does not have "
2619 "fromsnap (%s in stream); must have "
2620 "been deleted locally; ignoring\n",
2626 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2627 "name", &stream_fsname));
2628 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2629 "parentfromsnap", &stream_parent_fromsnap_guid));
2631 s1 = strrchr(fsname, '/');
2632 s2 = strrchr(stream_fsname, '/');
2635 * Check if we're going to rename based on parent guid change
2636 * and the current parent guid was also deleted. If it was then
2637 * rename will fail and is likely unneeded, so avoid this and
2638 * force an early retry to determine the new
2639 * parent_fromsnap_guid.
2641 if (stream_parent_fromsnap_guid != 0 &&
2642 parent_fromsnap_guid != 0 &&
2643 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
2644 sprintf(guidname, "%lu", parent_fromsnap_guid);
2645 if (nvlist_exists(deleted, guidname)) {
2653 * Check for rename. If the exact receive path is specified, it
2654 * does not count as a rename, but we still need to check the
2655 * datasets beneath it.
2657 if ((stream_parent_fromsnap_guid != 0 &&
2658 parent_fromsnap_guid != 0 &&
2659 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2660 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2661 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2663 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2665 parent = fsavl_find(local_avl,
2666 stream_parent_fromsnap_guid, NULL);
2668 * NB: parent might not be found if we used the
2669 * tosnap for stream_parent_fromsnap_guid,
2670 * because the parent is a newly-created fs;
2671 * we'll be able to rename it after we recv the
2674 if (parent != NULL) {
2677 VERIFY(0 == nvlist_lookup_string(parent, "name",
2679 (void) snprintf(tryname, sizeof (tryname),
2680 "%s%s", pname, strrchr(stream_fsname, '/'));
2683 if (flags->verbose) {
2684 (void) printf("local fs %s new parent "
2685 "not found\n", fsname);
2691 error = recv_rename(hdl, fsname, tryname,
2692 strlen(tofs)+1, newname, flags);
2694 if (renamed != NULL && newname[0] != '\0') {
2695 VERIFY(0 == nvlist_add_boolean(renamed,
2707 fsavl_destroy(local_avl);
2708 nvlist_free(local_nv);
2709 nvlist_free(deleted);
2711 if (needagain && progress) {
2712 /* do another pass to fix up temporary names */
2714 (void) printf("another pass:\n");
2722 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2723 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2724 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2726 nvlist_t *stream_nv = NULL;
2727 avl_tree_t *stream_avl = NULL;
2728 char *fromsnap = NULL;
2729 char *sendsnap = NULL;
2731 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2732 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
2734 dmu_replay_record_t drre;
2736 boolean_t anyerr = B_FALSE;
2737 boolean_t softerr = B_FALSE;
2738 boolean_t recursive;
2740 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2743 assert(drr->drr_type == DRR_BEGIN);
2744 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2745 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2746 DMU_COMPOUNDSTREAM);
2749 * Read in the nvlist from the stream.
2751 if (drr->drr_payloadlen != 0) {
2752 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2753 &stream_nv, flags->byteswap, zc);
2755 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2760 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2763 if (recursive && strchr(destname, '@')) {
2764 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2765 "cannot specify snapshot name for multi-snapshot stream"));
2766 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2771 * Read in the end record and verify checksum.
2773 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2774 flags->byteswap, NULL)))
2776 if (flags->byteswap) {
2777 drre.drr_type = BSWAP_32(drre.drr_type);
2778 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2779 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2780 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2781 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2782 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2783 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2784 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2785 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2787 if (drre.drr_type != DRR_END) {
2788 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2791 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2792 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2793 "incorrect header checksum"));
2794 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2798 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2800 if (drr->drr_payloadlen != 0) {
2801 nvlist_t *stream_fss;
2803 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2805 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2806 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2807 "couldn't allocate avl tree"));
2808 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2812 if (fromsnap != NULL && recursive) {
2813 nvlist_t *renamed = NULL;
2814 nvpair_t *pair = NULL;
2816 (void) strlcpy(tofs, destname, sizeof (tofs));
2817 if (flags->isprefix) {
2818 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2821 if (flags->istail) {
2822 cp = strrchr(drrb->drr_toname, '/');
2824 (void) strlcat(tofs, "/",
2828 i = (cp - drrb->drr_toname);
2831 i = strcspn(drrb->drr_toname, "/@");
2833 /* zfs_receive_one() will create_parents() */
2834 (void) strlcat(tofs, &drrb->drr_toname[i],
2836 *strchr(tofs, '@') = '\0';
2839 if (!flags->dryrun && !flags->nomount) {
2840 VERIFY(0 == nvlist_alloc(&renamed,
2841 NV_UNIQUE_NAME, 0));
2844 softerr = recv_incremental_replication(hdl, tofs, flags,
2845 stream_nv, stream_avl, renamed);
2847 /* Unmount renamed filesystems before receiving. */
2848 while ((pair = nvlist_next_nvpair(renamed,
2851 prop_changelist_t *clp = NULL;
2853 zhp = zfs_open(hdl, nvpair_name(pair),
2854 ZFS_TYPE_FILESYSTEM);
2856 clp = changelist_gather(zhp,
2857 ZFS_PROP_MOUNTPOINT, 0, 0);
2861 changelist_prefix(clp);
2862 changelist_free(clp);
2867 nvlist_free(renamed);
2872 * Get the fs specified by the first path in the stream (the top level
2873 * specified by 'zfs send') and pass it to each invocation of
2874 * zfs_receive_one().
2876 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2878 if ((cp = strchr(sendfs, '@')) != NULL) {
2881 * Find the "sendsnap", the final snapshot in a replication
2882 * stream. zfs_receive_one() handles certain errors
2883 * differently, depending on if the contained stream is the
2886 sendsnap = (cp + 1);
2889 /* Finally, receive each contained stream */
2892 * we should figure out if it has a recoverable
2893 * error, in which case do a recv_skip() and drive on.
2894 * Note, if we fail due to already having this guid,
2895 * zfs_receive_one() will take care of it (ie,
2896 * recv_skip() and return 0).
2898 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2899 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2900 action_handlep, sendsnap);
2901 if (error == ENODATA) {
2906 } while (error == 0);
2908 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
2910 * Now that we have the fs's they sent us, try the
2913 softerr = recv_incremental_replication(hdl, tofs, flags,
2914 stream_nv, stream_avl, NULL);
2918 fsavl_destroy(stream_avl);
2919 nvlist_free(stream_nv);
2928 trunc_prop_errs(int truncated)
2930 ASSERT(truncated != 0);
2933 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2934 "1 more property could not be set\n"));
2936 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2937 "%d more properties could not be set\n"), truncated);
2941 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2943 dmu_replay_record_t *drr;
2944 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
2947 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2948 "cannot receive:"));
2950 /* XXX would be great to use lseek if possible... */
2953 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2954 byteswap, NULL) == 0) {
2956 drr->drr_type = BSWAP_32(drr->drr_type);
2958 switch (drr->drr_type) {
2960 if (drr->drr_payloadlen != 0) {
2961 (void) recv_read(hdl, fd, buf,
2962 drr->drr_payloadlen, B_FALSE, NULL);
2972 drr->drr_u.drr_object.drr_bonuslen =
2973 BSWAP_32(drr->drr_u.drr_object.
2976 (void) recv_read(hdl, fd, buf,
2977 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2983 drr->drr_u.drr_write.drr_logical_size =
2985 drr->drr_u.drr_write.drr_logical_size);
2986 drr->drr_u.drr_write.drr_compressed_size =
2988 drr->drr_u.drr_write.drr_compressed_size);
2990 uint64_t payload_size =
2991 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
2992 (void) recv_read(hdl, fd, buf,
2993 payload_size, B_FALSE, NULL);
2997 drr->drr_u.drr_spill.drr_length =
2998 BSWAP_64(drr->drr_u.drr_spill.drr_length);
3000 (void) recv_read(hdl, fd, buf,
3001 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
3003 case DRR_WRITE_EMBEDDED:
3005 drr->drr_u.drr_write_embedded.drr_psize =
3006 BSWAP_32(drr->drr_u.drr_write_embedded.
3009 (void) recv_read(hdl, fd, buf,
3010 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3013 case DRR_WRITE_BYREF:
3014 case DRR_FREEOBJECTS:
3019 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3020 "invalid record type"));
3021 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3030 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3031 boolean_t resumable)
3033 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3035 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3036 "checksum mismatch or incomplete stream"));
3040 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3041 *strchr(target_fs, '@') = '\0';
3042 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3043 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3047 char token_buf[ZFS_MAXPROPLEN];
3048 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3049 token_buf, sizeof (token_buf),
3050 NULL, NULL, 0, B_TRUE);
3052 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3053 "checksum mismatch or incomplete stream.\n"
3054 "Partially received snapshot is saved.\n"
3055 "A resuming stream can be generated on the sending "
3056 "system by running:\n"
3064 * Restores a backup of tosnap from the file descriptor specified by infd.
3067 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3068 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3069 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3070 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3071 uint64_t *action_handlep, const char *finalsnap)
3073 zfs_cmd_t zc = { 0 };
3075 int ioctl_err, ioctl_errno, err;
3077 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3079 char prop_errbuf[1024];
3080 const char *chopprefix;
3081 boolean_t newfs = B_FALSE;
3082 boolean_t stream_wantsnewfs;
3083 uint64_t parent_snapguid = 0;
3084 prop_changelist_t *clp = NULL;
3085 nvlist_t *snapprops_nvlist = NULL;
3086 zprop_errflags_t prop_errflags;
3087 boolean_t recursive;
3088 char *snapname = NULL;
3090 begin_time = time(NULL);
3092 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3095 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3098 if (stream_avl != NULL) {
3099 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3104 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
3106 err = nvlist_lookup_nvlist(fs, "props", &props);
3108 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
3110 if (flags->canmountoff) {
3111 VERIFY(0 == nvlist_add_uint64(props,
3112 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3114 ret = zcmd_write_src_nvlist(hdl, &zc, props);
3118 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
3119 VERIFY(0 == nvlist_lookup_nvlist(props,
3120 snapname, &snapprops_nvlist));
3130 * Determine how much of the snapshot name stored in the stream
3131 * we are going to tack on to the name they specified on the
3132 * command line, and how much we are going to chop off.
3134 * If they specified a snapshot, chop the entire name stored in
3137 if (flags->istail) {
3139 * A filesystem was specified with -e. We want to tack on only
3140 * the tail of the sent snapshot path.
3142 if (strchr(tosnap, '@')) {
3143 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3144 "argument - snapshot not allowed with -e"));
3145 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3148 chopprefix = strrchr(sendfs, '/');
3150 if (chopprefix == NULL) {
3152 * The tail is the poolname, so we need to
3153 * prepend a path separator.
3155 int len = strlen(drrb->drr_toname);
3156 cp = malloc(len + 2);
3158 (void) strcpy(&cp[1], drrb->drr_toname);
3161 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3163 } else if (flags->isprefix) {
3165 * A filesystem was specified with -d. We want to tack on
3166 * everything but the first element of the sent snapshot path
3167 * (all but the pool name).
3169 if (strchr(tosnap, '@')) {
3170 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3171 "argument - snapshot not allowed with -d"));
3172 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3175 chopprefix = strchr(drrb->drr_toname, '/');
3176 if (chopprefix == NULL)
3177 chopprefix = strchr(drrb->drr_toname, '@');
3178 } else if (strchr(tosnap, '@') == NULL) {
3180 * If a filesystem was specified without -d or -e, we want to
3181 * tack on everything after the fs specified by 'zfs send'.
3183 chopprefix = drrb->drr_toname + strlen(sendfs);
3185 /* A snapshot was specified as an exact path (no -d or -e). */
3187 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3188 "cannot specify snapshot name for multi-snapshot "
3190 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3192 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3195 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3196 ASSERT(chopprefix > drrb->drr_toname);
3197 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
3198 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3199 chopprefix[0] == '\0');
3202 * Determine name of destination snapshot, store in zc_value.
3204 (void) strcpy(zc.zc_value, tosnap);
3205 (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
3207 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
3208 zfs_ioctl_version = get_zfs_ioctl_version();
3210 * For forward compatibility hide tosnap in zc_value
3212 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
3213 (void) strcpy(zc.zc_value + strlen(zc.zc_value) + 1, tosnap);
3216 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
3217 zcmd_free_nvlists(&zc);
3218 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3222 * Determine the name of the origin snapshot, store in zc_string.
3225 (void) strncpy(zc.zc_string, originsnap, sizeof (zc.zc_string));
3227 (void) printf("using provided clone origin %s\n",
3229 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3230 if (guid_to_name(hdl, zc.zc_value,
3231 drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3232 zcmd_free_nvlists(&zc);
3233 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3234 "local origin for clone %s does not exist"),
3236 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3239 (void) printf("found clone origin %s\n", zc.zc_string);
3242 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3243 DMU_BACKUP_FEATURE_RESUMING;
3244 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
3245 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3247 if (stream_wantsnewfs) {
3249 * if the parent fs does not exist, look for it based on
3250 * the parent snap GUID
3252 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3253 "cannot receive new filesystem stream"));
3255 (void) strcpy(zc.zc_name, zc.zc_value);
3256 cp = strrchr(zc.zc_name, '/');
3260 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3261 char suffix[ZFS_MAX_DATASET_NAME_LEN];
3262 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
3263 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
3264 B_FALSE, zc.zc_value) == 0) {
3265 *strchr(zc.zc_value, '@') = '\0';
3266 (void) strcat(zc.zc_value, suffix);
3271 * if the fs does not exist, look for it based on the
3274 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3275 "cannot receive incremental stream"));
3277 (void) strcpy(zc.zc_name, zc.zc_value);
3278 *strchr(zc.zc_name, '@') = '\0';
3281 * If the exact receive path was specified and this is the
3282 * topmost path in the stream, then if the fs does not exist we
3283 * should look no further.
3285 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
3286 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
3287 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3288 char snap[ZFS_MAX_DATASET_NAME_LEN];
3289 (void) strcpy(snap, strchr(zc.zc_value, '@'));
3290 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
3291 B_FALSE, zc.zc_value) == 0) {
3292 *strchr(zc.zc_value, '@') = '\0';
3293 (void) strcat(zc.zc_value, snap);
3298 (void) strcpy(zc.zc_name, zc.zc_value);
3299 *strchr(zc.zc_name, '@') = '\0';
3301 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3305 * Destination fs exists. It must be one of these cases:
3306 * - an incremental send stream
3307 * - the stream specifies a new fs (full stream or clone)
3308 * and they want us to blow away the existing fs (and
3309 * have therefore specified -F and removed any snapshots)
3310 * - we are resuming a failed receive.
3312 if (stream_wantsnewfs) {
3313 if (!flags->force) {
3314 zcmd_free_nvlists(&zc);
3315 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3316 "destination '%s' exists\n"
3317 "must specify -F to overwrite it"),
3319 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3321 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3323 zcmd_free_nvlists(&zc);
3324 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3325 "destination has snapshots (eg. %s)\n"
3326 "must destroy them to overwrite it"),
3328 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3332 if ((zhp = zfs_open(hdl, zc.zc_name,
3333 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
3334 zcmd_free_nvlists(&zc);
3338 if (stream_wantsnewfs &&
3339 zhp->zfs_dmustats.dds_origin[0]) {
3340 zcmd_free_nvlists(&zc);
3342 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3343 "destination '%s' is a clone\n"
3344 "must destroy it to overwrite it"),
3346 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3349 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3350 stream_wantsnewfs) {
3351 /* We can't do online recv in this case */
3352 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3355 zcmd_free_nvlists(&zc);
3358 if (changelist_prefix(clp) != 0) {
3359 changelist_free(clp);
3361 zcmd_free_nvlists(&zc);
3367 * If we are resuming a newfs, set newfs here so that we will
3368 * mount it if the recv succeeds this time. We can tell
3369 * that it was a newfs on the first recv because the fs
3370 * itself will be inconsistent (if the fs existed when we
3371 * did the first recv, we would have received it into
3374 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
3380 * Destination filesystem does not exist. Therefore we better
3381 * be creating a new filesystem (either from a full backup, or
3382 * a clone). It would therefore be invalid if the user
3383 * specified only the pool name (i.e. if the destination name
3384 * contained no slash character).
3386 if (!stream_wantsnewfs ||
3387 (cp = strrchr(zc.zc_name, '/')) == NULL) {
3388 zcmd_free_nvlists(&zc);
3389 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3390 "destination '%s' does not exist"), zc.zc_name);
3391 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3395 * Trim off the final dataset component so we perform the
3396 * recvbackup ioctl to the filesystems's parent.
3400 if (flags->isprefix && !flags->istail && !flags->dryrun &&
3401 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
3402 zcmd_free_nvlists(&zc);
3403 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3409 zc.zc_begin_record = *drr_noswap;
3410 zc.zc_cookie = infd;
3411 zc.zc_guid = flags->force;
3412 zc.zc_resumable = flags->resumable;
3413 if (flags->verbose) {
3414 (void) printf("%s %s stream of %s into %s\n",
3415 flags->dryrun ? "would receive" : "receiving",
3416 drrb->drr_fromguid ? "incremental" : "full",
3417 drrb->drr_toname, zc.zc_value);
3418 (void) fflush(stdout);
3421 if (flags->dryrun) {
3422 zcmd_free_nvlists(&zc);
3423 return (recv_skip(hdl, infd, flags->byteswap));
3426 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
3427 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
3428 zc.zc_cleanup_fd = cleanup_fd;
3429 zc.zc_action_handle = *action_handlep;
3431 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
3432 ioctl_errno = errno;
3433 prop_errflags = (zprop_errflags_t)zc.zc_obj;
3436 nvlist_t *prop_errors;
3437 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
3438 zc.zc_nvlist_dst_size, &prop_errors, 0));
3440 nvpair_t *prop_err = NULL;
3442 while ((prop_err = nvlist_next_nvpair(prop_errors,
3443 prop_err)) != NULL) {
3448 prop = zfs_name_to_prop(nvpair_name(prop_err));
3449 (void) nvpair_value_int32(prop_err, &intval);
3450 if (strcmp(nvpair_name(prop_err),
3451 ZPROP_N_MORE_ERRORS) == 0) {
3452 trunc_prop_errs(intval);
3454 } else if (snapname == NULL || finalsnap == NULL ||
3455 strcmp(finalsnap, snapname) == 0 ||
3456 strcmp(nvpair_name(prop_err),
3457 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
3459 * Skip the special case of, for example,
3460 * "refquota", errors on intermediate
3461 * snapshots leading up to a final one.
3462 * That's why we have all of the checks above.
3464 * See zfs_ioctl.c's extract_delay_props() for
3465 * a list of props which can fail on
3466 * intermediate snapshots, but shouldn't
3467 * affect the overall receive.
3469 (void) snprintf(tbuf, sizeof (tbuf),
3470 dgettext(TEXT_DOMAIN,
3471 "cannot receive %s property on %s"),
3472 nvpair_name(prop_err), zc.zc_name);
3473 zfs_setprop_error(hdl, prop, intval, tbuf);
3476 nvlist_free(prop_errors);
3479 zc.zc_nvlist_dst = 0;
3480 zc.zc_nvlist_dst_size = 0;
3481 zcmd_free_nvlists(&zc);
3483 if (err == 0 && snapprops_nvlist) {
3484 zfs_cmd_t zc2 = { 0 };
3486 (void) strcpy(zc2.zc_name, zc.zc_value);
3487 zc2.zc_cookie = B_TRUE; /* received */
3488 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
3489 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
3490 zcmd_free_nvlists(&zc2);
3494 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3496 * It may be that this snapshot already exists,
3497 * in which case we want to consume & ignore it
3498 * rather than failing.
3500 avl_tree_t *local_avl;
3501 nvlist_t *local_nv, *fs;
3502 cp = strchr(zc.zc_value, '@');
3505 * XXX Do this faster by just iterating over snaps in
3506 * this fs. Also if zc_value does not exist, we will
3507 * get a strange "does not exist" error message.
3510 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3511 B_FALSE, &local_nv, &local_avl) == 0) {
3513 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
3514 fsavl_destroy(local_avl);
3515 nvlist_free(local_nv);
3518 if (flags->verbose) {
3519 (void) printf("snap %s already exists; "
3520 "ignoring\n", zc.zc_value);
3522 err = ioctl_err = recv_skip(hdl, infd,
3529 if (ioctl_err != 0) {
3530 switch (ioctl_errno) {
3532 cp = strchr(zc.zc_value, '@');
3534 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3535 "most recent snapshot of %s does not\n"
3536 "match incremental source"), zc.zc_value);
3537 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3542 "destination %s has been modified\n"
3543 "since most recent snapshot"), zc.zc_name);
3544 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3547 cp = strchr(zc.zc_value, '@');
3549 /* it's the containing fs that exists */
3552 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3553 "destination already exists"));
3554 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
3555 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
3560 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3563 recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
3564 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3567 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3568 "pool must be upgraded to receive this stream."));
3569 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3572 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3573 "destination %s space quota exceeded"), zc.zc_name);
3574 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3577 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3582 * Mount the target filesystem (if created). Also mount any
3583 * children of the target filesystem if we did a replication
3584 * receive (indicated by stream_avl being non-NULL).
3586 cp = strchr(zc.zc_value, '@');
3587 if (cp && (ioctl_err == 0 || !newfs)) {
3591 h = zfs_open(hdl, zc.zc_value,
3592 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3594 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3596 } else if (newfs || stream_avl) {
3598 * Track the first/top of hierarchy fs,
3599 * for mounting and sharing later.
3601 if (top_zfs && *top_zfs == NULL)
3602 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3610 if (!flags->nomount)
3611 err |= changelist_postfix(clp);
3612 changelist_free(clp);
3615 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3616 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3617 "failed to clear unreceived properties on %s"),
3619 (void) fprintf(stderr, "\n");
3621 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3622 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3623 "failed to restore original properties on %s"),
3625 (void) fprintf(stderr, "\n");
3628 if (err || ioctl_err)
3631 *action_handlep = zc.zc_action_handle;
3633 if (flags->verbose) {
3636 uint64_t bytes = zc.zc_cookie;
3637 time_t delta = time(NULL) - begin_time;
3640 zfs_nicenum(bytes, buf1, sizeof (buf1));
3641 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3643 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3651 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3652 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
3653 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3654 uint64_t *action_handlep, const char *finalsnap)
3657 dmu_replay_record_t drr, drr_noswap;
3658 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3660 zio_cksum_t zcksum = { 0 };
3661 uint64_t featureflags;
3664 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3667 if (flags->isprefix &&
3668 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3670 "(%s) does not exist"), tosnap);
3671 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3674 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
3675 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
3676 "(%s) does not exist"), originsnap);
3677 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3680 /* read in the BEGIN record */
3681 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3685 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3686 /* It's the double end record at the end of a package */
3690 /* the kernel needs the non-byteswapped begin record */
3693 flags->byteswap = B_FALSE;
3694 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3696 * We computed the checksum in the wrong byteorder in
3697 * recv_read() above; do it again correctly.
3699 bzero(&zcksum, sizeof (zio_cksum_t));
3700 (void) fletcher_4_incremental_byteswap(&drr,
3701 sizeof (drr), &zcksum);
3702 flags->byteswap = B_TRUE;
3704 drr.drr_type = BSWAP_32(drr.drr_type);
3705 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3706 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3707 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3708 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3709 drrb->drr_type = BSWAP_32(drrb->drr_type);
3710 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3711 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3712 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3715 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3716 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3717 "stream (bad magic number)"));
3718 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3721 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3722 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3724 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3725 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3726 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3727 "stream has unsupported feature, feature flags = %lx"),
3729 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3732 if (strchr(drrb->drr_toname, '@') == NULL) {
3733 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3734 "stream (bad snapshot name)"));
3735 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3738 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3739 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
3740 if (sendfs == NULL) {
3742 * We were not called from zfs_receive_package(). Get
3743 * the fs specified by 'zfs send'.
3746 (void) strlcpy(nonpackage_sendfs,
3747 drr.drr_u.drr_begin.drr_toname,
3748 sizeof (nonpackage_sendfs));
3749 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3751 sendfs = nonpackage_sendfs;
3752 VERIFY(finalsnap == NULL);
3754 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3755 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
3756 cleanup_fd, action_handlep, finalsnap));
3758 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3759 DMU_COMPOUNDSTREAM);
3760 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
3761 &zcksum, top_zfs, cleanup_fd, action_handlep));
3766 * Restores a backup of tosnap from the file descriptor specified by infd.
3767 * Return 0 on total success, -2 if some things couldn't be
3768 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3769 * (-1 will override -2, if -1 and the resumable flag was specified the
3770 * transfer can be resumed if the sending side supports it).
3773 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
3774 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
3776 char *top_zfs = NULL;
3779 uint64_t action_handle = 0;
3780 char *originsnap = NULL;
3782 err = nvlist_lookup_string(props, "origin", &originsnap);
3783 if (err && err != ENOENT)
3787 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3788 VERIFY(cleanup_fd >= 0);
3790 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
3791 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL);
3793 VERIFY(0 == close(cleanup_fd));
3795 if (err == 0 && !flags->nomount && top_zfs) {
3797 prop_changelist_t *clp;
3799 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3801 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3802 CL_GATHER_MOUNT_ALWAYS, 0);
3805 /* mount and share received datasets */
3806 err = changelist_postfix(clp);
3807 changelist_free(clp);
3810 if (zhp == NULL || clp == NULL || err)