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>
53 #include <libzfs_core.h>
55 #include "zfs_namecheck.h"
57 #include "zfs_fletcher.h"
58 #include "libzfs_impl.h"
59 #include <sys/zio_checksum.h>
61 #include <sys/socket.h>
63 /* in libzfs_dataset.c */
64 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
66 static int zfs_receive_impl(libzfs_handle_t
*, const char *, recvflags_t
*,
67 int, const char *, nvlist_t
*, avl_tree_t
*, char **, int, uint64_t *);
69 static const zio_cksum_t zero_cksum
= { { 0 } };
71 typedef struct dedup_arg
{
74 libzfs_handle_t
*dedup_hdl
;
77 typedef struct progress_arg
{
80 boolean_t pa_parsable
;
83 typedef struct dataref
{
89 typedef struct dedup_entry
{
90 struct dedup_entry
*dde_next
;
91 zio_cksum_t dde_chksum
;
96 #define MAX_DDT_PHYSMEM_PERCENT 20
97 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
99 typedef struct dedup_table
{
100 dedup_entry_t
**dedup_hash_array
;
101 umem_cache_t
*ddecache
;
102 uint64_t max_ddt_size
; /* max dedup table size in bytes */
103 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
110 high_order_bit(uint64_t n
)
114 for (count
= 0; n
!= 0; count
++)
120 ssread(void *buf
, size_t len
, FILE *stream
)
124 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
131 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
132 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
136 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
137 if (ddt
->ddt_full
== B_FALSE
) {
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "Dedup table full. Deduplication will continue "
140 "with existing table entries"));
141 ddt
->ddt_full
= B_TRUE
;
146 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
148 assert(*ddepp
== NULL
);
149 dde
->dde_next
= NULL
;
150 dde
->dde_chksum
= *cs
;
151 dde
->dde_prop
= prop
;
154 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
160 * Using the specified dedup table, do a lookup for an entry with
161 * the checksum cs. If found, return the block's reference info
162 * in *dr. Otherwise, insert a new entry in the dedup table, using
163 * the reference information specified by *dr.
165 * return value: true - entry was found
166 * false - entry was not found
169 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
170 uint64_t prop
, dataref_t
*dr
)
173 dedup_entry_t
**ddepp
;
175 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
177 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
178 ddepp
= &((*ddepp
)->dde_next
)) {
179 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
180 (*ddepp
)->dde_prop
== prop
) {
181 *dr
= (*ddepp
)->dde_ref
;
185 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
190 cksum_and_write(const void *buf
, uint64_t len
, zio_cksum_t
*zc
, int outfd
)
192 fletcher_4_incremental_native(buf
, len
, zc
);
193 return (write(outfd
, buf
, len
));
197 * This function is started in a separate thread when the dedup option
198 * has been requested. The main send thread determines the list of
199 * snapshots to be included in the send stream and makes the ioctl calls
200 * for each one. But instead of having the ioctl send the output to the
201 * the output fd specified by the caller of zfs_send()), the
202 * ioctl is told to direct the output to a pipe, which is read by the
203 * alternate thread running THIS function. This function does the
205 * 1. building a dedup table (the DDT)
206 * 2. doing checksums on each data block and inserting a record in the DDT
207 * 3. looking for matching checksums, and
208 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
209 * a duplicate block is found.
210 * The output of this function then goes to the output fd requested
211 * by the caller of zfs_send().
216 dedup_arg_t
*dda
= arg
;
217 char *buf
= malloc(1<<20);
218 dmu_replay_record_t thedrr
;
219 dmu_replay_record_t
*drr
= &thedrr
;
220 struct drr_begin
*drrb
= &thedrr
.drr_u
.drr_begin
;
221 struct drr_end
*drre
= &thedrr
.drr_u
.drr_end
;
222 struct drr_object
*drro
= &thedrr
.drr_u
.drr_object
;
223 struct drr_write
*drrw
= &thedrr
.drr_u
.drr_write
;
224 struct drr_spill
*drrs
= &thedrr
.drr_u
.drr_spill
;
225 struct drr_write_embedded
*drrwe
= &thedrr
.drr_u
.drr_write_embedded
;
228 dmu_replay_record_t wbr_drr
= {0};
229 struct drr_write_byref
*wbr_drrr
= &wbr_drr
.drr_u
.drr_write_byref
;
231 zio_cksum_t stream_cksum
;
232 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
236 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
)/100,
237 SMALLEST_POSSIBLE_MAX_DDT_MB
<<20);
239 numbuckets
= ddt
.max_ddt_size
/(sizeof (dedup_entry_t
));
242 * numbuckets must be a power of 2. Increase number to
243 * a power of 2 if necessary.
245 if (!ISP2(numbuckets
))
246 numbuckets
= 1 << high_order_bit(numbuckets
);
248 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
249 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
250 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
251 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
252 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
253 ddt
.ddt_full
= B_FALSE
;
255 /* Initialize the write-by-reference block. */
256 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
257 wbr_drr
.drr_payloadlen
= 0;
259 outfd
= dda
->outputfd
;
260 ofp
= fdopen(dda
->inputfd
, "r");
261 while (ssread(drr
, sizeof (dmu_replay_record_t
), ofp
) != 0) {
263 switch (drr
->drr_type
) {
267 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
269 /* set the DEDUP feature flag for this stream */
270 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
271 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
272 DMU_BACKUP_FEATURE_DEDUPPROPS
);
273 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
275 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
276 &stream_cksum
, outfd
) == -1)
278 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
279 DMU_COMPOUNDSTREAM
&& drr
->drr_payloadlen
!= 0) {
280 int sz
= drr
->drr_payloadlen
;
286 (void) ssread(buf
, sz
, ofp
);
289 if (cksum_and_write(buf
, sz
, &stream_cksum
,
298 /* use the recalculated checksum */
299 ZIO_SET_CHECKSUM(&drre
->drr_checksum
,
300 stream_cksum
.zc_word
[0], stream_cksum
.zc_word
[1],
301 stream_cksum
.zc_word
[2], stream_cksum
.zc_word
[3]);
302 if ((write(outfd
, drr
,
303 sizeof (dmu_replay_record_t
))) == -1)
310 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
311 &stream_cksum
, outfd
) == -1)
313 if (drro
->drr_bonuslen
> 0) {
315 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
317 if (cksum_and_write(buf
,
318 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
319 &stream_cksum
, outfd
) == -1)
327 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
328 &stream_cksum
, outfd
) == -1)
330 (void) ssread(buf
, drrs
->drr_length
, ofp
);
331 if (cksum_and_write(buf
, drrs
->drr_length
,
332 &stream_cksum
, outfd
) == -1)
337 case DRR_FREEOBJECTS
:
339 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
340 &stream_cksum
, outfd
) == -1)
349 (void) ssread(buf
, drrw
->drr_length
, ofp
);
352 * Use the existing checksum if it's dedup-capable,
353 * else calculate a SHA256 checksum for it.
356 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
358 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
359 zio_cksum_t tmpsha256
;
361 zio_checksum_SHA256(buf
,
362 drrw
->drr_length
, &tmpsha256
);
364 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
365 BE_64(tmpsha256
.zc_word
[0]);
366 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
367 BE_64(tmpsha256
.zc_word
[1]);
368 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
369 BE_64(tmpsha256
.zc_word
[2]);
370 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
371 BE_64(tmpsha256
.zc_word
[3]);
372 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
373 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
376 dataref
.ref_guid
= drrw
->drr_toguid
;
377 dataref
.ref_object
= drrw
->drr_object
;
378 dataref
.ref_offset
= drrw
->drr_offset
;
380 if (ddt_update(dda
->dedup_hdl
, &ddt
,
381 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
383 /* block already present in stream */
384 wbr_drrr
->drr_object
= drrw
->drr_object
;
385 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
386 wbr_drrr
->drr_length
= drrw
->drr_length
;
387 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
388 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
389 wbr_drrr
->drr_refobject
=
391 wbr_drrr
->drr_refoffset
=
394 wbr_drrr
->drr_checksumtype
=
395 drrw
->drr_checksumtype
;
396 wbr_drrr
->drr_checksumflags
=
397 drrw
->drr_checksumtype
;
398 wbr_drrr
->drr_key
.ddk_cksum
=
399 drrw
->drr_key
.ddk_cksum
;
400 wbr_drrr
->drr_key
.ddk_prop
=
401 drrw
->drr_key
.ddk_prop
;
403 if (cksum_and_write(&wbr_drr
,
404 sizeof (dmu_replay_record_t
), &stream_cksum
,
408 /* block not previously seen */
409 if (cksum_and_write(drr
,
410 sizeof (dmu_replay_record_t
), &stream_cksum
,
413 if (cksum_and_write(buf
,
415 &stream_cksum
, outfd
) == -1)
421 case DRR_WRITE_EMBEDDED
:
423 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
424 &stream_cksum
, outfd
) == -1)
427 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
428 if (cksum_and_write(buf
,
429 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
430 &stream_cksum
, outfd
) == -1)
437 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
438 &stream_cksum
, outfd
) == -1)
444 (void) printf("INVALID record type 0x%x\n",
446 /* should never happen, so assert */
451 umem_cache_destroy(ddt
.ddecache
);
452 free(ddt
.dedup_hash_array
);
460 * Routines for dealing with the AVL tree of fs-nvlists
462 typedef struct fsavl_node
{
470 fsavl_compare(const void *arg1
, const void *arg2
)
472 const fsavl_node_t
*fn1
= arg1
;
473 const fsavl_node_t
*fn2
= arg2
;
475 if (fn1
->fn_guid
> fn2
->fn_guid
)
477 else if (fn1
->fn_guid
< fn2
->fn_guid
)
484 * Given the GUID of a snapshot, find its containing filesystem and
488 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
490 fsavl_node_t fn_find
;
493 fn_find
.fn_guid
= snapguid
;
495 fn
= avl_find(avl
, &fn_find
, NULL
);
498 *snapname
= fn
->fn_snapname
;
499 return (fn
->fn_nvfs
);
505 fsavl_destroy(avl_tree_t
*avl
)
514 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
521 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
524 fsavl_create(nvlist_t
*fss
)
527 nvpair_t
*fselem
= NULL
;
529 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
532 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
533 offsetof(fsavl_node_t
, fn_node
));
535 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
536 nvlist_t
*nvfs
, *snaps
;
537 nvpair_t
*snapelem
= NULL
;
539 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
540 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
543 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
547 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
548 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
549 fsavl_destroy(fsavl
);
553 fn
->fn_snapname
= nvpair_name(snapelem
);
557 * Note: if there are multiple snaps with the
558 * same GUID, we ignore all but one.
560 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
571 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
573 typedef struct send_data
{
574 uint64_t parent_fromsnap_guid
;
575 nvlist_t
*parent_snaps
;
578 const char *fromsnap
;
585 * The header nvlist is of the following format:
588 * "fromsnap" -> string (if incremental)
592 * "name" -> string (full name; for debugging)
593 * "parentfromsnap" -> number (guid of fromsnap in parent)
595 * "props" -> { name -> value (only if set here) }
596 * "snaps" -> { name (lastname) -> number (guid) }
597 * "snapprops" -> { name (lastname) -> { name -> value } }
599 * "origin" -> number (guid) (if clone)
600 * "sent" -> boolean (not on-disk)
608 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
611 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
613 send_data_t
*sd
= arg
;
614 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
617 boolean_t isfromsnap
, istosnap
, istosnapwithnofrom
;
619 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
620 isfromsnap
= (sd
->fromsnap
!= NULL
&&
621 strcmp(sd
->fromsnap
, snapname
) == 0);
622 istosnap
= (sd
->tosnap
!= NULL
&& (strcmp(sd
->tosnap
, snapname
) == 0));
623 istosnapwithnofrom
= (istosnap
&& sd
->fromsnap
== NULL
);
625 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
627 * NB: if there is no fromsnap here (it's a newly created fs in
628 * an incremental replication), we will substitute the tosnap.
630 if (isfromsnap
|| (sd
->parent_fromsnap_guid
== 0 && istosnap
)) {
631 sd
->parent_fromsnap_guid
= guid
;
634 if (!sd
->recursive
) {
635 if (!sd
->seenfrom
&& isfromsnap
) {
636 sd
->seenfrom
= B_TRUE
;
641 if ((sd
->seento
|| !sd
->seenfrom
) && !istosnapwithnofrom
) {
650 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
651 send_iterate_prop(zhp
, nv
);
652 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
660 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
662 nvpair_t
*elem
= NULL
;
664 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
665 char *propname
= nvpair_name(elem
);
666 zfs_prop_t prop
= zfs_name_to_prop(propname
);
669 if (!zfs_prop_user(propname
)) {
671 * Realistically, this should never happen. However,
672 * we want the ability to add DSL properties without
673 * needing to make incompatible version changes. We
674 * need to ignore unknown properties to allow older
675 * software to still send datasets containing these
676 * properties, with the unknown properties elided.
678 if (prop
== ZPROP_INVAL
)
681 if (zfs_prop_readonly(prop
))
685 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
686 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
687 prop
== ZFS_PROP_REFQUOTA
||
688 prop
== ZFS_PROP_REFRESERVATION
) {
691 verify(nvlist_lookup_uint64(propnv
,
692 ZPROP_VALUE
, &value
) == 0);
693 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
696 * May have no source before SPA_VERSION_RECVD_PROPS,
697 * but is still modifiable.
699 if (nvlist_lookup_string(propnv
,
700 ZPROP_SOURCE
, &source
) == 0) {
701 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
703 ZPROP_SOURCE_VAL_RECVD
) != 0))
708 if (nvlist_lookup_string(propnv
,
709 ZPROP_SOURCE
, &source
) != 0)
711 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
712 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
716 if (zfs_prop_user(propname
) ||
717 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
719 verify(nvlist_lookup_string(propnv
,
720 ZPROP_VALUE
, &value
) == 0);
721 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
724 verify(nvlist_lookup_uint64(propnv
,
725 ZPROP_VALUE
, &value
) == 0);
726 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
732 * recursively generate nvlists describing datasets. See comment
733 * for the data structure send_data_t above for description of contents
737 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
739 send_data_t
*sd
= arg
;
742 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
743 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
746 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
747 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
748 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
749 sd
->parent_fromsnap_guid
));
751 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
752 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
753 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
756 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
757 origin
->zfs_dmustats
.dds_guid
));
760 /* iterate over props */
761 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
762 send_iterate_prop(zhp
, nv
);
763 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
766 /* iterate over snaps, and set sd->parent_fromsnap_guid */
767 sd
->parent_fromsnap_guid
= 0;
768 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
769 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
770 (void) zfs_iter_snapshots_sorted(zhp
, send_iterate_snap
, sd
);
771 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
772 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
773 nvlist_free(sd
->parent_snaps
);
774 nvlist_free(sd
->snapprops
);
776 /* add this fs to nvlist */
777 (void) snprintf(guidstring
, sizeof (guidstring
),
778 "0x%llx", (longlong_t
)guid
);
779 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
782 /* iterate over children */
784 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
786 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
793 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
794 const char *tosnap
, boolean_t recursive
, nvlist_t
**nvlp
, avl_tree_t
**avlp
)
797 send_data_t sd
= { 0 };
800 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
802 return (EZFS_BADTYPE
);
804 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
805 sd
.fromsnap
= fromsnap
;
807 sd
.recursive
= recursive
;
809 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
817 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
828 * Routines specific to "zfs send"
830 typedef struct send_dump_data
{
831 /* these are all just the short snapname (the part after the @) */
832 const char *fromsnap
;
834 char prevsnap
[ZFS_MAXNAMELEN
];
835 uint64_t prevsnap_obj
;
836 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
837 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
;
843 snapfilter_cb_t
*filter_cb
;
846 char holdtag
[ZFS_MAXNAMELEN
];
852 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
853 boolean_t fromorigin
, uint64_t *sizep
)
855 zfs_cmd_t zc
= {"\0"};
856 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
858 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
859 assert(fromsnap_obj
== 0 || !fromorigin
);
861 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
862 zc
.zc_obj
= fromorigin
;
863 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
864 zc
.zc_fromobj
= fromsnap_obj
;
865 zc
.zc_guid
= 1; /* estimate flag */
867 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
869 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
870 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
874 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
875 "not an earlier snapshot from the same fs"));
876 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
879 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
880 ZFS_TYPE_SNAPSHOT
)) {
881 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
882 "incremental source (@%s) does not exist"),
885 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
898 zfs_error_aux(hdl
, strerror(errno
));
899 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
902 return (zfs_standard_error(hdl
, errno
, errbuf
));
906 *sizep
= zc
.zc_objset_type
;
912 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
913 * NULL) to the file descriptor specified by outfd.
916 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
917 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
920 zfs_cmd_t zc
= {"\0"};
921 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
924 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
925 assert(fromsnap_obj
== 0 || !fromorigin
);
927 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
928 zc
.zc_cookie
= outfd
;
929 zc
.zc_obj
= fromorigin
;
930 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
931 zc
.zc_fromobj
= fromsnap_obj
;
934 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
935 if (fromsnap
&& fromsnap
[0] != '\0') {
936 VERIFY(0 == nvlist_add_string(thisdbg
,
937 "fromsnap", fromsnap
));
940 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
942 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
943 "warning: cannot send '%s'"), zhp
->zfs_name
);
945 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
947 VERIFY(0 == nvlist_add_nvlist(debugnv
,
948 zhp
->zfs_name
, thisdbg
));
950 nvlist_free(thisdbg
);
954 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
955 "not an earlier snapshot from the same fs"));
956 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
959 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
960 ZFS_TYPE_SNAPSHOT
)) {
961 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
962 "incremental source (@%s) does not exist"),
965 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
978 zfs_error_aux(hdl
, strerror(errno
));
979 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
982 return (zfs_standard_error(hdl
, errno
, errbuf
));
987 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
988 nvlist_free(thisdbg
);
994 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
996 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
999 * zfs_send() only sets snapholds for sends that need them,
1000 * e.g. replication and doall.
1002 if (sdd
->snapholds
== NULL
)
1005 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1009 send_progress_thread(void *arg
)
1011 progress_arg_t
*pa
= arg
;
1013 zfs_cmd_t zc
= {"\0"};
1014 zfs_handle_t
*zhp
= pa
->pa_zhp
;
1015 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1016 unsigned long long bytes
;
1022 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1023 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1025 if (!pa
->pa_parsable
)
1026 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1029 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1034 zc
.zc_cookie
= pa
->pa_fd
;
1035 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1036 return ((void *)-1);
1040 bytes
= zc
.zc_cookie
;
1042 if (pa
->pa_parsable
) {
1043 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1044 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1045 bytes
, zhp
->zfs_name
);
1047 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1048 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1049 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1050 buf
, zhp
->zfs_name
);
1056 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1058 send_dump_data_t
*sdd
= arg
;
1059 progress_arg_t pa
= { 0 };
1063 boolean_t isfromsnap
, istosnap
, fromorigin
;
1064 boolean_t exclude
= B_FALSE
;
1067 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1068 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1069 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1071 if (!sdd
->seenfrom
&& isfromsnap
) {
1072 gather_holds(zhp
, sdd
);
1073 sdd
->seenfrom
= B_TRUE
;
1074 (void) strcpy(sdd
->prevsnap
, thissnap
);
1075 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1080 if (sdd
->seento
|| !sdd
->seenfrom
) {
1085 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1087 sdd
->seento
= B_TRUE
;
1089 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1090 if (sdd
->replicate
) {
1092 nvlist_t
*snapprops
;
1094 * Filter out all intermediate snapshots except origin
1095 * snapshots needed to replicate clones.
1097 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1098 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1100 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1101 "snapprops", &snapprops
));
1102 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1103 thissnap
, &snapprops
));
1104 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1111 * If a filter function exists, call it to determine whether
1112 * this snapshot will be sent.
1114 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1115 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1117 * This snapshot is filtered out. Don't send it, and don't
1118 * set prevsnap_obj, so it will be as if this snapshot didn't
1119 * exist, and the next accepted snapshot will be sent as
1120 * an incremental from the last accepted one, or as the
1121 * first (and full) snapshot in the case of a replication,
1122 * non-incremental send.
1128 gather_holds(zhp
, sdd
);
1129 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1130 (sdd
->fromorigin
|| sdd
->replicate
);
1134 err
= estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1137 if (sdd
->parsable
) {
1138 if (sdd
->prevsnap
[0] != '\0') {
1139 (void) fprintf(stderr
, "incremental\t%s\t%s",
1140 sdd
->prevsnap
, zhp
->zfs_name
);
1142 (void) fprintf(stderr
, "full\t%s",
1146 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1147 "send from @%s to %s"),
1148 sdd
->prevsnap
, zhp
->zfs_name
);
1151 if (sdd
->parsable
) {
1152 (void) fprintf(stderr
, "\t%llu\n",
1156 zfs_nicenum(size
, buf
, sizeof (buf
));
1157 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1158 " estimated size is %s\n"), buf
);
1162 (void) fprintf(stderr
, "\n");
1168 * If progress reporting is requested, spawn a new thread to
1169 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1171 if (sdd
->progress
) {
1173 pa
.pa_fd
= sdd
->outfd
;
1174 pa
.pa_parsable
= sdd
->parsable
;
1176 if ((err
= pthread_create(&tid
, NULL
,
1177 send_progress_thread
, &pa
))) {
1183 enum lzc_send_flags flags
= 0;
1184 if (sdd
->embed_data
)
1185 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1187 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1188 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1190 if (sdd
->progress
) {
1191 (void) pthread_cancel(tid
);
1192 (void) pthread_join(tid
, NULL
);
1196 (void) strcpy(sdd
->prevsnap
, thissnap
);
1197 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1203 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1206 send_dump_data_t
*sdd
= arg
;
1207 boolean_t missingfrom
= B_FALSE
;
1208 zfs_cmd_t zc
= {"\0"};
1210 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1211 zhp
->zfs_name
, sdd
->tosnap
);
1212 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1213 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1214 "WARNING: could not send %s@%s: does not exist\n"),
1215 zhp
->zfs_name
, sdd
->tosnap
);
1220 if (sdd
->replicate
&& sdd
->fromsnap
) {
1222 * If this fs does not have fromsnap, and we're doing
1223 * recursive, we need to send a full stream from the
1224 * beginning (or an incremental from the origin if this
1225 * is a clone). If we're doing non-recursive, then let
1226 * them get the error.
1228 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1229 zhp
->zfs_name
, sdd
->fromsnap
);
1230 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1231 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1232 missingfrom
= B_TRUE
;
1236 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1237 sdd
->prevsnap_obj
= 0;
1238 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1239 sdd
->seenfrom
= B_TRUE
;
1241 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1242 if (!sdd
->seenfrom
) {
1243 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1244 "WARNING: could not send %s@%s:\n"
1245 "incremental source (%s@%s) does not exist\n"),
1246 zhp
->zfs_name
, sdd
->tosnap
,
1247 zhp
->zfs_name
, sdd
->fromsnap
);
1249 } else if (!sdd
->seento
) {
1250 if (sdd
->fromsnap
) {
1251 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1252 "WARNING: could not send %s@%s:\n"
1253 "incremental source (%s@%s) "
1254 "is not earlier than it\n"),
1255 zhp
->zfs_name
, sdd
->tosnap
,
1256 zhp
->zfs_name
, sdd
->fromsnap
);
1258 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1260 "could not send %s@%s: does not exist\n"),
1261 zhp
->zfs_name
, sdd
->tosnap
);
1270 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1272 send_dump_data_t
*sdd
= arg
;
1274 boolean_t needagain
, progress
;
1276 if (!sdd
->replicate
)
1277 return (dump_filesystem(rzhp
, sdd
));
1279 /* Mark the clone origin snapshots. */
1280 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1281 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1283 uint64_t origin_guid
= 0;
1285 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1286 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1287 if (origin_guid
!= 0) {
1289 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1290 origin_guid
, &snapname
);
1291 if (origin_nv
!= NULL
) {
1292 nvlist_t
*snapprops
;
1293 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1294 "snapprops", &snapprops
));
1295 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1296 snapname
, &snapprops
));
1297 VERIFY(0 == nvlist_add_boolean(
1298 snapprops
, "is_clone_origin"));
1303 needagain
= progress
= B_FALSE
;
1304 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1305 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1306 nvlist_t
*fslist
, *parent_nv
;
1310 uint64_t origin_guid
= 0;
1311 uint64_t parent_guid
= 0;
1313 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1314 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1317 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1318 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1319 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1322 if (parent_guid
!= 0) {
1323 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1324 if (!nvlist_exists(parent_nv
, "sent")) {
1325 /* parent has not been sent; skip this one */
1331 if (origin_guid
!= 0) {
1332 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1334 if (origin_nv
!= NULL
&&
1335 !nvlist_exists(origin_nv
, "sent")) {
1337 * origin has not been sent yet;
1345 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1348 err
= dump_filesystem(zhp
, sdd
);
1349 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1360 /* clean out the sent flags in case we reuse this fss */
1361 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1362 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1365 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1366 (void) nvlist_remove_all(fslist
, "sent");
1373 * Generate a send stream for the dataset identified by the argument zhp.
1375 * The content of the send stream is the snapshot identified by
1376 * 'tosnap'. Incremental streams are requested in two ways:
1377 * - from the snapshot identified by "fromsnap" (if non-null) or
1378 * - from the origin of the dataset identified by zhp, which must
1379 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1382 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1383 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1384 * if "replicate" is set. If "doall" is set, dump all the intermediate
1385 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1386 * case too. If "props" is set, send properties.
1389 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1390 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1391 void *cb_arg
, nvlist_t
**debugnvp
)
1394 send_dump_data_t sdd
= { 0 };
1396 nvlist_t
*fss
= NULL
;
1397 avl_tree_t
*fsavl
= NULL
;
1398 static uint64_t holdseq
;
1402 dedup_arg_t dda
= { 0 };
1403 int featureflags
= 0;
1405 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1406 "cannot send '%s'"), zhp
->zfs_name
);
1408 if (fromsnap
&& fromsnap
[0] == '\0') {
1409 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1410 "zero-length incremental source"));
1411 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1414 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1416 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1417 if (version
>= ZPL_VERSION_SA
) {
1418 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1422 if (flags
->dedup
&& !flags
->dryrun
) {
1423 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1424 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1425 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
))) {
1426 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1427 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1430 dda
.outputfd
= outfd
;
1431 dda
.inputfd
= pipefd
[1];
1432 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1433 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1434 (void) close(pipefd
[0]);
1435 (void) close(pipefd
[1]);
1436 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1437 return (zfs_error(zhp
->zfs_hdl
,
1438 EZFS_THREADCREATEFAILED
, errbuf
));
1442 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1443 dmu_replay_record_t drr
= { 0 };
1444 char *packbuf
= NULL
;
1446 zio_cksum_t zc
= { { 0 } };
1448 if (flags
->replicate
|| flags
->props
) {
1451 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1453 VERIFY(0 == nvlist_add_string(hdrnv
,
1454 "fromsnap", fromsnap
));
1456 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1457 if (!flags
->replicate
) {
1458 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1462 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1463 fromsnap
, tosnap
, flags
->replicate
, &fss
, &fsavl
);
1466 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1467 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1477 if (!flags
->dryrun
) {
1478 /* write first begin record */
1479 drr
.drr_type
= DRR_BEGIN
;
1480 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1481 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1482 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1483 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1484 drr_versioninfo
, featureflags
);
1485 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1486 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1487 "%s@%s", zhp
->zfs_name
, tosnap
);
1488 drr
.drr_payloadlen
= buflen
;
1489 err
= cksum_and_write(&drr
, sizeof (drr
), &zc
, outfd
);
1491 /* write header nvlist */
1492 if (err
!= -1 && packbuf
!= NULL
) {
1493 err
= cksum_and_write(packbuf
, buflen
, &zc
,
1502 /* write end record */
1503 bzero(&drr
, sizeof (drr
));
1504 drr
.drr_type
= DRR_END
;
1505 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1506 err
= write(outfd
, &drr
, sizeof (drr
));
1516 /* dump each stream */
1517 sdd
.fromsnap
= fromsnap
;
1518 sdd
.tosnap
= tosnap
;
1520 sdd
.outfd
= pipefd
[0];
1523 sdd
.replicate
= flags
->replicate
;
1524 sdd
.doall
= flags
->doall
;
1525 sdd
.fromorigin
= flags
->fromorigin
;
1528 sdd
.verbose
= flags
->verbose
;
1529 sdd
.parsable
= flags
->parsable
;
1530 sdd
.progress
= flags
->progress
;
1531 sdd
.dryrun
= flags
->dryrun
;
1532 sdd
.embed_data
= flags
->embed_data
;
1533 sdd
.filter_cb
= filter_func
;
1534 sdd
.filter_cb_arg
= cb_arg
;
1536 sdd
.debugnv
= *debugnvp
;
1539 * Some flags require that we place user holds on the datasets that are
1540 * being sent so they don't get destroyed during the send. We can skip
1541 * this step if the pool is imported read-only since the datasets cannot
1544 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1545 ZPOOL_PROP_READONLY
, NULL
) &&
1546 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1547 spa_version
>= SPA_VERSION_USERREFS
&&
1548 (flags
->doall
|| flags
->replicate
)) {
1550 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1551 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1552 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1553 if (sdd
.cleanup_fd
< 0) {
1557 sdd
.snapholds
= fnvlist_alloc();
1559 sdd
.cleanup_fd
= -1;
1560 sdd
.snapholds
= NULL
;
1562 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1564 * Do a verbose no-op dry run to get all the verbose output
1565 * or to gather snapshot hold's before generating any data,
1566 * then do a non-verbose real run to generate the streams.
1568 sdd
.dryrun
= B_TRUE
;
1569 err
= dump_filesystems(zhp
, &sdd
);
1574 if (flags
->verbose
) {
1575 if (flags
->parsable
) {
1576 (void) fprintf(stderr
, "size\t%llu\n",
1577 (longlong_t
)sdd
.size
);
1580 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1581 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1582 "total estimated size is %s\n"), buf
);
1586 /* Ensure no snaps found is treated as an error. */
1592 /* Skip the second run if dryrun was requested. */
1596 if (sdd
.snapholds
!= NULL
) {
1597 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1601 fnvlist_free(sdd
.snapholds
);
1602 sdd
.snapholds
= NULL
;
1605 sdd
.dryrun
= B_FALSE
;
1606 sdd
.verbose
= B_FALSE
;
1609 err
= dump_filesystems(zhp
, &sdd
);
1610 fsavl_destroy(fsavl
);
1613 /* Ensure no snaps found is treated as an error. */
1614 if (err
== 0 && !sdd
.seento
)
1619 (void) pthread_cancel(tid
);
1620 (void) close(pipefd
[0]);
1621 (void) pthread_join(tid
, NULL
);
1624 if (sdd
.cleanup_fd
!= -1) {
1625 VERIFY(0 == close(sdd
.cleanup_fd
));
1626 sdd
.cleanup_fd
= -1;
1629 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1632 * write final end record. NB: want to do this even if
1633 * there was some error, because it might not be totally
1636 dmu_replay_record_t drr
= { 0 };
1637 drr
.drr_type
= DRR_END
;
1638 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1639 return (zfs_standard_error(zhp
->zfs_hdl
,
1644 return (err
|| sdd
.err
);
1647 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1649 fsavl_destroy(fsavl
);
1651 fnvlist_free(sdd
.snapholds
);
1653 if (sdd
.cleanup_fd
!= -1)
1654 VERIFY(0 == close(sdd
.cleanup_fd
));
1656 (void) pthread_cancel(tid
);
1657 (void) close(pipefd
[0]);
1658 (void) pthread_join(tid
, NULL
);
1664 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
1665 enum lzc_send_flags flags
)
1668 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1671 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1672 "warning: cannot send '%s'"), zhp
->zfs_name
);
1674 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
1678 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1679 "not an earlier snapshot from the same fs"));
1680 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1684 if (lzc_exists(zhp
->zfs_name
)) {
1685 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1686 "incremental source (%s) does not exist"),
1689 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1692 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1693 "target is busy; if a filesystem, "
1694 "it must not be mounted"));
1695 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
1708 zfs_error_aux(hdl
, strerror(errno
));
1709 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1712 return (zfs_standard_error(hdl
, errno
, errbuf
));
1719 * Routines specific to "zfs recv"
1723 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1724 boolean_t byteswap
, zio_cksum_t
*zc
)
1731 rv
= read(fd
, cp
, len
);
1736 if (rv
< 0 || len
!= 0) {
1737 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1738 "failed to read from stream"));
1739 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
1740 "cannot receive")));
1745 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
1747 fletcher_4_incremental_native(buf
, ilen
, zc
);
1753 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
1754 boolean_t byteswap
, zio_cksum_t
*zc
)
1759 buf
= zfs_alloc(hdl
, len
);
1763 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
1769 err
= nvlist_unpack(buf
, len
, nvp
, 0);
1772 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
1773 "stream (malformed nvlist)"));
1780 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
1781 int baselen
, char *newname
, recvflags_t
*flags
)
1784 zfs_cmd_t zc
= {"\0"};
1786 prop_changelist_t
*clp
;
1789 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1792 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1793 flags
->force
? MS_FORCE
: 0);
1797 err
= changelist_prefix(clp
);
1801 zc
.zc_objset_type
= DMU_OST_ZFS
;
1802 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1805 (void) strcpy(newname
, tryname
);
1807 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
1809 if (flags
->verbose
) {
1810 (void) printf("attempting rename %s to %s\n",
1811 zc
.zc_name
, zc
.zc_value
);
1813 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1815 changelist_rename(clp
, name
, tryname
);
1820 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
1823 (void) snprintf(newname
, ZFS_MAXNAMELEN
, "%.*srecv-%u-%u",
1824 baselen
, name
, getpid(), seq
);
1825 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
1827 if (flags
->verbose
) {
1828 (void) printf("failed - trying rename %s to %s\n",
1829 zc
.zc_name
, zc
.zc_value
);
1831 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1833 changelist_rename(clp
, name
, newname
);
1834 if (err
&& flags
->verbose
) {
1835 (void) printf("failed (%u) - "
1836 "will try again on next pass\n", errno
);
1839 } else if (flags
->verbose
) {
1841 (void) printf("success\n");
1843 (void) printf("failed (%u)\n", errno
);
1846 (void) changelist_postfix(clp
);
1847 changelist_free(clp
);
1853 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
1854 char *newname
, recvflags_t
*flags
)
1856 zfs_cmd_t zc
= {"\0"};
1858 prop_changelist_t
*clp
;
1860 boolean_t defer
= B_FALSE
;
1863 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1866 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1867 flags
->force
? MS_FORCE
: 0);
1868 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
1869 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1870 spa_version
>= SPA_VERSION_USERREFS
)
1875 err
= changelist_prefix(clp
);
1879 zc
.zc_objset_type
= DMU_OST_ZFS
;
1880 zc
.zc_defer_destroy
= defer
;
1881 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1884 (void) printf("attempting destroy %s\n", zc
.zc_name
);
1885 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
1888 (void) printf("success\n");
1889 changelist_remove(clp
, zc
.zc_name
);
1892 (void) changelist_postfix(clp
);
1893 changelist_free(clp
);
1896 * Deferred destroy might destroy the snapshot or only mark it to be
1897 * destroyed later, and it returns success in either case.
1899 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
1900 ZFS_TYPE_SNAPSHOT
))) {
1901 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
1907 typedef struct guid_to_name_data
{
1911 } guid_to_name_data_t
;
1914 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
1916 guid_to_name_data_t
*gtnd
= arg
;
1919 if (gtnd
->skip
!= NULL
&&
1920 strcmp(zhp
->zfs_name
, gtnd
->skip
) == 0) {
1924 if (zhp
->zfs_dmustats
.dds_guid
== gtnd
->guid
) {
1925 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
1930 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
1936 * Attempt to find the local dataset associated with this guid. In the case of
1937 * multiple matches, we attempt to find the "best" match by searching
1938 * progressively larger portions of the hierarchy. This allows one to send a
1939 * tree of datasets individually and guarantee that we will find the source
1940 * guid within that hierarchy, even if there are multiple matches elsewhere.
1943 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
1946 /* exhaustive search all local snapshots */
1947 char pname
[ZFS_MAXNAMELEN
];
1948 guid_to_name_data_t gtnd
;
1957 (void) strlcpy(pname
, parent
, sizeof (pname
));
1960 * Search progressively larger portions of the hierarchy. This will
1961 * select the "most local" version of the origin snapshot in the case
1962 * that there are multiple matching snapshots in the system.
1964 while ((cp
= strrchr(pname
, '/')) != NULL
) {
1966 /* Chop off the last component and open the parent */
1968 zhp
= make_dataset_handle(hdl
, pname
);
1973 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
1979 * Remember the dataset that we already searched, so we
1980 * skip it next time through.
1989 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1990 * guid1 is after guid2.
1993 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
1994 uint64_t guid1
, uint64_t guid2
)
1997 char *fsname
, *snapname
;
1998 char buf
[ZFS_MAXNAMELEN
];
2000 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2001 uint64_t create1
, create2
;
2008 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2009 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2010 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2011 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2012 if (guid1hdl
== NULL
)
2015 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2016 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2017 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2018 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2019 if (guid2hdl
== NULL
) {
2020 zfs_close(guid1hdl
);
2024 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2025 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2027 if (create1
< create2
)
2029 else if (create1
> create2
)
2034 zfs_close(guid1hdl
);
2035 zfs_close(guid2hdl
);
2041 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2042 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2045 nvlist_t
*local_nv
, *deleted
= NULL
;
2046 avl_tree_t
*local_avl
;
2047 nvpair_t
*fselem
, *nextfselem
;
2049 char newname
[ZFS_MAXNAMELEN
];
2052 boolean_t needagain
, progress
, recursive
;
2055 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2057 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2064 needagain
= progress
= B_FALSE
;
2066 VERIFY(0 == nvlist_alloc(&deleted
, NV_UNIQUE_NAME
, 0));
2068 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2069 recursive
, &local_nv
, &local_avl
)) != 0)
2073 * Process deletes and renames
2075 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2076 fselem
; fselem
= nextfselem
) {
2077 nvlist_t
*nvfs
, *snaps
;
2078 nvlist_t
*stream_nvfs
= NULL
;
2079 nvpair_t
*snapelem
, *nextsnapelem
;
2080 uint64_t fromguid
= 0;
2081 uint64_t originguid
= 0;
2082 uint64_t stream_originguid
= 0;
2083 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2084 char *fsname
, *stream_fsname
;
2086 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2088 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2089 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2090 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2091 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2092 &parent_fromsnap_guid
));
2093 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2096 * First find the stream's fs, so we can check for
2097 * a different origin (due to "zfs promote")
2099 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2100 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2103 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2104 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2106 if (stream_nvfs
!= NULL
)
2110 /* check for promote */
2111 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2112 &stream_originguid
);
2113 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2114 switch (created_before(hdl
, local_avl
,
2115 stream_originguid
, originguid
)) {
2118 zfs_cmd_t zc
= {"\0"};
2119 nvlist_t
*origin_nvfs
;
2120 char *origin_fsname
;
2123 (void) printf("promoting %s\n", fsname
);
2125 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2127 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2128 "name", &origin_fsname
));
2129 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2130 sizeof (zc
.zc_value
));
2131 (void) strlcpy(zc
.zc_name
, fsname
,
2132 sizeof (zc
.zc_name
));
2133 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2141 fsavl_destroy(local_avl
);
2142 nvlist_free(local_nv
);
2146 * We had/have the wrong origin, therefore our
2147 * list of snapshots is wrong. Need to handle
2148 * them on the next pass.
2154 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2155 snapelem
; snapelem
= nextsnapelem
) {
2157 char *stream_snapname
;
2158 nvlist_t
*found
, *props
;
2160 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2162 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2163 found
= fsavl_find(stream_avl
, thisguid
,
2166 /* check for delete */
2167 if (found
== NULL
) {
2168 char name
[ZFS_MAXNAMELEN
];
2173 (void) snprintf(name
, sizeof (name
), "%s@%s",
2174 fsname
, nvpair_name(snapelem
));
2176 error
= recv_destroy(hdl
, name
,
2177 strlen(fsname
)+1, newname
, flags
);
2182 sprintf(guidname
, "%llu",
2183 (u_longlong_t
)thisguid
);
2184 nvlist_add_boolean(deleted
, guidname
);
2188 stream_nvfs
= found
;
2190 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2191 &props
) && 0 == nvlist_lookup_nvlist(props
,
2192 stream_snapname
, &props
)) {
2193 zfs_cmd_t zc
= {"\0"};
2195 zc
.zc_cookie
= B_TRUE
; /* received */
2196 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2197 "%s@%s", fsname
, nvpair_name(snapelem
));
2198 if (zcmd_write_src_nvlist(hdl
, &zc
,
2200 (void) zfs_ioctl(hdl
,
2201 ZFS_IOC_SET_PROP
, &zc
);
2202 zcmd_free_nvlists(&zc
);
2206 /* check for different snapname */
2207 if (strcmp(nvpair_name(snapelem
),
2208 stream_snapname
) != 0) {
2209 char name
[ZFS_MAXNAMELEN
];
2210 char tryname
[ZFS_MAXNAMELEN
];
2212 (void) snprintf(name
, sizeof (name
), "%s@%s",
2213 fsname
, nvpair_name(snapelem
));
2214 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2215 fsname
, stream_snapname
);
2217 error
= recv_rename(hdl
, name
, tryname
,
2218 strlen(fsname
)+1, newname
, flags
);
2225 if (strcmp(stream_snapname
, fromsnap
) == 0)
2226 fromguid
= thisguid
;
2229 /* check for delete */
2230 if (stream_nvfs
== NULL
) {
2234 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2240 sprintf(guidname
, "%llu",
2241 (u_longlong_t
) parent_fromsnap_guid
);
2242 nvlist_add_boolean(deleted
, guidname
);
2246 if (fromguid
== 0) {
2247 if (flags
->verbose
) {
2248 (void) printf("local fs %s does not have "
2249 "fromsnap (%s in stream); must have "
2250 "been deleted locally; ignoring\n",
2256 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2257 "name", &stream_fsname
));
2258 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2259 "parentfromsnap", &stream_parent_fromsnap_guid
));
2261 s1
= strrchr(fsname
, '/');
2262 s2
= strrchr(stream_fsname
, '/');
2265 * Check if we're going to rename based on parent guid change
2266 * and the current parent guid was also deleted. If it was then
2267 * rename will fail and is likely unneeded, so avoid this and
2268 * force an early retry to determine the new
2269 * parent_fromsnap_guid.
2271 if (stream_parent_fromsnap_guid
!= 0 &&
2272 parent_fromsnap_guid
!= 0 &&
2273 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
2274 sprintf(guidname
, "%llu",
2275 (u_longlong_t
) parent_fromsnap_guid
);
2276 if (nvlist_exists(deleted
, guidname
)) {
2284 * Check for rename. If the exact receive path is specified, it
2285 * does not count as a rename, but we still need to check the
2286 * datasets beneath it.
2288 if ((stream_parent_fromsnap_guid
!= 0 &&
2289 parent_fromsnap_guid
!= 0 &&
2290 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2291 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2292 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2294 char tryname
[ZFS_MAXNAMELEN
];
2296 parent
= fsavl_find(local_avl
,
2297 stream_parent_fromsnap_guid
, NULL
);
2299 * NB: parent might not be found if we used the
2300 * tosnap for stream_parent_fromsnap_guid,
2301 * because the parent is a newly-created fs;
2302 * we'll be able to rename it after we recv the
2305 if (parent
!= NULL
) {
2308 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2310 (void) snprintf(tryname
, sizeof (tryname
),
2311 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2314 if (flags
->verbose
) {
2315 (void) printf("local fs %s new parent "
2316 "not found\n", fsname
);
2322 error
= recv_rename(hdl
, fsname
, tryname
,
2323 strlen(tofs
)+1, newname
, flags
);
2325 if (renamed
!= NULL
&& newname
[0] != '\0') {
2326 VERIFY(0 == nvlist_add_boolean(renamed
,
2338 fsavl_destroy(local_avl
);
2339 nvlist_free(local_nv
);
2340 nvlist_free(deleted
);
2342 if (needagain
&& progress
) {
2343 /* do another pass to fix up temporary names */
2345 (void) printf("another pass:\n");
2353 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2354 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2355 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2357 nvlist_t
*stream_nv
= NULL
;
2358 avl_tree_t
*stream_avl
= NULL
;
2359 char *fromsnap
= NULL
;
2361 char tofs
[ZFS_MAXNAMELEN
];
2362 char sendfs
[ZFS_MAXNAMELEN
];
2364 dmu_replay_record_t drre
;
2366 boolean_t anyerr
= B_FALSE
;
2367 boolean_t softerr
= B_FALSE
;
2368 boolean_t recursive
;
2370 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2373 assert(drr
->drr_type
== DRR_BEGIN
);
2374 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2375 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2376 DMU_COMPOUNDSTREAM
);
2379 * Read in the nvlist from the stream.
2381 if (drr
->drr_payloadlen
!= 0) {
2382 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2383 &stream_nv
, flags
->byteswap
, zc
);
2385 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2390 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2393 if (recursive
&& strchr(destname
, '@')) {
2394 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2395 "cannot specify snapshot name for multi-snapshot stream"));
2396 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2401 * Read in the end record and verify checksum.
2403 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2404 flags
->byteswap
, NULL
)))
2406 if (flags
->byteswap
) {
2407 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2408 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2409 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2410 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2411 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2412 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2413 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2414 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2415 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2417 if (drre
.drr_type
!= DRR_END
) {
2418 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2421 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2422 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2423 "incorrect header checksum"));
2424 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2428 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2430 if (drr
->drr_payloadlen
!= 0) {
2431 nvlist_t
*stream_fss
;
2433 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2435 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2436 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2437 "couldn't allocate avl tree"));
2438 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2442 if (fromsnap
!= NULL
) {
2443 nvlist_t
*renamed
= NULL
;
2444 nvpair_t
*pair
= NULL
;
2446 (void) strlcpy(tofs
, destname
, ZFS_MAXNAMELEN
);
2447 if (flags
->isprefix
) {
2448 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2451 if (flags
->istail
) {
2452 cp
= strrchr(drrb
->drr_toname
, '/');
2454 (void) strlcat(tofs
, "/",
2458 i
= (cp
- drrb
->drr_toname
);
2461 i
= strcspn(drrb
->drr_toname
, "/@");
2463 /* zfs_receive_one() will create_parents() */
2464 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2466 *strchr(tofs
, '@') = '\0';
2469 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2470 VERIFY(0 == nvlist_alloc(&renamed
,
2471 NV_UNIQUE_NAME
, 0));
2474 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2475 stream_nv
, stream_avl
, renamed
);
2477 /* Unmount renamed filesystems before receiving. */
2478 while ((pair
= nvlist_next_nvpair(renamed
,
2481 prop_changelist_t
*clp
= NULL
;
2483 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2484 ZFS_TYPE_FILESYSTEM
);
2486 clp
= changelist_gather(zhp
,
2487 ZFS_PROP_MOUNTPOINT
, 0, 0);
2491 changelist_prefix(clp
);
2492 changelist_free(clp
);
2497 nvlist_free(renamed
);
2502 * Get the fs specified by the first path in the stream (the top level
2503 * specified by 'zfs send') and pass it to each invocation of
2504 * zfs_receive_one().
2506 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2508 if ((cp
= strchr(sendfs
, '@')) != NULL
)
2511 /* Finally, receive each contained stream */
2514 * we should figure out if it has a recoverable
2515 * error, in which case do a recv_skip() and drive on.
2516 * Note, if we fail due to already having this guid,
2517 * zfs_receive_one() will take care of it (ie,
2518 * recv_skip() and return 0).
2520 error
= zfs_receive_impl(hdl
, destname
, flags
, fd
,
2521 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2523 if (error
== ENODATA
) {
2528 } while (error
== 0);
2530 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2532 * Now that we have the fs's they sent us, try the
2535 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2536 stream_nv
, stream_avl
, NULL
);
2540 fsavl_destroy(stream_avl
);
2542 nvlist_free(stream_nv
);
2551 trunc_prop_errs(int truncated
)
2553 ASSERT(truncated
!= 0);
2556 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2557 "1 more property could not be set\n"));
2559 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2560 "%d more properties could not be set\n"), truncated
);
2564 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2566 dmu_replay_record_t
*drr
;
2567 void *buf
= malloc(1<<20);
2570 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2571 "cannot receive:"));
2573 /* XXX would be great to use lseek if possible... */
2576 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2577 byteswap
, NULL
) == 0) {
2579 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2581 switch (drr
->drr_type
) {
2583 /* NB: not to be used on v2 stream packages */
2584 if (drr
->drr_payloadlen
!= 0) {
2585 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2586 "invalid substream header"));
2587 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2597 drr
->drr_u
.drr_object
.drr_bonuslen
=
2598 BSWAP_32(drr
->drr_u
.drr_object
.
2601 (void) recv_read(hdl
, fd
, buf
,
2602 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2608 drr
->drr_u
.drr_write
.drr_length
=
2609 BSWAP_64(drr
->drr_u
.drr_write
.drr_length
);
2611 (void) recv_read(hdl
, fd
, buf
,
2612 drr
->drr_u
.drr_write
.drr_length
, B_FALSE
, NULL
);
2616 drr
->drr_u
.drr_write
.drr_length
=
2617 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2619 (void) recv_read(hdl
, fd
, buf
,
2620 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2622 case DRR_WRITE_EMBEDDED
:
2624 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2625 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2628 (void) recv_read(hdl
, fd
, buf
,
2629 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2632 case DRR_WRITE_BYREF
:
2633 case DRR_FREEOBJECTS
:
2638 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2639 "invalid record type"));
2640 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2649 * Restores a backup of tosnap from the file descriptor specified by infd.
2652 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2653 recvflags_t
*flags
, dmu_replay_record_t
*drr
,
2654 dmu_replay_record_t
*drr_noswap
, const char *sendfs
,
2655 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2656 uint64_t *action_handlep
)
2658 zfs_cmd_t zc
= {"\0"};
2660 int ioctl_err
, ioctl_errno
, err
;
2662 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2664 char prop_errbuf
[1024];
2665 const char *chopprefix
;
2666 boolean_t newfs
= B_FALSE
;
2667 boolean_t stream_wantsnewfs
;
2668 uint64_t parent_snapguid
= 0;
2669 prop_changelist_t
*clp
= NULL
;
2670 nvlist_t
*snapprops_nvlist
= NULL
;
2671 zprop_errflags_t prop_errflags
;
2672 boolean_t recursive
;
2674 begin_time
= time(NULL
);
2676 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2679 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2682 if (stream_avl
!= NULL
) {
2684 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
2689 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
2691 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
2693 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
2695 if (flags
->canmountoff
) {
2696 VERIFY(0 == nvlist_add_uint64(props
,
2697 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
2699 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
2709 * Determine how much of the snapshot name stored in the stream
2710 * we are going to tack on to the name they specified on the
2711 * command line, and how much we are going to chop off.
2713 * If they specified a snapshot, chop the entire name stored in
2716 if (flags
->istail
) {
2718 * A filesystem was specified with -e. We want to tack on only
2719 * the tail of the sent snapshot path.
2721 if (strchr(tosnap
, '@')) {
2722 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2723 "argument - snapshot not allowed with -e"));
2724 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2727 chopprefix
= strrchr(sendfs
, '/');
2729 if (chopprefix
== NULL
) {
2731 * The tail is the poolname, so we need to
2732 * prepend a path separator.
2734 int len
= strlen(drrb
->drr_toname
);
2735 cp
= malloc(len
+ 2);
2737 (void) strcpy(&cp
[1], drrb
->drr_toname
);
2740 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
2742 } else if (flags
->isprefix
) {
2744 * A filesystem was specified with -d. We want to tack on
2745 * everything but the first element of the sent snapshot path
2746 * (all but the pool name).
2748 if (strchr(tosnap
, '@')) {
2749 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2750 "argument - snapshot not allowed with -d"));
2751 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2754 chopprefix
= strchr(drrb
->drr_toname
, '/');
2755 if (chopprefix
== NULL
)
2756 chopprefix
= strchr(drrb
->drr_toname
, '@');
2757 } else if (strchr(tosnap
, '@') == NULL
) {
2759 * If a filesystem was specified without -d or -e, we want to
2760 * tack on everything after the fs specified by 'zfs send'.
2762 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
2764 /* A snapshot was specified as an exact path (no -d or -e). */
2766 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2767 "cannot specify snapshot name for multi-snapshot "
2769 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2771 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
2774 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
2775 ASSERT(chopprefix
> drrb
->drr_toname
);
2776 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
2777 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
2778 chopprefix
[0] == '\0');
2781 * Determine name of destination snapshot, store in zc_value.
2783 (void) strcpy(zc
.zc_value
, tosnap
);
2784 (void) strlcat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
2786 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
2787 zcmd_free_nvlists(&zc
);
2788 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2792 * Determine the name of the origin snapshot, store in zc_string.
2794 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
2795 if (guid_to_name(hdl
, zc
.zc_value
,
2796 drrb
->drr_fromguid
, zc
.zc_string
) != 0) {
2797 zcmd_free_nvlists(&zc
);
2798 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2799 "local origin for clone %s does not exist"),
2801 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2804 (void) printf("found clone origin %s\n", zc
.zc_string
);
2807 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
2808 (drrb
->drr_flags
& DRR_FLAG_CLONE
));
2810 if (stream_wantsnewfs
) {
2812 * if the parent fs does not exist, look for it based on
2813 * the parent snap GUID
2815 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2816 "cannot receive new filesystem stream"));
2818 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2819 cp
= strrchr(zc
.zc_name
, '/');
2823 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2824 char suffix
[ZFS_MAXNAMELEN
];
2825 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
2826 if (guid_to_name(hdl
, zc
.zc_name
, parent_snapguid
,
2827 zc
.zc_value
) == 0) {
2828 *strchr(zc
.zc_value
, '@') = '\0';
2829 (void) strcat(zc
.zc_value
, suffix
);
2834 * if the fs does not exist, look for it based on the
2837 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2838 "cannot receive incremental stream"));
2840 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2841 *strchr(zc
.zc_name
, '@') = '\0';
2844 * If the exact receive path was specified and this is the
2845 * topmost path in the stream, then if the fs does not exist we
2846 * should look no further.
2848 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
2849 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
2850 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2851 char snap
[ZFS_MAXNAMELEN
];
2852 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
2853 if (guid_to_name(hdl
, zc
.zc_name
, drrb
->drr_fromguid
,
2854 zc
.zc_value
) == 0) {
2855 *strchr(zc
.zc_value
, '@') = '\0';
2856 (void) strcat(zc
.zc_value
, snap
);
2861 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2862 *strchr(zc
.zc_name
, '@') = '\0';
2864 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2868 * Destination fs exists. Therefore this should either
2869 * be an incremental, or the stream specifies a new fs
2870 * (full stream or clone) and they want us to blow it
2871 * away (and have therefore specified -F and removed any
2874 if (stream_wantsnewfs
) {
2875 if (!flags
->force
) {
2876 zcmd_free_nvlists(&zc
);
2877 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2878 "destination '%s' exists\n"
2879 "must specify -F to overwrite it"),
2881 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2883 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2885 zcmd_free_nvlists(&zc
);
2886 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2887 "destination has snapshots (eg. %s)\n"
2888 "must destroy them to overwrite it"),
2890 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2894 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
2895 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
2896 zcmd_free_nvlists(&zc
);
2900 if (stream_wantsnewfs
&&
2901 zhp
->zfs_dmustats
.dds_origin
[0]) {
2902 zcmd_free_nvlists(&zc
);
2904 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2905 "destination '%s' is a clone\n"
2906 "must destroy it to overwrite it"),
2908 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2911 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
2912 stream_wantsnewfs
) {
2913 /* We can't do online recv in this case */
2914 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
2917 zcmd_free_nvlists(&zc
);
2920 if (changelist_prefix(clp
) != 0) {
2921 changelist_free(clp
);
2923 zcmd_free_nvlists(&zc
);
2930 * Destination filesystem does not exist. Therefore we better
2931 * be creating a new filesystem (either from a full backup, or
2932 * a clone). It would therefore be invalid if the user
2933 * specified only the pool name (i.e. if the destination name
2934 * contained no slash character).
2936 if (!stream_wantsnewfs
||
2937 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
2938 zcmd_free_nvlists(&zc
);
2939 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2940 "destination '%s' does not exist"), zc
.zc_name
);
2941 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2945 * Trim off the final dataset component so we perform the
2946 * recvbackup ioctl to the filesystems's parent.
2950 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
2951 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
2952 zcmd_free_nvlists(&zc
);
2953 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
2959 zc
.zc_begin_record
= drr_noswap
->drr_u
.drr_begin
;
2960 zc
.zc_cookie
= infd
;
2961 zc
.zc_guid
= flags
->force
;
2962 if (flags
->verbose
) {
2963 (void) printf("%s %s stream of %s into %s\n",
2964 flags
->dryrun
? "would receive" : "receiving",
2965 drrb
->drr_fromguid
? "incremental" : "full",
2966 drrb
->drr_toname
, zc
.zc_value
);
2967 (void) fflush(stdout
);
2970 if (flags
->dryrun
) {
2971 zcmd_free_nvlists(&zc
);
2972 return (recv_skip(hdl
, infd
, flags
->byteswap
));
2975 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
2976 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
2977 zc
.zc_cleanup_fd
= cleanup_fd
;
2978 zc
.zc_action_handle
= *action_handlep
;
2980 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
2981 ioctl_errno
= errno
;
2982 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
2985 nvlist_t
*prop_errors
;
2986 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
2987 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
2989 nvpair_t
*prop_err
= NULL
;
2991 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
2992 prop_err
)) != NULL
) {
2997 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
2998 (void) nvpair_value_int32(prop_err
, &intval
);
2999 if (strcmp(nvpair_name(prop_err
),
3000 ZPROP_N_MORE_ERRORS
) == 0) {
3001 trunc_prop_errs(intval
);
3004 (void) snprintf(tbuf
, sizeof (tbuf
),
3005 dgettext(TEXT_DOMAIN
,
3006 "cannot receive %s property on %s"),
3007 nvpair_name(prop_err
), zc
.zc_name
);
3008 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3011 nvlist_free(prop_errors
);
3014 zc
.zc_nvlist_dst
= 0;
3015 zc
.zc_nvlist_dst_size
= 0;
3016 zcmd_free_nvlists(&zc
);
3018 if (err
== 0 && snapprops_nvlist
) {
3019 zfs_cmd_t zc2
= {"\0"};
3021 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
3022 zc2
.zc_cookie
= B_TRUE
; /* received */
3023 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
3024 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
3025 zcmd_free_nvlists(&zc2
);
3029 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3031 * It may be that this snapshot already exists,
3032 * in which case we want to consume & ignore it
3033 * rather than failing.
3035 avl_tree_t
*local_avl
;
3036 nvlist_t
*local_nv
, *fs
;
3037 cp
= strchr(zc
.zc_value
, '@');
3040 * XXX Do this faster by just iterating over snaps in
3041 * this fs. Also if zc_value does not exist, we will
3042 * get a strange "does not exist" error message.
3045 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
3046 &local_nv
, &local_avl
) == 0) {
3048 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3049 fsavl_destroy(local_avl
);
3050 nvlist_free(local_nv
);
3053 if (flags
->verbose
) {
3054 (void) printf("snap %s already exists; "
3055 "ignoring\n", zc
.zc_value
);
3057 err
= ioctl_err
= recv_skip(hdl
, infd
,
3064 if (ioctl_err
!= 0) {
3065 switch (ioctl_errno
) {
3067 cp
= strchr(zc
.zc_value
, '@');
3069 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3070 "most recent snapshot of %s does not\n"
3071 "match incremental source"), zc
.zc_value
);
3072 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3076 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3077 "destination %s has been modified\n"
3078 "since most recent snapshot"), zc
.zc_name
);
3079 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3082 cp
= strchr(zc
.zc_value
, '@');
3084 /* it's the containing fs that exists */
3087 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3088 "destination already exists"));
3089 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3090 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3095 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3098 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3099 "invalid stream (checksum mismatch)"));
3100 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3103 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3104 "pool must be upgraded to receive this stream."));
3105 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3108 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3109 "destination %s space quota exceeded"), zc
.zc_name
);
3110 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3113 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3118 * Mount the target filesystem (if created). Also mount any
3119 * children of the target filesystem if we did a replication
3120 * receive (indicated by stream_avl being non-NULL).
3122 cp
= strchr(zc
.zc_value
, '@');
3123 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3127 h
= zfs_open(hdl
, zc
.zc_value
,
3128 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3130 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3132 } else if (newfs
|| stream_avl
) {
3134 * Track the first/top of hierarchy fs,
3135 * for mounting and sharing later.
3137 if (top_zfs
&& *top_zfs
== NULL
)
3138 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
3146 err
|= changelist_postfix(clp
);
3147 changelist_free(clp
);
3150 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3151 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3152 "failed to clear unreceived properties on %s"),
3154 (void) fprintf(stderr
, "\n");
3156 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3157 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3158 "failed to restore original properties on %s"),
3160 (void) fprintf(stderr
, "\n");
3163 if (err
|| ioctl_err
)
3166 *action_handlep
= zc
.zc_action_handle
;
3168 if (flags
->verbose
) {
3171 uint64_t bytes
= zc
.zc_cookie
;
3172 time_t delta
= time(NULL
) - begin_time
;
3175 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3176 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3178 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3186 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t
*flags
,
3187 int infd
, const char *sendfs
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
3188 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
3191 dmu_replay_record_t drr
, drr_noswap
;
3192 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3194 zio_cksum_t zcksum
= { { 0 } };
3195 uint64_t featureflags
;
3198 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3201 if (flags
->isprefix
&&
3202 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3203 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3204 "(%s) does not exist"), tosnap
);
3205 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3208 /* read in the BEGIN record */
3209 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3213 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3214 /* It's the double end record at the end of a package */
3218 /* the kernel needs the non-byteswapped begin record */
3221 flags
->byteswap
= B_FALSE
;
3222 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3224 * We computed the checksum in the wrong byteorder in
3225 * recv_read() above; do it again correctly.
3227 bzero(&zcksum
, sizeof (zio_cksum_t
));
3228 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3229 flags
->byteswap
= B_TRUE
;
3231 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3232 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3233 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3234 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3235 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3236 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3237 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3238 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3239 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3242 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3243 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3244 "stream (bad magic number)"));
3245 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3248 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3249 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3251 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3252 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3253 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3254 "stream has unsupported feature, feature flags = %lx"),
3256 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3259 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3260 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3261 "stream (bad snapshot name)"));
3262 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3265 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3266 char nonpackage_sendfs
[ZFS_MAXNAMELEN
];
3267 if (sendfs
== NULL
) {
3269 * We were not called from zfs_receive_package(). Get
3270 * the fs specified by 'zfs send'.
3273 (void) strlcpy(nonpackage_sendfs
,
3274 drr
.drr_u
.drr_begin
.drr_toname
, ZFS_MAXNAMELEN
);
3275 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3277 sendfs
= nonpackage_sendfs
;
3279 return (zfs_receive_one(hdl
, infd
, tosnap
, flags
,
3280 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
,
3281 top_zfs
, cleanup_fd
, action_handlep
));
3283 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3284 DMU_COMPOUNDSTREAM
);
3285 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
,
3286 &drr
, &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3291 * Restores a backup of tosnap from the file descriptor specified by infd.
3292 * Return 0 on total success, -2 if some things couldn't be
3293 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3294 * (-1 will override -2).
3297 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t
*flags
,
3298 int infd
, avl_tree_t
*stream_avl
)
3300 char *top_zfs
= NULL
;
3303 uint64_t action_handle
= 0;
3307 * The only way fstat can fail is if we do not have a valid file
3310 if (fstat(infd
, &sb
) == -1) {
3316 #ifndef F_SETPIPE_SZ
3317 #define F_SETPIPE_SZ (F_SETLEASE + 7)
3318 #endif /* F_SETPIPE_SZ */
3320 #ifndef F_GETPIPE_SZ
3321 #define F_GETPIPE_SZ (F_GETLEASE + 7)
3322 #endif /* F_GETPIPE_SZ */
3325 * It is not uncommon for gigabytes to be processed in zfs receive.
3326 * Speculatively increase the buffer size via Linux-specific fcntl()
3329 if (S_ISFIFO(sb
.st_mode
)) {
3330 FILE *procf
= fopen("/proc/sys/fs/pipe-max-size", "r");
3332 if (procf
!= NULL
) {
3333 unsigned long max_psize
;
3335 if (fscanf(procf
, "%lu", &max_psize
) > 0) {
3336 cur_psize
= fcntl(infd
, F_GETPIPE_SZ
);
3337 if (cur_psize
> 0 &&
3338 max_psize
> (unsigned long) cur_psize
)
3339 (void) fcntl(infd
, F_SETPIPE_SZ
,
3345 #endif /* __linux__ */
3347 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3348 VERIFY(cleanup_fd
>= 0);
3350 err
= zfs_receive_impl(hdl
, tosnap
, flags
, infd
, NULL
, NULL
,
3351 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
);
3353 VERIFY(0 == close(cleanup_fd
));
3355 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3357 prop_changelist_t
*clp
;
3359 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3361 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3362 CL_GATHER_MOUNT_ALWAYS
, 0);
3365 /* mount and share received datasets */
3366 err
= changelist_postfix(clp
);
3367 changelist_free(clp
);
3370 if (zhp
== NULL
|| clp
== NULL
|| err
)