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