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.
36 #include <sys/mount.h>
42 #include "zfs_namecheck.h"
44 #include "zfs_fletcher.h"
45 #include "libzfs_impl.h"
46 #include <sys/zio_checksum.h>
49 /* in libzfs_dataset.c */
50 extern void zfs_setprop_error(libzfs_handle_t
*, zfs_prop_t
, int, char *);
52 static int zfs_receive_impl(libzfs_handle_t
*, const char *, recvflags_t
,
53 int, const char *, nvlist_t
*, avl_tree_t
*, char **, int, uint64_t *);
55 static const zio_cksum_t zero_cksum
= { { 0 } };
57 typedef struct dedup_arg
{
60 libzfs_handle_t
*dedup_hdl
;
63 typedef struct dataref
{
69 typedef struct dedup_entry
{
70 struct dedup_entry
*dde_next
;
71 zio_cksum_t dde_chksum
;
76 #define MAX_DDT_PHYSMEM_PERCENT 20
77 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
79 typedef struct dedup_table
{
80 dedup_entry_t
**dedup_hash_array
;
81 umem_cache_t
*ddecache
;
82 uint64_t max_ddt_size
; /* max dedup table size in bytes */
83 uint64_t cur_ddt_size
; /* current dedup table size in bytes */
90 high_order_bit(uint64_t n
)
94 for (count
= 0; n
!= 0; count
++)
100 ssread(void *buf
, size_t len
, FILE *stream
)
104 if ((outlen
= fread(buf
, len
, 1, stream
)) == 0)
111 ddt_hash_append(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, dedup_entry_t
**ddepp
,
112 zio_cksum_t
*cs
, uint64_t prop
, dataref_t
*dr
)
116 if (ddt
->cur_ddt_size
>= ddt
->max_ddt_size
) {
117 if (ddt
->ddt_full
== B_FALSE
) {
118 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
119 "Dedup table full. Deduplication will continue "
120 "with existing table entries"));
121 ddt
->ddt_full
= B_TRUE
;
126 if ((dde
= umem_cache_alloc(ddt
->ddecache
, UMEM_DEFAULT
))
128 assert(*ddepp
== NULL
);
129 dde
->dde_next
= NULL
;
130 dde
->dde_chksum
= *cs
;
131 dde
->dde_prop
= prop
;
134 ddt
->cur_ddt_size
+= sizeof (dedup_entry_t
);
140 * Using the specified dedup table, do a lookup for an entry with
141 * the checksum cs. If found, return the block's reference info
142 * in *dr. Otherwise, insert a new entry in the dedup table, using
143 * the reference information specified by *dr.
145 * return value: true - entry was found
146 * false - entry was not found
149 ddt_update(libzfs_handle_t
*hdl
, dedup_table_t
*ddt
, zio_cksum_t
*cs
,
150 uint64_t prop
, dataref_t
*dr
)
153 dedup_entry_t
**ddepp
;
155 hashcode
= BF64_GET(cs
->zc_word
[0], 0, ddt
->numhashbits
);
157 for (ddepp
= &(ddt
->dedup_hash_array
[hashcode
]); *ddepp
!= NULL
;
158 ddepp
= &((*ddepp
)->dde_next
)) {
159 if (ZIO_CHECKSUM_EQUAL(((*ddepp
)->dde_chksum
), *cs
) &&
160 (*ddepp
)->dde_prop
== prop
) {
161 *dr
= (*ddepp
)->dde_ref
;
165 ddt_hash_append(hdl
, ddt
, ddepp
, cs
, prop
, dr
);
170 cksum_and_write(const void *buf
, uint64_t len
, zio_cksum_t
*zc
, int outfd
)
172 fletcher_4_incremental_native(buf
, len
, zc
);
173 return (write(outfd
, buf
, len
));
177 * This function is started in a separate thread when the dedup option
178 * has been requested. The main send thread determines the list of
179 * snapshots to be included in the send stream and makes the ioctl calls
180 * for each one. But instead of having the ioctl send the output to the
181 * the output fd specified by the caller of zfs_send()), the
182 * ioctl is told to direct the output to a pipe, which is read by the
183 * alternate thread running THIS function. This function does the
185 * 1. building a dedup table (the DDT)
186 * 2. doing checksums on each data block and inserting a record in the DDT
187 * 3. looking for matching checksums, and
188 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
189 * a duplicate block is found.
190 * The output of this function then goes to the output fd requested
191 * by the caller of zfs_send().
196 dedup_arg_t
*dda
= arg
;
197 char *buf
= malloc(1<<20);
198 dmu_replay_record_t thedrr
;
199 dmu_replay_record_t
*drr
= &thedrr
;
200 struct drr_begin
*drrb
= &thedrr
.drr_u
.drr_begin
;
201 struct drr_end
*drre
= &thedrr
.drr_u
.drr_end
;
202 struct drr_object
*drro
= &thedrr
.drr_u
.drr_object
;
203 struct drr_write
*drrw
= &thedrr
.drr_u
.drr_write
;
204 struct drr_spill
*drrs
= &thedrr
.drr_u
.drr_spill
;
207 dmu_replay_record_t wbr_drr
= {0};
208 struct drr_write_byref
*wbr_drrr
= &wbr_drr
.drr_u
.drr_write_byref
;
210 zio_cksum_t stream_cksum
;
211 uint64_t physmem
= sysconf(_SC_PHYS_PAGES
) * sysconf(_SC_PAGESIZE
);
215 MAX((physmem
* MAX_DDT_PHYSMEM_PERCENT
)/100,
216 SMALLEST_POSSIBLE_MAX_DDT_MB
<<20);
218 numbuckets
= ddt
.max_ddt_size
/(sizeof (dedup_entry_t
));
221 * numbuckets must be a power of 2. Increase number to
222 * a power of 2 if necessary.
224 if (!ISP2(numbuckets
))
225 numbuckets
= 1 << high_order_bit(numbuckets
);
227 ddt
.dedup_hash_array
= calloc(numbuckets
, sizeof (dedup_entry_t
*));
228 ddt
.ddecache
= umem_cache_create("dde", sizeof (dedup_entry_t
), 0,
229 NULL
, NULL
, NULL
, NULL
, NULL
, 0);
230 ddt
.cur_ddt_size
= numbuckets
* sizeof (dedup_entry_t
*);
231 ddt
.numhashbits
= high_order_bit(numbuckets
) - 1;
232 ddt
.ddt_full
= B_FALSE
;
234 /* Initialize the write-by-reference block. */
235 wbr_drr
.drr_type
= DRR_WRITE_BYREF
;
236 wbr_drr
.drr_payloadlen
= 0;
238 outfd
= dda
->outputfd
;
239 ofp
= fdopen(dda
->inputfd
, "r");
240 while (ssread(drr
, sizeof (dmu_replay_record_t
), ofp
) != 0) {
242 switch (drr
->drr_type
) {
246 ZIO_SET_CHECKSUM(&stream_cksum
, 0, 0, 0, 0);
248 /* set the DEDUP feature flag for this stream */
249 fflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
250 fflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
251 DMU_BACKUP_FEATURE_DEDUPPROPS
);
252 DMU_SET_FEATUREFLAGS(drrb
->drr_versioninfo
, fflags
);
254 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
255 &stream_cksum
, outfd
) == -1)
257 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
258 DMU_COMPOUNDSTREAM
&& drr
->drr_payloadlen
!= 0) {
259 int sz
= drr
->drr_payloadlen
;
265 (void) ssread(buf
, sz
, ofp
);
268 if (cksum_and_write(buf
, sz
, &stream_cksum
,
277 /* use the recalculated checksum */
278 ZIO_SET_CHECKSUM(&drre
->drr_checksum
,
279 stream_cksum
.zc_word
[0], stream_cksum
.zc_word
[1],
280 stream_cksum
.zc_word
[2], stream_cksum
.zc_word
[3]);
281 if ((write(outfd
, drr
,
282 sizeof (dmu_replay_record_t
))) == -1)
289 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
290 &stream_cksum
, outfd
) == -1)
292 if (drro
->drr_bonuslen
> 0) {
294 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
296 if (cksum_and_write(buf
,
297 P2ROUNDUP((uint64_t)drro
->drr_bonuslen
, 8),
298 &stream_cksum
, outfd
) == -1)
306 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
307 &stream_cksum
, outfd
) == -1)
309 (void) ssread(buf
, drrs
->drr_length
, ofp
);
310 if (cksum_and_write(buf
, drrs
->drr_length
,
311 &stream_cksum
, outfd
) == -1)
316 case DRR_FREEOBJECTS
:
318 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
319 &stream_cksum
, outfd
) == -1)
328 (void) ssread(buf
, drrw
->drr_length
, ofp
);
331 * Use the existing checksum if it's dedup-capable,
332 * else calculate a SHA256 checksum for it.
335 if (ZIO_CHECKSUM_EQUAL(drrw
->drr_key
.ddk_cksum
,
337 !DRR_IS_DEDUP_CAPABLE(drrw
->drr_checksumflags
)) {
338 zio_cksum_t tmpsha256
;
340 zio_checksum_SHA256(buf
,
341 drrw
->drr_length
, &tmpsha256
);
343 drrw
->drr_key
.ddk_cksum
.zc_word
[0] =
344 BE_64(tmpsha256
.zc_word
[0]);
345 drrw
->drr_key
.ddk_cksum
.zc_word
[1] =
346 BE_64(tmpsha256
.zc_word
[1]);
347 drrw
->drr_key
.ddk_cksum
.zc_word
[2] =
348 BE_64(tmpsha256
.zc_word
[2]);
349 drrw
->drr_key
.ddk_cksum
.zc_word
[3] =
350 BE_64(tmpsha256
.zc_word
[3]);
351 drrw
->drr_checksumtype
= ZIO_CHECKSUM_SHA256
;
352 drrw
->drr_checksumflags
= DRR_CHECKSUM_DEDUP
;
355 dataref
.ref_guid
= drrw
->drr_toguid
;
356 dataref
.ref_object
= drrw
->drr_object
;
357 dataref
.ref_offset
= drrw
->drr_offset
;
359 if (ddt_update(dda
->dedup_hdl
, &ddt
,
360 &drrw
->drr_key
.ddk_cksum
, drrw
->drr_key
.ddk_prop
,
362 /* block already present in stream */
363 wbr_drrr
->drr_object
= drrw
->drr_object
;
364 wbr_drrr
->drr_offset
= drrw
->drr_offset
;
365 wbr_drrr
->drr_length
= drrw
->drr_length
;
366 wbr_drrr
->drr_toguid
= drrw
->drr_toguid
;
367 wbr_drrr
->drr_refguid
= dataref
.ref_guid
;
368 wbr_drrr
->drr_refobject
=
370 wbr_drrr
->drr_refoffset
=
373 wbr_drrr
->drr_checksumtype
=
374 drrw
->drr_checksumtype
;
375 wbr_drrr
->drr_checksumflags
=
376 drrw
->drr_checksumtype
;
377 wbr_drrr
->drr_key
.ddk_cksum
=
378 drrw
->drr_key
.ddk_cksum
;
379 wbr_drrr
->drr_key
.ddk_prop
=
380 drrw
->drr_key
.ddk_prop
;
382 if (cksum_and_write(&wbr_drr
,
383 sizeof (dmu_replay_record_t
), &stream_cksum
,
387 /* block not previously seen */
388 if (cksum_and_write(drr
,
389 sizeof (dmu_replay_record_t
), &stream_cksum
,
392 if (cksum_and_write(buf
,
394 &stream_cksum
, outfd
) == -1)
402 if (cksum_and_write(drr
, sizeof (dmu_replay_record_t
),
403 &stream_cksum
, outfd
) == -1)
409 (void) printf("INVALID record type 0x%x\n",
411 /* should never happen, so assert */
416 umem_cache_destroy(ddt
.ddecache
);
417 free(ddt
.dedup_hash_array
);
425 * Routines for dealing with the AVL tree of fs-nvlists
427 typedef struct fsavl_node
{
435 fsavl_compare(const void *arg1
, const void *arg2
)
437 const fsavl_node_t
*fn1
= arg1
;
438 const fsavl_node_t
*fn2
= arg2
;
440 if (fn1
->fn_guid
> fn2
->fn_guid
)
442 else if (fn1
->fn_guid
< fn2
->fn_guid
)
449 * Given the GUID of a snapshot, find its containing filesystem and
453 fsavl_find(avl_tree_t
*avl
, uint64_t snapguid
, char **snapname
)
455 fsavl_node_t fn_find
;
458 fn_find
.fn_guid
= snapguid
;
460 fn
= avl_find(avl
, &fn_find
, NULL
);
463 *snapname
= fn
->fn_snapname
;
464 return (fn
->fn_nvfs
);
470 fsavl_destroy(avl_tree_t
*avl
)
479 while ((fn
= avl_destroy_nodes(avl
, &cookie
)) != NULL
)
486 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
489 fsavl_create(nvlist_t
*fss
)
492 nvpair_t
*fselem
= NULL
;
494 if ((fsavl
= malloc(sizeof (avl_tree_t
))) == NULL
)
497 avl_create(fsavl
, fsavl_compare
, sizeof (fsavl_node_t
),
498 offsetof(fsavl_node_t
, fn_node
));
500 while ((fselem
= nvlist_next_nvpair(fss
, fselem
)) != NULL
) {
501 nvlist_t
*nvfs
, *snaps
;
502 nvpair_t
*snapelem
= NULL
;
504 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
505 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
508 nvlist_next_nvpair(snaps
, snapelem
)) != NULL
) {
512 VERIFY(0 == nvpair_value_uint64(snapelem
, &guid
));
513 if ((fn
= malloc(sizeof (fsavl_node_t
))) == NULL
) {
514 fsavl_destroy(fsavl
);
518 fn
->fn_snapname
= nvpair_name(snapelem
);
522 * Note: if there are multiple snaps with the
523 * same GUID, we ignore all but one.
525 if (avl_find(fsavl
, fn
, NULL
) == NULL
)
536 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
538 typedef struct send_data
{
539 uint64_t parent_fromsnap_guid
;
540 nvlist_t
*parent_snaps
;
543 const char *fromsnap
;
548 * The header nvlist is of the following format:
551 * "fromsnap" -> string (if incremental)
555 * "name" -> string (full name; for debugging)
556 * "parentfromsnap" -> number (guid of fromsnap in parent)
558 * "props" -> { name -> value (only if set here) }
559 * "snaps" -> { name (lastname) -> number (guid) }
560 * "snapprops" -> { name (lastname) -> { name -> value } }
562 * "origin" -> number (guid) (if clone)
563 * "sent" -> boolean (not on-disk)
571 static void send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
);
574 send_iterate_snap(zfs_handle_t
*zhp
, void *arg
)
576 send_data_t
*sd
= arg
;
577 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
581 snapname
= strrchr(zhp
->zfs_name
, '@')+1;
583 VERIFY(0 == nvlist_add_uint64(sd
->parent_snaps
, snapname
, guid
));
585 * NB: if there is no fromsnap here (it's a newly created fs in
586 * an incremental replication), we will substitute the tosnap.
588 if ((sd
->fromsnap
&& strcmp(snapname
, sd
->fromsnap
) == 0) ||
589 (sd
->parent_fromsnap_guid
== 0 && sd
->tosnap
&&
590 strcmp(snapname
, sd
->tosnap
) == 0)) {
591 sd
->parent_fromsnap_guid
= guid
;
594 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
595 send_iterate_prop(zhp
, nv
);
596 VERIFY(0 == nvlist_add_nvlist(sd
->snapprops
, snapname
, nv
));
604 send_iterate_prop(zfs_handle_t
*zhp
, nvlist_t
*nv
)
606 nvpair_t
*elem
= NULL
;
608 while ((elem
= nvlist_next_nvpair(zhp
->zfs_props
, elem
)) != NULL
) {
609 char *propname
= nvpair_name(elem
);
610 zfs_prop_t prop
= zfs_name_to_prop(propname
);
613 if (!zfs_prop_user(propname
)) {
615 * Realistically, this should never happen. However,
616 * we want the ability to add DSL properties without
617 * needing to make incompatible version changes. We
618 * need to ignore unknown properties to allow older
619 * software to still send datasets containing these
620 * properties, with the unknown properties elided.
622 if (prop
== ZPROP_INVAL
)
625 if (zfs_prop_readonly(prop
))
629 verify(nvpair_value_nvlist(elem
, &propnv
) == 0);
630 if (prop
== ZFS_PROP_QUOTA
|| prop
== ZFS_PROP_RESERVATION
||
631 prop
== ZFS_PROP_REFQUOTA
||
632 prop
== ZFS_PROP_REFRESERVATION
) {
635 verify(nvlist_lookup_uint64(propnv
,
636 ZPROP_VALUE
, &value
) == 0);
637 if (zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
)
640 * May have no source before SPA_VERSION_RECVD_PROPS,
641 * but is still modifiable.
643 if (nvlist_lookup_string(propnv
,
644 ZPROP_SOURCE
, &source
) == 0) {
645 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
647 ZPROP_SOURCE_VAL_RECVD
) != 0))
652 if (nvlist_lookup_string(propnv
,
653 ZPROP_SOURCE
, &source
) != 0)
655 if ((strcmp(source
, zhp
->zfs_name
) != 0) &&
656 (strcmp(source
, ZPROP_SOURCE_VAL_RECVD
) != 0))
660 if (zfs_prop_user(propname
) ||
661 zfs_prop_get_type(prop
) == PROP_TYPE_STRING
) {
663 verify(nvlist_lookup_string(propnv
,
664 ZPROP_VALUE
, &value
) == 0);
665 VERIFY(0 == nvlist_add_string(nv
, propname
, value
));
668 verify(nvlist_lookup_uint64(propnv
,
669 ZPROP_VALUE
, &value
) == 0);
670 VERIFY(0 == nvlist_add_uint64(nv
, propname
, value
));
676 * recursively generate nvlists describing datasets. See comment
677 * for the data structure send_data_t above for description of contents
681 send_iterate_fs(zfs_handle_t
*zhp
, void *arg
)
683 send_data_t
*sd
= arg
;
686 uint64_t parent_fromsnap_guid_save
= sd
->parent_fromsnap_guid
;
687 uint64_t guid
= zhp
->zfs_dmustats
.dds_guid
;
690 VERIFY(0 == nvlist_alloc(&nvfs
, NV_UNIQUE_NAME
, 0));
691 VERIFY(0 == nvlist_add_string(nvfs
, "name", zhp
->zfs_name
));
692 VERIFY(0 == nvlist_add_uint64(nvfs
, "parentfromsnap",
693 sd
->parent_fromsnap_guid
));
695 if (zhp
->zfs_dmustats
.dds_origin
[0]) {
696 zfs_handle_t
*origin
= zfs_open(zhp
->zfs_hdl
,
697 zhp
->zfs_dmustats
.dds_origin
, ZFS_TYPE_SNAPSHOT
);
700 VERIFY(0 == nvlist_add_uint64(nvfs
, "origin",
701 origin
->zfs_dmustats
.dds_guid
));
704 /* iterate over props */
705 VERIFY(0 == nvlist_alloc(&nv
, NV_UNIQUE_NAME
, 0));
706 send_iterate_prop(zhp
, nv
);
707 VERIFY(0 == nvlist_add_nvlist(nvfs
, "props", nv
));
710 /* iterate over snaps, and set sd->parent_fromsnap_guid */
711 sd
->parent_fromsnap_guid
= 0;
712 VERIFY(0 == nvlist_alloc(&sd
->parent_snaps
, NV_UNIQUE_NAME
, 0));
713 VERIFY(0 == nvlist_alloc(&sd
->snapprops
, NV_UNIQUE_NAME
, 0));
714 (void) zfs_iter_snapshots(zhp
, send_iterate_snap
, sd
);
715 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snaps", sd
->parent_snaps
));
716 VERIFY(0 == nvlist_add_nvlist(nvfs
, "snapprops", sd
->snapprops
));
717 nvlist_free(sd
->parent_snaps
);
718 nvlist_free(sd
->snapprops
);
720 /* add this fs to nvlist */
721 (void) snprintf(guidstring
, sizeof (guidstring
),
722 "0x%llx", (longlong_t
)guid
);
723 VERIFY(0 == nvlist_add_nvlist(sd
->fss
, guidstring
, nvfs
));
726 /* iterate over children */
728 rv
= zfs_iter_filesystems(zhp
, send_iterate_fs
, sd
);
730 sd
->parent_fromsnap_guid
= parent_fromsnap_guid_save
;
737 gather_nvlist(libzfs_handle_t
*hdl
, const char *fsname
, const char *fromsnap
,
738 const char *tosnap
, boolean_t recursive
, nvlist_t
**nvlp
, avl_tree_t
**avlp
)
741 send_data_t sd
= { 0 };
744 zhp
= zfs_open(hdl
, fsname
, ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
746 return (EZFS_BADTYPE
);
748 VERIFY(0 == nvlist_alloc(&sd
.fss
, NV_UNIQUE_NAME
, 0));
749 sd
.fromsnap
= fromsnap
;
751 sd
.recursive
= recursive
;
753 if ((error
= send_iterate_fs(zhp
, &sd
)) != 0) {
761 if (avlp
!= NULL
&& (*avlp
= fsavl_create(sd
.fss
)) == NULL
) {
772 * Routines for dealing with the sorted snapshot functionality
774 typedef struct zfs_node
{
775 zfs_handle_t
*zn_handle
;
776 avl_node_t zn_avlnode
;
780 zfs_sort_snaps(zfs_handle_t
*zhp
, void *data
)
782 avl_tree_t
*avl
= data
;
786 search
.zn_handle
= zhp
;
787 node
= avl_find(avl
, &search
, NULL
);
790 * If this snapshot was renamed while we were creating the
791 * AVL tree, it's possible that we already inserted it under
792 * its old name. Remove the old handle before adding the new
795 zfs_close(node
->zn_handle
);
796 avl_remove(avl
, node
);
800 node
= zfs_alloc(zhp
->zfs_hdl
, sizeof (zfs_node_t
));
801 node
->zn_handle
= zhp
;
808 zfs_snapshot_compare(const void *larg
, const void *rarg
)
810 zfs_handle_t
*l
= ((zfs_node_t
*)larg
)->zn_handle
;
811 zfs_handle_t
*r
= ((zfs_node_t
*)rarg
)->zn_handle
;
812 uint64_t lcreate
, rcreate
;
815 * Sort them according to creation time. We use the hidden
816 * CREATETXG property to get an absolute ordering of snapshots.
818 lcreate
= zfs_prop_get_int(l
, ZFS_PROP_CREATETXG
);
819 rcreate
= zfs_prop_get_int(r
, ZFS_PROP_CREATETXG
);
821 if (lcreate
< rcreate
)
823 else if (lcreate
> rcreate
)
830 zfs_iter_snapshots_sorted(zfs_handle_t
*zhp
, zfs_iter_f callback
, void *data
)
837 avl_create(&avl
, zfs_snapshot_compare
,
838 sizeof (zfs_node_t
), offsetof(zfs_node_t
, zn_avlnode
));
840 ret
= zfs_iter_snapshots(zhp
, zfs_sort_snaps
, &avl
);
842 for (node
= avl_first(&avl
); node
!= NULL
; node
= AVL_NEXT(&avl
, node
))
843 ret
|= callback(node
->zn_handle
, data
);
845 while ((node
= avl_destroy_nodes(&avl
, &cookie
)) != NULL
)
854 * Routines specific to "zfs send"
856 typedef struct send_dump_data
{
857 /* these are all just the short snapname (the part after the @) */
858 const char *fromsnap
;
860 char prevsnap
[ZFS_MAXNAMELEN
];
861 uint64_t prevsnap_obj
;
862 boolean_t seenfrom
, seento
, replicate
, doall
, fromorigin
;
868 snapfilter_cb_t
*filter_cb
;
871 char holdtag
[ZFS_MAXNAMELEN
];
876 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
877 * NULL) to the file descriptor specified by outfd.
880 dump_ioctl(zfs_handle_t
*zhp
, const char *fromsnap
, uint64_t fromsnap_obj
,
881 boolean_t fromorigin
, int outfd
, nvlist_t
*debugnv
)
883 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
884 libzfs_handle_t
*hdl
= zhp
->zfs_hdl
;
887 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
888 assert(fromsnap_obj
== 0 || !fromorigin
);
890 (void) strlcpy(zc
.zc_name
, zhp
->zfs_name
, sizeof (zc
.zc_name
));
891 zc
.zc_cookie
= outfd
;
892 zc
.zc_obj
= fromorigin
;
893 zc
.zc_sendobj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
894 zc
.zc_fromobj
= fromsnap_obj
;
896 VERIFY(0 == nvlist_alloc(&thisdbg
, NV_UNIQUE_NAME
, 0));
897 if (fromsnap
&& fromsnap
[0] != '\0') {
898 VERIFY(0 == nvlist_add_string(thisdbg
,
899 "fromsnap", fromsnap
));
902 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_SEND
, &zc
) != 0) {
904 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
905 "warning: cannot send '%s'"), zhp
->zfs_name
);
907 VERIFY(0 == nvlist_add_uint64(thisdbg
, "error", errno
));
909 VERIFY(0 == nvlist_add_nvlist(debugnv
,
910 zhp
->zfs_name
, thisdbg
));
912 nvlist_free(thisdbg
);
917 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
918 "not an earlier snapshot from the same fs"));
919 return (zfs_error(hdl
, EZFS_CROSSTARGET
, errbuf
));
922 if (zfs_dataset_exists(hdl
, zc
.zc_name
,
923 ZFS_TYPE_SNAPSHOT
)) {
924 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
925 "incremental source (@%s) does not exist"),
928 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
941 zfs_error_aux(hdl
, strerror(errno
));
942 return (zfs_error(hdl
, EZFS_BADBACKUP
, errbuf
));
945 return (zfs_standard_error(hdl
, errno
, errbuf
));
950 VERIFY(0 == nvlist_add_nvlist(debugnv
, zhp
->zfs_name
, thisdbg
));
951 nvlist_free(thisdbg
);
957 hold_for_send(zfs_handle_t
*zhp
, send_dump_data_t
*sdd
)
963 assert(zhp
->zfs_type
== ZFS_TYPE_SNAPSHOT
);
966 * zfs_send() only opens a cleanup_fd for sends that need it,
967 * e.g. replication and doall.
969 if (sdd
->cleanup_fd
== -1)
972 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
973 *(thissnap
- 1) = '\0';
974 pzhp
= zfs_open(zhp
->zfs_hdl
, zhp
->zfs_name
, ZFS_TYPE_DATASET
);
975 *(thissnap
- 1) = '@';
978 * It's OK if the parent no longer exists. The send code will
982 error
= zfs_hold(pzhp
, thissnap
, sdd
->holdtag
,
983 B_FALSE
, B_TRUE
, B_TRUE
, sdd
->cleanup_fd
,
984 zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
),
985 zfs_prop_get_int(zhp
, ZFS_PROP_CREATETXG
));
993 dump_snapshot(zfs_handle_t
*zhp
, void *arg
)
995 send_dump_data_t
*sdd
= arg
;
998 boolean_t isfromsnap
, istosnap
;
999 boolean_t exclude
= B_FALSE
;
1001 thissnap
= strchr(zhp
->zfs_name
, '@') + 1;
1002 isfromsnap
= (sdd
->fromsnap
!= NULL
&&
1003 strcmp(sdd
->fromsnap
, thissnap
) == 0);
1005 if (!sdd
->seenfrom
&& isfromsnap
) {
1006 err
= hold_for_send(zhp
, sdd
);
1008 sdd
->seenfrom
= B_TRUE
;
1009 (void) strcpy(sdd
->prevsnap
, thissnap
);
1010 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
,
1012 } else if (err
== ENOENT
) {
1019 if (sdd
->seento
|| !sdd
->seenfrom
) {
1024 istosnap
= (strcmp(sdd
->tosnap
, thissnap
) == 0);
1026 sdd
->seento
= B_TRUE
;
1028 if (!sdd
->doall
&& !isfromsnap
&& !istosnap
) {
1029 if (sdd
->replicate
) {
1031 nvlist_t
*snapprops
;
1033 * Filter out all intermediate snapshots except origin
1034 * snapshots needed to replicate clones.
1036 nvlist_t
*nvfs
= fsavl_find(sdd
->fsavl
,
1037 zhp
->zfs_dmustats
.dds_guid
, &snapname
);
1039 VERIFY(0 == nvlist_lookup_nvlist(nvfs
,
1040 "snapprops", &snapprops
));
1041 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1042 thissnap
, &snapprops
));
1043 exclude
= !nvlist_exists(snapprops
, "is_clone_origin");
1050 * If a filter function exists, call it to determine whether
1051 * this snapshot will be sent.
1053 if (exclude
|| (sdd
->filter_cb
!= NULL
&&
1054 sdd
->filter_cb(zhp
, sdd
->filter_cb_arg
) == B_FALSE
)) {
1056 * This snapshot is filtered out. Don't send it, and don't
1057 * set prevsnap_obj, so it will be as if this snapshot didn't
1058 * exist, and the next accepted snapshot will be sent as
1059 * an incremental from the last accepted one, or as the
1060 * first (and full) snapshot in the case of a replication,
1061 * non-incremental send.
1067 err
= hold_for_send(zhp
, sdd
);
1077 (void) fprintf(stderr
, "sending from @%s to %s\n",
1078 sdd
->prevsnap
, zhp
->zfs_name
);
1081 err
= dump_ioctl(zhp
, sdd
->prevsnap
, sdd
->prevsnap_obj
,
1082 sdd
->prevsnap
[0] == '\0' && (sdd
->fromorigin
|| sdd
->replicate
),
1083 sdd
->outfd
, sdd
->debugnv
);
1085 (void) strcpy(sdd
->prevsnap
, thissnap
);
1086 sdd
->prevsnap_obj
= zfs_prop_get_int(zhp
, ZFS_PROP_OBJSETID
);
1092 dump_filesystem(zfs_handle_t
*zhp
, void *arg
)
1095 send_dump_data_t
*sdd
= arg
;
1096 boolean_t missingfrom
= B_FALSE
;
1097 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1099 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1100 zhp
->zfs_name
, sdd
->tosnap
);
1101 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
, ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1102 (void) fprintf(stderr
, "WARNING: "
1103 "could not send %s@%s: does not exist\n",
1104 zhp
->zfs_name
, sdd
->tosnap
);
1109 if (sdd
->replicate
&& sdd
->fromsnap
) {
1111 * If this fs does not have fromsnap, and we're doing
1112 * recursive, we need to send a full stream from the
1113 * beginning (or an incremental from the origin if this
1114 * is a clone). If we're doing non-recursive, then let
1115 * them get the error.
1117 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
), "%s@%s",
1118 zhp
->zfs_name
, sdd
->fromsnap
);
1119 if (ioctl(zhp
->zfs_hdl
->libzfs_fd
,
1120 ZFS_IOC_OBJSET_STATS
, &zc
) != 0) {
1121 missingfrom
= B_TRUE
;
1125 sdd
->seenfrom
= sdd
->seento
= sdd
->prevsnap
[0] = 0;
1126 sdd
->prevsnap_obj
= 0;
1127 if (sdd
->fromsnap
== NULL
|| missingfrom
)
1128 sdd
->seenfrom
= B_TRUE
;
1130 rv
= zfs_iter_snapshots_sorted(zhp
, dump_snapshot
, arg
);
1131 if (!sdd
->seenfrom
) {
1132 (void) fprintf(stderr
,
1133 "WARNING: could not send %s@%s:\n"
1134 "incremental source (%s@%s) does not exist\n",
1135 zhp
->zfs_name
, sdd
->tosnap
,
1136 zhp
->zfs_name
, sdd
->fromsnap
);
1138 } else if (!sdd
->seento
) {
1139 if (sdd
->fromsnap
) {
1140 (void) fprintf(stderr
,
1141 "WARNING: could not send %s@%s:\n"
1142 "incremental source (%s@%s) "
1143 "is not earlier than it\n",
1144 zhp
->zfs_name
, sdd
->tosnap
,
1145 zhp
->zfs_name
, sdd
->fromsnap
);
1147 (void) fprintf(stderr
, "WARNING: "
1148 "could not send %s@%s: does not exist\n",
1149 zhp
->zfs_name
, sdd
->tosnap
);
1158 dump_filesystems(zfs_handle_t
*rzhp
, void *arg
)
1160 send_dump_data_t
*sdd
= arg
;
1162 boolean_t needagain
, progress
;
1164 if (!sdd
->replicate
)
1165 return (dump_filesystem(rzhp
, sdd
));
1167 /* Mark the clone origin snapshots. */
1168 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1169 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1171 uint64_t origin_guid
= 0;
1173 VERIFY(0 == nvpair_value_nvlist(fspair
, &nvfs
));
1174 (void) nvlist_lookup_uint64(nvfs
, "origin", &origin_guid
);
1175 if (origin_guid
!= 0) {
1177 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1178 origin_guid
, &snapname
);
1179 if (origin_nv
!= NULL
) {
1180 nvlist_t
*snapprops
;
1181 VERIFY(0 == nvlist_lookup_nvlist(origin_nv
,
1182 "snapprops", &snapprops
));
1183 VERIFY(0 == nvlist_lookup_nvlist(snapprops
,
1184 snapname
, &snapprops
));
1185 VERIFY(0 == nvlist_add_boolean(
1186 snapprops
, "is_clone_origin"));
1191 needagain
= progress
= B_FALSE
;
1192 for (fspair
= nvlist_next_nvpair(sdd
->fss
, NULL
); fspair
;
1193 fspair
= nvlist_next_nvpair(sdd
->fss
, fspair
)) {
1198 uint64_t origin_guid
= 0;
1200 VERIFY(nvpair_value_nvlist(fspair
, &fslist
) == 0);
1201 if (nvlist_lookup_boolean(fslist
, "sent") == 0)
1204 VERIFY(nvlist_lookup_string(fslist
, "name", &fsname
) == 0);
1205 (void) nvlist_lookup_uint64(fslist
, "origin", &origin_guid
);
1207 if (origin_guid
!= 0) {
1208 nvlist_t
*origin_nv
= fsavl_find(sdd
->fsavl
,
1210 if (origin_nv
!= NULL
&&
1211 nvlist_lookup_boolean(origin_nv
,
1212 "sent") == ENOENT
) {
1214 * origin has not been sent yet;
1222 zhp
= zfs_open(rzhp
->zfs_hdl
, fsname
, ZFS_TYPE_DATASET
);
1225 err
= dump_filesystem(zhp
, sdd
);
1226 VERIFY(nvlist_add_boolean(fslist
, "sent") == 0);
1240 * Generate a send stream for the dataset identified by the argument zhp.
1242 * The content of the send stream is the snapshot identified by
1243 * 'tosnap'. Incremental streams are requested in two ways:
1244 * - from the snapshot identified by "fromsnap" (if non-null) or
1245 * - from the origin of the dataset identified by zhp, which must
1246 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1249 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1250 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1251 * if "replicate" is set. If "doall" is set, dump all the intermediate
1252 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1253 * case too. If "props" is set, send properties.
1256 zfs_send(zfs_handle_t
*zhp
, const char *fromsnap
, const char *tosnap
,
1257 sendflags_t flags
, int outfd
, snapfilter_cb_t filter_func
,
1258 void *cb_arg
, nvlist_t
**debugnvp
)
1261 send_dump_data_t sdd
= { 0 };
1263 nvlist_t
*fss
= NULL
;
1264 avl_tree_t
*fsavl
= NULL
;
1265 static uint64_t holdseq
;
1267 boolean_t holdsnaps
= B_FALSE
;
1270 dedup_arg_t dda
= { 0 };
1271 int featureflags
= 0;
1273 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
1274 "cannot send '%s'"), zhp
->zfs_name
);
1276 if (fromsnap
&& fromsnap
[0] == '\0') {
1277 zfs_error_aux(zhp
->zfs_hdl
, dgettext(TEXT_DOMAIN
,
1278 "zero-length incremental source"));
1279 return (zfs_error(zhp
->zfs_hdl
, EZFS_NOENT
, errbuf
));
1282 if (zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
) {
1284 version
= zfs_prop_get_int(zhp
, ZFS_PROP_VERSION
);
1285 if (version
>= ZPL_VERSION_SA
) {
1286 featureflags
|= DMU_BACKUP_FEATURE_SA_SPILL
;
1290 if (zfs_spa_version(zhp
, &spa_version
) == 0 &&
1291 spa_version
>= SPA_VERSION_USERREFS
&&
1292 (flags
.doall
|| flags
.replicate
))
1296 featureflags
|= (DMU_BACKUP_FEATURE_DEDUP
|
1297 DMU_BACKUP_FEATURE_DEDUPPROPS
);
1298 if ((err
= pipe(pipefd
))) {
1299 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1300 return (zfs_error(zhp
->zfs_hdl
, EZFS_PIPEFAILED
,
1303 dda
.outputfd
= outfd
;
1304 dda
.inputfd
= pipefd
[1];
1305 dda
.dedup_hdl
= zhp
->zfs_hdl
;
1306 if ((err
= pthread_create(&tid
, NULL
, cksummer
, &dda
))) {
1307 (void) close(pipefd
[0]);
1308 (void) close(pipefd
[1]);
1309 zfs_error_aux(zhp
->zfs_hdl
, strerror(errno
));
1310 return (zfs_error(zhp
->zfs_hdl
,
1311 EZFS_THREADCREATEFAILED
, errbuf
));
1315 if (flags
.replicate
|| flags
.doall
|| flags
.props
) {
1316 dmu_replay_record_t drr
= { 0 };
1317 char *packbuf
= NULL
;
1319 zio_cksum_t zc
= { { 0 } };
1321 if (flags
.replicate
|| flags
.props
) {
1324 VERIFY(0 == nvlist_alloc(&hdrnv
, NV_UNIQUE_NAME
, 0));
1326 VERIFY(0 == nvlist_add_string(hdrnv
,
1327 "fromsnap", fromsnap
));
1329 VERIFY(0 == nvlist_add_string(hdrnv
, "tosnap", tosnap
));
1330 if (!flags
.replicate
) {
1331 VERIFY(0 == nvlist_add_boolean(hdrnv
,
1335 err
= gather_nvlist(zhp
->zfs_hdl
, zhp
->zfs_name
,
1336 fromsnap
, tosnap
, flags
.replicate
, &fss
, &fsavl
);
1339 VERIFY(0 == nvlist_add_nvlist(hdrnv
, "fss", fss
));
1340 err
= nvlist_pack(hdrnv
, &packbuf
, &buflen
,
1347 fsavl_destroy(fsavl
);
1353 /* write first begin record */
1354 drr
.drr_type
= DRR_BEGIN
;
1355 drr
.drr_u
.drr_begin
.drr_magic
= DMU_BACKUP_MAGIC
;
1356 DMU_SET_STREAM_HDRTYPE(drr
.drr_u
.drr_begin
.drr_versioninfo
,
1357 DMU_COMPOUNDSTREAM
);
1358 DMU_SET_FEATUREFLAGS(drr
.drr_u
.drr_begin
.drr_versioninfo
,
1360 (void) snprintf(drr
.drr_u
.drr_begin
.drr_toname
,
1361 sizeof (drr
.drr_u
.drr_begin
.drr_toname
),
1362 "%s@%s", zhp
->zfs_name
, tosnap
);
1363 drr
.drr_payloadlen
= buflen
;
1364 err
= cksum_and_write(&drr
, sizeof (drr
), &zc
, outfd
);
1366 /* write header nvlist */
1367 if (err
!= -1 && packbuf
!= NULL
) {
1368 err
= cksum_and_write(packbuf
, buflen
, &zc
, outfd
);
1372 fsavl_destroy(fsavl
);
1378 /* write end record */
1380 bzero(&drr
, sizeof (drr
));
1381 drr
.drr_type
= DRR_END
;
1382 drr
.drr_u
.drr_end
.drr_checksum
= zc
;
1383 err
= write(outfd
, &drr
, sizeof (drr
));
1385 fsavl_destroy(fsavl
);
1393 /* dump each stream */
1394 sdd
.fromsnap
= fromsnap
;
1395 sdd
.tosnap
= tosnap
;
1397 sdd
.outfd
= pipefd
[0];
1400 sdd
.replicate
= flags
.replicate
;
1401 sdd
.doall
= flags
.doall
;
1402 sdd
.fromorigin
= flags
.fromorigin
;
1405 sdd
.verbose
= flags
.verbose
;
1406 sdd
.filter_cb
= filter_func
;
1407 sdd
.filter_cb_arg
= cb_arg
;
1409 sdd
.debugnv
= *debugnvp
;
1412 (void) snprintf(sdd
.holdtag
, sizeof (sdd
.holdtag
),
1413 ".send-%d-%llu", getpid(), (u_longlong_t
)holdseq
);
1414 sdd
.cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
1415 if (sdd
.cleanup_fd
< 0) {
1420 sdd
.cleanup_fd
= -1;
1422 err
= dump_filesystems(zhp
, &sdd
);
1423 fsavl_destroy(fsavl
);
1427 (void) close(pipefd
[0]);
1428 (void) pthread_join(tid
, NULL
);
1431 if (sdd
.cleanup_fd
!= -1) {
1432 VERIFY(0 == close(sdd
.cleanup_fd
));
1433 sdd
.cleanup_fd
= -1;
1436 if (flags
.replicate
|| flags
.doall
|| flags
.props
) {
1438 * write final end record. NB: want to do this even if
1439 * there was some error, because it might not be totally
1442 dmu_replay_record_t drr
= { 0 };
1443 drr
.drr_type
= DRR_END
;
1444 if (write(outfd
, &drr
, sizeof (drr
)) == -1) {
1445 return (zfs_standard_error(zhp
->zfs_hdl
,
1450 return (err
|| sdd
.err
);
1453 err
= zfs_standard_error(zhp
->zfs_hdl
, err
, errbuf
);
1455 if (sdd
.cleanup_fd
!= -1)
1456 VERIFY(0 == close(sdd
.cleanup_fd
));
1458 (void) pthread_cancel(tid
);
1459 (void) pthread_join(tid
, NULL
);
1460 (void) close(pipefd
[0]);
1466 * Routines specific to "zfs recv"
1470 recv_read(libzfs_handle_t
*hdl
, int fd
, void *buf
, int ilen
,
1471 boolean_t byteswap
, zio_cksum_t
*zc
)
1478 rv
= read(fd
, cp
, len
);
1483 if (rv
< 0 || len
!= 0) {
1484 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
1485 "failed to read from stream"));
1486 return (zfs_error(hdl
, EZFS_BADSTREAM
, dgettext(TEXT_DOMAIN
,
1487 "cannot receive")));
1492 fletcher_4_incremental_byteswap(buf
, ilen
, zc
);
1494 fletcher_4_incremental_native(buf
, ilen
, zc
);
1500 recv_read_nvlist(libzfs_handle_t
*hdl
, int fd
, int len
, nvlist_t
**nvp
,
1501 boolean_t byteswap
, zio_cksum_t
*zc
)
1506 buf
= zfs_alloc(hdl
, len
);
1510 err
= recv_read(hdl
, fd
, buf
, len
, byteswap
, zc
);
1516 err
= nvlist_unpack(buf
, len
, nvp
, 0);
1519 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
1520 "stream (malformed nvlist)"));
1527 recv_rename(libzfs_handle_t
*hdl
, const char *name
, const char *tryname
,
1528 int baselen
, char *newname
, recvflags_t flags
)
1531 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1533 prop_changelist_t
*clp
;
1536 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1539 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1540 flags
.force
? MS_FORCE
: 0);
1544 err
= changelist_prefix(clp
);
1548 zc
.zc_objset_type
= DMU_OST_ZFS
;
1549 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1552 (void) strcpy(newname
, tryname
);
1554 (void) strlcpy(zc
.zc_value
, tryname
, sizeof (zc
.zc_value
));
1556 if (flags
.verbose
) {
1557 (void) printf("attempting rename %s to %s\n",
1558 zc
.zc_name
, zc
.zc_value
);
1560 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1562 changelist_rename(clp
, name
, tryname
);
1567 if (err
!= 0 && strncmp(name
+baselen
, "recv-", 5) != 0) {
1570 (void) strncpy(newname
, name
, baselen
);
1571 (void) snprintf(newname
+baselen
, ZFS_MAXNAMELEN
-baselen
,
1572 "recv-%ld-%u", (long) getpid(), seq
);
1573 (void) strlcpy(zc
.zc_value
, newname
, sizeof (zc
.zc_value
));
1575 if (flags
.verbose
) {
1576 (void) printf("failed - trying rename %s to %s\n",
1577 zc
.zc_name
, zc
.zc_value
);
1579 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_RENAME
, &zc
);
1581 changelist_rename(clp
, name
, newname
);
1582 if (err
&& flags
.verbose
) {
1583 (void) printf("failed (%u) - "
1584 "will try again on next pass\n", errno
);
1587 } else if (flags
.verbose
) {
1589 (void) printf("success\n");
1591 (void) printf("failed (%u)\n", errno
);
1594 (void) changelist_postfix(clp
);
1595 changelist_free(clp
);
1601 recv_destroy(libzfs_handle_t
*hdl
, const char *name
, int baselen
,
1602 char *newname
, recvflags_t flags
)
1604 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1606 prop_changelist_t
*clp
;
1608 boolean_t defer
= B_FALSE
;
1611 zhp
= zfs_open(hdl
, name
, ZFS_TYPE_DATASET
);
1614 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0,
1615 flags
.force
? MS_FORCE
: 0);
1616 if (zfs_get_type(zhp
) == ZFS_TYPE_SNAPSHOT
&&
1617 zfs_spa_version(zhp
, &spa_version
) == 0 &&
1618 spa_version
>= SPA_VERSION_USERREFS
)
1623 err
= changelist_prefix(clp
);
1627 zc
.zc_objset_type
= DMU_OST_ZFS
;
1628 zc
.zc_defer_destroy
= defer
;
1629 (void) strlcpy(zc
.zc_name
, name
, sizeof (zc
.zc_name
));
1632 (void) printf("attempting destroy %s\n", zc
.zc_name
);
1633 err
= ioctl(hdl
->libzfs_fd
, ZFS_IOC_DESTROY
, &zc
);
1636 (void) printf("success\n");
1637 changelist_remove(clp
, zc
.zc_name
);
1640 (void) changelist_postfix(clp
);
1641 changelist_free(clp
);
1644 * Deferred destroy might destroy the snapshot or only mark it to be
1645 * destroyed later, and it returns success in either case.
1647 if (err
!= 0 || (defer
&& zfs_dataset_exists(hdl
, name
,
1648 ZFS_TYPE_SNAPSHOT
))) {
1649 err
= recv_rename(hdl
, name
, NULL
, baselen
, newname
, flags
);
1655 typedef struct guid_to_name_data
{
1658 } guid_to_name_data_t
;
1661 guid_to_name_cb(zfs_handle_t
*zhp
, void *arg
)
1663 guid_to_name_data_t
*gtnd
= arg
;
1666 if (zhp
->zfs_dmustats
.dds_guid
== gtnd
->guid
) {
1667 (void) strcpy(gtnd
->name
, zhp
->zfs_name
);
1671 err
= zfs_iter_children(zhp
, guid_to_name_cb
, gtnd
);
1677 guid_to_name(libzfs_handle_t
*hdl
, const char *parent
, uint64_t guid
,
1680 /* exhaustive search all local snapshots */
1681 guid_to_name_data_t gtnd
;
1689 if (strchr(parent
, '@') == NULL
) {
1690 zhp
= make_dataset_handle(hdl
, parent
);
1692 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
1699 cp
= strchr(parent
, '/');
1702 zhp
= make_dataset_handle(hdl
, parent
);
1707 err
= zfs_iter_children(zhp
, guid_to_name_cb
, >nd
);
1711 return (err
== EEXIST
? 0 : ENOENT
);
1716 * Return true if dataset guid1 is created before guid2.
1719 created_before(libzfs_handle_t
*hdl
, avl_tree_t
*avl
,
1720 uint64_t guid1
, uint64_t guid2
)
1723 char *fsname
, *snapname
;
1724 char buf
[ZFS_MAXNAMELEN
];
1726 zfs_node_t zn1
, zn2
;
1733 nvfs
= fsavl_find(avl
, guid1
, &snapname
);
1734 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
1735 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
1736 zn1
.zn_handle
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
1737 if (zn1
.zn_handle
== NULL
)
1740 nvfs
= fsavl_find(avl
, guid2
, &snapname
);
1741 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
1742 (void) snprintf(buf
, sizeof (buf
), "%s@%s", fsname
, snapname
);
1743 zn2
.zn_handle
= zfs_open(hdl
, buf
, ZFS_TYPE_SNAPSHOT
);
1744 if (zn2
.zn_handle
== NULL
) {
1745 zfs_close(zn2
.zn_handle
);
1749 rv
= (zfs_snapshot_compare(&zn1
, &zn2
) == -1);
1751 zfs_close(zn1
.zn_handle
);
1752 zfs_close(zn2
.zn_handle
);
1758 recv_incremental_replication(libzfs_handle_t
*hdl
, const char *tofs
,
1759 recvflags_t flags
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
1763 avl_tree_t
*local_avl
;
1764 nvpair_t
*fselem
, *nextfselem
;
1766 char newname
[ZFS_MAXNAMELEN
];
1768 boolean_t needagain
, progress
, recursive
;
1771 VERIFY(0 == nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
));
1773 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
1780 needagain
= progress
= B_FALSE
;
1782 if ((error
= gather_nvlist(hdl
, tofs
, fromsnap
, NULL
,
1783 recursive
, &local_nv
, &local_avl
)) != 0)
1787 * Process deletes and renames
1789 for (fselem
= nvlist_next_nvpair(local_nv
, NULL
);
1790 fselem
; fselem
= nextfselem
) {
1791 nvlist_t
*nvfs
, *snaps
;
1792 nvlist_t
*stream_nvfs
= NULL
;
1793 nvpair_t
*snapelem
, *nextsnapelem
;
1794 uint64_t fromguid
= 0;
1795 uint64_t originguid
= 0;
1796 uint64_t stream_originguid
= 0;
1797 uint64_t parent_fromsnap_guid
, stream_parent_fromsnap_guid
;
1798 char *fsname
, *stream_fsname
;
1800 nextfselem
= nvlist_next_nvpair(local_nv
, fselem
);
1802 VERIFY(0 == nvpair_value_nvlist(fselem
, &nvfs
));
1803 VERIFY(0 == nvlist_lookup_nvlist(nvfs
, "snaps", &snaps
));
1804 VERIFY(0 == nvlist_lookup_string(nvfs
, "name", &fsname
));
1805 VERIFY(0 == nvlist_lookup_uint64(nvfs
, "parentfromsnap",
1806 &parent_fromsnap_guid
));
1807 (void) nvlist_lookup_uint64(nvfs
, "origin", &originguid
);
1810 * First find the stream's fs, so we can check for
1811 * a different origin (due to "zfs promote")
1813 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
1814 snapelem
; snapelem
= nvlist_next_nvpair(snaps
, snapelem
)) {
1817 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
1818 stream_nvfs
= fsavl_find(stream_avl
, thisguid
, NULL
);
1820 if (stream_nvfs
!= NULL
)
1824 /* check for promote */
1825 (void) nvlist_lookup_uint64(stream_nvfs
, "origin",
1826 &stream_originguid
);
1827 if (stream_nvfs
&& originguid
!= stream_originguid
) {
1828 switch (created_before(hdl
, local_avl
,
1829 stream_originguid
, originguid
)) {
1832 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1833 nvlist_t
*origin_nvfs
;
1834 char *origin_fsname
;
1837 (void) printf("promoting %s\n", fsname
);
1839 origin_nvfs
= fsavl_find(local_avl
, originguid
,
1841 VERIFY(0 == nvlist_lookup_string(origin_nvfs
,
1842 "name", &origin_fsname
));
1843 (void) strlcpy(zc
.zc_value
, origin_fsname
,
1844 sizeof (zc
.zc_value
));
1845 (void) strlcpy(zc
.zc_name
, fsname
,
1846 sizeof (zc
.zc_name
));
1847 error
= zfs_ioctl(hdl
, ZFS_IOC_PROMOTE
, &zc
);
1855 fsavl_destroy(local_avl
);
1856 nvlist_free(local_nv
);
1860 * We had/have the wrong origin, therefore our
1861 * list of snapshots is wrong. Need to handle
1862 * them on the next pass.
1868 for (snapelem
= nvlist_next_nvpair(snaps
, NULL
);
1869 snapelem
; snapelem
= nextsnapelem
) {
1871 char *stream_snapname
;
1872 nvlist_t
*found
, *props
;
1874 nextsnapelem
= nvlist_next_nvpair(snaps
, snapelem
);
1876 VERIFY(0 == nvpair_value_uint64(snapelem
, &thisguid
));
1877 found
= fsavl_find(stream_avl
, thisguid
,
1880 /* check for delete */
1881 if (found
== NULL
) {
1882 char name
[ZFS_MAXNAMELEN
];
1887 (void) snprintf(name
, sizeof (name
), "%s@%s",
1888 fsname
, nvpair_name(snapelem
));
1890 error
= recv_destroy(hdl
, name
,
1891 strlen(fsname
)+1, newname
, flags
);
1899 stream_nvfs
= found
;
1901 if (0 == nvlist_lookup_nvlist(stream_nvfs
, "snapprops",
1902 &props
) && 0 == nvlist_lookup_nvlist(props
,
1903 stream_snapname
, &props
)) {
1904 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
1906 zc
.zc_cookie
= B_TRUE
; /* received */
1907 (void) snprintf(zc
.zc_name
, sizeof (zc
.zc_name
),
1908 "%s@%s", fsname
, nvpair_name(snapelem
));
1909 if (zcmd_write_src_nvlist(hdl
, &zc
,
1911 (void) zfs_ioctl(hdl
,
1912 ZFS_IOC_SET_PROP
, &zc
);
1913 zcmd_free_nvlists(&zc
);
1917 /* check for different snapname */
1918 if (strcmp(nvpair_name(snapelem
),
1919 stream_snapname
) != 0) {
1920 char name
[ZFS_MAXNAMELEN
];
1921 char tryname
[ZFS_MAXNAMELEN
];
1923 (void) snprintf(name
, sizeof (name
), "%s@%s",
1924 fsname
, nvpair_name(snapelem
));
1925 (void) snprintf(tryname
, sizeof (name
), "%s@%s",
1926 fsname
, stream_snapname
);
1928 error
= recv_rename(hdl
, name
, tryname
,
1929 strlen(fsname
)+1, newname
, flags
);
1936 if (strcmp(stream_snapname
, fromsnap
) == 0)
1937 fromguid
= thisguid
;
1940 /* check for delete */
1941 if (stream_nvfs
== NULL
) {
1945 error
= recv_destroy(hdl
, fsname
, strlen(tofs
)+1,
1954 if (fromguid
== 0) {
1955 if (flags
.verbose
) {
1956 (void) printf("local fs %s does not have "
1957 "fromsnap (%s in stream); must have "
1958 "been deleted locally; ignoring\n",
1964 VERIFY(0 == nvlist_lookup_string(stream_nvfs
,
1965 "name", &stream_fsname
));
1966 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs
,
1967 "parentfromsnap", &stream_parent_fromsnap_guid
));
1969 s1
= strrchr(fsname
, '/');
1970 s2
= strrchr(stream_fsname
, '/');
1973 * Check for rename. If the exact receive path is specified, it
1974 * does not count as a rename, but we still need to check the
1975 * datasets beneath it.
1977 if ((stream_parent_fromsnap_guid
!= 0 &&
1978 parent_fromsnap_guid
!= 0 &&
1979 stream_parent_fromsnap_guid
!= parent_fromsnap_guid
) ||
1980 ((flags
.isprefix
|| strcmp(tofs
, fsname
) != 0) &&
1981 (s1
!= NULL
) && (s2
!= NULL
) && strcmp(s1
, s2
) != 0)) {
1983 char tryname
[ZFS_MAXNAMELEN
];
1985 parent
= fsavl_find(local_avl
,
1986 stream_parent_fromsnap_guid
, NULL
);
1988 * NB: parent might not be found if we used the
1989 * tosnap for stream_parent_fromsnap_guid,
1990 * because the parent is a newly-created fs;
1991 * we'll be able to rename it after we recv the
1994 if (parent
!= NULL
) {
1997 VERIFY(0 == nvlist_lookup_string(parent
, "name",
1999 (void) snprintf(tryname
, sizeof (tryname
),
2000 "%s%s", pname
, strrchr(stream_fsname
, '/'));
2003 if (flags
.verbose
) {
2004 (void) printf("local fs %s new parent "
2005 "not found\n", fsname
);
2011 error
= recv_rename(hdl
, fsname
, tryname
,
2012 strlen(tofs
)+1, newname
, flags
);
2014 if (renamed
!= NULL
&& newname
[0] != '\0') {
2015 VERIFY(0 == nvlist_add_boolean(renamed
,
2026 fsavl_destroy(local_avl
);
2027 nvlist_free(local_nv
);
2029 if (needagain
&& progress
) {
2030 /* do another pass to fix up temporary names */
2032 (void) printf("another pass:\n");
2040 zfs_receive_package(libzfs_handle_t
*hdl
, int fd
, const char *destname
,
2041 recvflags_t flags
, dmu_replay_record_t
*drr
, zio_cksum_t
*zc
,
2042 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2044 nvlist_t
*stream_nv
= NULL
;
2045 avl_tree_t
*stream_avl
= NULL
;
2046 char *fromsnap
= NULL
;
2048 char tofs
[ZFS_MAXNAMELEN
];
2049 char sendfs
[ZFS_MAXNAMELEN
];
2051 dmu_replay_record_t drre
;
2053 boolean_t anyerr
= B_FALSE
;
2054 boolean_t softerr
= B_FALSE
;
2055 boolean_t recursive
;
2057 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2060 assert(drr
->drr_type
== DRR_BEGIN
);
2061 assert(drr
->drr_u
.drr_begin
.drr_magic
== DMU_BACKUP_MAGIC
);
2062 assert(DMU_GET_STREAM_HDRTYPE(drr
->drr_u
.drr_begin
.drr_versioninfo
) ==
2063 DMU_COMPOUNDSTREAM
);
2066 * Read in the nvlist from the stream.
2068 if (drr
->drr_payloadlen
!= 0) {
2069 error
= recv_read_nvlist(hdl
, fd
, drr
->drr_payloadlen
,
2070 &stream_nv
, flags
.byteswap
, zc
);
2072 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2077 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2080 if (recursive
&& strchr(destname
, '@')) {
2081 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2082 "cannot specify snapshot name for multi-snapshot stream"));
2083 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2088 * Read in the end record and verify checksum.
2090 if (0 != (error
= recv_read(hdl
, fd
, &drre
, sizeof (drre
),
2091 flags
.byteswap
, NULL
)))
2093 if (flags
.byteswap
) {
2094 drre
.drr_type
= BSWAP_32(drre
.drr_type
);
2095 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0] =
2096 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[0]);
2097 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1] =
2098 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[1]);
2099 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2] =
2100 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[2]);
2101 drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3] =
2102 BSWAP_64(drre
.drr_u
.drr_end
.drr_checksum
.zc_word
[3]);
2104 if (drre
.drr_type
!= DRR_END
) {
2105 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2108 if (!ZIO_CHECKSUM_EQUAL(drre
.drr_u
.drr_end
.drr_checksum
, *zc
)) {
2109 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2110 "incorrect header checksum"));
2111 error
= zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2115 (void) nvlist_lookup_string(stream_nv
, "fromsnap", &fromsnap
);
2117 if (drr
->drr_payloadlen
!= 0) {
2118 nvlist_t
*stream_fss
;
2120 VERIFY(0 == nvlist_lookup_nvlist(stream_nv
, "fss",
2122 if ((stream_avl
= fsavl_create(stream_fss
)) == NULL
) {
2123 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2124 "couldn't allocate avl tree"));
2125 error
= zfs_error(hdl
, EZFS_NOMEM
, errbuf
);
2129 if (fromsnap
!= NULL
) {
2130 nvlist_t
*renamed
= NULL
;
2131 nvpair_t
*pair
= NULL
;
2133 (void) strlcpy(tofs
, destname
, ZFS_MAXNAMELEN
);
2134 if (flags
.isprefix
) {
2135 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2139 cp
= strrchr(drrb
->drr_toname
, '/');
2141 (void) strlcat(tofs
, "/",
2145 i
= (cp
- drrb
->drr_toname
);
2148 i
= strcspn(drrb
->drr_toname
, "/@");
2150 /* zfs_receive_one() will create_parents() */
2151 (void) strlcat(tofs
, &drrb
->drr_toname
[i
],
2153 *strchr(tofs
, '@') = '\0';
2156 if (recursive
&& !flags
.dryrun
&& !flags
.nomount
) {
2157 VERIFY(0 == nvlist_alloc(&renamed
,
2158 NV_UNIQUE_NAME
, 0));
2161 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2162 stream_nv
, stream_avl
, renamed
);
2164 /* Unmount renamed filesystems before receiving. */
2165 while ((pair
= nvlist_next_nvpair(renamed
,
2168 prop_changelist_t
*clp
= NULL
;
2170 zhp
= zfs_open(hdl
, nvpair_name(pair
),
2171 ZFS_TYPE_FILESYSTEM
);
2173 clp
= changelist_gather(zhp
,
2174 ZFS_PROP_MOUNTPOINT
, 0, 0);
2178 changelist_prefix(clp
);
2179 changelist_free(clp
);
2184 nvlist_free(renamed
);
2189 * Get the fs specified by the first path in the stream (the top level
2190 * specified by 'zfs send') and pass it to each invocation of
2191 * zfs_receive_one().
2193 (void) strlcpy(sendfs
, drr
->drr_u
.drr_begin
.drr_toname
,
2195 if ((cp
= strchr(sendfs
, '@')) != NULL
)
2198 /* Finally, receive each contained stream */
2201 * we should figure out if it has a recoverable
2202 * error, in which case do a recv_skip() and drive on.
2203 * Note, if we fail due to already having this guid,
2204 * zfs_receive_one() will take care of it (ie,
2205 * recv_skip() and return 0).
2207 error
= zfs_receive_impl(hdl
, destname
, flags
, fd
,
2208 sendfs
, stream_nv
, stream_avl
, top_zfs
, cleanup_fd
,
2210 if (error
== ENODATA
) {
2215 } while (error
== 0);
2217 if (drr
->drr_payloadlen
!= 0 && fromsnap
!= NULL
) {
2219 * Now that we have the fs's they sent us, try the
2222 softerr
= recv_incremental_replication(hdl
, tofs
, flags
,
2223 stream_nv
, stream_avl
, NULL
);
2227 fsavl_destroy(stream_avl
);
2229 nvlist_free(stream_nv
);
2238 trunc_prop_errs(int truncated
)
2240 ASSERT(truncated
!= 0);
2243 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2244 "1 more property could not be set\n"));
2246 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
,
2247 "%d more properties could not be set\n"), truncated
);
2251 recv_skip(libzfs_handle_t
*hdl
, int fd
, boolean_t byteswap
)
2253 dmu_replay_record_t
*drr
;
2254 void *buf
= malloc(1<<20);
2257 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2258 "cannot receive:"));
2260 /* XXX would be great to use lseek if possible... */
2263 while (recv_read(hdl
, fd
, drr
, sizeof (dmu_replay_record_t
),
2264 byteswap
, NULL
) == 0) {
2266 drr
->drr_type
= BSWAP_32(drr
->drr_type
);
2268 switch (drr
->drr_type
) {
2270 /* NB: not to be used on v2 stream packages */
2271 if (drr
->drr_payloadlen
!= 0) {
2272 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2273 "invalid substream header"));
2274 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2284 drr
->drr_u
.drr_object
.drr_bonuslen
=
2285 BSWAP_32(drr
->drr_u
.drr_object
.
2288 (void) recv_read(hdl
, fd
, buf
,
2289 P2ROUNDUP(drr
->drr_u
.drr_object
.drr_bonuslen
, 8),
2295 drr
->drr_u
.drr_write
.drr_length
=
2296 BSWAP_64(drr
->drr_u
.drr_write
.drr_length
);
2298 (void) recv_read(hdl
, fd
, buf
,
2299 drr
->drr_u
.drr_write
.drr_length
, B_FALSE
, NULL
);
2303 drr
->drr_u
.drr_write
.drr_length
=
2304 BSWAP_64(drr
->drr_u
.drr_spill
.drr_length
);
2306 (void) recv_read(hdl
, fd
, buf
,
2307 drr
->drr_u
.drr_spill
.drr_length
, B_FALSE
, NULL
);
2309 case DRR_WRITE_BYREF
:
2310 case DRR_FREEOBJECTS
:
2315 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2316 "invalid record type"));
2317 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2326 * Restores a backup of tosnap from the file descriptor specified by infd.
2329 zfs_receive_one(libzfs_handle_t
*hdl
, int infd
, const char *tosnap
,
2330 recvflags_t flags
, dmu_replay_record_t
*drr
,
2331 dmu_replay_record_t
*drr_noswap
, const char *sendfs
,
2332 nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
, char **top_zfs
, int cleanup_fd
,
2333 uint64_t *action_handlep
)
2335 zfs_cmd_t zc
= { "\0", "\0", "\0", "\0", 0 };
2337 int ioctl_err
, ioctl_errno
, err
;
2339 struct drr_begin
*drrb
= &drr
->drr_u
.drr_begin
;
2341 char prop_errbuf
[1024];
2342 const char *chopprefix
;
2343 boolean_t newfs
= B_FALSE
;
2344 boolean_t stream_wantsnewfs
;
2345 uint64_t parent_snapguid
= 0;
2346 prop_changelist_t
*clp
= NULL
;
2347 nvlist_t
*snapprops_nvlist
= NULL
;
2348 zprop_errflags_t prop_errflags
;
2349 boolean_t recursive
;
2351 begin_time
= time(NULL
);
2353 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2356 recursive
= (nvlist_lookup_boolean(stream_nv
, "not_recursive") ==
2359 if (stream_avl
!= NULL
) {
2361 nvlist_t
*fs
= fsavl_find(stream_avl
, drrb
->drr_toguid
,
2366 (void) nvlist_lookup_uint64(fs
, "parentfromsnap",
2368 err
= nvlist_lookup_nvlist(fs
, "props", &props
);
2370 VERIFY(0 == nvlist_alloc(&props
, NV_UNIQUE_NAME
, 0));
2372 if (flags
.canmountoff
) {
2373 VERIFY(0 == nvlist_add_uint64(props
,
2374 zfs_prop_to_name(ZFS_PROP_CANMOUNT
), 0));
2376 ret
= zcmd_write_src_nvlist(hdl
, &zc
, props
);
2380 if (0 == nvlist_lookup_nvlist(fs
, "snapprops", &props
)) {
2381 VERIFY(0 == nvlist_lookup_nvlist(props
,
2382 snapname
, &snapprops_nvlist
));
2392 * Determine how much of the snapshot name stored in the stream
2393 * we are going to tack on to the name they specified on the
2394 * command line, and how much we are going to chop off.
2396 * If they specified a snapshot, chop the entire name stored in
2401 * A filesystem was specified with -e. We want to tack on only
2402 * the tail of the sent snapshot path.
2404 if (strchr(tosnap
, '@')) {
2405 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2406 "argument - snapshot not allowed with -e"));
2407 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2410 chopprefix
= strrchr(sendfs
, '/');
2412 if (chopprefix
== NULL
) {
2414 * The tail is the poolname, so we need to
2415 * prepend a path separator.
2417 int len
= strlen(drrb
->drr_toname
);
2418 cp
= malloc(len
+ 2);
2420 (void) strcpy(&cp
[1], drrb
->drr_toname
);
2423 chopprefix
= drrb
->drr_toname
+ (chopprefix
- sendfs
);
2425 } else if (flags
.isprefix
) {
2427 * A filesystem was specified with -d. We want to tack on
2428 * everything but the first element of the sent snapshot path
2429 * (all but the pool name).
2431 if (strchr(tosnap
, '@')) {
2432 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2433 "argument - snapshot not allowed with -d"));
2434 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2437 chopprefix
= strchr(drrb
->drr_toname
, '/');
2438 if (chopprefix
== NULL
)
2439 chopprefix
= strchr(drrb
->drr_toname
, '@');
2440 } else if (strchr(tosnap
, '@') == NULL
) {
2442 * If a filesystem was specified without -d or -e, we want to
2443 * tack on everything after the fs specified by 'zfs send'.
2445 chopprefix
= drrb
->drr_toname
+ strlen(sendfs
);
2447 /* A snapshot was specified as an exact path (no -d or -e). */
2449 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2450 "cannot specify snapshot name for multi-snapshot "
2452 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2454 chopprefix
= drrb
->drr_toname
+ strlen(drrb
->drr_toname
);
2457 ASSERT(strstr(drrb
->drr_toname
, sendfs
) == drrb
->drr_toname
);
2458 ASSERT(chopprefix
> drrb
->drr_toname
);
2459 ASSERT(chopprefix
<= drrb
->drr_toname
+ strlen(drrb
->drr_toname
));
2460 ASSERT(chopprefix
[0] == '/' || chopprefix
[0] == '@' ||
2461 chopprefix
[0] == '\0');
2464 * Determine name of destination snapshot, store in zc_value.
2466 (void) strcpy(zc
.zc_top_ds
, tosnap
);
2467 (void) strcpy(zc
.zc_value
, tosnap
);
2468 (void) strlcat(zc
.zc_value
, chopprefix
, sizeof (zc
.zc_value
));
2470 if (!zfs_name_valid(zc
.zc_value
, ZFS_TYPE_SNAPSHOT
)) {
2471 zcmd_free_nvlists(&zc
);
2472 return (zfs_error(hdl
, EZFS_INVALIDNAME
, errbuf
));
2476 * Determine the name of the origin snapshot, store in zc_string.
2478 if (drrb
->drr_flags
& DRR_FLAG_CLONE
) {
2479 if (guid_to_name(hdl
, tosnap
,
2480 drrb
->drr_fromguid
, zc
.zc_string
) != 0) {
2481 zcmd_free_nvlists(&zc
);
2482 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2483 "local origin for clone %s does not exist"),
2485 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2488 (void) printf("found clone origin %s\n", zc
.zc_string
);
2491 stream_wantsnewfs
= (drrb
->drr_fromguid
== 0 ||
2492 (drrb
->drr_flags
& DRR_FLAG_CLONE
));
2494 if (stream_wantsnewfs
) {
2496 * if the parent fs does not exist, look for it based on
2497 * the parent snap GUID
2499 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2500 "cannot receive new filesystem stream"));
2502 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2503 cp
= strrchr(zc
.zc_name
, '/');
2507 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2508 char suffix
[ZFS_MAXNAMELEN
];
2509 (void) strcpy(suffix
, strrchr(zc
.zc_value
, '/'));
2510 if (guid_to_name(hdl
, tosnap
, parent_snapguid
,
2511 zc
.zc_value
) == 0) {
2512 *strchr(zc
.zc_value
, '@') = '\0';
2513 (void) strcat(zc
.zc_value
, suffix
);
2518 * if the fs does not exist, look for it based on the
2521 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2522 "cannot receive incremental stream"));
2524 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2525 *strchr(zc
.zc_name
, '@') = '\0';
2528 * If the exact receive path was specified and this is the
2529 * topmost path in the stream, then if the fs does not exist we
2530 * should look no further.
2532 if ((flags
.isprefix
|| (*(chopprefix
= drrb
->drr_toname
+
2533 strlen(sendfs
)) != '\0' && *chopprefix
!= '@')) &&
2534 !zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2535 char snap
[ZFS_MAXNAMELEN
];
2536 (void) strcpy(snap
, strchr(zc
.zc_value
, '@'));
2537 if (guid_to_name(hdl
, tosnap
, drrb
->drr_fromguid
,
2538 zc
.zc_value
) == 0) {
2539 *strchr(zc
.zc_value
, '@') = '\0';
2540 (void) strcat(zc
.zc_value
, snap
);
2545 (void) strcpy(zc
.zc_name
, zc
.zc_value
);
2546 *strchr(zc
.zc_name
, '@') = '\0';
2548 if (zfs_dataset_exists(hdl
, zc
.zc_name
, ZFS_TYPE_DATASET
)) {
2552 * Destination fs exists. Therefore this should either
2553 * be an incremental, or the stream specifies a new fs
2554 * (full stream or clone) and they want us to blow it
2555 * away (and have therefore specified -F and removed any
2558 if (stream_wantsnewfs
) {
2560 zcmd_free_nvlists(&zc
);
2561 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2562 "destination '%s' exists\n"
2563 "must specify -F to overwrite it"),
2565 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2567 if (ioctl(hdl
->libzfs_fd
, ZFS_IOC_SNAPSHOT_LIST_NEXT
,
2569 zcmd_free_nvlists(&zc
);
2570 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2571 "destination has snapshots (eg. %s)\n"
2572 "must destroy them to overwrite it"),
2574 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2578 if ((zhp
= zfs_open(hdl
, zc
.zc_name
,
2579 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
)) == NULL
) {
2580 zcmd_free_nvlists(&zc
);
2584 if (stream_wantsnewfs
&&
2585 zhp
->zfs_dmustats
.dds_origin
[0]) {
2586 zcmd_free_nvlists(&zc
);
2588 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2589 "destination '%s' is a clone\n"
2590 "must destroy it to overwrite it"),
2592 return (zfs_error(hdl
, EZFS_EXISTS
, errbuf
));
2595 if (!flags
.dryrun
&& zhp
->zfs_type
== ZFS_TYPE_FILESYSTEM
&&
2596 stream_wantsnewfs
) {
2597 /* We can't do online recv in this case */
2598 clp
= changelist_gather(zhp
, ZFS_PROP_NAME
, 0, 0);
2601 zcmd_free_nvlists(&zc
);
2604 if (changelist_prefix(clp
) != 0) {
2605 changelist_free(clp
);
2607 zcmd_free_nvlists(&zc
);
2611 if (!flags
.dryrun
&& zhp
->zfs_type
== ZFS_TYPE_VOLUME
&&
2612 zvol_remove_link(hdl
, zhp
->zfs_name
) != 0) {
2614 zcmd_free_nvlists(&zc
);
2620 * Destination filesystem does not exist. Therefore we better
2621 * be creating a new filesystem (either from a full backup, or
2622 * a clone). It would therefore be invalid if the user
2623 * specified only the pool name (i.e. if the destination name
2624 * contained no slash character).
2626 if (!stream_wantsnewfs
||
2627 (cp
= strrchr(zc
.zc_name
, '/')) == NULL
) {
2628 zcmd_free_nvlists(&zc
);
2629 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2630 "destination '%s' does not exist"), zc
.zc_name
);
2631 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2635 * Trim off the final dataset component so we perform the
2636 * recvbackup ioctl to the filesystems's parent.
2640 if (flags
.isprefix
&& !flags
.istail
&& !flags
.dryrun
&&
2641 create_parents(hdl
, zc
.zc_value
, strlen(tosnap
)) != 0) {
2642 zcmd_free_nvlists(&zc
);
2643 return (zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
));
2649 zc
.zc_begin_record
= drr_noswap
->drr_u
.drr_begin
;
2650 zc
.zc_cookie
= infd
;
2651 zc
.zc_guid
= flags
.force
;
2652 if (flags
.verbose
) {
2653 (void) printf("%s %s stream of %s into %s\n",
2654 flags
.dryrun
? "would receive" : "receiving",
2655 drrb
->drr_fromguid
? "incremental" : "full",
2656 drrb
->drr_toname
, zc
.zc_value
);
2657 (void) fflush(stdout
);
2661 zcmd_free_nvlists(&zc
);
2662 return (recv_skip(hdl
, infd
, flags
.byteswap
));
2665 zc
.zc_nvlist_dst
= (uint64_t)(uintptr_t)prop_errbuf
;
2666 zc
.zc_nvlist_dst_size
= sizeof (prop_errbuf
);
2667 zc
.zc_cleanup_fd
= cleanup_fd
;
2668 zc
.zc_action_handle
= *action_handlep
;
2670 err
= ioctl_err
= zfs_ioctl(hdl
, ZFS_IOC_RECV
, &zc
);
2671 ioctl_errno
= errno
;
2672 prop_errflags
= (zprop_errflags_t
)zc
.zc_obj
;
2675 nvlist_t
*prop_errors
;
2676 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc
.zc_nvlist_dst
,
2677 zc
.zc_nvlist_dst_size
, &prop_errors
, 0));
2679 nvpair_t
*prop_err
= NULL
;
2681 while ((prop_err
= nvlist_next_nvpair(prop_errors
,
2682 prop_err
)) != NULL
) {
2687 prop
= zfs_name_to_prop(nvpair_name(prop_err
));
2688 (void) nvpair_value_int32(prop_err
, &intval
);
2689 if (strcmp(nvpair_name(prop_err
),
2690 ZPROP_N_MORE_ERRORS
) == 0) {
2691 trunc_prop_errs(intval
);
2694 (void) snprintf(tbuf
, sizeof (tbuf
),
2695 dgettext(TEXT_DOMAIN
,
2696 "cannot receive %s property on %s"),
2697 nvpair_name(prop_err
), zc
.zc_name
);
2698 zfs_setprop_error(hdl
, prop
, intval
, tbuf
);
2701 nvlist_free(prop_errors
);
2704 zc
.zc_nvlist_dst
= 0;
2705 zc
.zc_nvlist_dst_size
= 0;
2706 zcmd_free_nvlists(&zc
);
2708 if (err
== 0 && snapprops_nvlist
) {
2709 zfs_cmd_t zc2
= { "\0", "\0", "\0", "\0", 0 };
2711 (void) strcpy(zc2
.zc_name
, zc
.zc_value
);
2712 zc2
.zc_cookie
= B_TRUE
; /* received */
2713 if (zcmd_write_src_nvlist(hdl
, &zc2
, snapprops_nvlist
) == 0) {
2714 (void) zfs_ioctl(hdl
, ZFS_IOC_SET_PROP
, &zc2
);
2715 zcmd_free_nvlists(&zc2
);
2719 if (err
&& (ioctl_errno
== ENOENT
|| ioctl_errno
== EEXIST
)) {
2721 * It may be that this snapshot already exists,
2722 * in which case we want to consume & ignore it
2723 * rather than failing.
2725 avl_tree_t
*local_avl
;
2726 nvlist_t
*local_nv
, *fs
;
2727 cp
= strchr(zc
.zc_value
, '@');
2730 * XXX Do this faster by just iterating over snaps in
2731 * this fs. Also if zc_value does not exist, we will
2732 * get a strange "does not exist" error message.
2735 if (gather_nvlist(hdl
, zc
.zc_value
, NULL
, NULL
, B_FALSE
,
2736 &local_nv
, &local_avl
) == 0) {
2738 fs
= fsavl_find(local_avl
, drrb
->drr_toguid
, NULL
);
2739 fsavl_destroy(local_avl
);
2740 nvlist_free(local_nv
);
2743 if (flags
.verbose
) {
2744 (void) printf("snap %s already exists; "
2745 "ignoring\n", zc
.zc_value
);
2747 err
= ioctl_err
= recv_skip(hdl
, infd
,
2754 if (ioctl_err
!= 0) {
2755 switch (ioctl_errno
) {
2757 cp
= strchr(zc
.zc_value
, '@');
2759 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2760 "most recent snapshot of %s does not\n"
2761 "match incremental source"), zc
.zc_value
);
2762 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
2766 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2767 "destination %s has been modified\n"
2768 "since most recent snapshot"), zc
.zc_name
);
2769 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
2772 cp
= strchr(zc
.zc_value
, '@');
2774 /* it's the containing fs that exists */
2777 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2778 "destination already exists"));
2779 (void) zfs_error_fmt(hdl
, EZFS_EXISTS
,
2780 dgettext(TEXT_DOMAIN
, "cannot restore to %s"),
2785 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2788 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2789 "invalid stream (checksum mismatch)"));
2790 (void) zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
);
2793 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2794 "pool must be upgraded to receive this stream."));
2795 (void) zfs_error(hdl
, EZFS_BADVERSION
, errbuf
);
2798 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2799 "destination %s space quota exceeded"), zc
.zc_name
);
2800 (void) zfs_error(hdl
, EZFS_BADRESTORE
, errbuf
);
2803 (void) zfs_standard_error(hdl
, ioctl_errno
, errbuf
);
2808 * Mount the target filesystem (if created). Also mount any
2809 * children of the target filesystem if we did a replication
2810 * receive (indicated by stream_avl being non-NULL).
2812 cp
= strchr(zc
.zc_value
, '@');
2813 if (cp
&& (ioctl_err
== 0 || !newfs
)) {
2817 h
= zfs_open(hdl
, zc
.zc_value
,
2818 ZFS_TYPE_FILESYSTEM
| ZFS_TYPE_VOLUME
);
2820 if (h
->zfs_type
== ZFS_TYPE_VOLUME
) {
2822 err
= zvol_create_link(hdl
, h
->zfs_name
);
2823 if (err
== 0 && ioctl_err
== 0)
2824 err
= zvol_create_link(hdl
,
2826 } else if (newfs
|| stream_avl
) {
2828 * Track the first/top of hierarchy fs,
2829 * for mounting and sharing later.
2831 if (top_zfs
&& *top_zfs
== NULL
)
2832 *top_zfs
= zfs_strdup(hdl
, zc
.zc_value
);
2840 err
|= changelist_postfix(clp
);
2841 changelist_free(clp
);
2844 if (prop_errflags
& ZPROP_ERR_NOCLEAR
) {
2845 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
2846 "failed to clear unreceived properties on %s"),
2848 (void) fprintf(stderr
, "\n");
2850 if (prop_errflags
& ZPROP_ERR_NORESTORE
) {
2851 (void) fprintf(stderr
, dgettext(TEXT_DOMAIN
, "Warning: "
2852 "failed to restore original properties on %s"),
2854 (void) fprintf(stderr
, "\n");
2857 if (err
|| ioctl_err
)
2860 *action_handlep
= zc
.zc_action_handle
;
2862 if (flags
.verbose
) {
2865 uint64_t bytes
= zc
.zc_cookie
;
2866 time_t delta
= time(NULL
) - begin_time
;
2869 zfs_nicenum(bytes
, buf1
, sizeof (buf1
));
2870 zfs_nicenum(bytes
/delta
, buf2
, sizeof (buf1
));
2872 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
2880 zfs_receive_impl(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t flags
,
2881 int infd
, const char *sendfs
, nvlist_t
*stream_nv
, avl_tree_t
*stream_avl
,
2882 char **top_zfs
, int cleanup_fd
, uint64_t *action_handlep
)
2885 dmu_replay_record_t drr
, drr_noswap
;
2886 struct drr_begin
*drrb
= &drr
.drr_u
.drr_begin
;
2888 zio_cksum_t zcksum
= { { 0 } };
2889 uint64_t featureflags
;
2892 (void) snprintf(errbuf
, sizeof (errbuf
), dgettext(TEXT_DOMAIN
,
2895 if (flags
.isprefix
&&
2896 !zfs_dataset_exists(hdl
, tosnap
, ZFS_TYPE_DATASET
)) {
2897 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "specified fs "
2898 "(%s) does not exist"), tosnap
);
2899 return (zfs_error(hdl
, EZFS_NOENT
, errbuf
));
2902 /* read in the BEGIN record */
2903 if (0 != (err
= recv_read(hdl
, infd
, &drr
, sizeof (drr
), B_FALSE
,
2907 if (drr
.drr_type
== DRR_END
|| drr
.drr_type
== BSWAP_32(DRR_END
)) {
2908 /* It's the double end record at the end of a package */
2912 /* the kernel needs the non-byteswapped begin record */
2915 flags
.byteswap
= B_FALSE
;
2916 if (drrb
->drr_magic
== BSWAP_64(DMU_BACKUP_MAGIC
)) {
2918 * We computed the checksum in the wrong byteorder in
2919 * recv_read() above; do it again correctly.
2921 bzero(&zcksum
, sizeof (zio_cksum_t
));
2922 fletcher_4_incremental_byteswap(&drr
, sizeof (drr
), &zcksum
);
2923 flags
.byteswap
= B_TRUE
;
2925 drr
.drr_type
= BSWAP_32(drr
.drr_type
);
2926 drr
.drr_payloadlen
= BSWAP_32(drr
.drr_payloadlen
);
2927 drrb
->drr_magic
= BSWAP_64(drrb
->drr_magic
);
2928 drrb
->drr_versioninfo
= BSWAP_64(drrb
->drr_versioninfo
);
2929 drrb
->drr_creation_time
= BSWAP_64(drrb
->drr_creation_time
);
2930 drrb
->drr_type
= BSWAP_32(drrb
->drr_type
);
2931 drrb
->drr_flags
= BSWAP_32(drrb
->drr_flags
);
2932 drrb
->drr_toguid
= BSWAP_64(drrb
->drr_toguid
);
2933 drrb
->drr_fromguid
= BSWAP_64(drrb
->drr_fromguid
);
2936 if (drrb
->drr_magic
!= DMU_BACKUP_MAGIC
|| drr
.drr_type
!= DRR_BEGIN
) {
2937 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2938 "stream (bad magic number)"));
2939 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2942 featureflags
= DMU_GET_FEATUREFLAGS(drrb
->drr_versioninfo
);
2943 hdrtype
= DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
);
2945 if (!DMU_STREAM_SUPPORTED(featureflags
) ||
2946 (hdrtype
!= DMU_SUBSTREAM
&& hdrtype
!= DMU_COMPOUNDSTREAM
)) {
2947 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
,
2948 "stream has unsupported feature, feature flags = %lx"),
2950 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2953 if (strchr(drrb
->drr_toname
, '@') == NULL
) {
2954 zfs_error_aux(hdl
, dgettext(TEXT_DOMAIN
, "invalid "
2955 "stream (bad snapshot name)"));
2956 return (zfs_error(hdl
, EZFS_BADSTREAM
, errbuf
));
2959 if (DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) == DMU_SUBSTREAM
) {
2960 char nonpackage_sendfs
[ZFS_MAXNAMELEN
];
2961 if (sendfs
== NULL
) {
2963 * We were not called from zfs_receive_package(). Get
2964 * the fs specified by 'zfs send'.
2967 (void) strlcpy(nonpackage_sendfs
,
2968 drr
.drr_u
.drr_begin
.drr_toname
, ZFS_MAXNAMELEN
);
2969 if ((cp
= strchr(nonpackage_sendfs
, '@')) != NULL
)
2971 sendfs
= nonpackage_sendfs
;
2973 return (zfs_receive_one(hdl
, infd
, tosnap
, flags
,
2974 &drr
, &drr_noswap
, sendfs
, stream_nv
, stream_avl
,
2975 top_zfs
, cleanup_fd
, action_handlep
));
2977 assert(DMU_GET_STREAM_HDRTYPE(drrb
->drr_versioninfo
) ==
2978 DMU_COMPOUNDSTREAM
);
2979 return (zfs_receive_package(hdl
, infd
, tosnap
, flags
,
2980 &drr
, &zcksum
, top_zfs
, cleanup_fd
, action_handlep
));
2985 * Restores a backup of tosnap from the file descriptor specified by infd.
2986 * Return 0 on total success, -2 if some things couldn't be
2987 * destroyed/renamed/promoted, -1 if some things couldn't be received.
2988 * (-1 will override -2).
2991 zfs_receive(libzfs_handle_t
*hdl
, const char *tosnap
, recvflags_t flags
,
2992 int infd
, avl_tree_t
*stream_avl
)
2994 char *top_zfs
= NULL
;
2997 uint64_t action_handle
= 0;
2999 cleanup_fd
= open(ZFS_DEV
, O_RDWR
);
3000 VERIFY(cleanup_fd
>= 0);
3002 err
= zfs_receive_impl(hdl
, tosnap
, flags
, infd
, NULL
, NULL
,
3003 stream_avl
, &top_zfs
, cleanup_fd
, &action_handle
);
3005 VERIFY(0 == close(cleanup_fd
));
3007 if (err
== 0 && !flags
.nomount
&& top_zfs
) {
3009 prop_changelist_t
*clp
;
3011 zhp
= zfs_open(hdl
, top_zfs
, ZFS_TYPE_FILESYSTEM
);
3013 clp
= changelist_gather(zhp
, ZFS_PROP_MOUNTPOINT
,
3014 CL_GATHER_MOUNT_ALWAYS
, 0);
3017 /* mount and share received datasets */
3018 err
= changelist_postfix(clp
);
3019 changelist_free(clp
);
3022 if (zhp
== NULL
|| clp
== NULL
|| err
)