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