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