efb80947b01107dafbbf0157de0d7e1af6551259ahrens/*
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * CDDL HEADER START
efb80947b01107dafbbf0157de0d7e1af6551259ahrens *
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * The contents of this file are subject to the terms of the
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * Common Development and Distribution License (the "License").
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * You may not use this file except in compliance with the License.
efb80947b01107dafbbf0157de0d7e1af6551259ahrens *
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * or http://www.opensolaris.org/os/licensing.
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * See the License for the specific language governing permissions
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * and limitations under the License.
efb80947b01107dafbbf0157de0d7e1af6551259ahrens *
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * When distributing Covered Code, include this CDDL HEADER in each
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * If applicable, add the following below this CDDL HEADER, with the
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * fields enclosed by brackets "[]" replaced with your own identifying
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * information: Portions Copyright [yyyy] [name of copyright owner]
efb80947b01107dafbbf0157de0d7e1af6551259ahrens *
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * CDDL HEADER END
efb80947b01107dafbbf0157de0d7e1af6551259ahrens */
efb80947b01107dafbbf0157de0d7e1af6551259ahrens/*
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Copyright (c) 2014, Joyent, Inc. All rights reserved.
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens * Copyright 2014 HybridCluster. All rights reserved.
880094b6062aebeec8eda6a8651757611c83b13eAndrew Stormont * Copyright 2016 RackTop Systems.
c3d26abc9ee97b4f60233556aadeb57e0bd30bb9Matthew Ahrens * Copyright (c) 2014 Integros [integros.com]
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko */
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dmu.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dmu_impl.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dmu_tx.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dbuf.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dnode.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/zfs_context.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dmu_objset.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dmu_traverse.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dsl_dataset.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dsl_dir.h>
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson#include <sys/dsl_prop.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dsl_pool.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/dsl_synctask.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/zfs_ioctl.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/zap.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#include <sys/zio_checksum.h>
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum#include <sys/zfs_znode.h>
cde58dbc6a23d4d38db7c8866312be83221c765fMatthew Ahrens#include <zfs_fletcher.h>
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt#include <sys/avl.h>
8e7144747a94247ce9dbb4fb1f67bdcb666b30d8Lori Alt#include <sys/ddt.h>
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby#include <sys/zfs_onexit.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dmu_send.h>
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#include <sys/dsl_destroy.h>
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens#include <sys/blkptr.h>
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens#include <sys/dsl_bookmark.h>
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens#include <sys/zfeature.h>
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie#include <sys/bqueue.h>
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens/* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrensint zfs_send_corrupt_data = B_FALSE;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelieint zfs_send_queue_length = 16 * 1024 * 1024;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelieint zfs_recv_queue_length = 16 * 1024 * 1024;
880094b6062aebeec8eda6a8651757611c83b13eAndrew Stormont/* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
880094b6062aebeec8eda6a8651757611c83b13eAndrew Stormontint zfs_send_set_freerecords_bit = B_TRUE;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensstatic char *dmu_recv_tag = "dmu_recv_tag";
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensconst char *recv_clone_name = "%recv";
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie#define BP_SPAN(datablkszsec, indblkshift, level) \
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (level) * (indblkshift - SPA_BLKPTRSHIFT)))
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensstatic void byteswap_record(dmu_replay_record_t *drr);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestruct send_thread_arg {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_t q;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_t *ds; /* Dataset to traverse */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t fromtxg; /* Traverse from this txg */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int flags; /* flags to pass to traverse_dataset */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int error_code;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie boolean_t cancel;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zbookmark_phys_t resume;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie};
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestruct send_block_record {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie boolean_t eos_marker; /* Marks the end of the stream */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie blkptr_t bp;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zbookmark_phys_t zb;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint8_t indblkshift;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint16_t datablkszsec;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_node_t ln;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie};
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewskidump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens ssize_t resid; /* have to get resid to get detailed errno */
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal /*
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * The code does not rely on this (len being a multiple of 8). We keep
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * this assertion because of the corresponding assertion in
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * receive_read(). Keeping this assertion ensures that we do not
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * inadvertently break backwards compatibility (causing the assertion
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * in receive_read() to trigger on old software).
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal *
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * Removing the assertions could be rolled into a new feature that uses
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * data that isn't 8-byte aligned; if the assertions were removed, a
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * feature flag would have to be added.
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal */
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(len % 8);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens (caddr_t)buf, len,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski mutex_enter(&ds->ds_sendstream_lock);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski *dsp->dsa_off += len;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski mutex_exit(&ds->ds_sendstream_lock);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski return (dsp->dsa_err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens/*
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens * For all record types except BEGIN, fill in the checksum (overlaid in
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens * up to the start of the checksum itself.
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens */
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrensstatic int
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrensdump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens{
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 fletcher_4_incremental_native(dsp->dsa_drr,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens &dsp->dsa_zc);
12b90ee2d3b10689fc45f4930d2392f5fe1d9cfaMatt Krantz if (dsp->dsa_drr->drr_type == DRR_BEGIN) {
12b90ee2d3b10689fc45f4930d2392f5fe1d9cfaMatt Krantz dsp->dsa_sent_begin = B_TRUE;
12b90ee2d3b10689fc45f4930d2392f5fe1d9cfaMatt Krantz } else {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens drr_checksum.drr_checksum));
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
12b90ee2d3b10689fc45f4930d2392f5fe1d9cfaMatt Krantz if (dsp->dsa_drr->drr_type == DRR_END) {
12b90ee2d3b10689fc45f4930d2392f5fe1d9cfaMatt Krantz dsp->dsa_sent_end = B_TRUE;
12b90ee2d3b10689fc45f4930d2392f5fe1d9cfaMatt Krantz }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens fletcher_4_incremental_native(&dsp->dsa_drr->
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens drr_u.drr_checksum.drr_checksum,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens sizeof (zio_cksum_t), &dsp->dsa_zc);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (SET_ERROR(EINTR));
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (payload_len != 0) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens fletcher_4_incremental_native(payload, payload_len,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens &dsp->dsa_zc);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_bytes(dsp, payload, payload_len) != 0)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (SET_ERROR(EINTR));
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (0);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens}
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie/*
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * Fill in the drr_free struct, or perform aggregation if the previous record is
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * also a free record, and the two are adjacent.
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie *
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * Note that we send free records even for a full send, because we want to be
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * able to receive a full send as a clone, which requires a list of all the free
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * and freeobject records that were generated on the source.
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie */
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewskidump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens uint64_t length)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens /*
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * When we receive a free record, dbuf_free_range() assumes
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * that the receiving system doesn't have any dbufs in the range
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * being freed. This is always true because there is a one-record
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * constraint: we only send one WRITE record for any given
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * object,offset. We know that the one-record constraint is
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * true because we always send data in increasing order by
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * object,offset.
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens *
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * If the increasing-order constraint ever changes, we should find
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * another way to assert that the one-record constraint is still
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * satisfied.
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens */
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens ASSERT(object > dsp->dsa_last_data_object ||
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens (object == dsp->dsa_last_data_object &&
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens offset > dsp->dsa_last_data_offset));
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens
534029e5aa7701f85c9a4e201014b5a5a1aaf488Simon Klinkert if (length != -1ULL && offset + length < offset)
534029e5aa7701f85c9a4e201014b5a5a1aaf488Simon Klinkert length = -1ULL;
534029e5aa7701f85c9a4e201014b5a5a1aaf488Simon Klinkert
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /*
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * If there is a pending op, but it's not PENDING_FREE, push it out,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * since free block aggregation can only be done for blocks of the
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * same type (i.e., DRR_FREE records can only be aggregated with
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * other DRR_FREE records. DRR_FREEOBJECTS records can only be
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * aggregated with other DRR_FREEOBJECTS records.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dsp->dsa_pending_op != PENDING_NONE &&
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op != PENDING_FREE) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_NONE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dsp->dsa_pending_op == PENDING_FREE) {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /*
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * There should never be a PENDING_FREE if length is -1
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * (because dump_dnode is the only place where this
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * function is called with a -1, and only after flushing
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * any pending record).
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt ASSERT(length != -1ULL);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /*
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * Check to see whether this free block can be aggregated
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * with pending one.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (drrf->drr_object == object && drrf->drr_offset +
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrf->drr_length == offset) {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrf->drr_length += length;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (0);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt } else {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* not a continuation. Push out pending record */
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_NONE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* create a FREE record and make it pending */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_drr->drr_type = DRR_FREE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrf->drr_object = object;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrf->drr_offset = offset;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrf->drr_length = length;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski drrf->drr_toguid = dsp->dsa_toguid;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (length == -1ULL) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt } else {
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_FREE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensdump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t object, uint64_t offset, int lsize, int psize, const blkptr_t *bp,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel void *data)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t payload_size;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens /*
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * We send data in increasing object, offset order.
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * See comment in dump_free() for details.
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens */
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens ASSERT(object > dsp->dsa_last_data_object ||
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens (object == dsp->dsa_last_data_object &&
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens offset > dsp->dsa_last_data_offset));
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens dsp->dsa_last_data_object = object;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel dsp->dsa_last_data_offset = offset + lsize - 1;
8e7144747a94247ce9dbb4fb1f67bdcb666b30d8Lori Alt
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /*
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * If there is any kind of pending aggregation (currently either
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * a grouping of free objects or free blocks), push it out to
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * the stream, since aggregation can't be done across operations
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * of different types.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dsp->dsa_pending_op != PENDING_NONE) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_NONE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens /* write a WRITE record */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_drr->drr_type = DRR_WRITE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrw->drr_object = object;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrw->drr_type = type;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrw->drr_offset = offset;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski drrw->drr_toguid = dsp->dsa_toguid;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel drrw->drr_logical_size = lsize;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /* only set the compression fields if the buf is compressed */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (lsize != psize) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(!BP_IS_EMBEDDED(bp));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(!BP_SHOULD_BYTESWAP(bp));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3S(psize, >, 0);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3S(lsize, >=, psize);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel drrw->drr_compressiontype = BP_GET_COMPRESS(bp);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel drrw->drr_compressed_size = psize;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel payload_size = drrw->drr_compressed_size;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel } else {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel payload_size = drrw->drr_logical_size;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens if (bp == NULL || BP_IS_EMBEDDED(bp)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * There's no pre-computed checksum for partial-block
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * writes or embedded BP's, so (like
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * fletcher4-checkummed blocks) userland will have to
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * compute a dedup-capable checksum itself.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens } else {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
45818ee124adeaaf947698996b4f4c722afc6d1fMatthew Ahrens ZCHECKSUM_FLAG_DEDUP)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_key.ddk_cksum = bp->blk_cksum;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (dump_record(dsp, data, payload_size) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensstatic int
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensdump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens int blksz, const blkptr_t *bp)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens{
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens char buf[BPE_PAYLOAD_SIZE];
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens struct drr_write_embedded *drrw =
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens &(dsp->dsa_drr->drr_u.drr_write_embedded);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (dsp->dsa_pending_op != PENDING_NONE) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (EINTR);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens dsp->dsa_pending_op = PENDING_NONE;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens ASSERT(BP_IS_EMBEDDED(bp));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_object = object;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_offset = offset;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_length = blksz;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_toguid = dsp->dsa_toguid;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_compression = BP_GET_COMPRESS(bp);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_etype = BPE_GET_ETYPE(bp);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_lsize = BPE_GET_LSIZE(bp);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrw->drr_psize = BPE_GET_PSIZE(bp);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens decode_embedded_bp_compressed(bp, buf);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (EINTR);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (0);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens}
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaumstatic int
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewskidump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum{
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dsp->dsa_pending_op != PENDING_NONE) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_NONE;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum }
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum /* write a SPILL record */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_drr->drr_type = DRR_SPILL;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum drrs->drr_object = object;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum drrs->drr_length = blksz;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski drrs->drr_toguid = dsp->dsa_toguid;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, data, blksz) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum return (0);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum}
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewskidump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /*
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * If there is a pending op, but it's not PENDING_FREEOBJECTS,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * push it out, since free block aggregation can only be done for
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * blocks of the same type (i.e., DRR_FREE records can only be
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * can only be aggregated with other DRR_FREEOBJECTS records.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dsp->dsa_pending_op != PENDING_NONE &&
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_NONE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /*
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * See whether this free object array can be aggregated
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * with pending one
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrfo->drr_numobjs += numobjs;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (0);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt } else {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* can't be aggregated. Push out pending record */
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_NONE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
efb80947b01107dafbbf0157de0d7e1af6551259ahrens /* write a FREEOBJECTS record */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrfo->drr_firstobj = firstobj;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrfo->drr_numobjs = numobjs;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski drrfo->drr_toguid = dsp->dsa_toguid;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_FREEOBJECTS;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewskidump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (object < dsp->dsa_resume_object) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Note: when resuming, we will visit all the dnodes in
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * the block of dnodes that we are resuming from. In
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * this case it's unnecessary to send the dnodes prior to
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * the one we are resuming from. We should be at most one
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * block's worth of dnodes behind the resume point.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT3U(dsp->dsa_resume_object - object, <,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski return (dump_freeobjects(dsp, object, 1));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dsp->dsa_pending_op != PENDING_NONE) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_NONE;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
efb80947b01107dafbbf0157de0d7e1af6551259ahrens /* write an OBJECT record */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_drr->drr_type = DRR_OBJECT;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drro->drr_object = object;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drro->drr_type = dnp->dn_type;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drro->drr_bonustype = dnp->dn_bonustype;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drro->drr_bonuslen = dnp->dn_bonuslen;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drro->drr_checksumtype = dnp->dn_checksum;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drro->drr_compress = dnp->dn_compress;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski drro->drr_toguid = dsp->dsa_toguid;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, DN_BONUS(dnp),
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens /* Free anything past the end of the file. */
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (dsp->dsa_err != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINTR));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensstatic boolean_t
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensbackup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens{
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (!BP_IS_EMBEDDED(bp))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (B_FALSE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Compression function must be legacy, or explicitly enabled.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LZ4)))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (B_FALSE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * Embed type must be explicitly enabled.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens switch (BPE_GET_ETYPE(bp)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens case BP_EMBEDDED_TYPE_DATA:
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (B_TRUE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens break;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens default:
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (B_FALSE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (B_FALSE);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens}
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * This is the callback function to traverse_dataset that acts as the worker
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * thread for dmu_send_impl.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*ARGSUSED*/
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliesend_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie{
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct send_thread_arg *sta = arg;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct send_block_record *record;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t record_size;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int err = 0;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zb->zb_object >= sta->resume.zb_object);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (sta->cancel)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (SET_ERROR(EINTR));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (bp == NULL) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (0);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie } else if (zb->zb_level < 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (0);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie record->eos_marker = B_FALSE;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie record->bp = *bp;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie record->zb = *zb;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie record->indblkshift = dnp->dn_indblkshift;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie record->datablkszsec = dnp->dn_datablkszsec;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_enqueue(&sta->q, record, record_size);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie}
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * This function kicks off the traverse_dataset. It also handles setting the
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * error code of the thread in case something goes wrong, and pushes the End of
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Stream record when the traverse_dataset call has finished. If there is no
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * dataset to traverse, the thread immediately pushes End of Stream marker.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestatic void
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliesend_traverse_thread(void *arg)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie{
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct send_thread_arg *st_arg = arg;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int err;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct send_block_record *data;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (st_arg->ds != NULL) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = traverse_dataset_resume(st_arg->ds,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens st_arg->fromtxg, &st_arg->resume,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens st_arg->flags, send_cb, st_arg);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err != EINTR)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie st_arg->error_code = err;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie data = kmem_zalloc(sizeof (*data), KM_SLEEP);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie data->eos_marker = B_TRUE;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_enqueue(&st_arg->q, data, 1);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie}
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * This function actually handles figuring out what kind of record needs to be
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * dumped, reading the data (which has hopefully been prefetched), and calling
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * the appropriate helper function.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliedo_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie const blkptr_t *bp = &data->bp;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie const zbookmark_phys_t *zb = &data->zb;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint8_t indblkshift = data->indblkshift;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint16_t dblkszsec = data->datablkszsec;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens int err = 0;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ASSERT3U(zb->zb_level, >=, 0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zb->zb_object >= dsa->dsa_resume_object);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick if (zb->zb_object != DMU_META_DNODE_OBJECT &&
b24ab6762772a3f6a89393947930c7fa61306783Jeff Bonwick DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
148434217c040ea38dc844384f6ba68d9b325906Matthew Ahrens return (0);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman } else if (BP_IS_HOLE(bp) &&
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman zb->zb_object == DMU_META_DNODE_OBJECT) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
43466aae47bfcd2ad9bf501faec8e75c08095e4fMax Grossman } else if (BP_IS_HOLE(bp)) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t offset = zb->zb_blkid * span;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dump_free(dsa, zb->zb_object, offset, span);
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens return (0);
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens } else if (type == DMU_OT_DNODE) {
efb80947b01107dafbbf0157de0d7e1af6551259ahrens int blksz = BP_GET_LSIZE(bp);
7adb730b589e553bf3b1ccfd9bae2df91c5c1061George Wilson arc_flags_t aflags = ARC_FLAG_WAIT;
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens arc_buf_t *abuf;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ASSERT0(zb->zb_level);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilson if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilson ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilson &aflags, zb) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EIO));
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dnode_phys_t *blk = abuf->b_data;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dump_dnode(dsa, dnobj + i, blk + i);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens break;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilson arc_buf_destroy(abuf, &abuf);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum } else if (type == DMU_OT_SA) {
7adb730b589e553bf3b1ccfd9bae2df91c5c1061George Wilson arc_flags_t aflags = ARC_FLAG_WAIT;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum arc_buf_t *abuf;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum int blksz = BP_GET_LSIZE(bp);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilson if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilson ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilson &aflags, zb) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EIO));
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilson arc_buf_destroy(abuf, &abuf);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie } else if (backup_do_embed(dsa, bp)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /* it's an embedded level-0 block of a regular object */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ASSERT0(zb->zb_level);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dump_write_embedded(dsa, zb->zb_object,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens zb->zb_blkid * blksz, blksz, bp);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie } else {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /* it's a level-0 block of a regular object */
7adb730b589e553bf3b1ccfd9bae2df91c5c1061George Wilson arc_flags_t aflags = ARC_FLAG_WAIT;
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens arc_buf_t *abuf;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens uint64_t offset;
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /*
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * If we have large blocks stored on disk but the send flags
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * don't allow us to send large blocks, we split the data from
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * the arc buf into chunks.
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t split_large_blocks = blksz > SPA_OLD_MAXBLOCKSIZE &&
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel !(dsa->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /*
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * We should only request compressed data from the ARC if all
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * the following are true:
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * - stream compression was requested
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * - we aren't splitting large blocks into smaller chunks
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * - the data won't need to be byteswapped before sending
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * - this isn't an embedded block
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * - this isn't metadata (if receiving on a different endian
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * system it can be byteswapped more easily)
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t request_compressed =
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED) &&
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) &&
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT0(zb->zb_level);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(zb->zb_object > dsa->dsa_resume_object ||
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel (zb->zb_object == dsa->dsa_resume_object &&
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens ASSERT0(zb->zb_level);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(zb->zb_object > dsa->dsa_resume_object ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (zb->zb_object == dsa->dsa_resume_object &&
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(blksz, ==, BP_GET_LSIZE(bp));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (request_compressed)
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zioflags |= ZIO_FLAG_RAW;
1b912ec7100c10e7243bf0879af0fe580e08c73dGeorge Wilson if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (zfs_send_corrupt_data) {
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /* Send a block filled with 0x"zfs badd bloc" */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel abuf = arc_alloc_buf(spa, &abuf, ARC_BUFC_DATA,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel blksz);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens uint64_t *ptr;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens for (ptr = abuf->b_data;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens (char *)ptr < (char *)abuf->b_data + blksz;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens ptr++)
8c76e0763bcf0029556e106377da859f6492a7eeBrian Behlendorf *ptr = 0x2f5baddb10cULL;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens } else {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EIO));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
88b7b0f29b20b808b9e06071885b1d6a3ddb6328Matthew Ahrens
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens offset = zb->zb_blkid * blksz;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (split_large_blocks) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(arc_get_compression(abuf), ==,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ZIO_COMPRESS_OFF);
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens char *buf = abuf->b_data;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens while (blksz > 0 && err == 0) {
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dump_write(dsa, type, zb->zb_object,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel offset, n, n, NULL, buf);
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens offset += n;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens buf += n;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens blksz -= n;
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens }
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens } else {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel err = dump_write(dsa, type, zb->zb_object, offset,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel blksz, arc_buf_size(abuf), bp, abuf->b_data);
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens }
dcbf3bd6a1f1360fc1afcee9e22c6dcff7844bf2George Wilson arc_buf_destroy(abuf, &abuf);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens ASSERT(err == 0 || err == EINTR);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Pop the new data off the queue, and free the old data.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestatic struct send_block_record *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelieget_next_record(bqueue_t *bq, struct send_block_record *data)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie{
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct send_block_record *tmp = bqueue_dequeue(bq);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(data, sizeof (*data));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (tmp);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie}
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Actually do the bulk of the work in a zfs send.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Note: Releases dp using the specified tag.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliedmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel zfs_bookmark_phys_t *ancestor_zb, boolean_t is_clone,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel int outfd, uint64_t resumeobj, uint64_t resumeoff,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens vnode_t *vp, offset_t *off)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens objset_t *os;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_replay_record_t *drr;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dmu_sendarg_t *dsp;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens int err;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens uint64_t fromtxg = 0;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens uint64_t featureflags = 0;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens struct send_thread_arg to_arg = { 0 };
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dmu_objset_from_ds(to_ds, &os);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drr->drr_type = DRR_BEGIN;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DMU_SUBSTREAM);
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum#ifdef _KERNEL
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (dmu_objset_type(os) == DMU_OST_ZFS) {
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum uint64_t version;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski kmem_free(drr, sizeof (dmu_replay_record_t));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, tag);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (version >= ZPL_VERSION_SA) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum }
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum }
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum#endif
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum
ca0cc3918a1789fa839194af2a9245f801a06b1aMatthew Ahrens if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (embedok &&
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel featureflags |= DMU_BACKUP_FEATURE_LZ4;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (compressok) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel featureflags |= DMU_BACKUP_FEATURE_COMPRESSED;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if ((featureflags &
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED)) !=
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel 0 && spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel featureflags |= DMU_BACKUP_FEATURE_LZ4;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (resumeobj != 0 || resumeoff != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens featureflags |= DMU_BACKUP_FEATURE_RESUMING;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens featureflags);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drr->drr_u.drr_begin.drr_creation_time =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_phys(to_ds)->ds_creation_time;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (is_clone)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
880094b6062aebeec8eda6a8651757611c83b13eAndrew Stormont if (zfs_send_set_freerecords_bit)
880094b6062aebeec8eda6a8651757611c83b13eAndrew Stormont drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (ancestor_zb != NULL) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie drr->drr_u.drr_begin.drr_fromguid =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ancestor_zb->zbm_guid;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie fromtxg = ancestor_zb->zbm_creation_txg;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (!to_ds->ds_is_snapshot) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens sizeof (drr->drr_u.drr_begin.drr_toname));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_drr = drr;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_vp = vp;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_outfd = outfd;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_proc = curproc;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsp->dsa_os = os;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_off = off;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski dsp->dsa_pending_op = PENDING_NONE;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens dsp->dsa_featureflags = featureflags;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsp->dsa_resume_object = resumeobj;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsp->dsa_resume_offset = resumeoff;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_enter(&to_ds->ds_sendstream_lock);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie list_insert_head(&to_ds->ds_sendstreams, dsp);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_exit(&to_ds->ds_sendstream_lock);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_long_hold(to_ds, FTAG);
de8d9cff565e928d0ace86f3ea0e2b15094d61dfMatthew Ahrens dsl_pool_rele(dp, tag);
de8d9cff565e928d0ace86f3ea0e2b15094d61dfMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens void *payload = NULL;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens size_t payload_len = 0;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (resumeobj != 0 || resumeoff != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_object_info_t to_doi;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = dmu_object_info(os, resumeobj, &to_doi);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (err != 0)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens goto out;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens resumeoff / to_doi.doi_data_block_size);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_t *nvl = fnvlist_alloc();
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(nvl, "resume_object", resumeobj);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens payload = fnvlist_pack(nvl, &payload_len);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drr->drr_payloadlen = payload_len;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_free(nvl);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = dump_record(dsp, payload, payload_len);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fnvlist_pack_free(payload, payload_len);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (err != 0) {
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski err = dsp->dsa_err;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski goto out;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = bqueue_init(&to_arg.q, zfs_send_queue_length,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie offsetof(struct send_block_record, ln));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_arg.error_code = 0;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_arg.cancel = B_FALSE;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_arg.ds = to_ds;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_arg.fromtxg = fromtxg;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie TS_RUN, minclsyspri);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct send_block_record *to_data;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_data = bqueue_dequeue(&to_arg.q);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie while (!to_data->eos_marker && err == 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = do_dump(dsp, to_data);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_data = get_next_record(&to_arg.q, to_data);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (issig(JUSTLOOKING) && issig(FORREAL))
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = EINTR;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err != 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_arg.cancel = B_TRUE;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie while (!to_data->eos_marker) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie to_data = get_next_record(&to_arg.q, to_data);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(to_data, sizeof (*to_data));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_destroy(&to_arg.q);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err == 0 && to_arg.error_code != 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = to_arg.error_code;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err != 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie goto out;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski if (dsp->dsa_pending_op != PENDING_NONE)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (dump_record(dsp, NULL, 0) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens err = SET_ERROR(EINTR);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err == EINTR && dsp->dsa_err != 0)
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski err = dsp->dsa_err;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski goto out;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens bzero(drr, sizeof (dmu_replay_record_t));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drr->drr_type = DRR_END;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (dump_record(dsp, NULL, 0) != 0)
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski err = dsp->dsa_err;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewskiout:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_enter(&to_ds->ds_sendstream_lock);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie list_remove(&to_ds->ds_sendstreams, dsp);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_exit(&to_ds->ds_sendstream_lock);
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski
12b90ee2d3b10689fc45f4930d2392f5fe1d9cfaMatt Krantz VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end));
12b90ee2d3b10689fc45f4930d2392f5fe1d9cfaMatt Krantz
efb80947b01107dafbbf0157de0d7e1af6551259ahrens kmem_free(drr, sizeof (dmu_replay_record_t));
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski kmem_free(dsp, sizeof (dmu_sendarg_t));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dsl_dataset_long_rele(to_ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
4e3c9f4489a18514e5e8caeb91d4e6db07c98415Bill Pijewski return (err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrensint
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens int outfd, vnode_t *vp, offset_t *off)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *fromds = NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int err;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_pool_hold(pool, FTAG, &dp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (fromsnap != 0) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens zfs_bookmark_phys_t zb;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens boolean_t is_clone;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (!dsl_dataset_is_before(ds, fromds, 0))
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens err = SET_ERROR(EXDEV);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zb.zbm_creation_time =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(fromds)->ds_creation_time;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens is_clone = (fromds->ds_dir != ds->ds_dir);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens dsl_dataset_rele(fromds, FTAG);
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel embedok, large_block_ok, compressok, outfd, 0, 0, vp, off);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens } else {
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel embedok, large_block_ok, compressok, outfd, 0, 0, vp, off);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens dsl_dataset_rele(ds, FTAG);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensdmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t large_block_ok, boolean_t compressok, int outfd,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t resumeobj, uint64_t resumeoff,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens vnode_t *vp, offset_t *off)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int err;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens boolean_t owned = B_FALSE;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_pool_hold(tosnap, FTAG, &dp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens /*
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * We are sending a filesystem or volume. Ensure
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * that it doesn't change by owning the dataset.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens owned = B_TRUE;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens } else {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (fromsnap != NULL) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens zfs_bookmark_phys_t zb;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens boolean_t is_clone = B_FALSE;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens int fsnamelen = strchr(tosnap, '@') - tosnap;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens /*
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * If the fromsnap is in a different filesystem, then
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens * mark the send stream as a clone.
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens (fromsnap[fsnamelen] != '@' &&
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens fromsnap[fsnamelen] != '#')) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens is_clone = B_TRUE;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (strchr(fromsnap, '@')) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens dsl_dataset_t *fromds;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (err == 0) {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (!dsl_dataset_is_before(ds, fromds, 0))
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens err = SET_ERROR(EXDEV);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens zb.zbm_creation_time =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(fromds)->ds_creation_time;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens zb.zbm_creation_txg =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(fromds)->ds_creation_txg;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens is_clone = (ds->ds_dir != fromds->ds_dir);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens dsl_dataset_rele(fromds, FTAG);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens } else {
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel embedok, large_block_ok, compressok,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens outfd, resumeobj, resumeoff, vp, off);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens } else {
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel embedok, large_block_ok, compressok,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens outfd, resumeobj, resumeoff, vp, off);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (owned)
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens dsl_dataset_disown(ds, FTAG);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens else
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens dsl_dataset_rele(ds, FTAG);
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens return (err);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
643da460c8ca583e39ce053081754e24087f84c8Max Grossmanstatic int
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmeldmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep)
643da460c8ca583e39ce053081754e24087f84c8Max Grossman{
643da460c8ca583e39ce053081754e24087f84c8Max Grossman int err;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t size;
643da460c8ca583e39ce053081754e24087f84c8Max Grossman /*
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * Assume that space (both on-disk and in-stream) is dominated by
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * data. We will adjust for indirect blocks and the copies property,
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
643da460c8ca583e39ce053081754e24087f84c8Max Grossman */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t recordsize;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t record_count;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /* Assume all (uncompressed) blocks are recordsize. */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel err = dsl_prop_get_int_ds(ds, zfs_prop_to_name(ZFS_PROP_RECORDSIZE),
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel &recordsize);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (err != 0)
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel return (err);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel record_count = uncompressed / recordsize;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /*
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * If we're estimating a send size for a compressed stream, use the
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * compressed data size to estimate the stream size. Otherwise, use the
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * uncompressed data size.
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel size = stream_compressed ? compressed : uncompressed;
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
643da460c8ca583e39ce053081754e24087f84c8Max Grossman /*
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * Subtract out approximate space used by indirect blocks.
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * Assume most space is used by data blocks (non-indirect, non-dnode).
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * Assume no ditto blocks or internal fragmentation.
643da460c8ca583e39ce053081754e24087f84c8Max Grossman *
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * block.
643da460c8ca583e39ce053081754e24087f84c8Max Grossman */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel size -= record_count * sizeof (blkptr_t);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
643da460c8ca583e39ce053081754e24087f84c8Max Grossman /* Add in the space for the record associated with each block. */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel size += record_count * sizeof (dmu_replay_record_t);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
643da460c8ca583e39ce053081754e24087f84c8Max Grossman *sizep = size;
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
643da460c8ca583e39ce053081754e24087f84c8Max Grossman return (0);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman}
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensint
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmeldmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t stream_compressed, uint64_t *sizep)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens{
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens int err;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t uncomp, comp;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT(dsl_pool_config_held(dp));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /* tosnap must be a snapshot */
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (!ds->ds_is_snapshot)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
24218bebb460e4015fac2c9f2cec1902eddbcd7bAndriy Gapon /* fromsnap, if provided, must be a snapshot */
24218bebb460e4015fac2c9f2cec1902eddbcd7bAndriy Gapon if (fromds != NULL && !fromds->ds_is_snapshot)
24218bebb460e4015fac2c9f2cec1902eddbcd7bAndriy Gapon return (SET_ERROR(EINVAL));
24218bebb460e4015fac2c9f2cec1902eddbcd7bAndriy Gapon
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens /*
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * fromsnap must be an earlier snapshot from the same fs as tosnap,
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens * or the origin's fs.
4445fffbbb1ea25fd0e9ea68b9380dd7a6709025Matthew Ahrens */
78f171005391b928aaf1642b3206c534ed644332Matthew Ahrens if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EXDEV));
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /* Get compressed and uncompressed size estimates of changed data. */
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens if (fromds == NULL) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel comp = dsl_dataset_phys(ds)->ds_compressed_bytes;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens } else {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t used;
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens err = dsl_dataset_space_written(fromds, ds,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel &used, &comp, &uncomp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens return (err);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel stream_compressed, sizep);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman return (err);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman}
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmelstruct calculate_send_arg {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t uncompressed;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel uint64_t compressed;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel};
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel
643da460c8ca583e39ce053081754e24087f84c8Max Grossman/*
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * Simple callback used to traverse the blocks of a snapshot and sum their
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * uncompressed and compressed sizes.
643da460c8ca583e39ce053081754e24087f84c8Max Grossman */
643da460c8ca583e39ce053081754e24087f84c8Max Grossman/* ARGSUSED */
643da460c8ca583e39ce053081754e24087f84c8Max Grossmanstatic int
643da460c8ca583e39ce053081754e24087f84c8Max Grossmandmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
643da460c8ca583e39ce053081754e24087f84c8Max Grossman const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
643da460c8ca583e39ce053081754e24087f84c8Max Grossman{
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel struct calculate_send_arg *space = arg;
643da460c8ca583e39ce053081754e24087f84c8Max Grossman if (bp != NULL && !BP_IS_HOLE(bp)) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel space->uncompressed += BP_GET_UCSIZE(bp);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel space->compressed += BP_GET_PSIZE(bp);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman }
643da460c8ca583e39ce053081754e24087f84c8Max Grossman return (0);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman}
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
643da460c8ca583e39ce053081754e24087f84c8Max Grossman/*
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * Given a desination snapshot and a TXG, calculate the approximate size of a
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * send stream sent from that TXG. from_txg may be zero, indicating that the
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * whole snapshot will be sent.
643da460c8ca583e39ce053081754e24087f84c8Max Grossman */
643da460c8ca583e39ce053081754e24087f84c8Max Grossmanint
643da460c8ca583e39ce053081754e24087f84c8Max Grossmandmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t stream_compressed, uint64_t *sizep)
643da460c8ca583e39ce053081754e24087f84c8Max Grossman{
643da460c8ca583e39ce053081754e24087f84c8Max Grossman dsl_pool_t *dp = ds->ds_dir->dd_pool;
643da460c8ca583e39ce053081754e24087f84c8Max Grossman int err;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel struct calculate_send_arg size = { 0 };
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
643da460c8ca583e39ce053081754e24087f84c8Max Grossman ASSERT(dsl_pool_config_held(dp));
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
643da460c8ca583e39ce053081754e24087f84c8Max Grossman /* tosnap must be a snapshot */
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (!ds->ds_is_snapshot)
643da460c8ca583e39ce053081754e24087f84c8Max Grossman return (SET_ERROR(EINVAL));
643da460c8ca583e39ce053081754e24087f84c8Max Grossman
643da460c8ca583e39ce053081754e24087f84c8Max Grossman /* verify that from_txg is before the provided snapshot was taken */
643da460c8ca583e39ce053081754e24087f84c8Max Grossman if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
643da460c8ca583e39ce053081754e24087f84c8Max Grossman return (SET_ERROR(EXDEV));
643da460c8ca583e39ce053081754e24087f84c8Max Grossman }
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens /*
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * traverse the blocks of the snapshot with birth times after
643da460c8ca583e39ce053081754e24087f84c8Max Grossman * from_txg, summing their uncompressed size
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens */
643da460c8ca583e39ce053081754e24087f84c8Max Grossman err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
643da460c8ca583e39ce053081754e24087f84c8Max Grossman dmu_calculate_send_traversal, &size);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman if (err)
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens return (err);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel err = dmu_adjust_send_estimate_for_indirects(ds, size.uncompressed,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel size.compressed, stream_compressed, sizep);
643da460c8ca583e39ce053081754e24087f84c8Max Grossman return (err);
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens}
19b94df933188a15d4f0d6c568f0bab3f127892eMatthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrenstypedef struct dmu_recv_begin_arg {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *drba_origin;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_cookie_t *drba_cookie;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens cred_t *drba_cred;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens uint64_t drba_snapobj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens} dmu_recv_begin_arg_t;
f18faf3f3e5def85fdfff681617d227703ace2adek
f18faf3f3e5def85fdfff681617d227703ace2adekstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensrecv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t fromguid)
f18faf3f3e5def85fdfff681617d227703ace2adek{
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens uint64_t val;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
f18faf3f3e5def85fdfff681617d227703ace2adek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* temporary clone name must not exist */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = zap_lookup(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens 8, 1, &val);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != ENOENT)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error == 0 ? EBUSY : error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee /* new snapshot name must not exist */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = zap_lookup(dp->dp_meta_objset,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_snapnames_zapobj,
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs drba->drba_cookie->drc_tosnap, 8, 1, &val);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != ENOENT)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error == 0 ? EEXIST : error);
feaa74e41c407fe56e66a47e097c2842d4f65b9fMark Maybee
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /*
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Check snapshot limit before receiving. We'll recheck again at the
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * end, but might as well abort before receiving if we're already over
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * the limit.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek *
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Note that we do not check the file system limit with
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * dsl_dir_fscount_check because the temporary %clones don't count
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * against that limit.
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek NULL, drba->drba_cred);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (error != 0)
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek return (error);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (fromguid != 0) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_t *snap;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens /* Find snapshot in this dir that matches fromguid. */
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens while (obj != 0) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG,
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens &snap);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (error != 0)
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens return (SET_ERROR(ENODEV));
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (snap->ds_dir != ds->ds_dir) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_rele(snap, FTAG);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens return (SET_ERROR(ENODEV));
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens }
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs if (dsl_dataset_phys(snap)->ds_guid == fromguid)
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens break;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_rele(snap, FTAG);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens }
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (obj == 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENODEV));
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (drba->drba_cookie->drc_force) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens drba->drba_snapobj = obj;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens } else {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens /*
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens * If we are not forcing, there must be no
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens * changes since fromsnap.
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens */
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (dsl_dataset_modified_since_snap(ds, snap)) {
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson dsl_dataset_rele(snap, FTAG);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens return (SET_ERROR(ETXTBSY));
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson }
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens drba->drba_snapobj = ds->ds_prev->ds_object;
92241e0b80813d0b83c08e730a29b9d1831794fcTom Erickson }
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_rele(snap, FTAG);
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens } else {
5bae108fe2364722de3aa86e04966a0d33fd027aAndriy Gapon /* if full, then must be forced */
5bae108fe2364722de3aa86e04966a0d33fd027aAndriy Gapon if (!drba->drba_cookie->drc_force)
5bae108fe2364722de3aa86e04966a0d33fd027aAndriy Gapon return (SET_ERROR(EEXIST));
5bae108fe2364722de3aa86e04966a0d33fd027aAndriy Gapon /* start from $ORIGIN@$ORIGIN, if supported */
5bae108fe2364722de3aa86e04966a0d33fd027aAndriy Gapon drba->drba_snapobj = dp->dp_origin_snap != NULL ?
5bae108fe2364722de3aa86e04966a0d33fd027aAndriy Gapon dp->dp_origin_snap->ds_object : 0;
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens }
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens return (0);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdmu_recv_begin_check(void *arg, dmu_tx_t *tx)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_begin_arg_t *drba = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens uint64_t fromguid = drrb->drr_fromguid;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int flags = drrb->drr_flags;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *tofs = drba->drba_cookie->drc_tofs;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* already checked */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens DMU_COMPOUNDSTREAM ||
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drrb->drr_type >= DMU_OST_NUMTYPES ||
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Verify pool version supports SA if SA_SPILL feature set */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens spa_version(dp->dp_spa) < SPA_VERSION_SA)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (SET_ERROR(ENOTSUP));
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drba->drba_cookie->drc_resumable &&
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(ENOTSUP));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens /*
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * The receiving code doesn't know how to translate a WRITE_EMBEDDED
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel * record to a plain WRITE record, so the pool must have the
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens */
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (SET_ERROR(ENOTSUP));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOTSUP));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens /*
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * The receiving code doesn't know how to translate large blocks
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * to smaller ones, so the pool must have the LARGE_BLOCKS
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens * feature enabled if the stream has LARGE_BLOCKS.
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens */
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens return (SET_ERROR(ENOTSUP));
b515258426fed6c7311fd3f1dea697cfbd4085c6Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* target fs already exists; recv into temp clone */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Can't recv a clone into an existing fs */
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = recv_begin_check_existing_impl(drba, ds, fromguid);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else if (error == ENOENT) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* target fs does not exist; must be a full backup or clone */
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char buf[ZFS_MAX_DATASET_NAME_LEN];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * If it's a non-clone incremental, we are missing the
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * target fs, so fail the recv.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie drba->drba_origin))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENOENT));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie /*
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * If we're receiving a full send as a clone, and it doesn't
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * contain all the necessary free records and freeobject
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * records, reject it.
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie */
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie if (fromguid == 0 && drba->drba_origin &&
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie !(flags & DRR_FLAG_FREERECORDS))
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie return (SET_ERROR(EINVAL));
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Open the parent of tofs */
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens ASSERT3U(strlen(tofs), <, sizeof (buf));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, buf, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek /*
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * Check filesystem and snapshot limits before receiving. We'll
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * recheck snapshot limits again at the end (we create the
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek * filesystems and increment those counts during begin_sync).
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek */
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (error != 0) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_dataset_rele(ds, FTAG);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek return (error);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek if (error != 0) {
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek dsl_dataset_rele(ds, FTAG);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek return (error);
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek }
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (drba->drba_origin != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, drba->drba_origin,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens FTAG, &origin);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
bc9014e6a81272073b9854d9f65dd59e18d18c35Justin Gibbs if (!origin->ds_is_snapshot) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(origin, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie fromguid != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(origin, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(ENODEV));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(origin, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
f18faf3f3e5def85fdfff681617d227703ace2adek}
f18faf3f3e5def85fdfff681617d227703ace2adek
f18faf3f3e5def85fdfff681617d227703ace2adekstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
f18faf3f3e5def85fdfff681617d227703ace2adek{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_begin_arg_t *drba = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens objset_t *mos = dp->dp_meta_objset;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *tofs = drba->drba_cookie->drc_tofs;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds, *newds;
f18faf3f3e5def85fdfff681617d227703ace2adek uint64_t dsobj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t crflags = 0;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drrb->drr_flags & DRR_FLAG_CI_DATA)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens crflags |= DS_FLAG_CI_DATASET;
f18faf3f3e5def85fdfff681617d227703ace2adek
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error == 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* create temporary clone */
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_t *snap = NULL;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (drba->drba_snapobj != 0) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp,
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens drba->drba_snapobj, FTAG, &snap));
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens snap, crflags, drba->drba_cred, tx);
f40b29ce2a815bcc0787acf6f520a2b74258b785Paul Dagnelie if (drba->drba_snapobj != 0)
f40b29ce2a815bcc0787acf6f520a2b74258b785Paul Dagnelie dsl_dataset_rele(snap, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(ds, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_t *dd;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens const char *tail;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin = NULL;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (drba->drba_origin != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens FTAG, &origin));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* Create new dataset. */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsobj = dsl_dataset_create_sync(dd,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens strrchr(tofs, '/') + 1,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin, crflags, drba->drba_cred, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (origin != NULL)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(origin, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dir_rele(dd, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drba->drba_cookie->drc_newfs = B_TRUE;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drba->drba_cookie->drc_resumable) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_zapify(newds, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drrb->drr_fromguid != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens 8, 1, &drrb->drr_fromguid, tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens 8, 1, &drrb->drr_toguid, tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t one = 1;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t zero = 0;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens 8, 1, &one, tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens 8, 1, &zero, tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens 8, 1, &zero, tx));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel 8, 1, &one, tx));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DMU_BACKUP_FEATURE_EMBED_DATA) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens 8, 1, &one, tx));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel DMU_BACKUP_FEATURE_COMPRESSED) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel 8, 1, &one, tx));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(newds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens /*
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens * If we actually created a non-clone, we need to create the
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens * objset in our new dataset.
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens */
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens (void) dmu_objset_create_impl(dp->dp_spa,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens }
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&newds->ds_bp_rwlock, FTAG);
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drba->drba_cookie->drc_ds = newds;
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(newds, "receive", tx, "");
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum}
dc7cd546d81fce970935f08099931c2ad2d57731Mark Shellenbaum
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensstatic int
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensdmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_recv_begin_arg_t *drba = arg;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens int error;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_t *ds;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens const char *tofs = drba->drba_cookie->drc_tofs;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* already checked */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DMU_COMPOUNDSTREAM ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drrb->drr_type >= DMU_OST_NUMTYPES)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* Verify pool version supports SA if SA_SPILL feature set */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens spa_version(dp->dp_spa) < SPA_VERSION_SA)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(ENOTSUP));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * The receiving code doesn't know how to translate a WRITE_EMBEDDED
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * record to a plain WRITE record, so the pool must have the
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(ENOTSUP));
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(ENOTSUP));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens /* 6 extra bytes for /%recv */
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) snprintf(recvname, sizeof (recvname), "%s/%s",
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens tofs, recv_clone_name);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* %recv does not exist; continue in tofs */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (error != 0)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (error);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* check that ds is marked inconsistent */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (!DS_IS_INCONSISTENT(ds)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* check that there is resuming data, and that the toguid matches */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (!dsl_dataset_is_zapified(ds)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t val;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (error != 0 || drrb->drr_toguid != val) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Check if the receive is still running. If so, it will be owned.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Note that nothing else can own the dataset (e.g. after the receive
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * fails) because it will be marked inconsistent.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (dsl_dataset_has_owner(ds)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EBUSY));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* There should not be any snapshots of this fs yet. */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Note: resume point will be checked when we process the first WRITE
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * record.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* check that the origin matches */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens val = 0;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drrb->drr_fromguid != val) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensstatic void
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensdmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_recv_begin_arg_t *drba = arg;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens const char *tofs = drba->drba_cookie->drc_tofs;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_t *ds;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t dsobj;
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens /* 6 extra bytes for /%recv */
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) snprintf(recvname, sizeof (recvname), "%s/%s",
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens tofs, recv_clone_name);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* %recv does not exist; continue in tofs */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drba->drba_cookie->drc_newfs = B_TRUE;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* clear the inconsistent flag so that we can own it */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(DS_IS_INCONSISTENT(ds));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsobj = ds->ds_object;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_rele(ds, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
c166b69d29138aed7a415fe7cef698e54c6ae945Paul Dagnelie rrw_exit(&ds->ds_bp_rwlock, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drba->drba_cookie->drc_ds = ds;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens spa_history_log_internal_ds(ds, "resume receive", tx, "");
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens/*
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * succeeds; otherwise we will leak the holds on the datasets.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens */
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensint
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensdmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_begin_arg_t drba = { 0 };
ab04eb8ef60d9dc9614d6cccffc474f24ca1d162timh
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens bzero(drc, sizeof (dmu_recv_cookie_t));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drc->drc_drr_begin = drr_begin;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drc->drc_drrb = &drr_begin->drr_u.drr_begin;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens drc->drc_tosnap = tosnap;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_tofs = tofs;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens drc->drc_force = force;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drc->drc_resumable = resumable;
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek drc->drc_cred = CRED();
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_byteswap = B_TRUE;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fletcher_4_incremental_byteswap(drr_begin,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens sizeof (dmu_replay_record_t), &drc->drc_cksum);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens byteswap_record(drr_begin);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens fletcher_4_incremental_native(drr_begin,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens sizeof (dmu_replay_record_t), &drc->drc_cksum);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens } else {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens }
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drba.drba_origin = origin;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drba.drba_cookie = drc;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drba.drba_cred = CRED();
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DMU_BACKUP_FEATURE_RESUMING) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (dsl_sync_task(tofs,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens &drba, 5, ZFS_SPACE_CHECK_NORMAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens } else {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (dsl_sync_task(tofs,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_recv_begin_check, dmu_recv_begin_sync,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens &drba, 5, ZFS_SPACE_CHECK_NORMAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestruct receive_record_arg {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_replay_record_t header;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie void *payload; /* Pointer to a buffer containing the payload */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * If the record is a write, pointer to the arc_buf_t containing the
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * payload.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie arc_buf_t *write_buf;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int payload_size;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t bytes_read; /* bytes read from stream when record created */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie boolean_t eos_marker; /* Marks the end of the stream */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_node_t node;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie};
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestruct receive_writer_arg {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens objset_t *os;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens boolean_t byteswap;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_t q;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * These three args are used to signal to the main thread that we're
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * done.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmutex_t mutex;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kcondvar_t cv;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie boolean_t done;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int err;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /* A map from guid to dataset to help handle dedup'd streams. */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie avl_tree_t *guid_to_ds_map;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens boolean_t resumable;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t last_object, last_offset;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t bytes_read; /* bytes read when current record created */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie};
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagneliestruct objlist {
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie list_t list; /* List of struct receive_objnode. */
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie /*
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * Last object looked up. Used to assert that objects are being looked
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * up in ascending order.
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie */
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie uint64_t last_lookup;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie};
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagneliestruct receive_objnode {
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie list_node_t node;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie uint64_t object;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie};
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmelstruct receive_arg {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie objset_t *os;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie vnode_t *vp; /* The vnode to read the stream from */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t voff; /* The current offset in the stream */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t bytes_read;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * A record that has had its payload read in, but hasn't yet been handed
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * off to the worker thread.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct receive_record_arg *rrd;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /* A record that has had its header read in, but not its payload. */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct receive_record_arg *next_rrd;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens zio_cksum_t cksum;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens zio_cksum_t prev_cksum;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int err;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie boolean_t byteswap;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /* Sorted list of objects not to issue prefetches for. */
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie struct objlist ignore_objlist;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens};
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alttypedef struct guid_map_entry {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt uint64_t guid;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt dsl_dataset_t *gme_ds;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt avl_node_t avlnode;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt} guid_map_entry_t;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Altstatic int
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Altguid_compare(const void *arg1, const void *arg2)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt{
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt const guid_map_entry_t *gmep1 = arg1;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt const guid_map_entry_t *gmep2 = arg2;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (gmep1->guid < gmep2->guid)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (-1);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt else if (gmep1->guid > gmep2->guid)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (1);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (0);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt}
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirbystatic void
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirbyfree_guid_map_onexit(void *arg)
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby{
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby avl_tree_t *ca = arg;
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby void *cookie = NULL;
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby guid_map_entry_t *gmep;
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_long_rele(gmep->gme_ds, gmep);
de8d9cff565e928d0ace86f3ea0e2b15094d61dfMatthew Ahrens dsl_dataset_rele(gmep->gme_ds, gmep);
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby kmem_free(gmep, sizeof (guid_map_entry_t));
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby }
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby avl_destroy(ca);
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby kmem_free(ca, sizeof (avl_tree_t));
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby}
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_read(struct receive_arg *ra, int len, void *buf)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens int done = 0;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal /*
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * The code doesn't rely on this (lengths being multiples of 8). See
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal * comment in dump_bytes.
c20404ff77119516354b0d112d28b7ea0dadd303Eli Rosenthal */
fb09f5aad449c97fe309678f3f604982b563a96fMadhav Suresh ASSERT0(len % 8);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens while (done < len) {
efb80947b01107dafbbf0157de0d7e1af6551259ahrens ssize_t resid;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens ra->err = vn_rdwr(UIO_READ, ra->vp,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens (char *)buf + done, len - done,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens ra->voff, UIO_SYSSPACE, FAPPEND,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens RLIM64_INFINITY, CRED(), &resid);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (resid == len - done) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Note: ECKSUM indicates that the receive
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * was interrupted and can potentially be resumed.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ra->err = SET_ERROR(ECKSUM);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens ra->voff += len - done - resid;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens done = len - resid;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ra->err != 0)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (ra->err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ra->bytes_read += len;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens ASSERT3U(done, ==, len);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic void
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrensbyteswap_record(dmu_replay_record_t *drr)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drr->drr_type = BSWAP_32(drr->drr_type);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens switch (drr->drr_type) {
efb80947b01107dafbbf0157de0d7e1af6551259ahrens case DRR_BEGIN:
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_begin.drr_magic);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_begin.drr_versioninfo);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_begin.drr_creation_time);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO32(drr_begin.drr_type);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens DO32(drr_begin.drr_flags);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_begin.drr_toguid);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_begin.drr_fromguid);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens break;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens case DRR_OBJECT:
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_object.drr_object);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO32(drr_object.drr_type);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO32(drr_object.drr_bonustype);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO32(drr_object.drr_blksz);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO32(drr_object.drr_bonuslen);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_object.drr_toguid);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens break;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens case DRR_FREEOBJECTS:
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_freeobjects.drr_firstobj);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_freeobjects.drr_numobjs);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_freeobjects.drr_toguid);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens break;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens case DRR_WRITE:
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_write.drr_object);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO32(drr_write.drr_type);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_write.drr_offset);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel DO64(drr_write.drr_logical_size);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_write.drr_toguid);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
8e7144747a94247ce9dbb4fb1f67bdcb666b30d8Lori Alt DO64(drr_write.drr_key.ddk_prop);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel DO64(drr_write.drr_compressed_size);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt break;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt case DRR_WRITE_BYREF:
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_write_byref.drr_object);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_write_byref.drr_offset);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_write_byref.drr_length);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_write_byref.drr_toguid);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_write_byref.drr_refguid);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_write_byref.drr_refobject);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_write_byref.drr_refoffset);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens drr_key.ddk_cksum);
8e7144747a94247ce9dbb4fb1f67bdcb666b30d8Lori Alt DO64(drr_write_byref.drr_key.ddk_prop);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens break;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens case DRR_WRITE_EMBEDDED:
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DO64(drr_write_embedded.drr_object);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DO64(drr_write_embedded.drr_offset);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DO64(drr_write_embedded.drr_length);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DO64(drr_write_embedded.drr_toguid);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DO32(drr_write_embedded.drr_lsize);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens DO32(drr_write_embedded.drr_psize);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens break;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens case DRR_FREE:
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_free.drr_object);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_free.drr_offset);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens DO64(drr_free.drr_length);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_free.drr_toguid);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens break;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum case DRR_SPILL:
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum DO64(drr_spill.drr_object);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum DO64(drr_spill.drr_length);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum DO64(drr_spill.drr_toguid);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum break;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens case DRR_END:
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DO64(drr_end.drr_toguid);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens break;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (drr->drr_type != DRR_BEGIN) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#undef DO64
efb80947b01107dafbbf0157de0d7e1af6551259ahrens#undef DO32
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrensstatic inline uint8_t
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrensdeduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens{
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens if (bonus_type == DMU_OT_SA) {
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens return (1);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens } else {
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens return (1 +
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens }
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens}
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensstatic void
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrenssave_resume_state(struct receive_writer_arg *rwa,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t object, uint64_t offset, dmu_tx_t *tx)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (!rwa->resumable)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * We use ds_resume_bytes[] != 0 to indicate that we need to
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * update this on disk, so it must not be 0.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(rwa->bytes_read != 0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * We only resume from write records, which have a valid
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * (non-meta-dnode) object number.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(object != 0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * For resuming to work correctly, we must receive records in order,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * sorted by object,offset. This is checked by the callers, but
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * assert it here for good measure.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT3U(rwa->bytes_read, >=,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie void *data)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens dmu_object_info_t doi;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_tx_t *tx;
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens uint64_t object;
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens int err;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens if (drro->drr_type == DMU_OT_NONE ||
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden !DMU_OT_IS_VALID(drro->drr_type) ||
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden !DMU_OT_IS_VALID(drro->drr_bonustype) ||
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
efb80947b01107dafbbf0157de0d7e1af6551259ahrens P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drro->drr_blksz < SPA_MINBLOCKSIZE ||
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drro->drr_bonuslen > DN_MAX_BONUSLEN) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dmu_object_info(rwa->os, drro->drr_object, &doi);
2bf405a25eb25f79638fc951ff8d8857ad384417Mark Maybee
2bf405a25eb25f79638fc951ff8d8857ad384417Mark Maybee if (err != 0 && err != ENOENT)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
2bf405a25eb25f79638fc951ff8d8857ad384417Mark Maybee
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens /*
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens * If we are losing blkptrs or changing the block size this must
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens * be a new file instance. We must clear out the previous file
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens * contents before we can change this type of metadata in the dnode.
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens */
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens if (err == 0) {
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens int nblkptr;
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens nblkptr = deduce_nblkptr(drro->drr_bonustype,
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens drro->drr_bonuslen);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens if (drro->drr_blksz != doi.doi_data_block_size ||
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens nblkptr < doi.doi_nblkptr) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dmu_free_long_range(rwa->os, drro->drr_object,
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens 0, DMU_OBJECT_END);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens if (err != 0)
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens return (SET_ERROR(EINVAL));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens }
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie tx = dmu_tx_create(rwa->os);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens dmu_tx_hold_bonus(tx, object);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens err = dmu_tx_assign(tx, TXG_WAIT);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens if (err != 0) {
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens dmu_tx_abort(tx);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens return (err);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens }
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens if (object == DMU_NEW_OBJECT) {
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens /* currently free, want to be allocated */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dmu_object_claim(rwa->os, drro->drr_object,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drro->drr_type, drro->drr_blksz,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drro->drr_bonustype, drro->drr_bonuslen, tx);
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens } else if (drro->drr_type != doi.doi_type ||
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens drro->drr_blksz != doi.doi_data_block_size ||
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens drro->drr_bonustype != doi.doi_bonus_type ||
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens drro->drr_bonuslen != doi.doi_bonus_size) {
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens /* currently allocated, but with different properties */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dmu_object_reclaim(rwa->os, drro->drr_object,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drro->drr_type, drro->drr_blksz,
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens drro->drr_bonustype, drro->drr_bonuslen, tx);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
e77d42eaa49fe55bfae1e0e0065c6e99affc001bMatthew Ahrens dmu_tx_commit(tx);
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum }
2bf405a25eb25f79638fc951ff8d8857ad384417Mark Maybee
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_object_set_checksum(rwa->os, drro->drr_object,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens drro->drr_checksumtype, tx);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_object_set_compress(rwa->os, drro->drr_object,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens drro->drr_compress, tx);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
adee0b6f10e370500326bde344e66b6662e0b9dcTim Haley if (data != NULL) {
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_buf_t *db;
adee0b6f10e370500326bde344e66b6662e0b9dcTim Haley
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_buf_will_dirty(db, tx);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
1934e92fc930c49429ad71a8ca97340f33227e78maybee ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
1934e92fc930c49429ad71a8ca97340f33227e78maybee bcopy(data, db->db_data, drro->drr_bonuslen);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (rwa->byteswap) {
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden dmu_object_byteswap_t byteswap =
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden DMU_OT_BYTESWAP(drro->drr_bonustype);
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden dmu_ot_byteswap[byteswap].ob_func(db->db_data,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drro->drr_bonuslen);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_buf_rele(db, FTAG);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_tx_commit(tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens/* ARGSUSED */
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_freeobjects(struct receive_writer_arg *rwa,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens struct drr_freeobjects *drrfo)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
efb80947b01107dafbbf0157de0d7e1af6551259ahrens uint64_t obj;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie int next_err = 0;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens for (obj = drrfo->drr_firstobj;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
efb80947b01107dafbbf0157de0d7e1af6551259ahrens int err;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (dmu_object_info(rwa->os, obj, NULL) != 0)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens continue;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dmu_free_long_object(rwa->os, obj);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie if (next_err != ESRCH)
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie return (next_err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie arc_buf_t *abuf)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_tx_t *tx;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens int err;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden !DMU_OT_IS_VALID(drrw->drr_type))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * For resuming to work, records must be in increasing order
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * by (object, offset).
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drrw->drr_object < rwa->last_object ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (drrw->drr_object == rwa->last_object &&
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drrw->drr_offset < rwa->last_offset)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rwa->last_object = drrw->drr_object;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rwa->last_offset = drrw->drr_offset;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
8a9047098ad8ce5afa38b6d012c8b509bb619f40Matthew Ahrens return (SET_ERROR(EINVAL));
8a9047098ad8ce5afa38b6d012c8b509bb619f40Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie tx = dmu_tx_create(rwa->os);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_tx_hold_write(tx, drrw->drr_object,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel drrw->drr_offset, drrw->drr_logical_size);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens err = dmu_tx_assign(tx, TXG_WAIT);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_tx_abort(tx);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (rwa->byteswap) {
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden dmu_object_byteswap_t byteswap =
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden DMU_OT_BYTESWAP(drrw->drr_type);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel DRR_WRITE_PAYLOAD_SIZE(drrw));
ad135b5d644628e791c3188a6ecbd9c257961ef8Christopher Siden }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel /* use the bonus buf to look up the dnode in dmu_assign_arcbuf */
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens dmu_buf_t *bonus;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (SET_ERROR(EINVAL));
8a9047098ad8ce5afa38b6d012c8b509bb619f40Matthew Ahrens dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Note: If the receive fails, we want the resume stream to start
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * with the same record that we last successfully received (as opposed
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * to the next record), so that we can verify that we are
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * resuming from the correct location.
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens dmu_tx_commit(tx);
8a9047098ad8ce5afa38b6d012c8b509bb619f40Matthew Ahrens dmu_buf_rele(bonus, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (0);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt/*
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * streams to refer to a copy of the data that is already on the
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * system because it came in earlier in the stream. This function
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * finds the earlier copy of the data, and uses that copy instead of
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * data from the stream to fulfill this write.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Altstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_write_byref(struct receive_writer_arg *rwa,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_write_byref *drrwbr)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt{
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt dmu_tx_t *tx;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt int err;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt guid_map_entry_t gmesrch;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt guid_map_entry_t *gmep;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens avl_index_t where;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt objset_t *ref_os = NULL;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt dmu_buf_t *dbp;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /*
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * If the GUID of the referenced dataset is different from the
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt * GUID of the target dataset, find the referenced dataset.
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt gmesrch.guid = drrwbr->drr_refguid;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt &where)) == NULL) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt } else {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ref_os = rwa->os;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (err != 0)
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (err);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie tx = dmu_tx_create(rwa->os);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt dmu_tx_hold_write(tx, drrwbr->drr_object,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrwbr->drr_offset, drrwbr->drr_length);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt err = dmu_tx_assign(tx, TXG_WAIT);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt dmu_tx_abort(tx);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (err);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_write(rwa->os, drrwbr->drr_object,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt dmu_buf_rele(dbp, FTAG);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* See comment in restore_write. */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt dmu_tx_commit(tx);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt return (0);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt}
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_write_embedded(struct receive_writer_arg *rwa,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens struct drr_write_embedded *drrwe, void *data)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens{
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens dmu_tx_t *tx;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens int err;
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (EINVAL);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (EINVAL);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (EINVAL);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (EINVAL);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie tx = dmu_tx_create(rwa->os);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_tx_hold_write(tx, drrwe->drr_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drrwe->drr_offset, drrwe->drr_length);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens err = dmu_tx_assign(tx, TXG_WAIT);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens if (err != 0) {
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens dmu_tx_abort(tx);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (err);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens }
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dmu_write_embedded(rwa->os, drrwe->drr_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drrwe->drr_offset, data, drrwe->drr_etype,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* See comment in restore_write. */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens dmu_tx_commit(tx);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens return (0);
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens}
5d7b4d438c4a51eccc95e77a83a437b4d48380ebMatthew Ahrens
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaumstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie void *data)
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum{
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_tx_t *tx;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_buf_t *db, *db_spill;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum int err;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum if (drrs->drr_length < SPA_MINBLOCKSIZE ||
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_buf_rele(db, FTAG);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum return (err);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum }
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie tx = dmu_tx_create(rwa->os);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_tx_hold_spill(tx, db->db_object);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum err = dmu_tx_assign(tx, TXG_WAIT);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0) {
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_buf_rele(db, FTAG);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_buf_rele(db_spill, FTAG);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_tx_abort(tx);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum return (err);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum }
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_buf_will_dirty(db_spill, tx);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum if (db_spill->db_size < drrs->drr_length)
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum VERIFY(0 == dbuf_spill_set_blksz(db_spill,
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum drrs->drr_length, tx));
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum bcopy(data, db_spill->db_data, drrs->drr_length);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_buf_rele(db, FTAG);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_buf_rele(db_spill, FTAG);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum dmu_tx_commit(tx);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum return (0);
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum}
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum
efb80947b01107dafbbf0157de0d7e1af6551259ahrens/* ARGSUSED */
efb80947b01107dafbbf0157de0d7e1af6551259ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
efb80947b01107dafbbf0157de0d7e1af6551259ahrens int err;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens if (drrf->drr_length != -1ULL &&
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens return (SET_ERROR(EINVAL));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dmu_free_long_range(rwa->os, drrf->drr_object,
efb80947b01107dafbbf0157de0d7e1af6551259ahrens drrf->drr_offset, drrf->drr_length);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
efb80947b01107dafbbf0157de0d7e1af6551259ahrens return (err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens/* used to destroy the drc_ds on error */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (drc->drc_resumable) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* wait for our resume state to be written to disk */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens } else {
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_name(drc->drc_ds, name);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) dsl_destroy_head(name);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens}
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrensstatic void
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_cksum(struct receive_arg *ra, int len, void *buf)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens{
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (ra->byteswap) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens } else {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens fletcher_4_incremental_native(buf, len, &ra->cksum);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens}
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Read the payload into a buffer of size len, and update the current record's
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * payload field.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Allocate ra->next_rrd and read the next record's header into
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * ra->next_rrd->header.
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens * Verify checksum of payload and next record.
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens */
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens{
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens int err;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (len != 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = receive_read(ra, len, buf);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (err != 0)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (err);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens receive_cksum(ra, len, buf);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* note: rrd is NULL when reading the begin record's payload */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (ra->rrd != NULL) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ra->rrd->payload = buf;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ra->rrd->payload_size = len;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ra->rrd->bytes_read = ra->bytes_read;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens ra->prev_cksum = ra->cksum;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read(ra, sizeof (ra->next_rrd->header),
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &ra->next_rrd->header);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ra->next_rrd->bytes_read = ra->bytes_read;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err != 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra->next_rrd = NULL;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra->next_rrd = NULL;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (SET_ERROR(EINVAL));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens /*
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens * Note: checksum is of everything up to but not including the
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens * checksum itself.
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens */
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));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie receive_cksum(ra,
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &ra->next_rrd->header);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zio_cksum_t cksum_orig =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie zio_cksum_t *cksump =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (ra->byteswap)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie byteswap_record(&ra->next_rrd->header);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra->next_rrd = NULL;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (SET_ERROR(ECKSUM));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (0);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens}
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagneliestatic void
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelieobjlist_create(struct objlist *list)
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie{
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie list_create(&list->list, sizeof (struct receive_objnode),
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie offsetof(struct receive_objnode, node));
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie list->last_lookup = 0;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie}
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagneliestatic void
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelieobjlist_destroy(struct objlist *list)
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie{
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie for (struct receive_objnode *n = list_remove_head(&list->list);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie n != NULL; n = list_remove_head(&list->list)) {
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie kmem_free(n, sizeof (*n));
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie }
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie list_destroy(&list->list);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie}
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie/*
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * This function looks through the objlist to see if the specified object number
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * is contained in the objlist. In the process, it will remove all object
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * numbers in the list that are smaller than the specified object number. Thus,
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * any lookup of an object number smaller than a previously looked up object
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * number will always return false; therefore, all lookups should be done in
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * ascending order.
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie */
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagneliestatic boolean_t
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelieobjlist_exists(struct objlist *list, uint64_t object)
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie{
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie struct receive_objnode *node = list_head(&list->list);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie ASSERT3U(object, >=, list->last_lookup);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie list->last_lookup = object;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie while (node != NULL && node->object < object) {
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie VERIFY3P(node, ==, list_remove_head(&list->list));
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie kmem_free(node, sizeof (*node));
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie node = list_head(&list->list);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie }
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie return (node != NULL && node->object == object);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie}
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie/*
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * The objlist is a list of object numbers stored in ascending order. However,
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * the insertion of new object numbers does not seek out the correct location to
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * store a new object number; instead, it appends it to the list for simplicity.
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * Thus, any users must take care to only insert new object numbers in ascending
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie * order.
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie */
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagneliestatic void
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelieobjlist_insert(struct objlist *list, uint64_t object)
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie{
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie node->object = object;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie#ifdef ZFS_DEBUG
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie struct receive_objnode *last_object = list_tail(&list->list);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie ASSERT3U(node->object, >, last_objnum);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie#endif
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie list_insert_tail(&list->list, node);
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie}
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Issue the prefetch reads for any necessary indirect blocks.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * We use the object ignore list to tell us whether or not to issue prefetches
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * for a given object. We do this for both correctness (in case the blocksize
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * of an object has changed) and performance (if the object doesn't exist, don't
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * needlessly try to issue prefetches). We also trim the list as we go through
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * the stream to prevent it from growing to an unbounded size.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * The object numbers within will always be in sorted order, and any write
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * records we see will also be in sorted order, but they're not sorted with
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * respect to each other (i.e. we can get several object records before
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * receiving each object's write records). As a result, once we've reached a
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * given object number, we can safely remove any reference to lower object
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * numbers in the ignore list. In practice, we receive up to 32 object records
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * before receiving write records, so the list can have up to 32 nodes in it.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/* ARGSUSED */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestatic void
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_read_prefetch(struct receive_arg *ra,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint64_t object, uint64_t offset, uint64_t length)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie{
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie if (!objlist_exists(&ra->ignore_objlist, object)) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_prefetch(ra->os, object, 1, offset, length,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ZIO_PRIORITY_SYNC_READ);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie}
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Read records off the stream, issuing any necessary prefetches.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrensstatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_read_record(struct receive_arg *ra)
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens{
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens int err;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie switch (ra->rrd->header.drr_type) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens case DRR_OBJECT:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie void *buf = kmem_zalloc(size, KM_SLEEP);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_object_info_t doi;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read_payload_and_next_header(ra, size, buf);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err != 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(buf, size);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = dmu_object_info(ra->os, drro->drr_object, &doi);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * See receive_read_prefetch for an explanation why we're
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * storing this object in the ignore_obj_list.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err == ENOENT ||
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie objlist_insert(&ra->ignore_objlist, drro->drr_object);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = 0;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens case DRR_FREEOBJECTS:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read_payload_and_next_header(ra, 0, NULL);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens case DRR_WRITE:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel arc_buf_t *abuf;
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel if (DRR_WRITE_COMPRESSED(drrw)) {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(drrw->drr_compressed_size, >, 0);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT3U(drrw->drr_logical_size, >=,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel drrw->drr_compressed_size);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel ASSERT(!is_meta);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel abuf = arc_loan_compressed_buf(
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel dmu_objset_spa(ra->os),
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel drrw->drr_compressed_size, drrw->drr_logical_size,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel drrw->drr_compressiontype);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel } else {
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel abuf = arc_loan_buf(dmu_objset_spa(ra->os),
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel is_meta, drrw->drr_logical_size);
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read_payload_and_next_header(ra,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err != 0) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens dmu_return_arcbuf(abuf);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra->rrd->write_buf = abuf;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
5602294fda888d923d57a78bafdaf48ae6223deaDan Kimmel drrw->drr_logical_size);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (err);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens case DRR_WRITE_BYREF:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_write_byref *drrwb =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &ra->rrd->header.drr_u.drr_write_byref;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read_payload_and_next_header(ra, 0, NULL);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie drrwb->drr_length);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens case DRR_WRITE_EMBEDDED:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens struct drr_write_embedded *drrwe =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &ra->rrd->header.drr_u.drr_write_embedded;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie void *buf = kmem_zalloc(size, KM_SLEEP);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read_payload_and_next_header(ra, size, buf);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err != 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(buf, size);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie drrwe->drr_length);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens case DRR_FREE:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * It might be beneficial to prefetch indirect blocks here, but
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * we don't really have the data to decide for sure.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read_payload_and_next_header(ra, 0, NULL);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens case DRR_END:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(ECKSUM));
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (0);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens case DRR_SPILL:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read_payload_and_next_header(ra, drrs->drr_length,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie buf);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (err != 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(buf, drrs->drr_length);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens default:
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (SET_ERROR(EINVAL));
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens}
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Commit the records to the pool.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestatic int
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_process_record(struct receive_writer_arg *rwa,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct receive_record_arg *rrd)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie{
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie int err;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens /* Processing in order, therefore bytes_read should be increasing. */
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rwa->bytes_read = rrd->bytes_read;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie switch (rrd->header.drr_type) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie case DRR_OBJECT:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_object *drro = &rrd->header.drr_u.drr_object;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_object(rwa, drro, rrd->payload);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(rrd->payload, rrd->payload_size);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd->payload = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie case DRR_FREEOBJECTS:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_freeobjects *drrfo =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &rrd->header.drr_u.drr_freeobjects;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (receive_freeobjects(rwa, drrfo));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie case DRR_WRITE:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_write *drrw = &rrd->header.drr_u.drr_write;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_write(rwa, drrw, rrd->write_buf);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /* if receive_write() is successful, it consumes the arc_buf */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err != 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_return_arcbuf(rrd->write_buf);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd->write_buf = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd->payload = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie case DRR_WRITE_BYREF:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_write_byref *drrwbr =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &rrd->header.drr_u.drr_write_byref;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (receive_write_byref(rwa, drrwbr));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie case DRR_WRITE_EMBEDDED:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_write_embedded *drrwe =
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie &rrd->header.drr_u.drr_write_embedded;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_write_embedded(rwa, drrwe, rrd->payload);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(rrd->payload, rrd->payload_size);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd->payload = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie case DRR_FREE:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_free *drrf = &rrd->header.drr_u.drr_free;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (receive_free(rwa, drrf));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie case DRR_SPILL:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_spill(rwa, drrs, rrd->payload);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(rrd->payload, rrd->payload_size);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd->payload = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (err);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie default:
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie return (SET_ERROR(EINVAL));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie}
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * dmu_recv_stream's worker thread; pull records off the queue, and then call
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * receive_process_record When we're done, signal the main thread and exit.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliestatic void
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagneliereceive_writer_thread(void *arg)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie{
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct receive_writer_arg *rwa = arg;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct receive_record_arg *rrd;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd = bqueue_dequeue(&rwa->q)) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * If there's an error, the main thread will stop putting things
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * on the queue, but we need to clear everything in it before we
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * can exit.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (rwa->err == 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rwa->err = receive_process_record(rwa, rrd);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie } else if (rrd->write_buf != NULL) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie dmu_return_arcbuf(rrd->write_buf);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd->write_buf = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd->payload = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie } else if (rrd->payload != NULL) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(rrd->payload, rrd->payload_size);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rrd->payload = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(rrd, sizeof (*rrd));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(rrd, sizeof (*rrd));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_enter(&rwa->mutex);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rwa->done = B_TRUE;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie cv_signal(&rwa->cv);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_exit(&rwa->mutex);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie}
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensstatic int
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrensresume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens{
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t val;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t dsobj = dmu_objset_id(ra->os);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint64_t resume_obj, resume_off;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (nvlist_lookup_uint64(begin_nvl,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume_object", &resume_obj) != 0 ||
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_lookup_uint64(begin_nvl,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens "resume_offset", &resume_off) != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_lookup(mos, dsobj,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (resume_obj != val)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens VERIFY0(zap_lookup(mos, dsobj,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (resume_off != val)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (SET_ERROR(EINVAL));
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens return (0);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens}
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie/*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * Read in the stream's records, one by one, and apply them to the pool. There
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * are two threads involved; the thread that calls this function will spin up a
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * worker thread, read the records off the stream one by one, and issue
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * prefetches for any necessary indirect blocks. It will then push the records
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * onto an internal blocking queue. The worker thread will pull the records off
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * the queue, and actually write the data into the DMU. This way, the worker
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * thread doesn't have to wait for reads to complete, since everything it needs
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * (the indirect blocks) will be prefetched.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens * NB: callers *must* call dmu_recv_end() if this succeeds.
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens */
efb80947b01107dafbbf0157de0d7e1af6551259ahrensint
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirbydmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby int cleanup_fd, uint64_t *action_handlep)
efb80947b01107dafbbf0157de0d7e1af6551259ahrens{
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens int err = 0;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct receive_arg ra = { 0 };
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie struct receive_writer_arg rwa = { 0 };
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt int featureflags;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_t *begin_nvl = NULL;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ra.byteswap = drc->drc_byteswap;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ra.cksum = drc->drc_cksum;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens ra.vp = vp;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens ra.voff = *voffp;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (dsl_dataset_is_zapified(drc->drc_ds)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens sizeof (ra.bytes_read), 1, &ra.bytes_read);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie objlist_create(&ra.ignore_objlist);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens /* these were verified in dmu_recv_begin */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt DMU_SUBSTREAM);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrens /*
efb80947b01107dafbbf0157de0d7e1af6551259ahrens * Open the objset we are modifying.
efb80947b01107dafbbf0157de0d7e1af6551259ahrens */
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt /* if this stream is dedup'ed, set up the avl tree for guid mapping */
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby minor_t minor;
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby if (cleanup_fd == -1) {
be6fd75a69ae679453d9cda5bff3326111e6d1caMatthew Ahrens ra.err = SET_ERROR(EBADF);
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby goto out;
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby }
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ra.err != 0) {
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby cleanup_fd = -1;
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby goto out;
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby }
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby if (*action_handlep == 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rwa.guid_to_ds_map =
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie avl_create(rwa.guid_to_ds_map, guid_compare,
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby sizeof (guid_map_entry_t),
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby offsetof(guid_map_entry_t, avlnode));
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens err = zfs_onexit_add_cb(minor,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie free_guid_map_onexit, rwa.guid_to_ds_map,
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby action_handlep);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ra.err != 0)
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby goto out;
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby } else {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens err = zfs_onexit_cb_data(minor, *action_handlep,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (void **)&rwa.guid_to_ds_map);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (ra.err != 0)
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby goto out;
c99e4bdccfb4ac4da569c64a43baaf908d726329Chris Kirby }
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt }
9e69d7d0feb2a0394435ca1d9746c4c3d7bf9b22Lori Alt
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens void *payload = NULL;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (payloadlen != 0)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens payload = kmem_alloc(payloadlen, KM_SLEEP);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (err != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (payloadlen != 0)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens kmem_free(payload, payloadlen);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens goto out;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (payloadlen != 0) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens kmem_free(payload, payloadlen);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (err != 0)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens goto out;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens err = resume_check(&ra, begin_nvl);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (err != 0)
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens goto out;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (void) bqueue_init(&rwa.q, zfs_recv_queue_length,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie offsetof(struct receive_record_arg, node));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rwa.os = ra.os;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie rwa.byteswap = drc->drc_byteswap;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens rwa.resumable = drc->drc_resumable;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie (void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, curproc,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie TS_RUN, minclsyspri);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /*
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * We're reading rwa.err without locks, which is safe since we are the
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * only reader, and the worker thread is the only writer. It's ok if we
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * miss a write for an iteration or two of the loop, since the writer
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * thread will keep freeing records we send it until we send it an eos
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * marker.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie *
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * We can leave this loop in 3 ways: First, if rwa.err is
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * non-zero. In that case, the writer thread will free the rrd we just
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * pushed. Second, if we're interrupted; in that case, either it's the
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * has been handed off to the writer thread who will free it. Finally,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * if receive_read_record fails or we're at the end of the stream, then
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie * we free ra.rrd and exit.
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie while (rwa.err == 0) {
efb80947b01107dafbbf0157de0d7e1af6551259ahrens if (issig(JUSTLOOKING) && issig(FORREAL)) {
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens err = SET_ERROR(EINTR);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens break;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ASSERT3P(ra.rrd, ==, NULL);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra.rrd = ra.next_rrd;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra.next_rrd = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie /* Allocates and loads header into ra.next_rrd */
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = receive_read_record(&ra);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (ra.rrd->header.drr_type == DRR_END || err != 0) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie kmem_free(ra.rrd, sizeof (*ra.rrd));
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra.rrd = NULL;
0a586cea3ceec7e5e50e7e54c745082a7a333ac2Mark Shellenbaum break;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_enqueue(&rwa.q, ra.rrd,
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie sizeof (struct receive_record_arg) + ra.rrd->payload_size);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra.rrd = NULL;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (ra.next_rrd == NULL)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie ra.next_rrd->eos_marker = B_TRUE;
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_enter(&rwa.mutex);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie while (!rwa.done) {
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie cv_wait(&rwa.cv, &rwa.mutex);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_exit(&rwa.mutex);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie cv_destroy(&rwa.cv);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie mutex_destroy(&rwa.mutex);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie bqueue_destroy(&rwa.q);
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie if (err == 0)
a2cdcdd260232b58202b11a9bfc0103c9449ed52Paul Dagnelie err = rwa.err;
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
efb80947b01107dafbbf0157de0d7e1af6551259ahrensout:
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens nvlist_free(begin_nvl);
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby zfs_onexit_fd_rele(cleanup_fd);
a7f53a5629374ca27c5696ace9a1946c2ca050f4Chris Kirby
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens if (err != 0) {
efb80947b01107dafbbf0157de0d7e1af6551259ahrens /*
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * Clean up references. If receive is not resumable,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * destroy what we created, so we don't leave it in
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens * the inconsistent state.
efb80947b01107dafbbf0157de0d7e1af6551259ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_cleanup_ds(drc);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens }
efb80947b01107dafbbf0157de0d7e1af6551259ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens *voffp = ra.voff;
68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76fPaul Dagnelie objlist_destroy(&ra.ignore_objlist);
98110f08fa182032082d98be2ddb9391fcd62bf1Matthew Ahrens return (err);
efb80947b01107dafbbf0157de0d7e1af6551259ahrens}
f18faf3f3e5def85fdfff681617d227703ace2adek
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensstatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdmu_recv_end_check(void *arg, dmu_tx_t *tx)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_cookie_t *drc = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!drc->drc_newfs) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin_head;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (drc->drc_force) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens /*
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens * We will destroy any snapshots in tofs (i.e. before
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens * origin_head) that are after the origin (which is
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens * the snap before drc_ds, because drc_ds can not
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens * have any snaps of its own).
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs uint64_t obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs while (obj !=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_t *snap;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens error = dsl_dataset_hold_obj(dp, obj, FTAG,
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens &snap);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (error != 0)
beddaa9ce797b9deaafc22b4f156d23f9b45c32dAndriy Gapon break;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (snap->ds_dir != origin_head->ds_dir)
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens error = SET_ERROR(EINVAL);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (error == 0) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens error = dsl_destroy_snapshot_check_impl(
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens snap, B_FALSE);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens }
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_rele(snap, FTAG);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (error != 0)
beddaa9ce797b9deaafc22b4f156d23f9b45c32dAndriy Gapon break;
beddaa9ce797b9deaafc22b4f156d23f9b45c32dAndriy Gapon }
beddaa9ce797b9deaafc22b4f156d23f9b45c32dAndriy Gapon if (error != 0) {
beddaa9ce797b9deaafc22b4f156d23f9b45c32dAndriy Gapon dsl_dataset_rele(origin_head, FTAG);
beddaa9ce797b9deaafc22b4f156d23f9b45c32dAndriy Gapon return (error);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens }
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski origin_head, drc->drc_force, drc->drc_owner, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(origin_head, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_snapshot_check_impl(origin_head,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(origin_head, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
a2afb611b30628fb74ad9eade4ae465f9031e262Jerry Jelinek drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens}
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3cb34c601f3ef3016f638574f5982e80c3735c71ahrensstatic void
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensdmu_recv_end_sync(void *arg, dmu_tx_t *tx)
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_cookie_t *drc = arg;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp = dmu_tx_pool(tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens tx, "snap=%s", drc->drc_tosnap);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (!drc->drc_newfs) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *origin_head;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens &origin_head));
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens if (drc->drc_force) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens /*
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens * Destroy any snapshots of drc_tofs (origin_head)
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens * after the origin (the snap before drc_ds).
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens */
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs uint64_t obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs while (obj !=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_t *snap;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens &snap));
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_destroy_snapshot_sync_impl(snap,
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens B_FALSE, tx);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens dsl_dataset_rele(snap, FTAG);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens }
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens }
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens VERIFY3P(drc->drc_ds->ds_prev, ==,
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens origin_head->ds_prev);
34f2f8cf94052481c81be2e134b94a00b501bf21Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens origin_head, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_sync_impl(origin_head,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_tosnap, tx);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* set snapshot's creation time and guid */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_drrb->drr_creation_time;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_drrb->drr_toguid;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens ~DS_FLAG_INCONSISTENT;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(origin_head)->ds_flags &=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ~DS_FLAG_INCONSISTENT;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson drc->drc_newsnapobj =
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_rele(origin_head, FTAG);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_destroy_head_sync_impl(drc->drc_ds, tx);
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski if (drc->drc_owner != NULL)
91948b51b8e978ddc88a36b2bc3ae83c20cdc9aaKeith M Wesolowski VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_t *ds = drc->drc_ds;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /* set snapshot's creation time and guid */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_drrb->drr_creation_time;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_guid =
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs drc->drc_drrb->drr_toguid;
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds->ds_prev)->ds_flags &=
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs ~DS_FLAG_INCONSISTENT;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens if (dsl_dataset_has_resume_receive_state(ds)) {
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_FROMGUID, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_OBJECT, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_OFFSET, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_BYTES, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_TOGUID, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens DS_FIELD_RESUME_TONAME, tx);
9c3fd1216fa7fb02cfbc78a2518a686d54b48ab8Matthew Ahrens }
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson drc->drc_newsnapobj =
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * Release the hold from dmu_recv_begin. This must be done before
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * we return to open context, so that when we free the dataset's dnode,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * we can evict its bonus buffer.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_ds = NULL;
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens}
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenkostatic int
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensadd_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_t *dp;
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko dsl_dataset_t *snapds;
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko guid_map_entry_t *gmep;
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko int err;
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko ASSERT(guid_map != NULL);
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens err = dsl_pool_hold(name, FTAG, &dp);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (err != 0)
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (err);
de8d9cff565e928d0ace86f3ea0e2b15094d61dfMatthew Ahrens gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
de8d9cff565e928d0ace86f3ea0e2b15094d61dfMatthew Ahrens err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko if (err == 0) {
c1379625401dfbe1c39b79136dd384a571d47fdeJustin T. Gibbs gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko gmep->gme_ds = snapds;
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko avl_add(guid_map, gmep);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_long_hold(snapds, gmep);
de8d9cff565e928d0ace86f3ea0e2b15094d61dfMatthew Ahrens } else {
de8d9cff565e928d0ace86f3ea0e2b15094d61dfMatthew Ahrens kmem_free(gmep, sizeof (*gmep));
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko }
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_pool_rele(dp, FTAG);
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko return (err);
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko}
ec5cf9d53a1d7280f3f1a6eadd7cdabdc99814ebAlexander Stetsenko
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrensstatic int dmu_recv_end_modified_blocks = 3;
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrensstatic int
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrensdmu_recv_existing_end(dmu_recv_cookie_t *drc)
f18faf3f3e5def85fdfff681617d227703ace2adek{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#ifdef _KERNEL
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens /*
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * We will be destroying the ds; make sure its origin is unmounted if
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens * necessary.
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens */
9adfa60d484ce2435f5af77cc99dcd4e692b6660Matthew Ahrens char name[ZFS_MAX_DATASET_NAME_LEN];
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dsl_dataset_name(drc->drc_ds, name);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens zfs_destroy_unmount_origin(name);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens#endif
3cb34c601f3ef3016f638574f5982e80c3735c71ahrens
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson return (dsl_sync_task(drc->drc_tofs,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_end_check, dmu_recv_end_sync, drc,
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
f18faf3f3e5def85fdfff681617d227703ace2adek}
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrensstatic int
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrensdmu_recv_new_end(dmu_recv_cookie_t *drc)
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson{
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson return (dsl_sync_task(drc->drc_tofs,
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson dmu_recv_end_check, dmu_recv_end_sync, drc,
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson}
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamsonint
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamsondmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens{
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens int error;
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson drc->drc_owner = owner;
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson if (drc->drc_newfs)
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson error = dmu_recv_new_end(drc);
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson else
2ad25b4055c18f39b3fb20bd5c8c61057e34f377Chris Williamson error = dmu_recv_existing_end(drc);
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens if (error != 0) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens dmu_recv_cleanup_ds(drc);
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens } else if (drc->drc_guid_to_ds_map != NULL) {
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens (void) add_ds_to_guidmap(drc->drc_tofs,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_guid_to_ds_map,
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens drc->drc_newsnapobj);
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens }
3b2aab18808792cbd248a12f1edf139b89833c13Matthew Ahrens return (error);
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens}
ae46e4c775f2becc5343ff90b60a95acb79735f9Matthew Ahrens
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens/*
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens * Return TRUE if this objset is currently being received into.
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens */
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrensboolean_t
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrensdmu_objset_is_receiving(objset_t *os)
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens{
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens return (os->os_dsl_dataset != NULL &&
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens os->os_dsl_dataset->ds_owner == dmu_recv_tag);
2f3d878000c3b33cde13e16c4f0a1ab15d883a18Matthew Ahrens}