xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision 68ecb2ec930c4b0f00acaf8e0abb2b19c4b8b76f)
1efb80947Sahrens /*
2efb80947Sahrens  * CDDL HEADER START
3efb80947Sahrens  *
4efb80947Sahrens  * The contents of this file are subject to the terms of the
5efb80947Sahrens  * Common Development and Distribution License (the "License").
6efb80947Sahrens  * You may not use this file except in compliance with the License.
7efb80947Sahrens  *
8efb80947Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9efb80947Sahrens  * or http://www.opensolaris.org/os/licensing.
10efb80947Sahrens  * See the License for the specific language governing permissions
11efb80947Sahrens  * and limitations under the License.
12efb80947Sahrens  *
13efb80947Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14efb80947Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15efb80947Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16efb80947Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17efb80947Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18efb80947Sahrens  *
19efb80947Sahrens  * CDDL HEADER END
20efb80947Sahrens  */
21efb80947Sahrens /*
22dc7cd546SMark Shellenbaum  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23ec5cf9d5SAlexander Stetsenko  * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
24ca0cc391SMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26e77d42eaSMatthew Ahrens  * Copyright 2014 HybridCluster. All rights reserved.
27ec5cf9d5SAlexander Stetsenko  */
28efb80947Sahrens 
29efb80947Sahrens #include <sys/dmu.h>
30efb80947Sahrens #include <sys/dmu_impl.h>
31efb80947Sahrens #include <sys/dmu_tx.h>
32efb80947Sahrens #include <sys/dbuf.h>
33efb80947Sahrens #include <sys/dnode.h>
34efb80947Sahrens #include <sys/zfs_context.h>
35efb80947Sahrens #include <sys/dmu_objset.h>
36efb80947Sahrens #include <sys/dmu_traverse.h>
37efb80947Sahrens #include <sys/dsl_dataset.h>
38efb80947Sahrens #include <sys/dsl_dir.h>
3992241e0bSTom Erickson #include <sys/dsl_prop.h>
40efb80947Sahrens #include <sys/dsl_pool.h>
41efb80947Sahrens #include <sys/dsl_synctask.h>
42efb80947Sahrens #include <sys/zfs_ioctl.h>
43efb80947Sahrens #include <sys/zap.h>
44efb80947Sahrens #include <sys/zio_checksum.h>
45dc7cd546SMark Shellenbaum #include <sys/zfs_znode.h>
46cde58dbcSMatthew Ahrens #include <zfs_fletcher.h>
479e69d7d0SLori Alt #include <sys/avl.h>
488e714474SLori Alt #include <sys/ddt.h>
49c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
503b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
513b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
525d7b4d43SMatthew Ahrens #include <sys/blkptr.h>
5378f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
545d7b4d43SMatthew Ahrens #include <sys/zfeature.h>
55a2cdcdd2SPaul Dagnelie #include <sys/bqueue.h>
56efb80947Sahrens 
5719b94df9SMatthew Ahrens /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
5819b94df9SMatthew Ahrens int zfs_send_corrupt_data = B_FALSE;
59a2cdcdd2SPaul Dagnelie int zfs_send_queue_length = 16 * 1024 * 1024;
60a2cdcdd2SPaul Dagnelie int zfs_recv_queue_length = 16 * 1024 * 1024;
6119b94df9SMatthew Ahrens 
623cb34c60Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
639c3fd121SMatthew Ahrens const char *recv_clone_name = "%recv";
643cb34c60Sahrens 
65a2cdcdd2SPaul Dagnelie #define	BP_SPAN(datablkszsec, indblkshift, level) \
66a2cdcdd2SPaul Dagnelie 	(((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
67a2cdcdd2SPaul Dagnelie 	(level) * (indblkshift - SPA_BLKPTRSHIFT)))
68a2cdcdd2SPaul Dagnelie 
699c3fd121SMatthew Ahrens static void byteswap_record(dmu_replay_record_t *drr);
709c3fd121SMatthew Ahrens 
71a2cdcdd2SPaul Dagnelie struct send_thread_arg {
72a2cdcdd2SPaul Dagnelie 	bqueue_t	q;
73a2cdcdd2SPaul Dagnelie 	dsl_dataset_t	*ds;		/* Dataset to traverse */
74a2cdcdd2SPaul Dagnelie 	uint64_t	fromtxg;	/* Traverse from this txg */
75a2cdcdd2SPaul Dagnelie 	int		flags;		/* flags to pass to traverse_dataset */
76a2cdcdd2SPaul Dagnelie 	int		error_code;
77a2cdcdd2SPaul Dagnelie 	boolean_t	cancel;
789c3fd121SMatthew Ahrens 	zbookmark_phys_t resume;
79a2cdcdd2SPaul Dagnelie };
80a2cdcdd2SPaul Dagnelie 
81a2cdcdd2SPaul Dagnelie struct send_block_record {
82a2cdcdd2SPaul Dagnelie 	boolean_t		eos_marker; /* Marks the end of the stream */
83a2cdcdd2SPaul Dagnelie 	blkptr_t		bp;
84a2cdcdd2SPaul Dagnelie 	zbookmark_phys_t	zb;
85a2cdcdd2SPaul Dagnelie 	uint8_t			indblkshift;
86a2cdcdd2SPaul Dagnelie 	uint16_t		datablkszsec;
87a2cdcdd2SPaul Dagnelie 	bqueue_node_t		ln;
88a2cdcdd2SPaul Dagnelie };
89a2cdcdd2SPaul Dagnelie 
90efb80947Sahrens static int
914e3c9f44SBill Pijewski dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
92efb80947Sahrens {
939c3fd121SMatthew Ahrens 	dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
94efb80947Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
95fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
96efb80947Sahrens 
974e3c9f44SBill Pijewski 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
98efb80947Sahrens 	    (caddr_t)buf, len,
99efb80947Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
1004e3c9f44SBill Pijewski 
1014e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
1024e3c9f44SBill Pijewski 	*dsp->dsa_off += len;
1034e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
1044e3c9f44SBill Pijewski 
1054e3c9f44SBill Pijewski 	return (dsp->dsa_err);
106efb80947Sahrens }
107efb80947Sahrens 
10898110f08SMatthew Ahrens /*
10998110f08SMatthew Ahrens  * For all record types except BEGIN, fill in the checksum (overlaid in
11098110f08SMatthew Ahrens  * drr_u.drr_checksum.drr_checksum).  The checksum verifies everything
11198110f08SMatthew Ahrens  * up to the start of the checksum itself.
11298110f08SMatthew Ahrens  */
11398110f08SMatthew Ahrens static int
11498110f08SMatthew Ahrens dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
11598110f08SMatthew Ahrens {
11698110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
11798110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
11898110f08SMatthew Ahrens 	fletcher_4_incremental_native(dsp->dsa_drr,
11998110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
12098110f08SMatthew Ahrens 	    &dsp->dsa_zc);
12198110f08SMatthew Ahrens 	if (dsp->dsa_drr->drr_type != DRR_BEGIN) {
12298110f08SMatthew Ahrens 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
12398110f08SMatthew Ahrens 		    drr_checksum.drr_checksum));
12498110f08SMatthew Ahrens 		dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
12598110f08SMatthew Ahrens 	}
12698110f08SMatthew Ahrens 	fletcher_4_incremental_native(&dsp->dsa_drr->
12798110f08SMatthew Ahrens 	    drr_u.drr_checksum.drr_checksum,
12898110f08SMatthew Ahrens 	    sizeof (zio_cksum_t), &dsp->dsa_zc);
12998110f08SMatthew Ahrens 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
13098110f08SMatthew Ahrens 		return (SET_ERROR(EINTR));
13198110f08SMatthew Ahrens 	if (payload_len != 0) {
13298110f08SMatthew Ahrens 		fletcher_4_incremental_native(payload, payload_len,
13398110f08SMatthew Ahrens 		    &dsp->dsa_zc);
13498110f08SMatthew Ahrens 		if (dump_bytes(dsp, payload, payload_len) != 0)
13598110f08SMatthew Ahrens 			return (SET_ERROR(EINTR));
13698110f08SMatthew Ahrens 	}
13798110f08SMatthew Ahrens 	return (0);
13898110f08SMatthew Ahrens }
13998110f08SMatthew Ahrens 
140*68ecb2ecSPaul Dagnelie /*
141*68ecb2ecSPaul Dagnelie  * Fill in the drr_free struct, or perform aggregation if the previous record is
142*68ecb2ecSPaul Dagnelie  * also a free record, and the two are adjacent.
143*68ecb2ecSPaul Dagnelie  *
144*68ecb2ecSPaul Dagnelie  * Note that we send free records even for a full send, because we want to be
145*68ecb2ecSPaul Dagnelie  * able to receive a full send as a clone, which requires a list of all the free
146*68ecb2ecSPaul Dagnelie  * and freeobject records that were generated on the source.
147*68ecb2ecSPaul Dagnelie  */
148efb80947Sahrens static int
1494e3c9f44SBill Pijewski dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
150efb80947Sahrens     uint64_t length)
151efb80947Sahrens {
1524e3c9f44SBill Pijewski 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
1539e69d7d0SLori Alt 
1542f3d8780SMatthew Ahrens 	/*
1552f3d8780SMatthew Ahrens 	 * When we receive a free record, dbuf_free_range() assumes
1562f3d8780SMatthew Ahrens 	 * that the receiving system doesn't have any dbufs in the range
1572f3d8780SMatthew Ahrens 	 * being freed.  This is always true because there is a one-record
1582f3d8780SMatthew Ahrens 	 * constraint: we only send one WRITE record for any given
1599c3fd121SMatthew Ahrens 	 * object,offset.  We know that the one-record constraint is
1602f3d8780SMatthew Ahrens 	 * true because we always send data in increasing order by
1612f3d8780SMatthew Ahrens 	 * object,offset.
1622f3d8780SMatthew Ahrens 	 *
1632f3d8780SMatthew Ahrens 	 * If the increasing-order constraint ever changes, we should find
1642f3d8780SMatthew Ahrens 	 * another way to assert that the one-record constraint is still
1652f3d8780SMatthew Ahrens 	 * satisfied.
1662f3d8780SMatthew Ahrens 	 */
1672f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
1682f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
1692f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
1702f3d8780SMatthew Ahrens 
171534029e5SSimon Klinkert 	if (length != -1ULL && offset + length < offset)
172534029e5SSimon Klinkert 		length = -1ULL;
173534029e5SSimon Klinkert 
1749e69d7d0SLori Alt 	/*
1759e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
1769e69d7d0SLori Alt 	 * since free block aggregation can only be done for blocks of the
1779e69d7d0SLori Alt 	 * same type (i.e., DRR_FREE records can only be aggregated with
1789e69d7d0SLori Alt 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
1799e69d7d0SLori Alt 	 * aggregated with other DRR_FREEOBJECTS records.
1809e69d7d0SLori Alt 	 */
1814e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
1824e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREE) {
18398110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
184be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1854e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
1869e69d7d0SLori Alt 	}
1879e69d7d0SLori Alt 
1884e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREE) {
1899e69d7d0SLori Alt 		/*
1909e69d7d0SLori Alt 		 * There should never be a PENDING_FREE if length is -1
1919e69d7d0SLori Alt 		 * (because dump_dnode is the only place where this
1929e69d7d0SLori Alt 		 * function is called with a -1, and only after flushing
1939e69d7d0SLori Alt 		 * any pending record).
1949e69d7d0SLori Alt 		 */
1959e69d7d0SLori Alt 		ASSERT(length != -1ULL);
1969e69d7d0SLori Alt 		/*
1979e69d7d0SLori Alt 		 * Check to see whether this free block can be aggregated
1989e69d7d0SLori Alt 		 * with pending one.
1999e69d7d0SLori Alt 		 */
2009e69d7d0SLori Alt 		if (drrf->drr_object == object && drrf->drr_offset +
2019e69d7d0SLori Alt 		    drrf->drr_length == offset) {
2029e69d7d0SLori Alt 			drrf->drr_length += length;
2039e69d7d0SLori Alt 			return (0);
2049e69d7d0SLori Alt 		} else {
2059e69d7d0SLori Alt 			/* not a continuation.  Push out pending record */
20698110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
207be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
2084e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
2099e69d7d0SLori Alt 		}
2109e69d7d0SLori Alt 	}
2119e69d7d0SLori Alt 	/* create a FREE record and make it pending */
2124e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2134e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREE;
2149e69d7d0SLori Alt 	drrf->drr_object = object;
2159e69d7d0SLori Alt 	drrf->drr_offset = offset;
2169e69d7d0SLori Alt 	drrf->drr_length = length;
2174e3c9f44SBill Pijewski 	drrf->drr_toguid = dsp->dsa_toguid;
2189e69d7d0SLori Alt 	if (length == -1ULL) {
21998110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
220be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2219e69d7d0SLori Alt 	} else {
2224e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_FREE;
2239e69d7d0SLori Alt 	}
224efb80947Sahrens 
225efb80947Sahrens 	return (0);
226efb80947Sahrens }
227efb80947Sahrens 
228efb80947Sahrens static int
2295d7b4d43SMatthew Ahrens dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
2308e714474SLori Alt     uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
231efb80947Sahrens {
2324e3c9f44SBill Pijewski 	struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
2339e69d7d0SLori Alt 
2342f3d8780SMatthew Ahrens 	/*
2352f3d8780SMatthew Ahrens 	 * We send data in increasing object, offset order.
2362f3d8780SMatthew Ahrens 	 * See comment in dump_free() for details.
2372f3d8780SMatthew Ahrens 	 */
2382f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
2392f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
2402f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
2412f3d8780SMatthew Ahrens 	dsp->dsa_last_data_object = object;
2422f3d8780SMatthew Ahrens 	dsp->dsa_last_data_offset = offset + blksz - 1;
2438e714474SLori Alt 
2449e69d7d0SLori Alt 	/*
2459e69d7d0SLori Alt 	 * If there is any kind of pending aggregation (currently either
2469e69d7d0SLori Alt 	 * a grouping of free objects or free blocks), push it out to
2479e69d7d0SLori Alt 	 * the stream, since aggregation can't be done across operations
2489e69d7d0SLori Alt 	 * of different types.
2499e69d7d0SLori Alt 	 */
2504e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
25198110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
252be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2534e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2549e69d7d0SLori Alt 	}
25598110f08SMatthew Ahrens 	/* write a WRITE record */
2564e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2574e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_WRITE;
2589e69d7d0SLori Alt 	drrw->drr_object = object;
2599e69d7d0SLori Alt 	drrw->drr_type = type;
2609e69d7d0SLori Alt 	drrw->drr_offset = offset;
2619e69d7d0SLori Alt 	drrw->drr_length = blksz;
2624e3c9f44SBill Pijewski 	drrw->drr_toguid = dsp->dsa_toguid;
263b5152584SMatthew Ahrens 	if (bp == NULL || BP_IS_EMBEDDED(bp)) {
2645d7b4d43SMatthew Ahrens 		/*
265b5152584SMatthew Ahrens 		 * There's no pre-computed checksum for partial-block
266b5152584SMatthew Ahrens 		 * writes or embedded BP's, so (like
267b5152584SMatthew Ahrens 		 * fletcher4-checkummed blocks) userland will have to
268b5152584SMatthew Ahrens 		 * compute a dedup-capable checksum itself.
2695d7b4d43SMatthew Ahrens 		 */
2705d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
2715d7b4d43SMatthew Ahrens 	} else {
2725d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
27345818ee1SMatthew Ahrens 		if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
27445818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_DEDUP)
2755d7b4d43SMatthew Ahrens 			drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
2765d7b4d43SMatthew Ahrens 		DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
2775d7b4d43SMatthew Ahrens 		DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
2785d7b4d43SMatthew Ahrens 		DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
2795d7b4d43SMatthew Ahrens 		drrw->drr_key.ddk_cksum = bp->blk_cksum;
2805d7b4d43SMatthew Ahrens 	}
281efb80947Sahrens 
28298110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
283be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
284efb80947Sahrens 	return (0);
285efb80947Sahrens }
286efb80947Sahrens 
2875d7b4d43SMatthew Ahrens static int
2885d7b4d43SMatthew Ahrens dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
2895d7b4d43SMatthew Ahrens     int blksz, const blkptr_t *bp)
2905d7b4d43SMatthew Ahrens {
2915d7b4d43SMatthew Ahrens 	char buf[BPE_PAYLOAD_SIZE];
2925d7b4d43SMatthew Ahrens 	struct drr_write_embedded *drrw =
2935d7b4d43SMatthew Ahrens 	    &(dsp->dsa_drr->drr_u.drr_write_embedded);
2945d7b4d43SMatthew Ahrens 
2955d7b4d43SMatthew Ahrens 	if (dsp->dsa_pending_op != PENDING_NONE) {
29698110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
2975d7b4d43SMatthew Ahrens 			return (EINTR);
2985d7b4d43SMatthew Ahrens 		dsp->dsa_pending_op = PENDING_NONE;
2995d7b4d43SMatthew Ahrens 	}
3005d7b4d43SMatthew Ahrens 
3015d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp));
3025d7b4d43SMatthew Ahrens 
3035d7b4d43SMatthew Ahrens 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3045d7b4d43SMatthew Ahrens 	dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
3055d7b4d43SMatthew Ahrens 	drrw->drr_object = object;
3065d7b4d43SMatthew Ahrens 	drrw->drr_offset = offset;
3075d7b4d43SMatthew Ahrens 	drrw->drr_length = blksz;
3085d7b4d43SMatthew Ahrens 	drrw->drr_toguid = dsp->dsa_toguid;
3095d7b4d43SMatthew Ahrens 	drrw->drr_compression = BP_GET_COMPRESS(bp);
3105d7b4d43SMatthew Ahrens 	drrw->drr_etype = BPE_GET_ETYPE(bp);
3115d7b4d43SMatthew Ahrens 	drrw->drr_lsize = BPE_GET_LSIZE(bp);
3125d7b4d43SMatthew Ahrens 	drrw->drr_psize = BPE_GET_PSIZE(bp);
3135d7b4d43SMatthew Ahrens 
3145d7b4d43SMatthew Ahrens 	decode_embedded_bp_compressed(bp, buf);
3155d7b4d43SMatthew Ahrens 
31698110f08SMatthew Ahrens 	if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
3175d7b4d43SMatthew Ahrens 		return (EINTR);
3185d7b4d43SMatthew Ahrens 	return (0);
3195d7b4d43SMatthew Ahrens }
3205d7b4d43SMatthew Ahrens 
3210a586ceaSMark Shellenbaum static int
3224e3c9f44SBill Pijewski dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
3230a586ceaSMark Shellenbaum {
3244e3c9f44SBill Pijewski 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
3250a586ceaSMark Shellenbaum 
3264e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
32798110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
328be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3294e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3300a586ceaSMark Shellenbaum 	}
3310a586ceaSMark Shellenbaum 
3320a586ceaSMark Shellenbaum 	/* write a SPILL record */
3334e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3344e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_SPILL;
3350a586ceaSMark Shellenbaum 	drrs->drr_object = object;
3360a586ceaSMark Shellenbaum 	drrs->drr_length = blksz;
3374e3c9f44SBill Pijewski 	drrs->drr_toguid = dsp->dsa_toguid;
3380a586ceaSMark Shellenbaum 
33998110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
340be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
3410a586ceaSMark Shellenbaum 	return (0);
3420a586ceaSMark Shellenbaum }
3430a586ceaSMark Shellenbaum 
344efb80947Sahrens static int
3454e3c9f44SBill Pijewski dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
346efb80947Sahrens {
3474e3c9f44SBill Pijewski 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
3489e69d7d0SLori Alt 
3499e69d7d0SLori Alt 	/*
3509e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
3519e69d7d0SLori Alt 	 * push it out, since free block aggregation can only be done for
3529e69d7d0SLori Alt 	 * blocks of the same type (i.e., DRR_FREE records can only be
3539e69d7d0SLori Alt 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
3549e69d7d0SLori Alt 	 * can only be aggregated with other DRR_FREEOBJECTS records.
3559e69d7d0SLori Alt 	 */
3564e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
3574e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
35898110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
359be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3604e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3619e69d7d0SLori Alt 	}
3624e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
3639e69d7d0SLori Alt 		/*
3649e69d7d0SLori Alt 		 * See whether this free object array can be aggregated
3659e69d7d0SLori Alt 		 * with pending one
3669e69d7d0SLori Alt 		 */
3679e69d7d0SLori Alt 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
3689e69d7d0SLori Alt 			drrfo->drr_numobjs += numobjs;
3699e69d7d0SLori Alt 			return (0);
3709e69d7d0SLori Alt 		} else {
3719e69d7d0SLori Alt 			/* can't be aggregated.  Push out pending record */
37298110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
373be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
3744e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
3759e69d7d0SLori Alt 		}
3769e69d7d0SLori Alt 	}
3779e69d7d0SLori Alt 
378efb80947Sahrens 	/* write a FREEOBJECTS record */
3794e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3804e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
3819e69d7d0SLori Alt 	drrfo->drr_firstobj = firstobj;
3829e69d7d0SLori Alt 	drrfo->drr_numobjs = numobjs;
3834e3c9f44SBill Pijewski 	drrfo->drr_toguid = dsp->dsa_toguid;
3849e69d7d0SLori Alt 
3854e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
386efb80947Sahrens 
387efb80947Sahrens 	return (0);
388efb80947Sahrens }
389efb80947Sahrens 
390efb80947Sahrens static int
3914e3c9f44SBill Pijewski dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
392efb80947Sahrens {
3934e3c9f44SBill Pijewski 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
3949e69d7d0SLori Alt 
3959c3fd121SMatthew Ahrens 	if (object < dsp->dsa_resume_object) {
3969c3fd121SMatthew Ahrens 		/*
3979c3fd121SMatthew Ahrens 		 * Note: when resuming, we will visit all the dnodes in
3989c3fd121SMatthew Ahrens 		 * the block of dnodes that we are resuming from.  In
3999c3fd121SMatthew Ahrens 		 * this case it's unnecessary to send the dnodes prior to
4009c3fd121SMatthew Ahrens 		 * the one we are resuming from.  We should be at most one
4019c3fd121SMatthew Ahrens 		 * block's worth of dnodes behind the resume point.
4029c3fd121SMatthew Ahrens 		 */
4039c3fd121SMatthew Ahrens 		ASSERT3U(dsp->dsa_resume_object - object, <,
4049c3fd121SMatthew Ahrens 		    1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
4059c3fd121SMatthew Ahrens 		return (0);
4069c3fd121SMatthew Ahrens 	}
4079c3fd121SMatthew Ahrens 
408efb80947Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
4094e3c9f44SBill Pijewski 		return (dump_freeobjects(dsp, object, 1));
410efb80947Sahrens 
4114e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
41298110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
413be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
4144e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
4159e69d7d0SLori Alt 	}
4169e69d7d0SLori Alt 
417efb80947Sahrens 	/* write an OBJECT record */
4184e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4194e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_OBJECT;
4209e69d7d0SLori Alt 	drro->drr_object = object;
4219e69d7d0SLori Alt 	drro->drr_type = dnp->dn_type;
4229e69d7d0SLori Alt 	drro->drr_bonustype = dnp->dn_bonustype;
4239e69d7d0SLori Alt 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
4249e69d7d0SLori Alt 	drro->drr_bonuslen = dnp->dn_bonuslen;
4259e69d7d0SLori Alt 	drro->drr_checksumtype = dnp->dn_checksum;
4269e69d7d0SLori Alt 	drro->drr_compress = dnp->dn_compress;
4274e3c9f44SBill Pijewski 	drro->drr_toguid = dsp->dsa_toguid;
4289e69d7d0SLori Alt 
429b5152584SMatthew Ahrens 	if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
430b5152584SMatthew Ahrens 	    drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
431b5152584SMatthew Ahrens 		drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
432b5152584SMatthew Ahrens 
43398110f08SMatthew Ahrens 	if (dump_record(dsp, DN_BONUS(dnp),
43498110f08SMatthew Ahrens 	    P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
435be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
43698110f08SMatthew Ahrens 	}
437efb80947Sahrens 
4382f3d8780SMatthew Ahrens 	/* Free anything past the end of the file. */
4394e3c9f44SBill Pijewski 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
4402f3d8780SMatthew Ahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
441be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
4423b2aab18SMatthew Ahrens 	if (dsp->dsa_err != 0)
443be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
444efb80947Sahrens 	return (0);
445efb80947Sahrens }
446efb80947Sahrens 
4475d7b4d43SMatthew Ahrens static boolean_t
4485d7b4d43SMatthew Ahrens backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
4495d7b4d43SMatthew Ahrens {
4505d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp))
4515d7b4d43SMatthew Ahrens 		return (B_FALSE);
4525d7b4d43SMatthew Ahrens 
4535d7b4d43SMatthew Ahrens 	/*
4545d7b4d43SMatthew Ahrens 	 * Compression function must be legacy, or explicitly enabled.
4555d7b4d43SMatthew Ahrens 	 */
4565d7b4d43SMatthew Ahrens 	if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
4575d7b4d43SMatthew Ahrens 	    !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
4585d7b4d43SMatthew Ahrens 		return (B_FALSE);
4595d7b4d43SMatthew Ahrens 
4605d7b4d43SMatthew Ahrens 	/*
4615d7b4d43SMatthew Ahrens 	 * Embed type must be explicitly enabled.
4625d7b4d43SMatthew Ahrens 	 */
4635d7b4d43SMatthew Ahrens 	switch (BPE_GET_ETYPE(bp)) {
4645d7b4d43SMatthew Ahrens 	case BP_EMBEDDED_TYPE_DATA:
4655d7b4d43SMatthew Ahrens 		if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
4665d7b4d43SMatthew Ahrens 			return (B_TRUE);
4675d7b4d43SMatthew Ahrens 		break;
4685d7b4d43SMatthew Ahrens 	default:
4695d7b4d43SMatthew Ahrens 		return (B_FALSE);
4705d7b4d43SMatthew Ahrens 	}
4715d7b4d43SMatthew Ahrens 	return (B_FALSE);
4725d7b4d43SMatthew Ahrens }
4735d7b4d43SMatthew Ahrens 
474a2cdcdd2SPaul Dagnelie /*
475a2cdcdd2SPaul Dagnelie  * This is the callback function to traverse_dataset that acts as the worker
476a2cdcdd2SPaul Dagnelie  * thread for dmu_send_impl.
477a2cdcdd2SPaul Dagnelie  */
478a2cdcdd2SPaul Dagnelie /*ARGSUSED*/
479a2cdcdd2SPaul Dagnelie static int
480a2cdcdd2SPaul Dagnelie send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
481a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
482a2cdcdd2SPaul Dagnelie {
483a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *sta = arg;
484a2cdcdd2SPaul Dagnelie 	struct send_block_record *record;
485a2cdcdd2SPaul Dagnelie 	uint64_t record_size;
486a2cdcdd2SPaul Dagnelie 	int err = 0;
487efb80947Sahrens 
4889c3fd121SMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
4899c3fd121SMatthew Ahrens 	    zb->zb_object >= sta->resume.zb_object);
4909c3fd121SMatthew Ahrens 
491a2cdcdd2SPaul Dagnelie 	if (sta->cancel)
492a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINTR));
493a2cdcdd2SPaul Dagnelie 
494a2cdcdd2SPaul Dagnelie 	if (bp == NULL) {
495a2cdcdd2SPaul Dagnelie 		ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
496a2cdcdd2SPaul Dagnelie 		return (0);
497a2cdcdd2SPaul Dagnelie 	} else if (zb->zb_level < 0) {
498a2cdcdd2SPaul Dagnelie 		return (0);
499a2cdcdd2SPaul Dagnelie 	}
500a2cdcdd2SPaul Dagnelie 
501a2cdcdd2SPaul Dagnelie 	record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
502a2cdcdd2SPaul Dagnelie 	record->eos_marker = B_FALSE;
503a2cdcdd2SPaul Dagnelie 	record->bp = *bp;
504a2cdcdd2SPaul Dagnelie 	record->zb = *zb;
505a2cdcdd2SPaul Dagnelie 	record->indblkshift = dnp->dn_indblkshift;
506a2cdcdd2SPaul Dagnelie 	record->datablkszsec = dnp->dn_datablkszsec;
507a2cdcdd2SPaul Dagnelie 	record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
508a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&sta->q, record, record_size);
509a2cdcdd2SPaul Dagnelie 
510a2cdcdd2SPaul Dagnelie 	return (err);
511a2cdcdd2SPaul Dagnelie }
512a2cdcdd2SPaul Dagnelie 
513a2cdcdd2SPaul Dagnelie /*
514a2cdcdd2SPaul Dagnelie  * This function kicks off the traverse_dataset.  It also handles setting the
515a2cdcdd2SPaul Dagnelie  * error code of the thread in case something goes wrong, and pushes the End of
516a2cdcdd2SPaul Dagnelie  * Stream record when the traverse_dataset call has finished.  If there is no
517a2cdcdd2SPaul Dagnelie  * dataset to traverse, the thread immediately pushes End of Stream marker.
518a2cdcdd2SPaul Dagnelie  */
519a2cdcdd2SPaul Dagnelie static void
520a2cdcdd2SPaul Dagnelie send_traverse_thread(void *arg)
521a2cdcdd2SPaul Dagnelie {
522a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *st_arg = arg;
523a2cdcdd2SPaul Dagnelie 	int err;
524a2cdcdd2SPaul Dagnelie 	struct send_block_record *data;
525a2cdcdd2SPaul Dagnelie 
526a2cdcdd2SPaul Dagnelie 	if (st_arg->ds != NULL) {
5279c3fd121SMatthew Ahrens 		err = traverse_dataset_resume(st_arg->ds,
5289c3fd121SMatthew Ahrens 		    st_arg->fromtxg, &st_arg->resume,
5299c3fd121SMatthew Ahrens 		    st_arg->flags, send_cb, st_arg);
5309c3fd121SMatthew Ahrens 
531a2cdcdd2SPaul Dagnelie 		if (err != EINTR)
532a2cdcdd2SPaul Dagnelie 			st_arg->error_code = err;
533a2cdcdd2SPaul Dagnelie 	}
534a2cdcdd2SPaul Dagnelie 	data = kmem_zalloc(sizeof (*data), KM_SLEEP);
535a2cdcdd2SPaul Dagnelie 	data->eos_marker = B_TRUE;
536a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&st_arg->q, data, 1);
537a2cdcdd2SPaul Dagnelie }
538a2cdcdd2SPaul Dagnelie 
539a2cdcdd2SPaul Dagnelie /*
540a2cdcdd2SPaul Dagnelie  * This function actually handles figuring out what kind of record needs to be
541a2cdcdd2SPaul Dagnelie  * dumped, reading the data (which has hopefully been prefetched), and calling
542a2cdcdd2SPaul Dagnelie  * the appropriate helper function.
543a2cdcdd2SPaul Dagnelie  */
544efb80947Sahrens static int
545a2cdcdd2SPaul Dagnelie do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
546efb80947Sahrens {
547a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
548a2cdcdd2SPaul Dagnelie 	const blkptr_t *bp = &data->bp;
549a2cdcdd2SPaul Dagnelie 	const zbookmark_phys_t *zb = &data->zb;
550a2cdcdd2SPaul Dagnelie 	uint8_t indblkshift = data->indblkshift;
551a2cdcdd2SPaul Dagnelie 	uint16_t dblkszsec = data->datablkszsec;
552a2cdcdd2SPaul Dagnelie 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
553efb80947Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
554efb80947Sahrens 	int err = 0;
555efb80947Sahrens 
556a2cdcdd2SPaul Dagnelie 	ASSERT3U(zb->zb_level, >=, 0);
557efb80947Sahrens 
5589c3fd121SMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
5599c3fd121SMatthew Ahrens 	    zb->zb_object >= dsa->dsa_resume_object);
5609c3fd121SMatthew Ahrens 
561b24ab676SJeff Bonwick 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
562b24ab676SJeff Bonwick 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
56314843421SMatthew Ahrens 		return (0);
56443466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp) &&
56543466aaeSMax Grossman 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
566a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
56788b7b0f2SMatthew Ahrens 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
568a2cdcdd2SPaul Dagnelie 		err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
56943466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp)) {
570a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
571a2cdcdd2SPaul Dagnelie 		uint64_t offset = zb->zb_blkid * span;
572a2cdcdd2SPaul Dagnelie 		err = dump_free(dsa, zb->zb_object, offset, span);
57388b7b0f2SMatthew Ahrens 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
57488b7b0f2SMatthew Ahrens 		return (0);
57588b7b0f2SMatthew Ahrens 	} else if (type == DMU_OT_DNODE) {
576efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
5777adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
57888b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
579efb80947Sahrens 
580a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
581a2cdcdd2SPaul Dagnelie 
5821b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
5831b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
5841b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
585be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
58688b7b0f2SMatthew Ahrens 
587a2cdcdd2SPaul Dagnelie 		dnode_phys_t *blk = abuf->b_data;
588a2cdcdd2SPaul Dagnelie 		uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
589a2cdcdd2SPaul Dagnelie 		for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
590a2cdcdd2SPaul Dagnelie 			err = dump_dnode(dsa, dnobj + i, blk + i);
5913b2aab18SMatthew Ahrens 			if (err != 0)
592efb80947Sahrens 				break;
593efb80947Sahrens 		}
59488b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
5950a586ceaSMark Shellenbaum 	} else if (type == DMU_OT_SA) {
5967adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
5970a586ceaSMark Shellenbaum 		arc_buf_t *abuf;
5980a586ceaSMark Shellenbaum 		int blksz = BP_GET_LSIZE(bp);
5990a586ceaSMark Shellenbaum 
6001b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
6011b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
6021b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
603be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
6040a586ceaSMark Shellenbaum 
605a2cdcdd2SPaul Dagnelie 		err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
6060a586ceaSMark Shellenbaum 		(void) arc_buf_remove_ref(abuf, &abuf);
607a2cdcdd2SPaul Dagnelie 	} else if (backup_do_embed(dsa, bp)) {
6085d7b4d43SMatthew Ahrens 		/* it's an embedded level-0 block of a regular object */
609a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
610a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
611a2cdcdd2SPaul Dagnelie 		err = dump_write_embedded(dsa, zb->zb_object,
6125d7b4d43SMatthew Ahrens 		    zb->zb_blkid * blksz, blksz, bp);
613a2cdcdd2SPaul Dagnelie 	} else {
614a2cdcdd2SPaul Dagnelie 		/* it's a level-0 block of a regular object */
6157adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
61688b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
617a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
618b5152584SMatthew Ahrens 		uint64_t offset;
61988b7b0f2SMatthew Ahrens 
62078f17100SMatthew Ahrens 		ASSERT0(zb->zb_level);
6219c3fd121SMatthew Ahrens 		ASSERT(zb->zb_object > dsa->dsa_resume_object ||
6229c3fd121SMatthew Ahrens 		    (zb->zb_object == dsa->dsa_resume_object &&
6239c3fd121SMatthew Ahrens 		    zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
6249c3fd121SMatthew Ahrens 
6251b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
6261b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
6271b912ec7SGeorge Wilson 		    &aflags, zb) != 0) {
62819b94df9SMatthew Ahrens 			if (zfs_send_corrupt_data) {
62919b94df9SMatthew Ahrens 				/* Send a block filled with 0x"zfs badd bloc" */
63019b94df9SMatthew Ahrens 				abuf = arc_buf_alloc(spa, blksz, &abuf,
63119b94df9SMatthew Ahrens 				    ARC_BUFC_DATA);
63219b94df9SMatthew Ahrens 				uint64_t *ptr;
63319b94df9SMatthew Ahrens 				for (ptr = abuf->b_data;
63419b94df9SMatthew Ahrens 				    (char *)ptr < (char *)abuf->b_data + blksz;
63519b94df9SMatthew Ahrens 				    ptr++)
6368c76e076SBrian Behlendorf 					*ptr = 0x2f5baddb10cULL;
63719b94df9SMatthew Ahrens 			} else {
638be6fd75aSMatthew Ahrens 				return (SET_ERROR(EIO));
63919b94df9SMatthew Ahrens 			}
64019b94df9SMatthew Ahrens 		}
64188b7b0f2SMatthew Ahrens 
642b5152584SMatthew Ahrens 		offset = zb->zb_blkid * blksz;
643b5152584SMatthew Ahrens 
644a2cdcdd2SPaul Dagnelie 		if (!(dsa->dsa_featureflags &
645b5152584SMatthew Ahrens 		    DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
646b5152584SMatthew Ahrens 		    blksz > SPA_OLD_MAXBLOCKSIZE) {
647b5152584SMatthew Ahrens 			char *buf = abuf->b_data;
648b5152584SMatthew Ahrens 			while (blksz > 0 && err == 0) {
649b5152584SMatthew Ahrens 				int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
650a2cdcdd2SPaul Dagnelie 				err = dump_write(dsa, type, zb->zb_object,
651b5152584SMatthew Ahrens 				    offset, n, NULL, buf);
652b5152584SMatthew Ahrens 				offset += n;
653b5152584SMatthew Ahrens 				buf += n;
654b5152584SMatthew Ahrens 				blksz -= n;
655b5152584SMatthew Ahrens 			}
656b5152584SMatthew Ahrens 		} else {
657a2cdcdd2SPaul Dagnelie 			err = dump_write(dsa, type, zb->zb_object,
658b5152584SMatthew Ahrens 			    offset, blksz, bp, abuf->b_data);
659b5152584SMatthew Ahrens 		}
66088b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
661efb80947Sahrens 	}
662efb80947Sahrens 
663efb80947Sahrens 	ASSERT(err == 0 || err == EINTR);
664efb80947Sahrens 	return (err);
665efb80947Sahrens }
666efb80947Sahrens 
6674445fffbSMatthew Ahrens /*
668a2cdcdd2SPaul Dagnelie  * Pop the new data off the queue, and free the old data.
669a2cdcdd2SPaul Dagnelie  */
670a2cdcdd2SPaul Dagnelie static struct send_block_record *
671a2cdcdd2SPaul Dagnelie get_next_record(bqueue_t *bq, struct send_block_record *data)
672a2cdcdd2SPaul Dagnelie {
673a2cdcdd2SPaul Dagnelie 	struct send_block_record *tmp = bqueue_dequeue(bq);
674a2cdcdd2SPaul Dagnelie 	kmem_free(data, sizeof (*data));
675a2cdcdd2SPaul Dagnelie 	return (tmp);
676a2cdcdd2SPaul Dagnelie }
677a2cdcdd2SPaul Dagnelie 
678a2cdcdd2SPaul Dagnelie /*
679a2cdcdd2SPaul Dagnelie  * Actually do the bulk of the work in a zfs send.
680a2cdcdd2SPaul Dagnelie  *
681a2cdcdd2SPaul Dagnelie  * Note: Releases dp using the specified tag.
6824445fffbSMatthew Ahrens  */
6833b2aab18SMatthew Ahrens static int
684a2cdcdd2SPaul Dagnelie dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
6859c3fd121SMatthew Ahrens     zfs_bookmark_phys_t *ancestor_zb,
6869c3fd121SMatthew Ahrens     boolean_t is_clone, boolean_t embedok, boolean_t large_block_ok, int outfd,
6879c3fd121SMatthew Ahrens     uint64_t resumeobj, uint64_t resumeoff,
6889c3fd121SMatthew Ahrens     vnode_t *vp, offset_t *off)
689efb80947Sahrens {
6903b2aab18SMatthew Ahrens 	objset_t *os;
691efb80947Sahrens 	dmu_replay_record_t *drr;
6924e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp;
693efb80947Sahrens 	int err;
6943cb34c60Sahrens 	uint64_t fromtxg = 0;
6955d7b4d43SMatthew Ahrens 	uint64_t featureflags = 0;
6969c3fd121SMatthew Ahrens 	struct send_thread_arg to_arg = { 0 };
697efb80947Sahrens 
698a2cdcdd2SPaul Dagnelie 	err = dmu_objset_from_ds(to_ds, &os);
6993b2aab18SMatthew Ahrens 	if (err != 0) {
7003b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
7013b2aab18SMatthew Ahrens 		return (err);
7023b2aab18SMatthew Ahrens 	}
703efb80947Sahrens 
704efb80947Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
705efb80947Sahrens 	drr->drr_type = DRR_BEGIN;
706efb80947Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
7079e69d7d0SLori Alt 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
7089e69d7d0SLori Alt 	    DMU_SUBSTREAM);
709dc7cd546SMark Shellenbaum 
710dc7cd546SMark Shellenbaum #ifdef _KERNEL
7113b2aab18SMatthew Ahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
712dc7cd546SMark Shellenbaum 		uint64_t version;
7133b2aab18SMatthew Ahrens 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
7144e3c9f44SBill Pijewski 			kmem_free(drr, sizeof (dmu_replay_record_t));
7153b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, tag);
716be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
7174e3c9f44SBill Pijewski 		}
7183b2aab18SMatthew Ahrens 		if (version >= ZPL_VERSION_SA) {
7195d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
720dc7cd546SMark Shellenbaum 		}
721dc7cd546SMark Shellenbaum 	}
722dc7cd546SMark Shellenbaum #endif
723dc7cd546SMark Shellenbaum 
724ca0cc391SMatthew Ahrens 	if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
725b5152584SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
7265d7b4d43SMatthew Ahrens 	if (embedok &&
7275d7b4d43SMatthew Ahrens 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
7285d7b4d43SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
7295d7b4d43SMatthew Ahrens 		if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
7305d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
7315d7b4d43SMatthew Ahrens 	}
7325d7b4d43SMatthew Ahrens 
7339c3fd121SMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
7349c3fd121SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_RESUMING;
7359c3fd121SMatthew Ahrens 	}
7369c3fd121SMatthew Ahrens 
7375d7b4d43SMatthew Ahrens 	DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
7385d7b4d43SMatthew Ahrens 	    featureflags);
7395d7b4d43SMatthew Ahrens 
740efb80947Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
741a2cdcdd2SPaul Dagnelie 	    dsl_dataset_phys(to_ds)->ds_creation_time;
7423b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
74378f17100SMatthew Ahrens 	if (is_clone)
7443cb34c60Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
745a2cdcdd2SPaul Dagnelie 	drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
746a2cdcdd2SPaul Dagnelie 	if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
747ab04eb8eStimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
748*68ecb2ecSPaul Dagnelie 	drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
749ab04eb8eStimh 
750a2cdcdd2SPaul Dagnelie 	if (ancestor_zb != NULL) {
751a2cdcdd2SPaul Dagnelie 		drr->drr_u.drr_begin.drr_fromguid =
752a2cdcdd2SPaul Dagnelie 		    ancestor_zb->zbm_guid;
753a2cdcdd2SPaul Dagnelie 		fromtxg = ancestor_zb->zbm_creation_txg;
75478f17100SMatthew Ahrens 	}
755a2cdcdd2SPaul Dagnelie 	dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
756a2cdcdd2SPaul Dagnelie 	if (!to_ds->ds_is_snapshot) {
75778f17100SMatthew Ahrens 		(void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
75878f17100SMatthew Ahrens 		    sizeof (drr->drr_u.drr_begin.drr_toname));
7593b2aab18SMatthew Ahrens 	}
7603cb34c60Sahrens 
7614e3c9f44SBill Pijewski 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
7624e3c9f44SBill Pijewski 
7634e3c9f44SBill Pijewski 	dsp->dsa_drr = drr;
7644e3c9f44SBill Pijewski 	dsp->dsa_vp = vp;
7654e3c9f44SBill Pijewski 	dsp->dsa_outfd = outfd;
7664e3c9f44SBill Pijewski 	dsp->dsa_proc = curproc;
7673b2aab18SMatthew Ahrens 	dsp->dsa_os = os;
7684e3c9f44SBill Pijewski 	dsp->dsa_off = off;
769a2cdcdd2SPaul Dagnelie 	dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
7704e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_NONE;
7715d7b4d43SMatthew Ahrens 	dsp->dsa_featureflags = featureflags;
7729c3fd121SMatthew Ahrens 	dsp->dsa_resume_object = resumeobj;
7739c3fd121SMatthew Ahrens 	dsp->dsa_resume_offset = resumeoff;
7744e3c9f44SBill Pijewski 
775a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
776a2cdcdd2SPaul Dagnelie 	list_insert_head(&to_ds->ds_sendstreams, dsp);
777a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
7784e3c9f44SBill Pijewski 
779a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_hold(to_ds, FTAG);
780de8d9cffSMatthew Ahrens 	dsl_pool_rele(dp, tag);
781de8d9cffSMatthew Ahrens 
7829c3fd121SMatthew Ahrens 	void *payload = NULL;
7839c3fd121SMatthew Ahrens 	size_t payload_len = 0;
7849c3fd121SMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
7859c3fd121SMatthew Ahrens 		dmu_object_info_t to_doi;
7869c3fd121SMatthew Ahrens 		err = dmu_object_info(os, resumeobj, &to_doi);
7879c3fd121SMatthew Ahrens 		if (err != 0)
7889c3fd121SMatthew Ahrens 			goto out;
7899c3fd121SMatthew Ahrens 		SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
7909c3fd121SMatthew Ahrens 		    resumeoff / to_doi.doi_data_block_size);
7919c3fd121SMatthew Ahrens 
7929c3fd121SMatthew Ahrens 		nvlist_t *nvl = fnvlist_alloc();
7939c3fd121SMatthew Ahrens 		fnvlist_add_uint64(nvl, "resume_object", resumeobj);
7949c3fd121SMatthew Ahrens 		fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
7959c3fd121SMatthew Ahrens 		payload = fnvlist_pack(nvl, &payload_len);
7969c3fd121SMatthew Ahrens 		drr->drr_payloadlen = payload_len;
7979c3fd121SMatthew Ahrens 		fnvlist_free(nvl);
7989c3fd121SMatthew Ahrens 	}
7999c3fd121SMatthew Ahrens 
8009c3fd121SMatthew Ahrens 	err = dump_record(dsp, payload, payload_len);
8019c3fd121SMatthew Ahrens 	fnvlist_pack_free(payload, payload_len);
8029c3fd121SMatthew Ahrens 	if (err != 0) {
8034e3c9f44SBill Pijewski 		err = dsp->dsa_err;
8044e3c9f44SBill Pijewski 		goto out;
805efb80947Sahrens 	}
806efb80947Sahrens 
807a2cdcdd2SPaul Dagnelie 	err = bqueue_init(&to_arg.q, zfs_send_queue_length,
808a2cdcdd2SPaul Dagnelie 	    offsetof(struct send_block_record, ln));
809a2cdcdd2SPaul Dagnelie 	to_arg.error_code = 0;
810a2cdcdd2SPaul Dagnelie 	to_arg.cancel = B_FALSE;
811a2cdcdd2SPaul Dagnelie 	to_arg.ds = to_ds;
812a2cdcdd2SPaul Dagnelie 	to_arg.fromtxg = fromtxg;
813a2cdcdd2SPaul Dagnelie 	to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
814a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
815a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
816a2cdcdd2SPaul Dagnelie 
817a2cdcdd2SPaul Dagnelie 	struct send_block_record *to_data;
818a2cdcdd2SPaul Dagnelie 	to_data = bqueue_dequeue(&to_arg.q);
819a2cdcdd2SPaul Dagnelie 
820a2cdcdd2SPaul Dagnelie 	while (!to_data->eos_marker && err == 0) {
821a2cdcdd2SPaul Dagnelie 		err = do_dump(dsp, to_data);
822a2cdcdd2SPaul Dagnelie 		to_data = get_next_record(&to_arg.q, to_data);
823a2cdcdd2SPaul Dagnelie 		if (issig(JUSTLOOKING) && issig(FORREAL))
824a2cdcdd2SPaul Dagnelie 			err = EINTR;
825a2cdcdd2SPaul Dagnelie 	}
826a2cdcdd2SPaul Dagnelie 
827a2cdcdd2SPaul Dagnelie 	if (err != 0) {
828a2cdcdd2SPaul Dagnelie 		to_arg.cancel = B_TRUE;
829a2cdcdd2SPaul Dagnelie 		while (!to_data->eos_marker) {
830a2cdcdd2SPaul Dagnelie 			to_data = get_next_record(&to_arg.q, to_data);
831a2cdcdd2SPaul Dagnelie 		}
832a2cdcdd2SPaul Dagnelie 	}
833a2cdcdd2SPaul Dagnelie 	kmem_free(to_data, sizeof (*to_data));
834a2cdcdd2SPaul Dagnelie 
835a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&to_arg.q);
836a2cdcdd2SPaul Dagnelie 
837a2cdcdd2SPaul Dagnelie 	if (err == 0 && to_arg.error_code != 0)
838a2cdcdd2SPaul Dagnelie 		err = to_arg.error_code;
839a2cdcdd2SPaul Dagnelie 
840a2cdcdd2SPaul Dagnelie 	if (err != 0)
841a2cdcdd2SPaul Dagnelie 		goto out;
842efb80947Sahrens 
8434e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE)
84498110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
845be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINTR);
8469e69d7d0SLori Alt 
8473b2aab18SMatthew Ahrens 	if (err != 0) {
8483b2aab18SMatthew Ahrens 		if (err == EINTR && dsp->dsa_err != 0)
8494e3c9f44SBill Pijewski 			err = dsp->dsa_err;
8504e3c9f44SBill Pijewski 		goto out;
851efb80947Sahrens 	}
852efb80947Sahrens 
853efb80947Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
854efb80947Sahrens 	drr->drr_type = DRR_END;
8554e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
8564e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
857efb80947Sahrens 
858a2cdcdd2SPaul Dagnelie 	if (dump_record(dsp, NULL, 0) != 0)
8594e3c9f44SBill Pijewski 		err = dsp->dsa_err;
860efb80947Sahrens 
8614e3c9f44SBill Pijewski out:
862a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
863a2cdcdd2SPaul Dagnelie 	list_remove(&to_ds->ds_sendstreams, dsp);
864a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
8654e3c9f44SBill Pijewski 
866efb80947Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
8674e3c9f44SBill Pijewski 	kmem_free(dsp, sizeof (dmu_sendarg_t));
868efb80947Sahrens 
869a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_rele(to_ds, FTAG);
8703b2aab18SMatthew Ahrens 
8714e3c9f44SBill Pijewski 	return (err);
872efb80947Sahrens }
873efb80947Sahrens 
87419b94df9SMatthew Ahrens int
8753b2aab18SMatthew Ahrens dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
876b5152584SMatthew Ahrens     boolean_t embedok, boolean_t large_block_ok,
877b5152584SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
8783b2aab18SMatthew Ahrens {
8793b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
8803b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
8813b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
8823b2aab18SMatthew Ahrens 	int err;
8833b2aab18SMatthew Ahrens 
8843b2aab18SMatthew Ahrens 	err = dsl_pool_hold(pool, FTAG, &dp);
8853b2aab18SMatthew Ahrens 	if (err != 0)
8863b2aab18SMatthew Ahrens 		return (err);
8873b2aab18SMatthew Ahrens 
8883b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
8893b2aab18SMatthew Ahrens 	if (err != 0) {
8903b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
8913b2aab18SMatthew Ahrens 		return (err);
8923b2aab18SMatthew Ahrens 	}
8933b2aab18SMatthew Ahrens 
8943b2aab18SMatthew Ahrens 	if (fromsnap != 0) {
89578f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
89678f17100SMatthew Ahrens 		boolean_t is_clone;
89778f17100SMatthew Ahrens 
8983b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
8993b2aab18SMatthew Ahrens 		if (err != 0) {
9003b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
9013b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
9023b2aab18SMatthew Ahrens 			return (err);
9033b2aab18SMatthew Ahrens 		}
90478f17100SMatthew Ahrens 		if (!dsl_dataset_is_before(ds, fromds, 0))
90578f17100SMatthew Ahrens 			err = SET_ERROR(EXDEV);
906c1379625SJustin T. Gibbs 		zb.zbm_creation_time =
907c1379625SJustin T. Gibbs 		    dsl_dataset_phys(fromds)->ds_creation_time;
908c1379625SJustin T. Gibbs 		zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
909c1379625SJustin T. Gibbs 		zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
91078f17100SMatthew Ahrens 		is_clone = (fromds->ds_dir != ds->ds_dir);
91178f17100SMatthew Ahrens 		dsl_dataset_rele(fromds, FTAG);
912b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
9139c3fd121SMatthew Ahrens 		    embedok, large_block_ok, outfd, 0, 0, vp, off);
91478f17100SMatthew Ahrens 	} else {
915b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
9169c3fd121SMatthew Ahrens 		    embedok, large_block_ok, outfd, 0, 0, vp, off);
9173b2aab18SMatthew Ahrens 	}
91878f17100SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
91978f17100SMatthew Ahrens 	return (err);
9203b2aab18SMatthew Ahrens }
9213b2aab18SMatthew Ahrens 
9223b2aab18SMatthew Ahrens int
9239c3fd121SMatthew Ahrens dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
9249c3fd121SMatthew Ahrens     boolean_t large_block_ok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
9259c3fd121SMatthew Ahrens     vnode_t *vp, offset_t *off)
9263b2aab18SMatthew Ahrens {
9273b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9283b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
9293b2aab18SMatthew Ahrens 	int err;
93078f17100SMatthew Ahrens 	boolean_t owned = B_FALSE;
9313b2aab18SMatthew Ahrens 
93278f17100SMatthew Ahrens 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
933be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9343b2aab18SMatthew Ahrens 
9353b2aab18SMatthew Ahrens 	err = dsl_pool_hold(tosnap, FTAG, &dp);
9363b2aab18SMatthew Ahrens 	if (err != 0)
9373b2aab18SMatthew Ahrens 		return (err);
9383b2aab18SMatthew Ahrens 
93978f17100SMatthew Ahrens 	if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
94078f17100SMatthew Ahrens 		/*
94178f17100SMatthew Ahrens 		 * We are sending a filesystem or volume.  Ensure
94278f17100SMatthew Ahrens 		 * that it doesn't change by owning the dataset.
94378f17100SMatthew Ahrens 		 */
94478f17100SMatthew Ahrens 		err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
94578f17100SMatthew Ahrens 		owned = B_TRUE;
94678f17100SMatthew Ahrens 	} else {
94778f17100SMatthew Ahrens 		err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
94878f17100SMatthew Ahrens 	}
9493b2aab18SMatthew Ahrens 	if (err != 0) {
9503b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
9513b2aab18SMatthew Ahrens 		return (err);
9523b2aab18SMatthew Ahrens 	}
9533b2aab18SMatthew Ahrens 
9543b2aab18SMatthew Ahrens 	if (fromsnap != NULL) {
95578f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
95678f17100SMatthew Ahrens 		boolean_t is_clone = B_FALSE;
95778f17100SMatthew Ahrens 		int fsnamelen = strchr(tosnap, '@') - tosnap;
95878f17100SMatthew Ahrens 
95978f17100SMatthew Ahrens 		/*
96078f17100SMatthew Ahrens 		 * If the fromsnap is in a different filesystem, then
96178f17100SMatthew Ahrens 		 * mark the send stream as a clone.
96278f17100SMatthew Ahrens 		 */
96378f17100SMatthew Ahrens 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
96478f17100SMatthew Ahrens 		    (fromsnap[fsnamelen] != '@' &&
96578f17100SMatthew Ahrens 		    fromsnap[fsnamelen] != '#')) {
96678f17100SMatthew Ahrens 			is_clone = B_TRUE;
96778f17100SMatthew Ahrens 		}
96878f17100SMatthew Ahrens 
96978f17100SMatthew Ahrens 		if (strchr(fromsnap, '@')) {
97078f17100SMatthew Ahrens 			dsl_dataset_t *fromds;
97178f17100SMatthew Ahrens 			err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
97278f17100SMatthew Ahrens 			if (err == 0) {
97378f17100SMatthew Ahrens 				if (!dsl_dataset_is_before(ds, fromds, 0))
97478f17100SMatthew Ahrens 					err = SET_ERROR(EXDEV);
97578f17100SMatthew Ahrens 				zb.zbm_creation_time =
976c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_time;
97778f17100SMatthew Ahrens 				zb.zbm_creation_txg =
978c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_txg;
979c1379625SJustin T. Gibbs 				zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
98078f17100SMatthew Ahrens 				is_clone = (ds->ds_dir != fromds->ds_dir);
98178f17100SMatthew Ahrens 				dsl_dataset_rele(fromds, FTAG);
98278f17100SMatthew Ahrens 			}
98378f17100SMatthew Ahrens 		} else {
98478f17100SMatthew Ahrens 			err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
98578f17100SMatthew Ahrens 		}
9863b2aab18SMatthew Ahrens 		if (err != 0) {
9873b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
9883b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
9893b2aab18SMatthew Ahrens 			return (err);
9903b2aab18SMatthew Ahrens 		}
991b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
9929c3fd121SMatthew Ahrens 		    embedok, large_block_ok,
9939c3fd121SMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
99478f17100SMatthew Ahrens 	} else {
995b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
9969c3fd121SMatthew Ahrens 		    embedok, large_block_ok,
9979c3fd121SMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
9983b2aab18SMatthew Ahrens 	}
99978f17100SMatthew Ahrens 	if (owned)
100078f17100SMatthew Ahrens 		dsl_dataset_disown(ds, FTAG);
100178f17100SMatthew Ahrens 	else
100278f17100SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
100378f17100SMatthew Ahrens 	return (err);
10043b2aab18SMatthew Ahrens }
10053b2aab18SMatthew Ahrens 
1006643da460SMax Grossman static int
1007643da460SMax Grossman dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t size,
1008643da460SMax Grossman     uint64_t *sizep)
1009643da460SMax Grossman {
1010643da460SMax Grossman 	int err;
1011643da460SMax Grossman 	/*
1012643da460SMax Grossman 	 * Assume that space (both on-disk and in-stream) is dominated by
1013643da460SMax Grossman 	 * data.  We will adjust for indirect blocks and the copies property,
1014643da460SMax Grossman 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1015643da460SMax Grossman 	 */
1016643da460SMax Grossman 
1017643da460SMax Grossman 	/*
1018643da460SMax Grossman 	 * Subtract out approximate space used by indirect blocks.
1019643da460SMax Grossman 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
1020643da460SMax Grossman 	 * Assume all blocks are recordsize.  Assume ditto blocks and
1021643da460SMax Grossman 	 * internal fragmentation counter out compression.
1022643da460SMax Grossman 	 *
1023643da460SMax Grossman 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1024643da460SMax Grossman 	 * block, which we observe in practice.
1025643da460SMax Grossman 	 */
1026643da460SMax Grossman 	uint64_t recordsize;
1027643da460SMax Grossman 	err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
1028643da460SMax Grossman 	if (err != 0)
1029643da460SMax Grossman 		return (err);
1030643da460SMax Grossman 	size -= size / recordsize * sizeof (blkptr_t);
1031643da460SMax Grossman 
1032643da460SMax Grossman 	/* Add in the space for the record associated with each block. */
1033643da460SMax Grossman 	size += size / recordsize * sizeof (dmu_replay_record_t);
1034643da460SMax Grossman 
1035643da460SMax Grossman 	*sizep = size;
1036643da460SMax Grossman 
1037643da460SMax Grossman 	return (0);
1038643da460SMax Grossman }
1039643da460SMax Grossman 
10403b2aab18SMatthew Ahrens int
10413b2aab18SMatthew Ahrens dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
104219b94df9SMatthew Ahrens {
104319b94df9SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
104419b94df9SMatthew Ahrens 	int err;
104519b94df9SMatthew Ahrens 	uint64_t size;
104619b94df9SMatthew Ahrens 
10473b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
10483b2aab18SMatthew Ahrens 
104919b94df9SMatthew Ahrens 	/* tosnap must be a snapshot */
1050bc9014e6SJustin Gibbs 	if (!ds->ds_is_snapshot)
1051be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
105219b94df9SMatthew Ahrens 
105324218bebSAndriy Gapon 	/* fromsnap, if provided, must be a snapshot */
105424218bebSAndriy Gapon 	if (fromds != NULL && !fromds->ds_is_snapshot)
105524218bebSAndriy Gapon 		return (SET_ERROR(EINVAL));
105624218bebSAndriy Gapon 
10574445fffbSMatthew Ahrens 	/*
10584445fffbSMatthew Ahrens 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
10594445fffbSMatthew Ahrens 	 * or the origin's fs.
10604445fffbSMatthew Ahrens 	 */
106178f17100SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1062be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
106319b94df9SMatthew Ahrens 
106419b94df9SMatthew Ahrens 	/* Get uncompressed size estimate of changed data. */
106519b94df9SMatthew Ahrens 	if (fromds == NULL) {
1066c1379625SJustin T. Gibbs 		size = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
106719b94df9SMatthew Ahrens 	} else {
106819b94df9SMatthew Ahrens 		uint64_t used, comp;
106919b94df9SMatthew Ahrens 		err = dsl_dataset_space_written(fromds, ds,
107019b94df9SMatthew Ahrens 		    &used, &comp, &size);
10713b2aab18SMatthew Ahrens 		if (err != 0)
107219b94df9SMatthew Ahrens 			return (err);
107319b94df9SMatthew Ahrens 	}
107419b94df9SMatthew Ahrens 
1075643da460SMax Grossman 	err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1076643da460SMax Grossman 	return (err);
1077643da460SMax Grossman }
1078643da460SMax Grossman 
1079643da460SMax Grossman /*
1080643da460SMax Grossman  * Simple callback used to traverse the blocks of a snapshot and sum their
1081643da460SMax Grossman  * uncompressed size
1082643da460SMax Grossman  */
1083643da460SMax Grossman /* ARGSUSED */
1084643da460SMax Grossman static int
1085643da460SMax Grossman dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1086643da460SMax Grossman     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1087643da460SMax Grossman {
1088643da460SMax Grossman 	uint64_t *spaceptr = arg;
1089643da460SMax Grossman 	if (bp != NULL && !BP_IS_HOLE(bp)) {
1090643da460SMax Grossman 		*spaceptr += BP_GET_UCSIZE(bp);
1091643da460SMax Grossman 	}
1092643da460SMax Grossman 	return (0);
1093643da460SMax Grossman }
1094643da460SMax Grossman 
1095643da460SMax Grossman /*
1096643da460SMax Grossman  * Given a desination snapshot and a TXG, calculate the approximate size of a
1097643da460SMax Grossman  * send stream sent from that TXG. from_txg may be zero, indicating that the
1098643da460SMax Grossman  * whole snapshot will be sent.
1099643da460SMax Grossman  */
1100643da460SMax Grossman int
1101643da460SMax Grossman dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
1102643da460SMax Grossman     uint64_t *sizep)
1103643da460SMax Grossman {
1104643da460SMax Grossman 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1105643da460SMax Grossman 	int err;
1106643da460SMax Grossman 	uint64_t size = 0;
1107643da460SMax Grossman 
1108643da460SMax Grossman 	ASSERT(dsl_pool_config_held(dp));
1109643da460SMax Grossman 
1110643da460SMax Grossman 	/* tosnap must be a snapshot */
1111643da460SMax Grossman 	if (!dsl_dataset_is_snapshot(ds))
1112643da460SMax Grossman 		return (SET_ERROR(EINVAL));
1113643da460SMax Grossman 
1114643da460SMax Grossman 	/* verify that from_txg is before the provided snapshot was taken */
1115643da460SMax Grossman 	if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1116643da460SMax Grossman 		return (SET_ERROR(EXDEV));
1117643da460SMax Grossman 	}
111819b94df9SMatthew Ahrens 
111919b94df9SMatthew Ahrens 	/*
1120643da460SMax Grossman 	 * traverse the blocks of the snapshot with birth times after
1121643da460SMax Grossman 	 * from_txg, summing their uncompressed size
112219b94df9SMatthew Ahrens 	 */
1123643da460SMax Grossman 	err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1124643da460SMax Grossman 	    dmu_calculate_send_traversal, &size);
1125643da460SMax Grossman 	if (err)
112619b94df9SMatthew Ahrens 		return (err);
112719b94df9SMatthew Ahrens 
1128643da460SMax Grossman 	err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1129643da460SMax Grossman 	return (err);
113019b94df9SMatthew Ahrens }
113119b94df9SMatthew Ahrens 
11323b2aab18SMatthew Ahrens typedef struct dmu_recv_begin_arg {
11333b2aab18SMatthew Ahrens 	const char *drba_origin;
11343b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drba_cookie;
11353b2aab18SMatthew Ahrens 	cred_t *drba_cred;
113634f2f8cfSMatthew Ahrens 	uint64_t drba_snapobj;
11373b2aab18SMatthew Ahrens } dmu_recv_begin_arg_t;
1138f18faf3fSek 
1139f18faf3fSek static int
11403b2aab18SMatthew Ahrens recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
11413b2aab18SMatthew Ahrens     uint64_t fromguid)
1142f18faf3fSek {
11433cb34c60Sahrens 	uint64_t val;
11443b2aab18SMatthew Ahrens 	int error;
11453b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1146f18faf3fSek 
11473b2aab18SMatthew Ahrens 	/* temporary clone name must not exist */
11483b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
1149c1379625SJustin T. Gibbs 	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
11503b2aab18SMatthew Ahrens 	    8, 1, &val);
11513b2aab18SMatthew Ahrens 	if (error != ENOENT)
11523b2aab18SMatthew Ahrens 		return (error == 0 ? EBUSY : error);
11533b2aab18SMatthew Ahrens 
1154feaa74e4SMark Maybee 	/* new snapshot name must not exist */
11553b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
1156c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1157c1379625SJustin T. Gibbs 	    drba->drba_cookie->drc_tosnap, 8, 1, &val);
11583b2aab18SMatthew Ahrens 	if (error != ENOENT)
11593b2aab18SMatthew Ahrens 		return (error == 0 ? EEXIST : error);
1160feaa74e4SMark Maybee 
1161a2afb611SJerry Jelinek 	/*
1162a2afb611SJerry Jelinek 	 * Check snapshot limit before receiving. We'll recheck again at the
1163a2afb611SJerry Jelinek 	 * end, but might as well abort before receiving if we're already over
1164a2afb611SJerry Jelinek 	 * the limit.
1165a2afb611SJerry Jelinek 	 *
1166a2afb611SJerry Jelinek 	 * Note that we do not check the file system limit with
1167a2afb611SJerry Jelinek 	 * dsl_dir_fscount_check because the temporary %clones don't count
1168a2afb611SJerry Jelinek 	 * against that limit.
1169a2afb611SJerry Jelinek 	 */
1170a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1171a2afb611SJerry Jelinek 	    NULL, drba->drba_cred);
1172a2afb611SJerry Jelinek 	if (error != 0)
1173a2afb611SJerry Jelinek 		return (error);
1174a2afb611SJerry Jelinek 
11753b2aab18SMatthew Ahrens 	if (fromguid != 0) {
117634f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap;
1177c1379625SJustin T. Gibbs 		uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
117834f2f8cfSMatthew Ahrens 
117934f2f8cfSMatthew Ahrens 		/* Find snapshot in this dir that matches fromguid. */
118034f2f8cfSMatthew Ahrens 		while (obj != 0) {
118134f2f8cfSMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, obj, FTAG,
118234f2f8cfSMatthew Ahrens 			    &snap);
118334f2f8cfSMatthew Ahrens 			if (error != 0)
118434f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
118534f2f8cfSMatthew Ahrens 			if (snap->ds_dir != ds->ds_dir) {
118634f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
118734f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
118834f2f8cfSMatthew Ahrens 			}
1189c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(snap)->ds_guid == fromguid)
119034f2f8cfSMatthew Ahrens 				break;
1191c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
119234f2f8cfSMatthew Ahrens 			dsl_dataset_rele(snap, FTAG);
119334f2f8cfSMatthew Ahrens 		}
119434f2f8cfSMatthew Ahrens 		if (obj == 0)
1195be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
119692241e0bSTom Erickson 
119734f2f8cfSMatthew Ahrens 		if (drba->drba_cookie->drc_force) {
119834f2f8cfSMatthew Ahrens 			drba->drba_snapobj = obj;
119934f2f8cfSMatthew Ahrens 		} else {
120034f2f8cfSMatthew Ahrens 			/*
120134f2f8cfSMatthew Ahrens 			 * If we are not forcing, there must be no
120234f2f8cfSMatthew Ahrens 			 * changes since fromsnap.
120334f2f8cfSMatthew Ahrens 			 */
120434f2f8cfSMatthew Ahrens 			if (dsl_dataset_modified_since_snap(ds, snap)) {
120592241e0bSTom Erickson 				dsl_dataset_rele(snap, FTAG);
120634f2f8cfSMatthew Ahrens 				return (SET_ERROR(ETXTBSY));
120792241e0bSTom Erickson 			}
120834f2f8cfSMatthew Ahrens 			drba->drba_snapobj = ds->ds_prev->ds_object;
120992241e0bSTom Erickson 		}
121034f2f8cfSMatthew Ahrens 
121134f2f8cfSMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
1212ae46e4c7SMatthew Ahrens 	} else {
12135bae108fSAndriy Gapon 		/* if full, then must be forced */
12145bae108fSAndriy Gapon 		if (!drba->drba_cookie->drc_force)
12155bae108fSAndriy Gapon 			return (SET_ERROR(EEXIST));
12165bae108fSAndriy Gapon 		/* start from $ORIGIN@$ORIGIN, if supported */
12175bae108fSAndriy Gapon 		drba->drba_snapobj = dp->dp_origin_snap != NULL ?
12185bae108fSAndriy Gapon 		    dp->dp_origin_snap->ds_object : 0;
1219ae46e4c7SMatthew Ahrens 	}
12203cb34c60Sahrens 
12213cb34c60Sahrens 	return (0);
12223b2aab18SMatthew Ahrens 
12233b2aab18SMatthew Ahrens }
12243b2aab18SMatthew Ahrens 
12253b2aab18SMatthew Ahrens static int
12263b2aab18SMatthew Ahrens dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
12273b2aab18SMatthew Ahrens {
12283b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
12293b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
12303b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
12313b2aab18SMatthew Ahrens 	uint64_t fromguid = drrb->drr_fromguid;
12323b2aab18SMatthew Ahrens 	int flags = drrb->drr_flags;
12333b2aab18SMatthew Ahrens 	int error;
12345d7b4d43SMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
12353b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
12363b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
12373b2aab18SMatthew Ahrens 
12383b2aab18SMatthew Ahrens 	/* already checked */
12393b2aab18SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
12409c3fd121SMatthew Ahrens 	ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
12413b2aab18SMatthew Ahrens 
12423b2aab18SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
12433b2aab18SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
12443b2aab18SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
12453b2aab18SMatthew Ahrens 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1246be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12473b2aab18SMatthew Ahrens 
12483b2aab18SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
12495d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
12505d7b4d43SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
12515d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12525d7b4d43SMatthew Ahrens 
12539c3fd121SMatthew Ahrens 	if (drba->drba_cookie->drc_resumable &&
12549c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
12559c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12569c3fd121SMatthew Ahrens 
12575d7b4d43SMatthew Ahrens 	/*
12585d7b4d43SMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
12595d7b4d43SMatthew Ahrens 	 * record to a plan WRITE record, so the pool must have the
12605d7b4d43SMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
12615d7b4d43SMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
12625d7b4d43SMatthew Ahrens 	 */
12635d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
12645d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
12655d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12665d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
12675d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1268be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12693b2aab18SMatthew Ahrens 
1270b5152584SMatthew Ahrens 	/*
1271b5152584SMatthew Ahrens 	 * The receiving code doesn't know how to translate large blocks
1272b5152584SMatthew Ahrens 	 * to smaller ones, so the pool must have the LARGE_BLOCKS
1273b5152584SMatthew Ahrens 	 * feature enabled if the stream has LARGE_BLOCKS.
1274b5152584SMatthew Ahrens 	 */
1275b5152584SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1276b5152584SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1277b5152584SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1278b5152584SMatthew Ahrens 
12793b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
12803b2aab18SMatthew Ahrens 	if (error == 0) {
12813b2aab18SMatthew Ahrens 		/* target fs already exists; recv into temp clone */
12823b2aab18SMatthew Ahrens 
12833b2aab18SMatthew Ahrens 		/* Can't recv a clone into an existing fs */
1284*68ecb2ecSPaul Dagnelie 		if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
12853b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
1286be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
12873b2aab18SMatthew Ahrens 		}
12883b2aab18SMatthew Ahrens 
12893b2aab18SMatthew Ahrens 		error = recv_begin_check_existing_impl(drba, ds, fromguid);
12903b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
12913b2aab18SMatthew Ahrens 	} else if (error == ENOENT) {
12923b2aab18SMatthew Ahrens 		/* target fs does not exist; must be a full backup or clone */
12933b2aab18SMatthew Ahrens 		char buf[MAXNAMELEN];
12943b2aab18SMatthew Ahrens 
12953b2aab18SMatthew Ahrens 		/*
12963b2aab18SMatthew Ahrens 		 * If it's a non-clone incremental, we are missing the
12973b2aab18SMatthew Ahrens 		 * target fs, so fail the recv.
12983b2aab18SMatthew Ahrens 		 */
1299a2cdcdd2SPaul Dagnelie 		if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1300a2cdcdd2SPaul Dagnelie 		    drba->drba_origin))
1301be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
13023b2aab18SMatthew Ahrens 
1303*68ecb2ecSPaul Dagnelie 		/*
1304*68ecb2ecSPaul Dagnelie 		 * If we're receiving a full send as a clone, and it doesn't
1305*68ecb2ecSPaul Dagnelie 		 * contain all the necessary free records and freeobject
1306*68ecb2ecSPaul Dagnelie 		 * records, reject it.
1307*68ecb2ecSPaul Dagnelie 		 */
1308*68ecb2ecSPaul Dagnelie 		if (fromguid == 0 && drba->drba_origin &&
1309*68ecb2ecSPaul Dagnelie 		    !(flags & DRR_FLAG_FREERECORDS))
1310*68ecb2ecSPaul Dagnelie 			return (SET_ERROR(EINVAL));
1311*68ecb2ecSPaul Dagnelie 
13123b2aab18SMatthew Ahrens 		/* Open the parent of tofs */
13133b2aab18SMatthew Ahrens 		ASSERT3U(strlen(tofs), <, MAXNAMELEN);
13143b2aab18SMatthew Ahrens 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
13153b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
13163b2aab18SMatthew Ahrens 		if (error != 0)
13173b2aab18SMatthew Ahrens 			return (error);
13183b2aab18SMatthew Ahrens 
1319a2afb611SJerry Jelinek 		/*
1320a2afb611SJerry Jelinek 		 * Check filesystem and snapshot limits before receiving. We'll
1321a2afb611SJerry Jelinek 		 * recheck snapshot limits again at the end (we create the
1322a2afb611SJerry Jelinek 		 * filesystems and increment those counts during begin_sync).
1323a2afb611SJerry Jelinek 		 */
1324a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1325a2afb611SJerry Jelinek 		    ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1326a2afb611SJerry Jelinek 		if (error != 0) {
1327a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1328a2afb611SJerry Jelinek 			return (error);
1329a2afb611SJerry Jelinek 		}
1330a2afb611SJerry Jelinek 
1331a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1332a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1333a2afb611SJerry Jelinek 		if (error != 0) {
1334a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1335a2afb611SJerry Jelinek 			return (error);
1336a2afb611SJerry Jelinek 		}
1337a2afb611SJerry Jelinek 
13383b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
13393b2aab18SMatthew Ahrens 			dsl_dataset_t *origin;
13403b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, drba->drba_origin,
13413b2aab18SMatthew Ahrens 			    FTAG, &origin);
13423b2aab18SMatthew Ahrens 			if (error != 0) {
13433b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
13443b2aab18SMatthew Ahrens 				return (error);
13453b2aab18SMatthew Ahrens 			}
1346bc9014e6SJustin Gibbs 			if (!origin->ds_is_snapshot) {
13473b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
13483b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1349be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
13503b2aab18SMatthew Ahrens 			}
1351*68ecb2ecSPaul Dagnelie 			if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
1352*68ecb2ecSPaul Dagnelie 			    fromguid != 0) {
13533b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
13543b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1355be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
13563b2aab18SMatthew Ahrens 			}
13573b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
13583b2aab18SMatthew Ahrens 		}
13593b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
13603b2aab18SMatthew Ahrens 		error = 0;
13613b2aab18SMatthew Ahrens 	}
13623b2aab18SMatthew Ahrens 	return (error);
1363f18faf3fSek }
1364f18faf3fSek 
1365f18faf3fSek static void
13663b2aab18SMatthew Ahrens dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1367f18faf3fSek {
13683b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
13693b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
13709c3fd121SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
13713b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
13723b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
13733b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *newds;
1374f18faf3fSek 	uint64_t dsobj;
13753b2aab18SMatthew Ahrens 	int error;
13769c3fd121SMatthew Ahrens 	uint64_t crflags = 0;
13773b2aab18SMatthew Ahrens 
13789c3fd121SMatthew Ahrens 	if (drrb->drr_flags & DRR_FLAG_CI_DATA)
13799c3fd121SMatthew Ahrens 		crflags |= DS_FLAG_CI_DATASET;
1380f18faf3fSek 
13813b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
13823b2aab18SMatthew Ahrens 	if (error == 0) {
13833b2aab18SMatthew Ahrens 		/* create temporary clone */
138434f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap = NULL;
138534f2f8cfSMatthew Ahrens 		if (drba->drba_snapobj != 0) {
138634f2f8cfSMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
138734f2f8cfSMatthew Ahrens 			    drba->drba_snapobj, FTAG, &snap));
138834f2f8cfSMatthew Ahrens 		}
13893b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
139034f2f8cfSMatthew Ahrens 		    snap, crflags, drba->drba_cred, tx);
1391f40b29ceSPaul Dagnelie 		if (drba->drba_snapobj != 0)
1392f40b29ceSPaul Dagnelie 			dsl_dataset_rele(snap, FTAG);
13933b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
13943b2aab18SMatthew Ahrens 	} else {
13953b2aab18SMatthew Ahrens 		dsl_dir_t *dd;
13963b2aab18SMatthew Ahrens 		const char *tail;
13973b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
13983b2aab18SMatthew Ahrens 
13993b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
14003b2aab18SMatthew Ahrens 
14013b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
14023b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
14033b2aab18SMatthew Ahrens 			    FTAG, &origin));
14043b2aab18SMatthew Ahrens 		}
14053b2aab18SMatthew Ahrens 
14063b2aab18SMatthew Ahrens 		/* Create new dataset. */
14073b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(dd,
14083b2aab18SMatthew Ahrens 		    strrchr(tofs, '/') + 1,
14093b2aab18SMatthew Ahrens 		    origin, crflags, drba->drba_cred, tx);
14103b2aab18SMatthew Ahrens 		if (origin != NULL)
14113b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
14123b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
14133b2aab18SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
14143b2aab18SMatthew Ahrens 	}
14153b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
14163b2aab18SMatthew Ahrens 
14179c3fd121SMatthew Ahrens 	if (drba->drba_cookie->drc_resumable) {
14189c3fd121SMatthew Ahrens 		dsl_dataset_zapify(newds, tx);
14199c3fd121SMatthew Ahrens 		if (drrb->drr_fromguid != 0) {
14209c3fd121SMatthew Ahrens 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
14219c3fd121SMatthew Ahrens 			    8, 1, &drrb->drr_fromguid, tx));
14229c3fd121SMatthew Ahrens 		}
14239c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
14249c3fd121SMatthew Ahrens 		    8, 1, &drrb->drr_toguid, tx));
14259c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
14269c3fd121SMatthew Ahrens 		    1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
14279c3fd121SMatthew Ahrens 		uint64_t one = 1;
14289c3fd121SMatthew Ahrens 		uint64_t zero = 0;
14299c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
14309c3fd121SMatthew Ahrens 		    8, 1, &one, tx));
14319c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
14329c3fd121SMatthew Ahrens 		    8, 1, &zero, tx));
14339c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
14349c3fd121SMatthew Ahrens 		    8, 1, &zero, tx));
14359c3fd121SMatthew Ahrens 		if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
14369c3fd121SMatthew Ahrens 		    DMU_BACKUP_FEATURE_EMBED_DATA) {
14379c3fd121SMatthew Ahrens 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
14389c3fd121SMatthew Ahrens 			    8, 1, &one, tx));
14399c3fd121SMatthew Ahrens 		}
14409c3fd121SMatthew Ahrens 	}
14419c3fd121SMatthew Ahrens 
14423b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
1443c1379625SJustin T. Gibbs 	dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
14443cb34c60Sahrens 
1445ae46e4c7SMatthew Ahrens 	/*
1446ae46e4c7SMatthew Ahrens 	 * If we actually created a non-clone, we need to create the
1447ae46e4c7SMatthew Ahrens 	 * objset in our new dataset.
1448ae46e4c7SMatthew Ahrens 	 */
14493b2aab18SMatthew Ahrens 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1450ae46e4c7SMatthew Ahrens 		(void) dmu_objset_create_impl(dp->dp_spa,
14513b2aab18SMatthew Ahrens 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1452ae46e4c7SMatthew Ahrens 	}
1453ae46e4c7SMatthew Ahrens 
14543b2aab18SMatthew Ahrens 	drba->drba_cookie->drc_ds = newds;
1455dc7cd546SMark Shellenbaum 
14563b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(newds, "receive", tx, "");
1457dc7cd546SMark Shellenbaum }
1458dc7cd546SMark Shellenbaum 
14599c3fd121SMatthew Ahrens static int
14609c3fd121SMatthew Ahrens dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
14619c3fd121SMatthew Ahrens {
14629c3fd121SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
14639c3fd121SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14649c3fd121SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
14659c3fd121SMatthew Ahrens 	int error;
14669c3fd121SMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
14679c3fd121SMatthew Ahrens 	dsl_dataset_t *ds;
14689c3fd121SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
14699c3fd121SMatthew Ahrens 
14709c3fd121SMatthew Ahrens 	/* already checked */
14719c3fd121SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
14729c3fd121SMatthew Ahrens 	ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
14739c3fd121SMatthew Ahrens 
14749c3fd121SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
14759c3fd121SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
14769c3fd121SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES)
14779c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
14789c3fd121SMatthew Ahrens 
14799c3fd121SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
14809c3fd121SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
14819c3fd121SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
14829c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
14839c3fd121SMatthew Ahrens 
14849c3fd121SMatthew Ahrens 	/*
14859c3fd121SMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
14869c3fd121SMatthew Ahrens 	 * record to a plain WRITE record, so the pool must have the
14879c3fd121SMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
14889c3fd121SMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
14899c3fd121SMatthew Ahrens 	 */
14909c3fd121SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
14919c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
14929c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
14939c3fd121SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
14949c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
14959c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
14969c3fd121SMatthew Ahrens 
14979c3fd121SMatthew Ahrens 	char recvname[ZFS_MAXNAMELEN];
14989c3fd121SMatthew Ahrens 
14999c3fd121SMatthew Ahrens 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
15009c3fd121SMatthew Ahrens 	    tofs, recv_clone_name);
15019c3fd121SMatthew Ahrens 
15029c3fd121SMatthew Ahrens 	if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
15039c3fd121SMatthew Ahrens 		/* %recv does not exist; continue in tofs */
15049c3fd121SMatthew Ahrens 		error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
15059c3fd121SMatthew Ahrens 		if (error != 0)
15069c3fd121SMatthew Ahrens 			return (error);
15079c3fd121SMatthew Ahrens 	}
15089c3fd121SMatthew Ahrens 
15099c3fd121SMatthew Ahrens 	/* check that ds is marked inconsistent */
15109c3fd121SMatthew Ahrens 	if (!DS_IS_INCONSISTENT(ds)) {
15119c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15129c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15139c3fd121SMatthew Ahrens 	}
15149c3fd121SMatthew Ahrens 
15159c3fd121SMatthew Ahrens 	/* check that there is resuming data, and that the toguid matches */
15169c3fd121SMatthew Ahrens 	if (!dsl_dataset_is_zapified(ds)) {
15179c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15189c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15199c3fd121SMatthew Ahrens 	}
15209c3fd121SMatthew Ahrens 	uint64_t val;
15219c3fd121SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
15229c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
15239c3fd121SMatthew Ahrens 	if (error != 0 || drrb->drr_toguid != val) {
15249c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15259c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15269c3fd121SMatthew Ahrens 	}
15279c3fd121SMatthew Ahrens 
15289c3fd121SMatthew Ahrens 	/*
15299c3fd121SMatthew Ahrens 	 * Check if the receive is still running.  If so, it will be owned.
15309c3fd121SMatthew Ahrens 	 * Note that nothing else can own the dataset (e.g. after the receive
15319c3fd121SMatthew Ahrens 	 * fails) because it will be marked inconsistent.
15329c3fd121SMatthew Ahrens 	 */
15339c3fd121SMatthew Ahrens 	if (dsl_dataset_has_owner(ds)) {
15349c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15359c3fd121SMatthew Ahrens 		return (SET_ERROR(EBUSY));
15369c3fd121SMatthew Ahrens 	}
15379c3fd121SMatthew Ahrens 
15389c3fd121SMatthew Ahrens 	/* There should not be any snapshots of this fs yet. */
15399c3fd121SMatthew Ahrens 	if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
15409c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15419c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15429c3fd121SMatthew Ahrens 	}
15439c3fd121SMatthew Ahrens 
15449c3fd121SMatthew Ahrens 	/*
15459c3fd121SMatthew Ahrens 	 * Note: resume point will be checked when we process the first WRITE
15469c3fd121SMatthew Ahrens 	 * record.
15479c3fd121SMatthew Ahrens 	 */
15489c3fd121SMatthew Ahrens 
15499c3fd121SMatthew Ahrens 	/* check that the origin matches */
15509c3fd121SMatthew Ahrens 	val = 0;
15519c3fd121SMatthew Ahrens 	(void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
15529c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
15539c3fd121SMatthew Ahrens 	if (drrb->drr_fromguid != val) {
15549c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15559c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15569c3fd121SMatthew Ahrens 	}
15579c3fd121SMatthew Ahrens 
15589c3fd121SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15599c3fd121SMatthew Ahrens 	return (0);
15609c3fd121SMatthew Ahrens }
15619c3fd121SMatthew Ahrens 
15629c3fd121SMatthew Ahrens static void
15639c3fd121SMatthew Ahrens dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
15649c3fd121SMatthew Ahrens {
15659c3fd121SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
15669c3fd121SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15679c3fd121SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
15689c3fd121SMatthew Ahrens 	dsl_dataset_t *ds;
15699c3fd121SMatthew Ahrens 	uint64_t dsobj;
15709c3fd121SMatthew Ahrens 	char recvname[ZFS_MAXNAMELEN];
15719c3fd121SMatthew Ahrens 
15729c3fd121SMatthew Ahrens 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
15739c3fd121SMatthew Ahrens 	    tofs, recv_clone_name);
15749c3fd121SMatthew Ahrens 
15759c3fd121SMatthew Ahrens 	if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
15769c3fd121SMatthew Ahrens 		/* %recv does not exist; continue in tofs */
15779c3fd121SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
15789c3fd121SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
15799c3fd121SMatthew Ahrens 	}
15809c3fd121SMatthew Ahrens 
15819c3fd121SMatthew Ahrens 	/* clear the inconsistent flag so that we can own it */
15829c3fd121SMatthew Ahrens 	ASSERT(DS_IS_INCONSISTENT(ds));
15839c3fd121SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
15849c3fd121SMatthew Ahrens 	dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
15859c3fd121SMatthew Ahrens 	dsobj = ds->ds_object;
15869c3fd121SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15879c3fd121SMatthew Ahrens 
15889c3fd121SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
15899c3fd121SMatthew Ahrens 
15909c3fd121SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
15919c3fd121SMatthew Ahrens 	dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
15929c3fd121SMatthew Ahrens 
15939c3fd121SMatthew Ahrens 	ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
15949c3fd121SMatthew Ahrens 
15959c3fd121SMatthew Ahrens 	drba->drba_cookie->drc_ds = ds;
15969c3fd121SMatthew Ahrens 
15979c3fd121SMatthew Ahrens 	spa_history_log_internal_ds(ds, "resume receive", tx, "");
15989c3fd121SMatthew Ahrens }
15999c3fd121SMatthew Ahrens 
16003cb34c60Sahrens /*
16013cb34c60Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
16023cb34c60Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
16033cb34c60Sahrens  */
16043cb34c60Sahrens int
16059c3fd121SMatthew Ahrens dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
16069c3fd121SMatthew Ahrens     boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1607efb80947Sahrens {
16083b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t drba = { 0 };
1609ab04eb8eStimh 
16103cb34c60Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
16119c3fd121SMatthew Ahrens 	drc->drc_drr_begin = drr_begin;
16129c3fd121SMatthew Ahrens 	drc->drc_drrb = &drr_begin->drr_u.drr_begin;
16133cb34c60Sahrens 	drc->drc_tosnap = tosnap;
16143b2aab18SMatthew Ahrens 	drc->drc_tofs = tofs;
16153cb34c60Sahrens 	drc->drc_force = force;
16169c3fd121SMatthew Ahrens 	drc->drc_resumable = resumable;
1617a2afb611SJerry Jelinek 	drc->drc_cred = CRED();
1618efb80947Sahrens 
16199c3fd121SMatthew Ahrens 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
16203b2aab18SMatthew Ahrens 		drc->drc_byteswap = B_TRUE;
16219c3fd121SMatthew Ahrens 		fletcher_4_incremental_byteswap(drr_begin,
16223b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
16239c3fd121SMatthew Ahrens 		byteswap_record(drr_begin);
16249c3fd121SMatthew Ahrens 	} else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
16259c3fd121SMatthew Ahrens 		fletcher_4_incremental_native(drr_begin,
16263b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
16279c3fd121SMatthew Ahrens 	} else {
16289c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16293cb34c60Sahrens 	}
16303cb34c60Sahrens 
16313b2aab18SMatthew Ahrens 	drba.drba_origin = origin;
16323b2aab18SMatthew Ahrens 	drba.drba_cookie = drc;
16333b2aab18SMatthew Ahrens 	drba.drba_cred = CRED();
16343b2aab18SMatthew Ahrens 
16359c3fd121SMatthew Ahrens 	if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
16369c3fd121SMatthew Ahrens 	    DMU_BACKUP_FEATURE_RESUMING) {
16379c3fd121SMatthew Ahrens 		return (dsl_sync_task(tofs,
16389c3fd121SMatthew Ahrens 		    dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
16399c3fd121SMatthew Ahrens 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
16409c3fd121SMatthew Ahrens 	} else  {
16419c3fd121SMatthew Ahrens 		return (dsl_sync_task(tofs,
16429c3fd121SMatthew Ahrens 		    dmu_recv_begin_check, dmu_recv_begin_sync,
16439c3fd121SMatthew Ahrens 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
16449c3fd121SMatthew Ahrens 	}
1645efb80947Sahrens }
1646efb80947Sahrens 
1647a2cdcdd2SPaul Dagnelie struct receive_record_arg {
1648a2cdcdd2SPaul Dagnelie 	dmu_replay_record_t header;
1649a2cdcdd2SPaul Dagnelie 	void *payload; /* Pointer to a buffer containing the payload */
1650a2cdcdd2SPaul Dagnelie 	/*
1651a2cdcdd2SPaul Dagnelie 	 * If the record is a write, pointer to the arc_buf_t containing the
1652a2cdcdd2SPaul Dagnelie 	 * payload.
1653a2cdcdd2SPaul Dagnelie 	 */
1654a2cdcdd2SPaul Dagnelie 	arc_buf_t *write_buf;
1655a2cdcdd2SPaul Dagnelie 	int payload_size;
16569c3fd121SMatthew Ahrens 	uint64_t bytes_read; /* bytes read from stream when record created */
1657a2cdcdd2SPaul Dagnelie 	boolean_t eos_marker; /* Marks the end of the stream */
1658a2cdcdd2SPaul Dagnelie 	bqueue_node_t node;
1659a2cdcdd2SPaul Dagnelie };
1660a2cdcdd2SPaul Dagnelie 
1661a2cdcdd2SPaul Dagnelie struct receive_writer_arg {
166298110f08SMatthew Ahrens 	objset_t *os;
16633b2aab18SMatthew Ahrens 	boolean_t byteswap;
1664a2cdcdd2SPaul Dagnelie 	bqueue_t q;
16659c3fd121SMatthew Ahrens 
1666a2cdcdd2SPaul Dagnelie 	/*
1667a2cdcdd2SPaul Dagnelie 	 * These three args are used to signal to the main thread that we're
1668a2cdcdd2SPaul Dagnelie 	 * done.
1669a2cdcdd2SPaul Dagnelie 	 */
1670a2cdcdd2SPaul Dagnelie 	kmutex_t mutex;
1671a2cdcdd2SPaul Dagnelie 	kcondvar_t cv;
1672a2cdcdd2SPaul Dagnelie 	boolean_t done;
16739c3fd121SMatthew Ahrens 
1674a2cdcdd2SPaul Dagnelie 	int err;
1675a2cdcdd2SPaul Dagnelie 	/* A map from guid to dataset to help handle dedup'd streams. */
1676a2cdcdd2SPaul Dagnelie 	avl_tree_t *guid_to_ds_map;
16779c3fd121SMatthew Ahrens 	boolean_t resumable;
16789c3fd121SMatthew Ahrens 	uint64_t last_object, last_offset;
16799c3fd121SMatthew Ahrens 	uint64_t bytes_read; /* bytes read when current record created */
1680a2cdcdd2SPaul Dagnelie };
168198110f08SMatthew Ahrens 
1682*68ecb2ecSPaul Dagnelie struct objlist {
1683*68ecb2ecSPaul Dagnelie 	list_t list; /* List of struct receive_objnode. */
1684*68ecb2ecSPaul Dagnelie 	/*
1685*68ecb2ecSPaul Dagnelie 	 * Last object looked up. Used to assert that objects are being looked
1686*68ecb2ecSPaul Dagnelie 	 * up in ascending order.
1687*68ecb2ecSPaul Dagnelie 	 */
1688*68ecb2ecSPaul Dagnelie 	uint64_t last_lookup;
1689*68ecb2ecSPaul Dagnelie };
1690*68ecb2ecSPaul Dagnelie 
1691*68ecb2ecSPaul Dagnelie struct receive_objnode {
1692*68ecb2ecSPaul Dagnelie 	list_node_t node;
1693*68ecb2ecSPaul Dagnelie 	uint64_t object;
1694*68ecb2ecSPaul Dagnelie };
1695*68ecb2ecSPaul Dagnelie 
1696a2cdcdd2SPaul Dagnelie struct receive_arg  {
1697a2cdcdd2SPaul Dagnelie 	objset_t *os;
1698a2cdcdd2SPaul Dagnelie 	vnode_t *vp; /* The vnode to read the stream from */
1699a2cdcdd2SPaul Dagnelie 	uint64_t voff; /* The current offset in the stream */
17009c3fd121SMatthew Ahrens 	uint64_t bytes_read;
1701a2cdcdd2SPaul Dagnelie 	/*
1702a2cdcdd2SPaul Dagnelie 	 * A record that has had its payload read in, but hasn't yet been handed
1703a2cdcdd2SPaul Dagnelie 	 * off to the worker thread.
1704a2cdcdd2SPaul Dagnelie 	 */
1705a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *rrd;
1706a2cdcdd2SPaul Dagnelie 	/* A record that has had its header read in, but not its payload. */
1707a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *next_rrd;
17083cb34c60Sahrens 	zio_cksum_t cksum;
170998110f08SMatthew Ahrens 	zio_cksum_t prev_cksum;
1710a2cdcdd2SPaul Dagnelie 	int err;
1711a2cdcdd2SPaul Dagnelie 	boolean_t byteswap;
1712a2cdcdd2SPaul Dagnelie 	/* Sorted list of objects not to issue prefetches for. */
1713*68ecb2ecSPaul Dagnelie 	struct objlist ignore_objlist;
17143cb34c60Sahrens };
17153cb34c60Sahrens 
17169e69d7d0SLori Alt typedef struct guid_map_entry {
17179e69d7d0SLori Alt 	uint64_t	guid;
17189e69d7d0SLori Alt 	dsl_dataset_t	*gme_ds;
17199e69d7d0SLori Alt 	avl_node_t	avlnode;
17209e69d7d0SLori Alt } guid_map_entry_t;
17219e69d7d0SLori Alt 
17229e69d7d0SLori Alt static int
17239e69d7d0SLori Alt guid_compare(const void *arg1, const void *arg2)
17249e69d7d0SLori Alt {
17259e69d7d0SLori Alt 	const guid_map_entry_t *gmep1 = arg1;
17269e69d7d0SLori Alt 	const guid_map_entry_t *gmep2 = arg2;
17279e69d7d0SLori Alt 
17289e69d7d0SLori Alt 	if (gmep1->guid < gmep2->guid)
17299e69d7d0SLori Alt 		return (-1);
17309e69d7d0SLori Alt 	else if (gmep1->guid > gmep2->guid)
17319e69d7d0SLori Alt 		return (1);
17329e69d7d0SLori Alt 	return (0);
17339e69d7d0SLori Alt }
17349e69d7d0SLori Alt 
1735c99e4bdcSChris Kirby static void
1736c99e4bdcSChris Kirby free_guid_map_onexit(void *arg)
1737c99e4bdcSChris Kirby {
1738c99e4bdcSChris Kirby 	avl_tree_t *ca = arg;
1739c99e4bdcSChris Kirby 	void *cookie = NULL;
1740c99e4bdcSChris Kirby 	guid_map_entry_t *gmep;
1741c99e4bdcSChris Kirby 
1742c99e4bdcSChris Kirby 	while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
17433b2aab18SMatthew Ahrens 		dsl_dataset_long_rele(gmep->gme_ds, gmep);
1744de8d9cffSMatthew Ahrens 		dsl_dataset_rele(gmep->gme_ds, gmep);
1745c99e4bdcSChris Kirby 		kmem_free(gmep, sizeof (guid_map_entry_t));
1746c99e4bdcSChris Kirby 	}
1747c99e4bdcSChris Kirby 	avl_destroy(ca);
1748c99e4bdcSChris Kirby 	kmem_free(ca, sizeof (avl_tree_t));
1749c99e4bdcSChris Kirby }
1750c99e4bdcSChris Kirby 
175198110f08SMatthew Ahrens static int
1752a2cdcdd2SPaul Dagnelie receive_read(struct receive_arg *ra, int len, void *buf)
1753efb80947Sahrens {
17543cb34c60Sahrens 	int done = 0;
1755efb80947Sahrens 
1756efb80947Sahrens 	/* some things will require 8-byte alignment, so everything must */
1757fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
1758efb80947Sahrens 
17593cb34c60Sahrens 	while (done < len) {
1760efb80947Sahrens 		ssize_t resid;
1761efb80947Sahrens 
1762efb80947Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
176398110f08SMatthew Ahrens 		    (char *)buf + done, len - done,
1764efb80947Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
1765efb80947Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
1766efb80947Sahrens 
17679c3fd121SMatthew Ahrens 		if (resid == len - done) {
17689c3fd121SMatthew Ahrens 			/*
17699c3fd121SMatthew Ahrens 			 * Note: ECKSUM indicates that the receive
17709c3fd121SMatthew Ahrens 			 * was interrupted and can potentially be resumed.
17719c3fd121SMatthew Ahrens 			 */
17729c3fd121SMatthew Ahrens 			ra->err = SET_ERROR(ECKSUM);
17739c3fd121SMatthew Ahrens 		}
17743cb34c60Sahrens 		ra->voff += len - done - resid;
17753cb34c60Sahrens 		done = len - resid;
17763b2aab18SMatthew Ahrens 		if (ra->err != 0)
177798110f08SMatthew Ahrens 			return (ra->err);
1778efb80947Sahrens 	}
1779efb80947Sahrens 
17809c3fd121SMatthew Ahrens 	ra->bytes_read += len;
17819c3fd121SMatthew Ahrens 
17823cb34c60Sahrens 	ASSERT3U(done, ==, len);
178398110f08SMatthew Ahrens 	return (0);
1784efb80947Sahrens }
1785efb80947Sahrens 
1786efb80947Sahrens static void
178798110f08SMatthew Ahrens byteswap_record(dmu_replay_record_t *drr)
1788efb80947Sahrens {
1789efb80947Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1790efb80947Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1791efb80947Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
17923cb34c60Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
179398110f08SMatthew Ahrens 
1794efb80947Sahrens 	switch (drr->drr_type) {
1795efb80947Sahrens 	case DRR_BEGIN:
1796efb80947Sahrens 		DO64(drr_begin.drr_magic);
17979e69d7d0SLori Alt 		DO64(drr_begin.drr_versioninfo);
1798efb80947Sahrens 		DO64(drr_begin.drr_creation_time);
1799efb80947Sahrens 		DO32(drr_begin.drr_type);
18003cb34c60Sahrens 		DO32(drr_begin.drr_flags);
1801efb80947Sahrens 		DO64(drr_begin.drr_toguid);
1802efb80947Sahrens 		DO64(drr_begin.drr_fromguid);
1803efb80947Sahrens 		break;
1804efb80947Sahrens 	case DRR_OBJECT:
1805efb80947Sahrens 		DO64(drr_object.drr_object);
1806efb80947Sahrens 		DO32(drr_object.drr_type);
1807efb80947Sahrens 		DO32(drr_object.drr_bonustype);
1808efb80947Sahrens 		DO32(drr_object.drr_blksz);
1809efb80947Sahrens 		DO32(drr_object.drr_bonuslen);
18109e69d7d0SLori Alt 		DO64(drr_object.drr_toguid);
1811efb80947Sahrens 		break;
1812efb80947Sahrens 	case DRR_FREEOBJECTS:
1813efb80947Sahrens 		DO64(drr_freeobjects.drr_firstobj);
1814efb80947Sahrens 		DO64(drr_freeobjects.drr_numobjs);
18159e69d7d0SLori Alt 		DO64(drr_freeobjects.drr_toguid);
1816efb80947Sahrens 		break;
1817efb80947Sahrens 	case DRR_WRITE:
1818efb80947Sahrens 		DO64(drr_write.drr_object);
1819efb80947Sahrens 		DO32(drr_write.drr_type);
1820efb80947Sahrens 		DO64(drr_write.drr_offset);
1821efb80947Sahrens 		DO64(drr_write.drr_length);
18229e69d7d0SLori Alt 		DO64(drr_write.drr_toguid);
182398110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
18248e714474SLori Alt 		DO64(drr_write.drr_key.ddk_prop);
18259e69d7d0SLori Alt 		break;
18269e69d7d0SLori Alt 	case DRR_WRITE_BYREF:
18279e69d7d0SLori Alt 		DO64(drr_write_byref.drr_object);
18289e69d7d0SLori Alt 		DO64(drr_write_byref.drr_offset);
18299e69d7d0SLori Alt 		DO64(drr_write_byref.drr_length);
18309e69d7d0SLori Alt 		DO64(drr_write_byref.drr_toguid);
18319e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refguid);
18329e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refobject);
18339e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refoffset);
183498110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
183598110f08SMatthew Ahrens 		    drr_key.ddk_cksum);
18368e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_prop);
1837efb80947Sahrens 		break;
18385d7b4d43SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
18395d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_object);
18405d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_offset);
18415d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_length);
18425d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_toguid);
18435d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_lsize);
18445d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_psize);
18455d7b4d43SMatthew Ahrens 		break;
1846efb80947Sahrens 	case DRR_FREE:
1847efb80947Sahrens 		DO64(drr_free.drr_object);
1848efb80947Sahrens 		DO64(drr_free.drr_offset);
1849efb80947Sahrens 		DO64(drr_free.drr_length);
18509e69d7d0SLori Alt 		DO64(drr_free.drr_toguid);
1851efb80947Sahrens 		break;
18520a586ceaSMark Shellenbaum 	case DRR_SPILL:
18530a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_object);
18540a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_length);
18550a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_toguid);
18560a586ceaSMark Shellenbaum 		break;
1857efb80947Sahrens 	case DRR_END:
18589e69d7d0SLori Alt 		DO64(drr_end.drr_toguid);
185998110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1860efb80947Sahrens 		break;
1861efb80947Sahrens 	}
186298110f08SMatthew Ahrens 
186398110f08SMatthew Ahrens 	if (drr->drr_type != DRR_BEGIN) {
186498110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
186598110f08SMatthew Ahrens 	}
186698110f08SMatthew Ahrens 
1867efb80947Sahrens #undef DO64
1868efb80947Sahrens #undef DO32
1869efb80947Sahrens }
1870efb80947Sahrens 
1871e77d42eaSMatthew Ahrens static inline uint8_t
1872e77d42eaSMatthew Ahrens deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1873e77d42eaSMatthew Ahrens {
1874e77d42eaSMatthew Ahrens 	if (bonus_type == DMU_OT_SA) {
1875e77d42eaSMatthew Ahrens 		return (1);
1876e77d42eaSMatthew Ahrens 	} else {
1877e77d42eaSMatthew Ahrens 		return (1 +
1878e77d42eaSMatthew Ahrens 		    ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
1879e77d42eaSMatthew Ahrens 	}
1880e77d42eaSMatthew Ahrens }
1881e77d42eaSMatthew Ahrens 
18829c3fd121SMatthew Ahrens static void
18839c3fd121SMatthew Ahrens save_resume_state(struct receive_writer_arg *rwa,
18849c3fd121SMatthew Ahrens     uint64_t object, uint64_t offset, dmu_tx_t *tx)
18859c3fd121SMatthew Ahrens {
18869c3fd121SMatthew Ahrens 	int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
18879c3fd121SMatthew Ahrens 
18889c3fd121SMatthew Ahrens 	if (!rwa->resumable)
18899c3fd121SMatthew Ahrens 		return;
18909c3fd121SMatthew Ahrens 
18919c3fd121SMatthew Ahrens 	/*
18929c3fd121SMatthew Ahrens 	 * We use ds_resume_bytes[] != 0 to indicate that we need to
18939c3fd121SMatthew Ahrens 	 * update this on disk, so it must not be 0.
18949c3fd121SMatthew Ahrens 	 */
18959c3fd121SMatthew Ahrens 	ASSERT(rwa->bytes_read != 0);
18969c3fd121SMatthew Ahrens 
18979c3fd121SMatthew Ahrens 	/*
18989c3fd121SMatthew Ahrens 	 * We only resume from write records, which have a valid
18999c3fd121SMatthew Ahrens 	 * (non-meta-dnode) object number.
19009c3fd121SMatthew Ahrens 	 */
19019c3fd121SMatthew Ahrens 	ASSERT(object != 0);
19029c3fd121SMatthew Ahrens 
19039c3fd121SMatthew Ahrens 	/*
19049c3fd121SMatthew Ahrens 	 * For resuming to work correctly, we must receive records in order,
19059c3fd121SMatthew Ahrens 	 * sorted by object,offset.  This is checked by the callers, but
19069c3fd121SMatthew Ahrens 	 * assert it here for good measure.
19079c3fd121SMatthew Ahrens 	 */
19089c3fd121SMatthew Ahrens 	ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
19099c3fd121SMatthew Ahrens 	ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
19109c3fd121SMatthew Ahrens 	    offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
19119c3fd121SMatthew Ahrens 	ASSERT3U(rwa->bytes_read, >=,
19129c3fd121SMatthew Ahrens 	    rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
19139c3fd121SMatthew Ahrens 
19149c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
19159c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
19169c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
19179c3fd121SMatthew Ahrens }
19189c3fd121SMatthew Ahrens 
1919efb80947Sahrens static int
1920a2cdcdd2SPaul Dagnelie receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1921a2cdcdd2SPaul Dagnelie     void *data)
1922efb80947Sahrens {
1923e77d42eaSMatthew Ahrens 	dmu_object_info_t doi;
1924efb80947Sahrens 	dmu_tx_t *tx;
1925e77d42eaSMatthew Ahrens 	uint64_t object;
1926e77d42eaSMatthew Ahrens 	int err;
1927efb80947Sahrens 
1928efb80947Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
1929ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_type) ||
1930ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
19319e69d7d0SLori Alt 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1932efb80947Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1933efb80947Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1934efb80947Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
1935a2cdcdd2SPaul Dagnelie 	    drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
1936efb80947Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1937be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1938efb80947Sahrens 	}
1939efb80947Sahrens 
1940a2cdcdd2SPaul Dagnelie 	err = dmu_object_info(rwa->os, drro->drr_object, &doi);
19412bf405a2SMark Maybee 
19422bf405a2SMark Maybee 	if (err != 0 && err != ENOENT)
1943be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1944e77d42eaSMatthew Ahrens 	object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
19452bf405a2SMark Maybee 
1946e77d42eaSMatthew Ahrens 	/*
1947e77d42eaSMatthew Ahrens 	 * If we are losing blkptrs or changing the block size this must
1948e77d42eaSMatthew Ahrens 	 * be a new file instance.  We must clear out the previous file
1949e77d42eaSMatthew Ahrens 	 * contents before we can change this type of metadata in the dnode.
1950e77d42eaSMatthew Ahrens 	 */
1951e77d42eaSMatthew Ahrens 	if (err == 0) {
1952e77d42eaSMatthew Ahrens 		int nblkptr;
1953e77d42eaSMatthew Ahrens 
1954e77d42eaSMatthew Ahrens 		nblkptr = deduce_nblkptr(drro->drr_bonustype,
1955e77d42eaSMatthew Ahrens 		    drro->drr_bonuslen);
1956e77d42eaSMatthew Ahrens 
1957e77d42eaSMatthew Ahrens 		if (drro->drr_blksz != doi.doi_data_block_size ||
1958e77d42eaSMatthew Ahrens 		    nblkptr < doi.doi_nblkptr) {
1959a2cdcdd2SPaul Dagnelie 			err = dmu_free_long_range(rwa->os, drro->drr_object,
1960e77d42eaSMatthew Ahrens 			    0, DMU_OBJECT_END);
1961e77d42eaSMatthew Ahrens 			if (err != 0)
1962e77d42eaSMatthew Ahrens 				return (SET_ERROR(EINVAL));
1963efb80947Sahrens 		}
1964e77d42eaSMatthew Ahrens 	}
1965e77d42eaSMatthew Ahrens 
1966a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
1967e77d42eaSMatthew Ahrens 	dmu_tx_hold_bonus(tx, object);
1968e77d42eaSMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
1969e77d42eaSMatthew Ahrens 	if (err != 0) {
1970e77d42eaSMatthew Ahrens 		dmu_tx_abort(tx);
1971e77d42eaSMatthew Ahrens 		return (err);
1972e77d42eaSMatthew Ahrens 	}
1973e77d42eaSMatthew Ahrens 
1974e77d42eaSMatthew Ahrens 	if (object == DMU_NEW_OBJECT) {
1975e77d42eaSMatthew Ahrens 		/* currently free, want to be allocated */
1976a2cdcdd2SPaul Dagnelie 		err = dmu_object_claim(rwa->os, drro->drr_object,
1977efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
1978efb80947Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1979e77d42eaSMatthew Ahrens 	} else if (drro->drr_type != doi.doi_type ||
1980e77d42eaSMatthew Ahrens 	    drro->drr_blksz != doi.doi_data_block_size ||
1981e77d42eaSMatthew Ahrens 	    drro->drr_bonustype != doi.doi_bonus_type ||
1982e77d42eaSMatthew Ahrens 	    drro->drr_bonuslen != doi.doi_bonus_size) {
1983e77d42eaSMatthew Ahrens 		/* currently allocated, but with different properties */
1984a2cdcdd2SPaul Dagnelie 		err = dmu_object_reclaim(rwa->os, drro->drr_object,
1985efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
1986e77d42eaSMatthew Ahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1987efb80947Sahrens 	}
19883b2aab18SMatthew Ahrens 	if (err != 0) {
1989e77d42eaSMatthew Ahrens 		dmu_tx_commit(tx);
1990be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
19910a586ceaSMark Shellenbaum 	}
19922bf405a2SMark Maybee 
1993a2cdcdd2SPaul Dagnelie 	dmu_object_set_checksum(rwa->os, drro->drr_object,
199498110f08SMatthew Ahrens 	    drro->drr_checksumtype, tx);
1995a2cdcdd2SPaul Dagnelie 	dmu_object_set_compress(rwa->os, drro->drr_object,
199698110f08SMatthew Ahrens 	    drro->drr_compress, tx);
1997efb80947Sahrens 
1998adee0b6fSTim Haley 	if (data != NULL) {
1999efb80947Sahrens 		dmu_buf_t *db;
2000adee0b6fSTim Haley 
2001a2cdcdd2SPaul Dagnelie 		VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
2002efb80947Sahrens 		dmu_buf_will_dirty(db, tx);
2003efb80947Sahrens 
20041934e92fSmaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
20051934e92fSmaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
2006a2cdcdd2SPaul Dagnelie 		if (rwa->byteswap) {
2007ad135b5dSChristopher Siden 			dmu_object_byteswap_t byteswap =
2008ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
2009ad135b5dSChristopher Siden 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2010efb80947Sahrens 			    drro->drr_bonuslen);
2011efb80947Sahrens 		}
2012efb80947Sahrens 		dmu_buf_rele(db, FTAG);
2013efb80947Sahrens 	}
2014efb80947Sahrens 	dmu_tx_commit(tx);
20159c3fd121SMatthew Ahrens 
2016efb80947Sahrens 	return (0);
2017efb80947Sahrens }
2018efb80947Sahrens 
2019efb80947Sahrens /* ARGSUSED */
2020efb80947Sahrens static int
2021a2cdcdd2SPaul Dagnelie receive_freeobjects(struct receive_writer_arg *rwa,
2022efb80947Sahrens     struct drr_freeobjects *drrfo)
2023efb80947Sahrens {
2024efb80947Sahrens 	uint64_t obj;
2025*68ecb2ecSPaul Dagnelie 	int next_err = 0;
2026efb80947Sahrens 
2027efb80947Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2028be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2029efb80947Sahrens 
2030efb80947Sahrens 	for (obj = drrfo->drr_firstobj;
2031*68ecb2ecSPaul Dagnelie 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
2032*68ecb2ecSPaul Dagnelie 	    next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2033efb80947Sahrens 		int err;
2034efb80947Sahrens 
2035a2cdcdd2SPaul Dagnelie 		if (dmu_object_info(rwa->os, obj, NULL) != 0)
2036efb80947Sahrens 			continue;
2037efb80947Sahrens 
2038a2cdcdd2SPaul Dagnelie 		err = dmu_free_long_object(rwa->os, obj);
20393b2aab18SMatthew Ahrens 		if (err != 0)
2040efb80947Sahrens 			return (err);
2041efb80947Sahrens 	}
2042*68ecb2ecSPaul Dagnelie 	if (next_err != ESRCH)
2043*68ecb2ecSPaul Dagnelie 		return (next_err);
2044efb80947Sahrens 	return (0);
2045efb80947Sahrens }
2046efb80947Sahrens 
2047efb80947Sahrens static int
2048a2cdcdd2SPaul Dagnelie receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2049a2cdcdd2SPaul Dagnelie     arc_buf_t *abuf)
2050efb80947Sahrens {
2051efb80947Sahrens 	dmu_tx_t *tx;
2052efb80947Sahrens 	int err;
2053efb80947Sahrens 
2054efb80947Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
2055ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drrw->drr_type))
2056be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2057efb80947Sahrens 
20589c3fd121SMatthew Ahrens 	/*
20599c3fd121SMatthew Ahrens 	 * For resuming to work, records must be in increasing order
20609c3fd121SMatthew Ahrens 	 * by (object, offset).
20619c3fd121SMatthew Ahrens 	 */
20629c3fd121SMatthew Ahrens 	if (drrw->drr_object < rwa->last_object ||
20639c3fd121SMatthew Ahrens 	    (drrw->drr_object == rwa->last_object &&
20649c3fd121SMatthew Ahrens 	    drrw->drr_offset < rwa->last_offset)) {
20659c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
20669c3fd121SMatthew Ahrens 	}
20679c3fd121SMatthew Ahrens 	rwa->last_object = drrw->drr_object;
20689c3fd121SMatthew Ahrens 	rwa->last_offset = drrw->drr_offset;
20699c3fd121SMatthew Ahrens 
2070a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
20718a904709SMatthew Ahrens 		return (SET_ERROR(EINVAL));
20728a904709SMatthew Ahrens 
2073a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
2074efb80947Sahrens 
2075efb80947Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
2076efb80947Sahrens 	    drrw->drr_offset, drrw->drr_length);
2077efb80947Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
20783b2aab18SMatthew Ahrens 	if (err != 0) {
2079efb80947Sahrens 		dmu_tx_abort(tx);
2080efb80947Sahrens 		return (err);
2081efb80947Sahrens 	}
2082a2cdcdd2SPaul Dagnelie 	if (rwa->byteswap) {
2083ad135b5dSChristopher Siden 		dmu_object_byteswap_t byteswap =
2084ad135b5dSChristopher Siden 		    DMU_OT_BYTESWAP(drrw->drr_type);
208598110f08SMatthew Ahrens 		dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
208698110f08SMatthew Ahrens 		    drrw->drr_length);
2087ad135b5dSChristopher Siden 	}
208898110f08SMatthew Ahrens 
208998110f08SMatthew Ahrens 	dmu_buf_t *bonus;
2090a2cdcdd2SPaul Dagnelie 	if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
209198110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
20928a904709SMatthew Ahrens 	dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
20939c3fd121SMatthew Ahrens 
20949c3fd121SMatthew Ahrens 	/*
20959c3fd121SMatthew Ahrens 	 * Note: If the receive fails, we want the resume stream to start
20969c3fd121SMatthew Ahrens 	 * with the same record that we last successfully received (as opposed
20979c3fd121SMatthew Ahrens 	 * to the next record), so that we can verify that we are
20989c3fd121SMatthew Ahrens 	 * resuming from the correct location.
20999c3fd121SMatthew Ahrens 	 */
21009c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2101efb80947Sahrens 	dmu_tx_commit(tx);
21028a904709SMatthew Ahrens 	dmu_buf_rele(bonus, FTAG);
21039c3fd121SMatthew Ahrens 
2104efb80947Sahrens 	return (0);
2105efb80947Sahrens }
2106efb80947Sahrens 
21079e69d7d0SLori Alt /*
21089e69d7d0SLori Alt  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
21099e69d7d0SLori Alt  * streams to refer to a copy of the data that is already on the
21109e69d7d0SLori Alt  * system because it came in earlier in the stream.  This function
21119e69d7d0SLori Alt  * finds the earlier copy of the data, and uses that copy instead of
21129e69d7d0SLori Alt  * data from the stream to fulfill this write.
21139e69d7d0SLori Alt  */
21149e69d7d0SLori Alt static int
2115a2cdcdd2SPaul Dagnelie receive_write_byref(struct receive_writer_arg *rwa,
2116a2cdcdd2SPaul Dagnelie     struct drr_write_byref *drrwbr)
21179e69d7d0SLori Alt {
21189e69d7d0SLori Alt 	dmu_tx_t *tx;
21199e69d7d0SLori Alt 	int err;
21209e69d7d0SLori Alt 	guid_map_entry_t gmesrch;
21219e69d7d0SLori Alt 	guid_map_entry_t *gmep;
21225d7b4d43SMatthew Ahrens 	avl_index_t where;
21239e69d7d0SLori Alt 	objset_t *ref_os = NULL;
21249e69d7d0SLori Alt 	dmu_buf_t *dbp;
21259e69d7d0SLori Alt 
21269e69d7d0SLori Alt 	if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2127be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21289e69d7d0SLori Alt 
21299e69d7d0SLori Alt 	/*
21309e69d7d0SLori Alt 	 * If the GUID of the referenced dataset is different from the
21319e69d7d0SLori Alt 	 * GUID of the target dataset, find the referenced dataset.
21329e69d7d0SLori Alt 	 */
21339e69d7d0SLori Alt 	if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
21349e69d7d0SLori Alt 		gmesrch.guid = drrwbr->drr_refguid;
2135a2cdcdd2SPaul Dagnelie 		if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
21369e69d7d0SLori Alt 		    &where)) == NULL) {
2137be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
21389e69d7d0SLori Alt 		}
21399e69d7d0SLori Alt 		if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2140be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
21419e69d7d0SLori Alt 	} else {
2142a2cdcdd2SPaul Dagnelie 		ref_os = rwa->os;
21439e69d7d0SLori Alt 	}
21449e69d7d0SLori Alt 
21455d7b4d43SMatthew Ahrens 	err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
21465d7b4d43SMatthew Ahrens 	    drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
21475d7b4d43SMatthew Ahrens 	if (err != 0)
21489e69d7d0SLori Alt 		return (err);
21499e69d7d0SLori Alt 
2150a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
21519e69d7d0SLori Alt 
21529e69d7d0SLori Alt 	dmu_tx_hold_write(tx, drrwbr->drr_object,
21539e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length);
21549e69d7d0SLori Alt 	err = dmu_tx_assign(tx, TXG_WAIT);
21553b2aab18SMatthew Ahrens 	if (err != 0) {
21569e69d7d0SLori Alt 		dmu_tx_abort(tx);
21579e69d7d0SLori Alt 		return (err);
21589e69d7d0SLori Alt 	}
2159a2cdcdd2SPaul Dagnelie 	dmu_write(rwa->os, drrwbr->drr_object,
21609e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
21619e69d7d0SLori Alt 	dmu_buf_rele(dbp, FTAG);
21629c3fd121SMatthew Ahrens 
21639c3fd121SMatthew Ahrens 	/* See comment in restore_write. */
21649c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
21659e69d7d0SLori Alt 	dmu_tx_commit(tx);
21669e69d7d0SLori Alt 	return (0);
21679e69d7d0SLori Alt }
21689e69d7d0SLori Alt 
21695d7b4d43SMatthew Ahrens static int
2170a2cdcdd2SPaul Dagnelie receive_write_embedded(struct receive_writer_arg *rwa,
21719c3fd121SMatthew Ahrens     struct drr_write_embedded *drrwe, void *data)
21725d7b4d43SMatthew Ahrens {
21735d7b4d43SMatthew Ahrens 	dmu_tx_t *tx;
21745d7b4d43SMatthew Ahrens 	int err;
21755d7b4d43SMatthew Ahrens 
21769c3fd121SMatthew Ahrens 	if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
21775d7b4d43SMatthew Ahrens 		return (EINVAL);
21785d7b4d43SMatthew Ahrens 
21799c3fd121SMatthew Ahrens 	if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
21805d7b4d43SMatthew Ahrens 		return (EINVAL);
21815d7b4d43SMatthew Ahrens 
21829c3fd121SMatthew Ahrens 	if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
21835d7b4d43SMatthew Ahrens 		return (EINVAL);
21849c3fd121SMatthew Ahrens 	if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
21855d7b4d43SMatthew Ahrens 		return (EINVAL);
21865d7b4d43SMatthew Ahrens 
2187a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
21885d7b4d43SMatthew Ahrens 
21899c3fd121SMatthew Ahrens 	dmu_tx_hold_write(tx, drrwe->drr_object,
21909c3fd121SMatthew Ahrens 	    drrwe->drr_offset, drrwe->drr_length);
21915d7b4d43SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
21925d7b4d43SMatthew Ahrens 	if (err != 0) {
21935d7b4d43SMatthew Ahrens 		dmu_tx_abort(tx);
21945d7b4d43SMatthew Ahrens 		return (err);
21955d7b4d43SMatthew Ahrens 	}
21965d7b4d43SMatthew Ahrens 
21979c3fd121SMatthew Ahrens 	dmu_write_embedded(rwa->os, drrwe->drr_object,
21989c3fd121SMatthew Ahrens 	    drrwe->drr_offset, data, drrwe->drr_etype,
21999c3fd121SMatthew Ahrens 	    drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2200a2cdcdd2SPaul Dagnelie 	    rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
22015d7b4d43SMatthew Ahrens 
22029c3fd121SMatthew Ahrens 	/* See comment in restore_write. */
22039c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
22045d7b4d43SMatthew Ahrens 	dmu_tx_commit(tx);
22055d7b4d43SMatthew Ahrens 	return (0);
22065d7b4d43SMatthew Ahrens }
22075d7b4d43SMatthew Ahrens 
22080a586ceaSMark Shellenbaum static int
2209a2cdcdd2SPaul Dagnelie receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2210a2cdcdd2SPaul Dagnelie     void *data)
22110a586ceaSMark Shellenbaum {
22120a586ceaSMark Shellenbaum 	dmu_tx_t *tx;
22130a586ceaSMark Shellenbaum 	dmu_buf_t *db, *db_spill;
22140a586ceaSMark Shellenbaum 	int err;
22150a586ceaSMark Shellenbaum 
22160a586ceaSMark Shellenbaum 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2217a2cdcdd2SPaul Dagnelie 	    drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2218be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
22190a586ceaSMark Shellenbaum 
2220a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2221be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
22220a586ceaSMark Shellenbaum 
2223a2cdcdd2SPaul Dagnelie 	VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
22240a586ceaSMark Shellenbaum 	if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
22250a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
22260a586ceaSMark Shellenbaum 		return (err);
22270a586ceaSMark Shellenbaum 	}
22280a586ceaSMark Shellenbaum 
2229a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
22300a586ceaSMark Shellenbaum 
22310a586ceaSMark Shellenbaum 	dmu_tx_hold_spill(tx, db->db_object);
22320a586ceaSMark Shellenbaum 
22330a586ceaSMark Shellenbaum 	err = dmu_tx_assign(tx, TXG_WAIT);
22343b2aab18SMatthew Ahrens 	if (err != 0) {
22350a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
22360a586ceaSMark Shellenbaum 		dmu_buf_rele(db_spill, FTAG);
22370a586ceaSMark Shellenbaum 		dmu_tx_abort(tx);
22380a586ceaSMark Shellenbaum 		return (err);
22390a586ceaSMark Shellenbaum 	}
22400a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(db_spill, tx);
22410a586ceaSMark Shellenbaum 
22420a586ceaSMark Shellenbaum 	if (db_spill->db_size < drrs->drr_length)
22430a586ceaSMark Shellenbaum 		VERIFY(0 == dbuf_spill_set_blksz(db_spill,
22440a586ceaSMark Shellenbaum 		    drrs->drr_length, tx));
22450a586ceaSMark Shellenbaum 	bcopy(data, db_spill->db_data, drrs->drr_length);
22460a586ceaSMark Shellenbaum 
22470a586ceaSMark Shellenbaum 	dmu_buf_rele(db, FTAG);
22480a586ceaSMark Shellenbaum 	dmu_buf_rele(db_spill, FTAG);
22490a586ceaSMark Shellenbaum 
22500a586ceaSMark Shellenbaum 	dmu_tx_commit(tx);
22510a586ceaSMark Shellenbaum 	return (0);
22520a586ceaSMark Shellenbaum }
22530a586ceaSMark Shellenbaum 
2254efb80947Sahrens /* ARGSUSED */
2255efb80947Sahrens static int
2256a2cdcdd2SPaul Dagnelie receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2257efb80947Sahrens {
2258efb80947Sahrens 	int err;
2259efb80947Sahrens 
2260efb80947Sahrens 	if (drrf->drr_length != -1ULL &&
2261efb80947Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2262be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2263efb80947Sahrens 
2264a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2265be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2266efb80947Sahrens 
2267a2cdcdd2SPaul Dagnelie 	err = dmu_free_long_range(rwa->os, drrf->drr_object,
2268efb80947Sahrens 	    drrf->drr_offset, drrf->drr_length);
2269a2cdcdd2SPaul Dagnelie 
2270efb80947Sahrens 	return (err);
2271efb80947Sahrens }
2272efb80947Sahrens 
22733b2aab18SMatthew Ahrens /* used to destroy the drc_ds on error */
22743b2aab18SMatthew Ahrens static void
22753b2aab18SMatthew Ahrens dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
22763b2aab18SMatthew Ahrens {
22779c3fd121SMatthew Ahrens 	if (drc->drc_resumable) {
22789c3fd121SMatthew Ahrens 		/* wait for our resume state to be written to disk */
22799c3fd121SMatthew Ahrens 		txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
22809c3fd121SMatthew Ahrens 		dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
22819c3fd121SMatthew Ahrens 	} else {
22829c3fd121SMatthew Ahrens 		char name[MAXNAMELEN];
22839c3fd121SMatthew Ahrens 		dsl_dataset_name(drc->drc_ds, name);
22849c3fd121SMatthew Ahrens 		dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
22859c3fd121SMatthew Ahrens 		(void) dsl_destroy_head(name);
22869c3fd121SMatthew Ahrens 	}
22873b2aab18SMatthew Ahrens }
22883b2aab18SMatthew Ahrens 
228998110f08SMatthew Ahrens static void
2290a2cdcdd2SPaul Dagnelie receive_cksum(struct receive_arg *ra, int len, void *buf)
229198110f08SMatthew Ahrens {
229298110f08SMatthew Ahrens 	if (ra->byteswap) {
229398110f08SMatthew Ahrens 		fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
229498110f08SMatthew Ahrens 	} else {
229598110f08SMatthew Ahrens 		fletcher_4_incremental_native(buf, len, &ra->cksum);
229698110f08SMatthew Ahrens 	}
229798110f08SMatthew Ahrens }
229898110f08SMatthew Ahrens 
229998110f08SMatthew Ahrens /*
2300a2cdcdd2SPaul Dagnelie  * Read the payload into a buffer of size len, and update the current record's
2301a2cdcdd2SPaul Dagnelie  * payload field.
2302a2cdcdd2SPaul Dagnelie  * Allocate ra->next_rrd and read the next record's header into
2303a2cdcdd2SPaul Dagnelie  * ra->next_rrd->header.
230498110f08SMatthew Ahrens  * Verify checksum of payload and next record.
230598110f08SMatthew Ahrens  */
230698110f08SMatthew Ahrens static int
2307a2cdcdd2SPaul Dagnelie receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
230898110f08SMatthew Ahrens {
230998110f08SMatthew Ahrens 	int err;
231098110f08SMatthew Ahrens 
231198110f08SMatthew Ahrens 	if (len != 0) {
2312a2cdcdd2SPaul Dagnelie 		ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
23139c3fd121SMatthew Ahrens 		err = receive_read(ra, len, buf);
231498110f08SMatthew Ahrens 		if (err != 0)
231598110f08SMatthew Ahrens 			return (err);
23169c3fd121SMatthew Ahrens 		receive_cksum(ra, len, buf);
23179c3fd121SMatthew Ahrens 
23189c3fd121SMatthew Ahrens 		/* note: rrd is NULL when reading the begin record's payload */
23199c3fd121SMatthew Ahrens 		if (ra->rrd != NULL) {
23209c3fd121SMatthew Ahrens 			ra->rrd->payload = buf;
23219c3fd121SMatthew Ahrens 			ra->rrd->payload_size = len;
23229c3fd121SMatthew Ahrens 			ra->rrd->bytes_read = ra->bytes_read;
23239c3fd121SMatthew Ahrens 		}
232498110f08SMatthew Ahrens 	}
232598110f08SMatthew Ahrens 
232698110f08SMatthew Ahrens 	ra->prev_cksum = ra->cksum;
232798110f08SMatthew Ahrens 
2328a2cdcdd2SPaul Dagnelie 	ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2329a2cdcdd2SPaul Dagnelie 	err = receive_read(ra, sizeof (ra->next_rrd->header),
2330a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header);
23319c3fd121SMatthew Ahrens 	ra->next_rrd->bytes_read = ra->bytes_read;
2332a2cdcdd2SPaul Dagnelie 	if (err != 0) {
2333a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2334a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
233598110f08SMatthew Ahrens 		return (err);
2336a2cdcdd2SPaul Dagnelie 	}
2337a2cdcdd2SPaul Dagnelie 	if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2338a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2339a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
234098110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
2341a2cdcdd2SPaul Dagnelie 	}
234298110f08SMatthew Ahrens 
234398110f08SMatthew Ahrens 	/*
234498110f08SMatthew Ahrens 	 * Note: checksum is of everything up to but not including the
234598110f08SMatthew Ahrens 	 * checksum itself.
234698110f08SMatthew Ahrens 	 */
234798110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
234898110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2349a2cdcdd2SPaul Dagnelie 	receive_cksum(ra,
235098110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2351a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header);
235298110f08SMatthew Ahrens 
2353a2cdcdd2SPaul Dagnelie 	zio_cksum_t cksum_orig =
2354a2cdcdd2SPaul Dagnelie 	    ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2355a2cdcdd2SPaul Dagnelie 	zio_cksum_t *cksump =
2356a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
235798110f08SMatthew Ahrens 
235898110f08SMatthew Ahrens 	if (ra->byteswap)
2359a2cdcdd2SPaul Dagnelie 		byteswap_record(&ra->next_rrd->header);
236098110f08SMatthew Ahrens 
236198110f08SMatthew Ahrens 	if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2362a2cdcdd2SPaul Dagnelie 	    !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2363a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2364a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
236598110f08SMatthew Ahrens 		return (SET_ERROR(ECKSUM));
2366a2cdcdd2SPaul Dagnelie 	}
236798110f08SMatthew Ahrens 
2368a2cdcdd2SPaul Dagnelie 	receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
236998110f08SMatthew Ahrens 
237098110f08SMatthew Ahrens 	return (0);
237198110f08SMatthew Ahrens }
237298110f08SMatthew Ahrens 
2373*68ecb2ecSPaul Dagnelie static void
2374*68ecb2ecSPaul Dagnelie objlist_create(struct objlist *list)
2375*68ecb2ecSPaul Dagnelie {
2376*68ecb2ecSPaul Dagnelie 	list_create(&list->list, sizeof (struct receive_objnode),
2377*68ecb2ecSPaul Dagnelie 	    offsetof(struct receive_objnode, node));
2378*68ecb2ecSPaul Dagnelie 	list->last_lookup = 0;
2379*68ecb2ecSPaul Dagnelie }
2380*68ecb2ecSPaul Dagnelie 
2381*68ecb2ecSPaul Dagnelie static void
2382*68ecb2ecSPaul Dagnelie objlist_destroy(struct objlist *list)
2383*68ecb2ecSPaul Dagnelie {
2384*68ecb2ecSPaul Dagnelie 	for (struct receive_objnode *n = list_remove_head(&list->list);
2385*68ecb2ecSPaul Dagnelie 	    n != NULL; n = list_remove_head(&list->list)) {
2386*68ecb2ecSPaul Dagnelie 		kmem_free(n, sizeof (*n));
2387*68ecb2ecSPaul Dagnelie 	}
2388*68ecb2ecSPaul Dagnelie 	list_destroy(&list->list);
2389*68ecb2ecSPaul Dagnelie }
2390*68ecb2ecSPaul Dagnelie 
2391*68ecb2ecSPaul Dagnelie /*
2392*68ecb2ecSPaul Dagnelie  * This function looks through the objlist to see if the specified object number
2393*68ecb2ecSPaul Dagnelie  * is contained in the objlist.  In the process, it will remove all object
2394*68ecb2ecSPaul Dagnelie  * numbers in the list that are smaller than the specified object number.  Thus,
2395*68ecb2ecSPaul Dagnelie  * any lookup of an object number smaller than a previously looked up object
2396*68ecb2ecSPaul Dagnelie  * number will always return false; therefore, all lookups should be done in
2397*68ecb2ecSPaul Dagnelie  * ascending order.
2398*68ecb2ecSPaul Dagnelie  */
2399*68ecb2ecSPaul Dagnelie static boolean_t
2400*68ecb2ecSPaul Dagnelie objlist_exists(struct objlist *list, uint64_t object)
2401*68ecb2ecSPaul Dagnelie {
2402*68ecb2ecSPaul Dagnelie 	struct receive_objnode *node = list_head(&list->list);
2403*68ecb2ecSPaul Dagnelie 	ASSERT3U(object, >=, list->last_lookup);
2404*68ecb2ecSPaul Dagnelie 	list->last_lookup = object;
2405*68ecb2ecSPaul Dagnelie 	while (node != NULL && node->object < object) {
2406*68ecb2ecSPaul Dagnelie 		VERIFY3P(node, ==, list_remove_head(&list->list));
2407*68ecb2ecSPaul Dagnelie 		kmem_free(node, sizeof (*node));
2408*68ecb2ecSPaul Dagnelie 		node = list_head(&list->list);
2409*68ecb2ecSPaul Dagnelie 	}
2410*68ecb2ecSPaul Dagnelie 	return (node != NULL && node->object == object);
2411*68ecb2ecSPaul Dagnelie }
2412*68ecb2ecSPaul Dagnelie 
2413*68ecb2ecSPaul Dagnelie /*
2414*68ecb2ecSPaul Dagnelie  * The objlist is a list of object numbers stored in ascending order.  However,
2415*68ecb2ecSPaul Dagnelie  * the insertion of new object numbers does not seek out the correct location to
2416*68ecb2ecSPaul Dagnelie  * store a new object number; instead, it appends it to the list for simplicity.
2417*68ecb2ecSPaul Dagnelie  * Thus, any users must take care to only insert new object numbers in ascending
2418*68ecb2ecSPaul Dagnelie  * order.
2419*68ecb2ecSPaul Dagnelie  */
2420*68ecb2ecSPaul Dagnelie static void
2421*68ecb2ecSPaul Dagnelie objlist_insert(struct objlist *list, uint64_t object)
2422*68ecb2ecSPaul Dagnelie {
2423*68ecb2ecSPaul Dagnelie 	struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
2424*68ecb2ecSPaul Dagnelie 	node->object = object;
2425*68ecb2ecSPaul Dagnelie #ifdef ZFS_DEBUG
2426*68ecb2ecSPaul Dagnelie 	struct receive_objnode *last_object = list_tail(&list->list);
2427*68ecb2ecSPaul Dagnelie 	uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
2428*68ecb2ecSPaul Dagnelie 	ASSERT3U(node->object, >, last_objnum);
2429*68ecb2ecSPaul Dagnelie #endif
2430*68ecb2ecSPaul Dagnelie 	list_insert_tail(&list->list, node);
2431*68ecb2ecSPaul Dagnelie }
2432*68ecb2ecSPaul Dagnelie 
2433a2cdcdd2SPaul Dagnelie /*
2434a2cdcdd2SPaul Dagnelie  * Issue the prefetch reads for any necessary indirect blocks.
2435a2cdcdd2SPaul Dagnelie  *
2436a2cdcdd2SPaul Dagnelie  * We use the object ignore list to tell us whether or not to issue prefetches
2437a2cdcdd2SPaul Dagnelie  * for a given object.  We do this for both correctness (in case the blocksize
2438a2cdcdd2SPaul Dagnelie  * of an object has changed) and performance (if the object doesn't exist, don't
2439a2cdcdd2SPaul Dagnelie  * needlessly try to issue prefetches).  We also trim the list as we go through
2440a2cdcdd2SPaul Dagnelie  * the stream to prevent it from growing to an unbounded size.
2441a2cdcdd2SPaul Dagnelie  *
2442a2cdcdd2SPaul Dagnelie  * The object numbers within will always be in sorted order, and any write
2443a2cdcdd2SPaul Dagnelie  * records we see will also be in sorted order, but they're not sorted with
2444a2cdcdd2SPaul Dagnelie  * respect to each other (i.e. we can get several object records before
2445a2cdcdd2SPaul Dagnelie  * receiving each object's write records).  As a result, once we've reached a
2446a2cdcdd2SPaul Dagnelie  * given object number, we can safely remove any reference to lower object
2447a2cdcdd2SPaul Dagnelie  * numbers in the ignore list. In practice, we receive up to 32 object records
2448a2cdcdd2SPaul Dagnelie  * before receiving write records, so the list can have up to 32 nodes in it.
2449a2cdcdd2SPaul Dagnelie  */
2450a2cdcdd2SPaul Dagnelie /* ARGSUSED */
2451a2cdcdd2SPaul Dagnelie static void
2452a2cdcdd2SPaul Dagnelie receive_read_prefetch(struct receive_arg *ra,
2453a2cdcdd2SPaul Dagnelie     uint64_t object, uint64_t offset, uint64_t length)
2454a2cdcdd2SPaul Dagnelie {
2455*68ecb2ecSPaul Dagnelie 	if (!objlist_exists(&ra->ignore_objlist, object)) {
2456a2cdcdd2SPaul Dagnelie 		dmu_prefetch(ra->os, object, 1, offset, length,
2457a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
2458a2cdcdd2SPaul Dagnelie 	}
2459a2cdcdd2SPaul Dagnelie }
2460a2cdcdd2SPaul Dagnelie 
2461a2cdcdd2SPaul Dagnelie /*
2462a2cdcdd2SPaul Dagnelie  * Read records off the stream, issuing any necessary prefetches.
2463a2cdcdd2SPaul Dagnelie  */
246498110f08SMatthew Ahrens static int
2465a2cdcdd2SPaul Dagnelie receive_read_record(struct receive_arg *ra)
246698110f08SMatthew Ahrens {
246798110f08SMatthew Ahrens 	int err;
246898110f08SMatthew Ahrens 
2469a2cdcdd2SPaul Dagnelie 	switch (ra->rrd->header.drr_type) {
247098110f08SMatthew Ahrens 	case DRR_OBJECT:
247198110f08SMatthew Ahrens 	{
2472a2cdcdd2SPaul Dagnelie 		struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2473a2cdcdd2SPaul Dagnelie 		uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2474a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(size, KM_SLEEP);
2475a2cdcdd2SPaul Dagnelie 		dmu_object_info_t doi;
2476a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, size, buf);
2477a2cdcdd2SPaul Dagnelie 		if (err != 0) {
2478a2cdcdd2SPaul Dagnelie 			kmem_free(buf, size);
247998110f08SMatthew Ahrens 			return (err);
2480a2cdcdd2SPaul Dagnelie 		}
2481a2cdcdd2SPaul Dagnelie 		err = dmu_object_info(ra->os, drro->drr_object, &doi);
2482a2cdcdd2SPaul Dagnelie 		/*
2483a2cdcdd2SPaul Dagnelie 		 * See receive_read_prefetch for an explanation why we're
2484a2cdcdd2SPaul Dagnelie 		 * storing this object in the ignore_obj_list.
2485a2cdcdd2SPaul Dagnelie 		 */
2486a2cdcdd2SPaul Dagnelie 		if (err == ENOENT ||
2487a2cdcdd2SPaul Dagnelie 		    (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2488*68ecb2ecSPaul Dagnelie 			objlist_insert(&ra->ignore_objlist, drro->drr_object);
2489a2cdcdd2SPaul Dagnelie 			err = 0;
2490a2cdcdd2SPaul Dagnelie 		}
2491a2cdcdd2SPaul Dagnelie 		return (err);
249298110f08SMatthew Ahrens 	}
249398110f08SMatthew Ahrens 	case DRR_FREEOBJECTS:
249498110f08SMatthew Ahrens 	{
2495a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2496a2cdcdd2SPaul Dagnelie 		return (err);
249798110f08SMatthew Ahrens 	}
249898110f08SMatthew Ahrens 	case DRR_WRITE:
249998110f08SMatthew Ahrens 	{
2500a2cdcdd2SPaul Dagnelie 		struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
250198110f08SMatthew Ahrens 		arc_buf_t *abuf = arc_loan_buf(dmu_objset_spa(ra->os),
250298110f08SMatthew Ahrens 		    drrw->drr_length);
250398110f08SMatthew Ahrens 
2504a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra,
250598110f08SMatthew Ahrens 		    drrw->drr_length, abuf->b_data);
2506a2cdcdd2SPaul Dagnelie 		if (err != 0) {
250798110f08SMatthew Ahrens 			dmu_return_arcbuf(abuf);
2508a2cdcdd2SPaul Dagnelie 			return (err);
2509a2cdcdd2SPaul Dagnelie 		}
2510a2cdcdd2SPaul Dagnelie 		ra->rrd->write_buf = abuf;
2511a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2512a2cdcdd2SPaul Dagnelie 		    drrw->drr_length);
251398110f08SMatthew Ahrens 		return (err);
251498110f08SMatthew Ahrens 	}
251598110f08SMatthew Ahrens 	case DRR_WRITE_BYREF:
251698110f08SMatthew Ahrens 	{
2517a2cdcdd2SPaul Dagnelie 		struct drr_write_byref *drrwb =
2518a2cdcdd2SPaul Dagnelie 		    &ra->rrd->header.drr_u.drr_write_byref;
2519a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2520a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2521a2cdcdd2SPaul Dagnelie 		    drrwb->drr_length);
2522a2cdcdd2SPaul Dagnelie 		return (err);
252398110f08SMatthew Ahrens 	}
252498110f08SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
252598110f08SMatthew Ahrens 	{
252698110f08SMatthew Ahrens 		struct drr_write_embedded *drrwe =
2527a2cdcdd2SPaul Dagnelie 		    &ra->rrd->header.drr_u.drr_write_embedded;
2528a2cdcdd2SPaul Dagnelie 		uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2529a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(size, KM_SLEEP);
2530a2cdcdd2SPaul Dagnelie 
2531a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, size, buf);
2532a2cdcdd2SPaul Dagnelie 		if (err != 0) {
2533a2cdcdd2SPaul Dagnelie 			kmem_free(buf, size);
253498110f08SMatthew Ahrens 			return (err);
2535a2cdcdd2SPaul Dagnelie 		}
2536a2cdcdd2SPaul Dagnelie 
2537a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2538a2cdcdd2SPaul Dagnelie 		    drrwe->drr_length);
2539a2cdcdd2SPaul Dagnelie 		return (err);
254098110f08SMatthew Ahrens 	}
254198110f08SMatthew Ahrens 	case DRR_FREE:
254298110f08SMatthew Ahrens 	{
2543a2cdcdd2SPaul Dagnelie 		/*
2544a2cdcdd2SPaul Dagnelie 		 * It might be beneficial to prefetch indirect blocks here, but
2545a2cdcdd2SPaul Dagnelie 		 * we don't really have the data to decide for sure.
2546a2cdcdd2SPaul Dagnelie 		 */
2547a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2548a2cdcdd2SPaul Dagnelie 		return (err);
254998110f08SMatthew Ahrens 	}
255098110f08SMatthew Ahrens 	case DRR_END:
255198110f08SMatthew Ahrens 	{
2552a2cdcdd2SPaul Dagnelie 		struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
255398110f08SMatthew Ahrens 		if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
25549c3fd121SMatthew Ahrens 			return (SET_ERROR(ECKSUM));
255598110f08SMatthew Ahrens 		return (0);
255698110f08SMatthew Ahrens 	}
255798110f08SMatthew Ahrens 	case DRR_SPILL:
255898110f08SMatthew Ahrens 	{
2559a2cdcdd2SPaul Dagnelie 		struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2560a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2561a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2562a2cdcdd2SPaul Dagnelie 		    buf);
256398110f08SMatthew Ahrens 		if (err != 0)
2564a2cdcdd2SPaul Dagnelie 			kmem_free(buf, drrs->drr_length);
2565a2cdcdd2SPaul Dagnelie 		return (err);
256698110f08SMatthew Ahrens 	}
256798110f08SMatthew Ahrens 	default:
256898110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
256998110f08SMatthew Ahrens 	}
257098110f08SMatthew Ahrens }
257198110f08SMatthew Ahrens 
25723cb34c60Sahrens /*
2573a2cdcdd2SPaul Dagnelie  * Commit the records to the pool.
2574a2cdcdd2SPaul Dagnelie  */
2575a2cdcdd2SPaul Dagnelie static int
2576a2cdcdd2SPaul Dagnelie receive_process_record(struct receive_writer_arg *rwa,
2577a2cdcdd2SPaul Dagnelie     struct receive_record_arg *rrd)
2578a2cdcdd2SPaul Dagnelie {
2579a2cdcdd2SPaul Dagnelie 	int err;
2580a2cdcdd2SPaul Dagnelie 
25819c3fd121SMatthew Ahrens 	/* Processing in order, therefore bytes_read should be increasing. */
25829c3fd121SMatthew Ahrens 	ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
25839c3fd121SMatthew Ahrens 	rwa->bytes_read = rrd->bytes_read;
25849c3fd121SMatthew Ahrens 
2585a2cdcdd2SPaul Dagnelie 	switch (rrd->header.drr_type) {
2586a2cdcdd2SPaul Dagnelie 	case DRR_OBJECT:
2587a2cdcdd2SPaul Dagnelie 	{
2588a2cdcdd2SPaul Dagnelie 		struct drr_object *drro = &rrd->header.drr_u.drr_object;
2589a2cdcdd2SPaul Dagnelie 		err = receive_object(rwa, drro, rrd->payload);
2590a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2591a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2592a2cdcdd2SPaul Dagnelie 		return (err);
2593a2cdcdd2SPaul Dagnelie 	}
2594a2cdcdd2SPaul Dagnelie 	case DRR_FREEOBJECTS:
2595a2cdcdd2SPaul Dagnelie 	{
2596a2cdcdd2SPaul Dagnelie 		struct drr_freeobjects *drrfo =
2597a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_freeobjects;
2598a2cdcdd2SPaul Dagnelie 		return (receive_freeobjects(rwa, drrfo));
2599a2cdcdd2SPaul Dagnelie 	}
2600a2cdcdd2SPaul Dagnelie 	case DRR_WRITE:
2601a2cdcdd2SPaul Dagnelie 	{
2602a2cdcdd2SPaul Dagnelie 		struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2603a2cdcdd2SPaul Dagnelie 		err = receive_write(rwa, drrw, rrd->write_buf);
2604a2cdcdd2SPaul Dagnelie 		/* if receive_write() is successful, it consumes the arc_buf */
2605a2cdcdd2SPaul Dagnelie 		if (err != 0)
2606a2cdcdd2SPaul Dagnelie 			dmu_return_arcbuf(rrd->write_buf);
2607a2cdcdd2SPaul Dagnelie 		rrd->write_buf = NULL;
2608a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2609a2cdcdd2SPaul Dagnelie 		return (err);
2610a2cdcdd2SPaul Dagnelie 	}
2611a2cdcdd2SPaul Dagnelie 	case DRR_WRITE_BYREF:
2612a2cdcdd2SPaul Dagnelie 	{
2613a2cdcdd2SPaul Dagnelie 		struct drr_write_byref *drrwbr =
2614a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_write_byref;
2615a2cdcdd2SPaul Dagnelie 		return (receive_write_byref(rwa, drrwbr));
2616a2cdcdd2SPaul Dagnelie 	}
2617a2cdcdd2SPaul Dagnelie 	case DRR_WRITE_EMBEDDED:
2618a2cdcdd2SPaul Dagnelie 	{
2619a2cdcdd2SPaul Dagnelie 		struct drr_write_embedded *drrwe =
2620a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_write_embedded;
2621a2cdcdd2SPaul Dagnelie 		err = receive_write_embedded(rwa, drrwe, rrd->payload);
2622a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2623a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2624a2cdcdd2SPaul Dagnelie 		return (err);
2625a2cdcdd2SPaul Dagnelie 	}
2626a2cdcdd2SPaul Dagnelie 	case DRR_FREE:
2627a2cdcdd2SPaul Dagnelie 	{
2628a2cdcdd2SPaul Dagnelie 		struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2629a2cdcdd2SPaul Dagnelie 		return (receive_free(rwa, drrf));
2630a2cdcdd2SPaul Dagnelie 	}
2631a2cdcdd2SPaul Dagnelie 	case DRR_SPILL:
2632a2cdcdd2SPaul Dagnelie 	{
2633a2cdcdd2SPaul Dagnelie 		struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2634a2cdcdd2SPaul Dagnelie 		err = receive_spill(rwa, drrs, rrd->payload);
2635a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2636a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2637a2cdcdd2SPaul Dagnelie 		return (err);
2638a2cdcdd2SPaul Dagnelie 	}
2639a2cdcdd2SPaul Dagnelie 	default:
2640a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINVAL));
2641a2cdcdd2SPaul Dagnelie 	}
2642a2cdcdd2SPaul Dagnelie }
2643a2cdcdd2SPaul Dagnelie 
2644a2cdcdd2SPaul Dagnelie /*
2645a2cdcdd2SPaul Dagnelie  * dmu_recv_stream's worker thread; pull records off the queue, and then call
2646a2cdcdd2SPaul Dagnelie  * receive_process_record  When we're done, signal the main thread and exit.
2647a2cdcdd2SPaul Dagnelie  */
2648a2cdcdd2SPaul Dagnelie static void
2649a2cdcdd2SPaul Dagnelie receive_writer_thread(void *arg)
2650a2cdcdd2SPaul Dagnelie {
2651a2cdcdd2SPaul Dagnelie 	struct receive_writer_arg *rwa = arg;
2652a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *rrd;
2653a2cdcdd2SPaul Dagnelie 	for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2654a2cdcdd2SPaul Dagnelie 	    rrd = bqueue_dequeue(&rwa->q)) {
2655a2cdcdd2SPaul Dagnelie 		/*
2656a2cdcdd2SPaul Dagnelie 		 * If there's an error, the main thread will stop putting things
2657a2cdcdd2SPaul Dagnelie 		 * on the queue, but we need to clear everything in it before we
2658a2cdcdd2SPaul Dagnelie 		 * can exit.
2659a2cdcdd2SPaul Dagnelie 		 */
2660a2cdcdd2SPaul Dagnelie 		if (rwa->err == 0) {
2661a2cdcdd2SPaul Dagnelie 			rwa->err = receive_process_record(rwa, rrd);
2662a2cdcdd2SPaul Dagnelie 		} else if (rrd->write_buf != NULL) {
2663a2cdcdd2SPaul Dagnelie 			dmu_return_arcbuf(rrd->write_buf);
2664a2cdcdd2SPaul Dagnelie 			rrd->write_buf = NULL;
2665a2cdcdd2SPaul Dagnelie 			rrd->payload = NULL;
2666a2cdcdd2SPaul Dagnelie 		} else if (rrd->payload != NULL) {
2667a2cdcdd2SPaul Dagnelie 			kmem_free(rrd->payload, rrd->payload_size);
2668a2cdcdd2SPaul Dagnelie 			rrd->payload = NULL;
2669a2cdcdd2SPaul Dagnelie 		}
2670a2cdcdd2SPaul Dagnelie 		kmem_free(rrd, sizeof (*rrd));
2671a2cdcdd2SPaul Dagnelie 	}
2672a2cdcdd2SPaul Dagnelie 	kmem_free(rrd, sizeof (*rrd));
2673a2cdcdd2SPaul Dagnelie 	mutex_enter(&rwa->mutex);
2674a2cdcdd2SPaul Dagnelie 	rwa->done = B_TRUE;
2675a2cdcdd2SPaul Dagnelie 	cv_signal(&rwa->cv);
2676a2cdcdd2SPaul Dagnelie 	mutex_exit(&rwa->mutex);
2677a2cdcdd2SPaul Dagnelie }
2678a2cdcdd2SPaul Dagnelie 
26799c3fd121SMatthew Ahrens static int
26809c3fd121SMatthew Ahrens resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
26819c3fd121SMatthew Ahrens {
26829c3fd121SMatthew Ahrens 	uint64_t val;
26839c3fd121SMatthew Ahrens 	objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
26849c3fd121SMatthew Ahrens 	uint64_t dsobj = dmu_objset_id(ra->os);
26859c3fd121SMatthew Ahrens 	uint64_t resume_obj, resume_off;
26869c3fd121SMatthew Ahrens 
26879c3fd121SMatthew Ahrens 	if (nvlist_lookup_uint64(begin_nvl,
26889c3fd121SMatthew Ahrens 	    "resume_object", &resume_obj) != 0 ||
26899c3fd121SMatthew Ahrens 	    nvlist_lookup_uint64(begin_nvl,
26909c3fd121SMatthew Ahrens 	    "resume_offset", &resume_off) != 0) {
26919c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
26929c3fd121SMatthew Ahrens 	}
26939c3fd121SMatthew Ahrens 	VERIFY0(zap_lookup(mos, dsobj,
26949c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
26959c3fd121SMatthew Ahrens 	if (resume_obj != val)
26969c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
26979c3fd121SMatthew Ahrens 	VERIFY0(zap_lookup(mos, dsobj,
26989c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
26999c3fd121SMatthew Ahrens 	if (resume_off != val)
27009c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
27019c3fd121SMatthew Ahrens 
27029c3fd121SMatthew Ahrens 	return (0);
27039c3fd121SMatthew Ahrens }
27049c3fd121SMatthew Ahrens 
2705a2cdcdd2SPaul Dagnelie /*
2706a2cdcdd2SPaul Dagnelie  * Read in the stream's records, one by one, and apply them to the pool.  There
2707a2cdcdd2SPaul Dagnelie  * are two threads involved; the thread that calls this function will spin up a
2708a2cdcdd2SPaul Dagnelie  * worker thread, read the records off the stream one by one, and issue
2709a2cdcdd2SPaul Dagnelie  * prefetches for any necessary indirect blocks.  It will then push the records
2710a2cdcdd2SPaul Dagnelie  * onto an internal blocking queue.  The worker thread will pull the records off
2711a2cdcdd2SPaul Dagnelie  * the queue, and actually write the data into the DMU.  This way, the worker
2712a2cdcdd2SPaul Dagnelie  * thread doesn't have to wait for reads to complete, since everything it needs
2713a2cdcdd2SPaul Dagnelie  * (the indirect blocks) will be prefetched.
2714a2cdcdd2SPaul Dagnelie  *
27153cb34c60Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
27163cb34c60Sahrens  */
2717efb80947Sahrens int
2718c99e4bdcSChris Kirby dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
2719c99e4bdcSChris Kirby     int cleanup_fd, uint64_t *action_handlep)
2720efb80947Sahrens {
272198110f08SMatthew Ahrens 	int err = 0;
2722a2cdcdd2SPaul Dagnelie 	struct receive_arg ra = { 0 };
2723a2cdcdd2SPaul Dagnelie 	struct receive_writer_arg rwa = { 0 };
27249e69d7d0SLori Alt 	int featureflags;
27259c3fd121SMatthew Ahrens 	nvlist_t *begin_nvl = NULL;
2726efb80947Sahrens 
27273b2aab18SMatthew Ahrens 	ra.byteswap = drc->drc_byteswap;
27283b2aab18SMatthew Ahrens 	ra.cksum = drc->drc_cksum;
27293cb34c60Sahrens 	ra.vp = vp;
27303cb34c60Sahrens 	ra.voff = *voffp;
27319c3fd121SMatthew Ahrens 
27329c3fd121SMatthew Ahrens 	if (dsl_dataset_is_zapified(drc->drc_ds)) {
27339c3fd121SMatthew Ahrens 		(void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
27349c3fd121SMatthew Ahrens 		    drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
27359c3fd121SMatthew Ahrens 		    sizeof (ra.bytes_read), 1, &ra.bytes_read);
27369c3fd121SMatthew Ahrens 	}
27379c3fd121SMatthew Ahrens 
2738*68ecb2ecSPaul Dagnelie 	objlist_create(&ra.ignore_objlist);
2739efb80947Sahrens 
27403cb34c60Sahrens 	/* these were verified in dmu_recv_begin */
27413b2aab18SMatthew Ahrens 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
27429e69d7d0SLori Alt 	    DMU_SUBSTREAM);
27433b2aab18SMatthew Ahrens 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2744efb80947Sahrens 
2745efb80947Sahrens 	/*
2746efb80947Sahrens 	 * Open the objset we are modifying.
2747efb80947Sahrens 	 */
274898110f08SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
2749efb80947Sahrens 
2750c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2751efb80947Sahrens 
27529e69d7d0SLori Alt 	featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
27539e69d7d0SLori Alt 
27549e69d7d0SLori Alt 	/* if this stream is dedup'ed, set up the avl tree for guid mapping */
27559e69d7d0SLori Alt 	if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2756a7f53a56SChris Kirby 		minor_t minor;
2757a7f53a56SChris Kirby 
2758c99e4bdcSChris Kirby 		if (cleanup_fd == -1) {
2759be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EBADF);
2760c99e4bdcSChris Kirby 			goto out;
2761c99e4bdcSChris Kirby 		}
2762a7f53a56SChris Kirby 		ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
27633b2aab18SMatthew Ahrens 		if (ra.err != 0) {
2764a7f53a56SChris Kirby 			cleanup_fd = -1;
2765a7f53a56SChris Kirby 			goto out;
2766a7f53a56SChris Kirby 		}
2767a7f53a56SChris Kirby 
2768c99e4bdcSChris Kirby 		if (*action_handlep == 0) {
2769a2cdcdd2SPaul Dagnelie 			rwa.guid_to_ds_map =
2770c99e4bdcSChris Kirby 			    kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2771a2cdcdd2SPaul Dagnelie 			avl_create(rwa.guid_to_ds_map, guid_compare,
2772c99e4bdcSChris Kirby 			    sizeof (guid_map_entry_t),
2773c99e4bdcSChris Kirby 			    offsetof(guid_map_entry_t, avlnode));
277498110f08SMatthew Ahrens 			err = zfs_onexit_add_cb(minor,
2775a2cdcdd2SPaul Dagnelie 			    free_guid_map_onexit, rwa.guid_to_ds_map,
2776c99e4bdcSChris Kirby 			    action_handlep);
27773b2aab18SMatthew Ahrens 			if (ra.err != 0)
2778c99e4bdcSChris Kirby 				goto out;
2779c99e4bdcSChris Kirby 		} else {
278098110f08SMatthew Ahrens 			err = zfs_onexit_cb_data(minor, *action_handlep,
2781a2cdcdd2SPaul Dagnelie 			    (void **)&rwa.guid_to_ds_map);
27823b2aab18SMatthew Ahrens 			if (ra.err != 0)
2783c99e4bdcSChris Kirby 				goto out;
2784c99e4bdcSChris Kirby 		}
2785ec5cf9d5SAlexander Stetsenko 
2786a2cdcdd2SPaul Dagnelie 		drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
27879e69d7d0SLori Alt 	}
27889e69d7d0SLori Alt 
27899c3fd121SMatthew Ahrens 	uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
27909c3fd121SMatthew Ahrens 	void *payload = NULL;
27919c3fd121SMatthew Ahrens 	if (payloadlen != 0)
27929c3fd121SMatthew Ahrens 		payload = kmem_alloc(payloadlen, KM_SLEEP);
27939c3fd121SMatthew Ahrens 
27949c3fd121SMatthew Ahrens 	err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
27959c3fd121SMatthew Ahrens 	if (err != 0) {
27969c3fd121SMatthew Ahrens 		if (payloadlen != 0)
27979c3fd121SMatthew Ahrens 			kmem_free(payload, payloadlen);
279898110f08SMatthew Ahrens 		goto out;
27999c3fd121SMatthew Ahrens 	}
28009c3fd121SMatthew Ahrens 	if (payloadlen != 0) {
28019c3fd121SMatthew Ahrens 		err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
28029c3fd121SMatthew Ahrens 		kmem_free(payload, payloadlen);
28039c3fd121SMatthew Ahrens 		if (err != 0)
28049c3fd121SMatthew Ahrens 			goto out;
28059c3fd121SMatthew Ahrens 	}
28069c3fd121SMatthew Ahrens 
28079c3fd121SMatthew Ahrens 	if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
28089c3fd121SMatthew Ahrens 		err = resume_check(&ra, begin_nvl);
28099c3fd121SMatthew Ahrens 		if (err != 0)
28109c3fd121SMatthew Ahrens 			goto out;
28119c3fd121SMatthew Ahrens 	}
281298110f08SMatthew Ahrens 
2813a2cdcdd2SPaul Dagnelie 	(void) bqueue_init(&rwa.q, zfs_recv_queue_length,
2814a2cdcdd2SPaul Dagnelie 	    offsetof(struct receive_record_arg, node));
2815a2cdcdd2SPaul Dagnelie 	cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
2816a2cdcdd2SPaul Dagnelie 	mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
2817a2cdcdd2SPaul Dagnelie 	rwa.os = ra.os;
2818a2cdcdd2SPaul Dagnelie 	rwa.byteswap = drc->drc_byteswap;
28199c3fd121SMatthew Ahrens 	rwa.resumable = drc->drc_resumable;
2820a2cdcdd2SPaul Dagnelie 
2821a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, curproc,
2822a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
2823a2cdcdd2SPaul Dagnelie 	/*
2824a2cdcdd2SPaul Dagnelie 	 * We're reading rwa.err without locks, which is safe since we are the
2825a2cdcdd2SPaul Dagnelie 	 * only reader, and the worker thread is the only writer.  It's ok if we
2826a2cdcdd2SPaul Dagnelie 	 * miss a write for an iteration or two of the loop, since the writer
2827a2cdcdd2SPaul Dagnelie 	 * thread will keep freeing records we send it until we send it an eos
2828a2cdcdd2SPaul Dagnelie 	 * marker.
2829a2cdcdd2SPaul Dagnelie 	 *
2830a2cdcdd2SPaul Dagnelie 	 * We can leave this loop in 3 ways:  First, if rwa.err is
2831a2cdcdd2SPaul Dagnelie 	 * non-zero.  In that case, the writer thread will free the rrd we just
2832a2cdcdd2SPaul Dagnelie 	 * pushed.  Second, if  we're interrupted; in that case, either it's the
2833a2cdcdd2SPaul Dagnelie 	 * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
2834a2cdcdd2SPaul Dagnelie 	 * has been handed off to the writer thread who will free it.  Finally,
2835a2cdcdd2SPaul Dagnelie 	 * if receive_read_record fails or we're at the end of the stream, then
2836a2cdcdd2SPaul Dagnelie 	 * we free ra.rrd and exit.
2837a2cdcdd2SPaul Dagnelie 	 */
2838a2cdcdd2SPaul Dagnelie 	while (rwa.err == 0) {
2839efb80947Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
284098110f08SMatthew Ahrens 			err = SET_ERROR(EINTR);
284198110f08SMatthew Ahrens 			break;
2842efb80947Sahrens 		}
2843efb80947Sahrens 
2844a2cdcdd2SPaul Dagnelie 		ASSERT3P(ra.rrd, ==, NULL);
2845a2cdcdd2SPaul Dagnelie 		ra.rrd = ra.next_rrd;
2846a2cdcdd2SPaul Dagnelie 		ra.next_rrd = NULL;
2847a2cdcdd2SPaul Dagnelie 		/* Allocates and loads header into ra.next_rrd */
2848a2cdcdd2SPaul Dagnelie 		err = receive_read_record(&ra);
2849efb80947Sahrens 
2850a2cdcdd2SPaul Dagnelie 		if (ra.rrd->header.drr_type == DRR_END || err != 0) {
2851a2cdcdd2SPaul Dagnelie 			kmem_free(ra.rrd, sizeof (*ra.rrd));
2852a2cdcdd2SPaul Dagnelie 			ra.rrd = NULL;
28530a586ceaSMark Shellenbaum 			break;
2854a2cdcdd2SPaul Dagnelie 		}
2855a2cdcdd2SPaul Dagnelie 
2856a2cdcdd2SPaul Dagnelie 		bqueue_enqueue(&rwa.q, ra.rrd,
2857a2cdcdd2SPaul Dagnelie 		    sizeof (struct receive_record_arg) + ra.rrd->payload_size);
2858a2cdcdd2SPaul Dagnelie 		ra.rrd = NULL;
2859a2cdcdd2SPaul Dagnelie 	}
2860a2cdcdd2SPaul Dagnelie 	if (ra.next_rrd == NULL)
2861a2cdcdd2SPaul Dagnelie 		ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
2862a2cdcdd2SPaul Dagnelie 	ra.next_rrd->eos_marker = B_TRUE;
2863a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
2864a2cdcdd2SPaul Dagnelie 
2865a2cdcdd2SPaul Dagnelie 	mutex_enter(&rwa.mutex);
2866a2cdcdd2SPaul Dagnelie 	while (!rwa.done) {
2867a2cdcdd2SPaul Dagnelie 		cv_wait(&rwa.cv, &rwa.mutex);
2868efb80947Sahrens 	}
2869a2cdcdd2SPaul Dagnelie 	mutex_exit(&rwa.mutex);
2870a2cdcdd2SPaul Dagnelie 
2871a2cdcdd2SPaul Dagnelie 	cv_destroy(&rwa.cv);
2872a2cdcdd2SPaul Dagnelie 	mutex_destroy(&rwa.mutex);
2873a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&rwa.q);
2874a2cdcdd2SPaul Dagnelie 	if (err == 0)
2875a2cdcdd2SPaul Dagnelie 		err = rwa.err;
2876efb80947Sahrens 
2877efb80947Sahrens out:
28789c3fd121SMatthew Ahrens 	nvlist_free(begin_nvl);
2879a7f53a56SChris Kirby 	if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2880a7f53a56SChris Kirby 		zfs_onexit_fd_rele(cleanup_fd);
2881a7f53a56SChris Kirby 
288298110f08SMatthew Ahrens 	if (err != 0) {
2883efb80947Sahrens 		/*
28849c3fd121SMatthew Ahrens 		 * Clean up references. If receive is not resumable,
28859c3fd121SMatthew Ahrens 		 * destroy what we created, so we don't leave it in
28869c3fd121SMatthew Ahrens 		 * the inconsistent state.
2887efb80947Sahrens 		 */
28883b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
2889efb80947Sahrens 	}
2890efb80947Sahrens 
28913cb34c60Sahrens 	*voffp = ra.voff;
2892*68ecb2ecSPaul Dagnelie 	objlist_destroy(&ra.ignore_objlist);
289398110f08SMatthew Ahrens 	return (err);
2894efb80947Sahrens }
2895f18faf3fSek 
28963cb34c60Sahrens static int
28973b2aab18SMatthew Ahrens dmu_recv_end_check(void *arg, dmu_tx_t *tx)
28983cb34c60Sahrens {
28993b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
29003b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
29013b2aab18SMatthew Ahrens 	int error;
29023b2aab18SMatthew Ahrens 
29033b2aab18SMatthew Ahrens 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
29043cb34c60Sahrens 
29053b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
29063b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
29073b2aab18SMatthew Ahrens 
29083b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
29093b2aab18SMatthew Ahrens 		if (error != 0)
29103b2aab18SMatthew Ahrens 			return (error);
291134f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
291234f2f8cfSMatthew Ahrens 			/*
291334f2f8cfSMatthew Ahrens 			 * We will destroy any snapshots in tofs (i.e. before
291434f2f8cfSMatthew Ahrens 			 * origin_head) that are after the origin (which is
291534f2f8cfSMatthew Ahrens 			 * the snap before drc_ds, because drc_ds can not
291634f2f8cfSMatthew Ahrens 			 * have any snaps of its own).
291734f2f8cfSMatthew Ahrens 			 */
2918c1379625SJustin T. Gibbs 			uint64_t obj;
2919c1379625SJustin T. Gibbs 
2920c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2921c1379625SJustin T. Gibbs 			while (obj !=
2922c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
292334f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
292434f2f8cfSMatthew Ahrens 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
292534f2f8cfSMatthew Ahrens 				    &snap);
292634f2f8cfSMatthew Ahrens 				if (error != 0)
2927beddaa9cSAndriy Gapon 					break;
292834f2f8cfSMatthew Ahrens 				if (snap->ds_dir != origin_head->ds_dir)
292934f2f8cfSMatthew Ahrens 					error = SET_ERROR(EINVAL);
293034f2f8cfSMatthew Ahrens 				if (error == 0)  {
293134f2f8cfSMatthew Ahrens 					error = dsl_destroy_snapshot_check_impl(
293234f2f8cfSMatthew Ahrens 					    snap, B_FALSE);
293334f2f8cfSMatthew Ahrens 				}
2934c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
293534f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
293634f2f8cfSMatthew Ahrens 				if (error != 0)
2937beddaa9cSAndriy Gapon 					break;
2938beddaa9cSAndriy Gapon 			}
2939beddaa9cSAndriy Gapon 			if (error != 0) {
2940beddaa9cSAndriy Gapon 				dsl_dataset_rele(origin_head, FTAG);
2941beddaa9cSAndriy Gapon 				return (error);
294234f2f8cfSMatthew Ahrens 			}
294334f2f8cfSMatthew Ahrens 		}
29443b2aab18SMatthew Ahrens 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
294591948b51SKeith M Wesolowski 		    origin_head, drc->drc_force, drc->drc_owner, tx);
29463b2aab18SMatthew Ahrens 		if (error != 0) {
29473b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin_head, FTAG);
29483b2aab18SMatthew Ahrens 			return (error);
29493b2aab18SMatthew Ahrens 		}
29503b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(origin_head,
2951a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
29523b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
29533b2aab18SMatthew Ahrens 		if (error != 0)
29543b2aab18SMatthew Ahrens 			return (error);
29553b2aab18SMatthew Ahrens 
29563b2aab18SMatthew Ahrens 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
29573b2aab18SMatthew Ahrens 	} else {
29583b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
2959a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
29603b2aab18SMatthew Ahrens 	}
29613b2aab18SMatthew Ahrens 	return (error);
29623cb34c60Sahrens }
29633cb34c60Sahrens 
29643cb34c60Sahrens static void
29653b2aab18SMatthew Ahrens dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
29663cb34c60Sahrens {
29673b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
29683b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
29693b2aab18SMatthew Ahrens 
29703b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
29713b2aab18SMatthew Ahrens 	    tx, "snap=%s", drc->drc_tosnap);
29723b2aab18SMatthew Ahrens 
29733b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
29743b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
29753b2aab18SMatthew Ahrens 
29763b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
29773b2aab18SMatthew Ahrens 		    &origin_head));
297834f2f8cfSMatthew Ahrens 
297934f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
298034f2f8cfSMatthew Ahrens 			/*
298134f2f8cfSMatthew Ahrens 			 * Destroy any snapshots of drc_tofs (origin_head)
298234f2f8cfSMatthew Ahrens 			 * after the origin (the snap before drc_ds).
298334f2f8cfSMatthew Ahrens 			 */
2984c1379625SJustin T. Gibbs 			uint64_t obj;
2985c1379625SJustin T. Gibbs 
2986c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2987c1379625SJustin T. Gibbs 			while (obj !=
2988c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
298934f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
299034f2f8cfSMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
299134f2f8cfSMatthew Ahrens 				    &snap));
299234f2f8cfSMatthew Ahrens 				ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
2993c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
299434f2f8cfSMatthew Ahrens 				dsl_destroy_snapshot_sync_impl(snap,
299534f2f8cfSMatthew Ahrens 				    B_FALSE, tx);
299634f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
299734f2f8cfSMatthew Ahrens 			}
299834f2f8cfSMatthew Ahrens 		}
299934f2f8cfSMatthew Ahrens 		VERIFY3P(drc->drc_ds->ds_prev, ==,
300034f2f8cfSMatthew Ahrens 		    origin_head->ds_prev);
300134f2f8cfSMatthew Ahrens 
30023b2aab18SMatthew Ahrens 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
30033b2aab18SMatthew Ahrens 		    origin_head, tx);
30043b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(origin_head,
30053b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
30063b2aab18SMatthew Ahrens 
30073b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
30083b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3009c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
30103b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
3011c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
30123b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_toguid;
3013c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
30143b2aab18SMatthew Ahrens 		    ~DS_FLAG_INCONSISTENT;
30153b2aab18SMatthew Ahrens 
30163b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3017c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_flags &=
3018c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
30193b2aab18SMatthew Ahrens 
30203b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
30213b2aab18SMatthew Ahrens 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
302291948b51SKeith M Wesolowski 
302391948b51SKeith M Wesolowski 		if (drc->drc_owner != NULL)
302491948b51SKeith M Wesolowski 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
30253b2aab18SMatthew Ahrens 	} else {
30263b2aab18SMatthew Ahrens 		dsl_dataset_t *ds = drc->drc_ds;
30273cb34c60Sahrens 
30283b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
30293cb34c60Sahrens 
30303b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
30313b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3032c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
30333b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
3034c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_guid =
3035c1379625SJustin T. Gibbs 		    drc->drc_drrb->drr_toguid;
3036c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3037c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
30383cb34c60Sahrens 
30393b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
3040c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
30419c3fd121SMatthew Ahrens 		if (dsl_dataset_has_resume_receive_state(ds)) {
30429c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30439c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_FROMGUID, tx);
30449c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30459c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_OBJECT, tx);
30469c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30479c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_OFFSET, tx);
30489c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30499c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_BYTES, tx);
30509c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30519c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_TOGUID, tx);
30529c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30539c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_TONAME, tx);
30549c3fd121SMatthew Ahrens 		}
30553b2aab18SMatthew Ahrens 	}
3056c1379625SJustin T. Gibbs 	drc->drc_newsnapobj = dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
30573b2aab18SMatthew Ahrens 	/*
30583b2aab18SMatthew Ahrens 	 * Release the hold from dmu_recv_begin.  This must be done before
30593b2aab18SMatthew Ahrens 	 * we return to open context, so that when we free the dataset's dnode,
30603b2aab18SMatthew Ahrens 	 * we can evict its bonus buffer.
30613b2aab18SMatthew Ahrens 	 */
30623b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
30633b2aab18SMatthew Ahrens 	drc->drc_ds = NULL;
30643cb34c60Sahrens }
30653cb34c60Sahrens 
3066ec5cf9d5SAlexander Stetsenko static int
30673b2aab18SMatthew Ahrens add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3068ec5cf9d5SAlexander Stetsenko {
30693b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
3070ec5cf9d5SAlexander Stetsenko 	dsl_dataset_t *snapds;
3071ec5cf9d5SAlexander Stetsenko 	guid_map_entry_t *gmep;
3072ec5cf9d5SAlexander Stetsenko 	int err;
3073ec5cf9d5SAlexander Stetsenko 
3074ec5cf9d5SAlexander Stetsenko 	ASSERT(guid_map != NULL);
3075ec5cf9d5SAlexander Stetsenko 
30763b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
30773b2aab18SMatthew Ahrens 	if (err != 0)
30783b2aab18SMatthew Ahrens 		return (err);
3079de8d9cffSMatthew Ahrens 	gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3080de8d9cffSMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3081ec5cf9d5SAlexander Stetsenko 	if (err == 0) {
3082c1379625SJustin T. Gibbs 		gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3083ec5cf9d5SAlexander Stetsenko 		gmep->gme_ds = snapds;
3084ec5cf9d5SAlexander Stetsenko 		avl_add(guid_map, gmep);
30853b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(snapds, gmep);
3086de8d9cffSMatthew Ahrens 	} else {
3087de8d9cffSMatthew Ahrens 		kmem_free(gmep, sizeof (*gmep));
3088ec5cf9d5SAlexander Stetsenko 	}
3089ec5cf9d5SAlexander Stetsenko 
30903b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
3091ec5cf9d5SAlexander Stetsenko 	return (err);
3092ec5cf9d5SAlexander Stetsenko }
3093ec5cf9d5SAlexander Stetsenko 
30943b2aab18SMatthew Ahrens static int dmu_recv_end_modified_blocks = 3;
30953b2aab18SMatthew Ahrens 
3096ae46e4c7SMatthew Ahrens static int
3097ae46e4c7SMatthew Ahrens dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3098f18faf3fSek {
30993b2aab18SMatthew Ahrens 	int error;
31003b2aab18SMatthew Ahrens 	char name[MAXNAMELEN];
31013cb34c60Sahrens 
31023b2aab18SMatthew Ahrens #ifdef _KERNEL
31033b2aab18SMatthew Ahrens 	/*
31043b2aab18SMatthew Ahrens 	 * We will be destroying the ds; make sure its origin is unmounted if
31053b2aab18SMatthew Ahrens 	 * necessary.
31063b2aab18SMatthew Ahrens 	 */
31073b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
31083b2aab18SMatthew Ahrens 	zfs_destroy_unmount_origin(name);
31093b2aab18SMatthew Ahrens #endif
31103cb34c60Sahrens 
31113b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
31123b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
31137d46dc6cSMatthew Ahrens 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
31143cb34c60Sahrens 
31153b2aab18SMatthew Ahrens 	if (error != 0)
31163b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
31173b2aab18SMatthew Ahrens 	return (error);
3118f18faf3fSek }
3119ae46e4c7SMatthew Ahrens 
3120ae46e4c7SMatthew Ahrens static int
3121ae46e4c7SMatthew Ahrens dmu_recv_new_end(dmu_recv_cookie_t *drc)
3122ae46e4c7SMatthew Ahrens {
31233b2aab18SMatthew Ahrens 	int error;
3124ae46e4c7SMatthew Ahrens 
31253b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
31263b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
31277d46dc6cSMatthew Ahrens 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3128ae46e4c7SMatthew Ahrens 
31293b2aab18SMatthew Ahrens 	if (error != 0) {
31303b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
31313b2aab18SMatthew Ahrens 	} else if (drc->drc_guid_to_ds_map != NULL) {
31323b2aab18SMatthew Ahrens 		(void) add_ds_to_guidmap(drc->drc_tofs,
31333b2aab18SMatthew Ahrens 		    drc->drc_guid_to_ds_map,
31343b2aab18SMatthew Ahrens 		    drc->drc_newsnapobj);
3135ae46e4c7SMatthew Ahrens 	}
31363b2aab18SMatthew Ahrens 	return (error);
3137ae46e4c7SMatthew Ahrens }
3138ae46e4c7SMatthew Ahrens 
3139ae46e4c7SMatthew Ahrens int
314091948b51SKeith M Wesolowski dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3141ae46e4c7SMatthew Ahrens {
314291948b51SKeith M Wesolowski 	drc->drc_owner = owner;
314391948b51SKeith M Wesolowski 
31443b2aab18SMatthew Ahrens 	if (drc->drc_newfs)
3145ae46e4c7SMatthew Ahrens 		return (dmu_recv_new_end(drc));
31463b2aab18SMatthew Ahrens 	else
31473b2aab18SMatthew Ahrens 		return (dmu_recv_existing_end(drc));
3148ae46e4c7SMatthew Ahrens }
31492f3d8780SMatthew Ahrens 
31502f3d8780SMatthew Ahrens /*
31512f3d8780SMatthew Ahrens  * Return TRUE if this objset is currently being received into.
31522f3d8780SMatthew Ahrens  */
31532f3d8780SMatthew Ahrens boolean_t
31542f3d8780SMatthew Ahrens dmu_objset_is_receiving(objset_t *os)
31552f3d8780SMatthew Ahrens {
31562f3d8780SMatthew Ahrens 	return (os->os_dsl_dataset != NULL &&
31572f3d8780SMatthew Ahrens 	    os->os_dsl_dataset->ds_owner == dmu_recv_tag);
31582f3d8780SMatthew Ahrens }
3159