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