4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, 2014 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
41 #include <sys/mount.h>
42 #include <sys/mntent.h>
43 #include <sys/mnttab.h>
45 #include <sys/debug.h>
53 #include <libzfs_core.h>
55 #include "zfs_namecheck.h"
57 #include "zfs_fletcher.h"
58 #include "libzfs_impl.h"
59 #include <sys/zio_checksum.h>
61 #include <sys/socket.h>
63 /* in libzfs_dataset.c */
64 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
66 static int zfs_receive_impl(libzfs_handle_t
*, const char *, recvflags_t
*,
67 int, const char *, nvlist_t
*, avl_tree_t
*, char **, int, uint64_t *);
69 static const zio_cksum_t zero_cksum
= { { 0 } };
71 typedef struct dedup_arg
{
74 libzfs_handle_t
*dedup_hdl
;
77 typedef struct progress_arg
{
80 boolean_t pa_parsable
;
83 typedef struct dataref
{
89 typedef struct dedup_entry
{
90 struct dedup_entry
*dde_next
;
91 zio_cksum_t dde_chksum
;
96 #define MAX_DDT_PHYSMEM_PERCENT 20
97 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
99 typedef struct dedup_table
{
100 dedup_entry_t
**dedup_hash_array
;
101 umem_cache_t
*ddecache
;
102 uint64_t max_ddt_size
; /* max dedup table size in bytes */
103 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
110 high_order_bit(uint64_t n
)
114 for (count
= 0; n
!= 0; count
++)
120 ssread(void *buf
, size_t len
, FILE *stream
)
124 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
131 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
132 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
136 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
137 if (ddt
->ddt_full
== B_FALSE
) {
138 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
139 "Dedup table full. Deduplication will continue "
140 "with existing table entries"));
141 ddt
->ddt_full
= B_TRUE
;
146 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
148 assert(*ddepp
== NULL
);
149 dde
->dde_next
= NULL
;
150 dde
->dde_chksum
= *cs
;
151 dde
->dde_prop
= prop
;
154 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
160 * Using the specified dedup table, do a lookup for an entry with
161 * the checksum cs. If found, return the block's reference info
162 * in *dr. Otherwise, insert a new entry in the dedup table, using
163 * the reference information specified by *dr.
165 * return value: true - entry was found
166 * false - entry was not found
169 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
170 uint64_t prop
, dataref_t
*dr
)
173 dedup_entry_t
**ddepp
;
175 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
177 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
178 ddepp
= &((*ddepp
)->dde_next
)) {
179 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
180 (*ddepp
)->dde_prop
== prop
) {
181 *dr
= (*ddepp
)->dde_ref
;
185 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
190 cksum_and_write(const void *buf
, uint64_t len
, zio_cksum_t
*zc
, int outfd
)
192 fletcher_4_incremental_native(buf
, len
, zc
);
193 return (write(outfd
, buf
, len
));
197 * This function is started in a separate thread when the dedup option
198 * has been requested. The main send thread determines the list of
199 * snapshots to be included in the send stream and makes the ioctl calls
200 * for each one. But instead of having the ioctl send the output to the
201 * the output fd specified by the caller of zfs_send()), the
202 * ioctl is told to direct the output to a pipe, which is read by the
203 * alternate thread running THIS function. This function does the
205 * 1. building a dedup table (the DDT)
206 * 2. doing checksums on each data block and inserting a record in the DDT
207 * 3. looking for matching checksums, and
208 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
209 * a duplicate block is found.
210 * The output of this function then goes to the output fd requested
211 * by the caller of zfs_send().
216 dedup_arg_t
*dda
= arg
;
217 char *buf
= zfs_alloc(dda
->dedup_hdl
, SPA_MAXBLOCKSIZE
);
218 dmu_replay_record_t thedrr
;
219 dmu_replay_record_t
*drr
= &thedrr
;
220 struct drr_begin
*drrb
= &thedrr
.drr_u
.drr_begin
;
221 struct drr_end
*drre
= &thedrr
.drr_u
.drr_end
;
222 struct drr_object
*drro
= &thedrr
.drr_u
.drr_object
;
223 struct drr_write
*drrw
= &thedrr
.drr_u
.drr_write
;
224 struct drr_spill
*drrs
= &thedrr
.drr_u
.drr_spill
;
225 struct drr_write_embedded
*drrwe
= &thedrr
.drr_u
.drr_write_embedded
;
228 dmu_replay_record_t wbr_drr
= {0};
229 struct drr_write_byref
*wbr_drrr
= &wbr_drr
.drr_u
.drr_write_byref
;
231 zio_cksum_t stream_cksum
;
232 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
236 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
)/100,
237 SMALLEST_POSSIBLE_MAX_DDT_MB
<<20);
239 numbuckets
= ddt
.max_ddt_size
/(sizeof (dedup_entry_t
));
242 * numbuckets must be a power of 2. Increase number to
243 * a power of 2 if necessary.
245 if (!ISP2(numbuckets
))
246 numbuckets
= 1 << high_order_bit(numbuckets
);
248 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
249 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
250 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
251 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
252 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
253 ddt
.ddt_full
= B_FALSE
;
255 /* Initialize the write-by-reference block. */
256 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
257 wbr_drr
.drr_payloadlen
= 0;
259 outfd
= dda
->outputfd
;
260 ofp
= fdopen(dda
->inputfd
, "r");
261 while (ssread(drr
, sizeof (dmu_replay_record_t
), ofp
) != 0) {
263 switch (drr
->drr_type
) {
267 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
269 /* set the DEDUP feature flag for this stream */
270 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
271 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
272 DMU_BACKUP_FEATURE_DEDUPPROPS
);
273 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
275 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
276 &stream_cksum
, outfd
) == -1)
278 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
279 DMU_COMPOUNDSTREAM
&& drr
->drr_payloadlen
!= 0) {
280 int sz
= drr
->drr_payloadlen
;
282 if (sz
> SPA_MAXBLOCKSIZE
) {
283 buf
= zfs_realloc(dda
->dedup_hdl
, buf
,
284 SPA_MAXBLOCKSIZE
, sz
);
286 (void) ssread(buf
, sz
, ofp
);
289 if (cksum_and_write(buf
, sz
, &stream_cksum
,
298 /* use the recalculated checksum */
299 ZIO_SET_CHECKSUM(&drre
->drr_checksum
,
300 stream_cksum
.zc_word
[0], stream_cksum
.zc_word
[1],
301 stream_cksum
.zc_word
[2], stream_cksum
.zc_word
[3]);
302 if ((write(outfd
, drr
,
303 sizeof (dmu_replay_record_t
))) == -1)
310 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
311 &stream_cksum
, outfd
) == -1)
313 if (drro
->drr_bonuslen
> 0) {
315 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
317 if (cksum_and_write(buf
,
318 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
319 &stream_cksum
, outfd
) == -1)
327 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
328 &stream_cksum
, outfd
) == -1)
330 (void) ssread(buf
, drrs
->drr_length
, ofp
);
331 if (cksum_and_write(buf
, drrs
->drr_length
,
332 &stream_cksum
, outfd
) == -1)
337 case DRR_FREEOBJECTS
:
339 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
340 &stream_cksum
, outfd
) == -1)
349 (void) ssread(buf
, drrw
->drr_length
, ofp
);
352 * Use the existing checksum if it's dedup-capable,
353 * else calculate a SHA256 checksum for it.
356 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
358 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
359 zio_cksum_t tmpsha256
;
361 zio_checksum_SHA256(buf
,
362 drrw
->drr_length
, &tmpsha256
);
364 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
365 BE_64(tmpsha256
.zc_word
[0]);
366 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
367 BE_64(tmpsha256
.zc_word
[1]);
368 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
369 BE_64(tmpsha256
.zc_word
[2]);
370 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
371 BE_64(tmpsha256
.zc_word
[3]);
372 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
373 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
376 dataref
.ref_guid
= drrw
->drr_toguid
;
377 dataref
.ref_object
= drrw
->drr_object
;
378 dataref
.ref_offset
= drrw
->drr_offset
;
380 if (ddt_update(dda
->dedup_hdl
, &ddt
,
381 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
383 /* block already present in stream */
384 wbr_drrr
->drr_object
= drrw
->drr_object
;
385 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
386 wbr_drrr
->drr_length
= drrw
->drr_length
;
387 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
388 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
389 wbr_drrr
->drr_refobject
=
391 wbr_drrr
->drr_refoffset
=
394 wbr_drrr
->drr_checksumtype
=
395 drrw
->drr_checksumtype
;
396 wbr_drrr
->drr_checksumflags
=
397 drrw
->drr_checksumtype
;
398 wbr_drrr
->drr_key
.ddk_cksum
=
399 drrw
->drr_key
.ddk_cksum
;
400 wbr_drrr
->drr_key
.ddk_prop
=
401 drrw
->drr_key
.ddk_prop
;
403 if (cksum_and_write(&wbr_drr
,
404 sizeof (dmu_replay_record_t
), &stream_cksum
,
408 /* block not previously seen */
409 if (cksum_and_write(drr
,
410 sizeof (dmu_replay_record_t
), &stream_cksum
,
413 if (cksum_and_write(buf
,
415 &stream_cksum
, outfd
) == -1)
421 case DRR_WRITE_EMBEDDED
:
423 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
424 &stream_cksum
, outfd
) == -1)
427 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8), ofp
);
428 if (cksum_and_write(buf
,
429 P2ROUNDUP((uint64_t)drrwe
->drr_psize
, 8),
430 &stream_cksum
, outfd
) == -1)
437 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
438 &stream_cksum
, outfd
) == -1)
444 (void) printf("INVALID record type 0x%x\n",
446 /* should never happen, so assert */
451 umem_cache_destroy(ddt
.ddecache
);
452 free(ddt
.dedup_hash_array
);
460 * Routines for dealing with the AVL tree of fs-nvlists
462 typedef struct fsavl_node
{
470 fsavl_compare(const void *arg1
, const void *arg2
)
472 const fsavl_node_t
*fn1
= arg1
;
473 const fsavl_node_t
*fn2
= arg2
;
475 if (fn1
->fn_guid
> fn2
->fn_guid
)
477 else if (fn1
->fn_guid
< fn2
->fn_guid
)
484 * Given the GUID of a snapshot, find its containing filesystem and
488 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
490 fsavl_node_t fn_find
;
493 fn_find
.fn_guid
= snapguid
;
495 fn
= avl_find(avl
, &fn_find
, NULL
);
498 *snapname
= fn
->fn_snapname
;
499 return (fn
->fn_nvfs
);
505 fsavl_destroy(avl_tree_t
*avl
)
514 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
521 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
524 fsavl_create(nvlist_t
*fss
)
527 nvpair_t
*fselem
= NULL
;
529 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
532 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
533 offsetof(fsavl_node_t
, fn_node
));
535 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
536 nvlist_t
*nvfs
, *snaps
;
537 nvpair_t
*snapelem
= NULL
;
539 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
540 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
543 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
547 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
548 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
549 fsavl_destroy(fsavl
);
553 fn
->fn_snapname
= nvpair_name(snapelem
);
557 * Note: if there are multiple snaps with the
558 * same GUID, we ignore all but one.
560 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
571 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
573 typedef struct send_data
{
574 uint64_t parent_fromsnap_guid
;
575 nvlist_t
*parent_snaps
;
578 const char *fromsnap
;
585 * The header nvlist is of the following format:
588 * "fromsnap" -> string (if incremental)
592 * "name" -> string (full name; for debugging)
593 * "parentfromsnap" -> number (guid of fromsnap in parent)
595 * "props" -> { name -> value (only if set here) }
596 * "snaps" -> { name (lastname) -> number (guid) }
597 * "snapprops" -> { name (lastname) -> { name -> value } }
599 * "origin" -> number (guid) (if clone)
600 * "sent" -> boolean (not on-disk)
608 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
611 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
613 send_data_t
*sd
= arg
;
614 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
617 boolean_t isfromsnap
, istosnap
, istosnapwithnofrom
;
619 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
620 isfromsnap
= (sd
->fromsnap
!= NULL
&&
621 strcmp(sd
->fromsnap
, snapname
) == 0);
622 istosnap
= (sd
->tosnap
!= NULL
&& (strcmp(sd
->tosnap
, snapname
) == 0));
623 istosnapwithnofrom
= (istosnap
&& sd
->fromsnap
== NULL
);
625 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
627 * NB: if there is no fromsnap here (it's a newly created fs in
628 * an incremental replication), we will substitute the tosnap.
630 if (isfromsnap
|| (sd
->parent_fromsnap_guid
== 0 && istosnap
)) {
631 sd
->parent_fromsnap_guid
= guid
;
634 if (!sd
->recursive
) {
635 if (!sd
->seenfrom
&& isfromsnap
) {
636 sd
->seenfrom
= B_TRUE
;
641 if ((sd
->seento
|| !sd
->seenfrom
) && !istosnapwithnofrom
) {
650 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
651 send_iterate_prop(zhp
, nv
);
652 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
660 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
662 nvpair_t
*elem
= NULL
;
664 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
665 char *propname
= nvpair_name(elem
);
666 zfs_prop_t prop
= zfs_name_to_prop(propname
);
669 if (!zfs_prop_user(propname
)) {
671 * Realistically, this should never happen. However,
672 * we want the ability to add DSL properties without
673 * needing to make incompatible version changes. We
674 * need to ignore unknown properties to allow older
675 * software to still send datasets containing these
676 * properties, with the unknown properties elided.
678 if (prop
== ZPROP_INVAL
)
681 if (zfs_prop_readonly(prop
))
685 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
686 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
687 prop
== ZFS_PROP_REFQUOTA
||
688 prop
== ZFS_PROP_REFRESERVATION
) {
691 verify(nvlist_lookup_uint64(propnv
,
692 ZPROP_VALUE
, &value
) == 0);
693 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
696 * May have no source before SPA_VERSION_RECVD_PROPS,
697 * but is still modifiable.
699 if (nvlist_lookup_string(propnv
,
700 ZPROP_SOURCE
, &source
) == 0) {
701 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
703 ZPROP_SOURCE_VAL_RECVD
) != 0))
708 if (nvlist_lookup_string(propnv
,
709 ZPROP_SOURCE
, &source
) != 0)
711 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
712 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
716 if (zfs_prop_user(propname
) ||
717 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
719 verify(nvlist_lookup_string(propnv
,
720 ZPROP_VALUE
, &value
) == 0);
721 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
724 verify(nvlist_lookup_uint64(propnv
,
725 ZPROP_VALUE
, &value
) == 0);
726 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
732 * recursively generate nvlists describing datasets. See comment
733 * for the data structure send_data_t above for description of contents
737 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
739 send_data_t
*sd
= arg
;
742 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
743 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
746 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
747 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
748 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
749 sd
->parent_fromsnap_guid
));
751 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
752 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
753 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
756 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
757 origin
->zfs_dmustats
.dds_guid
));
760 /* iterate over props */
761 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
762 send_iterate_prop(zhp
, nv
);
763 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
766 /* iterate over snaps, and set sd->parent_fromsnap_guid */
767 sd
->parent_fromsnap_guid
= 0;
768 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
769 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
770 (void) zfs_iter_snapshots_sorted(zhp
, send_iterate_snap
, sd
);
771 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
772 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
773 nvlist_free(sd
->parent_snaps
);
774 nvlist_free(sd
->snapprops
);
776 /* add this fs to nvlist */
777 (void) snprintf(guidstring
, sizeof (guidstring
),
778 "0x%llx", (longlong_t
)guid
);
779 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
782 /* iterate over children */
784 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
786 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
793 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
794 const char *tosnap
, boolean_t recursive
, nvlist_t
**nvlp
, avl_tree_t
**avlp
)
797 send_data_t sd
= { 0 };
800 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
802 return (EZFS_BADTYPE
);
804 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
805 sd
.fromsnap
= fromsnap
;
807 sd
.recursive
= recursive
;
809 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
817 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
828 * Routines specific to "zfs send"
830 typedef struct send_dump_data
{
831 /* these are all just the short snapname (the part after the @) */
832 const char *fromsnap
;
834 char prevsnap
[ZFS_MAXNAMELEN
];
835 uint64_t prevsnap_obj
;
836 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
837 boolean_t verbose
, dryrun
, parsable
, progress
, embed_data
, large_block
;
843 snapfilter_cb_t
*filter_cb
;
846 char holdtag
[ZFS_MAXNAMELEN
];
852 estimate_ioctl(zfs_handle_t
*zhp
, uint64_t fromsnap_obj
,
853 boolean_t fromorigin
, uint64_t *sizep
)
855 zfs_cmd_t zc
= {"\0"};
856 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
858 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
859 assert(fromsnap_obj
== 0 || !fromorigin
);
861 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
862 zc
.zc_obj
= fromorigin
;
863 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
864 zc
.zc_fromobj
= fromsnap_obj
;
865 zc
.zc_guid
= 1; /* estimate flag */
867 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
869 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
870 "warning: cannot estimate space for '%s'"), zhp
->zfs_name
);
874 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
875 "not an earlier snapshot from the same fs"));
876 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
879 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
880 ZFS_TYPE_SNAPSHOT
)) {
881 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
882 "incremental source (@%s) does not exist"),
885 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
898 zfs_error_aux(hdl
, strerror(errno
));
899 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
902 return (zfs_standard_error(hdl
, errno
, errbuf
));
906 *sizep
= zc
.zc_objset_type
;
912 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
913 * NULL) to the file descriptor specified by outfd.
916 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
917 boolean_t fromorigin
, int outfd
, enum lzc_send_flags flags
,
920 zfs_cmd_t zc
= {"\0"};
921 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
924 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
925 assert(fromsnap_obj
== 0 || !fromorigin
);
927 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
928 zc
.zc_cookie
= outfd
;
929 zc
.zc_obj
= fromorigin
;
930 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
931 zc
.zc_fromobj
= fromsnap_obj
;
934 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
935 if (fromsnap
&& fromsnap
[0] != '\0') {
936 VERIFY(0 == nvlist_add_string(thisdbg
,
937 "fromsnap", fromsnap
));
940 if (zfs_ioctl(zhp
->zfs_hdl
, ZFS_IOC_SEND
, &zc
) != 0) {
942 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
943 "warning: cannot send '%s'"), zhp
->zfs_name
);
945 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
947 VERIFY(0 == nvlist_add_nvlist(debugnv
,
948 zhp
->zfs_name
, thisdbg
));
950 nvlist_free(thisdbg
);
954 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
955 "not an earlier snapshot from the same fs"));
956 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
959 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
960 ZFS_TYPE_SNAPSHOT
)) {
961 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
962 "incremental source (@%s) does not exist"),
965 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
978 zfs_error_aux(hdl
, strerror(errno
));
979 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
982 return (zfs_standard_error(hdl
, errno
, errbuf
));
987 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
988 nvlist_free(thisdbg
);
994 gather_holds(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
996 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
999 * zfs_send() only sets snapholds for sends that need them,
1000 * e.g. replication and doall.
1002 if (sdd
->snapholds
== NULL
)
1005 fnvlist_add_string(sdd
->snapholds
, zhp
->zfs_name
, sdd
->holdtag
);
1009 send_progress_thread(void *arg
)
1011 progress_arg_t
*pa
= arg
;
1013 zfs_cmd_t zc
= {"\0"};
1014 zfs_handle_t
*zhp
= pa
->pa_zhp
;
1015 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1016 unsigned long long bytes
;
1022 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
1023 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
1025 if (!pa
->pa_parsable
)
1026 (void) fprintf(stderr
, "TIME SENT SNAPSHOT\n");
1029 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1034 zc
.zc_cookie
= pa
->pa_fd
;
1035 if (zfs_ioctl(hdl
, ZFS_IOC_SEND_PROGRESS
, &zc
) != 0)
1036 return ((void *)-1);
1040 bytes
= zc
.zc_cookie
;
1042 if (pa
->pa_parsable
) {
1043 (void) fprintf(stderr
, "%02d:%02d:%02d\t%llu\t%s\n",
1044 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1045 bytes
, zhp
->zfs_name
);
1047 zfs_nicenum(bytes
, buf
, sizeof (buf
));
1048 (void) fprintf(stderr
, "%02d:%02d:%02d %5s %s\n",
1049 tm
->tm_hour
, tm
->tm_min
, tm
->tm_sec
,
1050 buf
, zhp
->zfs_name
);
1056 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
1058 send_dump_data_t
*sdd
= arg
;
1059 progress_arg_t pa
= { 0 };
1063 boolean_t isfromsnap
, istosnap
, fromorigin
;
1064 boolean_t exclude
= B_FALSE
;
1067 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1068 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1069 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1071 if (!sdd
->seenfrom
&& isfromsnap
) {
1072 gather_holds(zhp
, sdd
);
1073 sdd
->seenfrom
= B_TRUE
;
1074 (void) strcpy(sdd
->prevsnap
, thissnap
);
1075 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1080 if (sdd
->seento
|| !sdd
->seenfrom
) {
1085 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1087 sdd
->seento
= B_TRUE
;
1089 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1090 if (sdd
->replicate
) {
1092 nvlist_t
*snapprops
;
1094 * Filter out all intermediate snapshots except origin
1095 * snapshots needed to replicate clones.
1097 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1098 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1100 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1101 "snapprops", &snapprops
));
1102 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1103 thissnap
, &snapprops
));
1104 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1111 * If a filter function exists, call it to determine whether
1112 * this snapshot will be sent.
1114 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1115 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1117 * This snapshot is filtered out. Don't send it, and don't
1118 * set prevsnap_obj, so it will be as if this snapshot didn't
1119 * exist, and the next accepted snapshot will be sent as
1120 * an incremental from the last accepted one, or as the
1121 * first (and full) snapshot in the case of a replication,
1122 * non-incremental send.
1128 gather_holds(zhp
, sdd
);
1129 fromorigin
= sdd
->prevsnap
[0] == '\0' &&
1130 (sdd
->fromorigin
|| sdd
->replicate
);
1134 err
= estimate_ioctl(zhp
, sdd
->prevsnap_obj
,
1137 if (sdd
->parsable
) {
1138 if (sdd
->prevsnap
[0] != '\0') {
1139 (void) fprintf(stderr
, "incremental\t%s\t%s",
1140 sdd
->prevsnap
, zhp
->zfs_name
);
1142 (void) fprintf(stderr
, "full\t%s",
1146 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1147 "send from @%s to %s"),
1148 sdd
->prevsnap
, zhp
->zfs_name
);
1151 if (sdd
->parsable
) {
1152 (void) fprintf(stderr
, "\t%llu\n",
1156 zfs_nicenum(size
, buf
, sizeof (buf
));
1157 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1158 " estimated size is %s\n"), buf
);
1162 (void) fprintf(stderr
, "\n");
1168 * If progress reporting is requested, spawn a new thread to
1169 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1171 if (sdd
->progress
) {
1173 pa
.pa_fd
= sdd
->outfd
;
1174 pa
.pa_parsable
= sdd
->parsable
;
1176 if ((err
= pthread_create(&tid
, NULL
,
1177 send_progress_thread
, &pa
))) {
1183 enum lzc_send_flags flags
= 0;
1184 if (sdd
->large_block
)
1185 flags
|= LZC_SEND_FLAG_LARGE_BLOCK
;
1186 if (sdd
->embed_data
)
1187 flags
|= LZC_SEND_FLAG_EMBED_DATA
;
1189 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1190 fromorigin
, sdd
->outfd
, flags
, sdd
->debugnv
);
1192 if (sdd
->progress
) {
1193 (void) pthread_cancel(tid
);
1194 (void) pthread_join(tid
, NULL
);
1198 (void) strcpy(sdd
->prevsnap
, thissnap
);
1199 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1205 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1208 send_dump_data_t
*sdd
= arg
;
1209 boolean_t missingfrom
= B_FALSE
;
1210 zfs_cmd_t zc
= {"\0"};
1212 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1213 zhp
->zfs_name
, sdd
->tosnap
);
1214 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1215 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1216 "WARNING: could not send %s@%s: does not exist\n"),
1217 zhp
->zfs_name
, sdd
->tosnap
);
1222 if (sdd
->replicate
&& sdd
->fromsnap
) {
1224 * If this fs does not have fromsnap, and we're doing
1225 * recursive, we need to send a full stream from the
1226 * beginning (or an incremental from the origin if this
1227 * is a clone). If we're doing non-recursive, then let
1228 * them get the error.
1230 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1231 zhp
->zfs_name
, sdd
->fromsnap
);
1232 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1233 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1234 missingfrom
= B_TRUE
;
1238 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1239 sdd
->prevsnap_obj
= 0;
1240 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1241 sdd
->seenfrom
= B_TRUE
;
1243 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1244 if (!sdd
->seenfrom
) {
1245 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1246 "WARNING: could not send %s@%s:\n"
1247 "incremental source (%s@%s) does not exist\n"),
1248 zhp
->zfs_name
, sdd
->tosnap
,
1249 zhp
->zfs_name
, sdd
->fromsnap
);
1251 } else if (!sdd
->seento
) {
1252 if (sdd
->fromsnap
) {
1253 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1254 "WARNING: could not send %s@%s:\n"
1255 "incremental source (%s@%s) "
1256 "is not earlier than it\n"),
1257 zhp
->zfs_name
, sdd
->tosnap
,
1258 zhp
->zfs_name
, sdd
->fromsnap
);
1260 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1262 "could not send %s@%s: does not exist\n"),
1263 zhp
->zfs_name
, sdd
->tosnap
);
1272 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1274 send_dump_data_t
*sdd
= arg
;
1276 boolean_t needagain
, progress
;
1278 if (!sdd
->replicate
)
1279 return (dump_filesystem(rzhp
, sdd
));
1281 /* Mark the clone origin snapshots. */
1282 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1283 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1285 uint64_t origin_guid
= 0;
1287 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1288 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1289 if (origin_guid
!= 0) {
1291 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1292 origin_guid
, &snapname
);
1293 if (origin_nv
!= NULL
) {
1294 nvlist_t
*snapprops
;
1295 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1296 "snapprops", &snapprops
));
1297 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1298 snapname
, &snapprops
));
1299 VERIFY(0 == nvlist_add_boolean(
1300 snapprops
, "is_clone_origin"));
1305 needagain
= progress
= B_FALSE
;
1306 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1307 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1308 nvlist_t
*fslist
, *parent_nv
;
1312 uint64_t origin_guid
= 0;
1313 uint64_t parent_guid
= 0;
1315 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1316 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1319 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1320 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1321 (void) nvlist_lookup_uint64(fslist
, "parentfromsnap",
1324 if (parent_guid
!= 0) {
1325 parent_nv
= fsavl_find(sdd
->fsavl
, parent_guid
, NULL
);
1326 if (!nvlist_exists(parent_nv
, "sent")) {
1327 /* parent has not been sent; skip this one */
1333 if (origin_guid
!= 0) {
1334 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1336 if (origin_nv
!= NULL
&&
1337 !nvlist_exists(origin_nv
, "sent")) {
1339 * origin has not been sent yet;
1347 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1350 err
= dump_filesystem(zhp
, sdd
);
1351 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1362 /* clean out the sent flags in case we reuse this fss */
1363 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1364 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1367 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1368 (void) nvlist_remove_all(fslist
, "sent");
1375 * Generate a send stream for the dataset identified by the argument zhp.
1377 * The content of the send stream is the snapshot identified by
1378 * 'tosnap'. Incremental streams are requested in two ways:
1379 * - from the snapshot identified by "fromsnap" (if non-null) or
1380 * - from the origin of the dataset identified by zhp, which must
1381 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1384 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1385 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1386 * if "replicate" is set. If "doall" is set, dump all the intermediate
1387 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1388 * case too. If "props" is set, send properties.
1391 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1392 sendflags_t
*flags
, int outfd
, snapfilter_cb_t filter_func
,
1393 void *cb_arg
, nvlist_t
**debugnvp
)
1396 send_dump_data_t sdd
= { 0 };
1398 nvlist_t
*fss
= NULL
;
1399 avl_tree_t
*fsavl
= NULL
;
1400 static uint64_t holdseq
;
1404 dedup_arg_t dda
= { 0 };
1405 int featureflags
= 0;
1407 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1408 "cannot send '%s'"), zhp
->zfs_name
);
1410 if (fromsnap
&& fromsnap
[0] == '\0') {
1411 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1412 "zero-length incremental source"));
1413 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1416 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1418 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1419 if (version
>= ZPL_VERSION_SA
) {
1420 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1424 if (flags
->dedup
&& !flags
->dryrun
) {
1425 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1426 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1427 if ((err
= socketpair(AF_UNIX
, SOCK_STREAM
, 0, pipefd
))) {
1428 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1429 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1432 dda
.outputfd
= outfd
;
1433 dda
.inputfd
= pipefd
[1];
1434 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1435 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1436 (void) close(pipefd
[0]);
1437 (void) close(pipefd
[1]);
1438 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1439 return (zfs_error(zhp
->zfs_hdl
,
1440 EZFS_THREADCREATEFAILED
, errbuf
));
1444 if (flags
->replicate
|| flags
->doall
|| flags
->props
) {
1445 dmu_replay_record_t drr
= { 0 };
1446 char *packbuf
= NULL
;
1448 zio_cksum_t zc
= { { 0 } };
1450 if (flags
->replicate
|| flags
->props
) {
1453 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1455 VERIFY(0 == nvlist_add_string(hdrnv
,
1456 "fromsnap", fromsnap
));
1458 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1459 if (!flags
->replicate
) {
1460 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1464 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1465 fromsnap
, tosnap
, flags
->replicate
, &fss
, &fsavl
);
1468 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1469 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1479 if (!flags
->dryrun
) {
1480 /* write first begin record */
1481 drr
.drr_type
= DRR_BEGIN
;
1482 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1483 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.
1484 drr_versioninfo
, DMU_COMPOUNDSTREAM
);
1485 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.
1486 drr_versioninfo
, featureflags
);
1487 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1488 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1489 "%s@%s", zhp
->zfs_name
, tosnap
);
1490 drr
.drr_payloadlen
= buflen
;
1491 err
= cksum_and_write(&drr
, sizeof (drr
), &zc
, outfd
);
1493 /* write header nvlist */
1494 if (err
!= -1 && packbuf
!= NULL
) {
1495 err
= cksum_and_write(packbuf
, buflen
, &zc
,
1504 /* write end record */
1505 bzero(&drr
, sizeof (drr
));
1506 drr
.drr_type
= DRR_END
;
1507 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1508 err
= write(outfd
, &drr
, sizeof (drr
));
1518 /* dump each stream */
1519 sdd
.fromsnap
= fromsnap
;
1520 sdd
.tosnap
= tosnap
;
1522 sdd
.outfd
= pipefd
[0];
1525 sdd
.replicate
= flags
->replicate
;
1526 sdd
.doall
= flags
->doall
;
1527 sdd
.fromorigin
= flags
->fromorigin
;
1530 sdd
.verbose
= flags
->verbose
;
1531 sdd
.parsable
= flags
->parsable
;
1532 sdd
.progress
= flags
->progress
;
1533 sdd
.dryrun
= flags
->dryrun
;
1534 sdd
.large_block
= flags
->largeblock
;
1535 sdd
.embed_data
= flags
->embed_data
;
1536 sdd
.filter_cb
= filter_func
;
1537 sdd
.filter_cb_arg
= cb_arg
;
1539 sdd
.debugnv
= *debugnvp
;
1542 * Some flags require that we place user holds on the datasets that are
1543 * being sent so they don't get destroyed during the send. We can skip
1544 * this step if the pool is imported read-only since the datasets cannot
1547 if (!flags
->dryrun
&& !zpool_get_prop_int(zfs_get_pool_handle(zhp
),
1548 ZPOOL_PROP_READONLY
, NULL
) &&
1549 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1550 spa_version
>= SPA_VERSION_USERREFS
&&
1551 (flags
->doall
|| flags
->replicate
)) {
1553 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1554 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1555 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1556 if (sdd
.cleanup_fd
< 0) {
1560 sdd
.snapholds
= fnvlist_alloc();
1562 sdd
.cleanup_fd
= -1;
1563 sdd
.snapholds
= NULL
;
1565 if (flags
->verbose
|| sdd
.snapholds
!= NULL
) {
1567 * Do a verbose no-op dry run to get all the verbose output
1568 * or to gather snapshot hold's before generating any data,
1569 * then do a non-verbose real run to generate the streams.
1571 sdd
.dryrun
= B_TRUE
;
1572 err
= dump_filesystems(zhp
, &sdd
);
1577 if (flags
->verbose
) {
1578 if (flags
->parsable
) {
1579 (void) fprintf(stderr
, "size\t%llu\n",
1580 (longlong_t
)sdd
.size
);
1583 zfs_nicenum(sdd
.size
, buf
, sizeof (buf
));
1584 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
1585 "total estimated size is %s\n"), buf
);
1589 /* Ensure no snaps found is treated as an error. */
1595 /* Skip the second run if dryrun was requested. */
1599 if (sdd
.snapholds
!= NULL
) {
1600 err
= zfs_hold_nvl(zhp
, sdd
.cleanup_fd
, sdd
.snapholds
);
1604 fnvlist_free(sdd
.snapholds
);
1605 sdd
.snapholds
= NULL
;
1608 sdd
.dryrun
= B_FALSE
;
1609 sdd
.verbose
= B_FALSE
;
1612 err
= dump_filesystems(zhp
, &sdd
);
1613 fsavl_destroy(fsavl
);
1616 /* Ensure no snaps found is treated as an error. */
1617 if (err
== 0 && !sdd
.seento
)
1622 (void) pthread_cancel(tid
);
1623 (void) close(pipefd
[0]);
1624 (void) pthread_join(tid
, NULL
);
1627 if (sdd
.cleanup_fd
!= -1) {
1628 VERIFY(0 == close(sdd
.cleanup_fd
));
1629 sdd
.cleanup_fd
= -1;
1632 if (!flags
->dryrun
&& (flags
->replicate
|| flags
->doall
||
1635 * write final end record. NB: want to do this even if
1636 * there was some error, because it might not be totally
1639 dmu_replay_record_t drr
= { 0 };
1640 drr
.drr_type
= DRR_END
;
1641 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1642 return (zfs_standard_error(zhp
->zfs_hdl
,
1647 return (err
|| sdd
.err
);
1650 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1652 fsavl_destroy(fsavl
);
1654 fnvlist_free(sdd
.snapholds
);
1656 if (sdd
.cleanup_fd
!= -1)
1657 VERIFY(0 == close(sdd
.cleanup_fd
));
1659 (void) pthread_cancel(tid
);
1660 (void) close(pipefd
[0]);
1661 (void) pthread_join(tid
, NULL
);
1667 zfs_send_one(zfs_handle_t
*zhp
, const char *from
, int fd
,
1668 enum lzc_send_flags flags
)
1671 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
1674 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1675 "warning: cannot send '%s'"), zhp
->zfs_name
);
1677 err
= lzc_send(zhp
->zfs_name
, from
, fd
, flags
);
1681 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1682 "not an earlier snapshot from the same fs"));
1683 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
1687 if (lzc_exists(zhp
->zfs_name
)) {
1688 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1689 "incremental source (%s) does not exist"),
1692 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
1695 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1696 "target is busy; if a filesystem, "
1697 "it must not be mounted"));
1698 return (zfs_error(hdl
, EZFS_BUSY
, errbuf
));
1711 zfs_error_aux(hdl
, strerror(errno
));
1712 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
1715 return (zfs_standard_error(hdl
, errno
, errbuf
));
1722 * Routines specific to "zfs recv"
1726 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1727 boolean_t byteswap
, zio_cksum_t
*zc
)
1734 rv
= read(fd
, cp
, len
);
1739 if (rv
< 0 || len
!= 0) {
1740 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1741 "failed to read from stream"));
1742 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
1743 "cannot receive")));
1748 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
1750 fletcher_4_incremental_native(buf
, ilen
, zc
);
1756 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
1757 boolean_t byteswap
, zio_cksum_t
*zc
)
1762 buf
= zfs_alloc(hdl
, len
);
1766 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
1772 err
= nvlist_unpack(buf
, len
, nvp
, 0);
1775 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
1776 "stream (malformed nvlist)"));
1783 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
1784 int baselen
, char *newname
, recvflags_t
*flags
)
1787 zfs_cmd_t zc
= {"\0"};
1789 prop_changelist_t
*clp
;
1792 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1795 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1796 flags
->force
? MS_FORCE
: 0);
1800 err
= changelist_prefix(clp
);
1804 zc
.zc_objset_type
= DMU_OST_ZFS
;
1805 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1808 (void) strcpy(newname
, tryname
);
1810 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
1812 if (flags
->verbose
) {
1813 (void) printf("attempting rename %s to %s\n",
1814 zc
.zc_name
, zc
.zc_value
);
1816 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1818 changelist_rename(clp
, name
, tryname
);
1823 if (err
!= 0 && strncmp(name
+ baselen
, "recv-", 5) != 0) {
1826 (void) snprintf(newname
, ZFS_MAXNAMELEN
, "%.*srecv-%u-%u",
1827 baselen
, name
, getpid(), seq
);
1828 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
1830 if (flags
->verbose
) {
1831 (void) printf("failed - trying rename %s to %s\n",
1832 zc
.zc_name
, zc
.zc_value
);
1834 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1836 changelist_rename(clp
, name
, newname
);
1837 if (err
&& flags
->verbose
) {
1838 (void) printf("failed (%u) - "
1839 "will try again on next pass\n", errno
);
1842 } else if (flags
->verbose
) {
1844 (void) printf("success\n");
1846 (void) printf("failed (%u)\n", errno
);
1849 (void) changelist_postfix(clp
);
1850 changelist_free(clp
);
1856 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
1857 char *newname
, recvflags_t
*flags
)
1859 zfs_cmd_t zc
= {"\0"};
1861 prop_changelist_t
*clp
;
1863 boolean_t defer
= B_FALSE
;
1866 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1869 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1870 flags
->force
? MS_FORCE
: 0);
1871 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
1872 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1873 spa_version
>= SPA_VERSION_USERREFS
)
1878 err
= changelist_prefix(clp
);
1882 zc
.zc_objset_type
= DMU_OST_ZFS
;
1883 zc
.zc_defer_destroy
= defer
;
1884 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1887 (void) printf("attempting destroy %s\n", zc
.zc_name
);
1888 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
1891 (void) printf("success\n");
1892 changelist_remove(clp
, zc
.zc_name
);
1895 (void) changelist_postfix(clp
);
1896 changelist_free(clp
);
1899 * Deferred destroy might destroy the snapshot or only mark it to be
1900 * destroyed later, and it returns success in either case.
1902 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
1903 ZFS_TYPE_SNAPSHOT
))) {
1904 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
1910 typedef struct guid_to_name_data
{
1914 } guid_to_name_data_t
;
1917 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
1919 guid_to_name_data_t
*gtnd
= arg
;
1922 if (gtnd
->skip
!= NULL
&&
1923 strcmp(zhp
->zfs_name
, gtnd
->skip
) == 0) {
1927 if (zhp
->zfs_dmustats
.dds_guid
== gtnd
->guid
) {
1928 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
1933 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
1939 * Attempt to find the local dataset associated with this guid. In the case of
1940 * multiple matches, we attempt to find the "best" match by searching
1941 * progressively larger portions of the hierarchy. This allows one to send a
1942 * tree of datasets individually and guarantee that we will find the source
1943 * guid within that hierarchy, even if there are multiple matches elsewhere.
1946 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
1949 /* exhaustive search all local snapshots */
1950 char pname
[ZFS_MAXNAMELEN
];
1951 guid_to_name_data_t gtnd
;
1960 (void) strlcpy(pname
, parent
, sizeof (pname
));
1963 * Search progressively larger portions of the hierarchy. This will
1964 * select the "most local" version of the origin snapshot in the case
1965 * that there are multiple matching snapshots in the system.
1967 while ((cp
= strrchr(pname
, '/')) != NULL
) {
1969 /* Chop off the last component and open the parent */
1971 zhp
= make_dataset_handle(hdl
, pname
);
1976 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
1982 * Remember the dataset that we already searched, so we
1983 * skip it next time through.
1992 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1993 * guid1 is after guid2.
1996 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
1997 uint64_t guid1
, uint64_t guid2
)
2000 char *fsname
, *snapname
;
2001 char buf
[ZFS_MAXNAMELEN
];
2003 zfs_handle_t
*guid1hdl
, *guid2hdl
;
2004 uint64_t create1
, create2
;
2011 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
2012 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2013 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2014 guid1hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2015 if (guid1hdl
== NULL
)
2018 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
2019 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2020 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
2021 guid2hdl
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
2022 if (guid2hdl
== NULL
) {
2023 zfs_close(guid1hdl
);
2027 create1
= zfs_prop_get_int(guid1hdl
, ZFS_PROP_CREATETXG
);
2028 create2
= zfs_prop_get_int(guid2hdl
, ZFS_PROP_CREATETXG
);
2030 if (create1
< create2
)
2032 else if (create1
> create2
)
2037 zfs_close(guid1hdl
);
2038 zfs_close(guid2hdl
);
2044 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
2045 recvflags_t
*flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2048 nvlist_t
*local_nv
, *deleted
= NULL
;
2049 avl_tree_t
*local_avl
;
2050 nvpair_t
*fselem
, *nextfselem
;
2052 char newname
[ZFS_MAXNAMELEN
];
2055 boolean_t needagain
, progress
, recursive
;
2058 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
2060 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2067 needagain
= progress
= B_FALSE
;
2069 VERIFY(0 == nvlist_alloc(&deleted
, NV_UNIQUE_NAME
, 0));
2071 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
2072 recursive
, &local_nv
, &local_avl
)) != 0)
2076 * Process deletes and renames
2078 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
2079 fselem
; fselem
= nextfselem
) {
2080 nvlist_t
*nvfs
, *snaps
;
2081 nvlist_t
*stream_nvfs
= NULL
;
2082 nvpair_t
*snapelem
, *nextsnapelem
;
2083 uint64_t fromguid
= 0;
2084 uint64_t originguid
= 0;
2085 uint64_t stream_originguid
= 0;
2086 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
2087 char *fsname
, *stream_fsname
;
2089 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
2091 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
2092 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
2093 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
2094 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
2095 &parent_fromsnap_guid
));
2096 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
2099 * First find the stream's fs, so we can check for
2100 * a different origin (due to "zfs promote")
2102 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2103 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
2106 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2107 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
2109 if (stream_nvfs
!= NULL
)
2113 /* check for promote */
2114 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
2115 &stream_originguid
);
2116 if (stream_nvfs
&& originguid
!= stream_originguid
) {
2117 switch (created_before(hdl
, local_avl
,
2118 stream_originguid
, originguid
)) {
2121 zfs_cmd_t zc
= {"\0"};
2122 nvlist_t
*origin_nvfs
;
2123 char *origin_fsname
;
2126 (void) printf("promoting %s\n", fsname
);
2128 origin_nvfs
= fsavl_find(local_avl
, originguid
,
2130 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
2131 "name", &origin_fsname
));
2132 (void) strlcpy(zc
.zc_value
, origin_fsname
,
2133 sizeof (zc
.zc_value
));
2134 (void) strlcpy(zc
.zc_name
, fsname
,
2135 sizeof (zc
.zc_name
));
2136 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
2144 fsavl_destroy(local_avl
);
2145 nvlist_free(local_nv
);
2149 * We had/have the wrong origin, therefore our
2150 * list of snapshots is wrong. Need to handle
2151 * them on the next pass.
2157 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
2158 snapelem
; snapelem
= nextsnapelem
) {
2160 char *stream_snapname
;
2161 nvlist_t
*found
, *props
;
2163 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
2165 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
2166 found
= fsavl_find(stream_avl
, thisguid
,
2169 /* check for delete */
2170 if (found
== NULL
) {
2171 char name
[ZFS_MAXNAMELEN
];
2176 (void) snprintf(name
, sizeof (name
), "%s@%s",
2177 fsname
, nvpair_name(snapelem
));
2179 error
= recv_destroy(hdl
, name
,
2180 strlen(fsname
)+1, newname
, flags
);
2185 sprintf(guidname
, "%llu",
2186 (u_longlong_t
)thisguid
);
2187 nvlist_add_boolean(deleted
, guidname
);
2191 stream_nvfs
= found
;
2193 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
2194 &props
) && 0 == nvlist_lookup_nvlist(props
,
2195 stream_snapname
, &props
)) {
2196 zfs_cmd_t zc
= {"\0"};
2198 zc
.zc_cookie
= B_TRUE
; /* received */
2199 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
2200 "%s@%s", fsname
, nvpair_name(snapelem
));
2201 if (zcmd_write_src_nvlist(hdl
, &zc
,
2203 (void) zfs_ioctl(hdl
,
2204 ZFS_IOC_SET_PROP
, &zc
);
2205 zcmd_free_nvlists(&zc
);
2209 /* check for different snapname */
2210 if (strcmp(nvpair_name(snapelem
),
2211 stream_snapname
) != 0) {
2212 char name
[ZFS_MAXNAMELEN
];
2213 char tryname
[ZFS_MAXNAMELEN
];
2215 (void) snprintf(name
, sizeof (name
), "%s@%s",
2216 fsname
, nvpair_name(snapelem
));
2217 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
2218 fsname
, stream_snapname
);
2220 error
= recv_rename(hdl
, name
, tryname
,
2221 strlen(fsname
)+1, newname
, flags
);
2228 if (strcmp(stream_snapname
, fromsnap
) == 0)
2229 fromguid
= thisguid
;
2232 /* check for delete */
2233 if (stream_nvfs
== NULL
) {
2237 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
2243 sprintf(guidname
, "%llu",
2244 (u_longlong_t
) parent_fromsnap_guid
);
2245 nvlist_add_boolean(deleted
, guidname
);
2249 if (fromguid
== 0) {
2250 if (flags
->verbose
) {
2251 (void) printf("local fs %s does not have "
2252 "fromsnap (%s in stream); must have "
2253 "been deleted locally; ignoring\n",
2259 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
2260 "name", &stream_fsname
));
2261 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
2262 "parentfromsnap", &stream_parent_fromsnap_guid
));
2264 s1
= strrchr(fsname
, '/');
2265 s2
= strrchr(stream_fsname
, '/');
2268 * Check if we're going to rename based on parent guid change
2269 * and the current parent guid was also deleted. If it was then
2270 * rename will fail and is likely unneeded, so avoid this and
2271 * force an early retry to determine the new
2272 * parent_fromsnap_guid.
2274 if (stream_parent_fromsnap_guid
!= 0 &&
2275 parent_fromsnap_guid
!= 0 &&
2276 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) {
2277 sprintf(guidname
, "%llu",
2278 (u_longlong_t
) parent_fromsnap_guid
);
2279 if (nvlist_exists(deleted
, guidname
)) {
2287 * Check for rename. If the exact receive path is specified, it
2288 * does not count as a rename, but we still need to check the
2289 * datasets beneath it.
2291 if ((stream_parent_fromsnap_guid
!= 0 &&
2292 parent_fromsnap_guid
!= 0 &&
2293 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
2294 ((flags
->isprefix
|| strcmp(tofs
, fsname
) != 0) &&
2295 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
2297 char tryname
[ZFS_MAXNAMELEN
];
2299 parent
= fsavl_find(local_avl
,
2300 stream_parent_fromsnap_guid
, NULL
);
2302 * NB: parent might not be found if we used the
2303 * tosnap for stream_parent_fromsnap_guid,
2304 * because the parent is a newly-created fs;
2305 * we'll be able to rename it after we recv the
2308 if (parent
!= NULL
) {
2311 VERIFY(0 == nvlist_lookup_string(parent
, "name",
2313 (void) snprintf(tryname
, sizeof (tryname
),
2314 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2317 if (flags
->verbose
) {
2318 (void) printf("local fs %s new parent "
2319 "not found\n", fsname
);
2325 error
= recv_rename(hdl
, fsname
, tryname
,
2326 strlen(tofs
)+1, newname
, flags
);
2328 if (renamed
!= NULL
&& newname
[0] != '\0') {
2329 VERIFY(0 == nvlist_add_boolean(renamed
,
2341 fsavl_destroy(local_avl
);
2342 nvlist_free(local_nv
);
2343 nvlist_free(deleted
);
2345 if (needagain
&& progress
) {
2346 /* do another pass to fix up temporary names */
2348 (void) printf("another pass:\n");
2356 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2357 recvflags_t
*flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2358 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2360 nvlist_t
*stream_nv
= NULL
;
2361 avl_tree_t
*stream_avl
= NULL
;
2362 char *fromsnap
= NULL
;
2364 char tofs
[ZFS_MAXNAMELEN
];
2365 char sendfs
[ZFS_MAXNAMELEN
];
2367 dmu_replay_record_t drre
;
2369 boolean_t anyerr
= B_FALSE
;
2370 boolean_t softerr
= B_FALSE
;
2371 boolean_t recursive
;
2373 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2376 assert(drr
->drr_type
== DRR_BEGIN
);
2377 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2378 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2379 DMU_COMPOUNDSTREAM
);
2382 * Read in the nvlist from the stream.
2384 if (drr
->drr_payloadlen
!= 0) {
2385 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2386 &stream_nv
, flags
->byteswap
, zc
);
2388 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2393 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2396 if (recursive
&& strchr(destname
, '@')) {
2397 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2398 "cannot specify snapshot name for multi-snapshot stream"));
2399 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2404 * Read in the end record and verify checksum.
2406 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2407 flags
->byteswap
, NULL
)))
2409 if (flags
->byteswap
) {
2410 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2411 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2412 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2413 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2414 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2415 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2416 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2417 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2418 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2420 if (drre
.drr_type
!= DRR_END
) {
2421 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2424 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2425 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2426 "incorrect header checksum"));
2427 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2431 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2433 if (drr
->drr_payloadlen
!= 0) {
2434 nvlist_t
*stream_fss
;
2436 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2438 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2439 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2440 "couldn't allocate avl tree"));
2441 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2445 if (fromsnap
!= NULL
) {
2446 nvlist_t
*renamed
= NULL
;
2447 nvpair_t
*pair
= NULL
;
2449 (void) strlcpy(tofs
, destname
, ZFS_MAXNAMELEN
);
2450 if (flags
->isprefix
) {
2451 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2454 if (flags
->istail
) {
2455 cp
= strrchr(drrb
->drr_toname
, '/');
2457 (void) strlcat(tofs
, "/",
2461 i
= (cp
- drrb
->drr_toname
);
2464 i
= strcspn(drrb
->drr_toname
, "/@");
2466 /* zfs_receive_one() will create_parents() */
2467 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2469 *strchr(tofs
, '@') = '\0';
2472 if (recursive
&& !flags
->dryrun
&& !flags
->nomount
) {
2473 VERIFY(0 == nvlist_alloc(&renamed
,
2474 NV_UNIQUE_NAME
, 0));
2477 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2478 stream_nv
, stream_avl
, renamed
);
2480 /* Unmount renamed filesystems before receiving. */
2481 while ((pair
= nvlist_next_nvpair(renamed
,
2484 prop_changelist_t
*clp
= NULL
;
2486 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2487 ZFS_TYPE_FILESYSTEM
);
2489 clp
= changelist_gather(zhp
,
2490 ZFS_PROP_MOUNTPOINT
, 0, 0);
2494 changelist_prefix(clp
);
2495 changelist_free(clp
);
2500 nvlist_free(renamed
);
2505 * Get the fs specified by the first path in the stream (the top level
2506 * specified by 'zfs send') and pass it to each invocation of
2507 * zfs_receive_one().
2509 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2511 if ((cp
= strchr(sendfs
, '@')) != NULL
)
2514 /* Finally, receive each contained stream */
2517 * we should figure out if it has a recoverable
2518 * error, in which case do a recv_skip() and drive on.
2519 * Note, if we fail due to already having this guid,
2520 * zfs_receive_one() will take care of it (ie,
2521 * recv_skip() and return 0).
2523 error
= zfs_receive_impl(hdl
, destname
, flags
, fd
,
2524 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2526 if (error
== ENODATA
) {
2531 } while (error
== 0);
2533 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2535 * Now that we have the fs's they sent us, try the
2538 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2539 stream_nv
, stream_avl
, NULL
);
2543 fsavl_destroy(stream_avl
);
2545 nvlist_free(stream_nv
);
2554 trunc_prop_errs(int truncated
)
2556 ASSERT(truncated
!= 0);
2559 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2560 "1 more property could not be set\n"));
2562 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2563 "%d more properties could not be set\n"), truncated
);
2567 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2569 dmu_replay_record_t
*drr
;
2570 void *buf
= zfs_alloc(hdl
, SPA_MAXBLOCKSIZE
);
2573 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2574 "cannot receive:"));
2576 /* XXX would be great to use lseek if possible... */
2579 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2580 byteswap
, NULL
) == 0) {
2582 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2584 switch (drr
->drr_type
) {
2586 /* NB: not to be used on v2 stream packages */
2587 if (drr
->drr_payloadlen
!= 0) {
2588 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2589 "invalid substream header"));
2590 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2600 drr
->drr_u
.drr_object
.drr_bonuslen
=
2601 BSWAP_32(drr
->drr_u
.drr_object
.
2604 (void) recv_read(hdl
, fd
, buf
,
2605 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2611 drr
->drr_u
.drr_write
.drr_length
=
2612 BSWAP_64(drr
->drr_u
.drr_write
.drr_length
);
2614 (void) recv_read(hdl
, fd
, buf
,
2615 drr
->drr_u
.drr_write
.drr_length
, B_FALSE
, NULL
);
2619 drr
->drr_u
.drr_write
.drr_length
=
2620 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2622 (void) recv_read(hdl
, fd
, buf
,
2623 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2625 case DRR_WRITE_EMBEDDED
:
2627 drr
->drr_u
.drr_write_embedded
.drr_psize
=
2628 BSWAP_32(drr
->drr_u
.drr_write_embedded
.
2631 (void) recv_read(hdl
, fd
, buf
,
2632 P2ROUNDUP(drr
->drr_u
.drr_write_embedded
.drr_psize
,
2635 case DRR_WRITE_BYREF
:
2636 case DRR_FREEOBJECTS
:
2641 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2642 "invalid record type"));
2643 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2652 * Restores a backup of tosnap from the file descriptor specified by infd.
2655 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2656 recvflags_t
*flags
, dmu_replay_record_t
*drr
,
2657 dmu_replay_record_t
*drr_noswap
, const char *sendfs
,
2658 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2659 uint64_t *action_handlep
)
2661 zfs_cmd_t zc
= {"\0"};
2663 int ioctl_err
, ioctl_errno
, err
;
2665 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2667 char prop_errbuf
[1024];
2668 const char *chopprefix
;
2669 boolean_t newfs
= B_FALSE
;
2670 boolean_t stream_wantsnewfs
;
2671 uint64_t parent_snapguid
= 0;
2672 prop_changelist_t
*clp
= NULL
;
2673 nvlist_t
*snapprops_nvlist
= NULL
;
2674 zprop_errflags_t prop_errflags
;
2675 boolean_t recursive
;
2677 begin_time
= time(NULL
);
2679 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2682 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2685 if (stream_avl
!= NULL
) {
2687 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
2692 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
2694 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
2696 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
2698 if (flags
->canmountoff
) {
2699 VERIFY(0 == nvlist_add_uint64(props
,
2700 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
2702 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
2712 * Determine how much of the snapshot name stored in the stream
2713 * we are going to tack on to the name they specified on the
2714 * command line, and how much we are going to chop off.
2716 * If they specified a snapshot, chop the entire name stored in
2719 if (flags
->istail
) {
2721 * A filesystem was specified with -e. We want to tack on only
2722 * the tail of the sent snapshot path.
2724 if (strchr(tosnap
, '@')) {
2725 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2726 "argument - snapshot not allowed with -e"));
2727 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2730 chopprefix
= strrchr(sendfs
, '/');
2732 if (chopprefix
== NULL
) {
2734 * The tail is the poolname, so we need to
2735 * prepend a path separator.
2737 int len
= strlen(drrb
->drr_toname
);
2738 cp
= malloc(len
+ 2);
2740 (void) strcpy(&cp
[1], drrb
->drr_toname
);
2743 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
2745 } else if (flags
->isprefix
) {
2747 * A filesystem was specified with -d. We want to tack on
2748 * everything but the first element of the sent snapshot path
2749 * (all but the pool name).
2751 if (strchr(tosnap
, '@')) {
2752 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2753 "argument - snapshot not allowed with -d"));
2754 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2757 chopprefix
= strchr(drrb
->drr_toname
, '/');
2758 if (chopprefix
== NULL
)
2759 chopprefix
= strchr(drrb
->drr_toname
, '@');
2760 } else if (strchr(tosnap
, '@') == NULL
) {
2762 * If a filesystem was specified without -d or -e, we want to
2763 * tack on everything after the fs specified by 'zfs send'.
2765 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
2767 /* A snapshot was specified as an exact path (no -d or -e). */
2769 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2770 "cannot specify snapshot name for multi-snapshot "
2772 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2774 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
2777 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
2778 ASSERT(chopprefix
> drrb
->drr_toname
);
2779 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
2780 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
2781 chopprefix
[0] == '\0');
2784 * Determine name of destination snapshot, store in zc_value.
2786 (void) strcpy(zc
.zc_value
, tosnap
);
2787 (void) strlcat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
2789 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
2790 zcmd_free_nvlists(&zc
);
2791 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2795 * Determine the name of the origin snapshot, store in zc_string.
2797 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
2798 if (guid_to_name(hdl
, zc
.zc_value
,
2799 drrb
->drr_fromguid
, zc
.zc_string
) != 0) {
2800 zcmd_free_nvlists(&zc
);
2801 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2802 "local origin for clone %s does not exist"),
2804 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2807 (void) printf("found clone origin %s\n", zc
.zc_string
);
2810 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
2811 (drrb
->drr_flags
& DRR_FLAG_CLONE
));
2813 if (stream_wantsnewfs
) {
2815 * if the parent fs does not exist, look for it based on
2816 * the parent snap GUID
2818 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2819 "cannot receive new filesystem stream"));
2821 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2822 cp
= strrchr(zc
.zc_name
, '/');
2826 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2827 char suffix
[ZFS_MAXNAMELEN
];
2828 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
2829 if (guid_to_name(hdl
, zc
.zc_name
, parent_snapguid
,
2830 zc
.zc_value
) == 0) {
2831 *strchr(zc
.zc_value
, '@') = '\0';
2832 (void) strcat(zc
.zc_value
, suffix
);
2837 * if the fs does not exist, look for it based on the
2840 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2841 "cannot receive incremental stream"));
2843 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2844 *strchr(zc
.zc_name
, '@') = '\0';
2847 * If the exact receive path was specified and this is the
2848 * topmost path in the stream, then if the fs does not exist we
2849 * should look no further.
2851 if ((flags
->isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
2852 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
2853 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2854 char snap
[ZFS_MAXNAMELEN
];
2855 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
2856 if (guid_to_name(hdl
, zc
.zc_name
, drrb
->drr_fromguid
,
2857 zc
.zc_value
) == 0) {
2858 *strchr(zc
.zc_value
, '@') = '\0';
2859 (void) strcat(zc
.zc_value
, snap
);
2864 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2865 *strchr(zc
.zc_name
, '@') = '\0';
2867 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2871 * Destination fs exists. Therefore this should either
2872 * be an incremental, or the stream specifies a new fs
2873 * (full stream or clone) and they want us to blow it
2874 * away (and have therefore specified -F and removed any
2877 if (stream_wantsnewfs
) {
2878 if (!flags
->force
) {
2879 zcmd_free_nvlists(&zc
);
2880 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2881 "destination '%s' exists\n"
2882 "must specify -F to overwrite it"),
2884 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2886 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2888 zcmd_free_nvlists(&zc
);
2889 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2890 "destination has snapshots (eg. %s)\n"
2891 "must destroy them to overwrite it"),
2893 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2897 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
2898 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
2899 zcmd_free_nvlists(&zc
);
2903 if (stream_wantsnewfs
&&
2904 zhp
->zfs_dmustats
.dds_origin
[0]) {
2905 zcmd_free_nvlists(&zc
);
2907 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2908 "destination '%s' is a clone\n"
2909 "must destroy it to overwrite it"),
2911 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2914 if (!flags
->dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
2915 stream_wantsnewfs
) {
2916 /* We can't do online recv in this case */
2917 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
2920 zcmd_free_nvlists(&zc
);
2923 if (changelist_prefix(clp
) != 0) {
2924 changelist_free(clp
);
2926 zcmd_free_nvlists(&zc
);
2933 * Destination filesystem does not exist. Therefore we better
2934 * be creating a new filesystem (either from a full backup, or
2935 * a clone). It would therefore be invalid if the user
2936 * specified only the pool name (i.e. if the destination name
2937 * contained no slash character).
2939 if (!stream_wantsnewfs
||
2940 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
2941 zcmd_free_nvlists(&zc
);
2942 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2943 "destination '%s' does not exist"), zc
.zc_name
);
2944 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2948 * Trim off the final dataset component so we perform the
2949 * recvbackup ioctl to the filesystems's parent.
2953 if (flags
->isprefix
&& !flags
->istail
&& !flags
->dryrun
&&
2954 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
2955 zcmd_free_nvlists(&zc
);
2956 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
2962 zc
.zc_begin_record
= drr_noswap
->drr_u
.drr_begin
;
2963 zc
.zc_cookie
= infd
;
2964 zc
.zc_guid
= flags
->force
;
2965 if (flags
->verbose
) {
2966 (void) printf("%s %s stream of %s into %s\n",
2967 flags
->dryrun
? "would receive" : "receiving",
2968 drrb
->drr_fromguid
? "incremental" : "full",
2969 drrb
->drr_toname
, zc
.zc_value
);
2970 (void) fflush(stdout
);
2973 if (flags
->dryrun
) {
2974 zcmd_free_nvlists(&zc
);
2975 return (recv_skip(hdl
, infd
, flags
->byteswap
));
2978 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
2979 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
2980 zc
.zc_cleanup_fd
= cleanup_fd
;
2981 zc
.zc_action_handle
= *action_handlep
;
2983 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
2984 ioctl_errno
= errno
;
2985 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
2988 nvlist_t
*prop_errors
;
2989 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
2990 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
2992 nvpair_t
*prop_err
= NULL
;
2994 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
2995 prop_err
)) != NULL
) {
3000 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
3001 (void) nvpair_value_int32(prop_err
, &intval
);
3002 if (strcmp(nvpair_name(prop_err
),
3003 ZPROP_N_MORE_ERRORS
) == 0) {
3004 trunc_prop_errs(intval
);
3007 (void) snprintf(tbuf
, sizeof (tbuf
),
3008 dgettext(TEXT_DOMAIN
,
3009 "cannot receive %s property on %s"),
3010 nvpair_name(prop_err
), zc
.zc_name
);
3011 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
3014 nvlist_free(prop_errors
);
3017 zc
.zc_nvlist_dst
= 0;
3018 zc
.zc_nvlist_dst_size
= 0;
3019 zcmd_free_nvlists(&zc
);
3021 if (err
== 0 && snapprops_nvlist
) {
3022 zfs_cmd_t zc2
= {"\0"};
3024 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
3025 zc2
.zc_cookie
= B_TRUE
; /* received */
3026 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
3027 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
3028 zcmd_free_nvlists(&zc2
);
3032 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
3034 * It may be that this snapshot already exists,
3035 * in which case we want to consume & ignore it
3036 * rather than failing.
3038 avl_tree_t
*local_avl
;
3039 nvlist_t
*local_nv
, *fs
;
3040 cp
= strchr(zc
.zc_value
, '@');
3043 * XXX Do this faster by just iterating over snaps in
3044 * this fs. Also if zc_value does not exist, we will
3045 * get a strange "does not exist" error message.
3048 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
3049 &local_nv
, &local_avl
) == 0) {
3051 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
3052 fsavl_destroy(local_avl
);
3053 nvlist_free(local_nv
);
3056 if (flags
->verbose
) {
3057 (void) printf("snap %s already exists; "
3058 "ignoring\n", zc
.zc_value
);
3060 err
= ioctl_err
= recv_skip(hdl
, infd
,
3067 if (ioctl_err
!= 0) {
3068 switch (ioctl_errno
) {
3070 cp
= strchr(zc
.zc_value
, '@');
3072 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3073 "most recent snapshot of %s does not\n"
3074 "match incremental source"), zc
.zc_value
);
3075 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3079 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3080 "destination %s has been modified\n"
3081 "since most recent snapshot"), zc
.zc_name
);
3082 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
3085 cp
= strchr(zc
.zc_value
, '@');
3087 /* it's the containing fs that exists */
3090 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3091 "destination already exists"));
3092 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
3093 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
3098 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3101 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3102 "invalid stream (checksum mismatch)"));
3103 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
3106 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3107 "pool must be upgraded to receive this stream."));
3108 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
3111 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3112 "destination %s space quota exceeded"), zc
.zc_name
);
3113 (void) zfs_error(hdl
, EZFS_NOSPC
, errbuf
);
3116 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
3121 * Mount the target filesystem (if created). Also mount any
3122 * children of the target filesystem if we did a replication
3123 * receive (indicated by stream_avl being non-NULL).
3125 cp
= strchr(zc
.zc_value
, '@');
3126 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
3130 h
= zfs_open(hdl
, zc
.zc_value
,
3131 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
3133 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
3135 } else if (newfs
|| stream_avl
) {
3137 * Track the first/top of hierarchy fs,
3138 * for mounting and sharing later.
3140 if (top_zfs
&& *top_zfs
== NULL
)
3141 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
3149 err
|= changelist_postfix(clp
);
3150 changelist_free(clp
);
3153 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
3154 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3155 "failed to clear unreceived properties on %s"),
3157 (void) fprintf(stderr
, "\n");
3159 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
3160 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
3161 "failed to restore original properties on %s"),
3163 (void) fprintf(stderr
, "\n");
3166 if (err
|| ioctl_err
)
3169 *action_handlep
= zc
.zc_action_handle
;
3171 if (flags
->verbose
) {
3174 uint64_t bytes
= zc
.zc_cookie
;
3175 time_t delta
= time(NULL
) - begin_time
;
3178 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
3179 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
3181 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3189 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t
*flags
,
3190 int infd
, const char *sendfs
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
3191 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
3194 dmu_replay_record_t drr
, drr_noswap
;
3195 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
3197 zio_cksum_t zcksum
= { { 0 } };
3198 uint64_t featureflags
;
3201 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
3204 if (flags
->isprefix
&&
3205 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
3206 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
3207 "(%s) does not exist"), tosnap
);
3208 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
3211 /* read in the BEGIN record */
3212 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
3216 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
3217 /* It's the double end record at the end of a package */
3221 /* the kernel needs the non-byteswapped begin record */
3224 flags
->byteswap
= B_FALSE
;
3225 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
3227 * We computed the checksum in the wrong byteorder in
3228 * recv_read() above; do it again correctly.
3230 bzero(&zcksum
, sizeof (zio_cksum_t
));
3231 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
3232 flags
->byteswap
= B_TRUE
;
3234 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
3235 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
3236 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
3237 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
3238 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
3239 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
3240 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
3241 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
3242 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
3245 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
3246 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3247 "stream (bad magic number)"));
3248 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3251 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
3252 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
3254 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
3255 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
3256 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
3257 "stream has unsupported feature, feature flags = %lx"),
3259 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3262 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
3263 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
3264 "stream (bad snapshot name)"));
3265 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
3268 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
3269 char nonpackage_sendfs
[ZFS_MAXNAMELEN
];
3270 if (sendfs
== NULL
) {
3272 * We were not called from zfs_receive_package(). Get
3273 * the fs specified by 'zfs send'.
3276 (void) strlcpy(nonpackage_sendfs
,
3277 drr
.drr_u
.drr_begin
.drr_toname
, ZFS_MAXNAMELEN
);
3278 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
3280 sendfs
= nonpackage_sendfs
;
3282 return (zfs_receive_one(hdl
, infd
, tosnap
, flags
,
3283 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
,
3284 top_zfs
, cleanup_fd
, action_handlep
));
3286 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
3287 DMU_COMPOUNDSTREAM
);
3288 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
,
3289 &drr
, &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
3294 * Restores a backup of tosnap from the file descriptor specified by infd.
3295 * Return 0 on total success, -2 if some things couldn't be
3296 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3297 * (-1 will override -2).
3300 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t
*flags
,
3301 int infd
, avl_tree_t
*stream_avl
)
3303 char *top_zfs
= NULL
;
3306 uint64_t action_handle
= 0;
3310 * The only way fstat can fail is if we do not have a valid file
3313 if (fstat(infd
, &sb
) == -1) {
3319 #ifndef F_SETPIPE_SZ
3320 #define F_SETPIPE_SZ (F_SETLEASE + 7)
3321 #endif /* F_SETPIPE_SZ */
3323 #ifndef F_GETPIPE_SZ
3324 #define F_GETPIPE_SZ (F_GETLEASE + 7)
3325 #endif /* F_GETPIPE_SZ */
3328 * It is not uncommon for gigabytes to be processed in zfs receive.
3329 * Speculatively increase the buffer size via Linux-specific fcntl()
3332 if (S_ISFIFO(sb
.st_mode
)) {
3333 FILE *procf
= fopen("/proc/sys/fs/pipe-max-size", "r");
3335 if (procf
!= NULL
) {
3336 unsigned long max_psize
;
3338 if (fscanf(procf
, "%lu", &max_psize
) > 0) {
3339 cur_psize
= fcntl(infd
, F_GETPIPE_SZ
);
3340 if (cur_psize
> 0 &&
3341 max_psize
> (unsigned long) cur_psize
)
3342 (void) fcntl(infd
, F_SETPIPE_SZ
,
3348 #endif /* __linux__ */
3350 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3351 VERIFY(cleanup_fd
>= 0);
3353 err
= zfs_receive_impl(hdl
, tosnap
, flags
, infd
, NULL
, NULL
,
3354 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
);
3356 VERIFY(0 == close(cleanup_fd
));
3358 if (err
== 0 && !flags
->nomount
&& top_zfs
) {
3360 prop_changelist_t
*clp
;
3362 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3364 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3365 CL_GATHER_MOUNT_ALWAYS
, 0);
3368 /* mount and share received datasets */
3369 err
= changelist_postfix(clp
);
3370 changelist_free(clp
);
3373 if (zhp
== NULL
|| clp
== NULL
|| err
)