4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
41 #include <sys/mount.h>
42 #include <sys/mntent.h>
43 #include <sys/mnttab.h>
45 #include <sys/debug.h>
53 #include <libzfs_core.h>
55 #include "zfs_namecheck.h"
57 #include "zfs_fletcher.h"
58 #include "libzfs_impl.h"
59 #include <sys/zio_checksum.h>
61 #include <sys/socket.h>
63 /* in libzfs_dataset.c */
64 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
66 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
67 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **, int,
70 static const zio_cksum_t zero_cksum
= { { 0 } };
72 typedef struct dedup_arg
{
75 libzfs_handle_t
*dedup_hdl
;
78 typedef struct progress_arg
{
81 boolean_t pa_parsable
;
84 typedef struct dataref
{
90 typedef struct dedup_entry
{
91 struct dedup_entry
*dde_next
;
92 zio_cksum_t dde_chksum
;
97 #define MAX_DDT_PHYSMEM_PERCENT 20
98 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
100 typedef struct dedup_table
{
101 dedup_entry_t
**dedup_hash_array
;
102 umem_cache_t
*ddecache
;
103 uint64_t max_ddt_size
; /* max dedup table size in bytes */
104 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
111 high_order_bit(uint64_t n
)
115 for (count
= 0; n
!= 0; count
++)
121 ssread(void *buf
, size_t len
, FILE *stream
)
125 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
132 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
133 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
137 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
138 if (ddt
->ddt_full
== B_FALSE
) {
139 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
140 "Dedup table full. Deduplication will continue "
141 "with existing table entries"));
142 ddt
->ddt_full
= B_TRUE
;
147 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
149 assert(*ddepp
== NULL
);
150 dde
->dde_next
= NULL
;
151 dde
->dde_chksum
= *cs
;
152 dde
->dde_prop
= prop
;
155 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
161 * Using the specified dedup table, do a lookup for an entry with
162 * the checksum cs. If found, return the block's reference info
163 * in *dr. Otherwise, insert a new entry in the dedup table, using
164 * the reference information specified by *dr.
166 * return value: true - entry was found
167 * false - entry was not found
170 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
171 uint64_t prop
, dataref_t
*dr
)
174 dedup_entry_t
**ddepp
;
176 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
178 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
179 ddepp
= &((*ddepp
)->dde_next
)) {
180 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
181 (*ddepp
)->dde_prop
== prop
) {
182 *dr
= (*ddepp
)->dde_ref
;
186 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
191 dump_record(dmu_replay_record_t
*drr
, void *payload
, int payload_len
,
192 zio_cksum_t
*zc
, int outfd
)
194 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
195 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
196 fletcher_4_incremental_native(drr
,
197 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
198 if (drr
->drr_type
!= DRR_BEGIN
) {
199 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
200 drr_checksum
.drr_checksum
));
201 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
203 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
204 sizeof (zio_cksum_t
), zc
);
205 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
207 if (payload_len
!= 0) {
208 fletcher_4_incremental_native(payload
, payload_len
, zc
);
209 if (write(outfd
, payload
, payload_len
) == -1)
216 * This function is started in a separate thread when the dedup option
217 * has been requested. The main send thread determines the list of
218 * snapshots to be included in the send stream and makes the ioctl calls
219 * for each one. But instead of having the ioctl send the output to the
220 * the output fd specified by the caller of zfs_send()), the
221 * ioctl is told to direct the output to a pipe, which is read by the
222 * alternate thread running THIS function. This function does the
224 * 1. building a dedup table (the DDT)
225 * 2. doing checksums on each data block and inserting a record in the DDT
226 * 3. looking for matching checksums, and
227 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
228 * a duplicate block is found.
229 * The output of this function then goes to the output fd requested
230 * by the caller of zfs_send().
235 dedup_arg_t
*dda
= arg
;
236 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
237 dmu_replay_record_t thedrr
;
238 dmu_replay_record_t
*drr
= &thedrr
;
242 zio_cksum_t stream_cksum
;
243 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
247 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
) / 100,
248 SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20);
250 numbuckets
= ddt
.max_ddt_size
/ (sizeof (dedup_entry_t
));
253 * numbuckets must be a power of 2. Increase number to
254 * a power of 2 if necessary.
256 if (!ISP2(numbuckets
))
257 numbuckets
= 1 << high_order_bit(numbuckets
);
259 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
260 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
261 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
262 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
263 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
264 ddt
.ddt_full
= B_FALSE
;
266 outfd
= dda
->outputfd
;
267 ofp
= fdopen(dda
->inputfd
, "r");
268 while (ssread(drr
, sizeof (*drr
), ofp
) != 0) {
270 switch (drr
->drr_type
) {
273 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
276 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
278 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
280 /* set the DEDUP feature flag for this stream */
281 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
282 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
283 DMU_BACKUP_FEATURE_DEDUPPROPS
);
284 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
286 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
287 DMU_COMPOUNDSTREAM
&& drr
->drr_payloadlen
!= 0) {
288 sz
= drr
->drr_payloadlen
;
290 if (sz
> SPA_MAXBLOCKSIZE
) {
291 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
292 SPA_MAXBLOCKSIZE
, sz
);
294 (void) ssread(buf
, sz
, ofp
);
298 if (dump_record(drr
, buf
, sz
, &stream_cksum
,
306 struct drr_end
*drre
= &drr
->drr_u
.drr_end
;
307 /* use the recalculated checksum */
308 drre
->drr_checksum
= stream_cksum
;
309 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
317 struct drr_object
*drro
= &drr
->drr_u
.drr_object
;
318 if (drro
->drr_bonuslen
> 0) {
320 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
323 if (dump_record(drr
, buf
,
324 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
325 &stream_cksum
, outfd
) != 0)
332 struct drr_spill
*drrs
= &drr
->drr_u
.drr_spill
;
333 (void) ssread(buf
, drrs
->drr_length
, ofp
);
334 if (dump_record(drr
, buf
, drrs
->drr_length
,
335 &stream_cksum
, outfd
) != 0)
340 case DRR_FREEOBJECTS
:
342 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
350 struct drr_write
*drrw
= &drr
->drr_u
.drr_write
;
353 (void) ssread(buf
, drrw
->drr_length
, ofp
);
356 * Use the existing checksum if it's dedup-capable,
357 * else calculate a SHA256 checksum for it.
360 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
362 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
363 zio_cksum_t tmpsha256
;
365 zio_checksum_SHA256(buf
,
366 drrw
->drr_length
, &tmpsha256
);
368 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
369 BE_64(tmpsha256
.zc_word
[0]);
370 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
371 BE_64(tmpsha256
.zc_word
[1]);
372 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
373 BE_64(tmpsha256
.zc_word
[2]);
374 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
375 BE_64(tmpsha256
.zc_word
[3]);
376 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
377 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
380 dataref
.ref_guid
= drrw
->drr_toguid
;
381 dataref
.ref_object
= drrw
->drr_object
;
382 dataref
.ref_offset
= drrw
->drr_offset
;
384 if (ddt_update(dda
->dedup_hdl
, &ddt
,
385 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
387 dmu_replay_record_t wbr_drr
= {0};
388 struct drr_write_byref
*wbr_drrr
=
389 &wbr_drr
.drr_u
.drr_write_byref
;
391 /* block already present in stream */
392 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
394 wbr_drrr
->drr_object
= drrw
->drr_object
;
395 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
396 wbr_drrr
->drr_length
= drrw
->drr_length
;
397 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
398 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
399 wbr_drrr
->drr_refobject
=
401 wbr_drrr
->drr_refoffset
=
404 wbr_drrr
->drr_checksumtype
=
405 drrw
->drr_checksumtype
;
406 wbr_drrr
->drr_checksumflags
=
407 drrw
->drr_checksumtype
;
408 wbr_drrr
->drr_key
.ddk_cksum
=
409 drrw
->drr_key
.ddk_cksum
;
410 wbr_drrr
->drr_key
.ddk_prop
=
411 drrw
->drr_key
.ddk_prop
;
413 if (dump_record(&wbr_drr
, NULL
, 0,
414 &stream_cksum
, outfd
) != 0)
417 /* block not previously seen */
418 if (dump_record(drr
, buf
, drrw
->drr_length
,
419 &stream_cksum
, outfd
) != 0)
425 case DRR_WRITE_EMBEDDED
:
427 struct drr_write_embedded
*drrwe
=
428 &drr
->drr_u
.drr_write_embedded
;
430 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
431 if (dump_record(drr
, buf
,
432 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
433 &stream_cksum
, outfd
) != 0)
440 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
447 (void) fprintf(stderr
, "INVALID record type 0x%x\n",
449 /* should never happen, so assert */
454 umem_cache_destroy(ddt
.ddecache
);
455 free(ddt
.dedup_hash_array
);
463 * Routines for dealing with the AVL tree of fs-nvlists
465 typedef struct fsavl_node
{
473 fsavl_compare(const void *arg1
, const void *arg2
)
475 const fsavl_node_t
*fn1
= arg1
;
476 const fsavl_node_t
*fn2
= arg2
;
478 if (fn1
->fn_guid
> fn2
->fn_guid
)
480 else if (fn1
->fn_guid
< fn2
->fn_guid
)
487 * Given the GUID of a snapshot, find its containing filesystem and
491 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
493 fsavl_node_t fn_find
;
496 fn_find
.fn_guid
= snapguid
;
498 fn
= avl_find(avl
, &fn_find
, NULL
);
501 *snapname
= fn
->fn_snapname
;
502 return (fn
->fn_nvfs
);
508 fsavl_destroy(avl_tree_t
*avl
)
517 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
524 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
527 fsavl_create(nvlist_t
*fss
)
530 nvpair_t
*fselem
= NULL
;
532 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
535 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
536 offsetof(fsavl_node_t
, fn_node
));
538 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
539 nvlist_t
*nvfs
, *snaps
;
540 nvpair_t
*snapelem
= NULL
;
542 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
543 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
546 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
550 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
551 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
552 fsavl_destroy(fsavl
);
556 fn
->fn_snapname
= nvpair_name(snapelem
);
560 * Note: if there are multiple snaps with the
561 * same GUID, we ignore all but one.
563 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
574 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
576 typedef struct send_data
{
577 uint64_t parent_fromsnap_guid
;
578 nvlist_t
*parent_snaps
;
581 const char *fromsnap
;
588 * The header nvlist is of the following format:
591 * "fromsnap" -> string (if incremental)
595 * "name" -> string (full name; for debugging)
596 * "parentfromsnap" -> number (guid of fromsnap in parent)
598 * "props" -> { name -> value (only if set here) }
599 * "snaps" -> { name (lastname) -> number (guid) }
600 * "snapprops" -> { name (lastname) -> { name -> value } }
602 * "origin" -> number (guid) (if clone)
603 * "sent" -> boolean (not on-disk)
611 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
614 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
616 send_data_t
*sd
= arg
;
617 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
620 boolean_t isfromsnap
, istosnap
, istosnapwithnofrom
;
622 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
623 isfromsnap
= (sd
->fromsnap
!= NULL
&&
624 strcmp(sd
->fromsnap
, snapname
) == 0);
625 istosnap
= (sd
->tosnap
!= NULL
&& (strcmp(sd
->tosnap
, snapname
) == 0));
626 istosnapwithnofrom
= (istosnap
&& sd
->fromsnap
== NULL
);
628 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
630 * NB: if there is no fromsnap here (it's a newly created fs in
631 * an incremental replication), we will substitute the tosnap.
633 if (isfromsnap
|| (sd
->parent_fromsnap_guid
== 0 && istosnap
)) {
634 sd
->parent_fromsnap_guid
= guid
;
637 if (!sd
->recursive
) {
638 if (!sd
->seenfrom
&& isfromsnap
) {
639 sd
->seenfrom
= B_TRUE
;
644 if ((sd
->seento
|| !sd
->seenfrom
) && !istosnapwithnofrom
) {
653 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
654 send_iterate_prop(zhp
, nv
);
655 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
663 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
665 nvpair_t
*elem
= NULL
;
667 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
668 char *propname
= nvpair_name(elem
);
669 zfs_prop_t prop
= zfs_name_to_prop(propname
);
672 if (!zfs_prop_user(propname
)) {
674 * Realistically, this should never happen. However,
675 * we want the ability to add DSL properties without
676 * needing to make incompatible version changes. We
677 * need to ignore unknown properties to allow older
678 * software to still send datasets containing these
679 * properties, with the unknown properties elided.
681 if (prop
== ZPROP_INVAL
)
684 if (zfs_prop_readonly(prop
))
688 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
689 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
690 prop
== ZFS_PROP_REFQUOTA
||
691 prop
== ZFS_PROP_REFRESERVATION
) {
694 verify(nvlist_lookup_uint64(propnv
,
695 ZPROP_VALUE
, &value
) == 0);
696 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
699 * May have no source before SPA_VERSION_RECVD_PROPS,
700 * but is still modifiable.
702 if (nvlist_lookup_string(propnv
,
703 ZPROP_SOURCE
, &source
) == 0) {
704 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
706 ZPROP_SOURCE_VAL_RECVD
) != 0))
711 if (nvlist_lookup_string(propnv
,
712 ZPROP_SOURCE
, &source
) != 0)
714 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
715 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
719 if (zfs_prop_user(propname
) ||
720 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
722 verify(nvlist_lookup_string(propnv
,
723 ZPROP_VALUE
, &value
) == 0);
724 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
727 verify(nvlist_lookup_uint64(propnv
,
728 ZPROP_VALUE
, &value
) == 0);
729 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
735 * recursively generate nvlists describing datasets. See comment
736 * for the data structure send_data_t above for description of contents
740 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
742 send_data_t
*sd
= arg
;
745 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
746 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
749 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
750 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
751 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
752 sd
->parent_fromsnap_guid
));
754 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
755 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
756 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
759 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
760 origin
->zfs_dmustats
.dds_guid
));
763 /* iterate over props */
764 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
765 send_iterate_prop(zhp
, nv
);
766 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
769 /* iterate over snaps, and set sd->parent_fromsnap_guid */
770 sd
->parent_fromsnap_guid
= 0;
771 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
772 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
773 (void) zfs_iter_snapshots_sorted(zhp
, send_iterate_snap
, sd
);
774 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
775 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
776 nvlist_free(sd
->parent_snaps
);
777 nvlist_free(sd
->snapprops
);
779 /* add this fs to nvlist */
780 (void) snprintf(guidstring
, sizeof (guidstring
),
781 "0x%llx", (longlong_t
)guid
);
782 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
785 /* iterate over children */
787 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
789 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
796 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
797 const char *tosnap
, boolean_t recursive
, nvlist_t
**nvlp
, avl_tree_t
**avlp
)
800 send_data_t sd
= { 0 };
803 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
805 return (EZFS_BADTYPE
);
807 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
808 sd
.fromsnap
= fromsnap
;
810 sd
.recursive
= recursive
;
812 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
820 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
831 * Routines specific to "zfs send"
833 typedef struct send_dump_data
{
834 /* these are all just the short snapname (the part after the @) */
835 const char *fromsnap
;
837 char prevsnap
[ZFS_MAXNAMELEN
];
838 uint64_t prevsnap_obj
;
839 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
840 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
, std_out
;
841 boolean_t large_block
;
847 snapfilter_cb_t
*filter_cb
;
850 char holdtag
[ZFS_MAXNAMELEN
];
856 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
857 boolean_t fromorigin
, uint64_t *sizep
)
859 zfs_cmd_t zc
= {"\0"};
860 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
862 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
863 assert(fromsnap_obj
== 0 || !fromorigin
);
865 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
866 zc
.zc_obj
= fromorigin
;
867 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
868 zc
.zc_fromobj
= fromsnap_obj
;
869 zc
.zc_guid
= 1; /* estimate flag */
871 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
873 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
874 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
878 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
879 "not an earlier snapshot from the same fs"));
880 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
883 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
884 ZFS_TYPE_SNAPSHOT
)) {
885 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
886 "incremental source (@%s) does not exist"),
889 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
902 zfs_error_aux(hdl
, strerror(errno
));
903 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
906 return (zfs_standard_error(hdl
, errno
, errbuf
));
910 *sizep
= zc
.zc_objset_type
;
916 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
917 * NULL) to the file descriptor specified by outfd.
920 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
921 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
924 zfs_cmd_t zc
= {"\0"};
925 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
928 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
929 assert(fromsnap_obj
== 0 || !fromorigin
);
931 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
932 zc
.zc_cookie
= outfd
;
933 zc
.zc_obj
= fromorigin
;
934 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
935 zc
.zc_fromobj
= fromsnap_obj
;
938 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
939 if (fromsnap
&& fromsnap
[0] != '\0') {
940 VERIFY(0 == nvlist_add_string(thisdbg
,
941 "fromsnap", fromsnap
));
944 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
946 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
947 "warning: cannot send '%s'"), zhp
->zfs_name
);
949 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
951 VERIFY(0 == nvlist_add_nvlist(debugnv
,
952 zhp
->zfs_name
, thisdbg
));
954 nvlist_free(thisdbg
);
958 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
959 "not an earlier snapshot from the same fs"));
960 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
963 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
964 ZFS_TYPE_SNAPSHOT
)) {
965 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
966 "incremental source (@%s) does not exist"),
969 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
982 zfs_error_aux(hdl
, strerror(errno
));
983 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
986 return (zfs_standard_error(hdl
, errno
, errbuf
));
991 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
992 nvlist_free(thisdbg
);
998 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
1000 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1003 * zfs_send() only sets snapholds for sends that need them,
1004 * e.g. replication and doall.
1006 if (sdd
->snapholds
== NULL
)
1009 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1013 send_progress_thread(void *arg
)
1015 progress_arg_t
*pa
= arg
;
1017 zfs_cmd_t zc
= {"\0"};
1018 zfs_handle_t
*zhp
= pa
->pa_zhp
;
1019 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1020 unsigned long long bytes
;
1026 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1027 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1029 if (!pa
->pa_parsable
)
1030 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1033 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1038 zc
.zc_cookie
= pa
->pa_fd
;
1039 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1040 return ((void *)-1);
1044 bytes
= zc
.zc_cookie
;
1046 if (pa
->pa_parsable
) {
1047 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1048 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1049 bytes
, zhp
->zfs_name
);
1051 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1052 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1053 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1054 buf
, zhp
->zfs_name
);
1060 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1062 send_dump_data_t
*sdd
= arg
;
1063 progress_arg_t pa
= { 0 };
1067 boolean_t isfromsnap
, istosnap
, fromorigin
;
1068 boolean_t exclude
= B_FALSE
;
1069 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1072 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1073 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1074 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1076 if (!sdd
->seenfrom
&& isfromsnap
) {
1077 gather_holds(zhp
, sdd
);
1078 sdd
->seenfrom
= B_TRUE
;
1079 (void) strcpy(sdd
->prevsnap
, thissnap
);
1080 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1085 if (sdd
->seento
|| !sdd
->seenfrom
) {
1090 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1092 sdd
->seento
= B_TRUE
;
1094 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1095 if (sdd
->replicate
) {
1097 nvlist_t
*snapprops
;
1099 * Filter out all intermediate snapshots except origin
1100 * snapshots needed to replicate clones.
1102 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1103 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1105 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1106 "snapprops", &snapprops
));
1107 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1108 thissnap
, &snapprops
));
1109 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1116 * If a filter function exists, call it to determine whether
1117 * this snapshot will be sent.
1119 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1120 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1122 * This snapshot is filtered out. Don't send it, and don't
1123 * set prevsnap_obj, so it will be as if this snapshot didn't
1124 * exist, and the next accepted snapshot will be sent as
1125 * an incremental from the last accepted one, or as the
1126 * first (and full) snapshot in the case of a replication,
1127 * non-incremental send.
1133 gather_holds(zhp
, sdd
);
1134 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1135 (sdd
->fromorigin
|| sdd
->replicate
);
1139 err
= estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1142 if (sdd
->parsable
) {
1143 if (sdd
->prevsnap
[0] != '\0') {
1144 (void) fprintf(fout
, "incremental\t%s\t%s",
1145 sdd
->prevsnap
, zhp
->zfs_name
);
1147 (void) fprintf(fout
, "full\t%s",
1151 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1152 "send from @%s to %s"),
1153 sdd
->prevsnap
, zhp
->zfs_name
);
1156 if (sdd
->parsable
) {
1157 (void) fprintf(fout
, "\t%llu\n",
1161 zfs_nicenum(size
, buf
, sizeof (buf
));
1162 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1163 " estimated size is %s\n"), buf
);
1167 (void) fprintf(fout
, "\n");
1173 * If progress reporting is requested, spawn a new thread to
1174 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1176 if (sdd
->progress
) {
1178 pa
.pa_fd
= sdd
->outfd
;
1179 pa
.pa_parsable
= sdd
->parsable
;
1181 if ((err
= pthread_create(&tid
, NULL
,
1182 send_progress_thread
, &pa
))) {
1188 enum lzc_send_flags flags
= 0;
1189 if (sdd
->large_block
)
1190 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1191 if (sdd
->embed_data
)
1192 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1194 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1195 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1197 if (sdd
->progress
) {
1198 (void) pthread_cancel(tid
);
1199 (void) pthread_join(tid
, NULL
);
1203 (void) strcpy(sdd
->prevsnap
, thissnap
);
1204 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1210 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1213 send_dump_data_t
*sdd
= arg
;
1214 boolean_t missingfrom
= B_FALSE
;
1215 zfs_cmd_t zc
= {"\0"};
1217 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1218 zhp
->zfs_name
, sdd
->tosnap
);
1219 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1220 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1221 "WARNING: could not send %s@%s: does not exist\n"),
1222 zhp
->zfs_name
, sdd
->tosnap
);
1227 if (sdd
->replicate
&& sdd
->fromsnap
) {
1229 * If this fs does not have fromsnap, and we're doing
1230 * recursive, we need to send a full stream from the
1231 * beginning (or an incremental from the origin if this
1232 * is a clone). If we're doing non-recursive, then let
1233 * them get the error.
1235 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1236 zhp
->zfs_name
, sdd
->fromsnap
);
1237 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1238 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1239 missingfrom
= B_TRUE
;
1243 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1244 sdd
->prevsnap_obj
= 0;
1245 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1246 sdd
->seenfrom
= B_TRUE
;
1248 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1249 if (!sdd
->seenfrom
) {
1250 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1251 "WARNING: could not send %s@%s:\n"
1252 "incremental source (%s@%s) does not exist\n"),
1253 zhp
->zfs_name
, sdd
->tosnap
,
1254 zhp
->zfs_name
, sdd
->fromsnap
);
1256 } else if (!sdd
->seento
) {
1257 if (sdd
->fromsnap
) {
1258 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1259 "WARNING: could not send %s@%s:\n"
1260 "incremental source (%s@%s) "
1261 "is not earlier than it\n"),
1262 zhp
->zfs_name
, sdd
->tosnap
,
1263 zhp
->zfs_name
, sdd
->fromsnap
);
1265 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1267 "could not send %s@%s: does not exist\n"),
1268 zhp
->zfs_name
, sdd
->tosnap
);
1277 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1279 send_dump_data_t
*sdd
= arg
;
1281 boolean_t needagain
, progress
;
1283 if (!sdd
->replicate
)
1284 return (dump_filesystem(rzhp
, sdd
));
1286 /* Mark the clone origin snapshots. */
1287 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1288 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1290 uint64_t origin_guid
= 0;
1292 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1293 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1294 if (origin_guid
!= 0) {
1296 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1297 origin_guid
, &snapname
);
1298 if (origin_nv
!= NULL
) {
1299 nvlist_t
*snapprops
;
1300 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1301 "snapprops", &snapprops
));
1302 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1303 snapname
, &snapprops
));
1304 VERIFY(0 == nvlist_add_boolean(
1305 snapprops
, "is_clone_origin"));
1310 needagain
= progress
= B_FALSE
;
1311 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1312 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1313 nvlist_t
*fslist
, *parent_nv
;
1317 uint64_t origin_guid
= 0;
1318 uint64_t parent_guid
= 0;
1320 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1321 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1324 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1325 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1326 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1329 if (parent_guid
!= 0) {
1330 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1331 if (!nvlist_exists(parent_nv
, "sent")) {
1332 /* parent has not been sent; skip this one */
1338 if (origin_guid
!= 0) {
1339 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1341 if (origin_nv
!= NULL
&&
1342 !nvlist_exists(origin_nv
, "sent")) {
1344 * origin has not been sent yet;
1352 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1355 err
= dump_filesystem(zhp
, sdd
);
1356 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1367 /* clean out the sent flags in case we reuse this fss */
1368 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1369 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1372 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1373 (void) nvlist_remove_all(fslist
, "sent");
1380 * Generate a send stream for the dataset identified by the argument zhp.
1382 * The content of the send stream is the snapshot identified by
1383 * 'tosnap'. Incremental streams are requested in two ways:
1384 * - from the snapshot identified by "fromsnap" (if non-null) or
1385 * - from the origin of the dataset identified by zhp, which must
1386 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1389 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1390 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1391 * if "replicate" is set. If "doall" is set, dump all the intermediate
1392 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1393 * case too. If "props" is set, send properties.
1396 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1397 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1398 void *cb_arg
, nvlist_t
**debugnvp
)
1401 send_dump_data_t sdd
= { 0 };
1403 nvlist_t
*fss
= NULL
;
1404 avl_tree_t
*fsavl
= NULL
;
1405 static uint64_t holdseq
;
1409 dedup_arg_t dda
= { 0 };
1410 int featureflags
= 0;
1413 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1414 "cannot send '%s'"), zhp
->zfs_name
);
1416 if (fromsnap
&& fromsnap
[0] == '\0') {
1417 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1418 "zero-length incremental source"));
1419 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1422 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1424 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1425 if (version
>= ZPL_VERSION_SA
) {
1426 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1430 if (flags
->dedup
&& !flags
->dryrun
) {
1431 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1432 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1433 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
))) {
1434 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1435 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1438 dda
.outputfd
= outfd
;
1439 dda
.inputfd
= pipefd
[1];
1440 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1441 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1442 (void) close(pipefd
[0]);
1443 (void) close(pipefd
[1]);
1444 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1445 return (zfs_error(zhp
->zfs_hdl
,
1446 EZFS_THREADCREATEFAILED
, errbuf
));
1450 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1451 dmu_replay_record_t drr
= { 0 };
1452 char *packbuf
= NULL
;
1454 zio_cksum_t zc
= { { 0 } };
1456 if (flags
->replicate
|| flags
->props
) {
1459 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1461 VERIFY(0 == nvlist_add_string(hdrnv
,
1462 "fromsnap", fromsnap
));
1464 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1465 if (!flags
->replicate
) {
1466 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1470 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1471 fromsnap
, tosnap
, flags
->replicate
, &fss
, &fsavl
);
1474 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1475 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1485 if (!flags
->dryrun
) {
1486 /* write first begin record */
1487 drr
.drr_type
= DRR_BEGIN
;
1488 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1489 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1490 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1491 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1492 drr_versioninfo
, featureflags
);
1493 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1494 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1495 "%s@%s", zhp
->zfs_name
, tosnap
);
1496 drr
.drr_payloadlen
= buflen
;
1498 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, outfd
);
1503 /* write end record */
1504 bzero(&drr
, sizeof (drr
));
1505 drr
.drr_type
= DRR_END
;
1506 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1507 err
= write(outfd
, &drr
, sizeof (drr
));
1517 /* dump each stream */
1518 sdd
.fromsnap
= fromsnap
;
1519 sdd
.tosnap
= tosnap
;
1521 sdd
.outfd
= pipefd
[0];
1524 sdd
.replicate
= flags
->replicate
;
1525 sdd
.doall
= flags
->doall
;
1526 sdd
.fromorigin
= flags
->fromorigin
;
1529 sdd
.verbose
= flags
->verbose
;
1530 sdd
.parsable
= flags
->parsable
;
1531 sdd
.progress
= flags
->progress
;
1532 sdd
.dryrun
= flags
->dryrun
;
1533 sdd
.large_block
= flags
->largeblock
;
1534 sdd
.embed_data
= flags
->embed_data
;
1535 sdd
.filter_cb
= filter_func
;
1536 sdd
.filter_cb_arg
= cb_arg
;
1538 sdd
.debugnv
= *debugnvp
;
1539 if (sdd
.verbose
&& sdd
.dryrun
)
1540 sdd
.std_out
= B_TRUE
;
1541 fout
= sdd
.std_out
? stdout
: stderr
;
1544 * Some flags require that we place user holds on the datasets that are
1545 * being sent so they don't get destroyed during the send. We can skip
1546 * this step if the pool is imported read-only since the datasets cannot
1549 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1550 ZPOOL_PROP_READONLY
, NULL
) &&
1551 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1552 spa_version
>= SPA_VERSION_USERREFS
&&
1553 (flags
->doall
|| flags
->replicate
)) {
1555 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1556 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1557 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1558 if (sdd
.cleanup_fd
< 0) {
1562 sdd
.snapholds
= fnvlist_alloc();
1564 sdd
.cleanup_fd
= -1;
1565 sdd
.snapholds
= NULL
;
1567 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1569 * Do a verbose no-op dry run to get all the verbose output
1570 * or to gather snapshot hold's before generating any data,
1571 * then do a non-verbose real run to generate the streams.
1573 sdd
.dryrun
= B_TRUE
;
1574 err
= dump_filesystems(zhp
, &sdd
);
1579 if (flags
->verbose
) {
1580 if (flags
->parsable
) {
1581 (void) fprintf(fout
, "size\t%llu\n",
1582 (longlong_t
)sdd
.size
);
1585 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1586 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1587 "total estimated size is %s\n"), buf
);
1591 /* Ensure no snaps found is treated as an error. */
1597 /* Skip the second run if dryrun was requested. */
1601 if (sdd
.snapholds
!= NULL
) {
1602 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1606 fnvlist_free(sdd
.snapholds
);
1607 sdd
.snapholds
= NULL
;
1610 sdd
.dryrun
= B_FALSE
;
1611 sdd
.verbose
= B_FALSE
;
1614 err
= dump_filesystems(zhp
, &sdd
);
1615 fsavl_destroy(fsavl
);
1618 /* Ensure no snaps found is treated as an error. */
1619 if (err
== 0 && !sdd
.seento
)
1624 (void) pthread_cancel(tid
);
1625 (void) close(pipefd
[0]);
1626 (void) pthread_join(tid
, NULL
);
1629 if (sdd
.cleanup_fd
!= -1) {
1630 VERIFY(0 == close(sdd
.cleanup_fd
));
1631 sdd
.cleanup_fd
= -1;
1634 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1637 * write final end record. NB: want to do this even if
1638 * there was some error, because it might not be totally
1641 dmu_replay_record_t drr
= { 0 };
1642 drr
.drr_type
= DRR_END
;
1643 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1644 return (zfs_standard_error(zhp
->zfs_hdl
,
1649 return (err
|| sdd
.err
);
1652 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1654 fsavl_destroy(fsavl
);
1656 fnvlist_free(sdd
.snapholds
);
1658 if (sdd
.cleanup_fd
!= -1)
1659 VERIFY(0 == close(sdd
.cleanup_fd
));
1661 (void) pthread_cancel(tid
);
1662 (void) close(pipefd
[0]);
1663 (void) pthread_join(tid
, NULL
);
1669 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
1670 enum lzc_send_flags flags
)
1673 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1676 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1677 "warning: cannot send '%s'"), zhp
->zfs_name
);
1679 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
1683 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1684 "not an earlier snapshot from the same fs"));
1685 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1689 if (lzc_exists(zhp
->zfs_name
)) {
1690 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1691 "incremental source (%s) does not exist"),
1694 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1697 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1698 "target is busy; if a filesystem, "
1699 "it must not be mounted"));
1700 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
1713 zfs_error_aux(hdl
, strerror(errno
));
1714 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1717 return (zfs_standard_error(hdl
, errno
, errbuf
));
1724 * Routines specific to "zfs recv"
1728 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1729 boolean_t byteswap
, zio_cksum_t
*zc
)
1735 assert(ilen
<= SPA_MAXBLOCKSIZE
);
1738 rv
= read(fd
, cp
, len
);
1743 if (rv
< 0 || len
!= 0) {
1744 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1745 "failed to read from stream"));
1746 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
1747 "cannot receive")));
1752 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
1754 fletcher_4_incremental_native(buf
, ilen
, zc
);
1760 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
1761 boolean_t byteswap
, zio_cksum_t
*zc
)
1766 buf
= zfs_alloc(hdl
, len
);
1770 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
1776 err
= nvlist_unpack(buf
, len
, nvp
, 0);
1779 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
1780 "stream (malformed nvlist)"));
1787 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
1788 int baselen
, char *newname
, recvflags_t
*flags
)
1791 zfs_cmd_t zc
= {"\0"};
1793 prop_changelist_t
*clp
;
1796 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1799 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1800 flags
->force
? MS_FORCE
: 0);
1804 err
= changelist_prefix(clp
);
1808 zc
.zc_objset_type
= DMU_OST_ZFS
;
1809 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1812 (void) strcpy(newname
, tryname
);
1814 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
1816 if (flags
->verbose
) {
1817 (void) printf("attempting rename %s to %s\n",
1818 zc
.zc_name
, zc
.zc_value
);
1820 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1822 changelist_rename(clp
, name
, tryname
);
1827 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
1830 (void) snprintf(newname
, ZFS_MAXNAMELEN
, "%.*srecv-%u-%u",
1831 baselen
, name
, getpid(), seq
);
1832 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
1834 if (flags
->verbose
) {
1835 (void) printf("failed - trying rename %s to %s\n",
1836 zc
.zc_name
, zc
.zc_value
);
1838 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1840 changelist_rename(clp
, name
, newname
);
1841 if (err
&& flags
->verbose
) {
1842 (void) printf("failed (%u) - "
1843 "will try again on next pass\n", errno
);
1846 } else if (flags
->verbose
) {
1848 (void) printf("success\n");
1850 (void) printf("failed (%u)\n", errno
);
1853 (void) changelist_postfix(clp
);
1854 changelist_free(clp
);
1860 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
1861 char *newname
, recvflags_t
*flags
)
1863 zfs_cmd_t zc
= {"\0"};
1865 prop_changelist_t
*clp
;
1867 boolean_t defer
= B_FALSE
;
1870 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1873 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1874 flags
->force
? MS_FORCE
: 0);
1875 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
1876 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1877 spa_version
>= SPA_VERSION_USERREFS
)
1882 err
= changelist_prefix(clp
);
1886 zc
.zc_objset_type
= DMU_OST_ZFS
;
1887 zc
.zc_defer_destroy
= defer
;
1888 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1891 (void) printf("attempting destroy %s\n", zc
.zc_name
);
1892 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
1895 (void) printf("success\n");
1896 changelist_remove(clp
, zc
.zc_name
);
1899 (void) changelist_postfix(clp
);
1900 changelist_free(clp
);
1903 * Deferred destroy might destroy the snapshot or only mark it to be
1904 * destroyed later, and it returns success in either case.
1906 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
1907 ZFS_TYPE_SNAPSHOT
))) {
1908 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
1914 typedef struct guid_to_name_data
{
1918 } guid_to_name_data_t
;
1921 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
1923 guid_to_name_data_t
*gtnd
= arg
;
1926 if (gtnd
->skip
!= NULL
&&
1927 strcmp(zhp
->zfs_name
, gtnd
->skip
) == 0) {
1931 if (zhp
->zfs_dmustats
.dds_guid
== gtnd
->guid
) {
1932 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
1937 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
1943 * Attempt to find the local dataset associated with this guid. In the case of
1944 * multiple matches, we attempt to find the "best" match by searching
1945 * progressively larger portions of the hierarchy. This allows one to send a
1946 * tree of datasets individually and guarantee that we will find the source
1947 * guid within that hierarchy, even if there are multiple matches elsewhere.
1950 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
1953 /* exhaustive search all local snapshots */
1954 char pname
[ZFS_MAXNAMELEN
];
1955 guid_to_name_data_t gtnd
;
1964 (void) strlcpy(pname
, parent
, sizeof (pname
));
1967 * Search progressively larger portions of the hierarchy. This will
1968 * select the "most local" version of the origin snapshot in the case
1969 * that there are multiple matching snapshots in the system.
1971 while ((cp
= strrchr(pname
, '/')) != NULL
) {
1973 /* Chop off the last component and open the parent */
1975 zhp
= make_dataset_handle(hdl
, pname
);
1980 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
1986 * Remember the dataset that we already searched, so we
1987 * skip it next time through.
1996 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1997 * guid1 is after guid2.
2000 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
2001 uint64_t guid1
, uint64_t guid2
)
2004 char *fsname
= NULL
, *snapname
= NULL
;
2005 char buf
[ZFS_MAXNAMELEN
];
2007 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2008 uint64_t create1
, create2
;
2015 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2016 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2017 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2018 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2019 if (guid1hdl
== NULL
)
2022 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2023 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2024 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2025 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2026 if (guid2hdl
== NULL
) {
2027 zfs_close(guid1hdl
);
2031 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2032 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2034 if (create1
< create2
)
2036 else if (create1
> create2
)
2041 zfs_close(guid1hdl
);
2042 zfs_close(guid2hdl
);
2048 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2049 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2052 nvlist_t
*local_nv
, *deleted
= NULL
;
2053 avl_tree_t
*local_avl
;
2054 nvpair_t
*fselem
, *nextfselem
;
2056 char newname
[ZFS_MAXNAMELEN
];
2059 boolean_t needagain
, progress
, recursive
;
2062 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2064 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2071 needagain
= progress
= B_FALSE
;
2073 VERIFY(0 == nvlist_alloc(&deleted
, NV_UNIQUE_NAME
, 0));
2075 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2076 recursive
, &local_nv
, &local_avl
)) != 0)
2080 * Process deletes and renames
2082 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2083 fselem
; fselem
= nextfselem
) {
2084 nvlist_t
*nvfs
, *snaps
;
2085 nvlist_t
*stream_nvfs
= NULL
;
2086 nvpair_t
*snapelem
, *nextsnapelem
;
2087 uint64_t fromguid
= 0;
2088 uint64_t originguid
= 0;
2089 uint64_t stream_originguid
= 0;
2090 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2091 char *fsname
, *stream_fsname
;
2093 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2095 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2096 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2097 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2098 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2099 &parent_fromsnap_guid
));
2100 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2103 * First find the stream's fs, so we can check for
2104 * a different origin (due to "zfs promote")
2106 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2107 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2110 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2111 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2113 if (stream_nvfs
!= NULL
)
2117 /* check for promote */
2118 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2119 &stream_originguid
);
2120 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2121 switch (created_before(hdl
, local_avl
,
2122 stream_originguid
, originguid
)) {
2125 zfs_cmd_t zc
= {"\0"};
2126 nvlist_t
*origin_nvfs
;
2127 char *origin_fsname
;
2130 (void) printf("promoting %s\n", fsname
);
2132 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2134 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2135 "name", &origin_fsname
));
2136 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2137 sizeof (zc
.zc_value
));
2138 (void) strlcpy(zc
.zc_name
, fsname
,
2139 sizeof (zc
.zc_name
));
2140 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2148 fsavl_destroy(local_avl
);
2149 nvlist_free(local_nv
);
2153 * We had/have the wrong origin, therefore our
2154 * list of snapshots is wrong. Need to handle
2155 * them on the next pass.
2161 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2162 snapelem
; snapelem
= nextsnapelem
) {
2164 char *stream_snapname
;
2165 nvlist_t
*found
, *props
;
2167 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2169 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2170 found
= fsavl_find(stream_avl
, thisguid
,
2173 /* check for delete */
2174 if (found
== NULL
) {
2175 char name
[ZFS_MAXNAMELEN
];
2180 (void) snprintf(name
, sizeof (name
), "%s@%s",
2181 fsname
, nvpair_name(snapelem
));
2183 error
= recv_destroy(hdl
, name
,
2184 strlen(fsname
)+1, newname
, flags
);
2189 sprintf(guidname
, "%llu",
2190 (u_longlong_t
)thisguid
);
2191 nvlist_add_boolean(deleted
, guidname
);
2195 stream_nvfs
= found
;
2197 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2198 &props
) && 0 == nvlist_lookup_nvlist(props
,
2199 stream_snapname
, &props
)) {
2200 zfs_cmd_t zc
= {"\0"};
2202 zc
.zc_cookie
= B_TRUE
; /* received */
2203 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2204 "%s@%s", fsname
, nvpair_name(snapelem
));
2205 if (zcmd_write_src_nvlist(hdl
, &zc
,
2207 (void) zfs_ioctl(hdl
,
2208 ZFS_IOC_SET_PROP
, &zc
);
2209 zcmd_free_nvlists(&zc
);
2213 /* check for different snapname */
2214 if (strcmp(nvpair_name(snapelem
),
2215 stream_snapname
) != 0) {
2216 char name
[ZFS_MAXNAMELEN
];
2217 char tryname
[ZFS_MAXNAMELEN
];
2219 (void) snprintf(name
, sizeof (name
), "%s@%s",
2220 fsname
, nvpair_name(snapelem
));
2221 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2222 fsname
, stream_snapname
);
2224 error
= recv_rename(hdl
, name
, tryname
,
2225 strlen(fsname
)+1, newname
, flags
);
2232 if (strcmp(stream_snapname
, fromsnap
) == 0)
2233 fromguid
= thisguid
;
2236 /* check for delete */
2237 if (stream_nvfs
== NULL
) {
2241 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2247 sprintf(guidname
, "%llu",
2248 (u_longlong_t
) parent_fromsnap_guid
);
2249 nvlist_add_boolean(deleted
, guidname
);
2253 if (fromguid
== 0) {
2254 if (flags
->verbose
) {
2255 (void) printf("local fs %s does not have "
2256 "fromsnap (%s in stream); must have "
2257 "been deleted locally; ignoring\n",
2263 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2264 "name", &stream_fsname
));
2265 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2266 "parentfromsnap", &stream_parent_fromsnap_guid
));
2268 s1
= strrchr(fsname
, '/');
2269 s2
= strrchr(stream_fsname
, '/');
2272 * Check if we're going to rename based on parent guid change
2273 * and the current parent guid was also deleted. If it was then
2274 * rename will fail and is likely unneeded, so avoid this and
2275 * force an early retry to determine the new
2276 * parent_fromsnap_guid.
2278 if (stream_parent_fromsnap_guid
!= 0 &&
2279 parent_fromsnap_guid
!= 0 &&
2280 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
2281 sprintf(guidname
, "%llu",
2282 (u_longlong_t
) parent_fromsnap_guid
);
2283 if (nvlist_exists(deleted
, guidname
)) {
2291 * Check for rename. If the exact receive path is specified, it
2292 * does not count as a rename, but we still need to check the
2293 * datasets beneath it.
2295 if ((stream_parent_fromsnap_guid
!= 0 &&
2296 parent_fromsnap_guid
!= 0 &&
2297 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2298 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2299 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2301 char tryname
[ZFS_MAXNAMELEN
];
2303 parent
= fsavl_find(local_avl
,
2304 stream_parent_fromsnap_guid
, NULL
);
2306 * NB: parent might not be found if we used the
2307 * tosnap for stream_parent_fromsnap_guid,
2308 * because the parent is a newly-created fs;
2309 * we'll be able to rename it after we recv the
2312 if (parent
!= NULL
) {
2315 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2317 (void) snprintf(tryname
, sizeof (tryname
),
2318 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2321 if (flags
->verbose
) {
2322 (void) printf("local fs %s new parent "
2323 "not found\n", fsname
);
2329 error
= recv_rename(hdl
, fsname
, tryname
,
2330 strlen(tofs
)+1, newname
, flags
);
2332 if (renamed
!= NULL
&& newname
[0] != '\0') {
2333 VERIFY(0 == nvlist_add_boolean(renamed
,
2345 fsavl_destroy(local_avl
);
2346 nvlist_free(local_nv
);
2347 nvlist_free(deleted
);
2349 if (needagain
&& progress
) {
2350 /* do another pass to fix up temporary names */
2352 (void) printf("another pass:\n");
2360 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2361 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2362 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2364 nvlist_t
*stream_nv
= NULL
;
2365 avl_tree_t
*stream_avl
= NULL
;
2366 char *fromsnap
= NULL
;
2368 char tofs
[ZFS_MAXNAMELEN
];
2369 char sendfs
[ZFS_MAXNAMELEN
];
2371 dmu_replay_record_t drre
;
2373 boolean_t anyerr
= B_FALSE
;
2374 boolean_t softerr
= B_FALSE
;
2375 boolean_t recursive
;
2377 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2380 assert(drr
->drr_type
== DRR_BEGIN
);
2381 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2382 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2383 DMU_COMPOUNDSTREAM
);
2386 * Read in the nvlist from the stream.
2388 if (drr
->drr_payloadlen
!= 0) {
2389 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2390 &stream_nv
, flags
->byteswap
, zc
);
2392 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2397 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2400 if (recursive
&& strchr(destname
, '@')) {
2401 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2402 "cannot specify snapshot name for multi-snapshot stream"));
2403 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2408 * Read in the end record and verify checksum.
2410 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2411 flags
->byteswap
, NULL
)))
2413 if (flags
->byteswap
) {
2414 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2415 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2416 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2417 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2418 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2419 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2420 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2421 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2422 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2424 if (drre
.drr_type
!= DRR_END
) {
2425 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2428 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2429 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2430 "incorrect header checksum"));
2431 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2435 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2437 if (drr
->drr_payloadlen
!= 0) {
2438 nvlist_t
*stream_fss
;
2440 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2442 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2443 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2444 "couldn't allocate avl tree"));
2445 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2449 if (fromsnap
!= NULL
) {
2450 nvlist_t
*renamed
= NULL
;
2451 nvpair_t
*pair
= NULL
;
2453 (void) strlcpy(tofs
, destname
, ZFS_MAXNAMELEN
);
2454 if (flags
->isprefix
) {
2455 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2458 if (flags
->istail
) {
2459 cp
= strrchr(drrb
->drr_toname
, '/');
2461 (void) strlcat(tofs
, "/",
2465 i
= (cp
- drrb
->drr_toname
);
2468 i
= strcspn(drrb
->drr_toname
, "/@");
2470 /* zfs_receive_one() will create_parents() */
2471 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2473 *strchr(tofs
, '@') = '\0';
2476 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2477 VERIFY(0 == nvlist_alloc(&renamed
,
2478 NV_UNIQUE_NAME
, 0));
2481 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2482 stream_nv
, stream_avl
, renamed
);
2484 /* Unmount renamed filesystems before receiving. */
2485 while ((pair
= nvlist_next_nvpair(renamed
,
2488 prop_changelist_t
*clp
= NULL
;
2490 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2491 ZFS_TYPE_FILESYSTEM
);
2493 clp
= changelist_gather(zhp
,
2494 ZFS_PROP_MOUNTPOINT
, 0, 0);
2498 changelist_prefix(clp
);
2499 changelist_free(clp
);
2504 nvlist_free(renamed
);
2509 * Get the fs specified by the first path in the stream (the top level
2510 * specified by 'zfs send') and pass it to each invocation of
2511 * zfs_receive_one().
2513 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2515 if ((cp
= strchr(sendfs
, '@')) != NULL
)
2518 /* Finally, receive each contained stream */
2521 * we should figure out if it has a recoverable
2522 * error, in which case do a recv_skip() and drive on.
2523 * Note, if we fail due to already having this guid,
2524 * zfs_receive_one() will take care of it (ie,
2525 * recv_skip() and return 0).
2527 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
2528 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2530 if (error
== ENODATA
) {
2535 } while (error
== 0);
2537 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2539 * Now that we have the fs's they sent us, try the
2542 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2543 stream_nv
, stream_avl
, NULL
);
2547 fsavl_destroy(stream_avl
);
2549 nvlist_free(stream_nv
);
2558 trunc_prop_errs(int truncated
)
2560 ASSERT(truncated
!= 0);
2563 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2564 "1 more property could not be set\n"));
2566 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2567 "%d more properties could not be set\n"), truncated
);
2571 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2573 dmu_replay_record_t
*drr
;
2574 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2577 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2578 "cannot receive:"));
2580 /* XXX would be great to use lseek if possible... */
2583 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2584 byteswap
, NULL
) == 0) {
2586 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2588 switch (drr
->drr_type
) {
2590 /* NB: not to be used on v2 stream packages */
2591 if (drr
->drr_payloadlen
!= 0) {
2592 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2593 "invalid substream header"));
2594 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2604 drr
->drr_u
.drr_object
.drr_bonuslen
=
2605 BSWAP_32(drr
->drr_u
.drr_object
.
2608 (void) recv_read(hdl
, fd
, buf
,
2609 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2615 drr
->drr_u
.drr_write
.drr_length
=
2616 BSWAP_64(drr
->drr_u
.drr_write
.drr_length
);
2618 (void) recv_read(hdl
, fd
, buf
,
2619 drr
->drr_u
.drr_write
.drr_length
, B_FALSE
, NULL
);
2623 drr
->drr_u
.drr_write
.drr_length
=
2624 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2626 (void) recv_read(hdl
, fd
, buf
,
2627 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2629 case DRR_WRITE_EMBEDDED
:
2631 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2632 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2635 (void) recv_read(hdl
, fd
, buf
,
2636 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2639 case DRR_WRITE_BYREF
:
2640 case DRR_FREEOBJECTS
:
2645 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2646 "invalid record type"));
2647 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2656 * Restores a backup of tosnap from the file descriptor specified by infd.
2659 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2660 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
2661 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
2662 avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2663 uint64_t *action_handlep
)
2665 zfs_cmd_t zc
= {"\0"};
2667 int ioctl_err
, ioctl_errno
, err
;
2669 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2671 char prop_errbuf
[1024];
2672 const char *chopprefix
;
2673 boolean_t newfs
= B_FALSE
;
2674 boolean_t stream_wantsnewfs
;
2675 uint64_t parent_snapguid
= 0;
2676 prop_changelist_t
*clp
= NULL
;
2677 nvlist_t
*snapprops_nvlist
= NULL
;
2678 zprop_errflags_t prop_errflags
;
2679 boolean_t recursive
;
2681 begin_time
= time(NULL
);
2683 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2686 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2689 if (stream_avl
!= NULL
) {
2691 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
2696 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
2698 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
2700 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
2702 if (flags
->canmountoff
) {
2703 VERIFY(0 == nvlist_add_uint64(props
,
2704 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
2706 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
2716 * Determine how much of the snapshot name stored in the stream
2717 * we are going to tack on to the name they specified on the
2718 * command line, and how much we are going to chop off.
2720 * If they specified a snapshot, chop the entire name stored in
2723 if (flags
->istail
) {
2725 * A filesystem was specified with -e. We want to tack on only
2726 * the tail of the sent snapshot path.
2728 if (strchr(tosnap
, '@')) {
2729 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2730 "argument - snapshot not allowed with -e"));
2731 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2734 chopprefix
= strrchr(sendfs
, '/');
2736 if (chopprefix
== NULL
) {
2738 * The tail is the poolname, so we need to
2739 * prepend a path separator.
2741 int len
= strlen(drrb
->drr_toname
);
2742 cp
= malloc(len
+ 2);
2744 (void) strcpy(&cp
[1], drrb
->drr_toname
);
2747 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
2749 } else if (flags
->isprefix
) {
2751 * A filesystem was specified with -d. We want to tack on
2752 * everything but the first element of the sent snapshot path
2753 * (all but the pool name).
2755 if (strchr(tosnap
, '@')) {
2756 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2757 "argument - snapshot not allowed with -d"));
2758 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2761 chopprefix
= strchr(drrb
->drr_toname
, '/');
2762 if (chopprefix
== NULL
)
2763 chopprefix
= strchr(drrb
->drr_toname
, '@');
2764 } else if (strchr(tosnap
, '@') == NULL
) {
2766 * If a filesystem was specified without -d or -e, we want to
2767 * tack on everything after the fs specified by 'zfs send'.
2769 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
2771 /* A snapshot was specified as an exact path (no -d or -e). */
2773 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2774 "cannot specify snapshot name for multi-snapshot "
2776 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2778 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
2781 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
2782 ASSERT(chopprefix
> drrb
->drr_toname
);
2783 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
2784 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
2785 chopprefix
[0] == '\0');
2788 * Determine name of destination snapshot, store in zc_value.
2790 (void) strcpy(zc
.zc_value
, tosnap
);
2791 (void) strlcat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
2793 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
2794 zcmd_free_nvlists(&zc
);
2795 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2799 * Determine the name of the origin snapshot, store in zc_string.
2801 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
2802 if (guid_to_name(hdl
, zc
.zc_value
,
2803 drrb
->drr_fromguid
, zc
.zc_string
) != 0) {
2804 zcmd_free_nvlists(&zc
);
2805 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2806 "local origin for clone %s does not exist"),
2808 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2811 (void) printf("found clone origin %s\n", zc
.zc_string
);
2812 } else if (originsnap
) {
2813 (void) strncpy(zc
.zc_string
, originsnap
, ZFS_MAXNAMELEN
);
2815 (void) printf("using provided clone origin %s\n",
2819 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
2820 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
);
2822 if (stream_wantsnewfs
) {
2824 * if the parent fs does not exist, look for it based on
2825 * the parent snap GUID
2827 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2828 "cannot receive new filesystem stream"));
2830 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2831 cp
= strrchr(zc
.zc_name
, '/');
2835 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2836 char suffix
[ZFS_MAXNAMELEN
];
2837 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
2838 if (guid_to_name(hdl
, zc
.zc_name
, parent_snapguid
,
2839 zc
.zc_value
) == 0) {
2840 *strchr(zc
.zc_value
, '@') = '\0';
2841 (void) strcat(zc
.zc_value
, suffix
);
2846 * if the fs does not exist, look for it based on the
2849 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2850 "cannot receive incremental stream"));
2852 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2853 *strchr(zc
.zc_name
, '@') = '\0';
2856 * If the exact receive path was specified and this is the
2857 * topmost path in the stream, then if the fs does not exist we
2858 * should look no further.
2860 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
2861 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
2862 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2863 char snap
[ZFS_MAXNAMELEN
];
2864 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
2865 if (guid_to_name(hdl
, zc
.zc_name
, drrb
->drr_fromguid
,
2866 zc
.zc_value
) == 0) {
2867 *strchr(zc
.zc_value
, '@') = '\0';
2868 (void) strcat(zc
.zc_value
, snap
);
2873 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2874 *strchr(zc
.zc_name
, '@') = '\0';
2876 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2880 * Destination fs exists. Therefore this should either
2881 * be an incremental, or the stream specifies a new fs
2882 * (full stream or clone) and they want us to blow it
2883 * away (and have therefore specified -F and removed any
2886 if (stream_wantsnewfs
) {
2887 if (!flags
->force
) {
2888 zcmd_free_nvlists(&zc
);
2889 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2890 "destination '%s' exists\n"
2891 "must specify -F to overwrite it"),
2893 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2895 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2897 zcmd_free_nvlists(&zc
);
2898 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2899 "destination has snapshots (eg. %s)\n"
2900 "must destroy them to overwrite it"),
2902 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2906 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
2907 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
2908 zcmd_free_nvlists(&zc
);
2912 if (stream_wantsnewfs
&&
2913 zhp
->zfs_dmustats
.dds_origin
[0]) {
2914 zcmd_free_nvlists(&zc
);
2916 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2917 "destination '%s' is a clone\n"
2918 "must destroy it to overwrite it"),
2920 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2923 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
2924 stream_wantsnewfs
) {
2925 /* We can't do online recv in this case */
2926 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
2929 zcmd_free_nvlists(&zc
);
2932 if (changelist_prefix(clp
) != 0) {
2933 changelist_free(clp
);
2935 zcmd_free_nvlists(&zc
);
2942 * Destination filesystem does not exist. Therefore we better
2943 * be creating a new filesystem (either from a full backup, or
2944 * a clone). It would therefore be invalid if the user
2945 * specified only the pool name (i.e. if the destination name
2946 * contained no slash character).
2948 if (!stream_wantsnewfs
||
2949 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
2950 zcmd_free_nvlists(&zc
);
2951 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2952 "destination '%s' does not exist"), zc
.zc_name
);
2953 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2957 * Trim off the final dataset component so we perform the
2958 * recvbackup ioctl to the filesystems's parent.
2962 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
2963 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
2964 zcmd_free_nvlists(&zc
);
2965 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
2971 zc
.zc_begin_record
= drr_noswap
->drr_u
.drr_begin
;
2972 zc
.zc_cookie
= infd
;
2973 zc
.zc_guid
= flags
->force
;
2974 if (flags
->verbose
) {
2975 (void) printf("%s %s stream of %s into %s\n",
2976 flags
->dryrun
? "would receive" : "receiving",
2977 drrb
->drr_fromguid
? "incremental" : "full",
2978 drrb
->drr_toname
, zc
.zc_value
);
2979 (void) fflush(stdout
);
2982 if (flags
->dryrun
) {
2983 zcmd_free_nvlists(&zc
);
2984 return (recv_skip(hdl
, infd
, flags
->byteswap
));
2987 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
2988 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
2989 zc
.zc_cleanup_fd
= cleanup_fd
;
2990 zc
.zc_action_handle
= *action_handlep
;
2992 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
2993 ioctl_errno
= errno
;
2994 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
2997 nvlist_t
*prop_errors
;
2998 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
2999 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
3001 nvpair_t
*prop_err
= NULL
;
3003 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
3004 prop_err
)) != NULL
) {
3009 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
3010 (void) nvpair_value_int32(prop_err
, &intval
);
3011 if (strcmp(nvpair_name(prop_err
),
3012 ZPROP_N_MORE_ERRORS
) == 0) {
3013 trunc_prop_errs(intval
);
3016 (void) snprintf(tbuf
, sizeof (tbuf
),
3017 dgettext(TEXT_DOMAIN
,
3018 "cannot receive %s property on %s"),
3019 nvpair_name(prop_err
), zc
.zc_name
);
3020 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3023 nvlist_free(prop_errors
);
3026 zc
.zc_nvlist_dst
= 0;
3027 zc
.zc_nvlist_dst_size
= 0;
3028 zcmd_free_nvlists(&zc
);
3030 if (err
== 0 && snapprops_nvlist
) {
3031 zfs_cmd_t zc2
= {"\0"};
3033 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
3034 zc2
.zc_cookie
= B_TRUE
; /* received */
3035 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
3036 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
3037 zcmd_free_nvlists(&zc2
);
3041 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3043 * It may be that this snapshot already exists,
3044 * in which case we want to consume & ignore it
3045 * rather than failing.
3047 avl_tree_t
*local_avl
;
3048 nvlist_t
*local_nv
, *fs
;
3049 cp
= strchr(zc
.zc_value
, '@');
3052 * XXX Do this faster by just iterating over snaps in
3053 * this fs. Also if zc_value does not exist, we will
3054 * get a strange "does not exist" error message.
3057 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
3058 &local_nv
, &local_avl
) == 0) {
3060 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3061 fsavl_destroy(local_avl
);
3062 nvlist_free(local_nv
);
3065 if (flags
->verbose
) {
3066 (void) printf("snap %s already exists; "
3067 "ignoring\n", zc
.zc_value
);
3069 err
= ioctl_err
= recv_skip(hdl
, infd
,
3076 if (ioctl_err
!= 0) {
3077 switch (ioctl_errno
) {
3079 cp
= strchr(zc
.zc_value
, '@');
3081 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3082 "most recent snapshot of %s does not\n"
3083 "match incremental source"), zc
.zc_value
);
3084 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3088 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3089 "destination %s has been modified\n"
3090 "since most recent snapshot"), zc
.zc_name
);
3091 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3094 cp
= strchr(zc
.zc_value
, '@');
3096 /* it's the containing fs that exists */
3099 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3100 "destination already exists"));
3101 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3102 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3107 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3110 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3111 "invalid stream (checksum mismatch)"));
3112 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3115 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3116 "pool must be upgraded to receive this stream."));
3117 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3120 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3121 "destination %s space quota exceeded"), zc
.zc_name
);
3122 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3125 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3130 * Mount the target filesystem (if created). Also mount any
3131 * children of the target filesystem if we did a replication
3132 * receive (indicated by stream_avl being non-NULL).
3134 cp
= strchr(zc
.zc_value
, '@');
3135 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3139 h
= zfs_open(hdl
, zc
.zc_value
,
3140 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3142 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3144 } else if (newfs
|| stream_avl
) {
3146 * Track the first/top of hierarchy fs,
3147 * for mounting and sharing later.
3149 if (top_zfs
&& *top_zfs
== NULL
)
3150 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
3158 err
|= changelist_postfix(clp
);
3159 changelist_free(clp
);
3162 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3163 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3164 "failed to clear unreceived properties on %s"),
3166 (void) fprintf(stderr
, "\n");
3168 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3169 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3170 "failed to restore original properties on %s"),
3172 (void) fprintf(stderr
, "\n");
3175 if (err
|| ioctl_err
)
3178 *action_handlep
= zc
.zc_action_handle
;
3180 if (flags
->verbose
) {
3183 uint64_t bytes
= zc
.zc_cookie
;
3184 time_t delta
= time(NULL
) - begin_time
;
3187 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3188 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3190 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3198 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
3199 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
3200 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3201 uint64_t *action_handlep
)
3204 dmu_replay_record_t drr
, drr_noswap
;
3205 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3207 zio_cksum_t zcksum
= { { 0 } };
3208 uint64_t featureflags
;
3211 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3214 if (flags
->isprefix
&&
3215 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3216 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3217 "(%s) does not exist"), tosnap
);
3218 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3221 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
3222 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
3223 "(%s) does not exist"), originsnap
);
3224 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3227 /* read in the BEGIN record */
3228 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3232 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3233 /* It's the double end record at the end of a package */
3237 /* the kernel needs the non-byteswapped begin record */
3240 flags
->byteswap
= B_FALSE
;
3241 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3243 * We computed the checksum in the wrong byteorder in
3244 * recv_read() above; do it again correctly.
3246 bzero(&zcksum
, sizeof (zio_cksum_t
));
3247 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3248 flags
->byteswap
= B_TRUE
;
3250 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3251 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3252 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3253 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3254 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3255 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3256 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3257 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3258 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3261 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3262 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3263 "stream (bad magic number)"));
3264 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3267 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3268 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3270 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3271 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3272 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3273 "stream has unsupported feature, feature flags = %lx"),
3275 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3278 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3279 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3280 "stream (bad snapshot name)"));
3281 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3284 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3285 char nonpackage_sendfs
[ZFS_MAXNAMELEN
];
3286 if (sendfs
== NULL
) {
3288 * We were not called from zfs_receive_package(). Get
3289 * the fs specified by 'zfs send'.
3292 (void) strlcpy(nonpackage_sendfs
,
3293 drr
.drr_u
.drr_begin
.drr_toname
, ZFS_MAXNAMELEN
);
3294 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3296 sendfs
= nonpackage_sendfs
;
3298 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
3299 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
3300 cleanup_fd
, action_handlep
));
3302 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3303 DMU_COMPOUNDSTREAM
);
3304 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
3305 &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3310 * Restores a backup of tosnap from the file descriptor specified by infd.
3311 * Return 0 on total success, -2 if some things couldn't be
3312 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3313 * (-1 will override -2).
3316 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
3317 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
3319 char *top_zfs
= NULL
;
3322 uint64_t action_handle
= 0;
3324 char *originsnap
= NULL
;
3327 * The only way fstat can fail is if we do not have a valid file
3330 if (fstat(infd
, &sb
) == -1) {
3336 #ifndef F_SETPIPE_SZ
3337 #define F_SETPIPE_SZ (F_SETLEASE + 7)
3338 #endif /* F_SETPIPE_SZ */
3340 #ifndef F_GETPIPE_SZ
3341 #define F_GETPIPE_SZ (F_GETLEASE + 7)
3342 #endif /* F_GETPIPE_SZ */
3345 * It is not uncommon for gigabytes to be processed in zfs receive.
3346 * Speculatively increase the buffer size via Linux-specific fcntl()
3349 if (S_ISFIFO(sb
.st_mode
)) {
3350 FILE *procf
= fopen("/proc/sys/fs/pipe-max-size", "r");
3352 if (procf
!= NULL
) {
3353 unsigned long max_psize
;
3355 if (fscanf(procf
, "%lu", &max_psize
) > 0) {
3356 cur_psize
= fcntl(infd
, F_GETPIPE_SZ
);
3357 if (cur_psize
> 0 &&
3358 max_psize
> (unsigned long) cur_psize
)
3359 (void) fcntl(infd
, F_SETPIPE_SZ
,
3365 #endif /* __linux__ */
3368 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
3369 if (err
&& err
!= ENOENT
)
3373 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3374 VERIFY(cleanup_fd
>= 0);
3376 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
3377 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
);
3379 VERIFY(0 == close(cleanup_fd
));
3381 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3383 prop_changelist_t
*clp
;
3385 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3387 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3388 CL_GATHER_MOUNT_ALWAYS
, 0);
3391 /* mount and share received datasets */
3392 err
= changelist_postfix(clp
);
3393 changelist_free(clp
);
3396 if (zhp
== NULL
|| clp
== NULL
|| err
)