xref: /titanic_50/usr/src/lib/libzfs/common/libzfs_sendrecv.c (revision ba3132cfb269da4118d7dc25a8c6c1a91f9f5303)
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 http://www.opensolaris.org/os/licensing.
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, 2015 by Delphix. All rights reserved.
25  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26  * Copyright (c) 2013 Steven Hartland. All rights reserved.
27  */
28 
29 #include <assert.h>
30 #include <ctype.h>
31 #include <errno.h>
32 #include <libintl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <strings.h>
36 #include <unistd.h>
37 #include <stddef.h>
38 #include <fcntl.h>
39 #include <sys/mount.h>
40 #include <pthread.h>
41 #include <umem.h>
42 #include <time.h>
43 
44 #include <libzfs.h>
45 #include <libzfs_core.h>
46 
47 #include "zfs_namecheck.h"
48 #include "zfs_prop.h"
49 #include "zfs_fletcher.h"
50 #include "libzfs_impl.h"
51 #include <zlib.h>
52 #include <sha2.h>
53 #include <sys/zio_checksum.h>
54 #include <sys/ddt.h>
55 
56 /* in libzfs_dataset.c */
57 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
58 
59 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
60     recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
61     uint64_t *);
62 static int guid_to_name(libzfs_handle_t *, const char *,
63     uint64_t, boolean_t, char *);
64 
65 static const zio_cksum_t zero_cksum = { 0 };
66 
67 typedef struct dedup_arg {
68 	int	inputfd;
69 	int	outputfd;
70 	libzfs_handle_t  *dedup_hdl;
71 } dedup_arg_t;
72 
73 typedef struct progress_arg {
74 	zfs_handle_t *pa_zhp;
75 	int pa_fd;
76 	boolean_t pa_parsable;
77 } progress_arg_t;
78 
79 typedef struct dataref {
80 	uint64_t ref_guid;
81 	uint64_t ref_object;
82 	uint64_t ref_offset;
83 } dataref_t;
84 
85 typedef struct dedup_entry {
86 	struct dedup_entry	*dde_next;
87 	zio_cksum_t dde_chksum;
88 	uint64_t dde_prop;
89 	dataref_t dde_ref;
90 } dedup_entry_t;
91 
92 #define	MAX_DDT_PHYSMEM_PERCENT		20
93 #define	SMALLEST_POSSIBLE_MAX_DDT_MB		128
94 
95 typedef struct dedup_table {
96 	dedup_entry_t	**dedup_hash_array;
97 	umem_cache_t	*ddecache;
98 	uint64_t	max_ddt_size;  /* max dedup table size in bytes */
99 	uint64_t	cur_ddt_size;  /* current dedup table size in bytes */
100 	uint64_t	ddt_count;
101 	int		numhashbits;
102 	boolean_t	ddt_full;
103 } dedup_table_t;
104 
105 static int
106 high_order_bit(uint64_t n)
107 {
108 	int count;
109 
110 	for (count = 0; n != 0; count++)
111 		n >>= 1;
112 	return (count);
113 }
114 
115 static size_t
116 ssread(void *buf, size_t len, FILE *stream)
117 {
118 	size_t outlen;
119 
120 	if ((outlen = fread(buf, len, 1, stream)) == 0)
121 		return (0);
122 
123 	return (outlen);
124 }
125 
126 static void
127 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
128     zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
129 {
130 	dedup_entry_t	*dde;
131 
132 	if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
133 		if (ddt->ddt_full == B_FALSE) {
134 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
135 			    "Dedup table full.  Deduplication will continue "
136 			    "with existing table entries"));
137 			ddt->ddt_full = B_TRUE;
138 		}
139 		return;
140 	}
141 
142 	if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
143 	    != NULL) {
144 		assert(*ddepp == NULL);
145 		dde->dde_next = NULL;
146 		dde->dde_chksum = *cs;
147 		dde->dde_prop = prop;
148 		dde->dde_ref = *dr;
149 		*ddepp = dde;
150 		ddt->cur_ddt_size += sizeof (dedup_entry_t);
151 		ddt->ddt_count++;
152 	}
153 }
154 
155 /*
156  * Using the specified dedup table, do a lookup for an entry with
157  * the checksum cs.  If found, return the block's reference info
158  * in *dr. Otherwise, insert a new entry in the dedup table, using
159  * the reference information specified by *dr.
160  *
161  * return value:  true - entry was found
162  *		  false - entry was not found
163  */
164 static boolean_t
165 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
166     uint64_t prop, dataref_t *dr)
167 {
168 	uint32_t hashcode;
169 	dedup_entry_t **ddepp;
170 
171 	hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
172 
173 	for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
174 	    ddepp = &((*ddepp)->dde_next)) {
175 		if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
176 		    (*ddepp)->dde_prop == prop) {
177 			*dr = (*ddepp)->dde_ref;
178 			return (B_TRUE);
179 		}
180 	}
181 	ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
182 	return (B_FALSE);
183 }
184 
185 static int
186 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
187     zio_cksum_t *zc, int outfd)
188 {
189 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
190 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
191 	fletcher_4_incremental_native(drr,
192 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
193 	if (drr->drr_type != DRR_BEGIN) {
194 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
195 		    drr_checksum.drr_checksum));
196 		drr->drr_u.drr_checksum.drr_checksum = *zc;
197 	}
198 	fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
199 	    sizeof (zio_cksum_t), zc);
200 	if (write(outfd, drr, sizeof (*drr)) == -1)
201 		return (errno);
202 	if (payload_len != 0) {
203 		fletcher_4_incremental_native(payload, payload_len, zc);
204 		if (write(outfd, payload, payload_len) == -1)
205 			return (errno);
206 	}
207 	return (0);
208 }
209 
210 /*
211  * This function is started in a separate thread when the dedup option
212  * has been requested.  The main send thread determines the list of
213  * snapshots to be included in the send stream and makes the ioctl calls
214  * for each one.  But instead of having the ioctl send the output to the
215  * the output fd specified by the caller of zfs_send()), the
216  * ioctl is told to direct the output to a pipe, which is read by the
217  * alternate thread running THIS function.  This function does the
218  * dedup'ing by:
219  *  1. building a dedup table (the DDT)
220  *  2. doing checksums on each data block and inserting a record in the DDT
221  *  3. looking for matching checksums, and
222  *  4.  sending a DRR_WRITE_BYREF record instead of a write record whenever
223  *      a duplicate block is found.
224  * The output of this function then goes to the output fd requested
225  * by the caller of zfs_send().
226  */
227 static void *
228 cksummer(void *arg)
229 {
230 	dedup_arg_t *dda = arg;
231 	char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
232 	dmu_replay_record_t thedrr;
233 	dmu_replay_record_t *drr = &thedrr;
234 	FILE *ofp;
235 	int outfd;
236 	dedup_table_t ddt;
237 	zio_cksum_t stream_cksum;
238 	uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
239 	uint64_t numbuckets;
240 
241 	ddt.max_ddt_size =
242 	    MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
243 	    SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
244 
245 	numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
246 
247 	/*
248 	 * numbuckets must be a power of 2.  Increase number to
249 	 * a power of 2 if necessary.
250 	 */
251 	if (!ISP2(numbuckets))
252 		numbuckets = 1 << high_order_bit(numbuckets);
253 
254 	ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
255 	ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
256 	    NULL, NULL, NULL, NULL, NULL, 0);
257 	ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
258 	ddt.numhashbits = high_order_bit(numbuckets) - 1;
259 	ddt.ddt_full = B_FALSE;
260 
261 	outfd = dda->outputfd;
262 	ofp = fdopen(dda->inputfd, "r");
263 	while (ssread(drr, sizeof (*drr), ofp) != 0) {
264 
265 		switch (drr->drr_type) {
266 		case DRR_BEGIN:
267 		{
268 			struct drr_begin *drrb = &drr->drr_u.drr_begin;
269 			int fflags;
270 			int sz = 0;
271 			ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
272 
273 			ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
274 
275 			/* set the DEDUP feature flag for this stream */
276 			fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
277 			fflags |= (DMU_BACKUP_FEATURE_DEDUP |
278 			    DMU_BACKUP_FEATURE_DEDUPPROPS);
279 			DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
280 
281 			if (drr->drr_payloadlen != 0) {
282 				sz = drr->drr_payloadlen;
283 
284 				if (sz > SPA_MAXBLOCKSIZE) {
285 					buf = zfs_realloc(dda->dedup_hdl, buf,
286 					    SPA_MAXBLOCKSIZE, sz);
287 				}
288 				(void) ssread(buf, sz, ofp);
289 				if (ferror(stdin))
290 					perror("fread");
291 			}
292 			if (dump_record(drr, buf, sz, &stream_cksum,
293 			    outfd) != 0)
294 				goto out;
295 			break;
296 		}
297 
298 		case DRR_END:
299 		{
300 			struct drr_end *drre = &drr->drr_u.drr_end;
301 			/* use the recalculated checksum */
302 			drre->drr_checksum = stream_cksum;
303 			if (dump_record(drr, NULL, 0, &stream_cksum,
304 			    outfd) != 0)
305 				goto out;
306 			break;
307 		}
308 
309 		case DRR_OBJECT:
310 		{
311 			struct drr_object *drro = &drr->drr_u.drr_object;
312 			if (drro->drr_bonuslen > 0) {
313 				(void) ssread(buf,
314 				    P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
315 				    ofp);
316 			}
317 			if (dump_record(drr, buf,
318 			    P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
319 			    &stream_cksum, outfd) != 0)
320 				goto out;
321 			break;
322 		}
323 
324 		case DRR_SPILL:
325 		{
326 			struct drr_spill *drrs = &drr->drr_u.drr_spill;
327 			(void) ssread(buf, drrs->drr_length, ofp);
328 			if (dump_record(drr, buf, drrs->drr_length,
329 			    &stream_cksum, outfd) != 0)
330 				goto out;
331 			break;
332 		}
333 
334 		case DRR_FREEOBJECTS:
335 		{
336 			if (dump_record(drr, NULL, 0, &stream_cksum,
337 			    outfd) != 0)
338 				goto out;
339 			break;
340 		}
341 
342 		case DRR_WRITE:
343 		{
344 			struct drr_write *drrw = &drr->drr_u.drr_write;
345 			dataref_t	dataref;
346 
347 			(void) ssread(buf, drrw->drr_length, ofp);
348 
349 			/*
350 			 * Use the existing checksum if it's dedup-capable,
351 			 * else calculate a SHA256 checksum for it.
352 			 */
353 
354 			if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
355 			    zero_cksum) ||
356 			    !DRR_IS_DEDUP_CAPABLE(drrw->drr_checksumflags)) {
357 				SHA256_CTX	ctx;
358 				zio_cksum_t	tmpsha256;
359 
360 				SHA256Init(&ctx);
361 				SHA256Update(&ctx, buf, drrw->drr_length);
362 				SHA256Final(&tmpsha256, &ctx);
363 				drrw->drr_key.ddk_cksum.zc_word[0] =
364 				    BE_64(tmpsha256.zc_word[0]);
365 				drrw->drr_key.ddk_cksum.zc_word[1] =
366 				    BE_64(tmpsha256.zc_word[1]);
367 				drrw->drr_key.ddk_cksum.zc_word[2] =
368 				    BE_64(tmpsha256.zc_word[2]);
369 				drrw->drr_key.ddk_cksum.zc_word[3] =
370 				    BE_64(tmpsha256.zc_word[3]);
371 				drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
372 				drrw->drr_checksumflags = DRR_CHECKSUM_DEDUP;
373 			}
374 
375 			dataref.ref_guid = drrw->drr_toguid;
376 			dataref.ref_object = drrw->drr_object;
377 			dataref.ref_offset = drrw->drr_offset;
378 
379 			if (ddt_update(dda->dedup_hdl, &ddt,
380 			    &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
381 			    &dataref)) {
382 				dmu_replay_record_t wbr_drr = {0};
383 				struct drr_write_byref *wbr_drrr =
384 				    &wbr_drr.drr_u.drr_write_byref;
385 
386 				/* block already present in stream */
387 				wbr_drr.drr_type = DRR_WRITE_BYREF;
388 
389 				wbr_drrr->drr_object = drrw->drr_object;
390 				wbr_drrr->drr_offset = drrw->drr_offset;
391 				wbr_drrr->drr_length = drrw->drr_length;
392 				wbr_drrr->drr_toguid = drrw->drr_toguid;
393 				wbr_drrr->drr_refguid = dataref.ref_guid;
394 				wbr_drrr->drr_refobject =
395 				    dataref.ref_object;
396 				wbr_drrr->drr_refoffset =
397 				    dataref.ref_offset;
398 
399 				wbr_drrr->drr_checksumtype =
400 				    drrw->drr_checksumtype;
401 				wbr_drrr->drr_checksumflags =
402 				    drrw->drr_checksumtype;
403 				wbr_drrr->drr_key.ddk_cksum =
404 				    drrw->drr_key.ddk_cksum;
405 				wbr_drrr->drr_key.ddk_prop =
406 				    drrw->drr_key.ddk_prop;
407 
408 				if (dump_record(&wbr_drr, NULL, 0,
409 				    &stream_cksum, outfd) != 0)
410 					goto out;
411 			} else {
412 				/* block not previously seen */
413 				if (dump_record(drr, buf, drrw->drr_length,
414 				    &stream_cksum, outfd) != 0)
415 					goto out;
416 			}
417 			break;
418 		}
419 
420 		case DRR_WRITE_EMBEDDED:
421 		{
422 			struct drr_write_embedded *drrwe =
423 			    &drr->drr_u.drr_write_embedded;
424 			(void) ssread(buf,
425 			    P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
426 			if (dump_record(drr, buf,
427 			    P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
428 			    &stream_cksum, outfd) != 0)
429 				goto out;
430 			break;
431 		}
432 
433 		case DRR_FREE:
434 		{
435 			if (dump_record(drr, NULL, 0, &stream_cksum,
436 			    outfd) != 0)
437 				goto out;
438 			break;
439 		}
440 
441 		default:
442 			(void) fprintf(stderr, "INVALID record type 0x%x\n",
443 			    drr->drr_type);
444 			/* should never happen, so assert */
445 			assert(B_FALSE);
446 		}
447 	}
448 out:
449 	umem_cache_destroy(ddt.ddecache);
450 	free(ddt.dedup_hash_array);
451 	free(buf);
452 	(void) fclose(ofp);
453 
454 	return (NULL);
455 }
456 
457 /*
458  * Routines for dealing with the AVL tree of fs-nvlists
459  */
460 typedef struct fsavl_node {
461 	avl_node_t fn_node;
462 	nvlist_t *fn_nvfs;
463 	char *fn_snapname;
464 	uint64_t fn_guid;
465 } fsavl_node_t;
466 
467 static int
468 fsavl_compare(const void *arg1, const void *arg2)
469 {
470 	const fsavl_node_t *fn1 = arg1;
471 	const fsavl_node_t *fn2 = arg2;
472 
473 	if (fn1->fn_guid > fn2->fn_guid)
474 		return (+1);
475 	else if (fn1->fn_guid < fn2->fn_guid)
476 		return (-1);
477 	else
478 		return (0);
479 }
480 
481 /*
482  * Given the GUID of a snapshot, find its containing filesystem and
483  * (optionally) name.
484  */
485 static nvlist_t *
486 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
487 {
488 	fsavl_node_t fn_find;
489 	fsavl_node_t *fn;
490 
491 	fn_find.fn_guid = snapguid;
492 
493 	fn = avl_find(avl, &fn_find, NULL);
494 	if (fn) {
495 		if (snapname)
496 			*snapname = fn->fn_snapname;
497 		return (fn->fn_nvfs);
498 	}
499 	return (NULL);
500 }
501 
502 static void
503 fsavl_destroy(avl_tree_t *avl)
504 {
505 	fsavl_node_t *fn;
506 	void *cookie;
507 
508 	if (avl == NULL)
509 		return;
510 
511 	cookie = NULL;
512 	while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
513 		free(fn);
514 	avl_destroy(avl);
515 	free(avl);
516 }
517 
518 /*
519  * Given an nvlist, produce an avl tree of snapshots, ordered by guid
520  */
521 static avl_tree_t *
522 fsavl_create(nvlist_t *fss)
523 {
524 	avl_tree_t *fsavl;
525 	nvpair_t *fselem = NULL;
526 
527 	if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
528 		return (NULL);
529 
530 	avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
531 	    offsetof(fsavl_node_t, fn_node));
532 
533 	while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
534 		nvlist_t *nvfs, *snaps;
535 		nvpair_t *snapelem = NULL;
536 
537 		VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
538 		VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
539 
540 		while ((snapelem =
541 		    nvlist_next_nvpair(snaps, snapelem)) != NULL) {
542 			fsavl_node_t *fn;
543 			uint64_t guid;
544 
545 			VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
546 			if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
547 				fsavl_destroy(fsavl);
548 				return (NULL);
549 			}
550 			fn->fn_nvfs = nvfs;
551 			fn->fn_snapname = nvpair_name(snapelem);
552 			fn->fn_guid = guid;
553 
554 			/*
555 			 * Note: if there are multiple snaps with the
556 			 * same GUID, we ignore all but one.
557 			 */
558 			if (avl_find(fsavl, fn, NULL) == NULL)
559 				avl_add(fsavl, fn);
560 			else
561 				free(fn);
562 		}
563 	}
564 
565 	return (fsavl);
566 }
567 
568 /*
569  * Routines for dealing with the giant nvlist of fs-nvlists, etc.
570  */
571 typedef struct send_data {
572 	uint64_t parent_fromsnap_guid;
573 	nvlist_t *parent_snaps;
574 	nvlist_t *fss;
575 	nvlist_t *snapprops;
576 	const char *fromsnap;
577 	const char *tosnap;
578 	boolean_t recursive;
579 
580 	/*
581 	 * The header nvlist is of the following format:
582 	 * {
583 	 *   "tosnap" -> string
584 	 *   "fromsnap" -> string (if incremental)
585 	 *   "fss" -> {
586 	 *	id -> {
587 	 *
588 	 *	 "name" -> string (full name; for debugging)
589 	 *	 "parentfromsnap" -> number (guid of fromsnap in parent)
590 	 *
591 	 *	 "props" -> { name -> value (only if set here) }
592 	 *	 "snaps" -> { name (lastname) -> number (guid) }
593 	 *	 "snapprops" -> { name (lastname) -> { name -> value } }
594 	 *
595 	 *	 "origin" -> number (guid) (if clone)
596 	 *	 "sent" -> boolean (not on-disk)
597 	 *	}
598 	 *   }
599 	 * }
600 	 *
601 	 */
602 } send_data_t;
603 
604 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
605 
606 static int
607 send_iterate_snap(zfs_handle_t *zhp, void *arg)
608 {
609 	send_data_t *sd = arg;
610 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
611 	char *snapname;
612 	nvlist_t *nv;
613 
614 	snapname = strrchr(zhp->zfs_name, '@')+1;
615 
616 	VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
617 	/*
618 	 * NB: if there is no fromsnap here (it's a newly created fs in
619 	 * an incremental replication), we will substitute the tosnap.
620 	 */
621 	if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
622 	    (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
623 	    strcmp(snapname, sd->tosnap) == 0)) {
624 		sd->parent_fromsnap_guid = guid;
625 	}
626 
627 	VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
628 	send_iterate_prop(zhp, nv);
629 	VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
630 	nvlist_free(nv);
631 
632 	zfs_close(zhp);
633 	return (0);
634 }
635 
636 static void
637 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
638 {
639 	nvpair_t *elem = NULL;
640 
641 	while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
642 		char *propname = nvpair_name(elem);
643 		zfs_prop_t prop = zfs_name_to_prop(propname);
644 		nvlist_t *propnv;
645 
646 		if (!zfs_prop_user(propname)) {
647 			/*
648 			 * Realistically, this should never happen.  However,
649 			 * we want the ability to add DSL properties without
650 			 * needing to make incompatible version changes.  We
651 			 * need to ignore unknown properties to allow older
652 			 * software to still send datasets containing these
653 			 * properties, with the unknown properties elided.
654 			 */
655 			if (prop == ZPROP_INVAL)
656 				continue;
657 
658 			if (zfs_prop_readonly(prop))
659 				continue;
660 		}
661 
662 		verify(nvpair_value_nvlist(elem, &propnv) == 0);
663 		if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
664 		    prop == ZFS_PROP_REFQUOTA ||
665 		    prop == ZFS_PROP_REFRESERVATION) {
666 			char *source;
667 			uint64_t value;
668 			verify(nvlist_lookup_uint64(propnv,
669 			    ZPROP_VALUE, &value) == 0);
670 			if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
671 				continue;
672 			/*
673 			 * May have no source before SPA_VERSION_RECVD_PROPS,
674 			 * but is still modifiable.
675 			 */
676 			if (nvlist_lookup_string(propnv,
677 			    ZPROP_SOURCE, &source) == 0) {
678 				if ((strcmp(source, zhp->zfs_name) != 0) &&
679 				    (strcmp(source,
680 				    ZPROP_SOURCE_VAL_RECVD) != 0))
681 					continue;
682 			}
683 		} else {
684 			char *source;
685 			if (nvlist_lookup_string(propnv,
686 			    ZPROP_SOURCE, &source) != 0)
687 				continue;
688 			if ((strcmp(source, zhp->zfs_name) != 0) &&
689 			    (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
690 				continue;
691 		}
692 
693 		if (zfs_prop_user(propname) ||
694 		    zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
695 			char *value;
696 			verify(nvlist_lookup_string(propnv,
697 			    ZPROP_VALUE, &value) == 0);
698 			VERIFY(0 == nvlist_add_string(nv, propname, value));
699 		} else {
700 			uint64_t value;
701 			verify(nvlist_lookup_uint64(propnv,
702 			    ZPROP_VALUE, &value) == 0);
703 			VERIFY(0 == nvlist_add_uint64(nv, propname, value));
704 		}
705 	}
706 }
707 
708 /*
709  * recursively generate nvlists describing datasets.  See comment
710  * for the data structure send_data_t above for description of contents
711  * of the nvlist.
712  */
713 static int
714 send_iterate_fs(zfs_handle_t *zhp, void *arg)
715 {
716 	send_data_t *sd = arg;
717 	nvlist_t *nvfs, *nv;
718 	int rv = 0;
719 	uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
720 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
721 	char guidstring[64];
722 
723 	VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
724 	VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
725 	VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
726 	    sd->parent_fromsnap_guid));
727 
728 	if (zhp->zfs_dmustats.dds_origin[0]) {
729 		zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
730 		    zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
731 		if (origin == NULL)
732 			return (-1);
733 		VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
734 		    origin->zfs_dmustats.dds_guid));
735 	}
736 
737 	/* iterate over props */
738 	VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
739 	send_iterate_prop(zhp, nv);
740 	VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
741 	nvlist_free(nv);
742 
743 	/* iterate over snaps, and set sd->parent_fromsnap_guid */
744 	sd->parent_fromsnap_guid = 0;
745 	VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
746 	VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
747 	(void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
748 	VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
749 	VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
750 	nvlist_free(sd->parent_snaps);
751 	nvlist_free(sd->snapprops);
752 
753 	/* add this fs to nvlist */
754 	(void) snprintf(guidstring, sizeof (guidstring),
755 	    "0x%llx", (longlong_t)guid);
756 	VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
757 	nvlist_free(nvfs);
758 
759 	/* iterate over children */
760 	if (sd->recursive)
761 		rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
762 
763 	sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
764 
765 	zfs_close(zhp);
766 	return (rv);
767 }
768 
769 static int
770 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
771     const char *tosnap, boolean_t recursive, nvlist_t **nvlp, avl_tree_t **avlp)
772 {
773 	zfs_handle_t *zhp;
774 	send_data_t sd = { 0 };
775 	int error;
776 
777 	zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
778 	if (zhp == NULL)
779 		return (EZFS_BADTYPE);
780 
781 	VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
782 	sd.fromsnap = fromsnap;
783 	sd.tosnap = tosnap;
784 	sd.recursive = recursive;
785 
786 	if ((error = send_iterate_fs(zhp, &sd)) != 0) {
787 		nvlist_free(sd.fss);
788 		if (avlp != NULL)
789 			*avlp = NULL;
790 		*nvlp = NULL;
791 		return (error);
792 	}
793 
794 	if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
795 		nvlist_free(sd.fss);
796 		*nvlp = NULL;
797 		return (EZFS_NOMEM);
798 	}
799 
800 	*nvlp = sd.fss;
801 	return (0);
802 }
803 
804 /*
805  * Routines specific to "zfs send"
806  */
807 typedef struct send_dump_data {
808 	/* these are all just the short snapname (the part after the @) */
809 	const char *fromsnap;
810 	const char *tosnap;
811 	char prevsnap[ZFS_MAXNAMELEN];
812 	uint64_t prevsnap_obj;
813 	boolean_t seenfrom, seento, replicate, doall, fromorigin;
814 	boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
815 	boolean_t large_block;
816 	int outfd;
817 	boolean_t err;
818 	nvlist_t *fss;
819 	nvlist_t *snapholds;
820 	avl_tree_t *fsavl;
821 	snapfilter_cb_t *filter_cb;
822 	void *filter_cb_arg;
823 	nvlist_t *debugnv;
824 	char holdtag[ZFS_MAXNAMELEN];
825 	int cleanup_fd;
826 	uint64_t size;
827 } send_dump_data_t;
828 
829 static int
830 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
831     boolean_t fromorigin, uint64_t *sizep)
832 {
833 	zfs_cmd_t zc = { 0 };
834 	libzfs_handle_t *hdl = zhp->zfs_hdl;
835 
836 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
837 	assert(fromsnap_obj == 0 || !fromorigin);
838 
839 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
840 	zc.zc_obj = fromorigin;
841 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
842 	zc.zc_fromobj = fromsnap_obj;
843 	zc.zc_guid = 1;  /* estimate flag */
844 
845 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
846 		char errbuf[1024];
847 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
848 		    "warning: cannot estimate space for '%s'"), zhp->zfs_name);
849 
850 		switch (errno) {
851 		case EXDEV:
852 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
853 			    "not an earlier snapshot from the same fs"));
854 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
855 
856 		case ENOENT:
857 			if (zfs_dataset_exists(hdl, zc.zc_name,
858 			    ZFS_TYPE_SNAPSHOT)) {
859 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
860 				    "incremental source (@%s) does not exist"),
861 				    zc.zc_value);
862 			}
863 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
864 
865 		case EDQUOT:
866 		case EFBIG:
867 		case EIO:
868 		case ENOLINK:
869 		case ENOSPC:
870 		case ENOSTR:
871 		case ENXIO:
872 		case EPIPE:
873 		case ERANGE:
874 		case EFAULT:
875 		case EROFS:
876 			zfs_error_aux(hdl, strerror(errno));
877 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
878 
879 		default:
880 			return (zfs_standard_error(hdl, errno, errbuf));
881 		}
882 	}
883 
884 	*sizep = zc.zc_objset_type;
885 
886 	return (0);
887 }
888 
889 /*
890  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
891  * NULL) to the file descriptor specified by outfd.
892  */
893 static int
894 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
895     boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
896     nvlist_t *debugnv)
897 {
898 	zfs_cmd_t zc = { 0 };
899 	libzfs_handle_t *hdl = zhp->zfs_hdl;
900 	nvlist_t *thisdbg;
901 
902 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
903 	assert(fromsnap_obj == 0 || !fromorigin);
904 
905 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
906 	zc.zc_cookie = outfd;
907 	zc.zc_obj = fromorigin;
908 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
909 	zc.zc_fromobj = fromsnap_obj;
910 	zc.zc_flags = flags;
911 
912 	VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
913 	if (fromsnap && fromsnap[0] != '\0') {
914 		VERIFY(0 == nvlist_add_string(thisdbg,
915 		    "fromsnap", fromsnap));
916 	}
917 
918 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
919 		char errbuf[1024];
920 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
921 		    "warning: cannot send '%s'"), zhp->zfs_name);
922 
923 		VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
924 		if (debugnv) {
925 			VERIFY(0 == nvlist_add_nvlist(debugnv,
926 			    zhp->zfs_name, thisdbg));
927 		}
928 		nvlist_free(thisdbg);
929 
930 		switch (errno) {
931 		case EXDEV:
932 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
933 			    "not an earlier snapshot from the same fs"));
934 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
935 
936 		case ENOENT:
937 			if (zfs_dataset_exists(hdl, zc.zc_name,
938 			    ZFS_TYPE_SNAPSHOT)) {
939 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
940 				    "incremental source (@%s) does not exist"),
941 				    zc.zc_value);
942 			}
943 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
944 
945 		case EDQUOT:
946 		case EFBIG:
947 		case EIO:
948 		case ENOLINK:
949 		case ENOSPC:
950 		case ENOSTR:
951 		case ENXIO:
952 		case EPIPE:
953 		case ERANGE:
954 		case EFAULT:
955 		case EROFS:
956 			zfs_error_aux(hdl, strerror(errno));
957 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
958 
959 		default:
960 			return (zfs_standard_error(hdl, errno, errbuf));
961 		}
962 	}
963 
964 	if (debugnv)
965 		VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
966 	nvlist_free(thisdbg);
967 
968 	return (0);
969 }
970 
971 static void
972 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
973 {
974 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
975 
976 	/*
977 	 * zfs_send() only sets snapholds for sends that need them,
978 	 * e.g. replication and doall.
979 	 */
980 	if (sdd->snapholds == NULL)
981 		return;
982 
983 	fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
984 }
985 
986 static void *
987 send_progress_thread(void *arg)
988 {
989 	progress_arg_t *pa = arg;
990 	zfs_cmd_t zc = { 0 };
991 	zfs_handle_t *zhp = pa->pa_zhp;
992 	libzfs_handle_t *hdl = zhp->zfs_hdl;
993 	unsigned long long bytes;
994 	char buf[16];
995 	time_t t;
996 	struct tm *tm;
997 
998 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
999 
1000 	if (!pa->pa_parsable)
1001 		(void) fprintf(stderr, "TIME        SENT   SNAPSHOT\n");
1002 
1003 	/*
1004 	 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1005 	 */
1006 	for (;;) {
1007 		(void) sleep(1);
1008 
1009 		zc.zc_cookie = pa->pa_fd;
1010 		if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1011 			return ((void *)-1);
1012 
1013 		(void) time(&t);
1014 		tm = localtime(&t);
1015 		bytes = zc.zc_cookie;
1016 
1017 		if (pa->pa_parsable) {
1018 			(void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1019 			    tm->tm_hour, tm->tm_min, tm->tm_sec,
1020 			    bytes, zhp->zfs_name);
1021 		} else {
1022 			zfs_nicenum(bytes, buf, sizeof (buf));
1023 			(void) fprintf(stderr, "%02d:%02d:%02d   %5s   %s\n",
1024 			    tm->tm_hour, tm->tm_min, tm->tm_sec,
1025 			    buf, zhp->zfs_name);
1026 		}
1027 	}
1028 }
1029 
1030 static void
1031 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1032     uint64_t size, boolean_t parsable)
1033 {
1034 	if (parsable) {
1035 		if (fromsnap != NULL) {
1036 			(void) fprintf(fout, "incremental\t%s\t%s",
1037 			    fromsnap, tosnap);
1038 		} else {
1039 			(void) fprintf(fout, "full\t%s",
1040 			    tosnap);
1041 		}
1042 	} else {
1043 		if (fromsnap != NULL) {
1044 			if (strchr(fromsnap, '@') == NULL &&
1045 			    strchr(fromsnap, '#') == NULL) {
1046 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1047 				    "send from @%s to %s"),
1048 				    fromsnap, tosnap);
1049 			} else {
1050 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1051 				    "send from %s to %s"),
1052 				    fromsnap, tosnap);
1053 			}
1054 		} else {
1055 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1056 			    "full send of %s"),
1057 			    tosnap);
1058 		}
1059 	}
1060 
1061 	if (size != 0) {
1062 		if (parsable) {
1063 			(void) fprintf(fout, "\t%llu",
1064 			    (longlong_t)size);
1065 		} else {
1066 			char buf[16];
1067 			zfs_nicenum(size, buf, sizeof (buf));
1068 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1069 			    " estimated size is %s"), buf);
1070 		}
1071 	}
1072 	(void) fprintf(fout, "\n");
1073 }
1074 
1075 static int
1076 dump_snapshot(zfs_handle_t *zhp, void *arg)
1077 {
1078 	send_dump_data_t *sdd = arg;
1079 	progress_arg_t pa = { 0 };
1080 	pthread_t tid;
1081 	char *thissnap;
1082 	int err;
1083 	boolean_t isfromsnap, istosnap, fromorigin;
1084 	boolean_t exclude = B_FALSE;
1085 	FILE *fout = sdd->std_out ? stdout : stderr;
1086 
1087 	err = 0;
1088 	thissnap = strchr(zhp->zfs_name, '@') + 1;
1089 	isfromsnap = (sdd->fromsnap != NULL &&
1090 	    strcmp(sdd->fromsnap, thissnap) == 0);
1091 
1092 	if (!sdd->seenfrom && isfromsnap) {
1093 		gather_holds(zhp, sdd);
1094 		sdd->seenfrom = B_TRUE;
1095 		(void) strcpy(sdd->prevsnap, thissnap);
1096 		sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1097 		zfs_close(zhp);
1098 		return (0);
1099 	}
1100 
1101 	if (sdd->seento || !sdd->seenfrom) {
1102 		zfs_close(zhp);
1103 		return (0);
1104 	}
1105 
1106 	istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1107 	if (istosnap)
1108 		sdd->seento = B_TRUE;
1109 
1110 	if (!sdd->doall && !isfromsnap && !istosnap) {
1111 		if (sdd->replicate) {
1112 			char *snapname;
1113 			nvlist_t *snapprops;
1114 			/*
1115 			 * Filter out all intermediate snapshots except origin
1116 			 * snapshots needed to replicate clones.
1117 			 */
1118 			nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1119 			    zhp->zfs_dmustats.dds_guid, &snapname);
1120 
1121 			VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1122 			    "snapprops", &snapprops));
1123 			VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1124 			    thissnap, &snapprops));
1125 			exclude = !nvlist_exists(snapprops, "is_clone_origin");
1126 		} else {
1127 			exclude = B_TRUE;
1128 		}
1129 	}
1130 
1131 	/*
1132 	 * If a filter function exists, call it to determine whether
1133 	 * this snapshot will be sent.
1134 	 */
1135 	if (exclude || (sdd->filter_cb != NULL &&
1136 	    sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1137 		/*
1138 		 * This snapshot is filtered out.  Don't send it, and don't
1139 		 * set prevsnap_obj, so it will be as if this snapshot didn't
1140 		 * exist, and the next accepted snapshot will be sent as
1141 		 * an incremental from the last accepted one, or as the
1142 		 * first (and full) snapshot in the case of a replication,
1143 		 * non-incremental send.
1144 		 */
1145 		zfs_close(zhp);
1146 		return (0);
1147 	}
1148 
1149 	gather_holds(zhp, sdd);
1150 	fromorigin = sdd->prevsnap[0] == '\0' &&
1151 	    (sdd->fromorigin || sdd->replicate);
1152 
1153 	if (sdd->verbose) {
1154 		uint64_t size = 0;
1155 		(void) estimate_ioctl(zhp, sdd->prevsnap_obj,
1156 		    fromorigin, &size);
1157 
1158 		send_print_verbose(fout, zhp->zfs_name,
1159 		    sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1160 		    size, sdd->parsable);
1161 		sdd->size += size;
1162 	}
1163 
1164 	if (!sdd->dryrun) {
1165 		/*
1166 		 * If progress reporting is requested, spawn a new thread to
1167 		 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1168 		 */
1169 		if (sdd->progress) {
1170 			pa.pa_zhp = zhp;
1171 			pa.pa_fd = sdd->outfd;
1172 			pa.pa_parsable = sdd->parsable;
1173 
1174 			if (err = pthread_create(&tid, NULL,
1175 			    send_progress_thread, &pa)) {
1176 				zfs_close(zhp);
1177 				return (err);
1178 			}
1179 		}
1180 
1181 		enum lzc_send_flags flags = 0;
1182 		if (sdd->large_block)
1183 			flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1184 		if (sdd->embed_data)
1185 			flags |= LZC_SEND_FLAG_EMBED_DATA;
1186 
1187 		err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1188 		    fromorigin, sdd->outfd, flags, sdd->debugnv);
1189 
1190 		if (sdd->progress) {
1191 			(void) pthread_cancel(tid);
1192 			(void) pthread_join(tid, NULL);
1193 		}
1194 	}
1195 
1196 	(void) strcpy(sdd->prevsnap, thissnap);
1197 	sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1198 	zfs_close(zhp);
1199 	return (err);
1200 }
1201 
1202 static int
1203 dump_filesystem(zfs_handle_t *zhp, void *arg)
1204 {
1205 	int rv = 0;
1206 	send_dump_data_t *sdd = arg;
1207 	boolean_t missingfrom = B_FALSE;
1208 	zfs_cmd_t zc = { 0 };
1209 
1210 	(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1211 	    zhp->zfs_name, sdd->tosnap);
1212 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1213 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1214 		    "WARNING: could not send %s@%s: does not exist\n"),
1215 		    zhp->zfs_name, sdd->tosnap);
1216 		sdd->err = B_TRUE;
1217 		return (0);
1218 	}
1219 
1220 	if (sdd->replicate && sdd->fromsnap) {
1221 		/*
1222 		 * If this fs does not have fromsnap, and we're doing
1223 		 * recursive, we need to send a full stream from the
1224 		 * beginning (or an incremental from the origin if this
1225 		 * is a clone).  If we're doing non-recursive, then let
1226 		 * them get the error.
1227 		 */
1228 		(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1229 		    zhp->zfs_name, sdd->fromsnap);
1230 		if (ioctl(zhp->zfs_hdl->libzfs_fd,
1231 		    ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1232 			missingfrom = B_TRUE;
1233 		}
1234 	}
1235 
1236 	sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1237 	sdd->prevsnap_obj = 0;
1238 	if (sdd->fromsnap == NULL || missingfrom)
1239 		sdd->seenfrom = B_TRUE;
1240 
1241 	rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1242 	if (!sdd->seenfrom) {
1243 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1244 		    "WARNING: could not send %s@%s:\n"
1245 		    "incremental source (%s@%s) does not exist\n"),
1246 		    zhp->zfs_name, sdd->tosnap,
1247 		    zhp->zfs_name, sdd->fromsnap);
1248 		sdd->err = B_TRUE;
1249 	} else if (!sdd->seento) {
1250 		if (sdd->fromsnap) {
1251 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1252 			    "WARNING: could not send %s@%s:\n"
1253 			    "incremental source (%s@%s) "
1254 			    "is not earlier than it\n"),
1255 			    zhp->zfs_name, sdd->tosnap,
1256 			    zhp->zfs_name, sdd->fromsnap);
1257 		} else {
1258 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1259 			    "WARNING: "
1260 			    "could not send %s@%s: does not exist\n"),
1261 			    zhp->zfs_name, sdd->tosnap);
1262 		}
1263 		sdd->err = B_TRUE;
1264 	}
1265 
1266 	return (rv);
1267 }
1268 
1269 static int
1270 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1271 {
1272 	send_dump_data_t *sdd = arg;
1273 	nvpair_t *fspair;
1274 	boolean_t needagain, progress;
1275 
1276 	if (!sdd->replicate)
1277 		return (dump_filesystem(rzhp, sdd));
1278 
1279 	/* Mark the clone origin snapshots. */
1280 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1281 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1282 		nvlist_t *nvfs;
1283 		uint64_t origin_guid = 0;
1284 
1285 		VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1286 		(void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1287 		if (origin_guid != 0) {
1288 			char *snapname;
1289 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1290 			    origin_guid, &snapname);
1291 			if (origin_nv != NULL) {
1292 				nvlist_t *snapprops;
1293 				VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1294 				    "snapprops", &snapprops));
1295 				VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1296 				    snapname, &snapprops));
1297 				VERIFY(0 == nvlist_add_boolean(
1298 				    snapprops, "is_clone_origin"));
1299 			}
1300 		}
1301 	}
1302 again:
1303 	needagain = progress = B_FALSE;
1304 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1305 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1306 		nvlist_t *fslist, *parent_nv;
1307 		char *fsname;
1308 		zfs_handle_t *zhp;
1309 		int err;
1310 		uint64_t origin_guid = 0;
1311 		uint64_t parent_guid = 0;
1312 
1313 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1314 		if (nvlist_lookup_boolean(fslist, "sent") == 0)
1315 			continue;
1316 
1317 		VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1318 		(void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1319 		(void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1320 		    &parent_guid);
1321 
1322 		if (parent_guid != 0) {
1323 			parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1324 			if (!nvlist_exists(parent_nv, "sent")) {
1325 				/* parent has not been sent; skip this one */
1326 				needagain = B_TRUE;
1327 				continue;
1328 			}
1329 		}
1330 
1331 		if (origin_guid != 0) {
1332 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1333 			    origin_guid, NULL);
1334 			if (origin_nv != NULL &&
1335 			    !nvlist_exists(origin_nv, "sent")) {
1336 				/*
1337 				 * origin has not been sent yet;
1338 				 * skip this clone.
1339 				 */
1340 				needagain = B_TRUE;
1341 				continue;
1342 			}
1343 		}
1344 
1345 		zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1346 		if (zhp == NULL)
1347 			return (-1);
1348 		err = dump_filesystem(zhp, sdd);
1349 		VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1350 		progress = B_TRUE;
1351 		zfs_close(zhp);
1352 		if (err)
1353 			return (err);
1354 	}
1355 	if (needagain) {
1356 		assert(progress);
1357 		goto again;
1358 	}
1359 
1360 	/* clean out the sent flags in case we reuse this fss */
1361 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1362 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1363 		nvlist_t *fslist;
1364 
1365 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1366 		(void) nvlist_remove_all(fslist, "sent");
1367 	}
1368 
1369 	return (0);
1370 }
1371 
1372 nvlist_t *
1373 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1374 {
1375 	unsigned int version;
1376 	int nread;
1377 	unsigned long long checksum, packed_len;
1378 
1379 	/*
1380 	 * Decode token header, which is:
1381 	 *   <token version>-<checksum of payload>-<uncompressed payload length>
1382 	 * Note that the only supported token version is 1.
1383 	 */
1384 	nread = sscanf(token, "%u-%llx-%llx-",
1385 	    &version, &checksum, &packed_len);
1386 	if (nread != 3) {
1387 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1388 		    "resume token is corrupt (invalid format)"));
1389 		return (NULL);
1390 	}
1391 
1392 	if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1393 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1394 		    "resume token is corrupt (invalid version %u)"),
1395 		    version);
1396 		return (NULL);
1397 	}
1398 
1399 	/* convert hexadecimal representation to binary */
1400 	token = strrchr(token, '-') + 1;
1401 	int len = strlen(token) / 2;
1402 	unsigned char *compressed = zfs_alloc(hdl, len);
1403 	for (int i = 0; i < len; i++) {
1404 		nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1405 		if (nread != 1) {
1406 			free(compressed);
1407 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1408 			    "resume token is corrupt "
1409 			    "(payload is not hex-encoded)"));
1410 			return (NULL);
1411 		}
1412 	}
1413 
1414 	/* verify checksum */
1415 	zio_cksum_t cksum;
1416 	fletcher_4_native(compressed, len, NULL, &cksum);
1417 	if (cksum.zc_word[0] != checksum) {
1418 		free(compressed);
1419 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1420 		    "resume token is corrupt (incorrect checksum)"));
1421 		return (NULL);
1422 	}
1423 
1424 	/* uncompress */
1425 	void *packed = zfs_alloc(hdl, packed_len);
1426 	uLongf packed_len_long = packed_len;
1427 	if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1428 	    packed_len_long != packed_len) {
1429 		free(packed);
1430 		free(compressed);
1431 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1432 		    "resume token is corrupt (decompression failed)"));
1433 		return (NULL);
1434 	}
1435 
1436 	/* unpack nvlist */
1437 	nvlist_t *nv;
1438 	int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1439 	free(packed);
1440 	free(compressed);
1441 	if (error != 0) {
1442 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1443 		    "resume token is corrupt (nvlist_unpack failed)"));
1444 		return (NULL);
1445 	}
1446 	return (nv);
1447 }
1448 
1449 int
1450 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1451     const char *resume_token)
1452 {
1453 	char errbuf[1024];
1454 	char *toname;
1455 	char *fromname = NULL;
1456 	uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1457 	zfs_handle_t *zhp;
1458 	int error = 0;
1459 	char name[ZFS_MAXNAMELEN];
1460 	enum lzc_send_flags lzc_flags = 0;
1461 
1462 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1463 	    "cannot resume send"));
1464 
1465 	nvlist_t *resume_nvl =
1466 	    zfs_send_resume_token_to_nvlist(hdl, resume_token);
1467 	if (resume_nvl == NULL) {
1468 		/*
1469 		 * zfs_error_aux has already been set by
1470 		 * zfs_send_resume_token_to_nvlist
1471 		 */
1472 		return (zfs_error(hdl, EZFS_FAULT, errbuf));
1473 	}
1474 	if (flags->verbose) {
1475 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1476 		    "resume token contents:\n"));
1477 		nvlist_print(stderr, resume_nvl);
1478 	}
1479 
1480 	if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1481 	    nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1482 	    nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1483 	    nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1484 	    nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1485 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1486 		    "resume token is corrupt"));
1487 		return (zfs_error(hdl, EZFS_FAULT, errbuf));
1488 	}
1489 	fromguid = 0;
1490 	(void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1491 
1492 	if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1493 		lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1494 
1495 	if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1496 		if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1497 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1498 			    "'%s' is no longer the same snapshot used in "
1499 			    "the initial send"), toname);
1500 		} else {
1501 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1502 			    "'%s' used in the initial send no longer exists"),
1503 			    toname);
1504 		}
1505 		return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1506 	}
1507 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1508 	if (zhp == NULL) {
1509 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1510 		    "unable to access '%s'"), name);
1511 		return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1512 	}
1513 
1514 	if (fromguid != 0) {
1515 		if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1516 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1517 			    "incremental source %#llx no longer exists"),
1518 			    (longlong_t)fromguid);
1519 			return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1520 		}
1521 		fromname = name;
1522 	}
1523 
1524 	if (flags->verbose) {
1525 		uint64_t size = 0;
1526 		error = lzc_send_space(zhp->zfs_name, fromname, &size);
1527 		if (error == 0)
1528 			size = MAX(0, (int64_t)(size - bytes));
1529 		send_print_verbose(stderr, zhp->zfs_name, fromname,
1530 		    size, flags->parsable);
1531 	}
1532 
1533 	if (!flags->dryrun) {
1534 		progress_arg_t pa = { 0 };
1535 		pthread_t tid;
1536 		/*
1537 		 * If progress reporting is requested, spawn a new thread to
1538 		 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1539 		 */
1540 		if (flags->progress) {
1541 			pa.pa_zhp = zhp;
1542 			pa.pa_fd = outfd;
1543 			pa.pa_parsable = flags->parsable;
1544 
1545 			error = pthread_create(&tid, NULL,
1546 			    send_progress_thread, &pa);
1547 			if (error != 0) {
1548 				zfs_close(zhp);
1549 				return (error);
1550 			}
1551 		}
1552 
1553 		error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1554 		    lzc_flags, resumeobj, resumeoff);
1555 
1556 		if (flags->progress) {
1557 			(void) pthread_cancel(tid);
1558 			(void) pthread_join(tid, NULL);
1559 		}
1560 
1561 		char errbuf[1024];
1562 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1563 		    "warning: cannot send '%s'"), zhp->zfs_name);
1564 
1565 		zfs_close(zhp);
1566 
1567 		switch (error) {
1568 		case 0:
1569 			return (0);
1570 		case EXDEV:
1571 		case ENOENT:
1572 		case EDQUOT:
1573 		case EFBIG:
1574 		case EIO:
1575 		case ENOLINK:
1576 		case ENOSPC:
1577 		case ENOSTR:
1578 		case ENXIO:
1579 		case EPIPE:
1580 		case ERANGE:
1581 		case EFAULT:
1582 		case EROFS:
1583 			zfs_error_aux(hdl, strerror(errno));
1584 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1585 
1586 		default:
1587 			return (zfs_standard_error(hdl, errno, errbuf));
1588 		}
1589 	}
1590 
1591 
1592 	zfs_close(zhp);
1593 
1594 	return (error);
1595 }
1596 
1597 /*
1598  * Generate a send stream for the dataset identified by the argument zhp.
1599  *
1600  * The content of the send stream is the snapshot identified by
1601  * 'tosnap'.  Incremental streams are requested in two ways:
1602  *     - from the snapshot identified by "fromsnap" (if non-null) or
1603  *     - from the origin of the dataset identified by zhp, which must
1604  *	 be a clone.  In this case, "fromsnap" is null and "fromorigin"
1605  *	 is TRUE.
1606  *
1607  * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1608  * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1609  * if "replicate" is set.  If "doall" is set, dump all the intermediate
1610  * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1611  * case too. If "props" is set, send properties.
1612  */
1613 int
1614 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1615     sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1616     void *cb_arg, nvlist_t **debugnvp)
1617 {
1618 	char errbuf[1024];
1619 	send_dump_data_t sdd = { 0 };
1620 	int err = 0;
1621 	nvlist_t *fss = NULL;
1622 	avl_tree_t *fsavl = NULL;
1623 	static uint64_t holdseq;
1624 	int spa_version;
1625 	pthread_t tid = 0;
1626 	int pipefd[2];
1627 	dedup_arg_t dda = { 0 };
1628 	int featureflags = 0;
1629 	FILE *fout;
1630 
1631 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1632 	    "cannot send '%s'"), zhp->zfs_name);
1633 
1634 	if (fromsnap && fromsnap[0] == '\0') {
1635 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1636 		    "zero-length incremental source"));
1637 		return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1638 	}
1639 
1640 	if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1641 		uint64_t version;
1642 		version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1643 		if (version >= ZPL_VERSION_SA) {
1644 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1645 		}
1646 	}
1647 
1648 	if (flags->dedup && !flags->dryrun) {
1649 		featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1650 		    DMU_BACKUP_FEATURE_DEDUPPROPS);
1651 		if (err = pipe(pipefd)) {
1652 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1653 			return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1654 			    errbuf));
1655 		}
1656 		dda.outputfd = outfd;
1657 		dda.inputfd = pipefd[1];
1658 		dda.dedup_hdl = zhp->zfs_hdl;
1659 		if (err = pthread_create(&tid, NULL, cksummer, &dda)) {
1660 			(void) close(pipefd[0]);
1661 			(void) close(pipefd[1]);
1662 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1663 			return (zfs_error(zhp->zfs_hdl,
1664 			    EZFS_THREADCREATEFAILED, errbuf));
1665 		}
1666 	}
1667 
1668 	if (flags->replicate || flags->doall || flags->props) {
1669 		dmu_replay_record_t drr = { 0 };
1670 		char *packbuf = NULL;
1671 		size_t buflen = 0;
1672 		zio_cksum_t zc = { 0 };
1673 
1674 		if (flags->replicate || flags->props) {
1675 			nvlist_t *hdrnv;
1676 
1677 			VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1678 			if (fromsnap) {
1679 				VERIFY(0 == nvlist_add_string(hdrnv,
1680 				    "fromsnap", fromsnap));
1681 			}
1682 			VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1683 			if (!flags->replicate) {
1684 				VERIFY(0 == nvlist_add_boolean(hdrnv,
1685 				    "not_recursive"));
1686 			}
1687 
1688 			err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1689 			    fromsnap, tosnap, flags->replicate, &fss, &fsavl);
1690 			if (err)
1691 				goto err_out;
1692 			VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1693 			err = nvlist_pack(hdrnv, &packbuf, &buflen,
1694 			    NV_ENCODE_XDR, 0);
1695 			if (debugnvp)
1696 				*debugnvp = hdrnv;
1697 			else
1698 				nvlist_free(hdrnv);
1699 			if (err)
1700 				goto stderr_out;
1701 		}
1702 
1703 		if (!flags->dryrun) {
1704 			/* write first begin record */
1705 			drr.drr_type = DRR_BEGIN;
1706 			drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1707 			DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1708 			    drr_versioninfo, DMU_COMPOUNDSTREAM);
1709 			DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1710 			    drr_versioninfo, featureflags);
1711 			(void) snprintf(drr.drr_u.drr_begin.drr_toname,
1712 			    sizeof (drr.drr_u.drr_begin.drr_toname),
1713 			    "%s@%s", zhp->zfs_name, tosnap);
1714 			drr.drr_payloadlen = buflen;
1715 
1716 			err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1717 			free(packbuf);
1718 			if (err != 0)
1719 				goto stderr_out;
1720 
1721 			/* write end record */
1722 			bzero(&drr, sizeof (drr));
1723 			drr.drr_type = DRR_END;
1724 			drr.drr_u.drr_end.drr_checksum = zc;
1725 			err = write(outfd, &drr, sizeof (drr));
1726 			if (err == -1) {
1727 				err = errno;
1728 				goto stderr_out;
1729 			}
1730 
1731 			err = 0;
1732 		}
1733 	}
1734 
1735 	/* dump each stream */
1736 	sdd.fromsnap = fromsnap;
1737 	sdd.tosnap = tosnap;
1738 	if (tid != 0)
1739 		sdd.outfd = pipefd[0];
1740 	else
1741 		sdd.outfd = outfd;
1742 	sdd.replicate = flags->replicate;
1743 	sdd.doall = flags->doall;
1744 	sdd.fromorigin = flags->fromorigin;
1745 	sdd.fss = fss;
1746 	sdd.fsavl = fsavl;
1747 	sdd.verbose = flags->verbose;
1748 	sdd.parsable = flags->parsable;
1749 	sdd.progress = flags->progress;
1750 	sdd.dryrun = flags->dryrun;
1751 	sdd.large_block = flags->largeblock;
1752 	sdd.embed_data = flags->embed_data;
1753 	sdd.filter_cb = filter_func;
1754 	sdd.filter_cb_arg = cb_arg;
1755 	if (debugnvp)
1756 		sdd.debugnv = *debugnvp;
1757 	if (sdd.verbose && sdd.dryrun)
1758 		sdd.std_out = B_TRUE;
1759 	fout = sdd.std_out ? stdout : stderr;
1760 
1761 	/*
1762 	 * Some flags require that we place user holds on the datasets that are
1763 	 * being sent so they don't get destroyed during the send. We can skip
1764 	 * this step if the pool is imported read-only since the datasets cannot
1765 	 * be destroyed.
1766 	 */
1767 	if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1768 	    ZPOOL_PROP_READONLY, NULL) &&
1769 	    zfs_spa_version(zhp, &spa_version) == 0 &&
1770 	    spa_version >= SPA_VERSION_USERREFS &&
1771 	    (flags->doall || flags->replicate)) {
1772 		++holdseq;
1773 		(void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1774 		    ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1775 		sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1776 		if (sdd.cleanup_fd < 0) {
1777 			err = errno;
1778 			goto stderr_out;
1779 		}
1780 		sdd.snapholds = fnvlist_alloc();
1781 	} else {
1782 		sdd.cleanup_fd = -1;
1783 		sdd.snapholds = NULL;
1784 	}
1785 	if (flags->verbose || sdd.snapholds != NULL) {
1786 		/*
1787 		 * Do a verbose no-op dry run to get all the verbose output
1788 		 * or to gather snapshot hold's before generating any data,
1789 		 * then do a non-verbose real run to generate the streams.
1790 		 */
1791 		sdd.dryrun = B_TRUE;
1792 		err = dump_filesystems(zhp, &sdd);
1793 
1794 		if (err != 0)
1795 			goto stderr_out;
1796 
1797 		if (flags->verbose) {
1798 			if (flags->parsable) {
1799 				(void) fprintf(fout, "size\t%llu\n",
1800 				    (longlong_t)sdd.size);
1801 			} else {
1802 				char buf[16];
1803 				zfs_nicenum(sdd.size, buf, sizeof (buf));
1804 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1805 				    "total estimated size is %s\n"), buf);
1806 			}
1807 		}
1808 
1809 		/* Ensure no snaps found is treated as an error. */
1810 		if (!sdd.seento) {
1811 			err = ENOENT;
1812 			goto err_out;
1813 		}
1814 
1815 		/* Skip the second run if dryrun was requested. */
1816 		if (flags->dryrun)
1817 			goto err_out;
1818 
1819 		if (sdd.snapholds != NULL) {
1820 			err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
1821 			if (err != 0)
1822 				goto stderr_out;
1823 
1824 			fnvlist_free(sdd.snapholds);
1825 			sdd.snapholds = NULL;
1826 		}
1827 
1828 		sdd.dryrun = B_FALSE;
1829 		sdd.verbose = B_FALSE;
1830 	}
1831 
1832 	err = dump_filesystems(zhp, &sdd);
1833 	fsavl_destroy(fsavl);
1834 	nvlist_free(fss);
1835 
1836 	/* Ensure no snaps found is treated as an error. */
1837 	if (err == 0 && !sdd.seento)
1838 		err = ENOENT;
1839 
1840 	if (tid != 0) {
1841 		if (err != 0)
1842 			(void) pthread_cancel(tid);
1843 		(void) close(pipefd[0]);
1844 		(void) pthread_join(tid, NULL);
1845 	}
1846 
1847 	if (sdd.cleanup_fd != -1) {
1848 		VERIFY(0 == close(sdd.cleanup_fd));
1849 		sdd.cleanup_fd = -1;
1850 	}
1851 
1852 	if (!flags->dryrun && (flags->replicate || flags->doall ||
1853 	    flags->props)) {
1854 		/*
1855 		 * write final end record.  NB: want to do this even if
1856 		 * there was some error, because it might not be totally
1857 		 * failed.
1858 		 */
1859 		dmu_replay_record_t drr = { 0 };
1860 		drr.drr_type = DRR_END;
1861 		if (write(outfd, &drr, sizeof (drr)) == -1) {
1862 			return (zfs_standard_error(zhp->zfs_hdl,
1863 			    errno, errbuf));
1864 		}
1865 	}
1866 
1867 	return (err || sdd.err);
1868 
1869 stderr_out:
1870 	err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1871 err_out:
1872 	fsavl_destroy(fsavl);
1873 	nvlist_free(fss);
1874 	fnvlist_free(sdd.snapholds);
1875 
1876 	if (sdd.cleanup_fd != -1)
1877 		VERIFY(0 == close(sdd.cleanup_fd));
1878 	if (tid != 0) {
1879 		(void) pthread_cancel(tid);
1880 		(void) close(pipefd[0]);
1881 		(void) pthread_join(tid, NULL);
1882 	}
1883 	return (err);
1884 }
1885 
1886 int
1887 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
1888     enum lzc_send_flags flags)
1889 {
1890 	int err;
1891 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1892 
1893 	char errbuf[1024];
1894 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1895 	    "warning: cannot send '%s'"), zhp->zfs_name);
1896 
1897 	err = lzc_send(zhp->zfs_name, from, fd, flags);
1898 	if (err != 0) {
1899 		switch (errno) {
1900 		case EXDEV:
1901 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1902 			    "not an earlier snapshot from the same fs"));
1903 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1904 
1905 		case ENOENT:
1906 		case ESRCH:
1907 			if (lzc_exists(zhp->zfs_name)) {
1908 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1909 				    "incremental source (%s) does not exist"),
1910 				    from);
1911 			}
1912 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
1913 
1914 		case EBUSY:
1915 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1916 			    "target is busy; if a filesystem, "
1917 			    "it must not be mounted"));
1918 			return (zfs_error(hdl, EZFS_BUSY, errbuf));
1919 
1920 		case EDQUOT:
1921 		case EFBIG:
1922 		case EIO:
1923 		case ENOLINK:
1924 		case ENOSPC:
1925 		case ENOSTR:
1926 		case ENXIO:
1927 		case EPIPE:
1928 		case ERANGE:
1929 		case EFAULT:
1930 		case EROFS:
1931 			zfs_error_aux(hdl, strerror(errno));
1932 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1933 
1934 		default:
1935 			return (zfs_standard_error(hdl, errno, errbuf));
1936 		}
1937 	}
1938 	return (err != 0);
1939 }
1940 
1941 /*
1942  * Routines specific to "zfs recv"
1943  */
1944 
1945 static int
1946 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
1947     boolean_t byteswap, zio_cksum_t *zc)
1948 {
1949 	char *cp = buf;
1950 	int rv;
1951 	int len = ilen;
1952 
1953 	assert(ilen <= SPA_MAXBLOCKSIZE);
1954 
1955 	do {
1956 		rv = read(fd, cp, len);
1957 		cp += rv;
1958 		len -= rv;
1959 	} while (rv > 0);
1960 
1961 	if (rv < 0 || len != 0) {
1962 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1963 		    "failed to read from stream"));
1964 		return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
1965 		    "cannot receive")));
1966 	}
1967 
1968 	if (zc) {
1969 		if (byteswap)
1970 			fletcher_4_incremental_byteswap(buf, ilen, zc);
1971 		else
1972 			fletcher_4_incremental_native(buf, ilen, zc);
1973 	}
1974 	return (0);
1975 }
1976 
1977 static int
1978 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
1979     boolean_t byteswap, zio_cksum_t *zc)
1980 {
1981 	char *buf;
1982 	int err;
1983 
1984 	buf = zfs_alloc(hdl, len);
1985 	if (buf == NULL)
1986 		return (ENOMEM);
1987 
1988 	err = recv_read(hdl, fd, buf, len, byteswap, zc);
1989 	if (err != 0) {
1990 		free(buf);
1991 		return (err);
1992 	}
1993 
1994 	err = nvlist_unpack(buf, len, nvp, 0);
1995 	free(buf);
1996 	if (err != 0) {
1997 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1998 		    "stream (malformed nvlist)"));
1999 		return (EINVAL);
2000 	}
2001 	return (0);
2002 }
2003 
2004 static int
2005 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2006     int baselen, char *newname, recvflags_t *flags)
2007 {
2008 	static int seq;
2009 	zfs_cmd_t zc = { 0 };
2010 	int err;
2011 	prop_changelist_t *clp;
2012 	zfs_handle_t *zhp;
2013 
2014 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2015 	if (zhp == NULL)
2016 		return (-1);
2017 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2018 	    flags->force ? MS_FORCE : 0);
2019 	zfs_close(zhp);
2020 	if (clp == NULL)
2021 		return (-1);
2022 	err = changelist_prefix(clp);
2023 	if (err)
2024 		return (err);
2025 
2026 	zc.zc_objset_type = DMU_OST_ZFS;
2027 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2028 
2029 	if (tryname) {
2030 		(void) strcpy(newname, tryname);
2031 
2032 		(void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
2033 
2034 		if (flags->verbose) {
2035 			(void) printf("attempting rename %s to %s\n",
2036 			    zc.zc_name, zc.zc_value);
2037 		}
2038 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2039 		if (err == 0)
2040 			changelist_rename(clp, name, tryname);
2041 	} else {
2042 		err = ENOENT;
2043 	}
2044 
2045 	if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2046 		seq++;
2047 
2048 		(void) snprintf(newname, ZFS_MAXNAMELEN, "%.*srecv-%u-%u",
2049 		    baselen, name, getpid(), seq);
2050 		(void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
2051 
2052 		if (flags->verbose) {
2053 			(void) printf("failed - trying rename %s to %s\n",
2054 			    zc.zc_name, zc.zc_value);
2055 		}
2056 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2057 		if (err == 0)
2058 			changelist_rename(clp, name, newname);
2059 		if (err && flags->verbose) {
2060 			(void) printf("failed (%u) - "
2061 			    "will try again on next pass\n", errno);
2062 		}
2063 		err = EAGAIN;
2064 	} else if (flags->verbose) {
2065 		if (err == 0)
2066 			(void) printf("success\n");
2067 		else
2068 			(void) printf("failed (%u)\n", errno);
2069 	}
2070 
2071 	(void) changelist_postfix(clp);
2072 	changelist_free(clp);
2073 
2074 	return (err);
2075 }
2076 
2077 static int
2078 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2079     char *newname, recvflags_t *flags)
2080 {
2081 	zfs_cmd_t zc = { 0 };
2082 	int err = 0;
2083 	prop_changelist_t *clp;
2084 	zfs_handle_t *zhp;
2085 	boolean_t defer = B_FALSE;
2086 	int spa_version;
2087 
2088 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2089 	if (zhp == NULL)
2090 		return (-1);
2091 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2092 	    flags->force ? MS_FORCE : 0);
2093 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2094 	    zfs_spa_version(zhp, &spa_version) == 0 &&
2095 	    spa_version >= SPA_VERSION_USERREFS)
2096 		defer = B_TRUE;
2097 	zfs_close(zhp);
2098 	if (clp == NULL)
2099 		return (-1);
2100 	err = changelist_prefix(clp);
2101 	if (err)
2102 		return (err);
2103 
2104 	zc.zc_objset_type = DMU_OST_ZFS;
2105 	zc.zc_defer_destroy = defer;
2106 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2107 
2108 	if (flags->verbose)
2109 		(void) printf("attempting destroy %s\n", zc.zc_name);
2110 	err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
2111 	if (err == 0) {
2112 		if (flags->verbose)
2113 			(void) printf("success\n");
2114 		changelist_remove(clp, zc.zc_name);
2115 	}
2116 
2117 	(void) changelist_postfix(clp);
2118 	changelist_free(clp);
2119 
2120 	/*
2121 	 * Deferred destroy might destroy the snapshot or only mark it to be
2122 	 * destroyed later, and it returns success in either case.
2123 	 */
2124 	if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2125 	    ZFS_TYPE_SNAPSHOT))) {
2126 		err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2127 	}
2128 
2129 	return (err);
2130 }
2131 
2132 typedef struct guid_to_name_data {
2133 	uint64_t guid;
2134 	boolean_t bookmark_ok;
2135 	char *name;
2136 	char *skip;
2137 } guid_to_name_data_t;
2138 
2139 static int
2140 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2141 {
2142 	guid_to_name_data_t *gtnd = arg;
2143 	const char *slash;
2144 	int err;
2145 
2146 	if (gtnd->skip != NULL &&
2147 	    (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2148 	    strcmp(slash + 1, gtnd->skip) == 0) {
2149 		zfs_close(zhp);
2150 		return (0);
2151 	}
2152 
2153 	if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2154 		(void) strcpy(gtnd->name, zhp->zfs_name);
2155 		zfs_close(zhp);
2156 		return (EEXIST);
2157 	}
2158 
2159 	err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2160 	if (err != EEXIST && gtnd->bookmark_ok)
2161 		err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2162 	zfs_close(zhp);
2163 	return (err);
2164 }
2165 
2166 /*
2167  * Attempt to find the local dataset associated with this guid.  In the case of
2168  * multiple matches, we attempt to find the "best" match by searching
2169  * progressively larger portions of the hierarchy.  This allows one to send a
2170  * tree of datasets individually and guarantee that we will find the source
2171  * guid within that hierarchy, even if there are multiple matches elsewhere.
2172  */
2173 static int
2174 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2175     boolean_t bookmark_ok, char *name)
2176 {
2177 	char pname[ZFS_MAXNAMELEN];
2178 	guid_to_name_data_t gtnd;
2179 
2180 	gtnd.guid = guid;
2181 	gtnd.bookmark_ok = bookmark_ok;
2182 	gtnd.name = name;
2183 	gtnd.skip = NULL;
2184 
2185 	/*
2186 	 * Search progressively larger portions of the hierarchy, starting
2187 	 * with the filesystem specified by 'parent'.  This will
2188 	 * select the "most local" version of the origin snapshot in the case
2189 	 * that there are multiple matching snapshots in the system.
2190 	 */
2191 	(void) strlcpy(pname, parent, sizeof (pname));
2192 	char *cp = strrchr(pname, '@');
2193 	if (cp == NULL)
2194 		cp = strchr(pname, '\0');
2195 	for (; cp != NULL; cp = strrchr(pname, '/')) {
2196 		/* Chop off the last component and open the parent */
2197 		*cp = '\0';
2198 		zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2199 
2200 		if (zhp == NULL)
2201 			continue;
2202 		int err = guid_to_name_cb(zfs_handle_dup(zhp), &gtnd);
2203 		if (err != EEXIST)
2204 			err = zfs_iter_children(zhp, guid_to_name_cb, &gtnd);
2205 		if (err != EEXIST && bookmark_ok)
2206 			err = zfs_iter_bookmarks(zhp, guid_to_name_cb, &gtnd);
2207 		zfs_close(zhp);
2208 		if (err == EEXIST)
2209 			return (0);
2210 
2211 		/*
2212 		 * Remember the last portion of the dataset so we skip it next
2213 		 * time through (as we've already searched that portion of the
2214 		 * hierarchy).
2215 		 */
2216 		gtnd.skip = strrchr(pname, '/') + 1;
2217 	}
2218 
2219 	return (ENOENT);
2220 }
2221 
2222 /*
2223  * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2224  * guid1 is after guid2.
2225  */
2226 static int
2227 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2228     uint64_t guid1, uint64_t guid2)
2229 {
2230 	nvlist_t *nvfs;
2231 	char *fsname, *snapname;
2232 	char buf[ZFS_MAXNAMELEN];
2233 	int rv;
2234 	zfs_handle_t *guid1hdl, *guid2hdl;
2235 	uint64_t create1, create2;
2236 
2237 	if (guid2 == 0)
2238 		return (0);
2239 	if (guid1 == 0)
2240 		return (1);
2241 
2242 	nvfs = fsavl_find(avl, guid1, &snapname);
2243 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2244 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2245 	guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2246 	if (guid1hdl == NULL)
2247 		return (-1);
2248 
2249 	nvfs = fsavl_find(avl, guid2, &snapname);
2250 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2251 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2252 	guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2253 	if (guid2hdl == NULL) {
2254 		zfs_close(guid1hdl);
2255 		return (-1);
2256 	}
2257 
2258 	create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2259 	create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2260 
2261 	if (create1 < create2)
2262 		rv = -1;
2263 	else if (create1 > create2)
2264 		rv = +1;
2265 	else
2266 		rv = 0;
2267 
2268 	zfs_close(guid1hdl);
2269 	zfs_close(guid2hdl);
2270 
2271 	return (rv);
2272 }
2273 
2274 static int
2275 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2276     recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2277     nvlist_t *renamed)
2278 {
2279 	nvlist_t *local_nv;
2280 	avl_tree_t *local_avl;
2281 	nvpair_t *fselem, *nextfselem;
2282 	char *fromsnap;
2283 	char newname[ZFS_MAXNAMELEN];
2284 	int error;
2285 	boolean_t needagain, progress, recursive;
2286 	char *s1, *s2;
2287 
2288 	VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2289 
2290 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2291 	    ENOENT);
2292 
2293 	if (flags->dryrun)
2294 		return (0);
2295 
2296 again:
2297 	needagain = progress = B_FALSE;
2298 
2299 	if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2300 	    recursive, &local_nv, &local_avl)) != 0)
2301 		return (error);
2302 
2303 	/*
2304 	 * Process deletes and renames
2305 	 */
2306 	for (fselem = nvlist_next_nvpair(local_nv, NULL);
2307 	    fselem; fselem = nextfselem) {
2308 		nvlist_t *nvfs, *snaps;
2309 		nvlist_t *stream_nvfs = NULL;
2310 		nvpair_t *snapelem, *nextsnapelem;
2311 		uint64_t fromguid = 0;
2312 		uint64_t originguid = 0;
2313 		uint64_t stream_originguid = 0;
2314 		uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2315 		char *fsname, *stream_fsname;
2316 
2317 		nextfselem = nvlist_next_nvpair(local_nv, fselem);
2318 
2319 		VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2320 		VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2321 		VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2322 		VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2323 		    &parent_fromsnap_guid));
2324 		(void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2325 
2326 		/*
2327 		 * First find the stream's fs, so we can check for
2328 		 * a different origin (due to "zfs promote")
2329 		 */
2330 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
2331 		    snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2332 			uint64_t thisguid;
2333 
2334 			VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2335 			stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2336 
2337 			if (stream_nvfs != NULL)
2338 				break;
2339 		}
2340 
2341 		/* check for promote */
2342 		(void) nvlist_lookup_uint64(stream_nvfs, "origin",
2343 		    &stream_originguid);
2344 		if (stream_nvfs && originguid != stream_originguid) {
2345 			switch (created_before(hdl, local_avl,
2346 			    stream_originguid, originguid)) {
2347 			case 1: {
2348 				/* promote it! */
2349 				zfs_cmd_t zc = { 0 };
2350 				nvlist_t *origin_nvfs;
2351 				char *origin_fsname;
2352 
2353 				if (flags->verbose)
2354 					(void) printf("promoting %s\n", fsname);
2355 
2356 				origin_nvfs = fsavl_find(local_avl, originguid,
2357 				    NULL);
2358 				VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2359 				    "name", &origin_fsname));
2360 				(void) strlcpy(zc.zc_value, origin_fsname,
2361 				    sizeof (zc.zc_value));
2362 				(void) strlcpy(zc.zc_name, fsname,
2363 				    sizeof (zc.zc_name));
2364 				error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2365 				if (error == 0)
2366 					progress = B_TRUE;
2367 				break;
2368 			}
2369 			default:
2370 				break;
2371 			case -1:
2372 				fsavl_destroy(local_avl);
2373 				nvlist_free(local_nv);
2374 				return (-1);
2375 			}
2376 			/*
2377 			 * We had/have the wrong origin, therefore our
2378 			 * list of snapshots is wrong.  Need to handle
2379 			 * them on the next pass.
2380 			 */
2381 			needagain = B_TRUE;
2382 			continue;
2383 		}
2384 
2385 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
2386 		    snapelem; snapelem = nextsnapelem) {
2387 			uint64_t thisguid;
2388 			char *stream_snapname;
2389 			nvlist_t *found, *props;
2390 
2391 			nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2392 
2393 			VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2394 			found = fsavl_find(stream_avl, thisguid,
2395 			    &stream_snapname);
2396 
2397 			/* check for delete */
2398 			if (found == NULL) {
2399 				char name[ZFS_MAXNAMELEN];
2400 
2401 				if (!flags->force)
2402 					continue;
2403 
2404 				(void) snprintf(name, sizeof (name), "%s@%s",
2405 				    fsname, nvpair_name(snapelem));
2406 
2407 				error = recv_destroy(hdl, name,
2408 				    strlen(fsname)+1, newname, flags);
2409 				if (error)
2410 					needagain = B_TRUE;
2411 				else
2412 					progress = B_TRUE;
2413 				continue;
2414 			}
2415 
2416 			stream_nvfs = found;
2417 
2418 			if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2419 			    &props) && 0 == nvlist_lookup_nvlist(props,
2420 			    stream_snapname, &props)) {
2421 				zfs_cmd_t zc = { 0 };
2422 
2423 				zc.zc_cookie = B_TRUE; /* received */
2424 				(void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2425 				    "%s@%s", fsname, nvpair_name(snapelem));
2426 				if (zcmd_write_src_nvlist(hdl, &zc,
2427 				    props) == 0) {
2428 					(void) zfs_ioctl(hdl,
2429 					    ZFS_IOC_SET_PROP, &zc);
2430 					zcmd_free_nvlists(&zc);
2431 				}
2432 			}
2433 
2434 			/* check for different snapname */
2435 			if (strcmp(nvpair_name(snapelem),
2436 			    stream_snapname) != 0) {
2437 				char name[ZFS_MAXNAMELEN];
2438 				char tryname[ZFS_MAXNAMELEN];
2439 
2440 				(void) snprintf(name, sizeof (name), "%s@%s",
2441 				    fsname, nvpair_name(snapelem));
2442 				(void) snprintf(tryname, sizeof (name), "%s@%s",
2443 				    fsname, stream_snapname);
2444 
2445 				error = recv_rename(hdl, name, tryname,
2446 				    strlen(fsname)+1, newname, flags);
2447 				if (error)
2448 					needagain = B_TRUE;
2449 				else
2450 					progress = B_TRUE;
2451 			}
2452 
2453 			if (strcmp(stream_snapname, fromsnap) == 0)
2454 				fromguid = thisguid;
2455 		}
2456 
2457 		/* check for delete */
2458 		if (stream_nvfs == NULL) {
2459 			if (!flags->force)
2460 				continue;
2461 
2462 			error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2463 			    newname, flags);
2464 			if (error)
2465 				needagain = B_TRUE;
2466 			else
2467 				progress = B_TRUE;
2468 			continue;
2469 		}
2470 
2471 		if (fromguid == 0) {
2472 			if (flags->verbose) {
2473 				(void) printf("local fs %s does not have "
2474 				    "fromsnap (%s in stream); must have "
2475 				    "been deleted locally; ignoring\n",
2476 				    fsname, fromsnap);
2477 			}
2478 			continue;
2479 		}
2480 
2481 		VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2482 		    "name", &stream_fsname));
2483 		VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2484 		    "parentfromsnap", &stream_parent_fromsnap_guid));
2485 
2486 		s1 = strrchr(fsname, '/');
2487 		s2 = strrchr(stream_fsname, '/');
2488 
2489 		/*
2490 		 * Check for rename. If the exact receive path is specified, it
2491 		 * does not count as a rename, but we still need to check the
2492 		 * datasets beneath it.
2493 		 */
2494 		if ((stream_parent_fromsnap_guid != 0 &&
2495 		    parent_fromsnap_guid != 0 &&
2496 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2497 		    ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2498 		    (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2499 			nvlist_t *parent;
2500 			char tryname[ZFS_MAXNAMELEN];
2501 
2502 			parent = fsavl_find(local_avl,
2503 			    stream_parent_fromsnap_guid, NULL);
2504 			/*
2505 			 * NB: parent might not be found if we used the
2506 			 * tosnap for stream_parent_fromsnap_guid,
2507 			 * because the parent is a newly-created fs;
2508 			 * we'll be able to rename it after we recv the
2509 			 * new fs.
2510 			 */
2511 			if (parent != NULL) {
2512 				char *pname;
2513 
2514 				VERIFY(0 == nvlist_lookup_string(parent, "name",
2515 				    &pname));
2516 				(void) snprintf(tryname, sizeof (tryname),
2517 				    "%s%s", pname, strrchr(stream_fsname, '/'));
2518 			} else {
2519 				tryname[0] = '\0';
2520 				if (flags->verbose) {
2521 					(void) printf("local fs %s new parent "
2522 					    "not found\n", fsname);
2523 				}
2524 			}
2525 
2526 			newname[0] = '\0';
2527 
2528 			error = recv_rename(hdl, fsname, tryname,
2529 			    strlen(tofs)+1, newname, flags);
2530 
2531 			if (renamed != NULL && newname[0] != '\0') {
2532 				VERIFY(0 == nvlist_add_boolean(renamed,
2533 				    newname));
2534 			}
2535 
2536 			if (error)
2537 				needagain = B_TRUE;
2538 			else
2539 				progress = B_TRUE;
2540 		}
2541 	}
2542 
2543 	fsavl_destroy(local_avl);
2544 	nvlist_free(local_nv);
2545 
2546 	if (needagain && progress) {
2547 		/* do another pass to fix up temporary names */
2548 		if (flags->verbose)
2549 			(void) printf("another pass:\n");
2550 		goto again;
2551 	}
2552 
2553 	return (needagain);
2554 }
2555 
2556 static int
2557 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2558     recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2559     char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2560 {
2561 	nvlist_t *stream_nv = NULL;
2562 	avl_tree_t *stream_avl = NULL;
2563 	char *fromsnap = NULL;
2564 	char *cp;
2565 	char tofs[ZFS_MAXNAMELEN];
2566 	char sendfs[ZFS_MAXNAMELEN];
2567 	char errbuf[1024];
2568 	dmu_replay_record_t drre;
2569 	int error;
2570 	boolean_t anyerr = B_FALSE;
2571 	boolean_t softerr = B_FALSE;
2572 	boolean_t recursive;
2573 
2574 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2575 	    "cannot receive"));
2576 
2577 	assert(drr->drr_type == DRR_BEGIN);
2578 	assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2579 	assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
2580 	    DMU_COMPOUNDSTREAM);
2581 
2582 	/*
2583 	 * Read in the nvlist from the stream.
2584 	 */
2585 	if (drr->drr_payloadlen != 0) {
2586 		error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
2587 		    &stream_nv, flags->byteswap, zc);
2588 		if (error) {
2589 			error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2590 			goto out;
2591 		}
2592 	}
2593 
2594 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2595 	    ENOENT);
2596 
2597 	if (recursive && strchr(destname, '@')) {
2598 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2599 		    "cannot specify snapshot name for multi-snapshot stream"));
2600 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2601 		goto out;
2602 	}
2603 
2604 	/*
2605 	 * Read in the end record and verify checksum.
2606 	 */
2607 	if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
2608 	    flags->byteswap, NULL)))
2609 		goto out;
2610 	if (flags->byteswap) {
2611 		drre.drr_type = BSWAP_32(drre.drr_type);
2612 		drre.drr_u.drr_end.drr_checksum.zc_word[0] =
2613 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
2614 		drre.drr_u.drr_end.drr_checksum.zc_word[1] =
2615 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
2616 		drre.drr_u.drr_end.drr_checksum.zc_word[2] =
2617 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
2618 		drre.drr_u.drr_end.drr_checksum.zc_word[3] =
2619 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2620 	}
2621 	if (drre.drr_type != DRR_END) {
2622 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2623 		goto out;
2624 	}
2625 	if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2626 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2627 		    "incorrect header checksum"));
2628 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2629 		goto out;
2630 	}
2631 
2632 	(void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2633 
2634 	if (drr->drr_payloadlen != 0) {
2635 		nvlist_t *stream_fss;
2636 
2637 		VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2638 		    &stream_fss));
2639 		if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2640 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2641 			    "couldn't allocate avl tree"));
2642 			error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2643 			goto out;
2644 		}
2645 
2646 		if (fromsnap != NULL) {
2647 			nvlist_t *renamed = NULL;
2648 			nvpair_t *pair = NULL;
2649 
2650 			(void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
2651 			if (flags->isprefix) {
2652 				struct drr_begin *drrb = &drr->drr_u.drr_begin;
2653 				int i;
2654 
2655 				if (flags->istail) {
2656 					cp = strrchr(drrb->drr_toname, '/');
2657 					if (cp == NULL) {
2658 						(void) strlcat(tofs, "/",
2659 						    ZFS_MAXNAMELEN);
2660 						i = 0;
2661 					} else {
2662 						i = (cp - drrb->drr_toname);
2663 					}
2664 				} else {
2665 					i = strcspn(drrb->drr_toname, "/@");
2666 				}
2667 				/* zfs_receive_one() will create_parents() */
2668 				(void) strlcat(tofs, &drrb->drr_toname[i],
2669 				    ZFS_MAXNAMELEN);
2670 				*strchr(tofs, '@') = '\0';
2671 			}
2672 
2673 			if (recursive && !flags->dryrun && !flags->nomount) {
2674 				VERIFY(0 == nvlist_alloc(&renamed,
2675 				    NV_UNIQUE_NAME, 0));
2676 			}
2677 
2678 			softerr = recv_incremental_replication(hdl, tofs, flags,
2679 			    stream_nv, stream_avl, renamed);
2680 
2681 			/* Unmount renamed filesystems before receiving. */
2682 			while ((pair = nvlist_next_nvpair(renamed,
2683 			    pair)) != NULL) {
2684 				zfs_handle_t *zhp;
2685 				prop_changelist_t *clp = NULL;
2686 
2687 				zhp = zfs_open(hdl, nvpair_name(pair),
2688 				    ZFS_TYPE_FILESYSTEM);
2689 				if (zhp != NULL) {
2690 					clp = changelist_gather(zhp,
2691 					    ZFS_PROP_MOUNTPOINT, 0, 0);
2692 					zfs_close(zhp);
2693 					if (clp != NULL) {
2694 						softerr |=
2695 						    changelist_prefix(clp);
2696 						changelist_free(clp);
2697 					}
2698 				}
2699 			}
2700 
2701 			nvlist_free(renamed);
2702 		}
2703 	}
2704 
2705 	/*
2706 	 * Get the fs specified by the first path in the stream (the top level
2707 	 * specified by 'zfs send') and pass it to each invocation of
2708 	 * zfs_receive_one().
2709 	 */
2710 	(void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
2711 	    ZFS_MAXNAMELEN);
2712 	if ((cp = strchr(sendfs, '@')) != NULL)
2713 		*cp = '\0';
2714 
2715 	/* Finally, receive each contained stream */
2716 	do {
2717 		/*
2718 		 * we should figure out if it has a recoverable
2719 		 * error, in which case do a recv_skip() and drive on.
2720 		 * Note, if we fail due to already having this guid,
2721 		 * zfs_receive_one() will take care of it (ie,
2722 		 * recv_skip() and return 0).
2723 		 */
2724 		error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2725 		    sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2726 		    action_handlep);
2727 		if (error == ENODATA) {
2728 			error = 0;
2729 			break;
2730 		}
2731 		anyerr |= error;
2732 	} while (error == 0);
2733 
2734 	if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
2735 		/*
2736 		 * Now that we have the fs's they sent us, try the
2737 		 * renames again.
2738 		 */
2739 		softerr = recv_incremental_replication(hdl, tofs, flags,
2740 		    stream_nv, stream_avl, NULL);
2741 	}
2742 
2743 out:
2744 	fsavl_destroy(stream_avl);
2745 	if (stream_nv)
2746 		nvlist_free(stream_nv);
2747 	if (softerr)
2748 		error = -2;
2749 	if (anyerr)
2750 		error = -1;
2751 	return (error);
2752 }
2753 
2754 static void
2755 trunc_prop_errs(int truncated)
2756 {
2757 	ASSERT(truncated != 0);
2758 
2759 	if (truncated == 1)
2760 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2761 		    "1 more property could not be set\n"));
2762 	else
2763 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2764 		    "%d more properties could not be set\n"), truncated);
2765 }
2766 
2767 static int
2768 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
2769 {
2770 	dmu_replay_record_t *drr;
2771 	void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
2772 	char errbuf[1024];
2773 
2774 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2775 	    "cannot receive:"));
2776 
2777 	/* XXX would be great to use lseek if possible... */
2778 	drr = buf;
2779 
2780 	while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
2781 	    byteswap, NULL) == 0) {
2782 		if (byteswap)
2783 			drr->drr_type = BSWAP_32(drr->drr_type);
2784 
2785 		switch (drr->drr_type) {
2786 		case DRR_BEGIN:
2787 			if (drr->drr_payloadlen != 0) {
2788 				(void) recv_read(hdl, fd, buf,
2789 				    drr->drr_payloadlen, B_FALSE, NULL);
2790 			}
2791 			break;
2792 
2793 		case DRR_END:
2794 			free(buf);
2795 			return (0);
2796 
2797 		case DRR_OBJECT:
2798 			if (byteswap) {
2799 				drr->drr_u.drr_object.drr_bonuslen =
2800 				    BSWAP_32(drr->drr_u.drr_object.
2801 				    drr_bonuslen);
2802 			}
2803 			(void) recv_read(hdl, fd, buf,
2804 			    P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
2805 			    B_FALSE, NULL);
2806 			break;
2807 
2808 		case DRR_WRITE:
2809 			if (byteswap) {
2810 				drr->drr_u.drr_write.drr_length =
2811 				    BSWAP_64(drr->drr_u.drr_write.drr_length);
2812 			}
2813 			(void) recv_read(hdl, fd, buf,
2814 			    drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
2815 			break;
2816 		case DRR_SPILL:
2817 			if (byteswap) {
2818 				drr->drr_u.drr_write.drr_length =
2819 				    BSWAP_64(drr->drr_u.drr_spill.drr_length);
2820 			}
2821 			(void) recv_read(hdl, fd, buf,
2822 			    drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
2823 			break;
2824 		case DRR_WRITE_EMBEDDED:
2825 			if (byteswap) {
2826 				drr->drr_u.drr_write_embedded.drr_psize =
2827 				    BSWAP_32(drr->drr_u.drr_write_embedded.
2828 				    drr_psize);
2829 			}
2830 			(void) recv_read(hdl, fd, buf,
2831 			    P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
2832 			    8), B_FALSE, NULL);
2833 			break;
2834 		case DRR_WRITE_BYREF:
2835 		case DRR_FREEOBJECTS:
2836 		case DRR_FREE:
2837 			break;
2838 
2839 		default:
2840 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2841 			    "invalid record type"));
2842 			return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2843 		}
2844 	}
2845 
2846 	free(buf);
2847 	return (-1);
2848 }
2849 
2850 static void
2851 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
2852     boolean_t resumable)
2853 {
2854 	char target_fs[ZFS_MAXNAMELEN];
2855 
2856 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2857 	    "checksum mismatch or incomplete stream"));
2858 
2859 	if (!resumable)
2860 		return;
2861 	(void) strlcpy(target_fs, target_snap, sizeof (target_fs));
2862 	*strchr(target_fs, '@') = '\0';
2863 	zfs_handle_t *zhp = zfs_open(hdl, target_fs,
2864 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2865 	if (zhp == NULL)
2866 		return;
2867 
2868 	char token_buf[ZFS_MAXPROPLEN];
2869 	int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
2870 	    token_buf, sizeof (token_buf),
2871 	    NULL, NULL, 0, B_TRUE);
2872 	if (error == 0) {
2873 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2874 		    "checksum mismatch or incomplete stream.\n"
2875 		    "Partially received snapshot is saved.\n"
2876 		    "A resuming stream can be generated on the sending "
2877 		    "system by running:\n"
2878 		    "    zfs send -t %s"),
2879 		    token_buf);
2880 	}
2881 	zfs_close(zhp);
2882 }
2883 
2884 /*
2885  * Restores a backup of tosnap from the file descriptor specified by infd.
2886  */
2887 static int
2888 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
2889     const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
2890     dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
2891     avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
2892     uint64_t *action_handlep)
2893 {
2894 	zfs_cmd_t zc = { 0 };
2895 	time_t begin_time;
2896 	int ioctl_err, ioctl_errno, err;
2897 	char *cp;
2898 	struct drr_begin *drrb = &drr->drr_u.drr_begin;
2899 	char errbuf[1024];
2900 	char prop_errbuf[1024];
2901 	const char *chopprefix;
2902 	boolean_t newfs = B_FALSE;
2903 	boolean_t stream_wantsnewfs;
2904 	uint64_t parent_snapguid = 0;
2905 	prop_changelist_t *clp = NULL;
2906 	nvlist_t *snapprops_nvlist = NULL;
2907 	zprop_errflags_t prop_errflags;
2908 	boolean_t recursive;
2909 
2910 	begin_time = time(NULL);
2911 
2912 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2913 	    "cannot receive"));
2914 
2915 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2916 	    ENOENT);
2917 
2918 	if (stream_avl != NULL) {
2919 		char *snapname;
2920 		nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
2921 		    &snapname);
2922 		nvlist_t *props;
2923 		int ret;
2924 
2925 		(void) nvlist_lookup_uint64(fs, "parentfromsnap",
2926 		    &parent_snapguid);
2927 		err = nvlist_lookup_nvlist(fs, "props", &props);
2928 		if (err)
2929 			VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
2930 
2931 		if (flags->canmountoff) {
2932 			VERIFY(0 == nvlist_add_uint64(props,
2933 			    zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
2934 		}
2935 		ret = zcmd_write_src_nvlist(hdl, &zc, props);
2936 		if (err)
2937 			nvlist_free(props);
2938 
2939 		if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
2940 			VERIFY(0 == nvlist_lookup_nvlist(props,
2941 			    snapname, &snapprops_nvlist));
2942 		}
2943 
2944 		if (ret != 0)
2945 			return (-1);
2946 	}
2947 
2948 	cp = NULL;
2949 
2950 	/*
2951 	 * Determine how much of the snapshot name stored in the stream
2952 	 * we are going to tack on to the name they specified on the
2953 	 * command line, and how much we are going to chop off.
2954 	 *
2955 	 * If they specified a snapshot, chop the entire name stored in
2956 	 * the stream.
2957 	 */
2958 	if (flags->istail) {
2959 		/*
2960 		 * A filesystem was specified with -e. We want to tack on only
2961 		 * the tail of the sent snapshot path.
2962 		 */
2963 		if (strchr(tosnap, '@')) {
2964 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2965 			    "argument - snapshot not allowed with -e"));
2966 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2967 		}
2968 
2969 		chopprefix = strrchr(sendfs, '/');
2970 
2971 		if (chopprefix == NULL) {
2972 			/*
2973 			 * The tail is the poolname, so we need to
2974 			 * prepend a path separator.
2975 			 */
2976 			int len = strlen(drrb->drr_toname);
2977 			cp = malloc(len + 2);
2978 			cp[0] = '/';
2979 			(void) strcpy(&cp[1], drrb->drr_toname);
2980 			chopprefix = cp;
2981 		} else {
2982 			chopprefix = drrb->drr_toname + (chopprefix - sendfs);
2983 		}
2984 	} else if (flags->isprefix) {
2985 		/*
2986 		 * A filesystem was specified with -d. We want to tack on
2987 		 * everything but the first element of the sent snapshot path
2988 		 * (all but the pool name).
2989 		 */
2990 		if (strchr(tosnap, '@')) {
2991 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2992 			    "argument - snapshot not allowed with -d"));
2993 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
2994 		}
2995 
2996 		chopprefix = strchr(drrb->drr_toname, '/');
2997 		if (chopprefix == NULL)
2998 			chopprefix = strchr(drrb->drr_toname, '@');
2999 	} else if (strchr(tosnap, '@') == NULL) {
3000 		/*
3001 		 * If a filesystem was specified without -d or -e, we want to
3002 		 * tack on everything after the fs specified by 'zfs send'.
3003 		 */
3004 		chopprefix = drrb->drr_toname + strlen(sendfs);
3005 	} else {
3006 		/* A snapshot was specified as an exact path (no -d or -e). */
3007 		if (recursive) {
3008 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3009 			    "cannot specify snapshot name for multi-snapshot "
3010 			    "stream"));
3011 			return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3012 		}
3013 		chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3014 	}
3015 
3016 	ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3017 	ASSERT(chopprefix > drrb->drr_toname);
3018 	ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
3019 	ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3020 	    chopprefix[0] == '\0');
3021 
3022 	/*
3023 	 * Determine name of destination snapshot, store in zc_value.
3024 	 */
3025 	(void) strcpy(zc.zc_value, tosnap);
3026 	(void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
3027 	free(cp);
3028 	if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
3029 		zcmd_free_nvlists(&zc);
3030 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
3031 	}
3032 
3033 	/*
3034 	 * Determine the name of the origin snapshot, store in zc_string.
3035 	 */
3036 	if (drrb->drr_flags & DRR_FLAG_CLONE) {
3037 		if (guid_to_name(hdl, zc.zc_value,
3038 		    drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3039 			zcmd_free_nvlists(&zc);
3040 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3041 			    "local origin for clone %s does not exist"),
3042 			    zc.zc_value);
3043 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3044 		}
3045 		if (flags->verbose)
3046 			(void) printf("found clone origin %s\n", zc.zc_string);
3047 	} else if (originsnap) {
3048 		(void) strncpy(zc.zc_string, originsnap, ZFS_MAXNAMELEN);
3049 		if (flags->verbose)
3050 			(void) printf("using provided clone origin %s\n",
3051 			    zc.zc_string);
3052 	}
3053 
3054 	boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3055 	    DMU_BACKUP_FEATURE_RESUMING;
3056 	stream_wantsnewfs = (drrb->drr_fromguid == NULL ||
3057 	    (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3058 
3059 	if (stream_wantsnewfs) {
3060 		/*
3061 		 * if the parent fs does not exist, look for it based on
3062 		 * the parent snap GUID
3063 		 */
3064 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3065 		    "cannot receive new filesystem stream"));
3066 
3067 		(void) strcpy(zc.zc_name, zc.zc_value);
3068 		cp = strrchr(zc.zc_name, '/');
3069 		if (cp)
3070 			*cp = '\0';
3071 		if (cp &&
3072 		    !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3073 			char suffix[ZFS_MAXNAMELEN];
3074 			(void) strcpy(suffix, strrchr(zc.zc_value, '/'));
3075 			if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
3076 			    B_FALSE, zc.zc_value) == 0) {
3077 				*strchr(zc.zc_value, '@') = '\0';
3078 				(void) strcat(zc.zc_value, suffix);
3079 			}
3080 		}
3081 	} else {
3082 		/*
3083 		 * if the fs does not exist, look for it based on the
3084 		 * fromsnap GUID
3085 		 */
3086 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3087 		    "cannot receive incremental stream"));
3088 
3089 		(void) strcpy(zc.zc_name, zc.zc_value);
3090 		*strchr(zc.zc_name, '@') = '\0';
3091 
3092 		/*
3093 		 * If the exact receive path was specified and this is the
3094 		 * topmost path in the stream, then if the fs does not exist we
3095 		 * should look no further.
3096 		 */
3097 		if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
3098 		    strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
3099 		    !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3100 			char snap[ZFS_MAXNAMELEN];
3101 			(void) strcpy(snap, strchr(zc.zc_value, '@'));
3102 			if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
3103 			    B_FALSE, zc.zc_value) == 0) {
3104 				*strchr(zc.zc_value, '@') = '\0';
3105 				(void) strcat(zc.zc_value, snap);
3106 			}
3107 		}
3108 	}
3109 
3110 	(void) strcpy(zc.zc_name, zc.zc_value);
3111 	*strchr(zc.zc_name, '@') = '\0';
3112 
3113 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3114 		zfs_handle_t *zhp;
3115 
3116 		/*
3117 		 * Destination fs exists.  It must be one of these cases:
3118 		 *  - an incremental send stream
3119 		 *  - the stream specifies a new fs (full stream or clone)
3120 		 *    and they want us to blow away the existing fs (and
3121 		 *    have therefore specified -F and removed any snapshots)
3122 		 *  - we are resuming a failed receive.
3123 		 */
3124 		if (stream_wantsnewfs) {
3125 			if (!flags->force) {
3126 				zcmd_free_nvlists(&zc);
3127 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3128 				    "destination '%s' exists\n"
3129 				    "must specify -F to overwrite it"),
3130 				    zc.zc_name);
3131 				return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3132 			}
3133 			if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3134 			    &zc) == 0) {
3135 				zcmd_free_nvlists(&zc);
3136 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3137 				    "destination has snapshots (eg. %s)\n"
3138 				    "must destroy them to overwrite it"),
3139 				    zc.zc_name);
3140 				return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3141 			}
3142 		}
3143 
3144 		if ((zhp = zfs_open(hdl, zc.zc_name,
3145 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
3146 			zcmd_free_nvlists(&zc);
3147 			return (-1);
3148 		}
3149 
3150 		if (stream_wantsnewfs &&
3151 		    zhp->zfs_dmustats.dds_origin[0]) {
3152 			zcmd_free_nvlists(&zc);
3153 			zfs_close(zhp);
3154 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3155 			    "destination '%s' is a clone\n"
3156 			    "must destroy it to overwrite it"),
3157 			    zc.zc_name);
3158 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3159 		}
3160 
3161 		if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3162 		    stream_wantsnewfs) {
3163 			/* We can't do online recv in this case */
3164 			clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3165 			if (clp == NULL) {
3166 				zfs_close(zhp);
3167 				zcmd_free_nvlists(&zc);
3168 				return (-1);
3169 			}
3170 			if (changelist_prefix(clp) != 0) {
3171 				changelist_free(clp);
3172 				zfs_close(zhp);
3173 				zcmd_free_nvlists(&zc);
3174 				return (-1);
3175 			}
3176 		}
3177 
3178 		/*
3179 		 * If we are resuming a newfs, set newfs here so that we will
3180 		 * mount it if the recv succeeds this time.  We can tell
3181 		 * that it was a newfs on the first recv because the fs
3182 		 * itself will be inconsistent (if the fs existed when we
3183 		 * did the first recv, we would have received it into
3184 		 * .../%recv).
3185 		 */
3186 		if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
3187 			newfs = B_TRUE;
3188 
3189 		zfs_close(zhp);
3190 	} else {
3191 		/*
3192 		 * Destination filesystem does not exist.  Therefore we better
3193 		 * be creating a new filesystem (either from a full backup, or
3194 		 * a clone).  It would therefore be invalid if the user
3195 		 * specified only the pool name (i.e. if the destination name
3196 		 * contained no slash character).
3197 		 */
3198 		if (!stream_wantsnewfs ||
3199 		    (cp = strrchr(zc.zc_name, '/')) == NULL) {
3200 			zcmd_free_nvlists(&zc);
3201 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3202 			    "destination '%s' does not exist"), zc.zc_name);
3203 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
3204 		}
3205 
3206 		/*
3207 		 * Trim off the final dataset component so we perform the
3208 		 * recvbackup ioctl to the filesystems's parent.
3209 		 */
3210 		*cp = '\0';
3211 
3212 		if (flags->isprefix && !flags->istail && !flags->dryrun &&
3213 		    create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
3214 			zcmd_free_nvlists(&zc);
3215 			return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3216 		}
3217 
3218 		newfs = B_TRUE;
3219 	}
3220 
3221 	zc.zc_begin_record = *drr_noswap;
3222 	zc.zc_cookie = infd;
3223 	zc.zc_guid = flags->force;
3224 	zc.zc_resumable = flags->resumable;
3225 	if (flags->verbose) {
3226 		(void) printf("%s %s stream of %s into %s\n",
3227 		    flags->dryrun ? "would receive" : "receiving",
3228 		    drrb->drr_fromguid ? "incremental" : "full",
3229 		    drrb->drr_toname, zc.zc_value);
3230 		(void) fflush(stdout);
3231 	}
3232 
3233 	if (flags->dryrun) {
3234 		zcmd_free_nvlists(&zc);
3235 		return (recv_skip(hdl, infd, flags->byteswap));
3236 	}
3237 
3238 	zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
3239 	zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
3240 	zc.zc_cleanup_fd = cleanup_fd;
3241 	zc.zc_action_handle = *action_handlep;
3242 
3243 	err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
3244 	ioctl_errno = errno;
3245 	prop_errflags = (zprop_errflags_t)zc.zc_obj;
3246 
3247 	if (err == 0) {
3248 		nvlist_t *prop_errors;
3249 		VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
3250 		    zc.zc_nvlist_dst_size, &prop_errors, 0));
3251 
3252 		nvpair_t *prop_err = NULL;
3253 
3254 		while ((prop_err = nvlist_next_nvpair(prop_errors,
3255 		    prop_err)) != NULL) {
3256 			char tbuf[1024];
3257 			zfs_prop_t prop;
3258 			int intval;
3259 
3260 			prop = zfs_name_to_prop(nvpair_name(prop_err));
3261 			(void) nvpair_value_int32(prop_err, &intval);
3262 			if (strcmp(nvpair_name(prop_err),
3263 			    ZPROP_N_MORE_ERRORS) == 0) {
3264 				trunc_prop_errs(intval);
3265 				break;
3266 			} else {
3267 				(void) snprintf(tbuf, sizeof (tbuf),
3268 				    dgettext(TEXT_DOMAIN,
3269 				    "cannot receive %s property on %s"),
3270 				    nvpair_name(prop_err), zc.zc_name);
3271 				zfs_setprop_error(hdl, prop, intval, tbuf);
3272 			}
3273 		}
3274 		nvlist_free(prop_errors);
3275 	}
3276 
3277 	zc.zc_nvlist_dst = 0;
3278 	zc.zc_nvlist_dst_size = 0;
3279 	zcmd_free_nvlists(&zc);
3280 
3281 	if (err == 0 && snapprops_nvlist) {
3282 		zfs_cmd_t zc2 = { 0 };
3283 
3284 		(void) strcpy(zc2.zc_name, zc.zc_value);
3285 		zc2.zc_cookie = B_TRUE; /* received */
3286 		if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
3287 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
3288 			zcmd_free_nvlists(&zc2);
3289 		}
3290 	}
3291 
3292 	if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3293 		/*
3294 		 * It may be that this snapshot already exists,
3295 		 * in which case we want to consume & ignore it
3296 		 * rather than failing.
3297 		 */
3298 		avl_tree_t *local_avl;
3299 		nvlist_t *local_nv, *fs;
3300 		cp = strchr(zc.zc_value, '@');
3301 
3302 		/*
3303 		 * XXX Do this faster by just iterating over snaps in
3304 		 * this fs.  Also if zc_value does not exist, we will
3305 		 * get a strange "does not exist" error message.
3306 		 */
3307 		*cp = '\0';
3308 		if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3309 		    &local_nv, &local_avl) == 0) {
3310 			*cp = '@';
3311 			fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
3312 			fsavl_destroy(local_avl);
3313 			nvlist_free(local_nv);
3314 
3315 			if (fs != NULL) {
3316 				if (flags->verbose) {
3317 					(void) printf("snap %s already exists; "
3318 					    "ignoring\n", zc.zc_value);
3319 				}
3320 				err = ioctl_err = recv_skip(hdl, infd,
3321 				    flags->byteswap);
3322 			}
3323 		}
3324 		*cp = '@';
3325 	}
3326 
3327 	if (ioctl_err != 0) {
3328 		switch (ioctl_errno) {
3329 		case ENODEV:
3330 			cp = strchr(zc.zc_value, '@');
3331 			*cp = '\0';
3332 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3333 			    "most recent snapshot of %s does not\n"
3334 			    "match incremental source"), zc.zc_value);
3335 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3336 			*cp = '@';
3337 			break;
3338 		case ETXTBSY:
3339 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3340 			    "destination %s has been modified\n"
3341 			    "since most recent snapshot"), zc.zc_name);
3342 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3343 			break;
3344 		case EEXIST:
3345 			cp = strchr(zc.zc_value, '@');
3346 			if (newfs) {
3347 				/* it's the containing fs that exists */
3348 				*cp = '\0';
3349 			}
3350 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3351 			    "destination already exists"));
3352 			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
3353 			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
3354 			    zc.zc_value);
3355 			*cp = '@';
3356 			break;
3357 		case EINVAL:
3358 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3359 			break;
3360 		case ECKSUM:
3361 			recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
3362 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3363 			break;
3364 		case ENOTSUP:
3365 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3366 			    "pool must be upgraded to receive this stream."));
3367 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3368 			break;
3369 		case EDQUOT:
3370 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3371 			    "destination %s space quota exceeded"), zc.zc_name);
3372 			(void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3373 			break;
3374 		default:
3375 			(void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3376 		}
3377 	}
3378 
3379 	/*
3380 	 * Mount the target filesystem (if created).  Also mount any
3381 	 * children of the target filesystem if we did a replication
3382 	 * receive (indicated by stream_avl being non-NULL).
3383 	 */
3384 	cp = strchr(zc.zc_value, '@');
3385 	if (cp && (ioctl_err == 0 || !newfs)) {
3386 		zfs_handle_t *h;
3387 
3388 		*cp = '\0';
3389 		h = zfs_open(hdl, zc.zc_value,
3390 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3391 		if (h != NULL) {
3392 			if (h->zfs_type == ZFS_TYPE_VOLUME) {
3393 				*cp = '@';
3394 			} else if (newfs || stream_avl) {
3395 				/*
3396 				 * Track the first/top of hierarchy fs,
3397 				 * for mounting and sharing later.
3398 				 */
3399 				if (top_zfs && *top_zfs == NULL)
3400 					*top_zfs = zfs_strdup(hdl, zc.zc_value);
3401 			}
3402 			zfs_close(h);
3403 		}
3404 		*cp = '@';
3405 	}
3406 
3407 	if (clp) {
3408 		err |= changelist_postfix(clp);
3409 		changelist_free(clp);
3410 	}
3411 
3412 	if (prop_errflags & ZPROP_ERR_NOCLEAR) {
3413 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3414 		    "failed to clear unreceived properties on %s"),
3415 		    zc.zc_name);
3416 		(void) fprintf(stderr, "\n");
3417 	}
3418 	if (prop_errflags & ZPROP_ERR_NORESTORE) {
3419 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
3420 		    "failed to restore original properties on %s"),
3421 		    zc.zc_name);
3422 		(void) fprintf(stderr, "\n");
3423 	}
3424 
3425 	if (err || ioctl_err)
3426 		return (-1);
3427 
3428 	*action_handlep = zc.zc_action_handle;
3429 
3430 	if (flags->verbose) {
3431 		char buf1[64];
3432 		char buf2[64];
3433 		uint64_t bytes = zc.zc_cookie;
3434 		time_t delta = time(NULL) - begin_time;
3435 		if (delta == 0)
3436 			delta = 1;
3437 		zfs_nicenum(bytes, buf1, sizeof (buf1));
3438 		zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3439 
3440 		(void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3441 		    buf1, delta, buf2);
3442 	}
3443 
3444 	return (0);
3445 }
3446 
3447 static int
3448 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3449     const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
3450     nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3451     uint64_t *action_handlep)
3452 {
3453 	int err;
3454 	dmu_replay_record_t drr, drr_noswap;
3455 	struct drr_begin *drrb = &drr.drr_u.drr_begin;
3456 	char errbuf[1024];
3457 	zio_cksum_t zcksum = { 0 };
3458 	uint64_t featureflags;
3459 	int hdrtype;
3460 
3461 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3462 	    "cannot receive"));
3463 
3464 	if (flags->isprefix &&
3465 	    !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3466 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3467 		    "(%s) does not exist"), tosnap);
3468 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3469 	}
3470 	if (originsnap &&
3471 	    !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
3472 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
3473 		    "(%s) does not exist"), originsnap);
3474 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
3475 	}
3476 
3477 	/* read in the BEGIN record */
3478 	if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3479 	    &zcksum)))
3480 		return (err);
3481 
3482 	if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3483 		/* It's the double end record at the end of a package */
3484 		return (ENODATA);
3485 	}
3486 
3487 	/* the kernel needs the non-byteswapped begin record */
3488 	drr_noswap = drr;
3489 
3490 	flags->byteswap = B_FALSE;
3491 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3492 		/*
3493 		 * We computed the checksum in the wrong byteorder in
3494 		 * recv_read() above; do it again correctly.
3495 		 */
3496 		bzero(&zcksum, sizeof (zio_cksum_t));
3497 		fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
3498 		flags->byteswap = B_TRUE;
3499 
3500 		drr.drr_type = BSWAP_32(drr.drr_type);
3501 		drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
3502 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
3503 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3504 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3505 		drrb->drr_type = BSWAP_32(drrb->drr_type);
3506 		drrb->drr_flags = BSWAP_32(drrb->drr_flags);
3507 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
3508 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
3509 	}
3510 
3511 	if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3512 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3513 		    "stream (bad magic number)"));
3514 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3515 	}
3516 
3517 	featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3518 	hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
3519 
3520 	if (!DMU_STREAM_SUPPORTED(featureflags) ||
3521 	    (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
3522 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3523 		    "stream has unsupported feature, feature flags = %lx"),
3524 		    featureflags);
3525 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3526 	}
3527 
3528 	if (strchr(drrb->drr_toname, '@') == NULL) {
3529 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3530 		    "stream (bad snapshot name)"));
3531 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3532 	}
3533 
3534 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3535 		char nonpackage_sendfs[ZFS_MAXNAMELEN];
3536 		if (sendfs == NULL) {
3537 			/*
3538 			 * We were not called from zfs_receive_package(). Get
3539 			 * the fs specified by 'zfs send'.
3540 			 */
3541 			char *cp;
3542 			(void) strlcpy(nonpackage_sendfs,
3543 			    drr.drr_u.drr_begin.drr_toname, ZFS_MAXNAMELEN);
3544 			if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3545 				*cp = '\0';
3546 			sendfs = nonpackage_sendfs;
3547 		}
3548 		return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3549 		    &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
3550 		    cleanup_fd, action_handlep));
3551 	} else {
3552 		assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3553 		    DMU_COMPOUNDSTREAM);
3554 		return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
3555 		    &zcksum, top_zfs, cleanup_fd, action_handlep));
3556 	}
3557 }
3558 
3559 /*
3560  * Restores a backup of tosnap from the file descriptor specified by infd.
3561  * Return 0 on total success, -2 if some things couldn't be
3562  * destroyed/renamed/promoted, -1 if some things couldn't be received.
3563  * (-1 will override -2, if -1 and the resumable flag was specified the
3564  * transfer can be resumed if the sending side supports it).
3565  */
3566 int
3567 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
3568     recvflags_t *flags, int infd, avl_tree_t *stream_avl)
3569 {
3570 	char *top_zfs = NULL;
3571 	int err;
3572 	int cleanup_fd;
3573 	uint64_t action_handle = 0;
3574 	char *originsnap = NULL;
3575 	if (props) {
3576 		err = nvlist_lookup_string(props, "origin", &originsnap);
3577 		if (err && err != ENOENT)
3578 			return (err);
3579 	}
3580 
3581 	cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3582 	VERIFY(cleanup_fd >= 0);
3583 
3584 	err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
3585 	    stream_avl, &top_zfs, cleanup_fd, &action_handle);
3586 
3587 	VERIFY(0 == close(cleanup_fd));
3588 
3589 	if (err == 0 && !flags->nomount && top_zfs) {
3590 		zfs_handle_t *zhp;
3591 		prop_changelist_t *clp;
3592 
3593 		zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3594 		if (zhp != NULL) {
3595 			clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3596 			    CL_GATHER_MOUNT_ALWAYS, 0);
3597 			zfs_close(zhp);
3598 			if (clp != NULL) {
3599 				/* mount and share received datasets */
3600 				err = changelist_postfix(clp);
3601 				changelist_free(clp);
3602 			}
3603 		}
3604 		if (zhp == NULL || clp == NULL || err)
3605 			err = -1;
3606 	}
3607 	if (top_zfs)
3608 		free(top_zfs);
3609 
3610 	return (err);
3611 }
3612