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, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27 * All rights reserved.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
41 #include <sys/param.h>
42 #include <sys/mount.h>
48 #include <libzfs_core.h>
50 #include "zfs_namecheck.h"
52 #include "zfs_fletcher.h"
53 #include "libzfs_impl.h"
55 #include <sys/zio_checksum.h>
59 extern int zfs_ioctl_version;
62 /* in libzfs_dataset.c */
63 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
64 /* We need to use something for ENODATA. */
67 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
68 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
71 static const zio_cksum_t zero_cksum = { 0 };
73 typedef struct dedup_arg {
76 libzfs_handle_t *dedup_hdl;
79 typedef struct progress_arg {
82 boolean_t pa_parsable;
85 typedef struct dataref {
91 typedef struct dedup_entry {
92 struct dedup_entry *dde_next;
93 zio_cksum_t dde_chksum;
98 #define MAX_DDT_PHYSMEM_PERCENT 20
99 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
101 typedef struct dedup_table {
102 dedup_entry_t **dedup_hash_array;
103 umem_cache_t *ddecache;
104 uint64_t max_ddt_size; /* max dedup table size in bytes */
105 uint64_t cur_ddt_size; /* current dedup table size in bytes */
112 high_order_bit(uint64_t n)
116 for (count = 0; n != 0; count++)
122 ssread(void *buf, size_t len, FILE *stream)
126 if ((outlen = fread(buf, len, 1, stream)) == 0)
133 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
134 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
138 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
139 if (ddt->ddt_full == B_FALSE) {
140 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
141 "Dedup table full. Deduplication will continue "
142 "with existing table entries"));
143 ddt->ddt_full = B_TRUE;
148 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
150 assert(*ddepp == NULL);
151 dde->dde_next = NULL;
152 dde->dde_chksum = *cs;
153 dde->dde_prop = prop;
156 ddt->cur_ddt_size += sizeof (dedup_entry_t);
162 * Using the specified dedup table, do a lookup for an entry with
163 * the checksum cs. If found, return the block's reference info
164 * in *dr. Otherwise, insert a new entry in the dedup table, using
165 * the reference information specified by *dr.
167 * return value: true - entry was found
168 * false - entry was not found
171 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
172 uint64_t prop, dataref_t *dr)
175 dedup_entry_t **ddepp;
177 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
179 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
180 ddepp = &((*ddepp)->dde_next)) {
181 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
182 (*ddepp)->dde_prop == prop) {
183 *dr = (*ddepp)->dde_ref;
187 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
192 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
193 zio_cksum_t *zc, int outfd)
195 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
196 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
197 fletcher_4_incremental_native(drr,
198 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
199 if (drr->drr_type != DRR_BEGIN) {
200 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
201 drr_checksum.drr_checksum));
202 drr->drr_u.drr_checksum.drr_checksum = *zc;
204 fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
205 sizeof (zio_cksum_t), zc);
206 if (write(outfd, drr, sizeof (*drr)) == -1)
208 if (payload_len != 0) {
209 fletcher_4_incremental_native(payload, payload_len, zc);
210 if (write(outfd, payload, payload_len) == -1)
217 * This function is started in a separate thread when the dedup option
218 * has been requested. The main send thread determines the list of
219 * snapshots to be included in the send stream and makes the ioctl calls
220 * for each one. But instead of having the ioctl send the output to the
221 * the output fd specified by the caller of zfs_send()), the
222 * ioctl is told to direct the output to a pipe, which is read by the
223 * alternate thread running THIS function. This function does the
225 * 1. building a dedup table (the DDT)
226 * 2. doing checksums on each data block and inserting a record in the DDT
227 * 3. looking for matching checksums, and
228 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
229 * a duplicate block is found.
230 * The output of this function then goes to the output fd requested
231 * by the caller of zfs_send().
236 dedup_arg_t *dda = arg;
237 char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
238 dmu_replay_record_t thedrr;
239 dmu_replay_record_t *drr = &thedrr;
243 zio_cksum_t stream_cksum;
244 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
248 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
249 SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
251 numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
254 * numbuckets must be a power of 2. Increase number to
255 * a power of 2 if necessary.
257 if (!ISP2(numbuckets))
258 numbuckets = 1 << high_order_bit(numbuckets);
260 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
261 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
262 NULL, NULL, NULL, NULL, NULL, 0);
263 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
264 ddt.numhashbits = high_order_bit(numbuckets) - 1;
265 ddt.ddt_full = B_FALSE;
267 outfd = dda->outputfd;
268 ofp = fdopen(dda->inputfd, "r");
269 while (ssread(drr, sizeof (*drr), ofp) != 0) {
271 switch (drr->drr_type) {
274 struct drr_begin *drrb = &drr->drr_u.drr_begin;
277 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
279 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
281 /* set the DEDUP feature flag for this stream */
282 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
283 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
284 DMU_BACKUP_FEATURE_DEDUPPROPS);
285 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
287 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
288 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) {
289 sz = drr->drr_payloadlen;
291 if (sz > SPA_MAXBLOCKSIZE) {
292 buf = zfs_realloc(dda->dedup_hdl, buf,
293 SPA_MAXBLOCKSIZE, sz);
295 (void) ssread(buf, sz, ofp);
299 if (dump_record(drr, buf, sz, &stream_cksum,
307 struct drr_end *drre = &drr->drr_u.drr_end;
308 /* use the recalculated checksum */
309 drre->drr_checksum = stream_cksum;
310 if (dump_record(drr, NULL, 0, &stream_cksum,
318 struct drr_object *drro = &drr->drr_u.drr_object;
319 if (drro->drr_bonuslen > 0) {
321 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
324 if (dump_record(drr, buf,
325 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
326 &stream_cksum, outfd) != 0)
333 struct drr_spill *drrs = &drr->drr_u.drr_spill;
334 (void) ssread(buf, drrs->drr_length, ofp);
335 if (dump_record(drr, buf, drrs->drr_length,
336 &stream_cksum, outfd) != 0)
341 case DRR_FREEOBJECTS:
343 if (dump_record(drr, NULL, 0, &stream_cksum,
351 struct drr_write *drrw = &drr->drr_u.drr_write;
354 (void) ssread(buf, drrw->drr_length, ofp);
357 * Use the existing checksum if it's dedup-capable,
358 * else calculate a SHA256 checksum for it.
361 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
363 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
365 zio_cksum_t tmpsha256;
368 SHA256Update(&ctx, buf, drrw->drr_length);
369 SHA256Final(&tmpsha256, &ctx);
370 drrw->drr_key.ddk_cksum.zc_word[0] =
371 BE_64(tmpsha256.zc_word[0]);
372 drrw->drr_key.ddk_cksum.zc_word[1] =
373 BE_64(tmpsha256.zc_word[1]);
374 drrw->drr_key.ddk_cksum.zc_word[2] =
375 BE_64(tmpsha256.zc_word[2]);
376 drrw->drr_key.ddk_cksum.zc_word[3] =
377 BE_64(tmpsha256.zc_word[3]);
378 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
379 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
382 dataref.ref_guid = drrw->drr_toguid;
383 dataref.ref_object = drrw->drr_object;
384 dataref.ref_offset = drrw->drr_offset;
386 if (ddt_update(dda->dedup_hdl, &ddt,
387 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
389 dmu_replay_record_t wbr_drr = {0};
390 struct drr_write_byref *wbr_drrr =
391 &wbr_drr.drr_u.drr_write_byref;
393 /* block already present in stream */
394 wbr_drr.drr_type = DRR_WRITE_BYREF;
396 wbr_drrr->drr_object = drrw->drr_object;
397 wbr_drrr->drr_offset = drrw->drr_offset;
398 wbr_drrr->drr_length = drrw->drr_length;
399 wbr_drrr->drr_toguid = drrw->drr_toguid;
400 wbr_drrr->drr_refguid = dataref.ref_guid;
401 wbr_drrr->drr_refobject =
403 wbr_drrr->drr_refoffset =
406 wbr_drrr->drr_checksumtype =
407 drrw->drr_checksumtype;
408 wbr_drrr->drr_checksumflags =
409 drrw->drr_checksumtype;
410 wbr_drrr->drr_key.ddk_cksum =
411 drrw->drr_key.ddk_cksum;
412 wbr_drrr->drr_key.ddk_prop =
413 drrw->drr_key.ddk_prop;
415 if (dump_record(&wbr_drr, NULL, 0,
416 &stream_cksum, outfd) != 0)
419 /* block not previously seen */
420 if (dump_record(drr, buf, drrw->drr_length,
421 &stream_cksum, outfd) != 0)
427 case DRR_WRITE_EMBEDDED:
429 struct drr_write_embedded *drrwe =
430 &drr->drr_u.drr_write_embedded;
432 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
433 if (dump_record(drr, buf,
434 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
435 &stream_cksum, outfd) != 0)
442 if (dump_record(drr, NULL, 0, &stream_cksum,
449 (void) fprintf(stderr, "INVALID record type 0x%x\n",
451 /* should never happen, so assert */
456 umem_cache_destroy(ddt.ddecache);
457 free(ddt.dedup_hash_array);
465 * Routines for dealing with the AVL tree of fs-nvlists
467 typedef struct fsavl_node {
475 fsavl_compare(const void *arg1, const void *arg2)
477 const fsavl_node_t *fn1 = arg1;
478 const fsavl_node_t *fn2 = arg2;
480 if (fn1->fn_guid > fn2->fn_guid)
482 else if (fn1->fn_guid < fn2->fn_guid)
489 * Given the GUID of a snapshot, find its containing filesystem and
493 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
495 fsavl_node_t fn_find;
498 fn_find.fn_guid = snapguid;
500 fn = avl_find(avl, &fn_find, NULL);
503 *snapname = fn->fn_snapname;
504 return (fn->fn_nvfs);
510 fsavl_destroy(avl_tree_t *avl)
519 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
526 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
529 fsavl_create(nvlist_t *fss)
532 nvpair_t *fselem = NULL;
534 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
537 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
538 offsetof(fsavl_node_t, fn_node));
540 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
541 nvlist_t *nvfs, *snaps;
542 nvpair_t *snapelem = NULL;
544 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
545 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
548 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
552 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
553 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
554 fsavl_destroy(fsavl);
558 fn->fn_snapname = nvpair_name(snapelem);
562 * Note: if there are multiple snaps with the
563 * same GUID, we ignore all but one.
565 if (avl_find(fsavl, fn, NULL) == NULL)
576 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
578 typedef struct send_data {
579 uint64_t parent_fromsnap_guid;
580 nvlist_t *parent_snaps;
583 const char *fromsnap;
588 * The header nvlist is of the following format:
591 * "fromsnap" -> string (if incremental)
595 * "name" -> string (full name; for debugging)
596 * "parentfromsnap" -> number (guid of fromsnap in parent)
598 * "props" -> { name -> value (only if set here) }
599 * "snaps" -> { name (lastname) -> number (guid) }
600 * "snapprops" -> { name (lastname) -> { name -> value } }
602 * "origin" -> number (guid) (if clone)
603 * "sent" -> boolean (not on-disk)
611 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
614 send_iterate_snap(zfs_handle_t *zhp, void *arg)
616 send_data_t *sd = arg;
617 uint64_t guid = zhp->zfs_dmustats.dds_guid;
621 snapname = strrchr(zhp->zfs_name, '@')+1;
623 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
625 * NB: if there is no fromsnap here (it's a newly created fs in
626 * an incremental replication), we will substitute the tosnap.
628 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
629 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
630 strcmp(snapname, sd->tosnap) == 0)) {
631 sd->parent_fromsnap_guid = guid;
634 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
635 send_iterate_prop(zhp, nv);
636 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
644 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
646 nvpair_t *elem = NULL;
648 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
649 char *propname = nvpair_name(elem);
650 zfs_prop_t prop = zfs_name_to_prop(propname);
653 if (!zfs_prop_user(propname)) {
655 * Realistically, this should never happen. However,
656 * we want the ability to add DSL properties without
657 * needing to make incompatible version changes. We
658 * need to ignore unknown properties to allow older
659 * software to still send datasets containing these
660 * properties, with the unknown properties elided.
662 if (prop == ZPROP_INVAL)
665 if (zfs_prop_readonly(prop))
669 verify(nvpair_value_nvlist(elem, &propnv) == 0);
670 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
671 prop == ZFS_PROP_REFQUOTA ||
672 prop == ZFS_PROP_REFRESERVATION) {
675 verify(nvlist_lookup_uint64(propnv,
676 ZPROP_VALUE, &value) == 0);
677 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
680 * May have no source before SPA_VERSION_RECVD_PROPS,
681 * but is still modifiable.
683 if (nvlist_lookup_string(propnv,
684 ZPROP_SOURCE, &source) == 0) {
685 if ((strcmp(source, zhp->zfs_name) != 0) &&
687 ZPROP_SOURCE_VAL_RECVD) != 0))
692 if (nvlist_lookup_string(propnv,
693 ZPROP_SOURCE, &source) != 0)
695 if ((strcmp(source, zhp->zfs_name) != 0) &&
696 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
700 if (zfs_prop_user(propname) ||
701 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
703 verify(nvlist_lookup_string(propnv,
704 ZPROP_VALUE, &value) == 0);
705 VERIFY(0 == nvlist_add_string(nv, propname, value));
708 verify(nvlist_lookup_uint64(propnv,
709 ZPROP_VALUE, &value) == 0);
710 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
716 * recursively generate nvlists describing datasets. See comment
717 * for the data structure send_data_t above for description of contents
721 send_iterate_fs(zfs_handle_t *zhp, void *arg)
723 send_data_t *sd = arg;
726 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
727 uint64_t guid = zhp->zfs_dmustats.dds_guid;
730 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
731 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
732 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
733 sd->parent_fromsnap_guid));
735 if (zhp->zfs_dmustats.dds_origin[0]) {
736 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
737 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
740 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
741 origin->zfs_dmustats.dds_guid));
744 /* iterate over props */
745 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
746 send_iterate_prop(zhp, nv);
747 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
750 /* iterate over snaps, and set sd->parent_fromsnap_guid */
751 sd->parent_fromsnap_guid = 0;
752 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
753 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
754 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd);
755 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
756 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
757 nvlist_free(sd->parent_snaps);
758 nvlist_free(sd->snapprops);
760 /* add this fs to nvlist */
761 (void) snprintf(guidstring, sizeof (guidstring),
762 "0x%llx", (longlong_t)guid);
763 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
766 /* iterate over children */
768 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
770 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
777 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
778 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
781 send_data_t sd = { 0 };
784 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
786 return (EZFS_BADTYPE);
788 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
789 sd.fromsnap = fromsnap;
791 sd.recursive = recursive;
793 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
801 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
812 * Routines specific to "zfs send"
814 typedef struct send_dump_data {
815 /* these are all just the short snapname (the part after the @) */
816 const char *fromsnap;
818 char prevsnap[ZFS_MAXNAMELEN];
819 uint64_t prevsnap_obj;
820 boolean_t seenfrom, seento, replicate, doall, fromorigin;
821 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
822 boolean_t large_block;
828 snapfilter_cb_t *filter_cb;
831 char holdtag[ZFS_MAXNAMELEN];
837 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
838 boolean_t fromorigin, uint64_t *sizep)
840 zfs_cmd_t zc = { 0 };
841 libzfs_handle_t *hdl = zhp->zfs_hdl;
843 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
844 assert(fromsnap_obj == 0 || !fromorigin);
846 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
847 zc.zc_obj = fromorigin;
848 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
849 zc.zc_fromobj = fromsnap_obj;
850 zc.zc_guid = 1; /* estimate flag */
852 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
854 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
855 "warning: cannot estimate space for '%s'"), zhp->zfs_name);
859 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
860 "not an earlier snapshot from the same fs"));
861 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
864 if (zfs_dataset_exists(hdl, zc.zc_name,
865 ZFS_TYPE_SNAPSHOT)) {
866 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
867 "incremental source (@%s) does not exist"),
870 return (zfs_error(hdl, EZFS_NOENT, errbuf));
882 zfs_error_aux(hdl, strerror(errno));
883 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
886 return (zfs_standard_error(hdl, errno, errbuf));
890 *sizep = zc.zc_objset_type;
896 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
897 * NULL) to the file descriptor specified by outfd.
900 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
901 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
904 zfs_cmd_t zc = { 0 };
905 libzfs_handle_t *hdl = zhp->zfs_hdl;
908 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
909 assert(fromsnap_obj == 0 || !fromorigin);
911 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
912 zc.zc_cookie = outfd;
913 zc.zc_obj = fromorigin;
914 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
915 zc.zc_fromobj = fromsnap_obj;
918 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
919 if (fromsnap && fromsnap[0] != '\0') {
920 VERIFY(0 == nvlist_add_string(thisdbg,
921 "fromsnap", fromsnap));
924 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
926 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
927 "warning: cannot send '%s'"), zhp->zfs_name);
929 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
931 VERIFY(0 == nvlist_add_nvlist(debugnv,
932 zhp->zfs_name, thisdbg));
934 nvlist_free(thisdbg);
938 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
939 "not an earlier snapshot from the same fs"));
940 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
943 if (zfs_dataset_exists(hdl, zc.zc_name,
944 ZFS_TYPE_SNAPSHOT)) {
945 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
946 "incremental source (@%s) does not exist"),
949 return (zfs_error(hdl, EZFS_NOENT, errbuf));
964 zfs_error_aux(hdl, strerror(errno));
965 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
968 return (zfs_standard_error(hdl, errno, errbuf));
973 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
974 nvlist_free(thisdbg);
980 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
982 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
985 * zfs_send() only sets snapholds for sends that need them,
986 * e.g. replication and doall.
988 if (sdd->snapholds == NULL)
991 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
995 send_progress_thread(void *arg)
997 progress_arg_t *pa = arg;
999 zfs_cmd_t zc = { 0 };
1000 zfs_handle_t *zhp = pa->pa_zhp;
1001 libzfs_handle_t *hdl = zhp->zfs_hdl;
1002 unsigned long long bytes;
1008 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1009 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1011 if (!pa->pa_parsable)
1012 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1015 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1020 zc.zc_cookie = pa->pa_fd;
1021 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1022 return ((void *)-1);
1026 bytes = zc.zc_cookie;
1028 if (pa->pa_parsable) {
1029 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1030 tm->tm_hour, tm->tm_min, tm->tm_sec,
1031 bytes, zhp->zfs_name);
1033 zfs_nicenum(bytes, buf, sizeof (buf));
1034 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1035 tm->tm_hour, tm->tm_min, tm->tm_sec,
1036 buf, zhp->zfs_name);
1042 dump_snapshot(zfs_handle_t *zhp, void *arg)
1044 send_dump_data_t *sdd = arg;
1045 progress_arg_t pa = { 0 };
1049 boolean_t isfromsnap, istosnap, fromorigin;
1050 boolean_t exclude = B_FALSE;
1051 FILE *fout = sdd->std_out ? stdout : stderr;
1054 thissnap = strchr(zhp->zfs_name, '@') + 1;
1055 isfromsnap = (sdd->fromsnap != NULL &&
1056 strcmp(sdd->fromsnap, thissnap) == 0);
1058 if (!sdd->seenfrom && isfromsnap) {
1059 gather_holds(zhp, sdd);
1060 sdd->seenfrom = B_TRUE;
1061 (void) strcpy(sdd->prevsnap, thissnap);
1062 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1067 if (sdd->seento || !sdd->seenfrom) {
1072 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1074 sdd->seento = B_TRUE;
1076 if (!sdd->doall && !isfromsnap && !istosnap) {
1077 if (sdd->replicate) {
1079 nvlist_t *snapprops;
1081 * Filter out all intermediate snapshots except origin
1082 * snapshots needed to replicate clones.
1084 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1085 zhp->zfs_dmustats.dds_guid, &snapname);
1087 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1088 "snapprops", &snapprops));
1089 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1090 thissnap, &snapprops));
1091 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1098 * If a filter function exists, call it to determine whether
1099 * this snapshot will be sent.
1101 if (exclude || (sdd->filter_cb != NULL &&
1102 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1104 * This snapshot is filtered out. Don't send it, and don't
1105 * set prevsnap_obj, so it will be as if this snapshot didn't
1106 * exist, and the next accepted snapshot will be sent as
1107 * an incremental from the last accepted one, or as the
1108 * first (and full) snapshot in the case of a replication,
1109 * non-incremental send.
1115 gather_holds(zhp, sdd);
1116 fromorigin = sdd->prevsnap[0] == '\0' &&
1117 (sdd->fromorigin || sdd->replicate);
1121 err = estimate_ioctl(zhp, sdd->prevsnap_obj,
1124 if (sdd->parsable) {
1125 if (sdd->prevsnap[0] != '\0') {
1126 (void) fprintf(fout, "incremental\t%s\t%s",
1127 sdd->prevsnap, zhp->zfs_name);
1129 (void) fprintf(fout, "full\t%s",
1133 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1134 "send from @%s to %s"),
1135 sdd->prevsnap, zhp->zfs_name);
1138 if (sdd->parsable) {
1139 (void) fprintf(fout, "\t%llu\n",
1143 zfs_nicenum(size, buf, sizeof (buf));
1144 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1145 " estimated size is %s\n"), buf);
1149 (void) fprintf(fout, "\n");
1155 * If progress reporting is requested, spawn a new thread to
1156 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1158 if (sdd->progress) {
1160 pa.pa_fd = sdd->outfd;
1161 pa.pa_parsable = sdd->parsable;
1163 if (err = pthread_create(&tid, NULL,
1164 send_progress_thread, &pa)) {
1170 enum lzc_send_flags flags = 0;
1171 if (sdd->large_block)
1172 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1173 if (sdd->embed_data)
1174 flags |= LZC_SEND_FLAG_EMBED_DATA;
1176 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1177 fromorigin, sdd->outfd, flags, sdd->debugnv);
1179 if (sdd->progress) {
1180 (void) pthread_cancel(tid);
1181 (void) pthread_join(tid, NULL);
1185 (void) strcpy(sdd->prevsnap, thissnap);
1186 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1192 dump_filesystem(zfs_handle_t *zhp, void *arg)
1195 send_dump_data_t *sdd = arg;
1196 boolean_t missingfrom = B_FALSE;
1197 zfs_cmd_t zc = { 0 };
1199 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1200 zhp->zfs_name, sdd->tosnap);
1201 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1202 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1203 "WARNING: could not send %s@%s: does not exist\n"),
1204 zhp->zfs_name, sdd->tosnap);
1209 if (sdd->replicate && sdd->fromsnap) {
1211 * If this fs does not have fromsnap, and we're doing
1212 * recursive, we need to send a full stream from the
1213 * beginning (or an incremental from the origin if this
1214 * is a clone). If we're doing non-recursive, then let
1215 * them get the error.
1217 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1218 zhp->zfs_name, sdd->fromsnap);
1219 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1220 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1221 missingfrom = B_TRUE;
1225 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1226 sdd->prevsnap_obj = 0;
1227 if (sdd->fromsnap == NULL || missingfrom)
1228 sdd->seenfrom = B_TRUE;
1230 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1231 if (!sdd->seenfrom) {
1232 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1233 "WARNING: could not send %s@%s:\n"
1234 "incremental source (%s@%s) does not exist\n"),
1235 zhp->zfs_name, sdd->tosnap,
1236 zhp->zfs_name, sdd->fromsnap);
1238 } else if (!sdd->seento) {
1239 if (sdd->fromsnap) {
1240 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1241 "WARNING: could not send %s@%s:\n"
1242 "incremental source (%s@%s) "
1243 "is not earlier than it\n"),
1244 zhp->zfs_name, sdd->tosnap,
1245 zhp->zfs_name, sdd->fromsnap);
1247 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1249 "could not send %s@%s: does not exist\n"),
1250 zhp->zfs_name, sdd->tosnap);
1259 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1261 send_dump_data_t *sdd = arg;
1263 boolean_t needagain, progress;
1265 if (!sdd->replicate)
1266 return (dump_filesystem(rzhp, sdd));
1268 /* Mark the clone origin snapshots. */
1269 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1270 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1272 uint64_t origin_guid = 0;
1274 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1275 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1276 if (origin_guid != 0) {
1278 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1279 origin_guid, &snapname);
1280 if (origin_nv != NULL) {
1281 nvlist_t *snapprops;
1282 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1283 "snapprops", &snapprops));
1284 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1285 snapname, &snapprops));
1286 VERIFY(0 == nvlist_add_boolean(
1287 snapprops, "is_clone_origin"));
1292 needagain = progress = B_FALSE;
1293 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1294 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1295 nvlist_t *fslist, *parent_nv;
1299 uint64_t origin_guid = 0;
1300 uint64_t parent_guid = 0;
1302 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1303 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1306 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1307 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1308 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1311 if (parent_guid != 0) {
1312 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1313 if (!nvlist_exists(parent_nv, "sent")) {
1314 /* parent has not been sent; skip this one */
1320 if (origin_guid != 0) {
1321 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1323 if (origin_nv != NULL &&
1324 !nvlist_exists(origin_nv, "sent")) {
1326 * origin has not been sent yet;
1334 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1337 err = dump_filesystem(zhp, sdd);
1338 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1349 /* clean out the sent flags in case we reuse this fss */
1350 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1351 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1354 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1355 (void) nvlist_remove_all(fslist, "sent");
1362 * Generate a send stream for the dataset identified by the argument zhp.
1364 * The content of the send stream is the snapshot identified by
1365 * 'tosnap'. Incremental streams are requested in two ways:
1366 * - from the snapshot identified by "fromsnap" (if non-null) or
1367 * - from the origin of the dataset identified by zhp, which must
1368 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1371 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1372 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1373 * if "replicate" is set. If "doall" is set, dump all the intermediate
1374 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1375 * case too. If "props" is set, send properties.
1378 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1379 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1380 void *cb_arg, nvlist_t **debugnvp)
1383 send_dump_data_t sdd = { 0 };
1385 nvlist_t *fss = NULL;
1386 avl_tree_t *fsavl = NULL;
1387 static uint64_t holdseq;
1391 dedup_arg_t dda = { 0 };
1392 int featureflags = 0;
1395 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1396 "cannot send '%s'"), zhp->zfs_name);
1398 if (fromsnap && fromsnap[0] == '\0') {
1399 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1400 "zero-length incremental source"));
1401 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1404 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1406 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1407 if (version >= ZPL_VERSION_SA) {
1408 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1412 if (flags->dedup && !flags->dryrun) {
1413 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1414 DMU_BACKUP_FEATURE_DEDUPPROPS);
1415 if (err = pipe(pipefd)) {
1416 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1417 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1420 dda.outputfd = outfd;
1421 dda.inputfd = pipefd[1];
1422 dda.dedup_hdl = zhp->zfs_hdl;
1423 if (err = pthread_create(&tid, NULL, cksummer, &dda)) {
1424 (void) close(pipefd[0]);
1425 (void) close(pipefd[1]);
1426 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1427 return (zfs_error(zhp->zfs_hdl,
1428 EZFS_THREADCREATEFAILED, errbuf));
1432 if (flags->replicate || flags->doall || flags->props) {
1433 dmu_replay_record_t drr = { 0 };
1434 char *packbuf = NULL;
1436 zio_cksum_t zc = { 0 };
1438 if (flags->replicate || flags->props) {
1441 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1443 VERIFY(0 == nvlist_add_string(hdrnv,
1444 "fromsnap", fromsnap));
1446 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1447 if (!flags->replicate) {
1448 VERIFY(0 == nvlist_add_boolean(hdrnv,
1452 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1453 fromsnap, tosnap, flags->replicate, &fss, &fsavl);
1456 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1457 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1467 if (!flags->dryrun) {
1468 /* write first begin record */
1469 drr.drr_type = DRR_BEGIN;
1470 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1471 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1472 drr_versioninfo, DMU_COMPOUNDSTREAM);
1473 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1474 drr_versioninfo, featureflags);
1475 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1476 sizeof (drr.drr_u.drr_begin.drr_toname),
1477 "%s@%s", zhp->zfs_name, tosnap);
1478 drr.drr_payloadlen = buflen;
1480 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1485 /* write end record */
1486 bzero(&drr, sizeof (drr));
1487 drr.drr_type = DRR_END;
1488 drr.drr_u.drr_end.drr_checksum = zc;
1489 err = write(outfd, &drr, sizeof (drr));
1499 /* dump each stream */
1500 sdd.fromsnap = fromsnap;
1501 sdd.tosnap = tosnap;
1503 sdd.outfd = pipefd[0];
1506 sdd.replicate = flags->replicate;
1507 sdd.doall = flags->doall;
1508 sdd.fromorigin = flags->fromorigin;
1511 sdd.verbose = flags->verbose;
1512 sdd.parsable = flags->parsable;
1513 sdd.progress = flags->progress;
1514 sdd.dryrun = flags->dryrun;
1515 sdd.large_block = flags->largeblock;
1516 sdd.embed_data = flags->embed_data;
1517 sdd.filter_cb = filter_func;
1518 sdd.filter_cb_arg = cb_arg;
1520 sdd.debugnv = *debugnvp;
1521 if (sdd.verbose && sdd.dryrun)
1522 sdd.std_out = B_TRUE;
1523 fout = sdd.std_out ? stdout : stderr;
1526 * Some flags require that we place user holds on the datasets that are
1527 * being sent so they don't get destroyed during the send. We can skip
1528 * this step if the pool is imported read-only since the datasets cannot
1531 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1532 ZPOOL_PROP_READONLY, NULL) &&
1533 zfs_spa_version(zhp, &spa_version) == 0 &&
1534 spa_version >= SPA_VERSION_USERREFS &&
1535 (flags->doall || flags->replicate)) {
1537 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1538 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1539 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1540 if (sdd.cleanup_fd < 0) {
1544 sdd.snapholds = fnvlist_alloc();
1546 sdd.cleanup_fd = -1;
1547 sdd.snapholds = NULL;
1549 if (flags->verbose || sdd.snapholds != NULL) {
1551 * Do a verbose no-op dry run to get all the verbose output
1552 * or to gather snapshot hold's before generating any data,
1553 * then do a non-verbose real run to generate the streams.
1555 sdd.dryrun = B_TRUE;
1556 err = dump_filesystems(zhp, &sdd);
1561 if (flags->verbose) {
1562 if (flags->parsable) {
1563 (void) fprintf(fout, "size\t%llu\n",
1564 (longlong_t)sdd.size);
1567 zfs_nicenum(sdd.size, buf, sizeof (buf));
1568 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1569 "total estimated size is %s\n"), buf);
1573 /* Ensure no snaps found is treated as an error. */
1579 /* Skip the second run if dryrun was requested. */
1583 if (sdd.snapholds != NULL) {
1584 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
1588 fnvlist_free(sdd.snapholds);
1589 sdd.snapholds = NULL;
1592 sdd.dryrun = B_FALSE;
1593 sdd.verbose = B_FALSE;
1596 err = dump_filesystems(zhp, &sdd);
1597 fsavl_destroy(fsavl);
1600 /* Ensure no snaps found is treated as an error. */
1601 if (err == 0 && !sdd.seento)
1606 (void) pthread_cancel(tid);
1607 (void) close(pipefd[0]);
1608 (void) pthread_join(tid, NULL);
1611 if (sdd.cleanup_fd != -1) {
1612 VERIFY(0 == close(sdd.cleanup_fd));
1613 sdd.cleanup_fd = -1;
1616 if (!flags->dryrun && (flags->replicate || flags->doall ||
1619 * write final end record. NB: want to do this even if
1620 * there was some error, because it might not be totally
1623 dmu_replay_record_t drr = { 0 };
1624 drr.drr_type = DRR_END;
1625 if (write(outfd, &drr, sizeof (drr)) == -1) {
1626 return (zfs_standard_error(zhp->zfs_hdl,
1631 return (err || sdd.err);
1634 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1636 fsavl_destroy(fsavl);
1638 fnvlist_free(sdd.snapholds);
1640 if (sdd.cleanup_fd != -1)
1641 VERIFY(0 == close(sdd.cleanup_fd));
1643 (void) pthread_cancel(tid);
1644 (void) close(pipefd[0]);
1645 (void) pthread_join(tid, NULL);
1651 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
1652 enum lzc_send_flags flags)
1655 libzfs_handle_t *hdl = zhp->zfs_hdl;
1658 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1659 "warning: cannot send '%s'"), zhp->zfs_name);
1661 err = lzc_send(zhp->zfs_name, from, fd, flags);
1665 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1666 "not an earlier snapshot from the same fs"));
1667 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1671 if (lzc_exists(zhp->zfs_name)) {
1672 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1673 "incremental source (%s) does not exist"),
1676 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1679 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1680 "target is busy; if a filesystem, "
1681 "it must not be mounted"));
1682 return (zfs_error(hdl, EZFS_BUSY, errbuf));
1697 zfs_error_aux(hdl, strerror(errno));
1698 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1701 return (zfs_standard_error(hdl, errno, errbuf));
1708 * Routines specific to "zfs recv"
1712 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1713 boolean_t byteswap, zio_cksum_t *zc)
1719 assert(ilen <= SPA_MAXBLOCKSIZE);
1722 rv = read(fd, cp, len);
1727 if (rv < 0 || len != 0) {
1728 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1729 "failed to read from stream"));
1730 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1731 "cannot receive")));
1736 fletcher_4_incremental_byteswap(buf, ilen, zc);
1738 fletcher_4_incremental_native(buf, ilen, zc);
1744 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1745 boolean_t byteswap, zio_cksum_t *zc)
1750 buf = zfs_alloc(hdl, len);
1754 err = recv_read(hdl, fd, buf, len, byteswap, zc);
1760 err = nvlist_unpack(buf, len, nvp, 0);
1763 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1764 "stream (malformed nvlist)"));
1771 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
1772 int baselen, char *newname, recvflags_t *flags)
1775 zfs_cmd_t zc = { 0 };
1777 prop_changelist_t *clp;
1780 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1783 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1784 flags->force ? MS_FORCE : 0);
1788 err = changelist_prefix(clp);
1792 zc.zc_objset_type = DMU_OST_ZFS;
1793 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1796 (void) strcpy(newname, tryname);
1798 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
1800 if (flags->verbose) {
1801 (void) printf("attempting rename %s to %s\n",
1802 zc.zc_name, zc.zc_value);
1804 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1806 changelist_rename(clp, name, tryname);
1811 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
1814 (void) snprintf(newname, ZFS_MAXNAMELEN, "%.*srecv-%u-%u",
1815 baselen, name, getpid(), seq);
1816 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
1818 if (flags->verbose) {
1819 (void) printf("failed - trying rename %s to %s\n",
1820 zc.zc_name, zc.zc_value);
1822 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1824 changelist_rename(clp, name, newname);
1825 if (err && flags->verbose) {
1826 (void) printf("failed (%u) - "
1827 "will try again on next pass\n", errno);
1830 } else if (flags->verbose) {
1832 (void) printf("success\n");
1834 (void) printf("failed (%u)\n", errno);
1837 (void) changelist_postfix(clp);
1838 changelist_free(clp);
1844 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1845 char *newname, recvflags_t *flags)
1847 zfs_cmd_t zc = { 0 };
1849 prop_changelist_t *clp;
1851 boolean_t defer = B_FALSE;
1854 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1857 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1858 flags->force ? MS_FORCE : 0);
1859 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1860 zfs_spa_version(zhp, &spa_version) == 0 &&
1861 spa_version >= SPA_VERSION_USERREFS)
1866 err = changelist_prefix(clp);
1870 zc.zc_objset_type = DMU_OST_ZFS;
1871 zc.zc_defer_destroy = defer;
1872 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1875 (void) printf("attempting destroy %s\n", zc.zc_name);
1876 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1879 (void) printf("success\n");
1880 changelist_remove(clp, zc.zc_name);
1883 (void) changelist_postfix(clp);
1884 changelist_free(clp);
1887 * Deferred destroy might destroy the snapshot or only mark it to be
1888 * destroyed later, and it returns success in either case.
1890 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
1891 ZFS_TYPE_SNAPSHOT))) {
1892 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1898 typedef struct guid_to_name_data {
1902 } guid_to_name_data_t;
1905 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1907 guid_to_name_data_t *gtnd = arg;
1910 if (gtnd->skip != NULL &&
1911 strcmp(zhp->zfs_name, gtnd->skip) == 0) {
1915 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1916 (void) strcpy(gtnd->name, zhp->zfs_name);
1921 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1927 * Attempt to find the local dataset associated with this guid. In the case of
1928 * multiple matches, we attempt to find the "best" match by searching
1929 * progressively larger portions of the hierarchy. This allows one to send a
1930 * tree of datasets individually and guarantee that we will find the source
1931 * guid within that hierarchy, even if there are multiple matches elsewhere.
1934 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1937 /* exhaustive search all local snapshots */
1938 char pname[ZFS_MAXNAMELEN];
1939 guid_to_name_data_t gtnd;
1948 (void) strlcpy(pname, parent, sizeof (pname));
1951 * Search progressively larger portions of the hierarchy. This will
1952 * select the "most local" version of the origin snapshot in the case
1953 * that there are multiple matching snapshots in the system.
1955 while ((cp = strrchr(pname, '/')) != NULL) {
1957 /* Chop off the last component and open the parent */
1959 zhp = make_dataset_handle(hdl, pname);
1964 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1970 * Remember the dataset that we already searched, so we
1971 * skip it next time through.
1980 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1981 * guid1 is after guid2.
1984 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1985 uint64_t guid1, uint64_t guid2)
1988 char *fsname, *snapname;
1989 char buf[ZFS_MAXNAMELEN];
1991 zfs_handle_t *guid1hdl, *guid2hdl;
1992 uint64_t create1, create2;
1999 nvfs = fsavl_find(avl, guid1, &snapname);
2000 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2001 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2002 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2003 if (guid1hdl == NULL)
2006 nvfs = fsavl_find(avl, guid2, &snapname);
2007 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2008 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2009 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2010 if (guid2hdl == NULL) {
2011 zfs_close(guid1hdl);
2015 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2016 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2018 if (create1 < create2)
2020 else if (create1 > create2)
2025 zfs_close(guid1hdl);
2026 zfs_close(guid2hdl);
2032 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2033 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2036 nvlist_t *local_nv, *deleted = NULL;
2037 avl_tree_t *local_avl;
2038 nvpair_t *fselem, *nextfselem;
2040 char newname[ZFS_MAXNAMELEN];
2043 boolean_t needagain, progress, recursive;
2046 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2048 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2055 needagain = progress = B_FALSE;
2057 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2059 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2060 recursive, &local_nv, &local_avl)) != 0)
2064 * Process deletes and renames
2066 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2067 fselem; fselem = nextfselem) {
2068 nvlist_t *nvfs, *snaps;
2069 nvlist_t *stream_nvfs = NULL;
2070 nvpair_t *snapelem, *nextsnapelem;
2071 uint64_t fromguid = 0;
2072 uint64_t originguid = 0;
2073 uint64_t stream_originguid = 0;
2074 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2075 char *fsname, *stream_fsname;
2077 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2079 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2080 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2081 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2082 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2083 &parent_fromsnap_guid));
2084 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2087 * First find the stream's fs, so we can check for
2088 * a different origin (due to "zfs promote")
2090 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2091 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2094 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2095 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2097 if (stream_nvfs != NULL)
2101 /* check for promote */
2102 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2103 &stream_originguid);
2104 if (stream_nvfs && originguid != stream_originguid) {
2105 switch (created_before(hdl, local_avl,
2106 stream_originguid, originguid)) {
2109 zfs_cmd_t zc = { 0 };
2110 nvlist_t *origin_nvfs;
2111 char *origin_fsname;
2114 (void) printf("promoting %s\n", fsname);
2116 origin_nvfs = fsavl_find(local_avl, originguid,
2118 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2119 "name", &origin_fsname));
2120 (void) strlcpy(zc.zc_value, origin_fsname,
2121 sizeof (zc.zc_value));
2122 (void) strlcpy(zc.zc_name, fsname,
2123 sizeof (zc.zc_name));
2124 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2132 fsavl_destroy(local_avl);
2133 nvlist_free(local_nv);
2137 * We had/have the wrong origin, therefore our
2138 * list of snapshots is wrong. Need to handle
2139 * them on the next pass.
2145 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2146 snapelem; snapelem = nextsnapelem) {
2148 char *stream_snapname;
2149 nvlist_t *found, *props;
2151 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2153 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2154 found = fsavl_find(stream_avl, thisguid,
2157 /* check for delete */
2158 if (found == NULL) {
2159 char name[ZFS_MAXNAMELEN];
2164 (void) snprintf(name, sizeof (name), "%s@%s",
2165 fsname, nvpair_name(snapelem));
2167 error = recv_destroy(hdl, name,
2168 strlen(fsname)+1, newname, flags);
2173 sprintf(guidname, "%lu", thisguid);
2174 nvlist_add_boolean(deleted, guidname);
2178 stream_nvfs = found;
2180 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2181 &props) && 0 == nvlist_lookup_nvlist(props,
2182 stream_snapname, &props)) {
2183 zfs_cmd_t zc = { 0 };
2185 zc.zc_cookie = B_TRUE; /* received */
2186 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2187 "%s@%s", fsname, nvpair_name(snapelem));
2188 if (zcmd_write_src_nvlist(hdl, &zc,
2190 (void) zfs_ioctl(hdl,
2191 ZFS_IOC_SET_PROP, &zc);
2192 zcmd_free_nvlists(&zc);
2196 /* check for different snapname */
2197 if (strcmp(nvpair_name(snapelem),
2198 stream_snapname) != 0) {
2199 char name[ZFS_MAXNAMELEN];
2200 char tryname[ZFS_MAXNAMELEN];
2202 (void) snprintf(name, sizeof (name), "%s@%s",
2203 fsname, nvpair_name(snapelem));
2204 (void) snprintf(tryname, sizeof (name), "%s@%s",
2205 fsname, stream_snapname);
2207 error = recv_rename(hdl, name, tryname,
2208 strlen(fsname)+1, newname, flags);
2215 if (strcmp(stream_snapname, fromsnap) == 0)
2216 fromguid = thisguid;
2219 /* check for delete */
2220 if (stream_nvfs == NULL) {
2224 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2230 sprintf(guidname, "%lu", parent_fromsnap_guid);
2231 nvlist_add_boolean(deleted, guidname);
2235 if (fromguid == 0) {
2236 if (flags->verbose) {
2237 (void) printf("local fs %s does not have "
2238 "fromsnap (%s in stream); must have "
2239 "been deleted locally; ignoring\n",
2245 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2246 "name", &stream_fsname));
2247 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2248 "parentfromsnap", &stream_parent_fromsnap_guid));
2250 s1 = strrchr(fsname, '/');
2251 s2 = strrchr(stream_fsname, '/');
2254 * Check if we're going to rename based on parent guid change
2255 * and the current parent guid was also deleted. If it was then
2256 * rename will fail and is likely unneeded, so avoid this and
2257 * force an early retry to determine the new
2258 * parent_fromsnap_guid.
2260 if (stream_parent_fromsnap_guid != 0 &&
2261 parent_fromsnap_guid != 0 &&
2262 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
2263 sprintf(guidname, "%lu", parent_fromsnap_guid);
2264 if (nvlist_exists(deleted, guidname)) {
2272 * Check for rename. If the exact receive path is specified, it
2273 * does not count as a rename, but we still need to check the
2274 * datasets beneath it.
2276 if ((stream_parent_fromsnap_guid != 0 &&
2277 parent_fromsnap_guid != 0 &&
2278 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2279 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2280 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2282 char tryname[ZFS_MAXNAMELEN];
2284 parent = fsavl_find(local_avl,
2285 stream_parent_fromsnap_guid, NULL);
2287 * NB: parent might not be found if we used the
2288 * tosnap for stream_parent_fromsnap_guid,
2289 * because the parent is a newly-created fs;
2290 * we'll be able to rename it after we recv the
2293 if (parent != NULL) {
2296 VERIFY(0 == nvlist_lookup_string(parent, "name",
2298 (void) snprintf(tryname, sizeof (tryname),
2299 "%s%s", pname, strrchr(stream_fsname, '/'));
2302 if (flags->verbose) {
2303 (void) printf("local fs %s new parent "
2304 "not found\n", fsname);
2310 error = recv_rename(hdl, fsname, tryname,
2311 strlen(tofs)+1, newname, flags);
2313 if (renamed != NULL && newname[0] != '\0') {
2314 VERIFY(0 == nvlist_add_boolean(renamed,
2326 fsavl_destroy(local_avl);
2327 nvlist_free(local_nv);
2328 nvlist_free(deleted);
2330 if (needagain && progress) {
2331 /* do another pass to fix up temporary names */
2333 (void) printf("another pass:\n");
2341 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2342 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2343 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2345 nvlist_t *stream_nv = NULL;
2346 avl_tree_t *stream_avl = NULL;
2347 char *fromsnap = NULL;
2349 char tofs[ZFS_MAXNAMELEN];
2350 char sendfs[ZFS_MAXNAMELEN];
2352 dmu_replay_record_t drre;
2354 boolean_t anyerr = B_FALSE;
2355 boolean_t softerr = B_FALSE;
2356 boolean_t recursive;
2358 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2361 assert(drr->drr_type == DRR_BEGIN);
2362 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2363 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2364 DMU_COMPOUNDSTREAM);
2367 * Read in the nvlist from the stream.
2369 if (drr->drr_payloadlen != 0) {
2370 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2371 &stream_nv, flags->byteswap, zc);
2373 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2378 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2381 if (recursive && strchr(destname, '@')) {
2382 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2383 "cannot specify snapshot name for multi-snapshot stream"));
2384 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2389 * Read in the end record and verify checksum.
2391 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2392 flags->byteswap, NULL)))
2394 if (flags->byteswap) {
2395 drre.drr_type = BSWAP_32(drre.drr_type);
2396 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2397 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2398 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2399 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2400 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2401 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2402 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2403 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2405 if (drre.drr_type != DRR_END) {
2406 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2409 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2410 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2411 "incorrect header checksum"));
2412 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2416 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2418 if (drr->drr_payloadlen != 0) {
2419 nvlist_t *stream_fss;
2421 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2423 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2424 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2425 "couldn't allocate avl tree"));
2426 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2430 if (fromsnap != NULL) {
2431 nvlist_t *renamed = NULL;
2432 nvpair_t *pair = NULL;
2434 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2435 if (flags->isprefix) {
2436 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2439 if (flags->istail) {
2440 cp = strrchr(drrb->drr_toname, '/');
2442 (void) strlcat(tofs, "/",
2446 i = (cp - drrb->drr_toname);
2449 i = strcspn(drrb->drr_toname, "/@");
2451 /* zfs_receive_one() will create_parents() */
2452 (void) strlcat(tofs, &drrb->drr_toname[i],
2454 *strchr(tofs, '@') = '\0';
2457 if (recursive && !flags->dryrun && !flags->nomount) {
2458 VERIFY(0 == nvlist_alloc(&renamed,
2459 NV_UNIQUE_NAME, 0));
2462 softerr = recv_incremental_replication(hdl, tofs, flags,
2463 stream_nv, stream_avl, renamed);
2465 /* Unmount renamed filesystems before receiving. */
2466 while ((pair = nvlist_next_nvpair(renamed,
2469 prop_changelist_t *clp = NULL;
2471 zhp = zfs_open(hdl, nvpair_name(pair),
2472 ZFS_TYPE_FILESYSTEM);
2474 clp = changelist_gather(zhp,
2475 ZFS_PROP_MOUNTPOINT, 0, 0);
2479 changelist_prefix(clp);
2480 changelist_free(clp);
2485 nvlist_free(renamed);
2490 * Get the fs specified by the first path in the stream (the top level
2491 * specified by 'zfs send') and pass it to each invocation of
2492 * zfs_receive_one().
2494 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2496 if ((cp = strchr(sendfs, '@')) != NULL)
2499 /* Finally, receive each contained stream */
2502 * we should figure out if it has a recoverable
2503 * error, in which case do a recv_skip() and drive on.
2504 * Note, if we fail due to already having this guid,
2505 * zfs_receive_one() will take care of it (ie,
2506 * recv_skip() and return 0).
2508 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2509 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2511 if (error == ENODATA) {
2516 } while (error == 0);
2518 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2520 * Now that we have the fs's they sent us, try the
2523 softerr = recv_incremental_replication(hdl, tofs, flags,
2524 stream_nv, stream_avl, NULL);
2528 fsavl_destroy(stream_avl);
2530 nvlist_free(stream_nv);
2539 trunc_prop_errs(int truncated)
2541 ASSERT(truncated != 0);
2544 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2545 "1 more property could not be set\n"));
2547 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2548 "%d more properties could not be set\n"), truncated);
2552 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2554 dmu_replay_record_t *drr;
2555 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
2558 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2559 "cannot receive:"));
2561 /* XXX would be great to use lseek if possible... */
2564 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2565 byteswap, NULL) == 0) {
2567 drr->drr_type = BSWAP_32(drr->drr_type);
2569 switch (drr->drr_type) {
2571 /* NB: not to be used on v2 stream packages */
2572 if (drr->drr_payloadlen != 0) {
2573 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2574 "invalid substream header"));
2575 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2585 drr->drr_u.drr_object.drr_bonuslen =
2586 BSWAP_32(drr->drr_u.drr_object.
2589 (void) recv_read(hdl, fd, buf,
2590 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2596 drr->drr_u.drr_write.drr_length =
2597 BSWAP_64(drr->drr_u.drr_write.drr_length);
2599 (void) recv_read(hdl, fd, buf,
2600 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2604 drr->drr_u.drr_write.drr_length =
2605 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2607 (void) recv_read(hdl, fd, buf,
2608 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2610 case DRR_WRITE_EMBEDDED:
2612 drr->drr_u.drr_write_embedded.drr_psize =
2613 BSWAP_32(drr->drr_u.drr_write_embedded.
2616 (void) recv_read(hdl, fd, buf,
2617 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
2620 case DRR_WRITE_BYREF:
2621 case DRR_FREEOBJECTS:
2626 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2627 "invalid record type"));
2628 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2637 * Restores a backup of tosnap from the file descriptor specified by infd.
2640 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2641 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
2642 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
2643 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2644 uint64_t *action_handlep)
2646 zfs_cmd_t zc = { 0 };
2648 int ioctl_err, ioctl_errno, err;
2650 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2652 char prop_errbuf[1024];
2653 const char *chopprefix;
2654 boolean_t newfs = B_FALSE;
2655 boolean_t stream_wantsnewfs;
2656 uint64_t parent_snapguid = 0;
2657 prop_changelist_t *clp = NULL;
2658 nvlist_t *snapprops_nvlist = NULL;
2659 zprop_errflags_t prop_errflags;
2660 boolean_t recursive;
2662 begin_time = time(NULL);
2664 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2667 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2670 if (stream_avl != NULL) {
2672 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2677 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2679 err = nvlist_lookup_nvlist(fs, "props", &props);
2681 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2683 if (flags->canmountoff) {
2684 VERIFY(0 == nvlist_add_uint64(props,
2685 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2687 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2691 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2692 VERIFY(0 == nvlist_lookup_nvlist(props,
2693 snapname, &snapprops_nvlist));
2703 * Determine how much of the snapshot name stored in the stream
2704 * we are going to tack on to the name they specified on the
2705 * command line, and how much we are going to chop off.
2707 * If they specified a snapshot, chop the entire name stored in
2710 if (flags->istail) {
2712 * A filesystem was specified with -e. We want to tack on only
2713 * the tail of the sent snapshot path.
2715 if (strchr(tosnap, '@')) {
2716 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2717 "argument - snapshot not allowed with -e"));
2718 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2721 chopprefix = strrchr(sendfs, '/');
2723 if (chopprefix == NULL) {
2725 * The tail is the poolname, so we need to
2726 * prepend a path separator.
2728 int len = strlen(drrb->drr_toname);
2729 cp = malloc(len + 2);
2731 (void) strcpy(&cp[1], drrb->drr_toname);
2734 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2736 } else if (flags->isprefix) {
2738 * A filesystem was specified with -d. We want to tack on
2739 * everything but the first element of the sent snapshot path
2740 * (all but the pool name).
2742 if (strchr(tosnap, '@')) {
2743 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2744 "argument - snapshot not allowed with -d"));
2745 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2748 chopprefix = strchr(drrb->drr_toname, '/');
2749 if (chopprefix == NULL)
2750 chopprefix = strchr(drrb->drr_toname, '@');
2751 } else if (strchr(tosnap, '@') == NULL) {
2753 * If a filesystem was specified without -d or -e, we want to
2754 * tack on everything after the fs specified by 'zfs send'.
2756 chopprefix = drrb->drr_toname + strlen(sendfs);
2758 /* A snapshot was specified as an exact path (no -d or -e). */
2760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2761 "cannot specify snapshot name for multi-snapshot "
2763 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2765 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
2768 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
2769 ASSERT(chopprefix > drrb->drr_toname);
2770 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
2771 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
2772 chopprefix[0] == '\0');
2775 * Determine name of destination snapshot, store in zc_value.
2777 (void) strcpy(zc.zc_value, tosnap);
2778 (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
2780 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
2781 zfs_ioctl_version = get_zfs_ioctl_version();
2783 * For forward compatibility hide tosnap in zc_value
2785 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
2786 (void) strcpy(zc.zc_value + strlen(zc.zc_value) + 1, tosnap);
2789 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
2790 zcmd_free_nvlists(&zc);
2791 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2795 * Determine the name of the origin snapshot, store in zc_string.
2797 if (drrb->drr_flags & DRR_FLAG_CLONE) {
2798 if (guid_to_name(hdl, zc.zc_value,
2799 drrb->drr_fromguid, zc.zc_string) != 0) {
2800 zcmd_free_nvlists(&zc);
2801 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2802 "local origin for clone %s does not exist"),
2804 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2807 (void) printf("found clone origin %s\n", zc.zc_string);
2808 } else if (originsnap) {
2809 (void) strncpy(zc.zc_string, originsnap, ZFS_MAXNAMELEN);
2811 (void) printf("using provided clone origin %s\n",
2815 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
2816 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap);
2818 if (stream_wantsnewfs) {
2820 * if the parent fs does not exist, look for it based on
2821 * the parent snap GUID
2823 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2824 "cannot receive new filesystem stream"));
2826 (void) strcpy(zc.zc_name, zc.zc_value);
2827 cp = strrchr(zc.zc_name, '/');
2831 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2832 char suffix[ZFS_MAXNAMELEN];
2833 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
2834 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
2835 zc.zc_value) == 0) {
2836 *strchr(zc.zc_value, '@') = '\0';
2837 (void) strcat(zc.zc_value, suffix);
2842 * if the fs does not exist, look for it based on the
2845 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2846 "cannot receive incremental stream"));
2848 (void) strcpy(zc.zc_name, zc.zc_value);
2849 *strchr(zc.zc_name, '@') = '\0';
2852 * If the exact receive path was specified and this is the
2853 * topmost path in the stream, then if the fs does not exist we
2854 * should look no further.
2856 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
2857 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
2858 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2859 char snap[ZFS_MAXNAMELEN];
2860 (void) strcpy(snap, strchr(zc.zc_value, '@'));
2861 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
2862 zc.zc_value) == 0) {
2863 *strchr(zc.zc_value, '@') = '\0';
2864 (void) strcat(zc.zc_value, snap);
2869 (void) strcpy(zc.zc_name, zc.zc_value);
2870 *strchr(zc.zc_name, '@') = '\0';
2872 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2876 * Destination fs exists. Therefore this should either
2877 * be an incremental, or the stream specifies a new fs
2878 * (full stream or clone) and they want us to blow it
2879 * away (and have therefore specified -F and removed any
2882 if (stream_wantsnewfs) {
2883 if (!flags->force) {
2884 zcmd_free_nvlists(&zc);
2885 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2886 "destination '%s' exists\n"
2887 "must specify -F to overwrite it"),
2889 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2891 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2893 zcmd_free_nvlists(&zc);
2894 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2895 "destination has snapshots (eg. %s)\n"
2896 "must destroy them to overwrite it"),
2898 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2902 if ((zhp = zfs_open(hdl, zc.zc_name,
2903 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2904 zcmd_free_nvlists(&zc);
2908 if (stream_wantsnewfs &&
2909 zhp->zfs_dmustats.dds_origin[0]) {
2910 zcmd_free_nvlists(&zc);
2912 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2913 "destination '%s' is a clone\n"
2914 "must destroy it to overwrite it"),
2916 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2919 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
2920 stream_wantsnewfs) {
2921 /* We can't do online recv in this case */
2922 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
2925 zcmd_free_nvlists(&zc);
2928 if (changelist_prefix(clp) != 0) {
2929 changelist_free(clp);
2931 zcmd_free_nvlists(&zc);
2938 * Destination filesystem does not exist. Therefore we better
2939 * be creating a new filesystem (either from a full backup, or
2940 * a clone). It would therefore be invalid if the user
2941 * specified only the pool name (i.e. if the destination name
2942 * contained no slash character).
2944 if (!stream_wantsnewfs ||
2945 (cp = strrchr(zc.zc_name, '/')) == NULL) {
2946 zcmd_free_nvlists(&zc);
2947 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2948 "destination '%s' does not exist"), zc.zc_name);
2949 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2953 * Trim off the final dataset component so we perform the
2954 * recvbackup ioctl to the filesystems's parent.
2958 if (flags->isprefix && !flags->istail && !flags->dryrun &&
2959 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
2960 zcmd_free_nvlists(&zc);
2961 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
2967 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
2968 zc.zc_cookie = infd;
2969 zc.zc_guid = flags->force;
2970 if (flags->verbose) {
2971 (void) printf("%s %s stream of %s into %s\n",
2972 flags->dryrun ? "would receive" : "receiving",
2973 drrb->drr_fromguid ? "incremental" : "full",
2974 drrb->drr_toname, zc.zc_value);
2975 (void) fflush(stdout);
2978 if (flags->dryrun) {
2979 zcmd_free_nvlists(&zc);
2980 return (recv_skip(hdl, infd, flags->byteswap));
2983 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
2984 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
2985 zc.zc_cleanup_fd = cleanup_fd;
2986 zc.zc_action_handle = *action_handlep;
2988 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
2989 ioctl_errno = errno;
2990 prop_errflags = (zprop_errflags_t)zc.zc_obj;
2993 nvlist_t *prop_errors;
2994 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
2995 zc.zc_nvlist_dst_size, &prop_errors, 0));
2997 nvpair_t *prop_err = NULL;
2999 while ((prop_err = nvlist_next_nvpair(prop_errors,
3000 prop_err)) != NULL) {
3005 prop = zfs_name_to_prop(nvpair_name(prop_err));
3006 (void) nvpair_value_int32(prop_err, &intval);
3007 if (strcmp(nvpair_name(prop_err),
3008 ZPROP_N_MORE_ERRORS) == 0) {
3009 trunc_prop_errs(intval);
3012 (void) snprintf(tbuf, sizeof (tbuf),
3013 dgettext(TEXT_DOMAIN,
3014 "cannot receive %s property on %s"),
3015 nvpair_name(prop_err), zc.zc_name);
3016 zfs_setprop_error(hdl, prop, intval, tbuf);
3019 nvlist_free(prop_errors);
3022 zc.zc_nvlist_dst = 0;
3023 zc.zc_nvlist_dst_size = 0;
3024 zcmd_free_nvlists(&zc);
3026 if (err == 0 && snapprops_nvlist) {
3027 zfs_cmd_t zc2 = { 0 };
3029 (void) strcpy(zc2.zc_name, zc.zc_value);
3030 zc2.zc_cookie = B_TRUE; /* received */
3031 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
3032 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
3033 zcmd_free_nvlists(&zc2);
3037 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3039 * It may be that this snapshot already exists,
3040 * in which case we want to consume & ignore it
3041 * rather than failing.
3043 avl_tree_t *local_avl;
3044 nvlist_t *local_nv, *fs;
3045 cp = strchr(zc.zc_value, '@');
3048 * XXX Do this faster by just iterating over snaps in
3049 * this fs. Also if zc_value does not exist, we will
3050 * get a strange "does not exist" error message.
3053 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3054 &local_nv, &local_avl) == 0) {
3056 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
3057 fsavl_destroy(local_avl);
3058 nvlist_free(local_nv);
3061 if (flags->verbose) {
3062 (void) printf("snap %s already exists; "
3063 "ignoring\n", zc.zc_value);
3065 err = ioctl_err = recv_skip(hdl, infd,
3072 if (ioctl_err != 0) {
3073 switch (ioctl_errno) {
3075 cp = strchr(zc.zc_value, '@');
3077 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3078 "most recent snapshot of %s does not\n"
3079 "match incremental source"), zc.zc_value);
3080 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3084 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3085 "destination %s has been modified\n"
3086 "since most recent snapshot"), zc.zc_name);
3087 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3090 cp = strchr(zc.zc_value, '@');
3092 /* it's the containing fs that exists */
3095 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3096 "destination already exists"));
3097 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
3098 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
3103 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3106 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3107 "invalid stream (checksum mismatch)"));
3108 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3111 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3112 "pool must be upgraded to receive this stream."));
3113 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3116 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3117 "destination %s space quota exceeded"), zc.zc_name);
3118 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3121 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3126 * Mount the target filesystem (if created). Also mount any
3127 * children of the target filesystem if we did a replication
3128 * receive (indicated by stream_avl being non-NULL).
3130 cp = strchr(zc.zc_value, '@');
3131 if (cp && (ioctl_err == 0 || !newfs)) {
3135 h = zfs_open(hdl, zc.zc_value,
3136 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3138 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3140 } else if (newfs || stream_avl) {
3142 * Track the first/top of hierarchy fs,
3143 * for mounting and sharing later.
3145 if (top_zfs && *top_zfs == NULL)
3146 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3154 err |= changelist_postfix(clp);
3155 changelist_free(clp);
3158 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3159 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3160 "failed to clear unreceived properties on %s"),
3162 (void) fprintf(stderr, "\n");
3164 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3165 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3166 "failed to restore original properties on %s"),
3168 (void) fprintf(stderr, "\n");
3171 if (err || ioctl_err)
3174 *action_handlep = zc.zc_action_handle;
3176 if (flags->verbose) {
3179 uint64_t bytes = zc.zc_cookie;
3180 time_t delta = time(NULL) - begin_time;
3183 zfs_nicenum(bytes, buf1, sizeof (buf1));
3184 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3186 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3194 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3195 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
3196 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3197 uint64_t *action_handlep)
3200 dmu_replay_record_t drr, drr_noswap;
3201 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3203 zio_cksum_t zcksum = { 0 };
3204 uint64_t featureflags;
3207 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3210 if (flags->isprefix &&
3211 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3212 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3213 "(%s) does not exist"), tosnap);
3214 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3217 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
3218 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
3219 "(%s) does not exist"), originsnap);
3220 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3223 /* read in the BEGIN record */
3224 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3228 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3229 /* It's the double end record at the end of a package */
3233 /* the kernel needs the non-byteswapped begin record */
3236 flags->byteswap = B_FALSE;
3237 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3239 * We computed the checksum in the wrong byteorder in
3240 * recv_read() above; do it again correctly.
3242 bzero(&zcksum, sizeof (zio_cksum_t));
3243 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
3244 flags->byteswap = B_TRUE;
3246 drr.drr_type = BSWAP_32(drr.drr_type);
3247 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3248 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3249 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3250 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3251 drrb->drr_type = BSWAP_32(drrb->drr_type);
3252 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3253 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3254 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3257 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3258 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3259 "stream (bad magic number)"));
3260 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3263 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3264 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3266 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3267 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3268 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3269 "stream has unsupported feature, feature flags = %lx"),
3271 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3274 if (strchr(drrb->drr_toname, '@') == NULL) {
3275 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3276 "stream (bad snapshot name)"));
3277 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3280 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3281 char nonpackage_sendfs[ZFS_MAXNAMELEN];
3282 if (sendfs == NULL) {
3284 * We were not called from zfs_receive_package(). Get
3285 * the fs specified by 'zfs send'.
3288 (void) strlcpy(nonpackage_sendfs,
3289 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
3290 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3292 sendfs = nonpackage_sendfs;
3294 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3295 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
3296 cleanup_fd, action_handlep));
3298 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3299 DMU_COMPOUNDSTREAM);
3300 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
3301 &zcksum, top_zfs, cleanup_fd, action_handlep));
3306 * Restores a backup of tosnap from the file descriptor specified by infd.
3307 * Return 0 on total success, -2 if some things couldn't be
3308 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3309 * (-1 will override -2).
3312 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
3313 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
3315 char *top_zfs = NULL;
3318 uint64_t action_handle = 0;
3319 char *originsnap = NULL;
3321 err = nvlist_lookup_string(props, "origin", &originsnap);
3322 if (err && err != ENOENT)
3326 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3327 VERIFY(cleanup_fd >= 0);
3329 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
3330 stream_avl, &top_zfs, cleanup_fd, &action_handle);
3332 VERIFY(0 == close(cleanup_fd));
3334 if (err == 0 && !flags->nomount && top_zfs) {
3336 prop_changelist_t *clp;
3338 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3340 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3341 CL_GATHER_MOUNT_ALWAYS, 0);
3344 /* mount and share received datasets */
3345 err = changelist_postfix(clp);
3346 changelist_free(clp);
3349 if (zhp == NULL || clp == NULL || err)