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) 2012 by Delphix. All rights reserved.
25 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26 * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27 * All rights reserved.
40 #include <sys/param.h>
41 #include <sys/mount.h>
48 #include "zfs_namecheck.h"
50 #include "zfs_fletcher.h"
51 #include "libzfs_impl.h"
53 #include <sys/zio_checksum.h>
56 /* in libzfs_dataset.c */
57 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
58 /* We need to use something for ENODATA. */
61 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t *,
62 int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *);
64 static const zio_cksum_t zero_cksum = { 0 };
66 typedef struct dedup_arg {
69 libzfs_handle_t *dedup_hdl;
72 typedef struct progress_arg {
75 boolean_t pa_parsable;
78 typedef struct dataref {
84 typedef struct dedup_entry {
85 struct dedup_entry *dde_next;
86 zio_cksum_t dde_chksum;
91 #define MAX_DDT_PHYSMEM_PERCENT 20
92 #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
94 typedef struct dedup_table {
95 dedup_entry_t **dedup_hash_array;
96 umem_cache_t *ddecache;
97 uint64_t max_ddt_size; /* max dedup table size in bytes */
98 uint64_t cur_ddt_size; /* current dedup table size in bytes */
105 high_order_bit(uint64_t n)
109 for (count = 0; n != 0; count++)
115 ssread(void *buf, size_t len, FILE *stream)
119 if ((outlen = fread(buf, len, 1, stream)) == 0)
126 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
127 zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
131 if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
132 if (ddt->ddt_full == B_FALSE) {
133 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
134 "Dedup table full. Deduplication will continue "
135 "with existing table entries"));
136 ddt->ddt_full = B_TRUE;
141 if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
143 assert(*ddepp == NULL);
144 dde->dde_next = NULL;
145 dde->dde_chksum = *cs;
146 dde->dde_prop = prop;
149 ddt->cur_ddt_size += sizeof (dedup_entry_t);
155 * Using the specified dedup table, do a lookup for an entry with
156 * the checksum cs. If found, return the block's reference info
157 * in *dr. Otherwise, insert a new entry in the dedup table, using
158 * the reference information specified by *dr.
160 * return value: true - entry was found
161 * false - entry was not found
164 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
165 uint64_t prop, dataref_t *dr)
168 dedup_entry_t **ddepp;
170 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
172 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
173 ddepp = &((*ddepp)->dde_next)) {
174 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
175 (*ddepp)->dde_prop == prop) {
176 *dr = (*ddepp)->dde_ref;
180 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
185 cksum_and_write(const void *buf, uint64_t len, zio_cksum_t *zc, int outfd)
187 fletcher_4_incremental_native(buf, len, zc);
188 return (write(outfd, buf, len));
192 * This function is started in a separate thread when the dedup option
193 * has been requested. The main send thread determines the list of
194 * snapshots to be included in the send stream and makes the ioctl calls
195 * for each one. But instead of having the ioctl send the output to the
196 * the output fd specified by the caller of zfs_send()), the
197 * ioctl is told to direct the output to a pipe, which is read by the
198 * alternate thread running THIS function. This function does the
200 * 1. building a dedup table (the DDT)
201 * 2. doing checksums on each data block and inserting a record in the DDT
202 * 3. looking for matching checksums, and
203 * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
204 * a duplicate block is found.
205 * The output of this function then goes to the output fd requested
206 * by the caller of zfs_send().
211 dedup_arg_t *dda = arg;
212 char *buf = malloc(1<<20);
213 dmu_replay_record_t thedrr;
214 dmu_replay_record_t *drr = &thedrr;
215 struct drr_begin *drrb = &thedrr.drr_u.drr_begin;
216 struct drr_end *drre = &thedrr.drr_u.drr_end;
217 struct drr_object *drro = &thedrr.drr_u.drr_object;
218 struct drr_write *drrw = &thedrr.drr_u.drr_write;
219 struct drr_spill *drrs = &thedrr.drr_u.drr_spill;
222 dmu_replay_record_t wbr_drr = {0};
223 struct drr_write_byref *wbr_drrr = &wbr_drr.drr_u.drr_write_byref;
225 zio_cksum_t stream_cksum;
226 uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
230 MAX((physmem * MAX_DDT_PHYSMEM_PERCENT)/100,
231 SMALLEST_POSSIBLE_MAX_DDT_MB<<20);
233 numbuckets = ddt.max_ddt_size/(sizeof (dedup_entry_t));
236 * numbuckets must be a power of 2. Increase number to
237 * a power of 2 if necessary.
239 if (!ISP2(numbuckets))
240 numbuckets = 1 << high_order_bit(numbuckets);
242 ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
243 ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
244 NULL, NULL, NULL, NULL, NULL, 0);
245 ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
246 ddt.numhashbits = high_order_bit(numbuckets) - 1;
247 ddt.ddt_full = B_FALSE;
249 /* Initialize the write-by-reference block. */
250 wbr_drr.drr_type = DRR_WRITE_BYREF;
251 wbr_drr.drr_payloadlen = 0;
253 outfd = dda->outputfd;
254 ofp = fdopen(dda->inputfd, "r");
255 while (ssread(drr, sizeof (dmu_replay_record_t), ofp) != 0) {
257 switch (drr->drr_type) {
261 ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
263 /* set the DEDUP feature flag for this stream */
264 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
265 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
266 DMU_BACKUP_FEATURE_DEDUPPROPS);
267 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
269 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
270 &stream_cksum, outfd) == -1)
272 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
273 DMU_COMPOUNDSTREAM && drr->drr_payloadlen != 0) {
274 int sz = drr->drr_payloadlen;
280 (void) ssread(buf, sz, ofp);
283 if (cksum_and_write(buf, sz, &stream_cksum,
292 /* use the recalculated checksum */
293 ZIO_SET_CHECKSUM(&drre->drr_checksum,
294 stream_cksum.zc_word[0], stream_cksum.zc_word[1],
295 stream_cksum.zc_word[2], stream_cksum.zc_word[3]);
296 if ((write(outfd, drr,
297 sizeof (dmu_replay_record_t))) == -1)
304 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
305 &stream_cksum, outfd) == -1)
307 if (drro->drr_bonuslen > 0) {
309 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
311 if (cksum_and_write(buf,
312 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
313 &stream_cksum, outfd) == -1)
321 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
322 &stream_cksum, outfd) == -1)
324 (void) ssread(buf, drrs->drr_length, ofp);
325 if (cksum_and_write(buf, drrs->drr_length,
326 &stream_cksum, outfd) == -1)
331 case DRR_FREEOBJECTS:
333 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
334 &stream_cksum, outfd) == -1)
343 (void) ssread(buf, drrw->drr_length, ofp);
346 * Use the existing checksum if it's dedup-capable,
347 * else calculate a SHA256 checksum for it.
350 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
352 !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
354 zio_cksum_t tmpsha256;
357 SHA256Update(&ctx, buf, drrw->drr_length);
358 SHA256Final(&tmpsha256, &ctx);
359 drrw->drr_key.ddk_cksum.zc_word[0] =
360 BE_64(tmpsha256.zc_word[0]);
361 drrw->drr_key.ddk_cksum.zc_word[1] =
362 BE_64(tmpsha256.zc_word[1]);
363 drrw->drr_key.ddk_cksum.zc_word[2] =
364 BE_64(tmpsha256.zc_word[2]);
365 drrw->drr_key.ddk_cksum.zc_word[3] =
366 BE_64(tmpsha256.zc_word[3]);
367 drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
368 drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
371 dataref.ref_guid = drrw->drr_toguid;
372 dataref.ref_object = drrw->drr_object;
373 dataref.ref_offset = drrw->drr_offset;
375 if (ddt_update(dda->dedup_hdl, &ddt,
376 &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
378 /* block already present in stream */
379 wbr_drrr->drr_object = drrw->drr_object;
380 wbr_drrr->drr_offset = drrw->drr_offset;
381 wbr_drrr->drr_length = drrw->drr_length;
382 wbr_drrr->drr_toguid = drrw->drr_toguid;
383 wbr_drrr->drr_refguid = dataref.ref_guid;
384 wbr_drrr->drr_refobject =
386 wbr_drrr->drr_refoffset =
389 wbr_drrr->drr_checksumtype =
390 drrw->drr_checksumtype;
391 wbr_drrr->drr_checksumflags =
392 drrw->drr_checksumtype;
393 wbr_drrr->drr_key.ddk_cksum =
394 drrw->drr_key.ddk_cksum;
395 wbr_drrr->drr_key.ddk_prop =
396 drrw->drr_key.ddk_prop;
398 if (cksum_and_write(&wbr_drr,
399 sizeof (dmu_replay_record_t), &stream_cksum,
403 /* block not previously seen */
404 if (cksum_and_write(drr,
405 sizeof (dmu_replay_record_t), &stream_cksum,
408 if (cksum_and_write(buf,
410 &stream_cksum, outfd) == -1)
418 if (cksum_and_write(drr, sizeof (dmu_replay_record_t),
419 &stream_cksum, outfd) == -1)
425 (void) printf("INVALID record type 0x%x\n",
427 /* should never happen, so assert */
432 umem_cache_destroy(ddt.ddecache);
433 free(ddt.dedup_hash_array);
441 * Routines for dealing with the AVL tree of fs-nvlists
443 typedef struct fsavl_node {
451 fsavl_compare(const void *arg1, const void *arg2)
453 const fsavl_node_t *fn1 = arg1;
454 const fsavl_node_t *fn2 = arg2;
456 if (fn1->fn_guid > fn2->fn_guid)
458 else if (fn1->fn_guid < fn2->fn_guid)
465 * Given the GUID of a snapshot, find its containing filesystem and
469 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
471 fsavl_node_t fn_find;
474 fn_find.fn_guid = snapguid;
476 fn = avl_find(avl, &fn_find, NULL);
479 *snapname = fn->fn_snapname;
480 return (fn->fn_nvfs);
486 fsavl_destroy(avl_tree_t *avl)
495 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
502 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
505 fsavl_create(nvlist_t *fss)
508 nvpair_t *fselem = NULL;
510 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
513 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
514 offsetof(fsavl_node_t, fn_node));
516 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
517 nvlist_t *nvfs, *snaps;
518 nvpair_t *snapelem = NULL;
520 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
521 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
524 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
528 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
529 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
530 fsavl_destroy(fsavl);
534 fn->fn_snapname = nvpair_name(snapelem);
538 * Note: if there are multiple snaps with the
539 * same GUID, we ignore all but one.
541 if (avl_find(fsavl, fn, NULL) == NULL)
552 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
554 typedef struct send_data {
555 uint64_t parent_fromsnap_guid;
556 nvlist_t *parent_snaps;
559 const char *fromsnap;
564 * The header nvlist is of the following format:
567 * "fromsnap" -> string (if incremental)
571 * "name" -> string (full name; for debugging)
572 * "parentfromsnap" -> number (guid of fromsnap in parent)
574 * "props" -> { name -> value (only if set here) }
575 * "snaps" -> { name (lastname) -> number (guid) }
576 * "snapprops" -> { name (lastname) -> { name -> value } }
578 * "origin" -> number (guid) (if clone)
579 * "sent" -> boolean (not on-disk)
587 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
590 send_iterate_snap(zfs_handle_t *zhp, void *arg)
592 send_data_t *sd = arg;
593 uint64_t guid = zhp->zfs_dmustats.dds_guid;
597 snapname = strrchr(zhp->zfs_name, '@')+1;
599 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
601 * NB: if there is no fromsnap here (it's a newly created fs in
602 * an incremental replication), we will substitute the tosnap.
604 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
605 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
606 strcmp(snapname, sd->tosnap) == 0)) {
607 sd->parent_fromsnap_guid = guid;
610 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
611 send_iterate_prop(zhp, nv);
612 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
620 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
622 nvpair_t *elem = NULL;
624 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
625 char *propname = nvpair_name(elem);
626 zfs_prop_t prop = zfs_name_to_prop(propname);
629 if (!zfs_prop_user(propname)) {
631 * Realistically, this should never happen. However,
632 * we want the ability to add DSL properties without
633 * needing to make incompatible version changes. We
634 * need to ignore unknown properties to allow older
635 * software to still send datasets containing these
636 * properties, with the unknown properties elided.
638 if (prop == ZPROP_INVAL)
641 if (zfs_prop_readonly(prop))
645 verify(nvpair_value_nvlist(elem, &propnv) == 0);
646 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
647 prop == ZFS_PROP_REFQUOTA ||
648 prop == ZFS_PROP_REFRESERVATION) {
651 verify(nvlist_lookup_uint64(propnv,
652 ZPROP_VALUE, &value) == 0);
653 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
656 * May have no source before SPA_VERSION_RECVD_PROPS,
657 * but is still modifiable.
659 if (nvlist_lookup_string(propnv,
660 ZPROP_SOURCE, &source) == 0) {
661 if ((strcmp(source, zhp->zfs_name) != 0) &&
663 ZPROP_SOURCE_VAL_RECVD) != 0))
668 if (nvlist_lookup_string(propnv,
669 ZPROP_SOURCE, &source) != 0)
671 if ((strcmp(source, zhp->zfs_name) != 0) &&
672 (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
676 if (zfs_prop_user(propname) ||
677 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
679 verify(nvlist_lookup_string(propnv,
680 ZPROP_VALUE, &value) == 0);
681 VERIFY(0 == nvlist_add_string(nv, propname, value));
684 verify(nvlist_lookup_uint64(propnv,
685 ZPROP_VALUE, &value) == 0);
686 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
692 * recursively generate nvlists describing datasets. See comment
693 * for the data structure send_data_t above for description of contents
697 send_iterate_fs(zfs_handle_t *zhp, void *arg)
699 send_data_t *sd = arg;
702 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
703 uint64_t guid = zhp->zfs_dmustats.dds_guid;
706 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
707 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
708 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
709 sd->parent_fromsnap_guid));
711 if (zhp->zfs_dmustats.dds_origin[0]) {
712 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
713 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
716 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
717 origin->zfs_dmustats.dds_guid));
720 /* iterate over props */
721 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
722 send_iterate_prop(zhp, nv);
723 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
726 /* iterate over snaps, and set sd->parent_fromsnap_guid */
727 sd->parent_fromsnap_guid = 0;
728 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
729 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
730 (void) zfs_iter_snapshots(zhp, B_FALSE, send_iterate_snap, sd);
731 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
732 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
733 nvlist_free(sd->parent_snaps);
734 nvlist_free(sd->snapprops);
736 /* add this fs to nvlist */
737 (void) snprintf(guidstring, sizeof (guidstring),
738 "0x%llx", (longlong_t)guid);
739 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
742 /* iterate over children */
744 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
746 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
753 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
754 const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
757 send_data_t sd = { 0 };
760 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
762 return (EZFS_BADTYPE);
764 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
765 sd.fromsnap = fromsnap;
767 sd.recursive = recursive;
769 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
777 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
788 * Routines specific to "zfs send"
790 typedef struct send_dump_data {
791 /* these are all just the short snapname (the part after the @) */
792 const char *fromsnap;
794 char prevsnap[ZFS_MAXNAMELEN];
795 uint64_t prevsnap_obj;
796 boolean_t seenfrom, seento, replicate, doall, fromorigin;
797 boolean_t verbose, dryrun, parsable, progress;
802 snapfilter_cb_t *filter_cb;
805 char holdtag[ZFS_MAXNAMELEN];
811 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
812 boolean_t fromorigin, uint64_t *sizep)
814 zfs_cmd_t zc = { 0 };
815 libzfs_handle_t *hdl = zhp->zfs_hdl;
817 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
818 assert(fromsnap_obj == 0 || !fromorigin);
820 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
821 zc.zc_obj = fromorigin;
822 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
823 zc.zc_fromobj = fromsnap_obj;
824 zc.zc_guid = 1; /* estimate flag */
826 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
828 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
829 "warning: cannot estimate space for '%s'"), zhp->zfs_name);
833 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
834 "not an earlier snapshot from the same fs"));
835 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
838 if (zfs_dataset_exists(hdl, zc.zc_name,
839 ZFS_TYPE_SNAPSHOT)) {
840 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
841 "incremental source (@%s) does not exist"),
844 return (zfs_error(hdl, EZFS_NOENT, errbuf));
856 zfs_error_aux(hdl, strerror(errno));
857 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
860 return (zfs_standard_error(hdl, errno, errbuf));
864 *sizep = zc.zc_objset_type;
870 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
871 * NULL) to the file descriptor specified by outfd.
874 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
875 boolean_t fromorigin, int outfd, nvlist_t *debugnv)
877 zfs_cmd_t zc = { 0 };
878 libzfs_handle_t *hdl = zhp->zfs_hdl;
881 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
882 assert(fromsnap_obj == 0 || !fromorigin);
884 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
885 zc.zc_cookie = outfd;
886 zc.zc_obj = fromorigin;
887 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
888 zc.zc_fromobj = fromsnap_obj;
890 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
891 if (fromsnap && fromsnap[0] != '\0') {
892 VERIFY(0 == nvlist_add_string(thisdbg,
893 "fromsnap", fromsnap));
896 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
898 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
899 "warning: cannot send '%s'"), zhp->zfs_name);
901 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
903 VERIFY(0 == nvlist_add_nvlist(debugnv,
904 zhp->zfs_name, thisdbg));
906 nvlist_free(thisdbg);
910 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
911 "not an earlier snapshot from the same fs"));
912 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
915 if (zfs_dataset_exists(hdl, zc.zc_name,
916 ZFS_TYPE_SNAPSHOT)) {
917 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
918 "incremental source (@%s) does not exist"),
921 return (zfs_error(hdl, EZFS_NOENT, errbuf));
936 zfs_error_aux(hdl, strerror(errno));
937 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
940 return (zfs_standard_error(hdl, errno, errbuf));
945 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
946 nvlist_free(thisdbg);
952 hold_for_send(zfs_handle_t *zhp, send_dump_data_t *sdd)
958 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
964 * zfs_send() only opens a cleanup_fd for sends that need it,
965 * e.g. replication and doall.
967 if (sdd->cleanup_fd == -1)
970 thissnap = strchr(zhp->zfs_name, '@') + 1;
971 *(thissnap - 1) = '\0';
972 pzhp = zfs_open(zhp->zfs_hdl, zhp->zfs_name, ZFS_TYPE_DATASET);
973 *(thissnap - 1) = '@';
976 * It's OK if the parent no longer exists. The send code will
980 error = zfs_hold(pzhp, thissnap, sdd->holdtag,
981 B_FALSE, B_TRUE, B_TRUE, sdd->cleanup_fd,
982 zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID),
983 zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG));
991 send_progress_thread(void *arg)
993 progress_arg_t *pa = arg;
995 zfs_cmd_t zc = { 0 };
996 zfs_handle_t *zhp = pa->pa_zhp;
997 libzfs_handle_t *hdl = zhp->zfs_hdl;
998 unsigned long long bytes;
1004 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1005 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1007 if (!pa->pa_parsable)
1008 (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
1011 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1016 zc.zc_cookie = pa->pa_fd;
1017 if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1018 return ((void *)-1);
1022 bytes = zc.zc_cookie;
1024 if (pa->pa_parsable) {
1025 (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1026 tm->tm_hour, tm->tm_min, tm->tm_sec,
1027 bytes, zhp->zfs_name);
1029 zfs_nicenum(bytes, buf, sizeof (buf));
1030 (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
1031 tm->tm_hour, tm->tm_min, tm->tm_sec,
1032 buf, zhp->zfs_name);
1038 dump_snapshot(zfs_handle_t *zhp, void *arg)
1040 send_dump_data_t *sdd = arg;
1041 progress_arg_t pa = { 0 };
1046 boolean_t isfromsnap, istosnap, fromorigin;
1047 boolean_t exclude = B_FALSE;
1049 thissnap = strchr(zhp->zfs_name, '@') + 1;
1050 isfromsnap = (sdd->fromsnap != NULL &&
1051 strcmp(sdd->fromsnap, thissnap) == 0);
1053 if (!sdd->seenfrom && isfromsnap) {
1054 err = hold_for_send(zhp, sdd);
1056 sdd->seenfrom = B_TRUE;
1057 (void) strcpy(sdd->prevsnap, thissnap);
1058 sdd->prevsnap_obj = zfs_prop_get_int(zhp,
1060 } else if (err == ENOENT) {
1067 if (sdd->seento || !sdd->seenfrom) {
1072 istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1074 sdd->seento = B_TRUE;
1076 if (!sdd->doall && !isfromsnap && !istosnap) {
1077 if (sdd->replicate) {
1079 nvlist_t *snapprops;
1081 * Filter out all intermediate snapshots except origin
1082 * snapshots needed to replicate clones.
1084 nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1085 zhp->zfs_dmustats.dds_guid, &snapname);
1087 VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1088 "snapprops", &snapprops));
1089 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1090 thissnap, &snapprops));
1091 exclude = !nvlist_exists(snapprops, "is_clone_origin");
1098 * If a filter function exists, call it to determine whether
1099 * this snapshot will be sent.
1101 if (exclude || (sdd->filter_cb != NULL &&
1102 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1104 * This snapshot is filtered out. Don't send it, and don't
1105 * set prevsnap_obj, so it will be as if this snapshot didn't
1106 * exist, and the next accepted snapshot will be sent as
1107 * an incremental from the last accepted one, or as the
1108 * first (and full) snapshot in the case of a replication,
1109 * non-incremental send.
1115 err = hold_for_send(zhp, sdd);
1123 fromorigin = sdd->prevsnap[0] == '\0' &&
1124 (sdd->fromorigin || sdd->replicate);
1128 err = estimate_ioctl(zhp, sdd->prevsnap_obj,
1131 if (sdd->parsable) {
1132 if (sdd->prevsnap[0] != '\0') {
1133 (void) fprintf(stderr, "incremental\t%s\t%s",
1134 sdd->prevsnap, zhp->zfs_name);
1136 (void) fprintf(stderr, "full\t%s",
1140 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1141 "send from @%s to %s"),
1142 sdd->prevsnap, zhp->zfs_name);
1145 if (sdd->parsable) {
1146 (void) fprintf(stderr, "\t%llu\n",
1150 zfs_nicenum(size, buf, sizeof (buf));
1151 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1152 " estimated size is %s\n"), buf);
1156 (void) fprintf(stderr, "\n");
1162 * If progress reporting is requested, spawn a new thread to
1163 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1165 if (sdd->progress) {
1167 pa.pa_fd = sdd->outfd;
1168 pa.pa_parsable = sdd->parsable;
1170 if (err = pthread_create(&tid, NULL,
1171 send_progress_thread, &pa)) {
1177 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1178 fromorigin, sdd->outfd, sdd->debugnv);
1180 if (sdd->progress) {
1181 (void) pthread_cancel(tid);
1182 (void) pthread_join(tid, NULL);
1186 (void) strcpy(sdd->prevsnap, thissnap);
1187 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1193 dump_filesystem(zfs_handle_t *zhp, void *arg)
1196 send_dump_data_t *sdd = arg;
1197 boolean_t missingfrom = B_FALSE;
1198 zfs_cmd_t zc = { 0 };
1200 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1201 zhp->zfs_name, sdd->tosnap);
1202 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1203 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1204 "WARNING: could not send %s@%s: does not exist\n"),
1205 zhp->zfs_name, sdd->tosnap);
1210 if (sdd->replicate && sdd->fromsnap) {
1212 * If this fs does not have fromsnap, and we're doing
1213 * recursive, we need to send a full stream from the
1214 * beginning (or an incremental from the origin if this
1215 * is a clone). If we're doing non-recursive, then let
1216 * them get the error.
1218 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1219 zhp->zfs_name, sdd->fromsnap);
1220 if (ioctl(zhp->zfs_hdl->libzfs_fd,
1221 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1222 missingfrom = B_TRUE;
1226 sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1227 sdd->prevsnap_obj = 0;
1228 if (sdd->fromsnap == NULL || missingfrom)
1229 sdd->seenfrom = B_TRUE;
1231 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1232 if (!sdd->seenfrom) {
1233 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1234 "WARNING: could not send %s@%s:\n"
1235 "incremental source (%s@%s) does not exist\n"),
1236 zhp->zfs_name, sdd->tosnap,
1237 zhp->zfs_name, sdd->fromsnap);
1239 } else if (!sdd->seento) {
1240 if (sdd->fromsnap) {
1241 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1242 "WARNING: could not send %s@%s:\n"
1243 "incremental source (%s@%s) "
1244 "is not earlier than it\n"),
1245 zhp->zfs_name, sdd->tosnap,
1246 zhp->zfs_name, sdd->fromsnap);
1248 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1250 "could not send %s@%s: does not exist\n"),
1251 zhp->zfs_name, sdd->tosnap);
1260 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1262 send_dump_data_t *sdd = arg;
1264 boolean_t needagain, progress;
1266 if (!sdd->replicate)
1267 return (dump_filesystem(rzhp, sdd));
1269 /* Mark the clone origin snapshots. */
1270 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1271 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1273 uint64_t origin_guid = 0;
1275 VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1276 (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1277 if (origin_guid != 0) {
1279 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1280 origin_guid, &snapname);
1281 if (origin_nv != NULL) {
1282 nvlist_t *snapprops;
1283 VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1284 "snapprops", &snapprops));
1285 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1286 snapname, &snapprops));
1287 VERIFY(0 == nvlist_add_boolean(
1288 snapprops, "is_clone_origin"));
1293 needagain = progress = B_FALSE;
1294 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1295 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1296 nvlist_t *fslist, *parent_nv;
1300 uint64_t origin_guid = 0;
1301 uint64_t parent_guid = 0;
1303 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1304 if (nvlist_lookup_boolean(fslist, "sent") == 0)
1307 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1308 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1309 (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1312 if (parent_guid != 0) {
1313 parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1314 if (!nvlist_exists(parent_nv, "sent")) {
1315 /* parent has not been sent; skip this one */
1321 if (origin_guid != 0) {
1322 nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1324 if (origin_nv != NULL &&
1325 !nvlist_exists(origin_nv, "sent")) {
1327 * origin has not been sent yet;
1335 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1338 err = dump_filesystem(zhp, sdd);
1339 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1350 /* clean out the sent flags in case we reuse this fss */
1351 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1352 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1355 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1356 (void) nvlist_remove_all(fslist, "sent");
1363 * Generate a send stream for the dataset identified by the argument zhp.
1365 * The content of the send stream is the snapshot identified by
1366 * 'tosnap'. Incremental streams are requested in two ways:
1367 * - from the snapshot identified by "fromsnap" (if non-null) or
1368 * - from the origin of the dataset identified by zhp, which must
1369 * be a clone. In this case, "fromsnap" is null and "fromorigin"
1372 * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1373 * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1374 * if "replicate" is set. If "doall" is set, dump all the intermediate
1375 * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1376 * case too. If "props" is set, send properties.
1379 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1380 sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1381 void *cb_arg, nvlist_t **debugnvp)
1384 send_dump_data_t sdd = { 0 };
1386 nvlist_t *fss = NULL;
1387 avl_tree_t *fsavl = NULL;
1388 static uint64_t holdseq;
1392 dedup_arg_t dda = { 0 };
1393 int featureflags = 0;
1395 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1396 "cannot send '%s'"), zhp->zfs_name);
1398 if (fromsnap && fromsnap[0] == '\0') {
1399 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1400 "zero-length incremental source"));
1401 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1404 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1406 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1407 if (version >= ZPL_VERSION_SA) {
1408 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1412 if (flags->dedup && !flags->dryrun) {
1413 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1414 DMU_BACKUP_FEATURE_DEDUPPROPS);
1415 if (err = pipe(pipefd)) {
1416 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1417 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1420 dda.outputfd = outfd;
1421 dda.inputfd = pipefd[1];
1422 dda.dedup_hdl = zhp->zfs_hdl;
1423 if (err = pthread_create(&tid, NULL, cksummer, &dda)) {
1424 (void) close(pipefd[0]);
1425 (void) close(pipefd[1]);
1426 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1427 return (zfs_error(zhp->zfs_hdl,
1428 EZFS_THREADCREATEFAILED, errbuf));
1432 if (flags->replicate || flags->doall || flags->props) {
1433 dmu_replay_record_t drr = { 0 };
1434 char *packbuf = NULL;
1436 zio_cksum_t zc = { 0 };
1438 if (flags->replicate || flags->props) {
1441 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1443 VERIFY(0 == nvlist_add_string(hdrnv,
1444 "fromsnap", fromsnap));
1446 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1447 if (!flags->replicate) {
1448 VERIFY(0 == nvlist_add_boolean(hdrnv,
1452 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1453 fromsnap, tosnap, flags->replicate, &fss, &fsavl);
1456 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1457 err = nvlist_pack(hdrnv, &packbuf, &buflen,
1464 fsavl_destroy(fsavl);
1470 if (!flags->dryrun) {
1471 /* write first begin record */
1472 drr.drr_type = DRR_BEGIN;
1473 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1474 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1475 drr_versioninfo, DMU_COMPOUNDSTREAM);
1476 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1477 drr_versioninfo, featureflags);
1478 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1479 sizeof (drr.drr_u.drr_begin.drr_toname),
1480 "%s@%s", zhp->zfs_name, tosnap);
1481 drr.drr_payloadlen = buflen;
1482 err = cksum_and_write(&drr, sizeof (drr), &zc, outfd);
1484 /* write header nvlist */
1485 if (err != -1 && packbuf != NULL) {
1486 err = cksum_and_write(packbuf, buflen, &zc,
1491 fsavl_destroy(fsavl);
1497 /* write end record */
1498 bzero(&drr, sizeof (drr));
1499 drr.drr_type = DRR_END;
1500 drr.drr_u.drr_end.drr_checksum = zc;
1501 err = write(outfd, &drr, sizeof (drr));
1503 fsavl_destroy(fsavl);
1513 /* dump each stream */
1514 sdd.fromsnap = fromsnap;
1515 sdd.tosnap = tosnap;
1517 sdd.outfd = pipefd[0];
1520 sdd.replicate = flags->replicate;
1521 sdd.doall = flags->doall;
1522 sdd.fromorigin = flags->fromorigin;
1525 sdd.verbose = flags->verbose;
1526 sdd.parsable = flags->parsable;
1527 sdd.progress = flags->progress;
1528 sdd.dryrun = flags->dryrun;
1529 sdd.filter_cb = filter_func;
1530 sdd.filter_cb_arg = cb_arg;
1532 sdd.debugnv = *debugnvp;
1535 * Some flags require that we place user holds on the datasets that are
1536 * being sent so they don't get destroyed during the send. We can skip
1537 * this step if the pool is imported read-only since the datasets cannot
1540 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1541 ZPOOL_PROP_READONLY, NULL) &&
1542 zfs_spa_version(zhp, &spa_version) == 0 &&
1543 spa_version >= SPA_VERSION_USERREFS &&
1544 (flags->doall || flags->replicate)) {
1546 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1547 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1548 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1549 if (sdd.cleanup_fd < 0) {
1554 sdd.cleanup_fd = -1;
1556 if (flags->verbose) {
1558 * Do a verbose no-op dry run to get all the verbose output
1559 * before generating any data. Then do a non-verbose real
1560 * run to generate the streams.
1562 sdd.dryrun = B_TRUE;
1563 err = dump_filesystems(zhp, &sdd);
1564 sdd.dryrun = flags->dryrun;
1565 sdd.verbose = B_FALSE;
1566 if (flags->parsable) {
1567 (void) fprintf(stderr, "size\t%llu\n",
1568 (longlong_t)sdd.size);
1571 zfs_nicenum(sdd.size, buf, sizeof (buf));
1572 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1573 "total estimated size is %s\n"), buf);
1576 err = dump_filesystems(zhp, &sdd);
1577 fsavl_destroy(fsavl);
1581 (void) close(pipefd[0]);
1582 (void) pthread_join(tid, NULL);
1585 if (sdd.cleanup_fd != -1) {
1586 VERIFY(0 == close(sdd.cleanup_fd));
1587 sdd.cleanup_fd = -1;
1590 if (!flags->dryrun && (flags->replicate || flags->doall ||
1593 * write final end record. NB: want to do this even if
1594 * there was some error, because it might not be totally
1597 dmu_replay_record_t drr = { 0 };
1598 drr.drr_type = DRR_END;
1599 if (write(outfd, &drr, sizeof (drr)) == -1) {
1600 return (zfs_standard_error(zhp->zfs_hdl,
1605 return (err || sdd.err);
1608 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1610 if (sdd.cleanup_fd != -1)
1611 VERIFY(0 == close(sdd.cleanup_fd));
1613 (void) pthread_cancel(tid);
1614 (void) pthread_join(tid, NULL);
1615 (void) close(pipefd[0]);
1621 * Routines specific to "zfs recv"
1625 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1626 boolean_t byteswap, zio_cksum_t *zc)
1633 rv = read(fd, cp, len);
1638 if (rv < 0 || len != 0) {
1639 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1640 "failed to read from stream"));
1641 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1642 "cannot receive")));
1647 fletcher_4_incremental_byteswap(buf, ilen, zc);
1649 fletcher_4_incremental_native(buf, ilen, zc);
1655 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1656 boolean_t byteswap, zio_cksum_t *zc)
1661 buf = zfs_alloc(hdl, len);
1665 err = recv_read(hdl, fd, buf, len, byteswap, zc);
1671 err = nvlist_unpack(buf, len, nvp, 0);
1674 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1675 "stream (malformed nvlist)"));
1682 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
1683 int baselen, char *newname, recvflags_t *flags)
1686 zfs_cmd_t zc = { 0 };
1688 prop_changelist_t *clp;
1691 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1694 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1695 flags->force ? MS_FORCE : 0);
1699 err = changelist_prefix(clp);
1703 zc.zc_objset_type = DMU_OST_ZFS;
1704 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1707 (void) strcpy(newname, tryname);
1709 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
1711 if (flags->verbose) {
1712 (void) printf("attempting rename %s to %s\n",
1713 zc.zc_name, zc.zc_value);
1715 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1717 changelist_rename(clp, name, tryname);
1722 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
1725 (void) strncpy(newname, name, baselen);
1726 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
1727 "recv-%u-%u", getpid(), seq);
1728 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
1730 if (flags->verbose) {
1731 (void) printf("failed - trying rename %s to %s\n",
1732 zc.zc_name, zc.zc_value);
1734 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
1736 changelist_rename(clp, name, newname);
1737 if (err && flags->verbose) {
1738 (void) printf("failed (%u) - "
1739 "will try again on next pass\n", errno);
1742 } else if (flags->verbose) {
1744 (void) printf("success\n");
1746 (void) printf("failed (%u)\n", errno);
1749 (void) changelist_postfix(clp);
1750 changelist_free(clp);
1756 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1757 char *newname, recvflags_t *flags)
1759 zfs_cmd_t zc = { 0 };
1761 prop_changelist_t *clp;
1763 boolean_t defer = B_FALSE;
1766 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1769 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1770 flags->force ? MS_FORCE : 0);
1771 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1772 zfs_spa_version(zhp, &spa_version) == 0 &&
1773 spa_version >= SPA_VERSION_USERREFS)
1778 err = changelist_prefix(clp);
1782 zc.zc_objset_type = DMU_OST_ZFS;
1783 zc.zc_defer_destroy = defer;
1784 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1787 (void) printf("attempting destroy %s\n", zc.zc_name);
1788 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1791 (void) printf("success\n");
1792 changelist_remove(clp, zc.zc_name);
1795 (void) changelist_postfix(clp);
1796 changelist_free(clp);
1799 * Deferred destroy might destroy the snapshot or only mark it to be
1800 * destroyed later, and it returns success in either case.
1802 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
1803 ZFS_TYPE_SNAPSHOT))) {
1804 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1810 typedef struct guid_to_name_data {
1814 } guid_to_name_data_t;
1817 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1819 guid_to_name_data_t *gtnd = arg;
1822 if (gtnd->skip != NULL &&
1823 strcmp(zhp->zfs_name, gtnd->skip) == 0) {
1827 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1828 (void) strcpy(gtnd->name, zhp->zfs_name);
1833 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1839 * Attempt to find the local dataset associated with this guid. In the case of
1840 * multiple matches, we attempt to find the "best" match by searching
1841 * progressively larger portions of the hierarchy. This allows one to send a
1842 * tree of datasets individually and guarantee that we will find the source
1843 * guid within that hierarchy, even if there are multiple matches elsewhere.
1846 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1849 /* exhaustive search all local snapshots */
1850 char pname[ZFS_MAXNAMELEN];
1851 guid_to_name_data_t gtnd;
1860 (void) strlcpy(pname, parent, sizeof (pname));
1863 * Search progressively larger portions of the hierarchy. This will
1864 * select the "most local" version of the origin snapshot in the case
1865 * that there are multiple matching snapshots in the system.
1867 while ((cp = strrchr(pname, '/')) != NULL) {
1869 /* Chop off the last component and open the parent */
1871 zhp = make_dataset_handle(hdl, pname);
1876 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1882 * Remember the dataset that we already searched, so we
1883 * skip it next time through.
1892 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
1893 * guid1 is after guid2.
1896 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1897 uint64_t guid1, uint64_t guid2)
1900 char *fsname, *snapname;
1901 char buf[ZFS_MAXNAMELEN];
1903 zfs_handle_t *guid1hdl, *guid2hdl;
1904 uint64_t create1, create2;
1911 nvfs = fsavl_find(avl, guid1, &snapname);
1912 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1913 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1914 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1915 if (guid1hdl == NULL)
1918 nvfs = fsavl_find(avl, guid2, &snapname);
1919 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1920 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1921 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1922 if (guid2hdl == NULL) {
1923 zfs_close(guid1hdl);
1927 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
1928 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
1930 if (create1 < create2)
1932 else if (create1 > create2)
1937 zfs_close(guid1hdl);
1938 zfs_close(guid2hdl);
1944 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1945 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
1949 avl_tree_t *local_avl;
1950 nvpair_t *fselem, *nextfselem;
1952 char newname[ZFS_MAXNAMELEN];
1954 boolean_t needagain, progress, recursive;
1957 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1959 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
1966 needagain = progress = B_FALSE;
1968 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1969 recursive, &local_nv, &local_avl)) != 0)
1973 * Process deletes and renames
1975 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1976 fselem; fselem = nextfselem) {
1977 nvlist_t *nvfs, *snaps;
1978 nvlist_t *stream_nvfs = NULL;
1979 nvpair_t *snapelem, *nextsnapelem;
1980 uint64_t fromguid = 0;
1981 uint64_t originguid = 0;
1982 uint64_t stream_originguid = 0;
1983 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1984 char *fsname, *stream_fsname;
1986 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1988 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1989 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1990 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1991 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1992 &parent_fromsnap_guid));
1993 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1996 * First find the stream's fs, so we can check for
1997 * a different origin (due to "zfs promote")
1999 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2000 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2003 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2004 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2006 if (stream_nvfs != NULL)
2010 /* check for promote */
2011 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2012 &stream_originguid);
2013 if (stream_nvfs && originguid != stream_originguid) {
2014 switch (created_before(hdl, local_avl,
2015 stream_originguid, originguid)) {
2018 zfs_cmd_t zc = { 0 };
2019 nvlist_t *origin_nvfs;
2020 char *origin_fsname;
2023 (void) printf("promoting %s\n", fsname);
2025 origin_nvfs = fsavl_find(local_avl, originguid,
2027 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2028 "name", &origin_fsname));
2029 (void) strlcpy(zc.zc_value, origin_fsname,
2030 sizeof (zc.zc_value));
2031 (void) strlcpy(zc.zc_name, fsname,
2032 sizeof (zc.zc_name));
2033 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2041 fsavl_destroy(local_avl);
2042 nvlist_free(local_nv);
2046 * We had/have the wrong origin, therefore our
2047 * list of snapshots is wrong. Need to handle
2048 * them on the next pass.
2054 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2055 snapelem; snapelem = nextsnapelem) {
2057 char *stream_snapname;
2058 nvlist_t *found, *props;
2060 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2062 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2063 found = fsavl_find(stream_avl, thisguid,
2066 /* check for delete */
2067 if (found == NULL) {
2068 char name[ZFS_MAXNAMELEN];
2073 (void) snprintf(name, sizeof (name), "%s@%s",
2074 fsname, nvpair_name(snapelem));
2076 error = recv_destroy(hdl, name,
2077 strlen(fsname)+1, newname, flags);
2085 stream_nvfs = found;
2087 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2088 &props) && 0 == nvlist_lookup_nvlist(props,
2089 stream_snapname, &props)) {
2090 zfs_cmd_t zc = { 0 };
2092 zc.zc_cookie = B_TRUE; /* received */
2093 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2094 "%s@%s", fsname, nvpair_name(snapelem));
2095 if (zcmd_write_src_nvlist(hdl, &zc,
2097 (void) zfs_ioctl(hdl,
2098 ZFS_IOC_SET_PROP, &zc);
2099 zcmd_free_nvlists(&zc);
2103 /* check for different snapname */
2104 if (strcmp(nvpair_name(snapelem),
2105 stream_snapname) != 0) {
2106 char name[ZFS_MAXNAMELEN];
2107 char tryname[ZFS_MAXNAMELEN];
2109 (void) snprintf(name, sizeof (name), "%s@%s",
2110 fsname, nvpair_name(snapelem));
2111 (void) snprintf(tryname, sizeof (name), "%s@%s",
2112 fsname, stream_snapname);
2114 error = recv_rename(hdl, name, tryname,
2115 strlen(fsname)+1, newname, flags);
2122 if (strcmp(stream_snapname, fromsnap) == 0)
2123 fromguid = thisguid;
2126 /* check for delete */
2127 if (stream_nvfs == NULL) {
2131 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2140 if (fromguid == 0) {
2141 if (flags->verbose) {
2142 (void) printf("local fs %s does not have "
2143 "fromsnap (%s in stream); must have "
2144 "been deleted locally; ignoring\n",
2150 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2151 "name", &stream_fsname));
2152 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2153 "parentfromsnap", &stream_parent_fromsnap_guid));
2155 s1 = strrchr(fsname, '/');
2156 s2 = strrchr(stream_fsname, '/');
2159 * Check for rename. If the exact receive path is specified, it
2160 * does not count as a rename, but we still need to check the
2161 * datasets beneath it.
2163 if ((stream_parent_fromsnap_guid != 0 &&
2164 parent_fromsnap_guid != 0 &&
2165 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2166 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2167 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2169 char tryname[ZFS_MAXNAMELEN];
2171 parent = fsavl_find(local_avl,
2172 stream_parent_fromsnap_guid, NULL);
2174 * NB: parent might not be found if we used the
2175 * tosnap for stream_parent_fromsnap_guid,
2176 * because the parent is a newly-created fs;
2177 * we'll be able to rename it after we recv the
2180 if (parent != NULL) {
2183 VERIFY(0 == nvlist_lookup_string(parent, "name",
2185 (void) snprintf(tryname, sizeof (tryname),
2186 "%s%s", pname, strrchr(stream_fsname, '/'));
2189 if (flags->verbose) {
2190 (void) printf("local fs %s new parent "
2191 "not found\n", fsname);
2197 error = recv_rename(hdl, fsname, tryname,
2198 strlen(tofs)+1, newname, flags);
2200 if (renamed != NULL && newname[0] != '\0') {
2201 VERIFY(0 == nvlist_add_boolean(renamed,
2212 fsavl_destroy(local_avl);
2213 nvlist_free(local_nv);
2215 if (needagain && progress) {
2216 /* do another pass to fix up temporary names */
2218 (void) printf("another pass:\n");
2226 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2227 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2228 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2230 nvlist_t *stream_nv = NULL;
2231 avl_tree_t *stream_avl = NULL;
2232 char *fromsnap = NULL;
2234 char tofs[ZFS_MAXNAMELEN];
2235 char sendfs[ZFS_MAXNAMELEN];
2237 dmu_replay_record_t drre;
2239 boolean_t anyerr = B_FALSE;
2240 boolean_t softerr = B_FALSE;
2241 boolean_t recursive;
2243 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2246 assert(drr->drr_type == DRR_BEGIN);
2247 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2248 assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2249 DMU_COMPOUNDSTREAM);
2252 * Read in the nvlist from the stream.
2254 if (drr->drr_payloadlen != 0) {
2255 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2256 &stream_nv, flags->byteswap, zc);
2258 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2263 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2266 if (recursive && strchr(destname, '@')) {
2267 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2268 "cannot specify snapshot name for multi-snapshot stream"));
2269 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2274 * Read in the end record and verify checksum.
2276 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2277 flags->byteswap, NULL)))
2279 if (flags->byteswap) {
2280 drre.drr_type = BSWAP_32(drre.drr_type);
2281 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2282 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2283 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2284 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2285 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2286 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2287 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2288 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2290 if (drre.drr_type != DRR_END) {
2291 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2294 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2295 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2296 "incorrect header checksum"));
2297 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2301 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2303 if (drr->drr_payloadlen != 0) {
2304 nvlist_t *stream_fss;
2306 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2308 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2309 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2310 "couldn't allocate avl tree"));
2311 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2315 if (fromsnap != NULL) {
2316 nvlist_t *renamed = NULL;
2317 nvpair_t *pair = NULL;
2319 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2320 if (flags->isprefix) {
2321 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2324 if (flags->istail) {
2325 cp = strrchr(drrb->drr_toname, '/');
2327 (void) strlcat(tofs, "/",
2331 i = (cp - drrb->drr_toname);
2334 i = strcspn(drrb->drr_toname, "/@");
2336 /* zfs_receive_one() will create_parents() */
2337 (void) strlcat(tofs, &drrb->drr_toname[i],
2339 *strchr(tofs, '@') = '\0';
2342 if (recursive && !flags->dryrun && !flags->nomount) {
2343 VERIFY(0 == nvlist_alloc(&renamed,
2344 NV_UNIQUE_NAME, 0));
2347 softerr = recv_incremental_replication(hdl, tofs, flags,
2348 stream_nv, stream_avl, renamed);
2350 /* Unmount renamed filesystems before receiving. */
2351 while ((pair = nvlist_next_nvpair(renamed,
2354 prop_changelist_t *clp = NULL;
2356 zhp = zfs_open(hdl, nvpair_name(pair),
2357 ZFS_TYPE_FILESYSTEM);
2359 clp = changelist_gather(zhp,
2360 ZFS_PROP_MOUNTPOINT, 0, 0);
2364 changelist_prefix(clp);
2365 changelist_free(clp);
2370 nvlist_free(renamed);
2375 * Get the fs specified by the first path in the stream (the top level
2376 * specified by 'zfs send') and pass it to each invocation of
2377 * zfs_receive_one().
2379 (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2381 if ((cp = strchr(sendfs, '@')) != NULL)
2384 /* Finally, receive each contained stream */
2387 * we should figure out if it has a recoverable
2388 * error, in which case do a recv_skip() and drive on.
2389 * Note, if we fail due to already having this guid,
2390 * zfs_receive_one() will take care of it (ie,
2391 * recv_skip() and return 0).
2393 error = zfs_receive_impl(hdl, destname, flags, fd,
2394 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2396 if (error == ENODATA) {
2401 } while (error == 0);
2403 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2405 * Now that we have the fs's they sent us, try the
2408 softerr = recv_incremental_replication(hdl, tofs, flags,
2409 stream_nv, stream_avl, NULL);
2413 fsavl_destroy(stream_avl);
2415 nvlist_free(stream_nv);
2424 trunc_prop_errs(int truncated)
2426 ASSERT(truncated != 0);
2429 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2430 "1 more property could not be set\n"));
2432 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2433 "%d more properties could not be set\n"), truncated);
2437 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2439 dmu_replay_record_t *drr;
2440 void *buf = malloc(1<<20);
2443 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2444 "cannot receive:"));
2446 /* XXX would be great to use lseek if possible... */
2449 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2450 byteswap, NULL) == 0) {
2452 drr->drr_type = BSWAP_32(drr->drr_type);
2454 switch (drr->drr_type) {
2456 /* NB: not to be used on v2 stream packages */
2457 if (drr->drr_payloadlen != 0) {
2458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2459 "invalid substream header"));
2460 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2470 drr->drr_u.drr_object.drr_bonuslen =
2471 BSWAP_32(drr->drr_u.drr_object.
2474 (void) recv_read(hdl, fd, buf,
2475 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2481 drr->drr_u.drr_write.drr_length =
2482 BSWAP_64(drr->drr_u.drr_write.drr_length);
2484 (void) recv_read(hdl, fd, buf,
2485 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2489 drr->drr_u.drr_write.drr_length =
2490 BSWAP_64(drr->drr_u.drr_spill.drr_length);
2492 (void) recv_read(hdl, fd, buf,
2493 drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2495 case DRR_WRITE_BYREF:
2496 case DRR_FREEOBJECTS:
2501 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2502 "invalid record type"));
2503 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2512 * Restores a backup of tosnap from the file descriptor specified by infd.
2515 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2516 recvflags_t *flags, dmu_replay_record_t *drr,
2517 dmu_replay_record_t *drr_noswap, const char *sendfs,
2518 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2519 uint64_t *action_handlep)
2521 zfs_cmd_t zc = { 0 };
2523 int ioctl_err, ioctl_errno, err;
2525 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2527 char prop_errbuf[1024];
2528 const char *chopprefix;
2529 boolean_t newfs = B_FALSE;
2530 boolean_t stream_wantsnewfs;
2531 uint64_t parent_snapguid = 0;
2532 prop_changelist_t *clp = NULL;
2533 nvlist_t *snapprops_nvlist = NULL;
2534 zprop_errflags_t prop_errflags;
2535 boolean_t recursive;
2537 begin_time = time(NULL);
2539 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2542 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2545 if (stream_avl != NULL) {
2547 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2552 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
2554 err = nvlist_lookup_nvlist(fs, "props", &props);
2556 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2558 if (flags->canmountoff) {
2559 VERIFY(0 == nvlist_add_uint64(props,
2560 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2562 ret = zcmd_write_src_nvlist(hdl, &zc, props);
2566 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2567 VERIFY(0 == nvlist_lookup_nvlist(props,
2568 snapname, &snapprops_nvlist));
2578 * Determine how much of the snapshot name stored in the stream
2579 * we are going to tack on to the name they specified on the
2580 * command line, and how much we are going to chop off.
2582 * If they specified a snapshot, chop the entire name stored in
2585 if (flags->istail) {
2587 * A filesystem was specified with -e. We want to tack on only
2588 * the tail of the sent snapshot path.
2590 if (strchr(tosnap, '@')) {
2591 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2592 "argument - snapshot not allowed with -e"));
2593 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2596 chopprefix = strrchr(sendfs, '/');
2598 if (chopprefix == NULL) {
2600 * The tail is the poolname, so we need to
2601 * prepend a path separator.
2603 int len = strlen(drrb->drr_toname);
2604 cp = malloc(len + 2);
2606 (void) strcpy(&cp[1], drrb->drr_toname);
2609 chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2611 } else if (flags->isprefix) {
2613 * A filesystem was specified with -d. We want to tack on
2614 * everything but the first element of the sent snapshot path
2615 * (all but the pool name).
2617 if (strchr(tosnap, '@')) {
2618 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2619 "argument - snapshot not allowed with -d"));
2620 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2623 chopprefix = strchr(drrb->drr_toname, '/');
2624 if (chopprefix == NULL)
2625 chopprefix = strchr(drrb->drr_toname, '@');
2626 } else if (strchr(tosnap, '@') == NULL) {
2628 * If a filesystem was specified without -d or -e, we want to
2629 * tack on everything after the fs specified by 'zfs send'.
2631 chopprefix = drrb->drr_toname + strlen(sendfs);
2633 /* A snapshot was specified as an exact path (no -d or -e). */
2635 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2636 "cannot specify snapshot name for multi-snapshot "
2638 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2640 chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
2643 ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
2644 ASSERT(chopprefix > drrb->drr_toname);
2645 ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
2646 ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
2647 chopprefix[0] == '\0');
2650 * Determine name of destination snapshot, store in zc_value.
2652 (void) strcpy(zc.zc_top_ds, tosnap);
2653 (void) strcpy(zc.zc_value, tosnap);
2654 (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
2656 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
2657 zcmd_free_nvlists(&zc);
2658 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2662 * Determine the name of the origin snapshot, store in zc_string.
2664 if (drrb->drr_flags & DRR_FLAG_CLONE) {
2665 if (guid_to_name(hdl, zc.zc_value,
2666 drrb->drr_fromguid, zc.zc_string) != 0) {
2667 zcmd_free_nvlists(&zc);
2668 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2669 "local origin for clone %s does not exist"),
2671 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2674 (void) printf("found clone origin %s\n", zc.zc_string);
2677 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
2678 (drrb->drr_flags & DRR_FLAG_CLONE));
2680 if (stream_wantsnewfs) {
2682 * if the parent fs does not exist, look for it based on
2683 * the parent snap GUID
2685 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2686 "cannot receive new filesystem stream"));
2688 (void) strcpy(zc.zc_name, zc.zc_value);
2689 cp = strrchr(zc.zc_name, '/');
2693 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2694 char suffix[ZFS_MAXNAMELEN];
2695 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
2696 if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
2697 zc.zc_value) == 0) {
2698 *strchr(zc.zc_value, '@') = '\0';
2699 (void) strcat(zc.zc_value, suffix);
2704 * if the fs does not exist, look for it based on the
2707 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2708 "cannot receive incremental stream"));
2710 (void) strcpy(zc.zc_name, zc.zc_value);
2711 *strchr(zc.zc_name, '@') = '\0';
2714 * If the exact receive path was specified and this is the
2715 * topmost path in the stream, then if the fs does not exist we
2716 * should look no further.
2718 if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
2719 strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
2720 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2721 char snap[ZFS_MAXNAMELEN];
2722 (void) strcpy(snap, strchr(zc.zc_value, '@'));
2723 if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
2724 zc.zc_value) == 0) {
2725 *strchr(zc.zc_value, '@') = '\0';
2726 (void) strcat(zc.zc_value, snap);
2731 (void) strcpy(zc.zc_name, zc.zc_value);
2732 *strchr(zc.zc_name, '@') = '\0';
2734 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
2738 * Destination fs exists. Therefore this should either
2739 * be an incremental, or the stream specifies a new fs
2740 * (full stream or clone) and they want us to blow it
2741 * away (and have therefore specified -F and removed any
2744 if (stream_wantsnewfs) {
2745 if (!flags->force) {
2746 zcmd_free_nvlists(&zc);
2747 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2748 "destination '%s' exists\n"
2749 "must specify -F to overwrite it"),
2751 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2753 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
2755 zcmd_free_nvlists(&zc);
2756 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2757 "destination has snapshots (eg. %s)\n"
2758 "must destroy them to overwrite it"),
2760 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2764 if ((zhp = zfs_open(hdl, zc.zc_name,
2765 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
2766 zcmd_free_nvlists(&zc);
2770 if (stream_wantsnewfs &&
2771 zhp->zfs_dmustats.dds_origin[0]) {
2772 zcmd_free_nvlists(&zc);
2774 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2775 "destination '%s' is a clone\n"
2776 "must destroy it to overwrite it"),
2778 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
2781 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
2782 stream_wantsnewfs) {
2783 /* We can't do online recv in this case */
2784 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
2787 zcmd_free_nvlists(&zc);
2790 if (changelist_prefix(clp) != 0) {
2791 changelist_free(clp);
2793 zcmd_free_nvlists(&zc);
2800 * Destination filesystem does not exist. Therefore we better
2801 * be creating a new filesystem (either from a full backup, or
2802 * a clone). It would therefore be invalid if the user
2803 * specified only the pool name (i.e. if the destination name
2804 * contained no slash character).
2806 if (!stream_wantsnewfs ||
2807 (cp = strrchr(zc.zc_name, '/')) == NULL) {
2808 zcmd_free_nvlists(&zc);
2809 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2810 "destination '%s' does not exist"), zc.zc_name);
2811 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2815 * Trim off the final dataset component so we perform the
2816 * recvbackup ioctl to the filesystems's parent.
2820 if (flags->isprefix && !flags->istail && !flags->dryrun &&
2821 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
2822 zcmd_free_nvlists(&zc);
2823 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
2829 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
2830 zc.zc_cookie = infd;
2831 zc.zc_guid = flags->force;
2832 if (flags->verbose) {
2833 (void) printf("%s %s stream of %s into %s\n",
2834 flags->dryrun ? "would receive" : "receiving",
2835 drrb->drr_fromguid ? "incremental" : "full",
2836 drrb->drr_toname, zc.zc_value);
2837 (void) fflush(stdout);
2840 if (flags->dryrun) {
2841 zcmd_free_nvlists(&zc);
2842 return (recv_skip(hdl, infd, flags->byteswap));
2845 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
2846 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
2847 zc.zc_cleanup_fd = cleanup_fd;
2848 zc.zc_action_handle = *action_handlep;
2850 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
2851 ioctl_errno = errno;
2852 prop_errflags = (zprop_errflags_t)zc.zc_obj;
2855 nvlist_t *prop_errors;
2856 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
2857 zc.zc_nvlist_dst_size, &prop_errors, 0));
2859 nvpair_t *prop_err = NULL;
2861 while ((prop_err = nvlist_next_nvpair(prop_errors,
2862 prop_err)) != NULL) {
2867 prop = zfs_name_to_prop(nvpair_name(prop_err));
2868 (void) nvpair_value_int32(prop_err, &intval);
2869 if (strcmp(nvpair_name(prop_err),
2870 ZPROP_N_MORE_ERRORS) == 0) {
2871 trunc_prop_errs(intval);
2874 (void) snprintf(tbuf, sizeof (tbuf),
2875 dgettext(TEXT_DOMAIN,
2876 "cannot receive %s property on %s"),
2877 nvpair_name(prop_err), zc.zc_name);
2878 zfs_setprop_error(hdl, prop, intval, tbuf);
2881 nvlist_free(prop_errors);
2884 zc.zc_nvlist_dst = 0;
2885 zc.zc_nvlist_dst_size = 0;
2886 zcmd_free_nvlists(&zc);
2888 if (err == 0 && snapprops_nvlist) {
2889 zfs_cmd_t zc2 = { 0 };
2891 (void) strcpy(zc2.zc_name, zc.zc_value);
2892 zc2.zc_cookie = B_TRUE; /* received */
2893 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
2894 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
2895 zcmd_free_nvlists(&zc2);
2899 if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
2901 * It may be that this snapshot already exists,
2902 * in which case we want to consume & ignore it
2903 * rather than failing.
2905 avl_tree_t *local_avl;
2906 nvlist_t *local_nv, *fs;
2907 cp = strchr(zc.zc_value, '@');
2910 * XXX Do this faster by just iterating over snaps in
2911 * this fs. Also if zc_value does not exist, we will
2912 * get a strange "does not exist" error message.
2915 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
2916 &local_nv, &local_avl) == 0) {
2918 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
2919 fsavl_destroy(local_avl);
2920 nvlist_free(local_nv);
2923 if (flags->verbose) {
2924 (void) printf("snap %s already exists; "
2925 "ignoring\n", zc.zc_value);
2927 err = ioctl_err = recv_skip(hdl, infd,
2934 if (ioctl_err != 0) {
2935 switch (ioctl_errno) {
2937 cp = strchr(zc.zc_value, '@');
2939 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2940 "most recent snapshot of %s does not\n"
2941 "match incremental source"), zc.zc_value);
2942 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2946 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2947 "destination %s has been modified\n"
2948 "since most recent snapshot"), zc.zc_name);
2949 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
2952 cp = strchr(zc.zc_value, '@');
2954 /* it's the containing fs that exists */
2957 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2958 "destination already exists"));
2959 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
2960 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
2965 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2968 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2969 "invalid stream (checksum mismatch)"));
2970 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2973 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2974 "pool must be upgraded to receive this stream."));
2975 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
2978 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2979 "destination %s space quota exceeded"), zc.zc_name);
2980 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
2983 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
2988 * Mount the target filesystem (if created). Also mount any
2989 * children of the target filesystem if we did a replication
2990 * receive (indicated by stream_avl being non-NULL).
2992 cp = strchr(zc.zc_value, '@');
2993 if (cp && (ioctl_err == 0 || !newfs)) {
2997 h = zfs_open(hdl, zc.zc_value,
2998 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3000 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3002 } else if (newfs || stream_avl) {
3004 * Track the first/top of hierarchy fs,
3005 * for mounting and sharing later.
3007 if (top_zfs && *top_zfs == NULL)
3008 *top_zfs = zfs_strdup(hdl, zc.zc_value);
3016 err |= changelist_postfix(clp);
3017 changelist_free(clp);
3020 if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3021 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3022 "failed to clear unreceived properties on %s"),
3024 (void) fprintf(stderr, "\n");
3026 if (prop_errflags & ZPROP_ERR_NORESTORE) {
3027 (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3028 "failed to restore original properties on %s"),
3030 (void) fprintf(stderr, "\n");
3033 if (err || ioctl_err)
3036 *action_handlep = zc.zc_action_handle;
3038 if (flags->verbose) {
3041 uint64_t bytes = zc.zc_cookie;
3042 time_t delta = time(NULL) - begin_time;
3045 zfs_nicenum(bytes, buf1, sizeof (buf1));
3046 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3048 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3056 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
3057 int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3058 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
3061 dmu_replay_record_t drr, drr_noswap;
3062 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3064 zio_cksum_t zcksum = { 0 };
3065 uint64_t featureflags;
3068 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3071 if (flags->isprefix &&
3072 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3073 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3074 "(%s) does not exist"), tosnap);
3075 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3078 /* read in the BEGIN record */
3079 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3083 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3084 /* It's the double end record at the end of a package */
3088 /* the kernel needs the non-byteswapped begin record */
3091 flags->byteswap = B_FALSE;
3092 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3094 * We computed the checksum in the wrong byteorder in
3095 * recv_read() above; do it again correctly.
3097 bzero(&zcksum, sizeof (zio_cksum_t));
3098 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
3099 flags->byteswap = B_TRUE;
3101 drr.drr_type = BSWAP_32(drr.drr_type);
3102 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3103 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3104 drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3105 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3106 drrb->drr_type = BSWAP_32(drrb->drr_type);
3107 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3108 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3109 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3112 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3113 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3114 "stream (bad magic number)"));
3115 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3118 featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3119 hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3121 if (!DMU_STREAM_SUPPORTED(featureflags) ||
3122 (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3123 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3124 "stream has unsupported feature, feature flags = %lx"),
3126 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3129 if (strchr(drrb->drr_toname, '@') == NULL) {
3130 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3131 "stream (bad snapshot name)"));
3132 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3135 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3136 char nonpackage_sendfs[ZFS_MAXNAMELEN];
3137 if (sendfs == NULL) {
3139 * We were not called from zfs_receive_package(). Get
3140 * the fs specified by 'zfs send'.
3143 (void) strlcpy(nonpackage_sendfs,
3144 drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
3145 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3147 sendfs = nonpackage_sendfs;
3149 return (zfs_receive_one(hdl, infd, tosnap, flags,
3150 &drr, &drr_noswap, sendfs, stream_nv, stream_avl,
3151 top_zfs, cleanup_fd, action_handlep));
3153 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3154 DMU_COMPOUNDSTREAM);
3155 return (zfs_receive_package(hdl, infd, tosnap, flags,
3156 &drr, &zcksum, top_zfs, cleanup_fd, action_handlep));
3161 * Restores a backup of tosnap from the file descriptor specified by infd.
3162 * Return 0 on total success, -2 if some things couldn't be
3163 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3164 * (-1 will override -2).
3167 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
3168 int infd, avl_tree_t *stream_avl)
3170 char *top_zfs = NULL;
3173 uint64_t action_handle = 0;
3175 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3176 VERIFY(cleanup_fd >= 0);
3178 err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
3179 stream_avl, &top_zfs, cleanup_fd, &action_handle);
3181 VERIFY(0 == close(cleanup_fd));
3183 if (err == 0 && !flags->nomount && top_zfs) {
3185 prop_changelist_t *clp;
3187 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3189 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3190 CL_GATHER_MOUNT_ALWAYS, 0);
3193 /* mount and share received datasets */
3194 err = changelist_postfix(clp);
3195 changelist_free(clp);
3198 if (zhp == NULL || clp == NULL || err)