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) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
42 #include <sys/mount.h>
43 #include <sys/mntent.h>
44 #include <sys/mnttab.h>
46 #include <sys/debug.h>
54 #include <libzfs_core.h>
56 #include "zfs_namecheck.h"
58 #include "zfs_fletcher.h"
59 #include "libzfs_impl.h"
61 #include <sys/zio_checksum.h>
63 #include <sys/socket.h>
65 /* in libzfs_dataset.c */
66 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
68 static int zfs_receive_impl(libzfs_handle_t
*, const char *, const char *,
69 recvflags_t
*, int, const char *, nvlist_t
*, avl_tree_t
*, char **, int,
70 uint64_t *, const char *);
71 static int guid_to_name(libzfs_handle_t
*, const char *,
72 uint64_t, boolean_t
, char *);
74 static const zio_cksum_t zero_cksum
= { { 0 } };
76 typedef struct dedup_arg
{
79 libzfs_handle_t
*dedup_hdl
;
82 typedef struct progress_arg
{
85 boolean_t pa_parsable
;
88 typedef struct dataref
{
94 typedef struct dedup_entry
{
95 struct dedup_entry
*dde_next
;
96 zio_cksum_t dde_chksum
;
101 #define MAX_DDT_PHYSMEM_PERCENT 20
102 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
104 typedef struct dedup_table
{
105 dedup_entry_t
**dedup_hash_array
;
106 umem_cache_t
*ddecache
;
107 uint64_t max_ddt_size
; /* max dedup table size in bytes */
108 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
115 high_order_bit(uint64_t n
)
119 for (count
= 0; n
!= 0; count
++)
125 ssread(void *buf
, size_t len
, FILE *stream
)
129 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
136 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
137 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
141 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
142 if (ddt
->ddt_full
== B_FALSE
) {
143 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
144 "Dedup table full. Deduplication will continue "
145 "with existing table entries"));
146 ddt
->ddt_full
= B_TRUE
;
151 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
153 assert(*ddepp
== NULL
);
154 dde
->dde_next
= NULL
;
155 dde
->dde_chksum
= *cs
;
156 dde
->dde_prop
= prop
;
159 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
165 * Using the specified dedup table, do a lookup for an entry with
166 * the checksum cs. If found, return the block's reference info
167 * in *dr. Otherwise, insert a new entry in the dedup table, using
168 * the reference information specified by *dr.
170 * return value: true - entry was found
171 * false - entry was not found
174 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
175 uint64_t prop
, dataref_t
*dr
)
178 dedup_entry_t
**ddepp
;
180 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
182 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
183 ddepp
= &((*ddepp
)->dde_next
)) {
184 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
185 (*ddepp
)->dde_prop
== prop
) {
186 *dr
= (*ddepp
)->dde_ref
;
190 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
195 dump_record(dmu_replay_record_t
*drr
, void *payload
, int payload_len
,
196 zio_cksum_t
*zc
, int outfd
)
198 ASSERT3U(offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
),
199 ==, sizeof (dmu_replay_record_t
) - sizeof (zio_cksum_t
));
200 fletcher_4_incremental_native(drr
,
201 offsetof(dmu_replay_record_t
, drr_u
.drr_checksum
.drr_checksum
), zc
);
202 if (drr
->drr_type
!= DRR_BEGIN
) {
203 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr
->drr_u
.
204 drr_checksum
.drr_checksum
));
205 drr
->drr_u
.drr_checksum
.drr_checksum
= *zc
;
207 fletcher_4_incremental_native(&drr
->drr_u
.drr_checksum
.drr_checksum
,
208 sizeof (zio_cksum_t
), zc
);
209 if (write(outfd
, drr
, sizeof (*drr
)) == -1)
211 if (payload_len
!= 0) {
212 fletcher_4_incremental_native(payload
, payload_len
, zc
);
213 if (write(outfd
, payload
, payload_len
) == -1)
220 * This function is started in a separate thread when the dedup option
221 * has been requested. The main send thread determines the list of
222 * snapshots to be included in the send stream and makes the ioctl calls
223 * for each one. But instead of having the ioctl send the output to the
224 * the output fd specified by the caller of zfs_send()), the
225 * ioctl is told to direct the output to a pipe, which is read by the
226 * alternate thread running THIS function. This function does the
228 * 1. building a dedup table (the DDT)
229 * 2. doing checksums on each data block and inserting a record in the DDT
230 * 3. looking for matching checksums, and
231 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
232 * a duplicate block is found.
233 * The output of this function then goes to the output fd requested
234 * by the caller of zfs_send().
239 dedup_arg_t
*dda
= arg
;
240 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
241 dmu_replay_record_t thedrr
= { 0 };
242 dmu_replay_record_t
*drr
= &thedrr
;
246 zio_cksum_t stream_cksum
;
247 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
251 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
) / 100,
252 SMALLEST_POSSIBLE_MAX_DDT_MB
<< 20);
254 numbuckets
= ddt
.max_ddt_size
/ (sizeof (dedup_entry_t
));
257 * numbuckets must be a power of 2. Increase number to
258 * a power of 2 if necessary.
260 if (!ISP2(numbuckets
))
261 numbuckets
= 1 << high_order_bit(numbuckets
);
263 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
264 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
265 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
266 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
267 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
268 ddt
.ddt_full
= B_FALSE
;
270 outfd
= dda
->outputfd
;
271 ofp
= fdopen(dda
->inputfd
, "r");
272 while (ssread(drr
, sizeof (*drr
), ofp
) != 0) {
274 switch (drr
->drr_type
) {
277 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
280 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
282 ASSERT3U(drrb
->drr_magic
, ==, DMU_BACKUP_MAGIC
);
284 /* set the DEDUP feature flag for this stream */
285 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
286 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
287 DMU_BACKUP_FEATURE_DEDUPPROPS
);
288 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
290 if (drr
->drr_payloadlen
!= 0) {
291 sz
= drr
->drr_payloadlen
;
293 if (sz
> SPA_MAXBLOCKSIZE
) {
294 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
295 SPA_MAXBLOCKSIZE
, sz
);
297 (void) ssread(buf
, sz
, ofp
);
301 if (dump_record(drr
, buf
, sz
, &stream_cksum
,
309 struct drr_end
*drre
= &drr
->drr_u
.drr_end
;
310 /* use the recalculated checksum */
311 drre
->drr_checksum
= stream_cksum
;
312 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
320 struct drr_object
*drro
= &drr
->drr_u
.drr_object
;
321 if (drro
->drr_bonuslen
> 0) {
323 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
326 if (dump_record(drr
, buf
,
327 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
328 &stream_cksum
, outfd
) != 0)
335 struct drr_spill
*drrs
= &drr
->drr_u
.drr_spill
;
336 (void) ssread(buf
, drrs
->drr_length
, ofp
);
337 if (dump_record(drr
, buf
, drrs
->drr_length
,
338 &stream_cksum
, outfd
) != 0)
343 case DRR_FREEOBJECTS
:
345 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
353 struct drr_write
*drrw
= &drr
->drr_u
.drr_write
;
355 uint64_t payload_size
;
357 payload_size
= DRR_WRITE_PAYLOAD_SIZE(drrw
);
358 (void) ssread(buf
, payload_size
, ofp
);
361 * Use the existing checksum if it's dedup-capable,
362 * else calculate a SHA256 checksum for it.
365 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
367 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
368 zio_cksum_t tmpsha256
;
370 zio_checksum_SHA256(buf
,
371 payload_size
, &tmpsha256
);
373 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
374 BE_64(tmpsha256
.zc_word
[0]);
375 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
376 BE_64(tmpsha256
.zc_word
[1]);
377 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
378 BE_64(tmpsha256
.zc_word
[2]);
379 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
380 BE_64(tmpsha256
.zc_word
[3]);
381 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
382 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
385 dataref
.ref_guid
= drrw
->drr_toguid
;
386 dataref
.ref_object
= drrw
->drr_object
;
387 dataref
.ref_offset
= drrw
->drr_offset
;
389 if (ddt_update(dda
->dedup_hdl
, &ddt
,
390 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
392 dmu_replay_record_t wbr_drr
= {0};
393 struct drr_write_byref
*wbr_drrr
=
394 &wbr_drr
.drr_u
.drr_write_byref
;
396 /* block already present in stream */
397 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
399 wbr_drrr
->drr_object
= drrw
->drr_object
;
400 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
401 wbr_drrr
->drr_length
= drrw
->drr_logical_size
;
402 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
403 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
404 wbr_drrr
->drr_refobject
=
406 wbr_drrr
->drr_refoffset
=
409 wbr_drrr
->drr_checksumtype
=
410 drrw
->drr_checksumtype
;
411 wbr_drrr
->drr_checksumflags
=
412 drrw
->drr_checksumtype
;
413 wbr_drrr
->drr_key
.ddk_cksum
=
414 drrw
->drr_key
.ddk_cksum
;
415 wbr_drrr
->drr_key
.ddk_prop
=
416 drrw
->drr_key
.ddk_prop
;
418 if (dump_record(&wbr_drr
, NULL
, 0,
419 &stream_cksum
, outfd
) != 0)
422 /* block not previously seen */
423 if (dump_record(drr
, buf
, payload_size
,
424 &stream_cksum
, outfd
) != 0)
430 case DRR_WRITE_EMBEDDED
:
432 struct drr_write_embedded
*drrwe
=
433 &drr
->drr_u
.drr_write_embedded
;
435 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
436 if (dump_record(drr
, buf
,
437 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
438 &stream_cksum
, outfd
) != 0)
445 if (dump_record(drr
, NULL
, 0, &stream_cksum
,
452 (void) fprintf(stderr
, "INVALID record type 0x%x\n",
454 /* should never happen, so assert */
459 umem_cache_destroy(ddt
.ddecache
);
460 free(ddt
.dedup_hash_array
);
468 * Routines for dealing with the AVL tree of fs-nvlists
470 typedef struct fsavl_node
{
478 fsavl_compare(const void *arg1
, const void *arg2
)
480 const fsavl_node_t
*fn1
= (const fsavl_node_t
*)arg1
;
481 const fsavl_node_t
*fn2
= (const fsavl_node_t
*)arg2
;
483 return (AVL_CMP(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_MAX_DATASET_NAME_LEN
];
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
, compress
;
847 snapfilter_cb_t
*filter_cb
;
850 char holdtag
[ZFS_MAX_DATASET_NAME_LEN
];
856 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
857 boolean_t fromorigin
, enum lzc_send_flags flags
, 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 */
872 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
874 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
875 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
879 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
880 "not an earlier snapshot from the same fs"));
881 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
884 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
885 ZFS_TYPE_SNAPSHOT
)) {
886 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
887 "incremental source (@%s) does not exist"),
890 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
903 zfs_error_aux(hdl
, strerror(errno
));
904 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
907 return (zfs_standard_error(hdl
, errno
, errbuf
));
911 *sizep
= zc
.zc_objset_type
;
917 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
918 * NULL) to the file descriptor specified by outfd.
921 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
922 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
925 zfs_cmd_t zc
= {"\0"};
926 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
929 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
930 assert(fromsnap_obj
== 0 || !fromorigin
);
932 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
933 zc
.zc_cookie
= outfd
;
934 zc
.zc_obj
= fromorigin
;
935 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
936 zc
.zc_fromobj
= fromsnap_obj
;
939 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
940 if (fromsnap
&& fromsnap
[0] != '\0') {
941 VERIFY(0 == nvlist_add_string(thisdbg
,
942 "fromsnap", fromsnap
));
945 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
947 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
948 "warning: cannot send '%s'"), zhp
->zfs_name
);
950 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
952 VERIFY(0 == nvlist_add_nvlist(debugnv
,
953 zhp
->zfs_name
, thisdbg
));
955 nvlist_free(thisdbg
);
959 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
960 "not an earlier snapshot from the same fs"));
961 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
964 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
965 ZFS_TYPE_SNAPSHOT
)) {
966 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
967 "incremental source (@%s) does not exist"),
970 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
983 zfs_error_aux(hdl
, strerror(errno
));
984 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
987 return (zfs_standard_error(hdl
, errno
, errbuf
));
992 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
993 nvlist_free(thisdbg
);
999 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
1001 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1004 * zfs_send() only sets snapholds for sends that need them,
1005 * e.g. replication and doall.
1007 if (sdd
->snapholds
== NULL
)
1010 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1014 send_progress_thread(void *arg
)
1016 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
;
1025 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1027 if (!pa
->pa_parsable
)
1028 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1031 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1036 zc
.zc_cookie
= pa
->pa_fd
;
1037 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1038 return ((void *)-1);
1042 bytes
= zc
.zc_cookie
;
1044 if (pa
->pa_parsable
) {
1045 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1046 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1047 bytes
, zhp
->zfs_name
);
1049 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1050 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1051 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1052 buf
, zhp
->zfs_name
);
1058 send_print_verbose(FILE *fout
, const char *tosnap
, const char *fromsnap
,
1059 uint64_t size
, boolean_t parsable
)
1062 if (fromsnap
!= NULL
) {
1063 (void) fprintf(fout
, "incremental\t%s\t%s",
1066 (void) fprintf(fout
, "full\t%s",
1070 if (fromsnap
!= NULL
) {
1071 if (strchr(fromsnap
, '@') == NULL
&&
1072 strchr(fromsnap
, '#') == NULL
) {
1073 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1074 "send from @%s to %s"),
1077 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1078 "send from %s to %s"),
1082 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1090 (void) fprintf(fout
, "\t%llu",
1094 zfs_nicenum(size
, buf
, sizeof (buf
));
1095 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1096 " estimated size is %s"), buf
);
1099 (void) fprintf(fout
, "\n");
1103 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1105 send_dump_data_t
*sdd
= arg
;
1106 progress_arg_t pa
= { 0 };
1109 enum lzc_send_flags flags
= 0;
1111 boolean_t isfromsnap
, istosnap
, fromorigin
;
1112 boolean_t exclude
= B_FALSE
;
1113 FILE *fout
= sdd
->std_out
? stdout
: stderr
;
1116 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1117 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1118 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1120 if (!sdd
->seenfrom
&& isfromsnap
) {
1121 gather_holds(zhp
, sdd
);
1122 sdd
->seenfrom
= B_TRUE
;
1123 (void) strcpy(sdd
->prevsnap
, thissnap
);
1124 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1129 if (sdd
->seento
|| !sdd
->seenfrom
) {
1134 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1136 sdd
->seento
= B_TRUE
;
1138 if (sdd
->large_block
)
1139 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1140 if (sdd
->embed_data
)
1141 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1143 flags
|= LZC_SEND_FLAG_COMPRESS
;
1145 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1146 if (sdd
->replicate
) {
1148 nvlist_t
*snapprops
;
1150 * Filter out all intermediate snapshots except origin
1151 * snapshots needed to replicate clones.
1153 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1154 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1156 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1157 "snapprops", &snapprops
));
1158 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1159 thissnap
, &snapprops
));
1160 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1167 * If a filter function exists, call it to determine whether
1168 * this snapshot will be sent.
1170 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1171 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1173 * This snapshot is filtered out. Don't send it, and don't
1174 * set prevsnap_obj, so it will be as if this snapshot didn't
1175 * exist, and the next accepted snapshot will be sent as
1176 * an incremental from the last accepted one, or as the
1177 * first (and full) snapshot in the case of a replication,
1178 * non-incremental send.
1184 gather_holds(zhp
, sdd
);
1185 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1186 (sdd
->fromorigin
|| sdd
->replicate
);
1190 (void) estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1191 fromorigin
, flags
, &size
);
1193 send_print_verbose(fout
, zhp
->zfs_name
,
1194 sdd
->prevsnap
[0] ? sdd
->prevsnap
: NULL
,
1195 size
, sdd
->parsable
);
1201 * If progress reporting is requested, spawn a new thread to
1202 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1204 if (sdd
->progress
) {
1206 pa
.pa_fd
= sdd
->outfd
;
1207 pa
.pa_parsable
= sdd
->parsable
;
1209 if ((err
= pthread_create(&tid
, NULL
,
1210 send_progress_thread
, &pa
))) {
1216 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1217 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1219 if (sdd
->progress
) {
1220 (void) pthread_cancel(tid
);
1221 (void) pthread_join(tid
, NULL
);
1225 (void) strcpy(sdd
->prevsnap
, thissnap
);
1226 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1232 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1235 send_dump_data_t
*sdd
= arg
;
1236 boolean_t missingfrom
= B_FALSE
;
1237 zfs_cmd_t zc
= {"\0"};
1239 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1240 zhp
->zfs_name
, sdd
->tosnap
);
1241 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1242 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1243 "WARNING: could not send %s@%s: does not exist\n"),
1244 zhp
->zfs_name
, sdd
->tosnap
);
1249 if (sdd
->replicate
&& sdd
->fromsnap
) {
1251 * If this fs does not have fromsnap, and we're doing
1252 * recursive, we need to send a full stream from the
1253 * beginning (or an incremental from the origin if this
1254 * is a clone). If we're doing non-recursive, then let
1255 * them get the error.
1257 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1258 zhp
->zfs_name
, sdd
->fromsnap
);
1259 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1260 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1261 missingfrom
= B_TRUE
;
1265 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1266 sdd
->prevsnap_obj
= 0;
1267 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1268 sdd
->seenfrom
= B_TRUE
;
1270 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1271 if (!sdd
->seenfrom
) {
1272 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1273 "WARNING: could not send %s@%s:\n"
1274 "incremental source (%s@%s) does not exist\n"),
1275 zhp
->zfs_name
, sdd
->tosnap
,
1276 zhp
->zfs_name
, sdd
->fromsnap
);
1278 } else if (!sdd
->seento
) {
1279 if (sdd
->fromsnap
) {
1280 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1281 "WARNING: could not send %s@%s:\n"
1282 "incremental source (%s@%s) "
1283 "is not earlier than it\n"),
1284 zhp
->zfs_name
, sdd
->tosnap
,
1285 zhp
->zfs_name
, sdd
->fromsnap
);
1287 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1289 "could not send %s@%s: does not exist\n"),
1290 zhp
->zfs_name
, sdd
->tosnap
);
1299 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1301 send_dump_data_t
*sdd
= arg
;
1303 boolean_t needagain
, progress
;
1305 if (!sdd
->replicate
)
1306 return (dump_filesystem(rzhp
, sdd
));
1308 /* Mark the clone origin snapshots. */
1309 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1310 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1312 uint64_t origin_guid
= 0;
1314 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1315 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1316 if (origin_guid
!= 0) {
1318 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1319 origin_guid
, &snapname
);
1320 if (origin_nv
!= NULL
) {
1321 nvlist_t
*snapprops
;
1322 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1323 "snapprops", &snapprops
));
1324 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1325 snapname
, &snapprops
));
1326 VERIFY(0 == nvlist_add_boolean(
1327 snapprops
, "is_clone_origin"));
1332 needagain
= progress
= B_FALSE
;
1333 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1334 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1335 nvlist_t
*fslist
, *parent_nv
;
1339 uint64_t origin_guid
= 0;
1340 uint64_t parent_guid
= 0;
1342 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1343 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1346 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1347 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1348 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1351 if (parent_guid
!= 0) {
1352 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1353 if (!nvlist_exists(parent_nv
, "sent")) {
1354 /* parent has not been sent; skip this one */
1360 if (origin_guid
!= 0) {
1361 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1363 if (origin_nv
!= NULL
&&
1364 !nvlist_exists(origin_nv
, "sent")) {
1366 * origin has not been sent yet;
1374 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1377 err
= dump_filesystem(zhp
, sdd
);
1378 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1389 /* clean out the sent flags in case we reuse this fss */
1390 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1391 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1394 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1395 (void) nvlist_remove_all(fslist
, "sent");
1402 zfs_send_resume_token_to_nvlist(libzfs_handle_t
*hdl
, const char *token
)
1404 unsigned int version
;
1406 unsigned long long checksum
, packed_len
;
1409 * Decode token header, which is:
1410 * <token version>-<checksum of payload>-<uncompressed payload length>
1411 * Note that the only supported token version is 1.
1413 nread
= sscanf(token
, "%u-%llx-%llx-",
1414 &version
, &checksum
, &packed_len
);
1416 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1417 "resume token is corrupt (invalid format)"));
1421 if (version
!= ZFS_SEND_RESUME_TOKEN_VERSION
) {
1422 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1423 "resume token is corrupt (invalid version %u)"),
1428 /* convert hexadecimal representation to binary */
1429 token
= strrchr(token
, '-') + 1;
1430 int len
= strlen(token
) / 2;
1431 unsigned char *compressed
= zfs_alloc(hdl
, len
);
1432 for (i
= 0; i
< len
; i
++) {
1433 nread
= sscanf(token
+ i
* 2, "%2hhx", compressed
+ i
);
1436 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1437 "resume token is corrupt "
1438 "(payload is not hex-encoded)"));
1443 /* verify checksum */
1445 fletcher_4_native_varsize(compressed
, len
, &cksum
);
1446 if (cksum
.zc_word
[0] != checksum
) {
1448 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1449 "resume token is corrupt (incorrect checksum)"));
1454 void *packed
= zfs_alloc(hdl
, packed_len
);
1455 uLongf packed_len_long
= packed_len
;
1456 if (uncompress(packed
, &packed_len_long
, compressed
, len
) != Z_OK
||
1457 packed_len_long
!= packed_len
) {
1460 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1461 "resume token is corrupt (decompression failed)"));
1467 int error
= nvlist_unpack(packed
, packed_len
, &nv
, KM_SLEEP
);
1471 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1472 "resume token is corrupt (nvlist_unpack failed)"));
1479 zfs_send_resume(libzfs_handle_t
*hdl
, sendflags_t
*flags
, int outfd
,
1480 const char *resume_token
)
1484 char *fromname
= NULL
;
1485 uint64_t resumeobj
, resumeoff
, toguid
, fromguid
, bytes
;
1488 char name
[ZFS_MAX_DATASET_NAME_LEN
];
1489 enum lzc_send_flags lzc_flags
= 0;
1491 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1492 "cannot resume send"));
1494 nvlist_t
*resume_nvl
=
1495 zfs_send_resume_token_to_nvlist(hdl
, resume_token
);
1496 if (resume_nvl
== NULL
) {
1498 * zfs_error_aux has already been set by
1499 * zfs_send_resume_token_to_nvlist
1501 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1503 if (flags
->verbose
) {
1504 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1505 "resume token contents:\n"));
1506 nvlist_print(stderr
, resume_nvl
);
1509 if (nvlist_lookup_string(resume_nvl
, "toname", &toname
) != 0 ||
1510 nvlist_lookup_uint64(resume_nvl
, "object", &resumeobj
) != 0 ||
1511 nvlist_lookup_uint64(resume_nvl
, "offset", &resumeoff
) != 0 ||
1512 nvlist_lookup_uint64(resume_nvl
, "bytes", &bytes
) != 0 ||
1513 nvlist_lookup_uint64(resume_nvl
, "toguid", &toguid
) != 0) {
1514 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1515 "resume token is corrupt"));
1516 return (zfs_error(hdl
, EZFS_FAULT
, errbuf
));
1519 (void) nvlist_lookup_uint64(resume_nvl
, "fromguid", &fromguid
);
1521 if (flags
->largeblock
|| nvlist_exists(resume_nvl
, "largeblockok"))
1522 lzc_flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1523 if (flags
->embed_data
|| nvlist_exists(resume_nvl
, "embedok"))
1524 lzc_flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1525 if (flags
->compress
|| nvlist_exists(resume_nvl
, "compressok"))
1526 lzc_flags
|= LZC_SEND_FLAG_COMPRESS
;
1528 if (guid_to_name(hdl
, toname
, toguid
, B_FALSE
, name
) != 0) {
1529 if (zfs_dataset_exists(hdl
, toname
, ZFS_TYPE_DATASET
)) {
1530 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1531 "'%s' is no longer the same snapshot used in "
1532 "the initial send"), toname
);
1534 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1535 "'%s' used in the initial send no longer exists"),
1538 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1540 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1542 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1543 "unable to access '%s'"), name
);
1544 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1547 if (fromguid
!= 0) {
1548 if (guid_to_name(hdl
, toname
, fromguid
, B_TRUE
, name
) != 0) {
1549 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1550 "incremental source %#llx no longer exists"),
1551 (longlong_t
)fromguid
);
1552 return (zfs_error(hdl
, EZFS_BADPATH
, errbuf
));
1557 if (flags
->verbose
) {
1559 error
= lzc_send_space(zhp
->zfs_name
, fromname
,
1562 size
= MAX(0, (int64_t)(size
- bytes
));
1563 send_print_verbose(stderr
, zhp
->zfs_name
, fromname
,
1564 size
, flags
->parsable
);
1567 if (!flags
->dryrun
) {
1568 progress_arg_t pa
= { 0 };
1571 * If progress reporting is requested, spawn a new thread to
1572 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1574 if (flags
->progress
) {
1577 pa
.pa_parsable
= flags
->parsable
;
1579 error
= pthread_create(&tid
, NULL
,
1580 send_progress_thread
, &pa
);
1587 error
= lzc_send_resume(zhp
->zfs_name
, fromname
, outfd
,
1588 lzc_flags
, resumeobj
, resumeoff
);
1590 if (flags
->progress
) {
1591 (void) pthread_cancel(tid
);
1592 (void) pthread_join(tid
, NULL
);
1596 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1597 "warning: cannot send '%s'"), zhp
->zfs_name
);
1617 zfs_error_aux(hdl
, strerror(errno
));
1618 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1621 return (zfs_standard_error(hdl
, errno
, errbuf
));
1632 * Generate a send stream for the dataset identified by the argument zhp.
1634 * The content of the send stream is the snapshot identified by
1635 * 'tosnap'. Incremental streams are requested in two ways:
1636 * - from the snapshot identified by "fromsnap" (if non-null) or
1637 * - from the origin of the dataset identified by zhp, which must
1638 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1641 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1642 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1643 * if "replicate" is set. If "doall" is set, dump all the intermediate
1644 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1645 * case too. If "props" is set, send properties.
1648 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1649 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1650 void *cb_arg
, nvlist_t
**debugnvp
)
1653 send_dump_data_t sdd
= { 0 };
1655 nvlist_t
*fss
= NULL
;
1656 avl_tree_t
*fsavl
= NULL
;
1657 static uint64_t holdseq
;
1661 dedup_arg_t dda
= { 0 };
1662 int featureflags
= 0;
1665 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1666 "cannot send '%s'"), zhp
->zfs_name
);
1668 if (fromsnap
&& fromsnap
[0] == '\0') {
1669 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1670 "zero-length incremental source"));
1671 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1674 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1676 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1677 if (version
>= ZPL_VERSION_SA
) {
1678 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1682 if (flags
->dedup
&& !flags
->dryrun
) {
1683 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1684 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1685 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
))) {
1686 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1687 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1690 dda
.outputfd
= outfd
;
1691 dda
.inputfd
= pipefd
[1];
1692 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1693 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1694 (void) close(pipefd
[0]);
1695 (void) close(pipefd
[1]);
1696 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1697 return (zfs_error(zhp
->zfs_hdl
,
1698 EZFS_THREADCREATEFAILED
, errbuf
));
1702 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1703 dmu_replay_record_t drr
= { 0 };
1704 char *packbuf
= NULL
;
1708 ZIO_SET_CHECKSUM(&zc
, 0, 0, 0, 0);
1710 if (flags
->replicate
|| flags
->props
) {
1713 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1715 VERIFY(0 == nvlist_add_string(hdrnv
,
1716 "fromsnap", fromsnap
));
1718 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1719 if (!flags
->replicate
) {
1720 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1724 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1725 fromsnap
, tosnap
, flags
->replicate
, &fss
, &fsavl
);
1728 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1729 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1739 if (!flags
->dryrun
) {
1740 /* write first begin record */
1741 drr
.drr_type
= DRR_BEGIN
;
1742 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1743 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1744 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1745 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1746 drr_versioninfo
, featureflags
);
1747 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1748 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1749 "%s@%s", zhp
->zfs_name
, tosnap
);
1750 drr
.drr_payloadlen
= buflen
;
1752 err
= dump_record(&drr
, packbuf
, buflen
, &zc
, outfd
);
1757 /* write end record */
1758 bzero(&drr
, sizeof (drr
));
1759 drr
.drr_type
= DRR_END
;
1760 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1761 err
= write(outfd
, &drr
, sizeof (drr
));
1771 /* dump each stream */
1772 sdd
.fromsnap
= fromsnap
;
1773 sdd
.tosnap
= tosnap
;
1775 sdd
.outfd
= pipefd
[0];
1778 sdd
.replicate
= flags
->replicate
;
1779 sdd
.doall
= flags
->doall
;
1780 sdd
.fromorigin
= flags
->fromorigin
;
1783 sdd
.verbose
= flags
->verbose
;
1784 sdd
.parsable
= flags
->parsable
;
1785 sdd
.progress
= flags
->progress
;
1786 sdd
.dryrun
= flags
->dryrun
;
1787 sdd
.large_block
= flags
->largeblock
;
1788 sdd
.embed_data
= flags
->embed_data
;
1789 sdd
.compress
= flags
->compress
;
1790 sdd
.filter_cb
= filter_func
;
1791 sdd
.filter_cb_arg
= cb_arg
;
1793 sdd
.debugnv
= *debugnvp
;
1794 if (sdd
.verbose
&& sdd
.dryrun
)
1795 sdd
.std_out
= B_TRUE
;
1796 fout
= sdd
.std_out
? stdout
: stderr
;
1799 * Some flags require that we place user holds on the datasets that are
1800 * being sent so they don't get destroyed during the send. We can skip
1801 * this step if the pool is imported read-only since the datasets cannot
1804 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1805 ZPOOL_PROP_READONLY
, NULL
) &&
1806 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1807 spa_version
>= SPA_VERSION_USERREFS
&&
1808 (flags
->doall
|| flags
->replicate
)) {
1810 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1811 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1812 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1813 if (sdd
.cleanup_fd
< 0) {
1817 sdd
.snapholds
= fnvlist_alloc();
1819 sdd
.cleanup_fd
= -1;
1820 sdd
.snapholds
= NULL
;
1822 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1824 * Do a verbose no-op dry run to get all the verbose output
1825 * or to gather snapshot hold's before generating any data,
1826 * then do a non-verbose real run to generate the streams.
1828 sdd
.dryrun
= B_TRUE
;
1829 err
= dump_filesystems(zhp
, &sdd
);
1834 if (flags
->verbose
) {
1835 if (flags
->parsable
) {
1836 (void) fprintf(fout
, "size\t%llu\n",
1837 (longlong_t
)sdd
.size
);
1840 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1841 (void) fprintf(fout
, dgettext(TEXT_DOMAIN
,
1842 "total estimated size is %s\n"), buf
);
1846 /* Ensure no snaps found is treated as an error. */
1852 /* Skip the second run if dryrun was requested. */
1856 if (sdd
.snapholds
!= NULL
) {
1857 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1861 fnvlist_free(sdd
.snapholds
);
1862 sdd
.snapholds
= NULL
;
1865 sdd
.dryrun
= B_FALSE
;
1866 sdd
.verbose
= B_FALSE
;
1869 err
= dump_filesystems(zhp
, &sdd
);
1870 fsavl_destroy(fsavl
);
1873 /* Ensure no snaps found is treated as an error. */
1874 if (err
== 0 && !sdd
.seento
)
1879 (void) pthread_cancel(tid
);
1880 (void) close(pipefd
[0]);
1881 (void) pthread_join(tid
, NULL
);
1884 if (sdd
.cleanup_fd
!= -1) {
1885 VERIFY(0 == close(sdd
.cleanup_fd
));
1886 sdd
.cleanup_fd
= -1;
1889 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1892 * write final end record. NB: want to do this even if
1893 * there was some error, because it might not be totally
1896 dmu_replay_record_t drr
= { 0 };
1897 drr
.drr_type
= DRR_END
;
1898 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1899 return (zfs_standard_error(zhp
->zfs_hdl
,
1904 return (err
|| sdd
.err
);
1907 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1909 fsavl_destroy(fsavl
);
1911 fnvlist_free(sdd
.snapholds
);
1913 if (sdd
.cleanup_fd
!= -1)
1914 VERIFY(0 == close(sdd
.cleanup_fd
));
1916 (void) pthread_cancel(tid
);
1917 (void) close(pipefd
[0]);
1918 (void) pthread_join(tid
, NULL
);
1924 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
1925 enum lzc_send_flags flags
)
1928 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1931 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1932 "warning: cannot send '%s'"), zhp
->zfs_name
);
1934 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
1938 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1939 "not an earlier snapshot from the same fs"));
1940 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1944 if (lzc_exists(zhp
->zfs_name
)) {
1945 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1946 "incremental source (%s) does not exist"),
1949 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1952 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1953 "target is busy; if a filesystem, "
1954 "it must not be mounted"));
1955 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
1968 zfs_error_aux(hdl
, strerror(errno
));
1969 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1972 return (zfs_standard_error(hdl
, errno
, errbuf
));
1979 * Routines specific to "zfs recv"
1983 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1984 boolean_t byteswap
, zio_cksum_t
*zc
)
1990 assert(ilen
<= SPA_MAXBLOCKSIZE
);
1993 rv
= read(fd
, cp
, len
);
1998 if (rv
< 0 || len
!= 0) {
1999 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2000 "failed to read from stream"));
2001 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
2002 "cannot receive")));
2007 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
2009 fletcher_4_incremental_native(buf
, ilen
, zc
);
2015 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
2016 boolean_t byteswap
, zio_cksum_t
*zc
)
2021 buf
= zfs_alloc(hdl
, len
);
2025 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
2031 err
= nvlist_unpack(buf
, len
, nvp
, 0);
2034 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2035 "stream (malformed nvlist)"));
2042 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
2043 int baselen
, char *newname
, recvflags_t
*flags
)
2046 zfs_cmd_t zc
= {"\0"};
2048 prop_changelist_t
*clp
;
2051 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2054 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2055 flags
->force
? MS_FORCE
: 0);
2059 err
= changelist_prefix(clp
);
2063 zc
.zc_objset_type
= DMU_OST_ZFS
;
2064 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2067 (void) strcpy(newname
, tryname
);
2069 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
2071 if (flags
->verbose
) {
2072 (void) printf("attempting rename %s to %s\n",
2073 zc
.zc_name
, zc
.zc_value
);
2075 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2077 changelist_rename(clp
, name
, tryname
);
2082 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
2085 (void) snprintf(newname
, ZFS_MAX_DATASET_NAME_LEN
,
2086 "%.*srecv-%u-%u", baselen
, name
, getpid(), seq
);
2087 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
2089 if (flags
->verbose
) {
2090 (void) printf("failed - trying rename %s to %s\n",
2091 zc
.zc_name
, zc
.zc_value
);
2093 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
2095 changelist_rename(clp
, name
, newname
);
2096 if (err
&& flags
->verbose
) {
2097 (void) printf("failed (%u) - "
2098 "will try again on next pass\n", errno
);
2101 } else if (flags
->verbose
) {
2103 (void) printf("success\n");
2105 (void) printf("failed (%u)\n", errno
);
2108 (void) changelist_postfix(clp
);
2109 changelist_free(clp
);
2115 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
2116 char *newname
, recvflags_t
*flags
)
2118 zfs_cmd_t zc
= {"\0"};
2120 prop_changelist_t
*clp
;
2122 boolean_t defer
= B_FALSE
;
2125 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
2128 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
2129 flags
->force
? MS_FORCE
: 0);
2130 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
2131 zfs_spa_version(zhp
, &spa_version
) == 0 &&
2132 spa_version
>= SPA_VERSION_USERREFS
)
2137 err
= changelist_prefix(clp
);
2141 zc
.zc_objset_type
= DMU_OST_ZFS
;
2142 zc
.zc_defer_destroy
= defer
;
2143 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
2146 (void) printf("attempting destroy %s\n", zc
.zc_name
);
2147 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
2150 (void) printf("success\n");
2151 changelist_remove(clp
, zc
.zc_name
);
2154 (void) changelist_postfix(clp
);
2155 changelist_free(clp
);
2158 * Deferred destroy might destroy the snapshot or only mark it to be
2159 * destroyed later, and it returns success in either case.
2161 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
2162 ZFS_TYPE_SNAPSHOT
))) {
2163 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
2169 typedef struct guid_to_name_data
{
2171 boolean_t bookmark_ok
;
2174 } guid_to_name_data_t
;
2177 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
2179 guid_to_name_data_t
*gtnd
= arg
;
2183 if (gtnd
->skip
!= NULL
&&
2184 (slash
= strrchr(zhp
->zfs_name
, '/')) != NULL
&&
2185 strcmp(slash
+ 1, gtnd
->skip
) == 0) {
2190 if (zfs_prop_get_int(zhp
, ZFS_PROP_GUID
) == gtnd
->guid
) {
2191 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
2196 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
2197 if (err
!= EEXIST
&& gtnd
->bookmark_ok
)
2198 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, gtnd
);
2204 * Attempt to find the local dataset associated with this guid. In the case of
2205 * multiple matches, we attempt to find the "best" match by searching
2206 * progressively larger portions of the hierarchy. This allows one to send a
2207 * tree of datasets individually and guarantee that we will find the source
2208 * guid within that hierarchy, even if there are multiple matches elsewhere.
2211 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
2212 boolean_t bookmark_ok
, char *name
)
2214 char pname
[ZFS_MAX_DATASET_NAME_LEN
];
2215 guid_to_name_data_t gtnd
;
2218 gtnd
.bookmark_ok
= bookmark_ok
;
2223 * Search progressively larger portions of the hierarchy, starting
2224 * with the filesystem specified by 'parent'. This will
2225 * select the "most local" version of the origin snapshot in the case
2226 * that there are multiple matching snapshots in the system.
2228 (void) strlcpy(pname
, parent
, sizeof (pname
));
2229 char *cp
= strrchr(pname
, '@');
2231 cp
= strchr(pname
, '\0');
2232 for (; cp
!= NULL
; cp
= strrchr(pname
, '/')) {
2233 /* Chop off the last component and open the parent */
2235 zfs_handle_t
*zhp
= make_dataset_handle(hdl
, pname
);
2239 int err
= guid_to_name_cb(zfs_handle_dup(zhp
), >nd
);
2241 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
2242 if (err
!= EEXIST
&& bookmark_ok
)
2243 err
= zfs_iter_bookmarks(zhp
, guid_to_name_cb
, >nd
);
2249 * Remember the last portion of the dataset so we skip it next
2250 * time through (as we've already searched that portion of the
2253 gtnd
.skip
= strrchr(pname
, '/') + 1;
2260 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2261 * guid1 is after guid2.
2264 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
2265 uint64_t guid1
, uint64_t guid2
)
2268 char *fsname
= NULL
, *snapname
= NULL
;
2269 char buf
[ZFS_MAX_DATASET_NAME_LEN
];
2271 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2272 uint64_t create1
, create2
;
2279 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2280 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2281 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2282 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2283 if (guid1hdl
== NULL
)
2286 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2287 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2288 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2289 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2290 if (guid2hdl
== NULL
) {
2291 zfs_close(guid1hdl
);
2295 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2296 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2298 if (create1
< create2
)
2300 else if (create1
> create2
)
2305 zfs_close(guid1hdl
);
2306 zfs_close(guid2hdl
);
2312 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2313 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2316 nvlist_t
*local_nv
, *deleted
= NULL
;
2317 avl_tree_t
*local_avl
;
2318 nvpair_t
*fselem
, *nextfselem
;
2320 char newname
[ZFS_MAX_DATASET_NAME_LEN
];
2323 boolean_t needagain
, progress
, recursive
;
2326 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2328 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2335 needagain
= progress
= B_FALSE
;
2337 VERIFY(0 == nvlist_alloc(&deleted
, NV_UNIQUE_NAME
, 0));
2339 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2340 recursive
, &local_nv
, &local_avl
)) != 0)
2344 * Process deletes and renames
2346 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2347 fselem
; fselem
= nextfselem
) {
2348 nvlist_t
*nvfs
, *snaps
;
2349 nvlist_t
*stream_nvfs
= NULL
;
2350 nvpair_t
*snapelem
, *nextsnapelem
;
2351 uint64_t fromguid
= 0;
2352 uint64_t originguid
= 0;
2353 uint64_t stream_originguid
= 0;
2354 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2355 char *fsname
, *stream_fsname
;
2357 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2359 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2360 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2361 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2362 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2363 &parent_fromsnap_guid
));
2364 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2367 * First find the stream's fs, so we can check for
2368 * a different origin (due to "zfs promote")
2370 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2371 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2374 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2375 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2377 if (stream_nvfs
!= NULL
)
2381 /* check for promote */
2382 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2383 &stream_originguid
);
2384 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2385 switch (created_before(hdl
, local_avl
,
2386 stream_originguid
, originguid
)) {
2389 zfs_cmd_t zc
= {"\0"};
2390 nvlist_t
*origin_nvfs
;
2391 char *origin_fsname
;
2394 (void) printf("promoting %s\n", fsname
);
2396 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2398 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2399 "name", &origin_fsname
));
2400 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2401 sizeof (zc
.zc_value
));
2402 (void) strlcpy(zc
.zc_name
, fsname
,
2403 sizeof (zc
.zc_name
));
2404 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2412 fsavl_destroy(local_avl
);
2413 nvlist_free(local_nv
);
2417 * We had/have the wrong origin, therefore our
2418 * list of snapshots is wrong. Need to handle
2419 * them on the next pass.
2425 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2426 snapelem
; snapelem
= nextsnapelem
) {
2428 char *stream_snapname
;
2429 nvlist_t
*found
, *props
;
2431 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2433 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2434 found
= fsavl_find(stream_avl
, thisguid
,
2437 /* check for delete */
2438 if (found
== NULL
) {
2439 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2444 (void) snprintf(name
, sizeof (name
), "%s@%s",
2445 fsname
, nvpair_name(snapelem
));
2447 error
= recv_destroy(hdl
, name
,
2448 strlen(fsname
)+1, newname
, flags
);
2453 sprintf(guidname
, "%llu",
2454 (u_longlong_t
)thisguid
);
2455 nvlist_add_boolean(deleted
, guidname
);
2459 stream_nvfs
= found
;
2461 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2462 &props
) && 0 == nvlist_lookup_nvlist(props
,
2463 stream_snapname
, &props
)) {
2464 zfs_cmd_t zc
= {"\0"};
2466 zc
.zc_cookie
= B_TRUE
; /* received */
2467 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2468 "%s@%s", fsname
, nvpair_name(snapelem
));
2469 if (zcmd_write_src_nvlist(hdl
, &zc
,
2471 (void) zfs_ioctl(hdl
,
2472 ZFS_IOC_SET_PROP
, &zc
);
2473 zcmd_free_nvlists(&zc
);
2477 /* check for different snapname */
2478 if (strcmp(nvpair_name(snapelem
),
2479 stream_snapname
) != 0) {
2480 char name
[ZFS_MAX_DATASET_NAME_LEN
];
2481 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2483 (void) snprintf(name
, sizeof (name
), "%s@%s",
2484 fsname
, nvpair_name(snapelem
));
2485 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2486 fsname
, stream_snapname
);
2488 error
= recv_rename(hdl
, name
, tryname
,
2489 strlen(fsname
)+1, newname
, flags
);
2496 if (strcmp(stream_snapname
, fromsnap
) == 0)
2497 fromguid
= thisguid
;
2500 /* check for delete */
2501 if (stream_nvfs
== NULL
) {
2505 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2511 sprintf(guidname
, "%llu",
2512 (u_longlong_t
) parent_fromsnap_guid
);
2513 nvlist_add_boolean(deleted
, guidname
);
2517 if (fromguid
== 0) {
2518 if (flags
->verbose
) {
2519 (void) printf("local fs %s does not have "
2520 "fromsnap (%s in stream); must have "
2521 "been deleted locally; ignoring\n",
2527 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2528 "name", &stream_fsname
));
2529 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2530 "parentfromsnap", &stream_parent_fromsnap_guid
));
2532 s1
= strrchr(fsname
, '/');
2533 s2
= strrchr(stream_fsname
, '/');
2536 * Check if we're going to rename based on parent guid change
2537 * and the current parent guid was also deleted. If it was then
2538 * rename will fail and is likely unneeded, so avoid this and
2539 * force an early retry to determine the new
2540 * parent_fromsnap_guid.
2542 if (stream_parent_fromsnap_guid
!= 0 &&
2543 parent_fromsnap_guid
!= 0 &&
2544 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
2545 sprintf(guidname
, "%llu",
2546 (u_longlong_t
) parent_fromsnap_guid
);
2547 if (nvlist_exists(deleted
, guidname
)) {
2555 * Check for rename. If the exact receive path is specified, it
2556 * does not count as a rename, but we still need to check the
2557 * datasets beneath it.
2559 if ((stream_parent_fromsnap_guid
!= 0 &&
2560 parent_fromsnap_guid
!= 0 &&
2561 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2562 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2563 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2565 char tryname
[ZFS_MAX_DATASET_NAME_LEN
];
2567 parent
= fsavl_find(local_avl
,
2568 stream_parent_fromsnap_guid
, NULL
);
2570 * NB: parent might not be found if we used the
2571 * tosnap for stream_parent_fromsnap_guid,
2572 * because the parent is a newly-created fs;
2573 * we'll be able to rename it after we recv the
2576 if (parent
!= NULL
) {
2579 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2581 (void) snprintf(tryname
, sizeof (tryname
),
2582 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2585 if (flags
->verbose
) {
2586 (void) printf("local fs %s new parent "
2587 "not found\n", fsname
);
2593 error
= recv_rename(hdl
, fsname
, tryname
,
2594 strlen(tofs
)+1, newname
, flags
);
2596 if (renamed
!= NULL
&& newname
[0] != '\0') {
2597 VERIFY(0 == nvlist_add_boolean(renamed
,
2609 fsavl_destroy(local_avl
);
2610 nvlist_free(local_nv
);
2611 nvlist_free(deleted
);
2613 if (needagain
&& progress
) {
2614 /* do another pass to fix up temporary names */
2616 (void) printf("another pass:\n");
2624 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2625 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2626 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2628 nvlist_t
*stream_nv
= NULL
;
2629 avl_tree_t
*stream_avl
= NULL
;
2630 char *fromsnap
= NULL
;
2631 char *sendsnap
= NULL
;
2633 char tofs
[ZFS_MAX_DATASET_NAME_LEN
];
2634 char sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
2636 dmu_replay_record_t drre
;
2638 boolean_t anyerr
= B_FALSE
;
2639 boolean_t softerr
= B_FALSE
;
2640 boolean_t recursive
;
2642 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2645 assert(drr
->drr_type
== DRR_BEGIN
);
2646 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2647 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2648 DMU_COMPOUNDSTREAM
);
2651 * Read in the nvlist from the stream.
2653 if (drr
->drr_payloadlen
!= 0) {
2654 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2655 &stream_nv
, flags
->byteswap
, zc
);
2657 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2662 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2665 if (recursive
&& strchr(destname
, '@')) {
2666 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2667 "cannot specify snapshot name for multi-snapshot stream"));
2668 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2673 * Read in the end record and verify checksum.
2675 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2676 flags
->byteswap
, NULL
)))
2678 if (flags
->byteswap
) {
2679 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2680 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2681 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2682 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2683 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2684 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2685 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2686 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2687 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2689 if (drre
.drr_type
!= DRR_END
) {
2690 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2693 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2694 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2695 "incorrect header checksum"));
2696 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2700 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2702 if (drr
->drr_payloadlen
!= 0) {
2703 nvlist_t
*stream_fss
;
2705 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2707 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2708 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2709 "couldn't allocate avl tree"));
2710 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2714 if (fromsnap
!= NULL
) {
2715 nvlist_t
*renamed
= NULL
;
2716 nvpair_t
*pair
= NULL
;
2718 (void) strlcpy(tofs
, destname
, sizeof (tofs
));
2719 if (flags
->isprefix
) {
2720 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2723 if (flags
->istail
) {
2724 cp
= strrchr(drrb
->drr_toname
, '/');
2726 (void) strlcat(tofs
, "/",
2730 i
= (cp
- drrb
->drr_toname
);
2733 i
= strcspn(drrb
->drr_toname
, "/@");
2735 /* zfs_receive_one() will create_parents() */
2736 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2738 *strchr(tofs
, '@') = '\0';
2741 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2742 VERIFY(0 == nvlist_alloc(&renamed
,
2743 NV_UNIQUE_NAME
, 0));
2746 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2747 stream_nv
, stream_avl
, renamed
);
2749 /* Unmount renamed filesystems before receiving. */
2750 while ((pair
= nvlist_next_nvpair(renamed
,
2753 prop_changelist_t
*clp
= NULL
;
2755 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2756 ZFS_TYPE_FILESYSTEM
);
2758 clp
= changelist_gather(zhp
,
2759 ZFS_PROP_MOUNTPOINT
, 0, 0);
2763 changelist_prefix(clp
);
2764 changelist_free(clp
);
2769 nvlist_free(renamed
);
2774 * Get the fs specified by the first path in the stream (the top level
2775 * specified by 'zfs send') and pass it to each invocation of
2776 * zfs_receive_one().
2778 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2780 if ((cp
= strchr(sendfs
, '@')) != NULL
) {
2783 * Find the "sendsnap", the final snapshot in a replication
2784 * stream. zfs_receive_one() handles certain errors
2785 * differently, depending on if the contained stream is the
2788 sendsnap
= (cp
+ 1);
2791 /* Finally, receive each contained stream */
2794 * we should figure out if it has a recoverable
2795 * error, in which case do a recv_skip() and drive on.
2796 * Note, if we fail due to already having this guid,
2797 * zfs_receive_one() will take care of it (ie,
2798 * recv_skip() and return 0).
2800 error
= zfs_receive_impl(hdl
, destname
, NULL
, flags
, fd
,
2801 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2802 action_handlep
, sendsnap
);
2803 if (error
== ENODATA
) {
2808 } while (error
== 0);
2810 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2812 * Now that we have the fs's they sent us, try the
2815 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2816 stream_nv
, stream_avl
, NULL
);
2820 fsavl_destroy(stream_avl
);
2821 nvlist_free(stream_nv
);
2830 trunc_prop_errs(int truncated
)
2832 ASSERT(truncated
!= 0);
2835 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2836 "1 more property could not be set\n"));
2838 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2839 "%d more properties could not be set\n"), truncated
);
2843 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2845 dmu_replay_record_t
*drr
;
2846 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2849 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2850 "cannot receive:"));
2852 /* XXX would be great to use lseek if possible... */
2855 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2856 byteswap
, NULL
) == 0) {
2858 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2860 switch (drr
->drr_type
) {
2862 if (drr
->drr_payloadlen
!= 0) {
2863 (void) recv_read(hdl
, fd
, buf
,
2864 drr
->drr_payloadlen
, B_FALSE
, NULL
);
2874 drr
->drr_u
.drr_object
.drr_bonuslen
=
2875 BSWAP_32(drr
->drr_u
.drr_object
.
2878 (void) recv_read(hdl
, fd
, buf
,
2879 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2885 drr
->drr_u
.drr_write
.drr_logical_size
=
2887 drr
->drr_u
.drr_write
.drr_logical_size
);
2888 drr
->drr_u
.drr_write
.drr_compressed_size
=
2890 drr
->drr_u
.drr_write
.drr_compressed_size
);
2892 uint64_t payload_size
=
2893 DRR_WRITE_PAYLOAD_SIZE(&drr
->drr_u
.drr_write
);
2894 (void) recv_read(hdl
, fd
, buf
,
2895 payload_size
, B_FALSE
, NULL
);
2899 drr
->drr_u
.drr_spill
.drr_length
=
2900 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2902 (void) recv_read(hdl
, fd
, buf
,
2903 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2905 case DRR_WRITE_EMBEDDED
:
2907 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2908 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2911 (void) recv_read(hdl
, fd
, buf
,
2912 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2915 case DRR_WRITE_BYREF
:
2916 case DRR_FREEOBJECTS
:
2921 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2922 "invalid record type"));
2923 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2932 recv_ecksum_set_aux(libzfs_handle_t
*hdl
, const char *target_snap
,
2933 boolean_t resumable
)
2935 char target_fs
[ZFS_MAX_DATASET_NAME_LEN
];
2937 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2938 "checksum mismatch or incomplete stream"));
2942 (void) strlcpy(target_fs
, target_snap
, sizeof (target_fs
));
2943 *strchr(target_fs
, '@') = '\0';
2944 zfs_handle_t
*zhp
= zfs_open(hdl
, target_fs
,
2945 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2949 char token_buf
[ZFS_MAXPROPLEN
];
2950 int error
= zfs_prop_get(zhp
, ZFS_PROP_RECEIVE_RESUME_TOKEN
,
2951 token_buf
, sizeof (token_buf
),
2952 NULL
, NULL
, 0, B_TRUE
);
2954 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2955 "checksum mismatch or incomplete stream.\n"
2956 "Partially received snapshot is saved.\n"
2957 "A resuming stream can be generated on the sending "
2958 "system by running:\n"
2966 * Restores a backup of tosnap from the file descriptor specified by infd.
2969 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2970 const char *originsnap
, recvflags_t
*flags
, dmu_replay_record_t
*drr
,
2971 dmu_replay_record_t
*drr_noswap
, const char *sendfs
, nvlist_t
*stream_nv
,
2972 avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2973 uint64_t *action_handlep
, const char *finalsnap
)
2976 int ioctl_err
, ioctl_errno
, err
;
2978 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2980 const char *chopprefix
;
2981 boolean_t newfs
= B_FALSE
;
2982 boolean_t stream_wantsnewfs
;
2983 boolean_t newprops
= B_FALSE
;
2984 uint64_t read_bytes
= 0;
2985 uint64_t errflags
= 0;
2986 uint64_t parent_snapguid
= 0;
2987 prop_changelist_t
*clp
= NULL
;
2988 nvlist_t
*snapprops_nvlist
= NULL
;
2989 zprop_errflags_t prop_errflags
;
2990 nvlist_t
*prop_errors
= NULL
;
2991 boolean_t recursive
;
2992 char *snapname
= NULL
;
2993 char destsnap
[MAXPATHLEN
* 2];
2994 char origin
[MAXNAMELEN
];
2995 char name
[MAXPATHLEN
];
2996 nvlist_t
*props
= NULL
;
2998 begin_time
= time(NULL
);
2999 bzero(origin
, MAXNAMELEN
);
3001 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3004 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
3007 if (stream_avl
!= NULL
) {
3008 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
3011 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
3013 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
3015 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
3019 if (flags
->canmountoff
) {
3020 VERIFY(0 == nvlist_add_uint64(props
,
3021 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
3028 * Determine how much of the snapshot name stored in the stream
3029 * we are going to tack on to the name they specified on the
3030 * command line, and how much we are going to chop off.
3032 * If they specified a snapshot, chop the entire name stored in
3035 if (flags
->istail
) {
3037 * A filesystem was specified with -e. We want to tack on only
3038 * the tail of the sent snapshot path.
3040 if (strchr(tosnap
, '@')) {
3041 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3042 "argument - snapshot not allowed with -e"));
3043 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3047 chopprefix
= strrchr(sendfs
, '/');
3049 if (chopprefix
== NULL
) {
3051 * The tail is the poolname, so we need to
3052 * prepend a path separator.
3054 int len
= strlen(drrb
->drr_toname
);
3055 cp
= malloc(len
+ 2);
3057 (void) strcpy(&cp
[1], drrb
->drr_toname
);
3060 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
3062 } else if (flags
->isprefix
) {
3064 * A filesystem was specified with -d. We want to tack on
3065 * everything but the first element of the sent snapshot path
3066 * (all but the pool name).
3068 if (strchr(tosnap
, '@')) {
3069 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3070 "argument - snapshot not allowed with -d"));
3071 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3075 chopprefix
= strchr(drrb
->drr_toname
, '/');
3076 if (chopprefix
== NULL
)
3077 chopprefix
= strchr(drrb
->drr_toname
, '@');
3078 } else if (strchr(tosnap
, '@') == NULL
) {
3080 * If a filesystem was specified without -d or -e, we want to
3081 * tack on everything after the fs specified by 'zfs send'.
3083 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
3085 /* A snapshot was specified as an exact path (no -d or -e). */
3087 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3088 "cannot specify snapshot name for multi-snapshot "
3090 err
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3093 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
3096 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
3097 ASSERT(chopprefix
> drrb
->drr_toname
);
3098 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
3099 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
3100 chopprefix
[0] == '\0');
3103 * Determine name of destination snapshot.
3105 (void) strcpy(destsnap
, tosnap
);
3106 (void) strlcat(destsnap
, chopprefix
, sizeof (destsnap
));
3108 if (!zfs_name_valid(destsnap
, ZFS_TYPE_SNAPSHOT
)) {
3109 err
= zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
);
3114 * Determine the name of the origin snapshot.
3116 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
3117 if (guid_to_name(hdl
, destsnap
,
3118 drrb
->drr_fromguid
, B_FALSE
, origin
) != 0) {
3119 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3120 "local origin for clone %s does not exist"),
3122 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
3126 (void) printf("found clone origin %s\n", origin
);
3127 } else if (originsnap
) {
3128 (void) strncpy(origin
, originsnap
, sizeof (origin
));
3130 (void) printf("using provided clone origin %s\n",
3134 boolean_t resuming
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
) &
3135 DMU_BACKUP_FEATURE_RESUMING
;
3136 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
3137 (drrb
->drr_flags
& DRR_FLAG_CLONE
) || originsnap
) && !resuming
;
3139 if (stream_wantsnewfs
) {
3141 * if the parent fs does not exist, look for it based on
3142 * the parent snap GUID
3144 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3145 "cannot receive new filesystem stream"));
3147 (void) strcpy(name
, destsnap
);
3148 cp
= strrchr(name
, '/');
3152 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3153 char suffix
[ZFS_MAX_DATASET_NAME_LEN
];
3154 (void) strcpy(suffix
, strrchr(destsnap
, '/'));
3155 if (guid_to_name(hdl
, name
, parent_snapguid
,
3156 B_FALSE
, destsnap
) == 0) {
3157 *strchr(destsnap
, '@') = '\0';
3158 (void) strcat(destsnap
, suffix
);
3163 * if the fs does not exist, look for it based on the
3166 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3167 "cannot receive incremental stream"));
3169 (void) strcpy(name
, destsnap
);
3170 *strchr(name
, '@') = '\0';
3173 * If the exact receive path was specified and this is the
3174 * topmost path in the stream, then if the fs does not exist we
3175 * should look no further.
3177 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
3178 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
3179 !zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3180 char snap
[ZFS_MAX_DATASET_NAME_LEN
];
3181 (void) strcpy(snap
, strchr(destsnap
, '@'));
3182 if (guid_to_name(hdl
, name
, drrb
->drr_fromguid
,
3183 B_FALSE
, destsnap
) == 0) {
3184 *strchr(destsnap
, '@') = '\0';
3185 (void) strcat(destsnap
, snap
);
3190 (void) strcpy(name
, destsnap
);
3191 *strchr(name
, '@') = '\0';
3193 if (zfs_dataset_exists(hdl
, name
, ZFS_TYPE_DATASET
)) {
3194 zfs_cmd_t zc
= {"\0"};
3197 (void) strcpy(zc
.zc_name
, name
);
3200 * Destination fs exists. It must be one of these cases:
3201 * - an incremental send stream
3202 * - the stream specifies a new fs (full stream or clone)
3203 * and they want us to blow away the existing fs (and
3204 * have therefore specified -F and removed any snapshots)
3205 * - we are resuming a failed receive.
3207 if (stream_wantsnewfs
) {
3208 if (!flags
->force
) {
3209 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3210 "destination '%s' exists\n"
3211 "must specify -F to overwrite it"), name
);
3212 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3215 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
3217 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3218 "destination has snapshots (eg. %s)\n"
3219 "must destroy them to overwrite it"),
3221 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3226 if ((zhp
= zfs_open(hdl
, name
,
3227 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
3232 if (stream_wantsnewfs
&&
3233 zhp
->zfs_dmustats
.dds_origin
[0]) {
3235 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3236 "destination '%s' is a clone\n"
3237 "must destroy it to overwrite it"), name
);
3238 err
= zfs_error(hdl
, EZFS_EXISTS
, errbuf
);
3242 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
3243 stream_wantsnewfs
) {
3244 /* We can't do online recv in this case */
3245 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
3251 if (changelist_prefix(clp
) != 0) {
3252 changelist_free(clp
);
3260 * If we are resuming a newfs, set newfs here so that we will
3261 * mount it if the recv succeeds this time. We can tell
3262 * that it was a newfs on the first recv because the fs
3263 * itself will be inconsistent (if the fs existed when we
3264 * did the first recv, we would have received it into
3267 if (resuming
&& zfs_prop_get_int(zhp
, ZFS_PROP_INCONSISTENT
))
3273 * Destination filesystem does not exist. Therefore we better
3274 * be creating a new filesystem (either from a full backup, or
3275 * a clone). It would therefore be invalid if the user
3276 * specified only the pool name (i.e. if the destination name
3277 * contained no slash character).
3279 cp
= strrchr(name
, '/');
3281 if (!stream_wantsnewfs
|| cp
== NULL
) {
3282 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3283 "destination '%s' does not exist"), name
);
3284 err
= zfs_error(hdl
, EZFS_NOENT
, errbuf
);
3289 * Trim off the final dataset component so we perform the
3290 * recvbackup ioctl to the filesystems's parent.
3294 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
3295 create_parents(hdl
, destsnap
, strlen(tosnap
)) != 0) {
3296 err
= zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3303 if (flags
->verbose
) {
3304 (void) printf("%s %s stream of %s into %s\n",
3305 flags
->dryrun
? "would receive" : "receiving",
3306 drrb
->drr_fromguid
? "incremental" : "full",
3307 drrb
->drr_toname
, destsnap
);
3308 (void) fflush(stdout
);
3311 if (flags
->dryrun
) {
3312 err
= recv_skip(hdl
, infd
, flags
->byteswap
);
3316 err
= ioctl_err
= lzc_receive_one(destsnap
, props
, origin
,
3317 flags
->force
, flags
->resumable
, infd
, drr_noswap
, cleanup_fd
,
3318 &read_bytes
, &errflags
, action_handlep
, &prop_errors
);
3319 ioctl_errno
= ioctl_err
;
3320 prop_errflags
= errflags
;
3323 nvpair_t
*prop_err
= NULL
;
3325 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
3326 prop_err
)) != NULL
) {
3331 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
3332 (void) nvpair_value_int32(prop_err
, &intval
);
3333 if (strcmp(nvpair_name(prop_err
),
3334 ZPROP_N_MORE_ERRORS
) == 0) {
3335 trunc_prop_errs(intval
);
3337 } else if (snapname
== NULL
|| finalsnap
== NULL
||
3338 strcmp(finalsnap
, snapname
) == 0 ||
3339 strcmp(nvpair_name(prop_err
),
3340 zfs_prop_to_name(ZFS_PROP_REFQUOTA
)) != 0) {
3342 * Skip the special case of, for example,
3343 * "refquota", errors on intermediate
3344 * snapshots leading up to a final one.
3345 * That's why we have all of the checks above.
3347 * See zfs_ioctl.c's extract_delay_props() for
3348 * a list of props which can fail on
3349 * intermediate snapshots, but shouldn't
3350 * affect the overall receive.
3352 (void) snprintf(tbuf
, sizeof (tbuf
),
3353 dgettext(TEXT_DOMAIN
,
3354 "cannot receive %s property on %s"),
3355 nvpair_name(prop_err
), name
);
3356 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3361 if (err
== 0 && snapprops_nvlist
) {
3362 zfs_cmd_t zc
= {"\0"};
3364 (void) strcpy(zc
.zc_name
, destsnap
);
3365 zc
.zc_cookie
= B_TRUE
; /* received */
3366 if (zcmd_write_src_nvlist(hdl
, &zc
, snapprops_nvlist
) == 0) {
3367 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc
);
3368 zcmd_free_nvlists(&zc
);
3372 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3374 * It may be that this snapshot already exists,
3375 * in which case we want to consume & ignore it
3376 * rather than failing.
3378 avl_tree_t
*local_avl
;
3379 nvlist_t
*local_nv
, *fs
;
3380 cp
= strchr(destsnap
, '@');
3383 * XXX Do this faster by just iterating over snaps in
3384 * this fs. Also if zc_value does not exist, we will
3385 * get a strange "does not exist" error message.
3388 if (gather_nvlist(hdl
, destsnap
, NULL
, NULL
, B_FALSE
,
3389 &local_nv
, &local_avl
) == 0) {
3391 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3392 fsavl_destroy(local_avl
);
3393 nvlist_free(local_nv
);
3396 if (flags
->verbose
) {
3397 (void) printf("snap %s already exists; "
3398 "ignoring\n", destsnap
);
3400 err
= ioctl_err
= recv_skip(hdl
, infd
,
3407 if (ioctl_err
!= 0) {
3408 switch (ioctl_errno
) {
3410 cp
= strchr(destsnap
, '@');
3412 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3413 "most recent snapshot of %s does not\n"
3414 "match incremental source"), destsnap
);
3415 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3419 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3420 "destination %s has been modified\n"
3421 "since most recent snapshot"), name
);
3422 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3425 cp
= strchr(destsnap
, '@');
3427 /* it's the containing fs that exists */
3430 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3431 "destination already exists"));
3432 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3433 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3438 if (flags
->resumable
)
3439 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3440 "kernel modules must be upgraded to "
3441 "receive this stream."));
3442 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3445 recv_ecksum_set_aux(hdl
, destsnap
, flags
->resumable
);
3446 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3449 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3450 "pool must be upgraded to receive this stream."));
3451 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3454 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3455 "destination %s space quota exceeded"), name
);
3456 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3459 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3464 * Mount the target filesystem (if created). Also mount any
3465 * children of the target filesystem if we did a replication
3466 * receive (indicated by stream_avl being non-NULL).
3468 cp
= strchr(destsnap
, '@');
3469 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3473 h
= zfs_open(hdl
, destsnap
,
3474 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3476 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3478 } else if (newfs
|| stream_avl
) {
3480 * Track the first/top of hierarchy fs,
3481 * for mounting and sharing later.
3483 if (top_zfs
&& *top_zfs
== NULL
)
3484 *top_zfs
= zfs_strdup(hdl
, destsnap
);
3492 err
|= changelist_postfix(clp
);
3493 changelist_free(clp
);
3496 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3497 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3498 "failed to clear unreceived properties on %s"), name
);
3499 (void) fprintf(stderr
, "\n");
3501 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3502 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3503 "failed to restore original properties on %s"), name
);
3504 (void) fprintf(stderr
, "\n");
3507 if (err
|| ioctl_err
) {
3512 if (flags
->verbose
) {
3515 uint64_t bytes
= read_bytes
;
3516 time_t delta
= time(NULL
) - begin_time
;
3519 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3520 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3522 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3528 if (prop_errors
!= NULL
)
3529 nvlist_free(prop_errors
);
3538 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
,
3539 const char *originsnap
, recvflags_t
*flags
, int infd
, const char *sendfs
,
3540 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
3541 uint64_t *action_handlep
, const char *finalsnap
)
3544 dmu_replay_record_t drr
, drr_noswap
;
3545 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3547 zio_cksum_t zcksum
= { { 0 } };
3548 uint64_t featureflags
;
3551 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3554 if (flags
->isprefix
&&
3555 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3556 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3557 "(%s) does not exist"), tosnap
);
3558 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3561 !zfs_dataset_exists(hdl
, originsnap
, ZFS_TYPE_DATASET
)) {
3562 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified origin fs "
3563 "(%s) does not exist"), originsnap
);
3564 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3567 /* read in the BEGIN record */
3568 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3572 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3573 /* It's the double end record at the end of a package */
3577 /* the kernel needs the non-byteswapped begin record */
3580 flags
->byteswap
= B_FALSE
;
3581 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3583 * We computed the checksum in the wrong byteorder in
3584 * recv_read() above; do it again correctly.
3586 bzero(&zcksum
, sizeof (zio_cksum_t
));
3587 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3588 flags
->byteswap
= B_TRUE
;
3590 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3591 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3592 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3593 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3594 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3595 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3596 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3597 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3598 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3601 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3602 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3603 "stream (bad magic number)"));
3604 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3607 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3608 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3610 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3611 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3612 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3613 "stream has unsupported feature, feature flags = %lx"),
3615 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3618 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3619 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3620 "stream (bad snapshot name)"));
3621 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3624 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3625 char nonpackage_sendfs
[ZFS_MAX_DATASET_NAME_LEN
];
3626 if (sendfs
== NULL
) {
3628 * We were not called from zfs_receive_package(). Get
3629 * the fs specified by 'zfs send'.
3632 (void) strlcpy(nonpackage_sendfs
,
3633 drr
.drr_u
.drr_begin
.drr_toname
,
3634 sizeof (nonpackage_sendfs
));
3635 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3637 sendfs
= nonpackage_sendfs
;
3638 VERIFY(finalsnap
== NULL
);
3640 return (zfs_receive_one(hdl
, infd
, tosnap
, originsnap
, flags
,
3641 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
, top_zfs
,
3642 cleanup_fd
, action_handlep
, finalsnap
));
3644 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3645 DMU_COMPOUNDSTREAM
);
3646 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
, &drr
,
3647 &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3652 * Restores a backup of tosnap from the file descriptor specified by infd.
3653 * Return 0 on total success, -2 if some things couldn't be
3654 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3655 * (-1 will override -2, if -1 and the resumable flag was specified the
3656 * transfer can be resumed if the sending side supports it).
3659 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, nvlist_t
*props
,
3660 recvflags_t
*flags
, int infd
, avl_tree_t
*stream_avl
)
3662 char *top_zfs
= NULL
;
3665 uint64_t action_handle
= 0;
3667 char *originsnap
= NULL
;
3670 * The only way fstat can fail is if we do not have a valid file
3673 if (fstat(infd
, &sb
) == -1) {
3679 #ifndef F_SETPIPE_SZ
3680 #define F_SETPIPE_SZ (F_SETLEASE + 7)
3681 #endif /* F_SETPIPE_SZ */
3683 #ifndef F_GETPIPE_SZ
3684 #define F_GETPIPE_SZ (F_GETLEASE + 7)
3685 #endif /* F_GETPIPE_SZ */
3688 * It is not uncommon for gigabytes to be processed in zfs receive.
3689 * Speculatively increase the buffer size via Linux-specific fcntl()
3692 if (S_ISFIFO(sb
.st_mode
)) {
3693 FILE *procf
= fopen("/proc/sys/fs/pipe-max-size", "r");
3695 if (procf
!= NULL
) {
3696 unsigned long max_psize
;
3698 if (fscanf(procf
, "%lu", &max_psize
) > 0) {
3699 cur_psize
= fcntl(infd
, F_GETPIPE_SZ
);
3700 if (cur_psize
> 0 &&
3701 max_psize
> (unsigned long) cur_psize
)
3702 (void) fcntl(infd
, F_SETPIPE_SZ
,
3708 #endif /* __linux__ */
3711 err
= nvlist_lookup_string(props
, "origin", &originsnap
);
3712 if (err
&& err
!= ENOENT
)
3716 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3717 VERIFY(cleanup_fd
>= 0);
3719 err
= zfs_receive_impl(hdl
, tosnap
, originsnap
, flags
, infd
, NULL
, NULL
,
3720 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
, NULL
);
3722 VERIFY(0 == close(cleanup_fd
));
3724 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3725 zfs_handle_t
*zhp
= NULL
;
3726 prop_changelist_t
*clp
= NULL
;
3728 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3730 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3731 CL_GATHER_MOUNT_ALWAYS
, 0);
3734 /* mount and share received datasets */
3735 err
= changelist_postfix(clp
);
3736 changelist_free(clp
);
3739 if (zhp
== NULL
|| clp
== NULL
|| err
)