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 = (const fsavl_node_t *)arg1;
493 const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
495 return (AVL_CMP(fn1->fn_guid, fn2->fn_guid));
499 * Given the GUID of a snapshot, find its containing filesystem and
503 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
505 fsavl_node_t fn_find;
508 fn_find.fn_guid = snapguid;
510 fn = avl_find(avl, &fn_find, NULL);
513 *snapname = fn->fn_snapname;
514 return (fn->fn_nvfs);
520 fsavl_destroy(avl_tree_t *avl)
529 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
536 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
539 fsavl_create(nvlist_t *fss)
542 nvpair_t *fselem = NULL;
544 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
547 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
548 offsetof(fsavl_node_t, fn_node));
550 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
551 nvlist_t *nvfs, *snaps;
552 nvpair_t *snapelem = NULL;
554 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
555 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
558 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
562 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
563 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
564 fsavl_destroy(fsavl);
568 fn->fn_snapname = nvpair_name(snapelem);
572 * Note: if there are multiple snaps with the
573 * same GUID, we ignore all but one.
575 if (avl_find(fsavl, fn, NULL) == NULL)
586 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
588 typedef struct send_data {
590 * assigned inside every recursive call,
591 * restored from *_save on return:
593 * guid of fromsnap snapshot in parent dataset
594 * txg of fromsnap snapshot in current dataset
595 * txg of tosnap snapshot in current dataset
598 uint64_t parent_fromsnap_guid;
599 uint64_t fromsnap_txg;
602 /* the nvlists get accumulated during depth-first traversal */
603 nvlist_t *parent_snaps;
607 /* send-receive configuration, does not change during traversal */
609 const char *fromsnap;
615 * The header nvlist is of the following format:
618 * "fromsnap" -> string (if incremental)
622 * "name" -> string (full name; for debugging)
623 * "parentfromsnap" -> number (guid of fromsnap in parent)
625 * "props" -> { name -> value (only if set here) }
626 * "snaps" -> { name (lastname) -> number (guid) }
627 * "snapprops" -> { name (lastname) -> { name -> value } }
629 * "origin" -> number (guid) (if clone)
630 * "sent" -> boolean (not on-disk)
638 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
641 send_iterate_snap(zfs_handle_t *zhp, void *arg)
643 send_data_t *sd = arg;
644 uint64_t guid = zhp->zfs_dmustats.dds_guid;
645 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
649 snapname = strrchr(zhp->zfs_name, '@')+1;
651 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
653 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
654 "skipping snapshot %s because it was created "
655 "after the destination snapshot (%s)\n"),
656 zhp->zfs_name, sd->tosnap);
662 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
664 * NB: if there is no fromsnap here (it's a newly created fs in
665 * an incremental replication), we will substitute the tosnap.
667 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
668 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
669 strcmp(snapname, sd->tosnap) == 0)) {
670 sd->parent_fromsnap_guid = guid;
673 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
674 send_iterate_prop(zhp, nv);
675 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
683 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
685 nvpair_t *elem = NULL;
687 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
688 char *propname = nvpair_name(elem);
689 zfs_prop_t prop = zfs_name_to_prop(propname);
692 if (!zfs_prop_user(propname)) {
694 * Realistically, this should never happen. However,
695 * we want the ability to add DSL properties without
696 * needing to make incompatible version changes. We
697 * need to ignore unknown properties to allow older
698 * software to still send datasets containing these
699 * properties, with the unknown properties elided.
701 if (prop == ZPROP_INVAL)
704 if (zfs_prop_readonly(prop))
708 verify(nvpair_value_nvlist(elem, &propnv) == 0);
709 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
710 prop == ZFS_PROP_REFQUOTA ||
711 prop == ZFS_PROP_REFRESERVATION) {
714 verify(nvlist_lookup_uint64(propnv,
715 ZPROP_VALUE, &value) == 0);
716 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
719 * May have no source before SPA_VERSION_RECVD_PROPS,
720 * but is still modifiable.
722 if (nvlist_lookup_string(propnv,
723 ZPROP_SOURCE, &source) == 0) {
724 if ((strcmp(source, zhp->zfs_name) != 0) &&
726 ZPROP_SOURCE_VAL_RECVD) != 0))
731 if (nvlist_lookup_string(propnv,
732 ZPROP_SOURCE, &source) != 0)
734 if ((strcmp(source, zhp->zfs_name) != 0) &&
735 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
739 if (zfs_prop_user(propname) ||
740 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
742 verify(nvlist_lookup_string(propnv,
743 ZPROP_VALUE, &value) == 0);
744 VERIFY(0 == nvlist_add_string(nv, propname, value));
747 verify(nvlist_lookup_uint64(propnv,
748 ZPROP_VALUE, &value) == 0);
749 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
755 * returns snapshot creation txg
756 * and returns 0 if the snapshot does not exist
759 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
761 char name[ZFS_MAX_DATASET_NAME_LEN];
764 if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
767 (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
768 if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
769 zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
771 txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
780 * recursively generate nvlists describing datasets. See comment
781 * for the data structure send_data_t above for description of contents
785 send_iterate_fs(zfs_handle_t *zhp, void *arg)
787 send_data_t *sd = arg;
790 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
791 uint64_t fromsnap_txg_save = sd->fromsnap_txg;
792 uint64_t tosnap_txg_save = sd->tosnap_txg;
793 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
794 uint64_t guid = zhp->zfs_dmustats.dds_guid;
795 uint64_t fromsnap_txg, tosnap_txg;
798 fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
799 if (fromsnap_txg != 0)
800 sd->fromsnap_txg = fromsnap_txg;
802 tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
804 sd->tosnap_txg = tosnap_txg;
807 * on the send side, if the current dataset does not have tosnap,
808 * perform two additional checks:
810 * - skip sending the current dataset if it was created later than
812 * - return error if the current dataset was created earlier than
815 if (sd->tosnap != NULL && tosnap_txg == 0) {
816 if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
818 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
819 "skipping dataset %s: snapshot %s does "
820 "not exist\n"), zhp->zfs_name, sd->tosnap);
823 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
824 "cannot send %s@%s%s: snapshot %s@%s does not "
825 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
826 dgettext(TEXT_DOMAIN, " recursively") : "",
827 zhp->zfs_name, sd->tosnap);
833 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
834 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
835 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
836 sd->parent_fromsnap_guid));
838 if (zhp->zfs_dmustats.dds_origin[0]) {
839 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
840 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
841 if (origin == NULL) {
845 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
846 origin->zfs_dmustats.dds_guid));
849 /* iterate over props */
850 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
851 send_iterate_prop(zhp, nv);
852 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
855 /* iterate over snaps, and set sd->parent_fromsnap_guid */
856 sd->parent_fromsnap_guid = 0;
857 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
858 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
859 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd);
860 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
861 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
862 nvlist_free(sd->parent_snaps);
863 nvlist_free(sd->snapprops);
865 /* add this fs to nvlist */
866 (void) snprintf(guidstring, sizeof (guidstring),
867 "0x%llx", (longlong_t)guid);
868 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
871 /* iterate over children */
873 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
876 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
877 sd->fromsnap_txg = fromsnap_txg_save;
878 sd->tosnap_txg = tosnap_txg_save;
885 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
886 const char *tosnap, boolean_t recursive, boolean_t verbose,
887 nvlist_t **nvlp, avl_tree_t **avlp)
890 send_data_t sd = { 0 };
893 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
895 return (EZFS_BADTYPE);
897 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
899 sd.fromsnap = fromsnap;
901 sd.recursive = recursive;
902 sd.verbose = verbose;
904 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
912 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
923 * Routines specific to "zfs send"
925 typedef struct send_dump_data {
926 /* these are all just the short snapname (the part after the @) */
927 const char *fromsnap;
929 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
930 uint64_t prevsnap_obj;
931 boolean_t seenfrom, seento, replicate, doall, fromorigin;
932 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
933 boolean_t large_block, compress;
939 snapfilter_cb_t *filter_cb;
942 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
948 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
949 boolean_t fromorigin, enum lzc_send_flags flags, uint64_t *sizep)
951 zfs_cmd_t zc = { 0 };
952 libzfs_handle_t *hdl = zhp->zfs_hdl;
954 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
955 assert(fromsnap_obj == 0 || !fromorigin);
957 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
958 zc.zc_obj = fromorigin;
959 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
960 zc.zc_fromobj = fromsnap_obj;
961 zc.zc_guid = 1; /* estimate flag */
964 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
966 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
967 "warning: cannot estimate space for '%s'"), zhp->zfs_name);
971 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
972 "not an earlier snapshot from the same fs"));
973 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
976 if (zfs_dataset_exists(hdl, zc.zc_name,
977 ZFS_TYPE_SNAPSHOT)) {
978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
979 "incremental source (@%s) does not exist"),
982 return (zfs_error(hdl, EZFS_NOENT, errbuf));
994 zfs_error_aux(hdl, strerror(errno));
995 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
998 return (zfs_standard_error(hdl, errno, errbuf));
1002 *sizep = zc.zc_objset_type;
1008 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1009 * NULL) to the file descriptor specified by outfd.
1012 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
1013 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
1016 zfs_cmd_t zc = { 0 };
1017 libzfs_handle_t *hdl = zhp->zfs_hdl;
1020 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1021 assert(fromsnap_obj == 0 || !fromorigin);
1023 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1024 zc.zc_cookie = outfd;
1025 zc.zc_obj = fromorigin;
1026 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1027 zc.zc_fromobj = fromsnap_obj;
1028 zc.zc_flags = flags;
1030 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
1031 if (fromsnap && fromsnap[0] != '\0') {
1032 VERIFY(0 == nvlist_add_string(thisdbg,
1033 "fromsnap", fromsnap));
1036 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1038 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1039 "warning: cannot send '%s'"), zhp->zfs_name);
1041 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
1043 VERIFY(0 == nvlist_add_nvlist(debugnv,
1044 zhp->zfs_name, thisdbg));
1046 nvlist_free(thisdbg);
1050 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1051 "not an earlier snapshot from the same fs"));
1052 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1055 if (zfs_dataset_exists(hdl, zc.zc_name,
1056 ZFS_TYPE_SNAPSHOT)) {
1057 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1058 "incremental source (@%s) does not exist"),
1061 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1076 zfs_error_aux(hdl, strerror(errno));
1077 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1080 return (zfs_standard_error(hdl, errno, errbuf));
1085 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1086 nvlist_free(thisdbg);
1092 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1094 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1097 * zfs_send() only sets snapholds for sends that need them,
1098 * e.g. replication and doall.
1100 if (sdd->snapholds == NULL)
1103 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1107 send_progress_thread(void *arg)
1109 progress_arg_t *pa = arg;
1110 zfs_cmd_t zc = { 0 };
1111 zfs_handle_t *zhp = pa->pa_zhp;
1112 libzfs_handle_t *hdl = zhp->zfs_hdl;
1113 unsigned long long bytes;
1118 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1120 if (!pa->pa_parsable)
1121 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1124 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1129 zc.zc_cookie = pa->pa_fd;
1130 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1131 return ((void *)-1);
1135 bytes = zc.zc_cookie;
1137 if (pa->pa_parsable) {
1138 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1139 tm->tm_hour, tm->tm_min, tm->tm_sec,
1140 bytes, zhp->zfs_name);
1142 zfs_nicenum(bytes, buf, sizeof (buf));
1143 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1144 tm->tm_hour, tm->tm_min, tm->tm_sec,
1145 buf, zhp->zfs_name);
1151 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1152 uint64_t size, boolean_t parsable)
1155 if (fromsnap != NULL) {
1156 (void) fprintf(fout, "incremental\t%s\t%s",
1159 (void) fprintf(fout, "full\t%s",
1163 if (fromsnap != NULL) {
1164 if (strchr(fromsnap, '@') == NULL &&
1165 strchr(fromsnap, '#') == NULL) {
1166 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1167 "send from @%s to %s"),
1170 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1171 "send from %s to %s"),
1175 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1183 (void) fprintf(fout, "\t%llu",
1187 zfs_nicenum(size, buf, sizeof (buf));
1188 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1189 " estimated size is %s"), buf);
1192 (void) fprintf(fout, "\n");
1196 dump_snapshot(zfs_handle_t *zhp, void *arg)
1198 send_dump_data_t *sdd = arg;
1199 progress_arg_t pa = { 0 };
1202 enum lzc_send_flags flags = 0;
1204 boolean_t isfromsnap, istosnap, fromorigin;
1205 boolean_t exclude = B_FALSE;
1206 FILE *fout = sdd->std_out ? stdout : stderr;
1209 thissnap = strchr(zhp->zfs_name, '@') + 1;
1210 isfromsnap = (sdd->fromsnap != NULL &&
1211 strcmp(sdd->fromsnap, thissnap) == 0);
1213 if (!sdd->seenfrom && isfromsnap) {
1214 gather_holds(zhp, sdd);
1215 sdd->seenfrom = B_TRUE;
1216 (void) strcpy(sdd->prevsnap, thissnap);
1217 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1222 if (sdd->seento || !sdd->seenfrom) {
1227 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1229 sdd->seento = B_TRUE;
1231 if (sdd->large_block)
1232 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1233 if (sdd->embed_data)
1234 flags |= LZC_SEND_FLAG_EMBED_DATA;
1236 flags |= LZC_SEND_FLAG_COMPRESS;
1238 if (!sdd->doall && !isfromsnap && !istosnap) {
1239 if (sdd->replicate) {
1241 nvlist_t *snapprops;
1243 * Filter out all intermediate snapshots except origin
1244 * snapshots needed to replicate clones.
1246 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1247 zhp->zfs_dmustats.dds_guid, &snapname);
1249 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1250 "snapprops", &snapprops));
1251 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1252 thissnap, &snapprops));
1253 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1260 * If a filter function exists, call it to determine whether
1261 * this snapshot will be sent.
1263 if (exclude || (sdd->filter_cb != NULL &&
1264 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1266 * This snapshot is filtered out. Don't send it, and don't
1267 * set prevsnap_obj, so it will be as if this snapshot didn't
1268 * exist, and the next accepted snapshot will be sent as
1269 * an incremental from the last accepted one, or as the
1270 * first (and full) snapshot in the case of a replication,
1271 * non-incremental send.
1277 gather_holds(zhp, sdd);
1278 fromorigin = sdd->prevsnap[0] == '\0' &&
1279 (sdd->fromorigin || sdd->replicate);
1283 (void) estimate_ioctl(zhp, sdd->prevsnap_obj,
1284 fromorigin, flags, &size);
1286 send_print_verbose(fout, zhp->zfs_name,
1287 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1288 size, sdd->parsable);
1294 * If progress reporting is requested, spawn a new thread to
1295 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1297 if (sdd->progress) {
1299 pa.pa_fd = sdd->outfd;
1300 pa.pa_parsable = sdd->parsable;
1302 if ((err = pthread_create(&tid, NULL,
1303 send_progress_thread, &pa)) != 0) {
1309 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1310 fromorigin, sdd->outfd, flags, sdd->debugnv);
1312 if (sdd->progress) {
1313 (void) pthread_cancel(tid);
1314 (void) pthread_join(tid, NULL);
1318 (void) strcpy(sdd->prevsnap, thissnap);
1319 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1325 dump_filesystem(zfs_handle_t *zhp, void *arg)
1328 send_dump_data_t *sdd = arg;
1329 boolean_t missingfrom = B_FALSE;
1330 zfs_cmd_t zc = { 0 };
1332 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1333 zhp->zfs_name, sdd->tosnap);
1334 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1335 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1336 "WARNING: could not send %s@%s: does not exist\n"),
1337 zhp->zfs_name, sdd->tosnap);
1342 if (sdd->replicate && sdd->fromsnap) {
1344 * If this fs does not have fromsnap, and we're doing
1345 * recursive, we need to send a full stream from the
1346 * beginning (or an incremental from the origin if this
1347 * is a clone). If we're doing non-recursive, then let
1348 * them get the error.
1350 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1351 zhp->zfs_name, sdd->fromsnap);
1352 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1353 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1354 missingfrom = B_TRUE;
1358 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1359 sdd->prevsnap_obj = 0;
1360 if (sdd->fromsnap == NULL || missingfrom)
1361 sdd->seenfrom = B_TRUE;
1363 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1364 if (!sdd->seenfrom) {
1365 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1366 "WARNING: could not send %s@%s:\n"
1367 "incremental source (%s@%s) does not exist\n"),
1368 zhp->zfs_name, sdd->tosnap,
1369 zhp->zfs_name, sdd->fromsnap);
1371 } else if (!sdd->seento) {
1372 if (sdd->fromsnap) {
1373 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1374 "WARNING: could not send %s@%s:\n"
1375 "incremental source (%s@%s) "
1376 "is not earlier than it\n"),
1377 zhp->zfs_name, sdd->tosnap,
1378 zhp->zfs_name, sdd->fromsnap);
1380 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1382 "could not send %s@%s: does not exist\n"),
1383 zhp->zfs_name, sdd->tosnap);
1392 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1394 send_dump_data_t *sdd = arg;
1396 boolean_t needagain, progress;
1398 if (!sdd->replicate)
1399 return (dump_filesystem(rzhp, sdd));
1401 /* Mark the clone origin snapshots. */
1402 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1403 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1405 uint64_t origin_guid = 0;
1407 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1408 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1409 if (origin_guid != 0) {
1411 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1412 origin_guid, &snapname);
1413 if (origin_nv != NULL) {
1414 nvlist_t *snapprops;
1415 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1416 "snapprops", &snapprops));
1417 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1418 snapname, &snapprops));
1419 VERIFY(0 == nvlist_add_boolean(
1420 snapprops, "is_clone_origin"));
1425 needagain = progress = B_FALSE;
1426 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1427 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1428 nvlist_t *fslist, *parent_nv;
1432 uint64_t origin_guid = 0;
1433 uint64_t parent_guid = 0;
1435 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1436 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1439 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1440 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1441 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1444 if (parent_guid != 0) {
1445 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1446 if (!nvlist_exists(parent_nv, "sent")) {
1447 /* parent has not been sent; skip this one */
1453 if (origin_guid != 0) {
1454 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1456 if (origin_nv != NULL &&
1457 !nvlist_exists(origin_nv, "sent")) {
1459 * origin has not been sent yet;
1467 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1470 err = dump_filesystem(zhp, sdd);
1471 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1482 /* clean out the sent flags in case we reuse this fss */
1483 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1484 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1487 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1488 (void) nvlist_remove_all(fslist, "sent");
1495 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1497 unsigned int version;
1499 unsigned long long checksum, packed_len;
1502 * Decode token header, which is:
1503 * <token version>-<checksum of payload>-<uncompressed payload length>
1504 * Note that the only supported token version is 1.
1506 nread = sscanf(token, "%u-%llx-%llx-",
1507 &version, &checksum, &packed_len);
1509 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1510 "resume token is corrupt (invalid format)"));
1514 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1515 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1516 "resume token is corrupt (invalid version %u)"),
1521 /* convert hexadecimal representation to binary */
1522 token = strrchr(token, '-') + 1;
1523 int len = strlen(token) / 2;
1524 unsigned char *compressed = zfs_alloc(hdl, len);
1525 for (int i = 0; i < len; i++) {
1526 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1529 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1530 "resume token is corrupt "
1531 "(payload is not hex-encoded)"));
1536 /* verify checksum */
1538 fletcher_4_native(compressed, len, NULL, &cksum);
1539 if (cksum.zc_word[0] != checksum) {
1541 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1542 "resume token is corrupt (incorrect checksum)"));
1547 void *packed = zfs_alloc(hdl, packed_len);
1548 uLongf packed_len_long = packed_len;
1549 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1550 packed_len_long != packed_len) {
1553 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1554 "resume token is corrupt (decompression failed)"));
1560 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1564 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1565 "resume token is corrupt (nvlist_unpack failed)"));
1572 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1573 const char *resume_token)
1577 char *fromname = NULL;
1578 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1581 char name[ZFS_MAX_DATASET_NAME_LEN];
1582 enum lzc_send_flags lzc_flags = 0;
1583 FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr;
1585 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1586 "cannot resume send"));
1588 nvlist_t *resume_nvl =
1589 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1590 if (resume_nvl == NULL) {
1592 * zfs_error_aux has already been set by
1593 * zfs_send_resume_token_to_nvlist
1595 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1597 if (flags->verbose) {
1598 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1599 "resume token contents:\n"));
1600 nvlist_print(fout, resume_nvl);
1603 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1604 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1605 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1606 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1607 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1608 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1609 "resume token is corrupt"));
1610 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1613 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1615 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1616 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1617 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1618 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1619 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1620 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1622 if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1623 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1625 "'%s' is no longer the same snapshot used in "
1626 "the initial send"), toname);
1628 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1629 "'%s' used in the initial send no longer exists"),
1632 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1634 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1636 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1637 "unable to access '%s'"), name);
1638 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1641 if (fromguid != 0) {
1642 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1643 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1644 "incremental source %#llx no longer exists"),
1645 (longlong_t)fromguid);
1646 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1651 if (flags->verbose) {
1653 error = lzc_send_space(zhp->zfs_name, fromname,
1656 size = MAX(0, (int64_t)(size - bytes));
1657 send_print_verbose(fout, zhp->zfs_name, fromname,
1658 size, flags->parsable);
1661 if (!flags->dryrun) {
1662 progress_arg_t pa = { 0 };
1665 * If progress reporting is requested, spawn a new thread to
1666 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1668 if (flags->progress) {
1671 pa.pa_parsable = flags->parsable;
1673 error = pthread_create(&tid, NULL,
1674 send_progress_thread, &pa);
1681 error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1682 lzc_flags, resumeobj, resumeoff);
1684 if (flags->progress) {
1685 (void) pthread_cancel(tid);
1686 (void) pthread_join(tid, NULL);
1690 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1691 "warning: cannot send '%s'"), zhp->zfs_name);
1713 zfs_error_aux(hdl, strerror(errno));
1714 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1717 return (zfs_standard_error(hdl, errno, errbuf));
1728 * Generate a send stream for the dataset identified by the argument zhp.
1730 * The content of the send stream is the snapshot identified by
1731 * 'tosnap'. Incremental streams are requested in two ways:
1732 * - from the snapshot identified by "fromsnap" (if non-null) or
1733 * - from the origin of the dataset identified by zhp, which must
1734 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1737 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1738 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1739 * if "replicate" is set. If "doall" is set, dump all the intermediate
1740 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1741 * case too. If "props" is set, send properties.
1744 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1745 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1746 void *cb_arg, nvlist_t **debugnvp)
1749 send_dump_data_t sdd = { 0 };
1751 nvlist_t *fss = NULL;
1752 avl_tree_t *fsavl = NULL;
1753 static uint64_t holdseq;
1757 dedup_arg_t dda = { 0 };
1758 int featureflags = 0;
1761 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1762 "cannot send '%s'"), zhp->zfs_name);
1764 if (fromsnap && fromsnap[0] == '\0') {
1765 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1766 "zero-length incremental source"));
1767 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1770 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1772 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1773 if (version >= ZPL_VERSION_SA) {
1774 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1778 if (flags->dedup && !flags->dryrun) {
1779 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1780 DMU_BACKUP_FEATURE_DEDUPPROPS);
1781 if ((err = pipe(pipefd)) != 0) {
1782 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1783 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1786 dda.outputfd = outfd;
1787 dda.inputfd = pipefd[1];
1788 dda.dedup_hdl = zhp->zfs_hdl;
1789 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1790 (void) close(pipefd[0]);
1791 (void) close(pipefd[1]);
1792 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1793 return (zfs_error(zhp->zfs_hdl,
1794 EZFS_THREADCREATEFAILED, errbuf));
1798 if (flags->replicate || flags->doall || flags->props) {
1799 dmu_replay_record_t drr = { 0 };
1800 char *packbuf = NULL;
1802 zio_cksum_t zc = { 0 };
1804 if (flags->replicate || flags->props) {
1807 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1809 VERIFY(0 == nvlist_add_string(hdrnv,
1810 "fromsnap", fromsnap));
1812 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1813 if (!flags->replicate) {
1814 VERIFY(0 == nvlist_add_boolean(hdrnv,
1818 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1819 fromsnap, tosnap, flags->replicate, flags->verbose,
1823 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1824 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1834 if (!flags->dryrun) {
1835 /* write first begin record */
1836 drr.drr_type = DRR_BEGIN;
1837 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1838 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1839 drr_versioninfo, DMU_COMPOUNDSTREAM);
1840 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1841 drr_versioninfo, featureflags);
1842 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1843 sizeof (drr.drr_u.drr_begin.drr_toname),
1844 "%s@%s", zhp->zfs_name, tosnap);
1845 drr.drr_payloadlen = buflen;
1847 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1852 /* write end record */
1853 bzero(&drr, sizeof (drr));
1854 drr.drr_type = DRR_END;
1855 drr.drr_u.drr_end.drr_checksum = zc;
1856 err = write(outfd, &drr, sizeof (drr));
1866 /* dump each stream */
1867 sdd.fromsnap = fromsnap;
1868 sdd.tosnap = tosnap;
1870 sdd.outfd = pipefd[0];
1873 sdd.replicate = flags->replicate;
1874 sdd.doall = flags->doall;
1875 sdd.fromorigin = flags->fromorigin;
1878 sdd.verbose = flags->verbose;
1879 sdd.parsable = flags->parsable;
1880 sdd.progress = flags->progress;
1881 sdd.dryrun = flags->dryrun;
1882 sdd.large_block = flags->largeblock;
1883 sdd.embed_data = flags->embed_data;
1884 sdd.compress = flags->compress;
1885 sdd.filter_cb = filter_func;
1886 sdd.filter_cb_arg = cb_arg;
1888 sdd.debugnv = *debugnvp;
1889 if (sdd.verbose && sdd.dryrun)
1890 sdd.std_out = B_TRUE;
1891 fout = sdd.std_out ? stdout : stderr;
1894 * Some flags require that we place user holds on the datasets that are
1895 * being sent so they don't get destroyed during the send. We can skip
1896 * this step if the pool is imported read-only since the datasets cannot
1899 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1900 ZPOOL_PROP_READONLY, NULL) &&
1901 zfs_spa_version(zhp, &spa_version) == 0 &&
1902 spa_version >= SPA_VERSION_USERREFS &&
1903 (flags->doall || flags->replicate)) {
1905 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1906 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1907 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1908 if (sdd.cleanup_fd < 0) {
1912 sdd.snapholds = fnvlist_alloc();
1914 sdd.cleanup_fd = -1;
1915 sdd.snapholds = NULL;
1917 if (flags->verbose || sdd.snapholds != NULL) {
1919 * Do a verbose no-op dry run to get all the verbose output
1920 * or to gather snapshot hold's before generating any data,
1921 * then do a non-verbose real run to generate the streams.
1923 sdd.dryrun = B_TRUE;
1924 err = dump_filesystems(zhp, &sdd);
1929 if (flags->verbose) {
1930 if (flags->parsable) {
1931 (void) fprintf(fout, "size\t%llu\n",
1932 (longlong_t)sdd.size);
1935 zfs_nicenum(sdd.size, buf, sizeof (buf));
1936 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1937 "total estimated size is %s\n"), buf);
1941 /* Ensure no snaps found is treated as an error. */
1947 /* Skip the second run if dryrun was requested. */
1951 if (sdd.snapholds != NULL) {
1952 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
1956 fnvlist_free(sdd.snapholds);
1957 sdd.snapholds = NULL;
1960 sdd.dryrun = B_FALSE;
1961 sdd.verbose = B_FALSE;
1964 err = dump_filesystems(zhp, &sdd);
1965 fsavl_destroy(fsavl);
1968 /* Ensure no snaps found is treated as an error. */
1969 if (err == 0 && !sdd.seento)
1974 (void) pthread_cancel(tid);
1975 (void) close(pipefd[0]);
1976 (void) pthread_join(tid, NULL);
1979 if (sdd.cleanup_fd != -1) {
1980 VERIFY(0 == close(sdd.cleanup_fd));
1981 sdd.cleanup_fd = -1;
1984 if (!flags->dryrun && (flags->replicate || flags->doall ||
1987 * write final end record. NB: want to do this even if
1988 * there was some error, because it might not be totally
1991 dmu_replay_record_t drr = { 0 };
1992 drr.drr_type = DRR_END;
1993 if (write(outfd, &drr, sizeof (drr)) == -1) {
1994 return (zfs_standard_error(zhp->zfs_hdl,
1999 return (err || sdd.err);
2002 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2004 fsavl_destroy(fsavl);
2006 fnvlist_free(sdd.snapholds);
2008 if (sdd.cleanup_fd != -1)
2009 VERIFY(0 == close(sdd.cleanup_fd));
2011 (void) pthread_cancel(tid);
2012 (void) close(pipefd[0]);
2013 (void) pthread_join(tid, NULL);
2019 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
2020 enum lzc_send_flags flags)
2023 libzfs_handle_t *hdl = zhp->zfs_hdl;
2026 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2027 "warning: cannot send '%s'"), zhp->zfs_name);
2029 err = lzc_send(zhp->zfs_name, from, fd, flags);
2033 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2034 "not an earlier snapshot from the same fs"));
2035 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2039 if (lzc_exists(zhp->zfs_name)) {
2040 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2041 "incremental source (%s) does not exist"),
2044 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2047 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2048 "target is busy; if a filesystem, "
2049 "it must not be mounted"));
2050 return (zfs_error(hdl, EZFS_BUSY, errbuf));
2065 zfs_error_aux(hdl, strerror(errno));
2066 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2069 return (zfs_standard_error(hdl, errno, errbuf));
2076 * Routines specific to "zfs recv"
2080 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2081 boolean_t byteswap, zio_cksum_t *zc)
2087 assert(ilen <= SPA_MAXBLOCKSIZE);
2090 rv = read(fd, cp, len);
2095 if (rv < 0 || len != 0) {
2096 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2097 "failed to read from stream"));
2098 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2099 "cannot receive")));
2104 (void) fletcher_4_incremental_byteswap(buf, ilen, zc);
2106 (void) fletcher_4_incremental_native(buf, ilen, zc);
2112 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2113 boolean_t byteswap, zio_cksum_t *zc)
2118 buf = zfs_alloc(hdl, len);
2122 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2128 err = nvlist_unpack(buf, len, nvp, 0);
2131 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2132 "stream (malformed nvlist)"));
2139 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2140 int baselen, char *newname, recvflags_t *flags)
2143 zfs_cmd_t zc = { 0 };
2145 prop_changelist_t *clp;
2148 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2151 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2152 flags->force ? MS_FORCE : 0);
2156 err = changelist_prefix(clp);
2160 zc.zc_objset_type = DMU_OST_ZFS;
2161 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2164 (void) strcpy(newname, tryname);
2166 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
2168 if (flags->verbose) {
2169 (void) printf("attempting rename %s to %s\n",
2170 zc.zc_name, zc.zc_value);
2172 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2174 changelist_rename(clp, name, tryname);
2179 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2182 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2183 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2184 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
2186 if (flags->verbose) {
2187 (void) printf("failed - trying rename %s to %s\n",
2188 zc.zc_name, zc.zc_value);
2190 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2192 changelist_rename(clp, name, newname);
2193 if (err && flags->verbose) {
2194 (void) printf("failed (%u) - "
2195 "will try again on next pass\n", errno);
2198 } else if (flags->verbose) {
2200 (void) printf("success\n");
2202 (void) printf("failed (%u)\n", errno);
2205 (void) changelist_postfix(clp);
2206 changelist_free(clp);
2212 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2213 char *newname, recvflags_t *flags)
2215 zfs_cmd_t zc = { 0 };
2217 prop_changelist_t *clp;
2219 boolean_t defer = B_FALSE;
2222 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2225 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2226 flags->force ? MS_FORCE : 0);
2227 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2228 zfs_spa_version(zhp, &spa_version) == 0 &&
2229 spa_version >= SPA_VERSION_USERREFS)
2234 err = changelist_prefix(clp);
2238 zc.zc_objset_type = DMU_OST_ZFS;
2239 zc.zc_defer_destroy = defer;
2240 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2243 (void) printf("attempting destroy %s\n", zc.zc_name);
2244 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
2247 (void) printf("success\n");
2248 changelist_remove(clp, zc.zc_name);
2251 (void) changelist_postfix(clp);
2252 changelist_free(clp);
2255 * Deferred destroy might destroy the snapshot or only mark it to be
2256 * destroyed later, and it returns success in either case.
2258 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2259 ZFS_TYPE_SNAPSHOT))) {
2260 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2266 typedef struct guid_to_name_data {
2268 boolean_t bookmark_ok;
2271 } guid_to_name_data_t;
2274 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2276 guid_to_name_data_t *gtnd = arg;
2280 if (gtnd->skip != NULL &&
2281 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2282 strcmp(slash + 1, gtnd->skip) == 0) {
2287 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2288 (void) strcpy(gtnd->name, zhp->zfs_name);
2293 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2294 if (err != EEXIST && gtnd->bookmark_ok)
2295 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2301 * Attempt to find the local dataset associated with this guid. In the case of
2302 * multiple matches, we attempt to find the "best" match by searching
2303 * progressively larger portions of the hierarchy. This allows one to send a
2304 * tree of datasets individually and guarantee that we will find the source
2305 * guid within that hierarchy, even if there are multiple matches elsewhere.
2308 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2309 boolean_t bookmark_ok, char *name)
2311 char pname[ZFS_MAX_DATASET_NAME_LEN];
2312 guid_to_name_data_t gtnd;
2315 gtnd.bookmark_ok = bookmark_ok;
2320 * Search progressively larger portions of the hierarchy, starting
2321 * with the filesystem specified by 'parent'. This will
2322 * select the "most local" version of the origin snapshot in the case
2323 * that there are multiple matching snapshots in the system.
2325 (void) strlcpy(pname, parent, sizeof (pname));
2326 char *cp = strrchr(pname, '@');
2328 cp = strchr(pname, '\0');
2329 for (; cp != NULL; cp = strrchr(pname, '/')) {
2330 /* Chop off the last component and open the parent */
2332 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2336 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
2338 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2339 if (err != EEXIST && bookmark_ok)
2340 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2346 * Remember the last portion of the dataset so we skip it next
2347 * time through (as we've already searched that portion of the
2350 gtnd.skip = strrchr(pname, '/') + 1;
2357 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2358 * guid1 is after guid2.
2361 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2362 uint64_t guid1, uint64_t guid2)
2365 char *fsname, *snapname;
2366 char buf[ZFS_MAX_DATASET_NAME_LEN];
2368 zfs_handle_t *guid1hdl, *guid2hdl;
2369 uint64_t create1, create2;
2376 nvfs = fsavl_find(avl, guid1, &snapname);
2377 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2378 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2379 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2380 if (guid1hdl == NULL)
2383 nvfs = fsavl_find(avl, guid2, &snapname);
2384 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2385 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2386 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2387 if (guid2hdl == NULL) {
2388 zfs_close(guid1hdl);
2392 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2393 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2395 if (create1 < create2)
2397 else if (create1 > create2)
2402 zfs_close(guid1hdl);
2403 zfs_close(guid2hdl);
2409 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2410 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2413 nvlist_t *local_nv, *deleted = NULL;
2414 avl_tree_t *local_avl;
2415 nvpair_t *fselem, *nextfselem;
2417 char newname[ZFS_MAX_DATASET_NAME_LEN];
2420 boolean_t needagain, progress, recursive;
2423 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2425 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2432 needagain = progress = B_FALSE;
2434 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2436 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2437 recursive, B_FALSE, &local_nv, &local_avl)) != 0)
2441 * Process deletes and renames
2443 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2444 fselem; fselem = nextfselem) {
2445 nvlist_t *nvfs, *snaps;
2446 nvlist_t *stream_nvfs = NULL;
2447 nvpair_t *snapelem, *nextsnapelem;
2448 uint64_t fromguid = 0;
2449 uint64_t originguid = 0;
2450 uint64_t stream_originguid = 0;
2451 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2452 char *fsname, *stream_fsname;
2454 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2456 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2457 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2458 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2459 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2460 &parent_fromsnap_guid));
2461 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2464 * First find the stream's fs, so we can check for
2465 * a different origin (due to "zfs promote")
2467 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2468 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2471 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2472 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2474 if (stream_nvfs != NULL)
2478 /* check for promote */
2479 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2480 &stream_originguid);
2481 if (stream_nvfs && originguid != stream_originguid) {
2482 switch (created_before(hdl, local_avl,
2483 stream_originguid, originguid)) {
2486 zfs_cmd_t zc = { 0 };
2487 nvlist_t *origin_nvfs;
2488 char *origin_fsname;
2491 (void) printf("promoting %s\n", fsname);
2493 origin_nvfs = fsavl_find(local_avl, originguid,
2495 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2496 "name", &origin_fsname));
2497 (void) strlcpy(zc.zc_value, origin_fsname,
2498 sizeof (zc.zc_value));
2499 (void) strlcpy(zc.zc_name, fsname,
2500 sizeof (zc.zc_name));
2501 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2509 fsavl_destroy(local_avl);
2510 nvlist_free(local_nv);
2514 * We had/have the wrong origin, therefore our
2515 * list of snapshots is wrong. Need to handle
2516 * them on the next pass.
2522 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2523 snapelem; snapelem = nextsnapelem) {
2525 char *stream_snapname;
2526 nvlist_t *found, *props;
2528 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2530 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2531 found = fsavl_find(stream_avl, thisguid,
2534 /* check for delete */
2535 if (found == NULL) {
2536 char name[ZFS_MAX_DATASET_NAME_LEN];
2541 (void) snprintf(name, sizeof (name), "%s@%s",
2542 fsname, nvpair_name(snapelem));
2544 error = recv_destroy(hdl, name,
2545 strlen(fsname)+1, newname, flags);
2550 sprintf(guidname, "%" PRIu64, thisguid);
2551 nvlist_add_boolean(deleted, guidname);
2555 stream_nvfs = found;
2557 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2558 &props) && 0 == nvlist_lookup_nvlist(props,
2559 stream_snapname, &props)) {
2560 zfs_cmd_t zc = { 0 };
2562 zc.zc_cookie = B_TRUE; /* received */
2563 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2564 "%s@%s", fsname, nvpair_name(snapelem));
2565 if (zcmd_write_src_nvlist(hdl, &zc,
2567 (void) zfs_ioctl(hdl,
2568 ZFS_IOC_SET_PROP, &zc);
2569 zcmd_free_nvlists(&zc);
2573 /* check for different snapname */
2574 if (strcmp(nvpair_name(snapelem),
2575 stream_snapname) != 0) {
2576 char name[ZFS_MAX_DATASET_NAME_LEN];
2577 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2579 (void) snprintf(name, sizeof (name), "%s@%s",
2580 fsname, nvpair_name(snapelem));
2581 (void) snprintf(tryname, sizeof (name), "%s@%s",
2582 fsname, stream_snapname);
2584 error = recv_rename(hdl, name, tryname,
2585 strlen(fsname)+1, newname, flags);
2592 if (strcmp(stream_snapname, fromsnap) == 0)
2593 fromguid = thisguid;
2596 /* check for delete */
2597 if (stream_nvfs == NULL) {
2601 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2607 sprintf(guidname, "%" PRIu64, parent_fromsnap_guid);
2608 nvlist_add_boolean(deleted, guidname);
2612 if (fromguid == 0) {
2613 if (flags->verbose) {
2614 (void) printf("local fs %s does not have "
2615 "fromsnap (%s in stream); must have "
2616 "been deleted locally; ignoring\n",
2622 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2623 "name", &stream_fsname));
2624 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2625 "parentfromsnap", &stream_parent_fromsnap_guid));
2627 s1 = strrchr(fsname, '/');
2628 s2 = strrchr(stream_fsname, '/');
2631 * Check if we're going to rename based on parent guid change
2632 * and the current parent guid was also deleted. If it was then
2633 * rename will fail and is likely unneeded, so avoid this and
2634 * force an early retry to determine the new
2635 * parent_fromsnap_guid.
2637 if (stream_parent_fromsnap_guid != 0 &&
2638 parent_fromsnap_guid != 0 &&
2639 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
2640 sprintf(guidname, "%" PRIu64, parent_fromsnap_guid);
2641 if (nvlist_exists(deleted, guidname)) {
2649 * Check for rename. If the exact receive path is specified, it
2650 * does not count as a rename, but we still need to check the
2651 * datasets beneath it.
2653 if ((stream_parent_fromsnap_guid != 0 &&
2654 parent_fromsnap_guid != 0 &&
2655 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2656 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2657 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2659 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2661 parent = fsavl_find(local_avl,
2662 stream_parent_fromsnap_guid, NULL);
2664 * NB: parent might not be found if we used the
2665 * tosnap for stream_parent_fromsnap_guid,
2666 * because the parent is a newly-created fs;
2667 * we'll be able to rename it after we recv the
2670 if (parent != NULL) {
2673 VERIFY(0 == nvlist_lookup_string(parent, "name",
2675 (void) snprintf(tryname, sizeof (tryname),
2676 "%s%s", pname, strrchr(stream_fsname, '/'));
2679 if (flags->verbose) {
2680 (void) printf("local fs %s new parent "
2681 "not found\n", fsname);
2687 error = recv_rename(hdl, fsname, tryname,
2688 strlen(tofs)+1, newname, flags);
2690 if (renamed != NULL && newname[0] != '\0') {
2691 VERIFY(0 == nvlist_add_boolean(renamed,
2703 fsavl_destroy(local_avl);
2704 nvlist_free(local_nv);
2705 nvlist_free(deleted);
2707 if (needagain && progress) {
2708 /* do another pass to fix up temporary names */
2710 (void) printf("another pass:\n");
2718 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2719 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2720 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2722 nvlist_t *stream_nv = NULL;
2723 avl_tree_t *stream_avl = NULL;
2724 char *fromsnap = NULL;
2725 char *sendsnap = NULL;
2727 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2728 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
2730 dmu_replay_record_t drre;
2732 boolean_t anyerr = B_FALSE;
2733 boolean_t softerr = B_FALSE;
2734 boolean_t recursive;
2736 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2739 assert(drr->drr_type == DRR_BEGIN);
2740 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2741 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2742 DMU_COMPOUNDSTREAM);
2745 * Read in the nvlist from the stream.
2747 if (drr->drr_payloadlen != 0) {
2748 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2749 &stream_nv, flags->byteswap, zc);
2751 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2756 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2759 if (recursive && strchr(destname, '@')) {
2760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2761 "cannot specify snapshot name for multi-snapshot stream"));
2762 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2767 * Read in the end record and verify checksum.
2769 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2770 flags->byteswap, NULL)))
2772 if (flags->byteswap) {
2773 drre.drr_type = BSWAP_32(drre.drr_type);
2774 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2775 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2776 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2777 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2778 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2779 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2780 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2781 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2783 if (drre.drr_type != DRR_END) {
2784 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2787 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2788 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2789 "incorrect header checksum"));
2790 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2794 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2796 if (drr->drr_payloadlen != 0) {
2797 nvlist_t *stream_fss;
2799 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2801 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2802 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2803 "couldn't allocate avl tree"));
2804 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2808 if (fromsnap != NULL && recursive) {
2809 nvlist_t *renamed = NULL;
2810 nvpair_t *pair = NULL;
2812 (void) strlcpy(tofs, destname, sizeof (tofs));
2813 if (flags->isprefix) {
2814 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2817 if (flags->istail) {
2818 cp = strrchr(drrb->drr_toname, '/');
2820 (void) strlcat(tofs, "/",
2824 i = (cp - drrb->drr_toname);
2827 i = strcspn(drrb->drr_toname, "/@");
2829 /* zfs_receive_one() will create_parents() */
2830 (void) strlcat(tofs, &drrb->drr_toname[i],
2832 *strchr(tofs, '@') = '\0';
2835 if (!flags->dryrun && !flags->nomount) {
2836 VERIFY(0 == nvlist_alloc(&renamed,
2837 NV_UNIQUE_NAME, 0));
2840 softerr = recv_incremental_replication(hdl, tofs, flags,
2841 stream_nv, stream_avl, renamed);
2843 /* Unmount renamed filesystems before receiving. */
2844 while ((pair = nvlist_next_nvpair(renamed,
2847 prop_changelist_t *clp = NULL;
2849 zhp = zfs_open(hdl, nvpair_name(pair),
2850 ZFS_TYPE_FILESYSTEM);
2852 clp = changelist_gather(zhp,
2853 ZFS_PROP_MOUNTPOINT, 0, 0);
2857 changelist_prefix(clp);
2858 changelist_free(clp);
2863 nvlist_free(renamed);
2868 * Get the fs specified by the first path in the stream (the top level
2869 * specified by 'zfs send') and pass it to each invocation of
2870 * zfs_receive_one().
2872 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2874 if ((cp = strchr(sendfs, '@')) != NULL) {
2877 * Find the "sendsnap", the final snapshot in a replication
2878 * stream. zfs_receive_one() handles certain errors
2879 * differently, depending on if the contained stream is the
2882 sendsnap = (cp + 1);
2885 /* Finally, receive each contained stream */
2888 * we should figure out if it has a recoverable
2889 * error, in which case do a recv_skip() and drive on.
2890 * Note, if we fail due to already having this guid,
2891 * zfs_receive_one() will take care of it (ie,
2892 * recv_skip() and return 0).
2894 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2895 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2896 action_handlep, sendsnap);
2897 if (error == ENODATA) {
2902 } while (error == 0);
2904 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
2906 * Now that we have the fs's they sent us, try the
2909 softerr = recv_incremental_replication(hdl, tofs, flags,
2910 stream_nv, stream_avl, NULL);
2914 fsavl_destroy(stream_avl);
2915 nvlist_free(stream_nv);
2924 trunc_prop_errs(int truncated)
2926 ASSERT(truncated != 0);
2929 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2930 "1 more property could not be set\n"));
2932 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2933 "%d more properties could not be set\n"), truncated);
2937 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2939 dmu_replay_record_t *drr;
2940 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
2943 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2944 "cannot receive:"));
2946 /* XXX would be great to use lseek if possible... */
2949 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2950 byteswap, NULL) == 0) {
2952 drr->drr_type = BSWAP_32(drr->drr_type);
2954 switch (drr->drr_type) {
2956 if (drr->drr_payloadlen != 0) {
2957 (void) recv_read(hdl, fd, buf,
2958 drr->drr_payloadlen, B_FALSE, NULL);
2968 drr->drr_u.drr_object.drr_bonuslen =
2969 BSWAP_32(drr->drr_u.drr_object.
2972 (void) recv_read(hdl, fd, buf,
2973 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2979 drr->drr_u.drr_write.drr_logical_size =
2981 drr->drr_u.drr_write.drr_logical_size);
2982 drr->drr_u.drr_write.drr_compressed_size =
2984 drr->drr_u.drr_write.drr_compressed_size);
2986 uint64_t payload_size =
2987 DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
2988 (void) recv_read(hdl, fd, buf,
2989 payload_size, B_FALSE, NULL);
2993 drr->drr_u.drr_spill.drr_length =
2994 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2996 (void) recv_read(hdl, fd, buf,
2997 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2999 case DRR_WRITE_EMBEDDED:
3001 drr->drr_u.drr_write_embedded.drr_psize =
3002 BSWAP_32(drr->drr_u.drr_write_embedded.
3005 (void) recv_read(hdl, fd, buf,
3006 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3009 case DRR_WRITE_BYREF:
3010 case DRR_FREEOBJECTS:
3015 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3016 "invalid record type"));
3017 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3026 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3027 boolean_t resumable)
3029 char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3031 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3032 "checksum mismatch or incomplete stream"));
3036 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3037 *strchr(target_fs, '@') = '\0';
3038 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3039 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3043 char token_buf[ZFS_MAXPROPLEN];
3044 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3045 token_buf, sizeof (token_buf),
3046 NULL, NULL, 0, B_TRUE);
3048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3049 "checksum mismatch or incomplete stream.\n"
3050 "Partially received snapshot is saved.\n"
3051 "A resuming stream can be generated on the sending "
3052 "system by running:\n"
3060 * Restores a backup of tosnap from the file descriptor specified by infd.
3063 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3064 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3065 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3066 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3067 uint64_t *action_handlep, const char *finalsnap)
3069 zfs_cmd_t zc = { 0 };
3071 int ioctl_err, ioctl_errno, err;
3073 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3075 char prop_errbuf[1024];
3076 const char *chopprefix;
3077 boolean_t newfs = B_FALSE;
3078 boolean_t stream_wantsnewfs;
3079 uint64_t parent_snapguid = 0;
3080 prop_changelist_t *clp = NULL;
3081 nvlist_t *snapprops_nvlist = NULL;
3082 zprop_errflags_t prop_errflags;
3083 boolean_t recursive;
3084 char *snapname = NULL;
3086 begin_time = time(NULL);
3088 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3091 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3094 if (stream_avl != NULL) {
3095 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3100 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
3102 err = nvlist_lookup_nvlist(fs, "props", &props);
3104 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
3106 if (flags->canmountoff) {
3107 VERIFY(0 == nvlist_add_uint64(props,
3108 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3110 ret = zcmd_write_src_nvlist(hdl, &zc, props);
3114 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
3115 VERIFY(0 == nvlist_lookup_nvlist(props,
3116 snapname, &snapprops_nvlist));
3126 * Determine how much of the snapshot name stored in the stream
3127 * we are going to tack on to the name they specified on the
3128 * command line, and how much we are going to chop off.
3130 * If they specified a snapshot, chop the entire name stored in
3133 if (flags->istail) {
3135 * A filesystem was specified with -e. We want to tack on only
3136 * the tail of the sent snapshot path.
3138 if (strchr(tosnap, '@')) {
3139 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3140 "argument - snapshot not allowed with -e"));
3141 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3144 chopprefix = strrchr(sendfs, '/');
3146 if (chopprefix == NULL) {
3148 * The tail is the poolname, so we need to
3149 * prepend a path separator.
3151 int len = strlen(drrb->drr_toname);
3152 cp = malloc(len + 2);
3154 (void) strcpy(&cp[1], drrb->drr_toname);
3157 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3159 } else if (flags->isprefix) {
3161 * A filesystem was specified with -d. We want to tack on
3162 * everything but the first element of the sent snapshot path
3163 * (all but the pool name).
3165 if (strchr(tosnap, '@')) {
3166 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3167 "argument - snapshot not allowed with -d"));
3168 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3171 chopprefix = strchr(drrb->drr_toname, '/');
3172 if (chopprefix == NULL)
3173 chopprefix = strchr(drrb->drr_toname, '@');
3174 } else if (strchr(tosnap, '@') == NULL) {
3176 * If a filesystem was specified without -d or -e, we want to
3177 * tack on everything after the fs specified by 'zfs send'.
3179 chopprefix = drrb->drr_toname + strlen(sendfs);
3181 /* A snapshot was specified as an exact path (no -d or -e). */
3183 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3184 "cannot specify snapshot name for multi-snapshot "
3186 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3188 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3191 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3192 ASSERT(chopprefix > drrb->drr_toname);
3193 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
3194 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3195 chopprefix[0] == '\0');
3198 * Determine name of destination snapshot, store in zc_value.
3200 (void) strcpy(zc.zc_value, tosnap);
3201 (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
3203 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
3204 zfs_ioctl_version = get_zfs_ioctl_version();
3206 * For forward compatibility hide tosnap in zc_value
3208 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
3209 (void) strcpy(zc.zc_value + strlen(zc.zc_value) + 1, tosnap);
3212 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
3213 zcmd_free_nvlists(&zc);
3214 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3218 * Determine the name of the origin snapshot, store in zc_string.
3221 (void) strncpy(zc.zc_string, originsnap, sizeof (zc.zc_string));
3223 (void) printf("using provided clone origin %s\n",
3225 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3226 if (guid_to_name(hdl, zc.zc_value,
3227 drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3228 zcmd_free_nvlists(&zc);
3229 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3230 "local origin for clone %s does not exist"),
3232 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3235 (void) printf("found clone origin %s\n", zc.zc_string);
3238 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3239 DMU_BACKUP_FEATURE_RESUMING;
3240 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
3241 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3243 if (stream_wantsnewfs) {
3245 * if the parent fs does not exist, look for it based on
3246 * the parent snap GUID
3248 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3249 "cannot receive new filesystem stream"));
3251 (void) strcpy(zc.zc_name, zc.zc_value);
3252 cp = strrchr(zc.zc_name, '/');
3256 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3257 char suffix[ZFS_MAX_DATASET_NAME_LEN];
3258 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
3259 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
3260 B_FALSE, zc.zc_value) == 0) {
3261 *strchr(zc.zc_value, '@') = '\0';
3262 (void) strcat(zc.zc_value, suffix);
3267 * if the fs does not exist, look for it based on the
3270 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3271 "cannot receive incremental stream"));
3273 (void) strcpy(zc.zc_name, zc.zc_value);
3274 *strchr(zc.zc_name, '@') = '\0';
3277 * If the exact receive path was specified and this is the
3278 * topmost path in the stream, then if the fs does not exist we
3279 * should look no further.
3281 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
3282 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
3283 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3284 char snap[ZFS_MAX_DATASET_NAME_LEN];
3285 (void) strcpy(snap, strchr(zc.zc_value, '@'));
3286 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
3287 B_FALSE, zc.zc_value) == 0) {
3288 *strchr(zc.zc_value, '@') = '\0';
3289 (void) strcat(zc.zc_value, snap);
3294 (void) strcpy(zc.zc_name, zc.zc_value);
3295 *strchr(zc.zc_name, '@') = '\0';
3297 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3301 * Destination fs exists. It must be one of these cases:
3302 * - an incremental send stream
3303 * - the stream specifies a new fs (full stream or clone)
3304 * and they want us to blow away the existing fs (and
3305 * have therefore specified -F and removed any snapshots)
3306 * - we are resuming a failed receive.
3308 if (stream_wantsnewfs) {
3309 if (!flags->force) {
3310 zcmd_free_nvlists(&zc);
3311 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3312 "destination '%s' exists\n"
3313 "must specify -F to overwrite it"),
3315 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3317 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3319 zcmd_free_nvlists(&zc);
3320 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3321 "destination has snapshots (eg. %s)\n"
3322 "must destroy them to overwrite it"),
3324 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3328 if ((zhp = zfs_open(hdl, zc.zc_name,
3329 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
3330 zcmd_free_nvlists(&zc);
3334 if (stream_wantsnewfs &&
3335 zhp->zfs_dmustats.dds_origin[0]) {
3336 zcmd_free_nvlists(&zc);
3338 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3339 "destination '%s' is a clone\n"
3340 "must destroy it to overwrite it"),
3342 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3345 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3346 stream_wantsnewfs) {
3347 /* We can't do online recv in this case */
3348 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3351 zcmd_free_nvlists(&zc);
3354 if (changelist_prefix(clp) != 0) {
3355 changelist_free(clp);
3357 zcmd_free_nvlists(&zc);
3363 * If we are resuming a newfs, set newfs here so that we will
3364 * mount it if the recv succeeds this time. We can tell
3365 * that it was a newfs on the first recv because the fs
3366 * itself will be inconsistent (if the fs existed when we
3367 * did the first recv, we would have received it into
3370 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
3376 * Destination filesystem does not exist. Therefore we better
3377 * be creating a new filesystem (either from a full backup, or
3378 * a clone). It would therefore be invalid if the user
3379 * specified only the pool name (i.e. if the destination name
3380 * contained no slash character).
3382 if (!stream_wantsnewfs ||
3383 (cp = strrchr(zc.zc_name, '/')) == NULL) {
3384 zcmd_free_nvlists(&zc);
3385 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3386 "destination '%s' does not exist"), zc.zc_name);
3387 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3391 * Trim off the final dataset component so we perform the
3392 * recvbackup ioctl to the filesystems's parent.
3396 if (flags->isprefix && !flags->istail && !flags->dryrun &&
3397 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
3398 zcmd_free_nvlists(&zc);
3399 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3405 zc.zc_begin_record = *drr_noswap;
3406 zc.zc_cookie = infd;
3407 zc.zc_guid = flags->force;
3408 zc.zc_resumable = flags->resumable;
3409 if (flags->verbose) {
3410 (void) printf("%s %s stream of %s into %s\n",
3411 flags->dryrun ? "would receive" : "receiving",
3412 drrb->drr_fromguid ? "incremental" : "full",
3413 drrb->drr_toname, zc.zc_value);
3414 (void) fflush(stdout);
3417 if (flags->dryrun) {
3418 zcmd_free_nvlists(&zc);
3419 return (recv_skip(hdl, infd, flags->byteswap));
3422 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
3423 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
3424 zc.zc_cleanup_fd = cleanup_fd;
3425 zc.zc_action_handle = *action_handlep;
3427 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
3428 ioctl_errno = errno;
3429 prop_errflags = (zprop_errflags_t)zc.zc_obj;
3432 nvlist_t *prop_errors;
3433 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
3434 zc.zc_nvlist_dst_size, &prop_errors, 0));
3436 nvpair_t *prop_err = NULL;
3438 while ((prop_err = nvlist_next_nvpair(prop_errors,
3439 prop_err)) != NULL) {
3444 prop = zfs_name_to_prop(nvpair_name(prop_err));
3445 (void) nvpair_value_int32(prop_err, &intval);
3446 if (strcmp(nvpair_name(prop_err),
3447 ZPROP_N_MORE_ERRORS) == 0) {
3448 trunc_prop_errs(intval);
3450 } else if (snapname == NULL || finalsnap == NULL ||
3451 strcmp(finalsnap, snapname) == 0 ||
3452 strcmp(nvpair_name(prop_err),
3453 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
3455 * Skip the special case of, for example,
3456 * "refquota", errors on intermediate
3457 * snapshots leading up to a final one.
3458 * That's why we have all of the checks above.
3460 * See zfs_ioctl.c's extract_delay_props() for
3461 * a list of props which can fail on
3462 * intermediate snapshots, but shouldn't
3463 * affect the overall receive.
3465 (void) snprintf(tbuf, sizeof (tbuf),
3466 dgettext(TEXT_DOMAIN,
3467 "cannot receive %s property on %s"),
3468 nvpair_name(prop_err), zc.zc_name);
3469 zfs_setprop_error(hdl, prop, intval, tbuf);
3472 nvlist_free(prop_errors);
3475 zc.zc_nvlist_dst = 0;
3476 zc.zc_nvlist_dst_size = 0;
3477 zcmd_free_nvlists(&zc);
3479 if (err == 0 && snapprops_nvlist) {
3480 zfs_cmd_t zc2 = { 0 };
3482 (void) strcpy(zc2.zc_name, zc.zc_value);
3483 zc2.zc_cookie = B_TRUE; /* received */
3484 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
3485 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
3486 zcmd_free_nvlists(&zc2);
3490 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3492 * It may be that this snapshot already exists,
3493 * in which case we want to consume & ignore it
3494 * rather than failing.
3496 avl_tree_t *local_avl;
3497 nvlist_t *local_nv, *fs;
3498 cp = strchr(zc.zc_value, '@');
3501 * XXX Do this faster by just iterating over snaps in
3502 * this fs. Also if zc_value does not exist, we will
3503 * get a strange "does not exist" error message.
3506 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3507 B_FALSE, &local_nv, &local_avl) == 0) {
3509 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
3510 fsavl_destroy(local_avl);
3511 nvlist_free(local_nv);
3514 if (flags->verbose) {
3515 (void) printf("snap %s already exists; "
3516 "ignoring\n", zc.zc_value);
3518 err = ioctl_err = recv_skip(hdl, infd,
3525 if (ioctl_err != 0) {
3526 switch (ioctl_errno) {
3528 cp = strchr(zc.zc_value, '@');
3530 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3531 "most recent snapshot of %s does not\n"
3532 "match incremental source"), zc.zc_value);
3533 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3537 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3538 "destination %s has been modified\n"
3539 "since most recent snapshot"), zc.zc_name);
3540 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3543 cp = strchr(zc.zc_value, '@');
3545 /* it's the containing fs that exists */
3548 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3549 "destination already exists"));
3550 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
3551 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
3556 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3559 recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
3560 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3563 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3564 "pool must be upgraded to receive this stream."));
3565 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3568 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3569 "destination %s space quota exceeded"), zc.zc_name);
3570 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3573 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3578 * Mount the target filesystem (if created). Also mount any
3579 * children of the target filesystem if we did a replication
3580 * receive (indicated by stream_avl being non-NULL).
3582 cp = strchr(zc.zc_value, '@');
3583 if (cp && (ioctl_err == 0 || !newfs)) {
3587 h = zfs_open(hdl, zc.zc_value,
3588 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3590 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3592 } else if (newfs || stream_avl) {
3594 * Track the first/top of hierarchy fs,
3595 * for mounting and sharing later.
3597 if (top_zfs && *top_zfs == NULL)
3598 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3606 if (!flags->nomount)
3607 err |= changelist_postfix(clp);
3608 changelist_free(clp);
3611 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3612 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3613 "failed to clear unreceived properties on %s"),
3615 (void) fprintf(stderr, "\n");
3617 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3618 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3619 "failed to restore original properties on %s"),
3621 (void) fprintf(stderr, "\n");
3624 if (err || ioctl_err)
3627 *action_handlep = zc.zc_action_handle;
3629 if (flags->verbose) {
3632 uint64_t bytes = zc.zc_cookie;
3633 time_t delta = time(NULL) - begin_time;
3636 zfs_nicenum(bytes, buf1, sizeof (buf1));
3637 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3639 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3647 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3648 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
3649 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3650 uint64_t *action_handlep, const char *finalsnap)
3653 dmu_replay_record_t drr, drr_noswap;
3654 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3656 zio_cksum_t zcksum = { 0 };
3657 uint64_t featureflags;
3660 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3663 if (flags->isprefix &&
3664 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3666 "(%s) does not exist"), tosnap);
3667 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3670 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
3671 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
3672 "(%s) does not exist"), originsnap);
3673 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3676 /* read in the BEGIN record */
3677 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3681 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3682 /* It's the double end record at the end of a package */
3686 /* the kernel needs the non-byteswapped begin record */
3689 flags->byteswap = B_FALSE;
3690 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3692 * We computed the checksum in the wrong byteorder in
3693 * recv_read() above; do it again correctly.
3695 bzero(&zcksum, sizeof (zio_cksum_t));
3696 (void) fletcher_4_incremental_byteswap(&drr,
3697 sizeof (drr), &zcksum);
3698 flags->byteswap = B_TRUE;
3700 drr.drr_type = BSWAP_32(drr.drr_type);
3701 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3702 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3703 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3704 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3705 drrb->drr_type = BSWAP_32(drrb->drr_type);
3706 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3707 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3708 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3711 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3712 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3713 "stream (bad magic number)"));
3714 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3717 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3718 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3720 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3721 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3722 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3723 "stream has unsupported feature, feature flags = %lx"),
3725 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3728 if (strchr(drrb->drr_toname, '@') == NULL) {
3729 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3730 "stream (bad snapshot name)"));
3731 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3734 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3735 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
3736 if (sendfs == NULL) {
3738 * We were not called from zfs_receive_package(). Get
3739 * the fs specified by 'zfs send'.
3742 (void) strlcpy(nonpackage_sendfs,
3743 drr.drr_u.drr_begin.drr_toname,
3744 sizeof (nonpackage_sendfs));
3745 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3747 sendfs = nonpackage_sendfs;
3748 VERIFY(finalsnap == NULL);
3750 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3751 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
3752 cleanup_fd, action_handlep, finalsnap));
3754 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3755 DMU_COMPOUNDSTREAM);
3756 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
3757 &zcksum, top_zfs, cleanup_fd, action_handlep));
3762 * Restores a backup of tosnap from the file descriptor specified by infd.
3763 * Return 0 on total success, -2 if some things couldn't be
3764 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3765 * (-1 will override -2, if -1 and the resumable flag was specified the
3766 * transfer can be resumed if the sending side supports it).
3769 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
3770 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
3772 char *top_zfs = NULL;
3775 uint64_t action_handle = 0;
3776 char *originsnap = NULL;
3778 err = nvlist_lookup_string(props, "origin", &originsnap);
3779 if (err && err != ENOENT)
3783 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3784 VERIFY(cleanup_fd >= 0);
3786 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
3787 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL);
3789 VERIFY(0 == close(cleanup_fd));
3791 if (err == 0 && !flags->nomount && top_zfs) {
3793 prop_changelist_t *clp;
3795 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3797 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3798 CL_GATHER_MOUNT_ALWAYS, 0);
3801 /* mount and share received datasets */
3802 err = changelist_postfix(clp);
3803 changelist_free(clp);
3806 if (zhp == NULL || clp == NULL || err)