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) 2012 Pawel Jakub Dawidek. All rights reserved.
27 * Copyright (c) 2013 Steven Hartland. All rights reserved.
28 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
29 * Copyright (c) 2014 Integros [integros.com]
30 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
31 */
32
33 #include <assert.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <libintl.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <strings.h>
40 #include <unistd.h>
41 #include <stddef.h>
42 #include <fcntl.h>
43 #include <sys/mount.h>
44 #include <pthread.h>
45 #include <umem.h>
46 #include <time.h>
47
48 #include <libzfs.h>
49 #include <libzfs_core.h>
50
51 #include "zfs_namecheck.h"
52 #include "zfs_prop.h"
53 #include "zfs_fletcher.h"
54 #include "libzfs_impl.h"
55 #include <zlib.h>
56 #include <sha2.h>
57 #include <sys/zio_checksum.h>
58 #include <sys/ddt.h>
59
60 /* in libzfs_dataset.c */
61 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
62
63 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
64 recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
65 uint64_t *, const char *);
66 static int guid_to_name(libzfs_handle_t *, const char *,
67 uint64_t, boolean_t, char *);
68
69 static const zio_cksum_t zero_cksum = { 0 };
70
71 typedef struct dedup_arg {
72 int inputfd;
73 int outputfd;
74 libzfs_handle_t *dedup_hdl;
75 } dedup_arg_t;
76
77 typedef struct progress_arg {
78 zfs_handle_t *pa_zhp;
79 int pa_fd;
80 boolean_t pa_parsable;
81 } progress_arg_t;
82
83 typedef struct dataref {
84 uint64_t ref_guid;
85 uint64_t ref_object;
86 uint64_t ref_offset;
87 } dataref_t;
88
89 typedef struct dedup_entry {
90 struct dedup_entry *dde_next;
91 zio_cksum_t dde_chksum;
92 uint64_t dde_prop;
93 dataref_t dde_ref;
170 uint64_t prop, dataref_t *dr)
171 {
172 uint32_t hashcode;
173 dedup_entry_t **ddepp;
174
175 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
176
177 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
178 ddepp = &((*ddepp)->dde_next)) {
179 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
180 (*ddepp)->dde_prop == prop) {
181 *dr = (*ddepp)->dde_ref;
182 return (B_TRUE);
183 }
184 }
185 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
186 return (B_FALSE);
187 }
188
189 static int
190 dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
191 zio_cksum_t *zc, int outfd)
192 {
193 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
194 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
195 (void) fletcher_4_incremental_native(drr,
196 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
197 if (drr->drr_type != DRR_BEGIN) {
198 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
199 drr_checksum.drr_checksum));
200 drr->drr_u.drr_checksum.drr_checksum = *zc;
201 }
202 (void) fletcher_4_incremental_native(
203 &drr->drr_u.drr_checksum.drr_checksum, sizeof (zio_cksum_t), zc);
204 if (write(outfd, drr, sizeof (*drr)) == -1)
205 return (errno);
206 if (payload_len != 0) {
207 (void) fletcher_4_incremental_native(payload, payload_len, zc);
208 if (write(outfd, payload, payload_len) == -1)
209 return (errno);
210 }
211 return (0);
212 }
213
214 /*
285
286 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
287
288 /* set the DEDUP feature flag for this stream */
289 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
290 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
291 DMU_BACKUP_FEATURE_DEDUPPROPS);
292 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
293
294 if (drr->drr_payloadlen != 0) {
295 sz = drr->drr_payloadlen;
296
297 if (sz > SPA_MAXBLOCKSIZE) {
298 buf = zfs_realloc(dda->dedup_hdl, buf,
299 SPA_MAXBLOCKSIZE, sz);
300 }
301 (void) ssread(buf, sz, ofp);
302 if (ferror(stdin))
303 perror("fread");
304 }
305 if (dump_record(drr, buf, sz, &stream_cksum,
306 outfd) != 0)
307 goto out;
308 break;
309 }
310
311 case DRR_END:
312 {
313 struct drr_end *drre = &drr->drr_u.drr_end;
314 /* use the recalculated checksum */
315 drre->drr_checksum = stream_cksum;
316 if (dump_record(drr, NULL, 0, &stream_cksum,
317 outfd) != 0)
318 goto out;
319 break;
320 }
321
322 case DRR_OBJECT:
323 {
324 struct drr_object *drro = &drr->drr_u.drr_object;
325 if (drro->drr_bonuslen > 0) {
326 (void) ssread(buf,
327 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
328 ofp);
329 }
330 if (dump_record(drr, buf,
331 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
332 &stream_cksum, outfd) != 0)
333 goto out;
334 break;
335 }
336
337 case DRR_SPILL:
338 {
339 struct drr_spill *drrs = &drr->drr_u.drr_spill;
340 (void) ssread(buf, drrs->drr_length, ofp);
341 if (dump_record(drr, buf, drrs->drr_length,
342 &stream_cksum, outfd) != 0)
343 goto out;
344 break;
345 }
346
347 case DRR_FREEOBJECTS:
348 {
349 if (dump_record(drr, NULL, 0, &stream_cksum,
350 outfd) != 0)
351 goto out;
352 break;
353 }
354
355 case DRR_WRITE:
356 {
357 struct drr_write *drrw = &drr->drr_u.drr_write;
358 dataref_t dataref;
359 uint64_t payload_size;
360
361 payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
362 (void) ssread(buf, payload_size, ofp);
363
364 /*
365 * Use the existing checksum if it's dedup-capable,
366 * else calculate a SHA256 checksum for it.
367 */
368
369 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
403
404 wbr_drrr->drr_object = drrw->drr_object;
405 wbr_drrr->drr_offset = drrw->drr_offset;
406 wbr_drrr->drr_length = drrw->drr_logical_size;
407 wbr_drrr->drr_toguid = drrw->drr_toguid;
408 wbr_drrr->drr_refguid = dataref.ref_guid;
409 wbr_drrr->drr_refobject =
410 dataref.ref_object;
411 wbr_drrr->drr_refoffset =
412 dataref.ref_offset;
413
414 wbr_drrr->drr_checksumtype =
415 drrw->drr_checksumtype;
416 wbr_drrr->drr_checksumflags =
417 drrw->drr_checksumtype;
418 wbr_drrr->drr_key.ddk_cksum =
419 drrw->drr_key.ddk_cksum;
420 wbr_drrr->drr_key.ddk_prop =
421 drrw->drr_key.ddk_prop;
422
423 if (dump_record(&wbr_drr, NULL, 0,
424 &stream_cksum, outfd) != 0)
425 goto out;
426 } else {
427 /* block not previously seen */
428 if (dump_record(drr, buf, payload_size,
429 &stream_cksum, outfd) != 0)
430 goto out;
431 }
432 break;
433 }
434
435 case DRR_WRITE_EMBEDDED:
436 {
437 struct drr_write_embedded *drrwe =
438 &drr->drr_u.drr_write_embedded;
439 (void) ssread(buf,
440 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
441 if (dump_record(drr, buf,
442 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
443 &stream_cksum, outfd) != 0)
444 goto out;
445 break;
446 }
447
448 case DRR_FREE:
449 {
450 if (dump_record(drr, NULL, 0, &stream_cksum,
451 outfd) != 0)
452 goto out;
453 break;
454 }
455
456 default:
457 (void) fprintf(stderr, "INVALID record type 0x%x\n",
458 drr->drr_type);
459 /* should never happen, so assert */
460 assert(B_FALSE);
461 }
462 }
463 out:
464 umem_cache_destroy(ddt.ddecache);
465 free(ddt.dedup_hash_array);
466 free(buf);
467 (void) fclose(ofp);
468
469 return (NULL);
470 }
471
472 /*
473 * Routines for dealing with the AVL tree of fs-nvlists
474 */
475 typedef struct fsavl_node {
476 avl_node_t fn_node;
477 nvlist_t *fn_nvfs;
478 char *fn_snapname;
479 uint64_t fn_guid;
480 } fsavl_node_t;
481
482 static int
483 fsavl_compare(const void *arg1, const void *arg2)
484 {
485 const fsavl_node_t *fn1 = arg1;
486 const fsavl_node_t *fn2 = arg2;
487
488 if (fn1->fn_guid > fn2->fn_guid)
489 return (+1);
490 else if (fn1->fn_guid < fn2->fn_guid)
491 return (-1);
492 else
493 return (0);
494 }
495
496 /*
497 * Given the GUID of a snapshot, find its containing filesystem and
498 * (optionally) name.
499 */
500 static nvlist_t *
501 fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
502 {
503 fsavl_node_t fn_find;
504 fsavl_node_t *fn;
505
506 fn_find.fn_guid = snapguid;
507
508 fn = avl_find(avl, &fn_find, NULL);
509 if (fn) {
510 if (snapname)
511 *snapname = fn->fn_snapname;
512 return (fn->fn_nvfs);
513 }
514 return (NULL);
515 }
516
517 static void
518 fsavl_destroy(avl_tree_t *avl)
519 {
520 fsavl_node_t *fn;
521 void *cookie;
522
523 if (avl == NULL)
524 return;
525
526 cookie = NULL;
527 while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
528 free(fn);
529 avl_destroy(avl);
530 free(avl);
531 }
532
533 /*
534 * Given an nvlist, produce an avl tree of snapshots, ordered by guid
535 */
536 static avl_tree_t *
537 fsavl_create(nvlist_t *fss)
538 {
539 avl_tree_t *fsavl;
540 nvpair_t *fselem = NULL;
541
542 if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
543 return (NULL);
544
545 avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
546 offsetof(fsavl_node_t, fn_node));
547
548 while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
549 nvlist_t *nvfs, *snaps;
550 nvpair_t *snapelem = NULL;
551
552 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
553 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
554
555 while ((snapelem =
556 nvlist_next_nvpair(snaps, snapelem)) != NULL) {
557 fsavl_node_t *fn;
558 uint64_t guid;
559
560 VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
561 if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
562 fsavl_destroy(fsavl);
563 return (NULL);
564 }
565 fn->fn_nvfs = nvfs;
566 fn->fn_snapname = nvpair_name(snapelem);
567 fn->fn_guid = guid;
568
569 /*
570 * Note: if there are multiple snaps with the
571 * same GUID, we ignore all but one.
572 */
573 if (avl_find(fsavl, fn, NULL) == NULL)
574 avl_add(fsavl, fn);
575 else
576 free(fn);
577 }
578 }
579
580 return (fsavl);
581 }
582
583 /*
584 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
585 */
586 typedef struct send_data {
587 /*
588 * assigned inside every recursive call,
589 * restored from *_save on return:
590 *
591 * guid of fromsnap snapshot in parent dataset
592 * txg of fromsnap snapshot in current dataset
593 * txg of tosnap snapshot in current dataset
594 */
595
596 uint64_t parent_fromsnap_guid;
597 uint64_t fromsnap_txg;
598 uint64_t tosnap_txg;
599
600 /* the nvlists get accumulated during depth-first traversal */
601 nvlist_t *parent_snaps;
602 nvlist_t *fss;
603 nvlist_t *snapprops;
607 const char *fromsnap;
608 const char *tosnap;
609 boolean_t recursive;
610 boolean_t verbose;
611
612 /*
613 * The header nvlist is of the following format:
614 * {
615 * "tosnap" -> string
616 * "fromsnap" -> string (if incremental)
617 * "fss" -> {
618 * id -> {
619 *
620 * "name" -> string (full name; for debugging)
621 * "parentfromsnap" -> number (guid of fromsnap in parent)
622 *
623 * "props" -> { name -> value (only if set here) }
624 * "snaps" -> { name (lastname) -> number (guid) }
625 * "snapprops" -> { name (lastname) -> { name -> value } }
626 *
627 * "origin" -> number (guid) (if clone)
628 * "sent" -> boolean (not on-disk)
629 * }
630 * }
631 * }
632 *
633 */
634 } send_data_t;
635
636 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
637
638 static int
639 send_iterate_snap(zfs_handle_t *zhp, void *arg)
640 {
641 send_data_t *sd = arg;
642 uint64_t guid = zhp->zfs_dmustats.dds_guid;
643 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
644 char *snapname;
645 nvlist_t *nv;
646
647 snapname = strrchr(zhp->zfs_name, '@')+1;
670
671 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
672 send_iterate_prop(zhp, nv);
673 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
674 nvlist_free(nv);
675
676 zfs_close(zhp);
677 return (0);
678 }
679
680 static void
681 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
682 {
683 nvpair_t *elem = NULL;
684
685 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
686 char *propname = nvpair_name(elem);
687 zfs_prop_t prop = zfs_name_to_prop(propname);
688 nvlist_t *propnv;
689
690 if (!zfs_prop_user(propname)) {
691 /*
692 * Realistically, this should never happen. However,
693 * we want the ability to add DSL properties without
694 * needing to make incompatible version changes. We
695 * need to ignore unknown properties to allow older
696 * software to still send datasets containing these
697 * properties, with the unknown properties elided.
698 */
699 if (prop == ZPROP_INVAL)
700 continue;
701
702 if (zfs_prop_readonly(prop))
703 continue;
704 }
705
706 verify(nvpair_value_nvlist(elem, &propnv) == 0);
707 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
708 prop == ZFS_PROP_REFQUOTA ||
709 prop == ZFS_PROP_REFRESERVATION) {
817 "skipping dataset %s: snapshot %s does "
818 "not exist\n"), zhp->zfs_name, sd->tosnap);
819 }
820 } else {
821 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
822 "cannot send %s@%s%s: snapshot %s@%s does not "
823 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
824 dgettext(TEXT_DOMAIN, " recursively") : "",
825 zhp->zfs_name, sd->tosnap);
826 rv = -1;
827 }
828 goto out;
829 }
830
831 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
832 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
833 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
834 sd->parent_fromsnap_guid));
835
836 if (zhp->zfs_dmustats.dds_origin[0]) {
837 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
838 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
839 if (origin == NULL) {
840 rv = -1;
841 goto out;
842 }
843 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
844 origin->zfs_dmustats.dds_guid));
845 }
846
847 /* iterate over props */
848 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
849 send_iterate_prop(zhp, nv);
850 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
851 nvlist_free(nv);
852
853 /* iterate over snaps, and set sd->parent_fromsnap_guid */
854 sd->parent_fromsnap_guid = 0;
855 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
856 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
857 (void) zfs_iter_snapshots(zhp, B_FALSE, send_iterate_snap, sd);
858 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
859 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
860 nvlist_free(sd->parent_snaps);
861 nvlist_free(sd->snapprops);
862
863 /* add this fs to nvlist */
864 (void) snprintf(guidstring, sizeof (guidstring),
865 "0x%llx", (longlong_t)guid);
866 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
867 nvlist_free(nvfs);
868
869 /* iterate over children */
870 if (sd->recursive)
871 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
872
873 out:
874 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
875 sd->fromsnap_txg = fromsnap_txg_save;
876 sd->tosnap_txg = tosnap_txg_save;
877
890
891 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
892 if (zhp == NULL)
893 return (EZFS_BADTYPE);
894
895 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
896 sd.fsname = fsname;
897 sd.fromsnap = fromsnap;
898 sd.tosnap = tosnap;
899 sd.recursive = recursive;
900 sd.verbose = verbose;
901
902 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
903 nvlist_free(sd.fss);
904 if (avlp != NULL)
905 *avlp = NULL;
906 *nvlp = NULL;
907 return (error);
908 }
909
910 if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
911 nvlist_free(sd.fss);
912 *nvlp = NULL;
913 return (EZFS_NOMEM);
914 }
915
916 *nvlp = sd.fss;
917 return (0);
918 }
919
920 /*
921 * Routines specific to "zfs send"
922 */
923 typedef struct send_dump_data {
924 /* these are all just the short snapname (the part after the @) */
925 const char *fromsnap;
926 const char *tosnap;
927 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
928 uint64_t prevsnap_obj;
929 boolean_t seenfrom, seento, replicate, doall, fromorigin;
930 boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
931 boolean_t large_block, compress;
932 int outfd;
933 boolean_t err;
934 nvlist_t *fss;
935 nvlist_t *snapholds;
936 avl_tree_t *fsavl;
937 snapfilter_cb_t *filter_cb;
938 void *filter_cb_arg;
939 nvlist_t *debugnv;
940 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
941 int cleanup_fd;
942 uint64_t size;
943 } send_dump_data_t;
944
945 static int
946 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
947 boolean_t fromorigin, enum lzc_send_flags flags, uint64_t *sizep)
948 {
949 zfs_cmd_t zc = { 0 };
950 libzfs_handle_t *hdl = zhp->zfs_hdl;
951
993 zfs_error_aux(hdl, strerror(errno));
994 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
995
996 default:
997 return (zfs_standard_error(hdl, errno, errbuf));
998 }
999 }
1000
1001 *sizep = zc.zc_objset_type;
1002
1003 return (0);
1004 }
1005
1006 /*
1007 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
1008 * NULL) to the file descriptor specified by outfd.
1009 */
1010 static int
1011 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
1012 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
1013 nvlist_t *debugnv)
1014 {
1015 zfs_cmd_t zc = { 0 };
1016 libzfs_handle_t *hdl = zhp->zfs_hdl;
1017 nvlist_t *thisdbg;
1018
1019 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1020 assert(fromsnap_obj == 0 || !fromorigin);
1021
1022 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
1023 zc.zc_cookie = outfd;
1024 zc.zc_obj = fromorigin;
1025 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1026 zc.zc_fromobj = fromsnap_obj;
1027 zc.zc_flags = flags;
1028
1029 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
1030 if (fromsnap && fromsnap[0] != '\0') {
1031 VERIFY(0 == nvlist_add_string(thisdbg,
1032 "fromsnap", fromsnap));
1033 }
1034
1035 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
1036 char errbuf[1024];
1037 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1038 "warning: cannot send '%s'"), zhp->zfs_name);
1039
1040 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
1041 if (debugnv) {
1042 VERIFY(0 == nvlist_add_nvlist(debugnv,
1043 zhp->zfs_name, thisdbg));
1044 }
1045 nvlist_free(thisdbg);
1046
1047 switch (errno) {
1061
1062 case EDQUOT:
1063 case EFBIG:
1064 case EIO:
1065 case ENOLINK:
1066 case ENOSPC:
1067 case ENOSTR:
1068 case ENXIO:
1069 case EPIPE:
1070 case ERANGE:
1071 case EFAULT:
1072 case EROFS:
1073 zfs_error_aux(hdl, strerror(errno));
1074 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
1075
1076 default:
1077 return (zfs_standard_error(hdl, errno, errbuf));
1078 }
1079 }
1080
1081 if (debugnv)
1082 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1083 nvlist_free(thisdbg);
1084
1085 return (0);
1086 }
1087
1088 static void
1089 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1090 {
1091 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1092
1093 /*
1094 * zfs_send() only sets snapholds for sends that need them,
1095 * e.g. replication and doall.
1096 */
1097 if (sdd->snapholds == NULL)
1098 return;
1099
1100 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1258 * this snapshot will be sent.
1259 */
1260 if (exclude || (sdd->filter_cb != NULL &&
1261 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1262 /*
1263 * This snapshot is filtered out. Don't send it, and don't
1264 * set prevsnap_obj, so it will be as if this snapshot didn't
1265 * exist, and the next accepted snapshot will be sent as
1266 * an incremental from the last accepted one, or as the
1267 * first (and full) snapshot in the case of a replication,
1268 * non-incremental send.
1269 */
1270 zfs_close(zhp);
1271 return (0);
1272 }
1273
1274 gather_holds(zhp, sdd);
1275 fromorigin = sdd->prevsnap[0] == '\0' &&
1276 (sdd->fromorigin || sdd->replicate);
1277
1278 if (sdd->verbose) {
1279 uint64_t size = 0;
1280 (void) estimate_ioctl(zhp, sdd->prevsnap_obj,
1281 fromorigin, flags, &size);
1282
1283 send_print_verbose(fout, zhp->zfs_name,
1284 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1285 size, sdd->parsable);
1286 sdd->size += size;
1287 }
1288
1289 if (!sdd->dryrun) {
1290 /*
1291 * If progress reporting is requested, spawn a new thread to
1292 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1293 */
1294 if (sdd->progress) {
1295 pa.pa_zhp = zhp;
1296 pa.pa_fd = sdd->outfd;
1297 pa.pa_parsable = sdd->parsable;
1298
1299 if ((err = pthread_create(&tid, NULL,
1300 send_progress_thread, &pa)) != 0) {
1301 zfs_close(zhp);
1302 return (err);
1303 }
1304 }
1305
1306 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1307 fromorigin, sdd->outfd, flags, sdd->debugnv);
1308
1309 if (sdd->progress) {
1310 (void) pthread_cancel(tid);
1311 (void) pthread_join(tid, NULL);
1312 }
1313 }
1314
1315 (void) strcpy(sdd->prevsnap, thissnap);
1316 sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1317 zfs_close(zhp);
1318 return (err);
1319 }
1320
1321 static int
1322 dump_filesystem(zfs_handle_t *zhp, void *arg)
1323 {
1324 int rv = 0;
1325 send_dump_data_t *sdd = arg;
1326 boolean_t missingfrom = B_FALSE;
1327 zfs_cmd_t zc = { 0 };
1328
1329 (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
1474 if (needagain) {
1475 assert(progress);
1476 goto again;
1477 }
1478
1479 /* clean out the sent flags in case we reuse this fss */
1480 for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1481 fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1482 nvlist_t *fslist;
1483
1484 VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
1485 (void) nvlist_remove_all(fslist, "sent");
1486 }
1487
1488 return (0);
1489 }
1490
1491 nvlist_t *
1492 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1493 {
1494 unsigned int version;
1495 int nread;
1496 unsigned long long checksum, packed_len;
1497
1498 /*
1499 * Decode token header, which is:
1500 * <token version>-<checksum of payload>-<uncompressed payload length>
1501 * Note that the only supported token version is 1.
1502 */
1503 nread = sscanf(token, "%u-%llx-%llx-",
1504 &version, &checksum, &packed_len);
1505 if (nread != 3) {
1506 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1507 "resume token is corrupt (invalid format)"));
1508 return (NULL);
1509 }
1510
1511 if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
1512 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1513 "resume token is corrupt (invalid version %u)"),
1514 version);
1515 return (NULL);
1516 }
1517
1518 /* convert hexadecimal representation to binary */
1519 token = strrchr(token, '-') + 1;
1520 int len = strlen(token) / 2;
1521 unsigned char *compressed = zfs_alloc(hdl, len);
1522 for (int i = 0; i < len; i++) {
1523 nread = sscanf(token + i * 2, "%2hhx", compressed + i);
1524 if (nread != 1) {
1525 free(compressed);
1526 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1527 "resume token is corrupt "
1528 "(payload is not hex-encoded)"));
1529 return (NULL);
1530 }
1531 }
1532
1533 /* verify checksum */
1534 zio_cksum_t cksum;
1535 fletcher_4_native(compressed, len, NULL, &cksum);
1536 if (cksum.zc_word[0] != checksum) {
1537 free(compressed);
1538 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1539 "resume token is corrupt (incorrect checksum)"));
1540 return (NULL);
1541 }
1542
1543 /* uncompress */
1544 void *packed = zfs_alloc(hdl, packed_len);
1545 uLongf packed_len_long = packed_len;
1546 if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
1547 packed_len_long != packed_len) {
1548 free(packed);
1549 free(compressed);
1550 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1551 "resume token is corrupt (decompression failed)"));
1552 return (NULL);
1553 }
1554
1555 /* unpack nvlist */
1556 nvlist_t *nv;
1557 int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
1558 free(packed);
1559 free(compressed);
1560 if (error != 0) {
1561 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1562 "resume token is corrupt (nvlist_unpack failed)"));
1563 return (NULL);
1564 }
1565 return (nv);
1566 }
1567
1568 int
1569 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1570 const char *resume_token)
1571 {
1572 char errbuf[1024];
1573 char *toname;
1574 char *fromname = NULL;
1575 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1576 zfs_handle_t *zhp;
1577 int error = 0;
1578 char name[ZFS_MAX_DATASET_NAME_LEN];
1579 enum lzc_send_flags lzc_flags = 0;
1580 FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr;
1581
1582 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1583 "cannot resume send"));
1584
1585 nvlist_t *resume_nvl =
1586 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1587 if (resume_nvl == NULL) {
1588 /*
1589 * zfs_error_aux has already been set by
1590 * zfs_send_resume_token_to_nvlist
1591 */
1592 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1593 }
1594 if (flags->verbose) {
1595 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1596 "resume token contents:\n"));
1597 nvlist_print(fout, resume_nvl);
1598 }
1599
1600 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1601 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1602 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1603 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1604 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1605 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1606 "resume token is corrupt"));
1607 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1608 }
1609 fromguid = 0;
1610 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1611
1612 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1613 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1614 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1615 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1616 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1617 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1634 "unable to access '%s'"), name);
1635 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1636 }
1637
1638 if (fromguid != 0) {
1639 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1640 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1641 "incremental source %#llx no longer exists"),
1642 (longlong_t)fromguid);
1643 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1644 }
1645 fromname = name;
1646 }
1647
1648 if (flags->verbose) {
1649 uint64_t size = 0;
1650 error = lzc_send_space(zhp->zfs_name, fromname,
1651 lzc_flags, &size);
1652 if (error == 0)
1653 size = MAX(0, (int64_t)(size - bytes));
1654 send_print_verbose(fout, zhp->zfs_name, fromname,
1655 size, flags->parsable);
1656 }
1657
1658 if (!flags->dryrun) {
1659 progress_arg_t pa = { 0 };
1660 pthread_t tid;
1661 /*
1662 * If progress reporting is requested, spawn a new thread to
1663 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1664 */
1665 if (flags->progress) {
1666 pa.pa_zhp = zhp;
1667 pa.pa_fd = outfd;
1668 pa.pa_parsable = flags->parsable;
1669
1670 error = pthread_create(&tid, NULL,
1671 send_progress_thread, &pa);
1672 if (error != 0) {
1673 zfs_close(zhp);
1674 return (error);
1764
1765 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1766 uint64_t version;
1767 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1768 if (version >= ZPL_VERSION_SA) {
1769 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1770 }
1771 }
1772
1773 if (flags->dedup && !flags->dryrun) {
1774 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1775 DMU_BACKUP_FEATURE_DEDUPPROPS);
1776 if ((err = pipe(pipefd)) != 0) {
1777 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1778 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1779 errbuf));
1780 }
1781 dda.outputfd = outfd;
1782 dda.inputfd = pipefd[1];
1783 dda.dedup_hdl = zhp->zfs_hdl;
1784 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1785 (void) close(pipefd[0]);
1786 (void) close(pipefd[1]);
1787 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1788 return (zfs_error(zhp->zfs_hdl,
1789 EZFS_THREADCREATEFAILED, errbuf));
1790 }
1791 }
1792
1793 if (flags->replicate || flags->doall || flags->props) {
1794 dmu_replay_record_t drr = { 0 };
1795 char *packbuf = NULL;
1796 size_t buflen = 0;
1797 zio_cksum_t zc = { 0 };
1798
1799 if (flags->replicate || flags->props) {
1800 nvlist_t *hdrnv;
1801
1802 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1803 if (fromsnap) {
1822 *debugnvp = hdrnv;
1823 else
1824 nvlist_free(hdrnv);
1825 if (err)
1826 goto stderr_out;
1827 }
1828
1829 if (!flags->dryrun) {
1830 /* write first begin record */
1831 drr.drr_type = DRR_BEGIN;
1832 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1833 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1834 drr_versioninfo, DMU_COMPOUNDSTREAM);
1835 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1836 drr_versioninfo, featureflags);
1837 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1838 sizeof (drr.drr_u.drr_begin.drr_toname),
1839 "%s@%s", zhp->zfs_name, tosnap);
1840 drr.drr_payloadlen = buflen;
1841
1842 err = dump_record(&drr, packbuf, buflen, &zc, outfd);
1843 free(packbuf);
1844 if (err != 0)
1845 goto stderr_out;
1846
1847 /* write end record */
1848 bzero(&drr, sizeof (drr));
1849 drr.drr_type = DRR_END;
1850 drr.drr_u.drr_end.drr_checksum = zc;
1851 err = write(outfd, &drr, sizeof (drr));
1852 if (err == -1) {
1853 err = errno;
1854 goto stderr_out;
1855 }
1856
1857 err = 0;
1858 }
1859 }
1860
1861 /* dump each stream */
1862 sdd.fromsnap = fromsnap;
1863 sdd.tosnap = tosnap;
1864 if (tid != 0)
1865 sdd.outfd = pipefd[0];
1866 else
1867 sdd.outfd = outfd;
1868 sdd.replicate = flags->replicate;
1869 sdd.doall = flags->doall;
1870 sdd.fromorigin = flags->fromorigin;
1871 sdd.fss = fss;
1872 sdd.fsavl = fsavl;
1873 sdd.verbose = flags->verbose;
1874 sdd.parsable = flags->parsable;
1875 sdd.progress = flags->progress;
1876 sdd.dryrun = flags->dryrun;
1877 sdd.large_block = flags->largeblock;
1878 sdd.embed_data = flags->embed_data;
1879 sdd.compress = flags->compress;
1880 sdd.filter_cb = filter_func;
1881 sdd.filter_cb_arg = cb_arg;
1882 if (debugnvp)
1883 sdd.debugnv = *debugnvp;
1884 if (sdd.verbose && sdd.dryrun)
1885 sdd.std_out = B_TRUE;
1886 fout = sdd.std_out ? stdout : stderr;
1887
1888 /*
1889 * Some flags require that we place user holds on the datasets that are
1890 * being sent so they don't get destroyed during the send. We can skip
1891 * this step if the pool is imported read-only since the datasets cannot
1892 * be destroyed.
1893 */
1894 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1895 ZPOOL_PROP_READONLY, NULL) &&
1896 zfs_spa_version(zhp, &spa_version) == 0 &&
1897 spa_version >= SPA_VERSION_USERREFS &&
1898 (flags->doall || flags->replicate)) {
1899 ++holdseq;
1900 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1901 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1902 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1903 if (sdd.cleanup_fd < 0) {
1904 err = errno;
1905 goto stderr_out;
1906 }
1907 sdd.snapholds = fnvlist_alloc();
1908 } else {
1909 sdd.cleanup_fd = -1;
1910 sdd.snapholds = NULL;
1911 }
1912 if (flags->verbose || sdd.snapholds != NULL) {
1913 /*
1914 * Do a verbose no-op dry run to get all the verbose output
1915 * or to gather snapshot hold's before generating any data,
1916 * then do a non-verbose real run to generate the streams.
1917 */
1918 sdd.dryrun = B_TRUE;
1919 err = dump_filesystems(zhp, &sdd);
1920
1921 if (err != 0)
1922 goto stderr_out;
1923
1924 if (flags->verbose) {
1925 if (flags->parsable) {
1926 (void) fprintf(fout, "size\t%llu\n",
1927 (longlong_t)sdd.size);
1928 } else {
1929 char buf[16];
1930 zfs_nicenum(sdd.size, buf, sizeof (buf));
1931 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1932 "total estimated size is %s\n"), buf);
1952 sdd.snapholds = NULL;
1953 }
1954
1955 sdd.dryrun = B_FALSE;
1956 sdd.verbose = B_FALSE;
1957 }
1958
1959 err = dump_filesystems(zhp, &sdd);
1960 fsavl_destroy(fsavl);
1961 nvlist_free(fss);
1962
1963 /* Ensure no snaps found is treated as an error. */
1964 if (err == 0 && !sdd.seento)
1965 err = ENOENT;
1966
1967 if (tid != 0) {
1968 if (err != 0)
1969 (void) pthread_cancel(tid);
1970 (void) close(pipefd[0]);
1971 (void) pthread_join(tid, NULL);
1972 }
1973
1974 if (sdd.cleanup_fd != -1) {
1975 VERIFY(0 == close(sdd.cleanup_fd));
1976 sdd.cleanup_fd = -1;
1977 }
1978
1979 if (!flags->dryrun && (flags->replicate || flags->doall ||
1980 flags->props)) {
1981 /*
1982 * write final end record. NB: want to do this even if
1983 * there was some error, because it might not be totally
1984 * failed.
1985 */
1986 dmu_replay_record_t drr = { 0 };
1987 drr.drr_type = DRR_END;
1988 if (write(outfd, &drr, sizeof (drr)) == -1) {
1989 return (zfs_standard_error(zhp->zfs_hdl,
1990 errno, errbuf));
1991 }
1992 }
1993
1994 return (err || sdd.err);
1995
1996 stderr_out:
1997 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1998 err_out:
1999 fsavl_destroy(fsavl);
2000 nvlist_free(fss);
2001 fnvlist_free(sdd.snapholds);
2002
2003 if (sdd.cleanup_fd != -1)
2004 VERIFY(0 == close(sdd.cleanup_fd));
2005 if (tid != 0) {
2006 (void) pthread_cancel(tid);
2007 (void) close(pipefd[0]);
2008 (void) pthread_join(tid, NULL);
2009 }
2010 return (err);
2011 }
2012
2013 int
2121 err = nvlist_unpack(buf, len, nvp, 0);
2122 free(buf);
2123 if (err != 0) {
2124 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2125 "stream (malformed nvlist)"));
2126 return (EINVAL);
2127 }
2128 return (0);
2129 }
2130
2131 static int
2132 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2133 int baselen, char *newname, recvflags_t *flags)
2134 {
2135 static int seq;
2136 zfs_cmd_t zc = { 0 };
2137 int err;
2138 prop_changelist_t *clp;
2139 zfs_handle_t *zhp;
2140
2141 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2142 if (zhp == NULL)
2143 return (-1);
2144 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2145 flags->force ? MS_FORCE : 0);
2146 zfs_close(zhp);
2147 if (clp == NULL)
2148 return (-1);
2149 err = changelist_prefix(clp);
2150 if (err)
2151 return (err);
2152
2153 zc.zc_objset_type = DMU_OST_ZFS;
2154 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2155
2156 if (tryname) {
2157 (void) strcpy(newname, tryname);
2158
2159 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
2160
2161 if (flags->verbose) {
2162 (void) printf("attempting rename %s to %s\n",
2163 zc.zc_name, zc.zc_value);
2164 }
2165 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2166 if (err == 0)
2167 changelist_rename(clp, name, tryname);
2168 } else {
2169 err = ENOENT;
2170 }
2171
2172 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2173 seq++;
2174
2175 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2176 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2177 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
2178
2179 if (flags->verbose) {
2180 (void) printf("failed - trying rename %s to %s\n",
2181 zc.zc_name, zc.zc_value);
2182 }
2183 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2184 if (err == 0)
2185 changelist_rename(clp, name, newname);
2186 if (err && flags->verbose) {
2187 (void) printf("failed (%u) - "
2188 "will try again on next pass\n", errno);
2189 }
2190 err = EAGAIN;
2191 } else if (flags->verbose) {
2192 if (err == 0)
2193 (void) printf("success\n");
2194 else
2195 (void) printf("failed (%u)\n", errno);
2196 }
2197
2198 (void) changelist_postfix(clp);
2199 changelist_free(clp);
2200
2201 return (err);
2202 }
2203
2204 static int
2205 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2206 char *newname, recvflags_t *flags)
2207 {
2208 zfs_cmd_t zc = { 0 };
2209 int err = 0;
2210 prop_changelist_t *clp;
2211 zfs_handle_t *zhp;
2212 boolean_t defer = B_FALSE;
2213 int spa_version;
2214
2215 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2216 if (zhp == NULL)
2217 return (-1);
2218 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2219 flags->force ? MS_FORCE : 0);
2220 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2221 zfs_spa_version(zhp, &spa_version) == 0 &&
2222 spa_version >= SPA_VERSION_USERREFS)
2223 defer = B_TRUE;
2224 zfs_close(zhp);
2225 if (clp == NULL)
2226 return (-1);
2227 err = changelist_prefix(clp);
2228 if (err)
2229 return (err);
2230
2231 zc.zc_objset_type = DMU_OST_ZFS;
2232 zc.zc_defer_destroy = defer;
2233 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2234
2235 if (flags->verbose)
2236 (void) printf("attempting destroy %s\n", zc.zc_name);
2237 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
2238 if (err == 0) {
2239 if (flags->verbose)
2240 (void) printf("success\n");
2241 changelist_remove(clp, zc.zc_name);
2242 }
2243
2244 (void) changelist_postfix(clp);
2245 changelist_free(clp);
2246
2247 /*
2248 * Deferred destroy might destroy the snapshot or only mark it to be
2249 * destroyed later, and it returns success in either case.
2250 */
2251 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2252 ZFS_TYPE_SNAPSHOT))) {
2253 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2254 }
2255
2256 return (err);
2257 }
2258
2259 typedef struct guid_to_name_data {
2260 uint64_t guid;
2261 boolean_t bookmark_ok;
2262 char *name;
2263 char *skip;
2330 if (err != EEXIST)
2331 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2332 if (err != EEXIST && bookmark_ok)
2333 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2334 zfs_close(zhp);
2335 if (err == EEXIST)
2336 return (0);
2337
2338 /*
2339 * Remember the last portion of the dataset so we skip it next
2340 * time through (as we've already searched that portion of the
2341 * hierarchy).
2342 */
2343 gtnd.skip = strrchr(pname, '/') + 1;
2344 }
2345
2346 return (ENOENT);
2347 }
2348
2349 /*
2350 * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
2351 * guid1 is after guid2.
2352 */
2353 static int
2354 created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
2355 uint64_t guid1, uint64_t guid2)
2356 {
2357 nvlist_t *nvfs;
2358 char *fsname, *snapname;
2359 char buf[ZFS_MAX_DATASET_NAME_LEN];
2360 int rv;
2361 zfs_handle_t *guid1hdl, *guid2hdl;
2362 uint64_t create1, create2;
2363
2364 if (guid2 == 0)
2365 return (0);
2366 if (guid1 == 0)
2367 return (1);
2368
2369 nvfs = fsavl_find(avl, guid1, &snapname);
2370 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2371 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2372 guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2373 if (guid1hdl == NULL)
2374 return (-1);
2375
2376 nvfs = fsavl_find(avl, guid2, &snapname);
2377 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2378 (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
2379 guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
2380 if (guid2hdl == NULL) {
2381 zfs_close(guid1hdl);
2382 return (-1);
2383 }
2384
2385 create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
2386 create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
2387
2388 if (create1 < create2)
2389 rv = -1;
2390 else if (create1 > create2)
2391 rv = +1;
2392 else
2393 rv = 0;
2394
2395 zfs_close(guid1hdl);
2396 zfs_close(guid2hdl);
2397
2398 return (rv);
2399 }
2400
2401 static int
2402 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2403 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2404 nvlist_t *renamed)
2405 {
2406 nvlist_t *local_nv;
2407 avl_tree_t *local_avl;
2408 nvpair_t *fselem, *nextfselem;
2409 char *fromsnap;
2410 char newname[ZFS_MAX_DATASET_NAME_LEN];
2411 int error;
2412 boolean_t needagain, progress, recursive;
2413 char *s1, *s2;
2414
2415 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2416
2417 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2418 ENOENT);
2419
2420 if (flags->dryrun)
2421 return (0);
2422
2423 again:
2424 needagain = progress = B_FALSE;
2425
2426 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2427 recursive, B_FALSE, &local_nv, &local_avl)) != 0)
2428 return (error);
2429
2430 /*
2431 * Process deletes and renames
2432 */
2433 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2434 fselem; fselem = nextfselem) {
2435 nvlist_t *nvfs, *snaps;
2436 nvlist_t *stream_nvfs = NULL;
2437 nvpair_t *snapelem, *nextsnapelem;
2438 uint64_t fromguid = 0;
2439 uint64_t originguid = 0;
2440 uint64_t stream_originguid = 0;
2441 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2442 char *fsname, *stream_fsname;
2443
2444 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2445
2446 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2447 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2448 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2449 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2450 &parent_fromsnap_guid));
2451 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2452
2453 /*
2454 * First find the stream's fs, so we can check for
2455 * a different origin (due to "zfs promote")
2456 */
2457 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2458 snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
2459 uint64_t thisguid;
2460
2461 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2462 stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
2463
2464 if (stream_nvfs != NULL)
2465 break;
2466 }
2467
2468 /* check for promote */
2469 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2470 &stream_originguid);
2471 if (stream_nvfs && originguid != stream_originguid) {
2472 switch (created_before(hdl, local_avl,
2473 stream_originguid, originguid)) {
2474 case 1: {
2475 /* promote it! */
2476 zfs_cmd_t zc = { 0 };
2477 nvlist_t *origin_nvfs;
2478 char *origin_fsname;
2479
2480 if (flags->verbose)
2481 (void) printf("promoting %s\n", fsname);
2482
2483 origin_nvfs = fsavl_find(local_avl, originguid,
2484 NULL);
2485 VERIFY(0 == nvlist_lookup_string(origin_nvfs,
2486 "name", &origin_fsname));
2487 (void) strlcpy(zc.zc_value, origin_fsname,
2488 sizeof (zc.zc_value));
2489 (void) strlcpy(zc.zc_name, fsname,
2490 sizeof (zc.zc_name));
2491 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2492 if (error == 0)
2493 progress = B_TRUE;
2494 break;
2495 }
2496 default:
2497 break;
2498 case -1:
2499 fsavl_destroy(local_avl);
2500 nvlist_free(local_nv);
2501 return (-1);
2502 }
2503 /*
2504 * We had/have the wrong origin, therefore our
2505 * list of snapshots is wrong. Need to handle
2506 * them on the next pass.
2507 */
2508 needagain = B_TRUE;
2509 continue;
2510 }
2511
2512 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2513 snapelem; snapelem = nextsnapelem) {
2514 uint64_t thisguid;
2515 char *stream_snapname;
2516 nvlist_t *found, *props;
2517
2518 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2519
2520 VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
2521 found = fsavl_find(stream_avl, thisguid,
2522 &stream_snapname);
2523
2524 /* check for delete */
2525 if (found == NULL) {
2526 char name[ZFS_MAX_DATASET_NAME_LEN];
2527
2528 if (!flags->force)
2529 continue;
2530
2531 (void) snprintf(name, sizeof (name), "%s@%s",
2532 fsname, nvpair_name(snapelem));
2533
2534 error = recv_destroy(hdl, name,
2535 strlen(fsname)+1, newname, flags);
2536 if (error)
2537 needagain = B_TRUE;
2538 else
2539 progress = B_TRUE;
2540 continue;
2541 }
2542
2543 stream_nvfs = found;
2544
2545 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2546 &props) && 0 == nvlist_lookup_nvlist(props,
2547 stream_snapname, &props)) {
2548 zfs_cmd_t zc = { 0 };
2549
2550 zc.zc_cookie = B_TRUE; /* received */
2551 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2552 "%s@%s", fsname, nvpair_name(snapelem));
2553 if (zcmd_write_src_nvlist(hdl, &zc,
2554 props) == 0) {
2555 (void) zfs_ioctl(hdl,
2556 ZFS_IOC_SET_PROP, &zc);
2557 zcmd_free_nvlists(&zc);
2558 }
2559 }
2560
2561 /* check for different snapname */
2562 if (strcmp(nvpair_name(snapelem),
2563 stream_snapname) != 0) {
2564 char name[ZFS_MAX_DATASET_NAME_LEN];
2565 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2566
2567 (void) snprintf(name, sizeof (name), "%s@%s",
2568 fsname, nvpair_name(snapelem));
2569 (void) snprintf(tryname, sizeof (name), "%s@%s",
2570 fsname, stream_snapname);
2571
2572 error = recv_rename(hdl, name, tryname,
2573 strlen(fsname)+1, newname, flags);
2574 if (error)
2575 needagain = B_TRUE;
2576 else
2577 progress = B_TRUE;
2578 }
2579
2580 if (strcmp(stream_snapname, fromsnap) == 0)
2581 fromguid = thisguid;
2582 }
2583
2584 /* check for delete */
2585 if (stream_nvfs == NULL) {
2586 if (!flags->force)
2587 continue;
2588
2589 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2590 newname, flags);
2591 if (error)
2592 needagain = B_TRUE;
2593 else
2594 progress = B_TRUE;
2595 continue;
2596 }
2597
2598 if (fromguid == 0) {
2599 if (flags->verbose) {
2600 (void) printf("local fs %s does not have "
2601 "fromsnap (%s in stream); must have "
2602 "been deleted locally; ignoring\n",
2603 fsname, fromsnap);
2604 }
2605 continue;
2606 }
2607
2608 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2609 "name", &stream_fsname));
2610 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2611 "parentfromsnap", &stream_parent_fromsnap_guid));
2612
2613 s1 = strrchr(fsname, '/');
2614 s2 = strrchr(stream_fsname, '/');
2615
2616 /*
2617 * Check for rename. If the exact receive path is specified, it
2618 * does not count as a rename, but we still need to check the
2619 * datasets beneath it.
2620 */
2621 if ((stream_parent_fromsnap_guid != 0 &&
2622 parent_fromsnap_guid != 0 &&
2623 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2624 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2625 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2626 nvlist_t *parent;
2627 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2628
2629 parent = fsavl_find(local_avl,
2630 stream_parent_fromsnap_guid, NULL);
2631 /*
2632 * NB: parent might not be found if we used the
2633 * tosnap for stream_parent_fromsnap_guid,
2634 * because the parent is a newly-created fs;
2635 * we'll be able to rename it after we recv the
2636 * new fs.
2637 */
2638 if (parent != NULL) {
2639 char *pname;
2640
2641 VERIFY(0 == nvlist_lookup_string(parent, "name",
2642 &pname));
2643 (void) snprintf(tryname, sizeof (tryname),
2644 "%s%s", pname, strrchr(stream_fsname, '/'));
2645 } else {
2646 tryname[0] = '\0';
2647 if (flags->verbose) {
2648 (void) printf("local fs %s new parent "
2649 "not found\n", fsname);
2650 }
2651 }
2652
2653 newname[0] = '\0';
2654
2655 error = recv_rename(hdl, fsname, tryname,
2656 strlen(tofs)+1, newname, flags);
2657
2658 if (renamed != NULL && newname[0] != '\0') {
2659 VERIFY(0 == nvlist_add_boolean(renamed,
2660 newname));
2661 }
2662
2663 if (error)
2664 needagain = B_TRUE;
2665 else
2666 progress = B_TRUE;
2667 }
2668 }
2669
2670 fsavl_destroy(local_avl);
2671 nvlist_free(local_nv);
2672
2673 if (needagain && progress) {
2674 /* do another pass to fix up temporary names */
2675 if (flags->verbose)
2676 (void) printf("another pass:\n");
2677 goto again;
2678 }
2679
2680 return (needagain);
2681 }
2682
2683 static int
2684 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2685 recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
2686 char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
2687 {
2688 nvlist_t *stream_nv = NULL;
2689 avl_tree_t *stream_avl = NULL;
2690 char *fromsnap = NULL;
2691 char *sendsnap = NULL;
2692 char *cp;
2693 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2694 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
2695 char errbuf[1024];
2696 dmu_replay_record_t drre;
2697 int error;
2698 boolean_t anyerr = B_FALSE;
2699 boolean_t softerr = B_FALSE;
2700 boolean_t recursive;
2701
2702 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2703 "cannot receive"));
2704
2705 assert(drr->drr_type == DRR_BEGIN);
2706 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2747 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2748 }
2749 if (drre.drr_type != DRR_END) {
2750 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2751 goto out;
2752 }
2753 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2754 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2755 "incorrect header checksum"));
2756 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2757 goto out;
2758 }
2759
2760 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2761
2762 if (drr->drr_payloadlen != 0) {
2763 nvlist_t *stream_fss;
2764
2765 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2766 &stream_fss));
2767 if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
2768 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2769 "couldn't allocate avl tree"));
2770 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2771 goto out;
2772 }
2773
2774 if (fromsnap != NULL && recursive) {
2775 nvlist_t *renamed = NULL;
2776 nvpair_t *pair = NULL;
2777
2778 (void) strlcpy(tofs, destname, sizeof (tofs));
2779 if (flags->isprefix) {
2780 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2781 int i;
2782
2783 if (flags->istail) {
2784 cp = strrchr(drrb->drr_toname, '/');
2785 if (cp == NULL) {
2786 (void) strlcat(tofs, "/",
2787 sizeof (tofs));
2788 i = 0;
2789 } else {
2790 i = (cp - drrb->drr_toname);
2791 }
2792 } else {
2793 i = strcspn(drrb->drr_toname, "/@");
2794 }
2795 /* zfs_receive_one() will create_parents() */
2796 (void) strlcat(tofs, &drrb->drr_toname[i],
2797 sizeof (tofs));
2798 *strchr(tofs, '@') = '\0';
2799 }
2800
2801 if (!flags->dryrun && !flags->nomount) {
2802 VERIFY(0 == nvlist_alloc(&renamed,
2803 NV_UNIQUE_NAME, 0));
2804 }
2805
2806 softerr = recv_incremental_replication(hdl, tofs, flags,
2807 stream_nv, stream_avl, renamed);
2808
2809 /* Unmount renamed filesystems before receiving. */
2810 while ((pair = nvlist_next_nvpair(renamed,
2811 pair)) != NULL) {
2812 zfs_handle_t *zhp;
2813 prop_changelist_t *clp = NULL;
2814
2815 zhp = zfs_open(hdl, nvpair_name(pair),
2816 ZFS_TYPE_FILESYSTEM);
2817 if (zhp != NULL) {
2818 clp = changelist_gather(zhp,
2819 ZFS_PROP_MOUNTPOINT, 0, 0);
2820 zfs_close(zhp);
2821 if (clp != NULL) {
2822 softerr |=
2823 changelist_prefix(clp);
2824 changelist_free(clp);
2825 }
2826 }
2827 }
2841 *cp = '\0';
2842 /*
2843 * Find the "sendsnap", the final snapshot in a replication
2844 * stream. zfs_receive_one() handles certain errors
2845 * differently, depending on if the contained stream is the
2846 * last one or not.
2847 */
2848 sendsnap = (cp + 1);
2849 }
2850
2851 /* Finally, receive each contained stream */
2852 do {
2853 /*
2854 * we should figure out if it has a recoverable
2855 * error, in which case do a recv_skip() and drive on.
2856 * Note, if we fail due to already having this guid,
2857 * zfs_receive_one() will take care of it (ie,
2858 * recv_skip() and return 0).
2859 */
2860 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2861 sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
2862 action_handlep, sendsnap);
2863 if (error == ENODATA) {
2864 error = 0;
2865 break;
2866 }
2867 anyerr |= error;
2868 } while (error == 0);
2869
2870 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
2871 /*
2872 * Now that we have the fs's they sent us, try the
2873 * renames again.
2874 */
2875 softerr = recv_incremental_replication(hdl, tofs, flags,
2876 stream_nv, stream_avl, NULL);
2877 }
2878
2879 out:
2880 fsavl_destroy(stream_avl);
2881 nvlist_free(stream_nv);
2882 if (softerr)
2883 error = -2;
2884 if (anyerr)
2885 error = -1;
2886 return (error);
2887 }
2888
2889 static void
2890 trunc_prop_errs(int truncated)
2891 {
2892 ASSERT(truncated != 0);
2893
2894 if (truncated == 1)
2895 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2896 "1 more property could not be set\n"));
3006 if (zhp == NULL)
3007 return;
3008
3009 char token_buf[ZFS_MAXPROPLEN];
3010 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3011 token_buf, sizeof (token_buf),
3012 NULL, NULL, 0, B_TRUE);
3013 if (error == 0) {
3014 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3015 "checksum mismatch or incomplete stream.\n"
3016 "Partially received snapshot is saved.\n"
3017 "A resuming stream can be generated on the sending "
3018 "system by running:\n"
3019 " zfs send -t %s"),
3020 token_buf);
3021 }
3022 zfs_close(zhp);
3023 }
3024
3025 /*
3026 * Restores a backup of tosnap from the file descriptor specified by infd.
3027 */
3028 static int
3029 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3030 const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3031 dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3032 avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3033 uint64_t *action_handlep, const char *finalsnap)
3034 {
3035 zfs_cmd_t zc = { 0 };
3036 time_t begin_time;
3037 int ioctl_err, ioctl_errno, err;
3038 char *cp;
3039 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3040 char errbuf[1024];
3041 char prop_errbuf[1024];
3042 const char *chopprefix;
3043 boolean_t newfs = B_FALSE;
3044 boolean_t stream_wantsnewfs;
3045 uint64_t parent_snapguid = 0;
3046 prop_changelist_t *clp = NULL;
3047 nvlist_t *snapprops_nvlist = NULL;
3048 zprop_errflags_t prop_errflags;
3049 boolean_t recursive;
3050 char *snapname = NULL;
3051
3052 begin_time = time(NULL);
3053
3054 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3055 "cannot receive"));
3056
3057 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3058 ENOENT);
3059
3060 if (stream_avl != NULL) {
3061 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3062 &snapname);
3063 nvlist_t *props;
3064 int ret;
3065
3066 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
3067 &parent_snapguid);
3068 err = nvlist_lookup_nvlist(fs, "props", &props);
3069 if (err)
3070 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
3071
3072 if (flags->canmountoff) {
3073 VERIFY(0 == nvlist_add_uint64(props,
3074 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3075 }
3076 ret = zcmd_write_src_nvlist(hdl, &zc, props);
3077 if (err)
3078 nvlist_free(props);
3079
3080 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
3081 VERIFY(0 == nvlist_lookup_nvlist(props,
3082 snapname, &snapprops_nvlist));
3083 }
3084
3085 if (ret != 0)
3086 return (-1);
3087 }
3088
3089 cp = NULL;
3090
3091 /*
3092 * Determine how much of the snapshot name stored in the stream
3093 * we are going to tack on to the name they specified on the
3094 * command line, and how much we are going to chop off.
3095 *
3096 * If they specified a snapshot, chop the entire name stored in
3097 * the stream.
3098 */
3099 if (flags->istail) {
3100 /*
3101 * A filesystem was specified with -e. We want to tack on only
3102 * the tail of the sent snapshot path.
3103 */
3104 if (strchr(tosnap, '@')) {
3105 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3106 "argument - snapshot not allowed with -e"));
3175 * Determine the name of the origin snapshot, store in zc_string.
3176 */
3177 if (originsnap) {
3178 (void) strncpy(zc.zc_string, originsnap, sizeof (zc.zc_string));
3179 if (flags->verbose)
3180 (void) printf("using provided clone origin %s\n",
3181 zc.zc_string);
3182 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3183 if (guid_to_name(hdl, zc.zc_value,
3184 drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3185 zcmd_free_nvlists(&zc);
3186 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3187 "local origin for clone %s does not exist"),
3188 zc.zc_value);
3189 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3190 }
3191 if (flags->verbose)
3192 (void) printf("found clone origin %s\n", zc.zc_string);
3193 }
3194
3195 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3196 DMU_BACKUP_FEATURE_RESUMING;
3197 stream_wantsnewfs = (drrb->drr_fromguid == NULL ||
3198 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3199
3200 if (stream_wantsnewfs) {
3201 /*
3202 * if the parent fs does not exist, look for it based on
3203 * the parent snap GUID
3204 */
3205 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3206 "cannot receive new filesystem stream"));
3207
3208 (void) strcpy(zc.zc_name, zc.zc_value);
3209 cp = strrchr(zc.zc_name, '/');
3210 if (cp)
3211 *cp = '\0';
3212 if (cp &&
3213 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3214 char suffix[ZFS_MAX_DATASET_NAME_LEN];
3299 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3300 }
3301
3302 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3303 stream_wantsnewfs) {
3304 /* We can't do online recv in this case */
3305 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3306 if (clp == NULL) {
3307 zfs_close(zhp);
3308 zcmd_free_nvlists(&zc);
3309 return (-1);
3310 }
3311 if (changelist_prefix(clp) != 0) {
3312 changelist_free(clp);
3313 zfs_close(zhp);
3314 zcmd_free_nvlists(&zc);
3315 return (-1);
3316 }
3317 }
3318
3319 /*
3320 * If we are resuming a newfs, set newfs here so that we will
3321 * mount it if the recv succeeds this time. We can tell
3322 * that it was a newfs on the first recv because the fs
3323 * itself will be inconsistent (if the fs existed when we
3324 * did the first recv, we would have received it into
3325 * .../%recv).
3326 */
3327 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
3328 newfs = B_TRUE;
3329
3330 zfs_close(zhp);
3331 } else {
3332 /*
3333 * Destination filesystem does not exist. Therefore we better
3334 * be creating a new filesystem (either from a full backup, or
3335 * a clone). It would therefore be invalid if the user
3336 * specified only the pool name (i.e. if the destination name
3337 * contained no slash character).
3338 */
3340 (cp = strrchr(zc.zc_name, '/')) == NULL) {
3341 zcmd_free_nvlists(&zc);
3342 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3343 "destination '%s' does not exist"), zc.zc_name);
3344 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3345 }
3346
3347 /*
3348 * Trim off the final dataset component so we perform the
3349 * recvbackup ioctl to the filesystems's parent.
3350 */
3351 *cp = '\0';
3352
3353 if (flags->isprefix && !flags->istail && !flags->dryrun &&
3354 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
3355 zcmd_free_nvlists(&zc);
3356 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3357 }
3358
3359 newfs = B_TRUE;
3360 }
3361
3362 zc.zc_begin_record = *drr_noswap;
3363 zc.zc_cookie = infd;
3364 zc.zc_guid = flags->force;
3365 zc.zc_resumable = flags->resumable;
3366 if (flags->verbose) {
3367 (void) printf("%s %s stream of %s into %s\n",
3368 flags->dryrun ? "would receive" : "receiving",
3369 drrb->drr_fromguid ? "incremental" : "full",
3370 drrb->drr_toname, zc.zc_value);
3371 (void) fflush(stdout);
3372 }
3373
3374 if (flags->dryrun) {
3375 zcmd_free_nvlists(&zc);
3376 return (recv_skip(hdl, infd, flags->byteswap));
3377 }
3378
3379 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
3380 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
3381 zc.zc_cleanup_fd = cleanup_fd;
3382 zc.zc_action_handle = *action_handlep;
3383
3384 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
3385 ioctl_errno = errno;
3386 prop_errflags = (zprop_errflags_t)zc.zc_obj;
3387
3388 if (err == 0) {
3389 nvlist_t *prop_errors;
3390 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
3391 zc.zc_nvlist_dst_size, &prop_errors, 0));
3392
3393 nvpair_t *prop_err = NULL;
3394
3395 while ((prop_err = nvlist_next_nvpair(prop_errors,
3396 prop_err)) != NULL) {
3397 char tbuf[1024];
3398 zfs_prop_t prop;
3399 int intval;
3400
3401 prop = zfs_name_to_prop(nvpair_name(prop_err));
3402 (void) nvpair_value_int32(prop_err, &intval);
3403 if (strcmp(nvpair_name(prop_err),
3509 zc.zc_value);
3510 *cp = '@';
3511 break;
3512 case EINVAL:
3513 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3514 break;
3515 case ECKSUM:
3516 recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
3517 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3518 break;
3519 case ENOTSUP:
3520 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3521 "pool must be upgraded to receive this stream."));
3522 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3523 break;
3524 case EDQUOT:
3525 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3526 "destination %s space quota exceeded"), zc.zc_name);
3527 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3528 break;
3529 default:
3530 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3531 }
3532 }
3533
3534 /*
3535 * Mount the target filesystem (if created). Also mount any
3536 * children of the target filesystem if we did a replication
3537 * receive (indicated by stream_avl being non-NULL).
3538 */
3539 cp = strchr(zc.zc_value, '@');
3540 if (cp && (ioctl_err == 0 || !newfs)) {
3541 zfs_handle_t *h;
3542
3543 *cp = '\0';
3544 h = zfs_open(hdl, zc.zc_value,
3545 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3546 if (h != NULL) {
3547 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3548 *cp = '@';
3585
3586 if (flags->verbose) {
3587 char buf1[64];
3588 char buf2[64];
3589 uint64_t bytes = zc.zc_cookie;
3590 time_t delta = time(NULL) - begin_time;
3591 if (delta == 0)
3592 delta = 1;
3593 zfs_nicenum(bytes, buf1, sizeof (buf1));
3594 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3595
3596 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3597 buf1, delta, buf2);
3598 }
3599
3600 return (0);
3601 }
3602
3603 static int
3604 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3605 const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
3606 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3607 uint64_t *action_handlep, const char *finalsnap)
3608 {
3609 int err;
3610 dmu_replay_record_t drr, drr_noswap;
3611 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3612 char errbuf[1024];
3613 zio_cksum_t zcksum = { 0 };
3614 uint64_t featureflags;
3615 int hdrtype;
3616
3617 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3618 "cannot receive"));
3619
3620 if (flags->isprefix &&
3621 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3622 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3623 "(%s) does not exist"), tosnap);
3624 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3625 }
3688 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3689 }
3690
3691 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3692 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
3693 if (sendfs == NULL) {
3694 /*
3695 * We were not called from zfs_receive_package(). Get
3696 * the fs specified by 'zfs send'.
3697 */
3698 char *cp;
3699 (void) strlcpy(nonpackage_sendfs,
3700 drr.drr_u.drr_begin.drr_toname,
3701 sizeof (nonpackage_sendfs));
3702 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3703 *cp = '\0';
3704 sendfs = nonpackage_sendfs;
3705 VERIFY(finalsnap == NULL);
3706 }
3707 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3708 &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
3709 cleanup_fd, action_handlep, finalsnap));
3710 } else {
3711 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3712 DMU_COMPOUNDSTREAM);
3713 return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
3714 &zcksum, top_zfs, cleanup_fd, action_handlep));
3715 }
3716 }
3717
3718 /*
3719 * Restores a backup of tosnap from the file descriptor specified by infd.
3720 * Return 0 on total success, -2 if some things couldn't be
3721 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3722 * (-1 will override -2, if -1 and the resumable flag was specified the
3723 * transfer can be resumed if the sending side supports it).
3724 */
3725 int
3726 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
3727 recvflags_t *flags, int infd, avl_tree_t *stream_avl)
3728 {
3729 char *top_zfs = NULL;
3730 int err;
3731 int cleanup_fd;
3732 uint64_t action_handle = 0;
3733 char *originsnap = NULL;
3734 if (props) {
3735 err = nvlist_lookup_string(props, "origin", &originsnap);
3736 if (err && err != ENOENT)
3737 return (err);
3738 }
3739
3740 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3741 VERIFY(cleanup_fd >= 0);
3742
3743 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
3744 stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL);
3745
3746 VERIFY(0 == close(cleanup_fd));
3747
3748 if (err == 0 && !flags->nomount && top_zfs) {
3749 zfs_handle_t *zhp;
3750 prop_changelist_t *clp;
3751
3752 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3753 if (zhp != NULL) {
3754 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3755 CL_GATHER_MOUNT_ALWAYS, 0);
3756 zfs_close(zhp);
3757 if (clp != NULL) {
3758 /* mount and share received datasets */
3759 err = changelist_postfix(clp);
3760 changelist_free(clp);
3761 }
3762 }
3763 if (zhp == NULL || clp == NULL || err)
3764 err = -1;
|
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 2017 Nexenta Systems, Inc. All rights reserved.
27 * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
28 * Copyright (c) 2013 Steven Hartland. All rights reserved.
29 * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
30 * Copyright (c) 2014 Integros [integros.com]
31 * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
32 */
33
34 #include <assert.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <libintl.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <strings.h>
41 #include <unistd.h>
42 #include <stddef.h>
43 #include <fcntl.h>
44 #include <sys/mount.h>
45 #include <pthread.h>
46 #include <umem.h>
47 #include <time.h>
48
49 #include <libzfs.h>
50 #include <libzfs_core.h>
51
52 #include "zfs_errno.h"
53 #include "zfs_namecheck.h"
54 #include "zfs_prop.h"
55 #include "zfs_fletcher.h"
56 #include "zfs_sendrecv.h"
57 #include "libzfs_impl.h"
58 #include <zlib.h>
59 #include <sha2.h>
60 #include <sys/zio_checksum.h>
61 #include <sys/ddt.h>
62
63 /* in libzfs_dataset.c */
64 extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
65
66 static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
67 recvflags_t *, int, nvlist_t *, nvlist_t *, const char *, nvlist_t *,
68 avl_tree_t *, char **, int, uint64_t *, const char *);
69 static int guid_to_name(libzfs_handle_t *, const char *,
70 uint64_t, boolean_t, char *);
71
72 static const zio_cksum_t zero_cksum = { 0 };
73
74 typedef struct dedup_arg {
75 int inputfd;
76 int outputfd;
77 uint64_t dedup_data_sz;
78 boolean_t sendsize;
79 libzfs_handle_t *dedup_hdl;
80 } dedup_arg_t;
81
82 typedef struct progress_arg {
83 zfs_handle_t *pa_zhp;
84 int pa_fd;
85 boolean_t pa_parsable;
86 } progress_arg_t;
87
88 typedef struct dataref {
89 uint64_t ref_guid;
90 uint64_t ref_object;
91 uint64_t ref_offset;
92 } dataref_t;
93
94 typedef struct dedup_entry {
95 struct dedup_entry *dde_next;
96 zio_cksum_t dde_chksum;
97 uint64_t dde_prop;
98 dataref_t dde_ref;
175 uint64_t prop, dataref_t *dr)
176 {
177 uint32_t hashcode;
178 dedup_entry_t **ddepp;
179
180 hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
181
182 for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
183 ddepp = &((*ddepp)->dde_next)) {
184 if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
185 (*ddepp)->dde_prop == prop) {
186 *dr = (*ddepp)->dde_ref;
187 return (B_TRUE);
188 }
189 }
190 ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
191 return (B_FALSE);
192 }
193
194 static int
195 dump_record(dedup_arg_t *dda, dmu_replay_record_t *drr, void *payload,
196 int payload_len, zio_cksum_t *zc, int outfd)
197 {
198 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
199 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
200 if (dda != NULL) {
201 dda->dedup_data_sz +=
202 sizeof (dmu_replay_record_t) + payload_len;
203 }
204 (void) fletcher_4_incremental_native(drr,
205 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
206 if (drr->drr_type != DRR_BEGIN) {
207 ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
208 drr_checksum.drr_checksum));
209 drr->drr_u.drr_checksum.drr_checksum = *zc;
210 }
211 (void) fletcher_4_incremental_native(
212 &drr->drr_u.drr_checksum.drr_checksum, sizeof (zio_cksum_t), zc);
213 if (write(outfd, drr, sizeof (*drr)) == -1)
214 return (errno);
215 if (payload_len != 0) {
216 (void) fletcher_4_incremental_native(payload, payload_len, zc);
217 if (write(outfd, payload, payload_len) == -1)
218 return (errno);
219 }
220 return (0);
221 }
222
223 /*
294
295 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
296
297 /* set the DEDUP feature flag for this stream */
298 fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
299 fflags |= (DMU_BACKUP_FEATURE_DEDUP |
300 DMU_BACKUP_FEATURE_DEDUPPROPS);
301 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
302
303 if (drr->drr_payloadlen != 0) {
304 sz = drr->drr_payloadlen;
305
306 if (sz > SPA_MAXBLOCKSIZE) {
307 buf = zfs_realloc(dda->dedup_hdl, buf,
308 SPA_MAXBLOCKSIZE, sz);
309 }
310 (void) ssread(buf, sz, ofp);
311 if (ferror(stdin))
312 perror("fread");
313 }
314 if (dump_record(dda, drr, buf, sz, &stream_cksum,
315 outfd) != 0)
316 goto out;
317 break;
318 }
319
320 case DRR_END:
321 {
322 struct drr_end *drre = &drr->drr_u.drr_end;
323 /* use the recalculated checksum */
324 drre->drr_checksum = stream_cksum;
325 if (dump_record(dda, drr, NULL, 0, &stream_cksum,
326 outfd) != 0)
327 goto out;
328 break;
329 }
330
331 case DRR_OBJECT:
332 {
333 struct drr_object *drro = &drr->drr_u.drr_object;
334 if (drro->drr_bonuslen > 0) {
335 (void) ssread(buf,
336 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
337 ofp);
338 }
339 if (dump_record(dda, drr, buf,
340 P2ROUNDUP((uint64_t)drro->drr_bonuslen, 8),
341 &stream_cksum, outfd) != 0)
342 goto out;
343 break;
344 }
345
346 case DRR_SPILL:
347 {
348 struct drr_spill *drrs = &drr->drr_u.drr_spill;
349 (void) ssread(buf, drrs->drr_length, ofp);
350 if (dump_record(dda, drr, buf, drrs->drr_length,
351 &stream_cksum, outfd) != 0)
352 goto out;
353 break;
354 }
355
356 case DRR_FREEOBJECTS:
357 {
358 if (dump_record(dda, drr, NULL, 0, &stream_cksum,
359 outfd) != 0)
360 goto out;
361 break;
362 }
363
364 case DRR_WRITE:
365 {
366 struct drr_write *drrw = &drr->drr_u.drr_write;
367 dataref_t dataref;
368 uint64_t payload_size;
369
370 payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
371 (void) ssread(buf, payload_size, ofp);
372
373 /*
374 * Use the existing checksum if it's dedup-capable,
375 * else calculate a SHA256 checksum for it.
376 */
377
378 if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
412
413 wbr_drrr->drr_object = drrw->drr_object;
414 wbr_drrr->drr_offset = drrw->drr_offset;
415 wbr_drrr->drr_length = drrw->drr_logical_size;
416 wbr_drrr->drr_toguid = drrw->drr_toguid;
417 wbr_drrr->drr_refguid = dataref.ref_guid;
418 wbr_drrr->drr_refobject =
419 dataref.ref_object;
420 wbr_drrr->drr_refoffset =
421 dataref.ref_offset;
422
423 wbr_drrr->drr_checksumtype =
424 drrw->drr_checksumtype;
425 wbr_drrr->drr_checksumflags =
426 drrw->drr_checksumtype;
427 wbr_drrr->drr_key.ddk_cksum =
428 drrw->drr_key.ddk_cksum;
429 wbr_drrr->drr_key.ddk_prop =
430 drrw->drr_key.ddk_prop;
431
432 if (dump_record(dda, &wbr_drr, NULL, 0,
433 &stream_cksum, outfd) != 0)
434 goto out;
435 } else {
436 /* block not previously seen */
437 if (dump_record(dda, drr, buf, payload_size,
438 &stream_cksum, outfd) != 0)
439 goto out;
440 }
441 break;
442 }
443
444 case DRR_WRITE_EMBEDDED:
445 {
446 struct drr_write_embedded *drrwe =
447 &drr->drr_u.drr_write_embedded;
448 (void) ssread(buf,
449 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
450 if (dump_record(dda, drr, buf,
451 P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
452 &stream_cksum, outfd) != 0)
453 goto out;
454 break;
455 }
456
457 case DRR_FREE:
458 {
459 if (dump_record(dda, drr, NULL, 0, &stream_cksum,
460 outfd) != 0)
461 goto out;
462 break;
463 }
464
465 default:
466 (void) fprintf(stderr, "INVALID record type 0x%x\n",
467 drr->drr_type);
468 /* should never happen, so assert */
469 assert(B_FALSE);
470 }
471 }
472 out:
473 umem_cache_destroy(ddt.ddecache);
474 free(ddt.dedup_hash_array);
475 free(buf);
476 (void) fclose(ofp);
477
478 return (NULL);
479 }
480
481 /*
482 * Routines for dealing with the giant nvlist of fs-nvlists, etc.
483 */
484 typedef struct send_data {
485 /*
486 * assigned inside every recursive call,
487 * restored from *_save on return:
488 *
489 * guid of fromsnap snapshot in parent dataset
490 * txg of fromsnap snapshot in current dataset
491 * txg of tosnap snapshot in current dataset
492 */
493
494 uint64_t parent_fromsnap_guid;
495 uint64_t fromsnap_txg;
496 uint64_t tosnap_txg;
497
498 /* the nvlists get accumulated during depth-first traversal */
499 nvlist_t *parent_snaps;
500 nvlist_t *fss;
501 nvlist_t *snapprops;
505 const char *fromsnap;
506 const char *tosnap;
507 boolean_t recursive;
508 boolean_t verbose;
509
510 /*
511 * The header nvlist is of the following format:
512 * {
513 * "tosnap" -> string
514 * "fromsnap" -> string (if incremental)
515 * "fss" -> {
516 * id -> {
517 *
518 * "name" -> string (full name; for debugging)
519 * "parentfromsnap" -> number (guid of fromsnap in parent)
520 *
521 * "props" -> { name -> value (only if set here) }
522 * "snaps" -> { name (lastname) -> number (guid) }
523 * "snapprops" -> { name (lastname) -> { name -> value } }
524 *
525 * "origin" -> number (guid of origin snapshot) (if clone)
526 * "origin_fsname" -> string (full name of origin file system)
527 * "sent" -> boolean (not on-disk)
528 * }
529 * }
530 * }
531 *
532 */
533 } send_data_t;
534
535 static void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
536
537 static int
538 send_iterate_snap(zfs_handle_t *zhp, void *arg)
539 {
540 send_data_t *sd = arg;
541 uint64_t guid = zhp->zfs_dmustats.dds_guid;
542 uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
543 char *snapname;
544 nvlist_t *nv;
545
546 snapname = strrchr(zhp->zfs_name, '@')+1;
569
570 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
571 send_iterate_prop(zhp, nv);
572 VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
573 nvlist_free(nv);
574
575 zfs_close(zhp);
576 return (0);
577 }
578
579 static void
580 send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv)
581 {
582 nvpair_t *elem = NULL;
583
584 while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
585 char *propname = nvpair_name(elem);
586 zfs_prop_t prop = zfs_name_to_prop(propname);
587 nvlist_t *propnv;
588
589 /*
590 * This property make sense only to this dataset,
591 * so no reasons to include it into stream
592 */
593 if (prop == ZFS_PROP_WBC_MODE)
594 continue;
595
596 if (!zfs_prop_user(propname)) {
597 /*
598 * Realistically, this should never happen. However,
599 * we want the ability to add DSL properties without
600 * needing to make incompatible version changes. We
601 * need to ignore unknown properties to allow older
602 * software to still send datasets containing these
603 * properties, with the unknown properties elided.
604 */
605 if (prop == ZPROP_INVAL)
606 continue;
607
608 if (zfs_prop_readonly(prop))
609 continue;
610 }
611
612 verify(nvpair_value_nvlist(elem, &propnv) == 0);
613 if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
614 prop == ZFS_PROP_REFQUOTA ||
615 prop == ZFS_PROP_REFRESERVATION) {
723 "skipping dataset %s: snapshot %s does "
724 "not exist\n"), zhp->zfs_name, sd->tosnap);
725 }
726 } else {
727 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
728 "cannot send %s@%s%s: snapshot %s@%s does not "
729 "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
730 dgettext(TEXT_DOMAIN, " recursively") : "",
731 zhp->zfs_name, sd->tosnap);
732 rv = -1;
733 }
734 goto out;
735 }
736
737 VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
738 VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
739 VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
740 sd->parent_fromsnap_guid));
741
742 if (zhp->zfs_dmustats.dds_origin[0]) {
743 char origin_fsname[ZFS_MAX_DATASET_NAME_LEN];
744 zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
745 zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
746 if (origin == NULL) {
747 rv = -1;
748 goto out;
749 }
750 VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
751 origin->zfs_dmustats.dds_guid));
752 zfs_close(origin);
753 (void) strlcpy(origin_fsname, zhp->zfs_dmustats.dds_origin,
754 sizeof (origin_fsname));
755 *strchr(origin_fsname, '@') = '\0';
756 VERIFY(0 == nvlist_add_string(nvfs, "origin_fsname",
757 origin_fsname));
758 }
759
760 /* iterate over props */
761 VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
762 send_iterate_prop(zhp, nv);
763 VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
764 nvlist_free(nv);
765
766 /* iterate over snaps, and set sd->parent_fromsnap_guid */
767 sd->parent_fromsnap_guid = 0;
768 VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
769 VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
770 (void) zfs_iter_snapshots_sorted(zhp, send_iterate_snap, sd);
771 VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
772 VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
773 nvlist_free(sd->parent_snaps);
774 nvlist_free(sd->snapprops);
775
776 /* add this fs to nvlist */
777 (void) snprintf(guidstring, sizeof (guidstring),
778 "0x%llx", (longlong_t)guid);
779 VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
780 nvlist_free(nvfs);
781
782 /* iterate over children */
783 if (sd->recursive)
784 rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
785
786 out:
787 sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
788 sd->fromsnap_txg = fromsnap_txg_save;
789 sd->tosnap_txg = tosnap_txg_save;
790
803
804 zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
805 if (zhp == NULL)
806 return (EZFS_BADTYPE);
807
808 VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
809 sd.fsname = fsname;
810 sd.fromsnap = fromsnap;
811 sd.tosnap = tosnap;
812 sd.recursive = recursive;
813 sd.verbose = verbose;
814
815 if ((error = send_iterate_fs(zhp, &sd)) != 0) {
816 nvlist_free(sd.fss);
817 if (avlp != NULL)
818 *avlp = NULL;
819 *nvlp = NULL;
820 return (error);
821 }
822
823 if (avlp != NULL && fsavl_create(sd.fss, avlp) != 0) {
824 nvlist_free(sd.fss);
825 *nvlp = NULL;
826 return (EZFS_NOMEM);
827 }
828
829 *nvlp = sd.fss;
830 return (0);
831 }
832
833 /*
834 * Routines specific to "zfs send"
835 */
836 typedef struct send_dump_data {
837 /* these are all just the short snapname (the part after the @) */
838 const char *fromsnap;
839 const char *tosnap;
840 char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
841 uint64_t prevsnap_obj;
842 boolean_t seenfrom, seento, replicate, doall, fromorigin;
843 boolean_t verbose, dryrun, dedup, parsable, progress, embed_data, std_out;
844 boolean_t large_block, compress;
845 boolean_t sendsize;
846 uint32_t hdr_send_sz;
847 uint64_t send_sz;
848 int outfd;
849 boolean_t err;
850 nvlist_t *fss;
851 nvlist_t *snapholds;
852 avl_tree_t *fsavl;
853 snapfilter_cb_t *filter_cb;
854 void *filter_cb_arg;
855 nvlist_t *debugnv;
856 char holdtag[ZFS_MAX_DATASET_NAME_LEN];
857 int cleanup_fd;
858 uint64_t size;
859 } send_dump_data_t;
860
861 static int
862 estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
863 boolean_t fromorigin, enum lzc_send_flags flags, uint64_t *sizep)
864 {
865 zfs_cmd_t zc = { 0 };
866 libzfs_handle_t *hdl = zhp->zfs_hdl;
867
909 zfs_error_aux(hdl, strerror(errno));
910 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
911
912 default:
913 return (zfs_standard_error(hdl, errno, errbuf));
914 }
915 }
916
917 *sizep = zc.zc_objset_type;
918
919 return (0);
920 }
921
922 /*
923 * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
924 * NULL) to the file descriptor specified by outfd.
925 */
926 static int
927 dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
928 boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
929 nvlist_t *debugnv, boolean_t sendsize, uint64_t *sendcounter)
930 {
931 zfs_cmd_t zc = { 0 };
932 libzfs_handle_t *hdl = zhp->zfs_hdl;
933 nvlist_t *thisdbg;
934
935 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
936 assert(fromsnap_obj == 0 || !fromorigin);
937
938 (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
939 zc.zc_cookie = outfd;
940 zc.zc_obj = fromorigin;
941 zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
942 zc.zc_fromobj = fromsnap_obj;
943 zc.zc_flags = flags;
944 zc.zc_sendsize = sendsize;
945 zc.zc_sendcounter = 0;
946
947 VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
948 if (fromsnap && fromsnap[0] != '\0') {
949 VERIFY(0 == nvlist_add_string(thisdbg,
950 "fromsnap", fromsnap));
951 }
952
953 if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
954 char errbuf[1024];
955 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
956 "warning: cannot send '%s'"), zhp->zfs_name);
957
958 VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
959 if (debugnv) {
960 VERIFY(0 == nvlist_add_nvlist(debugnv,
961 zhp->zfs_name, thisdbg));
962 }
963 nvlist_free(thisdbg);
964
965 switch (errno) {
979
980 case EDQUOT:
981 case EFBIG:
982 case EIO:
983 case ENOLINK:
984 case ENOSPC:
985 case ENOSTR:
986 case ENXIO:
987 case EPIPE:
988 case ERANGE:
989 case EFAULT:
990 case EROFS:
991 zfs_error_aux(hdl, strerror(errno));
992 return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
993
994 default:
995 return (zfs_standard_error(hdl, errno, errbuf));
996 }
997 }
998
999 *sendcounter = (uint64_t)zc.zc_sendcounter;
1000 if (debugnv)
1001 VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
1002 nvlist_free(thisdbg);
1003
1004 return (0);
1005 }
1006
1007 static void
1008 gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1009 {
1010 assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1011
1012 /*
1013 * zfs_send() only sets snapholds for sends that need them,
1014 * e.g. replication and doall.
1015 */
1016 if (sdd->snapholds == NULL)
1017 return;
1018
1019 fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1177 * this snapshot will be sent.
1178 */
1179 if (exclude || (sdd->filter_cb != NULL &&
1180 sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
1181 /*
1182 * This snapshot is filtered out. Don't send it, and don't
1183 * set prevsnap_obj, so it will be as if this snapshot didn't
1184 * exist, and the next accepted snapshot will be sent as
1185 * an incremental from the last accepted one, or as the
1186 * first (and full) snapshot in the case of a replication,
1187 * non-incremental send.
1188 */
1189 zfs_close(zhp);
1190 return (0);
1191 }
1192
1193 gather_holds(zhp, sdd);
1194 fromorigin = sdd->prevsnap[0] == '\0' &&
1195 (sdd->fromorigin || sdd->replicate);
1196
1197 /* print out to-from and approximate size in verbose mode */
1198 if (sdd->verbose) {
1199 uint64_t size = 0;
1200 (void) estimate_ioctl(zhp, sdd->prevsnap_obj,
1201 fromorigin, flags, &size);
1202
1203 send_print_verbose(fout, zhp->zfs_name,
1204 sdd->prevsnap[0] ? sdd->prevsnap : NULL,
1205 size, sdd->parsable);
1206 sdd->size += size;
1207 }
1208
1209 if (!sdd->dryrun) {
1210 uint64_t sendcounter = 0;
1211 boolean_t track_progress = (sdd->progress && !sdd->sendsize);
1212 boolean_t sendsize = B_FALSE;
1213 /*
1214 * If progress reporting is requested, spawn a new thread to
1215 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1216 */
1217 if (track_progress) {
1218 pa.pa_zhp = zhp;
1219 pa.pa_fd = sdd->outfd;
1220 pa.pa_parsable = sdd->parsable;
1221
1222 if ((err = pthread_create(&tid, NULL,
1223 send_progress_thread, &pa)) != 0) {
1224 zfs_close(zhp);
1225 return (err);
1226 }
1227 }
1228
1229 /*
1230 * We need to reset the sendsize flag being sent to
1231 * kernel if sdd->dedup is set. With dedup, the file
1232 * descriptor sent to kernel is one end of the pipe,
1233 * and we would want the data back in the pipe for
1234 * cksummer() to calculate the exact size of the dedup-ed
1235 * stream. So reset the sendsize flag such that
1236 * kernel writes to the pipe.
1237 */
1238
1239 sendsize = sdd->dedup ? B_FALSE : sdd->sendsize;
1240
1241 err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
1242 fromorigin, sdd->outfd, flags, sdd->debugnv,
1243 sendsize, &sendcounter);
1244
1245 sdd->send_sz += sendcounter;
1246
1247 if (track_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
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",
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 *
1430 zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
1431 {
1432 nvlist_t *nvl = NULL;
1433 int error;
1434
1435 error = zfs_send_resume_token_to_nvlist_impl(token, &nvl);
1436 switch (error) {
1437 case EINVAL:
1438 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1439 "resume token is corrupt (invalid format)"));
1440 break;
1441 case ENOTSUP:
1442 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1443 "resume token is corrupt (invalid version)"));
1444 break;
1445 case EBADMSG:
1446 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1447 "resume token is corrupt "
1448 "(payload is not hex-encoded)"));
1449 break;
1450 case ECKSUM:
1451 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1452 "resume token is corrupt (incorrect checksum)"));
1453 break;
1454 case ENOSR:
1455 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1456 "resume token is corrupt (decompression failed)"));
1457 break;
1458 case ENODATA:
1459 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1460 "resume token is corrupt (nvlist_unpack failed)"));
1461 break;
1462 case ENOMEM:
1463 (void) no_memory(hdl);
1464 break;
1465 default:
1466 break;
1467 };
1468
1469 return (nvl);
1470 }
1471
1472 int
1473 zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
1474 const char *resume_token)
1475 {
1476 char errbuf[1024];
1477 char *toname;
1478 char *fromname = NULL;
1479 uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
1480 zfs_handle_t *zhp;
1481 int error = 0;
1482 char name[ZFS_MAX_DATASET_NAME_LEN];
1483 enum lzc_send_flags lzc_flags = 0;
1484
1485 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
1486 "cannot resume send"));
1487
1488 nvlist_t *resume_nvl =
1489 zfs_send_resume_token_to_nvlist(hdl, resume_token);
1490 if (resume_nvl == NULL) {
1491 /*
1492 * zfs_error_aux has already been set by
1493 * zfs_send_resume_token_to_nvlist
1494 */
1495 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1496 }
1497 if (flags->verbose) {
1498 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1499 "resume token contents:\n"));
1500 nvlist_print(stderr, resume_nvl);
1501 }
1502
1503 if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
1504 nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
1505 nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
1506 nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
1507 nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
1508 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1509 "resume token is corrupt"));
1510 return (zfs_error(hdl, EZFS_FAULT, errbuf));
1511 }
1512 fromguid = 0;
1513 (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
1514
1515 if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
1516 lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
1517 if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
1518 lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
1519 if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
1520 lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1537 "unable to access '%s'"), name);
1538 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1539 }
1540
1541 if (fromguid != 0) {
1542 if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
1543 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1544 "incremental source %#llx no longer exists"),
1545 (longlong_t)fromguid);
1546 return (zfs_error(hdl, EZFS_BADPATH, errbuf));
1547 }
1548 fromname = name;
1549 }
1550
1551 if (flags->verbose) {
1552 uint64_t size = 0;
1553 error = lzc_send_space(zhp->zfs_name, fromname,
1554 lzc_flags, &size);
1555 if (error == 0)
1556 size = MAX(0, (int64_t)(size - bytes));
1557 send_print_verbose(stderr, zhp->zfs_name, fromname,
1558 size, flags->parsable);
1559 }
1560
1561 if (!flags->dryrun) {
1562 progress_arg_t pa = { 0 };
1563 pthread_t tid;
1564 /*
1565 * If progress reporting is requested, spawn a new thread to
1566 * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
1567 */
1568 if (flags->progress) {
1569 pa.pa_zhp = zhp;
1570 pa.pa_fd = outfd;
1571 pa.pa_parsable = flags->parsable;
1572
1573 error = pthread_create(&tid, NULL,
1574 send_progress_thread, &pa);
1575 if (error != 0) {
1576 zfs_close(zhp);
1577 return (error);
1667
1668 if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1669 uint64_t version;
1670 version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1671 if (version >= ZPL_VERSION_SA) {
1672 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1673 }
1674 }
1675
1676 if (flags->dedup && !flags->dryrun) {
1677 featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
1678 DMU_BACKUP_FEATURE_DEDUPPROPS);
1679 if ((err = pipe(pipefd)) != 0) {
1680 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1681 return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
1682 errbuf));
1683 }
1684 dda.outputfd = outfd;
1685 dda.inputfd = pipefd[1];
1686 dda.dedup_hdl = zhp->zfs_hdl;
1687 dda.sendsize = flags->sendsize;
1688 if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
1689 (void) close(pipefd[0]);
1690 (void) close(pipefd[1]);
1691 zfs_error_aux(zhp->zfs_hdl, strerror(errno));
1692 return (zfs_error(zhp->zfs_hdl,
1693 EZFS_THREADCREATEFAILED, errbuf));
1694 }
1695 }
1696
1697 if (flags->replicate || flags->doall || flags->props) {
1698 dmu_replay_record_t drr = { 0 };
1699 char *packbuf = NULL;
1700 size_t buflen = 0;
1701 zio_cksum_t zc = { 0 };
1702
1703 if (flags->replicate || flags->props) {
1704 nvlist_t *hdrnv;
1705
1706 VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
1707 if (fromsnap) {
1726 *debugnvp = hdrnv;
1727 else
1728 nvlist_free(hdrnv);
1729 if (err)
1730 goto stderr_out;
1731 }
1732
1733 if (!flags->dryrun) {
1734 /* write first begin record */
1735 drr.drr_type = DRR_BEGIN;
1736 drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
1737 DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
1738 drr_versioninfo, DMU_COMPOUNDSTREAM);
1739 DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
1740 drr_versioninfo, featureflags);
1741 (void) snprintf(drr.drr_u.drr_begin.drr_toname,
1742 sizeof (drr.drr_u.drr_begin.drr_toname),
1743 "%s@%s", zhp->zfs_name, tosnap);
1744 drr.drr_payloadlen = buflen;
1745
1746 err = dump_record(NULL, &drr, packbuf, buflen, &zc, outfd);
1747 free(packbuf);
1748 if (err != 0)
1749 goto stderr_out;
1750
1751 /* write end record */
1752 bzero(&drr, sizeof (drr));
1753 drr.drr_type = DRR_END;
1754 drr.drr_u.drr_end.drr_checksum = zc;
1755 err = write(outfd, &drr, sizeof (drr));
1756 sdd.hdr_send_sz += sizeof (drr);
1757 if (err == -1) {
1758 err = errno;
1759 goto stderr_out;
1760 }
1761
1762 err = 0;
1763 }
1764 }
1765
1766 /* dump each stream */
1767 sdd.fromsnap = fromsnap;
1768 sdd.tosnap = tosnap;
1769 if (tid != 0)
1770 sdd.outfd = pipefd[0];
1771 else
1772 sdd.outfd = outfd;
1773 sdd.replicate = flags->replicate;
1774 sdd.doall = flags->doall;
1775 sdd.fromorigin = flags->fromorigin;
1776 sdd.fss = fss;
1777 sdd.fsavl = fsavl;
1778 sdd.verbose = flags->verbose;
1779 sdd.dedup = flags->dedup;
1780 sdd.sendsize = flags->sendsize;
1781 sdd.parsable = flags->parsable;
1782 sdd.progress = flags->progress;
1783 sdd.dryrun = flags->dryrun;
1784 sdd.large_block = flags->largeblock;
1785 sdd.embed_data = flags->embed_data;
1786 sdd.compress = flags->compress;
1787 sdd.filter_cb = filter_func;
1788 sdd.filter_cb_arg = cb_arg;
1789 if (debugnvp)
1790 sdd.debugnv = *debugnvp;
1791 if (sdd.verbose && sdd.dryrun)
1792 sdd.std_out = B_TRUE;
1793 fout = sdd.std_out ? stdout : stderr;
1794
1795 /*
1796 * Some flags require that we place user holds on the datasets that are
1797 * being sent so they don't get destroyed during the send. We can skip
1798 * this step if the pool is imported read-only since the datasets cannot
1799 * be destroyed.
1800 */
1801 if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
1802 ZPOOL_PROP_READONLY, NULL) &&
1803 zfs_spa_version(zhp, &spa_version) == 0 &&
1804 spa_version >= SPA_VERSION_USERREFS &&
1805 (flags->doall || flags->replicate)) {
1806 ++holdseq;
1807 (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
1808 ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
1809 sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
1810 if (sdd.cleanup_fd < 0) {
1811 err = errno;
1812 goto stderr_out;
1813 }
1814 sdd.snapholds = fnvlist_alloc();
1815 } else {
1816 sdd.cleanup_fd = -1;
1817 sdd.snapholds = NULL;
1818 }
1819 if ((flags->verbose && !flags->sendsize) || sdd.snapholds != NULL) {
1820 /*
1821 * Do a verbose no-op dry run to get all the verbose output
1822 * or to gather snapshot hold's before generating any data,
1823 * then do a non-verbose real run to generate the streams.
1824 */
1825 sdd.dryrun = B_TRUE;
1826 err = dump_filesystems(zhp, &sdd);
1827
1828 if (err != 0)
1829 goto stderr_out;
1830
1831 if (flags->verbose) {
1832 if (flags->parsable) {
1833 (void) fprintf(fout, "size\t%llu\n",
1834 (longlong_t)sdd.size);
1835 } else {
1836 char buf[16];
1837 zfs_nicenum(sdd.size, buf, sizeof (buf));
1838 (void) fprintf(fout, dgettext(TEXT_DOMAIN,
1839 "total estimated size is %s\n"), buf);
1859 sdd.snapholds = NULL;
1860 }
1861
1862 sdd.dryrun = B_FALSE;
1863 sdd.verbose = B_FALSE;
1864 }
1865
1866 err = dump_filesystems(zhp, &sdd);
1867 fsavl_destroy(fsavl);
1868 nvlist_free(fss);
1869
1870 /* Ensure no snaps found is treated as an error. */
1871 if (err == 0 && !sdd.seento)
1872 err = ENOENT;
1873
1874 if (tid != 0) {
1875 if (err != 0)
1876 (void) pthread_cancel(tid);
1877 (void) close(pipefd[0]);
1878 (void) pthread_join(tid, NULL);
1879 sdd.send_sz = dda.dedup_data_sz;
1880 }
1881
1882 if (sdd.cleanup_fd != -1) {
1883 VERIFY(0 == close(sdd.cleanup_fd));
1884 sdd.cleanup_fd = -1;
1885 }
1886
1887 if (!flags->dryrun && (flags->replicate || flags->doall ||
1888 flags->props)) {
1889 /*
1890 * write final end record. NB: want to do this even if
1891 * there was some error, because it might not be totally
1892 * failed.
1893 */
1894 dmu_replay_record_t drr = { 0 };
1895 drr.drr_type = DRR_END;
1896 if (write(outfd, &drr, sizeof (drr)) == -1) {
1897 return (zfs_standard_error(zhp->zfs_hdl,
1898 errno, errbuf));
1899 }
1900 sdd.hdr_send_sz += sizeof (drr);
1901 }
1902
1903 if (flags->sendsize) {
1904 if (flags->verbose) {
1905 (void) fprintf(stderr,
1906 "Send stream header size (bytes): %u\n",
1907 sdd.hdr_send_sz);
1908 (void) fprintf(stderr,
1909 "Send stream data size (bytes): %llu\n",
1910 (longlong_t)sdd.send_sz);
1911 (void) fprintf(stderr,
1912 "Total send stream size (bytes): %llu\n",
1913 (longlong_t)(sdd.send_sz +
1914 (uint64_t)sdd.hdr_send_sz));
1915 } else {
1916 (void) fprintf(stderr,
1917 "Total send stream size (bytes): %llu\n",
1918 (longlong_t)(sdd.send_sz +
1919 (uint64_t)sdd.hdr_send_sz));
1920 }
1921 }
1922
1923 return (err || sdd.err);
1924
1925 stderr_out:
1926 err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
1927 err_out:
1928 fsavl_destroy(fsavl);
1929 nvlist_free(fss);
1930 fnvlist_free(sdd.snapholds);
1931
1932 if (sdd.cleanup_fd != -1)
1933 VERIFY(0 == close(sdd.cleanup_fd));
1934 if (tid != 0) {
1935 (void) pthread_cancel(tid);
1936 (void) close(pipefd[0]);
1937 (void) pthread_join(tid, NULL);
1938 }
1939 return (err);
1940 }
1941
1942 int
2050 err = nvlist_unpack(buf, len, nvp, 0);
2051 free(buf);
2052 if (err != 0) {
2053 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
2054 "stream (malformed nvlist)"));
2055 return (EINVAL);
2056 }
2057 return (0);
2058 }
2059
2060 static int
2061 recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
2062 int baselen, char *newname, recvflags_t *flags)
2063 {
2064 static int seq;
2065 zfs_cmd_t zc = { 0 };
2066 int err;
2067 prop_changelist_t *clp;
2068 zfs_handle_t *zhp;
2069
2070 if (!zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET))
2071 return (ENOENT);
2072
2073 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2074 if (zhp == NULL)
2075 return (-1);
2076 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2077 flags->force ? MS_FORCE : 0);
2078 zfs_close(zhp);
2079 if (clp == NULL)
2080 return (-1);
2081 err = changelist_prefix(clp);
2082 if (err)
2083 return (err);
2084
2085 zc.zc_objset_type = DMU_OST_ZFS;
2086 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2087
2088 if (tryname) {
2089 (void) strcpy(newname, tryname);
2090 (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
2091 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2092
2093 if (flags->verbose) {
2094 char errbuf[1024];
2095 (void) snprintf(errbuf, sizeof (errbuf),
2096 dgettext(TEXT_DOMAIN,
2097 "attempting to rename '%s' to '%s': "),
2098 zc.zc_name, zc.zc_value);
2099 if (err == 0) {
2100 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2101 "%s: success\n"), errbuf);
2102 } else {
2103 (void) zfs_standard_error(hdl, errno, errbuf);
2104 }
2105 }
2106
2107 if (err == 0)
2108 changelist_rename(clp, name, tryname);
2109
2110 } else {
2111 err = ENOENT;
2112 }
2113
2114 if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
2115 seq++;
2116
2117 (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
2118 "%.*srecv-%u-%u", baselen, name, getpid(), seq);
2119 (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
2120 err = ioctl(hdl->libzfs_fd, ZFS_IOC_RENAME, &zc);
2121
2122 if (flags->verbose) {
2123 char errbuf[1024];
2124 (void) snprintf(errbuf, sizeof (errbuf),
2125 dgettext(TEXT_DOMAIN,
2126 "attempting to temporarily rename '%s' to '%s': "),
2127 zc.zc_name, zc.zc_value);
2128 if (err == 0) {
2129 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2130 "%s: success\n"), errbuf);
2131 } else {
2132 (void) zfs_standard_error(hdl, errno, errbuf);
2133 }
2134 }
2135
2136 if (err == 0)
2137 changelist_rename(clp, name, newname);
2138
2139 err = EAGAIN;
2140 }
2141
2142 (void) changelist_postfix(clp);
2143 changelist_free(clp);
2144
2145 return (err);
2146 }
2147
2148 static int
2149 recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
2150 char *newname, recvflags_t *flags)
2151 {
2152 zfs_cmd_t zc = { 0 };
2153 int err = 0;
2154 prop_changelist_t *clp;
2155 zfs_handle_t *zhp;
2156 boolean_t defer = B_FALSE;
2157 int spa_version;
2158
2159 if (!zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET))
2160 return (ENOENT);
2161
2162 zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2163 if (zhp == NULL)
2164 return (-1);
2165 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
2166 flags->force ? MS_FORCE : 0);
2167 if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2168 zfs_spa_version(zhp, &spa_version) == 0 &&
2169 spa_version >= SPA_VERSION_USERREFS)
2170 defer = B_TRUE;
2171 zfs_close(zhp);
2172 if (clp == NULL)
2173 return (-1);
2174 err = changelist_prefix(clp);
2175 if (err)
2176 return (err);
2177
2178 zc.zc_objset_type = DMU_OST_ZFS;
2179 zc.zc_defer_destroy = defer;
2180 (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
2181 err = ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc);
2182
2183 if (flags->verbose) {
2184 char errbuf[1024];
2185 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2186 "attempting to destroy '%s'"), zc.zc_name);
2187 if (err == 0) {
2188 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2189 "%s: success\n"), errbuf);
2190 } else {
2191 (void) zfs_standard_error(hdl, errno, errbuf);
2192 }
2193 }
2194
2195 if (err == 0)
2196 changelist_remove(clp, zc.zc_name);
2197
2198 (void) changelist_postfix(clp);
2199 changelist_free(clp);
2200
2201 /*
2202 * Deferred destroy might destroy the snapshot or only mark it to be
2203 * destroyed later, and it returns success in either case.
2204 */
2205 if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
2206 ZFS_TYPE_SNAPSHOT))) {
2207 err = recv_rename(hdl, name, NULL, baselen, newname, flags);
2208 }
2209
2210 return (err);
2211 }
2212
2213 typedef struct guid_to_name_data {
2214 uint64_t guid;
2215 boolean_t bookmark_ok;
2216 char *name;
2217 char *skip;
2284 if (err != EEXIST)
2285 err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
2286 if (err != EEXIST && bookmark_ok)
2287 err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
2288 zfs_close(zhp);
2289 if (err == EEXIST)
2290 return (0);
2291
2292 /*
2293 * Remember the last portion of the dataset so we skip it next
2294 * time through (as we've already searched that portion of the
2295 * hierarchy).
2296 */
2297 gtnd.skip = strrchr(pname, '/') + 1;
2298 }
2299
2300 return (ENOENT);
2301 }
2302
2303 /*
2304 * Returns a value:
2305 * +1 - promote is reqired
2306 * 0 - promote is not required
2307 * -1 - an error is occured
2308 */
2309 static int
2310 check_promote(libzfs_handle_t *hdl, avl_tree_t *avl,
2311 uint64_t guid1, uint64_t guid2)
2312 {
2313 nvlist_t *nvfs;
2314 char *fsname, *snapname;
2315 uint64_t create1, create2;
2316
2317 /* the local dataset is not cloned */
2318 if (guid2 == 0)
2319 return (0);
2320
2321 /* the stream dataset is not cloned */
2322 if (guid1 == 0)
2323 return (1);
2324
2325 nvfs = fsavl_find(avl, guid1, &snapname);
2326 if (nvfs == NULL)
2327 return (0);
2328 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2329 create1 = get_snap_txg(hdl, fsname, snapname);
2330
2331 nvfs = fsavl_find(avl, guid2, &snapname);
2332 if (nvfs == NULL)
2333 return (0);
2334 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2335 create2 = get_snap_txg(hdl, fsname, snapname);
2336
2337 if (create1 == 0 || create2 == 0)
2338 return (-1);
2339
2340 if (create1 < create2)
2341 return (1);
2342
2343 return (0);
2344 }
2345
2346 static int
2347 recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
2348 recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2349 nvlist_t *renamed, nvlist_t *limitds)
2350 {
2351 nvlist_t *local_nv;
2352 avl_tree_t *local_avl;
2353 nvpair_t *fselem, *nextfselem;
2354 char *fromsnap;
2355 char newname[ZFS_MAX_DATASET_NAME_LEN];
2356 int error;
2357 boolean_t needagain, progress, recursive;
2358 char *s1, *s2;
2359
2360 VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
2361
2362 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
2363 ENOENT);
2364
2365 if (flags->dryrun)
2366 return (0);
2367
2368 again:
2369 needagain = progress = B_FALSE;
2370
2371 if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
2372 recursive, B_FALSE, &local_nv, &local_avl)) != 0)
2373 return (error);
2374
2375 /*
2376 * Process deletes and renames
2377 */
2378 for (fselem = nvlist_next_nvpair(local_nv, NULL);
2379 fselem; fselem = nextfselem) {
2380 nvlist_t *nvfs, *snaps;
2381 nvlist_t *stream_nvfs = NULL;
2382 nvpair_t *snapelem, *nextsnapelem;
2383 uint64_t fromguid = 0;
2384 uint64_t originguid = 0;
2385 uint64_t stream_originguid = 0;
2386 uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
2387 char *fsname, *stream_fsname;
2388 boolean_t stream_fs_exists = B_FALSE;
2389 boolean_t stream_originfs_exists = B_FALSE;
2390
2391 nextfselem = nvlist_next_nvpair(local_nv, fselem);
2392
2393 VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
2394 VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
2395 VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
2396 VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
2397 &parent_fromsnap_guid));
2398 (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
2399
2400 if (!nvlist_empty(limitds) && !nvlist_exists(limitds, fsname)) {
2401 if (flags->verbose) {
2402 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2403 "skip receiving for excluded '%s'\n"),
2404 fsname);
2405 }
2406 continue;
2407 }
2408
2409 /*
2410 * First find the stream's fs, so we can check for
2411 * a different origin (due to "zfs promote")
2412 * and for preserving snapshots on the receiving side
2413 */
2414 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2415 snapelem != NULL; snapelem = nextsnapelem) {
2416 uint64_t snapguid;
2417
2418 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2419 VERIFY(0 == nvpair_value_uint64(snapelem, &snapguid));
2420 stream_nvfs = fsavl_find(stream_avl, snapguid, NULL);
2421
2422 if (stream_nvfs != NULL) {
2423 stream_fs_exists = B_TRUE;
2424 break;
2425 }
2426 }
2427
2428 /* Check the stream's fs for origin snapshot */
2429 if (stream_fs_exists && originguid != 0) {
2430 nvlist_t *stream_snaps;
2431
2432 VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "snaps",
2433 &stream_snaps));
2434
2435 for (snapelem = nvlist_next_nvpair(stream_snaps, NULL);
2436 snapelem != NULL; snapelem = nextsnapelem) {
2437 uint64_t stream_snapguid;
2438
2439 nextsnapelem = nvlist_next_nvpair(stream_snaps,
2440 snapelem);
2441 VERIFY(0 == nvpair_value_uint64(snapelem,
2442 &stream_snapguid));
2443
2444 if (stream_snapguid == originguid) {
2445 stream_originfs_exists = B_TRUE;
2446 break;
2447 }
2448 }
2449 }
2450
2451 /* check for promote */
2452 (void) nvlist_lookup_uint64(stream_nvfs, "origin",
2453 &stream_originguid);
2454 if (originguid != stream_originguid && stream_originfs_exists) {
2455 switch (check_promote(hdl, local_avl,
2456 stream_originguid, originguid)) {
2457 case 0:
2458 break;
2459 case 1: {
2460 /* promote it! */
2461 zfs_cmd_t zc = { 0 };
2462 char *origin_fsname;
2463
2464 VERIFY(0 == nvlist_lookup_string(nvfs,
2465 "origin_fsname", &origin_fsname));
2466 (void) strlcpy(zc.zc_value, origin_fsname,
2467 sizeof (zc.zc_value));
2468 (void) strlcpy(zc.zc_name, fsname,
2469 sizeof (zc.zc_name));
2470 error = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2471
2472 if (flags->verbose) {
2473 char errbuf[1024];
2474 (void) snprintf(errbuf, sizeof (errbuf),
2475 dgettext(TEXT_DOMAIN,
2476 "attempting to promote '%s': "),
2477 zc.zc_name);
2478 if (error == 0) {
2479 (void) fprintf(stderr,
2480 dgettext(TEXT_DOMAIN,
2481 "%s: success\n"), errbuf);
2482 } else {
2483 (void) zfs_standard_error(hdl,
2484 errno, errbuf);
2485 }
2486 }
2487
2488 if (error == 0)
2489 progress = B_TRUE;
2490
2491 /*
2492 * We had/have the wrong origin, therefore our
2493 * list of snapshots is wrong. Need to handle
2494 * them on the next pass.
2495 */
2496
2497 needagain = B_TRUE;
2498 goto out;
2499 }
2500 default:
2501 progress = B_FALSE;
2502 needagain = B_FALSE;
2503 goto out;
2504 }
2505 }
2506
2507 for (snapelem = nvlist_next_nvpair(snaps, NULL);
2508 snapelem != NULL; snapelem = nextsnapelem) {
2509 uint64_t snapguid;
2510 char *stream_snapname;
2511 nvlist_t *found, *props;
2512
2513 nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
2514
2515 VERIFY(0 == nvpair_value_uint64(snapelem, &snapguid));
2516 found = fsavl_find(stream_avl, snapguid,
2517 &stream_snapname);
2518
2519 /* check for delete */
2520 if (found == NULL) {
2521 char name[ZFS_MAX_DATASET_NAME_LEN];
2522
2523 /*
2524 * Conventional force-receive (-F) behavior
2525 * combines two different steps:
2526 * 1. rollback the destination dataset to the
2527 * most recent received snapshot
2528 * 2. destroy all those destination snapshots
2529 * that are not present at the source
2530 * The keepsnap flag allows to effectively
2531 * separate 1 from 2 and perform forced receive
2532 * while still maintaining the destination
2533 * snapshots as per the corresponding snapshot
2534 * retention policy (at the destination).
2535 */
2536
2537 /*
2538 * When -F (force-receive) is not specified we
2539 * always keep snapshots at the destination
2540 * (i.e., this has always been zfs conventional
2541 * behavior). See also 'keepsnap' comment below
2542 */
2543 if (!flags->force)
2544 continue;
2545
2546 /*
2547 * keepsnap flag modifies the conventional
2548 * force-receive behavior not to destroy
2549 * destination snapshots that are not present
2550 * at the replication source
2551 */
2552 if (flags->keepsnap && stream_fs_exists)
2553 continue;
2554
2555 /*
2556 * Destroy destination snapshots that do
2557 * not exist at the replication source
2558 */
2559 (void) snprintf(name, sizeof (name), "%s@%s",
2560 fsname, nvpair_name(snapelem));
2561
2562 error = recv_destroy(hdl, name,
2563 strlen(fsname)+1, newname, flags);
2564
2565 if (error == 0)
2566 progress = B_TRUE;
2567 else
2568 needagain = B_TRUE;
2569
2570 continue;
2571 }
2572
2573 stream_nvfs = found;
2574
2575 if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2576 &props) && 0 == nvlist_lookup_nvlist(props,
2577 stream_snapname, &props)) {
2578 zfs_cmd_t zc = { 0 };
2579
2580 zc.zc_cookie = B_TRUE; /* received */
2581 (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2582 "%s@%s", fsname, nvpair_name(snapelem));
2583 if (zcmd_write_src_nvlist(hdl, &zc,
2584 props) == 0) {
2585 (void) zfs_ioctl(hdl,
2586 ZFS_IOC_SET_PROP, &zc);
2587 zcmd_free_nvlists(&zc);
2588 }
2589 }
2590
2591 /* check for different snapname */
2592 if (strcmp(nvpair_name(snapelem),
2593 stream_snapname) != 0) {
2594 char name[ZFS_MAX_DATASET_NAME_LEN];
2595 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2596
2597 (void) snprintf(name, sizeof (name), "%s@%s",
2598 fsname, nvpair_name(snapelem));
2599 (void) snprintf(tryname, sizeof (name), "%s@%s",
2600 fsname, stream_snapname);
2601
2602 error = recv_rename(hdl, name, tryname,
2603 strlen(fsname)+1, newname, flags);
2604
2605 if (error == 0)
2606 progress = B_TRUE;
2607 else
2608 needagain = B_TRUE;
2609 }
2610
2611 if (strcmp(stream_snapname, fromsnap) == 0)
2612 fromguid = snapguid;
2613 }
2614
2615 /* check for delete */
2616 if (stream_nvfs == NULL) {
2617 if (!flags->force)
2618 continue;
2619
2620 error = recv_destroy(hdl, fsname, strlen(tofs)+1,
2621 newname, flags);
2622
2623 switch (error) {
2624 case 0:
2625 progress = B_TRUE;
2626 break;
2627 case EAGAIN:
2628 progress = B_TRUE;
2629 needagain = B_TRUE;
2630 goto out;
2631 default:
2632 needagain = B_TRUE;
2633 break;
2634 }
2635
2636 continue;
2637 }
2638
2639 /* skip destroyed or re-created datasets */
2640 if (fromguid == 0)
2641 continue;
2642
2643 VERIFY(0 == nvlist_lookup_string(stream_nvfs,
2644 "name", &stream_fsname));
2645 VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
2646 "parentfromsnap", &stream_parent_fromsnap_guid));
2647
2648 s1 = strrchr(fsname, '/');
2649 s2 = strrchr(stream_fsname, '/');
2650
2651 /*
2652 * Check for rename. If the exact receive path is specified, it
2653 * does not count as a rename, but we still need to check the
2654 * datasets beneath it.
2655 */
2656 if ((stream_parent_fromsnap_guid != 0 &&
2657 parent_fromsnap_guid != 0 &&
2658 stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
2659 ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
2660 (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
2661 nvlist_t *parent;
2662 char tryname[ZFS_MAX_DATASET_NAME_LEN];
2663
2664 parent = fsavl_find(local_avl,
2665 stream_parent_fromsnap_guid, NULL);
2666 /*
2667 * NB: parent might not be found if we used the
2668 * tosnap for stream_parent_fromsnap_guid,
2669 * because the parent is a newly-created fs;
2670 * we'll be able to rename it after we recv the
2671 * new fs.
2672 */
2673 if (parent != NULL) {
2674 char *pname;
2675
2676 VERIFY(0 == nvlist_lookup_string(parent, "name",
2677 &pname));
2678 (void) snprintf(tryname, sizeof (tryname),
2679 "%s%s", pname, strrchr(stream_fsname, '/'));
2680 } else {
2681 tryname[0] = '\0';
2682 if (flags->verbose) {
2683 (void) fprintf(stderr,
2684 dgettext(TEXT_DOMAIN,
2685 "parent dataset not found for "
2686 "local dataset '%s'\n"), fsname);
2687 }
2688 }
2689
2690 newname[0] = '\0';
2691
2692 error = recv_rename(hdl, fsname, tryname,
2693 strlen(tofs)+1, newname, flags);
2694
2695 if (renamed != NULL && newname[0] != '\0') {
2696 VERIFY(0 == nvlist_add_boolean(renamed,
2697 newname));
2698 }
2699
2700 if (error == 0)
2701 progress = B_TRUE;
2702 else
2703 needagain = B_TRUE;
2704 }
2705 }
2706
2707 out:
2708 fsavl_destroy(local_avl);
2709 nvlist_free(local_nv);
2710
2711 if (needagain && progress) {
2712 /* do another pass to fix up temporary names */
2713 if (flags->verbose)
2714 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2715 "another pass for promote, destroy and rename:\n"));
2716 goto again;
2717 }
2718
2719 return (needagain);
2720 }
2721
2722 static int
2723 zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
2724 recvflags_t *flags, nvlist_t *exprops, nvlist_t *limitds,
2725 dmu_replay_record_t *drr, zio_cksum_t *zc, char **top_zfs, int cleanup_fd,
2726 uint64_t *action_handlep)
2727 {
2728 nvlist_t *stream_nv = NULL;
2729 avl_tree_t *stream_avl = NULL;
2730 char *fromsnap = NULL;
2731 char *sendsnap = NULL;
2732 char *cp;
2733 char tofs[ZFS_MAX_DATASET_NAME_LEN];
2734 char sendfs[ZFS_MAX_DATASET_NAME_LEN];
2735 char errbuf[1024];
2736 dmu_replay_record_t drre;
2737 int error;
2738 boolean_t anyerr = B_FALSE;
2739 boolean_t softerr = B_FALSE;
2740 boolean_t recursive;
2741
2742 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
2743 "cannot receive"));
2744
2745 assert(drr->drr_type == DRR_BEGIN);
2746 assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
2787 BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
2788 }
2789 if (drre.drr_type != DRR_END) {
2790 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2791 goto out;
2792 }
2793 if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
2794 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2795 "incorrect header checksum"));
2796 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2797 goto out;
2798 }
2799
2800 (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
2801
2802 if (drr->drr_payloadlen != 0) {
2803 nvlist_t *stream_fss;
2804
2805 VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
2806 &stream_fss));
2807 error = fsavl_create(stream_fss, &stream_avl);
2808 if (error != 0) {
2809 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2810 "couldn't allocate avl tree"));
2811 if (error == ENOMEM)
2812 error = zfs_error(hdl, EZFS_NOMEM, errbuf);
2813 else
2814 error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
2815
2816 goto out;
2817 }
2818
2819 if (fromsnap != NULL && recursive) {
2820 nvlist_t *renamed = NULL;
2821 nvpair_t *pair = NULL;
2822
2823 (void) strlcpy(tofs, destname, sizeof (tofs));
2824 if (flags->isprefix) {
2825 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2826 int i;
2827
2828 if (flags->istail) {
2829 cp = strrchr(drrb->drr_toname, '/');
2830 if (cp == NULL) {
2831 (void) strlcat(tofs, "/",
2832 sizeof (tofs));
2833 i = 0;
2834 } else {
2835 i = (cp - drrb->drr_toname);
2836 }
2837 } else {
2838 i = strcspn(drrb->drr_toname, "/@");
2839 }
2840 /* zfs_receive_one() will create_parents() */
2841 (void) strlcat(tofs, &drrb->drr_toname[i],
2842 sizeof (tofs));
2843 *strchr(tofs, '@') = '\0';
2844 }
2845
2846 if (!flags->dryrun && !flags->nomount) {
2847 VERIFY(0 == nvlist_alloc(&renamed,
2848 NV_UNIQUE_NAME, 0));
2849 }
2850
2851 softerr = recv_incremental_replication(hdl, tofs, flags,
2852 stream_nv, stream_avl, renamed, limitds);
2853
2854 /* Unmount renamed filesystems before receiving. */
2855 while ((pair = nvlist_next_nvpair(renamed,
2856 pair)) != NULL) {
2857 zfs_handle_t *zhp;
2858 prop_changelist_t *clp = NULL;
2859
2860 zhp = zfs_open(hdl, nvpair_name(pair),
2861 ZFS_TYPE_FILESYSTEM);
2862 if (zhp != NULL) {
2863 clp = changelist_gather(zhp,
2864 ZFS_PROP_MOUNTPOINT, 0, 0);
2865 zfs_close(zhp);
2866 if (clp != NULL) {
2867 softerr |=
2868 changelist_prefix(clp);
2869 changelist_free(clp);
2870 }
2871 }
2872 }
2886 *cp = '\0';
2887 /*
2888 * Find the "sendsnap", the final snapshot in a replication
2889 * stream. zfs_receive_one() handles certain errors
2890 * differently, depending on if the contained stream is the
2891 * last one or not.
2892 */
2893 sendsnap = (cp + 1);
2894 }
2895
2896 /* Finally, receive each contained stream */
2897 do {
2898 /*
2899 * we should figure out if it has a recoverable
2900 * error, in which case do a recv_skip() and drive on.
2901 * Note, if we fail due to already having this guid,
2902 * zfs_receive_one() will take care of it (ie,
2903 * recv_skip() and return 0).
2904 */
2905 error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
2906 exprops, limitds, sendfs, stream_nv, stream_avl, top_zfs,
2907 cleanup_fd, action_handlep, sendsnap);
2908 if (error == ENODATA) {
2909 error = 0;
2910 break;
2911 }
2912 anyerr |= error;
2913 } while (error == 0);
2914
2915 if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
2916 /*
2917 * Now that we have the fs's they sent us, try the
2918 * renames again.
2919 */
2920 softerr = recv_incremental_replication(hdl, tofs, flags,
2921 stream_nv, stream_avl, NULL, limitds);
2922 }
2923
2924 out:
2925 fsavl_destroy(stream_avl);
2926 nvlist_free(stream_nv);
2927 if (softerr)
2928 error = -2;
2929 if (anyerr)
2930 error = -1;
2931 return (error);
2932 }
2933
2934 static void
2935 trunc_prop_errs(int truncated)
2936 {
2937 ASSERT(truncated != 0);
2938
2939 if (truncated == 1)
2940 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
2941 "1 more property could not be set\n"));
3051 if (zhp == NULL)
3052 return;
3053
3054 char token_buf[ZFS_MAXPROPLEN];
3055 int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
3056 token_buf, sizeof (token_buf),
3057 NULL, NULL, 0, B_TRUE);
3058 if (error == 0) {
3059 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3060 "checksum mismatch or incomplete stream.\n"
3061 "Partially received snapshot is saved.\n"
3062 "A resuming stream can be generated on the sending "
3063 "system by running:\n"
3064 " zfs send -t %s"),
3065 token_buf);
3066 }
3067 zfs_close(zhp);
3068 }
3069
3070 /*
3071 * Calculate a list of properties for the current dataset taking into account
3072 * stream properties (props) and the properties specified on the command line
3073 * using -x and/or -o options (exprops)
3074 *
3075 * This calculation:
3076 * - Removes excluded properties (booleans)
3077 * - Changes the values of overridden properties (strings)
3078 *
3079 */
3080 static int
3081 props_override(char *dsname, nvlist_t *props, nvlist_t *exprops,
3082 nvlist_t **merged_propsp, recvflags_t *flags, libzfs_handle_t *hdl,
3083 zfs_type_t type, uint64_t zoned, zfs_handle_t *zhp,
3084 zpool_handle_t *zpool_hdl, const char *errbuf)
3085 {
3086 nvlist_t *goprops, *gxprops, *merged_props, *vprops;
3087 nvpair_t *pair;
3088 int ret = 0;
3089
3090 if (nvlist_empty(props) || nvlist_empty(exprops))
3091 return (0); /* No properties */
3092
3093 if (nvlist_dup(props, &merged_props, 0) != 0)
3094 return (-1);
3095
3096 VERIFY(nvlist_alloc(&goprops, NV_UNIQUE_NAME, 0) == 0);
3097 VERIFY(nvlist_alloc(&gxprops, NV_UNIQUE_NAME, 0) == 0);
3098
3099 /* build lists to process in order */
3100 for (pair = nvlist_next_nvpair(exprops, NULL); pair != NULL;
3101 pair = nvlist_next_nvpair(exprops, pair)) {
3102 const char *propname = nvpair_name(pair);
3103 switch (nvpair_type(pair)) {
3104 case DATA_TYPE_BOOLEAN:
3105 VERIFY0(nvlist_add_nvpair(gxprops, pair));
3106 break;
3107 case DATA_TYPE_STRING:
3108 VERIFY0(nvlist_add_nvpair(goprops, pair));
3109 break;
3110 default:
3111 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
3112 "property '%s' must be a string or boolean"),
3113 propname);
3114 /* should never happen, so assert */
3115 assert(B_FALSE);
3116 }
3117 }
3118
3119 /* convert override properties e.g. strings to native */
3120 if ((vprops = zfs_valid_proplist(hdl, type, goprops, zoned, zhp,
3121 zpool_hdl, errbuf)) == NULL)
3122 goto error;
3123
3124 nvlist_free(goprops);
3125 goprops = vprops;
3126
3127 /* override / set properties */
3128 for (nvpair_t *pair = nvlist_next_nvpair(goprops, NULL); pair != NULL;
3129 pair = nvlist_next_nvpair(goprops, pair)) {
3130 const char *pname = nvpair_name(pair);
3131 if (!nvlist_exists(gxprops, pname)) {
3132 if (flags->verbose) {
3133 (void) printf("%s %s property from %s\n",
3134 nvlist_exists(merged_props, pname) ?
3135 "overriding" : "setting", pname, dsname);
3136 }
3137 VERIFY0(nvlist_add_nvpair(merged_props, pair));
3138 }
3139 }
3140
3141 /* exclude properties */
3142 for (nvpair_t *pair = nvlist_next_nvpair(gxprops, NULL); pair != NULL;
3143 pair = nvlist_next_nvpair(gxprops, pair)) {
3144 const char *pname = nvpair_name(pair);
3145 if (nvlist_exists(merged_props, pname)) {
3146 if (flags->verbose) {
3147 (void) printf("excluding %s property "
3148 "from %s\n", pname, dsname);
3149 }
3150 VERIFY0(nvlist_remove_all(merged_props, pname));
3151 }
3152 }
3153
3154 *merged_propsp = merged_props;
3155
3156 error:
3157 if (0 != ret)
3158 nvlist_free(merged_props);
3159 nvlist_free(goprops);
3160 nvlist_free(gxprops);
3161
3162 return (ret);
3163 }
3164
3165 /*
3166 * Restores a backup of tosnap from the file descriptor specified by infd.
3167 */
3168 static int
3169 zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3170 const char *originsnap, recvflags_t *flags, nvlist_t *exprops,
3171 nvlist_t *limitds, dmu_replay_record_t *drr,
3172 dmu_replay_record_t *drr_noswap,
3173 const char *sendfs, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3174 char **top_zfs, int cleanup_fd, uint64_t *action_handlep, const char *finalsnap)
3175 {
3176 zfs_cmd_t zc = { 0 };
3177 time_t begin_time;
3178 int ioctl_err, ioctl_errno, err;
3179 char *cp;
3180 struct drr_begin *drrb = &drr->drr_u.drr_begin;
3181 char dsname[ZFS_MAX_DATASET_NAME_LEN];
3182 char errbuf[1024];
3183 char prop_errbuf[1024];
3184 const char *chopprefix;
3185 boolean_t newfs = B_FALSE;
3186 boolean_t stream_wantsnewfs;
3187 uint64_t parent_snapguid = 0;
3188 prop_changelist_t *clp = NULL;
3189 nvlist_t *snapprops_nvlist = NULL, *props = NULL, *merged_props = NULL;
3190 zprop_errflags_t prop_errflags;
3191 boolean_t recursive, skip;
3192 char *snapname = NULL;
3193
3194 begin_time = time(NULL);
3195
3196 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3197 "cannot receive"));
3198
3199 recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3200 ENOENT);
3201
3202 if (stream_avl != NULL) {
3203 nvlist_t *snapprops;
3204 nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3205 &snapname);
3206
3207 (void) nvlist_lookup_uint64(fs, "parentfromsnap",
3208 &parent_snapguid);
3209 err = nvlist_lookup_nvlist(fs, "props", &props);
3210 if (err)
3211 VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
3212
3213 if (flags->canmountoff) {
3214 VERIFY(0 == nvlist_add_uint64(props,
3215 zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
3216 }
3217
3218 if (err) {
3219 nvlist_free(props);
3220 props = NULL;
3221 }
3222
3223 if (0 == nvlist_lookup_nvlist(fs, "snapprops", &snapprops)) {
3224 VERIFY(0 == nvlist_lookup_nvlist(snapprops,
3225 snapname, &snapprops_nvlist));
3226 }
3227 }
3228
3229 cp = NULL;
3230
3231 /*
3232 * Determine how much of the snapshot name stored in the stream
3233 * we are going to tack on to the name they specified on the
3234 * command line, and how much we are going to chop off.
3235 *
3236 * If they specified a snapshot, chop the entire name stored in
3237 * the stream.
3238 */
3239 if (flags->istail) {
3240 /*
3241 * A filesystem was specified with -e. We want to tack on only
3242 * the tail of the sent snapshot path.
3243 */
3244 if (strchr(tosnap, '@')) {
3245 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3246 "argument - snapshot not allowed with -e"));
3315 * Determine the name of the origin snapshot, store in zc_string.
3316 */
3317 if (originsnap) {
3318 (void) strncpy(zc.zc_string, originsnap, sizeof (zc.zc_string));
3319 if (flags->verbose)
3320 (void) printf("using provided clone origin %s\n",
3321 zc.zc_string);
3322 } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
3323 if (guid_to_name(hdl, zc.zc_value,
3324 drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3325 zcmd_free_nvlists(&zc);
3326 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3327 "local origin for clone %s does not exist"),
3328 zc.zc_value);
3329 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3330 }
3331 if (flags->verbose)
3332 (void) printf("found clone origin %s\n", zc.zc_string);
3333 }
3334
3335 (void) strcpy(dsname, drrb->drr_toname);
3336 *strchr(dsname, '@') = '\0';
3337
3338 boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3339 DMU_BACKUP_FEATURE_RESUMING;
3340 stream_wantsnewfs = (drrb->drr_fromguid == NULL ||
3341 (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3342
3343 if (stream_wantsnewfs) {
3344 /*
3345 * if the parent fs does not exist, look for it based on
3346 * the parent snap GUID
3347 */
3348 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3349 "cannot receive new filesystem stream"));
3350
3351 (void) strcpy(zc.zc_name, zc.zc_value);
3352 cp = strrchr(zc.zc_name, '/');
3353 if (cp)
3354 *cp = '\0';
3355 if (cp &&
3356 !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
3357 char suffix[ZFS_MAX_DATASET_NAME_LEN];
3442 return (zfs_error(hdl, EZFS_EXISTS, errbuf));
3443 }
3444
3445 if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3446 stream_wantsnewfs) {
3447 /* We can't do online recv in this case */
3448 clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3449 if (clp == NULL) {
3450 zfs_close(zhp);
3451 zcmd_free_nvlists(&zc);
3452 return (-1);
3453 }
3454 if (changelist_prefix(clp) != 0) {
3455 changelist_free(clp);
3456 zfs_close(zhp);
3457 zcmd_free_nvlists(&zc);
3458 return (-1);
3459 }
3460 }
3461
3462 /* convert override properties e.g. strings to native */
3463 if (!nvlist_empty(exprops) && props_override(dsname, props,
3464 exprops, &merged_props, flags, hdl, zhp->zfs_type,
3465 zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl,
3466 errbuf) != 0) {
3467 zfs_close(zhp);
3468 zcmd_free_nvlists(&zc);
3469 return (-1);
3470 }
3471
3472 /*
3473 * If we are resuming a newfs, set newfs here so that we will
3474 * mount it if the recv succeeds this time. We can tell
3475 * that it was a newfs on the first recv because the fs
3476 * itself will be inconsistent (if the fs existed when we
3477 * did the first recv, we would have received it into
3478 * .../%recv).
3479 */
3480 if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
3481 newfs = B_TRUE;
3482
3483 zfs_close(zhp);
3484 } else {
3485 /*
3486 * Destination filesystem does not exist. Therefore we better
3487 * be creating a new filesystem (either from a full backup, or
3488 * a clone). It would therefore be invalid if the user
3489 * specified only the pool name (i.e. if the destination name
3490 * contained no slash character).
3491 */
3493 (cp = strrchr(zc.zc_name, '/')) == NULL) {
3494 zcmd_free_nvlists(&zc);
3495 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3496 "destination '%s' does not exist"), zc.zc_name);
3497 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3498 }
3499
3500 /*
3501 * Trim off the final dataset component so we perform the
3502 * recvbackup ioctl to the filesystems's parent.
3503 */
3504 *cp = '\0';
3505
3506 if (flags->isprefix && !flags->istail && !flags->dryrun &&
3507 create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
3508 zcmd_free_nvlists(&zc);
3509 return (zfs_error(hdl, EZFS_BADRESTORE, errbuf));
3510 }
3511
3512 newfs = B_TRUE;
3513
3514 if (!nvlist_empty(exprops)) {
3515 /* Create an override set of properties if needed */
3516 uint64_t zoned = 0;
3517 char zp_name[MAXNAMELEN];
3518 zpool_handle_t *zp_handle;
3519 if (flags->isprefix && !flags->istail &&
3520 !flags->dryrun) {
3521 /* Check if we're zoned or not */
3522 if (check_parents(hdl, zc.zc_value, &zoned,
3523 B_FALSE, NULL) != 0) {
3524 zcmd_free_nvlists(&zc);
3525 return (-1);
3526 }
3527 }
3528
3529 (void) strlcpy(zp_name, zc.zc_name, sizeof (zp_name));
3530 cp = strchr(zp_name, '/');
3531 if (cp != NULL)
3532 *cp = '\0';
3533 zp_handle = zpool_open(hdl, zp_name);
3534 if (zp_handle != NULL &&
3535 props_override(dsname, props,exprops,
3536 &merged_props, flags, hdl, ZFS_TYPE_DATASET,
3537 zoned, NULL, zp_handle, errbuf) != 0) {
3538 zcmd_free_nvlists(&zc);
3539 zpool_close(zp_handle);
3540 return (-1);
3541 }
3542 if (zp_handle != NULL)
3543 zpool_close(zp_handle);
3544 }
3545 }
3546
3547 zc.zc_begin_record = *drr_noswap;
3548 zc.zc_cookie = infd;
3549 zc.zc_guid = flags->force;
3550 zc.zc_resumable = flags->resumable;
3551 skip = !nvlist_empty(limitds) && !nvlist_exists(limitds, dsname);
3552 if (flags->verbose) {
3553 (void) printf("%s %s stream of %s into %s\n",
3554 skip ? (flags->dryrun ? "would skip" : "skipping") :
3555 (flags->dryrun ? "would receive" : "receiving"),
3556 drrb->drr_fromguid ? "incremental" : "full",
3557 drrb->drr_toname, zc.zc_value);
3558 (void) fflush(stdout);
3559 }
3560
3561 if (flags->dryrun || skip) {
3562 zcmd_free_nvlists(&zc);
3563 return (recv_skip(hdl, infd, flags->byteswap));
3564 }
3565
3566 zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
3567 zc.zc_nvlist_dst_size = sizeof (prop_errbuf);
3568 zc.zc_cleanup_fd = cleanup_fd;
3569 zc.zc_action_handle = *action_handlep;
3570
3571 /*
3572 * if we ended up overriding props, use the merged ones,
3573 * otherwise use the ones that we got from the send stream
3574 */
3575 if (merged_props) {
3576 if (zcmd_write_src_nvlist(hdl, &zc, merged_props) != 0) {
3577 nvlist_free(merged_props);
3578 return (-1);
3579 }
3580 nvlist_free(merged_props);
3581 } else if (props && zcmd_write_src_nvlist(hdl, &zc, props) != 0)
3582 return (-1);
3583
3584 err = ioctl_err = zfs_ioctl(hdl, ZFS_IOC_RECV, &zc);
3585 ioctl_errno = errno;
3586 prop_errflags = (zprop_errflags_t)zc.zc_obj;
3587
3588 if (err == 0) {
3589 nvlist_t *prop_errors;
3590 VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
3591 zc.zc_nvlist_dst_size, &prop_errors, 0));
3592
3593 nvpair_t *prop_err = NULL;
3594
3595 while ((prop_err = nvlist_next_nvpair(prop_errors,
3596 prop_err)) != NULL) {
3597 char tbuf[1024];
3598 zfs_prop_t prop;
3599 int intval;
3600
3601 prop = zfs_name_to_prop(nvpair_name(prop_err));
3602 (void) nvpair_value_int32(prop_err, &intval);
3603 if (strcmp(nvpair_name(prop_err),
3709 zc.zc_value);
3710 *cp = '@';
3711 break;
3712 case EINVAL:
3713 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3714 break;
3715 case ECKSUM:
3716 recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
3717 (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3718 break;
3719 case ENOTSUP:
3720 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3721 "pool must be upgraded to receive this stream."));
3722 (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
3723 break;
3724 case EDQUOT:
3725 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3726 "destination %s space quota exceeded"), zc.zc_name);
3727 (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
3728 break;
3729 case EKZFS_WBCNOTSUP:
3730 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3731 "write back cached datasets do not support recv"));
3732 (void) zfs_error(hdl, EZFS_WBCNOTSUP, errbuf);
3733 break;
3734 default:
3735 (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
3736 }
3737 }
3738
3739 /*
3740 * Mount the target filesystem (if created). Also mount any
3741 * children of the target filesystem if we did a replication
3742 * receive (indicated by stream_avl being non-NULL).
3743 */
3744 cp = strchr(zc.zc_value, '@');
3745 if (cp && (ioctl_err == 0 || !newfs)) {
3746 zfs_handle_t *h;
3747
3748 *cp = '\0';
3749 h = zfs_open(hdl, zc.zc_value,
3750 ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3751 if (h != NULL) {
3752 if (h->zfs_type == ZFS_TYPE_VOLUME) {
3753 *cp = '@';
3790
3791 if (flags->verbose) {
3792 char buf1[64];
3793 char buf2[64];
3794 uint64_t bytes = zc.zc_cookie;
3795 time_t delta = time(NULL) - begin_time;
3796 if (delta == 0)
3797 delta = 1;
3798 zfs_nicenum(bytes, buf1, sizeof (buf1));
3799 zfs_nicenum(bytes/delta, buf2, sizeof (buf1));
3800
3801 (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3802 buf1, delta, buf2);
3803 }
3804
3805 return (0);
3806 }
3807
3808 static int
3809 zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
3810 const char *originsnap, recvflags_t *flags,
3811 int infd, nvlist_t *exprops, nvlist_t *limitds, const char *sendfs,
3812 nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3813 uint64_t *action_handlep, const char *finalsnap)
3814 {
3815 int err;
3816 dmu_replay_record_t drr, drr_noswap;
3817 struct drr_begin *drrb = &drr.drr_u.drr_begin;
3818 char errbuf[1024];
3819 zio_cksum_t zcksum = { 0 };
3820 uint64_t featureflags;
3821 int hdrtype;
3822
3823 (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3824 "cannot receive"));
3825
3826 if (flags->isprefix &&
3827 !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3828 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
3829 "(%s) does not exist"), tosnap);
3830 return (zfs_error(hdl, EZFS_NOENT, errbuf));
3831 }
3894 return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
3895 }
3896
3897 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
3898 char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
3899 if (sendfs == NULL) {
3900 /*
3901 * We were not called from zfs_receive_package(). Get
3902 * the fs specified by 'zfs send'.
3903 */
3904 char *cp;
3905 (void) strlcpy(nonpackage_sendfs,
3906 drr.drr_u.drr_begin.drr_toname,
3907 sizeof (nonpackage_sendfs));
3908 if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
3909 *cp = '\0';
3910 sendfs = nonpackage_sendfs;
3911 VERIFY(finalsnap == NULL);
3912 }
3913 return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
3914 exprops, limitds, &drr, &drr_noswap, sendfs, stream_nv,
3915 stream_avl, top_zfs, cleanup_fd, action_handlep, finalsnap));
3916 } else {
3917 assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3918 DMU_COMPOUNDSTREAM);
3919 return (zfs_receive_package(hdl, infd, tosnap, flags, exprops,
3920 limitds, &drr, &zcksum, top_zfs, cleanup_fd,
3921 action_handlep));
3922 }
3923 }
3924
3925 /*
3926 * Restores a backup of tosnap from the file descriptor specified by infd.
3927 * Return 0 on total success, -2 if some things couldn't be
3928 * destroyed/renamed/promoted, -1 if some things couldn't be received.
3929 * (-1 will override -2, if -1 and the resumable flag was specified the
3930 * transfer can be resumed if the sending side supports it).
3931 */
3932 int
3933 zfs_receive(libzfs_handle_t *hdl, const char *tosnap, recvflags_t *flags,
3934 int infd, nvlist_t *exprops, nvlist_t *limitds, avl_tree_t *stream_avl)
3935 {
3936 char *top_zfs = NULL;
3937 int err;
3938 int cleanup_fd;
3939 uint64_t action_handle = 0;
3940 char *originsnap = NULL;
3941 if (exprops) {
3942 err = nvlist_lookup_string(exprops, "origin", &originsnap);
3943 if (err && err != ENOENT)
3944 return (err);
3945 }
3946
3947 cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
3948 VERIFY(cleanup_fd >= 0);
3949
3950 err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, exprops,
3951 limitds, NULL, NULL, stream_avl, &top_zfs, cleanup_fd,
3952 &action_handle, NULL);
3953
3954 VERIFY(0 == close(cleanup_fd));
3955
3956 if (err == 0 && !flags->nomount && top_zfs) {
3957 zfs_handle_t *zhp;
3958 prop_changelist_t *clp;
3959
3960 zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
3961 if (zhp != NULL) {
3962 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
3963 CL_GATHER_MOUNT_ALWAYS, 0);
3964 zfs_close(zhp);
3965 if (clp != NULL) {
3966 /* mount and share received datasets */
3967 err = changelist_postfix(clp);
3968 changelist_free(clp);
3969 }
3970 }
3971 if (zhp == NULL || clp == NULL || err)
3972 err = -1;
|