xref: /freebsd-src/sys/contrib/openzfs/lib/libzfs/libzfs_sendrecv.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  * Copyright (c) 2011, 2020 by Delphix. All rights reserved.
25eda14cbcSMatt Macy  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26eda14cbcSMatt Macy  * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>.
27eda14cbcSMatt Macy  * All rights reserved
28eda14cbcSMatt Macy  * Copyright (c) 2013 Steven Hartland. All rights reserved.
29eda14cbcSMatt Macy  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
30eda14cbcSMatt Macy  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
31eda14cbcSMatt Macy  * Copyright (c) 2018, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
32eda14cbcSMatt Macy  * Copyright (c) 2019 Datto Inc.
33*7a7741afSMartin Matuska  * Copyright (c) 2024, Klara, Inc.
34eda14cbcSMatt Macy  */
35eda14cbcSMatt Macy 
36eda14cbcSMatt Macy #include <assert.h>
37eda14cbcSMatt Macy #include <ctype.h>
38eda14cbcSMatt Macy #include <errno.h>
39eda14cbcSMatt Macy #include <libintl.h>
40eda14cbcSMatt Macy #include <stdio.h>
41eda14cbcSMatt Macy #include <stdlib.h>
42da5137abSMartin Matuska #include <string.h>
43eda14cbcSMatt Macy #include <unistd.h>
44eda14cbcSMatt Macy #include <stddef.h>
45eda14cbcSMatt Macy #include <fcntl.h>
46eda14cbcSMatt Macy #include <sys/mount.h>
47eda14cbcSMatt Macy #include <sys/mntent.h>
48eda14cbcSMatt Macy #include <sys/mnttab.h>
49eda14cbcSMatt Macy #include <sys/avl.h>
50eda14cbcSMatt Macy #include <sys/debug.h>
51eda14cbcSMatt Macy #include <sys/stat.h>
52eda14cbcSMatt Macy #include <pthread.h>
53eda14cbcSMatt Macy #include <umem.h>
54eda14cbcSMatt Macy #include <time.h>
55eda14cbcSMatt Macy 
56eda14cbcSMatt Macy #include <libzfs.h>
57eda14cbcSMatt Macy #include <libzfs_core.h>
58eda14cbcSMatt Macy #include <libzutil.h>
59eda14cbcSMatt Macy 
60eda14cbcSMatt Macy #include "zfs_namecheck.h"
61eda14cbcSMatt Macy #include "zfs_prop.h"
62eda14cbcSMatt Macy #include "zfs_fletcher.h"
63eda14cbcSMatt Macy #include "libzfs_impl.h"
64eda14cbcSMatt Macy #include <cityhash.h>
65eda14cbcSMatt Macy #include <zlib.h>
66eda14cbcSMatt Macy #include <sys/zio_checksum.h>
67eda14cbcSMatt Macy #include <sys/dsl_crypt.h>
68eda14cbcSMatt Macy #include <sys/ddt.h>
69eda14cbcSMatt Macy #include <sys/socket.h>
70eda14cbcSMatt Macy #include <sys/sha2.h>
71eda14cbcSMatt Macy 
72eda14cbcSMatt Macy static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
73eda14cbcSMatt Macy     recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **,
74eda14cbcSMatt Macy     const char *, nvlist_t *);
75eda14cbcSMatt Macy static int guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
76eda14cbcSMatt Macy     uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
77eda14cbcSMatt Macy     uint64_t num_redact_snaps, char *name);
78eda14cbcSMatt Macy static int guid_to_name(libzfs_handle_t *, const char *,
79eda14cbcSMatt Macy     uint64_t, boolean_t, char *);
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy typedef struct progress_arg {
82eda14cbcSMatt Macy 	zfs_handle_t *pa_zhp;
83eda14cbcSMatt Macy 	int pa_fd;
84eda14cbcSMatt Macy 	boolean_t pa_parsable;
85eda14cbcSMatt Macy 	boolean_t pa_estimate;
86eda14cbcSMatt Macy 	int pa_verbosity;
8715f0b8c3SMartin Matuska 	boolean_t pa_astitle;
88c9539b89SMartin Matuska 	boolean_t pa_progress;
8915f0b8c3SMartin Matuska 	uint64_t pa_size;
90eda14cbcSMatt Macy } progress_arg_t;
91eda14cbcSMatt Macy 
92eda14cbcSMatt Macy static int
93c03c5b1cSMartin Matuska dump_record(dmu_replay_record_t *drr, void *payload, size_t payload_len,
94eda14cbcSMatt Macy     zio_cksum_t *zc, int outfd)
95eda14cbcSMatt Macy {
96eda14cbcSMatt Macy 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
97eda14cbcSMatt Macy 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
98eda14cbcSMatt Macy 	fletcher_4_incremental_native(drr,
99eda14cbcSMatt Macy 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
100eda14cbcSMatt Macy 	if (drr->drr_type != DRR_BEGIN) {
101eda14cbcSMatt Macy 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
102eda14cbcSMatt Macy 		    drr_checksum.drr_checksum));
103eda14cbcSMatt Macy 		drr->drr_u.drr_checksum.drr_checksum = *zc;
104eda14cbcSMatt Macy 	}
105eda14cbcSMatt Macy 	fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
106eda14cbcSMatt Macy 	    sizeof (zio_cksum_t), zc);
107eda14cbcSMatt Macy 	if (write(outfd, drr, sizeof (*drr)) == -1)
108eda14cbcSMatt Macy 		return (errno);
109eda14cbcSMatt Macy 	if (payload_len != 0) {
110eda14cbcSMatt Macy 		fletcher_4_incremental_native(payload, payload_len, zc);
111eda14cbcSMatt Macy 		if (write(outfd, payload, payload_len) == -1)
112eda14cbcSMatt Macy 			return (errno);
113eda14cbcSMatt Macy 	}
114eda14cbcSMatt Macy 	return (0);
115eda14cbcSMatt Macy }
116eda14cbcSMatt Macy 
117eda14cbcSMatt Macy /*
118eda14cbcSMatt Macy  * Routines for dealing with the AVL tree of fs-nvlists
119eda14cbcSMatt Macy  */
120eda14cbcSMatt Macy typedef struct fsavl_node {
121eda14cbcSMatt Macy 	avl_node_t fn_node;
122eda14cbcSMatt Macy 	nvlist_t *fn_nvfs;
1232a58b312SMartin Matuska 	const char *fn_snapname;
124eda14cbcSMatt Macy 	uint64_t fn_guid;
125eda14cbcSMatt Macy } fsavl_node_t;
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy static int
128eda14cbcSMatt Macy fsavl_compare(const void *arg1, const void *arg2)
129eda14cbcSMatt Macy {
130eda14cbcSMatt Macy 	const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
131eda14cbcSMatt Macy 	const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
132eda14cbcSMatt Macy 
133eda14cbcSMatt Macy 	return (TREE_CMP(fn1->fn_guid, fn2->fn_guid));
134eda14cbcSMatt Macy }
135eda14cbcSMatt Macy 
136eda14cbcSMatt Macy /*
137eda14cbcSMatt Macy  * Given the GUID of a snapshot, find its containing filesystem and
138eda14cbcSMatt Macy  * (optionally) name.
139eda14cbcSMatt Macy  */
140eda14cbcSMatt Macy static nvlist_t *
1412a58b312SMartin Matuska fsavl_find(avl_tree_t *avl, uint64_t snapguid, const char **snapname)
142eda14cbcSMatt Macy {
143eda14cbcSMatt Macy 	fsavl_node_t fn_find;
144eda14cbcSMatt Macy 	fsavl_node_t *fn;
145eda14cbcSMatt Macy 
146eda14cbcSMatt Macy 	fn_find.fn_guid = snapguid;
147eda14cbcSMatt Macy 
148eda14cbcSMatt Macy 	fn = avl_find(avl, &fn_find, NULL);
149eda14cbcSMatt Macy 	if (fn) {
150eda14cbcSMatt Macy 		if (snapname)
151eda14cbcSMatt Macy 			*snapname = fn->fn_snapname;
152eda14cbcSMatt Macy 		return (fn->fn_nvfs);
153eda14cbcSMatt Macy 	}
154eda14cbcSMatt Macy 	return (NULL);
155eda14cbcSMatt Macy }
156eda14cbcSMatt Macy 
157eda14cbcSMatt Macy static void
158eda14cbcSMatt Macy fsavl_destroy(avl_tree_t *avl)
159eda14cbcSMatt Macy {
160eda14cbcSMatt Macy 	fsavl_node_t *fn;
161eda14cbcSMatt Macy 	void *cookie;
162eda14cbcSMatt Macy 
163eda14cbcSMatt Macy 	if (avl == NULL)
164eda14cbcSMatt Macy 		return;
165eda14cbcSMatt Macy 
166eda14cbcSMatt Macy 	cookie = NULL;
167eda14cbcSMatt Macy 	while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
168eda14cbcSMatt Macy 		free(fn);
169eda14cbcSMatt Macy 	avl_destroy(avl);
170eda14cbcSMatt Macy 	free(avl);
171eda14cbcSMatt Macy }
172eda14cbcSMatt Macy 
173eda14cbcSMatt Macy /*
174eda14cbcSMatt Macy  * Given an nvlist, produce an avl tree of snapshots, ordered by guid
175eda14cbcSMatt Macy  */
176eda14cbcSMatt Macy static avl_tree_t *
177eda14cbcSMatt Macy fsavl_create(nvlist_t *fss)
178eda14cbcSMatt Macy {
179eda14cbcSMatt Macy 	avl_tree_t *fsavl;
180eda14cbcSMatt Macy 	nvpair_t *fselem = NULL;
181eda14cbcSMatt Macy 
182eda14cbcSMatt Macy 	if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
183eda14cbcSMatt Macy 		return (NULL);
184eda14cbcSMatt Macy 
185eda14cbcSMatt Macy 	avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
186eda14cbcSMatt Macy 	    offsetof(fsavl_node_t, fn_node));
187eda14cbcSMatt Macy 
188eda14cbcSMatt Macy 	while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
189eda14cbcSMatt Macy 		nvlist_t *nvfs, *snaps;
190eda14cbcSMatt Macy 		nvpair_t *snapelem = NULL;
191eda14cbcSMatt Macy 
192184c1b94SMartin Matuska 		nvfs = fnvpair_value_nvlist(fselem);
193184c1b94SMartin Matuska 		snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
194eda14cbcSMatt Macy 
195eda14cbcSMatt Macy 		while ((snapelem =
196eda14cbcSMatt Macy 		    nvlist_next_nvpair(snaps, snapelem)) != NULL) {
197eda14cbcSMatt Macy 			fsavl_node_t *fn;
198eda14cbcSMatt Macy 
199eda14cbcSMatt Macy 			if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
200eda14cbcSMatt Macy 				fsavl_destroy(fsavl);
201eda14cbcSMatt Macy 				return (NULL);
202eda14cbcSMatt Macy 			}
203eda14cbcSMatt Macy 			fn->fn_nvfs = nvfs;
204eda14cbcSMatt Macy 			fn->fn_snapname = nvpair_name(snapelem);
205c03c5b1cSMartin Matuska 			fn->fn_guid = fnvpair_value_uint64(snapelem);
206eda14cbcSMatt Macy 
207eda14cbcSMatt Macy 			/*
208eda14cbcSMatt Macy 			 * Note: if there are multiple snaps with the
209eda14cbcSMatt Macy 			 * same GUID, we ignore all but one.
210eda14cbcSMatt Macy 			 */
211c03c5b1cSMartin Matuska 			avl_index_t where = 0;
212c03c5b1cSMartin Matuska 			if (avl_find(fsavl, fn, &where) == NULL)
213c03c5b1cSMartin Matuska 				avl_insert(fsavl, fn, where);
214eda14cbcSMatt Macy 			else
215eda14cbcSMatt Macy 				free(fn);
216eda14cbcSMatt Macy 		}
217eda14cbcSMatt Macy 	}
218eda14cbcSMatt Macy 
219eda14cbcSMatt Macy 	return (fsavl);
220eda14cbcSMatt Macy }
221eda14cbcSMatt Macy 
222eda14cbcSMatt Macy /*
223eda14cbcSMatt Macy  * Routines for dealing with the giant nvlist of fs-nvlists, etc.
224eda14cbcSMatt Macy  */
225eda14cbcSMatt Macy typedef struct send_data {
226eda14cbcSMatt Macy 	/*
227eda14cbcSMatt Macy 	 * assigned inside every recursive call,
228eda14cbcSMatt Macy 	 * restored from *_save on return:
229eda14cbcSMatt Macy 	 *
230eda14cbcSMatt Macy 	 * guid of fromsnap snapshot in parent dataset
231eda14cbcSMatt Macy 	 * txg of fromsnap snapshot in current dataset
232eda14cbcSMatt Macy 	 * txg of tosnap snapshot in current dataset
233eda14cbcSMatt Macy 	 */
234eda14cbcSMatt Macy 
235eda14cbcSMatt Macy 	uint64_t parent_fromsnap_guid;
236eda14cbcSMatt Macy 	uint64_t fromsnap_txg;
237eda14cbcSMatt Macy 	uint64_t tosnap_txg;
238eda14cbcSMatt Macy 
239eda14cbcSMatt Macy 	/* the nvlists get accumulated during depth-first traversal */
240eda14cbcSMatt Macy 	nvlist_t *parent_snaps;
241eda14cbcSMatt Macy 	nvlist_t *fss;
242eda14cbcSMatt Macy 	nvlist_t *snapprops;
243eda14cbcSMatt Macy 	nvlist_t *snapholds;	/* user holds */
244eda14cbcSMatt Macy 
245eda14cbcSMatt Macy 	/* send-receive configuration, does not change during traversal */
246eda14cbcSMatt Macy 	const char *fsname;
247eda14cbcSMatt Macy 	const char *fromsnap;
248eda14cbcSMatt Macy 	const char *tosnap;
249eda14cbcSMatt Macy 	boolean_t recursive;
250eda14cbcSMatt Macy 	boolean_t raw;
251eda14cbcSMatt Macy 	boolean_t doall;
252eda14cbcSMatt Macy 	boolean_t replicate;
25316038816SMartin Matuska 	boolean_t skipmissing;
254eda14cbcSMatt Macy 	boolean_t verbose;
255eda14cbcSMatt Macy 	boolean_t backup;
256eda14cbcSMatt Macy 	boolean_t seenfrom;
257eda14cbcSMatt Macy 	boolean_t seento;
258eda14cbcSMatt Macy 	boolean_t holds;	/* were holds requested with send -h */
259eda14cbcSMatt Macy 	boolean_t props;
260eda14cbcSMatt Macy 
261eda14cbcSMatt Macy 	/*
262eda14cbcSMatt Macy 	 * The header nvlist is of the following format:
263eda14cbcSMatt Macy 	 * {
264eda14cbcSMatt Macy 	 *   "tosnap" -> string
265eda14cbcSMatt Macy 	 *   "fromsnap" -> string (if incremental)
266eda14cbcSMatt Macy 	 *   "fss" -> {
267eda14cbcSMatt Macy 	 *	id -> {
268eda14cbcSMatt Macy 	 *
269eda14cbcSMatt Macy 	 *	 "name" -> string (full name; for debugging)
270eda14cbcSMatt Macy 	 *	 "parentfromsnap" -> number (guid of fromsnap in parent)
271eda14cbcSMatt Macy 	 *
272eda14cbcSMatt Macy 	 *	 "props" -> { name -> value (only if set here) }
273eda14cbcSMatt Macy 	 *	 "snaps" -> { name (lastname) -> number (guid) }
274eda14cbcSMatt Macy 	 *	 "snapprops" -> { name (lastname) -> { name -> value } }
275eda14cbcSMatt Macy 	 *	 "snapholds" -> { name (lastname) -> { holdname -> crtime } }
276eda14cbcSMatt Macy 	 *
277eda14cbcSMatt Macy 	 *	 "origin" -> number (guid) (if clone)
278eda14cbcSMatt Macy 	 *	 "is_encroot" -> boolean
279eda14cbcSMatt Macy 	 *	 "sent" -> boolean (not on-disk)
280eda14cbcSMatt Macy 	 *	}
281eda14cbcSMatt Macy 	 *   }
282eda14cbcSMatt Macy 	 * }
283eda14cbcSMatt Macy 	 *
284eda14cbcSMatt Macy 	 */
285eda14cbcSMatt Macy } send_data_t;
286eda14cbcSMatt Macy 
287eda14cbcSMatt Macy static void
288eda14cbcSMatt Macy send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
289eda14cbcSMatt Macy 
290c03c5b1cSMartin Matuska /*
291c03c5b1cSMartin Matuska  * Collect guid, valid props, optionally holds, etc. of a snapshot.
292d411c1d6SMartin Matuska  * This interface is intended for use as a zfs_iter_snapshots_v2_sorted visitor.
293c03c5b1cSMartin Matuska  */
294eda14cbcSMatt Macy static int
295eda14cbcSMatt Macy send_iterate_snap(zfs_handle_t *zhp, void *arg)
296eda14cbcSMatt Macy {
297eda14cbcSMatt Macy 	send_data_t *sd = arg;
298eda14cbcSMatt Macy 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
299eda14cbcSMatt Macy 	uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
300eda14cbcSMatt Macy 	boolean_t isfromsnap, istosnap, istosnapwithnofrom;
301c03c5b1cSMartin Matuska 	char *snapname;
302c03c5b1cSMartin Matuska 	const char *from = sd->fromsnap;
303c03c5b1cSMartin Matuska 	const char *to = sd->tosnap;
304eda14cbcSMatt Macy 
305c03c5b1cSMartin Matuska 	snapname = strrchr(zhp->zfs_name, '@');
306c03c5b1cSMartin Matuska 	assert(snapname != NULL);
307c03c5b1cSMartin Matuska 	++snapname;
308c03c5b1cSMartin Matuska 
309c03c5b1cSMartin Matuska 	isfromsnap = (from != NULL && strcmp(from, snapname) == 0);
310c03c5b1cSMartin Matuska 	istosnap = (to != NULL && strcmp(to, snapname) == 0);
311c03c5b1cSMartin Matuska 	istosnapwithnofrom = (istosnap && from == NULL);
312eda14cbcSMatt Macy 
313eda14cbcSMatt Macy 	if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
314eda14cbcSMatt Macy 		if (sd->verbose) {
315eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
316eda14cbcSMatt Macy 			    "skipping snapshot %s because it was created "
317eda14cbcSMatt Macy 			    "after the destination snapshot (%s)\n"),
318c03c5b1cSMartin Matuska 			    zhp->zfs_name, to);
319eda14cbcSMatt Macy 		}
320eda14cbcSMatt Macy 		zfs_close(zhp);
321eda14cbcSMatt Macy 		return (0);
322eda14cbcSMatt Macy 	}
323eda14cbcSMatt Macy 
324184c1b94SMartin Matuska 	fnvlist_add_uint64(sd->parent_snaps, snapname, guid);
325c03c5b1cSMartin Matuska 
326eda14cbcSMatt Macy 	/*
327eda14cbcSMatt Macy 	 * NB: if there is no fromsnap here (it's a newly created fs in
328eda14cbcSMatt Macy 	 * an incremental replication), we will substitute the tosnap.
329eda14cbcSMatt Macy 	 */
330c03c5b1cSMartin Matuska 	if (isfromsnap || (sd->parent_fromsnap_guid == 0 && istosnap))
331eda14cbcSMatt Macy 		sd->parent_fromsnap_guid = guid;
332eda14cbcSMatt Macy 
333eda14cbcSMatt Macy 	if (!sd->recursive) {
334caed7b1cSMartin Matuska 		/*
335caed7b1cSMartin Matuska 		 * To allow a doall stream to work properly
336caed7b1cSMartin Matuska 		 * with a NULL fromsnap
337caed7b1cSMartin Matuska 		 */
338c03c5b1cSMartin Matuska 		if (sd->doall && from == NULL && !sd->seenfrom)
339caed7b1cSMartin Matuska 			sd->seenfrom = B_TRUE;
340caed7b1cSMartin Matuska 
341eda14cbcSMatt Macy 		if (!sd->seenfrom && isfromsnap) {
342eda14cbcSMatt Macy 			sd->seenfrom = B_TRUE;
343eda14cbcSMatt Macy 			zfs_close(zhp);
344eda14cbcSMatt Macy 			return (0);
345eda14cbcSMatt Macy 		}
346eda14cbcSMatt Macy 
347eda14cbcSMatt Macy 		if ((sd->seento || !sd->seenfrom) && !istosnapwithnofrom) {
348eda14cbcSMatt Macy 			zfs_close(zhp);
349eda14cbcSMatt Macy 			return (0);
350eda14cbcSMatt Macy 		}
351eda14cbcSMatt Macy 
352eda14cbcSMatt Macy 		if (istosnap)
353eda14cbcSMatt Macy 			sd->seento = B_TRUE;
354eda14cbcSMatt Macy 	}
355eda14cbcSMatt Macy 
356c03c5b1cSMartin Matuska 	nvlist_t *nv = fnvlist_alloc();
357eda14cbcSMatt Macy 	send_iterate_prop(zhp, sd->backup, nv);
358184c1b94SMartin Matuska 	fnvlist_add_nvlist(sd->snapprops, snapname, nv);
359184c1b94SMartin Matuska 	fnvlist_free(nv);
360c03c5b1cSMartin Matuska 
361eda14cbcSMatt Macy 	if (sd->holds) {
362c03c5b1cSMartin Matuska 		nvlist_t *holds;
363c03c5b1cSMartin Matuska 		if (lzc_get_holds(zhp->zfs_name, &holds) == 0) {
364184c1b94SMartin Matuska 			fnvlist_add_nvlist(sd->snapholds, snapname, holds);
365eda14cbcSMatt Macy 			fnvlist_free(holds);
366eda14cbcSMatt Macy 		}
367c03c5b1cSMartin Matuska 	}
368eda14cbcSMatt Macy 
369eda14cbcSMatt Macy 	zfs_close(zhp);
370eda14cbcSMatt Macy 	return (0);
371eda14cbcSMatt Macy }
372eda14cbcSMatt Macy 
373c03c5b1cSMartin Matuska /*
374c03c5b1cSMartin Matuska  * Collect all valid props from the handle snap into an nvlist.
375c03c5b1cSMartin Matuska  */
376eda14cbcSMatt Macy static void
377eda14cbcSMatt Macy send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
378eda14cbcSMatt Macy {
379c03c5b1cSMartin Matuska 	nvlist_t *props;
380eda14cbcSMatt Macy 
381eda14cbcSMatt Macy 	if (received_only)
382eda14cbcSMatt Macy 		props = zfs_get_recvd_props(zhp);
383eda14cbcSMatt Macy 	else
384eda14cbcSMatt Macy 		props = zhp->zfs_props;
385eda14cbcSMatt Macy 
386c03c5b1cSMartin Matuska 	nvpair_t *elem = NULL;
387eda14cbcSMatt Macy 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
3882a58b312SMartin Matuska 		const char *propname = nvpair_name(elem);
389eda14cbcSMatt Macy 		zfs_prop_t prop = zfs_name_to_prop(propname);
390eda14cbcSMatt Macy 
391eda14cbcSMatt Macy 		if (!zfs_prop_user(propname)) {
392eda14cbcSMatt Macy 			/*
393eda14cbcSMatt Macy 			 * Realistically, this should never happen.  However,
394eda14cbcSMatt Macy 			 * we want the ability to add DSL properties without
395eda14cbcSMatt Macy 			 * needing to make incompatible version changes.  We
396eda14cbcSMatt Macy 			 * need to ignore unknown properties to allow older
397eda14cbcSMatt Macy 			 * software to still send datasets containing these
398eda14cbcSMatt Macy 			 * properties, with the unknown properties elided.
399eda14cbcSMatt Macy 			 */
400eda14cbcSMatt Macy 			if (prop == ZPROP_INVAL)
401eda14cbcSMatt Macy 				continue;
402eda14cbcSMatt Macy 
403eda14cbcSMatt Macy 			if (zfs_prop_readonly(prop))
404eda14cbcSMatt Macy 				continue;
405eda14cbcSMatt Macy 		}
406eda14cbcSMatt Macy 
407c03c5b1cSMartin Matuska 		nvlist_t *propnv = fnvpair_value_nvlist(elem);
408c03c5b1cSMartin Matuska 
409c03c5b1cSMartin Matuska 		boolean_t isspacelimit = (prop == ZFS_PROP_QUOTA ||
410c03c5b1cSMartin Matuska 		    prop == ZFS_PROP_RESERVATION ||
411eda14cbcSMatt Macy 		    prop == ZFS_PROP_REFQUOTA ||
412c03c5b1cSMartin Matuska 		    prop == ZFS_PROP_REFRESERVATION);
413c03c5b1cSMartin Matuska 		if (isspacelimit && zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
414eda14cbcSMatt Macy 			continue;
415c03c5b1cSMartin Matuska 
4162a58b312SMartin Matuska 		const char *source;
417c03c5b1cSMartin Matuska 		if (nvlist_lookup_string(propnv, ZPROP_SOURCE, &source) == 0) {
418c03c5b1cSMartin Matuska 			if (strcmp(source, zhp->zfs_name) != 0 &&
419c03c5b1cSMartin Matuska 			    strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
420c03c5b1cSMartin Matuska 				continue;
421c03c5b1cSMartin Matuska 		} else {
422eda14cbcSMatt Macy 			/*
423eda14cbcSMatt Macy 			 * May have no source before SPA_VERSION_RECVD_PROPS,
424eda14cbcSMatt Macy 			 * but is still modifiable.
425eda14cbcSMatt Macy 			 */
426c03c5b1cSMartin Matuska 			if (!isspacelimit)
427eda14cbcSMatt Macy 				continue;
428eda14cbcSMatt Macy 		}
429eda14cbcSMatt Macy 
430eda14cbcSMatt Macy 		if (zfs_prop_user(propname) ||
431eda14cbcSMatt Macy 		    zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
4322a58b312SMartin Matuska 			const char *value;
433184c1b94SMartin Matuska 			value = fnvlist_lookup_string(propnv, ZPROP_VALUE);
434184c1b94SMartin Matuska 			fnvlist_add_string(nv, propname, value);
435eda14cbcSMatt Macy 		} else {
436eda14cbcSMatt Macy 			uint64_t value;
437184c1b94SMartin Matuska 			value = fnvlist_lookup_uint64(propnv, ZPROP_VALUE);
438184c1b94SMartin Matuska 			fnvlist_add_uint64(nv, propname, value);
439eda14cbcSMatt Macy 		}
440eda14cbcSMatt Macy 	}
441eda14cbcSMatt Macy }
442eda14cbcSMatt Macy 
443eda14cbcSMatt Macy /*
444271171e0SMartin Matuska  * returns snapshot guid
445271171e0SMartin Matuska  * and returns 0 if the snapshot does not exist
446271171e0SMartin Matuska  */
447271171e0SMartin Matuska static uint64_t
448271171e0SMartin Matuska get_snap_guid(libzfs_handle_t *hdl, const char *fs, const char *snap)
449271171e0SMartin Matuska {
450271171e0SMartin Matuska 	char name[MAXPATHLEN + 1];
451271171e0SMartin Matuska 	uint64_t guid = 0;
452271171e0SMartin Matuska 
453271171e0SMartin Matuska 	if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
454271171e0SMartin Matuska 		return (guid);
455271171e0SMartin Matuska 
456271171e0SMartin Matuska 	(void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
457271171e0SMartin Matuska 	zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
458271171e0SMartin Matuska 	if (zhp != NULL) {
459271171e0SMartin Matuska 		guid = zfs_prop_get_int(zhp, ZFS_PROP_GUID);
460271171e0SMartin Matuska 		zfs_close(zhp);
461271171e0SMartin Matuska 	}
462271171e0SMartin Matuska 
463271171e0SMartin Matuska 	return (guid);
464271171e0SMartin Matuska }
465271171e0SMartin Matuska 
466271171e0SMartin Matuska /*
467eda14cbcSMatt Macy  * returns snapshot creation txg
468eda14cbcSMatt Macy  * and returns 0 if the snapshot does not exist
469eda14cbcSMatt Macy  */
470eda14cbcSMatt Macy static uint64_t
471eda14cbcSMatt Macy get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
472eda14cbcSMatt Macy {
473eda14cbcSMatt Macy 	char name[ZFS_MAX_DATASET_NAME_LEN];
474eda14cbcSMatt Macy 	uint64_t txg = 0;
475eda14cbcSMatt Macy 
476eda14cbcSMatt Macy 	if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
477eda14cbcSMatt Macy 		return (txg);
478eda14cbcSMatt Macy 
479eda14cbcSMatt Macy 	(void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
480eda14cbcSMatt Macy 	if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
481eda14cbcSMatt Macy 		zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
482eda14cbcSMatt Macy 		if (zhp != NULL) {
483eda14cbcSMatt Macy 			txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
484eda14cbcSMatt Macy 			zfs_close(zhp);
485eda14cbcSMatt Macy 		}
486eda14cbcSMatt Macy 	}
487eda14cbcSMatt Macy 
488eda14cbcSMatt Macy 	return (txg);
489eda14cbcSMatt Macy }
490eda14cbcSMatt Macy 
491eda14cbcSMatt Macy /*
492c03c5b1cSMartin Matuska  * Recursively generate nvlists describing datasets.  See comment
493eda14cbcSMatt Macy  * for the data structure send_data_t above for description of contents
494eda14cbcSMatt Macy  * of the nvlist.
495eda14cbcSMatt Macy  */
496eda14cbcSMatt Macy static int
497eda14cbcSMatt Macy send_iterate_fs(zfs_handle_t *zhp, void *arg)
498eda14cbcSMatt Macy {
499eda14cbcSMatt Macy 	send_data_t *sd = arg;
500eda14cbcSMatt Macy 	nvlist_t *nvfs = NULL, *nv = NULL;
501eda14cbcSMatt Macy 	int rv = 0;
502eda14cbcSMatt Macy 	uint64_t min_txg = 0, max_txg = 0;
503eda14cbcSMatt Macy 	uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
504eda14cbcSMatt Macy 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
505eda14cbcSMatt Macy 	uint64_t fromsnap_txg, tosnap_txg;
506eda14cbcSMatt Macy 	char guidstring[64];
507eda14cbcSMatt Macy 
508c03c5b1cSMartin Matuska 	/* These fields are restored on return from a recursive call. */
509c03c5b1cSMartin Matuska 	uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
510c03c5b1cSMartin Matuska 	uint64_t fromsnap_txg_save = sd->fromsnap_txg;
511c03c5b1cSMartin Matuska 	uint64_t tosnap_txg_save = sd->tosnap_txg;
512c03c5b1cSMartin Matuska 
513eda14cbcSMatt Macy 	fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
514eda14cbcSMatt Macy 	if (fromsnap_txg != 0)
515eda14cbcSMatt Macy 		sd->fromsnap_txg = fromsnap_txg;
516eda14cbcSMatt Macy 
517eda14cbcSMatt Macy 	tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
518eda14cbcSMatt Macy 	if (tosnap_txg != 0)
519eda14cbcSMatt Macy 		sd->tosnap_txg = tosnap_txg;
520eda14cbcSMatt Macy 
521eda14cbcSMatt Macy 	/*
522c03c5b1cSMartin Matuska 	 * On the send side, if the current dataset does not have tosnap,
523eda14cbcSMatt Macy 	 * perform two additional checks:
524eda14cbcSMatt Macy 	 *
525c03c5b1cSMartin Matuska 	 * - Skip sending the current dataset if it was created later than
526c03c5b1cSMartin Matuska 	 *   the parent tosnap.
527c03c5b1cSMartin Matuska 	 * - Return error if the current dataset was created earlier than
52816038816SMartin Matuska 	 *   the parent tosnap, unless --skip-missing specified. Then
529c03c5b1cSMartin Matuska 	 *   just print a warning.
530eda14cbcSMatt Macy 	 */
531eda14cbcSMatt Macy 	if (sd->tosnap != NULL && tosnap_txg == 0) {
532eda14cbcSMatt Macy 		if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
533eda14cbcSMatt Macy 			if (sd->verbose) {
534eda14cbcSMatt Macy 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
535eda14cbcSMatt Macy 				    "skipping dataset %s: snapshot %s does "
536eda14cbcSMatt Macy 				    "not exist\n"), zhp->zfs_name, sd->tosnap);
537eda14cbcSMatt Macy 			}
53816038816SMartin Matuska 		} else if (sd->skipmissing) {
53916038816SMartin Matuska 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
54016038816SMartin Matuska 			    "WARNING: skipping dataset %s and its children:"
54116038816SMartin Matuska 			    " snapshot %s does not exist\n"),
54216038816SMartin Matuska 			    zhp->zfs_name, sd->tosnap);
543eda14cbcSMatt Macy 		} else {
544eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
545eda14cbcSMatt Macy 			    "cannot send %s@%s%s: snapshot %s@%s does not "
546eda14cbcSMatt Macy 			    "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
547eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, " recursively") : "",
548eda14cbcSMatt Macy 			    zhp->zfs_name, sd->tosnap);
549eda14cbcSMatt Macy 			rv = EZFS_NOENT;
550eda14cbcSMatt Macy 		}
551eda14cbcSMatt Macy 		goto out;
552eda14cbcSMatt Macy 	}
553eda14cbcSMatt Macy 
554eda14cbcSMatt Macy 	nvfs = fnvlist_alloc();
555eda14cbcSMatt Macy 	fnvlist_add_string(nvfs, "name", zhp->zfs_name);
556c03c5b1cSMartin Matuska 	fnvlist_add_uint64(nvfs, "parentfromsnap", sd->parent_fromsnap_guid);
557eda14cbcSMatt Macy 
558c03c5b1cSMartin Matuska 	if (zhp->zfs_dmustats.dds_origin[0] != '\0') {
559eda14cbcSMatt Macy 		zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
560eda14cbcSMatt Macy 		    zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
561eda14cbcSMatt Macy 		if (origin == NULL) {
562eda14cbcSMatt Macy 			rv = -1;
563eda14cbcSMatt Macy 			goto out;
564eda14cbcSMatt Macy 		}
565eda14cbcSMatt Macy 		fnvlist_add_uint64(nvfs, "origin",
566eda14cbcSMatt Macy 		    origin->zfs_dmustats.dds_guid);
567eda14cbcSMatt Macy 		zfs_close(origin);
568eda14cbcSMatt Macy 	}
569eda14cbcSMatt Macy 
570c03c5b1cSMartin Matuska 	/* Iterate over props. */
571eda14cbcSMatt Macy 	if (sd->props || sd->backup || sd->recursive) {
572eda14cbcSMatt Macy 		nv = fnvlist_alloc();
573eda14cbcSMatt Macy 		send_iterate_prop(zhp, sd->backup, nv);
574c03c5b1cSMartin Matuska 		fnvlist_add_nvlist(nvfs, "props", nv);
575eda14cbcSMatt Macy 	}
576eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
577eda14cbcSMatt Macy 		boolean_t encroot;
578eda14cbcSMatt Macy 
579c03c5b1cSMartin Matuska 		/* Determine if this dataset is an encryption root. */
580eda14cbcSMatt Macy 		if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
581eda14cbcSMatt Macy 			rv = -1;
582eda14cbcSMatt Macy 			goto out;
583eda14cbcSMatt Macy 		}
584eda14cbcSMatt Macy 
585eda14cbcSMatt Macy 		if (encroot)
586eda14cbcSMatt Macy 			fnvlist_add_boolean(nvfs, "is_encroot");
587eda14cbcSMatt Macy 
588eda14cbcSMatt Macy 		/*
589eda14cbcSMatt Macy 		 * Encrypted datasets can only be sent with properties if
590eda14cbcSMatt Macy 		 * the raw flag is specified because the receive side doesn't
591eda14cbcSMatt Macy 		 * currently have a mechanism for recursively asking the user
592eda14cbcSMatt Macy 		 * for new encryption parameters.
593eda14cbcSMatt Macy 		 */
594eda14cbcSMatt Macy 		if (!sd->raw) {
595eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
596eda14cbcSMatt Macy 			    "cannot send %s@%s: encrypted dataset %s may not "
597eda14cbcSMatt Macy 			    "be sent with properties without the raw flag\n"),
598eda14cbcSMatt Macy 			    sd->fsname, sd->tosnap, zhp->zfs_name);
599eda14cbcSMatt Macy 			rv = -1;
600eda14cbcSMatt Macy 			goto out;
601eda14cbcSMatt Macy 		}
602eda14cbcSMatt Macy 
603eda14cbcSMatt Macy 	}
604eda14cbcSMatt Macy 
605eda14cbcSMatt Macy 	/*
606c03c5b1cSMartin Matuska 	 * Iterate over snaps, and set sd->parent_fromsnap_guid.
607c03c5b1cSMartin Matuska 	 *
608eda14cbcSMatt Macy 	 * If this is a "doall" send, a replicate send or we're just trying
609eda14cbcSMatt Macy 	 * to gather a list of previous snapshots, iterate through all the
610eda14cbcSMatt Macy 	 * snaps in the txg range. Otherwise just look at the one we're
611eda14cbcSMatt Macy 	 * interested in.
612eda14cbcSMatt Macy 	 */
613c03c5b1cSMartin Matuska 	sd->parent_fromsnap_guid = 0;
614c03c5b1cSMartin Matuska 	sd->parent_snaps = fnvlist_alloc();
615c03c5b1cSMartin Matuska 	sd->snapprops = fnvlist_alloc();
616c03c5b1cSMartin Matuska 	if (sd->holds)
617c03c5b1cSMartin Matuska 		sd->snapholds = fnvlist_alloc();
618eda14cbcSMatt Macy 	if (sd->doall || sd->replicate || sd->tosnap == NULL) {
619eda14cbcSMatt Macy 		if (!sd->replicate && fromsnap_txg != 0)
620eda14cbcSMatt Macy 			min_txg = fromsnap_txg;
621eda14cbcSMatt Macy 		if (!sd->replicate && tosnap_txg != 0)
622eda14cbcSMatt Macy 			max_txg = tosnap_txg;
623d411c1d6SMartin Matuska 		(void) zfs_iter_snapshots_sorted_v2(zhp, 0, send_iterate_snap,
624d411c1d6SMartin Matuska 		    sd, min_txg, max_txg);
625eda14cbcSMatt Macy 	} else {
62615f0b8c3SMartin Matuska 		char snapname[MAXPATHLEN] = { 0 };
627eda14cbcSMatt Macy 		zfs_handle_t *snap;
628eda14cbcSMatt Macy 
629eda14cbcSMatt Macy 		(void) snprintf(snapname, sizeof (snapname), "%s@%s",
630eda14cbcSMatt Macy 		    zhp->zfs_name, sd->tosnap);
631eda14cbcSMatt Macy 		if (sd->fromsnap != NULL)
632eda14cbcSMatt Macy 			sd->seenfrom = B_TRUE;
633c03c5b1cSMartin Matuska 		snap = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
634eda14cbcSMatt Macy 		if (snap != NULL)
635eda14cbcSMatt Macy 			(void) send_iterate_snap(snap, sd);
636eda14cbcSMatt Macy 	}
637eda14cbcSMatt Macy 
638eda14cbcSMatt Macy 	fnvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps);
639eda14cbcSMatt Macy 	fnvlist_free(sd->parent_snaps);
640c03c5b1cSMartin Matuska 	fnvlist_add_nvlist(nvfs, "snapprops", sd->snapprops);
641eda14cbcSMatt Macy 	fnvlist_free(sd->snapprops);
642c03c5b1cSMartin Matuska 	if (sd->holds) {
643c03c5b1cSMartin Matuska 		fnvlist_add_nvlist(nvfs, "snapholds", sd->snapholds);
644eda14cbcSMatt Macy 		fnvlist_free(sd->snapholds);
645c03c5b1cSMartin Matuska 	}
646eda14cbcSMatt Macy 
647ac0bf12eSMatt Macy 	/* Do not allow the size of the properties list to exceed the limit */
648ac0bf12eSMatt Macy 	if ((fnvlist_size(nvfs) + fnvlist_size(sd->fss)) >
649ac0bf12eSMatt Macy 	    zhp->zfs_hdl->libzfs_max_nvlist) {
650ac0bf12eSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
651ac0bf12eSMatt Macy 		    "warning: cannot send %s@%s: the size of the list of "
652ac0bf12eSMatt Macy 		    "snapshots and properties is too large to be received "
653ac0bf12eSMatt Macy 		    "successfully.\n"
654ac0bf12eSMatt Macy 		    "Select a smaller number of snapshots to send.\n"),
655ac0bf12eSMatt Macy 		    zhp->zfs_name, sd->tosnap);
656ac0bf12eSMatt Macy 		rv = EZFS_NOSPC;
657ac0bf12eSMatt Macy 		goto out;
658ac0bf12eSMatt Macy 	}
659c03c5b1cSMartin Matuska 	/* Add this fs to nvlist. */
660eda14cbcSMatt Macy 	(void) snprintf(guidstring, sizeof (guidstring),
661eda14cbcSMatt Macy 	    "0x%llx", (longlong_t)guid);
662eda14cbcSMatt Macy 	fnvlist_add_nvlist(sd->fss, guidstring, nvfs);
663eda14cbcSMatt Macy 
664c03c5b1cSMartin Matuska 	/* Iterate over children. */
665eda14cbcSMatt Macy 	if (sd->recursive)
666d411c1d6SMartin Matuska 		rv = zfs_iter_filesystems_v2(zhp, 0, send_iterate_fs, sd);
667eda14cbcSMatt Macy 
668eda14cbcSMatt Macy out:
669c03c5b1cSMartin Matuska 	/* Restore saved fields. */
670eda14cbcSMatt Macy 	sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
671eda14cbcSMatt Macy 	sd->fromsnap_txg = fromsnap_txg_save;
672eda14cbcSMatt Macy 	sd->tosnap_txg = tosnap_txg_save;
673c03c5b1cSMartin Matuska 
674eda14cbcSMatt Macy 	fnvlist_free(nv);
675eda14cbcSMatt Macy 	fnvlist_free(nvfs);
676eda14cbcSMatt Macy 
677eda14cbcSMatt Macy 	zfs_close(zhp);
678eda14cbcSMatt Macy 	return (rv);
679eda14cbcSMatt Macy }
680eda14cbcSMatt Macy 
681eda14cbcSMatt Macy static int
682eda14cbcSMatt Macy gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
683eda14cbcSMatt Macy     const char *tosnap, boolean_t recursive, boolean_t raw, boolean_t doall,
68416038816SMartin Matuska     boolean_t replicate, boolean_t skipmissing, boolean_t verbose,
68516038816SMartin Matuska     boolean_t backup, boolean_t holds, boolean_t props, nvlist_t **nvlp,
68616038816SMartin Matuska     avl_tree_t **avlp)
687eda14cbcSMatt Macy {
688eda14cbcSMatt Macy 	zfs_handle_t *zhp;
689eda14cbcSMatt Macy 	send_data_t sd = { 0 };
690eda14cbcSMatt Macy 	int error;
691eda14cbcSMatt Macy 
692eda14cbcSMatt Macy 	zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
693eda14cbcSMatt Macy 	if (zhp == NULL)
694eda14cbcSMatt Macy 		return (EZFS_BADTYPE);
695eda14cbcSMatt Macy 
696184c1b94SMartin Matuska 	sd.fss = fnvlist_alloc();
697eda14cbcSMatt Macy 	sd.fsname = fsname;
698eda14cbcSMatt Macy 	sd.fromsnap = fromsnap;
699eda14cbcSMatt Macy 	sd.tosnap = tosnap;
700eda14cbcSMatt Macy 	sd.recursive = recursive;
701eda14cbcSMatt Macy 	sd.raw = raw;
702eda14cbcSMatt Macy 	sd.doall = doall;
703eda14cbcSMatt Macy 	sd.replicate = replicate;
70416038816SMartin Matuska 	sd.skipmissing = skipmissing;
705eda14cbcSMatt Macy 	sd.verbose = verbose;
706eda14cbcSMatt Macy 	sd.backup = backup;
707eda14cbcSMatt Macy 	sd.holds = holds;
708eda14cbcSMatt Macy 	sd.props = props;
709eda14cbcSMatt Macy 
710eda14cbcSMatt Macy 	if ((error = send_iterate_fs(zhp, &sd)) != 0) {
711184c1b94SMartin Matuska 		fnvlist_free(sd.fss);
712eda14cbcSMatt Macy 		if (avlp != NULL)
713eda14cbcSMatt Macy 			*avlp = NULL;
714eda14cbcSMatt Macy 		*nvlp = NULL;
715eda14cbcSMatt Macy 		return (error);
716eda14cbcSMatt Macy 	}
717eda14cbcSMatt Macy 
718eda14cbcSMatt Macy 	if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
719184c1b94SMartin Matuska 		fnvlist_free(sd.fss);
720eda14cbcSMatt Macy 		*nvlp = NULL;
721eda14cbcSMatt Macy 		return (EZFS_NOMEM);
722eda14cbcSMatt Macy 	}
723eda14cbcSMatt Macy 
724eda14cbcSMatt Macy 	*nvlp = sd.fss;
725eda14cbcSMatt Macy 	return (0);
726eda14cbcSMatt Macy }
727eda14cbcSMatt Macy 
728eda14cbcSMatt Macy /*
729eda14cbcSMatt Macy  * Routines specific to "zfs send"
730eda14cbcSMatt Macy  */
731eda14cbcSMatt Macy typedef struct send_dump_data {
732eda14cbcSMatt Macy 	/* these are all just the short snapname (the part after the @) */
733eda14cbcSMatt Macy 	const char *fromsnap;
734eda14cbcSMatt Macy 	const char *tosnap;
735eda14cbcSMatt Macy 	char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
736eda14cbcSMatt Macy 	uint64_t prevsnap_obj;
737eda14cbcSMatt Macy 	boolean_t seenfrom, seento, replicate, doall, fromorigin;
738eda14cbcSMatt Macy 	boolean_t dryrun, parsable, progress, embed_data, std_out;
739eda14cbcSMatt Macy 	boolean_t large_block, compress, raw, holds;
74015f0b8c3SMartin Matuska 	boolean_t progressastitle;
741eda14cbcSMatt Macy 	int outfd;
742eda14cbcSMatt Macy 	boolean_t err;
743eda14cbcSMatt Macy 	nvlist_t *fss;
744eda14cbcSMatt Macy 	nvlist_t *snapholds;
745eda14cbcSMatt Macy 	avl_tree_t *fsavl;
746eda14cbcSMatt Macy 	snapfilter_cb_t *filter_cb;
747eda14cbcSMatt Macy 	void *filter_cb_arg;
748eda14cbcSMatt Macy 	nvlist_t *debugnv;
749eda14cbcSMatt Macy 	char holdtag[ZFS_MAX_DATASET_NAME_LEN];
750eda14cbcSMatt Macy 	int cleanup_fd;
751eda14cbcSMatt Macy 	int verbosity;
752eda14cbcSMatt Macy 	uint64_t size;
753eda14cbcSMatt Macy } send_dump_data_t;
754eda14cbcSMatt Macy 
755eda14cbcSMatt Macy static int
756eda14cbcSMatt Macy zfs_send_space(zfs_handle_t *zhp, const char *snapname, const char *from,
757eda14cbcSMatt Macy     enum lzc_send_flags flags, uint64_t *spacep)
758eda14cbcSMatt Macy {
759eda14cbcSMatt Macy 	assert(snapname != NULL);
760eda14cbcSMatt Macy 
761c03c5b1cSMartin Matuska 	int error = lzc_send_space(snapname, from, flags, spacep);
762c03c5b1cSMartin Matuska 	if (error == 0)
763c03c5b1cSMartin Matuska 		return (0);
764c03c5b1cSMartin Matuska 
7651f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
766eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
767eda14cbcSMatt Macy 	    "warning: cannot estimate space for '%s'"), snapname);
768eda14cbcSMatt Macy 
769c03c5b1cSMartin Matuska 	libzfs_handle_t *hdl = zhp->zfs_hdl;
770eda14cbcSMatt Macy 	switch (error) {
771eda14cbcSMatt Macy 	case EXDEV:
772eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
773eda14cbcSMatt Macy 		    "not an earlier snapshot from the same fs"));
774eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
775eda14cbcSMatt Macy 
776eda14cbcSMatt Macy 	case ENOENT:
777eda14cbcSMatt Macy 		if (zfs_dataset_exists(hdl, snapname,
778eda14cbcSMatt Macy 		    ZFS_TYPE_SNAPSHOT)) {
779eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
780eda14cbcSMatt Macy 			    "incremental source (%s) does not exist"),
781eda14cbcSMatt Macy 			    snapname);
782eda14cbcSMatt Macy 		}
783eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
784eda14cbcSMatt Macy 
785eda14cbcSMatt Macy 	case EDQUOT:
786eda14cbcSMatt Macy 	case EFBIG:
787eda14cbcSMatt Macy 	case EIO:
788eda14cbcSMatt Macy 	case ENOLINK:
789eda14cbcSMatt Macy 	case ENOSPC:
790eda14cbcSMatt Macy 	case ENOSTR:
791eda14cbcSMatt Macy 	case ENXIO:
792eda14cbcSMatt Macy 	case EPIPE:
793eda14cbcSMatt Macy 	case ERANGE:
794eda14cbcSMatt Macy 	case EFAULT:
795eda14cbcSMatt Macy 	case EROFS:
796eda14cbcSMatt Macy 	case EINVAL:
797fd45b686SMartin Matuska 		zfs_error_aux(hdl, "%s", zfs_strerror(error));
798eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
799eda14cbcSMatt Macy 
800eda14cbcSMatt Macy 	default:
801eda14cbcSMatt Macy 		return (zfs_standard_error(hdl, error, errbuf));
802eda14cbcSMatt Macy 	}
803eda14cbcSMatt Macy }
804eda14cbcSMatt Macy 
805eda14cbcSMatt Macy /*
806eda14cbcSMatt Macy  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
807eda14cbcSMatt Macy  * NULL) to the file descriptor specified by outfd.
808eda14cbcSMatt Macy  */
809eda14cbcSMatt Macy static int
810eda14cbcSMatt Macy dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
811eda14cbcSMatt Macy     boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
812eda14cbcSMatt Macy     nvlist_t *debugnv)
813eda14cbcSMatt Macy {
814eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
815eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
816eda14cbcSMatt Macy 	nvlist_t *thisdbg;
817eda14cbcSMatt Macy 
818eda14cbcSMatt Macy 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
819eda14cbcSMatt Macy 	assert(fromsnap_obj == 0 || !fromorigin);
820eda14cbcSMatt Macy 
821eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
822eda14cbcSMatt Macy 	zc.zc_cookie = outfd;
823eda14cbcSMatt Macy 	zc.zc_obj = fromorigin;
824eda14cbcSMatt Macy 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
825eda14cbcSMatt Macy 	zc.zc_fromobj = fromsnap_obj;
826eda14cbcSMatt Macy 	zc.zc_flags = flags;
827eda14cbcSMatt Macy 
828c03c5b1cSMartin Matuska 	if (debugnv != NULL) {
829184c1b94SMartin Matuska 		thisdbg = fnvlist_alloc();
830c03c5b1cSMartin Matuska 		if (fromsnap != NULL && fromsnap[0] != '\0')
831184c1b94SMartin Matuska 			fnvlist_add_string(thisdbg, "fromsnap", fromsnap);
832eda14cbcSMatt Macy 	}
833eda14cbcSMatt Macy 
834eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
8351f1e2261SMartin Matuska 		char errbuf[ERRBUFLEN];
836c03c5b1cSMartin Matuska 		int error = errno;
837eda14cbcSMatt Macy 
838c03c5b1cSMartin Matuska 		(void) snprintf(errbuf, sizeof (errbuf), "%s '%s'",
839c03c5b1cSMartin Matuska 		    dgettext(TEXT_DOMAIN, "warning: cannot send"),
840c03c5b1cSMartin Matuska 		    zhp->zfs_name);
841c03c5b1cSMartin Matuska 
842c03c5b1cSMartin Matuska 		if (debugnv != NULL) {
843c03c5b1cSMartin Matuska 			fnvlist_add_uint64(thisdbg, "error", error);
844184c1b94SMartin Matuska 			fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
845184c1b94SMartin Matuska 			fnvlist_free(thisdbg);
846c03c5b1cSMartin Matuska 		}
847eda14cbcSMatt Macy 
848c03c5b1cSMartin Matuska 		switch (error) {
849eda14cbcSMatt Macy 		case EXDEV:
850eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
851eda14cbcSMatt Macy 			    "not an earlier snapshot from the same fs"));
852eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
853eda14cbcSMatt Macy 
854eda14cbcSMatt Macy 		case EACCES:
855eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
856eda14cbcSMatt Macy 			    "source key must be loaded"));
857eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
858eda14cbcSMatt Macy 
859eda14cbcSMatt Macy 		case ENOENT:
860eda14cbcSMatt Macy 			if (zfs_dataset_exists(hdl, zc.zc_name,
861eda14cbcSMatt Macy 			    ZFS_TYPE_SNAPSHOT)) {
862eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
863eda14cbcSMatt Macy 				    "incremental source (@%s) does not exist"),
864eda14cbcSMatt Macy 				    zc.zc_value);
865eda14cbcSMatt Macy 			}
866eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
867eda14cbcSMatt Macy 
868eda14cbcSMatt Macy 		case EDQUOT:
869eda14cbcSMatt Macy 		case EFBIG:
870eda14cbcSMatt Macy 		case EIO:
871eda14cbcSMatt Macy 		case ENOLINK:
872eda14cbcSMatt Macy 		case ENOSPC:
873eda14cbcSMatt Macy 		case ENOSTR:
874eda14cbcSMatt Macy 		case ENXIO:
875eda14cbcSMatt Macy 		case EPIPE:
876eda14cbcSMatt Macy 		case ERANGE:
877eda14cbcSMatt Macy 		case EFAULT:
878eda14cbcSMatt Macy 		case EROFS:
8793ff01b23SMartin Matuska 		case EINVAL:
880fd45b686SMartin Matuska 			zfs_error_aux(hdl, "%s", zfs_strerror(errno));
881eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
882eda14cbcSMatt Macy 
883eda14cbcSMatt Macy 		default:
884eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, errno, errbuf));
885eda14cbcSMatt Macy 		}
886eda14cbcSMatt Macy 	}
887eda14cbcSMatt Macy 
888c03c5b1cSMartin Matuska 	if (debugnv != NULL) {
889184c1b94SMartin Matuska 		fnvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg);
890184c1b94SMartin Matuska 		fnvlist_free(thisdbg);
891c03c5b1cSMartin Matuska 	}
892eda14cbcSMatt Macy 
893eda14cbcSMatt Macy 	return (0);
894eda14cbcSMatt Macy }
895eda14cbcSMatt Macy 
896eda14cbcSMatt Macy static void
897eda14cbcSMatt Macy gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
898eda14cbcSMatt Macy {
899eda14cbcSMatt Macy 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
900eda14cbcSMatt Macy 
901eda14cbcSMatt Macy 	/*
902eda14cbcSMatt Macy 	 * zfs_send() only sets snapholds for sends that need them,
903eda14cbcSMatt Macy 	 * e.g. replication and doall.
904eda14cbcSMatt Macy 	 */
905eda14cbcSMatt Macy 	if (sdd->snapholds == NULL)
906eda14cbcSMatt Macy 		return;
907eda14cbcSMatt Macy 
908eda14cbcSMatt Macy 	fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
909eda14cbcSMatt Macy }
910eda14cbcSMatt Macy 
911eda14cbcSMatt Macy int
912eda14cbcSMatt Macy zfs_send_progress(zfs_handle_t *zhp, int fd, uint64_t *bytes_written,
913eda14cbcSMatt Macy     uint64_t *blocks_visited)
914eda14cbcSMatt Macy {
915eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
916eda14cbcSMatt Macy 
917c03c5b1cSMartin Matuska 	if (bytes_written != NULL)
918c03c5b1cSMartin Matuska 		*bytes_written = 0;
919c03c5b1cSMartin Matuska 	if (blocks_visited != NULL)
920c03c5b1cSMartin Matuska 		*blocks_visited = 0;
921eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
922eda14cbcSMatt Macy 	zc.zc_cookie = fd;
923eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
924eda14cbcSMatt Macy 		return (errno);
925eda14cbcSMatt Macy 	if (bytes_written != NULL)
926eda14cbcSMatt Macy 		*bytes_written = zc.zc_cookie;
927eda14cbcSMatt Macy 	if (blocks_visited != NULL)
928eda14cbcSMatt Macy 		*blocks_visited = zc.zc_objset_type;
929eda14cbcSMatt Macy 	return (0);
930eda14cbcSMatt Macy }
931eda14cbcSMatt Macy 
932315ee00fSMartin Matuska static volatile boolean_t send_progress_thread_signal_duetotimer;
933315ee00fSMartin Matuska static void
934315ee00fSMartin Matuska send_progress_thread_act(int sig, siginfo_t *info, void *ucontext)
935315ee00fSMartin Matuska {
936315ee00fSMartin Matuska 	(void) sig, (void) ucontext;
937315ee00fSMartin Matuska 	send_progress_thread_signal_duetotimer = info->si_code == SI_TIMER;
938315ee00fSMartin Matuska }
939315ee00fSMartin Matuska 
940315ee00fSMartin Matuska struct timer_desirability {
941315ee00fSMartin Matuska 	timer_t timer;
942315ee00fSMartin Matuska 	boolean_t desired;
943315ee00fSMartin Matuska };
944315ee00fSMartin Matuska static void
945315ee00fSMartin Matuska timer_delete_cleanup(void *timer)
946315ee00fSMartin Matuska {
947315ee00fSMartin Matuska 	struct timer_desirability *td = timer;
948315ee00fSMartin Matuska 	if (td->desired)
949315ee00fSMartin Matuska 		timer_delete(td->timer);
950315ee00fSMartin Matuska }
951315ee00fSMartin Matuska 
952315ee00fSMartin Matuska #ifdef SIGINFO
953315ee00fSMartin Matuska #define	SEND_PROGRESS_THREAD_PARENT_BLOCK_SIGINFO sigaddset(&new, SIGINFO)
954315ee00fSMartin Matuska #else
955315ee00fSMartin Matuska #define	SEND_PROGRESS_THREAD_PARENT_BLOCK_SIGINFO
956315ee00fSMartin Matuska #endif
957315ee00fSMartin Matuska #define	SEND_PROGRESS_THREAD_PARENT_BLOCK(old) { \
958315ee00fSMartin Matuska 	sigset_t new; \
959315ee00fSMartin Matuska 	sigemptyset(&new); \
960315ee00fSMartin Matuska 	sigaddset(&new, SIGUSR1); \
961315ee00fSMartin Matuska 	SEND_PROGRESS_THREAD_PARENT_BLOCK_SIGINFO; \
962315ee00fSMartin Matuska 	pthread_sigmask(SIG_BLOCK, &new, old); \
963315ee00fSMartin Matuska }
964315ee00fSMartin Matuska 
965eda14cbcSMatt Macy static void *
966eda14cbcSMatt Macy send_progress_thread(void *arg)
967eda14cbcSMatt Macy {
968eda14cbcSMatt Macy 	progress_arg_t *pa = arg;
969eda14cbcSMatt Macy 	zfs_handle_t *zhp = pa->pa_zhp;
970eda14cbcSMatt Macy 	uint64_t bytes;
971eda14cbcSMatt Macy 	uint64_t blocks;
97215f0b8c3SMartin Matuska 	uint64_t total = pa->pa_size / 100;
973eda14cbcSMatt Macy 	char buf[16];
974eda14cbcSMatt Macy 	time_t t;
975716fd348SMartin Matuska 	struct tm tm;
976c03c5b1cSMartin Matuska 	int err;
977c03c5b1cSMartin Matuska 
978315ee00fSMartin Matuska 	const struct sigaction signal_action =
979315ee00fSMartin Matuska 	    {.sa_sigaction = send_progress_thread_act, .sa_flags = SA_SIGINFO};
980315ee00fSMartin Matuska 	struct sigevent timer_cfg =
981315ee00fSMartin Matuska 	    {.sigev_notify = SIGEV_SIGNAL, .sigev_signo = SIGUSR1};
982315ee00fSMartin Matuska 	const struct itimerspec timer_time =
983315ee00fSMartin Matuska 	    {.it_value = {.tv_sec = 1}, .it_interval = {.tv_sec = 1}};
984315ee00fSMartin Matuska 	struct timer_desirability timer = {};
985315ee00fSMartin Matuska 
986315ee00fSMartin Matuska 	sigaction(SIGUSR1, &signal_action, NULL);
987315ee00fSMartin Matuska #ifdef SIGINFO
988315ee00fSMartin Matuska 	sigaction(SIGINFO, &signal_action, NULL);
989315ee00fSMartin Matuska #endif
990315ee00fSMartin Matuska 
991315ee00fSMartin Matuska 	if ((timer.desired = pa->pa_progress || pa->pa_astitle)) {
992315ee00fSMartin Matuska 		if (timer_create(CLOCK_MONOTONIC, &timer_cfg, &timer.timer))
993315ee00fSMartin Matuska 			return ((void *)(uintptr_t)errno);
994315ee00fSMartin Matuska 		(void) timer_settime(timer.timer, 0, &timer_time, NULL);
995315ee00fSMartin Matuska 	}
996315ee00fSMartin Matuska 	pthread_cleanup_push(timer_delete_cleanup, &timer);
997315ee00fSMartin Matuska 
998c9539b89SMartin Matuska 	if (!pa->pa_parsable && pa->pa_progress) {
999c03c5b1cSMartin Matuska 		(void) fprintf(stderr,
1000c03c5b1cSMartin Matuska 		    "TIME       %s   %sSNAPSHOT %s\n",
1001c03c5b1cSMartin Matuska 		    pa->pa_estimate ? "BYTES" : " SENT",
1002c03c5b1cSMartin Matuska 		    pa->pa_verbosity >= 2 ? "   BLOCKS    " : "",
1003c03c5b1cSMartin Matuska 		    zhp->zfs_name);
1004c03c5b1cSMartin Matuska 	}
1005eda14cbcSMatt Macy 
1006eda14cbcSMatt Macy 	/*
1007eda14cbcSMatt Macy 	 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1008eda14cbcSMatt Macy 	 */
1009eda14cbcSMatt Macy 	for (;;) {
1010315ee00fSMartin Matuska 		pause();
1011eda14cbcSMatt Macy 		if ((err = zfs_send_progress(zhp, pa->pa_fd, &bytes,
1012eda14cbcSMatt Macy 		    &blocks)) != 0) {
1013eda14cbcSMatt Macy 			if (err == EINTR || err == ENOENT)
1014315ee00fSMartin Matuska 				err = 0;
1015315ee00fSMartin Matuska 			pthread_exit(((void *)(uintptr_t)err));
1016eda14cbcSMatt Macy 		}
1017eda14cbcSMatt Macy 
1018eda14cbcSMatt Macy 		(void) time(&t);
1019716fd348SMartin Matuska 		localtime_r(&t, &tm);
1020eda14cbcSMatt Macy 
102115f0b8c3SMartin Matuska 		if (pa->pa_astitle) {
102215f0b8c3SMartin Matuska 			char buf_bytes[16];
102315f0b8c3SMartin Matuska 			char buf_size[16];
102415f0b8c3SMartin Matuska 			int pct;
102515f0b8c3SMartin Matuska 			zfs_nicenum(bytes, buf_bytes, sizeof (buf_bytes));
102615f0b8c3SMartin Matuska 			zfs_nicenum(pa->pa_size, buf_size, sizeof (buf_size));
102715f0b8c3SMartin Matuska 			pct = (total > 0) ? bytes / total : 100;
102815f0b8c3SMartin Matuska 			zfs_setproctitle("sending %s (%d%%: %s/%s)",
102915f0b8c3SMartin Matuska 			    zhp->zfs_name, MIN(pct, 100), buf_bytes, buf_size);
103015f0b8c3SMartin Matuska 		}
103115f0b8c3SMartin Matuska 
1032eda14cbcSMatt Macy 		if (pa->pa_verbosity >= 2 && pa->pa_parsable) {
1033eda14cbcSMatt Macy 			(void) fprintf(stderr,
1034eda14cbcSMatt Macy 			    "%02d:%02d:%02d\t%llu\t%llu\t%s\n",
1035716fd348SMartin Matuska 			    tm.tm_hour, tm.tm_min, tm.tm_sec,
1036eda14cbcSMatt Macy 			    (u_longlong_t)bytes, (u_longlong_t)blocks,
1037eda14cbcSMatt Macy 			    zhp->zfs_name);
1038eda14cbcSMatt Macy 		} else if (pa->pa_verbosity >= 2) {
1039eda14cbcSMatt Macy 			zfs_nicenum(bytes, buf, sizeof (buf));
1040eda14cbcSMatt Macy 			(void) fprintf(stderr,
1041eda14cbcSMatt Macy 			    "%02d:%02d:%02d   %5s    %8llu    %s\n",
1042716fd348SMartin Matuska 			    tm.tm_hour, tm.tm_min, tm.tm_sec,
1043eda14cbcSMatt Macy 			    buf, (u_longlong_t)blocks, zhp->zfs_name);
1044eda14cbcSMatt Macy 		} else if (pa->pa_parsable) {
1045eda14cbcSMatt Macy 			(void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1046716fd348SMartin Matuska 			    tm.tm_hour, tm.tm_min, tm.tm_sec,
1047eda14cbcSMatt Macy 			    (u_longlong_t)bytes, zhp->zfs_name);
1048315ee00fSMartin Matuska 		} else if (pa->pa_progress ||
1049315ee00fSMartin Matuska 		    !send_progress_thread_signal_duetotimer) {
1050eda14cbcSMatt Macy 			zfs_nicebytes(bytes, buf, sizeof (buf));
1051eda14cbcSMatt Macy 			(void) fprintf(stderr, "%02d:%02d:%02d   %5s   %s\n",
1052716fd348SMartin Matuska 			    tm.tm_hour, tm.tm_min, tm.tm_sec,
1053eda14cbcSMatt Macy 			    buf, zhp->zfs_name);
1054eda14cbcSMatt Macy 		}
1055eda14cbcSMatt Macy 	}
1056315ee00fSMartin Matuska 	pthread_cleanup_pop(B_TRUE);
10571719886fSMartin Matuska 	return (NULL);
1058eda14cbcSMatt Macy }
1059eda14cbcSMatt Macy 
1060716fd348SMartin Matuska static boolean_t
1061315ee00fSMartin Matuska send_progress_thread_exit(
1062315ee00fSMartin Matuska     libzfs_handle_t *hdl, pthread_t ptid, sigset_t *oldmask)
1063716fd348SMartin Matuska {
1064716fd348SMartin Matuska 	void *status = NULL;
1065716fd348SMartin Matuska 	(void) pthread_cancel(ptid);
1066716fd348SMartin Matuska 	(void) pthread_join(ptid, &status);
1067315ee00fSMartin Matuska 	pthread_sigmask(SIG_SETMASK, oldmask, NULL);
1068716fd348SMartin Matuska 	int error = (int)(uintptr_t)status;
1069716fd348SMartin Matuska 	if (error != 0 && status != PTHREAD_CANCELED)
1070716fd348SMartin Matuska 		return (zfs_standard_error(hdl, error,
1071716fd348SMartin Matuska 		    dgettext(TEXT_DOMAIN, "progress thread exited nonzero")));
1072716fd348SMartin Matuska 	else
1073716fd348SMartin Matuska 		return (B_FALSE);
1074716fd348SMartin Matuska }
1075716fd348SMartin Matuska 
1076eda14cbcSMatt Macy static void
1077eda14cbcSMatt Macy send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1078eda14cbcSMatt Macy     uint64_t size, boolean_t parsable)
1079eda14cbcSMatt Macy {
1080eda14cbcSMatt Macy 	if (parsable) {
1081eda14cbcSMatt Macy 		if (fromsnap != NULL) {
1082c03c5b1cSMartin Matuska 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1083c03c5b1cSMartin Matuska 			    "incremental\t%s\t%s"), fromsnap, tosnap);
1084eda14cbcSMatt Macy 		} else {
1085bb2d13b6SMartin Matuska /*
1086bb2d13b6SMartin Matuska  * Workaround for GCC 12+ with UBSan enabled deficencies.
1087bb2d13b6SMartin Matuska  *
1088bb2d13b6SMartin Matuska  * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1089bb2d13b6SMartin Matuska  * below as violating -Wformat-overflow.
1090bb2d13b6SMartin Matuska  */
1091bb2d13b6SMartin Matuska #if defined(__GNUC__) && !defined(__clang__) && \
1092bb2d13b6SMartin Matuska 	defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1093bb2d13b6SMartin Matuska #pragma GCC diagnostic push
1094bb2d13b6SMartin Matuska #pragma GCC diagnostic ignored "-Wformat-overflow"
1095bb2d13b6SMartin Matuska #endif
1096c03c5b1cSMartin Matuska 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1097c03c5b1cSMartin Matuska 			    "full\t%s"), tosnap);
1098bb2d13b6SMartin Matuska #if defined(__GNUC__) && !defined(__clang__) && \
1099bb2d13b6SMartin Matuska 	defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1100bb2d13b6SMartin Matuska #pragma GCC diagnostic pop
1101bb2d13b6SMartin Matuska #endif
1102eda14cbcSMatt Macy 		}
1103c03c5b1cSMartin Matuska 		(void) fprintf(fout, "\t%llu", (longlong_t)size);
1104eda14cbcSMatt Macy 	} else {
1105eda14cbcSMatt Macy 		if (fromsnap != NULL) {
1106eda14cbcSMatt Macy 			if (strchr(fromsnap, '@') == NULL &&
1107eda14cbcSMatt Macy 			    strchr(fromsnap, '#') == NULL) {
1108eda14cbcSMatt Macy 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1109c03c5b1cSMartin Matuska 				    "send from @%s to %s"), fromsnap, tosnap);
1110eda14cbcSMatt Macy 			} else {
1111eda14cbcSMatt Macy 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1112c03c5b1cSMartin Matuska 				    "send from %s to %s"), fromsnap, tosnap);
1113eda14cbcSMatt Macy 			}
1114eda14cbcSMatt Macy 		} else {
1115eda14cbcSMatt Macy 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1116c03c5b1cSMartin Matuska 			    "full send of %s"), tosnap);
1117eda14cbcSMatt Macy 		}
1118c03c5b1cSMartin Matuska 		if (size != 0) {
1119eda14cbcSMatt Macy 			char buf[16];
1120eda14cbcSMatt Macy 			zfs_nicebytes(size, buf, sizeof (buf));
1121bb2d13b6SMartin Matuska /*
1122bb2d13b6SMartin Matuska  * Workaround for GCC 12+ with UBSan enabled deficencies.
1123bb2d13b6SMartin Matuska  *
1124bb2d13b6SMartin Matuska  * GCC 12+ invoked with -fsanitize=undefined incorrectly reports the code
1125bb2d13b6SMartin Matuska  * below as violating -Wformat-overflow.
1126bb2d13b6SMartin Matuska  */
1127bb2d13b6SMartin Matuska #if defined(__GNUC__) && !defined(__clang__) && \
1128bb2d13b6SMartin Matuska 	defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1129bb2d13b6SMartin Matuska #pragma GCC diagnostic push
1130bb2d13b6SMartin Matuska #pragma GCC diagnostic ignored "-Wformat-overflow"
1131bb2d13b6SMartin Matuska #endif
1132eda14cbcSMatt Macy 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1133eda14cbcSMatt Macy 			    " estimated size is %s"), buf);
1134bb2d13b6SMartin Matuska #if defined(__GNUC__) && !defined(__clang__) && \
1135bb2d13b6SMartin Matuska 	defined(ZFS_UBSAN_ENABLED) && defined(HAVE_FORMAT_OVERFLOW)
1136bb2d13b6SMartin Matuska #pragma GCC diagnostic pop
1137bb2d13b6SMartin Matuska #endif
1138eda14cbcSMatt Macy 		}
1139c03c5b1cSMartin Matuska 	}
1140eda14cbcSMatt Macy 	(void) fprintf(fout, "\n");
1141eda14cbcSMatt Macy }
1142eda14cbcSMatt Macy 
1143c03c5b1cSMartin Matuska /*
1144c03c5b1cSMartin Matuska  * Send a single filesystem snapshot, updating the send dump data.
1145d411c1d6SMartin Matuska  * This interface is intended for use as a zfs_iter_snapshots_v2_sorted visitor.
1146c03c5b1cSMartin Matuska  */
1147eda14cbcSMatt Macy static int
1148eda14cbcSMatt Macy dump_snapshot(zfs_handle_t *zhp, void *arg)
1149eda14cbcSMatt Macy {
1150eda14cbcSMatt Macy 	send_dump_data_t *sdd = arg;
1151eda14cbcSMatt Macy 	progress_arg_t pa = { 0 };
1152eda14cbcSMatt Macy 	pthread_t tid;
1153eda14cbcSMatt Macy 	char *thissnap;
1154eda14cbcSMatt Macy 	enum lzc_send_flags flags = 0;
1155eda14cbcSMatt Macy 	int err;
1156eda14cbcSMatt Macy 	boolean_t isfromsnap, istosnap, fromorigin;
1157eda14cbcSMatt Macy 	boolean_t exclude = B_FALSE;
1158eda14cbcSMatt Macy 	FILE *fout = sdd->std_out ? stdout : stderr;
1159eda14cbcSMatt Macy 
1160eda14cbcSMatt Macy 	err = 0;
1161eda14cbcSMatt Macy 	thissnap = strchr(zhp->zfs_name, '@') + 1;
1162eda14cbcSMatt Macy 	isfromsnap = (sdd->fromsnap != NULL &&
1163eda14cbcSMatt Macy 	    strcmp(sdd->fromsnap, thissnap) == 0);
1164eda14cbcSMatt Macy 
1165eda14cbcSMatt Macy 	if (!sdd->seenfrom && isfromsnap) {
1166eda14cbcSMatt Macy 		gather_holds(zhp, sdd);
1167eda14cbcSMatt Macy 		sdd->seenfrom = B_TRUE;
1168c03c5b1cSMartin Matuska 		(void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap));
1169eda14cbcSMatt Macy 		sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1170eda14cbcSMatt Macy 		zfs_close(zhp);
1171eda14cbcSMatt Macy 		return (0);
1172eda14cbcSMatt Macy 	}
1173eda14cbcSMatt Macy 
1174eda14cbcSMatt Macy 	if (sdd->seento || !sdd->seenfrom) {
1175eda14cbcSMatt Macy 		zfs_close(zhp);
1176eda14cbcSMatt Macy 		return (0);
1177eda14cbcSMatt Macy 	}
1178eda14cbcSMatt Macy 
1179eda14cbcSMatt Macy 	istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1180eda14cbcSMatt Macy 	if (istosnap)
1181eda14cbcSMatt Macy 		sdd->seento = B_TRUE;
1182eda14cbcSMatt Macy 
1183eda14cbcSMatt Macy 	if (sdd->large_block)
1184eda14cbcSMatt Macy 		flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1185eda14cbcSMatt Macy 	if (sdd->embed_data)
1186eda14cbcSMatt Macy 		flags |= LZC_SEND_FLAG_EMBED_DATA;
1187eda14cbcSMatt Macy 	if (sdd->compress)
1188eda14cbcSMatt Macy 		flags |= LZC_SEND_FLAG_COMPRESS;
1189eda14cbcSMatt Macy 	if (sdd->raw)
1190eda14cbcSMatt Macy 		flags |= LZC_SEND_FLAG_RAW;
1191eda14cbcSMatt Macy 
1192eda14cbcSMatt Macy 	if (!sdd->doall && !isfromsnap && !istosnap) {
1193eda14cbcSMatt Macy 		if (sdd->replicate) {
11942a58b312SMartin Matuska 			const char *snapname;
1195eda14cbcSMatt Macy 			nvlist_t *snapprops;
1196eda14cbcSMatt Macy 			/*
1197eda14cbcSMatt Macy 			 * Filter out all intermediate snapshots except origin
1198eda14cbcSMatt Macy 			 * snapshots needed to replicate clones.
1199eda14cbcSMatt Macy 			 */
1200eda14cbcSMatt Macy 			nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1201eda14cbcSMatt Macy 			    zhp->zfs_dmustats.dds_guid, &snapname);
1202eda14cbcSMatt Macy 
12036ba2210eSMartin Matuska 			if (nvfs != NULL) {
12046ba2210eSMartin Matuska 				snapprops = fnvlist_lookup_nvlist(nvfs,
12056ba2210eSMartin Matuska 				    "snapprops");
12066ba2210eSMartin Matuska 				snapprops = fnvlist_lookup_nvlist(snapprops,
12076ba2210eSMartin Matuska 				    thissnap);
12086ba2210eSMartin Matuska 				exclude = !nvlist_exists(snapprops,
12096ba2210eSMartin Matuska 				    "is_clone_origin");
12106ba2210eSMartin Matuska 			}
1211eda14cbcSMatt Macy 		} else {
1212eda14cbcSMatt Macy 			exclude = B_TRUE;
1213eda14cbcSMatt Macy 		}
1214eda14cbcSMatt Macy 	}
1215eda14cbcSMatt Macy 
1216eda14cbcSMatt Macy 	/*
1217eda14cbcSMatt Macy 	 * If a filter function exists, call it to determine whether
1218eda14cbcSMatt Macy 	 * this snapshot will be sent.
1219eda14cbcSMatt Macy 	 */
1220eda14cbcSMatt Macy 	if (exclude || (sdd->filter_cb != NULL &&
1221eda14cbcSMatt Macy 	    sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1222eda14cbcSMatt Macy 		/*
1223eda14cbcSMatt Macy 		 * This snapshot is filtered out.  Don't send it, and don't
1224eda14cbcSMatt Macy 		 * set prevsnap_obj, so it will be as if this snapshot didn't
1225eda14cbcSMatt Macy 		 * exist, and the next accepted snapshot will be sent as
1226eda14cbcSMatt Macy 		 * an incremental from the last accepted one, or as the
1227eda14cbcSMatt Macy 		 * first (and full) snapshot in the case of a replication,
1228eda14cbcSMatt Macy 		 * non-incremental send.
1229eda14cbcSMatt Macy 		 */
1230eda14cbcSMatt Macy 		zfs_close(zhp);
1231eda14cbcSMatt Macy 		return (0);
1232eda14cbcSMatt Macy 	}
1233eda14cbcSMatt Macy 
1234eda14cbcSMatt Macy 	gather_holds(zhp, sdd);
1235eda14cbcSMatt Macy 	fromorigin = sdd->prevsnap[0] == '\0' &&
1236eda14cbcSMatt Macy 	    (sdd->fromorigin || sdd->replicate);
1237eda14cbcSMatt Macy 
1238eda14cbcSMatt Macy 	if (sdd->verbosity != 0) {
1239eda14cbcSMatt Macy 		uint64_t size = 0;
1240eda14cbcSMatt Macy 		char fromds[ZFS_MAX_DATASET_NAME_LEN];
1241eda14cbcSMatt Macy 
1242eda14cbcSMatt Macy 		if (sdd->prevsnap[0] != '\0') {
1243eda14cbcSMatt Macy 			(void) strlcpy(fromds, zhp->zfs_name, sizeof (fromds));
1244eda14cbcSMatt Macy 			*(strchr(fromds, '@') + 1) = '\0';
1245eda14cbcSMatt Macy 			(void) strlcat(fromds, sdd->prevsnap, sizeof (fromds));
1246eda14cbcSMatt Macy 		}
1247eda14cbcSMatt Macy 		if (zfs_send_space(zhp, zhp->zfs_name,
1248c03c5b1cSMartin Matuska 		    sdd->prevsnap[0] ? fromds : NULL, flags, &size) == 0) {
1249eda14cbcSMatt Macy 			send_print_verbose(fout, zhp->zfs_name,
1250eda14cbcSMatt Macy 			    sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1251eda14cbcSMatt Macy 			    size, sdd->parsable);
1252eda14cbcSMatt Macy 			sdd->size += size;
1253eda14cbcSMatt Macy 		}
1254c03c5b1cSMartin Matuska 	}
1255eda14cbcSMatt Macy 
1256eda14cbcSMatt Macy 	if (!sdd->dryrun) {
1257eda14cbcSMatt Macy 		/*
1258eda14cbcSMatt Macy 		 * If progress reporting is requested, spawn a new thread to
1259eda14cbcSMatt Macy 		 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1260eda14cbcSMatt Macy 		 */
1261315ee00fSMartin Matuska 		sigset_t oldmask;
1262315ee00fSMartin Matuska 		{
1263eda14cbcSMatt Macy 			pa.pa_zhp = zhp;
1264eda14cbcSMatt Macy 			pa.pa_fd = sdd->outfd;
1265eda14cbcSMatt Macy 			pa.pa_parsable = sdd->parsable;
1266eda14cbcSMatt Macy 			pa.pa_estimate = B_FALSE;
1267eda14cbcSMatt Macy 			pa.pa_verbosity = sdd->verbosity;
126815f0b8c3SMartin Matuska 			pa.pa_size = sdd->size;
126915f0b8c3SMartin Matuska 			pa.pa_astitle = sdd->progressastitle;
1270c9539b89SMartin Matuska 			pa.pa_progress = sdd->progress;
1271eda14cbcSMatt Macy 
1272eda14cbcSMatt Macy 			if ((err = pthread_create(&tid, NULL,
1273eda14cbcSMatt Macy 			    send_progress_thread, &pa)) != 0) {
1274eda14cbcSMatt Macy 				zfs_close(zhp);
1275eda14cbcSMatt Macy 				return (err);
1276eda14cbcSMatt Macy 			}
1277315ee00fSMartin Matuska 			SEND_PROGRESS_THREAD_PARENT_BLOCK(&oldmask);
1278eda14cbcSMatt Macy 		}
1279eda14cbcSMatt Macy 
1280eda14cbcSMatt Macy 		err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1281eda14cbcSMatt Macy 		    fromorigin, sdd->outfd, flags, sdd->debugnv);
1282eda14cbcSMatt Macy 
1283315ee00fSMartin Matuska 		if (send_progress_thread_exit(zhp->zfs_hdl, tid, &oldmask))
1284716fd348SMartin Matuska 			return (-1);
1285eda14cbcSMatt Macy 	}
1286eda14cbcSMatt Macy 
1287be181ee2SMartin Matuska 	(void) strlcpy(sdd->prevsnap, thissnap, sizeof (sdd->prevsnap));
1288eda14cbcSMatt Macy 	sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1289eda14cbcSMatt Macy 	zfs_close(zhp);
1290eda14cbcSMatt Macy 	return (err);
1291eda14cbcSMatt Macy }
1292eda14cbcSMatt Macy 
1293c03c5b1cSMartin Matuska /*
1294c03c5b1cSMartin Matuska  * Send all snapshots for a filesystem, updating the send dump data.
1295c03c5b1cSMartin Matuska  */
1296eda14cbcSMatt Macy static int
1297c03c5b1cSMartin Matuska dump_filesystem(zfs_handle_t *zhp, send_dump_data_t *sdd)
1298eda14cbcSMatt Macy {
1299eda14cbcSMatt Macy 	int rv = 0;
1300eda14cbcSMatt Macy 	boolean_t missingfrom = B_FALSE;
1301eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
1302eda14cbcSMatt Macy 	uint64_t min_txg = 0, max_txg = 0;
1303eda14cbcSMatt Macy 
1304c03c5b1cSMartin Matuska 	/*
1305c03c5b1cSMartin Matuska 	 * Make sure the tosnap exists.
1306c03c5b1cSMartin Matuska 	 */
1307eda14cbcSMatt Macy 	(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1308eda14cbcSMatt Macy 	    zhp->zfs_name, sdd->tosnap);
1309eda14cbcSMatt Macy 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1310eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1311eda14cbcSMatt Macy 		    "WARNING: could not send %s@%s: does not exist\n"),
1312eda14cbcSMatt Macy 		    zhp->zfs_name, sdd->tosnap);
1313eda14cbcSMatt Macy 		sdd->err = B_TRUE;
1314eda14cbcSMatt Macy 		return (0);
1315eda14cbcSMatt Macy 	}
1316eda14cbcSMatt Macy 
1317eda14cbcSMatt Macy 	/*
1318eda14cbcSMatt Macy 	 * If this fs does not have fromsnap, and we're doing
1319eda14cbcSMatt Macy 	 * recursive, we need to send a full stream from the
1320eda14cbcSMatt Macy 	 * beginning (or an incremental from the origin if this
1321eda14cbcSMatt Macy 	 * is a clone).  If we're doing non-recursive, then let
1322eda14cbcSMatt Macy 	 * them get the error.
1323eda14cbcSMatt Macy 	 */
1324c03c5b1cSMartin Matuska 	if (sdd->replicate && sdd->fromsnap) {
1325c03c5b1cSMartin Matuska 		/*
1326c03c5b1cSMartin Matuska 		 * Make sure the fromsnap exists.
1327c03c5b1cSMartin Matuska 		 */
1328eda14cbcSMatt Macy 		(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1329eda14cbcSMatt Macy 		    zhp->zfs_name, sdd->fromsnap);
1330c03c5b1cSMartin Matuska 		if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_STATS, &zc) != 0)
1331eda14cbcSMatt Macy 			missingfrom = B_TRUE;
1332eda14cbcSMatt Macy 	}
1333eda14cbcSMatt Macy 
1334c03c5b1cSMartin Matuska 	sdd->seenfrom = sdd->seento = B_FALSE;
1335c03c5b1cSMartin Matuska 	sdd->prevsnap[0] = '\0';
1336eda14cbcSMatt Macy 	sdd->prevsnap_obj = 0;
1337eda14cbcSMatt Macy 	if (sdd->fromsnap == NULL || missingfrom)
1338eda14cbcSMatt Macy 		sdd->seenfrom = B_TRUE;
1339eda14cbcSMatt Macy 
1340eda14cbcSMatt Macy 	/*
1341eda14cbcSMatt Macy 	 * Iterate through all snapshots and process the ones we will be
1342eda14cbcSMatt Macy 	 * sending. If we only have a "from" and "to" snapshot to deal
1343eda14cbcSMatt Macy 	 * with, we can avoid iterating through all the other snapshots.
1344eda14cbcSMatt Macy 	 */
1345eda14cbcSMatt Macy 	if (sdd->doall || sdd->replicate || sdd->tosnap == NULL) {
1346c03c5b1cSMartin Matuska 		if (!sdd->replicate) {
1347c03c5b1cSMartin Matuska 			if (sdd->fromsnap != NULL) {
1348c03c5b1cSMartin Matuska 				min_txg = get_snap_txg(zhp->zfs_hdl,
1349c03c5b1cSMartin Matuska 				    zhp->zfs_name, sdd->fromsnap);
1350c03c5b1cSMartin Matuska 			}
1351c03c5b1cSMartin Matuska 			if (sdd->tosnap != NULL) {
1352c03c5b1cSMartin Matuska 				max_txg = get_snap_txg(zhp->zfs_hdl,
1353c03c5b1cSMartin Matuska 				    zhp->zfs_name, sdd->tosnap);
1354c03c5b1cSMartin Matuska 			}
1355c03c5b1cSMartin Matuska 		}
1356d411c1d6SMartin Matuska 		rv = zfs_iter_snapshots_sorted_v2(zhp, 0, dump_snapshot, sdd,
1357eda14cbcSMatt Macy 		    min_txg, max_txg);
1358eda14cbcSMatt Macy 	} else {
1359eda14cbcSMatt Macy 		char snapname[MAXPATHLEN] = { 0 };
1360eda14cbcSMatt Macy 		zfs_handle_t *snap;
1361eda14cbcSMatt Macy 
1362c03c5b1cSMartin Matuska 		/* Dump fromsnap. */
1363eda14cbcSMatt Macy 		if (!sdd->seenfrom) {
1364eda14cbcSMatt Macy 			(void) snprintf(snapname, sizeof (snapname),
1365eda14cbcSMatt Macy 			    "%s@%s", zhp->zfs_name, sdd->fromsnap);
1366eda14cbcSMatt Macy 			snap = zfs_open(zhp->zfs_hdl, snapname,
1367eda14cbcSMatt Macy 			    ZFS_TYPE_SNAPSHOT);
1368eda14cbcSMatt Macy 			if (snap != NULL)
1369eda14cbcSMatt Macy 				rv = dump_snapshot(snap, sdd);
1370eda14cbcSMatt Macy 			else
137115f0b8c3SMartin Matuska 				rv = errno;
1372eda14cbcSMatt Macy 		}
1373eda14cbcSMatt Macy 
1374c03c5b1cSMartin Matuska 		/* Dump tosnap. */
1375eda14cbcSMatt Macy 		if (rv == 0) {
1376eda14cbcSMatt Macy 			(void) snprintf(snapname, sizeof (snapname),
1377eda14cbcSMatt Macy 			    "%s@%s", zhp->zfs_name, sdd->tosnap);
1378eda14cbcSMatt Macy 			snap = zfs_open(zhp->zfs_hdl, snapname,
1379eda14cbcSMatt Macy 			    ZFS_TYPE_SNAPSHOT);
1380eda14cbcSMatt Macy 			if (snap != NULL)
1381eda14cbcSMatt Macy 				rv = dump_snapshot(snap, sdd);
1382eda14cbcSMatt Macy 			else
138315f0b8c3SMartin Matuska 				rv = errno;
1384eda14cbcSMatt Macy 		}
1385eda14cbcSMatt Macy 	}
1386eda14cbcSMatt Macy 
1387eda14cbcSMatt Macy 	if (!sdd->seenfrom) {
1388eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1389eda14cbcSMatt Macy 		    "WARNING: could not send %s@%s:\n"
1390eda14cbcSMatt Macy 		    "incremental source (%s@%s) does not exist\n"),
1391eda14cbcSMatt Macy 		    zhp->zfs_name, sdd->tosnap,
1392eda14cbcSMatt Macy 		    zhp->zfs_name, sdd->fromsnap);
1393eda14cbcSMatt Macy 		sdd->err = B_TRUE;
1394eda14cbcSMatt Macy 	} else if (!sdd->seento) {
1395eda14cbcSMatt Macy 		if (sdd->fromsnap) {
1396eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1397eda14cbcSMatt Macy 			    "WARNING: could not send %s@%s:\n"
1398eda14cbcSMatt Macy 			    "incremental source (%s@%s) "
1399eda14cbcSMatt Macy 			    "is not earlier than it\n"),
1400eda14cbcSMatt Macy 			    zhp->zfs_name, sdd->tosnap,
1401eda14cbcSMatt Macy 			    zhp->zfs_name, sdd->fromsnap);
1402eda14cbcSMatt Macy 		} else {
1403eda14cbcSMatt Macy 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1404eda14cbcSMatt Macy 			    "WARNING: "
1405eda14cbcSMatt Macy 			    "could not send %s@%s: does not exist\n"),
1406eda14cbcSMatt Macy 			    zhp->zfs_name, sdd->tosnap);
1407eda14cbcSMatt Macy 		}
1408eda14cbcSMatt Macy 		sdd->err = B_TRUE;
1409eda14cbcSMatt Macy 	}
1410eda14cbcSMatt Macy 
1411eda14cbcSMatt Macy 	return (rv);
1412eda14cbcSMatt Macy }
1413eda14cbcSMatt Macy 
1414c03c5b1cSMartin Matuska /*
1415c03c5b1cSMartin Matuska  * Send all snapshots for all filesystems in sdd.
1416c03c5b1cSMartin Matuska  */
1417eda14cbcSMatt Macy static int
1418c03c5b1cSMartin Matuska dump_filesystems(zfs_handle_t *rzhp, send_dump_data_t *sdd)
1419eda14cbcSMatt Macy {
1420eda14cbcSMatt Macy 	nvpair_t *fspair;
1421eda14cbcSMatt Macy 	boolean_t needagain, progress;
1422eda14cbcSMatt Macy 
1423eda14cbcSMatt Macy 	if (!sdd->replicate)
1424eda14cbcSMatt Macy 		return (dump_filesystem(rzhp, sdd));
1425eda14cbcSMatt Macy 
1426eda14cbcSMatt Macy 	/* Mark the clone origin snapshots. */
1427eda14cbcSMatt Macy 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1428eda14cbcSMatt Macy 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1429eda14cbcSMatt Macy 		nvlist_t *nvfs;
1430eda14cbcSMatt Macy 		uint64_t origin_guid = 0;
1431eda14cbcSMatt Macy 
1432184c1b94SMartin Matuska 		nvfs = fnvpair_value_nvlist(fspair);
1433eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1434eda14cbcSMatt Macy 		if (origin_guid != 0) {
14352a58b312SMartin Matuska 			const char *snapname;
1436eda14cbcSMatt Macy 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1437eda14cbcSMatt Macy 			    origin_guid, &snapname);
1438eda14cbcSMatt Macy 			if (origin_nv != NULL) {
1439eda14cbcSMatt Macy 				nvlist_t *snapprops;
1440184c1b94SMartin Matuska 				snapprops = fnvlist_lookup_nvlist(origin_nv,
1441184c1b94SMartin Matuska 				    "snapprops");
1442184c1b94SMartin Matuska 				snapprops = fnvlist_lookup_nvlist(snapprops,
1443184c1b94SMartin Matuska 				    snapname);
1444184c1b94SMartin Matuska 				fnvlist_add_boolean(snapprops,
1445184c1b94SMartin Matuska 				    "is_clone_origin");
1446eda14cbcSMatt Macy 			}
1447eda14cbcSMatt Macy 		}
1448eda14cbcSMatt Macy 	}
1449eda14cbcSMatt Macy again:
1450eda14cbcSMatt Macy 	needagain = progress = B_FALSE;
1451eda14cbcSMatt Macy 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1452eda14cbcSMatt Macy 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1453eda14cbcSMatt Macy 		nvlist_t *fslist, *parent_nv;
14542a58b312SMartin Matuska 		const char *fsname;
1455eda14cbcSMatt Macy 		zfs_handle_t *zhp;
1456eda14cbcSMatt Macy 		int err;
1457eda14cbcSMatt Macy 		uint64_t origin_guid = 0;
1458eda14cbcSMatt Macy 		uint64_t parent_guid = 0;
1459eda14cbcSMatt Macy 
1460184c1b94SMartin Matuska 		fslist = fnvpair_value_nvlist(fspair);
1461eda14cbcSMatt Macy 		if (nvlist_lookup_boolean(fslist, "sent") == 0)
1462eda14cbcSMatt Macy 			continue;
1463eda14cbcSMatt Macy 
1464184c1b94SMartin Matuska 		fsname = fnvlist_lookup_string(fslist, "name");
1465eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1466eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1467eda14cbcSMatt Macy 		    &parent_guid);
1468eda14cbcSMatt Macy 
1469eda14cbcSMatt Macy 		if (parent_guid != 0) {
1470eda14cbcSMatt Macy 			parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1471eda14cbcSMatt Macy 			if (!nvlist_exists(parent_nv, "sent")) {
1472c03c5b1cSMartin Matuska 				/* Parent has not been sent; skip this one. */
1473eda14cbcSMatt Macy 				needagain = B_TRUE;
1474eda14cbcSMatt Macy 				continue;
1475eda14cbcSMatt Macy 			}
1476eda14cbcSMatt Macy 		}
1477eda14cbcSMatt Macy 
1478eda14cbcSMatt Macy 		if (origin_guid != 0) {
1479eda14cbcSMatt Macy 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1480eda14cbcSMatt Macy 			    origin_guid, NULL);
1481eda14cbcSMatt Macy 			if (origin_nv != NULL &&
1482eda14cbcSMatt Macy 			    !nvlist_exists(origin_nv, "sent")) {
1483eda14cbcSMatt Macy 				/*
1484c03c5b1cSMartin Matuska 				 * Origin has not been sent yet;
1485eda14cbcSMatt Macy 				 * skip this clone.
1486eda14cbcSMatt Macy 				 */
1487eda14cbcSMatt Macy 				needagain = B_TRUE;
1488eda14cbcSMatt Macy 				continue;
1489eda14cbcSMatt Macy 			}
1490eda14cbcSMatt Macy 		}
1491eda14cbcSMatt Macy 
1492eda14cbcSMatt Macy 		zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1493eda14cbcSMatt Macy 		if (zhp == NULL)
1494eda14cbcSMatt Macy 			return (-1);
1495eda14cbcSMatt Macy 		err = dump_filesystem(zhp, sdd);
1496184c1b94SMartin Matuska 		fnvlist_add_boolean(fslist, "sent");
1497eda14cbcSMatt Macy 		progress = B_TRUE;
1498eda14cbcSMatt Macy 		zfs_close(zhp);
1499eda14cbcSMatt Macy 		if (err)
1500eda14cbcSMatt Macy 			return (err);
1501eda14cbcSMatt Macy 	}
1502eda14cbcSMatt Macy 	if (needagain) {
1503eda14cbcSMatt Macy 		assert(progress);
1504eda14cbcSMatt Macy 		goto again;
1505eda14cbcSMatt Macy 	}
1506eda14cbcSMatt Macy 
1507c03c5b1cSMartin Matuska 	/* Clean out the sent flags in case we reuse this fss. */
1508eda14cbcSMatt Macy 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1509eda14cbcSMatt Macy 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1510eda14cbcSMatt Macy 		nvlist_t *fslist;
1511eda14cbcSMatt Macy 
1512184c1b94SMartin Matuska 		fslist = fnvpair_value_nvlist(fspair);
1513eda14cbcSMatt Macy 		(void) nvlist_remove_all(fslist, "sent");
1514eda14cbcSMatt Macy 	}
1515eda14cbcSMatt Macy 
1516eda14cbcSMatt Macy 	return (0);
1517eda14cbcSMatt Macy }
1518eda14cbcSMatt Macy 
1519eda14cbcSMatt Macy nvlist_t *
1520eda14cbcSMatt Macy zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1521eda14cbcSMatt Macy {
1522eda14cbcSMatt Macy 	unsigned int version;
1523eda14cbcSMatt Macy 	int nread, i;
1524eda14cbcSMatt Macy 	unsigned long long checksum, packed_len;
1525eda14cbcSMatt Macy 
1526eda14cbcSMatt Macy 	/*
1527eda14cbcSMatt Macy 	 * Decode token header, which is:
1528eda14cbcSMatt Macy 	 *   <token version>-<checksum of payload>-<uncompressed payload length>
1529eda14cbcSMatt Macy 	 * Note that the only supported token version is 1.
1530eda14cbcSMatt Macy 	 */
1531eda14cbcSMatt Macy 	nread = sscanf(token, "%u-%llx-%llx-",
1532eda14cbcSMatt Macy 	    &version, &checksum, &packed_len);
1533eda14cbcSMatt Macy 	if (nread != 3) {
1534eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1535eda14cbcSMatt Macy 		    "resume token is corrupt (invalid format)"));
1536eda14cbcSMatt Macy 		return (NULL);
1537eda14cbcSMatt Macy 	}
1538eda14cbcSMatt Macy 
1539eda14cbcSMatt Macy 	if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1540eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1541eda14cbcSMatt Macy 		    "resume token is corrupt (invalid version %u)"),
1542eda14cbcSMatt Macy 		    version);
1543eda14cbcSMatt Macy 		return (NULL);
1544eda14cbcSMatt Macy 	}
1545eda14cbcSMatt Macy 
1546c03c5b1cSMartin Matuska 	/* Convert hexadecimal representation to binary. */
1547eda14cbcSMatt Macy 	token = strrchr(token, '-') + 1;
1548eda14cbcSMatt Macy 	int len = strlen(token) / 2;
1549eda14cbcSMatt Macy 	unsigned char *compressed = zfs_alloc(hdl, len);
1550eda14cbcSMatt Macy 	for (i = 0; i < len; i++) {
1551eda14cbcSMatt Macy 		nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1552eda14cbcSMatt Macy 		if (nread != 1) {
1553eda14cbcSMatt Macy 			free(compressed);
1554eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1555eda14cbcSMatt Macy 			    "resume token is corrupt "
1556eda14cbcSMatt Macy 			    "(payload is not hex-encoded)"));
1557eda14cbcSMatt Macy 			return (NULL);
1558eda14cbcSMatt Macy 		}
1559eda14cbcSMatt Macy 	}
1560eda14cbcSMatt Macy 
1561c03c5b1cSMartin Matuska 	/* Verify checksum. */
1562eda14cbcSMatt Macy 	zio_cksum_t cksum;
1563eda14cbcSMatt Macy 	fletcher_4_native_varsize(compressed, len, &cksum);
1564eda14cbcSMatt Macy 	if (cksum.zc_word[0] != checksum) {
1565eda14cbcSMatt Macy 		free(compressed);
1566eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1567eda14cbcSMatt Macy 		    "resume token is corrupt (incorrect checksum)"));
1568eda14cbcSMatt Macy 		return (NULL);
1569eda14cbcSMatt Macy 	}
1570eda14cbcSMatt Macy 
1571c03c5b1cSMartin Matuska 	/* Uncompress. */
1572eda14cbcSMatt Macy 	void *packed = zfs_alloc(hdl, packed_len);
1573eda14cbcSMatt Macy 	uLongf packed_len_long = packed_len;
1574eda14cbcSMatt Macy 	if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1575eda14cbcSMatt Macy 	    packed_len_long != packed_len) {
1576eda14cbcSMatt Macy 		free(packed);
1577eda14cbcSMatt Macy 		free(compressed);
1578eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1579eda14cbcSMatt Macy 		    "resume token is corrupt (decompression failed)"));
1580eda14cbcSMatt Macy 		return (NULL);
1581eda14cbcSMatt Macy 	}
1582eda14cbcSMatt Macy 
1583c03c5b1cSMartin Matuska 	/* Unpack nvlist. */
1584eda14cbcSMatt Macy 	nvlist_t *nv;
1585eda14cbcSMatt Macy 	int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1586eda14cbcSMatt Macy 	free(packed);
1587eda14cbcSMatt Macy 	free(compressed);
1588eda14cbcSMatt Macy 	if (error != 0) {
1589eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1590eda14cbcSMatt Macy 		    "resume token is corrupt (nvlist_unpack failed)"));
1591eda14cbcSMatt Macy 		return (NULL);
1592eda14cbcSMatt Macy 	}
1593eda14cbcSMatt Macy 	return (nv);
1594eda14cbcSMatt Macy }
1595c03c5b1cSMartin Matuska 
1596eda14cbcSMatt Macy static enum lzc_send_flags
1597eda14cbcSMatt Macy lzc_flags_from_sendflags(const sendflags_t *flags)
1598eda14cbcSMatt Macy {
1599eda14cbcSMatt Macy 	enum lzc_send_flags lzc_flags = 0;
1600c03c5b1cSMartin Matuska 
1601eda14cbcSMatt Macy 	if (flags->largeblock)
1602eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1603eda14cbcSMatt Macy 	if (flags->embed_data)
1604eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1605eda14cbcSMatt Macy 	if (flags->compress)
1606eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1607eda14cbcSMatt Macy 	if (flags->raw)
1608eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_RAW;
1609eda14cbcSMatt Macy 	if (flags->saved)
1610eda14cbcSMatt Macy 		lzc_flags |= LZC_SEND_FLAG_SAVED;
1611c03c5b1cSMartin Matuska 
1612eda14cbcSMatt Macy 	return (lzc_flags);
1613eda14cbcSMatt Macy }
1614eda14cbcSMatt Macy 
1615eda14cbcSMatt Macy static int
1616eda14cbcSMatt Macy estimate_size(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
1617eda14cbcSMatt Macy     uint64_t resumeobj, uint64_t resumeoff, uint64_t bytes,
161815f0b8c3SMartin Matuska     const char *redactbook, char *errbuf, uint64_t *sizep)
1619eda14cbcSMatt Macy {
1620eda14cbcSMatt Macy 	uint64_t size;
1621eda14cbcSMatt Macy 	FILE *fout = flags->dryrun ? stdout : stderr;
1622eda14cbcSMatt Macy 	progress_arg_t pa = { 0 };
1623eda14cbcSMatt Macy 	int err = 0;
1624eda14cbcSMatt Macy 	pthread_t ptid;
1625315ee00fSMartin Matuska 	sigset_t oldmask;
1626eda14cbcSMatt Macy 
1627315ee00fSMartin Matuska 	{
1628eda14cbcSMatt Macy 		pa.pa_zhp = zhp;
1629eda14cbcSMatt Macy 		pa.pa_fd = fd;
1630eda14cbcSMatt Macy 		pa.pa_parsable = flags->parsable;
1631eda14cbcSMatt Macy 		pa.pa_estimate = B_TRUE;
1632eda14cbcSMatt Macy 		pa.pa_verbosity = flags->verbosity;
1633eda14cbcSMatt Macy 
1634eda14cbcSMatt Macy 		err = pthread_create(&ptid, NULL,
1635eda14cbcSMatt Macy 		    send_progress_thread, &pa);
1636eda14cbcSMatt Macy 		if (err != 0) {
1637fd45b686SMartin Matuska 			zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
1638eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl,
1639eda14cbcSMatt Macy 			    EZFS_THREADCREATEFAILED, errbuf));
1640eda14cbcSMatt Macy 		}
1641315ee00fSMartin Matuska 		SEND_PROGRESS_THREAD_PARENT_BLOCK(&oldmask);
1642eda14cbcSMatt Macy 	}
1643eda14cbcSMatt Macy 
1644eda14cbcSMatt Macy 	err = lzc_send_space_resume_redacted(zhp->zfs_name, from,
1645eda14cbcSMatt Macy 	    lzc_flags_from_sendflags(flags), resumeobj, resumeoff, bytes,
1646eda14cbcSMatt Macy 	    redactbook, fd, &size);
164715f0b8c3SMartin Matuska 	*sizep = size;
1648eda14cbcSMatt Macy 
1649315ee00fSMartin Matuska 	if (send_progress_thread_exit(zhp->zfs_hdl, ptid, &oldmask))
1650716fd348SMartin Matuska 		return (-1);
1651eda14cbcSMatt Macy 
165215f0b8c3SMartin Matuska 	if (!flags->progress && !flags->parsable)
165315f0b8c3SMartin Matuska 		return (err);
165415f0b8c3SMartin Matuska 
1655eda14cbcSMatt Macy 	if (err != 0) {
1656fd45b686SMartin Matuska 		zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(err));
1657eda14cbcSMatt Macy 		return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
1658eda14cbcSMatt Macy 		    errbuf));
1659eda14cbcSMatt Macy 	}
1660eda14cbcSMatt Macy 	send_print_verbose(fout, zhp->zfs_name, from, size,
1661eda14cbcSMatt Macy 	    flags->parsable);
1662eda14cbcSMatt Macy 
1663eda14cbcSMatt Macy 	if (flags->parsable) {
1664eda14cbcSMatt Macy 		(void) fprintf(fout, "size\t%llu\n", (longlong_t)size);
1665eda14cbcSMatt Macy 	} else {
1666eda14cbcSMatt Macy 		char buf[16];
1667eda14cbcSMatt Macy 		zfs_nicenum(size, buf, sizeof (buf));
1668eda14cbcSMatt Macy 		(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1669eda14cbcSMatt Macy 		    "total estimated size is %s\n"), buf);
1670eda14cbcSMatt Macy 	}
1671eda14cbcSMatt Macy 	return (0);
1672eda14cbcSMatt Macy }
1673eda14cbcSMatt Macy 
1674eda14cbcSMatt Macy static boolean_t
1675eda14cbcSMatt Macy redact_snaps_contains(const uint64_t *snaps, uint64_t num_snaps, uint64_t guid)
1676eda14cbcSMatt Macy {
1677eda14cbcSMatt Macy 	for (int i = 0; i < num_snaps; i++) {
1678eda14cbcSMatt Macy 		if (snaps[i] == guid)
1679eda14cbcSMatt Macy 			return (B_TRUE);
1680eda14cbcSMatt Macy 	}
1681eda14cbcSMatt Macy 	return (B_FALSE);
1682eda14cbcSMatt Macy }
1683eda14cbcSMatt Macy 
1684eda14cbcSMatt Macy static boolean_t
1685eda14cbcSMatt Macy redact_snaps_equal(const uint64_t *snaps1, uint64_t num_snaps1,
1686eda14cbcSMatt Macy     const uint64_t *snaps2, uint64_t num_snaps2)
1687eda14cbcSMatt Macy {
1688eda14cbcSMatt Macy 	if (num_snaps1 != num_snaps2)
1689eda14cbcSMatt Macy 		return (B_FALSE);
1690eda14cbcSMatt Macy 	for (int i = 0; i < num_snaps1; i++) {
1691eda14cbcSMatt Macy 		if (!redact_snaps_contains(snaps2, num_snaps2, snaps1[i]))
1692eda14cbcSMatt Macy 			return (B_FALSE);
1693eda14cbcSMatt Macy 	}
1694eda14cbcSMatt Macy 	return (B_TRUE);
1695eda14cbcSMatt Macy }
1696eda14cbcSMatt Macy 
1697eda14cbcSMatt Macy static int
1698c03c5b1cSMartin Matuska get_bookmarks(const char *path, nvlist_t **bmarksp)
1699eda14cbcSMatt Macy {
1700eda14cbcSMatt Macy 	nvlist_t *props = fnvlist_alloc();
1701c03c5b1cSMartin Matuska 	int error;
1702eda14cbcSMatt Macy 
1703eda14cbcSMatt Macy 	fnvlist_add_boolean(props, "redact_complete");
1704eda14cbcSMatt Macy 	fnvlist_add_boolean(props, zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1705c03c5b1cSMartin Matuska 	error = lzc_get_bookmarks(path, props, bmarksp);
1706184c1b94SMartin Matuska 	fnvlist_free(props);
1707c03c5b1cSMartin Matuska 	return (error);
1708eda14cbcSMatt Macy }
1709c03c5b1cSMartin Matuska 
1710c03c5b1cSMartin Matuska static nvpair_t *
1711c03c5b1cSMartin Matuska find_redact_pair(nvlist_t *bmarks, const uint64_t *redact_snap_guids,
1712c03c5b1cSMartin Matuska     int num_redact_snaps)
1713c03c5b1cSMartin Matuska {
1714eda14cbcSMatt Macy 	nvpair_t *pair;
1715c03c5b1cSMartin Matuska 
1716eda14cbcSMatt Macy 	for (pair = nvlist_next_nvpair(bmarks, NULL); pair;
1717eda14cbcSMatt Macy 	    pair = nvlist_next_nvpair(bmarks, pair)) {
1718eda14cbcSMatt Macy 
1719eda14cbcSMatt Macy 		nvlist_t *bmark = fnvpair_value_nvlist(pair);
1720eda14cbcSMatt Macy 		nvlist_t *vallist = fnvlist_lookup_nvlist(bmark,
1721eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
1722eda14cbcSMatt Macy 		uint_t len = 0;
1723eda14cbcSMatt Macy 		uint64_t *bmarksnaps = fnvlist_lookup_uint64_array(vallist,
1724eda14cbcSMatt Macy 		    ZPROP_VALUE, &len);
1725eda14cbcSMatt Macy 		if (redact_snaps_equal(redact_snap_guids,
1726eda14cbcSMatt Macy 		    num_redact_snaps, bmarksnaps, len)) {
1727eda14cbcSMatt Macy 			break;
1728eda14cbcSMatt Macy 		}
1729eda14cbcSMatt Macy 	}
1730c03c5b1cSMartin Matuska 	return (pair);
1731c03c5b1cSMartin Matuska }
1732c03c5b1cSMartin Matuska 
1733c03c5b1cSMartin Matuska static boolean_t
1734c03c5b1cSMartin Matuska get_redact_complete(nvpair_t *pair)
1735c03c5b1cSMartin Matuska {
1736c03c5b1cSMartin Matuska 	nvlist_t *bmark = fnvpair_value_nvlist(pair);
1737c03c5b1cSMartin Matuska 	nvlist_t *vallist = fnvlist_lookup_nvlist(bmark, "redact_complete");
1738c03c5b1cSMartin Matuska 	boolean_t complete = fnvlist_lookup_boolean_value(vallist,
1739c03c5b1cSMartin Matuska 	    ZPROP_VALUE);
1740c03c5b1cSMartin Matuska 
1741c03c5b1cSMartin Matuska 	return (complete);
1742c03c5b1cSMartin Matuska }
1743c03c5b1cSMartin Matuska 
1744c03c5b1cSMartin Matuska /*
1745c03c5b1cSMartin Matuska  * Check that the list of redaction snapshots in the bookmark matches the send
1746c03c5b1cSMartin Matuska  * we're resuming, and return whether or not it's complete.
1747c03c5b1cSMartin Matuska  *
1748c03c5b1cSMartin Matuska  * Note that the caller needs to free the contents of *bookname with free() if
1749c03c5b1cSMartin Matuska  * this function returns successfully.
1750c03c5b1cSMartin Matuska  */
1751c03c5b1cSMartin Matuska static int
1752c03c5b1cSMartin Matuska find_redact_book(libzfs_handle_t *hdl, const char *path,
1753c03c5b1cSMartin Matuska     const uint64_t *redact_snap_guids, int num_redact_snaps,
1754c03c5b1cSMartin Matuska     char **bookname)
1755c03c5b1cSMartin Matuska {
17561f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
1757c03c5b1cSMartin Matuska 	nvlist_t *bmarks;
1758c03c5b1cSMartin Matuska 
1759c03c5b1cSMartin Matuska 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1760c03c5b1cSMartin Matuska 	    "cannot resume send"));
1761c03c5b1cSMartin Matuska 
1762c03c5b1cSMartin Matuska 	int error = get_bookmarks(path, &bmarks);
1763c03c5b1cSMartin Matuska 	if (error != 0) {
1764c03c5b1cSMartin Matuska 		if (error == ESRCH) {
1765c03c5b1cSMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1766c03c5b1cSMartin Matuska 			    "nonexistent redaction bookmark provided"));
1767c03c5b1cSMartin Matuska 		} else if (error == ENOENT) {
1768c03c5b1cSMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1769c03c5b1cSMartin Matuska 			    "dataset to be sent no longer exists"));
1770c03c5b1cSMartin Matuska 		} else {
1771c03c5b1cSMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1772fd45b686SMartin Matuska 			    "unknown error: %s"), zfs_strerror(error));
1773c03c5b1cSMartin Matuska 		}
1774c03c5b1cSMartin Matuska 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1775c03c5b1cSMartin Matuska 	}
1776c03c5b1cSMartin Matuska 	nvpair_t *pair = find_redact_pair(bmarks, redact_snap_guids,
1777c03c5b1cSMartin Matuska 	    num_redact_snaps);
1778eda14cbcSMatt Macy 	if (pair == NULL)  {
1779eda14cbcSMatt Macy 		fnvlist_free(bmarks);
1780eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1781eda14cbcSMatt Macy 		    "no appropriate redaction bookmark exists"));
1782eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1783eda14cbcSMatt Macy 	}
1784c03c5b1cSMartin Matuska 	boolean_t complete = get_redact_complete(pair);
1785eda14cbcSMatt Macy 	if (!complete) {
1786eda14cbcSMatt Macy 		fnvlist_free(bmarks);
1787eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1788eda14cbcSMatt Macy 		    "incomplete redaction bookmark provided"));
1789eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
1790eda14cbcSMatt Macy 	}
1791c03c5b1cSMartin Matuska 	*bookname = strndup(nvpair_name(pair), ZFS_MAX_DATASET_NAME_LEN);
1792eda14cbcSMatt Macy 	ASSERT3P(*bookname, !=, NULL);
1793eda14cbcSMatt Macy 	fnvlist_free(bmarks);
1794eda14cbcSMatt Macy 	return (0);
1795eda14cbcSMatt Macy }
1796eda14cbcSMatt Macy 
1797c03c5b1cSMartin Matuska static enum lzc_send_flags
1798c03c5b1cSMartin Matuska lzc_flags_from_resume_nvl(nvlist_t *resume_nvl)
1799c03c5b1cSMartin Matuska {
1800c03c5b1cSMartin Matuska 	enum lzc_send_flags lzc_flags = 0;
1801c03c5b1cSMartin Matuska 
1802c03c5b1cSMartin Matuska 	if (nvlist_exists(resume_nvl, "largeblockok"))
1803c03c5b1cSMartin Matuska 		lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1804c03c5b1cSMartin Matuska 	if (nvlist_exists(resume_nvl, "embedok"))
1805c03c5b1cSMartin Matuska 		lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1806c03c5b1cSMartin Matuska 	if (nvlist_exists(resume_nvl, "compressok"))
1807c03c5b1cSMartin Matuska 		lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1808c03c5b1cSMartin Matuska 	if (nvlist_exists(resume_nvl, "rawok"))
1809c03c5b1cSMartin Matuska 		lzc_flags |= LZC_SEND_FLAG_RAW;
1810c03c5b1cSMartin Matuska 	if (nvlist_exists(resume_nvl, "savedok"))
1811c03c5b1cSMartin Matuska 		lzc_flags |= LZC_SEND_FLAG_SAVED;
1812c03c5b1cSMartin Matuska 
1813c03c5b1cSMartin Matuska 	return (lzc_flags);
1814c03c5b1cSMartin Matuska }
1815c03c5b1cSMartin Matuska 
1816eda14cbcSMatt Macy static int
1817c03c5b1cSMartin Matuska zfs_send_resume_impl_cb_impl(libzfs_handle_t *hdl, sendflags_t *flags,
1818c03c5b1cSMartin Matuska     int outfd, nvlist_t *resume_nvl)
1819eda14cbcSMatt Macy {
18201f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
18212a58b312SMartin Matuska 	const char *toname;
18222a58b312SMartin Matuska 	const char *fromname = NULL;
1823eda14cbcSMatt Macy 	uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1824eda14cbcSMatt Macy 	zfs_handle_t *zhp;
1825eda14cbcSMatt Macy 	int error = 0;
1826eda14cbcSMatt Macy 	char name[ZFS_MAX_DATASET_NAME_LEN];
1827eda14cbcSMatt Macy 	FILE *fout = (flags->verbosity > 0 && flags->dryrun) ? stdout : stderr;
1828eda14cbcSMatt Macy 	uint64_t *redact_snap_guids = NULL;
1829eda14cbcSMatt Macy 	int num_redact_snaps = 0;
1830eda14cbcSMatt Macy 	char *redact_book = NULL;
183115f0b8c3SMartin Matuska 	uint64_t size = 0;
1832eda14cbcSMatt Macy 
1833eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1834eda14cbcSMatt Macy 	    "cannot resume send"));
1835eda14cbcSMatt Macy 
1836eda14cbcSMatt Macy 	if (flags->verbosity != 0) {
1837eda14cbcSMatt Macy 		(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1838eda14cbcSMatt Macy 		    "resume token contents:\n"));
1839eda14cbcSMatt Macy 		nvlist_print(fout, resume_nvl);
1840eda14cbcSMatt Macy 	}
1841eda14cbcSMatt Macy 
1842eda14cbcSMatt Macy 	if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1843eda14cbcSMatt Macy 	    nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1844eda14cbcSMatt Macy 	    nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1845eda14cbcSMatt Macy 	    nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1846eda14cbcSMatt Macy 	    nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1847eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1848eda14cbcSMatt Macy 		    "resume token is corrupt"));
1849eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_FAULT, errbuf));
1850eda14cbcSMatt Macy 	}
1851eda14cbcSMatt Macy 	fromguid = 0;
1852eda14cbcSMatt Macy 	(void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1853eda14cbcSMatt Macy 
1854eda14cbcSMatt Macy 	if (flags->saved) {
1855be181ee2SMartin Matuska 		(void) strlcpy(name, toname, sizeof (name));
1856eda14cbcSMatt Macy 	} else {
1857eda14cbcSMatt Macy 		error = guid_to_name(hdl, toname, toguid, B_FALSE, name);
1858eda14cbcSMatt Macy 		if (error != 0) {
1859eda14cbcSMatt Macy 			if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1860eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1861eda14cbcSMatt Macy 				    "'%s' is no longer the same snapshot "
1862eda14cbcSMatt Macy 				    "used in the initial send"), toname);
1863eda14cbcSMatt Macy 			} else {
1864eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1865eda14cbcSMatt Macy 				    "'%s' used in the initial send no "
1866eda14cbcSMatt Macy 				    "longer exists"), toname);
1867eda14cbcSMatt Macy 			}
1868eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1869eda14cbcSMatt Macy 		}
1870eda14cbcSMatt Macy 	}
1871eda14cbcSMatt Macy 
1872eda14cbcSMatt Macy 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1873eda14cbcSMatt Macy 	if (zhp == NULL) {
1874eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1875eda14cbcSMatt Macy 		    "unable to access '%s'"), name);
1876eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1877eda14cbcSMatt Macy 	}
1878eda14cbcSMatt Macy 
1879eda14cbcSMatt Macy 	if (nvlist_lookup_uint64_array(resume_nvl, "book_redact_snaps",
1880eda14cbcSMatt Macy 	    &redact_snap_guids, (uint_t *)&num_redact_snaps) != 0) {
1881eda14cbcSMatt Macy 		num_redact_snaps = -1;
1882eda14cbcSMatt Macy 	}
1883eda14cbcSMatt Macy 
1884eda14cbcSMatt Macy 	if (fromguid != 0) {
1885eda14cbcSMatt Macy 		if (guid_to_name_redact_snaps(hdl, toname, fromguid, B_TRUE,
1886eda14cbcSMatt Macy 		    redact_snap_guids, num_redact_snaps, name) != 0) {
1887eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1888eda14cbcSMatt Macy 			    "incremental source %#llx no longer exists"),
1889eda14cbcSMatt Macy 			    (longlong_t)fromguid);
1890eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1891eda14cbcSMatt Macy 		}
1892eda14cbcSMatt Macy 		fromname = name;
1893eda14cbcSMatt Macy 	}
1894eda14cbcSMatt Macy 
1895eda14cbcSMatt Macy 	redact_snap_guids = NULL;
1896eda14cbcSMatt Macy 
1897eda14cbcSMatt Macy 	if (nvlist_lookup_uint64_array(resume_nvl,
1898eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS), &redact_snap_guids,
1899eda14cbcSMatt Macy 	    (uint_t *)&num_redact_snaps) == 0) {
1900eda14cbcSMatt Macy 		char path[ZFS_MAX_DATASET_NAME_LEN];
1901eda14cbcSMatt Macy 
1902eda14cbcSMatt Macy 		(void) strlcpy(path, toname, sizeof (path));
1903eda14cbcSMatt Macy 		char *at = strchr(path, '@');
1904eda14cbcSMatt Macy 		ASSERT3P(at, !=, NULL);
1905eda14cbcSMatt Macy 
1906eda14cbcSMatt Macy 		*at = '\0';
1907eda14cbcSMatt Macy 
1908eda14cbcSMatt Macy 		if ((error = find_redact_book(hdl, path, redact_snap_guids,
1909eda14cbcSMatt Macy 		    num_redact_snaps, &redact_book)) != 0) {
1910eda14cbcSMatt Macy 			return (error);
1911eda14cbcSMatt Macy 		}
1912eda14cbcSMatt Macy 	}
1913eda14cbcSMatt Macy 
1914c03c5b1cSMartin Matuska 	enum lzc_send_flags lzc_flags = lzc_flags_from_sendflags(flags) |
1915c03c5b1cSMartin Matuska 	    lzc_flags_from_resume_nvl(resume_nvl);
1916c03c5b1cSMartin Matuska 
191715f0b8c3SMartin Matuska 	if (flags->verbosity != 0 || flags->progressastitle) {
1918eda14cbcSMatt Macy 		/*
1919eda14cbcSMatt Macy 		 * Some of these may have come from the resume token, set them
1920eda14cbcSMatt Macy 		 * here for size estimate purposes.
1921eda14cbcSMatt Macy 		 */
1922eda14cbcSMatt Macy 		sendflags_t tmpflags = *flags;
1923eda14cbcSMatt Macy 		if (lzc_flags & LZC_SEND_FLAG_LARGE_BLOCK)
1924eda14cbcSMatt Macy 			tmpflags.largeblock = B_TRUE;
1925eda14cbcSMatt Macy 		if (lzc_flags & LZC_SEND_FLAG_COMPRESS)
1926eda14cbcSMatt Macy 			tmpflags.compress = B_TRUE;
1927eda14cbcSMatt Macy 		if (lzc_flags & LZC_SEND_FLAG_EMBED_DATA)
1928eda14cbcSMatt Macy 			tmpflags.embed_data = B_TRUE;
192933b8c039SMartin Matuska 		if (lzc_flags & LZC_SEND_FLAG_RAW)
193033b8c039SMartin Matuska 			tmpflags.raw = B_TRUE;
193133b8c039SMartin Matuska 		if (lzc_flags & LZC_SEND_FLAG_SAVED)
193233b8c039SMartin Matuska 			tmpflags.saved = B_TRUE;
1933eda14cbcSMatt Macy 		error = estimate_size(zhp, fromname, outfd, &tmpflags,
193415f0b8c3SMartin Matuska 		    resumeobj, resumeoff, bytes, redact_book, errbuf, &size);
1935eda14cbcSMatt Macy 	}
1936eda14cbcSMatt Macy 
1937eda14cbcSMatt Macy 	if (!flags->dryrun) {
1938eda14cbcSMatt Macy 		progress_arg_t pa = { 0 };
1939eda14cbcSMatt Macy 		pthread_t tid;
1940315ee00fSMartin Matuska 		sigset_t oldmask;
1941eda14cbcSMatt Macy 		/*
1942eda14cbcSMatt Macy 		 * If progress reporting is requested, spawn a new thread to
1943eda14cbcSMatt Macy 		 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1944eda14cbcSMatt Macy 		 */
1945315ee00fSMartin Matuska 		{
1946eda14cbcSMatt Macy 			pa.pa_zhp = zhp;
1947eda14cbcSMatt Macy 			pa.pa_fd = outfd;
1948eda14cbcSMatt Macy 			pa.pa_parsable = flags->parsable;
1949eda14cbcSMatt Macy 			pa.pa_estimate = B_FALSE;
1950eda14cbcSMatt Macy 			pa.pa_verbosity = flags->verbosity;
195115f0b8c3SMartin Matuska 			pa.pa_size = size;
195215f0b8c3SMartin Matuska 			pa.pa_astitle = flags->progressastitle;
1953c9539b89SMartin Matuska 			pa.pa_progress = flags->progress;
1954eda14cbcSMatt Macy 
1955eda14cbcSMatt Macy 			error = pthread_create(&tid, NULL,
1956eda14cbcSMatt Macy 			    send_progress_thread, &pa);
1957eda14cbcSMatt Macy 			if (error != 0) {
1958eda14cbcSMatt Macy 				if (redact_book != NULL)
1959eda14cbcSMatt Macy 					free(redact_book);
1960eda14cbcSMatt Macy 				zfs_close(zhp);
1961eda14cbcSMatt Macy 				return (error);
1962eda14cbcSMatt Macy 			}
1963315ee00fSMartin Matuska 			SEND_PROGRESS_THREAD_PARENT_BLOCK(&oldmask);
1964eda14cbcSMatt Macy 		}
1965eda14cbcSMatt Macy 
1966eda14cbcSMatt Macy 		error = lzc_send_resume_redacted(zhp->zfs_name, fromname, outfd,
1967eda14cbcSMatt Macy 		    lzc_flags, resumeobj, resumeoff, redact_book);
1968eda14cbcSMatt Macy 		if (redact_book != NULL)
1969eda14cbcSMatt Macy 			free(redact_book);
1970eda14cbcSMatt Macy 
1971315ee00fSMartin Matuska 		if (send_progress_thread_exit(hdl, tid, &oldmask)) {
197215f0b8c3SMartin Matuska 			zfs_close(zhp);
1973716fd348SMartin Matuska 			return (-1);
197415f0b8c3SMartin Matuska 		}
1975eda14cbcSMatt Macy 
19761f1e2261SMartin Matuska 		char errbuf[ERRBUFLEN];
1977eda14cbcSMatt Macy 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1978eda14cbcSMatt Macy 		    "warning: cannot send '%s'"), zhp->zfs_name);
1979eda14cbcSMatt Macy 
1980eda14cbcSMatt Macy 		zfs_close(zhp);
1981eda14cbcSMatt Macy 
1982eda14cbcSMatt Macy 		switch (error) {
1983eda14cbcSMatt Macy 		case 0:
1984eda14cbcSMatt Macy 			return (0);
1985eda14cbcSMatt Macy 		case EACCES:
1986eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1987eda14cbcSMatt Macy 			    "source key must be loaded"));
1988eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1989eda14cbcSMatt Macy 		case ESRCH:
1990eda14cbcSMatt Macy 			if (lzc_exists(zhp->zfs_name)) {
1991eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1992eda14cbcSMatt Macy 				    "incremental source could not be found"));
1993eda14cbcSMatt Macy 			}
1994eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
1995eda14cbcSMatt Macy 
1996eda14cbcSMatt Macy 		case EXDEV:
1997eda14cbcSMatt Macy 		case ENOENT:
1998eda14cbcSMatt Macy 		case EDQUOT:
1999eda14cbcSMatt Macy 		case EFBIG:
2000eda14cbcSMatt Macy 		case EIO:
2001eda14cbcSMatt Macy 		case ENOLINK:
2002eda14cbcSMatt Macy 		case ENOSPC:
2003eda14cbcSMatt Macy 		case ENOSTR:
2004eda14cbcSMatt Macy 		case ENXIO:
2005eda14cbcSMatt Macy 		case EPIPE:
2006eda14cbcSMatt Macy 		case ERANGE:
2007eda14cbcSMatt Macy 		case EFAULT:
2008eda14cbcSMatt Macy 		case EROFS:
2009fd45b686SMartin Matuska 			zfs_error_aux(hdl, "%s", zfs_strerror(errno));
2010eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2011eda14cbcSMatt Macy 
2012eda14cbcSMatt Macy 		default:
2013eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, errno, errbuf));
2014eda14cbcSMatt Macy 		}
2015eda14cbcSMatt Macy 	} else {
2016eda14cbcSMatt Macy 		if (redact_book != NULL)
2017eda14cbcSMatt Macy 			free(redact_book);
2018eda14cbcSMatt Macy 	}
2019eda14cbcSMatt Macy 
2020eda14cbcSMatt Macy 	zfs_close(zhp);
2021eda14cbcSMatt Macy 
2022eda14cbcSMatt Macy 	return (error);
2023eda14cbcSMatt Macy }
2024eda14cbcSMatt Macy 
2025c03c5b1cSMartin Matuska struct zfs_send_resume_impl {
2026c03c5b1cSMartin Matuska 	libzfs_handle_t *hdl;
2027c03c5b1cSMartin Matuska 	sendflags_t *flags;
2028c03c5b1cSMartin Matuska 	nvlist_t *resume_nvl;
2029c03c5b1cSMartin Matuska };
2030c03c5b1cSMartin Matuska 
2031c03c5b1cSMartin Matuska static int
2032c03c5b1cSMartin Matuska zfs_send_resume_impl_cb(int outfd, void *arg)
2033c03c5b1cSMartin Matuska {
2034c03c5b1cSMartin Matuska 	struct zfs_send_resume_impl *zsri = arg;
2035c03c5b1cSMartin Matuska 	return (zfs_send_resume_impl_cb_impl(zsri->hdl, zsri->flags, outfd,
2036c03c5b1cSMartin Matuska 	    zsri->resume_nvl));
2037c03c5b1cSMartin Matuska }
2038c03c5b1cSMartin Matuska 
2039c03c5b1cSMartin Matuska static int
2040c03c5b1cSMartin Matuska zfs_send_resume_impl(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
2041c03c5b1cSMartin Matuska     nvlist_t *resume_nvl)
2042c03c5b1cSMartin Matuska {
2043c03c5b1cSMartin Matuska 	struct zfs_send_resume_impl zsri = {
2044c03c5b1cSMartin Matuska 		.hdl = hdl,
2045c03c5b1cSMartin Matuska 		.flags = flags,
2046c03c5b1cSMartin Matuska 		.resume_nvl = resume_nvl,
2047c03c5b1cSMartin Matuska 	};
2048c03c5b1cSMartin Matuska 	return (lzc_send_wrapper(zfs_send_resume_impl_cb, outfd, &zsri));
2049c03c5b1cSMartin Matuska }
2050c03c5b1cSMartin Matuska 
2051eda14cbcSMatt Macy int
2052eda14cbcSMatt Macy zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
2053eda14cbcSMatt Macy     const char *resume_token)
2054eda14cbcSMatt Macy {
2055eda14cbcSMatt Macy 	int ret;
20561f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
2057eda14cbcSMatt Macy 	nvlist_t *resume_nvl;
2058eda14cbcSMatt Macy 
2059eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2060eda14cbcSMatt Macy 	    "cannot resume send"));
2061eda14cbcSMatt Macy 
2062eda14cbcSMatt Macy 	resume_nvl = zfs_send_resume_token_to_nvlist(hdl, resume_token);
2063eda14cbcSMatt Macy 	if (resume_nvl == NULL) {
2064eda14cbcSMatt Macy 		/*
2065eda14cbcSMatt Macy 		 * zfs_error_aux has already been set by
2066eda14cbcSMatt Macy 		 * zfs_send_resume_token_to_nvlist()
2067eda14cbcSMatt Macy 		 */
2068eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_FAULT, errbuf));
2069eda14cbcSMatt Macy 	}
2070eda14cbcSMatt Macy 
2071eda14cbcSMatt Macy 	ret = zfs_send_resume_impl(hdl, flags, outfd, resume_nvl);
2072184c1b94SMartin Matuska 	fnvlist_free(resume_nvl);
2073eda14cbcSMatt Macy 
2074eda14cbcSMatt Macy 	return (ret);
2075eda14cbcSMatt Macy }
2076eda14cbcSMatt Macy 
2077eda14cbcSMatt Macy int
2078eda14cbcSMatt Macy zfs_send_saved(zfs_handle_t *zhp, sendflags_t *flags, int outfd,
2079eda14cbcSMatt Macy     const char *resume_token)
2080eda14cbcSMatt Macy {
2081eda14cbcSMatt Macy 	int ret;
2082eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2083eda14cbcSMatt Macy 	nvlist_t *saved_nvl = NULL, *resume_nvl = NULL;
2084eda14cbcSMatt Macy 	uint64_t saved_guid = 0, resume_guid = 0;
2085eda14cbcSMatt Macy 	uint64_t obj = 0, off = 0, bytes = 0;
2086eda14cbcSMatt Macy 	char token_buf[ZFS_MAXPROPLEN];
20871f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
2088eda14cbcSMatt Macy 
2089eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2090eda14cbcSMatt Macy 	    "saved send failed"));
2091eda14cbcSMatt Macy 
2092eda14cbcSMatt Macy 	ret = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
2093eda14cbcSMatt Macy 	    token_buf, sizeof (token_buf), NULL, NULL, 0, B_TRUE);
2094eda14cbcSMatt Macy 	if (ret != 0)
2095eda14cbcSMatt Macy 		goto out;
2096eda14cbcSMatt Macy 
2097eda14cbcSMatt Macy 	saved_nvl = zfs_send_resume_token_to_nvlist(hdl, token_buf);
2098eda14cbcSMatt Macy 	if (saved_nvl == NULL) {
2099eda14cbcSMatt Macy 		/*
2100eda14cbcSMatt Macy 		 * zfs_error_aux has already been set by
2101eda14cbcSMatt Macy 		 * zfs_send_resume_token_to_nvlist()
2102eda14cbcSMatt Macy 		 */
2103eda14cbcSMatt Macy 		ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2104eda14cbcSMatt Macy 		goto out;
2105eda14cbcSMatt Macy 	}
2106eda14cbcSMatt Macy 
2107eda14cbcSMatt Macy 	/*
2108eda14cbcSMatt Macy 	 * If a resume token is provided we use the object and offset
2109eda14cbcSMatt Macy 	 * from that instead of the default, which starts from the
2110eda14cbcSMatt Macy 	 * beginning.
2111eda14cbcSMatt Macy 	 */
2112eda14cbcSMatt Macy 	if (resume_token != NULL) {
2113eda14cbcSMatt Macy 		resume_nvl = zfs_send_resume_token_to_nvlist(hdl,
2114eda14cbcSMatt Macy 		    resume_token);
2115eda14cbcSMatt Macy 		if (resume_nvl == NULL) {
2116eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2117eda14cbcSMatt Macy 			goto out;
2118eda14cbcSMatt Macy 		}
2119eda14cbcSMatt Macy 
2120eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(resume_nvl, "object", &obj) != 0 ||
2121eda14cbcSMatt Macy 		    nvlist_lookup_uint64(resume_nvl, "offset", &off) != 0 ||
2122eda14cbcSMatt Macy 		    nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
2123eda14cbcSMatt Macy 		    nvlist_lookup_uint64(resume_nvl, "toguid",
2124eda14cbcSMatt Macy 		    &resume_guid) != 0) {
2125eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2126eda14cbcSMatt Macy 			    "provided resume token is corrupt"));
2127eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2128eda14cbcSMatt Macy 			goto out;
2129eda14cbcSMatt Macy 		}
2130eda14cbcSMatt Macy 
2131eda14cbcSMatt Macy 		if (nvlist_lookup_uint64(saved_nvl, "toguid",
2132eda14cbcSMatt Macy 		    &saved_guid)) {
2133eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2134eda14cbcSMatt Macy 			    "dataset's resume token is corrupt"));
2135eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_FAULT, errbuf);
2136eda14cbcSMatt Macy 			goto out;
2137eda14cbcSMatt Macy 		}
2138eda14cbcSMatt Macy 
2139eda14cbcSMatt Macy 		if (resume_guid != saved_guid) {
2140eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2141eda14cbcSMatt Macy 			    "provided resume token does not match dataset"));
2142eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADBACKUP, errbuf);
2143eda14cbcSMatt Macy 			goto out;
2144eda14cbcSMatt Macy 		}
2145eda14cbcSMatt Macy 	}
2146eda14cbcSMatt Macy 
2147eda14cbcSMatt Macy 	(void) nvlist_remove_all(saved_nvl, "object");
2148eda14cbcSMatt Macy 	fnvlist_add_uint64(saved_nvl, "object", obj);
2149eda14cbcSMatt Macy 
2150eda14cbcSMatt Macy 	(void) nvlist_remove_all(saved_nvl, "offset");
2151eda14cbcSMatt Macy 	fnvlist_add_uint64(saved_nvl, "offset", off);
2152eda14cbcSMatt Macy 
2153eda14cbcSMatt Macy 	(void) nvlist_remove_all(saved_nvl, "bytes");
2154eda14cbcSMatt Macy 	fnvlist_add_uint64(saved_nvl, "bytes", bytes);
2155eda14cbcSMatt Macy 
2156eda14cbcSMatt Macy 	(void) nvlist_remove_all(saved_nvl, "toname");
2157eda14cbcSMatt Macy 	fnvlist_add_string(saved_nvl, "toname", zhp->zfs_name);
2158eda14cbcSMatt Macy 
2159eda14cbcSMatt Macy 	ret = zfs_send_resume_impl(hdl, flags, outfd, saved_nvl);
2160eda14cbcSMatt Macy 
2161eda14cbcSMatt Macy out:
2162184c1b94SMartin Matuska 	fnvlist_free(saved_nvl);
2163184c1b94SMartin Matuska 	fnvlist_free(resume_nvl);
2164eda14cbcSMatt Macy 	return (ret);
2165eda14cbcSMatt Macy }
2166eda14cbcSMatt Macy 
2167eda14cbcSMatt Macy /*
2168eda14cbcSMatt Macy  * This function informs the target system that the recursive send is complete.
2169eda14cbcSMatt Macy  * The record is also expected in the case of a send -p.
2170eda14cbcSMatt Macy  */
2171eda14cbcSMatt Macy static int
2172eda14cbcSMatt Macy send_conclusion_record(int fd, zio_cksum_t *zc)
2173eda14cbcSMatt Macy {
2174aca928a5SMartin Matuska 	dmu_replay_record_t drr;
2175aca928a5SMartin Matuska 	memset(&drr, 0, sizeof (dmu_replay_record_t));
2176eda14cbcSMatt Macy 	drr.drr_type = DRR_END;
2177eda14cbcSMatt Macy 	if (zc != NULL)
2178eda14cbcSMatt Macy 		drr.drr_u.drr_end.drr_checksum = *zc;
2179eda14cbcSMatt Macy 	if (write(fd, &drr, sizeof (drr)) == -1) {
2180eda14cbcSMatt Macy 		return (errno);
2181eda14cbcSMatt Macy 	}
2182eda14cbcSMatt Macy 	return (0);
2183eda14cbcSMatt Macy }
2184eda14cbcSMatt Macy 
2185eda14cbcSMatt Macy /*
2186eda14cbcSMatt Macy  * This function is responsible for sending the records that contain the
2187eda14cbcSMatt Macy  * necessary information for the target system's libzfs to be able to set the
2188eda14cbcSMatt Macy  * properties of the filesystem being received, or to be able to prepare for
2189eda14cbcSMatt Macy  * a recursive receive.
2190eda14cbcSMatt Macy  *
2191eda14cbcSMatt Macy  * The "zhp" argument is the handle of the snapshot we are sending
2192eda14cbcSMatt Macy  * (the "tosnap").  The "from" argument is the short snapshot name (the part
2193eda14cbcSMatt Macy  * after the @) of the incremental source.
2194eda14cbcSMatt Macy  */
2195eda14cbcSMatt Macy static int
2196eda14cbcSMatt Macy send_prelim_records(zfs_handle_t *zhp, const char *from, int fd,
2197eda14cbcSMatt Macy     boolean_t gather_props, boolean_t recursive, boolean_t verbose,
219816038816SMartin Matuska     boolean_t dryrun, boolean_t raw, boolean_t replicate, boolean_t skipmissing,
219916038816SMartin Matuska     boolean_t backup, boolean_t holds, boolean_t props, boolean_t doall,
2200eda14cbcSMatt Macy     nvlist_t **fssp, avl_tree_t **fsavlp)
2201eda14cbcSMatt Macy {
2202eda14cbcSMatt Macy 	int err = 0;
2203eda14cbcSMatt Macy 	char *packbuf = NULL;
2204eda14cbcSMatt Macy 	size_t buflen = 0;
2205eda14cbcSMatt Macy 	zio_cksum_t zc = { {0} };
2206eda14cbcSMatt Macy 	int featureflags = 0;
2207eda14cbcSMatt Macy 	/* name of filesystem/volume that contains snapshot we are sending */
2208eda14cbcSMatt Macy 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
2209eda14cbcSMatt Macy 	/* short name of snap we are sending */
2210a0b956f5SMartin Matuska 	const char *tosnap = "";
2211eda14cbcSMatt Macy 
22121f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
2213eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2214eda14cbcSMatt Macy 	    "warning: cannot send '%s'"), zhp->zfs_name);
2215eda14cbcSMatt Macy 	if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM && zfs_prop_get_int(zhp,
2216eda14cbcSMatt Macy 	    ZFS_PROP_VERSION) >= ZPL_VERSION_SA) {
2217eda14cbcSMatt Macy 		featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
2218eda14cbcSMatt Macy 	}
2219eda14cbcSMatt Macy 
2220eda14cbcSMatt Macy 	if (holds)
2221eda14cbcSMatt Macy 		featureflags |= DMU_BACKUP_FEATURE_HOLDS;
2222eda14cbcSMatt Macy 
2223eda14cbcSMatt Macy 	(void) strlcpy(tofs, zhp->zfs_name, ZFS_MAX_DATASET_NAME_LEN);
2224eda14cbcSMatt Macy 	char *at = strchr(tofs, '@');
2225eda14cbcSMatt Macy 	if (at != NULL) {
2226eda14cbcSMatt Macy 		*at = '\0';
2227eda14cbcSMatt Macy 		tosnap = at + 1;
2228eda14cbcSMatt Macy 	}
2229eda14cbcSMatt Macy 
2230eda14cbcSMatt Macy 	if (gather_props) {
2231eda14cbcSMatt Macy 		nvlist_t *hdrnv = fnvlist_alloc();
2232eda14cbcSMatt Macy 		nvlist_t *fss = NULL;
2233eda14cbcSMatt Macy 
2234eda14cbcSMatt Macy 		if (from != NULL)
2235eda14cbcSMatt Macy 			fnvlist_add_string(hdrnv, "fromsnap", from);
2236eda14cbcSMatt Macy 		fnvlist_add_string(hdrnv, "tosnap", tosnap);
2237eda14cbcSMatt Macy 		if (!recursive)
2238eda14cbcSMatt Macy 			fnvlist_add_boolean(hdrnv, "not_recursive");
2239eda14cbcSMatt Macy 
2240eda14cbcSMatt Macy 		if (raw) {
2241184c1b94SMartin Matuska 			fnvlist_add_boolean(hdrnv, "raw");
2242eda14cbcSMatt Macy 		}
2243eda14cbcSMatt Macy 
2244dbd5678dSMartin Matuska 		if (gather_nvlist(zhp->zfs_hdl, tofs,
224516038816SMartin Matuska 		    from, tosnap, recursive, raw, doall, replicate, skipmissing,
2246dbd5678dSMartin Matuska 		    verbose, backup, holds, props, &fss, fsavlp) != 0) {
2247eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2248eda14cbcSMatt Macy 			    errbuf));
2249eda14cbcSMatt Macy 		}
2250ac0bf12eSMatt Macy 		/*
2251ac0bf12eSMatt Macy 		 * Do not allow the size of the properties list to exceed
2252ac0bf12eSMatt Macy 		 * the limit
2253ac0bf12eSMatt Macy 		 */
2254ac0bf12eSMatt Macy 		if ((fnvlist_size(fss) + fnvlist_size(hdrnv)) >
2255ac0bf12eSMatt Macy 		    zhp->zfs_hdl->libzfs_max_nvlist) {
2256ac0bf12eSMatt Macy 			(void) snprintf(errbuf, sizeof (errbuf),
2257ac0bf12eSMatt Macy 			    dgettext(TEXT_DOMAIN, "warning: cannot send '%s': "
2258ac0bf12eSMatt Macy 			    "the size of the list of snapshots and properties "
2259ac0bf12eSMatt Macy 			    "is too large to be received successfully.\n"
2260ac0bf12eSMatt Macy 			    "Select a smaller number of snapshots to send.\n"),
2261ac0bf12eSMatt Macy 			    zhp->zfs_name);
2262ac0bf12eSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_NOSPC,
2263ac0bf12eSMatt Macy 			    errbuf));
2264ac0bf12eSMatt Macy 		}
2265eda14cbcSMatt Macy 		fnvlist_add_nvlist(hdrnv, "fss", fss);
2266eda14cbcSMatt Macy 		VERIFY0(nvlist_pack(hdrnv, &packbuf, &buflen, NV_ENCODE_XDR,
2267eda14cbcSMatt Macy 		    0));
2268eda14cbcSMatt Macy 		if (fssp != NULL) {
2269eda14cbcSMatt Macy 			*fssp = fss;
2270eda14cbcSMatt Macy 		} else {
2271184c1b94SMartin Matuska 			fnvlist_free(fss);
2272eda14cbcSMatt Macy 		}
2273184c1b94SMartin Matuska 		fnvlist_free(hdrnv);
2274eda14cbcSMatt Macy 	}
2275eda14cbcSMatt Macy 
2276eda14cbcSMatt Macy 	if (!dryrun) {
2277aca928a5SMartin Matuska 		dmu_replay_record_t drr;
2278aca928a5SMartin Matuska 		memset(&drr, 0, sizeof (dmu_replay_record_t));
2279eda14cbcSMatt Macy 		/* write first begin record */
2280eda14cbcSMatt Macy 		drr.drr_type = DRR_BEGIN;
2281eda14cbcSMatt Macy 		drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
2282eda14cbcSMatt Macy 		DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
2283eda14cbcSMatt Macy 		    drr_versioninfo, DMU_COMPOUNDSTREAM);
2284eda14cbcSMatt Macy 		DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
2285eda14cbcSMatt Macy 		    drr_versioninfo, featureflags);
2286eda14cbcSMatt Macy 		if (snprintf(drr.drr_u.drr_begin.drr_toname,
2287eda14cbcSMatt Macy 		    sizeof (drr.drr_u.drr_begin.drr_toname), "%s@%s", tofs,
2288eda14cbcSMatt Macy 		    tosnap) >= sizeof (drr.drr_u.drr_begin.drr_toname)) {
2289eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2290eda14cbcSMatt Macy 			    errbuf));
2291eda14cbcSMatt Macy 		}
2292eda14cbcSMatt Macy 		drr.drr_payloadlen = buflen;
2293eda14cbcSMatt Macy 
2294eda14cbcSMatt Macy 		err = dump_record(&drr, packbuf, buflen, &zc, fd);
2295eda14cbcSMatt Macy 		free(packbuf);
2296eda14cbcSMatt Macy 		if (err != 0) {
2297fd45b686SMartin Matuska 			zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(err));
2298eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2299eda14cbcSMatt Macy 			    errbuf));
2300eda14cbcSMatt Macy 		}
2301eda14cbcSMatt Macy 		err = send_conclusion_record(fd, &zc);
2302eda14cbcSMatt Macy 		if (err != 0) {
2303fd45b686SMartin Matuska 			zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(err));
2304eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl, EZFS_BADBACKUP,
2305eda14cbcSMatt Macy 			    errbuf));
2306eda14cbcSMatt Macy 		}
2307eda14cbcSMatt Macy 	}
2308eda14cbcSMatt Macy 	return (0);
2309eda14cbcSMatt Macy }
2310eda14cbcSMatt Macy 
2311eda14cbcSMatt Macy /*
2312eda14cbcSMatt Macy  * Generate a send stream.  The "zhp" argument is the filesystem/volume
2313eda14cbcSMatt Macy  * that contains the snapshot to send.  The "fromsnap" argument is the
2314eda14cbcSMatt Macy  * short name (the part after the '@') of the snapshot that is the
2315eda14cbcSMatt Macy  * incremental source to send from (if non-NULL).  The "tosnap" argument
2316eda14cbcSMatt Macy  * is the short name of the snapshot to send.
2317eda14cbcSMatt Macy  *
2318eda14cbcSMatt Macy  * The content of the send stream is the snapshot identified by
2319eda14cbcSMatt Macy  * 'tosnap'.  Incremental streams are requested in two ways:
2320eda14cbcSMatt Macy  *     - from the snapshot identified by "fromsnap" (if non-null) or
2321eda14cbcSMatt Macy  *     - from the origin of the dataset identified by zhp, which must
2322eda14cbcSMatt Macy  *	 be a clone.  In this case, "fromsnap" is null and "fromorigin"
2323eda14cbcSMatt Macy  *	 is TRUE.
2324eda14cbcSMatt Macy  *
2325eda14cbcSMatt Macy  * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
2326eda14cbcSMatt Macy  * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
2327eda14cbcSMatt Macy  * if "replicate" is set.  If "doall" is set, dump all the intermediate
2328eda14cbcSMatt Macy  * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
2329eda14cbcSMatt Macy  * case too. If "props" is set, send properties.
2330c03c5b1cSMartin Matuska  *
2331c03c5b1cSMartin Matuska  * Pre-wrapped (cf. lzc_send_wrapper()).
2332eda14cbcSMatt Macy  */
2333c03c5b1cSMartin Matuska static int
2334c03c5b1cSMartin Matuska zfs_send_cb_impl(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2335eda14cbcSMatt Macy     sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2336eda14cbcSMatt Macy     void *cb_arg, nvlist_t **debugnvp)
2337eda14cbcSMatt Macy {
23381f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
2339eda14cbcSMatt Macy 	send_dump_data_t sdd = { 0 };
2340eda14cbcSMatt Macy 	int err = 0;
2341eda14cbcSMatt Macy 	nvlist_t *fss = NULL;
2342eda14cbcSMatt Macy 	avl_tree_t *fsavl = NULL;
2343eda14cbcSMatt Macy 	static uint64_t holdseq;
2344eda14cbcSMatt Macy 	int spa_version;
2345eda14cbcSMatt Macy 	FILE *fout;
2346eda14cbcSMatt Macy 
2347eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2348eda14cbcSMatt Macy 	    "cannot send '%s'"), zhp->zfs_name);
2349eda14cbcSMatt Macy 
2350eda14cbcSMatt Macy 	if (fromsnap && fromsnap[0] == '\0') {
2351eda14cbcSMatt Macy 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
2352eda14cbcSMatt Macy 		    "zero-length incremental source"));
2353eda14cbcSMatt Macy 		return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
2354eda14cbcSMatt Macy 	}
2355eda14cbcSMatt Macy 
23566ba2210eSMartin Matuska 	if (fromsnap) {
23576ba2210eSMartin Matuska 		char full_fromsnap_name[ZFS_MAX_DATASET_NAME_LEN];
23586ba2210eSMartin Matuska 		if (snprintf(full_fromsnap_name, sizeof (full_fromsnap_name),
23596ba2210eSMartin Matuska 		    "%s@%s", zhp->zfs_name, fromsnap) >=
23606ba2210eSMartin Matuska 		    sizeof (full_fromsnap_name)) {
23616ba2210eSMartin Matuska 			err = EINVAL;
23626ba2210eSMartin Matuska 			goto stderr_out;
23636ba2210eSMartin Matuska 		}
23646ba2210eSMartin Matuska 		zfs_handle_t *fromsnapn = zfs_open(zhp->zfs_hdl,
23656ba2210eSMartin Matuska 		    full_fromsnap_name, ZFS_TYPE_SNAPSHOT);
23666ba2210eSMartin Matuska 		if (fromsnapn == NULL) {
23676ba2210eSMartin Matuska 			err = -1;
23686ba2210eSMartin Matuska 			goto err_out;
23696ba2210eSMartin Matuska 		}
23706ba2210eSMartin Matuska 		zfs_close(fromsnapn);
23716ba2210eSMartin Matuska 	}
23726ba2210eSMartin Matuska 
2373eda14cbcSMatt Macy 	if (flags->replicate || flags->doall || flags->props ||
2374eda14cbcSMatt Macy 	    flags->holds || flags->backup) {
2375eda14cbcSMatt Macy 		char full_tosnap_name[ZFS_MAX_DATASET_NAME_LEN];
2376eda14cbcSMatt Macy 		if (snprintf(full_tosnap_name, sizeof (full_tosnap_name),
2377eda14cbcSMatt Macy 		    "%s@%s", zhp->zfs_name, tosnap) >=
2378eda14cbcSMatt Macy 		    sizeof (full_tosnap_name)) {
2379eda14cbcSMatt Macy 			err = EINVAL;
2380eda14cbcSMatt Macy 			goto stderr_out;
2381eda14cbcSMatt Macy 		}
2382eda14cbcSMatt Macy 		zfs_handle_t *tosnap = zfs_open(zhp->zfs_hdl,
2383eda14cbcSMatt Macy 		    full_tosnap_name, ZFS_TYPE_SNAPSHOT);
2384eda14cbcSMatt Macy 		if (tosnap == NULL) {
2385eda14cbcSMatt Macy 			err = -1;
2386eda14cbcSMatt Macy 			goto err_out;
2387eda14cbcSMatt Macy 		}
2388eda14cbcSMatt Macy 		err = send_prelim_records(tosnap, fromsnap, outfd,
2389eda14cbcSMatt Macy 		    flags->replicate || flags->props || flags->holds,
2390eda14cbcSMatt Macy 		    flags->replicate, flags->verbosity > 0, flags->dryrun,
239116038816SMartin Matuska 		    flags->raw, flags->replicate, flags->skipmissing,
239216038816SMartin Matuska 		    flags->backup, flags->holds, flags->props, flags->doall,
239316038816SMartin Matuska 		    &fss, &fsavl);
2394eda14cbcSMatt Macy 		zfs_close(tosnap);
2395eda14cbcSMatt Macy 		if (err != 0)
2396eda14cbcSMatt Macy 			goto err_out;
2397eda14cbcSMatt Macy 	}
2398eda14cbcSMatt Macy 
2399eda14cbcSMatt Macy 	/* dump each stream */
2400eda14cbcSMatt Macy 	sdd.fromsnap = fromsnap;
2401eda14cbcSMatt Macy 	sdd.tosnap = tosnap;
2402eda14cbcSMatt Macy 	sdd.outfd = outfd;
2403eda14cbcSMatt Macy 	sdd.replicate = flags->replicate;
2404eda14cbcSMatt Macy 	sdd.doall = flags->doall;
2405eda14cbcSMatt Macy 	sdd.fromorigin = flags->fromorigin;
2406eda14cbcSMatt Macy 	sdd.fss = fss;
2407eda14cbcSMatt Macy 	sdd.fsavl = fsavl;
2408eda14cbcSMatt Macy 	sdd.verbosity = flags->verbosity;
2409eda14cbcSMatt Macy 	sdd.parsable = flags->parsable;
2410eda14cbcSMatt Macy 	sdd.progress = flags->progress;
241115f0b8c3SMartin Matuska 	sdd.progressastitle = flags->progressastitle;
2412eda14cbcSMatt Macy 	sdd.dryrun = flags->dryrun;
2413eda14cbcSMatt Macy 	sdd.large_block = flags->largeblock;
2414eda14cbcSMatt Macy 	sdd.embed_data = flags->embed_data;
2415eda14cbcSMatt Macy 	sdd.compress = flags->compress;
2416eda14cbcSMatt Macy 	sdd.raw = flags->raw;
2417eda14cbcSMatt Macy 	sdd.holds = flags->holds;
2418eda14cbcSMatt Macy 	sdd.filter_cb = filter_func;
2419eda14cbcSMatt Macy 	sdd.filter_cb_arg = cb_arg;
2420eda14cbcSMatt Macy 	if (debugnvp)
2421eda14cbcSMatt Macy 		sdd.debugnv = *debugnvp;
2422eda14cbcSMatt Macy 	if (sdd.verbosity != 0 && sdd.dryrun)
2423eda14cbcSMatt Macy 		sdd.std_out = B_TRUE;
2424eda14cbcSMatt Macy 	fout = sdd.std_out ? stdout : stderr;
2425eda14cbcSMatt Macy 
2426eda14cbcSMatt Macy 	/*
2427eda14cbcSMatt Macy 	 * Some flags require that we place user holds on the datasets that are
2428eda14cbcSMatt Macy 	 * being sent so they don't get destroyed during the send. We can skip
2429eda14cbcSMatt Macy 	 * this step if the pool is imported read-only since the datasets cannot
2430eda14cbcSMatt Macy 	 * be destroyed.
2431eda14cbcSMatt Macy 	 */
2432eda14cbcSMatt Macy 	if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2433eda14cbcSMatt Macy 	    ZPOOL_PROP_READONLY, NULL) &&
2434eda14cbcSMatt Macy 	    zfs_spa_version(zhp, &spa_version) == 0 &&
2435eda14cbcSMatt Macy 	    spa_version >= SPA_VERSION_USERREFS &&
2436eda14cbcSMatt Macy 	    (flags->doall || flags->replicate)) {
2437eda14cbcSMatt Macy 		++holdseq;
2438eda14cbcSMatt Macy 		(void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2439eda14cbcSMatt Macy 		    ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
244016038816SMartin Matuska 		sdd.cleanup_fd = open(ZFS_DEV, O_RDWR | O_CLOEXEC);
2441eda14cbcSMatt Macy 		if (sdd.cleanup_fd < 0) {
2442eda14cbcSMatt Macy 			err = errno;
2443eda14cbcSMatt Macy 			goto stderr_out;
2444eda14cbcSMatt Macy 		}
2445eda14cbcSMatt Macy 		sdd.snapholds = fnvlist_alloc();
2446eda14cbcSMatt Macy 	} else {
2447eda14cbcSMatt Macy 		sdd.cleanup_fd = -1;
2448eda14cbcSMatt Macy 		sdd.snapholds = NULL;
2449eda14cbcSMatt Macy 	}
2450eda14cbcSMatt Macy 
2451eda14cbcSMatt Macy 	if (flags->verbosity != 0 || sdd.snapholds != NULL) {
2452eda14cbcSMatt Macy 		/*
2453eda14cbcSMatt Macy 		 * Do a verbose no-op dry run to get all the verbose output
2454eda14cbcSMatt Macy 		 * or to gather snapshot hold's before generating any data,
2455eda14cbcSMatt Macy 		 * then do a non-verbose real run to generate the streams.
2456eda14cbcSMatt Macy 		 */
2457eda14cbcSMatt Macy 		sdd.dryrun = B_TRUE;
2458eda14cbcSMatt Macy 		err = dump_filesystems(zhp, &sdd);
2459eda14cbcSMatt Macy 
2460eda14cbcSMatt Macy 		if (err != 0)
2461eda14cbcSMatt Macy 			goto stderr_out;
2462eda14cbcSMatt Macy 
2463eda14cbcSMatt Macy 		if (flags->verbosity != 0) {
2464eda14cbcSMatt Macy 			if (flags->parsable) {
2465eda14cbcSMatt Macy 				(void) fprintf(fout, "size\t%llu\n",
2466eda14cbcSMatt Macy 				    (longlong_t)sdd.size);
2467eda14cbcSMatt Macy 			} else {
2468eda14cbcSMatt Macy 				char buf[16];
2469eda14cbcSMatt Macy 				zfs_nicebytes(sdd.size, buf, sizeof (buf));
2470eda14cbcSMatt Macy 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
2471eda14cbcSMatt Macy 				    "total estimated size is %s\n"), buf);
2472eda14cbcSMatt Macy 			}
2473eda14cbcSMatt Macy 		}
2474eda14cbcSMatt Macy 
2475eda14cbcSMatt Macy 		/* Ensure no snaps found is treated as an error. */
2476eda14cbcSMatt Macy 		if (!sdd.seento) {
2477eda14cbcSMatt Macy 			err = ENOENT;
2478eda14cbcSMatt Macy 			goto err_out;
2479eda14cbcSMatt Macy 		}
2480eda14cbcSMatt Macy 
2481eda14cbcSMatt Macy 		/* Skip the second run if dryrun was requested. */
2482eda14cbcSMatt Macy 		if (flags->dryrun)
2483eda14cbcSMatt Macy 			goto err_out;
2484eda14cbcSMatt Macy 
2485eda14cbcSMatt Macy 		if (sdd.snapholds != NULL) {
2486eda14cbcSMatt Macy 			err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2487eda14cbcSMatt Macy 			if (err != 0)
2488eda14cbcSMatt Macy 				goto stderr_out;
2489eda14cbcSMatt Macy 
2490eda14cbcSMatt Macy 			fnvlist_free(sdd.snapholds);
2491eda14cbcSMatt Macy 			sdd.snapholds = NULL;
2492eda14cbcSMatt Macy 		}
2493eda14cbcSMatt Macy 
2494eda14cbcSMatt Macy 		sdd.dryrun = B_FALSE;
2495eda14cbcSMatt Macy 		sdd.verbosity = 0;
2496eda14cbcSMatt Macy 	}
2497eda14cbcSMatt Macy 
2498eda14cbcSMatt Macy 	err = dump_filesystems(zhp, &sdd);
2499eda14cbcSMatt Macy 	fsavl_destroy(fsavl);
2500184c1b94SMartin Matuska 	fnvlist_free(fss);
2501eda14cbcSMatt Macy 
2502eda14cbcSMatt Macy 	/* Ensure no snaps found is treated as an error. */
2503eda14cbcSMatt Macy 	if (err == 0 && !sdd.seento)
2504eda14cbcSMatt Macy 		err = ENOENT;
2505eda14cbcSMatt Macy 
2506eda14cbcSMatt Macy 	if (sdd.cleanup_fd != -1) {
2507eda14cbcSMatt Macy 		VERIFY(0 == close(sdd.cleanup_fd));
2508eda14cbcSMatt Macy 		sdd.cleanup_fd = -1;
2509eda14cbcSMatt Macy 	}
2510eda14cbcSMatt Macy 
2511eda14cbcSMatt Macy 	if (!flags->dryrun && (flags->replicate || flags->doall ||
2512eda14cbcSMatt Macy 	    flags->props || flags->backup || flags->holds)) {
2513eda14cbcSMatt Macy 		/*
2514eda14cbcSMatt Macy 		 * write final end record.  NB: want to do this even if
2515eda14cbcSMatt Macy 		 * there was some error, because it might not be totally
2516eda14cbcSMatt Macy 		 * failed.
2517eda14cbcSMatt Macy 		 */
25181f1e2261SMartin Matuska 		int err2 = send_conclusion_record(outfd, NULL);
25191f1e2261SMartin Matuska 		if (err2 != 0)
25201f1e2261SMartin Matuska 			return (zfs_standard_error(zhp->zfs_hdl, err2, errbuf));
2521eda14cbcSMatt Macy 	}
2522eda14cbcSMatt Macy 
2523eda14cbcSMatt Macy 	return (err || sdd.err);
2524eda14cbcSMatt Macy 
2525eda14cbcSMatt Macy stderr_out:
2526eda14cbcSMatt Macy 	err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2527eda14cbcSMatt Macy err_out:
2528eda14cbcSMatt Macy 	fsavl_destroy(fsavl);
2529184c1b94SMartin Matuska 	fnvlist_free(fss);
2530eda14cbcSMatt Macy 	fnvlist_free(sdd.snapholds);
2531eda14cbcSMatt Macy 
2532eda14cbcSMatt Macy 	if (sdd.cleanup_fd != -1)
2533eda14cbcSMatt Macy 		VERIFY(0 == close(sdd.cleanup_fd));
2534eda14cbcSMatt Macy 	return (err);
2535eda14cbcSMatt Macy }
2536eda14cbcSMatt Macy 
2537c03c5b1cSMartin Matuska struct zfs_send {
2538c03c5b1cSMartin Matuska 	zfs_handle_t *zhp;
2539c03c5b1cSMartin Matuska 	const char *fromsnap;
2540c03c5b1cSMartin Matuska 	const char *tosnap;
2541c03c5b1cSMartin Matuska 	sendflags_t *flags;
2542c03c5b1cSMartin Matuska 	snapfilter_cb_t *filter_func;
2543c03c5b1cSMartin Matuska 	void *cb_arg;
2544c03c5b1cSMartin Matuska 	nvlist_t **debugnvp;
2545c03c5b1cSMartin Matuska };
2546c03c5b1cSMartin Matuska 
2547c03c5b1cSMartin Matuska static int
2548c03c5b1cSMartin Matuska zfs_send_cb(int outfd, void *arg)
2549c03c5b1cSMartin Matuska {
2550c03c5b1cSMartin Matuska 	struct zfs_send *zs = arg;
2551c03c5b1cSMartin Matuska 	return (zfs_send_cb_impl(zs->zhp, zs->fromsnap, zs->tosnap, zs->flags,
2552c03c5b1cSMartin Matuska 	    outfd, zs->filter_func, zs->cb_arg, zs->debugnvp));
2553c03c5b1cSMartin Matuska }
2554c03c5b1cSMartin Matuska 
2555c03c5b1cSMartin Matuska int
2556c03c5b1cSMartin Matuska zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
2557c03c5b1cSMartin Matuska     sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
2558c03c5b1cSMartin Matuska     void *cb_arg, nvlist_t **debugnvp)
2559c03c5b1cSMartin Matuska {
2560c03c5b1cSMartin Matuska 	struct zfs_send arg = {
2561c03c5b1cSMartin Matuska 		.zhp = zhp,
2562c03c5b1cSMartin Matuska 		.fromsnap = fromsnap,
2563c03c5b1cSMartin Matuska 		.tosnap = tosnap,
2564c03c5b1cSMartin Matuska 		.flags = flags,
2565c03c5b1cSMartin Matuska 		.filter_func = filter_func,
2566c03c5b1cSMartin Matuska 		.cb_arg = cb_arg,
2567c03c5b1cSMartin Matuska 		.debugnvp = debugnvp,
2568c03c5b1cSMartin Matuska 	};
2569c03c5b1cSMartin Matuska 	return (lzc_send_wrapper(zfs_send_cb, outfd, &arg));
2570c03c5b1cSMartin Matuska }
2571c03c5b1cSMartin Matuska 
2572c03c5b1cSMartin Matuska 
2573eda14cbcSMatt Macy static zfs_handle_t *
2574eda14cbcSMatt Macy name_to_dir_handle(libzfs_handle_t *hdl, const char *snapname)
2575eda14cbcSMatt Macy {
2576eda14cbcSMatt Macy 	char dirname[ZFS_MAX_DATASET_NAME_LEN];
2577eda14cbcSMatt Macy 	(void) strlcpy(dirname, snapname, ZFS_MAX_DATASET_NAME_LEN);
2578eda14cbcSMatt Macy 	char *c = strchr(dirname, '@');
2579eda14cbcSMatt Macy 	if (c != NULL)
2580eda14cbcSMatt Macy 		*c = '\0';
2581eda14cbcSMatt Macy 	return (zfs_open(hdl, dirname, ZFS_TYPE_DATASET));
2582eda14cbcSMatt Macy }
2583eda14cbcSMatt Macy 
2584eda14cbcSMatt Macy /*
2585eda14cbcSMatt Macy  * Returns B_TRUE if earlier is an earlier snapshot in later's timeline; either
2586eda14cbcSMatt Macy  * an earlier snapshot in the same filesystem, or a snapshot before later's
2587eda14cbcSMatt Macy  * origin, or it's origin's origin, etc.
2588eda14cbcSMatt Macy  */
2589eda14cbcSMatt Macy static boolean_t
2590eda14cbcSMatt Macy snapshot_is_before(zfs_handle_t *earlier, zfs_handle_t *later)
2591eda14cbcSMatt Macy {
2592eda14cbcSMatt Macy 	boolean_t ret;
2593eda14cbcSMatt Macy 	uint64_t later_txg =
2594eda14cbcSMatt Macy 	    (later->zfs_type == ZFS_TYPE_FILESYSTEM ||
2595eda14cbcSMatt Macy 	    later->zfs_type == ZFS_TYPE_VOLUME ?
2596eda14cbcSMatt Macy 	    UINT64_MAX : zfs_prop_get_int(later, ZFS_PROP_CREATETXG));
2597eda14cbcSMatt Macy 	uint64_t earlier_txg = zfs_prop_get_int(earlier, ZFS_PROP_CREATETXG);
2598eda14cbcSMatt Macy 
2599eda14cbcSMatt Macy 	if (earlier_txg >= later_txg)
2600eda14cbcSMatt Macy 		return (B_FALSE);
2601eda14cbcSMatt Macy 
2602eda14cbcSMatt Macy 	zfs_handle_t *earlier_dir = name_to_dir_handle(earlier->zfs_hdl,
2603eda14cbcSMatt Macy 	    earlier->zfs_name);
2604eda14cbcSMatt Macy 	zfs_handle_t *later_dir = name_to_dir_handle(later->zfs_hdl,
2605eda14cbcSMatt Macy 	    later->zfs_name);
2606eda14cbcSMatt Macy 
2607eda14cbcSMatt Macy 	if (strcmp(earlier_dir->zfs_name, later_dir->zfs_name) == 0) {
2608eda14cbcSMatt Macy 		zfs_close(earlier_dir);
2609eda14cbcSMatt Macy 		zfs_close(later_dir);
2610eda14cbcSMatt Macy 		return (B_TRUE);
2611eda14cbcSMatt Macy 	}
2612eda14cbcSMatt Macy 
2613eda14cbcSMatt Macy 	char clonename[ZFS_MAX_DATASET_NAME_LEN];
2614eda14cbcSMatt Macy 	if (zfs_prop_get(later_dir, ZFS_PROP_ORIGIN, clonename,
2615eda14cbcSMatt Macy 	    ZFS_MAX_DATASET_NAME_LEN, NULL, NULL, 0, B_TRUE) != 0) {
2616eda14cbcSMatt Macy 		zfs_close(earlier_dir);
2617eda14cbcSMatt Macy 		zfs_close(later_dir);
2618eda14cbcSMatt Macy 		return (B_FALSE);
2619eda14cbcSMatt Macy 	}
2620eda14cbcSMatt Macy 
2621eda14cbcSMatt Macy 	zfs_handle_t *origin = zfs_open(earlier->zfs_hdl, clonename,
2622eda14cbcSMatt Macy 	    ZFS_TYPE_DATASET);
2623eda14cbcSMatt Macy 	uint64_t origin_txg = zfs_prop_get_int(origin, ZFS_PROP_CREATETXG);
2624eda14cbcSMatt Macy 
2625eda14cbcSMatt Macy 	/*
2626eda14cbcSMatt Macy 	 * If "earlier" is exactly the origin, then
2627eda14cbcSMatt Macy 	 * snapshot_is_before(earlier, origin) will return false (because
2628eda14cbcSMatt Macy 	 * they're the same).
2629eda14cbcSMatt Macy 	 */
2630eda14cbcSMatt Macy 	if (origin_txg == earlier_txg &&
2631eda14cbcSMatt Macy 	    strcmp(origin->zfs_name, earlier->zfs_name) == 0) {
2632eda14cbcSMatt Macy 		zfs_close(earlier_dir);
2633eda14cbcSMatt Macy 		zfs_close(later_dir);
2634eda14cbcSMatt Macy 		zfs_close(origin);
2635eda14cbcSMatt Macy 		return (B_TRUE);
2636eda14cbcSMatt Macy 	}
2637eda14cbcSMatt Macy 	zfs_close(earlier_dir);
2638eda14cbcSMatt Macy 	zfs_close(later_dir);
2639eda14cbcSMatt Macy 
2640eda14cbcSMatt Macy 	ret = snapshot_is_before(earlier, origin);
2641eda14cbcSMatt Macy 	zfs_close(origin);
2642eda14cbcSMatt Macy 	return (ret);
2643eda14cbcSMatt Macy }
2644eda14cbcSMatt Macy 
2645eda14cbcSMatt Macy /*
2646eda14cbcSMatt Macy  * The "zhp" argument is the handle of the dataset to send (typically a
2647eda14cbcSMatt Macy  * snapshot).  The "from" argument is the full name of the snapshot or
2648eda14cbcSMatt Macy  * bookmark that is the incremental source.
2649c03c5b1cSMartin Matuska  *
2650c03c5b1cSMartin Matuska  * Pre-wrapped (cf. lzc_send_wrapper()).
2651eda14cbcSMatt Macy  */
2652c03c5b1cSMartin Matuska static int
2653c03c5b1cSMartin Matuska zfs_send_one_cb_impl(zfs_handle_t *zhp, const char *from, int fd,
2654c03c5b1cSMartin Matuska     sendflags_t *flags, const char *redactbook)
2655eda14cbcSMatt Macy {
2656eda14cbcSMatt Macy 	int err;
2657eda14cbcSMatt Macy 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2658eda14cbcSMatt Macy 	char *name = zhp->zfs_name;
2659eda14cbcSMatt Macy 	pthread_t ptid;
2660eda14cbcSMatt Macy 	progress_arg_t pa = { 0 };
266115f0b8c3SMartin Matuska 	uint64_t size = 0;
2662eda14cbcSMatt Macy 
26631f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
2664eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2665eda14cbcSMatt Macy 	    "warning: cannot send '%s'"), name);
2666eda14cbcSMatt Macy 
2667eda14cbcSMatt Macy 	if (from != NULL && strchr(from, '@')) {
2668eda14cbcSMatt Macy 		zfs_handle_t *from_zhp = zfs_open(hdl, from,
2669eda14cbcSMatt Macy 		    ZFS_TYPE_DATASET);
2670eda14cbcSMatt Macy 		if (from_zhp == NULL)
2671eda14cbcSMatt Macy 			return (-1);
2672eda14cbcSMatt Macy 		if (!snapshot_is_before(from_zhp, zhp)) {
2673eda14cbcSMatt Macy 			zfs_close(from_zhp);
2674eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2675eda14cbcSMatt Macy 			    "not an earlier snapshot from the same fs"));
2676eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2677eda14cbcSMatt Macy 		}
2678eda14cbcSMatt Macy 		zfs_close(from_zhp);
2679eda14cbcSMatt Macy 	}
2680eda14cbcSMatt Macy 
2681eda14cbcSMatt Macy 	if (redactbook != NULL) {
2682eda14cbcSMatt Macy 		char bookname[ZFS_MAX_DATASET_NAME_LEN];
2683eda14cbcSMatt Macy 		nvlist_t *redact_snaps;
2684eda14cbcSMatt Macy 		zfs_handle_t *book_zhp;
2685eda14cbcSMatt Macy 		char *at, *pound;
2686eda14cbcSMatt Macy 		int dsnamelen;
2687eda14cbcSMatt Macy 
2688eda14cbcSMatt Macy 		pound = strchr(redactbook, '#');
2689eda14cbcSMatt Macy 		if (pound != NULL)
2690eda14cbcSMatt Macy 			redactbook = pound + 1;
2691eda14cbcSMatt Macy 		at = strchr(name, '@');
2692eda14cbcSMatt Macy 		if (at == NULL) {
2693eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2694eda14cbcSMatt Macy 			    "cannot do a redacted send to a filesystem"));
2695eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2696eda14cbcSMatt Macy 		}
2697eda14cbcSMatt Macy 		dsnamelen = at - name;
2698eda14cbcSMatt Macy 		if (snprintf(bookname, sizeof (bookname), "%.*s#%s",
2699eda14cbcSMatt Macy 		    dsnamelen, name, redactbook)
2700eda14cbcSMatt Macy 		    >= sizeof (bookname)) {
2701eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2702eda14cbcSMatt Macy 			    "invalid bookmark name"));
2703eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2704eda14cbcSMatt Macy 		}
2705eda14cbcSMatt Macy 		book_zhp = zfs_open(hdl, bookname, ZFS_TYPE_BOOKMARK);
2706eda14cbcSMatt Macy 		if (book_zhp == NULL)
2707eda14cbcSMatt Macy 			return (-1);
2708eda14cbcSMatt Macy 		if (nvlist_lookup_nvlist(book_zhp->zfs_props,
2709eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS),
2710eda14cbcSMatt Macy 		    &redact_snaps) != 0 || redact_snaps == NULL) {
2711eda14cbcSMatt Macy 			zfs_close(book_zhp);
2712eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2713eda14cbcSMatt Macy 			    "not a redaction bookmark"));
2714eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADTYPE, errbuf));
2715eda14cbcSMatt Macy 		}
2716eda14cbcSMatt Macy 		zfs_close(book_zhp);
2717eda14cbcSMatt Macy 	}
2718eda14cbcSMatt Macy 
2719eda14cbcSMatt Macy 	/*
2720eda14cbcSMatt Macy 	 * Send fs properties
2721eda14cbcSMatt Macy 	 */
2722eda14cbcSMatt Macy 	if (flags->props || flags->holds || flags->backup) {
2723eda14cbcSMatt Macy 		/*
2724eda14cbcSMatt Macy 		 * Note: the header generated by send_prelim_records()
2725eda14cbcSMatt Macy 		 * assumes that the incremental source is in the same
2726eda14cbcSMatt Macy 		 * filesystem/volume as the target (which is a requirement
2727eda14cbcSMatt Macy 		 * when doing "zfs send -R").  But that isn't always the
2728eda14cbcSMatt Macy 		 * case here (e.g. send from snap in origin, or send from
2729eda14cbcSMatt Macy 		 * bookmark).  We pass from=NULL, which will omit this
2730eda14cbcSMatt Macy 		 * information from the prelim records; it isn't used
2731eda14cbcSMatt Macy 		 * when receiving this type of stream.
2732eda14cbcSMatt Macy 		 */
2733eda14cbcSMatt Macy 		err = send_prelim_records(zhp, NULL, fd, B_TRUE, B_FALSE,
2734eda14cbcSMatt Macy 		    flags->verbosity > 0, flags->dryrun, flags->raw,
273516038816SMartin Matuska 		    flags->replicate, B_FALSE, flags->backup, flags->holds,
2736eda14cbcSMatt Macy 		    flags->props, flags->doall, NULL, NULL);
2737eda14cbcSMatt Macy 		if (err != 0)
2738eda14cbcSMatt Macy 			return (err);
2739eda14cbcSMatt Macy 	}
2740eda14cbcSMatt Macy 
2741eda14cbcSMatt Macy 	/*
2742eda14cbcSMatt Macy 	 * Perform size estimate if verbose was specified.
2743eda14cbcSMatt Macy 	 */
274415f0b8c3SMartin Matuska 	if (flags->verbosity != 0 || flags->progressastitle) {
2745eda14cbcSMatt Macy 		err = estimate_size(zhp, from, fd, flags, 0, 0, 0, redactbook,
274615f0b8c3SMartin Matuska 		    errbuf, &size);
2747eda14cbcSMatt Macy 		if (err != 0)
2748eda14cbcSMatt Macy 			return (err);
2749eda14cbcSMatt Macy 	}
2750eda14cbcSMatt Macy 
2751eda14cbcSMatt Macy 	if (flags->dryrun)
2752eda14cbcSMatt Macy 		return (0);
2753eda14cbcSMatt Macy 
2754eda14cbcSMatt Macy 	/*
2755eda14cbcSMatt Macy 	 * If progress reporting is requested, spawn a new thread to poll
2756eda14cbcSMatt Macy 	 * ZFS_IOC_SEND_PROGRESS at a regular interval.
2757eda14cbcSMatt Macy 	 */
2758315ee00fSMartin Matuska 	sigset_t oldmask;
2759315ee00fSMartin Matuska 	{
2760eda14cbcSMatt Macy 		pa.pa_zhp = zhp;
2761eda14cbcSMatt Macy 		pa.pa_fd = fd;
2762eda14cbcSMatt Macy 		pa.pa_parsable = flags->parsable;
2763eda14cbcSMatt Macy 		pa.pa_estimate = B_FALSE;
2764eda14cbcSMatt Macy 		pa.pa_verbosity = flags->verbosity;
276515f0b8c3SMartin Matuska 		pa.pa_size = size;
276615f0b8c3SMartin Matuska 		pa.pa_astitle = flags->progressastitle;
2767c9539b89SMartin Matuska 		pa.pa_progress = flags->progress;
2768eda14cbcSMatt Macy 
2769eda14cbcSMatt Macy 		err = pthread_create(&ptid, NULL,
2770eda14cbcSMatt Macy 		    send_progress_thread, &pa);
2771eda14cbcSMatt Macy 		if (err != 0) {
2772fd45b686SMartin Matuska 			zfs_error_aux(zhp->zfs_hdl, "%s", zfs_strerror(errno));
2773eda14cbcSMatt Macy 			return (zfs_error(zhp->zfs_hdl,
2774eda14cbcSMatt Macy 			    EZFS_THREADCREATEFAILED, errbuf));
2775eda14cbcSMatt Macy 		}
2776315ee00fSMartin Matuska 		SEND_PROGRESS_THREAD_PARENT_BLOCK(&oldmask);
2777eda14cbcSMatt Macy 	}
2778eda14cbcSMatt Macy 
2779eda14cbcSMatt Macy 	err = lzc_send_redacted(name, from, fd,
2780eda14cbcSMatt Macy 	    lzc_flags_from_sendflags(flags), redactbook);
2781eda14cbcSMatt Macy 
2782315ee00fSMartin Matuska 	if (send_progress_thread_exit(hdl, ptid, &oldmask))
2783716fd348SMartin Matuska 			return (-1);
2784eda14cbcSMatt Macy 
2785e92ffd9bSMartin Matuska 	if (err == 0 && (flags->props || flags->holds || flags->backup)) {
2786eda14cbcSMatt Macy 		/* Write the final end record. */
27873f9d360cSMartin Matuska 		err = send_conclusion_record(fd, NULL);
2788eda14cbcSMatt Macy 		if (err != 0)
2789eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, err, errbuf));
2790eda14cbcSMatt Macy 	}
2791eda14cbcSMatt Macy 	if (err != 0) {
2792eda14cbcSMatt Macy 		switch (errno) {
2793eda14cbcSMatt Macy 		case EXDEV:
2794eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2795eda14cbcSMatt Macy 			    "not an earlier snapshot from the same fs"));
2796eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2797eda14cbcSMatt Macy 
2798eda14cbcSMatt Macy 		case ENOENT:
2799eda14cbcSMatt Macy 		case ESRCH:
2800eda14cbcSMatt Macy 			if (lzc_exists(name)) {
2801eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2802eda14cbcSMatt Macy 				    "incremental source (%s) does not exist"),
2803eda14cbcSMatt Macy 				    from);
2804eda14cbcSMatt Macy 			}
2805eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2806eda14cbcSMatt Macy 
2807eda14cbcSMatt Macy 		case EACCES:
2808eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2809eda14cbcSMatt Macy 			    "dataset key must be loaded"));
2810eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2811eda14cbcSMatt Macy 
2812eda14cbcSMatt Macy 		case EBUSY:
2813eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2814eda14cbcSMatt Macy 			    "target is busy; if a filesystem, "
2815eda14cbcSMatt Macy 			    "it must not be mounted"));
2816eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BUSY, errbuf));
2817eda14cbcSMatt Macy 
2818eda14cbcSMatt Macy 		case EDQUOT:
2819eda14cbcSMatt Macy 		case EFAULT:
2820eda14cbcSMatt Macy 		case EFBIG:
2821eda14cbcSMatt Macy 		case EINVAL:
2822eda14cbcSMatt Macy 		case EIO:
2823eda14cbcSMatt Macy 		case ENOLINK:
2824eda14cbcSMatt Macy 		case ENOSPC:
2825eda14cbcSMatt Macy 		case ENOSTR:
2826eda14cbcSMatt Macy 		case ENXIO:
2827eda14cbcSMatt Macy 		case EPIPE:
2828eda14cbcSMatt Macy 		case ERANGE:
2829eda14cbcSMatt Macy 		case EROFS:
2830fd45b686SMartin Matuska 			zfs_error_aux(hdl, "%s", zfs_strerror(errno));
2831eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2832*7a7741afSMartin Matuska 		case ZFS_ERR_STREAM_LARGE_MICROZAP:
2833*7a7741afSMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2834*7a7741afSMartin Matuska 			    "source snapshot contains large microzaps, "
2835*7a7741afSMartin Matuska 			    "need -L (--large-block) or -w (--raw) to "
2836*7a7741afSMartin Matuska 			    "generate stream"));
2837*7a7741afSMartin Matuska 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2838eda14cbcSMatt Macy 		default:
2839eda14cbcSMatt Macy 			return (zfs_standard_error(hdl, errno, errbuf));
2840eda14cbcSMatt Macy 		}
2841eda14cbcSMatt Macy 	}
2842eda14cbcSMatt Macy 	return (err != 0);
2843eda14cbcSMatt Macy }
2844eda14cbcSMatt Macy 
2845c03c5b1cSMartin Matuska struct zfs_send_one {
2846c03c5b1cSMartin Matuska 	zfs_handle_t *zhp;
2847c03c5b1cSMartin Matuska 	const char *from;
2848c03c5b1cSMartin Matuska 	sendflags_t *flags;
2849c03c5b1cSMartin Matuska 	const char *redactbook;
2850c03c5b1cSMartin Matuska };
2851c03c5b1cSMartin Matuska 
2852c03c5b1cSMartin Matuska static int
2853c03c5b1cSMartin Matuska zfs_send_one_cb(int fd, void *arg)
2854c03c5b1cSMartin Matuska {
2855c03c5b1cSMartin Matuska 	struct zfs_send_one *zso = arg;
2856c03c5b1cSMartin Matuska 	return (zfs_send_one_cb_impl(zso->zhp, zso->from, fd, zso->flags,
2857c03c5b1cSMartin Matuska 	    zso->redactbook));
2858c03c5b1cSMartin Matuska }
2859c03c5b1cSMartin Matuska 
2860c03c5b1cSMartin Matuska int
2861c03c5b1cSMartin Matuska zfs_send_one(zfs_handle_t *zhp, const char *from, int fd, sendflags_t *flags,
2862c03c5b1cSMartin Matuska     const char *redactbook)
2863c03c5b1cSMartin Matuska {
2864c03c5b1cSMartin Matuska 	struct zfs_send_one zso = {
2865c03c5b1cSMartin Matuska 		.zhp = zhp,
2866c03c5b1cSMartin Matuska 		.from = from,
2867c03c5b1cSMartin Matuska 		.flags = flags,
2868c03c5b1cSMartin Matuska 		.redactbook = redactbook,
2869c03c5b1cSMartin Matuska 	};
2870c03c5b1cSMartin Matuska 	return (lzc_send_wrapper(zfs_send_one_cb, fd, &zso));
2871c03c5b1cSMartin Matuska }
2872c03c5b1cSMartin Matuska 
2873eda14cbcSMatt Macy /*
2874eda14cbcSMatt Macy  * Routines specific to "zfs recv"
2875eda14cbcSMatt Macy  */
2876eda14cbcSMatt Macy 
2877eda14cbcSMatt Macy static int
2878eda14cbcSMatt Macy recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2879eda14cbcSMatt Macy     boolean_t byteswap, zio_cksum_t *zc)
2880eda14cbcSMatt Macy {
2881eda14cbcSMatt Macy 	char *cp = buf;
2882eda14cbcSMatt Macy 	int rv;
2883eda14cbcSMatt Macy 	int len = ilen;
2884eda14cbcSMatt Macy 
2885eda14cbcSMatt Macy 	do {
2886eda14cbcSMatt Macy 		rv = read(fd, cp, len);
2887eda14cbcSMatt Macy 		cp += rv;
2888eda14cbcSMatt Macy 		len -= rv;
2889eda14cbcSMatt Macy 	} while (rv > 0);
2890eda14cbcSMatt Macy 
2891eda14cbcSMatt Macy 	if (rv < 0 || len != 0) {
2892eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2893eda14cbcSMatt Macy 		    "failed to read from stream"));
2894eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2895eda14cbcSMatt Macy 		    "cannot receive")));
2896eda14cbcSMatt Macy 	}
2897eda14cbcSMatt Macy 
2898eda14cbcSMatt Macy 	if (zc) {
2899eda14cbcSMatt Macy 		if (byteswap)
2900eda14cbcSMatt Macy 			fletcher_4_incremental_byteswap(buf, ilen, zc);
2901eda14cbcSMatt Macy 		else
2902eda14cbcSMatt Macy 			fletcher_4_incremental_native(buf, ilen, zc);
2903eda14cbcSMatt Macy 	}
2904eda14cbcSMatt Macy 	return (0);
2905eda14cbcSMatt Macy }
2906eda14cbcSMatt Macy 
2907eda14cbcSMatt Macy static int
2908eda14cbcSMatt Macy recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2909eda14cbcSMatt Macy     boolean_t byteswap, zio_cksum_t *zc)
2910eda14cbcSMatt Macy {
2911eda14cbcSMatt Macy 	char *buf;
2912eda14cbcSMatt Macy 	int err;
2913eda14cbcSMatt Macy 
2914eda14cbcSMatt Macy 	buf = zfs_alloc(hdl, len);
2915eda14cbcSMatt Macy 
2916ac0bf12eSMatt Macy 	if (len > hdl->libzfs_max_nvlist) {
2917ac0bf12eSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "nvlist too large"));
29182c48331dSMatt Macy 		free(buf);
2919ac0bf12eSMatt Macy 		return (ENOMEM);
2920ac0bf12eSMatt Macy 	}
2921ac0bf12eSMatt Macy 
2922eda14cbcSMatt Macy 	err = recv_read(hdl, fd, buf, len, byteswap, zc);
2923eda14cbcSMatt Macy 	if (err != 0) {
2924eda14cbcSMatt Macy 		free(buf);
2925eda14cbcSMatt Macy 		return (err);
2926eda14cbcSMatt Macy 	}
2927eda14cbcSMatt Macy 
2928eda14cbcSMatt Macy 	err = nvlist_unpack(buf, len, nvp, 0);
2929eda14cbcSMatt Macy 	free(buf);
2930eda14cbcSMatt Macy 	if (err != 0) {
2931eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2932eda14cbcSMatt Macy 		    "stream (malformed nvlist)"));
2933eda14cbcSMatt Macy 		return (EINVAL);
2934eda14cbcSMatt Macy 	}
2935eda14cbcSMatt Macy 	return (0);
2936eda14cbcSMatt Macy }
2937eda14cbcSMatt Macy 
2938eda14cbcSMatt Macy /*
2939eda14cbcSMatt Macy  * Returns the grand origin (origin of origin of origin...) of a given handle.
2940eda14cbcSMatt Macy  * If this dataset is not a clone, it simply returns a copy of the original
2941eda14cbcSMatt Macy  * handle.
2942eda14cbcSMatt Macy  */
2943eda14cbcSMatt Macy static zfs_handle_t *
2944eda14cbcSMatt Macy recv_open_grand_origin(zfs_handle_t *zhp)
2945eda14cbcSMatt Macy {
2946eda14cbcSMatt Macy 	char origin[ZFS_MAX_DATASET_NAME_LEN];
2947eda14cbcSMatt Macy 	zprop_source_t src;
2948eda14cbcSMatt Macy 	zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2949eda14cbcSMatt Macy 
2950eda14cbcSMatt Macy 	while (ozhp != NULL) {
2951eda14cbcSMatt Macy 		if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2952eda14cbcSMatt Macy 		    sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2953eda14cbcSMatt Macy 			break;
2954eda14cbcSMatt Macy 
2955eda14cbcSMatt Macy 		(void) zfs_close(ozhp);
2956eda14cbcSMatt Macy 		ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2957eda14cbcSMatt Macy 	}
2958eda14cbcSMatt Macy 
2959eda14cbcSMatt Macy 	return (ozhp);
2960eda14cbcSMatt Macy }
2961eda14cbcSMatt Macy 
2962eda14cbcSMatt Macy static int
2963eda14cbcSMatt Macy recv_rename_impl(zfs_handle_t *zhp, const char *name, const char *newname)
2964eda14cbcSMatt Macy {
2965eda14cbcSMatt Macy 	int err;
2966eda14cbcSMatt Macy 	zfs_handle_t *ozhp = NULL;
2967eda14cbcSMatt Macy 
2968eda14cbcSMatt Macy 	/*
2969eda14cbcSMatt Macy 	 * Attempt to rename the dataset. If it fails with EACCES we have
2970eda14cbcSMatt Macy 	 * attempted to rename the dataset outside of its encryption root.
2971eda14cbcSMatt Macy 	 * Force the dataset to become an encryption root and try again.
2972eda14cbcSMatt Macy 	 */
2973eda14cbcSMatt Macy 	err = lzc_rename(name, newname);
2974eda14cbcSMatt Macy 	if (err == EACCES) {
2975eda14cbcSMatt Macy 		ozhp = recv_open_grand_origin(zhp);
2976eda14cbcSMatt Macy 		if (ozhp == NULL) {
2977eda14cbcSMatt Macy 			err = ENOENT;
2978eda14cbcSMatt Macy 			goto out;
2979eda14cbcSMatt Macy 		}
2980eda14cbcSMatt Macy 
2981eda14cbcSMatt Macy 		err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2982eda14cbcSMatt Macy 		    NULL, NULL, 0);
2983eda14cbcSMatt Macy 		if (err != 0)
2984eda14cbcSMatt Macy 			goto out;
2985eda14cbcSMatt Macy 
2986eda14cbcSMatt Macy 		err = lzc_rename(name, newname);
2987eda14cbcSMatt Macy 	}
2988eda14cbcSMatt Macy 
2989eda14cbcSMatt Macy out:
2990eda14cbcSMatt Macy 	if (ozhp != NULL)
2991eda14cbcSMatt Macy 		zfs_close(ozhp);
2992eda14cbcSMatt Macy 	return (err);
2993eda14cbcSMatt Macy }
2994eda14cbcSMatt Macy 
2995eda14cbcSMatt Macy static int
2996eda14cbcSMatt Macy recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2997eda14cbcSMatt Macy     int baselen, char *newname, recvflags_t *flags)
2998eda14cbcSMatt Macy {
2999eda14cbcSMatt Macy 	static int seq;
3000eda14cbcSMatt Macy 	int err;
3001eda14cbcSMatt Macy 	prop_changelist_t *clp = NULL;
3002eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL;
3003eda14cbcSMatt Macy 
3004eda14cbcSMatt Macy 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
3005eda14cbcSMatt Macy 	if (zhp == NULL) {
3006eda14cbcSMatt Macy 		err = -1;
3007eda14cbcSMatt Macy 		goto out;
3008eda14cbcSMatt Macy 	}
3009eda14cbcSMatt Macy 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3010eda14cbcSMatt Macy 	    flags->force ? MS_FORCE : 0);
3011eda14cbcSMatt Macy 	if (clp == NULL) {
3012eda14cbcSMatt Macy 		err = -1;
3013eda14cbcSMatt Macy 		goto out;
3014eda14cbcSMatt Macy 	}
3015eda14cbcSMatt Macy 	err = changelist_prefix(clp);
3016eda14cbcSMatt Macy 	if (err)
3017eda14cbcSMatt Macy 		goto out;
3018eda14cbcSMatt Macy 
3019eda14cbcSMatt Macy 	if (tryname) {
3020be181ee2SMartin Matuska 		(void) strlcpy(newname, tryname, ZFS_MAX_DATASET_NAME_LEN);
3021eda14cbcSMatt Macy 		if (flags->verbose) {
3022eda14cbcSMatt Macy 			(void) printf("attempting rename %s to %s\n",
3023eda14cbcSMatt Macy 			    name, newname);
3024eda14cbcSMatt Macy 		}
3025eda14cbcSMatt Macy 		err = recv_rename_impl(zhp, name, newname);
3026eda14cbcSMatt Macy 		if (err == 0)
3027eda14cbcSMatt Macy 			changelist_rename(clp, name, tryname);
3028eda14cbcSMatt Macy 	} else {
3029eda14cbcSMatt Macy 		err = ENOENT;
3030eda14cbcSMatt Macy 	}
3031eda14cbcSMatt Macy 
3032eda14cbcSMatt Macy 	if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
3033eda14cbcSMatt Macy 		seq++;
3034eda14cbcSMatt Macy 
3035eda14cbcSMatt Macy 		(void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
3036eda14cbcSMatt Macy 		    "%.*srecv-%u-%u", baselen, name, getpid(), seq);
3037eda14cbcSMatt Macy 
3038eda14cbcSMatt Macy 		if (flags->verbose) {
3039eda14cbcSMatt Macy 			(void) printf("failed - trying rename %s to %s\n",
3040eda14cbcSMatt Macy 			    name, newname);
3041eda14cbcSMatt Macy 		}
3042eda14cbcSMatt Macy 		err = recv_rename_impl(zhp, name, newname);
3043eda14cbcSMatt Macy 		if (err == 0)
3044eda14cbcSMatt Macy 			changelist_rename(clp, name, newname);
3045eda14cbcSMatt Macy 		if (err && flags->verbose) {
3046eda14cbcSMatt Macy 			(void) printf("failed (%u) - "
3047eda14cbcSMatt Macy 			    "will try again on next pass\n", errno);
3048eda14cbcSMatt Macy 		}
3049eda14cbcSMatt Macy 		err = EAGAIN;
3050eda14cbcSMatt Macy 	} else if (flags->verbose) {
3051eda14cbcSMatt Macy 		if (err == 0)
3052eda14cbcSMatt Macy 			(void) printf("success\n");
3053eda14cbcSMatt Macy 		else
3054eda14cbcSMatt Macy 			(void) printf("failed (%u)\n", errno);
3055eda14cbcSMatt Macy 	}
3056eda14cbcSMatt Macy 
3057eda14cbcSMatt Macy 	(void) changelist_postfix(clp);
3058eda14cbcSMatt Macy 
3059eda14cbcSMatt Macy out:
3060eda14cbcSMatt Macy 	if (clp != NULL)
3061eda14cbcSMatt Macy 		changelist_free(clp);
3062eda14cbcSMatt Macy 	if (zhp != NULL)
3063eda14cbcSMatt Macy 		zfs_close(zhp);
3064eda14cbcSMatt Macy 
3065eda14cbcSMatt Macy 	return (err);
3066eda14cbcSMatt Macy }
3067eda14cbcSMatt Macy 
3068eda14cbcSMatt Macy static int
3069eda14cbcSMatt Macy recv_promote(libzfs_handle_t *hdl, const char *fsname,
3070eda14cbcSMatt Macy     const char *origin_fsname, recvflags_t *flags)
3071eda14cbcSMatt Macy {
3072eda14cbcSMatt Macy 	int err;
3073eda14cbcSMatt Macy 	zfs_cmd_t zc = {"\0"};
3074eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL, *ozhp = NULL;
3075eda14cbcSMatt Macy 
3076eda14cbcSMatt Macy 	if (flags->verbose)
3077eda14cbcSMatt Macy 		(void) printf("promoting %s\n", fsname);
3078eda14cbcSMatt Macy 
3079eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
3080eda14cbcSMatt Macy 	(void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
3081eda14cbcSMatt Macy 
3082eda14cbcSMatt Macy 	/*
3083eda14cbcSMatt Macy 	 * Attempt to promote the dataset. If it fails with EACCES the
3084eda14cbcSMatt Macy 	 * promotion would cause this dataset to leave its encryption root.
3085eda14cbcSMatt Macy 	 * Force the origin to become an encryption root and try again.
3086eda14cbcSMatt Macy 	 */
3087eda14cbcSMatt Macy 	err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3088eda14cbcSMatt Macy 	if (err == EACCES) {
3089eda14cbcSMatt Macy 		zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3090eda14cbcSMatt Macy 		if (zhp == NULL) {
3091eda14cbcSMatt Macy 			err = -1;
3092eda14cbcSMatt Macy 			goto out;
3093eda14cbcSMatt Macy 		}
3094eda14cbcSMatt Macy 
3095eda14cbcSMatt Macy 		ozhp = recv_open_grand_origin(zhp);
3096eda14cbcSMatt Macy 		if (ozhp == NULL) {
3097eda14cbcSMatt Macy 			err = -1;
3098eda14cbcSMatt Macy 			goto out;
3099eda14cbcSMatt Macy 		}
3100eda14cbcSMatt Macy 
3101eda14cbcSMatt Macy 		err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
3102eda14cbcSMatt Macy 		    NULL, NULL, 0);
3103eda14cbcSMatt Macy 		if (err != 0)
3104eda14cbcSMatt Macy 			goto out;
3105eda14cbcSMatt Macy 
3106eda14cbcSMatt Macy 		err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
3107eda14cbcSMatt Macy 	}
3108eda14cbcSMatt Macy 
3109eda14cbcSMatt Macy out:
3110eda14cbcSMatt Macy 	if (zhp != NULL)
3111eda14cbcSMatt Macy 		zfs_close(zhp);
3112eda14cbcSMatt Macy 	if (ozhp != NULL)
3113eda14cbcSMatt Macy 		zfs_close(ozhp);
3114eda14cbcSMatt Macy 
3115eda14cbcSMatt Macy 	return (err);
3116eda14cbcSMatt Macy }
3117eda14cbcSMatt Macy 
3118eda14cbcSMatt Macy static int
3119eda14cbcSMatt Macy recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
3120eda14cbcSMatt Macy     char *newname, recvflags_t *flags)
3121eda14cbcSMatt Macy {
3122eda14cbcSMatt Macy 	int err = 0;
3123eda14cbcSMatt Macy 	prop_changelist_t *clp;
3124eda14cbcSMatt Macy 	zfs_handle_t *zhp;
3125eda14cbcSMatt Macy 	boolean_t defer = B_FALSE;
3126eda14cbcSMatt Macy 	int spa_version;
3127eda14cbcSMatt Macy 
3128eda14cbcSMatt Macy 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
3129eda14cbcSMatt Macy 	if (zhp == NULL)
3130eda14cbcSMatt Macy 		return (-1);
3131c03c5b1cSMartin Matuska 	zfs_type_t type = zfs_get_type(zhp);
3132c03c5b1cSMartin Matuska 	if (type == ZFS_TYPE_SNAPSHOT &&
3133eda14cbcSMatt Macy 	    zfs_spa_version(zhp, &spa_version) == 0 &&
3134eda14cbcSMatt Macy 	    spa_version >= SPA_VERSION_USERREFS)
3135eda14cbcSMatt Macy 		defer = B_TRUE;
3136c03c5b1cSMartin Matuska 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
3137c03c5b1cSMartin Matuska 	    flags->force ? MS_FORCE : 0);
3138eda14cbcSMatt Macy 	zfs_close(zhp);
3139eda14cbcSMatt Macy 	if (clp == NULL)
3140eda14cbcSMatt Macy 		return (-1);
3141c03c5b1cSMartin Matuska 
3142eda14cbcSMatt Macy 	err = changelist_prefix(clp);
3143eda14cbcSMatt Macy 	if (err)
3144eda14cbcSMatt Macy 		return (err);
3145eda14cbcSMatt Macy 
3146eda14cbcSMatt Macy 	if (flags->verbose)
3147eda14cbcSMatt Macy 		(void) printf("attempting destroy %s\n", name);
3148c03c5b1cSMartin Matuska 	if (type == ZFS_TYPE_SNAPSHOT) {
3149eda14cbcSMatt Macy 		nvlist_t *nv = fnvlist_alloc();
3150eda14cbcSMatt Macy 		fnvlist_add_boolean(nv, name);
3151eda14cbcSMatt Macy 		err = lzc_destroy_snaps(nv, defer, NULL);
3152eda14cbcSMatt Macy 		fnvlist_free(nv);
3153eda14cbcSMatt Macy 	} else {
3154eda14cbcSMatt Macy 		err = lzc_destroy(name);
3155eda14cbcSMatt Macy 	}
3156eda14cbcSMatt Macy 	if (err == 0) {
3157eda14cbcSMatt Macy 		if (flags->verbose)
3158eda14cbcSMatt Macy 			(void) printf("success\n");
3159eda14cbcSMatt Macy 		changelist_remove(clp, name);
3160eda14cbcSMatt Macy 	}
3161eda14cbcSMatt Macy 
3162eda14cbcSMatt Macy 	(void) changelist_postfix(clp);
3163eda14cbcSMatt Macy 	changelist_free(clp);
3164eda14cbcSMatt Macy 
3165eda14cbcSMatt Macy 	/*
3166eda14cbcSMatt Macy 	 * Deferred destroy might destroy the snapshot or only mark it to be
3167eda14cbcSMatt Macy 	 * destroyed later, and it returns success in either case.
3168eda14cbcSMatt Macy 	 */
3169eda14cbcSMatt Macy 	if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
3170eda14cbcSMatt Macy 	    ZFS_TYPE_SNAPSHOT))) {
3171eda14cbcSMatt Macy 		err = recv_rename(hdl, name, NULL, baselen, newname, flags);
3172eda14cbcSMatt Macy 	}
3173eda14cbcSMatt Macy 
3174eda14cbcSMatt Macy 	return (err);
3175eda14cbcSMatt Macy }
3176eda14cbcSMatt Macy 
3177eda14cbcSMatt Macy typedef struct guid_to_name_data {
3178eda14cbcSMatt Macy 	uint64_t guid;
3179eda14cbcSMatt Macy 	boolean_t bookmark_ok;
3180eda14cbcSMatt Macy 	char *name;
3181eda14cbcSMatt Macy 	char *skip;
3182eda14cbcSMatt Macy 	uint64_t *redact_snap_guids;
3183eda14cbcSMatt Macy 	uint64_t num_redact_snaps;
3184eda14cbcSMatt Macy } guid_to_name_data_t;
3185eda14cbcSMatt Macy 
3186eda14cbcSMatt Macy static boolean_t
3187eda14cbcSMatt Macy redact_snaps_match(zfs_handle_t *zhp, guid_to_name_data_t *gtnd)
3188eda14cbcSMatt Macy {
3189eda14cbcSMatt Macy 	uint64_t *bmark_snaps;
3190eda14cbcSMatt Macy 	uint_t bmark_num_snaps;
3191eda14cbcSMatt Macy 	nvlist_t *nvl;
3192eda14cbcSMatt Macy 	if (zhp->zfs_type != ZFS_TYPE_BOOKMARK)
3193eda14cbcSMatt Macy 		return (B_FALSE);
3194eda14cbcSMatt Macy 
3195eda14cbcSMatt Macy 	nvl = fnvlist_lookup_nvlist(zhp->zfs_props,
3196eda14cbcSMatt Macy 	    zfs_prop_to_name(ZFS_PROP_REDACT_SNAPS));
3197eda14cbcSMatt Macy 	bmark_snaps = fnvlist_lookup_uint64_array(nvl, ZPROP_VALUE,
3198eda14cbcSMatt Macy 	    &bmark_num_snaps);
3199eda14cbcSMatt Macy 	if (bmark_num_snaps != gtnd->num_redact_snaps)
3200eda14cbcSMatt Macy 		return (B_FALSE);
3201eda14cbcSMatt Macy 	int i = 0;
3202eda14cbcSMatt Macy 	for (; i < bmark_num_snaps; i++) {
3203eda14cbcSMatt Macy 		int j = 0;
3204eda14cbcSMatt Macy 		for (; j < bmark_num_snaps; j++) {
3205eda14cbcSMatt Macy 			if (bmark_snaps[i] == gtnd->redact_snap_guids[j])
3206eda14cbcSMatt Macy 				break;
3207eda14cbcSMatt Macy 		}
3208eda14cbcSMatt Macy 		if (j == bmark_num_snaps)
3209eda14cbcSMatt Macy 			break;
3210eda14cbcSMatt Macy 	}
3211eda14cbcSMatt Macy 	return (i == bmark_num_snaps);
3212eda14cbcSMatt Macy }
3213eda14cbcSMatt Macy 
3214eda14cbcSMatt Macy static int
3215eda14cbcSMatt Macy guid_to_name_cb(zfs_handle_t *zhp, void *arg)
3216eda14cbcSMatt Macy {
3217eda14cbcSMatt Macy 	guid_to_name_data_t *gtnd = arg;
3218eda14cbcSMatt Macy 	const char *slash;
3219eda14cbcSMatt Macy 	int err;
3220eda14cbcSMatt Macy 
3221eda14cbcSMatt Macy 	if (gtnd->skip != NULL &&
3222eda14cbcSMatt Macy 	    (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
3223eda14cbcSMatt Macy 	    strcmp(slash + 1, gtnd->skip) == 0) {
3224eda14cbcSMatt Macy 		zfs_close(zhp);
3225eda14cbcSMatt Macy 		return (0);
3226eda14cbcSMatt Macy 	}
3227eda14cbcSMatt Macy 
3228eda14cbcSMatt Macy 	if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid &&
3229eda14cbcSMatt Macy 	    (gtnd->num_redact_snaps == -1 || redact_snaps_match(zhp, gtnd))) {
3230eda14cbcSMatt Macy 		(void) strcpy(gtnd->name, zhp->zfs_name);
3231eda14cbcSMatt Macy 		zfs_close(zhp);
3232eda14cbcSMatt Macy 		return (EEXIST);
3233eda14cbcSMatt Macy 	}
3234eda14cbcSMatt Macy 
3235d411c1d6SMartin Matuska 	err = zfs_iter_children_v2(zhp, 0, guid_to_name_cb, gtnd);
3236eda14cbcSMatt Macy 	if (err != EEXIST && gtnd->bookmark_ok)
3237d411c1d6SMartin Matuska 		err = zfs_iter_bookmarks_v2(zhp, 0, guid_to_name_cb, gtnd);
3238eda14cbcSMatt Macy 	zfs_close(zhp);
3239eda14cbcSMatt Macy 	return (err);
3240eda14cbcSMatt Macy }
3241eda14cbcSMatt Macy 
3242eda14cbcSMatt Macy /*
3243eda14cbcSMatt Macy  * Attempt to find the local dataset associated with this guid.  In the case of
3244eda14cbcSMatt Macy  * multiple matches, we attempt to find the "best" match by searching
3245eda14cbcSMatt Macy  * progressively larger portions of the hierarchy.  This allows one to send a
3246eda14cbcSMatt Macy  * tree of datasets individually and guarantee that we will find the source
3247eda14cbcSMatt Macy  * guid within that hierarchy, even if there are multiple matches elsewhere.
3248eda14cbcSMatt Macy  *
3249eda14cbcSMatt Macy  * If num_redact_snaps is not -1, we attempt to find a redaction bookmark with
3250eda14cbcSMatt Macy  * the specified number of redaction snapshots.  If num_redact_snaps isn't 0 or
3251eda14cbcSMatt Macy  * -1, then redact_snap_guids will be an array of the guids of the snapshots the
3252eda14cbcSMatt Macy  * redaction bookmark was created with.  If num_redact_snaps is -1, then we will
3253eda14cbcSMatt Macy  * attempt to find a snapshot or bookmark (if bookmark_ok is passed) with the
3254eda14cbcSMatt Macy  * given guid.  Note that a redaction bookmark can be returned if
3255eda14cbcSMatt Macy  * num_redact_snaps == -1.
3256eda14cbcSMatt Macy  */
3257eda14cbcSMatt Macy static int
3258eda14cbcSMatt Macy guid_to_name_redact_snaps(libzfs_handle_t *hdl, const char *parent,
3259eda14cbcSMatt Macy     uint64_t guid, boolean_t bookmark_ok, uint64_t *redact_snap_guids,
3260eda14cbcSMatt Macy     uint64_t num_redact_snaps, char *name)
3261eda14cbcSMatt Macy {
3262eda14cbcSMatt Macy 	char pname[ZFS_MAX_DATASET_NAME_LEN];
3263eda14cbcSMatt Macy 	guid_to_name_data_t gtnd;
3264eda14cbcSMatt Macy 
3265eda14cbcSMatt Macy 	gtnd.guid = guid;
3266eda14cbcSMatt Macy 	gtnd.bookmark_ok = bookmark_ok;
3267eda14cbcSMatt Macy 	gtnd.name = name;
3268eda14cbcSMatt Macy 	gtnd.skip = NULL;
3269eda14cbcSMatt Macy 	gtnd.redact_snap_guids = redact_snap_guids;
3270eda14cbcSMatt Macy 	gtnd.num_redact_snaps = num_redact_snaps;
3271eda14cbcSMatt Macy 
3272eda14cbcSMatt Macy 	/*
3273eda14cbcSMatt Macy 	 * Search progressively larger portions of the hierarchy, starting
3274eda14cbcSMatt Macy 	 * with the filesystem specified by 'parent'.  This will
3275eda14cbcSMatt Macy 	 * select the "most local" version of the origin snapshot in the case
3276eda14cbcSMatt Macy 	 * that there are multiple matching snapshots in the system.
3277eda14cbcSMatt Macy 	 */
3278eda14cbcSMatt Macy 	(void) strlcpy(pname, parent, sizeof (pname));
3279eda14cbcSMatt Macy 	char *cp = strrchr(pname, '@');
3280eda14cbcSMatt Macy 	if (cp == NULL)
3281eda14cbcSMatt Macy 		cp = strchr(pname, '\0');
3282eda14cbcSMatt Macy 	for (; cp != NULL; cp = strrchr(pname, '/')) {
3283eda14cbcSMatt Macy 		/* Chop off the last component and open the parent */
3284eda14cbcSMatt Macy 		*cp = '\0';
3285eda14cbcSMatt Macy 		zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
3286eda14cbcSMatt Macy 
3287eda14cbcSMatt Macy 		if (zhp == NULL)
3288eda14cbcSMatt Macy 			continue;
3289eda14cbcSMatt Macy 		int err = guid_to_name_cb(zfs_handle_dup(zhp), &gtnd);
3290eda14cbcSMatt Macy 		if (err != EEXIST)
3291d411c1d6SMartin Matuska 			err = zfs_iter_children_v2(zhp, 0, guid_to_name_cb,
3292d411c1d6SMartin Matuska 			    &gtnd);
3293eda14cbcSMatt Macy 		if (err != EEXIST && bookmark_ok)
3294d411c1d6SMartin Matuska 			err = zfs_iter_bookmarks_v2(zhp, 0, guid_to_name_cb,
329515f0b8c3SMartin Matuska 			    &gtnd);
3296eda14cbcSMatt Macy 		zfs_close(zhp);
3297eda14cbcSMatt Macy 		if (err == EEXIST)
3298eda14cbcSMatt Macy 			return (0);
3299eda14cbcSMatt Macy 
3300eda14cbcSMatt Macy 		/*
3301eda14cbcSMatt Macy 		 * Remember the last portion of the dataset so we skip it next
3302eda14cbcSMatt Macy 		 * time through (as we've already searched that portion of the
3303eda14cbcSMatt Macy 		 * hierarchy).
3304eda14cbcSMatt Macy 		 */
3305eda14cbcSMatt Macy 		gtnd.skip = strrchr(pname, '/') + 1;
3306eda14cbcSMatt Macy 	}
3307eda14cbcSMatt Macy 
3308eda14cbcSMatt Macy 	return (ENOENT);
3309eda14cbcSMatt Macy }
3310eda14cbcSMatt Macy 
3311eda14cbcSMatt Macy static int
3312eda14cbcSMatt Macy guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
3313eda14cbcSMatt Macy     boolean_t bookmark_ok, char *name)
3314eda14cbcSMatt Macy {
3315eda14cbcSMatt Macy 	return (guid_to_name_redact_snaps(hdl, parent, guid, bookmark_ok, NULL,
3316eda14cbcSMatt Macy 	    -1, name));
3317eda14cbcSMatt Macy }
3318eda14cbcSMatt Macy 
3319eda14cbcSMatt Macy /*
3320eda14cbcSMatt Macy  * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
3321eda14cbcSMatt Macy  * guid1 is after guid2.
3322eda14cbcSMatt Macy  */
3323eda14cbcSMatt Macy static int
3324eda14cbcSMatt Macy created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
3325eda14cbcSMatt Macy     uint64_t guid1, uint64_t guid2)
3326eda14cbcSMatt Macy {
3327eda14cbcSMatt Macy 	nvlist_t *nvfs;
33282a58b312SMartin Matuska 	const char *fsname = NULL, *snapname = NULL;
3329eda14cbcSMatt Macy 	char buf[ZFS_MAX_DATASET_NAME_LEN];
3330eda14cbcSMatt Macy 	int rv;
3331eda14cbcSMatt Macy 	zfs_handle_t *guid1hdl, *guid2hdl;
3332eda14cbcSMatt Macy 	uint64_t create1, create2;
3333eda14cbcSMatt Macy 
3334eda14cbcSMatt Macy 	if (guid2 == 0)
3335eda14cbcSMatt Macy 		return (0);
3336eda14cbcSMatt Macy 	if (guid1 == 0)
3337eda14cbcSMatt Macy 		return (1);
3338eda14cbcSMatt Macy 
3339eda14cbcSMatt Macy 	nvfs = fsavl_find(avl, guid1, &snapname);
3340184c1b94SMartin Matuska 	fsname = fnvlist_lookup_string(nvfs, "name");
3341eda14cbcSMatt Macy 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3342eda14cbcSMatt Macy 	guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3343eda14cbcSMatt Macy 	if (guid1hdl == NULL)
3344eda14cbcSMatt Macy 		return (-1);
3345eda14cbcSMatt Macy 
3346eda14cbcSMatt Macy 	nvfs = fsavl_find(avl, guid2, &snapname);
3347184c1b94SMartin Matuska 	fsname = fnvlist_lookup_string(nvfs, "name");
3348eda14cbcSMatt Macy 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
3349eda14cbcSMatt Macy 	guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
3350eda14cbcSMatt Macy 	if (guid2hdl == NULL) {
3351eda14cbcSMatt Macy 		zfs_close(guid1hdl);
3352eda14cbcSMatt Macy 		return (-1);
3353eda14cbcSMatt Macy 	}
3354eda14cbcSMatt Macy 
3355eda14cbcSMatt Macy 	create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
3356eda14cbcSMatt Macy 	create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
3357eda14cbcSMatt Macy 
3358eda14cbcSMatt Macy 	if (create1 < create2)
3359eda14cbcSMatt Macy 		rv = -1;
3360eda14cbcSMatt Macy 	else if (create1 > create2)
3361eda14cbcSMatt Macy 		rv = +1;
3362eda14cbcSMatt Macy 	else
3363eda14cbcSMatt Macy 		rv = 0;
3364eda14cbcSMatt Macy 
3365eda14cbcSMatt Macy 	zfs_close(guid1hdl);
3366eda14cbcSMatt Macy 	zfs_close(guid2hdl);
3367eda14cbcSMatt Macy 
3368eda14cbcSMatt Macy 	return (rv);
3369eda14cbcSMatt Macy }
3370eda14cbcSMatt Macy 
3371eda14cbcSMatt Macy /*
3372eda14cbcSMatt Macy  * This function reestablishes the hierarchy of encryption roots after a
3373eda14cbcSMatt Macy  * recursive incremental receive has completed. This must be done after the
3374eda14cbcSMatt Macy  * second call to recv_incremental_replication() has renamed and promoted all
3375eda14cbcSMatt Macy  * sent datasets to their final locations in the dataset hierarchy.
3376eda14cbcSMatt Macy  */
3377eda14cbcSMatt Macy static int
3378eda14cbcSMatt Macy recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *top_zfs,
3379c03c5b1cSMartin Matuska     nvlist_t *stream_nv)
3380eda14cbcSMatt Macy {
3381eda14cbcSMatt Macy 	int err;
3382eda14cbcSMatt Macy 	nvpair_t *fselem = NULL;
3383eda14cbcSMatt Macy 	nvlist_t *stream_fss;
3384eda14cbcSMatt Macy 
3385184c1b94SMartin Matuska 	stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3386eda14cbcSMatt Macy 
3387eda14cbcSMatt Macy 	while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
3388eda14cbcSMatt Macy 		zfs_handle_t *zhp = NULL;
3389eda14cbcSMatt Macy 		uint64_t crypt;
3390eda14cbcSMatt Macy 		nvlist_t *snaps, *props, *stream_nvfs = NULL;
3391eda14cbcSMatt Macy 		nvpair_t *snapel = NULL;
3392eda14cbcSMatt Macy 		boolean_t is_encroot, is_clone, stream_encroot;
3393eda14cbcSMatt Macy 		char *cp;
33942a58b312SMartin Matuska 		const char *stream_keylocation = NULL;
3395eda14cbcSMatt Macy 		char keylocation[MAXNAMELEN];
3396eda14cbcSMatt Macy 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
3397eda14cbcSMatt Macy 
3398eda14cbcSMatt Macy 		keylocation[0] = '\0';
3399184c1b94SMartin Matuska 		stream_nvfs = fnvpair_value_nvlist(fselem);
3400184c1b94SMartin Matuska 		snaps = fnvlist_lookup_nvlist(stream_nvfs, "snaps");
3401184c1b94SMartin Matuska 		props = fnvlist_lookup_nvlist(stream_nvfs, "props");
3402eda14cbcSMatt Macy 		stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
3403eda14cbcSMatt Macy 
3404eda14cbcSMatt Macy 		/* find a snapshot from the stream that exists locally */
3405eda14cbcSMatt Macy 		err = ENOENT;
3406eda14cbcSMatt Macy 		while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
3407eda14cbcSMatt Macy 			uint64_t guid;
3408eda14cbcSMatt Macy 
3409184c1b94SMartin Matuska 			guid = fnvpair_value_uint64(snapel);
3410eda14cbcSMatt Macy 			err = guid_to_name(hdl, top_zfs, guid, B_FALSE,
3411eda14cbcSMatt Macy 			    fsname);
3412eda14cbcSMatt Macy 			if (err == 0)
3413eda14cbcSMatt Macy 				break;
3414eda14cbcSMatt Macy 		}
3415eda14cbcSMatt Macy 
3416eda14cbcSMatt Macy 		if (err != 0)
3417eda14cbcSMatt Macy 			continue;
3418eda14cbcSMatt Macy 
3419eda14cbcSMatt Macy 		cp = strchr(fsname, '@');
3420eda14cbcSMatt Macy 		if (cp != NULL)
3421eda14cbcSMatt Macy 			*cp = '\0';
3422eda14cbcSMatt Macy 
3423eda14cbcSMatt Macy 		zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
3424eda14cbcSMatt Macy 		if (zhp == NULL) {
3425eda14cbcSMatt Macy 			err = ENOENT;
3426eda14cbcSMatt Macy 			goto error;
3427eda14cbcSMatt Macy 		}
3428eda14cbcSMatt Macy 
3429eda14cbcSMatt Macy 		crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
3430eda14cbcSMatt Macy 		is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
3431eda14cbcSMatt Macy 		(void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
3432eda14cbcSMatt Macy 
3433eda14cbcSMatt Macy 		/* we don't need to do anything for unencrypted datasets */
3434eda14cbcSMatt Macy 		if (crypt == ZIO_CRYPT_OFF) {
3435eda14cbcSMatt Macy 			zfs_close(zhp);
3436eda14cbcSMatt Macy 			continue;
3437eda14cbcSMatt Macy 		}
3438eda14cbcSMatt Macy 
3439eda14cbcSMatt Macy 		/*
3440eda14cbcSMatt Macy 		 * If the dataset is flagged as an encryption root, was not
3441eda14cbcSMatt Macy 		 * received as a clone and is not currently an encryption root,
3442eda14cbcSMatt Macy 		 * force it to become one. Fixup the keylocation if necessary.
3443eda14cbcSMatt Macy 		 */
3444eda14cbcSMatt Macy 		if (stream_encroot) {
3445eda14cbcSMatt Macy 			if (!is_clone && !is_encroot) {
3446eda14cbcSMatt Macy 				err = lzc_change_key(fsname,
3447eda14cbcSMatt Macy 				    DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
3448eda14cbcSMatt Macy 				if (err != 0) {
3449eda14cbcSMatt Macy 					zfs_close(zhp);
3450eda14cbcSMatt Macy 					goto error;
3451eda14cbcSMatt Macy 				}
3452eda14cbcSMatt Macy 			}
3453eda14cbcSMatt Macy 
3454184c1b94SMartin Matuska 			stream_keylocation = fnvlist_lookup_string(props,
3455184c1b94SMartin Matuska 			    zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
3456eda14cbcSMatt Macy 
3457eda14cbcSMatt Macy 			/*
3458eda14cbcSMatt Macy 			 * Refresh the properties in case the call to
3459eda14cbcSMatt Macy 			 * lzc_change_key() changed the value.
3460eda14cbcSMatt Macy 			 */
3461eda14cbcSMatt Macy 			zfs_refresh_properties(zhp);
3462eda14cbcSMatt Macy 			err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
3463eda14cbcSMatt Macy 			    keylocation, sizeof (keylocation), NULL, NULL,
3464eda14cbcSMatt Macy 			    0, B_TRUE);
3465eda14cbcSMatt Macy 			if (err != 0) {
3466eda14cbcSMatt Macy 				zfs_close(zhp);
3467eda14cbcSMatt Macy 				goto error;
3468eda14cbcSMatt Macy 			}
3469eda14cbcSMatt Macy 
3470eda14cbcSMatt Macy 			if (strcmp(keylocation, stream_keylocation) != 0) {
3471eda14cbcSMatt Macy 				err = zfs_prop_set(zhp,
3472eda14cbcSMatt Macy 				    zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
3473eda14cbcSMatt Macy 				    stream_keylocation);
3474eda14cbcSMatt Macy 				if (err != 0) {
3475eda14cbcSMatt Macy 					zfs_close(zhp);
3476eda14cbcSMatt Macy 					goto error;
3477eda14cbcSMatt Macy 				}
3478eda14cbcSMatt Macy 			}
3479eda14cbcSMatt Macy 		}
3480eda14cbcSMatt Macy 
3481eda14cbcSMatt Macy 		/*
3482eda14cbcSMatt Macy 		 * If the dataset is not flagged as an encryption root and is
3483eda14cbcSMatt Macy 		 * currently an encryption root, force it to inherit from its
3484eda14cbcSMatt Macy 		 * parent. The root of a raw send should never be
3485eda14cbcSMatt Macy 		 * force-inherited.
3486eda14cbcSMatt Macy 		 */
3487eda14cbcSMatt Macy 		if (!stream_encroot && is_encroot &&
3488eda14cbcSMatt Macy 		    strcmp(top_zfs, fsname) != 0) {
3489eda14cbcSMatt Macy 			err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
3490eda14cbcSMatt Macy 			    NULL, NULL, 0);
3491eda14cbcSMatt Macy 			if (err != 0) {
3492eda14cbcSMatt Macy 				zfs_close(zhp);
3493eda14cbcSMatt Macy 				goto error;
3494eda14cbcSMatt Macy 			}
3495eda14cbcSMatt Macy 		}
3496eda14cbcSMatt Macy 
3497eda14cbcSMatt Macy 		zfs_close(zhp);
3498eda14cbcSMatt Macy 	}
3499eda14cbcSMatt Macy 
3500eda14cbcSMatt Macy 	return (0);
3501eda14cbcSMatt Macy 
3502eda14cbcSMatt Macy error:
3503eda14cbcSMatt Macy 	return (err);
3504eda14cbcSMatt Macy }
3505eda14cbcSMatt Macy 
3506eda14cbcSMatt Macy static int
3507eda14cbcSMatt Macy recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
3508eda14cbcSMatt Macy     recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3509eda14cbcSMatt Macy     nvlist_t *renamed)
3510eda14cbcSMatt Macy {
3511eda14cbcSMatt Macy 	nvlist_t *local_nv, *deleted = NULL;
3512eda14cbcSMatt Macy 	avl_tree_t *local_avl;
3513eda14cbcSMatt Macy 	nvpair_t *fselem, *nextfselem;
35142a58b312SMartin Matuska 	const char *fromsnap;
3515eda14cbcSMatt Macy 	char newname[ZFS_MAX_DATASET_NAME_LEN];
3516eda14cbcSMatt Macy 	char guidname[32];
3517eda14cbcSMatt Macy 	int error;
3518eda14cbcSMatt Macy 	boolean_t needagain, progress, recursive;
35192a58b312SMartin Matuska 	const char *s1, *s2;
3520eda14cbcSMatt Macy 
3521184c1b94SMartin Matuska 	fromsnap = fnvlist_lookup_string(stream_nv, "fromsnap");
3522eda14cbcSMatt Macy 
3523eda14cbcSMatt Macy 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3524eda14cbcSMatt Macy 	    ENOENT);
3525eda14cbcSMatt Macy 
3526eda14cbcSMatt Macy 	if (flags->dryrun)
3527eda14cbcSMatt Macy 		return (0);
3528eda14cbcSMatt Macy 
3529eda14cbcSMatt Macy again:
3530eda14cbcSMatt Macy 	needagain = progress = B_FALSE;
3531eda14cbcSMatt Macy 
3532184c1b94SMartin Matuska 	deleted = fnvlist_alloc();
3533eda14cbcSMatt Macy 
3534eda14cbcSMatt Macy 	if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
353516038816SMartin Matuska 	    recursive, B_TRUE, B_FALSE, recursive, B_FALSE, B_FALSE, B_FALSE,
3536eda14cbcSMatt Macy 	    B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
3537eda14cbcSMatt Macy 		return (error);
3538eda14cbcSMatt Macy 
3539eda14cbcSMatt Macy 	/*
3540eda14cbcSMatt Macy 	 * Process deletes and renames
3541eda14cbcSMatt Macy 	 */
3542eda14cbcSMatt Macy 	for (fselem = nvlist_next_nvpair(local_nv, NULL);
3543eda14cbcSMatt Macy 	    fselem; fselem = nextfselem) {
3544eda14cbcSMatt Macy 		nvlist_t *nvfs, *snaps;
3545eda14cbcSMatt Macy 		nvlist_t *stream_nvfs = NULL;
3546eda14cbcSMatt Macy 		nvpair_t *snapelem, *nextsnapelem;
3547eda14cbcSMatt Macy 		uint64_t fromguid = 0;
3548eda14cbcSMatt Macy 		uint64_t originguid = 0;
3549eda14cbcSMatt Macy 		uint64_t stream_originguid = 0;
3550eda14cbcSMatt Macy 		uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
35512a58b312SMartin Matuska 		const char *fsname, *stream_fsname;
3552eda14cbcSMatt Macy 
3553eda14cbcSMatt Macy 		nextfselem = nvlist_next_nvpair(local_nv, fselem);
3554eda14cbcSMatt Macy 
3555184c1b94SMartin Matuska 		nvfs = fnvpair_value_nvlist(fselem);
3556184c1b94SMartin Matuska 		snaps = fnvlist_lookup_nvlist(nvfs, "snaps");
3557184c1b94SMartin Matuska 		fsname = fnvlist_lookup_string(nvfs, "name");
3558184c1b94SMartin Matuska 		parent_fromsnap_guid = fnvlist_lookup_uint64(nvfs,
3559184c1b94SMartin Matuska 		    "parentfromsnap");
3560eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
3561eda14cbcSMatt Macy 
3562eda14cbcSMatt Macy 		/*
3563eda14cbcSMatt Macy 		 * First find the stream's fs, so we can check for
3564eda14cbcSMatt Macy 		 * a different origin (due to "zfs promote")
3565eda14cbcSMatt Macy 		 */
3566eda14cbcSMatt Macy 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
3567eda14cbcSMatt Macy 		    snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3568eda14cbcSMatt Macy 			uint64_t thisguid;
3569eda14cbcSMatt Macy 
3570184c1b94SMartin Matuska 			thisguid = fnvpair_value_uint64(snapelem);
3571eda14cbcSMatt Macy 			stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3572eda14cbcSMatt Macy 
3573eda14cbcSMatt Macy 			if (stream_nvfs != NULL)
3574eda14cbcSMatt Macy 				break;
3575eda14cbcSMatt Macy 		}
3576eda14cbcSMatt Macy 
3577eda14cbcSMatt Macy 		/* check for promote */
3578eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(stream_nvfs, "origin",
3579eda14cbcSMatt Macy 		    &stream_originguid);
3580eda14cbcSMatt Macy 		if (stream_nvfs && originguid != stream_originguid) {
3581eda14cbcSMatt Macy 			switch (created_before(hdl, local_avl,
3582eda14cbcSMatt Macy 			    stream_originguid, originguid)) {
3583eda14cbcSMatt Macy 			case 1: {
3584eda14cbcSMatt Macy 				/* promote it! */
3585eda14cbcSMatt Macy 				nvlist_t *origin_nvfs;
35862a58b312SMartin Matuska 				const char *origin_fsname;
3587eda14cbcSMatt Macy 
3588eda14cbcSMatt Macy 				origin_nvfs = fsavl_find(local_avl, originguid,
3589eda14cbcSMatt Macy 				    NULL);
3590184c1b94SMartin Matuska 				origin_fsname = fnvlist_lookup_string(
3591184c1b94SMartin Matuska 				    origin_nvfs, "name");
3592eda14cbcSMatt Macy 				error = recv_promote(hdl, fsname, origin_fsname,
3593eda14cbcSMatt Macy 				    flags);
3594eda14cbcSMatt Macy 				if (error == 0)
3595eda14cbcSMatt Macy 					progress = B_TRUE;
3596eda14cbcSMatt Macy 				break;
3597eda14cbcSMatt Macy 			}
3598eda14cbcSMatt Macy 			default:
3599eda14cbcSMatt Macy 				break;
3600eda14cbcSMatt Macy 			case -1:
3601eda14cbcSMatt Macy 				fsavl_destroy(local_avl);
3602184c1b94SMartin Matuska 				fnvlist_free(local_nv);
3603eda14cbcSMatt Macy 				return (-1);
3604eda14cbcSMatt Macy 			}
3605eda14cbcSMatt Macy 			/*
3606eda14cbcSMatt Macy 			 * We had/have the wrong origin, therefore our
3607eda14cbcSMatt Macy 			 * list of snapshots is wrong.  Need to handle
3608eda14cbcSMatt Macy 			 * them on the next pass.
3609eda14cbcSMatt Macy 			 */
3610eda14cbcSMatt Macy 			needagain = B_TRUE;
3611eda14cbcSMatt Macy 			continue;
3612eda14cbcSMatt Macy 		}
3613eda14cbcSMatt Macy 
3614eda14cbcSMatt Macy 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
3615eda14cbcSMatt Macy 		    snapelem; snapelem = nextsnapelem) {
3616eda14cbcSMatt Macy 			uint64_t thisguid;
36172a58b312SMartin Matuska 			const char *stream_snapname;
3618eda14cbcSMatt Macy 			nvlist_t *found, *props;
3619eda14cbcSMatt Macy 
3620eda14cbcSMatt Macy 			nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
3621eda14cbcSMatt Macy 
3622184c1b94SMartin Matuska 			thisguid = fnvpair_value_uint64(snapelem);
3623eda14cbcSMatt Macy 			found = fsavl_find(stream_avl, thisguid,
3624eda14cbcSMatt Macy 			    &stream_snapname);
3625eda14cbcSMatt Macy 
3626eda14cbcSMatt Macy 			/* check for delete */
3627eda14cbcSMatt Macy 			if (found == NULL) {
3628eda14cbcSMatt Macy 				char name[ZFS_MAX_DATASET_NAME_LEN];
3629eda14cbcSMatt Macy 
3630eda14cbcSMatt Macy 				if (!flags->force)
3631eda14cbcSMatt Macy 					continue;
3632eda14cbcSMatt Macy 
3633eda14cbcSMatt Macy 				(void) snprintf(name, sizeof (name), "%s@%s",
3634eda14cbcSMatt Macy 				    fsname, nvpair_name(snapelem));
3635eda14cbcSMatt Macy 
3636eda14cbcSMatt Macy 				error = recv_destroy(hdl, name,
3637eda14cbcSMatt Macy 				    strlen(fsname)+1, newname, flags);
3638eda14cbcSMatt Macy 				if (error)
3639eda14cbcSMatt Macy 					needagain = B_TRUE;
3640eda14cbcSMatt Macy 				else
3641eda14cbcSMatt Macy 					progress = B_TRUE;
3642eda14cbcSMatt Macy 				sprintf(guidname, "%llu",
3643eda14cbcSMatt Macy 				    (u_longlong_t)thisguid);
3644eda14cbcSMatt Macy 				nvlist_add_boolean(deleted, guidname);
3645eda14cbcSMatt Macy 				continue;
3646eda14cbcSMatt Macy 			}
3647eda14cbcSMatt Macy 
3648eda14cbcSMatt Macy 			stream_nvfs = found;
3649eda14cbcSMatt Macy 
3650eda14cbcSMatt Macy 			if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
3651eda14cbcSMatt Macy 			    &props) && 0 == nvlist_lookup_nvlist(props,
3652eda14cbcSMatt Macy 			    stream_snapname, &props)) {
3653eda14cbcSMatt Macy 				zfs_cmd_t zc = {"\0"};
3654eda14cbcSMatt Macy 
3655eda14cbcSMatt Macy 				zc.zc_cookie = B_TRUE; /* received */
3656eda14cbcSMatt Macy 				(void) snprintf(zc.zc_name, sizeof (zc.zc_name),
3657eda14cbcSMatt Macy 				    "%s@%s", fsname, nvpair_name(snapelem));
3658716fd348SMartin Matuska 				zcmd_write_src_nvlist(hdl, &zc, props);
3659eda14cbcSMatt Macy 				(void) zfs_ioctl(hdl,
3660eda14cbcSMatt Macy 				    ZFS_IOC_SET_PROP, &zc);
3661eda14cbcSMatt Macy 				zcmd_free_nvlists(&zc);
3662eda14cbcSMatt Macy 			}
3663eda14cbcSMatt Macy 
3664eda14cbcSMatt Macy 			/* check for different snapname */
3665eda14cbcSMatt Macy 			if (strcmp(nvpair_name(snapelem),
3666eda14cbcSMatt Macy 			    stream_snapname) != 0) {
3667eda14cbcSMatt Macy 				char name[ZFS_MAX_DATASET_NAME_LEN];
3668eda14cbcSMatt Macy 				char tryname[ZFS_MAX_DATASET_NAME_LEN];
3669eda14cbcSMatt Macy 
3670eda14cbcSMatt Macy 				(void) snprintf(name, sizeof (name), "%s@%s",
3671eda14cbcSMatt Macy 				    fsname, nvpair_name(snapelem));
3672eda14cbcSMatt Macy 				(void) snprintf(tryname, sizeof (name), "%s@%s",
3673eda14cbcSMatt Macy 				    fsname, stream_snapname);
3674eda14cbcSMatt Macy 
3675eda14cbcSMatt Macy 				error = recv_rename(hdl, name, tryname,
3676eda14cbcSMatt Macy 				    strlen(fsname)+1, newname, flags);
3677eda14cbcSMatt Macy 				if (error)
3678eda14cbcSMatt Macy 					needagain = B_TRUE;
3679eda14cbcSMatt Macy 				else
3680eda14cbcSMatt Macy 					progress = B_TRUE;
3681eda14cbcSMatt Macy 			}
3682eda14cbcSMatt Macy 
3683eda14cbcSMatt Macy 			if (strcmp(stream_snapname, fromsnap) == 0)
3684eda14cbcSMatt Macy 				fromguid = thisguid;
3685eda14cbcSMatt Macy 		}
3686eda14cbcSMatt Macy 
3687eda14cbcSMatt Macy 		/* check for delete */
3688eda14cbcSMatt Macy 		if (stream_nvfs == NULL) {
3689eda14cbcSMatt Macy 			if (!flags->force)
3690eda14cbcSMatt Macy 				continue;
3691eda14cbcSMatt Macy 
3692eda14cbcSMatt Macy 			error = recv_destroy(hdl, fsname, strlen(tofs)+1,
3693eda14cbcSMatt Macy 			    newname, flags);
3694eda14cbcSMatt Macy 			if (error)
3695eda14cbcSMatt Macy 				needagain = B_TRUE;
3696eda14cbcSMatt Macy 			else
3697eda14cbcSMatt Macy 				progress = B_TRUE;
3698eda14cbcSMatt Macy 			sprintf(guidname, "%llu",
3699eda14cbcSMatt Macy 			    (u_longlong_t)parent_fromsnap_guid);
3700eda14cbcSMatt Macy 			nvlist_add_boolean(deleted, guidname);
3701eda14cbcSMatt Macy 			continue;
3702eda14cbcSMatt Macy 		}
3703eda14cbcSMatt Macy 
3704eda14cbcSMatt Macy 		if (fromguid == 0) {
3705eda14cbcSMatt Macy 			if (flags->verbose) {
3706eda14cbcSMatt Macy 				(void) printf("local fs %s does not have "
3707eda14cbcSMatt Macy 				    "fromsnap (%s in stream); must have "
3708eda14cbcSMatt Macy 				    "been deleted locally; ignoring\n",
3709eda14cbcSMatt Macy 				    fsname, fromsnap);
3710eda14cbcSMatt Macy 			}
3711eda14cbcSMatt Macy 			continue;
3712eda14cbcSMatt Macy 		}
3713eda14cbcSMatt Macy 
3714184c1b94SMartin Matuska 		stream_fsname = fnvlist_lookup_string(stream_nvfs, "name");
3715184c1b94SMartin Matuska 		stream_parent_fromsnap_guid = fnvlist_lookup_uint64(
3716184c1b94SMartin Matuska 		    stream_nvfs, "parentfromsnap");
3717eda14cbcSMatt Macy 
3718eda14cbcSMatt Macy 		s1 = strrchr(fsname, '/');
3719eda14cbcSMatt Macy 		s2 = strrchr(stream_fsname, '/');
3720eda14cbcSMatt Macy 
3721eda14cbcSMatt Macy 		/*
3722eda14cbcSMatt Macy 		 * Check if we're going to rename based on parent guid change
3723eda14cbcSMatt Macy 		 * and the current parent guid was also deleted. If it was then
3724eda14cbcSMatt Macy 		 * rename will fail and is likely unneeded, so avoid this and
3725eda14cbcSMatt Macy 		 * force an early retry to determine the new
3726eda14cbcSMatt Macy 		 * parent_fromsnap_guid.
3727eda14cbcSMatt Macy 		 */
3728eda14cbcSMatt Macy 		if (stream_parent_fromsnap_guid != 0 &&
3729eda14cbcSMatt Macy 		    parent_fromsnap_guid != 0 &&
3730eda14cbcSMatt Macy 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) {
3731eda14cbcSMatt Macy 			sprintf(guidname, "%llu",
3732eda14cbcSMatt Macy 			    (u_longlong_t)parent_fromsnap_guid);
3733eda14cbcSMatt Macy 			if (nvlist_exists(deleted, guidname)) {
3734eda14cbcSMatt Macy 				progress = B_TRUE;
3735eda14cbcSMatt Macy 				needagain = B_TRUE;
3736eda14cbcSMatt Macy 				goto doagain;
3737eda14cbcSMatt Macy 			}
3738eda14cbcSMatt Macy 		}
3739eda14cbcSMatt Macy 
3740eda14cbcSMatt Macy 		/*
3741eda14cbcSMatt Macy 		 * Check for rename. If the exact receive path is specified, it
3742eda14cbcSMatt Macy 		 * does not count as a rename, but we still need to check the
3743eda14cbcSMatt Macy 		 * datasets beneath it.
3744eda14cbcSMatt Macy 		 */
3745eda14cbcSMatt Macy 		if ((stream_parent_fromsnap_guid != 0 &&
3746eda14cbcSMatt Macy 		    parent_fromsnap_guid != 0 &&
3747eda14cbcSMatt Macy 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
3748eda14cbcSMatt Macy 		    ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3749eda14cbcSMatt Macy 		    (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3750eda14cbcSMatt Macy 			nvlist_t *parent;
3751eda14cbcSMatt Macy 			char tryname[ZFS_MAX_DATASET_NAME_LEN];
3752eda14cbcSMatt Macy 
3753eda14cbcSMatt Macy 			parent = fsavl_find(local_avl,
3754eda14cbcSMatt Macy 			    stream_parent_fromsnap_guid, NULL);
3755eda14cbcSMatt Macy 			/*
3756eda14cbcSMatt Macy 			 * NB: parent might not be found if we used the
3757eda14cbcSMatt Macy 			 * tosnap for stream_parent_fromsnap_guid,
3758eda14cbcSMatt Macy 			 * because the parent is a newly-created fs;
3759eda14cbcSMatt Macy 			 * we'll be able to rename it after we recv the
3760eda14cbcSMatt Macy 			 * new fs.
3761eda14cbcSMatt Macy 			 */
3762eda14cbcSMatt Macy 			if (parent != NULL) {
37632a58b312SMartin Matuska 				const char *pname;
3764eda14cbcSMatt Macy 
3765184c1b94SMartin Matuska 				pname = fnvlist_lookup_string(parent, "name");
3766eda14cbcSMatt Macy 				(void) snprintf(tryname, sizeof (tryname),
3767eda14cbcSMatt Macy 				    "%s%s", pname, strrchr(stream_fsname, '/'));
3768eda14cbcSMatt Macy 			} else {
3769eda14cbcSMatt Macy 				tryname[0] = '\0';
3770eda14cbcSMatt Macy 				if (flags->verbose) {
3771eda14cbcSMatt Macy 					(void) printf("local fs %s new parent "
3772eda14cbcSMatt Macy 					    "not found\n", fsname);
3773eda14cbcSMatt Macy 				}
3774eda14cbcSMatt Macy 			}
3775eda14cbcSMatt Macy 
3776eda14cbcSMatt Macy 			newname[0] = '\0';
3777eda14cbcSMatt Macy 
3778eda14cbcSMatt Macy 			error = recv_rename(hdl, fsname, tryname,
3779eda14cbcSMatt Macy 			    strlen(tofs)+1, newname, flags);
3780eda14cbcSMatt Macy 
3781eda14cbcSMatt Macy 			if (renamed != NULL && newname[0] != '\0') {
3782184c1b94SMartin Matuska 				fnvlist_add_boolean(renamed, newname);
3783eda14cbcSMatt Macy 			}
3784eda14cbcSMatt Macy 
3785eda14cbcSMatt Macy 			if (error)
3786eda14cbcSMatt Macy 				needagain = B_TRUE;
3787eda14cbcSMatt Macy 			else
3788eda14cbcSMatt Macy 				progress = B_TRUE;
3789eda14cbcSMatt Macy 		}
3790eda14cbcSMatt Macy 	}
3791eda14cbcSMatt Macy 
3792eda14cbcSMatt Macy doagain:
3793eda14cbcSMatt Macy 	fsavl_destroy(local_avl);
3794184c1b94SMartin Matuska 	fnvlist_free(local_nv);
3795184c1b94SMartin Matuska 	fnvlist_free(deleted);
3796eda14cbcSMatt Macy 
3797eda14cbcSMatt Macy 	if (needagain && progress) {
3798eda14cbcSMatt Macy 		/* do another pass to fix up temporary names */
3799eda14cbcSMatt Macy 		if (flags->verbose)
3800eda14cbcSMatt Macy 			(void) printf("another pass:\n");
3801eda14cbcSMatt Macy 		goto again;
3802eda14cbcSMatt Macy 	}
3803eda14cbcSMatt Macy 
3804eda14cbcSMatt Macy 	return (needagain || error != 0);
3805eda14cbcSMatt Macy }
3806eda14cbcSMatt Macy 
3807eda14cbcSMatt Macy static int
3808eda14cbcSMatt Macy zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
3809eda14cbcSMatt Macy     recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3810eda14cbcSMatt Macy     char **top_zfs, nvlist_t *cmdprops)
3811eda14cbcSMatt Macy {
3812eda14cbcSMatt Macy 	nvlist_t *stream_nv = NULL;
3813eda14cbcSMatt Macy 	avl_tree_t *stream_avl = NULL;
38142a58b312SMartin Matuska 	const char *fromsnap = NULL;
38152a58b312SMartin Matuska 	const char *sendsnap = NULL;
3816eda14cbcSMatt Macy 	char *cp;
3817eda14cbcSMatt Macy 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
3818eda14cbcSMatt Macy 	char sendfs[ZFS_MAX_DATASET_NAME_LEN];
38191f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
3820eda14cbcSMatt Macy 	dmu_replay_record_t drre;
3821eda14cbcSMatt Macy 	int error;
3822eda14cbcSMatt Macy 	boolean_t anyerr = B_FALSE;
3823eda14cbcSMatt Macy 	boolean_t softerr = B_FALSE;
3824eda14cbcSMatt Macy 	boolean_t recursive, raw;
3825eda14cbcSMatt Macy 
3826eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3827eda14cbcSMatt Macy 	    "cannot receive"));
3828eda14cbcSMatt Macy 
3829eda14cbcSMatt Macy 	assert(drr->drr_type == DRR_BEGIN);
3830eda14cbcSMatt Macy 	assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
3831eda14cbcSMatt Macy 	assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3832eda14cbcSMatt Macy 	    DMU_COMPOUNDSTREAM);
3833eda14cbcSMatt Macy 
3834eda14cbcSMatt Macy 	/*
3835eda14cbcSMatt Macy 	 * Read in the nvlist from the stream.
3836eda14cbcSMatt Macy 	 */
3837eda14cbcSMatt Macy 	if (drr->drr_payloadlen != 0) {
3838eda14cbcSMatt Macy 		error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
3839eda14cbcSMatt Macy 		    &stream_nv, flags->byteswap, zc);
3840eda14cbcSMatt Macy 		if (error) {
3841eda14cbcSMatt Macy 			error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3842eda14cbcSMatt Macy 			goto out;
3843eda14cbcSMatt Macy 		}
3844eda14cbcSMatt Macy 	}
3845eda14cbcSMatt Macy 
3846eda14cbcSMatt Macy 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3847eda14cbcSMatt Macy 	    ENOENT);
3848eda14cbcSMatt Macy 	raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3849eda14cbcSMatt Macy 
3850eda14cbcSMatt Macy 	if (recursive && strchr(destname, '@')) {
3851eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3852eda14cbcSMatt Macy 		    "cannot specify snapshot name for multi-snapshot stream"));
3853eda14cbcSMatt Macy 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3854eda14cbcSMatt Macy 		goto out;
3855eda14cbcSMatt Macy 	}
3856eda14cbcSMatt Macy 
3857eda14cbcSMatt Macy 	/*
3858eda14cbcSMatt Macy 	 * Read in the end record and verify checksum.
3859eda14cbcSMatt Macy 	 */
3860eda14cbcSMatt Macy 	if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3861eda14cbcSMatt Macy 	    flags->byteswap, NULL)))
3862eda14cbcSMatt Macy 		goto out;
3863eda14cbcSMatt Macy 	if (flags->byteswap) {
3864eda14cbcSMatt Macy 		drre.drr_type = BSWAP_32(drre.drr_type);
3865eda14cbcSMatt Macy 		drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3866eda14cbcSMatt Macy 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3867eda14cbcSMatt Macy 		drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3868eda14cbcSMatt Macy 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3869eda14cbcSMatt Macy 		drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3870eda14cbcSMatt Macy 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3871eda14cbcSMatt Macy 		drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3872eda14cbcSMatt Macy 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3873eda14cbcSMatt Macy 	}
3874eda14cbcSMatt Macy 	if (drre.drr_type != DRR_END) {
3875eda14cbcSMatt Macy 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3876eda14cbcSMatt Macy 		goto out;
3877eda14cbcSMatt Macy 	}
3878eda14cbcSMatt Macy 	if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3879eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3880eda14cbcSMatt Macy 		    "incorrect header checksum"));
3881eda14cbcSMatt Macy 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3882eda14cbcSMatt Macy 		goto out;
3883eda14cbcSMatt Macy 	}
3884eda14cbcSMatt Macy 
3885eda14cbcSMatt Macy 	(void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3886eda14cbcSMatt Macy 
3887eda14cbcSMatt Macy 	if (drr->drr_payloadlen != 0) {
3888eda14cbcSMatt Macy 		nvlist_t *stream_fss;
3889eda14cbcSMatt Macy 
3890184c1b94SMartin Matuska 		stream_fss = fnvlist_lookup_nvlist(stream_nv, "fss");
3891eda14cbcSMatt Macy 		if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3892eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3893eda14cbcSMatt Macy 			    "couldn't allocate avl tree"));
3894eda14cbcSMatt Macy 			error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3895eda14cbcSMatt Macy 			goto out;
3896eda14cbcSMatt Macy 		}
3897eda14cbcSMatt Macy 
3898eda14cbcSMatt Macy 		if (fromsnap != NULL && recursive) {
3899eda14cbcSMatt Macy 			nvlist_t *renamed = NULL;
3900eda14cbcSMatt Macy 			nvpair_t *pair = NULL;
3901eda14cbcSMatt Macy 
3902eda14cbcSMatt Macy 			(void) strlcpy(tofs, destname, sizeof (tofs));
3903eda14cbcSMatt Macy 			if (flags->isprefix) {
3904eda14cbcSMatt Macy 				struct drr_begin *drrb = &drr->drr_u.drr_begin;
3905eda14cbcSMatt Macy 				int i;
3906eda14cbcSMatt Macy 
3907eda14cbcSMatt Macy 				if (flags->istail) {
3908eda14cbcSMatt Macy 					cp = strrchr(drrb->drr_toname, '/');
3909eda14cbcSMatt Macy 					if (cp == NULL) {
3910eda14cbcSMatt Macy 						(void) strlcat(tofs, "/",
3911eda14cbcSMatt Macy 						    sizeof (tofs));
3912eda14cbcSMatt Macy 						i = 0;
3913eda14cbcSMatt Macy 					} else {
3914eda14cbcSMatt Macy 						i = (cp - drrb->drr_toname);
3915eda14cbcSMatt Macy 					}
3916eda14cbcSMatt Macy 				} else {
3917eda14cbcSMatt Macy 					i = strcspn(drrb->drr_toname, "/@");
3918eda14cbcSMatt Macy 				}
3919eda14cbcSMatt Macy 				/* zfs_receive_one() will create_parents() */
3920eda14cbcSMatt Macy 				(void) strlcat(tofs, &drrb->drr_toname[i],
3921eda14cbcSMatt Macy 				    sizeof (tofs));
3922eda14cbcSMatt Macy 				*strchr(tofs, '@') = '\0';
3923eda14cbcSMatt Macy 			}
3924eda14cbcSMatt Macy 
3925eda14cbcSMatt Macy 			if (!flags->dryrun && !flags->nomount) {
3926184c1b94SMartin Matuska 				renamed = fnvlist_alloc();
3927eda14cbcSMatt Macy 			}
3928eda14cbcSMatt Macy 
3929eda14cbcSMatt Macy 			softerr = recv_incremental_replication(hdl, tofs, flags,
3930eda14cbcSMatt Macy 			    stream_nv, stream_avl, renamed);
3931eda14cbcSMatt Macy 
3932eda14cbcSMatt Macy 			/* Unmount renamed filesystems before receiving. */
3933eda14cbcSMatt Macy 			while ((pair = nvlist_next_nvpair(renamed,
3934eda14cbcSMatt Macy 			    pair)) != NULL) {
3935eda14cbcSMatt Macy 				zfs_handle_t *zhp;
3936eda14cbcSMatt Macy 				prop_changelist_t *clp = NULL;
3937eda14cbcSMatt Macy 
3938eda14cbcSMatt Macy 				zhp = zfs_open(hdl, nvpair_name(pair),
3939eda14cbcSMatt Macy 				    ZFS_TYPE_FILESYSTEM);
3940eda14cbcSMatt Macy 				if (zhp != NULL) {
3941eda14cbcSMatt Macy 					clp = changelist_gather(zhp,
3942eda14cbcSMatt Macy 					    ZFS_PROP_MOUNTPOINT, 0,
3943eda14cbcSMatt Macy 					    flags->forceunmount ? MS_FORCE : 0);
3944eda14cbcSMatt Macy 					zfs_close(zhp);
3945eda14cbcSMatt Macy 					if (clp != NULL) {
3946eda14cbcSMatt Macy 						softerr |=
3947eda14cbcSMatt Macy 						    changelist_prefix(clp);
3948eda14cbcSMatt Macy 						changelist_free(clp);
3949eda14cbcSMatt Macy 					}
3950eda14cbcSMatt Macy 				}
3951eda14cbcSMatt Macy 			}
3952eda14cbcSMatt Macy 
3953184c1b94SMartin Matuska 			fnvlist_free(renamed);
3954eda14cbcSMatt Macy 		}
3955eda14cbcSMatt Macy 	}
3956eda14cbcSMatt Macy 
3957eda14cbcSMatt Macy 	/*
3958eda14cbcSMatt Macy 	 * Get the fs specified by the first path in the stream (the top level
3959eda14cbcSMatt Macy 	 * specified by 'zfs send') and pass it to each invocation of
3960eda14cbcSMatt Macy 	 * zfs_receive_one().
3961eda14cbcSMatt Macy 	 */
3962eda14cbcSMatt Macy 	(void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
3963eda14cbcSMatt Macy 	    sizeof (sendfs));
3964eda14cbcSMatt Macy 	if ((cp = strchr(sendfs, '@')) != NULL) {
3965eda14cbcSMatt Macy 		*cp = '\0';
3966eda14cbcSMatt Macy 		/*
3967eda14cbcSMatt Macy 		 * Find the "sendsnap", the final snapshot in a replication
3968eda14cbcSMatt Macy 		 * stream.  zfs_receive_one() handles certain errors
3969eda14cbcSMatt Macy 		 * differently, depending on if the contained stream is the
3970eda14cbcSMatt Macy 		 * last one or not.
3971eda14cbcSMatt Macy 		 */
3972eda14cbcSMatt Macy 		sendsnap = (cp + 1);
3973eda14cbcSMatt Macy 	}
3974eda14cbcSMatt Macy 
3975eda14cbcSMatt Macy 	/* Finally, receive each contained stream */
3976eda14cbcSMatt Macy 	do {
3977eda14cbcSMatt Macy 		/*
3978eda14cbcSMatt Macy 		 * we should figure out if it has a recoverable
3979eda14cbcSMatt Macy 		 * error, in which case do a recv_skip() and drive on.
3980eda14cbcSMatt Macy 		 * Note, if we fail due to already having this guid,
3981eda14cbcSMatt Macy 		 * zfs_receive_one() will take care of it (ie,
3982eda14cbcSMatt Macy 		 * recv_skip() and return 0).
3983eda14cbcSMatt Macy 		 */
3984eda14cbcSMatt Macy 		error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3985eda14cbcSMatt Macy 		    sendfs, stream_nv, stream_avl, top_zfs, sendsnap, cmdprops);
3986eda14cbcSMatt Macy 		if (error == ENODATA) {
3987eda14cbcSMatt Macy 			error = 0;
3988eda14cbcSMatt Macy 			break;
3989eda14cbcSMatt Macy 		}
3990eda14cbcSMatt Macy 		anyerr |= error;
3991eda14cbcSMatt Macy 	} while (error == 0);
3992eda14cbcSMatt Macy 
3993eda14cbcSMatt Macy 	if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
3994eda14cbcSMatt Macy 		/*
3995eda14cbcSMatt Macy 		 * Now that we have the fs's they sent us, try the
3996eda14cbcSMatt Macy 		 * renames again.
3997eda14cbcSMatt Macy 		 */
3998eda14cbcSMatt Macy 		softerr = recv_incremental_replication(hdl, tofs, flags,
3999eda14cbcSMatt Macy 		    stream_nv, stream_avl, NULL);
4000eda14cbcSMatt Macy 	}
4001eda14cbcSMatt Macy 
4002eda14cbcSMatt Macy 	if (raw && softerr == 0 && *top_zfs != NULL) {
4003eda14cbcSMatt Macy 		softerr = recv_fix_encryption_hierarchy(hdl, *top_zfs,
4004c03c5b1cSMartin Matuska 		    stream_nv);
4005eda14cbcSMatt Macy 	}
4006eda14cbcSMatt Macy 
4007eda14cbcSMatt Macy out:
4008eda14cbcSMatt Macy 	fsavl_destroy(stream_avl);
4009184c1b94SMartin Matuska 	fnvlist_free(stream_nv);
4010eda14cbcSMatt Macy 	if (softerr)
4011eda14cbcSMatt Macy 		error = -2;
4012eda14cbcSMatt Macy 	if (anyerr)
4013eda14cbcSMatt Macy 		error = -1;
4014eda14cbcSMatt Macy 	return (error);
4015eda14cbcSMatt Macy }
4016eda14cbcSMatt Macy 
4017eda14cbcSMatt Macy static void
4018eda14cbcSMatt Macy trunc_prop_errs(int truncated)
4019eda14cbcSMatt Macy {
4020eda14cbcSMatt Macy 	ASSERT(truncated != 0);
4021eda14cbcSMatt Macy 
4022eda14cbcSMatt Macy 	if (truncated == 1)
4023eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4024eda14cbcSMatt Macy 		    "1 more property could not be set\n"));
4025eda14cbcSMatt Macy 	else
4026eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
4027eda14cbcSMatt Macy 		    "%d more properties could not be set\n"), truncated);
4028eda14cbcSMatt Macy }
4029eda14cbcSMatt Macy 
4030eda14cbcSMatt Macy static int
4031eda14cbcSMatt Macy recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
4032eda14cbcSMatt Macy {
4033eda14cbcSMatt Macy 	dmu_replay_record_t *drr;
4034eda14cbcSMatt Macy 	void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
4035eda14cbcSMatt Macy 	uint64_t payload_size;
40361f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
4037eda14cbcSMatt Macy 
4038eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4039eda14cbcSMatt Macy 	    "cannot receive"));
4040eda14cbcSMatt Macy 
4041eda14cbcSMatt Macy 	/* XXX would be great to use lseek if possible... */
4042eda14cbcSMatt Macy 	drr = buf;
4043eda14cbcSMatt Macy 
4044eda14cbcSMatt Macy 	while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
4045eda14cbcSMatt Macy 	    byteswap, NULL) == 0) {
4046eda14cbcSMatt Macy 		if (byteswap)
4047eda14cbcSMatt Macy 			drr->drr_type = BSWAP_32(drr->drr_type);
4048eda14cbcSMatt Macy 
4049eda14cbcSMatt Macy 		switch (drr->drr_type) {
4050eda14cbcSMatt Macy 		case DRR_BEGIN:
4051eda14cbcSMatt Macy 			if (drr->drr_payloadlen != 0) {
4052eda14cbcSMatt Macy 				(void) recv_read(hdl, fd, buf,
4053eda14cbcSMatt Macy 				    drr->drr_payloadlen, B_FALSE, NULL);
4054eda14cbcSMatt Macy 			}
4055eda14cbcSMatt Macy 			break;
4056eda14cbcSMatt Macy 
4057eda14cbcSMatt Macy 		case DRR_END:
4058eda14cbcSMatt Macy 			free(buf);
4059eda14cbcSMatt Macy 			return (0);
4060eda14cbcSMatt Macy 
4061eda14cbcSMatt Macy 		case DRR_OBJECT:
4062eda14cbcSMatt Macy 			if (byteswap) {
4063eda14cbcSMatt Macy 				drr->drr_u.drr_object.drr_bonuslen =
4064eda14cbcSMatt Macy 				    BSWAP_32(drr->drr_u.drr_object.
4065eda14cbcSMatt Macy 				    drr_bonuslen);
4066eda14cbcSMatt Macy 				drr->drr_u.drr_object.drr_raw_bonuslen =
4067eda14cbcSMatt Macy 				    BSWAP_32(drr->drr_u.drr_object.
4068eda14cbcSMatt Macy 				    drr_raw_bonuslen);
4069eda14cbcSMatt Macy 			}
4070eda14cbcSMatt Macy 
4071eda14cbcSMatt Macy 			payload_size =
4072eda14cbcSMatt Macy 			    DRR_OBJECT_PAYLOAD_SIZE(&drr->drr_u.drr_object);
4073eda14cbcSMatt Macy 			(void) recv_read(hdl, fd, buf, payload_size,
4074eda14cbcSMatt Macy 			    B_FALSE, NULL);
4075eda14cbcSMatt Macy 			break;
4076eda14cbcSMatt Macy 
4077eda14cbcSMatt Macy 		case DRR_WRITE:
4078eda14cbcSMatt Macy 			if (byteswap) {
4079eda14cbcSMatt Macy 				drr->drr_u.drr_write.drr_logical_size =
4080eda14cbcSMatt Macy 				    BSWAP_64(
4081eda14cbcSMatt Macy 				    drr->drr_u.drr_write.drr_logical_size);
4082eda14cbcSMatt Macy 				drr->drr_u.drr_write.drr_compressed_size =
4083eda14cbcSMatt Macy 				    BSWAP_64(
4084eda14cbcSMatt Macy 				    drr->drr_u.drr_write.drr_compressed_size);
4085eda14cbcSMatt Macy 			}
4086eda14cbcSMatt Macy 			payload_size =
4087eda14cbcSMatt Macy 			    DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
4088ac0bf12eSMatt Macy 			assert(payload_size <= SPA_MAXBLOCKSIZE);
4089eda14cbcSMatt Macy 			(void) recv_read(hdl, fd, buf,
4090eda14cbcSMatt Macy 			    payload_size, B_FALSE, NULL);
4091eda14cbcSMatt Macy 			break;
4092eda14cbcSMatt Macy 		case DRR_SPILL:
4093eda14cbcSMatt Macy 			if (byteswap) {
4094eda14cbcSMatt Macy 				drr->drr_u.drr_spill.drr_length =
4095eda14cbcSMatt Macy 				    BSWAP_64(drr->drr_u.drr_spill.drr_length);
4096eda14cbcSMatt Macy 				drr->drr_u.drr_spill.drr_compressed_size =
4097eda14cbcSMatt Macy 				    BSWAP_64(drr->drr_u.drr_spill.
4098eda14cbcSMatt Macy 				    drr_compressed_size);
4099eda14cbcSMatt Macy 			}
4100eda14cbcSMatt Macy 
4101eda14cbcSMatt Macy 			payload_size =
4102eda14cbcSMatt Macy 			    DRR_SPILL_PAYLOAD_SIZE(&drr->drr_u.drr_spill);
4103eda14cbcSMatt Macy 			(void) recv_read(hdl, fd, buf, payload_size,
4104eda14cbcSMatt Macy 			    B_FALSE, NULL);
4105eda14cbcSMatt Macy 			break;
4106eda14cbcSMatt Macy 		case DRR_WRITE_EMBEDDED:
4107eda14cbcSMatt Macy 			if (byteswap) {
4108eda14cbcSMatt Macy 				drr->drr_u.drr_write_embedded.drr_psize =
4109eda14cbcSMatt Macy 				    BSWAP_32(drr->drr_u.drr_write_embedded.
4110eda14cbcSMatt Macy 				    drr_psize);
4111eda14cbcSMatt Macy 			}
4112eda14cbcSMatt Macy 			(void) recv_read(hdl, fd, buf,
4113eda14cbcSMatt Macy 			    P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
4114eda14cbcSMatt Macy 			    8), B_FALSE, NULL);
4115eda14cbcSMatt Macy 			break;
4116eda14cbcSMatt Macy 		case DRR_OBJECT_RANGE:
4117eda14cbcSMatt Macy 		case DRR_WRITE_BYREF:
4118eda14cbcSMatt Macy 		case DRR_FREEOBJECTS:
4119eda14cbcSMatt Macy 		case DRR_FREE:
4120eda14cbcSMatt Macy 			break;
4121eda14cbcSMatt Macy 
4122eda14cbcSMatt Macy 		default:
4123eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4124eda14cbcSMatt Macy 			    "invalid record type"));
4125eda14cbcSMatt Macy 			free(buf);
4126eda14cbcSMatt Macy 			return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4127eda14cbcSMatt Macy 		}
4128eda14cbcSMatt Macy 	}
4129eda14cbcSMatt Macy 
4130eda14cbcSMatt Macy 	free(buf);
4131eda14cbcSMatt Macy 	return (-1);
4132eda14cbcSMatt Macy }
4133eda14cbcSMatt Macy 
4134eda14cbcSMatt Macy static void
4135eda14cbcSMatt Macy recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
4136eda14cbcSMatt Macy     boolean_t resumable, boolean_t checksum)
4137eda14cbcSMatt Macy {
4138eda14cbcSMatt Macy 	char target_fs[ZFS_MAX_DATASET_NAME_LEN];
4139eda14cbcSMatt Macy 
4140eda14cbcSMatt Macy 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, (checksum ?
4141eda14cbcSMatt Macy 	    "checksum mismatch" : "incomplete stream")));
4142eda14cbcSMatt Macy 
4143eda14cbcSMatt Macy 	if (!resumable)
4144eda14cbcSMatt Macy 		return;
4145eda14cbcSMatt Macy 	(void) strlcpy(target_fs, target_snap, sizeof (target_fs));
4146eda14cbcSMatt Macy 	*strchr(target_fs, '@') = '\0';
4147eda14cbcSMatt Macy 	zfs_handle_t *zhp = zfs_open(hdl, target_fs,
4148eda14cbcSMatt Macy 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4149eda14cbcSMatt Macy 	if (zhp == NULL)
4150eda14cbcSMatt Macy 		return;
4151eda14cbcSMatt Macy 
4152eda14cbcSMatt Macy 	char token_buf[ZFS_MAXPROPLEN];
4153eda14cbcSMatt Macy 	int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
4154eda14cbcSMatt Macy 	    token_buf, sizeof (token_buf),
4155eda14cbcSMatt Macy 	    NULL, NULL, 0, B_TRUE);
4156eda14cbcSMatt Macy 	if (error == 0) {
4157eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4158eda14cbcSMatt Macy 		    "checksum mismatch or incomplete stream.\n"
4159eda14cbcSMatt Macy 		    "Partially received snapshot is saved.\n"
4160eda14cbcSMatt Macy 		    "A resuming stream can be generated on the sending "
4161eda14cbcSMatt Macy 		    "system by running:\n"
4162eda14cbcSMatt Macy 		    "    zfs send -t %s"),
4163eda14cbcSMatt Macy 		    token_buf);
4164eda14cbcSMatt Macy 	}
4165eda14cbcSMatt Macy 	zfs_close(zhp);
4166eda14cbcSMatt Macy }
4167eda14cbcSMatt Macy 
4168eda14cbcSMatt Macy /*
4169eda14cbcSMatt Macy  * Prepare a new nvlist of properties that are to override (-o) or be excluded
4170eda14cbcSMatt Macy  * (-x) from the received dataset
4171eda14cbcSMatt Macy  * recvprops: received properties from the send stream
4172eda14cbcSMatt Macy  * cmdprops: raw input properties from command line
4173eda14cbcSMatt Macy  * origprops: properties, both locally-set and received, currently set on the
4174eda14cbcSMatt Macy  *            target dataset if it exists, NULL otherwise.
4175eda14cbcSMatt Macy  * oxprops: valid output override (-o) and excluded (-x) properties
4176eda14cbcSMatt Macy  */
4177eda14cbcSMatt Macy static int
4178eda14cbcSMatt Macy zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
4179eda14cbcSMatt Macy     char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
4180eda14cbcSMatt Macy     boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
4181eda14cbcSMatt Macy     nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
4182eda14cbcSMatt Macy     uint_t *wkeylen_out, const char *errbuf)
4183eda14cbcSMatt Macy {
4184eda14cbcSMatt Macy 	nvpair_t *nvp;
4185eda14cbcSMatt Macy 	nvlist_t *oprops, *voprops;
4186eda14cbcSMatt Macy 	zfs_handle_t *zhp = NULL;
4187eda14cbcSMatt Macy 	zpool_handle_t *zpool_hdl = NULL;
4188eda14cbcSMatt Macy 	char *cp;
4189eda14cbcSMatt Macy 	int ret = 0;
4190eda14cbcSMatt Macy 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
4191eda14cbcSMatt Macy 
4192eda14cbcSMatt Macy 	if (nvlist_empty(cmdprops))
4193eda14cbcSMatt Macy 		return (0); /* No properties to override or exclude */
4194eda14cbcSMatt Macy 
4195eda14cbcSMatt Macy 	*oxprops = fnvlist_alloc();
4196eda14cbcSMatt Macy 	oprops = fnvlist_alloc();
4197eda14cbcSMatt Macy 
4198eda14cbcSMatt Macy 	strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
4199eda14cbcSMatt Macy 
4200eda14cbcSMatt Macy 	/*
4201eda14cbcSMatt Macy 	 * Get our dataset handle. The target dataset may not exist yet.
4202eda14cbcSMatt Macy 	 */
4203eda14cbcSMatt Macy 	if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
4204eda14cbcSMatt Macy 		zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
4205eda14cbcSMatt Macy 		if (zhp == NULL) {
4206eda14cbcSMatt Macy 			ret = -1;
4207eda14cbcSMatt Macy 			goto error;
4208eda14cbcSMatt Macy 		}
4209eda14cbcSMatt Macy 	}
4210eda14cbcSMatt Macy 
4211eda14cbcSMatt Macy 	/* open the zpool handle */
4212eda14cbcSMatt Macy 	cp = strchr(namebuf, '/');
4213eda14cbcSMatt Macy 	if (cp != NULL)
4214eda14cbcSMatt Macy 		*cp = '\0';
4215eda14cbcSMatt Macy 	zpool_hdl = zpool_open(hdl, namebuf);
4216eda14cbcSMatt Macy 	if (zpool_hdl == NULL) {
4217eda14cbcSMatt Macy 		ret = -1;
4218eda14cbcSMatt Macy 		goto error;
4219eda14cbcSMatt Macy 	}
4220eda14cbcSMatt Macy 
4221eda14cbcSMatt Macy 	/* restore namebuf to match fsname for later use */
4222eda14cbcSMatt Macy 	if (cp != NULL)
4223eda14cbcSMatt Macy 		*cp = '/';
4224eda14cbcSMatt Macy 
4225eda14cbcSMatt Macy 	/*
4226eda14cbcSMatt Macy 	 * first iteration: process excluded (-x) properties now and gather
4227eda14cbcSMatt Macy 	 * added (-o) properties to be later processed by zfs_valid_proplist()
4228eda14cbcSMatt Macy 	 */
4229eda14cbcSMatt Macy 	nvp = NULL;
4230eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
4231eda14cbcSMatt Macy 		const char *name = nvpair_name(nvp);
4232eda14cbcSMatt Macy 		zfs_prop_t prop = zfs_name_to_prop(name);
4233eda14cbcSMatt Macy 
423481b22a98SMartin Matuska 		/*
423581b22a98SMartin Matuska 		 * It turns out, if we don't normalize "aliased" names
423681b22a98SMartin Matuska 		 * e.g. compress= against the "real" names (e.g. compression)
423781b22a98SMartin Matuska 		 * here, then setting/excluding them does not work as
423881b22a98SMartin Matuska 		 * intended.
423981b22a98SMartin Matuska 		 *
424081b22a98SMartin Matuska 		 * But since user-defined properties wouldn't have a valid
424181b22a98SMartin Matuska 		 * mapping here, we do this conditional dance.
424281b22a98SMartin Matuska 		 */
424381b22a98SMartin Matuska 		const char *newname = name;
424481b22a98SMartin Matuska 		if (prop >= ZFS_PROP_TYPE)
424581b22a98SMartin Matuska 			newname = zfs_prop_to_name(prop);
424681b22a98SMartin Matuska 
4247eda14cbcSMatt Macy 		/* "origin" is processed separately, don't handle it here */
4248eda14cbcSMatt Macy 		if (prop == ZFS_PROP_ORIGIN)
4249eda14cbcSMatt Macy 			continue;
4250eda14cbcSMatt Macy 
4251eda14cbcSMatt Macy 		/* raw streams can't override encryption properties */
4252eda14cbcSMatt Macy 		if ((zfs_prop_encryption_key_param(prop) ||
4253eda14cbcSMatt Macy 		    prop == ZFS_PROP_ENCRYPTION) && raw) {
4254eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4255eda14cbcSMatt Macy 			    "encryption property '%s' cannot "
4256eda14cbcSMatt Macy 			    "be set or excluded for raw streams."), name);
4257eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4258eda14cbcSMatt Macy 			goto error;
4259eda14cbcSMatt Macy 		}
4260eda14cbcSMatt Macy 
426115f0b8c3SMartin Matuska 		/*
426215f0b8c3SMartin Matuska 		 * For plain replicated send, we can ignore encryption
426315f0b8c3SMartin Matuska 		 * properties other than first stream
426415f0b8c3SMartin Matuska 		 */
426515f0b8c3SMartin Matuska 		if ((zfs_prop_encryption_key_param(prop) || prop ==
426615f0b8c3SMartin Matuska 		    ZFS_PROP_ENCRYPTION) && !newfs && recursive && !raw) {
426715f0b8c3SMartin Matuska 			continue;
426815f0b8c3SMartin Matuska 		}
426915f0b8c3SMartin Matuska 
4270eda14cbcSMatt Macy 		/* incremental streams can only exclude encryption properties */
4271eda14cbcSMatt Macy 		if ((zfs_prop_encryption_key_param(prop) ||
4272eda14cbcSMatt Macy 		    prop == ZFS_PROP_ENCRYPTION) && !newfs &&
4273eda14cbcSMatt Macy 		    nvpair_type(nvp) != DATA_TYPE_BOOLEAN) {
4274eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4275eda14cbcSMatt Macy 			    "encryption property '%s' cannot "
4276eda14cbcSMatt Macy 			    "be set for incremental streams."), name);
4277eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4278eda14cbcSMatt Macy 			goto error;
4279eda14cbcSMatt Macy 		}
4280eda14cbcSMatt Macy 
4281eda14cbcSMatt Macy 		switch (nvpair_type(nvp)) {
4282eda14cbcSMatt Macy 		case DATA_TYPE_BOOLEAN: /* -x property */
4283eda14cbcSMatt Macy 			/*
4284eda14cbcSMatt Macy 			 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
4285eda14cbcSMatt Macy 			 * a property: this is done by forcing an explicit
4286eda14cbcSMatt Macy 			 * inherit on the destination so the effective value is
4287eda14cbcSMatt Macy 			 * not the one we received from the send stream.
428816038816SMartin Matuska 			 */
428916038816SMartin Matuska 			if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
429016038816SMartin Matuska 			    !zfs_prop_user(name)) {
429116038816SMartin Matuska 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
429216038816SMartin Matuska 				    "Warning: %s: property '%s' does not "
429316038816SMartin Matuska 				    "apply to datasets of this type\n"),
429416038816SMartin Matuska 				    fsname, name);
429516038816SMartin Matuska 				continue;
429616038816SMartin Matuska 			}
429716038816SMartin Matuska 			/*
4298eda14cbcSMatt Macy 			 * We do this only if the property is not already
4299eda14cbcSMatt Macy 			 * locally-set, in which case its value will take
4300eda14cbcSMatt Macy 			 * priority over the received anyway.
4301eda14cbcSMatt Macy 			 */
430281b22a98SMartin Matuska 			if (nvlist_exists(origprops, newname)) {
4303eda14cbcSMatt Macy 				nvlist_t *attrs;
43042a58b312SMartin Matuska 				const char *source = NULL;
4305eda14cbcSMatt Macy 
430681b22a98SMartin Matuska 				attrs = fnvlist_lookup_nvlist(origprops,
430781b22a98SMartin Matuska 				    newname);
4308eda14cbcSMatt Macy 				if (nvlist_lookup_string(attrs,
4309eda14cbcSMatt Macy 				    ZPROP_SOURCE, &source) == 0 &&
4310eda14cbcSMatt Macy 				    strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
4311eda14cbcSMatt Macy 					continue;
4312eda14cbcSMatt Macy 			}
4313eda14cbcSMatt Macy 			/*
4314eda14cbcSMatt Macy 			 * We can't force an explicit inherit on non-inheritable
4315eda14cbcSMatt Macy 			 * properties: if we're asked to exclude this kind of
4316eda14cbcSMatt Macy 			 * values we remove them from "recvprops" input nvlist.
4317eda14cbcSMatt Macy 			 */
4318c03c5b1cSMartin Matuska 			if (!zfs_prop_user(name) && /* can be inherited too */
4319c03c5b1cSMartin Matuska 			    !zfs_prop_inheritable(prop) &&
432081b22a98SMartin Matuska 			    nvlist_exists(recvprops, newname))
432181b22a98SMartin Matuska 				fnvlist_remove(recvprops, newname);
4322eda14cbcSMatt Macy 			else
432381b22a98SMartin Matuska 				fnvlist_add_boolean(*oxprops, newname);
4324eda14cbcSMatt Macy 			break;
4325eda14cbcSMatt Macy 		case DATA_TYPE_STRING: /* -o property=value */
432616038816SMartin Matuska 			/*
432716038816SMartin Matuska 			 * we're trying to override a property that does not
432816038816SMartin Matuska 			 * make sense for this type of dataset, but we don't
432916038816SMartin Matuska 			 * want to fail if the receive is recursive: this comes
433016038816SMartin Matuska 			 * in handy when the send stream contains, for
433116038816SMartin Matuska 			 * instance, a child ZVOL and we're trying to receive
433216038816SMartin Matuska 			 * it with "-o atime=on"
433316038816SMartin Matuska 			 */
433416038816SMartin Matuska 			if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
433516038816SMartin Matuska 			    !zfs_prop_user(name)) {
433616038816SMartin Matuska 				if (recursive)
433716038816SMartin Matuska 					continue;
433816038816SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
433916038816SMartin Matuska 				    "property '%s' does not apply to datasets "
434016038816SMartin Matuska 				    "of this type"), name);
434116038816SMartin Matuska 				ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
434216038816SMartin Matuska 				goto error;
434316038816SMartin Matuska 			}
434481b22a98SMartin Matuska 			fnvlist_add_string(oprops, newname,
434581b22a98SMartin Matuska 			    fnvpair_value_string(nvp));
4346eda14cbcSMatt Macy 			break;
4347eda14cbcSMatt Macy 		default:
4348eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4349eda14cbcSMatt Macy 			    "property '%s' must be a string or boolean"), name);
4350eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4351eda14cbcSMatt Macy 			goto error;
4352eda14cbcSMatt Macy 		}
4353eda14cbcSMatt Macy 	}
4354eda14cbcSMatt Macy 
4355eda14cbcSMatt Macy 	if (toplevel) {
4356eda14cbcSMatt Macy 		/* convert override strings properties to native */
4357eda14cbcSMatt Macy 		if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
4358eda14cbcSMatt Macy 		    oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
4359eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
4360eda14cbcSMatt Macy 			goto error;
4361eda14cbcSMatt Macy 		}
4362eda14cbcSMatt Macy 
4363eda14cbcSMatt Macy 		/*
4364eda14cbcSMatt Macy 		 * zfs_crypto_create() requires the parent name. Get it
4365eda14cbcSMatt Macy 		 * by truncating the fsname copy stored in namebuf.
4366eda14cbcSMatt Macy 		 */
4367eda14cbcSMatt Macy 		cp = strrchr(namebuf, '/');
4368eda14cbcSMatt Macy 		if (cp != NULL)
4369eda14cbcSMatt Macy 			*cp = '\0';
4370eda14cbcSMatt Macy 
437115f0b8c3SMartin Matuska 		if (!raw && !(!newfs && recursive) &&
437215f0b8c3SMartin Matuska 		    zfs_crypto_create(hdl, namebuf, voprops, NULL,
4373eda14cbcSMatt Macy 		    B_FALSE, wkeydata_out, wkeylen_out) != 0) {
4374eda14cbcSMatt Macy 			fnvlist_free(voprops);
4375eda14cbcSMatt Macy 			ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4376eda14cbcSMatt Macy 			goto error;
4377eda14cbcSMatt Macy 		}
4378eda14cbcSMatt Macy 
4379eda14cbcSMatt Macy 		/* second pass: process "-o" properties */
4380eda14cbcSMatt Macy 		fnvlist_merge(*oxprops, voprops);
4381eda14cbcSMatt Macy 		fnvlist_free(voprops);
4382eda14cbcSMatt Macy 	} else {
4383eda14cbcSMatt Macy 		/* override props on child dataset are inherited */
4384eda14cbcSMatt Macy 		nvp = NULL;
4385eda14cbcSMatt Macy 		while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
4386eda14cbcSMatt Macy 			const char *name = nvpair_name(nvp);
4387eda14cbcSMatt Macy 			fnvlist_add_boolean(*oxprops, name);
4388eda14cbcSMatt Macy 		}
4389eda14cbcSMatt Macy 	}
4390eda14cbcSMatt Macy 
4391eda14cbcSMatt Macy error:
4392eda14cbcSMatt Macy 	if (zhp != NULL)
4393eda14cbcSMatt Macy 		zfs_close(zhp);
4394eda14cbcSMatt Macy 	if (zpool_hdl != NULL)
4395eda14cbcSMatt Macy 		zpool_close(zpool_hdl);
4396eda14cbcSMatt Macy 	fnvlist_free(oprops);
4397eda14cbcSMatt Macy 	return (ret);
4398eda14cbcSMatt Macy }
4399eda14cbcSMatt Macy 
4400eda14cbcSMatt Macy /*
4401eda14cbcSMatt Macy  * Restores a backup of tosnap from the file descriptor specified by infd.
4402eda14cbcSMatt Macy  */
4403eda14cbcSMatt Macy static int
4404eda14cbcSMatt Macy zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
4405eda14cbcSMatt Macy     const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
4406eda14cbcSMatt Macy     dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
4407eda14cbcSMatt Macy     avl_tree_t *stream_avl, char **top_zfs,
4408eda14cbcSMatt Macy     const char *finalsnap, nvlist_t *cmdprops)
4409eda14cbcSMatt Macy {
4410c03c5b1cSMartin Matuska 	struct timespec begin_time;
4411eda14cbcSMatt Macy 	int ioctl_err, ioctl_errno, err;
4412eda14cbcSMatt Macy 	char *cp;
4413eda14cbcSMatt Macy 	struct drr_begin *drrb = &drr->drr_u.drr_begin;
44141f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
4415eda14cbcSMatt Macy 	const char *chopprefix;
4416eda14cbcSMatt Macy 	boolean_t newfs = B_FALSE;
4417180f8225SMatt Macy 	boolean_t stream_wantsnewfs, stream_resumingnewfs;
4418eda14cbcSMatt Macy 	boolean_t newprops = B_FALSE;
4419eda14cbcSMatt Macy 	uint64_t read_bytes = 0;
4420eda14cbcSMatt Macy 	uint64_t errflags = 0;
4421eda14cbcSMatt Macy 	uint64_t parent_snapguid = 0;
4422eda14cbcSMatt Macy 	prop_changelist_t *clp = NULL;
4423eda14cbcSMatt Macy 	nvlist_t *snapprops_nvlist = NULL;
4424eda14cbcSMatt Macy 	nvlist_t *snapholds_nvlist = NULL;
4425eda14cbcSMatt Macy 	zprop_errflags_t prop_errflags;
4426eda14cbcSMatt Macy 	nvlist_t *prop_errors = NULL;
4427eda14cbcSMatt Macy 	boolean_t recursive;
44282a58b312SMartin Matuska 	const char *snapname = NULL;
4429eda14cbcSMatt Macy 	char destsnap[MAXPATHLEN * 2];
4430da5137abSMartin Matuska 	char origin[MAXNAMELEN] = {0};
4431eda14cbcSMatt Macy 	char name[MAXPATHLEN];
4432da5137abSMartin Matuska 	char tmp_keylocation[MAXNAMELEN] = {0};
4433eda14cbcSMatt Macy 	nvlist_t *rcvprops = NULL; /* props received from the send stream */
4434eda14cbcSMatt Macy 	nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
4435eda14cbcSMatt Macy 	nvlist_t *origprops = NULL; /* original props (if destination exists) */
4436c03c5b1cSMartin Matuska 	zfs_type_t type = ZFS_TYPE_INVALID;
4437eda14cbcSMatt Macy 	boolean_t toplevel = B_FALSE;
4438eda14cbcSMatt Macy 	boolean_t zoned = B_FALSE;
4439eda14cbcSMatt Macy 	boolean_t hastoken = B_FALSE;
4440eda14cbcSMatt Macy 	boolean_t redacted;
4441eda14cbcSMatt Macy 	uint8_t *wkeydata = NULL;
4442eda14cbcSMatt Macy 	uint_t wkeylen = 0;
4443eda14cbcSMatt Macy 
4444c03c5b1cSMartin Matuska #ifndef CLOCK_MONOTONIC_RAW
4445c03c5b1cSMartin Matuska #define	CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
4446c03c5b1cSMartin Matuska #endif
4447c03c5b1cSMartin Matuska 	clock_gettime(CLOCK_MONOTONIC_RAW, &begin_time);
4448eda14cbcSMatt Macy 
4449eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4450eda14cbcSMatt Macy 	    "cannot receive"));
4451eda14cbcSMatt Macy 
4452eda14cbcSMatt Macy 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
4453eda14cbcSMatt Macy 	    ENOENT);
4454eda14cbcSMatt Macy 
4455eda14cbcSMatt Macy 	/* Did the user request holds be skipped via zfs recv -k? */
4456eda14cbcSMatt Macy 	boolean_t holds = flags->holds && !flags->skipholds;
4457eda14cbcSMatt Macy 
4458eda14cbcSMatt Macy 	if (stream_avl != NULL) {
44592a58b312SMartin Matuska 		const char *keylocation = NULL;
4460eda14cbcSMatt Macy 		nvlist_t *lookup = NULL;
4461eda14cbcSMatt Macy 		nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
4462eda14cbcSMatt Macy 		    &snapname);
4463eda14cbcSMatt Macy 
4464eda14cbcSMatt Macy 		(void) nvlist_lookup_uint64(fs, "parentfromsnap",
4465eda14cbcSMatt Macy 		    &parent_snapguid);
4466eda14cbcSMatt Macy 		err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
4467eda14cbcSMatt Macy 		if (err) {
4468184c1b94SMartin Matuska 			rcvprops = fnvlist_alloc();
4469eda14cbcSMatt Macy 			newprops = B_TRUE;
4470eda14cbcSMatt Macy 		}
4471eda14cbcSMatt Macy 
4472eda14cbcSMatt Macy 		/*
4473eda14cbcSMatt Macy 		 * The keylocation property may only be set on encryption roots,
4474eda14cbcSMatt Macy 		 * but this dataset might not become an encryption root until
4475eda14cbcSMatt Macy 		 * recv_fix_encryption_hierarchy() is called. That function
4476eda14cbcSMatt Macy 		 * will fixup the keylocation anyway, so we temporarily unset
4477eda14cbcSMatt Macy 		 * the keylocation for now to avoid any errors from the receive
4478eda14cbcSMatt Macy 		 * ioctl.
4479eda14cbcSMatt Macy 		 */
4480eda14cbcSMatt Macy 		err = nvlist_lookup_string(rcvprops,
4481eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
4482eda14cbcSMatt Macy 		if (err == 0) {
4483be181ee2SMartin Matuska 			strlcpy(tmp_keylocation, keylocation, MAXNAMELEN);
4484eda14cbcSMatt Macy 			(void) nvlist_remove_all(rcvprops,
4485eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
4486eda14cbcSMatt Macy 		}
4487eda14cbcSMatt Macy 
4488eda14cbcSMatt Macy 		if (flags->canmountoff) {
4489184c1b94SMartin Matuska 			fnvlist_add_uint64(rcvprops,
4490184c1b94SMartin Matuska 			    zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0);
4491eda14cbcSMatt Macy 		} else if (newprops) {	/* nothing in rcvprops, eliminate it */
4492184c1b94SMartin Matuska 			fnvlist_free(rcvprops);
4493eda14cbcSMatt Macy 			rcvprops = NULL;
4494eda14cbcSMatt Macy 			newprops = B_FALSE;
4495eda14cbcSMatt Macy 		}
4496eda14cbcSMatt Macy 		if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
4497184c1b94SMartin Matuska 			snapprops_nvlist = fnvlist_lookup_nvlist(lookup,
4498184c1b94SMartin Matuska 			    snapname);
4499eda14cbcSMatt Macy 		}
4500eda14cbcSMatt Macy 		if (holds) {
4501eda14cbcSMatt Macy 			if (0 == nvlist_lookup_nvlist(fs, "snapholds",
4502eda14cbcSMatt Macy 			    &lookup)) {
4503184c1b94SMartin Matuska 				snapholds_nvlist = fnvlist_lookup_nvlist(
4504184c1b94SMartin Matuska 				    lookup, snapname);
4505eda14cbcSMatt Macy 			}
4506eda14cbcSMatt Macy 		}
4507eda14cbcSMatt Macy 	}
4508eda14cbcSMatt Macy 
4509eda14cbcSMatt Macy 	cp = NULL;
4510eda14cbcSMatt Macy 
4511eda14cbcSMatt Macy 	/*
4512eda14cbcSMatt Macy 	 * Determine how much of the snapshot name stored in the stream
4513eda14cbcSMatt Macy 	 * we are going to tack on to the name they specified on the
4514eda14cbcSMatt Macy 	 * command line, and how much we are going to chop off.
4515eda14cbcSMatt Macy 	 *
4516eda14cbcSMatt Macy 	 * If they specified a snapshot, chop the entire name stored in
4517eda14cbcSMatt Macy 	 * the stream.
4518eda14cbcSMatt Macy 	 */
4519eda14cbcSMatt Macy 	if (flags->istail) {
4520eda14cbcSMatt Macy 		/*
4521eda14cbcSMatt Macy 		 * A filesystem was specified with -e. We want to tack on only
4522eda14cbcSMatt Macy 		 * the tail of the sent snapshot path.
4523eda14cbcSMatt Macy 		 */
4524eda14cbcSMatt Macy 		if (strchr(tosnap, '@')) {
4525eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4526eda14cbcSMatt Macy 			    "argument - snapshot not allowed with -e"));
4527eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4528eda14cbcSMatt Macy 			goto out;
4529eda14cbcSMatt Macy 		}
4530eda14cbcSMatt Macy 
4531eda14cbcSMatt Macy 		chopprefix = strrchr(sendfs, '/');
4532eda14cbcSMatt Macy 
4533eda14cbcSMatt Macy 		if (chopprefix == NULL) {
4534eda14cbcSMatt Macy 			/*
4535eda14cbcSMatt Macy 			 * The tail is the poolname, so we need to
4536eda14cbcSMatt Macy 			 * prepend a path separator.
4537eda14cbcSMatt Macy 			 */
4538eda14cbcSMatt Macy 			int len = strlen(drrb->drr_toname);
4539dbd5678dSMartin Matuska 			cp = umem_alloc(len + 2, UMEM_NOFAIL);
4540eda14cbcSMatt Macy 			cp[0] = '/';
4541eda14cbcSMatt Macy 			(void) strcpy(&cp[1], drrb->drr_toname);
4542eda14cbcSMatt Macy 			chopprefix = cp;
4543eda14cbcSMatt Macy 		} else {
4544eda14cbcSMatt Macy 			chopprefix = drrb->drr_toname + (chopprefix - sendfs);
4545eda14cbcSMatt Macy 		}
4546eda14cbcSMatt Macy 	} else if (flags->isprefix) {
4547eda14cbcSMatt Macy 		/*
4548eda14cbcSMatt Macy 		 * A filesystem was specified with -d. We want to tack on
4549eda14cbcSMatt Macy 		 * everything but the first element of the sent snapshot path
4550eda14cbcSMatt Macy 		 * (all but the pool name).
4551eda14cbcSMatt Macy 		 */
4552eda14cbcSMatt Macy 		if (strchr(tosnap, '@')) {
4553eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4554eda14cbcSMatt Macy 			    "argument - snapshot not allowed with -d"));
4555eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4556eda14cbcSMatt Macy 			goto out;
4557eda14cbcSMatt Macy 		}
4558eda14cbcSMatt Macy 
4559eda14cbcSMatt Macy 		chopprefix = strchr(drrb->drr_toname, '/');
4560eda14cbcSMatt Macy 		if (chopprefix == NULL)
4561eda14cbcSMatt Macy 			chopprefix = strchr(drrb->drr_toname, '@');
4562eda14cbcSMatt Macy 	} else if (strchr(tosnap, '@') == NULL) {
4563eda14cbcSMatt Macy 		/*
4564eda14cbcSMatt Macy 		 * If a filesystem was specified without -d or -e, we want to
4565eda14cbcSMatt Macy 		 * tack on everything after the fs specified by 'zfs send'.
4566eda14cbcSMatt Macy 		 */
4567eda14cbcSMatt Macy 		chopprefix = drrb->drr_toname + strlen(sendfs);
4568eda14cbcSMatt Macy 	} else {
4569eda14cbcSMatt Macy 		/* A snapshot was specified as an exact path (no -d or -e). */
4570eda14cbcSMatt Macy 		if (recursive) {
4571eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4572eda14cbcSMatt Macy 			    "cannot specify snapshot name for multi-snapshot "
4573eda14cbcSMatt Macy 			    "stream"));
4574eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4575eda14cbcSMatt Macy 			goto out;
4576eda14cbcSMatt Macy 		}
4577eda14cbcSMatt Macy 		chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
4578eda14cbcSMatt Macy 	}
4579eda14cbcSMatt Macy 
4580eda14cbcSMatt Macy 	ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
4581eda14cbcSMatt Macy 	ASSERT(chopprefix > drrb->drr_toname || strchr(sendfs, '/') == NULL);
4582eda14cbcSMatt Macy 	ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname) ||
4583eda14cbcSMatt Macy 	    strchr(sendfs, '/') == NULL);
4584eda14cbcSMatt Macy 	ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
4585eda14cbcSMatt Macy 	    chopprefix[0] == '\0');
4586eda14cbcSMatt Macy 
4587eda14cbcSMatt Macy 	/*
4588eda14cbcSMatt Macy 	 * Determine name of destination snapshot.
4589eda14cbcSMatt Macy 	 */
4590eda14cbcSMatt Macy 	(void) strlcpy(destsnap, tosnap, sizeof (destsnap));
4591eda14cbcSMatt Macy 	(void) strlcat(destsnap, chopprefix, sizeof (destsnap));
4592dbd5678dSMartin Matuska 	if (cp != NULL)
4593dbd5678dSMartin Matuska 		umem_free(cp, strlen(cp) + 1);
4594eda14cbcSMatt Macy 	if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
4595eda14cbcSMatt Macy 		err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4596eda14cbcSMatt Macy 		goto out;
4597eda14cbcSMatt Macy 	}
4598eda14cbcSMatt Macy 
4599eda14cbcSMatt Macy 	/*
4600eda14cbcSMatt Macy 	 * Determine the name of the origin snapshot.
4601eda14cbcSMatt Macy 	 */
4602eda14cbcSMatt Macy 	if (originsnap) {
4603eda14cbcSMatt Macy 		(void) strlcpy(origin, originsnap, sizeof (origin));
4604eda14cbcSMatt Macy 		if (flags->verbose)
4605eda14cbcSMatt Macy 			(void) printf("using provided clone origin %s\n",
4606eda14cbcSMatt Macy 			    origin);
4607eda14cbcSMatt Macy 	} else if (drrb->drr_flags & DRR_FLAG_CLONE) {
4608eda14cbcSMatt Macy 		if (guid_to_name(hdl, destsnap,
4609eda14cbcSMatt Macy 		    drrb->drr_fromguid, B_FALSE, origin) != 0) {
4610eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4611eda14cbcSMatt Macy 			    "local origin for clone %s does not exist"),
4612eda14cbcSMatt Macy 			    destsnap);
4613eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4614eda14cbcSMatt Macy 			goto out;
4615eda14cbcSMatt Macy 		}
4616eda14cbcSMatt Macy 		if (flags->verbose)
4617eda14cbcSMatt Macy 			(void) printf("found clone origin %s\n", origin);
4618eda14cbcSMatt Macy 	}
4619eda14cbcSMatt Macy 
4620eda14cbcSMatt Macy 	if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4621eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_DEDUP)) {
4622eda14cbcSMatt Macy 		(void) fprintf(stderr,
4623eda14cbcSMatt Macy 		    gettext("ERROR: \"zfs receive\" no longer supports "
4624eda14cbcSMatt Macy 		    "deduplicated send streams.  Use\n"
4625eda14cbcSMatt Macy 		    "the \"zstream redup\" command to convert this stream "
4626eda14cbcSMatt Macy 		    "to a regular,\n"
4627eda14cbcSMatt Macy 		    "non-deduplicated stream.\n"));
4628eda14cbcSMatt Macy 		err = zfs_error(hdl, EZFS_NOTSUP, errbuf);
4629eda14cbcSMatt Macy 		goto out;
4630eda14cbcSMatt Macy 	}
4631eda14cbcSMatt Macy 
4632eda14cbcSMatt Macy 	boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4633eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_RESUMING;
4634eda14cbcSMatt Macy 	boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4635eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_RAW;
4636eda14cbcSMatt Macy 	boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4637eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_EMBED_DATA;
4638eda14cbcSMatt Macy 	stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
4639eda14cbcSMatt Macy 	    (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
4640180f8225SMatt Macy 	stream_resumingnewfs = (drrb->drr_fromguid == 0 ||
4641180f8225SMatt Macy 	    (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && resuming;
4642eda14cbcSMatt Macy 
4643eda14cbcSMatt Macy 	if (stream_wantsnewfs) {
4644eda14cbcSMatt Macy 		/*
4645eda14cbcSMatt Macy 		 * if the parent fs does not exist, look for it based on
4646eda14cbcSMatt Macy 		 * the parent snap GUID
4647eda14cbcSMatt Macy 		 */
4648eda14cbcSMatt Macy 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4649eda14cbcSMatt Macy 		    "cannot receive new filesystem stream"));
4650eda14cbcSMatt Macy 
4651be181ee2SMartin Matuska 		(void) strlcpy(name, destsnap, sizeof (name));
4652eda14cbcSMatt Macy 		cp = strrchr(name, '/');
4653eda14cbcSMatt Macy 		if (cp)
4654eda14cbcSMatt Macy 			*cp = '\0';
4655eda14cbcSMatt Macy 		if (cp &&
4656eda14cbcSMatt Macy 		    !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4657eda14cbcSMatt Macy 			char suffix[ZFS_MAX_DATASET_NAME_LEN];
4658be181ee2SMartin Matuska 			(void) strlcpy(suffix, strrchr(destsnap, '/'),
4659be181ee2SMartin Matuska 			    sizeof (suffix));
4660eda14cbcSMatt Macy 			if (guid_to_name(hdl, name, parent_snapguid,
4661eda14cbcSMatt Macy 			    B_FALSE, destsnap) == 0) {
4662eda14cbcSMatt Macy 				*strchr(destsnap, '@') = '\0';
4663be181ee2SMartin Matuska 				(void) strlcat(destsnap, suffix,
4664c9539b89SMartin Matuska 				    sizeof (destsnap));
4665eda14cbcSMatt Macy 			}
4666eda14cbcSMatt Macy 		}
4667eda14cbcSMatt Macy 	} else {
4668eda14cbcSMatt Macy 		/*
4669eda14cbcSMatt Macy 		 * If the fs does not exist, look for it based on the
4670eda14cbcSMatt Macy 		 * fromsnap GUID.
4671eda14cbcSMatt Macy 		 */
4672eda14cbcSMatt Macy 		if (resuming) {
4673eda14cbcSMatt Macy 			(void) snprintf(errbuf, sizeof (errbuf),
4674eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN,
4675eda14cbcSMatt Macy 			    "cannot receive resume stream"));
4676eda14cbcSMatt Macy 		} else {
4677eda14cbcSMatt Macy 			(void) snprintf(errbuf, sizeof (errbuf),
4678eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN,
4679eda14cbcSMatt Macy 			    "cannot receive incremental stream"));
4680eda14cbcSMatt Macy 		}
4681eda14cbcSMatt Macy 
4682be181ee2SMartin Matuska 		(void) strlcpy(name, destsnap, sizeof (name));
4683eda14cbcSMatt Macy 		*strchr(name, '@') = '\0';
4684eda14cbcSMatt Macy 
4685eda14cbcSMatt Macy 		/*
4686eda14cbcSMatt Macy 		 * If the exact receive path was specified and this is the
4687eda14cbcSMatt Macy 		 * topmost path in the stream, then if the fs does not exist we
4688eda14cbcSMatt Macy 		 * should look no further.
4689eda14cbcSMatt Macy 		 */
4690eda14cbcSMatt Macy 		if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
4691eda14cbcSMatt Macy 		    strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
4692eda14cbcSMatt Macy 		    !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4693eda14cbcSMatt Macy 			char snap[ZFS_MAX_DATASET_NAME_LEN];
4694be181ee2SMartin Matuska 			(void) strlcpy(snap, strchr(destsnap, '@'),
4695be181ee2SMartin Matuska 			    sizeof (snap));
4696eda14cbcSMatt Macy 			if (guid_to_name(hdl, name, drrb->drr_fromguid,
4697eda14cbcSMatt Macy 			    B_FALSE, destsnap) == 0) {
4698eda14cbcSMatt Macy 				*strchr(destsnap, '@') = '\0';
4699be181ee2SMartin Matuska 				(void) strlcat(destsnap, snap,
4700c9539b89SMartin Matuska 				    sizeof (destsnap));
4701eda14cbcSMatt Macy 			}
4702eda14cbcSMatt Macy 		}
4703eda14cbcSMatt Macy 	}
4704eda14cbcSMatt Macy 
4705be181ee2SMartin Matuska 	(void) strlcpy(name, destsnap, sizeof (name));
4706eda14cbcSMatt Macy 	*strchr(name, '@') = '\0';
4707eda14cbcSMatt Macy 
4708eda14cbcSMatt Macy 	redacted = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4709eda14cbcSMatt Macy 	    DMU_BACKUP_FEATURE_REDACTED;
4710eda14cbcSMatt Macy 
4711271171e0SMartin Matuska 	if (flags->heal) {
4712271171e0SMartin Matuska 		if (flags->isprefix || flags->istail || flags->force ||
4713271171e0SMartin Matuska 		    flags->canmountoff || flags->resumable || flags->nomount ||
4714271171e0SMartin Matuska 		    flags->skipholds) {
4715271171e0SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4716271171e0SMartin Matuska 			    "corrective recv can not be used when combined with"
4717271171e0SMartin Matuska 			    " this flag"));
4718271171e0SMartin Matuska 			err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4719271171e0SMartin Matuska 			goto out;
4720271171e0SMartin Matuska 		}
4721271171e0SMartin Matuska 		uint64_t guid =
4722271171e0SMartin Matuska 		    get_snap_guid(hdl, name, strchr(destsnap, '@') + 1);
4723271171e0SMartin Matuska 		if (guid == 0) {
4724271171e0SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4725271171e0SMartin Matuska 			    "corrective recv must specify an existing snapshot"
4726271171e0SMartin Matuska 			    " to heal"));
4727271171e0SMartin Matuska 			err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
4728271171e0SMartin Matuska 			goto out;
4729271171e0SMartin Matuska 		} else if (guid != drrb->drr_toguid) {
4730271171e0SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4731271171e0SMartin Matuska 			    "local snapshot doesn't match the snapshot"
4732271171e0SMartin Matuska 			    " in the provided stream"));
4733271171e0SMartin Matuska 			err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4734271171e0SMartin Matuska 			goto out;
4735271171e0SMartin Matuska 		}
4736271171e0SMartin Matuska 	} else if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
4737eda14cbcSMatt Macy 		zfs_cmd_t zc = {"\0"};
4738271171e0SMartin Matuska 		zfs_handle_t *zhp = NULL;
4739eda14cbcSMatt Macy 		boolean_t encrypted;
4740eda14cbcSMatt Macy 
4741eda14cbcSMatt Macy 		(void) strcpy(zc.zc_name, name);
4742eda14cbcSMatt Macy 
4743eda14cbcSMatt Macy 		/*
4744eda14cbcSMatt Macy 		 * Destination fs exists.  It must be one of these cases:
4745eda14cbcSMatt Macy 		 *  - an incremental send stream
4746eda14cbcSMatt Macy 		 *  - the stream specifies a new fs (full stream or clone)
4747eda14cbcSMatt Macy 		 *    and they want us to blow away the existing fs (and
4748eda14cbcSMatt Macy 		 *    have therefore specified -F and removed any snapshots)
4749eda14cbcSMatt Macy 		 *  - we are resuming a failed receive.
4750eda14cbcSMatt Macy 		 */
4751eda14cbcSMatt Macy 		if (stream_wantsnewfs) {
4752eda14cbcSMatt Macy 			boolean_t is_volume = drrb->drr_type == DMU_OST_ZVOL;
4753eda14cbcSMatt Macy 			if (!flags->force) {
4754eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4755eda14cbcSMatt Macy 				    "destination '%s' exists\n"
4756eda14cbcSMatt Macy 				    "must specify -F to overwrite it"), name);
4757eda14cbcSMatt Macy 				err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4758eda14cbcSMatt Macy 				goto out;
4759eda14cbcSMatt Macy 			}
4760eda14cbcSMatt Macy 			if (zfs_ioctl(hdl, ZFS_IOC_SNAPSHOT_LIST_NEXT,
4761eda14cbcSMatt Macy 			    &zc) == 0) {
4762eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4763eda14cbcSMatt Macy 				    "destination has snapshots (eg. %s)\n"
4764eda14cbcSMatt Macy 				    "must destroy them to overwrite it"),
4765eda14cbcSMatt Macy 				    zc.zc_name);
4766eda14cbcSMatt Macy 				err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4767eda14cbcSMatt Macy 				goto out;
4768eda14cbcSMatt Macy 			}
4769eda14cbcSMatt Macy 			if (is_volume && strrchr(name, '/') == NULL) {
4770eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4771eda14cbcSMatt Macy 				    "destination %s is the root dataset\n"
4772eda14cbcSMatt Macy 				    "cannot overwrite with a ZVOL"),
4773eda14cbcSMatt Macy 				    name);
4774eda14cbcSMatt Macy 				err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4775eda14cbcSMatt Macy 				goto out;
4776eda14cbcSMatt Macy 			}
4777eda14cbcSMatt Macy 			if (is_volume &&
4778eda14cbcSMatt Macy 			    zfs_ioctl(hdl, ZFS_IOC_DATASET_LIST_NEXT,
4779eda14cbcSMatt Macy 			    &zc) == 0) {
4780eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4781eda14cbcSMatt Macy 				    "destination has children (eg. %s)\n"
4782eda14cbcSMatt Macy 				    "cannot overwrite with a ZVOL"),
4783eda14cbcSMatt Macy 				    zc.zc_name);
4784eda14cbcSMatt Macy 				err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4785eda14cbcSMatt Macy 				goto out;
4786eda14cbcSMatt Macy 			}
4787eda14cbcSMatt Macy 		}
4788eda14cbcSMatt Macy 
4789eda14cbcSMatt Macy 		if ((zhp = zfs_open(hdl, name,
4790eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
4791eda14cbcSMatt Macy 			err = -1;
4792eda14cbcSMatt Macy 			goto out;
4793eda14cbcSMatt Macy 		}
4794eda14cbcSMatt Macy 
4795be181ee2SMartin Matuska 		/*
4796be181ee2SMartin Matuska 		 * When receiving full/newfs on existing dataset, then it
4797be181ee2SMartin Matuska 		 * should be done with "-F" flag. Its enforced for initial
4798be181ee2SMartin Matuska 		 * receive in previous checks in this function.
4799be181ee2SMartin Matuska 		 * Similarly, on resuming full/newfs recv on existing dataset,
4800be181ee2SMartin Matuska 		 * it should be done with "-F" flag.
4801be181ee2SMartin Matuska 		 *
4802be181ee2SMartin Matuska 		 * When dataset doesn't exist, then full/newfs recv is done on
4803be181ee2SMartin Matuska 		 * newly created dataset and it's marked INCONSISTENT. But
4804be181ee2SMartin Matuska 		 * When receiving on existing dataset, recv is first done on
4805be181ee2SMartin Matuska 		 * %recv and its marked INCONSISTENT. Existing dataset is not
4806be181ee2SMartin Matuska 		 * marked INCONSISTENT.
4807be181ee2SMartin Matuska 		 * Resume of full/newfs receive with dataset not INCONSISTENT
4808be181ee2SMartin Matuska 		 * indicates that its resuming newfs on existing dataset. So,
4809be181ee2SMartin Matuska 		 * enforce "-F" flag in this case.
4810be181ee2SMartin Matuska 		 */
4811be181ee2SMartin Matuska 		if (stream_resumingnewfs &&
4812be181ee2SMartin Matuska 		    !zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
4813be181ee2SMartin Matuska 		    !flags->force) {
4814be181ee2SMartin Matuska 			zfs_close(zhp);
4815be181ee2SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4816be181ee2SMartin Matuska 			    "Resuming recv on existing destination '%s'\n"
4817be181ee2SMartin Matuska 			    "must specify -F to overwrite it"), name);
4818be181ee2SMartin Matuska 			err = zfs_error(hdl, EZFS_RESUME_EXISTS, errbuf);
4819be181ee2SMartin Matuska 			goto out;
4820be181ee2SMartin Matuska 		}
4821be181ee2SMartin Matuska 
4822eda14cbcSMatt Macy 		if (stream_wantsnewfs &&
4823eda14cbcSMatt Macy 		    zhp->zfs_dmustats.dds_origin[0]) {
4824eda14cbcSMatt Macy 			zfs_close(zhp);
4825eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4826eda14cbcSMatt Macy 			    "destination '%s' is a clone\n"
4827eda14cbcSMatt Macy 			    "must destroy it to overwrite it"), name);
4828eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_EXISTS, errbuf);
4829eda14cbcSMatt Macy 			goto out;
4830eda14cbcSMatt Macy 		}
4831eda14cbcSMatt Macy 
4832eda14cbcSMatt Macy 		/*
4833eda14cbcSMatt Macy 		 * Raw sends can not be performed as an incremental on top
4834eda14cbcSMatt Macy 		 * of existing unencrypted datasets. zfs recv -F can't be
4835eda14cbcSMatt Macy 		 * used to blow away an existing encrypted filesystem. This
4836eda14cbcSMatt Macy 		 * is because it would require the dsl dir to point to the
4837eda14cbcSMatt Macy 		 * new key (or lack of a key) and the old key at the same
4838eda14cbcSMatt Macy 		 * time. The -F flag may still be used for deleting
4839eda14cbcSMatt Macy 		 * intermediate snapshots that would otherwise prevent the
4840eda14cbcSMatt Macy 		 * receive from working.
4841eda14cbcSMatt Macy 		 */
4842eda14cbcSMatt Macy 		encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
4843eda14cbcSMatt Macy 		    ZIO_CRYPT_OFF;
4844eda14cbcSMatt Macy 		if (!stream_wantsnewfs && !encrypted && raw) {
4845eda14cbcSMatt Macy 			zfs_close(zhp);
4846eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4847eda14cbcSMatt Macy 			    "cannot perform raw receive on top of "
4848eda14cbcSMatt Macy 			    "existing unencrypted dataset"));
4849eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4850eda14cbcSMatt Macy 			goto out;
4851eda14cbcSMatt Macy 		}
4852eda14cbcSMatt Macy 
4853eda14cbcSMatt Macy 		if (stream_wantsnewfs && flags->force &&
4854eda14cbcSMatt Macy 		    ((raw && !encrypted) || encrypted)) {
4855eda14cbcSMatt Macy 			zfs_close(zhp);
4856eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4857eda14cbcSMatt Macy 			    "zfs receive -F cannot be used to destroy an "
4858eda14cbcSMatt Macy 			    "encrypted filesystem or overwrite an "
4859eda14cbcSMatt Macy 			    "unencrypted one with an encrypted one"));
4860eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4861eda14cbcSMatt Macy 			goto out;
4862eda14cbcSMatt Macy 		}
4863eda14cbcSMatt Macy 
4864eda14cbcSMatt Macy 		if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4865180f8225SMatt Macy 		    (stream_wantsnewfs || stream_resumingnewfs)) {
4866eda14cbcSMatt Macy 			/* We can't do online recv in this case */
4867eda14cbcSMatt Macy 			clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
4868eda14cbcSMatt Macy 			    flags->forceunmount ? MS_FORCE : 0);
4869eda14cbcSMatt Macy 			if (clp == NULL) {
4870eda14cbcSMatt Macy 				zfs_close(zhp);
4871eda14cbcSMatt Macy 				err = -1;
4872eda14cbcSMatt Macy 				goto out;
4873eda14cbcSMatt Macy 			}
4874eda14cbcSMatt Macy 			if (changelist_prefix(clp) != 0) {
4875eda14cbcSMatt Macy 				changelist_free(clp);
4876eda14cbcSMatt Macy 				zfs_close(zhp);
4877eda14cbcSMatt Macy 				err = -1;
4878eda14cbcSMatt Macy 				goto out;
4879eda14cbcSMatt Macy 			}
4880eda14cbcSMatt Macy 		}
4881eda14cbcSMatt Macy 
4882eda14cbcSMatt Macy 		/*
4883eda14cbcSMatt Macy 		 * If we are resuming a newfs, set newfs here so that we will
4884eda14cbcSMatt Macy 		 * mount it if the recv succeeds this time.  We can tell
4885eda14cbcSMatt Macy 		 * that it was a newfs on the first recv because the fs
4886eda14cbcSMatt Macy 		 * itself will be inconsistent (if the fs existed when we
4887eda14cbcSMatt Macy 		 * did the first recv, we would have received it into
4888eda14cbcSMatt Macy 		 * .../%recv).
4889eda14cbcSMatt Macy 		 */
4890eda14cbcSMatt Macy 		if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4891eda14cbcSMatt Macy 			newfs = B_TRUE;
4892eda14cbcSMatt Macy 
4893eda14cbcSMatt Macy 		/* we want to know if we're zoned when validating -o|-x props */
4894eda14cbcSMatt Macy 		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4895eda14cbcSMatt Macy 
4896eda14cbcSMatt Macy 		/* may need this info later, get it now we have zhp around */
4897eda14cbcSMatt Macy 		if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4898eda14cbcSMatt Macy 		    NULL, NULL, 0, B_TRUE) == 0)
4899eda14cbcSMatt Macy 			hastoken = B_TRUE;
4900eda14cbcSMatt Macy 
4901eda14cbcSMatt Macy 		/* gather existing properties on destination */
4902eda14cbcSMatt Macy 		origprops = fnvlist_alloc();
4903eda14cbcSMatt Macy 		fnvlist_merge(origprops, zhp->zfs_props);
4904eda14cbcSMatt Macy 		fnvlist_merge(origprops, zhp->zfs_user_props);
4905eda14cbcSMatt Macy 
4906eda14cbcSMatt Macy 		zfs_close(zhp);
4907eda14cbcSMatt Macy 	} else {
4908eda14cbcSMatt Macy 		zfs_handle_t *zhp;
4909eda14cbcSMatt Macy 
4910eda14cbcSMatt Macy 		/*
4911eda14cbcSMatt Macy 		 * Destination filesystem does not exist.  Therefore we better
4912eda14cbcSMatt Macy 		 * be creating a new filesystem (either from a full backup, or
4913eda14cbcSMatt Macy 		 * a clone).  It would therefore be invalid if the user
4914eda14cbcSMatt Macy 		 * specified only the pool name (i.e. if the destination name
4915eda14cbcSMatt Macy 		 * contained no slash character).
4916eda14cbcSMatt Macy 		 */
4917eda14cbcSMatt Macy 		cp = strrchr(name, '/');
4918eda14cbcSMatt Macy 
4919eda14cbcSMatt Macy 		if (!stream_wantsnewfs || cp == NULL) {
4920eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4921eda14cbcSMatt Macy 			    "destination '%s' does not exist"), name);
4922eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4923eda14cbcSMatt Macy 			goto out;
4924eda14cbcSMatt Macy 		}
4925eda14cbcSMatt Macy 
4926eda14cbcSMatt Macy 		/*
4927eda14cbcSMatt Macy 		 * Trim off the final dataset component so we perform the
4928eda14cbcSMatt Macy 		 * recvbackup ioctl to the filesystems's parent.
4929eda14cbcSMatt Macy 		 */
4930eda14cbcSMatt Macy 		*cp = '\0';
4931eda14cbcSMatt Macy 
4932eda14cbcSMatt Macy 		if (flags->isprefix && !flags->istail && !flags->dryrun &&
4933eda14cbcSMatt Macy 		    create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4934eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4935eda14cbcSMatt Macy 			goto out;
4936eda14cbcSMatt Macy 		}
4937eda14cbcSMatt Macy 
4938eda14cbcSMatt Macy 		/* validate parent */
4939eda14cbcSMatt Macy 		zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4940eda14cbcSMatt Macy 		if (zhp == NULL) {
4941eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4942eda14cbcSMatt Macy 			goto out;
4943eda14cbcSMatt Macy 		}
4944eda14cbcSMatt Macy 		if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
4945eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4946eda14cbcSMatt Macy 			    "parent '%s' is not a filesystem"), name);
4947eda14cbcSMatt Macy 			err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4948eda14cbcSMatt Macy 			zfs_close(zhp);
4949eda14cbcSMatt Macy 			goto out;
4950eda14cbcSMatt Macy 		}
4951eda14cbcSMatt Macy 
4952eda14cbcSMatt Macy 		zfs_close(zhp);
4953eda14cbcSMatt Macy 
4954eda14cbcSMatt Macy 		newfs = B_TRUE;
4955eda14cbcSMatt Macy 		*cp = '/';
4956eda14cbcSMatt Macy 	}
4957eda14cbcSMatt Macy 
4958eda14cbcSMatt Macy 	if (flags->verbose) {
4959271171e0SMartin Matuska 		(void) printf("%s %s%s stream of %s into %s\n",
4960eda14cbcSMatt Macy 		    flags->dryrun ? "would receive" : "receiving",
4961271171e0SMartin Matuska 		    flags->heal ? "corrective " : "",
4962eda14cbcSMatt Macy 		    drrb->drr_fromguid ? "incremental" : "full",
4963eda14cbcSMatt Macy 		    drrb->drr_toname, destsnap);
4964eda14cbcSMatt Macy 		(void) fflush(stdout);
4965eda14cbcSMatt Macy 	}
4966eda14cbcSMatt Macy 
4967eda14cbcSMatt Macy 	/*
4968eda14cbcSMatt Macy 	 * If this is the top-level dataset, record it so we can use it
4969eda14cbcSMatt Macy 	 * for recursive operations later.
4970eda14cbcSMatt Macy 	 */
4971eda14cbcSMatt Macy 	if (top_zfs != NULL &&
4972eda14cbcSMatt Macy 	    (*top_zfs == NULL || strcmp(*top_zfs, name) == 0)) {
4973eda14cbcSMatt Macy 		toplevel = B_TRUE;
4974eda14cbcSMatt Macy 		if (*top_zfs == NULL)
4975eda14cbcSMatt Macy 			*top_zfs = zfs_strdup(hdl, name);
4976eda14cbcSMatt Macy 	}
4977eda14cbcSMatt Macy 
4978eda14cbcSMatt Macy 	if (drrb->drr_type == DMU_OST_ZVOL) {
4979eda14cbcSMatt Macy 		type = ZFS_TYPE_VOLUME;
4980eda14cbcSMatt Macy 	} else if (drrb->drr_type == DMU_OST_ZFS) {
4981eda14cbcSMatt Macy 		type = ZFS_TYPE_FILESYSTEM;
4982eda14cbcSMatt Macy 	} else {
4983eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4984eda14cbcSMatt Macy 		    "invalid record type: 0x%d"), drrb->drr_type);
4985eda14cbcSMatt Macy 		err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4986eda14cbcSMatt Macy 		goto out;
4987eda14cbcSMatt Macy 	}
4988eda14cbcSMatt Macy 	if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4989eda14cbcSMatt Macy 	    stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4990eda14cbcSMatt Macy 	    &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
4991eda14cbcSMatt Macy 		goto out;
4992eda14cbcSMatt Macy 
4993eda14cbcSMatt Macy 	/*
4994eda14cbcSMatt Macy 	 * When sending with properties (zfs send -p), the encryption property
4995eda14cbcSMatt Macy 	 * is not included because it is a SETONCE property and therefore
4996eda14cbcSMatt Macy 	 * treated as read only. However, we are always able to determine its
4997eda14cbcSMatt Macy 	 * value because raw sends will include it in the DRR_BDEGIN payload
4998eda14cbcSMatt Macy 	 * and non-raw sends with properties are not allowed for encrypted
4999eda14cbcSMatt Macy 	 * datasets. Therefore, if this is a non-raw properties stream, we can
5000eda14cbcSMatt Macy 	 * infer that the value should be ZIO_CRYPT_OFF and manually add that
5001eda14cbcSMatt Macy 	 * to the received properties.
5002eda14cbcSMatt Macy 	 */
5003eda14cbcSMatt Macy 	if (stream_wantsnewfs && !raw && rcvprops != NULL &&
5004eda14cbcSMatt Macy 	    !nvlist_exists(cmdprops, zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
5005eda14cbcSMatt Macy 		if (oxprops == NULL)
5006eda14cbcSMatt Macy 			oxprops = fnvlist_alloc();
5007eda14cbcSMatt Macy 		fnvlist_add_uint64(oxprops,
5008eda14cbcSMatt Macy 		    zfs_prop_to_name(ZFS_PROP_ENCRYPTION), ZIO_CRYPT_OFF);
5009eda14cbcSMatt Macy 	}
5010eda14cbcSMatt Macy 
501116038816SMartin Matuska 	if (flags->dryrun) {
501216038816SMartin Matuska 		void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
501316038816SMartin Matuska 
501416038816SMartin Matuska 		/*
501516038816SMartin Matuska 		 * We have read the DRR_BEGIN record, but we have
501616038816SMartin Matuska 		 * not yet read the payload. For non-dryrun sends
501716038816SMartin Matuska 		 * this will be done by the kernel, so we must
501816038816SMartin Matuska 		 * emulate that here, before attempting to read
501916038816SMartin Matuska 		 * more records.
502016038816SMartin Matuska 		 */
502116038816SMartin Matuska 		err = recv_read(hdl, infd, buf, drr->drr_payloadlen,
502216038816SMartin Matuska 		    flags->byteswap, NULL);
502316038816SMartin Matuska 		free(buf);
502416038816SMartin Matuska 		if (err != 0)
502516038816SMartin Matuska 			goto out;
502616038816SMartin Matuska 
502716038816SMartin Matuska 		err = recv_skip(hdl, infd, flags->byteswap);
502816038816SMartin Matuska 		goto out;
502916038816SMartin Matuska 	}
503016038816SMartin Matuska 
5031271171e0SMartin Matuska 	if (flags->heal) {
5032271171e0SMartin Matuska 		err = ioctl_err = lzc_receive_with_heal(destsnap, rcvprops,
5033271171e0SMartin Matuska 		    oxprops, wkeydata, wkeylen, origin, flags->force,
5034271171e0SMartin Matuska 		    flags->heal, flags->resumable, raw, infd, drr_noswap, -1,
5035271171e0SMartin Matuska 		    &read_bytes, &errflags, NULL, &prop_errors);
5036271171e0SMartin Matuska 	} else {
5037eda14cbcSMatt Macy 		err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
5038271171e0SMartin Matuska 		    oxprops, wkeydata, wkeylen, origin, flags->force,
5039271171e0SMartin Matuska 		    flags->resumable, raw, infd, drr_noswap, -1, &read_bytes,
5040271171e0SMartin Matuska 		    &errflags, NULL, &prop_errors);
5041271171e0SMartin Matuska 	}
5042eda14cbcSMatt Macy 	ioctl_errno = ioctl_err;
5043eda14cbcSMatt Macy 	prop_errflags = errflags;
5044eda14cbcSMatt Macy 
5045eda14cbcSMatt Macy 	if (err == 0) {
5046eda14cbcSMatt Macy 		nvpair_t *prop_err = NULL;
5047eda14cbcSMatt Macy 
5048eda14cbcSMatt Macy 		while ((prop_err = nvlist_next_nvpair(prop_errors,
5049eda14cbcSMatt Macy 		    prop_err)) != NULL) {
5050eda14cbcSMatt Macy 			char tbuf[1024];
5051eda14cbcSMatt Macy 			zfs_prop_t prop;
5052eda14cbcSMatt Macy 			int intval;
5053eda14cbcSMatt Macy 
5054eda14cbcSMatt Macy 			prop = zfs_name_to_prop(nvpair_name(prop_err));
5055eda14cbcSMatt Macy 			(void) nvpair_value_int32(prop_err, &intval);
5056eda14cbcSMatt Macy 			if (strcmp(nvpair_name(prop_err),
5057eda14cbcSMatt Macy 			    ZPROP_N_MORE_ERRORS) == 0) {
5058eda14cbcSMatt Macy 				trunc_prop_errs(intval);
5059eda14cbcSMatt Macy 				break;
5060eda14cbcSMatt Macy 			} else if (snapname == NULL || finalsnap == NULL ||
5061eda14cbcSMatt Macy 			    strcmp(finalsnap, snapname) == 0 ||
5062eda14cbcSMatt Macy 			    strcmp(nvpair_name(prop_err),
5063eda14cbcSMatt Macy 			    zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
5064eda14cbcSMatt Macy 				/*
5065eda14cbcSMatt Macy 				 * Skip the special case of, for example,
5066eda14cbcSMatt Macy 				 * "refquota", errors on intermediate
5067eda14cbcSMatt Macy 				 * snapshots leading up to a final one.
5068eda14cbcSMatt Macy 				 * That's why we have all of the checks above.
5069eda14cbcSMatt Macy 				 *
5070eda14cbcSMatt Macy 				 * See zfs_ioctl.c's extract_delay_props() for
5071eda14cbcSMatt Macy 				 * a list of props which can fail on
5072eda14cbcSMatt Macy 				 * intermediate snapshots, but shouldn't
5073eda14cbcSMatt Macy 				 * affect the overall receive.
5074eda14cbcSMatt Macy 				 */
5075eda14cbcSMatt Macy 				(void) snprintf(tbuf, sizeof (tbuf),
5076eda14cbcSMatt Macy 				    dgettext(TEXT_DOMAIN,
5077eda14cbcSMatt Macy 				    "cannot receive %s property on %s"),
5078eda14cbcSMatt Macy 				    nvpair_name(prop_err), name);
5079eda14cbcSMatt Macy 				zfs_setprop_error(hdl, prop, intval, tbuf);
5080eda14cbcSMatt Macy 			}
5081eda14cbcSMatt Macy 		}
5082eda14cbcSMatt Macy 	}
5083eda14cbcSMatt Macy 
5084eda14cbcSMatt Macy 	if (err == 0 && snapprops_nvlist) {
5085eda14cbcSMatt Macy 		zfs_cmd_t zc = {"\0"};
5086eda14cbcSMatt Macy 
5087be181ee2SMartin Matuska 		(void) strlcpy(zc.zc_name, destsnap, sizeof (zc.zc_name));
5088eda14cbcSMatt Macy 		zc.zc_cookie = B_TRUE; /* received */
5089716fd348SMartin Matuska 		zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist);
5090eda14cbcSMatt Macy 		(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
5091eda14cbcSMatt Macy 		zcmd_free_nvlists(&zc);
5092eda14cbcSMatt Macy 	}
5093eda14cbcSMatt Macy 	if (err == 0 && snapholds_nvlist) {
5094eda14cbcSMatt Macy 		nvpair_t *pair;
5095eda14cbcSMatt Macy 		nvlist_t *holds, *errors = NULL;
5096eda14cbcSMatt Macy 		int cleanup_fd = -1;
5097eda14cbcSMatt Macy 
5098eda14cbcSMatt Macy 		VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
5099eda14cbcSMatt Macy 		for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
5100eda14cbcSMatt Macy 		    pair != NULL;
5101eda14cbcSMatt Macy 		    pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
5102184c1b94SMartin Matuska 			fnvlist_add_string(holds, destsnap, nvpair_name(pair));
5103eda14cbcSMatt Macy 		}
5104eda14cbcSMatt Macy 		(void) lzc_hold(holds, cleanup_fd, &errors);
5105184c1b94SMartin Matuska 		fnvlist_free(snapholds_nvlist);
5106184c1b94SMartin Matuska 		fnvlist_free(holds);
5107eda14cbcSMatt Macy 	}
5108eda14cbcSMatt Macy 
5109eda14cbcSMatt Macy 	if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
5110eda14cbcSMatt Macy 		/*
5111eda14cbcSMatt Macy 		 * It may be that this snapshot already exists,
5112eda14cbcSMatt Macy 		 * in which case we want to consume & ignore it
5113eda14cbcSMatt Macy 		 * rather than failing.
5114eda14cbcSMatt Macy 		 */
5115eda14cbcSMatt Macy 		avl_tree_t *local_avl;
5116eda14cbcSMatt Macy 		nvlist_t *local_nv, *fs;
5117eda14cbcSMatt Macy 		cp = strchr(destsnap, '@');
5118eda14cbcSMatt Macy 
5119eda14cbcSMatt Macy 		/*
5120eda14cbcSMatt Macy 		 * XXX Do this faster by just iterating over snaps in
5121eda14cbcSMatt Macy 		 * this fs.  Also if zc_value does not exist, we will
5122eda14cbcSMatt Macy 		 * get a strange "does not exist" error message.
5123eda14cbcSMatt Macy 		 */
5124eda14cbcSMatt Macy 		*cp = '\0';
5125eda14cbcSMatt Macy 		if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
512616038816SMartin Matuska 		    B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE, B_FALSE,
512716038816SMartin Matuska 		    B_TRUE, &local_nv, &local_avl) == 0) {
5128eda14cbcSMatt Macy 			*cp = '@';
5129eda14cbcSMatt Macy 			fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
5130eda14cbcSMatt Macy 			fsavl_destroy(local_avl);
5131184c1b94SMartin Matuska 			fnvlist_free(local_nv);
5132eda14cbcSMatt Macy 
5133eda14cbcSMatt Macy 			if (fs != NULL) {
5134eda14cbcSMatt Macy 				if (flags->verbose) {
5135eda14cbcSMatt Macy 					(void) printf("snap %s already exists; "
5136eda14cbcSMatt Macy 					    "ignoring\n", destsnap);
5137eda14cbcSMatt Macy 				}
5138eda14cbcSMatt Macy 				err = ioctl_err = recv_skip(hdl, infd,
5139eda14cbcSMatt Macy 				    flags->byteswap);
5140eda14cbcSMatt Macy 			}
5141eda14cbcSMatt Macy 		}
5142eda14cbcSMatt Macy 		*cp = '@';
5143eda14cbcSMatt Macy 	}
5144eda14cbcSMatt Macy 
5145eda14cbcSMatt Macy 	if (ioctl_err != 0) {
5146eda14cbcSMatt Macy 		switch (ioctl_errno) {
5147eda14cbcSMatt Macy 		case ENODEV:
5148eda14cbcSMatt Macy 			cp = strchr(destsnap, '@');
5149eda14cbcSMatt Macy 			*cp = '\0';
5150eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5151eda14cbcSMatt Macy 			    "most recent snapshot of %s does not\n"
5152eda14cbcSMatt Macy 			    "match incremental source"), destsnap);
5153eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
5154eda14cbcSMatt Macy 			*cp = '@';
5155eda14cbcSMatt Macy 			break;
5156eda14cbcSMatt Macy 		case ETXTBSY:
5157eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5158eda14cbcSMatt Macy 			    "destination %s has been modified\n"
5159eda14cbcSMatt Macy 			    "since most recent snapshot"), name);
5160eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
5161eda14cbcSMatt Macy 			break;
5162eda14cbcSMatt Macy 		case EACCES:
5163271171e0SMartin Matuska 			if (flags->heal) {
5164271171e0SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5165271171e0SMartin Matuska 				    "key must be loaded to do a non-raw "
5166271171e0SMartin Matuska 				    "corrective recv on an encrypted "
5167271171e0SMartin Matuska 				    "dataset."));
5168271171e0SMartin Matuska 			} else if (raw && stream_wantsnewfs) {
5169eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5170eda14cbcSMatt Macy 				    "failed to create encryption key"));
5171eda14cbcSMatt Macy 			} else if (raw && !stream_wantsnewfs) {
5172eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5173eda14cbcSMatt Macy 				    "encryption key does not match "
5174eda14cbcSMatt Macy 				    "existing key"));
5175eda14cbcSMatt Macy 			} else {
5176eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5177eda14cbcSMatt Macy 				    "inherited key must be loaded"));
5178eda14cbcSMatt Macy 			}
5179eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
5180eda14cbcSMatt Macy 			break;
5181eda14cbcSMatt Macy 		case EEXIST:
5182eda14cbcSMatt Macy 			cp = strchr(destsnap, '@');
5183eda14cbcSMatt Macy 			if (newfs) {
5184eda14cbcSMatt Macy 				/* it's the containing fs that exists */
5185eda14cbcSMatt Macy 				*cp = '\0';
5186eda14cbcSMatt Macy 			}
5187eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5188eda14cbcSMatt Macy 			    "destination already exists"));
5189eda14cbcSMatt Macy 			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
5190eda14cbcSMatt Macy 			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
5191eda14cbcSMatt Macy 			    destsnap);
5192eda14cbcSMatt Macy 			*cp = '@';
5193eda14cbcSMatt Macy 			break;
5194eda14cbcSMatt Macy 		case EINVAL:
519515f0b8c3SMartin Matuska 			if (embedded && !raw) {
5196eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5197eda14cbcSMatt Macy 				    "incompatible embedded data stream "
5198eda14cbcSMatt Macy 				    "feature with encrypted receive."));
519915f0b8c3SMartin Matuska 			} else if (flags->resumable) {
520015f0b8c3SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
520115f0b8c3SMartin Matuska 				    "kernel modules must be upgraded to "
520215f0b8c3SMartin Matuska 				    "receive this stream."));
5203eda14cbcSMatt Macy 			}
5204eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5205eda14cbcSMatt Macy 			break;
5206eda14cbcSMatt Macy 		case ECKSUM:
5207eda14cbcSMatt Macy 		case ZFS_ERR_STREAM_TRUNCATED:
5208271171e0SMartin Matuska 			if (flags->heal)
5209271171e0SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5210271171e0SMartin Matuska 				    "corrective receive was not able to "
5211271171e0SMartin Matuska 				    "reconstruct the data needed for "
5212271171e0SMartin Matuska 				    "healing."));
5213271171e0SMartin Matuska 			else
5214271171e0SMartin Matuska 				recv_ecksum_set_aux(hdl, destsnap,
5215271171e0SMartin Matuska 				    flags->resumable, ioctl_err == ECKSUM);
5216eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5217eda14cbcSMatt Macy 			break;
5218eda14cbcSMatt Macy 		case ZFS_ERR_STREAM_LARGE_BLOCK_MISMATCH:
5219eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5220eda14cbcSMatt Macy 			    "incremental send stream requires -L "
5221eda14cbcSMatt Macy 			    "(--large-block), to match previous receive."));
5222eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5223eda14cbcSMatt Macy 			break;
5224eda14cbcSMatt Macy 		case ENOTSUP:
5225271171e0SMartin Matuska 			if (flags->heal)
5226eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5227271171e0SMartin Matuska 				    "stream is not compatible with the "
5228271171e0SMartin Matuska 				    "data in the pool."));
5229271171e0SMartin Matuska 			else
5230271171e0SMartin Matuska 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5231271171e0SMartin Matuska 				    "pool must be upgraded to receive this "
5232271171e0SMartin Matuska 				    "stream."));
5233eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
5234eda14cbcSMatt Macy 			break;
52352a58b312SMartin Matuska 		case ZFS_ERR_CRYPTO_NOTSUP:
52362a58b312SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
52372a58b312SMartin Matuska 			    "stream uses crypto parameters not compatible with "
52382a58b312SMartin Matuska 			    "this pool"));
52392a58b312SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
52402a58b312SMartin Matuska 			break;
5241eda14cbcSMatt Macy 		case EDQUOT:
5242eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5243eda14cbcSMatt Macy 			    "destination %s space quota exceeded."), name);
5244eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_NOSPC, errbuf);
5245eda14cbcSMatt Macy 			break;
5246eda14cbcSMatt Macy 		case ZFS_ERR_FROM_IVSET_GUID_MISSING:
5247eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5248eda14cbcSMatt Macy 			    "IV set guid missing. See errata %u at "
5249ac0bf12eSMatt Macy 			    "https://openzfs.github.io/openzfs-docs/msg/"
5250ac0bf12eSMatt Macy 			    "ZFS-8000-ER."),
5251eda14cbcSMatt Macy 			    ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
5252eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5253eda14cbcSMatt Macy 			break;
5254eda14cbcSMatt Macy 		case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
5255eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5256eda14cbcSMatt Macy 			    "IV set guid mismatch. See the 'zfs receive' "
5257eda14cbcSMatt Macy 			    "man page section\n discussing the limitations "
5258eda14cbcSMatt Macy 			    "of raw encrypted send streams."));
5259eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5260eda14cbcSMatt Macy 			break;
5261eda14cbcSMatt Macy 		case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING:
5262eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5263eda14cbcSMatt Macy 			    "Spill block flag missing for raw send.\n"
5264eda14cbcSMatt Macy 			    "The zfs software on the sending system must "
5265eda14cbcSMatt Macy 			    "be updated."));
5266eda14cbcSMatt Macy 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
5267eda14cbcSMatt Macy 			break;
5268be181ee2SMartin Matuska 		case ZFS_ERR_RESUME_EXISTS:
5269be181ee2SMartin Matuska 			cp = strchr(destsnap, '@');
5270be181ee2SMartin Matuska 			if (newfs) {
5271be181ee2SMartin Matuska 				/* it's the containing fs that exists */
5272be181ee2SMartin Matuska 				*cp = '\0';
5273be181ee2SMartin Matuska 			}
5274be181ee2SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5275be181ee2SMartin Matuska 			    "Resuming recv on existing dataset without force"));
5276be181ee2SMartin Matuska 			(void) zfs_error_fmt(hdl, EZFS_RESUME_EXISTS,
5277be181ee2SMartin Matuska 			    dgettext(TEXT_DOMAIN, "cannot resume recv %s"),
5278be181ee2SMartin Matuska 			    destsnap);
5279be181ee2SMartin Matuska 			*cp = '@';
5280be181ee2SMartin Matuska 			break;
528115f0b8c3SMartin Matuska 		case E2BIG:
528215f0b8c3SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
528315f0b8c3SMartin Matuska 			    "zfs receive required kernel memory allocation "
528415f0b8c3SMartin Matuska 			    "larger than the system can support. Please file "
528515f0b8c3SMartin Matuska 			    "an issue at the OpenZFS issue tracker:\n"
528615f0b8c3SMartin Matuska 			    "https://github.com/openzfs/zfs/issues/new"));
528715f0b8c3SMartin Matuska 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
528815f0b8c3SMartin Matuska 			break;
5289eda14cbcSMatt Macy 		case EBUSY:
5290eda14cbcSMatt Macy 			if (hastoken) {
5291eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5292eda14cbcSMatt Macy 				    "destination %s contains "
5293eda14cbcSMatt Macy 				    "partially-complete state from "
5294eda14cbcSMatt Macy 				    "\"zfs receive -s\"."), name);
5295eda14cbcSMatt Macy 				(void) zfs_error(hdl, EZFS_BUSY, errbuf);
5296eda14cbcSMatt Macy 				break;
5297eda14cbcSMatt Macy 			}
5298c03c5b1cSMartin Matuska 			zfs_fallthrough;
5299eda14cbcSMatt Macy 		default:
5300eda14cbcSMatt Macy 			(void) zfs_standard_error(hdl, ioctl_errno, errbuf);
5301eda14cbcSMatt Macy 		}
5302eda14cbcSMatt Macy 	}
5303eda14cbcSMatt Macy 
5304eda14cbcSMatt Macy 	/*
5305eda14cbcSMatt Macy 	 * Mount the target filesystem (if created).  Also mount any
5306eda14cbcSMatt Macy 	 * children of the target filesystem if we did a replication
5307eda14cbcSMatt Macy 	 * receive (indicated by stream_avl being non-NULL).
5308eda14cbcSMatt Macy 	 */
5309eda14cbcSMatt Macy 	if (clp) {
5310eda14cbcSMatt Macy 		if (!flags->nomount)
5311eda14cbcSMatt Macy 			err |= changelist_postfix(clp);
5312eda14cbcSMatt Macy 		changelist_free(clp);
5313eda14cbcSMatt Macy 	}
5314eda14cbcSMatt Macy 
5315eda14cbcSMatt Macy 	if ((newfs || stream_avl) && type == ZFS_TYPE_FILESYSTEM && !redacted)
5316eda14cbcSMatt Macy 		flags->domount = B_TRUE;
5317eda14cbcSMatt Macy 
5318eda14cbcSMatt Macy 	if (prop_errflags & ZPROP_ERR_NOCLEAR) {
5319eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
5320eda14cbcSMatt Macy 		    "failed to clear unreceived properties on %s"), name);
5321eda14cbcSMatt Macy 		(void) fprintf(stderr, "\n");
5322eda14cbcSMatt Macy 	}
5323eda14cbcSMatt Macy 	if (prop_errflags & ZPROP_ERR_NORESTORE) {
5324eda14cbcSMatt Macy 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
5325eda14cbcSMatt Macy 		    "failed to restore original properties on %s"), name);
5326eda14cbcSMatt Macy 		(void) fprintf(stderr, "\n");
5327eda14cbcSMatt Macy 	}
5328eda14cbcSMatt Macy 
5329eda14cbcSMatt Macy 	if (err || ioctl_err) {
5330eda14cbcSMatt Macy 		err = -1;
5331eda14cbcSMatt Macy 		goto out;
5332eda14cbcSMatt Macy 	}
5333eda14cbcSMatt Macy 
5334eda14cbcSMatt Macy 	if (flags->verbose) {
5335eda14cbcSMatt Macy 		char buf1[64];
5336eda14cbcSMatt Macy 		char buf2[64];
5337eda14cbcSMatt Macy 		uint64_t bytes = read_bytes;
5338c03c5b1cSMartin Matuska 		struct timespec delta;
5339c03c5b1cSMartin Matuska 		clock_gettime(CLOCK_MONOTONIC_RAW, &delta);
5340c03c5b1cSMartin Matuska 		if (begin_time.tv_nsec > delta.tv_nsec) {
5341c03c5b1cSMartin Matuska 			delta.tv_nsec =
5342c03c5b1cSMartin Matuska 			    1000000000 + delta.tv_nsec - begin_time.tv_nsec;
5343c03c5b1cSMartin Matuska 			delta.tv_sec -= 1;
5344c03c5b1cSMartin Matuska 		} else
5345c03c5b1cSMartin Matuska 			delta.tv_nsec -= begin_time.tv_nsec;
5346c03c5b1cSMartin Matuska 		delta.tv_sec -= begin_time.tv_sec;
5347c03c5b1cSMartin Matuska 		if (delta.tv_sec == 0 && delta.tv_nsec == 0)
5348c03c5b1cSMartin Matuska 			delta.tv_nsec = 1;
5349c03c5b1cSMartin Matuska 		double delta_f = delta.tv_sec + (delta.tv_nsec / 1e9);
5350eda14cbcSMatt Macy 		zfs_nicebytes(bytes, buf1, sizeof (buf1));
5351c03c5b1cSMartin Matuska 		zfs_nicebytes(bytes / delta_f, buf2, sizeof (buf2));
5352eda14cbcSMatt Macy 
5353c03c5b1cSMartin Matuska 		(void) printf("received %s stream in %.2f seconds (%s/sec)\n",
5354c03c5b1cSMartin Matuska 		    buf1, delta_f, buf2);
5355eda14cbcSMatt Macy 	}
5356eda14cbcSMatt Macy 
5357eda14cbcSMatt Macy 	err = 0;
5358eda14cbcSMatt Macy out:
5359eda14cbcSMatt Macy 	if (prop_errors != NULL)
5360184c1b94SMartin Matuska 		fnvlist_free(prop_errors);
5361eda14cbcSMatt Macy 
5362eda14cbcSMatt Macy 	if (tmp_keylocation[0] != '\0') {
5363184c1b94SMartin Matuska 		fnvlist_add_string(rcvprops,
5364184c1b94SMartin Matuska 		    zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation);
5365eda14cbcSMatt Macy 	}
5366eda14cbcSMatt Macy 
5367eda14cbcSMatt Macy 	if (newprops)
5368184c1b94SMartin Matuska 		fnvlist_free(rcvprops);
5369eda14cbcSMatt Macy 
5370184c1b94SMartin Matuska 	fnvlist_free(oxprops);
5371184c1b94SMartin Matuska 	fnvlist_free(origprops);
5372eda14cbcSMatt Macy 
5373eda14cbcSMatt Macy 	return (err);
5374eda14cbcSMatt Macy }
5375eda14cbcSMatt Macy 
5376eda14cbcSMatt Macy /*
5377eda14cbcSMatt Macy  * Check properties we were asked to override (both -o|-x)
5378eda14cbcSMatt Macy  */
5379eda14cbcSMatt Macy static boolean_t
5380eda14cbcSMatt Macy zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
5381eda14cbcSMatt Macy     const char *errbuf)
5382eda14cbcSMatt Macy {
5383c03c5b1cSMartin Matuska 	nvpair_t *nvp = NULL;
5384eda14cbcSMatt Macy 	zfs_prop_t prop;
5385eda14cbcSMatt Macy 	const char *name;
5386eda14cbcSMatt Macy 
5387eda14cbcSMatt Macy 	while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
5388eda14cbcSMatt Macy 		name = nvpair_name(nvp);
5389eda14cbcSMatt Macy 		prop = zfs_name_to_prop(name);
5390eda14cbcSMatt Macy 
53911f1e2261SMartin Matuska 		if (prop == ZPROP_USERPROP) {
5392eda14cbcSMatt Macy 			if (!zfs_prop_user(name)) {
5393eda14cbcSMatt Macy 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5394c03c5b1cSMartin Matuska 				    "%s: invalid property '%s'"), errbuf, name);
5395eda14cbcSMatt Macy 				return (B_FALSE);
5396eda14cbcSMatt Macy 			}
5397eda14cbcSMatt Macy 			continue;
5398eda14cbcSMatt Macy 		}
5399eda14cbcSMatt Macy 		/*
5400eda14cbcSMatt Macy 		 * "origin" is readonly but is used to receive datasets as
5401eda14cbcSMatt Macy 		 * clones so we don't raise an error here
5402eda14cbcSMatt Macy 		 */
5403eda14cbcSMatt Macy 		if (prop == ZFS_PROP_ORIGIN)
5404eda14cbcSMatt Macy 			continue;
5405eda14cbcSMatt Macy 
5406eda14cbcSMatt Macy 		/* encryption params have their own verification later */
5407eda14cbcSMatt Macy 		if (prop == ZFS_PROP_ENCRYPTION ||
5408eda14cbcSMatt Macy 		    zfs_prop_encryption_key_param(prop))
5409eda14cbcSMatt Macy 			continue;
5410eda14cbcSMatt Macy 
5411eda14cbcSMatt Macy 		/*
5412eda14cbcSMatt Macy 		 * cannot override readonly, set-once and other specific
5413eda14cbcSMatt Macy 		 * settable properties
5414eda14cbcSMatt Macy 		 */
5415eda14cbcSMatt Macy 		if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
5416eda14cbcSMatt Macy 		    prop == ZFS_PROP_VOLSIZE) {
5417eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
5418c03c5b1cSMartin Matuska 			    "%s: invalid property '%s'"), errbuf, name);
5419eda14cbcSMatt Macy 			return (B_FALSE);
5420eda14cbcSMatt Macy 		}
5421eda14cbcSMatt Macy 	}
5422eda14cbcSMatt Macy 
5423eda14cbcSMatt Macy 	return (B_TRUE);
5424eda14cbcSMatt Macy }
5425eda14cbcSMatt Macy 
5426eda14cbcSMatt Macy static int
5427eda14cbcSMatt Macy zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
5428eda14cbcSMatt Macy     const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
5429eda14cbcSMatt Macy     nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs,
5430eda14cbcSMatt Macy     const char *finalsnap, nvlist_t *cmdprops)
5431eda14cbcSMatt Macy {
5432eda14cbcSMatt Macy 	int err;
5433eda14cbcSMatt Macy 	dmu_replay_record_t drr, drr_noswap;
5434eda14cbcSMatt Macy 	struct drr_begin *drrb = &drr.drr_u.drr_begin;
54351f1e2261SMartin Matuska 	char errbuf[ERRBUFLEN];
5436eda14cbcSMatt Macy 	zio_cksum_t zcksum = { { 0 } };
5437eda14cbcSMatt Macy 	uint64_t featureflags;
5438eda14cbcSMatt Macy 	int hdrtype;
5439eda14cbcSMatt Macy 
5440eda14cbcSMatt Macy 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
5441eda14cbcSMatt Macy 	    "cannot receive"));
5442eda14cbcSMatt Macy 
5443eda14cbcSMatt Macy 	/* check cmdline props, raise an error if they cannot be received */
5444c03c5b1cSMartin Matuska 	if (!zfs_receive_checkprops(hdl, cmdprops, errbuf))
5445eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
5446eda14cbcSMatt Macy 
5447eda14cbcSMatt Macy 	if (flags->isprefix &&
5448eda14cbcSMatt Macy 	    !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
5449eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
5450eda14cbcSMatt Macy 		    "(%s) does not exist"), tosnap);
5451eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
5452eda14cbcSMatt Macy 	}
5453eda14cbcSMatt Macy 	if (originsnap &&
5454eda14cbcSMatt Macy 	    !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
5455eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
5456eda14cbcSMatt Macy 		    "(%s) does not exist"), originsnap);
5457eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
5458eda14cbcSMatt Macy 	}
5459eda14cbcSMatt Macy 
5460eda14cbcSMatt Macy 	/* read in the BEGIN record */
5461eda14cbcSMatt Macy 	if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
5462eda14cbcSMatt Macy 	    &zcksum)))
5463eda14cbcSMatt Macy 		return (err);
5464eda14cbcSMatt Macy 
5465eda14cbcSMatt Macy 	if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
5466eda14cbcSMatt Macy 		/* It's the double end record at the end of a package */
5467eda14cbcSMatt Macy 		return (ENODATA);
5468eda14cbcSMatt Macy 	}
5469eda14cbcSMatt Macy 
5470eda14cbcSMatt Macy 	/* the kernel needs the non-byteswapped begin record */
5471eda14cbcSMatt Macy 	drr_noswap = drr;
5472eda14cbcSMatt Macy 
5473eda14cbcSMatt Macy 	flags->byteswap = B_FALSE;
5474eda14cbcSMatt Macy 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
5475eda14cbcSMatt Macy 		/*
5476eda14cbcSMatt Macy 		 * We computed the checksum in the wrong byteorder in
5477eda14cbcSMatt Macy 		 * recv_read() above; do it again correctly.
5478eda14cbcSMatt Macy 		 */
5479da5137abSMartin Matuska 		memset(&zcksum, 0, sizeof (zio_cksum_t));
5480eda14cbcSMatt Macy 		fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
5481eda14cbcSMatt Macy 		flags->byteswap = B_TRUE;
5482eda14cbcSMatt Macy 
5483eda14cbcSMatt Macy 		drr.drr_type = BSWAP_32(drr.drr_type);
5484eda14cbcSMatt Macy 		drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
5485eda14cbcSMatt Macy 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
5486eda14cbcSMatt Macy 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
5487eda14cbcSMatt Macy 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
5488eda14cbcSMatt Macy 		drrb->drr_type = BSWAP_32(drrb->drr_type);
5489eda14cbcSMatt Macy 		drrb->drr_flags = BSWAP_32(drrb->drr_flags);
5490eda14cbcSMatt Macy 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
5491eda14cbcSMatt Macy 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
5492eda14cbcSMatt Macy 	}
5493eda14cbcSMatt Macy 
5494eda14cbcSMatt Macy 	if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
5495eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5496eda14cbcSMatt Macy 		    "stream (bad magic number)"));
5497eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5498eda14cbcSMatt Macy 	}
5499eda14cbcSMatt Macy 
5500eda14cbcSMatt Macy 	featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
5501eda14cbcSMatt Macy 	hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
5502eda14cbcSMatt Macy 
5503eda14cbcSMatt Macy 	if (!DMU_STREAM_SUPPORTED(featureflags) ||
5504eda14cbcSMatt Macy 	    (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
550581b22a98SMartin Matuska 		/*
550681b22a98SMartin Matuska 		 * Let's be explicit about this one, since rather than
550781b22a98SMartin Matuska 		 * being a new feature we can't know, it's an old
550881b22a98SMartin Matuska 		 * feature we dropped.
550981b22a98SMartin Matuska 		 */
551081b22a98SMartin Matuska 		if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
5511eda14cbcSMatt Macy 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
551281b22a98SMartin Matuska 			    "stream has deprecated feature: dedup, try "
551381b22a98SMartin Matuska 			    "'zstream redup [send in a file] | zfs recv "
551481b22a98SMartin Matuska 			    "[...]'"));
551581b22a98SMartin Matuska 		} else {
551681b22a98SMartin Matuska 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
551781b22a98SMartin Matuska 			    "stream has unsupported feature, feature flags = "
551881b22a98SMartin Matuska 			    "%llx (unknown flags = %llx)"),
551981b22a98SMartin Matuska 			    (u_longlong_t)featureflags,
552081b22a98SMartin Matuska 			    (u_longlong_t)((featureflags) &
552181b22a98SMartin Matuska 			    ~DMU_BACKUP_FEATURE_MASK));
552281b22a98SMartin Matuska 		}
5523eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5524eda14cbcSMatt Macy 	}
5525eda14cbcSMatt Macy 
5526eda14cbcSMatt Macy 	/* Holds feature is set once in the compound stream header. */
5527eda14cbcSMatt Macy 	if (featureflags & DMU_BACKUP_FEATURE_HOLDS)
5528eda14cbcSMatt Macy 		flags->holds = B_TRUE;
5529eda14cbcSMatt Macy 
5530eda14cbcSMatt Macy 	if (strchr(drrb->drr_toname, '@') == NULL) {
5531eda14cbcSMatt Macy 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
5532eda14cbcSMatt Macy 		    "stream (bad snapshot name)"));
5533eda14cbcSMatt Macy 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
5534eda14cbcSMatt Macy 	}
5535eda14cbcSMatt Macy 
5536eda14cbcSMatt Macy 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
5537eda14cbcSMatt Macy 		char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
5538eda14cbcSMatt Macy 		if (sendfs == NULL) {
5539eda14cbcSMatt Macy 			/*
5540eda14cbcSMatt Macy 			 * We were not called from zfs_receive_package(). Get
5541eda14cbcSMatt Macy 			 * the fs specified by 'zfs send'.
5542eda14cbcSMatt Macy 			 */
5543eda14cbcSMatt Macy 			char *cp;
5544eda14cbcSMatt Macy 			(void) strlcpy(nonpackage_sendfs,
5545eda14cbcSMatt Macy 			    drr.drr_u.drr_begin.drr_toname,
5546eda14cbcSMatt Macy 			    sizeof (nonpackage_sendfs));
5547eda14cbcSMatt Macy 			if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
5548eda14cbcSMatt Macy 				*cp = '\0';
5549eda14cbcSMatt Macy 			sendfs = nonpackage_sendfs;
5550eda14cbcSMatt Macy 			VERIFY(finalsnap == NULL);
5551eda14cbcSMatt Macy 		}
5552eda14cbcSMatt Macy 		return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
5553eda14cbcSMatt Macy 		    &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
5554eda14cbcSMatt Macy 		    finalsnap, cmdprops));
5555eda14cbcSMatt Macy 	} else {
5556eda14cbcSMatt Macy 		assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
5557eda14cbcSMatt Macy 		    DMU_COMPOUNDSTREAM);
5558eda14cbcSMatt Macy 		return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
5559eda14cbcSMatt Macy 		    &zcksum, top_zfs, cmdprops));
5560eda14cbcSMatt Macy 	}
5561eda14cbcSMatt Macy }
5562eda14cbcSMatt Macy 
5563eda14cbcSMatt Macy /*
5564eda14cbcSMatt Macy  * Restores a backup of tosnap from the file descriptor specified by infd.
5565eda14cbcSMatt Macy  * Return 0 on total success, -2 if some things couldn't be
5566eda14cbcSMatt Macy  * destroyed/renamed/promoted, -1 if some things couldn't be received.
5567eda14cbcSMatt Macy  * (-1 will override -2, if -1 and the resumable flag was specified the
5568eda14cbcSMatt Macy  * transfer can be resumed if the sending side supports it).
5569eda14cbcSMatt Macy  */
5570eda14cbcSMatt Macy int
5571eda14cbcSMatt Macy zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
5572eda14cbcSMatt Macy     recvflags_t *flags, int infd, avl_tree_t *stream_avl)
5573eda14cbcSMatt Macy {
5574eda14cbcSMatt Macy 	char *top_zfs = NULL;
5575eda14cbcSMatt Macy 	int err;
5576eda14cbcSMatt Macy 	struct stat sb;
55772a58b312SMartin Matuska 	const char *originsnap = NULL;
5578eda14cbcSMatt Macy 
5579eda14cbcSMatt Macy 	/*
5580eda14cbcSMatt Macy 	 * The only way fstat can fail is if we do not have a valid file
5581eda14cbcSMatt Macy 	 * descriptor.
5582eda14cbcSMatt Macy 	 */
5583eda14cbcSMatt Macy 	if (fstat(infd, &sb) == -1) {
5584eda14cbcSMatt Macy 		perror("fstat");
5585eda14cbcSMatt Macy 		return (-2);
5586eda14cbcSMatt Macy 	}
5587eda14cbcSMatt Macy 
5588eda14cbcSMatt Macy 	if (props) {
5589eda14cbcSMatt Macy 		err = nvlist_lookup_string(props, "origin", &originsnap);
5590eda14cbcSMatt Macy 		if (err && err != ENOENT)
5591eda14cbcSMatt Macy 			return (err);
5592eda14cbcSMatt Macy 	}
5593eda14cbcSMatt Macy 
5594eda14cbcSMatt Macy 	err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
5595eda14cbcSMatt Macy 	    stream_avl, &top_zfs, NULL, props);
5596eda14cbcSMatt Macy 
5597eda14cbcSMatt Macy 	if (err == 0 && !flags->nomount && flags->domount && top_zfs) {
5598eda14cbcSMatt Macy 		zfs_handle_t *zhp = NULL;
5599eda14cbcSMatt Macy 		prop_changelist_t *clp = NULL;
5600eda14cbcSMatt Macy 
5601eda14cbcSMatt Macy 		zhp = zfs_open(hdl, top_zfs,
5602eda14cbcSMatt Macy 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
5603eda14cbcSMatt Macy 		if (zhp == NULL) {
5604eda14cbcSMatt Macy 			err = -1;
5605eda14cbcSMatt Macy 			goto out;
5606eda14cbcSMatt Macy 		} else {
5607eda14cbcSMatt Macy 			if (zhp->zfs_type == ZFS_TYPE_VOLUME) {
5608eda14cbcSMatt Macy 				zfs_close(zhp);
5609eda14cbcSMatt Macy 				goto out;
5610eda14cbcSMatt Macy 			}
5611eda14cbcSMatt Macy 
5612eda14cbcSMatt Macy 			clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
5613eda14cbcSMatt Macy 			    CL_GATHER_MOUNT_ALWAYS,
5614eda14cbcSMatt Macy 			    flags->forceunmount ? MS_FORCE : 0);
5615eda14cbcSMatt Macy 			zfs_close(zhp);
5616eda14cbcSMatt Macy 			if (clp == NULL) {
5617eda14cbcSMatt Macy 				err = -1;
5618eda14cbcSMatt Macy 				goto out;
5619eda14cbcSMatt Macy 			}
5620eda14cbcSMatt Macy 
5621eda14cbcSMatt Macy 			/* mount and share received datasets */
5622eda14cbcSMatt Macy 			err = changelist_postfix(clp);
5623eda14cbcSMatt Macy 			changelist_free(clp);
5624eda14cbcSMatt Macy 			if (err != 0)
5625eda14cbcSMatt Macy 				err = -1;
5626eda14cbcSMatt Macy 		}
5627eda14cbcSMatt Macy 	}
5628eda14cbcSMatt Macy 
5629eda14cbcSMatt Macy out:
5630eda14cbcSMatt Macy 	if (top_zfs)
5631eda14cbcSMatt Macy 		free(top_zfs);
5632eda14cbcSMatt Macy 
5633eda14cbcSMatt Macy 	return (err);
5634eda14cbcSMatt Macy }
5635