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
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
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
;
282 if (sz
> SPA_MAXBLOCKSIZE
) {
283 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
284 SPA_MAXBLOCKSIZE
, sz
);
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
, std_out
;
838 boolean_t large_block
;
844 snapfilter_cb_t
*filter_cb
;
847 char holdtag
[ZFS_MAXNAMELEN
];
853 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
854 boolean_t fromorigin
, uint64_t *sizep
)
856 zfs_cmd_t zc
= {"\0"};
857 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
859 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
860 assert(fromsnap_obj
== 0 || !fromorigin
);
862 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
863 zc
.zc_obj
= fromorigin
;
864 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
865 zc
.zc_fromobj
= fromsnap_obj
;
866 zc
.zc_guid
= 1; /* estimate flag */
868 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
870 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
871 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
875 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
876 "not an earlier snapshot from the same fs"));
877 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
880 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
881 ZFS_TYPE_SNAPSHOT
)) {
882 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
883 "incremental source (@%s) does not exist"),
886 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
899 zfs_error_aux(hdl
, strerror(errno
));
900 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
903 return (zfs_standard_error(hdl
, errno
, errbuf
));
907 *sizep
= zc
.zc_objset_type
;
913 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
914 * NULL) to the file descriptor specified by outfd.
917 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
918 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
921 zfs_cmd_t zc
= {"\0"};
922 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
925 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
926 assert(fromsnap_obj
== 0 || !fromorigin
);
928 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
929 zc
.zc_cookie
= outfd
;
930 zc
.zc_obj
= fromorigin
;
931 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
932 zc
.zc_fromobj
= fromsnap_obj
;
935 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
936 if (fromsnap
&& fromsnap
[0] != '\0') {
937 VERIFY(0 == nvlist_add_string(thisdbg
,
938 "fromsnap", fromsnap
));
941 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
943 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
944 "warning: cannot send '%s'"), zhp
->zfs_name
);
946 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
948 VERIFY(0 == nvlist_add_nvlist(debugnv
,
949 zhp
->zfs_name
, thisdbg
));
951 nvlist_free(thisdbg
);
955 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
956 "not an earlier snapshot from the same fs"));
957 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
960 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
961 ZFS_TYPE_SNAPSHOT
)) {
962 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
963 "incremental source (@%s) does not exist"),
966 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
979 zfs_error_aux(hdl
, strerror(errno
));
980 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
983 return (zfs_standard_error(hdl
, errno
, errbuf
));
988 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
989 nvlist_free(thisdbg
);
995 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
997 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1000 * zfs_send() only sets snapholds for sends that need them,
1001 * e.g. replication and doall.
1003 if (sdd
->snapholds
== NULL
)
1006 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1010 send_progress_thread(void *arg
)
1012 progress_arg_t
*pa
= arg
;
1014 zfs_cmd_t zc
= {"\0"};
1015 zfs_handle_t
*zhp
= pa
->pa_zhp
;
1016 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1017 unsigned long long bytes
;
1023 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1024 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1026 if (!pa
->pa_parsable
)
1027 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1030 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1035 zc
.zc_cookie
= pa
->pa_fd
;
1036 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1037 return ((void *)-1);
1041 bytes
= zc
.zc_cookie
;
1043 if (pa
->pa_parsable
) {
1044 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1045 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1046 bytes
, zhp
->zfs_name
);
1048 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1049 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1050 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1051 buf
, zhp
->zfs_name
);
1057 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1059 send_dump_data_t
*sdd
= arg
;
1060 progress_arg_t pa
= { 0 };
1064 boolean_t isfromsnap
, istosnap
, fromorigin
;
1065 boolean_t exclude
= B_FALSE
;
1066 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1069 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1070 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1071 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1073 if (!sdd
->seenfrom
&& isfromsnap
) {
1074 gather_holds(zhp
, sdd
);
1075 sdd
->seenfrom
= B_TRUE
;
1076 (void) strcpy(sdd
->prevsnap
, thissnap
);
1077 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1082 if (sdd
->seento
|| !sdd
->seenfrom
) {
1087 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1089 sdd
->seento
= B_TRUE
;
1091 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1092 if (sdd
->replicate
) {
1094 nvlist_t
*snapprops
;
1096 * Filter out all intermediate snapshots except origin
1097 * snapshots needed to replicate clones.
1099 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1100 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1102 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1103 "snapprops", &snapprops
));
1104 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1105 thissnap
, &snapprops
));
1106 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1113 * If a filter function exists, call it to determine whether
1114 * this snapshot will be sent.
1116 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1117 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1119 * This snapshot is filtered out. Don't send it, and don't
1120 * set prevsnap_obj, so it will be as if this snapshot didn't
1121 * exist, and the next accepted snapshot will be sent as
1122 * an incremental from the last accepted one, or as the
1123 * first (and full) snapshot in the case of a replication,
1124 * non-incremental send.
1130 gather_holds(zhp
, sdd
);
1131 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1132 (sdd
->fromorigin
|| sdd
->replicate
);
1136 err
= estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1139 if (sdd
->parsable
) {
1140 if (sdd
->prevsnap
[0] != '\0') {
1141 (void) fprintf(fout
, "incremental\t%s\t%s",
1142 sdd
->prevsnap
, zhp
->zfs_name
);
1144 (void) fprintf(fout
, "full\t%s",
1148 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1149 "send from @%s to %s"),
1150 sdd
->prevsnap
, zhp
->zfs_name
);
1153 if (sdd
->parsable
) {
1154 (void) fprintf(fout
, "\t%llu\n",
1158 zfs_nicenum(size
, buf
, sizeof (buf
));
1159 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1160 " estimated size is %s\n"), buf
);
1164 (void) fprintf(fout
, "\n");
1170 * If progress reporting is requested, spawn a new thread to
1171 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1173 if (sdd
->progress
) {
1175 pa
.pa_fd
= sdd
->outfd
;
1176 pa
.pa_parsable
= sdd
->parsable
;
1178 if ((err
= pthread_create(&tid
, NULL
,
1179 send_progress_thread
, &pa
))) {
1185 enum lzc_send_flags flags
= 0;
1186 if (sdd
->large_block
)
1187 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1188 if (sdd
->embed_data
)
1189 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1191 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1192 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1194 if (sdd
->progress
) {
1195 (void) pthread_cancel(tid
);
1196 (void) pthread_join(tid
, NULL
);
1200 (void) strcpy(sdd
->prevsnap
, thissnap
);
1201 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1207 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1210 send_dump_data_t
*sdd
= arg
;
1211 boolean_t missingfrom
= B_FALSE
;
1212 zfs_cmd_t zc
= {"\0"};
1214 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1215 zhp
->zfs_name
, sdd
->tosnap
);
1216 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1217 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1218 "WARNING: could not send %s@%s: does not exist\n"),
1219 zhp
->zfs_name
, sdd
->tosnap
);
1224 if (sdd
->replicate
&& sdd
->fromsnap
) {
1226 * If this fs does not have fromsnap, and we're doing
1227 * recursive, we need to send a full stream from the
1228 * beginning (or an incremental from the origin if this
1229 * is a clone). If we're doing non-recursive, then let
1230 * them get the error.
1232 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1233 zhp
->zfs_name
, sdd
->fromsnap
);
1234 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1235 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1236 missingfrom
= B_TRUE
;
1240 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1241 sdd
->prevsnap_obj
= 0;
1242 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1243 sdd
->seenfrom
= B_TRUE
;
1245 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1246 if (!sdd
->seenfrom
) {
1247 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1248 "WARNING: could not send %s@%s:\n"
1249 "incremental source (%s@%s) does not exist\n"),
1250 zhp
->zfs_name
, sdd
->tosnap
,
1251 zhp
->zfs_name
, sdd
->fromsnap
);
1253 } else if (!sdd
->seento
) {
1254 if (sdd
->fromsnap
) {
1255 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1256 "WARNING: could not send %s@%s:\n"
1257 "incremental source (%s@%s) "
1258 "is not earlier than it\n"),
1259 zhp
->zfs_name
, sdd
->tosnap
,
1260 zhp
->zfs_name
, sdd
->fromsnap
);
1262 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1264 "could not send %s@%s: does not exist\n"),
1265 zhp
->zfs_name
, sdd
->tosnap
);
1274 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1276 send_dump_data_t
*sdd
= arg
;
1278 boolean_t needagain
, progress
;
1280 if (!sdd
->replicate
)
1281 return (dump_filesystem(rzhp
, sdd
));
1283 /* Mark the clone origin snapshots. */
1284 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1285 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1287 uint64_t origin_guid
= 0;
1289 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1290 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1291 if (origin_guid
!= 0) {
1293 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1294 origin_guid
, &snapname
);
1295 if (origin_nv
!= NULL
) {
1296 nvlist_t
*snapprops
;
1297 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1298 "snapprops", &snapprops
));
1299 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1300 snapname
, &snapprops
));
1301 VERIFY(0 == nvlist_add_boolean(
1302 snapprops
, "is_clone_origin"));
1307 needagain
= progress
= B_FALSE
;
1308 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1309 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1310 nvlist_t
*fslist
, *parent_nv
;
1314 uint64_t origin_guid
= 0;
1315 uint64_t parent_guid
= 0;
1317 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1318 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1321 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1322 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1323 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1326 if (parent_guid
!= 0) {
1327 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1328 if (!nvlist_exists(parent_nv
, "sent")) {
1329 /* parent has not been sent; skip this one */
1335 if (origin_guid
!= 0) {
1336 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1338 if (origin_nv
!= NULL
&&
1339 !nvlist_exists(origin_nv
, "sent")) {
1341 * origin has not been sent yet;
1349 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1352 err
= dump_filesystem(zhp
, sdd
);
1353 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1364 /* clean out the sent flags in case we reuse this fss */
1365 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1366 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1369 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1370 (void) nvlist_remove_all(fslist
, "sent");
1377 * Generate a send stream for the dataset identified by the argument zhp.
1379 * The content of the send stream is the snapshot identified by
1380 * 'tosnap'. Incremental streams are requested in two ways:
1381 * - from the snapshot identified by "fromsnap" (if non-null) or
1382 * - from the origin of the dataset identified by zhp, which must
1383 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1386 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1387 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1388 * if "replicate" is set. If "doall" is set, dump all the intermediate
1389 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1390 * case too. If "props" is set, send properties.
1393 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1394 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1395 void *cb_arg
, nvlist_t
**debugnvp
)
1398 send_dump_data_t sdd
= { 0 };
1400 nvlist_t
*fss
= NULL
;
1401 avl_tree_t
*fsavl
= NULL
;
1402 static uint64_t holdseq
;
1406 dedup_arg_t dda
= { 0 };
1407 int featureflags
= 0;
1410 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1411 "cannot send '%s'"), zhp
->zfs_name
);
1413 if (fromsnap
&& fromsnap
[0] == '\0') {
1414 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1415 "zero-length incremental source"));
1416 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1419 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1421 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1422 if (version
>= ZPL_VERSION_SA
) {
1423 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1427 if (flags
->dedup
&& !flags
->dryrun
) {
1428 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1429 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1430 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
))) {
1431 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1432 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1435 dda
.outputfd
= outfd
;
1436 dda
.inputfd
= pipefd
[1];
1437 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1438 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1439 (void) close(pipefd
[0]);
1440 (void) close(pipefd
[1]);
1441 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1442 return (zfs_error(zhp
->zfs_hdl
,
1443 EZFS_THREADCREATEFAILED
, errbuf
));
1447 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1448 dmu_replay_record_t drr
= { 0 };
1449 char *packbuf
= NULL
;
1451 zio_cksum_t zc
= { { 0 } };
1453 if (flags
->replicate
|| flags
->props
) {
1456 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1458 VERIFY(0 == nvlist_add_string(hdrnv
,
1459 "fromsnap", fromsnap
));
1461 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1462 if (!flags
->replicate
) {
1463 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1467 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1468 fromsnap
, tosnap
, flags
->replicate
, &fss
, &fsavl
);
1471 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1472 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1482 if (!flags
->dryrun
) {
1483 /* write first begin record */
1484 drr
.drr_type
= DRR_BEGIN
;
1485 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1486 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1487 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1488 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1489 drr_versioninfo
, featureflags
);
1490 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1491 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1492 "%s@%s", zhp
->zfs_name
, tosnap
);
1493 drr
.drr_payloadlen
= buflen
;
1494 err
= cksum_and_write(&drr
, sizeof (drr
), &zc
, outfd
);
1496 /* write header nvlist */
1497 if (err
!= -1 && packbuf
!= NULL
) {
1498 err
= cksum_and_write(packbuf
, buflen
, &zc
,
1507 /* write end record */
1508 bzero(&drr
, sizeof (drr
));
1509 drr
.drr_type
= DRR_END
;
1510 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1511 err
= write(outfd
, &drr
, sizeof (drr
));
1521 /* dump each stream */
1522 sdd
.fromsnap
= fromsnap
;
1523 sdd
.tosnap
= tosnap
;
1525 sdd
.outfd
= pipefd
[0];
1528 sdd
.replicate
= flags
->replicate
;
1529 sdd
.doall
= flags
->doall
;
1530 sdd
.fromorigin
= flags
->fromorigin
;
1533 sdd
.verbose
= flags
->verbose
;
1534 sdd
.parsable
= flags
->parsable
;
1535 sdd
.progress
= flags
->progress
;
1536 sdd
.dryrun
= flags
->dryrun
;
1537 sdd
.large_block
= flags
->largeblock
;
1538 sdd
.embed_data
= flags
->embed_data
;
1539 sdd
.filter_cb
= filter_func
;
1540 sdd
.filter_cb_arg
= cb_arg
;
1542 sdd
.debugnv
= *debugnvp
;
1543 if (sdd
.verbose
&& sdd
.dryrun
)
1544 sdd
.std_out
= B_TRUE
;
1545 fout
= sdd
.std_out
? stdout
: stderr
;
1548 * Some flags require that we place user holds on the datasets that are
1549 * being sent so they don't get destroyed during the send. We can skip
1550 * this step if the pool is imported read-only since the datasets cannot
1553 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1554 ZPOOL_PROP_READONLY
, NULL
) &&
1555 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1556 spa_version
>= SPA_VERSION_USERREFS
&&
1557 (flags
->doall
|| flags
->replicate
)) {
1559 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1560 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1561 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1562 if (sdd
.cleanup_fd
< 0) {
1566 sdd
.snapholds
= fnvlist_alloc();
1568 sdd
.cleanup_fd
= -1;
1569 sdd
.snapholds
= NULL
;
1571 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1573 * Do a verbose no-op dry run to get all the verbose output
1574 * or to gather snapshot hold's before generating any data,
1575 * then do a non-verbose real run to generate the streams.
1577 sdd
.dryrun
= B_TRUE
;
1578 err
= dump_filesystems(zhp
, &sdd
);
1583 if (flags
->verbose
) {
1584 if (flags
->parsable
) {
1585 (void) fprintf(fout
, "size\t%llu\n",
1586 (longlong_t
)sdd
.size
);
1589 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1590 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1591 "total estimated size is %s\n"), buf
);
1595 /* Ensure no snaps found is treated as an error. */
1601 /* Skip the second run if dryrun was requested. */
1605 if (sdd
.snapholds
!= NULL
) {
1606 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1610 fnvlist_free(sdd
.snapholds
);
1611 sdd
.snapholds
= NULL
;
1614 sdd
.dryrun
= B_FALSE
;
1615 sdd
.verbose
= B_FALSE
;
1618 err
= dump_filesystems(zhp
, &sdd
);
1619 fsavl_destroy(fsavl
);
1622 /* Ensure no snaps found is treated as an error. */
1623 if (err
== 0 && !sdd
.seento
)
1628 (void) pthread_cancel(tid
);
1629 (void) close(pipefd
[0]);
1630 (void) pthread_join(tid
, NULL
);
1633 if (sdd
.cleanup_fd
!= -1) {
1634 VERIFY(0 == close(sdd
.cleanup_fd
));
1635 sdd
.cleanup_fd
= -1;
1638 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1641 * write final end record. NB: want to do this even if
1642 * there was some error, because it might not be totally
1645 dmu_replay_record_t drr
= { 0 };
1646 drr
.drr_type
= DRR_END
;
1647 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1648 return (zfs_standard_error(zhp
->zfs_hdl
,
1653 return (err
|| sdd
.err
);
1656 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1658 fsavl_destroy(fsavl
);
1660 fnvlist_free(sdd
.snapholds
);
1662 if (sdd
.cleanup_fd
!= -1)
1663 VERIFY(0 == close(sdd
.cleanup_fd
));
1665 (void) pthread_cancel(tid
);
1666 (void) close(pipefd
[0]);
1667 (void) pthread_join(tid
, NULL
);
1673 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
1674 enum lzc_send_flags flags
)
1677 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1680 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1681 "warning: cannot send '%s'"), zhp
->zfs_name
);
1683 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
1687 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1688 "not an earlier snapshot from the same fs"));
1689 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1693 if (lzc_exists(zhp
->zfs_name
)) {
1694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1695 "incremental source (%s) does not exist"),
1698 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1701 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1702 "target is busy; if a filesystem, "
1703 "it must not be mounted"));
1704 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
1717 zfs_error_aux(hdl
, strerror(errno
));
1718 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1721 return (zfs_standard_error(hdl
, errno
, errbuf
));
1728 * Routines specific to "zfs recv"
1732 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1733 boolean_t byteswap
, zio_cksum_t
*zc
)
1740 rv
= read(fd
, cp
, len
);
1745 if (rv
< 0 || len
!= 0) {
1746 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1747 "failed to read from stream"));
1748 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
1749 "cannot receive")));
1754 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
1756 fletcher_4_incremental_native(buf
, ilen
, zc
);
1762 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
1763 boolean_t byteswap
, zio_cksum_t
*zc
)
1768 buf
= zfs_alloc(hdl
, len
);
1772 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
1778 err
= nvlist_unpack(buf
, len
, nvp
, 0);
1781 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
1782 "stream (malformed nvlist)"));
1789 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
1790 int baselen
, char *newname
, recvflags_t
*flags
)
1793 zfs_cmd_t zc
= {"\0"};
1795 prop_changelist_t
*clp
;
1798 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1801 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1802 flags
->force
? MS_FORCE
: 0);
1806 err
= changelist_prefix(clp
);
1810 zc
.zc_objset_type
= DMU_OST_ZFS
;
1811 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1814 (void) strcpy(newname
, tryname
);
1816 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
1818 if (flags
->verbose
) {
1819 (void) printf("attempting rename %s to %s\n",
1820 zc
.zc_name
, zc
.zc_value
);
1822 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1824 changelist_rename(clp
, name
, tryname
);
1829 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
1832 (void) snprintf(newname
, ZFS_MAXNAMELEN
, "%.*srecv-%u-%u",
1833 baselen
, name
, getpid(), seq
);
1834 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
1836 if (flags
->verbose
) {
1837 (void) printf("failed - trying rename %s to %s\n",
1838 zc
.zc_name
, zc
.zc_value
);
1840 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1842 changelist_rename(clp
, name
, newname
);
1843 if (err
&& flags
->verbose
) {
1844 (void) printf("failed (%u) - "
1845 "will try again on next pass\n", errno
);
1848 } else if (flags
->verbose
) {
1850 (void) printf("success\n");
1852 (void) printf("failed (%u)\n", errno
);
1855 (void) changelist_postfix(clp
);
1856 changelist_free(clp
);
1862 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
1863 char *newname
, recvflags_t
*flags
)
1865 zfs_cmd_t zc
= {"\0"};
1867 prop_changelist_t
*clp
;
1869 boolean_t defer
= B_FALSE
;
1872 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1875 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1876 flags
->force
? MS_FORCE
: 0);
1877 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
1878 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1879 spa_version
>= SPA_VERSION_USERREFS
)
1884 err
= changelist_prefix(clp
);
1888 zc
.zc_objset_type
= DMU_OST_ZFS
;
1889 zc
.zc_defer_destroy
= defer
;
1890 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1893 (void) printf("attempting destroy %s\n", zc
.zc_name
);
1894 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
1897 (void) printf("success\n");
1898 changelist_remove(clp
, zc
.zc_name
);
1901 (void) changelist_postfix(clp
);
1902 changelist_free(clp
);
1905 * Deferred destroy might destroy the snapshot or only mark it to be
1906 * destroyed later, and it returns success in either case.
1908 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
1909 ZFS_TYPE_SNAPSHOT
))) {
1910 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
1916 typedef struct guid_to_name_data
{
1920 } guid_to_name_data_t
;
1923 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
1925 guid_to_name_data_t
*gtnd
= arg
;
1928 if (gtnd
->skip
!= NULL
&&
1929 strcmp(zhp
->zfs_name
, gtnd
->skip
) == 0) {
1933 if (zhp
->zfs_dmustats
.dds_guid
== gtnd
->guid
) {
1934 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
1939 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
1945 * Attempt to find the local dataset associated with this guid. In the case of
1946 * multiple matches, we attempt to find the "best" match by searching
1947 * progressively larger portions of the hierarchy. This allows one to send a
1948 * tree of datasets individually and guarantee that we will find the source
1949 * guid within that hierarchy, even if there are multiple matches elsewhere.
1952 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
1955 /* exhaustive search all local snapshots */
1956 char pname
[ZFS_MAXNAMELEN
];
1957 guid_to_name_data_t gtnd
;
1966 (void) strlcpy(pname
, parent
, sizeof (pname
));
1969 * Search progressively larger portions of the hierarchy. This will
1970 * select the "most local" version of the origin snapshot in the case
1971 * that there are multiple matching snapshots in the system.
1973 while ((cp
= strrchr(pname
, '/')) != NULL
) {
1975 /* Chop off the last component and open the parent */
1977 zhp
= make_dataset_handle(hdl
, pname
);
1982 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
1988 * Remember the dataset that we already searched, so we
1989 * skip it next time through.
1998 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1999 * guid1 is after guid2.
2002 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
2003 uint64_t guid1
, uint64_t guid2
)
2006 char *fsname
, *snapname
;
2007 char buf
[ZFS_MAXNAMELEN
];
2009 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2010 uint64_t create1
, create2
;
2017 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2018 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2019 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2020 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2021 if (guid1hdl
== NULL
)
2024 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2025 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2026 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2027 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2028 if (guid2hdl
== NULL
) {
2029 zfs_close(guid1hdl
);
2033 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2034 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2036 if (create1
< create2
)
2038 else if (create1
> create2
)
2043 zfs_close(guid1hdl
);
2044 zfs_close(guid2hdl
);
2050 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2051 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2054 nvlist_t
*local_nv
, *deleted
= NULL
;
2055 avl_tree_t
*local_avl
;
2056 nvpair_t
*fselem
, *nextfselem
;
2058 char newname
[ZFS_MAXNAMELEN
];
2061 boolean_t needagain
, progress
, recursive
;
2064 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2066 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2073 needagain
= progress
= B_FALSE
;
2075 VERIFY(0 == nvlist_alloc(&deleted
, NV_UNIQUE_NAME
, 0));
2077 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2078 recursive
, &local_nv
, &local_avl
)) != 0)
2082 * Process deletes and renames
2084 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2085 fselem
; fselem
= nextfselem
) {
2086 nvlist_t
*nvfs
, *snaps
;
2087 nvlist_t
*stream_nvfs
= NULL
;
2088 nvpair_t
*snapelem
, *nextsnapelem
;
2089 uint64_t fromguid
= 0;
2090 uint64_t originguid
= 0;
2091 uint64_t stream_originguid
= 0;
2092 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2093 char *fsname
, *stream_fsname
;
2095 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2097 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2098 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2099 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2100 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2101 &parent_fromsnap_guid
));
2102 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2105 * First find the stream's fs, so we can check for
2106 * a different origin (due to "zfs promote")
2108 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2109 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2112 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2113 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2115 if (stream_nvfs
!= NULL
)
2119 /* check for promote */
2120 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2121 &stream_originguid
);
2122 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2123 switch (created_before(hdl
, local_avl
,
2124 stream_originguid
, originguid
)) {
2127 zfs_cmd_t zc
= {"\0"};
2128 nvlist_t
*origin_nvfs
;
2129 char *origin_fsname
;
2132 (void) printf("promoting %s\n", fsname
);
2134 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2136 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2137 "name", &origin_fsname
));
2138 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2139 sizeof (zc
.zc_value
));
2140 (void) strlcpy(zc
.zc_name
, fsname
,
2141 sizeof (zc
.zc_name
));
2142 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2150 fsavl_destroy(local_avl
);
2151 nvlist_free(local_nv
);
2155 * We had/have the wrong origin, therefore our
2156 * list of snapshots is wrong. Need to handle
2157 * them on the next pass.
2163 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2164 snapelem
; snapelem
= nextsnapelem
) {
2166 char *stream_snapname
;
2167 nvlist_t
*found
, *props
;
2169 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2171 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2172 found
= fsavl_find(stream_avl
, thisguid
,
2175 /* check for delete */
2176 if (found
== NULL
) {
2177 char name
[ZFS_MAXNAMELEN
];
2182 (void) snprintf(name
, sizeof (name
), "%s@%s",
2183 fsname
, nvpair_name(snapelem
));
2185 error
= recv_destroy(hdl
, name
,
2186 strlen(fsname
)+1, newname
, flags
);
2191 sprintf(guidname
, "%llu",
2192 (u_longlong_t
)thisguid
);
2193 nvlist_add_boolean(deleted
, guidname
);
2197 stream_nvfs
= found
;
2199 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2200 &props
) && 0 == nvlist_lookup_nvlist(props
,
2201 stream_snapname
, &props
)) {
2202 zfs_cmd_t zc
= {"\0"};
2204 zc
.zc_cookie
= B_TRUE
; /* received */
2205 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2206 "%s@%s", fsname
, nvpair_name(snapelem
));
2207 if (zcmd_write_src_nvlist(hdl
, &zc
,
2209 (void) zfs_ioctl(hdl
,
2210 ZFS_IOC_SET_PROP
, &zc
);
2211 zcmd_free_nvlists(&zc
);
2215 /* check for different snapname */
2216 if (strcmp(nvpair_name(snapelem
),
2217 stream_snapname
) != 0) {
2218 char name
[ZFS_MAXNAMELEN
];
2219 char tryname
[ZFS_MAXNAMELEN
];
2221 (void) snprintf(name
, sizeof (name
), "%s@%s",
2222 fsname
, nvpair_name(snapelem
));
2223 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2224 fsname
, stream_snapname
);
2226 error
= recv_rename(hdl
, name
, tryname
,
2227 strlen(fsname
)+1, newname
, flags
);
2234 if (strcmp(stream_snapname
, fromsnap
) == 0)
2235 fromguid
= thisguid
;
2238 /* check for delete */
2239 if (stream_nvfs
== NULL
) {
2243 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2249 sprintf(guidname
, "%llu",
2250 (u_longlong_t
) parent_fromsnap_guid
);
2251 nvlist_add_boolean(deleted
, guidname
);
2255 if (fromguid
== 0) {
2256 if (flags
->verbose
) {
2257 (void) printf("local fs %s does not have "
2258 "fromsnap (%s in stream); must have "
2259 "been deleted locally; ignoring\n",
2265 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2266 "name", &stream_fsname
));
2267 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2268 "parentfromsnap", &stream_parent_fromsnap_guid
));
2270 s1
= strrchr(fsname
, '/');
2271 s2
= strrchr(stream_fsname
, '/');
2274 * Check if we're going to rename based on parent guid change
2275 * and the current parent guid was also deleted. If it was then
2276 * rename will fail and is likely unneeded, so avoid this and
2277 * force an early retry to determine the new
2278 * parent_fromsnap_guid.
2280 if (stream_parent_fromsnap_guid
!= 0 &&
2281 parent_fromsnap_guid
!= 0 &&
2282 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
2283 sprintf(guidname
, "%llu",
2284 (u_longlong_t
) parent_fromsnap_guid
);
2285 if (nvlist_exists(deleted
, guidname
)) {
2293 * Check for rename. If the exact receive path is specified, it
2294 * does not count as a rename, but we still need to check the
2295 * datasets beneath it.
2297 if ((stream_parent_fromsnap_guid
!= 0 &&
2298 parent_fromsnap_guid
!= 0 &&
2299 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2300 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2301 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2303 char tryname
[ZFS_MAXNAMELEN
];
2305 parent
= fsavl_find(local_avl
,
2306 stream_parent_fromsnap_guid
, NULL
);
2308 * NB: parent might not be found if we used the
2309 * tosnap for stream_parent_fromsnap_guid,
2310 * because the parent is a newly-created fs;
2311 * we'll be able to rename it after we recv the
2314 if (parent
!= NULL
) {
2317 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2319 (void) snprintf(tryname
, sizeof (tryname
),
2320 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2323 if (flags
->verbose
) {
2324 (void) printf("local fs %s new parent "
2325 "not found\n", fsname
);
2331 error
= recv_rename(hdl
, fsname
, tryname
,
2332 strlen(tofs
)+1, newname
, flags
);
2334 if (renamed
!= NULL
&& newname
[0] != '\0') {
2335 VERIFY(0 == nvlist_add_boolean(renamed
,
2347 fsavl_destroy(local_avl
);
2348 nvlist_free(local_nv
);
2349 nvlist_free(deleted
);
2351 if (needagain
&& progress
) {
2352 /* do another pass to fix up temporary names */
2354 (void) printf("another pass:\n");
2362 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2363 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2364 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2366 nvlist_t
*stream_nv
= NULL
;
2367 avl_tree_t
*stream_avl
= NULL
;
2368 char *fromsnap
= NULL
;
2370 char tofs
[ZFS_MAXNAMELEN
];
2371 char sendfs
[ZFS_MAXNAMELEN
];
2373 dmu_replay_record_t drre
;
2375 boolean_t anyerr
= B_FALSE
;
2376 boolean_t softerr
= B_FALSE
;
2377 boolean_t recursive
;
2379 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2382 assert(drr
->drr_type
== DRR_BEGIN
);
2383 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2384 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2385 DMU_COMPOUNDSTREAM
);
2388 * Read in the nvlist from the stream.
2390 if (drr
->drr_payloadlen
!= 0) {
2391 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2392 &stream_nv
, flags
->byteswap
, zc
);
2394 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2399 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2402 if (recursive
&& strchr(destname
, '@')) {
2403 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2404 "cannot specify snapshot name for multi-snapshot stream"));
2405 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2410 * Read in the end record and verify checksum.
2412 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2413 flags
->byteswap
, NULL
)))
2415 if (flags
->byteswap
) {
2416 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2417 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2418 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2419 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2420 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2421 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2422 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2423 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2424 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2426 if (drre
.drr_type
!= DRR_END
) {
2427 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2430 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2431 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2432 "incorrect header checksum"));
2433 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2437 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2439 if (drr
->drr_payloadlen
!= 0) {
2440 nvlist_t
*stream_fss
;
2442 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2444 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2445 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2446 "couldn't allocate avl tree"));
2447 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2451 if (fromsnap
!= NULL
) {
2452 nvlist_t
*renamed
= NULL
;
2453 nvpair_t
*pair
= NULL
;
2455 (void) strlcpy(tofs
, destname
, ZFS_MAXNAMELEN
);
2456 if (flags
->isprefix
) {
2457 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2460 if (flags
->istail
) {
2461 cp
= strrchr(drrb
->drr_toname
, '/');
2463 (void) strlcat(tofs
, "/",
2467 i
= (cp
- drrb
->drr_toname
);
2470 i
= strcspn(drrb
->drr_toname
, "/@");
2472 /* zfs_receive_one() will create_parents() */
2473 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2475 *strchr(tofs
, '@') = '\0';
2478 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2479 VERIFY(0 == nvlist_alloc(&renamed
,
2480 NV_UNIQUE_NAME
, 0));
2483 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2484 stream_nv
, stream_avl
, renamed
);
2486 /* Unmount renamed filesystems before receiving. */
2487 while ((pair
= nvlist_next_nvpair(renamed
,
2490 prop_changelist_t
*clp
= NULL
;
2492 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2493 ZFS_TYPE_FILESYSTEM
);
2495 clp
= changelist_gather(zhp
,
2496 ZFS_PROP_MOUNTPOINT
, 0, 0);
2500 changelist_prefix(clp
);
2501 changelist_free(clp
);
2506 nvlist_free(renamed
);
2511 * Get the fs specified by the first path in the stream (the top level
2512 * specified by 'zfs send') and pass it to each invocation of
2513 * zfs_receive_one().
2515 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2517 if ((cp
= strchr(sendfs
, '@')) != NULL
)
2520 /* Finally, receive each contained stream */
2523 * we should figure out if it has a recoverable
2524 * error, in which case do a recv_skip() and drive on.
2525 * Note, if we fail due to already having this guid,
2526 * zfs_receive_one() will take care of it (ie,
2527 * recv_skip() and return 0).
2529 error
= zfs_receive_impl(hdl
, destname
, flags
, fd
,
2530 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2532 if (error
== ENODATA
) {
2537 } while (error
== 0);
2539 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2541 * Now that we have the fs's they sent us, try the
2544 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2545 stream_nv
, stream_avl
, NULL
);
2549 fsavl_destroy(stream_avl
);
2551 nvlist_free(stream_nv
);
2560 trunc_prop_errs(int truncated
)
2562 ASSERT(truncated
!= 0);
2565 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2566 "1 more property could not be set\n"));
2568 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2569 "%d more properties could not be set\n"), truncated
);
2573 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2575 dmu_replay_record_t
*drr
;
2576 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2579 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2580 "cannot receive:"));
2582 /* XXX would be great to use lseek if possible... */
2585 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2586 byteswap
, NULL
) == 0) {
2588 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2590 switch (drr
->drr_type
) {
2592 /* NB: not to be used on v2 stream packages */
2593 if (drr
->drr_payloadlen
!= 0) {
2594 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2595 "invalid substream header"));
2596 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2606 drr
->drr_u
.drr_object
.drr_bonuslen
=
2607 BSWAP_32(drr
->drr_u
.drr_object
.
2610 (void) recv_read(hdl
, fd
, buf
,
2611 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2617 drr
->drr_u
.drr_write
.drr_length
=
2618 BSWAP_64(drr
->drr_u
.drr_write
.drr_length
);
2620 (void) recv_read(hdl
, fd
, buf
,
2621 drr
->drr_u
.drr_write
.drr_length
, B_FALSE
, NULL
);
2625 drr
->drr_u
.drr_write
.drr_length
=
2626 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2628 (void) recv_read(hdl
, fd
, buf
,
2629 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2631 case DRR_WRITE_EMBEDDED
:
2633 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2634 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2637 (void) recv_read(hdl
, fd
, buf
,
2638 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2641 case DRR_WRITE_BYREF
:
2642 case DRR_FREEOBJECTS
:
2647 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2648 "invalid record type"));
2649 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2658 * Restores a backup of tosnap from the file descriptor specified by infd.
2661 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2662 recvflags_t
*flags
, dmu_replay_record_t
*drr
,
2663 dmu_replay_record_t
*drr_noswap
, const char *sendfs
,
2664 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2665 uint64_t *action_handlep
)
2667 zfs_cmd_t zc
= {"\0"};
2669 int ioctl_err
, ioctl_errno
, err
;
2671 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2673 char prop_errbuf
[1024];
2674 const char *chopprefix
;
2675 boolean_t newfs
= B_FALSE
;
2676 boolean_t stream_wantsnewfs
;
2677 uint64_t parent_snapguid
= 0;
2678 prop_changelist_t
*clp
= NULL
;
2679 nvlist_t
*snapprops_nvlist
= NULL
;
2680 zprop_errflags_t prop_errflags
;
2681 boolean_t recursive
;
2683 begin_time
= time(NULL
);
2685 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2688 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2691 if (stream_avl
!= NULL
) {
2693 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
2698 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
2700 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
2702 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
2704 if (flags
->canmountoff
) {
2705 VERIFY(0 == nvlist_add_uint64(props
,
2706 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
2708 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
2718 * Determine how much of the snapshot name stored in the stream
2719 * we are going to tack on to the name they specified on the
2720 * command line, and how much we are going to chop off.
2722 * If they specified a snapshot, chop the entire name stored in
2725 if (flags
->istail
) {
2727 * A filesystem was specified with -e. We want to tack on only
2728 * the tail of the sent snapshot path.
2730 if (strchr(tosnap
, '@')) {
2731 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2732 "argument - snapshot not allowed with -e"));
2733 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2736 chopprefix
= strrchr(sendfs
, '/');
2738 if (chopprefix
== NULL
) {
2740 * The tail is the poolname, so we need to
2741 * prepend a path separator.
2743 int len
= strlen(drrb
->drr_toname
);
2744 cp
= malloc(len
+ 2);
2746 (void) strcpy(&cp
[1], drrb
->drr_toname
);
2749 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
2751 } else if (flags
->isprefix
) {
2753 * A filesystem was specified with -d. We want to tack on
2754 * everything but the first element of the sent snapshot path
2755 * (all but the pool name).
2757 if (strchr(tosnap
, '@')) {
2758 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2759 "argument - snapshot not allowed with -d"));
2760 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2763 chopprefix
= strchr(drrb
->drr_toname
, '/');
2764 if (chopprefix
== NULL
)
2765 chopprefix
= strchr(drrb
->drr_toname
, '@');
2766 } else if (strchr(tosnap
, '@') == NULL
) {
2768 * If a filesystem was specified without -d or -e, we want to
2769 * tack on everything after the fs specified by 'zfs send'.
2771 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
2773 /* A snapshot was specified as an exact path (no -d or -e). */
2775 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2776 "cannot specify snapshot name for multi-snapshot "
2778 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2780 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
2783 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
2784 ASSERT(chopprefix
> drrb
->drr_toname
);
2785 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
2786 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
2787 chopprefix
[0] == '\0');
2790 * Determine name of destination snapshot, store in zc_value.
2792 (void) strcpy(zc
.zc_value
, tosnap
);
2793 (void) strlcat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
2795 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
2796 zcmd_free_nvlists(&zc
);
2797 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2801 * Determine the name of the origin snapshot, store in zc_string.
2803 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
2804 if (guid_to_name(hdl
, zc
.zc_value
,
2805 drrb
->drr_fromguid
, zc
.zc_string
) != 0) {
2806 zcmd_free_nvlists(&zc
);
2807 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2808 "local origin for clone %s does not exist"),
2810 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2813 (void) printf("found clone origin %s\n", zc
.zc_string
);
2816 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
2817 (drrb
->drr_flags
& DRR_FLAG_CLONE
));
2819 if (stream_wantsnewfs
) {
2821 * if the parent fs does not exist, look for it based on
2822 * the parent snap GUID
2824 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2825 "cannot receive new filesystem stream"));
2827 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2828 cp
= strrchr(zc
.zc_name
, '/');
2832 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2833 char suffix
[ZFS_MAXNAMELEN
];
2834 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
2835 if (guid_to_name(hdl
, zc
.zc_name
, parent_snapguid
,
2836 zc
.zc_value
) == 0) {
2837 *strchr(zc
.zc_value
, '@') = '\0';
2838 (void) strcat(zc
.zc_value
, suffix
);
2843 * if the fs does not exist, look for it based on the
2846 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2847 "cannot receive incremental stream"));
2849 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2850 *strchr(zc
.zc_name
, '@') = '\0';
2853 * If the exact receive path was specified and this is the
2854 * topmost path in the stream, then if the fs does not exist we
2855 * should look no further.
2857 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
2858 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
2859 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2860 char snap
[ZFS_MAXNAMELEN
];
2861 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
2862 if (guid_to_name(hdl
, zc
.zc_name
, drrb
->drr_fromguid
,
2863 zc
.zc_value
) == 0) {
2864 *strchr(zc
.zc_value
, '@') = '\0';
2865 (void) strcat(zc
.zc_value
, snap
);
2870 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2871 *strchr(zc
.zc_name
, '@') = '\0';
2873 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2877 * Destination fs exists. Therefore this should either
2878 * be an incremental, or the stream specifies a new fs
2879 * (full stream or clone) and they want us to blow it
2880 * away (and have therefore specified -F and removed any
2883 if (stream_wantsnewfs
) {
2884 if (!flags
->force
) {
2885 zcmd_free_nvlists(&zc
);
2886 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2887 "destination '%s' exists\n"
2888 "must specify -F to overwrite it"),
2890 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2892 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2894 zcmd_free_nvlists(&zc
);
2895 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2896 "destination has snapshots (eg. %s)\n"
2897 "must destroy them to overwrite it"),
2899 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2903 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
2904 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
2905 zcmd_free_nvlists(&zc
);
2909 if (stream_wantsnewfs
&&
2910 zhp
->zfs_dmustats
.dds_origin
[0]) {
2911 zcmd_free_nvlists(&zc
);
2913 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2914 "destination '%s' is a clone\n"
2915 "must destroy it to overwrite it"),
2917 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2920 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
2921 stream_wantsnewfs
) {
2922 /* We can't do online recv in this case */
2923 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
2926 zcmd_free_nvlists(&zc
);
2929 if (changelist_prefix(clp
) != 0) {
2930 changelist_free(clp
);
2932 zcmd_free_nvlists(&zc
);
2939 * Destination filesystem does not exist. Therefore we better
2940 * be creating a new filesystem (either from a full backup, or
2941 * a clone). It would therefore be invalid if the user
2942 * specified only the pool name (i.e. if the destination name
2943 * contained no slash character).
2945 if (!stream_wantsnewfs
||
2946 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
2947 zcmd_free_nvlists(&zc
);
2948 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2949 "destination '%s' does not exist"), zc
.zc_name
);
2950 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2954 * Trim off the final dataset component so we perform the
2955 * recvbackup ioctl to the filesystems's parent.
2959 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
2960 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
2961 zcmd_free_nvlists(&zc
);
2962 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
2968 zc
.zc_begin_record
= drr_noswap
->drr_u
.drr_begin
;
2969 zc
.zc_cookie
= infd
;
2970 zc
.zc_guid
= flags
->force
;
2971 if (flags
->verbose
) {
2972 (void) printf("%s %s stream of %s into %s\n",
2973 flags
->dryrun
? "would receive" : "receiving",
2974 drrb
->drr_fromguid
? "incremental" : "full",
2975 drrb
->drr_toname
, zc
.zc_value
);
2976 (void) fflush(stdout
);
2979 if (flags
->dryrun
) {
2980 zcmd_free_nvlists(&zc
);
2981 return (recv_skip(hdl
, infd
, flags
->byteswap
));
2984 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
2985 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
2986 zc
.zc_cleanup_fd
= cleanup_fd
;
2987 zc
.zc_action_handle
= *action_handlep
;
2989 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
2990 ioctl_errno
= errno
;
2991 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
2994 nvlist_t
*prop_errors
;
2995 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
2996 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
2998 nvpair_t
*prop_err
= NULL
;
3000 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
3001 prop_err
)) != NULL
) {
3006 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
3007 (void) nvpair_value_int32(prop_err
, &intval
);
3008 if (strcmp(nvpair_name(prop_err
),
3009 ZPROP_N_MORE_ERRORS
) == 0) {
3010 trunc_prop_errs(intval
);
3013 (void) snprintf(tbuf
, sizeof (tbuf
),
3014 dgettext(TEXT_DOMAIN
,
3015 "cannot receive %s property on %s"),
3016 nvpair_name(prop_err
), zc
.zc_name
);
3017 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3020 nvlist_free(prop_errors
);
3023 zc
.zc_nvlist_dst
= 0;
3024 zc
.zc_nvlist_dst_size
= 0;
3025 zcmd_free_nvlists(&zc
);
3027 if (err
== 0 && snapprops_nvlist
) {
3028 zfs_cmd_t zc2
= {"\0"};
3030 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
3031 zc2
.zc_cookie
= B_TRUE
; /* received */
3032 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
3033 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
3034 zcmd_free_nvlists(&zc2
);
3038 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3040 * It may be that this snapshot already exists,
3041 * in which case we want to consume & ignore it
3042 * rather than failing.
3044 avl_tree_t
*local_avl
;
3045 nvlist_t
*local_nv
, *fs
;
3046 cp
= strchr(zc
.zc_value
, '@');
3049 * XXX Do this faster by just iterating over snaps in
3050 * this fs. Also if zc_value does not exist, we will
3051 * get a strange "does not exist" error message.
3054 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
3055 &local_nv
, &local_avl
) == 0) {
3057 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3058 fsavl_destroy(local_avl
);
3059 nvlist_free(local_nv
);
3062 if (flags
->verbose
) {
3063 (void) printf("snap %s already exists; "
3064 "ignoring\n", zc
.zc_value
);
3066 err
= ioctl_err
= recv_skip(hdl
, infd
,
3073 if (ioctl_err
!= 0) {
3074 switch (ioctl_errno
) {
3076 cp
= strchr(zc
.zc_value
, '@');
3078 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3079 "most recent snapshot of %s does not\n"
3080 "match incremental source"), zc
.zc_value
);
3081 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3085 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3086 "destination %s has been modified\n"
3087 "since most recent snapshot"), zc
.zc_name
);
3088 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3091 cp
= strchr(zc
.zc_value
, '@');
3093 /* it's the containing fs that exists */
3096 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3097 "destination already exists"));
3098 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3099 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3104 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3107 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3108 "invalid stream (checksum mismatch)"));
3109 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3112 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3113 "pool must be upgraded to receive this stream."));
3114 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3117 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3118 "destination %s space quota exceeded"), zc
.zc_name
);
3119 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3122 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3127 * Mount the target filesystem (if created). Also mount any
3128 * children of the target filesystem if we did a replication
3129 * receive (indicated by stream_avl being non-NULL).
3131 cp
= strchr(zc
.zc_value
, '@');
3132 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3136 h
= zfs_open(hdl
, zc
.zc_value
,
3137 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3139 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3141 } else if (newfs
|| stream_avl
) {
3143 * Track the first/top of hierarchy fs,
3144 * for mounting and sharing later.
3146 if (top_zfs
&& *top_zfs
== NULL
)
3147 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
3155 err
|= changelist_postfix(clp
);
3156 changelist_free(clp
);
3159 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3160 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3161 "failed to clear unreceived properties on %s"),
3163 (void) fprintf(stderr
, "\n");
3165 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3166 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3167 "failed to restore original properties on %s"),
3169 (void) fprintf(stderr
, "\n");
3172 if (err
|| ioctl_err
)
3175 *action_handlep
= zc
.zc_action_handle
;
3177 if (flags
->verbose
) {
3180 uint64_t bytes
= zc
.zc_cookie
;
3181 time_t delta
= time(NULL
) - begin_time
;
3184 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3185 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3187 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3195 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t
*flags
,
3196 int infd
, const char *sendfs
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
3197 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
3200 dmu_replay_record_t drr
, drr_noswap
;
3201 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3203 zio_cksum_t zcksum
= { { 0 } };
3204 uint64_t featureflags
;
3207 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3210 if (flags
->isprefix
&&
3211 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3212 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3213 "(%s) does not exist"), tosnap
);
3214 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3217 /* read in the BEGIN record */
3218 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3222 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3223 /* It's the double end record at the end of a package */
3227 /* the kernel needs the non-byteswapped begin record */
3230 flags
->byteswap
= B_FALSE
;
3231 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3233 * We computed the checksum in the wrong byteorder in
3234 * recv_read() above; do it again correctly.
3236 bzero(&zcksum
, sizeof (zio_cksum_t
));
3237 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3238 flags
->byteswap
= B_TRUE
;
3240 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3241 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3242 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3243 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3244 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3245 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3246 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3247 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3248 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3251 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3252 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3253 "stream (bad magic number)"));
3254 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3257 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3258 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3260 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3261 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3262 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3263 "stream has unsupported feature, feature flags = %lx"),
3265 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3268 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3269 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3270 "stream (bad snapshot name)"));
3271 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3274 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3275 char nonpackage_sendfs
[ZFS_MAXNAMELEN
];
3276 if (sendfs
== NULL
) {
3278 * We were not called from zfs_receive_package(). Get
3279 * the fs specified by 'zfs send'.
3282 (void) strlcpy(nonpackage_sendfs
,
3283 drr
.drr_u
.drr_begin
.drr_toname
, ZFS_MAXNAMELEN
);
3284 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3286 sendfs
= nonpackage_sendfs
;
3288 return (zfs_receive_one(hdl
, infd
, tosnap
, flags
,
3289 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
,
3290 top_zfs
, cleanup_fd
, action_handlep
));
3292 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3293 DMU_COMPOUNDSTREAM
);
3294 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
,
3295 &drr
, &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3300 * Restores a backup of tosnap from the file descriptor specified by infd.
3301 * Return 0 on total success, -2 if some things couldn't be
3302 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3303 * (-1 will override -2).
3306 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t
*flags
,
3307 int infd
, avl_tree_t
*stream_avl
)
3309 char *top_zfs
= NULL
;
3312 uint64_t action_handle
= 0;
3316 * The only way fstat can fail is if we do not have a valid file
3319 if (fstat(infd
, &sb
) == -1) {
3325 #ifndef F_SETPIPE_SZ
3326 #define F_SETPIPE_SZ (F_SETLEASE + 7)
3327 #endif /* F_SETPIPE_SZ */
3329 #ifndef F_GETPIPE_SZ
3330 #define F_GETPIPE_SZ (F_GETLEASE + 7)
3331 #endif /* F_GETPIPE_SZ */
3334 * It is not uncommon for gigabytes to be processed in zfs receive.
3335 * Speculatively increase the buffer size via Linux-specific fcntl()
3338 if (S_ISFIFO(sb
.st_mode
)) {
3339 FILE *procf
= fopen("/proc/sys/fs/pipe-max-size", "r");
3341 if (procf
!= NULL
) {
3342 unsigned long max_psize
;
3344 if (fscanf(procf
, "%lu", &max_psize
) > 0) {
3345 cur_psize
= fcntl(infd
, F_GETPIPE_SZ
);
3346 if (cur_psize
> 0 &&
3347 max_psize
> (unsigned long) cur_psize
)
3348 (void) fcntl(infd
, F_SETPIPE_SZ
,
3354 #endif /* __linux__ */
3356 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3357 VERIFY(cleanup_fd
>= 0);
3359 err
= zfs_receive_impl(hdl
, tosnap
, flags
, infd
, NULL
, NULL
,
3360 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
);
3362 VERIFY(0 == close(cleanup_fd
));
3364 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3366 prop_changelist_t
*clp
;
3368 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3370 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3371 CL_GATHER_MOUNT_ALWAYS
, 0);
3374 /* mount and share received datasets */
3375 err
= changelist_postfix(clp
);
3376 changelist_free(clp
);
3379 if (zhp
== NULL
|| clp
== NULL
|| err
)