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.
37 #include <sys/param.h>
38 #include <sys/mount.h>
39 #include <sys/mntent.h>
40 #include <sys/mnttab.h>
46 #include "zfs_namecheck.h"
48 #include "libzfs_impl.h"
50 #include <fletcher.c> /* XXX */
52 /* We need to use something for ENODATA. */
55 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t,
56 int, avl_tree_t *, char **);
59 * Routines for dealing with the AVL tree of fs-nvlists
61 typedef struct fsavl_node {
69 fsavl_compare(const void *arg1, const void *arg2)
71 const fsavl_node_t *fn1 = arg1;
72 const fsavl_node_t *fn2 = arg2;
74 if (fn1->fn_guid > fn2->fn_guid)
76 else if (fn1->fn_guid < fn2->fn_guid)
83 * Given the GUID of a snapshot, find its containing filesystem and
87 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
92 fn_find.fn_guid = snapguid;
94 fn = avl_find(avl, &fn_find, NULL);
97 *snapname = fn->fn_snapname;
104 fsavl_destroy(avl_tree_t *avl)
113 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
120 fsavl_create(nvlist_t *fss)
123 nvpair_t *fselem = NULL;
125 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
128 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
129 offsetof(fsavl_node_t, fn_node));
131 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
132 nvlist_t *nvfs, *snaps;
133 nvpair_t *snapelem = NULL;
135 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
136 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
139 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
143 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
144 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
145 fsavl_destroy(fsavl);
149 fn->fn_snapname = nvpair_name(snapelem);
153 * Note: if there are multiple snaps with the
154 * same GUID, we ignore all but one.
156 if (avl_find(fsavl, fn, NULL) == NULL)
167 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
169 typedef struct send_data {
170 uint64_t parent_fromsnap_guid;
171 nvlist_t *parent_snaps;
174 const char *fromsnap;
178 * The header nvlist is of the following format:
181 * "fromsnap" -> string (if incremental)
185 * "name" -> string (full name; for debugging)
186 * "parentfromsnap" -> number (guid of fromsnap in parent)
188 * "props" -> { name -> value (only if set here) }
189 * "snaps" -> { name (lastname) -> number (guid) }
190 * "snapprops" -> { name (lastname) -> { name -> value } }
192 * "origin" -> number (guid) (if clone)
193 * "sent" -> boolean (not on-disk)
201 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
204 send_iterate_snap(zfs_handle_t *zhp, void *arg)
206 send_data_t *sd = arg;
207 uint64_t guid = zhp->zfs_dmustats.dds_guid;
211 snapname = strrchr(zhp->zfs_name, '@')+1;
213 VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
215 * NB: if there is no fromsnap here (it's a newly created fs in
216 * an incremental replication), we will substitute the tosnap.
218 if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
219 (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
220 strcmp(snapname, sd->tosnap) == 0)) {
221 sd->parent_fromsnap_guid = guid;
224 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
225 send_iterate_prop(zhp, nv);
226 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
234 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
236 nvpair_t *elem = NULL;
238 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
239 char *propname = nvpair_name(elem);
240 zfs_prop_t prop = zfs_name_to_prop(propname);
243 if (!zfs_prop_user(propname) && zfs_prop_readonly(prop))
246 verify(nvpair_value_nvlist(elem, &propnv) == 0);
247 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION) {
248 /* these guys are modifyable, but have no source */
250 verify(nvlist_lookup_uint64(propnv,
251 ZPROP_VALUE, &value) == 0);
252 if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
256 if (nvlist_lookup_string(propnv,
257 ZPROP_SOURCE, &source) != 0)
259 if (strcmp(source, zhp->zfs_name) != 0)
263 if (zfs_prop_user(propname) ||
264 zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
266 verify(nvlist_lookup_string(propnv,
267 ZPROP_VALUE, &value) == 0);
268 VERIFY(0 == nvlist_add_string(nv, propname, value));
271 verify(nvlist_lookup_uint64(propnv,
272 ZPROP_VALUE, &value) == 0);
273 VERIFY(0 == nvlist_add_uint64(nv, propname, value));
279 send_iterate_fs(zfs_handle_t *zhp, void *arg)
281 send_data_t *sd = arg;
284 uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
285 uint64_t guid = zhp->zfs_dmustats.dds_guid;
288 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
289 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
290 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
291 sd->parent_fromsnap_guid));
293 if (zhp->zfs_dmustats.dds_origin[0]) {
294 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
295 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
298 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
299 origin->zfs_dmustats.dds_guid));
302 /* iterate over props */
303 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
304 send_iterate_prop(zhp, nv);
305 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
308 /* iterate over snaps, and set sd->parent_fromsnap_guid */
309 sd->parent_fromsnap_guid = 0;
310 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
311 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
312 (void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
313 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
314 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
315 nvlist_free(sd->parent_snaps);
316 nvlist_free(sd->snapprops);
318 /* add this fs to nvlist */
319 (void) snprintf(guidstring, sizeof (guidstring),
320 "0x%llx", (longlong_t)guid);
321 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
324 /* iterate over children */
325 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
327 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
334 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
335 const char *tosnap, nvlist_t **nvlp, avl_tree_t **avlp)
338 send_data_t sd = { 0 };
341 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
343 return (EZFS_BADTYPE);
345 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
346 sd.fromsnap = fromsnap;
349 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
357 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
368 * Routines for dealing with the sorted snapshot functionality
370 typedef struct zfs_node {
371 zfs_handle_t *zn_handle;
372 avl_node_t zn_avlnode;
376 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
378 avl_tree_t *avl = data;
379 zfs_node_t *node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
381 node->zn_handle = zhp;
388 zfs_snapshot_compare(const void *larg, const void *rarg)
390 zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
391 zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
392 uint64_t lcreate, rcreate;
395 * Sort them according to creation time. We use the hidden
396 * CREATETXG property to get an absolute ordering of snapshots.
398 lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
399 rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
401 if (lcreate < rcreate)
403 else if (lcreate > rcreate)
410 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
417 avl_create(&avl, zfs_snapshot_compare,
418 sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
420 ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
422 for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
423 ret |= callback(node->zn_handle, data);
425 while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
434 * Routines specific to "zfs send"
436 typedef struct send_dump_data {
437 /* these are all just the short snapname (the part after the @) */
438 const char *fromsnap;
440 char lastsnap[ZFS_MAXNAMELEN];
441 boolean_t seenfrom, seento, replicate, doall, fromorigin;
450 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
451 * NULL) to the file descriptor specified by outfd.
454 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, boolean_t fromorigin,
457 zfs_cmd_t zc = { 0 };
458 libzfs_handle_t *hdl = zhp->zfs_hdl;
460 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
461 assert(fromsnap == NULL || fromsnap[0] == '\0' || !fromorigin);
463 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
465 (void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_value));
466 zc.zc_cookie = outfd;
467 zc.zc_obj = fromorigin;
469 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
471 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
472 "warning: cannot send '%s'"), zhp->zfs_name);
477 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
478 "not an earlier snapshot from the same fs"));
479 return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
482 if (zfs_dataset_exists(hdl, zc.zc_name,
483 ZFS_TYPE_SNAPSHOT)) {
484 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
485 "incremental source (@%s) does not exist"),
488 return (zfs_error(hdl, EZFS_NOENT, errbuf));
500 zfs_error_aux(hdl, strerror(errno));
501 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
504 return (zfs_standard_error(hdl, errno, errbuf));
512 dump_snapshot(zfs_handle_t *zhp, void *arg)
514 send_dump_data_t *sdd = arg;
515 const char *thissnap;
518 thissnap = strchr(zhp->zfs_name, '@') + 1;
520 if (sdd->fromsnap && !sdd->seenfrom &&
521 strcmp(sdd->fromsnap, thissnap) == 0) {
522 sdd->seenfrom = B_TRUE;
523 (void) strcpy(sdd->lastsnap, thissnap);
528 if (sdd->seento || !sdd->seenfrom) {
535 (void) fprintf(stderr, "sending from @%s to %s\n",
536 sdd->lastsnap, zhp->zfs_name);
539 err = dump_ioctl(zhp, sdd->lastsnap,
540 sdd->lastsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
543 if (!sdd->seento && strcmp(sdd->tosnap, thissnap) == 0)
544 sdd->seento = B_TRUE;
546 (void) strcpy(sdd->lastsnap, thissnap);
552 dump_filesystem(zfs_handle_t *zhp, void *arg)
555 send_dump_data_t *sdd = arg;
556 boolean_t missingfrom = B_FALSE;
557 zfs_cmd_t zc = { 0 };
559 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
560 zhp->zfs_name, sdd->tosnap);
561 if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
562 (void) fprintf(stderr, "WARNING: "
563 "could not send %s@%s: does not exist\n",
564 zhp->zfs_name, sdd->tosnap);
569 if (sdd->replicate && sdd->fromsnap) {
571 * If this fs does not have fromsnap, and we're doing
572 * recursive, we need to send a full stream from the
573 * beginning (or an incremental from the origin if this
574 * is a clone). If we're doing non-recursive, then let
575 * them get the error.
577 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
578 zhp->zfs_name, sdd->fromsnap);
579 if (ioctl(zhp->zfs_hdl->libzfs_fd,
580 ZFS_IOC_OBJSET_STATS, &zc) != 0) {
581 missingfrom = B_TRUE;
586 sdd->seenfrom = sdd->seento = sdd->lastsnap[0] = 0;
587 if (sdd->fromsnap == NULL || missingfrom)
588 sdd->seenfrom = B_TRUE;
590 rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
591 if (!sdd->seenfrom) {
592 (void) fprintf(stderr,
593 "WARNING: could not send %s@%s:\n"
594 "incremental source (%s@%s) does not exist\n",
595 zhp->zfs_name, sdd->tosnap,
596 zhp->zfs_name, sdd->fromsnap);
598 } else if (!sdd->seento) {
599 (void) fprintf(stderr,
600 "WARNING: could not send %s@%s:\n"
601 "incremental source (%s@%s) "
602 "is not earlier than it\n",
603 zhp->zfs_name, sdd->tosnap,
604 zhp->zfs_name, sdd->fromsnap);
608 zfs_handle_t *snapzhp;
609 char snapname[ZFS_MAXNAMELEN];
611 (void) snprintf(snapname, sizeof (snapname), "%s@%s",
612 zfs_get_name(zhp), sdd->tosnap);
613 snapzhp = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
614 if (snapzhp == NULL) {
617 rv = dump_ioctl(snapzhp,
618 missingfrom ? NULL : sdd->fromsnap,
619 sdd->fromorigin || missingfrom,
621 sdd->seento = B_TRUE;
630 dump_filesystems(zfs_handle_t *rzhp, void *arg)
632 send_dump_data_t *sdd = arg;
634 boolean_t needagain, progress;
637 return (dump_filesystem(rzhp, sdd));
640 needagain = progress = B_FALSE;
641 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
642 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
647 uint64_t origin_guid = 0;
650 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
651 if (nvlist_lookup_boolean(fslist, "sent") == 0)
654 VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
655 (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
657 origin_nv = fsavl_find(sdd->fsavl, origin_guid, NULL);
659 nvlist_lookup_boolean(origin_nv, "sent") == ENOENT) {
661 * origin has not been sent yet;
668 zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
671 err = dump_filesystem(zhp, sdd);
672 VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
686 * Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL.
687 * If 'doall', dump all intermediate snaps.
688 * If 'replicate', dump special header and do recursively.
691 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
692 boolean_t replicate, boolean_t doall, boolean_t fromorigin,
693 boolean_t verbose, int outfd)
696 send_dump_data_t sdd = { 0 };
698 nvlist_t *fss = NULL;
699 avl_tree_t *fsavl = NULL;
701 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
702 "cannot send '%s'"), zhp->zfs_name);
704 if (fromsnap && fromsnap[0] == '\0') {
705 zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
706 "zero-length incremental source"));
707 return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
710 if (replicate || doall) {
711 dmu_replay_record_t drr = { 0 };
712 char *packbuf = NULL;
714 zio_cksum_t zc = { 0 };
716 assert(fromsnap || doall);
721 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
723 VERIFY(0 == nvlist_add_string(hdrnv,
724 "fromsnap", fromsnap));
726 VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
728 err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
729 fromsnap, tosnap, &fss, &fsavl);
732 VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
733 err = nvlist_pack(hdrnv, &packbuf, &buflen,
737 fsavl_destroy(fsavl);
739 return (zfs_standard_error(zhp->zfs_hdl,
744 /* write first begin record */
745 drr.drr_type = DRR_BEGIN;
746 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
747 drr.drr_u.drr_begin.drr_version = DMU_BACKUP_HEADER_VERSION;
748 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
749 sizeof (drr.drr_u.drr_begin.drr_toname),
750 "%s@%s", zhp->zfs_name, tosnap);
751 drr.drr_payloadlen = buflen;
752 fletcher_4_incremental_native(&drr, sizeof (drr), &zc);
753 err = write(outfd, &drr, sizeof (drr));
755 /* write header nvlist */
757 fletcher_4_incremental_native(packbuf, buflen, &zc);
758 err = write(outfd, packbuf, buflen);
762 fsavl_destroy(fsavl);
764 return (zfs_standard_error(zhp->zfs_hdl,
768 /* write end record */
770 bzero(&drr, sizeof (drr));
771 drr.drr_type = DRR_END;
772 drr.drr_u.drr_end.drr_checksum = zc;
773 err = write(outfd, &drr, sizeof (drr));
775 fsavl_destroy(fsavl);
777 return (zfs_standard_error(zhp->zfs_hdl,
783 /* dump each stream */
784 sdd.fromsnap = fromsnap;
787 sdd.replicate = replicate;
789 sdd.fromorigin = fromorigin;
792 sdd.verbose = verbose;
793 err = dump_filesystems(zhp, &sdd);
794 fsavl_destroy(fsavl);
797 if (replicate || doall) {
799 * write final end record. NB: want to do this even if
800 * there was some error, because it might not be totally
803 dmu_replay_record_t drr = { 0 };
804 drr.drr_type = DRR_END;
805 if (write(outfd, &drr, sizeof (drr)) == -1) {
806 return (zfs_standard_error(zhp->zfs_hdl,
811 return (err || sdd.err);
815 * Routines specific to "zfs recv"
819 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
820 boolean_t byteswap, zio_cksum_t *zc)
827 rv = read(fd, cp, len);
832 if (rv < 0 || len != 0) {
833 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
834 "failed to read from stream"));
835 return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
841 fletcher_4_incremental_byteswap(buf, ilen, zc);
843 fletcher_4_incremental_native(buf, ilen, zc);
849 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
850 boolean_t byteswap, zio_cksum_t *zc)
855 buf = zfs_alloc(hdl, len);
859 err = recv_read(hdl, fd, buf, len, byteswap, zc);
865 err = nvlist_unpack(buf, len, nvp, 0);
868 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
869 "stream (malformed nvlist)"));
876 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
877 int baselen, char *newname, recvflags_t flags)
880 zfs_cmd_t zc = { 0 };
882 prop_changelist_t *clp;
885 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
888 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
889 flags.force ? MS_FORCE : 0);
893 err = changelist_prefix(clp);
898 (void) strcpy(newname, tryname);
900 zc.zc_objset_type = DMU_OST_ZFS;
901 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
902 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
905 (void) printf("attempting 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, tryname);
915 if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
918 (void) strncpy(newname, name, baselen);
919 (void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
920 "recv-%u-%u", getpid(), seq);
921 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
924 (void) printf("failed - trying rename %s to %s\n",
925 zc.zc_name, zc.zc_value);
927 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
929 changelist_rename(clp, name, newname);
930 if (err && flags.verbose) {
931 (void) printf("failed (%u) - "
932 "will try again on next pass\n", errno);
935 } else if (flags.verbose) {
937 (void) printf("success\n");
939 (void) printf("failed (%u)\n", errno);
942 (void) changelist_postfix(clp);
943 changelist_free(clp);
949 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
950 char *newname, recvflags_t flags)
952 zfs_cmd_t zc = { 0 };
954 prop_changelist_t *clp;
957 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
960 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
961 flags.force ? MS_FORCE : 0);
965 err = changelist_prefix(clp);
969 zc.zc_objset_type = DMU_OST_ZFS;
970 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
973 (void) printf("attempting destroy %s\n", zc.zc_name);
974 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
978 (void) printf("success\n");
979 changelist_remove(clp, zc.zc_name);
982 (void) changelist_postfix(clp);
983 changelist_free(clp);
986 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
991 typedef struct guid_to_name_data {
994 } guid_to_name_data_t;
997 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
999 guid_to_name_data_t *gtnd = arg;
1002 if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1003 (void) strcpy(gtnd->name, zhp->zfs_name);
1006 err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1012 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1015 /* exhaustive search all local snapshots */
1016 guid_to_name_data_t gtnd;
1024 if (strchr(parent, '@') == NULL) {
1025 zhp = make_dataset_handle(hdl, parent);
1027 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1034 cp = strchr(parent, '/');
1037 zhp = make_dataset_handle(hdl, parent);
1042 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
1046 return (err == EEXIST ? 0 : ENOENT);
1051 * Return true if dataset guid1 is created before guid2.
1054 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1055 uint64_t guid1, uint64_t guid2)
1058 char *fsname, *snapname;
1059 char buf[ZFS_MAXNAMELEN];
1061 zfs_node_t zn1, zn2;
1068 nvfs = fsavl_find(avl, guid1, &snapname);
1069 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1070 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1071 zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1072 if (zn1.zn_handle == NULL)
1075 nvfs = fsavl_find(avl, guid2, &snapname);
1076 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1077 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1078 zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1079 if (zn2.zn_handle == NULL) {
1080 zfs_close(zn2.zn_handle);
1084 rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1086 zfs_close(zn1.zn_handle);
1087 zfs_close(zn2.zn_handle);
1093 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1094 recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl)
1097 avl_tree_t *local_avl;
1098 nvpair_t *fselem, *nextfselem;
1099 char *tosnap, *fromsnap;
1100 char newname[ZFS_MAXNAMELEN];
1102 boolean_t needagain, progress;
1104 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1105 VERIFY(0 == nvlist_lookup_string(stream_nv, "tosnap", &tosnap));
1111 needagain = progress = B_FALSE;
1113 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1114 &local_nv, &local_avl)) != 0)
1118 * Process deletes and renames
1120 for (fselem = nvlist_next_nvpair(local_nv, NULL);
1121 fselem; fselem = nextfselem) {
1122 nvlist_t *nvfs, *snaps;
1123 nvlist_t *stream_nvfs = NULL;
1124 nvpair_t *snapelem, *nextsnapelem;
1125 uint64_t fromguid = 0;
1126 uint64_t originguid = 0;
1127 uint64_t stream_originguid = 0;
1128 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1129 char *fsname, *stream_fsname;
1131 nextfselem = nvlist_next_nvpair(local_nv, fselem);
1133 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1134 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1135 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1136 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1137 &parent_fromsnap_guid));
1138 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1141 * First find the stream's fs, so we can check for
1142 * a different origin (due to "zfs promote")
1144 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1145 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1148 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1149 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1151 if (stream_nvfs != NULL)
1155 /* check for promote */
1156 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
1157 &stream_originguid);
1158 if (stream_nvfs && originguid != stream_originguid) {
1159 switch (created_before(hdl, local_avl,
1160 stream_originguid, originguid)) {
1163 zfs_cmd_t zc = { 0 };
1164 nvlist_t *origin_nvfs;
1165 char *origin_fsname;
1168 (void) printf("promoting %s\n", fsname);
1170 origin_nvfs = fsavl_find(local_avl, originguid,
1172 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
1173 "name", &origin_fsname));
1174 (void) strlcpy(zc.zc_value, origin_fsname,
1175 sizeof (zc.zc_value));
1176 (void) strlcpy(zc.zc_name, fsname,
1177 sizeof (zc.zc_name));
1178 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
1186 fsavl_destroy(local_avl);
1187 nvlist_free(local_nv);
1191 * We had/have the wrong origin, therefore our
1192 * list of snapshots is wrong. Need to handle
1193 * them on the next pass.
1199 for (snapelem = nvlist_next_nvpair(snaps, NULL);
1200 snapelem; snapelem = nextsnapelem) {
1202 char *stream_snapname;
1203 nvlist_t *found, *props;
1205 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1207 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1208 found = fsavl_find(stream_avl, thisguid,
1211 /* check for delete */
1212 if (found == NULL) {
1213 char name[ZFS_MAXNAMELEN];
1218 (void) snprintf(name, sizeof (name), "%s@%s",
1219 fsname, nvpair_name(snapelem));
1221 error = recv_destroy(hdl, name,
1222 strlen(fsname)+1, newname, flags);
1230 stream_nvfs = found;
1232 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
1233 &props) && 0 == nvlist_lookup_nvlist(props,
1234 stream_snapname, &props)) {
1235 zfs_cmd_t zc = { 0 };
1237 zc.zc_cookie = B_TRUE; /* clear current props */
1238 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
1239 "%s@%s", fsname, nvpair_name(snapelem));
1240 if (zcmd_write_src_nvlist(hdl, &zc,
1242 (void) zfs_ioctl(hdl,
1243 ZFS_IOC_SET_PROP, &zc);
1244 zcmd_free_nvlists(&zc);
1248 /* check for different snapname */
1249 if (strcmp(nvpair_name(snapelem),
1250 stream_snapname) != 0) {
1251 char name[ZFS_MAXNAMELEN];
1252 char tryname[ZFS_MAXNAMELEN];
1254 (void) snprintf(name, sizeof (name), "%s@%s",
1255 fsname, nvpair_name(snapelem));
1256 (void) snprintf(tryname, sizeof (name), "%s@%s",
1257 fsname, stream_snapname);
1259 error = recv_rename(hdl, name, tryname,
1260 strlen(fsname)+1, newname, flags);
1267 if (strcmp(stream_snapname, fromsnap) == 0)
1268 fromguid = thisguid;
1271 /* check for delete */
1272 if (stream_nvfs == NULL) {
1276 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
1285 if (fromguid == 0 && flags.verbose) {
1286 (void) printf("local fs %s does not have fromsnap "
1287 "(%s in stream); must have been deleted locally; "
1288 "ignoring\n", fsname, fromsnap);
1292 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
1293 "name", &stream_fsname));
1294 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
1295 "parentfromsnap", &stream_parent_fromsnap_guid));
1297 /* check for rename */
1298 if ((stream_parent_fromsnap_guid != 0 &&
1299 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
1300 strcmp(strrchr(fsname, '/'),
1301 strrchr(stream_fsname, '/')) != 0) {
1303 char tryname[ZFS_MAXNAMELEN];
1305 parent = fsavl_find(local_avl,
1306 stream_parent_fromsnap_guid, NULL);
1308 * NB: parent might not be found if we used the
1309 * tosnap for stream_parent_fromsnap_guid,
1310 * because the parent is a newly-created fs;
1311 * we'll be able to rename it after we recv the
1314 if (parent != NULL) {
1317 VERIFY(0 == nvlist_lookup_string(parent, "name",
1319 (void) snprintf(tryname, sizeof (tryname),
1320 "%s%s", pname, strrchr(stream_fsname, '/'));
1323 if (flags.verbose) {
1324 (void) printf("local fs %s new parent "
1325 "not found\n", fsname);
1329 error = recv_rename(hdl, fsname, tryname,
1330 strlen(tofs)+1, newname, flags);
1338 fsavl_destroy(local_avl);
1339 nvlist_free(local_nv);
1341 if (needagain && progress) {
1342 /* do another pass to fix up temporary names */
1344 (void) printf("another pass:\n");
1352 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
1353 recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
1356 nvlist_t *stream_nv = NULL;
1357 avl_tree_t *stream_avl = NULL;
1358 char *fromsnap = NULL;
1359 char tofs[ZFS_MAXNAMELEN];
1361 dmu_replay_record_t drre;
1363 boolean_t anyerr = B_FALSE;
1364 boolean_t softerr = B_FALSE;
1366 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1369 if (strchr(destname, '@')) {
1370 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1371 "can not specify snapshot name for multi-snapshot stream"));
1372 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1375 assert(drr->drr_type == DRR_BEGIN);
1376 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
1377 assert(drr->drr_u.drr_begin.drr_version == DMU_BACKUP_HEADER_VERSION);
1380 * Read in the nvlist from the stream.
1382 if (drr->drr_payloadlen != 0) {
1383 if (!flags.isprefix) {
1384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1385 "must use -d to receive replication "
1386 "(send -R) stream"));
1387 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1390 error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
1391 &stream_nv, flags.byteswap, zc);
1393 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1399 * Read in the end record and verify checksum.
1401 if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
1402 flags.byteswap, NULL)))
1404 if (flags.byteswap) {
1405 drre.drr_type = BSWAP_32(drre.drr_type);
1406 drre.drr_u.drr_end.drr_checksum.zc_word[0] =
1407 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
1408 drre.drr_u.drr_end.drr_checksum.zc_word[1] =
1409 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
1410 drre.drr_u.drr_end.drr_checksum.zc_word[2] =
1411 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
1412 drre.drr_u.drr_end.drr_checksum.zc_word[3] =
1413 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
1415 if (drre.drr_type != DRR_END) {
1416 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1419 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
1420 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1421 "incorrect header checksum"));
1422 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1426 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
1428 if (drr->drr_payloadlen != 0) {
1429 nvlist_t *stream_fss;
1431 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
1433 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
1434 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1435 "couldn't allocate avl tree"));
1436 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
1440 if (fromsnap != NULL) {
1441 (void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
1442 if (flags.isprefix) {
1443 int i = strcspn(drr->drr_u.drr_begin.drr_toname,
1445 /* zfs_receive_one() will create_parents() */
1446 (void) strlcat(tofs,
1447 &drr->drr_u.drr_begin.drr_toname[i],
1449 *strchr(tofs, '@') = '\0';
1451 softerr = recv_incremental_replication(hdl, tofs,
1452 flags, stream_nv, stream_avl);
1457 /* Finally, receive each contained stream */
1460 * we should figure out if it has a recoverable
1461 * error, in which case do a recv_skip() and drive on.
1462 * Note, if we fail due to already having this guid,
1463 * zfs_receive_one() will take care of it (ie,
1464 * recv_skip() and return 0).
1466 error = zfs_receive_impl(hdl, destname, flags, fd,
1467 stream_avl, top_zfs);
1468 if (error == ENODATA) {
1473 } while (error == 0);
1475 if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
1477 * Now that we have the fs's they sent us, try the
1480 softerr = recv_incremental_replication(hdl, tofs, flags,
1481 stream_nv, stream_avl);
1485 fsavl_destroy(stream_avl);
1487 nvlist_free(stream_nv);
1496 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
1498 dmu_replay_record_t *drr;
1499 void *buf = malloc(1<<20);
1501 /* XXX would be great to use lseek if possible... */
1504 while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
1505 byteswap, NULL) == 0) {
1507 drr->drr_type = BSWAP_32(drr->drr_type);
1509 switch (drr->drr_type) {
1511 /* NB: not to be used on v2 stream packages */
1512 assert(drr->drr_payloadlen == 0);
1521 drr->drr_u.drr_object.drr_bonuslen =
1522 BSWAP_32(drr->drr_u.drr_object.
1525 (void) recv_read(hdl, fd, buf,
1526 P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
1532 drr->drr_u.drr_write.drr_length =
1533 BSWAP_64(drr->drr_u.drr_write.drr_length);
1535 (void) recv_read(hdl, fd, buf,
1536 drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
1539 case DRR_FREEOBJECTS:
1544 assert(!"invalid record type");
1553 * Restores a backup of tosnap from the file descriptor specified by infd.
1556 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
1557 recvflags_t flags, dmu_replay_record_t *drr,
1558 dmu_replay_record_t *drr_noswap, avl_tree_t *stream_avl,
1561 zfs_cmd_t zc = { 0 };
1563 int ioctl_err, ioctl_errno, err, choplen;
1565 struct drr_begin *drrb = &drr->drr_u.drr_begin;
1567 char chopprefix[ZFS_MAXNAMELEN];
1568 boolean_t newfs = B_FALSE;
1569 boolean_t stream_wantsnewfs;
1570 uint64_t parent_snapguid = 0;
1571 prop_changelist_t *clp = NULL;
1572 nvlist_t *snapprops_nvlist = NULL;
1574 begin_time = time(NULL);
1576 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1579 if (stream_avl != NULL) {
1581 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
1586 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
1588 err = nvlist_lookup_nvlist(fs, "props", &props);
1590 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
1592 if (flags.canmountoff) {
1593 VERIFY(0 == nvlist_add_uint64(props,
1594 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
1596 ret = zcmd_write_src_nvlist(hdl, &zc, props);
1600 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
1601 VERIFY(0 == nvlist_lookup_nvlist(props,
1602 snapname, &snapprops_nvlist));
1610 * Determine how much of the snapshot name stored in the stream
1611 * we are going to tack on to the name they specified on the
1612 * command line, and how much we are going to chop off.
1614 * If they specified a snapshot, chop the entire name stored in
1617 (void) strcpy(chopprefix, drrb->drr_toname);
1618 if (flags.isprefix) {
1620 * They specified a fs with -d, we want to tack on
1621 * everything but the pool name stored in the stream
1623 if (strchr(tosnap, '@')) {
1624 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1625 "argument - snapshot not allowed with -d"));
1626 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1628 cp = strchr(chopprefix, '/');
1630 cp = strchr(chopprefix, '@');
1632 } else if (strchr(tosnap, '@') == NULL) {
1634 * If they specified a filesystem without -d, we want to
1635 * tack on everything after the fs specified in the
1636 * first name from the stream.
1638 cp = strchr(chopprefix, '@');
1641 choplen = strlen(chopprefix);
1644 * Determine name of destination snapshot, store in zc_value.
1646 (void) strcpy(zc.zc_value, tosnap);
1647 (void) strncat(zc.zc_value, drrb->drr_toname+choplen,
1648 sizeof (zc.zc_value));
1649 if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
1650 zcmd_free_nvlists(&zc);
1651 return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1655 * Determine the name of the origin snapshot, store in zc_string.
1657 if (drrb->drr_flags & DRR_FLAG_CLONE) {
1658 if (guid_to_name(hdl, tosnap,
1659 drrb->drr_fromguid, zc.zc_string) != 0) {
1660 zcmd_free_nvlists(&zc);
1661 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1662 "local origin for clone %s does not exist"),
1664 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1667 (void) printf("found clone origin %s\n", zc.zc_string);
1670 stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
1671 (drrb->drr_flags & DRR_FLAG_CLONE));
1673 if (stream_wantsnewfs) {
1675 * if the parent fs does not exist, look for it based on
1676 * the parent snap GUID
1678 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1679 "cannot receive new filesystem stream"));
1681 (void) strcpy(zc.zc_name, zc.zc_value);
1682 cp = strrchr(zc.zc_name, '/');
1686 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1687 char suffix[ZFS_MAXNAMELEN];
1688 (void) strcpy(suffix, strrchr(zc.zc_value, '/'));
1689 if (guid_to_name(hdl, tosnap, parent_snapguid,
1690 zc.zc_value) == 0) {
1691 *strchr(zc.zc_value, '@') = '\0';
1692 (void) strcat(zc.zc_value, suffix);
1697 * if the fs does not exist, look for it based on the
1700 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1701 "cannot receive incremental stream"));
1703 (void) strcpy(zc.zc_name, zc.zc_value);
1704 *strchr(zc.zc_name, '@') = '\0';
1706 if (!zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1707 char snap[ZFS_MAXNAMELEN];
1708 (void) strcpy(snap, strchr(zc.zc_value, '@'));
1709 if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
1710 zc.zc_value) == 0) {
1711 *strchr(zc.zc_value, '@') = '\0';
1712 (void) strcat(zc.zc_value, snap);
1717 (void) strcpy(zc.zc_name, zc.zc_value);
1718 *strchr(zc.zc_name, '@') = '\0';
1720 if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1723 * Destination fs exists. Therefore this should either
1724 * be an incremental, or the stream specifies a new fs
1725 * (full stream or clone) and they want us to blow it
1726 * away (and have therefore specified -F and removed any
1730 if (stream_wantsnewfs) {
1732 zcmd_free_nvlists(&zc);
1733 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1734 "destination '%s' exists\n"
1735 "must specify -F to overwrite it"),
1737 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1739 if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
1741 zcmd_free_nvlists(&zc);
1742 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1743 "destination has snapshots (eg. %s)\n"
1744 "must destroy them to overwrite it"),
1746 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1750 if ((zhp = zfs_open(hdl, zc.zc_name,
1751 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
1752 zcmd_free_nvlists(&zc);
1756 if (stream_wantsnewfs &&
1757 zhp->zfs_dmustats.dds_origin[0]) {
1758 zcmd_free_nvlists(&zc);
1760 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1761 "destination '%s' is a clone\n"
1762 "must destroy it to overwrite it"),
1764 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1767 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
1768 stream_wantsnewfs) {
1769 /* We can't do online recv in this case */
1770 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
1772 zcmd_free_nvlists(&zc);
1775 if (changelist_prefix(clp) != 0) {
1776 changelist_free(clp);
1777 zcmd_free_nvlists(&zc);
1781 if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_VOLUME &&
1782 zvol_remove_link(hdl, zhp->zfs_name) != 0) {
1784 zcmd_free_nvlists(&zc);
1790 * Destination filesystem does not exist. Therefore we better
1791 * be creating a new filesystem (either from a full backup, or
1792 * a clone). It would therefore be invalid if the user
1793 * specified only the pool name (i.e. if the destination name
1794 * contained no slash character).
1796 if (!stream_wantsnewfs ||
1797 (cp = strrchr(zc.zc_name, '/')) == NULL) {
1798 zcmd_free_nvlists(&zc);
1799 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1800 "destination '%s' does not exist"), zc.zc_name);
1801 return (zfs_error(hdl, EZFS_NOENT, errbuf));
1805 * Trim off the final dataset component so we perform the
1806 * recvbackup ioctl to the filesystems's parent.
1810 if (flags.isprefix && !flags.dryrun &&
1811 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
1812 zcmd_free_nvlists(&zc);
1813 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
1819 zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
1820 zc.zc_cookie = infd;
1821 zc.zc_guid = flags.force;
1822 if (flags.verbose) {
1823 (void) printf("%s %s stream of %s into %s\n",
1824 flags.dryrun ? "would receive" : "receiving",
1825 drrb->drr_fromguid ? "incremental" : "full",
1826 drrb->drr_toname, zc.zc_value);
1827 (void) fflush(stdout);
1831 zcmd_free_nvlists(&zc);
1832 return (recv_skip(hdl, infd, flags.byteswap));
1835 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
1836 ioctl_errno = errno;
1837 zcmd_free_nvlists(&zc);
1839 if (err == 0 && snapprops_nvlist) {
1840 zfs_cmd_t zc2 = { 0 };
1842 (void) strcpy(zc2.zc_name, zc.zc_value);
1843 if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
1844 (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
1845 zcmd_free_nvlists(&zc2);
1849 if (err && (ioctl_errno == ENOENT || ioctl_errno == ENODEV)) {
1851 * It may be that this snapshot already exists,
1852 * in which case we want to consume & ignore it
1853 * rather than failing.
1855 avl_tree_t *local_avl;
1856 nvlist_t *local_nv, *fs;
1857 char *cp = strchr(zc.zc_value, '@');
1860 * XXX Do this faster by just iterating over snaps in
1861 * this fs. Also if zc_value does not exist, we will
1862 * get a strange "does not exist" error message.
1865 if (gather_nvlist(hdl, zc.zc_value, NULL, NULL,
1866 &local_nv, &local_avl) == 0) {
1868 fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
1869 fsavl_destroy(local_avl);
1870 nvlist_free(local_nv);
1873 if (flags.verbose) {
1874 (void) printf("snap %s already exists; "
1875 "ignoring\n", zc.zc_value);
1877 ioctl_err = recv_skip(hdl, infd,
1885 if (ioctl_err != 0) {
1886 switch (ioctl_errno) {
1888 cp = strchr(zc.zc_value, '@');
1890 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1891 "most recent snapshot of %s does not\n"
1892 "match incremental source"), zc.zc_value);
1893 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1897 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1898 "destination %s has been modified\n"
1899 "since most recent snapshot"), zc.zc_name);
1900 (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1903 cp = strchr(zc.zc_value, '@');
1905 /* it's the containing fs that exists */
1908 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1909 "destination already exists"));
1910 (void) zfs_error_fmt(hdl, EZFS_EXISTS,
1911 dgettext(TEXT_DOMAIN, "cannot restore to %s"),
1916 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1919 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1920 "invalid stream (checksum mismatch)"));
1921 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1924 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
1929 * Mount or recreate the /dev links for the target filesystem
1930 * (if created, or if we tore them down to do an incremental
1931 * restore), and the /dev links for the new snapshot (if
1932 * created). Also mount any children of the target filesystem
1933 * if we did an incremental receive.
1935 cp = strchr(zc.zc_value, '@');
1936 if (cp && (ioctl_err == 0 || !newfs)) {
1940 h = zfs_open(hdl, zc.zc_value,
1941 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
1943 if (h->zfs_type == ZFS_TYPE_VOLUME) {
1945 err = zvol_create_link(hdl, h->zfs_name);
1946 if (err == 0 && ioctl_err == 0)
1947 err = zvol_create_link(hdl,
1951 * Track the first/top of hierarchy fs,
1952 * for mounting and sharing later.
1954 if (top_zfs && *top_zfs == NULL)
1955 *top_zfs = zfs_strdup(hdl, zc.zc_value);
1963 err |= changelist_postfix(clp);
1964 changelist_free(clp);
1967 if (err || ioctl_err)
1970 if (flags.verbose) {
1973 uint64_t bytes = zc.zc_cookie;
1974 time_t delta = time(NULL) - begin_time;
1977 zfs_nicenum(bytes, buf1, sizeof (buf1));
1978 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
1980 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
1988 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
1989 int infd, avl_tree_t *stream_avl, char **top_zfs)
1992 dmu_replay_record_t drr, drr_noswap;
1993 struct drr_begin *drrb = &drr.drr_u.drr_begin;
1995 zio_cksum_t zcksum = { 0 };
1997 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2000 if (flags.isprefix &&
2001 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
2002 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
2003 "(%s) does not exist"), tosnap);
2004 return (zfs_error(hdl, EZFS_NOENT, errbuf));
2007 /* read in the BEGIN record */
2008 if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
2012 if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
2013 /* It's the double end record at the end of a package */
2017 /* the kernel needs the non-byteswapped begin record */
2020 flags.byteswap = B_FALSE;
2021 if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
2023 * We computed the checksum in the wrong byteorder in
2024 * recv_read() above; do it again correctly.
2026 bzero(&zcksum, sizeof (zio_cksum_t));
2027 fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
2028 flags.byteswap = B_TRUE;
2030 drr.drr_type = BSWAP_32(drr.drr_type);
2031 drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
2032 drrb->drr_magic = BSWAP_64(drrb->drr_magic);
2033 drrb->drr_version = BSWAP_64(drrb->drr_version);
2034 drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
2035 drrb->drr_type = BSWAP_32(drrb->drr_type);
2036 drrb->drr_flags = BSWAP_32(drrb->drr_flags);
2037 drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
2038 drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
2041 if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
2042 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2043 "stream (bad magic number)"));
2044 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2047 if (strchr(drrb->drr_toname, '@') == NULL) {
2048 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2049 "stream (bad snapshot name)"));
2050 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2053 if (drrb->drr_version == DMU_BACKUP_STREAM_VERSION) {
2054 return (zfs_receive_one(hdl, infd, tosnap, flags,
2055 &drr, &drr_noswap, stream_avl, top_zfs));
2056 } else if (drrb->drr_version == DMU_BACKUP_HEADER_VERSION) {
2057 return (zfs_receive_package(hdl, infd, tosnap, flags,
2058 &drr, &zcksum, top_zfs));
2060 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2061 "stream is unsupported version %llu"),
2063 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2068 * Restores a backup of tosnap from the file descriptor specified by infd.
2069 * Return 0 on total success, -2 if some things couldn't be
2070 * destroyed/renamed/promoted, -1 if some things couldn't be received.
2071 * (-1 will override -2).
2074 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2075 int infd, avl_tree_t *stream_avl)
2077 char *top_zfs = NULL;
2080 err = zfs_receive_impl(hdl, tosnap, flags, infd, stream_avl, &top_zfs);
2082 if (err == 0 && top_zfs) {
2084 prop_changelist_t *clp;
2086 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
2088 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
2089 CL_GATHER_MOUNT_ALWAYS, 0);
2092 /* mount and share received datasets */
2093 err = changelist_postfix(clp);
2094 changelist_free(clp);
2097 if (zhp == NULL || clp == NULL || err)