4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
41 #include <sys/mount.h>
42 #include <sys/mntent.h>
43 #include <sys/mnttab.h>
45 #include <sys/debug.h>
52 #include <libzfs_core.h>
54 #include "zfs_namecheck.h"
56 #include "zfs_fletcher.h"
57 #include "libzfs_impl.h"
58 #include <sys/zio_checksum.h>
60 #include <sys/socket.h>
62 /* in libzfs_dataset.c */
63 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
65 static int zfs_receive_impl(libzfs_handle_t
*, const char *, recvflags_t
*,
66 int, const char *, nvlist_t
*, avl_tree_t
*, char **, int, uint64_t *);
68 static const zio_cksum_t zero_cksum
= { { 0 } };
70 typedef struct dedup_arg
{
73 libzfs_handle_t
*dedup_hdl
;
76 typedef struct progress_arg
{
79 boolean_t pa_parsable
;
82 typedef struct dataref
{
88 typedef struct dedup_entry
{
89 struct dedup_entry
*dde_next
;
90 zio_cksum_t dde_chksum
;
95 #define MAX_DDT_PHYSMEM_PERCENT 20
96 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
98 typedef struct dedup_table
{
99 dedup_entry_t
**dedup_hash_array
;
100 umem_cache_t
*ddecache
;
101 uint64_t max_ddt_size
; /* max dedup table size in bytes */
102 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
109 high_order_bit(uint64_t n
)
113 for (count
= 0; n
!= 0; count
++)
119 ssread(void *buf
, size_t len
, FILE *stream
)
123 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
130 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
131 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
135 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
136 if (ddt
->ddt_full
== B_FALSE
) {
137 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
138 "Dedup table full. Deduplication will continue "
139 "with existing table entries"));
140 ddt
->ddt_full
= B_TRUE
;
145 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
147 assert(*ddepp
== NULL
);
148 dde
->dde_next
= NULL
;
149 dde
->dde_chksum
= *cs
;
150 dde
->dde_prop
= prop
;
153 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
159 * Using the specified dedup table, do a lookup for an entry with
160 * the checksum cs. If found, return the block's reference info
161 * in *dr. Otherwise, insert a new entry in the dedup table, using
162 * the reference information specified by *dr.
164 * return value: true - entry was found
165 * false - entry was not found
168 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
169 uint64_t prop
, dataref_t
*dr
)
172 dedup_entry_t
**ddepp
;
174 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
176 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
177 ddepp
= &((*ddepp
)->dde_next
)) {
178 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
179 (*ddepp
)->dde_prop
== prop
) {
180 *dr
= (*ddepp
)->dde_ref
;
184 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
189 cksum_and_write(const void *buf
, uint64_t len
, zio_cksum_t
*zc
, int outfd
)
191 fletcher_4_incremental_native(buf
, len
, zc
);
192 return (write(outfd
, buf
, len
));
196 * This function is started in a separate thread when the dedup option
197 * has been requested. The main send thread determines the list of
198 * snapshots to be included in the send stream and makes the ioctl calls
199 * for each one. But instead of having the ioctl send the output to the
200 * the output fd specified by the caller of zfs_send()), the
201 * ioctl is told to direct the output to a pipe, which is read by the
202 * alternate thread running THIS function. This function does the
204 * 1. building a dedup table (the DDT)
205 * 2. doing checksums on each data block and inserting a record in the DDT
206 * 3. looking for matching checksums, and
207 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
208 * a duplicate block is found.
209 * The output of this function then goes to the output fd requested
210 * by the caller of zfs_send().
215 dedup_arg_t
*dda
= arg
;
216 char *buf
= malloc(1<<20);
217 dmu_replay_record_t thedrr
;
218 dmu_replay_record_t
*drr
= &thedrr
;
219 struct drr_begin
*drrb
= &thedrr
.drr_u
.drr_begin
;
220 struct drr_end
*drre
= &thedrr
.drr_u
.drr_end
;
221 struct drr_object
*drro
= &thedrr
.drr_u
.drr_object
;
222 struct drr_write
*drrw
= &thedrr
.drr_u
.drr_write
;
223 struct drr_spill
*drrs
= &thedrr
.drr_u
.drr_spill
;
224 struct drr_write_embedded
*drrwe
= &thedrr
.drr_u
.drr_write_embedded
;
227 dmu_replay_record_t wbr_drr
= {0};
228 struct drr_write_byref
*wbr_drrr
= &wbr_drr
.drr_u
.drr_write_byref
;
230 zio_cksum_t stream_cksum
;
231 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
235 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
)/100,
236 SMALLEST_POSSIBLE_MAX_DDT_MB
<<20);
238 numbuckets
= ddt
.max_ddt_size
/(sizeof (dedup_entry_t
));
241 * numbuckets must be a power of 2. Increase number to
242 * a power of 2 if necessary.
244 if (!ISP2(numbuckets
))
245 numbuckets
= 1 << high_order_bit(numbuckets
);
247 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
248 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
249 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
250 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
251 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
252 ddt
.ddt_full
= B_FALSE
;
254 /* Initialize the write-by-reference block. */
255 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
256 wbr_drr
.drr_payloadlen
= 0;
258 outfd
= dda
->outputfd
;
259 ofp
= fdopen(dda
->inputfd
, "r");
260 while (ssread(drr
, sizeof (dmu_replay_record_t
), ofp
) != 0) {
262 switch (drr
->drr_type
) {
266 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
268 /* set the DEDUP feature flag for this stream */
269 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
270 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
271 DMU_BACKUP_FEATURE_DEDUPPROPS
);
272 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
274 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
275 &stream_cksum
, outfd
) == -1)
277 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
278 DMU_COMPOUNDSTREAM
&& drr
->drr_payloadlen
!= 0) {
279 int sz
= drr
->drr_payloadlen
;
285 (void) ssread(buf
, sz
, ofp
);
288 if (cksum_and_write(buf
, sz
, &stream_cksum
,
297 /* use the recalculated checksum */
298 ZIO_SET_CHECKSUM(&drre
->drr_checksum
,
299 stream_cksum
.zc_word
[0], stream_cksum
.zc_word
[1],
300 stream_cksum
.zc_word
[2], stream_cksum
.zc_word
[3]);
301 if ((write(outfd
, drr
,
302 sizeof (dmu_replay_record_t
))) == -1)
309 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
310 &stream_cksum
, outfd
) == -1)
312 if (drro
->drr_bonuslen
> 0) {
314 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
316 if (cksum_and_write(buf
,
317 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
318 &stream_cksum
, outfd
) == -1)
326 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
327 &stream_cksum
, outfd
) == -1)
329 (void) ssread(buf
, drrs
->drr_length
, ofp
);
330 if (cksum_and_write(buf
, drrs
->drr_length
,
331 &stream_cksum
, outfd
) == -1)
336 case DRR_FREEOBJECTS
:
338 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
339 &stream_cksum
, outfd
) == -1)
348 (void) ssread(buf
, drrw
->drr_length
, ofp
);
351 * Use the existing checksum if it's dedup-capable,
352 * else calculate a SHA256 checksum for it.
355 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
357 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
358 zio_cksum_t tmpsha256
;
360 zio_checksum_SHA256(buf
,
361 drrw
->drr_length
, &tmpsha256
);
363 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
364 BE_64(tmpsha256
.zc_word
[0]);
365 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
366 BE_64(tmpsha256
.zc_word
[1]);
367 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
368 BE_64(tmpsha256
.zc_word
[2]);
369 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
370 BE_64(tmpsha256
.zc_word
[3]);
371 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
372 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
375 dataref
.ref_guid
= drrw
->drr_toguid
;
376 dataref
.ref_object
= drrw
->drr_object
;
377 dataref
.ref_offset
= drrw
->drr_offset
;
379 if (ddt_update(dda
->dedup_hdl
, &ddt
,
380 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
382 /* block already present in stream */
383 wbr_drrr
->drr_object
= drrw
->drr_object
;
384 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
385 wbr_drrr
->drr_length
= drrw
->drr_length
;
386 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
387 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
388 wbr_drrr
->drr_refobject
=
390 wbr_drrr
->drr_refoffset
=
393 wbr_drrr
->drr_checksumtype
=
394 drrw
->drr_checksumtype
;
395 wbr_drrr
->drr_checksumflags
=
396 drrw
->drr_checksumtype
;
397 wbr_drrr
->drr_key
.ddk_cksum
=
398 drrw
->drr_key
.ddk_cksum
;
399 wbr_drrr
->drr_key
.ddk_prop
=
400 drrw
->drr_key
.ddk_prop
;
402 if (cksum_and_write(&wbr_drr
,
403 sizeof (dmu_replay_record_t
), &stream_cksum
,
407 /* block not previously seen */
408 if (cksum_and_write(drr
,
409 sizeof (dmu_replay_record_t
), &stream_cksum
,
412 if (cksum_and_write(buf
,
414 &stream_cksum
, outfd
) == -1)
420 case DRR_WRITE_EMBEDDED
:
422 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
423 &stream_cksum
, outfd
) == -1)
426 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
427 if (cksum_and_write(buf
,
428 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
429 &stream_cksum
, outfd
) == -1)
436 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
437 &stream_cksum
, outfd
) == -1)
443 (void) printf("INVALID record type 0x%x\n",
445 /* should never happen, so assert */
450 umem_cache_destroy(ddt
.ddecache
);
451 free(ddt
.dedup_hash_array
);
459 * Routines for dealing with the AVL tree of fs-nvlists
461 typedef struct fsavl_node
{
469 fsavl_compare(const void *arg1
, const void *arg2
)
471 const fsavl_node_t
*fn1
= arg1
;
472 const fsavl_node_t
*fn2
= arg2
;
474 if (fn1
->fn_guid
> fn2
->fn_guid
)
476 else if (fn1
->fn_guid
< fn2
->fn_guid
)
483 * Given the GUID of a snapshot, find its containing filesystem and
487 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
489 fsavl_node_t fn_find
;
492 fn_find
.fn_guid
= snapguid
;
494 fn
= avl_find(avl
, &fn_find
, NULL
);
497 *snapname
= fn
->fn_snapname
;
498 return (fn
->fn_nvfs
);
504 fsavl_destroy(avl_tree_t
*avl
)
513 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
520 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
523 fsavl_create(nvlist_t
*fss
)
526 nvpair_t
*fselem
= NULL
;
528 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
531 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
532 offsetof(fsavl_node_t
, fn_node
));
534 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
535 nvlist_t
*nvfs
, *snaps
;
536 nvpair_t
*snapelem
= NULL
;
538 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
539 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
542 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
546 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
547 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
548 fsavl_destroy(fsavl
);
552 fn
->fn_snapname
= nvpair_name(snapelem
);
556 * Note: if there are multiple snaps with the
557 * same GUID, we ignore all but one.
559 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
570 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
572 typedef struct send_data
{
573 uint64_t parent_fromsnap_guid
;
574 nvlist_t
*parent_snaps
;
577 const char *fromsnap
;
582 * The header nvlist is of the following format:
585 * "fromsnap" -> string (if incremental)
589 * "name" -> string (full name; for debugging)
590 * "parentfromsnap" -> number (guid of fromsnap in parent)
592 * "props" -> { name -> value (only if set here) }
593 * "snaps" -> { name (lastname) -> number (guid) }
594 * "snapprops" -> { name (lastname) -> { name -> value } }
596 * "origin" -> number (guid) (if clone)
597 * "sent" -> boolean (not on-disk)
605 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
608 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
610 send_data_t
*sd
= arg
;
611 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
615 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
617 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
619 * NB: if there is no fromsnap here (it's a newly created fs in
620 * an incremental replication), we will substitute the tosnap.
622 if ((sd
->fromsnap
&& strcmp(snapname
, sd
->fromsnap
) == 0) ||
623 (sd
->parent_fromsnap_guid
== 0 && sd
->tosnap
&&
624 strcmp(snapname
, sd
->tosnap
) == 0)) {
625 sd
->parent_fromsnap_guid
= guid
;
628 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
629 send_iterate_prop(zhp
, nv
);
630 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
638 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
640 nvpair_t
*elem
= NULL
;
642 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
643 char *propname
= nvpair_name(elem
);
644 zfs_prop_t prop
= zfs_name_to_prop(propname
);
647 if (!zfs_prop_user(propname
)) {
649 * Realistically, this should never happen. However,
650 * we want the ability to add DSL properties without
651 * needing to make incompatible version changes. We
652 * need to ignore unknown properties to allow older
653 * software to still send datasets containing these
654 * properties, with the unknown properties elided.
656 if (prop
== ZPROP_INVAL
)
659 if (zfs_prop_readonly(prop
))
663 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
664 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
665 prop
== ZFS_PROP_REFQUOTA
||
666 prop
== ZFS_PROP_REFRESERVATION
) {
669 verify(nvlist_lookup_uint64(propnv
,
670 ZPROP_VALUE
, &value
) == 0);
671 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
674 * May have no source before SPA_VERSION_RECVD_PROPS,
675 * but is still modifiable.
677 if (nvlist_lookup_string(propnv
,
678 ZPROP_SOURCE
, &source
) == 0) {
679 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
681 ZPROP_SOURCE_VAL_RECVD
) != 0))
686 if (nvlist_lookup_string(propnv
,
687 ZPROP_SOURCE
, &source
) != 0)
689 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
690 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
694 if (zfs_prop_user(propname
) ||
695 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
697 verify(nvlist_lookup_string(propnv
,
698 ZPROP_VALUE
, &value
) == 0);
699 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
702 verify(nvlist_lookup_uint64(propnv
,
703 ZPROP_VALUE
, &value
) == 0);
704 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
710 * recursively generate nvlists describing datasets. See comment
711 * for the data structure send_data_t above for description of contents
715 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
717 send_data_t
*sd
= arg
;
720 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
721 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
724 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
725 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
726 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
727 sd
->parent_fromsnap_guid
));
729 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
730 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
731 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
734 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
735 origin
->zfs_dmustats
.dds_guid
));
738 /* iterate over props */
739 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
740 send_iterate_prop(zhp
, nv
);
741 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
744 /* iterate over snaps, and set sd->parent_fromsnap_guid */
745 sd
->parent_fromsnap_guid
= 0;
746 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
747 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
748 (void) zfs_iter_snapshots(zhp
, B_FALSE
, send_iterate_snap
, sd
);
749 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
750 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
751 nvlist_free(sd
->parent_snaps
);
752 nvlist_free(sd
->snapprops
);
754 /* add this fs to nvlist */
755 (void) snprintf(guidstring
, sizeof (guidstring
),
756 "0x%llx", (longlong_t
)guid
);
757 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
760 /* iterate over children */
762 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
764 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
771 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
772 const char *tosnap
, boolean_t recursive
, nvlist_t
**nvlp
, avl_tree_t
**avlp
)
775 send_data_t sd
= { 0 };
778 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
780 return (EZFS_BADTYPE
);
782 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
783 sd
.fromsnap
= fromsnap
;
785 sd
.recursive
= recursive
;
787 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
795 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
806 * Routines specific to "zfs send"
808 typedef struct send_dump_data
{
809 /* these are all just the short snapname (the part after the @) */
810 const char *fromsnap
;
812 char prevsnap
[ZFS_MAXNAMELEN
];
813 uint64_t prevsnap_obj
;
814 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
815 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
;
821 snapfilter_cb_t
*filter_cb
;
824 char holdtag
[ZFS_MAXNAMELEN
];
830 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
831 boolean_t fromorigin
, uint64_t *sizep
)
833 zfs_cmd_t zc
= {"\0"};
834 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
836 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
837 assert(fromsnap_obj
== 0 || !fromorigin
);
839 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
840 zc
.zc_obj
= fromorigin
;
841 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
842 zc
.zc_fromobj
= fromsnap_obj
;
843 zc
.zc_guid
= 1; /* estimate flag */
845 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
847 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
848 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
852 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
853 "not an earlier snapshot from the same fs"));
854 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
857 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
858 ZFS_TYPE_SNAPSHOT
)) {
859 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
860 "incremental source (@%s) does not exist"),
863 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
876 zfs_error_aux(hdl
, strerror(errno
));
877 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
880 return (zfs_standard_error(hdl
, errno
, errbuf
));
884 *sizep
= zc
.zc_objset_type
;
890 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
891 * NULL) to the file descriptor specified by outfd.
894 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
895 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
898 zfs_cmd_t zc
= {"\0"};
899 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
902 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
903 assert(fromsnap_obj
== 0 || !fromorigin
);
905 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
906 zc
.zc_cookie
= outfd
;
907 zc
.zc_obj
= fromorigin
;
908 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
909 zc
.zc_fromobj
= fromsnap_obj
;
912 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
913 if (fromsnap
&& fromsnap
[0] != '\0') {
914 VERIFY(0 == nvlist_add_string(thisdbg
,
915 "fromsnap", fromsnap
));
918 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
920 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
921 "warning: cannot send '%s'"), zhp
->zfs_name
);
923 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
925 VERIFY(0 == nvlist_add_nvlist(debugnv
,
926 zhp
->zfs_name
, thisdbg
));
928 nvlist_free(thisdbg
);
932 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
933 "not an earlier snapshot from the same fs"));
934 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
937 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
938 ZFS_TYPE_SNAPSHOT
)) {
939 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
940 "incremental source (@%s) does not exist"),
943 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
956 zfs_error_aux(hdl
, strerror(errno
));
957 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
960 return (zfs_standard_error(hdl
, errno
, errbuf
));
965 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
966 nvlist_free(thisdbg
);
972 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
974 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
977 * zfs_send() only sets snapholds for sends that need them,
978 * e.g. replication and doall.
980 if (sdd
->snapholds
== NULL
)
983 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
987 send_progress_thread(void *arg
)
989 progress_arg_t
*pa
= arg
;
991 zfs_cmd_t zc
= {"\0"};
992 zfs_handle_t
*zhp
= pa
->pa_zhp
;
993 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
994 unsigned long long bytes
;
1000 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1001 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1003 if (!pa
->pa_parsable
)
1004 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1007 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1012 zc
.zc_cookie
= pa
->pa_fd
;
1013 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1014 return ((void *)-1);
1018 bytes
= zc
.zc_cookie
;
1020 if (pa
->pa_parsable
) {
1021 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1022 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1023 bytes
, zhp
->zfs_name
);
1025 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1026 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1027 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1028 buf
, zhp
->zfs_name
);
1034 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1036 send_dump_data_t
*sdd
= arg
;
1037 progress_arg_t pa
= { 0 };
1041 boolean_t isfromsnap
, istosnap
, fromorigin
;
1042 boolean_t exclude
= B_FALSE
;
1045 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1046 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1047 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1049 if (!sdd
->seenfrom
&& isfromsnap
) {
1050 gather_holds(zhp
, sdd
);
1051 sdd
->seenfrom
= B_TRUE
;
1052 (void) strcpy(sdd
->prevsnap
, thissnap
);
1053 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1058 if (sdd
->seento
|| !sdd
->seenfrom
) {
1063 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1065 sdd
->seento
= B_TRUE
;
1067 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1068 if (sdd
->replicate
) {
1070 nvlist_t
*snapprops
;
1072 * Filter out all intermediate snapshots except origin
1073 * snapshots needed to replicate clones.
1075 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1076 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1078 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1079 "snapprops", &snapprops
));
1080 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1081 thissnap
, &snapprops
));
1082 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1089 * If a filter function exists, call it to determine whether
1090 * this snapshot will be sent.
1092 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1093 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1095 * This snapshot is filtered out. Don't send it, and don't
1096 * set prevsnap_obj, so it will be as if this snapshot didn't
1097 * exist, and the next accepted snapshot will be sent as
1098 * an incremental from the last accepted one, or as the
1099 * first (and full) snapshot in the case of a replication,
1100 * non-incremental send.
1106 gather_holds(zhp
, sdd
);
1107 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1108 (sdd
->fromorigin
|| sdd
->replicate
);
1112 err
= estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1115 if (sdd
->parsable
) {
1116 if (sdd
->prevsnap
[0] != '\0') {
1117 (void) fprintf(stderr
, "incremental\t%s\t%s",
1118 sdd
->prevsnap
, zhp
->zfs_name
);
1120 (void) fprintf(stderr
, "full\t%s",
1124 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1125 "send from @%s to %s"),
1126 sdd
->prevsnap
, zhp
->zfs_name
);
1129 if (sdd
->parsable
) {
1130 (void) fprintf(stderr
, "\t%llu\n",
1134 zfs_nicenum(size
, buf
, sizeof (buf
));
1135 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1136 " estimated size is %s\n"), buf
);
1140 (void) fprintf(stderr
, "\n");
1146 * If progress reporting is requested, spawn a new thread to
1147 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1149 if (sdd
->progress
) {
1151 pa
.pa_fd
= sdd
->outfd
;
1152 pa
.pa_parsable
= sdd
->parsable
;
1154 if ((err
= pthread_create(&tid
, NULL
,
1155 send_progress_thread
, &pa
))) {
1161 enum lzc_send_flags flags
= 0;
1162 if (sdd
->embed_data
)
1163 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1165 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1166 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1168 if (sdd
->progress
) {
1169 (void) pthread_cancel(tid
);
1170 (void) pthread_join(tid
, NULL
);
1174 (void) strcpy(sdd
->prevsnap
, thissnap
);
1175 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1181 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1184 send_dump_data_t
*sdd
= arg
;
1185 boolean_t missingfrom
= B_FALSE
;
1186 zfs_cmd_t zc
= {"\0"};
1188 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1189 zhp
->zfs_name
, sdd
->tosnap
);
1190 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1191 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1192 "WARNING: could not send %s@%s: does not exist\n"),
1193 zhp
->zfs_name
, sdd
->tosnap
);
1198 if (sdd
->replicate
&& sdd
->fromsnap
) {
1200 * If this fs does not have fromsnap, and we're doing
1201 * recursive, we need to send a full stream from the
1202 * beginning (or an incremental from the origin if this
1203 * is a clone). If we're doing non-recursive, then let
1204 * them get the error.
1206 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1207 zhp
->zfs_name
, sdd
->fromsnap
);
1208 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1209 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1210 missingfrom
= B_TRUE
;
1214 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1215 sdd
->prevsnap_obj
= 0;
1216 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1217 sdd
->seenfrom
= B_TRUE
;
1219 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1220 if (!sdd
->seenfrom
) {
1221 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1222 "WARNING: could not send %s@%s:\n"
1223 "incremental source (%s@%s) does not exist\n"),
1224 zhp
->zfs_name
, sdd
->tosnap
,
1225 zhp
->zfs_name
, sdd
->fromsnap
);
1227 } else if (!sdd
->seento
) {
1228 if (sdd
->fromsnap
) {
1229 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1230 "WARNING: could not send %s@%s:\n"
1231 "incremental source (%s@%s) "
1232 "is not earlier than it\n"),
1233 zhp
->zfs_name
, sdd
->tosnap
,
1234 zhp
->zfs_name
, sdd
->fromsnap
);
1236 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1238 "could not send %s@%s: does not exist\n"),
1239 zhp
->zfs_name
, sdd
->tosnap
);
1248 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1250 send_dump_data_t
*sdd
= arg
;
1252 boolean_t needagain
, progress
;
1254 if (!sdd
->replicate
)
1255 return (dump_filesystem(rzhp
, sdd
));
1257 /* Mark the clone origin snapshots. */
1258 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1259 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1261 uint64_t origin_guid
= 0;
1263 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1264 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1265 if (origin_guid
!= 0) {
1267 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1268 origin_guid
, &snapname
);
1269 if (origin_nv
!= NULL
) {
1270 nvlist_t
*snapprops
;
1271 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1272 "snapprops", &snapprops
));
1273 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1274 snapname
, &snapprops
));
1275 VERIFY(0 == nvlist_add_boolean(
1276 snapprops
, "is_clone_origin"));
1281 needagain
= progress
= B_FALSE
;
1282 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1283 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1284 nvlist_t
*fslist
, *parent_nv
;
1288 uint64_t origin_guid
= 0;
1289 uint64_t parent_guid
= 0;
1291 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1292 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1295 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1296 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1297 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1300 if (parent_guid
!= 0) {
1301 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1302 if (!nvlist_exists(parent_nv
, "sent")) {
1303 /* parent has not been sent; skip this one */
1309 if (origin_guid
!= 0) {
1310 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1312 if (origin_nv
!= NULL
&&
1313 !nvlist_exists(origin_nv
, "sent")) {
1315 * origin has not been sent yet;
1323 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1326 err
= dump_filesystem(zhp
, sdd
);
1327 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1338 /* clean out the sent flags in case we reuse this fss */
1339 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1340 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1343 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1344 (void) nvlist_remove_all(fslist
, "sent");
1351 * Generate a send stream for the dataset identified by the argument zhp.
1353 * The content of the send stream is the snapshot identified by
1354 * 'tosnap'. Incremental streams are requested in two ways:
1355 * - from the snapshot identified by "fromsnap" (if non-null) or
1356 * - from the origin of the dataset identified by zhp, which must
1357 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1360 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1361 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1362 * if "replicate" is set. If "doall" is set, dump all the intermediate
1363 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1364 * case too. If "props" is set, send properties.
1367 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1368 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1369 void *cb_arg
, nvlist_t
**debugnvp
)
1372 send_dump_data_t sdd
= { 0 };
1374 nvlist_t
*fss
= NULL
;
1375 avl_tree_t
*fsavl
= NULL
;
1376 static uint64_t holdseq
;
1380 dedup_arg_t dda
= { 0 };
1381 int featureflags
= 0;
1383 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1384 "cannot send '%s'"), zhp
->zfs_name
);
1386 if (fromsnap
&& fromsnap
[0] == '\0') {
1387 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1388 "zero-length incremental source"));
1389 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1392 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1394 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1395 if (version
>= ZPL_VERSION_SA
) {
1396 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1400 if (flags
->dedup
&& !flags
->dryrun
) {
1401 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1402 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1403 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
))) {
1404 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1405 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1408 dda
.outputfd
= outfd
;
1409 dda
.inputfd
= pipefd
[1];
1410 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1411 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1412 (void) close(pipefd
[0]);
1413 (void) close(pipefd
[1]);
1414 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1415 return (zfs_error(zhp
->zfs_hdl
,
1416 EZFS_THREADCREATEFAILED
, errbuf
));
1420 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1421 dmu_replay_record_t drr
= { 0 };
1422 char *packbuf
= NULL
;
1424 zio_cksum_t zc
= { { 0 } };
1426 if (flags
->replicate
|| flags
->props
) {
1429 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1431 VERIFY(0 == nvlist_add_string(hdrnv
,
1432 "fromsnap", fromsnap
));
1434 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1435 if (!flags
->replicate
) {
1436 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1440 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1441 fromsnap
, tosnap
, flags
->replicate
, &fss
, &fsavl
);
1444 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1445 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1455 if (!flags
->dryrun
) {
1456 /* write first begin record */
1457 drr
.drr_type
= DRR_BEGIN
;
1458 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1459 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1460 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1461 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1462 drr_versioninfo
, featureflags
);
1463 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1464 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1465 "%s@%s", zhp
->zfs_name
, tosnap
);
1466 drr
.drr_payloadlen
= buflen
;
1467 err
= cksum_and_write(&drr
, sizeof (drr
), &zc
, outfd
);
1469 /* write header nvlist */
1470 if (err
!= -1 && packbuf
!= NULL
) {
1471 err
= cksum_and_write(packbuf
, buflen
, &zc
,
1480 /* write end record */
1481 bzero(&drr
, sizeof (drr
));
1482 drr
.drr_type
= DRR_END
;
1483 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1484 err
= write(outfd
, &drr
, sizeof (drr
));
1494 /* dump each stream */
1495 sdd
.fromsnap
= fromsnap
;
1496 sdd
.tosnap
= tosnap
;
1498 sdd
.outfd
= pipefd
[0];
1501 sdd
.replicate
= flags
->replicate
;
1502 sdd
.doall
= flags
->doall
;
1503 sdd
.fromorigin
= flags
->fromorigin
;
1506 sdd
.verbose
= flags
->verbose
;
1507 sdd
.parsable
= flags
->parsable
;
1508 sdd
.progress
= flags
->progress
;
1509 sdd
.dryrun
= flags
->dryrun
;
1510 sdd
.embed_data
= flags
->embed_data
;
1511 sdd
.filter_cb
= filter_func
;
1512 sdd
.filter_cb_arg
= cb_arg
;
1514 sdd
.debugnv
= *debugnvp
;
1517 * Some flags require that we place user holds on the datasets that are
1518 * being sent so they don't get destroyed during the send. We can skip
1519 * this step if the pool is imported read-only since the datasets cannot
1522 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1523 ZPOOL_PROP_READONLY
, NULL
) &&
1524 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1525 spa_version
>= SPA_VERSION_USERREFS
&&
1526 (flags
->doall
|| flags
->replicate
)) {
1528 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1529 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1530 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1531 if (sdd
.cleanup_fd
< 0) {
1535 sdd
.snapholds
= fnvlist_alloc();
1537 sdd
.cleanup_fd
= -1;
1538 sdd
.snapholds
= NULL
;
1540 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1542 * Do a verbose no-op dry run to get all the verbose output
1543 * or to gather snapshot hold's before generating any data,
1544 * then do a non-verbose real run to generate the streams.
1546 sdd
.dryrun
= B_TRUE
;
1547 err
= dump_filesystems(zhp
, &sdd
);
1552 if (flags
->verbose
) {
1553 if (flags
->parsable
) {
1554 (void) fprintf(stderr
, "size\t%llu\n",
1555 (longlong_t
)sdd
.size
);
1558 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1559 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1560 "total estimated size is %s\n"), buf
);
1564 /* Ensure no snaps found is treated as an error. */
1570 /* Skip the second run if dryrun was requested. */
1574 if (sdd
.snapholds
!= NULL
) {
1575 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1579 fnvlist_free(sdd
.snapholds
);
1580 sdd
.snapholds
= NULL
;
1583 sdd
.dryrun
= B_FALSE
;
1584 sdd
.verbose
= B_FALSE
;
1587 err
= dump_filesystems(zhp
, &sdd
);
1588 fsavl_destroy(fsavl
);
1591 /* Ensure no snaps found is treated as an error. */
1592 if (err
== 0 && !sdd
.seento
)
1597 (void) pthread_cancel(tid
);
1598 (void) close(pipefd
[0]);
1599 (void) pthread_join(tid
, NULL
);
1602 if (sdd
.cleanup_fd
!= -1) {
1603 VERIFY(0 == close(sdd
.cleanup_fd
));
1604 sdd
.cleanup_fd
= -1;
1607 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1610 * write final end record. NB: want to do this even if
1611 * there was some error, because it might not be totally
1614 dmu_replay_record_t drr
= { 0 };
1615 drr
.drr_type
= DRR_END
;
1616 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1617 return (zfs_standard_error(zhp
->zfs_hdl
,
1622 return (err
|| sdd
.err
);
1625 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1627 fsavl_destroy(fsavl
);
1629 fnvlist_free(sdd
.snapholds
);
1631 if (sdd
.cleanup_fd
!= -1)
1632 VERIFY(0 == close(sdd
.cleanup_fd
));
1634 (void) pthread_cancel(tid
);
1635 (void) close(pipefd
[0]);
1636 (void) pthread_join(tid
, NULL
);
1642 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
1643 enum lzc_send_flags flags
)
1646 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1649 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1650 "warning: cannot send '%s'"), zhp
->zfs_name
);
1652 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
1656 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1657 "not an earlier snapshot from the same fs"));
1658 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1662 if (lzc_exists(zhp
->zfs_name
)) {
1663 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1664 "incremental source (%s) does not exist"),
1667 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1670 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1671 "target is busy; if a filesystem, "
1672 "it must not be mounted"));
1673 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
1686 zfs_error_aux(hdl
, strerror(errno
));
1687 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1690 return (zfs_standard_error(hdl
, errno
, errbuf
));
1697 * Routines specific to "zfs recv"
1701 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1702 boolean_t byteswap
, zio_cksum_t
*zc
)
1709 rv
= read(fd
, cp
, len
);
1714 if (rv
< 0 || len
!= 0) {
1715 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1716 "failed to read from stream"));
1717 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
1718 "cannot receive")));
1723 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
1725 fletcher_4_incremental_native(buf
, ilen
, zc
);
1731 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
1732 boolean_t byteswap
, zio_cksum_t
*zc
)
1737 buf
= zfs_alloc(hdl
, len
);
1741 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
1747 err
= nvlist_unpack(buf
, len
, nvp
, 0);
1750 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
1751 "stream (malformed nvlist)"));
1758 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
1759 int baselen
, char *newname
, recvflags_t
*flags
)
1762 zfs_cmd_t zc
= {"\0"};
1764 prop_changelist_t
*clp
;
1767 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1770 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1771 flags
->force
? MS_FORCE
: 0);
1775 err
= changelist_prefix(clp
);
1779 zc
.zc_objset_type
= DMU_OST_ZFS
;
1780 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1783 (void) strcpy(newname
, tryname
);
1785 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
1787 if (flags
->verbose
) {
1788 (void) printf("attempting rename %s to %s\n",
1789 zc
.zc_name
, zc
.zc_value
);
1791 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1793 changelist_rename(clp
, name
, tryname
);
1798 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
1801 (void) snprintf(newname
, ZFS_MAXNAMELEN
, "%.*srecv-%u-%u",
1802 baselen
, name
, getpid(), seq
);
1803 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
1805 if (flags
->verbose
) {
1806 (void) printf("failed - trying rename %s to %s\n",
1807 zc
.zc_name
, zc
.zc_value
);
1809 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1811 changelist_rename(clp
, name
, newname
);
1812 if (err
&& flags
->verbose
) {
1813 (void) printf("failed (%u) - "
1814 "will try again on next pass\n", errno
);
1817 } else if (flags
->verbose
) {
1819 (void) printf("success\n");
1821 (void) printf("failed (%u)\n", errno
);
1824 (void) changelist_postfix(clp
);
1825 changelist_free(clp
);
1831 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
1832 char *newname
, recvflags_t
*flags
)
1834 zfs_cmd_t zc
= {"\0"};
1836 prop_changelist_t
*clp
;
1838 boolean_t defer
= B_FALSE
;
1841 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1844 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1845 flags
->force
? MS_FORCE
: 0);
1846 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
1847 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1848 spa_version
>= SPA_VERSION_USERREFS
)
1853 err
= changelist_prefix(clp
);
1857 zc
.zc_objset_type
= DMU_OST_ZFS
;
1858 zc
.zc_defer_destroy
= defer
;
1859 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1862 (void) printf("attempting destroy %s\n", zc
.zc_name
);
1863 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
1866 (void) printf("success\n");
1867 changelist_remove(clp
, zc
.zc_name
);
1870 (void) changelist_postfix(clp
);
1871 changelist_free(clp
);
1874 * Deferred destroy might destroy the snapshot or only mark it to be
1875 * destroyed later, and it returns success in either case.
1877 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
1878 ZFS_TYPE_SNAPSHOT
))) {
1879 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
1885 typedef struct guid_to_name_data
{
1889 } guid_to_name_data_t
;
1892 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
1894 guid_to_name_data_t
*gtnd
= arg
;
1897 if (gtnd
->skip
!= NULL
&&
1898 strcmp(zhp
->zfs_name
, gtnd
->skip
) == 0) {
1902 if (zhp
->zfs_dmustats
.dds_guid
== gtnd
->guid
) {
1903 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
1908 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
1914 * Attempt to find the local dataset associated with this guid. In the case of
1915 * multiple matches, we attempt to find the "best" match by searching
1916 * progressively larger portions of the hierarchy. This allows one to send a
1917 * tree of datasets individually and guarantee that we will find the source
1918 * guid within that hierarchy, even if there are multiple matches elsewhere.
1921 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
1924 /* exhaustive search all local snapshots */
1925 char pname
[ZFS_MAXNAMELEN
];
1926 guid_to_name_data_t gtnd
;
1935 (void) strlcpy(pname
, parent
, sizeof (pname
));
1938 * Search progressively larger portions of the hierarchy. This will
1939 * select the "most local" version of the origin snapshot in the case
1940 * that there are multiple matching snapshots in the system.
1942 while ((cp
= strrchr(pname
, '/')) != NULL
) {
1944 /* Chop off the last component and open the parent */
1946 zhp
= make_dataset_handle(hdl
, pname
);
1951 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
1957 * Remember the dataset that we already searched, so we
1958 * skip it next time through.
1967 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1968 * guid1 is after guid2.
1971 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
1972 uint64_t guid1
, uint64_t guid2
)
1975 char *fsname
, *snapname
;
1976 char buf
[ZFS_MAXNAMELEN
];
1978 zfs_handle_t
*guid1hdl
, *guid2hdl
;
1979 uint64_t create1
, create2
;
1986 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
1987 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
1988 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
1989 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
1990 if (guid1hdl
== NULL
)
1993 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
1994 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
1995 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
1996 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
1997 if (guid2hdl
== NULL
) {
1998 zfs_close(guid1hdl
);
2002 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2003 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2005 if (create1
< create2
)
2007 else if (create1
> create2
)
2012 zfs_close(guid1hdl
);
2013 zfs_close(guid2hdl
);
2019 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2020 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2024 avl_tree_t
*local_avl
;
2025 nvpair_t
*fselem
, *nextfselem
;
2027 char newname
[ZFS_MAXNAMELEN
];
2029 boolean_t needagain
, progress
, recursive
;
2032 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2034 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2041 needagain
= progress
= B_FALSE
;
2043 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2044 recursive
, &local_nv
, &local_avl
)) != 0)
2048 * Process deletes and renames
2050 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2051 fselem
; fselem
= nextfselem
) {
2052 nvlist_t
*nvfs
, *snaps
;
2053 nvlist_t
*stream_nvfs
= NULL
;
2054 nvpair_t
*snapelem
, *nextsnapelem
;
2055 uint64_t fromguid
= 0;
2056 uint64_t originguid
= 0;
2057 uint64_t stream_originguid
= 0;
2058 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2059 char *fsname
, *stream_fsname
;
2061 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2063 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2064 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2065 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2066 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2067 &parent_fromsnap_guid
));
2068 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2071 * First find the stream's fs, so we can check for
2072 * a different origin (due to "zfs promote")
2074 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2075 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2078 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2079 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2081 if (stream_nvfs
!= NULL
)
2085 /* check for promote */
2086 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2087 &stream_originguid
);
2088 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2089 switch (created_before(hdl
, local_avl
,
2090 stream_originguid
, originguid
)) {
2093 zfs_cmd_t zc
= {"\0"};
2094 nvlist_t
*origin_nvfs
;
2095 char *origin_fsname
;
2098 (void) printf("promoting %s\n", fsname
);
2100 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2102 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2103 "name", &origin_fsname
));
2104 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2105 sizeof (zc
.zc_value
));
2106 (void) strlcpy(zc
.zc_name
, fsname
,
2107 sizeof (zc
.zc_name
));
2108 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2116 fsavl_destroy(local_avl
);
2117 nvlist_free(local_nv
);
2121 * We had/have the wrong origin, therefore our
2122 * list of snapshots is wrong. Need to handle
2123 * them on the next pass.
2129 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2130 snapelem
; snapelem
= nextsnapelem
) {
2132 char *stream_snapname
;
2133 nvlist_t
*found
, *props
;
2135 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2137 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2138 found
= fsavl_find(stream_avl
, thisguid
,
2141 /* check for delete */
2142 if (found
== NULL
) {
2143 char name
[ZFS_MAXNAMELEN
];
2148 (void) snprintf(name
, sizeof (name
), "%s@%s",
2149 fsname
, nvpair_name(snapelem
));
2151 error
= recv_destroy(hdl
, name
,
2152 strlen(fsname
)+1, newname
, flags
);
2160 stream_nvfs
= found
;
2162 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2163 &props
) && 0 == nvlist_lookup_nvlist(props
,
2164 stream_snapname
, &props
)) {
2165 zfs_cmd_t zc
= {"\0"};
2167 zc
.zc_cookie
= B_TRUE
; /* received */
2168 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2169 "%s@%s", fsname
, nvpair_name(snapelem
));
2170 if (zcmd_write_src_nvlist(hdl
, &zc
,
2172 (void) zfs_ioctl(hdl
,
2173 ZFS_IOC_SET_PROP
, &zc
);
2174 zcmd_free_nvlists(&zc
);
2178 /* check for different snapname */
2179 if (strcmp(nvpair_name(snapelem
),
2180 stream_snapname
) != 0) {
2181 char name
[ZFS_MAXNAMELEN
];
2182 char tryname
[ZFS_MAXNAMELEN
];
2184 (void) snprintf(name
, sizeof (name
), "%s@%s",
2185 fsname
, nvpair_name(snapelem
));
2186 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2187 fsname
, stream_snapname
);
2189 error
= recv_rename(hdl
, name
, tryname
,
2190 strlen(fsname
)+1, newname
, flags
);
2197 if (strcmp(stream_snapname
, fromsnap
) == 0)
2198 fromguid
= thisguid
;
2201 /* check for delete */
2202 if (stream_nvfs
== NULL
) {
2206 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2215 if (fromguid
== 0) {
2216 if (flags
->verbose
) {
2217 (void) printf("local fs %s does not have "
2218 "fromsnap (%s in stream); must have "
2219 "been deleted locally; ignoring\n",
2225 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2226 "name", &stream_fsname
));
2227 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2228 "parentfromsnap", &stream_parent_fromsnap_guid
));
2230 s1
= strrchr(fsname
, '/');
2231 s2
= strrchr(stream_fsname
, '/');
2234 * Check for rename. If the exact receive path is specified, it
2235 * does not count as a rename, but we still need to check the
2236 * datasets beneath it.
2238 if ((stream_parent_fromsnap_guid
!= 0 &&
2239 parent_fromsnap_guid
!= 0 &&
2240 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2241 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2242 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2244 char tryname
[ZFS_MAXNAMELEN
];
2246 parent
= fsavl_find(local_avl
,
2247 stream_parent_fromsnap_guid
, NULL
);
2249 * NB: parent might not be found if we used the
2250 * tosnap for stream_parent_fromsnap_guid,
2251 * because the parent is a newly-created fs;
2252 * we'll be able to rename it after we recv the
2255 if (parent
!= NULL
) {
2258 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2260 (void) snprintf(tryname
, sizeof (tryname
),
2261 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2264 if (flags
->verbose
) {
2265 (void) printf("local fs %s new parent "
2266 "not found\n", fsname
);
2272 error
= recv_rename(hdl
, fsname
, tryname
,
2273 strlen(tofs
)+1, newname
, flags
);
2275 if (renamed
!= NULL
&& newname
[0] != '\0') {
2276 VERIFY(0 == nvlist_add_boolean(renamed
,
2287 fsavl_destroy(local_avl
);
2288 nvlist_free(local_nv
);
2290 if (needagain
&& progress
) {
2291 /* do another pass to fix up temporary names */
2293 (void) printf("another pass:\n");
2301 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2302 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2303 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2305 nvlist_t
*stream_nv
= NULL
;
2306 avl_tree_t
*stream_avl
= NULL
;
2307 char *fromsnap
= NULL
;
2309 char tofs
[ZFS_MAXNAMELEN
];
2310 char sendfs
[ZFS_MAXNAMELEN
];
2312 dmu_replay_record_t drre
;
2314 boolean_t anyerr
= B_FALSE
;
2315 boolean_t softerr
= B_FALSE
;
2316 boolean_t recursive
;
2318 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2321 assert(drr
->drr_type
== DRR_BEGIN
);
2322 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2323 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2324 DMU_COMPOUNDSTREAM
);
2327 * Read in the nvlist from the stream.
2329 if (drr
->drr_payloadlen
!= 0) {
2330 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2331 &stream_nv
, flags
->byteswap
, zc
);
2333 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2338 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2341 if (recursive
&& strchr(destname
, '@')) {
2342 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2343 "cannot specify snapshot name for multi-snapshot stream"));
2344 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2349 * Read in the end record and verify checksum.
2351 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2352 flags
->byteswap
, NULL
)))
2354 if (flags
->byteswap
) {
2355 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2356 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2357 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2358 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2359 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2360 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2361 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2362 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2363 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2365 if (drre
.drr_type
!= DRR_END
) {
2366 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2369 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2370 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2371 "incorrect header checksum"));
2372 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2376 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2378 if (drr
->drr_payloadlen
!= 0) {
2379 nvlist_t
*stream_fss
;
2381 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2383 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2384 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2385 "couldn't allocate avl tree"));
2386 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2390 if (fromsnap
!= NULL
) {
2391 nvlist_t
*renamed
= NULL
;
2392 nvpair_t
*pair
= NULL
;
2394 (void) strlcpy(tofs
, destname
, ZFS_MAXNAMELEN
);
2395 if (flags
->isprefix
) {
2396 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2399 if (flags
->istail
) {
2400 cp
= strrchr(drrb
->drr_toname
, '/');
2402 (void) strlcat(tofs
, "/",
2406 i
= (cp
- drrb
->drr_toname
);
2409 i
= strcspn(drrb
->drr_toname
, "/@");
2411 /* zfs_receive_one() will create_parents() */
2412 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2414 *strchr(tofs
, '@') = '\0';
2417 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2418 VERIFY(0 == nvlist_alloc(&renamed
,
2419 NV_UNIQUE_NAME
, 0));
2422 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2423 stream_nv
, stream_avl
, renamed
);
2425 /* Unmount renamed filesystems before receiving. */
2426 while ((pair
= nvlist_next_nvpair(renamed
,
2429 prop_changelist_t
*clp
= NULL
;
2431 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2432 ZFS_TYPE_FILESYSTEM
);
2434 clp
= changelist_gather(zhp
,
2435 ZFS_PROP_MOUNTPOINT
, 0, 0);
2439 changelist_prefix(clp
);
2440 changelist_free(clp
);
2445 nvlist_free(renamed
);
2450 * Get the fs specified by the first path in the stream (the top level
2451 * specified by 'zfs send') and pass it to each invocation of
2452 * zfs_receive_one().
2454 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2456 if ((cp
= strchr(sendfs
, '@')) != NULL
)
2459 /* Finally, receive each contained stream */
2462 * we should figure out if it has a recoverable
2463 * error, in which case do a recv_skip() and drive on.
2464 * Note, if we fail due to already having this guid,
2465 * zfs_receive_one() will take care of it (ie,
2466 * recv_skip() and return 0).
2468 error
= zfs_receive_impl(hdl
, destname
, flags
, fd
,
2469 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2471 if (error
== ENODATA
) {
2476 } while (error
== 0);
2478 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2480 * Now that we have the fs's they sent us, try the
2483 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2484 stream_nv
, stream_avl
, NULL
);
2488 fsavl_destroy(stream_avl
);
2490 nvlist_free(stream_nv
);
2499 trunc_prop_errs(int truncated
)
2501 ASSERT(truncated
!= 0);
2504 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2505 "1 more property could not be set\n"));
2507 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2508 "%d more properties could not be set\n"), truncated
);
2512 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2514 dmu_replay_record_t
*drr
;
2515 void *buf
= malloc(1<<20);
2518 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2519 "cannot receive:"));
2521 /* XXX would be great to use lseek if possible... */
2524 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2525 byteswap
, NULL
) == 0) {
2527 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2529 switch (drr
->drr_type
) {
2531 /* NB: not to be used on v2 stream packages */
2532 if (drr
->drr_payloadlen
!= 0) {
2533 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2534 "invalid substream header"));
2535 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2545 drr
->drr_u
.drr_object
.drr_bonuslen
=
2546 BSWAP_32(drr
->drr_u
.drr_object
.
2549 (void) recv_read(hdl
, fd
, buf
,
2550 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2556 drr
->drr_u
.drr_write
.drr_length
=
2557 BSWAP_64(drr
->drr_u
.drr_write
.drr_length
);
2559 (void) recv_read(hdl
, fd
, buf
,
2560 drr
->drr_u
.drr_write
.drr_length
, B_FALSE
, NULL
);
2564 drr
->drr_u
.drr_write
.drr_length
=
2565 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2567 (void) recv_read(hdl
, fd
, buf
,
2568 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2570 case DRR_WRITE_EMBEDDED
:
2572 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2573 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2576 (void) recv_read(hdl
, fd
, buf
,
2577 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2580 case DRR_WRITE_BYREF
:
2581 case DRR_FREEOBJECTS
:
2586 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2587 "invalid record type"));
2588 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2597 * Restores a backup of tosnap from the file descriptor specified by infd.
2600 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2601 recvflags_t
*flags
, dmu_replay_record_t
*drr
,
2602 dmu_replay_record_t
*drr_noswap
, const char *sendfs
,
2603 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2604 uint64_t *action_handlep
)
2606 zfs_cmd_t zc
= {"\0"};
2608 int ioctl_err
, ioctl_errno
, err
;
2610 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2612 char prop_errbuf
[1024];
2613 const char *chopprefix
;
2614 boolean_t newfs
= B_FALSE
;
2615 boolean_t stream_wantsnewfs
;
2616 uint64_t parent_snapguid
= 0;
2617 prop_changelist_t
*clp
= NULL
;
2618 nvlist_t
*snapprops_nvlist
= NULL
;
2619 zprop_errflags_t prop_errflags
;
2620 boolean_t recursive
;
2622 begin_time
= time(NULL
);
2624 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2627 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2630 if (stream_avl
!= NULL
) {
2632 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
2637 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
2639 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
2641 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
2643 if (flags
->canmountoff
) {
2644 VERIFY(0 == nvlist_add_uint64(props
,
2645 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
2647 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
2651 if (0 == nvlist_lookup_nvlist(fs
, "snapprops", &props
)) {
2652 VERIFY(0 == nvlist_lookup_nvlist(props
,
2653 snapname
, &snapprops_nvlist
));
2663 * Determine how much of the snapshot name stored in the stream
2664 * we are going to tack on to the name they specified on the
2665 * command line, and how much we are going to chop off.
2667 * If they specified a snapshot, chop the entire name stored in
2670 if (flags
->istail
) {
2672 * A filesystem was specified with -e. We want to tack on only
2673 * the tail of the sent snapshot path.
2675 if (strchr(tosnap
, '@')) {
2676 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2677 "argument - snapshot not allowed with -e"));
2678 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2681 chopprefix
= strrchr(sendfs
, '/');
2683 if (chopprefix
== NULL
) {
2685 * The tail is the poolname, so we need to
2686 * prepend a path separator.
2688 int len
= strlen(drrb
->drr_toname
);
2689 cp
= malloc(len
+ 2);
2691 (void) strcpy(&cp
[1], drrb
->drr_toname
);
2694 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
2696 } else if (flags
->isprefix
) {
2698 * A filesystem was specified with -d. We want to tack on
2699 * everything but the first element of the sent snapshot path
2700 * (all but the pool name).
2702 if (strchr(tosnap
, '@')) {
2703 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2704 "argument - snapshot not allowed with -d"));
2705 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2708 chopprefix
= strchr(drrb
->drr_toname
, '/');
2709 if (chopprefix
== NULL
)
2710 chopprefix
= strchr(drrb
->drr_toname
, '@');
2711 } else if (strchr(tosnap
, '@') == NULL
) {
2713 * If a filesystem was specified without -d or -e, we want to
2714 * tack on everything after the fs specified by 'zfs send'.
2716 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
2718 /* A snapshot was specified as an exact path (no -d or -e). */
2720 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2721 "cannot specify snapshot name for multi-snapshot "
2723 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2725 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
2728 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
2729 ASSERT(chopprefix
> drrb
->drr_toname
);
2730 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
2731 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
2732 chopprefix
[0] == '\0');
2735 * Determine name of destination snapshot, store in zc_value.
2737 (void) strcpy(zc
.zc_value
, tosnap
);
2738 (void) strlcat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
2740 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
2741 zcmd_free_nvlists(&zc
);
2742 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2746 * Determine the name of the origin snapshot, store in zc_string.
2748 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
2749 if (guid_to_name(hdl
, zc
.zc_value
,
2750 drrb
->drr_fromguid
, zc
.zc_string
) != 0) {
2751 zcmd_free_nvlists(&zc
);
2752 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2753 "local origin for clone %s does not exist"),
2755 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2758 (void) printf("found clone origin %s\n", zc
.zc_string
);
2761 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
2762 (drrb
->drr_flags
& DRR_FLAG_CLONE
));
2764 if (stream_wantsnewfs
) {
2766 * if the parent fs does not exist, look for it based on
2767 * the parent snap GUID
2769 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2770 "cannot receive new filesystem stream"));
2772 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2773 cp
= strrchr(zc
.zc_name
, '/');
2777 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2778 char suffix
[ZFS_MAXNAMELEN
];
2779 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
2780 if (guid_to_name(hdl
, zc
.zc_name
, parent_snapguid
,
2781 zc
.zc_value
) == 0) {
2782 *strchr(zc
.zc_value
, '@') = '\0';
2783 (void) strcat(zc
.zc_value
, suffix
);
2788 * if the fs does not exist, look for it based on the
2791 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2792 "cannot receive incremental stream"));
2794 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2795 *strchr(zc
.zc_name
, '@') = '\0';
2798 * If the exact receive path was specified and this is the
2799 * topmost path in the stream, then if the fs does not exist we
2800 * should look no further.
2802 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
2803 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
2804 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2805 char snap
[ZFS_MAXNAMELEN
];
2806 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
2807 if (guid_to_name(hdl
, zc
.zc_name
, drrb
->drr_fromguid
,
2808 zc
.zc_value
) == 0) {
2809 *strchr(zc
.zc_value
, '@') = '\0';
2810 (void) strcat(zc
.zc_value
, snap
);
2815 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2816 *strchr(zc
.zc_name
, '@') = '\0';
2818 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2822 * Destination fs exists. Therefore this should either
2823 * be an incremental, or the stream specifies a new fs
2824 * (full stream or clone) and they want us to blow it
2825 * away (and have therefore specified -F and removed any
2828 if (stream_wantsnewfs
) {
2829 if (!flags
->force
) {
2830 zcmd_free_nvlists(&zc
);
2831 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2832 "destination '%s' exists\n"
2833 "must specify -F to overwrite it"),
2835 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2837 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2839 zcmd_free_nvlists(&zc
);
2840 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2841 "destination has snapshots (eg. %s)\n"
2842 "must destroy them to overwrite it"),
2844 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2848 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
2849 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
2850 zcmd_free_nvlists(&zc
);
2854 if (stream_wantsnewfs
&&
2855 zhp
->zfs_dmustats
.dds_origin
[0]) {
2856 zcmd_free_nvlists(&zc
);
2858 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2859 "destination '%s' is a clone\n"
2860 "must destroy it to overwrite it"),
2862 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2865 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
2866 stream_wantsnewfs
) {
2867 /* We can't do online recv in this case */
2868 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
2871 zcmd_free_nvlists(&zc
);
2874 if (changelist_prefix(clp
) != 0) {
2875 changelist_free(clp
);
2877 zcmd_free_nvlists(&zc
);
2884 * Destination filesystem does not exist. Therefore we better
2885 * be creating a new filesystem (either from a full backup, or
2886 * a clone). It would therefore be invalid if the user
2887 * specified only the pool name (i.e. if the destination name
2888 * contained no slash character).
2890 if (!stream_wantsnewfs
||
2891 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
2892 zcmd_free_nvlists(&zc
);
2893 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2894 "destination '%s' does not exist"), zc
.zc_name
);
2895 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2899 * Trim off the final dataset component so we perform the
2900 * recvbackup ioctl to the filesystems's parent.
2904 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
2905 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
2906 zcmd_free_nvlists(&zc
);
2907 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
2913 zc
.zc_begin_record
= drr_noswap
->drr_u
.drr_begin
;
2914 zc
.zc_cookie
= infd
;
2915 zc
.zc_guid
= flags
->force
;
2916 if (flags
->verbose
) {
2917 (void) printf("%s %s stream of %s into %s\n",
2918 flags
->dryrun
? "would receive" : "receiving",
2919 drrb
->drr_fromguid
? "incremental" : "full",
2920 drrb
->drr_toname
, zc
.zc_value
);
2921 (void) fflush(stdout
);
2924 if (flags
->dryrun
) {
2925 zcmd_free_nvlists(&zc
);
2926 return (recv_skip(hdl
, infd
, flags
->byteswap
));
2929 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
2930 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
2931 zc
.zc_cleanup_fd
= cleanup_fd
;
2932 zc
.zc_action_handle
= *action_handlep
;
2934 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
2935 ioctl_errno
= errno
;
2936 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
2939 nvlist_t
*prop_errors
;
2940 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
2941 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
2943 nvpair_t
*prop_err
= NULL
;
2945 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
2946 prop_err
)) != NULL
) {
2951 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
2952 (void) nvpair_value_int32(prop_err
, &intval
);
2953 if (strcmp(nvpair_name(prop_err
),
2954 ZPROP_N_MORE_ERRORS
) == 0) {
2955 trunc_prop_errs(intval
);
2958 (void) snprintf(tbuf
, sizeof (tbuf
),
2959 dgettext(TEXT_DOMAIN
,
2960 "cannot receive %s property on %s"),
2961 nvpair_name(prop_err
), zc
.zc_name
);
2962 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
2965 nvlist_free(prop_errors
);
2968 zc
.zc_nvlist_dst
= 0;
2969 zc
.zc_nvlist_dst_size
= 0;
2970 zcmd_free_nvlists(&zc
);
2972 if (err
== 0 && snapprops_nvlist
) {
2973 zfs_cmd_t zc2
= {"\0"};
2975 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
2976 zc2
.zc_cookie
= B_TRUE
; /* received */
2977 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
2978 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
2979 zcmd_free_nvlists(&zc2
);
2983 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
2985 * It may be that this snapshot already exists,
2986 * in which case we want to consume & ignore it
2987 * rather than failing.
2989 avl_tree_t
*local_avl
;
2990 nvlist_t
*local_nv
, *fs
;
2991 cp
= strchr(zc
.zc_value
, '@');
2994 * XXX Do this faster by just iterating over snaps in
2995 * this fs. Also if zc_value does not exist, we will
2996 * get a strange "does not exist" error message.
2999 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
3000 &local_nv
, &local_avl
) == 0) {
3002 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3003 fsavl_destroy(local_avl
);
3004 nvlist_free(local_nv
);
3007 if (flags
->verbose
) {
3008 (void) printf("snap %s already exists; "
3009 "ignoring\n", zc
.zc_value
);
3011 err
= ioctl_err
= recv_skip(hdl
, infd
,
3018 if (ioctl_err
!= 0) {
3019 switch (ioctl_errno
) {
3021 cp
= strchr(zc
.zc_value
, '@');
3023 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3024 "most recent snapshot of %s does not\n"
3025 "match incremental source"), zc
.zc_value
);
3026 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3030 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3031 "destination %s has been modified\n"
3032 "since most recent snapshot"), zc
.zc_name
);
3033 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3036 cp
= strchr(zc
.zc_value
, '@');
3038 /* it's the containing fs that exists */
3041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3042 "destination already exists"));
3043 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3044 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3049 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3052 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3053 "invalid stream (checksum mismatch)"));
3054 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3057 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3058 "pool must be upgraded to receive this stream."));
3059 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3062 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3063 "destination %s space quota exceeded"), zc
.zc_name
);
3064 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3067 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3072 * Mount the target filesystem (if created). Also mount any
3073 * children of the target filesystem if we did a replication
3074 * receive (indicated by stream_avl being non-NULL).
3076 cp
= strchr(zc
.zc_value
, '@');
3077 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3081 h
= zfs_open(hdl
, zc
.zc_value
,
3082 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3084 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3086 } else if (newfs
|| stream_avl
) {
3088 * Track the first/top of hierarchy fs,
3089 * for mounting and sharing later.
3091 if (top_zfs
&& *top_zfs
== NULL
)
3092 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
3100 err
|= changelist_postfix(clp
);
3101 changelist_free(clp
);
3104 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3105 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3106 "failed to clear unreceived properties on %s"),
3108 (void) fprintf(stderr
, "\n");
3110 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3111 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3112 "failed to restore original properties on %s"),
3114 (void) fprintf(stderr
, "\n");
3117 if (err
|| ioctl_err
)
3120 *action_handlep
= zc
.zc_action_handle
;
3122 if (flags
->verbose
) {
3125 uint64_t bytes
= zc
.zc_cookie
;
3126 time_t delta
= time(NULL
) - begin_time
;
3129 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3130 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3132 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3140 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t
*flags
,
3141 int infd
, const char *sendfs
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
3142 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
3145 dmu_replay_record_t drr
, drr_noswap
;
3146 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3148 zio_cksum_t zcksum
= { { 0 } };
3149 uint64_t featureflags
;
3152 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3155 if (flags
->isprefix
&&
3156 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3157 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3158 "(%s) does not exist"), tosnap
);
3159 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3162 /* read in the BEGIN record */
3163 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3167 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3168 /* It's the double end record at the end of a package */
3172 /* the kernel needs the non-byteswapped begin record */
3175 flags
->byteswap
= B_FALSE
;
3176 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3178 * We computed the checksum in the wrong byteorder in
3179 * recv_read() above; do it again correctly.
3181 bzero(&zcksum
, sizeof (zio_cksum_t
));
3182 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3183 flags
->byteswap
= B_TRUE
;
3185 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3186 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3187 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3188 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3189 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3190 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3191 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3192 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3193 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3196 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3197 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3198 "stream (bad magic number)"));
3199 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3202 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3203 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3205 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3206 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3207 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3208 "stream has unsupported feature, feature flags = %lx"),
3210 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3213 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3214 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3215 "stream (bad snapshot name)"));
3216 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3219 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3220 char nonpackage_sendfs
[ZFS_MAXNAMELEN
];
3221 if (sendfs
== NULL
) {
3223 * We were not called from zfs_receive_package(). Get
3224 * the fs specified by 'zfs send'.
3227 (void) strlcpy(nonpackage_sendfs
,
3228 drr
.drr_u
.drr_begin
.drr_toname
, ZFS_MAXNAMELEN
);
3229 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3231 sendfs
= nonpackage_sendfs
;
3233 return (zfs_receive_one(hdl
, infd
, tosnap
, flags
,
3234 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
,
3235 top_zfs
, cleanup_fd
, action_handlep
));
3237 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3238 DMU_COMPOUNDSTREAM
);
3239 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
,
3240 &drr
, &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3245 * Restores a backup of tosnap from the file descriptor specified by infd.
3246 * Return 0 on total success, -2 if some things couldn't be
3247 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3248 * (-1 will override -2).
3251 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t
*flags
,
3252 int infd
, avl_tree_t
*stream_avl
)
3254 char *top_zfs
= NULL
;
3257 uint64_t action_handle
= 0;
3259 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3260 VERIFY(cleanup_fd
>= 0);
3262 err
= zfs_receive_impl(hdl
, tosnap
, flags
, infd
, NULL
, NULL
,
3263 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
);
3265 VERIFY(0 == close(cleanup_fd
));
3267 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3269 prop_changelist_t
*clp
;
3271 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3273 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3274 CL_GATHER_MOUNT_ALWAYS
, 0);
3277 /* mount and share received datasets */
3278 err
= changelist_postfix(clp
);
3279 changelist_free(clp
);
3282 if (zhp
== NULL
|| clp
== NULL
|| err
)