xref: /freebsd-src/sys/contrib/openzfs/lib/libzfs/libzfs_sendrecv.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1*eda14cbcSMatt Macy /*
2*eda14cbcSMatt Macy  * CDDL HEADER START
3*eda14cbcSMatt Macy  *
4*eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5*eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6*eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7*eda14cbcSMatt Macy  *
8*eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*eda14cbcSMatt Macy  * or http://www.opensolaris.org/os/licensing.
10*eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11*eda14cbcSMatt Macy  * and limitations under the License.
12*eda14cbcSMatt Macy  *
13*eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14*eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16*eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17*eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18*eda14cbcSMatt Macy  *
19*eda14cbcSMatt Macy  * CDDL HEADER END
20*eda14cbcSMatt Macy  */
21*eda14cbcSMatt Macy 
22*eda14cbcSMatt Macy /*
23*eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24*eda14cbcSMatt Macy  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25*eda14cbcSMatt Macy  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26*eda14cbcSMatt Macy  * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27*eda14cbcSMatt Macy  * All rights reserved
28*eda14cbcSMatt Macy  * Copyright (c) 2013 Steven Hartland. All rights reserved.
29*eda14cbcSMatt Macy  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
30*eda14cbcSMatt Macy  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
31*eda14cbcSMatt Macy  * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
32*eda14cbcSMatt Macy  * Copyright (c) 2019 Datto Inc.
33*eda14cbcSMatt Macy  */
34*eda14cbcSMatt Macy 
35*eda14cbcSMatt Macy #include <assert.h>
36*eda14cbcSMatt Macy #include <ctype.h>
37*eda14cbcSMatt Macy #include <errno.h>
38*eda14cbcSMatt Macy #include <libintl.h>
39*eda14cbcSMatt Macy #include <stdio.h>
40*eda14cbcSMatt Macy #include <stdlib.h>
41*eda14cbcSMatt Macy #include <strings.h>
42*eda14cbcSMatt Macy #include <unistd.h>
43*eda14cbcSMatt Macy #include <stddef.h>
44*eda14cbcSMatt Macy #include <fcntl.h>
45*eda14cbcSMatt Macy #include <sys/mount.h>
46*eda14cbcSMatt Macy #include <sys/mntent.h>
47*eda14cbcSMatt Macy #include <sys/mnttab.h>
48*eda14cbcSMatt Macy #include <sys/avl.h>
49*eda14cbcSMatt Macy #include <sys/debug.h>
50*eda14cbcSMatt Macy #include <sys/stat.h>
51*eda14cbcSMatt Macy #include <stddef.h>
52*eda14cbcSMatt Macy #include <pthread.h>
53*eda14cbcSMatt Macy #include <umem.h>
54*eda14cbcSMatt Macy #include <time.h>
55*eda14cbcSMatt Macy 
56*eda14cbcSMatt Macy #include <libzfs.h>
57*eda14cbcSMatt Macy #include <libzfs_core.h>
58*eda14cbcSMatt Macy #include <libzutil.h>
59*eda14cbcSMatt Macy 
60*eda14cbcSMatt Macy #include "zfs_namecheck.h"
61*eda14cbcSMatt Macy #include "zfs_prop.h"
62*eda14cbcSMatt Macy #include "zfs_fletcher.h"
63*eda14cbcSMatt Macy #include "libzfs_impl.h"
64*eda14cbcSMatt Macy #include <cityhash.h>
65*eda14cbcSMatt Macy #include <zlib.h>
66*eda14cbcSMatt Macy #include <sys/zio_checksum.h>
67*eda14cbcSMatt Macy #include <sys/dsl_crypt.h>
68*eda14cbcSMatt Macy #include <sys/ddt.h>
69*eda14cbcSMatt Macy #include <sys/socket.h>
70*eda14cbcSMatt Macy #include <sys/sha2.h>
71*eda14cbcSMatt Macy 
72*eda14cbcSMatt Macy static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
73*eda14cbcSMatt Macy     recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **,
74*eda14cbcSMatt Macy     const char *, nvlist_t *);
75*eda14cbcSMatt Macy static int guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
76*eda14cbcSMatt Macy     uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
77*eda14cbcSMatt Macy     uint64_t num_redact_snaps, char *name);
78*eda14cbcSMatt Macy static int guid_to_name(libzfs_handle_t *, const char *,
79*eda14cbcSMatt Macy     uint64_t, boolean_t, char *);
80*eda14cbcSMatt Macy 
81*eda14cbcSMatt Macy typedef struct progress_arg {
82*eda14cbcSMatt Macy 	zfs_handle_t *pa_zhp;
83*eda14cbcSMatt Macy 	int pa_fd;
84*eda14cbcSMatt Macy 	boolean_t pa_parsable;
85*eda14cbcSMatt Macy 	boolean_t pa_estimate;
86*eda14cbcSMatt Macy 	int pa_verbosity;
87*eda14cbcSMatt Macy } progress_arg_t;
88*eda14cbcSMatt Macy 
89*eda14cbcSMatt Macy static int
90*eda14cbcSMatt Macy dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
91*eda14cbcSMatt Macy     zio_cksum_t *zc, int outfd)
92*eda14cbcSMatt Macy {
93*eda14cbcSMatt Macy 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
94*eda14cbcSMatt Macy 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
95*eda14cbcSMatt Macy 	fletcher_4_incremental_native(drr,
96*eda14cbcSMatt Macy 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
97*eda14cbcSMatt Macy 	if (drr->drr_type != DRR_BEGIN) {
98*eda14cbcSMatt Macy 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
99*eda14cbcSMatt Macy 		    drr_checksum.drr_checksum));
100*eda14cbcSMatt Macy 		drr->drr_u.drr_checksum.drr_checksum = *zc;
101*eda14cbcSMatt Macy 	}
102*eda14cbcSMatt Macy 	fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
103*eda14cbcSMatt Macy 	    sizeof (zio_cksum_t), zc);
104*eda14cbcSMatt Macy 	if (write(outfd, drr, sizeof (*drr)) == -1)
105*eda14cbcSMatt Macy 		return (errno);
106*eda14cbcSMatt Macy 	if (payload_len != 0) {
107*eda14cbcSMatt Macy 		fletcher_4_incremental_native(payload, payload_len, zc);
108*eda14cbcSMatt Macy 		if (write(outfd, payload, payload_len) == -1)
109*eda14cbcSMatt Macy 			return (errno);
110*eda14cbcSMatt Macy 	}
111*eda14cbcSMatt Macy 	return (0);
112*eda14cbcSMatt Macy }
113*eda14cbcSMatt Macy 
114*eda14cbcSMatt Macy /*
115*eda14cbcSMatt Macy  * Routines for dealing with the AVL tree of fs-nvlists
116*eda14cbcSMatt Macy  */
117*eda14cbcSMatt Macy typedef struct fsavl_node {
118*eda14cbcSMatt Macy 	avl_node_t fn_node;
119*eda14cbcSMatt Macy 	nvlist_t *fn_nvfs;
120*eda14cbcSMatt Macy 	char *fn_snapname;
121*eda14cbcSMatt Macy 	uint64_t fn_guid;
122*eda14cbcSMatt Macy } fsavl_node_t;
123*eda14cbcSMatt Macy 
124*eda14cbcSMatt Macy static int
125*eda14cbcSMatt Macy fsavl_compare(const void *arg1, const void *arg2)
126*eda14cbcSMatt Macy {
127*eda14cbcSMatt Macy 	const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
128*eda14cbcSMatt Macy 	const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
129*eda14cbcSMatt Macy 
130*eda14cbcSMatt Macy 	return (TREE_CMP(fn1->fn_guid, fn2->fn_guid));
131*eda14cbcSMatt Macy }
132*eda14cbcSMatt Macy 
133*eda14cbcSMatt Macy /*
134*eda14cbcSMatt Macy  * Given the GUID of a snapshot, find its containing filesystem and
135*eda14cbcSMatt Macy  * (optionally) name.
136*eda14cbcSMatt Macy  */
137*eda14cbcSMatt Macy static nvlist_t *
138*eda14cbcSMatt Macy fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
139*eda14cbcSMatt Macy {
140*eda14cbcSMatt Macy 	fsavl_node_t fn_find;
141*eda14cbcSMatt Macy 	fsavl_node_t *fn;
142*eda14cbcSMatt Macy 
143*eda14cbcSMatt Macy 	fn_find.fn_guid = snapguid;
144*eda14cbcSMatt Macy 
145*eda14cbcSMatt Macy 	fn = avl_find(avl, &fn_find, NULL);
146*eda14cbcSMatt Macy 	if (fn) {
147*eda14cbcSMatt Macy 		if (snapname)
148*eda14cbcSMatt Macy 			*snapname = fn->fn_snapname;
149*eda14cbcSMatt Macy 		return (fn->fn_nvfs);
150*eda14cbcSMatt Macy 	}
151*eda14cbcSMatt Macy 	return (NULL);
152*eda14cbcSMatt Macy }
153*eda14cbcSMatt Macy 
154*eda14cbcSMatt Macy static void
155*eda14cbcSMatt Macy fsavl_destroy(avl_tree_t *avl)
156*eda14cbcSMatt Macy {
157*eda14cbcSMatt Macy 	fsavl_node_t *fn;
158*eda14cbcSMatt Macy 	void *cookie;
159*eda14cbcSMatt Macy 
160*eda14cbcSMatt Macy 	if (avl == NULL)
161*eda14cbcSMatt Macy 		return;
162*eda14cbcSMatt Macy 
163*eda14cbcSMatt Macy 	cookie = NULL;
164*eda14cbcSMatt Macy 	while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
165*eda14cbcSMatt Macy 		free(fn);
166*eda14cbcSMatt Macy 	avl_destroy(avl);
167*eda14cbcSMatt Macy 	free(avl);
168*eda14cbcSMatt Macy }
169*eda14cbcSMatt Macy 
170*eda14cbcSMatt Macy /*
171*eda14cbcSMatt Macy  * Given an nvlist, produce an avl tree of snapshots, ordered by guid
172*eda14cbcSMatt Macy  */
173*eda14cbcSMatt Macy static avl_tree_t *
174*eda14cbcSMatt Macy fsavl_create(nvlist_t *fss)
175*eda14cbcSMatt Macy {
176*eda14cbcSMatt Macy 	avl_tree_t *fsavl;
177*eda14cbcSMatt Macy 	nvpair_t *fselem = NULL;
178*eda14cbcSMatt Macy 
179*eda14cbcSMatt Macy 	if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
180*eda14cbcSMatt Macy 		return (NULL);
181*eda14cbcSMatt Macy 
182*eda14cbcSMatt Macy 	avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
183*eda14cbcSMatt Macy 	    offsetof(fsavl_node_t, fn_node));
184*eda14cbcSMatt Macy 
185*eda14cbcSMatt Macy 	while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
186*eda14cbcSMatt Macy 		nvlist_t *nvfs, *snaps;
187*eda14cbcSMatt Macy 		nvpair_t *snapelem = NULL;
188*eda14cbcSMatt Macy 
189*eda14cbcSMatt Macy 		VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
190*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
191*eda14cbcSMatt Macy 
192*eda14cbcSMatt Macy 		while ((snapelem =
193*eda14cbcSMatt Macy 		    nvlist_next_nvpair(snaps, snapelem)) != NULL) {
194*eda14cbcSMatt Macy 			fsavl_node_t *fn;
195*eda14cbcSMatt Macy 			uint64_t guid;
196*eda14cbcSMatt Macy 
197*eda14cbcSMatt Macy 			VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
198*eda14cbcSMatt Macy 			if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
199*eda14cbcSMatt Macy 				fsavl_destroy(fsavl);
200*eda14cbcSMatt Macy 				return (NULL);
201*eda14cbcSMatt Macy 			}
202*eda14cbcSMatt Macy 			fn->fn_nvfs = nvfs;
203*eda14cbcSMatt Macy 			fn->fn_snapname = nvpair_name(snapelem);
204*eda14cbcSMatt Macy 			fn->fn_guid = guid;
205*eda14cbcSMatt Macy 
206*eda14cbcSMatt Macy 			/*
207*eda14cbcSMatt Macy 			 * Note: if there are multiple snaps with the
208*eda14cbcSMatt Macy 			 * same GUID, we ignore all but one.
209*eda14cbcSMatt Macy 			 */
210*eda14cbcSMatt Macy 			if (avl_find(fsavl, fn, NULL) == NULL)
211*eda14cbcSMatt Macy 				avl_add(fsavl, fn);
212*eda14cbcSMatt Macy 			else
213*eda14cbcSMatt Macy 				free(fn);
214*eda14cbcSMatt Macy 		}
215*eda14cbcSMatt Macy 	}
216*eda14cbcSMatt Macy 
217*eda14cbcSMatt Macy 	return (fsavl);
218*eda14cbcSMatt Macy }
219*eda14cbcSMatt Macy 
220*eda14cbcSMatt Macy /*
221*eda14cbcSMatt Macy  * Routines for dealing with the giant nvlist of fs-nvlists, etc.
222*eda14cbcSMatt Macy  */
223*eda14cbcSMatt Macy typedef struct send_data {
224*eda14cbcSMatt Macy 	/*
225*eda14cbcSMatt Macy 	 * assigned inside every recursive call,
226*eda14cbcSMatt Macy 	 * restored from *_save on return:
227*eda14cbcSMatt Macy 	 *
228*eda14cbcSMatt Macy 	 * guid of fromsnap snapshot in parent dataset
229*eda14cbcSMatt Macy 	 * txg of fromsnap snapshot in current dataset
230*eda14cbcSMatt Macy 	 * txg of tosnap snapshot in current dataset
231*eda14cbcSMatt Macy 	 */
232*eda14cbcSMatt Macy 
233*eda14cbcSMatt Macy 	uint64_t parent_fromsnap_guid;
234*eda14cbcSMatt Macy 	uint64_t fromsnap_txg;
235*eda14cbcSMatt Macy 	uint64_t tosnap_txg;
236*eda14cbcSMatt Macy 
237*eda14cbcSMatt Macy 	/* the nvlists get accumulated during depth-first traversal */
238*eda14cbcSMatt Macy 	nvlist_t *parent_snaps;
239*eda14cbcSMatt Macy 	nvlist_t *fss;
240*eda14cbcSMatt Macy 	nvlist_t *snapprops;
241*eda14cbcSMatt Macy 	nvlist_t *snapholds;	/* user holds */
242*eda14cbcSMatt Macy 
243*eda14cbcSMatt Macy 	/* send-receive configuration, does not change during traversal */
244*eda14cbcSMatt Macy 	const char *fsname;
245*eda14cbcSMatt Macy 	const char *fromsnap;
246*eda14cbcSMatt Macy 	const char *tosnap;
247*eda14cbcSMatt Macy 	boolean_t recursive;
248*eda14cbcSMatt Macy 	boolean_t raw;
249*eda14cbcSMatt Macy 	boolean_t doall;
250*eda14cbcSMatt Macy 	boolean_t replicate;
251*eda14cbcSMatt Macy 	boolean_t verbose;
252*eda14cbcSMatt Macy 	boolean_t backup;
253*eda14cbcSMatt Macy 	boolean_t seenfrom;
254*eda14cbcSMatt Macy 	boolean_t seento;
255*eda14cbcSMatt Macy 	boolean_t holds;	/* were holds requested with send -h */
256*eda14cbcSMatt Macy 	boolean_t props;
257*eda14cbcSMatt Macy 
258*eda14cbcSMatt Macy 	/*
259*eda14cbcSMatt Macy 	 * The header nvlist is of the following format:
260*eda14cbcSMatt Macy 	 * {
261*eda14cbcSMatt Macy 	 *   "tosnap" -> string
262*eda14cbcSMatt Macy 	 *   "fromsnap" -> string (if incremental)
263*eda14cbcSMatt Macy 	 *   "fss" -> {
264*eda14cbcSMatt Macy 	 *	id -> {
265*eda14cbcSMatt Macy 	 *
266*eda14cbcSMatt Macy 	 *	 "name" -> string (full name; for debugging)
267*eda14cbcSMatt Macy 	 *	 "parentfromsnap" -> number (guid of fromsnap in parent)
268*eda14cbcSMatt Macy 	 *
269*eda14cbcSMatt Macy 	 *	 "props" -> { name -> value (only if set here) }
270*eda14cbcSMatt Macy 	 *	 "snaps" -> { name (lastname) -> number (guid) }
271*eda14cbcSMatt Macy 	 *	 "snapprops" -> { name (lastname) -> { name -> value } }
272*eda14cbcSMatt Macy 	 *	 "snapholds" -> { name (lastname) -> { holdname -> crtime } }
273*eda14cbcSMatt Macy 	 *
274*eda14cbcSMatt Macy 	 *	 "origin" -> number (guid) (if clone)
275*eda14cbcSMatt Macy 	 *	 "is_encroot" -> boolean
276*eda14cbcSMatt Macy 	 *	 "sent" -> boolean (not on-disk)
277*eda14cbcSMatt Macy 	 *	}
278*eda14cbcSMatt Macy 	 *   }
279*eda14cbcSMatt Macy 	 * }
280*eda14cbcSMatt Macy 	 *
281*eda14cbcSMatt Macy 	 */
282*eda14cbcSMatt Macy } send_data_t;
283*eda14cbcSMatt Macy 
284*eda14cbcSMatt Macy static void
285*eda14cbcSMatt Macy send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
286*eda14cbcSMatt Macy 
287*eda14cbcSMatt Macy static int
288*eda14cbcSMatt Macy send_iterate_snap(zfs_handle_t *zhp, void *arg)
289*eda14cbcSMatt Macy {
290*eda14cbcSMatt Macy 	send_data_t *sd = arg;
291*eda14cbcSMatt Macy 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
292*eda14cbcSMatt Macy 	uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
293*eda14cbcSMatt Macy 	char *snapname;
294*eda14cbcSMatt Macy 	nvlist_t *nv;
295*eda14cbcSMatt Macy 	boolean_t isfromsnap, istosnap, istosnapwithnofrom;
296*eda14cbcSMatt Macy 
297*eda14cbcSMatt Macy 	snapname = strrchr(zhp->zfs_name, '@')+1;
298*eda14cbcSMatt Macy 	isfromsnap = (sd->fromsnap != NULL &&
299*eda14cbcSMatt Macy 	    strcmp(sd->fromsnap, snapname) == 0);
300*eda14cbcSMatt Macy 	istosnap = (sd->tosnap != NULL && (strcmp(sd->tosnap, snapname) == 0));
301*eda14cbcSMatt Macy 	istosnapwithnofrom = (istosnap && sd->fromsnap == NULL);
302*eda14cbcSMatt Macy 
303*eda14cbcSMatt Macy 	if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
304*eda14cbcSMatt Macy 		if (sd->verbose) {
305*eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
306*eda14cbcSMatt Macy 			    "skipping snapshot %s because it was created "
307*eda14cbcSMatt Macy 			    "after the destination snapshot (%s)\n"),
308*eda14cbcSMatt Macy 			    zhp->zfs_name, sd->tosnap);
309*eda14cbcSMatt Macy 		}
310*eda14cbcSMatt Macy 		zfs_close(zhp);
311*eda14cbcSMatt Macy 		return (0);
312*eda14cbcSMatt Macy 	}
313*eda14cbcSMatt Macy 
314*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
315*eda14cbcSMatt Macy 	/*
316*eda14cbcSMatt Macy 	 * NB: if there is no fromsnap here (it's a newly created fs in
317*eda14cbcSMatt Macy 	 * an incremental replication), we will substitute the tosnap.
318*eda14cbcSMatt Macy 	 */
319*eda14cbcSMatt Macy 	if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap)) {
320*eda14cbcSMatt Macy 		sd->parent_fromsnap_guid = guid;
321*eda14cbcSMatt Macy 	}
322*eda14cbcSMatt Macy 
323*eda14cbcSMatt Macy 	if (!sd->recursive) {
324*eda14cbcSMatt Macy 		if (!sd->seenfrom && isfromsnap) {
325*eda14cbcSMatt Macy 			sd->seenfrom = B_TRUE;
326*eda14cbcSMatt Macy 			zfs_close(zhp);
327*eda14cbcSMatt Macy 			return (0);
328*eda14cbcSMatt Macy 		}
329*eda14cbcSMatt Macy 
330*eda14cbcSMatt Macy 		if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) {
331*eda14cbcSMatt Macy 			zfs_close(zhp);
332*eda14cbcSMatt Macy 			return (0);
333*eda14cbcSMatt Macy 		}
334*eda14cbcSMatt Macy 
335*eda14cbcSMatt Macy 		if (istosnap)
336*eda14cbcSMatt Macy 			sd->seento = B_TRUE;
337*eda14cbcSMatt Macy 	}
338*eda14cbcSMatt Macy 
339*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
340*eda14cbcSMatt Macy 	send_iterate_prop(zhp, sd->backup, nv);
341*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
342*eda14cbcSMatt Macy 	nvlist_free(nv);
343*eda14cbcSMatt Macy 	if (sd->holds) {
344*eda14cbcSMatt Macy 		nvlist_t *holds = fnvlist_alloc();
345*eda14cbcSMatt Macy 		int err = lzc_get_holds(zhp->zfs_name, &holds);
346*eda14cbcSMatt Macy 		if (err == 0) {
347*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_add_nvlist(sd->snapholds,
348*eda14cbcSMatt Macy 			    snapname, holds));
349*eda14cbcSMatt Macy 		}
350*eda14cbcSMatt Macy 		fnvlist_free(holds);
351*eda14cbcSMatt Macy 	}
352*eda14cbcSMatt Macy 
353*eda14cbcSMatt Macy 	zfs_close(zhp);
354*eda14cbcSMatt Macy 	return (0);
355*eda14cbcSMatt Macy }
356*eda14cbcSMatt Macy 
357*eda14cbcSMatt Macy static void
358*eda14cbcSMatt Macy send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
359*eda14cbcSMatt Macy {
360*eda14cbcSMatt Macy 	nvlist_t *props = NULL;
361*eda14cbcSMatt Macy 	nvpair_t *elem = NULL;
362*eda14cbcSMatt Macy 
363*eda14cbcSMatt Macy 	if (received_only)
364*eda14cbcSMatt Macy 		props = zfs_get_recvd_props(zhp);
365*eda14cbcSMatt Macy 	else
366*eda14cbcSMatt Macy 		props = zhp->zfs_props;
367*eda14cbcSMatt Macy 
368*eda14cbcSMatt Macy 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
369*eda14cbcSMatt Macy 		char *propname = nvpair_name(elem);
370*eda14cbcSMatt Macy 		zfs_prop_t prop = zfs_name_to_prop(propname);
371*eda14cbcSMatt Macy 		nvlist_t *propnv;
372*eda14cbcSMatt Macy 
373*eda14cbcSMatt Macy 		if (!zfs_prop_user(propname)) {
374*eda14cbcSMatt Macy 			/*
375*eda14cbcSMatt Macy 			 * Realistically, this should never happen.  However,
376*eda14cbcSMatt Macy 			 * we want the ability to add DSL properties without
377*eda14cbcSMatt Macy 			 * needing to make incompatible version changes.  We
378*eda14cbcSMatt Macy 			 * need to ignore unknown properties to allow older
379*eda14cbcSMatt Macy 			 * software to still send datasets containing these
380*eda14cbcSMatt Macy 			 * properties, with the unknown properties elided.
381*eda14cbcSMatt Macy 			 */
382*eda14cbcSMatt Macy 			if (prop == ZPROP_INVAL)
383*eda14cbcSMatt Macy 				continue;
384*eda14cbcSMatt Macy 
385*eda14cbcSMatt Macy 			if (zfs_prop_readonly(prop))
386*eda14cbcSMatt Macy 				continue;
387*eda14cbcSMatt Macy 		}
388*eda14cbcSMatt Macy 
389*eda14cbcSMatt Macy 		verify(nvpair_value_nvlist(elem, &propnv) == 0);
390*eda14cbcSMatt Macy 		if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
391*eda14cbcSMatt Macy 		    prop == ZFS_PROP_REFQUOTA ||
392*eda14cbcSMatt Macy 		    prop == ZFS_PROP_REFRESERVATION) {
393*eda14cbcSMatt Macy 			char *source;
394*eda14cbcSMatt Macy 			uint64_t value;
395*eda14cbcSMatt Macy 			verify(nvlist_lookup_uint64(propnv,
396*eda14cbcSMatt Macy 			    ZPROP_VALUE, &value) == 0);
397*eda14cbcSMatt Macy 			if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
398*eda14cbcSMatt Macy 				continue;
399*eda14cbcSMatt Macy 			/*
400*eda14cbcSMatt Macy 			 * May have no source before SPA_VERSION_RECVD_PROPS,
401*eda14cbcSMatt Macy 			 * but is still modifiable.
402*eda14cbcSMatt Macy 			 */
403*eda14cbcSMatt Macy 			if (nvlist_lookup_string(propnv,
404*eda14cbcSMatt Macy 			    ZPROP_SOURCE, &source) == 0) {
405*eda14cbcSMatt Macy 				if ((strcmp(source, zhp->zfs_name) != 0) &&
406*eda14cbcSMatt Macy 				    (strcmp(source,
407*eda14cbcSMatt Macy 				    ZPROP_SOURCE_VAL_RECVD) != 0))
408*eda14cbcSMatt Macy 					continue;
409*eda14cbcSMatt Macy 			}
410*eda14cbcSMatt Macy 		} else {
411*eda14cbcSMatt Macy 			char *source;
412*eda14cbcSMatt Macy 			if (nvlist_lookup_string(propnv,
413*eda14cbcSMatt Macy 			    ZPROP_SOURCE, &source) != 0)
414*eda14cbcSMatt Macy 				continue;
415*eda14cbcSMatt Macy 			if ((strcmp(source, zhp->zfs_name) != 0) &&
416*eda14cbcSMatt Macy 			    (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
417*eda14cbcSMatt Macy 				continue;
418*eda14cbcSMatt Macy 		}
419*eda14cbcSMatt Macy 
420*eda14cbcSMatt Macy 		if (zfs_prop_user(propname) ||
421*eda14cbcSMatt Macy 		    zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
422*eda14cbcSMatt Macy 			char *value;
423*eda14cbcSMatt Macy 			verify(nvlist_lookup_string(propnv,
424*eda14cbcSMatt Macy 			    ZPROP_VALUE, &value) == 0);
425*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_add_string(nv, propname, value));
426*eda14cbcSMatt Macy 		} else {
427*eda14cbcSMatt Macy 			uint64_t value;
428*eda14cbcSMatt Macy 			verify(nvlist_lookup_uint64(propnv,
429*eda14cbcSMatt Macy 			    ZPROP_VALUE, &value) == 0);
430*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_add_uint64(nv, propname, value));
431*eda14cbcSMatt Macy 		}
432*eda14cbcSMatt Macy 	}
433*eda14cbcSMatt Macy }
434*eda14cbcSMatt Macy 
435*eda14cbcSMatt Macy /*
436*eda14cbcSMatt Macy  * returns snapshot creation txg
437*eda14cbcSMatt Macy  * and returns 0 if the snapshot does not exist
438*eda14cbcSMatt Macy  */
439*eda14cbcSMatt Macy static uint64_t
440*eda14cbcSMatt Macy get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
441*eda14cbcSMatt Macy {
442*eda14cbcSMatt Macy 	char name[ZFS_MAX_DATASET_NAME_LEN];
443*eda14cbcSMatt Macy 	uint64_t txg = 0;
444*eda14cbcSMatt Macy 
445*eda14cbcSMatt Macy 	if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
446*eda14cbcSMatt Macy 		return (txg);
447*eda14cbcSMatt Macy 
448*eda14cbcSMatt Macy 	(void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
449*eda14cbcSMatt Macy 	if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
450*eda14cbcSMatt Macy 		zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
451*eda14cbcSMatt Macy 		if (zhp != NULL) {
452*eda14cbcSMatt Macy 			txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
453*eda14cbcSMatt Macy 			zfs_close(zhp);
454*eda14cbcSMatt Macy 		}
455*eda14cbcSMatt Macy 	}
456*eda14cbcSMatt Macy 
457*eda14cbcSMatt Macy 	return (txg);
458*eda14cbcSMatt Macy }
459*eda14cbcSMatt Macy 
460*eda14cbcSMatt Macy /*
461*eda14cbcSMatt Macy  * recursively generate nvlists describing datasets.  See comment
462*eda14cbcSMatt Macy  * for the data structure send_data_t above for description of contents
463*eda14cbcSMatt Macy  * of the nvlist.
464*eda14cbcSMatt Macy  */
465*eda14cbcSMatt Macy static int
466*eda14cbcSMatt Macy send_iterate_fs(zfs_handle_t *zhp, void *arg)
467*eda14cbcSMatt Macy {
468*eda14cbcSMatt Macy 	send_data_t *sd = arg;
469*eda14cbcSMatt Macy 	nvlist_t *nvfs = NULL, *nv = NULL;
470*eda14cbcSMatt Macy 	int rv = 0;
471*eda14cbcSMatt Macy 	uint64_t min_txg = 0, max_txg = 0;
472*eda14cbcSMatt Macy 	uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
473*eda14cbcSMatt Macy 	uint64_t fromsnap_txg_save = sd->fromsnap_txg;
474*eda14cbcSMatt Macy 	uint64_t tosnap_txg_save = sd->tosnap_txg;
475*eda14cbcSMatt Macy 	uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
476*eda14cbcSMatt Macy 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
477*eda14cbcSMatt Macy 	uint64_t fromsnap_txg, tosnap_txg;
478*eda14cbcSMatt Macy 	char guidstring[64];
479*eda14cbcSMatt Macy 
480*eda14cbcSMatt Macy 	fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
481*eda14cbcSMatt Macy 	if (fromsnap_txg != 0)
482*eda14cbcSMatt Macy 		sd->fromsnap_txg = fromsnap_txg;
483*eda14cbcSMatt Macy 
484*eda14cbcSMatt Macy 	tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
485*eda14cbcSMatt Macy 	if (tosnap_txg != 0)
486*eda14cbcSMatt Macy 		sd->tosnap_txg = tosnap_txg;
487*eda14cbcSMatt Macy 
488*eda14cbcSMatt Macy 	/*
489*eda14cbcSMatt Macy 	 * on the send side, if the current dataset does not have tosnap,
490*eda14cbcSMatt Macy 	 * perform two additional checks:
491*eda14cbcSMatt Macy 	 *
492*eda14cbcSMatt Macy 	 * - skip sending the current dataset if it was created later than
493*eda14cbcSMatt Macy 	 *   the parent tosnap
494*eda14cbcSMatt Macy 	 * - return error if the current dataset was created earlier than
495*eda14cbcSMatt Macy 	 *   the parent tosnap
496*eda14cbcSMatt Macy 	 */
497*eda14cbcSMatt Macy 	if (sd->tosnap != NULL && tosnap_txg == 0) {
498*eda14cbcSMatt Macy 		if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
499*eda14cbcSMatt Macy 			if (sd->verbose) {
500*eda14cbcSMatt Macy 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
501*eda14cbcSMatt Macy 				    "skipping dataset %s: snapshot %s does "
502*eda14cbcSMatt Macy 				    "not exist\n"), zhp->zfs_name, sd->tosnap);
503*eda14cbcSMatt Macy 			}
504*eda14cbcSMatt Macy 		} else {
505*eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
506*eda14cbcSMatt Macy 			    "cannot send %s@%s%s: snapshot %s@%s does not "
507*eda14cbcSMatt Macy 			    "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
508*eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, " recursively") : "",
509*eda14cbcSMatt Macy 			    zhp->zfs_name, sd->tosnap);
510*eda14cbcSMatt Macy 			rv = EZFS_NOENT;
511*eda14cbcSMatt Macy 		}
512*eda14cbcSMatt Macy 		goto out;
513*eda14cbcSMatt Macy 	}
514*eda14cbcSMatt Macy 
515*eda14cbcSMatt Macy 	nvfs = fnvlist_alloc();
516*eda14cbcSMatt Macy 	fnvlist_add_string(nvfs, "name", zhp->zfs_name);
517*eda14cbcSMatt Macy 	fnvlist_add_uint64(nvfs, "parentfromsnap",
518*eda14cbcSMatt Macy 	    sd->parent_fromsnap_guid);
519*eda14cbcSMatt Macy 
520*eda14cbcSMatt Macy 	if (zhp->zfs_dmustats.dds_origin[0]) {
521*eda14cbcSMatt Macy 		zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
522*eda14cbcSMatt Macy 		    zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
523*eda14cbcSMatt Macy 		if (origin == NULL) {
524*eda14cbcSMatt Macy 			rv = -1;
525*eda14cbcSMatt Macy 			goto out;
526*eda14cbcSMatt Macy 		}
527*eda14cbcSMatt Macy 		fnvlist_add_uint64(nvfs, "origin",
528*eda14cbcSMatt Macy 		    origin->zfs_dmustats.dds_guid);
529*eda14cbcSMatt Macy 
530*eda14cbcSMatt Macy 		zfs_close(origin);
531*eda14cbcSMatt Macy 	}
532*eda14cbcSMatt Macy 
533*eda14cbcSMatt Macy 	/* iterate over props */
534*eda14cbcSMatt Macy 	if (sd->props || sd->backup || sd->recursive) {
535*eda14cbcSMatt Macy 		nv = fnvlist_alloc();
536*eda14cbcSMatt Macy 		send_iterate_prop(zhp, sd->backup, nv);
537*eda14cbcSMatt Macy 	}
538*eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
539*eda14cbcSMatt Macy 		boolean_t encroot;
540*eda14cbcSMatt Macy 
541*eda14cbcSMatt Macy 		/* determine if this dataset is an encryption root */
542*eda14cbcSMatt Macy 		if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
543*eda14cbcSMatt Macy 			rv = -1;
544*eda14cbcSMatt Macy 			goto out;
545*eda14cbcSMatt Macy 		}
546*eda14cbcSMatt Macy 
547*eda14cbcSMatt Macy 		if (encroot)
548*eda14cbcSMatt Macy 			fnvlist_add_boolean(nvfs, "is_encroot");
549*eda14cbcSMatt Macy 
550*eda14cbcSMatt Macy 		/*
551*eda14cbcSMatt Macy 		 * Encrypted datasets can only be sent with properties if
552*eda14cbcSMatt Macy 		 * the raw flag is specified because the receive side doesn't
553*eda14cbcSMatt Macy 		 * currently have a mechanism for recursively asking the user
554*eda14cbcSMatt Macy 		 * for new encryption parameters.
555*eda14cbcSMatt Macy 		 */
556*eda14cbcSMatt Macy 		if (!sd->raw) {
557*eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
558*eda14cbcSMatt Macy 			    "cannot send %s@%s: encrypted dataset %s may not "
559*eda14cbcSMatt Macy 			    "be sent with properties without the raw flag\n"),
560*eda14cbcSMatt Macy 			    sd->fsname, sd->tosnap, zhp->zfs_name);
561*eda14cbcSMatt Macy 			rv = -1;
562*eda14cbcSMatt Macy 			goto out;
563*eda14cbcSMatt Macy 		}
564*eda14cbcSMatt Macy 
565*eda14cbcSMatt Macy 	}
566*eda14cbcSMatt Macy 
567*eda14cbcSMatt Macy 	if (nv != NULL)
568*eda14cbcSMatt Macy 		fnvlist_add_nvlist(nvfs, "props", nv);
569*eda14cbcSMatt Macy 
570*eda14cbcSMatt Macy 	/* iterate over snaps, and set sd->parent_fromsnap_guid */
571*eda14cbcSMatt Macy 	sd->parent_fromsnap_guid = 0;
572*eda14cbcSMatt Macy 	sd->parent_snaps = fnvlist_alloc();
573*eda14cbcSMatt Macy 	sd->snapprops = fnvlist_alloc();
574*eda14cbcSMatt Macy 	if (sd->holds)
575*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_alloc(&sd->snapholds, NV_UNIQUE_NAME, 0));
576*eda14cbcSMatt Macy 
577*eda14cbcSMatt Macy 
578*eda14cbcSMatt Macy 	/*
579*eda14cbcSMatt Macy 	 * If this is a "doall" send, a replicate send or we're just trying
580*eda14cbcSMatt Macy 	 * to gather a list of previous snapshots, iterate through all the
581*eda14cbcSMatt Macy 	 * snaps in the txg range. Otherwise just look at the one we're
582*eda14cbcSMatt Macy 	 * interested in.
583*eda14cbcSMatt Macy 	 */
584*eda14cbcSMatt Macy 	if (sd->doall || sd->replicate || sd->tosnap == NULL) {
585*eda14cbcSMatt Macy 		if (!sd->replicate && fromsnap_txg != 0)
586*eda14cbcSMatt Macy 			min_txg = fromsnap_txg;
587*eda14cbcSMatt Macy 		if (!sd->replicate && tosnap_txg != 0)
588*eda14cbcSMatt Macy 			max_txg = tosnap_txg;
589*eda14cbcSMatt Macy 		(void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd,
590*eda14cbcSMatt Macy 		    min_txg, max_txg);
591*eda14cbcSMatt Macy 	} else {
592*eda14cbcSMatt Macy 		char snapname[MAXPATHLEN] = { 0 };
593*eda14cbcSMatt Macy 		zfs_handle_t *snap;
594*eda14cbcSMatt Macy 
595*eda14cbcSMatt Macy 		(void) snprintf(snapname, sizeof (snapname), "%s@%s",
596*eda14cbcSMatt Macy 		    zhp->zfs_name, sd->tosnap);
597*eda14cbcSMatt Macy 		if (sd->fromsnap != NULL)
598*eda14cbcSMatt Macy 			sd->seenfrom = B_TRUE;
599*eda14cbcSMatt Macy 		snap = zfs_open(zhp->zfs_hdl, snapname,
600*eda14cbcSMatt Macy 		    ZFS_TYPE_SNAPSHOT);
601*eda14cbcSMatt Macy 		if (snap != NULL)
602*eda14cbcSMatt Macy 			(void) send_iterate_snap(snap, sd);
603*eda14cbcSMatt Macy 	}
604*eda14cbcSMatt Macy 
605*eda14cbcSMatt Macy 	fnvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps);
606*eda14cbcSMatt Macy 	fnvlist_add_nvlist(nvfs, "snapprops", sd->snapprops);
607*eda14cbcSMatt Macy 	if (sd->holds)
608*eda14cbcSMatt Macy 		fnvlist_add_nvlist(nvfs, "snapholds", sd->snapholds);
609*eda14cbcSMatt Macy 	fnvlist_free(sd->parent_snaps);
610*eda14cbcSMatt Macy 	fnvlist_free(sd->snapprops);
611*eda14cbcSMatt Macy 	fnvlist_free(sd->snapholds);
612*eda14cbcSMatt Macy 
613*eda14cbcSMatt Macy 	/* add this fs to nvlist */
614*eda14cbcSMatt Macy 	(void) snprintf(guidstring, sizeof (guidstring),
615*eda14cbcSMatt Macy 	    "0x%llx", (longlong_t)guid);
616*eda14cbcSMatt Macy 	fnvlist_add_nvlist(sd->fss, guidstring, nvfs);
617*eda14cbcSMatt Macy 
618*eda14cbcSMatt Macy 	/* iterate over children */
619*eda14cbcSMatt Macy 	if (sd->recursive)
620*eda14cbcSMatt Macy 		rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
621*eda14cbcSMatt Macy 
622*eda14cbcSMatt Macy out:
623*eda14cbcSMatt Macy 	sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
624*eda14cbcSMatt Macy 	sd->fromsnap_txg = fromsnap_txg_save;
625*eda14cbcSMatt Macy 	sd->tosnap_txg = tosnap_txg_save;
626*eda14cbcSMatt Macy 	fnvlist_free(nv);
627*eda14cbcSMatt Macy 	fnvlist_free(nvfs);
628*eda14cbcSMatt Macy 
629*eda14cbcSMatt Macy 	zfs_close(zhp);
630*eda14cbcSMatt Macy 	return (rv);
631*eda14cbcSMatt Macy }
632*eda14cbcSMatt Macy 
633*eda14cbcSMatt Macy static int
634*eda14cbcSMatt Macy gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
635*eda14cbcSMatt Macy     const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t doall,
636*eda14cbcSMatt Macy     boolean_t replicate, boolean_t verbose, boolean_t backup, boolean_t holds,
637*eda14cbcSMatt Macy     boolean_t props, nvlist_t **nvlp, avl_tree_t **avlp)
638*eda14cbcSMatt Macy {
639*eda14cbcSMatt Macy 	zfs_handle_t *zhp;
640*eda14cbcSMatt Macy 	send_data_t sd = { 0 };
641*eda14cbcSMatt Macy 	int error;
642*eda14cbcSMatt Macy 
643*eda14cbcSMatt Macy 	zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
644*eda14cbcSMatt Macy 	if (zhp == NULL)
645*eda14cbcSMatt Macy 		return (EZFS_BADTYPE);
646*eda14cbcSMatt Macy 
647*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
648*eda14cbcSMatt Macy 	sd.fsname = fsname;
649*eda14cbcSMatt Macy 	sd.fromsnap = fromsnap;
650*eda14cbcSMatt Macy 	sd.tosnap = tosnap;
651*eda14cbcSMatt Macy 	sd.recursive = recursive;
652*eda14cbcSMatt Macy 	sd.raw = raw;
653*eda14cbcSMatt Macy 	sd.doall = doall;
654*eda14cbcSMatt Macy 	sd.replicate = replicate;
655*eda14cbcSMatt Macy 	sd.verbose = verbose;
656*eda14cbcSMatt Macy 	sd.backup = backup;
657*eda14cbcSMatt Macy 	sd.holds = holds;
658*eda14cbcSMatt Macy 	sd.props = props;
659*eda14cbcSMatt Macy 
660*eda14cbcSMatt Macy 	if ((error = send_iterate_fs(zhp, &sd)) != 0) {
661*eda14cbcSMatt Macy 		nvlist_free(sd.fss);
662*eda14cbcSMatt Macy 		if (avlp != NULL)
663*eda14cbcSMatt Macy 			*avlp = NULL;
664*eda14cbcSMatt Macy 		*nvlp = NULL;
665*eda14cbcSMatt Macy 		return (error);
666*eda14cbcSMatt Macy 	}
667*eda14cbcSMatt Macy 
668*eda14cbcSMatt Macy 	if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
669*eda14cbcSMatt Macy 		nvlist_free(sd.fss);
670*eda14cbcSMatt Macy 		*nvlp = NULL;
671*eda14cbcSMatt Macy 		return (EZFS_NOMEM);
672*eda14cbcSMatt Macy 	}
673*eda14cbcSMatt Macy 
674*eda14cbcSMatt Macy 	*nvlp = sd.fss;
675*eda14cbcSMatt Macy 	return (0);
676*eda14cbcSMatt Macy }
677*eda14cbcSMatt Macy 
678*eda14cbcSMatt Macy /*
679*eda14cbcSMatt Macy  * Routines specific to "zfs send"
680*eda14cbcSMatt Macy  */
681*eda14cbcSMatt Macy typedef struct send_dump_data {
682*eda14cbcSMatt Macy 	/* these are all just the short snapname (the part after the @) */
683*eda14cbcSMatt Macy 	const char *fromsnap;
684*eda14cbcSMatt Macy 	const char *tosnap;
685*eda14cbcSMatt Macy 	char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
686*eda14cbcSMatt Macy 	uint64_t prevsnap_obj;
687*eda14cbcSMatt Macy 	boolean_t seenfrom, seento, replicate, doall, fromorigin;
688*eda14cbcSMatt Macy 	boolean_t dryrun, parsable, progress, embed_data, std_out;
689*eda14cbcSMatt Macy 	boolean_t large_block, compress, raw, holds;
690*eda14cbcSMatt Macy 	int outfd;
691*eda14cbcSMatt Macy 	boolean_t err;
692*eda14cbcSMatt Macy 	nvlist_t *fss;
693*eda14cbcSMatt Macy 	nvlist_t *snapholds;
694*eda14cbcSMatt Macy 	avl_tree_t *fsavl;
695*eda14cbcSMatt Macy 	snapfilter_cb_t *filter_cb;
696*eda14cbcSMatt Macy 	void *filter_cb_arg;
697*eda14cbcSMatt Macy 	nvlist_t *debugnv;
698*eda14cbcSMatt Macy 	char holdtag[ZFS_MAX_DATASET_NAME_LEN];
699*eda14cbcSMatt Macy 	int cleanup_fd;
700*eda14cbcSMatt Macy 	int verbosity;
701*eda14cbcSMatt Macy 	uint64_t size;
702*eda14cbcSMatt Macy } send_dump_data_t;
703*eda14cbcSMatt Macy 
704*eda14cbcSMatt Macy static int
705*eda14cbcSMatt Macy zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
706*eda14cbcSMatt Macy     enum lzc_send_flags flags, uint64_t *spacep)
707*eda14cbcSMatt Macy {
708*eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
709*eda14cbcSMatt Macy 	int error;
710*eda14cbcSMatt Macy 
711*eda14cbcSMatt Macy 	assert(snapname != NULL);
712*eda14cbcSMatt Macy 	error = lzc_send_space(snapname, from, flags, spacep);
713*eda14cbcSMatt Macy 
714*eda14cbcSMatt Macy 	if (error != 0) {
715*eda14cbcSMatt Macy 		char errbuf[1024];
716*eda14cbcSMatt Macy 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
717*eda14cbcSMatt Macy 		    "warning: cannot estimate space for '%s'"), snapname);
718*eda14cbcSMatt Macy 
719*eda14cbcSMatt Macy 		switch (error) {
720*eda14cbcSMatt Macy 		case EXDEV:
721*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
722*eda14cbcSMatt Macy 			    "not an earlier snapshot from the same fs"));
723*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
724*eda14cbcSMatt Macy 
725*eda14cbcSMatt Macy 		case ENOENT:
726*eda14cbcSMatt Macy 			if (zfs_dataset_exists(hdl, snapname,
727*eda14cbcSMatt Macy 			    ZFS_TYPE_SNAPSHOT)) {
728*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
729*eda14cbcSMatt Macy 				    "incremental source (%s) does not exist"),
730*eda14cbcSMatt Macy 				    snapname);
731*eda14cbcSMatt Macy 			}
732*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
733*eda14cbcSMatt Macy 
734*eda14cbcSMatt Macy 		case EDQUOT:
735*eda14cbcSMatt Macy 		case EFBIG:
736*eda14cbcSMatt Macy 		case EIO:
737*eda14cbcSMatt Macy 		case ENOLINK:
738*eda14cbcSMatt Macy 		case ENOSPC:
739*eda14cbcSMatt Macy 		case ENOSTR:
740*eda14cbcSMatt Macy 		case ENXIO:
741*eda14cbcSMatt Macy 		case EPIPE:
742*eda14cbcSMatt Macy 		case ERANGE:
743*eda14cbcSMatt Macy 		case EFAULT:
744*eda14cbcSMatt Macy 		case EROFS:
745*eda14cbcSMatt Macy 		case EINVAL:
746*eda14cbcSMatt Macy 			zfs_error_aux(hdl, strerror(error));
747*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
748*eda14cbcSMatt Macy 
749*eda14cbcSMatt Macy 		default:
750*eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, error, errbuf));
751*eda14cbcSMatt Macy 		}
752*eda14cbcSMatt Macy 	}
753*eda14cbcSMatt Macy 
754*eda14cbcSMatt Macy 	return (0);
755*eda14cbcSMatt Macy }
756*eda14cbcSMatt Macy 
757*eda14cbcSMatt Macy /*
758*eda14cbcSMatt Macy  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
759*eda14cbcSMatt Macy  * NULL) to the file descriptor specified by outfd.
760*eda14cbcSMatt Macy  */
761*eda14cbcSMatt Macy static int
762*eda14cbcSMatt Macy dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
763*eda14cbcSMatt Macy     boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
764*eda14cbcSMatt Macy     nvlist_t *debugnv)
765*eda14cbcSMatt Macy {
766*eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
767*eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
768*eda14cbcSMatt Macy 	nvlist_t *thisdbg;
769*eda14cbcSMatt Macy 
770*eda14cbcSMatt Macy 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
771*eda14cbcSMatt Macy 	assert(fromsnap_obj == 0 || !fromorigin);
772*eda14cbcSMatt Macy 
773*eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
774*eda14cbcSMatt Macy 	zc.zc_cookie = outfd;
775*eda14cbcSMatt Macy 	zc.zc_obj = fromorigin;
776*eda14cbcSMatt Macy 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
777*eda14cbcSMatt Macy 	zc.zc_fromobj = fromsnap_obj;
778*eda14cbcSMatt Macy 	zc.zc_flags = flags;
779*eda14cbcSMatt Macy 
780*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
781*eda14cbcSMatt Macy 	if (fromsnap && fromsnap[0] != '\0') {
782*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_add_string(thisdbg,
783*eda14cbcSMatt Macy 		    "fromsnap", fromsnap));
784*eda14cbcSMatt Macy 	}
785*eda14cbcSMatt Macy 
786*eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
787*eda14cbcSMatt Macy 		char errbuf[1024];
788*eda14cbcSMatt Macy 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
789*eda14cbcSMatt Macy 		    "warning: cannot send '%s'"), zhp->zfs_name);
790*eda14cbcSMatt Macy 
791*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
792*eda14cbcSMatt Macy 		if (debugnv) {
793*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_add_nvlist(debugnv,
794*eda14cbcSMatt Macy 			    zhp->zfs_name, thisdbg));
795*eda14cbcSMatt Macy 		}
796*eda14cbcSMatt Macy 		nvlist_free(thisdbg);
797*eda14cbcSMatt Macy 
798*eda14cbcSMatt Macy 		switch (errno) {
799*eda14cbcSMatt Macy 		case EXDEV:
800*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
801*eda14cbcSMatt Macy 			    "not an earlier snapshot from the same fs"));
802*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
803*eda14cbcSMatt Macy 
804*eda14cbcSMatt Macy 		case EACCES:
805*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
806*eda14cbcSMatt Macy 			    "source key must be loaded"));
807*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
808*eda14cbcSMatt Macy 
809*eda14cbcSMatt Macy 		case ENOENT:
810*eda14cbcSMatt Macy 			if (zfs_dataset_exists(hdl, zc.zc_name,
811*eda14cbcSMatt Macy 			    ZFS_TYPE_SNAPSHOT)) {
812*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
813*eda14cbcSMatt Macy 				    "incremental source (@%s) does not exist"),
814*eda14cbcSMatt Macy 				    zc.zc_value);
815*eda14cbcSMatt Macy 			}
816*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
817*eda14cbcSMatt Macy 
818*eda14cbcSMatt Macy 		case EDQUOT:
819*eda14cbcSMatt Macy 		case EFBIG:
820*eda14cbcSMatt Macy 		case EIO:
821*eda14cbcSMatt Macy 		case ENOLINK:
822*eda14cbcSMatt Macy 		case ENOSPC:
823*eda14cbcSMatt Macy 		case ENOSTR:
824*eda14cbcSMatt Macy 		case ENXIO:
825*eda14cbcSMatt Macy 		case EPIPE:
826*eda14cbcSMatt Macy 		case ERANGE:
827*eda14cbcSMatt Macy 		case EFAULT:
828*eda14cbcSMatt Macy 		case EROFS:
829*eda14cbcSMatt Macy 			zfs_error_aux(hdl, strerror(errno));
830*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
831*eda14cbcSMatt Macy 
832*eda14cbcSMatt Macy 		default:
833*eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, errno, errbuf));
834*eda14cbcSMatt Macy 		}
835*eda14cbcSMatt Macy 	}
836*eda14cbcSMatt Macy 
837*eda14cbcSMatt Macy 	if (debugnv)
838*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
839*eda14cbcSMatt Macy 	nvlist_free(thisdbg);
840*eda14cbcSMatt Macy 
841*eda14cbcSMatt Macy 	return (0);
842*eda14cbcSMatt Macy }
843*eda14cbcSMatt Macy 
844*eda14cbcSMatt Macy static void
845*eda14cbcSMatt Macy gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
846*eda14cbcSMatt Macy {
847*eda14cbcSMatt Macy 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
848*eda14cbcSMatt Macy 
849*eda14cbcSMatt Macy 	/*
850*eda14cbcSMatt Macy 	 * zfs_send() only sets snapholds for sends that need them,
851*eda14cbcSMatt Macy 	 * e.g. replication and doall.
852*eda14cbcSMatt Macy 	 */
853*eda14cbcSMatt Macy 	if (sdd->snapholds == NULL)
854*eda14cbcSMatt Macy 		return;
855*eda14cbcSMatt Macy 
856*eda14cbcSMatt Macy 	fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
857*eda14cbcSMatt Macy }
858*eda14cbcSMatt Macy 
859*eda14cbcSMatt Macy int
860*eda14cbcSMatt Macy zfs_send_progress(zfs_handle_t *zhp, int fd, uint64_t *bytes_written,
861*eda14cbcSMatt Macy     uint64_t *blocks_visited)
862*eda14cbcSMatt Macy {
863*eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
864*eda14cbcSMatt Macy 
865*eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
866*eda14cbcSMatt Macy 	zc.zc_cookie = fd;
867*eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
868*eda14cbcSMatt Macy 		return (errno);
869*eda14cbcSMatt Macy 	if (bytes_written != NULL)
870*eda14cbcSMatt Macy 		*bytes_written = zc.zc_cookie;
871*eda14cbcSMatt Macy 	if (blocks_visited != NULL)
872*eda14cbcSMatt Macy 		*blocks_visited = zc.zc_objset_type;
873*eda14cbcSMatt Macy 	return (0);
874*eda14cbcSMatt Macy }
875*eda14cbcSMatt Macy 
876*eda14cbcSMatt Macy static void *
877*eda14cbcSMatt Macy send_progress_thread(void *arg)
878*eda14cbcSMatt Macy {
879*eda14cbcSMatt Macy 	progress_arg_t *pa = arg;
880*eda14cbcSMatt Macy 	zfs_handle_t *zhp = pa->pa_zhp;
881*eda14cbcSMatt Macy 	uint64_t bytes;
882*eda14cbcSMatt Macy 	uint64_t blocks;
883*eda14cbcSMatt Macy 	char buf[16];
884*eda14cbcSMatt Macy 	time_t t;
885*eda14cbcSMatt Macy 	struct tm *tm;
886*eda14cbcSMatt Macy 	boolean_t firstloop = B_TRUE;
887*eda14cbcSMatt Macy 
888*eda14cbcSMatt Macy 	/*
889*eda14cbcSMatt Macy 	 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
890*eda14cbcSMatt Macy 	 */
891*eda14cbcSMatt Macy 	for (;;) {
892*eda14cbcSMatt Macy 		int err;
893*eda14cbcSMatt Macy 		(void) sleep(1);
894*eda14cbcSMatt Macy 		if ((err = zfs_send_progress(zhp, pa->pa_fd, &bytes,
895*eda14cbcSMatt Macy 		    &blocks)) != 0) {
896*eda14cbcSMatt Macy 			if (err == EINTR || err == ENOENT)
897*eda14cbcSMatt Macy 				return ((void *)0);
898*eda14cbcSMatt Macy 			return ((void *)(uintptr_t)err);
899*eda14cbcSMatt Macy 		}
900*eda14cbcSMatt Macy 
901*eda14cbcSMatt Macy 		if (firstloop && !pa->pa_parsable) {
902*eda14cbcSMatt Macy 			(void) fprintf(stderr,
903*eda14cbcSMatt Macy 			    "TIME       %s   %sSNAPSHOT %s\n",
904*eda14cbcSMatt Macy 			    pa->pa_estimate ? "BYTES" : " SENT",
905*eda14cbcSMatt Macy 			    pa->pa_verbosity >= 2 ? "   BLOCKS    " : "",
906*eda14cbcSMatt Macy 			    zhp->zfs_name);
907*eda14cbcSMatt Macy 			firstloop = B_FALSE;
908*eda14cbcSMatt Macy 		}
909*eda14cbcSMatt Macy 
910*eda14cbcSMatt Macy 		(void) time(&t);
911*eda14cbcSMatt Macy 		tm = localtime(&t);
912*eda14cbcSMatt Macy 
913*eda14cbcSMatt Macy 		if (pa->pa_verbosity >= 2 && pa->pa_parsable) {
914*eda14cbcSMatt Macy 			(void) fprintf(stderr,
915*eda14cbcSMatt Macy 			    "%02d:%02d:%02d\t%llu\t%llu\t%s\n",
916*eda14cbcSMatt Macy 			    tm->tm_hour, tm->tm_min, tm->tm_sec,
917*eda14cbcSMatt Macy 			    (u_longlong_t)bytes, (u_longlong_t)blocks,
918*eda14cbcSMatt Macy 			    zhp->zfs_name);
919*eda14cbcSMatt Macy 		} else if (pa->pa_verbosity >= 2) {
920*eda14cbcSMatt Macy 			zfs_nicenum(bytes, buf, sizeof (buf));
921*eda14cbcSMatt Macy 			(void) fprintf(stderr,
922*eda14cbcSMatt Macy 			    "%02d:%02d:%02d   %5s    %8llu    %s\n",
923*eda14cbcSMatt Macy 			    tm->tm_hour, tm->tm_min, tm->tm_sec,
924*eda14cbcSMatt Macy 			    buf, (u_longlong_t)blocks, zhp->zfs_name);
925*eda14cbcSMatt Macy 		} else if (pa->pa_parsable) {
926*eda14cbcSMatt Macy 			(void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
927*eda14cbcSMatt Macy 			    tm->tm_hour, tm->tm_min, tm->tm_sec,
928*eda14cbcSMatt Macy 			    (u_longlong_t)bytes, zhp->zfs_name);
929*eda14cbcSMatt Macy 		} else {
930*eda14cbcSMatt Macy 			zfs_nicebytes(bytes, buf, sizeof (buf));
931*eda14cbcSMatt Macy 			(void) fprintf(stderr, "%02d:%02d:%02d   %5s   %s\n",
932*eda14cbcSMatt Macy 			    tm->tm_hour, tm->tm_min, tm->tm_sec,
933*eda14cbcSMatt Macy 			    buf, zhp->zfs_name);
934*eda14cbcSMatt Macy 		}
935*eda14cbcSMatt Macy 	}
936*eda14cbcSMatt Macy }
937*eda14cbcSMatt Macy 
938*eda14cbcSMatt Macy static void
939*eda14cbcSMatt Macy send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
940*eda14cbcSMatt Macy     uint64_t size, boolean_t parsable)
941*eda14cbcSMatt Macy {
942*eda14cbcSMatt Macy 	if (parsable) {
943*eda14cbcSMatt Macy 		if (fromsnap != NULL) {
944*eda14cbcSMatt Macy 			(void) fprintf(fout, "incremental\t%s\t%s",
945*eda14cbcSMatt Macy 			    fromsnap, tosnap);
946*eda14cbcSMatt Macy 		} else {
947*eda14cbcSMatt Macy 			(void) fprintf(fout, "full\t%s",
948*eda14cbcSMatt Macy 			    tosnap);
949*eda14cbcSMatt Macy 		}
950*eda14cbcSMatt Macy 	} else {
951*eda14cbcSMatt Macy 		if (fromsnap != NULL) {
952*eda14cbcSMatt Macy 			if (strchr(fromsnap, '@') == NULL &&
953*eda14cbcSMatt Macy 			    strchr(fromsnap, '#') == NULL) {
954*eda14cbcSMatt Macy 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
955*eda14cbcSMatt Macy 				    "send from @%s to %s"),
956*eda14cbcSMatt Macy 				    fromsnap, tosnap);
957*eda14cbcSMatt Macy 			} else {
958*eda14cbcSMatt Macy 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
959*eda14cbcSMatt Macy 				    "send from %s to %s"),
960*eda14cbcSMatt Macy 				    fromsnap, tosnap);
961*eda14cbcSMatt Macy 			}
962*eda14cbcSMatt Macy 		} else {
963*eda14cbcSMatt Macy 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
964*eda14cbcSMatt Macy 			    "full send of %s"),
965*eda14cbcSMatt Macy 			    tosnap);
966*eda14cbcSMatt Macy 		}
967*eda14cbcSMatt Macy 	}
968*eda14cbcSMatt Macy 
969*eda14cbcSMatt Macy 	if (parsable) {
970*eda14cbcSMatt Macy 		(void) fprintf(fout, "\t%llu",
971*eda14cbcSMatt Macy 		    (longlong_t)size);
972*eda14cbcSMatt Macy 	} else if (size != 0) {
973*eda14cbcSMatt Macy 		char buf[16];
974*eda14cbcSMatt Macy 		zfs_nicebytes(size, buf, sizeof (buf));
975*eda14cbcSMatt Macy 		(void) fprintf(fout, dgettext(TEXT_DOMAIN,
976*eda14cbcSMatt Macy 		    " estimated size is %s"), buf);
977*eda14cbcSMatt Macy 	}
978*eda14cbcSMatt Macy 	(void) fprintf(fout, "\n");
979*eda14cbcSMatt Macy }
980*eda14cbcSMatt Macy 
981*eda14cbcSMatt Macy static int
982*eda14cbcSMatt Macy dump_snapshot(zfs_handle_t *zhp, void *arg)
983*eda14cbcSMatt Macy {
984*eda14cbcSMatt Macy 	send_dump_data_t *sdd = arg;
985*eda14cbcSMatt Macy 	progress_arg_t pa = { 0 };
986*eda14cbcSMatt Macy 	pthread_t tid;
987*eda14cbcSMatt Macy 	char *thissnap;
988*eda14cbcSMatt Macy 	enum lzc_send_flags flags = 0;
989*eda14cbcSMatt Macy 	int err;
990*eda14cbcSMatt Macy 	boolean_t isfromsnap, istosnap, fromorigin;
991*eda14cbcSMatt Macy 	boolean_t exclude = B_FALSE;
992*eda14cbcSMatt Macy 	FILE *fout = sdd->std_out ? stdout : stderr;
993*eda14cbcSMatt Macy 
994*eda14cbcSMatt Macy 	err = 0;
995*eda14cbcSMatt Macy 	thissnap = strchr(zhp->zfs_name, '@') + 1;
996*eda14cbcSMatt Macy 	isfromsnap = (sdd->fromsnap != NULL &&
997*eda14cbcSMatt Macy 	    strcmp(sdd->fromsnap, thissnap) == 0);
998*eda14cbcSMatt Macy 
999*eda14cbcSMatt Macy 	if (!sdd->seenfrom && isfromsnap) {
1000*eda14cbcSMatt Macy 		gather_holds(zhp, sdd);
1001*eda14cbcSMatt Macy 		sdd->seenfrom = B_TRUE;
1002*eda14cbcSMatt Macy 		(void) strlcpy(sdd->prevsnap, thissnap,
1003*eda14cbcSMatt Macy 		    sizeof (sdd->prevsnap));
1004*eda14cbcSMatt Macy 		sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1005*eda14cbcSMatt Macy 		zfs_close(zhp);
1006*eda14cbcSMatt Macy 		return (0);
1007*eda14cbcSMatt Macy 	}
1008*eda14cbcSMatt Macy 
1009*eda14cbcSMatt Macy 	if (sdd->seento || !sdd->seenfrom) {
1010*eda14cbcSMatt Macy 		zfs_close(zhp);
1011*eda14cbcSMatt Macy 		return (0);
1012*eda14cbcSMatt Macy 	}
1013*eda14cbcSMatt Macy 
1014*eda14cbcSMatt Macy 	istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1015*eda14cbcSMatt Macy 	if (istosnap)
1016*eda14cbcSMatt Macy 		sdd->seento = B_TRUE;
1017*eda14cbcSMatt Macy 
1018*eda14cbcSMatt Macy 	if (sdd->large_block)
1019*eda14cbcSMatt Macy 		flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1020*eda14cbcSMatt Macy 	if (sdd->embed_data)
1021*eda14cbcSMatt Macy 		flags |= LZC_SEND_FLAG_EMBED_DATA;
1022*eda14cbcSMatt Macy 	if (sdd->compress)
1023*eda14cbcSMatt Macy 		flags |= LZC_SEND_FLAG_COMPRESS;
1024*eda14cbcSMatt Macy 	if (sdd->raw)
1025*eda14cbcSMatt Macy 		flags |= LZC_SEND_FLAG_RAW;
1026*eda14cbcSMatt Macy 
1027*eda14cbcSMatt Macy 	if (!sdd->doall && !isfromsnap && !istosnap) {
1028*eda14cbcSMatt Macy 		if (sdd->replicate) {
1029*eda14cbcSMatt Macy 			char *snapname;
1030*eda14cbcSMatt Macy 			nvlist_t *snapprops;
1031*eda14cbcSMatt Macy 			/*
1032*eda14cbcSMatt Macy 			 * Filter out all intermediate snapshots except origin
1033*eda14cbcSMatt Macy 			 * snapshots needed to replicate clones.
1034*eda14cbcSMatt Macy 			 */
1035*eda14cbcSMatt Macy 			nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1036*eda14cbcSMatt Macy 			    zhp->zfs_dmustats.dds_guid, &snapname);
1037*eda14cbcSMatt Macy 
1038*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1039*eda14cbcSMatt Macy 			    "snapprops", &snapprops));
1040*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1041*eda14cbcSMatt Macy 			    thissnap, &snapprops));
1042*eda14cbcSMatt Macy 			exclude = !nvlist_exists(snapprops, "is_clone_origin");
1043*eda14cbcSMatt Macy 		} else {
1044*eda14cbcSMatt Macy 			exclude = B_TRUE;
1045*eda14cbcSMatt Macy 		}
1046*eda14cbcSMatt Macy 	}
1047*eda14cbcSMatt Macy 
1048*eda14cbcSMatt Macy 	/*
1049*eda14cbcSMatt Macy 	 * If a filter function exists, call it to determine whether
1050*eda14cbcSMatt Macy 	 * this snapshot will be sent.
1051*eda14cbcSMatt Macy 	 */
1052*eda14cbcSMatt Macy 	if (exclude || (sdd->filter_cb != NULL &&
1053*eda14cbcSMatt Macy 	    sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1054*eda14cbcSMatt Macy 		/*
1055*eda14cbcSMatt Macy 		 * This snapshot is filtered out.  Don't send it, and don't
1056*eda14cbcSMatt Macy 		 * set prevsnap_obj, so it will be as if this snapshot didn't
1057*eda14cbcSMatt Macy 		 * exist, and the next accepted snapshot will be sent as
1058*eda14cbcSMatt Macy 		 * an incremental from the last accepted one, or as the
1059*eda14cbcSMatt Macy 		 * first (and full) snapshot in the case of a replication,
1060*eda14cbcSMatt Macy 		 * non-incremental send.
1061*eda14cbcSMatt Macy 		 */
1062*eda14cbcSMatt Macy 		zfs_close(zhp);
1063*eda14cbcSMatt Macy 		return (0);
1064*eda14cbcSMatt Macy 	}
1065*eda14cbcSMatt Macy 
1066*eda14cbcSMatt Macy 	gather_holds(zhp, sdd);
1067*eda14cbcSMatt Macy 	fromorigin = sdd->prevsnap[0] == '\0' &&
1068*eda14cbcSMatt Macy 	    (sdd->fromorigin || sdd->replicate);
1069*eda14cbcSMatt Macy 
1070*eda14cbcSMatt Macy 	if (sdd->verbosity != 0) {
1071*eda14cbcSMatt Macy 		uint64_t size = 0;
1072*eda14cbcSMatt Macy 		char fromds[ZFS_MAX_DATASET_NAME_LEN];
1073*eda14cbcSMatt Macy 
1074*eda14cbcSMatt Macy 		if (sdd->prevsnap[0] != '\0') {
1075*eda14cbcSMatt Macy 			(void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1076*eda14cbcSMatt Macy 			*(strchr(fromds, '@') + 1) = '\0';
1077*eda14cbcSMatt Macy 			(void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1078*eda14cbcSMatt Macy 		}
1079*eda14cbcSMatt Macy 		if (zfs_send_space(zhp, zhp->zfs_name,
1080*eda14cbcSMatt Macy 		    sdd->prevsnap[0] ? fromds : NULL, flags, &size) != 0) {
1081*eda14cbcSMatt Macy 			size = 0; /* cannot estimate send space */
1082*eda14cbcSMatt Macy 		} else {
1083*eda14cbcSMatt Macy 			send_print_verbose(fout, zhp->zfs_name,
1084*eda14cbcSMatt Macy 			    sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1085*eda14cbcSMatt Macy 			    size, sdd->parsable);
1086*eda14cbcSMatt Macy 		}
1087*eda14cbcSMatt Macy 		sdd->size += size;
1088*eda14cbcSMatt Macy 	}
1089*eda14cbcSMatt Macy 
1090*eda14cbcSMatt Macy 	if (!sdd->dryrun) {
1091*eda14cbcSMatt Macy 		/*
1092*eda14cbcSMatt Macy 		 * If progress reporting is requested, spawn a new thread to
1093*eda14cbcSMatt Macy 		 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1094*eda14cbcSMatt Macy 		 */
1095*eda14cbcSMatt Macy 		if (sdd->progress) {
1096*eda14cbcSMatt Macy 			pa.pa_zhp = zhp;
1097*eda14cbcSMatt Macy 			pa.pa_fd = sdd->outfd;
1098*eda14cbcSMatt Macy 			pa.pa_parsable = sdd->parsable;
1099*eda14cbcSMatt Macy 			pa.pa_estimate = B_FALSE;
1100*eda14cbcSMatt Macy 			pa.pa_verbosity = sdd->verbosity;
1101*eda14cbcSMatt Macy 
1102*eda14cbcSMatt Macy 			if ((err = pthread_create(&tid, NULL,
1103*eda14cbcSMatt Macy 			    send_progress_thread, &pa)) != 0) {
1104*eda14cbcSMatt Macy 				zfs_close(zhp);
1105*eda14cbcSMatt Macy 				return (err);
1106*eda14cbcSMatt Macy 			}
1107*eda14cbcSMatt Macy 		}
1108*eda14cbcSMatt Macy 
1109*eda14cbcSMatt Macy 		err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1110*eda14cbcSMatt Macy 		    fromorigin, sdd->outfd, flags, sdd->debugnv);
1111*eda14cbcSMatt Macy 
1112*eda14cbcSMatt Macy 		if (sdd->progress) {
1113*eda14cbcSMatt Macy 			void *status = NULL;
1114*eda14cbcSMatt Macy 			(void) pthread_cancel(tid);
1115*eda14cbcSMatt Macy 			(void) pthread_join(tid, &status);
1116*eda14cbcSMatt Macy 			int error = (int)(uintptr_t)status;
1117*eda14cbcSMatt Macy 			if (error != 0 && status != PTHREAD_CANCELED) {
1118*eda14cbcSMatt Macy 				char errbuf[1024];
1119*eda14cbcSMatt Macy 				(void) snprintf(errbuf, sizeof (errbuf),
1120*eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN,
1121*eda14cbcSMatt Macy 				    "progress thread exited nonzero"));
1122*eda14cbcSMatt Macy 				return (zfs_standard_error(zhp->zfs_hdl, error,
1123*eda14cbcSMatt Macy 				    errbuf));
1124*eda14cbcSMatt Macy 			}
1125*eda14cbcSMatt Macy 		}
1126*eda14cbcSMatt Macy 	}
1127*eda14cbcSMatt Macy 
1128*eda14cbcSMatt Macy 	(void) strcpy(sdd->prevsnap, thissnap);
1129*eda14cbcSMatt Macy 	sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1130*eda14cbcSMatt Macy 	zfs_close(zhp);
1131*eda14cbcSMatt Macy 	return (err);
1132*eda14cbcSMatt Macy }
1133*eda14cbcSMatt Macy 
1134*eda14cbcSMatt Macy static int
1135*eda14cbcSMatt Macy dump_filesystem(zfs_handle_t *zhp, void *arg)
1136*eda14cbcSMatt Macy {
1137*eda14cbcSMatt Macy 	int rv = 0;
1138*eda14cbcSMatt Macy 	send_dump_data_t *sdd = arg;
1139*eda14cbcSMatt Macy 	boolean_t missingfrom = B_FALSE;
1140*eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
1141*eda14cbcSMatt Macy 	uint64_t min_txg = 0, max_txg = 0;
1142*eda14cbcSMatt Macy 
1143*eda14cbcSMatt Macy 	(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1144*eda14cbcSMatt Macy 	    zhp->zfs_name, sdd->tosnap);
1145*eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1146*eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1147*eda14cbcSMatt Macy 		    "WARNING: could not send %s@%s: does not exist\n"),
1148*eda14cbcSMatt Macy 		    zhp->zfs_name, sdd->tosnap);
1149*eda14cbcSMatt Macy 		sdd->err = B_TRUE;
1150*eda14cbcSMatt Macy 		return (0);
1151*eda14cbcSMatt Macy 	}
1152*eda14cbcSMatt Macy 
1153*eda14cbcSMatt Macy 	if (sdd->replicate && sdd->fromsnap) {
1154*eda14cbcSMatt Macy 		/*
1155*eda14cbcSMatt Macy 		 * If this fs does not have fromsnap, and we're doing
1156*eda14cbcSMatt Macy 		 * recursive, we need to send a full stream from the
1157*eda14cbcSMatt Macy 		 * beginning (or an incremental from the origin if this
1158*eda14cbcSMatt Macy 		 * is a clone).  If we're doing non-recursive, then let
1159*eda14cbcSMatt Macy 		 * them get the error.
1160*eda14cbcSMatt Macy 		 */
1161*eda14cbcSMatt Macy 		(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1162*eda14cbcSMatt Macy 		    zhp->zfs_name, sdd->fromsnap);
1163*eda14cbcSMatt Macy 		if (zfs_ioctl(zhp->zfs_hdl,
1164*eda14cbcSMatt Macy 		    ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1165*eda14cbcSMatt Macy 			missingfrom = B_TRUE;
1166*eda14cbcSMatt Macy 		}
1167*eda14cbcSMatt Macy 	}
1168*eda14cbcSMatt Macy 
1169*eda14cbcSMatt Macy 	sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1170*eda14cbcSMatt Macy 	sdd->prevsnap_obj = 0;
1171*eda14cbcSMatt Macy 	if (sdd->fromsnap == NULL || missingfrom)
1172*eda14cbcSMatt Macy 		sdd->seenfrom = B_TRUE;
1173*eda14cbcSMatt Macy 
1174*eda14cbcSMatt Macy 
1175*eda14cbcSMatt Macy 
1176*eda14cbcSMatt Macy 	/*
1177*eda14cbcSMatt Macy 	 * Iterate through all snapshots and process the ones we will be
1178*eda14cbcSMatt Macy 	 * sending. If we only have a "from" and "to" snapshot to deal
1179*eda14cbcSMatt Macy 	 * with, we can avoid iterating through all the other snapshots.
1180*eda14cbcSMatt Macy 	 */
1181*eda14cbcSMatt Macy 	if (sdd->doall || sdd->replicate || sdd->tosnap == NULL) {
1182*eda14cbcSMatt Macy 		if (!sdd->replicate && sdd->fromsnap != NULL)
1183*eda14cbcSMatt Macy 			min_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name,
1184*eda14cbcSMatt Macy 			    sdd->fromsnap);
1185*eda14cbcSMatt Macy 		if (!sdd->replicate && sdd->tosnap != NULL)
1186*eda14cbcSMatt Macy 			max_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name,
1187*eda14cbcSMatt Macy 			    sdd->tosnap);
1188*eda14cbcSMatt Macy 		rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg,
1189*eda14cbcSMatt Macy 		    min_txg, max_txg);
1190*eda14cbcSMatt Macy 	} else {
1191*eda14cbcSMatt Macy 		char snapname[MAXPATHLEN] = { 0 };
1192*eda14cbcSMatt Macy 		zfs_handle_t *snap;
1193*eda14cbcSMatt Macy 
1194*eda14cbcSMatt Macy 		if (!sdd->seenfrom) {
1195*eda14cbcSMatt Macy 			(void) snprintf(snapname, sizeof (snapname),
1196*eda14cbcSMatt Macy 			    "%s@%s", zhp->zfs_name, sdd->fromsnap);
1197*eda14cbcSMatt Macy 			snap = zfs_open(zhp->zfs_hdl, snapname,
1198*eda14cbcSMatt Macy 			    ZFS_TYPE_SNAPSHOT);
1199*eda14cbcSMatt Macy 			if (snap != NULL)
1200*eda14cbcSMatt Macy 				rv = dump_snapshot(snap, sdd);
1201*eda14cbcSMatt Macy 			else
1202*eda14cbcSMatt Macy 				rv = -1;
1203*eda14cbcSMatt Macy 		}
1204*eda14cbcSMatt Macy 
1205*eda14cbcSMatt Macy 		if (rv == 0) {
1206*eda14cbcSMatt Macy 			(void) snprintf(snapname, sizeof (snapname),
1207*eda14cbcSMatt Macy 			    "%s@%s", zhp->zfs_name, sdd->tosnap);
1208*eda14cbcSMatt Macy 			snap = zfs_open(zhp->zfs_hdl, snapname,
1209*eda14cbcSMatt Macy 			    ZFS_TYPE_SNAPSHOT);
1210*eda14cbcSMatt Macy 			if (snap != NULL)
1211*eda14cbcSMatt Macy 				rv = dump_snapshot(snap, sdd);
1212*eda14cbcSMatt Macy 			else
1213*eda14cbcSMatt Macy 				rv = -1;
1214*eda14cbcSMatt Macy 		}
1215*eda14cbcSMatt Macy 	}
1216*eda14cbcSMatt Macy 
1217*eda14cbcSMatt Macy 	if (!sdd->seenfrom) {
1218*eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1219*eda14cbcSMatt Macy 		    "WARNING: could not send %s@%s:\n"
1220*eda14cbcSMatt Macy 		    "incremental source (%s@%s) does not exist\n"),
1221*eda14cbcSMatt Macy 		    zhp->zfs_name, sdd->tosnap,
1222*eda14cbcSMatt Macy 		    zhp->zfs_name, sdd->fromsnap);
1223*eda14cbcSMatt Macy 		sdd->err = B_TRUE;
1224*eda14cbcSMatt Macy 	} else if (!sdd->seento) {
1225*eda14cbcSMatt Macy 		if (sdd->fromsnap) {
1226*eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1227*eda14cbcSMatt Macy 			    "WARNING: could not send %s@%s:\n"
1228*eda14cbcSMatt Macy 			    "incremental source (%s@%s) "
1229*eda14cbcSMatt Macy 			    "is not earlier than it\n"),
1230*eda14cbcSMatt Macy 			    zhp->zfs_name, sdd->tosnap,
1231*eda14cbcSMatt Macy 			    zhp->zfs_name, sdd->fromsnap);
1232*eda14cbcSMatt Macy 		} else {
1233*eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1234*eda14cbcSMatt Macy 			    "WARNING: "
1235*eda14cbcSMatt Macy 			    "could not send %s@%s: does not exist\n"),
1236*eda14cbcSMatt Macy 			    zhp->zfs_name, sdd->tosnap);
1237*eda14cbcSMatt Macy 		}
1238*eda14cbcSMatt Macy 		sdd->err = B_TRUE;
1239*eda14cbcSMatt Macy 	}
1240*eda14cbcSMatt Macy 
1241*eda14cbcSMatt Macy 	return (rv);
1242*eda14cbcSMatt Macy }
1243*eda14cbcSMatt Macy 
1244*eda14cbcSMatt Macy static int
1245*eda14cbcSMatt Macy dump_filesystems(zfs_handle_t *rzhp, void *arg)
1246*eda14cbcSMatt Macy {
1247*eda14cbcSMatt Macy 	send_dump_data_t *sdd = arg;
1248*eda14cbcSMatt Macy 	nvpair_t *fspair;
1249*eda14cbcSMatt Macy 	boolean_t needagain, progress;
1250*eda14cbcSMatt Macy 
1251*eda14cbcSMatt Macy 	if (!sdd->replicate)
1252*eda14cbcSMatt Macy 		return (dump_filesystem(rzhp, sdd));
1253*eda14cbcSMatt Macy 
1254*eda14cbcSMatt Macy 	/* Mark the clone origin snapshots. */
1255*eda14cbcSMatt Macy 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1256*eda14cbcSMatt Macy 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1257*eda14cbcSMatt Macy 		nvlist_t *nvfs;
1258*eda14cbcSMatt Macy 		uint64_t origin_guid = 0;
1259*eda14cbcSMatt Macy 
1260*eda14cbcSMatt Macy 		VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1261*eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1262*eda14cbcSMatt Macy 		if (origin_guid != 0) {
1263*eda14cbcSMatt Macy 			char *snapname;
1264*eda14cbcSMatt Macy 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1265*eda14cbcSMatt Macy 			    origin_guid, &snapname);
1266*eda14cbcSMatt Macy 			if (origin_nv != NULL) {
1267*eda14cbcSMatt Macy 				nvlist_t *snapprops;
1268*eda14cbcSMatt Macy 				VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1269*eda14cbcSMatt Macy 				    "snapprops", &snapprops));
1270*eda14cbcSMatt Macy 				VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1271*eda14cbcSMatt Macy 				    snapname, &snapprops));
1272*eda14cbcSMatt Macy 				VERIFY(0 == nvlist_add_boolean(
1273*eda14cbcSMatt Macy 				    snapprops, "is_clone_origin"));
1274*eda14cbcSMatt Macy 			}
1275*eda14cbcSMatt Macy 		}
1276*eda14cbcSMatt Macy 	}
1277*eda14cbcSMatt Macy again:
1278*eda14cbcSMatt Macy 	needagain = progress = B_FALSE;
1279*eda14cbcSMatt Macy 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1280*eda14cbcSMatt Macy 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1281*eda14cbcSMatt Macy 		nvlist_t *fslist, *parent_nv;
1282*eda14cbcSMatt Macy 		char *fsname;
1283*eda14cbcSMatt Macy 		zfs_handle_t *zhp;
1284*eda14cbcSMatt Macy 		int err;
1285*eda14cbcSMatt Macy 		uint64_t origin_guid = 0;
1286*eda14cbcSMatt Macy 		uint64_t parent_guid = 0;
1287*eda14cbcSMatt Macy 
1288*eda14cbcSMatt Macy 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1289*eda14cbcSMatt Macy 		if (nvlist_lookup_boolean(fslist, "sent") == 0)
1290*eda14cbcSMatt Macy 			continue;
1291*eda14cbcSMatt Macy 
1292*eda14cbcSMatt Macy 		VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1293*eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1294*eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1295*eda14cbcSMatt Macy 		    &parent_guid);
1296*eda14cbcSMatt Macy 
1297*eda14cbcSMatt Macy 		if (parent_guid != 0) {
1298*eda14cbcSMatt Macy 			parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1299*eda14cbcSMatt Macy 			if (!nvlist_exists(parent_nv, "sent")) {
1300*eda14cbcSMatt Macy 				/* parent has not been sent; skip this one */
1301*eda14cbcSMatt Macy 				needagain = B_TRUE;
1302*eda14cbcSMatt Macy 				continue;
1303*eda14cbcSMatt Macy 			}
1304*eda14cbcSMatt Macy 		}
1305*eda14cbcSMatt Macy 
1306*eda14cbcSMatt Macy 		if (origin_guid != 0) {
1307*eda14cbcSMatt Macy 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1308*eda14cbcSMatt Macy 			    origin_guid, NULL);
1309*eda14cbcSMatt Macy 			if (origin_nv != NULL &&
1310*eda14cbcSMatt Macy 			    !nvlist_exists(origin_nv, "sent")) {
1311*eda14cbcSMatt Macy 				/*
1312*eda14cbcSMatt Macy 				 * origin has not been sent yet;
1313*eda14cbcSMatt Macy 				 * skip this clone.
1314*eda14cbcSMatt Macy 				 */
1315*eda14cbcSMatt Macy 				needagain = B_TRUE;
1316*eda14cbcSMatt Macy 				continue;
1317*eda14cbcSMatt Macy 			}
1318*eda14cbcSMatt Macy 		}
1319*eda14cbcSMatt Macy 
1320*eda14cbcSMatt Macy 		zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1321*eda14cbcSMatt Macy 		if (zhp == NULL)
1322*eda14cbcSMatt Macy 			return (-1);
1323*eda14cbcSMatt Macy 		err = dump_filesystem(zhp, sdd);
1324*eda14cbcSMatt Macy 		VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1325*eda14cbcSMatt Macy 		progress = B_TRUE;
1326*eda14cbcSMatt Macy 		zfs_close(zhp);
1327*eda14cbcSMatt Macy 		if (err)
1328*eda14cbcSMatt Macy 			return (err);
1329*eda14cbcSMatt Macy 	}
1330*eda14cbcSMatt Macy 	if (needagain) {
1331*eda14cbcSMatt Macy 		assert(progress);
1332*eda14cbcSMatt Macy 		goto again;
1333*eda14cbcSMatt Macy 	}
1334*eda14cbcSMatt Macy 
1335*eda14cbcSMatt Macy 	/* clean out the sent flags in case we reuse this fss */
1336*eda14cbcSMatt Macy 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1337*eda14cbcSMatt Macy 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1338*eda14cbcSMatt Macy 		nvlist_t *fslist;
1339*eda14cbcSMatt Macy 
1340*eda14cbcSMatt Macy 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1341*eda14cbcSMatt Macy 		(void) nvlist_remove_all(fslist, "sent");
1342*eda14cbcSMatt Macy 	}
1343*eda14cbcSMatt Macy 
1344*eda14cbcSMatt Macy 	return (0);
1345*eda14cbcSMatt Macy }
1346*eda14cbcSMatt Macy 
1347*eda14cbcSMatt Macy nvlist_t *
1348*eda14cbcSMatt Macy zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1349*eda14cbcSMatt Macy {
1350*eda14cbcSMatt Macy 	unsigned int version;
1351*eda14cbcSMatt Macy 	int nread, i;
1352*eda14cbcSMatt Macy 	unsigned long long checksum, packed_len;
1353*eda14cbcSMatt Macy 
1354*eda14cbcSMatt Macy 	/*
1355*eda14cbcSMatt Macy 	 * Decode token header, which is:
1356*eda14cbcSMatt Macy 	 *   <token version>-<checksum of payload>-<uncompressed payload length>
1357*eda14cbcSMatt Macy 	 * Note that the only supported token version is 1.
1358*eda14cbcSMatt Macy 	 */
1359*eda14cbcSMatt Macy 	nread = sscanf(token, "%u-%llx-%llx-",
1360*eda14cbcSMatt Macy 	    &version, &checksum, &packed_len);
1361*eda14cbcSMatt Macy 	if (nread != 3) {
1362*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1363*eda14cbcSMatt Macy 		    "resume token is corrupt (invalid format)"));
1364*eda14cbcSMatt Macy 		return (NULL);
1365*eda14cbcSMatt Macy 	}
1366*eda14cbcSMatt Macy 
1367*eda14cbcSMatt Macy 	if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1368*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1369*eda14cbcSMatt Macy 		    "resume token is corrupt (invalid version %u)"),
1370*eda14cbcSMatt Macy 		    version);
1371*eda14cbcSMatt Macy 		return (NULL);
1372*eda14cbcSMatt Macy 	}
1373*eda14cbcSMatt Macy 
1374*eda14cbcSMatt Macy 	/* convert hexadecimal representation to binary */
1375*eda14cbcSMatt Macy 	token = strrchr(token, '-') + 1;
1376*eda14cbcSMatt Macy 	int len = strlen(token) / 2;
1377*eda14cbcSMatt Macy 	unsigned char *compressed = zfs_alloc(hdl, len);
1378*eda14cbcSMatt Macy 	for (i = 0; i < len; i++) {
1379*eda14cbcSMatt Macy 		nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1380*eda14cbcSMatt Macy 		if (nread != 1) {
1381*eda14cbcSMatt Macy 			free(compressed);
1382*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1383*eda14cbcSMatt Macy 			    "resume token is corrupt "
1384*eda14cbcSMatt Macy 			    "(payload is not hex-encoded)"));
1385*eda14cbcSMatt Macy 			return (NULL);
1386*eda14cbcSMatt Macy 		}
1387*eda14cbcSMatt Macy 	}
1388*eda14cbcSMatt Macy 
1389*eda14cbcSMatt Macy 	/* verify checksum */
1390*eda14cbcSMatt Macy 	zio_cksum_t cksum;
1391*eda14cbcSMatt Macy 	fletcher_4_native_varsize(compressed, len, &cksum);
1392*eda14cbcSMatt Macy 	if (cksum.zc_word[0] != checksum) {
1393*eda14cbcSMatt Macy 		free(compressed);
1394*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1395*eda14cbcSMatt Macy 		    "resume token is corrupt (incorrect checksum)"));
1396*eda14cbcSMatt Macy 		return (NULL);
1397*eda14cbcSMatt Macy 	}
1398*eda14cbcSMatt Macy 
1399*eda14cbcSMatt Macy 	/* uncompress */
1400*eda14cbcSMatt Macy 	void *packed = zfs_alloc(hdl, packed_len);
1401*eda14cbcSMatt Macy 	uLongf packed_len_long = packed_len;
1402*eda14cbcSMatt Macy 	if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1403*eda14cbcSMatt Macy 	    packed_len_long != packed_len) {
1404*eda14cbcSMatt Macy 		free(packed);
1405*eda14cbcSMatt Macy 		free(compressed);
1406*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1407*eda14cbcSMatt Macy 		    "resume token is corrupt (decompression failed)"));
1408*eda14cbcSMatt Macy 		return (NULL);
1409*eda14cbcSMatt Macy 	}
1410*eda14cbcSMatt Macy 
1411*eda14cbcSMatt Macy 	/* unpack nvlist */
1412*eda14cbcSMatt Macy 	nvlist_t *nv;
1413*eda14cbcSMatt Macy 	int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1414*eda14cbcSMatt Macy 	free(packed);
1415*eda14cbcSMatt Macy 	free(compressed);
1416*eda14cbcSMatt Macy 	if (error != 0) {
1417*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1418*eda14cbcSMatt Macy 		    "resume token is corrupt (nvlist_unpack failed)"));
1419*eda14cbcSMatt Macy 		return (NULL);
1420*eda14cbcSMatt Macy 	}
1421*eda14cbcSMatt Macy 	return (nv);
1422*eda14cbcSMatt Macy }
1423*eda14cbcSMatt Macy static enum lzc_send_flags
1424*eda14cbcSMatt Macy lzc_flags_from_sendflags(const sendflags_t *flags)
1425*eda14cbcSMatt Macy {
1426*eda14cbcSMatt Macy 	enum lzc_send_flags lzc_flags = 0;
1427*eda14cbcSMatt Macy 	if (flags->largeblock)
1428*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1429*eda14cbcSMatt Macy 	if (flags->embed_data)
1430*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1431*eda14cbcSMatt Macy 	if (flags->compress)
1432*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1433*eda14cbcSMatt Macy 	if (flags->raw)
1434*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_RAW;
1435*eda14cbcSMatt Macy 	if (flags->saved)
1436*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_SAVED;
1437*eda14cbcSMatt Macy 	return (lzc_flags);
1438*eda14cbcSMatt Macy }
1439*eda14cbcSMatt Macy 
1440*eda14cbcSMatt Macy static int
1441*eda14cbcSMatt Macy estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
1442*eda14cbcSMatt Macy     uint64_t resumeobj, uint64_t resumeoff, uint64_t bytes,
1443*eda14cbcSMatt Macy     const char *redactbook, char *errbuf)
1444*eda14cbcSMatt Macy {
1445*eda14cbcSMatt Macy 	uint64_t size;
1446*eda14cbcSMatt Macy 	FILE *fout = flags->dryrun ? stdout : stderr;
1447*eda14cbcSMatt Macy 	progress_arg_t pa = { 0 };
1448*eda14cbcSMatt Macy 	int err = 0;
1449*eda14cbcSMatt Macy 	pthread_t ptid;
1450*eda14cbcSMatt Macy 
1451*eda14cbcSMatt Macy 	if (flags->progress) {
1452*eda14cbcSMatt Macy 		pa.pa_zhp = zhp;
1453*eda14cbcSMatt Macy 		pa.pa_fd = fd;
1454*eda14cbcSMatt Macy 		pa.pa_parsable = flags->parsable;
1455*eda14cbcSMatt Macy 		pa.pa_estimate = B_TRUE;
1456*eda14cbcSMatt Macy 		pa.pa_verbosity = flags->verbosity;
1457*eda14cbcSMatt Macy 
1458*eda14cbcSMatt Macy 		err = pthread_create(&ptid, NULL,
1459*eda14cbcSMatt Macy 		    send_progress_thread, &pa);
1460*eda14cbcSMatt Macy 		if (err != 0) {
1461*eda14cbcSMatt Macy 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1462*eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl,
1463*eda14cbcSMatt Macy 			    EZFS_THREADCREATEFAILED, errbuf));
1464*eda14cbcSMatt Macy 		}
1465*eda14cbcSMatt Macy 	}
1466*eda14cbcSMatt Macy 
1467*eda14cbcSMatt Macy 	err = lzc_send_space_resume_redacted(zhp->zfs_name, from,
1468*eda14cbcSMatt Macy 	    lzc_flags_from_sendflags(flags), resumeobj, resumeoff, bytes,
1469*eda14cbcSMatt Macy 	    redactbook, fd, &size);
1470*eda14cbcSMatt Macy 
1471*eda14cbcSMatt Macy 	if (flags->progress) {
1472*eda14cbcSMatt Macy 		void *status = NULL;
1473*eda14cbcSMatt Macy 		(void) pthread_cancel(ptid);
1474*eda14cbcSMatt Macy 		(void) pthread_join(ptid, &status);
1475*eda14cbcSMatt Macy 		int error = (int)(uintptr_t)status;
1476*eda14cbcSMatt Macy 		if (error != 0 && status != PTHREAD_CANCELED) {
1477*eda14cbcSMatt Macy 			char errbuf[1024];
1478*eda14cbcSMatt Macy 			(void) snprintf(errbuf, sizeof (errbuf),
1479*eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "progress thread exited "
1480*eda14cbcSMatt Macy 			    "nonzero"));
1481*eda14cbcSMatt Macy 			return (zfs_standard_error(zhp->zfs_hdl, error,
1482*eda14cbcSMatt Macy 			    errbuf));
1483*eda14cbcSMatt Macy 		}
1484*eda14cbcSMatt Macy 	}
1485*eda14cbcSMatt Macy 
1486*eda14cbcSMatt Macy 	if (err != 0) {
1487*eda14cbcSMatt Macy 		zfs_error_aux(zhp->zfs_hdl, strerror(err));
1488*eda14cbcSMatt Macy 		return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
1489*eda14cbcSMatt Macy 		    errbuf));
1490*eda14cbcSMatt Macy 	}
1491*eda14cbcSMatt Macy 	send_print_verbose(fout, zhp->zfs_name, from, size,
1492*eda14cbcSMatt Macy 	    flags->parsable);
1493*eda14cbcSMatt Macy 
1494*eda14cbcSMatt Macy 	if (flags->parsable) {
1495*eda14cbcSMatt Macy 		(void) fprintf(fout, "size\t%llu\n", (longlong_t)size);
1496*eda14cbcSMatt Macy 	} else {
1497*eda14cbcSMatt Macy 		char buf[16];
1498*eda14cbcSMatt Macy 		zfs_nicenum(size, buf, sizeof (buf));
1499*eda14cbcSMatt Macy 		(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1500*eda14cbcSMatt Macy 		    "total estimated size is %s\n"), buf);
1501*eda14cbcSMatt Macy 	}
1502*eda14cbcSMatt Macy 	return (0);
1503*eda14cbcSMatt Macy }
1504*eda14cbcSMatt Macy 
1505*eda14cbcSMatt Macy static boolean_t
1506*eda14cbcSMatt Macy redact_snaps_contains(const uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
1507*eda14cbcSMatt Macy {
1508*eda14cbcSMatt Macy 	for (int i = 0; i < num_snaps; i++) {
1509*eda14cbcSMatt Macy 		if (snaps[i] == guid)
1510*eda14cbcSMatt Macy 			return (B_TRUE);
1511*eda14cbcSMatt Macy 	}
1512*eda14cbcSMatt Macy 	return (B_FALSE);
1513*eda14cbcSMatt Macy }
1514*eda14cbcSMatt Macy 
1515*eda14cbcSMatt Macy static boolean_t
1516*eda14cbcSMatt Macy redact_snaps_equal(const uint64_t *snaps1, uint64_t num_snaps1,
1517*eda14cbcSMatt Macy     const uint64_t *snaps2, uint64_t num_snaps2)
1518*eda14cbcSMatt Macy {
1519*eda14cbcSMatt Macy 	if (num_snaps1 != num_snaps2)
1520*eda14cbcSMatt Macy 		return (B_FALSE);
1521*eda14cbcSMatt Macy 	for (int i = 0; i < num_snaps1; i++) {
1522*eda14cbcSMatt Macy 		if (!redact_snaps_contains(snaps2, num_snaps2, snaps1[i]))
1523*eda14cbcSMatt Macy 			return (B_FALSE);
1524*eda14cbcSMatt Macy 	}
1525*eda14cbcSMatt Macy 	return (B_TRUE);
1526*eda14cbcSMatt Macy }
1527*eda14cbcSMatt Macy 
1528*eda14cbcSMatt Macy /*
1529*eda14cbcSMatt Macy  * Check that the list of redaction snapshots in the bookmark matches the send
1530*eda14cbcSMatt Macy  * we're resuming, and return whether or not it's complete.
1531*eda14cbcSMatt Macy  *
1532*eda14cbcSMatt Macy  * Note that the caller needs to free the contents of *bookname with free() if
1533*eda14cbcSMatt Macy  * this function returns successfully.
1534*eda14cbcSMatt Macy  */
1535*eda14cbcSMatt Macy static int
1536*eda14cbcSMatt Macy find_redact_book(libzfs_handle_t *hdl, const char *path,
1537*eda14cbcSMatt Macy     const uint64_t *redact_snap_guids, int num_redact_snaps,
1538*eda14cbcSMatt Macy     char **bookname)
1539*eda14cbcSMatt Macy {
1540*eda14cbcSMatt Macy 	char errbuf[1024];
1541*eda14cbcSMatt Macy 	int error = 0;
1542*eda14cbcSMatt Macy 	nvlist_t *props = fnvlist_alloc();
1543*eda14cbcSMatt Macy 	nvlist_t *bmarks;
1544*eda14cbcSMatt Macy 
1545*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1546*eda14cbcSMatt Macy 	    "cannot resume send"));
1547*eda14cbcSMatt Macy 
1548*eda14cbcSMatt Macy 	fnvlist_add_boolean(props, "redact_complete");
1549*eda14cbcSMatt Macy 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1550*eda14cbcSMatt Macy 	error = lzc_get_bookmarks(path, props, &bmarks);
1551*eda14cbcSMatt Macy 	nvlist_free(props);
1552*eda14cbcSMatt Macy 	if (error != 0) {
1553*eda14cbcSMatt Macy 		if (error == ESRCH) {
1554*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1555*eda14cbcSMatt Macy 			    "nonexistent redaction bookmark provided"));
1556*eda14cbcSMatt Macy 		} else if (error == ENOENT) {
1557*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1558*eda14cbcSMatt Macy 			    "dataset to be sent no longer exists"));
1559*eda14cbcSMatt Macy 		} else {
1560*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1561*eda14cbcSMatt Macy 			    "unknown error: %s"), strerror(error));
1562*eda14cbcSMatt Macy 		}
1563*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1564*eda14cbcSMatt Macy 	}
1565*eda14cbcSMatt Macy 	nvpair_t *pair;
1566*eda14cbcSMatt Macy 	for (pair = nvlist_next_nvpair(bmarks, NULL); pair;
1567*eda14cbcSMatt Macy 	    pair = nvlist_next_nvpair(bmarks, pair)) {
1568*eda14cbcSMatt Macy 
1569*eda14cbcSMatt Macy 		nvlist_t *bmark = fnvpair_value_nvlist(pair);
1570*eda14cbcSMatt Macy 		nvlist_t *vallist = fnvlist_lookup_nvlist(bmark,
1571*eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1572*eda14cbcSMatt Macy 		uint_t len = 0;
1573*eda14cbcSMatt Macy 		uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist,
1574*eda14cbcSMatt Macy 		    ZPROP_VALUE, &len);
1575*eda14cbcSMatt Macy 		if (redact_snaps_equal(redact_snap_guids,
1576*eda14cbcSMatt Macy 		    num_redact_snaps, bmarksnaps, len)) {
1577*eda14cbcSMatt Macy 			break;
1578*eda14cbcSMatt Macy 		}
1579*eda14cbcSMatt Macy 	}
1580*eda14cbcSMatt Macy 	if (pair == NULL)  {
1581*eda14cbcSMatt Macy 		fnvlist_free(bmarks);
1582*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1583*eda14cbcSMatt Macy 		    "no appropriate redaction bookmark exists"));
1584*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1585*eda14cbcSMatt Macy 	}
1586*eda14cbcSMatt Macy 	char *name = nvpair_name(pair);
1587*eda14cbcSMatt Macy 	nvlist_t *bmark = fnvpair_value_nvlist(pair);
1588*eda14cbcSMatt Macy 	nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete");
1589*eda14cbcSMatt Macy 	boolean_t complete = fnvlist_lookup_boolean_value(vallist,
1590*eda14cbcSMatt Macy 	    ZPROP_VALUE);
1591*eda14cbcSMatt Macy 	if (!complete) {
1592*eda14cbcSMatt Macy 		fnvlist_free(bmarks);
1593*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1594*eda14cbcSMatt Macy 		    "incomplete redaction bookmark provided"));
1595*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1596*eda14cbcSMatt Macy 	}
1597*eda14cbcSMatt Macy 	*bookname = strndup(name, ZFS_MAX_DATASET_NAME_LEN);
1598*eda14cbcSMatt Macy 	ASSERT3P(*bookname, !=, NULL);
1599*eda14cbcSMatt Macy 	fnvlist_free(bmarks);
1600*eda14cbcSMatt Macy 	return (0);
1601*eda14cbcSMatt Macy }
1602*eda14cbcSMatt Macy 
1603*eda14cbcSMatt Macy static int
1604*eda14cbcSMatt Macy zfs_send_resume_impl(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1605*eda14cbcSMatt Macy     nvlist_t *resume_nvl)
1606*eda14cbcSMatt Macy {
1607*eda14cbcSMatt Macy 	char errbuf[1024];
1608*eda14cbcSMatt Macy 	char *toname;
1609*eda14cbcSMatt Macy 	char *fromname = NULL;
1610*eda14cbcSMatt Macy 	uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1611*eda14cbcSMatt Macy 	zfs_handle_t *zhp;
1612*eda14cbcSMatt Macy 	int error = 0;
1613*eda14cbcSMatt Macy 	char name[ZFS_MAX_DATASET_NAME_LEN];
1614*eda14cbcSMatt Macy 	enum lzc_send_flags lzc_flags = 0;
1615*eda14cbcSMatt Macy 	FILE *fout = (flags->verbosity > 0 && flags->dryrun) ? stdout : stderr;
1616*eda14cbcSMatt Macy 	uint64_t *redact_snap_guids = NULL;
1617*eda14cbcSMatt Macy 	int num_redact_snaps = 0;
1618*eda14cbcSMatt Macy 	char *redact_book = NULL;
1619*eda14cbcSMatt Macy 
1620*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1621*eda14cbcSMatt Macy 	    "cannot resume send"));
1622*eda14cbcSMatt Macy 
1623*eda14cbcSMatt Macy 	if (flags->verbosity != 0) {
1624*eda14cbcSMatt Macy 		(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1625*eda14cbcSMatt Macy 		    "resume token contents:\n"));
1626*eda14cbcSMatt Macy 		nvlist_print(fout, resume_nvl);
1627*eda14cbcSMatt Macy 	}
1628*eda14cbcSMatt Macy 
1629*eda14cbcSMatt Macy 	if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1630*eda14cbcSMatt Macy 	    nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1631*eda14cbcSMatt Macy 	    nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1632*eda14cbcSMatt Macy 	    nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1633*eda14cbcSMatt Macy 	    nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1634*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1635*eda14cbcSMatt Macy 		    "resume token is corrupt"));
1636*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_FAULT, errbuf));
1637*eda14cbcSMatt Macy 	}
1638*eda14cbcSMatt Macy 	fromguid = 0;
1639*eda14cbcSMatt Macy 	(void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1640*eda14cbcSMatt Macy 
1641*eda14cbcSMatt Macy 	if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1642*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1643*eda14cbcSMatt Macy 	if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1644*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1645*eda14cbcSMatt Macy 	if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1646*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1647*eda14cbcSMatt Macy 	if (flags->raw || nvlist_exists(resume_nvl, "rawok"))
1648*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_RAW;
1649*eda14cbcSMatt Macy 	if (flags->saved || nvlist_exists(resume_nvl, "savedok"))
1650*eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_SAVED;
1651*eda14cbcSMatt Macy 
1652*eda14cbcSMatt Macy 	if (flags->saved) {
1653*eda14cbcSMatt Macy 		(void) strcpy(name, toname);
1654*eda14cbcSMatt Macy 	} else {
1655*eda14cbcSMatt Macy 		error = guid_to_name(hdl, toname, toguid, B_FALSE, name);
1656*eda14cbcSMatt Macy 		if (error != 0) {
1657*eda14cbcSMatt Macy 			if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1658*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1659*eda14cbcSMatt Macy 				    "'%s' is no longer the same snapshot "
1660*eda14cbcSMatt Macy 				    "used in the initial send"), toname);
1661*eda14cbcSMatt Macy 			} else {
1662*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1663*eda14cbcSMatt Macy 				    "'%s' used in the initial send no "
1664*eda14cbcSMatt Macy 				    "longer exists"), toname);
1665*eda14cbcSMatt Macy 			}
1666*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1667*eda14cbcSMatt Macy 		}
1668*eda14cbcSMatt Macy 	}
1669*eda14cbcSMatt Macy 
1670*eda14cbcSMatt Macy 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1671*eda14cbcSMatt Macy 	if (zhp == NULL) {
1672*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1673*eda14cbcSMatt Macy 		    "unable to access '%s'"), name);
1674*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1675*eda14cbcSMatt Macy 	}
1676*eda14cbcSMatt Macy 
1677*eda14cbcSMatt Macy 	if (nvlist_lookup_uint64_array(resume_nvl, "book_redact_snaps",
1678*eda14cbcSMatt Macy 	    &redact_snap_guids, (uint_t *)&num_redact_snaps) != 0) {
1679*eda14cbcSMatt Macy 		num_redact_snaps = -1;
1680*eda14cbcSMatt Macy 	}
1681*eda14cbcSMatt Macy 
1682*eda14cbcSMatt Macy 	if (fromguid != 0) {
1683*eda14cbcSMatt Macy 		if (guid_to_name_redact_snaps(hdl, toname, fromguid, B_TRUE,
1684*eda14cbcSMatt Macy 		    redact_snap_guids, num_redact_snaps, name) != 0) {
1685*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1686*eda14cbcSMatt Macy 			    "incremental source %#llx no longer exists"),
1687*eda14cbcSMatt Macy 			    (longlong_t)fromguid);
1688*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1689*eda14cbcSMatt Macy 		}
1690*eda14cbcSMatt Macy 		fromname = name;
1691*eda14cbcSMatt Macy 	}
1692*eda14cbcSMatt Macy 
1693*eda14cbcSMatt Macy 	redact_snap_guids = NULL;
1694*eda14cbcSMatt Macy 
1695*eda14cbcSMatt Macy 	if (nvlist_lookup_uint64_array(resume_nvl,
1696*eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &redact_snap_guids,
1697*eda14cbcSMatt Macy 	    (uint_t *)&num_redact_snaps) == 0) {
1698*eda14cbcSMatt Macy 		char path[ZFS_MAX_DATASET_NAME_LEN];
1699*eda14cbcSMatt Macy 
1700*eda14cbcSMatt Macy 		(void) strlcpy(path, toname, sizeof (path));
1701*eda14cbcSMatt Macy 		char *at = strchr(path, '@');
1702*eda14cbcSMatt Macy 		ASSERT3P(at, !=, NULL);
1703*eda14cbcSMatt Macy 
1704*eda14cbcSMatt Macy 		*at = '\0';
1705*eda14cbcSMatt Macy 
1706*eda14cbcSMatt Macy 		if ((error = find_redact_book(hdl, path, redact_snap_guids,
1707*eda14cbcSMatt Macy 		    num_redact_snaps, &redact_book)) != 0) {
1708*eda14cbcSMatt Macy 			return (error);
1709*eda14cbcSMatt Macy 		}
1710*eda14cbcSMatt Macy 	}
1711*eda14cbcSMatt Macy 
1712*eda14cbcSMatt Macy 	if (flags->verbosity != 0) {
1713*eda14cbcSMatt Macy 		/*
1714*eda14cbcSMatt Macy 		 * Some of these may have come from the resume token, set them
1715*eda14cbcSMatt Macy 		 * here for size estimate purposes.
1716*eda14cbcSMatt Macy 		 */
1717*eda14cbcSMatt Macy 		sendflags_t tmpflags = *flags;
1718*eda14cbcSMatt Macy 		if (lzc_flags & LZC_SEND_FLAG_LARGE_BLOCK)
1719*eda14cbcSMatt Macy 			tmpflags.largeblock = B_TRUE;
1720*eda14cbcSMatt Macy 		if (lzc_flags & LZC_SEND_FLAG_COMPRESS)
1721*eda14cbcSMatt Macy 			tmpflags.compress = B_TRUE;
1722*eda14cbcSMatt Macy 		if (lzc_flags & LZC_SEND_FLAG_EMBED_DATA)
1723*eda14cbcSMatt Macy 			tmpflags.embed_data = B_TRUE;
1724*eda14cbcSMatt Macy 		error = estimate_size(zhp, fromname, outfd, &tmpflags,
1725*eda14cbcSMatt Macy 		    resumeobj, resumeoff, bytes, redact_book, errbuf);
1726*eda14cbcSMatt Macy 	}
1727*eda14cbcSMatt Macy 
1728*eda14cbcSMatt Macy 	if (!flags->dryrun) {
1729*eda14cbcSMatt Macy 		progress_arg_t pa = { 0 };
1730*eda14cbcSMatt Macy 		pthread_t tid;
1731*eda14cbcSMatt Macy 		/*
1732*eda14cbcSMatt Macy 		 * If progress reporting is requested, spawn a new thread to
1733*eda14cbcSMatt Macy 		 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1734*eda14cbcSMatt Macy 		 */
1735*eda14cbcSMatt Macy 		if (flags->progress) {
1736*eda14cbcSMatt Macy 			pa.pa_zhp = zhp;
1737*eda14cbcSMatt Macy 			pa.pa_fd = outfd;
1738*eda14cbcSMatt Macy 			pa.pa_parsable = flags->parsable;
1739*eda14cbcSMatt Macy 			pa.pa_estimate = B_FALSE;
1740*eda14cbcSMatt Macy 			pa.pa_verbosity = flags->verbosity;
1741*eda14cbcSMatt Macy 
1742*eda14cbcSMatt Macy 			error = pthread_create(&tid, NULL,
1743*eda14cbcSMatt Macy 			    send_progress_thread, &pa);
1744*eda14cbcSMatt Macy 			if (error != 0) {
1745*eda14cbcSMatt Macy 				if (redact_book != NULL)
1746*eda14cbcSMatt Macy 					free(redact_book);
1747*eda14cbcSMatt Macy 				zfs_close(zhp);
1748*eda14cbcSMatt Macy 				return (error);
1749*eda14cbcSMatt Macy 			}
1750*eda14cbcSMatt Macy 		}
1751*eda14cbcSMatt Macy 
1752*eda14cbcSMatt Macy 		error = lzc_send_resume_redacted(zhp->zfs_name, fromname, outfd,
1753*eda14cbcSMatt Macy 		    lzc_flags, resumeobj, resumeoff, redact_book);
1754*eda14cbcSMatt Macy 		if (redact_book != NULL)
1755*eda14cbcSMatt Macy 			free(redact_book);
1756*eda14cbcSMatt Macy 
1757*eda14cbcSMatt Macy 		if (flags->progress) {
1758*eda14cbcSMatt Macy 			void *status = NULL;
1759*eda14cbcSMatt Macy 			(void) pthread_cancel(tid);
1760*eda14cbcSMatt Macy 			(void) pthread_join(tid, &status);
1761*eda14cbcSMatt Macy 			int error = (int)(uintptr_t)status;
1762*eda14cbcSMatt Macy 			if (error != 0 && status != PTHREAD_CANCELED) {
1763*eda14cbcSMatt Macy 				char errbuf[1024];
1764*eda14cbcSMatt Macy 				(void) snprintf(errbuf, sizeof (errbuf),
1765*eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN,
1766*eda14cbcSMatt Macy 				    "progress thread exited nonzero"));
1767*eda14cbcSMatt Macy 				return (zfs_standard_error(hdl, error, errbuf));
1768*eda14cbcSMatt Macy 			}
1769*eda14cbcSMatt Macy 		}
1770*eda14cbcSMatt Macy 
1771*eda14cbcSMatt Macy 		char errbuf[1024];
1772*eda14cbcSMatt Macy 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1773*eda14cbcSMatt Macy 		    "warning: cannot send '%s'"), zhp->zfs_name);
1774*eda14cbcSMatt Macy 
1775*eda14cbcSMatt Macy 		zfs_close(zhp);
1776*eda14cbcSMatt Macy 
1777*eda14cbcSMatt Macy 		switch (error) {
1778*eda14cbcSMatt Macy 		case 0:
1779*eda14cbcSMatt Macy 			return (0);
1780*eda14cbcSMatt Macy 		case EACCES:
1781*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1782*eda14cbcSMatt Macy 			    "source key must be loaded"));
1783*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1784*eda14cbcSMatt Macy 		case ESRCH:
1785*eda14cbcSMatt Macy 			if (lzc_exists(zhp->zfs_name)) {
1786*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1787*eda14cbcSMatt Macy 				    "incremental source could not be found"));
1788*eda14cbcSMatt Macy 			}
1789*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
1790*eda14cbcSMatt Macy 
1791*eda14cbcSMatt Macy 		case EXDEV:
1792*eda14cbcSMatt Macy 		case ENOENT:
1793*eda14cbcSMatt Macy 		case EDQUOT:
1794*eda14cbcSMatt Macy 		case EFBIG:
1795*eda14cbcSMatt Macy 		case EIO:
1796*eda14cbcSMatt Macy 		case ENOLINK:
1797*eda14cbcSMatt Macy 		case ENOSPC:
1798*eda14cbcSMatt Macy 		case ENOSTR:
1799*eda14cbcSMatt Macy 		case ENXIO:
1800*eda14cbcSMatt Macy 		case EPIPE:
1801*eda14cbcSMatt Macy 		case ERANGE:
1802*eda14cbcSMatt Macy 		case EFAULT:
1803*eda14cbcSMatt Macy 		case EROFS:
1804*eda14cbcSMatt Macy 			zfs_error_aux(hdl, strerror(errno));
1805*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1806*eda14cbcSMatt Macy 
1807*eda14cbcSMatt Macy 		default:
1808*eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, errno, errbuf));
1809*eda14cbcSMatt Macy 		}
1810*eda14cbcSMatt Macy 	} else {
1811*eda14cbcSMatt Macy 		if (redact_book != NULL)
1812*eda14cbcSMatt Macy 			free(redact_book);
1813*eda14cbcSMatt Macy 	}
1814*eda14cbcSMatt Macy 
1815*eda14cbcSMatt Macy 	zfs_close(zhp);
1816*eda14cbcSMatt Macy 
1817*eda14cbcSMatt Macy 	return (error);
1818*eda14cbcSMatt Macy }
1819*eda14cbcSMatt Macy 
1820*eda14cbcSMatt Macy int
1821*eda14cbcSMatt Macy zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1822*eda14cbcSMatt Macy     const char *resume_token)
1823*eda14cbcSMatt Macy {
1824*eda14cbcSMatt Macy 	int ret;
1825*eda14cbcSMatt Macy 	char errbuf[1024];
1826*eda14cbcSMatt Macy 	nvlist_t *resume_nvl;
1827*eda14cbcSMatt Macy 
1828*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1829*eda14cbcSMatt Macy 	    "cannot resume send"));
1830*eda14cbcSMatt Macy 
1831*eda14cbcSMatt Macy 	resume_nvl = zfs_send_resume_token_to_nvlist(hdl, resume_token);
1832*eda14cbcSMatt Macy 	if (resume_nvl == NULL) {
1833*eda14cbcSMatt Macy 		/*
1834*eda14cbcSMatt Macy 		 * zfs_error_aux has already been set by
1835*eda14cbcSMatt Macy 		 * zfs_send_resume_token_to_nvlist()
1836*eda14cbcSMatt Macy 		 */
1837*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_FAULT, errbuf));
1838*eda14cbcSMatt Macy 	}
1839*eda14cbcSMatt Macy 
1840*eda14cbcSMatt Macy 	ret = zfs_send_resume_impl(hdl, flags, outfd, resume_nvl);
1841*eda14cbcSMatt Macy 	nvlist_free(resume_nvl);
1842*eda14cbcSMatt Macy 
1843*eda14cbcSMatt Macy 	return (ret);
1844*eda14cbcSMatt Macy }
1845*eda14cbcSMatt Macy 
1846*eda14cbcSMatt Macy int
1847*eda14cbcSMatt Macy zfs_send_saved(zfs_handle_t *zhp, sendflags_t *flags, int outfd,
1848*eda14cbcSMatt Macy     const char *resume_token)
1849*eda14cbcSMatt Macy {
1850*eda14cbcSMatt Macy 	int ret;
1851*eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1852*eda14cbcSMatt Macy 	nvlist_t *saved_nvl = NULL, *resume_nvl = NULL;
1853*eda14cbcSMatt Macy 	uint64_t saved_guid = 0, resume_guid = 0;
1854*eda14cbcSMatt Macy 	uint64_t obj = 0, off = 0, bytes = 0;
1855*eda14cbcSMatt Macy 	char token_buf[ZFS_MAXPROPLEN];
1856*eda14cbcSMatt Macy 	char errbuf[1024];
1857*eda14cbcSMatt Macy 
1858*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1859*eda14cbcSMatt Macy 	    "saved send failed"));
1860*eda14cbcSMatt Macy 
1861*eda14cbcSMatt Macy 	ret = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1862*eda14cbcSMatt Macy 	    token_buf, sizeof (token_buf), NULL, NULL, 0, B_TRUE);
1863*eda14cbcSMatt Macy 	if (ret != 0)
1864*eda14cbcSMatt Macy 		goto out;
1865*eda14cbcSMatt Macy 
1866*eda14cbcSMatt Macy 	saved_nvl = zfs_send_resume_token_to_nvlist(hdl, token_buf);
1867*eda14cbcSMatt Macy 	if (saved_nvl == NULL) {
1868*eda14cbcSMatt Macy 		/*
1869*eda14cbcSMatt Macy 		 * zfs_error_aux has already been set by
1870*eda14cbcSMatt Macy 		 * zfs_send_resume_token_to_nvlist()
1871*eda14cbcSMatt Macy 		 */
1872*eda14cbcSMatt Macy 		ret = zfs_error(hdl, EZFS_FAULT, errbuf);
1873*eda14cbcSMatt Macy 		goto out;
1874*eda14cbcSMatt Macy 	}
1875*eda14cbcSMatt Macy 
1876*eda14cbcSMatt Macy 	/*
1877*eda14cbcSMatt Macy 	 * If a resume token is provided we use the object and offset
1878*eda14cbcSMatt Macy 	 * from that instead of the default, which starts from the
1879*eda14cbcSMatt Macy 	 * beginning.
1880*eda14cbcSMatt Macy 	 */
1881*eda14cbcSMatt Macy 	if (resume_token != NULL) {
1882*eda14cbcSMatt Macy 		resume_nvl = zfs_send_resume_token_to_nvlist(hdl,
1883*eda14cbcSMatt Macy 		    resume_token);
1884*eda14cbcSMatt Macy 		if (resume_nvl == NULL) {
1885*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_FAULT, errbuf);
1886*eda14cbcSMatt Macy 			goto out;
1887*eda14cbcSMatt Macy 		}
1888*eda14cbcSMatt Macy 
1889*eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(resume_nvl, "object", &obj) != 0 ||
1890*eda14cbcSMatt Macy 		    nvlist_lookup_uint64(resume_nvl, "offset", &off) != 0 ||
1891*eda14cbcSMatt Macy 		    nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1892*eda14cbcSMatt Macy 		    nvlist_lookup_uint64(resume_nvl, "toguid",
1893*eda14cbcSMatt Macy 		    &resume_guid) != 0) {
1894*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1895*eda14cbcSMatt Macy 			    "provided resume token is corrupt"));
1896*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_FAULT, errbuf);
1897*eda14cbcSMatt Macy 			goto out;
1898*eda14cbcSMatt Macy 		}
1899*eda14cbcSMatt Macy 
1900*eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(saved_nvl, "toguid",
1901*eda14cbcSMatt Macy 		    &saved_guid)) {
1902*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1903*eda14cbcSMatt Macy 			    "dataset's resume token is corrupt"));
1904*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_FAULT, errbuf);
1905*eda14cbcSMatt Macy 			goto out;
1906*eda14cbcSMatt Macy 		}
1907*eda14cbcSMatt Macy 
1908*eda14cbcSMatt Macy 		if (resume_guid != saved_guid) {
1909*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1910*eda14cbcSMatt Macy 			    "provided resume token does not match dataset"));
1911*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADBACKUP, errbuf);
1912*eda14cbcSMatt Macy 			goto out;
1913*eda14cbcSMatt Macy 		}
1914*eda14cbcSMatt Macy 	}
1915*eda14cbcSMatt Macy 
1916*eda14cbcSMatt Macy 	(void) nvlist_remove_all(saved_nvl, "object");
1917*eda14cbcSMatt Macy 	fnvlist_add_uint64(saved_nvl, "object", obj);
1918*eda14cbcSMatt Macy 
1919*eda14cbcSMatt Macy 	(void) nvlist_remove_all(saved_nvl, "offset");
1920*eda14cbcSMatt Macy 	fnvlist_add_uint64(saved_nvl, "offset", off);
1921*eda14cbcSMatt Macy 
1922*eda14cbcSMatt Macy 	(void) nvlist_remove_all(saved_nvl, "bytes");
1923*eda14cbcSMatt Macy 	fnvlist_add_uint64(saved_nvl, "bytes", bytes);
1924*eda14cbcSMatt Macy 
1925*eda14cbcSMatt Macy 	(void) nvlist_remove_all(saved_nvl, "toname");
1926*eda14cbcSMatt Macy 	fnvlist_add_string(saved_nvl, "toname", zhp->zfs_name);
1927*eda14cbcSMatt Macy 
1928*eda14cbcSMatt Macy 	ret = zfs_send_resume_impl(hdl, flags, outfd, saved_nvl);
1929*eda14cbcSMatt Macy 
1930*eda14cbcSMatt Macy out:
1931*eda14cbcSMatt Macy 	nvlist_free(saved_nvl);
1932*eda14cbcSMatt Macy 	nvlist_free(resume_nvl);
1933*eda14cbcSMatt Macy 	return (ret);
1934*eda14cbcSMatt Macy }
1935*eda14cbcSMatt Macy 
1936*eda14cbcSMatt Macy /*
1937*eda14cbcSMatt Macy  * This function informs the target system that the recursive send is complete.
1938*eda14cbcSMatt Macy  * The record is also expected in the case of a send -p.
1939*eda14cbcSMatt Macy  */
1940*eda14cbcSMatt Macy static int
1941*eda14cbcSMatt Macy send_conclusion_record(int fd, zio_cksum_t *zc)
1942*eda14cbcSMatt Macy {
1943*eda14cbcSMatt Macy 	dmu_replay_record_t drr = { 0 };
1944*eda14cbcSMatt Macy 	drr.drr_type = DRR_END;
1945*eda14cbcSMatt Macy 	if (zc != NULL)
1946*eda14cbcSMatt Macy 		drr.drr_u.drr_end.drr_checksum = *zc;
1947*eda14cbcSMatt Macy 	if (write(fd, &drr, sizeof (drr)) == -1) {
1948*eda14cbcSMatt Macy 		return (errno);
1949*eda14cbcSMatt Macy 	}
1950*eda14cbcSMatt Macy 	return (0);
1951*eda14cbcSMatt Macy }
1952*eda14cbcSMatt Macy 
1953*eda14cbcSMatt Macy /*
1954*eda14cbcSMatt Macy  * This function is responsible for sending the records that contain the
1955*eda14cbcSMatt Macy  * necessary information for the target system's libzfs to be able to set the
1956*eda14cbcSMatt Macy  * properties of the filesystem being received, or to be able to prepare for
1957*eda14cbcSMatt Macy  * a recursive receive.
1958*eda14cbcSMatt Macy  *
1959*eda14cbcSMatt Macy  * The "zhp" argument is the handle of the snapshot we are sending
1960*eda14cbcSMatt Macy  * (the "tosnap").  The "from" argument is the short snapshot name (the part
1961*eda14cbcSMatt Macy  * after the @) of the incremental source.
1962*eda14cbcSMatt Macy  */
1963*eda14cbcSMatt Macy static int
1964*eda14cbcSMatt Macy send_prelim_records(zfs_handle_t *zhp, const char *from, int fd,
1965*eda14cbcSMatt Macy     boolean_t gather_props, boolean_t recursive, boolean_t verbose,
1966*eda14cbcSMatt Macy     boolean_t dryrun, boolean_t raw, boolean_t replicate, boolean_t backup,
1967*eda14cbcSMatt Macy     boolean_t holds, boolean_t props, boolean_t doall,
1968*eda14cbcSMatt Macy     nvlist_t **fssp, avl_tree_t **fsavlp)
1969*eda14cbcSMatt Macy {
1970*eda14cbcSMatt Macy 	int err = 0;
1971*eda14cbcSMatt Macy 	char *packbuf = NULL;
1972*eda14cbcSMatt Macy 	size_t buflen = 0;
1973*eda14cbcSMatt Macy 	zio_cksum_t zc = { {0} };
1974*eda14cbcSMatt Macy 	int featureflags = 0;
1975*eda14cbcSMatt Macy 	/* name of filesystem/volume that contains snapshot we are sending */
1976*eda14cbcSMatt Macy 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
1977*eda14cbcSMatt Macy 	/* short name of snap we are sending */
1978*eda14cbcSMatt Macy 	char *tosnap = "";
1979*eda14cbcSMatt Macy 
1980*eda14cbcSMatt Macy 	char errbuf[1024];
1981*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1982*eda14cbcSMatt Macy 	    "warning: cannot send '%s'"), zhp->zfs_name);
1983*eda14cbcSMatt Macy 	if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && zfs_prop_get_int(zhp,
1984*eda14cbcSMatt Macy 	    ZFS_PROP_VERSION) >= ZPL_VERSION_SA) {
1985*eda14cbcSMatt Macy 		featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1986*eda14cbcSMatt Macy 	}
1987*eda14cbcSMatt Macy 
1988*eda14cbcSMatt Macy 	if (holds)
1989*eda14cbcSMatt Macy 		featureflags |= DMU_BACKUP_FEATURE_HOLDS;
1990*eda14cbcSMatt Macy 
1991*eda14cbcSMatt Macy 	(void) strlcpy(tofs, zhp->zfs_name, ZFS_MAX_DATASET_NAME_LEN);
1992*eda14cbcSMatt Macy 	char *at = strchr(tofs, '@');
1993*eda14cbcSMatt Macy 	if (at != NULL) {
1994*eda14cbcSMatt Macy 		*at = '\0';
1995*eda14cbcSMatt Macy 		tosnap = at + 1;
1996*eda14cbcSMatt Macy 	}
1997*eda14cbcSMatt Macy 
1998*eda14cbcSMatt Macy 	if (gather_props) {
1999*eda14cbcSMatt Macy 		nvlist_t *hdrnv = fnvlist_alloc();
2000*eda14cbcSMatt Macy 		nvlist_t *fss = NULL;
2001*eda14cbcSMatt Macy 
2002*eda14cbcSMatt Macy 		if (from != NULL)
2003*eda14cbcSMatt Macy 			fnvlist_add_string(hdrnv, "fromsnap", from);
2004*eda14cbcSMatt Macy 		fnvlist_add_string(hdrnv, "tosnap", tosnap);
2005*eda14cbcSMatt Macy 		if (!recursive)
2006*eda14cbcSMatt Macy 			fnvlist_add_boolean(hdrnv, "not_recursive");
2007*eda14cbcSMatt Macy 
2008*eda14cbcSMatt Macy 		if (raw) {
2009*eda14cbcSMatt Macy 			VERIFY0(nvlist_add_boolean(hdrnv, "raw"));
2010*eda14cbcSMatt Macy 		}
2011*eda14cbcSMatt Macy 
2012*eda14cbcSMatt Macy 		if ((err = gather_nvlist(zhp->zfs_hdl, tofs,
2013*eda14cbcSMatt Macy 		    from, tosnap, recursive, raw, doall, replicate, verbose,
2014*eda14cbcSMatt Macy 		    backup, holds, props, &fss, fsavlp)) != 0) {
2015*eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2016*eda14cbcSMatt Macy 			    errbuf));
2017*eda14cbcSMatt Macy 		}
2018*eda14cbcSMatt Macy 		fnvlist_add_nvlist(hdrnv, "fss", fss);
2019*eda14cbcSMatt Macy 		VERIFY0(nvlist_pack(hdrnv, &packbuf, &buflen, NV_ENCODE_XDR,
2020*eda14cbcSMatt Macy 		    0));
2021*eda14cbcSMatt Macy 		if (fssp != NULL) {
2022*eda14cbcSMatt Macy 			*fssp = fss;
2023*eda14cbcSMatt Macy 		} else {
2024*eda14cbcSMatt Macy 			nvlist_free(fss);
2025*eda14cbcSMatt Macy 		}
2026*eda14cbcSMatt Macy 		nvlist_free(hdrnv);
2027*eda14cbcSMatt Macy 	}
2028*eda14cbcSMatt Macy 
2029*eda14cbcSMatt Macy 	if (!dryrun) {
2030*eda14cbcSMatt Macy 		dmu_replay_record_t drr = { 0 };
2031*eda14cbcSMatt Macy 		/* write first begin record */
2032*eda14cbcSMatt Macy 		drr.drr_type = DRR_BEGIN;
2033*eda14cbcSMatt Macy 		drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
2034*eda14cbcSMatt Macy 		DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
2035*eda14cbcSMatt Macy 		    drr_versioninfo, DMU_COMPOUNDSTREAM);
2036*eda14cbcSMatt Macy 		DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
2037*eda14cbcSMatt Macy 		    drr_versioninfo, featureflags);
2038*eda14cbcSMatt Macy 		if (snprintf(drr.drr_u.drr_begin.drr_toname,
2039*eda14cbcSMatt Macy 		    sizeof (drr.drr_u.drr_begin.drr_toname), "%s@%s", tofs,
2040*eda14cbcSMatt Macy 		    tosnap) >= sizeof (drr.drr_u.drr_begin.drr_toname)) {
2041*eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2042*eda14cbcSMatt Macy 			    errbuf));
2043*eda14cbcSMatt Macy 		}
2044*eda14cbcSMatt Macy 		drr.drr_payloadlen = buflen;
2045*eda14cbcSMatt Macy 
2046*eda14cbcSMatt Macy 		err = dump_record(&drr, packbuf, buflen, &zc, fd);
2047*eda14cbcSMatt Macy 		free(packbuf);
2048*eda14cbcSMatt Macy 		if (err != 0) {
2049*eda14cbcSMatt Macy 			zfs_error_aux(zhp->zfs_hdl, strerror(err));
2050*eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2051*eda14cbcSMatt Macy 			    errbuf));
2052*eda14cbcSMatt Macy 		}
2053*eda14cbcSMatt Macy 		err = send_conclusion_record(fd, &zc);
2054*eda14cbcSMatt Macy 		if (err != 0) {
2055*eda14cbcSMatt Macy 			zfs_error_aux(zhp->zfs_hdl, strerror(err));
2056*eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2057*eda14cbcSMatt Macy 			    errbuf));
2058*eda14cbcSMatt Macy 		}
2059*eda14cbcSMatt Macy 	}
2060*eda14cbcSMatt Macy 	return (0);
2061*eda14cbcSMatt Macy }
2062*eda14cbcSMatt Macy 
2063*eda14cbcSMatt Macy /*
2064*eda14cbcSMatt Macy  * Generate a send stream.  The "zhp" argument is the filesystem/volume
2065*eda14cbcSMatt Macy  * that contains the snapshot to send.  The "fromsnap" argument is the
2066*eda14cbcSMatt Macy  * short name (the part after the '@') of the snapshot that is the
2067*eda14cbcSMatt Macy  * incremental source to send from (if non-NULL).  The "tosnap" argument
2068*eda14cbcSMatt Macy  * is the short name of the snapshot to send.
2069*eda14cbcSMatt Macy  *
2070*eda14cbcSMatt Macy  * The content of the send stream is the snapshot identified by
2071*eda14cbcSMatt Macy  * 'tosnap'.  Incremental streams are requested in two ways:
2072*eda14cbcSMatt Macy  *     - from the snapshot identified by "fromsnap" (if non-null) or
2073*eda14cbcSMatt Macy  *     - from the origin of the dataset identified by zhp, which must
2074*eda14cbcSMatt Macy  *	 be a clone.  In this case, "fromsnap" is null and "fromorigin"
2075*eda14cbcSMatt Macy  *	 is TRUE.
2076*eda14cbcSMatt Macy  *
2077*eda14cbcSMatt Macy  * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
2078*eda14cbcSMatt Macy  * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
2079*eda14cbcSMatt Macy  * if "replicate" is set.  If "doall" is set, dump all the intermediate
2080*eda14cbcSMatt Macy  * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
2081*eda14cbcSMatt Macy  * case too. If "props" is set, send properties.
2082*eda14cbcSMatt Macy  */
2083*eda14cbcSMatt Macy int
2084*eda14cbcSMatt Macy zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2085*eda14cbcSMatt Macy     sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2086*eda14cbcSMatt Macy     void *cb_arg, nvlist_t **debugnvp)
2087*eda14cbcSMatt Macy {
2088*eda14cbcSMatt Macy 	char errbuf[1024];
2089*eda14cbcSMatt Macy 	send_dump_data_t sdd = { 0 };
2090*eda14cbcSMatt Macy 	int err = 0;
2091*eda14cbcSMatt Macy 	nvlist_t *fss = NULL;
2092*eda14cbcSMatt Macy 	avl_tree_t *fsavl = NULL;
2093*eda14cbcSMatt Macy 	static uint64_t holdseq;
2094*eda14cbcSMatt Macy 	int spa_version;
2095*eda14cbcSMatt Macy 	pthread_t tid = 0;
2096*eda14cbcSMatt Macy 	int pipefd[2];
2097*eda14cbcSMatt Macy 	int featureflags = 0;
2098*eda14cbcSMatt Macy 	FILE *fout;
2099*eda14cbcSMatt Macy 
2100*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2101*eda14cbcSMatt Macy 	    "cannot send '%s'"), zhp->zfs_name);
2102*eda14cbcSMatt Macy 
2103*eda14cbcSMatt Macy 	if (fromsnap && fromsnap[0] == '\0') {
2104*eda14cbcSMatt Macy 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2105*eda14cbcSMatt Macy 		    "zero-length incremental source"));
2106*eda14cbcSMatt Macy 		return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
2107*eda14cbcSMatt Macy 	}
2108*eda14cbcSMatt Macy 
2109*eda14cbcSMatt Macy 	if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
2110*eda14cbcSMatt Macy 		uint64_t version;
2111*eda14cbcSMatt Macy 		version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
2112*eda14cbcSMatt Macy 		if (version >= ZPL_VERSION_SA) {
2113*eda14cbcSMatt Macy 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
2114*eda14cbcSMatt Macy 		}
2115*eda14cbcSMatt Macy 	}
2116*eda14cbcSMatt Macy 
2117*eda14cbcSMatt Macy 	if (flags->holds)
2118*eda14cbcSMatt Macy 		featureflags |= DMU_BACKUP_FEATURE_HOLDS;
2119*eda14cbcSMatt Macy 
2120*eda14cbcSMatt Macy 	if (flags->replicate || flags->doall || flags->props ||
2121*eda14cbcSMatt Macy 	    flags->holds || flags->backup) {
2122*eda14cbcSMatt Macy 		char full_tosnap_name[ZFS_MAX_DATASET_NAME_LEN];
2123*eda14cbcSMatt Macy 		if (snprintf(full_tosnap_name, sizeof (full_tosnap_name),
2124*eda14cbcSMatt Macy 		    "%s@%s", zhp->zfs_name, tosnap) >=
2125*eda14cbcSMatt Macy 		    sizeof (full_tosnap_name)) {
2126*eda14cbcSMatt Macy 			err = EINVAL;
2127*eda14cbcSMatt Macy 			goto stderr_out;
2128*eda14cbcSMatt Macy 		}
2129*eda14cbcSMatt Macy 		zfs_handle_t *tosnap = zfs_open(zhp->zfs_hdl,
2130*eda14cbcSMatt Macy 		    full_tosnap_name, ZFS_TYPE_SNAPSHOT);
2131*eda14cbcSMatt Macy 		if (tosnap == NULL) {
2132*eda14cbcSMatt Macy 			err = -1;
2133*eda14cbcSMatt Macy 			goto err_out;
2134*eda14cbcSMatt Macy 		}
2135*eda14cbcSMatt Macy 		err = send_prelim_records(tosnap, fromsnap, outfd,
2136*eda14cbcSMatt Macy 		    flags->replicate || flags->props || flags->holds,
2137*eda14cbcSMatt Macy 		    flags->replicate, flags->verbosity > 0, flags->dryrun,
2138*eda14cbcSMatt Macy 		    flags->raw, flags->replicate, flags->backup, flags->holds,
2139*eda14cbcSMatt Macy 		    flags->props, flags->doall, &fss, &fsavl);
2140*eda14cbcSMatt Macy 		zfs_close(tosnap);
2141*eda14cbcSMatt Macy 		if (err != 0)
2142*eda14cbcSMatt Macy 			goto err_out;
2143*eda14cbcSMatt Macy 	}
2144*eda14cbcSMatt Macy 
2145*eda14cbcSMatt Macy 	/* dump each stream */
2146*eda14cbcSMatt Macy 	sdd.fromsnap = fromsnap;
2147*eda14cbcSMatt Macy 	sdd.tosnap = tosnap;
2148*eda14cbcSMatt Macy 	if (tid != 0)
2149*eda14cbcSMatt Macy 		sdd.outfd = pipefd[0];
2150*eda14cbcSMatt Macy 	else
2151*eda14cbcSMatt Macy 		sdd.outfd = outfd;
2152*eda14cbcSMatt Macy 	sdd.replicate = flags->replicate;
2153*eda14cbcSMatt Macy 	sdd.doall = flags->doall;
2154*eda14cbcSMatt Macy 	sdd.fromorigin = flags->fromorigin;
2155*eda14cbcSMatt Macy 	sdd.fss = fss;
2156*eda14cbcSMatt Macy 	sdd.fsavl = fsavl;
2157*eda14cbcSMatt Macy 	sdd.verbosity = flags->verbosity;
2158*eda14cbcSMatt Macy 	sdd.parsable = flags->parsable;
2159*eda14cbcSMatt Macy 	sdd.progress = flags->progress;
2160*eda14cbcSMatt Macy 	sdd.dryrun = flags->dryrun;
2161*eda14cbcSMatt Macy 	sdd.large_block = flags->largeblock;
2162*eda14cbcSMatt Macy 	sdd.embed_data = flags->embed_data;
2163*eda14cbcSMatt Macy 	sdd.compress = flags->compress;
2164*eda14cbcSMatt Macy 	sdd.raw = flags->raw;
2165*eda14cbcSMatt Macy 	sdd.holds = flags->holds;
2166*eda14cbcSMatt Macy 	sdd.filter_cb = filter_func;
2167*eda14cbcSMatt Macy 	sdd.filter_cb_arg = cb_arg;
2168*eda14cbcSMatt Macy 	if (debugnvp)
2169*eda14cbcSMatt Macy 		sdd.debugnv = *debugnvp;
2170*eda14cbcSMatt Macy 	if (sdd.verbosity != 0 && sdd.dryrun)
2171*eda14cbcSMatt Macy 		sdd.std_out = B_TRUE;
2172*eda14cbcSMatt Macy 	fout = sdd.std_out ? stdout : stderr;
2173*eda14cbcSMatt Macy 
2174*eda14cbcSMatt Macy 	/*
2175*eda14cbcSMatt Macy 	 * Some flags require that we place user holds on the datasets that are
2176*eda14cbcSMatt Macy 	 * being sent so they don't get destroyed during the send. We can skip
2177*eda14cbcSMatt Macy 	 * this step if the pool is imported read-only since the datasets cannot
2178*eda14cbcSMatt Macy 	 * be destroyed.
2179*eda14cbcSMatt Macy 	 */
2180*eda14cbcSMatt Macy 	if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2181*eda14cbcSMatt Macy 	    ZPOOL_PROP_READONLY, NULL) &&
2182*eda14cbcSMatt Macy 	    zfs_spa_version(zhp, &spa_version) == 0 &&
2183*eda14cbcSMatt Macy 	    spa_version >= SPA_VERSION_USERREFS &&
2184*eda14cbcSMatt Macy 	    (flags->doall || flags->replicate)) {
2185*eda14cbcSMatt Macy 		++holdseq;
2186*eda14cbcSMatt Macy 		(void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2187*eda14cbcSMatt Macy 		    ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2188*eda14cbcSMatt Macy 		sdd.cleanup_fd = open(ZFS_DEV, O_RDWR);
2189*eda14cbcSMatt Macy 		if (sdd.cleanup_fd < 0) {
2190*eda14cbcSMatt Macy 			err = errno;
2191*eda14cbcSMatt Macy 			goto stderr_out;
2192*eda14cbcSMatt Macy 		}
2193*eda14cbcSMatt Macy 		sdd.snapholds = fnvlist_alloc();
2194*eda14cbcSMatt Macy 	} else {
2195*eda14cbcSMatt Macy 		sdd.cleanup_fd = -1;
2196*eda14cbcSMatt Macy 		sdd.snapholds = NULL;
2197*eda14cbcSMatt Macy 	}
2198*eda14cbcSMatt Macy 
2199*eda14cbcSMatt Macy 	if (flags->verbosity != 0 || sdd.snapholds != NULL) {
2200*eda14cbcSMatt Macy 		/*
2201*eda14cbcSMatt Macy 		 * Do a verbose no-op dry run to get all the verbose output
2202*eda14cbcSMatt Macy 		 * or to gather snapshot hold's before generating any data,
2203*eda14cbcSMatt Macy 		 * then do a non-verbose real run to generate the streams.
2204*eda14cbcSMatt Macy 		 */
2205*eda14cbcSMatt Macy 		sdd.dryrun = B_TRUE;
2206*eda14cbcSMatt Macy 		err = dump_filesystems(zhp, &sdd);
2207*eda14cbcSMatt Macy 
2208*eda14cbcSMatt Macy 		if (err != 0)
2209*eda14cbcSMatt Macy 			goto stderr_out;
2210*eda14cbcSMatt Macy 
2211*eda14cbcSMatt Macy 		if (flags->verbosity != 0) {
2212*eda14cbcSMatt Macy 			if (flags->parsable) {
2213*eda14cbcSMatt Macy 				(void) fprintf(fout, "size\t%llu\n",
2214*eda14cbcSMatt Macy 				    (longlong_t)sdd.size);
2215*eda14cbcSMatt Macy 			} else {
2216*eda14cbcSMatt Macy 				char buf[16];
2217*eda14cbcSMatt Macy 				zfs_nicebytes(sdd.size, buf, sizeof (buf));
2218*eda14cbcSMatt Macy 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
2219*eda14cbcSMatt Macy 				    "total estimated size is %s\n"), buf);
2220*eda14cbcSMatt Macy 			}
2221*eda14cbcSMatt Macy 		}
2222*eda14cbcSMatt Macy 
2223*eda14cbcSMatt Macy 		/* Ensure no snaps found is treated as an error. */
2224*eda14cbcSMatt Macy 		if (!sdd.seento) {
2225*eda14cbcSMatt Macy 			err = ENOENT;
2226*eda14cbcSMatt Macy 			goto err_out;
2227*eda14cbcSMatt Macy 		}
2228*eda14cbcSMatt Macy 
2229*eda14cbcSMatt Macy 		/* Skip the second run if dryrun was requested. */
2230*eda14cbcSMatt Macy 		if (flags->dryrun)
2231*eda14cbcSMatt Macy 			goto err_out;
2232*eda14cbcSMatt Macy 
2233*eda14cbcSMatt Macy 		if (sdd.snapholds != NULL) {
2234*eda14cbcSMatt Macy 			err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2235*eda14cbcSMatt Macy 			if (err != 0)
2236*eda14cbcSMatt Macy 				goto stderr_out;
2237*eda14cbcSMatt Macy 
2238*eda14cbcSMatt Macy 			fnvlist_free(sdd.snapholds);
2239*eda14cbcSMatt Macy 			sdd.snapholds = NULL;
2240*eda14cbcSMatt Macy 		}
2241*eda14cbcSMatt Macy 
2242*eda14cbcSMatt Macy 		sdd.dryrun = B_FALSE;
2243*eda14cbcSMatt Macy 		sdd.verbosity = 0;
2244*eda14cbcSMatt Macy 	}
2245*eda14cbcSMatt Macy 
2246*eda14cbcSMatt Macy 	err = dump_filesystems(zhp, &sdd);
2247*eda14cbcSMatt Macy 	fsavl_destroy(fsavl);
2248*eda14cbcSMatt Macy 	nvlist_free(fss);
2249*eda14cbcSMatt Macy 
2250*eda14cbcSMatt Macy 	/* Ensure no snaps found is treated as an error. */
2251*eda14cbcSMatt Macy 	if (err == 0 && !sdd.seento)
2252*eda14cbcSMatt Macy 		err = ENOENT;
2253*eda14cbcSMatt Macy 
2254*eda14cbcSMatt Macy 	if (tid != 0) {
2255*eda14cbcSMatt Macy 		if (err != 0)
2256*eda14cbcSMatt Macy 			(void) pthread_cancel(tid);
2257*eda14cbcSMatt Macy 		(void) close(pipefd[0]);
2258*eda14cbcSMatt Macy 		(void) pthread_join(tid, NULL);
2259*eda14cbcSMatt Macy 	}
2260*eda14cbcSMatt Macy 
2261*eda14cbcSMatt Macy 	if (sdd.cleanup_fd != -1) {
2262*eda14cbcSMatt Macy 		VERIFY(0 == close(sdd.cleanup_fd));
2263*eda14cbcSMatt Macy 		sdd.cleanup_fd = -1;
2264*eda14cbcSMatt Macy 	}
2265*eda14cbcSMatt Macy 
2266*eda14cbcSMatt Macy 	if (!flags->dryrun && (flags->replicate || flags->doall ||
2267*eda14cbcSMatt Macy 	    flags->props || flags->backup || flags->holds)) {
2268*eda14cbcSMatt Macy 		/*
2269*eda14cbcSMatt Macy 		 * write final end record.  NB: want to do this even if
2270*eda14cbcSMatt Macy 		 * there was some error, because it might not be totally
2271*eda14cbcSMatt Macy 		 * failed.
2272*eda14cbcSMatt Macy 		 */
2273*eda14cbcSMatt Macy 		err = send_conclusion_record(outfd, NULL);
2274*eda14cbcSMatt Macy 		if (err != 0)
2275*eda14cbcSMatt Macy 			return (zfs_standard_error(zhp->zfs_hdl, err, errbuf));
2276*eda14cbcSMatt Macy 	}
2277*eda14cbcSMatt Macy 
2278*eda14cbcSMatt Macy 	return (err || sdd.err);
2279*eda14cbcSMatt Macy 
2280*eda14cbcSMatt Macy stderr_out:
2281*eda14cbcSMatt Macy 	err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2282*eda14cbcSMatt Macy err_out:
2283*eda14cbcSMatt Macy 	fsavl_destroy(fsavl);
2284*eda14cbcSMatt Macy 	nvlist_free(fss);
2285*eda14cbcSMatt Macy 	fnvlist_free(sdd.snapholds);
2286*eda14cbcSMatt Macy 
2287*eda14cbcSMatt Macy 	if (sdd.cleanup_fd != -1)
2288*eda14cbcSMatt Macy 		VERIFY(0 == close(sdd.cleanup_fd));
2289*eda14cbcSMatt Macy 	if (tid != 0) {
2290*eda14cbcSMatt Macy 		(void) pthread_cancel(tid);
2291*eda14cbcSMatt Macy 		(void) close(pipefd[0]);
2292*eda14cbcSMatt Macy 		(void) pthread_join(tid, NULL);
2293*eda14cbcSMatt Macy 	}
2294*eda14cbcSMatt Macy 	return (err);
2295*eda14cbcSMatt Macy }
2296*eda14cbcSMatt Macy 
2297*eda14cbcSMatt Macy static zfs_handle_t *
2298*eda14cbcSMatt Macy name_to_dir_handle(libzfs_handle_t *hdl, const char *snapname)
2299*eda14cbcSMatt Macy {
2300*eda14cbcSMatt Macy 	char dirname[ZFS_MAX_DATASET_NAME_LEN];
2301*eda14cbcSMatt Macy 	(void) strlcpy(dirname, snapname, ZFS_MAX_DATASET_NAME_LEN);
2302*eda14cbcSMatt Macy 	char *c = strchr(dirname, '@');
2303*eda14cbcSMatt Macy 	if (c != NULL)
2304*eda14cbcSMatt Macy 		*c = '\0';
2305*eda14cbcSMatt Macy 	return (zfs_open(hdl, dirname, ZFS_TYPE_DATASET));
2306*eda14cbcSMatt Macy }
2307*eda14cbcSMatt Macy 
2308*eda14cbcSMatt Macy /*
2309*eda14cbcSMatt Macy  * Returns B_TRUE if earlier is an earlier snapshot in later's timeline; either
2310*eda14cbcSMatt Macy  * an earlier snapshot in the same filesystem, or a snapshot before later's
2311*eda14cbcSMatt Macy  * origin, or it's origin's origin, etc.
2312*eda14cbcSMatt Macy  */
2313*eda14cbcSMatt Macy static boolean_t
2314*eda14cbcSMatt Macy snapshot_is_before(zfs_handle_t *earlier, zfs_handle_t *later)
2315*eda14cbcSMatt Macy {
2316*eda14cbcSMatt Macy 	boolean_t ret;
2317*eda14cbcSMatt Macy 	uint64_t later_txg =
2318*eda14cbcSMatt Macy 	    (later->zfs_type == ZFS_TYPE_FILESYSTEM ||
2319*eda14cbcSMatt Macy 	    later->zfs_type == ZFS_TYPE_VOLUME ?
2320*eda14cbcSMatt Macy 	    UINT64_MAX : zfs_prop_get_int(later, ZFS_PROP_CREATETXG));
2321*eda14cbcSMatt Macy 	uint64_t earlier_txg = zfs_prop_get_int(earlier, ZFS_PROP_CREATETXG);
2322*eda14cbcSMatt Macy 
2323*eda14cbcSMatt Macy 	if (earlier_txg >= later_txg)
2324*eda14cbcSMatt Macy 		return (B_FALSE);
2325*eda14cbcSMatt Macy 
2326*eda14cbcSMatt Macy 	zfs_handle_t *earlier_dir = name_to_dir_handle(earlier->zfs_hdl,
2327*eda14cbcSMatt Macy 	    earlier->zfs_name);
2328*eda14cbcSMatt Macy 	zfs_handle_t *later_dir = name_to_dir_handle(later->zfs_hdl,
2329*eda14cbcSMatt Macy 	    later->zfs_name);
2330*eda14cbcSMatt Macy 
2331*eda14cbcSMatt Macy 	if (strcmp(earlier_dir->zfs_name, later_dir->zfs_name) == 0) {
2332*eda14cbcSMatt Macy 		zfs_close(earlier_dir);
2333*eda14cbcSMatt Macy 		zfs_close(later_dir);
2334*eda14cbcSMatt Macy 		return (B_TRUE);
2335*eda14cbcSMatt Macy 	}
2336*eda14cbcSMatt Macy 
2337*eda14cbcSMatt Macy 	char clonename[ZFS_MAX_DATASET_NAME_LEN];
2338*eda14cbcSMatt Macy 	if (zfs_prop_get(later_dir, ZFS_PROP_ORIGIN, clonename,
2339*eda14cbcSMatt Macy 	    ZFS_MAX_DATASET_NAME_LEN, NULL, NULL, 0, B_TRUE) != 0) {
2340*eda14cbcSMatt Macy 		zfs_close(earlier_dir);
2341*eda14cbcSMatt Macy 		zfs_close(later_dir);
2342*eda14cbcSMatt Macy 		return (B_FALSE);
2343*eda14cbcSMatt Macy 	}
2344*eda14cbcSMatt Macy 
2345*eda14cbcSMatt Macy 	zfs_handle_t *origin = zfs_open(earlier->zfs_hdl, clonename,
2346*eda14cbcSMatt Macy 	    ZFS_TYPE_DATASET);
2347*eda14cbcSMatt Macy 	uint64_t origin_txg = zfs_prop_get_int(origin, ZFS_PROP_CREATETXG);
2348*eda14cbcSMatt Macy 
2349*eda14cbcSMatt Macy 	/*
2350*eda14cbcSMatt Macy 	 * If "earlier" is exactly the origin, then
2351*eda14cbcSMatt Macy 	 * snapshot_is_before(earlier, origin) will return false (because
2352*eda14cbcSMatt Macy 	 * they're the same).
2353*eda14cbcSMatt Macy 	 */
2354*eda14cbcSMatt Macy 	if (origin_txg == earlier_txg &&
2355*eda14cbcSMatt Macy 	    strcmp(origin->zfs_name, earlier->zfs_name) == 0) {
2356*eda14cbcSMatt Macy 		zfs_close(earlier_dir);
2357*eda14cbcSMatt Macy 		zfs_close(later_dir);
2358*eda14cbcSMatt Macy 		zfs_close(origin);
2359*eda14cbcSMatt Macy 		return (B_TRUE);
2360*eda14cbcSMatt Macy 	}
2361*eda14cbcSMatt Macy 	zfs_close(earlier_dir);
2362*eda14cbcSMatt Macy 	zfs_close(later_dir);
2363*eda14cbcSMatt Macy 
2364*eda14cbcSMatt Macy 	ret = snapshot_is_before(earlier, origin);
2365*eda14cbcSMatt Macy 	zfs_close(origin);
2366*eda14cbcSMatt Macy 	return (ret);
2367*eda14cbcSMatt Macy }
2368*eda14cbcSMatt Macy 
2369*eda14cbcSMatt Macy /*
2370*eda14cbcSMatt Macy  * The "zhp" argument is the handle of the dataset to send (typically a
2371*eda14cbcSMatt Macy  * snapshot).  The "from" argument is the full name of the snapshot or
2372*eda14cbcSMatt Macy  * bookmark that is the incremental source.
2373*eda14cbcSMatt Macy  */
2374*eda14cbcSMatt Macy int
2375*eda14cbcSMatt Macy zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
2376*eda14cbcSMatt Macy     const char *redactbook)
2377*eda14cbcSMatt Macy {
2378*eda14cbcSMatt Macy 	int err;
2379*eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2380*eda14cbcSMatt Macy 	char *name = zhp->zfs_name;
2381*eda14cbcSMatt Macy 	int orig_fd = fd;
2382*eda14cbcSMatt Macy 	pthread_t ptid;
2383*eda14cbcSMatt Macy 	progress_arg_t pa = { 0 };
2384*eda14cbcSMatt Macy 
2385*eda14cbcSMatt Macy 	char errbuf[1024];
2386*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2387*eda14cbcSMatt Macy 	    "warning: cannot send '%s'"), name);
2388*eda14cbcSMatt Macy 
2389*eda14cbcSMatt Macy 	if (from != NULL && strchr(from, '@')) {
2390*eda14cbcSMatt Macy 		zfs_handle_t *from_zhp = zfs_open(hdl, from,
2391*eda14cbcSMatt Macy 		    ZFS_TYPE_DATASET);
2392*eda14cbcSMatt Macy 		if (from_zhp == NULL)
2393*eda14cbcSMatt Macy 			return (-1);
2394*eda14cbcSMatt Macy 		if (!snapshot_is_before(from_zhp, zhp)) {
2395*eda14cbcSMatt Macy 			zfs_close(from_zhp);
2396*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2397*eda14cbcSMatt Macy 			    "not an earlier snapshot from the same fs"));
2398*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2399*eda14cbcSMatt Macy 		}
2400*eda14cbcSMatt Macy 		zfs_close(from_zhp);
2401*eda14cbcSMatt Macy 	}
2402*eda14cbcSMatt Macy 
2403*eda14cbcSMatt Macy 	if (redactbook != NULL) {
2404*eda14cbcSMatt Macy 		char bookname[ZFS_MAX_DATASET_NAME_LEN];
2405*eda14cbcSMatt Macy 		nvlist_t *redact_snaps;
2406*eda14cbcSMatt Macy 		zfs_handle_t *book_zhp;
2407*eda14cbcSMatt Macy 		char *at, *pound;
2408*eda14cbcSMatt Macy 		int dsnamelen;
2409*eda14cbcSMatt Macy 
2410*eda14cbcSMatt Macy 		pound = strchr(redactbook, '#');
2411*eda14cbcSMatt Macy 		if (pound != NULL)
2412*eda14cbcSMatt Macy 			redactbook = pound + 1;
2413*eda14cbcSMatt Macy 		at = strchr(name, '@');
2414*eda14cbcSMatt Macy 		if (at == NULL) {
2415*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2416*eda14cbcSMatt Macy 			    "cannot do a redacted send to a filesystem"));
2417*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2418*eda14cbcSMatt Macy 		}
2419*eda14cbcSMatt Macy 		dsnamelen = at - name;
2420*eda14cbcSMatt Macy 		if (snprintf(bookname, sizeof (bookname), "%.*s#%s",
2421*eda14cbcSMatt Macy 		    dsnamelen, name, redactbook)
2422*eda14cbcSMatt Macy 		    >= sizeof (bookname)) {
2423*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2424*eda14cbcSMatt Macy 			    "invalid bookmark name"));
2425*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2426*eda14cbcSMatt Macy 		}
2427*eda14cbcSMatt Macy 		book_zhp = zfs_open(hdl, bookname, ZFS_TYPE_BOOKMARK);
2428*eda14cbcSMatt Macy 		if (book_zhp == NULL)
2429*eda14cbcSMatt Macy 			return (-1);
2430*eda14cbcSMatt Macy 		if (nvlist_lookup_nvlist(book_zhp->zfs_props,
2431*eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2432*eda14cbcSMatt Macy 		    &redact_snaps) != 0 || redact_snaps == NULL) {
2433*eda14cbcSMatt Macy 			zfs_close(book_zhp);
2434*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2435*eda14cbcSMatt Macy 			    "not a redaction bookmark"));
2436*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2437*eda14cbcSMatt Macy 		}
2438*eda14cbcSMatt Macy 		zfs_close(book_zhp);
2439*eda14cbcSMatt Macy 	}
2440*eda14cbcSMatt Macy 
2441*eda14cbcSMatt Macy 	/*
2442*eda14cbcSMatt Macy 	 * Send fs properties
2443*eda14cbcSMatt Macy 	 */
2444*eda14cbcSMatt Macy 	if (flags->props || flags->holds || flags->backup) {
2445*eda14cbcSMatt Macy 		/*
2446*eda14cbcSMatt Macy 		 * Note: the header generated by send_prelim_records()
2447*eda14cbcSMatt Macy 		 * assumes that the incremental source is in the same
2448*eda14cbcSMatt Macy 		 * filesystem/volume as the target (which is a requirement
2449*eda14cbcSMatt Macy 		 * when doing "zfs send -R").  But that isn't always the
2450*eda14cbcSMatt Macy 		 * case here (e.g. send from snap in origin, or send from
2451*eda14cbcSMatt Macy 		 * bookmark).  We pass from=NULL, which will omit this
2452*eda14cbcSMatt Macy 		 * information from the prelim records; it isn't used
2453*eda14cbcSMatt Macy 		 * when receiving this type of stream.
2454*eda14cbcSMatt Macy 		 */
2455*eda14cbcSMatt Macy 		err = send_prelim_records(zhp, NULL, fd, B_TRUE, B_FALSE,
2456*eda14cbcSMatt Macy 		    flags->verbosity > 0, flags->dryrun, flags->raw,
2457*eda14cbcSMatt Macy 		    flags->replicate, flags->backup, flags->holds,
2458*eda14cbcSMatt Macy 		    flags->props, flags->doall, NULL, NULL);
2459*eda14cbcSMatt Macy 		if (err != 0)
2460*eda14cbcSMatt Macy 			return (err);
2461*eda14cbcSMatt Macy 	}
2462*eda14cbcSMatt Macy 
2463*eda14cbcSMatt Macy 	/*
2464*eda14cbcSMatt Macy 	 * Perform size estimate if verbose was specified.
2465*eda14cbcSMatt Macy 	 */
2466*eda14cbcSMatt Macy 	if (flags->verbosity != 0) {
2467*eda14cbcSMatt Macy 		err = estimate_size(zhp, from, fd, flags, 0, 0, 0, redactbook,
2468*eda14cbcSMatt Macy 		    errbuf);
2469*eda14cbcSMatt Macy 		if (err != 0)
2470*eda14cbcSMatt Macy 			return (err);
2471*eda14cbcSMatt Macy 	}
2472*eda14cbcSMatt Macy 
2473*eda14cbcSMatt Macy 	if (flags->dryrun)
2474*eda14cbcSMatt Macy 		return (0);
2475*eda14cbcSMatt Macy 
2476*eda14cbcSMatt Macy 	/*
2477*eda14cbcSMatt Macy 	 * If progress reporting is requested, spawn a new thread to poll
2478*eda14cbcSMatt Macy 	 * ZFS_IOC_SEND_PROGRESS at a regular interval.
2479*eda14cbcSMatt Macy 	 */
2480*eda14cbcSMatt Macy 	if (flags->progress) {
2481*eda14cbcSMatt Macy 		pa.pa_zhp = zhp;
2482*eda14cbcSMatt Macy 		pa.pa_fd = fd;
2483*eda14cbcSMatt Macy 		pa.pa_parsable = flags->parsable;
2484*eda14cbcSMatt Macy 		pa.pa_estimate = B_FALSE;
2485*eda14cbcSMatt Macy 		pa.pa_verbosity = flags->verbosity;
2486*eda14cbcSMatt Macy 
2487*eda14cbcSMatt Macy 		err = pthread_create(&ptid, NULL,
2488*eda14cbcSMatt Macy 		    send_progress_thread, &pa);
2489*eda14cbcSMatt Macy 		if (err != 0) {
2490*eda14cbcSMatt Macy 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
2491*eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl,
2492*eda14cbcSMatt Macy 			    EZFS_THREADCREATEFAILED, errbuf));
2493*eda14cbcSMatt Macy 		}
2494*eda14cbcSMatt Macy 	}
2495*eda14cbcSMatt Macy 
2496*eda14cbcSMatt Macy 	err = lzc_send_redacted(name, from, fd,
2497*eda14cbcSMatt Macy 	    lzc_flags_from_sendflags(flags), redactbook);
2498*eda14cbcSMatt Macy 
2499*eda14cbcSMatt Macy 	if (flags->progress) {
2500*eda14cbcSMatt Macy 		void *status = NULL;
2501*eda14cbcSMatt Macy 		if (err != 0)
2502*eda14cbcSMatt Macy 			(void) pthread_cancel(ptid);
2503*eda14cbcSMatt Macy 		(void) pthread_join(ptid, &status);
2504*eda14cbcSMatt Macy 		int error = (int)(uintptr_t)status;
2505*eda14cbcSMatt Macy 		if (error != 0 && status != PTHREAD_CANCELED) {
2506*eda14cbcSMatt Macy 			char errbuf[1024];
2507*eda14cbcSMatt Macy 			(void) snprintf(errbuf, sizeof (errbuf),
2508*eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "progress thread exited "
2509*eda14cbcSMatt Macy 			    "nonzero"));
2510*eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, error, errbuf));
2511*eda14cbcSMatt Macy 		}
2512*eda14cbcSMatt Macy 	}
2513*eda14cbcSMatt Macy 
2514*eda14cbcSMatt Macy 	if (flags->props || flags->holds || flags->backup) {
2515*eda14cbcSMatt Macy 		/* Write the final end record. */
2516*eda14cbcSMatt Macy 		err = send_conclusion_record(orig_fd, NULL);
2517*eda14cbcSMatt Macy 		if (err != 0)
2518*eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, err, errbuf));
2519*eda14cbcSMatt Macy 	}
2520*eda14cbcSMatt Macy 	if (err != 0) {
2521*eda14cbcSMatt Macy 		switch (errno) {
2522*eda14cbcSMatt Macy 		case EXDEV:
2523*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2524*eda14cbcSMatt Macy 			    "not an earlier snapshot from the same fs"));
2525*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2526*eda14cbcSMatt Macy 
2527*eda14cbcSMatt Macy 		case ENOENT:
2528*eda14cbcSMatt Macy 		case ESRCH:
2529*eda14cbcSMatt Macy 			if (lzc_exists(name)) {
2530*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2531*eda14cbcSMatt Macy 				    "incremental source (%s) does not exist"),
2532*eda14cbcSMatt Macy 				    from);
2533*eda14cbcSMatt Macy 			}
2534*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2535*eda14cbcSMatt Macy 
2536*eda14cbcSMatt Macy 		case EACCES:
2537*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2538*eda14cbcSMatt Macy 			    "dataset key must be loaded"));
2539*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2540*eda14cbcSMatt Macy 
2541*eda14cbcSMatt Macy 		case EBUSY:
2542*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2543*eda14cbcSMatt Macy 			    "target is busy; if a filesystem, "
2544*eda14cbcSMatt Macy 			    "it must not be mounted"));
2545*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BUSY, errbuf));
2546*eda14cbcSMatt Macy 
2547*eda14cbcSMatt Macy 		case EDQUOT:
2548*eda14cbcSMatt Macy 		case EFAULT:
2549*eda14cbcSMatt Macy 		case EFBIG:
2550*eda14cbcSMatt Macy 		case EINVAL:
2551*eda14cbcSMatt Macy 		case EIO:
2552*eda14cbcSMatt Macy 		case ENOLINK:
2553*eda14cbcSMatt Macy 		case ENOSPC:
2554*eda14cbcSMatt Macy 		case ENOSTR:
2555*eda14cbcSMatt Macy 		case ENXIO:
2556*eda14cbcSMatt Macy 		case EPIPE:
2557*eda14cbcSMatt Macy 		case ERANGE:
2558*eda14cbcSMatt Macy 		case EROFS:
2559*eda14cbcSMatt Macy 			zfs_error_aux(hdl, strerror(errno));
2560*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2561*eda14cbcSMatt Macy 
2562*eda14cbcSMatt Macy 		default:
2563*eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, errno, errbuf));
2564*eda14cbcSMatt Macy 		}
2565*eda14cbcSMatt Macy 	}
2566*eda14cbcSMatt Macy 	return (err != 0);
2567*eda14cbcSMatt Macy }
2568*eda14cbcSMatt Macy 
2569*eda14cbcSMatt Macy /*
2570*eda14cbcSMatt Macy  * Routines specific to "zfs recv"
2571*eda14cbcSMatt Macy  */
2572*eda14cbcSMatt Macy 
2573*eda14cbcSMatt Macy static int
2574*eda14cbcSMatt Macy recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2575*eda14cbcSMatt Macy     boolean_t byteswap, zio_cksum_t *zc)
2576*eda14cbcSMatt Macy {
2577*eda14cbcSMatt Macy 	char *cp = buf;
2578*eda14cbcSMatt Macy 	int rv;
2579*eda14cbcSMatt Macy 	int len = ilen;
2580*eda14cbcSMatt Macy 
2581*eda14cbcSMatt Macy 	assert(ilen <= SPA_MAXBLOCKSIZE);
2582*eda14cbcSMatt Macy 
2583*eda14cbcSMatt Macy 	do {
2584*eda14cbcSMatt Macy 		rv = read(fd, cp, len);
2585*eda14cbcSMatt Macy 		cp += rv;
2586*eda14cbcSMatt Macy 		len -= rv;
2587*eda14cbcSMatt Macy 	} while (rv > 0);
2588*eda14cbcSMatt Macy 
2589*eda14cbcSMatt Macy 	if (rv < 0 || len != 0) {
2590*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2591*eda14cbcSMatt Macy 		    "failed to read from stream"));
2592*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2593*eda14cbcSMatt Macy 		    "cannot receive")));
2594*eda14cbcSMatt Macy 	}
2595*eda14cbcSMatt Macy 
2596*eda14cbcSMatt Macy 	if (zc) {
2597*eda14cbcSMatt Macy 		if (byteswap)
2598*eda14cbcSMatt Macy 			fletcher_4_incremental_byteswap(buf, ilen, zc);
2599*eda14cbcSMatt Macy 		else
2600*eda14cbcSMatt Macy 			fletcher_4_incremental_native(buf, ilen, zc);
2601*eda14cbcSMatt Macy 	}
2602*eda14cbcSMatt Macy 	return (0);
2603*eda14cbcSMatt Macy }
2604*eda14cbcSMatt Macy 
2605*eda14cbcSMatt Macy static int
2606*eda14cbcSMatt Macy recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2607*eda14cbcSMatt Macy     boolean_t byteswap, zio_cksum_t *zc)
2608*eda14cbcSMatt Macy {
2609*eda14cbcSMatt Macy 	char *buf;
2610*eda14cbcSMatt Macy 	int err;
2611*eda14cbcSMatt Macy 
2612*eda14cbcSMatt Macy 	buf = zfs_alloc(hdl, len);
2613*eda14cbcSMatt Macy 	if (buf == NULL)
2614*eda14cbcSMatt Macy 		return (ENOMEM);
2615*eda14cbcSMatt Macy 
2616*eda14cbcSMatt Macy 	err = recv_read(hdl, fd, buf, len, byteswap, zc);
2617*eda14cbcSMatt Macy 	if (err != 0) {
2618*eda14cbcSMatt Macy 		free(buf);
2619*eda14cbcSMatt Macy 		return (err);
2620*eda14cbcSMatt Macy 	}
2621*eda14cbcSMatt Macy 
2622*eda14cbcSMatt Macy 	err = nvlist_unpack(buf, len, nvp, 0);
2623*eda14cbcSMatt Macy 	free(buf);
2624*eda14cbcSMatt Macy 	if (err != 0) {
2625*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2626*eda14cbcSMatt Macy 		    "stream (malformed nvlist)"));
2627*eda14cbcSMatt Macy 		return (EINVAL);
2628*eda14cbcSMatt Macy 	}
2629*eda14cbcSMatt Macy 	return (0);
2630*eda14cbcSMatt Macy }
2631*eda14cbcSMatt Macy 
2632*eda14cbcSMatt Macy /*
2633*eda14cbcSMatt Macy  * Returns the grand origin (origin of origin of origin...) of a given handle.
2634*eda14cbcSMatt Macy  * If this dataset is not a clone, it simply returns a copy of the original
2635*eda14cbcSMatt Macy  * handle.
2636*eda14cbcSMatt Macy  */
2637*eda14cbcSMatt Macy static zfs_handle_t *
2638*eda14cbcSMatt Macy recv_open_grand_origin(zfs_handle_t *zhp)
2639*eda14cbcSMatt Macy {
2640*eda14cbcSMatt Macy 	char origin[ZFS_MAX_DATASET_NAME_LEN];
2641*eda14cbcSMatt Macy 	zprop_source_t src;
2642*eda14cbcSMatt Macy 	zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2643*eda14cbcSMatt Macy 
2644*eda14cbcSMatt Macy 	while (ozhp != NULL) {
2645*eda14cbcSMatt Macy 		if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2646*eda14cbcSMatt Macy 		    sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2647*eda14cbcSMatt Macy 			break;
2648*eda14cbcSMatt Macy 
2649*eda14cbcSMatt Macy 		(void) zfs_close(ozhp);
2650*eda14cbcSMatt Macy 		ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2651*eda14cbcSMatt Macy 	}
2652*eda14cbcSMatt Macy 
2653*eda14cbcSMatt Macy 	return (ozhp);
2654*eda14cbcSMatt Macy }
2655*eda14cbcSMatt Macy 
2656*eda14cbcSMatt Macy static int
2657*eda14cbcSMatt Macy recv_rename_impl(zfs_handle_t *zhp, const char *name, const char *newname)
2658*eda14cbcSMatt Macy {
2659*eda14cbcSMatt Macy 	int err;
2660*eda14cbcSMatt Macy 	zfs_handle_t *ozhp = NULL;
2661*eda14cbcSMatt Macy 
2662*eda14cbcSMatt Macy 	/*
2663*eda14cbcSMatt Macy 	 * Attempt to rename the dataset. If it fails with EACCES we have
2664*eda14cbcSMatt Macy 	 * attempted to rename the dataset outside of its encryption root.
2665*eda14cbcSMatt Macy 	 * Force the dataset to become an encryption root and try again.
2666*eda14cbcSMatt Macy 	 */
2667*eda14cbcSMatt Macy 	err = lzc_rename(name, newname);
2668*eda14cbcSMatt Macy 	if (err == EACCES) {
2669*eda14cbcSMatt Macy 		ozhp = recv_open_grand_origin(zhp);
2670*eda14cbcSMatt Macy 		if (ozhp == NULL) {
2671*eda14cbcSMatt Macy 			err = ENOENT;
2672*eda14cbcSMatt Macy 			goto out;
2673*eda14cbcSMatt Macy 		}
2674*eda14cbcSMatt Macy 
2675*eda14cbcSMatt Macy 		err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2676*eda14cbcSMatt Macy 		    NULL, NULL, 0);
2677*eda14cbcSMatt Macy 		if (err != 0)
2678*eda14cbcSMatt Macy 			goto out;
2679*eda14cbcSMatt Macy 
2680*eda14cbcSMatt Macy 		err = lzc_rename(name, newname);
2681*eda14cbcSMatt Macy 	}
2682*eda14cbcSMatt Macy 
2683*eda14cbcSMatt Macy out:
2684*eda14cbcSMatt Macy 	if (ozhp != NULL)
2685*eda14cbcSMatt Macy 		zfs_close(ozhp);
2686*eda14cbcSMatt Macy 	return (err);
2687*eda14cbcSMatt Macy }
2688*eda14cbcSMatt Macy 
2689*eda14cbcSMatt Macy static int
2690*eda14cbcSMatt Macy recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2691*eda14cbcSMatt Macy     int baselen, char *newname, recvflags_t *flags)
2692*eda14cbcSMatt Macy {
2693*eda14cbcSMatt Macy 	static int seq;
2694*eda14cbcSMatt Macy 	int err;
2695*eda14cbcSMatt Macy 	prop_changelist_t *clp = NULL;
2696*eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL;
2697*eda14cbcSMatt Macy 
2698*eda14cbcSMatt Macy 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2699*eda14cbcSMatt Macy 	if (zhp == NULL) {
2700*eda14cbcSMatt Macy 		err = -1;
2701*eda14cbcSMatt Macy 		goto out;
2702*eda14cbcSMatt Macy 	}
2703*eda14cbcSMatt Macy 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2704*eda14cbcSMatt Macy 	    flags->force ? MS_FORCE : 0);
2705*eda14cbcSMatt Macy 	if (clp == NULL) {
2706*eda14cbcSMatt Macy 		err = -1;
2707*eda14cbcSMatt Macy 		goto out;
2708*eda14cbcSMatt Macy 	}
2709*eda14cbcSMatt Macy 	err = changelist_prefix(clp);
2710*eda14cbcSMatt Macy 	if (err)
2711*eda14cbcSMatt Macy 		goto out;
2712*eda14cbcSMatt Macy 
2713*eda14cbcSMatt Macy 	if (tryname) {
2714*eda14cbcSMatt Macy 		(void) strcpy(newname, tryname);
2715*eda14cbcSMatt Macy 		if (flags->verbose) {
2716*eda14cbcSMatt Macy 			(void) printf("attempting rename %s to %s\n",
2717*eda14cbcSMatt Macy 			    name, newname);
2718*eda14cbcSMatt Macy 		}
2719*eda14cbcSMatt Macy 		err = recv_rename_impl(zhp, name, newname);
2720*eda14cbcSMatt Macy 		if (err == 0)
2721*eda14cbcSMatt Macy 			changelist_rename(clp, name, tryname);
2722*eda14cbcSMatt Macy 	} else {
2723*eda14cbcSMatt Macy 		err = ENOENT;
2724*eda14cbcSMatt Macy 	}
2725*eda14cbcSMatt Macy 
2726*eda14cbcSMatt Macy 	if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2727*eda14cbcSMatt Macy 		seq++;
2728*eda14cbcSMatt Macy 
2729*eda14cbcSMatt Macy 		(void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2730*eda14cbcSMatt Macy 		    "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2731*eda14cbcSMatt Macy 
2732*eda14cbcSMatt Macy 		if (flags->verbose) {
2733*eda14cbcSMatt Macy 			(void) printf("failed - trying rename %s to %s\n",
2734*eda14cbcSMatt Macy 			    name, newname);
2735*eda14cbcSMatt Macy 		}
2736*eda14cbcSMatt Macy 		err = recv_rename_impl(zhp, name, newname);
2737*eda14cbcSMatt Macy 		if (err == 0)
2738*eda14cbcSMatt Macy 			changelist_rename(clp, name, newname);
2739*eda14cbcSMatt Macy 		if (err && flags->verbose) {
2740*eda14cbcSMatt Macy 			(void) printf("failed (%u) - "
2741*eda14cbcSMatt Macy 			    "will try again on next pass\n", errno);
2742*eda14cbcSMatt Macy 		}
2743*eda14cbcSMatt Macy 		err = EAGAIN;
2744*eda14cbcSMatt Macy 	} else if (flags->verbose) {
2745*eda14cbcSMatt Macy 		if (err == 0)
2746*eda14cbcSMatt Macy 			(void) printf("success\n");
2747*eda14cbcSMatt Macy 		else
2748*eda14cbcSMatt Macy 			(void) printf("failed (%u)\n", errno);
2749*eda14cbcSMatt Macy 	}
2750*eda14cbcSMatt Macy 
2751*eda14cbcSMatt Macy 	(void) changelist_postfix(clp);
2752*eda14cbcSMatt Macy 
2753*eda14cbcSMatt Macy out:
2754*eda14cbcSMatt Macy 	if (clp != NULL)
2755*eda14cbcSMatt Macy 		changelist_free(clp);
2756*eda14cbcSMatt Macy 	if (zhp != NULL)
2757*eda14cbcSMatt Macy 		zfs_close(zhp);
2758*eda14cbcSMatt Macy 
2759*eda14cbcSMatt Macy 	return (err);
2760*eda14cbcSMatt Macy }
2761*eda14cbcSMatt Macy 
2762*eda14cbcSMatt Macy static int
2763*eda14cbcSMatt Macy recv_promote(libzfs_handle_t *hdl, const char *fsname,
2764*eda14cbcSMatt Macy     const char *origin_fsname, recvflags_t *flags)
2765*eda14cbcSMatt Macy {
2766*eda14cbcSMatt Macy 	int err;
2767*eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
2768*eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL, *ozhp = NULL;
2769*eda14cbcSMatt Macy 
2770*eda14cbcSMatt Macy 	if (flags->verbose)
2771*eda14cbcSMatt Macy 		(void) printf("promoting %s\n", fsname);
2772*eda14cbcSMatt Macy 
2773*eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
2774*eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
2775*eda14cbcSMatt Macy 
2776*eda14cbcSMatt Macy 	/*
2777*eda14cbcSMatt Macy 	 * Attempt to promote the dataset. If it fails with EACCES the
2778*eda14cbcSMatt Macy 	 * promotion would cause this dataset to leave its encryption root.
2779*eda14cbcSMatt Macy 	 * Force the origin to become an encryption root and try again.
2780*eda14cbcSMatt Macy 	 */
2781*eda14cbcSMatt Macy 	err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2782*eda14cbcSMatt Macy 	if (err == EACCES) {
2783*eda14cbcSMatt Macy 		zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2784*eda14cbcSMatt Macy 		if (zhp == NULL) {
2785*eda14cbcSMatt Macy 			err = -1;
2786*eda14cbcSMatt Macy 			goto out;
2787*eda14cbcSMatt Macy 		}
2788*eda14cbcSMatt Macy 
2789*eda14cbcSMatt Macy 		ozhp = recv_open_grand_origin(zhp);
2790*eda14cbcSMatt Macy 		if (ozhp == NULL) {
2791*eda14cbcSMatt Macy 			err = -1;
2792*eda14cbcSMatt Macy 			goto out;
2793*eda14cbcSMatt Macy 		}
2794*eda14cbcSMatt Macy 
2795*eda14cbcSMatt Macy 		err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2796*eda14cbcSMatt Macy 		    NULL, NULL, 0);
2797*eda14cbcSMatt Macy 		if (err != 0)
2798*eda14cbcSMatt Macy 			goto out;
2799*eda14cbcSMatt Macy 
2800*eda14cbcSMatt Macy 		err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2801*eda14cbcSMatt Macy 	}
2802*eda14cbcSMatt Macy 
2803*eda14cbcSMatt Macy out:
2804*eda14cbcSMatt Macy 	if (zhp != NULL)
2805*eda14cbcSMatt Macy 		zfs_close(zhp);
2806*eda14cbcSMatt Macy 	if (ozhp != NULL)
2807*eda14cbcSMatt Macy 		zfs_close(ozhp);
2808*eda14cbcSMatt Macy 
2809*eda14cbcSMatt Macy 	return (err);
2810*eda14cbcSMatt Macy }
2811*eda14cbcSMatt Macy 
2812*eda14cbcSMatt Macy static int
2813*eda14cbcSMatt Macy recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2814*eda14cbcSMatt Macy     char *newname, recvflags_t *flags)
2815*eda14cbcSMatt Macy {
2816*eda14cbcSMatt Macy 	int err = 0;
2817*eda14cbcSMatt Macy 	prop_changelist_t *clp;
2818*eda14cbcSMatt Macy 	zfs_handle_t *zhp;
2819*eda14cbcSMatt Macy 	boolean_t defer = B_FALSE;
2820*eda14cbcSMatt Macy 	int spa_version;
2821*eda14cbcSMatt Macy 
2822*eda14cbcSMatt Macy 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2823*eda14cbcSMatt Macy 	if (zhp == NULL)
2824*eda14cbcSMatt Macy 		return (-1);
2825*eda14cbcSMatt Macy 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2826*eda14cbcSMatt Macy 	    flags->force ? MS_FORCE : 0);
2827*eda14cbcSMatt Macy 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2828*eda14cbcSMatt Macy 	    zfs_spa_version(zhp, &spa_version) == 0 &&
2829*eda14cbcSMatt Macy 	    spa_version >= SPA_VERSION_USERREFS)
2830*eda14cbcSMatt Macy 		defer = B_TRUE;
2831*eda14cbcSMatt Macy 	zfs_close(zhp);
2832*eda14cbcSMatt Macy 	if (clp == NULL)
2833*eda14cbcSMatt Macy 		return (-1);
2834*eda14cbcSMatt Macy 	err = changelist_prefix(clp);
2835*eda14cbcSMatt Macy 	if (err)
2836*eda14cbcSMatt Macy 		return (err);
2837*eda14cbcSMatt Macy 
2838*eda14cbcSMatt Macy 	if (flags->verbose)
2839*eda14cbcSMatt Macy 		(void) printf("attempting destroy %s\n", name);
2840*eda14cbcSMatt Macy 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2841*eda14cbcSMatt Macy 		nvlist_t *nv = fnvlist_alloc();
2842*eda14cbcSMatt Macy 		fnvlist_add_boolean(nv, name);
2843*eda14cbcSMatt Macy 		err = lzc_destroy_snaps(nv, defer, NULL);
2844*eda14cbcSMatt Macy 		fnvlist_free(nv);
2845*eda14cbcSMatt Macy 	} else {
2846*eda14cbcSMatt Macy 		err = lzc_destroy(name);
2847*eda14cbcSMatt Macy 	}
2848*eda14cbcSMatt Macy 	if (err == 0) {
2849*eda14cbcSMatt Macy 		if (flags->verbose)
2850*eda14cbcSMatt Macy 			(void) printf("success\n");
2851*eda14cbcSMatt Macy 		changelist_remove(clp, name);
2852*eda14cbcSMatt Macy 	}
2853*eda14cbcSMatt Macy 
2854*eda14cbcSMatt Macy 	(void) changelist_postfix(clp);
2855*eda14cbcSMatt Macy 	changelist_free(clp);
2856*eda14cbcSMatt Macy 
2857*eda14cbcSMatt Macy 	/*
2858*eda14cbcSMatt Macy 	 * Deferred destroy might destroy the snapshot or only mark it to be
2859*eda14cbcSMatt Macy 	 * destroyed later, and it returns success in either case.
2860*eda14cbcSMatt Macy 	 */
2861*eda14cbcSMatt Macy 	if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2862*eda14cbcSMatt Macy 	    ZFS_TYPE_SNAPSHOT))) {
2863*eda14cbcSMatt Macy 		err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2864*eda14cbcSMatt Macy 	}
2865*eda14cbcSMatt Macy 
2866*eda14cbcSMatt Macy 	return (err);
2867*eda14cbcSMatt Macy }
2868*eda14cbcSMatt Macy 
2869*eda14cbcSMatt Macy typedef struct guid_to_name_data {
2870*eda14cbcSMatt Macy 	uint64_t guid;
2871*eda14cbcSMatt Macy 	boolean_t bookmark_ok;
2872*eda14cbcSMatt Macy 	char *name;
2873*eda14cbcSMatt Macy 	char *skip;
2874*eda14cbcSMatt Macy 	uint64_t *redact_snap_guids;
2875*eda14cbcSMatt Macy 	uint64_t num_redact_snaps;
2876*eda14cbcSMatt Macy } guid_to_name_data_t;
2877*eda14cbcSMatt Macy 
2878*eda14cbcSMatt Macy static boolean_t
2879*eda14cbcSMatt Macy redact_snaps_match(zfs_handle_t *zhp, guid_to_name_data_t *gtnd)
2880*eda14cbcSMatt Macy {
2881*eda14cbcSMatt Macy 	uint64_t *bmark_snaps;
2882*eda14cbcSMatt Macy 	uint_t bmark_num_snaps;
2883*eda14cbcSMatt Macy 	nvlist_t *nvl;
2884*eda14cbcSMatt Macy 	if (zhp->zfs_type != ZFS_TYPE_BOOKMARK)
2885*eda14cbcSMatt Macy 		return (B_FALSE);
2886*eda14cbcSMatt Macy 
2887*eda14cbcSMatt Macy 	nvl = fnvlist_lookup_nvlist(zhp->zfs_props,
2888*eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
2889*eda14cbcSMatt Macy 	bmark_snaps = fnvlist_lookup_uint64_array(nvl, ZPROP_VALUE,
2890*eda14cbcSMatt Macy 	    &bmark_num_snaps);
2891*eda14cbcSMatt Macy 	if (bmark_num_snaps != gtnd->num_redact_snaps)
2892*eda14cbcSMatt Macy 		return (B_FALSE);
2893*eda14cbcSMatt Macy 	int i = 0;
2894*eda14cbcSMatt Macy 	for (; i < bmark_num_snaps; i++) {
2895*eda14cbcSMatt Macy 		int j = 0;
2896*eda14cbcSMatt Macy 		for (; j < bmark_num_snaps; j++) {
2897*eda14cbcSMatt Macy 			if (bmark_snaps[i] == gtnd->redact_snap_guids[j])
2898*eda14cbcSMatt Macy 				break;
2899*eda14cbcSMatt Macy 		}
2900*eda14cbcSMatt Macy 		if (j == bmark_num_snaps)
2901*eda14cbcSMatt Macy 			break;
2902*eda14cbcSMatt Macy 	}
2903*eda14cbcSMatt Macy 	return (i == bmark_num_snaps);
2904*eda14cbcSMatt Macy }
2905*eda14cbcSMatt Macy 
2906*eda14cbcSMatt Macy static int
2907*eda14cbcSMatt Macy guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2908*eda14cbcSMatt Macy {
2909*eda14cbcSMatt Macy 	guid_to_name_data_t *gtnd = arg;
2910*eda14cbcSMatt Macy 	const char *slash;
2911*eda14cbcSMatt Macy 	int err;
2912*eda14cbcSMatt Macy 
2913*eda14cbcSMatt Macy 	if (gtnd->skip != NULL &&
2914*eda14cbcSMatt Macy 	    (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2915*eda14cbcSMatt Macy 	    strcmp(slash + 1, gtnd->skip) == 0) {
2916*eda14cbcSMatt Macy 		zfs_close(zhp);
2917*eda14cbcSMatt Macy 		return (0);
2918*eda14cbcSMatt Macy 	}
2919*eda14cbcSMatt Macy 
2920*eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid &&
2921*eda14cbcSMatt Macy 	    (gtnd->num_redact_snaps == -1 || redact_snaps_match(zhp, gtnd))) {
2922*eda14cbcSMatt Macy 		(void) strcpy(gtnd->name, zhp->zfs_name);
2923*eda14cbcSMatt Macy 		zfs_close(zhp);
2924*eda14cbcSMatt Macy 		return (EEXIST);
2925*eda14cbcSMatt Macy 	}
2926*eda14cbcSMatt Macy 
2927*eda14cbcSMatt Macy 	err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2928*eda14cbcSMatt Macy 	if (err != EEXIST && gtnd->bookmark_ok)
2929*eda14cbcSMatt Macy 		err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2930*eda14cbcSMatt Macy 	zfs_close(zhp);
2931*eda14cbcSMatt Macy 	return (err);
2932*eda14cbcSMatt Macy }
2933*eda14cbcSMatt Macy 
2934*eda14cbcSMatt Macy /*
2935*eda14cbcSMatt Macy  * Attempt to find the local dataset associated with this guid.  In the case of
2936*eda14cbcSMatt Macy  * multiple matches, we attempt to find the "best" match by searching
2937*eda14cbcSMatt Macy  * progressively larger portions of the hierarchy.  This allows one to send a
2938*eda14cbcSMatt Macy  * tree of datasets individually and guarantee that we will find the source
2939*eda14cbcSMatt Macy  * guid within that hierarchy, even if there are multiple matches elsewhere.
2940*eda14cbcSMatt Macy  *
2941*eda14cbcSMatt Macy  * If num_redact_snaps is not -1, we attempt to find a redaction bookmark with
2942*eda14cbcSMatt Macy  * the specified number of redaction snapshots.  If num_redact_snaps isn't 0 or
2943*eda14cbcSMatt Macy  * -1, then redact_snap_guids will be an array of the guids of the snapshots the
2944*eda14cbcSMatt Macy  * redaction bookmark was created with.  If num_redact_snaps is -1, then we will
2945*eda14cbcSMatt Macy  * attempt to find a snapshot or bookmark (if bookmark_ok is passed) with the
2946*eda14cbcSMatt Macy  * given guid.  Note that a redaction bookmark can be returned if
2947*eda14cbcSMatt Macy  * num_redact_snaps == -1.
2948*eda14cbcSMatt Macy  */
2949*eda14cbcSMatt Macy static int
2950*eda14cbcSMatt Macy guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
2951*eda14cbcSMatt Macy     uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
2952*eda14cbcSMatt Macy     uint64_t num_redact_snaps, char *name)
2953*eda14cbcSMatt Macy {
2954*eda14cbcSMatt Macy 	char pname[ZFS_MAX_DATASET_NAME_LEN];
2955*eda14cbcSMatt Macy 	guid_to_name_data_t gtnd;
2956*eda14cbcSMatt Macy 
2957*eda14cbcSMatt Macy 	gtnd.guid = guid;
2958*eda14cbcSMatt Macy 	gtnd.bookmark_ok = bookmark_ok;
2959*eda14cbcSMatt Macy 	gtnd.name = name;
2960*eda14cbcSMatt Macy 	gtnd.skip = NULL;
2961*eda14cbcSMatt Macy 	gtnd.redact_snap_guids = redact_snap_guids;
2962*eda14cbcSMatt Macy 	gtnd.num_redact_snaps = num_redact_snaps;
2963*eda14cbcSMatt Macy 
2964*eda14cbcSMatt Macy 	/*
2965*eda14cbcSMatt Macy 	 * Search progressively larger portions of the hierarchy, starting
2966*eda14cbcSMatt Macy 	 * with the filesystem specified by 'parent'.  This will
2967*eda14cbcSMatt Macy 	 * select the "most local" version of the origin snapshot in the case
2968*eda14cbcSMatt Macy 	 * that there are multiple matching snapshots in the system.
2969*eda14cbcSMatt Macy 	 */
2970*eda14cbcSMatt Macy 	(void) strlcpy(pname, parent, sizeof (pname));
2971*eda14cbcSMatt Macy 	char *cp = strrchr(pname, '@');
2972*eda14cbcSMatt Macy 	if (cp == NULL)
2973*eda14cbcSMatt Macy 		cp = strchr(pname, '\0');
2974*eda14cbcSMatt Macy 	for (; cp != NULL; cp = strrchr(pname, '/')) {
2975*eda14cbcSMatt Macy 		/* Chop off the last component and open the parent */
2976*eda14cbcSMatt Macy 		*cp = '\0';
2977*eda14cbcSMatt Macy 		zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2978*eda14cbcSMatt Macy 
2979*eda14cbcSMatt Macy 		if (zhp == NULL)
2980*eda14cbcSMatt Macy 			continue;
2981*eda14cbcSMatt Macy 		int err = guid_to_name_cb(zfs_handle_dup(zhp), &gtnd);
2982*eda14cbcSMatt Macy 		if (err != EEXIST)
2983*eda14cbcSMatt Macy 			err = zfs_iter_children(zhp, guid_to_name_cb, &gtnd);
2984*eda14cbcSMatt Macy 		if (err != EEXIST && bookmark_ok)
2985*eda14cbcSMatt Macy 			err = zfs_iter_bookmarks(zhp, guid_to_name_cb, &gtnd);
2986*eda14cbcSMatt Macy 		zfs_close(zhp);
2987*eda14cbcSMatt Macy 		if (err == EEXIST)
2988*eda14cbcSMatt Macy 			return (0);
2989*eda14cbcSMatt Macy 
2990*eda14cbcSMatt Macy 		/*
2991*eda14cbcSMatt Macy 		 * Remember the last portion of the dataset so we skip it next
2992*eda14cbcSMatt Macy 		 * time through (as we've already searched that portion of the
2993*eda14cbcSMatt Macy 		 * hierarchy).
2994*eda14cbcSMatt Macy 		 */
2995*eda14cbcSMatt Macy 		gtnd.skip = strrchr(pname, '/') + 1;
2996*eda14cbcSMatt Macy 	}
2997*eda14cbcSMatt Macy 
2998*eda14cbcSMatt Macy 	return (ENOENT);
2999*eda14cbcSMatt Macy }
3000*eda14cbcSMatt Macy 
3001*eda14cbcSMatt Macy static int
3002*eda14cbcSMatt Macy guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
3003*eda14cbcSMatt Macy     boolean_t bookmark_ok, char *name)
3004*eda14cbcSMatt Macy {
3005*eda14cbcSMatt Macy 	return (guid_to_name_redact_snaps(hdl, parent, guid, bookmark_ok, NULL,
3006*eda14cbcSMatt Macy 	    -1, name));
3007*eda14cbcSMatt Macy }
3008*eda14cbcSMatt Macy 
3009*eda14cbcSMatt Macy /*
3010*eda14cbcSMatt Macy  * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
3011*eda14cbcSMatt Macy  * guid1 is after guid2.
3012*eda14cbcSMatt Macy  */
3013*eda14cbcSMatt Macy static int
3014*eda14cbcSMatt Macy created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
3015*eda14cbcSMatt Macy     uint64_t guid1, uint64_t guid2)
3016*eda14cbcSMatt Macy {
3017*eda14cbcSMatt Macy 	nvlist_t *nvfs;
3018*eda14cbcSMatt Macy 	char *fsname = NULL, *snapname = NULL;
3019*eda14cbcSMatt Macy 	char buf[ZFS_MAX_DATASET_NAME_LEN];
3020*eda14cbcSMatt Macy 	int rv;
3021*eda14cbcSMatt Macy 	zfs_handle_t *guid1hdl, *guid2hdl;
3022*eda14cbcSMatt Macy 	uint64_t create1, create2;
3023*eda14cbcSMatt Macy 
3024*eda14cbcSMatt Macy 	if (guid2 == 0)
3025*eda14cbcSMatt Macy 		return (0);
3026*eda14cbcSMatt Macy 	if (guid1 == 0)
3027*eda14cbcSMatt Macy 		return (1);
3028*eda14cbcSMatt Macy 
3029*eda14cbcSMatt Macy 	nvfs = fsavl_find(avl, guid1, &snapname);
3030*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
3031*eda14cbcSMatt Macy 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3032*eda14cbcSMatt Macy 	guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3033*eda14cbcSMatt Macy 	if (guid1hdl == NULL)
3034*eda14cbcSMatt Macy 		return (-1);
3035*eda14cbcSMatt Macy 
3036*eda14cbcSMatt Macy 	nvfs = fsavl_find(avl, guid2, &snapname);
3037*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
3038*eda14cbcSMatt Macy 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3039*eda14cbcSMatt Macy 	guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3040*eda14cbcSMatt Macy 	if (guid2hdl == NULL) {
3041*eda14cbcSMatt Macy 		zfs_close(guid1hdl);
3042*eda14cbcSMatt Macy 		return (-1);
3043*eda14cbcSMatt Macy 	}
3044*eda14cbcSMatt Macy 
3045*eda14cbcSMatt Macy 	create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
3046*eda14cbcSMatt Macy 	create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
3047*eda14cbcSMatt Macy 
3048*eda14cbcSMatt Macy 	if (create1 < create2)
3049*eda14cbcSMatt Macy 		rv = -1;
3050*eda14cbcSMatt Macy 	else if (create1 > create2)
3051*eda14cbcSMatt Macy 		rv = +1;
3052*eda14cbcSMatt Macy 	else
3053*eda14cbcSMatt Macy 		rv = 0;
3054*eda14cbcSMatt Macy 
3055*eda14cbcSMatt Macy 	zfs_close(guid1hdl);
3056*eda14cbcSMatt Macy 	zfs_close(guid2hdl);
3057*eda14cbcSMatt Macy 
3058*eda14cbcSMatt Macy 	return (rv);
3059*eda14cbcSMatt Macy }
3060*eda14cbcSMatt Macy 
3061*eda14cbcSMatt Macy /*
3062*eda14cbcSMatt Macy  * This function reestablishes the hierarchy of encryption roots after a
3063*eda14cbcSMatt Macy  * recursive incremental receive has completed. This must be done after the
3064*eda14cbcSMatt Macy  * second call to recv_incremental_replication() has renamed and promoted all
3065*eda14cbcSMatt Macy  * sent datasets to their final locations in the dataset hierarchy.
3066*eda14cbcSMatt Macy  */
3067*eda14cbcSMatt Macy static int
3068*eda14cbcSMatt Macy recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
3069*eda14cbcSMatt Macy     nvlist_t *stream_nv, avl_tree_t *stream_avl)
3070*eda14cbcSMatt Macy {
3071*eda14cbcSMatt Macy 	int err;
3072*eda14cbcSMatt Macy 	nvpair_t *fselem = NULL;
3073*eda14cbcSMatt Macy 	nvlist_t *stream_fss;
3074*eda14cbcSMatt Macy 
3075*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", &stream_fss));
3076*eda14cbcSMatt Macy 
3077*eda14cbcSMatt Macy 	while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3078*eda14cbcSMatt Macy 		zfs_handle_t *zhp = NULL;
3079*eda14cbcSMatt Macy 		uint64_t crypt;
3080*eda14cbcSMatt Macy 		nvlist_t *snaps, *props, *stream_nvfs = NULL;
3081*eda14cbcSMatt Macy 		nvpair_t *snapel = NULL;
3082*eda14cbcSMatt Macy 		boolean_t is_encroot, is_clone, stream_encroot;
3083*eda14cbcSMatt Macy 		char *cp;
3084*eda14cbcSMatt Macy 		char *stream_keylocation = NULL;
3085*eda14cbcSMatt Macy 		char keylocation[MAXNAMELEN];
3086*eda14cbcSMatt Macy 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
3087*eda14cbcSMatt Macy 
3088*eda14cbcSMatt Macy 		keylocation[0] = '\0';
3089*eda14cbcSMatt Macy 		VERIFY(0 == nvpair_value_nvlist(fselem, &stream_nvfs));
3090*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "snaps", &snaps));
3091*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "props", &props));
3092*eda14cbcSMatt Macy 		stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3093*eda14cbcSMatt Macy 
3094*eda14cbcSMatt Macy 		/* find a snapshot from the stream that exists locally */
3095*eda14cbcSMatt Macy 		err = ENOENT;
3096*eda14cbcSMatt Macy 		while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
3097*eda14cbcSMatt Macy 			uint64_t guid;
3098*eda14cbcSMatt Macy 
3099*eda14cbcSMatt Macy 			VERIFY(0 == nvpair_value_uint64(snapel, &guid));
3100*eda14cbcSMatt Macy 			err = guid_to_name(hdl, top_zfs, guid, B_FALSE,
3101*eda14cbcSMatt Macy 			    fsname);
3102*eda14cbcSMatt Macy 			if (err == 0)
3103*eda14cbcSMatt Macy 				break;
3104*eda14cbcSMatt Macy 		}
3105*eda14cbcSMatt Macy 
3106*eda14cbcSMatt Macy 		if (err != 0)
3107*eda14cbcSMatt Macy 			continue;
3108*eda14cbcSMatt Macy 
3109*eda14cbcSMatt Macy 		cp = strchr(fsname, '@');
3110*eda14cbcSMatt Macy 		if (cp != NULL)
3111*eda14cbcSMatt Macy 			*cp = '\0';
3112*eda14cbcSMatt Macy 
3113*eda14cbcSMatt Macy 		zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3114*eda14cbcSMatt Macy 		if (zhp == NULL) {
3115*eda14cbcSMatt Macy 			err = ENOENT;
3116*eda14cbcSMatt Macy 			goto error;
3117*eda14cbcSMatt Macy 		}
3118*eda14cbcSMatt Macy 
3119*eda14cbcSMatt Macy 		crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
3120*eda14cbcSMatt Macy 		is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3121*eda14cbcSMatt Macy 		(void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3122*eda14cbcSMatt Macy 
3123*eda14cbcSMatt Macy 		/* we don't need to do anything for unencrypted datasets */
3124*eda14cbcSMatt Macy 		if (crypt == ZIO_CRYPT_OFF) {
3125*eda14cbcSMatt Macy 			zfs_close(zhp);
3126*eda14cbcSMatt Macy 			continue;
3127*eda14cbcSMatt Macy 		}
3128*eda14cbcSMatt Macy 
3129*eda14cbcSMatt Macy 		/*
3130*eda14cbcSMatt Macy 		 * If the dataset is flagged as an encryption root, was not
3131*eda14cbcSMatt Macy 		 * received as a clone and is not currently an encryption root,
3132*eda14cbcSMatt Macy 		 * force it to become one. Fixup the keylocation if necessary.
3133*eda14cbcSMatt Macy 		 */
3134*eda14cbcSMatt Macy 		if (stream_encroot) {
3135*eda14cbcSMatt Macy 			if (!is_clone && !is_encroot) {
3136*eda14cbcSMatt Macy 				err = lzc_change_key(fsname,
3137*eda14cbcSMatt Macy 				    DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
3138*eda14cbcSMatt Macy 				if (err != 0) {
3139*eda14cbcSMatt Macy 					zfs_close(zhp);
3140*eda14cbcSMatt Macy 					goto error;
3141*eda14cbcSMatt Macy 				}
3142*eda14cbcSMatt Macy 			}
3143*eda14cbcSMatt Macy 
3144*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_lookup_string(props,
3145*eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
3146*eda14cbcSMatt Macy 			    &stream_keylocation));
3147*eda14cbcSMatt Macy 
3148*eda14cbcSMatt Macy 			/*
3149*eda14cbcSMatt Macy 			 * Refresh the properties in case the call to
3150*eda14cbcSMatt Macy 			 * lzc_change_key() changed the value.
3151*eda14cbcSMatt Macy 			 */
3152*eda14cbcSMatt Macy 			zfs_refresh_properties(zhp);
3153*eda14cbcSMatt Macy 			err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
3154*eda14cbcSMatt Macy 			    keylocation, sizeof (keylocation), NULL, NULL,
3155*eda14cbcSMatt Macy 			    0, B_TRUE);
3156*eda14cbcSMatt Macy 			if (err != 0) {
3157*eda14cbcSMatt Macy 				zfs_close(zhp);
3158*eda14cbcSMatt Macy 				goto error;
3159*eda14cbcSMatt Macy 			}
3160*eda14cbcSMatt Macy 
3161*eda14cbcSMatt Macy 			if (strcmp(keylocation, stream_keylocation) != 0) {
3162*eda14cbcSMatt Macy 				err = zfs_prop_set(zhp,
3163*eda14cbcSMatt Macy 				    zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
3164*eda14cbcSMatt Macy 				    stream_keylocation);
3165*eda14cbcSMatt Macy 				if (err != 0) {
3166*eda14cbcSMatt Macy 					zfs_close(zhp);
3167*eda14cbcSMatt Macy 					goto error;
3168*eda14cbcSMatt Macy 				}
3169*eda14cbcSMatt Macy 			}
3170*eda14cbcSMatt Macy 		}
3171*eda14cbcSMatt Macy 
3172*eda14cbcSMatt Macy 		/*
3173*eda14cbcSMatt Macy 		 * If the dataset is not flagged as an encryption root and is
3174*eda14cbcSMatt Macy 		 * currently an encryption root, force it to inherit from its
3175*eda14cbcSMatt Macy 		 * parent. The root of a raw send should never be
3176*eda14cbcSMatt Macy 		 * force-inherited.
3177*eda14cbcSMatt Macy 		 */
3178*eda14cbcSMatt Macy 		if (!stream_encroot && is_encroot &&
3179*eda14cbcSMatt Macy 		    strcmp(top_zfs, fsname) != 0) {
3180*eda14cbcSMatt Macy 			err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
3181*eda14cbcSMatt Macy 			    NULL, NULL, 0);
3182*eda14cbcSMatt Macy 			if (err != 0) {
3183*eda14cbcSMatt Macy 				zfs_close(zhp);
3184*eda14cbcSMatt Macy 				goto error;
3185*eda14cbcSMatt Macy 			}
3186*eda14cbcSMatt Macy 		}
3187*eda14cbcSMatt Macy 
3188*eda14cbcSMatt Macy 		zfs_close(zhp);
3189*eda14cbcSMatt Macy 	}
3190*eda14cbcSMatt Macy 
3191*eda14cbcSMatt Macy 	return (0);
3192*eda14cbcSMatt Macy 
3193*eda14cbcSMatt Macy error:
3194*eda14cbcSMatt Macy 	return (err);
3195*eda14cbcSMatt Macy }
3196*eda14cbcSMatt Macy 
3197*eda14cbcSMatt Macy static int
3198*eda14cbcSMatt Macy recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
3199*eda14cbcSMatt Macy     recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3200*eda14cbcSMatt Macy     nvlist_t *renamed)
3201*eda14cbcSMatt Macy {
3202*eda14cbcSMatt Macy 	nvlist_t *local_nv, *deleted = NULL;
3203*eda14cbcSMatt Macy 	avl_tree_t *local_avl;
3204*eda14cbcSMatt Macy 	nvpair_t *fselem, *nextfselem;
3205*eda14cbcSMatt Macy 	char *fromsnap;
3206*eda14cbcSMatt Macy 	char newname[ZFS_MAX_DATASET_NAME_LEN];
3207*eda14cbcSMatt Macy 	char guidname[32];
3208*eda14cbcSMatt Macy 	int error;
3209*eda14cbcSMatt Macy 	boolean_t needagain, progress, recursive;
3210*eda14cbcSMatt Macy 	char *s1, *s2;
3211*eda14cbcSMatt Macy 
3212*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
3213*eda14cbcSMatt Macy 
3214*eda14cbcSMatt Macy 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3215*eda14cbcSMatt Macy 	    ENOENT);
3216*eda14cbcSMatt Macy 
3217*eda14cbcSMatt Macy 	if (flags->dryrun)
3218*eda14cbcSMatt Macy 		return (0);
3219*eda14cbcSMatt Macy 
3220*eda14cbcSMatt Macy again:
3221*eda14cbcSMatt Macy 	needagain = progress = B_FALSE;
3222*eda14cbcSMatt Macy 
3223*eda14cbcSMatt Macy 	VERIFY(0 == nvlist_alloc(&deleted, NV_UNIQUE_NAME, 0));
3224*eda14cbcSMatt Macy 
3225*eda14cbcSMatt Macy 	if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
3226*eda14cbcSMatt Macy 	    recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE,
3227*eda14cbcSMatt Macy 	    B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
3228*eda14cbcSMatt Macy 		return (error);
3229*eda14cbcSMatt Macy 
3230*eda14cbcSMatt Macy 	/*
3231*eda14cbcSMatt Macy 	 * Process deletes and renames
3232*eda14cbcSMatt Macy 	 */
3233*eda14cbcSMatt Macy 	for (fselem = nvlist_next_nvpair(local_nv, NULL);
3234*eda14cbcSMatt Macy 	    fselem; fselem = nextfselem) {
3235*eda14cbcSMatt Macy 		nvlist_t *nvfs, *snaps;
3236*eda14cbcSMatt Macy 		nvlist_t *stream_nvfs = NULL;
3237*eda14cbcSMatt Macy 		nvpair_t *snapelem, *nextsnapelem;
3238*eda14cbcSMatt Macy 		uint64_t fromguid = 0;
3239*eda14cbcSMatt Macy 		uint64_t originguid = 0;
3240*eda14cbcSMatt Macy 		uint64_t stream_originguid = 0;
3241*eda14cbcSMatt Macy 		uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
3242*eda14cbcSMatt Macy 		char *fsname, *stream_fsname;
3243*eda14cbcSMatt Macy 
3244*eda14cbcSMatt Macy 		nextfselem = nvlist_next_nvpair(local_nv, fselem);
3245*eda14cbcSMatt Macy 
3246*eda14cbcSMatt Macy 		VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
3247*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
3248*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
3249*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
3250*eda14cbcSMatt Macy 		    &parent_fromsnap_guid));
3251*eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
3252*eda14cbcSMatt Macy 
3253*eda14cbcSMatt Macy 		/*
3254*eda14cbcSMatt Macy 		 * First find the stream's fs, so we can check for
3255*eda14cbcSMatt Macy 		 * a different origin (due to "zfs promote")
3256*eda14cbcSMatt Macy 		 */
3257*eda14cbcSMatt Macy 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
3258*eda14cbcSMatt Macy 		    snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3259*eda14cbcSMatt Macy 			uint64_t thisguid;
3260*eda14cbcSMatt Macy 
3261*eda14cbcSMatt Macy 			VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
3262*eda14cbcSMatt Macy 			stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3263*eda14cbcSMatt Macy 
3264*eda14cbcSMatt Macy 			if (stream_nvfs != NULL)
3265*eda14cbcSMatt Macy 				break;
3266*eda14cbcSMatt Macy 		}
3267*eda14cbcSMatt Macy 
3268*eda14cbcSMatt Macy 		/* check for promote */
3269*eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(stream_nvfs, "origin",
3270*eda14cbcSMatt Macy 		    &stream_originguid);
3271*eda14cbcSMatt Macy 		if (stream_nvfs && originguid != stream_originguid) {
3272*eda14cbcSMatt Macy 			switch (created_before(hdl, local_avl,
3273*eda14cbcSMatt Macy 			    stream_originguid, originguid)) {
3274*eda14cbcSMatt Macy 			case 1: {
3275*eda14cbcSMatt Macy 				/* promote it! */
3276*eda14cbcSMatt Macy 				nvlist_t *origin_nvfs;
3277*eda14cbcSMatt Macy 				char *origin_fsname;
3278*eda14cbcSMatt Macy 
3279*eda14cbcSMatt Macy 				origin_nvfs = fsavl_find(local_avl, originguid,
3280*eda14cbcSMatt Macy 				    NULL);
3281*eda14cbcSMatt Macy 				VERIFY(0 == nvlist_lookup_string(origin_nvfs,
3282*eda14cbcSMatt Macy 				    "name", &origin_fsname));
3283*eda14cbcSMatt Macy 				error = recv_promote(hdl, fsname, origin_fsname,
3284*eda14cbcSMatt Macy 				    flags);
3285*eda14cbcSMatt Macy 				if (error == 0)
3286*eda14cbcSMatt Macy 					progress = B_TRUE;
3287*eda14cbcSMatt Macy 				break;
3288*eda14cbcSMatt Macy 			}
3289*eda14cbcSMatt Macy 			default:
3290*eda14cbcSMatt Macy 				break;
3291*eda14cbcSMatt Macy 			case -1:
3292*eda14cbcSMatt Macy 				fsavl_destroy(local_avl);
3293*eda14cbcSMatt Macy 				nvlist_free(local_nv);
3294*eda14cbcSMatt Macy 				return (-1);
3295*eda14cbcSMatt Macy 			}
3296*eda14cbcSMatt Macy 			/*
3297*eda14cbcSMatt Macy 			 * We had/have the wrong origin, therefore our
3298*eda14cbcSMatt Macy 			 * list of snapshots is wrong.  Need to handle
3299*eda14cbcSMatt Macy 			 * them on the next pass.
3300*eda14cbcSMatt Macy 			 */
3301*eda14cbcSMatt Macy 			needagain = B_TRUE;
3302*eda14cbcSMatt Macy 			continue;
3303*eda14cbcSMatt Macy 		}
3304*eda14cbcSMatt Macy 
3305*eda14cbcSMatt Macy 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
3306*eda14cbcSMatt Macy 		    snapelem; snapelem = nextsnapelem) {
3307*eda14cbcSMatt Macy 			uint64_t thisguid;
3308*eda14cbcSMatt Macy 			char *stream_snapname;
3309*eda14cbcSMatt Macy 			nvlist_t *found, *props;
3310*eda14cbcSMatt Macy 
3311*eda14cbcSMatt Macy 			nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
3312*eda14cbcSMatt Macy 
3313*eda14cbcSMatt Macy 			VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
3314*eda14cbcSMatt Macy 			found = fsavl_find(stream_avl, thisguid,
3315*eda14cbcSMatt Macy 			    &stream_snapname);
3316*eda14cbcSMatt Macy 
3317*eda14cbcSMatt Macy 			/* check for delete */
3318*eda14cbcSMatt Macy 			if (found == NULL) {
3319*eda14cbcSMatt Macy 				char name[ZFS_MAX_DATASET_NAME_LEN];
3320*eda14cbcSMatt Macy 
3321*eda14cbcSMatt Macy 				if (!flags->force)
3322*eda14cbcSMatt Macy 					continue;
3323*eda14cbcSMatt Macy 
3324*eda14cbcSMatt Macy 				(void) snprintf(name, sizeof (name), "%s@%s",
3325*eda14cbcSMatt Macy 				    fsname, nvpair_name(snapelem));
3326*eda14cbcSMatt Macy 
3327*eda14cbcSMatt Macy 				error = recv_destroy(hdl, name,
3328*eda14cbcSMatt Macy 				    strlen(fsname)+1, newname, flags);
3329*eda14cbcSMatt Macy 				if (error)
3330*eda14cbcSMatt Macy 					needagain = B_TRUE;
3331*eda14cbcSMatt Macy 				else
3332*eda14cbcSMatt Macy 					progress = B_TRUE;
3333*eda14cbcSMatt Macy 				sprintf(guidname, "%llu",
3334*eda14cbcSMatt Macy 				    (u_longlong_t)thisguid);
3335*eda14cbcSMatt Macy 				nvlist_add_boolean(deleted, guidname);
3336*eda14cbcSMatt Macy 				continue;
3337*eda14cbcSMatt Macy 			}
3338*eda14cbcSMatt Macy 
3339*eda14cbcSMatt Macy 			stream_nvfs = found;
3340*eda14cbcSMatt Macy 
3341*eda14cbcSMatt Macy 			if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
3342*eda14cbcSMatt Macy 			    &props) && 0 == nvlist_lookup_nvlist(props,
3343*eda14cbcSMatt Macy 			    stream_snapname, &props)) {
3344*eda14cbcSMatt Macy 				zfs_cmd_t zc = {"\0"};
3345*eda14cbcSMatt Macy 
3346*eda14cbcSMatt Macy 				zc.zc_cookie = B_TRUE; /* received */
3347*eda14cbcSMatt Macy 				(void) snprintf(zc.zc_name, sizeof (zc.zc_name),
3348*eda14cbcSMatt Macy 				    "%s@%s", fsname, nvpair_name(snapelem));
3349*eda14cbcSMatt Macy 				if (zcmd_write_src_nvlist(hdl, &zc,
3350*eda14cbcSMatt Macy 				    props) == 0) {
3351*eda14cbcSMatt Macy 					(void) zfs_ioctl(hdl,
3352*eda14cbcSMatt Macy 					    ZFS_IOC_SET_PROP, &zc);
3353*eda14cbcSMatt Macy 					zcmd_free_nvlists(&zc);
3354*eda14cbcSMatt Macy 				}
3355*eda14cbcSMatt Macy 			}
3356*eda14cbcSMatt Macy 
3357*eda14cbcSMatt Macy 			/* check for different snapname */
3358*eda14cbcSMatt Macy 			if (strcmp(nvpair_name(snapelem),
3359*eda14cbcSMatt Macy 			    stream_snapname) != 0) {
3360*eda14cbcSMatt Macy 				char name[ZFS_MAX_DATASET_NAME_LEN];
3361*eda14cbcSMatt Macy 				char tryname[ZFS_MAX_DATASET_NAME_LEN];
3362*eda14cbcSMatt Macy 
3363*eda14cbcSMatt Macy 				(void) snprintf(name, sizeof (name), "%s@%s",
3364*eda14cbcSMatt Macy 				    fsname, nvpair_name(snapelem));
3365*eda14cbcSMatt Macy 				(void) snprintf(tryname, sizeof (name), "%s@%s",
3366*eda14cbcSMatt Macy 				    fsname, stream_snapname);
3367*eda14cbcSMatt Macy 
3368*eda14cbcSMatt Macy 				error = recv_rename(hdl, name, tryname,
3369*eda14cbcSMatt Macy 				    strlen(fsname)+1, newname, flags);
3370*eda14cbcSMatt Macy 				if (error)
3371*eda14cbcSMatt Macy 					needagain = B_TRUE;
3372*eda14cbcSMatt Macy 				else
3373*eda14cbcSMatt Macy 					progress = B_TRUE;
3374*eda14cbcSMatt Macy 			}
3375*eda14cbcSMatt Macy 
3376*eda14cbcSMatt Macy 			if (strcmp(stream_snapname, fromsnap) == 0)
3377*eda14cbcSMatt Macy 				fromguid = thisguid;
3378*eda14cbcSMatt Macy 		}
3379*eda14cbcSMatt Macy 
3380*eda14cbcSMatt Macy 		/* check for delete */
3381*eda14cbcSMatt Macy 		if (stream_nvfs == NULL) {
3382*eda14cbcSMatt Macy 			if (!flags->force)
3383*eda14cbcSMatt Macy 				continue;
3384*eda14cbcSMatt Macy 
3385*eda14cbcSMatt Macy 			error = recv_destroy(hdl, fsname, strlen(tofs)+1,
3386*eda14cbcSMatt Macy 			    newname, flags);
3387*eda14cbcSMatt Macy 			if (error)
3388*eda14cbcSMatt Macy 				needagain = B_TRUE;
3389*eda14cbcSMatt Macy 			else
3390*eda14cbcSMatt Macy 				progress = B_TRUE;
3391*eda14cbcSMatt Macy 			sprintf(guidname, "%llu",
3392*eda14cbcSMatt Macy 			    (u_longlong_t)parent_fromsnap_guid);
3393*eda14cbcSMatt Macy 			nvlist_add_boolean(deleted, guidname);
3394*eda14cbcSMatt Macy 			continue;
3395*eda14cbcSMatt Macy 		}
3396*eda14cbcSMatt Macy 
3397*eda14cbcSMatt Macy 		if (fromguid == 0) {
3398*eda14cbcSMatt Macy 			if (flags->verbose) {
3399*eda14cbcSMatt Macy 				(void) printf("local fs %s does not have "
3400*eda14cbcSMatt Macy 				    "fromsnap (%s in stream); must have "
3401*eda14cbcSMatt Macy 				    "been deleted locally; ignoring\n",
3402*eda14cbcSMatt Macy 				    fsname, fromsnap);
3403*eda14cbcSMatt Macy 			}
3404*eda14cbcSMatt Macy 			continue;
3405*eda14cbcSMatt Macy 		}
3406*eda14cbcSMatt Macy 
3407*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_string(stream_nvfs,
3408*eda14cbcSMatt Macy 		    "name", &stream_fsname));
3409*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
3410*eda14cbcSMatt Macy 		    "parentfromsnap", &stream_parent_fromsnap_guid));
3411*eda14cbcSMatt Macy 
3412*eda14cbcSMatt Macy 		s1 = strrchr(fsname, '/');
3413*eda14cbcSMatt Macy 		s2 = strrchr(stream_fsname, '/');
3414*eda14cbcSMatt Macy 
3415*eda14cbcSMatt Macy 		/*
3416*eda14cbcSMatt Macy 		 * Check if we're going to rename based on parent guid change
3417*eda14cbcSMatt Macy 		 * and the current parent guid was also deleted. If it was then
3418*eda14cbcSMatt Macy 		 * rename will fail and is likely unneeded, so avoid this and
3419*eda14cbcSMatt Macy 		 * force an early retry to determine the new
3420*eda14cbcSMatt Macy 		 * parent_fromsnap_guid.
3421*eda14cbcSMatt Macy 		 */
3422*eda14cbcSMatt Macy 		if (stream_parent_fromsnap_guid != 0 &&
3423*eda14cbcSMatt Macy 		    parent_fromsnap_guid != 0 &&
3424*eda14cbcSMatt Macy 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) {
3425*eda14cbcSMatt Macy 			sprintf(guidname, "%llu",
3426*eda14cbcSMatt Macy 			    (u_longlong_t)parent_fromsnap_guid);
3427*eda14cbcSMatt Macy 			if (nvlist_exists(deleted, guidname)) {
3428*eda14cbcSMatt Macy 				progress = B_TRUE;
3429*eda14cbcSMatt Macy 				needagain = B_TRUE;
3430*eda14cbcSMatt Macy 				goto doagain;
3431*eda14cbcSMatt Macy 			}
3432*eda14cbcSMatt Macy 		}
3433*eda14cbcSMatt Macy 
3434*eda14cbcSMatt Macy 		/*
3435*eda14cbcSMatt Macy 		 * Check for rename. If the exact receive path is specified, it
3436*eda14cbcSMatt Macy 		 * does not count as a rename, but we still need to check the
3437*eda14cbcSMatt Macy 		 * datasets beneath it.
3438*eda14cbcSMatt Macy 		 */
3439*eda14cbcSMatt Macy 		if ((stream_parent_fromsnap_guid != 0 &&
3440*eda14cbcSMatt Macy 		    parent_fromsnap_guid != 0 &&
3441*eda14cbcSMatt Macy 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
3442*eda14cbcSMatt Macy 		    ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3443*eda14cbcSMatt Macy 		    (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3444*eda14cbcSMatt Macy 			nvlist_t *parent;
3445*eda14cbcSMatt Macy 			char tryname[ZFS_MAX_DATASET_NAME_LEN];
3446*eda14cbcSMatt Macy 
3447*eda14cbcSMatt Macy 			parent = fsavl_find(local_avl,
3448*eda14cbcSMatt Macy 			    stream_parent_fromsnap_guid, NULL);
3449*eda14cbcSMatt Macy 			/*
3450*eda14cbcSMatt Macy 			 * NB: parent might not be found if we used the
3451*eda14cbcSMatt Macy 			 * tosnap for stream_parent_fromsnap_guid,
3452*eda14cbcSMatt Macy 			 * because the parent is a newly-created fs;
3453*eda14cbcSMatt Macy 			 * we'll be able to rename it after we recv the
3454*eda14cbcSMatt Macy 			 * new fs.
3455*eda14cbcSMatt Macy 			 */
3456*eda14cbcSMatt Macy 			if (parent != NULL) {
3457*eda14cbcSMatt Macy 				char *pname;
3458*eda14cbcSMatt Macy 
3459*eda14cbcSMatt Macy 				VERIFY(0 == nvlist_lookup_string(parent, "name",
3460*eda14cbcSMatt Macy 				    &pname));
3461*eda14cbcSMatt Macy 				(void) snprintf(tryname, sizeof (tryname),
3462*eda14cbcSMatt Macy 				    "%s%s", pname, strrchr(stream_fsname, '/'));
3463*eda14cbcSMatt Macy 			} else {
3464*eda14cbcSMatt Macy 				tryname[0] = '\0';
3465*eda14cbcSMatt Macy 				if (flags->verbose) {
3466*eda14cbcSMatt Macy 					(void) printf("local fs %s new parent "
3467*eda14cbcSMatt Macy 					    "not found\n", fsname);
3468*eda14cbcSMatt Macy 				}
3469*eda14cbcSMatt Macy 			}
3470*eda14cbcSMatt Macy 
3471*eda14cbcSMatt Macy 			newname[0] = '\0';
3472*eda14cbcSMatt Macy 
3473*eda14cbcSMatt Macy 			error = recv_rename(hdl, fsname, tryname,
3474*eda14cbcSMatt Macy 			    strlen(tofs)+1, newname, flags);
3475*eda14cbcSMatt Macy 
3476*eda14cbcSMatt Macy 			if (renamed != NULL && newname[0] != '\0') {
3477*eda14cbcSMatt Macy 				VERIFY(0 == nvlist_add_boolean(renamed,
3478*eda14cbcSMatt Macy 				    newname));
3479*eda14cbcSMatt Macy 			}
3480*eda14cbcSMatt Macy 
3481*eda14cbcSMatt Macy 			if (error)
3482*eda14cbcSMatt Macy 				needagain = B_TRUE;
3483*eda14cbcSMatt Macy 			else
3484*eda14cbcSMatt Macy 				progress = B_TRUE;
3485*eda14cbcSMatt Macy 		}
3486*eda14cbcSMatt Macy 	}
3487*eda14cbcSMatt Macy 
3488*eda14cbcSMatt Macy doagain:
3489*eda14cbcSMatt Macy 	fsavl_destroy(local_avl);
3490*eda14cbcSMatt Macy 	nvlist_free(local_nv);
3491*eda14cbcSMatt Macy 	nvlist_free(deleted);
3492*eda14cbcSMatt Macy 
3493*eda14cbcSMatt Macy 	if (needagain && progress) {
3494*eda14cbcSMatt Macy 		/* do another pass to fix up temporary names */
3495*eda14cbcSMatt Macy 		if (flags->verbose)
3496*eda14cbcSMatt Macy 			(void) printf("another pass:\n");
3497*eda14cbcSMatt Macy 		goto again;
3498*eda14cbcSMatt Macy 	}
3499*eda14cbcSMatt Macy 
3500*eda14cbcSMatt Macy 	return (needagain || error != 0);
3501*eda14cbcSMatt Macy }
3502*eda14cbcSMatt Macy 
3503*eda14cbcSMatt Macy static int
3504*eda14cbcSMatt Macy zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
3505*eda14cbcSMatt Macy     recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3506*eda14cbcSMatt Macy     char **top_zfs, nvlist_t *cmdprops)
3507*eda14cbcSMatt Macy {
3508*eda14cbcSMatt Macy 	nvlist_t *stream_nv = NULL;
3509*eda14cbcSMatt Macy 	avl_tree_t *stream_avl = NULL;
3510*eda14cbcSMatt Macy 	char *fromsnap = NULL;
3511*eda14cbcSMatt Macy 	char *sendsnap = NULL;
3512*eda14cbcSMatt Macy 	char *cp;
3513*eda14cbcSMatt Macy 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
3514*eda14cbcSMatt Macy 	char sendfs[ZFS_MAX_DATASET_NAME_LEN];
3515*eda14cbcSMatt Macy 	char errbuf[1024];
3516*eda14cbcSMatt Macy 	dmu_replay_record_t drre;
3517*eda14cbcSMatt Macy 	int error;
3518*eda14cbcSMatt Macy 	boolean_t anyerr = B_FALSE;
3519*eda14cbcSMatt Macy 	boolean_t softerr = B_FALSE;
3520*eda14cbcSMatt Macy 	boolean_t recursive, raw;
3521*eda14cbcSMatt Macy 
3522*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3523*eda14cbcSMatt Macy 	    "cannot receive"));
3524*eda14cbcSMatt Macy 
3525*eda14cbcSMatt Macy 	assert(drr->drr_type == DRR_BEGIN);
3526*eda14cbcSMatt Macy 	assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
3527*eda14cbcSMatt Macy 	assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3528*eda14cbcSMatt Macy 	    DMU_COMPOUNDSTREAM);
3529*eda14cbcSMatt Macy 
3530*eda14cbcSMatt Macy 	/*
3531*eda14cbcSMatt Macy 	 * Read in the nvlist from the stream.
3532*eda14cbcSMatt Macy 	 */
3533*eda14cbcSMatt Macy 	if (drr->drr_payloadlen != 0) {
3534*eda14cbcSMatt Macy 		error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
3535*eda14cbcSMatt Macy 		    &stream_nv, flags->byteswap, zc);
3536*eda14cbcSMatt Macy 		if (error) {
3537*eda14cbcSMatt Macy 			error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3538*eda14cbcSMatt Macy 			goto out;
3539*eda14cbcSMatt Macy 		}
3540*eda14cbcSMatt Macy 	}
3541*eda14cbcSMatt Macy 
3542*eda14cbcSMatt Macy 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3543*eda14cbcSMatt Macy 	    ENOENT);
3544*eda14cbcSMatt Macy 	raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3545*eda14cbcSMatt Macy 
3546*eda14cbcSMatt Macy 	if (recursive && strchr(destname, '@')) {
3547*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3548*eda14cbcSMatt Macy 		    "cannot specify snapshot name for multi-snapshot stream"));
3549*eda14cbcSMatt Macy 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3550*eda14cbcSMatt Macy 		goto out;
3551*eda14cbcSMatt Macy 	}
3552*eda14cbcSMatt Macy 
3553*eda14cbcSMatt Macy 	/*
3554*eda14cbcSMatt Macy 	 * Read in the end record and verify checksum.
3555*eda14cbcSMatt Macy 	 */
3556*eda14cbcSMatt Macy 	if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3557*eda14cbcSMatt Macy 	    flags->byteswap, NULL)))
3558*eda14cbcSMatt Macy 		goto out;
3559*eda14cbcSMatt Macy 	if (flags->byteswap) {
3560*eda14cbcSMatt Macy 		drre.drr_type = BSWAP_32(drre.drr_type);
3561*eda14cbcSMatt Macy 		drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3562*eda14cbcSMatt Macy 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3563*eda14cbcSMatt Macy 		drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3564*eda14cbcSMatt Macy 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3565*eda14cbcSMatt Macy 		drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3566*eda14cbcSMatt Macy 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3567*eda14cbcSMatt Macy 		drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3568*eda14cbcSMatt Macy 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3569*eda14cbcSMatt Macy 	}
3570*eda14cbcSMatt Macy 	if (drre.drr_type != DRR_END) {
3571*eda14cbcSMatt Macy 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3572*eda14cbcSMatt Macy 		goto out;
3573*eda14cbcSMatt Macy 	}
3574*eda14cbcSMatt Macy 	if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3575*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3576*eda14cbcSMatt Macy 		    "incorrect header checksum"));
3577*eda14cbcSMatt Macy 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3578*eda14cbcSMatt Macy 		goto out;
3579*eda14cbcSMatt Macy 	}
3580*eda14cbcSMatt Macy 
3581*eda14cbcSMatt Macy 	(void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3582*eda14cbcSMatt Macy 
3583*eda14cbcSMatt Macy 	if (drr->drr_payloadlen != 0) {
3584*eda14cbcSMatt Macy 		nvlist_t *stream_fss;
3585*eda14cbcSMatt Macy 
3586*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
3587*eda14cbcSMatt Macy 		    &stream_fss));
3588*eda14cbcSMatt Macy 		if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3589*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3590*eda14cbcSMatt Macy 			    "couldn't allocate avl tree"));
3591*eda14cbcSMatt Macy 			error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3592*eda14cbcSMatt Macy 			goto out;
3593*eda14cbcSMatt Macy 		}
3594*eda14cbcSMatt Macy 
3595*eda14cbcSMatt Macy 		if (fromsnap != NULL && recursive) {
3596*eda14cbcSMatt Macy 			nvlist_t *renamed = NULL;
3597*eda14cbcSMatt Macy 			nvpair_t *pair = NULL;
3598*eda14cbcSMatt Macy 
3599*eda14cbcSMatt Macy 			(void) strlcpy(tofs, destname, sizeof (tofs));
3600*eda14cbcSMatt Macy 			if (flags->isprefix) {
3601*eda14cbcSMatt Macy 				struct drr_begin *drrb = &drr->drr_u.drr_begin;
3602*eda14cbcSMatt Macy 				int i;
3603*eda14cbcSMatt Macy 
3604*eda14cbcSMatt Macy 				if (flags->istail) {
3605*eda14cbcSMatt Macy 					cp = strrchr(drrb->drr_toname, '/');
3606*eda14cbcSMatt Macy 					if (cp == NULL) {
3607*eda14cbcSMatt Macy 						(void) strlcat(tofs, "/",
3608*eda14cbcSMatt Macy 						    sizeof (tofs));
3609*eda14cbcSMatt Macy 						i = 0;
3610*eda14cbcSMatt Macy 					} else {
3611*eda14cbcSMatt Macy 						i = (cp - drrb->drr_toname);
3612*eda14cbcSMatt Macy 					}
3613*eda14cbcSMatt Macy 				} else {
3614*eda14cbcSMatt Macy 					i = strcspn(drrb->drr_toname, "/@");
3615*eda14cbcSMatt Macy 				}
3616*eda14cbcSMatt Macy 				/* zfs_receive_one() will create_parents() */
3617*eda14cbcSMatt Macy 				(void) strlcat(tofs, &drrb->drr_toname[i],
3618*eda14cbcSMatt Macy 				    sizeof (tofs));
3619*eda14cbcSMatt Macy 				*strchr(tofs, '@') = '\0';
3620*eda14cbcSMatt Macy 			}
3621*eda14cbcSMatt Macy 
3622*eda14cbcSMatt Macy 			if (!flags->dryrun && !flags->nomount) {
3623*eda14cbcSMatt Macy 				VERIFY(0 == nvlist_alloc(&renamed,
3624*eda14cbcSMatt Macy 				    NV_UNIQUE_NAME, 0));
3625*eda14cbcSMatt Macy 			}
3626*eda14cbcSMatt Macy 
3627*eda14cbcSMatt Macy 			softerr = recv_incremental_replication(hdl, tofs, flags,
3628*eda14cbcSMatt Macy 			    stream_nv, stream_avl, renamed);
3629*eda14cbcSMatt Macy 
3630*eda14cbcSMatt Macy 			/* Unmount renamed filesystems before receiving. */
3631*eda14cbcSMatt Macy 			while ((pair = nvlist_next_nvpair(renamed,
3632*eda14cbcSMatt Macy 			    pair)) != NULL) {
3633*eda14cbcSMatt Macy 				zfs_handle_t *zhp;
3634*eda14cbcSMatt Macy 				prop_changelist_t *clp = NULL;
3635*eda14cbcSMatt Macy 
3636*eda14cbcSMatt Macy 				zhp = zfs_open(hdl, nvpair_name(pair),
3637*eda14cbcSMatt Macy 				    ZFS_TYPE_FILESYSTEM);
3638*eda14cbcSMatt Macy 				if (zhp != NULL) {
3639*eda14cbcSMatt Macy 					clp = changelist_gather(zhp,
3640*eda14cbcSMatt Macy 					    ZFS_PROP_MOUNTPOINT, 0,
3641*eda14cbcSMatt Macy 					    flags->forceunmount ? MS_FORCE : 0);
3642*eda14cbcSMatt Macy 					zfs_close(zhp);
3643*eda14cbcSMatt Macy 					if (clp != NULL) {
3644*eda14cbcSMatt Macy 						softerr |=
3645*eda14cbcSMatt Macy 						    changelist_prefix(clp);
3646*eda14cbcSMatt Macy 						changelist_free(clp);
3647*eda14cbcSMatt Macy 					}
3648*eda14cbcSMatt Macy 				}
3649*eda14cbcSMatt Macy 			}
3650*eda14cbcSMatt Macy 
3651*eda14cbcSMatt Macy 			nvlist_free(renamed);
3652*eda14cbcSMatt Macy 		}
3653*eda14cbcSMatt Macy 	}
3654*eda14cbcSMatt Macy 
3655*eda14cbcSMatt Macy 	/*
3656*eda14cbcSMatt Macy 	 * Get the fs specified by the first path in the stream (the top level
3657*eda14cbcSMatt Macy 	 * specified by 'zfs send') and pass it to each invocation of
3658*eda14cbcSMatt Macy 	 * zfs_receive_one().
3659*eda14cbcSMatt Macy 	 */
3660*eda14cbcSMatt Macy 	(void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
3661*eda14cbcSMatt Macy 	    sizeof (sendfs));
3662*eda14cbcSMatt Macy 	if ((cp = strchr(sendfs, '@')) != NULL) {
3663*eda14cbcSMatt Macy 		*cp = '\0';
3664*eda14cbcSMatt Macy 		/*
3665*eda14cbcSMatt Macy 		 * Find the "sendsnap", the final snapshot in a replication
3666*eda14cbcSMatt Macy 		 * stream.  zfs_receive_one() handles certain errors
3667*eda14cbcSMatt Macy 		 * differently, depending on if the contained stream is the
3668*eda14cbcSMatt Macy 		 * last one or not.
3669*eda14cbcSMatt Macy 		 */
3670*eda14cbcSMatt Macy 		sendsnap = (cp + 1);
3671*eda14cbcSMatt Macy 	}
3672*eda14cbcSMatt Macy 
3673*eda14cbcSMatt Macy 	/* Finally, receive each contained stream */
3674*eda14cbcSMatt Macy 	do {
3675*eda14cbcSMatt Macy 		/*
3676*eda14cbcSMatt Macy 		 * we should figure out if it has a recoverable
3677*eda14cbcSMatt Macy 		 * error, in which case do a recv_skip() and drive on.
3678*eda14cbcSMatt Macy 		 * Note, if we fail due to already having this guid,
3679*eda14cbcSMatt Macy 		 * zfs_receive_one() will take care of it (ie,
3680*eda14cbcSMatt Macy 		 * recv_skip() and return 0).
3681*eda14cbcSMatt Macy 		 */
3682*eda14cbcSMatt Macy 		error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3683*eda14cbcSMatt Macy 		    sendfs, stream_nv, stream_avl, top_zfs, sendsnap, cmdprops);
3684*eda14cbcSMatt Macy 		if (error == ENODATA) {
3685*eda14cbcSMatt Macy 			error = 0;
3686*eda14cbcSMatt Macy 			break;
3687*eda14cbcSMatt Macy 		}
3688*eda14cbcSMatt Macy 		anyerr |= error;
3689*eda14cbcSMatt Macy 	} while (error == 0);
3690*eda14cbcSMatt Macy 
3691*eda14cbcSMatt Macy 	if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
3692*eda14cbcSMatt Macy 		/*
3693*eda14cbcSMatt Macy 		 * Now that we have the fs's they sent us, try the
3694*eda14cbcSMatt Macy 		 * renames again.
3695*eda14cbcSMatt Macy 		 */
3696*eda14cbcSMatt Macy 		softerr = recv_incremental_replication(hdl, tofs, flags,
3697*eda14cbcSMatt Macy 		    stream_nv, stream_avl, NULL);
3698*eda14cbcSMatt Macy 	}
3699*eda14cbcSMatt Macy 
3700*eda14cbcSMatt Macy 	if (raw && softerr == 0 && *top_zfs != NULL) {
3701*eda14cbcSMatt Macy 		softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
3702*eda14cbcSMatt Macy 		    stream_nv, stream_avl);
3703*eda14cbcSMatt Macy 	}
3704*eda14cbcSMatt Macy 
3705*eda14cbcSMatt Macy out:
3706*eda14cbcSMatt Macy 	fsavl_destroy(stream_avl);
3707*eda14cbcSMatt Macy 	nvlist_free(stream_nv);
3708*eda14cbcSMatt Macy 	if (softerr)
3709*eda14cbcSMatt Macy 		error = -2;
3710*eda14cbcSMatt Macy 	if (anyerr)
3711*eda14cbcSMatt Macy 		error = -1;
3712*eda14cbcSMatt Macy 	return (error);
3713*eda14cbcSMatt Macy }
3714*eda14cbcSMatt Macy 
3715*eda14cbcSMatt Macy static void
3716*eda14cbcSMatt Macy trunc_prop_errs(int truncated)
3717*eda14cbcSMatt Macy {
3718*eda14cbcSMatt Macy 	ASSERT(truncated != 0);
3719*eda14cbcSMatt Macy 
3720*eda14cbcSMatt Macy 	if (truncated == 1)
3721*eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3722*eda14cbcSMatt Macy 		    "1 more property could not be set\n"));
3723*eda14cbcSMatt Macy 	else
3724*eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3725*eda14cbcSMatt Macy 		    "%d more properties could not be set\n"), truncated);
3726*eda14cbcSMatt Macy }
3727*eda14cbcSMatt Macy 
3728*eda14cbcSMatt Macy static int
3729*eda14cbcSMatt Macy recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3730*eda14cbcSMatt Macy {
3731*eda14cbcSMatt Macy 	dmu_replay_record_t *drr;
3732*eda14cbcSMatt Macy 	void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
3733*eda14cbcSMatt Macy 	uint64_t payload_size;
3734*eda14cbcSMatt Macy 	char errbuf[1024];
3735*eda14cbcSMatt Macy 
3736*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3737*eda14cbcSMatt Macy 	    "cannot receive"));
3738*eda14cbcSMatt Macy 
3739*eda14cbcSMatt Macy 	/* XXX would be great to use lseek if possible... */
3740*eda14cbcSMatt Macy 	drr = buf;
3741*eda14cbcSMatt Macy 
3742*eda14cbcSMatt Macy 	while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3743*eda14cbcSMatt Macy 	    byteswap, NULL) == 0) {
3744*eda14cbcSMatt Macy 		if (byteswap)
3745*eda14cbcSMatt Macy 			drr->drr_type = BSWAP_32(drr->drr_type);
3746*eda14cbcSMatt Macy 
3747*eda14cbcSMatt Macy 		switch (drr->drr_type) {
3748*eda14cbcSMatt Macy 		case DRR_BEGIN:
3749*eda14cbcSMatt Macy 			if (drr->drr_payloadlen != 0) {
3750*eda14cbcSMatt Macy 				(void) recv_read(hdl, fd, buf,
3751*eda14cbcSMatt Macy 				    drr->drr_payloadlen, B_FALSE, NULL);
3752*eda14cbcSMatt Macy 			}
3753*eda14cbcSMatt Macy 			break;
3754*eda14cbcSMatt Macy 
3755*eda14cbcSMatt Macy 		case DRR_END:
3756*eda14cbcSMatt Macy 			free(buf);
3757*eda14cbcSMatt Macy 			return (0);
3758*eda14cbcSMatt Macy 
3759*eda14cbcSMatt Macy 		case DRR_OBJECT:
3760*eda14cbcSMatt Macy 			if (byteswap) {
3761*eda14cbcSMatt Macy 				drr->drr_u.drr_object.drr_bonuslen =
3762*eda14cbcSMatt Macy 				    BSWAP_32(drr->drr_u.drr_object.
3763*eda14cbcSMatt Macy 				    drr_bonuslen);
3764*eda14cbcSMatt Macy 				drr->drr_u.drr_object.drr_raw_bonuslen =
3765*eda14cbcSMatt Macy 				    BSWAP_32(drr->drr_u.drr_object.
3766*eda14cbcSMatt Macy 				    drr_raw_bonuslen);
3767*eda14cbcSMatt Macy 			}
3768*eda14cbcSMatt Macy 
3769*eda14cbcSMatt Macy 			payload_size =
3770*eda14cbcSMatt Macy 			    DRR_OBJECT_PAYLOAD_SIZE(&drr->drr_u.drr_object);
3771*eda14cbcSMatt Macy 			(void) recv_read(hdl, fd, buf, payload_size,
3772*eda14cbcSMatt Macy 			    B_FALSE, NULL);
3773*eda14cbcSMatt Macy 			break;
3774*eda14cbcSMatt Macy 
3775*eda14cbcSMatt Macy 		case DRR_WRITE:
3776*eda14cbcSMatt Macy 			if (byteswap) {
3777*eda14cbcSMatt Macy 				drr->drr_u.drr_write.drr_logical_size =
3778*eda14cbcSMatt Macy 				    BSWAP_64(
3779*eda14cbcSMatt Macy 				    drr->drr_u.drr_write.drr_logical_size);
3780*eda14cbcSMatt Macy 				drr->drr_u.drr_write.drr_compressed_size =
3781*eda14cbcSMatt Macy 				    BSWAP_64(
3782*eda14cbcSMatt Macy 				    drr->drr_u.drr_write.drr_compressed_size);
3783*eda14cbcSMatt Macy 			}
3784*eda14cbcSMatt Macy 			payload_size =
3785*eda14cbcSMatt Macy 			    DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3786*eda14cbcSMatt Macy 			(void) recv_read(hdl, fd, buf,
3787*eda14cbcSMatt Macy 			    payload_size, B_FALSE, NULL);
3788*eda14cbcSMatt Macy 			break;
3789*eda14cbcSMatt Macy 		case DRR_SPILL:
3790*eda14cbcSMatt Macy 			if (byteswap) {
3791*eda14cbcSMatt Macy 				drr->drr_u.drr_spill.drr_length =
3792*eda14cbcSMatt Macy 				    BSWAP_64(drr->drr_u.drr_spill.drr_length);
3793*eda14cbcSMatt Macy 				drr->drr_u.drr_spill.drr_compressed_size =
3794*eda14cbcSMatt Macy 				    BSWAP_64(drr->drr_u.drr_spill.
3795*eda14cbcSMatt Macy 				    drr_compressed_size);
3796*eda14cbcSMatt Macy 			}
3797*eda14cbcSMatt Macy 
3798*eda14cbcSMatt Macy 			payload_size =
3799*eda14cbcSMatt Macy 			    DRR_SPILL_PAYLOAD_SIZE(&drr->drr_u.drr_spill);
3800*eda14cbcSMatt Macy 			(void) recv_read(hdl, fd, buf, payload_size,
3801*eda14cbcSMatt Macy 			    B_FALSE, NULL);
3802*eda14cbcSMatt Macy 			break;
3803*eda14cbcSMatt Macy 		case DRR_WRITE_EMBEDDED:
3804*eda14cbcSMatt Macy 			if (byteswap) {
3805*eda14cbcSMatt Macy 				drr->drr_u.drr_write_embedded.drr_psize =
3806*eda14cbcSMatt Macy 				    BSWAP_32(drr->drr_u.drr_write_embedded.
3807*eda14cbcSMatt Macy 				    drr_psize);
3808*eda14cbcSMatt Macy 			}
3809*eda14cbcSMatt Macy 			(void) recv_read(hdl, fd, buf,
3810*eda14cbcSMatt Macy 			    P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3811*eda14cbcSMatt Macy 			    8), B_FALSE, NULL);
3812*eda14cbcSMatt Macy 			break;
3813*eda14cbcSMatt Macy 		case DRR_OBJECT_RANGE:
3814*eda14cbcSMatt Macy 		case DRR_WRITE_BYREF:
3815*eda14cbcSMatt Macy 		case DRR_FREEOBJECTS:
3816*eda14cbcSMatt Macy 		case DRR_FREE:
3817*eda14cbcSMatt Macy 			break;
3818*eda14cbcSMatt Macy 
3819*eda14cbcSMatt Macy 		default:
3820*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3821*eda14cbcSMatt Macy 			    "invalid record type"));
3822*eda14cbcSMatt Macy 			free(buf);
3823*eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3824*eda14cbcSMatt Macy 		}
3825*eda14cbcSMatt Macy 	}
3826*eda14cbcSMatt Macy 
3827*eda14cbcSMatt Macy 	free(buf);
3828*eda14cbcSMatt Macy 	return (-1);
3829*eda14cbcSMatt Macy }
3830*eda14cbcSMatt Macy 
3831*eda14cbcSMatt Macy static void
3832*eda14cbcSMatt Macy recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3833*eda14cbcSMatt Macy     boolean_t resumable, boolean_t checksum)
3834*eda14cbcSMatt Macy {
3835*eda14cbcSMatt Macy 	char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3836*eda14cbcSMatt Macy 
3837*eda14cbcSMatt Macy 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, (checksum ?
3838*eda14cbcSMatt Macy 	    "checksum mismatch" : "incomplete stream")));
3839*eda14cbcSMatt Macy 
3840*eda14cbcSMatt Macy 	if (!resumable)
3841*eda14cbcSMatt Macy 		return;
3842*eda14cbcSMatt Macy 	(void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3843*eda14cbcSMatt Macy 	*strchr(target_fs, '@') = '\0';
3844*eda14cbcSMatt Macy 	zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3845*eda14cbcSMatt Macy 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3846*eda14cbcSMatt Macy 	if (zhp == NULL)
3847*eda14cbcSMatt Macy 		return;
3848*eda14cbcSMatt Macy 
3849*eda14cbcSMatt Macy 	char token_buf[ZFS_MAXPROPLEN];
3850*eda14cbcSMatt Macy 	int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3851*eda14cbcSMatt Macy 	    token_buf, sizeof (token_buf),
3852*eda14cbcSMatt Macy 	    NULL, NULL, 0, B_TRUE);
3853*eda14cbcSMatt Macy 	if (error == 0) {
3854*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3855*eda14cbcSMatt Macy 		    "checksum mismatch or incomplete stream.\n"
3856*eda14cbcSMatt Macy 		    "Partially received snapshot is saved.\n"
3857*eda14cbcSMatt Macy 		    "A resuming stream can be generated on the sending "
3858*eda14cbcSMatt Macy 		    "system by running:\n"
3859*eda14cbcSMatt Macy 		    "    zfs send -t %s"),
3860*eda14cbcSMatt Macy 		    token_buf);
3861*eda14cbcSMatt Macy 	}
3862*eda14cbcSMatt Macy 	zfs_close(zhp);
3863*eda14cbcSMatt Macy }
3864*eda14cbcSMatt Macy 
3865*eda14cbcSMatt Macy /*
3866*eda14cbcSMatt Macy  * Prepare a new nvlist of properties that are to override (-o) or be excluded
3867*eda14cbcSMatt Macy  * (-x) from the received dataset
3868*eda14cbcSMatt Macy  * recvprops: received properties from the send stream
3869*eda14cbcSMatt Macy  * cmdprops: raw input properties from command line
3870*eda14cbcSMatt Macy  * origprops: properties, both locally-set and received, currently set on the
3871*eda14cbcSMatt Macy  *            target dataset if it exists, NULL otherwise.
3872*eda14cbcSMatt Macy  * oxprops: valid output override (-o) and excluded (-x) properties
3873*eda14cbcSMatt Macy  */
3874*eda14cbcSMatt Macy static int
3875*eda14cbcSMatt Macy zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
3876*eda14cbcSMatt Macy     char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
3877*eda14cbcSMatt Macy     boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
3878*eda14cbcSMatt Macy     nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
3879*eda14cbcSMatt Macy     uint_t *wkeylen_out, const char *errbuf)
3880*eda14cbcSMatt Macy {
3881*eda14cbcSMatt Macy 	nvpair_t *nvp;
3882*eda14cbcSMatt Macy 	nvlist_t *oprops, *voprops;
3883*eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL;
3884*eda14cbcSMatt Macy 	zpool_handle_t *zpool_hdl = NULL;
3885*eda14cbcSMatt Macy 	char *cp;
3886*eda14cbcSMatt Macy 	int ret = 0;
3887*eda14cbcSMatt Macy 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
3888*eda14cbcSMatt Macy 
3889*eda14cbcSMatt Macy 	if (nvlist_empty(cmdprops))
3890*eda14cbcSMatt Macy 		return (0); /* No properties to override or exclude */
3891*eda14cbcSMatt Macy 
3892*eda14cbcSMatt Macy 	*oxprops = fnvlist_alloc();
3893*eda14cbcSMatt Macy 	oprops = fnvlist_alloc();
3894*eda14cbcSMatt Macy 
3895*eda14cbcSMatt Macy 	strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
3896*eda14cbcSMatt Macy 
3897*eda14cbcSMatt Macy 	/*
3898*eda14cbcSMatt Macy 	 * Get our dataset handle. The target dataset may not exist yet.
3899*eda14cbcSMatt Macy 	 */
3900*eda14cbcSMatt Macy 	if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
3901*eda14cbcSMatt Macy 		zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
3902*eda14cbcSMatt Macy 		if (zhp == NULL) {
3903*eda14cbcSMatt Macy 			ret = -1;
3904*eda14cbcSMatt Macy 			goto error;
3905*eda14cbcSMatt Macy 		}
3906*eda14cbcSMatt Macy 	}
3907*eda14cbcSMatt Macy 
3908*eda14cbcSMatt Macy 	/* open the zpool handle */
3909*eda14cbcSMatt Macy 	cp = strchr(namebuf, '/');
3910*eda14cbcSMatt Macy 	if (cp != NULL)
3911*eda14cbcSMatt Macy 		*cp = '\0';
3912*eda14cbcSMatt Macy 	zpool_hdl = zpool_open(hdl, namebuf);
3913*eda14cbcSMatt Macy 	if (zpool_hdl == NULL) {
3914*eda14cbcSMatt Macy 		ret = -1;
3915*eda14cbcSMatt Macy 		goto error;
3916*eda14cbcSMatt Macy 	}
3917*eda14cbcSMatt Macy 
3918*eda14cbcSMatt Macy 	/* restore namebuf to match fsname for later use */
3919*eda14cbcSMatt Macy 	if (cp != NULL)
3920*eda14cbcSMatt Macy 		*cp = '/';
3921*eda14cbcSMatt Macy 
3922*eda14cbcSMatt Macy 	/*
3923*eda14cbcSMatt Macy 	 * first iteration: process excluded (-x) properties now and gather
3924*eda14cbcSMatt Macy 	 * added (-o) properties to be later processed by zfs_valid_proplist()
3925*eda14cbcSMatt Macy 	 */
3926*eda14cbcSMatt Macy 	nvp = NULL;
3927*eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
3928*eda14cbcSMatt Macy 		const char *name = nvpair_name(nvp);
3929*eda14cbcSMatt Macy 		zfs_prop_t prop = zfs_name_to_prop(name);
3930*eda14cbcSMatt Macy 
3931*eda14cbcSMatt Macy 		/* "origin" is processed separately, don't handle it here */
3932*eda14cbcSMatt Macy 		if (prop == ZFS_PROP_ORIGIN)
3933*eda14cbcSMatt Macy 			continue;
3934*eda14cbcSMatt Macy 
3935*eda14cbcSMatt Macy 		/*
3936*eda14cbcSMatt Macy 		 * we're trying to override or exclude a property that does not
3937*eda14cbcSMatt Macy 		 * make sense for this type of dataset, but we don't want to
3938*eda14cbcSMatt Macy 		 * fail if the receive is recursive: this comes in handy when
3939*eda14cbcSMatt Macy 		 * the send stream contains, for instance, a child ZVOL and
3940*eda14cbcSMatt Macy 		 * we're trying to receive it with "-o atime=on"
3941*eda14cbcSMatt Macy 		 */
3942*eda14cbcSMatt Macy 		if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
3943*eda14cbcSMatt Macy 		    !zfs_prop_user(name)) {
3944*eda14cbcSMatt Macy 			if (recursive)
3945*eda14cbcSMatt Macy 				continue;
3946*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3947*eda14cbcSMatt Macy 			    "property '%s' does not apply to datasets of this "
3948*eda14cbcSMatt Macy 			    "type"), name);
3949*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3950*eda14cbcSMatt Macy 			goto error;
3951*eda14cbcSMatt Macy 		}
3952*eda14cbcSMatt Macy 
3953*eda14cbcSMatt Macy 		/* raw streams can't override encryption properties */
3954*eda14cbcSMatt Macy 		if ((zfs_prop_encryption_key_param(prop) ||
3955*eda14cbcSMatt Macy 		    prop == ZFS_PROP_ENCRYPTION) && raw) {
3956*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3957*eda14cbcSMatt Macy 			    "encryption property '%s' cannot "
3958*eda14cbcSMatt Macy 			    "be set or excluded for raw streams."), name);
3959*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3960*eda14cbcSMatt Macy 			goto error;
3961*eda14cbcSMatt Macy 		}
3962*eda14cbcSMatt Macy 
3963*eda14cbcSMatt Macy 		/* incremental streams can only exclude encryption properties */
3964*eda14cbcSMatt Macy 		if ((zfs_prop_encryption_key_param(prop) ||
3965*eda14cbcSMatt Macy 		    prop == ZFS_PROP_ENCRYPTION) && !newfs &&
3966*eda14cbcSMatt Macy 		    nvpair_type(nvp) != DATA_TYPE_BOOLEAN) {
3967*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3968*eda14cbcSMatt Macy 			    "encryption property '%s' cannot "
3969*eda14cbcSMatt Macy 			    "be set for incremental streams."), name);
3970*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3971*eda14cbcSMatt Macy 			goto error;
3972*eda14cbcSMatt Macy 		}
3973*eda14cbcSMatt Macy 
3974*eda14cbcSMatt Macy 		switch (nvpair_type(nvp)) {
3975*eda14cbcSMatt Macy 		case DATA_TYPE_BOOLEAN: /* -x property */
3976*eda14cbcSMatt Macy 			/*
3977*eda14cbcSMatt Macy 			 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
3978*eda14cbcSMatt Macy 			 * a property: this is done by forcing an explicit
3979*eda14cbcSMatt Macy 			 * inherit on the destination so the effective value is
3980*eda14cbcSMatt Macy 			 * not the one we received from the send stream.
3981*eda14cbcSMatt Macy 			 * We do this only if the property is not already
3982*eda14cbcSMatt Macy 			 * locally-set, in which case its value will take
3983*eda14cbcSMatt Macy 			 * priority over the received anyway.
3984*eda14cbcSMatt Macy 			 */
3985*eda14cbcSMatt Macy 			if (nvlist_exists(origprops, name)) {
3986*eda14cbcSMatt Macy 				nvlist_t *attrs;
3987*eda14cbcSMatt Macy 				char *source = NULL;
3988*eda14cbcSMatt Macy 
3989*eda14cbcSMatt Macy 				attrs = fnvlist_lookup_nvlist(origprops, name);
3990*eda14cbcSMatt Macy 				if (nvlist_lookup_string(attrs,
3991*eda14cbcSMatt Macy 				    ZPROP_SOURCE, &source) == 0 &&
3992*eda14cbcSMatt Macy 				    strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
3993*eda14cbcSMatt Macy 					continue;
3994*eda14cbcSMatt Macy 			}
3995*eda14cbcSMatt Macy 			/*
3996*eda14cbcSMatt Macy 			 * We can't force an explicit inherit on non-inheritable
3997*eda14cbcSMatt Macy 			 * properties: if we're asked to exclude this kind of
3998*eda14cbcSMatt Macy 			 * values we remove them from "recvprops" input nvlist.
3999*eda14cbcSMatt Macy 			 */
4000*eda14cbcSMatt Macy 			if (!zfs_prop_inheritable(prop) &&
4001*eda14cbcSMatt Macy 			    !zfs_prop_user(name) && /* can be inherited too */
4002*eda14cbcSMatt Macy 			    nvlist_exists(recvprops, name))
4003*eda14cbcSMatt Macy 				fnvlist_remove(recvprops, name);
4004*eda14cbcSMatt Macy 			else
4005*eda14cbcSMatt Macy 				fnvlist_add_nvpair(*oxprops, nvp);
4006*eda14cbcSMatt Macy 			break;
4007*eda14cbcSMatt Macy 		case DATA_TYPE_STRING: /* -o property=value */
4008*eda14cbcSMatt Macy 			fnvlist_add_nvpair(oprops, nvp);
4009*eda14cbcSMatt Macy 			break;
4010*eda14cbcSMatt Macy 		default:
4011*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4012*eda14cbcSMatt Macy 			    "property '%s' must be a string or boolean"), name);
4013*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4014*eda14cbcSMatt Macy 			goto error;
4015*eda14cbcSMatt Macy 		}
4016*eda14cbcSMatt Macy 	}
4017*eda14cbcSMatt Macy 
4018*eda14cbcSMatt Macy 	if (toplevel) {
4019*eda14cbcSMatt Macy 		/* convert override strings properties to native */
4020*eda14cbcSMatt Macy 		if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
4021*eda14cbcSMatt Macy 		    oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4022*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4023*eda14cbcSMatt Macy 			goto error;
4024*eda14cbcSMatt Macy 		}
4025*eda14cbcSMatt Macy 
4026*eda14cbcSMatt Macy 		/*
4027*eda14cbcSMatt Macy 		 * zfs_crypto_create() requires the parent name. Get it
4028*eda14cbcSMatt Macy 		 * by truncating the fsname copy stored in namebuf.
4029*eda14cbcSMatt Macy 		 */
4030*eda14cbcSMatt Macy 		cp = strrchr(namebuf, '/');
4031*eda14cbcSMatt Macy 		if (cp != NULL)
4032*eda14cbcSMatt Macy 			*cp = '\0';
4033*eda14cbcSMatt Macy 
4034*eda14cbcSMatt Macy 		if (!raw && zfs_crypto_create(hdl, namebuf, voprops, NULL,
4035*eda14cbcSMatt Macy 		    B_FALSE, wkeydata_out, wkeylen_out) != 0) {
4036*eda14cbcSMatt Macy 			fnvlist_free(voprops);
4037*eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4038*eda14cbcSMatt Macy 			goto error;
4039*eda14cbcSMatt Macy 		}
4040*eda14cbcSMatt Macy 
4041*eda14cbcSMatt Macy 		/* second pass: process "-o" properties */
4042*eda14cbcSMatt Macy 		fnvlist_merge(*oxprops, voprops);
4043*eda14cbcSMatt Macy 		fnvlist_free(voprops);
4044*eda14cbcSMatt Macy 	} else {
4045*eda14cbcSMatt Macy 		/* override props on child dataset are inherited */
4046*eda14cbcSMatt Macy 		nvp = NULL;
4047*eda14cbcSMatt Macy 		while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
4048*eda14cbcSMatt Macy 			const char *name = nvpair_name(nvp);
4049*eda14cbcSMatt Macy 			fnvlist_add_boolean(*oxprops, name);
4050*eda14cbcSMatt Macy 		}
4051*eda14cbcSMatt Macy 	}
4052*eda14cbcSMatt Macy 
4053*eda14cbcSMatt Macy error:
4054*eda14cbcSMatt Macy 	if (zhp != NULL)
4055*eda14cbcSMatt Macy 		zfs_close(zhp);
4056*eda14cbcSMatt Macy 	if (zpool_hdl != NULL)
4057*eda14cbcSMatt Macy 		zpool_close(zpool_hdl);
4058*eda14cbcSMatt Macy 	fnvlist_free(oprops);
4059*eda14cbcSMatt Macy 	return (ret);
4060*eda14cbcSMatt Macy }
4061*eda14cbcSMatt Macy 
4062*eda14cbcSMatt Macy /*
4063*eda14cbcSMatt Macy  * Restores a backup of tosnap from the file descriptor specified by infd.
4064*eda14cbcSMatt Macy  */
4065*eda14cbcSMatt Macy static int
4066*eda14cbcSMatt Macy zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
4067*eda14cbcSMatt Macy     const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
4068*eda14cbcSMatt Macy     dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
4069*eda14cbcSMatt Macy     avl_tree_t *stream_avl, char **top_zfs,
4070*eda14cbcSMatt Macy     const char *finalsnap, nvlist_t *cmdprops)
4071*eda14cbcSMatt Macy {
4072*eda14cbcSMatt Macy 	time_t begin_time;
4073*eda14cbcSMatt Macy 	int ioctl_err, ioctl_errno, err;
4074*eda14cbcSMatt Macy 	char *cp;
4075*eda14cbcSMatt Macy 	struct drr_begin *drrb = &drr->drr_u.drr_begin;
4076*eda14cbcSMatt Macy 	char errbuf[1024];
4077*eda14cbcSMatt Macy 	const char *chopprefix;
4078*eda14cbcSMatt Macy 	boolean_t newfs = B_FALSE;
4079*eda14cbcSMatt Macy 	boolean_t stream_wantsnewfs;
4080*eda14cbcSMatt Macy 	boolean_t newprops = B_FALSE;
4081*eda14cbcSMatt Macy 	uint64_t read_bytes = 0;
4082*eda14cbcSMatt Macy 	uint64_t errflags = 0;
4083*eda14cbcSMatt Macy 	uint64_t parent_snapguid = 0;
4084*eda14cbcSMatt Macy 	prop_changelist_t *clp = NULL;
4085*eda14cbcSMatt Macy 	nvlist_t *snapprops_nvlist = NULL;
4086*eda14cbcSMatt Macy 	nvlist_t *snapholds_nvlist = NULL;
4087*eda14cbcSMatt Macy 	zprop_errflags_t prop_errflags;
4088*eda14cbcSMatt Macy 	nvlist_t *prop_errors = NULL;
4089*eda14cbcSMatt Macy 	boolean_t recursive;
4090*eda14cbcSMatt Macy 	char *snapname = NULL;
4091*eda14cbcSMatt Macy 	char destsnap[MAXPATHLEN * 2];
4092*eda14cbcSMatt Macy 	char origin[MAXNAMELEN];
4093*eda14cbcSMatt Macy 	char name[MAXPATHLEN];
4094*eda14cbcSMatt Macy 	char tmp_keylocation[MAXNAMELEN];
4095*eda14cbcSMatt Macy 	nvlist_t *rcvprops = NULL; /* props received from the send stream */
4096*eda14cbcSMatt Macy 	nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
4097*eda14cbcSMatt Macy 	nvlist_t *origprops = NULL; /* original props (if destination exists) */
4098*eda14cbcSMatt Macy 	zfs_type_t type;
4099*eda14cbcSMatt Macy 	boolean_t toplevel = B_FALSE;
4100*eda14cbcSMatt Macy 	boolean_t zoned = B_FALSE;
4101*eda14cbcSMatt Macy 	boolean_t hastoken = B_FALSE;
4102*eda14cbcSMatt Macy 	boolean_t redacted;
4103*eda14cbcSMatt Macy 	uint8_t *wkeydata = NULL;
4104*eda14cbcSMatt Macy 	uint_t wkeylen = 0;
4105*eda14cbcSMatt Macy 
4106*eda14cbcSMatt Macy 	begin_time = time(NULL);
4107*eda14cbcSMatt Macy 	bzero(origin, MAXNAMELEN);
4108*eda14cbcSMatt Macy 	bzero(tmp_keylocation, MAXNAMELEN);
4109*eda14cbcSMatt Macy 
4110*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4111*eda14cbcSMatt Macy 	    "cannot receive"));
4112*eda14cbcSMatt Macy 
4113*eda14cbcSMatt Macy 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
4114*eda14cbcSMatt Macy 	    ENOENT);
4115*eda14cbcSMatt Macy 
4116*eda14cbcSMatt Macy 	/* Did the user request holds be skipped via zfs recv -k? */
4117*eda14cbcSMatt Macy 	boolean_t holds = flags->holds && !flags->skipholds;
4118*eda14cbcSMatt Macy 
4119*eda14cbcSMatt Macy 	if (stream_avl != NULL) {
4120*eda14cbcSMatt Macy 		char *keylocation = NULL;
4121*eda14cbcSMatt Macy 		nvlist_t *lookup = NULL;
4122*eda14cbcSMatt Macy 		nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
4123*eda14cbcSMatt Macy 		    &snapname);
4124*eda14cbcSMatt Macy 
4125*eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(fs, "parentfromsnap",
4126*eda14cbcSMatt Macy 		    &parent_snapguid);
4127*eda14cbcSMatt Macy 		err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
4128*eda14cbcSMatt Macy 		if (err) {
4129*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_alloc(&rcvprops, NV_UNIQUE_NAME, 0));
4130*eda14cbcSMatt Macy 			newprops = B_TRUE;
4131*eda14cbcSMatt Macy 		}
4132*eda14cbcSMatt Macy 
4133*eda14cbcSMatt Macy 		/*
4134*eda14cbcSMatt Macy 		 * The keylocation property may only be set on encryption roots,
4135*eda14cbcSMatt Macy 		 * but this dataset might not become an encryption root until
4136*eda14cbcSMatt Macy 		 * recv_fix_encryption_hierarchy() is called. That function
4137*eda14cbcSMatt Macy 		 * will fixup the keylocation anyway, so we temporarily unset
4138*eda14cbcSMatt Macy 		 * the keylocation for now to avoid any errors from the receive
4139*eda14cbcSMatt Macy 		 * ioctl.
4140*eda14cbcSMatt Macy 		 */
4141*eda14cbcSMatt Macy 		err = nvlist_lookup_string(rcvprops,
4142*eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
4143*eda14cbcSMatt Macy 		if (err == 0) {
4144*eda14cbcSMatt Macy 			strcpy(tmp_keylocation, keylocation);
4145*eda14cbcSMatt Macy 			(void) nvlist_remove_all(rcvprops,
4146*eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
4147*eda14cbcSMatt Macy 		}
4148*eda14cbcSMatt Macy 
4149*eda14cbcSMatt Macy 		if (flags->canmountoff) {
4150*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_add_uint64(rcvprops,
4151*eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
4152*eda14cbcSMatt Macy 		} else if (newprops) {	/* nothing in rcvprops, eliminate it */
4153*eda14cbcSMatt Macy 			nvlist_free(rcvprops);
4154*eda14cbcSMatt Macy 			rcvprops = NULL;
4155*eda14cbcSMatt Macy 			newprops = B_FALSE;
4156*eda14cbcSMatt Macy 		}
4157*eda14cbcSMatt Macy 		if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
4158*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_lookup_nvlist(lookup,
4159*eda14cbcSMatt Macy 			    snapname, &snapprops_nvlist));
4160*eda14cbcSMatt Macy 		}
4161*eda14cbcSMatt Macy 		if (holds) {
4162*eda14cbcSMatt Macy 			if (0 == nvlist_lookup_nvlist(fs, "snapholds",
4163*eda14cbcSMatt Macy 			    &lookup)) {
4164*eda14cbcSMatt Macy 				VERIFY(0 == nvlist_lookup_nvlist(lookup,
4165*eda14cbcSMatt Macy 				    snapname, &snapholds_nvlist));
4166*eda14cbcSMatt Macy 			}
4167*eda14cbcSMatt Macy 		}
4168*eda14cbcSMatt Macy 	}
4169*eda14cbcSMatt Macy 
4170*eda14cbcSMatt Macy 	cp = NULL;
4171*eda14cbcSMatt Macy 
4172*eda14cbcSMatt Macy 	/*
4173*eda14cbcSMatt Macy 	 * Determine how much of the snapshot name stored in the stream
4174*eda14cbcSMatt Macy 	 * we are going to tack on to the name they specified on the
4175*eda14cbcSMatt Macy 	 * command line, and how much we are going to chop off.
4176*eda14cbcSMatt Macy 	 *
4177*eda14cbcSMatt Macy 	 * If they specified a snapshot, chop the entire name stored in
4178*eda14cbcSMatt Macy 	 * the stream.
4179*eda14cbcSMatt Macy 	 */
4180*eda14cbcSMatt Macy 	if (flags->istail) {
4181*eda14cbcSMatt Macy 		/*
4182*eda14cbcSMatt Macy 		 * A filesystem was specified with -e. We want to tack on only
4183*eda14cbcSMatt Macy 		 * the tail of the sent snapshot path.
4184*eda14cbcSMatt Macy 		 */
4185*eda14cbcSMatt Macy 		if (strchr(tosnap, '@')) {
4186*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4187*eda14cbcSMatt Macy 			    "argument - snapshot not allowed with -e"));
4188*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4189*eda14cbcSMatt Macy 			goto out;
4190*eda14cbcSMatt Macy 		}
4191*eda14cbcSMatt Macy 
4192*eda14cbcSMatt Macy 		chopprefix = strrchr(sendfs, '/');
4193*eda14cbcSMatt Macy 
4194*eda14cbcSMatt Macy 		if (chopprefix == NULL) {
4195*eda14cbcSMatt Macy 			/*
4196*eda14cbcSMatt Macy 			 * The tail is the poolname, so we need to
4197*eda14cbcSMatt Macy 			 * prepend a path separator.
4198*eda14cbcSMatt Macy 			 */
4199*eda14cbcSMatt Macy 			int len = strlen(drrb->drr_toname);
4200*eda14cbcSMatt Macy 			cp = malloc(len + 2);
4201*eda14cbcSMatt Macy 			cp[0] = '/';
4202*eda14cbcSMatt Macy 			(void) strcpy(&cp[1], drrb->drr_toname);
4203*eda14cbcSMatt Macy 			chopprefix = cp;
4204*eda14cbcSMatt Macy 		} else {
4205*eda14cbcSMatt Macy 			chopprefix = drrb->drr_toname + (chopprefix - sendfs);
4206*eda14cbcSMatt Macy 		}
4207*eda14cbcSMatt Macy 	} else if (flags->isprefix) {
4208*eda14cbcSMatt Macy 		/*
4209*eda14cbcSMatt Macy 		 * A filesystem was specified with -d. We want to tack on
4210*eda14cbcSMatt Macy 		 * everything but the first element of the sent snapshot path
4211*eda14cbcSMatt Macy 		 * (all but the pool name).
4212*eda14cbcSMatt Macy 		 */
4213*eda14cbcSMatt Macy 		if (strchr(tosnap, '@')) {
4214*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4215*eda14cbcSMatt Macy 			    "argument - snapshot not allowed with -d"));
4216*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4217*eda14cbcSMatt Macy 			goto out;
4218*eda14cbcSMatt Macy 		}
4219*eda14cbcSMatt Macy 
4220*eda14cbcSMatt Macy 		chopprefix = strchr(drrb->drr_toname, '/');
4221*eda14cbcSMatt Macy 		if (chopprefix == NULL)
4222*eda14cbcSMatt Macy 			chopprefix = strchr(drrb->drr_toname, '@');
4223*eda14cbcSMatt Macy 	} else if (strchr(tosnap, '@') == NULL) {
4224*eda14cbcSMatt Macy 		/*
4225*eda14cbcSMatt Macy 		 * If a filesystem was specified without -d or -e, we want to
4226*eda14cbcSMatt Macy 		 * tack on everything after the fs specified by 'zfs send'.
4227*eda14cbcSMatt Macy 		 */
4228*eda14cbcSMatt Macy 		chopprefix = drrb->drr_toname + strlen(sendfs);
4229*eda14cbcSMatt Macy 	} else {
4230*eda14cbcSMatt Macy 		/* A snapshot was specified as an exact path (no -d or -e). */
4231*eda14cbcSMatt Macy 		if (recursive) {
4232*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4233*eda14cbcSMatt Macy 			    "cannot specify snapshot name for multi-snapshot "
4234*eda14cbcSMatt Macy 			    "stream"));
4235*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4236*eda14cbcSMatt Macy 			goto out;
4237*eda14cbcSMatt Macy 		}
4238*eda14cbcSMatt Macy 		chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
4239*eda14cbcSMatt Macy 	}
4240*eda14cbcSMatt Macy 
4241*eda14cbcSMatt Macy 	ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
4242*eda14cbcSMatt Macy 	ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
4243*eda14cbcSMatt Macy 	ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
4244*eda14cbcSMatt Macy 	    strchr(sendfs, '/') == NULL);
4245*eda14cbcSMatt Macy 	ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
4246*eda14cbcSMatt Macy 	    chopprefix[0] == '\0');
4247*eda14cbcSMatt Macy 
4248*eda14cbcSMatt Macy 	/*
4249*eda14cbcSMatt Macy 	 * Determine name of destination snapshot.
4250*eda14cbcSMatt Macy 	 */
4251*eda14cbcSMatt Macy 	(void) strlcpy(destsnap, tosnap, sizeof (destsnap));
4252*eda14cbcSMatt Macy 	(void) strlcat(destsnap, chopprefix, sizeof (destsnap));
4253*eda14cbcSMatt Macy 	free(cp);
4254*eda14cbcSMatt Macy 	if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
4255*eda14cbcSMatt Macy 		err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4256*eda14cbcSMatt Macy 		goto out;
4257*eda14cbcSMatt Macy 	}
4258*eda14cbcSMatt Macy 
4259*eda14cbcSMatt Macy 	/*
4260*eda14cbcSMatt Macy 	 * Determine the name of the origin snapshot.
4261*eda14cbcSMatt Macy 	 */
4262*eda14cbcSMatt Macy 	if (originsnap) {
4263*eda14cbcSMatt Macy 		(void) strlcpy(origin, originsnap, sizeof (origin));
4264*eda14cbcSMatt Macy 		if (flags->verbose)
4265*eda14cbcSMatt Macy 			(void) printf("using provided clone origin %s\n",
4266*eda14cbcSMatt Macy 			    origin);
4267*eda14cbcSMatt Macy 	} else if (drrb->drr_flags & DRR_FLAG_CLONE) {
4268*eda14cbcSMatt Macy 		if (guid_to_name(hdl, destsnap,
4269*eda14cbcSMatt Macy 		    drrb->drr_fromguid, B_FALSE, origin) != 0) {
4270*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4271*eda14cbcSMatt Macy 			    "local origin for clone %s does not exist"),
4272*eda14cbcSMatt Macy 			    destsnap);
4273*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4274*eda14cbcSMatt Macy 			goto out;
4275*eda14cbcSMatt Macy 		}
4276*eda14cbcSMatt Macy 		if (flags->verbose)
4277*eda14cbcSMatt Macy 			(void) printf("found clone origin %s\n", origin);
4278*eda14cbcSMatt Macy 	}
4279*eda14cbcSMatt Macy 
4280*eda14cbcSMatt Macy 	if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4281*eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_DEDUP)) {
4282*eda14cbcSMatt Macy 		(void) fprintf(stderr,
4283*eda14cbcSMatt Macy 		    gettext("ERROR: \"zfs receive\" no longer supports "
4284*eda14cbcSMatt Macy 		    "deduplicated send streams.  Use\n"
4285*eda14cbcSMatt Macy 		    "the \"zstream redup\" command to convert this stream "
4286*eda14cbcSMatt Macy 		    "to a regular,\n"
4287*eda14cbcSMatt Macy 		    "non-deduplicated stream.\n"));
4288*eda14cbcSMatt Macy 		err = zfs_error(hdl, EZFS_NOTSUP, errbuf);
4289*eda14cbcSMatt Macy 		goto out;
4290*eda14cbcSMatt Macy 	}
4291*eda14cbcSMatt Macy 
4292*eda14cbcSMatt Macy 	boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4293*eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_RESUMING;
4294*eda14cbcSMatt Macy 	boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4295*eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_RAW;
4296*eda14cbcSMatt Macy 	boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4297*eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_EMBED_DATA;
4298*eda14cbcSMatt Macy 	stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
4299*eda14cbcSMatt Macy 	    (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
4300*eda14cbcSMatt Macy 
4301*eda14cbcSMatt Macy 	if (stream_wantsnewfs) {
4302*eda14cbcSMatt Macy 		/*
4303*eda14cbcSMatt Macy 		 * if the parent fs does not exist, look for it based on
4304*eda14cbcSMatt Macy 		 * the parent snap GUID
4305*eda14cbcSMatt Macy 		 */
4306*eda14cbcSMatt Macy 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4307*eda14cbcSMatt Macy 		    "cannot receive new filesystem stream"));
4308*eda14cbcSMatt Macy 
4309*eda14cbcSMatt Macy 		(void) strcpy(name, destsnap);
4310*eda14cbcSMatt Macy 		cp = strrchr(name, '/');
4311*eda14cbcSMatt Macy 		if (cp)
4312*eda14cbcSMatt Macy 			*cp = '\0';
4313*eda14cbcSMatt Macy 		if (cp &&
4314*eda14cbcSMatt Macy 		    !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4315*eda14cbcSMatt Macy 			char suffix[ZFS_MAX_DATASET_NAME_LEN];
4316*eda14cbcSMatt Macy 			(void) strcpy(suffix, strrchr(destsnap, '/'));
4317*eda14cbcSMatt Macy 			if (guid_to_name(hdl, name, parent_snapguid,
4318*eda14cbcSMatt Macy 			    B_FALSE, destsnap) == 0) {
4319*eda14cbcSMatt Macy 				*strchr(destsnap, '@') = '\0';
4320*eda14cbcSMatt Macy 				(void) strcat(destsnap, suffix);
4321*eda14cbcSMatt Macy 			}
4322*eda14cbcSMatt Macy 		}
4323*eda14cbcSMatt Macy 	} else {
4324*eda14cbcSMatt Macy 		/*
4325*eda14cbcSMatt Macy 		 * If the fs does not exist, look for it based on the
4326*eda14cbcSMatt Macy 		 * fromsnap GUID.
4327*eda14cbcSMatt Macy 		 */
4328*eda14cbcSMatt Macy 		if (resuming) {
4329*eda14cbcSMatt Macy 			(void) snprintf(errbuf, sizeof (errbuf),
4330*eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN,
4331*eda14cbcSMatt Macy 			    "cannot receive resume stream"));
4332*eda14cbcSMatt Macy 		} else {
4333*eda14cbcSMatt Macy 			(void) snprintf(errbuf, sizeof (errbuf),
4334*eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN,
4335*eda14cbcSMatt Macy 			    "cannot receive incremental stream"));
4336*eda14cbcSMatt Macy 		}
4337*eda14cbcSMatt Macy 
4338*eda14cbcSMatt Macy 		(void) strcpy(name, destsnap);
4339*eda14cbcSMatt Macy 		*strchr(name, '@') = '\0';
4340*eda14cbcSMatt Macy 
4341*eda14cbcSMatt Macy 		/*
4342*eda14cbcSMatt Macy 		 * If the exact receive path was specified and this is the
4343*eda14cbcSMatt Macy 		 * topmost path in the stream, then if the fs does not exist we
4344*eda14cbcSMatt Macy 		 * should look no further.
4345*eda14cbcSMatt Macy 		 */
4346*eda14cbcSMatt Macy 		if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
4347*eda14cbcSMatt Macy 		    strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
4348*eda14cbcSMatt Macy 		    !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4349*eda14cbcSMatt Macy 			char snap[ZFS_MAX_DATASET_NAME_LEN];
4350*eda14cbcSMatt Macy 			(void) strcpy(snap, strchr(destsnap, '@'));
4351*eda14cbcSMatt Macy 			if (guid_to_name(hdl, name, drrb->drr_fromguid,
4352*eda14cbcSMatt Macy 			    B_FALSE, destsnap) == 0) {
4353*eda14cbcSMatt Macy 				*strchr(destsnap, '@') = '\0';
4354*eda14cbcSMatt Macy 				(void) strcat(destsnap, snap);
4355*eda14cbcSMatt Macy 			}
4356*eda14cbcSMatt Macy 		}
4357*eda14cbcSMatt Macy 	}
4358*eda14cbcSMatt Macy 
4359*eda14cbcSMatt Macy 	(void) strcpy(name, destsnap);
4360*eda14cbcSMatt Macy 	*strchr(name, '@') = '\0';
4361*eda14cbcSMatt Macy 
4362*eda14cbcSMatt Macy 	redacted = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4363*eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_REDACTED;
4364*eda14cbcSMatt Macy 
4365*eda14cbcSMatt Macy 	if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4366*eda14cbcSMatt Macy 		zfs_cmd_t zc = {"\0"};
4367*eda14cbcSMatt Macy 		zfs_handle_t *zhp;
4368*eda14cbcSMatt Macy 		boolean_t encrypted;
4369*eda14cbcSMatt Macy 
4370*eda14cbcSMatt Macy 		(void) strcpy(zc.zc_name, name);
4371*eda14cbcSMatt Macy 
4372*eda14cbcSMatt Macy 		/*
4373*eda14cbcSMatt Macy 		 * Destination fs exists.  It must be one of these cases:
4374*eda14cbcSMatt Macy 		 *  - an incremental send stream
4375*eda14cbcSMatt Macy 		 *  - the stream specifies a new fs (full stream or clone)
4376*eda14cbcSMatt Macy 		 *    and they want us to blow away the existing fs (and
4377*eda14cbcSMatt Macy 		 *    have therefore specified -F and removed any snapshots)
4378*eda14cbcSMatt Macy 		 *  - we are resuming a failed receive.
4379*eda14cbcSMatt Macy 		 */
4380*eda14cbcSMatt Macy 		if (stream_wantsnewfs) {
4381*eda14cbcSMatt Macy 			boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL;
4382*eda14cbcSMatt Macy 			if (!flags->force) {
4383*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4384*eda14cbcSMatt Macy 				    "destination '%s' exists\n"
4385*eda14cbcSMatt Macy 				    "must specify -F to overwrite it"), name);
4386*eda14cbcSMatt Macy 				err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4387*eda14cbcSMatt Macy 				goto out;
4388*eda14cbcSMatt Macy 			}
4389*eda14cbcSMatt Macy 			if (zfs_ioctl(hdl, ZFS_IOC_SNAPSHOT_LIST_NEXT,
4390*eda14cbcSMatt Macy 			    &zc) == 0) {
4391*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4392*eda14cbcSMatt Macy 				    "destination has snapshots (eg. %s)\n"
4393*eda14cbcSMatt Macy 				    "must destroy them to overwrite it"),
4394*eda14cbcSMatt Macy 				    zc.zc_name);
4395*eda14cbcSMatt Macy 				err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4396*eda14cbcSMatt Macy 				goto out;
4397*eda14cbcSMatt Macy 			}
4398*eda14cbcSMatt Macy 			if (is_volume && strrchr(name, '/') == NULL) {
4399*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4400*eda14cbcSMatt Macy 				    "destination %s is the root dataset\n"
4401*eda14cbcSMatt Macy 				    "cannot overwrite with a ZVOL"),
4402*eda14cbcSMatt Macy 				    name);
4403*eda14cbcSMatt Macy 				err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4404*eda14cbcSMatt Macy 				goto out;
4405*eda14cbcSMatt Macy 			}
4406*eda14cbcSMatt Macy 			if (is_volume &&
4407*eda14cbcSMatt Macy 			    zfs_ioctl(hdl, ZFS_IOC_DATASET_LIST_NEXT,
4408*eda14cbcSMatt Macy 			    &zc) == 0) {
4409*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4410*eda14cbcSMatt Macy 				    "destination has children (eg. %s)\n"
4411*eda14cbcSMatt Macy 				    "cannot overwrite with a ZVOL"),
4412*eda14cbcSMatt Macy 				    zc.zc_name);
4413*eda14cbcSMatt Macy 				err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4414*eda14cbcSMatt Macy 				goto out;
4415*eda14cbcSMatt Macy 			}
4416*eda14cbcSMatt Macy 		}
4417*eda14cbcSMatt Macy 
4418*eda14cbcSMatt Macy 		if ((zhp = zfs_open(hdl, name,
4419*eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
4420*eda14cbcSMatt Macy 			err = -1;
4421*eda14cbcSMatt Macy 			goto out;
4422*eda14cbcSMatt Macy 		}
4423*eda14cbcSMatt Macy 
4424*eda14cbcSMatt Macy 		if (stream_wantsnewfs &&
4425*eda14cbcSMatt Macy 		    zhp->zfs_dmustats.dds_origin[0]) {
4426*eda14cbcSMatt Macy 			zfs_close(zhp);
4427*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4428*eda14cbcSMatt Macy 			    "destination '%s' is a clone\n"
4429*eda14cbcSMatt Macy 			    "must destroy it to overwrite it"), name);
4430*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4431*eda14cbcSMatt Macy 			goto out;
4432*eda14cbcSMatt Macy 		}
4433*eda14cbcSMatt Macy 
4434*eda14cbcSMatt Macy 		/*
4435*eda14cbcSMatt Macy 		 * Raw sends can not be performed as an incremental on top
4436*eda14cbcSMatt Macy 		 * of existing unencrypted datasets. zfs recv -F can't be
4437*eda14cbcSMatt Macy 		 * used to blow away an existing encrypted filesystem. This
4438*eda14cbcSMatt Macy 		 * is because it would require the dsl dir to point to the
4439*eda14cbcSMatt Macy 		 * new key (or lack of a key) and the old key at the same
4440*eda14cbcSMatt Macy 		 * time. The -F flag may still be used for deleting
4441*eda14cbcSMatt Macy 		 * intermediate snapshots that would otherwise prevent the
4442*eda14cbcSMatt Macy 		 * receive from working.
4443*eda14cbcSMatt Macy 		 */
4444*eda14cbcSMatt Macy 		encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
4445*eda14cbcSMatt Macy 		    ZIO_CRYPT_OFF;
4446*eda14cbcSMatt Macy 		if (!stream_wantsnewfs && !encrypted && raw) {
4447*eda14cbcSMatt Macy 			zfs_close(zhp);
4448*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4449*eda14cbcSMatt Macy 			    "cannot perform raw receive on top of "
4450*eda14cbcSMatt Macy 			    "existing unencrypted dataset"));
4451*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4452*eda14cbcSMatt Macy 			goto out;
4453*eda14cbcSMatt Macy 		}
4454*eda14cbcSMatt Macy 
4455*eda14cbcSMatt Macy 		if (stream_wantsnewfs && flags->force &&
4456*eda14cbcSMatt Macy 		    ((raw && !encrypted) || encrypted)) {
4457*eda14cbcSMatt Macy 			zfs_close(zhp);
4458*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4459*eda14cbcSMatt Macy 			    "zfs receive -F cannot be used to destroy an "
4460*eda14cbcSMatt Macy 			    "encrypted filesystem or overwrite an "
4461*eda14cbcSMatt Macy 			    "unencrypted one with an encrypted one"));
4462*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4463*eda14cbcSMatt Macy 			goto out;
4464*eda14cbcSMatt Macy 		}
4465*eda14cbcSMatt Macy 
4466*eda14cbcSMatt Macy 		if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4467*eda14cbcSMatt Macy 		    stream_wantsnewfs) {
4468*eda14cbcSMatt Macy 			/* We can't do online recv in this case */
4469*eda14cbcSMatt Macy 			clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4470*eda14cbcSMatt Macy 			    flags->forceunmount ? MS_FORCE : 0);
4471*eda14cbcSMatt Macy 			if (clp == NULL) {
4472*eda14cbcSMatt Macy 				zfs_close(zhp);
4473*eda14cbcSMatt Macy 				err = -1;
4474*eda14cbcSMatt Macy 				goto out;
4475*eda14cbcSMatt Macy 			}
4476*eda14cbcSMatt Macy 			if (changelist_prefix(clp) != 0) {
4477*eda14cbcSMatt Macy 				changelist_free(clp);
4478*eda14cbcSMatt Macy 				zfs_close(zhp);
4479*eda14cbcSMatt Macy 				err = -1;
4480*eda14cbcSMatt Macy 				goto out;
4481*eda14cbcSMatt Macy 			}
4482*eda14cbcSMatt Macy 		}
4483*eda14cbcSMatt Macy 
4484*eda14cbcSMatt Macy 		/*
4485*eda14cbcSMatt Macy 		 * If we are resuming a newfs, set newfs here so that we will
4486*eda14cbcSMatt Macy 		 * mount it if the recv succeeds this time.  We can tell
4487*eda14cbcSMatt Macy 		 * that it was a newfs on the first recv because the fs
4488*eda14cbcSMatt Macy 		 * itself will be inconsistent (if the fs existed when we
4489*eda14cbcSMatt Macy 		 * did the first recv, we would have received it into
4490*eda14cbcSMatt Macy 		 * .../%recv).
4491*eda14cbcSMatt Macy 		 */
4492*eda14cbcSMatt Macy 		if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4493*eda14cbcSMatt Macy 			newfs = B_TRUE;
4494*eda14cbcSMatt Macy 
4495*eda14cbcSMatt Macy 		/* we want to know if we're zoned when validating -o|-x props */
4496*eda14cbcSMatt Macy 		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4497*eda14cbcSMatt Macy 
4498*eda14cbcSMatt Macy 		/* may need this info later, get it now we have zhp around */
4499*eda14cbcSMatt Macy 		if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4500*eda14cbcSMatt Macy 		    NULL, NULL, 0, B_TRUE) == 0)
4501*eda14cbcSMatt Macy 			hastoken = B_TRUE;
4502*eda14cbcSMatt Macy 
4503*eda14cbcSMatt Macy 		/* gather existing properties on destination */
4504*eda14cbcSMatt Macy 		origprops = fnvlist_alloc();
4505*eda14cbcSMatt Macy 		fnvlist_merge(origprops, zhp->zfs_props);
4506*eda14cbcSMatt Macy 		fnvlist_merge(origprops, zhp->zfs_user_props);
4507*eda14cbcSMatt Macy 
4508*eda14cbcSMatt Macy 		zfs_close(zhp);
4509*eda14cbcSMatt Macy 	} else {
4510*eda14cbcSMatt Macy 		zfs_handle_t *zhp;
4511*eda14cbcSMatt Macy 
4512*eda14cbcSMatt Macy 		/*
4513*eda14cbcSMatt Macy 		 * Destination filesystem does not exist.  Therefore we better
4514*eda14cbcSMatt Macy 		 * be creating a new filesystem (either from a full backup, or
4515*eda14cbcSMatt Macy 		 * a clone).  It would therefore be invalid if the user
4516*eda14cbcSMatt Macy 		 * specified only the pool name (i.e. if the destination name
4517*eda14cbcSMatt Macy 		 * contained no slash character).
4518*eda14cbcSMatt Macy 		 */
4519*eda14cbcSMatt Macy 		cp = strrchr(name, '/');
4520*eda14cbcSMatt Macy 
4521*eda14cbcSMatt Macy 		if (!stream_wantsnewfs || cp == NULL) {
4522*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4523*eda14cbcSMatt Macy 			    "destination '%s' does not exist"), name);
4524*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4525*eda14cbcSMatt Macy 			goto out;
4526*eda14cbcSMatt Macy 		}
4527*eda14cbcSMatt Macy 
4528*eda14cbcSMatt Macy 		/*
4529*eda14cbcSMatt Macy 		 * Trim off the final dataset component so we perform the
4530*eda14cbcSMatt Macy 		 * recvbackup ioctl to the filesystems's parent.
4531*eda14cbcSMatt Macy 		 */
4532*eda14cbcSMatt Macy 		*cp = '\0';
4533*eda14cbcSMatt Macy 
4534*eda14cbcSMatt Macy 		if (flags->isprefix && !flags->istail && !flags->dryrun &&
4535*eda14cbcSMatt Macy 		    create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4536*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4537*eda14cbcSMatt Macy 			goto out;
4538*eda14cbcSMatt Macy 		}
4539*eda14cbcSMatt Macy 
4540*eda14cbcSMatt Macy 		/* validate parent */
4541*eda14cbcSMatt Macy 		zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4542*eda14cbcSMatt Macy 		if (zhp == NULL) {
4543*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4544*eda14cbcSMatt Macy 			goto out;
4545*eda14cbcSMatt Macy 		}
4546*eda14cbcSMatt Macy 		if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
4547*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4548*eda14cbcSMatt Macy 			    "parent '%s' is not a filesystem"), name);
4549*eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4550*eda14cbcSMatt Macy 			zfs_close(zhp);
4551*eda14cbcSMatt Macy 			goto out;
4552*eda14cbcSMatt Macy 		}
4553*eda14cbcSMatt Macy 
4554*eda14cbcSMatt Macy 		zfs_close(zhp);
4555*eda14cbcSMatt Macy 
4556*eda14cbcSMatt Macy 		newfs = B_TRUE;
4557*eda14cbcSMatt Macy 		*cp = '/';
4558*eda14cbcSMatt Macy 	}
4559*eda14cbcSMatt Macy 
4560*eda14cbcSMatt Macy 	if (flags->verbose) {
4561*eda14cbcSMatt Macy 		(void) printf("%s %s stream of %s into %s\n",
4562*eda14cbcSMatt Macy 		    flags->dryrun ? "would receive" : "receiving",
4563*eda14cbcSMatt Macy 		    drrb->drr_fromguid ? "incremental" : "full",
4564*eda14cbcSMatt Macy 		    drrb->drr_toname, destsnap);
4565*eda14cbcSMatt Macy 		(void) fflush(stdout);
4566*eda14cbcSMatt Macy 	}
4567*eda14cbcSMatt Macy 
4568*eda14cbcSMatt Macy 	if (flags->dryrun) {
4569*eda14cbcSMatt Macy 		void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
4570*eda14cbcSMatt Macy 
4571*eda14cbcSMatt Macy 		/*
4572*eda14cbcSMatt Macy 		 * We have read the DRR_BEGIN record, but we have
4573*eda14cbcSMatt Macy 		 * not yet read the payload. For non-dryrun sends
4574*eda14cbcSMatt Macy 		 * this will be done by the kernel, so we must
4575*eda14cbcSMatt Macy 		 * emulate that here, before attempting to read
4576*eda14cbcSMatt Macy 		 * more records.
4577*eda14cbcSMatt Macy 		 */
4578*eda14cbcSMatt Macy 		err = recv_read(hdl, infd, buf, drr->drr_payloadlen,
4579*eda14cbcSMatt Macy 		    flags->byteswap, NULL);
4580*eda14cbcSMatt Macy 		free(buf);
4581*eda14cbcSMatt Macy 		if (err != 0)
4582*eda14cbcSMatt Macy 			goto out;
4583*eda14cbcSMatt Macy 
4584*eda14cbcSMatt Macy 		err = recv_skip(hdl, infd, flags->byteswap);
4585*eda14cbcSMatt Macy 		goto out;
4586*eda14cbcSMatt Macy 	}
4587*eda14cbcSMatt Macy 
4588*eda14cbcSMatt Macy 	/*
4589*eda14cbcSMatt Macy 	 * If this is the top-level dataset, record it so we can use it
4590*eda14cbcSMatt Macy 	 * for recursive operations later.
4591*eda14cbcSMatt Macy 	 */
4592*eda14cbcSMatt Macy 	if (top_zfs != NULL &&
4593*eda14cbcSMatt Macy 	    (*top_zfs == NULL || strcmp(*top_zfs, name) == 0)) {
4594*eda14cbcSMatt Macy 		toplevel = B_TRUE;
4595*eda14cbcSMatt Macy 		if (*top_zfs == NULL)
4596*eda14cbcSMatt Macy 			*top_zfs = zfs_strdup(hdl, name);
4597*eda14cbcSMatt Macy 	}
4598*eda14cbcSMatt Macy 
4599*eda14cbcSMatt Macy 	if (drrb->drr_type == DMU_OST_ZVOL) {
4600*eda14cbcSMatt Macy 		type = ZFS_TYPE_VOLUME;
4601*eda14cbcSMatt Macy 	} else if (drrb->drr_type == DMU_OST_ZFS) {
4602*eda14cbcSMatt Macy 		type = ZFS_TYPE_FILESYSTEM;
4603*eda14cbcSMatt Macy 	} else {
4604*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4605*eda14cbcSMatt Macy 		    "invalid record type: 0x%d"), drrb->drr_type);
4606*eda14cbcSMatt Macy 		err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4607*eda14cbcSMatt Macy 		goto out;
4608*eda14cbcSMatt Macy 	}
4609*eda14cbcSMatt Macy 	if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4610*eda14cbcSMatt Macy 	    stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4611*eda14cbcSMatt Macy 	    &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
4612*eda14cbcSMatt Macy 		goto out;
4613*eda14cbcSMatt Macy 
4614*eda14cbcSMatt Macy 	/*
4615*eda14cbcSMatt Macy 	 * When sending with properties (zfs send -p), the encryption property
4616*eda14cbcSMatt Macy 	 * is not included because it is a SETONCE property and therefore
4617*eda14cbcSMatt Macy 	 * treated as read only. However, we are always able to determine its
4618*eda14cbcSMatt Macy 	 * value because raw sends will include it in the DRR_BDEGIN payload
4619*eda14cbcSMatt Macy 	 * and non-raw sends with properties are not allowed for encrypted
4620*eda14cbcSMatt Macy 	 * datasets. Therefore, if this is a non-raw properties stream, we can
4621*eda14cbcSMatt Macy 	 * infer that the value should be ZIO_CRYPT_OFF and manually add that
4622*eda14cbcSMatt Macy 	 * to the received properties.
4623*eda14cbcSMatt Macy 	 */
4624*eda14cbcSMatt Macy 	if (stream_wantsnewfs && !raw && rcvprops != NULL &&
4625*eda14cbcSMatt Macy 	    !nvlist_exists(cmdprops, zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
4626*eda14cbcSMatt Macy 		if (oxprops == NULL)
4627*eda14cbcSMatt Macy 			oxprops = fnvlist_alloc();
4628*eda14cbcSMatt Macy 		fnvlist_add_uint64(oxprops,
4629*eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_ENCRYPTION), ZIO_CRYPT_OFF);
4630*eda14cbcSMatt Macy 	}
4631*eda14cbcSMatt Macy 
4632*eda14cbcSMatt Macy 	err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
4633*eda14cbcSMatt Macy 	    oxprops, wkeydata, wkeylen, origin, flags->force, flags->resumable,
4634*eda14cbcSMatt Macy 	    raw, infd, drr_noswap, -1, &read_bytes, &errflags,
4635*eda14cbcSMatt Macy 	    NULL, &prop_errors);
4636*eda14cbcSMatt Macy 	ioctl_errno = ioctl_err;
4637*eda14cbcSMatt Macy 	prop_errflags = errflags;
4638*eda14cbcSMatt Macy 
4639*eda14cbcSMatt Macy 	if (err == 0) {
4640*eda14cbcSMatt Macy 		nvpair_t *prop_err = NULL;
4641*eda14cbcSMatt Macy 
4642*eda14cbcSMatt Macy 		while ((prop_err = nvlist_next_nvpair(prop_errors,
4643*eda14cbcSMatt Macy 		    prop_err)) != NULL) {
4644*eda14cbcSMatt Macy 			char tbuf[1024];
4645*eda14cbcSMatt Macy 			zfs_prop_t prop;
4646*eda14cbcSMatt Macy 			int intval;
4647*eda14cbcSMatt Macy 
4648*eda14cbcSMatt Macy 			prop = zfs_name_to_prop(nvpair_name(prop_err));
4649*eda14cbcSMatt Macy 			(void) nvpair_value_int32(prop_err, &intval);
4650*eda14cbcSMatt Macy 			if (strcmp(nvpair_name(prop_err),
4651*eda14cbcSMatt Macy 			    ZPROP_N_MORE_ERRORS) == 0) {
4652*eda14cbcSMatt Macy 				trunc_prop_errs(intval);
4653*eda14cbcSMatt Macy 				break;
4654*eda14cbcSMatt Macy 			} else if (snapname == NULL || finalsnap == NULL ||
4655*eda14cbcSMatt Macy 			    strcmp(finalsnap, snapname) == 0 ||
4656*eda14cbcSMatt Macy 			    strcmp(nvpair_name(prop_err),
4657*eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
4658*eda14cbcSMatt Macy 				/*
4659*eda14cbcSMatt Macy 				 * Skip the special case of, for example,
4660*eda14cbcSMatt Macy 				 * "refquota", errors on intermediate
4661*eda14cbcSMatt Macy 				 * snapshots leading up to a final one.
4662*eda14cbcSMatt Macy 				 * That's why we have all of the checks above.
4663*eda14cbcSMatt Macy 				 *
4664*eda14cbcSMatt Macy 				 * See zfs_ioctl.c's extract_delay_props() for
4665*eda14cbcSMatt Macy 				 * a list of props which can fail on
4666*eda14cbcSMatt Macy 				 * intermediate snapshots, but shouldn't
4667*eda14cbcSMatt Macy 				 * affect the overall receive.
4668*eda14cbcSMatt Macy 				 */
4669*eda14cbcSMatt Macy 				(void) snprintf(tbuf, sizeof (tbuf),
4670*eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN,
4671*eda14cbcSMatt Macy 				    "cannot receive %s property on %s"),
4672*eda14cbcSMatt Macy 				    nvpair_name(prop_err), name);
4673*eda14cbcSMatt Macy 				zfs_setprop_error(hdl, prop, intval, tbuf);
4674*eda14cbcSMatt Macy 			}
4675*eda14cbcSMatt Macy 		}
4676*eda14cbcSMatt Macy 	}
4677*eda14cbcSMatt Macy 
4678*eda14cbcSMatt Macy 	if (err == 0 && snapprops_nvlist) {
4679*eda14cbcSMatt Macy 		zfs_cmd_t zc = {"\0"};
4680*eda14cbcSMatt Macy 
4681*eda14cbcSMatt Macy 		(void) strcpy(zc.zc_name, destsnap);
4682*eda14cbcSMatt Macy 		zc.zc_cookie = B_TRUE; /* received */
4683*eda14cbcSMatt Macy 		if (zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist) == 0) {
4684*eda14cbcSMatt Macy 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
4685*eda14cbcSMatt Macy 			zcmd_free_nvlists(&zc);
4686*eda14cbcSMatt Macy 		}
4687*eda14cbcSMatt Macy 	}
4688*eda14cbcSMatt Macy 	if (err == 0 && snapholds_nvlist) {
4689*eda14cbcSMatt Macy 		nvpair_t *pair;
4690*eda14cbcSMatt Macy 		nvlist_t *holds, *errors = NULL;
4691*eda14cbcSMatt Macy 		int cleanup_fd = -1;
4692*eda14cbcSMatt Macy 
4693*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
4694*eda14cbcSMatt Macy 		for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
4695*eda14cbcSMatt Macy 		    pair != NULL;
4696*eda14cbcSMatt Macy 		    pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
4697*eda14cbcSMatt Macy 			VERIFY(0 == nvlist_add_string(holds, destsnap,
4698*eda14cbcSMatt Macy 			    nvpair_name(pair)));
4699*eda14cbcSMatt Macy 		}
4700*eda14cbcSMatt Macy 		(void) lzc_hold(holds, cleanup_fd, &errors);
4701*eda14cbcSMatt Macy 		nvlist_free(snapholds_nvlist);
4702*eda14cbcSMatt Macy 		nvlist_free(holds);
4703*eda14cbcSMatt Macy 	}
4704*eda14cbcSMatt Macy 
4705*eda14cbcSMatt Macy 	if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
4706*eda14cbcSMatt Macy 		/*
4707*eda14cbcSMatt Macy 		 * It may be that this snapshot already exists,
4708*eda14cbcSMatt Macy 		 * in which case we want to consume & ignore it
4709*eda14cbcSMatt Macy 		 * rather than failing.
4710*eda14cbcSMatt Macy 		 */
4711*eda14cbcSMatt Macy 		avl_tree_t *local_avl;
4712*eda14cbcSMatt Macy 		nvlist_t *local_nv, *fs;
4713*eda14cbcSMatt Macy 		cp = strchr(destsnap, '@');
4714*eda14cbcSMatt Macy 
4715*eda14cbcSMatt Macy 		/*
4716*eda14cbcSMatt Macy 		 * XXX Do this faster by just iterating over snaps in
4717*eda14cbcSMatt Macy 		 * this fs.  Also if zc_value does not exist, we will
4718*eda14cbcSMatt Macy 		 * get a strange "does not exist" error message.
4719*eda14cbcSMatt Macy 		 */
4720*eda14cbcSMatt Macy 		*cp = '\0';
4721*eda14cbcSMatt Macy 		if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
4722*eda14cbcSMatt Macy 		    B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_TRUE,
4723*eda14cbcSMatt Macy 		    &local_nv, &local_avl) == 0) {
4724*eda14cbcSMatt Macy 			*cp = '@';
4725*eda14cbcSMatt Macy 			fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
4726*eda14cbcSMatt Macy 			fsavl_destroy(local_avl);
4727*eda14cbcSMatt Macy 			nvlist_free(local_nv);
4728*eda14cbcSMatt Macy 
4729*eda14cbcSMatt Macy 			if (fs != NULL) {
4730*eda14cbcSMatt Macy 				if (flags->verbose) {
4731*eda14cbcSMatt Macy 					(void) printf("snap %s already exists; "
4732*eda14cbcSMatt Macy 					    "ignoring\n", destsnap);
4733*eda14cbcSMatt Macy 				}
4734*eda14cbcSMatt Macy 				err = ioctl_err = recv_skip(hdl, infd,
4735*eda14cbcSMatt Macy 				    flags->byteswap);
4736*eda14cbcSMatt Macy 			}
4737*eda14cbcSMatt Macy 		}
4738*eda14cbcSMatt Macy 		*cp = '@';
4739*eda14cbcSMatt Macy 	}
4740*eda14cbcSMatt Macy 
4741*eda14cbcSMatt Macy 	if (ioctl_err != 0) {
4742*eda14cbcSMatt Macy 		switch (ioctl_errno) {
4743*eda14cbcSMatt Macy 		case ENODEV:
4744*eda14cbcSMatt Macy 			cp = strchr(destsnap, '@');
4745*eda14cbcSMatt Macy 			*cp = '\0';
4746*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4747*eda14cbcSMatt Macy 			    "most recent snapshot of %s does not\n"
4748*eda14cbcSMatt Macy 			    "match incremental source"), destsnap);
4749*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4750*eda14cbcSMatt Macy 			*cp = '@';
4751*eda14cbcSMatt Macy 			break;
4752*eda14cbcSMatt Macy 		case ETXTBSY:
4753*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4754*eda14cbcSMatt Macy 			    "destination %s has been modified\n"
4755*eda14cbcSMatt Macy 			    "since most recent snapshot"), name);
4756*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4757*eda14cbcSMatt Macy 			break;
4758*eda14cbcSMatt Macy 		case EACCES:
4759*eda14cbcSMatt Macy 			if (raw && stream_wantsnewfs) {
4760*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4761*eda14cbcSMatt Macy 				    "failed to create encryption key"));
4762*eda14cbcSMatt Macy 			} else if (raw && !stream_wantsnewfs) {
4763*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4764*eda14cbcSMatt Macy 				    "encryption key does not match "
4765*eda14cbcSMatt Macy 				    "existing key"));
4766*eda14cbcSMatt Macy 			} else {
4767*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4768*eda14cbcSMatt Macy 				    "inherited key must be loaded"));
4769*eda14cbcSMatt Macy 			}
4770*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4771*eda14cbcSMatt Macy 			break;
4772*eda14cbcSMatt Macy 		case EEXIST:
4773*eda14cbcSMatt Macy 			cp = strchr(destsnap, '@');
4774*eda14cbcSMatt Macy 			if (newfs) {
4775*eda14cbcSMatt Macy 				/* it's the containing fs that exists */
4776*eda14cbcSMatt Macy 				*cp = '\0';
4777*eda14cbcSMatt Macy 			}
4778*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4779*eda14cbcSMatt Macy 			    "destination already exists"));
4780*eda14cbcSMatt Macy 			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
4781*eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
4782*eda14cbcSMatt Macy 			    destsnap);
4783*eda14cbcSMatt Macy 			*cp = '@';
4784*eda14cbcSMatt Macy 			break;
4785*eda14cbcSMatt Macy 		case EINVAL:
4786*eda14cbcSMatt Macy 			if (flags->resumable) {
4787*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4788*eda14cbcSMatt Macy 				    "kernel modules must be upgraded to "
4789*eda14cbcSMatt Macy 				    "receive this stream."));
4790*eda14cbcSMatt Macy 			} else if (embedded && !raw) {
4791*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4792*eda14cbcSMatt Macy 				    "incompatible embedded data stream "
4793*eda14cbcSMatt Macy 				    "feature with encrypted receive."));
4794*eda14cbcSMatt Macy 			}
4795*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4796*eda14cbcSMatt Macy 			break;
4797*eda14cbcSMatt Macy 		case ECKSUM:
4798*eda14cbcSMatt Macy 		case ZFS_ERR_STREAM_TRUNCATED:
4799*eda14cbcSMatt Macy 			recv_ecksum_set_aux(hdl, destsnap, flags->resumable,
4800*eda14cbcSMatt Macy 			    ioctl_err == ECKSUM);
4801*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4802*eda14cbcSMatt Macy 			break;
4803*eda14cbcSMatt Macy 		case ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH:
4804*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4805*eda14cbcSMatt Macy 			    "incremental send stream requires -L "
4806*eda14cbcSMatt Macy 			    "(--large-block), to match previous receive."));
4807*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4808*eda14cbcSMatt Macy 			break;
4809*eda14cbcSMatt Macy 		case ENOTSUP:
4810*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4811*eda14cbcSMatt Macy 			    "pool must be upgraded to receive this stream."));
4812*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4813*eda14cbcSMatt Macy 			break;
4814*eda14cbcSMatt Macy 		case EDQUOT:
4815*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4816*eda14cbcSMatt Macy 			    "destination %s space quota exceeded."), name);
4817*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_NOSPC, errbuf);
4818*eda14cbcSMatt Macy 			break;
4819*eda14cbcSMatt Macy 		case ZFS_ERR_FROM_IVSET_GUID_MISSING:
4820*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4821*eda14cbcSMatt Macy 			    "IV set guid missing. See errata %u at "
4822*eda14cbcSMatt Macy 			    "https://zfsonlinux.org/msg/ZFS-8000-ER."),
4823*eda14cbcSMatt Macy 			    ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
4824*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4825*eda14cbcSMatt Macy 			break;
4826*eda14cbcSMatt Macy 		case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
4827*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4828*eda14cbcSMatt Macy 			    "IV set guid mismatch. See the 'zfs receive' "
4829*eda14cbcSMatt Macy 			    "man page section\n discussing the limitations "
4830*eda14cbcSMatt Macy 			    "of raw encrypted send streams."));
4831*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4832*eda14cbcSMatt Macy 			break;
4833*eda14cbcSMatt Macy 		case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING:
4834*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4835*eda14cbcSMatt Macy 			    "Spill block flag missing for raw send.\n"
4836*eda14cbcSMatt Macy 			    "The zfs software on the sending system must "
4837*eda14cbcSMatt Macy 			    "be updated."));
4838*eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4839*eda14cbcSMatt Macy 			break;
4840*eda14cbcSMatt Macy 		case EBUSY:
4841*eda14cbcSMatt Macy 			if (hastoken) {
4842*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4843*eda14cbcSMatt Macy 				    "destination %s contains "
4844*eda14cbcSMatt Macy 				    "partially-complete state from "
4845*eda14cbcSMatt Macy 				    "\"zfs receive -s\"."), name);
4846*eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BUSY, errbuf);
4847*eda14cbcSMatt Macy 				break;
4848*eda14cbcSMatt Macy 			}
4849*eda14cbcSMatt Macy 			/* fallthru */
4850*eda14cbcSMatt Macy 		default:
4851*eda14cbcSMatt Macy 			(void) zfs_standard_error(hdl, ioctl_errno, errbuf);
4852*eda14cbcSMatt Macy 		}
4853*eda14cbcSMatt Macy 	}
4854*eda14cbcSMatt Macy 
4855*eda14cbcSMatt Macy 	/*
4856*eda14cbcSMatt Macy 	 * Mount the target filesystem (if created).  Also mount any
4857*eda14cbcSMatt Macy 	 * children of the target filesystem if we did a replication
4858*eda14cbcSMatt Macy 	 * receive (indicated by stream_avl being non-NULL).
4859*eda14cbcSMatt Macy 	 */
4860*eda14cbcSMatt Macy 	if (clp) {
4861*eda14cbcSMatt Macy 		if (!flags->nomount)
4862*eda14cbcSMatt Macy 			err |= changelist_postfix(clp);
4863*eda14cbcSMatt Macy 		changelist_free(clp);
4864*eda14cbcSMatt Macy 	}
4865*eda14cbcSMatt Macy 
4866*eda14cbcSMatt Macy 	if ((newfs || stream_avl) && type == ZFS_TYPE_FILESYSTEM && !redacted)
4867*eda14cbcSMatt Macy 		flags->domount = B_TRUE;
4868*eda14cbcSMatt Macy 
4869*eda14cbcSMatt Macy 	if (prop_errflags & ZPROP_ERR_NOCLEAR) {
4870*eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4871*eda14cbcSMatt Macy 		    "failed to clear unreceived properties on %s"), name);
4872*eda14cbcSMatt Macy 		(void) fprintf(stderr, "\n");
4873*eda14cbcSMatt Macy 	}
4874*eda14cbcSMatt Macy 	if (prop_errflags & ZPROP_ERR_NORESTORE) {
4875*eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4876*eda14cbcSMatt Macy 		    "failed to restore original properties on %s"), name);
4877*eda14cbcSMatt Macy 		(void) fprintf(stderr, "\n");
4878*eda14cbcSMatt Macy 	}
4879*eda14cbcSMatt Macy 
4880*eda14cbcSMatt Macy 	if (err || ioctl_err) {
4881*eda14cbcSMatt Macy 		err = -1;
4882*eda14cbcSMatt Macy 		goto out;
4883*eda14cbcSMatt Macy 	}
4884*eda14cbcSMatt Macy 
4885*eda14cbcSMatt Macy 	if (flags->verbose) {
4886*eda14cbcSMatt Macy 		char buf1[64];
4887*eda14cbcSMatt Macy 		char buf2[64];
4888*eda14cbcSMatt Macy 		uint64_t bytes = read_bytes;
4889*eda14cbcSMatt Macy 		time_t delta = time(NULL) - begin_time;
4890*eda14cbcSMatt Macy 		if (delta == 0)
4891*eda14cbcSMatt Macy 			delta = 1;
4892*eda14cbcSMatt Macy 		zfs_nicebytes(bytes, buf1, sizeof (buf1));
4893*eda14cbcSMatt Macy 		zfs_nicebytes(bytes/delta, buf2, sizeof (buf1));
4894*eda14cbcSMatt Macy 
4895*eda14cbcSMatt Macy 		(void) printf("received %s stream in %lld seconds (%s/sec)\n",
4896*eda14cbcSMatt Macy 		    buf1, (longlong_t)delta, buf2);
4897*eda14cbcSMatt Macy 	}
4898*eda14cbcSMatt Macy 
4899*eda14cbcSMatt Macy 	err = 0;
4900*eda14cbcSMatt Macy out:
4901*eda14cbcSMatt Macy 	if (prop_errors != NULL)
4902*eda14cbcSMatt Macy 		nvlist_free(prop_errors);
4903*eda14cbcSMatt Macy 
4904*eda14cbcSMatt Macy 	if (tmp_keylocation[0] != '\0') {
4905*eda14cbcSMatt Macy 		VERIFY(0 == nvlist_add_string(rcvprops,
4906*eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation));
4907*eda14cbcSMatt Macy 	}
4908*eda14cbcSMatt Macy 
4909*eda14cbcSMatt Macy 	if (newprops)
4910*eda14cbcSMatt Macy 		nvlist_free(rcvprops);
4911*eda14cbcSMatt Macy 
4912*eda14cbcSMatt Macy 	nvlist_free(oxprops);
4913*eda14cbcSMatt Macy 	nvlist_free(origprops);
4914*eda14cbcSMatt Macy 
4915*eda14cbcSMatt Macy 	return (err);
4916*eda14cbcSMatt Macy }
4917*eda14cbcSMatt Macy 
4918*eda14cbcSMatt Macy /*
4919*eda14cbcSMatt Macy  * Check properties we were asked to override (both -o|-x)
4920*eda14cbcSMatt Macy  */
4921*eda14cbcSMatt Macy static boolean_t
4922*eda14cbcSMatt Macy zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
4923*eda14cbcSMatt Macy     const char *errbuf)
4924*eda14cbcSMatt Macy {
4925*eda14cbcSMatt Macy 	nvpair_t *nvp;
4926*eda14cbcSMatt Macy 	zfs_prop_t prop;
4927*eda14cbcSMatt Macy 	const char *name;
4928*eda14cbcSMatt Macy 
4929*eda14cbcSMatt Macy 	nvp = NULL;
4930*eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
4931*eda14cbcSMatt Macy 		name = nvpair_name(nvp);
4932*eda14cbcSMatt Macy 		prop = zfs_name_to_prop(name);
4933*eda14cbcSMatt Macy 
4934*eda14cbcSMatt Macy 		if (prop == ZPROP_INVAL) {
4935*eda14cbcSMatt Macy 			if (!zfs_prop_user(name)) {
4936*eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4937*eda14cbcSMatt Macy 				    "invalid property '%s'"), name);
4938*eda14cbcSMatt Macy 				return (B_FALSE);
4939*eda14cbcSMatt Macy 			}
4940*eda14cbcSMatt Macy 			continue;
4941*eda14cbcSMatt Macy 		}
4942*eda14cbcSMatt Macy 		/*
4943*eda14cbcSMatt Macy 		 * "origin" is readonly but is used to receive datasets as
4944*eda14cbcSMatt Macy 		 * clones so we don't raise an error here
4945*eda14cbcSMatt Macy 		 */
4946*eda14cbcSMatt Macy 		if (prop == ZFS_PROP_ORIGIN)
4947*eda14cbcSMatt Macy 			continue;
4948*eda14cbcSMatt Macy 
4949*eda14cbcSMatt Macy 		/* encryption params have their own verification later */
4950*eda14cbcSMatt Macy 		if (prop == ZFS_PROP_ENCRYPTION ||
4951*eda14cbcSMatt Macy 		    zfs_prop_encryption_key_param(prop))
4952*eda14cbcSMatt Macy 			continue;
4953*eda14cbcSMatt Macy 
4954*eda14cbcSMatt Macy 		/*
4955*eda14cbcSMatt Macy 		 * cannot override readonly, set-once and other specific
4956*eda14cbcSMatt Macy 		 * settable properties
4957*eda14cbcSMatt Macy 		 */
4958*eda14cbcSMatt Macy 		if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
4959*eda14cbcSMatt Macy 		    prop == ZFS_PROP_VOLSIZE) {
4960*eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4961*eda14cbcSMatt Macy 			    "invalid property '%s'"), name);
4962*eda14cbcSMatt Macy 			return (B_FALSE);
4963*eda14cbcSMatt Macy 		}
4964*eda14cbcSMatt Macy 	}
4965*eda14cbcSMatt Macy 
4966*eda14cbcSMatt Macy 	return (B_TRUE);
4967*eda14cbcSMatt Macy }
4968*eda14cbcSMatt Macy 
4969*eda14cbcSMatt Macy static int
4970*eda14cbcSMatt Macy zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
4971*eda14cbcSMatt Macy     const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
4972*eda14cbcSMatt Macy     nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs,
4973*eda14cbcSMatt Macy     const char *finalsnap, nvlist_t *cmdprops)
4974*eda14cbcSMatt Macy {
4975*eda14cbcSMatt Macy 	int err;
4976*eda14cbcSMatt Macy 	dmu_replay_record_t drr, drr_noswap;
4977*eda14cbcSMatt Macy 	struct drr_begin *drrb = &drr.drr_u.drr_begin;
4978*eda14cbcSMatt Macy 	char errbuf[1024];
4979*eda14cbcSMatt Macy 	zio_cksum_t zcksum = { { 0 } };
4980*eda14cbcSMatt Macy 	uint64_t featureflags;
4981*eda14cbcSMatt Macy 	int hdrtype;
4982*eda14cbcSMatt Macy 
4983*eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4984*eda14cbcSMatt Macy 	    "cannot receive"));
4985*eda14cbcSMatt Macy 
4986*eda14cbcSMatt Macy 	/* check cmdline props, raise an error if they cannot be received */
4987*eda14cbcSMatt Macy 	if (!zfs_receive_checkprops(hdl, cmdprops, errbuf)) {
4988*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
4989*eda14cbcSMatt Macy 	}
4990*eda14cbcSMatt Macy 
4991*eda14cbcSMatt Macy 	if (flags->isprefix &&
4992*eda14cbcSMatt Macy 	    !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
4993*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
4994*eda14cbcSMatt Macy 		    "(%s) does not exist"), tosnap);
4995*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
4996*eda14cbcSMatt Macy 	}
4997*eda14cbcSMatt Macy 	if (originsnap &&
4998*eda14cbcSMatt Macy 	    !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
4999*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
5000*eda14cbcSMatt Macy 		    "(%s) does not exist"), originsnap);
5001*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
5002*eda14cbcSMatt Macy 	}
5003*eda14cbcSMatt Macy 
5004*eda14cbcSMatt Macy 	/* read in the BEGIN record */
5005*eda14cbcSMatt Macy 	if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
5006*eda14cbcSMatt Macy 	    &zcksum)))
5007*eda14cbcSMatt Macy 		return (err);
5008*eda14cbcSMatt Macy 
5009*eda14cbcSMatt Macy 	if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
5010*eda14cbcSMatt Macy 		/* It's the double end record at the end of a package */
5011*eda14cbcSMatt Macy 		return (ENODATA);
5012*eda14cbcSMatt Macy 	}
5013*eda14cbcSMatt Macy 
5014*eda14cbcSMatt Macy 	/* the kernel needs the non-byteswapped begin record */
5015*eda14cbcSMatt Macy 	drr_noswap = drr;
5016*eda14cbcSMatt Macy 
5017*eda14cbcSMatt Macy 	flags->byteswap = B_FALSE;
5018*eda14cbcSMatt Macy 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
5019*eda14cbcSMatt Macy 		/*
5020*eda14cbcSMatt Macy 		 * We computed the checksum in the wrong byteorder in
5021*eda14cbcSMatt Macy 		 * recv_read() above; do it again correctly.
5022*eda14cbcSMatt Macy 		 */
5023*eda14cbcSMatt Macy 		bzero(&zcksum, sizeof (zio_cksum_t));
5024*eda14cbcSMatt Macy 		fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
5025*eda14cbcSMatt Macy 		flags->byteswap = B_TRUE;
5026*eda14cbcSMatt Macy 
5027*eda14cbcSMatt Macy 		drr.drr_type = BSWAP_32(drr.drr_type);
5028*eda14cbcSMatt Macy 		drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
5029*eda14cbcSMatt Macy 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
5030*eda14cbcSMatt Macy 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
5031*eda14cbcSMatt Macy 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
5032*eda14cbcSMatt Macy 		drrb->drr_type = BSWAP_32(drrb->drr_type);
5033*eda14cbcSMatt Macy 		drrb->drr_flags = BSWAP_32(drrb->drr_flags);
5034*eda14cbcSMatt Macy 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
5035*eda14cbcSMatt Macy 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
5036*eda14cbcSMatt Macy 	}
5037*eda14cbcSMatt Macy 
5038*eda14cbcSMatt Macy 	if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
5039*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5040*eda14cbcSMatt Macy 		    "stream (bad magic number)"));
5041*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5042*eda14cbcSMatt Macy 	}
5043*eda14cbcSMatt Macy 
5044*eda14cbcSMatt Macy 	featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
5045*eda14cbcSMatt Macy 	hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
5046*eda14cbcSMatt Macy 
5047*eda14cbcSMatt Macy 	if (!DMU_STREAM_SUPPORTED(featureflags) ||
5048*eda14cbcSMatt Macy 	    (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
5049*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5050*eda14cbcSMatt Macy 		    "stream has unsupported feature, feature flags = %lx"),
5051*eda14cbcSMatt Macy 		    featureflags);
5052*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5053*eda14cbcSMatt Macy 	}
5054*eda14cbcSMatt Macy 
5055*eda14cbcSMatt Macy 	/* Holds feature is set once in the compound stream header. */
5056*eda14cbcSMatt Macy 	if (featureflags & DMU_BACKUP_FEATURE_HOLDS)
5057*eda14cbcSMatt Macy 		flags->holds = B_TRUE;
5058*eda14cbcSMatt Macy 
5059*eda14cbcSMatt Macy 	if (strchr(drrb->drr_toname, '@') == NULL) {
5060*eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5061*eda14cbcSMatt Macy 		    "stream (bad snapshot name)"));
5062*eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5063*eda14cbcSMatt Macy 	}
5064*eda14cbcSMatt Macy 
5065*eda14cbcSMatt Macy 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
5066*eda14cbcSMatt Macy 		char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
5067*eda14cbcSMatt Macy 		if (sendfs == NULL) {
5068*eda14cbcSMatt Macy 			/*
5069*eda14cbcSMatt Macy 			 * We were not called from zfs_receive_package(). Get
5070*eda14cbcSMatt Macy 			 * the fs specified by 'zfs send'.
5071*eda14cbcSMatt Macy 			 */
5072*eda14cbcSMatt Macy 			char *cp;
5073*eda14cbcSMatt Macy 			(void) strlcpy(nonpackage_sendfs,
5074*eda14cbcSMatt Macy 			    drr.drr_u.drr_begin.drr_toname,
5075*eda14cbcSMatt Macy 			    sizeof (nonpackage_sendfs));
5076*eda14cbcSMatt Macy 			if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
5077*eda14cbcSMatt Macy 				*cp = '\0';
5078*eda14cbcSMatt Macy 			sendfs = nonpackage_sendfs;
5079*eda14cbcSMatt Macy 			VERIFY(finalsnap == NULL);
5080*eda14cbcSMatt Macy 		}
5081*eda14cbcSMatt Macy 		return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
5082*eda14cbcSMatt Macy 		    &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
5083*eda14cbcSMatt Macy 		    finalsnap, cmdprops));
5084*eda14cbcSMatt Macy 	} else {
5085*eda14cbcSMatt Macy 		assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
5086*eda14cbcSMatt Macy 		    DMU_COMPOUNDSTREAM);
5087*eda14cbcSMatt Macy 		return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
5088*eda14cbcSMatt Macy 		    &zcksum, top_zfs, cmdprops));
5089*eda14cbcSMatt Macy 	}
5090*eda14cbcSMatt Macy }
5091*eda14cbcSMatt Macy 
5092*eda14cbcSMatt Macy /*
5093*eda14cbcSMatt Macy  * Restores a backup of tosnap from the file descriptor specified by infd.
5094*eda14cbcSMatt Macy  * Return 0 on total success, -2 if some things couldn't be
5095*eda14cbcSMatt Macy  * destroyed/renamed/promoted, -1 if some things couldn't be received.
5096*eda14cbcSMatt Macy  * (-1 will override -2, if -1 and the resumable flag was specified the
5097*eda14cbcSMatt Macy  * transfer can be resumed if the sending side supports it).
5098*eda14cbcSMatt Macy  */
5099*eda14cbcSMatt Macy int
5100*eda14cbcSMatt Macy zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
5101*eda14cbcSMatt Macy     recvflags_t *flags, int infd, avl_tree_t *stream_avl)
5102*eda14cbcSMatt Macy {
5103*eda14cbcSMatt Macy 	char *top_zfs = NULL;
5104*eda14cbcSMatt Macy 	int err;
5105*eda14cbcSMatt Macy 	struct stat sb;
5106*eda14cbcSMatt Macy 	char *originsnap = NULL;
5107*eda14cbcSMatt Macy 
5108*eda14cbcSMatt Macy 	/*
5109*eda14cbcSMatt Macy 	 * The only way fstat can fail is if we do not have a valid file
5110*eda14cbcSMatt Macy 	 * descriptor.
5111*eda14cbcSMatt Macy 	 */
5112*eda14cbcSMatt Macy 	if (fstat(infd, &sb) == -1) {
5113*eda14cbcSMatt Macy 		perror("fstat");
5114*eda14cbcSMatt Macy 		return (-2);
5115*eda14cbcSMatt Macy 	}
5116*eda14cbcSMatt Macy 
5117*eda14cbcSMatt Macy 	/*
5118*eda14cbcSMatt Macy 	 * It is not uncommon for gigabytes to be processed in zfs receive.
5119*eda14cbcSMatt Macy 	 * Speculatively increase the buffer size if supported by the platform.
5120*eda14cbcSMatt Macy 	 */
5121*eda14cbcSMatt Macy 	if (S_ISFIFO(sb.st_mode))
5122*eda14cbcSMatt Macy 		libzfs_set_pipe_max(infd);
5123*eda14cbcSMatt Macy 
5124*eda14cbcSMatt Macy 	if (props) {
5125*eda14cbcSMatt Macy 		err = nvlist_lookup_string(props, "origin", &originsnap);
5126*eda14cbcSMatt Macy 		if (err && err != ENOENT)
5127*eda14cbcSMatt Macy 			return (err);
5128*eda14cbcSMatt Macy 	}
5129*eda14cbcSMatt Macy 
5130*eda14cbcSMatt Macy 	err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
5131*eda14cbcSMatt Macy 	    stream_avl, &top_zfs, NULL, props);
5132*eda14cbcSMatt Macy 
5133*eda14cbcSMatt Macy 	if (err == 0 && !flags->nomount && flags->domount && top_zfs) {
5134*eda14cbcSMatt Macy 		zfs_handle_t *zhp = NULL;
5135*eda14cbcSMatt Macy 		prop_changelist_t *clp = NULL;
5136*eda14cbcSMatt Macy 
5137*eda14cbcSMatt Macy 		zhp = zfs_open(hdl, top_zfs,
5138*eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5139*eda14cbcSMatt Macy 		if (zhp == NULL) {
5140*eda14cbcSMatt Macy 			err = -1;
5141*eda14cbcSMatt Macy 			goto out;
5142*eda14cbcSMatt Macy 		} else {
5143*eda14cbcSMatt Macy 			if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
5144*eda14cbcSMatt Macy 				zfs_close(zhp);
5145*eda14cbcSMatt Macy 				goto out;
5146*eda14cbcSMatt Macy 			}
5147*eda14cbcSMatt Macy 
5148*eda14cbcSMatt Macy 			clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
5149*eda14cbcSMatt Macy 			    CL_GATHER_MOUNT_ALWAYS,
5150*eda14cbcSMatt Macy 			    flags->forceunmount ? MS_FORCE : 0);
5151*eda14cbcSMatt Macy 			zfs_close(zhp);
5152*eda14cbcSMatt Macy 			if (clp == NULL) {
5153*eda14cbcSMatt Macy 				err = -1;
5154*eda14cbcSMatt Macy 				goto out;
5155*eda14cbcSMatt Macy 			}
5156*eda14cbcSMatt Macy 
5157*eda14cbcSMatt Macy 			/* mount and share received datasets */
5158*eda14cbcSMatt Macy 			err = changelist_postfix(clp);
5159*eda14cbcSMatt Macy 			changelist_free(clp);
5160*eda14cbcSMatt Macy 			if (err != 0)
5161*eda14cbcSMatt Macy 				err = -1;
5162*eda14cbcSMatt Macy 		}
5163*eda14cbcSMatt Macy 	}
5164*eda14cbcSMatt Macy 
5165*eda14cbcSMatt Macy out:
5166*eda14cbcSMatt Macy 	if (top_zfs)
5167*eda14cbcSMatt Macy 		free(top_zfs);
5168*eda14cbcSMatt Macy 
5169*eda14cbcSMatt Macy 	return (err);
5170*eda14cbcSMatt Macy }
5171