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