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