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