xref: /titanic_44/usr/src/lib/libzfs/common/libzfs_sendrecv.c (revision 825277341c15b6b0d2c4b8b622ae7b1d2bdc0390)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #include <assert.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <libintl.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <strings.h>
34 #include <unistd.h>
35 #include <stddef.h>
36 #include <fcntl.h>
37 #include <sys/mount.h>
38 
39 #include <libzfs.h>
40 
41 #include "zfs_namecheck.h"
42 #include "zfs_prop.h"
43 #include "zfs_fletcher.h"
44 #include "libzfs_impl.h"
45 
46 static int zfs_receive_impl(libzfs_handle_t *, const char *, recvflags_t,
47     int, avl_tree_t *, char **);
48 
49 /*
50  * Routines for dealing with the AVL tree of fs-nvlists
51  */
52 typedef struct fsavl_node {
53 	avl_node_t fn_node;
54 	nvlist_t *fn_nvfs;
55 	char *fn_snapname;
56 	uint64_t fn_guid;
57 } fsavl_node_t;
58 
59 static int
60 fsavl_compare(const void *arg1, const void *arg2)
61 {
62 	const fsavl_node_t *fn1 = arg1;
63 	const fsavl_node_t *fn2 = arg2;
64 
65 	if (fn1->fn_guid > fn2->fn_guid)
66 		return (+1);
67 	else if (fn1->fn_guid < fn2->fn_guid)
68 		return (-1);
69 	else
70 		return (0);
71 }
72 
73 /*
74  * Given the GUID of a snapshot, find its containing filesystem and
75  * (optionally) name.
76  */
77 static nvlist_t *
78 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
79 {
80 	fsavl_node_t fn_find;
81 	fsavl_node_t *fn;
82 
83 	fn_find.fn_guid = snapguid;
84 
85 	fn = avl_find(avl, &fn_find, NULL);
86 	if (fn) {
87 		if (snapname)
88 			*snapname = fn->fn_snapname;
89 		return (fn->fn_nvfs);
90 	}
91 	return (NULL);
92 }
93 
94 static void
95 fsavl_destroy(avl_tree_t *avl)
96 {
97 	fsavl_node_t *fn;
98 	void *cookie;
99 
100 	if (avl == NULL)
101 		return;
102 
103 	cookie = NULL;
104 	while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
105 		free(fn);
106 	avl_destroy(avl);
107 	free(avl);
108 }
109 
110 /*
111  * Given an nvlist, produce an avl tree of snapshots, ordered by guid
112  */
113 static avl_tree_t *
114 fsavl_create(nvlist_t *fss)
115 {
116 	avl_tree_t *fsavl;
117 	nvpair_t *fselem = NULL;
118 
119 	if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
120 		return (NULL);
121 
122 	avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
123 	    offsetof(fsavl_node_t, fn_node));
124 
125 	while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
126 		nvlist_t *nvfs, *snaps;
127 		nvpair_t *snapelem = NULL;
128 
129 		VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
130 		VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
131 
132 		while ((snapelem =
133 		    nvlist_next_nvpair(snaps, snapelem)) != NULL) {
134 			fsavl_node_t *fn;
135 			uint64_t guid;
136 
137 			VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
138 			if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
139 				fsavl_destroy(fsavl);
140 				return (NULL);
141 			}
142 			fn->fn_nvfs = nvfs;
143 			fn->fn_snapname = nvpair_name(snapelem);
144 			fn->fn_guid = guid;
145 
146 			/*
147 			 * Note: if there are multiple snaps with the
148 			 * same GUID, we ignore all but one.
149 			 */
150 			if (avl_find(fsavl, fn, NULL) == NULL)
151 				avl_add(fsavl, fn);
152 			else
153 				free(fn);
154 		}
155 	}
156 
157 	return (fsavl);
158 }
159 
160 /*
161  * Routines for dealing with the giant nvlist of fs-nvlists, etc.
162  */
163 typedef struct send_data {
164 	uint64_t parent_fromsnap_guid;
165 	nvlist_t *parent_snaps;
166 	nvlist_t *fss;
167 	nvlist_t *snapprops;
168 	const char *fromsnap;
169 	const char *tosnap;
170 
171 	/*
172 	 * The header nvlist is of the following format:
173 	 * {
174 	 *   "tosnap" -> string
175 	 *   "fromsnap" -> string (if incremental)
176 	 *   "fss" -> {
177 	 *	id -> {
178 	 *
179 	 *	 "name" -> string (full name; for debugging)
180 	 *	 "parentfromsnap" -> number (guid of fromsnap in parent)
181 	 *
182 	 *	 "props" -> { name -> value (only if set here) }
183 	 *	 "snaps" -> { name (lastname) -> number (guid) }
184 	 *	 "snapprops" -> { name (lastname) -> { name -> value } }
185 	 *
186 	 *	 "origin" -> number (guid) (if clone)
187 	 *	 "sent" -> boolean (not on-disk)
188 	 *	}
189 	 *   }
190 	 * }
191 	 *
192 	 */
193 } send_data_t;
194 
195 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
196 
197 static int
198 send_iterate_snap(zfs_handle_t *zhp, void *arg)
199 {
200 	send_data_t *sd = arg;
201 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
202 	char *snapname;
203 	nvlist_t *nv;
204 
205 	snapname = strrchr(zhp->zfs_name, '@')+1;
206 
207 	VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
208 	/*
209 	 * NB: if there is no fromsnap here (it's a newly created fs in
210 	 * an incremental replication), we will substitute the tosnap.
211 	 */
212 	if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
213 	    (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
214 	    strcmp(snapname, sd->tosnap) == 0)) {
215 		sd->parent_fromsnap_guid = guid;
216 	}
217 
218 	VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
219 	send_iterate_prop(zhp, nv);
220 	VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
221 	nvlist_free(nv);
222 
223 	zfs_close(zhp);
224 	return (0);
225 }
226 
227 static void
228 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
229 {
230 	nvpair_t *elem = NULL;
231 
232 	while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
233 		char *propname = nvpair_name(elem);
234 		zfs_prop_t prop = zfs_name_to_prop(propname);
235 		nvlist_t *propnv;
236 
237 		if (!zfs_prop_user(propname)) {
238 			/*
239 			 * Realistically, this should never happen.  However,
240 			 * we want the ability to add DSL properties without
241 			 * needing to make incompatible version changes.  We
242 			 * need to ignore unknown properties to allow older
243 			 * software to still send datasets containing these
244 			 * properties, with the unknown properties elided.
245 			 */
246 			if (prop == ZPROP_INVAL)
247 				continue;
248 
249 			if (zfs_prop_readonly(prop))
250 				continue;
251 		}
252 
253 		verify(nvpair_value_nvlist(elem, &propnv) == 0);
254 		if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
255 		    prop == ZFS_PROP_REFQUOTA ||
256 		    prop == ZFS_PROP_REFRESERVATION) {
257 			/* these guys are modifyable, but have no source */
258 			uint64_t value;
259 			verify(nvlist_lookup_uint64(propnv,
260 			    ZPROP_VALUE, &value) == 0);
261 			if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
262 				continue;
263 		} else {
264 			char *source;
265 			if (nvlist_lookup_string(propnv,
266 			    ZPROP_SOURCE, &source) != 0)
267 				continue;
268 			if (strcmp(source, zhp->zfs_name) != 0)
269 				continue;
270 		}
271 
272 		if (zfs_prop_user(propname) ||
273 		    zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
274 			char *value;
275 			verify(nvlist_lookup_string(propnv,
276 			    ZPROP_VALUE, &value) == 0);
277 			VERIFY(0 == nvlist_add_string(nv, propname, value));
278 		} else {
279 			uint64_t value;
280 			verify(nvlist_lookup_uint64(propnv,
281 			    ZPROP_VALUE, &value) == 0);
282 			VERIFY(0 == nvlist_add_uint64(nv, propname, value));
283 		}
284 	}
285 }
286 
287 /*
288  * recursively generate nvlists describing datasets.  See comment
289  * for the data structure send_data_t above for description of contents
290  * of the nvlist.
291  */
292 static int
293 send_iterate_fs(zfs_handle_t *zhp, void *arg)
294 {
295 	send_data_t *sd = arg;
296 	nvlist_t *nvfs, *nv;
297 	int rv;
298 	uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
299 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
300 	char guidstring[64];
301 
302 	VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
303 	VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
304 	VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
305 	    sd->parent_fromsnap_guid));
306 
307 	if (zhp->zfs_dmustats.dds_origin[0]) {
308 		zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
309 		    zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
310 		if (origin == NULL)
311 			return (-1);
312 		VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
313 		    origin->zfs_dmustats.dds_guid));
314 	}
315 
316 	/* iterate over props */
317 	VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
318 	send_iterate_prop(zhp, nv);
319 	VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
320 	nvlist_free(nv);
321 
322 	/* iterate over snaps, and set sd->parent_fromsnap_guid */
323 	sd->parent_fromsnap_guid = 0;
324 	VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
325 	VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
326 	(void) zfs_iter_snapshots(zhp, send_iterate_snap, sd);
327 	VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
328 	VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
329 	nvlist_free(sd->parent_snaps);
330 	nvlist_free(sd->snapprops);
331 
332 	/* add this fs to nvlist */
333 	(void) snprintf(guidstring, sizeof (guidstring),
334 	    "0x%llx", (longlong_t)guid);
335 	VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
336 	nvlist_free(nvfs);
337 
338 	/* iterate over children */
339 	rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
340 
341 	sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
342 
343 	zfs_close(zhp);
344 	return (rv);
345 }
346 
347 static int
348 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
349     const char *tosnap, nvlist_t **nvlp, avl_tree_t **avlp)
350 {
351 	zfs_handle_t *zhp;
352 	send_data_t sd = { 0 };
353 	int error;
354 
355 	zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
356 	if (zhp == NULL)
357 		return (EZFS_BADTYPE);
358 
359 	VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
360 	sd.fromsnap = fromsnap;
361 	sd.tosnap = tosnap;
362 
363 	if ((error = send_iterate_fs(zhp, &sd)) != 0) {
364 		nvlist_free(sd.fss);
365 		if (avlp != NULL)
366 			*avlp = NULL;
367 		*nvlp = NULL;
368 		return (error);
369 	}
370 
371 	if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
372 		nvlist_free(sd.fss);
373 		*nvlp = NULL;
374 		return (EZFS_NOMEM);
375 	}
376 
377 	*nvlp = sd.fss;
378 	return (0);
379 }
380 
381 /*
382  * Routines for dealing with the sorted snapshot functionality
383  */
384 typedef struct zfs_node {
385 	zfs_handle_t	*zn_handle;
386 	avl_node_t	zn_avlnode;
387 } zfs_node_t;
388 
389 static int
390 zfs_sort_snaps(zfs_handle_t *zhp, void *data)
391 {
392 	avl_tree_t *avl = data;
393 	zfs_node_t *node = zfs_alloc(zhp->zfs_hdl, sizeof (zfs_node_t));
394 
395 	node->zn_handle = zhp;
396 	avl_add(avl, node);
397 	return (0);
398 }
399 
400 /* ARGSUSED */
401 static int
402 zfs_snapshot_compare(const void *larg, const void *rarg)
403 {
404 	zfs_handle_t *l = ((zfs_node_t *)larg)->zn_handle;
405 	zfs_handle_t *r = ((zfs_node_t *)rarg)->zn_handle;
406 	uint64_t lcreate, rcreate;
407 
408 	/*
409 	 * Sort them according to creation time.  We use the hidden
410 	 * CREATETXG property to get an absolute ordering of snapshots.
411 	 */
412 	lcreate = zfs_prop_get_int(l, ZFS_PROP_CREATETXG);
413 	rcreate = zfs_prop_get_int(r, ZFS_PROP_CREATETXG);
414 
415 	if (lcreate < rcreate)
416 		return (-1);
417 	else if (lcreate > rcreate)
418 		return (+1);
419 	else
420 		return (0);
421 }
422 
423 int
424 zfs_iter_snapshots_sorted(zfs_handle_t *zhp, zfs_iter_f callback, void *data)
425 {
426 	int ret = 0;
427 	zfs_node_t *node;
428 	avl_tree_t avl;
429 	void *cookie = NULL;
430 
431 	avl_create(&avl, zfs_snapshot_compare,
432 	    sizeof (zfs_node_t), offsetof(zfs_node_t, zn_avlnode));
433 
434 	ret = zfs_iter_snapshots(zhp, zfs_sort_snaps, &avl);
435 
436 	for (node = avl_first(&avl); node != NULL; node = AVL_NEXT(&avl, node))
437 		ret |= callback(node->zn_handle, data);
438 
439 	while ((node = avl_destroy_nodes(&avl, &cookie)) != NULL)
440 		free(node);
441 
442 	avl_destroy(&avl);
443 
444 	return (ret);
445 }
446 
447 /*
448  * Routines specific to "zfs send"
449  */
450 typedef struct send_dump_data {
451 	/* these are all just the short snapname (the part after the @) */
452 	const char *fromsnap;
453 	const char *tosnap;
454 	char lastsnap[ZFS_MAXNAMELEN];
455 	boolean_t seenfrom, seento, replicate, doall, fromorigin;
456 	boolean_t verbose;
457 	int outfd;
458 	boolean_t err;
459 	nvlist_t *fss;
460 	avl_tree_t *fsavl;
461 } send_dump_data_t;
462 
463 /*
464  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
465  * NULL) to the file descriptor specified by outfd.
466  */
467 static int
468 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, boolean_t fromorigin,
469     int outfd)
470 {
471 	zfs_cmd_t zc = { 0 };
472 	libzfs_handle_t *hdl = zhp->zfs_hdl;
473 
474 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
475 	assert(fromsnap == NULL || fromsnap[0] == '\0' || !fromorigin);
476 
477 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
478 	if (fromsnap)
479 		(void) strlcpy(zc.zc_value, fromsnap, sizeof (zc.zc_value));
480 	zc.zc_cookie = outfd;
481 	zc.zc_obj = fromorigin;
482 
483 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SEND, &zc) != 0) {
484 		char errbuf[1024];
485 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
486 		    "warning: cannot send '%s'"), zhp->zfs_name);
487 
488 		switch (errno) {
489 
490 		case EXDEV:
491 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
492 			    "not an earlier snapshot from the same fs"));
493 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
494 
495 		case ENOENT:
496 			if (zfs_dataset_exists(hdl, zc.zc_name,
497 			    ZFS_TYPE_SNAPSHOT)) {
498 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
499 				    "incremental source (@%s) does not exist"),
500 				    zc.zc_value);
501 			}
502 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
503 
504 		case EDQUOT:
505 		case EFBIG:
506 		case EIO:
507 		case ENOLINK:
508 		case ENOSPC:
509 		case ENOSTR:
510 		case ENXIO:
511 		case EPIPE:
512 		case ERANGE:
513 		case EFAULT:
514 		case EROFS:
515 			zfs_error_aux(hdl, strerror(errno));
516 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
517 
518 		default:
519 			return (zfs_standard_error(hdl, errno, errbuf));
520 		}
521 	}
522 
523 	return (0);
524 }
525 
526 static int
527 dump_snapshot(zfs_handle_t *zhp, void *arg)
528 {
529 	send_dump_data_t *sdd = arg;
530 	const char *thissnap;
531 	int err;
532 
533 	thissnap = strchr(zhp->zfs_name, '@') + 1;
534 
535 	if (sdd->fromsnap && !sdd->seenfrom &&
536 	    strcmp(sdd->fromsnap, thissnap) == 0) {
537 		sdd->seenfrom = B_TRUE;
538 		(void) strcpy(sdd->lastsnap, thissnap);
539 		zfs_close(zhp);
540 		return (0);
541 	}
542 
543 	if (sdd->seento || !sdd->seenfrom) {
544 		zfs_close(zhp);
545 		return (0);
546 	}
547 
548 	/* send it */
549 	if (sdd->verbose) {
550 		(void) fprintf(stderr, "sending from @%s to %s\n",
551 		    sdd->lastsnap, zhp->zfs_name);
552 	}
553 
554 	err = dump_ioctl(zhp, sdd->lastsnap,
555 	    sdd->lastsnap[0] == '\0' && (sdd->fromorigin || sdd->replicate),
556 	    sdd->outfd);
557 
558 	if (!sdd->seento && strcmp(sdd->tosnap, thissnap) == 0)
559 		sdd->seento = B_TRUE;
560 
561 	(void) strcpy(sdd->lastsnap, thissnap);
562 	zfs_close(zhp);
563 	return (err);
564 }
565 
566 static int
567 dump_filesystem(zfs_handle_t *zhp, void *arg)
568 {
569 	int rv = 0;
570 	send_dump_data_t *sdd = arg;
571 	boolean_t missingfrom = B_FALSE;
572 	zfs_cmd_t zc = { 0 };
573 
574 	(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
575 	    zhp->zfs_name, sdd->tosnap);
576 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
577 		(void) fprintf(stderr, "WARNING: "
578 		    "could not send %s@%s: does not exist\n",
579 		    zhp->zfs_name, sdd->tosnap);
580 		sdd->err = B_TRUE;
581 		return (0);
582 	}
583 
584 	if (sdd->replicate && sdd->fromsnap) {
585 		/*
586 		 * If this fs does not have fromsnap, and we're doing
587 		 * recursive, we need to send a full stream from the
588 		 * beginning (or an incremental from the origin if this
589 		 * is a clone).  If we're doing non-recursive, then let
590 		 * them get the error.
591 		 */
592 		(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
593 		    zhp->zfs_name, sdd->fromsnap);
594 		if (ioctl(zhp->zfs_hdl->libzfs_fd,
595 		    ZFS_IOC_OBJSET_STATS, &zc) != 0) {
596 			missingfrom = B_TRUE;
597 		}
598 	}
599 
600 	if (sdd->doall) {
601 		sdd->seenfrom = sdd->seento = sdd->lastsnap[0] = 0;
602 		if (sdd->fromsnap == NULL || missingfrom)
603 			sdd->seenfrom = B_TRUE;
604 
605 		rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
606 		if (!sdd->seenfrom) {
607 			(void) fprintf(stderr,
608 			    "WARNING: could not send %s@%s:\n"
609 			    "incremental source (%s@%s) does not exist\n",
610 			    zhp->zfs_name, sdd->tosnap,
611 			    zhp->zfs_name, sdd->fromsnap);
612 			sdd->err = B_TRUE;
613 		} else if (!sdd->seento) {
614 			if (sdd->fromsnap) {
615 				(void) fprintf(stderr,
616 				    "WARNING: could not send %s@%s:\n"
617 				    "incremental source (%s@%s) "
618 				    "is not earlier than it\n",
619 				    zhp->zfs_name, sdd->tosnap,
620 				    zhp->zfs_name, sdd->fromsnap);
621 			} else {
622 				(void) fprintf(stderr, "WARNING: "
623 				    "could not send %s@%s: does not exist\n",
624 				    zhp->zfs_name, sdd->tosnap);
625 			}
626 			sdd->err = B_TRUE;
627 		}
628 	} else {
629 		zfs_handle_t *snapzhp;
630 		char snapname[ZFS_MAXNAMELEN];
631 
632 		(void) snprintf(snapname, sizeof (snapname), "%s@%s",
633 		    zfs_get_name(zhp), sdd->tosnap);
634 		snapzhp = zfs_open(zhp->zfs_hdl, snapname, ZFS_TYPE_SNAPSHOT);
635 		if (snapzhp == NULL) {
636 			rv = -1;
637 		} else {
638 			rv = dump_ioctl(snapzhp,
639 			    missingfrom ? NULL : sdd->fromsnap,
640 			    sdd->fromorigin || missingfrom,
641 			    sdd->outfd);
642 			sdd->seento = B_TRUE;
643 			zfs_close(snapzhp);
644 		}
645 	}
646 
647 	return (rv);
648 }
649 
650 static int
651 dump_filesystems(zfs_handle_t *rzhp, void *arg)
652 {
653 	send_dump_data_t *sdd = arg;
654 	nvpair_t *fspair;
655 	boolean_t needagain, progress;
656 
657 	if (!sdd->replicate)
658 		return (dump_filesystem(rzhp, sdd));
659 
660 again:
661 	needagain = progress = B_FALSE;
662 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
663 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
664 		nvlist_t *fslist;
665 		char *fsname;
666 		zfs_handle_t *zhp;
667 		int err;
668 		uint64_t origin_guid = 0;
669 		nvlist_t *origin_nv;
670 
671 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
672 		if (nvlist_lookup_boolean(fslist, "sent") == 0)
673 			continue;
674 
675 		VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
676 		(void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
677 
678 		origin_nv = fsavl_find(sdd->fsavl, origin_guid, NULL);
679 		if (origin_nv &&
680 		    nvlist_lookup_boolean(origin_nv, "sent") == ENOENT) {
681 			/*
682 			 * origin has not been sent yet;
683 			 * skip this clone.
684 			 */
685 			needagain = B_TRUE;
686 			continue;
687 		}
688 
689 		zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
690 		if (zhp == NULL)
691 			return (-1);
692 		err = dump_filesystem(zhp, sdd);
693 		VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
694 		progress = B_TRUE;
695 		zfs_close(zhp);
696 		if (err)
697 			return (err);
698 	}
699 	if (needagain) {
700 		assert(progress);
701 		goto again;
702 	}
703 	return (0);
704 }
705 
706 /*
707  * Generate a send stream for the dataset identified by the argument zhp.
708  *
709  * The content of the send stream is the snapshot identified by
710  * 'tosnap'.  Incremental streams are requested in two ways:
711  *     - from the snapshot identified by "fromsnap" (if non-null) or
712  *     - from the origin of the dataset identified by zhp, which must
713  *	 be a clone.  In this case, "fromsnap" is null and "fromorigin"
714  *	 is TRUE.
715  *
716  * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
717  * uses a special header (with a version field of DMU_BACKUP_HEADER_VERSION)
718  * if "replicate" is set.  If "doall" is set, dump all the intermediate
719  * snapshots. The DMU_BACKUP_HEADER_VERSION header is used in the "doall"
720  * case too.
721  */
722 int
723 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
724     boolean_t replicate, boolean_t doall, boolean_t fromorigin,
725     boolean_t verbose, int outfd)
726 {
727 	char errbuf[1024];
728 	send_dump_data_t sdd = { 0 };
729 	int err;
730 	nvlist_t *fss = NULL;
731 	avl_tree_t *fsavl = NULL;
732 	char holdtag[128];
733 	static uint64_t holdseq;
734 	int spa_version;
735 	boolean_t holdsnaps = B_FALSE;
736 
737 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
738 	    "cannot send '%s'"), zhp->zfs_name);
739 
740 	if (fromsnap && fromsnap[0] == '\0') {
741 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
742 		    "zero-length incremental source"));
743 		return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
744 	}
745 
746 	if (zfs_spa_version(zhp, &spa_version) == 0 &&
747 	    spa_version >= SPA_VERSION_USERREFS)
748 		holdsnaps = B_TRUE;
749 
750 	if (replicate || doall) {
751 		dmu_replay_record_t drr = { 0 };
752 		char *packbuf = NULL;
753 		size_t buflen = 0;
754 		zio_cksum_t zc = { 0 };
755 
756 		assert(fromsnap || doall);
757 
758 		if (holdsnaps) {
759 			(void) snprintf(holdtag, sizeof (holdtag),
760 			    ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
761 			++holdseq;
762 			err = zfs_hold_range(zhp, fromsnap, tosnap,
763 			    holdtag, B_TRUE);
764 			if (err)
765 				return (err);
766 		}
767 		if (replicate) {
768 			nvlist_t *hdrnv;
769 
770 			VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
771 			if (fromsnap) {
772 				VERIFY(0 == nvlist_add_string(hdrnv,
773 				    "fromsnap", fromsnap));
774 			}
775 			VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
776 
777 			err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
778 			    fromsnap, tosnap, &fss, &fsavl);
779 			if (err) {
780 				if (holdsnaps) {
781 					(void) zfs_release_range(zhp, fromsnap,
782 					    tosnap, holdtag);
783 				}
784 				return (err);
785 			}
786 			VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
787 			err = nvlist_pack(hdrnv, &packbuf, &buflen,
788 			    NV_ENCODE_XDR, 0);
789 			nvlist_free(hdrnv);
790 			if (err) {
791 				fsavl_destroy(fsavl);
792 				nvlist_free(fss);
793 				if (holdsnaps) {
794 					(void) zfs_release_range(zhp, fromsnap,
795 					    tosnap, holdtag);
796 				}
797 				return (zfs_standard_error(zhp->zfs_hdl,
798 				    err, errbuf));
799 			}
800 		}
801 
802 		/* write first begin record */
803 		drr.drr_type = DRR_BEGIN;
804 		drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
805 		drr.drr_u.drr_begin.drr_version = DMU_BACKUP_HEADER_VERSION;
806 		(void) snprintf(drr.drr_u.drr_begin.drr_toname,
807 		    sizeof (drr.drr_u.drr_begin.drr_toname),
808 		    "%s@%s", zhp->zfs_name, tosnap);
809 		drr.drr_payloadlen = buflen;
810 		fletcher_4_incremental_native(&drr, sizeof (drr), &zc);
811 		err = write(outfd, &drr, sizeof (drr));
812 
813 		/* write header nvlist */
814 		if (err != -1) {
815 			fletcher_4_incremental_native(packbuf, buflen, &zc);
816 			err = write(outfd, packbuf, buflen);
817 		}
818 		free(packbuf);
819 		if (err == -1) {
820 			fsavl_destroy(fsavl);
821 			nvlist_free(fss);
822 			if (holdsnaps) {
823 				(void) zfs_release_range(zhp, fromsnap, tosnap,
824 				    holdtag);
825 			}
826 			return (zfs_standard_error(zhp->zfs_hdl,
827 			    errno, errbuf));
828 		}
829 
830 		/* write end record */
831 		if (err != -1) {
832 			bzero(&drr, sizeof (drr));
833 			drr.drr_type = DRR_END;
834 			drr.drr_u.drr_end.drr_checksum = zc;
835 			err = write(outfd, &drr, sizeof (drr));
836 			if (err == -1) {
837 				fsavl_destroy(fsavl);
838 				nvlist_free(fss);
839 				if (holdsnaps) {
840 					(void) zfs_release_range(zhp, fromsnap,
841 					    tosnap, holdtag);
842 				}
843 				return (zfs_standard_error(zhp->zfs_hdl,
844 				    errno, errbuf));
845 			}
846 		}
847 	}
848 
849 	/* dump each stream */
850 	sdd.fromsnap = fromsnap;
851 	sdd.tosnap = tosnap;
852 	sdd.outfd = outfd;
853 	sdd.replicate = replicate;
854 	sdd.doall = doall;
855 	sdd.fromorigin = fromorigin;
856 	sdd.fss = fss;
857 	sdd.fsavl = fsavl;
858 	sdd.verbose = verbose;
859 	err = dump_filesystems(zhp, &sdd);
860 	fsavl_destroy(fsavl);
861 	nvlist_free(fss);
862 
863 	if (replicate || doall) {
864 		/*
865 		 * write final end record.  NB: want to do this even if
866 		 * there was some error, because it might not be totally
867 		 * failed.
868 		 */
869 		dmu_replay_record_t drr = { 0 };
870 		drr.drr_type = DRR_END;
871 		if (holdsnaps) {
872 			(void) zfs_release_range(zhp, fromsnap, tosnap,
873 			    holdtag);
874 		}
875 		if (write(outfd, &drr, sizeof (drr)) == -1) {
876 			return (zfs_standard_error(zhp->zfs_hdl,
877 			    errno, errbuf));
878 		}
879 	}
880 
881 	return (err || sdd.err);
882 }
883 
884 /*
885  * Routines specific to "zfs recv"
886  */
887 
888 static int
889 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
890     boolean_t byteswap, zio_cksum_t *zc)
891 {
892 	char *cp = buf;
893 	int rv;
894 	int len = ilen;
895 
896 	do {
897 		rv = read(fd, cp, len);
898 		cp += rv;
899 		len -= rv;
900 	} while (rv > 0);
901 
902 	if (rv < 0 || len != 0) {
903 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
904 		    "failed to read from stream"));
905 		return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
906 		    "cannot receive")));
907 	}
908 
909 	if (zc) {
910 		if (byteswap)
911 			fletcher_4_incremental_byteswap(buf, ilen, zc);
912 		else
913 			fletcher_4_incremental_native(buf, ilen, zc);
914 	}
915 	return (0);
916 }
917 
918 static int
919 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
920     boolean_t byteswap, zio_cksum_t *zc)
921 {
922 	char *buf;
923 	int err;
924 
925 	buf = zfs_alloc(hdl, len);
926 	if (buf == NULL)
927 		return (ENOMEM);
928 
929 	err = recv_read(hdl, fd, buf, len, byteswap, zc);
930 	if (err != 0) {
931 		free(buf);
932 		return (err);
933 	}
934 
935 	err = nvlist_unpack(buf, len, nvp, 0);
936 	free(buf);
937 	if (err != 0) {
938 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
939 		    "stream (malformed nvlist)"));
940 		return (EINVAL);
941 	}
942 	return (0);
943 }
944 
945 static int
946 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
947     int baselen, char *newname, recvflags_t flags)
948 {
949 	static int seq;
950 	zfs_cmd_t zc = { 0 };
951 	int err;
952 	prop_changelist_t *clp;
953 	zfs_handle_t *zhp;
954 
955 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
956 	if (zhp == NULL)
957 		return (-1);
958 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
959 	    flags.force ? MS_FORCE : 0);
960 	zfs_close(zhp);
961 	if (clp == NULL)
962 		return (-1);
963 	err = changelist_prefix(clp);
964 	if (err)
965 		return (err);
966 
967 	zc.zc_objset_type = DMU_OST_ZFS;
968 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
969 
970 	if (tryname) {
971 		(void) strcpy(newname, tryname);
972 
973 		(void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
974 
975 		if (flags.verbose) {
976 			(void) printf("attempting rename %s to %s\n",
977 			    zc.zc_name, zc.zc_value);
978 		}
979 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
980 		if (err == 0)
981 			changelist_rename(clp, name, tryname);
982 	} else {
983 		err = ENOENT;
984 	}
985 
986 	if (err != 0 && strncmp(name+baselen, "recv-", 5) != 0) {
987 		seq++;
988 
989 		(void) strncpy(newname, name, baselen);
990 		(void) snprintf(newname+baselen, ZFS_MAXNAMELEN-baselen,
991 		    "recv-%u-%u", getpid(), seq);
992 		(void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
993 
994 		if (flags.verbose) {
995 			(void) printf("failed - trying rename %s to %s\n",
996 			    zc.zc_name, zc.zc_value);
997 		}
998 		err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
999 		if (err == 0)
1000 			changelist_rename(clp, name, newname);
1001 		if (err && flags.verbose) {
1002 			(void) printf("failed (%u) - "
1003 			    "will try again on next pass\n", errno);
1004 		}
1005 		err = EAGAIN;
1006 	} else if (flags.verbose) {
1007 		if (err == 0)
1008 			(void) printf("success\n");
1009 		else
1010 			(void) printf("failed (%u)\n", errno);
1011 	}
1012 
1013 	(void) changelist_postfix(clp);
1014 	changelist_free(clp);
1015 
1016 	return (err);
1017 }
1018 
1019 static int
1020 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
1021     char *newname, recvflags_t flags)
1022 {
1023 	zfs_cmd_t zc = { 0 };
1024 	int err = 0;
1025 	prop_changelist_t *clp;
1026 	zfs_handle_t *zhp;
1027 	boolean_t defer = B_FALSE;
1028 	int spa_version;
1029 
1030 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1031 	if (zhp == NULL)
1032 		return (-1);
1033 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
1034 	    flags.force ? MS_FORCE : 0);
1035 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
1036 	    zfs_spa_version(zhp, &spa_version) == 0 &&
1037 	    spa_version >= SPA_VERSION_USERREFS)
1038 		defer = B_TRUE;
1039 	zfs_close(zhp);
1040 	if (clp == NULL)
1041 		return (-1);
1042 	err = changelist_prefix(clp);
1043 	if (err)
1044 		return (err);
1045 
1046 	zc.zc_objset_type = DMU_OST_ZFS;
1047 	zc.zc_defer_destroy = defer;
1048 	(void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
1049 
1050 	if (flags.verbose)
1051 		(void) printf("attempting destroy %s\n", zc.zc_name);
1052 	err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
1053 	if (err == 0) {
1054 		if (flags.verbose)
1055 			(void) printf("success\n");
1056 		changelist_remove(clp, zc.zc_name);
1057 	}
1058 
1059 	(void) changelist_postfix(clp);
1060 	changelist_free(clp);
1061 
1062 	/*
1063 	 * Deferred destroy should always succeed. Since we can't tell
1064 	 * if it destroyed the dataset or just marked it for deferred
1065 	 * destroy, always do the rename just in case.
1066 	 */
1067 	if (err != 0 || defer)
1068 		err = recv_rename(hdl, name, NULL, baselen, newname, flags);
1069 
1070 	return (err);
1071 }
1072 
1073 typedef struct guid_to_name_data {
1074 	uint64_t guid;
1075 	char *name;
1076 } guid_to_name_data_t;
1077 
1078 static int
1079 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
1080 {
1081 	guid_to_name_data_t *gtnd = arg;
1082 	int err;
1083 
1084 	if (zhp->zfs_dmustats.dds_guid == gtnd->guid) {
1085 		(void) strcpy(gtnd->name, zhp->zfs_name);
1086 		return (EEXIST);
1087 	}
1088 	err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
1089 	zfs_close(zhp);
1090 	return (err);
1091 }
1092 
1093 static int
1094 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
1095     char *name)
1096 {
1097 	/* exhaustive search all local snapshots */
1098 	guid_to_name_data_t gtnd;
1099 	int err = 0;
1100 	zfs_handle_t *zhp;
1101 	char *cp;
1102 
1103 	gtnd.guid = guid;
1104 	gtnd.name = name;
1105 
1106 	if (strchr(parent, '@') == NULL) {
1107 		zhp = make_dataset_handle(hdl, parent);
1108 		if (zhp != NULL) {
1109 			err = zfs_iter_children(zhp, guid_to_name_cb, &gtnd);
1110 			zfs_close(zhp);
1111 			if (err == EEXIST)
1112 				return (0);
1113 		}
1114 	}
1115 
1116 	cp = strchr(parent, '/');
1117 	if (cp)
1118 		*cp = '\0';
1119 	zhp = make_dataset_handle(hdl, parent);
1120 	if (cp)
1121 		*cp = '/';
1122 
1123 	if (zhp) {
1124 		err = zfs_iter_children(zhp, guid_to_name_cb, &gtnd);
1125 		zfs_close(zhp);
1126 	}
1127 
1128 	return (err == EEXIST ? 0 : ENOENT);
1129 
1130 }
1131 
1132 /*
1133  * Return true if dataset guid1 is created before guid2.
1134  */
1135 static int
1136 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
1137     uint64_t guid1, uint64_t guid2)
1138 {
1139 	nvlist_t *nvfs;
1140 	char *fsname, *snapname;
1141 	char buf[ZFS_MAXNAMELEN];
1142 	int rv;
1143 	zfs_node_t zn1, zn2;
1144 
1145 	if (guid2 == 0)
1146 		return (0);
1147 	if (guid1 == 0)
1148 		return (1);
1149 
1150 	nvfs = fsavl_find(avl, guid1, &snapname);
1151 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1152 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1153 	zn1.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1154 	if (zn1.zn_handle == NULL)
1155 		return (-1);
1156 
1157 	nvfs = fsavl_find(avl, guid2, &snapname);
1158 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1159 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
1160 	zn2.zn_handle = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
1161 	if (zn2.zn_handle == NULL) {
1162 		zfs_close(zn2.zn_handle);
1163 		return (-1);
1164 	}
1165 
1166 	rv = (zfs_snapshot_compare(&zn1, &zn2) == -1);
1167 
1168 	zfs_close(zn1.zn_handle);
1169 	zfs_close(zn2.zn_handle);
1170 
1171 	return (rv);
1172 }
1173 
1174 static int
1175 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
1176     recvflags_t flags, nvlist_t *stream_nv, avl_tree_t *stream_avl)
1177 {
1178 	nvlist_t *local_nv;
1179 	avl_tree_t *local_avl;
1180 	nvpair_t *fselem, *nextfselem;
1181 	char *tosnap, *fromsnap;
1182 	char newname[ZFS_MAXNAMELEN];
1183 	int error;
1184 	boolean_t needagain, progress;
1185 	char *s1, *s2;
1186 
1187 	VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
1188 	VERIFY(0 == nvlist_lookup_string(stream_nv, "tosnap", &tosnap));
1189 
1190 	if (flags.dryrun)
1191 		return (0);
1192 
1193 again:
1194 	needagain = progress = B_FALSE;
1195 
1196 	if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
1197 	    &local_nv, &local_avl)) != 0)
1198 		return (error);
1199 
1200 	/*
1201 	 * Process deletes and renames
1202 	 */
1203 	for (fselem = nvlist_next_nvpair(local_nv, NULL);
1204 	    fselem; fselem = nextfselem) {
1205 		nvlist_t *nvfs, *snaps;
1206 		nvlist_t *stream_nvfs = NULL;
1207 		nvpair_t *snapelem, *nextsnapelem;
1208 		uint64_t fromguid = 0;
1209 		uint64_t originguid = 0;
1210 		uint64_t stream_originguid = 0;
1211 		uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
1212 		char *fsname, *stream_fsname;
1213 
1214 		nextfselem = nvlist_next_nvpair(local_nv, fselem);
1215 
1216 		VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
1217 		VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
1218 		VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
1219 		VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
1220 		    &parent_fromsnap_guid));
1221 		(void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
1222 
1223 		/*
1224 		 * First find the stream's fs, so we can check for
1225 		 * a different origin (due to "zfs promote")
1226 		 */
1227 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
1228 		    snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
1229 			uint64_t thisguid;
1230 
1231 			VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1232 			stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
1233 
1234 			if (stream_nvfs != NULL)
1235 				break;
1236 		}
1237 
1238 		/* check for promote */
1239 		(void) nvlist_lookup_uint64(stream_nvfs, "origin",
1240 		    &stream_originguid);
1241 		if (stream_nvfs && originguid != stream_originguid) {
1242 			switch (created_before(hdl, local_avl,
1243 			    stream_originguid, originguid)) {
1244 			case 1: {
1245 				/* promote it! */
1246 				zfs_cmd_t zc = { 0 };
1247 				nvlist_t *origin_nvfs;
1248 				char *origin_fsname;
1249 
1250 				if (flags.verbose)
1251 					(void) printf("promoting %s\n", fsname);
1252 
1253 				origin_nvfs = fsavl_find(local_avl, originguid,
1254 				    NULL);
1255 				VERIFY(0 == nvlist_lookup_string(origin_nvfs,
1256 				    "name", &origin_fsname));
1257 				(void) strlcpy(zc.zc_value, origin_fsname,
1258 				    sizeof (zc.zc_value));
1259 				(void) strlcpy(zc.zc_name, fsname,
1260 				    sizeof (zc.zc_name));
1261 				error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
1262 				if (error == 0)
1263 					progress = B_TRUE;
1264 				break;
1265 			}
1266 			default:
1267 				break;
1268 			case -1:
1269 				fsavl_destroy(local_avl);
1270 				nvlist_free(local_nv);
1271 				return (-1);
1272 			}
1273 			/*
1274 			 * We had/have the wrong origin, therefore our
1275 			 * list of snapshots is wrong.  Need to handle
1276 			 * them on the next pass.
1277 			 */
1278 			needagain = B_TRUE;
1279 			continue;
1280 		}
1281 
1282 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
1283 		    snapelem; snapelem = nextsnapelem) {
1284 			uint64_t thisguid;
1285 			char *stream_snapname;
1286 			nvlist_t *found, *props;
1287 
1288 			nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
1289 
1290 			VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
1291 			found = fsavl_find(stream_avl, thisguid,
1292 			    &stream_snapname);
1293 
1294 			/* check for delete */
1295 			if (found == NULL) {
1296 				char name[ZFS_MAXNAMELEN];
1297 
1298 				if (!flags.force)
1299 					continue;
1300 
1301 				(void) snprintf(name, sizeof (name), "%s@%s",
1302 				    fsname, nvpair_name(snapelem));
1303 
1304 				error = recv_destroy(hdl, name,
1305 				    strlen(fsname)+1, newname, flags);
1306 				if (error)
1307 					needagain = B_TRUE;
1308 				else
1309 					progress = B_TRUE;
1310 				continue;
1311 			}
1312 
1313 			stream_nvfs = found;
1314 
1315 			if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
1316 			    &props) && 0 == nvlist_lookup_nvlist(props,
1317 			    stream_snapname, &props)) {
1318 				zfs_cmd_t zc = { 0 };
1319 
1320 				zc.zc_cookie = B_TRUE; /* clear current props */
1321 				(void) snprintf(zc.zc_name, sizeof (zc.zc_name),
1322 				    "%s@%s", fsname, nvpair_name(snapelem));
1323 				if (zcmd_write_src_nvlist(hdl, &zc,
1324 				    props) == 0) {
1325 					(void) zfs_ioctl(hdl,
1326 					    ZFS_IOC_SET_PROP, &zc);
1327 					zcmd_free_nvlists(&zc);
1328 				}
1329 			}
1330 
1331 			/* check for different snapname */
1332 			if (strcmp(nvpair_name(snapelem),
1333 			    stream_snapname) != 0) {
1334 				char name[ZFS_MAXNAMELEN];
1335 				char tryname[ZFS_MAXNAMELEN];
1336 
1337 				(void) snprintf(name, sizeof (name), "%s@%s",
1338 				    fsname, nvpair_name(snapelem));
1339 				(void) snprintf(tryname, sizeof (name), "%s@%s",
1340 				    fsname, stream_snapname);
1341 
1342 				error = recv_rename(hdl, name, tryname,
1343 				    strlen(fsname)+1, newname, flags);
1344 				if (error)
1345 					needagain = B_TRUE;
1346 				else
1347 					progress = B_TRUE;
1348 			}
1349 
1350 			if (strcmp(stream_snapname, fromsnap) == 0)
1351 				fromguid = thisguid;
1352 		}
1353 
1354 		/* check for delete */
1355 		if (stream_nvfs == NULL) {
1356 			if (!flags.force)
1357 				continue;
1358 
1359 			error = recv_destroy(hdl, fsname, strlen(tofs)+1,
1360 			    newname, flags);
1361 			if (error)
1362 				needagain = B_TRUE;
1363 			else
1364 				progress = B_TRUE;
1365 			continue;
1366 		}
1367 
1368 		if (fromguid == 0 && flags.verbose) {
1369 			(void) printf("local fs %s does not have fromsnap "
1370 			    "(%s in stream); must have been deleted locally; "
1371 			    "ignoring\n", fsname, fromsnap);
1372 			continue;
1373 		}
1374 
1375 		VERIFY(0 == nvlist_lookup_string(stream_nvfs,
1376 		    "name", &stream_fsname));
1377 		VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
1378 		    "parentfromsnap", &stream_parent_fromsnap_guid));
1379 
1380 		s1 = strrchr(fsname, '/');
1381 		s2 = strrchr(stream_fsname, '/');
1382 
1383 		/* check for rename */
1384 		if ((stream_parent_fromsnap_guid != 0 &&
1385 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
1386 		    ((s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
1387 			nvlist_t *parent;
1388 			char tryname[ZFS_MAXNAMELEN];
1389 
1390 			parent = fsavl_find(local_avl,
1391 			    stream_parent_fromsnap_guid, NULL);
1392 			/*
1393 			 * NB: parent might not be found if we used the
1394 			 * tosnap for stream_parent_fromsnap_guid,
1395 			 * because the parent is a newly-created fs;
1396 			 * we'll be able to rename it after we recv the
1397 			 * new fs.
1398 			 */
1399 			if (parent != NULL) {
1400 				char *pname;
1401 
1402 				VERIFY(0 == nvlist_lookup_string(parent, "name",
1403 				    &pname));
1404 				(void) snprintf(tryname, sizeof (tryname),
1405 				    "%s%s", pname, strrchr(stream_fsname, '/'));
1406 			} else {
1407 				tryname[0] = '\0';
1408 				if (flags.verbose) {
1409 					(void) printf("local fs %s new parent "
1410 					    "not found\n", fsname);
1411 				}
1412 			}
1413 
1414 			error = recv_rename(hdl, fsname, tryname,
1415 			    strlen(tofs)+1, newname, flags);
1416 			if (error)
1417 				needagain = B_TRUE;
1418 			else
1419 				progress = B_TRUE;
1420 		}
1421 	}
1422 
1423 	fsavl_destroy(local_avl);
1424 	nvlist_free(local_nv);
1425 
1426 	if (needagain && progress) {
1427 		/* do another pass to fix up temporary names */
1428 		if (flags.verbose)
1429 			(void) printf("another pass:\n");
1430 		goto again;
1431 	}
1432 
1433 	return (needagain);
1434 }
1435 
1436 static int
1437 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
1438     recvflags_t flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
1439     char **top_zfs)
1440 {
1441 	nvlist_t *stream_nv = NULL;
1442 	avl_tree_t *stream_avl = NULL;
1443 	char *fromsnap = NULL;
1444 	char tofs[ZFS_MAXNAMELEN];
1445 	char errbuf[1024];
1446 	dmu_replay_record_t drre;
1447 	int error;
1448 	boolean_t anyerr = B_FALSE;
1449 	boolean_t softerr = B_FALSE;
1450 
1451 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1452 	    "cannot receive"));
1453 
1454 	if (strchr(destname, '@')) {
1455 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1456 		    "can not specify snapshot name for multi-snapshot stream"));
1457 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1458 	}
1459 
1460 	assert(drr->drr_type == DRR_BEGIN);
1461 	assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
1462 	assert(drr->drr_u.drr_begin.drr_version == DMU_BACKUP_HEADER_VERSION);
1463 
1464 	/*
1465 	 * Read in the nvlist from the stream.
1466 	 */
1467 	if (drr->drr_payloadlen != 0) {
1468 		if (!flags.isprefix) {
1469 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1470 			    "must use -d to receive replication "
1471 			    "(send -R) stream"));
1472 			return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
1473 		}
1474 
1475 		error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
1476 		    &stream_nv, flags.byteswap, zc);
1477 		if (error) {
1478 			error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1479 			goto out;
1480 		}
1481 	}
1482 
1483 	/*
1484 	 * Read in the end record and verify checksum.
1485 	 */
1486 	if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
1487 	    flags.byteswap, NULL)))
1488 		goto out;
1489 	if (flags.byteswap) {
1490 		drre.drr_type = BSWAP_32(drre.drr_type);
1491 		drre.drr_u.drr_end.drr_checksum.zc_word[0] =
1492 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
1493 		drre.drr_u.drr_end.drr_checksum.zc_word[1] =
1494 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
1495 		drre.drr_u.drr_end.drr_checksum.zc_word[2] =
1496 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
1497 		drre.drr_u.drr_end.drr_checksum.zc_word[3] =
1498 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
1499 	}
1500 	if (drre.drr_type != DRR_END) {
1501 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1502 		goto out;
1503 	}
1504 	if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
1505 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1506 		    "incorrect header checksum"));
1507 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1508 		goto out;
1509 	}
1510 
1511 	(void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
1512 
1513 	if (drr->drr_payloadlen != 0) {
1514 		nvlist_t *stream_fss;
1515 
1516 		VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
1517 		    &stream_fss));
1518 		if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
1519 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1520 			    "couldn't allocate avl tree"));
1521 			error = zfs_error(hdl, EZFS_NOMEM, errbuf);
1522 			goto out;
1523 		}
1524 
1525 		if (fromsnap != NULL) {
1526 			(void) strlcpy(tofs, destname, ZFS_MAXNAMELEN);
1527 			if (flags.isprefix) {
1528 				int i = strcspn(drr->drr_u.drr_begin.drr_toname,
1529 				    "/@");
1530 				/* zfs_receive_one() will create_parents() */
1531 				(void) strlcat(tofs,
1532 				    &drr->drr_u.drr_begin.drr_toname[i],
1533 				    ZFS_MAXNAMELEN);
1534 				*strchr(tofs, '@') = '\0';
1535 			}
1536 			softerr = recv_incremental_replication(hdl, tofs,
1537 			    flags, stream_nv, stream_avl);
1538 		}
1539 	}
1540 
1541 
1542 	/* Finally, receive each contained stream */
1543 	do {
1544 		/*
1545 		 * we should figure out if it has a recoverable
1546 		 * error, in which case do a recv_skip() and drive on.
1547 		 * Note, if we fail due to already having this guid,
1548 		 * zfs_receive_one() will take care of it (ie,
1549 		 * recv_skip() and return 0).
1550 		 */
1551 		error = zfs_receive_impl(hdl, destname, flags, fd,
1552 		    stream_avl, top_zfs);
1553 		if (error == ENODATA) {
1554 			error = 0;
1555 			break;
1556 		}
1557 		anyerr |= error;
1558 	} while (error == 0);
1559 
1560 	if (drr->drr_payloadlen != 0 && fromsnap != NULL) {
1561 		/*
1562 		 * Now that we have the fs's they sent us, try the
1563 		 * renames again.
1564 		 */
1565 		softerr = recv_incremental_replication(hdl, tofs, flags,
1566 		    stream_nv, stream_avl);
1567 	}
1568 
1569 out:
1570 	fsavl_destroy(stream_avl);
1571 	if (stream_nv)
1572 		nvlist_free(stream_nv);
1573 	if (softerr)
1574 		error = -2;
1575 	if (anyerr)
1576 		error = -1;
1577 	return (error);
1578 }
1579 
1580 static int
1581 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
1582 {
1583 	dmu_replay_record_t *drr;
1584 	void *buf = malloc(1<<20);
1585 
1586 	/* XXX would be great to use lseek if possible... */
1587 	drr = buf;
1588 
1589 	while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
1590 	    byteswap, NULL) == 0) {
1591 		if (byteswap)
1592 			drr->drr_type = BSWAP_32(drr->drr_type);
1593 
1594 		switch (drr->drr_type) {
1595 		case DRR_BEGIN:
1596 			/* NB: not to be used on v2 stream packages */
1597 			assert(drr->drr_payloadlen == 0);
1598 			break;
1599 
1600 		case DRR_END:
1601 			free(buf);
1602 			return (0);
1603 
1604 		case DRR_OBJECT:
1605 			if (byteswap) {
1606 				drr->drr_u.drr_object.drr_bonuslen =
1607 				    BSWAP_32(drr->drr_u.drr_object.
1608 				    drr_bonuslen);
1609 			}
1610 			(void) recv_read(hdl, fd, buf,
1611 			    P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
1612 			    B_FALSE, NULL);
1613 			break;
1614 
1615 		case DRR_WRITE:
1616 			if (byteswap) {
1617 				drr->drr_u.drr_write.drr_length =
1618 				    BSWAP_64(drr->drr_u.drr_write.drr_length);
1619 			}
1620 			(void) recv_read(hdl, fd, buf,
1621 			    drr->drr_u.drr_write.drr_length, B_FALSE, NULL);
1622 			break;
1623 
1624 		case DRR_FREEOBJECTS:
1625 		case DRR_FREE:
1626 			break;
1627 
1628 		default:
1629 			assert(!"invalid record type");
1630 		}
1631 	}
1632 
1633 	free(buf);
1634 	return (-1);
1635 }
1636 
1637 /*
1638  * Restores a backup of tosnap from the file descriptor specified by infd.
1639  */
1640 static int
1641 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
1642     recvflags_t flags, dmu_replay_record_t *drr,
1643     dmu_replay_record_t *drr_noswap, avl_tree_t *stream_avl,
1644     char **top_zfs)
1645 {
1646 	zfs_cmd_t zc = { 0 };
1647 	time_t begin_time;
1648 	int ioctl_err, ioctl_errno, err, choplen;
1649 	char *cp;
1650 	struct drr_begin *drrb = &drr->drr_u.drr_begin;
1651 	char errbuf[1024];
1652 	char chopprefix[ZFS_MAXNAMELEN];
1653 	boolean_t newfs = B_FALSE;
1654 	boolean_t stream_wantsnewfs;
1655 	uint64_t parent_snapguid = 0;
1656 	prop_changelist_t *clp = NULL;
1657 	nvlist_t *snapprops_nvlist = NULL;
1658 
1659 	begin_time = time(NULL);
1660 
1661 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1662 	    "cannot receive"));
1663 
1664 	if (stream_avl != NULL) {
1665 		char *snapname;
1666 		nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
1667 		    &snapname);
1668 		nvlist_t *props;
1669 		int ret;
1670 
1671 		(void) nvlist_lookup_uint64(fs, "parentfromsnap",
1672 		    &parent_snapguid);
1673 		err = nvlist_lookup_nvlist(fs, "props", &props);
1674 		if (err)
1675 			VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
1676 
1677 		if (flags.canmountoff) {
1678 			VERIFY(0 == nvlist_add_uint64(props,
1679 			    zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
1680 		}
1681 		ret = zcmd_write_src_nvlist(hdl, &zc, props);
1682 		if (err)
1683 			nvlist_free(props);
1684 
1685 		if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
1686 			VERIFY(0 == nvlist_lookup_nvlist(props,
1687 			    snapname, &snapprops_nvlist));
1688 		}
1689 
1690 		if (ret != 0)
1691 			return (-1);
1692 	}
1693 
1694 	/*
1695 	 * Determine how much of the snapshot name stored in the stream
1696 	 * we are going to tack on to the name they specified on the
1697 	 * command line, and how much we are going to chop off.
1698 	 *
1699 	 * If they specified a snapshot, chop the entire name stored in
1700 	 * the stream.
1701 	 */
1702 	(void) strcpy(chopprefix, drrb->drr_toname);
1703 	if (flags.isprefix) {
1704 		/*
1705 		 * They specified a fs with -d, we want to tack on
1706 		 * everything but the pool name stored in the stream
1707 		 */
1708 		if (strchr(tosnap, '@')) {
1709 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
1710 			    "argument - snapshot not allowed with -d"));
1711 			return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1712 		}
1713 		cp = strchr(chopprefix, '/');
1714 		if (cp == NULL)
1715 			cp = strchr(chopprefix, '@');
1716 		*cp = '\0';
1717 	} else if (strchr(tosnap, '@') == NULL) {
1718 		/*
1719 		 * If they specified a filesystem without -d, we want to
1720 		 * tack on everything after the fs specified in the
1721 		 * first name from the stream.
1722 		 */
1723 		cp = strchr(chopprefix, '@');
1724 		*cp = '\0';
1725 	}
1726 	choplen = strlen(chopprefix);
1727 
1728 	/*
1729 	 * Determine name of destination snapshot, store in zc_value.
1730 	 */
1731 	(void) strcpy(zc.zc_value, tosnap);
1732 	(void) strncat(zc.zc_value, drrb->drr_toname+choplen,
1733 	    sizeof (zc.zc_value));
1734 	if (!zfs_name_valid(zc.zc_value, ZFS_TYPE_SNAPSHOT)) {
1735 		zcmd_free_nvlists(&zc);
1736 		return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
1737 	}
1738 
1739 	/*
1740 	 * Determine the name of the origin snapshot, store in zc_string.
1741 	 */
1742 	if (drrb->drr_flags & DRR_FLAG_CLONE) {
1743 		if (guid_to_name(hdl, tosnap,
1744 		    drrb->drr_fromguid, zc.zc_string) != 0) {
1745 			zcmd_free_nvlists(&zc);
1746 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1747 			    "local origin for clone %s does not exist"),
1748 			    zc.zc_value);
1749 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
1750 		}
1751 		if (flags.verbose)
1752 			(void) printf("found clone origin %s\n", zc.zc_string);
1753 	}
1754 
1755 	stream_wantsnewfs = (drrb->drr_fromguid == NULL ||
1756 	    (drrb->drr_flags & DRR_FLAG_CLONE));
1757 
1758 	if (stream_wantsnewfs) {
1759 		/*
1760 		 * if the parent fs does not exist, look for it based on
1761 		 * the parent snap GUID
1762 		 */
1763 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1764 		    "cannot receive new filesystem stream"));
1765 
1766 		(void) strcpy(zc.zc_name, zc.zc_value);
1767 		cp = strrchr(zc.zc_name, '/');
1768 		if (cp)
1769 			*cp = '\0';
1770 		if (cp &&
1771 		    !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1772 			char suffix[ZFS_MAXNAMELEN];
1773 			(void) strcpy(suffix, strrchr(zc.zc_value, '/'));
1774 			if (guid_to_name(hdl, tosnap, parent_snapguid,
1775 			    zc.zc_value) == 0) {
1776 				*strchr(zc.zc_value, '@') = '\0';
1777 				(void) strcat(zc.zc_value, suffix);
1778 			}
1779 		}
1780 	} else {
1781 		/*
1782 		 * if the fs does not exist, look for it based on the
1783 		 * fromsnap GUID
1784 		 */
1785 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1786 		    "cannot receive incremental stream"));
1787 
1788 		(void) strcpy(zc.zc_name, zc.zc_value);
1789 		*strchr(zc.zc_name, '@') = '\0';
1790 
1791 		if (!zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1792 			char snap[ZFS_MAXNAMELEN];
1793 			(void) strcpy(snap, strchr(zc.zc_value, '@'));
1794 			if (guid_to_name(hdl, tosnap, drrb->drr_fromguid,
1795 			    zc.zc_value) == 0) {
1796 				*strchr(zc.zc_value, '@') = '\0';
1797 				(void) strcat(zc.zc_value, snap);
1798 			}
1799 		}
1800 	}
1801 
1802 	(void) strcpy(zc.zc_name, zc.zc_value);
1803 	*strchr(zc.zc_name, '@') = '\0';
1804 
1805 	if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
1806 		zfs_handle_t *zhp;
1807 		/*
1808 		 * Destination fs exists.  Therefore this should either
1809 		 * be an incremental, or the stream specifies a new fs
1810 		 * (full stream or clone) and they want us to blow it
1811 		 * away (and have therefore specified -F and removed any
1812 		 * snapshots).
1813 		 */
1814 
1815 		if (stream_wantsnewfs) {
1816 			if (!flags.force) {
1817 				zcmd_free_nvlists(&zc);
1818 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1819 				    "destination '%s' exists\n"
1820 				    "must specify -F to overwrite it"),
1821 				    zc.zc_name);
1822 				return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1823 			}
1824 			if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
1825 			    &zc) == 0) {
1826 				zcmd_free_nvlists(&zc);
1827 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1828 				    "destination has snapshots (eg. %s)\n"
1829 				    "must destroy them to overwrite it"),
1830 				    zc.zc_name);
1831 				return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1832 			}
1833 		}
1834 
1835 		if ((zhp = zfs_open(hdl, zc.zc_name,
1836 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
1837 			zcmd_free_nvlists(&zc);
1838 			return (-1);
1839 		}
1840 
1841 		if (stream_wantsnewfs &&
1842 		    zhp->zfs_dmustats.dds_origin[0]) {
1843 			zcmd_free_nvlists(&zc);
1844 			zfs_close(zhp);
1845 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1846 			    "destination '%s' is a clone\n"
1847 			    "must destroy it to overwrite it"),
1848 			    zc.zc_name);
1849 			return (zfs_error(hdl, EZFS_EXISTS, errbuf));
1850 		}
1851 
1852 		if (!flags.dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
1853 		    stream_wantsnewfs) {
1854 			/* We can't do online recv in this case */
1855 			clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
1856 			if (clp == NULL) {
1857 				zfs_close(zhp);
1858 				zcmd_free_nvlists(&zc);
1859 				return (-1);
1860 			}
1861 			if (changelist_prefix(clp) != 0) {
1862 				changelist_free(clp);
1863 				zfs_close(zhp);
1864 				zcmd_free_nvlists(&zc);
1865 				return (-1);
1866 			}
1867 		}
1868 		zfs_close(zhp);
1869 	} else {
1870 		/*
1871 		 * Destination filesystem does not exist.  Therefore we better
1872 		 * be creating a new filesystem (either from a full backup, or
1873 		 * a clone).  It would therefore be invalid if the user
1874 		 * specified only the pool name (i.e. if the destination name
1875 		 * contained no slash character).
1876 		 */
1877 		if (!stream_wantsnewfs ||
1878 		    (cp = strrchr(zc.zc_name, '/')) == NULL) {
1879 			zcmd_free_nvlists(&zc);
1880 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1881 			    "destination '%s' does not exist"), zc.zc_name);
1882 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
1883 		}
1884 
1885 		/*
1886 		 * Trim off the final dataset component so we perform the
1887 		 * recvbackup ioctl to the filesystems's parent.
1888 		 */
1889 		*cp = '\0';
1890 
1891 		if (flags.isprefix && !flags.dryrun &&
1892 		    create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
1893 			zcmd_free_nvlists(&zc);
1894 			return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
1895 		}
1896 
1897 		newfs = B_TRUE;
1898 	}
1899 
1900 	zc.zc_begin_record = drr_noswap->drr_u.drr_begin;
1901 	zc.zc_cookie = infd;
1902 	zc.zc_guid = flags.force;
1903 	if (flags.verbose) {
1904 		(void) printf("%s %s stream of %s into %s\n",
1905 		    flags.dryrun ? "would receive" : "receiving",
1906 		    drrb->drr_fromguid ? "incremental" : "full",
1907 		    drrb->drr_toname, zc.zc_value);
1908 		(void) fflush(stdout);
1909 	}
1910 
1911 	if (flags.dryrun) {
1912 		zcmd_free_nvlists(&zc);
1913 		return (recv_skip(hdl, infd, flags.byteswap));
1914 	}
1915 
1916 	err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
1917 	ioctl_errno = errno;
1918 	zcmd_free_nvlists(&zc);
1919 
1920 	if (err == 0 && snapprops_nvlist) {
1921 		zfs_cmd_t zc2 = { 0 };
1922 
1923 		(void) strcpy(zc2.zc_name, zc.zc_value);
1924 		if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
1925 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc2);
1926 			zcmd_free_nvlists(&zc2);
1927 		}
1928 	}
1929 
1930 	if (err && (ioctl_errno == ENOENT || ioctl_errno == ENODEV)) {
1931 		/*
1932 		 * It may be that this snapshot already exists,
1933 		 * in which case we want to consume & ignore it
1934 		 * rather than failing.
1935 		 */
1936 		avl_tree_t *local_avl;
1937 		nvlist_t *local_nv, *fs;
1938 		char *cp = strchr(zc.zc_value, '@');
1939 
1940 		/*
1941 		 * XXX Do this faster by just iterating over snaps in
1942 		 * this fs.  Also if zc_value does not exist, we will
1943 		 * get a strange "does not exist" error message.
1944 		 */
1945 		*cp = '\0';
1946 		if (gather_nvlist(hdl, zc.zc_value, NULL, NULL,
1947 		    &local_nv, &local_avl) == 0) {
1948 			*cp = '@';
1949 			fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
1950 			fsavl_destroy(local_avl);
1951 			nvlist_free(local_nv);
1952 
1953 			if (fs != NULL) {
1954 				if (flags.verbose) {
1955 					(void) printf("snap %s already exists; "
1956 					    "ignoring\n", zc.zc_value);
1957 				}
1958 				ioctl_err = recv_skip(hdl, infd,
1959 				    flags.byteswap);
1960 			}
1961 		}
1962 		*cp = '@';
1963 	}
1964 
1965 
1966 	if (ioctl_err != 0) {
1967 		switch (ioctl_errno) {
1968 		case ENODEV:
1969 			cp = strchr(zc.zc_value, '@');
1970 			*cp = '\0';
1971 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1972 			    "most recent snapshot of %s does not\n"
1973 			    "match incremental source"), zc.zc_value);
1974 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1975 			*cp = '@';
1976 			break;
1977 		case ETXTBSY:
1978 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1979 			    "destination %s has been modified\n"
1980 			    "since most recent snapshot"), zc.zc_name);
1981 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
1982 			break;
1983 		case EEXIST:
1984 			cp = strchr(zc.zc_value, '@');
1985 			if (newfs) {
1986 				/* it's the containing fs that exists */
1987 				*cp = '\0';
1988 			}
1989 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1990 			    "destination already exists"));
1991 			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
1992 			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
1993 			    zc.zc_value);
1994 			*cp = '@';
1995 			break;
1996 		case EINVAL:
1997 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
1998 			break;
1999 		case ECKSUM:
2000 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2001 			    "invalid stream (checksum mismatch)"));
2002 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2003 			break;
2004 		default:
2005 			(void) zfs_standard_error(hdl, ioctl_errno, errbuf);
2006 		}
2007 	}
2008 
2009 	/*
2010 	 * Mount the target filesystem (if created).  Also mount any
2011 	 * children of the target filesystem if we did a replication
2012 	 * receive (indicated by stream_avl being non-NULL).
2013 	 */
2014 	cp = strchr(zc.zc_value, '@');
2015 	if (cp && (ioctl_err == 0 || !newfs)) {
2016 		zfs_handle_t *h;
2017 
2018 		*cp = '\0';
2019 		h = zfs_open(hdl, zc.zc_value,
2020 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
2021 		if (h != NULL) {
2022 			if (h->zfs_type == ZFS_TYPE_VOLUME) {
2023 				*cp = '@';
2024 			} else if (newfs || stream_avl) {
2025 				/*
2026 				 * Track the first/top of hierarchy fs,
2027 				 * for mounting and sharing later.
2028 				 */
2029 				if (top_zfs && *top_zfs == NULL)
2030 					*top_zfs = zfs_strdup(hdl, zc.zc_value);
2031 			}
2032 			zfs_close(h);
2033 		}
2034 		*cp = '@';
2035 	}
2036 
2037 	if (clp) {
2038 		err |= changelist_postfix(clp);
2039 		changelist_free(clp);
2040 	}
2041 
2042 	if (err || ioctl_err)
2043 		return (-1);
2044 
2045 	if (flags.verbose) {
2046 		char buf1[64];
2047 		char buf2[64];
2048 		uint64_t bytes = zc.zc_cookie;
2049 		time_t delta = time(NULL) - begin_time;
2050 		if (delta == 0)
2051 			delta = 1;
2052 		zfs_nicenum(bytes, buf1, sizeof (buf1));
2053 		zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
2054 
2055 		(void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
2056 		    buf1, delta, buf2);
2057 	}
2058 
2059 	return (0);
2060 }
2061 
2062 static int
2063 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2064     int infd, avl_tree_t *stream_avl, char **top_zfs)
2065 {
2066 	int err;
2067 	dmu_replay_record_t drr, drr_noswap;
2068 	struct drr_begin *drrb = &drr.drr_u.drr_begin;
2069 	char errbuf[1024];
2070 	zio_cksum_t zcksum = { 0 };
2071 
2072 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2073 	    "cannot receive"));
2074 
2075 	if (flags.isprefix &&
2076 	    !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
2077 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
2078 		    "(%s) does not exist"), tosnap);
2079 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
2080 	}
2081 
2082 	/* read in the BEGIN record */
2083 	if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
2084 	    &zcksum)))
2085 		return (err);
2086 
2087 	if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
2088 		/* It's the double end record at the end of a package */
2089 		return (ENODATA);
2090 	}
2091 
2092 	/* the kernel needs the non-byteswapped begin record */
2093 	drr_noswap = drr;
2094 
2095 	flags.byteswap = B_FALSE;
2096 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
2097 		/*
2098 		 * We computed the checksum in the wrong byteorder in
2099 		 * recv_read() above; do it again correctly.
2100 		 */
2101 		bzero(&zcksum, sizeof (zio_cksum_t));
2102 		fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
2103 		flags.byteswap = B_TRUE;
2104 
2105 		drr.drr_type = BSWAP_32(drr.drr_type);
2106 		drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
2107 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
2108 		drrb->drr_version = BSWAP_64(drrb->drr_version);
2109 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
2110 		drrb->drr_type = BSWAP_32(drrb->drr_type);
2111 		drrb->drr_flags = BSWAP_32(drrb->drr_flags);
2112 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
2113 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
2114 	}
2115 
2116 	if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
2117 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2118 		    "stream (bad magic number)"));
2119 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2120 	}
2121 
2122 	if (strchr(drrb->drr_toname, '@') == NULL) {
2123 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2124 		    "stream (bad snapshot name)"));
2125 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2126 	}
2127 
2128 	if (drrb->drr_version == DMU_BACKUP_STREAM_VERSION) {
2129 		return (zfs_receive_one(hdl, infd, tosnap, flags,
2130 		    &drr, &drr_noswap, stream_avl, top_zfs));
2131 	} else if (drrb->drr_version == DMU_BACKUP_HEADER_VERSION) {
2132 		return (zfs_receive_package(hdl, infd, tosnap, flags,
2133 		    &drr, &zcksum, top_zfs));
2134 	} else {
2135 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2136 		    "stream is unsupported version %llu"),
2137 		    drrb->drr_version);
2138 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
2139 	}
2140 }
2141 
2142 /*
2143  * Restores a backup of tosnap from the file descriptor specified by infd.
2144  * Return 0 on total success, -2 if some things couldn't be
2145  * destroyed/renamed/promoted, -1 if some things couldn't be received.
2146  * (-1 will override -2).
2147  */
2148 int
2149 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t flags,
2150     int infd, avl_tree_t *stream_avl)
2151 {
2152 	char *top_zfs = NULL;
2153 	int err;
2154 
2155 	err = zfs_receive_impl(hdl, tosnap, flags, infd, stream_avl, &top_zfs);
2156 
2157 	if (err == 0 && !flags.nomount && top_zfs) {
2158 		zfs_handle_t *zhp;
2159 		prop_changelist_t *clp;
2160 
2161 		zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
2162 		if (zhp != NULL) {
2163 			clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
2164 			    CL_GATHER_MOUNT_ALWAYS, 0);
2165 			zfs_close(zhp);
2166 			if (clp != NULL) {
2167 				/* mount and share received datasets */
2168 				err = changelist_postfix(clp);
2169 				changelist_free(clp);
2170 			}
2171 		}
2172 		if (zhp == NULL || clp == NULL || err)
2173 			err = -1;
2174 	}
2175 	if (top_zfs)
2176 		free(top_zfs);
2177 
2178 	return (err);
2179 }
2180