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