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