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