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