xref: /illumos-gate/usr/src/lib/libzfs/common/libzfs_sendrecv.c (revision 1bcef0f3e9dee4e6f3c617d8a92ac97c0ea47fb5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25  * Copyright 2019 Joyent, Inc.
26  * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
27  * Copyright (c) 2013 Steven Hartland. All rights reserved.
28  * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
29  * Copyright (c) 2014 Integros [integros.com]
30  * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
31  * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
32  * Copyright (c) 2018 Datto Inc.
33  */
34 
35 #include <assert.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <libintl.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <strings.h>
42 #include <unistd.h>
43 #include <stddef.h>
44 #include <fcntl.h>
45 #include <sys/mount.h>
46 #include <pthread.h>
47 #include <umem.h>
48 #include <time.h>
49 
50 #include <libzfs.h>
51 #include <libzfs_core.h>
52 #include <libzutil.h>
53 
54 #include "zfs_namecheck.h"
55 #include "zfs_prop.h"
56 #include "zfs_fletcher.h"
57 #include "libzfs_impl.h"
58 #include <zlib.h>
59 #include <sha2.h>
60 #include <sys/zio_checksum.h>
61 #include <sys/dsl_crypt.h>
62 #include <sys/ddt.h>
63 
64 /* in libzfs_dataset.c */
65 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
66 
67 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
68     recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
69     uint64_t *, const char *, nvlist_t *);
70 static int guid_to_name(libzfs_handle_t *, const char *,
71     uint64_t, boolean_t, char *);
72 
73 static const zio_cksum_t zero_cksum = { 0 };
74 
75 typedef struct dedup_arg {
76 	int	inputfd;
77 	int	outputfd;
78 	libzfs_handle_t  *dedup_hdl;
79 } dedup_arg_t;
80 
81 typedef struct progress_arg {
82 	zfs_handle_t *pa_zhp;
83 	int pa_fd;
84 	boolean_t pa_parsable;
85 } progress_arg_t;
86 
87 typedef struct dataref {
88 	uint64_t ref_guid;
89 	uint64_t ref_object;
90 	uint64_t ref_offset;
91 } dataref_t;
92 
93 typedef struct dedup_entry {
94 	struct dedup_entry	*dde_next;
95 	zio_cksum_t dde_chksum;
96 	uint64_t dde_prop;
97 	dataref_t dde_ref;
98 } dedup_entry_t;
99 
100 #define	MAX_DDT_PHYSMEM_PERCENT		20
101 #define	SMALLEST_POSSIBLE_MAX_DDT_MB		128
102 
103 typedef struct dedup_table {
104 	dedup_entry_t	**dedup_hash_array;
105 	umem_cache_t	*ddecache;
106 	uint64_t	max_ddt_size;  /* max dedup table size in bytes */
107 	uint64_t	cur_ddt_size;  /* current dedup table size in bytes */
108 	uint64_t	ddt_count;
109 	int		numhashbits;
110 	boolean_t	ddt_full;
111 } dedup_table_t;
112 
113 static int
high_order_bit(uint64_t n)114 high_order_bit(uint64_t n)
115 {
116 	int count;
117 
118 	for (count = 0; n != 0; count++)
119 		n >>= 1;
120 	return (count);
121 }
122 
123 static size_t
ssread(void * buf,size_t len,FILE * stream)124 ssread(void *buf, size_t len, FILE *stream)
125 {
126 	size_t outlen;
127 
128 	if ((outlen = fread(buf, len, 1, stream)) == 0)
129 		return (0);
130 
131 	return (outlen);
132 }
133 
134 static void
ddt_hash_append(libzfs_handle_t * hdl,dedup_table_t * ddt,dedup_entry_t ** ddepp,zio_cksum_t * cs,uint64_t prop,dataref_t * dr)135 ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
136     zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
137 {
138 	dedup_entry_t	*dde;
139 
140 	if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
141 		if (ddt->ddt_full == B_FALSE) {
142 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
143 			    "Dedup table full.  Deduplication will continue "
144 			    "with existing table entries"));
145 			ddt->ddt_full = B_TRUE;
146 		}
147 		return;
148 	}
149 
150 	if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
151 	    != NULL) {
152 		assert(*ddepp == NULL);
153 		dde->dde_next = NULL;
154 		dde->dde_chksum = *cs;
155 		dde->dde_prop = prop;
156 		dde->dde_ref = *dr;
157 		*ddepp = dde;
158 		ddt->cur_ddt_size += sizeof (dedup_entry_t);
159 		ddt->ddt_count++;
160 	}
161 }
162 
163 /*
164  * Using the specified dedup table, do a lookup for an entry with
165  * the checksum cs.  If found, return the block's reference info
166  * in *dr. Otherwise, insert a new entry in the dedup table, using
167  * the reference information specified by *dr.
168  *
169  * return value:  true - entry was found
170  *		  false - entry was not found
171  */
172 static boolean_t
ddt_update(libzfs_handle_t * hdl,dedup_table_t * ddt,zio_cksum_t * cs,uint64_t prop,dataref_t * dr)173 ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
174     uint64_t prop, dataref_t *dr)
175 {
176 	uint32_t hashcode;
177 	dedup_entry_t **ddepp;
178 
179 	hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
180 
181 	for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
182 	    ddepp = &((*ddepp)->dde_next)) {
183 		if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
184 		    (*ddepp)->dde_prop == prop) {
185 			*dr = (*ddepp)->dde_ref;
186 			return (B_TRUE);
187 		}
188 	}
189 	ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
190 	return (B_FALSE);
191 }
192 
193 static int
dump_record(dmu_replay_record_t * drr,void * payload,int payload_len,zio_cksum_t * zc,int outfd)194 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
195     zio_cksum_t *zc, int outfd)
196 {
197 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
198 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
199 	(void) fletcher_4_incremental_native(drr,
200 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
201 	if (drr->drr_type != DRR_BEGIN) {
202 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
203 		    drr_checksum.drr_checksum));
204 		drr->drr_u.drr_checksum.drr_checksum = *zc;
205 	}
206 	(void) fletcher_4_incremental_native(
207 	    &drr->drr_u.drr_checksum.drr_checksum, sizeof (zio_cksum_t), zc);
208 	if (write(outfd, drr, sizeof (*drr)) == -1)
209 		return (errno);
210 	if (payload_len != 0) {
211 		(void) fletcher_4_incremental_native(payload, payload_len, zc);
212 		if (write(outfd, payload, payload_len) == -1)
213 			return (errno);
214 	}
215 	return (0);
216 }
217 
218 /*
219  * This function is started in a separate thread when the dedup option
220  * has been requested.  The main send thread determines the list of
221  * snapshots to be included in the send stream and makes the ioctl calls
222  * for each one.  But instead of having the ioctl send the output to the
223  * the output fd specified by the caller of zfs_send()), the
224  * ioctl is told to direct the output to a pipe, which is read by the
225  * alternate thread running THIS function.  This function does the
226  * dedup'ing by:
227  *  1. building a dedup table (the DDT)
228  *  2. doing checksums on each data block and inserting a record in the DDT
229  *  3. looking for matching checksums, and
230  *  4.  sending a DRR_WRITE_BYREF record instead of a write record whenever
231  *      a duplicate block is found.
232  * The output of this function then goes to the output fd requested
233  * by the caller of zfs_send().
234  */
235 static void *
cksummer(void * arg)236 cksummer(void *arg)
237 {
238 	dedup_arg_t *dda = arg;
239 	char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
240 	dmu_replay_record_t thedrr;
241 	dmu_replay_record_t *drr = &thedrr;
242 	FILE *ofp;
243 	int outfd;
244 	dedup_table_t ddt;
245 	zio_cksum_t stream_cksum;
246 	uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
247 	uint64_t numbuckets;
248 
249 	ddt.max_ddt_size =
250 	    MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
251 	    SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
252 
253 	numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
254 
255 	/*
256 	 * numbuckets must be a power of 2.  Increase number to
257 	 * a power of 2 if necessary.
258 	 */
259 	if (!ISP2(numbuckets))
260 		numbuckets = 1 << high_order_bit(numbuckets);
261 
262 	ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
263 	ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
264 	    NULL, NULL, NULL, NULL, NULL, 0);
265 	ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
266 	ddt.numhashbits = high_order_bit(numbuckets) - 1;
267 	ddt.ddt_full = B_FALSE;
268 
269 	outfd = dda->outputfd;
270 	ofp = fdopen(dda->inputfd, "r");
271 	while (ssread(drr, sizeof (*drr), ofp) != 0) {
272 
273 		/*
274 		 * kernel filled in checksum, we are going to write same
275 		 * record, but need to regenerate checksum.
276 		 */
277 		if (drr->drr_type != DRR_BEGIN) {
278 			bzero(&drr->drr_u.drr_checksum.drr_checksum,
279 			    sizeof (drr->drr_u.drr_checksum.drr_checksum));
280 		}
281 
282 		switch (drr->drr_type) {
283 		case DRR_BEGIN:
284 		{
285 			struct drr_begin *drrb = &drr->drr_u.drr_begin;
286 			int fflags;
287 			int sz = 0;
288 			ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
289 
290 			ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
291 
292 			/* set the DEDUP feature flag for this stream */
293 			fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
294 			fflags |= (DMU_BACKUP_FEATURE_DEDUP |
295 			    DMU_BACKUP_FEATURE_DEDUPPROPS);
296 			DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
297 
298 			if (drr->drr_payloadlen != 0) {
299 				sz = drr->drr_payloadlen;
300 
301 				if (sz > SPA_MAXBLOCKSIZE) {
302 					buf = zfs_realloc(dda->dedup_hdl, buf,
303 					    SPA_MAXBLOCKSIZE, sz);
304 				}
305 				(void) ssread(buf, sz, ofp);
306 				if (ferror(stdin))
307 					perror("fread");
308 			}
309 			if (dump_record(drr, buf, sz, &stream_cksum,
310 			    outfd) != 0)
311 				goto out;
312 			break;
313 		}
314 
315 		case DRR_END:
316 		{
317 			struct drr_end *drre = &drr->drr_u.drr_end;
318 			/* use the recalculated checksum */
319 			drre->drr_checksum = stream_cksum;
320 			if (dump_record(drr, NULL, 0, &stream_cksum,
321 			    outfd) != 0)
322 				goto out;
323 			break;
324 		}
325 
326 		case DRR_OBJECT:
327 		{
328 			struct drr_object *drro = &drr->drr_u.drr_object;
329 			if (drro->drr_bonuslen > 0) {
330 				(void) ssread(buf,
331 				    DRR_OBJECT_PAYLOAD_SIZE(drro), ofp);
332 			}
333 			if (dump_record(drr, buf, DRR_OBJECT_PAYLOAD_SIZE(drro),
334 			    &stream_cksum, outfd) != 0)
335 				goto out;
336 			break;
337 		}
338 
339 		case DRR_SPILL:
340 		{
341 			struct drr_spill *drrs = &drr->drr_u.drr_spill;
342 			(void) ssread(buf, DRR_SPILL_PAYLOAD_SIZE(drrs), ofp);
343 			if (dump_record(drr, buf, DRR_SPILL_PAYLOAD_SIZE(drrs),
344 			    &stream_cksum, outfd) != 0)
345 				goto out;
346 			break;
347 		}
348 
349 		case DRR_FREEOBJECTS:
350 		{
351 			if (dump_record(drr, NULL, 0, &stream_cksum,
352 			    outfd) != 0)
353 				goto out;
354 			break;
355 		}
356 
357 		case DRR_WRITE:
358 		{
359 			struct drr_write *drrw = &drr->drr_u.drr_write;
360 			dataref_t	dataref;
361 			uint64_t	payload_size;
362 
363 			payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
364 			(void) ssread(buf, payload_size, ofp);
365 
366 			/*
367 			 * Use the existing checksum if it's dedup-capable,
368 			 * else calculate a SHA256 checksum for it.
369 			 */
370 
371 			if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
372 			    zero_cksum) ||
373 			    !DRR_IS_DEDUP_CAPABLE(drrw->drr_flags)) {
374 				SHA256_CTX	ctx;
375 				zio_cksum_t	tmpsha256;
376 
377 				SHA256Init(&ctx);
378 				SHA256Update(&ctx, buf, payload_size);
379 				SHA256Final(&tmpsha256, &ctx);
380 				drrw->drr_key.ddk_cksum.zc_word[0] =
381 				    BE_64(tmpsha256.zc_word[0]);
382 				drrw->drr_key.ddk_cksum.zc_word[1] =
383 				    BE_64(tmpsha256.zc_word[1]);
384 				drrw->drr_key.ddk_cksum.zc_word[2] =
385 				    BE_64(tmpsha256.zc_word[2]);
386 				drrw->drr_key.ddk_cksum.zc_word[3] =
387 				    BE_64(tmpsha256.zc_word[3]);
388 				drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
389 				drrw->drr_flags |= DRR_CHECKSUM_DEDUP;
390 			}
391 
392 			dataref.ref_guid = drrw->drr_toguid;
393 			dataref.ref_object = drrw->drr_object;
394 			dataref.ref_offset = drrw->drr_offset;
395 
396 			if (ddt_update(dda->dedup_hdl, &ddt,
397 			    &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
398 			    &dataref)) {
399 				dmu_replay_record_t wbr_drr = {0};
400 				struct drr_write_byref *wbr_drrr =
401 				    &wbr_drr.drr_u.drr_write_byref;
402 
403 				/* block already present in stream */
404 				wbr_drr.drr_type = DRR_WRITE_BYREF;
405 
406 				wbr_drrr->drr_object = drrw->drr_object;
407 				wbr_drrr->drr_offset = drrw->drr_offset;
408 				wbr_drrr->drr_length = drrw->drr_logical_size;
409 				wbr_drrr->drr_toguid = drrw->drr_toguid;
410 				wbr_drrr->drr_refguid = dataref.ref_guid;
411 				wbr_drrr->drr_refobject =
412 				    dataref.ref_object;
413 				wbr_drrr->drr_refoffset =
414 				    dataref.ref_offset;
415 
416 				wbr_drrr->drr_checksumtype =
417 				    drrw->drr_checksumtype;
418 				wbr_drrr->drr_flags = drrw->drr_flags;
419 				wbr_drrr->drr_key.ddk_cksum =
420 				    drrw->drr_key.ddk_cksum;
421 				wbr_drrr->drr_key.ddk_prop =
422 				    drrw->drr_key.ddk_prop;
423 
424 				if (dump_record(&wbr_drr, NULL, 0,
425 				    &stream_cksum, outfd) != 0)
426 					goto out;
427 			} else {
428 				/* block not previously seen */
429 				if (dump_record(drr, buf, payload_size,
430 				    &stream_cksum, outfd) != 0)
431 					goto out;
432 			}
433 			break;
434 		}
435 
436 		case DRR_WRITE_EMBEDDED:
437 		{
438 			struct drr_write_embedded *drrwe =
439 			    &drr->drr_u.drr_write_embedded;
440 			(void) ssread(buf,
441 			    P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
442 			if (dump_record(drr, buf,
443 			    P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
444 			    &stream_cksum, outfd) != 0)
445 				goto out;
446 			break;
447 		}
448 
449 		case DRR_FREE:
450 		{
451 			if (dump_record(drr, NULL, 0, &stream_cksum,
452 			    outfd) != 0)
453 				goto out;
454 			break;
455 		}
456 
457 		case DRR_OBJECT_RANGE:
458 		{
459 			if (dump_record(drr, NULL, 0, &stream_cksum,
460 			    outfd) != 0)
461 				goto out;
462 			break;
463 		}
464 
465 		default:
466 			(void) fprintf(stderr, "INVALID record type 0x%x\n",
467 			    drr->drr_type);
468 			/* should never happen, so assert */
469 			assert(B_FALSE);
470 		}
471 	}
472 out:
473 	umem_cache_destroy(ddt.ddecache);
474 	free(ddt.dedup_hash_array);
475 	free(buf);
476 	(void) fclose(ofp);
477 
478 	return (NULL);
479 }
480 
481 /*
482  * Routines for dealing with the AVL tree of fs-nvlists
483  */
484 typedef struct fsavl_node {
485 	avl_node_t fn_node;
486 	nvlist_t *fn_nvfs;
487 	char *fn_snapname;
488 	uint64_t fn_guid;
489 } fsavl_node_t;
490 
491 static int
fsavl_compare(const void * arg1,const void * arg2)492 fsavl_compare(const void *arg1, const void *arg2)
493 {
494 	const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
495 	const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
496 
497 	if (fn1->fn_guid > fn2->fn_guid)
498 		return (+1);
499 	if (fn1->fn_guid < fn2->fn_guid)
500 		return (-1);
501 	return (0);
502 }
503 
504 /*
505  * Given the GUID of a snapshot, find its containing filesystem and
506  * (optionally) name.
507  */
508 static nvlist_t *
fsavl_find(avl_tree_t * avl,uint64_t snapguid,char ** snapname)509 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
510 {
511 	fsavl_node_t fn_find;
512 	fsavl_node_t *fn;
513 
514 	fn_find.fn_guid = snapguid;
515 
516 	fn = avl_find(avl, &fn_find, NULL);
517 	if (fn) {
518 		if (snapname)
519 			*snapname = fn->fn_snapname;
520 		return (fn->fn_nvfs);
521 	}
522 	return (NULL);
523 }
524 
525 static void
fsavl_destroy(avl_tree_t * avl)526 fsavl_destroy(avl_tree_t *avl)
527 {
528 	fsavl_node_t *fn;
529 	void *cookie;
530 
531 	if (avl == NULL)
532 		return;
533 
534 	cookie = NULL;
535 	while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
536 		free(fn);
537 	avl_destroy(avl);
538 	free(avl);
539 }
540 
541 /*
542  * Given an nvlist, produce an avl tree of snapshots, ordered by guid
543  */
544 static avl_tree_t *
fsavl_create(nvlist_t * fss)545 fsavl_create(nvlist_t *fss)
546 {
547 	avl_tree_t *fsavl;
548 	nvpair_t *fselem = NULL;
549 
550 	if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
551 		return (NULL);
552 
553 	avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
554 	    offsetof(fsavl_node_t, fn_node));
555 
556 	while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
557 		nvlist_t *nvfs, *snaps;
558 		nvpair_t *snapelem = NULL;
559 
560 		VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
561 		VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
562 
563 		while ((snapelem =
564 		    nvlist_next_nvpair(snaps, snapelem)) != NULL) {
565 			fsavl_node_t *fn;
566 			uint64_t guid;
567 
568 			VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
569 			if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
570 				fsavl_destroy(fsavl);
571 				return (NULL);
572 			}
573 			fn->fn_nvfs = nvfs;
574 			fn->fn_snapname = nvpair_name(snapelem);
575 			fn->fn_guid = guid;
576 
577 			/*
578 			 * Note: if there are multiple snaps with the
579 			 * same GUID, we ignore all but one.
580 			 */
581 			if (avl_find(fsavl, fn, NULL) == NULL)
582 				avl_add(fsavl, fn);
583 			else
584 				free(fn);
585 		}
586 	}
587 
588 	return (fsavl);
589 }
590 
591 /*
592  * Routines for dealing with the giant nvlist of fs-nvlists, etc.
593  */
594 typedef struct send_data {
595 	/*
596 	 * assigned inside every recursive call,
597 	 * restored from *_save on return:
598 	 *
599 	 * guid of fromsnap snapshot in parent dataset
600 	 * txg of fromsnap snapshot in current dataset
601 	 * txg of tosnap snapshot in current dataset
602 	 */
603 
604 	uint64_t parent_fromsnap_guid;
605 	uint64_t fromsnap_txg;
606 	uint64_t tosnap_txg;
607 
608 	/* the nvlists get accumulated during depth-first traversal */
609 	nvlist_t *parent_snaps;
610 	nvlist_t *fss;
611 	nvlist_t *snapprops;
612 	nvlist_t *snapholds;	/* user holds */
613 
614 	/* send-receive configuration, does not change during traversal */
615 	const char *fsname;
616 	const char *fromsnap;
617 	const char *tosnap;
618 	boolean_t recursive;
619 	boolean_t raw;
620 	boolean_t verbose;
621 	boolean_t backup;
622 	boolean_t holds;	/* were holds requested with send -h */
623 	boolean_t props;
624 
625 	/*
626 	 * The header nvlist is of the following format:
627 	 * {
628 	 *   "tosnap" -> string
629 	 *   "fromsnap" -> string (if incremental)
630 	 *   "fss" -> {
631 	 *	id -> {
632 	 *
633 	 *	 "name" -> string (full name; for debugging)
634 	 *	 "parentfromsnap" -> number (guid of fromsnap in parent)
635 	 *
636 	 *	 "props" -> { name -> value (only if set here) }
637 	 *	 "snaps" -> { name (lastname) -> number (guid) }
638 	 *	 "snapprops" -> { name (lastname) -> { name -> value } }
639 	 *	 "snapholds" -> { name (lastname) -> { holdname -> crtime } }
640 	 *
641 	 *	 "origin" -> number (guid) (if clone)
642 	 *	 "is_encroot" -> boolean
643 	 *	 "sent" -> boolean (not on-disk)
644 	 *	}
645 	 *   }
646 	 * }
647 	 *
648 	 */
649 } send_data_t;
650 
651 static void
652 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
653 
654 static int
send_iterate_snap(zfs_handle_t * zhp,void * arg)655 send_iterate_snap(zfs_handle_t *zhp, void *arg)
656 {
657 	send_data_t *sd = arg;
658 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
659 	uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
660 	char *snapname;
661 	nvlist_t *nv;
662 
663 	snapname = strrchr(zhp->zfs_name, '@')+1;
664 
665 	if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
666 		if (sd->verbose) {
667 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
668 			    "skipping snapshot %s because it was created "
669 			    "after the destination snapshot (%s)\n"),
670 			    zhp->zfs_name, sd->tosnap);
671 		}
672 		zfs_close(zhp);
673 		return (0);
674 	}
675 
676 	VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
677 	/*
678 	 * NB: if there is no fromsnap here (it's a newly created fs in
679 	 * an incremental replication), we will substitute the tosnap.
680 	 */
681 	if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
682 	    (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
683 	    strcmp(snapname, sd->tosnap) == 0)) {
684 		sd->parent_fromsnap_guid = guid;
685 	}
686 
687 	VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
688 	send_iterate_prop(zhp, sd->backup, nv);
689 	VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
690 	nvlist_free(nv);
691 	if (sd->holds) {
692 		nvlist_t *holds = fnvlist_alloc();
693 		int err = lzc_get_holds(zhp->zfs_name, &holds);
694 		if (err == 0) {
695 			VERIFY(0 == nvlist_add_nvlist(sd->snapholds,
696 			    snapname, holds));
697 		}
698 		fnvlist_free(holds);
699 	}
700 
701 	zfs_close(zhp);
702 	return (0);
703 }
704 
705 static void
send_iterate_prop(zfs_handle_t * zhp,boolean_t received_only,nvlist_t * nv)706 send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
707 {
708 	nvlist_t *props = NULL;
709 	nvpair_t *elem = NULL;
710 
711 	if (received_only)
712 		props = zfs_get_recvd_props(zhp);
713 	else
714 		props = zhp->zfs_props;
715 
716 	while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
717 		char *propname = nvpair_name(elem);
718 		zfs_prop_t prop = zfs_name_to_prop(propname);
719 		nvlist_t *propnv;
720 
721 		if (!zfs_prop_user(propname)) {
722 			/*
723 			 * Realistically, this should never happen.  However,
724 			 * we want the ability to add DSL properties without
725 			 * needing to make incompatible version changes.  We
726 			 * need to ignore unknown properties to allow older
727 			 * software to still send datasets containing these
728 			 * properties, with the unknown properties elided.
729 			 */
730 			if (prop == ZPROP_INVAL)
731 				continue;
732 
733 			if (zfs_prop_readonly(prop))
734 				continue;
735 		}
736 
737 		verify(nvpair_value_nvlist(elem, &propnv) == 0);
738 		if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
739 		    prop == ZFS_PROP_REFQUOTA ||
740 		    prop == ZFS_PROP_REFRESERVATION) {
741 			char *source;
742 			uint64_t value;
743 			verify(nvlist_lookup_uint64(propnv,
744 			    ZPROP_VALUE, &value) == 0);
745 			if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
746 				continue;
747 			/*
748 			 * May have no source before SPA_VERSION_RECVD_PROPS,
749 			 * but is still modifiable.
750 			 */
751 			if (nvlist_lookup_string(propnv,
752 			    ZPROP_SOURCE, &source) == 0) {
753 				if ((strcmp(source, zhp->zfs_name) != 0) &&
754 				    (strcmp(source,
755 				    ZPROP_SOURCE_VAL_RECVD) != 0))
756 					continue;
757 			}
758 		} else {
759 			char *source;
760 			if (nvlist_lookup_string(propnv,
761 			    ZPROP_SOURCE, &source) != 0)
762 				continue;
763 			if ((strcmp(source, zhp->zfs_name) != 0) &&
764 			    (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
765 				continue;
766 		}
767 
768 		if (zfs_prop_user(propname) ||
769 		    zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
770 			char *value;
771 			verify(nvlist_lookup_string(propnv,
772 			    ZPROP_VALUE, &value) == 0);
773 			VERIFY(0 == nvlist_add_string(nv, propname, value));
774 		} else {
775 			uint64_t value;
776 			verify(nvlist_lookup_uint64(propnv,
777 			    ZPROP_VALUE, &value) == 0);
778 			VERIFY(0 == nvlist_add_uint64(nv, propname, value));
779 		}
780 	}
781 }
782 
783 /*
784  * returns snapshot creation txg
785  * and returns 0 if the snapshot does not exist
786  */
787 static uint64_t
get_snap_txg(libzfs_handle_t * hdl,const char * fs,const char * snap)788 get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
789 {
790 	char name[ZFS_MAX_DATASET_NAME_LEN];
791 	uint64_t txg = 0;
792 
793 	if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
794 		return (txg);
795 
796 	(void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
797 	if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
798 		zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
799 		if (zhp != NULL) {
800 			txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
801 			zfs_close(zhp);
802 		}
803 	}
804 
805 	return (txg);
806 }
807 
808 /*
809  * recursively generate nvlists describing datasets.  See comment
810  * for the data structure send_data_t above for description of contents
811  * of the nvlist.
812  */
813 static int
send_iterate_fs(zfs_handle_t * zhp,void * arg)814 send_iterate_fs(zfs_handle_t *zhp, void *arg)
815 {
816 	send_data_t *sd = arg;
817 	nvlist_t *nvfs = NULL, *nv = NULL;
818 	int rv = 0;
819 	uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
820 	uint64_t fromsnap_txg_save = sd->fromsnap_txg;
821 	uint64_t tosnap_txg_save = sd->tosnap_txg;
822 	uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
823 	uint64_t guid = zhp->zfs_dmustats.dds_guid;
824 	uint64_t fromsnap_txg, tosnap_txg;
825 	char guidstring[64];
826 
827 	fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
828 	if (fromsnap_txg != 0)
829 		sd->fromsnap_txg = fromsnap_txg;
830 
831 	tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
832 	if (tosnap_txg != 0)
833 		sd->tosnap_txg = tosnap_txg;
834 
835 	/*
836 	 * on the send side, if the current dataset does not have tosnap,
837 	 * perform two additional checks:
838 	 *
839 	 * - skip sending the current dataset if it was created later than
840 	 *   the parent tosnap
841 	 * - return error if the current dataset was created earlier than
842 	 *   the parent tosnap
843 	 */
844 	if (sd->tosnap != NULL && tosnap_txg == 0) {
845 		if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
846 			if (sd->verbose) {
847 				(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
848 				    "skipping dataset %s: snapshot %s does "
849 				    "not exist\n"), zhp->zfs_name, sd->tosnap);
850 			}
851 		} else {
852 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
853 			    "cannot send %s@%s%s: snapshot %s@%s does not "
854 			    "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
855 			    dgettext(TEXT_DOMAIN, " recursively") : "",
856 			    zhp->zfs_name, sd->tosnap);
857 			rv = -1;
858 		}
859 		goto out;
860 	}
861 
862 	VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
863 	VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
864 	VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
865 	    sd->parent_fromsnap_guid));
866 
867 	if (zhp->zfs_dmustats.dds_origin[0]) {
868 		zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
869 		    zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
870 		if (origin == NULL) {
871 			rv = -1;
872 			goto out;
873 		}
874 		VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
875 		    origin->zfs_dmustats.dds_guid));
876 	}
877 
878 	/* iterate over props */
879 	if (sd->props || sd->backup || sd->recursive) {
880 		VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
881 		send_iterate_prop(zhp, sd->backup, nv);
882 	}
883 
884 	if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
885 		boolean_t encroot;
886 
887 		/* determine if this dataset is an encryption root */
888 		if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
889 			rv = -1;
890 			goto out;
891 		}
892 
893 		if (encroot)
894 			VERIFY(0 == nvlist_add_boolean(nvfs, "is_encroot"));
895 
896 		/*
897 		 * Encrypted datasets can only be sent with properties if
898 		 * the raw flag is specified because the receive side doesn't
899 		 * currently have a mechanism for recursively asking the user
900 		 * for new encryption parameters.
901 		 */
902 		if (!sd->raw) {
903 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
904 			    "cannot send %s@%s: encrypted dataset %s may not "
905 			    "be sent with properties without the raw flag\n"),
906 			    sd->fsname, sd->tosnap, zhp->zfs_name);
907 			rv = -1;
908 			goto out;
909 		}
910 
911 	}
912 
913 	if (nv != NULL)
914 		VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
915 
916 	/* iterate over snaps, and set sd->parent_fromsnap_guid */
917 	sd->parent_fromsnap_guid = 0;
918 	VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
919 	VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
920 	if (sd->holds)
921 		VERIFY(0 == nvlist_alloc(&sd->snapholds, NV_UNIQUE_NAME, 0));
922 	(void) zfs_iter_snapshots(zhp, B_FALSE, send_iterate_snap, sd);
923 	VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
924 	VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
925 	if (sd->holds)
926 		VERIFY(0 == nvlist_add_nvlist(nvfs, "snapholds",
927 		    sd->snapholds));
928 	nvlist_free(sd->parent_snaps);
929 	nvlist_free(sd->snapprops);
930 	nvlist_free(sd->snapholds);
931 
932 	/* Do not allow the size of the properties list to exceed the limit */
933 	if ((fnvlist_size(nvfs) + fnvlist_size(sd->fss)) >
934 	    zhp->zfs_hdl->libzfs_max_nvlist) {
935 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
936 		    "warning: cannot send %s@%s: the size of the list of "
937 		    "snapshots and properties is too large to be received "
938 		    "successfully.\n"
939 		    "Select a smaller number of snapshots to send.\n"),
940 		    zhp->zfs_name, sd->tosnap);
941 		rv = EZFS_NOSPC;
942 		goto out;
943 	}
944 	/* add this fs to nvlist */
945 	(void) snprintf(guidstring, sizeof (guidstring),
946 	    "0x%llx", (longlong_t)guid);
947 	VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
948 
949 	/* iterate over children */
950 	if (sd->recursive)
951 		rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
952 
953 out:
954 	sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
955 	sd->fromsnap_txg = fromsnap_txg_save;
956 	sd->tosnap_txg = tosnap_txg_save;
957 	nvlist_free(nv);
958 	nvlist_free(nvfs);
959 
960 	zfs_close(zhp);
961 	return (rv);
962 }
963 
964 static int
gather_nvlist(libzfs_handle_t * hdl,const char * fsname,const char * fromsnap,const char * tosnap,boolean_t recursive,boolean_t raw,boolean_t verbose,boolean_t backup,boolean_t holds,boolean_t props,nvlist_t ** nvlp,avl_tree_t ** avlp)965 gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
966     const char *tosnap, boolean_t recursive, boolean_t raw,
967     boolean_t verbose, boolean_t backup, boolean_t holds,
968     boolean_t props, nvlist_t **nvlp, avl_tree_t **avlp)
969 {
970 	zfs_handle_t *zhp;
971 	send_data_t sd = { 0 };
972 	int error;
973 
974 	zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
975 	if (zhp == NULL)
976 		return (EZFS_BADTYPE);
977 
978 	VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
979 	sd.fsname = fsname;
980 	sd.fromsnap = fromsnap;
981 	sd.tosnap = tosnap;
982 	sd.recursive = recursive;
983 	sd.raw = raw;
984 	sd.verbose = verbose;
985 	sd.backup = backup;
986 	sd.holds = holds;
987 	sd.props = props;
988 
989 	if ((error = send_iterate_fs(zhp, &sd)) != 0) {
990 		nvlist_free(sd.fss);
991 		if (avlp != NULL)
992 			*avlp = NULL;
993 		*nvlp = NULL;
994 		return (error);
995 	}
996 
997 	if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
998 		nvlist_free(sd.fss);
999 		*nvlp = NULL;
1000 		return (EZFS_NOMEM);
1001 	}
1002 
1003 	*nvlp = sd.fss;
1004 	return (0);
1005 }
1006 
1007 /*
1008  * Routines specific to "zfs send"
1009  */
1010 typedef struct send_dump_data {
1011 	/* these are all just the short snapname (the part after the @) */
1012 	const char *fromsnap;
1013 	const char *tosnap;
1014 	char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
1015 	uint64_t prevsnap_obj;
1016 	boolean_t seenfrom, seento, replicate, doall, fromorigin;
1017 	boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
1018 	boolean_t large_block, compress, raw, holds;
1019 	int outfd;
1020 	boolean_t err;
1021 	nvlist_t *fss;
1022 	nvlist_t *snapholds;
1023 	avl_tree_t *fsavl;
1024 	snapfilter_cb_t *filter_cb;
1025 	void *filter_cb_arg;
1026 	nvlist_t *debugnv;
1027 	char holdtag[ZFS_MAX_DATASET_NAME_LEN];
1028 	int cleanup_fd;
1029 	uint64_t size;
1030 } send_dump_data_t;
1031 
1032 static int
estimate_ioctl(zfs_handle_t * zhp,uint64_t fromsnap_obj,boolean_t fromorigin,enum lzc_send_flags flags,uint64_t * sizep)1033 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
1034     boolean_t fromorigin, enum lzc_send_flags flags, uint64_t *sizep)
1035 {
1036 	zfs_cmd_t zc = { 0 };
1037 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1038 
1039 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1040 	assert(fromsnap_obj == 0 || !fromorigin);
1041 
1042 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1043 	zc.zc_obj = fromorigin;
1044 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1045 	zc.zc_fromobj = fromsnap_obj;
1046 	zc.zc_guid = 1;  /* estimate flag */
1047 	zc.zc_flags = flags;
1048 
1049 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1050 		char errbuf[1024];
1051 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1052 		    "warning: cannot estimate space for '%s'"), zhp->zfs_name);
1053 
1054 		switch (errno) {
1055 		case EXDEV:
1056 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1057 			    "not an earlier snapshot from the same fs"));
1058 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1059 
1060 		case EACCES:
1061 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062 			    "source key must be loaded"));
1063 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1064 
1065 		case ENOENT:
1066 			if (zfs_dataset_exists(hdl, zc.zc_name,
1067 			    ZFS_TYPE_SNAPSHOT)) {
1068 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1069 				    "incremental source (@%s) does not exist"),
1070 				    zc.zc_value);
1071 			}
1072 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
1073 
1074 		case EDQUOT:
1075 		case EFBIG:
1076 		case EIO:
1077 		case ENOLINK:
1078 		case ENOSPC:
1079 		case ENOSTR:
1080 		case ENXIO:
1081 		case EPIPE:
1082 		case ERANGE:
1083 		case EFAULT:
1084 		case EROFS:
1085 			zfs_error_aux(hdl, strerror(errno));
1086 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1087 
1088 		default:
1089 			return (zfs_standard_error(hdl, errno, errbuf));
1090 		}
1091 	}
1092 
1093 	*sizep = zc.zc_objset_type;
1094 
1095 	return (0);
1096 }
1097 
1098 /*
1099  * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1100  * NULL) to the file descriptor specified by outfd.
1101  */
1102 static int
dump_ioctl(zfs_handle_t * zhp,const char * fromsnap,uint64_t fromsnap_obj,boolean_t fromorigin,int outfd,enum lzc_send_flags flags,nvlist_t * debugnv)1103 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
1104     boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
1105     nvlist_t *debugnv)
1106 {
1107 	zfs_cmd_t zc = { 0 };
1108 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1109 	nvlist_t *thisdbg;
1110 
1111 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1112 	assert(fromsnap_obj == 0 || !fromorigin);
1113 
1114 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1115 	zc.zc_cookie = outfd;
1116 	zc.zc_obj = fromorigin;
1117 	zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1118 	zc.zc_fromobj = fromsnap_obj;
1119 	zc.zc_flags = flags;
1120 
1121 	VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
1122 	if (fromsnap && fromsnap[0] != '\0') {
1123 		VERIFY(0 == nvlist_add_string(thisdbg,
1124 		    "fromsnap", fromsnap));
1125 	}
1126 
1127 	if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1128 		char errbuf[1024];
1129 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1130 		    "warning: cannot send '%s'"), zhp->zfs_name);
1131 
1132 		VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
1133 		if (debugnv) {
1134 			VERIFY(0 == nvlist_add_nvlist(debugnv,
1135 			    zhp->zfs_name, thisdbg));
1136 		}
1137 		nvlist_free(thisdbg);
1138 
1139 		switch (errno) {
1140 		case EXDEV:
1141 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1142 			    "not an earlier snapshot from the same fs"));
1143 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
1144 
1145 		case EACCES:
1146 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1147 			    "source key must be loaded"));
1148 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1149 
1150 		case ENOENT:
1151 			if (zfs_dataset_exists(hdl, zc.zc_name,
1152 			    ZFS_TYPE_SNAPSHOT)) {
1153 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1154 				    "incremental source (@%s) does not exist"),
1155 				    zc.zc_value);
1156 			}
1157 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
1158 
1159 		case EDQUOT:
1160 		case EFBIG:
1161 		case EIO:
1162 		case ENOLINK:
1163 		case ENOSPC:
1164 		case ENOSTR:
1165 		case ENXIO:
1166 		case EPIPE:
1167 		case ERANGE:
1168 		case EFAULT:
1169 		case EROFS:
1170 			zfs_error_aux(hdl, strerror(errno));
1171 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1172 
1173 		default:
1174 			return (zfs_standard_error(hdl, errno, errbuf));
1175 		}
1176 	}
1177 
1178 	if (debugnv)
1179 		VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1180 	nvlist_free(thisdbg);
1181 
1182 	return (0);
1183 }
1184 
1185 static void
gather_holds(zfs_handle_t * zhp,send_dump_data_t * sdd)1186 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1187 {
1188 	assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1189 
1190 	/*
1191 	 * zfs_send() only sets snapholds for sends that need them,
1192 	 * e.g. replication and doall.
1193 	 */
1194 	if (sdd->snapholds == NULL)
1195 		return;
1196 
1197 	fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1198 }
1199 
1200 static void *
send_progress_thread(void * arg)1201 send_progress_thread(void *arg)
1202 {
1203 	progress_arg_t *pa = arg;
1204 	zfs_cmd_t zc = { 0 };
1205 	zfs_handle_t *zhp = pa->pa_zhp;
1206 	libzfs_handle_t *hdl = zhp->zfs_hdl;
1207 	unsigned long long bytes;
1208 	char buf[16];
1209 	time_t t;
1210 	struct tm *tm;
1211 
1212 	(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1213 
1214 	if (!pa->pa_parsable)
1215 		(void) fprintf(stderr, "TIME        SENT   SNAPSHOT\n");
1216 
1217 	/*
1218 	 * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
1219 	 */
1220 	for (;;) {
1221 		(void) sleep(1);
1222 
1223 		zc.zc_cookie = pa->pa_fd;
1224 		if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
1225 			return ((void *)-1);
1226 
1227 		(void) time(&t);
1228 		tm = localtime(&t);
1229 		bytes = zc.zc_cookie;
1230 
1231 		if (pa->pa_parsable) {
1232 			(void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
1233 			    tm->tm_hour, tm->tm_min, tm->tm_sec,
1234 			    bytes, zhp->zfs_name);
1235 		} else {
1236 			zfs_nicebytes(bytes, buf, sizeof (buf));
1237 			(void) fprintf(stderr, "%02d:%02d:%02d   %5s   %s\n",
1238 			    tm->tm_hour, tm->tm_min, tm->tm_sec,
1239 			    buf, zhp->zfs_name);
1240 		}
1241 	}
1242 }
1243 
1244 static void
send_print_verbose(FILE * fout,const char * tosnap,const char * fromsnap,uint64_t size,boolean_t parsable)1245 send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
1246     uint64_t size, boolean_t parsable)
1247 {
1248 	if (parsable) {
1249 		if (fromsnap != NULL) {
1250 			(void) fprintf(fout, "incremental\t%s\t%s",
1251 			    fromsnap, tosnap);
1252 		} else {
1253 			(void) fprintf(fout, "full\t%s",
1254 			    tosnap);
1255 		}
1256 	} else {
1257 		if (fromsnap != NULL) {
1258 			if (strchr(fromsnap, '@') == NULL &&
1259 			    strchr(fromsnap, '#') == NULL) {
1260 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1261 				    "send from @%s to %s"),
1262 				    fromsnap, tosnap);
1263 			} else {
1264 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1265 				    "send from %s to %s"),
1266 				    fromsnap, tosnap);
1267 			}
1268 		} else {
1269 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1270 			    "full send of %s"),
1271 			    tosnap);
1272 		}
1273 	}
1274 
1275 	if (size != 0) {
1276 		if (parsable) {
1277 			(void) fprintf(fout, "\t%llu",
1278 			    (longlong_t)size);
1279 		} else {
1280 			char buf[16];
1281 			zfs_nicebytes(size, buf, sizeof (buf));
1282 			(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1283 			    " estimated size is %s"), buf);
1284 		}
1285 	}
1286 	(void) fprintf(fout, "\n");
1287 }
1288 
1289 static int
dump_snapshot(zfs_handle_t * zhp,void * arg)1290 dump_snapshot(zfs_handle_t *zhp, void *arg)
1291 {
1292 	send_dump_data_t *sdd = arg;
1293 	progress_arg_t pa = { 0 };
1294 	pthread_t tid;
1295 	char *thissnap;
1296 	enum lzc_send_flags flags = 0;
1297 	int err;
1298 	boolean_t isfromsnap, istosnap, fromorigin;
1299 	boolean_t exclude = B_FALSE;
1300 	FILE *fout = sdd->std_out ? stdout : stderr;
1301 
1302 	err = 0;
1303 	thissnap = strchr(zhp->zfs_name, '@') + 1;
1304 	isfromsnap = (sdd->fromsnap != NULL &&
1305 	    strcmp(sdd->fromsnap, thissnap) == 0);
1306 
1307 	if (!sdd->seenfrom && isfromsnap) {
1308 		gather_holds(zhp, sdd);
1309 		sdd->seenfrom = B_TRUE;
1310 		(void) strlcpy(sdd->prevsnap, thissnap,
1311 		    sizeof (sdd->prevsnap));
1312 		sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1313 		zfs_close(zhp);
1314 		return (0);
1315 	}
1316 
1317 	if (sdd->seento || !sdd->seenfrom) {
1318 		zfs_close(zhp);
1319 		return (0);
1320 	}
1321 
1322 	istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1323 	if (istosnap)
1324 		sdd->seento = B_TRUE;
1325 
1326 	if (sdd->large_block)
1327 		flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1328 	if (sdd->embed_data)
1329 		flags |= LZC_SEND_FLAG_EMBED_DATA;
1330 	if (sdd->compress)
1331 		flags |= LZC_SEND_FLAG_COMPRESS;
1332 	if (sdd->raw)
1333 		flags |= LZC_SEND_FLAG_RAW;
1334 
1335 	if (!sdd->doall && !isfromsnap && !istosnap) {
1336 		if (sdd->replicate) {
1337 			char *snapname;
1338 			nvlist_t *snapprops;
1339 			/*
1340 			 * Filter out all intermediate snapshots except origin
1341 			 * snapshots needed to replicate clones.
1342 			 */
1343 			nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1344 			    zhp->zfs_dmustats.dds_guid, &snapname);
1345 
1346 			VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1347 			    "snapprops", &snapprops));
1348 			VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1349 			    thissnap, &snapprops));
1350 			exclude = !nvlist_exists(snapprops, "is_clone_origin");
1351 		} else {
1352 			exclude = B_TRUE;
1353 		}
1354 	}
1355 
1356 	/*
1357 	 * If a filter function exists, call it to determine whether
1358 	 * this snapshot will be sent.
1359 	 */
1360 	if (exclude || (sdd->filter_cb != NULL &&
1361 	    sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1362 		/*
1363 		 * This snapshot is filtered out.  Don't send it, and don't
1364 		 * set prevsnap_obj, so it will be as if this snapshot didn't
1365 		 * exist, and the next accepted snapshot will be sent as
1366 		 * an incremental from the last accepted one, or as the
1367 		 * first (and full) snapshot in the case of a replication,
1368 		 * non-incremental send.
1369 		 */
1370 		zfs_close(zhp);
1371 		return (0);
1372 	}
1373 
1374 	gather_holds(zhp, sdd);
1375 	fromorigin = sdd->prevsnap[0] == '\0' &&
1376 	    (sdd->fromorigin || sdd->replicate);
1377 
1378 	if (sdd->verbose) {
1379 		uint64_t size = 0;
1380 		(void) estimate_ioctl(zhp, sdd->prevsnap_obj,
1381 		    fromorigin, flags, &size);
1382 
1383 		send_print_verbose(fout, zhp->zfs_name,
1384 		    sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1385 		    size, sdd->parsable);
1386 		sdd->size += size;
1387 	}
1388 
1389 	if (!sdd->dryrun) {
1390 		/*
1391 		 * If progress reporting is requested, spawn a new thread to
1392 		 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1393 		 */
1394 		if (sdd->progress) {
1395 			pa.pa_zhp = zhp;
1396 			pa.pa_fd = sdd->outfd;
1397 			pa.pa_parsable = sdd->parsable;
1398 
1399 			if ((err = pthread_create(&tid, NULL,
1400 			    send_progress_thread, &pa)) != 0) {
1401 				zfs_close(zhp);
1402 				return (err);
1403 			}
1404 		}
1405 
1406 		err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1407 		    fromorigin, sdd->outfd, flags, sdd->debugnv);
1408 
1409 		if (sdd->progress) {
1410 			(void) pthread_cancel(tid);
1411 			(void) pthread_join(tid, NULL);
1412 		}
1413 	}
1414 
1415 	(void) strcpy(sdd->prevsnap, thissnap);
1416 	sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1417 	zfs_close(zhp);
1418 	return (err);
1419 }
1420 
1421 static int
dump_filesystem(zfs_handle_t * zhp,void * arg)1422 dump_filesystem(zfs_handle_t *zhp, void *arg)
1423 {
1424 	int rv = 0;
1425 	send_dump_data_t *sdd = arg;
1426 	boolean_t missingfrom = B_FALSE;
1427 	zfs_cmd_t zc = { 0 };
1428 
1429 	(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1430 	    zhp->zfs_name, sdd->tosnap);
1431 	if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1432 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1433 		    "WARNING: could not send %s@%s: does not exist\n"),
1434 		    zhp->zfs_name, sdd->tosnap);
1435 		sdd->err = B_TRUE;
1436 		return (0);
1437 	}
1438 
1439 	if (sdd->replicate && sdd->fromsnap) {
1440 		/*
1441 		 * If this fs does not have fromsnap, and we're doing
1442 		 * recursive, we need to send a full stream from the
1443 		 * beginning (or an incremental from the origin if this
1444 		 * is a clone).  If we're doing non-recursive, then let
1445 		 * them get the error.
1446 		 */
1447 		(void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1448 		    zhp->zfs_name, sdd->fromsnap);
1449 		if (ioctl(zhp->zfs_hdl->libzfs_fd,
1450 		    ZFS_IOC_OBJSET_STATS, &zc) != 0) {
1451 			missingfrom = B_TRUE;
1452 		}
1453 	}
1454 
1455 	sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1456 	sdd->prevsnap_obj = 0;
1457 	if (sdd->fromsnap == NULL || missingfrom)
1458 		sdd->seenfrom = B_TRUE;
1459 
1460 	rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1461 	if (!sdd->seenfrom) {
1462 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1463 		    "WARNING: could not send %s@%s:\n"
1464 		    "incremental source (%s@%s) does not exist\n"),
1465 		    zhp->zfs_name, sdd->tosnap,
1466 		    zhp->zfs_name, sdd->fromsnap);
1467 		sdd->err = B_TRUE;
1468 	} else if (!sdd->seento) {
1469 		if (sdd->fromsnap) {
1470 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1471 			    "WARNING: could not send %s@%s:\n"
1472 			    "incremental source (%s@%s) "
1473 			    "is not earlier than it\n"),
1474 			    zhp->zfs_name, sdd->tosnap,
1475 			    zhp->zfs_name, sdd->fromsnap);
1476 		} else {
1477 			(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1478 			    "WARNING: "
1479 			    "could not send %s@%s: does not exist\n"),
1480 			    zhp->zfs_name, sdd->tosnap);
1481 		}
1482 		sdd->err = B_TRUE;
1483 	}
1484 
1485 	return (rv);
1486 }
1487 
1488 static int
dump_filesystems(zfs_handle_t * rzhp,void * arg)1489 dump_filesystems(zfs_handle_t *rzhp, void *arg)
1490 {
1491 	send_dump_data_t *sdd = arg;
1492 	nvpair_t *fspair;
1493 	boolean_t needagain, progress;
1494 
1495 	if (!sdd->replicate)
1496 		return (dump_filesystem(rzhp, sdd));
1497 
1498 	/* Mark the clone origin snapshots. */
1499 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1500 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1501 		nvlist_t *nvfs;
1502 		uint64_t origin_guid = 0;
1503 
1504 		VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1505 		(void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1506 		if (origin_guid != 0) {
1507 			char *snapname;
1508 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1509 			    origin_guid, &snapname);
1510 			if (origin_nv != NULL) {
1511 				nvlist_t *snapprops;
1512 				VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1513 				    "snapprops", &snapprops));
1514 				VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1515 				    snapname, &snapprops));
1516 				VERIFY(0 == nvlist_add_boolean(
1517 				    snapprops, "is_clone_origin"));
1518 			}
1519 		}
1520 	}
1521 again:
1522 	needagain = progress = B_FALSE;
1523 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1524 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1525 		nvlist_t *fslist, *parent_nv;
1526 		char *fsname;
1527 		zfs_handle_t *zhp;
1528 		int err;
1529 		uint64_t origin_guid = 0;
1530 		uint64_t parent_guid = 0;
1531 
1532 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1533 		if (nvlist_lookup_boolean(fslist, "sent") == 0)
1534 			continue;
1535 
1536 		VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
1537 		(void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
1538 		(void) nvlist_lookup_uint64(fslist, "parentfromsnap",
1539 		    &parent_guid);
1540 
1541 		if (parent_guid != 0) {
1542 			parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
1543 			if (!nvlist_exists(parent_nv, "sent")) {
1544 				/* parent has not been sent; skip this one */
1545 				needagain = B_TRUE;
1546 				continue;
1547 			}
1548 		}
1549 
1550 		if (origin_guid != 0) {
1551 			nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1552 			    origin_guid, NULL);
1553 			if (origin_nv != NULL &&
1554 			    !nvlist_exists(origin_nv, "sent")) {
1555 				/*
1556 				 * origin has not been sent yet;
1557 				 * skip this clone.
1558 				 */
1559 				needagain = B_TRUE;
1560 				continue;
1561 			}
1562 		}
1563 
1564 		zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
1565 		if (zhp == NULL)
1566 			return (-1);
1567 		err = dump_filesystem(zhp, sdd);
1568 		VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
1569 		progress = B_TRUE;
1570 		zfs_close(zhp);
1571 		if (err)
1572 			return (err);
1573 	}
1574 	if (needagain) {
1575 		assert(progress);
1576 		goto again;
1577 	}
1578 
1579 	/* clean out the sent flags in case we reuse this fss */
1580 	for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1581 	    fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1582 		nvlist_t *fslist;
1583 
1584 		VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1585 		(void) nvlist_remove_all(fslist, "sent");
1586 	}
1587 
1588 	return (0);
1589 }
1590 
1591 nvlist_t *
zfs_send_resume_token_to_nvlist(libzfs_handle_t * hdl,const char * token)1592 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1593 {
1594 	unsigned int version;
1595 	int nread;
1596 	unsigned long long checksum, packed_len;
1597 
1598 	/*
1599 	 * Decode token header, which is:
1600 	 *   <token version>-<checksum of payload>-<uncompressed payload length>
1601 	 * Note that the only supported token version is 1.
1602 	 */
1603 	nread = sscanf(token, "%u-%llx-%llx-",
1604 	    &version, &checksum, &packed_len);
1605 	if (nread != 3) {
1606 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1607 		    "resume token is corrupt (invalid format)"));
1608 		return (NULL);
1609 	}
1610 
1611 	if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1612 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1613 		    "resume token is corrupt (invalid version %u)"),
1614 		    version);
1615 		return (NULL);
1616 	}
1617 
1618 	/* convert hexadecimal representation to binary */
1619 	token = strrchr(token, '-') + 1;
1620 	int len = strlen(token) / 2;
1621 	unsigned char *compressed = zfs_alloc(hdl, len);
1622 	for (int i = 0; i < len; i++) {
1623 		nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1624 		if (nread != 1) {
1625 			free(compressed);
1626 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1627 			    "resume token is corrupt "
1628 			    "(payload is not hex-encoded)"));
1629 			return (NULL);
1630 		}
1631 	}
1632 
1633 	/* verify checksum */
1634 	zio_cksum_t cksum;
1635 	fletcher_4_native_varsize(compressed, len, &cksum);
1636 	if (cksum.zc_word[0] != checksum) {
1637 		free(compressed);
1638 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1639 		    "resume token is corrupt (incorrect checksum)"));
1640 		return (NULL);
1641 	}
1642 
1643 	/* uncompress */
1644 	void *packed = zfs_alloc(hdl, packed_len);
1645 	uLongf packed_len_long = packed_len;
1646 	if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1647 	    packed_len_long != packed_len) {
1648 		free(packed);
1649 		free(compressed);
1650 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1651 		    "resume token is corrupt (decompression failed)"));
1652 		return (NULL);
1653 	}
1654 
1655 	/* unpack nvlist */
1656 	nvlist_t *nv;
1657 	int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1658 	free(packed);
1659 	free(compressed);
1660 	if (error != 0) {
1661 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1662 		    "resume token is corrupt (nvlist_unpack failed)"));
1663 		return (NULL);
1664 	}
1665 	return (nv);
1666 }
1667 
1668 int
zfs_send_resume(libzfs_handle_t * hdl,sendflags_t * flags,int outfd,const char * resume_token)1669 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1670     const char *resume_token)
1671 {
1672 	char errbuf[1024];
1673 	char *toname;
1674 	char *fromname = NULL;
1675 	uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1676 	zfs_handle_t *zhp;
1677 	int error = 0;
1678 	char name[ZFS_MAX_DATASET_NAME_LEN];
1679 	enum lzc_send_flags lzc_flags = 0;
1680 	FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr;
1681 
1682 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1683 	    "cannot resume send"));
1684 
1685 	nvlist_t *resume_nvl =
1686 	    zfs_send_resume_token_to_nvlist(hdl, resume_token);
1687 	if (resume_nvl == NULL) {
1688 		/*
1689 		 * zfs_error_aux has already been set by
1690 		 * zfs_send_resume_token_to_nvlist
1691 		 */
1692 		return (zfs_error(hdl, EZFS_FAULT, errbuf));
1693 	}
1694 	if (flags->verbose) {
1695 		(void) fprintf(fout, dgettext(TEXT_DOMAIN,
1696 		    "resume token contents:\n"));
1697 		nvlist_print(fout, resume_nvl);
1698 	}
1699 
1700 	if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1701 	    nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1702 	    nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1703 	    nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1704 	    nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1705 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1706 		    "resume token is corrupt"));
1707 		return (zfs_error(hdl, EZFS_FAULT, errbuf));
1708 	}
1709 	fromguid = 0;
1710 	(void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1711 
1712 	if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1713 		lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1714 	if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1715 		lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1716 	if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1717 		lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1718 	if (flags->raw || nvlist_exists(resume_nvl, "rawok"))
1719 		lzc_flags |= LZC_SEND_FLAG_RAW;
1720 
1721 	if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
1722 		if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
1723 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1724 			    "'%s' is no longer the same snapshot used in "
1725 			    "the initial send"), toname);
1726 		} else {
1727 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1728 			    "'%s' used in the initial send no longer exists"),
1729 			    toname);
1730 		}
1731 		return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1732 	}
1733 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
1734 	if (zhp == NULL) {
1735 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1736 		    "unable to access '%s'"), name);
1737 		return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1738 	}
1739 
1740 	if (fromguid != 0) {
1741 		if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1742 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1743 			    "incremental source %#llx no longer exists"),
1744 			    (longlong_t)fromguid);
1745 			return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1746 		}
1747 		fromname = name;
1748 	}
1749 
1750 	if (flags->verbose) {
1751 		uint64_t size = 0;
1752 		error = lzc_send_space(zhp->zfs_name, fromname,
1753 		    lzc_flags, &size);
1754 		if (error == 0)
1755 			size = MAX(0, (int64_t)(size - bytes));
1756 		send_print_verbose(fout, zhp->zfs_name, fromname,
1757 		    size, flags->parsable);
1758 	}
1759 
1760 	if (!flags->dryrun) {
1761 		progress_arg_t pa = { 0 };
1762 		pthread_t tid;
1763 		/*
1764 		 * If progress reporting is requested, spawn a new thread to
1765 		 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1766 		 */
1767 		if (flags->progress) {
1768 			pa.pa_zhp = zhp;
1769 			pa.pa_fd = outfd;
1770 			pa.pa_parsable = flags->parsable;
1771 
1772 			error = pthread_create(&tid, NULL,
1773 			    send_progress_thread, &pa);
1774 			if (error != 0) {
1775 				zfs_close(zhp);
1776 				return (error);
1777 			}
1778 		}
1779 
1780 		error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
1781 		    lzc_flags, resumeobj, resumeoff);
1782 
1783 		if (flags->progress) {
1784 			(void) pthread_cancel(tid);
1785 			(void) pthread_join(tid, NULL);
1786 		}
1787 
1788 		char errbuf[1024];
1789 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1790 		    "warning: cannot send '%s'"), zhp->zfs_name);
1791 
1792 		zfs_close(zhp);
1793 
1794 		switch (error) {
1795 		case 0:
1796 			return (0);
1797 		case EACCES:
1798 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1799 			    "source key must be loaded"));
1800 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1801 
1802 		case EXDEV:
1803 		case ENOENT:
1804 		case EDQUOT:
1805 		case EFBIG:
1806 		case EIO:
1807 		case ENOLINK:
1808 		case ENOSPC:
1809 		case ENOSTR:
1810 		case ENXIO:
1811 		case EPIPE:
1812 		case ERANGE:
1813 		case EFAULT:
1814 		case EROFS:
1815 			zfs_error_aux(hdl, strerror(errno));
1816 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1817 
1818 		default:
1819 			return (zfs_standard_error(hdl, errno, errbuf));
1820 		}
1821 	}
1822 
1823 
1824 	zfs_close(zhp);
1825 
1826 	return (error);
1827 }
1828 
1829 /*
1830  * Generate a send stream for the dataset identified by the argument zhp.
1831  *
1832  * The content of the send stream is the snapshot identified by
1833  * 'tosnap'.  Incremental streams are requested in two ways:
1834  *     - from the snapshot identified by "fromsnap" (if non-null) or
1835  *     - from the origin of the dataset identified by zhp, which must
1836  *	 be a clone.  In this case, "fromsnap" is null and "fromorigin"
1837  *	 is TRUE.
1838  *
1839  * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
1840  * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
1841  * if "replicate" is set.  If "doall" is set, dump all the intermediate
1842  * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
1843  * case too. If "props" is set, send properties.
1844  */
1845 int
zfs_send(zfs_handle_t * zhp,const char * fromsnap,const char * tosnap,sendflags_t * flags,int outfd,snapfilter_cb_t filter_func,void * cb_arg,nvlist_t ** debugnvp)1846 zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
1847     sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
1848     void *cb_arg, nvlist_t **debugnvp)
1849 {
1850 	char errbuf[1024];
1851 	send_dump_data_t sdd = { 0 };
1852 	int err = 0;
1853 	nvlist_t *fss = NULL;
1854 	avl_tree_t *fsavl = NULL;
1855 	static uint64_t holdseq;
1856 	int spa_version;
1857 	pthread_t tid = 0;
1858 	int pipefd[2];
1859 	dedup_arg_t dda = { 0 };
1860 	int featureflags = 0;
1861 	FILE *fout;
1862 
1863 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1864 	    "cannot send '%s'"), zhp->zfs_name);
1865 
1866 	if (fromsnap && fromsnap[0] == '\0') {
1867 		zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
1868 		    "zero-length incremental source"));
1869 		return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
1870 	}
1871 
1872 	if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1873 		uint64_t version;
1874 		version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1875 		if (version >= ZPL_VERSION_SA) {
1876 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1877 		}
1878 	}
1879 
1880 	if (flags->holds)
1881 		featureflags |= DMU_BACKUP_FEATURE_HOLDS;
1882 
1883 	/*
1884 	 * Start the dedup thread if this is a dedup stream. We do not bother
1885 	 * doing this if this a raw send of an encrypted dataset with dedup off
1886 	 * because normal encrypted blocks won't dedup.
1887 	 */
1888 	if (flags->dedup && !flags->dryrun && !(flags->raw &&
1889 	    zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
1890 	    zfs_prop_get_int(zhp, ZFS_PROP_DEDUP) == ZIO_CHECKSUM_OFF)) {
1891 		featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1892 		    DMU_BACKUP_FEATURE_DEDUPPROPS);
1893 		if ((err = pipe(pipefd)) != 0) {
1894 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1895 			return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1896 			    errbuf));
1897 		}
1898 		dda.outputfd = outfd;
1899 		dda.inputfd = pipefd[1];
1900 		dda.dedup_hdl = zhp->zfs_hdl;
1901 		if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1902 			(void) close(pipefd[0]);
1903 			(void) close(pipefd[1]);
1904 			zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1905 			return (zfs_error(zhp->zfs_hdl,
1906 			    EZFS_THREADCREATEFAILED, errbuf));
1907 		}
1908 	}
1909 
1910 	if (flags->replicate || flags->doall || flags->props ||
1911 	    flags->holds || flags->backup) {
1912 		dmu_replay_record_t drr = { 0 };
1913 		char *packbuf = NULL;
1914 		size_t buflen = 0;
1915 		zio_cksum_t zc;
1916 
1917 		ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
1918 
1919 		if (flags->replicate || flags->props || flags->backup ||
1920 		    flags->holds) {
1921 			nvlist_t *hdrnv;
1922 
1923 			VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1924 			if (fromsnap) {
1925 				VERIFY(0 == nvlist_add_string(hdrnv,
1926 				    "fromsnap", fromsnap));
1927 			}
1928 			VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
1929 			if (!flags->replicate) {
1930 				VERIFY(0 == nvlist_add_boolean(hdrnv,
1931 				    "not_recursive"));
1932 			}
1933 			if (flags->raw) {
1934 				VERIFY(0 == nvlist_add_boolean(hdrnv, "raw"));
1935 			}
1936 
1937 			err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1938 			    fromsnap, tosnap, flags->replicate, flags->raw,
1939 			    flags->verbose, flags->backup,
1940 			    flags->holds, flags->props, &fss,
1941 			    &fsavl);
1942 			if (err) {
1943 				nvlist_free(hdrnv);
1944 				goto err_out;
1945 			}
1946 
1947 			/*
1948 			 * Do not allow the size of the properties list to
1949 			 * exceed the limit
1950 			 */
1951 			if ((fnvlist_size(fss) + fnvlist_size(hdrnv)) >
1952 			    zhp->zfs_hdl->libzfs_max_nvlist) {
1953 				(void) snprintf(errbuf, sizeof (errbuf),
1954 				    dgettext(TEXT_DOMAIN,
1955 				    "warning: cannot send '%s': "
1956 				    "the size of the list of snapshots and "
1957 				    "properties is too large to be received "
1958 				    "successfully.\n"
1959 				    "Select a smaller number of snapshots to "
1960 				    "send.\n"),
1961 				    zhp->zfs_name);
1962 				nvlist_free(hdrnv);
1963 				err = zfs_error(zhp->zfs_hdl, EZFS_NOSPC,
1964 				    errbuf);
1965 				goto err_out;
1966 			}
1967 			VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
1968 			err = nvlist_pack(hdrnv, &packbuf, &buflen,
1969 			    NV_ENCODE_XDR, 0);
1970 			if (debugnvp)
1971 				*debugnvp = hdrnv;
1972 			else
1973 				nvlist_free(hdrnv);
1974 			if (err)
1975 				goto stderr_out;
1976 		}
1977 
1978 		if (!flags->dryrun) {
1979 			/* write first begin record */
1980 			drr.drr_type = DRR_BEGIN;
1981 			drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1982 			DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1983 			    drr_versioninfo, DMU_COMPOUNDSTREAM);
1984 			DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1985 			    drr_versioninfo, featureflags);
1986 			(void) snprintf(drr.drr_u.drr_begin.drr_toname,
1987 			    sizeof (drr.drr_u.drr_begin.drr_toname),
1988 			    "%s@%s", zhp->zfs_name, tosnap);
1989 			drr.drr_payloadlen = buflen;
1990 
1991 			err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1992 			free(packbuf);
1993 			if (err != 0)
1994 				goto stderr_out;
1995 
1996 			/* write end record */
1997 			bzero(&drr, sizeof (drr));
1998 			drr.drr_type = DRR_END;
1999 			drr.drr_u.drr_end.drr_checksum = zc;
2000 			err = write(outfd, &drr, sizeof (drr));
2001 			if (err == -1) {
2002 				err = errno;
2003 				goto stderr_out;
2004 			}
2005 
2006 			err = 0;
2007 		}
2008 	}
2009 
2010 	/* dump each stream */
2011 	sdd.fromsnap = fromsnap;
2012 	sdd.tosnap = tosnap;
2013 	if (tid != 0)
2014 		sdd.outfd = pipefd[0];
2015 	else
2016 		sdd.outfd = outfd;
2017 	sdd.replicate = flags->replicate;
2018 	sdd.doall = flags->doall;
2019 	sdd.fromorigin = flags->fromorigin;
2020 	sdd.fss = fss;
2021 	sdd.fsavl = fsavl;
2022 	sdd.verbose = flags->verbose;
2023 	sdd.parsable = flags->parsable;
2024 	sdd.progress = flags->progress;
2025 	sdd.dryrun = flags->dryrun;
2026 	sdd.large_block = flags->largeblock;
2027 	sdd.embed_data = flags->embed_data;
2028 	sdd.compress = flags->compress;
2029 	sdd.raw = flags->raw;
2030 	sdd.holds = flags->holds;
2031 	sdd.filter_cb = filter_func;
2032 	sdd.filter_cb_arg = cb_arg;
2033 	if (debugnvp)
2034 		sdd.debugnv = *debugnvp;
2035 	if (sdd.verbose && sdd.dryrun)
2036 		sdd.std_out = B_TRUE;
2037 	fout = sdd.std_out ? stdout : stderr;
2038 
2039 	/*
2040 	 * Some flags require that we place user holds on the datasets that are
2041 	 * being sent so they don't get destroyed during the send. We can skip
2042 	 * this step if the pool is imported read-only since the datasets cannot
2043 	 * be destroyed.
2044 	 */
2045 	if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
2046 	    ZPOOL_PROP_READONLY, NULL) &&
2047 	    zfs_spa_version(zhp, &spa_version) == 0 &&
2048 	    spa_version >= SPA_VERSION_USERREFS &&
2049 	    (flags->doall || flags->replicate)) {
2050 		++holdseq;
2051 		(void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2052 		    ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2053 		sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
2054 		if (sdd.cleanup_fd < 0) {
2055 			err = errno;
2056 			goto stderr_out;
2057 		}
2058 		sdd.snapholds = fnvlist_alloc();
2059 	} else {
2060 		sdd.cleanup_fd = -1;
2061 		sdd.snapholds = NULL;
2062 	}
2063 
2064 	if (flags->verbose || sdd.snapholds != NULL) {
2065 		/*
2066 		 * Do a verbose no-op dry run to get all the verbose output
2067 		 * or to gather snapshot hold's before generating any data,
2068 		 * then do a non-verbose real run to generate the streams.
2069 		 */
2070 		sdd.dryrun = B_TRUE;
2071 		err = dump_filesystems(zhp, &sdd);
2072 
2073 		if (err != 0)
2074 			goto stderr_out;
2075 
2076 		if (flags->verbose) {
2077 			if (flags->parsable) {
2078 				(void) fprintf(fout, "size\t%llu\n",
2079 				    (longlong_t)sdd.size);
2080 			} else {
2081 				char buf[16];
2082 				zfs_nicebytes(sdd.size, buf, sizeof (buf));
2083 				(void) fprintf(fout, dgettext(TEXT_DOMAIN,
2084 				    "total estimated size is %s\n"), buf);
2085 			}
2086 		}
2087 
2088 		/* Ensure no snaps found is treated as an error. */
2089 		if (!sdd.seento) {
2090 			err = ENOENT;
2091 			goto err_out;
2092 		}
2093 
2094 		/* Skip the second run if dryrun was requested. */
2095 		if (flags->dryrun)
2096 			goto err_out;
2097 
2098 		if (sdd.snapholds != NULL) {
2099 			err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2100 			if (err != 0)
2101 				goto stderr_out;
2102 
2103 			fnvlist_free(sdd.snapholds);
2104 			sdd.snapholds = NULL;
2105 		}
2106 
2107 		sdd.dryrun = B_FALSE;
2108 		sdd.verbose = B_FALSE;
2109 	}
2110 
2111 	err = dump_filesystems(zhp, &sdd);
2112 	fsavl_destroy(fsavl);
2113 	nvlist_free(fss);
2114 
2115 	/* Ensure no snaps found is treated as an error. */
2116 	if (err == 0 && !sdd.seento)
2117 		err = ENOENT;
2118 
2119 	if (tid != 0) {
2120 		if (err != 0)
2121 			(void) pthread_cancel(tid);
2122 		(void) close(pipefd[0]);
2123 		(void) pthread_join(tid, NULL);
2124 	}
2125 
2126 	if (sdd.cleanup_fd != -1) {
2127 		VERIFY(0 == close(sdd.cleanup_fd));
2128 		sdd.cleanup_fd = -1;
2129 	}
2130 
2131 	if (!flags->dryrun && (flags->replicate || flags->doall ||
2132 	    flags->props || flags->backup || flags->holds)) {
2133 		/*
2134 		 * write final end record.  NB: want to do this even if
2135 		 * there was some error, because it might not be totally
2136 		 * failed.
2137 		 */
2138 		dmu_replay_record_t drr = { 0 };
2139 		drr.drr_type = DRR_END;
2140 		if (write(outfd, &drr, sizeof (drr)) == -1) {
2141 			return (zfs_standard_error(zhp->zfs_hdl,
2142 			    errno, errbuf));
2143 		}
2144 	}
2145 
2146 	return (err || sdd.err);
2147 
2148 stderr_out:
2149 	err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
2150 err_out:
2151 	fsavl_destroy(fsavl);
2152 	nvlist_free(fss);
2153 	fnvlist_free(sdd.snapholds);
2154 
2155 	if (sdd.cleanup_fd != -1)
2156 		VERIFY(0 == close(sdd.cleanup_fd));
2157 	if (tid != 0) {
2158 		(void) pthread_cancel(tid);
2159 		(void) close(pipefd[0]);
2160 		(void) pthread_join(tid, NULL);
2161 	}
2162 	return (err);
2163 }
2164 
2165 int
zfs_send_one(zfs_handle_t * zhp,const char * from,int fd,enum lzc_send_flags flags)2166 zfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
2167     enum lzc_send_flags flags)
2168 {
2169 	int err;
2170 	libzfs_handle_t *hdl = zhp->zfs_hdl;
2171 
2172 	char errbuf[1024];
2173 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2174 	    "warning: cannot send '%s'"), zhp->zfs_name);
2175 
2176 	err = lzc_send(zhp->zfs_name, from, fd, flags);
2177 	if (err != 0) {
2178 		switch (errno) {
2179 		case EXDEV:
2180 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2181 			    "not an earlier snapshot from the same fs"));
2182 			return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
2183 
2184 		case ENOENT:
2185 		case ESRCH:
2186 			if (lzc_exists(zhp->zfs_name)) {
2187 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2188 				    "incremental source (%s) does not exist"),
2189 				    from);
2190 			}
2191 			return (zfs_error(hdl, EZFS_NOENT, errbuf));
2192 
2193 		case EACCES:
2194 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2195 			    "dataset key must be loaded"));
2196 			return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2197 
2198 		case EBUSY:
2199 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2200 			    "target is busy; if a filesystem, "
2201 			    "it must not be mounted"));
2202 			return (zfs_error(hdl, EZFS_BUSY, errbuf));
2203 
2204 		case EDQUOT:
2205 		case EFBIG:
2206 		case EIO:
2207 		case ENOLINK:
2208 		case ENOSPC:
2209 		case ENOSTR:
2210 		case ENXIO:
2211 		case EPIPE:
2212 		case ERANGE:
2213 		case EFAULT:
2214 		case EROFS:
2215 			zfs_error_aux(hdl, strerror(errno));
2216 			return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
2217 
2218 		default:
2219 			return (zfs_standard_error(hdl, errno, errbuf));
2220 		}
2221 	}
2222 	return (err != 0);
2223 }
2224 
2225 /*
2226  * Routines specific to "zfs recv"
2227  */
2228 
2229 static int
recv_read(libzfs_handle_t * hdl,int fd,void * buf,int ilen,boolean_t byteswap,zio_cksum_t * zc)2230 recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
2231     boolean_t byteswap, zio_cksum_t *zc)
2232 {
2233 	char *cp = buf;
2234 	int rv;
2235 	int len = ilen;
2236 
2237 	do {
2238 		rv = read(fd, cp, len);
2239 		cp += rv;
2240 		len -= rv;
2241 	} while (rv > 0);
2242 
2243 	if (rv < 0 || len != 0) {
2244 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2245 		    "failed to read from stream"));
2246 		return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
2247 		    "cannot receive")));
2248 	}
2249 
2250 	if (zc) {
2251 		if (byteswap)
2252 			(void) fletcher_4_incremental_byteswap(buf, ilen, zc);
2253 		else
2254 			(void) fletcher_4_incremental_native(buf, ilen, zc);
2255 	}
2256 	return (0);
2257 }
2258 
2259 static int
recv_read_nvlist(libzfs_handle_t * hdl,int fd,int len,nvlist_t ** nvp,boolean_t byteswap,zio_cksum_t * zc)2260 recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
2261     boolean_t byteswap, zio_cksum_t *zc)
2262 {
2263 	char *buf;
2264 	int err;
2265 
2266 	buf = zfs_alloc(hdl, len);
2267 	if (buf == NULL)
2268 		return (ENOMEM);
2269 
2270 	if (len > hdl->libzfs_max_nvlist) {
2271 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "nvlist too large"));
2272 		free(buf);
2273 		return (ENOMEM);
2274 	}
2275 
2276 	err = recv_read(hdl, fd, buf, len, byteswap, zc);
2277 	if (err != 0) {
2278 		free(buf);
2279 		return (err);
2280 	}
2281 
2282 	err = nvlist_unpack(buf, len, nvp, 0);
2283 	free(buf);
2284 	if (err != 0) {
2285 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2286 		    "stream (malformed nvlist)"));
2287 		return (EINVAL);
2288 	}
2289 	return (0);
2290 }
2291 
2292 /*
2293  * Returns the grand origin (origin of origin of origin...) of a given handle.
2294  * If this dataset is not a clone, it simply returns a copy of the original
2295  * handle.
2296  */
2297 static zfs_handle_t *
recv_open_grand_origin(zfs_handle_t * zhp)2298 recv_open_grand_origin(zfs_handle_t *zhp)
2299 {
2300 	char origin[ZFS_MAX_DATASET_NAME_LEN];
2301 	zprop_source_t src;
2302 	zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2303 
2304 	while (ozhp != NULL) {
2305 		if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2306 		    sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2307 			break;
2308 
2309 		(void) zfs_close(ozhp);
2310 		ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2311 	}
2312 
2313 	return (ozhp);
2314 }
2315 
2316 static int
recv_rename_impl(zfs_handle_t * zhp,const char * source,const char * target)2317 recv_rename_impl(zfs_handle_t *zhp, const char *source, const char *target)
2318 {
2319 	int err;
2320 	zfs_handle_t *ozhp = NULL;
2321 
2322 	/*
2323 	 * Attempt to rename the dataset. If it fails with EACCES we have
2324 	 * attempted to rename the dataset outside of its encryption root.
2325 	 * Force the dataset to become an encryption root and try again.
2326 	 */
2327 	err = lzc_rename(source, target);
2328 	if (err == EACCES) {
2329 		ozhp = recv_open_grand_origin(zhp);
2330 		if (ozhp == NULL) {
2331 			err = ENOENT;
2332 			goto out;
2333 		}
2334 
2335 		err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2336 		    NULL, NULL, 0);
2337 		if (err != 0)
2338 			goto out;
2339 
2340 		err = lzc_rename(source, target);
2341 	}
2342 
2343 out:
2344 	if (ozhp != NULL)
2345 		zfs_close(ozhp);
2346 	return (err);
2347 }
2348 
2349 static int
recv_rename(libzfs_handle_t * hdl,const char * name,const char * tryname,int baselen,char * newname,recvflags_t * flags)2350 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2351     int baselen, char *newname, recvflags_t *flags)
2352 {
2353 	static int seq;
2354 	int err;
2355 	prop_changelist_t *clp = NULL;
2356 	zfs_handle_t *zhp = NULL;
2357 
2358 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2359 	if (zhp == NULL) {
2360 		err = -1;
2361 		goto out;
2362 	}
2363 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2364 	    flags->force ? MS_FORCE : 0);
2365 	if (clp == NULL) {
2366 		err = -1;
2367 		goto out;
2368 	}
2369 	err = changelist_prefix(clp);
2370 	if (err)
2371 		goto out;
2372 
2373 	if (tryname) {
2374 		(void) strcpy(newname, tryname);
2375 		if (flags->verbose) {
2376 			(void) printf("attempting rename %s to %s\n",
2377 			    name, newname);
2378 		}
2379 		err = recv_rename_impl(zhp, name, newname);
2380 		if (err == 0)
2381 			changelist_rename(clp, name, tryname);
2382 	} else {
2383 		err = ENOENT;
2384 	}
2385 
2386 	if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2387 		seq++;
2388 
2389 		(void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2390 		    "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2391 		if (flags->verbose) {
2392 			(void) printf("failed - trying rename %s to %s\n",
2393 			    name, newname);
2394 		}
2395 		err = recv_rename_impl(zhp, name, newname);
2396 		if (err == 0)
2397 			changelist_rename(clp, name, newname);
2398 		if (err && flags->verbose) {
2399 			(void) printf("failed (%u) - "
2400 			    "will try again on next pass\n", errno);
2401 		}
2402 		err = EAGAIN;
2403 	} else if (flags->verbose) {
2404 		if (err == 0)
2405 			(void) printf("success\n");
2406 		else
2407 			(void) printf("failed (%u)\n", errno);
2408 	}
2409 
2410 	(void) changelist_postfix(clp);
2411 
2412 out:
2413 	if (clp != NULL)
2414 		changelist_free(clp);
2415 	if (zhp != NULL)
2416 		zfs_close(zhp);
2417 
2418 	return (err);
2419 }
2420 
2421 static int
recv_promote(libzfs_handle_t * hdl,const char * fsname,const char * origin_fsname,recvflags_t * flags)2422 recv_promote(libzfs_handle_t *hdl, const char *fsname,
2423     const char *origin_fsname, recvflags_t *flags)
2424 {
2425 	int err;
2426 	zfs_cmd_t zc = {"\0"};
2427 	zfs_handle_t *zhp = NULL, *ozhp = NULL;
2428 
2429 	if (flags->verbose)
2430 		(void) printf("promoting %s\n", fsname);
2431 
2432 	(void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
2433 	(void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
2434 
2435 	/*
2436 	 * Attempt to promote the dataset. If it fails with EACCES the
2437 	 * promotion would cause this dataset to leave its encryption root.
2438 	 * Force the origin to become an encryption root and try again.
2439 	 */
2440 	err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2441 	if (err == EACCES) {
2442 		zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2443 		if (zhp == NULL) {
2444 			err = -1;
2445 			goto out;
2446 		}
2447 
2448 		ozhp = recv_open_grand_origin(zhp);
2449 		if (ozhp == NULL) {
2450 			err = -1;
2451 			goto out;
2452 		}
2453 
2454 		err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2455 		    NULL, NULL, 0);
2456 		if (err != 0)
2457 			goto out;
2458 
2459 		err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2460 	}
2461 
2462 out:
2463 	if (zhp != NULL)
2464 		zfs_close(zhp);
2465 	if (ozhp != NULL)
2466 		zfs_close(ozhp);
2467 
2468 	return (err);
2469 }
2470 
2471 static int
recv_destroy(libzfs_handle_t * hdl,const char * name,int baselen,char * newname,recvflags_t * flags)2472 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2473     char *newname, recvflags_t *flags)
2474 {
2475 	int err = 0;
2476 	prop_changelist_t *clp;
2477 	zfs_handle_t *zhp;
2478 	boolean_t defer = B_FALSE;
2479 	int spa_version;
2480 
2481 	zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2482 	if (zhp == NULL)
2483 		return (-1);
2484 	clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2485 	    flags->force ? MS_FORCE : 0);
2486 	if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2487 	    zfs_spa_version(zhp, &spa_version) == 0 &&
2488 	    spa_version >= SPA_VERSION_USERREFS)
2489 		defer = B_TRUE;
2490 	zfs_close(zhp);
2491 	if (clp == NULL)
2492 		return (-1);
2493 	err = changelist_prefix(clp);
2494 	if (err)
2495 		return (err);
2496 
2497 	if (flags->verbose)
2498 		(void) printf("attempting destroy %s\n", name);
2499 	if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2500 		nvlist_t *nv = fnvlist_alloc();
2501 		fnvlist_add_boolean(nv, name);
2502 		err = lzc_destroy_snaps(nv, defer, NULL);
2503 		fnvlist_free(nv);
2504 	} else {
2505 		err = lzc_destroy(name);
2506 	}
2507 	if (err == 0) {
2508 		if (flags->verbose)
2509 			(void) printf("success\n");
2510 		changelist_remove(clp, name);
2511 	}
2512 
2513 	(void) changelist_postfix(clp);
2514 	changelist_free(clp);
2515 
2516 	/*
2517 	 * Deferred destroy might destroy the snapshot or only mark it to be
2518 	 * destroyed later, and it returns success in either case.
2519 	 */
2520 	if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2521 	    ZFS_TYPE_SNAPSHOT))) {
2522 		err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2523 	}
2524 
2525 	return (err);
2526 }
2527 
2528 typedef struct guid_to_name_data {
2529 	uint64_t guid;
2530 	boolean_t bookmark_ok;
2531 	char *name;
2532 	char *skip;
2533 } guid_to_name_data_t;
2534 
2535 static int
guid_to_name_cb(zfs_handle_t * zhp,void * arg)2536 guid_to_name_cb(zfs_handle_t *zhp, void *arg)
2537 {
2538 	guid_to_name_data_t *gtnd = arg;
2539 	const char *slash;
2540 	int err;
2541 
2542 	if (gtnd->skip != NULL &&
2543 	    (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
2544 	    strcmp(slash + 1, gtnd->skip) == 0) {
2545 		zfs_close(zhp);
2546 		return (0);
2547 	}
2548 
2549 	if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
2550 		(void) strcpy(gtnd->name, zhp->zfs_name);
2551 		zfs_close(zhp);
2552 		return (EEXIST);
2553 	}
2554 
2555 	err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
2556 	if (err != EEXIST && gtnd->bookmark_ok)
2557 		err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
2558 	zfs_close(zhp);
2559 	return (err);
2560 }
2561 
2562 /*
2563  * Attempt to find the local dataset associated with this guid.  In the case of
2564  * multiple matches, we attempt to find the "best" match by searching
2565  * progressively larger portions of the hierarchy.  This allows one to send a
2566  * tree of datasets individually and guarantee that we will find the source
2567  * guid within that hierarchy, even if there are multiple matches elsewhere.
2568  */
2569 static int
guid_to_name(libzfs_handle_t * hdl,const char * parent,uint64_t guid,boolean_t bookmark_ok,char * name)2570 guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
2571     boolean_t bookmark_ok, char *name)
2572 {
2573 	char pname[ZFS_MAX_DATASET_NAME_LEN];
2574 	guid_to_name_data_t gtnd;
2575 
2576 	gtnd.guid = guid;
2577 	gtnd.bookmark_ok = bookmark_ok;
2578 	gtnd.name = name;
2579 	gtnd.skip = NULL;
2580 
2581 	/*
2582 	 * Search progressively larger portions of the hierarchy, starting
2583 	 * with the filesystem specified by 'parent'.  This will
2584 	 * select the "most local" version of the origin snapshot in the case
2585 	 * that there are multiple matching snapshots in the system.
2586 	 */
2587 	(void) strlcpy(pname, parent, sizeof (pname));
2588 	char *cp = strrchr(pname, '@');
2589 	if (cp == NULL)
2590 		cp = strchr(pname, '\0');
2591 	for (; cp != NULL; cp = strrchr(pname, '/')) {
2592 		/* Chop off the last component and open the parent */
2593 		*cp = '\0';
2594 		zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
2595 
2596 		if (zhp == NULL)
2597 			continue;
2598 		int err = guid_to_name_cb(zfs_handle_dup(zhp), &gtnd);
2599 		if (err != EEXIST)
2600 			err = zfs_iter_children(zhp, guid_to_name_cb, &gtnd);
2601 		if (err != EEXIST && bookmark_ok)
2602 			err = zfs_iter_bookmarks(zhp, guid_to_name_cb, &gtnd);
2603 		zfs_close(zhp);
2604 		if (err == EEXIST)
2605 			return (0);
2606 
2607 		/*
2608 		 * Remember the last portion of the dataset so we skip it next
2609 		 * time through (as we've already searched that portion of the
2610 		 * hierarchy).
2611 		 */
2612 		gtnd.skip = strrchr(pname, '/') + 1;
2613 	}
2614 
2615 	return (ENOENT);
2616 }
2617 
2618 /*
2619  * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2620  * guid1 is after guid2.
2621  */
2622 static int
created_before(libzfs_handle_t * hdl,avl_tree_t * avl,uint64_t guid1,uint64_t guid2)2623 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2624     uint64_t guid1, uint64_t guid2)
2625 {
2626 	nvlist_t *nvfs;
2627 	char *fsname, *snapname;
2628 	char buf[ZFS_MAX_DATASET_NAME_LEN];
2629 	int rv;
2630 	zfs_handle_t *guid1hdl, *guid2hdl;
2631 	uint64_t create1, create2;
2632 
2633 	if (guid2 == 0)
2634 		return (0);
2635 	if (guid1 == 0)
2636 		return (1);
2637 
2638 	nvfs = fsavl_find(avl, guid1, &snapname);
2639 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2640 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2641 	guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2642 	if (guid1hdl == NULL)
2643 		return (-1);
2644 
2645 	nvfs = fsavl_find(avl, guid2, &snapname);
2646 	VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2647 	(void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2648 	guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2649 	if (guid2hdl == NULL) {
2650 		zfs_close(guid1hdl);
2651 		return (-1);
2652 	}
2653 
2654 	create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2655 	create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2656 
2657 	if (create1 < create2)
2658 		rv = -1;
2659 	else if (create1 > create2)
2660 		rv = +1;
2661 	else
2662 		rv = 0;
2663 
2664 	zfs_close(guid1hdl);
2665 	zfs_close(guid2hdl);
2666 
2667 	return (rv);
2668 }
2669 
2670 /*
2671  * This function reestablishes the heirarchy of encryption roots after a
2672  * recursive incremental receive has completed. This must be done after the
2673  * second call to recv_incremental_replication() has renamed and promoted all
2674  * sent datasets to their final locations in the dataset heriarchy.
2675  */
2676 /* ARGSUSED */
2677 static int
recv_fix_encryption_hierarchy(libzfs_handle_t * hdl,const char * destname,nvlist_t * stream_nv,avl_tree_t * stream_avl)2678 recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *destname,
2679     nvlist_t *stream_nv, avl_tree_t *stream_avl)
2680 {
2681 	int err;
2682 	nvpair_t *fselem = NULL;
2683 	nvlist_t *stream_fss;
2684 	char *cp;
2685 	char top_zfs[ZFS_MAX_DATASET_NAME_LEN];
2686 
2687 	(void) strcpy(top_zfs, destname);
2688 	cp = strrchr(top_zfs, '@');
2689 	if (cp != NULL)
2690 		*cp = '\0';
2691 
2692 	VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", &stream_fss));
2693 
2694 	while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
2695 		zfs_handle_t *zhp = NULL;
2696 		uint64_t crypt;
2697 		nvlist_t *snaps, *props, *stream_nvfs = NULL;
2698 		nvpair_t *snapel = NULL;
2699 		boolean_t is_encroot, is_clone, stream_encroot;
2700 		char *cp;
2701 		char *stream_keylocation = NULL;
2702 		char keylocation[MAXNAMELEN];
2703 		char fsname[ZFS_MAX_DATASET_NAME_LEN];
2704 
2705 		keylocation[0] = '\0';
2706 		VERIFY(0 == nvpair_value_nvlist(fselem, &stream_nvfs));
2707 		VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "snaps", &snaps));
2708 		VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "props", &props));
2709 		stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
2710 
2711 		/* find a snapshot from the stream that exists locally */
2712 		err = ENOENT;
2713 		while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
2714 			uint64_t guid;
2715 
2716 			VERIFY(0 == nvpair_value_uint64(snapel, &guid));
2717 			err = guid_to_name(hdl, destname, guid, B_FALSE,
2718 			    fsname);
2719 			if (err == 0)
2720 				break;
2721 		}
2722 
2723 		if (err != 0)
2724 			continue;
2725 
2726 		cp = strchr(fsname, '@');
2727 		if (cp != NULL)
2728 			*cp = '\0';
2729 
2730 		zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2731 		if (zhp == NULL) {
2732 			err = ENOENT;
2733 			goto error;
2734 		}
2735 
2736 		crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
2737 		is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
2738 		(void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
2739 
2740 		/* we don't need to do anything for unencrypted datasets */
2741 		if (crypt == ZIO_CRYPT_OFF) {
2742 			zfs_close(zhp);
2743 			continue;
2744 		}
2745 
2746 		/*
2747 		 * If the dataset is flagged as an encryption root, was not
2748 		 * received as a clone and is not currently an encryption root,
2749 		 * force it to become one. Fixup the keylocation if necessary.
2750 		 */
2751 		if (stream_encroot) {
2752 			if (!is_clone && !is_encroot) {
2753 				err = lzc_change_key(fsname,
2754 				    DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
2755 				if (err != 0) {
2756 					zfs_close(zhp);
2757 					goto error;
2758 				}
2759 			}
2760 
2761 			VERIFY(0 == nvlist_lookup_string(props,
2762 			    zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2763 			    &stream_keylocation));
2764 
2765 			/*
2766 			 * Refresh the properties in case the call to
2767 			 * lzc_change_key() changed the value.
2768 			 */
2769 			zfs_refresh_properties(zhp);
2770 			err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
2771 			    keylocation, sizeof (keylocation), NULL, NULL,
2772 			    0, B_TRUE);
2773 			if (err != 0) {
2774 				zfs_close(zhp);
2775 				goto error;
2776 			}
2777 
2778 			if (strcmp(keylocation, stream_keylocation) != 0) {
2779 				err = zfs_prop_set(zhp,
2780 				    zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2781 				    stream_keylocation);
2782 				if (err != 0) {
2783 					zfs_close(zhp);
2784 					goto error;
2785 				}
2786 			}
2787 		}
2788 
2789 		/*
2790 		 * If the dataset is not flagged as an encryption root and is
2791 		 * currently an encryption root, force it to inherit from its
2792 		 * parent. The root of a raw send should never be
2793 		 * force-inherited.
2794 		 */
2795 		if (!stream_encroot && is_encroot &&
2796 		    strcmp(top_zfs, fsname) != 0) {
2797 			err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
2798 			    NULL, NULL, 0);
2799 			if (err != 0) {
2800 				zfs_close(zhp);
2801 				goto error;
2802 			}
2803 		}
2804 
2805 		zfs_close(zhp);
2806 	}
2807 
2808 	return (0);
2809 
2810 error:
2811 	return (err);
2812 }
2813 
2814 static int
recv_incremental_replication(libzfs_handle_t * hdl,const char * tofs,recvflags_t * flags,nvlist_t * stream_nv,avl_tree_t * stream_avl,nvlist_t * renamed)2815 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2816     recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2817     nvlist_t *renamed)
2818 {
2819 	nvlist_t *local_nv;
2820 	avl_tree_t *local_avl;
2821 	nvpair_t *fselem, *nextfselem;
2822 	char *fromsnap;
2823 	char newname[ZFS_MAX_DATASET_NAME_LEN];
2824 	int error;
2825 	boolean_t needagain, progress, recursive;
2826 	char *s1, *s2;
2827 
2828 	VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2829 
2830 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2831 	    ENOENT);
2832 
2833 	if (flags->dryrun)
2834 		return (0);
2835 
2836 again:
2837 	needagain = progress = B_FALSE;
2838 
2839 	if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2840 	    recursive, B_TRUE, B_FALSE,
2841 	    B_FALSE, B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
2842 		return (error);
2843 
2844 	/*
2845 	 * Process deletes and renames
2846 	 */
2847 	for (fselem = nvlist_next_nvpair(local_nv, NULL);
2848 	    fselem; fselem = nextfselem) {
2849 		nvlist_t *nvfs, *snaps;
2850 		nvlist_t *stream_nvfs = NULL;
2851 		nvpair_t *snapelem, *nextsnapelem;
2852 		uint64_t fromguid = 0;
2853 		uint64_t originguid = 0;
2854 		uint64_t stream_originguid = 0;
2855 		uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2856 		char *fsname, *stream_fsname;
2857 
2858 		nextfselem = nvlist_next_nvpair(local_nv, fselem);
2859 
2860 		VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2861 		VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2862 		VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2863 		VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2864 		    &parent_fromsnap_guid));
2865 		(void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2866 
2867 		/*
2868 		 * First find the stream's fs, so we can check for
2869 		 * a different origin (due to "zfs promote")
2870 		 */
2871 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
2872 		    snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2873 			uint64_t thisguid;
2874 
2875 			VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2876 			stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2877 
2878 			if (stream_nvfs != NULL)
2879 				break;
2880 		}
2881 
2882 		/* check for promote */
2883 		(void) nvlist_lookup_uint64(stream_nvfs, "origin",
2884 		    &stream_originguid);
2885 		if (stream_nvfs && originguid != stream_originguid) {
2886 			switch (created_before(hdl, local_avl,
2887 			    stream_originguid, originguid)) {
2888 			case 1: {
2889 				/* promote it! */
2890 				nvlist_t *origin_nvfs;
2891 				char *origin_fsname;
2892 
2893 				origin_nvfs = fsavl_find(local_avl, originguid,
2894 				    NULL);
2895 				VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2896 				    "name", &origin_fsname));
2897 				error = recv_promote(hdl, fsname, origin_fsname,
2898 				    flags);
2899 				if (error == 0)
2900 					progress = B_TRUE;
2901 				break;
2902 			}
2903 			default:
2904 				break;
2905 			case -1:
2906 				fsavl_destroy(local_avl);
2907 				nvlist_free(local_nv);
2908 				return (-1);
2909 			}
2910 			/*
2911 			 * We had/have the wrong origin, therefore our
2912 			 * list of snapshots is wrong.  Need to handle
2913 			 * them on the next pass.
2914 			 */
2915 			needagain = B_TRUE;
2916 			continue;
2917 		}
2918 
2919 		for (snapelem = nvlist_next_nvpair(snaps, NULL);
2920 		    snapelem; snapelem = nextsnapelem) {
2921 			uint64_t thisguid;
2922 			char *stream_snapname;
2923 			nvlist_t *found, *props;
2924 
2925 			nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2926 
2927 			VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2928 			found = fsavl_find(stream_avl, thisguid,
2929 			    &stream_snapname);
2930 
2931 			/* check for delete */
2932 			if (found == NULL) {
2933 				char name[ZFS_MAX_DATASET_NAME_LEN];
2934 
2935 				if (!flags->force)
2936 					continue;
2937 
2938 				(void) snprintf(name, sizeof (name), "%s@%s",
2939 				    fsname, nvpair_name(snapelem));
2940 
2941 				error = recv_destroy(hdl, name,
2942 				    strlen(fsname)+1, newname, flags);
2943 				if (error)
2944 					needagain = B_TRUE;
2945 				else
2946 					progress = B_TRUE;
2947 				continue;
2948 			}
2949 
2950 			stream_nvfs = found;
2951 
2952 			if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2953 			    &props) && 0 == nvlist_lookup_nvlist(props,
2954 			    stream_snapname, &props)) {
2955 				zfs_cmd_t zc = { 0 };
2956 
2957 				zc.zc_cookie = B_TRUE; /* received */
2958 				(void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2959 				    "%s@%s", fsname, nvpair_name(snapelem));
2960 				if (zcmd_write_src_nvlist(hdl, &zc,
2961 				    props) == 0) {
2962 					(void) zfs_ioctl(hdl,
2963 					    ZFS_IOC_SET_PROP, &zc);
2964 					zcmd_free_nvlists(&zc);
2965 				}
2966 			}
2967 
2968 			/* check for different snapname */
2969 			if (strcmp(nvpair_name(snapelem),
2970 			    stream_snapname) != 0) {
2971 				char name[ZFS_MAX_DATASET_NAME_LEN];
2972 				char tryname[ZFS_MAX_DATASET_NAME_LEN];
2973 
2974 				(void) snprintf(name, sizeof (name), "%s@%s",
2975 				    fsname, nvpair_name(snapelem));
2976 				(void) snprintf(tryname, sizeof (name), "%s@%s",
2977 				    fsname, stream_snapname);
2978 
2979 				error = recv_rename(hdl, name, tryname,
2980 				    strlen(fsname)+1, newname, flags);
2981 				if (error)
2982 					needagain = B_TRUE;
2983 				else
2984 					progress = B_TRUE;
2985 			}
2986 
2987 			if (strcmp(stream_snapname, fromsnap) == 0)
2988 				fromguid = thisguid;
2989 		}
2990 
2991 		/* check for delete */
2992 		if (stream_nvfs == NULL) {
2993 			if (!flags->force)
2994 				continue;
2995 
2996 			error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2997 			    newname, flags);
2998 			if (error)
2999 				needagain = B_TRUE;
3000 			else
3001 				progress = B_TRUE;
3002 			continue;
3003 		}
3004 
3005 		if (fromguid == 0) {
3006 			if (flags->verbose) {
3007 				(void) printf("local fs %s does not have "
3008 				    "fromsnap (%s in stream); must have "
3009 				    "been deleted locally; ignoring\n",
3010 				    fsname, fromsnap);
3011 			}
3012 			continue;
3013 		}
3014 
3015 		VERIFY(0 == nvlist_lookup_string(stream_nvfs,
3016 		    "name", &stream_fsname));
3017 		VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
3018 		    "parentfromsnap", &stream_parent_fromsnap_guid));
3019 
3020 		s1 = strrchr(fsname, '/');
3021 		s2 = strrchr(stream_fsname, '/');
3022 
3023 		/*
3024 		 * Check for rename. If the exact receive path is specified, it
3025 		 * does not count as a rename, but we still need to check the
3026 		 * datasets beneath it.
3027 		 */
3028 		if ((stream_parent_fromsnap_guid != 0 &&
3029 		    parent_fromsnap_guid != 0 &&
3030 		    stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
3031 		    ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3032 		    (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3033 			nvlist_t *parent;
3034 			char tryname[ZFS_MAX_DATASET_NAME_LEN];
3035 
3036 			parent = fsavl_find(local_avl,
3037 			    stream_parent_fromsnap_guid, NULL);
3038 			/*
3039 			 * NB: parent might not be found if we used the
3040 			 * tosnap for stream_parent_fromsnap_guid,
3041 			 * because the parent is a newly-created fs;
3042 			 * we'll be able to rename it after we recv the
3043 			 * new fs.
3044 			 */
3045 			if (parent != NULL) {
3046 				char *pname;
3047 
3048 				VERIFY(0 == nvlist_lookup_string(parent, "name",
3049 				    &pname));
3050 				(void) snprintf(tryname, sizeof (tryname),
3051 				    "%s%s", pname, strrchr(stream_fsname, '/'));
3052 			} else {
3053 				tryname[0] = '\0';
3054 				if (flags->verbose) {
3055 					(void) printf("local fs %s new parent "
3056 					    "not found\n", fsname);
3057 				}
3058 			}
3059 
3060 			newname[0] = '\0';
3061 
3062 			error = recv_rename(hdl, fsname, tryname,
3063 			    strlen(tofs)+1, newname, flags);
3064 
3065 			if (renamed != NULL && newname[0] != '\0') {
3066 				VERIFY(0 == nvlist_add_boolean(renamed,
3067 				    newname));
3068 			}
3069 
3070 			if (error)
3071 				needagain = B_TRUE;
3072 			else
3073 				progress = B_TRUE;
3074 		}
3075 	}
3076 
3077 	fsavl_destroy(local_avl);
3078 	nvlist_free(local_nv);
3079 
3080 	if (needagain && progress) {
3081 		/* do another pass to fix up temporary names */
3082 		if (flags->verbose)
3083 			(void) printf("another pass:\n");
3084 		goto again;
3085 	}
3086 
3087 	return (needagain || error != 0);
3088 }
3089 
3090 static int
zfs_receive_package(libzfs_handle_t * hdl,int fd,const char * destname,recvflags_t * flags,dmu_replay_record_t * drr,zio_cksum_t * zc,char ** top_zfs,int cleanup_fd,uint64_t * action_handlep,nvlist_t * cmdprops)3091 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
3092     recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3093     char **top_zfs, int cleanup_fd, uint64_t *action_handlep,
3094     nvlist_t *cmdprops)
3095 {
3096 	nvlist_t *stream_nv = NULL;
3097 	avl_tree_t *stream_avl = NULL;
3098 	char *fromsnap = NULL;
3099 	char *sendsnap = NULL;
3100 	char *cp;
3101 	char tofs[ZFS_MAX_DATASET_NAME_LEN];
3102 	char sendfs[ZFS_MAX_DATASET_NAME_LEN];
3103 	char errbuf[1024];
3104 	dmu_replay_record_t drre;
3105 	int error;
3106 	boolean_t anyerr = B_FALSE;
3107 	boolean_t softerr = B_FALSE;
3108 	boolean_t recursive, raw;
3109 
3110 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3111 	    "cannot receive"));
3112 
3113 	assert(drr->drr_type == DRR_BEGIN);
3114 	assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
3115 	assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3116 	    DMU_COMPOUNDSTREAM);
3117 
3118 	/*
3119 	 * Read in the nvlist from the stream.
3120 	 */
3121 	if (drr->drr_payloadlen != 0) {
3122 		error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
3123 		    &stream_nv, flags->byteswap, zc);
3124 		if (error) {
3125 			error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3126 			goto out;
3127 		}
3128 	}
3129 
3130 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3131 	    ENOENT);
3132 	raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3133 
3134 	if (recursive && strchr(destname, '@')) {
3135 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3136 		    "cannot specify snapshot name for multi-snapshot stream"));
3137 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3138 		goto out;
3139 	}
3140 
3141 	/*
3142 	 * Read in the end record and verify checksum.
3143 	 */
3144 	if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3145 	    flags->byteswap, NULL)))
3146 		goto out;
3147 	if (flags->byteswap) {
3148 		drre.drr_type = BSWAP_32(drre.drr_type);
3149 		drre.drr_u.drr_end.drr_checksum.zc_word[0] =
3150 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3151 		drre.drr_u.drr_end.drr_checksum.zc_word[1] =
3152 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3153 		drre.drr_u.drr_end.drr_checksum.zc_word[2] =
3154 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3155 		drre.drr_u.drr_end.drr_checksum.zc_word[3] =
3156 		    BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3157 	}
3158 	if (drre.drr_type != DRR_END) {
3159 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3160 		goto out;
3161 	}
3162 	if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3163 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3164 		    "incorrect header checksum"));
3165 		error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3166 		goto out;
3167 	}
3168 
3169 	(void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
3170 
3171 	if (drr->drr_payloadlen != 0) {
3172 		nvlist_t *stream_fss;
3173 
3174 		VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
3175 		    &stream_fss));
3176 		if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
3177 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3178 			    "couldn't allocate avl tree"));
3179 			error = zfs_error(hdl, EZFS_NOMEM, errbuf);
3180 			goto out;
3181 		}
3182 
3183 		if (fromsnap != NULL && recursive) {
3184 			nvlist_t *renamed = NULL;
3185 			nvpair_t *pair = NULL;
3186 
3187 			(void) strlcpy(tofs, destname, sizeof (tofs));
3188 			if (flags->isprefix) {
3189 				struct drr_begin *drrb = &drr->drr_u.drr_begin;
3190 				int i;
3191 
3192 				if (flags->istail) {
3193 					cp = strrchr(drrb->drr_toname, '/');
3194 					if (cp == NULL) {
3195 						(void) strlcat(tofs, "/",
3196 						    sizeof (tofs));
3197 						i = 0;
3198 					} else {
3199 						i = (cp - drrb->drr_toname);
3200 					}
3201 				} else {
3202 					i = strcspn(drrb->drr_toname, "/@");
3203 				}
3204 				/* zfs_receive_one() will create_parents() */
3205 				(void) strlcat(tofs, &drrb->drr_toname[i],
3206 				    sizeof (tofs));
3207 				*strchr(tofs, '@') = '\0';
3208 			}
3209 
3210 			if (!flags->dryrun && !flags->nomount) {
3211 				VERIFY(0 == nvlist_alloc(&renamed,
3212 				    NV_UNIQUE_NAME, 0));
3213 			}
3214 
3215 			softerr = recv_incremental_replication(hdl, tofs, flags,
3216 			    stream_nv, stream_avl, renamed);
3217 
3218 			/* Unmount renamed filesystems before receiving. */
3219 			while ((pair = nvlist_next_nvpair(renamed,
3220 			    pair)) != NULL) {
3221 				zfs_handle_t *zhp;
3222 				prop_changelist_t *clp = NULL;
3223 
3224 				zhp = zfs_open(hdl, nvpair_name(pair),
3225 				    ZFS_TYPE_FILESYSTEM);
3226 				if (zhp != NULL) {
3227 					clp = changelist_gather(zhp,
3228 					    ZFS_PROP_MOUNTPOINT, 0, 0);
3229 					zfs_close(zhp);
3230 					if (clp != NULL) {
3231 						softerr |=
3232 						    changelist_prefix(clp);
3233 						changelist_free(clp);
3234 					}
3235 				}
3236 			}
3237 
3238 			nvlist_free(renamed);
3239 		}
3240 	}
3241 
3242 	/*
3243 	 * Get the fs specified by the first path in the stream (the top level
3244 	 * specified by 'zfs send') and pass it to each invocation of
3245 	 * zfs_receive_one().
3246 	 */
3247 	(void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
3248 	    sizeof (sendfs));
3249 	if ((cp = strchr(sendfs, '@')) != NULL) {
3250 		*cp = '\0';
3251 		/*
3252 		 * Find the "sendsnap", the final snapshot in a replication
3253 		 * stream.  zfs_receive_one() handles certain errors
3254 		 * differently, depending on if the contained stream is the
3255 		 * last one or not.
3256 		 */
3257 		sendsnap = (cp + 1);
3258 	}
3259 
3260 	/* Finally, receive each contained stream */
3261 	do {
3262 		/*
3263 		 * we should figure out if it has a recoverable
3264 		 * error, in which case do a recv_skip() and drive on.
3265 		 * Note, if we fail due to already having this guid,
3266 		 * zfs_receive_one() will take care of it (ie,
3267 		 * recv_skip() and return 0).
3268 		 */
3269 		error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3270 		    sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
3271 		    action_handlep, sendsnap, cmdprops);
3272 		if (error == ENODATA) {
3273 			error = 0;
3274 			break;
3275 		}
3276 		anyerr |= error;
3277 	} while (error == 0);
3278 
3279 	if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
3280 		/*
3281 		 * Now that we have the fs's they sent us, try the
3282 		 * renames again.
3283 		 */
3284 		softerr = recv_incremental_replication(hdl, tofs, flags,
3285 		    stream_nv, stream_avl, NULL);
3286 	}
3287 
3288 	if (raw && softerr == 0) {
3289 		softerr = recv_fix_encryption_hierarchy(hdl, destname,
3290 		    stream_nv, stream_avl);
3291 	}
3292 
3293 out:
3294 	fsavl_destroy(stream_avl);
3295 	nvlist_free(stream_nv);
3296 	if (softerr)
3297 		error = -2;
3298 	if (anyerr)
3299 		error = -1;
3300 	return (error);
3301 }
3302 
3303 static void
trunc_prop_errs(int truncated)3304 trunc_prop_errs(int truncated)
3305 {
3306 	ASSERT(truncated != 0);
3307 
3308 	if (truncated == 1)
3309 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3310 		    "1 more property could not be set\n"));
3311 	else
3312 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3313 		    "%d more properties could not be set\n"), truncated);
3314 }
3315 
3316 static int
recv_skip(libzfs_handle_t * hdl,int fd,boolean_t byteswap)3317 recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
3318 {
3319 	dmu_replay_record_t *drr;
3320 	void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
3321 	char errbuf[1024];
3322 
3323 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3324 	    "cannot receive:"));
3325 
3326 	/* XXX would be great to use lseek if possible... */
3327 	drr = buf;
3328 
3329 	while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3330 	    byteswap, NULL) == 0) {
3331 		if (byteswap)
3332 			drr->drr_type = BSWAP_32(drr->drr_type);
3333 
3334 		switch (drr->drr_type) {
3335 		case DRR_BEGIN:
3336 			if (drr->drr_payloadlen != 0) {
3337 				(void) recv_read(hdl, fd, buf,
3338 				    drr->drr_payloadlen, B_FALSE, NULL);
3339 			}
3340 			break;
3341 
3342 		case DRR_END:
3343 			free(buf);
3344 			return (0);
3345 
3346 		case DRR_OBJECT:
3347 			if (byteswap) {
3348 				drr->drr_u.drr_object.drr_bonuslen =
3349 				    BSWAP_32(drr->drr_u.drr_object.
3350 				    drr_bonuslen);
3351 			}
3352 			(void) recv_read(hdl, fd, buf,
3353 			    P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
3354 			    B_FALSE, NULL);
3355 			break;
3356 
3357 		case DRR_WRITE:
3358 			if (byteswap) {
3359 				drr->drr_u.drr_write.drr_logical_size =
3360 				    BSWAP_64(
3361 				    drr->drr_u.drr_write.drr_logical_size);
3362 				drr->drr_u.drr_write.drr_compressed_size =
3363 				    BSWAP_64(
3364 				    drr->drr_u.drr_write.drr_compressed_size);
3365 			}
3366 			uint64_t payload_size =
3367 			    DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3368 			assert(payload_size <= SPA_MAXBLOCKSIZE);
3369 			(void) recv_read(hdl, fd, buf,
3370 			    payload_size, B_FALSE, NULL);
3371 			break;
3372 		case DRR_SPILL:
3373 			if (byteswap) {
3374 				drr->drr_u.drr_spill.drr_length =
3375 				    BSWAP_64(drr->drr_u.drr_spill.drr_length);
3376 			}
3377 			(void) recv_read(hdl, fd, buf,
3378 			    drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
3379 			break;
3380 		case DRR_WRITE_EMBEDDED:
3381 			if (byteswap) {
3382 				drr->drr_u.drr_write_embedded.drr_psize =
3383 				    BSWAP_32(drr->drr_u.drr_write_embedded.
3384 				    drr_psize);
3385 			}
3386 			(void) recv_read(hdl, fd, buf,
3387 			    P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
3388 			    8), B_FALSE, NULL);
3389 			break;
3390 		case DRR_WRITE_BYREF:
3391 		case DRR_FREEOBJECTS:
3392 		case DRR_FREE:
3393 			break;
3394 
3395 		default:
3396 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3397 			    "invalid record type"));
3398 			return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3399 		}
3400 	}
3401 
3402 	free(buf);
3403 	return (-1);
3404 }
3405 
3406 static void
recv_ecksum_set_aux(libzfs_handle_t * hdl,const char * target_snap,boolean_t resumable)3407 recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
3408     boolean_t resumable)
3409 {
3410 	char target_fs[ZFS_MAX_DATASET_NAME_LEN];
3411 
3412 	zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3413 	    "checksum mismatch or incomplete stream"));
3414 
3415 	if (!resumable)
3416 		return;
3417 	(void) strlcpy(target_fs, target_snap, sizeof (target_fs));
3418 	*strchr(target_fs, '@') = '\0';
3419 	zfs_handle_t *zhp = zfs_open(hdl, target_fs,
3420 	    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3421 	if (zhp == NULL)
3422 		return;
3423 
3424 	char token_buf[ZFS_MAXPROPLEN];
3425 	int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3426 	    token_buf, sizeof (token_buf),
3427 	    NULL, NULL, 0, B_TRUE);
3428 	if (error == 0) {
3429 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3430 		    "checksum mismatch or incomplete stream.\n"
3431 		    "Partially received snapshot is saved.\n"
3432 		    "A resuming stream can be generated on the sending "
3433 		    "system by running:\n"
3434 		    "    zfs send -t %s"),
3435 		    token_buf);
3436 	}
3437 	zfs_close(zhp);
3438 }
3439 
3440 /*
3441  * Prepare a new nvlist of properties that are to override (-o) or be excluded
3442  * (-x) from the received dataset
3443  * recvprops: received properties from the send stream
3444  * cmdprops: raw input properties from command line
3445  * origprops: properties, both locally-set and received, currently set on the
3446  *            target dataset if it exists, NULL otherwise.
3447  * oxprops: valid output override (-o) and excluded (-x) properties
3448  */
3449 static int
zfs_setup_cmdline_props(libzfs_handle_t * hdl,zfs_type_t type,char * fsname,boolean_t zoned,boolean_t recursive,boolean_t newfs,boolean_t raw,boolean_t toplevel,nvlist_t * recvprops,nvlist_t * cmdprops,nvlist_t * origprops,nvlist_t ** oxprops,uint8_t ** wkeydata_out,uint_t * wkeylen_out,const char * errbuf)3450 zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
3451     char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
3452     boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
3453     nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
3454     uint_t *wkeylen_out, const char *errbuf)
3455 {
3456 	nvpair_t *nvp;
3457 	nvlist_t *oprops, *voprops;
3458 	zfs_handle_t *zhp = NULL;
3459 	zpool_handle_t *zpool_hdl = NULL;
3460 	char *cp;
3461 	int ret = 0;
3462 	char namebuf[ZFS_MAX_DATASET_NAME_LEN];
3463 
3464 	if (nvlist_empty(cmdprops))
3465 		return (0); /* No properties to override or exclude */
3466 
3467 	*oxprops = fnvlist_alloc();
3468 	oprops = fnvlist_alloc();
3469 
3470 	strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
3471 
3472 	/*
3473 	 * Get our dataset handle. The target dataset may not exist yet.
3474 	 */
3475 	if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
3476 		zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
3477 		if (zhp == NULL) {
3478 			ret = -1;
3479 			goto error;
3480 		}
3481 	}
3482 
3483 	/* open the zpool handle */
3484 	cp = strchr(namebuf, '/');
3485 	if (cp != NULL)
3486 		*cp = '\0';
3487 	zpool_hdl = zpool_open(hdl, namebuf);
3488 	if (zpool_hdl == NULL) {
3489 		ret = -1;
3490 		goto error;
3491 	}
3492 
3493 	/* restore namebuf to match fsname for later use */
3494 	if (cp != NULL)
3495 		*cp = '/';
3496 
3497 	/*
3498 	 * first iteration: process excluded (-x) properties now and gather
3499 	 * added (-o) properties to be later processed by zfs_valid_proplist()
3500 	 */
3501 	nvp = NULL;
3502 	while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
3503 		const char *name = nvpair_name(nvp);
3504 		zfs_prop_t prop = zfs_name_to_prop(name);
3505 
3506 		/* "origin" is processed separately, don't handle it here */
3507 		if (prop == ZFS_PROP_ORIGIN)
3508 			continue;
3509 
3510 		/*
3511 		 * we're trying to override or exclude a property that does not
3512 		 * make sense for this type of dataset, but we don't want to
3513 		 * fail if the receive is recursive: this comes in handy when
3514 		 * the send stream contains, for instance, a child ZVOL and
3515 		 * we're trying to receive it with "-o atime=on"
3516 		 */
3517 		if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
3518 		    !zfs_prop_user(name)) {
3519 			if (recursive)
3520 				continue;
3521 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3522 			    "property '%s' does not apply to datasets of this "
3523 			    "type"), name);
3524 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3525 			goto error;
3526 		}
3527 
3528 		/* raw streams can't override encryption properties */
3529 		if ((zfs_prop_encryption_key_param(prop) ||
3530 		    prop == ZFS_PROP_ENCRYPTION) && raw) {
3531 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3532 			    "encryption property '%s' cannot "
3533 			    "be set or excluded for raw streams."), name);
3534 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3535 			goto error;
3536 		}
3537 
3538 		/* incremental streams can only exclude encryption properties */
3539 		if ((zfs_prop_encryption_key_param(prop) ||
3540 		    prop == ZFS_PROP_ENCRYPTION) && !newfs &&
3541 		    nvpair_type(nvp) != DATA_TYPE_BOOLEAN) {
3542 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3543 			    "encryption property '%s' cannot "
3544 			    "be set for incremental streams."), name);
3545 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3546 			goto error;
3547 		}
3548 
3549 		switch (nvpair_type(nvp)) {
3550 		case DATA_TYPE_BOOLEAN: /* -x property */
3551 			/*
3552 			 * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
3553 			 * a property: this is done by forcing an explicit
3554 			 * inherit on the destination so the effective value is
3555 			 * not the one we received from the send stream.
3556 			 * We do this only if the property is not already
3557 			 * locally-set, in which case its value will take
3558 			 * priority over the received anyway.
3559 			 */
3560 			if (nvlist_exists(origprops, name)) {
3561 				nvlist_t *attrs;
3562 				char  *source = NULL;
3563 
3564 				attrs = fnvlist_lookup_nvlist(origprops, name);
3565 				if (nvlist_lookup_string(attrs,
3566 				    ZPROP_SOURCE, &source) == 0 &&
3567 				    strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
3568 					continue;
3569 			}
3570 			/*
3571 			 * We can't force an explicit inherit on non-inheritable
3572 			 * properties: if we're asked to exclude this kind of
3573 			 * values we remove them from "recvprops" input nvlist.
3574 			 */
3575 			if (!zfs_prop_inheritable(prop) &&
3576 			    !zfs_prop_user(name) && /* can be inherited too */
3577 			    nvlist_exists(recvprops, name))
3578 				fnvlist_remove(recvprops, name);
3579 			else
3580 				fnvlist_add_nvpair(*oxprops, nvp);
3581 			break;
3582 		case DATA_TYPE_STRING: /* -o property=value */
3583 			fnvlist_add_nvpair(oprops, nvp);
3584 			break;
3585 		default:
3586 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3587 			    "property '%s' must be a string or boolean"), name);
3588 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3589 			goto error;
3590 		}
3591 	}
3592 
3593 	if (toplevel) {
3594 		/* convert override strings properties to native */
3595 		if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
3596 		    oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
3597 			ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
3598 			goto error;
3599 		}
3600 
3601 		/*
3602 		 * zfs_crypto_create() requires the parent name. Get it
3603 		 * by truncating the fsname copy stored in namebuf.
3604 		 */
3605 		cp = strrchr(namebuf, '/');
3606 		if (cp != NULL)
3607 			*cp = '\0';
3608 
3609 		if (!raw && zfs_crypto_create(hdl, namebuf, voprops, NULL,
3610 		    B_FALSE, wkeydata_out, wkeylen_out) != 0) {
3611 			fnvlist_free(voprops);
3612 			ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
3613 			goto error;
3614 		}
3615 
3616 		/* second pass: process "-o" properties */
3617 		fnvlist_merge(*oxprops, voprops);
3618 		fnvlist_free(voprops);
3619 	} else {
3620 		/* override props on child dataset are inherited */
3621 		nvp = NULL;
3622 		while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
3623 			const char *name = nvpair_name(nvp);
3624 			fnvlist_add_boolean(*oxprops, name);
3625 		}
3626 	}
3627 
3628 error:
3629 	if (zhp != NULL)
3630 		zfs_close(zhp);
3631 	if (zpool_hdl != NULL)
3632 		zpool_close(zpool_hdl);
3633 	fnvlist_free(oprops);
3634 	return (ret);
3635 }
3636 
3637 /*
3638  * Restores a backup of tosnap from the file descriptor specified by infd.
3639  */
3640 static int
zfs_receive_one(libzfs_handle_t * hdl,int infd,const char * tosnap,const char * originsnap,recvflags_t * flags,dmu_replay_record_t * drr,dmu_replay_record_t * drr_noswap,const char * sendfs,nvlist_t * stream_nv,avl_tree_t * stream_avl,char ** top_zfs,int cleanup_fd,uint64_t * action_handlep,const char * finalsnap,nvlist_t * cmdprops)3641 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3642     const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3643     dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3644     avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3645     uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops)
3646 {
3647 	time_t begin_time;
3648 	int ioctl_err, ioctl_errno, err;
3649 	char *cp;
3650 	struct drr_begin *drrb = &drr->drr_u.drr_begin;
3651 	char errbuf[1024];
3652 	const char *chopprefix;
3653 	boolean_t newfs = B_FALSE;
3654 	boolean_t stream_wantsnewfs;
3655 	boolean_t newprops = B_FALSE;
3656 	uint64_t read_bytes = 0;
3657 	uint64_t errflags = 0;
3658 	uint64_t parent_snapguid = 0;
3659 	prop_changelist_t *clp = NULL;
3660 	nvlist_t *snapprops_nvlist = NULL;
3661 	nvlist_t *snapholds_nvlist = NULL;
3662 	zprop_errflags_t prop_errflags;
3663 	nvlist_t *prop_errors = NULL;
3664 	boolean_t recursive;
3665 	char *snapname = NULL;
3666 	char destsnap[MAXPATHLEN * 2];
3667 	char origin[MAXNAMELEN];
3668 	char name[MAXPATHLEN];
3669 	char tmp_keylocation[MAXNAMELEN];
3670 	nvlist_t *rcvprops = NULL; /* props received from the send stream */
3671 	nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
3672 	nvlist_t *origprops = NULL; /* original props (if destination exists) */
3673 	zfs_type_t type;
3674 	boolean_t toplevel = B_FALSE;
3675 	boolean_t zoned = B_FALSE;
3676 	boolean_t hastoken = B_FALSE;
3677 	uint8_t *wkeydata = NULL;
3678 	uint_t wkeylen = 0;
3679 
3680 	begin_time = time(NULL);
3681 	bzero(origin, MAXNAMELEN);
3682 	bzero(tmp_keylocation, MAXNAMELEN);
3683 
3684 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3685 	    "cannot receive"));
3686 
3687 	recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3688 	    ENOENT);
3689 
3690 	/* Did the user request holds be skipped via zfs recv -k? */
3691 	boolean_t holds = flags->holds && !flags->skipholds;
3692 
3693 	if (stream_avl != NULL) {
3694 		char *keylocation = NULL;
3695 		nvlist_t *lookup = NULL;
3696 		nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3697 		    &snapname);
3698 
3699 		(void) nvlist_lookup_uint64(fs, "parentfromsnap",
3700 		    &parent_snapguid);
3701 		err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
3702 		if (err) {
3703 			VERIFY(0 == nvlist_alloc(&rcvprops, NV_UNIQUE_NAME, 0));
3704 			newprops = B_TRUE;
3705 		}
3706 		/*
3707 		 * The keylocation property may only be set on encryption roots,
3708 		 * but this dataset might not become an encryption root until
3709 		 * recv_fix_encryption_hierarchy() is called. That function
3710 		 * will fixup the keylocation anyway, so we temporarily unset
3711 		 * the keylocation for now to avoid any errors from the receive
3712 		 * ioctl.
3713 		 */
3714 		err = nvlist_lookup_string(rcvprops,
3715 		    zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
3716 		if (err == 0) {
3717 			(void) strcpy(tmp_keylocation, keylocation);
3718 			(void) nvlist_remove_all(rcvprops,
3719 			    zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
3720 		}
3721 
3722 		if (flags->canmountoff) {
3723 			VERIFY(0 == nvlist_add_uint64(rcvprops,
3724 			    zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3725 		} else if (newprops) {  /* nothing in rcvprops, eliminate it */
3726 			nvlist_free(rcvprops);
3727 			rcvprops = NULL;
3728 			newprops = B_FALSE;
3729 		}
3730 		if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
3731 			VERIFY(0 == nvlist_lookup_nvlist(lookup,
3732 			    snapname, &snapprops_nvlist));
3733 		}
3734 		if (holds) {
3735 			if (0 == nvlist_lookup_nvlist(fs, "snapholds",
3736 			    &lookup)) {
3737 				VERIFY(0 == nvlist_lookup_nvlist(lookup,
3738 				    snapname, &snapholds_nvlist));
3739 			}
3740 		}
3741 	}
3742 
3743 	cp = NULL;
3744 
3745 	/*
3746 	 * Determine how much of the snapshot name stored in the stream
3747 	 * we are going to tack on to the name they specified on the
3748 	 * command line, and how much we are going to chop off.
3749 	 *
3750 	 * If they specified a snapshot, chop the entire name stored in
3751 	 * the stream.
3752 	 */
3753 	if (flags->istail) {
3754 		/*
3755 		 * A filesystem was specified with -e. We want to tack on only
3756 		 * the tail of the sent snapshot path.
3757 		 */
3758 		if (strchr(tosnap, '@')) {
3759 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3760 			    "argument - snapshot not allowed with -e"));
3761 			err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3762 			goto out;
3763 		}
3764 
3765 		chopprefix = strrchr(sendfs, '/');
3766 
3767 		if (chopprefix == NULL) {
3768 			/*
3769 			 * The tail is the poolname, so we need to
3770 			 * prepend a path separator.
3771 			 */
3772 			int len = strlen(drrb->drr_toname);
3773 			cp = malloc(len + 2);
3774 			cp[0] = '/';
3775 			(void) strcpy(&cp[1], drrb->drr_toname);
3776 			chopprefix = cp;
3777 		} else {
3778 			chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3779 		}
3780 	} else if (flags->isprefix) {
3781 		/*
3782 		 * A filesystem was specified with -d. We want to tack on
3783 		 * everything but the first element of the sent snapshot path
3784 		 * (all but the pool name).
3785 		 */
3786 		if (strchr(tosnap, '@')) {
3787 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3788 			    "argument - snapshot not allowed with -d"));
3789 			err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3790 			goto out;
3791 		}
3792 
3793 		chopprefix = strchr(drrb->drr_toname, '/');
3794 		if (chopprefix == NULL)
3795 			chopprefix = strchr(drrb->drr_toname, '@');
3796 	} else if (strchr(tosnap, '@') == NULL) {
3797 		/*
3798 		 * If a filesystem was specified without -d or -e, we want to
3799 		 * tack on everything after the fs specified by 'zfs send'.
3800 		 */
3801 		chopprefix = drrb->drr_toname + strlen(sendfs);
3802 	} else {
3803 		/* A snapshot was specified as an exact path (no -d or -e). */
3804 		if (recursive) {
3805 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3806 			    "cannot specify snapshot name for multi-snapshot "
3807 			    "stream"));
3808 			err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3809 			goto out;
3810 		}
3811 		chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
3812 	}
3813 
3814 	ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3815 	ASSERT(chopprefix > drrb->drr_toname);
3816 	ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
3817 	ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3818 	    chopprefix[0] == '\0');
3819 
3820 	/*
3821 	 * Determine name of destination snapshot, store in zc_value.
3822 	 */
3823 	(void) strlcpy(destsnap, tosnap, sizeof (destsnap));
3824 	(void) strlcat(destsnap, chopprefix, sizeof (destsnap));
3825 	free(cp);
3826 	if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
3827 		err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3828 		goto out;
3829 	}
3830 
3831 	/*
3832 	 * Determine the name of the origin snapshot, store in zc_string.
3833 	 */
3834 	if (originsnap) {
3835 		(void) strlcpy(origin, originsnap, sizeof (origin));
3836 		if (flags->verbose)
3837 			(void) printf("using provided clone origin %s\n",
3838 			    origin);
3839 	} else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3840 		if (guid_to_name(hdl, destsnap,
3841 		    drrb->drr_fromguid, B_FALSE, origin) != 0) {
3842 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3843 			    "local origin for clone %s does not exist"),
3844 			    destsnap);
3845 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
3846 			goto out;
3847 		}
3848 		if (flags->verbose)
3849 			(void) printf("found clone origin %s\n", origin);
3850 	}
3851 
3852 	boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3853 	    DMU_BACKUP_FEATURE_RESUMING;
3854 	boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3855 	    DMU_BACKUP_FEATURE_RAW;
3856 	boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3857 	    DMU_BACKUP_FEATURE_EMBED_DATA;
3858 	stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
3859 	    (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3860 
3861 	if (stream_wantsnewfs) {
3862 		/*
3863 		 * if the parent fs does not exist, look for it based on
3864 		 * the parent snap GUID
3865 		 */
3866 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3867 		    "cannot receive new filesystem stream"));
3868 
3869 		(void) strcpy(name, destsnap);
3870 		cp = strrchr(name, '/');
3871 		if (cp)
3872 			*cp = '\0';
3873 		if (cp &&
3874 		    !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
3875 			char suffix[ZFS_MAX_DATASET_NAME_LEN];
3876 			(void) strcpy(suffix, strrchr(destsnap, '/'));
3877 			if (guid_to_name(hdl, name, parent_snapguid,
3878 			    B_FALSE, destsnap) == 0) {
3879 				*strchr(destsnap, '@') = '\0';
3880 				(void) strcat(destsnap, suffix);
3881 			}
3882 		}
3883 	} else {
3884 		/*
3885 		 * if the fs does not exist, look for it based on the
3886 		 * fromsnap GUID
3887 		 */
3888 		(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3889 		    "cannot receive incremental stream"));
3890 
3891 		(void) strcpy(name, destsnap);
3892 		*strchr(name, '@') = '\0';
3893 
3894 		/*
3895 		 * If the exact receive path was specified and this is the
3896 		 * topmost path in the stream, then if the fs does not exist we
3897 		 * should look no further.
3898 		 */
3899 		if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
3900 		    strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
3901 		    !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
3902 			char snap[ZFS_MAX_DATASET_NAME_LEN];
3903 			(void) strcpy(snap, strchr(destsnap, '@'));
3904 			if (guid_to_name(hdl, name, drrb->drr_fromguid,
3905 			    B_FALSE, destsnap) == 0) {
3906 				*strchr(destsnap, '@') = '\0';
3907 				(void) strcat(destsnap, snap);
3908 			}
3909 		}
3910 	}
3911 
3912 	(void) strcpy(name, destsnap);
3913 	*strchr(name, '@') = '\0';
3914 
3915 	if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
3916 		zfs_cmd_t zc = { 0 };
3917 		zfs_handle_t *zhp;
3918 		boolean_t encrypted;
3919 
3920 		(void) strcpy(zc.zc_name, name);
3921 
3922 		/*
3923 		 * Destination fs exists.  It must be one of these cases:
3924 		 *  - an incremental send stream
3925 		 *  - the stream specifies a new fs (full stream or clone)
3926 		 *    and they want us to blow away the existing fs (and
3927 		 *    have therefore specified -F and removed any snapshots)
3928 		 *  - we are resuming a failed receive.
3929 		 */
3930 		if (stream_wantsnewfs) {
3931 			if (!flags->force) {
3932 				zcmd_free_nvlists(&zc);
3933 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3934 				    "destination '%s' exists\n"
3935 				    "must specify -F to overwrite it"), name);
3936 				err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3937 				goto out;
3938 			}
3939 			if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3940 			    &zc) == 0) {
3941 				zcmd_free_nvlists(&zc);
3942 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3943 				    "destination has snapshots (eg. %s)\n"
3944 				    "must destroy them to overwrite it"),
3945 				    zc.zc_name);
3946 				err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3947 				goto out;
3948 			}
3949 		}
3950 
3951 		if ((zhp = zfs_open(hdl, name,
3952 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
3953 			zcmd_free_nvlists(&zc);
3954 			err = -1;
3955 			goto out;
3956 		}
3957 
3958 		if (stream_wantsnewfs &&
3959 		    zhp->zfs_dmustats.dds_origin[0]) {
3960 			zcmd_free_nvlists(&zc);
3961 			zfs_close(zhp);
3962 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3963 			    "destination '%s' is a clone\n"
3964 			    "must destroy it to overwrite it"), name);
3965 			err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3966 			goto out;
3967 		}
3968 
3969 		/*
3970 		 * Raw sends can not be performed as an incremental on top
3971 		 * of existing unencrypted datasets. zfs recv -F cant be
3972 		 * used to blow away an existing encrypted filesystem. This
3973 		 * is because it would require the dsl dir to point to the
3974 		 * new key (or lack of a key) and the old key at the same
3975 		 * time. The -F flag may still be used for deleting
3976 		 * intermediate snapshots that would otherwise prevent the
3977 		 * receive from working.
3978 		 */
3979 		encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
3980 		    ZIO_CRYPT_OFF;
3981 		if (!stream_wantsnewfs && !encrypted && raw) {
3982 			zfs_close(zhp);
3983 			zcmd_free_nvlists(&zc);
3984 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3985 			    "cannot perform raw receive on top of "
3986 			    "existing unencrypted dataset"));
3987 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3988 			goto out;
3989 		}
3990 
3991 		if (stream_wantsnewfs && flags->force &&
3992 		    ((raw && !encrypted) || encrypted)) {
3993 			zfs_close(zhp);
3994 			zcmd_free_nvlists(&zc);
3995 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3996 			    "zfs receive -F cannot be used to destroy an "
3997 			    "encrypted filesystem or overwrite an "
3998 			    "unencrypted one with an encrypted one"));
3999 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4000 			goto out;
4001 		}
4002 
4003 		if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
4004 		    stream_wantsnewfs) {
4005 			/* We can't do online recv in this case */
4006 			clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
4007 			if (clp == NULL) {
4008 				zfs_close(zhp);
4009 				err = -1;
4010 				goto out;
4011 			}
4012 			if (changelist_prefix(clp) != 0) {
4013 				changelist_free(clp);
4014 				zfs_close(zhp);
4015 				err = -1;
4016 				goto out;
4017 			}
4018 		}
4019 
4020 		/*
4021 		 * If we are resuming a newfs, set newfs here so that we will
4022 		 * mount it if the recv succeeds this time.  We can tell
4023 		 * that it was a newfs on the first recv because the fs
4024 		 * itself will be inconsistent (if the fs existed when we
4025 		 * did the first recv, we would have received it into
4026 		 * .../%recv).
4027 		 */
4028 		if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
4029 			newfs = B_TRUE;
4030 
4031 		/* we want to know if we're zoned when validating -o|-x props */
4032 		zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
4033 
4034 		/* may need this info later, get it now we have zhp around */
4035 		if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
4036 		    NULL, NULL, 0, B_TRUE) == 0)
4037 			hastoken = B_TRUE;
4038 
4039 		/* gather existing properties on destination */
4040 		origprops = fnvlist_alloc();
4041 		fnvlist_merge(origprops, zhp->zfs_props);
4042 		fnvlist_merge(origprops, zhp->zfs_user_props);
4043 
4044 		zfs_close(zhp);
4045 		cp = NULL;
4046 	} else {
4047 		zfs_handle_t *zhp;
4048 
4049 		/*
4050 		 * Destination filesystem does not exist.  Therefore we better
4051 		 * be creating a new filesystem (either from a full backup, or
4052 		 * a clone).  It would therefore be invalid if the user
4053 		 * specified only the pool name (i.e. if the destination name
4054 		 * contained no slash character).
4055 		 */
4056 		cp = strrchr(name, '/');
4057 
4058 		if (!stream_wantsnewfs || cp == NULL) {
4059 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4060 			    "destination '%s' does not exist"), name);
4061 			err = zfs_error(hdl, EZFS_NOENT, errbuf);
4062 			goto out;
4063 		}
4064 
4065 		/*
4066 		 * Trim off the final dataset component so we perform the
4067 		 * recvbackup ioctl to the filesystems's parent.
4068 		 */
4069 		*cp = '\0';
4070 
4071 		if (flags->isprefix && !flags->istail && !flags->dryrun &&
4072 		    create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
4073 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4074 			goto out;
4075 		}
4076 
4077 		/* validate parent */
4078 		zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
4079 		if (zhp == NULL) {
4080 			err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4081 			goto out;
4082 		}
4083 		if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
4084 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4085 			    "parent '%s' is not a filesystem"), name);
4086 			err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
4087 			zfs_close(zhp);
4088 			goto out;
4089 		}
4090 
4091 		/*
4092 		 * It is invalid to receive a properties stream that was
4093 		 * unencrypted on the send side as a child of an encrypted
4094 		 * parent. Technically there is nothing preventing this, but
4095 		 * it would mean that the encryption=off property which is
4096 		 * locally set on the send side would not be received correctly.
4097 		 * We can infer encryption=off if the stream is not raw and
4098 		 * properties were included since the send side will only ever
4099 		 * send the encryption property in a raw nvlist header. This
4100 		 * check will be avoided if the user specifically overrides
4101 		 * the encryption property on the command line.
4102 		 */
4103 		if (!raw && rcvprops != NULL &&
4104 		    !nvlist_exists(cmdprops,
4105 		    zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
4106 			uint64_t crypt;
4107 
4108 			crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
4109 
4110 			if (crypt != ZIO_CRYPT_OFF) {
4111 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4112 				    "parent '%s' must not be encrypted to "
4113 				    "receive unenecrypted property"), name);
4114 				err = zfs_error(hdl, EZFS_BADPROP, errbuf);
4115 				zfs_close(zhp);
4116 				goto out;
4117 			}
4118 		}
4119 		zfs_close(zhp);
4120 
4121 		newfs = B_TRUE;
4122 		*cp = '/';
4123 	}
4124 
4125 	if (flags->verbose) {
4126 		(void) printf("%s %s stream of %s into %s\n",
4127 		    flags->dryrun ? "would receive" : "receiving",
4128 		    drrb->drr_fromguid ? "incremental" : "full",
4129 		    drrb->drr_toname, destsnap);
4130 		(void) fflush(stdout);
4131 	}
4132 
4133 	if (flags->dryrun) {
4134 		err = recv_skip(hdl, infd, flags->byteswap);
4135 		goto out;
4136 	}
4137 
4138 	if (top_zfs && (*top_zfs == NULL || strcmp(*top_zfs, name) == 0))
4139 		toplevel = B_TRUE;
4140 	if (drrb->drr_type == DMU_OST_ZVOL) {
4141 		type = ZFS_TYPE_VOLUME;
4142 	} else if (drrb->drr_type == DMU_OST_ZFS) {
4143 		type = ZFS_TYPE_FILESYSTEM;
4144 	} else {
4145 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4146 		    "invalid record type: 0x%d"), drrb->drr_type);
4147 		err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4148 		goto out;
4149 	}
4150 	if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
4151 	    stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
4152 	    &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
4153 		goto out;
4154 
4155 	/*
4156 	 * The following is a difference between ZoL and illumos.
4157 	 *
4158 	 * On illumos, we must trim the last component of the dataset name
4159 	 * that is passed via the ioctl so that we can properly validate
4160 	 * zfs_secpolicy_recv() when receiving to a delegated dataset within
4161 	 * zone. This matches the historical behavior of the receive ioctl.
4162 	 * However,  we can't do this until after zfs_setup_cmdline_props()
4163 	 * has finished with the full name.
4164 	 */
4165 	if (cp != NULL)
4166 		*cp = '\0';
4167 
4168 	err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
4169 	    oxprops, wkeydata, wkeylen, origin, flags->force, flags->resumable,
4170 	    raw, infd, drr_noswap, cleanup_fd, &read_bytes, &errflags,
4171 	    action_handlep, &prop_errors);
4172 	ioctl_errno = errno;
4173 	prop_errflags = errflags;
4174 
4175 	if (err == 0) {
4176 		nvpair_t *prop_err = NULL;
4177 
4178 		while ((prop_err = nvlist_next_nvpair(prop_errors,
4179 		    prop_err)) != NULL) {
4180 			char tbuf[1024];
4181 			zfs_prop_t prop;
4182 			int intval;
4183 
4184 			prop = zfs_name_to_prop(nvpair_name(prop_err));
4185 			(void) nvpair_value_int32(prop_err, &intval);
4186 			if (strcmp(nvpair_name(prop_err),
4187 			    ZPROP_N_MORE_ERRORS) == 0) {
4188 				trunc_prop_errs(intval);
4189 				break;
4190 			} else if (snapname == NULL || finalsnap == NULL ||
4191 			    strcmp(finalsnap, snapname) == 0 ||
4192 			    strcmp(nvpair_name(prop_err),
4193 			    zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
4194 				/*
4195 				 * Skip the special case of, for example,
4196 				 * "refquota", errors on intermediate
4197 				 * snapshots leading up to a final one.
4198 				 * That's why we have all of the checks above.
4199 				 *
4200 				 * See zfs_ioctl.c's extract_delay_props() for
4201 				 * a list of props which can fail on
4202 				 * intermediate snapshots, but shouldn't
4203 				 * affect the overall receive.
4204 				 */
4205 				(void) snprintf(tbuf, sizeof (tbuf),
4206 				    dgettext(TEXT_DOMAIN,
4207 				    "cannot receive %s property on %s"),
4208 				    nvpair_name(prop_err), name);
4209 				zfs_setprop_error(hdl, prop, intval, tbuf);
4210 			}
4211 		}
4212 		nvlist_free(prop_errors);
4213 	}
4214 
4215 	if (err == 0 && snapprops_nvlist) {
4216 		zfs_cmd_t zc = { 0 };
4217 
4218 		(void) strcpy(zc.zc_name, destsnap);
4219 		zc.zc_cookie = B_TRUE; /* received */
4220 		if (zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist) == 0) {
4221 			(void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
4222 			zcmd_free_nvlists(&zc);
4223 		}
4224 	}
4225 	if (err == 0 && snapholds_nvlist) {
4226 		nvpair_t *pair;
4227 		nvlist_t *holds, *errors = NULL;
4228 		int cleanup_fd = -1;
4229 
4230 		VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
4231 		for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
4232 		    pair != NULL;
4233 		    pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
4234 			VERIFY(0 == nvlist_add_string(holds, destsnap,
4235 			    nvpair_name(pair)));
4236 		}
4237 		(void) lzc_hold(holds, cleanup_fd, &errors);
4238 		nvlist_free(snapholds_nvlist);
4239 		nvlist_free(holds);
4240 	}
4241 
4242 	if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
4243 		/*
4244 		 * It may be that this snapshot already exists,
4245 		 * in which case we want to consume & ignore it
4246 		 * rather than failing.
4247 		 */
4248 		avl_tree_t *local_avl;
4249 		nvlist_t *local_nv, *fs;
4250 		cp = strchr(destsnap, '@');
4251 
4252 		/*
4253 		 * XXX Do this faster by just iterating over snaps in
4254 		 * this fs.  Also if zc_value does not exist, we will
4255 		 * get a strange "does not exist" error message.
4256 		 */
4257 		*cp = '\0';
4258 		if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
4259 		    B_FALSE, B_FALSE, B_FALSE, B_TRUE,
4260 		    &local_nv, &local_avl) == 0) {
4261 			*cp = '@';
4262 			fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
4263 			fsavl_destroy(local_avl);
4264 			nvlist_free(local_nv);
4265 
4266 			if (fs != NULL) {
4267 				if (flags->verbose) {
4268 					(void) printf("snap %s already exists; "
4269 					    "ignoring\n", destsnap);
4270 				}
4271 				err = ioctl_err = recv_skip(hdl, infd,
4272 				    flags->byteswap);
4273 			}
4274 		}
4275 		*cp = '@';
4276 	}
4277 
4278 	if (ioctl_err != 0) {
4279 		switch (ioctl_errno) {
4280 		case ENODEV:
4281 			cp = strchr(destsnap, '@');
4282 			*cp = '\0';
4283 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4284 			    "most recent snapshot of %s does not\n"
4285 			    "match incremental source"), destsnap);
4286 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4287 			*cp = '@';
4288 			break;
4289 		case ETXTBSY:
4290 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4291 			    "destination %s has been modified\n"
4292 			    "since most recent snapshot"), name);
4293 			(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4294 			break;
4295 		case EACCES:
4296 			if (raw && stream_wantsnewfs) {
4297 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4298 				    "failed to create encryption key"));
4299 			} else if (raw && !stream_wantsnewfs) {
4300 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4301 				    "encryption key does not match "
4302 				    "existing key"));
4303 			} else {
4304 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4305 				    "inherited key must be loaded"));
4306 			}
4307 			(void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4308 			break;
4309 		case EEXIST:
4310 			cp = strchr(destsnap, '@');
4311 			if (newfs) {
4312 				/* it's the containing fs that exists */
4313 				*cp = '\0';
4314 			}
4315 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4316 			    "destination already exists"));
4317 			(void) zfs_error_fmt(hdl, EZFS_EXISTS,
4318 			    dgettext(TEXT_DOMAIN, "cannot restore to %s"),
4319 			    destsnap);
4320 			*cp = '@';
4321 			break;
4322 		case EINVAL:
4323 			if (embedded && !raw)
4324 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4325 				    "incompatible embedded data stream "
4326 				    "feature with encrypted receive."));
4327 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4328 			break;
4329 		case ECKSUM:
4330 			recv_ecksum_set_aux(hdl, destsnap, flags->resumable);
4331 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4332 			break;
4333 		case ENOTSUP:
4334 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4335 			    "pool must be upgraded to receive this stream."));
4336 			(void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
4337 			break;
4338 		case EDQUOT:
4339 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4340 			    "destination %s space quota exceeded."), name);
4341 			(void) zfs_error(hdl, EZFS_NOSPC, errbuf);
4342 			break;
4343 		case ZFS_ERR_FROM_IVSET_GUID_MISSING:
4344 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4345 			    "IV set guid missing. See errata %u at"
4346 			    "http://zfsonlinux.org/msg/ZFS-8000-ER"),
4347 			    ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
4348 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4349 			break;
4350 		case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
4351 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4352 			    "IV set guid mismatch. See the 'zfs receive' "
4353 			    "man page section\n discussing the limitations "
4354 			    "of raw encrypted send streams."));
4355 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4356 			break;
4357 		case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING:
4358 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4359 			    "Spill block flag missing for raw send.\n"
4360 			    "The zfs software on the sending system must "
4361 			    "be updated."));
4362 			(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4363 			break;
4364 		case EBUSY:
4365 			if (hastoken) {
4366 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4367 				    "destination %s contains "
4368 				    "partially-complete state from "
4369 				    "\"zfs receive -s\"."), name);
4370 				(void) zfs_error(hdl, EZFS_BUSY, errbuf);
4371 				break;
4372 			}
4373 			/* fallthru */
4374 		default:
4375 			(void) zfs_standard_error(hdl, ioctl_errno, errbuf);
4376 		}
4377 	}
4378 
4379 	/*
4380 	 * Mount the target filesystem (if created).  Also mount any
4381 	 * children of the target filesystem if we did a replication
4382 	 * receive (indicated by stream_avl being non-NULL).
4383 	 */
4384 	cp = strchr(destsnap, '@');
4385 	if (cp && (ioctl_err == 0 || !newfs)) {
4386 		zfs_handle_t *h;
4387 
4388 		*cp = '\0';
4389 		h = zfs_open(hdl, destsnap,
4390 		    ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
4391 		if (h != NULL) {
4392 			if (h->zfs_type == ZFS_TYPE_VOLUME) {
4393 				*cp = '@';
4394 			} else if (newfs || stream_avl) {
4395 				/*
4396 				 * Track the first/top of hierarchy fs,
4397 				 * for mounting and sharing later.
4398 				 */
4399 				if (top_zfs && *top_zfs == NULL)
4400 					*top_zfs = zfs_strdup(hdl, destsnap);
4401 			}
4402 			zfs_close(h);
4403 		}
4404 		*cp = '@';
4405 	}
4406 
4407 	if (clp) {
4408 		if (!flags->nomount)
4409 			err |= changelist_postfix(clp);
4410 		changelist_free(clp);
4411 	}
4412 
4413 	if (prop_errflags & ZPROP_ERR_NOCLEAR) {
4414 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4415 		    "failed to clear unreceived properties on %s"), name);
4416 		(void) fprintf(stderr, "\n");
4417 	}
4418 	if (prop_errflags & ZPROP_ERR_NORESTORE) {
4419 		(void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
4420 		    "failed to restore original properties on %s"), name);
4421 		(void) fprintf(stderr, "\n");
4422 	}
4423 
4424 	if (err || ioctl_err) {
4425 		err = -1;
4426 		goto out;
4427 	}
4428 
4429 	if (flags->verbose) {
4430 		char buf1[64];
4431 		char buf2[64];
4432 		uint64_t bytes = read_bytes;
4433 		time_t delta = time(NULL) - begin_time;
4434 		if (delta == 0)
4435 			delta = 1;
4436 		zfs_nicebytes(bytes, buf1, sizeof (buf1));
4437 		zfs_nicebytes(bytes / delta, buf2, sizeof (buf2));
4438 
4439 		(void) printf("received %s stream in %lu seconds (%s/sec)\n",
4440 		    buf1, delta, buf2);
4441 	}
4442 
4443 	err = 0;
4444 out:
4445 
4446 	if (tmp_keylocation[0] != '\0') {
4447 		VERIFY(0 == nvlist_add_string(rcvprops,
4448 		    zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation));
4449 	}
4450 
4451 	if (newprops)
4452 		nvlist_free(rcvprops);
4453 
4454 	nvlist_free(oxprops);
4455 	nvlist_free(origprops);
4456 
4457 	return (err);
4458 }
4459 
4460 /*
4461  * Check properties we were asked to override (both -o|-x)
4462  */
4463 static boolean_t
zfs_receive_checkprops(libzfs_handle_t * hdl,nvlist_t * props,const char * errbuf)4464 zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
4465     const char *errbuf)
4466 {
4467 	nvpair_t *nvp;
4468 	zfs_prop_t prop;
4469 	const char *name;
4470 
4471 	nvp = NULL;
4472 	while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
4473 		name = nvpair_name(nvp);
4474 		prop = zfs_name_to_prop(name);
4475 
4476 		if (prop == ZPROP_INVAL) {
4477 			if (!zfs_prop_user(name)) {
4478 				zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4479 				    "invalid property '%s'"), name);
4480 				return (B_FALSE);
4481 			}
4482 			continue;
4483 		}
4484 		/*
4485 		 * "origin" is readonly but is used to receive datasets as
4486 		 * clones so we don't raise an error here
4487 		 */
4488 		if (prop == ZFS_PROP_ORIGIN)
4489 			continue;
4490 
4491 		/* encryption params have their own verification later */
4492 		if (prop == ZFS_PROP_ENCRYPTION ||
4493 		    zfs_prop_encryption_key_param(prop))
4494 			continue;
4495 
4496 		/*
4497 		 * cannot override readonly, set-once and other specific
4498 		 * settable properties
4499 		 */
4500 		if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
4501 		    prop == ZFS_PROP_VOLSIZE) {
4502 			zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4503 			    "invalid property '%s'"), name);
4504 			return (B_FALSE);
4505 		}
4506 	}
4507 
4508 	return (B_TRUE);
4509 }
4510 
4511 static int
zfs_receive_impl(libzfs_handle_t * hdl,const char * tosnap,const char * originsnap,recvflags_t * flags,int infd,const char * sendfs,nvlist_t * stream_nv,avl_tree_t * stream_avl,char ** top_zfs,int cleanup_fd,uint64_t * action_handlep,const char * finalsnap,nvlist_t * cmdprops)4512 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
4513     const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
4514     nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
4515     uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops)
4516 {
4517 	int err;
4518 	dmu_replay_record_t drr, drr_noswap;
4519 	struct drr_begin *drrb = &drr.drr_u.drr_begin;
4520 	char errbuf[1024];
4521 	zio_cksum_t zcksum = { 0 };
4522 	uint64_t featureflags;
4523 	int hdrtype;
4524 
4525 	(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
4526 	    "cannot receive"));
4527 
4528 	/* check cmdline props, raise an error if they cannot be received */
4529 	if (!zfs_receive_checkprops(hdl, cmdprops, errbuf)) {
4530 		return (zfs_error(hdl, EZFS_BADPROP, errbuf));
4531 	}
4532 
4533 	if (flags->isprefix &&
4534 	    !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
4535 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
4536 		    "(%s) does not exist"), tosnap);
4537 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
4538 	}
4539 	if (originsnap &&
4540 	    !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
4541 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
4542 		    "(%s) does not exist"), originsnap);
4543 		return (zfs_error(hdl, EZFS_NOENT, errbuf));
4544 	}
4545 
4546 	/* read in the BEGIN record */
4547 	if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
4548 	    &zcksum)))
4549 		return (err);
4550 
4551 	if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
4552 		/* It's the double end record at the end of a package */
4553 		return (ENODATA);
4554 	}
4555 
4556 	/* the kernel needs the non-byteswapped begin record */
4557 	drr_noswap = drr;
4558 
4559 	flags->byteswap = B_FALSE;
4560 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
4561 		/*
4562 		 * We computed the checksum in the wrong byteorder in
4563 		 * recv_read() above; do it again correctly.
4564 		 */
4565 		bzero(&zcksum, sizeof (zio_cksum_t));
4566 		(void) fletcher_4_incremental_byteswap(&drr,
4567 		    sizeof (drr), &zcksum);
4568 		flags->byteswap = B_TRUE;
4569 
4570 		drr.drr_type = BSWAP_32(drr.drr_type);
4571 		drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
4572 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
4573 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
4574 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
4575 		drrb->drr_type = BSWAP_32(drrb->drr_type);
4576 		drrb->drr_flags = BSWAP_32(drrb->drr_flags);
4577 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
4578 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
4579 	}
4580 
4581 	if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
4582 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4583 		    "stream (bad magic number)"));
4584 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4585 	}
4586 
4587 	featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
4588 	hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
4589 
4590 	if (!DMU_STREAM_SUPPORTED(featureflags) ||
4591 	    (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
4592 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4593 		    "stream has unsupported feature, feature flags = %lx"),
4594 		    featureflags);
4595 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4596 	}
4597 
4598 	/* Holds feature is set once in the compound stream header. */
4599 	boolean_t holds = (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
4600 	    DMU_BACKUP_FEATURE_HOLDS);
4601 	if (holds)
4602 		flags->holds = B_TRUE;
4603 
4604 	if (strchr(drrb->drr_toname, '@') == NULL) {
4605 		zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
4606 		    "stream (bad snapshot name)"));
4607 		return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
4608 	}
4609 
4610 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
4611 		char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
4612 		if (sendfs == NULL) {
4613 			/*
4614 			 * We were not called from zfs_receive_package(). Get
4615 			 * the fs specified by 'zfs send'.
4616 			 */
4617 			char *cp;
4618 			(void) strlcpy(nonpackage_sendfs,
4619 			    drr.drr_u.drr_begin.drr_toname,
4620 			    sizeof (nonpackage_sendfs));
4621 			if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
4622 				*cp = '\0';
4623 			sendfs = nonpackage_sendfs;
4624 			VERIFY(finalsnap == NULL);
4625 		}
4626 		return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
4627 		    &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
4628 		    cleanup_fd, action_handlep, finalsnap, cmdprops));
4629 	} else {
4630 		assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
4631 		    DMU_COMPOUNDSTREAM);
4632 		return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
4633 		    &zcksum, top_zfs, cleanup_fd, action_handlep, cmdprops));
4634 	}
4635 }
4636 
4637 /*
4638  * Restores a backup of tosnap from the file descriptor specified by infd.
4639  * Return 0 on total success, -2 if some things couldn't be
4640  * destroyed/renamed/promoted, -1 if some things couldn't be received.
4641  * (-1 will override -2, if -1 and the resumable flag was specified the
4642  * transfer can be resumed if the sending side supports it).
4643  */
4644 int
zfs_receive(libzfs_handle_t * hdl,const char * tosnap,nvlist_t * props,recvflags_t * flags,int infd,avl_tree_t * stream_avl)4645 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
4646     recvflags_t *flags, int infd, avl_tree_t *stream_avl)
4647 {
4648 	char *top_zfs = NULL;
4649 	int err;
4650 	int cleanup_fd;
4651 	uint64_t action_handle = 0;
4652 	char *originsnap = NULL;
4653 	if (props) {
4654 		err = nvlist_lookup_string(props, "origin", &originsnap);
4655 		if (err && err != ENOENT)
4656 			return (err);
4657 	}
4658 
4659 	cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
4660 	VERIFY(cleanup_fd >= 0);
4661 
4662 	err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
4663 	    stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL, props);
4664 
4665 	VERIFY(0 == close(cleanup_fd));
4666 
4667 	if (err == 0 && !flags->nomount && top_zfs) {
4668 		zfs_handle_t *zhp;
4669 		prop_changelist_t *clp;
4670 
4671 		zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
4672 		if (zhp != NULL) {
4673 			clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
4674 			    CL_GATHER_MOUNT_ALWAYS, 0);
4675 			zfs_close(zhp);
4676 			if (clp != NULL) {
4677 				/* mount and share received datasets */
4678 				err = changelist_postfix(clp);
4679 				changelist_free(clp);
4680 			}
4681 		}
4682 		if (zhp == NULL || clp == NULL || err)
4683 			err = -1;
4684 	}
4685 	if (top_zfs)
4686 		free(top_zfs);
4687 
4688 	return (err);
4689 }
4690