3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * CDDL HEADER START
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * The contents of this file are subject to the terms of the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Common Development and Distribution License (the "License").
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * You may not use this file except in compliance with the License.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * See the License for the specific language governing permissions
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * and limitations under the License.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * When distributing Covered Code, include this CDDL HEADER in each
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * If applicable, add the following below this CDDL HEADER, with the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * fields enclosed by brackets "[]" replaced with your own identifying
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * CDDL HEADER END
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski * Copyright (c) 2012, Joyent, Inc. All rights reserved.
0d8fa8f8eba3ea46bc79d73445009505d1dd5d7dMartin Matuska * Copyright (c) 2012 Pawel Jakub Dawidek. All rights reserved.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * Copyright (c) 2013 Steven Hartland. All rights reserved.
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
88f61dee20b358671b1b643e9d1dbf220a1d69beIgor Kozhukhov * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com>
92241e0b80813d0b83c08e730a29b9d1831794fcTom Ericksonextern void zfs_setprop_error(libzfs_handle_t *, zfs_prop_t, int, char *);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestatic int zfs_receive_impl(libzfs_handle_t *, const char *, const char *,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie recvflags_t *, int, const char *, nvlist_t *, avl_tree_t *, char **, int,
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald uint64_t *, const char *);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensstatic int guid_to_name(libzfs_handle_t *, const char *,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt uint64_t max_ddt_size; /* max dedup table size in bytes */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt uint64_t cur_ddt_size; /* current dedup table size in bytes */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (0);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Altddt_hash_append(libzfs_handle_t *hdl, dedup_table_t *ddt, dedup_entry_t **ddepp,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt "Dedup table full. Deduplication will continue "
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt "with existing table entries"));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if ((dde = umem_cache_alloc(ddt->ddecache, UMEM_DEFAULT))
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * Using the specified dedup table, do a lookup for an entry with
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * the checksum cs. If found, return the block's reference info
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * in *dr. Otherwise, insert a new entry in the dedup table, using
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * the reference information specified by *dr.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * return value: true - entry was found
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * false - entry was not found
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Altddt_update(libzfs_handle_t *hdl, dedup_table_t *ddt, zio_cksum_t *cs,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt hashcode = BF64_GET(cs->zc_word[0], 0, ddt->numhashbits);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt for (ddepp = &(ddt->dedup_hash_array[hashcode]); *ddepp != NULL;
8e7144747a94247ce9dbb4fb1f67bdcb666b30d8Lori Alt if (ZIO_CHECKSUM_EQUAL(((*ddepp)->dde_chksum), *cs) &&
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrensdump_record(dmu_replay_record_t *drr, void *payload, int payload_len,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum), zc);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens fletcher_4_incremental_native(&drr->drr_u.drr_checksum.drr_checksum,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens fletcher_4_incremental_native(payload, payload_len, zc);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (write(outfd, payload, payload_len) == -1)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * This function is started in a separate thread when the dedup option
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * has been requested. The main send thread determines the list of
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * snapshots to be included in the send stream and makes the ioctl calls
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * for each one. But instead of having the ioctl send the output to the
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * the output fd specified by the caller of zfs_send()), the
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * ioctl is told to direct the output to a pipe, which is read by the
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * alternate thread running THIS function. This function does the
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * dedup'ing by:
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * 1. building a dedup table (the DDT)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * 2. doing checksums on each data block and inserting a record in the DDT
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * 3. looking for matching checksums, and
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * 4. sending a DRR_WRITE_BYREF record instead of a write record whenever
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * a duplicate block is found.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * The output of this function then goes to the output fd requested
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * by the caller of zfs_send().
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Altstatic void *
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens char *buf = zfs_alloc(dda->dedup_hdl, SPA_MAXBLOCKSIZE);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt uint64_t physmem = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens MAX((physmem * MAX_DDT_PHYSMEM_PERCENT) / 100,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens numbuckets = ddt.max_ddt_size / (sizeof (dedup_entry_t));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * numbuckets must be a power of 2. Increase number to
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * a power of 2 if necessary.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt ddt.dedup_hash_array = calloc(numbuckets, sizeof (dedup_entry_t *));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt ddt.ddecache = umem_cache_create("dde", sizeof (dedup_entry_t), 0,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt ddt.cur_ddt_size = numbuckets * sizeof (dedup_entry_t *);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens while (ssread(drr, sizeof (*drr), ofp) != 0) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens struct drr_begin *drrb = &drr->drr_u.drr_begin;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* set the DEDUP feature flag for this stream */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt fflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, fflags);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* use the recalculated checksum */
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens struct drr_object *drro = &drr->drr_u.drr_object;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens struct drr_spill *drrs = &drr->drr_u.drr_spill;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens struct drr_write *drrw = &drr->drr_u.drr_write;
8e7144747a94247ce9dbb4fb1f67bdcb666b30d8Lori Alt * Use the existing checksum if it's dedup-capable,
8e7144747a94247ce9dbb4fb1f67bdcb666b30d8Lori Alt * else calculate a SHA256 checksum for it.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* block already present in stream */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* block not previously seen */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens P2ROUNDUP((uint64_t)drrwe->drr_psize, 8), ofp);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens (void) fprintf(stderr, "INVALID record type 0x%x\n",
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* should never happen, so assert */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Routines for dealing with the AVL tree of fs-nvlists
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (+1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (-1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Given the GUID of a snapshot, find its containing filesystem and
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * (optionally) name.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensfsavl_find(avl_tree_t *avl, uint64_t snapguid, char **snapname)
00954d7bf18589273e08e0d7fa824c69ce96e70bahlstatic void
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * Given an nvlist, produce an avl tree of snapshots, ordered by guid
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens avl_create(fsavl, fsavl_compare, sizeof (fsavl_node_t),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens while ((fselem = nvlist_next_nvpair(fss, fselem)) != NULL) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Note: if there are multiple snaps with the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * same GUID, we ignore all but one.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Routines for dealing with the giant nvlist of fs-nvlists, etc.
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * assigned inside every recursive call,
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * restored from *_save on return:
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * guid of fromsnap snapshot in parent dataset
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * txg of fromsnap snapshot in current dataset
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * txg of tosnap snapshot in current dataset
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter /* the nvlists get accumulated during depth-first traversal */
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter /* send-receive configuration, does not change during traversal */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * The header nvlist is of the following format:
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "tosnap" -> string
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "fromsnap" -> string (if incremental)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "fss" -> {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "name" -> string (full name; for debugging)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "parentfromsnap" -> number (guid of fromsnap in parent)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "props" -> { name -> value (only if set here) }
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "snaps" -> { name (lastname) -> number (guid) }
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrens * "snapprops" -> { name (lastname) -> { name -> value } }
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "origin" -> number (guid) (if clone)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * "sent" -> boolean (not on-disk)
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrensstatic void send_iterate_prop(zfs_handle_t *zhp, nvlist_t *nv);
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter "skipping snapshot %s because it was created "
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter "after the destination snapshot (%s)\n"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_add_uint64(sd->parent_snaps, snapname, guid));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * NB: if there is no fromsnap here (it's a newly created fs in
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * an incremental replication), we will substitute the tosnap.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if ((sd->fromsnap && strcmp(snapname, sd->fromsnap) == 0) ||
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrens VERIFY(0 == nvlist_add_nvlist(sd->snapprops, snapname, nv));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens while ((elem = nvlist_next_nvpair(zhp->zfs_props, elem)) != NULL) {
faaa641552b99562194e991c932a94074682407bEric Schrock * Realistically, this should never happen. However,
faaa641552b99562194e991c932a94074682407bEric Schrock * we want the ability to add DSL properties without
faaa641552b99562194e991c932a94074682407bEric Schrock * needing to make incompatible version changes. We
faaa641552b99562194e991c932a94074682407bEric Schrock * need to ignore unknown properties to allow older
faaa641552b99562194e991c932a94074682407bEric Schrock * software to still send datasets containing these
faaa641552b99562194e991c932a94074682407bEric Schrock * properties, with the unknown properties elided.
166773ebc7a1af7f0d7996ebf321a3e96727fefbSanjeev Bagewadi if (prop == ZFS_PROP_QUOTA || prop == ZFS_PROP_RESERVATION ||
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson * May have no source before SPA_VERSION_RECVD_PROPS,
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson * but is still modifiable.
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * returns snapshot creation txg
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * and returns 0 if the snapshot does not exist
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiterget_snap_txg(libzfs_handle_t *hdl, const char *fs, const char *snap)
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter if (fs == NULL || fs[0] == '\0' || snap == NULL || snap[0] == '\0')
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter (void) snprintf(name, sizeof (name), "%s@%s", fs, snap);
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter if (zfs_dataset_exists(hdl, name, ZFS_TYPE_SNAPSHOT)) {
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter zfs_handle_t *zhp = zfs_open(hdl, name, ZFS_TYPE_SNAPSHOT);
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter txg = zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG);
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * recursively generate nvlists describing datasets. See comment
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * for the data structure send_data_t above for description of contents
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * of the nvlist.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens uint64_t parent_fromsnap_guid_save = sd->parent_fromsnap_guid;
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter uint64_t txg = zhp->zfs_dmustats.dds_creation_txg;
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter fromsnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->fromsnap);
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter tosnap_txg = get_snap_txg(zhp->zfs_hdl, zhp->zfs_name, sd->tosnap);
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * on the send side, if the current dataset does not have tosnap,
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * perform two additional checks:
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * - skip sending the current dataset if it was created later than
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * the parent tosnap
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * - return error if the current dataset was created earlier than
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter * the parent tosnap
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter if (sd->tosnap_txg != 0 && txg > sd->tosnap_txg) {
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter "skipping dataset %s: snapshot %s does "
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter "cannot send %s@%s%s: snapshot %s@%s does not "
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter "exist\n"), sd->fsname, sd->tosnap, sd->recursive ?
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_add_string(nvfs, "name", zhp->zfs_name));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_add_uint64(nvfs, "parentfromsnap",
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* iterate over props */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* iterate over snaps, and set sd->parent_fromsnap_guid */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_alloc(&sd->parent_snaps, NV_UNIQUE_NAME, 0));
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrens VERIFY(0 == nvlist_alloc(&sd->snapprops, NV_UNIQUE_NAME, 0));
0d8fa8f8eba3ea46bc79d73445009505d1dd5d7dMartin Matuska (void) zfs_iter_snapshots(zhp, B_FALSE, send_iterate_snap, sd);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_add_nvlist(nvfs, "snaps", sd->parent_snaps));
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrens VERIFY(0 == nvlist_add_nvlist(nvfs, "snapprops", sd->snapprops));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* add this fs to nvlist */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_add_nvlist(sd->fss, guidstring, nvfs));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* iterate over children */
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson rv = zfs_iter_filesystems(zhp, send_iterate_fs, sd);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens sd->parent_fromsnap_guid = parent_fromsnap_guid_save;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensgather_nvlist(libzfs_handle_t *hdl, const char *fsname, const char *fromsnap,
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter const char *tosnap, boolean_t recursive, boolean_t verbose,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_alloc(&sd.fss, NV_UNIQUE_NAME, 0));
00954d7bf18589273e08e0d7fa824c69ce96e70bahl if (avlp != NULL && (*avlp = fsavl_create(sd.fss)) == NULL) {
00954d7bf18589273e08e0d7fa824c69ce96e70bahl return (0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Routines specific to "zfs send"
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* these are all just the short snapname (the part after the @) */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens boolean_t seenfrom, seento, replicate, doall, fromorigin;
dc5f28a3c341db7c241bba77ddc109c141072f27Manoj Joseph boolean_t verbose, dryrun, parsable, progress, embed_data, std_out;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrensestimate_ioctl(zfs_handle_t *zhp, uint64_t fromsnap_obj,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t fromorigin, enum lzc_send_flags flags, uint64_t *sizep)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens "warning: cannot estimate space for '%s'"), zhp->zfs_name);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens "not an earlier snapshot from the same fs"));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens "incremental source (@%s) does not exist"),
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens return (zfs_standard_error(hdl, errno, errbuf));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Dumps a backup of the given snapshot (incremental from fromsnap if it's not
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * NULL) to the file descriptor specified by outfd.
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirbydump_ioctl(zfs_handle_t *zhp, const char *fromsnap, uint64_t fromsnap_obj,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens boolean_t fromorigin, int outfd, enum lzc_send_flags flags,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby zc.zc_sendobj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling VERIFY(0 == nvlist_alloc(&thisdbg, NV_UNIQUE_NAME, 0));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_SEND, &zc) != 0) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling VERIFY(0 == nvlist_add_uint64(thisdbg, "error", errno));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "not an earlier snapshot from the same fs"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "incremental source (@%s) does not exist"),
3f9d6ad73e45c6823b409f93b0c8d4f62861d2d5Lin Ling VERIFY(0 == nvlist_add_nvlist(debugnv, zhp->zfs_name, thisdbg));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartlandgather_holds(zfs_handle_t *zhp, send_dump_data_t *sdd)
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * zfs_send() only sets snapholds for sends that need them,
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * e.g. replication and doall.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland fnvlist_add_string(sdd->snapholds, zhp->zfs_name, sdd->holdtag);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski (void) fprintf(stderr, "TIME SENT SNAPSHOT\n");
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski * Print the progress from ZFS_IOC_SEND_PROGRESS every second.
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (zfs_ioctl(hdl, ZFS_IOC_SEND_PROGRESS, &zc) != 0)
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski return ((void *)-1);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski (void) fprintf(stderr, "%02d:%02d:%02d\t%llu\t%s\n",
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski (void) fprintf(stderr, "%02d:%02d:%02d %5s %s\n",
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrenssend_print_verbose(FILE *fout, const char *tosnap, const char *fromsnap,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "send from @%s to %s"),
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "send from %s to %s"),
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "full send of %s"),
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson istosnap = (strcmp(sdd->tosnap, thissnap) == 0);
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson * Filter out all intermediate snapshots except origin
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson * snapshots needed to replicate clones.
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson exclude = !nvlist_exists(snapprops, "is_clone_origin");
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * If a filter function exists, call it to determine whether
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * this snapshot will be sent.
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson sdd->filter_cb(zhp, sdd->filter_cb_arg) == B_FALSE)) {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * This snapshot is filtered out. Don't send it, and don't
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby * set prevsnap_obj, so it will be as if this snapshot didn't
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * exist, and the next accepted snapshot will be sent as
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * an incremental from the last accepted one, or as the
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * first (and full) snapshot in the case of a replication,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * non-incremental send.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) estimate_ioctl(zhp, sdd->prevsnap_obj,
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski * If progress reporting is requested, spawn a new thread to
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens err = dump_ioctl(zhp, sdd->prevsnap, sdd->prevsnap_obj,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens fromorigin, sdd->outfd, flags, sdd->debugnv);
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby sdd->prevsnap_obj = zfs_prop_get_int(zhp, ZFS_PROP_OBJSETID);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens "WARNING: could not send %s@%s: does not exist\n"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * If this fs does not have fromsnap, and we're doing
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * recursive, we need to send a full stream from the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * beginning (or an incremental from the origin if this
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * is a clone). If we're doing non-recursive, then let
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * them get the error.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(zc.zc_name, sizeof (zc.zc_name), "%s@%s",
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson sdd->seenfrom = sdd->seento = sdd->prevsnap[0] = 0;
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson rv = zfs_iter_snapshots_sorted(zhp, dump_snapshot, arg);
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson "WARNING: could not send %s@%s:\n"
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens "incremental source (%s@%s) does not exist\n"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "WARNING: could not send %s@%s:\n"
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson "incremental source (%s@%s) "
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens "is not earlier than it\n"),
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens "could not send %s@%s: does not exist\n"),
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson /* Mark the clone origin snapshots. */
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson VERIFY(0 == nvpair_value_nvlist(fspair, &nvfs));
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson (void) nvlist_lookup_uint64(nvfs, "origin", &origin_guid);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(nvlist_lookup_string(fslist, "name", &fsname) == 0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) nvlist_lookup_uint64(fslist, "origin", &origin_guid);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens (void) nvlist_lookup_uint64(fslist, "parentfromsnap",
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens parent_nv = fsavl_find(sdd->fsavl, parent_guid, NULL);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /* parent has not been sent; skip this one */
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson * origin has not been sent yet;
468db2f465c2cf57998a476fec1d007c91f1da8aTom Erickson * skip this clone.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens zhp = zfs_open(rzhp->zfs_hdl, fsname, ZFS_TYPE_DATASET);
04bb726ecc2ce091dd01d32316d6730863741df0ahl return (-1);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /* clean out the sent flags in case we reuse this fss */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens for (fspair = nvlist_next_nvpair(sdd->fss, NULL); fspair;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens fspair = nvlist_next_nvpair(sdd->fss, fspair)) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens VERIFY(nvpair_value_nvlist(fspair, &fslist) == 0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrenszfs_send_resume_token_to_nvlist(libzfs_handle_t *hdl, const char *token)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Decode token header, which is:
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * <token version>-<checksum of payload>-<uncompressed payload length>
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Note that the only supported token version is 1.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume token is corrupt (invalid format)"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (version != ZFS_SEND_RESUME_TOKEN_VERSION) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume token is corrupt (invalid version %u)"),
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* convert hexadecimal representation to binary */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens unsigned char *compressed = zfs_alloc(hdl, len);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens for (int i = 0; i < len; i++) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nread = sscanf(token + i * 2, "%2hhx", compressed + i);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume token is corrupt "
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "(payload is not hex-encoded)"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* verify checksum */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fletcher_4_native(compressed, len, NULL, &cksum);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume token is corrupt (incorrect checksum)"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* uncompress */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (uncompress(packed, &packed_len_long, compressed, len) != Z_OK ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume token is corrupt (decompression failed)"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* unpack nvlist */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens int error = nvlist_unpack(packed, packed_len, &nv, KM_SLEEP);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume token is corrupt (nvlist_unpack failed)"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrenszfs_send_resume(libzfs_handle_t *hdl, sendflags_t *flags, int outfd,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t resumeobj, resumeoff, toguid, fromguid, bytes;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "cannot resume send"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zfs_send_resume_token_to_nvlist(hdl, resume_token);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * zfs_error_aux has already been set by
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * zfs_send_resume_token_to_nvlist
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume token contents:\n"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (nvlist_lookup_string(resume_nvl, "toname", &toname) != 0 ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_lookup_uint64(resume_nvl, "object", &resumeobj) != 0 ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_lookup_uint64(resume_nvl, "offset", &resumeoff) != 0 ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_lookup_uint64(resume_nvl, "bytes", &bytes) != 0 ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_lookup_uint64(resume_nvl, "toguid", &toguid) != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume token is corrupt"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) nvlist_lookup_uint64(resume_nvl, "fromguid", &fromguid);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (flags->largeblock || nvlist_exists(resume_nvl, "largeblockok"))
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (flags->embed_data || nvlist_exists(resume_nvl, "embedok"))
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (flags->compress || nvlist_exists(resume_nvl, "compressok"))
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (guid_to_name(hdl, toname, toguid, B_FALSE, name) != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zfs_dataset_exists(hdl, toname, ZFS_TYPE_DATASET)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "'%s' is no longer the same snapshot used in "
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "'%s' used in the initial send no longer exists"),
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (zfs_error(hdl, EZFS_BADPATH, errbuf));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (zfs_error(hdl, EZFS_BADPATH, errbuf));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (guid_to_name(hdl, toname, fromguid, B_TRUE, name) != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "incremental source %#llx no longer exists"),
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (zfs_error(hdl, EZFS_BADPATH, errbuf));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens send_print_verbose(stderr, zhp->zfs_name, fromname,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * If progress reporting is requested, spawn a new thread to
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * poll ZFS_IOC_SEND_PROGRESS at a regular interval.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens error = lzc_send_resume(zhp->zfs_name, fromname, outfd,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "warning: cannot send '%s'"), zhp->zfs_name);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (zfs_standard_error(hdl, errno, errbuf));
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * Generate a send stream for the dataset identified by the argument zhp.
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * The content of the send stream is the snapshot identified by
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * 'tosnap'. Incremental streams are requested in two ways:
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * - from the snapshot identified by "fromsnap" (if non-null) or
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * - from the origin of the dataset identified by zhp, which must
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * be a clone. In this case, "fromsnap" is null and "fromorigin"
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * The send stream is recursive (i.e. dumps a hierarchy of snapshots) and
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * uses a special header (with a hdrtype field of DMU_COMPOUNDSTREAM)
88bb18d257c743ee382a1694604771c6b7dee061Lori Alt * if "replicate" is set. If "doall" is set, dump all the intermediate
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * snapshots. The DMU_COMPOUNDSTREAM header is used in the "doall"
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson * case too. If "props" is set, send properties.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrenszfs_send(zfs_handle_t *zhp, const char *fromsnap, const char *tosnap,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens sendflags_t *flags, int outfd, snapfilter_cb_t filter_func,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "zero-length incremental source"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf));
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
88f61dee20b358671b1b643e9d1dbf220a1d69beIgor Kozhukhov if ((err = pthread_create(&tid, NULL, cksummer, &dda)) != 0) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (flags->replicate || flags->doall || flags->props) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_alloc(&hdrnv, NV_UNIQUE_NAME, 0));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_add_string(hdrnv, "tosnap", tosnap));
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson "not_recursive"));
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter fromsnap, tosnap, flags->replicate, flags->verbose,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /* write first begin record */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens drr.drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens (void) snprintf(drr.drr_u.drr_begin.drr_toname,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens err = dump_record(&drr, packbuf, buflen, &zc, outfd);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /* write end record */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* dump each stream */
65fec9f6c72fa6b5f798dcf3831d94959ef5e281Christopher Siden * Some flags require that we place user holds on the datasets that are
65fec9f6c72fa6b5f798dcf3831d94959ef5e281Christopher Siden * being sent so they don't get destroyed during the send. We can skip
65fec9f6c72fa6b5f798dcf3831d94959ef5e281Christopher Siden * this step if the pool is imported read-only since the datasets cannot
65fec9f6c72fa6b5f798dcf3831d94959ef5e281Christopher Siden * be destroyed.
65fec9f6c72fa6b5f798dcf3831d94959ef5e281Christopher Siden if (!flags->dryrun && !zpool_get_prop_int(zfs_get_pool_handle(zhp),
65fec9f6c72fa6b5f798dcf3831d94959ef5e281Christopher Siden zfs_spa_version(zhp, &spa_version) == 0 &&
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby (void) snprintf(sdd.holdtag, sizeof (sdd.holdtag),
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby ".send-%d-%llu", getpid(), (u_longlong_t)holdseq);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland if (flags->verbose || sdd.snapholds != NULL) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Do a verbose no-op dry run to get all the verbose output
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * or to gather snapshot hold's before generating any data,
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland * then do a non-verbose real run to generate the streams.
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Ensure no snaps found is treated as an error. */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Skip the second run if dryrun was requested. */
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland err = zfs_hold_nvl(zhp, sdd.cleanup_fd, sdd.snapholds);
a7a845e4bf22fd1b2a284729ccd95c7370a0438cSteven Hartland /* Ensure no snaps found is treated as an error. */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (!flags->dryrun && (flags->replicate || flags->doall ||
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * write final end record. NB: want to do this even if
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * there was some error, because it might not be totally
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt err = zfs_standard_error(zhp->zfs_hdl, err, errbuf);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrenszfs_send_one(zfs_handle_t *zhp, const char *from, int fd,
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens "warning: cannot send '%s'"), zhp->zfs_name);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens err = lzc_send(zhp->zfs_name, from, fd, flags);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens "not an earlier snapshot from the same fs"));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens "incremental source (%s) does not exist"),
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens "target is busy; if a filesystem, "
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens "it must not be mounted"));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (zfs_error(hdl, EZFS_BADBACKUP, errbuf));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (zfs_standard_error(hdl, errno, errbuf));
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (err != 0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Routines specific to "zfs recv"
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensrecv_read(libzfs_handle_t *hdl, int fd, void *buf, int ilen,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens } while (rv > 0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "failed to read from stream"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (zfs_error(hdl, EZFS_BADSTREAM, dgettext(TEXT_DOMAIN,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "cannot receive")));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensrecv_read_nvlist(libzfs_handle_t *hdl, int fd, int len, nvlist_t **nvp,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (err != 0) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (err != 0) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "stream (malformed nvlist)"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensrecv_rename(libzfs_handle_t *hdl, const char *name, const char *tryname,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens int baselen, char *newname, recvflags_t *flags)
d8d7720025734ca1adbaa020f5015dc3d586478dahrens return (-1);
d8d7720025734ca1adbaa020f5015dc3d586478dahrens return (-1);
842727c2f41f01b380de4f5e787d905702870f23Chris Kirby (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) strlcpy(zc.zc_value, tryname, sizeof (zc.zc_value));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0 && strncmp(name + baselen, "recv-", 5) != 0) {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens (void) snprintf(newname, ZFS_MAX_DATASET_NAME_LEN,
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens "%.*srecv-%u-%u", baselen, name, getpid(), seq);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) strlcpy(zc.zc_value, newname, sizeof (zc.zc_value));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensrecv_destroy(libzfs_handle_t *hdl, const char *name, int baselen,
d8d7720025734ca1adbaa020f5015dc3d586478dahrens return (-1);
d8d7720025734ca1adbaa020f5015dc3d586478dahrens return (-1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) strlcpy(zc.zc_name, name, sizeof (zc.zc_name));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) printf("attempting destroy %s\n", zc.zc_name);
d8d7720025734ca1adbaa020f5015dc3d586478dahrens if (err == 0) {
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson * Deferred destroy might destroy the snapshot or only mark it to be
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson * destroyed later, and it returns success in either case.
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson if (err != 0 || (defer && zfs_dataset_exists(hdl, name,
d8d7720025734ca1adbaa020f5015dc3d586478dahrens err = recv_rename(hdl, name, NULL, baselen, newname, flags);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (slash = strrchr(zhp->zfs_name, '/')) != NULL &&
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (zfs_prop_get_int(zhp, ZFS_PROP_GUID) == gtnd->guid) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = zfs_iter_bookmarks(zhp, guid_to_name_cb, gtnd);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Attempt to find the local dataset associated with this guid. In the case of
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * multiple matches, we attempt to find the "best" match by searching
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * progressively larger portions of the hierarchy. This allows one to send a
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * tree of datasets individually and guarantee that we will find the source
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * guid within that hierarchy, even if there are multiple matches elsewhere.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensguid_to_name(libzfs_handle_t *hdl, const char *parent, uint64_t guid,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Search progressively larger portions of the hierarchy, starting
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * with the filesystem specified by 'parent'. This will
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * select the "most local" version of the origin snapshot in the case
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * that there are multiple matching snapshots in the system.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) strlcpy(pname, parent, sizeof (pname));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens for (; cp != NULL; cp = strrchr(pname, '/')) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /* Chop off the last component and open the parent */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zfs_handle_t *zhp = make_dataset_handle(hdl, pname);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens int err = guid_to_name_cb(zfs_handle_dup(zhp), >nd);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = zfs_iter_children(zhp, guid_to_name_cb, >nd);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = zfs_iter_bookmarks(zhp, guid_to_name_cb, >nd);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Remember the last portion of the dataset so we skip it next
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * time through (as we've already searched that portion of the
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * hierarchy).
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * Return +1 if guid1 is before guid2, 0 if they are the same, and -1 if
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens * guid1 is after guid2.
04bb726ecc2ce091dd01d32316d6730863741df0ahl return (0);
04bb726ecc2ce091dd01d32316d6730863741df0ahl return (1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens guid1hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
04bb726ecc2ce091dd01d32316d6730863741df0ahl return (-1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(buf, sizeof (buf), "%s@%s", fsname, snapname);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens guid2hdl = zfs_open(hdl, buf, ZFS_TYPE_SNAPSHOT);
04bb726ecc2ce091dd01d32316d6730863741df0ahl return (-1);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens create1 = zfs_prop_get_int(guid1hdl, ZFS_PROP_CREATETXG);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens create2 = zfs_prop_get_int(guid2hdl, ZFS_PROP_CREATETXG);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensrecv_incremental_replication(libzfs_handle_t *hdl, const char *tofs,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens recvflags_t *flags, nvlist_t *stream_nv, avl_tree_t *stream_avl,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap));
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if ((error = gather_nvlist(hdl, tofs, fromsnap, NULL,
4a20c933b148de8a1c1d3538391c64284e636653Alex Deiter recursive, B_FALSE, &local_nv, &local_avl)) != 0)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Process deletes and renames
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens uint64_t parent_fromsnap_guid, stream_parent_fromsnap_guid;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_lookup_nvlist(nvfs, "snaps", &snaps));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_lookup_string(nvfs, "name", &fsname));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_lookup_uint64(nvfs, "parentfromsnap",
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) nvlist_lookup_uint64(nvfs, "origin", &originguid);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * First find the stream's fs, so we can check for
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * a different origin (due to "zfs promote")
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens snapelem; snapelem = nvlist_next_nvpair(snaps, snapelem)) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens stream_nvfs = fsavl_find(stream_avl, thisguid, NULL);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* check for promote */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (stream_nvfs && originguid != stream_originguid) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* promote it! */
04bb726ecc2ce091dd01d32316d6730863741df0ahl return (-1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * We had/have the wrong origin, therefore our
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * list of snapshots is wrong. Need to handle
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * them on the next pass.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvpair_value_uint64(snapelem, &thisguid));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* check for delete */
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrens if (0 == nvlist_lookup_nvlist(stream_nvfs, "snapprops",
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* check for different snapname */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* check for delete */
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson "fromsnap (%s in stream); must have "
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson "been deleted locally; ignoring\n",
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * Check for rename. If the exact receive path is specified, it
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * does not count as a rename, but we still need to check the
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * datasets beneath it.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens stream_parent_fromsnap_guid != parent_fromsnap_guid) ||
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens ((flags->isprefix || strcmp(tofs, fsname) != 0) &&
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson (s1 != NULL) && (s2 != NULL) && strcmp(s1, s2) != 0)) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * NB: parent might not be found if we used the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * tosnap for stream_parent_fromsnap_guid,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * because the parent is a newly-created fs;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * we'll be able to rename it after we recv the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* do another pass to fix up temporary names */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrenszfs_receive_package(libzfs_handle_t *hdl, int fd, const char *destname,
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens recvflags_t *flags, dmu_replay_record_t *drr, zio_cksum_t *zc,
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby char **top_zfs, int cleanup_fd, uint64_t *action_handlep)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "cannot receive"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens assert(drr->drr_u.drr_begin.drr_magic == DMU_BACKUP_MAGIC);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt assert(DMU_GET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo) ==
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Read in the nvlist from the stream.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens error = recv_read_nvlist(hdl, fd, drr->drr_payloadlen,
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson "cannot specify snapshot name for multi-snapshot stream"));
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson error = zfs_error(hdl, EZFS_BADSTREAM, errbuf);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Read in the end record and verify checksum.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (0 != (error = recv_read(hdl, fd, &drre, sizeof (drre),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[0]);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[1]);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[2]);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens BSWAP_64(drre.drr_u.drr_end.drr_checksum.zc_word[3]);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (!ZIO_CHECKSUM_EQUAL(drre.drr_u.drr_end.drr_checksum, *zc)) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "incorrect header checksum"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) nvlist_lookup_string(stream_nv, "fromsnap", &fromsnap);
00954d7bf18589273e08e0d7fa824c69ce96e70bahl "couldn't allocate avl tree"));
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens (void) strlcpy(tofs, destname, sizeof (tofs));
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson struct drr_begin *drrb = &drr->drr_u.drr_begin;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* zfs_receive_one() will create_parents() */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (recursive && !flags->dryrun && !flags->nomount) {
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson softerr = recv_incremental_replication(hdl, tofs, flags,
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson /* Unmount renamed filesystems before receiving. */
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * Get the fs specified by the first path in the stream (the top level
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * specified by 'zfs send') and pass it to each invocation of
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * zfs_receive_one().
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson (void) strlcpy(sendfs, drr->drr_u.drr_begin.drr_toname,
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * Find the "sendsnap", the final snapshot in a replication
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * stream. zfs_receive_one() handles certain errors
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * differently, depending on if the contained stream is the
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * last one or not.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* Finally, receive each contained stream */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * we should figure out if it has a recoverable
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * error, in which case do a recv_skip() and drive on.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Note, if we fail due to already having this guid,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * zfs_receive_one() will take care of it (ie,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * recv_skip() and return 0).
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie error = zfs_receive_impl(hdl, destname, NULL, flags, fd,
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby sendfs, stream_nv, stream_avl, top_zfs, cleanup_fd,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens } while (error == 0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Now that we have the fs's they sent us, try the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * renames again.
137fa06774810a67312b8142740a4177af4c08dbahrens softerr = recv_incremental_replication(hdl, tofs, flags,
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson "1 more property could not be set\n"));
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson "%d more properties could not be set\n"), truncated);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensrecv_skip(libzfs_handle_t *hdl, int fd, boolean_t byteswap)
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens void *buf = zfs_alloc(hdl, SPA_MAXBLOCKSIZE);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt "cannot receive:"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* XXX would be great to use lseek if possible... */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens while (recv_read(hdl, fd, drr, sizeof (dmu_replay_record_t),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum drr->drr_u.drr_spill.drr_length, B_FALSE, NULL);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens P2ROUNDUP(drr->drr_u.drr_write_embedded.drr_psize,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt "invalid record type"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (-1);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensrecv_ecksum_set_aux(libzfs_handle_t *hdl, const char *target_snap,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "checksum mismatch or incomplete stream"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) strlcpy(target_fs, target_snap, sizeof (target_fs));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens int error = zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "checksum mismatch or incomplete stream.\n"
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "Partially received snapshot is saved.\n"
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "A resuming stream can be generated on the sending "
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "system by running:\n"
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens " zfs send -t %s"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Restores a backup of tosnap from the file descriptor specified by infd.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrenszfs_receive_one(libzfs_handle_t *hdl, int infd, const char *tosnap,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie const char *originsnap, recvflags_t *flags, dmu_replay_record_t *drr,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_replay_record_t *drr_noswap, const char *sendfs, nvlist_t *stream_nv,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald uint64_t *action_handlep, const char *finalsnap)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "cannot receive"));
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson recursive = (nvlist_lookup_boolean(stream_nv, "not_recursive") ==
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrens nvlist_t *fs = fsavl_find(stream_avl, drrb->drr_toguid,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens VERIFY(0 == nvlist_alloc(&props, NV_UNIQUE_NAME, 0));
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrens if (0 == nvlist_lookup_nvlist(fs, "snapprops", &props)) {
00954d7bf18589273e08e0d7fa824c69ce96e70bahl return (-1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Determine how much of the snapshot name stored in the stream
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * we are going to tack on to the name they specified on the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * command line, and how much we are going to chop off.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * If they specified a snapshot, chop the entire name stored in
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * the stream.
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * A filesystem was specified with -e. We want to tack on only
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * the tail of the sent snapshot path.
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid "
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson "argument - snapshot not allowed with -e"));
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf));
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * The tail is the poolname, so we need to
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * prepend a path separator.
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson chopprefix = drrb->drr_toname + (chopprefix - sendfs);
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * A filesystem was specified with -d. We want to tack on
f64930f5a99dc5cadc013502deedef5a6ef3971fTom Erickson * everything but the first element of the sent snapshot path
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * (all but the pool name).
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson "argument - snapshot not allowed with -d"));
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * If a filesystem was specified without -d or -e, we want to
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * tack on everything after the fs specified by 'zfs send'.
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson chopprefix = drrb->drr_toname + strlen(sendfs);
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson /* A snapshot was specified as an exact path (no -d or -e). */
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson "cannot specify snapshot name for multi-snapshot "
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson return (zfs_error(hdl, EZFS_BADSTREAM, errbuf));
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson chopprefix = drrb->drr_toname + strlen(drrb->drr_toname);
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson ASSERT(strstr(drrb->drr_toname, sendfs) == drrb->drr_toname);
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson ASSERT(chopprefix <= drrb->drr_toname + strlen(drrb->drr_toname));
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson ASSERT(chopprefix[0] == '/' || chopprefix[0] == '@' ||
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Determine name of destination snapshot, store in zc_value.
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson (void) strncat(zc.zc_value, chopprefix, sizeof (zc.zc_value));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * Determine the name of the origin snapshot, store in zc_string.
ed4e7a6a5cbc5e8986dc649ad54435210487b102Paul Dagnelie (void) strncpy(zc.zc_string, originsnap, sizeof (zc.zc_string));
ed4e7a6a5cbc5e8986dc649ad54435210487b102Paul Dagnelie (void) printf("using provided clone origin %s\n",
ed4e7a6a5cbc5e8986dc649ad54435210487b102Paul Dagnelie } else if (drrb->drr_flags & DRR_FLAG_CLONE) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drrb->drr_fromguid, B_FALSE, zc.zc_string) != 0) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "local origin for clone %s does not exist"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) printf("found clone origin %s\n", zc.zc_string);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens boolean_t resuming = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (drrb->drr_flags & DRR_FLAG_CLONE) || originsnap) && !resuming;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * if the parent fs does not exist, look for it based on
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * the parent snap GUID
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "cannot receive new filesystem stream"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (guid_to_name(hdl, zc.zc_name, parent_snapguid,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * if the fs does not exist, look for it based on the
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * fromsnap GUID
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "cannot receive incremental stream"));
83d7f9fe6bbb3e281a1ca55fdc5d1ec82ecc3e70Tom Erickson * If the exact receive path was specified and this is the
83d7f9fe6bbb3e281a1ca55fdc5d1ec82ecc3e70Tom Erickson * topmost path in the stream, then if the fs does not exist we
83d7f9fe6bbb3e281a1ca55fdc5d1ec82ecc3e70Tom Erickson * should look no further.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if ((flags->isprefix || (*(chopprefix = drrb->drr_toname +
83d7f9fe6bbb3e281a1ca55fdc5d1ec82ecc3e70Tom Erickson strlen(sendfs)) != '\0' && *chopprefix != '@')) &&
83d7f9fe6bbb3e281a1ca55fdc5d1ec82ecc3e70Tom Erickson !zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (guid_to_name(hdl, zc.zc_name, drrb->drr_fromguid,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (zfs_dataset_exists(hdl, zc.zc_name, ZFS_TYPE_DATASET)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Destination fs exists. It must be one of these cases:
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * - an incremental send stream
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * - the stream specifies a new fs (full stream or clone)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * and they want us to blow away the existing fs (and
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * have therefore specified -F and removed any snapshots)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * - we are resuming a failed receive.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "destination '%s' exists\n"
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "must specify -F to overwrite it"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (ioctl(hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "destination has snapshots (eg. %s)\n"
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "must destroy them to overwrite it"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (-1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "destination '%s' is a clone\n"
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "must destroy it to overwrite it"),
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (!flags->dryrun && zhp->zfs_type == ZFS_TYPE_FILESYSTEM &&
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* We can't do online recv in this case */
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, 0);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (-1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (-1);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * If we are resuming a newfs, set newfs here so that we will
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * mount it if the recv succeeds this time. We can tell
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * that it was a newfs on the first recv because the fs
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * itself will be inconsistent (if the fs existed when we
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * did the first recv, we would have received it into
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * .../%recv).
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (resuming && zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT))
00954d7bf18589273e08e0d7fa824c69ce96e70bahl * Destination filesystem does not exist. Therefore we better
00954d7bf18589273e08e0d7fa824c69ce96e70bahl * be creating a new filesystem (either from a full backup, or
00954d7bf18589273e08e0d7fa824c69ce96e70bahl * a clone). It would therefore be invalid if the user
00954d7bf18589273e08e0d7fa824c69ce96e70bahl * specified only the pool name (i.e. if the destination name
00954d7bf18589273e08e0d7fa824c69ce96e70bahl * contained no slash character).
00954d7bf18589273e08e0d7fa824c69ce96e70bahl * Trim off the final dataset component so we perform the
00954d7bf18589273e08e0d7fa824c69ce96e70bahl * recvbackup ioctl to the filesystems's parent.
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (flags->isprefix && !flags->istail && !flags->dryrun &&
00954d7bf18589273e08e0d7fa824c69ce96e70bahl create_parents(hdl, zc.zc_value, strlen(tosnap)) != 0) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens flags->dryrun ? "would receive" : "receiving",
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens return (recv_skip(hdl, infd, flags->byteswap));
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson zc.zc_nvlist_dst = (uint64_t)(uintptr_t)prop_errbuf;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson VERIFY(0 == nvlist_unpack((void *)(uintptr_t)zc.zc_nvlist_dst,
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson while ((prop_err = nvlist_next_nvpair(prop_errors,
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson prop = zfs_name_to_prop(nvpair_name(prop_err));
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald } else if (snapname == NULL || finalsnap == NULL ||
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * Skip the special case of, for example,
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * "refquota", errors on intermediate
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * snapshots leading up to a final one.
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * That's why we have all of the checks above.
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * See zfs_ioctl.c's extract_delay_props() for
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * a list of props which can fail on
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * intermediate snapshots, but shouldn't
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald * affect the overall receive.
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson "cannot receive %s property on %s"),
bb0ade0978a02d3fe0b0165cd4725fdcb593fbfbahrens if (zcmd_write_src_nvlist(hdl, &zc2, snapprops_nvlist) == 0) {
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson if (err && (ioctl_errno == ENOENT || ioctl_errno == EEXIST)) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * It may be that this snapshot already exists,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * in which case we want to consume & ignore it
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * rather than failing.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * XXX Do this faster by just iterating over snaps in
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * this fs. Also if zc_value does not exist, we will
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * get a strange "does not exist" error message.
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson if (gather_nvlist(hdl, zc.zc_value, NULL, NULL, B_FALSE,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "most recent snapshot of %s does not\n"
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "destination %s has been modified\n"
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* it's the containing fs that exists */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "destination already exists"));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens recv_ecksum_set_aux(hdl, zc.zc_value, flags->resumable);
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum "pool must be upgraded to receive this stream."));
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum (void) zfs_error(hdl, EZFS_BADVERSION, errbuf);
37f8ae65e05ea39982de563e0318b8c9e70cee4cJohn Harres "destination %s space quota exceeded"), zc.zc_name);
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor * Mount the target filesystem (if created). Also mount any
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor * children of the target filesystem if we did a replication
681d9761e8516a7dc5ab6589e2dfe717777e1123Eric Taylor * receive (indicated by stream_avl being non-NULL).
04bb726ecc2ce091dd01d32316d6730863741df0ahl if (h != NULL) {
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley * Track the first/top of hierarchy fs,
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley * for mounting and sharing later.
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson "failed to clear unreceived properties on %s"),
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson (void) fprintf(stderr, dgettext(TEXT_DOMAIN, "Warning: "
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson "failed to restore original properties on %s"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (-1);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) printf("received %sB stream in %lu seconds (%sB/sec)\n",
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliezfs_receive_impl(libzfs_handle_t *hdl, const char *tosnap,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie const char *originsnap, recvflags_t *flags, int infd, const char *sendfs,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie nvlist_t *stream_nv, avl_tree_t *stream_avl, char **top_zfs, int cleanup_fd,
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald uint64_t *action_handlep, const char *finalsnap)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "cannot receive"));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens !zfs_dataset_exists(hdl, tosnap, ZFS_TYPE_DATASET)) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified fs "
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie !zfs_dataset_exists(hdl, originsnap, ZFS_TYPE_DATASET)) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "specified origin fs "
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* read in the BEGIN record */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (0 != (err = recv_read(hdl, infd, &drr, sizeof (drr), B_FALSE,
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (drr.drr_type == DRR_END || drr.drr_type == BSWAP_32(DRR_END)) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* It's the double end record at the end of a package */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* the kernel needs the non-byteswapped begin record */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * We computed the checksum in the wrong byteorder in
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * recv_read() above; do it again correctly.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens fletcher_4_incremental_byteswap(&drr, sizeof (drr), &zcksum);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens if (drrb->drr_magic != DMU_BACKUP_MAGIC || drr.drr_type != DRR_BEGIN) {
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "stream (bad magic number)"));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt hdrtype = DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt (hdrtype != DMU_SUBSTREAM && hdrtype != DMU_COMPOUNDSTREAM)) {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt "stream has unsupported feature, feature flags = %lx"),
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens "stream (bad snapshot name)"));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) == DMU_SUBSTREAM) {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char nonpackage_sendfs[ZFS_MAX_DATASET_NAME_LEN];
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * We were not called from zfs_receive_package(). Get
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson * the fs specified by 'zfs send'.
bd6a198f3e258da0f1a540cdd696fc84e2bcc807Tom Erickson if ((cp = strchr(nonpackage_sendfs, '@')) != NULL)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (zfs_receive_one(hdl, infd, tosnap, originsnap, flags,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &drr, &drr_noswap, sendfs, stream_nv, stream_avl, top_zfs,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt assert(DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (zfs_receive_package(hdl, infd, tosnap, flags, &drr,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &zcksum, top_zfs, cleanup_fd, action_handlep));
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley * Restores a backup of tosnap from the file descriptor specified by infd.
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley * Return 0 on total success, -2 if some things couldn't be
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley * destroyed/renamed/promoted, -1 if some things couldn't be received.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * (-1 will override -2, if -1 and the resumable flag was specified the
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * transfer can be resumed if the sending side supports it).
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliezfs_receive(libzfs_handle_t *hdl, const char *tosnap, nvlist_t *props,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie recvflags_t *flags, int infd, avl_tree_t *stream_avl)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = nvlist_lookup_string(props, "origin", &originsnap);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = zfs_receive_impl(hdl, tosnap, originsnap, flags, infd, NULL, NULL,
5878fad70d76d8711f6608c1f80b0447601261c6Dan McDonald stream_avl, &top_zfs, cleanup_fd, &action_handle, NULL);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (err == 0 && !flags->nomount && top_zfs) {
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley zhp = zfs_open(hdl, top_zfs, ZFS_TYPE_FILESYSTEM);
0069fd67511146f5f43175204d5b71d2b357be71Tim Haley /* mount and share received datasets */