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