13cb34c60Sahrens /*
23cb34c60Sahrens * CDDL HEADER START
33cb34c60Sahrens *
43cb34c60Sahrens * The contents of this file are subject to the terms of the
53cb34c60Sahrens * Common Development and Distribution License (the "License").
63cb34c60Sahrens * You may not use this file except in compliance with the License.
73cb34c60Sahrens *
83cb34c60Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
93cb34c60Sahrens * or http://www.opensolaris.org/os/licensing.
103cb34c60Sahrens * See the License for the specific language governing permissions
113cb34c60Sahrens * and limitations under the License.
123cb34c60Sahrens *
133cb34c60Sahrens * When distributing Covered Code, include this CDDL HEADER in each
143cb34c60Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
153cb34c60Sahrens * If applicable, add the following below this CDDL HEADER, with the
163cb34c60Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
173cb34c60Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
183cb34c60Sahrens *
193cb34c60Sahrens * CDDL HEADER END
203cb34c60Sahrens */
213cb34c60Sahrens
223cb34c60Sahrens /*
23dc7cd546SMark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
249c3fd121SMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
2508ae7ca2SJerry Jelinek * Copyright 2019 Joyent, Inc.
260d8fa8f8SMartin Matuska * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
27a7a845e4SSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
285878fad7SDan McDonald * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
29c3d26abcSMatthew Ahrens * Copyright (c) 2014 Integros [integros.com]
3088f61deeSIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
316ccda740Sloli * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved.
326ccda740Sloli * Copyright (c) 2018 Datto Inc.
333cb34c60Sahrens */
343cb34c60Sahrens
353cb34c60Sahrens #include <assert.h>
363cb34c60Sahrens #include <ctype.h>
373cb34c60Sahrens #include <errno.h>
383cb34c60Sahrens #include <libintl.h>
393cb34c60Sahrens #include <stdio.h>
403cb34c60Sahrens #include <stdlib.h>
413cb34c60Sahrens #include <strings.h>
423cb34c60Sahrens #include <unistd.h>
433cb34c60Sahrens #include <stddef.h>
443cb34c60Sahrens #include <fcntl.h>
453cb34c60Sahrens #include <sys/mount.h>
469e69d7d0SLori Alt #include <pthread.h>
479e69d7d0SLori Alt #include <umem.h>
484e3c9f44SBill Pijewski #include <time.h>
493cb34c60Sahrens
503cb34c60Sahrens #include <libzfs.h>
515d7b4d43SMatthew Ahrens #include <libzfs_core.h>
52d8ab6e12SDon Brady #include <libzutil.h>
533cb34c60Sahrens
543cb34c60Sahrens #include "zfs_namecheck.h"
553cb34c60Sahrens #include "zfs_prop.h"
56495db6fbSLori Alt #include "zfs_fletcher.h"
573cb34c60Sahrens #include "libzfs_impl.h"
589c3fd121SMatthew Ahrens #include <zlib.h>
599e69d7d0SLori Alt #include <sha2.h>
608e714474SLori Alt #include <sys/zio_checksum.h>
61eb633035STom Caputi #include <sys/dsl_crypt.h>
628e714474SLori Alt #include <sys/ddt.h>
633cb34c60Sahrens
6492241e0bSTom Erickson /* in libzfs_dataset.c */
6592241e0bSTom Erickson extern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
6692241e0bSTom Erickson
67a2cdcdd2SPaul Dagnelie static int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
68a2cdcdd2SPaul Dagnelie recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
69a60ca23dSTom Caputi uint64_t *, const char *, nvlist_t *);
709c3fd121SMatthew Ahrens static int guid_to_name(libzfs_handle_t *, const char *,
719c3fd121SMatthew Ahrens uint64_t, boolean_t, char *);
720069fd67STim Haley
739e69d7d0SLori Alt static const zio_cksum_t zero_cksum = { 0 };
749e69d7d0SLori Alt
759e69d7d0SLori Alt typedef struct dedup_arg {
769e69d7d0SLori Alt int inputfd;
779e69d7d0SLori Alt int outputfd;
789e69d7d0SLori Alt libzfs_handle_t *dedup_hdl;
799e69d7d0SLori Alt } dedup_arg_t;
809e69d7d0SLori Alt
814e3c9f44SBill Pijewski typedef struct progress_arg {
824e3c9f44SBill Pijewski zfs_handle_t *pa_zhp;
834e3c9f44SBill Pijewski int pa_fd;
844e3c9f44SBill Pijewski boolean_t pa_parsable;
854e3c9f44SBill Pijewski } progress_arg_t;
864e3c9f44SBill Pijewski
879e69d7d0SLori Alt typedef struct dataref {
889e69d7d0SLori Alt uint64_t ref_guid;
899e69d7d0SLori Alt uint64_t ref_object;
909e69d7d0SLori Alt uint64_t ref_offset;
919e69d7d0SLori Alt } dataref_t;
929e69d7d0SLori Alt
939e69d7d0SLori Alt typedef struct dedup_entry {
949e69d7d0SLori Alt struct dedup_entry *dde_next;
959e69d7d0SLori Alt zio_cksum_t dde_chksum;
968e714474SLori Alt uint64_t dde_prop;
979e69d7d0SLori Alt dataref_t dde_ref;
989e69d7d0SLori Alt } dedup_entry_t;
999e69d7d0SLori Alt
1009e69d7d0SLori Alt #define MAX_DDT_PHYSMEM_PERCENT 20
1019e69d7d0SLori Alt #define SMALLEST_POSSIBLE_MAX_DDT_MB 128
1029e69d7d0SLori Alt
1039e69d7d0SLori Alt typedef struct dedup_table {
1049e69d7d0SLori Alt dedup_entry_t **dedup_hash_array;
1059e69d7d0SLori Alt umem_cache_t *ddecache;
1069e69d7d0SLori Alt uint64_t max_ddt_size; /* max dedup table size in bytes */
1079e69d7d0SLori Alt uint64_t cur_ddt_size; /* current dedup table size in bytes */
1089e69d7d0SLori Alt uint64_t ddt_count;
1099e69d7d0SLori Alt int numhashbits;
1109e69d7d0SLori Alt boolean_t ddt_full;
1119e69d7d0SLori Alt } dedup_table_t;
1129e69d7d0SLori Alt
1139e69d7d0SLori Alt static int
high_order_bit(uint64_t n)1149e69d7d0SLori Alt high_order_bit(uint64_t n)
1159e69d7d0SLori Alt {
1169e69d7d0SLori Alt int count;
1179e69d7d0SLori Alt
1189e69d7d0SLori Alt for (count = 0; n != 0; count++)
1199e69d7d0SLori Alt n >>= 1;
1209e69d7d0SLori Alt return (count);
1219e69d7d0SLori Alt }
1229e69d7d0SLori Alt
1239e69d7d0SLori Alt static size_t
ssread(void * buf,size_t len,FILE * stream)1249e69d7d0SLori Alt ssread(void *buf, size_t len, FILE *stream)
1259e69d7d0SLori Alt {
1269e69d7d0SLori Alt size_t outlen;
1279e69d7d0SLori Alt
1289e69d7d0SLori Alt if ((outlen = fread(buf, len, 1, stream)) == 0)
1299e69d7d0SLori Alt return (0);
1309e69d7d0SLori Alt
1319e69d7d0SLori Alt return (outlen);
1329e69d7d0SLori Alt }
1339e69d7d0SLori Alt
1349e69d7d0SLori Alt static void
ddt_hash_append(libzfs_handle_t * hdl,dedup_table_t * ddt,dedup_entry_t ** ddepp,zio_cksum_t * cs,uint64_t prop,dataref_t * dr)1359e69d7d0SLori Alt ddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
1368e714474SLori Alt zio_cksum_t *cs, uint64_t prop, dataref_t *dr)
1379e69d7d0SLori Alt {
1389e69d7d0SLori Alt dedup_entry_t *dde;
1399e69d7d0SLori Alt
1409e69d7d0SLori Alt if (ddt->cur_ddt_size >= ddt->max_ddt_size) {
1419e69d7d0SLori Alt if (ddt->ddt_full == B_FALSE) {
1429e69d7d0SLori Alt zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1439e69d7d0SLori Alt "Dedup table full. Deduplication will continue "
1449e69d7d0SLori Alt "with existing table entries"));
1459e69d7d0SLori Alt ddt->ddt_full = B_TRUE;
1469e69d7d0SLori Alt }
1479e69d7d0SLori Alt return;
1489e69d7d0SLori Alt }
1499e69d7d0SLori Alt
1509e69d7d0SLori Alt if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
1519e69d7d0SLori Alt != NULL) {
1529e69d7d0SLori Alt assert(*ddepp == NULL);
1539e69d7d0SLori Alt dde->dde_next = NULL;
1549e69d7d0SLori Alt dde->dde_chksum = *cs;
1558e714474SLori Alt dde->dde_prop = prop;
1569e69d7d0SLori Alt dde->dde_ref = *dr;
1579e69d7d0SLori Alt *ddepp = dde;
1589e69d7d0SLori Alt ddt->cur_ddt_size += sizeof (dedup_entry_t);
1599e69d7d0SLori Alt ddt->ddt_count++;
1609e69d7d0SLori Alt }
1619e69d7d0SLori Alt }
1629e69d7d0SLori Alt
1639e69d7d0SLori Alt /*
1649e69d7d0SLori Alt * Using the specified dedup table, do a lookup for an entry with
1659e69d7d0SLori Alt * the checksum cs. If found, return the block's reference info
1669e69d7d0SLori Alt * in *dr. Otherwise, insert a new entry in the dedup table, using
1679e69d7d0SLori Alt * the reference information specified by *dr.
1689e69d7d0SLori Alt *
1699e69d7d0SLori Alt * return value: true - entry was found
1709e69d7d0SLori Alt * false - entry was not found
1719e69d7d0SLori Alt */
1729e69d7d0SLori Alt static boolean_t
ddt_update(libzfs_handle_t * hdl,dedup_table_t * ddt,zio_cksum_t * cs,uint64_t prop,dataref_t * dr)1739e69d7d0SLori Alt ddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
1748e714474SLori Alt uint64_t prop, dataref_t *dr)
1759e69d7d0SLori Alt {
1769e69d7d0SLori Alt uint32_t hashcode;
1779e69d7d0SLori Alt dedup_entry_t **ddepp;
1789e69d7d0SLori Alt
1799e69d7d0SLori Alt hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
1809e69d7d0SLori Alt
1819e69d7d0SLori Alt for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
1829e69d7d0SLori Alt ddepp = &((*ddepp)->dde_next)) {
1838e714474SLori Alt if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
1848e714474SLori Alt (*ddepp)->dde_prop == prop) {
1859e69d7d0SLori Alt *dr = (*ddepp)->dde_ref;
1869e69d7d0SLori Alt return (B_TRUE);
1879e69d7d0SLori Alt }
1889e69d7d0SLori Alt }
1898e714474SLori Alt ddt_hash_append(hdl, ddt, ddepp, cs, prop, dr);
1909e69d7d0SLori Alt return (B_FALSE);
1919e69d7d0SLori Alt }
1929e69d7d0SLori Alt
1939e69d7d0SLori Alt static int
dump_record(dmu_replay_record_t * drr,void * payload,int payload_len,zio_cksum_t * zc,int outfd)19498110f08SMatthew Ahrens dump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
19598110f08SMatthew Ahrens zio_cksum_t *zc, int outfd)
1969e69d7d0SLori Alt {
19798110f08SMatthew Ahrens ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
19898110f08SMatthew Ahrens ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
199770499e1SDan Kimmel (void) fletcher_4_incremental_native(drr,
20098110f08SMatthew Ahrens offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
20198110f08SMatthew Ahrens if (drr->drr_type != DRR_BEGIN) {
20298110f08SMatthew Ahrens ASSERT(ZIO_CHECKSUM_IS_ZERO(&drr->drr_u.
20398110f08SMatthew Ahrens drr_checksum.drr_checksum));
20498110f08SMatthew Ahrens drr->drr_u.drr_checksum.drr_checksum = *zc;
20598110f08SMatthew Ahrens }
206770499e1SDan Kimmel (void) fletcher_4_incremental_native(
207770499e1SDan Kimmel &drr->drr_u.drr_checksum.drr_checksum, sizeof (zio_cksum_t), zc);
20898110f08SMatthew Ahrens if (write(outfd, drr, sizeof (*drr)) == -1)
20998110f08SMatthew Ahrens return (errno);
21098110f08SMatthew Ahrens if (payload_len != 0) {
211770499e1SDan Kimmel (void) fletcher_4_incremental_native(payload, payload_len, zc);
21298110f08SMatthew Ahrens if (write(outfd, payload, payload_len) == -1)
21398110f08SMatthew Ahrens return (errno);
21498110f08SMatthew Ahrens }
21598110f08SMatthew Ahrens return (0);
2169e69d7d0SLori Alt }
2179e69d7d0SLori Alt
2189e69d7d0SLori Alt /*
2199e69d7d0SLori Alt * This function is started in a separate thread when the dedup option
2209e69d7d0SLori Alt * has been requested. The main send thread determines the list of
2219e69d7d0SLori Alt * snapshots to be included in the send stream and makes the ioctl calls
2229e69d7d0SLori Alt * for each one. But instead of having the ioctl send the output to the
2239e69d7d0SLori Alt * the output fd specified by the caller of zfs_send()), the
2249e69d7d0SLori Alt * ioctl is told to direct the output to a pipe, which is read by the
2259e69d7d0SLori Alt * alternate thread running THIS function. This function does the
2269e69d7d0SLori Alt * dedup'ing by:
2279e69d7d0SLori Alt * 1. building a dedup table (the DDT)
2289e69d7d0SLori Alt * 2. doing checksums on each data block and inserting a record in the DDT
2299e69d7d0SLori Alt * 3. looking for matching checksums, and
2309e69d7d0SLori Alt * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
2319e69d7d0SLori Alt * a duplicate block is found.
2329e69d7d0SLori Alt * The output of this function then goes to the output fd requested
2339e69d7d0SLori Alt * by the caller of zfs_send().
2349e69d7d0SLori Alt */
2359e69d7d0SLori Alt static void *
cksummer(void * arg)2369e69d7d0SLori Alt cksummer(void *arg)
2379e69d7d0SLori Alt {
2389e69d7d0SLori Alt dedup_arg_t *dda = arg;
239b5152584SMatthew Ahrens char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
2409e69d7d0SLori Alt dmu_replay_record_t thedrr;
2419e69d7d0SLori Alt dmu_replay_record_t *drr = &thedrr;
2429e69d7d0SLori Alt FILE *ofp;
2439e69d7d0SLori Alt int outfd;
2449e69d7d0SLori Alt dedup_table_t ddt;
2459e69d7d0SLori Alt zio_cksum_t stream_cksum;
2469e69d7d0SLori Alt uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
2479e69d7d0SLori Alt uint64_t numbuckets;
2489e69d7d0SLori Alt
2499e69d7d0SLori Alt ddt.max_ddt_size =
25098110f08SMatthew Ahrens MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
25198110f08SMatthew Ahrens SMALLEST_POSSIBLE_MAX_DDT_MB << 20);
2529e69d7d0SLori Alt
25398110f08SMatthew Ahrens numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
2549e69d7d0SLori Alt
2559e69d7d0SLori Alt /*
2569e69d7d0SLori Alt * numbuckets must be a power of 2. Increase number to
2579e69d7d0SLori Alt * a power of 2 if necessary.
2589e69d7d0SLori Alt */
2599e69d7d0SLori Alt if (!ISP2(numbuckets))
2609e69d7d0SLori Alt numbuckets = 1 << high_order_bit(numbuckets);
2619e69d7d0SLori Alt
2629e69d7d0SLori Alt ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
2639e69d7d0SLori Alt ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
2649e69d7d0SLori Alt NULL, NULL, NULL, NULL, NULL, 0);
2659e69d7d0SLori Alt ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
2669e69d7d0SLori Alt ddt.numhashbits = high_order_bit(numbuckets) - 1;
2679e69d7d0SLori Alt ddt.ddt_full = B_FALSE;
2689e69d7d0SLori Alt
2699e69d7d0SLori Alt outfd = dda->outputfd;
2709e69d7d0SLori Alt ofp = fdopen(dda->inputfd, "r");
27198110f08SMatthew Ahrens while (ssread(drr, sizeof (*drr), ofp) != 0) {
2729e69d7d0SLori Alt
2737c13517fSSerapheim Dimitropoulos /*
2747c13517fSSerapheim Dimitropoulos * kernel filled in checksum, we are going to write same
2757c13517fSSerapheim Dimitropoulos * record, but need to regenerate checksum.
2767c13517fSSerapheim Dimitropoulos */
2777c13517fSSerapheim Dimitropoulos if (drr->drr_type != DRR_BEGIN) {
2787c13517fSSerapheim Dimitropoulos bzero(&drr->drr_u.drr_checksum.drr_checksum,
2797c13517fSSerapheim Dimitropoulos sizeof (drr->drr_u.drr_checksum.drr_checksum));
2807c13517fSSerapheim Dimitropoulos }
2817c13517fSSerapheim Dimitropoulos
2829e69d7d0SLori Alt switch (drr->drr_type) {
2839e69d7d0SLori Alt case DRR_BEGIN:
2849e69d7d0SLori Alt {
28598110f08SMatthew Ahrens struct drr_begin *drrb = &drr->drr_u.drr_begin;
28698110f08SMatthew Ahrens int fflags;
28798110f08SMatthew Ahrens int sz = 0;
2889e69d7d0SLori Alt ZIO_SET_CHECKSUM(&stream_cksum, 0, 0, 0, 0);
2899e69d7d0SLori Alt
29098110f08SMatthew Ahrens ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
29198110f08SMatthew Ahrens
2929e69d7d0SLori Alt /* set the DEDUP feature flag for this stream */
2939e69d7d0SLori Alt fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
2948e714474SLori Alt fflags |= (DMU_BACKUP_FEATURE_DEDUP |
2958e714474SLori Alt DMU_BACKUP_FEATURE_DEDUPPROPS);
2969e69d7d0SLori Alt DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
2979e69d7d0SLori Alt
2989c3fd121SMatthew Ahrens if (drr->drr_payloadlen != 0) {
29998110f08SMatthew Ahrens sz = drr->drr_payloadlen;
3009e69d7d0SLori Alt
301b5152584SMatthew Ahrens if (sz > SPA_MAXBLOCKSIZE) {
302b5152584SMatthew Ahrens buf = zfs_realloc(dda->dedup_hdl, buf,
303b5152584SMatthew Ahrens SPA_MAXBLOCKSIZE, sz);
3049e69d7d0SLori Alt }
3059e69d7d0SLori Alt (void) ssread(buf, sz, ofp);
3069e69d7d0SLori Alt if (ferror(stdin))
3079e69d7d0SLori Alt perror("fread");
3089e69d7d0SLori Alt }
30998110f08SMatthew Ahrens if (dump_record(drr, buf, sz, &stream_cksum,
31098110f08SMatthew Ahrens outfd) != 0)
31198110f08SMatthew Ahrens goto out;
3129e69d7d0SLori Alt break;
3139e69d7d0SLori Alt }
3149e69d7d0SLori Alt
3159e69d7d0SLori Alt case DRR_END:
3169e69d7d0SLori Alt {
31798110f08SMatthew Ahrens struct drr_end *drre = &drr->drr_u.drr_end;
3189e69d7d0SLori Alt /* use the recalculated checksum */
31998110f08SMatthew Ahrens drre->drr_checksum = stream_cksum;
32098110f08SMatthew Ahrens if (dump_record(drr, NULL, 0, &stream_cksum,
32198110f08SMatthew Ahrens outfd) != 0)
3229e69d7d0SLori Alt goto out;
3239e69d7d0SLori Alt break;
3249e69d7d0SLori Alt }
3259e69d7d0SLori Alt
3269e69d7d0SLori Alt case DRR_OBJECT:
3279e69d7d0SLori Alt {
32898110f08SMatthew Ahrens struct drr_object *drro = &drr->drr_u.drr_object;
3299e69d7d0SLori Alt if (drro->drr_bonuslen > 0) {
3309e69d7d0SLori Alt (void) ssread(buf,
331eb633035STom Caputi DRR_OBJECT_PAYLOAD_SIZE(drro), ofp);
3329e69d7d0SLori Alt }
333eb633035STom Caputi if (dump_record(drr, buf, DRR_OBJECT_PAYLOAD_SIZE(drro),
33498110f08SMatthew Ahrens &stream_cksum, outfd) != 0)
33598110f08SMatthew Ahrens goto out;
3369e69d7d0SLori Alt break;
3379e69d7d0SLori Alt }
3389e69d7d0SLori Alt
3390a586ceaSMark Shellenbaum case DRR_SPILL:
3400a586ceaSMark Shellenbaum {
34198110f08SMatthew Ahrens struct drr_spill *drrs = &drr->drr_u.drr_spill;
342eb633035STom Caputi (void) ssread(buf, DRR_SPILL_PAYLOAD_SIZE(drrs), ofp);
343eb633035STom Caputi if (dump_record(drr, buf, DRR_SPILL_PAYLOAD_SIZE(drrs),
34498110f08SMatthew Ahrens &stream_cksum, outfd) != 0)
3450a586ceaSMark Shellenbaum goto out;
3460a586ceaSMark Shellenbaum break;
3470a586ceaSMark Shellenbaum }
3480a586ceaSMark Shellenbaum
3499e69d7d0SLori Alt case DRR_FREEOBJECTS:
3509e69d7d0SLori Alt {
35198110f08SMatthew Ahrens if (dump_record(drr, NULL, 0, &stream_cksum,
35298110f08SMatthew Ahrens outfd) != 0)
3539e69d7d0SLori Alt goto out;
3549e69d7d0SLori Alt break;
3559e69d7d0SLori Alt }
3569e69d7d0SLori Alt
3579e69d7d0SLori Alt case DRR_WRITE:
3589e69d7d0SLori Alt {
35998110f08SMatthew Ahrens struct drr_write *drrw = &drr->drr_u.drr_write;
3609e69d7d0SLori Alt dataref_t dataref;
3615602294fSDan Kimmel uint64_t payload_size;
3629e69d7d0SLori Alt
3635602294fSDan Kimmel payload_size = DRR_WRITE_PAYLOAD_SIZE(drrw);
3645602294fSDan Kimmel (void) ssread(buf, payload_size, ofp);
3658e714474SLori Alt
3669e69d7d0SLori Alt /*
3678e714474SLori Alt * Use the existing checksum if it's dedup-capable,
3688e714474SLori Alt * else calculate a SHA256 checksum for it.
3699e69d7d0SLori Alt */
3708e714474SLori Alt
3718e714474SLori Alt if (ZIO_CHECKSUM_EQUAL(drrw->drr_key.ddk_cksum,
3728e714474SLori Alt zero_cksum) ||
373eb633035STom Caputi !DRR_IS_DEDUP_CAPABLE(drrw->drr_flags)) {
3749e69d7d0SLori Alt SHA256_CTX ctx;
3759e69d7d0SLori Alt zio_cksum_t tmpsha256;
3769e69d7d0SLori Alt
3779e69d7d0SLori Alt SHA256Init(&ctx);
3785602294fSDan Kimmel SHA256Update(&ctx, buf, payload_size);
3799e69d7d0SLori Alt SHA256Final(&tmpsha256, &ctx);
3808e714474SLori Alt drrw->drr_key.ddk_cksum.zc_word[0] =
3819e69d7d0SLori Alt BE_64(tmpsha256.zc_word[0]);
3828e714474SLori Alt drrw->drr_key.ddk_cksum.zc_word[1] =
3839e69d7d0SLori Alt BE_64(tmpsha256.zc_word[1]);
3848e714474SLori Alt drrw->drr_key.ddk_cksum.zc_word[2] =
3859e69d7d0SLori Alt BE_64(tmpsha256.zc_word[2]);
3868e714474SLori Alt drrw->drr_key.ddk_cksum.zc_word[3] =
3879e69d7d0SLori Alt BE_64(tmpsha256.zc_word[3]);
3888e714474SLori Alt drrw->drr_checksumtype = ZIO_CHECKSUM_SHA256;
389eb633035STom Caputi drrw->drr_flags |= DRR_CHECKSUM_DEDUP;
3909e69d7d0SLori Alt }
3919e69d7d0SLori Alt
3929e69d7d0SLori Alt dataref.ref_guid = drrw->drr_toguid;
3939e69d7d0SLori Alt dataref.ref_object = drrw->drr_object;
3949e69d7d0SLori Alt dataref.ref_offset = drrw->drr_offset;
3959e69d7d0SLori Alt
3969e69d7d0SLori Alt if (ddt_update(dda->dedup_hdl, &ddt,
3978e714474SLori Alt &drrw->drr_key.ddk_cksum, drrw->drr_key.ddk_prop,
3988e714474SLori Alt &dataref)) {
39998110f08SMatthew Ahrens dmu_replay_record_t wbr_drr = {0};
40098110f08SMatthew Ahrens struct drr_write_byref *wbr_drrr =
40198110f08SMatthew Ahrens &wbr_drr.drr_u.drr_write_byref;
40298110f08SMatthew Ahrens
4039e69d7d0SLori Alt /* block already present in stream */
40498110f08SMatthew Ahrens wbr_drr.drr_type = DRR_WRITE_BYREF;
40598110f08SMatthew Ahrens
4069e69d7d0SLori Alt wbr_drrr->drr_object = drrw->drr_object;
4079e69d7d0SLori Alt wbr_drrr->drr_offset = drrw->drr_offset;
4085602294fSDan Kimmel wbr_drrr->drr_length = drrw->drr_logical_size;
4099e69d7d0SLori Alt wbr_drrr->drr_toguid = drrw->drr_toguid;
4109e69d7d0SLori Alt wbr_drrr->drr_refguid = dataref.ref_guid;
4119e69d7d0SLori Alt wbr_drrr->drr_refobject =
4129e69d7d0SLori Alt dataref.ref_object;
4139e69d7d0SLori Alt wbr_drrr->drr_refoffset =
4149e69d7d0SLori Alt dataref.ref_offset;
4159e69d7d0SLori Alt
4168e714474SLori Alt wbr_drrr->drr_checksumtype =
4178e714474SLori Alt drrw->drr_checksumtype;
418eb633035STom Caputi wbr_drrr->drr_flags = drrw->drr_flags;
4198e714474SLori Alt wbr_drrr->drr_key.ddk_cksum =
4208e714474SLori Alt drrw->drr_key.ddk_cksum;
4218e714474SLori Alt wbr_drrr->drr_key.ddk_prop =
4228e714474SLori Alt drrw->drr_key.ddk_prop;
4239e69d7d0SLori Alt
42498110f08SMatthew Ahrens if (dump_record(&wbr_drr, NULL, 0,
42598110f08SMatthew Ahrens &stream_cksum, outfd) != 0)
4269e69d7d0SLori Alt goto out;
4279e69d7d0SLori Alt } else {
4289e69d7d0SLori Alt /* block not previously seen */
4295602294fSDan Kimmel if (dump_record(drr, buf, payload_size,
43098110f08SMatthew Ahrens &stream_cksum, outfd) != 0)
4319e69d7d0SLori Alt goto out;
4329e69d7d0SLori Alt }
4339e69d7d0SLori Alt break;
4349e69d7d0SLori Alt }
4359e69d7d0SLori Alt
4365d7b4d43SMatthew Ahrens case DRR_WRITE_EMBEDDED:
4375d7b4d43SMatthew Ahrens {
43898110f08SMatthew Ahrens struct drr_write_embedded *drrwe =
43998110f08SMatthew Ahrens &drr->drr_u.drr_write_embedded;
4405d7b4d43SMatthew Ahrens (void) ssread(buf,
4415d7b4d43SMatthew Ahrens P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
44298110f08SMatthew Ahrens if (dump_record(drr, buf,
4435d7b4d43SMatthew Ahrens P2ROUNDUP((uint64_t)drrwe->drr_psize, 8),
44498110f08SMatthew Ahrens &stream_cksum, outfd) != 0)
4455d7b4d43SMatthew Ahrens goto out;
4465d7b4d43SMatthew Ahrens break;
4475d7b4d43SMatthew Ahrens }
4485d7b4d43SMatthew Ahrens
4499e69d7d0SLori Alt case DRR_FREE:
4509e69d7d0SLori Alt {
45198110f08SMatthew Ahrens if (dump_record(drr, NULL, 0, &stream_cksum,
45298110f08SMatthew Ahrens outfd) != 0)
4539e69d7d0SLori Alt goto out;
4549e69d7d0SLori Alt break;
4559e69d7d0SLori Alt }
4569e69d7d0SLori Alt
457eb633035STom Caputi case DRR_OBJECT_RANGE:
458eb633035STom Caputi {
459eb633035STom Caputi if (dump_record(drr, NULL, 0, &stream_cksum,
460eb633035STom Caputi outfd) != 0)
461eb633035STom Caputi goto out;
462eb633035STom Caputi break;
463eb633035STom Caputi }
464eb633035STom Caputi
4659e69d7d0SLori Alt default:
46698110f08SMatthew Ahrens (void) fprintf(stderr, "INVALID record type 0x%x\n",
4679e69d7d0SLori Alt drr->drr_type);
4689e69d7d0SLori Alt /* should never happen, so assert */
4699e69d7d0SLori Alt assert(B_FALSE);
4709e69d7d0SLori Alt }
4719e69d7d0SLori Alt }
4729e69d7d0SLori Alt out:
4739e69d7d0SLori Alt umem_cache_destroy(ddt.ddecache);
4749e69d7d0SLori Alt free(ddt.dedup_hash_array);
4759e69d7d0SLori Alt free(buf);
4769e69d7d0SLori Alt (void) fclose(ofp);
4779e69d7d0SLori Alt
4789e69d7d0SLori Alt return (NULL);
4799e69d7d0SLori Alt }
4809e69d7d0SLori Alt
4813cb34c60Sahrens /*
4823cb34c60Sahrens * Routines for dealing with the AVL tree of fs-nvlists
4833cb34c60Sahrens */
4843cb34c60Sahrens typedef struct fsavl_node {
4853cb34c60Sahrens avl_node_t fn_node;
4863cb34c60Sahrens nvlist_t *fn_nvfs;
4873cb34c60Sahrens char *fn_snapname;
4883cb34c60Sahrens uint64_t fn_guid;
4893cb34c60Sahrens } fsavl_node_t;
4903cb34c60Sahrens
4913cb34c60Sahrens static int
fsavl_compare(const void * arg1,const void * arg2)4923cb34c60Sahrens fsavl_compare(const void *arg1, const void *arg2)
4933cb34c60Sahrens {
494c4ab0d3fSGvozden Neskovic const fsavl_node_t *fn1 = (const fsavl_node_t *)arg1;
495c4ab0d3fSGvozden Neskovic const fsavl_node_t *fn2 = (const fsavl_node_t *)arg2;
496c4ab0d3fSGvozden Neskovic
4974d7988d6SPaul Dagnelie if (fn1->fn_guid > fn2->fn_guid)
4984d7988d6SPaul Dagnelie return (+1);
4994d7988d6SPaul Dagnelie if (fn1->fn_guid < fn2->fn_guid)
5004d7988d6SPaul Dagnelie return (-1);
5014d7988d6SPaul Dagnelie return (0);
5023cb34c60Sahrens }
5033cb34c60Sahrens
5043cb34c60Sahrens /*
5053cb34c60Sahrens * Given the GUID of a snapshot, find its containing filesystem and
5063cb34c60Sahrens * (optionally) name.
5073cb34c60Sahrens */
5083cb34c60Sahrens static nvlist_t *
fsavl_find(avl_tree_t * avl,uint64_t snapguid,char ** snapname)5093cb34c60Sahrens fsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
5103cb34c60Sahrens {
5113cb34c60Sahrens fsavl_node_t fn_find;
5123cb34c60Sahrens fsavl_node_t *fn;
5133cb34c60Sahrens
5143cb34c60Sahrens fn_find.fn_guid = snapguid;
5153cb34c60Sahrens
5163cb34c60Sahrens fn = avl_find(avl, &fn_find, NULL);
5173cb34c60Sahrens if (fn) {
5183cb34c60Sahrens if (snapname)
5193cb34c60Sahrens *snapname = fn->fn_snapname;
5203cb34c60Sahrens return (fn->fn_nvfs);
5213cb34c60Sahrens }
5223cb34c60Sahrens return (NULL);
5233cb34c60Sahrens }
5243cb34c60Sahrens
52500954d7bSahl static void
fsavl_destroy(avl_tree_t * avl)52600954d7bSahl fsavl_destroy(avl_tree_t *avl)
52700954d7bSahl {
52800954d7bSahl fsavl_node_t *fn;
52900954d7bSahl void *cookie;
53000954d7bSahl
53100954d7bSahl if (avl == NULL)
53200954d7bSahl return;
53300954d7bSahl
53400954d7bSahl cookie = NULL;
53500954d7bSahl while ((fn = avl_destroy_nodes(avl, &cookie)) != NULL)
53600954d7bSahl free(fn);
53700954d7bSahl avl_destroy(avl);
53800954d7bSahl free(avl);
53900954d7bSahl }
54000954d7bSahl
54188bb18d2SLori Alt /*
54288bb18d2SLori Alt * Given an nvlist, produce an avl tree of snapshots, ordered by guid
54388bb18d2SLori Alt */
5443cb34c60Sahrens static avl_tree_t *
fsavl_create(nvlist_t * fss)5453cb34c60Sahrens fsavl_create(nvlist_t *fss)
5463cb34c60Sahrens {
5473cb34c60Sahrens avl_tree_t *fsavl;
5483cb34c60Sahrens nvpair_t *fselem = NULL;
5493cb34c60Sahrens
55000954d7bSahl if ((fsavl = malloc(sizeof (avl_tree_t))) == NULL)
55100954d7bSahl return (NULL);
55200954d7bSahl
5533cb34c60Sahrens avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
5543cb34c60Sahrens offsetof(fsavl_node_t, fn_node));
5553cb34c60Sahrens
5563cb34c60Sahrens while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
5573cb34c60Sahrens nvlist_t *nvfs, *snaps;
5583cb34c60Sahrens nvpair_t *snapelem = NULL;
5593cb34c60Sahrens
5603cb34c60Sahrens VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
5613cb34c60Sahrens VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
5623cb34c60Sahrens
5633cb34c60Sahrens while ((snapelem =
5643cb34c60Sahrens nvlist_next_nvpair(snaps, snapelem)) != NULL) {
5653cb34c60Sahrens fsavl_node_t *fn;
5663cb34c60Sahrens uint64_t guid;
5673cb34c60Sahrens
5683cb34c60Sahrens VERIFY(0 == nvpair_value_uint64(snapelem, &guid));
56900954d7bSahl if ((fn = malloc(sizeof (fsavl_node_t))) == NULL) {
57000954d7bSahl fsavl_destroy(fsavl);
57100954d7bSahl return (NULL);
57200954d7bSahl }
5733cb34c60Sahrens fn->fn_nvfs = nvfs;
5743cb34c60Sahrens fn->fn_snapname = nvpair_name(snapelem);
5753cb34c60Sahrens fn->fn_guid = guid;
5763cb34c60Sahrens
5773cb34c60Sahrens /*
5783cb34c60Sahrens * Note: if there are multiple snaps with the
5793cb34c60Sahrens * same GUID, we ignore all but one.
5803cb34c60Sahrens */
5813cb34c60Sahrens if (avl_find(fsavl, fn, NULL) == NULL)
5823cb34c60Sahrens avl_add(fsavl, fn);
5833cb34c60Sahrens else
5843cb34c60Sahrens free(fn);
5853cb34c60Sahrens }
5863cb34c60Sahrens }
5873cb34c60Sahrens
5883cb34c60Sahrens return (fsavl);
5893cb34c60Sahrens }
5903cb34c60Sahrens
5913cb34c60Sahrens /*
5923cb34c60Sahrens * Routines for dealing with the giant nvlist of fs-nvlists, etc.
5933cb34c60Sahrens */
5943cb34c60Sahrens typedef struct send_data {
5954a20c933SAlex Deiter /*
5964a20c933SAlex Deiter * assigned inside every recursive call,
5974a20c933SAlex Deiter * restored from *_save on return:
5984a20c933SAlex Deiter *
5994a20c933SAlex Deiter * guid of fromsnap snapshot in parent dataset
6004a20c933SAlex Deiter * txg of fromsnap snapshot in current dataset
6014a20c933SAlex Deiter * txg of tosnap snapshot in current dataset
6024a20c933SAlex Deiter */
6034a20c933SAlex Deiter
6043cb34c60Sahrens uint64_t parent_fromsnap_guid;
6054a20c933SAlex Deiter uint64_t fromsnap_txg;
6064a20c933SAlex Deiter uint64_t tosnap_txg;
6074a20c933SAlex Deiter
6084a20c933SAlex Deiter /* the nvlists get accumulated during depth-first traversal */
6093cb34c60Sahrens nvlist_t *parent_snaps;
6103cb34c60Sahrens nvlist_t *fss;
611bb0ade09Sahrens nvlist_t *snapprops;
6126ccda740Sloli nvlist_t *snapholds; /* user holds */
6134a20c933SAlex Deiter
6144a20c933SAlex Deiter /* send-receive configuration, does not change during traversal */
6154a20c933SAlex Deiter const char *fsname;
6163cb34c60Sahrens const char *fromsnap;
6173cb34c60Sahrens const char *tosnap;
61892241e0bSTom Erickson boolean_t recursive;
6196ccda740Sloli boolean_t raw;
6204a20c933SAlex Deiter boolean_t verbose;
6216ccda740Sloli boolean_t backup;
6226ccda740Sloli boolean_t holds; /* were holds requested with send -h */
6236ccda740Sloli boolean_t props;
6243cb34c60Sahrens
6253cb34c60Sahrens /*
6263cb34c60Sahrens * The header nvlist is of the following format:
6273cb34c60Sahrens * {
6283cb34c60Sahrens * "tosnap" -> string
6293cb34c60Sahrens * "fromsnap" -> string (if incremental)
6303cb34c60Sahrens * "fss" -> {
6313cb34c60Sahrens * id -> {
6323cb34c60Sahrens *
6333cb34c60Sahrens * "name" -> string (full name; for debugging)
6343cb34c60Sahrens * "parentfromsnap" -> number (guid of fromsnap in parent)
6353cb34c60Sahrens *
6363cb34c60Sahrens * "props" -> { name -> value (only if set here) }
6373cb34c60Sahrens * "snaps" -> { name (lastname) -> number (guid) }
638bb0ade09Sahrens * "snapprops" -> { name (lastname) -> { name -> value } }
6396ccda740Sloli * "snapholds" -> { name (lastname) -> { holdname -> crtime } }
6403cb34c60Sahrens *
6413cb34c60Sahrens * "origin" -> number (guid) (if clone)
642eb633035STom Caputi * "is_encroot" -> boolean
6433cb34c60Sahrens * "sent" -> boolean (not on-disk)
6443cb34c60Sahrens * }
6453cb34c60Sahrens * }
6463cb34c60Sahrens * }
6473cb34c60Sahrens *
6483cb34c60Sahrens */
6493cb34c60Sahrens } send_data_t;
6503cb34c60Sahrens
6516ccda740Sloli static void
6526ccda740Sloli send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv);
653bb0ade09Sahrens
6543cb34c60Sahrens static int
send_iterate_snap(zfs_handle_t * zhp,void * arg)6553cb34c60Sahrens send_iterate_snap(zfs_handle_t *zhp, void *arg)
6563cb34c60Sahrens {
6573cb34c60Sahrens send_data_t *sd = arg;
6583cb34c60Sahrens uint64_t guid = zhp->zfs_dmustats.dds_guid;
6594a20c933SAlex Deiter uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
6603cb34c60Sahrens char *snapname;
661bb0ade09Sahrens nvlist_t *nv;
6623cb34c60Sahrens
6633cb34c60Sahrens snapname = strrchr(zhp->zfs_name, '@')+1;
6643cb34c60Sahrens
6654a20c933SAlex Deiter if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
6664a20c933SAlex Deiter if (sd->verbose) {
6674a20c933SAlex Deiter (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
6684a20c933SAlex Deiter "skipping snapshot %s because it was created "
6694a20c933SAlex Deiter "after the destination snapshot (%s)\n"),
6704a20c933SAlex Deiter zhp->zfs_name, sd->tosnap);
6714a20c933SAlex Deiter }
6724a20c933SAlex Deiter zfs_close(zhp);
6734a20c933SAlex Deiter return (0);
6744a20c933SAlex Deiter }
6754a20c933SAlex Deiter
6763cb34c60Sahrens VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
6773cb34c60Sahrens /*
6783cb34c60Sahrens * NB: if there is no fromsnap here (it's a newly created fs in
6793cb34c60Sahrens * an incremental replication), we will substitute the tosnap.
6803cb34c60Sahrens */
6813cb34c60Sahrens if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
6823cb34c60Sahrens (sd->parent_fromsnap_guid == 0 && sd->tosnap &&
6833cb34c60Sahrens strcmp(snapname, sd->tosnap) == 0)) {
6843cb34c60Sahrens sd->parent_fromsnap_guid = guid;
6853cb34c60Sahrens }
6863cb34c60Sahrens
687bb0ade09Sahrens VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
6886ccda740Sloli send_iterate_prop(zhp, sd->backup, nv);
689bb0ade09Sahrens VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
690bb0ade09Sahrens nvlist_free(nv);
6916ccda740Sloli if (sd->holds) {
6926ccda740Sloli nvlist_t *holds = fnvlist_alloc();
6936ccda740Sloli int err = lzc_get_holds(zhp->zfs_name, &holds);
6946ccda740Sloli if (err == 0) {
6956ccda740Sloli VERIFY(0 == nvlist_add_nvlist(sd->snapholds,
6966ccda740Sloli snapname, holds));
6976ccda740Sloli }
6986ccda740Sloli fnvlist_free(holds);
6996ccda740Sloli }
700bb0ade09Sahrens
7013cb34c60Sahrens zfs_close(zhp);
7023cb34c60Sahrens return (0);
7033cb34c60Sahrens }
7043cb34c60Sahrens
7053cb34c60Sahrens static void
send_iterate_prop(zfs_handle_t * zhp,boolean_t received_only,nvlist_t * nv)7066ccda740Sloli send_iterate_prop(zfs_handle_t *zhp, boolean_t received_only, nvlist_t *nv)
7073cb34c60Sahrens {
7086ccda740Sloli nvlist_t *props = NULL;
7093cb34c60Sahrens nvpair_t *elem = NULL;
7103cb34c60Sahrens
7116ccda740Sloli if (received_only)
7126ccda740Sloli props = zfs_get_recvd_props(zhp);
7136ccda740Sloli else
7146ccda740Sloli props = zhp->zfs_props;
7156ccda740Sloli
7166ccda740Sloli while ((elem = nvlist_next_nvpair(props, elem)) != NULL) {
7173cb34c60Sahrens char *propname = nvpair_name(elem);
7183cb34c60Sahrens zfs_prop_t prop = zfs_name_to_prop(propname);
7193cb34c60Sahrens nvlist_t *propnv;
7203cb34c60Sahrens
721faaa6415SEric Schrock if (!zfs_prop_user(propname)) {
722faaa6415SEric Schrock /*
723faaa6415SEric Schrock * Realistically, this should never happen. However,
724faaa6415SEric Schrock * we want the ability to add DSL properties without
725faaa6415SEric Schrock * needing to make incompatible version changes. We
726faaa6415SEric Schrock * need to ignore unknown properties to allow older
727faaa6415SEric Schrock * software to still send datasets containing these
728faaa6415SEric Schrock * properties, with the unknown properties elided.
729faaa6415SEric Schrock */
730faaa6415SEric Schrock if (prop == ZPROP_INVAL)
731faaa6415SEric Schrock continue;
73214843421SMatthew Ahrens
733faaa6415SEric Schrock if (zfs_prop_readonly(prop))
734faaa6415SEric Schrock continue;
735faaa6415SEric Schrock }
7363cb34c60Sahrens
7373cb34c60Sahrens verify(nvpair_value_nvlist(elem, &propnv) == 0);
738166773ebSSanjeev Bagewadi if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
739166773ebSSanjeev Bagewadi prop == ZFS_PROP_REFQUOTA ||
740166773ebSSanjeev Bagewadi prop == ZFS_PROP_REFRESERVATION) {
74192241e0bSTom Erickson char *source;
7423cb34c60Sahrens uint64_t value;
7433cb34c60Sahrens verify(nvlist_lookup_uint64(propnv,
7443cb34c60Sahrens ZPROP_VALUE, &value) == 0);
745bb0ade09Sahrens if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT)
746bb0ade09Sahrens continue;
74792241e0bSTom Erickson /*
74892241e0bSTom Erickson * May have no source before SPA_VERSION_RECVD_PROPS,
74992241e0bSTom Erickson * but is still modifiable.
75092241e0bSTom Erickson */
75192241e0bSTom Erickson if (nvlist_lookup_string(propnv,
75292241e0bSTom Erickson ZPROP_SOURCE, &source) == 0) {
75392241e0bSTom Erickson if ((strcmp(source, zhp->zfs_name) != 0) &&
75492241e0bSTom Erickson (strcmp(source,
75592241e0bSTom Erickson ZPROP_SOURCE_VAL_RECVD) != 0))
75692241e0bSTom Erickson continue;
75792241e0bSTom Erickson }
7583cb34c60Sahrens } else {
7593cb34c60Sahrens char *source;
7603cb34c60Sahrens if (nvlist_lookup_string(propnv,
7613cb34c60Sahrens ZPROP_SOURCE, &source) != 0)
7623cb34c60Sahrens continue;
76392241e0bSTom Erickson if ((strcmp(source, zhp->zfs_name) != 0) &&
76492241e0bSTom Erickson (strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0))
7653cb34c60Sahrens continue;
7663cb34c60Sahrens }
7673cb34c60Sahrens
7683cb34c60Sahrens if (zfs_prop_user(propname) ||
7693cb34c60Sahrens zfs_prop_get_type(prop) == PROP_TYPE_STRING) {
7703cb34c60Sahrens char *value;
7713cb34c60Sahrens verify(nvlist_lookup_string(propnv,
7723cb34c60Sahrens ZPROP_VALUE, &value) == 0);
7733cb34c60Sahrens VERIFY(0 == nvlist_add_string(nv, propname, value));
7743cb34c60Sahrens } else {
7753cb34c60Sahrens uint64_t value;
7763cb34c60Sahrens verify(nvlist_lookup_uint64(propnv,
7773cb34c60Sahrens ZPROP_VALUE, &value) == 0);
7783cb34c60Sahrens VERIFY(0 == nvlist_add_uint64(nv, propname, value));
7793cb34c60Sahrens }
7803cb34c60Sahrens }
7813cb34c60Sahrens }
7823cb34c60Sahrens
7834a20c933SAlex Deiter /*
7844a20c933SAlex Deiter * returns snapshot creation txg
7854a20c933SAlex Deiter * and returns 0 if the snapshot does not exist
7864a20c933SAlex Deiter */
7874a20c933SAlex Deiter static uint64_t
get_snap_txg(libzfs_handle_t * hdl,const char * fs,const char * snap)7884a20c933SAlex Deiter get_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
7894a20c933SAlex Deiter {
7909adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
7914a20c933SAlex Deiter uint64_t txg = 0;
7924a20c933SAlex Deiter
7934a20c933SAlex Deiter if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
7944a20c933SAlex Deiter return (txg);
7954a20c933SAlex Deiter
7964a20c933SAlex Deiter (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
7974a20c933SAlex Deiter if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
7984a20c933SAlex Deiter zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
7994a20c933SAlex Deiter if (zhp != NULL) {
8004a20c933SAlex Deiter txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
8014a20c933SAlex Deiter zfs_close(zhp);
8024a20c933SAlex Deiter }
8034a20c933SAlex Deiter }
8044a20c933SAlex Deiter
8054a20c933SAlex Deiter return (txg);
8064a20c933SAlex Deiter }
8074a20c933SAlex Deiter
80888bb18d2SLori Alt /*
80988bb18d2SLori Alt * recursively generate nvlists describing datasets. See comment
81088bb18d2SLori Alt * for the data structure send_data_t above for description of contents
81188bb18d2SLori Alt * of the nvlist.
81288bb18d2SLori Alt */
8133cb34c60Sahrens static int
send_iterate_fs(zfs_handle_t * zhp,void * arg)8143cb34c60Sahrens send_iterate_fs(zfs_handle_t *zhp, void *arg)
8153cb34c60Sahrens {
8163cb34c60Sahrens send_data_t *sd = arg;
817eb633035STom Caputi nvlist_t *nvfs = NULL, *nv = NULL;
81892241e0bSTom Erickson int rv = 0;
8193cb34c60Sahrens uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
8204a20c933SAlex Deiter uint64_t fromsnap_txg_save = sd->fromsnap_txg;
8214a20c933SAlex Deiter uint64_t tosnap_txg_save = sd->tosnap_txg;
8224a20c933SAlex Deiter uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
8233cb34c60Sahrens uint64_t guid = zhp->zfs_dmustats.dds_guid;
8244a20c933SAlex Deiter uint64_t fromsnap_txg, tosnap_txg;
8253cb34c60Sahrens char guidstring[64];
8263cb34c60Sahrens
8274a20c933SAlex Deiter fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
8284a20c933SAlex Deiter if (fromsnap_txg != 0)
8294a20c933SAlex Deiter sd->fromsnap_txg = fromsnap_txg;
8304a20c933SAlex Deiter
8314a20c933SAlex Deiter tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
8324a20c933SAlex Deiter if (tosnap_txg != 0)
8334a20c933SAlex Deiter sd->tosnap_txg = tosnap_txg;
8344a20c933SAlex Deiter
8354a20c933SAlex Deiter /*
8364a20c933SAlex Deiter * on the send side, if the current dataset does not have tosnap,
8374a20c933SAlex Deiter * perform two additional checks:
8384a20c933SAlex Deiter *
8394a20c933SAlex Deiter * - skip sending the current dataset if it was created later than
8404a20c933SAlex Deiter * the parent tosnap
8414a20c933SAlex Deiter * - return error if the current dataset was created earlier than
8424a20c933SAlex Deiter * the parent tosnap
8434a20c933SAlex Deiter */
8444a20c933SAlex Deiter if (sd->tosnap != NULL && tosnap_txg == 0) {
8454a20c933SAlex Deiter if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
8464a20c933SAlex Deiter if (sd->verbose) {
8474a20c933SAlex Deiter (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
8484a20c933SAlex Deiter "skipping dataset %s: snapshot %s does "
8494a20c933SAlex Deiter "not exist\n"), zhp->zfs_name, sd->tosnap);
8504a20c933SAlex Deiter }
8514a20c933SAlex Deiter } else {
8524a20c933SAlex Deiter (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
8534a20c933SAlex Deiter "cannot send %s@%s%s: snapshot %s@%s does not "
8544a20c933SAlex Deiter "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
8554a20c933SAlex Deiter dgettext(TEXT_DOMAIN, " recursively") : "",
8564a20c933SAlex Deiter zhp->zfs_name, sd->tosnap);
8574a20c933SAlex Deiter rv = -1;
8584a20c933SAlex Deiter }
8594a20c933SAlex Deiter goto out;
8604a20c933SAlex Deiter }
8614a20c933SAlex Deiter
8623cb34c60Sahrens VERIFY(0 == nvlist_alloc(&nvfs, NV_UNIQUE_NAME, 0));
8633cb34c60Sahrens VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
8643cb34c60Sahrens VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
8653cb34c60Sahrens sd->parent_fromsnap_guid));
8663cb34c60Sahrens
8673cb34c60Sahrens if (zhp->zfs_dmustats.dds_origin[0]) {
8683cb34c60Sahrens zfs_handle_t *origin = zfs_open(zhp->zfs_hdl,
8693cb34c60Sahrens zhp->zfs_dmustats.dds_origin, ZFS_TYPE_SNAPSHOT);
8704a20c933SAlex Deiter if (origin == NULL) {
8714a20c933SAlex Deiter rv = -1;
8724a20c933SAlex Deiter goto out;
8734a20c933SAlex Deiter }
8743cb34c60Sahrens VERIFY(0 == nvlist_add_uint64(nvfs, "origin",
8753cb34c60Sahrens origin->zfs_dmustats.dds_guid));
8763cb34c60Sahrens }
8773cb34c60Sahrens
8783cb34c60Sahrens /* iterate over props */
8796ccda740Sloli if (sd->props || sd->backup || sd->recursive) {
8806ccda740Sloli VERIFY(0 == nvlist_alloc(&nv, NV_UNIQUE_NAME, 0));
8816ccda740Sloli send_iterate_prop(zhp, sd->backup, nv);
8826ccda740Sloli }
883eb633035STom Caputi
884eb633035STom Caputi if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF) {
885eb633035STom Caputi boolean_t encroot;
886eb633035STom Caputi
887eb633035STom Caputi /* determine if this dataset is an encryption root */
888eb633035STom Caputi if (zfs_crypto_get_encryption_root(zhp, &encroot, NULL) != 0) {
889eb633035STom Caputi rv = -1;
890eb633035STom Caputi goto out;
891eb633035STom Caputi }
892eb633035STom Caputi
893eb633035STom Caputi if (encroot)
894eb633035STom Caputi VERIFY(0 == nvlist_add_boolean(nvfs, "is_encroot"));
895eb633035STom Caputi
896eb633035STom Caputi /*
897eb633035STom Caputi * Encrypted datasets can only be sent with properties if
898eb633035STom Caputi * the raw flag is specified because the receive side doesn't
899eb633035STom Caputi * currently have a mechanism for recursively asking the user
900eb633035STom Caputi * for new encryption parameters.
901eb633035STom Caputi */
902eb633035STom Caputi if (!sd->raw) {
903eb633035STom Caputi (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
904eb633035STom Caputi "cannot send %s@%s: encrypted dataset %s may not "
905eb633035STom Caputi "be sent with properties without the raw flag\n"),
906eb633035STom Caputi sd->fsname, sd->tosnap, zhp->zfs_name);
907eb633035STom Caputi rv = -1;
908eb633035STom Caputi goto out;
909eb633035STom Caputi }
910eb633035STom Caputi
911eb633035STom Caputi }
912eb633035STom Caputi
9136ccda740Sloli if (nv != NULL)
9146ccda740Sloli VERIFY(0 == nvlist_add_nvlist(nvfs, "props", nv));
9153cb34c60Sahrens
9163cb34c60Sahrens /* iterate over snaps, and set sd->parent_fromsnap_guid */
9173cb34c60Sahrens sd->parent_fromsnap_guid = 0;
9183cb34c60Sahrens VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
919bb0ade09Sahrens VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
9206ccda740Sloli if (sd->holds)
9216ccda740Sloli VERIFY(0 == nvlist_alloc(&sd->snapholds, NV_UNIQUE_NAME, 0));
9220d8fa8f8SMartin Matuska (void) zfs_iter_snapshots(zhp, B_FALSE, send_iterate_snap, sd);
9233cb34c60Sahrens VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
924bb0ade09Sahrens VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
9256ccda740Sloli if (sd->holds)
9266ccda740Sloli VERIFY(0 == nvlist_add_nvlist(nvfs, "snapholds",
9276ccda740Sloli sd->snapholds));
9283cb34c60Sahrens nvlist_free(sd->parent_snaps);
929bb0ade09Sahrens nvlist_free(sd->snapprops);
9306ccda740Sloli nvlist_free(sd->snapholds);
9313cb34c60Sahrens
932c5286370SAllan Jude /* Do not allow the size of the properties list to exceed the limit */
933c5286370SAllan Jude if ((fnvlist_size(nvfs) + fnvlist_size(sd->fss)) >
934c5286370SAllan Jude zhp->zfs_hdl->libzfs_max_nvlist) {
935c5286370SAllan Jude (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
936c5286370SAllan Jude "warning: cannot send %s@%s: the size of the list of "
937c5286370SAllan Jude "snapshots and properties is too large to be received "
938c5286370SAllan Jude "successfully.\n"
939c5286370SAllan Jude "Select a smaller number of snapshots to send.\n"),
940c5286370SAllan Jude zhp->zfs_name, sd->tosnap);
941c5286370SAllan Jude rv = EZFS_NOSPC;
942c5286370SAllan Jude goto out;
943c5286370SAllan Jude }
9443cb34c60Sahrens /* add this fs to nvlist */
9453cb34c60Sahrens (void) snprintf(guidstring, sizeof (guidstring),
9463cb34c60Sahrens "0x%llx", (longlong_t)guid);
9473cb34c60Sahrens VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
9483cb34c60Sahrens
9493cb34c60Sahrens /* iterate over children */
95092241e0bSTom Erickson if (sd->recursive)
95192241e0bSTom Erickson rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
9523cb34c60Sahrens
9534a20c933SAlex Deiter out:
9543cb34c60Sahrens sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
9554a20c933SAlex Deiter sd->fromsnap_txg = fromsnap_txg_save;
9564a20c933SAlex Deiter sd->tosnap_txg = tosnap_txg_save;
957eb633035STom Caputi nvlist_free(nv);
958eb633035STom Caputi nvlist_free(nvfs);
9593cb34c60Sahrens
9603cb34c60Sahrens zfs_close(zhp);
9613cb34c60Sahrens return (rv);
9623cb34c60Sahrens }
9633cb34c60Sahrens
9643cb34c60Sahrens static int
gather_nvlist(libzfs_handle_t * hdl,const char * fsname,const char * fromsnap,const char * tosnap,boolean_t recursive,boolean_t raw,boolean_t verbose,boolean_t backup,boolean_t holds,boolean_t props,nvlist_t ** nvlp,avl_tree_t ** avlp)9653cb34c60Sahrens gather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
9666ccda740Sloli const char *tosnap, boolean_t recursive, boolean_t raw,
9676ccda740Sloli boolean_t verbose, boolean_t backup, boolean_t holds,
9686ccda740Sloli boolean_t props, nvlist_t **nvlp, avl_tree_t **avlp)
9693cb34c60Sahrens {
9703cb34c60Sahrens zfs_handle_t *zhp;
9713cb34c60Sahrens send_data_t sd = { 0 };
9723cb34c60Sahrens int error;
9733cb34c60Sahrens
9743cb34c60Sahrens zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
9753cb34c60Sahrens if (zhp == NULL)
9763cb34c60Sahrens return (EZFS_BADTYPE);
9773cb34c60Sahrens
9783cb34c60Sahrens VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
9794a20c933SAlex Deiter sd.fsname = fsname;
9803cb34c60Sahrens sd.fromsnap = fromsnap;
9813cb34c60Sahrens sd.tosnap = tosnap;
98292241e0bSTom Erickson sd.recursive = recursive;
983eb633035STom Caputi sd.raw = raw;
9844a20c933SAlex Deiter sd.verbose = verbose;
9856ccda740Sloli sd.backup = backup;
9866ccda740Sloli sd.holds = holds;
9876ccda740Sloli sd.props = props;
98800954d7bSahl
98900954d7bSahl if ((error = send_iterate_fs(zhp, &sd)) != 0) {
99000954d7bSahl nvlist_free(sd.fss);
99100954d7bSahl if (avlp != NULL)
99200954d7bSahl *avlp = NULL;
99300954d7bSahl *nvlp = NULL;
99400954d7bSahl return (error);
99500954d7bSahl }
99600954d7bSahl
99700954d7bSahl if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
99800954d7bSahl nvlist_free(sd.fss);
99900954d7bSahl *nvlp = NULL;
100000954d7bSahl return (EZFS_NOMEM);
100100954d7bSahl }
10023cb34c60Sahrens
10033cb34c60Sahrens *nvlp = sd.fss;
100400954d7bSahl return (0);
10053cb34c60Sahrens }
10063cb34c60Sahrens
10073cb34c60Sahrens /*
10083cb34c60Sahrens * Routines specific to "zfs send"
10093cb34c60Sahrens */
10103cb34c60Sahrens typedef struct send_dump_data {
10113cb34c60Sahrens /* these are all just the short snapname (the part after the @) */
10123cb34c60Sahrens const char *fromsnap;
10133cb34c60Sahrens const char *tosnap;
10149adfa60dSMatthew Ahrens char prevsnap[ZFS_MAX_DATASET_NAME_LEN];
1015a7f53a56SChris Kirby uint64_t prevsnap_obj;
10163cb34c60Sahrens boolean_t seenfrom, seento, replicate, doall, fromorigin;
1017dc5f28a3SManoj Joseph boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
10186ccda740Sloli boolean_t large_block, compress, raw, holds;
10193cb34c60Sahrens int outfd;
10203cb34c60Sahrens boolean_t err;
10213cb34c60Sahrens nvlist_t *fss;
1022a7a845e4SSteven Hartland nvlist_t *snapholds;
10233cb34c60Sahrens avl_tree_t *fsavl;
10249e69d7d0SLori Alt snapfilter_cb_t *filter_cb;
10259e69d7d0SLori Alt void *filter_cb_arg;
10263f9d6ad7SLin Ling nvlist_t *debugnv;
10279adfa60dSMatthew Ahrens char holdtag[ZFS_MAX_DATASET_NAME_LEN];
1028a7f53a56SChris Kirby int cleanup_fd;
102919b94df9SMatthew Ahrens uint64_t size;
10303cb34c60Sahrens } send_dump_data_t;
10313cb34c60Sahrens
103219b94df9SMatthew Ahrens static int
estimate_ioctl(zfs_handle_t * zhp,uint64_t fromsnap_obj,boolean_t fromorigin,enum lzc_send_flags flags,uint64_t * sizep)103319b94df9SMatthew Ahrens estimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
10345602294fSDan Kimmel boolean_t fromorigin, enum lzc_send_flags flags, uint64_t *sizep)
103519b94df9SMatthew Ahrens {
103619b94df9SMatthew Ahrens zfs_cmd_t zc = { 0 };
103719b94df9SMatthew Ahrens libzfs_handle_t *hdl = zhp->zfs_hdl;
103819b94df9SMatthew Ahrens
103919b94df9SMatthew Ahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
104019b94df9SMatthew Ahrens assert(fromsnap_obj == 0 || !fromorigin);
104119b94df9SMatthew Ahrens
104219b94df9SMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
104319b94df9SMatthew Ahrens zc.zc_obj = fromorigin;
104419b94df9SMatthew Ahrens zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
104519b94df9SMatthew Ahrens zc.zc_fromobj = fromsnap_obj;
104619b94df9SMatthew Ahrens zc.zc_guid = 1; /* estimate flag */
10475602294fSDan Kimmel zc.zc_flags = flags;
104819b94df9SMatthew Ahrens
104919b94df9SMatthew Ahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
105019b94df9SMatthew Ahrens char errbuf[1024];
105119b94df9SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
105219b94df9SMatthew Ahrens "warning: cannot estimate space for '%s'"), zhp->zfs_name);
105319b94df9SMatthew Ahrens
105419b94df9SMatthew Ahrens switch (errno) {
105519b94df9SMatthew Ahrens case EXDEV:
105619b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
105719b94df9SMatthew Ahrens "not an earlier snapshot from the same fs"));
105819b94df9SMatthew Ahrens return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
105919b94df9SMatthew Ahrens
1060eb633035STom Caputi case EACCES:
1061eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1062eb633035STom Caputi "source key must be loaded"));
1063eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1064eb633035STom Caputi
106519b94df9SMatthew Ahrens case ENOENT:
106619b94df9SMatthew Ahrens if (zfs_dataset_exists(hdl, zc.zc_name,
106719b94df9SMatthew Ahrens ZFS_TYPE_SNAPSHOT)) {
106819b94df9SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
106919b94df9SMatthew Ahrens "incremental source (@%s) does not exist"),
107019b94df9SMatthew Ahrens zc.zc_value);
107119b94df9SMatthew Ahrens }
107219b94df9SMatthew Ahrens return (zfs_error(hdl, EZFS_NOENT, errbuf));
107319b94df9SMatthew Ahrens
107419b94df9SMatthew Ahrens case EDQUOT:
107519b94df9SMatthew Ahrens case EFBIG:
107619b94df9SMatthew Ahrens case EIO:
107719b94df9SMatthew Ahrens case ENOLINK:
107819b94df9SMatthew Ahrens case ENOSPC:
107919b94df9SMatthew Ahrens case ENOSTR:
108019b94df9SMatthew Ahrens case ENXIO:
108119b94df9SMatthew Ahrens case EPIPE:
108219b94df9SMatthew Ahrens case ERANGE:
108319b94df9SMatthew Ahrens case EFAULT:
108419b94df9SMatthew Ahrens case EROFS:
108519b94df9SMatthew Ahrens zfs_error_aux(hdl, strerror(errno));
108619b94df9SMatthew Ahrens return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
108719b94df9SMatthew Ahrens
108819b94df9SMatthew Ahrens default:
108919b94df9SMatthew Ahrens return (zfs_standard_error(hdl, errno, errbuf));
109019b94df9SMatthew Ahrens }
109119b94df9SMatthew Ahrens }
109219b94df9SMatthew Ahrens
109319b94df9SMatthew Ahrens *sizep = zc.zc_objset_type;
109419b94df9SMatthew Ahrens
109519b94df9SMatthew Ahrens return (0);
109619b94df9SMatthew Ahrens }
109719b94df9SMatthew Ahrens
10983cb34c60Sahrens /*
10993cb34c60Sahrens * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
11003cb34c60Sahrens * NULL) to the file descriptor specified by outfd.
11013cb34c60Sahrens */
11023cb34c60Sahrens static int
dump_ioctl(zfs_handle_t * zhp,const char * fromsnap,uint64_t fromsnap_obj,boolean_t fromorigin,int outfd,enum lzc_send_flags flags,nvlist_t * debugnv)1103a7f53a56SChris Kirby dump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
11045d7b4d43SMatthew Ahrens boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
11055d7b4d43SMatthew Ahrens nvlist_t *debugnv)
11063cb34c60Sahrens {
11073cb34c60Sahrens zfs_cmd_t zc = { 0 };
11083cb34c60Sahrens libzfs_handle_t *hdl = zhp->zfs_hdl;
11093f9d6ad7SLin Ling nvlist_t *thisdbg;
11103cb34c60Sahrens
11113cb34c60Sahrens assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1112a7f53a56SChris Kirby assert(fromsnap_obj == 0 || !fromorigin);
11133cb34c60Sahrens
11143cb34c60Sahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
11153cb34c60Sahrens zc.zc_cookie = outfd;
11163cb34c60Sahrens zc.zc_obj = fromorigin;
1117a7f53a56SChris Kirby zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
1118a7f53a56SChris Kirby zc.zc_fromobj = fromsnap_obj;
11195d7b4d43SMatthew Ahrens zc.zc_flags = flags;
1120c6fd73aeSChris Kirby
11213f9d6ad7SLin Ling VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
11223f9d6ad7SLin Ling if (fromsnap && fromsnap[0] != '\0') {
11233f9d6ad7SLin Ling VERIFY(0 == nvlist_add_string(thisdbg,
11243f9d6ad7SLin Ling "fromsnap", fromsnap));
11253f9d6ad7SLin Ling }
11263f9d6ad7SLin Ling
112719b94df9SMatthew Ahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
11283cb34c60Sahrens char errbuf[1024];
11293cb34c60Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
11303cb34c60Sahrens "warning: cannot send '%s'"), zhp->zfs_name);
11313cb34c60Sahrens
11323f9d6ad7SLin Ling VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
11333f9d6ad7SLin Ling if (debugnv) {
11343f9d6ad7SLin Ling VERIFY(0 == nvlist_add_nvlist(debugnv,
11353f9d6ad7SLin Ling zhp->zfs_name, thisdbg));
11363f9d6ad7SLin Ling }
11373f9d6ad7SLin Ling nvlist_free(thisdbg);
11383f9d6ad7SLin Ling
11393cb34c60Sahrens switch (errno) {
11403cb34c60Sahrens case EXDEV:
11413cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11423cb34c60Sahrens "not an earlier snapshot from the same fs"));
11433cb34c60Sahrens return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
11443cb34c60Sahrens
1145eb633035STom Caputi case EACCES:
1146eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1147eb633035STom Caputi "source key must be loaded"));
1148eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1149eb633035STom Caputi
11503cb34c60Sahrens case ENOENT:
11513cb34c60Sahrens if (zfs_dataset_exists(hdl, zc.zc_name,
11523cb34c60Sahrens ZFS_TYPE_SNAPSHOT)) {
11533cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
11543cb34c60Sahrens "incremental source (@%s) does not exist"),
11553cb34c60Sahrens zc.zc_value);
11563cb34c60Sahrens }
11573cb34c60Sahrens return (zfs_error(hdl, EZFS_NOENT, errbuf));
11583cb34c60Sahrens
11593cb34c60Sahrens case EDQUOT:
11603cb34c60Sahrens case EFBIG:
11613cb34c60Sahrens case EIO:
11623cb34c60Sahrens case ENOLINK:
11633cb34c60Sahrens case ENOSPC:
11643cb34c60Sahrens case ENOSTR:
11653cb34c60Sahrens case ENXIO:
11663cb34c60Sahrens case EPIPE:
11673cb34c60Sahrens case ERANGE:
11683cb34c60Sahrens case EFAULT:
11693cb34c60Sahrens case EROFS:
11703cb34c60Sahrens zfs_error_aux(hdl, strerror(errno));
11713cb34c60Sahrens return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
11723cb34c60Sahrens
11733cb34c60Sahrens default:
11743cb34c60Sahrens return (zfs_standard_error(hdl, errno, errbuf));
11753cb34c60Sahrens }
11763cb34c60Sahrens }
11773cb34c60Sahrens
11783f9d6ad7SLin Ling if (debugnv)
11793f9d6ad7SLin Ling VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
11803f9d6ad7SLin Ling nvlist_free(thisdbg);
11813f9d6ad7SLin Ling
11823cb34c60Sahrens return (0);
11833cb34c60Sahrens }
11843cb34c60Sahrens
1185a7a845e4SSteven Hartland static void
gather_holds(zfs_handle_t * zhp,send_dump_data_t * sdd)1186a7a845e4SSteven Hartland gather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
1187a7f53a56SChris Kirby {
1188a7f53a56SChris Kirby assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT);
1189a7f53a56SChris Kirby
1190a7f53a56SChris Kirby /*
1191a7a845e4SSteven Hartland * zfs_send() only sets snapholds for sends that need them,
1192a7f53a56SChris Kirby * e.g. replication and doall.
1193a7f53a56SChris Kirby */
1194a7a845e4SSteven Hartland if (sdd->snapholds == NULL)
1195a7a845e4SSteven Hartland return;
1196a7f53a56SChris Kirby
1197a7a845e4SSteven Hartland fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
1198a7f53a56SChris Kirby }
1199a7f53a56SChris Kirby
12004e3c9f44SBill Pijewski static void *
send_progress_thread(void * arg)12014e3c9f44SBill Pijewski send_progress_thread(void *arg)
12024e3c9f44SBill Pijewski {
12034e3c9f44SBill Pijewski progress_arg_t *pa = arg;
12044e3c9f44SBill Pijewski zfs_cmd_t zc = { 0 };
12054e3c9f44SBill Pijewski zfs_handle_t *zhp = pa->pa_zhp;
12064e3c9f44SBill Pijewski libzfs_handle_t *hdl = zhp->zfs_hdl;
12074e3c9f44SBill Pijewski unsigned long long bytes;
12084e3c9f44SBill Pijewski char buf[16];
12094e3c9f44SBill Pijewski time_t t;
12104e3c9f44SBill Pijewski struct tm *tm;
12114e3c9f44SBill Pijewski
12124e3c9f44SBill Pijewski (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
12134e3c9f44SBill Pijewski
12144e3c9f44SBill Pijewski if (!pa->pa_parsable)
12154e3c9f44SBill Pijewski (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
12164e3c9f44SBill Pijewski
12174e3c9f44SBill Pijewski /*
12184e3c9f44SBill Pijewski * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
12194e3c9f44SBill Pijewski */
12204e3c9f44SBill Pijewski for (;;) {
12214e3c9f44SBill Pijewski (void) sleep(1);
12224e3c9f44SBill Pijewski
12234e3c9f44SBill Pijewski zc.zc_cookie = pa->pa_fd;
12244e3c9f44SBill Pijewski if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
12254e3c9f44SBill Pijewski return ((void *)-1);
12264e3c9f44SBill Pijewski
12274e3c9f44SBill Pijewski (void) time(&t);
12284e3c9f44SBill Pijewski tm = localtime(&t);
12294e3c9f44SBill Pijewski bytes = zc.zc_cookie;
12304e3c9f44SBill Pijewski
12314e3c9f44SBill Pijewski if (pa->pa_parsable) {
12324e3c9f44SBill Pijewski (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
12334e3c9f44SBill Pijewski tm->tm_hour, tm->tm_min, tm->tm_sec,
12344e3c9f44SBill Pijewski bytes, zhp->zfs_name);
12354e3c9f44SBill Pijewski } else {
12366520eed5SToomas Soome zfs_nicebytes(bytes, buf, sizeof (buf));
12374e3c9f44SBill Pijewski (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
12384e3c9f44SBill Pijewski tm->tm_hour, tm->tm_min, tm->tm_sec,
12394e3c9f44SBill Pijewski buf, zhp->zfs_name);
12404e3c9f44SBill Pijewski }
12414e3c9f44SBill Pijewski }
12424e3c9f44SBill Pijewski }
12434e3c9f44SBill Pijewski
12449c3fd121SMatthew Ahrens static void
send_print_verbose(FILE * fout,const char * tosnap,const char * fromsnap,uint64_t size,boolean_t parsable)12459c3fd121SMatthew Ahrens send_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
12469c3fd121SMatthew Ahrens uint64_t size, boolean_t parsable)
12479c3fd121SMatthew Ahrens {
12489c3fd121SMatthew Ahrens if (parsable) {
12499c3fd121SMatthew Ahrens if (fromsnap != NULL) {
12509c3fd121SMatthew Ahrens (void) fprintf(fout, "incremental\t%s\t%s",
12519c3fd121SMatthew Ahrens fromsnap, tosnap);
12529c3fd121SMatthew Ahrens } else {
12539c3fd121SMatthew Ahrens (void) fprintf(fout, "full\t%s",
12549c3fd121SMatthew Ahrens tosnap);
12559c3fd121SMatthew Ahrens }
12569c3fd121SMatthew Ahrens } else {
12579c3fd121SMatthew Ahrens if (fromsnap != NULL) {
12589c3fd121SMatthew Ahrens if (strchr(fromsnap, '@') == NULL &&
12599c3fd121SMatthew Ahrens strchr(fromsnap, '#') == NULL) {
12609c3fd121SMatthew Ahrens (void) fprintf(fout, dgettext(TEXT_DOMAIN,
12619c3fd121SMatthew Ahrens "send from @%s to %s"),
12629c3fd121SMatthew Ahrens fromsnap, tosnap);
12639c3fd121SMatthew Ahrens } else {
12649c3fd121SMatthew Ahrens (void) fprintf(fout, dgettext(TEXT_DOMAIN,
12659c3fd121SMatthew Ahrens "send from %s to %s"),
12669c3fd121SMatthew Ahrens fromsnap, tosnap);
12679c3fd121SMatthew Ahrens }
12689c3fd121SMatthew Ahrens } else {
12699c3fd121SMatthew Ahrens (void) fprintf(fout, dgettext(TEXT_DOMAIN,
12709c3fd121SMatthew Ahrens "full send of %s"),
12719c3fd121SMatthew Ahrens tosnap);
12729c3fd121SMatthew Ahrens }
12739c3fd121SMatthew Ahrens }
12749c3fd121SMatthew Ahrens
12759c3fd121SMatthew Ahrens if (size != 0) {
12769c3fd121SMatthew Ahrens if (parsable) {
12779c3fd121SMatthew Ahrens (void) fprintf(fout, "\t%llu",
12789c3fd121SMatthew Ahrens (longlong_t)size);
12799c3fd121SMatthew Ahrens } else {
12809c3fd121SMatthew Ahrens char buf[16];
12816520eed5SToomas Soome zfs_nicebytes(size, buf, sizeof (buf));
12829c3fd121SMatthew Ahrens (void) fprintf(fout, dgettext(TEXT_DOMAIN,
12839c3fd121SMatthew Ahrens " estimated size is %s"), buf);
12849c3fd121SMatthew Ahrens }
12859c3fd121SMatthew Ahrens }
12869c3fd121SMatthew Ahrens (void) fprintf(fout, "\n");
12879c3fd121SMatthew Ahrens }
12889c3fd121SMatthew Ahrens
12893cb34c60Sahrens static int
dump_snapshot(zfs_handle_t * zhp,void * arg)12903cb34c60Sahrens dump_snapshot(zfs_handle_t *zhp, void *arg)
12913cb34c60Sahrens {
12923cb34c60Sahrens send_dump_data_t *sdd = arg;
12934e3c9f44SBill Pijewski progress_arg_t pa = { 0 };
12944e3c9f44SBill Pijewski pthread_t tid;
1295a7f53a56SChris Kirby char *thissnap;
12965602294fSDan Kimmel enum lzc_send_flags flags = 0;
12973cb34c60Sahrens int err;
129819b94df9SMatthew Ahrens boolean_t isfromsnap, istosnap, fromorigin;
1299468db2f4STom Erickson boolean_t exclude = B_FALSE;
1300dc5f28a3SManoj Joseph FILE *fout = sdd->std_out ? stdout : stderr;
13013cb34c60Sahrens
1302a7a845e4SSteven Hartland err = 0;
13033cb34c60Sahrens thissnap = strchr(zhp->zfs_name, '@') + 1;
1304468db2f4STom Erickson isfromsnap = (sdd->fromsnap != NULL &&
1305468db2f4STom Erickson strcmp(sdd->fromsnap, thissnap) == 0);
13063cb34c60Sahrens
1307468db2f4STom Erickson if (!sdd->seenfrom && isfromsnap) {
1308a7a845e4SSteven Hartland gather_holds(zhp, sdd);
1309a7a845e4SSteven Hartland sdd->seenfrom = B_TRUE;
1310a7a845e4SSteven Hartland (void) strcpy(sdd->prevsnap, thissnap);
1311a7a845e4SSteven Hartland sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
13123cb34c60Sahrens zfs_close(zhp);
1313a7a845e4SSteven Hartland return (0);
13143cb34c60Sahrens }
13153cb34c60Sahrens
13163cb34c60Sahrens if (sdd->seento || !sdd->seenfrom) {
13173cb34c60Sahrens zfs_close(zhp);
13183cb34c60Sahrens return (0);
13193cb34c60Sahrens }
13203cb34c60Sahrens
1321468db2f4STom Erickson istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
1322468db2f4STom Erickson if (istosnap)
13239e69d7d0SLori Alt sdd->seento = B_TRUE;
13249e69d7d0SLori Alt
13255602294fSDan Kimmel if (sdd->large_block)
13265602294fSDan Kimmel flags |= LZC_SEND_FLAG_LARGE_BLOCK;
13275602294fSDan Kimmel if (sdd->embed_data)
13285602294fSDan Kimmel flags |= LZC_SEND_FLAG_EMBED_DATA;
13295602294fSDan Kimmel if (sdd->compress)
13305602294fSDan Kimmel flags |= LZC_SEND_FLAG_COMPRESS;
1331eb633035STom Caputi if (sdd->raw)
1332eb633035STom Caputi flags |= LZC_SEND_FLAG_RAW;
13335602294fSDan Kimmel
1334468db2f4STom Erickson if (!sdd->doall && !isfromsnap && !istosnap) {
1335468db2f4STom Erickson if (sdd->replicate) {
1336468db2f4STom Erickson char *snapname;
1337468db2f4STom Erickson nvlist_t *snapprops;
1338468db2f4STom Erickson /*
1339468db2f4STom Erickson * Filter out all intermediate snapshots except origin
1340468db2f4STom Erickson * snapshots needed to replicate clones.
1341468db2f4STom Erickson */
1342468db2f4STom Erickson nvlist_t *nvfs = fsavl_find(sdd->fsavl,
1343468db2f4STom Erickson zhp->zfs_dmustats.dds_guid, &snapname);
1344468db2f4STom Erickson
1345468db2f4STom Erickson VERIFY(0 == nvlist_lookup_nvlist(nvfs,
1346468db2f4STom Erickson "snapprops", &snapprops));
1347468db2f4STom Erickson VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1348468db2f4STom Erickson thissnap, &snapprops));
1349468db2f4STom Erickson exclude = !nvlist_exists(snapprops, "is_clone_origin");
1350468db2f4STom Erickson } else {
1351468db2f4STom Erickson exclude = B_TRUE;
1352468db2f4STom Erickson }
1353468db2f4STom Erickson }
1354468db2f4STom Erickson
13559e69d7d0SLori Alt /*
13569e69d7d0SLori Alt * If a filter function exists, call it to determine whether
13579e69d7d0SLori Alt * this snapshot will be sent.
13589e69d7d0SLori Alt */
1359468db2f4STom Erickson if (exclude || (sdd->filter_cb != NULL &&
1360468db2f4STom Erickson sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
13619e69d7d0SLori Alt /*
13629e69d7d0SLori Alt * This snapshot is filtered out. Don't send it, and don't
1363a7f53a56SChris Kirby * set prevsnap_obj, so it will be as if this snapshot didn't
13649e69d7d0SLori Alt * exist, and the next accepted snapshot will be sent as
13659e69d7d0SLori Alt * an incremental from the last accepted one, or as the
13669e69d7d0SLori Alt * first (and full) snapshot in the case of a replication,
13679e69d7d0SLori Alt * non-incremental send.
13689e69d7d0SLori Alt */
13699e69d7d0SLori Alt zfs_close(zhp);
13709e69d7d0SLori Alt return (0);
13719e69d7d0SLori Alt }
13729e69d7d0SLori Alt
1373a7a845e4SSteven Hartland gather_holds(zhp, sdd);
137419b94df9SMatthew Ahrens fromorigin = sdd->prevsnap[0] == '\0' &&
137519b94df9SMatthew Ahrens (sdd->fromorigin || sdd->replicate);
137619b94df9SMatthew Ahrens
13773cb34c60Sahrens if (sdd->verbose) {
13789c3fd121SMatthew Ahrens uint64_t size = 0;
13799c3fd121SMatthew Ahrens (void) estimate_ioctl(zhp, sdd->prevsnap_obj,
13805602294fSDan Kimmel fromorigin, flags, &size);
138119b94df9SMatthew Ahrens
13829c3fd121SMatthew Ahrens send_print_verbose(fout, zhp->zfs_name,
13839c3fd121SMatthew Ahrens sdd->prevsnap[0] ? sdd->prevsnap : NULL,
13849c3fd121SMatthew Ahrens size, sdd->parsable);
13859c3fd121SMatthew Ahrens sdd->size += size;
13863cb34c60Sahrens }
13873cb34c60Sahrens
138819b94df9SMatthew Ahrens if (!sdd->dryrun) {
13894e3c9f44SBill Pijewski /*
13904e3c9f44SBill Pijewski * If progress reporting is requested, spawn a new thread to
13914e3c9f44SBill Pijewski * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
13924e3c9f44SBill Pijewski */
13934e3c9f44SBill Pijewski if (sdd->progress) {
13944e3c9f44SBill Pijewski pa.pa_zhp = zhp;
13954e3c9f44SBill Pijewski pa.pa_fd = sdd->outfd;
13964e3c9f44SBill Pijewski pa.pa_parsable = sdd->parsable;
13974e3c9f44SBill Pijewski
139888f61deeSIgor Kozhukhov if ((err = pthread_create(&tid, NULL,
139988f61deeSIgor Kozhukhov send_progress_thread, &pa)) != 0) {
14004e3c9f44SBill Pijewski zfs_close(zhp);
14014e3c9f44SBill Pijewski return (err);
14024e3c9f44SBill Pijewski }
14034e3c9f44SBill Pijewski }
14044e3c9f44SBill Pijewski
140519b94df9SMatthew Ahrens err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
14065d7b4d43SMatthew Ahrens fromorigin, sdd->outfd, flags, sdd->debugnv);
14074e3c9f44SBill Pijewski
14084e3c9f44SBill Pijewski if (sdd->progress) {
14094e3c9f44SBill Pijewski (void) pthread_cancel(tid);
14104e3c9f44SBill Pijewski (void) pthread_join(tid, NULL);
14114e3c9f44SBill Pijewski }
141219b94df9SMatthew Ahrens }
14133cb34c60Sahrens
1414a7f53a56SChris Kirby (void) strcpy(sdd->prevsnap, thissnap);
1415a7f53a56SChris Kirby sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
14163cb34c60Sahrens zfs_close(zhp);
14173cb34c60Sahrens return (err);
14183cb34c60Sahrens }
14193cb34c60Sahrens
14203cb34c60Sahrens static int
dump_filesystem(zfs_handle_t * zhp,void * arg)14213cb34c60Sahrens dump_filesystem(zfs_handle_t *zhp, void *arg)
14223cb34c60Sahrens {
14233cb34c60Sahrens int rv = 0;
14243cb34c60Sahrens send_dump_data_t *sdd = arg;
14253cb34c60Sahrens boolean_t missingfrom = B_FALSE;
14263cb34c60Sahrens zfs_cmd_t zc = { 0 };
14273cb34c60Sahrens
14283cb34c60Sahrens (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
14293cb34c60Sahrens zhp->zfs_name, sdd->tosnap);
14303cb34c60Sahrens if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
143119b94df9SMatthew Ahrens (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
143219b94df9SMatthew Ahrens "WARNING: could not send %s@%s: does not exist\n"),
14333cb34c60Sahrens zhp->zfs_name, sdd->tosnap);
14343cb34c60Sahrens sdd->err = B_TRUE;
14353cb34c60Sahrens return (0);
14363cb34c60Sahrens }
14373cb34c60Sahrens
14383cb34c60Sahrens if (sdd->replicate && sdd->fromsnap) {
14393cb34c60Sahrens /*
14403cb34c60Sahrens * If this fs does not have fromsnap, and we're doing
14413cb34c60Sahrens * recursive, we need to send a full stream from the
14423cb34c60Sahrens * beginning (or an incremental from the origin if this
14433cb34c60Sahrens * is a clone). If we're doing non-recursive, then let
14443cb34c60Sahrens * them get the error.
14453cb34c60Sahrens */
14463cb34c60Sahrens (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
14473cb34c60Sahrens zhp->zfs_name, sdd->fromsnap);
14483cb34c60Sahrens if (ioctl(zhp->zfs_hdl->libzfs_fd,
14493cb34c60Sahrens ZFS_IOC_OBJSET_STATS, &zc) != 0) {
14503cb34c60Sahrens missingfrom = B_TRUE;
14513cb34c60Sahrens }
14523cb34c60Sahrens }
14533cb34c60Sahrens
1454468db2f4STom Erickson sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
1455a7f53a56SChris Kirby sdd->prevsnap_obj = 0;
1456468db2f4STom Erickson if (sdd->fromsnap == NULL || missingfrom)
1457468db2f4STom Erickson sdd->seenfrom = B_TRUE;
14583cb34c60Sahrens
1459468db2f4STom Erickson rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
1460468db2f4STom Erickson if (!sdd->seenfrom) {
146119b94df9SMatthew Ahrens (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
1462468db2f4STom Erickson "WARNING: could not send %s@%s:\n"
146319b94df9SMatthew Ahrens "incremental source (%s@%s) does not exist\n"),
1464468db2f4STom Erickson zhp->zfs_name, sdd->tosnap,
1465468db2f4STom Erickson zhp->zfs_name, sdd->fromsnap);
1466468db2f4STom Erickson sdd->err = B_TRUE;
1467468db2f4STom Erickson } else if (!sdd->seento) {
1468468db2f4STom Erickson if (sdd->fromsnap) {
146919b94df9SMatthew Ahrens (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
14703cb34c60Sahrens "WARNING: could not send %s@%s:\n"
1471468db2f4STom Erickson "incremental source (%s@%s) "
147219b94df9SMatthew Ahrens "is not earlier than it\n"),
14733cb34c60Sahrens zhp->zfs_name, sdd->tosnap,
14743cb34c60Sahrens zhp->zfs_name, sdd->fromsnap);
147504bb726eSahl } else {
147619b94df9SMatthew Ahrens (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
147719b94df9SMatthew Ahrens "WARNING: "
147819b94df9SMatthew Ahrens "could not send %s@%s: does not exist\n"),
1479468db2f4STom Erickson zhp->zfs_name, sdd->tosnap);
148004bb726eSahl }
1481468db2f4STom Erickson sdd->err = B_TRUE;
14823cb34c60Sahrens }
14833cb34c60Sahrens
14843cb34c60Sahrens return (rv);
14853cb34c60Sahrens }
14863cb34c60Sahrens
14873cb34c60Sahrens static int
dump_filesystems(zfs_handle_t * rzhp,void * arg)14883cb34c60Sahrens dump_filesystems(zfs_handle_t *rzhp, void *arg)
14893cb34c60Sahrens {
14903cb34c60Sahrens send_dump_data_t *sdd = arg;
14913cb34c60Sahrens nvpair_t *fspair;
14923cb34c60Sahrens boolean_t needagain, progress;
14933cb34c60Sahrens
14943cb34c60Sahrens if (!sdd->replicate)
14953cb34c60Sahrens return (dump_filesystem(rzhp, sdd));
14963cb34c60Sahrens
1497468db2f4STom Erickson /* Mark the clone origin snapshots. */
1498468db2f4STom Erickson for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
1499468db2f4STom Erickson fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
1500468db2f4STom Erickson nvlist_t *nvfs;
1501468db2f4STom Erickson uint64_t origin_guid = 0;
1502468db2f4STom Erickson
1503468db2f4STom Erickson VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
1504468db2f4STom Erickson (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
1505468db2f4STom Erickson if (origin_guid != 0) {
1506468db2f4STom Erickson char *snapname;
1507468db2f4STom Erickson nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1508468db2f4STom Erickson origin_guid, &snapname);
1509468db2f4STom Erickson if (origin_nv != NULL) {
1510468db2f4STom Erickson nvlist_t *snapprops;
1511468db2f4STom Erickson VERIFY(0 == nvlist_lookup_nvlist(origin_nv,
1512468db2f4STom Erickson "snapprops", &snapprops));
1513468db2f4STom Erickson VERIFY(0 == nvlist_lookup_nvlist(snapprops,
1514468db2f4STom Erickson snapname, &snapprops));
1515468db2f4STom Erickson VERIFY(0 == nvlist_add_boolean(
1516468db2f4STom Erickson snapprops, "is_clone_origin"));
1517468db2f4STom Erickson }
1518468db2f4STom Erickson }
1519468db2f4STom Erickson }
15203cb34c60Sahrens again:
15213cb34c60Sahrens needagain = progress = B_FALSE;
15223cb34c60Sahrens for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
15233cb34c60Sahrens fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
152419b94df9SMatthew Ahrens nvlist_t *fslist, *parent_nv;
15253cb34c60Sahrens char *fsname;
15263cb34c60Sahrens zfs_handle_t *zhp;
15273cb34c60Sahrens int err;
15283cb34c60Sahrens uint64_t origin_guid = 0;
152919b94df9SMatthew Ahrens uint64_t parent_guid = 0;
15303cb34c60Sahrens
15313cb34c60Sahrens VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
15323cb34c60Sahrens if (nvlist_lookup_boolean(fslist, "sent") == 0)
15333cb34c60Sahrens continue;
15343cb34c60Sahrens
15353cb34c60Sahrens VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
15363cb34c60Sahrens (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
153719b94df9SMatthew Ahrens (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
153819b94df9SMatthew Ahrens &parent_guid);
153919b94df9SMatthew Ahrens
154019b94df9SMatthew Ahrens if (parent_guid != 0) {
154119b94df9SMatthew Ahrens parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
154219b94df9SMatthew Ahrens if (!nvlist_exists(parent_nv, "sent")) {
154319b94df9SMatthew Ahrens /* parent has not been sent; skip this one */
154419b94df9SMatthew Ahrens needagain = B_TRUE;
154519b94df9SMatthew Ahrens continue;
154619b94df9SMatthew Ahrens }
154719b94df9SMatthew Ahrens }
15483cb34c60Sahrens
1549468db2f4STom Erickson if (origin_guid != 0) {
1550468db2f4STom Erickson nvlist_t *origin_nv = fsavl_find(sdd->fsavl,
1551468db2f4STom Erickson origin_guid, NULL);
1552468db2f4STom Erickson if (origin_nv != NULL &&
155319b94df9SMatthew Ahrens !nvlist_exists(origin_nv, "sent")) {
1554468db2f4STom Erickson /*
1555468db2f4STom Erickson * origin has not been sent yet;
1556468db2f4STom Erickson * skip this clone.
1557468db2f4STom Erickson */
1558468db2f4STom Erickson needagain = B_TRUE;
1559468db2f4STom Erickson continue;
1560468db2f4STom Erickson }
15613cb34c60Sahrens }
15623cb34c60Sahrens
15633cb34c60Sahrens zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
156404bb726eSahl if (zhp == NULL)
156504bb726eSahl return (-1);
15663cb34c60Sahrens err = dump_filesystem(zhp, sdd);
15673cb34c60Sahrens VERIFY(nvlist_add_boolean(fslist, "sent") == 0);
15683cb34c60Sahrens progress = B_TRUE;
15693cb34c60Sahrens zfs_close(zhp);
15703cb34c60Sahrens if (err)
15713cb34c60Sahrens return (err);
15723cb34c60Sahrens }
15733cb34c60Sahrens if (needagain) {
15743cb34c60Sahrens assert(progress);
15753cb34c60Sahrens goto again;
15763cb34c60Sahrens }
157719b94df9SMatthew Ahrens
157819b94df9SMatthew Ahrens /* clean out the sent flags in case we reuse this fss */
157919b94df9SMatthew Ahrens for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
158019b94df9SMatthew Ahrens fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
158119b94df9SMatthew Ahrens nvlist_t *fslist;
158219b94df9SMatthew Ahrens
158319b94df9SMatthew Ahrens VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
158419b94df9SMatthew Ahrens (void) nvlist_remove_all(fslist, "sent");
158519b94df9SMatthew Ahrens }
158619b94df9SMatthew Ahrens
15873cb34c60Sahrens return (0);
15883cb34c60Sahrens }
15893cb34c60Sahrens
15909c3fd121SMatthew Ahrens nvlist_t *
zfs_send_resume_token_to_nvlist(libzfs_handle_t * hdl,const char * token)15919c3fd121SMatthew Ahrens zfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
15929c3fd121SMatthew Ahrens {
15939c3fd121SMatthew Ahrens unsigned int version;
15949c3fd121SMatthew Ahrens int nread;
15959c3fd121SMatthew Ahrens unsigned long long checksum, packed_len;
15969c3fd121SMatthew Ahrens
15979c3fd121SMatthew Ahrens /*
15989c3fd121SMatthew Ahrens * Decode token header, which is:
15999c3fd121SMatthew Ahrens * <token version>-<checksum of payload>-<uncompressed payload length>
16009c3fd121SMatthew Ahrens * Note that the only supported token version is 1.
16019c3fd121SMatthew Ahrens */
16029c3fd121SMatthew Ahrens nread = sscanf(token, "%u-%llx-%llx-",
16039c3fd121SMatthew Ahrens &version, &checksum, &packed_len);
16049c3fd121SMatthew Ahrens if (nread != 3) {
16059c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16069c3fd121SMatthew Ahrens "resume token is corrupt (invalid format)"));
16079c3fd121SMatthew Ahrens return (NULL);
16089c3fd121SMatthew Ahrens }
16099c3fd121SMatthew Ahrens
16109c3fd121SMatthew Ahrens if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
16119c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16129c3fd121SMatthew Ahrens "resume token is corrupt (invalid version %u)"),
16139c3fd121SMatthew Ahrens version);
16149c3fd121SMatthew Ahrens return (NULL);
16159c3fd121SMatthew Ahrens }
16169c3fd121SMatthew Ahrens
16179c3fd121SMatthew Ahrens /* convert hexadecimal representation to binary */
16189c3fd121SMatthew Ahrens token = strrchr(token, '-') + 1;
16199c3fd121SMatthew Ahrens int len = strlen(token) / 2;
16209c3fd121SMatthew Ahrens unsigned char *compressed = zfs_alloc(hdl, len);
16219c3fd121SMatthew Ahrens for (int i = 0; i < len; i++) {
16229c3fd121SMatthew Ahrens nread = sscanf(token + i * 2, "%2hhx", compressed + i);
16239c3fd121SMatthew Ahrens if (nread != 1) {
16249c3fd121SMatthew Ahrens free(compressed);
16259c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16269c3fd121SMatthew Ahrens "resume token is corrupt "
16279c3fd121SMatthew Ahrens "(payload is not hex-encoded)"));
16289c3fd121SMatthew Ahrens return (NULL);
16299c3fd121SMatthew Ahrens }
16309c3fd121SMatthew Ahrens }
16319c3fd121SMatthew Ahrens
16329c3fd121SMatthew Ahrens /* verify checksum */
16339c3fd121SMatthew Ahrens zio_cksum_t cksum;
1634*0886dcadSAndy Fiddaman fletcher_4_native_varsize(compressed, len, &cksum);
16359c3fd121SMatthew Ahrens if (cksum.zc_word[0] != checksum) {
16369c3fd121SMatthew Ahrens free(compressed);
16379c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16389c3fd121SMatthew Ahrens "resume token is corrupt (incorrect checksum)"));
16399c3fd121SMatthew Ahrens return (NULL);
16409c3fd121SMatthew Ahrens }
16419c3fd121SMatthew Ahrens
16429c3fd121SMatthew Ahrens /* uncompress */
16439c3fd121SMatthew Ahrens void *packed = zfs_alloc(hdl, packed_len);
16449c3fd121SMatthew Ahrens uLongf packed_len_long = packed_len;
16459c3fd121SMatthew Ahrens if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
16469c3fd121SMatthew Ahrens packed_len_long != packed_len) {
16479c3fd121SMatthew Ahrens free(packed);
16489c3fd121SMatthew Ahrens free(compressed);
16499c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16509c3fd121SMatthew Ahrens "resume token is corrupt (decompression failed)"));
16519c3fd121SMatthew Ahrens return (NULL);
16529c3fd121SMatthew Ahrens }
16539c3fd121SMatthew Ahrens
16549c3fd121SMatthew Ahrens /* unpack nvlist */
16559c3fd121SMatthew Ahrens nvlist_t *nv;
16569c3fd121SMatthew Ahrens int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
16579c3fd121SMatthew Ahrens free(packed);
16589c3fd121SMatthew Ahrens free(compressed);
16599c3fd121SMatthew Ahrens if (error != 0) {
16609c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
16619c3fd121SMatthew Ahrens "resume token is corrupt (nvlist_unpack failed)"));
16629c3fd121SMatthew Ahrens return (NULL);
16639c3fd121SMatthew Ahrens }
16649c3fd121SMatthew Ahrens return (nv);
16659c3fd121SMatthew Ahrens }
16669c3fd121SMatthew Ahrens
16679c3fd121SMatthew Ahrens int
zfs_send_resume(libzfs_handle_t * hdl,sendflags_t * flags,int outfd,const char * resume_token)16689c3fd121SMatthew Ahrens zfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
16699c3fd121SMatthew Ahrens const char *resume_token)
16709c3fd121SMatthew Ahrens {
16719c3fd121SMatthew Ahrens char errbuf[1024];
16729c3fd121SMatthew Ahrens char *toname;
16739c3fd121SMatthew Ahrens char *fromname = NULL;
16749c3fd121SMatthew Ahrens uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
16759c3fd121SMatthew Ahrens zfs_handle_t *zhp;
16769c3fd121SMatthew Ahrens int error = 0;
16779adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
16789c3fd121SMatthew Ahrens enum lzc_send_flags lzc_flags = 0;
1679544132fcSloli FILE *fout = (flags->verbose && flags->dryrun) ? stdout : stderr;
16809c3fd121SMatthew Ahrens
16819c3fd121SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
16829c3fd121SMatthew Ahrens "cannot resume send"));
16839c3fd121SMatthew Ahrens
16849c3fd121SMatthew Ahrens nvlist_t *resume_nvl =
16859c3fd121SMatthew Ahrens zfs_send_resume_token_to_nvlist(hdl, resume_token);
16869c3fd121SMatthew Ahrens if (resume_nvl == NULL) {
16879c3fd121SMatthew Ahrens /*
16889c3fd121SMatthew Ahrens * zfs_error_aux has already been set by
16899c3fd121SMatthew Ahrens * zfs_send_resume_token_to_nvlist
16909c3fd121SMatthew Ahrens */
16919c3fd121SMatthew Ahrens return (zfs_error(hdl, EZFS_FAULT, errbuf));
16929c3fd121SMatthew Ahrens }
16939c3fd121SMatthew Ahrens if (flags->verbose) {
1694544132fcSloli (void) fprintf(fout, dgettext(TEXT_DOMAIN,
16959c3fd121SMatthew Ahrens "resume token contents:\n"));
1696544132fcSloli nvlist_print(fout, resume_nvl);
16979c3fd121SMatthew Ahrens }
16989c3fd121SMatthew Ahrens
16999c3fd121SMatthew Ahrens if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
17009c3fd121SMatthew Ahrens nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
17019c3fd121SMatthew Ahrens nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
17029c3fd121SMatthew Ahrens nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
17039c3fd121SMatthew Ahrens nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
17049c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17059c3fd121SMatthew Ahrens "resume token is corrupt"));
17069c3fd121SMatthew Ahrens return (zfs_error(hdl, EZFS_FAULT, errbuf));
17079c3fd121SMatthew Ahrens }
17089c3fd121SMatthew Ahrens fromguid = 0;
17099c3fd121SMatthew Ahrens (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
17109c3fd121SMatthew Ahrens
17115602294fSDan Kimmel if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
17125602294fSDan Kimmel lzc_flags |= LZC_SEND_FLAG_LARGE_BLOCK;
17139c3fd121SMatthew Ahrens if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
17149c3fd121SMatthew Ahrens lzc_flags |= LZC_SEND_FLAG_EMBED_DATA;
17155602294fSDan Kimmel if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
17165602294fSDan Kimmel lzc_flags |= LZC_SEND_FLAG_COMPRESS;
1717eb633035STom Caputi if (flags->raw || nvlist_exists(resume_nvl, "rawok"))
1718eb633035STom Caputi lzc_flags |= LZC_SEND_FLAG_RAW;
17199c3fd121SMatthew Ahrens
17209c3fd121SMatthew Ahrens if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
17219c3fd121SMatthew Ahrens if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
17229c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17239c3fd121SMatthew Ahrens "'%s' is no longer the same snapshot used in "
17249c3fd121SMatthew Ahrens "the initial send"), toname);
17259c3fd121SMatthew Ahrens } else {
17269c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17279c3fd121SMatthew Ahrens "'%s' used in the initial send no longer exists"),
17289c3fd121SMatthew Ahrens toname);
17299c3fd121SMatthew Ahrens }
17309c3fd121SMatthew Ahrens return (zfs_error(hdl, EZFS_BADPATH, errbuf));
17319c3fd121SMatthew Ahrens }
17329c3fd121SMatthew Ahrens zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
17339c3fd121SMatthew Ahrens if (zhp == NULL) {
17349c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17359c3fd121SMatthew Ahrens "unable to access '%s'"), name);
17369c3fd121SMatthew Ahrens return (zfs_error(hdl, EZFS_BADPATH, errbuf));
17379c3fd121SMatthew Ahrens }
17389c3fd121SMatthew Ahrens
17399c3fd121SMatthew Ahrens if (fromguid != 0) {
17409c3fd121SMatthew Ahrens if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
17419c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
17429c3fd121SMatthew Ahrens "incremental source %#llx no longer exists"),
17439c3fd121SMatthew Ahrens (longlong_t)fromguid);
17449c3fd121SMatthew Ahrens return (zfs_error(hdl, EZFS_BADPATH, errbuf));
17459c3fd121SMatthew Ahrens }
17469c3fd121SMatthew Ahrens fromname = name;
17479c3fd121SMatthew Ahrens }
17489c3fd121SMatthew Ahrens
17499c3fd121SMatthew Ahrens if (flags->verbose) {
17509c3fd121SMatthew Ahrens uint64_t size = 0;
17515602294fSDan Kimmel error = lzc_send_space(zhp->zfs_name, fromname,
17525602294fSDan Kimmel lzc_flags, &size);
17539c3fd121SMatthew Ahrens if (error == 0)
17549c3fd121SMatthew Ahrens size = MAX(0, (int64_t)(size - bytes));
1755544132fcSloli send_print_verbose(fout, zhp->zfs_name, fromname,
17569c3fd121SMatthew Ahrens size, flags->parsable);
17579c3fd121SMatthew Ahrens }
17589c3fd121SMatthew Ahrens
17599c3fd121SMatthew Ahrens if (!flags->dryrun) {
17609c3fd121SMatthew Ahrens progress_arg_t pa = { 0 };
17619c3fd121SMatthew Ahrens pthread_t tid;
17629c3fd121SMatthew Ahrens /*
17639c3fd121SMatthew Ahrens * If progress reporting is requested, spawn a new thread to
17649c3fd121SMatthew Ahrens * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
17659c3fd121SMatthew Ahrens */
17669c3fd121SMatthew Ahrens if (flags->progress) {
17679c3fd121SMatthew Ahrens pa.pa_zhp = zhp;
17689c3fd121SMatthew Ahrens pa.pa_fd = outfd;
17699c3fd121SMatthew Ahrens pa.pa_parsable = flags->parsable;
17709c3fd121SMatthew Ahrens
17719c3fd121SMatthew Ahrens error = pthread_create(&tid, NULL,
17729c3fd121SMatthew Ahrens send_progress_thread, &pa);
17739c3fd121SMatthew Ahrens if (error != 0) {
17749c3fd121SMatthew Ahrens zfs_close(zhp);
17759c3fd121SMatthew Ahrens return (error);
17769c3fd121SMatthew Ahrens }
17779c3fd121SMatthew Ahrens }
17789c3fd121SMatthew Ahrens
17799c3fd121SMatthew Ahrens error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
17809c3fd121SMatthew Ahrens lzc_flags, resumeobj, resumeoff);
17819c3fd121SMatthew Ahrens
17829c3fd121SMatthew Ahrens if (flags->progress) {
17839c3fd121SMatthew Ahrens (void) pthread_cancel(tid);
17849c3fd121SMatthew Ahrens (void) pthread_join(tid, NULL);
17859c3fd121SMatthew Ahrens }
17869c3fd121SMatthew Ahrens
17879c3fd121SMatthew Ahrens char errbuf[1024];
17889c3fd121SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
17899c3fd121SMatthew Ahrens "warning: cannot send '%s'"), zhp->zfs_name);
17909c3fd121SMatthew Ahrens
17919c3fd121SMatthew Ahrens zfs_close(zhp);
17929c3fd121SMatthew Ahrens
17939c3fd121SMatthew Ahrens switch (error) {
17949c3fd121SMatthew Ahrens case 0:
17959c3fd121SMatthew Ahrens return (0);
1796eb633035STom Caputi case EACCES:
1797eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
1798eb633035STom Caputi "source key must be loaded"));
1799eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
1800eb633035STom Caputi
18019c3fd121SMatthew Ahrens case EXDEV:
18029c3fd121SMatthew Ahrens case ENOENT:
18039c3fd121SMatthew Ahrens case EDQUOT:
18049c3fd121SMatthew Ahrens case EFBIG:
18059c3fd121SMatthew Ahrens case EIO:
18069c3fd121SMatthew Ahrens case ENOLINK:
18079c3fd121SMatthew Ahrens case ENOSPC:
18089c3fd121SMatthew Ahrens case ENOSTR:
18099c3fd121SMatthew Ahrens case ENXIO:
18109c3fd121SMatthew Ahrens case EPIPE:
18119c3fd121SMatthew Ahrens case ERANGE:
18129c3fd121SMatthew Ahrens case EFAULT:
18139c3fd121SMatthew Ahrens case EROFS:
18149c3fd121SMatthew Ahrens zfs_error_aux(hdl, strerror(errno));
18159c3fd121SMatthew Ahrens return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
18169c3fd121SMatthew Ahrens
18179c3fd121SMatthew Ahrens default:
18189c3fd121SMatthew Ahrens return (zfs_standard_error(hdl, errno, errbuf));
18199c3fd121SMatthew Ahrens }
18209c3fd121SMatthew Ahrens }
18219c3fd121SMatthew Ahrens
18229c3fd121SMatthew Ahrens
18239c3fd121SMatthew Ahrens zfs_close(zhp);
18249c3fd121SMatthew Ahrens
18259c3fd121SMatthew Ahrens return (error);
18269c3fd121SMatthew Ahrens }
18279c3fd121SMatthew Ahrens
18283cb34c60Sahrens /*
182988bb18d2SLori Alt * Generate a send stream for the dataset identified by the argument zhp.
183088bb18d2SLori Alt *
183188bb18d2SLori Alt * The content of the send stream is the snapshot identified by
183288bb18d2SLori Alt * 'tosnap'. Incremental streams are requested in two ways:
183388bb18d2SLori Alt * - from the snapshot identified by "fromsnap" (if non-null) or
183488bb18d2SLori Alt * - from the origin of the dataset identified by zhp, which must
183588bb18d2SLori Alt * be a clone. In this case, "fromsnap" is null and "fromorigin"
183688bb18d2SLori Alt * is TRUE.
183788bb18d2SLori Alt *
183888bb18d2SLori Alt * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
18399e69d7d0SLori Alt * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
184088bb18d2SLori Alt * if "replicate" is set. If "doall" is set, dump all the intermediate
18419e69d7d0SLori Alt * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
184292241e0bSTom Erickson * case too. If "props" is set, send properties.
18433cb34c60Sahrens */
18443cb34c60Sahrens int
zfs_send(zfs_handle_t * zhp,const char * fromsnap,const char * tosnap,sendflags_t * flags,int outfd,snapfilter_cb_t filter_func,void * cb_arg,nvlist_t ** debugnvp)18453cb34c60Sahrens zfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
184619b94df9SMatthew Ahrens sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
18473f9d6ad7SLin Ling void *cb_arg, nvlist_t **debugnvp)
18483cb34c60Sahrens {
18493cb34c60Sahrens char errbuf[1024];
18503cb34c60Sahrens send_dump_data_t sdd = { 0 };
185119b94df9SMatthew Ahrens int err = 0;
18523cb34c60Sahrens nvlist_t *fss = NULL;
18533cb34c60Sahrens avl_tree_t *fsavl = NULL;
1854ca45db41SChris Kirby static uint64_t holdseq;
185554809200SChris Kirby int spa_version;
1856a7a845e4SSteven Hartland pthread_t tid = 0;
18579e69d7d0SLori Alt int pipefd[2];
18589e69d7d0SLori Alt dedup_arg_t dda = { 0 };
18599e69d7d0SLori Alt int featureflags = 0;
1860dc5f28a3SManoj Joseph FILE *fout;
18610a586ceaSMark Shellenbaum
18623cb34c60Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
18633cb34c60Sahrens "cannot send '%s'"), zhp->zfs_name);
18643cb34c60Sahrens
18653cb34c60Sahrens if (fromsnap && fromsnap[0] == '\0') {
18663cb34c60Sahrens zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN,
18673cb34c60Sahrens "zero-length incremental source"));
18683cb34c60Sahrens return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
18693cb34c60Sahrens }
18703cb34c60Sahrens
1871a7f53a56SChris Kirby if (zhp->zfs_type == ZFS_TYPE_FILESYSTEM) {
1872a7f53a56SChris Kirby uint64_t version;
1873a7f53a56SChris Kirby version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
1874a7f53a56SChris Kirby if (version >= ZPL_VERSION_SA) {
1875a7f53a56SChris Kirby featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1876a7f53a56SChris Kirby }
1877a7f53a56SChris Kirby }
1878a7f53a56SChris Kirby
18796ccda740Sloli if (flags->holds)
18806ccda740Sloli featureflags |= DMU_BACKUP_FEATURE_HOLDS;
18816ccda740Sloli
1882eb633035STom Caputi /*
1883eb633035STom Caputi * Start the dedup thread if this is a dedup stream. We do not bother
1884eb633035STom Caputi * doing this if this a raw send of an encrypted dataset with dedup off
1885eb633035STom Caputi * because normal encrypted blocks won't dedup.
1886eb633035STom Caputi */
1887eb633035STom Caputi if (flags->dedup && !flags->dryrun && !(flags->raw &&
1888eb633035STom Caputi zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) != ZIO_CRYPT_OFF &&
1889eb633035STom Caputi zfs_prop_get_int(zhp, ZFS_PROP_DEDUP) == ZIO_CHECKSUM_OFF)) {
18908e714474SLori Alt featureflags |= (DMU_BACKUP_FEATURE_DEDUP |
18918e714474SLori Alt DMU_BACKUP_FEATURE_DEDUPPROPS);
189288f61deeSIgor Kozhukhov if ((err = pipe(pipefd)) != 0) {
18939e69d7d0SLori Alt zfs_error_aux(zhp->zfs_hdl, strerror(errno));
18949e69d7d0SLori Alt return (zfs_error(zhp->zfs_hdl, EZFS_PIPEFAILED,
18959e69d7d0SLori Alt errbuf));
18969e69d7d0SLori Alt }
18979e69d7d0SLori Alt dda.outputfd = outfd;
18989e69d7d0SLori Alt dda.inputfd = pipefd[1];
18999e69d7d0SLori Alt dda.dedup_hdl = zhp->zfs_hdl;
190088f61deeSIgor Kozhukhov if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
19019e69d7d0SLori Alt (void) close(pipefd[0]);
19029e69d7d0SLori Alt (void) close(pipefd[1]);
19039e69d7d0SLori Alt zfs_error_aux(zhp->zfs_hdl, strerror(errno));
19049e69d7d0SLori Alt return (zfs_error(zhp->zfs_hdl,
19059e69d7d0SLori Alt EZFS_THREADCREATEFAILED, errbuf));
19069e69d7d0SLori Alt }
19079e69d7d0SLori Alt }
19089e69d7d0SLori Alt
19096ccda740Sloli if (flags->replicate || flags->doall || flags->props ||
19106ccda740Sloli flags->holds || flags->backup) {
19113cb34c60Sahrens dmu_replay_record_t drr = { 0 };
19123cb34c60Sahrens char *packbuf = NULL;
19133cb34c60Sahrens size_t buflen = 0;
19146ccda740Sloli zio_cksum_t zc;
19153cb34c60Sahrens
19166ccda740Sloli ZIO_SET_CHECKSUM(&zc, 0, 0, 0, 0);
19176ccda740Sloli
19186ccda740Sloli if (flags->replicate || flags->props || flags->backup ||
19196ccda740Sloli flags->holds) {
19203cb34c60Sahrens nvlist_t *hdrnv;
19213cb34c60Sahrens
19223cb34c60Sahrens VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
19233cb34c60Sahrens if (fromsnap) {
19243cb34c60Sahrens VERIFY(0 == nvlist_add_string(hdrnv,
19253cb34c60Sahrens "fromsnap", fromsnap));
19263cb34c60Sahrens }
19273cb34c60Sahrens VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
192819b94df9SMatthew Ahrens if (!flags->replicate) {
192992241e0bSTom Erickson VERIFY(0 == nvlist_add_boolean(hdrnv,
193092241e0bSTom Erickson "not_recursive"));
193192241e0bSTom Erickson }
1932eb633035STom Caputi if (flags->raw) {
1933eb633035STom Caputi VERIFY(0 == nvlist_add_boolean(hdrnv, "raw"));
1934eb633035STom Caputi }
19353cb34c60Sahrens
19363cb34c60Sahrens err = gather_nvlist(zhp->zfs_hdl, zhp->zfs_name,
1937eb633035STom Caputi fromsnap, tosnap, flags->replicate, flags->raw,
19386ccda740Sloli flags->verbose, flags->backup,
19396ccda740Sloli flags->holds, flags->props, &fss,
19406ccda740Sloli &fsavl);
1941c5286370SAllan Jude if (err) {
1942c5286370SAllan Jude nvlist_free(hdrnv);
19439e69d7d0SLori Alt goto err_out;
1944c5286370SAllan Jude }
1945c5286370SAllan Jude
1946c5286370SAllan Jude /*
1947c5286370SAllan Jude * Do not allow the size of the properties list to
1948c5286370SAllan Jude * exceed the limit
1949c5286370SAllan Jude */
1950c5286370SAllan Jude if ((fnvlist_size(fss) + fnvlist_size(hdrnv)) >
1951c5286370SAllan Jude zhp->zfs_hdl->libzfs_max_nvlist) {
1952c5286370SAllan Jude (void) snprintf(errbuf, sizeof (errbuf),
1953c5286370SAllan Jude dgettext(TEXT_DOMAIN,
1954c5286370SAllan Jude "warning: cannot send '%s': "
1955c5286370SAllan Jude "the size of the list of snapshots and "
1956c5286370SAllan Jude "properties is too large to be received "
1957c5286370SAllan Jude "successfully.\n"
1958c5286370SAllan Jude "Select a smaller number of snapshots to "
1959c5286370SAllan Jude "send.\n"),
1960c5286370SAllan Jude zhp->zfs_name);
1961c5286370SAllan Jude nvlist_free(hdrnv);
1962c5286370SAllan Jude err = zfs_error(zhp->zfs_hdl, EZFS_NOSPC,
1963c5286370SAllan Jude errbuf);
1964c5286370SAllan Jude goto err_out;
1965c5286370SAllan Jude }
19663cb34c60Sahrens VERIFY(0 == nvlist_add_nvlist(hdrnv, "fss", fss));
19673cb34c60Sahrens err = nvlist_pack(hdrnv, &packbuf, &buflen,
19683cb34c60Sahrens NV_ENCODE_XDR, 0);
19693f9d6ad7SLin Ling if (debugnvp)
19703f9d6ad7SLin Ling *debugnvp = hdrnv;
19713f9d6ad7SLin Ling else
19723f9d6ad7SLin Ling nvlist_free(hdrnv);
1973a7a845e4SSteven Hartland if (err)
19749e69d7d0SLori Alt goto stderr_out;
19753cb34c60Sahrens }
19763cb34c60Sahrens
197719b94df9SMatthew Ahrens if (!flags->dryrun) {
197819b94df9SMatthew Ahrens /* write first begin record */
197919b94df9SMatthew Ahrens drr.drr_type = DRR_BEGIN;
198019b94df9SMatthew Ahrens drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
198119b94df9SMatthew Ahrens DMU_SET_STREAM_HDRTYPE(drr.drr_u.drr_begin.
198219b94df9SMatthew Ahrens drr_versioninfo, DMU_COMPOUNDSTREAM);
198319b94df9SMatthew Ahrens DMU_SET_FEATUREFLAGS(drr.drr_u.drr_begin.
198419b94df9SMatthew Ahrens drr_versioninfo, featureflags);
198519b94df9SMatthew Ahrens (void) snprintf(drr.drr_u.drr_begin.drr_toname,
198619b94df9SMatthew Ahrens sizeof (drr.drr_u.drr_begin.drr_toname),
198719b94df9SMatthew Ahrens "%s@%s", zhp->zfs_name, tosnap);
198819b94df9SMatthew Ahrens drr.drr_payloadlen = buflen;
198919b94df9SMatthew Ahrens
199098110f08SMatthew Ahrens err = dump_record(&drr, packbuf, buflen, &zc, outfd);
199119b94df9SMatthew Ahrens free(packbuf);
199298110f08SMatthew Ahrens if (err != 0)
199319b94df9SMatthew Ahrens goto stderr_out;
19948ac09fceSRichard Lowe
199519b94df9SMatthew Ahrens /* write end record */
19968ac09fceSRichard Lowe bzero(&drr, sizeof (drr));
19978ac09fceSRichard Lowe drr.drr_type = DRR_END;
19988ac09fceSRichard Lowe drr.drr_u.drr_end.drr_checksum = zc;
19998ac09fceSRichard Lowe err = write(outfd, &drr, sizeof (drr));
20003cb34c60Sahrens if (err == -1) {
2001c6fd73aeSChris Kirby err = errno;
20029e69d7d0SLori Alt goto stderr_out;
20033cb34c60Sahrens }
200419b94df9SMatthew Ahrens
200519b94df9SMatthew Ahrens err = 0;
20063cb34c60Sahrens }
20073cb34c60Sahrens }
20083cb34c60Sahrens
20093cb34c60Sahrens /* dump each stream */
20103cb34c60Sahrens sdd.fromsnap = fromsnap;
20113cb34c60Sahrens sdd.tosnap = tosnap;
2012a7a845e4SSteven Hartland if (tid != 0)
20139e69d7d0SLori Alt sdd.outfd = pipefd[0];
20149e69d7d0SLori Alt else
20159e69d7d0SLori Alt sdd.outfd = outfd;
201619b94df9SMatthew Ahrens sdd.replicate = flags->replicate;
201719b94df9SMatthew Ahrens sdd.doall = flags->doall;
201819b94df9SMatthew Ahrens sdd.fromorigin = flags->fromorigin;
20193cb34c60Sahrens sdd.fss = fss;
20203cb34c60Sahrens sdd.fsavl = fsavl;
202119b94df9SMatthew Ahrens sdd.verbose = flags->verbose;
202219b94df9SMatthew Ahrens sdd.parsable = flags->parsable;
20234e3c9f44SBill Pijewski sdd.progress = flags->progress;
202419b94df9SMatthew Ahrens sdd.dryrun = flags->dryrun;
2025b5152584SMatthew Ahrens sdd.large_block = flags->largeblock;
20265d7b4d43SMatthew Ahrens sdd.embed_data = flags->embed_data;
20275602294fSDan Kimmel sdd.compress = flags->compress;
2028eb633035STom Caputi sdd.raw = flags->raw;
20296ccda740Sloli sdd.holds = flags->holds;
20309e69d7d0SLori Alt sdd.filter_cb = filter_func;
20319e69d7d0SLori Alt sdd.filter_cb_arg = cb_arg;
20323f9d6ad7SLin Ling if (debugnvp)
20333f9d6ad7SLin Ling sdd.debugnv = *debugnvp;
2034dc5f28a3SManoj Joseph if (sdd.verbose && sdd.dryrun)
2035dc5f28a3SManoj Joseph sdd.std_out = B_TRUE;
2036dc5f28a3SManoj Joseph fout = sdd.std_out ? stdout : stderr;
203765fec9f6SChristopher Siden
203865fec9f6SChristopher Siden /*
203965fec9f6SChristopher Siden * Some flags require that we place user holds on the datasets that are
204065fec9f6SChristopher Siden * being sent so they don't get destroyed during the send. We can skip
204165fec9f6SChristopher Siden * this step if the pool is imported read-only since the datasets cannot
204265fec9f6SChristopher Siden * be destroyed.
204365fec9f6SChristopher Siden */
204465fec9f6SChristopher Siden if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
204565fec9f6SChristopher Siden ZPOOL_PROP_READONLY, NULL) &&
204665fec9f6SChristopher Siden zfs_spa_version(zhp, &spa_version) == 0 &&
204765fec9f6SChristopher Siden spa_version >= SPA_VERSION_USERREFS &&
204865fec9f6SChristopher Siden (flags->doall || flags->replicate)) {
2049a7f53a56SChris Kirby ++holdseq;
2050a7f53a56SChris Kirby (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
2051a7f53a56SChris Kirby ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
2052a7f53a56SChris Kirby sdd.cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
2053a7f53a56SChris Kirby if (sdd.cleanup_fd < 0) {
2054a7f53a56SChris Kirby err = errno;
2055a7f53a56SChris Kirby goto stderr_out;
2056a7f53a56SChris Kirby }
2057a7a845e4SSteven Hartland sdd.snapholds = fnvlist_alloc();
2058a7f53a56SChris Kirby } else {
2059a7f53a56SChris Kirby sdd.cleanup_fd = -1;
2060a7a845e4SSteven Hartland sdd.snapholds = NULL;
2061a7f53a56SChris Kirby }
20626ccda740Sloli
2063a7a845e4SSteven Hartland if (flags->verbose || sdd.snapholds != NULL) {
206419b94df9SMatthew Ahrens /*
206519b94df9SMatthew Ahrens * Do a verbose no-op dry run to get all the verbose output
2066a7a845e4SSteven Hartland * or to gather snapshot hold's before generating any data,
2067a7a845e4SSteven Hartland * then do a non-verbose real run to generate the streams.
206819b94df9SMatthew Ahrens */
206919b94df9SMatthew Ahrens sdd.dryrun = B_TRUE;
207019b94df9SMatthew Ahrens err = dump_filesystems(zhp, &sdd);
2071a7a845e4SSteven Hartland
2072a7a845e4SSteven Hartland if (err != 0)
2073a7a845e4SSteven Hartland goto stderr_out;
2074a7a845e4SSteven Hartland
2075a7a845e4SSteven Hartland if (flags->verbose) {
2076a7a845e4SSteven Hartland if (flags->parsable) {
2077dc5f28a3SManoj Joseph (void) fprintf(fout, "size\t%llu\n",
2078a7a845e4SSteven Hartland (longlong_t)sdd.size);
2079a7a845e4SSteven Hartland } else {
2080a7a845e4SSteven Hartland char buf[16];
20816520eed5SToomas Soome zfs_nicebytes(sdd.size, buf, sizeof (buf));
2082dc5f28a3SManoj Joseph (void) fprintf(fout, dgettext(TEXT_DOMAIN,
2083a7a845e4SSteven Hartland "total estimated size is %s\n"), buf);
2084a7a845e4SSteven Hartland }
2085a7a845e4SSteven Hartland }
2086a7a845e4SSteven Hartland
2087a7a845e4SSteven Hartland /* Ensure no snaps found is treated as an error. */
2088a7a845e4SSteven Hartland if (!sdd.seento) {
2089a7a845e4SSteven Hartland err = ENOENT;
2090a7a845e4SSteven Hartland goto err_out;
2091a7a845e4SSteven Hartland }
2092a7a845e4SSteven Hartland
2093a7a845e4SSteven Hartland /* Skip the second run if dryrun was requested. */
2094a7a845e4SSteven Hartland if (flags->dryrun)
2095a7a845e4SSteven Hartland goto err_out;
2096a7a845e4SSteven Hartland
2097a7a845e4SSteven Hartland if (sdd.snapholds != NULL) {
2098a7a845e4SSteven Hartland err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
2099a7a845e4SSteven Hartland if (err != 0)
2100a7a845e4SSteven Hartland goto stderr_out;
2101a7a845e4SSteven Hartland
2102a7a845e4SSteven Hartland fnvlist_free(sdd.snapholds);
2103a7a845e4SSteven Hartland sdd.snapholds = NULL;
210419b94df9SMatthew Ahrens }
2105a7a845e4SSteven Hartland
2106a7a845e4SSteven Hartland sdd.dryrun = B_FALSE;
2107a7a845e4SSteven Hartland sdd.verbose = B_FALSE;
210819b94df9SMatthew Ahrens }
2109a7a845e4SSteven Hartland
21103cb34c60Sahrens err = dump_filesystems(zhp, &sdd);
21113cb34c60Sahrens fsavl_destroy(fsavl);
21123cb34c60Sahrens nvlist_free(fss);
21133cb34c60Sahrens
2114a7a845e4SSteven Hartland /* Ensure no snaps found is treated as an error. */
2115a7a845e4SSteven Hartland if (err == 0 && !sdd.seento)
2116a7a845e4SSteven Hartland err = ENOENT;
2117a7a845e4SSteven Hartland
2118a7a845e4SSteven Hartland if (tid != 0) {
2119a7a845e4SSteven Hartland if (err != 0)
2120a7a845e4SSteven Hartland (void) pthread_cancel(tid);
2121a7a845e4SSteven Hartland (void) close(pipefd[0]);
212236f7455dSSteven Hartland (void) pthread_join(tid, NULL);
21239e69d7d0SLori Alt }
212492241e0bSTom Erickson
2125a7f53a56SChris Kirby if (sdd.cleanup_fd != -1) {
2126a7f53a56SChris Kirby VERIFY(0 == close(sdd.cleanup_fd));
2127a7f53a56SChris Kirby sdd.cleanup_fd = -1;
2128c99e4bdcSChris Kirby }
2129c99e4bdcSChris Kirby
213019b94df9SMatthew Ahrens if (!flags->dryrun && (flags->replicate || flags->doall ||
21316ccda740Sloli flags->props || flags->backup || flags->holds)) {
21323cb34c60Sahrens /*
21333cb34c60Sahrens * write final end record. NB: want to do this even if
21343cb34c60Sahrens * there was some error, because it might not be totally
21353cb34c60Sahrens * failed.
21363cb34c60Sahrens */
21373cb34c60Sahrens dmu_replay_record_t drr = { 0 };
21383cb34c60Sahrens drr.drr_type = DRR_END;
21393cb34c60Sahrens if (write(outfd, &drr, sizeof (drr)) == -1) {
21403cb34c60Sahrens return (zfs_standard_error(zhp->zfs_hdl,
21413cb34c60Sahrens errno, errbuf));
21423cb34c60Sahrens }
21433cb34c60Sahrens }
21443cb34c60Sahrens
21453cb34c60Sahrens return (err || sdd.err);
21469e69d7d0SLori Alt
21479e69d7d0SLori Alt stderr_out:
21489e69d7d0SLori Alt err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
21499e69d7d0SLori Alt err_out:
2150a7a845e4SSteven Hartland fsavl_destroy(fsavl);
2151a7a845e4SSteven Hartland nvlist_free(fss);
2152a7a845e4SSteven Hartland fnvlist_free(sdd.snapholds);
2153a7a845e4SSteven Hartland
2154a7f53a56SChris Kirby if (sdd.cleanup_fd != -1)
2155a7f53a56SChris Kirby VERIFY(0 == close(sdd.cleanup_fd));
2156a7a845e4SSteven Hartland if (tid != 0) {
21579e69d7d0SLori Alt (void) pthread_cancel(tid);
21589e69d7d0SLori Alt (void) close(pipefd[0]);
215936f7455dSSteven Hartland (void) pthread_join(tid, NULL);
21609e69d7d0SLori Alt }
21619e69d7d0SLori Alt return (err);
21623cb34c60Sahrens }
21633cb34c60Sahrens
216478f17100SMatthew Ahrens int
zfs_send_one(zfs_handle_t * zhp,const char * from,int fd,enum lzc_send_flags flags)21655d7b4d43SMatthew Ahrens zfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
21665d7b4d43SMatthew Ahrens enum lzc_send_flags flags)
216778f17100SMatthew Ahrens {
216878f17100SMatthew Ahrens int err;
216978f17100SMatthew Ahrens libzfs_handle_t *hdl = zhp->zfs_hdl;
217078f17100SMatthew Ahrens
217178f17100SMatthew Ahrens char errbuf[1024];
217278f17100SMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
217378f17100SMatthew Ahrens "warning: cannot send '%s'"), zhp->zfs_name);
217478f17100SMatthew Ahrens
21755d7b4d43SMatthew Ahrens err = lzc_send(zhp->zfs_name, from, fd, flags);
217678f17100SMatthew Ahrens if (err != 0) {
217778f17100SMatthew Ahrens switch (errno) {
217878f17100SMatthew Ahrens case EXDEV:
217978f17100SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
218078f17100SMatthew Ahrens "not an earlier snapshot from the same fs"));
218178f17100SMatthew Ahrens return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
218278f17100SMatthew Ahrens
218378f17100SMatthew Ahrens case ENOENT:
218478f17100SMatthew Ahrens case ESRCH:
218578f17100SMatthew Ahrens if (lzc_exists(zhp->zfs_name)) {
218678f17100SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
218778f17100SMatthew Ahrens "incremental source (%s) does not exist"),
218878f17100SMatthew Ahrens from);
218978f17100SMatthew Ahrens }
219078f17100SMatthew Ahrens return (zfs_error(hdl, EZFS_NOENT, errbuf));
219178f17100SMatthew Ahrens
2192eb633035STom Caputi case EACCES:
2193eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
2194eb633035STom Caputi "dataset key must be loaded"));
2195eb633035STom Caputi return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf));
2196eb633035STom Caputi
219778f17100SMatthew Ahrens case EBUSY:
219878f17100SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
219978f17100SMatthew Ahrens "target is busy; if a filesystem, "
220078f17100SMatthew Ahrens "it must not be mounted"));
220178f17100SMatthew Ahrens return (zfs_error(hdl, EZFS_BUSY, errbuf));
220278f17100SMatthew Ahrens
220378f17100SMatthew Ahrens case EDQUOT:
220478f17100SMatthew Ahrens case EFBIG:
220578f17100SMatthew Ahrens case EIO:
220678f17100SMatthew Ahrens case ENOLINK:
220778f17100SMatthew Ahrens case ENOSPC:
220878f17100SMatthew Ahrens case ENOSTR:
220978f17100SMatthew Ahrens case ENXIO:
221078f17100SMatthew Ahrens case EPIPE:
221178f17100SMatthew Ahrens case ERANGE:
221278f17100SMatthew Ahrens case EFAULT:
221378f17100SMatthew Ahrens case EROFS:
221478f17100SMatthew Ahrens zfs_error_aux(hdl, strerror(errno));
221578f17100SMatthew Ahrens return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
221678f17100SMatthew Ahrens
221778f17100SMatthew Ahrens default:
221878f17100SMatthew Ahrens return (zfs_standard_error(hdl, errno, errbuf));
221978f17100SMatthew Ahrens }
222078f17100SMatthew Ahrens }
222178f17100SMatthew Ahrens return (err != 0);
222278f17100SMatthew Ahrens }
222378f17100SMatthew Ahrens
22243cb34c60Sahrens /*
22253cb34c60Sahrens * Routines specific to "zfs recv"
22263cb34c60Sahrens */
22273cb34c60Sahrens
22283cb34c60Sahrens static int
recv_read(libzfs_handle_t * hdl,int fd,void * buf,int ilen,boolean_t byteswap,zio_cksum_t * zc)22293cb34c60Sahrens recv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
22303cb34c60Sahrens boolean_t byteswap, zio_cksum_t *zc)
22313cb34c60Sahrens {
22323cb34c60Sahrens char *cp = buf;
22333cb34c60Sahrens int rv;
22343cb34c60Sahrens int len = ilen;
22353cb34c60Sahrens
22363cb34c60Sahrens do {
22373cb34c60Sahrens rv = read(fd, cp, len);
22383cb34c60Sahrens cp += rv;
22393cb34c60Sahrens len -= rv;
22403cb34c60Sahrens } while (rv > 0);
22413cb34c60Sahrens
22423cb34c60Sahrens if (rv < 0 || len != 0) {
22433cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
22443cb34c60Sahrens "failed to read from stream"));
22453cb34c60Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
22463cb34c60Sahrens "cannot receive")));
22473cb34c60Sahrens }
22483cb34c60Sahrens
22493cb34c60Sahrens if (zc) {
22503cb34c60Sahrens if (byteswap)
2251770499e1SDan Kimmel (void) fletcher_4_incremental_byteswap(buf, ilen, zc);
22523cb34c60Sahrens else
2253770499e1SDan Kimmel (void) fletcher_4_incremental_native(buf, ilen, zc);
22543cb34c60Sahrens }
22553cb34c60Sahrens return (0);
22563cb34c60Sahrens }
22573cb34c60Sahrens
22583cb34c60Sahrens static int
recv_read_nvlist(libzfs_handle_t * hdl,int fd,int len,nvlist_t ** nvp,boolean_t byteswap,zio_cksum_t * zc)22593cb34c60Sahrens recv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
22603cb34c60Sahrens boolean_t byteswap, zio_cksum_t *zc)
22613cb34c60Sahrens {
22623cb34c60Sahrens char *buf;
22633cb34c60Sahrens int err;
22643cb34c60Sahrens
22653cb34c60Sahrens buf = zfs_alloc(hdl, len);
22663cb34c60Sahrens if (buf == NULL)
22673cb34c60Sahrens return (ENOMEM);
22683cb34c60Sahrens
2269c5286370SAllan Jude if (len > hdl->libzfs_max_nvlist) {
2270c5286370SAllan Jude zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "nvlist too large"));
2271c5286370SAllan Jude free(buf);
2272c5286370SAllan Jude return (ENOMEM);
2273c5286370SAllan Jude }
2274c5286370SAllan Jude
22753cb34c60Sahrens err = recv_read(hdl, fd, buf, len, byteswap, zc);
22763cb34c60Sahrens if (err != 0) {
22773cb34c60Sahrens free(buf);
22783cb34c60Sahrens return (err);
22793cb34c60Sahrens }
22803cb34c60Sahrens
22813cb34c60Sahrens err = nvlist_unpack(buf, len, nvp, 0);
22823cb34c60Sahrens free(buf);
22833cb34c60Sahrens if (err != 0) {
22843cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
22853cb34c60Sahrens "stream (malformed nvlist)"));
22863cb34c60Sahrens return (EINVAL);
22873cb34c60Sahrens }
22883cb34c60Sahrens return (0);
22893cb34c60Sahrens }
22903cb34c60Sahrens
2291eb633035STom Caputi /*
2292eb633035STom Caputi * Returns the grand origin (origin of origin of origin...) of a given handle.
2293eb633035STom Caputi * If this dataset is not a clone, it simply returns a copy of the original
2294eb633035STom Caputi * handle.
2295eb633035STom Caputi */
2296eb633035STom Caputi static zfs_handle_t *
recv_open_grand_origin(zfs_handle_t * zhp)2297eb633035STom Caputi recv_open_grand_origin(zfs_handle_t *zhp)
2298eb633035STom Caputi {
2299eb633035STom Caputi char origin[ZFS_MAX_DATASET_NAME_LEN];
2300eb633035STom Caputi zprop_source_t src;
2301eb633035STom Caputi zfs_handle_t *ozhp = zfs_handle_dup(zhp);
2302eb633035STom Caputi
2303eb633035STom Caputi while (ozhp != NULL) {
2304eb633035STom Caputi if (zfs_prop_get(ozhp, ZFS_PROP_ORIGIN, origin,
2305eb633035STom Caputi sizeof (origin), &src, NULL, 0, B_FALSE) != 0)
2306eb633035STom Caputi break;
2307eb633035STom Caputi
2308eb633035STom Caputi (void) zfs_close(ozhp);
2309eb633035STom Caputi ozhp = zfs_open(zhp->zfs_hdl, origin, ZFS_TYPE_FILESYSTEM);
2310eb633035STom Caputi }
2311eb633035STom Caputi
2312eb633035STom Caputi return (ozhp);
2313eb633035STom Caputi }
2314eb633035STom Caputi
2315eb633035STom Caputi static int
recv_rename_impl(zfs_handle_t * zhp,const char * source,const char * target)2316eb633035STom Caputi recv_rename_impl(zfs_handle_t *zhp, const char *source, const char *target)
2317eb633035STom Caputi {
2318eb633035STom Caputi int err;
2319eb633035STom Caputi zfs_handle_t *ozhp = NULL;
2320eb633035STom Caputi
2321eb633035STom Caputi /*
2322eb633035STom Caputi * Attempt to rename the dataset. If it fails with EACCES we have
2323eb633035STom Caputi * attempted to rename the dataset outside of its encryption root.
2324eb633035STom Caputi * Force the dataset to become an encryption root and try again.
2325eb633035STom Caputi */
2326eb633035STom Caputi err = lzc_rename(source, target);
2327eb633035STom Caputi if (err == EACCES) {
2328eb633035STom Caputi ozhp = recv_open_grand_origin(zhp);
2329eb633035STom Caputi if (ozhp == NULL) {
2330eb633035STom Caputi err = ENOENT;
2331eb633035STom Caputi goto out;
2332eb633035STom Caputi }
2333eb633035STom Caputi
2334eb633035STom Caputi err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2335eb633035STom Caputi NULL, NULL, 0);
2336eb633035STom Caputi if (err != 0)
2337eb633035STom Caputi goto out;
2338eb633035STom Caputi
2339eb633035STom Caputi err = lzc_rename(source, target);
2340eb633035STom Caputi }
2341eb633035STom Caputi
2342eb633035STom Caputi out:
2343eb633035STom Caputi if (ozhp != NULL)
2344eb633035STom Caputi zfs_close(ozhp);
2345eb633035STom Caputi return (err);
2346eb633035STom Caputi }
2347eb633035STom Caputi
23483cb34c60Sahrens static int
recv_rename(libzfs_handle_t * hdl,const char * name,const char * tryname,int baselen,char * newname,recvflags_t * flags)23493cb34c60Sahrens recv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
235019b94df9SMatthew Ahrens int baselen, char *newname, recvflags_t *flags)
23513cb34c60Sahrens {
23523cb34c60Sahrens static int seq;
23533cb34c60Sahrens int err;
2354eb633035STom Caputi prop_changelist_t *clp = NULL;
2355eb633035STom Caputi zfs_handle_t *zhp = NULL;
23563cb34c60Sahrens
2357d8d77200Sahrens zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2358eb633035STom Caputi if (zhp == NULL) {
2359eb633035STom Caputi err = -1;
2360eb633035STom Caputi goto out;
2361eb633035STom Caputi }
23620069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
236319b94df9SMatthew Ahrens flags->force ? MS_FORCE : 0);
2364eb633035STom Caputi if (clp == NULL) {
2365eb633035STom Caputi err = -1;
2366eb633035STom Caputi goto out;
2367eb633035STom Caputi }
2368d8d77200Sahrens err = changelist_prefix(clp);
2369d8d77200Sahrens if (err)
2370eb633035STom Caputi goto out;
23713cb34c60Sahrens
23723cb34c60Sahrens if (tryname) {
23733cb34c60Sahrens (void) strcpy(newname, tryname);
237419b94df9SMatthew Ahrens if (flags->verbose) {
23753cb34c60Sahrens (void) printf("attempting rename %s to %s\n",
2376049ba636SAndriy Gapon name, newname);
23773cb34c60Sahrens }
2378eb633035STom Caputi err = recv_rename_impl(zhp, name, newname);
2379d8d77200Sahrens if (err == 0)
23803cb34c60Sahrens changelist_rename(clp, name, tryname);
23813cb34c60Sahrens } else {
23823cb34c60Sahrens err = ENOENT;
23833cb34c60Sahrens }
23843cb34c60Sahrens
23853b2aab18SMatthew Ahrens if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
23863cb34c60Sahrens seq++;
23873cb34c60Sahrens
23889adfa60dSMatthew Ahrens (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
23899adfa60dSMatthew Ahrens "%.*srecv-%u-%u", baselen, name, getpid(), seq);
239019b94df9SMatthew Ahrens if (flags->verbose) {
23913cb34c60Sahrens (void) printf("failed - trying rename %s to %s\n",
2392049ba636SAndriy Gapon name, newname);
23933cb34c60Sahrens }
2394eb633035STom Caputi err = recv_rename_impl(zhp, name, newname);
2395d8d77200Sahrens if (err == 0)
23963cb34c60Sahrens changelist_rename(clp, name, newname);
239719b94df9SMatthew Ahrens if (err && flags->verbose) {
23983cb34c60Sahrens (void) printf("failed (%u) - "
23993cb34c60Sahrens "will try again on next pass\n", errno);
24003cb34c60Sahrens }
24013cb34c60Sahrens err = EAGAIN;
240219b94df9SMatthew Ahrens } else if (flags->verbose) {
24033cb34c60Sahrens if (err == 0)
24043cb34c60Sahrens (void) printf("success\n");
24053cb34c60Sahrens else
24063cb34c60Sahrens (void) printf("failed (%u)\n", errno);
24073cb34c60Sahrens }
24083cb34c60Sahrens
2409d8d77200Sahrens (void) changelist_postfix(clp);
2410eb633035STom Caputi
2411eb633035STom Caputi out:
2412eb633035STom Caputi if (clp != NULL)
2413eb633035STom Caputi changelist_free(clp);
2414eb633035STom Caputi if (zhp != NULL)
2415eb633035STom Caputi zfs_close(zhp);
2416eb633035STom Caputi
2417eb633035STom Caputi return (err);
2418eb633035STom Caputi }
2419eb633035STom Caputi
2420eb633035STom Caputi static int
recv_promote(libzfs_handle_t * hdl,const char * fsname,const char * origin_fsname,recvflags_t * flags)2421eb633035STom Caputi recv_promote(libzfs_handle_t *hdl, const char *fsname,
2422eb633035STom Caputi const char *origin_fsname, recvflags_t *flags)
2423eb633035STom Caputi {
2424eb633035STom Caputi int err;
2425eb633035STom Caputi zfs_cmd_t zc = {"\0"};
2426eb633035STom Caputi zfs_handle_t *zhp = NULL, *ozhp = NULL;
2427eb633035STom Caputi
2428eb633035STom Caputi if (flags->verbose)
2429eb633035STom Caputi (void) printf("promoting %s\n", fsname);
2430eb633035STom Caputi
2431eb633035STom Caputi (void) strlcpy(zc.zc_value, origin_fsname, sizeof (zc.zc_value));
2432eb633035STom Caputi (void) strlcpy(zc.zc_name, fsname, sizeof (zc.zc_name));
2433eb633035STom Caputi
2434eb633035STom Caputi /*
2435eb633035STom Caputi * Attempt to promote the dataset. If it fails with EACCES the
2436eb633035STom Caputi * promotion would cause this dataset to leave its encryption root.
2437eb633035STom Caputi * Force the origin to become an encryption root and try again.
2438eb633035STom Caputi */
2439eb633035STom Caputi err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2440eb633035STom Caputi if (err == EACCES) {
2441eb633035STom Caputi zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2442eb633035STom Caputi if (zhp == NULL) {
2443eb633035STom Caputi err = -1;
2444eb633035STom Caputi goto out;
2445eb633035STom Caputi }
2446eb633035STom Caputi
2447eb633035STom Caputi ozhp = recv_open_grand_origin(zhp);
2448eb633035STom Caputi if (ozhp == NULL) {
2449eb633035STom Caputi err = -1;
2450eb633035STom Caputi goto out;
2451eb633035STom Caputi }
2452eb633035STom Caputi
2453eb633035STom Caputi err = lzc_change_key(ozhp->zfs_name, DCP_CMD_FORCE_NEW_KEY,
2454eb633035STom Caputi NULL, NULL, 0);
2455eb633035STom Caputi if (err != 0)
2456eb633035STom Caputi goto out;
2457eb633035STom Caputi
2458eb633035STom Caputi err = zfs_ioctl(hdl, ZFS_IOC_PROMOTE, &zc);
2459eb633035STom Caputi }
2460eb633035STom Caputi
2461eb633035STom Caputi out:
2462eb633035STom Caputi if (zhp != NULL)
2463eb633035STom Caputi zfs_close(zhp);
2464eb633035STom Caputi if (ozhp != NULL)
2465eb633035STom Caputi zfs_close(ozhp);
24663cb34c60Sahrens
24673cb34c60Sahrens return (err);
24683cb34c60Sahrens }
24693cb34c60Sahrens
24703cb34c60Sahrens static int
recv_destroy(libzfs_handle_t * hdl,const char * name,int baselen,char * newname,recvflags_t * flags)24713cb34c60Sahrens recv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
247219b94df9SMatthew Ahrens char *newname, recvflags_t *flags)
24733cb34c60Sahrens {
2474d8d77200Sahrens int err = 0;
2475d8d77200Sahrens prop_changelist_t *clp;
2476d8d77200Sahrens zfs_handle_t *zhp;
2477842727c2SChris Kirby boolean_t defer = B_FALSE;
2478842727c2SChris Kirby int spa_version;
2479d8d77200Sahrens
2480d8d77200Sahrens zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
2481d8d77200Sahrens if (zhp == NULL)
2482d8d77200Sahrens return (-1);
24830069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0,
248419b94df9SMatthew Ahrens flags->force ? MS_FORCE : 0);
2485842727c2SChris Kirby if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT &&
2486842727c2SChris Kirby zfs_spa_version(zhp, &spa_version) == 0 &&
2487842727c2SChris Kirby spa_version >= SPA_VERSION_USERREFS)
2488842727c2SChris Kirby defer = B_TRUE;
2489d8d77200Sahrens zfs_close(zhp);
2490d8d77200Sahrens if (clp == NULL)
2491d8d77200Sahrens return (-1);
2492d8d77200Sahrens err = changelist_prefix(clp);
2493d8d77200Sahrens if (err)
2494d8d77200Sahrens return (err);
24953cb34c60Sahrens
249619b94df9SMatthew Ahrens if (flags->verbose)
2497049ba636SAndriy Gapon (void) printf("attempting destroy %s\n", name);
2498049ba636SAndriy Gapon if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) {
2499049ba636SAndriy Gapon nvlist_t *nv = fnvlist_alloc();
2500049ba636SAndriy Gapon fnvlist_add_boolean(nv, name);
2501049ba636SAndriy Gapon err = lzc_destroy_snaps(nv, defer, NULL);
2502049ba636SAndriy Gapon fnvlist_free(nv);
2503049ba636SAndriy Gapon } else {
2504049ba636SAndriy Gapon err = lzc_destroy(name);
2505049ba636SAndriy Gapon }
2506d8d77200Sahrens if (err == 0) {
250719b94df9SMatthew Ahrens if (flags->verbose)
25083cb34c60Sahrens (void) printf("success\n");
2509049ba636SAndriy Gapon changelist_remove(clp, name);
25103cb34c60Sahrens }
25113cb34c60Sahrens
2512d8d77200Sahrens (void) changelist_postfix(clp);
2513d8d77200Sahrens changelist_free(clp);
2514d8d77200Sahrens
2515842727c2SChris Kirby /*
251692241e0bSTom Erickson * Deferred destroy might destroy the snapshot or only mark it to be
251792241e0bSTom Erickson * destroyed later, and it returns success in either case.
2518842727c2SChris Kirby */
251992241e0bSTom Erickson if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
252092241e0bSTom Erickson ZFS_TYPE_SNAPSHOT))) {
2521d8d77200Sahrens err = recv_rename(hdl, name, NULL, baselen, newname, flags);
252292241e0bSTom Erickson }
2523d8d77200Sahrens
25243cb34c60Sahrens return (err);
25253cb34c60Sahrens }
25263cb34c60Sahrens
25273cb34c60Sahrens typedef struct guid_to_name_data {
25283cb34c60Sahrens uint64_t guid;
25299c3fd121SMatthew Ahrens boolean_t bookmark_ok;
25303cb34c60Sahrens char *name;
253119b94df9SMatthew Ahrens char *skip;
25323cb34c60Sahrens } guid_to_name_data_t;
25333cb34c60Sahrens
25343cb34c60Sahrens static int
guid_to_name_cb(zfs_handle_t * zhp,void * arg)25353cb34c60Sahrens guid_to_name_cb(zfs_handle_t *zhp, void *arg)
25363cb34c60Sahrens {
25373cb34c60Sahrens guid_to_name_data_t *gtnd = arg;
25389c3fd121SMatthew Ahrens const char *slash;
25393cb34c60Sahrens int err;
25403cb34c60Sahrens
254119b94df9SMatthew Ahrens if (gtnd->skip != NULL &&
25429c3fd121SMatthew Ahrens (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
25439c3fd121SMatthew Ahrens strcmp(slash + 1, gtnd->skip) == 0) {
25449c3fd121SMatthew Ahrens zfs_close(zhp);
254519b94df9SMatthew Ahrens return (0);
254619b94df9SMatthew Ahrens }
254719b94df9SMatthew Ahrens
25489c3fd121SMatthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
25493cb34c60Sahrens (void) strcpy(gtnd->name, zhp->zfs_name);
2550a79992aaSTom Erickson zfs_close(zhp);
25513cb34c60Sahrens return (EEXIST);
25523cb34c60Sahrens }
255319b94df9SMatthew Ahrens
25543cb34c60Sahrens err = zfs_iter_children(zhp, guid_to_name_cb, gtnd);
25559c3fd121SMatthew Ahrens if (err != EEXIST && gtnd->bookmark_ok)
25569c3fd121SMatthew Ahrens err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
25573cb34c60Sahrens zfs_close(zhp);
25583cb34c60Sahrens return (err);
25593cb34c60Sahrens }
25603cb34c60Sahrens
256119b94df9SMatthew Ahrens /*
256219b94df9SMatthew Ahrens * Attempt to find the local dataset associated with this guid. In the case of
256319b94df9SMatthew Ahrens * multiple matches, we attempt to find the "best" match by searching
256419b94df9SMatthew Ahrens * progressively larger portions of the hierarchy. This allows one to send a
256519b94df9SMatthew Ahrens * tree of datasets individually and guarantee that we will find the source
256619b94df9SMatthew Ahrens * guid within that hierarchy, even if there are multiple matches elsewhere.
256719b94df9SMatthew Ahrens */
25683cb34c60Sahrens static int
guid_to_name(libzfs_handle_t * hdl,const char * parent,uint64_t guid,boolean_t bookmark_ok,char * name)25693cb34c60Sahrens guid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
25709c3fd121SMatthew Ahrens boolean_t bookmark_ok, char *name)
25713cb34c60Sahrens {
25729adfa60dSMatthew Ahrens char pname[ZFS_MAX_DATASET_NAME_LEN];
25733cb34c60Sahrens guid_to_name_data_t gtnd;
25743cb34c60Sahrens
25753cb34c60Sahrens gtnd.guid = guid;
25769c3fd121SMatthew Ahrens gtnd.bookmark_ok = bookmark_ok;
25773cb34c60Sahrens gtnd.name = name;
257819b94df9SMatthew Ahrens gtnd.skip = NULL;
25793cb34c60Sahrens
258019b94df9SMatthew Ahrens /*
25819c3fd121SMatthew Ahrens * Search progressively larger portions of the hierarchy, starting
25829c3fd121SMatthew Ahrens * with the filesystem specified by 'parent'. This will
258319b94df9SMatthew Ahrens * select the "most local" version of the origin snapshot in the case
258419b94df9SMatthew Ahrens * that there are multiple matching snapshots in the system.
258519b94df9SMatthew Ahrens */
25869c3fd121SMatthew Ahrens (void) strlcpy(pname, parent, sizeof (pname));
25879c3fd121SMatthew Ahrens char *cp = strrchr(pname, '@');
25889c3fd121SMatthew Ahrens if (cp == NULL)
25899c3fd121SMatthew Ahrens cp = strchr(pname, '\0');
25909c3fd121SMatthew Ahrens for (; cp != NULL; cp = strrchr(pname, '/')) {
259119b94df9SMatthew Ahrens /* Chop off the last component and open the parent */
25923cb34c60Sahrens *cp = '\0';
25939c3fd121SMatthew Ahrens zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
259419b94df9SMatthew Ahrens
259519b94df9SMatthew Ahrens if (zhp == NULL)
259619b94df9SMatthew Ahrens continue;
25979c3fd121SMatthew Ahrens int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
25989c3fd121SMatthew Ahrens if (err != EEXIST)
25999c3fd121SMatthew Ahrens err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
26009c3fd121SMatthew Ahrens if (err != EEXIST && bookmark_ok)
26019c3fd121SMatthew Ahrens err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
26023cb34c60Sahrens zfs_close(zhp);
260319b94df9SMatthew Ahrens if (err == EEXIST)
260419b94df9SMatthew Ahrens return (0);
26053cb34c60Sahrens
260619b94df9SMatthew Ahrens /*
26079c3fd121SMatthew Ahrens * Remember the last portion of the dataset so we skip it next
26089c3fd121SMatthew Ahrens * time through (as we've already searched that portion of the
26099c3fd121SMatthew Ahrens * hierarchy).
261019b94df9SMatthew Ahrens */
26119c3fd121SMatthew Ahrens gtnd.skip = strrchr(pname, '/') + 1;
261219b94df9SMatthew Ahrens }
26133cb34c60Sahrens
261419b94df9SMatthew Ahrens return (ENOENT);
26153cb34c60Sahrens }
26163cb34c60Sahrens
26173cb34c60Sahrens /*
261819b94df9SMatthew Ahrens * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
261919b94df9SMatthew Ahrens * guid1 is after guid2.
26203cb34c60Sahrens */
262104bb726eSahl static int
created_before(libzfs_handle_t * hdl,avl_tree_t * avl,uint64_t guid1,uint64_t guid2)26223cb34c60Sahrens created_before(libzfs_handle_t *hdl, avl_tree_t *avl,
26233cb34c60Sahrens uint64_t guid1, uint64_t guid2)
26243cb34c60Sahrens {
26253cb34c60Sahrens nvlist_t *nvfs;
26263cb34c60Sahrens char *fsname, *snapname;
26279adfa60dSMatthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN];
262804bb726eSahl int rv;
262919b94df9SMatthew Ahrens zfs_handle_t *guid1hdl, *guid2hdl;
263019b94df9SMatthew Ahrens uint64_t create1, create2;
26313cb34c60Sahrens
26323cb34c60Sahrens if (guid2 == 0)
263304bb726eSahl return (0);
26343cb34c60Sahrens if (guid1 == 0)
263504bb726eSahl return (1);
26363cb34c60Sahrens
26373cb34c60Sahrens nvfs = fsavl_find(avl, guid1, &snapname);
26383cb34c60Sahrens VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
26393cb34c60Sahrens (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
264019b94df9SMatthew Ahrens guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
264119b94df9SMatthew Ahrens if (guid1hdl == NULL)
264204bb726eSahl return (-1);
26433cb34c60Sahrens
26443cb34c60Sahrens nvfs = fsavl_find(avl, guid2, &snapname);
26453cb34c60Sahrens VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
26463cb34c60Sahrens (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
264719b94df9SMatthew Ahrens guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
264819b94df9SMatthew Ahrens if (guid2hdl == NULL) {
264919b94df9SMatthew Ahrens zfs_close(guid1hdl);
265004bb726eSahl return (-1);
265104bb726eSahl }
26523cb34c60Sahrens
265319b94df9SMatthew Ahrens create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
265419b94df9SMatthew Ahrens create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
26553cb34c60Sahrens
265619b94df9SMatthew Ahrens if (create1 < create2)
265719b94df9SMatthew Ahrens rv = -1;
265819b94df9SMatthew Ahrens else if (create1 > create2)
265919b94df9SMatthew Ahrens rv = +1;
266019b94df9SMatthew Ahrens else
266119b94df9SMatthew Ahrens rv = 0;
266219b94df9SMatthew Ahrens
266319b94df9SMatthew Ahrens zfs_close(guid1hdl);
266419b94df9SMatthew Ahrens zfs_close(guid2hdl);
26653cb34c60Sahrens
26663cb34c60Sahrens return (rv);
26673cb34c60Sahrens }
26683cb34c60Sahrens
2669eb633035STom Caputi /*
2670eb633035STom Caputi * This function reestablishes the heirarchy of encryption roots after a
2671eb633035STom Caputi * recursive incremental receive has completed. This must be done after the
2672eb633035STom Caputi * second call to recv_incremental_replication() has renamed and promoted all
2673eb633035STom Caputi * sent datasets to their final locations in the dataset heriarchy.
2674eb633035STom Caputi */
2675eb633035STom Caputi /* ARGSUSED */
2676eb633035STom Caputi static int
recv_fix_encryption_hierarchy(libzfs_handle_t * hdl,const char * destname,nvlist_t * stream_nv,avl_tree_t * stream_avl)2677eb633035STom Caputi recv_fix_encryption_hierarchy(libzfs_handle_t *hdl, const char *destname,
2678eb633035STom Caputi nvlist_t *stream_nv, avl_tree_t *stream_avl)
2679eb633035STom Caputi {
2680eb633035STom Caputi int err;
2681eb633035STom Caputi nvpair_t *fselem = NULL;
2682eb633035STom Caputi nvlist_t *stream_fss;
2683eb633035STom Caputi char *cp;
2684eb633035STom Caputi char top_zfs[ZFS_MAX_DATASET_NAME_LEN];
2685eb633035STom Caputi
2686eb633035STom Caputi (void) strcpy(top_zfs, destname);
2687eb633035STom Caputi cp = strrchr(top_zfs, '@');
2688eb633035STom Caputi if (cp != NULL)
2689eb633035STom Caputi *cp = '\0';
2690eb633035STom Caputi
2691eb633035STom Caputi VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss", &stream_fss));
2692eb633035STom Caputi
2693eb633035STom Caputi while ((fselem = nvlist_next_nvpair(stream_fss, fselem)) != NULL) {
2694eb633035STom Caputi zfs_handle_t *zhp = NULL;
2695eb633035STom Caputi uint64_t crypt;
2696eb633035STom Caputi nvlist_t *snaps, *props, *stream_nvfs = NULL;
2697eb633035STom Caputi nvpair_t *snapel = NULL;
2698eb633035STom Caputi boolean_t is_encroot, is_clone, stream_encroot;
2699eb633035STom Caputi char *cp;
2700eb633035STom Caputi char *stream_keylocation = NULL;
2701eb633035STom Caputi char keylocation[MAXNAMELEN];
2702eb633035STom Caputi char fsname[ZFS_MAX_DATASET_NAME_LEN];
2703eb633035STom Caputi
2704eb633035STom Caputi keylocation[0] = '\0';
2705eb633035STom Caputi VERIFY(0 == nvpair_value_nvlist(fselem, &stream_nvfs));
2706eb633035STom Caputi VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "snaps", &snaps));
2707eb633035STom Caputi VERIFY(0 == nvlist_lookup_nvlist(stream_nvfs, "props", &props));
2708eb633035STom Caputi stream_encroot = nvlist_exists(stream_nvfs, "is_encroot");
2709eb633035STom Caputi
2710eb633035STom Caputi /* find a snapshot from the stream that exists locally */
2711eb633035STom Caputi err = ENOENT;
2712eb633035STom Caputi while ((snapel = nvlist_next_nvpair(snaps, snapel)) != NULL) {
2713eb633035STom Caputi uint64_t guid;
2714eb633035STom Caputi
2715eb633035STom Caputi VERIFY(0 == nvpair_value_uint64(snapel, &guid));
2716eb633035STom Caputi err = guid_to_name(hdl, destname, guid, B_FALSE,
2717eb633035STom Caputi fsname);
2718eb633035STom Caputi if (err == 0)
2719eb633035STom Caputi break;
2720eb633035STom Caputi }
2721eb633035STom Caputi
2722eb633035STom Caputi if (err != 0)
2723eb633035STom Caputi continue;
2724eb633035STom Caputi
2725eb633035STom Caputi cp = strchr(fsname, '@');
2726eb633035STom Caputi if (cp != NULL)
2727eb633035STom Caputi *cp = '\0';
2728eb633035STom Caputi
2729eb633035STom Caputi zhp = zfs_open(hdl, fsname, ZFS_TYPE_DATASET);
2730eb633035STom Caputi if (zhp == NULL) {
2731eb633035STom Caputi err = ENOENT;
2732eb633035STom Caputi goto error;
2733eb633035STom Caputi }
2734eb633035STom Caputi
2735eb633035STom Caputi crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
2736eb633035STom Caputi is_clone = zhp->zfs_dmustats.dds_origin[0] != '\0';
2737eb633035STom Caputi (void) zfs_crypto_get_encryption_root(zhp, &is_encroot, NULL);
2738eb633035STom Caputi
2739a60ca23dSTom Caputi /* we don't need to do anything for unencrypted datasets */
2740eb633035STom Caputi if (crypt == ZIO_CRYPT_OFF) {
2741eb633035STom Caputi zfs_close(zhp);
2742eb633035STom Caputi continue;
2743eb633035STom Caputi }
2744eb633035STom Caputi
2745eb633035STom Caputi /*
2746eb633035STom Caputi * If the dataset is flagged as an encryption root, was not
2747eb633035STom Caputi * received as a clone and is not currently an encryption root,
2748eb633035STom Caputi * force it to become one. Fixup the keylocation if necessary.
2749eb633035STom Caputi */
2750eb633035STom Caputi if (stream_encroot) {
2751eb633035STom Caputi if (!is_clone && !is_encroot) {
2752eb633035STom Caputi err = lzc_change_key(fsname,
2753eb633035STom Caputi DCP_CMD_FORCE_NEW_KEY, NULL, NULL, 0);
2754eb633035STom Caputi if (err != 0) {
2755eb633035STom Caputi zfs_close(zhp);
2756eb633035STom Caputi goto error;
2757eb633035STom Caputi }
2758eb633035STom Caputi }
2759eb633035STom Caputi
2760eb633035STom Caputi VERIFY(0 == nvlist_lookup_string(props,
2761eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2762eb633035STom Caputi &stream_keylocation));
2763eb633035STom Caputi
2764eb633035STom Caputi /*
2765eb633035STom Caputi * Refresh the properties in case the call to
2766eb633035STom Caputi * lzc_change_key() changed the value.
2767eb633035STom Caputi */
2768eb633035STom Caputi zfs_refresh_properties(zhp);
2769eb633035STom Caputi err = zfs_prop_get(zhp, ZFS_PROP_KEYLOCATION,
2770eb633035STom Caputi keylocation, sizeof (keylocation), NULL, NULL,
2771eb633035STom Caputi 0, B_TRUE);
2772eb633035STom Caputi if (err != 0) {
2773eb633035STom Caputi zfs_close(zhp);
2774eb633035STom Caputi goto error;
2775eb633035STom Caputi }
2776eb633035STom Caputi
2777eb633035STom Caputi if (strcmp(keylocation, stream_keylocation) != 0) {
2778eb633035STom Caputi err = zfs_prop_set(zhp,
2779eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION),
2780eb633035STom Caputi stream_keylocation);
2781eb633035STom Caputi if (err != 0) {
2782eb633035STom Caputi zfs_close(zhp);
2783eb633035STom Caputi goto error;
2784eb633035STom Caputi }
2785eb633035STom Caputi }
2786eb633035STom Caputi }
2787eb633035STom Caputi
2788eb633035STom Caputi /*
2789eb633035STom Caputi * If the dataset is not flagged as an encryption root and is
2790eb633035STom Caputi * currently an encryption root, force it to inherit from its
2791eb633035STom Caputi * parent. The root of a raw send should never be
2792eb633035STom Caputi * force-inherited.
2793eb633035STom Caputi */
2794eb633035STom Caputi if (!stream_encroot && is_encroot &&
2795eb633035STom Caputi strcmp(top_zfs, fsname) != 0) {
2796eb633035STom Caputi err = lzc_change_key(fsname, DCP_CMD_FORCE_INHERIT,
2797eb633035STom Caputi NULL, NULL, 0);
2798eb633035STom Caputi if (err != 0) {
2799eb633035STom Caputi zfs_close(zhp);
2800eb633035STom Caputi goto error;
2801eb633035STom Caputi }
2802eb633035STom Caputi }
2803eb633035STom Caputi
2804eb633035STom Caputi zfs_close(zhp);
2805eb633035STom Caputi }
2806eb633035STom Caputi
2807eb633035STom Caputi return (0);
2808eb633035STom Caputi
2809eb633035STom Caputi error:
2810eb633035STom Caputi return (err);
2811eb633035STom Caputi }
2812eb633035STom Caputi
28133cb34c60Sahrens static int
recv_incremental_replication(libzfs_handle_t * hdl,const char * tofs,recvflags_t * flags,nvlist_t * stream_nv,avl_tree_t * stream_avl,nvlist_t * renamed)28143cb34c60Sahrens recv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
281519b94df9SMatthew Ahrens recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
2816bd6a198fSTom Erickson nvlist_t *renamed)
28173cb34c60Sahrens {
28183cb34c60Sahrens nvlist_t *local_nv;
28193cb34c60Sahrens avl_tree_t *local_avl;
28203cb34c60Sahrens nvpair_t *fselem, *nextfselem;
2821bd6a198fSTom Erickson char *fromsnap;
28229adfa60dSMatthew Ahrens char newname[ZFS_MAX_DATASET_NAME_LEN];
28233cb34c60Sahrens int error;
282492241e0bSTom Erickson boolean_t needagain, progress, recursive;
2825fb2d63c6SPrabahar Jeyaram char *s1, *s2;
28263cb34c60Sahrens
28273cb34c60Sahrens VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
28283cb34c60Sahrens
282992241e0bSTom Erickson recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
283092241e0bSTom Erickson ENOENT);
283192241e0bSTom Erickson
283219b94df9SMatthew Ahrens if (flags->dryrun)
28333cb34c60Sahrens return (0);
28343cb34c60Sahrens
28353cb34c60Sahrens again:
28363cb34c60Sahrens needagain = progress = B_FALSE;
28373cb34c60Sahrens
28383cb34c60Sahrens if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
28396ccda740Sloli recursive, B_TRUE, B_FALSE,
28406ccda740Sloli B_FALSE, B_FALSE, B_TRUE, &local_nv, &local_avl)) != 0)
28413cb34c60Sahrens return (error);
28423cb34c60Sahrens
28433cb34c60Sahrens /*
28443cb34c60Sahrens * Process deletes and renames
28453cb34c60Sahrens */
28463cb34c60Sahrens for (fselem = nvlist_next_nvpair(local_nv, NULL);
28473cb34c60Sahrens fselem; fselem = nextfselem) {
28483cb34c60Sahrens nvlist_t *nvfs, *snaps;
28493cb34c60Sahrens nvlist_t *stream_nvfs = NULL;
28503cb34c60Sahrens nvpair_t *snapelem, *nextsnapelem;
28513cb34c60Sahrens uint64_t fromguid = 0;
28523cb34c60Sahrens uint64_t originguid = 0;
28533cb34c60Sahrens uint64_t stream_originguid = 0;
28543cb34c60Sahrens uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
28553cb34c60Sahrens char *fsname, *stream_fsname;
28563cb34c60Sahrens
28573cb34c60Sahrens nextfselem = nvlist_next_nvpair(local_nv, fselem);
28583cb34c60Sahrens
28593cb34c60Sahrens VERIFY(0 == nvpair_value_nvlist(fselem, &nvfs));
28603cb34c60Sahrens VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
28613cb34c60Sahrens VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
28623cb34c60Sahrens VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
28633cb34c60Sahrens &parent_fromsnap_guid));
28643cb34c60Sahrens (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
28653cb34c60Sahrens
28663cb34c60Sahrens /*
28673cb34c60Sahrens * First find the stream's fs, so we can check for
28683cb34c60Sahrens * a different origin (due to "zfs promote")
28693cb34c60Sahrens */
28703cb34c60Sahrens for (snapelem = nvlist_next_nvpair(snaps, NULL);
28713cb34c60Sahrens snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
28723cb34c60Sahrens uint64_t thisguid;
28733cb34c60Sahrens
28743cb34c60Sahrens VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
28753cb34c60Sahrens stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
28763cb34c60Sahrens
28773cb34c60Sahrens if (stream_nvfs != NULL)
28783cb34c60Sahrens break;
28793cb34c60Sahrens }
28803cb34c60Sahrens
28813cb34c60Sahrens /* check for promote */
28823cb34c60Sahrens (void) nvlist_lookup_uint64(stream_nvfs, "origin",
28833cb34c60Sahrens &stream_originguid);
28843cb34c60Sahrens if (stream_nvfs && originguid != stream_originguid) {
288504bb726eSahl switch (created_before(hdl, local_avl,
288604bb726eSahl stream_originguid, originguid)) {
288704bb726eSahl case 1: {
28883cb34c60Sahrens /* promote it! */
28893cb34c60Sahrens nvlist_t *origin_nvfs;
28903cb34c60Sahrens char *origin_fsname;
28913cb34c60Sahrens
28923cb34c60Sahrens origin_nvfs = fsavl_find(local_avl, originguid,
28933cb34c60Sahrens NULL);
28943cb34c60Sahrens VERIFY(0 == nvlist_lookup_string(origin_nvfs,
28953cb34c60Sahrens "name", &origin_fsname));
2896eb633035STom Caputi error = recv_promote(hdl, fsname, origin_fsname,
2897eb633035STom Caputi flags);
28983cb34c60Sahrens if (error == 0)
28993cb34c60Sahrens progress = B_TRUE;
290004bb726eSahl break;
290104bb726eSahl }
290204bb726eSahl default:
290304bb726eSahl break;
290404bb726eSahl case -1:
290504bb726eSahl fsavl_destroy(local_avl);
290604bb726eSahl nvlist_free(local_nv);
290704bb726eSahl return (-1);
29083cb34c60Sahrens }
29093cb34c60Sahrens /*
29103cb34c60Sahrens * We had/have the wrong origin, therefore our
29113cb34c60Sahrens * list of snapshots is wrong. Need to handle
29123cb34c60Sahrens * them on the next pass.
29133cb34c60Sahrens */
29143cb34c60Sahrens needagain = B_TRUE;
29153cb34c60Sahrens continue;
29163cb34c60Sahrens }
29173cb34c60Sahrens
29183cb34c60Sahrens for (snapelem = nvlist_next_nvpair(snaps, NULL);
29193cb34c60Sahrens snapelem; snapelem = nextsnapelem) {
29203cb34c60Sahrens uint64_t thisguid;
29213cb34c60Sahrens char *stream_snapname;
2922bb0ade09Sahrens nvlist_t *found, *props;
29233cb34c60Sahrens
29243cb34c60Sahrens nextsnapelem = nvlist_next_nvpair(snaps, snapelem);
29253cb34c60Sahrens
29263cb34c60Sahrens VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
29273cb34c60Sahrens found = fsavl_find(stream_avl, thisguid,
29283cb34c60Sahrens &stream_snapname);
29293cb34c60Sahrens
29303cb34c60Sahrens /* check for delete */
29313cb34c60Sahrens if (found == NULL) {
29329adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
29333cb34c60Sahrens
293419b94df9SMatthew Ahrens if (!flags->force)
29353cb34c60Sahrens continue;
29363cb34c60Sahrens
29373cb34c60Sahrens (void) snprintf(name, sizeof (name), "%s@%s",
29383cb34c60Sahrens fsname, nvpair_name(snapelem));
29393cb34c60Sahrens
29403cb34c60Sahrens error = recv_destroy(hdl, name,
29413cb34c60Sahrens strlen(fsname)+1, newname, flags);
29423cb34c60Sahrens if (error)
29433cb34c60Sahrens needagain = B_TRUE;
29443cb34c60Sahrens else
29453cb34c60Sahrens progress = B_TRUE;
29463cb34c60Sahrens continue;
29473cb34c60Sahrens }
29483cb34c60Sahrens
29493cb34c60Sahrens stream_nvfs = found;
29503cb34c60Sahrens
2951bb0ade09Sahrens if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
2952bb0ade09Sahrens &props) && 0 == nvlist_lookup_nvlist(props,
2953bb0ade09Sahrens stream_snapname, &props)) {
2954bb0ade09Sahrens zfs_cmd_t zc = { 0 };
2955bb0ade09Sahrens
295692241e0bSTom Erickson zc.zc_cookie = B_TRUE; /* received */
2957b6c10d80Sahrens (void) snprintf(zc.zc_name, sizeof (zc.zc_name),
2958bb0ade09Sahrens "%s@%s", fsname, nvpair_name(snapelem));
2959bb0ade09Sahrens if (zcmd_write_src_nvlist(hdl, &zc,
2960bb0ade09Sahrens props) == 0) {
2961bb0ade09Sahrens (void) zfs_ioctl(hdl,
2962bb0ade09Sahrens ZFS_IOC_SET_PROP, &zc);
2963bb0ade09Sahrens zcmd_free_nvlists(&zc);
2964bb0ade09Sahrens }
2965bb0ade09Sahrens }
2966bb0ade09Sahrens
29673cb34c60Sahrens /* check for different snapname */
29683cb34c60Sahrens if (strcmp(nvpair_name(snapelem),
29693cb34c60Sahrens stream_snapname) != 0) {
29709adfa60dSMatthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
29719adfa60dSMatthew Ahrens char tryname[ZFS_MAX_DATASET_NAME_LEN];
29723cb34c60Sahrens
29733cb34c60Sahrens (void) snprintf(name, sizeof (name), "%s@%s",
29743cb34c60Sahrens fsname, nvpair_name(snapelem));
29753cb34c60Sahrens (void) snprintf(tryname, sizeof (name), "%s@%s",
29763cb34c60Sahrens fsname, stream_snapname);
29773cb34c60Sahrens
29783cb34c60Sahrens error = recv_rename(hdl, name, tryname,
29793cb34c60Sahrens strlen(fsname)+1, newname, flags);
29803cb34c60Sahrens if (error)
29813cb34c60Sahrens needagain = B_TRUE;
29823cb34c60Sahrens else
29833cb34c60Sahrens progress = B_TRUE;
29843cb34c60Sahrens }
29853cb34c60Sahrens
29863cb34c60Sahrens if (strcmp(stream_snapname, fromsnap) == 0)
29873cb34c60Sahrens fromguid = thisguid;
29883cb34c60Sahrens }
29893cb34c60Sahrens
29903cb34c60Sahrens /* check for delete */
29913cb34c60Sahrens if (stream_nvfs == NULL) {
299219b94df9SMatthew Ahrens if (!flags->force)
29933cb34c60Sahrens continue;
29943cb34c60Sahrens
29953cb34c60Sahrens error = recv_destroy(hdl, fsname, strlen(tofs)+1,
29963cb34c60Sahrens newname, flags);
29973cb34c60Sahrens if (error)
29983cb34c60Sahrens needagain = B_TRUE;
29993cb34c60Sahrens else
30003cb34c60Sahrens progress = B_TRUE;
30013cb34c60Sahrens continue;
30023cb34c60Sahrens }
30033cb34c60Sahrens
3004bd6a198fSTom Erickson if (fromguid == 0) {
300519b94df9SMatthew Ahrens if (flags->verbose) {
3006bd6a198fSTom Erickson (void) printf("local fs %s does not have "
3007bd6a198fSTom Erickson "fromsnap (%s in stream); must have "
3008bd6a198fSTom Erickson "been deleted locally; ignoring\n",
3009bd6a198fSTom Erickson fsname, fromsnap);
3010bd6a198fSTom Erickson }
30113cb34c60Sahrens continue;
30123cb34c60Sahrens }
30133cb34c60Sahrens
30143cb34c60Sahrens VERIFY(0 == nvlist_lookup_string(stream_nvfs,
30153cb34c60Sahrens "name", &stream_fsname));
30163cb34c60Sahrens VERIFY(0 == nvlist_lookup_uint64(stream_nvfs,
30173cb34c60Sahrens "parentfromsnap", &stream_parent_fromsnap_guid));
30183cb34c60Sahrens
301994f5c1a2SPrabahar Jeyaram s1 = strrchr(fsname, '/');
302094f5c1a2SPrabahar Jeyaram s2 = strrchr(stream_fsname, '/');
302194f5c1a2SPrabahar Jeyaram
3022bd6a198fSTom Erickson /*
3023bd6a198fSTom Erickson * Check for rename. If the exact receive path is specified, it
3024bd6a198fSTom Erickson * does not count as a rename, but we still need to check the
3025bd6a198fSTom Erickson * datasets beneath it.
3026bd6a198fSTom Erickson */
30273cb34c60Sahrens if ((stream_parent_fromsnap_guid != 0 &&
3028bd6a198fSTom Erickson parent_fromsnap_guid != 0 &&
30293cb34c60Sahrens stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
303019b94df9SMatthew Ahrens ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
3031bd6a198fSTom Erickson (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
30323cb34c60Sahrens nvlist_t *parent;
30339adfa60dSMatthew Ahrens char tryname[ZFS_MAX_DATASET_NAME_LEN];
30343cb34c60Sahrens
30353cb34c60Sahrens parent = fsavl_find(local_avl,
30363cb34c60Sahrens stream_parent_fromsnap_guid, NULL);
30373cb34c60Sahrens /*
30383cb34c60Sahrens * NB: parent might not be found if we used the
30393cb34c60Sahrens * tosnap for stream_parent_fromsnap_guid,
30403cb34c60Sahrens * because the parent is a newly-created fs;
30413cb34c60Sahrens * we'll be able to rename it after we recv the
30423cb34c60Sahrens * new fs.
30433cb34c60Sahrens */
30443cb34c60Sahrens if (parent != NULL) {
30453cb34c60Sahrens char *pname;
30463cb34c60Sahrens
30473cb34c60Sahrens VERIFY(0 == nvlist_lookup_string(parent, "name",
30483cb34c60Sahrens &pname));
30493cb34c60Sahrens (void) snprintf(tryname, sizeof (tryname),
30503cb34c60Sahrens "%s%s", pname, strrchr(stream_fsname, '/'));
30513cb34c60Sahrens } else {
30523cb34c60Sahrens tryname[0] = '\0';
305319b94df9SMatthew Ahrens if (flags->verbose) {
30543cb34c60Sahrens (void) printf("local fs %s new parent "
30553cb34c60Sahrens "not found\n", fsname);
30563cb34c60Sahrens }
30573cb34c60Sahrens }
30583cb34c60Sahrens
3059bd6a198fSTom Erickson newname[0] = '\0';
3060bd6a198fSTom Erickson
30613cb34c60Sahrens error = recv_rename(hdl, fsname, tryname,
30623cb34c60Sahrens strlen(tofs)+1, newname, flags);
3063bd6a198fSTom Erickson
3064bd6a198fSTom Erickson if (renamed != NULL && newname[0] != '\0') {
3065bd6a198fSTom Erickson VERIFY(0 == nvlist_add_boolean(renamed,
3066bd6a198fSTom Erickson newname));
3067bd6a198fSTom Erickson }
3068bd6a198fSTom Erickson
30693cb34c60Sahrens if (error)
30703cb34c60Sahrens needagain = B_TRUE;
30713cb34c60Sahrens else
30723cb34c60Sahrens progress = B_TRUE;
30733cb34c60Sahrens }
30743cb34c60Sahrens }
30753cb34c60Sahrens
30763cb34c60Sahrens fsavl_destroy(local_avl);
30773cb34c60Sahrens nvlist_free(local_nv);
30783cb34c60Sahrens
30793cb34c60Sahrens if (needagain && progress) {
30803cb34c60Sahrens /* do another pass to fix up temporary names */
308119b94df9SMatthew Ahrens if (flags->verbose)
30823cb34c60Sahrens (void) printf("another pass:\n");
30833cb34c60Sahrens goto again;
30843cb34c60Sahrens }
30853cb34c60Sahrens
3086eb633035STom Caputi return (needagain || error != 0);
30873cb34c60Sahrens }
30883cb34c60Sahrens
30893cb34c60Sahrens static int
zfs_receive_package(libzfs_handle_t * hdl,int fd,const char * destname,recvflags_t * flags,dmu_replay_record_t * drr,zio_cksum_t * zc,char ** top_zfs,int cleanup_fd,uint64_t * action_handlep,nvlist_t * cmdprops)30903cb34c60Sahrens zfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
309119b94df9SMatthew Ahrens recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
3092a60ca23dSTom Caputi char **top_zfs, int cleanup_fd, uint64_t *action_handlep,
3093a60ca23dSTom Caputi nvlist_t *cmdprops)
30943cb34c60Sahrens {
30953cb34c60Sahrens nvlist_t *stream_nv = NULL;
30963cb34c60Sahrens avl_tree_t *stream_avl = NULL;
30973cb34c60Sahrens char *fromsnap = NULL;
30985878fad7SDan McDonald char *sendsnap = NULL;
3099bd6a198fSTom Erickson char *cp;
31009adfa60dSMatthew Ahrens char tofs[ZFS_MAX_DATASET_NAME_LEN];
31019adfa60dSMatthew Ahrens char sendfs[ZFS_MAX_DATASET_NAME_LEN];
31023cb34c60Sahrens char errbuf[1024];
31033cb34c60Sahrens dmu_replay_record_t drre;
31043cb34c60Sahrens int error;
31053cb34c60Sahrens boolean_t anyerr = B_FALSE;
3106137fa067Sahrens boolean_t softerr = B_FALSE;
3107eb633035STom Caputi boolean_t recursive, raw;
31083cb34c60Sahrens
31093cb34c60Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
31103cb34c60Sahrens "cannot receive"));
31113cb34c60Sahrens
31123cb34c60Sahrens assert(drr->drr_type == DRR_BEGIN);
31133cb34c60Sahrens assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
31149e69d7d0SLori Alt assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
31159e69d7d0SLori Alt DMU_COMPOUNDSTREAM);
31163cb34c60Sahrens
31173cb34c60Sahrens /*
31183cb34c60Sahrens * Read in the nvlist from the stream.
31193cb34c60Sahrens */
31203cb34c60Sahrens if (drr->drr_payloadlen != 0) {
31213cb34c60Sahrens error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
312219b94df9SMatthew Ahrens &stream_nv, flags->byteswap, zc);
31233cb34c60Sahrens if (error) {
31243cb34c60Sahrens error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
31253cb34c60Sahrens goto out;
31263cb34c60Sahrens }
31273cb34c60Sahrens }
31283cb34c60Sahrens
3129bd6a198fSTom Erickson recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3130bd6a198fSTom Erickson ENOENT);
3131eb633035STom Caputi raw = (nvlist_lookup_boolean(stream_nv, "raw") == 0);
3132bd6a198fSTom Erickson
3133bd6a198fSTom Erickson if (recursive && strchr(destname, '@')) {
3134bd6a198fSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3135bd6a198fSTom Erickson "cannot specify snapshot name for multi-snapshot stream"));
3136bd6a198fSTom Erickson error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3137bd6a198fSTom Erickson goto out;
3138bd6a198fSTom Erickson }
3139bd6a198fSTom Erickson
31403cb34c60Sahrens /*
31413cb34c60Sahrens * Read in the end record and verify checksum.
31423cb34c60Sahrens */
31433cb34c60Sahrens if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
314419b94df9SMatthew Ahrens flags->byteswap, NULL)))
31453cb34c60Sahrens goto out;
314619b94df9SMatthew Ahrens if (flags->byteswap) {
31473cb34c60Sahrens drre.drr_type = BSWAP_32(drre.drr_type);
31483cb34c60Sahrens drre.drr_u.drr_end.drr_checksum.zc_word[0] =
31493cb34c60Sahrens BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
31503cb34c60Sahrens drre.drr_u.drr_end.drr_checksum.zc_word[1] =
31513cb34c60Sahrens BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
31523cb34c60Sahrens drre.drr_u.drr_end.drr_checksum.zc_word[2] =
31533cb34c60Sahrens BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
31543cb34c60Sahrens drre.drr_u.drr_end.drr_checksum.zc_word[3] =
31553cb34c60Sahrens BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
31563cb34c60Sahrens }
31573cb34c60Sahrens if (drre.drr_type != DRR_END) {
31583cb34c60Sahrens error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
31593cb34c60Sahrens goto out;
31603cb34c60Sahrens }
31613cb34c60Sahrens if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
31623cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
31633cb34c60Sahrens "incorrect header checksum"));
31643cb34c60Sahrens error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
31653cb34c60Sahrens goto out;
31663cb34c60Sahrens }
31673cb34c60Sahrens
31683cb34c60Sahrens (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
31693cb34c60Sahrens
31703cb34c60Sahrens if (drr->drr_payloadlen != 0) {
31713cb34c60Sahrens nvlist_t *stream_fss;
31723cb34c60Sahrens
31733cb34c60Sahrens VERIFY(0 == nvlist_lookup_nvlist(stream_nv, "fss",
31743cb34c60Sahrens &stream_fss));
317500954d7bSahl if ((stream_avl = fsavl_create(stream_fss)) == NULL) {
317600954d7bSahl zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
317700954d7bSahl "couldn't allocate avl tree"));
317800954d7bSahl error = zfs_error(hdl, EZFS_NOMEM, errbuf);
317900954d7bSahl goto out;
318000954d7bSahl }
31813cb34c60Sahrens
3182471a88e4SAndriy Gapon if (fromsnap != NULL && recursive) {
3183bd6a198fSTom Erickson nvlist_t *renamed = NULL;
3184bd6a198fSTom Erickson nvpair_t *pair = NULL;
3185bd6a198fSTom Erickson
31869adfa60dSMatthew Ahrens (void) strlcpy(tofs, destname, sizeof (tofs));
318719b94df9SMatthew Ahrens if (flags->isprefix) {
3188bd6a198fSTom Erickson struct drr_begin *drrb = &drr->drr_u.drr_begin;
3189bd6a198fSTom Erickson int i;
3190bd6a198fSTom Erickson
319119b94df9SMatthew Ahrens if (flags->istail) {
3192bd6a198fSTom Erickson cp = strrchr(drrb->drr_toname, '/');
3193bd6a198fSTom Erickson if (cp == NULL) {
3194bd6a198fSTom Erickson (void) strlcat(tofs, "/",
31959adfa60dSMatthew Ahrens sizeof (tofs));
3196bd6a198fSTom Erickson i = 0;
3197bd6a198fSTom Erickson } else {
3198bd6a198fSTom Erickson i = (cp - drrb->drr_toname);
3199bd6a198fSTom Erickson }
3200bd6a198fSTom Erickson } else {
3201bd6a198fSTom Erickson i = strcspn(drrb->drr_toname, "/@");
3202bd6a198fSTom Erickson }
32033cb34c60Sahrens /* zfs_receive_one() will create_parents() */
3204bd6a198fSTom Erickson (void) strlcat(tofs, &drrb->drr_toname[i],
32059adfa60dSMatthew Ahrens sizeof (tofs));
32063cb34c60Sahrens *strchr(tofs, '@') = '\0';
32073cb34c60Sahrens }
3208bd6a198fSTom Erickson
3209471a88e4SAndriy Gapon if (!flags->dryrun && !flags->nomount) {
3210bd6a198fSTom Erickson VERIFY(0 == nvlist_alloc(&renamed,
3211bd6a198fSTom Erickson NV_UNIQUE_NAME, 0));
3212bd6a198fSTom Erickson }
3213bd6a198fSTom Erickson
3214bd6a198fSTom Erickson softerr = recv_incremental_replication(hdl, tofs, flags,
3215bd6a198fSTom Erickson stream_nv, stream_avl, renamed);
3216bd6a198fSTom Erickson
3217bd6a198fSTom Erickson /* Unmount renamed filesystems before receiving. */
3218bd6a198fSTom Erickson while ((pair = nvlist_next_nvpair(renamed,
3219bd6a198fSTom Erickson pair)) != NULL) {
3220bd6a198fSTom Erickson zfs_handle_t *zhp;
3221bd6a198fSTom Erickson prop_changelist_t *clp = NULL;
3222bd6a198fSTom Erickson
3223bd6a198fSTom Erickson zhp = zfs_open(hdl, nvpair_name(pair),
3224bd6a198fSTom Erickson ZFS_TYPE_FILESYSTEM);
3225bd6a198fSTom Erickson if (zhp != NULL) {
3226bd6a198fSTom Erickson clp = changelist_gather(zhp,
3227bd6a198fSTom Erickson ZFS_PROP_MOUNTPOINT, 0, 0);
3228bd6a198fSTom Erickson zfs_close(zhp);
3229bd6a198fSTom Erickson if (clp != NULL) {
3230bd6a198fSTom Erickson softerr |=
3231bd6a198fSTom Erickson changelist_prefix(clp);
3232bd6a198fSTom Erickson changelist_free(clp);
3233bd6a198fSTom Erickson }
3234bd6a198fSTom Erickson }
3235bd6a198fSTom Erickson }
3236bd6a198fSTom Erickson
3237bd6a198fSTom Erickson nvlist_free(renamed);
32383cb34c60Sahrens }
32393cb34c60Sahrens }
32403cb34c60Sahrens
3241bd6a198fSTom Erickson /*
3242bd6a198fSTom Erickson * Get the fs specified by the first path in the stream (the top level
3243bd6a198fSTom Erickson * specified by 'zfs send') and pass it to each invocation of
3244bd6a198fSTom Erickson * zfs_receive_one().
3245bd6a198fSTom Erickson */
3246bd6a198fSTom Erickson (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
32479adfa60dSMatthew Ahrens sizeof (sendfs));
32485878fad7SDan McDonald if ((cp = strchr(sendfs, '@')) != NULL) {
3249bd6a198fSTom Erickson *cp = '\0';
32505878fad7SDan McDonald /*
32515878fad7SDan McDonald * Find the "sendsnap", the final snapshot in a replication
32525878fad7SDan McDonald * stream. zfs_receive_one() handles certain errors
32535878fad7SDan McDonald * differently, depending on if the contained stream is the
32545878fad7SDan McDonald * last one or not.
32555878fad7SDan McDonald */
32565878fad7SDan McDonald sendsnap = (cp + 1);
32575878fad7SDan McDonald }
32583cb34c60Sahrens
32593cb34c60Sahrens /* Finally, receive each contained stream */
32603cb34c60Sahrens do {
32613cb34c60Sahrens /*
32623cb34c60Sahrens * we should figure out if it has a recoverable
32633cb34c60Sahrens * error, in which case do a recv_skip() and drive on.
32643cb34c60Sahrens * Note, if we fail due to already having this guid,
32653cb34c60Sahrens * zfs_receive_one() will take care of it (ie,
32663cb34c60Sahrens * recv_skip() and return 0).
32673cb34c60Sahrens */
3268a2cdcdd2SPaul Dagnelie error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
3269c99e4bdcSChris Kirby sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
3270a60ca23dSTom Caputi action_handlep, sendsnap, cmdprops);
32713cb34c60Sahrens if (error == ENODATA) {
32723cb34c60Sahrens error = 0;
32733cb34c60Sahrens break;
32743cb34c60Sahrens }
32753cb34c60Sahrens anyerr |= error;
32763cb34c60Sahrens } while (error == 0);
32773cb34c60Sahrens
3278471a88e4SAndriy Gapon if (drr->drr_payloadlen != 0 && recursive && fromsnap != NULL) {
32793cb34c60Sahrens /*
32803cb34c60Sahrens * Now that we have the fs's they sent us, try the
32813cb34c60Sahrens * renames again.
32823cb34c60Sahrens */
3283137fa067Sahrens softerr = recv_incremental_replication(hdl, tofs, flags,
3284bd6a198fSTom Erickson stream_nv, stream_avl, NULL);
32853cb34c60Sahrens }
32863cb34c60Sahrens
3287eb633035STom Caputi if (raw && softerr == 0) {
3288eb633035STom Caputi softerr = recv_fix_encryption_hierarchy(hdl, destname,
3289eb633035STom Caputi stream_nv, stream_avl);
3290eb633035STom Caputi }
3291eb633035STom Caputi
32923cb34c60Sahrens out:
32933cb34c60Sahrens fsavl_destroy(stream_avl);
3294aab83bb8SJosef 'Jeff' Sipek nvlist_free(stream_nv);
3295137fa067Sahrens if (softerr)
3296137fa067Sahrens error = -2;
32973cb34c60Sahrens if (anyerr)
32983cb34c60Sahrens error = -1;
32993cb34c60Sahrens return (error);
33003cb34c60Sahrens }
33013cb34c60Sahrens
330292241e0bSTom Erickson static void
trunc_prop_errs(int truncated)330392241e0bSTom Erickson trunc_prop_errs(int truncated)
330492241e0bSTom Erickson {
330592241e0bSTom Erickson ASSERT(truncated != 0);
330692241e0bSTom Erickson
330792241e0bSTom Erickson if (truncated == 1)
330892241e0bSTom Erickson (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
330992241e0bSTom Erickson "1 more property could not be set\n"));
331092241e0bSTom Erickson else
331192241e0bSTom Erickson (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
331292241e0bSTom Erickson "%d more properties could not be set\n"), truncated);
331392241e0bSTom Erickson }
331492241e0bSTom Erickson
33153cb34c60Sahrens static int
recv_skip(libzfs_handle_t * hdl,int fd,boolean_t byteswap)33163cb34c60Sahrens recv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
33173cb34c60Sahrens {
33183cb34c60Sahrens dmu_replay_record_t *drr;
3319b5152584SMatthew Ahrens void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
33209e69d7d0SLori Alt char errbuf[1024];
33219e69d7d0SLori Alt
33229e69d7d0SLori Alt (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
33239e69d7d0SLori Alt "cannot receive:"));
33243cb34c60Sahrens
33253cb34c60Sahrens /* XXX would be great to use lseek if possible... */
33263cb34c60Sahrens drr = buf;
33273cb34c60Sahrens
33283cb34c60Sahrens while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
33293cb34c60Sahrens byteswap, NULL) == 0) {
33303cb34c60Sahrens if (byteswap)
33313cb34c60Sahrens drr->drr_type = BSWAP_32(drr->drr_type);
33323cb34c60Sahrens
33333cb34c60Sahrens switch (drr->drr_type) {
33343cb34c60Sahrens case DRR_BEGIN:
33359e69d7d0SLori Alt if (drr->drr_payloadlen != 0) {
33369c3fd121SMatthew Ahrens (void) recv_read(hdl, fd, buf,
33379c3fd121SMatthew Ahrens drr->drr_payloadlen, B_FALSE, NULL);
33389e69d7d0SLori Alt }
33393cb34c60Sahrens break;
33403cb34c60Sahrens
33413cb34c60Sahrens case DRR_END:
33423cb34c60Sahrens free(buf);
33433cb34c60Sahrens return (0);
33443cb34c60Sahrens
33453cb34c60Sahrens case DRR_OBJECT:
33463cb34c60Sahrens if (byteswap) {
33473cb34c60Sahrens drr->drr_u.drr_object.drr_bonuslen =
33483cb34c60Sahrens BSWAP_32(drr->drr_u.drr_object.
33493cb34c60Sahrens drr_bonuslen);
33503cb34c60Sahrens }
33513cb34c60Sahrens (void) recv_read(hdl, fd, buf,
33523cb34c60Sahrens P2ROUNDUP(drr->drr_u.drr_object.drr_bonuslen, 8),
33533cb34c60Sahrens B_FALSE, NULL);
33543cb34c60Sahrens break;
33553cb34c60Sahrens
33563cb34c60Sahrens case DRR_WRITE:
33573cb34c60Sahrens if (byteswap) {
33585602294fSDan Kimmel drr->drr_u.drr_write.drr_logical_size =
33595602294fSDan Kimmel BSWAP_64(
33605602294fSDan Kimmel drr->drr_u.drr_write.drr_logical_size);
33615602294fSDan Kimmel drr->drr_u.drr_write.drr_compressed_size =
33625602294fSDan Kimmel BSWAP_64(
33635602294fSDan Kimmel drr->drr_u.drr_write.drr_compressed_size);
33643cb34c60Sahrens }
33655602294fSDan Kimmel uint64_t payload_size =
33665602294fSDan Kimmel DRR_WRITE_PAYLOAD_SIZE(&drr->drr_u.drr_write);
3367c5286370SAllan Jude assert(payload_size <= SPA_MAXBLOCKSIZE);
33683cb34c60Sahrens (void) recv_read(hdl, fd, buf,
33695602294fSDan Kimmel payload_size, B_FALSE, NULL);
33703cb34c60Sahrens break;
33710a586ceaSMark Shellenbaum case DRR_SPILL:
33720a586ceaSMark Shellenbaum if (byteswap) {
337320fea7a4SDan Kimmel drr->drr_u.drr_spill.drr_length =
33740a586ceaSMark Shellenbaum BSWAP_64(drr->drr_u.drr_spill.drr_length);
33750a586ceaSMark Shellenbaum }
33760a586ceaSMark Shellenbaum (void) recv_read(hdl, fd, buf,
33770a586ceaSMark Shellenbaum drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
33780a586ceaSMark Shellenbaum break;
33795d7b4d43SMatthew Ahrens case DRR_WRITE_EMBEDDED:
33805d7b4d43SMatthew Ahrens if (byteswap) {
33815d7b4d43SMatthew Ahrens drr->drr_u.drr_write_embedded.drr_psize =
33825d7b4d43SMatthew Ahrens BSWAP_32(drr->drr_u.drr_write_embedded.
33835d7b4d43SMatthew Ahrens drr_psize);
33845d7b4d43SMatthew Ahrens }
33855d7b4d43SMatthew Ahrens (void) recv_read(hdl, fd, buf,
33865d7b4d43SMatthew Ahrens P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
33875d7b4d43SMatthew Ahrens 8), B_FALSE, NULL);
33885d7b4d43SMatthew Ahrens break;
33899e69d7d0SLori Alt case DRR_WRITE_BYREF:
33903cb34c60Sahrens case DRR_FREEOBJECTS:
33913cb34c60Sahrens case DRR_FREE:
33923cb34c60Sahrens break;
33933cb34c60Sahrens
33943cb34c60Sahrens default:
33959e69d7d0SLori Alt zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
33969e69d7d0SLori Alt "invalid record type"));
33979e69d7d0SLori Alt return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
33983cb34c60Sahrens }
33993cb34c60Sahrens }
34003cb34c60Sahrens
34013cb34c60Sahrens free(buf);
34023cb34c60Sahrens return (-1);
34033cb34c60Sahrens }
34043cb34c60Sahrens
34059c3fd121SMatthew Ahrens static void
recv_ecksum_set_aux(libzfs_handle_t * hdl,const char * target_snap,boolean_t resumable)34069c3fd121SMatthew Ahrens recv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
34079c3fd121SMatthew Ahrens boolean_t resumable)
34089c3fd121SMatthew Ahrens {
34099adfa60dSMatthew Ahrens char target_fs[ZFS_MAX_DATASET_NAME_LEN];
34109c3fd121SMatthew Ahrens
34119c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34129c3fd121SMatthew Ahrens "checksum mismatch or incomplete stream"));
34139c3fd121SMatthew Ahrens
34149c3fd121SMatthew Ahrens if (!resumable)
34159c3fd121SMatthew Ahrens return;
34169c3fd121SMatthew Ahrens (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
34179c3fd121SMatthew Ahrens *strchr(target_fs, '@') = '\0';
34189c3fd121SMatthew Ahrens zfs_handle_t *zhp = zfs_open(hdl, target_fs,
34199c3fd121SMatthew Ahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
34209c3fd121SMatthew Ahrens if (zhp == NULL)
34219c3fd121SMatthew Ahrens return;
34229c3fd121SMatthew Ahrens
34239c3fd121SMatthew Ahrens char token_buf[ZFS_MAXPROPLEN];
34249c3fd121SMatthew Ahrens int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
34259c3fd121SMatthew Ahrens token_buf, sizeof (token_buf),
34269c3fd121SMatthew Ahrens NULL, NULL, 0, B_TRUE);
34279c3fd121SMatthew Ahrens if (error == 0) {
34289c3fd121SMatthew Ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
34299c3fd121SMatthew Ahrens "checksum mismatch or incomplete stream.\n"
34309c3fd121SMatthew Ahrens "Partially received snapshot is saved.\n"
34319c3fd121SMatthew Ahrens "A resuming stream can be generated on the sending "
34329c3fd121SMatthew Ahrens "system by running:\n"
34339c3fd121SMatthew Ahrens " zfs send -t %s"),
34349c3fd121SMatthew Ahrens token_buf);
34359c3fd121SMatthew Ahrens }
34369c3fd121SMatthew Ahrens zfs_close(zhp);
34379c3fd121SMatthew Ahrens }
34389c3fd121SMatthew Ahrens
34396ccda740Sloli /*
34406ccda740Sloli * Prepare a new nvlist of properties that are to override (-o) or be excluded
34416ccda740Sloli * (-x) from the received dataset
34426ccda740Sloli * recvprops: received properties from the send stream
34436ccda740Sloli * cmdprops: raw input properties from command line
34446ccda740Sloli * origprops: properties, both locally-set and received, currently set on the
34456ccda740Sloli * target dataset if it exists, NULL otherwise.
34466ccda740Sloli * oxprops: valid output override (-o) and excluded (-x) properties
34476ccda740Sloli */
34486ccda740Sloli static int
zfs_setup_cmdline_props(libzfs_handle_t * hdl,zfs_type_t type,char * fsname,boolean_t zoned,boolean_t recursive,boolean_t newfs,boolean_t raw,boolean_t toplevel,nvlist_t * recvprops,nvlist_t * cmdprops,nvlist_t * origprops,nvlist_t ** oxprops,uint8_t ** wkeydata_out,uint_t * wkeylen_out,const char * errbuf)34496ccda740Sloli zfs_setup_cmdline_props(libzfs_handle_t *hdl, zfs_type_t type,
34506ccda740Sloli char *fsname, boolean_t zoned, boolean_t recursive, boolean_t newfs,
34516ccda740Sloli boolean_t raw, boolean_t toplevel, nvlist_t *recvprops, nvlist_t *cmdprops,
34526ccda740Sloli nvlist_t *origprops, nvlist_t **oxprops, uint8_t **wkeydata_out,
34536ccda740Sloli uint_t *wkeylen_out, const char *errbuf)
34546ccda740Sloli {
34556ccda740Sloli nvpair_t *nvp;
34566ccda740Sloli nvlist_t *oprops, *voprops;
34576ccda740Sloli zfs_handle_t *zhp = NULL;
34586ccda740Sloli zpool_handle_t *zpool_hdl = NULL;
34596ccda740Sloli char *cp;
34606ccda740Sloli int ret = 0;
34616ccda740Sloli char namebuf[ZFS_MAX_DATASET_NAME_LEN];
34626ccda740Sloli
34636ccda740Sloli if (nvlist_empty(cmdprops))
34646ccda740Sloli return (0); /* No properties to override or exclude */
34656ccda740Sloli
34666ccda740Sloli *oxprops = fnvlist_alloc();
34676ccda740Sloli oprops = fnvlist_alloc();
34686ccda740Sloli
34696ccda740Sloli strlcpy(namebuf, fsname, ZFS_MAX_DATASET_NAME_LEN);
34706ccda740Sloli
34716ccda740Sloli /*
34726ccda740Sloli * Get our dataset handle. The target dataset may not exist yet.
34736ccda740Sloli */
34746ccda740Sloli if (zfs_dataset_exists(hdl, namebuf, ZFS_TYPE_DATASET)) {
34756ccda740Sloli zhp = zfs_open(hdl, namebuf, ZFS_TYPE_DATASET);
34766ccda740Sloli if (zhp == NULL) {
34776ccda740Sloli ret = -1;
34786ccda740Sloli goto error;
34796ccda740Sloli }
34806ccda740Sloli }
34816ccda740Sloli
34826ccda740Sloli /* open the zpool handle */
34836ccda740Sloli cp = strchr(namebuf, '/');
34846ccda740Sloli if (cp != NULL)
34856ccda740Sloli *cp = '\0';
34866ccda740Sloli zpool_hdl = zpool_open(hdl, namebuf);
34876ccda740Sloli if (zpool_hdl == NULL) {
34886ccda740Sloli ret = -1;
34896ccda740Sloli goto error;
34906ccda740Sloli }
34916ccda740Sloli
34926ccda740Sloli /* restore namebuf to match fsname for later use */
34936ccda740Sloli if (cp != NULL)
34946ccda740Sloli *cp = '/';
34956ccda740Sloli
34966ccda740Sloli /*
34976ccda740Sloli * first iteration: process excluded (-x) properties now and gather
34986ccda740Sloli * added (-o) properties to be later processed by zfs_valid_proplist()
34996ccda740Sloli */
35006ccda740Sloli nvp = NULL;
35016ccda740Sloli while ((nvp = nvlist_next_nvpair(cmdprops, nvp)) != NULL) {
35026ccda740Sloli const char *name = nvpair_name(nvp);
35036ccda740Sloli zfs_prop_t prop = zfs_name_to_prop(name);
35046ccda740Sloli
35056ccda740Sloli /* "origin" is processed separately, don't handle it here */
35066ccda740Sloli if (prop == ZFS_PROP_ORIGIN)
35076ccda740Sloli continue;
35086ccda740Sloli
35096ccda740Sloli /*
35106ccda740Sloli * we're trying to override or exclude a property that does not
35116ccda740Sloli * make sense for this type of dataset, but we don't want to
35126ccda740Sloli * fail if the receive is recursive: this comes in handy when
35136ccda740Sloli * the send stream contains, for instance, a child ZVOL and
35146ccda740Sloli * we're trying to receive it with "-o atime=on"
35156ccda740Sloli */
35164c2bdae2STim Chase if (!zfs_prop_valid_for_type(prop, type, B_FALSE) &&
35176ccda740Sloli !zfs_prop_user(name)) {
35186ccda740Sloli if (recursive)
35196ccda740Sloli continue;
35206ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35216ccda740Sloli "property '%s' does not apply to datasets of this "
35226ccda740Sloli "type"), name);
35236ccda740Sloli ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
35246ccda740Sloli goto error;
35256ccda740Sloli }
35266ccda740Sloli
35276ccda740Sloli /* raw streams can't override encryption properties */
35286ccda740Sloli if ((zfs_prop_encryption_key_param(prop) ||
35298675de3aSTom Caputi prop == ZFS_PROP_ENCRYPTION) && raw) {
35306ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35316ccda740Sloli "encryption property '%s' cannot "
35328675de3aSTom Caputi "be set or excluded for raw streams."), name);
35338675de3aSTom Caputi ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
35348675de3aSTom Caputi goto error;
35358675de3aSTom Caputi }
35368675de3aSTom Caputi
35378675de3aSTom Caputi /* incremental streams can only exclude encryption properties */
35388675de3aSTom Caputi if ((zfs_prop_encryption_key_param(prop) ||
35398675de3aSTom Caputi prop == ZFS_PROP_ENCRYPTION) && !newfs &&
35408675de3aSTom Caputi nvpair_type(nvp) != DATA_TYPE_BOOLEAN) {
35418675de3aSTom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35428675de3aSTom Caputi "encryption property '%s' cannot "
35438675de3aSTom Caputi "be set for incremental streams."), name);
35446ccda740Sloli ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
35456ccda740Sloli goto error;
35466ccda740Sloli }
35476ccda740Sloli
35486ccda740Sloli switch (nvpair_type(nvp)) {
35496ccda740Sloli case DATA_TYPE_BOOLEAN: /* -x property */
35506ccda740Sloli /*
35516ccda740Sloli * DATA_TYPE_BOOLEAN is the way we're asked to "exclude"
35526ccda740Sloli * a property: this is done by forcing an explicit
35536ccda740Sloli * inherit on the destination so the effective value is
35546ccda740Sloli * not the one we received from the send stream.
35556ccda740Sloli * We do this only if the property is not already
35566ccda740Sloli * locally-set, in which case its value will take
35576ccda740Sloli * priority over the received anyway.
35586ccda740Sloli */
35596ccda740Sloli if (nvlist_exists(origprops, name)) {
35606ccda740Sloli nvlist_t *attrs;
35618675de3aSTom Caputi char *source = NULL;
35626ccda740Sloli
35636ccda740Sloli attrs = fnvlist_lookup_nvlist(origprops, name);
35648675de3aSTom Caputi if (nvlist_lookup_string(attrs,
35658675de3aSTom Caputi ZPROP_SOURCE, &source) == 0 &&
35668675de3aSTom Caputi strcmp(source, ZPROP_SOURCE_VAL_RECVD) != 0)
35676ccda740Sloli continue;
35686ccda740Sloli }
35696ccda740Sloli /*
35706ccda740Sloli * We can't force an explicit inherit on non-inheritable
35716ccda740Sloli * properties: if we're asked to exclude this kind of
35726ccda740Sloli * values we remove them from "recvprops" input nvlist.
35736ccda740Sloli */
35746ccda740Sloli if (!zfs_prop_inheritable(prop) &&
35756ccda740Sloli !zfs_prop_user(name) && /* can be inherited too */
35766ccda740Sloli nvlist_exists(recvprops, name))
35776ccda740Sloli fnvlist_remove(recvprops, name);
35786ccda740Sloli else
35796ccda740Sloli fnvlist_add_nvpair(*oxprops, nvp);
35806ccda740Sloli break;
35816ccda740Sloli case DATA_TYPE_STRING: /* -o property=value */
35826ccda740Sloli fnvlist_add_nvpair(oprops, nvp);
35836ccda740Sloli break;
35846ccda740Sloli default:
35856ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
35866ccda740Sloli "property '%s' must be a string or boolean"), name);
35876ccda740Sloli ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
35886ccda740Sloli goto error;
35896ccda740Sloli }
35906ccda740Sloli }
35916ccda740Sloli
35926ccda740Sloli if (toplevel) {
35936ccda740Sloli /* convert override strings properties to native */
35946ccda740Sloli if ((voprops = zfs_valid_proplist(hdl, ZFS_TYPE_DATASET,
35956ccda740Sloli oprops, zoned, zhp, zpool_hdl, B_FALSE, errbuf)) == NULL) {
35966ccda740Sloli ret = zfs_error(hdl, EZFS_BADPROP, errbuf);
35976ccda740Sloli goto error;
35986ccda740Sloli }
35996ccda740Sloli
36006ccda740Sloli /*
36016ccda740Sloli * zfs_crypto_create() requires the parent name. Get it
36026ccda740Sloli * by truncating the fsname copy stored in namebuf.
36036ccda740Sloli */
36046ccda740Sloli cp = strrchr(namebuf, '/');
36056ccda740Sloli if (cp != NULL)
36066ccda740Sloli *cp = '\0';
36076ccda740Sloli
36086ccda740Sloli if (!raw && zfs_crypto_create(hdl, namebuf, voprops, NULL,
36096ccda740Sloli B_FALSE, wkeydata_out, wkeylen_out) != 0) {
36106ccda740Sloli fnvlist_free(voprops);
36116ccda740Sloli ret = zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
36126ccda740Sloli goto error;
36136ccda740Sloli }
36146ccda740Sloli
36156ccda740Sloli /* second pass: process "-o" properties */
36166ccda740Sloli fnvlist_merge(*oxprops, voprops);
36176ccda740Sloli fnvlist_free(voprops);
36186ccda740Sloli } else {
36196ccda740Sloli /* override props on child dataset are inherited */
36206ccda740Sloli nvp = NULL;
36216ccda740Sloli while ((nvp = nvlist_next_nvpair(oprops, nvp)) != NULL) {
36226ccda740Sloli const char *name = nvpair_name(nvp);
36236ccda740Sloli fnvlist_add_boolean(*oxprops, name);
36246ccda740Sloli }
36256ccda740Sloli }
36266ccda740Sloli
36276ccda740Sloli error:
36286ccda740Sloli if (zhp != NULL)
36296ccda740Sloli zfs_close(zhp);
36306ccda740Sloli if (zpool_hdl != NULL)
36316ccda740Sloli zpool_close(zpool_hdl);
36326ccda740Sloli fnvlist_free(oprops);
36336ccda740Sloli return (ret);
36346ccda740Sloli }
36356ccda740Sloli
36363cb34c60Sahrens /*
36373cb34c60Sahrens * Restores a backup of tosnap from the file descriptor specified by infd.
36383cb34c60Sahrens */
36393cb34c60Sahrens static int
zfs_receive_one(libzfs_handle_t * hdl,int infd,const char * tosnap,const char * originsnap,recvflags_t * flags,dmu_replay_record_t * drr,dmu_replay_record_t * drr_noswap,const char * sendfs,nvlist_t * stream_nv,avl_tree_t * stream_avl,char ** top_zfs,int cleanup_fd,uint64_t * action_handlep,const char * finalsnap,nvlist_t * cmdprops)36403cb34c60Sahrens zfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
3641a2cdcdd2SPaul Dagnelie const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
3642a2cdcdd2SPaul Dagnelie dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
3643a2cdcdd2SPaul Dagnelie avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
3644a60ca23dSTom Caputi uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops)
36453cb34c60Sahrens {
36463cb34c60Sahrens time_t begin_time;
3647bd6a198fSTom Erickson int ioctl_err, ioctl_errno, err;
36483cb34c60Sahrens char *cp;
36493cb34c60Sahrens struct drr_begin *drrb = &drr->drr_u.drr_begin;
36503cb34c60Sahrens char errbuf[1024];
3651bd6a198fSTom Erickson const char *chopprefix;
36523cb34c60Sahrens boolean_t newfs = B_FALSE;
36533cb34c60Sahrens boolean_t stream_wantsnewfs;
3654eb633035STom Caputi boolean_t newprops = B_FALSE;
36556ccda740Sloli uint64_t read_bytes = 0;
36566ccda740Sloli uint64_t errflags = 0;
36573cb34c60Sahrens uint64_t parent_snapguid = 0;
36583cb34c60Sahrens prop_changelist_t *clp = NULL;
3659bb0ade09Sahrens nvlist_t *snapprops_nvlist = NULL;
36606ccda740Sloli nvlist_t *snapholds_nvlist = NULL;
366192241e0bSTom Erickson zprop_errflags_t prop_errflags;
36626ccda740Sloli nvlist_t *prop_errors = NULL;
3663bd6a198fSTom Erickson boolean_t recursive;
36645878fad7SDan McDonald char *snapname = NULL;
36656ccda740Sloli char destsnap[MAXPATHLEN * 2];
36666ccda740Sloli char origin[MAXNAMELEN];
36676ccda740Sloli char name[MAXPATHLEN];
3668eb633035STom Caputi char tmp_keylocation[MAXNAMELEN];
3669a60ca23dSTom Caputi nvlist_t *rcvprops = NULL; /* props received from the send stream */
3670a60ca23dSTom Caputi nvlist_t *oxprops = NULL; /* override (-o) and exclude (-x) props */
36716ccda740Sloli nvlist_t *origprops = NULL; /* original props (if destination exists) */
36726ccda740Sloli zfs_type_t type;
36736ccda740Sloli boolean_t toplevel = B_FALSE;
36746ccda740Sloli boolean_t zoned = B_FALSE;
36756ccda740Sloli boolean_t hastoken = B_FALSE;
36766ccda740Sloli uint8_t *wkeydata = NULL;
36776ccda740Sloli uint_t wkeylen = 0;
36783cb34c60Sahrens
36793cb34c60Sahrens begin_time = time(NULL);
36806ccda740Sloli bzero(origin, MAXNAMELEN);
3681eb633035STom Caputi bzero(tmp_keylocation, MAXNAMELEN);
36823cb34c60Sahrens
36833cb34c60Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
36843cb34c60Sahrens "cannot receive"));
36853cb34c60Sahrens
3686bd6a198fSTom Erickson recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3687bd6a198fSTom Erickson ENOENT);
3688bd6a198fSTom Erickson
36896ccda740Sloli /* Did the user request holds be skipped via zfs recv -k? */
36906ccda740Sloli boolean_t holds = flags->holds && !flags->skipholds;
36916ccda740Sloli
36923cb34c60Sahrens if (stream_avl != NULL) {
3693eb633035STom Caputi char *keylocation = NULL;
3694eb633035STom Caputi nvlist_t *lookup = NULL;
3695bb0ade09Sahrens nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3696bb0ade09Sahrens &snapname);
36973cb34c60Sahrens
36983cb34c60Sahrens (void) nvlist_lookup_uint64(fs, "parentfromsnap",
36993cb34c60Sahrens &parent_snapguid);
37006ccda740Sloli err = nvlist_lookup_nvlist(fs, "props", &rcvprops);
3701eb633035STom Caputi if (err) {
37026ccda740Sloli VERIFY(0 == nvlist_alloc(&rcvprops, NV_UNIQUE_NAME, 0));
3703eb633035STom Caputi newprops = B_TRUE;
3704eb633035STom Caputi }
3705eb633035STom Caputi /*
3706eb633035STom Caputi * The keylocation property may only be set on encryption roots,
3707eb633035STom Caputi * but this dataset might not become an encryption root until
3708eb633035STom Caputi * recv_fix_encryption_hierarchy() is called. That function
3709eb633035STom Caputi * will fixup the keylocation anyway, so we temporarily unset
3710eb633035STom Caputi * the keylocation for now to avoid any errors from the receive
3711eb633035STom Caputi * ioctl.
3712eb633035STom Caputi */
37136ccda740Sloli err = nvlist_lookup_string(rcvprops,
3714eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION), &keylocation);
3715eb633035STom Caputi if (err == 0) {
3716eb633035STom Caputi (void) strcpy(tmp_keylocation, keylocation);
37176ccda740Sloli (void) nvlist_remove_all(rcvprops,
3718eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION));
3719eb633035STom Caputi }
372000954d7bSahl
372119b94df9SMatthew Ahrens if (flags->canmountoff) {
37226ccda740Sloli VERIFY(0 == nvlist_add_uint64(rcvprops,
37233cb34c60Sahrens zfs_prop_to_name(ZFS_PROP_CANMOUNT), 0));
37246ccda740Sloli } else if (newprops) { /* nothing in rcvprops, eliminate it */
37256ccda740Sloli nvlist_free(rcvprops);
37266ccda740Sloli rcvprops = NULL;
37276ccda740Sloli newprops = B_FALSE;
37283cb34c60Sahrens }
3729eb633035STom Caputi if (0 == nvlist_lookup_nvlist(fs, "snapprops", &lookup)) {
3730eb633035STom Caputi VERIFY(0 == nvlist_lookup_nvlist(lookup,
3731bb0ade09Sahrens snapname, &snapprops_nvlist));
3732bb0ade09Sahrens }
37336ccda740Sloli if (holds) {
37346ccda740Sloli if (0 == nvlist_lookup_nvlist(fs, "snapholds",
37356ccda740Sloli &lookup)) {
37366ccda740Sloli VERIFY(0 == nvlist_lookup_nvlist(lookup,
37376ccda740Sloli snapname, &snapholds_nvlist));
37386ccda740Sloli }
3739eb633035STom Caputi }
37403cb34c60Sahrens }
37413cb34c60Sahrens
3742bd6a198fSTom Erickson cp = NULL;
3743bd6a198fSTom Erickson
37443cb34c60Sahrens /*
37453cb34c60Sahrens * Determine how much of the snapshot name stored in the stream
37463cb34c60Sahrens * we are going to tack on to the name they specified on the
37473cb34c60Sahrens * command line, and how much we are going to chop off.
37483cb34c60Sahrens *
37493cb34c60Sahrens * If they specified a snapshot, chop the entire name stored in
37503cb34c60Sahrens * the stream.
37513cb34c60Sahrens */
375219b94df9SMatthew Ahrens if (flags->istail) {
3753bd6a198fSTom Erickson /*
3754bd6a198fSTom Erickson * A filesystem was specified with -e. We want to tack on only
3755bd6a198fSTom Erickson * the tail of the sent snapshot path.
3756bd6a198fSTom Erickson */
3757bd6a198fSTom Erickson if (strchr(tosnap, '@')) {
3758bd6a198fSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3759bd6a198fSTom Erickson "argument - snapshot not allowed with -e"));
3760eb633035STom Caputi err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3761eb633035STom Caputi goto out;
3762bd6a198fSTom Erickson }
3763bd6a198fSTom Erickson
3764bd6a198fSTom Erickson chopprefix = strrchr(sendfs, '/');
3765bd6a198fSTom Erickson
3766bd6a198fSTom Erickson if (chopprefix == NULL) {
3767bd6a198fSTom Erickson /*
3768bd6a198fSTom Erickson * The tail is the poolname, so we need to
3769bd6a198fSTom Erickson * prepend a path separator.
3770bd6a198fSTom Erickson */
3771bd6a198fSTom Erickson int len = strlen(drrb->drr_toname);
3772bd6a198fSTom Erickson cp = malloc(len + 2);
3773bd6a198fSTom Erickson cp[0] = '/';
3774bd6a198fSTom Erickson (void) strcpy(&cp[1], drrb->drr_toname);
3775bd6a198fSTom Erickson chopprefix = cp;
3776bd6a198fSTom Erickson } else {
3777bd6a198fSTom Erickson chopprefix = drrb->drr_toname + (chopprefix - sendfs);
3778bd6a198fSTom Erickson }
377919b94df9SMatthew Ahrens } else if (flags->isprefix) {
37803cb34c60Sahrens /*
3781bd6a198fSTom Erickson * A filesystem was specified with -d. We want to tack on
3782f64930f5STom Erickson * everything but the first element of the sent snapshot path
3783bd6a198fSTom Erickson * (all but the pool name).
37843cb34c60Sahrens */
37853cb34c60Sahrens if (strchr(tosnap, '@')) {
37863cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
3787bd6a198fSTom Erickson "argument - snapshot not allowed with -d"));
3788eb633035STom Caputi err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3789eb633035STom Caputi goto out;
37903cb34c60Sahrens }
3791bd6a198fSTom Erickson
3792bd6a198fSTom Erickson chopprefix = strchr(drrb->drr_toname, '/');
3793bd6a198fSTom Erickson if (chopprefix == NULL)
3794bd6a198fSTom Erickson chopprefix = strchr(drrb->drr_toname, '@');
37953cb34c60Sahrens } else if (strchr(tosnap, '@') == NULL) {
37963cb34c60Sahrens /*
3797bd6a198fSTom Erickson * If a filesystem was specified without -d or -e, we want to
3798bd6a198fSTom Erickson * tack on everything after the fs specified by 'zfs send'.
37993cb34c60Sahrens */
3800bd6a198fSTom Erickson chopprefix = drrb->drr_toname + strlen(sendfs);
3801bd6a198fSTom Erickson } else {
3802bd6a198fSTom Erickson /* A snapshot was specified as an exact path (no -d or -e). */
3803bd6a198fSTom Erickson if (recursive) {
3804bd6a198fSTom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3805bd6a198fSTom Erickson "cannot specify snapshot name for multi-snapshot "
3806bd6a198fSTom Erickson "stream"));
3807eb633035STom Caputi err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3808eb633035STom Caputi goto out;
3809bd6a198fSTom Erickson }
3810bd6a198fSTom Erickson chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
38113cb34c60Sahrens }
3812bd6a198fSTom Erickson
3813bd6a198fSTom Erickson ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
3814bd6a198fSTom Erickson ASSERT(chopprefix > drrb->drr_toname);
3815bd6a198fSTom Erickson ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
3816bd6a198fSTom Erickson ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3817bd6a198fSTom Erickson chopprefix[0] == '\0');
38183cb34c60Sahrens
38193cb34c60Sahrens /*
38203cb34c60Sahrens * Determine name of destination snapshot, store in zc_value.
38213cb34c60Sahrens */
38226ccda740Sloli (void) strlcpy(destsnap, tosnap, sizeof (destsnap));
38236ccda740Sloli (void) strlcat(destsnap, chopprefix, sizeof (destsnap));
3824bd6a198fSTom Erickson free(cp);
38256ccda740Sloli if (!zfs_name_valid(destsnap, ZFS_TYPE_SNAPSHOT)) {
3826eb633035STom Caputi err = zfs_error(hdl, EZFS_INVALIDNAME, errbuf);
3827eb633035STom Caputi goto out;
382800954d7bSahl }
38293cb34c60Sahrens
38303cb34c60Sahrens /*
38313cb34c60Sahrens * Determine the name of the origin snapshot, store in zc_string.
38323cb34c60Sahrens */
3833ed4e7a6aSPaul Dagnelie if (originsnap) {
38346ccda740Sloli (void) strlcpy(origin, originsnap, sizeof (origin));
3835ed4e7a6aSPaul Dagnelie if (flags->verbose)
3836ed4e7a6aSPaul Dagnelie (void) printf("using provided clone origin %s\n",
38376ccda740Sloli origin);
3838ed4e7a6aSPaul Dagnelie } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
38396ccda740Sloli if (guid_to_name(hdl, destsnap,
38406ccda740Sloli drrb->drr_fromguid, B_FALSE, origin) != 0) {
38413cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
38423cb34c60Sahrens "local origin for clone %s does not exist"),
38436ccda740Sloli destsnap);
3844eb633035STom Caputi err = zfs_error(hdl, EZFS_NOENT, errbuf);
3845eb633035STom Caputi goto out;
38463cb34c60Sahrens }
384719b94df9SMatthew Ahrens if (flags->verbose)
38486ccda740Sloli (void) printf("found clone origin %s\n", origin);
38493cb34c60Sahrens }
38503cb34c60Sahrens
38519c3fd121SMatthew Ahrens boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
38529c3fd121SMatthew Ahrens DMU_BACKUP_FEATURE_RESUMING;
3853eb633035STom Caputi boolean_t raw = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3854eb633035STom Caputi DMU_BACKUP_FEATURE_RAW;
3855eb633035STom Caputi boolean_t embedded = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
3856eb633035STom Caputi DMU_BACKUP_FEATURE_EMBED_DATA;
38575b8cbb8eSToomas Soome stream_wantsnewfs = (drrb->drr_fromguid == 0 ||
38589c3fd121SMatthew Ahrens (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
38593cb34c60Sahrens
38603cb34c60Sahrens if (stream_wantsnewfs) {
38613cb34c60Sahrens /*
38623cb34c60Sahrens * if the parent fs does not exist, look for it based on
38633cb34c60Sahrens * the parent snap GUID
38643cb34c60Sahrens */
38653cb34c60Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38663cb34c60Sahrens "cannot receive new filesystem stream"));
38673cb34c60Sahrens
38686ccda740Sloli (void) strcpy(name, destsnap);
38696ccda740Sloli cp = strrchr(name, '/');
38703cb34c60Sahrens if (cp)
38713cb34c60Sahrens *cp = '\0';
38723cb34c60Sahrens if (cp &&
38736ccda740Sloli !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
38749adfa60dSMatthew Ahrens char suffix[ZFS_MAX_DATASET_NAME_LEN];
38756ccda740Sloli (void) strcpy(suffix, strrchr(destsnap, '/'));
38766ccda740Sloli if (guid_to_name(hdl, name, parent_snapguid,
38776ccda740Sloli B_FALSE, destsnap) == 0) {
38786ccda740Sloli *strchr(destsnap, '@') = '\0';
38796ccda740Sloli (void) strcat(destsnap, suffix);
38803cb34c60Sahrens }
38813cb34c60Sahrens }
38823cb34c60Sahrens } else {
38833cb34c60Sahrens /*
38843cb34c60Sahrens * if the fs does not exist, look for it based on the
38853cb34c60Sahrens * fromsnap GUID
38863cb34c60Sahrens */
38873cb34c60Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
38883cb34c60Sahrens "cannot receive incremental stream"));
38893cb34c60Sahrens
38906ccda740Sloli (void) strcpy(name, destsnap);
38916ccda740Sloli *strchr(name, '@') = '\0';
38923cb34c60Sahrens
389383d7f9feSTom Erickson /*
389483d7f9feSTom Erickson * If the exact receive path was specified and this is the
389583d7f9feSTom Erickson * topmost path in the stream, then if the fs does not exist we
389683d7f9feSTom Erickson * should look no further.
389783d7f9feSTom Erickson */
389819b94df9SMatthew Ahrens if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
389983d7f9feSTom Erickson strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
39006ccda740Sloli !zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
39019adfa60dSMatthew Ahrens char snap[ZFS_MAX_DATASET_NAME_LEN];
39026ccda740Sloli (void) strcpy(snap, strchr(destsnap, '@'));
39036ccda740Sloli if (guid_to_name(hdl, name, drrb->drr_fromguid,
39046ccda740Sloli B_FALSE, destsnap) == 0) {
39056ccda740Sloli *strchr(destsnap, '@') = '\0';
39066ccda740Sloli (void) strcat(destsnap, snap);
39073cb34c60Sahrens }
39083cb34c60Sahrens }
39093cb34c60Sahrens }
39103cb34c60Sahrens
39116ccda740Sloli (void) strcpy(name, destsnap);
39126ccda740Sloli *strchr(name, '@') = '\0';
39133cb34c60Sahrens
39146ccda740Sloli if (zfs_dataset_exists(hdl, name, ZFS_TYPE_DATASET)) {
39156ccda740Sloli zfs_cmd_t zc = { 0 };
39163cb34c60Sahrens zfs_handle_t *zhp;
3917eb633035STom Caputi boolean_t encrypted;
3918bd6a198fSTom Erickson
39196ccda740Sloli (void) strcpy(zc.zc_name, name);
39206ccda740Sloli
39213cb34c60Sahrens /*
39229c3fd121SMatthew Ahrens * Destination fs exists. It must be one of these cases:
39239c3fd121SMatthew Ahrens * - an incremental send stream
39249c3fd121SMatthew Ahrens * - the stream specifies a new fs (full stream or clone)
39259c3fd121SMatthew Ahrens * and they want us to blow away the existing fs (and
39269c3fd121SMatthew Ahrens * have therefore specified -F and removed any snapshots)
39279c3fd121SMatthew Ahrens * - we are resuming a failed receive.
39283cb34c60Sahrens */
39293cb34c60Sahrens if (stream_wantsnewfs) {
393019b94df9SMatthew Ahrens if (!flags->force) {
393100954d7bSahl zcmd_free_nvlists(&zc);
39323cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39333cb34c60Sahrens "destination '%s' exists\n"
39346ccda740Sloli "must specify -F to overwrite it"), name);
3935eb633035STom Caputi err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3936eb633035STom Caputi goto out;
39373cb34c60Sahrens }
39383cb34c60Sahrens if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
39393cb34c60Sahrens &zc) == 0) {
394000954d7bSahl zcmd_free_nvlists(&zc);
39413cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39423cb34c60Sahrens "destination has snapshots (eg. %s)\n"
39433cb34c60Sahrens "must destroy them to overwrite it"),
39443cb34c60Sahrens zc.zc_name);
3945eb633035STom Caputi err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3946eb633035STom Caputi goto out;
39473cb34c60Sahrens }
39483cb34c60Sahrens }
39493cb34c60Sahrens
39506ccda740Sloli if ((zhp = zfs_open(hdl, name,
395100954d7bSahl ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME)) == NULL) {
395200954d7bSahl zcmd_free_nvlists(&zc);
3953eb633035STom Caputi err = -1;
3954eb633035STom Caputi goto out;
395500954d7bSahl }
395600954d7bSahl
39573cb34c60Sahrens if (stream_wantsnewfs &&
39583cb34c60Sahrens zhp->zfs_dmustats.dds_origin[0]) {
395900954d7bSahl zcmd_free_nvlists(&zc);
39603cb34c60Sahrens zfs_close(zhp);
39613cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
39623cb34c60Sahrens "destination '%s' is a clone\n"
39636ccda740Sloli "must destroy it to overwrite it"), name);
3964eb633035STom Caputi err = zfs_error(hdl, EZFS_EXISTS, errbuf);
3965eb633035STom Caputi goto out;
3966eb633035STom Caputi }
3967eb633035STom Caputi
3968eb633035STom Caputi /*
3969eb633035STom Caputi * Raw sends can not be performed as an incremental on top
3970eb633035STom Caputi * of existing unencrypted datasets. zfs recv -F cant be
3971eb633035STom Caputi * used to blow away an existing encrypted filesystem. This
3972eb633035STom Caputi * is because it would require the dsl dir to point to the
3973eb633035STom Caputi * new key (or lack of a key) and the old key at the same
3974eb633035STom Caputi * time. The -F flag may still be used for deleting
3975eb633035STom Caputi * intermediate snapshots that would otherwise prevent the
3976eb633035STom Caputi * receive from working.
3977eb633035STom Caputi */
3978eb633035STom Caputi encrypted = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) !=
3979eb633035STom Caputi ZIO_CRYPT_OFF;
3980eb633035STom Caputi if (!stream_wantsnewfs && !encrypted && raw) {
3981eb633035STom Caputi zfs_close(zhp);
3982eb633035STom Caputi zcmd_free_nvlists(&zc);
3983eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3984eb633035STom Caputi "cannot perform raw receive on top of "
3985eb633035STom Caputi "existing unencrypted dataset"));
3986eb633035STom Caputi err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3987eb633035STom Caputi goto out;
3988eb633035STom Caputi }
3989eb633035STom Caputi
3990eb633035STom Caputi if (stream_wantsnewfs && flags->force &&
3991eb633035STom Caputi ((raw && !encrypted) || encrypted)) {
3992eb633035STom Caputi zfs_close(zhp);
3993eb633035STom Caputi zcmd_free_nvlists(&zc);
3994eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
3995eb633035STom Caputi "zfs receive -F cannot be used to destroy an "
3996eb633035STom Caputi "encrypted filesystem or overwrite an "
3997eb633035STom Caputi "unencrypted one with an encrypted one"));
3998eb633035STom Caputi err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
3999eb633035STom Caputi goto out;
40003cb34c60Sahrens }
40013cb34c60Sahrens
400219b94df9SMatthew Ahrens if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
40033cb34c60Sahrens stream_wantsnewfs) {
40043cb34c60Sahrens /* We can't do online recv in this case */
40050069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
400600954d7bSahl if (clp == NULL) {
400788bb18d2SLori Alt zfs_close(zhp);
4008eb633035STom Caputi err = -1;
4009eb633035STom Caputi goto out;
401000954d7bSahl }
40113cb34c60Sahrens if (changelist_prefix(clp) != 0) {
40123cb34c60Sahrens changelist_free(clp);
401388bb18d2SLori Alt zfs_close(zhp);
4014eb633035STom Caputi err = -1;
4015eb633035STom Caputi goto out;
40163cb34c60Sahrens }
40173cb34c60Sahrens }
40189c3fd121SMatthew Ahrens
40199c3fd121SMatthew Ahrens /*
40209c3fd121SMatthew Ahrens * If we are resuming a newfs, set newfs here so that we will
40219c3fd121SMatthew Ahrens * mount it if the recv succeeds this time. We can tell
40229c3fd121SMatthew Ahrens * that it was a newfs on the first recv because the fs
40239c3fd121SMatthew Ahrens * itself will be inconsistent (if the fs existed when we
40249c3fd121SMatthew Ahrens * did the first recv, we would have received it into
40259c3fd121SMatthew Ahrens * .../%recv).
40269c3fd121SMatthew Ahrens */
40279c3fd121SMatthew Ahrens if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
40289c3fd121SMatthew Ahrens newfs = B_TRUE;
40299c3fd121SMatthew Ahrens
40306ccda740Sloli /* we want to know if we're zoned when validating -o|-x props */
40316ccda740Sloli zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED);
40326ccda740Sloli
40336ccda740Sloli /* may need this info later, get it now we have zhp around */
40346ccda740Sloli if (zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN, NULL, 0,
40356ccda740Sloli NULL, NULL, 0, B_TRUE) == 0)
40366ccda740Sloli hastoken = B_TRUE;
40376ccda740Sloli
40386ccda740Sloli /* gather existing properties on destination */
40396ccda740Sloli origprops = fnvlist_alloc();
40406ccda740Sloli fnvlist_merge(origprops, zhp->zfs_props);
40416ccda740Sloli fnvlist_merge(origprops, zhp->zfs_user_props);
40426ccda740Sloli
40433cb34c60Sahrens zfs_close(zhp);
40446ccda740Sloli cp = NULL;
40453cb34c60Sahrens } else {
40466ccda740Sloli zfs_handle_t *zhp;
40476ccda740Sloli
40483cb34c60Sahrens /*
404900954d7bSahl * Destination filesystem does not exist. Therefore we better
405000954d7bSahl * be creating a new filesystem (either from a full backup, or
405100954d7bSahl * a clone). It would therefore be invalid if the user
405200954d7bSahl * specified only the pool name (i.e. if the destination name
405300954d7bSahl * contained no slash character).
40543cb34c60Sahrens */
40556ccda740Sloli cp = strrchr(name, '/');
40566ccda740Sloli
40576ccda740Sloli if (!stream_wantsnewfs || cp == NULL) {
40583cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40596ccda740Sloli "destination '%s' does not exist"), name);
4060eb633035STom Caputi err = zfs_error(hdl, EZFS_NOENT, errbuf);
4061eb633035STom Caputi goto out;
40623cb34c60Sahrens }
40633cb34c60Sahrens
406400954d7bSahl /*
406500954d7bSahl * Trim off the final dataset component so we perform the
406600954d7bSahl * recvbackup ioctl to the filesystems's parent.
406700954d7bSahl */
406800954d7bSahl *cp = '\0';
40693cb34c60Sahrens
407019b94df9SMatthew Ahrens if (flags->isprefix && !flags->istail && !flags->dryrun &&
40716ccda740Sloli create_parents(hdl, destsnap, strlen(tosnap)) != 0) {
40726ccda740Sloli err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
40736ccda740Sloli goto out;
40746ccda740Sloli }
40756ccda740Sloli
40766ccda740Sloli /* validate parent */
40776ccda740Sloli zhp = zfs_open(hdl, name, ZFS_TYPE_DATASET);
40786ccda740Sloli if (zhp == NULL) {
4079eb633035STom Caputi err = zfs_error(hdl, EZFS_BADRESTORE, errbuf);
4080eb633035STom Caputi goto out;
4081eb633035STom Caputi }
40826ccda740Sloli if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) {
40836ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
40846ccda740Sloli "parent '%s' is not a filesystem"), name);
40856ccda740Sloli err = zfs_error(hdl, EZFS_WRONG_PARENT, errbuf);
40866ccda740Sloli zfs_close(zhp);
40876ccda740Sloli goto out;
40886ccda740Sloli }
40896ccda740Sloli
40906ccda740Sloli /*
40916ccda740Sloli * It is invalid to receive a properties stream that was
40926ccda740Sloli * unencrypted on the send side as a child of an encrypted
40936ccda740Sloli * parent. Technically there is nothing preventing this, but
40946ccda740Sloli * it would mean that the encryption=off property which is
40956ccda740Sloli * locally set on the send side would not be received correctly.
40966ccda740Sloli * We can infer encryption=off if the stream is not raw and
40976ccda740Sloli * properties were included since the send side will only ever
40986ccda740Sloli * send the encryption property in a raw nvlist header. This
40996ccda740Sloli * check will be avoided if the user specifically overrides
41006ccda740Sloli * the encryption property on the command line.
41016ccda740Sloli */
41026ccda740Sloli if (!raw && rcvprops != NULL &&
41036ccda740Sloli !nvlist_exists(cmdprops,
41046ccda740Sloli zfs_prop_to_name(ZFS_PROP_ENCRYPTION))) {
41056ccda740Sloli uint64_t crypt;
41066ccda740Sloli
41076ccda740Sloli crypt = zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION);
41086ccda740Sloli
41096ccda740Sloli if (crypt != ZIO_CRYPT_OFF) {
41106ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41116ccda740Sloli "parent '%s' must not be encrypted to "
41126ccda740Sloli "receive unenecrypted property"), name);
41136ccda740Sloli err = zfs_error(hdl, EZFS_BADPROP, errbuf);
41146ccda740Sloli zfs_close(zhp);
41156ccda740Sloli goto out;
41166ccda740Sloli }
41176ccda740Sloli }
41186ccda740Sloli zfs_close(zhp);
4119eb633035STom Caputi
41203cb34c60Sahrens newfs = B_TRUE;
41216ccda740Sloli *cp = '/';
41223cb34c60Sahrens }
41233cb34c60Sahrens
412419b94df9SMatthew Ahrens if (flags->verbose) {
41253cb34c60Sahrens (void) printf("%s %s stream of %s into %s\n",
412619b94df9SMatthew Ahrens flags->dryrun ? "would receive" : "receiving",
41273cb34c60Sahrens drrb->drr_fromguid ? "incremental" : "full",
41286ccda740Sloli drrb->drr_toname, destsnap);
41293cb34c60Sahrens (void) fflush(stdout);
41303cb34c60Sahrens }
41313cb34c60Sahrens
413219b94df9SMatthew Ahrens if (flags->dryrun) {
4133eb633035STom Caputi err = recv_skip(hdl, infd, flags->byteswap);
4134eb633035STom Caputi goto out;
413500954d7bSahl }
41363cb34c60Sahrens
41376ccda740Sloli if (top_zfs && (*top_zfs == NULL || strcmp(*top_zfs, name) == 0))
41386ccda740Sloli toplevel = B_TRUE;
41396ccda740Sloli if (drrb->drr_type == DMU_OST_ZVOL) {
41406ccda740Sloli type = ZFS_TYPE_VOLUME;
41416ccda740Sloli } else if (drrb->drr_type == DMU_OST_ZFS) {
41426ccda740Sloli type = ZFS_TYPE_FILESYSTEM;
41436ccda740Sloli } else {
41446ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
41456ccda740Sloli "invalid record type: 0x%d"), drrb->drr_type);
41466ccda740Sloli err = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
41476ccda740Sloli goto out;
4148a60ca23dSTom Caputi }
41496ccda740Sloli if ((err = zfs_setup_cmdline_props(hdl, type, name, zoned, recursive,
41506ccda740Sloli stream_wantsnewfs, raw, toplevel, rcvprops, cmdprops, origprops,
41516ccda740Sloli &oxprops, &wkeydata, &wkeylen, errbuf)) != 0)
41526ccda740Sloli goto out;
4153a60ca23dSTom Caputi
41546ccda740Sloli /*
41556ccda740Sloli * The following is a difference between ZoL and illumos.
41566ccda740Sloli *
41576ccda740Sloli * On illumos, we must trim the last component of the dataset name
41586ccda740Sloli * that is passed via the ioctl so that we can properly validate
41596ccda740Sloli * zfs_secpolicy_recv() when receiving to a delegated dataset within
41606ccda740Sloli * zone. This matches the historical behavior of the receive ioctl.
41616ccda740Sloli * However, we can't do this until after zfs_setup_cmdline_props()
41626ccda740Sloli * has finished with the full name.
41636ccda740Sloli */
41646ccda740Sloli if (cp != NULL)
41656ccda740Sloli *cp = '\0';
416692241e0bSTom Erickson
41676ccda740Sloli err = ioctl_err = lzc_receive_with_cmdprops(destsnap, rcvprops,
41686ccda740Sloli oxprops, wkeydata, wkeylen, origin, flags->force, flags->resumable,
41696ccda740Sloli raw, infd, drr_noswap, cleanup_fd, &read_bytes, &errflags,
41706ccda740Sloli action_handlep, &prop_errors);
41713cb34c60Sahrens ioctl_errno = errno;
41726ccda740Sloli prop_errflags = errflags;
417392241e0bSTom Erickson
417492241e0bSTom Erickson if (err == 0) {
417592241e0bSTom Erickson nvpair_t *prop_err = NULL;
417692241e0bSTom Erickson
417792241e0bSTom Erickson while ((prop_err = nvlist_next_nvpair(prop_errors,
417892241e0bSTom Erickson prop_err)) != NULL) {
417992241e0bSTom Erickson char tbuf[1024];
418092241e0bSTom Erickson zfs_prop_t prop;
418192241e0bSTom Erickson int intval;
418292241e0bSTom Erickson
418392241e0bSTom Erickson prop = zfs_name_to_prop(nvpair_name(prop_err));
418492241e0bSTom Erickson (void) nvpair_value_int32(prop_err, &intval);
418592241e0bSTom Erickson if (strcmp(nvpair_name(prop_err),
418692241e0bSTom Erickson ZPROP_N_MORE_ERRORS) == 0) {
418792241e0bSTom Erickson trunc_prop_errs(intval);
418892241e0bSTom Erickson break;
41895878fad7SDan McDonald } else if (snapname == NULL || finalsnap == NULL ||
41905878fad7SDan McDonald strcmp(finalsnap, snapname) == 0 ||
41915878fad7SDan McDonald strcmp(nvpair_name(prop_err),
41925878fad7SDan McDonald zfs_prop_to_name(ZFS_PROP_REFQUOTA)) != 0) {
41935878fad7SDan McDonald /*
41945878fad7SDan McDonald * Skip the special case of, for example,
41955878fad7SDan McDonald * "refquota", errors on intermediate
41965878fad7SDan McDonald * snapshots leading up to a final one.
41975878fad7SDan McDonald * That's why we have all of the checks above.
41985878fad7SDan McDonald *
41995878fad7SDan McDonald * See zfs_ioctl.c's extract_delay_props() for
42005878fad7SDan McDonald * a list of props which can fail on
42015878fad7SDan McDonald * intermediate snapshots, but shouldn't
42025878fad7SDan McDonald * affect the overall receive.
42035878fad7SDan McDonald */
420492241e0bSTom Erickson (void) snprintf(tbuf, sizeof (tbuf),
420592241e0bSTom Erickson dgettext(TEXT_DOMAIN,
420692241e0bSTom Erickson "cannot receive %s property on %s"),
42076ccda740Sloli nvpair_name(prop_err), name);
420892241e0bSTom Erickson zfs_setprop_error(hdl, prop, intval, tbuf);
420992241e0bSTom Erickson }
421092241e0bSTom Erickson }
421192241e0bSTom Erickson nvlist_free(prop_errors);
421292241e0bSTom Erickson }
421392241e0bSTom Erickson
4214bb0ade09Sahrens if (err == 0 && snapprops_nvlist) {
42156ccda740Sloli zfs_cmd_t zc = { 0 };
4216bb0ade09Sahrens
42176ccda740Sloli (void) strcpy(zc.zc_name, destsnap);
42186ccda740Sloli zc.zc_cookie = B_TRUE; /* received */
42196ccda740Sloli if (zcmd_write_src_nvlist(hdl, &zc, snapprops_nvlist) == 0) {
42206ccda740Sloli (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc);
42216ccda740Sloli zcmd_free_nvlists(&zc);
42226ccda740Sloli }
42236ccda740Sloli }
42246ccda740Sloli if (err == 0 && snapholds_nvlist) {
42256ccda740Sloli nvpair_t *pair;
42266ccda740Sloli nvlist_t *holds, *errors = NULL;
42276ccda740Sloli int cleanup_fd = -1;
42286ccda740Sloli
42296ccda740Sloli VERIFY(0 == nvlist_alloc(&holds, 0, KM_SLEEP));
42306ccda740Sloli for (pair = nvlist_next_nvpair(snapholds_nvlist, NULL);
42316ccda740Sloli pair != NULL;
42326ccda740Sloli pair = nvlist_next_nvpair(snapholds_nvlist, pair)) {
42336ccda740Sloli VERIFY(0 == nvlist_add_string(holds, destsnap,
42346ccda740Sloli nvpair_name(pair)));
4235bb0ade09Sahrens }
42366ccda740Sloli (void) lzc_hold(holds, cleanup_fd, &errors);
42376ccda740Sloli nvlist_free(snapholds_nvlist);
42386ccda740Sloli nvlist_free(holds);
4239bb0ade09Sahrens }
4240bb0ade09Sahrens
4241bd6a198fSTom Erickson if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
42423cb34c60Sahrens /*
42433cb34c60Sahrens * It may be that this snapshot already exists,
42443cb34c60Sahrens * in which case we want to consume & ignore it
42453cb34c60Sahrens * rather than failing.
42463cb34c60Sahrens */
42473cb34c60Sahrens avl_tree_t *local_avl;
42483cb34c60Sahrens nvlist_t *local_nv, *fs;
42496ccda740Sloli cp = strchr(destsnap, '@');
42503cb34c60Sahrens
42513cb34c60Sahrens /*
42523cb34c60Sahrens * XXX Do this faster by just iterating over snaps in
42533cb34c60Sahrens * this fs. Also if zc_value does not exist, we will
42543cb34c60Sahrens * get a strange "does not exist" error message.
42553cb34c60Sahrens */
42563cb34c60Sahrens *cp = '\0';
42576ccda740Sloli if (gather_nvlist(hdl, destsnap, NULL, NULL, B_FALSE, B_TRUE,
42586ccda740Sloli B_FALSE, B_FALSE, B_FALSE, B_TRUE,
42596ccda740Sloli &local_nv, &local_avl) == 0) {
42603cb34c60Sahrens *cp = '@';
42613cb34c60Sahrens fs = fsavl_find(local_avl, drrb->drr_toguid, NULL);
42623cb34c60Sahrens fsavl_destroy(local_avl);
42633cb34c60Sahrens nvlist_free(local_nv);
42643cb34c60Sahrens
42653cb34c60Sahrens if (fs != NULL) {
426619b94df9SMatthew Ahrens if (flags->verbose) {
42673cb34c60Sahrens (void) printf("snap %s already exists; "
42686ccda740Sloli "ignoring\n", destsnap);
42693cb34c60Sahrens }
427092241e0bSTom Erickson err = ioctl_err = recv_skip(hdl, infd,
427119b94df9SMatthew Ahrens flags->byteswap);
42723cb34c60Sahrens }
42733cb34c60Sahrens }
42743cb34c60Sahrens *cp = '@';
42753cb34c60Sahrens }
42763cb34c60Sahrens
42773cb34c60Sahrens if (ioctl_err != 0) {
42783cb34c60Sahrens switch (ioctl_errno) {
42793cb34c60Sahrens case ENODEV:
42806ccda740Sloli cp = strchr(destsnap, '@');
42813cb34c60Sahrens *cp = '\0';
42823cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42833cb34c60Sahrens "most recent snapshot of %s does not\n"
42846ccda740Sloli "match incremental source"), destsnap);
42853cb34c60Sahrens (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
42863cb34c60Sahrens *cp = '@';
42873cb34c60Sahrens break;
42883cb34c60Sahrens case ETXTBSY:
42893cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
42903cb34c60Sahrens "destination %s has been modified\n"
42916ccda740Sloli "since most recent snapshot"), name);
42923cb34c60Sahrens (void) zfs_error(hdl, EZFS_BADRESTORE, errbuf);
42933cb34c60Sahrens break;
4294eb633035STom Caputi case EACCES:
4295eb633035STom Caputi if (raw && stream_wantsnewfs) {
4296eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4297eb633035STom Caputi "failed to create encryption key"));
4298eb633035STom Caputi } else if (raw && !stream_wantsnewfs) {
4299eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4300eb633035STom Caputi "encryption key does not match "
4301eb633035STom Caputi "existing key"));
4302eb633035STom Caputi } else {
4303eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4304eb633035STom Caputi "inherited key must be loaded"));
4305eb633035STom Caputi }
4306eb633035STom Caputi (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf);
4307eb633035STom Caputi break;
43083cb34c60Sahrens case EEXIST:
43096ccda740Sloli cp = strchr(destsnap, '@');
43103cb34c60Sahrens if (newfs) {
43113cb34c60Sahrens /* it's the containing fs that exists */
43123cb34c60Sahrens *cp = '\0';
43133cb34c60Sahrens }
43143cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43153cb34c60Sahrens "destination already exists"));
43163cb34c60Sahrens (void) zfs_error_fmt(hdl, EZFS_EXISTS,
43173cb34c60Sahrens dgettext(TEXT_DOMAIN, "cannot restore to %s"),
43186ccda740Sloli destsnap);
43193cb34c60Sahrens *cp = '@';
43203cb34c60Sahrens break;
43213cb34c60Sahrens case EINVAL:
4322eb633035STom Caputi if (embedded && !raw)
4323eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4324eb633035STom Caputi "incompatible embedded data stream "
4325eb633035STom Caputi "feature with encrypted receive."));
43263cb34c60Sahrens (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
43273cb34c60Sahrens break;
43283cb34c60Sahrens case ECKSUM:
43296ccda740Sloli recv_ecksum_set_aux(hdl, destsnap, flags->resumable);
43303cb34c60Sahrens (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4331dc7cd546SMark Shellenbaum break;
4332dc7cd546SMark Shellenbaum case ENOTSUP:
4333dc7cd546SMark Shellenbaum zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4334dc7cd546SMark Shellenbaum "pool must be upgraded to receive this stream."));
4335dc7cd546SMark Shellenbaum (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
433637f8ae65SJohn Harres break;
433737f8ae65SJohn Harres case EDQUOT:
433837f8ae65SJohn Harres zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43396ccda740Sloli "destination %s space quota exceeded."), name);
434019b94df9SMatthew Ahrens (void) zfs_error(hdl, EZFS_NOSPC, errbuf);
43413cb34c60Sahrens break;
4342eb633035STom Caputi case ZFS_ERR_FROM_IVSET_GUID_MISSING:
4343eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4344eb633035STom Caputi "IV set guid missing. See errata %u at"
4345eb633035STom Caputi "http://zfsonlinux.org/msg/ZFS-8000-ER"),
4346eb633035STom Caputi ZPOOL_ERRATA_ZOL_8308_ENCRYPTION);
4347eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4348eb633035STom Caputi break;
4349eb633035STom Caputi case ZFS_ERR_FROM_IVSET_GUID_MISMATCH:
4350eb633035STom Caputi zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
4351eb633035STom Caputi "IV set guid mismatch. See the 'zfs receive' "
4352eb633035STom Caputi "man page section\n discussing the limitations "
4353eb633035STom Caputi "of raw encrypted send streams."));
4354eb633035STom Caputi (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
4355eb633035STom Caputi break;
43566ccda740Sloli case ZFS_ERR_SPILL_BLOCK_FLAG_MISSING:
43576ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43586ccda740Sloli "Spill block flag missing for raw send.\n"
43596ccda740Sloli "The zfs software on the sending system must "
43606ccda740Sloli "be updated."));
43616ccda740Sloli (void) zfs_error(hdl, EZFS_BADSTREAM, errbuf);
43626ccda740Sloli break;
43636ccda740Sloli case EBUSY:
43646ccda740Sloli if (hastoken) {
43656ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
43666ccda740Sloli "destination %s contains "
43676ccda740Sloli "partially-complete state from "
43686ccda740Sloli "\"zfs receive -s\"."), name);
43696ccda740Sloli (void) zfs_error(hdl, EZFS_BUSY, errbuf);
43706ccda740Sloli break;
43716ccda740Sloli }
43726ccda740Sloli /* fallthru */
43733cb34c60Sahrens default:
43743cb34c60Sahrens (void) zfs_standard_error(hdl, ioctl_errno, errbuf);
43753cb34c60Sahrens }
43763cb34c60Sahrens }
43773cb34c60Sahrens
43783cb34c60Sahrens /*
4379681d9761SEric Taylor * Mount the target filesystem (if created). Also mount any
4380681d9761SEric Taylor * children of the target filesystem if we did a replication
4381681d9761SEric Taylor * receive (indicated by stream_avl being non-NULL).
43823cb34c60Sahrens */
43836ccda740Sloli cp = strchr(destsnap, '@');
43843cb34c60Sahrens if (cp && (ioctl_err == 0 || !newfs)) {
43853cb34c60Sahrens zfs_handle_t *h;
43863cb34c60Sahrens
43873cb34c60Sahrens *cp = '\0';
43886ccda740Sloli h = zfs_open(hdl, destsnap,
43893cb34c60Sahrens ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
439004bb726eSahl if (h != NULL) {
43913cb34c60Sahrens if (h->zfs_type == ZFS_TYPE_VOLUME) {
43920069fd67STim Haley *cp = '@';
439388bb18d2SLori Alt } else if (newfs || stream_avl) {
43940069fd67STim Haley /*
43950069fd67STim Haley * Track the first/top of hierarchy fs,
43960069fd67STim Haley * for mounting and sharing later.
43970069fd67STim Haley */
43980069fd67STim Haley if (top_zfs && *top_zfs == NULL)
43996ccda740Sloli *top_zfs = zfs_strdup(hdl, destsnap);
44003cb34c60Sahrens }
440104bb726eSahl zfs_close(h);
44023cb34c60Sahrens }
44030069fd67STim Haley *cp = '@';
44043cb34c60Sahrens }
44053cb34c60Sahrens
44063cb34c60Sahrens if (clp) {
44079185393fSAndriy Gapon if (!flags->nomount)
44089185393fSAndriy Gapon err |= changelist_postfix(clp);
44093cb34c60Sahrens changelist_free(clp);
44103cb34c60Sahrens }
44113cb34c60Sahrens
441292241e0bSTom Erickson if (prop_errflags & ZPROP_ERR_NOCLEAR) {
441392241e0bSTom Erickson (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
44146ccda740Sloli "failed to clear unreceived properties on %s"), name);
441592241e0bSTom Erickson (void) fprintf(stderr, "\n");
441692241e0bSTom Erickson }
441792241e0bSTom Erickson if (prop_errflags & ZPROP_ERR_NORESTORE) {
441892241e0bSTom Erickson (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
44196ccda740Sloli "failed to restore original properties on %s"), name);
442092241e0bSTom Erickson (void) fprintf(stderr, "\n");
442192241e0bSTom Erickson }
442292241e0bSTom Erickson
4423eb633035STom Caputi if (err || ioctl_err) {
4424eb633035STom Caputi err = -1;
4425eb633035STom Caputi goto out;
4426eb633035STom Caputi }
44273cb34c60Sahrens
442819b94df9SMatthew Ahrens if (flags->verbose) {
44293cb34c60Sahrens char buf1[64];
44303cb34c60Sahrens char buf2[64];
44316ccda740Sloli uint64_t bytes = read_bytes;
44323cb34c60Sahrens time_t delta = time(NULL) - begin_time;
44333cb34c60Sahrens if (delta == 0)
44343cb34c60Sahrens delta = 1;
44356520eed5SToomas Soome zfs_nicebytes(bytes, buf1, sizeof (buf1));
44366520eed5SToomas Soome zfs_nicebytes(bytes / delta, buf2, sizeof (buf2));
44373cb34c60Sahrens
44386520eed5SToomas Soome (void) printf("received %s stream in %lu seconds (%s/sec)\n",
44393cb34c60Sahrens buf1, delta, buf2);
44403cb34c60Sahrens }
44413cb34c60Sahrens
4442eb633035STom Caputi err = 0;
4443eb633035STom Caputi out:
4444eb633035STom Caputi
4445eb633035STom Caputi if (tmp_keylocation[0] != '\0') {
44466ccda740Sloli VERIFY(0 == nvlist_add_string(rcvprops,
4447eb633035STom Caputi zfs_prop_to_name(ZFS_PROP_KEYLOCATION), tmp_keylocation));
4448eb633035STom Caputi }
4449eb633035STom Caputi
4450eb633035STom Caputi if (newprops)
44516ccda740Sloli nvlist_free(rcvprops);
44526ccda740Sloli
44536ccda740Sloli nvlist_free(oxprops);
44546ccda740Sloli nvlist_free(origprops);
4455eb633035STom Caputi
4456eb633035STom Caputi return (err);
44573cb34c60Sahrens }
44583cb34c60Sahrens
44596ccda740Sloli /*
44606ccda740Sloli * Check properties we were asked to override (both -o|-x)
44616ccda740Sloli */
44626ccda740Sloli static boolean_t
zfs_receive_checkprops(libzfs_handle_t * hdl,nvlist_t * props,const char * errbuf)44636ccda740Sloli zfs_receive_checkprops(libzfs_handle_t *hdl, nvlist_t *props,
44646ccda740Sloli const char *errbuf)
44656ccda740Sloli {
44666ccda740Sloli nvpair_t *nvp;
44676ccda740Sloli zfs_prop_t prop;
44686ccda740Sloli const char *name;
44696ccda740Sloli
44706ccda740Sloli nvp = NULL;
44716ccda740Sloli while ((nvp = nvlist_next_nvpair(props, nvp)) != NULL) {
44726ccda740Sloli name = nvpair_name(nvp);
44736ccda740Sloli prop = zfs_name_to_prop(name);
44746ccda740Sloli
44756ccda740Sloli if (prop == ZPROP_INVAL) {
44766ccda740Sloli if (!zfs_prop_user(name)) {
44776ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
44786ccda740Sloli "invalid property '%s'"), name);
44796ccda740Sloli return (B_FALSE);
44806ccda740Sloli }
44816ccda740Sloli continue;
44826ccda740Sloli }
44836ccda740Sloli /*
44846ccda740Sloli * "origin" is readonly but is used to receive datasets as
44856ccda740Sloli * clones so we don't raise an error here
44866ccda740Sloli */
44876ccda740Sloli if (prop == ZFS_PROP_ORIGIN)
44886ccda740Sloli continue;
44896ccda740Sloli
44906ccda740Sloli /* encryption params have their own verification later */
44916ccda740Sloli if (prop == ZFS_PROP_ENCRYPTION ||
44926ccda740Sloli zfs_prop_encryption_key_param(prop))
44936ccda740Sloli continue;
44946ccda740Sloli
44956ccda740Sloli /*
44966ccda740Sloli * cannot override readonly, set-once and other specific
44976ccda740Sloli * settable properties
44986ccda740Sloli */
44996ccda740Sloli if (zfs_prop_readonly(prop) || prop == ZFS_PROP_VERSION ||
45006ccda740Sloli prop == ZFS_PROP_VOLSIZE) {
45016ccda740Sloli zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
45026ccda740Sloli "invalid property '%s'"), name);
45036ccda740Sloli return (B_FALSE);
45046ccda740Sloli }
45056ccda740Sloli }
45066ccda740Sloli
45076ccda740Sloli return (B_TRUE);
45086ccda740Sloli }
45096ccda740Sloli
45100069fd67STim Haley static int
zfs_receive_impl(libzfs_handle_t * hdl,const char * tosnap,const char * originsnap,recvflags_t * flags,int infd,const char * sendfs,nvlist_t * stream_nv,avl_tree_t * stream_avl,char ** top_zfs,int cleanup_fd,uint64_t * action_handlep,const char * finalsnap,nvlist_t * cmdprops)4511a2cdcdd2SPaul Dagnelie zfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
4512a2cdcdd2SPaul Dagnelie const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
4513a2cdcdd2SPaul Dagnelie nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
4514a60ca23dSTom Caputi uint64_t *action_handlep, const char *finalsnap, nvlist_t *cmdprops)
45153cb34c60Sahrens {
45163cb34c60Sahrens int err;
45173cb34c60Sahrens dmu_replay_record_t drr, drr_noswap;
45183cb34c60Sahrens struct drr_begin *drrb = &drr.drr_u.drr_begin;
45193cb34c60Sahrens char errbuf[1024];
45203cb34c60Sahrens zio_cksum_t zcksum = { 0 };
45219e69d7d0SLori Alt uint64_t featureflags;
45229e69d7d0SLori Alt int hdrtype;
45233cb34c60Sahrens
45243cb34c60Sahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
45253cb34c60Sahrens "cannot receive"));
45263cb34c60Sahrens
45276ccda740Sloli /* check cmdline props, raise an error if they cannot be received */
45286ccda740Sloli if (!zfs_receive_checkprops(hdl, cmdprops, errbuf)) {
45296ccda740Sloli return (zfs_error(hdl, EZFS_BADPROP, errbuf));
45306ccda740Sloli }
45316ccda740Sloli
453219b94df9SMatthew Ahrens if (flags->isprefix &&
45333cb34c60Sahrens !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
45343cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
45353cb34c60Sahrens "(%s) does not exist"), tosnap);
45363cb34c60Sahrens return (zfs_error(hdl, EZFS_NOENT, errbuf));
45373cb34c60Sahrens }
4538a2cdcdd2SPaul Dagnelie if (originsnap &&
4539a2cdcdd2SPaul Dagnelie !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
4540a2cdcdd2SPaul Dagnelie zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
4541a2cdcdd2SPaul Dagnelie "(%s) does not exist"), originsnap);
4542a2cdcdd2SPaul Dagnelie return (zfs_error(hdl, EZFS_NOENT, errbuf));
4543a2cdcdd2SPaul Dagnelie }
45443cb34c60Sahrens
45453cb34c60Sahrens /* read in the BEGIN record */
45463cb34c60Sahrens if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
45473cb34c60Sahrens &zcksum)))
45483cb34c60Sahrens return (err);
45493cb34c60Sahrens
45503cb34c60Sahrens if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
45513cb34c60Sahrens /* It's the double end record at the end of a package */
45523cb34c60Sahrens return (ENODATA);
45533cb34c60Sahrens }
45543cb34c60Sahrens
45553cb34c60Sahrens /* the kernel needs the non-byteswapped begin record */
45563cb34c60Sahrens drr_noswap = drr;
45573cb34c60Sahrens
455819b94df9SMatthew Ahrens flags->byteswap = B_FALSE;
45593cb34c60Sahrens if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
45603cb34c60Sahrens /*
45613cb34c60Sahrens * We computed the checksum in the wrong byteorder in
45623cb34c60Sahrens * recv_read() above; do it again correctly.
45633cb34c60Sahrens */
45643cb34c60Sahrens bzero(&zcksum, sizeof (zio_cksum_t));
4565770499e1SDan Kimmel (void) fletcher_4_incremental_byteswap(&drr,
4566770499e1SDan Kimmel sizeof (drr), &zcksum);
456719b94df9SMatthew Ahrens flags->byteswap = B_TRUE;
45683cb34c60Sahrens
45693cb34c60Sahrens drr.drr_type = BSWAP_32(drr.drr_type);
45703cb34c60Sahrens drr.drr_payloadlen = BSWAP_32(drr.drr_payloadlen);
45713cb34c60Sahrens drrb->drr_magic = BSWAP_64(drrb->drr_magic);
45729e69d7d0SLori Alt drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
45733cb34c60Sahrens drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
45743cb34c60Sahrens drrb->drr_type = BSWAP_32(drrb->drr_type);
45753cb34c60Sahrens drrb->drr_flags = BSWAP_32(drrb->drr_flags);
45763cb34c60Sahrens drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
45773cb34c60Sahrens drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
45783cb34c60Sahrens }
45793cb34c60Sahrens
45803cb34c60Sahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
45813cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
45823cb34c60Sahrens "stream (bad magic number)"));
45833cb34c60Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
45843cb34c60Sahrens }
45853cb34c60Sahrens
45869e69d7d0SLori Alt featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
45879e69d7d0SLori Alt hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
45889e69d7d0SLori Alt
45899e69d7d0SLori Alt if (!DMU_STREAM_SUPPORTED(featureflags) ||
45909e69d7d0SLori Alt (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
45919e69d7d0SLori Alt zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
45929e69d7d0SLori Alt "stream has unsupported feature, feature flags = %lx"),
45939e69d7d0SLori Alt featureflags);
45949e69d7d0SLori Alt return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
45959e69d7d0SLori Alt }
45969e69d7d0SLori Alt
45976ccda740Sloli /* Holds feature is set once in the compound stream header. */
45986ccda740Sloli boolean_t holds = (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
45996ccda740Sloli DMU_BACKUP_FEATURE_HOLDS);
46006ccda740Sloli if (holds)
46016ccda740Sloli flags->holds = B_TRUE;
46026ccda740Sloli
46033cb34c60Sahrens if (strchr(drrb->drr_toname, '@') == NULL) {
46043cb34c60Sahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
46053cb34c60Sahrens "stream (bad snapshot name)"));
46063cb34c60Sahrens return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
46073cb34c60Sahrens }
46083cb34c60Sahrens
46099e69d7d0SLori Alt if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
46109adfa60dSMatthew Ahrens char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
4611bd6a198fSTom Erickson if (sendfs == NULL) {
4612bd6a198fSTom Erickson /*
4613bd6a198fSTom Erickson * We were not called from zfs_receive_package(). Get
4614bd6a198fSTom Erickson * the fs specified by 'zfs send'.
4615bd6a198fSTom Erickson */
4616bd6a198fSTom Erickson char *cp;
4617bd6a198fSTom Erickson (void) strlcpy(nonpackage_sendfs,
46189adfa60dSMatthew Ahrens drr.drr_u.drr_begin.drr_toname,
46199adfa60dSMatthew Ahrens sizeof (nonpackage_sendfs));
4620bd6a198fSTom Erickson if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
4621bd6a198fSTom Erickson *cp = '\0';
4622bd6a198fSTom Erickson sendfs = nonpackage_sendfs;
46235878fad7SDan McDonald VERIFY(finalsnap == NULL);
4624bd6a198fSTom Erickson }
4625a2cdcdd2SPaul Dagnelie return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
4626a2cdcdd2SPaul Dagnelie &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
4627a60ca23dSTom Caputi cleanup_fd, action_handlep, finalsnap, cmdprops));
462883d7f9feSTom Erickson } else {
46299e69d7d0SLori Alt assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
46309e69d7d0SLori Alt DMU_COMPOUNDSTREAM);
4631a2cdcdd2SPaul Dagnelie return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
4632a60ca23dSTom Caputi &zcksum, top_zfs, cleanup_fd, action_handlep, cmdprops));
46333cb34c60Sahrens }
46343cb34c60Sahrens }
46350069fd67STim Haley
46360069fd67STim Haley /*
46370069fd67STim Haley * Restores a backup of tosnap from the file descriptor specified by infd.
46380069fd67STim Haley * Return 0 on total success, -2 if some things couldn't be
46390069fd67STim Haley * destroyed/renamed/promoted, -1 if some things couldn't be received.
46409c3fd121SMatthew Ahrens * (-1 will override -2, if -1 and the resumable flag was specified the
46419c3fd121SMatthew Ahrens * transfer can be resumed if the sending side supports it).
46420069fd67STim Haley */
46430069fd67STim Haley int
zfs_receive(libzfs_handle_t * hdl,const char * tosnap,nvlist_t * props,recvflags_t * flags,int infd,avl_tree_t * stream_avl)4644a2cdcdd2SPaul Dagnelie zfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
4645a2cdcdd2SPaul Dagnelie recvflags_t *flags, int infd, avl_tree_t *stream_avl)
46460069fd67STim Haley {
46470069fd67STim Haley char *top_zfs = NULL;
46480069fd67STim Haley int err;
4649c99e4bdcSChris Kirby int cleanup_fd;
4650c99e4bdcSChris Kirby uint64_t action_handle = 0;
4651a2cdcdd2SPaul Dagnelie char *originsnap = NULL;
4652a2cdcdd2SPaul Dagnelie if (props) {
4653a2cdcdd2SPaul Dagnelie err = nvlist_lookup_string(props, "origin", &originsnap);
4654a2cdcdd2SPaul Dagnelie if (err && err != ENOENT)
4655a2cdcdd2SPaul Dagnelie return (err);
4656a2cdcdd2SPaul Dagnelie }
4657c99e4bdcSChris Kirby
4658c99e4bdcSChris Kirby cleanup_fd = open(ZFS_DEV, O_RDWR|O_EXCL);
4659c99e4bdcSChris Kirby VERIFY(cleanup_fd >= 0);
46600069fd67STim Haley
4661a2cdcdd2SPaul Dagnelie err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
4662a60ca23dSTom Caputi stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL, props);
4663c99e4bdcSChris Kirby
4664c99e4bdcSChris Kirby VERIFY(0 == close(cleanup_fd));
46650069fd67STim Haley
466619b94df9SMatthew Ahrens if (err == 0 && !flags->nomount && top_zfs) {
46670069fd67STim Haley zfs_handle_t *zhp;
46680069fd67STim Haley prop_changelist_t *clp;
46690069fd67STim Haley
46700069fd67STim Haley zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
46710069fd67STim Haley if (zhp != NULL) {
46720069fd67STim Haley clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT,
46730069fd67STim Haley CL_GATHER_MOUNT_ALWAYS, 0);
46740069fd67STim Haley zfs_close(zhp);
46750069fd67STim Haley if (clp != NULL) {
46760069fd67STim Haley /* mount and share received datasets */
46770069fd67STim Haley err = changelist_postfix(clp);
46780069fd67STim Haley changelist_free(clp);
46790069fd67STim Haley }
46800069fd67STim Haley }
46810069fd67STim Haley if (zhp == NULL || clp == NULL || err)
46820069fd67STim Haley err = -1;
46830069fd67STim Haley }
46840069fd67STim Haley if (top_zfs)
46850069fd67STim Haley free(top_zfs);
46860069fd67STim Haley
46870069fd67STim Haley return (err);
46880069fd67STim Haley }
4689