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>.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
41 #include <sys/mount.h>
42 #include <sys/mntent.h>
43 #include <sys/mnttab.h>
45 #include <sys/debug.h>
52 #include <libzfs_core.h>
54 #include "zfs_namecheck.h"
56 #include "zfs_fletcher.h"
57 #include "libzfs_impl.h"
58 #include <sys/zio_checksum.h>
60 #include <sys/socket.h>
62 /* in libzfs_dataset.c */
63 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
65 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t *,
66 int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *);
68 static const zio_cksum_t zero_cksum = { { 0 } };
70 typedef struct dedup_arg {
73 libzfs_handle_t *dedup_hdl;
76 typedef struct progress_arg {
79 boolean_t pa_parsable;
82 typedef struct dataref {
88 typedef struct dedup_entry {
89 struct dedup_entry *dde_next;
90 zio_cksum_t dde_chksum;
95 #define MAX_DDT_PHYSMEM_PERCENT 20
96 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
98 typedef struct dedup_table {
99 dedup_entry_t **dedup_hash_array;
100 umem_cache_t *ddecache;
101 uint64_t max_ddt_size; /* max dedup table size in bytes */
102 uint64_t cur_ddt_size; /* current dedup table size in bytes */
109 high_order_bit(uint64_t n)
113 for (count = 0; n != 0; count++)
119 ssread(void *buf, size_t len, FILE *stream)
123 if ((outlen = fread(buf, len, 1, stream)) == 0)
130 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
131 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
135 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
136 if (ddt->ddt_full == B_FALSE) {
137 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
138 "Dedup table full. Deduplication will continue "
139 "with existing table entries"));
140 ddt->ddt_full = B_TRUE;
145 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
147 assert(*ddepp == NULL);
148 dde->dde_next = NULL;
149 dde->dde_chksum = *cs;
150 dde->dde_prop = prop;
153 ddt->cur_ddt_size += sizeof (dedup_entry_t);
159 * Using the specified dedup table, do a lookup for an entry with
160 * the checksum cs. If found, return the block's reference info
161 * in *dr. Otherwise, insert a new entry in the dedup table, using
162 * the reference information specified by *dr.
164 * return value: true - entry was found
165 * false - entry was not found
168 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
169 uint64_t prop, dataref_t *dr)
172 dedup_entry_t **ddepp;
174 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
176 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
177 ddepp = &((*ddepp)->dde_next)) {
178 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
179 (*ddepp)->dde_prop == prop) {
180 *dr = (*ddepp)->dde_ref;
184 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
189 cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd)
191 fletcher_4_incremental_native(buf, len, zc);
192 return (write(outfd, buf, len));
196 * This function is started in a separate thread when the dedup option
197 * has been requested. The main send thread determines the list of
198 * snapshots to be included in the send stream and makes the ioctl calls
199 * for each one. But instead of having the ioctl send the output to the
200 * the output fd specified by the caller of zfs_send()), the
201 * ioctl is told to direct the output to a pipe, which is read by the
202 * alternate thread running THIS function. This function does the
204 * 1. building a dedup table (the DDT)
205 * 2. doing checksums on each data block and inserting a record in the DDT
206 * 3. looking for matching checksums, and
207 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
208 * a duplicate block is found.
209 * The output of this function then goes to the output fd requested
210 * by the caller of zfs_send().
215 dedup_arg_t *dda = arg;
216 char *buf = malloc(1<<20);
217 dmu_replay_record_t thedrr;
218 dmu_replay_record_t *drr = &thedrr;
219 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
220 struct drr_end *drre = &thedrr.drr_u.drr_end;
221 struct drr_object *drro = &thedrr.drr_u.drr_object;
222 struct drr_write *drrw = &thedrr.drr_u.drr_write;
223 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
224 struct drr_write_embedded *drrwe = &thedrr.drr_u.drr_write_embedded;
227 dmu_replay_record_t wbr_drr = {0};
228 struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref;
230 zio_cksum_t stream_cksum;
231 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
235 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100,
236 SMALLEST_POSSIBLE_MAX_DDT_MB<<20);
238 numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t));
241 * numbuckets must be a power of 2. Increase number to
242 * a power of 2 if necessary.
244 if (!ISP2(numbuckets))
245 numbuckets = 1 << high_order_bit(numbuckets);
247 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
248 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
249 NULL, NULL, NULL, NULL, NULL, 0);
250 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
251 ddt.numhashbits = high_order_bit(numbuckets) - 1;
252 ddt.ddt_full = B_FALSE;
254 /* Initialize the write-by-reference block. */
255 wbr_drr.drr_type = DRR_WRITE_BYREF;
256 wbr_drr.drr_payloadlen = 0;
258 outfd = dda->outputfd;
259 ofp = fdopen(dda->inputfd, "r");
260 while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) {
262 switch (drr->drr_type) {
266 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
268 /* set the DEDUP feature flag for this stream */
269 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
270 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
271 DMU_BACKUP_FEATURE_DEDUPPROPS);
272 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
274 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
275 &stream_cksum, outfd) == -1)
277 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
278 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) {
279 int sz = drr->drr_payloadlen;
285 (void) ssread(buf, sz, ofp);
288 if (cksum_and_write(buf, sz, &stream_cksum,
297 /* use the recalculated checksum */
298 ZIO_SET_CHECKSUM(&drre->drr_checksum,
299 stream_cksum.zc_word[0], stream_cksum.zc_word[1],
300 stream_cksum.zc_word[2], stream_cksum.zc_word[3]);
301 if ((write(outfd, drr,
302 sizeof (dmu_replay_record_t))) == -1)
309 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
310 &stream_cksum, outfd) == -1)
312 if (drro->drr_bonuslen > 0) {
314 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
316 if (cksum_and_write(buf,
317 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
318 &stream_cksum, outfd) == -1)
326 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
327 &stream_cksum, outfd) == -1)
329 (void) ssread(buf, drrs->drr_length, ofp);
330 if (cksum_and_write(buf, drrs->drr_length,
331 &stream_cksum, outfd) == -1)
336 case DRR_FREEOBJECTS:
338 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
339 &stream_cksum, outfd) == -1)
348 (void) ssread(buf, drrw->drr_length, ofp);
351 * Use the existing checksum if it's dedup-capable,
352 * else calculate a SHA256 checksum for it.
355 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
357 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
358 zio_cksum_t tmpsha256;
360 zio_checksum_SHA256(buf,
361 drrw->drr_length, &tmpsha256);
363 drrw->drr_key.ddk_cksum.zc_word[0] =
364 BE_64(tmpsha256.zc_word[0]);
365 drrw->drr_key.ddk_cksum.zc_word[1] =
366 BE_64(tmpsha256.zc_word[1]);
367 drrw->drr_key.ddk_cksum.zc_word[2] =
368 BE_64(tmpsha256.zc_word[2]);
369 drrw->drr_key.ddk_cksum.zc_word[3] =
370 BE_64(tmpsha256.zc_word[3]);
371 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
372 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
375 dataref.ref_guid = drrw->drr_toguid;
376 dataref.ref_object = drrw->drr_object;
377 dataref.ref_offset = drrw->drr_offset;
379 if (ddt_update(dda->dedup_hdl, &ddt,
380 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
382 /* block already present in stream */
383 wbr_drrr->drr_object = drrw->drr_object;
384 wbr_drrr->drr_offset = drrw->drr_offset;
385 wbr_drrr->drr_length = drrw->drr_length;
386 wbr_drrr->drr_toguid = drrw->drr_toguid;
387 wbr_drrr->drr_refguid = dataref.ref_guid;
388 wbr_drrr->drr_refobject =
390 wbr_drrr->drr_refoffset =
393 wbr_drrr->drr_checksumtype =
394 drrw->drr_checksumtype;
395 wbr_drrr->drr_checksumflags =
396 drrw->drr_checksumtype;
397 wbr_drrr->drr_key.ddk_cksum =
398 drrw->drr_key.ddk_cksum;
399 wbr_drrr->drr_key.ddk_prop =
400 drrw->drr_key.ddk_prop;
402 if (cksum_and_write(&wbr_drr,
403 sizeof (dmu_replay_record_t), &stream_cksum,
407 /* block not previously seen */
408 if (cksum_and_write(drr,
409 sizeof (dmu_replay_record_t), &stream_cksum,
412 if (cksum_and_write(buf,
414 &stream_cksum, outfd) == -1)
420 case DRR_WRITE_EMBEDDED:
422 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
423 &stream_cksum, outfd) == -1)
426 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
427 if (cksum_and_write(buf,
428 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
429 &stream_cksum, outfd) == -1)
436 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
437 &stream_cksum, outfd) == -1)
443 (void) printf("INVALID record type 0x%x\n",
445 /* should never happen, so assert */
450 umem_cache_destroy(ddt.ddecache);
451 free(ddt.dedup_hash_array);
459 * Routines for dealing with the AVL tree of fs-nvlists
461 typedef struct fsavl_node {
469 fsavl_compare(const void *arg1, const void *arg2)
471 const fsavl_node_t *fn1 = arg1;
472 const fsavl_node_t *fn2 = arg2;
474 if (fn1->fn_guid > fn2->fn_guid)
476 else if (fn1->fn_guid < fn2->fn_guid)
483 * Given the GUID of a snapshot, find its containing filesystem and
487 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
489 fsavl_node_t fn_find;
492 fn_find.fn_guid = snapguid;
494 fn = avl_find(avl, &fn_find, NULL);
497 *snapname = fn->fn_snapname;
498 return (fn->fn_nvfs);
504 fsavl_destroy(avl_tree_t *avl)
513 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
520 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
523 fsavl_create(nvlist_t *fss)
526 nvpair_t *fselem = NULL;
528 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
531 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
532 offsetof(fsavl_node_t, fn_node));
534 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
535 nvlist_t *nvfs, *snaps;
536 nvpair_t *snapelem = NULL;
538 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
539 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
542 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
546 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
547 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
548 fsavl_destroy(fsavl);
552 fn->fn_snapname = nvpair_name(snapelem);
556 * Note: if there are multiple snaps with the
557 * same GUID, we ignore all but one.
559 if (avl_find(fsavl, fn, NULL) == NULL)
570 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
572 typedef struct send_data {
573 uint64_t parent_fromsnap_guid;
574 nvlist_t *parent_snaps;
577 const char *fromsnap;
584 * The header nvlist is of the following format:
587 * "fromsnap" -> string (if incremental)
591 * "name" -> string (full name; for debugging)
592 * "parentfromsnap" -> number (guid of fromsnap in parent)
594 * "props" -> { name -> value (only if set here) }
595 * "snaps" -> { name (lastname) -> number (guid) }
596 * "snapprops" -> { name (lastname) -> { name -> value } }
598 * "origin" -> number (guid) (if clone)
599 * "sent" -> boolean (not on-disk)
607 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
610 send_iterate_snap(zfs_handle_t *zhp, void *arg)
612 send_data_t *sd = arg;
613 uint64_t guid = zhp->zfs_dmustats.dds_guid;
616 boolean_t isfromsnap, istosnap;
618 snapname = strrchr(zhp->zfs_name, '@')+1;
619 isfromsnap = (sd->fromsnap != NULL &&
620 strcmp(sd->fromsnap, snapname) == 0);
621 istosnap = (sd->tosnap != NULL && (strcmp(sd->tosnap, snapname) == 0));
624 * NB: if there is no fromsnap here (it's a newly created fs in
625 * an incremental replication), we will substitute the tosnap.
627 if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap)) {
628 sd->parent_fromsnap_guid = guid;
631 if (!sd->recursive) {
632 if (!sd->seenfrom && isfromsnap) {
633 sd->seenfrom = B_TRUE;
638 if (sd->seento || !sd->seenfrom) {
647 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
649 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
650 send_iterate_prop(zhp, nv);
651 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
659 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
661 nvpair_t *elem = NULL;
663 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
664 char *propname = nvpair_name(elem);
665 zfs_prop_t prop = zfs_name_to_prop(propname);
668 if (!zfs_prop_user(propname)) {
670 * Realistically, this should never happen. However,
671 * we want the ability to add DSL properties without
672 * needing to make incompatible version changes. We
673 * need to ignore unknown properties to allow older
674 * software to still send datasets containing these
675 * properties, with the unknown properties elided.
677 if (prop == ZPROP_INVAL)
680 if (zfs_prop_readonly(prop))
684 verify(nvpair_value_nvlist(elem, &propnv) == 0);
685 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
686 prop == ZFS_PROP_REFQUOTA ||
687 prop == ZFS_PROP_REFRESERVATION) {
690 verify(nvlist_lookup_uint64(propnv,
691 ZPROP_VALUE, &value) == 0);
692 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
695 * May have no source before SPA_VERSION_RECVD_PROPS,
696 * but is still modifiable.
698 if (nvlist_lookup_string(propnv,
699 ZPROP_SOURCE, &source) == 0) {
700 if ((strcmp(source, zhp->zfs_name) != 0) &&
702 ZPROP_SOURCE_VAL_RECVD) != 0))
707 if (nvlist_lookup_string(propnv,
708 ZPROP_SOURCE, &source) != 0)
710 if ((strcmp(source, zhp->zfs_name) != 0) &&
711 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
715 if (zfs_prop_user(propname) ||
716 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
718 verify(nvlist_lookup_string(propnv,
719 ZPROP_VALUE, &value) == 0);
720 VERIFY(0 == nvlist_add_string(nv, propname, value));
723 verify(nvlist_lookup_uint64(propnv,
724 ZPROP_VALUE, &value) == 0);
725 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
731 * recursively generate nvlists describing datasets. See comment
732 * for the data structure send_data_t above for description of contents
736 send_iterate_fs(zfs_handle_t *zhp, void *arg)
738 send_data_t *sd = arg;
741 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
742 uint64_t guid = zhp->zfs_dmustats.dds_guid;
745 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
746 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
747 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
748 sd->parent_fromsnap_guid));
750 if (zhp->zfs_dmustats.dds_origin[0]) {
751 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
752 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
755 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
756 origin->zfs_dmustats.dds_guid));
759 /* iterate over props */
760 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
761 send_iterate_prop(zhp, nv);
762 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
765 /* iterate over snaps, and set sd->parent_fromsnap_guid */
766 sd->parent_fromsnap_guid = 0;
767 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
768 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
769 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd);
770 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
771 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
772 nvlist_free(sd->parent_snaps);
773 nvlist_free(sd->snapprops);
775 /* add this fs to nvlist */
776 (void) snprintf(guidstring, sizeof (guidstring),
777 "0x%llx", (longlong_t)guid);
778 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
781 /* iterate over children */
783 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
785 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
792 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
793 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
796 send_data_t sd = { 0 };
799 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
801 return (EZFS_BADTYPE);
803 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
804 sd.fromsnap = fromsnap;
806 sd.recursive = recursive;
808 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
816 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
827 * Routines specific to "zfs send"
829 typedef struct send_dump_data {
830 /* these are all just the short snapname (the part after the @) */
831 const char *fromsnap;
833 char prevsnap[ZFS_MAXNAMELEN];
834 uint64_t prevsnap_obj;
835 boolean_t seenfrom, seento, replicate, doall, fromorigin;
836 boolean_t verbose, dryrun, parsable, progress, embed_data;
842 snapfilter_cb_t *filter_cb;
845 char holdtag[ZFS_MAXNAMELEN];
851 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
852 boolean_t fromorigin, uint64_t *sizep)
854 zfs_cmd_t zc = {"\0"};
855 libzfs_handle_t *hdl = zhp->zfs_hdl;
857 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
858 assert(fromsnap_obj == 0 || !fromorigin);
860 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
861 zc.zc_obj = fromorigin;
862 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
863 zc.zc_fromobj = fromsnap_obj;
864 zc.zc_guid = 1; /* estimate flag */
866 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
868 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
869 "warning: cannot estimate space for '%s'"), zhp->zfs_name);
873 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
874 "not an earlier snapshot from the same fs"));
875 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
878 if (zfs_dataset_exists(hdl, zc.zc_name,
879 ZFS_TYPE_SNAPSHOT)) {
880 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
881 "incremental source (@%s) does not exist"),
884 return (zfs_error(hdl, EZFS_NOENT, errbuf));
897 zfs_error_aux(hdl, strerror(errno));
898 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
901 return (zfs_standard_error(hdl, errno, errbuf));
905 *sizep = zc.zc_objset_type;
911 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
912 * NULL) to the file descriptor specified by outfd.
915 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
916 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
919 zfs_cmd_t zc = {"\0"};
920 libzfs_handle_t *hdl = zhp->zfs_hdl;
923 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
924 assert(fromsnap_obj == 0 || !fromorigin);
926 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
927 zc.zc_cookie = outfd;
928 zc.zc_obj = fromorigin;
929 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
930 zc.zc_fromobj = fromsnap_obj;
933 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
934 if (fromsnap && fromsnap[0] != '\0') {
935 VERIFY(0 == nvlist_add_string(thisdbg,
936 "fromsnap", fromsnap));
939 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
941 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
942 "warning: cannot send '%s'"), zhp->zfs_name);
944 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
946 VERIFY(0 == nvlist_add_nvlist(debugnv,
947 zhp->zfs_name, thisdbg));
949 nvlist_free(thisdbg);
953 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
954 "not an earlier snapshot from the same fs"));
955 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
958 if (zfs_dataset_exists(hdl, zc.zc_name,
959 ZFS_TYPE_SNAPSHOT)) {
960 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
961 "incremental source (@%s) does not exist"),
964 return (zfs_error(hdl, EZFS_NOENT, errbuf));
977 zfs_error_aux(hdl, strerror(errno));
978 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
981 return (zfs_standard_error(hdl, errno, errbuf));
986 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
987 nvlist_free(thisdbg);
993 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
995 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
998 * zfs_send() only sets snapholds for sends that need them,
999 * e.g. replication and doall.
1001 if (sdd->snapholds == NULL)
1004 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1008 send_progress_thread(void *arg)
1010 progress_arg_t *pa = arg;
1012 zfs_cmd_t zc = {"\0"};
1013 zfs_handle_t *zhp = pa->pa_zhp;
1014 libzfs_handle_t *hdl = zhp->zfs_hdl;
1015 unsigned long long bytes;
1021 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1022 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1024 if (!pa->pa_parsable)
1025 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1028 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1033 zc.zc_cookie = pa->pa_fd;
1034 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1035 return ((void *)-1);
1039 bytes = zc.zc_cookie;
1041 if (pa->pa_parsable) {
1042 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1043 tm->tm_hour, tm->tm_min, tm->tm_sec,
1044 bytes, zhp->zfs_name);
1046 zfs_nicenum(bytes, buf, sizeof (buf));
1047 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1048 tm->tm_hour, tm->tm_min, tm->tm_sec,
1049 buf, zhp->zfs_name);
1055 dump_snapshot(zfs_handle_t *zhp, void *arg)
1057 send_dump_data_t *sdd = arg;
1058 progress_arg_t pa = { 0 };
1062 boolean_t isfromsnap, istosnap, fromorigin;
1063 boolean_t exclude = B_FALSE;
1066 thissnap = strchr(zhp->zfs_name, '@') + 1;
1067 isfromsnap = (sdd->fromsnap != NULL &&
1068 strcmp(sdd->fromsnap, thissnap) == 0);
1070 if (!sdd->seenfrom && isfromsnap) {
1071 gather_holds(zhp, sdd);
1072 sdd->seenfrom = B_TRUE;
1073 (void) strcpy(sdd->prevsnap, thissnap);
1074 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1079 if (sdd->seento || !sdd->seenfrom) {
1084 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1086 sdd->seento = B_TRUE;
1088 if (!sdd->doall && !isfromsnap && !istosnap) {
1089 if (sdd->replicate) {
1091 nvlist_t *snapprops;
1093 * Filter out all intermediate snapshots except origin
1094 * snapshots needed to replicate clones.
1096 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1097 zhp->zfs_dmustats.dds_guid, &snapname);
1099 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1100 "snapprops", &snapprops));
1101 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1102 thissnap, &snapprops));
1103 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1110 * If a filter function exists, call it to determine whether
1111 * this snapshot will be sent.
1113 if (exclude || (sdd->filter_cb != NULL &&
1114 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1116 * This snapshot is filtered out. Don't send it, and don't
1117 * set prevsnap_obj, so it will be as if this snapshot didn't
1118 * exist, and the next accepted snapshot will be sent as
1119 * an incremental from the last accepted one, or as the
1120 * first (and full) snapshot in the case of a replication,
1121 * non-incremental send.
1127 gather_holds(zhp, sdd);
1128 fromorigin = sdd->prevsnap[0] == '\0' &&
1129 (sdd->fromorigin || sdd->replicate);
1133 err = estimate_ioctl(zhp, sdd->prevsnap_obj,
1136 if (sdd->parsable) {
1137 if (sdd->prevsnap[0] != '\0') {
1138 (void) fprintf(stderr, "incremental\t%s\t%s",
1139 sdd->prevsnap, zhp->zfs_name);
1141 (void) fprintf(stderr, "full\t%s",
1145 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1146 "send from @%s to %s"),
1147 sdd->prevsnap, zhp->zfs_name);
1150 if (sdd->parsable) {
1151 (void) fprintf(stderr, "\t%llu\n",
1155 zfs_nicenum(size, buf, sizeof (buf));
1156 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1157 " estimated size is %s\n"), buf);
1161 (void) fprintf(stderr, "\n");
1167 * If progress reporting is requested, spawn a new thread to
1168 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1170 if (sdd->progress) {
1172 pa.pa_fd = sdd->outfd;
1173 pa.pa_parsable = sdd->parsable;
1175 if ((err = pthread_create(&tid, NULL,
1176 send_progress_thread, &pa))) {
1182 enum lzc_send_flags flags = 0;
1183 if (sdd->embed_data)
1184 flags |= LZC_SEND_FLAG_EMBED_DATA;
1186 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1187 fromorigin, sdd->outfd, flags, sdd->debugnv);
1189 if (sdd->progress) {
1190 (void) pthread_cancel(tid);
1191 (void) pthread_join(tid, NULL);
1195 (void) strcpy(sdd->prevsnap, thissnap);
1196 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1202 dump_filesystem(zfs_handle_t *zhp, void *arg)
1205 send_dump_data_t *sdd = arg;
1206 boolean_t missingfrom = B_FALSE;
1207 zfs_cmd_t zc = {"\0"};
1209 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1210 zhp->zfs_name, sdd->tosnap);
1211 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1212 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1213 "WARNING: could not send %s@%s: does not exist\n"),
1214 zhp->zfs_name, sdd->tosnap);
1219 if (sdd->replicate && sdd->fromsnap) {
1221 * If this fs does not have fromsnap, and we're doing
1222 * recursive, we need to send a full stream from the
1223 * beginning (or an incremental from the origin if this
1224 * is a clone). If we're doing non-recursive, then let
1225 * them get the error.
1227 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1228 zhp->zfs_name, sdd->fromsnap);
1229 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1230 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1231 missingfrom = B_TRUE;
1235 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1236 sdd->prevsnap_obj = 0;
1237 if (sdd->fromsnap == NULL || missingfrom)
1238 sdd->seenfrom = B_TRUE;
1240 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1241 if (!sdd->seenfrom) {
1242 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1243 "WARNING: could not send %s@%s:\n"
1244 "incremental source (%s@%s) does not exist\n"),
1245 zhp->zfs_name, sdd->tosnap,
1246 zhp->zfs_name, sdd->fromsnap);
1248 } else if (!sdd->seento) {
1249 if (sdd->fromsnap) {
1250 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1251 "WARNING: could not send %s@%s:\n"
1252 "incremental source (%s@%s) "
1253 "is not earlier than it\n"),
1254 zhp->zfs_name, sdd->tosnap,
1255 zhp->zfs_name, sdd->fromsnap);
1257 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1259 "could not send %s@%s: does not exist\n"),
1260 zhp->zfs_name, sdd->tosnap);
1269 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1271 send_dump_data_t *sdd = arg;
1273 boolean_t needagain, progress;
1275 if (!sdd->replicate)
1276 return (dump_filesystem(rzhp, sdd));
1278 /* Mark the clone origin snapshots. */
1279 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1280 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1282 uint64_t origin_guid = 0;
1284 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1285 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1286 if (origin_guid != 0) {
1288 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1289 origin_guid, &snapname);
1290 if (origin_nv != NULL) {
1291 nvlist_t *snapprops;
1292 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1293 "snapprops", &snapprops));
1294 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1295 snapname, &snapprops));
1296 VERIFY(0 == nvlist_add_boolean(
1297 snapprops, "is_clone_origin"));
1302 needagain = progress = B_FALSE;
1303 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1304 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1305 nvlist_t *fslist, *parent_nv;
1309 uint64_t origin_guid = 0;
1310 uint64_t parent_guid = 0;
1312 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1313 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1316 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1317 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1318 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1321 if (parent_guid != 0) {
1322 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1323 if (!nvlist_exists(parent_nv, "sent")) {
1324 /* parent has not been sent; skip this one */
1330 if (origin_guid != 0) {
1331 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1333 if (origin_nv != NULL &&
1334 !nvlist_exists(origin_nv, "sent")) {
1336 * origin has not been sent yet;
1344 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1347 err = dump_filesystem(zhp, sdd);
1348 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1359 /* clean out the sent flags in case we reuse this fss */
1360 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1361 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1364 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1365 (void) nvlist_remove_all(fslist, "sent");
1372 * Generate a send stream for the dataset identified by the argument zhp.
1374 * The content of the send stream is the snapshot identified by
1375 * 'tosnap'. Incremental streams are requested in two ways:
1376 * - from the snapshot identified by "fromsnap" (if non-null) or
1377 * - from the origin of the dataset identified by zhp, which must
1378 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1381 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1382 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1383 * if "replicate" is set. If "doall" is set, dump all the intermediate
1384 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1385 * case too. If "props" is set, send properties.
1388 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1389 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1390 void *cb_arg, nvlist_t **debugnvp)
1393 send_dump_data_t sdd = { 0 };
1395 nvlist_t *fss = NULL;
1396 avl_tree_t *fsavl = NULL;
1397 static uint64_t holdseq;
1401 dedup_arg_t dda = { 0 };
1402 int featureflags = 0;
1404 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1405 "cannot send '%s'"), zhp->zfs_name);
1407 if (fromsnap && fromsnap[0] == '\0') {
1408 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1409 "zero-length incremental source"));
1410 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1413 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1415 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1416 if (version >= ZPL_VERSION_SA) {
1417 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1421 if (flags->dedup && !flags->dryrun) {
1422 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1423 DMU_BACKUP_FEATURE_DEDUPPROPS);
1424 if ((err = socketpair(AF_UNIX, SOCK_STREAM, 0, pipefd))) {
1425 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1426 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1429 dda.outputfd = outfd;
1430 dda.inputfd = pipefd[1];
1431 dda.dedup_hdl = zhp->zfs_hdl;
1432 if ((err = pthread_create(&tid, NULL, cksummer, &dda))) {
1433 (void) close(pipefd[0]);
1434 (void) close(pipefd[1]);
1435 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1436 return (zfs_error(zhp->zfs_hdl,
1437 EZFS_THREADCREATEFAILED, errbuf));
1441 if (flags->replicate || flags->doall || flags->props) {
1442 dmu_replay_record_t drr = { 0 };
1443 char *packbuf = NULL;
1445 zio_cksum_t zc = { { 0 } };
1447 if (flags->replicate || flags->props) {
1450 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1452 VERIFY(0 == nvlist_add_string(hdrnv,
1453 "fromsnap", fromsnap));
1455 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1456 if (!flags->replicate) {
1457 VERIFY(0 == nvlist_add_boolean(hdrnv,
1461 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1462 fromsnap, tosnap, flags->replicate, &fss, &fsavl);
1465 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1466 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1476 if (!flags->dryrun) {
1477 /* write first begin record */
1478 drr.drr_type = DRR_BEGIN;
1479 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1480 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1481 drr_versioninfo, DMU_COMPOUNDSTREAM);
1482 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1483 drr_versioninfo, featureflags);
1484 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1485 sizeof (drr.drr_u.drr_begin.drr_toname),
1486 "%s@%s", zhp->zfs_name, tosnap);
1487 drr.drr_payloadlen = buflen;
1488 err = cksum_and_write(&drr, sizeof (drr), &zc, outfd);
1490 /* write header nvlist */
1491 if (err != -1 && packbuf != NULL) {
1492 err = cksum_and_write(packbuf, buflen, &zc,
1501 /* write end record */
1502 bzero(&drr, sizeof (drr));
1503 drr.drr_type = DRR_END;
1504 drr.drr_u.drr_end.drr_checksum = zc;
1505 err = write(outfd, &drr, sizeof (drr));
1515 /* dump each stream */
1516 sdd.fromsnap = fromsnap;
1517 sdd.tosnap = tosnap;
1519 sdd.outfd = pipefd[0];
1522 sdd.replicate = flags->replicate;
1523 sdd.doall = flags->doall;
1524 sdd.fromorigin = flags->fromorigin;
1527 sdd.verbose = flags->verbose;
1528 sdd.parsable = flags->parsable;
1529 sdd.progress = flags->progress;
1530 sdd.dryrun = flags->dryrun;
1531 sdd.embed_data = flags->embed_data;
1532 sdd.filter_cb = filter_func;
1533 sdd.filter_cb_arg = cb_arg;
1535 sdd.debugnv = *debugnvp;
1538 * Some flags require that we place user holds on the datasets that are
1539 * being sent so they don't get destroyed during the send. We can skip
1540 * this step if the pool is imported read-only since the datasets cannot
1543 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1544 ZPOOL_PROP_READONLY, NULL) &&
1545 zfs_spa_version(zhp, &spa_version) == 0 &&
1546 spa_version >= SPA_VERSION_USERREFS &&
1547 (flags->doall || flags->replicate)) {
1549 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1550 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1551 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
1552 if (sdd.cleanup_fd < 0) {
1556 sdd.snapholds = fnvlist_alloc();
1558 sdd.cleanup_fd = -1;
1559 sdd.snapholds = NULL;
1561 if (flags->verbose || sdd.snapholds != NULL) {
1563 * Do a verbose no-op dry run to get all the verbose output
1564 * or to gather snapshot hold's before generating any data,
1565 * then do a non-verbose real run to generate the streams.
1567 sdd.dryrun = B_TRUE;
1568 err = dump_filesystems(zhp, &sdd);
1573 if (flags->verbose) {
1574 if (flags->parsable) {
1575 (void) fprintf(stderr, "size\t%llu\n",
1576 (longlong_t)sdd.size);
1579 zfs_nicenum(sdd.size, buf, sizeof (buf));
1580 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1581 "total estimated size is %s\n"), buf);
1585 /* Ensure no snaps found is treated as an error. */
1591 /* Skip the second run if dryrun was requested. */
1595 if (sdd.snapholds != NULL) {
1596 err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
1600 fnvlist_free(sdd.snapholds);
1601 sdd.snapholds = NULL;
1604 sdd.dryrun = B_FALSE;
1605 sdd.verbose = B_FALSE;
1608 err = dump_filesystems(zhp, &sdd);
1609 fsavl_destroy(fsavl);
1612 /* Ensure no snaps found is treated as an error. */
1613 if (err == 0 && !sdd.seento)
1618 (void) pthread_cancel(tid);
1619 (void) close(pipefd[0]);
1620 (void) pthread_join(tid, NULL);
1623 if (sdd.cleanup_fd != -1) {
1624 VERIFY(0 == close(sdd.cleanup_fd));
1625 sdd.cleanup_fd = -1;
1628 if (!flags->dryrun && (flags->replicate || flags->doall ||
1631 * write final end record. NB: want to do this even if
1632 * there was some error, because it might not be totally
1635 dmu_replay_record_t drr = { 0 };
1636 drr.drr_type = DRR_END;
1637 if (write(outfd, &drr, sizeof (drr)) == -1) {
1638 return (zfs_standard_error(zhp->zfs_hdl,
1643 return (err || sdd.err);
1646 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1648 fsavl_destroy(fsavl);
1650 fnvlist_free(sdd.snapholds);
1652 if (sdd.cleanup_fd != -1)
1653 VERIFY(0 == close(sdd.cleanup_fd));
1655 (void) pthread_cancel(tid);
1656 (void) close(pipefd[0]);
1657 (void) pthread_join(tid, NULL);
1663 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
1664 enum lzc_send_flags flags)
1667 libzfs_handle_t *hdl = zhp->zfs_hdl;
1670 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1671 "warning: cannot send '%s'"), zhp->zfs_name);
1673 err = lzc_send(zhp->zfs_name, from, fd, flags);
1677 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1678 "not an earlier snapshot from the same fs"));
1679 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1683 if (lzc_exists(zhp->zfs_name)) {
1684 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1685 "incremental source (%s) does not exist"),
1688 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1691 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1692 "target is busy; if a filesystem, "
1693 "it must not be mounted"));
1694 return (zfs_error(hdl, EZFS_BUSY, errbuf));
1707 zfs_error_aux(hdl, strerror(errno));
1708 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1711 return (zfs_standard_error(hdl, errno, errbuf));
1718 * Routines specific to "zfs recv"
1722 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1723 boolean_t byteswap, zio_cksum_t *zc)
1730 rv = read(fd, cp, len);
1735 if (rv < 0 || len != 0) {
1736 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1737 "failed to read from stream"));
1738 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1739 "cannot receive")));
1744 fletcher_4_incremental_byteswap(buf, ilen, zc);
1746 fletcher_4_incremental_native(buf, ilen, zc);
1752 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1753 boolean_t byteswap, zio_cksum_t *zc)
1758 buf = zfs_alloc(hdl, len);
1762 err = recv_read(hdl, fd, buf, len, byteswap, zc);
1768 err = nvlist_unpack(buf, len, nvp, 0);
1771 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1772 "stream (malformed nvlist)"));
1779 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
1780 int baselen, char *newname, recvflags_t *flags)
1783 zfs_cmd_t zc = {"\0"};
1785 prop_changelist_t *clp;
1788 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1791 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1792 flags->force ? MS_FORCE : 0);
1796 err = changelist_prefix(clp);
1800 zc.zc_objset_type = DMU_OST_ZFS;
1801 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1804 (void) strcpy(newname, tryname);
1806 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
1808 if (flags->verbose) {
1809 (void) printf("attempting rename %s to %s\n",
1810 zc.zc_name, zc.zc_value);
1812 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1814 changelist_rename(clp, name, tryname);
1819 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
1822 (void) snprintf(newname, ZFS_MAXNAMELEN, "%.*srecv-%u-%u",
1823 baselen, name, getpid(), seq);
1824 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
1826 if (flags->verbose) {
1827 (void) printf("failed - trying rename %s to %s\n",
1828 zc.zc_name, zc.zc_value);
1830 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1832 changelist_rename(clp, name, newname);
1833 if (err && flags->verbose) {
1834 (void) printf("failed (%u) - "
1835 "will try again on next pass\n", errno);
1838 } else if (flags->verbose) {
1840 (void) printf("success\n");
1842 (void) printf("failed (%u)\n", errno);
1845 (void) changelist_postfix(clp);
1846 changelist_free(clp);
1852 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1853 char *newname, recvflags_t *flags)
1855 zfs_cmd_t zc = {"\0"};
1857 prop_changelist_t *clp;
1859 boolean_t defer = B_FALSE;
1862 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1865 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1866 flags->force ? MS_FORCE : 0);
1867 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1868 zfs_spa_version(zhp, &spa_version) == 0 &&
1869 spa_version >= SPA_VERSION_USERREFS)
1874 err = changelist_prefix(clp);
1878 zc.zc_objset_type = DMU_OST_ZFS;
1879 zc.zc_defer_destroy = defer;
1880 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1883 (void) printf("attempting destroy %s\n", zc.zc_name);
1884 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1887 (void) printf("success\n");
1888 changelist_remove(clp, zc.zc_name);
1891 (void) changelist_postfix(clp);
1892 changelist_free(clp);
1895 * Deferred destroy might destroy the snapshot or only mark it to be
1896 * destroyed later, and it returns success in either case.
1898 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
1899 ZFS_TYPE_SNAPSHOT))) {
1900 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1906 typedef struct guid_to_name_data {
1910 } guid_to_name_data_t;
1913 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1915 guid_to_name_data_t *gtnd = arg;
1918 if (gtnd->skip != NULL &&
1919 strcmp(zhp->zfs_name, gtnd->skip) == 0) {
1923 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1924 (void) strcpy(gtnd->name, zhp->zfs_name);
1929 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1935 * Attempt to find the local dataset associated with this guid. In the case of
1936 * multiple matches, we attempt to find the "best" match by searching
1937 * progressively larger portions of the hierarchy. This allows one to send a
1938 * tree of datasets individually and guarantee that we will find the source
1939 * guid within that hierarchy, even if there are multiple matches elsewhere.
1942 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1945 /* exhaustive search all local snapshots */
1946 char pname[ZFS_MAXNAMELEN];
1947 guid_to_name_data_t gtnd;
1956 (void) strlcpy(pname, parent, sizeof (pname));
1959 * Search progressively larger portions of the hierarchy. This will
1960 * select the "most local" version of the origin snapshot in the case
1961 * that there are multiple matching snapshots in the system.
1963 while ((cp = strrchr(pname, '/')) != NULL) {
1965 /* Chop off the last component and open the parent */
1967 zhp = make_dataset_handle(hdl, pname);
1972 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1978 * Remember the dataset that we already searched, so we
1979 * skip it next time through.
1988 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1989 * guid1 is after guid2.
1992 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1993 uint64_t guid1, uint64_t guid2)
1996 char *fsname, *snapname;
1997 char buf[ZFS_MAXNAMELEN];
1999 zfs_handle_t *guid1hdl, *guid2hdl;
2000 uint64_t create1, create2;
2007 nvfs = fsavl_find(avl, guid1, &snapname);
2008 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2009 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2010 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2011 if (guid1hdl == NULL)
2014 nvfs = fsavl_find(avl, guid2, &snapname);
2015 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2016 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2017 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2018 if (guid2hdl == NULL) {
2019 zfs_close(guid1hdl);
2023 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2024 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2026 if (create1 < create2)
2028 else if (create1 > create2)
2033 zfs_close(guid1hdl);
2034 zfs_close(guid2hdl);
2040 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2041 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2044 nvlist_t *local_nv, *deleted = NULL;
2045 avl_tree_t *local_avl;
2046 nvpair_t *fselem, *nextfselem;
2048 char newname[ZFS_MAXNAMELEN];
2051 boolean_t needagain, progress, recursive;
2054 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2056 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2063 needagain = progress = B_FALSE;
2065 VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
2067 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2068 recursive, &local_nv, &local_avl)) != 0)
2072 * Process deletes and renames
2074 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2075 fselem; fselem = nextfselem) {
2076 nvlist_t *nvfs, *snaps;
2077 nvlist_t *stream_nvfs = NULL;
2078 nvpair_t *snapelem, *nextsnapelem;
2079 uint64_t fromguid = 0;
2080 uint64_t originguid = 0;
2081 uint64_t stream_originguid = 0;
2082 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2083 char *fsname, *stream_fsname;
2085 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2087 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2088 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2089 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2090 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2091 &parent_fromsnap_guid));
2092 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2095 * First find the stream's fs, so we can check for
2096 * a different origin (due to "zfs promote")
2098 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2099 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2102 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2103 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2105 if (stream_nvfs != NULL)
2109 /* check for promote */
2110 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2111 &stream_originguid);
2112 if (stream_nvfs && originguid != stream_originguid) {
2113 switch (created_before(hdl, local_avl,
2114 stream_originguid, originguid)) {
2117 zfs_cmd_t zc = {"\0"};
2118 nvlist_t *origin_nvfs;
2119 char *origin_fsname;
2122 (void) printf("promoting %s\n", fsname);
2124 origin_nvfs = fsavl_find(local_avl, originguid,
2126 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2127 "name", &origin_fsname));
2128 (void) strlcpy(zc.zc_value, origin_fsname,
2129 sizeof (zc.zc_value));
2130 (void) strlcpy(zc.zc_name, fsname,
2131 sizeof (zc.zc_name));
2132 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2140 fsavl_destroy(local_avl);
2141 nvlist_free(local_nv);
2145 * We had/have the wrong origin, therefore our
2146 * list of snapshots is wrong. Need to handle
2147 * them on the next pass.
2153 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2154 snapelem; snapelem = nextsnapelem) {
2156 char *stream_snapname;
2157 nvlist_t *found, *props;
2159 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2161 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2162 found = fsavl_find(stream_avl, thisguid,
2165 /* check for delete */
2166 if (found == NULL) {
2167 char name[ZFS_MAXNAMELEN];
2172 (void) snprintf(name, sizeof (name), "%s@%s",
2173 fsname, nvpair_name(snapelem));
2175 error = recv_destroy(hdl, name,
2176 strlen(fsname)+1, newname, flags);
2181 sprintf(guidname, "%lu", thisguid);
2182 nvlist_add_boolean(deleted, guidname);
2186 stream_nvfs = found;
2188 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2189 &props) && 0 == nvlist_lookup_nvlist(props,
2190 stream_snapname, &props)) {
2191 zfs_cmd_t zc = {"\0"};
2193 zc.zc_cookie = B_TRUE; /* received */
2194 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2195 "%s@%s", fsname, nvpair_name(snapelem));
2196 if (zcmd_write_src_nvlist(hdl, &zc,
2198 (void) zfs_ioctl(hdl,
2199 ZFS_IOC_SET_PROP, &zc);
2200 zcmd_free_nvlists(&zc);
2204 /* check for different snapname */
2205 if (strcmp(nvpair_name(snapelem),
2206 stream_snapname) != 0) {
2207 char name[ZFS_MAXNAMELEN];
2208 char tryname[ZFS_MAXNAMELEN];
2210 (void) snprintf(name, sizeof (name), "%s@%s",
2211 fsname, nvpair_name(snapelem));
2212 (void) snprintf(tryname, sizeof (name), "%s@%s",
2213 fsname, stream_snapname);
2215 error = recv_rename(hdl, name, tryname,
2216 strlen(fsname)+1, newname, flags);
2223 if (strcmp(stream_snapname, fromsnap) == 0)
2224 fromguid = thisguid;
2227 /* check for delete */
2228 if (stream_nvfs == NULL) {
2232 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2238 sprintf(guidname, "%lu", parent_fromsnap_guid);
2239 nvlist_add_boolean(deleted, guidname);
2243 if (fromguid == 0) {
2244 if (flags->verbose) {
2245 (void) printf("local fs %s does not have "
2246 "fromsnap (%s in stream); must have "
2247 "been deleted locally; ignoring\n",
2253 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2254 "name", &stream_fsname));
2255 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2256 "parentfromsnap", &stream_parent_fromsnap_guid));
2258 s1 = strrchr(fsname, '/');
2259 s2 = strrchr(stream_fsname, '/');
2262 * Check if we're going to rename based on parent guid change
2263 * and the current parent guid was also deleted. If it was then
2264 * rename will fail and is likely unneeded, so avoid this and
2265 * force an early retry to determine the new
2266 * parent_fromsnap_guid.
2268 if (stream_parent_fromsnap_guid != 0 &&
2269 parent_fromsnap_guid != 0 &&
2270 stream_parent_fromsnap_guid != parent_fromsnap_guid) {
2271 sprintf(guidname, "%lu", parent_fromsnap_guid);
2272 if (nvlist_exists(deleted, guidname)) {
2280 * Check for rename. If the exact receive path is specified, it
2281 * does not count as a rename, but we still need to check the
2282 * datasets beneath it.
2284 if ((stream_parent_fromsnap_guid != 0 &&
2285 parent_fromsnap_guid != 0 &&
2286 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2287 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2288 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2290 char tryname[ZFS_MAXNAMELEN];
2292 parent = fsavl_find(local_avl,
2293 stream_parent_fromsnap_guid, NULL);
2295 * NB: parent might not be found if we used the
2296 * tosnap for stream_parent_fromsnap_guid,
2297 * because the parent is a newly-created fs;
2298 * we'll be able to rename it after we recv the
2301 if (parent != NULL) {
2304 VERIFY(0 == nvlist_lookup_string(parent, "name",
2306 (void) snprintf(tryname, sizeof (tryname),
2307 "%s%s", pname, strrchr(stream_fsname, '/'));
2310 if (flags->verbose) {
2311 (void) printf("local fs %s new parent "
2312 "not found\n", fsname);
2318 error = recv_rename(hdl, fsname, tryname,
2319 strlen(tofs)+1, newname, flags);
2321 if (renamed != NULL && newname[0] != '\0') {
2322 VERIFY(0 == nvlist_add_boolean(renamed,
2334 fsavl_destroy(local_avl);
2335 nvlist_free(local_nv);
2336 nvlist_free(deleted);
2338 if (needagain && progress) {
2339 /* do another pass to fix up temporary names */
2341 (void) printf("another pass:\n");
2349 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2350 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2351 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2353 nvlist_t *stream_nv = NULL;
2354 avl_tree_t *stream_avl = NULL;
2355 char *fromsnap = NULL;
2357 char tofs[ZFS_MAXNAMELEN];
2358 char sendfs[ZFS_MAXNAMELEN];
2360 dmu_replay_record_t drre;
2362 boolean_t anyerr = B_FALSE;
2363 boolean_t softerr = B_FALSE;
2364 boolean_t recursive;
2366 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2369 assert(drr->drr_type == DRR_BEGIN);
2370 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2371 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2372 DMU_COMPOUNDSTREAM);
2375 * Read in the nvlist from the stream.
2377 if (drr->drr_payloadlen != 0) {
2378 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2379 &stream_nv, flags->byteswap, zc);
2381 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2386 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2389 if (recursive && strchr(destname, '@')) {
2390 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2391 "cannot specify snapshot name for multi-snapshot stream"));
2392 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2397 * Read in the end record and verify checksum.
2399 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2400 flags->byteswap, NULL)))
2402 if (flags->byteswap) {
2403 drre.drr_type = BSWAP_32(drre.drr_type);
2404 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2405 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2406 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2407 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2408 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2409 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2410 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2411 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2413 if (drre.drr_type != DRR_END) {
2414 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2417 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2418 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2419 "incorrect header checksum"));
2420 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2424 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2426 if (drr->drr_payloadlen != 0) {
2427 nvlist_t *stream_fss;
2429 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2431 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2432 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2433 "couldn't allocate avl tree"));
2434 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2438 if (fromsnap != NULL) {
2439 nvlist_t *renamed = NULL;
2440 nvpair_t *pair = NULL;
2442 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2443 if (flags->isprefix) {
2444 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2447 if (flags->istail) {
2448 cp = strrchr(drrb->drr_toname, '/');
2450 (void) strlcat(tofs, "/",
2454 i = (cp - drrb->drr_toname);
2457 i = strcspn(drrb->drr_toname, "/@");
2459 /* zfs_receive_one() will create_parents() */
2460 (void) strlcat(tofs, &drrb->drr_toname[i],
2462 *strchr(tofs, '@') = '\0';
2465 if (recursive && !flags->dryrun && !flags->nomount) {
2466 VERIFY(0 == nvlist_alloc(&renamed,
2467 NV_UNIQUE_NAME, 0));
2470 softerr = recv_incremental_replication(hdl, tofs, flags,
2471 stream_nv, stream_avl, renamed);
2473 /* Unmount renamed filesystems before receiving. */
2474 while ((pair = nvlist_next_nvpair(renamed,
2477 prop_changelist_t *clp = NULL;
2479 zhp = zfs_open(hdl, nvpair_name(pair),
2480 ZFS_TYPE_FILESYSTEM);
2482 clp = changelist_gather(zhp,
2483 ZFS_PROP_MOUNTPOINT, 0, 0);
2487 changelist_prefix(clp);
2488 changelist_free(clp);
2493 nvlist_free(renamed);
2498 * Get the fs specified by the first path in the stream (the top level
2499 * specified by 'zfs send') and pass it to each invocation of
2500 * zfs_receive_one().
2502 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2504 if ((cp = strchr(sendfs, '@')) != NULL)
2507 /* Finally, receive each contained stream */
2510 * we should figure out if it has a recoverable
2511 * error, in which case do a recv_skip() and drive on.
2512 * Note, if we fail due to already having this guid,
2513 * zfs_receive_one() will take care of it (ie,
2514 * recv_skip() and return 0).
2516 error = zfs_receive_impl(hdl, destname, flags, fd,
2517 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2519 if (error == ENODATA) {
2524 } while (error == 0);
2526 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2528 * Now that we have the fs's they sent us, try the
2531 softerr = recv_incremental_replication(hdl, tofs, flags,
2532 stream_nv, stream_avl, NULL);
2536 fsavl_destroy(stream_avl);
2538 nvlist_free(stream_nv);
2547 trunc_prop_errs(int truncated)
2549 ASSERT(truncated != 0);
2552 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2553 "1 more property could not be set\n"));
2555 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2556 "%d more properties could not be set\n"), truncated);
2560 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2562 dmu_replay_record_t *drr;
2563 void *buf = malloc(1<<20);
2566 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2567 "cannot receive:"));
2569 /* XXX would be great to use lseek if possible... */
2572 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2573 byteswap, NULL) == 0) {
2575 drr->drr_type = BSWAP_32(drr->drr_type);
2577 switch (drr->drr_type) {
2579 /* NB: not to be used on v2 stream packages */
2580 if (drr->drr_payloadlen != 0) {
2581 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2582 "invalid substream header"));
2583 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2593 drr->drr_u.drr_object.drr_bonuslen =
2594 BSWAP_32(drr->drr_u.drr_object.
2597 (void) recv_read(hdl, fd, buf,
2598 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2604 drr->drr_u.drr_write.drr_length =
2605 BSWAP_64(drr->drr_u.drr_write.drr_length);
2607 (void) recv_read(hdl, fd, buf,
2608 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2612 drr->drr_u.drr_write.drr_length =
2613 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2615 (void) recv_read(hdl, fd, buf,
2616 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2618 case DRR_WRITE_EMBEDDED:
2620 drr->drr_u.drr_write_embedded.drr_psize =
2621 BSWAP_32(drr->drr_u.drr_write_embedded.
2624 (void) recv_read(hdl, fd, buf,
2625 P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
2628 case DRR_WRITE_BYREF:
2629 case DRR_FREEOBJECTS:
2634 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2635 "invalid record type"));
2636 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2645 * Restores a backup of tosnap from the file descriptor specified by infd.
2648 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2649 recvflags_t *flags, dmu_replay_record_t *drr,
2650 dmu_replay_record_t *drr_noswap, const char *sendfs,
2651 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2652 uint64_t *action_handlep)
2654 zfs_cmd_t zc = {"\0"};
2656 int ioctl_err, ioctl_errno, err;
2658 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2660 char prop_errbuf[1024];
2661 const char *chopprefix;
2662 boolean_t newfs = B_FALSE;
2663 boolean_t stream_wantsnewfs;
2664 uint64_t parent_snapguid = 0;
2665 prop_changelist_t *clp = NULL;
2666 nvlist_t *snapprops_nvlist = NULL;
2667 zprop_errflags_t prop_errflags;
2668 boolean_t recursive;
2670 begin_time = time(NULL);
2672 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2675 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2678 if (stream_avl != NULL) {
2680 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2685 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2687 err = nvlist_lookup_nvlist(fs, "props", &props);
2689 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2691 if (flags->canmountoff) {
2692 VERIFY(0 == nvlist_add_uint64(props,
2693 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2695 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2699 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2700 VERIFY(0 == nvlist_lookup_nvlist(props,
2701 snapname, &snapprops_nvlist));
2711 * Determine how much of the snapshot name stored in the stream
2712 * we are going to tack on to the name they specified on the
2713 * command line, and how much we are going to chop off.
2715 * If they specified a snapshot, chop the entire name stored in
2718 if (flags->istail) {
2720 * A filesystem was specified with -e. We want to tack on only
2721 * the tail of the sent snapshot path.
2723 if (strchr(tosnap, '@')) {
2724 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2725 "argument - snapshot not allowed with -e"));
2726 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2729 chopprefix = strrchr(sendfs, '/');
2731 if (chopprefix == NULL) {
2733 * The tail is the poolname, so we need to
2734 * prepend a path separator.
2736 int len = strlen(drrb->drr_toname);
2737 cp = malloc(len + 2);
2739 (void) strcpy(&cp[1], drrb->drr_toname);
2742 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2744 } else if (flags->isprefix) {
2746 * A filesystem was specified with -d. We want to tack on
2747 * everything but the first element of the sent snapshot path
2748 * (all but the pool name).
2750 if (strchr(tosnap, '@')) {
2751 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2752 "argument - snapshot not allowed with -d"));
2753 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2756 chopprefix = strchr(drrb->drr_toname, '/');
2757 if (chopprefix == NULL)
2758 chopprefix = strchr(drrb->drr_toname, '@');
2759 } else if (strchr(tosnap, '@') == NULL) {
2761 * If a filesystem was specified without -d or -e, we want to
2762 * tack on everything after the fs specified by 'zfs send'.
2764 chopprefix = drrb->drr_toname + strlen(sendfs);
2766 /* A snapshot was specified as an exact path (no -d or -e). */
2768 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2769 "cannot specify snapshot name for multi-snapshot "
2771 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2773 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
2776 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
2777 ASSERT(chopprefix > drrb->drr_toname);
2778 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
2779 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
2780 chopprefix[0] == '\0');
2783 * Determine name of destination snapshot, store in zc_value.
2785 (void) strcpy(zc.zc_value, tosnap);
2786 (void) strlcat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
2788 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
2789 zcmd_free_nvlists(&zc);
2790 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2794 * Determine the name of the origin snapshot, store in zc_string.
2796 if (drrb->drr_flags & DRR_FLAG_CLONE) {
2797 if (guid_to_name(hdl, zc.zc_value,
2798 drrb->drr_fromguid, zc.zc_string) != 0) {
2799 zcmd_free_nvlists(&zc);
2800 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2801 "local origin for clone %s does not exist"),
2803 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2806 (void) printf("found clone origin %s\n", zc.zc_string);
2809 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
2810 (drrb->drr_flags & DRR_FLAG_CLONE));
2812 if (stream_wantsnewfs) {
2814 * if the parent fs does not exist, look for it based on
2815 * the parent snap GUID
2817 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2818 "cannot receive new filesystem stream"));
2820 (void) strcpy(zc.zc_name, zc.zc_value);
2821 cp = strrchr(zc.zc_name, '/');
2825 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2826 char suffix[ZFS_MAXNAMELEN];
2827 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
2828 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
2829 zc.zc_value) == 0) {
2830 *strchr(zc.zc_value, '@') = '\0';
2831 (void) strcat(zc.zc_value, suffix);
2836 * if the fs does not exist, look for it based on the
2839 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2840 "cannot receive incremental stream"));
2842 (void) strcpy(zc.zc_name, zc.zc_value);
2843 *strchr(zc.zc_name, '@') = '\0';
2846 * If the exact receive path was specified and this is the
2847 * topmost path in the stream, then if the fs does not exist we
2848 * should look no further.
2850 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
2851 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
2852 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2853 char snap[ZFS_MAXNAMELEN];
2854 (void) strcpy(snap, strchr(zc.zc_value, '@'));
2855 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
2856 zc.zc_value) == 0) {
2857 *strchr(zc.zc_value, '@') = '\0';
2858 (void) strcat(zc.zc_value, snap);
2863 (void) strcpy(zc.zc_name, zc.zc_value);
2864 *strchr(zc.zc_name, '@') = '\0';
2866 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2870 * Destination fs exists. Therefore this should either
2871 * be an incremental, or the stream specifies a new fs
2872 * (full stream or clone) and they want us to blow it
2873 * away (and have therefore specified -F and removed any
2876 if (stream_wantsnewfs) {
2877 if (!flags->force) {
2878 zcmd_free_nvlists(&zc);
2879 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2880 "destination '%s' exists\n"
2881 "must specify -F to overwrite it"),
2883 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2885 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2887 zcmd_free_nvlists(&zc);
2888 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2889 "destination has snapshots (eg. %s)\n"
2890 "must destroy them to overwrite it"),
2892 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2896 if ((zhp = zfs_open(hdl, zc.zc_name,
2897 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2898 zcmd_free_nvlists(&zc);
2902 if (stream_wantsnewfs &&
2903 zhp->zfs_dmustats.dds_origin[0]) {
2904 zcmd_free_nvlists(&zc);
2906 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2907 "destination '%s' is a clone\n"
2908 "must destroy it to overwrite it"),
2910 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2913 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
2914 stream_wantsnewfs) {
2915 /* We can't do online recv in this case */
2916 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
2919 zcmd_free_nvlists(&zc);
2922 if (changelist_prefix(clp) != 0) {
2923 changelist_free(clp);
2925 zcmd_free_nvlists(&zc);
2932 * Destination filesystem does not exist. Therefore we better
2933 * be creating a new filesystem (either from a full backup, or
2934 * a clone). It would therefore be invalid if the user
2935 * specified only the pool name (i.e. if the destination name
2936 * contained no slash character).
2938 if (!stream_wantsnewfs ||
2939 (cp = strrchr(zc.zc_name, '/')) == NULL) {
2940 zcmd_free_nvlists(&zc);
2941 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2942 "destination '%s' does not exist"), zc.zc_name);
2943 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2947 * Trim off the final dataset component so we perform the
2948 * recvbackup ioctl to the filesystems's parent.
2952 if (flags->isprefix && !flags->istail && !flags->dryrun &&
2953 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
2954 zcmd_free_nvlists(&zc);
2955 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
2961 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
2962 zc.zc_cookie = infd;
2963 zc.zc_guid = flags->force;
2964 if (flags->verbose) {
2965 (void) printf("%s %s stream of %s into %s\n",
2966 flags->dryrun ? "would receive" : "receiving",
2967 drrb->drr_fromguid ? "incremental" : "full",
2968 drrb->drr_toname, zc.zc_value);
2969 (void) fflush(stdout);
2972 if (flags->dryrun) {
2973 zcmd_free_nvlists(&zc);
2974 return (recv_skip(hdl, infd, flags->byteswap));
2977 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
2978 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
2979 zc.zc_cleanup_fd = cleanup_fd;
2980 zc.zc_action_handle = *action_handlep;
2982 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
2983 ioctl_errno = errno;
2984 prop_errflags = (zprop_errflags_t)zc.zc_obj;
2987 nvlist_t *prop_errors;
2988 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
2989 zc.zc_nvlist_dst_size, &prop_errors, 0));
2991 nvpair_t *prop_err = NULL;
2993 while ((prop_err = nvlist_next_nvpair(prop_errors,
2994 prop_err)) != NULL) {
2999 prop = zfs_name_to_prop(nvpair_name(prop_err));
3000 (void) nvpair_value_int32(prop_err, &intval);
3001 if (strcmp(nvpair_name(prop_err),
3002 ZPROP_N_MORE_ERRORS) == 0) {
3003 trunc_prop_errs(intval);
3006 (void) snprintf(tbuf, sizeof (tbuf),
3007 dgettext(TEXT_DOMAIN,
3008 "cannot receive %s property on %s"),
3009 nvpair_name(prop_err), zc.zc_name);
3010 zfs_setprop_error(hdl, prop, intval, tbuf);
3013 nvlist_free(prop_errors);
3016 zc.zc_nvlist_dst = 0;
3017 zc.zc_nvlist_dst_size = 0;
3018 zcmd_free_nvlists(&zc);
3020 if (err == 0 && snapprops_nvlist) {
3021 zfs_cmd_t zc2 = {"\0"};
3023 (void) strcpy(zc2.zc_name, zc.zc_value);
3024 zc2.zc_cookie = B_TRUE; /* received */
3025 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
3026 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
3027 zcmd_free_nvlists(&zc2);
3031 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3033 * It may be that this snapshot already exists,
3034 * in which case we want to consume & ignore it
3035 * rather than failing.
3037 avl_tree_t *local_avl;
3038 nvlist_t *local_nv, *fs;
3039 cp = strchr(zc.zc_value, '@');
3042 * XXX Do this faster by just iterating over snaps in
3043 * this fs. Also if zc_value does not exist, we will
3044 * get a strange "does not exist" error message.
3047 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3048 &local_nv, &local_avl) == 0) {
3050 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
3051 fsavl_destroy(local_avl);
3052 nvlist_free(local_nv);
3055 if (flags->verbose) {
3056 (void) printf("snap %s already exists; "
3057 "ignoring\n", zc.zc_value);
3059 err = ioctl_err = recv_skip(hdl, infd,
3066 if (ioctl_err != 0) {
3067 switch (ioctl_errno) {
3069 cp = strchr(zc.zc_value, '@');
3071 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3072 "most recent snapshot of %s does not\n"
3073 "match incremental source"), zc.zc_value);
3074 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3078 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3079 "destination %s has been modified\n"
3080 "since most recent snapshot"), zc.zc_name);
3081 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3084 cp = strchr(zc.zc_value, '@');
3086 /* it's the containing fs that exists */
3089 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3090 "destination already exists"));
3091 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
3092 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
3097 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3100 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3101 "invalid stream (checksum mismatch)"));
3102 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3106 "pool must be upgraded to receive this stream."));
3107 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3110 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3111 "destination %s space quota exceeded"), zc.zc_name);
3112 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3115 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3120 * Mount the target filesystem (if created). Also mount any
3121 * children of the target filesystem if we did a replication
3122 * receive (indicated by stream_avl being non-NULL).
3124 cp = strchr(zc.zc_value, '@');
3125 if (cp && (ioctl_err == 0 || !newfs)) {
3129 h = zfs_open(hdl, zc.zc_value,
3130 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3132 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3134 } else if (newfs || stream_avl) {
3136 * Track the first/top of hierarchy fs,
3137 * for mounting and sharing later.
3139 if (top_zfs && *top_zfs == NULL)
3140 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3148 err |= changelist_postfix(clp);
3149 changelist_free(clp);
3152 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3153 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3154 "failed to clear unreceived properties on %s"),
3156 (void) fprintf(stderr, "\n");
3158 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3159 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3160 "failed to restore original properties on %s"),
3162 (void) fprintf(stderr, "\n");
3165 if (err || ioctl_err)
3168 *action_handlep = zc.zc_action_handle;
3170 if (flags->verbose) {
3173 uint64_t bytes = zc.zc_cookie;
3174 time_t delta = time(NULL) - begin_time;
3177 zfs_nicenum(bytes, buf1, sizeof (buf1));
3178 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3180 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3188 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
3189 int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3190 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
3193 dmu_replay_record_t drr, drr_noswap;
3194 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3196 zio_cksum_t zcksum = { { 0 } };
3197 uint64_t featureflags;
3200 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3203 if (flags->isprefix &&
3204 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3205 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3206 "(%s) does not exist"), tosnap);
3207 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3210 /* read in the BEGIN record */
3211 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3215 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3216 /* It's the double end record at the end of a package */
3220 /* the kernel needs the non-byteswapped begin record */
3223 flags->byteswap = B_FALSE;
3224 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3226 * We computed the checksum in the wrong byteorder in
3227 * recv_read() above; do it again correctly.
3229 bzero(&zcksum, sizeof (zio_cksum_t));
3230 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
3231 flags->byteswap = B_TRUE;
3233 drr.drr_type = BSWAP_32(drr.drr_type);
3234 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3235 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3236 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3237 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3238 drrb->drr_type = BSWAP_32(drrb->drr_type);
3239 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3240 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3241 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3244 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3245 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3246 "stream (bad magic number)"));
3247 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3250 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3251 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3253 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3254 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3255 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3256 "stream has unsupported feature, feature flags = %lx"),
3258 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3261 if (strchr(drrb->drr_toname, '@') == NULL) {
3262 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3263 "stream (bad snapshot name)"));
3264 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3267 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3268 char nonpackage_sendfs[ZFS_MAXNAMELEN];
3269 if (sendfs == NULL) {
3271 * We were not called from zfs_receive_package(). Get
3272 * the fs specified by 'zfs send'.
3275 (void) strlcpy(nonpackage_sendfs,
3276 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
3277 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3279 sendfs = nonpackage_sendfs;
3281 return (zfs_receive_one(hdl, infd, tosnap, flags,
3282 &drr, &drr_noswap, sendfs, stream_nv, stream_avl,
3283 top_zfs, cleanup_fd, action_handlep));
3285 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3286 DMU_COMPOUNDSTREAM);
3287 return (zfs_receive_package(hdl, infd, tosnap, flags,
3288 &drr, &zcksum, top_zfs, cleanup_fd, action_handlep));
3293 * Restores a backup of tosnap from the file descriptor specified by infd.
3294 * Return 0 on total success, -2 if some things couldn't be
3295 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3296 * (-1 will override -2).
3299 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
3300 int infd, avl_tree_t *stream_avl)
3302 char *top_zfs = NULL;
3305 uint64_t action_handle = 0;
3307 cleanup_fd = open(ZFS_DEV, O_RDWR);
3308 VERIFY(cleanup_fd >= 0);
3310 err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
3311 stream_avl, &top_zfs, cleanup_fd, &action_handle);
3313 VERIFY(0 == close(cleanup_fd));
3315 if (err == 0 && !flags->nomount && top_zfs) {
3317 prop_changelist_t *clp;
3319 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3321 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3322 CL_GATHER_MOUNT_ALWAYS, 0);
3325 /* mount and share received datasets */
3326 err = changelist_postfix(clp);
3327 changelist_free(clp);
3330 if (zhp == NULL || clp == NULL || err)