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]
42 #include <sys/param.h>
43 #include <sys/mount.h>
49 #include <libzfs_core.h>
51 #include "zfs_namecheck.h"
53 #include "zfs_fletcher.h"
54 #include "libzfs_impl.h"
57 #include <sys/zio_checksum.h>
61 extern int zfs_ioctl_version;
64 /* in libzfs_dataset.c */
65 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
66 /* We need to use something for ENODATA. */
69 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
70 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
71 uint64_t *, const char *);
72 static int guid_to_name(libzfs_handle_t *, const char *,
73 uint64_t, boolean_t, char *);
75 static const zio_cksum_t zero_cksum = { 0 };
77 typedef struct dedup_arg {
80 libzfs_handle_t *dedup_hdl;
83 typedef struct progress_arg {
86 boolean_t pa_parsable;
89 typedef struct dataref {
95 typedef struct dedup_entry {
96 struct dedup_entry *dde_next;
97 zio_cksum_t dde_chksum;
102 #define MAX_DDT_PHYSMEM_PERCENT 20
103 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
105 typedef struct dedup_table {
106 dedup_entry_t **dedup_hash_array;
107 umem_cache_t *ddecache;
108 uint64_t max_ddt_size; /* max dedup table size in bytes */
109 uint64_t cur_ddt_size; /* current dedup table size in bytes */
116 high_order_bit(uint64_t n)
120 for (count = 0; n != 0; count++)
126 ssread(void *buf, size_t len, FILE *stream)
130 if ((outlen = fread(buf, len, 1, stream)) == 0)
137 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
138 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
142 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
143 if (ddt->ddt_full == B_FALSE) {
144 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
145 "Dedup table full. Deduplication will continue "
146 "with existing table entries"));
147 ddt->ddt_full = B_TRUE;
152 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
154 assert(*ddepp == NULL);
155 dde->dde_next = NULL;
156 dde->dde_chksum = *cs;
157 dde->dde_prop = prop;
160 ddt->cur_ddt_size += sizeof (dedup_entry_t);
166 * Using the specified dedup table, do a lookup for an entry with
167 * the checksum cs. If found, return the block's reference info
168 * in *dr. Otherwise, insert a new entry in the dedup table, using
169 * the reference information specified by *dr.
171 * return value: true - entry was found
172 * false - entry was not found
175 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
176 uint64_t prop, dataref_t *dr)
179 dedup_entry_t **ddepp;
181 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
183 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
184 ddepp = &((*ddepp)->dde_next)) {
185 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
186 (*ddepp)->dde_prop == prop) {
187 *dr = (*ddepp)->dde_ref;
191 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
196 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
197 zio_cksum_t *zc, int outfd)
199 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
200 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
201 fletcher_4_incremental_native(drr,
202 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
203 if (drr->drr_type != DRR_BEGIN) {
204 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
205 drr_checksum.drr_checksum));
206 drr->drr_u.drr_checksum.drr_checksum = *zc;
208 fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
209 sizeof (zio_cksum_t), zc);
210 if (write(outfd, drr, sizeof (*drr)) == -1)
212 if (payload_len != 0) {
213 fletcher_4_incremental_native(payload, payload_len, zc);
214 if (write(outfd, payload, payload_len) == -1)
221 * This function is started in a separate thread when the dedup option
222 * has been requested. The main send thread determines the list of
223 * snapshots to be included in the send stream and makes the ioctl calls
224 * for each one. But instead of having the ioctl send the output to the
225 * the output fd specified by the caller of zfs_send()), the
226 * ioctl is told to direct the output to a pipe, which is read by the
227 * alternate thread running THIS function. This function does the
229 * 1. building a dedup table (the DDT)
230 * 2. doing checksums on each data block and inserting a record in the DDT
231 * 3. looking for matching checksums, and
232 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
233 * a duplicate block is found.
234 * The output of this function then goes to the output fd requested
235 * by the caller of zfs_send().
240 dedup_arg_t *dda = arg;
241 char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
242 dmu_replay_record_t thedrr;
243 dmu_replay_record_t *drr = &thedrr;
247 zio_cksum_t stream_cksum;
248 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
252 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
253 SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
255 numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
258 * numbuckets must be a power of 2. Increase number to
259 * a power of 2 if necessary.
261 if (!ISP2(numbuckets))
262 numbuckets = 1 << high_order_bit(numbuckets);
264 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
265 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
266 NULL, NULL, NULL, NULL, NULL, 0);
267 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
268 ddt.numhashbits = high_order_bit(numbuckets) - 1;
269 ddt.ddt_full = B_FALSE;
271 outfd = dda->outputfd;
272 ofp = fdopen(dda->inputfd, "r");
273 while (ssread(drr, sizeof (*drr), ofp) != 0) {
275 switch (drr->drr_type) {
278 struct drr_begin *drrb = &drr->drr_u.drr_begin;
281 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
283 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
285 /* set the DEDUP feature flag for this stream */
286 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
287 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
288 DMU_BACKUP_FEATURE_DEDUPPROPS);
289 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
291 if (drr->drr_payloadlen != 0) {
292 sz = drr->drr_payloadlen;
294 if (sz > SPA_MAXBLOCKSIZE) {
295 buf = zfs_realloc(dda->dedup_hdl, buf,
296 SPA_MAXBLOCKSIZE, sz);
298 (void) ssread(buf, sz, ofp);
302 if (dump_record(drr, buf, sz, &stream_cksum,
310 struct drr_end *drre = &drr->drr_u.drr_end;
311 /* use the recalculated checksum */
312 drre->drr_checksum = stream_cksum;
313 if (dump_record(drr, NULL, 0, &stream_cksum,
321 struct drr_object *drro = &drr->drr_u.drr_object;
322 if (drro->drr_bonuslen > 0) {
324 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
327 if (dump_record(drr, buf,
328 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
329 &stream_cksum, outfd) != 0)
336 struct drr_spill *drrs = &drr->drr_u.drr_spill;
337 (void) ssread(buf, drrs->drr_length, ofp);
338 if (dump_record(drr, buf, drrs->drr_length,
339 &stream_cksum, outfd) != 0)
344 case DRR_FREEOBJECTS:
346 if (dump_record(drr, NULL, 0, &stream_cksum,
354 struct drr_write *drrw = &drr->drr_u.drr_write;
357 (void) ssread(buf, drrw->drr_length, ofp);
360 * Use the existing checksum if it's dedup-capable,
361 * else calculate a SHA256 checksum for it.
364 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
366 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
368 zio_cksum_t tmpsha256;
371 SHA256Update(&ctx, buf, drrw->drr_length);
372 SHA256Final(&tmpsha256, &ctx);
373 drrw->drr_key.ddk_cksum.zc_word[0] =
374 BE_64(tmpsha256.zc_word[0]);
375 drrw->drr_key.ddk_cksum.zc_word[1] =
376 BE_64(tmpsha256.zc_word[1]);
377 drrw->drr_key.ddk_cksum.zc_word[2] =
378 BE_64(tmpsha256.zc_word[2]);
379 drrw->drr_key.ddk_cksum.zc_word[3] =
380 BE_64(tmpsha256.zc_word[3]);
381 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
382 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
385 dataref.ref_guid = drrw->drr_toguid;
386 dataref.ref_object = drrw->drr_object;
387 dataref.ref_offset = drrw->drr_offset;
389 if (ddt_update(dda->dedup_hdl, &ddt,
390 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
392 dmu_replay_record_t wbr_drr = {0};
393 struct drr_write_byref *wbr_drrr =
394 &wbr_drr.drr_u.drr_write_byref;
396 /* block already present in stream */
397 wbr_drr.drr_type = DRR_WRITE_BYREF;
399 wbr_drrr->drr_object = drrw->drr_object;
400 wbr_drrr->drr_offset = drrw->drr_offset;
401 wbr_drrr->drr_length = drrw->drr_length;
402 wbr_drrr->drr_toguid = drrw->drr_toguid;
403 wbr_drrr->drr_refguid = dataref.ref_guid;
404 wbr_drrr->drr_refobject =
406 wbr_drrr->drr_refoffset =
409 wbr_drrr->drr_checksumtype =
410 drrw->drr_checksumtype;
411 wbr_drrr->drr_checksumflags =
412 drrw->drr_checksumtype;
413 wbr_drrr->drr_key.ddk_cksum =
414 drrw->drr_key.ddk_cksum;
415 wbr_drrr->drr_key.ddk_prop =
416 drrw->drr_key.ddk_prop;
418 if (dump_record(&wbr_drr, NULL, 0,
419 &stream_cksum, outfd) != 0)
422 /* block not previously seen */
423 if (dump_record(drr, buf, drrw->drr_length,
424 &stream_cksum, outfd) != 0)
430 case DRR_WRITE_EMBEDDED:
432 struct drr_write_embedded *drrwe =
433 &drr->drr_u.drr_write_embedded;
435 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
436 if (dump_record(drr, buf,
437 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
438 &stream_cksum, outfd) != 0)
445 if (dump_record(drr, NULL, 0, &stream_cksum,
452 (void) fprintf(stderr, "INVALID record type 0x%x\n",
454 /* should never happen, so assert */
459 umem_cache_destroy(ddt.ddecache);
460 free(ddt.dedup_hash_array);
468 * Routines for dealing with the AVL tree of fs-nvlists
470 typedef struct fsavl_node {
478 fsavl_compare(const void *arg1, const void *arg2)
480 const fsavl_node_t *fn1 = arg1;
481 const fsavl_node_t *fn2 = arg2;
483 if (fn1->fn_guid > fn2->fn_guid)
485 else if (fn1->fn_guid < fn2->fn_guid)
492 * Given the GUID of a snapshot, find its containing filesystem and
496 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
498 fsavl_node_t fn_find;
501 fn_find.fn_guid = snapguid;
503 fn = avl_find(avl, &fn_find, NULL);
506 *snapname = fn->fn_snapname;
507 return (fn->fn_nvfs);
513 fsavl_destroy(avl_tree_t *avl)
522 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
529 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
532 fsavl_create(nvlist_t *fss)
535 nvpair_t *fselem = NULL;
537 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
540 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
541 offsetof(fsavl_node_t, fn_node));
543 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
544 nvlist_t *nvfs, *snaps;
545 nvpair_t *snapelem = NULL;
547 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
548 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
551 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
555 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
556 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
557 fsavl_destroy(fsavl);
561 fn->fn_snapname = nvpair_name(snapelem);
565 * Note: if there are multiple snaps with the
566 * same GUID, we ignore all but one.
568 if (avl_find(fsavl, fn, NULL) == NULL)
579 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
581 typedef struct send_data {
582 uint64_t parent_fromsnap_guid;
583 nvlist_t *parent_snaps;
586 const char *fromsnap;
591 * The header nvlist is of the following format:
594 * "fromsnap" -> string (if incremental)
598 * "name" -> string (full name; for debugging)
599 * "parentfromsnap" -> number (guid of fromsnap in parent)
601 * "props" -> { name -> value (only if set here) }
602 * "snaps" -> { name (lastname) -> number (guid) }
603 * "snapprops" -> { name (lastname) -> { name -> value } }
605 * "origin" -> number (guid) (if clone)
606 * "sent" -> boolean (not on-disk)
614 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
617 send_iterate_snap(zfs_handle_t *zhp, void *arg)
619 send_data_t *sd = arg;
620 uint64_t guid = zhp->zfs_dmustats.dds_guid;
624 snapname = strrchr(zhp->zfs_name, '@')+1;
626 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
628 * NB: if there is no fromsnap here (it's a newly created fs in
629 * an incremental replication), we will substitute the tosnap.
631 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
632 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
633 strcmp(snapname, sd->tosnap) == 0)) {
634 sd->parent_fromsnap_guid = guid;
637 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
638 send_iterate_prop(zhp, nv);
639 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
647 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
649 nvpair_t *elem = NULL;
651 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
652 char *propname = nvpair_name(elem);
653 zfs_prop_t prop = zfs_name_to_prop(propname);
656 if (!zfs_prop_user(propname)) {
658 * Realistically, this should never happen. However,
659 * we want the ability to add DSL properties without
660 * needing to make incompatible version changes. We
661 * need to ignore unknown properties to allow older
662 * software to still send datasets containing these
663 * properties, with the unknown properties elided.
665 if (prop == ZPROP_INVAL)
668 if (zfs_prop_readonly(prop))
672 verify(nvpair_value_nvlist(elem, &propnv) == 0);
673 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
674 prop == ZFS_PROP_REFQUOTA ||
675 prop == ZFS_PROP_REFRESERVATION) {
678 verify(nvlist_lookup_uint64(propnv,
679 ZPROP_VALUE, &value) == 0);
680 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
683 * May have no source before SPA_VERSION_RECVD_PROPS,
684 * but is still modifiable.
686 if (nvlist_lookup_string(propnv,
687 ZPROP_SOURCE, &source) == 0) {
688 if ((strcmp(source, zhp->zfs_name) != 0) &&
690 ZPROP_SOURCE_VAL_RECVD) != 0))
695 if (nvlist_lookup_string(propnv,
696 ZPROP_SOURCE, &source) != 0)
698 if ((strcmp(source, zhp->zfs_name) != 0) &&
699 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
703 if (zfs_prop_user(propname) ||
704 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
706 verify(nvlist_lookup_string(propnv,
707 ZPROP_VALUE, &value) == 0);
708 VERIFY(0 == nvlist_add_string(nv, propname, value));
711 verify(nvlist_lookup_uint64(propnv,
712 ZPROP_VALUE, &value) == 0);
713 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
719 * recursively generate nvlists describing datasets. See comment
720 * for the data structure send_data_t above for description of contents
724 send_iterate_fs(zfs_handle_t *zhp, void *arg)
726 send_data_t *sd = arg;
729 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
730 uint64_t guid = zhp->zfs_dmustats.dds_guid;
733 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
734 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
735 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
736 sd->parent_fromsnap_guid));
738 if (zhp->zfs_dmustats.dds_origin[0]) {
739 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
740 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
743 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
744 origin->zfs_dmustats.dds_guid));
747 /* iterate over props */
748 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
749 send_iterate_prop(zhp, nv);
750 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
753 /* iterate over snaps, and set sd->parent_fromsnap_guid */
754 sd->parent_fromsnap_guid = 0;
755 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
756 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
757 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd);
758 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
759 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
760 nvlist_free(sd->parent_snaps);
761 nvlist_free(sd->snapprops);
763 /* add this fs to nvlist */
764 (void) snprintf(guidstring, sizeof (guidstring),
765 "0x%llx", (longlong_t)guid);
766 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
769 /* iterate over children */
771 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
773 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
780 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
781 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
784 send_data_t sd = { 0 };
787 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
789 return (EZFS_BADTYPE);
791 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
792 sd.fromsnap = fromsnap;
794 sd.recursive = recursive;
796 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
804 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
815 * Routines specific to "zfs send"
817 typedef struct send_dump_data {
818 /* these are all just the short snapname (the part after the @) */
819 const char *fromsnap;
821 char prevsnap[ZFS_MAXNAMELEN];
822 uint64_t prevsnap_obj;
823 boolean_t seenfrom, seento, replicate, doall, fromorigin;
824 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
825 boolean_t large_block;
831 snapfilter_cb_t *filter_cb;
834 char holdtag[ZFS_MAXNAMELEN];
840 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
841 boolean_t fromorigin, uint64_t *sizep)
843 zfs_cmd_t zc = { 0 };
844 libzfs_handle_t *hdl = zhp->zfs_hdl;
846 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
847 assert(fromsnap_obj == 0 || !fromorigin);
849 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
850 zc.zc_obj = fromorigin;
851 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
852 zc.zc_fromobj = fromsnap_obj;
853 zc.zc_guid = 1; /* estimate flag */
855 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
857 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
858 "warning: cannot estimate space for '%s'"), zhp->zfs_name);
862 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
863 "not an earlier snapshot from the same fs"));
864 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
867 if (zfs_dataset_exists(hdl, zc.zc_name,
868 ZFS_TYPE_SNAPSHOT)) {
869 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
870 "incremental source (@%s) does not exist"),
873 return (zfs_error(hdl, EZFS_NOENT, errbuf));
885 zfs_error_aux(hdl, strerror(errno));
886 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
889 return (zfs_standard_error(hdl, errno, errbuf));
893 *sizep = zc.zc_objset_type;
899 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
900 * NULL) to the file descriptor specified by outfd.
903 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
904 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
907 zfs_cmd_t zc = { 0 };
908 libzfs_handle_t *hdl = zhp->zfs_hdl;
911 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
912 assert(fromsnap_obj == 0 || !fromorigin);
914 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
915 zc.zc_cookie = outfd;
916 zc.zc_obj = fromorigin;
917 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
918 zc.zc_fromobj = fromsnap_obj;
921 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
922 if (fromsnap && fromsnap[0] != '\0') {
923 VERIFY(0 == nvlist_add_string(thisdbg,
924 "fromsnap", fromsnap));
927 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
929 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
930 "warning: cannot send '%s'"), zhp->zfs_name);
932 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
934 VERIFY(0 == nvlist_add_nvlist(debugnv,
935 zhp->zfs_name, thisdbg));
937 nvlist_free(thisdbg);
941 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
942 "not an earlier snapshot from the same fs"));
943 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
946 if (zfs_dataset_exists(hdl, zc.zc_name,
947 ZFS_TYPE_SNAPSHOT)) {
948 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
949 "incremental source (@%s) does not exist"),
952 return (zfs_error(hdl, EZFS_NOENT, errbuf));
967 zfs_error_aux(hdl, strerror(errno));
968 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
971 return (zfs_standard_error(hdl, errno, errbuf));
976 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
977 nvlist_free(thisdbg);
983 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
985 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
988 * zfs_send() only sets snapholds for sends that need them,
989 * e.g. replication and doall.
991 if (sdd->snapholds == NULL)
994 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
998 send_progress_thread(void *arg)
1000 progress_arg_t *pa = arg;
1001 zfs_cmd_t zc = { 0 };
1002 zfs_handle_t *zhp = pa->pa_zhp;
1003 libzfs_handle_t *hdl = zhp->zfs_hdl;
1004 unsigned long long bytes;
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 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1043 uint64_t size, boolean_t parsable)
1046 if (fromsnap != NULL) {
1047 (void) fprintf(fout, "incremental\t%s\t%s",
1050 (void) fprintf(fout, "full\t%s",
1054 if (fromsnap != NULL) {
1055 if (strchr(fromsnap, '@') == NULL &&
1056 strchr(fromsnap, '#') == NULL) {
1057 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1058 "send from @%s to %s"),
1061 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1062 "send from %s to %s"),
1066 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1074 (void) fprintf(fout, "\t%llu",
1078 zfs_nicenum(size, buf, sizeof (buf));
1079 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1080 " estimated size is %s"), buf);
1083 (void) fprintf(fout, "\n");
1087 dump_snapshot(zfs_handle_t *zhp, void *arg)
1089 send_dump_data_t *sdd = arg;
1090 progress_arg_t pa = { 0 };
1094 boolean_t isfromsnap, istosnap, fromorigin;
1095 boolean_t exclude = B_FALSE;
1096 FILE *fout = sdd->std_out ? stdout : stderr;
1099 thissnap = strchr(zhp->zfs_name, '@') + 1;
1100 isfromsnap = (sdd->fromsnap != NULL &&
1101 strcmp(sdd->fromsnap, thissnap) == 0);
1103 if (!sdd->seenfrom && isfromsnap) {
1104 gather_holds(zhp, sdd);
1105 sdd->seenfrom = B_TRUE;
1106 (void) strcpy(sdd->prevsnap, thissnap);
1107 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1112 if (sdd->seento || !sdd->seenfrom) {
1117 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1119 sdd->seento = B_TRUE;
1121 if (!sdd->doall && !isfromsnap && !istosnap) {
1122 if (sdd->replicate) {
1124 nvlist_t *snapprops;
1126 * Filter out all intermediate snapshots except origin
1127 * snapshots needed to replicate clones.
1129 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1130 zhp->zfs_dmustats.dds_guid, &snapname);
1132 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1133 "snapprops", &snapprops));
1134 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1135 thissnap, &snapprops));
1136 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1143 * If a filter function exists, call it to determine whether
1144 * this snapshot will be sent.
1146 if (exclude || (sdd->filter_cb != NULL &&
1147 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1149 * This snapshot is filtered out. Don't send it, and don't
1150 * set prevsnap_obj, so it will be as if this snapshot didn't
1151 * exist, and the next accepted snapshot will be sent as
1152 * an incremental from the last accepted one, or as the
1153 * first (and full) snapshot in the case of a replication,
1154 * non-incremental send.
1160 gather_holds(zhp, sdd);
1161 fromorigin = sdd->prevsnap[0] == '\0' &&
1162 (sdd->fromorigin || sdd->replicate);
1166 (void) estimate_ioctl(zhp, sdd->prevsnap_obj,
1169 send_print_verbose(fout, zhp->zfs_name,
1170 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1171 size, sdd->parsable);
1177 * If progress reporting is requested, spawn a new thread to
1178 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1180 if (sdd->progress) {
1182 pa.pa_fd = sdd->outfd;
1183 pa.pa_parsable = sdd->parsable;
1185 if (err = pthread_create(&tid, NULL,
1186 send_progress_thread, &pa)) {
1192 enum lzc_send_flags flags = 0;
1193 if (sdd->large_block)
1194 flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1195 if (sdd->embed_data)
1196 flags |= LZC_SEND_FLAG_EMBED_DATA;
1198 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1199 fromorigin, sdd->outfd, flags, sdd->debugnv);
1201 if (sdd->progress) {
1202 (void) pthread_cancel(tid);
1203 (void) pthread_join(tid, NULL);
1207 (void) strcpy(sdd->prevsnap, thissnap);
1208 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1214 dump_filesystem(zfs_handle_t *zhp, void *arg)
1217 send_dump_data_t *sdd = arg;
1218 boolean_t missingfrom = B_FALSE;
1219 zfs_cmd_t zc = { 0 };
1221 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1222 zhp->zfs_name, sdd->tosnap);
1223 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1224 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1225 "WARNING: could not send %s@%s: does not exist\n"),
1226 zhp->zfs_name, sdd->tosnap);
1231 if (sdd->replicate && sdd->fromsnap) {
1233 * If this fs does not have fromsnap, and we're doing
1234 * recursive, we need to send a full stream from the
1235 * beginning (or an incremental from the origin if this
1236 * is a clone). If we're doing non-recursive, then let
1237 * them get the error.
1239 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1240 zhp->zfs_name, sdd->fromsnap);
1241 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1242 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1243 missingfrom = B_TRUE;
1247 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1248 sdd->prevsnap_obj = 0;
1249 if (sdd->fromsnap == NULL || missingfrom)
1250 sdd->seenfrom = B_TRUE;
1252 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1253 if (!sdd->seenfrom) {
1254 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1255 "WARNING: could not send %s@%s:\n"
1256 "incremental source (%s@%s) does not exist\n"),
1257 zhp->zfs_name, sdd->tosnap,
1258 zhp->zfs_name, sdd->fromsnap);
1260 } else if (!sdd->seento) {
1261 if (sdd->fromsnap) {
1262 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1263 "WARNING: could not send %s@%s:\n"
1264 "incremental source (%s@%s) "
1265 "is not earlier than it\n"),
1266 zhp->zfs_name, sdd->tosnap,
1267 zhp->zfs_name, sdd->fromsnap);
1269 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1271 "could not send %s@%s: does not exist\n"),
1272 zhp->zfs_name, sdd->tosnap);
1281 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1283 send_dump_data_t *sdd = arg;
1285 boolean_t needagain, progress;
1287 if (!sdd->replicate)
1288 return (dump_filesystem(rzhp, sdd));
1290 /* Mark the clone origin snapshots. */
1291 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1292 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1294 uint64_t origin_guid = 0;
1296 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1297 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1298 if (origin_guid != 0) {
1300 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1301 origin_guid, &snapname);
1302 if (origin_nv != NULL) {
1303 nvlist_t *snapprops;
1304 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1305 "snapprops", &snapprops));
1306 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1307 snapname, &snapprops));
1308 VERIFY(0 == nvlist_add_boolean(
1309 snapprops, "is_clone_origin"));
1314 needagain = progress = B_FALSE;
1315 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1316 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1317 nvlist_t *fslist, *parent_nv;
1321 uint64_t origin_guid = 0;
1322 uint64_t parent_guid = 0;
1324 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1325 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1328 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1329 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1330 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1333 if (parent_guid != 0) {
1334 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1335 if (!nvlist_exists(parent_nv, "sent")) {
1336 /* parent has not been sent; skip this one */
1342 if (origin_guid != 0) {
1343 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1345 if (origin_nv != NULL &&
1346 !nvlist_exists(origin_nv, "sent")) {
1348 * origin has not been sent yet;
1356 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1359 err = dump_filesystem(zhp, sdd);
1360 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1371 /* clean out the sent flags in case we reuse this fss */
1372 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1373 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1376 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1377 (void) nvlist_remove_all(fslist, "sent");
1384 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1386 unsigned int version;
1388 unsigned long long checksum, packed_len;
1391 * Decode token header, which is:
1392 * <token version>-<checksum of payload>-<uncompressed payload length>
1393 * Note that the only supported token version is 1.
1395 nread = sscanf(token, "%u-%llx-%llx-",
1396 &version, &checksum, &packed_len);
1398 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1399 "resume token is corrupt (invalid format)"));
1403 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1404 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1405 "resume token is corrupt (invalid version %u)"),
1410 /* convert hexadecimal representation to binary */
1411 token = strrchr(token, '-') + 1;
1412 int len = strlen(token) / 2;
1413 unsigned char *compressed = zfs_alloc(hdl, len);
1414 for (int i = 0; i < len; i++) {
1415 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1418 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1419 "resume token is corrupt "
1420 "(payload is not hex-encoded)"));
1425 /* verify checksum */
1427 fletcher_4_native(compressed, len, NULL, &cksum);
1428 if (cksum.zc_word[0] != checksum) {
1430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1431 "resume token is corrupt (incorrect checksum)"));
1436 void *packed = zfs_alloc(hdl, packed_len);
1437 uLongf packed_len_long = packed_len;
1438 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1439 packed_len_long != packed_len) {
1442 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1443 "resume token is corrupt (decompression failed)"));
1449 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1453 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1454 "resume token is corrupt (nvlist_unpack failed)"));
1461 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1462 const char *resume_token)
1466 char *fromname = NULL;
1467 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1470 char name[ZFS_MAXNAMELEN];
1471 enum lzc_send_flags lzc_flags = 0;
1473 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1474 "cannot resume send"));
1476 nvlist_t *resume_nvl =
1477 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1478 if (resume_nvl == NULL) {
1480 * zfs_error_aux has already been set by
1481 * zfs_send_resume_token_to_nvlist
1483 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1485 if (flags->verbose) {
1486 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1487 "resume token contents:\n"));
1488 nvlist_print(stderr, resume_nvl);
1491 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1492 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1493 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1494 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1495 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1496 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1497 "resume token is corrupt"));
1498 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1501 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1503 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1504 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1506 if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1507 if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1509 "'%s' is no longer the same snapshot used in "
1510 "the initial send"), toname);
1512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1513 "'%s' used in the initial send no longer exists"),
1516 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1518 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1521 "unable to access '%s'"), name);
1522 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1525 if (fromguid != 0) {
1526 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1527 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1528 "incremental source %#llx no longer exists"),
1529 (longlong_t)fromguid);
1530 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1535 if (flags->verbose) {
1537 error = lzc_send_space(zhp->zfs_name, fromname, &size);
1539 size = MAX(0, (int64_t)(size - bytes));
1540 send_print_verbose(stderr, zhp->zfs_name, fromname,
1541 size, flags->parsable);
1544 if (!flags->dryrun) {
1545 progress_arg_t pa = { 0 };
1548 * If progress reporting is requested, spawn a new thread to
1549 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1551 if (flags->progress) {
1554 pa.pa_parsable = flags->parsable;
1556 error = pthread_create(&tid, NULL,
1557 send_progress_thread, &pa);
1564 error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1565 lzc_flags, resumeobj, resumeoff);
1567 if (flags->progress) {
1568 (void) pthread_cancel(tid);
1569 (void) pthread_join(tid, NULL);
1573 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1574 "warning: cannot send '%s'"), zhp->zfs_name);
1596 zfs_error_aux(hdl, strerror(errno));
1597 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1600 return (zfs_standard_error(hdl, errno, errbuf));
1611 * Generate a send stream for the dataset identified by the argument zhp.
1613 * The content of the send stream is the snapshot identified by
1614 * 'tosnap'. Incremental streams are requested in two ways:
1615 * - from the snapshot identified by "fromsnap" (if non-null) or
1616 * - from the origin of the dataset identified by zhp, which must
1617 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1620 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1621 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1622 * if "replicate" is set. If "doall" is set, dump all the intermediate
1623 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1624 * case too. If "props" is set, send properties.
1627 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1628 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1629 void *cb_arg, nvlist_t **debugnvp)
1632 send_dump_data_t sdd = { 0 };
1634 nvlist_t *fss = NULL;
1635 avl_tree_t *fsavl = NULL;
1636 static uint64_t holdseq;
1640 dedup_arg_t dda = { 0 };
1641 int featureflags = 0;
1644 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1645 "cannot send '%s'"), zhp->zfs_name);
1647 if (fromsnap && fromsnap[0] == '\0') {
1648 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1649 "zero-length incremental source"));
1650 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1653 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1655 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1656 if (version >= ZPL_VERSION_SA) {
1657 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1661 if (flags->dedup && !flags->dryrun) {
1662 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1663 DMU_BACKUP_FEATURE_DEDUPPROPS);
1664 if (err = pipe(pipefd)) {
1665 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1666 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1669 dda.outputfd = outfd;
1670 dda.inputfd = pipefd[1];
1671 dda.dedup_hdl = zhp->zfs_hdl;
1672 if (err = pthread_create(&tid, NULL, cksummer, &dda)) {
1673 (void) close(pipefd[0]);
1674 (void) close(pipefd[1]);
1675 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1676 return (zfs_error(zhp->zfs_hdl,
1677 EZFS_THREADCREATEFAILED, errbuf));
1681 if (flags->replicate || flags->doall || flags->props) {
1682 dmu_replay_record_t drr = { 0 };
1683 char *packbuf = NULL;
1685 zio_cksum_t zc = { 0 };
1687 if (flags->replicate || flags->props) {
1690 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1692 VERIFY(0 == nvlist_add_string(hdrnv,
1693 "fromsnap", fromsnap));
1695 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1696 if (!flags->replicate) {
1697 VERIFY(0 == nvlist_add_boolean(hdrnv,
1701 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1702 fromsnap, tosnap, flags->replicate, &fss, &fsavl);
1705 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1706 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1716 if (!flags->dryrun) {
1717 /* write first begin record */
1718 drr.drr_type = DRR_BEGIN;
1719 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1720 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1721 drr_versioninfo, DMU_COMPOUNDSTREAM);
1722 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1723 drr_versioninfo, featureflags);
1724 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1725 sizeof (drr.drr_u.drr_begin.drr_toname),
1726 "%s@%s", zhp->zfs_name, tosnap);
1727 drr.drr_payloadlen = buflen;
1729 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1734 /* write end record */
1735 bzero(&drr, sizeof (drr));
1736 drr.drr_type = DRR_END;
1737 drr.drr_u.drr_end.drr_checksum = zc;
1738 err = write(outfd, &drr, sizeof (drr));
1748 /* dump each stream */
1749 sdd.fromsnap = fromsnap;
1750 sdd.tosnap = tosnap;
1752 sdd.outfd = pipefd[0];
1755 sdd.replicate = flags->replicate;
1756 sdd.doall = flags->doall;
1757 sdd.fromorigin = flags->fromorigin;
1760 sdd.verbose = flags->verbose;
1761 sdd.parsable = flags->parsable;
1762 sdd.progress = flags->progress;
1763 sdd.dryrun = flags->dryrun;
1764 sdd.large_block = flags->largeblock;
1765 sdd.embed_data = flags->embed_data;
1766 sdd.filter_cb = filter_func;
1767 sdd.filter_cb_arg = cb_arg;
1769 sdd.debugnv = *debugnvp;
1770 if (sdd.verbose && sdd.dryrun)
1771 sdd.std_out = B_TRUE;
1772 fout = sdd.std_out ? stdout : stderr;
1775 * Some flags require that we place user holds on the datasets that are
1776 * being sent so they don't get destroyed during the send. We can skip
1777 * this step if the pool is imported read-only since the datasets cannot
1780 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1781 ZPOOL_PROP_READONLY, NULL) &&
1782 zfs_spa_version(zhp, &spa_version) == 0 &&
1783 spa_version >= SPA_VERSION_USERREFS &&
1784 (flags->doall || flags->replicate)) {
1786 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1787 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1788 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1789 if (sdd.cleanup_fd < 0) {
1793 sdd.snapholds = fnvlist_alloc();
1795 sdd.cleanup_fd = -1;
1796 sdd.snapholds = NULL;
1798 if (flags->verbose || sdd.snapholds != NULL) {
1800 * Do a verbose no-op dry run to get all the verbose output
1801 * or to gather snapshot hold's before generating any data,
1802 * then do a non-verbose real run to generate the streams.
1804 sdd.dryrun = B_TRUE;
1805 err = dump_filesystems(zhp, &sdd);
1810 if (flags->verbose) {
1811 if (flags->parsable) {
1812 (void) fprintf(fout, "size\t%llu\n",
1813 (longlong_t)sdd.size);
1816 zfs_nicenum(sdd.size, buf, sizeof (buf));
1817 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1818 "total estimated size is %s\n"), buf);
1822 /* Ensure no snaps found is treated as an error. */
1828 /* Skip the second run if dryrun was requested. */
1832 if (sdd.snapholds != NULL) {
1833 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
1837 fnvlist_free(sdd.snapholds);
1838 sdd.snapholds = NULL;
1841 sdd.dryrun = B_FALSE;
1842 sdd.verbose = B_FALSE;
1845 err = dump_filesystems(zhp, &sdd);
1846 fsavl_destroy(fsavl);
1849 /* Ensure no snaps found is treated as an error. */
1850 if (err == 0 && !sdd.seento)
1855 (void) pthread_cancel(tid);
1856 (void) close(pipefd[0]);
1857 (void) pthread_join(tid, NULL);
1860 if (sdd.cleanup_fd != -1) {
1861 VERIFY(0 == close(sdd.cleanup_fd));
1862 sdd.cleanup_fd = -1;
1865 if (!flags->dryrun && (flags->replicate || flags->doall ||
1868 * write final end record. NB: want to do this even if
1869 * there was some error, because it might not be totally
1872 dmu_replay_record_t drr = { 0 };
1873 drr.drr_type = DRR_END;
1874 if (write(outfd, &drr, sizeof (drr)) == -1) {
1875 return (zfs_standard_error(zhp->zfs_hdl,
1880 return (err || sdd.err);
1883 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1885 fsavl_destroy(fsavl);
1887 fnvlist_free(sdd.snapholds);
1889 if (sdd.cleanup_fd != -1)
1890 VERIFY(0 == close(sdd.cleanup_fd));
1892 (void) pthread_cancel(tid);
1893 (void) close(pipefd[0]);
1894 (void) pthread_join(tid, NULL);
1900 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
1901 enum lzc_send_flags flags)
1904 libzfs_handle_t *hdl = zhp->zfs_hdl;
1907 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1908 "warning: cannot send '%s'"), zhp->zfs_name);
1910 err = lzc_send(zhp->zfs_name, from, fd, flags);
1914 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1915 "not an earlier snapshot from the same fs"));
1916 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1920 if (lzc_exists(zhp->zfs_name)) {
1921 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1922 "incremental source (%s) does not exist"),
1925 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1928 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1929 "target is busy; if a filesystem, "
1930 "it must not be mounted"));
1931 return (zfs_error(hdl, EZFS_BUSY, errbuf));
1946 zfs_error_aux(hdl, strerror(errno));
1947 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1950 return (zfs_standard_error(hdl, errno, errbuf));
1957 * Routines specific to "zfs recv"
1961 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1962 boolean_t byteswap, zio_cksum_t *zc)
1968 assert(ilen <= SPA_MAXBLOCKSIZE);
1971 rv = read(fd, cp, len);
1976 if (rv < 0 || len != 0) {
1977 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1978 "failed to read from stream"));
1979 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1980 "cannot receive")));
1985 fletcher_4_incremental_byteswap(buf, ilen, zc);
1987 fletcher_4_incremental_native(buf, ilen, zc);
1993 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1994 boolean_t byteswap, zio_cksum_t *zc)
1999 buf = zfs_alloc(hdl, len);
2003 err = recv_read(hdl, fd, buf, len, byteswap, zc);
2009 err = nvlist_unpack(buf, len, nvp, 0);
2012 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2013 "stream (malformed nvlist)"));
2020 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2021 int baselen, char *newname, recvflags_t *flags)
2024 zfs_cmd_t zc = { 0 };
2026 prop_changelist_t *clp;
2029 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2032 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2033 flags->force ? MS_FORCE : 0);
2037 err = changelist_prefix(clp);
2041 zc.zc_objset_type = DMU_OST_ZFS;
2042 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2045 (void) strcpy(newname, tryname);
2047 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
2049 if (flags->verbose) {
2050 (void) printf("attempting rename %s to %s\n",
2051 zc.zc_name, zc.zc_value);
2053 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2055 changelist_rename(clp, name, tryname);
2060 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2063 (void) snprintf(newname, ZFS_MAXNAMELEN, "%.*srecv-%u-%u",
2064 baselen, name, getpid(), seq);
2065 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
2067 if (flags->verbose) {
2068 (void) printf("failed - trying rename %s to %s\n",
2069 zc.zc_name, zc.zc_value);
2071 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2073 changelist_rename(clp, name, newname);
2074 if (err && flags->verbose) {
2075 (void) printf("failed (%u) - "
2076 "will try again on next pass\n", errno);
2079 } else if (flags->verbose) {
2081 (void) printf("success\n");
2083 (void) printf("failed (%u)\n", errno);
2086 (void) changelist_postfix(clp);
2087 changelist_free(clp);
2093 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2094 char *newname, recvflags_t *flags)
2096 zfs_cmd_t zc = { 0 };
2098 prop_changelist_t *clp;
2100 boolean_t defer = B_FALSE;
2103 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2106 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2107 flags->force ? MS_FORCE : 0);
2108 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2109 zfs_spa_version(zhp, &spa_version) == 0 &&
2110 spa_version >= SPA_VERSION_USERREFS)
2115 err = changelist_prefix(clp);
2119 zc.zc_objset_type = DMU_OST_ZFS;
2120 zc.zc_defer_destroy = defer;
2121 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2124 (void) printf("attempting destroy %s\n", zc.zc_name);
2125 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
2128 (void) printf("success\n");
2129 changelist_remove(clp, zc.zc_name);
2132 (void) changelist_postfix(clp);
2133 changelist_free(clp);
2136 * Deferred destroy might destroy the snapshot or only mark it to be
2137 * destroyed later, and it returns success in either case.
2139 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2140 ZFS_TYPE_SNAPSHOT))) {
2141 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2147 typedef struct guid_to_name_data {
2149 boolean_t bookmark_ok;
2152 } guid_to_name_data_t;
2155 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2157 guid_to_name_data_t *gtnd = arg;
2161 if (gtnd->skip != NULL &&
2162 (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2163 strcmp(slash + 1, gtnd->skip) == 0) {
2168 if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2169 (void) strcpy(gtnd->name, zhp->zfs_name);
2174 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2175 if (err != EEXIST && gtnd->bookmark_ok)
2176 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2182 * Attempt to find the local dataset associated with this guid. In the case of
2183 * multiple matches, we attempt to find the "best" match by searching
2184 * progressively larger portions of the hierarchy. This allows one to send a
2185 * tree of datasets individually and guarantee that we will find the source
2186 * guid within that hierarchy, even if there are multiple matches elsewhere.
2189 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2190 boolean_t bookmark_ok, char *name)
2192 char pname[ZFS_MAXNAMELEN];
2193 guid_to_name_data_t gtnd;
2196 gtnd.bookmark_ok = bookmark_ok;
2201 * Search progressively larger portions of the hierarchy, starting
2202 * with the filesystem specified by 'parent'. This will
2203 * select the "most local" version of the origin snapshot in the case
2204 * that there are multiple matching snapshots in the system.
2206 (void) strlcpy(pname, parent, sizeof (pname));
2207 char *cp = strrchr(pname, '@');
2209 cp = strchr(pname, '\0');
2210 for (; cp != NULL; cp = strrchr(pname, '/')) {
2211 /* Chop off the last component and open the parent */
2213 zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2217 int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
2219 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2220 if (err != EEXIST && bookmark_ok)
2221 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2227 * Remember the last portion of the dataset so we skip it next
2228 * time through (as we've already searched that portion of the
2231 gtnd.skip = strrchr(pname, '/') + 1;
2238 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2239 * guid1 is after guid2.
2242 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2243 uint64_t guid1, uint64_t guid2)
2246 char *fsname, *snapname;
2247 char buf[ZFS_MAXNAMELEN];
2249 zfs_handle_t *guid1hdl, *guid2hdl;
2250 uint64_t create1, create2;
2257 nvfs = fsavl_find(avl, guid1, &snapname);
2258 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2259 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2260 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2261 if (guid1hdl == NULL)
2264 nvfs = fsavl_find(avl, guid2, &snapname);
2265 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2266 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2267 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2268 if (guid2hdl == NULL) {
2269 zfs_close(guid1hdl);
2273 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2274 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2276 if (create1 < create2)
2278 else if (create1 > create2)
2283 zfs_close(guid1hdl);
2284 zfs_close(guid2hdl);
2290 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2291 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2294 nvlist_t *local_nv, *deleted = NULL;
2295 avl_tree_t *local_avl;
2296 nvpair_t *fselem, *nextfselem;
2298 char newname[ZFS_MAXNAMELEN];
2301 boolean_t needagain, progress, recursive;
2304 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2306 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2313 needagain = progress = B_FALSE;
2315 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2317 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2318 recursive, &local_nv, &local_avl)) != 0)
2322 * Process deletes and renames
2324 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2325 fselem; fselem = nextfselem) {
2326 nvlist_t *nvfs, *snaps;
2327 nvlist_t *stream_nvfs = NULL;
2328 nvpair_t *snapelem, *nextsnapelem;
2329 uint64_t fromguid = 0;
2330 uint64_t originguid = 0;
2331 uint64_t stream_originguid = 0;
2332 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2333 char *fsname, *stream_fsname;
2335 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2337 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2338 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2339 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2340 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2341 &parent_fromsnap_guid));
2342 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2345 * First find the stream's fs, so we can check for
2346 * a different origin (due to "zfs promote")
2348 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2349 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2352 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2353 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2355 if (stream_nvfs != NULL)
2359 /* check for promote */
2360 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2361 &stream_originguid);
2362 if (stream_nvfs && originguid != stream_originguid) {
2363 switch (created_before(hdl, local_avl,
2364 stream_originguid, originguid)) {
2367 zfs_cmd_t zc = { 0 };
2368 nvlist_t *origin_nvfs;
2369 char *origin_fsname;
2372 (void) printf("promoting %s\n", fsname);
2374 origin_nvfs = fsavl_find(local_avl, originguid,
2376 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2377 "name", &origin_fsname));
2378 (void) strlcpy(zc.zc_value, origin_fsname,
2379 sizeof (zc.zc_value));
2380 (void) strlcpy(zc.zc_name, fsname,
2381 sizeof (zc.zc_name));
2382 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2390 fsavl_destroy(local_avl);
2391 nvlist_free(local_nv);
2395 * We had/have the wrong origin, therefore our
2396 * list of snapshots is wrong. Need to handle
2397 * them on the next pass.
2403 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2404 snapelem; snapelem = nextsnapelem) {
2406 char *stream_snapname;
2407 nvlist_t *found, *props;
2409 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2411 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2412 found = fsavl_find(stream_avl, thisguid,
2415 /* check for delete */
2416 if (found == NULL) {
2417 char name[ZFS_MAXNAMELEN];
2422 (void) snprintf(name, sizeof (name), "%s@%s",
2423 fsname, nvpair_name(snapelem));
2425 error = recv_destroy(hdl, name,
2426 strlen(fsname)+1, newname, flags);
2431 sprintf(guidname, "%lu", thisguid);
2432 nvlist_add_boolean(deleted, guidname);
2436 stream_nvfs = found;
2438 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2439 &props) && 0 == nvlist_lookup_nvlist(props,
2440 stream_snapname, &props)) {
2441 zfs_cmd_t zc = { 0 };
2443 zc.zc_cookie = B_TRUE; /* received */
2444 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2445 "%s@%s", fsname, nvpair_name(snapelem));
2446 if (zcmd_write_src_nvlist(hdl, &zc,
2448 (void) zfs_ioctl(hdl,
2449 ZFS_IOC_SET_PROP, &zc);
2450 zcmd_free_nvlists(&zc);
2454 /* check for different snapname */
2455 if (strcmp(nvpair_name(snapelem),
2456 stream_snapname) != 0) {
2457 char name[ZFS_MAXNAMELEN];
2458 char tryname[ZFS_MAXNAMELEN];
2460 (void) snprintf(name, sizeof (name), "%s@%s",
2461 fsname, nvpair_name(snapelem));
2462 (void) snprintf(tryname, sizeof (name), "%s@%s",
2463 fsname, stream_snapname);
2465 error = recv_rename(hdl, name, tryname,
2466 strlen(fsname)+1, newname, flags);
2473 if (strcmp(stream_snapname, fromsnap) == 0)
2474 fromguid = thisguid;
2477 /* check for delete */
2478 if (stream_nvfs == NULL) {
2482 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2488 sprintf(guidname, "%lu", parent_fromsnap_guid);
2489 nvlist_add_boolean(deleted, guidname);
2493 if (fromguid == 0) {
2494 if (flags->verbose) {
2495 (void) printf("local fs %s does not have "
2496 "fromsnap (%s in stream); must have "
2497 "been deleted locally; ignoring\n",
2503 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2504 "name", &stream_fsname));
2505 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2506 "parentfromsnap", &stream_parent_fromsnap_guid));
2508 s1 = strrchr(fsname, '/');
2509 s2 = strrchr(stream_fsname, '/');
2512 * Check if we're going to rename based on parent guid change
2513 * and the current parent guid was also deleted. If it was then
2514 * rename will fail and is likely unneeded, so avoid this and
2515 * force an early retry to determine the new
2516 * parent_fromsnap_guid.
2518 if (stream_parent_fromsnap_guid != 0 &&
2519 parent_fromsnap_guid != 0 &&
2520 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
2521 sprintf(guidname, "%lu", parent_fromsnap_guid);
2522 if (nvlist_exists(deleted, guidname)) {
2530 * Check for rename. If the exact receive path is specified, it
2531 * does not count as a rename, but we still need to check the
2532 * datasets beneath it.
2534 if ((stream_parent_fromsnap_guid != 0 &&
2535 parent_fromsnap_guid != 0 &&
2536 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2537 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2538 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2540 char tryname[ZFS_MAXNAMELEN];
2542 parent = fsavl_find(local_avl,
2543 stream_parent_fromsnap_guid, NULL);
2545 * NB: parent might not be found if we used the
2546 * tosnap for stream_parent_fromsnap_guid,
2547 * because the parent is a newly-created fs;
2548 * we'll be able to rename it after we recv the
2551 if (parent != NULL) {
2554 VERIFY(0 == nvlist_lookup_string(parent, "name",
2556 (void) snprintf(tryname, sizeof (tryname),
2557 "%s%s", pname, strrchr(stream_fsname, '/'));
2560 if (flags->verbose) {
2561 (void) printf("local fs %s new parent "
2562 "not found\n", fsname);
2568 error = recv_rename(hdl, fsname, tryname,
2569 strlen(tofs)+1, newname, flags);
2571 if (renamed != NULL && newname[0] != '\0') {
2572 VERIFY(0 == nvlist_add_boolean(renamed,
2584 fsavl_destroy(local_avl);
2585 nvlist_free(local_nv);
2586 nvlist_free(deleted);
2588 if (needagain && progress) {
2589 /* do another pass to fix up temporary names */
2591 (void) printf("another pass:\n");
2599 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2600 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2601 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2603 nvlist_t *stream_nv = NULL;
2604 avl_tree_t *stream_avl = NULL;
2605 char *fromsnap = NULL;
2606 char *sendsnap = NULL;
2608 char tofs[ZFS_MAXNAMELEN];
2609 char sendfs[ZFS_MAXNAMELEN];
2611 dmu_replay_record_t drre;
2613 boolean_t anyerr = B_FALSE;
2614 boolean_t softerr = B_FALSE;
2615 boolean_t recursive;
2617 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2620 assert(drr->drr_type == DRR_BEGIN);
2621 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2622 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2623 DMU_COMPOUNDSTREAM);
2626 * Read in the nvlist from the stream.
2628 if (drr->drr_payloadlen != 0) {
2629 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2630 &stream_nv, flags->byteswap, zc);
2632 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2637 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2640 if (recursive && strchr(destname, '@')) {
2641 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2642 "cannot specify snapshot name for multi-snapshot stream"));
2643 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2648 * Read in the end record and verify checksum.
2650 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2651 flags->byteswap, NULL)))
2653 if (flags->byteswap) {
2654 drre.drr_type = BSWAP_32(drre.drr_type);
2655 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2656 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2657 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2658 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2659 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2660 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2661 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2662 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2664 if (drre.drr_type != DRR_END) {
2665 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2668 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2669 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2670 "incorrect header checksum"));
2671 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2675 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2677 if (drr->drr_payloadlen != 0) {
2678 nvlist_t *stream_fss;
2680 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2682 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2683 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2684 "couldn't allocate avl tree"));
2685 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2689 if (fromsnap != NULL) {
2690 nvlist_t *renamed = NULL;
2691 nvpair_t *pair = NULL;
2693 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2694 if (flags->isprefix) {
2695 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2698 if (flags->istail) {
2699 cp = strrchr(drrb->drr_toname, '/');
2701 (void) strlcat(tofs, "/",
2705 i = (cp - drrb->drr_toname);
2708 i = strcspn(drrb->drr_toname, "/@");
2710 /* zfs_receive_one() will create_parents() */
2711 (void) strlcat(tofs, &drrb->drr_toname[i],
2713 *strchr(tofs, '@') = '\0';
2716 if (recursive && !flags->dryrun && !flags->nomount) {
2717 VERIFY(0 == nvlist_alloc(&renamed,
2718 NV_UNIQUE_NAME, 0));
2721 softerr = recv_incremental_replication(hdl, tofs, flags,
2722 stream_nv, stream_avl, renamed);
2724 /* Unmount renamed filesystems before receiving. */
2725 while ((pair = nvlist_next_nvpair(renamed,
2728 prop_changelist_t *clp = NULL;
2730 zhp = zfs_open(hdl, nvpair_name(pair),
2731 ZFS_TYPE_FILESYSTEM);
2733 clp = changelist_gather(zhp,
2734 ZFS_PROP_MOUNTPOINT, 0, 0);
2738 changelist_prefix(clp);
2739 changelist_free(clp);
2744 nvlist_free(renamed);
2749 * Get the fs specified by the first path in the stream (the top level
2750 * specified by 'zfs send') and pass it to each invocation of
2751 * zfs_receive_one().
2753 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2755 if ((cp = strchr(sendfs, '@')) != NULL) {
2758 * Find the "sendsnap", the final snapshot in a replication
2759 * stream. zfs_receive_one() handles certain errors
2760 * differently, depending on if the contained stream is the
2763 sendsnap = (cp + 1);
2766 /* Finally, receive each contained stream */
2769 * we should figure out if it has a recoverable
2770 * error, in which case do a recv_skip() and drive on.
2771 * Note, if we fail due to already having this guid,
2772 * zfs_receive_one() will take care of it (ie,
2773 * recv_skip() and return 0).
2775 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2776 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2777 action_handlep, sendsnap);
2778 if (error == ENODATA) {
2783 } while (error == 0);
2785 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2787 * Now that we have the fs's they sent us, try the
2790 softerr = recv_incremental_replication(hdl, tofs, flags,
2791 stream_nv, stream_avl, NULL);
2795 fsavl_destroy(stream_avl);
2796 nvlist_free(stream_nv);
2805 trunc_prop_errs(int truncated)
2807 ASSERT(truncated != 0);
2810 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2811 "1 more property could not be set\n"));
2813 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2814 "%d more properties could not be set\n"), truncated);
2818 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2820 dmu_replay_record_t *drr;
2821 void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
2824 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2825 "cannot receive:"));
2827 /* XXX would be great to use lseek if possible... */
2830 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2831 byteswap, NULL) == 0) {
2833 drr->drr_type = BSWAP_32(drr->drr_type);
2835 switch (drr->drr_type) {
2837 if (drr->drr_payloadlen != 0) {
2838 (void) recv_read(hdl, fd, buf,
2839 drr->drr_payloadlen, B_FALSE, NULL);
2849 drr->drr_u.drr_object.drr_bonuslen =
2850 BSWAP_32(drr->drr_u.drr_object.
2853 (void) recv_read(hdl, fd, buf,
2854 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2860 drr->drr_u.drr_write.drr_length =
2861 BSWAP_64(drr->drr_u.drr_write.drr_length);
2863 (void) recv_read(hdl, fd, buf,
2864 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2868 drr->drr_u.drr_write.drr_length =
2869 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2871 (void) recv_read(hdl, fd, buf,
2872 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2874 case DRR_WRITE_EMBEDDED:
2876 drr->drr_u.drr_write_embedded.drr_psize =
2877 BSWAP_32(drr->drr_u.drr_write_embedded.
2880 (void) recv_read(hdl, fd, buf,
2881 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
2884 case DRR_WRITE_BYREF:
2885 case DRR_FREEOBJECTS:
2890 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2891 "invalid record type"));
2892 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2901 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
2902 boolean_t resumable)
2904 char target_fs[ZFS_MAXNAMELEN];
2906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2907 "checksum mismatch or incomplete stream"));
2911 (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
2912 *strchr(target_fs, '@') = '\0';
2913 zfs_handle_t *zhp = zfs_open(hdl, target_fs,
2914 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2918 char token_buf[ZFS_MAXPROPLEN];
2919 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
2920 token_buf, sizeof (token_buf),
2921 NULL, NULL, 0, B_TRUE);
2923 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2924 "checksum mismatch or incomplete stream.\n"
2925 "Partially received snapshot is saved.\n"
2926 "A resuming stream can be generated on the sending "
2927 "system by running:\n"
2935 * Restores a backup of tosnap from the file descriptor specified by infd.
2938 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2939 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
2940 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
2941 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2942 uint64_t *action_handlep, const char *finalsnap)
2944 zfs_cmd_t zc = { 0 };
2946 int ioctl_err, ioctl_errno, err;
2948 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2950 char prop_errbuf[1024];
2951 const char *chopprefix;
2952 boolean_t newfs = B_FALSE;
2953 boolean_t stream_wantsnewfs;
2954 uint64_t parent_snapguid = 0;
2955 prop_changelist_t *clp = NULL;
2956 nvlist_t *snapprops_nvlist = NULL;
2957 zprop_errflags_t prop_errflags;
2958 boolean_t recursive;
2959 char *snapname = NULL;
2961 begin_time = time(NULL);
2963 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2966 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2969 if (stream_avl != NULL) {
2970 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2975 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2977 err = nvlist_lookup_nvlist(fs, "props", &props);
2979 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2981 if (flags->canmountoff) {
2982 VERIFY(0 == nvlist_add_uint64(props,
2983 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2985 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2989 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2990 VERIFY(0 == nvlist_lookup_nvlist(props,
2991 snapname, &snapprops_nvlist));
3001 * Determine how much of the snapshot name stored in the stream
3002 * we are going to tack on to the name they specified on the
3003 * command line, and how much we are going to chop off.
3005 * If they specified a snapshot, chop the entire name stored in
3008 if (flags->istail) {
3010 * A filesystem was specified with -e. We want to tack on only
3011 * the tail of the sent snapshot path.
3013 if (strchr(tosnap, '@')) {
3014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3015 "argument - snapshot not allowed with -e"));
3016 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3019 chopprefix = strrchr(sendfs, '/');
3021 if (chopprefix == NULL) {
3023 * The tail is the poolname, so we need to
3024 * prepend a path separator.
3026 int len = strlen(drrb->drr_toname);
3027 cp = malloc(len + 2);
3029 (void) strcpy(&cp[1], drrb->drr_toname);
3032 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3034 } else if (flags->isprefix) {
3036 * A filesystem was specified with -d. We want to tack on
3037 * everything but the first element of the sent snapshot path
3038 * (all but the pool name).
3040 if (strchr(tosnap, '@')) {
3041 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3042 "argument - snapshot not allowed with -d"));
3043 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3046 chopprefix = strchr(drrb->drr_toname, '/');
3047 if (chopprefix == NULL)
3048 chopprefix = strchr(drrb->drr_toname, '@');
3049 } else if (strchr(tosnap, '@') == NULL) {
3051 * If a filesystem was specified without -d or -e, we want to
3052 * tack on everything after the fs specified by 'zfs send'.
3054 chopprefix = drrb->drr_toname + strlen(sendfs);
3056 /* A snapshot was specified as an exact path (no -d or -e). */
3058 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3059 "cannot specify snapshot name for multi-snapshot "
3061 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3063 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3066 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3067 ASSERT(chopprefix > drrb->drr_toname);
3068 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
3069 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3070 chopprefix[0] == '\0');
3073 * Determine name of destination snapshot, store in zc_value.
3075 (void) strcpy(zc.zc_value, tosnap);
3076 (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
3078 if (zfs_ioctl_version == ZFS_IOCVER_UNDEF)
3079 zfs_ioctl_version = get_zfs_ioctl_version();
3081 * For forward compatibility hide tosnap in zc_value
3083 if (zfs_ioctl_version < ZFS_IOCVER_LZC)
3084 (void) strcpy(zc.zc_value + strlen(zc.zc_value) + 1, tosnap);
3087 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
3088 zcmd_free_nvlists(&zc);
3089 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3093 * Determine the name of the origin snapshot, store in zc_string.
3095 if (drrb->drr_flags & DRR_FLAG_CLONE) {
3096 if (guid_to_name(hdl, zc.zc_value,
3097 drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3098 zcmd_free_nvlists(&zc);
3099 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3100 "local origin for clone %s does not exist"),
3102 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3105 (void) printf("found clone origin %s\n", zc.zc_string);
3106 } else if (originsnap) {
3107 (void) strncpy(zc.zc_string, originsnap, ZFS_MAXNAMELEN);
3109 (void) printf("using provided clone origin %s\n",
3113 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3114 DMU_BACKUP_FEATURE_RESUMING;
3115 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
3116 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3118 if (stream_wantsnewfs) {
3120 * if the parent fs does not exist, look for it based on
3121 * the parent snap GUID
3123 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3124 "cannot receive new filesystem stream"));
3126 (void) strcpy(zc.zc_name, zc.zc_value);
3127 cp = strrchr(zc.zc_name, '/');
3131 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3132 char suffix[ZFS_MAXNAMELEN];
3133 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
3134 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
3135 B_FALSE, zc.zc_value) == 0) {
3136 *strchr(zc.zc_value, '@') = '\0';
3137 (void) strcat(zc.zc_value, suffix);
3142 * if the fs does not exist, look for it based on the
3145 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3146 "cannot receive incremental stream"));
3148 (void) strcpy(zc.zc_name, zc.zc_value);
3149 *strchr(zc.zc_name, '@') = '\0';
3152 * If the exact receive path was specified and this is the
3153 * topmost path in the stream, then if the fs does not exist we
3154 * should look no further.
3156 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
3157 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
3158 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3159 char snap[ZFS_MAXNAMELEN];
3160 (void) strcpy(snap, strchr(zc.zc_value, '@'));
3161 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
3162 B_FALSE, zc.zc_value) == 0) {
3163 *strchr(zc.zc_value, '@') = '\0';
3164 (void) strcat(zc.zc_value, snap);
3169 (void) strcpy(zc.zc_name, zc.zc_value);
3170 *strchr(zc.zc_name, '@') = '\0';
3172 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3176 * Destination fs exists. It must be one of these cases:
3177 * - an incremental send stream
3178 * - the stream specifies a new fs (full stream or clone)
3179 * and they want us to blow away the existing fs (and
3180 * have therefore specified -F and removed any snapshots)
3181 * - we are resuming a failed receive.
3183 if (stream_wantsnewfs) {
3184 if (!flags->force) {
3185 zcmd_free_nvlists(&zc);
3186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3187 "destination '%s' exists\n"
3188 "must specify -F to overwrite it"),
3190 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3192 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3194 zcmd_free_nvlists(&zc);
3195 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3196 "destination has snapshots (eg. %s)\n"
3197 "must destroy them to overwrite it"),
3199 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3203 if ((zhp = zfs_open(hdl, zc.zc_name,
3204 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
3205 zcmd_free_nvlists(&zc);
3209 if (stream_wantsnewfs &&
3210 zhp->zfs_dmustats.dds_origin[0]) {
3211 zcmd_free_nvlists(&zc);
3213 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3214 "destination '%s' is a clone\n"
3215 "must destroy it to overwrite it"),
3217 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3220 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3221 stream_wantsnewfs) {
3222 /* We can't do online recv in this case */
3223 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3226 zcmd_free_nvlists(&zc);
3229 if (changelist_prefix(clp) != 0) {
3230 changelist_free(clp);
3232 zcmd_free_nvlists(&zc);
3238 * If we are resuming a newfs, set newfs here so that we will
3239 * mount it if the recv succeeds this time. We can tell
3240 * that it was a newfs on the first recv because the fs
3241 * itself will be inconsistent (if the fs existed when we
3242 * did the first recv, we would have received it into
3245 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
3251 * Destination filesystem does not exist. Therefore we better
3252 * be creating a new filesystem (either from a full backup, or
3253 * a clone). It would therefore be invalid if the user
3254 * specified only the pool name (i.e. if the destination name
3255 * contained no slash character).
3257 if (!stream_wantsnewfs ||
3258 (cp = strrchr(zc.zc_name, '/')) == NULL) {
3259 zcmd_free_nvlists(&zc);
3260 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3261 "destination '%s' does not exist"), zc.zc_name);
3262 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3266 * Trim off the final dataset component so we perform the
3267 * recvbackup ioctl to the filesystems's parent.
3271 if (flags->isprefix && !flags->istail && !flags->dryrun &&
3272 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
3273 zcmd_free_nvlists(&zc);
3274 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3280 zc.zc_begin_record = *drr_noswap;
3281 zc.zc_cookie = infd;
3282 zc.zc_guid = flags->force;
3283 zc.zc_resumable = flags->resumable;
3284 if (flags->verbose) {
3285 (void) printf("%s %s stream of %s into %s\n",
3286 flags->dryrun ? "would receive" : "receiving",
3287 drrb->drr_fromguid ? "incremental" : "full",
3288 drrb->drr_toname, zc.zc_value);
3289 (void) fflush(stdout);
3292 if (flags->dryrun) {
3293 zcmd_free_nvlists(&zc);
3294 return (recv_skip(hdl, infd, flags->byteswap));
3297 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
3298 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
3299 zc.zc_cleanup_fd = cleanup_fd;
3300 zc.zc_action_handle = *action_handlep;
3302 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
3303 ioctl_errno = errno;
3304 prop_errflags = (zprop_errflags_t)zc.zc_obj;
3307 nvlist_t *prop_errors;
3308 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
3309 zc.zc_nvlist_dst_size, &prop_errors, 0));
3311 nvpair_t *prop_err = NULL;
3313 while ((prop_err = nvlist_next_nvpair(prop_errors,
3314 prop_err)) != NULL) {
3319 prop = zfs_name_to_prop(nvpair_name(prop_err));
3320 (void) nvpair_value_int32(prop_err, &intval);
3321 if (strcmp(nvpair_name(prop_err),
3322 ZPROP_N_MORE_ERRORS) == 0) {
3323 trunc_prop_errs(intval);
3325 } else if (snapname == NULL || finalsnap == NULL ||
3326 strcmp(finalsnap, snapname) == 0 ||
3327 strcmp(nvpair_name(prop_err),
3328 zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
3330 * Skip the special case of, for example,
3331 * "refquota", errors on intermediate
3332 * snapshots leading up to a final one.
3333 * That's why we have all of the checks above.
3335 * See zfs_ioctl.c's extract_delay_props() for
3336 * a list of props which can fail on
3337 * intermediate snapshots, but shouldn't
3338 * affect the overall receive.
3340 (void) snprintf(tbuf, sizeof (tbuf),
3341 dgettext(TEXT_DOMAIN,
3342 "cannot receive %s property on %s"),
3343 nvpair_name(prop_err), zc.zc_name);
3344 zfs_setprop_error(hdl, prop, intval, tbuf);
3347 nvlist_free(prop_errors);
3350 zc.zc_nvlist_dst = 0;
3351 zc.zc_nvlist_dst_size = 0;
3352 zcmd_free_nvlists(&zc);
3354 if (err == 0 && snapprops_nvlist) {
3355 zfs_cmd_t zc2 = { 0 };
3357 (void) strcpy(zc2.zc_name, zc.zc_value);
3358 zc2.zc_cookie = B_TRUE; /* received */
3359 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
3360 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
3361 zcmd_free_nvlists(&zc2);
3365 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3367 * It may be that this snapshot already exists,
3368 * in which case we want to consume & ignore it
3369 * rather than failing.
3371 avl_tree_t *local_avl;
3372 nvlist_t *local_nv, *fs;
3373 cp = strchr(zc.zc_value, '@');
3376 * XXX Do this faster by just iterating over snaps in
3377 * this fs. Also if zc_value does not exist, we will
3378 * get a strange "does not exist" error message.
3381 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3382 &local_nv, &local_avl) == 0) {
3384 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
3385 fsavl_destroy(local_avl);
3386 nvlist_free(local_nv);
3389 if (flags->verbose) {
3390 (void) printf("snap %s already exists; "
3391 "ignoring\n", zc.zc_value);
3393 err = ioctl_err = recv_skip(hdl, infd,
3400 if (ioctl_err != 0) {
3401 switch (ioctl_errno) {
3403 cp = strchr(zc.zc_value, '@');
3405 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3406 "most recent snapshot of %s does not\n"
3407 "match incremental source"), zc.zc_value);
3408 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3412 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3413 "destination %s has been modified\n"
3414 "since most recent snapshot"), zc.zc_name);
3415 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3418 cp = strchr(zc.zc_value, '@');
3420 /* it's the containing fs that exists */
3423 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3424 "destination already exists"));
3425 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
3426 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
3431 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3434 recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
3435 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3439 "pool must be upgraded to receive this stream."));
3440 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3443 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3444 "destination %s space quota exceeded"), zc.zc_name);
3445 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3448 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3453 * Mount the target filesystem (if created). Also mount any
3454 * children of the target filesystem if we did a replication
3455 * receive (indicated by stream_avl being non-NULL).
3457 cp = strchr(zc.zc_value, '@');
3458 if (cp && (ioctl_err == 0 || !newfs)) {
3462 h = zfs_open(hdl, zc.zc_value,
3463 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3465 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3467 } else if (newfs || stream_avl) {
3469 * Track the first/top of hierarchy fs,
3470 * for mounting and sharing later.
3472 if (top_zfs && *top_zfs == NULL)
3473 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3481 if (!flags->nomount)
3482 err |= changelist_postfix(clp);
3483 changelist_free(clp);
3486 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3487 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3488 "failed to clear unreceived properties on %s"),
3490 (void) fprintf(stderr, "\n");
3492 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3493 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3494 "failed to restore original properties on %s"),
3496 (void) fprintf(stderr, "\n");
3499 if (err || ioctl_err)
3502 *action_handlep = zc.zc_action_handle;
3504 if (flags->verbose) {
3507 uint64_t bytes = zc.zc_cookie;
3508 time_t delta = time(NULL) - begin_time;
3511 zfs_nicenum(bytes, buf1, sizeof (buf1));
3512 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3514 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3522 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3523 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
3524 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3525 uint64_t *action_handlep, const char *finalsnap)
3528 dmu_replay_record_t drr, drr_noswap;
3529 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3531 zio_cksum_t zcksum = { 0 };
3532 uint64_t featureflags;
3535 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3538 if (flags->isprefix &&
3539 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3540 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3541 "(%s) does not exist"), tosnap);
3542 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3545 !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
3546 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
3547 "(%s) does not exist"), originsnap);
3548 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3551 /* read in the BEGIN record */
3552 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3556 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3557 /* It's the double end record at the end of a package */
3561 /* the kernel needs the non-byteswapped begin record */
3564 flags->byteswap = B_FALSE;
3565 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3567 * We computed the checksum in the wrong byteorder in
3568 * recv_read() above; do it again correctly.
3570 bzero(&zcksum, sizeof (zio_cksum_t));
3571 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
3572 flags->byteswap = B_TRUE;
3574 drr.drr_type = BSWAP_32(drr.drr_type);
3575 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3576 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3577 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3578 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3579 drrb->drr_type = BSWAP_32(drrb->drr_type);
3580 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3581 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3582 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3585 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3586 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3587 "stream (bad magic number)"));
3588 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3591 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3592 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3594 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3595 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3596 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3597 "stream has unsupported feature, feature flags = %lx"),
3599 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3602 if (strchr(drrb->drr_toname, '@') == NULL) {
3603 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3604 "stream (bad snapshot name)"));
3605 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3608 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3609 char nonpackage_sendfs[ZFS_MAXNAMELEN];
3610 if (sendfs == NULL) {
3612 * We were not called from zfs_receive_package(). Get
3613 * the fs specified by 'zfs send'.
3616 (void) strlcpy(nonpackage_sendfs,
3617 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
3618 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3620 sendfs = nonpackage_sendfs;
3621 VERIFY(finalsnap == NULL);
3623 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3624 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
3625 cleanup_fd, action_handlep, finalsnap));
3627 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3628 DMU_COMPOUNDSTREAM);
3629 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
3630 &zcksum, top_zfs, cleanup_fd, action_handlep));
3635 * Restores a backup of tosnap from the file descriptor specified by infd.
3636 * Return 0 on total success, -2 if some things couldn't be
3637 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3638 * (-1 will override -2, if -1 and the resumable flag was specified the
3639 * transfer can be resumed if the sending side supports it).
3642 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
3643 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
3645 char *top_zfs = NULL;
3648 uint64_t action_handle = 0;
3649 char *originsnap = NULL;
3651 err = nvlist_lookup_string(props, "origin", &originsnap);
3652 if (err && err != ENOENT)
3656 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3657 VERIFY(cleanup_fd >= 0);
3659 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
3660 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL);
3662 VERIFY(0 == close(cleanup_fd));
3664 if (err == 0 && !flags->nomount && top_zfs) {
3666 prop_changelist_t *clp;
3668 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3670 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3671 CL_GATHER_MOUNT_ALWAYS, 0);
3674 /* mount and share received datasets */
3675 err = changelist_postfix(clp);
3676 changelist_free(clp);
3679 if (zhp == NULL || clp == NULL || err)