4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2013 Steven Hartland. All rights reserved.
27 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
28 * Copyright (c) 2014 Integros [integros.com]
41 #include <sys/mount.h>
47 #include <libzfs_core.h>
49 #include "zfs_namecheck.h"
51 #include "zfs_fletcher.h"
52 #include "libzfs_impl.h"
55 #include <sys/zio_checksum.h>
58 /* in libzfs_dataset.c */
59 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
61 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
62 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **, int,
63 uint64_t *, const char *);
64 static int guid_to_name(libzfs_handle_t
*, const char *,
65 uint64_t, boolean_t
, char *);
67 static const zio_cksum_t zero_cksum
= { 0 };
69 typedef struct dedup_arg
{
72 libzfs_handle_t
*dedup_hdl
;
75 typedef struct progress_arg
{
78 boolean_t pa_parsable
;
81 typedef struct dataref
{
87 typedef struct dedup_entry
{
88 struct dedup_entry
*dde_next
;
89 zio_cksum_t dde_chksum
;
94 #define MAX_DDT_PHYSMEM_PERCENT 20
95 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
97 typedef struct dedup_table
{
98 dedup_entry_t
**dedup_hash_array
;
99 umem_cache_t
*ddecache
;
100 uint64_t max_ddt_size
; /* max dedup table size in bytes */
101 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
108 high_order_bit(uint64_t n
)
112 for (count
= 0; n
!= 0; count
++)
118 ssread(void *buf
, size_t len
, FILE *stream
)
122 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
129 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
130 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
134 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
135 if (ddt
->ddt_full
== B_FALSE
) {
136 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
137 "Dedup table full. Deduplication will continue "
138 "with existing table entries"));
139 ddt
->ddt_full
= B_TRUE
;
144 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
146 assert(*ddepp
== NULL
);
147 dde
->dde_next
= NULL
;
148 dde
->dde_chksum
= *cs
;
149 dde
->dde_prop
= prop
;
152 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
158 * Using the specified dedup table, do a lookup for an entry with
159 * the checksum cs. If found, return the block's reference info
160 * in *dr. Otherwise, insert a new entry in the dedup table, using
161 * the reference information specified by *dr.
163 * return value: true - entry was found
164 * false - entry was not found
167 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
168 uint64_t prop
, dataref_t
*dr
)
171 dedup_entry_t
**ddepp
;
173 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
175 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
176 ddepp
= &((*ddepp
)->dde_next
)) {
177 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
178 (*ddepp
)->dde_prop
== prop
) {
179 *dr
= (*ddepp
)->dde_ref
;
183 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
188 dump_record(dmu_replay_record_t
*drr
, void *payload
, int payload_len
,
189 zio_cksum_t
*zc
, int outfd
)
191 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
192 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
193 fletcher_4_incremental_native(drr
,
194 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
195 if (drr
->drr_type
!= DRR_BEGIN
) {
196 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
197 drr_checksum
.drr_checksum
));
198 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
200 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
201 sizeof (zio_cksum_t
), zc
);
202 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
204 if (payload_len
!= 0) {
205 fletcher_4_incremental_native(payload
, payload_len
, zc
);
206 if (write(outfd
, payload
, payload_len
) == -1)
213 * This function is started in a separate thread when the dedup option
214 * has been requested. The main send thread determines the list of
215 * snapshots to be included in the send stream and makes the ioctl calls
216 * for each one. But instead of having the ioctl send the output to the
217 * the output fd specified by the caller of zfs_send()), the
218 * ioctl is told to direct the output to a pipe, which is read by the
219 * alternate thread running THIS function. This function does the
221 * 1. building a dedup table (the DDT)
222 * 2. doing checksums on each data block and inserting a record in the DDT
223 * 3. looking for matching checksums, and
224 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
225 * a duplicate block is found.
226 * The output of this function then goes to the output fd requested
227 * by the caller of zfs_send().
232 dedup_arg_t
*dda
= arg
;
233 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
234 dmu_replay_record_t thedrr
;
235 dmu_replay_record_t
*drr
= &thedrr
;
239 zio_cksum_t stream_cksum
;
240 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
244 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
) / 100,
245 SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20);
247 numbuckets
= ddt
.max_ddt_size
/ (sizeof (dedup_entry_t
));
250 * numbuckets must be a power of 2. Increase number to
251 * a power of 2 if necessary.
253 if (!ISP2(numbuckets
))
254 numbuckets
= 1 << high_order_bit(numbuckets
);
256 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
257 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
258 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
259 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
260 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
261 ddt
.ddt_full
= B_FALSE
;
263 outfd
= dda
->outputfd
;
264 ofp
= fdopen(dda
->inputfd
, "r");
265 while (ssread(drr
, sizeof (*drr
), ofp
) != 0) {
267 switch (drr
->drr_type
) {
270 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
273 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
275 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
277 /* set the DEDUP feature flag for this stream */
278 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
279 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
280 DMU_BACKUP_FEATURE_DEDUPPROPS
);
281 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
283 if (drr
->drr_payloadlen
!= 0) {
284 sz
= drr
->drr_payloadlen
;
286 if (sz
> SPA_MAXBLOCKSIZE
) {
287 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
288 SPA_MAXBLOCKSIZE
, sz
);
290 (void) ssread(buf
, sz
, ofp
);
294 if (dump_record(drr
, buf
, sz
, &stream_cksum
,
302 struct drr_end
*drre
= &drr
->drr_u
.drr_end
;
303 /* use the recalculated checksum */
304 drre
->drr_checksum
= stream_cksum
;
305 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
313 struct drr_object
*drro
= &drr
->drr_u
.drr_object
;
314 if (drro
->drr_bonuslen
> 0) {
316 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
319 if (dump_record(drr
, buf
,
320 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
321 &stream_cksum
, outfd
) != 0)
328 struct drr_spill
*drrs
= &drr
->drr_u
.drr_spill
;
329 (void) ssread(buf
, drrs
->drr_length
, ofp
);
330 if (dump_record(drr
, buf
, drrs
->drr_length
,
331 &stream_cksum
, outfd
) != 0)
336 case DRR_FREEOBJECTS
:
338 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
346 struct drr_write
*drrw
= &drr
->drr_u
.drr_write
;
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
)) {
360 zio_cksum_t tmpsha256
;
363 SHA256Update(&ctx
, buf
, drrw
->drr_length
);
364 SHA256Final(&tmpsha256
, &ctx
);
365 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
366 BE_64(tmpsha256
.zc_word
[0]);
367 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
368 BE_64(tmpsha256
.zc_word
[1]);
369 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
370 BE_64(tmpsha256
.zc_word
[2]);
371 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
372 BE_64(tmpsha256
.zc_word
[3]);
373 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
374 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
377 dataref
.ref_guid
= drrw
->drr_toguid
;
378 dataref
.ref_object
= drrw
->drr_object
;
379 dataref
.ref_offset
= drrw
->drr_offset
;
381 if (ddt_update(dda
->dedup_hdl
, &ddt
,
382 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
384 dmu_replay_record_t wbr_drr
= {0};
385 struct drr_write_byref
*wbr_drrr
=
386 &wbr_drr
.drr_u
.drr_write_byref
;
388 /* block already present in stream */
389 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
391 wbr_drrr
->drr_object
= drrw
->drr_object
;
392 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
393 wbr_drrr
->drr_length
= drrw
->drr_length
;
394 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
395 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
396 wbr_drrr
->drr_refobject
=
398 wbr_drrr
->drr_refoffset
=
401 wbr_drrr
->drr_checksumtype
=
402 drrw
->drr_checksumtype
;
403 wbr_drrr
->drr_checksumflags
=
404 drrw
->drr_checksumtype
;
405 wbr_drrr
->drr_key
.ddk_cksum
=
406 drrw
->drr_key
.ddk_cksum
;
407 wbr_drrr
->drr_key
.ddk_prop
=
408 drrw
->drr_key
.ddk_prop
;
410 if (dump_record(&wbr_drr
, NULL
, 0,
411 &stream_cksum
, outfd
) != 0)
414 /* block not previously seen */
415 if (dump_record(drr
, buf
, drrw
->drr_length
,
416 &stream_cksum
, outfd
) != 0)
422 case DRR_WRITE_EMBEDDED
:
424 struct drr_write_embedded
*drrwe
=
425 &drr
->drr_u
.drr_write_embedded
;
427 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
428 if (dump_record(drr
, buf
,
429 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
430 &stream_cksum
, outfd
) != 0)
437 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
444 (void) fprintf(stderr
, "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
;
583 * The header nvlist is of the following format:
586 * "fromsnap" -> string (if incremental)
590 * "name" -> string (full name; for debugging)
591 * "parentfromsnap" -> number (guid of fromsnap in parent)
593 * "props" -> { name -> value (only if set here) }
594 * "snaps" -> { name (lastname) -> number (guid) }
595 * "snapprops" -> { name (lastname) -> { name -> value } }
597 * "origin" -> number (guid) (if clone)
598 * "sent" -> boolean (not on-disk)
606 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
609 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
611 send_data_t
*sd
= arg
;
612 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
616 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
618 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
620 * NB: if there is no fromsnap here (it's a newly created fs in
621 * an incremental replication), we will substitute the tosnap.
623 if ((sd
->fromsnap
&& strcmp(snapname
, sd
->fromsnap
) == 0) ||
624 (sd
->parent_fromsnap_guid
== 0 && sd
->tosnap
&&
625 strcmp(snapname
, sd
->tosnap
) == 0)) {
626 sd
->parent_fromsnap_guid
= guid
;
629 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
630 send_iterate_prop(zhp
, nv
);
631 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
639 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
641 nvpair_t
*elem
= NULL
;
643 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
644 char *propname
= nvpair_name(elem
);
645 zfs_prop_t prop
= zfs_name_to_prop(propname
);
648 if (!zfs_prop_user(propname
)) {
650 * Realistically, this should never happen. However,
651 * we want the ability to add DSL properties without
652 * needing to make incompatible version changes. We
653 * need to ignore unknown properties to allow older
654 * software to still send datasets containing these
655 * properties, with the unknown properties elided.
657 if (prop
== ZPROP_INVAL
)
660 if (zfs_prop_readonly(prop
))
664 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
665 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
666 prop
== ZFS_PROP_REFQUOTA
||
667 prop
== ZFS_PROP_REFRESERVATION
) {
670 verify(nvlist_lookup_uint64(propnv
,
671 ZPROP_VALUE
, &value
) == 0);
672 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
675 * May have no source before SPA_VERSION_RECVD_PROPS,
676 * but is still modifiable.
678 if (nvlist_lookup_string(propnv
,
679 ZPROP_SOURCE
, &source
) == 0) {
680 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
682 ZPROP_SOURCE_VAL_RECVD
) != 0))
687 if (nvlist_lookup_string(propnv
,
688 ZPROP_SOURCE
, &source
) != 0)
690 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
691 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
695 if (zfs_prop_user(propname
) ||
696 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
698 verify(nvlist_lookup_string(propnv
,
699 ZPROP_VALUE
, &value
) == 0);
700 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
703 verify(nvlist_lookup_uint64(propnv
,
704 ZPROP_VALUE
, &value
) == 0);
705 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
711 * recursively generate nvlists describing datasets. See comment
712 * for the data structure send_data_t above for description of contents
716 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
718 send_data_t
*sd
= arg
;
721 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
722 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
725 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
726 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
727 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
728 sd
->parent_fromsnap_guid
));
730 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
731 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
732 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
735 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
736 origin
->zfs_dmustats
.dds_guid
));
739 /* iterate over props */
740 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
741 send_iterate_prop(zhp
, nv
);
742 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
745 /* iterate over snaps, and set sd->parent_fromsnap_guid */
746 sd
->parent_fromsnap_guid
= 0;
747 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
748 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
749 (void) zfs_iter_snapshots(zhp
, send_iterate_snap
, sd
);
750 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
751 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
752 nvlist_free(sd
->parent_snaps
);
753 nvlist_free(sd
->snapprops
);
755 /* add this fs to nvlist */
756 (void) snprintf(guidstring
, sizeof (guidstring
),
757 "0x%llx", (longlong_t
)guid
);
758 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
761 /* iterate over children */
763 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
765 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
772 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
773 const char *tosnap
, boolean_t recursive
, nvlist_t
**nvlp
, avl_tree_t
**avlp
)
776 send_data_t sd
= { 0 };
779 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
781 return (EZFS_BADTYPE
);
783 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
784 sd
.fromsnap
= fromsnap
;
786 sd
.recursive
= recursive
;
788 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
796 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
807 * Routines specific to "zfs send"
809 typedef struct send_dump_data
{
810 /* these are all just the short snapname (the part after the @) */
811 const char *fromsnap
;
813 char prevsnap
[ZFS_MAXNAMELEN
];
814 uint64_t prevsnap_obj
;
815 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
816 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
, std_out
;
817 boolean_t large_block
;
823 snapfilter_cb_t
*filter_cb
;
826 char holdtag
[ZFS_MAXNAMELEN
];
832 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
833 boolean_t fromorigin
, uint64_t *sizep
)
835 zfs_cmd_t zc
= { 0 };
836 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
838 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
839 assert(fromsnap_obj
== 0 || !fromorigin
);
841 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
842 zc
.zc_obj
= fromorigin
;
843 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
844 zc
.zc_fromobj
= fromsnap_obj
;
845 zc
.zc_guid
= 1; /* estimate flag */
847 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
849 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
850 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
854 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
855 "not an earlier snapshot from the same fs"));
856 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
859 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
860 ZFS_TYPE_SNAPSHOT
)) {
861 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
862 "incremental source (@%s) does not exist"),
865 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
878 zfs_error_aux(hdl
, strerror(errno
));
879 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
882 return (zfs_standard_error(hdl
, errno
, errbuf
));
886 *sizep
= zc
.zc_objset_type
;
892 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
893 * NULL) to the file descriptor specified by outfd.
896 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
897 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
900 zfs_cmd_t zc
= { 0 };
901 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
904 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
905 assert(fromsnap_obj
== 0 || !fromorigin
);
907 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
908 zc
.zc_cookie
= outfd
;
909 zc
.zc_obj
= fromorigin
;
910 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
911 zc
.zc_fromobj
= fromsnap_obj
;
914 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
915 if (fromsnap
&& fromsnap
[0] != '\0') {
916 VERIFY(0 == nvlist_add_string(thisdbg
,
917 "fromsnap", fromsnap
));
920 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
922 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
923 "warning: cannot send '%s'"), zhp
->zfs_name
);
925 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
927 VERIFY(0 == nvlist_add_nvlist(debugnv
,
928 zhp
->zfs_name
, thisdbg
));
930 nvlist_free(thisdbg
);
934 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
935 "not an earlier snapshot from the same fs"));
936 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
939 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
940 ZFS_TYPE_SNAPSHOT
)) {
941 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
942 "incremental source (@%s) does not exist"),
945 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
958 zfs_error_aux(hdl
, strerror(errno
));
959 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
962 return (zfs_standard_error(hdl
, errno
, errbuf
));
967 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
968 nvlist_free(thisdbg
);
974 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
976 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
979 * zfs_send() only sets snapholds for sends that need them,
980 * e.g. replication and doall.
982 if (sdd
->snapholds
== NULL
)
985 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
989 send_progress_thread(void *arg
)
991 progress_arg_t
*pa
= arg
;
992 zfs_cmd_t zc
= { 0 };
993 zfs_handle_t
*zhp
= pa
->pa_zhp
;
994 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
995 unsigned long long bytes
;
1000 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1002 if (!pa
->pa_parsable
)
1003 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1006 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1011 zc
.zc_cookie
= pa
->pa_fd
;
1012 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1013 return ((void *)-1);
1017 bytes
= zc
.zc_cookie
;
1019 if (pa
->pa_parsable
) {
1020 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1021 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1022 bytes
, zhp
->zfs_name
);
1024 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1025 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1026 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1027 buf
, zhp
->zfs_name
);
1033 send_print_verbose(FILE *fout
, const char *tosnap
, const char *fromsnap
,
1034 uint64_t size
, boolean_t parsable
)
1037 if (fromsnap
!= NULL
) {
1038 (void) fprintf(fout
, "incremental\t%s\t%s",
1041 (void) fprintf(fout
, "full\t%s",
1045 if (fromsnap
!= NULL
) {
1046 if (strchr(fromsnap
, '@') == NULL
&&
1047 strchr(fromsnap
, '#') == NULL
) {
1048 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1049 "send from @%s to %s"),
1052 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1053 "send from %s to %s"),
1057 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1065 (void) fprintf(fout
, "\t%llu",
1069 zfs_nicenum(size
, buf
, sizeof (buf
));
1070 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1071 " estimated size is %s"), buf
);
1074 (void) fprintf(fout
, "\n");
1078 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1080 send_dump_data_t
*sdd
= arg
;
1081 progress_arg_t pa
= { 0 };
1085 boolean_t isfromsnap
, istosnap
, fromorigin
;
1086 boolean_t exclude
= B_FALSE
;
1087 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1090 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1091 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1092 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1094 if (!sdd
->seenfrom
&& isfromsnap
) {
1095 gather_holds(zhp
, sdd
);
1096 sdd
->seenfrom
= B_TRUE
;
1097 (void) strcpy(sdd
->prevsnap
, thissnap
);
1098 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1103 if (sdd
->seento
|| !sdd
->seenfrom
) {
1108 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1110 sdd
->seento
= B_TRUE
;
1112 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1113 if (sdd
->replicate
) {
1115 nvlist_t
*snapprops
;
1117 * Filter out all intermediate snapshots except origin
1118 * snapshots needed to replicate clones.
1120 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1121 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1123 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1124 "snapprops", &snapprops
));
1125 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1126 thissnap
, &snapprops
));
1127 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1134 * If a filter function exists, call it to determine whether
1135 * this snapshot will be sent.
1137 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1138 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1140 * This snapshot is filtered out. Don't send it, and don't
1141 * set prevsnap_obj, so it will be as if this snapshot didn't
1142 * exist, and the next accepted snapshot will be sent as
1143 * an incremental from the last accepted one, or as the
1144 * first (and full) snapshot in the case of a replication,
1145 * non-incremental send.
1151 gather_holds(zhp
, sdd
);
1152 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1153 (sdd
->fromorigin
|| sdd
->replicate
);
1157 (void) estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1160 send_print_verbose(fout
, zhp
->zfs_name
,
1161 sdd
->prevsnap
[0] ? sdd
->prevsnap
: NULL
,
1162 size
, sdd
->parsable
);
1168 * If progress reporting is requested, spawn a new thread to
1169 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1171 if (sdd
->progress
) {
1173 pa
.pa_fd
= sdd
->outfd
;
1174 pa
.pa_parsable
= sdd
->parsable
;
1176 if (err
= pthread_create(&tid
, NULL
,
1177 send_progress_thread
, &pa
)) {
1183 enum lzc_send_flags flags
= 0;
1184 if (sdd
->large_block
)
1185 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1186 if (sdd
->embed_data
)
1187 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1189 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1190 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1192 if (sdd
->progress
) {
1193 (void) pthread_cancel(tid
);
1194 (void) pthread_join(tid
, NULL
);
1198 (void) strcpy(sdd
->prevsnap
, thissnap
);
1199 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1205 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1208 send_dump_data_t
*sdd
= arg
;
1209 boolean_t missingfrom
= B_FALSE
;
1210 zfs_cmd_t zc
= { 0 };
1212 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1213 zhp
->zfs_name
, sdd
->tosnap
);
1214 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1215 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1216 "WARNING: could not send %s@%s: does not exist\n"),
1217 zhp
->zfs_name
, sdd
->tosnap
);
1222 if (sdd
->replicate
&& sdd
->fromsnap
) {
1224 * If this fs does not have fromsnap, and we're doing
1225 * recursive, we need to send a full stream from the
1226 * beginning (or an incremental from the origin if this
1227 * is a clone). If we're doing non-recursive, then let
1228 * them get the error.
1230 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1231 zhp
->zfs_name
, sdd
->fromsnap
);
1232 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1233 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1234 missingfrom
= B_TRUE
;
1238 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1239 sdd
->prevsnap_obj
= 0;
1240 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1241 sdd
->seenfrom
= B_TRUE
;
1243 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1244 if (!sdd
->seenfrom
) {
1245 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1246 "WARNING: could not send %s@%s:\n"
1247 "incremental source (%s@%s) does not exist\n"),
1248 zhp
->zfs_name
, sdd
->tosnap
,
1249 zhp
->zfs_name
, sdd
->fromsnap
);
1251 } else if (!sdd
->seento
) {
1252 if (sdd
->fromsnap
) {
1253 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1254 "WARNING: could not send %s@%s:\n"
1255 "incremental source (%s@%s) "
1256 "is not earlier than it\n"),
1257 zhp
->zfs_name
, sdd
->tosnap
,
1258 zhp
->zfs_name
, sdd
->fromsnap
);
1260 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1262 "could not send %s@%s: does not exist\n"),
1263 zhp
->zfs_name
, sdd
->tosnap
);
1272 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1274 send_dump_data_t
*sdd
= arg
;
1276 boolean_t needagain
, progress
;
1278 if (!sdd
->replicate
)
1279 return (dump_filesystem(rzhp
, sdd
));
1281 /* Mark the clone origin snapshots. */
1282 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1283 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1285 uint64_t origin_guid
= 0;
1287 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1288 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1289 if (origin_guid
!= 0) {
1291 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1292 origin_guid
, &snapname
);
1293 if (origin_nv
!= NULL
) {
1294 nvlist_t
*snapprops
;
1295 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1296 "snapprops", &snapprops
));
1297 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1298 snapname
, &snapprops
));
1299 VERIFY(0 == nvlist_add_boolean(
1300 snapprops
, "is_clone_origin"));
1305 needagain
= progress
= B_FALSE
;
1306 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1307 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1308 nvlist_t
*fslist
, *parent_nv
;
1312 uint64_t origin_guid
= 0;
1313 uint64_t parent_guid
= 0;
1315 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1316 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1319 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1320 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1321 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1324 if (parent_guid
!= 0) {
1325 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1326 if (!nvlist_exists(parent_nv
, "sent")) {
1327 /* parent has not been sent; skip this one */
1333 if (origin_guid
!= 0) {
1334 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1336 if (origin_nv
!= NULL
&&
1337 !nvlist_exists(origin_nv
, "sent")) {
1339 * origin has not been sent yet;
1347 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1350 err
= dump_filesystem(zhp
, sdd
);
1351 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1362 /* clean out the sent flags in case we reuse this fss */
1363 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1364 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1367 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1368 (void) nvlist_remove_all(fslist
, "sent");
1375 zfs_send_resume_token_to_nvlist(libzfs_handle_t
*hdl
, const char *token
)
1377 unsigned int version
;
1379 unsigned long long checksum
, packed_len
;
1382 * Decode token header, which is:
1383 * <token version>-<checksum of payload>-<uncompressed payload length>
1384 * Note that the only supported token version is 1.
1386 nread
= sscanf(token
, "%u-%llx-%llx-",
1387 &version
, &checksum
, &packed_len
);
1389 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1390 "resume token is corrupt (invalid format)"));
1394 if (version
!= ZFS_SEND_RESUME_TOKEN_VERSION
) {
1395 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1396 "resume token is corrupt (invalid version %u)"),
1401 /* convert hexadecimal representation to binary */
1402 token
= strrchr(token
, '-') + 1;
1403 int len
= strlen(token
) / 2;
1404 unsigned char *compressed
= zfs_alloc(hdl
, len
);
1405 for (int i
= 0; i
< len
; i
++) {
1406 nread
= sscanf(token
+ i
* 2, "%2hhx", compressed
+ i
);
1409 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1410 "resume token is corrupt "
1411 "(payload is not hex-encoded)"));
1416 /* verify checksum */
1418 fletcher_4_native(compressed
, len
, NULL
, &cksum
);
1419 if (cksum
.zc_word
[0] != checksum
) {
1421 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1422 "resume token is corrupt (incorrect checksum)"));
1427 void *packed
= zfs_alloc(hdl
, packed_len
);
1428 uLongf packed_len_long
= packed_len
;
1429 if (uncompress(packed
, &packed_len_long
, compressed
, len
) != Z_OK
||
1430 packed_len_long
!= packed_len
) {
1433 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1434 "resume token is corrupt (decompression failed)"));
1440 int error
= nvlist_unpack(packed
, packed_len
, &nv
, KM_SLEEP
);
1444 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1445 "resume token is corrupt (nvlist_unpack failed)"));
1452 zfs_send_resume(libzfs_handle_t
*hdl
, sendflags_t
*flags
, int outfd
,
1453 const char *resume_token
)
1457 char *fromname
= NULL
;
1458 uint64_t resumeobj
, resumeoff
, toguid
, fromguid
, bytes
;
1461 char name
[ZFS_MAXNAMELEN
];
1462 enum lzc_send_flags lzc_flags
= 0;
1464 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1465 "cannot resume send"));
1467 nvlist_t
*resume_nvl
=
1468 zfs_send_resume_token_to_nvlist(hdl
, resume_token
);
1469 if (resume_nvl
== NULL
) {
1471 * zfs_error_aux has already been set by
1472 * zfs_send_resume_token_to_nvlist
1474 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1476 if (flags
->verbose
) {
1477 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1478 "resume token contents:\n"));
1479 nvlist_print(stderr
, resume_nvl
);
1482 if (nvlist_lookup_string(resume_nvl
, "toname", &toname
) != 0 ||
1483 nvlist_lookup_uint64(resume_nvl
, "object", &resumeobj
) != 0 ||
1484 nvlist_lookup_uint64(resume_nvl
, "offset", &resumeoff
) != 0 ||
1485 nvlist_lookup_uint64(resume_nvl
, "bytes", &bytes
) != 0 ||
1486 nvlist_lookup_uint64(resume_nvl
, "toguid", &toguid
) != 0) {
1487 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1488 "resume token is corrupt"));
1489 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1492 (void) nvlist_lookup_uint64(resume_nvl
, "fromguid", &fromguid
);
1494 if (flags
->embed_data
|| nvlist_exists(resume_nvl
, "embedok"))
1495 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1497 if (guid_to_name(hdl
, toname
, toguid
, B_FALSE
, name
) != 0) {
1498 if (zfs_dataset_exists(hdl
, toname
, ZFS_TYPE_DATASET
)) {
1499 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1500 "'%s' is no longer the same snapshot used in "
1501 "the initial send"), toname
);
1503 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1504 "'%s' used in the initial send no longer exists"),
1507 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1509 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1511 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1512 "unable to access '%s'"), name
);
1513 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1516 if (fromguid
!= 0) {
1517 if (guid_to_name(hdl
, toname
, fromguid
, B_TRUE
, name
) != 0) {
1518 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1519 "incremental source %#llx no longer exists"),
1520 (longlong_t
)fromguid
);
1521 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1526 if (flags
->verbose
) {
1528 error
= lzc_send_space(zhp
->zfs_name
, fromname
, &size
);
1530 size
= MAX(0, (int64_t)(size
- bytes
));
1531 send_print_verbose(stderr
, zhp
->zfs_name
, fromname
,
1532 size
, flags
->parsable
);
1535 if (!flags
->dryrun
) {
1536 progress_arg_t pa
= { 0 };
1539 * If progress reporting is requested, spawn a new thread to
1540 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1542 if (flags
->progress
) {
1545 pa
.pa_parsable
= flags
->parsable
;
1547 error
= pthread_create(&tid
, NULL
,
1548 send_progress_thread
, &pa
);
1555 error
= lzc_send_resume(zhp
->zfs_name
, fromname
, outfd
,
1556 lzc_flags
, resumeobj
, resumeoff
);
1558 if (flags
->progress
) {
1559 (void) pthread_cancel(tid
);
1560 (void) pthread_join(tid
, NULL
);
1564 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1565 "warning: cannot send '%s'"), zhp
->zfs_name
);
1585 zfs_error_aux(hdl
, strerror(errno
));
1586 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1589 return (zfs_standard_error(hdl
, errno
, errbuf
));
1600 * Generate a send stream for the dataset identified by the argument zhp.
1602 * The content of the send stream is the snapshot identified by
1603 * 'tosnap'. Incremental streams are requested in two ways:
1604 * - from the snapshot identified by "fromsnap" (if non-null) or
1605 * - from the origin of the dataset identified by zhp, which must
1606 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1609 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1610 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1611 * if "replicate" is set. If "doall" is set, dump all the intermediate
1612 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1613 * case too. If "props" is set, send properties.
1616 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1617 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1618 void *cb_arg
, nvlist_t
**debugnvp
)
1621 send_dump_data_t sdd
= { 0 };
1623 nvlist_t
*fss
= NULL
;
1624 avl_tree_t
*fsavl
= NULL
;
1625 static uint64_t holdseq
;
1629 dedup_arg_t dda
= { 0 };
1630 int featureflags
= 0;
1633 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1634 "cannot send '%s'"), zhp
->zfs_name
);
1636 if (fromsnap
&& fromsnap
[0] == '\0') {
1637 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1638 "zero-length incremental source"));
1639 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1642 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1644 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1645 if (version
>= ZPL_VERSION_SA
) {
1646 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1650 if (flags
->dedup
&& !flags
->dryrun
) {
1651 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1652 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1653 if (err
= pipe(pipefd
)) {
1654 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1655 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1658 dda
.outputfd
= outfd
;
1659 dda
.inputfd
= pipefd
[1];
1660 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1661 if (err
= pthread_create(&tid
, NULL
, cksummer
, &dda
)) {
1662 (void) close(pipefd
[0]);
1663 (void) close(pipefd
[1]);
1664 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1665 return (zfs_error(zhp
->zfs_hdl
,
1666 EZFS_THREADCREATEFAILED
, errbuf
));
1670 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1671 dmu_replay_record_t drr
= { 0 };
1672 char *packbuf
= NULL
;
1674 zio_cksum_t zc
= { 0 };
1676 if (flags
->replicate
|| flags
->props
) {
1679 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1681 VERIFY(0 == nvlist_add_string(hdrnv
,
1682 "fromsnap", fromsnap
));
1684 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1685 if (!flags
->replicate
) {
1686 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1690 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1691 fromsnap
, tosnap
, flags
->replicate
, &fss
, &fsavl
);
1694 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1695 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1705 if (!flags
->dryrun
) {
1706 /* write first begin record */
1707 drr
.drr_type
= DRR_BEGIN
;
1708 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1709 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1710 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1711 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1712 drr_versioninfo
, featureflags
);
1713 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1714 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1715 "%s@%s", zhp
->zfs_name
, tosnap
);
1716 drr
.drr_payloadlen
= buflen
;
1718 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, outfd
);
1723 /* write end record */
1724 bzero(&drr
, sizeof (drr
));
1725 drr
.drr_type
= DRR_END
;
1726 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1727 err
= write(outfd
, &drr
, sizeof (drr
));
1737 /* dump each stream */
1738 sdd
.fromsnap
= fromsnap
;
1739 sdd
.tosnap
= tosnap
;
1741 sdd
.outfd
= pipefd
[0];
1744 sdd
.replicate
= flags
->replicate
;
1745 sdd
.doall
= flags
->doall
;
1746 sdd
.fromorigin
= flags
->fromorigin
;
1749 sdd
.verbose
= flags
->verbose
;
1750 sdd
.parsable
= flags
->parsable
;
1751 sdd
.progress
= flags
->progress
;
1752 sdd
.dryrun
= flags
->dryrun
;
1753 sdd
.large_block
= flags
->largeblock
;
1754 sdd
.embed_data
= flags
->embed_data
;
1755 sdd
.filter_cb
= filter_func
;
1756 sdd
.filter_cb_arg
= cb_arg
;
1758 sdd
.debugnv
= *debugnvp
;
1759 if (sdd
.verbose
&& sdd
.dryrun
)
1760 sdd
.std_out
= B_TRUE
;
1761 fout
= sdd
.std_out
? stdout
: stderr
;
1764 * Some flags require that we place user holds on the datasets that are
1765 * being sent so they don't get destroyed during the send. We can skip
1766 * this step if the pool is imported read-only since the datasets cannot
1769 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1770 ZPOOL_PROP_READONLY
, NULL
) &&
1771 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1772 spa_version
>= SPA_VERSION_USERREFS
&&
1773 (flags
->doall
|| flags
->replicate
)) {
1775 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1776 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1777 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
|O_EXCL
);
1778 if (sdd
.cleanup_fd
< 0) {
1782 sdd
.snapholds
= fnvlist_alloc();
1784 sdd
.cleanup_fd
= -1;
1785 sdd
.snapholds
= NULL
;
1787 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1789 * Do a verbose no-op dry run to get all the verbose output
1790 * or to gather snapshot hold's before generating any data,
1791 * then do a non-verbose real run to generate the streams.
1793 sdd
.dryrun
= B_TRUE
;
1794 err
= dump_filesystems(zhp
, &sdd
);
1799 if (flags
->verbose
) {
1800 if (flags
->parsable
) {
1801 (void) fprintf(fout
, "size\t%llu\n",
1802 (longlong_t
)sdd
.size
);
1805 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1806 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1807 "total estimated size is %s\n"), buf
);
1811 /* Ensure no snaps found is treated as an error. */
1817 /* Skip the second run if dryrun was requested. */
1821 if (sdd
.snapholds
!= NULL
) {
1822 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1826 fnvlist_free(sdd
.snapholds
);
1827 sdd
.snapholds
= NULL
;
1830 sdd
.dryrun
= B_FALSE
;
1831 sdd
.verbose
= B_FALSE
;
1834 err
= dump_filesystems(zhp
, &sdd
);
1835 fsavl_destroy(fsavl
);
1838 /* Ensure no snaps found is treated as an error. */
1839 if (err
== 0 && !sdd
.seento
)
1844 (void) pthread_cancel(tid
);
1845 (void) close(pipefd
[0]);
1846 (void) pthread_join(tid
, NULL
);
1849 if (sdd
.cleanup_fd
!= -1) {
1850 VERIFY(0 == close(sdd
.cleanup_fd
));
1851 sdd
.cleanup_fd
= -1;
1854 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1857 * write final end record. NB: want to do this even if
1858 * there was some error, because it might not be totally
1861 dmu_replay_record_t drr
= { 0 };
1862 drr
.drr_type
= DRR_END
;
1863 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1864 return (zfs_standard_error(zhp
->zfs_hdl
,
1869 return (err
|| sdd
.err
);
1872 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1874 fsavl_destroy(fsavl
);
1876 fnvlist_free(sdd
.snapholds
);
1878 if (sdd
.cleanup_fd
!= -1)
1879 VERIFY(0 == close(sdd
.cleanup_fd
));
1881 (void) pthread_cancel(tid
);
1882 (void) close(pipefd
[0]);
1883 (void) pthread_join(tid
, NULL
);
1889 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
1890 enum lzc_send_flags flags
)
1893 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1896 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1897 "warning: cannot send '%s'"), zhp
->zfs_name
);
1899 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
1903 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1904 "not an earlier snapshot from the same fs"));
1905 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1909 if (lzc_exists(zhp
->zfs_name
)) {
1910 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1911 "incremental source (%s) does not exist"),
1914 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1917 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1918 "target is busy; if a filesystem, "
1919 "it must not be mounted"));
1920 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
1933 zfs_error_aux(hdl
, strerror(errno
));
1934 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1937 return (zfs_standard_error(hdl
, errno
, errbuf
));
1944 * Routines specific to "zfs recv"
1948 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1949 boolean_t byteswap
, zio_cksum_t
*zc
)
1955 assert(ilen
<= SPA_MAXBLOCKSIZE
);
1958 rv
= read(fd
, cp
, len
);
1963 if (rv
< 0 || len
!= 0) {
1964 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1965 "failed to read from stream"));
1966 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
1967 "cannot receive")));
1972 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
1974 fletcher_4_incremental_native(buf
, ilen
, zc
);
1980 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
1981 boolean_t byteswap
, zio_cksum_t
*zc
)
1986 buf
= zfs_alloc(hdl
, len
);
1990 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
1996 err
= nvlist_unpack(buf
, len
, nvp
, 0);
1999 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2000 "stream (malformed nvlist)"));
2007 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
2008 int baselen
, char *newname
, recvflags_t
*flags
)
2011 zfs_cmd_t zc
= { 0 };
2013 prop_changelist_t
*clp
;
2016 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2019 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2020 flags
->force
? MS_FORCE
: 0);
2024 err
= changelist_prefix(clp
);
2028 zc
.zc_objset_type
= DMU_OST_ZFS
;
2029 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2032 (void) strcpy(newname
, tryname
);
2034 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
2036 if (flags
->verbose
) {
2037 (void) printf("attempting rename %s to %s\n",
2038 zc
.zc_name
, zc
.zc_value
);
2040 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2042 changelist_rename(clp
, name
, tryname
);
2047 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
2050 (void) snprintf(newname
, ZFS_MAXNAMELEN
, "%.*srecv-%u-%u",
2051 baselen
, name
, getpid(), seq
);
2052 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
2054 if (flags
->verbose
) {
2055 (void) printf("failed - trying rename %s to %s\n",
2056 zc
.zc_name
, zc
.zc_value
);
2058 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2060 changelist_rename(clp
, name
, newname
);
2061 if (err
&& flags
->verbose
) {
2062 (void) printf("failed (%u) - "
2063 "will try again on next pass\n", errno
);
2066 } else if (flags
->verbose
) {
2068 (void) printf("success\n");
2070 (void) printf("failed (%u)\n", errno
);
2073 (void) changelist_postfix(clp
);
2074 changelist_free(clp
);
2080 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
2081 char *newname
, recvflags_t
*flags
)
2083 zfs_cmd_t zc
= { 0 };
2085 prop_changelist_t
*clp
;
2087 boolean_t defer
= B_FALSE
;
2090 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2093 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2094 flags
->force
? MS_FORCE
: 0);
2095 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2096 zfs_spa_version(zhp
, &spa_version
) == 0 &&
2097 spa_version
>= SPA_VERSION_USERREFS
)
2102 err
= changelist_prefix(clp
);
2106 zc
.zc_objset_type
= DMU_OST_ZFS
;
2107 zc
.zc_defer_destroy
= defer
;
2108 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2111 (void) printf("attempting destroy %s\n", zc
.zc_name
);
2112 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
2115 (void) printf("success\n");
2116 changelist_remove(clp
, zc
.zc_name
);
2119 (void) changelist_postfix(clp
);
2120 changelist_free(clp
);
2123 * Deferred destroy might destroy the snapshot or only mark it to be
2124 * destroyed later, and it returns success in either case.
2126 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
2127 ZFS_TYPE_SNAPSHOT
))) {
2128 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
2134 typedef struct guid_to_name_data
{
2136 boolean_t bookmark_ok
;
2139 } guid_to_name_data_t
;
2142 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
2144 guid_to_name_data_t
*gtnd
= arg
;
2148 if (gtnd
->skip
!= NULL
&&
2149 (slash
= strrchr(zhp
->zfs_name
, '/')) != NULL
&&
2150 strcmp(slash
+ 1, gtnd
->skip
) == 0) {
2155 if (zfs_prop_get_int(zhp
, ZFS_PROP_GUID
) == gtnd
->guid
) {
2156 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
2161 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
2162 if (err
!= EEXIST
&& gtnd
->bookmark_ok
)
2163 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, gtnd
);
2169 * Attempt to find the local dataset associated with this guid. In the case of
2170 * multiple matches, we attempt to find the "best" match by searching
2171 * progressively larger portions of the hierarchy. This allows one to send a
2172 * tree of datasets individually and guarantee that we will find the source
2173 * guid within that hierarchy, even if there are multiple matches elsewhere.
2176 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
2177 boolean_t bookmark_ok
, char *name
)
2179 char pname
[ZFS_MAXNAMELEN
];
2180 guid_to_name_data_t gtnd
;
2183 gtnd
.bookmark_ok
= bookmark_ok
;
2188 * Search progressively larger portions of the hierarchy, starting
2189 * with the filesystem specified by 'parent'. This will
2190 * select the "most local" version of the origin snapshot in the case
2191 * that there are multiple matching snapshots in the system.
2193 (void) strlcpy(pname
, parent
, sizeof (pname
));
2194 char *cp
= strrchr(pname
, '@');
2196 cp
= strchr(pname
, '\0');
2197 for (; cp
!= NULL
; cp
= strrchr(pname
, '/')) {
2198 /* Chop off the last component and open the parent */
2200 zfs_handle_t
*zhp
= make_dataset_handle(hdl
, pname
);
2204 int err
= guid_to_name_cb(zfs_handle_dup(zhp
), >nd
);
2206 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
2207 if (err
!= EEXIST
&& bookmark_ok
)
2208 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, >nd
);
2214 * Remember the last portion of the dataset so we skip it next
2215 * time through (as we've already searched that portion of the
2218 gtnd
.skip
= strrchr(pname
, '/') + 1;
2225 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2226 * guid1 is after guid2.
2229 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
2230 uint64_t guid1
, uint64_t guid2
)
2233 char *fsname
, *snapname
;
2234 char buf
[ZFS_MAXNAMELEN
];
2236 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2237 uint64_t create1
, create2
;
2244 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2245 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2246 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2247 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2248 if (guid1hdl
== NULL
)
2251 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2252 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2253 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2254 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2255 if (guid2hdl
== NULL
) {
2256 zfs_close(guid1hdl
);
2260 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2261 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2263 if (create1
< create2
)
2265 else if (create1
> create2
)
2270 zfs_close(guid1hdl
);
2271 zfs_close(guid2hdl
);
2277 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2278 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2282 avl_tree_t
*local_avl
;
2283 nvpair_t
*fselem
, *nextfselem
;
2285 char newname
[ZFS_MAXNAMELEN
];
2287 boolean_t needagain
, progress
, recursive
;
2290 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2292 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2299 needagain
= progress
= B_FALSE
;
2301 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2302 recursive
, &local_nv
, &local_avl
)) != 0)
2306 * Process deletes and renames
2308 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2309 fselem
; fselem
= nextfselem
) {
2310 nvlist_t
*nvfs
, *snaps
;
2311 nvlist_t
*stream_nvfs
= NULL
;
2312 nvpair_t
*snapelem
, *nextsnapelem
;
2313 uint64_t fromguid
= 0;
2314 uint64_t originguid
= 0;
2315 uint64_t stream_originguid
= 0;
2316 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2317 char *fsname
, *stream_fsname
;
2319 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2321 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2322 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2323 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2324 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2325 &parent_fromsnap_guid
));
2326 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2329 * First find the stream's fs, so we can check for
2330 * a different origin (due to "zfs promote")
2332 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2333 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2336 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2337 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2339 if (stream_nvfs
!= NULL
)
2343 /* check for promote */
2344 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2345 &stream_originguid
);
2346 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2347 switch (created_before(hdl
, local_avl
,
2348 stream_originguid
, originguid
)) {
2351 zfs_cmd_t zc
= { 0 };
2352 nvlist_t
*origin_nvfs
;
2353 char *origin_fsname
;
2356 (void) printf("promoting %s\n", fsname
);
2358 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2360 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2361 "name", &origin_fsname
));
2362 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2363 sizeof (zc
.zc_value
));
2364 (void) strlcpy(zc
.zc_name
, fsname
,
2365 sizeof (zc
.zc_name
));
2366 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2374 fsavl_destroy(local_avl
);
2375 nvlist_free(local_nv
);
2379 * We had/have the wrong origin, therefore our
2380 * list of snapshots is wrong. Need to handle
2381 * them on the next pass.
2387 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2388 snapelem
; snapelem
= nextsnapelem
) {
2390 char *stream_snapname
;
2391 nvlist_t
*found
, *props
;
2393 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2395 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2396 found
= fsavl_find(stream_avl
, thisguid
,
2399 /* check for delete */
2400 if (found
== NULL
) {
2401 char name
[ZFS_MAXNAMELEN
];
2406 (void) snprintf(name
, sizeof (name
), "%s@%s",
2407 fsname
, nvpair_name(snapelem
));
2409 error
= recv_destroy(hdl
, name
,
2410 strlen(fsname
)+1, newname
, flags
);
2418 stream_nvfs
= found
;
2420 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2421 &props
) && 0 == nvlist_lookup_nvlist(props
,
2422 stream_snapname
, &props
)) {
2423 zfs_cmd_t zc
= { 0 };
2425 zc
.zc_cookie
= B_TRUE
; /* received */
2426 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2427 "%s@%s", fsname
, nvpair_name(snapelem
));
2428 if (zcmd_write_src_nvlist(hdl
, &zc
,
2430 (void) zfs_ioctl(hdl
,
2431 ZFS_IOC_SET_PROP
, &zc
);
2432 zcmd_free_nvlists(&zc
);
2436 /* check for different snapname */
2437 if (strcmp(nvpair_name(snapelem
),
2438 stream_snapname
) != 0) {
2439 char name
[ZFS_MAXNAMELEN
];
2440 char tryname
[ZFS_MAXNAMELEN
];
2442 (void) snprintf(name
, sizeof (name
), "%s@%s",
2443 fsname
, nvpair_name(snapelem
));
2444 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2445 fsname
, stream_snapname
);
2447 error
= recv_rename(hdl
, name
, tryname
,
2448 strlen(fsname
)+1, newname
, flags
);
2455 if (strcmp(stream_snapname
, fromsnap
) == 0)
2456 fromguid
= thisguid
;
2459 /* check for delete */
2460 if (stream_nvfs
== NULL
) {
2464 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2473 if (fromguid
== 0) {
2474 if (flags
->verbose
) {
2475 (void) printf("local fs %s does not have "
2476 "fromsnap (%s in stream); must have "
2477 "been deleted locally; ignoring\n",
2483 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2484 "name", &stream_fsname
));
2485 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2486 "parentfromsnap", &stream_parent_fromsnap_guid
));
2488 s1
= strrchr(fsname
, '/');
2489 s2
= strrchr(stream_fsname
, '/');
2492 * Check for rename. If the exact receive path is specified, it
2493 * does not count as a rename, but we still need to check the
2494 * datasets beneath it.
2496 if ((stream_parent_fromsnap_guid
!= 0 &&
2497 parent_fromsnap_guid
!= 0 &&
2498 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2499 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2500 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2502 char tryname
[ZFS_MAXNAMELEN
];
2504 parent
= fsavl_find(local_avl
,
2505 stream_parent_fromsnap_guid
, NULL
);
2507 * NB: parent might not be found if we used the
2508 * tosnap for stream_parent_fromsnap_guid,
2509 * because the parent is a newly-created fs;
2510 * we'll be able to rename it after we recv the
2513 if (parent
!= NULL
) {
2516 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2518 (void) snprintf(tryname
, sizeof (tryname
),
2519 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2522 if (flags
->verbose
) {
2523 (void) printf("local fs %s new parent "
2524 "not found\n", fsname
);
2530 error
= recv_rename(hdl
, fsname
, tryname
,
2531 strlen(tofs
)+1, newname
, flags
);
2533 if (renamed
!= NULL
&& newname
[0] != '\0') {
2534 VERIFY(0 == nvlist_add_boolean(renamed
,
2545 fsavl_destroy(local_avl
);
2546 nvlist_free(local_nv
);
2548 if (needagain
&& progress
) {
2549 /* do another pass to fix up temporary names */
2551 (void) printf("another pass:\n");
2559 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2560 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2561 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2563 nvlist_t
*stream_nv
= NULL
;
2564 avl_tree_t
*stream_avl
= NULL
;
2565 char *fromsnap
= NULL
;
2566 char *sendsnap
= NULL
;
2568 char tofs
[ZFS_MAXNAMELEN
];
2569 char sendfs
[ZFS_MAXNAMELEN
];
2571 dmu_replay_record_t drre
;
2573 boolean_t anyerr
= B_FALSE
;
2574 boolean_t softerr
= B_FALSE
;
2575 boolean_t recursive
;
2577 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2580 assert(drr
->drr_type
== DRR_BEGIN
);
2581 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2582 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2583 DMU_COMPOUNDSTREAM
);
2586 * Read in the nvlist from the stream.
2588 if (drr
->drr_payloadlen
!= 0) {
2589 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2590 &stream_nv
, flags
->byteswap
, zc
);
2592 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2597 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2600 if (recursive
&& strchr(destname
, '@')) {
2601 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2602 "cannot specify snapshot name for multi-snapshot stream"));
2603 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2608 * Read in the end record and verify checksum.
2610 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2611 flags
->byteswap
, NULL
)))
2613 if (flags
->byteswap
) {
2614 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2615 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2616 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2617 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2618 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2619 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2620 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2621 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2622 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2624 if (drre
.drr_type
!= DRR_END
) {
2625 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2628 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2630 "incorrect header checksum"));
2631 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2635 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2637 if (drr
->drr_payloadlen
!= 0) {
2638 nvlist_t
*stream_fss
;
2640 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2642 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2643 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2644 "couldn't allocate avl tree"));
2645 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2649 if (fromsnap
!= NULL
) {
2650 nvlist_t
*renamed
= NULL
;
2651 nvpair_t
*pair
= NULL
;
2653 (void) strlcpy(tofs
, destname
, ZFS_MAXNAMELEN
);
2654 if (flags
->isprefix
) {
2655 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2658 if (flags
->istail
) {
2659 cp
= strrchr(drrb
->drr_toname
, '/');
2661 (void) strlcat(tofs
, "/",
2665 i
= (cp
- drrb
->drr_toname
);
2668 i
= strcspn(drrb
->drr_toname
, "/@");
2670 /* zfs_receive_one() will create_parents() */
2671 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2673 *strchr(tofs
, '@') = '\0';
2676 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2677 VERIFY(0 == nvlist_alloc(&renamed
,
2678 NV_UNIQUE_NAME
, 0));
2681 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2682 stream_nv
, stream_avl
, renamed
);
2684 /* Unmount renamed filesystems before receiving. */
2685 while ((pair
= nvlist_next_nvpair(renamed
,
2688 prop_changelist_t
*clp
= NULL
;
2690 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2691 ZFS_TYPE_FILESYSTEM
);
2693 clp
= changelist_gather(zhp
,
2694 ZFS_PROP_MOUNTPOINT
, 0, 0);
2698 changelist_prefix(clp
);
2699 changelist_free(clp
);
2704 nvlist_free(renamed
);
2709 * Get the fs specified by the first path in the stream (the top level
2710 * specified by 'zfs send') and pass it to each invocation of
2711 * zfs_receive_one().
2713 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2715 if ((cp
= strchr(sendfs
, '@')) != NULL
) {
2718 * Find the "sendsnap", the final snapshot in a replication
2719 * stream. zfs_receive_one() handles certain errors
2720 * differently, depending on if the contained stream is the
2723 sendsnap
= (cp
+ 1);
2726 /* Finally, receive each contained stream */
2729 * we should figure out if it has a recoverable
2730 * error, in which case do a recv_skip() and drive on.
2731 * Note, if we fail due to already having this guid,
2732 * zfs_receive_one() will take care of it (ie,
2733 * recv_skip() and return 0).
2735 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
2736 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2737 action_handlep
, sendsnap
);
2738 if (error
== ENODATA
) {
2743 } while (error
== 0);
2745 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2747 * Now that we have the fs's they sent us, try the
2750 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2751 stream_nv
, stream_avl
, NULL
);
2755 fsavl_destroy(stream_avl
);
2757 nvlist_free(stream_nv
);
2766 trunc_prop_errs(int truncated
)
2768 ASSERT(truncated
!= 0);
2771 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2772 "1 more property could not be set\n"));
2774 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2775 "%d more properties could not be set\n"), truncated
);
2779 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2781 dmu_replay_record_t
*drr
;
2782 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2785 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2786 "cannot receive:"));
2788 /* XXX would be great to use lseek if possible... */
2791 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2792 byteswap
, NULL
) == 0) {
2794 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2796 switch (drr
->drr_type
) {
2798 if (drr
->drr_payloadlen
!= 0) {
2799 (void) recv_read(hdl
, fd
, buf
,
2800 drr
->drr_payloadlen
, B_FALSE
, NULL
);
2810 drr
->drr_u
.drr_object
.drr_bonuslen
=
2811 BSWAP_32(drr
->drr_u
.drr_object
.
2814 (void) recv_read(hdl
, fd
, buf
,
2815 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2821 drr
->drr_u
.drr_write
.drr_length
=
2822 BSWAP_64(drr
->drr_u
.drr_write
.drr_length
);
2824 (void) recv_read(hdl
, fd
, buf
,
2825 drr
->drr_u
.drr_write
.drr_length
, B_FALSE
, NULL
);
2829 drr
->drr_u
.drr_write
.drr_length
=
2830 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2832 (void) recv_read(hdl
, fd
, buf
,
2833 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2835 case DRR_WRITE_EMBEDDED
:
2837 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2838 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2841 (void) recv_read(hdl
, fd
, buf
,
2842 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2845 case DRR_WRITE_BYREF
:
2846 case DRR_FREEOBJECTS
:
2851 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2852 "invalid record type"));
2853 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2862 recv_ecksum_set_aux(libzfs_handle_t
*hdl
, const char *target_snap
,
2863 boolean_t resumable
)
2865 char target_fs
[ZFS_MAXNAMELEN
];
2867 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2868 "checksum mismatch or incomplete stream"));
2872 (void) strlcpy(target_fs
, target_snap
, sizeof (target_fs
));
2873 *strchr(target_fs
, '@') = '\0';
2874 zfs_handle_t
*zhp
= zfs_open(hdl
, target_fs
,
2875 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2879 char token_buf
[ZFS_MAXPROPLEN
];
2880 int error
= zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
2881 token_buf
, sizeof (token_buf
),
2882 NULL
, NULL
, 0, B_TRUE
);
2884 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2885 "checksum mismatch or incomplete stream.\n"
2886 "Partially received snapshot is saved.\n"
2887 "A resuming stream can be generated on the sending "
2888 "system by running:\n"
2896 * Restores a backup of tosnap from the file descriptor specified by infd.
2899 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2900 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
2901 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
2902 avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2903 uint64_t *action_handlep
, const char *finalsnap
)
2905 zfs_cmd_t zc
= { 0 };
2907 int ioctl_err
, ioctl_errno
, err
;
2909 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2911 char prop_errbuf
[1024];
2912 const char *chopprefix
;
2913 boolean_t newfs
= B_FALSE
;
2914 boolean_t stream_wantsnewfs
;
2915 uint64_t parent_snapguid
= 0;
2916 prop_changelist_t
*clp
= NULL
;
2917 nvlist_t
*snapprops_nvlist
= NULL
;
2918 zprop_errflags_t prop_errflags
;
2919 boolean_t recursive
;
2920 char *snapname
= NULL
;
2922 begin_time
= time(NULL
);
2924 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2927 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2930 if (stream_avl
!= NULL
) {
2931 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
2936 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
2938 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
2940 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
2942 if (flags
->canmountoff
) {
2943 VERIFY(0 == nvlist_add_uint64(props
,
2944 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
2946 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
2950 if (0 == nvlist_lookup_nvlist(fs
, "snapprops", &props
)) {
2951 VERIFY(0 == nvlist_lookup_nvlist(props
,
2952 snapname
, &snapprops_nvlist
));
2962 * Determine how much of the snapshot name stored in the stream
2963 * we are going to tack on to the name they specified on the
2964 * command line, and how much we are going to chop off.
2966 * If they specified a snapshot, chop the entire name stored in
2969 if (flags
->istail
) {
2971 * A filesystem was specified with -e. We want to tack on only
2972 * the tail of the sent snapshot path.
2974 if (strchr(tosnap
, '@')) {
2975 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2976 "argument - snapshot not allowed with -e"));
2977 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2980 chopprefix
= strrchr(sendfs
, '/');
2982 if (chopprefix
== NULL
) {
2984 * The tail is the poolname, so we need to
2985 * prepend a path separator.
2987 int len
= strlen(drrb
->drr_toname
);
2988 cp
= malloc(len
+ 2);
2990 (void) strcpy(&cp
[1], drrb
->drr_toname
);
2993 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
2995 } else if (flags
->isprefix
) {
2997 * A filesystem was specified with -d. We want to tack on
2998 * everything but the first element of the sent snapshot path
2999 * (all but the pool name).
3001 if (strchr(tosnap
, '@')) {
3002 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3003 "argument - snapshot not allowed with -d"));
3004 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3007 chopprefix
= strchr(drrb
->drr_toname
, '/');
3008 if (chopprefix
== NULL
)
3009 chopprefix
= strchr(drrb
->drr_toname
, '@');
3010 } else if (strchr(tosnap
, '@') == NULL
) {
3012 * If a filesystem was specified without -d or -e, we want to
3013 * tack on everything after the fs specified by 'zfs send'.
3015 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
3017 /* A snapshot was specified as an exact path (no -d or -e). */
3019 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3020 "cannot specify snapshot name for multi-snapshot "
3022 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3024 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
3027 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
3028 ASSERT(chopprefix
> drrb
->drr_toname
);
3029 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
3030 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
3031 chopprefix
[0] == '\0');
3034 * Determine name of destination snapshot, store in zc_value.
3036 (void) strcpy(zc
.zc_value
, tosnap
);
3037 (void) strncat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
3039 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
3040 zcmd_free_nvlists(&zc
);
3041 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
3045 * Determine the name of the origin snapshot, store in zc_string.
3047 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
3048 if (guid_to_name(hdl
, zc
.zc_value
,
3049 drrb
->drr_fromguid
, B_FALSE
, zc
.zc_string
) != 0) {
3050 zcmd_free_nvlists(&zc
);
3051 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3052 "local origin for clone %s does not exist"),
3054 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3057 (void) printf("found clone origin %s\n", zc
.zc_string
);
3058 } else if (originsnap
) {
3059 (void) strncpy(zc
.zc_string
, originsnap
, ZFS_MAXNAMELEN
);
3061 (void) printf("using provided clone origin %s\n",
3065 boolean_t resuming
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
3066 DMU_BACKUP_FEATURE_RESUMING
;
3067 stream_wantsnewfs
= (drrb
->drr_fromguid
== NULL
||
3068 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
) && !resuming
;
3070 if (stream_wantsnewfs
) {
3072 * if the parent fs does not exist, look for it based on
3073 * the parent snap GUID
3075 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3076 "cannot receive new filesystem stream"));
3078 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
3079 cp
= strrchr(zc
.zc_name
, '/');
3083 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
3084 char suffix
[ZFS_MAXNAMELEN
];
3085 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
3086 if (guid_to_name(hdl
, zc
.zc_name
, parent_snapguid
,
3087 B_FALSE
, zc
.zc_value
) == 0) {
3088 *strchr(zc
.zc_value
, '@') = '\0';
3089 (void) strcat(zc
.zc_value
, suffix
);
3094 * if the fs does not exist, look for it based on the
3097 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3098 "cannot receive incremental stream"));
3100 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
3101 *strchr(zc
.zc_name
, '@') = '\0';
3104 * If the exact receive path was specified and this is the
3105 * topmost path in the stream, then if the fs does not exist we
3106 * should look no further.
3108 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
3109 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
3110 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
3111 char snap
[ZFS_MAXNAMELEN
];
3112 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
3113 if (guid_to_name(hdl
, zc
.zc_name
, drrb
->drr_fromguid
,
3114 B_FALSE
, zc
.zc_value
) == 0) {
3115 *strchr(zc
.zc_value
, '@') = '\0';
3116 (void) strcat(zc
.zc_value
, snap
);
3121 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
3122 *strchr(zc
.zc_name
, '@') = '\0';
3124 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
3128 * Destination fs exists. It must be one of these cases:
3129 * - an incremental send stream
3130 * - the stream specifies a new fs (full stream or clone)
3131 * and they want us to blow away the existing fs (and
3132 * have therefore specified -F and removed any snapshots)
3133 * - we are resuming a failed receive.
3135 if (stream_wantsnewfs
) {
3136 if (!flags
->force
) {
3137 zcmd_free_nvlists(&zc
);
3138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3139 "destination '%s' exists\n"
3140 "must specify -F to overwrite it"),
3142 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3144 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
3146 zcmd_free_nvlists(&zc
);
3147 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3148 "destination has snapshots (eg. %s)\n"
3149 "must destroy them to overwrite it"),
3151 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3155 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
3156 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
3157 zcmd_free_nvlists(&zc
);
3161 if (stream_wantsnewfs
&&
3162 zhp
->zfs_dmustats
.dds_origin
[0]) {
3163 zcmd_free_nvlists(&zc
);
3165 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3166 "destination '%s' is a clone\n"
3167 "must destroy it to overwrite it"),
3169 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
3172 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
3173 stream_wantsnewfs
) {
3174 /* We can't do online recv in this case */
3175 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
3178 zcmd_free_nvlists(&zc
);
3181 if (changelist_prefix(clp
) != 0) {
3182 changelist_free(clp
);
3184 zcmd_free_nvlists(&zc
);
3190 * If we are resuming a newfs, set newfs here so that we will
3191 * mount it if the recv succeeds this time. We can tell
3192 * that it was a newfs on the first recv because the fs
3193 * itself will be inconsistent (if the fs existed when we
3194 * did the first recv, we would have received it into
3197 if (resuming
&& zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
))
3203 * Destination filesystem does not exist. Therefore we better
3204 * be creating a new filesystem (either from a full backup, or
3205 * a clone). It would therefore be invalid if the user
3206 * specified only the pool name (i.e. if the destination name
3207 * contained no slash character).
3209 if (!stream_wantsnewfs
||
3210 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
3211 zcmd_free_nvlists(&zc
);
3212 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3213 "destination '%s' does not exist"), zc
.zc_name
);
3214 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3218 * Trim off the final dataset component so we perform the
3219 * recvbackup ioctl to the filesystems's parent.
3223 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
3224 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
3225 zcmd_free_nvlists(&zc
);
3226 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
3232 zc
.zc_begin_record
= *drr_noswap
;
3233 zc
.zc_cookie
= infd
;
3234 zc
.zc_guid
= flags
->force
;
3235 zc
.zc_resumable
= flags
->resumable
;
3236 if (flags
->verbose
) {
3237 (void) printf("%s %s stream of %s into %s\n",
3238 flags
->dryrun
? "would receive" : "receiving",
3239 drrb
->drr_fromguid
? "incremental" : "full",
3240 drrb
->drr_toname
, zc
.zc_value
);
3241 (void) fflush(stdout
);
3244 if (flags
->dryrun
) {
3245 zcmd_free_nvlists(&zc
);
3246 return (recv_skip(hdl
, infd
, flags
->byteswap
));
3249 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
3250 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
3251 zc
.zc_cleanup_fd
= cleanup_fd
;
3252 zc
.zc_action_handle
= *action_handlep
;
3254 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
3255 ioctl_errno
= errno
;
3256 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
3259 nvlist_t
*prop_errors
;
3260 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
3261 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
3263 nvpair_t
*prop_err
= NULL
;
3265 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
3266 prop_err
)) != NULL
) {
3271 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
3272 (void) nvpair_value_int32(prop_err
, &intval
);
3273 if (strcmp(nvpair_name(prop_err
),
3274 ZPROP_N_MORE_ERRORS
) == 0) {
3275 trunc_prop_errs(intval
);
3277 } else if (snapname
== NULL
|| finalsnap
== NULL
||
3278 strcmp(finalsnap
, snapname
) == 0 ||
3279 strcmp(nvpair_name(prop_err
),
3280 zfs_prop_to_name(ZFS_PROP_REFQUOTA
)) != 0) {
3282 * Skip the special case of, for example,
3283 * "refquota", errors on intermediate
3284 * snapshots leading up to a final one.
3285 * That's why we have all of the checks above.
3287 * See zfs_ioctl.c's extract_delay_props() for
3288 * a list of props which can fail on
3289 * intermediate snapshots, but shouldn't
3290 * affect the overall receive.
3292 (void) snprintf(tbuf
, sizeof (tbuf
),
3293 dgettext(TEXT_DOMAIN
,
3294 "cannot receive %s property on %s"),
3295 nvpair_name(prop_err
), zc
.zc_name
);
3296 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3299 nvlist_free(prop_errors
);
3302 zc
.zc_nvlist_dst
= 0;
3303 zc
.zc_nvlist_dst_size
= 0;
3304 zcmd_free_nvlists(&zc
);
3306 if (err
== 0 && snapprops_nvlist
) {
3307 zfs_cmd_t zc2
= { 0 };
3309 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
3310 zc2
.zc_cookie
= B_TRUE
; /* received */
3311 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
3312 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
3313 zcmd_free_nvlists(&zc2
);
3317 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3319 * It may be that this snapshot already exists,
3320 * in which case we want to consume & ignore it
3321 * rather than failing.
3323 avl_tree_t
*local_avl
;
3324 nvlist_t
*local_nv
, *fs
;
3325 cp
= strchr(zc
.zc_value
, '@');
3328 * XXX Do this faster by just iterating over snaps in
3329 * this fs. Also if zc_value does not exist, we will
3330 * get a strange "does not exist" error message.
3333 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
3334 &local_nv
, &local_avl
) == 0) {
3336 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3337 fsavl_destroy(local_avl
);
3338 nvlist_free(local_nv
);
3341 if (flags
->verbose
) {
3342 (void) printf("snap %s already exists; "
3343 "ignoring\n", zc
.zc_value
);
3345 err
= ioctl_err
= recv_skip(hdl
, infd
,
3352 if (ioctl_err
!= 0) {
3353 switch (ioctl_errno
) {
3355 cp
= strchr(zc
.zc_value
, '@');
3357 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3358 "most recent snapshot of %s does not\n"
3359 "match incremental source"), zc
.zc_value
);
3360 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3364 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3365 "destination %s has been modified\n"
3366 "since most recent snapshot"), zc
.zc_name
);
3367 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3370 cp
= strchr(zc
.zc_value
, '@');
3372 /* it's the containing fs that exists */
3375 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3376 "destination already exists"));
3377 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3378 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3383 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3386 recv_ecksum_set_aux(hdl
, zc
.zc_value
, flags
->resumable
);
3387 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3390 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3391 "pool must be upgraded to receive this stream."));
3392 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3395 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3396 "destination %s space quota exceeded"), zc
.zc_name
);
3397 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3400 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3405 * Mount the target filesystem (if created). Also mount any
3406 * children of the target filesystem if we did a replication
3407 * receive (indicated by stream_avl being non-NULL).
3409 cp
= strchr(zc
.zc_value
, '@');
3410 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3414 h
= zfs_open(hdl
, zc
.zc_value
,
3415 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3417 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3419 } else if (newfs
|| stream_avl
) {
3421 * Track the first/top of hierarchy fs,
3422 * for mounting and sharing later.
3424 if (top_zfs
&& *top_zfs
== NULL
)
3425 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
3433 err
|= changelist_postfix(clp
);
3434 changelist_free(clp
);
3437 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3438 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3439 "failed to clear unreceived properties on %s"),
3441 (void) fprintf(stderr
, "\n");
3443 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3444 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3445 "failed to restore original properties on %s"),
3447 (void) fprintf(stderr
, "\n");
3450 if (err
|| ioctl_err
)
3453 *action_handlep
= zc
.zc_action_handle
;
3455 if (flags
->verbose
) {
3458 uint64_t bytes
= zc
.zc_cookie
;
3459 time_t delta
= time(NULL
) - begin_time
;
3462 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3463 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3465 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3473 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
3474 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
3475 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3476 uint64_t *action_handlep
, const char *finalsnap
)
3479 dmu_replay_record_t drr
, drr_noswap
;
3480 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3482 zio_cksum_t zcksum
= { 0 };
3483 uint64_t featureflags
;
3486 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3489 if (flags
->isprefix
&&
3490 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3491 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3492 "(%s) does not exist"), tosnap
);
3493 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3496 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
3497 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
3498 "(%s) does not exist"), originsnap
);
3499 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3502 /* read in the BEGIN record */
3503 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3507 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3508 /* It's the double end record at the end of a package */
3512 /* the kernel needs the non-byteswapped begin record */
3515 flags
->byteswap
= B_FALSE
;
3516 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3518 * We computed the checksum in the wrong byteorder in
3519 * recv_read() above; do it again correctly.
3521 bzero(&zcksum
, sizeof (zio_cksum_t
));
3522 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3523 flags
->byteswap
= B_TRUE
;
3525 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3526 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3527 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3528 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3529 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3530 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3531 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3532 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3533 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3536 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3537 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3538 "stream (bad magic number)"));
3539 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3542 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3543 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3545 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3546 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3547 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3548 "stream has unsupported feature, feature flags = %lx"),
3550 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3553 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3554 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3555 "stream (bad snapshot name)"));
3556 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3559 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3560 char nonpackage_sendfs
[ZFS_MAXNAMELEN
];
3561 if (sendfs
== NULL
) {
3563 * We were not called from zfs_receive_package(). Get
3564 * the fs specified by 'zfs send'.
3567 (void) strlcpy(nonpackage_sendfs
,
3568 drr
.drr_u
.drr_begin
.drr_toname
, ZFS_MAXNAMELEN
);
3569 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3571 sendfs
= nonpackage_sendfs
;
3572 VERIFY(finalsnap
== NULL
);
3574 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
3575 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
3576 cleanup_fd
, action_handlep
, finalsnap
));
3578 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3579 DMU_COMPOUNDSTREAM
);
3580 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
3581 &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3586 * Restores a backup of tosnap from the file descriptor specified by infd.
3587 * Return 0 on total success, -2 if some things couldn't be
3588 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3589 * (-1 will override -2, if -1 and the resumable flag was specified the
3590 * transfer can be resumed if the sending side supports it).
3593 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
3594 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
3596 char *top_zfs
= NULL
;
3599 uint64_t action_handle
= 0;
3600 char *originsnap
= NULL
;
3602 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
3603 if (err
&& err
!= ENOENT
)
3607 cleanup_fd
= open(ZFS_DEV
, O_RDWR
|O_EXCL
);
3608 VERIFY(cleanup_fd
>= 0);
3610 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
3611 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
, NULL
);
3613 VERIFY(0 == close(cleanup_fd
));
3615 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3617 prop_changelist_t
*clp
;
3619 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3621 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3622 CL_GATHER_MOUNT_ALWAYS
, 0);
3625 /* mount and share received datasets */
3626 err
= changelist_postfix(clp
);
3627 changelist_free(clp
);
3630 if (zhp
== NULL
|| clp
== NULL
|| err
)