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 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "@(#)libzfs_sendrecv.c 1.7 08/04/23 SMI"
32 #include <libdevinfo.h>
40 #include <sys/mount.h>
41 #include <sys/mntent.h>
42 #include <sys/mnttab.h>
48 #include "zfs_namecheck.h"
50 #include "libzfs_impl.h"
52 #include <fletcher.c> /* XXX */
55 * Routines for dealing with the AVL tree of fs-nvlists
57 typedef struct fsavl_node {
65 fsavl_compare(const void *arg1, const void *arg2)
67 const fsavl_node_t *fn1 = arg1;
68 const fsavl_node_t *fn2 = arg2;
70 if (fn1->fn_guid > fn2->fn_guid)
72 else if (fn1->fn_guid < fn2->fn_guid)
79 * Given the GUID of a snapshot, find its containing filesystem and
83 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
88 fn_find.fn_guid = snapguid;
90 fn = avl_find(avl, &fn_find, NULL);
93 *snapname = fn->fn_snapname;
100 fsavl_destroy(avl_tree_t *avl)
109 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
116 fsavl_create(nvlist_t *fss)
119 nvpair_t *fselem = NULL;
121 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
124 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
125 offsetof(fsavl_node_t, fn_node));
127 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
128 nvlist_t *nvfs, *snaps;
129 nvpair_t *snapelem = NULL;
131 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
132 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
135 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
139 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
140 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
141 fsavl_destroy(fsavl);
145 fn->fn_snapname = nvpair_name(snapelem);
149 * Note: if there are multiple snaps with the
150 * same GUID, we ignore all but one.
152 if (avl_find(fsavl, fn, NULL) == NULL)
163 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
165 typedef struct send_data {
166 uint64_t parent_fromsnap_guid;
167 nvlist_t *parent_snaps;
169 const char *fromsnap;
173 * The header nvlist is of the following format:
176 * "fromsnap" -> string (if incremental)
180 * "name" -> string (full name; for debugging)
181 * "parentfromsnap" -> number (guid of fromsnap in parent)
183 * "props" -> { name -> value (only if set here) }
184 * "snaps" -> { name (lastname) -> number (guid) }
186 * "origin" -> number (guid) (if clone)
187 * "sent" -> boolean (not on-disk)
196 send_iterate_snap(zfs_handle_t *zhp, void *arg)
198 send_data_t *sd = arg;
199 uint64_t guid = zhp->zfs_dmustats.dds_guid;
202 snapname = strrchr(zhp->zfs_name, '@')+1;
204 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
206 * NB: if there is no fromsnap here (it's a newly created fs in
207 * an incremental replication), we will substitute the tosnap.
209 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
210 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
211 strcmp(snapname, sd->tosnap) == 0)) {
212 sd->parent_fromsnap_guid = guid;
220 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
222 nvpair_t *elem = NULL;
224 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
225 char *propname = nvpair_name(elem);
226 zfs_prop_t prop = zfs_name_to_prop(propname);
229 if (!zfs_prop_user(propname) && zfs_prop_readonly(prop))
232 verify(nvpair_value_nvlist(elem, &propnv) == 0);
233 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION) {
234 /* these guys are modifyable, but have no source */
236 verify(nvlist_lookup_uint64(propnv,
237 ZPROP_VALUE, &value) == 0);
240 if (nvlist_lookup_string(propnv,
241 ZPROP_SOURCE, &source) != 0)
243 if (strcmp(source, zhp->zfs_name) != 0)
247 if (zfs_prop_user(propname) ||
248 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
250 verify(nvlist_lookup_string(propnv,
251 ZPROP_VALUE, &value) == 0);
252 VERIFY(0 == nvlist_add_string(nv, propname, value));
255 verify(nvlist_lookup_uint64(propnv,
256 ZPROP_VALUE, &value) == 0);
257 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
263 send_iterate_fs(zfs_handle_t *zhp, void *arg)
265 send_data_t *sd = arg;
268 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
269 uint64_t guid = zhp->zfs_dmustats.dds_guid;
272 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
273 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
274 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
275 sd->parent_fromsnap_guid));
277 if (zhp->zfs_dmustats.dds_origin[0]) {
278 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
279 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
282 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
283 origin->zfs_dmustats.dds_guid));
286 /* iterate over props */
287 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
288 send_iterate_prop(zhp, nv);
289 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
292 /* iterate over snaps, and set sd->parent_fromsnap_guid */
293 sd->parent_fromsnap_guid = 0;
294 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
295 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
296 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
297 nvlist_free(sd->parent_snaps);
299 /* add this fs to nvlist */
300 (void) snprintf(guidstring, sizeof (guidstring),
301 "0x%llx", (longlong_t)guid);
302 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
305 /* iterate over children */
306 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
308 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
315 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
316 const char *tosnap, nvlist_t **nvlp, avl_tree_t **avlp)
319 send_data_t sd = { 0 };
322 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
324 return (EZFS_BADTYPE);
326 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
327 sd.fromsnap = fromsnap;
330 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
338 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
349 * Routines for dealing with the sorted snapshot functionality
351 typedef struct zfs_node {
352 zfs_handle_t *zn_handle;
353 avl_node_t zn_avlnode;
357 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
359 avl_tree_t *avl = data;
360 zfs_node_t *node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
362 node->zn_handle = zhp;
369 zfs_snapshot_compare(const void *larg, const void *rarg)
371 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
372 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
373 uint64_t lcreate, rcreate;
376 * Sort them according to creation time. We use the hidden
377 * CREATETXG property to get an absolute ordering of snapshots.
379 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
380 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
382 if (lcreate < rcreate)
384 else if (lcreate > rcreate)
391 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
398 avl_create(&avl, zfs_snapshot_compare,
399 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
401 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
403 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
404 ret |= callback(node->zn_handle, data);
406 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
415 * Routines specific to "zfs send"
417 typedef struct send_dump_data {
418 /* these are all just the short snapname (the part after the @) */
419 const char *fromsnap;
421 char lastsnap[ZFS_MAXNAMELEN];
422 boolean_t seenfrom, seento, replicate, doall, fromorigin;
431 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
432 * NULL) to the file descriptor specified by outfd.
435 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, boolean_t fromorigin,
438 zfs_cmd_t zc = { 0 };
439 libzfs_handle_t *hdl = zhp->zfs_hdl;
441 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
442 assert(fromsnap == NULL || fromsnap[0] == '\0' || !fromorigin);
444 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
446 (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_value));
447 zc.zc_cookie = outfd;
448 zc.zc_obj = fromorigin;
450 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
452 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
453 "warning: cannot send '%s'"), zhp->zfs_name);
458 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
459 "not an earlier snapshot from the same fs"));
460 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
463 if (zfs_dataset_exists(hdl, zc.zc_name,
464 ZFS_TYPE_SNAPSHOT)) {
465 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
466 "incremental source (@%s) does not exist"),
469 return (zfs_error(hdl, EZFS_NOENT, errbuf));
482 zfs_error_aux(hdl, strerror(errno));
483 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
486 return (zfs_standard_error(hdl, errno, errbuf));
494 dump_snapshot(zfs_handle_t *zhp, void *arg)
496 send_dump_data_t *sdd = arg;
497 const char *thissnap;
500 thissnap = strchr(zhp->zfs_name, '@') + 1;
502 if (sdd->fromsnap && !sdd->seenfrom &&
503 strcmp(sdd->fromsnap, thissnap) == 0) {
504 sdd->seenfrom = B_TRUE;
505 (void) strcpy(sdd->lastsnap, thissnap);
510 if (sdd->seento || !sdd->seenfrom) {
517 (void) fprintf(stderr, "sending from @%s to %s\n",
518 sdd->lastsnap, zhp->zfs_name);
521 err = dump_ioctl(zhp, sdd->lastsnap,
522 sdd->lastsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
525 if (!sdd->seento && strcmp(sdd->tosnap, thissnap) == 0)
526 sdd->seento = B_TRUE;
528 (void) strcpy(sdd->lastsnap, thissnap);
534 dump_filesystem(zfs_handle_t *zhp, void *arg)
537 send_dump_data_t *sdd = arg;
538 boolean_t missingfrom = B_FALSE;
539 zfs_cmd_t zc = { 0 };
541 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
542 zhp->zfs_name, sdd->tosnap);
543 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
544 (void) fprintf(stderr, "WARNING: "
545 "could not send %s@%s: does not exist\n",
546 zhp->zfs_name, sdd->tosnap);
551 if (sdd->replicate && sdd->fromsnap) {
553 * If this fs does not have fromsnap, and we're doing
554 * recursive, we need to send a full stream from the
555 * beginning (or an incremental from the origin if this
556 * is a clone). If we're doing non-recursive, then let
557 * them get the error.
559 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
560 zhp->zfs_name, sdd->fromsnap);
561 if (ioctl(zhp->zfs_hdl->libzfs_fd,
562 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
563 missingfrom = B_TRUE;
568 sdd->seenfrom = sdd->seento = sdd->lastsnap[0] = 0;
569 if (sdd->fromsnap == NULL || missingfrom)
570 sdd->seenfrom = B_TRUE;
572 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
573 if (!sdd->seenfrom) {
574 (void) fprintf(stderr,
575 "WARNING: could not send %s@%s:\n"
576 "incremental source (%s@%s) does not exist\n",
577 zhp->zfs_name, sdd->tosnap,
578 zhp->zfs_name, sdd->fromsnap);
580 } else if (!sdd->seento) {
581 (void) fprintf(stderr,
582 "WARNING: could not send %s@%s:\n"
583 "incremental source (%s@%s) "
584 "is not earlier than it\n",
585 zhp->zfs_name, sdd->tosnap,
586 zhp->zfs_name, sdd->fromsnap);
590 zfs_handle_t *snapzhp;
591 char snapname[ZFS_MAXNAMELEN];
593 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
594 zfs_get_name(zhp), sdd->tosnap);
595 snapzhp = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
596 if (snapzhp == NULL) {
599 rv = dump_ioctl(snapzhp,
600 missingfrom ? NULL : sdd->fromsnap,
601 sdd->fromorigin || missingfrom,
603 sdd->seento = B_TRUE;
612 dump_filesystems(zfs_handle_t *rzhp, void *arg)
614 send_dump_data_t *sdd = arg;
616 boolean_t needagain, progress;
619 return (dump_filesystem(rzhp, sdd));
622 needagain = progress = B_FALSE;
623 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
624 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
629 uint64_t origin_guid = 0;
632 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
633 if (nvlist_lookup_boolean(fslist, "sent") == 0)
636 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
637 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
639 origin_nv = fsavl_find(sdd->fsavl, origin_guid, NULL);
641 nvlist_lookup_boolean(origin_nv, "sent") == ENOENT) {
643 * origin has not been sent yet;
650 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
653 err = dump_filesystem(zhp, sdd);
654 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
668 * Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL.
669 * If 'doall', dump all intermediate snaps.
670 * If 'replicate', dump special header and do recursively.
673 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
674 boolean_t replicate, boolean_t doall, boolean_t fromorigin,
675 boolean_t verbose, int outfd)
678 send_dump_data_t sdd = { 0 };
680 nvlist_t *fss = NULL;
681 avl_tree_t *fsavl = NULL;
683 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
684 "cannot send '%s'"), zhp->zfs_name);
686 if (fromsnap && fromsnap[0] == '\0') {
687 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
688 "zero-length incremental source"));
689 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
692 if (replicate || doall) {
693 dmu_replay_record_t drr = { 0 };
694 char *packbuf = NULL;
696 zio_cksum_t zc = { 0 };
698 assert(fromsnap || doall);
703 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
705 VERIFY(0 == nvlist_add_string(hdrnv,
706 "fromsnap", fromsnap));
708 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
710 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
711 fromsnap, tosnap, &fss, &fsavl);
714 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
715 err = nvlist_pack(hdrnv, &packbuf, &buflen,
719 fsavl_destroy(fsavl);
721 return (zfs_standard_error(zhp->zfs_hdl,
726 /* write first begin record */
727 drr.drr_type = DRR_BEGIN;
728 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
729 drr.drr_u.drr_begin.drr_version = DMU_BACKUP_HEADER_VERSION;
730 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
731 sizeof (drr.drr_u.drr_begin.drr_toname),
732 "%s@%s", zhp->zfs_name, tosnap);
733 drr.drr_payloadlen = buflen;
734 fletcher_4_incremental_native(&drr, sizeof (drr), &zc);
735 err = write(outfd, &drr, sizeof (drr));
737 /* write header nvlist */
739 fletcher_4_incremental_native(packbuf, buflen, &zc);
740 err = write(outfd, packbuf, buflen);
744 fsavl_destroy(fsavl);
746 return (zfs_standard_error(zhp->zfs_hdl,
750 /* write end record */
752 bzero(&drr, sizeof (drr));
753 drr.drr_type = DRR_END;
754 drr.drr_u.drr_end.drr_checksum = zc;
755 err = write(outfd, &drr, sizeof (drr));
757 fsavl_destroy(fsavl);
759 return (zfs_standard_error(zhp->zfs_hdl,
765 /* dump each stream */
766 sdd.fromsnap = fromsnap;
769 sdd.replicate = replicate;
771 sdd.fromorigin = fromorigin;
774 sdd.verbose = verbose;
775 err = dump_filesystems(zhp, &sdd);
776 fsavl_destroy(fsavl);
779 if (replicate || doall) {
781 * write final end record. NB: want to do this even if
782 * there was some error, because it might not be totally
785 dmu_replay_record_t drr = { 0 };
786 drr.drr_type = DRR_END;
787 if (write(outfd, &drr, sizeof (drr)) == -1) {
788 return (zfs_standard_error(zhp->zfs_hdl,
793 return (err || sdd.err);
797 * Routines specific to "zfs recv"
801 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
802 boolean_t byteswap, zio_cksum_t *zc)
809 rv = read(fd, cp, len);
814 if (rv < 0 || len != 0) {
815 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
816 "failed to read from stream"));
817 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
823 fletcher_4_incremental_byteswap(buf, ilen, zc);
825 fletcher_4_incremental_native(buf, ilen, zc);
831 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
832 boolean_t byteswap, zio_cksum_t *zc)
837 buf = zfs_alloc(hdl, len);
841 err = recv_read(hdl, fd, buf, len, byteswap, zc);
847 err = nvlist_unpack(buf, len, nvp, 0);
850 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
851 "stream (malformed nvlist)"));
858 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
859 int baselen, char *newname, recvflags_t flags)
862 zfs_cmd_t zc = { 0 };
864 prop_changelist_t *clp;
867 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
870 clp = changelist_gather(zhp, ZFS_PROP_NAME, flags.force ? MS_FORCE : 0);
874 err = changelist_prefix(clp);
879 (void) strcpy(newname, tryname);
881 zc.zc_objset_type = DMU_OST_ZFS;
882 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
883 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
886 (void) printf("attempting rename %s to %s\n",
887 zc.zc_name, zc.zc_value);
889 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
891 changelist_rename(clp, name, tryname);
896 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
899 (void) strncpy(newname, name, baselen);
900 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
901 "recv-%u-%u", getpid(), seq);
902 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
905 (void) printf("failed - trying rename %s to %s\n",
906 zc.zc_name, zc.zc_value);
908 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
910 changelist_rename(clp, name, newname);
911 if (err && flags.verbose) {
912 (void) printf("failed (%u) - "
913 "will try again on next pass\n", errno);
916 } else if (flags.verbose) {
918 (void) printf("success\n");
920 (void) printf("failed (%u)\n", errno);
923 (void) changelist_postfix(clp);
924 changelist_free(clp);
930 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
931 char *newname, recvflags_t flags)
933 zfs_cmd_t zc = { 0 };
935 prop_changelist_t *clp;
938 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
941 clp = changelist_gather(zhp, ZFS_PROP_NAME, flags.force ? MS_FORCE : 0);
945 err = changelist_prefix(clp);
949 zc.zc_objset_type = DMU_OST_ZFS;
950 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
953 (void) printf("attempting destroy %s\n", zc.zc_name);
954 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
958 (void) printf("success\n");
959 changelist_remove(clp, zc.zc_name);
962 (void) changelist_postfix(clp);
963 changelist_free(clp);
966 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
971 typedef struct guid_to_name_data {
974 } guid_to_name_data_t;
977 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
979 guid_to_name_data_t *gtnd = arg;
982 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
983 (void) strcpy(gtnd->name, zhp->zfs_name);
986 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
992 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
995 /* exhaustive search all local snapshots */
996 guid_to_name_data_t gtnd;
1004 if (strchr(parent, '@') == NULL) {
1005 zhp = make_dataset_handle(hdl, parent);
1007 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1014 cp = strchr(parent, '/');
1017 zhp = make_dataset_handle(hdl, parent);
1022 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1026 return (err == EEXIST ? 0 : ENOENT);
1031 * Return true if dataset guid1 is created before guid2.
1034 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1035 uint64_t guid1, uint64_t guid2)
1038 char *fsname, *snapname;
1039 char buf[ZFS_MAXNAMELEN];
1041 zfs_node_t zn1, zn2;
1048 nvfs = fsavl_find(avl, guid1, &snapname);
1049 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1050 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1051 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1052 if (zn1.zn_handle == NULL)
1055 nvfs = fsavl_find(avl, guid2, &snapname);
1056 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1057 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1058 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1059 if (zn2.zn_handle == NULL) {
1060 zfs_close(zn2.zn_handle);
1064 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1066 zfs_close(zn1.zn_handle);
1067 zfs_close(zn2.zn_handle);
1073 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1074 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl)
1077 avl_tree_t *local_avl;
1078 nvpair_t *fselem, *nextfselem;
1079 char *tosnap, *fromsnap;
1080 char newname[ZFS_MAXNAMELEN];
1082 boolean_t needagain, progress;
1084 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1085 VERIFY(0 == nvlist_lookup_string(stream_nv, "tosnap", &tosnap));
1091 needagain = progress = B_FALSE;
1093 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1094 &local_nv, &local_avl)) != 0)
1098 * Process deletes and renames
1100 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1101 fselem; fselem = nextfselem) {
1102 nvlist_t *nvfs, *snaps;
1103 nvlist_t *stream_nvfs = NULL;
1104 nvpair_t *snapelem, *nextsnapelem;
1105 uint64_t fromguid = 0;
1106 uint64_t originguid = 0;
1107 uint64_t stream_originguid = 0;
1108 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1109 char *fsname, *stream_fsname;
1111 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1113 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1114 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1115 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1116 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1117 &parent_fromsnap_guid));
1118 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1121 * First find the stream's fs, so we can check for
1122 * a different origin (due to "zfs promote")
1124 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1125 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1128 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1129 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1131 if (stream_nvfs != NULL)
1135 /* check for promote */
1136 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
1137 &stream_originguid);
1138 if (stream_nvfs && originguid != stream_originguid) {
1139 switch (created_before(hdl, local_avl,
1140 stream_originguid, originguid)) {
1143 zfs_cmd_t zc = { 0 };
1144 nvlist_t *origin_nvfs;
1145 char *origin_fsname;
1148 (void) printf("promoting %s\n", fsname);
1150 origin_nvfs = fsavl_find(local_avl, originguid,
1152 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
1153 "name", &origin_fsname));
1154 (void) strlcpy(zc.zc_value, origin_fsname,
1155 sizeof (zc.zc_value));
1156 (void) strlcpy(zc.zc_name, fsname,
1157 sizeof (zc.zc_name));
1158 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
1166 fsavl_destroy(local_avl);
1167 nvlist_free(local_nv);
1171 * We had/have the wrong origin, therefore our
1172 * list of snapshots is wrong. Need to handle
1173 * them on the next pass.
1179 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1180 snapelem; snapelem = nextsnapelem) {
1182 char *stream_snapname;
1185 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1187 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1188 found = fsavl_find(stream_avl, thisguid,
1191 /* check for delete */
1192 if (found == NULL) {
1193 char name[ZFS_MAXNAMELEN];
1198 (void) snprintf(name, sizeof (name), "%s@%s",
1199 fsname, nvpair_name(snapelem));
1201 error = recv_destroy(hdl, name,
1202 strlen(fsname)+1, newname, flags);
1210 stream_nvfs = found;
1212 /* check for different snapname */
1213 if (strcmp(nvpair_name(snapelem),
1214 stream_snapname) != 0) {
1215 char name[ZFS_MAXNAMELEN];
1216 char tryname[ZFS_MAXNAMELEN];
1218 (void) snprintf(name, sizeof (name), "%s@%s",
1219 fsname, nvpair_name(snapelem));
1220 (void) snprintf(tryname, sizeof (name), "%s@%s",
1221 fsname, stream_snapname);
1223 error = recv_rename(hdl, name, tryname,
1224 strlen(fsname)+1, newname, flags);
1231 if (strcmp(stream_snapname, fromsnap) == 0)
1232 fromguid = thisguid;
1235 /* check for delete */
1236 if (stream_nvfs == NULL) {
1240 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
1249 if (fromguid == 0 && flags.verbose) {
1250 (void) printf("local fs %s does not have fromsnap "
1251 "(%s in stream); must have been deleted locally; "
1252 "ignoring\n", fsname, fromsnap);
1256 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
1257 "name", &stream_fsname));
1258 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
1259 "parentfromsnap", &stream_parent_fromsnap_guid));
1261 /* check for rename */
1262 if ((stream_parent_fromsnap_guid != 0 &&
1263 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
1264 strcmp(strrchr(fsname, '/'),
1265 strrchr(stream_fsname, '/')) != 0) {
1267 char tryname[ZFS_MAXNAMELEN];
1269 parent = fsavl_find(local_avl,
1270 stream_parent_fromsnap_guid, NULL);
1272 * NB: parent might not be found if we used the
1273 * tosnap for stream_parent_fromsnap_guid,
1274 * because the parent is a newly-created fs;
1275 * we'll be able to rename it after we recv the
1278 if (parent != NULL) {
1281 VERIFY(0 == nvlist_lookup_string(parent, "name",
1283 (void) snprintf(tryname, sizeof (tryname),
1284 "%s%s", pname, strrchr(stream_fsname, '/'));
1287 if (flags.verbose) {
1288 (void) printf("local fs %s new parent "
1289 "not found\n", fsname);
1293 error = recv_rename(hdl, fsname, tryname,
1294 strlen(tofs)+1, newname, flags);
1302 fsavl_destroy(local_avl);
1303 nvlist_free(local_nv);
1305 if (needagain && progress) {
1306 /* do another pass to fix up temporary names */
1308 (void) printf("another pass:\n");
1316 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
1317 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc)
1319 nvlist_t *stream_nv = NULL;
1320 avl_tree_t *stream_avl = NULL;
1321 char *fromsnap = NULL;
1322 char tofs[ZFS_MAXNAMELEN];
1324 dmu_replay_record_t drre;
1326 boolean_t anyerr = B_FALSE;
1327 boolean_t softerr = B_FALSE;
1329 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1332 if (strchr(destname, '@')) {
1333 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1334 "can not specify snapshot name for multi-snapshot stream"));
1335 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1338 assert(drr->drr_type == DRR_BEGIN);
1339 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
1340 assert(drr->drr_u.drr_begin.drr_version == DMU_BACKUP_HEADER_VERSION);
1343 * Read in the nvlist from the stream.
1345 if (drr->drr_payloadlen != 0) {
1346 if (!flags.isprefix) {
1347 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1348 "must use -d to receive replication "
1349 "(send -R) stream"));
1350 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1353 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
1354 &stream_nv, flags.byteswap, zc);
1356 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1362 * Read in the end record and verify checksum.
1364 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
1365 flags.byteswap, NULL)))
1367 if (flags.byteswap) {
1368 drre.drr_type = BSWAP_32(drre.drr_type);
1369 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
1370 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
1371 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
1372 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
1373 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
1374 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
1375 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
1376 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
1378 if (drre.drr_type != DRR_END) {
1379 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1382 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
1383 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1384 "incorrect header checksum"));
1385 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1389 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
1391 if (drr->drr_payloadlen != 0) {
1392 nvlist_t *stream_fss;
1394 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
1396 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
1397 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1398 "couldn't allocate avl tree"));
1399 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
1403 if (fromsnap != NULL) {
1404 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
1405 if (flags.isprefix) {
1406 int i = strcspn(drr->drr_u.drr_begin.drr_toname,
1408 /* zfs_receive_one() will create_parents() */
1409 (void) strlcat(tofs,
1410 &drr->drr_u.drr_begin.drr_toname[i],
1412 *strchr(tofs, '@') = '\0';
1414 softerr = recv_incremental_replication(hdl, tofs,
1415 flags, stream_nv, stream_avl);
1420 /* Finally, receive each contained stream */
1423 * we should figure out if it has a recoverable
1424 * error, in which case do a recv_skip() and drive on.
1425 * Note, if we fail due to already having this guid,
1426 * zfs_receive_one() will take care of it (ie,
1427 * recv_skip() and return 0).
1429 error = zfs_receive(hdl, destname, flags, fd, stream_avl);
1430 if (error == ENODATA) {
1435 } while (error == 0);
1437 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
1439 * Now that we have the fs's they sent us, try the
1442 softerr = recv_incremental_replication(hdl, tofs, flags,
1443 stream_nv, stream_avl);
1447 fsavl_destroy(stream_avl);
1449 nvlist_free(stream_nv);
1458 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
1460 dmu_replay_record_t *drr;
1461 void *buf = malloc(1<<20);
1463 /* XXX would be great to use lseek if possible... */
1466 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
1467 byteswap, NULL) == 0) {
1469 drr->drr_type = BSWAP_32(drr->drr_type);
1471 switch (drr->drr_type) {
1473 /* NB: not to be used on v2 stream packages */
1474 assert(drr->drr_payloadlen == 0);
1483 drr->drr_u.drr_object.drr_bonuslen =
1484 BSWAP_32(drr->drr_u.drr_object.
1487 (void) recv_read(hdl, fd, buf,
1488 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
1494 drr->drr_u.drr_write.drr_length =
1495 BSWAP_32(drr->drr_u.drr_write.drr_length);
1497 (void) recv_read(hdl, fd, buf,
1498 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
1501 case DRR_FREEOBJECTS:
1506 assert(!"invalid record type");
1515 * Restores a backup of tosnap from the file descriptor specified by infd.
1518 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
1519 recvflags_t flags, dmu_replay_record_t *drr,
1520 dmu_replay_record_t *drr_noswap, avl_tree_t *stream_avl)
1522 zfs_cmd_t zc = { 0 };
1524 int ioctl_err, ioctl_errno, err, choplen;
1526 struct drr_begin *drrb = &drr->drr_u.drr_begin;
1528 char chopprefix[ZFS_MAXNAMELEN];
1529 boolean_t newfs = B_FALSE;
1530 boolean_t stream_wantsnewfs;
1531 uint64_t parent_snapguid = 0;
1532 prop_changelist_t *clp = NULL;
1534 begin_time = time(NULL);
1536 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1539 if (stream_avl != NULL) {
1540 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid, NULL);
1544 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
1546 err = nvlist_lookup_nvlist(fs, "props", &props);
1548 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
1550 if (flags.canmountoff) {
1551 VERIFY(0 == nvlist_add_uint64(props,
1552 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
1554 ret = zcmd_write_src_nvlist(hdl, &zc, props);
1563 * Determine how much of the snapshot name stored in the stream
1564 * we are going to tack on to the name they specified on the
1565 * command line, and how much we are going to chop off.
1567 * If they specified a snapshot, chop the entire name stored in
1570 (void) strcpy(chopprefix, drrb->drr_toname);
1571 if (flags.isprefix) {
1573 * They specified a fs with -d, we want to tack on
1574 * everything but the pool name stored in the stream
1576 if (strchr(tosnap, '@')) {
1577 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1578 "argument - snapshot not allowed with -d"));
1579 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1581 cp = strchr(chopprefix, '/');
1583 cp = strchr(chopprefix, '@');
1585 } else if (strchr(tosnap, '@') == NULL) {
1587 * If they specified a filesystem without -d, we want to
1588 * tack on everything after the fs specified in the
1589 * first name from the stream.
1591 cp = strchr(chopprefix, '@');
1594 choplen = strlen(chopprefix);
1597 * Determine name of destination snapshot, store in zc_value.
1599 (void) strcpy(zc.zc_value, tosnap);
1600 (void) strncat(zc.zc_value, drrb->drr_toname+choplen,
1601 sizeof (zc.zc_value));
1602 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
1603 zcmd_free_nvlists(&zc);
1604 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1608 * Determine the name of the origin snapshot, store in zc_string.
1610 if (drrb->drr_flags & DRR_FLAG_CLONE) {
1611 if (guid_to_name(hdl, tosnap,
1612 drrb->drr_fromguid, zc.zc_string) != 0) {
1613 zcmd_free_nvlists(&zc);
1614 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1615 "local origin for clone %s does not exist"),
1617 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1620 (void) printf("found clone origin %s\n", zc.zc_string);
1623 stream_wantsnewfs = (drrb->drr_fromguid == NULL ||
1624 (drrb->drr_flags & DRR_FLAG_CLONE));
1626 if (stream_wantsnewfs) {
1628 * if the parent fs does not exist, look for it based on
1629 * the parent snap GUID
1631 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1632 "cannot receive new filesystem stream"));
1634 (void) strcpy(zc.zc_name, zc.zc_value);
1635 cp = strrchr(zc.zc_name, '/');
1639 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1640 char suffix[ZFS_MAXNAMELEN];
1641 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
1642 if (guid_to_name(hdl, tosnap, parent_snapguid,
1643 zc.zc_value) == 0) {
1644 *strchr(zc.zc_value, '@') = '\0';
1645 (void) strcat(zc.zc_value, suffix);
1650 * if the fs does not exist, look for it based on the
1653 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1654 "cannot receive incremental stream"));
1656 (void) strcpy(zc.zc_name, zc.zc_value);
1657 *strchr(zc.zc_name, '@') = '\0';
1659 if (!zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1660 char snap[ZFS_MAXNAMELEN];
1661 (void) strcpy(snap, strchr(zc.zc_value, '@'));
1662 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
1663 zc.zc_value) == 0) {
1664 *strchr(zc.zc_value, '@') = '\0';
1665 (void) strcat(zc.zc_value, snap);
1670 (void) strcpy(zc.zc_name, zc.zc_value);
1671 *strchr(zc.zc_name, '@') = '\0';
1673 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1676 * Destination fs exists. Therefore this should either
1677 * be an incremental, or the stream specifies a new fs
1678 * (full stream or clone) and they want us to blow it
1679 * away (and have therefore specified -F and removed any
1683 if (stream_wantsnewfs) {
1685 zcmd_free_nvlists(&zc);
1686 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1687 "destination '%s' exists\n"
1688 "must specify -F to overwrite it"),
1690 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1692 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
1694 zcmd_free_nvlists(&zc);
1695 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1696 "destination has snapshots (eg. %s)\n"
1697 "must destroy them to overwrite it"),
1699 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1703 if ((zhp = zfs_open(hdl, zc.zc_name,
1704 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
1705 zcmd_free_nvlists(&zc);
1709 if (stream_wantsnewfs &&
1710 zhp->zfs_dmustats.dds_origin[0]) {
1711 zcmd_free_nvlists(&zc);
1713 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1714 "destination '%s' is a clone\n"
1715 "must destroy it to overwrite it"),
1717 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1720 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
1721 stream_wantsnewfs) {
1722 /* We can't do online recv in this case */
1723 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0);
1725 zcmd_free_nvlists(&zc);
1728 if (changelist_prefix(clp) != 0) {
1729 changelist_free(clp);
1730 zcmd_free_nvlists(&zc);
1734 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME &&
1735 zvol_remove_link(hdl, zhp->zfs_name) != 0) {
1737 zcmd_free_nvlists(&zc);
1743 * Destination filesystem does not exist. Therefore we better
1744 * be creating a new filesystem (either from a full backup, or
1745 * a clone). It would therefore be invalid if the user
1746 * specified only the pool name (i.e. if the destination name
1747 * contained no slash character).
1749 if (!stream_wantsnewfs ||
1750 (cp = strrchr(zc.zc_name, '/')) == NULL) {
1751 zcmd_free_nvlists(&zc);
1752 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1753 "destination '%s' does not exist"), zc.zc_name);
1754 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1758 * Trim off the final dataset component so we perform the
1759 * recvbackup ioctl to the filesystems's parent.
1763 if (flags.isprefix && !flags.dryrun &&
1764 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
1765 zcmd_free_nvlists(&zc);
1766 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
1772 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
1773 zc.zc_cookie = infd;
1774 zc.zc_guid = flags.force;
1775 if (flags.verbose) {
1776 (void) printf("%s %s stream of %s into %s\n",
1777 flags.dryrun ? "would receive" : "receiving",
1778 drrb->drr_fromguid ? "incremental" : "full",
1779 drrb->drr_toname, zc.zc_value);
1780 (void) fflush(stdout);
1784 zcmd_free_nvlists(&zc);
1785 return (recv_skip(hdl, infd, flags.byteswap));
1788 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
1789 ioctl_errno = errno;
1790 if (err && (ioctl_errno == ENOENT || ioctl_errno == ENODEV)) {
1792 * It may be that this snapshot already exists,
1793 * in which case we want to consume & ignore it
1794 * rather than failing.
1796 avl_tree_t *local_avl;
1797 nvlist_t *local_nv, *fs;
1798 char *cp = strchr(zc.zc_value, '@');
1801 * XXX Do this faster by just iterating over snaps in
1802 * this fs. Also if zc_value does not exist, we will
1803 * get a strange "does not exist" error message.
1806 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL,
1807 &local_nv, &local_avl) == 0) {
1809 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
1810 fsavl_destroy(local_avl);
1811 nvlist_free(local_nv);
1814 if (flags.verbose) {
1815 (void) printf("snap %s already exists; "
1816 "ignoring\n", zc.zc_value);
1818 ioctl_err = recv_skip(hdl, infd,
1825 zcmd_free_nvlists(&zc);
1827 if (ioctl_err != 0) {
1828 switch (ioctl_errno) {
1830 cp = strchr(zc.zc_value, '@');
1832 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1833 "most recent snapshot of %s does not\n"
1834 "match incremental source"), zc.zc_value);
1835 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1839 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1840 "destination %s has been modified\n"
1841 "since most recent snapshot"), zc.zc_name);
1842 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1845 cp = strchr(zc.zc_value, '@');
1847 /* it's the containing fs that exists */
1850 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1851 "destination already exists"));
1852 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
1853 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
1858 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1861 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1862 "invalid stream (checksum mismatch)"));
1863 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1866 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
1871 * Mount or recreate the /dev links for the target filesystem
1872 * (if created, or if we tore them down to do an incremental
1873 * restore), and the /dev links for the new snapshot (if
1874 * created). Also mount any children of the target filesystem
1875 * if we did an incremental receive.
1877 cp = strchr(zc.zc_value, '@');
1878 if (cp && (ioctl_err == 0 || !newfs)) {
1882 h = zfs_open(hdl, zc.zc_value,
1883 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1886 if (h->zfs_type == ZFS_TYPE_VOLUME) {
1887 err = zvol_create_link(hdl, h->zfs_name);
1888 if (err == 0 && ioctl_err == 0)
1889 err = zvol_create_link(hdl,
1892 err = zfs_mount(h, NULL, 0);
1899 err |= changelist_postfix(clp);
1900 changelist_free(clp);
1903 if (err || ioctl_err)
1906 if (flags.verbose) {
1909 uint64_t bytes = zc.zc_cookie;
1910 time_t delta = time(NULL) - begin_time;
1913 zfs_nicenum(bytes, buf1, sizeof (buf1));
1914 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
1916 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
1924 * Restores a backup of tosnap from the file descriptor specified by infd.
1925 * Return 0 on total success, -2 if some things couldn't be
1926 * destroyed/renamed/promoted, -1 if some things couldn't be received.
1927 * (-1 will override -2).
1930 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
1931 int infd, avl_tree_t *stream_avl)
1934 dmu_replay_record_t drr, drr_noswap;
1935 struct drr_begin *drrb = &drr.drr_u.drr_begin;
1937 zio_cksum_t zcksum = { 0 };
1939 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1942 if (flags.isprefix &&
1943 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
1944 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
1945 "(%s) does not exist"), tosnap);
1946 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1949 /* read in the BEGIN record */
1950 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
1954 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
1955 /* It's the double end record at the end of a package */
1959 /* the kernel needs the non-byteswapped begin record */
1962 flags.byteswap = B_FALSE;
1963 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1965 * We computed the checksum in the wrong byteorder in
1966 * recv_read() above; do it again correctly.
1968 bzero(&zcksum, sizeof (zio_cksum_t));
1969 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
1970 flags.byteswap = B_TRUE;
1972 drr.drr_type = BSWAP_32(drr.drr_type);
1973 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
1974 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
1975 drrb->drr_version = BSWAP_64(drrb->drr_version);
1976 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
1977 drrb->drr_type = BSWAP_32(drrb->drr_type);
1978 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
1979 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
1980 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
1983 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
1984 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1985 "stream (bad magic number)"));
1986 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1989 if (strchr(drrb->drr_toname, '@') == NULL) {
1990 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1991 "stream (bad snapshot name)"));
1992 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1995 if (drrb->drr_version == DMU_BACKUP_STREAM_VERSION) {
1996 return (zfs_receive_one(hdl, infd, tosnap, flags,
1997 &drr, &drr_noswap, stream_avl));
1998 } else if (drrb->drr_version == DMU_BACKUP_HEADER_VERSION) {
1999 return (zfs_receive_package(hdl, infd, tosnap, flags,
2002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2003 "stream is unsupported version %llu"),
2005 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));