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