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