Print this page
NEX-15090 Due to mismerge "zfs send" command cannot generate compressed stream
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
NEX-15090 Due to mismerge "zfs send" command cannot generate compressed stream
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
NEX-9732 Infinite loop in zfs receive (lint fix)
NEX-9732 Infinite loop in zfs receive
NEX-10194 replication Assertion failed: 0 == nvlist_lookup_string(nvfs, "name", &fsname)
NEX-5300 ZFS receive failed: no error
NEX-10201 Replication successfully completed but zfs receive exit with status 1 and without error message
Reviewed by: Eugene Khudyakoff <eugene.khudyakoff@nexenta.com>
Reviewed by: Dan Fields <dan.fields@nexenta.com>
Reviewed by: Steve Peng <steve.peng@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
Reviewed by: Cynthia Eastham <cynthia.eastham@nexenta.com>
NEX-1520 Incorrect behavior of ZFS recv in case of incremental recursive replication to a cloned FS
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
Reviewed by: Saso Kiselkov <saso.kiselkov@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-6115 Segmentation Fault on zfs recv -x property command
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
NEX-5928 KRRP: Integrate illumos/openzfs resume-token, to resume replication from a given synced offset
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Alexey Komarov <alexey.komarov@nexenta.com>
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
NEX-5728 Autosync Destination retention policy not being honoured
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
NEX-5795 Rename 'wrc' as 'wbc' in the source and in the tech docs
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
NEX-5239 Recursive auto-sync may get broken if new child datasets are added between the job's runs
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Roman Strashkin <roman.strashkin@nexenta.com>
NEX-5272 KRRP: replicate snapshot properties
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
Reviewed by: Alexey Komarov <alexey.komarov@nexenta.com>
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
NEX-5270 WBC: Incorrect error message when trying to 'zfs recv' into wrcached dataset
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Sanjay Nadkarni <sanjay.nadkarni@nexenta.com>
4986 receiving replication stream fails if any snapshot exceeds refquota
Reviewed by: John Kennedy <john.kennedy@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Approved by: Gordon Ross <gordon.ross@nexenta.com>
6298 zfs_create_008_neg and zpool_create_023_neg need to be updated for large block support
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: John Kennedy <john.kennedy@delphix.com>
Approved by: Robert Mustacchi <rm@joyent.com>
2605 want to resume interrupted zfs send
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Paul Dagnelie <pcd@delphix.com>
Reviewed by: Richard Elling <Richard.Elling@RichardElling.com>
Reviewed by: Xin Li <delphij@freebsd.org>
Reviewed by: Arne Jansen <sensille@gmx.net>
Approved by: Dan McDonald <danmcd@omniti.com>
NEX-4582 update wrc test cases for allow to use write back cache per tree of datasets
Reviewed by: Steve Peng <steve.peng@nexenta.com>
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
5960 zfs recv should prefetch indirect blocks
5925 zfs receive -o origin=
Reviewed by: Prakash Surya <prakash.surya@delphix.com>
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
5746 more checksumming in zfs send
Reviewed by: Christopher Siden <christopher.siden@delphix.com>
Reviewed by: George Wilson <george.wilson@delphix.com>
Reviewed by: Bayard Bell <buffer.g.overflow@gmail.com>
Approved by: Albert Lee <trisk@omniti.com>
5764 "zfs send -nv" directs output to stderr
Reviewed by: Matthew Ahrens <mahrens@delphix.com>
Reviewed by: Paul Dagnelie <paul.dagnelie@delphix.com>
Reviewed by: Basil Crow <basil.crow@delphix.com>
Reviewed by: Steven Hartland <killing@multiplay.co.uk>
Reviewed by: Bayard Bell <buffer.g.overflow@gmail.com>
Approved by: Dan McDonald <danmcd@omniti.com>
NEX-4476 WRC: Allow to use write back cache per tree of datasets
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
Revert "NEX-4476 WRC: Allow to use write back cache per tree of datasets"
This reverts commit fe97b74444278a6f36fec93179133641296312da.
NEX-4476 WRC: Allow to use write back cache per tree of datasets
Reviewed by: Alek Pinchuk <alek.pinchuk@nexenta.com>
Reviewed by: Alex Aizman <alex.aizman@nexenta.com>
NEX-1456 Part 2, port FreeBSD patch - new zfs recv options support
Bug 10481 - cstyle/lint cleanup
Bug 10481 - Dry run option in 'zfs send' isn't the same as in NexentaStor 3.1

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