usr/src/lib/libzfs/common/libzfs_sendrecv.c
changeset 13524 f0e12b33f77c
parent 13512 060607df0c9d
child 13686 4bc0783f6064
equal deleted inserted replaced
13523:6763769941d2 13524:f0e12b33f77c
    19  * CDDL HEADER END
    19  * CDDL HEADER END
    20  */
    20  */
    21 
    21 
    22 /*
    22 /*
    23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
    23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
       
    24  * Copyright (c) 2011 by Delphix. All rights reserved.
    24  */
    25  */
    25 
    26 
    26 #include <assert.h>
    27 #include <assert.h>
    27 #include <ctype.h>
    28 #include <ctype.h>
    28 #include <errno.h>
    29 #include <errno.h>
    48 #include <sys/ddt.h>
    49 #include <sys/ddt.h>
    49 
    50 
    50 /* in libzfs_dataset.c */
    51 /* in libzfs_dataset.c */
    51 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
    52 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
    52 
    53 
    53 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t,
    54 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t *,
    54     int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *);
    55     int, const char *, nvlist_t *, avl_tree_t *, char **, int, uint64_t *);
    55 
    56 
    56 static const zio_cksum_t zero_cksum = { 0 };
    57 static const zio_cksum_t zero_cksum = { 0 };
    57 
    58 
    58 typedef struct dedup_arg {
    59 typedef struct dedup_arg {
   769 	*nvlp = sd.fss;
   770 	*nvlp = sd.fss;
   770 	return (0);
   771 	return (0);
   771 }
   772 }
   772 
   773 
   773 /*
   774 /*
   774  * Routines for dealing with the sorted snapshot functionality
       
   775  */
       
   776 typedef struct zfs_node {
       
   777 	zfs_handle_t	*zn_handle;
       
   778 	avl_node_t	zn_avlnode;
       
   779 } zfs_node_t;
       
   780 
       
   781 static int
       
   782 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
       
   783 {
       
   784 	avl_tree_t *avl = data;
       
   785 	zfs_node_t *node;
       
   786 	zfs_node_t search;
       
   787 
       
   788 	search.zn_handle = zhp;
       
   789 	node = avl_find(avl, &search, NULL);
       
   790 	if (node) {
       
   791 		/*
       
   792 		 * If this snapshot was renamed while we were creating the
       
   793 		 * AVL tree, it's possible that we already inserted it under
       
   794 		 * its old name. Remove the old handle before adding the new
       
   795 		 * one.
       
   796 		 */
       
   797 		zfs_close(node->zn_handle);
       
   798 		avl_remove(avl, node);
       
   799 		free(node);
       
   800 	}
       
   801 
       
   802 	node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
       
   803 	node->zn_handle = zhp;
       
   804 	avl_add(avl, node);
       
   805 
       
   806 	return (0);
       
   807 }
       
   808 
       
   809 static int
       
   810 zfs_snapshot_compare(const void *larg, const void *rarg)
       
   811 {
       
   812 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
       
   813 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
       
   814 	uint64_t lcreate, rcreate;
       
   815 
       
   816 	/*
       
   817 	 * Sort them according to creation time.  We use the hidden
       
   818 	 * CREATETXG property to get an absolute ordering of snapshots.
       
   819 	 */
       
   820 	lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
       
   821 	rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
       
   822 
       
   823 	if (lcreate < rcreate)
       
   824 		return (-1);
       
   825 	else if (lcreate > rcreate)
       
   826 		return (+1);
       
   827 	else
       
   828 		return (0);
       
   829 }
       
   830 
       
   831 int
       
   832 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
       
   833 {
       
   834 	int ret = 0;
       
   835 	zfs_node_t *node;
       
   836 	avl_tree_t avl;
       
   837 	void *cookie = NULL;
       
   838 
       
   839 	avl_create(&avl, zfs_snapshot_compare,
       
   840 	    sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
       
   841 
       
   842 	ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
       
   843 
       
   844 	for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
       
   845 		ret |= callback(node->zn_handle, data);
       
   846 
       
   847 	while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
       
   848 		free(node);
       
   849 
       
   850 	avl_destroy(&avl);
       
   851 
       
   852 	return (ret);
       
   853 }
       
   854 
       
   855 /*
       
   856  * Routines specific to "zfs send"
   775  * Routines specific to "zfs send"
   857  */
   776  */
   858 typedef struct send_dump_data {
   777 typedef struct send_dump_data {
   859 	/* these are all just the short snapname (the part after the @) */
   778 	/* these are all just the short snapname (the part after the @) */
   860 	const char *fromsnap;
   779 	const char *fromsnap;
   861 	const char *tosnap;
   780 	const char *tosnap;
   862 	char prevsnap[ZFS_MAXNAMELEN];
   781 	char prevsnap[ZFS_MAXNAMELEN];
   863 	uint64_t prevsnap_obj;
   782 	uint64_t prevsnap_obj;
   864 	boolean_t seenfrom, seento, replicate, doall, fromorigin;
   783 	boolean_t seenfrom, seento, replicate, doall, fromorigin;
   865 	boolean_t verbose;
   784 	boolean_t verbose, dryrun, parsable;
   866 	int outfd;
   785 	int outfd;
   867 	boolean_t err;
   786 	boolean_t err;
   868 	nvlist_t *fss;
   787 	nvlist_t *fss;
   869 	avl_tree_t *fsavl;
   788 	avl_tree_t *fsavl;
   870 	snapfilter_cb_t *filter_cb;
   789 	snapfilter_cb_t *filter_cb;
   871 	void *filter_cb_arg;
   790 	void *filter_cb_arg;
   872 	nvlist_t *debugnv;
   791 	nvlist_t *debugnv;
   873 	char holdtag[ZFS_MAXNAMELEN];
   792 	char holdtag[ZFS_MAXNAMELEN];
   874 	int cleanup_fd;
   793 	int cleanup_fd;
       
   794 	uint64_t size;
   875 } send_dump_data_t;
   795 } send_dump_data_t;
   876 
   796 
   877 /*
       
   878  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
       
   879  * NULL) to the file descriptor specified by outfd.
       
   880  */
       
   881 static int
   797 static int
   882 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
   798 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
   883     boolean_t fromorigin, int outfd, nvlist_t *debugnv)
   799     boolean_t fromorigin, uint64_t *sizep)
   884 {
   800 {
   885 	zfs_cmd_t zc = { 0 };
   801 	zfs_cmd_t zc = { 0 };
   886 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   802 	libzfs_handle_t *hdl = zhp->zfs_hdl;
   887 	nvlist_t *thisdbg;
       
   888 
   803 
   889 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
   804 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
   890 	assert(fromsnap_obj == 0 || !fromorigin);
   805 	assert(fromsnap_obj == 0 || !fromorigin);
   891 
   806 
   892 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   807 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
   893 	zc.zc_cookie = outfd;
       
   894 	zc.zc_obj = fromorigin;
   808 	zc.zc_obj = fromorigin;
   895 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
   809 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
   896 	zc.zc_fromobj = fromsnap_obj;
   810 	zc.zc_fromobj = fromsnap_obj;
   897 
   811 	zc.zc_guid = 1;  /* estimate flag */
   898 	VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
   812 
   899 	if (fromsnap && fromsnap[0] != '\0') {
   813 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
   900 		VERIFY(0 == nvlist_add_string(thisdbg,
       
   901 		    "fromsnap", fromsnap));
       
   902 	}
       
   903 
       
   904 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
       
   905 		char errbuf[1024];
   814 		char errbuf[1024];
   906 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   815 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
   907 		    "warning: cannot send '%s'"), zhp->zfs_name);
   816 		    "warning: cannot estimate space for '%s'"), zhp->zfs_name);
   908 
       
   909 		VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
       
   910 		if (debugnv) {
       
   911 			VERIFY(0 == nvlist_add_nvlist(debugnv,
       
   912 			    zhp->zfs_name, thisdbg));
       
   913 		}
       
   914 		nvlist_free(thisdbg);
       
   915 
   817 
   916 		switch (errno) {
   818 		switch (errno) {
   917 
       
   918 		case EXDEV:
   819 		case EXDEV:
   919 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   820 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
   920 			    "not an earlier snapshot from the same fs"));
   821 			    "not an earlier snapshot from the same fs"));
   921 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
   822 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
   922 
   823 
   946 		default:
   847 		default:
   947 			return (zfs_standard_error(hdl, errno, errbuf));
   848 			return (zfs_standard_error(hdl, errno, errbuf));
   948 		}
   849 		}
   949 	}
   850 	}
   950 
   851 
       
   852 	*sizep = zc.zc_objset_type;
       
   853 
       
   854 	return (0);
       
   855 }
       
   856 
       
   857 /*
       
   858  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
       
   859  * NULL) to the file descriptor specified by outfd.
       
   860  */
       
   861 static int
       
   862 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
       
   863     boolean_t fromorigin, int outfd, nvlist_t *debugnv)
       
   864 {
       
   865 	zfs_cmd_t zc = { 0 };
       
   866 	libzfs_handle_t *hdl = zhp->zfs_hdl;
       
   867 	nvlist_t *thisdbg;
       
   868 
       
   869 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
       
   870 	assert(fromsnap_obj == 0 || !fromorigin);
       
   871 
       
   872 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
       
   873 	zc.zc_cookie = outfd;
       
   874 	zc.zc_obj = fromorigin;
       
   875 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
       
   876 	zc.zc_fromobj = fromsnap_obj;
       
   877 
       
   878 	VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
       
   879 	if (fromsnap && fromsnap[0] != '\0') {
       
   880 		VERIFY(0 == nvlist_add_string(thisdbg,
       
   881 		    "fromsnap", fromsnap));
       
   882 	}
       
   883 
       
   884 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
       
   885 		char errbuf[1024];
       
   886 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
       
   887 		    "warning: cannot send '%s'"), zhp->zfs_name);
       
   888 
       
   889 		VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
       
   890 		if (debugnv) {
       
   891 			VERIFY(0 == nvlist_add_nvlist(debugnv,
       
   892 			    zhp->zfs_name, thisdbg));
       
   893 		}
       
   894 		nvlist_free(thisdbg);
       
   895 
       
   896 		switch (errno) {
       
   897 		case EXDEV:
       
   898 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
       
   899 			    "not an earlier snapshot from the same fs"));
       
   900 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
       
   901 
       
   902 		case ENOENT:
       
   903 			if (zfs_dataset_exists(hdl, zc.zc_name,
       
   904 			    ZFS_TYPE_SNAPSHOT)) {
       
   905 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
       
   906 				    "incremental source (@%s) does not exist"),
       
   907 				    zc.zc_value);
       
   908 			}
       
   909 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
       
   910 
       
   911 		case EDQUOT:
       
   912 		case EFBIG:
       
   913 		case EIO:
       
   914 		case ENOLINK:
       
   915 		case ENOSPC:
       
   916 		case ENOSTR:
       
   917 		case ENXIO:
       
   918 		case EPIPE:
       
   919 		case ERANGE:
       
   920 		case EFAULT:
       
   921 		case EROFS:
       
   922 			zfs_error_aux(hdl, strerror(errno));
       
   923 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
       
   924 
       
   925 		default:
       
   926 			return (zfs_standard_error(hdl, errno, errbuf));
       
   927 		}
       
   928 	}
       
   929 
   951 	if (debugnv)
   930 	if (debugnv)
   952 		VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
   931 		VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
   953 	nvlist_free(thisdbg);
   932 	nvlist_free(thisdbg);
   954 
   933 
   955 	return (0);
   934 	return (0);
   961 	zfs_handle_t *pzhp;
   940 	zfs_handle_t *pzhp;
   962 	int error = 0;
   941 	int error = 0;
   963 	char *thissnap;
   942 	char *thissnap;
   964 
   943 
   965 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
   944 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
       
   945 
       
   946 	if (sdd->dryrun)
       
   947 		return (0);
   966 
   948 
   967 	/*
   949 	/*
   968 	 * zfs_send() only opens a cleanup_fd for sends that need it,
   950 	 * zfs_send() only opens a cleanup_fd for sends that need it,
   969 	 * e.g. replication and doall.
   951 	 * e.g. replication and doall.
   970 	 */
   952 	 */
   995 dump_snapshot(zfs_handle_t *zhp, void *arg)
   977 dump_snapshot(zfs_handle_t *zhp, void *arg)
   996 {
   978 {
   997 	send_dump_data_t *sdd = arg;
   979 	send_dump_data_t *sdd = arg;
   998 	char *thissnap;
   980 	char *thissnap;
   999 	int err;
   981 	int err;
  1000 	boolean_t isfromsnap, istosnap;
   982 	boolean_t isfromsnap, istosnap, fromorigin;
  1001 	boolean_t exclude = B_FALSE;
   983 	boolean_t exclude = B_FALSE;
  1002 
   984 
  1003 	thissnap = strchr(zhp->zfs_name, '@') + 1;
   985 	thissnap = strchr(zhp->zfs_name, '@') + 1;
  1004 	isfromsnap = (sdd->fromsnap != NULL &&
   986 	isfromsnap = (sdd->fromsnap != NULL &&
  1005 	    strcmp(sdd->fromsnap, thissnap) == 0);
   987 	    strcmp(sdd->fromsnap, thissnap) == 0);
  1072 			err = 0;
  1054 			err = 0;
  1073 		zfs_close(zhp);
  1055 		zfs_close(zhp);
  1074 		return (err);
  1056 		return (err);
  1075 	}
  1057 	}
  1076 
  1058 
  1077 	/* send it */
  1059 	fromorigin = sdd->prevsnap[0] == '\0' &&
       
  1060 	    (sdd->fromorigin || sdd->replicate);
       
  1061 
  1078 	if (sdd->verbose) {
  1062 	if (sdd->verbose) {
  1079 		(void) fprintf(stderr, "sending from @%s to %s\n",
  1063 		uint64_t size;
  1080 		    sdd->prevsnap, zhp->zfs_name);
  1064 		err = estimate_ioctl(zhp, sdd->prevsnap_obj,
  1081 	}
  1065 		    fromorigin, &size);
  1082 
  1066 
  1083 	err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
  1067 		if (sdd->parsable) {
  1084 	    sdd->prevsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
  1068 			if (sdd->prevsnap[0] != '\0') {
  1085 	    sdd->outfd, sdd->debugnv);
  1069 				(void) fprintf(stderr, "incremental\t%s\t%s",
       
  1070 				    sdd->prevsnap, zhp->zfs_name);
       
  1071 			} else {
       
  1072 				(void) fprintf(stderr, "full\t%s",
       
  1073 				    zhp->zfs_name);
       
  1074 			}
       
  1075 		} else {
       
  1076 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
       
  1077 			    "send from @%s to %s"),
       
  1078 			    sdd->prevsnap, zhp->zfs_name);
       
  1079 		}
       
  1080 		if (err == 0) {
       
  1081 			if (sdd->parsable) {
       
  1082 				(void) fprintf(stderr, "\t%llu\n",
       
  1083 				    (longlong_t)size);
       
  1084 			} else {
       
  1085 				char buf[16];
       
  1086 				zfs_nicenum(size, buf, sizeof (buf));
       
  1087 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
       
  1088 				    " estimated size is %s\n"), buf);
       
  1089 			}
       
  1090 			sdd->size += size;
       
  1091 		} else {
       
  1092 			(void) fprintf(stderr, "\n");
       
  1093 		}
       
  1094 	}
       
  1095 
       
  1096 	if (!sdd->dryrun) {
       
  1097 		err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
       
  1098 		    fromorigin, sdd->outfd, sdd->debugnv);
       
  1099 	}
  1086 
  1100 
  1087 	(void) strcpy(sdd->prevsnap, thissnap);
  1101 	(void) strcpy(sdd->prevsnap, thissnap);
  1088 	sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
  1102 	sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
  1089 	zfs_close(zhp);
  1103 	zfs_close(zhp);
  1090 	return (err);
  1104 	return (err);
  1099 	zfs_cmd_t zc = { 0 };
  1113 	zfs_cmd_t zc = { 0 };
  1100 
  1114 
  1101 	(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
  1115 	(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
  1102 	    zhp->zfs_name, sdd->tosnap);
  1116 	    zhp->zfs_name, sdd->tosnap);
  1103 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
  1117 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
  1104 		(void) fprintf(stderr, "WARNING: "
  1118 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
  1105 		    "could not send %s@%s: does not exist\n",
  1119 		    "WARNING: could not send %s@%s: does not exist\n"),
  1106 		    zhp->zfs_name, sdd->tosnap);
  1120 		    zhp->zfs_name, sdd->tosnap);
  1107 		sdd->err = B_TRUE;
  1121 		sdd->err = B_TRUE;
  1108 		return (0);
  1122 		return (0);
  1109 	}
  1123 	}
  1110 
  1124 
  1129 	if (sdd->fromsnap == NULL || missingfrom)
  1143 	if (sdd->fromsnap == NULL || missingfrom)
  1130 		sdd->seenfrom = B_TRUE;
  1144 		sdd->seenfrom = B_TRUE;
  1131 
  1145 
  1132 	rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
  1146 	rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
  1133 	if (!sdd->seenfrom) {
  1147 	if (!sdd->seenfrom) {
  1134 		(void) fprintf(stderr,
  1148 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
  1135 		    "WARNING: could not send %s@%s:\n"
  1149 		    "WARNING: could not send %s@%s:\n"
  1136 		    "incremental source (%s@%s) does not exist\n",
  1150 		    "incremental source (%s@%s) does not exist\n"),
  1137 		    zhp->zfs_name, sdd->tosnap,
  1151 		    zhp->zfs_name, sdd->tosnap,
  1138 		    zhp->zfs_name, sdd->fromsnap);
  1152 		    zhp->zfs_name, sdd->fromsnap);
  1139 		sdd->err = B_TRUE;
  1153 		sdd->err = B_TRUE;
  1140 	} else if (!sdd->seento) {
  1154 	} else if (!sdd->seento) {
  1141 		if (sdd->fromsnap) {
  1155 		if (sdd->fromsnap) {
  1142 			(void) fprintf(stderr,
  1156 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
  1143 			    "WARNING: could not send %s@%s:\n"
  1157 			    "WARNING: could not send %s@%s:\n"
  1144 			    "incremental source (%s@%s) "
  1158 			    "incremental source (%s@%s) "
  1145 			    "is not earlier than it\n",
  1159 			    "is not earlier than it\n"),
  1146 			    zhp->zfs_name, sdd->tosnap,
  1160 			    zhp->zfs_name, sdd->tosnap,
  1147 			    zhp->zfs_name, sdd->fromsnap);
  1161 			    zhp->zfs_name, sdd->fromsnap);
  1148 		} else {
  1162 		} else {
  1149 			(void) fprintf(stderr, "WARNING: "
  1163 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
  1150 			    "could not send %s@%s: does not exist\n",
  1164 			    "WARNING: "
       
  1165 			    "could not send %s@%s: does not exist\n"),
  1151 			    zhp->zfs_name, sdd->tosnap);
  1166 			    zhp->zfs_name, sdd->tosnap);
  1152 		}
  1167 		}
  1153 		sdd->err = B_TRUE;
  1168 		sdd->err = B_TRUE;
  1154 	}
  1169 	}
  1155 
  1170 
  1191 	}
  1206 	}
  1192 again:
  1207 again:
  1193 	needagain = progress = B_FALSE;
  1208 	needagain = progress = B_FALSE;
  1194 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
  1209 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
  1195 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
  1210 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
  1196 		nvlist_t *fslist;
  1211 		nvlist_t *fslist, *parent_nv;
  1197 		char *fsname;
  1212 		char *fsname;
  1198 		zfs_handle_t *zhp;
  1213 		zfs_handle_t *zhp;
  1199 		int err;
  1214 		int err;
  1200 		uint64_t origin_guid = 0;
  1215 		uint64_t origin_guid = 0;
       
  1216 		uint64_t parent_guid = 0;
  1201 
  1217 
  1202 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
  1218 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
  1203 		if (nvlist_lookup_boolean(fslist, "sent") == 0)
  1219 		if (nvlist_lookup_boolean(fslist, "sent") == 0)
  1204 			continue;
  1220 			continue;
  1205 
  1221 
  1206 		VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
  1222 		VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
  1207 		(void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
  1223 		(void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
       
  1224 		(void) nvlist_lookup_uint64(fslist, "parentfromsnap",
       
  1225 		    &parent_guid);
       
  1226 
       
  1227 		if (parent_guid != 0) {
       
  1228 			parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
       
  1229 			if (!nvlist_exists(parent_nv, "sent")) {
       
  1230 				/* parent has not been sent; skip this one */
       
  1231 				needagain = B_TRUE;
       
  1232 				continue;
       
  1233 			}
       
  1234 		}
  1208 
  1235 
  1209 		if (origin_guid != 0) {
  1236 		if (origin_guid != 0) {
  1210 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
  1237 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
  1211 			    origin_guid, NULL);
  1238 			    origin_guid, NULL);
  1212 			if (origin_nv != NULL &&
  1239 			if (origin_nv != NULL &&
  1213 			    nvlist_lookup_boolean(origin_nv,
  1240 			    !nvlist_exists(origin_nv, "sent")) {
  1214 			    "sent") == ENOENT) {
       
  1215 				/*
  1241 				/*
  1216 				 * origin has not been sent yet;
  1242 				 * origin has not been sent yet;
  1217 				 * skip this clone.
  1243 				 * skip this clone.
  1218 				 */
  1244 				 */
  1219 				needagain = B_TRUE;
  1245 				needagain = B_TRUE;
  1233 	}
  1259 	}
  1234 	if (needagain) {
  1260 	if (needagain) {
  1235 		assert(progress);
  1261 		assert(progress);
  1236 		goto again;
  1262 		goto again;
  1237 	}
  1263 	}
       
  1264 
       
  1265 	/* clean out the sent flags in case we reuse this fss */
       
  1266 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
       
  1267 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
       
  1268 		nvlist_t *fslist;
       
  1269 
       
  1270 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
       
  1271 		(void) nvlist_remove_all(fslist, "sent");
       
  1272 	}
       
  1273 
  1238 	return (0);
  1274 	return (0);
  1239 }
  1275 }
  1240 
  1276 
  1241 /*
  1277 /*
  1242  * Generate a send stream for the dataset identified by the argument zhp.
  1278  * Generate a send stream for the dataset identified by the argument zhp.
  1254  * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
  1290  * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
  1255  * case too. If "props" is set, send properties.
  1291  * case too. If "props" is set, send properties.
  1256  */
  1292  */
  1257 int
  1293 int
  1258 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
  1294 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
  1259     sendflags_t flags, int outfd, snapfilter_cb_t filter_func,
  1295     sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
  1260     void *cb_arg, nvlist_t **debugnvp)
  1296     void *cb_arg, nvlist_t **debugnvp)
  1261 {
  1297 {
  1262 	char errbuf[1024];
  1298 	char errbuf[1024];
  1263 	send_dump_data_t sdd = { 0 };
  1299 	send_dump_data_t sdd = { 0 };
  1264 	int err;
  1300 	int err = 0;
  1265 	nvlist_t *fss = NULL;
  1301 	nvlist_t *fss = NULL;
  1266 	avl_tree_t *fsavl = NULL;
  1302 	avl_tree_t *fsavl = NULL;
  1267 	static uint64_t holdseq;
  1303 	static uint64_t holdseq;
  1268 	int spa_version;
  1304 	int spa_version;
  1269 	boolean_t holdsnaps = B_FALSE;
  1305 	boolean_t holdsnaps = B_FALSE;
  1287 		if (version >= ZPL_VERSION_SA) {
  1323 		if (version >= ZPL_VERSION_SA) {
  1288 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
  1324 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
  1289 		}
  1325 		}
  1290 	}
  1326 	}
  1291 
  1327 
  1292 	if (zfs_spa_version(zhp, &spa_version) == 0 &&
  1328 	if (!flags->dryrun && zfs_spa_version(zhp, &spa_version) == 0 &&
  1293 	    spa_version >= SPA_VERSION_USERREFS &&
  1329 	    spa_version >= SPA_VERSION_USERREFS &&
  1294 	    (flags.doall || flags.replicate))
  1330 	    (flags->doall || flags->replicate))
  1295 		holdsnaps = B_TRUE;
  1331 		holdsnaps = B_TRUE;
  1296 
  1332 
  1297 	if (flags.dedup) {
  1333 	if (flags->dedup && !flags->dryrun) {
  1298 		featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
  1334 		featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
  1299 		    DMU_BACKUP_FEATURE_DEDUPPROPS);
  1335 		    DMU_BACKUP_FEATURE_DEDUPPROPS);
  1300 		if (err = pipe(pipefd)) {
  1336 		if (err = pipe(pipefd)) {
  1301 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
  1337 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
  1302 			return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
  1338 			return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
  1312 			return (zfs_error(zhp->zfs_hdl,
  1348 			return (zfs_error(zhp->zfs_hdl,
  1313 			    EZFS_THREADCREATEFAILED, errbuf));
  1349 			    EZFS_THREADCREATEFAILED, errbuf));
  1314 		}
  1350 		}
  1315 	}
  1351 	}
  1316 
  1352 
  1317 	if (flags.replicate || flags.doall || flags.props) {
  1353 	if (flags->replicate || flags->doall || flags->props) {
  1318 		dmu_replay_record_t drr = { 0 };
  1354 		dmu_replay_record_t drr = { 0 };
  1319 		char *packbuf = NULL;
  1355 		char *packbuf = NULL;
  1320 		size_t buflen = 0;
  1356 		size_t buflen = 0;
  1321 		zio_cksum_t zc = { 0 };
  1357 		zio_cksum_t zc = { 0 };
  1322 
  1358 
  1323 		if (flags.replicate || flags.props) {
  1359 		if (flags->replicate || flags->props) {
  1324 			nvlist_t *hdrnv;
  1360 			nvlist_t *hdrnv;
  1325 
  1361 
  1326 			VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
  1362 			VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
  1327 			if (fromsnap) {
  1363 			if (fromsnap) {
  1328 				VERIFY(0 == nvlist_add_string(hdrnv,
  1364 				VERIFY(0 == nvlist_add_string(hdrnv,
  1329 				    "fromsnap", fromsnap));
  1365 				    "fromsnap", fromsnap));
  1330 			}
  1366 			}
  1331 			VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
  1367 			VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
  1332 			if (!flags.replicate) {
  1368 			if (!flags->replicate) {
  1333 				VERIFY(0 == nvlist_add_boolean(hdrnv,
  1369 				VERIFY(0 == nvlist_add_boolean(hdrnv,
  1334 				    "not_recursive"));
  1370 				    "not_recursive"));
  1335 			}
  1371 			}
  1336 
  1372 
  1337 			err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
  1373 			err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
  1338 			    fromsnap, tosnap, flags.replicate, &fss, &fsavl);
  1374 			    fromsnap, tosnap, flags->replicate, &fss, &fsavl);
  1339 			if (err)
  1375 			if (err)
  1340 				goto err_out;
  1376 				goto err_out;
  1341 			VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
  1377 			VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
  1342 			err = nvlist_pack(hdrnv, &packbuf, &buflen,
  1378 			err = nvlist_pack(hdrnv, &packbuf, &buflen,
  1343 			    NV_ENCODE_XDR, 0);
  1379 			    NV_ENCODE_XDR, 0);
  1350 				nvlist_free(fss);
  1386 				nvlist_free(fss);
  1351 				goto stderr_out;
  1387 				goto stderr_out;
  1352 			}
  1388 			}
  1353 		}
  1389 		}
  1354 
  1390 
  1355 		/* write first begin record */
  1391 		if (!flags->dryrun) {
  1356 		drr.drr_type = DRR_BEGIN;
  1392 			/* write first begin record */
  1357 		drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
  1393 			drr.drr_type = DRR_BEGIN;
  1358 		DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.drr_versioninfo,
  1394 			drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
  1359 		    DMU_COMPOUNDSTREAM);
  1395 			DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
  1360 		DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.drr_versioninfo,
  1396 			    drr_versioninfo, DMU_COMPOUNDSTREAM);
  1361 		    featureflags);
  1397 			DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
  1362 		(void) snprintf(drr.drr_u.drr_begin.drr_toname,
  1398 			    drr_versioninfo, featureflags);
  1363 		    sizeof (drr.drr_u.drr_begin.drr_toname),
  1399 			(void) snprintf(drr.drr_u.drr_begin.drr_toname,
  1364 		    "%s@%s", zhp->zfs_name, tosnap);
  1400 			    sizeof (drr.drr_u.drr_begin.drr_toname),
  1365 		drr.drr_payloadlen = buflen;
  1401 			    "%s@%s", zhp->zfs_name, tosnap);
  1366 		err = cksum_and_write(&drr, sizeof (drr), &zc, outfd);
  1402 			drr.drr_payloadlen = buflen;
  1367 
  1403 			err = cksum_and_write(&drr, sizeof (drr), &zc, outfd);
  1368 		/* write header nvlist */
  1404 
  1369 		if (err != -1 && packbuf != NULL) {
  1405 			/* write header nvlist */
  1370 			err = cksum_and_write(packbuf, buflen, &zc, outfd);
  1406 			if (err != -1 && packbuf != NULL) {
  1371 		}
  1407 				err = cksum_and_write(packbuf, buflen, &zc,
  1372 		free(packbuf);
  1408 				    outfd);
  1373 		if (err == -1) {
  1409 			}
  1374 			fsavl_destroy(fsavl);
  1410 			free(packbuf);
  1375 			nvlist_free(fss);
  1411 			if (err == -1) {
  1376 			err = errno;
  1412 				fsavl_destroy(fsavl);
  1377 			goto stderr_out;
  1413 				nvlist_free(fss);
  1378 		}
  1414 				err = errno;
  1379 
  1415 				goto stderr_out;
  1380 		/* write end record */
  1416 			}
  1381 		if (err != -1) {
  1417 
       
  1418 			/* write end record */
  1382 			bzero(&drr, sizeof (drr));
  1419 			bzero(&drr, sizeof (drr));
  1383 			drr.drr_type = DRR_END;
  1420 			drr.drr_type = DRR_END;
  1384 			drr.drr_u.drr_end.drr_checksum = zc;
  1421 			drr.drr_u.drr_end.drr_checksum = zc;
  1385 			err = write(outfd, &drr, sizeof (drr));
  1422 			err = write(outfd, &drr, sizeof (drr));
  1386 			if (err == -1) {
  1423 			if (err == -1) {
  1387 				fsavl_destroy(fsavl);
  1424 				fsavl_destroy(fsavl);
  1388 				nvlist_free(fss);
  1425 				nvlist_free(fss);
  1389 				err = errno;
  1426 				err = errno;
  1390 				goto stderr_out;
  1427 				goto stderr_out;
  1391 			}
  1428 			}
       
  1429 
       
  1430 			err = 0;
  1392 		}
  1431 		}
  1393 	}
  1432 	}
  1394 
  1433 
  1395 	/* dump each stream */
  1434 	/* dump each stream */
  1396 	sdd.fromsnap = fromsnap;
  1435 	sdd.fromsnap = fromsnap;
  1397 	sdd.tosnap = tosnap;
  1436 	sdd.tosnap = tosnap;
  1398 	if (flags.dedup)
  1437 	if (flags->dedup)
  1399 		sdd.outfd = pipefd[0];
  1438 		sdd.outfd = pipefd[0];
  1400 	else
  1439 	else
  1401 		sdd.outfd = outfd;
  1440 		sdd.outfd = outfd;
  1402 	sdd.replicate = flags.replicate;
  1441 	sdd.replicate = flags->replicate;
  1403 	sdd.doall = flags.doall;
  1442 	sdd.doall = flags->doall;
  1404 	sdd.fromorigin = flags.fromorigin;
  1443 	sdd.fromorigin = flags->fromorigin;
  1405 	sdd.fss = fss;
  1444 	sdd.fss = fss;
  1406 	sdd.fsavl = fsavl;
  1445 	sdd.fsavl = fsavl;
  1407 	sdd.verbose = flags.verbose;
  1446 	sdd.verbose = flags->verbose;
       
  1447 	sdd.parsable = flags->parsable;
       
  1448 	sdd.dryrun = flags->dryrun;
  1408 	sdd.filter_cb = filter_func;
  1449 	sdd.filter_cb = filter_func;
  1409 	sdd.filter_cb_arg = cb_arg;
  1450 	sdd.filter_cb_arg = cb_arg;
  1410 	if (debugnvp)
  1451 	if (debugnvp)
  1411 		sdd.debugnv = *debugnvp;
  1452 		sdd.debugnv = *debugnvp;
  1412 	if (holdsnaps) {
  1453 	if (holdsnaps) {
  1419 			goto stderr_out;
  1460 			goto stderr_out;
  1420 		}
  1461 		}
  1421 	} else {
  1462 	} else {
  1422 		sdd.cleanup_fd = -1;
  1463 		sdd.cleanup_fd = -1;
  1423 	}
  1464 	}
       
  1465 	if (flags->verbose) {
       
  1466 		/*
       
  1467 		 * Do a verbose no-op dry run to get all the verbose output
       
  1468 		 * before generating any data.  Then do a non-verbose real
       
  1469 		 * run to generate the streams.
       
  1470 		 */
       
  1471 		sdd.dryrun = B_TRUE;
       
  1472 		err = dump_filesystems(zhp, &sdd);
       
  1473 		sdd.dryrun = flags->dryrun;
       
  1474 		sdd.verbose = B_FALSE;
       
  1475 		if (flags->parsable) {
       
  1476 			(void) fprintf(stderr, "size\t%llu\n",
       
  1477 			    (longlong_t)sdd.size);
       
  1478 		} else {
       
  1479 			char buf[16];
       
  1480 			zfs_nicenum(sdd.size, buf, sizeof (buf));
       
  1481 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
       
  1482 			    "total estimated size is %s\n"), buf);
       
  1483 		}
       
  1484 	}
  1424 	err = dump_filesystems(zhp, &sdd);
  1485 	err = dump_filesystems(zhp, &sdd);
  1425 	fsavl_destroy(fsavl);
  1486 	fsavl_destroy(fsavl);
  1426 	nvlist_free(fss);
  1487 	nvlist_free(fss);
  1427 
  1488 
  1428 	if (flags.dedup) {
  1489 	if (flags->dedup) {
  1429 		(void) close(pipefd[0]);
  1490 		(void) close(pipefd[0]);
  1430 		(void) pthread_join(tid, NULL);
  1491 		(void) pthread_join(tid, NULL);
  1431 	}
  1492 	}
  1432 
  1493 
  1433 	if (sdd.cleanup_fd != -1) {
  1494 	if (sdd.cleanup_fd != -1) {
  1434 		VERIFY(0 == close(sdd.cleanup_fd));
  1495 		VERIFY(0 == close(sdd.cleanup_fd));
  1435 		sdd.cleanup_fd = -1;
  1496 		sdd.cleanup_fd = -1;
  1436 	}
  1497 	}
  1437 
  1498 
  1438 	if (flags.replicate || flags.doall || flags.props) {
  1499 	if (!flags->dryrun && (flags->replicate || flags->doall ||
       
  1500 	    flags->props)) {
  1439 		/*
  1501 		/*
  1440 		 * write final end record.  NB: want to do this even if
  1502 		 * write final end record.  NB: want to do this even if
  1441 		 * there was some error, because it might not be totally
  1503 		 * there was some error, because it might not be totally
  1442 		 * failed.
  1504 		 * failed.
  1443 		 */
  1505 		 */
  1454 stderr_out:
  1516 stderr_out:
  1455 	err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
  1517 	err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
  1456 err_out:
  1518 err_out:
  1457 	if (sdd.cleanup_fd != -1)
  1519 	if (sdd.cleanup_fd != -1)
  1458 		VERIFY(0 == close(sdd.cleanup_fd));
  1520 		VERIFY(0 == close(sdd.cleanup_fd));
  1459 	if (flags.dedup) {
  1521 	if (flags->dedup) {
  1460 		(void) pthread_cancel(tid);
  1522 		(void) pthread_cancel(tid);
  1461 		(void) pthread_join(tid, NULL);
  1523 		(void) pthread_join(tid, NULL);
  1462 		(void) close(pipefd[0]);
  1524 		(void) close(pipefd[0]);
  1463 	}
  1525 	}
  1464 	return (err);
  1526 	return (err);
  1525 	return (0);
  1587 	return (0);
  1526 }
  1588 }
  1527 
  1589 
  1528 static int
  1590 static int
  1529 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
  1591 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
  1530     int baselen, char *newname, recvflags_t flags)
  1592     int baselen, char *newname, recvflags_t *flags)
  1531 {
  1593 {
  1532 	static int seq;
  1594 	static int seq;
  1533 	zfs_cmd_t zc = { 0 };
  1595 	zfs_cmd_t zc = { 0 };
  1534 	int err;
  1596 	int err;
  1535 	prop_changelist_t *clp;
  1597 	prop_changelist_t *clp;
  1537 
  1599 
  1538 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
  1600 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
  1539 	if (zhp == NULL)
  1601 	if (zhp == NULL)
  1540 		return (-1);
  1602 		return (-1);
  1541 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
  1603 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
  1542 	    flags.force ? MS_FORCE : 0);
  1604 	    flags->force ? MS_FORCE : 0);
  1543 	zfs_close(zhp);
  1605 	zfs_close(zhp);
  1544 	if (clp == NULL)
  1606 	if (clp == NULL)
  1545 		return (-1);
  1607 		return (-1);
  1546 	err = changelist_prefix(clp);
  1608 	err = changelist_prefix(clp);
  1547 	if (err)
  1609 	if (err)
  1553 	if (tryname) {
  1615 	if (tryname) {
  1554 		(void) strcpy(newname, tryname);
  1616 		(void) strcpy(newname, tryname);
  1555 
  1617 
  1556 		(void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
  1618 		(void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
  1557 
  1619 
  1558 		if (flags.verbose) {
  1620 		if (flags->verbose) {
  1559 			(void) printf("attempting rename %s to %s\n",
  1621 			(void) printf("attempting rename %s to %s\n",
  1560 			    zc.zc_name, zc.zc_value);
  1622 			    zc.zc_name, zc.zc_value);
  1561 		}
  1623 		}
  1562 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
  1624 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
  1563 		if (err == 0)
  1625 		if (err == 0)
  1572 		(void) strncpy(newname, name, baselen);
  1634 		(void) strncpy(newname, name, baselen);
  1573 		(void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
  1635 		(void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
  1574 		    "recv-%u-%u", getpid(), seq);
  1636 		    "recv-%u-%u", getpid(), seq);
  1575 		(void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
  1637 		(void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
  1576 
  1638 
  1577 		if (flags.verbose) {
  1639 		if (flags->verbose) {
  1578 			(void) printf("failed - trying rename %s to %s\n",
  1640 			(void) printf("failed - trying rename %s to %s\n",
  1579 			    zc.zc_name, zc.zc_value);
  1641 			    zc.zc_name, zc.zc_value);
  1580 		}
  1642 		}
  1581 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
  1643 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
  1582 		if (err == 0)
  1644 		if (err == 0)
  1583 			changelist_rename(clp, name, newname);
  1645 			changelist_rename(clp, name, newname);
  1584 		if (err && flags.verbose) {
  1646 		if (err && flags->verbose) {
  1585 			(void) printf("failed (%u) - "
  1647 			(void) printf("failed (%u) - "
  1586 			    "will try again on next pass\n", errno);
  1648 			    "will try again on next pass\n", errno);
  1587 		}
  1649 		}
  1588 		err = EAGAIN;
  1650 		err = EAGAIN;
  1589 	} else if (flags.verbose) {
  1651 	} else if (flags->verbose) {
  1590 		if (err == 0)
  1652 		if (err == 0)
  1591 			(void) printf("success\n");
  1653 			(void) printf("success\n");
  1592 		else
  1654 		else
  1593 			(void) printf("failed (%u)\n", errno);
  1655 			(void) printf("failed (%u)\n", errno);
  1594 	}
  1656 	}
  1599 	return (err);
  1661 	return (err);
  1600 }
  1662 }
  1601 
  1663 
  1602 static int
  1664 static int
  1603 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
  1665 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
  1604     char *newname, recvflags_t flags)
  1666     char *newname, recvflags_t *flags)
  1605 {
  1667 {
  1606 	zfs_cmd_t zc = { 0 };
  1668 	zfs_cmd_t zc = { 0 };
  1607 	int err = 0;
  1669 	int err = 0;
  1608 	prop_changelist_t *clp;
  1670 	prop_changelist_t *clp;
  1609 	zfs_handle_t *zhp;
  1671 	zfs_handle_t *zhp;
  1612 
  1674 
  1613 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
  1675 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
  1614 	if (zhp == NULL)
  1676 	if (zhp == NULL)
  1615 		return (-1);
  1677 		return (-1);
  1616 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
  1678 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
  1617 	    flags.force ? MS_FORCE : 0);
  1679 	    flags->force ? MS_FORCE : 0);
  1618 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
  1680 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
  1619 	    zfs_spa_version(zhp, &spa_version) == 0 &&
  1681 	    zfs_spa_version(zhp, &spa_version) == 0 &&
  1620 	    spa_version >= SPA_VERSION_USERREFS)
  1682 	    spa_version >= SPA_VERSION_USERREFS)
  1621 		defer = B_TRUE;
  1683 		defer = B_TRUE;
  1622 	zfs_close(zhp);
  1684 	zfs_close(zhp);
  1628 
  1690 
  1629 	zc.zc_objset_type = DMU_OST_ZFS;
  1691 	zc.zc_objset_type = DMU_OST_ZFS;
  1630 	zc.zc_defer_destroy = defer;
  1692 	zc.zc_defer_destroy = defer;
  1631 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
  1693 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
  1632 
  1694 
  1633 	if (flags.verbose)
  1695 	if (flags->verbose)
  1634 		(void) printf("attempting destroy %s\n", zc.zc_name);
  1696 		(void) printf("attempting destroy %s\n", zc.zc_name);
  1635 	err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
  1697 	err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
  1636 	if (err == 0) {
  1698 	if (err == 0) {
  1637 		if (flags.verbose)
  1699 		if (flags->verbose)
  1638 			(void) printf("success\n");
  1700 			(void) printf("success\n");
  1639 		changelist_remove(clp, zc.zc_name);
  1701 		changelist_remove(clp, zc.zc_name);
  1640 	}
  1702 	}
  1641 
  1703 
  1642 	(void) changelist_postfix(clp);
  1704 	(void) changelist_postfix(clp);
  1655 }
  1717 }
  1656 
  1718 
  1657 typedef struct guid_to_name_data {
  1719 typedef struct guid_to_name_data {
  1658 	uint64_t guid;
  1720 	uint64_t guid;
  1659 	char *name;
  1721 	char *name;
       
  1722 	char *skip;
  1660 } guid_to_name_data_t;
  1723 } guid_to_name_data_t;
  1661 
  1724 
  1662 static int
  1725 static int
  1663 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
  1726 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
  1664 {
  1727 {
  1665 	guid_to_name_data_t *gtnd = arg;
  1728 	guid_to_name_data_t *gtnd = arg;
  1666 	int err;
  1729 	int err;
       
  1730 
       
  1731 	if (gtnd->skip != NULL &&
       
  1732 	    strcmp(zhp->zfs_name, gtnd->skip) == 0) {
       
  1733 		return (0);
       
  1734 	}
  1667 
  1735 
  1668 	if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
  1736 	if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
  1669 		(void) strcpy(gtnd->name, zhp->zfs_name);
  1737 		(void) strcpy(gtnd->name, zhp->zfs_name);
  1670 		zfs_close(zhp);
  1738 		zfs_close(zhp);
  1671 		return (EEXIST);
  1739 		return (EEXIST);
  1672 	}
  1740 	}
       
  1741 
  1673 	err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
  1742 	err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
  1674 	zfs_close(zhp);
  1743 	zfs_close(zhp);
  1675 	return (err);
  1744 	return (err);
  1676 }
  1745 }
  1677 
  1746 
       
  1747 /*
       
  1748  * Attempt to find the local dataset associated with this guid.  In the case of
       
  1749  * multiple matches, we attempt to find the "best" match by searching
       
  1750  * progressively larger portions of the hierarchy.  This allows one to send a
       
  1751  * tree of datasets individually and guarantee that we will find the source
       
  1752  * guid within that hierarchy, even if there are multiple matches elsewhere.
       
  1753  */
  1678 static int
  1754 static int
  1679 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
  1755 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
  1680     char *name)
  1756     char *name)
  1681 {
  1757 {
  1682 	/* exhaustive search all local snapshots */
  1758 	/* exhaustive search all local snapshots */
       
  1759 	char pname[ZFS_MAXNAMELEN];
  1683 	guid_to_name_data_t gtnd;
  1760 	guid_to_name_data_t gtnd;
  1684 	int err = 0;
  1761 	int err = 0;
  1685 	zfs_handle_t *zhp;
  1762 	zfs_handle_t *zhp;
  1686 	char *cp;
  1763 	char *cp;
  1687 
  1764 
  1688 	gtnd.guid = guid;
  1765 	gtnd.guid = guid;
  1689 	gtnd.name = name;
  1766 	gtnd.name = name;
  1690 
  1767 	gtnd.skip = NULL;
  1691 	if (strchr(parent, '@') == NULL) {
  1768 
  1692 		zhp = make_dataset_handle(hdl, parent);
  1769 	(void) strlcpy(pname, parent, sizeof (pname));
  1693 		if (zhp != NULL) {
  1770 
  1694 			err = zfs_iter_children(zhp, guid_to_name_cb, &gtnd);
  1771 	/*
  1695 			zfs_close(zhp);
  1772 	 * Search progressively larger portions of the hierarchy.  This will
  1696 			if (err == EEXIST)
  1773 	 * select the "most local" version of the origin snapshot in the case
  1697 				return (0);
  1774 	 * that there are multiple matching snapshots in the system.
  1698 		}
  1775 	 */
  1699 	}
  1776 	while ((cp = strrchr(pname, '/')) != NULL) {
  1700 
  1777 
  1701 	cp = strchr(parent, '/');
  1778 		/* Chop off the last component and open the parent */
  1702 	if (cp)
       
  1703 		*cp = '\0';
  1779 		*cp = '\0';
  1704 	zhp = make_dataset_handle(hdl, parent);
  1780 		zhp = make_dataset_handle(hdl, pname);
  1705 	if (cp)
  1781 
  1706 		*cp = '/';
  1782 		if (zhp == NULL)
  1707 
  1783 			continue;
  1708 	if (zhp) {
  1784 
  1709 		err = zfs_iter_children(zhp, guid_to_name_cb, &gtnd);
  1785 		err = zfs_iter_children(zhp, guid_to_name_cb, &gtnd);
  1710 		zfs_close(zhp);
  1786 		zfs_close(zhp);
  1711 	}
  1787 		if (err == EEXIST)
  1712 
  1788 			return (0);
  1713 	return (err == EEXIST ? 0 : ENOENT);
  1789 
  1714 
  1790 		/*
       
  1791 		 * Remember the dataset that we already searched, so we
       
  1792 		 * skip it next time through.
       
  1793 		 */
       
  1794 		gtnd.skip = pname;
       
  1795 	}
       
  1796 
       
  1797 	return (ENOENT);
  1715 }
  1798 }
  1716 
  1799 
  1717 /*
  1800 /*
  1718  * Return true if dataset guid1 is created before guid2.
  1801  * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
       
  1802  * guid1 is after guid2.
  1719  */
  1803  */
  1720 static int
  1804 static int
  1721 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
  1805 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
  1722     uint64_t guid1, uint64_t guid2)
  1806     uint64_t guid1, uint64_t guid2)
  1723 {
  1807 {
  1724 	nvlist_t *nvfs;
  1808 	nvlist_t *nvfs;
  1725 	char *fsname, *snapname;
  1809 	char *fsname, *snapname;
  1726 	char buf[ZFS_MAXNAMELEN];
  1810 	char buf[ZFS_MAXNAMELEN];
  1727 	int rv;
  1811 	int rv;
  1728 	zfs_node_t zn1, zn2;
  1812 	zfs_handle_t *guid1hdl, *guid2hdl;
       
  1813 	uint64_t create1, create2;
  1729 
  1814 
  1730 	if (guid2 == 0)
  1815 	if (guid2 == 0)
  1731 		return (0);
  1816 		return (0);
  1732 	if (guid1 == 0)
  1817 	if (guid1 == 0)
  1733 		return (1);
  1818 		return (1);
  1734 
  1819 
  1735 	nvfs = fsavl_find(avl, guid1, &snapname);
  1820 	nvfs = fsavl_find(avl, guid1, &snapname);
  1736 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
  1821 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
  1737 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
  1822 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
  1738 	zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
  1823 	guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
  1739 	if (zn1.zn_handle == NULL)
  1824 	if (guid1hdl == NULL)
  1740 		return (-1);
  1825 		return (-1);
  1741 
  1826 
  1742 	nvfs = fsavl_find(avl, guid2, &snapname);
  1827 	nvfs = fsavl_find(avl, guid2, &snapname);
  1743 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
  1828 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
  1744 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
  1829 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
  1745 	zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
  1830 	guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
  1746 	if (zn2.zn_handle == NULL) {
  1831 	if (guid2hdl == NULL) {
  1747 		zfs_close(zn2.zn_handle);
  1832 		zfs_close(guid1hdl);
  1748 		return (-1);
  1833 		return (-1);
  1749 	}
  1834 	}
  1750 
  1835 
  1751 	rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
  1836 	create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
  1752 
  1837 	create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
  1753 	zfs_close(zn1.zn_handle);
  1838 
  1754 	zfs_close(zn2.zn_handle);
  1839 	if (create1 < create2)
       
  1840 		rv = -1;
       
  1841 	else if (create1 > create2)
       
  1842 		rv = +1;
       
  1843 	else
       
  1844 		rv = 0;
       
  1845 
       
  1846 	zfs_close(guid1hdl);
       
  1847 	zfs_close(guid2hdl);
  1755 
  1848 
  1756 	return (rv);
  1849 	return (rv);
  1757 }
  1850 }
  1758 
  1851 
  1759 static int
  1852 static int
  1760 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
  1853 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
  1761     recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
  1854     recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
  1762     nvlist_t *renamed)
  1855     nvlist_t *renamed)
  1763 {
  1856 {
  1764 	nvlist_t *local_nv;
  1857 	nvlist_t *local_nv;
  1765 	avl_tree_t *local_avl;
  1858 	avl_tree_t *local_avl;
  1766 	nvpair_t *fselem, *nextfselem;
  1859 	nvpair_t *fselem, *nextfselem;
  1773 	VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
  1866 	VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
  1774 
  1867 
  1775 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
  1868 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
  1776 	    ENOENT);
  1869 	    ENOENT);
  1777 
  1870 
  1778 	if (flags.dryrun)
  1871 	if (flags->dryrun)
  1779 		return (0);
  1872 		return (0);
  1780 
  1873 
  1781 again:
  1874 again:
  1782 	needagain = progress = B_FALSE;
  1875 	needagain = progress = B_FALSE;
  1783 
  1876 
  1833 				/* promote it! */
  1926 				/* promote it! */
  1834 				zfs_cmd_t zc = { 0 };
  1927 				zfs_cmd_t zc = { 0 };
  1835 				nvlist_t *origin_nvfs;
  1928 				nvlist_t *origin_nvfs;
  1836 				char *origin_fsname;
  1929 				char *origin_fsname;
  1837 
  1930 
  1838 				if (flags.verbose)
  1931 				if (flags->verbose)
  1839 					(void) printf("promoting %s\n", fsname);
  1932 					(void) printf("promoting %s\n", fsname);
  1840 
  1933 
  1841 				origin_nvfs = fsavl_find(local_avl, originguid,
  1934 				origin_nvfs = fsavl_find(local_avl, originguid,
  1842 				    NULL);
  1935 				    NULL);
  1843 				VERIFY(0 == nvlist_lookup_string(origin_nvfs,
  1936 				VERIFY(0 == nvlist_lookup_string(origin_nvfs,
  1881 
  1974 
  1882 			/* check for delete */
  1975 			/* check for delete */
  1883 			if (found == NULL) {
  1976 			if (found == NULL) {
  1884 				char name[ZFS_MAXNAMELEN];
  1977 				char name[ZFS_MAXNAMELEN];
  1885 
  1978 
  1886 				if (!flags.force)
  1979 				if (!flags->force)
  1887 					continue;
  1980 					continue;
  1888 
  1981 
  1889 				(void) snprintf(name, sizeof (name), "%s@%s",
  1982 				(void) snprintf(name, sizeof (name), "%s@%s",
  1890 				    fsname, nvpair_name(snapelem));
  1983 				    fsname, nvpair_name(snapelem));
  1891 
  1984 
  1939 				fromguid = thisguid;
  2032 				fromguid = thisguid;
  1940 		}
  2033 		}
  1941 
  2034 
  1942 		/* check for delete */
  2035 		/* check for delete */
  1943 		if (stream_nvfs == NULL) {
  2036 		if (stream_nvfs == NULL) {
  1944 			if (!flags.force)
  2037 			if (!flags->force)
  1945 				continue;
  2038 				continue;
  1946 
  2039 
  1947 			error = recv_destroy(hdl, fsname, strlen(tofs)+1,
  2040 			error = recv_destroy(hdl, fsname, strlen(tofs)+1,
  1948 			    newname, flags);
  2041 			    newname, flags);
  1949 			if (error)
  2042 			if (error)
  1952 				progress = B_TRUE;
  2045 				progress = B_TRUE;
  1953 			continue;
  2046 			continue;
  1954 		}
  2047 		}
  1955 
  2048 
  1956 		if (fromguid == 0) {
  2049 		if (fromguid == 0) {
  1957 			if (flags.verbose) {
  2050 			if (flags->verbose) {
  1958 				(void) printf("local fs %s does not have "
  2051 				(void) printf("local fs %s does not have "
  1959 				    "fromsnap (%s in stream); must have "
  2052 				    "fromsnap (%s in stream); must have "
  1960 				    "been deleted locally; ignoring\n",
  2053 				    "been deleted locally; ignoring\n",
  1961 				    fsname, fromsnap);
  2054 				    fsname, fromsnap);
  1962 			}
  2055 			}
  1977 		 * datasets beneath it.
  2070 		 * datasets beneath it.
  1978 		 */
  2071 		 */
  1979 		if ((stream_parent_fromsnap_guid != 0 &&
  2072 		if ((stream_parent_fromsnap_guid != 0 &&
  1980 		    parent_fromsnap_guid != 0 &&
  2073 		    parent_fromsnap_guid != 0 &&
  1981 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
  2074 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
  1982 		    ((flags.isprefix || strcmp(tofs, fsname) != 0) &&
  2075 		    ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
  1983 		    (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
  2076 		    (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
  1984 			nvlist_t *parent;
  2077 			nvlist_t *parent;
  1985 			char tryname[ZFS_MAXNAMELEN];
  2078 			char tryname[ZFS_MAXNAMELEN];
  1986 
  2079 
  1987 			parent = fsavl_find(local_avl,
  2080 			parent = fsavl_find(local_avl,
  2000 				    &pname));
  2093 				    &pname));
  2001 				(void) snprintf(tryname, sizeof (tryname),
  2094 				(void) snprintf(tryname, sizeof (tryname),
  2002 				    "%s%s", pname, strrchr(stream_fsname, '/'));
  2095 				    "%s%s", pname, strrchr(stream_fsname, '/'));
  2003 			} else {
  2096 			} else {
  2004 				tryname[0] = '\0';
  2097 				tryname[0] = '\0';
  2005 				if (flags.verbose) {
  2098 				if (flags->verbose) {
  2006 					(void) printf("local fs %s new parent "
  2099 					(void) printf("local fs %s new parent "
  2007 					    "not found\n", fsname);
  2100 					    "not found\n", fsname);
  2008 				}
  2101 				}
  2009 			}
  2102 			}
  2010 
  2103 
  2028 	fsavl_destroy(local_avl);
  2121 	fsavl_destroy(local_avl);
  2029 	nvlist_free(local_nv);
  2122 	nvlist_free(local_nv);
  2030 
  2123 
  2031 	if (needagain && progress) {
  2124 	if (needagain && progress) {
  2032 		/* do another pass to fix up temporary names */
  2125 		/* do another pass to fix up temporary names */
  2033 		if (flags.verbose)
  2126 		if (flags->verbose)
  2034 			(void) printf("another pass:\n");
  2127 			(void) printf("another pass:\n");
  2035 		goto again;
  2128 		goto again;
  2036 	}
  2129 	}
  2037 
  2130 
  2038 	return (needagain);
  2131 	return (needagain);
  2039 }
  2132 }
  2040 
  2133 
  2041 static int
  2134 static int
  2042 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
  2135 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
  2043     recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
  2136     recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
  2044     char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
  2137     char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
  2045 {
  2138 {
  2046 	nvlist_t *stream_nv = NULL;
  2139 	nvlist_t *stream_nv = NULL;
  2047 	avl_tree_t *stream_avl = NULL;
  2140 	avl_tree_t *stream_avl = NULL;
  2048 	char *fromsnap = NULL;
  2141 	char *fromsnap = NULL;
  2067 	/*
  2160 	/*
  2068 	 * Read in the nvlist from the stream.
  2161 	 * Read in the nvlist from the stream.
  2069 	 */
  2162 	 */
  2070 	if (drr->drr_payloadlen != 0) {
  2163 	if (drr->drr_payloadlen != 0) {
  2071 		error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
  2164 		error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
  2072 		    &stream_nv, flags.byteswap, zc);
  2165 		    &stream_nv, flags->byteswap, zc);
  2073 		if (error) {
  2166 		if (error) {
  2074 			error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
  2167 			error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
  2075 			goto out;
  2168 			goto out;
  2076 		}
  2169 		}
  2077 	}
  2170 	}
  2088 
  2181 
  2089 	/*
  2182 	/*
  2090 	 * Read in the end record and verify checksum.
  2183 	 * Read in the end record and verify checksum.
  2091 	 */
  2184 	 */
  2092 	if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
  2185 	if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
  2093 	    flags.byteswap, NULL)))
  2186 	    flags->byteswap, NULL)))
  2094 		goto out;
  2187 		goto out;
  2095 	if (flags.byteswap) {
  2188 	if (flags->byteswap) {
  2096 		drre.drr_type = BSWAP_32(drre.drr_type);
  2189 		drre.drr_type = BSWAP_32(drre.drr_type);
  2097 		drre.drr_u.drr_end.drr_checksum.zc_word[0] =
  2190 		drre.drr_u.drr_end.drr_checksum.zc_word[0] =
  2098 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
  2191 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
  2099 		drre.drr_u.drr_end.drr_checksum.zc_word[1] =
  2192 		drre.drr_u.drr_end.drr_checksum.zc_word[1] =
  2100 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
  2193 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
  2131 		if (fromsnap != NULL) {
  2224 		if (fromsnap != NULL) {
  2132 			nvlist_t *renamed = NULL;
  2225 			nvlist_t *renamed = NULL;
  2133 			nvpair_t *pair = NULL;
  2226 			nvpair_t *pair = NULL;
  2134 
  2227 
  2135 			(void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
  2228 			(void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
  2136 			if (flags.isprefix) {
  2229 			if (flags->isprefix) {
  2137 				struct drr_begin *drrb = &drr->drr_u.drr_begin;
  2230 				struct drr_begin *drrb = &drr->drr_u.drr_begin;
  2138 				int i;
  2231 				int i;
  2139 
  2232 
  2140 				if (flags.istail) {
  2233 				if (flags->istail) {
  2141 					cp = strrchr(drrb->drr_toname, '/');
  2234 					cp = strrchr(drrb->drr_toname, '/');
  2142 					if (cp == NULL) {
  2235 					if (cp == NULL) {
  2143 						(void) strlcat(tofs, "/",
  2236 						(void) strlcat(tofs, "/",
  2144 						    ZFS_MAXNAMELEN);
  2237 						    ZFS_MAXNAMELEN);
  2145 						i = 0;
  2238 						i = 0;
  2153 				(void) strlcat(tofs, &drrb->drr_toname[i],
  2246 				(void) strlcat(tofs, &drrb->drr_toname[i],
  2154 				    ZFS_MAXNAMELEN);
  2247 				    ZFS_MAXNAMELEN);
  2155 				*strchr(tofs, '@') = '\0';
  2248 				*strchr(tofs, '@') = '\0';
  2156 			}
  2249 			}
  2157 
  2250 
  2158 			if (recursive && !flags.dryrun && !flags.nomount) {
  2251 			if (recursive && !flags->dryrun && !flags->nomount) {
  2159 				VERIFY(0 == nvlist_alloc(&renamed,
  2252 				VERIFY(0 == nvlist_alloc(&renamed,
  2160 				    NV_UNIQUE_NAME, 0));
  2253 				    NV_UNIQUE_NAME, 0));
  2161 			}
  2254 			}
  2162 
  2255 
  2163 			softerr = recv_incremental_replication(hdl, tofs, flags,
  2256 			softerr = recv_incremental_replication(hdl, tofs, flags,
  2327 /*
  2420 /*
  2328  * Restores a backup of tosnap from the file descriptor specified by infd.
  2421  * Restores a backup of tosnap from the file descriptor specified by infd.
  2329  */
  2422  */
  2330 static int
  2423 static int
  2331 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
  2424 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
  2332     recvflags_t flags, dmu_replay_record_t *drr,
  2425     recvflags_t *flags, dmu_replay_record_t *drr,
  2333     dmu_replay_record_t *drr_noswap, const char *sendfs,
  2426     dmu_replay_record_t *drr_noswap, const char *sendfs,
  2334     nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
  2427     nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
  2335     uint64_t *action_handlep)
  2428     uint64_t *action_handlep)
  2336 {
  2429 {
  2337 	zfs_cmd_t zc = { 0 };
  2430 	zfs_cmd_t zc = { 0 };
  2369 		    &parent_snapguid);
  2462 		    &parent_snapguid);
  2370 		err = nvlist_lookup_nvlist(fs, "props", &props);
  2463 		err = nvlist_lookup_nvlist(fs, "props", &props);
  2371 		if (err)
  2464 		if (err)
  2372 			VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
  2465 			VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
  2373 
  2466 
  2374 		if (flags.canmountoff) {
  2467 		if (flags->canmountoff) {
  2375 			VERIFY(0 == nvlist_add_uint64(props,
  2468 			VERIFY(0 == nvlist_add_uint64(props,
  2376 			    zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
  2469 			    zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
  2377 		}
  2470 		}
  2378 		ret = zcmd_write_src_nvlist(hdl, &zc, props);
  2471 		ret = zcmd_write_src_nvlist(hdl, &zc, props);
  2379 		if (err)
  2472 		if (err)
  2396 	 * command line, and how much we are going to chop off.
  2489 	 * command line, and how much we are going to chop off.
  2397 	 *
  2490 	 *
  2398 	 * If they specified a snapshot, chop the entire name stored in
  2491 	 * If they specified a snapshot, chop the entire name stored in
  2399 	 * the stream.
  2492 	 * the stream.
  2400 	 */
  2493 	 */
  2401 	if (flags.istail) {
  2494 	if (flags->istail) {
  2402 		/*
  2495 		/*
  2403 		 * A filesystem was specified with -e. We want to tack on only
  2496 		 * A filesystem was specified with -e. We want to tack on only
  2404 		 * the tail of the sent snapshot path.
  2497 		 * the tail of the sent snapshot path.
  2405 		 */
  2498 		 */
  2406 		if (strchr(tosnap, '@')) {
  2499 		if (strchr(tosnap, '@')) {
  2422 			(void) strcpy(&cp[1], drrb->drr_toname);
  2515 			(void) strcpy(&cp[1], drrb->drr_toname);
  2423 			chopprefix = cp;
  2516 			chopprefix = cp;
  2424 		} else {
  2517 		} else {
  2425 			chopprefix = drrb->drr_toname + (chopprefix - sendfs);
  2518 			chopprefix = drrb->drr_toname + (chopprefix - sendfs);
  2426 		}
  2519 		}
  2427 	} else if (flags.isprefix) {
  2520 	} else if (flags->isprefix) {
  2428 		/*
  2521 		/*
  2429 		 * A filesystem was specified with -d. We want to tack on
  2522 		 * A filesystem was specified with -d. We want to tack on
  2430 		 * everything but the first element of the sent snapshot path
  2523 		 * everything but the first element of the sent snapshot path
  2431 		 * (all but the pool name).
  2524 		 * (all but the pool name).
  2432 		 */
  2525 		 */
  2476 
  2569 
  2477 	/*
  2570 	/*
  2478 	 * Determine the name of the origin snapshot, store in zc_string.
  2571 	 * Determine the name of the origin snapshot, store in zc_string.
  2479 	 */
  2572 	 */
  2480 	if (drrb->drr_flags & DRR_FLAG_CLONE) {
  2573 	if (drrb->drr_flags & DRR_FLAG_CLONE) {
  2481 		if (guid_to_name(hdl, tosnap,
  2574 		if (guid_to_name(hdl, zc.zc_value,
  2482 		    drrb->drr_fromguid, zc.zc_string) != 0) {
  2575 		    drrb->drr_fromguid, zc.zc_string) != 0) {
  2483 			zcmd_free_nvlists(&zc);
  2576 			zcmd_free_nvlists(&zc);
  2484 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
  2577 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
  2485 			    "local origin for clone %s does not exist"),
  2578 			    "local origin for clone %s does not exist"),
  2486 			    zc.zc_value);
  2579 			    zc.zc_value);
  2487 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
  2580 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
  2488 		}
  2581 		}
  2489 		if (flags.verbose)
  2582 		if (flags->verbose)
  2490 			(void) printf("found clone origin %s\n", zc.zc_string);
  2583 			(void) printf("found clone origin %s\n", zc.zc_string);
  2491 	}
  2584 	}
  2492 
  2585 
  2493 	stream_wantsnewfs = (drrb->drr_fromguid == NULL ||
  2586 	stream_wantsnewfs = (drrb->drr_fromguid == NULL ||
  2494 	    (drrb->drr_flags & DRR_FLAG_CLONE));
  2587 	    (drrb->drr_flags & DRR_FLAG_CLONE));
  2507 			*cp = '\0';
  2600 			*cp = '\0';
  2508 		if (cp &&
  2601 		if (cp &&
  2509 		    !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
  2602 		    !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
  2510 			char suffix[ZFS_MAXNAMELEN];
  2603 			char suffix[ZFS_MAXNAMELEN];
  2511 			(void) strcpy(suffix, strrchr(zc.zc_value, '/'));
  2604 			(void) strcpy(suffix, strrchr(zc.zc_value, '/'));
  2512 			if (guid_to_name(hdl, tosnap, parent_snapguid,
  2605 			if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
  2513 			    zc.zc_value) == 0) {
  2606 			    zc.zc_value) == 0) {
  2514 				*strchr(zc.zc_value, '@') = '\0';
  2607 				*strchr(zc.zc_value, '@') = '\0';
  2515 				(void) strcat(zc.zc_value, suffix);
  2608 				(void) strcat(zc.zc_value, suffix);
  2516 			}
  2609 			}
  2517 		}
  2610 		}
  2529 		/*
  2622 		/*
  2530 		 * If the exact receive path was specified and this is the
  2623 		 * If the exact receive path was specified and this is the
  2531 		 * topmost path in the stream, then if the fs does not exist we
  2624 		 * topmost path in the stream, then if the fs does not exist we
  2532 		 * should look no further.
  2625 		 * should look no further.
  2533 		 */
  2626 		 */
  2534 		if ((flags.isprefix || (*(chopprefix = drrb->drr_toname +
  2627 		if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
  2535 		    strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
  2628 		    strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
  2536 		    !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
  2629 		    !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
  2537 			char snap[ZFS_MAXNAMELEN];
  2630 			char snap[ZFS_MAXNAMELEN];
  2538 			(void) strcpy(snap, strchr(zc.zc_value, '@'));
  2631 			(void) strcpy(snap, strchr(zc.zc_value, '@'));
  2539 			if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
  2632 			if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
  2540 			    zc.zc_value) == 0) {
  2633 			    zc.zc_value) == 0) {
  2541 				*strchr(zc.zc_value, '@') = '\0';
  2634 				*strchr(zc.zc_value, '@') = '\0';
  2542 				(void) strcat(zc.zc_value, snap);
  2635 				(void) strcat(zc.zc_value, snap);
  2543 			}
  2636 			}
  2544 		}
  2637 		}
  2556 		 * (full stream or clone) and they want us to blow it
  2649 		 * (full stream or clone) and they want us to blow it
  2557 		 * away (and have therefore specified -F and removed any
  2650 		 * away (and have therefore specified -F and removed any
  2558 		 * snapshots).
  2651 		 * snapshots).
  2559 		 */
  2652 		 */
  2560 		if (stream_wantsnewfs) {
  2653 		if (stream_wantsnewfs) {
  2561 			if (!flags.force) {
  2654 			if (!flags->force) {
  2562 				zcmd_free_nvlists(&zc);
  2655 				zcmd_free_nvlists(&zc);
  2563 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
  2656 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
  2564 				    "destination '%s' exists\n"
  2657 				    "destination '%s' exists\n"
  2565 				    "must specify -F to overwrite it"),
  2658 				    "must specify -F to overwrite it"),
  2566 				    zc.zc_name);
  2659 				    zc.zc_name);
  2592 			    "must destroy it to overwrite it"),
  2685 			    "must destroy it to overwrite it"),
  2593 			    zc.zc_name);
  2686 			    zc.zc_name);
  2594 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
  2687 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
  2595 		}
  2688 		}
  2596 
  2689 
  2597 		if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
  2690 		if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
  2598 		    stream_wantsnewfs) {
  2691 		    stream_wantsnewfs) {
  2599 			/* We can't do online recv in this case */
  2692 			/* We can't do online recv in this case */
  2600 			clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
  2693 			clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
  2601 			if (clp == NULL) {
  2694 			if (clp == NULL) {
  2602 				zfs_close(zhp);
  2695 				zfs_close(zhp);
  2631 		 * Trim off the final dataset component so we perform the
  2724 		 * Trim off the final dataset component so we perform the
  2632 		 * recvbackup ioctl to the filesystems's parent.
  2725 		 * recvbackup ioctl to the filesystems's parent.
  2633 		 */
  2726 		 */
  2634 		*cp = '\0';
  2727 		*cp = '\0';
  2635 
  2728 
  2636 		if (flags.isprefix && !flags.istail && !flags.dryrun &&
  2729 		if (flags->isprefix && !flags->istail && !flags->dryrun &&
  2637 		    create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
  2730 		    create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
  2638 			zcmd_free_nvlists(&zc);
  2731 			zcmd_free_nvlists(&zc);
  2639 			return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
  2732 			return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
  2640 		}
  2733 		}
  2641 
  2734 
  2642 		newfs = B_TRUE;
  2735 		newfs = B_TRUE;
  2643 	}
  2736 	}
  2644 
  2737 
  2645 	zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
  2738 	zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
  2646 	zc.zc_cookie = infd;
  2739 	zc.zc_cookie = infd;
  2647 	zc.zc_guid = flags.force;
  2740 	zc.zc_guid = flags->force;
  2648 	if (flags.verbose) {
  2741 	if (flags->verbose) {
  2649 		(void) printf("%s %s stream of %s into %s\n",
  2742 		(void) printf("%s %s stream of %s into %s\n",
  2650 		    flags.dryrun ? "would receive" : "receiving",
  2743 		    flags->dryrun ? "would receive" : "receiving",
  2651 		    drrb->drr_fromguid ? "incremental" : "full",
  2744 		    drrb->drr_fromguid ? "incremental" : "full",
  2652 		    drrb->drr_toname, zc.zc_value);
  2745 		    drrb->drr_toname, zc.zc_value);
  2653 		(void) fflush(stdout);
  2746 		(void) fflush(stdout);
  2654 	}
  2747 	}
  2655 
  2748 
  2656 	if (flags.dryrun) {
  2749 	if (flags->dryrun) {
  2657 		zcmd_free_nvlists(&zc);
  2750 		zcmd_free_nvlists(&zc);
  2658 		return (recv_skip(hdl, infd, flags.byteswap));
  2751 		return (recv_skip(hdl, infd, flags->byteswap));
  2659 	}
  2752 	}
  2660 
  2753 
  2661 	zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
  2754 	zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
  2662 	zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
  2755 	zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
  2663 	zc.zc_cleanup_fd = cleanup_fd;
  2756 	zc.zc_cleanup_fd = cleanup_fd;
  2734 			fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
  2827 			fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
  2735 			fsavl_destroy(local_avl);
  2828 			fsavl_destroy(local_avl);
  2736 			nvlist_free(local_nv);
  2829 			nvlist_free(local_nv);
  2737 
  2830 
  2738 			if (fs != NULL) {
  2831 			if (fs != NULL) {
  2739 				if (flags.verbose) {
  2832 				if (flags->verbose) {
  2740 					(void) printf("snap %s already exists; "
  2833 					(void) printf("snap %s already exists; "
  2741 					    "ignoring\n", zc.zc_value);
  2834 					    "ignoring\n", zc.zc_value);
  2742 				}
  2835 				}
  2743 				err = ioctl_err = recv_skip(hdl, infd,
  2836 				err = ioctl_err = recv_skip(hdl, infd,
  2744 				    flags.byteswap);
  2837 				    flags->byteswap);
  2745 			}
  2838 			}
  2746 		}
  2839 		}
  2747 		*cp = '@';
  2840 		*cp = '@';
  2748 	}
  2841 	}
  2749 
  2842 
  2791 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
  2884 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
  2792 			break;
  2885 			break;
  2793 		case EDQUOT:
  2886 		case EDQUOT:
  2794 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
  2887 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
  2795 			    "destination %s space quota exceeded"), zc.zc_name);
  2888 			    "destination %s space quota exceeded"), zc.zc_name);
  2796 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
  2889 			(void) zfs_error(hdl, EZFS_NOSPC, errbuf);
  2797 			break;
  2890 			break;
  2798 		default:
  2891 		default:
  2799 			(void) zfs_standard_error(hdl, ioctl_errno, errbuf);
  2892 			(void) zfs_standard_error(hdl, ioctl_errno, errbuf);
  2800 		}
  2893 		}
  2801 	}
  2894 	}
  2849 	if (err || ioctl_err)
  2942 	if (err || ioctl_err)
  2850 		return (-1);
  2943 		return (-1);
  2851 
  2944 
  2852 	*action_handlep = zc.zc_action_handle;
  2945 	*action_handlep = zc.zc_action_handle;
  2853 
  2946 
  2854 	if (flags.verbose) {
  2947 	if (flags->verbose) {
  2855 		char buf1[64];
  2948 		char buf1[64];
  2856 		char buf2[64];
  2949 		char buf2[64];
  2857 		uint64_t bytes = zc.zc_cookie;
  2950 		uint64_t bytes = zc.zc_cookie;
  2858 		time_t delta = time(NULL) - begin_time;
  2951 		time_t delta = time(NULL) - begin_time;
  2859 		if (delta == 0)
  2952 		if (delta == 0)
  2867 
  2960 
  2868 	return (0);
  2961 	return (0);
  2869 }
  2962 }
  2870 
  2963 
  2871 static int
  2964 static int
  2872 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
  2965 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
  2873     int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
  2966     int infd, const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
  2874     char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
  2967     char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
  2875 {
  2968 {
  2876 	int err;
  2969 	int err;
  2877 	dmu_replay_record_t drr, drr_noswap;
  2970 	dmu_replay_record_t drr, drr_noswap;
  2882 	int hdrtype;
  2975 	int hdrtype;
  2883 
  2976 
  2884 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
  2977 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
  2885 	    "cannot receive"));
  2978 	    "cannot receive"));
  2886 
  2979 
  2887 	if (flags.isprefix &&
  2980 	if (flags->isprefix &&
  2888 	    !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
  2981 	    !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
  2889 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
  2982 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
  2890 		    "(%s) does not exist"), tosnap);
  2983 		    "(%s) does not exist"), tosnap);
  2891 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
  2984 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
  2892 	}
  2985 	}
  2902 	}
  2995 	}
  2903 
  2996 
  2904 	/* the kernel needs the non-byteswapped begin record */
  2997 	/* the kernel needs the non-byteswapped begin record */
  2905 	drr_noswap = drr;
  2998 	drr_noswap = drr;
  2906 
  2999 
  2907 	flags.byteswap = B_FALSE;
  3000 	flags->byteswap = B_FALSE;
  2908 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
  3001 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
  2909 		/*
  3002 		/*
  2910 		 * We computed the checksum in the wrong byteorder in
  3003 		 * We computed the checksum in the wrong byteorder in
  2911 		 * recv_read() above; do it again correctly.
  3004 		 * recv_read() above; do it again correctly.
  2912 		 */
  3005 		 */
  2913 		bzero(&zcksum, sizeof (zio_cksum_t));
  3006 		bzero(&zcksum, sizeof (zio_cksum_t));
  2914 		fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
  3007 		fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
  2915 		flags.byteswap = B_TRUE;
  3008 		flags->byteswap = B_TRUE;
  2916 
  3009 
  2917 		drr.drr_type = BSWAP_32(drr.drr_type);
  3010 		drr.drr_type = BSWAP_32(drr.drr_type);
  2918 		drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
  3011 		drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
  2919 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
  3012 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
  2920 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
  3013 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
  2978  * Return 0 on total success, -2 if some things couldn't be
  3071  * Return 0 on total success, -2 if some things couldn't be
  2979  * destroyed/renamed/promoted, -1 if some things couldn't be received.
  3072  * destroyed/renamed/promoted, -1 if some things couldn't be received.
  2980  * (-1 will override -2).
  3073  * (-1 will override -2).
  2981  */
  3074  */
  2982 int
  3075 int
  2983 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
  3076 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
  2984     int infd, avl_tree_t *stream_avl)
  3077     int infd, avl_tree_t *stream_avl)
  2985 {
  3078 {
  2986 	char *top_zfs = NULL;
  3079 	char *top_zfs = NULL;
  2987 	int err;
  3080 	int err;
  2988 	int cleanup_fd;
  3081 	int cleanup_fd;
  2994 	err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
  3087 	err = zfs_receive_impl(hdl, tosnap, flags, infd, NULL, NULL,
  2995 	    stream_avl, &top_zfs, cleanup_fd, &action_handle);
  3088 	    stream_avl, &top_zfs, cleanup_fd, &action_handle);
  2996 
  3089 
  2997 	VERIFY(0 == close(cleanup_fd));
  3090 	VERIFY(0 == close(cleanup_fd));
  2998 
  3091 
  2999 	if (err == 0 && !flags.nomount && top_zfs) {
  3092 	if (err == 0 && !flags->nomount && top_zfs) {
  3000 		zfs_handle_t *zhp;
  3093 		zfs_handle_t *zhp;
  3001 		prop_changelist_t *clp;
  3094 		prop_changelist_t *clp;
  3002 
  3095 
  3003 		zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
  3096 		zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
  3004 		if (zhp != NULL) {
  3097 		if (zhp != NULL) {