xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision 12b90ee2d3b10689fc45f4930d2392f5fe1d9cfa)
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.
27880094b6SAndrew Stormont  * Copyright 2016 RackTop Systems.
28c3d26abcSMatthew Ahrens  * Copyright (c) 2014 Integros [integros.com]
29ec5cf9d5SAlexander Stetsenko  */
30efb80947Sahrens 
31efb80947Sahrens #include <sys/dmu.h>
32efb80947Sahrens #include <sys/dmu_impl.h>
33efb80947Sahrens #include <sys/dmu_tx.h>
34efb80947Sahrens #include <sys/dbuf.h>
35efb80947Sahrens #include <sys/dnode.h>
36efb80947Sahrens #include <sys/zfs_context.h>
37efb80947Sahrens #include <sys/dmu_objset.h>
38efb80947Sahrens #include <sys/dmu_traverse.h>
39efb80947Sahrens #include <sys/dsl_dataset.h>
40efb80947Sahrens #include <sys/dsl_dir.h>
4192241e0bSTom Erickson #include <sys/dsl_prop.h>
42efb80947Sahrens #include <sys/dsl_pool.h>
43efb80947Sahrens #include <sys/dsl_synctask.h>
44efb80947Sahrens #include <sys/zfs_ioctl.h>
45efb80947Sahrens #include <sys/zap.h>
46efb80947Sahrens #include <sys/zio_checksum.h>
47dc7cd546SMark Shellenbaum #include <sys/zfs_znode.h>
48cde58dbcSMatthew Ahrens #include <zfs_fletcher.h>
499e69d7d0SLori Alt #include <sys/avl.h>
508e714474SLori Alt #include <sys/ddt.h>
51c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
523b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
533b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
545d7b4d43SMatthew Ahrens #include <sys/blkptr.h>
5578f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
565d7b4d43SMatthew Ahrens #include <sys/zfeature.h>
57a2cdcdd2SPaul Dagnelie #include <sys/bqueue.h>
58efb80947Sahrens 
5919b94df9SMatthew Ahrens /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
6019b94df9SMatthew Ahrens int zfs_send_corrupt_data = B_FALSE;
61a2cdcdd2SPaul Dagnelie int zfs_send_queue_length = 16 * 1024 * 1024;
62a2cdcdd2SPaul Dagnelie int zfs_recv_queue_length = 16 * 1024 * 1024;
63880094b6SAndrew Stormont /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
64880094b6SAndrew Stormont int zfs_send_set_freerecords_bit = B_TRUE;
6519b94df9SMatthew Ahrens 
663cb34c60Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
679c3fd121SMatthew Ahrens const char *recv_clone_name = "%recv";
683cb34c60Sahrens 
69a2cdcdd2SPaul Dagnelie #define	BP_SPAN(datablkszsec, indblkshift, level) \
70a2cdcdd2SPaul Dagnelie 	(((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
71a2cdcdd2SPaul Dagnelie 	(level) * (indblkshift - SPA_BLKPTRSHIFT)))
72a2cdcdd2SPaul Dagnelie 
739c3fd121SMatthew Ahrens static void byteswap_record(dmu_replay_record_t *drr);
749c3fd121SMatthew Ahrens 
75a2cdcdd2SPaul Dagnelie struct send_thread_arg {
76a2cdcdd2SPaul Dagnelie 	bqueue_t	q;
77a2cdcdd2SPaul Dagnelie 	dsl_dataset_t	*ds;		/* Dataset to traverse */
78a2cdcdd2SPaul Dagnelie 	uint64_t	fromtxg;	/* Traverse from this txg */
79a2cdcdd2SPaul Dagnelie 	int		flags;		/* flags to pass to traverse_dataset */
80a2cdcdd2SPaul Dagnelie 	int		error_code;
81a2cdcdd2SPaul Dagnelie 	boolean_t	cancel;
829c3fd121SMatthew Ahrens 	zbookmark_phys_t resume;
83a2cdcdd2SPaul Dagnelie };
84a2cdcdd2SPaul Dagnelie 
85a2cdcdd2SPaul Dagnelie struct send_block_record {
86a2cdcdd2SPaul Dagnelie 	boolean_t		eos_marker; /* Marks the end of the stream */
87a2cdcdd2SPaul Dagnelie 	blkptr_t		bp;
88a2cdcdd2SPaul Dagnelie 	zbookmark_phys_t	zb;
89a2cdcdd2SPaul Dagnelie 	uint8_t			indblkshift;
90a2cdcdd2SPaul Dagnelie 	uint16_t		datablkszsec;
91a2cdcdd2SPaul Dagnelie 	bqueue_node_t		ln;
92a2cdcdd2SPaul Dagnelie };
93a2cdcdd2SPaul Dagnelie 
94efb80947Sahrens static int
954e3c9f44SBill Pijewski dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
96efb80947Sahrens {
979c3fd121SMatthew Ahrens 	dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
98efb80947Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
99c20404ffSEli Rosenthal 
100c20404ffSEli Rosenthal 	/*
101c20404ffSEli Rosenthal 	 * The code does not rely on this (len being a multiple of 8).  We keep
102c20404ffSEli Rosenthal 	 * this assertion because of the corresponding assertion in
103c20404ffSEli Rosenthal 	 * receive_read().  Keeping this assertion ensures that we do not
104c20404ffSEli Rosenthal 	 * inadvertently break backwards compatibility (causing the assertion
105c20404ffSEli Rosenthal 	 * in receive_read() to trigger on old software).
106c20404ffSEli Rosenthal 	 *
107c20404ffSEli Rosenthal 	 * Removing the assertions could be rolled into a new feature that uses
108c20404ffSEli Rosenthal 	 * data that isn't 8-byte aligned; if the assertions were removed, a
109c20404ffSEli Rosenthal 	 * feature flag would have to be added.
110c20404ffSEli Rosenthal 	 */
111c20404ffSEli Rosenthal 
112fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
113efb80947Sahrens 
1144e3c9f44SBill Pijewski 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
115efb80947Sahrens 	    (caddr_t)buf, len,
116efb80947Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
1174e3c9f44SBill Pijewski 
1184e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
1194e3c9f44SBill Pijewski 	*dsp->dsa_off += len;
1204e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
1214e3c9f44SBill Pijewski 
1224e3c9f44SBill Pijewski 	return (dsp->dsa_err);
123efb80947Sahrens }
124efb80947Sahrens 
12598110f08SMatthew Ahrens /*
12698110f08SMatthew Ahrens  * For all record types except BEGIN, fill in the checksum (overlaid in
12798110f08SMatthew Ahrens  * drr_u.drr_checksum.drr_checksum).  The checksum verifies everything
12898110f08SMatthew Ahrens  * up to the start of the checksum itself.
12998110f08SMatthew Ahrens  */
13098110f08SMatthew Ahrens static int
13198110f08SMatthew Ahrens dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
13298110f08SMatthew Ahrens {
13398110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
13498110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
13598110f08SMatthew Ahrens 	fletcher_4_incremental_native(dsp->dsa_drr,
13698110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
13798110f08SMatthew Ahrens 	    &dsp->dsa_zc);
138*12b90ee2SMatt Krantz 	if (dsp->dsa_drr->drr_type == DRR_BEGIN) {
139*12b90ee2SMatt Krantz 		dsp->dsa_sent_begin = B_TRUE;
140*12b90ee2SMatt Krantz 	} else {
14198110f08SMatthew Ahrens 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
14298110f08SMatthew Ahrens 		    drr_checksum.drr_checksum));
14398110f08SMatthew Ahrens 		dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
14498110f08SMatthew Ahrens 	}
145*12b90ee2SMatt Krantz 	if (dsp->dsa_drr->drr_type == DRR_END) {
146*12b90ee2SMatt Krantz 		dsp->dsa_sent_end = B_TRUE;
147*12b90ee2SMatt Krantz 	}
14898110f08SMatthew Ahrens 	fletcher_4_incremental_native(&dsp->dsa_drr->
14998110f08SMatthew Ahrens 	    drr_u.drr_checksum.drr_checksum,
15098110f08SMatthew Ahrens 	    sizeof (zio_cksum_t), &dsp->dsa_zc);
15198110f08SMatthew Ahrens 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
15298110f08SMatthew Ahrens 		return (SET_ERROR(EINTR));
15398110f08SMatthew Ahrens 	if (payload_len != 0) {
15498110f08SMatthew Ahrens 		fletcher_4_incremental_native(payload, payload_len,
15598110f08SMatthew Ahrens 		    &dsp->dsa_zc);
15698110f08SMatthew Ahrens 		if (dump_bytes(dsp, payload, payload_len) != 0)
15798110f08SMatthew Ahrens 			return (SET_ERROR(EINTR));
15898110f08SMatthew Ahrens 	}
15998110f08SMatthew Ahrens 	return (0);
16098110f08SMatthew Ahrens }
16198110f08SMatthew Ahrens 
16268ecb2ecSPaul Dagnelie /*
16368ecb2ecSPaul Dagnelie  * Fill in the drr_free struct, or perform aggregation if the previous record is
16468ecb2ecSPaul Dagnelie  * also a free record, and the two are adjacent.
16568ecb2ecSPaul Dagnelie  *
16668ecb2ecSPaul Dagnelie  * Note that we send free records even for a full send, because we want to be
16768ecb2ecSPaul Dagnelie  * able to receive a full send as a clone, which requires a list of all the free
16868ecb2ecSPaul Dagnelie  * and freeobject records that were generated on the source.
16968ecb2ecSPaul Dagnelie  */
170efb80947Sahrens static int
1714e3c9f44SBill Pijewski dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
172efb80947Sahrens     uint64_t length)
173efb80947Sahrens {
1744e3c9f44SBill Pijewski 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
1759e69d7d0SLori Alt 
1762f3d8780SMatthew Ahrens 	/*
1772f3d8780SMatthew Ahrens 	 * When we receive a free record, dbuf_free_range() assumes
1782f3d8780SMatthew Ahrens 	 * that the receiving system doesn't have any dbufs in the range
1792f3d8780SMatthew Ahrens 	 * being freed.  This is always true because there is a one-record
1802f3d8780SMatthew Ahrens 	 * constraint: we only send one WRITE record for any given
1819c3fd121SMatthew Ahrens 	 * object,offset.  We know that the one-record constraint is
1822f3d8780SMatthew Ahrens 	 * true because we always send data in increasing order by
1832f3d8780SMatthew Ahrens 	 * object,offset.
1842f3d8780SMatthew Ahrens 	 *
1852f3d8780SMatthew Ahrens 	 * If the increasing-order constraint ever changes, we should find
1862f3d8780SMatthew Ahrens 	 * another way to assert that the one-record constraint is still
1872f3d8780SMatthew Ahrens 	 * satisfied.
1882f3d8780SMatthew Ahrens 	 */
1892f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
1902f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
1912f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
1922f3d8780SMatthew Ahrens 
193534029e5SSimon Klinkert 	if (length != -1ULL && offset + length < offset)
194534029e5SSimon Klinkert 		length = -1ULL;
195534029e5SSimon Klinkert 
1969e69d7d0SLori Alt 	/*
1979e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
1989e69d7d0SLori Alt 	 * since free block aggregation can only be done for blocks of the
1999e69d7d0SLori Alt 	 * same type (i.e., DRR_FREE records can only be aggregated with
2009e69d7d0SLori Alt 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
2019e69d7d0SLori Alt 	 * aggregated with other DRR_FREEOBJECTS records.
2029e69d7d0SLori Alt 	 */
2034e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
2044e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREE) {
20598110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
206be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2074e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2089e69d7d0SLori Alt 	}
2099e69d7d0SLori Alt 
2104e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREE) {
2119e69d7d0SLori Alt 		/*
2129e69d7d0SLori Alt 		 * There should never be a PENDING_FREE if length is -1
2139e69d7d0SLori Alt 		 * (because dump_dnode is the only place where this
2149e69d7d0SLori Alt 		 * function is called with a -1, and only after flushing
2159e69d7d0SLori Alt 		 * any pending record).
2169e69d7d0SLori Alt 		 */
2179e69d7d0SLori Alt 		ASSERT(length != -1ULL);
2189e69d7d0SLori Alt 		/*
2199e69d7d0SLori Alt 		 * Check to see whether this free block can be aggregated
2209e69d7d0SLori Alt 		 * with pending one.
2219e69d7d0SLori Alt 		 */
2229e69d7d0SLori Alt 		if (drrf->drr_object == object && drrf->drr_offset +
2239e69d7d0SLori Alt 		    drrf->drr_length == offset) {
2249e69d7d0SLori Alt 			drrf->drr_length += length;
2259e69d7d0SLori Alt 			return (0);
2269e69d7d0SLori Alt 		} else {
2279e69d7d0SLori Alt 			/* not a continuation.  Push out pending record */
22898110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
229be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
2304e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
2319e69d7d0SLori Alt 		}
2329e69d7d0SLori Alt 	}
2339e69d7d0SLori Alt 	/* create a FREE record and make it pending */
2344e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2354e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREE;
2369e69d7d0SLori Alt 	drrf->drr_object = object;
2379e69d7d0SLori Alt 	drrf->drr_offset = offset;
2389e69d7d0SLori Alt 	drrf->drr_length = length;
2394e3c9f44SBill Pijewski 	drrf->drr_toguid = dsp->dsa_toguid;
2409e69d7d0SLori Alt 	if (length == -1ULL) {
24198110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
242be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2439e69d7d0SLori Alt 	} else {
2444e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_FREE;
2459e69d7d0SLori Alt 	}
246efb80947Sahrens 
247efb80947Sahrens 	return (0);
248efb80947Sahrens }
249efb80947Sahrens 
250efb80947Sahrens static int
2515d7b4d43SMatthew Ahrens dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
2528e714474SLori Alt     uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
253efb80947Sahrens {
2544e3c9f44SBill Pijewski 	struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
2559e69d7d0SLori Alt 
2562f3d8780SMatthew Ahrens 	/*
2572f3d8780SMatthew Ahrens 	 * We send data in increasing object, offset order.
2582f3d8780SMatthew Ahrens 	 * See comment in dump_free() for details.
2592f3d8780SMatthew Ahrens 	 */
2602f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
2612f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
2622f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
2632f3d8780SMatthew Ahrens 	dsp->dsa_last_data_object = object;
2642f3d8780SMatthew Ahrens 	dsp->dsa_last_data_offset = offset + blksz - 1;
2658e714474SLori Alt 
2669e69d7d0SLori Alt 	/*
2679e69d7d0SLori Alt 	 * If there is any kind of pending aggregation (currently either
2689e69d7d0SLori Alt 	 * a grouping of free objects or free blocks), push it out to
2699e69d7d0SLori Alt 	 * the stream, since aggregation can't be done across operations
2709e69d7d0SLori Alt 	 * of different types.
2719e69d7d0SLori Alt 	 */
2724e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
27398110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
274be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2754e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2769e69d7d0SLori Alt 	}
27798110f08SMatthew Ahrens 	/* write a WRITE record */
2784e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2794e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_WRITE;
2809e69d7d0SLori Alt 	drrw->drr_object = object;
2819e69d7d0SLori Alt 	drrw->drr_type = type;
2829e69d7d0SLori Alt 	drrw->drr_offset = offset;
2839e69d7d0SLori Alt 	drrw->drr_length = blksz;
2844e3c9f44SBill Pijewski 	drrw->drr_toguid = dsp->dsa_toguid;
285b5152584SMatthew Ahrens 	if (bp == NULL || BP_IS_EMBEDDED(bp)) {
2865d7b4d43SMatthew Ahrens 		/*
287b5152584SMatthew Ahrens 		 * There's no pre-computed checksum for partial-block
288b5152584SMatthew Ahrens 		 * writes or embedded BP's, so (like
289b5152584SMatthew Ahrens 		 * fletcher4-checkummed blocks) userland will have to
290b5152584SMatthew Ahrens 		 * compute a dedup-capable checksum itself.
2915d7b4d43SMatthew Ahrens 		 */
2925d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
2935d7b4d43SMatthew Ahrens 	} else {
2945d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
29545818ee1SMatthew Ahrens 		if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
29645818ee1SMatthew Ahrens 		    ZCHECKSUM_FLAG_DEDUP)
2975d7b4d43SMatthew Ahrens 			drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
2985d7b4d43SMatthew Ahrens 		DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
2995d7b4d43SMatthew Ahrens 		DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
3005d7b4d43SMatthew Ahrens 		DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
3015d7b4d43SMatthew Ahrens 		drrw->drr_key.ddk_cksum = bp->blk_cksum;
3025d7b4d43SMatthew Ahrens 	}
303efb80947Sahrens 
30498110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
305be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
306efb80947Sahrens 	return (0);
307efb80947Sahrens }
308efb80947Sahrens 
3095d7b4d43SMatthew Ahrens static int
3105d7b4d43SMatthew Ahrens dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
3115d7b4d43SMatthew Ahrens     int blksz, const blkptr_t *bp)
3125d7b4d43SMatthew Ahrens {
3135d7b4d43SMatthew Ahrens 	char buf[BPE_PAYLOAD_SIZE];
3145d7b4d43SMatthew Ahrens 	struct drr_write_embedded *drrw =
3155d7b4d43SMatthew Ahrens 	    &(dsp->dsa_drr->drr_u.drr_write_embedded);
3165d7b4d43SMatthew Ahrens 
3175d7b4d43SMatthew Ahrens 	if (dsp->dsa_pending_op != PENDING_NONE) {
31898110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
3195d7b4d43SMatthew Ahrens 			return (EINTR);
3205d7b4d43SMatthew Ahrens 		dsp->dsa_pending_op = PENDING_NONE;
3215d7b4d43SMatthew Ahrens 	}
3225d7b4d43SMatthew Ahrens 
3235d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp));
3245d7b4d43SMatthew Ahrens 
3255d7b4d43SMatthew Ahrens 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3265d7b4d43SMatthew Ahrens 	dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
3275d7b4d43SMatthew Ahrens 	drrw->drr_object = object;
3285d7b4d43SMatthew Ahrens 	drrw->drr_offset = offset;
3295d7b4d43SMatthew Ahrens 	drrw->drr_length = blksz;
3305d7b4d43SMatthew Ahrens 	drrw->drr_toguid = dsp->dsa_toguid;
3315d7b4d43SMatthew Ahrens 	drrw->drr_compression = BP_GET_COMPRESS(bp);
3325d7b4d43SMatthew Ahrens 	drrw->drr_etype = BPE_GET_ETYPE(bp);
3335d7b4d43SMatthew Ahrens 	drrw->drr_lsize = BPE_GET_LSIZE(bp);
3345d7b4d43SMatthew Ahrens 	drrw->drr_psize = BPE_GET_PSIZE(bp);
3355d7b4d43SMatthew Ahrens 
3365d7b4d43SMatthew Ahrens 	decode_embedded_bp_compressed(bp, buf);
3375d7b4d43SMatthew Ahrens 
33898110f08SMatthew Ahrens 	if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
3395d7b4d43SMatthew Ahrens 		return (EINTR);
3405d7b4d43SMatthew Ahrens 	return (0);
3415d7b4d43SMatthew Ahrens }
3425d7b4d43SMatthew Ahrens 
3430a586ceaSMark Shellenbaum static int
3444e3c9f44SBill Pijewski dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
3450a586ceaSMark Shellenbaum {
3464e3c9f44SBill Pijewski 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
3470a586ceaSMark Shellenbaum 
3484e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
34998110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
350be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3514e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3520a586ceaSMark Shellenbaum 	}
3530a586ceaSMark Shellenbaum 
3540a586ceaSMark Shellenbaum 	/* write a SPILL record */
3554e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3564e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_SPILL;
3570a586ceaSMark Shellenbaum 	drrs->drr_object = object;
3580a586ceaSMark Shellenbaum 	drrs->drr_length = blksz;
3594e3c9f44SBill Pijewski 	drrs->drr_toguid = dsp->dsa_toguid;
3600a586ceaSMark Shellenbaum 
36198110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
362be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
3630a586ceaSMark Shellenbaum 	return (0);
3640a586ceaSMark Shellenbaum }
3650a586ceaSMark Shellenbaum 
366efb80947Sahrens static int
3674e3c9f44SBill Pijewski dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
368efb80947Sahrens {
3694e3c9f44SBill Pijewski 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
3709e69d7d0SLori Alt 
3719e69d7d0SLori Alt 	/*
3729e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
3739e69d7d0SLori Alt 	 * push it out, since free block aggregation can only be done for
3749e69d7d0SLori Alt 	 * blocks of the same type (i.e., DRR_FREE records can only be
3759e69d7d0SLori Alt 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
3769e69d7d0SLori Alt 	 * can only be aggregated with other DRR_FREEOBJECTS records.
3779e69d7d0SLori Alt 	 */
3784e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
3794e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
38098110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
381be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3824e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3839e69d7d0SLori Alt 	}
3844e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
3859e69d7d0SLori Alt 		/*
3869e69d7d0SLori Alt 		 * See whether this free object array can be aggregated
3879e69d7d0SLori Alt 		 * with pending one
3889e69d7d0SLori Alt 		 */
3899e69d7d0SLori Alt 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
3909e69d7d0SLori Alt 			drrfo->drr_numobjs += numobjs;
3919e69d7d0SLori Alt 			return (0);
3929e69d7d0SLori Alt 		} else {
3939e69d7d0SLori Alt 			/* can't be aggregated.  Push out pending record */
39498110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
395be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
3964e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
3979e69d7d0SLori Alt 		}
3989e69d7d0SLori Alt 	}
3999e69d7d0SLori Alt 
400efb80947Sahrens 	/* write a FREEOBJECTS record */
4014e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4024e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
4039e69d7d0SLori Alt 	drrfo->drr_firstobj = firstobj;
4049e69d7d0SLori Alt 	drrfo->drr_numobjs = numobjs;
4054e3c9f44SBill Pijewski 	drrfo->drr_toguid = dsp->dsa_toguid;
4069e69d7d0SLori Alt 
4074e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
408efb80947Sahrens 
409efb80947Sahrens 	return (0);
410efb80947Sahrens }
411efb80947Sahrens 
412efb80947Sahrens static int
4134e3c9f44SBill Pijewski dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
414efb80947Sahrens {
4154e3c9f44SBill Pijewski 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
4169e69d7d0SLori Alt 
4179c3fd121SMatthew Ahrens 	if (object < dsp->dsa_resume_object) {
4189c3fd121SMatthew Ahrens 		/*
4199c3fd121SMatthew Ahrens 		 * Note: when resuming, we will visit all the dnodes in
4209c3fd121SMatthew Ahrens 		 * the block of dnodes that we are resuming from.  In
4219c3fd121SMatthew Ahrens 		 * this case it's unnecessary to send the dnodes prior to
4229c3fd121SMatthew Ahrens 		 * the one we are resuming from.  We should be at most one
4239c3fd121SMatthew Ahrens 		 * block's worth of dnodes behind the resume point.
4249c3fd121SMatthew Ahrens 		 */
4259c3fd121SMatthew Ahrens 		ASSERT3U(dsp->dsa_resume_object - object, <,
4269c3fd121SMatthew Ahrens 		    1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
4279c3fd121SMatthew Ahrens 		return (0);
4289c3fd121SMatthew Ahrens 	}
4299c3fd121SMatthew Ahrens 
430efb80947Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
4314e3c9f44SBill Pijewski 		return (dump_freeobjects(dsp, object, 1));
432efb80947Sahrens 
4334e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
43498110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
435be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
4364e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
4379e69d7d0SLori Alt 	}
4389e69d7d0SLori Alt 
439efb80947Sahrens 	/* write an OBJECT record */
4404e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4414e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_OBJECT;
4429e69d7d0SLori Alt 	drro->drr_object = object;
4439e69d7d0SLori Alt 	drro->drr_type = dnp->dn_type;
4449e69d7d0SLori Alt 	drro->drr_bonustype = dnp->dn_bonustype;
4459e69d7d0SLori Alt 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
4469e69d7d0SLori Alt 	drro->drr_bonuslen = dnp->dn_bonuslen;
4479e69d7d0SLori Alt 	drro->drr_checksumtype = dnp->dn_checksum;
4489e69d7d0SLori Alt 	drro->drr_compress = dnp->dn_compress;
4494e3c9f44SBill Pijewski 	drro->drr_toguid = dsp->dsa_toguid;
4509e69d7d0SLori Alt 
451b5152584SMatthew Ahrens 	if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
452b5152584SMatthew Ahrens 	    drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
453b5152584SMatthew Ahrens 		drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
454b5152584SMatthew Ahrens 
45598110f08SMatthew Ahrens 	if (dump_record(dsp, DN_BONUS(dnp),
45698110f08SMatthew Ahrens 	    P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
457be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
45898110f08SMatthew Ahrens 	}
459efb80947Sahrens 
4602f3d8780SMatthew Ahrens 	/* Free anything past the end of the file. */
4614e3c9f44SBill Pijewski 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
4622f3d8780SMatthew Ahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
463be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
4643b2aab18SMatthew Ahrens 	if (dsp->dsa_err != 0)
465be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
466efb80947Sahrens 	return (0);
467efb80947Sahrens }
468efb80947Sahrens 
4695d7b4d43SMatthew Ahrens static boolean_t
4705d7b4d43SMatthew Ahrens backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
4715d7b4d43SMatthew Ahrens {
4725d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp))
4735d7b4d43SMatthew Ahrens 		return (B_FALSE);
4745d7b4d43SMatthew Ahrens 
4755d7b4d43SMatthew Ahrens 	/*
4765d7b4d43SMatthew Ahrens 	 * Compression function must be legacy, or explicitly enabled.
4775d7b4d43SMatthew Ahrens 	 */
4785d7b4d43SMatthew Ahrens 	if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
4795d7b4d43SMatthew Ahrens 	    !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
4805d7b4d43SMatthew Ahrens 		return (B_FALSE);
4815d7b4d43SMatthew Ahrens 
4825d7b4d43SMatthew Ahrens 	/*
4835d7b4d43SMatthew Ahrens 	 * Embed type must be explicitly enabled.
4845d7b4d43SMatthew Ahrens 	 */
4855d7b4d43SMatthew Ahrens 	switch (BPE_GET_ETYPE(bp)) {
4865d7b4d43SMatthew Ahrens 	case BP_EMBEDDED_TYPE_DATA:
4875d7b4d43SMatthew Ahrens 		if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
4885d7b4d43SMatthew Ahrens 			return (B_TRUE);
4895d7b4d43SMatthew Ahrens 		break;
4905d7b4d43SMatthew Ahrens 	default:
4915d7b4d43SMatthew Ahrens 		return (B_FALSE);
4925d7b4d43SMatthew Ahrens 	}
4935d7b4d43SMatthew Ahrens 	return (B_FALSE);
4945d7b4d43SMatthew Ahrens }
4955d7b4d43SMatthew Ahrens 
496a2cdcdd2SPaul Dagnelie /*
497a2cdcdd2SPaul Dagnelie  * This is the callback function to traverse_dataset that acts as the worker
498a2cdcdd2SPaul Dagnelie  * thread for dmu_send_impl.
499a2cdcdd2SPaul Dagnelie  */
500a2cdcdd2SPaul Dagnelie /*ARGSUSED*/
501a2cdcdd2SPaul Dagnelie static int
502a2cdcdd2SPaul Dagnelie send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
503a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
504a2cdcdd2SPaul Dagnelie {
505a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *sta = arg;
506a2cdcdd2SPaul Dagnelie 	struct send_block_record *record;
507a2cdcdd2SPaul Dagnelie 	uint64_t record_size;
508a2cdcdd2SPaul Dagnelie 	int err = 0;
509efb80947Sahrens 
5109c3fd121SMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
5119c3fd121SMatthew Ahrens 	    zb->zb_object >= sta->resume.zb_object);
5129c3fd121SMatthew Ahrens 
513a2cdcdd2SPaul Dagnelie 	if (sta->cancel)
514a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINTR));
515a2cdcdd2SPaul Dagnelie 
516a2cdcdd2SPaul Dagnelie 	if (bp == NULL) {
517a2cdcdd2SPaul Dagnelie 		ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
518a2cdcdd2SPaul Dagnelie 		return (0);
519a2cdcdd2SPaul Dagnelie 	} else if (zb->zb_level < 0) {
520a2cdcdd2SPaul Dagnelie 		return (0);
521a2cdcdd2SPaul Dagnelie 	}
522a2cdcdd2SPaul Dagnelie 
523a2cdcdd2SPaul Dagnelie 	record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
524a2cdcdd2SPaul Dagnelie 	record->eos_marker = B_FALSE;
525a2cdcdd2SPaul Dagnelie 	record->bp = *bp;
526a2cdcdd2SPaul Dagnelie 	record->zb = *zb;
527a2cdcdd2SPaul Dagnelie 	record->indblkshift = dnp->dn_indblkshift;
528a2cdcdd2SPaul Dagnelie 	record->datablkszsec = dnp->dn_datablkszsec;
529a2cdcdd2SPaul Dagnelie 	record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
530a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&sta->q, record, record_size);
531a2cdcdd2SPaul Dagnelie 
532a2cdcdd2SPaul Dagnelie 	return (err);
533a2cdcdd2SPaul Dagnelie }
534a2cdcdd2SPaul Dagnelie 
535a2cdcdd2SPaul Dagnelie /*
536a2cdcdd2SPaul Dagnelie  * This function kicks off the traverse_dataset.  It also handles setting the
537a2cdcdd2SPaul Dagnelie  * error code of the thread in case something goes wrong, and pushes the End of
538a2cdcdd2SPaul Dagnelie  * Stream record when the traverse_dataset call has finished.  If there is no
539a2cdcdd2SPaul Dagnelie  * dataset to traverse, the thread immediately pushes End of Stream marker.
540a2cdcdd2SPaul Dagnelie  */
541a2cdcdd2SPaul Dagnelie static void
542a2cdcdd2SPaul Dagnelie send_traverse_thread(void *arg)
543a2cdcdd2SPaul Dagnelie {
544a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *st_arg = arg;
545a2cdcdd2SPaul Dagnelie 	int err;
546a2cdcdd2SPaul Dagnelie 	struct send_block_record *data;
547a2cdcdd2SPaul Dagnelie 
548a2cdcdd2SPaul Dagnelie 	if (st_arg->ds != NULL) {
5499c3fd121SMatthew Ahrens 		err = traverse_dataset_resume(st_arg->ds,
5509c3fd121SMatthew Ahrens 		    st_arg->fromtxg, &st_arg->resume,
5519c3fd121SMatthew Ahrens 		    st_arg->flags, send_cb, st_arg);
5529c3fd121SMatthew Ahrens 
553a2cdcdd2SPaul Dagnelie 		if (err != EINTR)
554a2cdcdd2SPaul Dagnelie 			st_arg->error_code = err;
555a2cdcdd2SPaul Dagnelie 	}
556a2cdcdd2SPaul Dagnelie 	data = kmem_zalloc(sizeof (*data), KM_SLEEP);
557a2cdcdd2SPaul Dagnelie 	data->eos_marker = B_TRUE;
558a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&st_arg->q, data, 1);
559a2cdcdd2SPaul Dagnelie }
560a2cdcdd2SPaul Dagnelie 
561a2cdcdd2SPaul Dagnelie /*
562a2cdcdd2SPaul Dagnelie  * This function actually handles figuring out what kind of record needs to be
563a2cdcdd2SPaul Dagnelie  * dumped, reading the data (which has hopefully been prefetched), and calling
564a2cdcdd2SPaul Dagnelie  * the appropriate helper function.
565a2cdcdd2SPaul Dagnelie  */
566efb80947Sahrens static int
567a2cdcdd2SPaul Dagnelie do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
568efb80947Sahrens {
569a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
570a2cdcdd2SPaul Dagnelie 	const blkptr_t *bp = &data->bp;
571a2cdcdd2SPaul Dagnelie 	const zbookmark_phys_t *zb = &data->zb;
572a2cdcdd2SPaul Dagnelie 	uint8_t indblkshift = data->indblkshift;
573a2cdcdd2SPaul Dagnelie 	uint16_t dblkszsec = data->datablkszsec;
574a2cdcdd2SPaul Dagnelie 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
575efb80947Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
576efb80947Sahrens 	int err = 0;
577efb80947Sahrens 
578a2cdcdd2SPaul Dagnelie 	ASSERT3U(zb->zb_level, >=, 0);
579efb80947Sahrens 
5809c3fd121SMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
5819c3fd121SMatthew Ahrens 	    zb->zb_object >= dsa->dsa_resume_object);
5829c3fd121SMatthew Ahrens 
583b24ab676SJeff Bonwick 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
584b24ab676SJeff Bonwick 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
58514843421SMatthew Ahrens 		return (0);
58643466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp) &&
58743466aaeSMax Grossman 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
588a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
58988b7b0f2SMatthew Ahrens 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
590a2cdcdd2SPaul Dagnelie 		err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
59143466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp)) {
592a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
593a2cdcdd2SPaul Dagnelie 		uint64_t offset = zb->zb_blkid * span;
594a2cdcdd2SPaul Dagnelie 		err = dump_free(dsa, zb->zb_object, offset, span);
59588b7b0f2SMatthew Ahrens 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
59688b7b0f2SMatthew Ahrens 		return (0);
59788b7b0f2SMatthew Ahrens 	} else if (type == DMU_OT_DNODE) {
598efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
5997adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
60088b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
601efb80947Sahrens 
602a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
603a2cdcdd2SPaul Dagnelie 
6041b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
6051b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
6061b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
607be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
60888b7b0f2SMatthew Ahrens 
609a2cdcdd2SPaul Dagnelie 		dnode_phys_t *blk = abuf->b_data;
610a2cdcdd2SPaul Dagnelie 		uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
611a2cdcdd2SPaul Dagnelie 		for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
612a2cdcdd2SPaul Dagnelie 			err = dump_dnode(dsa, dnobj + i, blk + i);
6133b2aab18SMatthew Ahrens 			if (err != 0)
614efb80947Sahrens 				break;
615efb80947Sahrens 		}
616dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
6170a586ceaSMark Shellenbaum 	} else if (type == DMU_OT_SA) {
6187adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
6190a586ceaSMark Shellenbaum 		arc_buf_t *abuf;
6200a586ceaSMark Shellenbaum 		int blksz = BP_GET_LSIZE(bp);
6210a586ceaSMark Shellenbaum 
6221b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
6231b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
6241b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
625be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
6260a586ceaSMark Shellenbaum 
627a2cdcdd2SPaul Dagnelie 		err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
628dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
629a2cdcdd2SPaul Dagnelie 	} else if (backup_do_embed(dsa, bp)) {
6305d7b4d43SMatthew Ahrens 		/* it's an embedded level-0 block of a regular object */
631a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
632a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
633a2cdcdd2SPaul Dagnelie 		err = dump_write_embedded(dsa, zb->zb_object,
6345d7b4d43SMatthew Ahrens 		    zb->zb_blkid * blksz, blksz, bp);
635a2cdcdd2SPaul Dagnelie 	} else {
636a2cdcdd2SPaul Dagnelie 		/* it's a level-0 block of a regular object */
6377adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
63888b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
639a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
640b5152584SMatthew Ahrens 		uint64_t offset;
64188b7b0f2SMatthew Ahrens 
64278f17100SMatthew Ahrens 		ASSERT0(zb->zb_level);
6439c3fd121SMatthew Ahrens 		ASSERT(zb->zb_object > dsa->dsa_resume_object ||
6449c3fd121SMatthew Ahrens 		    (zb->zb_object == dsa->dsa_resume_object &&
6459c3fd121SMatthew Ahrens 		    zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
6469c3fd121SMatthew Ahrens 
6471b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
6481b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
6491b912ec7SGeorge Wilson 		    &aflags, zb) != 0) {
65019b94df9SMatthew Ahrens 			if (zfs_send_corrupt_data) {
65119b94df9SMatthew Ahrens 				/* Send a block filled with 0x"zfs badd bloc" */
652dcbf3bd6SGeorge Wilson 				abuf = arc_alloc_buf(spa, blksz, &abuf,
65319b94df9SMatthew Ahrens 				    ARC_BUFC_DATA);
65419b94df9SMatthew Ahrens 				uint64_t *ptr;
65519b94df9SMatthew Ahrens 				for (ptr = abuf->b_data;
65619b94df9SMatthew Ahrens 				    (char *)ptr < (char *)abuf->b_data + blksz;
65719b94df9SMatthew Ahrens 				    ptr++)
6588c76e076SBrian Behlendorf 					*ptr = 0x2f5baddb10cULL;
65919b94df9SMatthew Ahrens 			} else {
660be6fd75aSMatthew Ahrens 				return (SET_ERROR(EIO));
66119b94df9SMatthew Ahrens 			}
66219b94df9SMatthew Ahrens 		}
66388b7b0f2SMatthew Ahrens 
664b5152584SMatthew Ahrens 		offset = zb->zb_blkid * blksz;
665b5152584SMatthew Ahrens 
666a2cdcdd2SPaul Dagnelie 		if (!(dsa->dsa_featureflags &
667b5152584SMatthew Ahrens 		    DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
668b5152584SMatthew Ahrens 		    blksz > SPA_OLD_MAXBLOCKSIZE) {
669b5152584SMatthew Ahrens 			char *buf = abuf->b_data;
670b5152584SMatthew Ahrens 			while (blksz > 0 && err == 0) {
671b5152584SMatthew Ahrens 				int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
672a2cdcdd2SPaul Dagnelie 				err = dump_write(dsa, type, zb->zb_object,
673b5152584SMatthew Ahrens 				    offset, n, NULL, buf);
674b5152584SMatthew Ahrens 				offset += n;
675b5152584SMatthew Ahrens 				buf += n;
676b5152584SMatthew Ahrens 				blksz -= n;
677b5152584SMatthew Ahrens 			}
678b5152584SMatthew Ahrens 		} else {
679a2cdcdd2SPaul Dagnelie 			err = dump_write(dsa, type, zb->zb_object,
680b5152584SMatthew Ahrens 			    offset, blksz, bp, abuf->b_data);
681b5152584SMatthew Ahrens 		}
682dcbf3bd6SGeorge Wilson 		arc_buf_destroy(abuf, &abuf);
683efb80947Sahrens 	}
684efb80947Sahrens 
685efb80947Sahrens 	ASSERT(err == 0 || err == EINTR);
686efb80947Sahrens 	return (err);
687efb80947Sahrens }
688efb80947Sahrens 
6894445fffbSMatthew Ahrens /*
690a2cdcdd2SPaul Dagnelie  * Pop the new data off the queue, and free the old data.
691a2cdcdd2SPaul Dagnelie  */
692a2cdcdd2SPaul Dagnelie static struct send_block_record *
693a2cdcdd2SPaul Dagnelie get_next_record(bqueue_t *bq, struct send_block_record *data)
694a2cdcdd2SPaul Dagnelie {
695a2cdcdd2SPaul Dagnelie 	struct send_block_record *tmp = bqueue_dequeue(bq);
696a2cdcdd2SPaul Dagnelie 	kmem_free(data, sizeof (*data));
697a2cdcdd2SPaul Dagnelie 	return (tmp);
698a2cdcdd2SPaul Dagnelie }
699a2cdcdd2SPaul Dagnelie 
700a2cdcdd2SPaul Dagnelie /*
701a2cdcdd2SPaul Dagnelie  * Actually do the bulk of the work in a zfs send.
702a2cdcdd2SPaul Dagnelie  *
703a2cdcdd2SPaul Dagnelie  * Note: Releases dp using the specified tag.
7044445fffbSMatthew Ahrens  */
7053b2aab18SMatthew Ahrens static int
706a2cdcdd2SPaul Dagnelie dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
7079c3fd121SMatthew Ahrens     zfs_bookmark_phys_t *ancestor_zb,
7089c3fd121SMatthew Ahrens     boolean_t is_clone, boolean_t embedok, boolean_t large_block_ok, int outfd,
7099c3fd121SMatthew Ahrens     uint64_t resumeobj, uint64_t resumeoff,
7109c3fd121SMatthew Ahrens     vnode_t *vp, offset_t *off)
711efb80947Sahrens {
7123b2aab18SMatthew Ahrens 	objset_t *os;
713efb80947Sahrens 	dmu_replay_record_t *drr;
7144e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp;
715efb80947Sahrens 	int err;
7163cb34c60Sahrens 	uint64_t fromtxg = 0;
7175d7b4d43SMatthew Ahrens 	uint64_t featureflags = 0;
7189c3fd121SMatthew Ahrens 	struct send_thread_arg to_arg = { 0 };
719efb80947Sahrens 
720a2cdcdd2SPaul Dagnelie 	err = dmu_objset_from_ds(to_ds, &os);
7213b2aab18SMatthew Ahrens 	if (err != 0) {
7223b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
7233b2aab18SMatthew Ahrens 		return (err);
7243b2aab18SMatthew Ahrens 	}
725efb80947Sahrens 
726efb80947Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
727efb80947Sahrens 	drr->drr_type = DRR_BEGIN;
728efb80947Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
7299e69d7d0SLori Alt 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
7309e69d7d0SLori Alt 	    DMU_SUBSTREAM);
731dc7cd546SMark Shellenbaum 
732dc7cd546SMark Shellenbaum #ifdef _KERNEL
7333b2aab18SMatthew Ahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
734dc7cd546SMark Shellenbaum 		uint64_t version;
7353b2aab18SMatthew Ahrens 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
7364e3c9f44SBill Pijewski 			kmem_free(drr, sizeof (dmu_replay_record_t));
7373b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, tag);
738be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
7394e3c9f44SBill Pijewski 		}
7403b2aab18SMatthew Ahrens 		if (version >= ZPL_VERSION_SA) {
7415d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
742dc7cd546SMark Shellenbaum 		}
743dc7cd546SMark Shellenbaum 	}
744dc7cd546SMark Shellenbaum #endif
745dc7cd546SMark Shellenbaum 
746ca0cc391SMatthew Ahrens 	if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
747b5152584SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
7485d7b4d43SMatthew Ahrens 	if (embedok &&
7495d7b4d43SMatthew Ahrens 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
7505d7b4d43SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
7515d7b4d43SMatthew Ahrens 		if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
7525d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
7535d7b4d43SMatthew Ahrens 	}
7545d7b4d43SMatthew Ahrens 
7559c3fd121SMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
7569c3fd121SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_RESUMING;
7579c3fd121SMatthew Ahrens 	}
7589c3fd121SMatthew Ahrens 
7595d7b4d43SMatthew Ahrens 	DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
7605d7b4d43SMatthew Ahrens 	    featureflags);
7615d7b4d43SMatthew Ahrens 
762efb80947Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
763a2cdcdd2SPaul Dagnelie 	    dsl_dataset_phys(to_ds)->ds_creation_time;
7643b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
76578f17100SMatthew Ahrens 	if (is_clone)
7663cb34c60Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
767a2cdcdd2SPaul Dagnelie 	drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
768a2cdcdd2SPaul Dagnelie 	if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
769ab04eb8eStimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
770880094b6SAndrew Stormont 	if (zfs_send_set_freerecords_bit)
771880094b6SAndrew Stormont 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
772ab04eb8eStimh 
773a2cdcdd2SPaul Dagnelie 	if (ancestor_zb != NULL) {
774a2cdcdd2SPaul Dagnelie 		drr->drr_u.drr_begin.drr_fromguid =
775a2cdcdd2SPaul Dagnelie 		    ancestor_zb->zbm_guid;
776a2cdcdd2SPaul Dagnelie 		fromtxg = ancestor_zb->zbm_creation_txg;
77778f17100SMatthew Ahrens 	}
778a2cdcdd2SPaul Dagnelie 	dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
779a2cdcdd2SPaul Dagnelie 	if (!to_ds->ds_is_snapshot) {
78078f17100SMatthew Ahrens 		(void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
78178f17100SMatthew Ahrens 		    sizeof (drr->drr_u.drr_begin.drr_toname));
7823b2aab18SMatthew Ahrens 	}
7833cb34c60Sahrens 
7844e3c9f44SBill Pijewski 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
7854e3c9f44SBill Pijewski 
7864e3c9f44SBill Pijewski 	dsp->dsa_drr = drr;
7874e3c9f44SBill Pijewski 	dsp->dsa_vp = vp;
7884e3c9f44SBill Pijewski 	dsp->dsa_outfd = outfd;
7894e3c9f44SBill Pijewski 	dsp->dsa_proc = curproc;
7903b2aab18SMatthew Ahrens 	dsp->dsa_os = os;
7914e3c9f44SBill Pijewski 	dsp->dsa_off = off;
792a2cdcdd2SPaul Dagnelie 	dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
7934e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_NONE;
7945d7b4d43SMatthew Ahrens 	dsp->dsa_featureflags = featureflags;
7959c3fd121SMatthew Ahrens 	dsp->dsa_resume_object = resumeobj;
7969c3fd121SMatthew Ahrens 	dsp->dsa_resume_offset = resumeoff;
7974e3c9f44SBill Pijewski 
798a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
799a2cdcdd2SPaul Dagnelie 	list_insert_head(&to_ds->ds_sendstreams, dsp);
800a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
8014e3c9f44SBill Pijewski 
802a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_hold(to_ds, FTAG);
803de8d9cffSMatthew Ahrens 	dsl_pool_rele(dp, tag);
804de8d9cffSMatthew Ahrens 
8059c3fd121SMatthew Ahrens 	void *payload = NULL;
8069c3fd121SMatthew Ahrens 	size_t payload_len = 0;
8079c3fd121SMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
8089c3fd121SMatthew Ahrens 		dmu_object_info_t to_doi;
8099c3fd121SMatthew Ahrens 		err = dmu_object_info(os, resumeobj, &to_doi);
8109c3fd121SMatthew Ahrens 		if (err != 0)
8119c3fd121SMatthew Ahrens 			goto out;
8129c3fd121SMatthew Ahrens 		SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
8139c3fd121SMatthew Ahrens 		    resumeoff / to_doi.doi_data_block_size);
8149c3fd121SMatthew Ahrens 
8159c3fd121SMatthew Ahrens 		nvlist_t *nvl = fnvlist_alloc();
8169c3fd121SMatthew Ahrens 		fnvlist_add_uint64(nvl, "resume_object", resumeobj);
8179c3fd121SMatthew Ahrens 		fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
8189c3fd121SMatthew Ahrens 		payload = fnvlist_pack(nvl, &payload_len);
8199c3fd121SMatthew Ahrens 		drr->drr_payloadlen = payload_len;
8209c3fd121SMatthew Ahrens 		fnvlist_free(nvl);
8219c3fd121SMatthew Ahrens 	}
8229c3fd121SMatthew Ahrens 
8239c3fd121SMatthew Ahrens 	err = dump_record(dsp, payload, payload_len);
8249c3fd121SMatthew Ahrens 	fnvlist_pack_free(payload, payload_len);
8259c3fd121SMatthew Ahrens 	if (err != 0) {
8264e3c9f44SBill Pijewski 		err = dsp->dsa_err;
8274e3c9f44SBill Pijewski 		goto out;
828efb80947Sahrens 	}
829efb80947Sahrens 
830a2cdcdd2SPaul Dagnelie 	err = bqueue_init(&to_arg.q, zfs_send_queue_length,
831a2cdcdd2SPaul Dagnelie 	    offsetof(struct send_block_record, ln));
832a2cdcdd2SPaul Dagnelie 	to_arg.error_code = 0;
833a2cdcdd2SPaul Dagnelie 	to_arg.cancel = B_FALSE;
834a2cdcdd2SPaul Dagnelie 	to_arg.ds = to_ds;
835a2cdcdd2SPaul Dagnelie 	to_arg.fromtxg = fromtxg;
836a2cdcdd2SPaul Dagnelie 	to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
837a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
838a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
839a2cdcdd2SPaul Dagnelie 
840a2cdcdd2SPaul Dagnelie 	struct send_block_record *to_data;
841a2cdcdd2SPaul Dagnelie 	to_data = bqueue_dequeue(&to_arg.q);
842a2cdcdd2SPaul Dagnelie 
843a2cdcdd2SPaul Dagnelie 	while (!to_data->eos_marker && err == 0) {
844a2cdcdd2SPaul Dagnelie 		err = do_dump(dsp, to_data);
845a2cdcdd2SPaul Dagnelie 		to_data = get_next_record(&to_arg.q, to_data);
846a2cdcdd2SPaul Dagnelie 		if (issig(JUSTLOOKING) && issig(FORREAL))
847a2cdcdd2SPaul Dagnelie 			err = EINTR;
848a2cdcdd2SPaul Dagnelie 	}
849a2cdcdd2SPaul Dagnelie 
850a2cdcdd2SPaul Dagnelie 	if (err != 0) {
851a2cdcdd2SPaul Dagnelie 		to_arg.cancel = B_TRUE;
852a2cdcdd2SPaul Dagnelie 		while (!to_data->eos_marker) {
853a2cdcdd2SPaul Dagnelie 			to_data = get_next_record(&to_arg.q, to_data);
854a2cdcdd2SPaul Dagnelie 		}
855a2cdcdd2SPaul Dagnelie 	}
856a2cdcdd2SPaul Dagnelie 	kmem_free(to_data, sizeof (*to_data));
857a2cdcdd2SPaul Dagnelie 
858a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&to_arg.q);
859a2cdcdd2SPaul Dagnelie 
860a2cdcdd2SPaul Dagnelie 	if (err == 0 && to_arg.error_code != 0)
861a2cdcdd2SPaul Dagnelie 		err = to_arg.error_code;
862a2cdcdd2SPaul Dagnelie 
863a2cdcdd2SPaul Dagnelie 	if (err != 0)
864a2cdcdd2SPaul Dagnelie 		goto out;
865efb80947Sahrens 
8664e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE)
86798110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
868be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINTR);
8699e69d7d0SLori Alt 
8703b2aab18SMatthew Ahrens 	if (err != 0) {
8713b2aab18SMatthew Ahrens 		if (err == EINTR && dsp->dsa_err != 0)
8724e3c9f44SBill Pijewski 			err = dsp->dsa_err;
8734e3c9f44SBill Pijewski 		goto out;
874efb80947Sahrens 	}
875efb80947Sahrens 
876efb80947Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
877efb80947Sahrens 	drr->drr_type = DRR_END;
8784e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
8794e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
880efb80947Sahrens 
881a2cdcdd2SPaul Dagnelie 	if (dump_record(dsp, NULL, 0) != 0)
8824e3c9f44SBill Pijewski 		err = dsp->dsa_err;
883efb80947Sahrens 
8844e3c9f44SBill Pijewski out:
885a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
886a2cdcdd2SPaul Dagnelie 	list_remove(&to_ds->ds_sendstreams, dsp);
887a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
8884e3c9f44SBill Pijewski 
889*12b90ee2SMatt Krantz 	VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end));
890*12b90ee2SMatt Krantz 
891efb80947Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
8924e3c9f44SBill Pijewski 	kmem_free(dsp, sizeof (dmu_sendarg_t));
893efb80947Sahrens 
894a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_rele(to_ds, FTAG);
8953b2aab18SMatthew Ahrens 
8964e3c9f44SBill Pijewski 	return (err);
897efb80947Sahrens }
898efb80947Sahrens 
89919b94df9SMatthew Ahrens int
9003b2aab18SMatthew Ahrens dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
901b5152584SMatthew Ahrens     boolean_t embedok, boolean_t large_block_ok,
902b5152584SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
9033b2aab18SMatthew Ahrens {
9043b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9053b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
9063b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
9073b2aab18SMatthew Ahrens 	int err;
9083b2aab18SMatthew Ahrens 
9093b2aab18SMatthew Ahrens 	err = dsl_pool_hold(pool, FTAG, &dp);
9103b2aab18SMatthew Ahrens 	if (err != 0)
9113b2aab18SMatthew Ahrens 		return (err);
9123b2aab18SMatthew Ahrens 
9133b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
9143b2aab18SMatthew Ahrens 	if (err != 0) {
9153b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
9163b2aab18SMatthew Ahrens 		return (err);
9173b2aab18SMatthew Ahrens 	}
9183b2aab18SMatthew Ahrens 
9193b2aab18SMatthew Ahrens 	if (fromsnap != 0) {
92078f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
92178f17100SMatthew Ahrens 		boolean_t is_clone;
92278f17100SMatthew Ahrens 
9233b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
9243b2aab18SMatthew Ahrens 		if (err != 0) {
9253b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
9263b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
9273b2aab18SMatthew Ahrens 			return (err);
9283b2aab18SMatthew Ahrens 		}
92978f17100SMatthew Ahrens 		if (!dsl_dataset_is_before(ds, fromds, 0))
93078f17100SMatthew Ahrens 			err = SET_ERROR(EXDEV);
931c1379625SJustin T. Gibbs 		zb.zbm_creation_time =
932c1379625SJustin T. Gibbs 		    dsl_dataset_phys(fromds)->ds_creation_time;
933c1379625SJustin T. Gibbs 		zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
934c1379625SJustin T. Gibbs 		zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
93578f17100SMatthew Ahrens 		is_clone = (fromds->ds_dir != ds->ds_dir);
93678f17100SMatthew Ahrens 		dsl_dataset_rele(fromds, FTAG);
937b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
9389c3fd121SMatthew Ahrens 		    embedok, large_block_ok, outfd, 0, 0, vp, off);
93978f17100SMatthew Ahrens 	} else {
940b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
9419c3fd121SMatthew Ahrens 		    embedok, large_block_ok, outfd, 0, 0, vp, off);
9423b2aab18SMatthew Ahrens 	}
94378f17100SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
94478f17100SMatthew Ahrens 	return (err);
9453b2aab18SMatthew Ahrens }
9463b2aab18SMatthew Ahrens 
9473b2aab18SMatthew Ahrens int
9489c3fd121SMatthew Ahrens dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
9499c3fd121SMatthew Ahrens     boolean_t large_block_ok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
9509c3fd121SMatthew Ahrens     vnode_t *vp, offset_t *off)
9513b2aab18SMatthew Ahrens {
9523b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9533b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
9543b2aab18SMatthew Ahrens 	int err;
95578f17100SMatthew Ahrens 	boolean_t owned = B_FALSE;
9563b2aab18SMatthew Ahrens 
95778f17100SMatthew Ahrens 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
958be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9593b2aab18SMatthew Ahrens 
9603b2aab18SMatthew Ahrens 	err = dsl_pool_hold(tosnap, FTAG, &dp);
9613b2aab18SMatthew Ahrens 	if (err != 0)
9623b2aab18SMatthew Ahrens 		return (err);
9633b2aab18SMatthew Ahrens 
96478f17100SMatthew Ahrens 	if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
96578f17100SMatthew Ahrens 		/*
96678f17100SMatthew Ahrens 		 * We are sending a filesystem or volume.  Ensure
96778f17100SMatthew Ahrens 		 * that it doesn't change by owning the dataset.
96878f17100SMatthew Ahrens 		 */
96978f17100SMatthew Ahrens 		err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
97078f17100SMatthew Ahrens 		owned = B_TRUE;
97178f17100SMatthew Ahrens 	} else {
97278f17100SMatthew Ahrens 		err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
97378f17100SMatthew Ahrens 	}
9743b2aab18SMatthew Ahrens 	if (err != 0) {
9753b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
9763b2aab18SMatthew Ahrens 		return (err);
9773b2aab18SMatthew Ahrens 	}
9783b2aab18SMatthew Ahrens 
9793b2aab18SMatthew Ahrens 	if (fromsnap != NULL) {
98078f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
98178f17100SMatthew Ahrens 		boolean_t is_clone = B_FALSE;
98278f17100SMatthew Ahrens 		int fsnamelen = strchr(tosnap, '@') - tosnap;
98378f17100SMatthew Ahrens 
98478f17100SMatthew Ahrens 		/*
98578f17100SMatthew Ahrens 		 * If the fromsnap is in a different filesystem, then
98678f17100SMatthew Ahrens 		 * mark the send stream as a clone.
98778f17100SMatthew Ahrens 		 */
98878f17100SMatthew Ahrens 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
98978f17100SMatthew Ahrens 		    (fromsnap[fsnamelen] != '@' &&
99078f17100SMatthew Ahrens 		    fromsnap[fsnamelen] != '#')) {
99178f17100SMatthew Ahrens 			is_clone = B_TRUE;
99278f17100SMatthew Ahrens 		}
99378f17100SMatthew Ahrens 
99478f17100SMatthew Ahrens 		if (strchr(fromsnap, '@')) {
99578f17100SMatthew Ahrens 			dsl_dataset_t *fromds;
99678f17100SMatthew Ahrens 			err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
99778f17100SMatthew Ahrens 			if (err == 0) {
99878f17100SMatthew Ahrens 				if (!dsl_dataset_is_before(ds, fromds, 0))
99978f17100SMatthew Ahrens 					err = SET_ERROR(EXDEV);
100078f17100SMatthew Ahrens 				zb.zbm_creation_time =
1001c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_time;
100278f17100SMatthew Ahrens 				zb.zbm_creation_txg =
1003c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_txg;
1004c1379625SJustin T. Gibbs 				zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
100578f17100SMatthew Ahrens 				is_clone = (ds->ds_dir != fromds->ds_dir);
100678f17100SMatthew Ahrens 				dsl_dataset_rele(fromds, FTAG);
100778f17100SMatthew Ahrens 			}
100878f17100SMatthew Ahrens 		} else {
100978f17100SMatthew Ahrens 			err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
101078f17100SMatthew Ahrens 		}
10113b2aab18SMatthew Ahrens 		if (err != 0) {
10123b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
10133b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
10143b2aab18SMatthew Ahrens 			return (err);
10153b2aab18SMatthew Ahrens 		}
1016b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
10179c3fd121SMatthew Ahrens 		    embedok, large_block_ok,
10189c3fd121SMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
101978f17100SMatthew Ahrens 	} else {
1020b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
10219c3fd121SMatthew Ahrens 		    embedok, large_block_ok,
10229c3fd121SMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
10233b2aab18SMatthew Ahrens 	}
102478f17100SMatthew Ahrens 	if (owned)
102578f17100SMatthew Ahrens 		dsl_dataset_disown(ds, FTAG);
102678f17100SMatthew Ahrens 	else
102778f17100SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
102878f17100SMatthew Ahrens 	return (err);
10293b2aab18SMatthew Ahrens }
10303b2aab18SMatthew Ahrens 
1031643da460SMax Grossman static int
1032643da460SMax Grossman dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t size,
1033643da460SMax Grossman     uint64_t *sizep)
1034643da460SMax Grossman {
1035643da460SMax Grossman 	int err;
1036643da460SMax Grossman 	/*
1037643da460SMax Grossman 	 * Assume that space (both on-disk and in-stream) is dominated by
1038643da460SMax Grossman 	 * data.  We will adjust for indirect blocks and the copies property,
1039643da460SMax Grossman 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1040643da460SMax Grossman 	 */
1041643da460SMax Grossman 
1042643da460SMax Grossman 	/*
1043643da460SMax Grossman 	 * Subtract out approximate space used by indirect blocks.
1044643da460SMax Grossman 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
1045643da460SMax Grossman 	 * Assume all blocks are recordsize.  Assume ditto blocks and
1046643da460SMax Grossman 	 * internal fragmentation counter out compression.
1047643da460SMax Grossman 	 *
1048643da460SMax Grossman 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1049643da460SMax Grossman 	 * block, which we observe in practice.
1050643da460SMax Grossman 	 */
1051643da460SMax Grossman 	uint64_t recordsize;
1052643da460SMax Grossman 	err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
1053643da460SMax Grossman 	if (err != 0)
1054643da460SMax Grossman 		return (err);
1055643da460SMax Grossman 	size -= size / recordsize * sizeof (blkptr_t);
1056643da460SMax Grossman 
1057643da460SMax Grossman 	/* Add in the space for the record associated with each block. */
1058643da460SMax Grossman 	size += size / recordsize * sizeof (dmu_replay_record_t);
1059643da460SMax Grossman 
1060643da460SMax Grossman 	*sizep = size;
1061643da460SMax Grossman 
1062643da460SMax Grossman 	return (0);
1063643da460SMax Grossman }
1064643da460SMax Grossman 
10653b2aab18SMatthew Ahrens int
10663b2aab18SMatthew Ahrens dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
106719b94df9SMatthew Ahrens {
106819b94df9SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
106919b94df9SMatthew Ahrens 	int err;
107019b94df9SMatthew Ahrens 	uint64_t size;
107119b94df9SMatthew Ahrens 
10723b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
10733b2aab18SMatthew Ahrens 
107419b94df9SMatthew Ahrens 	/* tosnap must be a snapshot */
1075bc9014e6SJustin Gibbs 	if (!ds->ds_is_snapshot)
1076be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
107719b94df9SMatthew Ahrens 
107824218bebSAndriy Gapon 	/* fromsnap, if provided, must be a snapshot */
107924218bebSAndriy Gapon 	if (fromds != NULL && !fromds->ds_is_snapshot)
108024218bebSAndriy Gapon 		return (SET_ERROR(EINVAL));
108124218bebSAndriy Gapon 
10824445fffbSMatthew Ahrens 	/*
10834445fffbSMatthew Ahrens 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
10844445fffbSMatthew Ahrens 	 * or the origin's fs.
10854445fffbSMatthew Ahrens 	 */
108678f17100SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1087be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
108819b94df9SMatthew Ahrens 
108919b94df9SMatthew Ahrens 	/* Get uncompressed size estimate of changed data. */
109019b94df9SMatthew Ahrens 	if (fromds == NULL) {
1091c1379625SJustin T. Gibbs 		size = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
109219b94df9SMatthew Ahrens 	} else {
109319b94df9SMatthew Ahrens 		uint64_t used, comp;
109419b94df9SMatthew Ahrens 		err = dsl_dataset_space_written(fromds, ds,
109519b94df9SMatthew Ahrens 		    &used, &comp, &size);
10963b2aab18SMatthew Ahrens 		if (err != 0)
109719b94df9SMatthew Ahrens 			return (err);
109819b94df9SMatthew Ahrens 	}
109919b94df9SMatthew Ahrens 
1100643da460SMax Grossman 	err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1101643da460SMax Grossman 	return (err);
1102643da460SMax Grossman }
1103643da460SMax Grossman 
1104643da460SMax Grossman /*
1105643da460SMax Grossman  * Simple callback used to traverse the blocks of a snapshot and sum their
1106643da460SMax Grossman  * uncompressed size
1107643da460SMax Grossman  */
1108643da460SMax Grossman /* ARGSUSED */
1109643da460SMax Grossman static int
1110643da460SMax Grossman dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1111643da460SMax Grossman     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1112643da460SMax Grossman {
1113643da460SMax Grossman 	uint64_t *spaceptr = arg;
1114643da460SMax Grossman 	if (bp != NULL && !BP_IS_HOLE(bp)) {
1115643da460SMax Grossman 		*spaceptr += BP_GET_UCSIZE(bp);
1116643da460SMax Grossman 	}
1117643da460SMax Grossman 	return (0);
1118643da460SMax Grossman }
1119643da460SMax Grossman 
1120643da460SMax Grossman /*
1121643da460SMax Grossman  * Given a desination snapshot and a TXG, calculate the approximate size of a
1122643da460SMax Grossman  * send stream sent from that TXG. from_txg may be zero, indicating that the
1123643da460SMax Grossman  * whole snapshot will be sent.
1124643da460SMax Grossman  */
1125643da460SMax Grossman int
1126643da460SMax Grossman dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
1127643da460SMax Grossman     uint64_t *sizep)
1128643da460SMax Grossman {
1129643da460SMax Grossman 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1130643da460SMax Grossman 	int err;
1131643da460SMax Grossman 	uint64_t size = 0;
1132643da460SMax Grossman 
1133643da460SMax Grossman 	ASSERT(dsl_pool_config_held(dp));
1134643da460SMax Grossman 
1135643da460SMax Grossman 	/* tosnap must be a snapshot */
1136643da460SMax Grossman 	if (!dsl_dataset_is_snapshot(ds))
1137643da460SMax Grossman 		return (SET_ERROR(EINVAL));
1138643da460SMax Grossman 
1139643da460SMax Grossman 	/* verify that from_txg is before the provided snapshot was taken */
1140643da460SMax Grossman 	if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1141643da460SMax Grossman 		return (SET_ERROR(EXDEV));
1142643da460SMax Grossman 	}
114319b94df9SMatthew Ahrens 
114419b94df9SMatthew Ahrens 	/*
1145643da460SMax Grossman 	 * traverse the blocks of the snapshot with birth times after
1146643da460SMax Grossman 	 * from_txg, summing their uncompressed size
114719b94df9SMatthew Ahrens 	 */
1148643da460SMax Grossman 	err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1149643da460SMax Grossman 	    dmu_calculate_send_traversal, &size);
1150643da460SMax Grossman 	if (err)
115119b94df9SMatthew Ahrens 		return (err);
115219b94df9SMatthew Ahrens 
1153643da460SMax Grossman 	err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1154643da460SMax Grossman 	return (err);
115519b94df9SMatthew Ahrens }
115619b94df9SMatthew Ahrens 
11573b2aab18SMatthew Ahrens typedef struct dmu_recv_begin_arg {
11583b2aab18SMatthew Ahrens 	const char *drba_origin;
11593b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drba_cookie;
11603b2aab18SMatthew Ahrens 	cred_t *drba_cred;
116134f2f8cfSMatthew Ahrens 	uint64_t drba_snapobj;
11623b2aab18SMatthew Ahrens } dmu_recv_begin_arg_t;
1163f18faf3fSek 
1164f18faf3fSek static int
11653b2aab18SMatthew Ahrens recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
11663b2aab18SMatthew Ahrens     uint64_t fromguid)
1167f18faf3fSek {
11683cb34c60Sahrens 	uint64_t val;
11693b2aab18SMatthew Ahrens 	int error;
11703b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1171f18faf3fSek 
11723b2aab18SMatthew Ahrens 	/* temporary clone name must not exist */
11733b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
1174c1379625SJustin T. Gibbs 	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
11753b2aab18SMatthew Ahrens 	    8, 1, &val);
11763b2aab18SMatthew Ahrens 	if (error != ENOENT)
11773b2aab18SMatthew Ahrens 		return (error == 0 ? EBUSY : error);
11783b2aab18SMatthew Ahrens 
1179feaa74e4SMark Maybee 	/* new snapshot name must not exist */
11803b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
1181c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1182c1379625SJustin T. Gibbs 	    drba->drba_cookie->drc_tosnap, 8, 1, &val);
11833b2aab18SMatthew Ahrens 	if (error != ENOENT)
11843b2aab18SMatthew Ahrens 		return (error == 0 ? EEXIST : error);
1185feaa74e4SMark Maybee 
1186a2afb611SJerry Jelinek 	/*
1187a2afb611SJerry Jelinek 	 * Check snapshot limit before receiving. We'll recheck again at the
1188a2afb611SJerry Jelinek 	 * end, but might as well abort before receiving if we're already over
1189a2afb611SJerry Jelinek 	 * the limit.
1190a2afb611SJerry Jelinek 	 *
1191a2afb611SJerry Jelinek 	 * Note that we do not check the file system limit with
1192a2afb611SJerry Jelinek 	 * dsl_dir_fscount_check because the temporary %clones don't count
1193a2afb611SJerry Jelinek 	 * against that limit.
1194a2afb611SJerry Jelinek 	 */
1195a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1196a2afb611SJerry Jelinek 	    NULL, drba->drba_cred);
1197a2afb611SJerry Jelinek 	if (error != 0)
1198a2afb611SJerry Jelinek 		return (error);
1199a2afb611SJerry Jelinek 
12003b2aab18SMatthew Ahrens 	if (fromguid != 0) {
120134f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap;
1202c1379625SJustin T. Gibbs 		uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
120334f2f8cfSMatthew Ahrens 
120434f2f8cfSMatthew Ahrens 		/* Find snapshot in this dir that matches fromguid. */
120534f2f8cfSMatthew Ahrens 		while (obj != 0) {
120634f2f8cfSMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, obj, FTAG,
120734f2f8cfSMatthew Ahrens 			    &snap);
120834f2f8cfSMatthew Ahrens 			if (error != 0)
120934f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
121034f2f8cfSMatthew Ahrens 			if (snap->ds_dir != ds->ds_dir) {
121134f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
121234f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
121334f2f8cfSMatthew Ahrens 			}
1214c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(snap)->ds_guid == fromguid)
121534f2f8cfSMatthew Ahrens 				break;
1216c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
121734f2f8cfSMatthew Ahrens 			dsl_dataset_rele(snap, FTAG);
121834f2f8cfSMatthew Ahrens 		}
121934f2f8cfSMatthew Ahrens 		if (obj == 0)
1220be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
122192241e0bSTom Erickson 
122234f2f8cfSMatthew Ahrens 		if (drba->drba_cookie->drc_force) {
122334f2f8cfSMatthew Ahrens 			drba->drba_snapobj = obj;
122434f2f8cfSMatthew Ahrens 		} else {
122534f2f8cfSMatthew Ahrens 			/*
122634f2f8cfSMatthew Ahrens 			 * If we are not forcing, there must be no
122734f2f8cfSMatthew Ahrens 			 * changes since fromsnap.
122834f2f8cfSMatthew Ahrens 			 */
122934f2f8cfSMatthew Ahrens 			if (dsl_dataset_modified_since_snap(ds, snap)) {
123092241e0bSTom Erickson 				dsl_dataset_rele(snap, FTAG);
123134f2f8cfSMatthew Ahrens 				return (SET_ERROR(ETXTBSY));
123292241e0bSTom Erickson 			}
123334f2f8cfSMatthew Ahrens 			drba->drba_snapobj = ds->ds_prev->ds_object;
123492241e0bSTom Erickson 		}
123534f2f8cfSMatthew Ahrens 
123634f2f8cfSMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
1237ae46e4c7SMatthew Ahrens 	} else {
12385bae108fSAndriy Gapon 		/* if full, then must be forced */
12395bae108fSAndriy Gapon 		if (!drba->drba_cookie->drc_force)
12405bae108fSAndriy Gapon 			return (SET_ERROR(EEXIST));
12415bae108fSAndriy Gapon 		/* start from $ORIGIN@$ORIGIN, if supported */
12425bae108fSAndriy Gapon 		drba->drba_snapobj = dp->dp_origin_snap != NULL ?
12435bae108fSAndriy Gapon 		    dp->dp_origin_snap->ds_object : 0;
1244ae46e4c7SMatthew Ahrens 	}
12453cb34c60Sahrens 
12463cb34c60Sahrens 	return (0);
12473b2aab18SMatthew Ahrens 
12483b2aab18SMatthew Ahrens }
12493b2aab18SMatthew Ahrens 
12503b2aab18SMatthew Ahrens static int
12513b2aab18SMatthew Ahrens dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
12523b2aab18SMatthew Ahrens {
12533b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
12543b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
12553b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
12563b2aab18SMatthew Ahrens 	uint64_t fromguid = drrb->drr_fromguid;
12573b2aab18SMatthew Ahrens 	int flags = drrb->drr_flags;
12583b2aab18SMatthew Ahrens 	int error;
12595d7b4d43SMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
12603b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
12613b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
12623b2aab18SMatthew Ahrens 
12633b2aab18SMatthew Ahrens 	/* already checked */
12643b2aab18SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
12659c3fd121SMatthew Ahrens 	ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
12663b2aab18SMatthew Ahrens 
12673b2aab18SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
12683b2aab18SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
12693b2aab18SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
12703b2aab18SMatthew Ahrens 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1271be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12723b2aab18SMatthew Ahrens 
12733b2aab18SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
12745d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
12755d7b4d43SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
12765d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12775d7b4d43SMatthew Ahrens 
12789c3fd121SMatthew Ahrens 	if (drba->drba_cookie->drc_resumable &&
12799c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
12809c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12819c3fd121SMatthew Ahrens 
12825d7b4d43SMatthew Ahrens 	/*
12835d7b4d43SMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
12845d7b4d43SMatthew Ahrens 	 * record to a plan WRITE record, so the pool must have the
12855d7b4d43SMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
12865d7b4d43SMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
12875d7b4d43SMatthew Ahrens 	 */
12885d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
12895d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
12905d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12915d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
12925d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1293be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12943b2aab18SMatthew Ahrens 
1295b5152584SMatthew Ahrens 	/*
1296b5152584SMatthew Ahrens 	 * The receiving code doesn't know how to translate large blocks
1297b5152584SMatthew Ahrens 	 * to smaller ones, so the pool must have the LARGE_BLOCKS
1298b5152584SMatthew Ahrens 	 * feature enabled if the stream has LARGE_BLOCKS.
1299b5152584SMatthew Ahrens 	 */
1300b5152584SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1301b5152584SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1302b5152584SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1303b5152584SMatthew Ahrens 
13043b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
13053b2aab18SMatthew Ahrens 	if (error == 0) {
13063b2aab18SMatthew Ahrens 		/* target fs already exists; recv into temp clone */
13073b2aab18SMatthew Ahrens 
13083b2aab18SMatthew Ahrens 		/* Can't recv a clone into an existing fs */
130968ecb2ecSPaul Dagnelie 		if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
13103b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
1311be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
13123b2aab18SMatthew Ahrens 		}
13133b2aab18SMatthew Ahrens 
13143b2aab18SMatthew Ahrens 		error = recv_begin_check_existing_impl(drba, ds, fromguid);
13153b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
13163b2aab18SMatthew Ahrens 	} else if (error == ENOENT) {
13173b2aab18SMatthew Ahrens 		/* target fs does not exist; must be a full backup or clone */
13189adfa60dSMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
13193b2aab18SMatthew Ahrens 
13203b2aab18SMatthew Ahrens 		/*
13213b2aab18SMatthew Ahrens 		 * If it's a non-clone incremental, we are missing the
13223b2aab18SMatthew Ahrens 		 * target fs, so fail the recv.
13233b2aab18SMatthew Ahrens 		 */
1324a2cdcdd2SPaul Dagnelie 		if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1325a2cdcdd2SPaul Dagnelie 		    drba->drba_origin))
1326be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
13273b2aab18SMatthew Ahrens 
132868ecb2ecSPaul Dagnelie 		/*
132968ecb2ecSPaul Dagnelie 		 * If we're receiving a full send as a clone, and it doesn't
133068ecb2ecSPaul Dagnelie 		 * contain all the necessary free records and freeobject
133168ecb2ecSPaul Dagnelie 		 * records, reject it.
133268ecb2ecSPaul Dagnelie 		 */
133368ecb2ecSPaul Dagnelie 		if (fromguid == 0 && drba->drba_origin &&
133468ecb2ecSPaul Dagnelie 		    !(flags & DRR_FLAG_FREERECORDS))
133568ecb2ecSPaul Dagnelie 			return (SET_ERROR(EINVAL));
133668ecb2ecSPaul Dagnelie 
13373b2aab18SMatthew Ahrens 		/* Open the parent of tofs */
13389adfa60dSMatthew Ahrens 		ASSERT3U(strlen(tofs), <, sizeof (buf));
13393b2aab18SMatthew Ahrens 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
13403b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
13413b2aab18SMatthew Ahrens 		if (error != 0)
13423b2aab18SMatthew Ahrens 			return (error);
13433b2aab18SMatthew Ahrens 
1344a2afb611SJerry Jelinek 		/*
1345a2afb611SJerry Jelinek 		 * Check filesystem and snapshot limits before receiving. We'll
1346a2afb611SJerry Jelinek 		 * recheck snapshot limits again at the end (we create the
1347a2afb611SJerry Jelinek 		 * filesystems and increment those counts during begin_sync).
1348a2afb611SJerry Jelinek 		 */
1349a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1350a2afb611SJerry Jelinek 		    ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1351a2afb611SJerry Jelinek 		if (error != 0) {
1352a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1353a2afb611SJerry Jelinek 			return (error);
1354a2afb611SJerry Jelinek 		}
1355a2afb611SJerry Jelinek 
1356a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1357a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1358a2afb611SJerry Jelinek 		if (error != 0) {
1359a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1360a2afb611SJerry Jelinek 			return (error);
1361a2afb611SJerry Jelinek 		}
1362a2afb611SJerry Jelinek 
13633b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
13643b2aab18SMatthew Ahrens 			dsl_dataset_t *origin;
13653b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, drba->drba_origin,
13663b2aab18SMatthew Ahrens 			    FTAG, &origin);
13673b2aab18SMatthew Ahrens 			if (error != 0) {
13683b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
13693b2aab18SMatthew Ahrens 				return (error);
13703b2aab18SMatthew Ahrens 			}
1371bc9014e6SJustin Gibbs 			if (!origin->ds_is_snapshot) {
13723b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
13733b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1374be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
13753b2aab18SMatthew Ahrens 			}
137668ecb2ecSPaul Dagnelie 			if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
137768ecb2ecSPaul Dagnelie 			    fromguid != 0) {
13783b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
13793b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1380be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
13813b2aab18SMatthew Ahrens 			}
13823b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
13833b2aab18SMatthew Ahrens 		}
13843b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
13853b2aab18SMatthew Ahrens 		error = 0;
13863b2aab18SMatthew Ahrens 	}
13873b2aab18SMatthew Ahrens 	return (error);
1388f18faf3fSek }
1389f18faf3fSek 
1390f18faf3fSek static void
13913b2aab18SMatthew Ahrens dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1392f18faf3fSek {
13933b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
13943b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
13959c3fd121SMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
13963b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
13973b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
13983b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *newds;
1399f18faf3fSek 	uint64_t dsobj;
14003b2aab18SMatthew Ahrens 	int error;
14019c3fd121SMatthew Ahrens 	uint64_t crflags = 0;
14023b2aab18SMatthew Ahrens 
14039c3fd121SMatthew Ahrens 	if (drrb->drr_flags & DRR_FLAG_CI_DATA)
14049c3fd121SMatthew Ahrens 		crflags |= DS_FLAG_CI_DATASET;
1405f18faf3fSek 
14063b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
14073b2aab18SMatthew Ahrens 	if (error == 0) {
14083b2aab18SMatthew Ahrens 		/* create temporary clone */
140934f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap = NULL;
141034f2f8cfSMatthew Ahrens 		if (drba->drba_snapobj != 0) {
141134f2f8cfSMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
141234f2f8cfSMatthew Ahrens 			    drba->drba_snapobj, FTAG, &snap));
141334f2f8cfSMatthew Ahrens 		}
14143b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
141534f2f8cfSMatthew Ahrens 		    snap, crflags, drba->drba_cred, tx);
1416f40b29ceSPaul Dagnelie 		if (drba->drba_snapobj != 0)
1417f40b29ceSPaul Dagnelie 			dsl_dataset_rele(snap, FTAG);
14183b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
14193b2aab18SMatthew Ahrens 	} else {
14203b2aab18SMatthew Ahrens 		dsl_dir_t *dd;
14213b2aab18SMatthew Ahrens 		const char *tail;
14223b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
14233b2aab18SMatthew Ahrens 
14243b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
14253b2aab18SMatthew Ahrens 
14263b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
14273b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
14283b2aab18SMatthew Ahrens 			    FTAG, &origin));
14293b2aab18SMatthew Ahrens 		}
14303b2aab18SMatthew Ahrens 
14313b2aab18SMatthew Ahrens 		/* Create new dataset. */
14323b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(dd,
14333b2aab18SMatthew Ahrens 		    strrchr(tofs, '/') + 1,
14343b2aab18SMatthew Ahrens 		    origin, crflags, drba->drba_cred, tx);
14353b2aab18SMatthew Ahrens 		if (origin != NULL)
14363b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
14373b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
14383b2aab18SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
14393b2aab18SMatthew Ahrens 	}
14403b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
14413b2aab18SMatthew Ahrens 
14429c3fd121SMatthew Ahrens 	if (drba->drba_cookie->drc_resumable) {
14439c3fd121SMatthew Ahrens 		dsl_dataset_zapify(newds, tx);
14449c3fd121SMatthew Ahrens 		if (drrb->drr_fromguid != 0) {
14459c3fd121SMatthew Ahrens 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
14469c3fd121SMatthew Ahrens 			    8, 1, &drrb->drr_fromguid, tx));
14479c3fd121SMatthew Ahrens 		}
14489c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
14499c3fd121SMatthew Ahrens 		    8, 1, &drrb->drr_toguid, tx));
14509c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
14519c3fd121SMatthew Ahrens 		    1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
14529c3fd121SMatthew Ahrens 		uint64_t one = 1;
14539c3fd121SMatthew Ahrens 		uint64_t zero = 0;
14549c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
14559c3fd121SMatthew Ahrens 		    8, 1, &one, tx));
14569c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
14579c3fd121SMatthew Ahrens 		    8, 1, &zero, tx));
14589c3fd121SMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
14599c3fd121SMatthew Ahrens 		    8, 1, &zero, tx));
14609c3fd121SMatthew Ahrens 		if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
14619c3fd121SMatthew Ahrens 		    DMU_BACKUP_FEATURE_EMBED_DATA) {
14629c3fd121SMatthew Ahrens 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
14639c3fd121SMatthew Ahrens 			    8, 1, &one, tx));
14649c3fd121SMatthew Ahrens 		}
14659c3fd121SMatthew Ahrens 	}
14669c3fd121SMatthew Ahrens 
14673b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
1468c1379625SJustin T. Gibbs 	dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
14693cb34c60Sahrens 
1470ae46e4c7SMatthew Ahrens 	/*
1471ae46e4c7SMatthew Ahrens 	 * If we actually created a non-clone, we need to create the
1472ae46e4c7SMatthew Ahrens 	 * objset in our new dataset.
1473ae46e4c7SMatthew Ahrens 	 */
14743b2aab18SMatthew Ahrens 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1475ae46e4c7SMatthew Ahrens 		(void) dmu_objset_create_impl(dp->dp_spa,
14763b2aab18SMatthew Ahrens 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1477ae46e4c7SMatthew Ahrens 	}
1478ae46e4c7SMatthew Ahrens 
14793b2aab18SMatthew Ahrens 	drba->drba_cookie->drc_ds = newds;
1480dc7cd546SMark Shellenbaum 
14813b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(newds, "receive", tx, "");
1482dc7cd546SMark Shellenbaum }
1483dc7cd546SMark Shellenbaum 
14849c3fd121SMatthew Ahrens static int
14859c3fd121SMatthew Ahrens dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
14869c3fd121SMatthew Ahrens {
14879c3fd121SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
14889c3fd121SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
14899c3fd121SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
14909c3fd121SMatthew Ahrens 	int error;
14919c3fd121SMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
14929c3fd121SMatthew Ahrens 	dsl_dataset_t *ds;
14939c3fd121SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
14949c3fd121SMatthew Ahrens 
14959c3fd121SMatthew Ahrens 	/* already checked */
14969c3fd121SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
14979c3fd121SMatthew Ahrens 	ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
14989c3fd121SMatthew Ahrens 
14999c3fd121SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
15009c3fd121SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
15019c3fd121SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES)
15029c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15039c3fd121SMatthew Ahrens 
15049c3fd121SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
15059c3fd121SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
15069c3fd121SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
15079c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
15089c3fd121SMatthew Ahrens 
15099c3fd121SMatthew Ahrens 	/*
15109c3fd121SMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
15119c3fd121SMatthew Ahrens 	 * record to a plain WRITE record, so the pool must have the
15129c3fd121SMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
15139c3fd121SMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
15149c3fd121SMatthew Ahrens 	 */
15159c3fd121SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
15169c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
15179c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
15189c3fd121SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
15199c3fd121SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
15209c3fd121SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
15219c3fd121SMatthew Ahrens 
15229adfa60dSMatthew Ahrens 	/* 6 extra bytes for /%recv */
15239adfa60dSMatthew Ahrens 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
15249c3fd121SMatthew Ahrens 
15259c3fd121SMatthew Ahrens 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
15269c3fd121SMatthew Ahrens 	    tofs, recv_clone_name);
15279c3fd121SMatthew Ahrens 
15289c3fd121SMatthew Ahrens 	if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
15299c3fd121SMatthew Ahrens 		/* %recv does not exist; continue in tofs */
15309c3fd121SMatthew Ahrens 		error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
15319c3fd121SMatthew Ahrens 		if (error != 0)
15329c3fd121SMatthew Ahrens 			return (error);
15339c3fd121SMatthew Ahrens 	}
15349c3fd121SMatthew Ahrens 
15359c3fd121SMatthew Ahrens 	/* check that ds is marked inconsistent */
15369c3fd121SMatthew Ahrens 	if (!DS_IS_INCONSISTENT(ds)) {
15379c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15389c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15399c3fd121SMatthew Ahrens 	}
15409c3fd121SMatthew Ahrens 
15419c3fd121SMatthew Ahrens 	/* check that there is resuming data, and that the toguid matches */
15429c3fd121SMatthew Ahrens 	if (!dsl_dataset_is_zapified(ds)) {
15439c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15449c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15459c3fd121SMatthew Ahrens 	}
15469c3fd121SMatthew Ahrens 	uint64_t val;
15479c3fd121SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
15489c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
15499c3fd121SMatthew Ahrens 	if (error != 0 || drrb->drr_toguid != val) {
15509c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15519c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15529c3fd121SMatthew Ahrens 	}
15539c3fd121SMatthew Ahrens 
15549c3fd121SMatthew Ahrens 	/*
15559c3fd121SMatthew Ahrens 	 * Check if the receive is still running.  If so, it will be owned.
15569c3fd121SMatthew Ahrens 	 * Note that nothing else can own the dataset (e.g. after the receive
15579c3fd121SMatthew Ahrens 	 * fails) because it will be marked inconsistent.
15589c3fd121SMatthew Ahrens 	 */
15599c3fd121SMatthew Ahrens 	if (dsl_dataset_has_owner(ds)) {
15609c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15619c3fd121SMatthew Ahrens 		return (SET_ERROR(EBUSY));
15629c3fd121SMatthew Ahrens 	}
15639c3fd121SMatthew Ahrens 
15649c3fd121SMatthew Ahrens 	/* There should not be any snapshots of this fs yet. */
15659c3fd121SMatthew Ahrens 	if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
15669c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15679c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15689c3fd121SMatthew Ahrens 	}
15699c3fd121SMatthew Ahrens 
15709c3fd121SMatthew Ahrens 	/*
15719c3fd121SMatthew Ahrens 	 * Note: resume point will be checked when we process the first WRITE
15729c3fd121SMatthew Ahrens 	 * record.
15739c3fd121SMatthew Ahrens 	 */
15749c3fd121SMatthew Ahrens 
15759c3fd121SMatthew Ahrens 	/* check that the origin matches */
15769c3fd121SMatthew Ahrens 	val = 0;
15779c3fd121SMatthew Ahrens 	(void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
15789c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
15799c3fd121SMatthew Ahrens 	if (drrb->drr_fromguid != val) {
15809c3fd121SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
15819c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
15829c3fd121SMatthew Ahrens 	}
15839c3fd121SMatthew Ahrens 
15849c3fd121SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
15859c3fd121SMatthew Ahrens 	return (0);
15869c3fd121SMatthew Ahrens }
15879c3fd121SMatthew Ahrens 
15889c3fd121SMatthew Ahrens static void
15899c3fd121SMatthew Ahrens dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
15909c3fd121SMatthew Ahrens {
15919c3fd121SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
15929c3fd121SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15939c3fd121SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
15949c3fd121SMatthew Ahrens 	dsl_dataset_t *ds;
15959c3fd121SMatthew Ahrens 	uint64_t dsobj;
15969adfa60dSMatthew Ahrens 	/* 6 extra bytes for /%recv */
15979adfa60dSMatthew Ahrens 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
15989c3fd121SMatthew Ahrens 
15999c3fd121SMatthew Ahrens 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
16009c3fd121SMatthew Ahrens 	    tofs, recv_clone_name);
16019c3fd121SMatthew Ahrens 
16029c3fd121SMatthew Ahrens 	if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
16039c3fd121SMatthew Ahrens 		/* %recv does not exist; continue in tofs */
16049c3fd121SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
16059c3fd121SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
16069c3fd121SMatthew Ahrens 	}
16079c3fd121SMatthew Ahrens 
16089c3fd121SMatthew Ahrens 	/* clear the inconsistent flag so that we can own it */
16099c3fd121SMatthew Ahrens 	ASSERT(DS_IS_INCONSISTENT(ds));
16109c3fd121SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
16119c3fd121SMatthew Ahrens 	dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
16129c3fd121SMatthew Ahrens 	dsobj = ds->ds_object;
16139c3fd121SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
16149c3fd121SMatthew Ahrens 
16159c3fd121SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
16169c3fd121SMatthew Ahrens 
16179c3fd121SMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
16189c3fd121SMatthew Ahrens 	dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
16199c3fd121SMatthew Ahrens 
16209c3fd121SMatthew Ahrens 	ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
16219c3fd121SMatthew Ahrens 
16229c3fd121SMatthew Ahrens 	drba->drba_cookie->drc_ds = ds;
16239c3fd121SMatthew Ahrens 
16249c3fd121SMatthew Ahrens 	spa_history_log_internal_ds(ds, "resume receive", tx, "");
16259c3fd121SMatthew Ahrens }
16269c3fd121SMatthew Ahrens 
16273cb34c60Sahrens /*
16283cb34c60Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
16293cb34c60Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
16303cb34c60Sahrens  */
16313cb34c60Sahrens int
16329c3fd121SMatthew Ahrens dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
16339c3fd121SMatthew Ahrens     boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1634efb80947Sahrens {
16353b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t drba = { 0 };
1636ab04eb8eStimh 
16373cb34c60Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
16389c3fd121SMatthew Ahrens 	drc->drc_drr_begin = drr_begin;
16399c3fd121SMatthew Ahrens 	drc->drc_drrb = &drr_begin->drr_u.drr_begin;
16403cb34c60Sahrens 	drc->drc_tosnap = tosnap;
16413b2aab18SMatthew Ahrens 	drc->drc_tofs = tofs;
16423cb34c60Sahrens 	drc->drc_force = force;
16439c3fd121SMatthew Ahrens 	drc->drc_resumable = resumable;
1644a2afb611SJerry Jelinek 	drc->drc_cred = CRED();
1645efb80947Sahrens 
16469c3fd121SMatthew Ahrens 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
16473b2aab18SMatthew Ahrens 		drc->drc_byteswap = B_TRUE;
16489c3fd121SMatthew Ahrens 		fletcher_4_incremental_byteswap(drr_begin,
16493b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
16509c3fd121SMatthew Ahrens 		byteswap_record(drr_begin);
16519c3fd121SMatthew Ahrens 	} else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
16529c3fd121SMatthew Ahrens 		fletcher_4_incremental_native(drr_begin,
16533b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
16549c3fd121SMatthew Ahrens 	} else {
16559c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16563cb34c60Sahrens 	}
16573cb34c60Sahrens 
16583b2aab18SMatthew Ahrens 	drba.drba_origin = origin;
16593b2aab18SMatthew Ahrens 	drba.drba_cookie = drc;
16603b2aab18SMatthew Ahrens 	drba.drba_cred = CRED();
16613b2aab18SMatthew Ahrens 
16629c3fd121SMatthew Ahrens 	if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
16639c3fd121SMatthew Ahrens 	    DMU_BACKUP_FEATURE_RESUMING) {
16649c3fd121SMatthew Ahrens 		return (dsl_sync_task(tofs,
16659c3fd121SMatthew Ahrens 		    dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
16669c3fd121SMatthew Ahrens 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
16679c3fd121SMatthew Ahrens 	} else  {
16689c3fd121SMatthew Ahrens 		return (dsl_sync_task(tofs,
16699c3fd121SMatthew Ahrens 		    dmu_recv_begin_check, dmu_recv_begin_sync,
16709c3fd121SMatthew Ahrens 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
16719c3fd121SMatthew Ahrens 	}
1672efb80947Sahrens }
1673efb80947Sahrens 
1674a2cdcdd2SPaul Dagnelie struct receive_record_arg {
1675a2cdcdd2SPaul Dagnelie 	dmu_replay_record_t header;
1676a2cdcdd2SPaul Dagnelie 	void *payload; /* Pointer to a buffer containing the payload */
1677a2cdcdd2SPaul Dagnelie 	/*
1678a2cdcdd2SPaul Dagnelie 	 * If the record is a write, pointer to the arc_buf_t containing the
1679a2cdcdd2SPaul Dagnelie 	 * payload.
1680a2cdcdd2SPaul Dagnelie 	 */
1681a2cdcdd2SPaul Dagnelie 	arc_buf_t *write_buf;
1682a2cdcdd2SPaul Dagnelie 	int payload_size;
16839c3fd121SMatthew Ahrens 	uint64_t bytes_read; /* bytes read from stream when record created */
1684a2cdcdd2SPaul Dagnelie 	boolean_t eos_marker; /* Marks the end of the stream */
1685a2cdcdd2SPaul Dagnelie 	bqueue_node_t node;
1686a2cdcdd2SPaul Dagnelie };
1687a2cdcdd2SPaul Dagnelie 
1688a2cdcdd2SPaul Dagnelie struct receive_writer_arg {
168998110f08SMatthew Ahrens 	objset_t *os;
16903b2aab18SMatthew Ahrens 	boolean_t byteswap;
1691a2cdcdd2SPaul Dagnelie 	bqueue_t q;
16929c3fd121SMatthew Ahrens 
1693a2cdcdd2SPaul Dagnelie 	/*
1694a2cdcdd2SPaul Dagnelie 	 * These three args are used to signal to the main thread that we're
1695a2cdcdd2SPaul Dagnelie 	 * done.
1696a2cdcdd2SPaul Dagnelie 	 */
1697a2cdcdd2SPaul Dagnelie 	kmutex_t mutex;
1698a2cdcdd2SPaul Dagnelie 	kcondvar_t cv;
1699a2cdcdd2SPaul Dagnelie 	boolean_t done;
17009c3fd121SMatthew Ahrens 
1701a2cdcdd2SPaul Dagnelie 	int err;
1702a2cdcdd2SPaul Dagnelie 	/* A map from guid to dataset to help handle dedup'd streams. */
1703a2cdcdd2SPaul Dagnelie 	avl_tree_t *guid_to_ds_map;
17049c3fd121SMatthew Ahrens 	boolean_t resumable;
17059c3fd121SMatthew Ahrens 	uint64_t last_object, last_offset;
17069c3fd121SMatthew Ahrens 	uint64_t bytes_read; /* bytes read when current record created */
1707a2cdcdd2SPaul Dagnelie };
170898110f08SMatthew Ahrens 
170968ecb2ecSPaul Dagnelie struct objlist {
171068ecb2ecSPaul Dagnelie 	list_t list; /* List of struct receive_objnode. */
171168ecb2ecSPaul Dagnelie 	/*
171268ecb2ecSPaul Dagnelie 	 * Last object looked up. Used to assert that objects are being looked
171368ecb2ecSPaul Dagnelie 	 * up in ascending order.
171468ecb2ecSPaul Dagnelie 	 */
171568ecb2ecSPaul Dagnelie 	uint64_t last_lookup;
171668ecb2ecSPaul Dagnelie };
171768ecb2ecSPaul Dagnelie 
171868ecb2ecSPaul Dagnelie struct receive_objnode {
171968ecb2ecSPaul Dagnelie 	list_node_t node;
172068ecb2ecSPaul Dagnelie 	uint64_t object;
172168ecb2ecSPaul Dagnelie };
172268ecb2ecSPaul Dagnelie 
1723a2cdcdd2SPaul Dagnelie struct receive_arg  {
1724a2cdcdd2SPaul Dagnelie 	objset_t *os;
1725a2cdcdd2SPaul Dagnelie 	vnode_t *vp; /* The vnode to read the stream from */
1726a2cdcdd2SPaul Dagnelie 	uint64_t voff; /* The current offset in the stream */
17279c3fd121SMatthew Ahrens 	uint64_t bytes_read;
1728a2cdcdd2SPaul Dagnelie 	/*
1729a2cdcdd2SPaul Dagnelie 	 * A record that has had its payload read in, but hasn't yet been handed
1730a2cdcdd2SPaul Dagnelie 	 * off to the worker thread.
1731a2cdcdd2SPaul Dagnelie 	 */
1732a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *rrd;
1733a2cdcdd2SPaul Dagnelie 	/* A record that has had its header read in, but not its payload. */
1734a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *next_rrd;
17353cb34c60Sahrens 	zio_cksum_t cksum;
173698110f08SMatthew Ahrens 	zio_cksum_t prev_cksum;
1737a2cdcdd2SPaul Dagnelie 	int err;
1738a2cdcdd2SPaul Dagnelie 	boolean_t byteswap;
1739a2cdcdd2SPaul Dagnelie 	/* Sorted list of objects not to issue prefetches for. */
174068ecb2ecSPaul Dagnelie 	struct objlist ignore_objlist;
17413cb34c60Sahrens };
17423cb34c60Sahrens 
17439e69d7d0SLori Alt typedef struct guid_map_entry {
17449e69d7d0SLori Alt 	uint64_t	guid;
17459e69d7d0SLori Alt 	dsl_dataset_t	*gme_ds;
17469e69d7d0SLori Alt 	avl_node_t	avlnode;
17479e69d7d0SLori Alt } guid_map_entry_t;
17489e69d7d0SLori Alt 
17499e69d7d0SLori Alt static int
17509e69d7d0SLori Alt guid_compare(const void *arg1, const void *arg2)
17519e69d7d0SLori Alt {
17529e69d7d0SLori Alt 	const guid_map_entry_t *gmep1 = arg1;
17539e69d7d0SLori Alt 	const guid_map_entry_t *gmep2 = arg2;
17549e69d7d0SLori Alt 
17559e69d7d0SLori Alt 	if (gmep1->guid < gmep2->guid)
17569e69d7d0SLori Alt 		return (-1);
17579e69d7d0SLori Alt 	else if (gmep1->guid > gmep2->guid)
17589e69d7d0SLori Alt 		return (1);
17599e69d7d0SLori Alt 	return (0);
17609e69d7d0SLori Alt }
17619e69d7d0SLori Alt 
1762c99e4bdcSChris Kirby static void
1763c99e4bdcSChris Kirby free_guid_map_onexit(void *arg)
1764c99e4bdcSChris Kirby {
1765c99e4bdcSChris Kirby 	avl_tree_t *ca = arg;
1766c99e4bdcSChris Kirby 	void *cookie = NULL;
1767c99e4bdcSChris Kirby 	guid_map_entry_t *gmep;
1768c99e4bdcSChris Kirby 
1769c99e4bdcSChris Kirby 	while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
17703b2aab18SMatthew Ahrens 		dsl_dataset_long_rele(gmep->gme_ds, gmep);
1771de8d9cffSMatthew Ahrens 		dsl_dataset_rele(gmep->gme_ds, gmep);
1772c99e4bdcSChris Kirby 		kmem_free(gmep, sizeof (guid_map_entry_t));
1773c99e4bdcSChris Kirby 	}
1774c99e4bdcSChris Kirby 	avl_destroy(ca);
1775c99e4bdcSChris Kirby 	kmem_free(ca, sizeof (avl_tree_t));
1776c99e4bdcSChris Kirby }
1777c99e4bdcSChris Kirby 
177898110f08SMatthew Ahrens static int
1779a2cdcdd2SPaul Dagnelie receive_read(struct receive_arg *ra, int len, void *buf)
1780efb80947Sahrens {
17813cb34c60Sahrens 	int done = 0;
1782efb80947Sahrens 
1783c20404ffSEli Rosenthal 	/*
1784c20404ffSEli Rosenthal 	 * The code doesn't rely on this (lengths being multiples of 8).  See
1785c20404ffSEli Rosenthal 	 * comment in dump_bytes.
1786c20404ffSEli Rosenthal 	 */
1787fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
1788efb80947Sahrens 
17893cb34c60Sahrens 	while (done < len) {
1790efb80947Sahrens 		ssize_t resid;
1791efb80947Sahrens 
1792efb80947Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
179398110f08SMatthew Ahrens 		    (char *)buf + done, len - done,
1794efb80947Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
1795efb80947Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
1796efb80947Sahrens 
17979c3fd121SMatthew Ahrens 		if (resid == len - done) {
17989c3fd121SMatthew Ahrens 			/*
17999c3fd121SMatthew Ahrens 			 * Note: ECKSUM indicates that the receive
18009c3fd121SMatthew Ahrens 			 * was interrupted and can potentially be resumed.
18019c3fd121SMatthew Ahrens 			 */
18029c3fd121SMatthew Ahrens 			ra->err = SET_ERROR(ECKSUM);
18039c3fd121SMatthew Ahrens 		}
18043cb34c60Sahrens 		ra->voff += len - done - resid;
18053cb34c60Sahrens 		done = len - resid;
18063b2aab18SMatthew Ahrens 		if (ra->err != 0)
180798110f08SMatthew Ahrens 			return (ra->err);
1808efb80947Sahrens 	}
1809efb80947Sahrens 
18109c3fd121SMatthew Ahrens 	ra->bytes_read += len;
18119c3fd121SMatthew Ahrens 
18123cb34c60Sahrens 	ASSERT3U(done, ==, len);
181398110f08SMatthew Ahrens 	return (0);
1814efb80947Sahrens }
1815efb80947Sahrens 
1816efb80947Sahrens static void
181798110f08SMatthew Ahrens byteswap_record(dmu_replay_record_t *drr)
1818efb80947Sahrens {
1819efb80947Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1820efb80947Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1821efb80947Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
18223cb34c60Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
182398110f08SMatthew Ahrens 
1824efb80947Sahrens 	switch (drr->drr_type) {
1825efb80947Sahrens 	case DRR_BEGIN:
1826efb80947Sahrens 		DO64(drr_begin.drr_magic);
18279e69d7d0SLori Alt 		DO64(drr_begin.drr_versioninfo);
1828efb80947Sahrens 		DO64(drr_begin.drr_creation_time);
1829efb80947Sahrens 		DO32(drr_begin.drr_type);
18303cb34c60Sahrens 		DO32(drr_begin.drr_flags);
1831efb80947Sahrens 		DO64(drr_begin.drr_toguid);
1832efb80947Sahrens 		DO64(drr_begin.drr_fromguid);
1833efb80947Sahrens 		break;
1834efb80947Sahrens 	case DRR_OBJECT:
1835efb80947Sahrens 		DO64(drr_object.drr_object);
1836efb80947Sahrens 		DO32(drr_object.drr_type);
1837efb80947Sahrens 		DO32(drr_object.drr_bonustype);
1838efb80947Sahrens 		DO32(drr_object.drr_blksz);
1839efb80947Sahrens 		DO32(drr_object.drr_bonuslen);
18409e69d7d0SLori Alt 		DO64(drr_object.drr_toguid);
1841efb80947Sahrens 		break;
1842efb80947Sahrens 	case DRR_FREEOBJECTS:
1843efb80947Sahrens 		DO64(drr_freeobjects.drr_firstobj);
1844efb80947Sahrens 		DO64(drr_freeobjects.drr_numobjs);
18459e69d7d0SLori Alt 		DO64(drr_freeobjects.drr_toguid);
1846efb80947Sahrens 		break;
1847efb80947Sahrens 	case DRR_WRITE:
1848efb80947Sahrens 		DO64(drr_write.drr_object);
1849efb80947Sahrens 		DO32(drr_write.drr_type);
1850efb80947Sahrens 		DO64(drr_write.drr_offset);
1851efb80947Sahrens 		DO64(drr_write.drr_length);
18529e69d7d0SLori Alt 		DO64(drr_write.drr_toguid);
185398110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
18548e714474SLori Alt 		DO64(drr_write.drr_key.ddk_prop);
18559e69d7d0SLori Alt 		break;
18569e69d7d0SLori Alt 	case DRR_WRITE_BYREF:
18579e69d7d0SLori Alt 		DO64(drr_write_byref.drr_object);
18589e69d7d0SLori Alt 		DO64(drr_write_byref.drr_offset);
18599e69d7d0SLori Alt 		DO64(drr_write_byref.drr_length);
18609e69d7d0SLori Alt 		DO64(drr_write_byref.drr_toguid);
18619e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refguid);
18629e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refobject);
18639e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refoffset);
186498110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
186598110f08SMatthew Ahrens 		    drr_key.ddk_cksum);
18668e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_prop);
1867efb80947Sahrens 		break;
18685d7b4d43SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
18695d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_object);
18705d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_offset);
18715d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_length);
18725d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_toguid);
18735d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_lsize);
18745d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_psize);
18755d7b4d43SMatthew Ahrens 		break;
1876efb80947Sahrens 	case DRR_FREE:
1877efb80947Sahrens 		DO64(drr_free.drr_object);
1878efb80947Sahrens 		DO64(drr_free.drr_offset);
1879efb80947Sahrens 		DO64(drr_free.drr_length);
18809e69d7d0SLori Alt 		DO64(drr_free.drr_toguid);
1881efb80947Sahrens 		break;
18820a586ceaSMark Shellenbaum 	case DRR_SPILL:
18830a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_object);
18840a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_length);
18850a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_toguid);
18860a586ceaSMark Shellenbaum 		break;
1887efb80947Sahrens 	case DRR_END:
18889e69d7d0SLori Alt 		DO64(drr_end.drr_toguid);
188998110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1890efb80947Sahrens 		break;
1891efb80947Sahrens 	}
189298110f08SMatthew Ahrens 
189398110f08SMatthew Ahrens 	if (drr->drr_type != DRR_BEGIN) {
189498110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
189598110f08SMatthew Ahrens 	}
189698110f08SMatthew Ahrens 
1897efb80947Sahrens #undef DO64
1898efb80947Sahrens #undef DO32
1899efb80947Sahrens }
1900efb80947Sahrens 
1901e77d42eaSMatthew Ahrens static inline uint8_t
1902e77d42eaSMatthew Ahrens deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1903e77d42eaSMatthew Ahrens {
1904e77d42eaSMatthew Ahrens 	if (bonus_type == DMU_OT_SA) {
1905e77d42eaSMatthew Ahrens 		return (1);
1906e77d42eaSMatthew Ahrens 	} else {
1907e77d42eaSMatthew Ahrens 		return (1 +
1908e77d42eaSMatthew Ahrens 		    ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
1909e77d42eaSMatthew Ahrens 	}
1910e77d42eaSMatthew Ahrens }
1911e77d42eaSMatthew Ahrens 
19129c3fd121SMatthew Ahrens static void
19139c3fd121SMatthew Ahrens save_resume_state(struct receive_writer_arg *rwa,
19149c3fd121SMatthew Ahrens     uint64_t object, uint64_t offset, dmu_tx_t *tx)
19159c3fd121SMatthew Ahrens {
19169c3fd121SMatthew Ahrens 	int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
19179c3fd121SMatthew Ahrens 
19189c3fd121SMatthew Ahrens 	if (!rwa->resumable)
19199c3fd121SMatthew Ahrens 		return;
19209c3fd121SMatthew Ahrens 
19219c3fd121SMatthew Ahrens 	/*
19229c3fd121SMatthew Ahrens 	 * We use ds_resume_bytes[] != 0 to indicate that we need to
19239c3fd121SMatthew Ahrens 	 * update this on disk, so it must not be 0.
19249c3fd121SMatthew Ahrens 	 */
19259c3fd121SMatthew Ahrens 	ASSERT(rwa->bytes_read != 0);
19269c3fd121SMatthew Ahrens 
19279c3fd121SMatthew Ahrens 	/*
19289c3fd121SMatthew Ahrens 	 * We only resume from write records, which have a valid
19299c3fd121SMatthew Ahrens 	 * (non-meta-dnode) object number.
19309c3fd121SMatthew Ahrens 	 */
19319c3fd121SMatthew Ahrens 	ASSERT(object != 0);
19329c3fd121SMatthew Ahrens 
19339c3fd121SMatthew Ahrens 	/*
19349c3fd121SMatthew Ahrens 	 * For resuming to work correctly, we must receive records in order,
19359c3fd121SMatthew Ahrens 	 * sorted by object,offset.  This is checked by the callers, but
19369c3fd121SMatthew Ahrens 	 * assert it here for good measure.
19379c3fd121SMatthew Ahrens 	 */
19389c3fd121SMatthew Ahrens 	ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
19399c3fd121SMatthew Ahrens 	ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
19409c3fd121SMatthew Ahrens 	    offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
19419c3fd121SMatthew Ahrens 	ASSERT3U(rwa->bytes_read, >=,
19429c3fd121SMatthew Ahrens 	    rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
19439c3fd121SMatthew Ahrens 
19449c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
19459c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
19469c3fd121SMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
19479c3fd121SMatthew Ahrens }
19489c3fd121SMatthew Ahrens 
1949efb80947Sahrens static int
1950a2cdcdd2SPaul Dagnelie receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1951a2cdcdd2SPaul Dagnelie     void *data)
1952efb80947Sahrens {
1953e77d42eaSMatthew Ahrens 	dmu_object_info_t doi;
1954efb80947Sahrens 	dmu_tx_t *tx;
1955e77d42eaSMatthew Ahrens 	uint64_t object;
1956e77d42eaSMatthew Ahrens 	int err;
1957efb80947Sahrens 
1958efb80947Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
1959ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_type) ||
1960ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
19619e69d7d0SLori Alt 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1962efb80947Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1963efb80947Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1964efb80947Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
1965a2cdcdd2SPaul Dagnelie 	    drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
1966efb80947Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1967be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1968efb80947Sahrens 	}
1969efb80947Sahrens 
1970a2cdcdd2SPaul Dagnelie 	err = dmu_object_info(rwa->os, drro->drr_object, &doi);
19712bf405a2SMark Maybee 
19722bf405a2SMark Maybee 	if (err != 0 && err != ENOENT)
1973be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1974e77d42eaSMatthew Ahrens 	object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
19752bf405a2SMark Maybee 
1976e77d42eaSMatthew Ahrens 	/*
1977e77d42eaSMatthew Ahrens 	 * If we are losing blkptrs or changing the block size this must
1978e77d42eaSMatthew Ahrens 	 * be a new file instance.  We must clear out the previous file
1979e77d42eaSMatthew Ahrens 	 * contents before we can change this type of metadata in the dnode.
1980e77d42eaSMatthew Ahrens 	 */
1981e77d42eaSMatthew Ahrens 	if (err == 0) {
1982e77d42eaSMatthew Ahrens 		int nblkptr;
1983e77d42eaSMatthew Ahrens 
1984e77d42eaSMatthew Ahrens 		nblkptr = deduce_nblkptr(drro->drr_bonustype,
1985e77d42eaSMatthew Ahrens 		    drro->drr_bonuslen);
1986e77d42eaSMatthew Ahrens 
1987e77d42eaSMatthew Ahrens 		if (drro->drr_blksz != doi.doi_data_block_size ||
1988e77d42eaSMatthew Ahrens 		    nblkptr < doi.doi_nblkptr) {
1989a2cdcdd2SPaul Dagnelie 			err = dmu_free_long_range(rwa->os, drro->drr_object,
1990e77d42eaSMatthew Ahrens 			    0, DMU_OBJECT_END);
1991e77d42eaSMatthew Ahrens 			if (err != 0)
1992e77d42eaSMatthew Ahrens 				return (SET_ERROR(EINVAL));
1993efb80947Sahrens 		}
1994e77d42eaSMatthew Ahrens 	}
1995e77d42eaSMatthew Ahrens 
1996a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
1997e77d42eaSMatthew Ahrens 	dmu_tx_hold_bonus(tx, object);
1998e77d42eaSMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
1999e77d42eaSMatthew Ahrens 	if (err != 0) {
2000e77d42eaSMatthew Ahrens 		dmu_tx_abort(tx);
2001e77d42eaSMatthew Ahrens 		return (err);
2002e77d42eaSMatthew Ahrens 	}
2003e77d42eaSMatthew Ahrens 
2004e77d42eaSMatthew Ahrens 	if (object == DMU_NEW_OBJECT) {
2005e77d42eaSMatthew Ahrens 		/* currently free, want to be allocated */
2006a2cdcdd2SPaul Dagnelie 		err = dmu_object_claim(rwa->os, drro->drr_object,
2007efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
2008efb80947Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
2009e77d42eaSMatthew Ahrens 	} else if (drro->drr_type != doi.doi_type ||
2010e77d42eaSMatthew Ahrens 	    drro->drr_blksz != doi.doi_data_block_size ||
2011e77d42eaSMatthew Ahrens 	    drro->drr_bonustype != doi.doi_bonus_type ||
2012e77d42eaSMatthew Ahrens 	    drro->drr_bonuslen != doi.doi_bonus_size) {
2013e77d42eaSMatthew Ahrens 		/* currently allocated, but with different properties */
2014a2cdcdd2SPaul Dagnelie 		err = dmu_object_reclaim(rwa->os, drro->drr_object,
2015efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
2016e77d42eaSMatthew Ahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
2017efb80947Sahrens 	}
20183b2aab18SMatthew Ahrens 	if (err != 0) {
2019e77d42eaSMatthew Ahrens 		dmu_tx_commit(tx);
2020be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
20210a586ceaSMark Shellenbaum 	}
20222bf405a2SMark Maybee 
2023a2cdcdd2SPaul Dagnelie 	dmu_object_set_checksum(rwa->os, drro->drr_object,
202498110f08SMatthew Ahrens 	    drro->drr_checksumtype, tx);
2025a2cdcdd2SPaul Dagnelie 	dmu_object_set_compress(rwa->os, drro->drr_object,
202698110f08SMatthew Ahrens 	    drro->drr_compress, tx);
2027efb80947Sahrens 
2028adee0b6fSTim Haley 	if (data != NULL) {
2029efb80947Sahrens 		dmu_buf_t *db;
2030adee0b6fSTim Haley 
2031a2cdcdd2SPaul Dagnelie 		VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
2032efb80947Sahrens 		dmu_buf_will_dirty(db, tx);
2033efb80947Sahrens 
20341934e92fSmaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
20351934e92fSmaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
2036a2cdcdd2SPaul Dagnelie 		if (rwa->byteswap) {
2037ad135b5dSChristopher Siden 			dmu_object_byteswap_t byteswap =
2038ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
2039ad135b5dSChristopher Siden 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2040efb80947Sahrens 			    drro->drr_bonuslen);
2041efb80947Sahrens 		}
2042efb80947Sahrens 		dmu_buf_rele(db, FTAG);
2043efb80947Sahrens 	}
2044efb80947Sahrens 	dmu_tx_commit(tx);
20459c3fd121SMatthew Ahrens 
2046efb80947Sahrens 	return (0);
2047efb80947Sahrens }
2048efb80947Sahrens 
2049efb80947Sahrens /* ARGSUSED */
2050efb80947Sahrens static int
2051a2cdcdd2SPaul Dagnelie receive_freeobjects(struct receive_writer_arg *rwa,
2052efb80947Sahrens     struct drr_freeobjects *drrfo)
2053efb80947Sahrens {
2054efb80947Sahrens 	uint64_t obj;
205568ecb2ecSPaul Dagnelie 	int next_err = 0;
2056efb80947Sahrens 
2057efb80947Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2058be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2059efb80947Sahrens 
2060efb80947Sahrens 	for (obj = drrfo->drr_firstobj;
206168ecb2ecSPaul Dagnelie 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
206268ecb2ecSPaul Dagnelie 	    next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2063efb80947Sahrens 		int err;
2064efb80947Sahrens 
2065a2cdcdd2SPaul Dagnelie 		if (dmu_object_info(rwa->os, obj, NULL) != 0)
2066efb80947Sahrens 			continue;
2067efb80947Sahrens 
2068a2cdcdd2SPaul Dagnelie 		err = dmu_free_long_object(rwa->os, obj);
20693b2aab18SMatthew Ahrens 		if (err != 0)
2070efb80947Sahrens 			return (err);
2071efb80947Sahrens 	}
207268ecb2ecSPaul Dagnelie 	if (next_err != ESRCH)
207368ecb2ecSPaul Dagnelie 		return (next_err);
2074efb80947Sahrens 	return (0);
2075efb80947Sahrens }
2076efb80947Sahrens 
2077efb80947Sahrens static int
2078a2cdcdd2SPaul Dagnelie receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2079a2cdcdd2SPaul Dagnelie     arc_buf_t *abuf)
2080efb80947Sahrens {
2081efb80947Sahrens 	dmu_tx_t *tx;
2082efb80947Sahrens 	int err;
2083efb80947Sahrens 
2084efb80947Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
2085ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drrw->drr_type))
2086be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2087efb80947Sahrens 
20889c3fd121SMatthew Ahrens 	/*
20899c3fd121SMatthew Ahrens 	 * For resuming to work, records must be in increasing order
20909c3fd121SMatthew Ahrens 	 * by (object, offset).
20919c3fd121SMatthew Ahrens 	 */
20929c3fd121SMatthew Ahrens 	if (drrw->drr_object < rwa->last_object ||
20939c3fd121SMatthew Ahrens 	    (drrw->drr_object == rwa->last_object &&
20949c3fd121SMatthew Ahrens 	    drrw->drr_offset < rwa->last_offset)) {
20959c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
20969c3fd121SMatthew Ahrens 	}
20979c3fd121SMatthew Ahrens 	rwa->last_object = drrw->drr_object;
20989c3fd121SMatthew Ahrens 	rwa->last_offset = drrw->drr_offset;
20999c3fd121SMatthew Ahrens 
2100a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
21018a904709SMatthew Ahrens 		return (SET_ERROR(EINVAL));
21028a904709SMatthew Ahrens 
2103a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
2104efb80947Sahrens 
2105efb80947Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
2106efb80947Sahrens 	    drrw->drr_offset, drrw->drr_length);
2107efb80947Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
21083b2aab18SMatthew Ahrens 	if (err != 0) {
2109efb80947Sahrens 		dmu_tx_abort(tx);
2110efb80947Sahrens 		return (err);
2111efb80947Sahrens 	}
2112a2cdcdd2SPaul Dagnelie 	if (rwa->byteswap) {
2113ad135b5dSChristopher Siden 		dmu_object_byteswap_t byteswap =
2114ad135b5dSChristopher Siden 		    DMU_OT_BYTESWAP(drrw->drr_type);
211598110f08SMatthew Ahrens 		dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
211698110f08SMatthew Ahrens 		    drrw->drr_length);
2117ad135b5dSChristopher Siden 	}
211898110f08SMatthew Ahrens 
211998110f08SMatthew Ahrens 	dmu_buf_t *bonus;
2120a2cdcdd2SPaul Dagnelie 	if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
212198110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
21228a904709SMatthew Ahrens 	dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
21239c3fd121SMatthew Ahrens 
21249c3fd121SMatthew Ahrens 	/*
21259c3fd121SMatthew Ahrens 	 * Note: If the receive fails, we want the resume stream to start
21269c3fd121SMatthew Ahrens 	 * with the same record that we last successfully received (as opposed
21279c3fd121SMatthew Ahrens 	 * to the next record), so that we can verify that we are
21289c3fd121SMatthew Ahrens 	 * resuming from the correct location.
21299c3fd121SMatthew Ahrens 	 */
21309c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2131efb80947Sahrens 	dmu_tx_commit(tx);
21328a904709SMatthew Ahrens 	dmu_buf_rele(bonus, FTAG);
21339c3fd121SMatthew Ahrens 
2134efb80947Sahrens 	return (0);
2135efb80947Sahrens }
2136efb80947Sahrens 
21379e69d7d0SLori Alt /*
21389e69d7d0SLori Alt  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
21399e69d7d0SLori Alt  * streams to refer to a copy of the data that is already on the
21409e69d7d0SLori Alt  * system because it came in earlier in the stream.  This function
21419e69d7d0SLori Alt  * finds the earlier copy of the data, and uses that copy instead of
21429e69d7d0SLori Alt  * data from the stream to fulfill this write.
21439e69d7d0SLori Alt  */
21449e69d7d0SLori Alt static int
2145a2cdcdd2SPaul Dagnelie receive_write_byref(struct receive_writer_arg *rwa,
2146a2cdcdd2SPaul Dagnelie     struct drr_write_byref *drrwbr)
21479e69d7d0SLori Alt {
21489e69d7d0SLori Alt 	dmu_tx_t *tx;
21499e69d7d0SLori Alt 	int err;
21509e69d7d0SLori Alt 	guid_map_entry_t gmesrch;
21519e69d7d0SLori Alt 	guid_map_entry_t *gmep;
21525d7b4d43SMatthew Ahrens 	avl_index_t where;
21539e69d7d0SLori Alt 	objset_t *ref_os = NULL;
21549e69d7d0SLori Alt 	dmu_buf_t *dbp;
21559e69d7d0SLori Alt 
21569e69d7d0SLori Alt 	if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2157be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21589e69d7d0SLori Alt 
21599e69d7d0SLori Alt 	/*
21609e69d7d0SLori Alt 	 * If the GUID of the referenced dataset is different from the
21619e69d7d0SLori Alt 	 * GUID of the target dataset, find the referenced dataset.
21629e69d7d0SLori Alt 	 */
21639e69d7d0SLori Alt 	if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
21649e69d7d0SLori Alt 		gmesrch.guid = drrwbr->drr_refguid;
2165a2cdcdd2SPaul Dagnelie 		if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
21669e69d7d0SLori Alt 		    &where)) == NULL) {
2167be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
21689e69d7d0SLori Alt 		}
21699e69d7d0SLori Alt 		if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2170be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
21719e69d7d0SLori Alt 	} else {
2172a2cdcdd2SPaul Dagnelie 		ref_os = rwa->os;
21739e69d7d0SLori Alt 	}
21749e69d7d0SLori Alt 
21755d7b4d43SMatthew Ahrens 	err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
21765d7b4d43SMatthew Ahrens 	    drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
21775d7b4d43SMatthew Ahrens 	if (err != 0)
21789e69d7d0SLori Alt 		return (err);
21799e69d7d0SLori Alt 
2180a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
21819e69d7d0SLori Alt 
21829e69d7d0SLori Alt 	dmu_tx_hold_write(tx, drrwbr->drr_object,
21839e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length);
21849e69d7d0SLori Alt 	err = dmu_tx_assign(tx, TXG_WAIT);
21853b2aab18SMatthew Ahrens 	if (err != 0) {
21869e69d7d0SLori Alt 		dmu_tx_abort(tx);
21879e69d7d0SLori Alt 		return (err);
21889e69d7d0SLori Alt 	}
2189a2cdcdd2SPaul Dagnelie 	dmu_write(rwa->os, drrwbr->drr_object,
21909e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
21919e69d7d0SLori Alt 	dmu_buf_rele(dbp, FTAG);
21929c3fd121SMatthew Ahrens 
21939c3fd121SMatthew Ahrens 	/* See comment in restore_write. */
21949c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
21959e69d7d0SLori Alt 	dmu_tx_commit(tx);
21969e69d7d0SLori Alt 	return (0);
21979e69d7d0SLori Alt }
21989e69d7d0SLori Alt 
21995d7b4d43SMatthew Ahrens static int
2200a2cdcdd2SPaul Dagnelie receive_write_embedded(struct receive_writer_arg *rwa,
22019c3fd121SMatthew Ahrens     struct drr_write_embedded *drrwe, void *data)
22025d7b4d43SMatthew Ahrens {
22035d7b4d43SMatthew Ahrens 	dmu_tx_t *tx;
22045d7b4d43SMatthew Ahrens 	int err;
22055d7b4d43SMatthew Ahrens 
22069c3fd121SMatthew Ahrens 	if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
22075d7b4d43SMatthew Ahrens 		return (EINVAL);
22085d7b4d43SMatthew Ahrens 
22099c3fd121SMatthew Ahrens 	if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
22105d7b4d43SMatthew Ahrens 		return (EINVAL);
22115d7b4d43SMatthew Ahrens 
22129c3fd121SMatthew Ahrens 	if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
22135d7b4d43SMatthew Ahrens 		return (EINVAL);
22149c3fd121SMatthew Ahrens 	if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
22155d7b4d43SMatthew Ahrens 		return (EINVAL);
22165d7b4d43SMatthew Ahrens 
2217a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
22185d7b4d43SMatthew Ahrens 
22199c3fd121SMatthew Ahrens 	dmu_tx_hold_write(tx, drrwe->drr_object,
22209c3fd121SMatthew Ahrens 	    drrwe->drr_offset, drrwe->drr_length);
22215d7b4d43SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
22225d7b4d43SMatthew Ahrens 	if (err != 0) {
22235d7b4d43SMatthew Ahrens 		dmu_tx_abort(tx);
22245d7b4d43SMatthew Ahrens 		return (err);
22255d7b4d43SMatthew Ahrens 	}
22265d7b4d43SMatthew Ahrens 
22279c3fd121SMatthew Ahrens 	dmu_write_embedded(rwa->os, drrwe->drr_object,
22289c3fd121SMatthew Ahrens 	    drrwe->drr_offset, data, drrwe->drr_etype,
22299c3fd121SMatthew Ahrens 	    drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2230a2cdcdd2SPaul Dagnelie 	    rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
22315d7b4d43SMatthew Ahrens 
22329c3fd121SMatthew Ahrens 	/* See comment in restore_write. */
22339c3fd121SMatthew Ahrens 	save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
22345d7b4d43SMatthew Ahrens 	dmu_tx_commit(tx);
22355d7b4d43SMatthew Ahrens 	return (0);
22365d7b4d43SMatthew Ahrens }
22375d7b4d43SMatthew Ahrens 
22380a586ceaSMark Shellenbaum static int
2239a2cdcdd2SPaul Dagnelie receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2240a2cdcdd2SPaul Dagnelie     void *data)
22410a586ceaSMark Shellenbaum {
22420a586ceaSMark Shellenbaum 	dmu_tx_t *tx;
22430a586ceaSMark Shellenbaum 	dmu_buf_t *db, *db_spill;
22440a586ceaSMark Shellenbaum 	int err;
22450a586ceaSMark Shellenbaum 
22460a586ceaSMark Shellenbaum 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2247a2cdcdd2SPaul Dagnelie 	    drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2248be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
22490a586ceaSMark Shellenbaum 
2250a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2251be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
22520a586ceaSMark Shellenbaum 
2253a2cdcdd2SPaul Dagnelie 	VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
22540a586ceaSMark Shellenbaum 	if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
22550a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
22560a586ceaSMark Shellenbaum 		return (err);
22570a586ceaSMark Shellenbaum 	}
22580a586ceaSMark Shellenbaum 
2259a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
22600a586ceaSMark Shellenbaum 
22610a586ceaSMark Shellenbaum 	dmu_tx_hold_spill(tx, db->db_object);
22620a586ceaSMark Shellenbaum 
22630a586ceaSMark Shellenbaum 	err = dmu_tx_assign(tx, TXG_WAIT);
22643b2aab18SMatthew Ahrens 	if (err != 0) {
22650a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
22660a586ceaSMark Shellenbaum 		dmu_buf_rele(db_spill, FTAG);
22670a586ceaSMark Shellenbaum 		dmu_tx_abort(tx);
22680a586ceaSMark Shellenbaum 		return (err);
22690a586ceaSMark Shellenbaum 	}
22700a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(db_spill, tx);
22710a586ceaSMark Shellenbaum 
22720a586ceaSMark Shellenbaum 	if (db_spill->db_size < drrs->drr_length)
22730a586ceaSMark Shellenbaum 		VERIFY(0 == dbuf_spill_set_blksz(db_spill,
22740a586ceaSMark Shellenbaum 		    drrs->drr_length, tx));
22750a586ceaSMark Shellenbaum 	bcopy(data, db_spill->db_data, drrs->drr_length);
22760a586ceaSMark Shellenbaum 
22770a586ceaSMark Shellenbaum 	dmu_buf_rele(db, FTAG);
22780a586ceaSMark Shellenbaum 	dmu_buf_rele(db_spill, FTAG);
22790a586ceaSMark Shellenbaum 
22800a586ceaSMark Shellenbaum 	dmu_tx_commit(tx);
22810a586ceaSMark Shellenbaum 	return (0);
22820a586ceaSMark Shellenbaum }
22830a586ceaSMark Shellenbaum 
2284efb80947Sahrens /* ARGSUSED */
2285efb80947Sahrens static int
2286a2cdcdd2SPaul Dagnelie receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2287efb80947Sahrens {
2288efb80947Sahrens 	int err;
2289efb80947Sahrens 
2290efb80947Sahrens 	if (drrf->drr_length != -1ULL &&
2291efb80947Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2292be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2293efb80947Sahrens 
2294a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2295be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2296efb80947Sahrens 
2297a2cdcdd2SPaul Dagnelie 	err = dmu_free_long_range(rwa->os, drrf->drr_object,
2298efb80947Sahrens 	    drrf->drr_offset, drrf->drr_length);
2299a2cdcdd2SPaul Dagnelie 
2300efb80947Sahrens 	return (err);
2301efb80947Sahrens }
2302efb80947Sahrens 
23033b2aab18SMatthew Ahrens /* used to destroy the drc_ds on error */
23043b2aab18SMatthew Ahrens static void
23053b2aab18SMatthew Ahrens dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
23063b2aab18SMatthew Ahrens {
23079c3fd121SMatthew Ahrens 	if (drc->drc_resumable) {
23089c3fd121SMatthew Ahrens 		/* wait for our resume state to be written to disk */
23099c3fd121SMatthew Ahrens 		txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
23109c3fd121SMatthew Ahrens 		dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
23119c3fd121SMatthew Ahrens 	} else {
23129adfa60dSMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
23139c3fd121SMatthew Ahrens 		dsl_dataset_name(drc->drc_ds, name);
23149c3fd121SMatthew Ahrens 		dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
23159c3fd121SMatthew Ahrens 		(void) dsl_destroy_head(name);
23169c3fd121SMatthew Ahrens 	}
23173b2aab18SMatthew Ahrens }
23183b2aab18SMatthew Ahrens 
231998110f08SMatthew Ahrens static void
2320a2cdcdd2SPaul Dagnelie receive_cksum(struct receive_arg *ra, int len, void *buf)
232198110f08SMatthew Ahrens {
232298110f08SMatthew Ahrens 	if (ra->byteswap) {
232398110f08SMatthew Ahrens 		fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
232498110f08SMatthew Ahrens 	} else {
232598110f08SMatthew Ahrens 		fletcher_4_incremental_native(buf, len, &ra->cksum);
232698110f08SMatthew Ahrens 	}
232798110f08SMatthew Ahrens }
232898110f08SMatthew Ahrens 
232998110f08SMatthew Ahrens /*
2330a2cdcdd2SPaul Dagnelie  * Read the payload into a buffer of size len, and update the current record's
2331a2cdcdd2SPaul Dagnelie  * payload field.
2332a2cdcdd2SPaul Dagnelie  * Allocate ra->next_rrd and read the next record's header into
2333a2cdcdd2SPaul Dagnelie  * ra->next_rrd->header.
233498110f08SMatthew Ahrens  * Verify checksum of payload and next record.
233598110f08SMatthew Ahrens  */
233698110f08SMatthew Ahrens static int
2337a2cdcdd2SPaul Dagnelie receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
233898110f08SMatthew Ahrens {
233998110f08SMatthew Ahrens 	int err;
234098110f08SMatthew Ahrens 
234198110f08SMatthew Ahrens 	if (len != 0) {
2342a2cdcdd2SPaul Dagnelie 		ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
23439c3fd121SMatthew Ahrens 		err = receive_read(ra, len, buf);
234498110f08SMatthew Ahrens 		if (err != 0)
234598110f08SMatthew Ahrens 			return (err);
23469c3fd121SMatthew Ahrens 		receive_cksum(ra, len, buf);
23479c3fd121SMatthew Ahrens 
23489c3fd121SMatthew Ahrens 		/* note: rrd is NULL when reading the begin record's payload */
23499c3fd121SMatthew Ahrens 		if (ra->rrd != NULL) {
23509c3fd121SMatthew Ahrens 			ra->rrd->payload = buf;
23519c3fd121SMatthew Ahrens 			ra->rrd->payload_size = len;
23529c3fd121SMatthew Ahrens 			ra->rrd->bytes_read = ra->bytes_read;
23539c3fd121SMatthew Ahrens 		}
235498110f08SMatthew Ahrens 	}
235598110f08SMatthew Ahrens 
235698110f08SMatthew Ahrens 	ra->prev_cksum = ra->cksum;
235798110f08SMatthew Ahrens 
2358a2cdcdd2SPaul Dagnelie 	ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2359a2cdcdd2SPaul Dagnelie 	err = receive_read(ra, sizeof (ra->next_rrd->header),
2360a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header);
23619c3fd121SMatthew Ahrens 	ra->next_rrd->bytes_read = ra->bytes_read;
2362a2cdcdd2SPaul Dagnelie 	if (err != 0) {
2363a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2364a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
236598110f08SMatthew Ahrens 		return (err);
2366a2cdcdd2SPaul Dagnelie 	}
2367a2cdcdd2SPaul Dagnelie 	if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2368a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2369a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
237098110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
2371a2cdcdd2SPaul Dagnelie 	}
237298110f08SMatthew Ahrens 
237398110f08SMatthew Ahrens 	/*
237498110f08SMatthew Ahrens 	 * Note: checksum is of everything up to but not including the
237598110f08SMatthew Ahrens 	 * checksum itself.
237698110f08SMatthew Ahrens 	 */
237798110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
237898110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2379a2cdcdd2SPaul Dagnelie 	receive_cksum(ra,
238098110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2381a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header);
238298110f08SMatthew Ahrens 
2383a2cdcdd2SPaul Dagnelie 	zio_cksum_t cksum_orig =
2384a2cdcdd2SPaul Dagnelie 	    ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2385a2cdcdd2SPaul Dagnelie 	zio_cksum_t *cksump =
2386a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
238798110f08SMatthew Ahrens 
238898110f08SMatthew Ahrens 	if (ra->byteswap)
2389a2cdcdd2SPaul Dagnelie 		byteswap_record(&ra->next_rrd->header);
239098110f08SMatthew Ahrens 
239198110f08SMatthew Ahrens 	if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2392a2cdcdd2SPaul Dagnelie 	    !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2393a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2394a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
239598110f08SMatthew Ahrens 		return (SET_ERROR(ECKSUM));
2396a2cdcdd2SPaul Dagnelie 	}
239798110f08SMatthew Ahrens 
2398a2cdcdd2SPaul Dagnelie 	receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
239998110f08SMatthew Ahrens 
240098110f08SMatthew Ahrens 	return (0);
240198110f08SMatthew Ahrens }
240298110f08SMatthew Ahrens 
240368ecb2ecSPaul Dagnelie static void
240468ecb2ecSPaul Dagnelie objlist_create(struct objlist *list)
240568ecb2ecSPaul Dagnelie {
240668ecb2ecSPaul Dagnelie 	list_create(&list->list, sizeof (struct receive_objnode),
240768ecb2ecSPaul Dagnelie 	    offsetof(struct receive_objnode, node));
240868ecb2ecSPaul Dagnelie 	list->last_lookup = 0;
240968ecb2ecSPaul Dagnelie }
241068ecb2ecSPaul Dagnelie 
241168ecb2ecSPaul Dagnelie static void
241268ecb2ecSPaul Dagnelie objlist_destroy(struct objlist *list)
241368ecb2ecSPaul Dagnelie {
241468ecb2ecSPaul Dagnelie 	for (struct receive_objnode *n = list_remove_head(&list->list);
241568ecb2ecSPaul Dagnelie 	    n != NULL; n = list_remove_head(&list->list)) {
241668ecb2ecSPaul Dagnelie 		kmem_free(n, sizeof (*n));
241768ecb2ecSPaul Dagnelie 	}
241868ecb2ecSPaul Dagnelie 	list_destroy(&list->list);
241968ecb2ecSPaul Dagnelie }
242068ecb2ecSPaul Dagnelie 
242168ecb2ecSPaul Dagnelie /*
242268ecb2ecSPaul Dagnelie  * This function looks through the objlist to see if the specified object number
242368ecb2ecSPaul Dagnelie  * is contained in the objlist.  In the process, it will remove all object
242468ecb2ecSPaul Dagnelie  * numbers in the list that are smaller than the specified object number.  Thus,
242568ecb2ecSPaul Dagnelie  * any lookup of an object number smaller than a previously looked up object
242668ecb2ecSPaul Dagnelie  * number will always return false; therefore, all lookups should be done in
242768ecb2ecSPaul Dagnelie  * ascending order.
242868ecb2ecSPaul Dagnelie  */
242968ecb2ecSPaul Dagnelie static boolean_t
243068ecb2ecSPaul Dagnelie objlist_exists(struct objlist *list, uint64_t object)
243168ecb2ecSPaul Dagnelie {
243268ecb2ecSPaul Dagnelie 	struct receive_objnode *node = list_head(&list->list);
243368ecb2ecSPaul Dagnelie 	ASSERT3U(object, >=, list->last_lookup);
243468ecb2ecSPaul Dagnelie 	list->last_lookup = object;
243568ecb2ecSPaul Dagnelie 	while (node != NULL && node->object < object) {
243668ecb2ecSPaul Dagnelie 		VERIFY3P(node, ==, list_remove_head(&list->list));
243768ecb2ecSPaul Dagnelie 		kmem_free(node, sizeof (*node));
243868ecb2ecSPaul Dagnelie 		node = list_head(&list->list);
243968ecb2ecSPaul Dagnelie 	}
244068ecb2ecSPaul Dagnelie 	return (node != NULL && node->object == object);
244168ecb2ecSPaul Dagnelie }
244268ecb2ecSPaul Dagnelie 
244368ecb2ecSPaul Dagnelie /*
244468ecb2ecSPaul Dagnelie  * The objlist is a list of object numbers stored in ascending order.  However,
244568ecb2ecSPaul Dagnelie  * the insertion of new object numbers does not seek out the correct location to
244668ecb2ecSPaul Dagnelie  * store a new object number; instead, it appends it to the list for simplicity.
244768ecb2ecSPaul Dagnelie  * Thus, any users must take care to only insert new object numbers in ascending
244868ecb2ecSPaul Dagnelie  * order.
244968ecb2ecSPaul Dagnelie  */
245068ecb2ecSPaul Dagnelie static void
245168ecb2ecSPaul Dagnelie objlist_insert(struct objlist *list, uint64_t object)
245268ecb2ecSPaul Dagnelie {
245368ecb2ecSPaul Dagnelie 	struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
245468ecb2ecSPaul Dagnelie 	node->object = object;
245568ecb2ecSPaul Dagnelie #ifdef ZFS_DEBUG
245668ecb2ecSPaul Dagnelie 	struct receive_objnode *last_object = list_tail(&list->list);
245768ecb2ecSPaul Dagnelie 	uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
245868ecb2ecSPaul Dagnelie 	ASSERT3U(node->object, >, last_objnum);
245968ecb2ecSPaul Dagnelie #endif
246068ecb2ecSPaul Dagnelie 	list_insert_tail(&list->list, node);
246168ecb2ecSPaul Dagnelie }
246268ecb2ecSPaul Dagnelie 
2463a2cdcdd2SPaul Dagnelie /*
2464a2cdcdd2SPaul Dagnelie  * Issue the prefetch reads for any necessary indirect blocks.
2465a2cdcdd2SPaul Dagnelie  *
2466a2cdcdd2SPaul Dagnelie  * We use the object ignore list to tell us whether or not to issue prefetches
2467a2cdcdd2SPaul Dagnelie  * for a given object.  We do this for both correctness (in case the blocksize
2468a2cdcdd2SPaul Dagnelie  * of an object has changed) and performance (if the object doesn't exist, don't
2469a2cdcdd2SPaul Dagnelie  * needlessly try to issue prefetches).  We also trim the list as we go through
2470a2cdcdd2SPaul Dagnelie  * the stream to prevent it from growing to an unbounded size.
2471a2cdcdd2SPaul Dagnelie  *
2472a2cdcdd2SPaul Dagnelie  * The object numbers within will always be in sorted order, and any write
2473a2cdcdd2SPaul Dagnelie  * records we see will also be in sorted order, but they're not sorted with
2474a2cdcdd2SPaul Dagnelie  * respect to each other (i.e. we can get several object records before
2475a2cdcdd2SPaul Dagnelie  * receiving each object's write records).  As a result, once we've reached a
2476a2cdcdd2SPaul Dagnelie  * given object number, we can safely remove any reference to lower object
2477a2cdcdd2SPaul Dagnelie  * numbers in the ignore list. In practice, we receive up to 32 object records
2478a2cdcdd2SPaul Dagnelie  * before receiving write records, so the list can have up to 32 nodes in it.
2479a2cdcdd2SPaul Dagnelie  */
2480a2cdcdd2SPaul Dagnelie /* ARGSUSED */
2481a2cdcdd2SPaul Dagnelie static void
2482a2cdcdd2SPaul Dagnelie receive_read_prefetch(struct receive_arg *ra,
2483a2cdcdd2SPaul Dagnelie     uint64_t object, uint64_t offset, uint64_t length)
2484a2cdcdd2SPaul Dagnelie {
248568ecb2ecSPaul Dagnelie 	if (!objlist_exists(&ra->ignore_objlist, object)) {
2486a2cdcdd2SPaul Dagnelie 		dmu_prefetch(ra->os, object, 1, offset, length,
2487a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
2488a2cdcdd2SPaul Dagnelie 	}
2489a2cdcdd2SPaul Dagnelie }
2490a2cdcdd2SPaul Dagnelie 
2491a2cdcdd2SPaul Dagnelie /*
2492a2cdcdd2SPaul Dagnelie  * Read records off the stream, issuing any necessary prefetches.
2493a2cdcdd2SPaul Dagnelie  */
249498110f08SMatthew Ahrens static int
2495a2cdcdd2SPaul Dagnelie receive_read_record(struct receive_arg *ra)
249698110f08SMatthew Ahrens {
249798110f08SMatthew Ahrens 	int err;
249898110f08SMatthew Ahrens 
2499a2cdcdd2SPaul Dagnelie 	switch (ra->rrd->header.drr_type) {
250098110f08SMatthew Ahrens 	case DRR_OBJECT:
250198110f08SMatthew Ahrens 	{
2502a2cdcdd2SPaul Dagnelie 		struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2503a2cdcdd2SPaul Dagnelie 		uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2504a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(size, KM_SLEEP);
2505a2cdcdd2SPaul Dagnelie 		dmu_object_info_t doi;
2506a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, size, buf);
2507a2cdcdd2SPaul Dagnelie 		if (err != 0) {
2508a2cdcdd2SPaul Dagnelie 			kmem_free(buf, size);
250998110f08SMatthew Ahrens 			return (err);
2510a2cdcdd2SPaul Dagnelie 		}
2511a2cdcdd2SPaul Dagnelie 		err = dmu_object_info(ra->os, drro->drr_object, &doi);
2512a2cdcdd2SPaul Dagnelie 		/*
2513a2cdcdd2SPaul Dagnelie 		 * See receive_read_prefetch for an explanation why we're
2514a2cdcdd2SPaul Dagnelie 		 * storing this object in the ignore_obj_list.
2515a2cdcdd2SPaul Dagnelie 		 */
2516a2cdcdd2SPaul Dagnelie 		if (err == ENOENT ||
2517a2cdcdd2SPaul Dagnelie 		    (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
251868ecb2ecSPaul Dagnelie 			objlist_insert(&ra->ignore_objlist, drro->drr_object);
2519a2cdcdd2SPaul Dagnelie 			err = 0;
2520a2cdcdd2SPaul Dagnelie 		}
2521a2cdcdd2SPaul Dagnelie 		return (err);
252298110f08SMatthew Ahrens 	}
252398110f08SMatthew Ahrens 	case DRR_FREEOBJECTS:
252498110f08SMatthew Ahrens 	{
2525a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2526a2cdcdd2SPaul Dagnelie 		return (err);
252798110f08SMatthew Ahrens 	}
252898110f08SMatthew Ahrens 	case DRR_WRITE:
252998110f08SMatthew Ahrens 	{
2530a2cdcdd2SPaul Dagnelie 		struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
253198110f08SMatthew Ahrens 		arc_buf_t *abuf = arc_loan_buf(dmu_objset_spa(ra->os),
253298110f08SMatthew Ahrens 		    drrw->drr_length);
253398110f08SMatthew Ahrens 
2534a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra,
253598110f08SMatthew Ahrens 		    drrw->drr_length, abuf->b_data);
2536a2cdcdd2SPaul Dagnelie 		if (err != 0) {
253798110f08SMatthew Ahrens 			dmu_return_arcbuf(abuf);
2538a2cdcdd2SPaul Dagnelie 			return (err);
2539a2cdcdd2SPaul Dagnelie 		}
2540a2cdcdd2SPaul Dagnelie 		ra->rrd->write_buf = abuf;
2541a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2542a2cdcdd2SPaul Dagnelie 		    drrw->drr_length);
254398110f08SMatthew Ahrens 		return (err);
254498110f08SMatthew Ahrens 	}
254598110f08SMatthew Ahrens 	case DRR_WRITE_BYREF:
254698110f08SMatthew Ahrens 	{
2547a2cdcdd2SPaul Dagnelie 		struct drr_write_byref *drrwb =
2548a2cdcdd2SPaul Dagnelie 		    &ra->rrd->header.drr_u.drr_write_byref;
2549a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2550a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2551a2cdcdd2SPaul Dagnelie 		    drrwb->drr_length);
2552a2cdcdd2SPaul Dagnelie 		return (err);
255398110f08SMatthew Ahrens 	}
255498110f08SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
255598110f08SMatthew Ahrens 	{
255698110f08SMatthew Ahrens 		struct drr_write_embedded *drrwe =
2557a2cdcdd2SPaul Dagnelie 		    &ra->rrd->header.drr_u.drr_write_embedded;
2558a2cdcdd2SPaul Dagnelie 		uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2559a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(size, KM_SLEEP);
2560a2cdcdd2SPaul Dagnelie 
2561a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, size, buf);
2562a2cdcdd2SPaul Dagnelie 		if (err != 0) {
2563a2cdcdd2SPaul Dagnelie 			kmem_free(buf, size);
256498110f08SMatthew Ahrens 			return (err);
2565a2cdcdd2SPaul Dagnelie 		}
2566a2cdcdd2SPaul Dagnelie 
2567a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2568a2cdcdd2SPaul Dagnelie 		    drrwe->drr_length);
2569a2cdcdd2SPaul Dagnelie 		return (err);
257098110f08SMatthew Ahrens 	}
257198110f08SMatthew Ahrens 	case DRR_FREE:
257298110f08SMatthew Ahrens 	{
2573a2cdcdd2SPaul Dagnelie 		/*
2574a2cdcdd2SPaul Dagnelie 		 * It might be beneficial to prefetch indirect blocks here, but
2575a2cdcdd2SPaul Dagnelie 		 * we don't really have the data to decide for sure.
2576a2cdcdd2SPaul Dagnelie 		 */
2577a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2578a2cdcdd2SPaul Dagnelie 		return (err);
257998110f08SMatthew Ahrens 	}
258098110f08SMatthew Ahrens 	case DRR_END:
258198110f08SMatthew Ahrens 	{
2582a2cdcdd2SPaul Dagnelie 		struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
258398110f08SMatthew Ahrens 		if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
25849c3fd121SMatthew Ahrens 			return (SET_ERROR(ECKSUM));
258598110f08SMatthew Ahrens 		return (0);
258698110f08SMatthew Ahrens 	}
258798110f08SMatthew Ahrens 	case DRR_SPILL:
258898110f08SMatthew Ahrens 	{
2589a2cdcdd2SPaul Dagnelie 		struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2590a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2591a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2592a2cdcdd2SPaul Dagnelie 		    buf);
259398110f08SMatthew Ahrens 		if (err != 0)
2594a2cdcdd2SPaul Dagnelie 			kmem_free(buf, drrs->drr_length);
2595a2cdcdd2SPaul Dagnelie 		return (err);
259698110f08SMatthew Ahrens 	}
259798110f08SMatthew Ahrens 	default:
259898110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
259998110f08SMatthew Ahrens 	}
260098110f08SMatthew Ahrens }
260198110f08SMatthew Ahrens 
26023cb34c60Sahrens /*
2603a2cdcdd2SPaul Dagnelie  * Commit the records to the pool.
2604a2cdcdd2SPaul Dagnelie  */
2605a2cdcdd2SPaul Dagnelie static int
2606a2cdcdd2SPaul Dagnelie receive_process_record(struct receive_writer_arg *rwa,
2607a2cdcdd2SPaul Dagnelie     struct receive_record_arg *rrd)
2608a2cdcdd2SPaul Dagnelie {
2609a2cdcdd2SPaul Dagnelie 	int err;
2610a2cdcdd2SPaul Dagnelie 
26119c3fd121SMatthew Ahrens 	/* Processing in order, therefore bytes_read should be increasing. */
26129c3fd121SMatthew Ahrens 	ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
26139c3fd121SMatthew Ahrens 	rwa->bytes_read = rrd->bytes_read;
26149c3fd121SMatthew Ahrens 
2615a2cdcdd2SPaul Dagnelie 	switch (rrd->header.drr_type) {
2616a2cdcdd2SPaul Dagnelie 	case DRR_OBJECT:
2617a2cdcdd2SPaul Dagnelie 	{
2618a2cdcdd2SPaul Dagnelie 		struct drr_object *drro = &rrd->header.drr_u.drr_object;
2619a2cdcdd2SPaul Dagnelie 		err = receive_object(rwa, drro, rrd->payload);
2620a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2621a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2622a2cdcdd2SPaul Dagnelie 		return (err);
2623a2cdcdd2SPaul Dagnelie 	}
2624a2cdcdd2SPaul Dagnelie 	case DRR_FREEOBJECTS:
2625a2cdcdd2SPaul Dagnelie 	{
2626a2cdcdd2SPaul Dagnelie 		struct drr_freeobjects *drrfo =
2627a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_freeobjects;
2628a2cdcdd2SPaul Dagnelie 		return (receive_freeobjects(rwa, drrfo));
2629a2cdcdd2SPaul Dagnelie 	}
2630a2cdcdd2SPaul Dagnelie 	case DRR_WRITE:
2631a2cdcdd2SPaul Dagnelie 	{
2632a2cdcdd2SPaul Dagnelie 		struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2633a2cdcdd2SPaul Dagnelie 		err = receive_write(rwa, drrw, rrd->write_buf);
2634a2cdcdd2SPaul Dagnelie 		/* if receive_write() is successful, it consumes the arc_buf */
2635a2cdcdd2SPaul Dagnelie 		if (err != 0)
2636a2cdcdd2SPaul Dagnelie 			dmu_return_arcbuf(rrd->write_buf);
2637a2cdcdd2SPaul Dagnelie 		rrd->write_buf = NULL;
2638a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2639a2cdcdd2SPaul Dagnelie 		return (err);
2640a2cdcdd2SPaul Dagnelie 	}
2641a2cdcdd2SPaul Dagnelie 	case DRR_WRITE_BYREF:
2642a2cdcdd2SPaul Dagnelie 	{
2643a2cdcdd2SPaul Dagnelie 		struct drr_write_byref *drrwbr =
2644a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_write_byref;
2645a2cdcdd2SPaul Dagnelie 		return (receive_write_byref(rwa, drrwbr));
2646a2cdcdd2SPaul Dagnelie 	}
2647a2cdcdd2SPaul Dagnelie 	case DRR_WRITE_EMBEDDED:
2648a2cdcdd2SPaul Dagnelie 	{
2649a2cdcdd2SPaul Dagnelie 		struct drr_write_embedded *drrwe =
2650a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_write_embedded;
2651a2cdcdd2SPaul Dagnelie 		err = receive_write_embedded(rwa, drrwe, rrd->payload);
2652a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2653a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2654a2cdcdd2SPaul Dagnelie 		return (err);
2655a2cdcdd2SPaul Dagnelie 	}
2656a2cdcdd2SPaul Dagnelie 	case DRR_FREE:
2657a2cdcdd2SPaul Dagnelie 	{
2658a2cdcdd2SPaul Dagnelie 		struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2659a2cdcdd2SPaul Dagnelie 		return (receive_free(rwa, drrf));
2660a2cdcdd2SPaul Dagnelie 	}
2661a2cdcdd2SPaul Dagnelie 	case DRR_SPILL:
2662a2cdcdd2SPaul Dagnelie 	{
2663a2cdcdd2SPaul Dagnelie 		struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2664a2cdcdd2SPaul Dagnelie 		err = receive_spill(rwa, drrs, rrd->payload);
2665a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2666a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2667a2cdcdd2SPaul Dagnelie 		return (err);
2668a2cdcdd2SPaul Dagnelie 	}
2669a2cdcdd2SPaul Dagnelie 	default:
2670a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINVAL));
2671a2cdcdd2SPaul Dagnelie 	}
2672a2cdcdd2SPaul Dagnelie }
2673a2cdcdd2SPaul Dagnelie 
2674a2cdcdd2SPaul Dagnelie /*
2675a2cdcdd2SPaul Dagnelie  * dmu_recv_stream's worker thread; pull records off the queue, and then call
2676a2cdcdd2SPaul Dagnelie  * receive_process_record  When we're done, signal the main thread and exit.
2677a2cdcdd2SPaul Dagnelie  */
2678a2cdcdd2SPaul Dagnelie static void
2679a2cdcdd2SPaul Dagnelie receive_writer_thread(void *arg)
2680a2cdcdd2SPaul Dagnelie {
2681a2cdcdd2SPaul Dagnelie 	struct receive_writer_arg *rwa = arg;
2682a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *rrd;
2683a2cdcdd2SPaul Dagnelie 	for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2684a2cdcdd2SPaul Dagnelie 	    rrd = bqueue_dequeue(&rwa->q)) {
2685a2cdcdd2SPaul Dagnelie 		/*
2686a2cdcdd2SPaul Dagnelie 		 * If there's an error, the main thread will stop putting things
2687a2cdcdd2SPaul Dagnelie 		 * on the queue, but we need to clear everything in it before we
2688a2cdcdd2SPaul Dagnelie 		 * can exit.
2689a2cdcdd2SPaul Dagnelie 		 */
2690a2cdcdd2SPaul Dagnelie 		if (rwa->err == 0) {
2691a2cdcdd2SPaul Dagnelie 			rwa->err = receive_process_record(rwa, rrd);
2692a2cdcdd2SPaul Dagnelie 		} else if (rrd->write_buf != NULL) {
2693a2cdcdd2SPaul Dagnelie 			dmu_return_arcbuf(rrd->write_buf);
2694a2cdcdd2SPaul Dagnelie 			rrd->write_buf = NULL;
2695a2cdcdd2SPaul Dagnelie 			rrd->payload = NULL;
2696a2cdcdd2SPaul Dagnelie 		} else if (rrd->payload != NULL) {
2697a2cdcdd2SPaul Dagnelie 			kmem_free(rrd->payload, rrd->payload_size);
2698a2cdcdd2SPaul Dagnelie 			rrd->payload = NULL;
2699a2cdcdd2SPaul Dagnelie 		}
2700a2cdcdd2SPaul Dagnelie 		kmem_free(rrd, sizeof (*rrd));
2701a2cdcdd2SPaul Dagnelie 	}
2702a2cdcdd2SPaul Dagnelie 	kmem_free(rrd, sizeof (*rrd));
2703a2cdcdd2SPaul Dagnelie 	mutex_enter(&rwa->mutex);
2704a2cdcdd2SPaul Dagnelie 	rwa->done = B_TRUE;
2705a2cdcdd2SPaul Dagnelie 	cv_signal(&rwa->cv);
2706a2cdcdd2SPaul Dagnelie 	mutex_exit(&rwa->mutex);
2707a2cdcdd2SPaul Dagnelie }
2708a2cdcdd2SPaul Dagnelie 
27099c3fd121SMatthew Ahrens static int
27109c3fd121SMatthew Ahrens resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
27119c3fd121SMatthew Ahrens {
27129c3fd121SMatthew Ahrens 	uint64_t val;
27139c3fd121SMatthew Ahrens 	objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
27149c3fd121SMatthew Ahrens 	uint64_t dsobj = dmu_objset_id(ra->os);
27159c3fd121SMatthew Ahrens 	uint64_t resume_obj, resume_off;
27169c3fd121SMatthew Ahrens 
27179c3fd121SMatthew Ahrens 	if (nvlist_lookup_uint64(begin_nvl,
27189c3fd121SMatthew Ahrens 	    "resume_object", &resume_obj) != 0 ||
27199c3fd121SMatthew Ahrens 	    nvlist_lookup_uint64(begin_nvl,
27209c3fd121SMatthew Ahrens 	    "resume_offset", &resume_off) != 0) {
27219c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
27229c3fd121SMatthew Ahrens 	}
27239c3fd121SMatthew Ahrens 	VERIFY0(zap_lookup(mos, dsobj,
27249c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
27259c3fd121SMatthew Ahrens 	if (resume_obj != val)
27269c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
27279c3fd121SMatthew Ahrens 	VERIFY0(zap_lookup(mos, dsobj,
27289c3fd121SMatthew Ahrens 	    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
27299c3fd121SMatthew Ahrens 	if (resume_off != val)
27309c3fd121SMatthew Ahrens 		return (SET_ERROR(EINVAL));
27319c3fd121SMatthew Ahrens 
27329c3fd121SMatthew Ahrens 	return (0);
27339c3fd121SMatthew Ahrens }
27349c3fd121SMatthew Ahrens 
2735a2cdcdd2SPaul Dagnelie /*
2736a2cdcdd2SPaul Dagnelie  * Read in the stream's records, one by one, and apply them to the pool.  There
2737a2cdcdd2SPaul Dagnelie  * are two threads involved; the thread that calls this function will spin up a
2738a2cdcdd2SPaul Dagnelie  * worker thread, read the records off the stream one by one, and issue
2739a2cdcdd2SPaul Dagnelie  * prefetches for any necessary indirect blocks.  It will then push the records
2740a2cdcdd2SPaul Dagnelie  * onto an internal blocking queue.  The worker thread will pull the records off
2741a2cdcdd2SPaul Dagnelie  * the queue, and actually write the data into the DMU.  This way, the worker
2742a2cdcdd2SPaul Dagnelie  * thread doesn't have to wait for reads to complete, since everything it needs
2743a2cdcdd2SPaul Dagnelie  * (the indirect blocks) will be prefetched.
2744a2cdcdd2SPaul Dagnelie  *
27453cb34c60Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
27463cb34c60Sahrens  */
2747efb80947Sahrens int
2748c99e4bdcSChris Kirby dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
2749c99e4bdcSChris Kirby     int cleanup_fd, uint64_t *action_handlep)
2750efb80947Sahrens {
275198110f08SMatthew Ahrens 	int err = 0;
2752a2cdcdd2SPaul Dagnelie 	struct receive_arg ra = { 0 };
2753a2cdcdd2SPaul Dagnelie 	struct receive_writer_arg rwa = { 0 };
27549e69d7d0SLori Alt 	int featureflags;
27559c3fd121SMatthew Ahrens 	nvlist_t *begin_nvl = NULL;
2756efb80947Sahrens 
27573b2aab18SMatthew Ahrens 	ra.byteswap = drc->drc_byteswap;
27583b2aab18SMatthew Ahrens 	ra.cksum = drc->drc_cksum;
27593cb34c60Sahrens 	ra.vp = vp;
27603cb34c60Sahrens 	ra.voff = *voffp;
27619c3fd121SMatthew Ahrens 
27629c3fd121SMatthew Ahrens 	if (dsl_dataset_is_zapified(drc->drc_ds)) {
27639c3fd121SMatthew Ahrens 		(void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
27649c3fd121SMatthew Ahrens 		    drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
27659c3fd121SMatthew Ahrens 		    sizeof (ra.bytes_read), 1, &ra.bytes_read);
27669c3fd121SMatthew Ahrens 	}
27679c3fd121SMatthew Ahrens 
276868ecb2ecSPaul Dagnelie 	objlist_create(&ra.ignore_objlist);
2769efb80947Sahrens 
27703cb34c60Sahrens 	/* these were verified in dmu_recv_begin */
27713b2aab18SMatthew Ahrens 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
27729e69d7d0SLori Alt 	    DMU_SUBSTREAM);
27733b2aab18SMatthew Ahrens 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2774efb80947Sahrens 
2775efb80947Sahrens 	/*
2776efb80947Sahrens 	 * Open the objset we are modifying.
2777efb80947Sahrens 	 */
277898110f08SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
2779efb80947Sahrens 
2780c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2781efb80947Sahrens 
27829e69d7d0SLori Alt 	featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
27839e69d7d0SLori Alt 
27849e69d7d0SLori Alt 	/* if this stream is dedup'ed, set up the avl tree for guid mapping */
27859e69d7d0SLori Alt 	if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2786a7f53a56SChris Kirby 		minor_t minor;
2787a7f53a56SChris Kirby 
2788c99e4bdcSChris Kirby 		if (cleanup_fd == -1) {
2789be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EBADF);
2790c99e4bdcSChris Kirby 			goto out;
2791c99e4bdcSChris Kirby 		}
2792a7f53a56SChris Kirby 		ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
27933b2aab18SMatthew Ahrens 		if (ra.err != 0) {
2794a7f53a56SChris Kirby 			cleanup_fd = -1;
2795a7f53a56SChris Kirby 			goto out;
2796a7f53a56SChris Kirby 		}
2797a7f53a56SChris Kirby 
2798c99e4bdcSChris Kirby 		if (*action_handlep == 0) {
2799a2cdcdd2SPaul Dagnelie 			rwa.guid_to_ds_map =
2800c99e4bdcSChris Kirby 			    kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2801a2cdcdd2SPaul Dagnelie 			avl_create(rwa.guid_to_ds_map, guid_compare,
2802c99e4bdcSChris Kirby 			    sizeof (guid_map_entry_t),
2803c99e4bdcSChris Kirby 			    offsetof(guid_map_entry_t, avlnode));
280498110f08SMatthew Ahrens 			err = zfs_onexit_add_cb(minor,
2805a2cdcdd2SPaul Dagnelie 			    free_guid_map_onexit, rwa.guid_to_ds_map,
2806c99e4bdcSChris Kirby 			    action_handlep);
28073b2aab18SMatthew Ahrens 			if (ra.err != 0)
2808c99e4bdcSChris Kirby 				goto out;
2809c99e4bdcSChris Kirby 		} else {
281098110f08SMatthew Ahrens 			err = zfs_onexit_cb_data(minor, *action_handlep,
2811a2cdcdd2SPaul Dagnelie 			    (void **)&rwa.guid_to_ds_map);
28123b2aab18SMatthew Ahrens 			if (ra.err != 0)
2813c99e4bdcSChris Kirby 				goto out;
2814c99e4bdcSChris Kirby 		}
2815ec5cf9d5SAlexander Stetsenko 
2816a2cdcdd2SPaul Dagnelie 		drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
28179e69d7d0SLori Alt 	}
28189e69d7d0SLori Alt 
28199c3fd121SMatthew Ahrens 	uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
28209c3fd121SMatthew Ahrens 	void *payload = NULL;
28219c3fd121SMatthew Ahrens 	if (payloadlen != 0)
28229c3fd121SMatthew Ahrens 		payload = kmem_alloc(payloadlen, KM_SLEEP);
28239c3fd121SMatthew Ahrens 
28249c3fd121SMatthew Ahrens 	err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
28259c3fd121SMatthew Ahrens 	if (err != 0) {
28269c3fd121SMatthew Ahrens 		if (payloadlen != 0)
28279c3fd121SMatthew Ahrens 			kmem_free(payload, payloadlen);
282898110f08SMatthew Ahrens 		goto out;
28299c3fd121SMatthew Ahrens 	}
28309c3fd121SMatthew Ahrens 	if (payloadlen != 0) {
28319c3fd121SMatthew Ahrens 		err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
28329c3fd121SMatthew Ahrens 		kmem_free(payload, payloadlen);
28339c3fd121SMatthew Ahrens 		if (err != 0)
28349c3fd121SMatthew Ahrens 			goto out;
28359c3fd121SMatthew Ahrens 	}
28369c3fd121SMatthew Ahrens 
28379c3fd121SMatthew Ahrens 	if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
28389c3fd121SMatthew Ahrens 		err = resume_check(&ra, begin_nvl);
28399c3fd121SMatthew Ahrens 		if (err != 0)
28409c3fd121SMatthew Ahrens 			goto out;
28419c3fd121SMatthew Ahrens 	}
284298110f08SMatthew Ahrens 
2843a2cdcdd2SPaul Dagnelie 	(void) bqueue_init(&rwa.q, zfs_recv_queue_length,
2844a2cdcdd2SPaul Dagnelie 	    offsetof(struct receive_record_arg, node));
2845a2cdcdd2SPaul Dagnelie 	cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
2846a2cdcdd2SPaul Dagnelie 	mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
2847a2cdcdd2SPaul Dagnelie 	rwa.os = ra.os;
2848a2cdcdd2SPaul Dagnelie 	rwa.byteswap = drc->drc_byteswap;
28499c3fd121SMatthew Ahrens 	rwa.resumable = drc->drc_resumable;
2850a2cdcdd2SPaul Dagnelie 
2851a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, curproc,
2852a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
2853a2cdcdd2SPaul Dagnelie 	/*
2854a2cdcdd2SPaul Dagnelie 	 * We're reading rwa.err without locks, which is safe since we are the
2855a2cdcdd2SPaul Dagnelie 	 * only reader, and the worker thread is the only writer.  It's ok if we
2856a2cdcdd2SPaul Dagnelie 	 * miss a write for an iteration or two of the loop, since the writer
2857a2cdcdd2SPaul Dagnelie 	 * thread will keep freeing records we send it until we send it an eos
2858a2cdcdd2SPaul Dagnelie 	 * marker.
2859a2cdcdd2SPaul Dagnelie 	 *
2860a2cdcdd2SPaul Dagnelie 	 * We can leave this loop in 3 ways:  First, if rwa.err is
2861a2cdcdd2SPaul Dagnelie 	 * non-zero.  In that case, the writer thread will free the rrd we just
2862a2cdcdd2SPaul Dagnelie 	 * pushed.  Second, if  we're interrupted; in that case, either it's the
2863a2cdcdd2SPaul Dagnelie 	 * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
2864a2cdcdd2SPaul Dagnelie 	 * has been handed off to the writer thread who will free it.  Finally,
2865a2cdcdd2SPaul Dagnelie 	 * if receive_read_record fails or we're at the end of the stream, then
2866a2cdcdd2SPaul Dagnelie 	 * we free ra.rrd and exit.
2867a2cdcdd2SPaul Dagnelie 	 */
2868a2cdcdd2SPaul Dagnelie 	while (rwa.err == 0) {
2869efb80947Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
287098110f08SMatthew Ahrens 			err = SET_ERROR(EINTR);
287198110f08SMatthew Ahrens 			break;
2872efb80947Sahrens 		}
2873efb80947Sahrens 
2874a2cdcdd2SPaul Dagnelie 		ASSERT3P(ra.rrd, ==, NULL);
2875a2cdcdd2SPaul Dagnelie 		ra.rrd = ra.next_rrd;
2876a2cdcdd2SPaul Dagnelie 		ra.next_rrd = NULL;
2877a2cdcdd2SPaul Dagnelie 		/* Allocates and loads header into ra.next_rrd */
2878a2cdcdd2SPaul Dagnelie 		err = receive_read_record(&ra);
2879efb80947Sahrens 
2880a2cdcdd2SPaul Dagnelie 		if (ra.rrd->header.drr_type == DRR_END || err != 0) {
2881a2cdcdd2SPaul Dagnelie 			kmem_free(ra.rrd, sizeof (*ra.rrd));
2882a2cdcdd2SPaul Dagnelie 			ra.rrd = NULL;
28830a586ceaSMark Shellenbaum 			break;
2884a2cdcdd2SPaul Dagnelie 		}
2885a2cdcdd2SPaul Dagnelie 
2886a2cdcdd2SPaul Dagnelie 		bqueue_enqueue(&rwa.q, ra.rrd,
2887a2cdcdd2SPaul Dagnelie 		    sizeof (struct receive_record_arg) + ra.rrd->payload_size);
2888a2cdcdd2SPaul Dagnelie 		ra.rrd = NULL;
2889a2cdcdd2SPaul Dagnelie 	}
2890a2cdcdd2SPaul Dagnelie 	if (ra.next_rrd == NULL)
2891a2cdcdd2SPaul Dagnelie 		ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
2892a2cdcdd2SPaul Dagnelie 	ra.next_rrd->eos_marker = B_TRUE;
2893a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
2894a2cdcdd2SPaul Dagnelie 
2895a2cdcdd2SPaul Dagnelie 	mutex_enter(&rwa.mutex);
2896a2cdcdd2SPaul Dagnelie 	while (!rwa.done) {
2897a2cdcdd2SPaul Dagnelie 		cv_wait(&rwa.cv, &rwa.mutex);
2898efb80947Sahrens 	}
2899a2cdcdd2SPaul Dagnelie 	mutex_exit(&rwa.mutex);
2900a2cdcdd2SPaul Dagnelie 
2901a2cdcdd2SPaul Dagnelie 	cv_destroy(&rwa.cv);
2902a2cdcdd2SPaul Dagnelie 	mutex_destroy(&rwa.mutex);
2903a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&rwa.q);
2904a2cdcdd2SPaul Dagnelie 	if (err == 0)
2905a2cdcdd2SPaul Dagnelie 		err = rwa.err;
2906efb80947Sahrens 
2907efb80947Sahrens out:
29089c3fd121SMatthew Ahrens 	nvlist_free(begin_nvl);
2909a7f53a56SChris Kirby 	if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2910a7f53a56SChris Kirby 		zfs_onexit_fd_rele(cleanup_fd);
2911a7f53a56SChris Kirby 
291298110f08SMatthew Ahrens 	if (err != 0) {
2913efb80947Sahrens 		/*
29149c3fd121SMatthew Ahrens 		 * Clean up references. If receive is not resumable,
29159c3fd121SMatthew Ahrens 		 * destroy what we created, so we don't leave it in
29169c3fd121SMatthew Ahrens 		 * the inconsistent state.
2917efb80947Sahrens 		 */
29183b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
2919efb80947Sahrens 	}
2920efb80947Sahrens 
29213cb34c60Sahrens 	*voffp = ra.voff;
292268ecb2ecSPaul Dagnelie 	objlist_destroy(&ra.ignore_objlist);
292398110f08SMatthew Ahrens 	return (err);
2924efb80947Sahrens }
2925f18faf3fSek 
29263cb34c60Sahrens static int
29273b2aab18SMatthew Ahrens dmu_recv_end_check(void *arg, dmu_tx_t *tx)
29283cb34c60Sahrens {
29293b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
29303b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
29313b2aab18SMatthew Ahrens 	int error;
29323b2aab18SMatthew Ahrens 
29333b2aab18SMatthew Ahrens 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
29343cb34c60Sahrens 
29353b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
29363b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
29373b2aab18SMatthew Ahrens 
29383b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
29393b2aab18SMatthew Ahrens 		if (error != 0)
29403b2aab18SMatthew Ahrens 			return (error);
294134f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
294234f2f8cfSMatthew Ahrens 			/*
294334f2f8cfSMatthew Ahrens 			 * We will destroy any snapshots in tofs (i.e. before
294434f2f8cfSMatthew Ahrens 			 * origin_head) that are after the origin (which is
294534f2f8cfSMatthew Ahrens 			 * the snap before drc_ds, because drc_ds can not
294634f2f8cfSMatthew Ahrens 			 * have any snaps of its own).
294734f2f8cfSMatthew Ahrens 			 */
2948c1379625SJustin T. Gibbs 			uint64_t obj;
2949c1379625SJustin T. Gibbs 
2950c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2951c1379625SJustin T. Gibbs 			while (obj !=
2952c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
295334f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
295434f2f8cfSMatthew Ahrens 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
295534f2f8cfSMatthew Ahrens 				    &snap);
295634f2f8cfSMatthew Ahrens 				if (error != 0)
2957beddaa9cSAndriy Gapon 					break;
295834f2f8cfSMatthew Ahrens 				if (snap->ds_dir != origin_head->ds_dir)
295934f2f8cfSMatthew Ahrens 					error = SET_ERROR(EINVAL);
296034f2f8cfSMatthew Ahrens 				if (error == 0)  {
296134f2f8cfSMatthew Ahrens 					error = dsl_destroy_snapshot_check_impl(
296234f2f8cfSMatthew Ahrens 					    snap, B_FALSE);
296334f2f8cfSMatthew Ahrens 				}
2964c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
296534f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
296634f2f8cfSMatthew Ahrens 				if (error != 0)
2967beddaa9cSAndriy Gapon 					break;
2968beddaa9cSAndriy Gapon 			}
2969beddaa9cSAndriy Gapon 			if (error != 0) {
2970beddaa9cSAndriy Gapon 				dsl_dataset_rele(origin_head, FTAG);
2971beddaa9cSAndriy Gapon 				return (error);
297234f2f8cfSMatthew Ahrens 			}
297334f2f8cfSMatthew Ahrens 		}
29743b2aab18SMatthew Ahrens 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
297591948b51SKeith M Wesolowski 		    origin_head, drc->drc_force, drc->drc_owner, tx);
29763b2aab18SMatthew Ahrens 		if (error != 0) {
29773b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin_head, FTAG);
29783b2aab18SMatthew Ahrens 			return (error);
29793b2aab18SMatthew Ahrens 		}
29803b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(origin_head,
2981a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
29823b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
29833b2aab18SMatthew Ahrens 		if (error != 0)
29843b2aab18SMatthew Ahrens 			return (error);
29853b2aab18SMatthew Ahrens 
29863b2aab18SMatthew Ahrens 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
29873b2aab18SMatthew Ahrens 	} else {
29883b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
2989a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
29903b2aab18SMatthew Ahrens 	}
29913b2aab18SMatthew Ahrens 	return (error);
29923cb34c60Sahrens }
29933cb34c60Sahrens 
29943cb34c60Sahrens static void
29953b2aab18SMatthew Ahrens dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
29963cb34c60Sahrens {
29973b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
29983b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
29993b2aab18SMatthew Ahrens 
30003b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
30013b2aab18SMatthew Ahrens 	    tx, "snap=%s", drc->drc_tosnap);
30023b2aab18SMatthew Ahrens 
30033b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
30043b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
30053b2aab18SMatthew Ahrens 
30063b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
30073b2aab18SMatthew Ahrens 		    &origin_head));
300834f2f8cfSMatthew Ahrens 
300934f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
301034f2f8cfSMatthew Ahrens 			/*
301134f2f8cfSMatthew Ahrens 			 * Destroy any snapshots of drc_tofs (origin_head)
301234f2f8cfSMatthew Ahrens 			 * after the origin (the snap before drc_ds).
301334f2f8cfSMatthew Ahrens 			 */
3014c1379625SJustin T. Gibbs 			uint64_t obj;
3015c1379625SJustin T. Gibbs 
3016c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3017c1379625SJustin T. Gibbs 			while (obj !=
3018c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
301934f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
302034f2f8cfSMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
302134f2f8cfSMatthew Ahrens 				    &snap));
302234f2f8cfSMatthew Ahrens 				ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3023c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
302434f2f8cfSMatthew Ahrens 				dsl_destroy_snapshot_sync_impl(snap,
302534f2f8cfSMatthew Ahrens 				    B_FALSE, tx);
302634f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
302734f2f8cfSMatthew Ahrens 			}
302834f2f8cfSMatthew Ahrens 		}
302934f2f8cfSMatthew Ahrens 		VERIFY3P(drc->drc_ds->ds_prev, ==,
303034f2f8cfSMatthew Ahrens 		    origin_head->ds_prev);
303134f2f8cfSMatthew Ahrens 
30323b2aab18SMatthew Ahrens 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
30333b2aab18SMatthew Ahrens 		    origin_head, tx);
30343b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(origin_head,
30353b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
30363b2aab18SMatthew Ahrens 
30373b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
30383b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3039c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
30403b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
3041c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
30423b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_toguid;
3043c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
30443b2aab18SMatthew Ahrens 		    ~DS_FLAG_INCONSISTENT;
30453b2aab18SMatthew Ahrens 
30463b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3047c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_flags &=
3048c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
30493b2aab18SMatthew Ahrens 
30503b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
30513b2aab18SMatthew Ahrens 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
305291948b51SKeith M Wesolowski 
305391948b51SKeith M Wesolowski 		if (drc->drc_owner != NULL)
305491948b51SKeith M Wesolowski 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
30553b2aab18SMatthew Ahrens 	} else {
30563b2aab18SMatthew Ahrens 		dsl_dataset_t *ds = drc->drc_ds;
30573cb34c60Sahrens 
30583b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
30593cb34c60Sahrens 
30603b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
30613b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3062c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
30633b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
3064c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_guid =
3065c1379625SJustin T. Gibbs 		    drc->drc_drrb->drr_toguid;
3066c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3067c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
30683cb34c60Sahrens 
30693b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
3070c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
30719c3fd121SMatthew Ahrens 		if (dsl_dataset_has_resume_receive_state(ds)) {
30729c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30739c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_FROMGUID, tx);
30749c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30759c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_OBJECT, tx);
30769c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30779c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_OFFSET, tx);
30789c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30799c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_BYTES, tx);
30809c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30819c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_TOGUID, tx);
30829c3fd121SMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
30839c3fd121SMatthew Ahrens 			    DS_FIELD_RESUME_TONAME, tx);
30849c3fd121SMatthew Ahrens 		}
30853b2aab18SMatthew Ahrens 	}
3086c1379625SJustin T. Gibbs 	drc->drc_newsnapobj = dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
30873b2aab18SMatthew Ahrens 	/*
30883b2aab18SMatthew Ahrens 	 * Release the hold from dmu_recv_begin.  This must be done before
30893b2aab18SMatthew Ahrens 	 * we return to open context, so that when we free the dataset's dnode,
30903b2aab18SMatthew Ahrens 	 * we can evict its bonus buffer.
30913b2aab18SMatthew Ahrens 	 */
30923b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
30933b2aab18SMatthew Ahrens 	drc->drc_ds = NULL;
30943cb34c60Sahrens }
30953cb34c60Sahrens 
3096ec5cf9d5SAlexander Stetsenko static int
30973b2aab18SMatthew Ahrens add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3098ec5cf9d5SAlexander Stetsenko {
30993b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
3100ec5cf9d5SAlexander Stetsenko 	dsl_dataset_t *snapds;
3101ec5cf9d5SAlexander Stetsenko 	guid_map_entry_t *gmep;
3102ec5cf9d5SAlexander Stetsenko 	int err;
3103ec5cf9d5SAlexander Stetsenko 
3104ec5cf9d5SAlexander Stetsenko 	ASSERT(guid_map != NULL);
3105ec5cf9d5SAlexander Stetsenko 
31063b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
31073b2aab18SMatthew Ahrens 	if (err != 0)
31083b2aab18SMatthew Ahrens 		return (err);
3109de8d9cffSMatthew Ahrens 	gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3110de8d9cffSMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3111ec5cf9d5SAlexander Stetsenko 	if (err == 0) {
3112c1379625SJustin T. Gibbs 		gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3113ec5cf9d5SAlexander Stetsenko 		gmep->gme_ds = snapds;
3114ec5cf9d5SAlexander Stetsenko 		avl_add(guid_map, gmep);
31153b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(snapds, gmep);
3116de8d9cffSMatthew Ahrens 	} else {
3117de8d9cffSMatthew Ahrens 		kmem_free(gmep, sizeof (*gmep));
3118ec5cf9d5SAlexander Stetsenko 	}
3119ec5cf9d5SAlexander Stetsenko 
31203b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
3121ec5cf9d5SAlexander Stetsenko 	return (err);
3122ec5cf9d5SAlexander Stetsenko }
3123ec5cf9d5SAlexander Stetsenko 
31243b2aab18SMatthew Ahrens static int dmu_recv_end_modified_blocks = 3;
31253b2aab18SMatthew Ahrens 
3126ae46e4c7SMatthew Ahrens static int
3127ae46e4c7SMatthew Ahrens dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3128f18faf3fSek {
31293b2aab18SMatthew Ahrens 	int error;
31303cb34c60Sahrens 
31313b2aab18SMatthew Ahrens #ifdef _KERNEL
31323b2aab18SMatthew Ahrens 	/*
31333b2aab18SMatthew Ahrens 	 * We will be destroying the ds; make sure its origin is unmounted if
31343b2aab18SMatthew Ahrens 	 * necessary.
31353b2aab18SMatthew Ahrens 	 */
31369adfa60dSMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
31373b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
31383b2aab18SMatthew Ahrens 	zfs_destroy_unmount_origin(name);
31393b2aab18SMatthew Ahrens #endif
31403cb34c60Sahrens 
31413b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
31423b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
31437d46dc6cSMatthew Ahrens 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
31443cb34c60Sahrens 
31453b2aab18SMatthew Ahrens 	if (error != 0)
31463b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
31473b2aab18SMatthew Ahrens 	return (error);
3148f18faf3fSek }
3149ae46e4c7SMatthew Ahrens 
3150ae46e4c7SMatthew Ahrens static int
3151ae46e4c7SMatthew Ahrens dmu_recv_new_end(dmu_recv_cookie_t *drc)
3152ae46e4c7SMatthew Ahrens {
31533b2aab18SMatthew Ahrens 	int error;
3154ae46e4c7SMatthew Ahrens 
31553b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
31563b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
31577d46dc6cSMatthew Ahrens 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3158ae46e4c7SMatthew Ahrens 
31593b2aab18SMatthew Ahrens 	if (error != 0) {
31603b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
31613b2aab18SMatthew Ahrens 	} else if (drc->drc_guid_to_ds_map != NULL) {
31623b2aab18SMatthew Ahrens 		(void) add_ds_to_guidmap(drc->drc_tofs,
31633b2aab18SMatthew Ahrens 		    drc->drc_guid_to_ds_map,
31643b2aab18SMatthew Ahrens 		    drc->drc_newsnapobj);
3165ae46e4c7SMatthew Ahrens 	}
31663b2aab18SMatthew Ahrens 	return (error);
3167ae46e4c7SMatthew Ahrens }
3168ae46e4c7SMatthew Ahrens 
3169ae46e4c7SMatthew Ahrens int
317091948b51SKeith M Wesolowski dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3171ae46e4c7SMatthew Ahrens {
317291948b51SKeith M Wesolowski 	drc->drc_owner = owner;
317391948b51SKeith M Wesolowski 
31743b2aab18SMatthew Ahrens 	if (drc->drc_newfs)
3175ae46e4c7SMatthew Ahrens 		return (dmu_recv_new_end(drc));
31763b2aab18SMatthew Ahrens 	else
31773b2aab18SMatthew Ahrens 		return (dmu_recv_existing_end(drc));
3178ae46e4c7SMatthew Ahrens }
31792f3d8780SMatthew Ahrens 
31802f3d8780SMatthew Ahrens /*
31812f3d8780SMatthew Ahrens  * Return TRUE if this objset is currently being received into.
31822f3d8780SMatthew Ahrens  */
31832f3d8780SMatthew Ahrens boolean_t
31842f3d8780SMatthew Ahrens dmu_objset_is_receiving(objset_t *os)
31852f3d8780SMatthew Ahrens {
31862f3d8780SMatthew Ahrens 	return (os->os_dsl_dataset != NULL &&
31872f3d8780SMatthew Ahrens 	    os->os_dsl_dataset->ds_owner == dmu_recv_tag);
31882f3d8780SMatthew Ahrens }
3189