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