xref: /titanic_44/usr/src/uts/common/fs/zfs/dmu_send.c (revision a1988827cf132ed4264c002279c6126834830520)
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.
24f22efbdaSMatthew Ahrens  * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
25a2afb611SJerry Jelinek  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
26e77d42eaSMatthew Ahrens  * Copyright 2014 HybridCluster. All rights reserved.
27ec5cf9d5SAlexander Stetsenko  */
28efb80947Sahrens 
29efb80947Sahrens #include <sys/dmu.h>
30efb80947Sahrens #include <sys/dmu_impl.h>
31efb80947Sahrens #include <sys/dmu_tx.h>
32efb80947Sahrens #include <sys/dbuf.h>
33efb80947Sahrens #include <sys/dnode.h>
34efb80947Sahrens #include <sys/zfs_context.h>
35efb80947Sahrens #include <sys/dmu_objset.h>
36efb80947Sahrens #include <sys/dmu_traverse.h>
37efb80947Sahrens #include <sys/dsl_dataset.h>
38efb80947Sahrens #include <sys/dsl_dir.h>
3992241e0bSTom Erickson #include <sys/dsl_prop.h>
40efb80947Sahrens #include <sys/dsl_pool.h>
41efb80947Sahrens #include <sys/dsl_synctask.h>
42efb80947Sahrens #include <sys/zfs_ioctl.h>
43efb80947Sahrens #include <sys/zap.h>
44efb80947Sahrens #include <sys/zio_checksum.h>
45dc7cd546SMark Shellenbaum #include <sys/zfs_znode.h>
46cde58dbcSMatthew Ahrens #include <zfs_fletcher.h>
479e69d7d0SLori Alt #include <sys/avl.h>
488e714474SLori Alt #include <sys/ddt.h>
49c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
503b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
513b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
525d7b4d43SMatthew Ahrens #include <sys/blkptr.h>
5378f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
545d7b4d43SMatthew Ahrens #include <sys/zfeature.h>
55a2cdcdd2SPaul Dagnelie #include <sys/bqueue.h>
56efb80947Sahrens 
5719b94df9SMatthew Ahrens /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
5819b94df9SMatthew Ahrens int zfs_send_corrupt_data = B_FALSE;
59a2cdcdd2SPaul Dagnelie int zfs_send_queue_length = 16 * 1024 * 1024;
60a2cdcdd2SPaul Dagnelie int zfs_recv_queue_length = 16 * 1024 * 1024;
6119b94df9SMatthew Ahrens 
623cb34c60Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
63c1a50c7eSMatthew Ahrens const char *recv_clone_name = "%recv";
643cb34c60Sahrens 
65a2cdcdd2SPaul Dagnelie #define	BP_SPAN(datablkszsec, indblkshift, level) \
66a2cdcdd2SPaul Dagnelie 	(((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
67a2cdcdd2SPaul Dagnelie 	(level) * (indblkshift - SPA_BLKPTRSHIFT)))
68a2cdcdd2SPaul Dagnelie 
69c1a50c7eSMatthew Ahrens static void byteswap_record(dmu_replay_record_t *drr);
70c1a50c7eSMatthew Ahrens 
71a2cdcdd2SPaul Dagnelie struct send_thread_arg {
72a2cdcdd2SPaul Dagnelie 	bqueue_t	q;
73a2cdcdd2SPaul Dagnelie 	dsl_dataset_t	*ds;		/* Dataset to traverse */
74a2cdcdd2SPaul Dagnelie 	uint64_t	fromtxg;	/* Traverse from this txg */
75a2cdcdd2SPaul Dagnelie 	int		flags;		/* flags to pass to traverse_dataset */
76a2cdcdd2SPaul Dagnelie 	int		error_code;
77a2cdcdd2SPaul Dagnelie 	boolean_t	cancel;
78c1a50c7eSMatthew Ahrens 	zbookmark_phys_t resume;
79a2cdcdd2SPaul Dagnelie };
80a2cdcdd2SPaul Dagnelie 
81a2cdcdd2SPaul Dagnelie struct send_block_record {
82a2cdcdd2SPaul Dagnelie 	boolean_t		eos_marker; /* Marks the end of the stream */
83a2cdcdd2SPaul Dagnelie 	blkptr_t		bp;
84a2cdcdd2SPaul Dagnelie 	zbookmark_phys_t	zb;
85a2cdcdd2SPaul Dagnelie 	uint8_t			indblkshift;
86a2cdcdd2SPaul Dagnelie 	uint16_t		datablkszsec;
87a2cdcdd2SPaul Dagnelie 	bqueue_node_t		ln;
88a2cdcdd2SPaul Dagnelie };
89a2cdcdd2SPaul Dagnelie 
90efb80947Sahrens static int
dump_bytes(dmu_sendarg_t * dsp,void * buf,int len)914e3c9f44SBill Pijewski dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
92efb80947Sahrens {
93c1a50c7eSMatthew Ahrens 	dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
94efb80947Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
95fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
96efb80947Sahrens 
974e3c9f44SBill Pijewski 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
98efb80947Sahrens 	    (caddr_t)buf, len,
99efb80947Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
1004e3c9f44SBill Pijewski 
1014e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
1024e3c9f44SBill Pijewski 	*dsp->dsa_off += len;
1034e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
1044e3c9f44SBill Pijewski 
1054e3c9f44SBill Pijewski 	return (dsp->dsa_err);
106efb80947Sahrens }
107efb80947Sahrens 
10898110f08SMatthew Ahrens /*
10998110f08SMatthew Ahrens  * For all record types except BEGIN, fill in the checksum (overlaid in
11098110f08SMatthew Ahrens  * drr_u.drr_checksum.drr_checksum).  The checksum verifies everything
11198110f08SMatthew Ahrens  * up to the start of the checksum itself.
11298110f08SMatthew Ahrens  */
11398110f08SMatthew Ahrens static int
dump_record(dmu_sendarg_t * dsp,void * payload,int payload_len)11498110f08SMatthew Ahrens dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
11598110f08SMatthew Ahrens {
11698110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
11798110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
11898110f08SMatthew Ahrens 	fletcher_4_incremental_native(dsp->dsa_drr,
11998110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
12098110f08SMatthew Ahrens 	    &dsp->dsa_zc);
12198110f08SMatthew Ahrens 	if (dsp->dsa_drr->drr_type != DRR_BEGIN) {
12298110f08SMatthew Ahrens 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
12398110f08SMatthew Ahrens 		    drr_checksum.drr_checksum));
12498110f08SMatthew Ahrens 		dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
12598110f08SMatthew Ahrens 	}
12698110f08SMatthew Ahrens 	fletcher_4_incremental_native(&dsp->dsa_drr->
12798110f08SMatthew Ahrens 	    drr_u.drr_checksum.drr_checksum,
12898110f08SMatthew Ahrens 	    sizeof (zio_cksum_t), &dsp->dsa_zc);
12998110f08SMatthew Ahrens 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
13098110f08SMatthew Ahrens 		return (SET_ERROR(EINTR));
13198110f08SMatthew Ahrens 	if (payload_len != 0) {
13298110f08SMatthew Ahrens 		fletcher_4_incremental_native(payload, payload_len,
13398110f08SMatthew Ahrens 		    &dsp->dsa_zc);
13498110f08SMatthew Ahrens 		if (dump_bytes(dsp, payload, payload_len) != 0)
13598110f08SMatthew Ahrens 			return (SET_ERROR(EINTR));
13698110f08SMatthew Ahrens 	}
13798110f08SMatthew Ahrens 	return (0);
13898110f08SMatthew Ahrens }
13998110f08SMatthew Ahrens 
140efb80947Sahrens static int
dump_free(dmu_sendarg_t * dsp,uint64_t object,uint64_t offset,uint64_t length)1414e3c9f44SBill Pijewski dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
142efb80947Sahrens     uint64_t length)
143efb80947Sahrens {
1444e3c9f44SBill Pijewski 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
1459e69d7d0SLori Alt 
1462f3d8780SMatthew Ahrens 	/*
1472f3d8780SMatthew Ahrens 	 * When we receive a free record, dbuf_free_range() assumes
1482f3d8780SMatthew Ahrens 	 * that the receiving system doesn't have any dbufs in the range
1492f3d8780SMatthew Ahrens 	 * being freed.  This is always true because there is a one-record
1502f3d8780SMatthew Ahrens 	 * constraint: we only send one WRITE record for any given
151c1a50c7eSMatthew Ahrens 	 * object,offset.  We know that the one-record constraint is
1522f3d8780SMatthew Ahrens 	 * true because we always send data in increasing order by
1532f3d8780SMatthew Ahrens 	 * object,offset.
1542f3d8780SMatthew Ahrens 	 *
1552f3d8780SMatthew Ahrens 	 * If the increasing-order constraint ever changes, we should find
1562f3d8780SMatthew Ahrens 	 * another way to assert that the one-record constraint is still
1572f3d8780SMatthew Ahrens 	 * satisfied.
1582f3d8780SMatthew Ahrens 	 */
1592f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
1602f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
1612f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
1622f3d8780SMatthew Ahrens 
1632f3d8780SMatthew Ahrens 	/*
1642f3d8780SMatthew Ahrens 	 * If we are doing a non-incremental send, then there can't
1652f3d8780SMatthew Ahrens 	 * be any data in the dataset we're receiving into.  Therefore
1662f3d8780SMatthew Ahrens 	 * a free record would simply be a no-op.  Save space by not
1672f3d8780SMatthew Ahrens 	 * sending it to begin with.
1682f3d8780SMatthew Ahrens 	 */
1692f3d8780SMatthew Ahrens 	if (!dsp->dsa_incremental)
1702f3d8780SMatthew Ahrens 		return (0);
1712f3d8780SMatthew Ahrens 
172534029e5SSimon Klinkert 	if (length != -1ULL && offset + length < offset)
173534029e5SSimon Klinkert 		length = -1ULL;
174534029e5SSimon Klinkert 
1759e69d7d0SLori Alt 	/*
1769e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
1779e69d7d0SLori Alt 	 * since free block aggregation can only be done for blocks of the
1789e69d7d0SLori Alt 	 * same type (i.e., DRR_FREE records can only be aggregated with
1799e69d7d0SLori Alt 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
1809e69d7d0SLori Alt 	 * aggregated with other DRR_FREEOBJECTS records.
1819e69d7d0SLori Alt 	 */
1824e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
1834e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREE) {
18498110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
185be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1864e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
1879e69d7d0SLori Alt 	}
1889e69d7d0SLori Alt 
1894e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREE) {
1909e69d7d0SLori Alt 		/*
1919e69d7d0SLori Alt 		 * There should never be a PENDING_FREE if length is -1
1929e69d7d0SLori Alt 		 * (because dump_dnode is the only place where this
1939e69d7d0SLori Alt 		 * function is called with a -1, and only after flushing
1949e69d7d0SLori Alt 		 * any pending record).
1959e69d7d0SLori Alt 		 */
1969e69d7d0SLori Alt 		ASSERT(length != -1ULL);
1979e69d7d0SLori Alt 		/*
1989e69d7d0SLori Alt 		 * Check to see whether this free block can be aggregated
1999e69d7d0SLori Alt 		 * with pending one.
2009e69d7d0SLori Alt 		 */
2019e69d7d0SLori Alt 		if (drrf->drr_object == object && drrf->drr_offset +
2029e69d7d0SLori Alt 		    drrf->drr_length == offset) {
2039e69d7d0SLori Alt 			drrf->drr_length += length;
2049e69d7d0SLori Alt 			return (0);
2059e69d7d0SLori Alt 		} else {
2069e69d7d0SLori Alt 			/* not a continuation.  Push out pending record */
20798110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
208be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
2094e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
2109e69d7d0SLori Alt 		}
2119e69d7d0SLori Alt 	}
2129e69d7d0SLori Alt 	/* create a FREE record and make it pending */
2134e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2144e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREE;
2159e69d7d0SLori Alt 	drrf->drr_object = object;
2169e69d7d0SLori Alt 	drrf->drr_offset = offset;
2179e69d7d0SLori Alt 	drrf->drr_length = length;
2184e3c9f44SBill Pijewski 	drrf->drr_toguid = dsp->dsa_toguid;
2199e69d7d0SLori Alt 	if (length == -1ULL) {
22098110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
221be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2229e69d7d0SLori Alt 	} else {
2234e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_FREE;
2249e69d7d0SLori Alt 	}
2259e69d7d0SLori Alt 
226efb80947Sahrens 	return (0);
227efb80947Sahrens }
228efb80947Sahrens 
229efb80947Sahrens static int
dump_write(dmu_sendarg_t * dsp,dmu_object_type_t type,uint64_t object,uint64_t offset,int blksz,const blkptr_t * bp,void * data)2305d7b4d43SMatthew Ahrens dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
2318e714474SLori Alt     uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
232efb80947Sahrens {
2334e3c9f44SBill Pijewski 	struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
2349e69d7d0SLori Alt 
2352f3d8780SMatthew Ahrens 	/*
2362f3d8780SMatthew Ahrens 	 * We send data in increasing object, offset order.
2372f3d8780SMatthew Ahrens 	 * See comment in dump_free() for details.
2382f3d8780SMatthew Ahrens 	 */
2392f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
2402f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
2412f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
2422f3d8780SMatthew Ahrens 	dsp->dsa_last_data_object = object;
2432f3d8780SMatthew Ahrens 	dsp->dsa_last_data_offset = offset + blksz - 1;
2448e714474SLori Alt 
2459e69d7d0SLori Alt 	/*
2469e69d7d0SLori Alt 	 * If there is any kind of pending aggregation (currently either
2479e69d7d0SLori Alt 	 * a grouping of free objects or free blocks), push it out to
2489e69d7d0SLori Alt 	 * the stream, since aggregation can't be done across operations
2499e69d7d0SLori Alt 	 * of different types.
2509e69d7d0SLori Alt 	 */
2514e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
25298110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
253be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2544e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2559e69d7d0SLori Alt 	}
25698110f08SMatthew Ahrens 	/* write a WRITE record */
2574e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2584e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_WRITE;
2599e69d7d0SLori Alt 	drrw->drr_object = object;
2609e69d7d0SLori Alt 	drrw->drr_type = type;
2619e69d7d0SLori Alt 	drrw->drr_offset = offset;
2629e69d7d0SLori Alt 	drrw->drr_length = blksz;
2634e3c9f44SBill Pijewski 	drrw->drr_toguid = dsp->dsa_toguid;
264b5152584SMatthew Ahrens 	if (bp == NULL || BP_IS_EMBEDDED(bp)) {
2655d7b4d43SMatthew Ahrens 		/*
266b5152584SMatthew Ahrens 		 * There's no pre-computed checksum for partial-block
267b5152584SMatthew Ahrens 		 * writes or embedded BP's, so (like
268b5152584SMatthew Ahrens 		 * fletcher4-checkummed blocks) userland will have to
269b5152584SMatthew Ahrens 		 * compute a dedup-capable checksum itself.
2705d7b4d43SMatthew Ahrens 		 */
2715d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
2725d7b4d43SMatthew Ahrens 	} else {
2738e714474SLori Alt 		drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
2748e714474SLori Alt 		if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup)
2758e714474SLori Alt 			drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
2768e714474SLori Alt 		DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
2778e714474SLori Alt 		DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
2788e714474SLori Alt 		DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
2798e714474SLori Alt 		drrw->drr_key.ddk_cksum = bp->blk_cksum;
2805d7b4d43SMatthew Ahrens 	}
281efb80947Sahrens 
28298110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
283be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
284efb80947Sahrens 	return (0);
285efb80947Sahrens }
286efb80947Sahrens 
287efb80947Sahrens static int
dump_write_embedded(dmu_sendarg_t * dsp,uint64_t object,uint64_t offset,int blksz,const blkptr_t * bp)2885d7b4d43SMatthew Ahrens dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
2895d7b4d43SMatthew Ahrens     int blksz, const blkptr_t *bp)
2905d7b4d43SMatthew Ahrens {
2915d7b4d43SMatthew Ahrens 	char buf[BPE_PAYLOAD_SIZE];
2925d7b4d43SMatthew Ahrens 	struct drr_write_embedded *drrw =
2935d7b4d43SMatthew Ahrens 	    &(dsp->dsa_drr->drr_u.drr_write_embedded);
2945d7b4d43SMatthew Ahrens 
2955d7b4d43SMatthew Ahrens 	if (dsp->dsa_pending_op != PENDING_NONE) {
29698110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
2975d7b4d43SMatthew Ahrens 			return (EINTR);
2985d7b4d43SMatthew Ahrens 		dsp->dsa_pending_op = PENDING_NONE;
2995d7b4d43SMatthew Ahrens 	}
3005d7b4d43SMatthew Ahrens 
3015d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp));
3025d7b4d43SMatthew Ahrens 
3035d7b4d43SMatthew Ahrens 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3045d7b4d43SMatthew Ahrens 	dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
3055d7b4d43SMatthew Ahrens 	drrw->drr_object = object;
3065d7b4d43SMatthew Ahrens 	drrw->drr_offset = offset;
3075d7b4d43SMatthew Ahrens 	drrw->drr_length = blksz;
3085d7b4d43SMatthew Ahrens 	drrw->drr_toguid = dsp->dsa_toguid;
3095d7b4d43SMatthew Ahrens 	drrw->drr_compression = BP_GET_COMPRESS(bp);
3105d7b4d43SMatthew Ahrens 	drrw->drr_etype = BPE_GET_ETYPE(bp);
3115d7b4d43SMatthew Ahrens 	drrw->drr_lsize = BPE_GET_LSIZE(bp);
3125d7b4d43SMatthew Ahrens 	drrw->drr_psize = BPE_GET_PSIZE(bp);
3135d7b4d43SMatthew Ahrens 
3145d7b4d43SMatthew Ahrens 	decode_embedded_bp_compressed(bp, buf);
3155d7b4d43SMatthew Ahrens 
31698110f08SMatthew Ahrens 	if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
3175d7b4d43SMatthew Ahrens 		return (EINTR);
3185d7b4d43SMatthew Ahrens 	return (0);
3195d7b4d43SMatthew Ahrens }
3205d7b4d43SMatthew Ahrens 
3215d7b4d43SMatthew Ahrens static int
dump_spill(dmu_sendarg_t * dsp,uint64_t object,int blksz,void * data)3224e3c9f44SBill Pijewski dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
3230a586ceaSMark Shellenbaum {
3244e3c9f44SBill Pijewski 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
3250a586ceaSMark Shellenbaum 
3264e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
32798110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
328be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3294e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3300a586ceaSMark Shellenbaum 	}
3310a586ceaSMark Shellenbaum 
3320a586ceaSMark Shellenbaum 	/* write a SPILL record */
3334e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3344e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_SPILL;
3350a586ceaSMark Shellenbaum 	drrs->drr_object = object;
3360a586ceaSMark Shellenbaum 	drrs->drr_length = blksz;
3374e3c9f44SBill Pijewski 	drrs->drr_toguid = dsp->dsa_toguid;
3380a586ceaSMark Shellenbaum 
33998110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
340be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
3410a586ceaSMark Shellenbaum 	return (0);
3420a586ceaSMark Shellenbaum }
3430a586ceaSMark Shellenbaum 
3440a586ceaSMark Shellenbaum static int
dump_freeobjects(dmu_sendarg_t * dsp,uint64_t firstobj,uint64_t numobjs)3454e3c9f44SBill Pijewski dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
346efb80947Sahrens {
3474e3c9f44SBill Pijewski 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
3489e69d7d0SLori Alt 
3492f3d8780SMatthew Ahrens 	/* See comment in dump_free(). */
3502f3d8780SMatthew Ahrens 	if (!dsp->dsa_incremental)
3512f3d8780SMatthew Ahrens 		return (0);
3522f3d8780SMatthew Ahrens 
3539e69d7d0SLori Alt 	/*
3549e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
3559e69d7d0SLori Alt 	 * push it out, since free block aggregation can only be done for
3569e69d7d0SLori Alt 	 * blocks of the same type (i.e., DRR_FREE records can only be
3579e69d7d0SLori Alt 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
3589e69d7d0SLori Alt 	 * can only be aggregated with other DRR_FREEOBJECTS records.
3599e69d7d0SLori Alt 	 */
3604e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
3614e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
36298110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
363be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3644e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3659e69d7d0SLori Alt 	}
3664e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
3679e69d7d0SLori Alt 		/*
3689e69d7d0SLori Alt 		 * See whether this free object array can be aggregated
3699e69d7d0SLori Alt 		 * with pending one
3709e69d7d0SLori Alt 		 */
3719e69d7d0SLori Alt 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
3729e69d7d0SLori Alt 			drrfo->drr_numobjs += numobjs;
3739e69d7d0SLori Alt 			return (0);
3749e69d7d0SLori Alt 		} else {
3759e69d7d0SLori Alt 			/* can't be aggregated.  Push out pending record */
37698110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
377be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
3784e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
3799e69d7d0SLori Alt 		}
3809e69d7d0SLori Alt 	}
3819e69d7d0SLori Alt 
382efb80947Sahrens 	/* write a FREEOBJECTS record */
3834e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3844e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
3859e69d7d0SLori Alt 	drrfo->drr_firstobj = firstobj;
3869e69d7d0SLori Alt 	drrfo->drr_numobjs = numobjs;
3874e3c9f44SBill Pijewski 	drrfo->drr_toguid = dsp->dsa_toguid;
388efb80947Sahrens 
3894e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
3909e69d7d0SLori Alt 
391efb80947Sahrens 	return (0);
392efb80947Sahrens }
393efb80947Sahrens 
394efb80947Sahrens static int
dump_dnode(dmu_sendarg_t * dsp,uint64_t object,dnode_phys_t * dnp)3954e3c9f44SBill Pijewski dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
396efb80947Sahrens {
3974e3c9f44SBill Pijewski 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
3989e69d7d0SLori Alt 
399c1a50c7eSMatthew Ahrens 	if (object < dsp->dsa_resume_object) {
400c1a50c7eSMatthew Ahrens 		/*
401c1a50c7eSMatthew Ahrens 		 * Note: when resuming, we will visit all the dnodes in
402c1a50c7eSMatthew Ahrens 		 * the block of dnodes that we are resuming from.  In
403c1a50c7eSMatthew Ahrens 		 * this case it's unnecessary to send the dnodes prior to
404c1a50c7eSMatthew Ahrens 		 * the one we are resuming from.  We should be at most one
405c1a50c7eSMatthew Ahrens 		 * block's worth of dnodes behind the resume point.
406c1a50c7eSMatthew Ahrens 		 */
407c1a50c7eSMatthew Ahrens 		ASSERT3U(dsp->dsa_resume_object - object, <,
408c1a50c7eSMatthew Ahrens 		    1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
409c1a50c7eSMatthew Ahrens 		return (0);
410c1a50c7eSMatthew Ahrens 	}
411c1a50c7eSMatthew Ahrens 
412efb80947Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
4134e3c9f44SBill Pijewski 		return (dump_freeobjects(dsp, object, 1));
414efb80947Sahrens 
4154e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
41698110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
417be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
4184e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
4199e69d7d0SLori Alt 	}
4209e69d7d0SLori Alt 
421efb80947Sahrens 	/* write an OBJECT record */
4224e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
4234e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_OBJECT;
4249e69d7d0SLori Alt 	drro->drr_object = object;
4259e69d7d0SLori Alt 	drro->drr_type = dnp->dn_type;
4269e69d7d0SLori Alt 	drro->drr_bonustype = dnp->dn_bonustype;
4279e69d7d0SLori Alt 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
4289e69d7d0SLori Alt 	drro->drr_bonuslen = dnp->dn_bonuslen;
4299e69d7d0SLori Alt 	drro->drr_checksumtype = dnp->dn_checksum;
4309e69d7d0SLori Alt 	drro->drr_compress = dnp->dn_compress;
4314e3c9f44SBill Pijewski 	drro->drr_toguid = dsp->dsa_toguid;
432efb80947Sahrens 
433b5152584SMatthew Ahrens 	if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
434b5152584SMatthew Ahrens 	    drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
435b5152584SMatthew Ahrens 		drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
436b5152584SMatthew Ahrens 
43798110f08SMatthew Ahrens 	if (dump_record(dsp, DN_BONUS(dnp),
43898110f08SMatthew Ahrens 	    P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
439be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
44098110f08SMatthew Ahrens 	}
441efb80947Sahrens 
4422f3d8780SMatthew Ahrens 	/* Free anything past the end of the file. */
4434e3c9f44SBill Pijewski 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
4442f3d8780SMatthew Ahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
445be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
4463b2aab18SMatthew Ahrens 	if (dsp->dsa_err != 0)
447be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
448efb80947Sahrens 	return (0);
449efb80947Sahrens }
450efb80947Sahrens 
4515d7b4d43SMatthew Ahrens static boolean_t
backup_do_embed(dmu_sendarg_t * dsp,const blkptr_t * bp)4525d7b4d43SMatthew Ahrens backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
4535d7b4d43SMatthew Ahrens {
4545d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp))
4555d7b4d43SMatthew Ahrens 		return (B_FALSE);
4565d7b4d43SMatthew Ahrens 
4575d7b4d43SMatthew Ahrens 	/*
4585d7b4d43SMatthew Ahrens 	 * Compression function must be legacy, or explicitly enabled.
4595d7b4d43SMatthew Ahrens 	 */
4605d7b4d43SMatthew Ahrens 	if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
4615d7b4d43SMatthew Ahrens 	    !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
4625d7b4d43SMatthew Ahrens 		return (B_FALSE);
4635d7b4d43SMatthew Ahrens 
4645d7b4d43SMatthew Ahrens 	/*
4655d7b4d43SMatthew Ahrens 	 * Embed type must be explicitly enabled.
4665d7b4d43SMatthew Ahrens 	 */
4675d7b4d43SMatthew Ahrens 	switch (BPE_GET_ETYPE(bp)) {
4685d7b4d43SMatthew Ahrens 	case BP_EMBEDDED_TYPE_DATA:
4695d7b4d43SMatthew Ahrens 		if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
4705d7b4d43SMatthew Ahrens 			return (B_TRUE);
4715d7b4d43SMatthew Ahrens 		break;
4725d7b4d43SMatthew Ahrens 	default:
4735d7b4d43SMatthew Ahrens 		return (B_FALSE);
4745d7b4d43SMatthew Ahrens 	}
4755d7b4d43SMatthew Ahrens 	return (B_FALSE);
4765d7b4d43SMatthew Ahrens }
4775d7b4d43SMatthew Ahrens 
478a2cdcdd2SPaul Dagnelie /*
479a2cdcdd2SPaul Dagnelie  * This is the callback function to traverse_dataset that acts as the worker
480a2cdcdd2SPaul Dagnelie  * thread for dmu_send_impl.
481a2cdcdd2SPaul Dagnelie  */
482b24ab676SJeff Bonwick /*ARGSUSED*/
483efb80947Sahrens static int
send_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const struct dnode_phys * dnp,void * arg)484a2cdcdd2SPaul Dagnelie send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
485a2cdcdd2SPaul Dagnelie     const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
486efb80947Sahrens {
487a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *sta = arg;
488a2cdcdd2SPaul Dagnelie 	struct send_block_record *record;
489a2cdcdd2SPaul Dagnelie 	uint64_t record_size;
490a2cdcdd2SPaul Dagnelie 	int err = 0;
491a2cdcdd2SPaul Dagnelie 
492c1a50c7eSMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
493c1a50c7eSMatthew Ahrens 	    zb->zb_object >= sta->resume.zb_object);
494c1a50c7eSMatthew Ahrens 
495a2cdcdd2SPaul Dagnelie 	if (sta->cancel)
496a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINTR));
497a2cdcdd2SPaul Dagnelie 
498a2cdcdd2SPaul Dagnelie 	if (bp == NULL) {
499a2cdcdd2SPaul Dagnelie 		ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
500a2cdcdd2SPaul Dagnelie 		return (0);
501a2cdcdd2SPaul Dagnelie 	} else if (zb->zb_level < 0) {
502a2cdcdd2SPaul Dagnelie 		return (0);
503a2cdcdd2SPaul Dagnelie 	}
504a2cdcdd2SPaul Dagnelie 
505a2cdcdd2SPaul Dagnelie 	record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
506a2cdcdd2SPaul Dagnelie 	record->eos_marker = B_FALSE;
507a2cdcdd2SPaul Dagnelie 	record->bp = *bp;
508a2cdcdd2SPaul Dagnelie 	record->zb = *zb;
509a2cdcdd2SPaul Dagnelie 	record->indblkshift = dnp->dn_indblkshift;
510a2cdcdd2SPaul Dagnelie 	record->datablkszsec = dnp->dn_datablkszsec;
511a2cdcdd2SPaul Dagnelie 	record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
512a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&sta->q, record, record_size);
513a2cdcdd2SPaul Dagnelie 
514a2cdcdd2SPaul Dagnelie 	return (err);
515a2cdcdd2SPaul Dagnelie }
516a2cdcdd2SPaul Dagnelie 
517a2cdcdd2SPaul Dagnelie /*
518a2cdcdd2SPaul Dagnelie  * This function kicks off the traverse_dataset.  It also handles setting the
519a2cdcdd2SPaul Dagnelie  * error code of the thread in case something goes wrong, and pushes the End of
520a2cdcdd2SPaul Dagnelie  * Stream record when the traverse_dataset call has finished.  If there is no
521a2cdcdd2SPaul Dagnelie  * dataset to traverse, the thread immediately pushes End of Stream marker.
522a2cdcdd2SPaul Dagnelie  */
523a2cdcdd2SPaul Dagnelie static void
send_traverse_thread(void * arg)524a2cdcdd2SPaul Dagnelie send_traverse_thread(void *arg)
525a2cdcdd2SPaul Dagnelie {
526a2cdcdd2SPaul Dagnelie 	struct send_thread_arg *st_arg = arg;
527a2cdcdd2SPaul Dagnelie 	int err;
528a2cdcdd2SPaul Dagnelie 	struct send_block_record *data;
529a2cdcdd2SPaul Dagnelie 
530a2cdcdd2SPaul Dagnelie 	if (st_arg->ds != NULL) {
531c1a50c7eSMatthew Ahrens 		err = traverse_dataset_resume(st_arg->ds,
532c1a50c7eSMatthew Ahrens 		    st_arg->fromtxg, &st_arg->resume,
533c1a50c7eSMatthew Ahrens 		    st_arg->flags, send_cb, st_arg);
534c1a50c7eSMatthew Ahrens 
535a2cdcdd2SPaul Dagnelie 		if (err != EINTR)
536a2cdcdd2SPaul Dagnelie 			st_arg->error_code = err;
537a2cdcdd2SPaul Dagnelie 	}
538a2cdcdd2SPaul Dagnelie 	data = kmem_zalloc(sizeof (*data), KM_SLEEP);
539a2cdcdd2SPaul Dagnelie 	data->eos_marker = B_TRUE;
540a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&st_arg->q, data, 1);
541a2cdcdd2SPaul Dagnelie }
542a2cdcdd2SPaul Dagnelie 
543a2cdcdd2SPaul Dagnelie /*
544a2cdcdd2SPaul Dagnelie  * This function actually handles figuring out what kind of record needs to be
545a2cdcdd2SPaul Dagnelie  * dumped, reading the data (which has hopefully been prefetched), and calling
546a2cdcdd2SPaul Dagnelie  * the appropriate helper function.
547a2cdcdd2SPaul Dagnelie  */
548a2cdcdd2SPaul Dagnelie static int
do_dump(dmu_sendarg_t * dsa,struct send_block_record * data)549a2cdcdd2SPaul Dagnelie do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
550a2cdcdd2SPaul Dagnelie {
551a2cdcdd2SPaul Dagnelie 	dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
552a2cdcdd2SPaul Dagnelie 	const blkptr_t *bp = &data->bp;
553a2cdcdd2SPaul Dagnelie 	const zbookmark_phys_t *zb = &data->zb;
554a2cdcdd2SPaul Dagnelie 	uint8_t indblkshift = data->indblkshift;
555a2cdcdd2SPaul Dagnelie 	uint16_t dblkszsec = data->datablkszsec;
556a2cdcdd2SPaul Dagnelie 	spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
557efb80947Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
558efb80947Sahrens 	int err = 0;
559efb80947Sahrens 
560a2cdcdd2SPaul Dagnelie 	ASSERT3U(zb->zb_level, >=, 0);
561efb80947Sahrens 
562c1a50c7eSMatthew Ahrens 	ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
563c1a50c7eSMatthew Ahrens 	    zb->zb_object >= dsa->dsa_resume_object);
564c1a50c7eSMatthew Ahrens 
565b24ab676SJeff Bonwick 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
566b24ab676SJeff Bonwick 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
56714843421SMatthew Ahrens 		return (0);
56843466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp) &&
56943466aaeSMax Grossman 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
570a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
57188b7b0f2SMatthew Ahrens 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
572a2cdcdd2SPaul Dagnelie 		err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
57343466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp)) {
574a2cdcdd2SPaul Dagnelie 		uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
575a2cdcdd2SPaul Dagnelie 		uint64_t offset = zb->zb_blkid * span;
576a2cdcdd2SPaul Dagnelie 		err = dump_free(dsa, zb->zb_object, offset, span);
57788b7b0f2SMatthew Ahrens 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
57888b7b0f2SMatthew Ahrens 		return (0);
57988b7b0f2SMatthew Ahrens 	} else if (type == DMU_OT_DNODE) {
580efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
5817adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
58288b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
583efb80947Sahrens 
584a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
585a2cdcdd2SPaul Dagnelie 
5861b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
5871b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
5881b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
589be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
59088b7b0f2SMatthew Ahrens 
591a2cdcdd2SPaul Dagnelie 		dnode_phys_t *blk = abuf->b_data;
592a2cdcdd2SPaul Dagnelie 		uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
593a2cdcdd2SPaul Dagnelie 		for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
594a2cdcdd2SPaul Dagnelie 			err = dump_dnode(dsa, dnobj + i, blk + i);
5953b2aab18SMatthew Ahrens 			if (err != 0)
596efb80947Sahrens 				break;
597efb80947Sahrens 		}
59888b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
5990a586ceaSMark Shellenbaum 	} else if (type == DMU_OT_SA) {
6007adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
6010a586ceaSMark Shellenbaum 		arc_buf_t *abuf;
6020a586ceaSMark Shellenbaum 		int blksz = BP_GET_LSIZE(bp);
6030a586ceaSMark Shellenbaum 
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));
6080a586ceaSMark Shellenbaum 
609a2cdcdd2SPaul Dagnelie 		err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
6100a586ceaSMark Shellenbaum 		(void) arc_buf_remove_ref(abuf, &abuf);
611a2cdcdd2SPaul Dagnelie 	} else if (backup_do_embed(dsa, bp)) {
6125d7b4d43SMatthew Ahrens 		/* it's an embedded level-0 block of a regular object */
613a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
614a2cdcdd2SPaul Dagnelie 		ASSERT0(zb->zb_level);
615a2cdcdd2SPaul Dagnelie 		err = dump_write_embedded(dsa, zb->zb_object,
6165d7b4d43SMatthew Ahrens 		    zb->zb_blkid * blksz, blksz, bp);
617a2cdcdd2SPaul Dagnelie 	} else {
618a2cdcdd2SPaul Dagnelie 		/* it's a level-0 block of a regular object */
6197adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
620efb80947Sahrens 		arc_buf_t *abuf;
621a2cdcdd2SPaul Dagnelie 		int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
622b5152584SMatthew Ahrens 		uint64_t offset;
623efb80947Sahrens 
62478f17100SMatthew Ahrens 		ASSERT0(zb->zb_level);
625c1a50c7eSMatthew Ahrens 		ASSERT(zb->zb_object > dsa->dsa_resume_object ||
626c1a50c7eSMatthew Ahrens 		    (zb->zb_object == dsa->dsa_resume_object &&
627c1a50c7eSMatthew Ahrens 		    zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
628c1a50c7eSMatthew Ahrens 
6291b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
6301b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
6311b912ec7SGeorge Wilson 		    &aflags, zb) != 0) {
63219b94df9SMatthew Ahrens 			if (zfs_send_corrupt_data) {
63319b94df9SMatthew Ahrens 				/* Send a block filled with 0x"zfs badd bloc" */
63419b94df9SMatthew Ahrens 				abuf = arc_buf_alloc(spa, blksz, &abuf,
63519b94df9SMatthew Ahrens 				    ARC_BUFC_DATA);
63619b94df9SMatthew Ahrens 				uint64_t *ptr;
63719b94df9SMatthew Ahrens 				for (ptr = abuf->b_data;
63819b94df9SMatthew Ahrens 				    (char *)ptr < (char *)abuf->b_data + blksz;
63919b94df9SMatthew Ahrens 				    ptr++)
6408c76e076SBrian Behlendorf 					*ptr = 0x2f5baddb10cULL;
64119b94df9SMatthew Ahrens 			} else {
642be6fd75aSMatthew Ahrens 				return (SET_ERROR(EIO));
64319b94df9SMatthew Ahrens 			}
64419b94df9SMatthew Ahrens 		}
645efb80947Sahrens 
646b5152584SMatthew Ahrens 		offset = zb->zb_blkid * blksz;
647b5152584SMatthew Ahrens 
648a2cdcdd2SPaul Dagnelie 		if (!(dsa->dsa_featureflags &
649b5152584SMatthew Ahrens 		    DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
650b5152584SMatthew Ahrens 		    blksz > SPA_OLD_MAXBLOCKSIZE) {
651b5152584SMatthew Ahrens 			char *buf = abuf->b_data;
652b5152584SMatthew Ahrens 			while (blksz > 0 && err == 0) {
653b5152584SMatthew Ahrens 				int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
654a2cdcdd2SPaul Dagnelie 				err = dump_write(dsa, type, zb->zb_object,
655b5152584SMatthew Ahrens 				    offset, n, NULL, buf);
656b5152584SMatthew Ahrens 				offset += n;
657b5152584SMatthew Ahrens 				buf += n;
658b5152584SMatthew Ahrens 				blksz -= n;
659b5152584SMatthew Ahrens 			}
660b5152584SMatthew Ahrens 		} else {
661a2cdcdd2SPaul Dagnelie 			err = dump_write(dsa, type, zb->zb_object,
662b5152584SMatthew Ahrens 			    offset, blksz, bp, abuf->b_data);
663b5152584SMatthew Ahrens 		}
664efb80947Sahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
665efb80947Sahrens 	}
666efb80947Sahrens 
667efb80947Sahrens 	ASSERT(err == 0 || err == EINTR);
668efb80947Sahrens 	return (err);
669efb80947Sahrens }
670efb80947Sahrens 
6714445fffbSMatthew Ahrens /*
672a2cdcdd2SPaul Dagnelie  * Pop the new data off the queue, and free the old data.
673a2cdcdd2SPaul Dagnelie  */
674a2cdcdd2SPaul Dagnelie static struct send_block_record *
get_next_record(bqueue_t * bq,struct send_block_record * data)675a2cdcdd2SPaul Dagnelie get_next_record(bqueue_t *bq, struct send_block_record *data)
676a2cdcdd2SPaul Dagnelie {
677a2cdcdd2SPaul Dagnelie 	struct send_block_record *tmp = bqueue_dequeue(bq);
678a2cdcdd2SPaul Dagnelie 	kmem_free(data, sizeof (*data));
679a2cdcdd2SPaul Dagnelie 	return (tmp);
680a2cdcdd2SPaul Dagnelie }
681a2cdcdd2SPaul Dagnelie 
682a2cdcdd2SPaul Dagnelie /*
683a2cdcdd2SPaul Dagnelie  * Actually do the bulk of the work in a zfs send.
684a2cdcdd2SPaul Dagnelie  *
685a2cdcdd2SPaul Dagnelie  * Note: Releases dp using the specified tag.
6864445fffbSMatthew Ahrens  */
6873b2aab18SMatthew Ahrens static int
dmu_send_impl(void * tag,dsl_pool_t * dp,dsl_dataset_t * to_ds,zfs_bookmark_phys_t * ancestor_zb,boolean_t is_clone,boolean_t embedok,boolean_t large_block_ok,int outfd,uint64_t resumeobj,uint64_t resumeoff,vnode_t * vp,offset_t * off)688a2cdcdd2SPaul Dagnelie dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
689c1a50c7eSMatthew Ahrens     zfs_bookmark_phys_t *ancestor_zb,
690c1a50c7eSMatthew Ahrens     boolean_t is_clone, boolean_t embedok, boolean_t large_block_ok, int outfd,
691c1a50c7eSMatthew Ahrens     uint64_t resumeobj, uint64_t resumeoff,
692c1a50c7eSMatthew Ahrens     vnode_t *vp, offset_t *off)
6934445fffbSMatthew Ahrens {
6943b2aab18SMatthew Ahrens 	objset_t *os;
695efb80947Sahrens 	dmu_replay_record_t *drr;
6964e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp;
697efb80947Sahrens 	int err;
6983cb34c60Sahrens 	uint64_t fromtxg = 0;
6995d7b4d43SMatthew Ahrens 	uint64_t featureflags = 0;
700c1a50c7eSMatthew Ahrens 	struct send_thread_arg to_arg = { 0 };
701efb80947Sahrens 
702a2cdcdd2SPaul Dagnelie 	err = dmu_objset_from_ds(to_ds, &os);
7033b2aab18SMatthew Ahrens 	if (err != 0) {
7043b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
7053b2aab18SMatthew Ahrens 		return (err);
7063b2aab18SMatthew Ahrens 	}
707efb80947Sahrens 
708efb80947Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
709efb80947Sahrens 	drr->drr_type = DRR_BEGIN;
710efb80947Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
7119e69d7d0SLori Alt 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
7129e69d7d0SLori Alt 	    DMU_SUBSTREAM);
713dc7cd546SMark Shellenbaum 
714dc7cd546SMark Shellenbaum #ifdef _KERNEL
7153b2aab18SMatthew Ahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
716dc7cd546SMark Shellenbaum 		uint64_t version;
7173b2aab18SMatthew Ahrens 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
7184e3c9f44SBill Pijewski 			kmem_free(drr, sizeof (dmu_replay_record_t));
7193b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, tag);
720be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
7214e3c9f44SBill Pijewski 		}
7223b2aab18SMatthew Ahrens 		if (version >= ZPL_VERSION_SA) {
7235d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
724dc7cd546SMark Shellenbaum 		}
725dc7cd546SMark Shellenbaum 	}
726dc7cd546SMark Shellenbaum #endif
727dc7cd546SMark Shellenbaum 
728f22efbdaSMatthew Ahrens 	if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
729b5152584SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
7305d7b4d43SMatthew Ahrens 	if (embedok &&
7315d7b4d43SMatthew Ahrens 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
7325d7b4d43SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
7335d7b4d43SMatthew Ahrens 		if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
7345d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
7355d7b4d43SMatthew Ahrens 	}
7365d7b4d43SMatthew Ahrens 
737c1a50c7eSMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
738c1a50c7eSMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_RESUMING;
739c1a50c7eSMatthew Ahrens 	}
740c1a50c7eSMatthew Ahrens 
7415d7b4d43SMatthew Ahrens 	DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
7425d7b4d43SMatthew Ahrens 	    featureflags);
7435d7b4d43SMatthew Ahrens 
744efb80947Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
745a2cdcdd2SPaul Dagnelie 	    dsl_dataset_phys(to_ds)->ds_creation_time;
7463b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
74778f17100SMatthew Ahrens 	if (is_clone)
7483cb34c60Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
749a2cdcdd2SPaul Dagnelie 	drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
750a2cdcdd2SPaul Dagnelie 	if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
751ab04eb8eStimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
752ab04eb8eStimh 
753a2cdcdd2SPaul Dagnelie 	if (ancestor_zb != NULL) {
754a2cdcdd2SPaul Dagnelie 		drr->drr_u.drr_begin.drr_fromguid =
755a2cdcdd2SPaul Dagnelie 		    ancestor_zb->zbm_guid;
756a2cdcdd2SPaul Dagnelie 		fromtxg = ancestor_zb->zbm_creation_txg;
75778f17100SMatthew Ahrens 	}
758a2cdcdd2SPaul Dagnelie 	dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
759a2cdcdd2SPaul Dagnelie 	if (!to_ds->ds_is_snapshot) {
76078f17100SMatthew Ahrens 		(void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
76178f17100SMatthew Ahrens 		    sizeof (drr->drr_u.drr_begin.drr_toname));
7623b2aab18SMatthew Ahrens 	}
7633cb34c60Sahrens 
7644e3c9f44SBill Pijewski 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
765efb80947Sahrens 
7664e3c9f44SBill Pijewski 	dsp->dsa_drr = drr;
7674e3c9f44SBill Pijewski 	dsp->dsa_vp = vp;
7684e3c9f44SBill Pijewski 	dsp->dsa_outfd = outfd;
7694e3c9f44SBill Pijewski 	dsp->dsa_proc = curproc;
7703b2aab18SMatthew Ahrens 	dsp->dsa_os = os;
7714e3c9f44SBill Pijewski 	dsp->dsa_off = off;
772a2cdcdd2SPaul Dagnelie 	dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
7734e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_NONE;
774a2cdcdd2SPaul Dagnelie 	dsp->dsa_incremental = (ancestor_zb != NULL);
7755d7b4d43SMatthew Ahrens 	dsp->dsa_featureflags = featureflags;
776c1a50c7eSMatthew Ahrens 	dsp->dsa_resume_object = resumeobj;
777c1a50c7eSMatthew Ahrens 	dsp->dsa_resume_offset = resumeoff;
7784e3c9f44SBill Pijewski 
779a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
780a2cdcdd2SPaul Dagnelie 	list_insert_head(&to_ds->ds_sendstreams, dsp);
781a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
7824e3c9f44SBill Pijewski 
783a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_hold(to_ds, FTAG);
784de8d9cffSMatthew Ahrens 	dsl_pool_rele(dp, tag);
785de8d9cffSMatthew Ahrens 
786c1a50c7eSMatthew Ahrens 	void *payload = NULL;
787c1a50c7eSMatthew Ahrens 	size_t payload_len = 0;
788c1a50c7eSMatthew Ahrens 	if (resumeobj != 0 || resumeoff != 0) {
789c1a50c7eSMatthew Ahrens 		dmu_object_info_t to_doi;
790c1a50c7eSMatthew Ahrens 		err = dmu_object_info(os, resumeobj, &to_doi);
791c1a50c7eSMatthew Ahrens 		if (err != 0)
792c1a50c7eSMatthew Ahrens 			goto out;
793c1a50c7eSMatthew Ahrens 		SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
794c1a50c7eSMatthew Ahrens 		    resumeoff / to_doi.doi_data_block_size);
795c1a50c7eSMatthew Ahrens 
796c1a50c7eSMatthew Ahrens 		nvlist_t *nvl = fnvlist_alloc();
797c1a50c7eSMatthew Ahrens 		fnvlist_add_uint64(nvl, "resume_object", resumeobj);
798c1a50c7eSMatthew Ahrens 		fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
799c1a50c7eSMatthew Ahrens 		payload = fnvlist_pack(nvl, &payload_len);
800c1a50c7eSMatthew Ahrens 		drr->drr_payloadlen = payload_len;
801c1a50c7eSMatthew Ahrens 		fnvlist_free(nvl);
802c1a50c7eSMatthew Ahrens 	}
803c1a50c7eSMatthew Ahrens 
804c1a50c7eSMatthew Ahrens 	err = dump_record(dsp, payload, payload_len);
805c1a50c7eSMatthew Ahrens 	fnvlist_pack_free(payload, payload_len);
806c1a50c7eSMatthew Ahrens 	if (err != 0) {
8074e3c9f44SBill Pijewski 		err = dsp->dsa_err;
8084e3c9f44SBill Pijewski 		goto out;
809efb80947Sahrens 	}
810efb80947Sahrens 
811a2cdcdd2SPaul Dagnelie 	err = bqueue_init(&to_arg.q, zfs_send_queue_length,
812a2cdcdd2SPaul Dagnelie 	    offsetof(struct send_block_record, ln));
813a2cdcdd2SPaul Dagnelie 	to_arg.error_code = 0;
814a2cdcdd2SPaul Dagnelie 	to_arg.cancel = B_FALSE;
815a2cdcdd2SPaul Dagnelie 	to_arg.ds = to_ds;
816a2cdcdd2SPaul Dagnelie 	to_arg.fromtxg = fromtxg;
817a2cdcdd2SPaul Dagnelie 	to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
818a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
819a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
820a2cdcdd2SPaul Dagnelie 
821a2cdcdd2SPaul Dagnelie 	struct send_block_record *to_data;
822a2cdcdd2SPaul Dagnelie 	to_data = bqueue_dequeue(&to_arg.q);
823a2cdcdd2SPaul Dagnelie 
824a2cdcdd2SPaul Dagnelie 	while (!to_data->eos_marker && err == 0) {
825a2cdcdd2SPaul Dagnelie 		err = do_dump(dsp, to_data);
826a2cdcdd2SPaul Dagnelie 		to_data = get_next_record(&to_arg.q, to_data);
827a2cdcdd2SPaul Dagnelie 		if (issig(JUSTLOOKING) && issig(FORREAL))
828a2cdcdd2SPaul Dagnelie 			err = EINTR;
829a2cdcdd2SPaul Dagnelie 	}
830a2cdcdd2SPaul Dagnelie 
831a2cdcdd2SPaul Dagnelie 	if (err != 0) {
832a2cdcdd2SPaul Dagnelie 		to_arg.cancel = B_TRUE;
833a2cdcdd2SPaul Dagnelie 		while (!to_data->eos_marker) {
834a2cdcdd2SPaul Dagnelie 			to_data = get_next_record(&to_arg.q, to_data);
835a2cdcdd2SPaul Dagnelie 		}
836a2cdcdd2SPaul Dagnelie 	}
837a2cdcdd2SPaul Dagnelie 	kmem_free(to_data, sizeof (*to_data));
838a2cdcdd2SPaul Dagnelie 
839a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&to_arg.q);
840a2cdcdd2SPaul Dagnelie 
841a2cdcdd2SPaul Dagnelie 	if (err == 0 && to_arg.error_code != 0)
842a2cdcdd2SPaul Dagnelie 		err = to_arg.error_code;
843a2cdcdd2SPaul Dagnelie 
844a2cdcdd2SPaul Dagnelie 	if (err != 0)
845a2cdcdd2SPaul Dagnelie 		goto out;
846efb80947Sahrens 
8474e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE)
84898110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
849be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINTR);
8509e69d7d0SLori Alt 
8513b2aab18SMatthew Ahrens 	if (err != 0) {
8523b2aab18SMatthew Ahrens 		if (err == EINTR && dsp->dsa_err != 0)
8534e3c9f44SBill Pijewski 			err = dsp->dsa_err;
8544e3c9f44SBill Pijewski 		goto out;
855efb80947Sahrens 	}
856efb80947Sahrens 
857efb80947Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
858efb80947Sahrens 	drr->drr_type = DRR_END;
8594e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
8604e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
861efb80947Sahrens 
862a2cdcdd2SPaul Dagnelie 	if (dump_record(dsp, NULL, 0) != 0)
8634e3c9f44SBill Pijewski 		err = dsp->dsa_err;
864efb80947Sahrens 
8654e3c9f44SBill Pijewski out:
866a2cdcdd2SPaul Dagnelie 	mutex_enter(&to_ds->ds_sendstream_lock);
867a2cdcdd2SPaul Dagnelie 	list_remove(&to_ds->ds_sendstreams, dsp);
868a2cdcdd2SPaul Dagnelie 	mutex_exit(&to_ds->ds_sendstream_lock);
869efb80947Sahrens 
8704e3c9f44SBill Pijewski 	kmem_free(drr, sizeof (dmu_replay_record_t));
8714e3c9f44SBill Pijewski 	kmem_free(dsp, sizeof (dmu_sendarg_t));
8724e3c9f44SBill Pijewski 
873a2cdcdd2SPaul Dagnelie 	dsl_dataset_long_rele(to_ds, FTAG);
8743b2aab18SMatthew Ahrens 
8754e3c9f44SBill Pijewski 	return (err);
876efb80947Sahrens }
877efb80947Sahrens 
87819b94df9SMatthew Ahrens int
dmu_send_obj(const char * pool,uint64_t tosnap,uint64_t fromsnap,boolean_t embedok,boolean_t large_block_ok,int outfd,vnode_t * vp,offset_t * off)8793b2aab18SMatthew Ahrens dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
880b5152584SMatthew Ahrens     boolean_t embedok, boolean_t large_block_ok,
881b5152584SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
88219b94df9SMatthew Ahrens {
8833b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
8843b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
8853b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
8863b2aab18SMatthew Ahrens 	int err;
8873b2aab18SMatthew Ahrens 
8883b2aab18SMatthew Ahrens 	err = dsl_pool_hold(pool, FTAG, &dp);
8893b2aab18SMatthew Ahrens 	if (err != 0)
8903b2aab18SMatthew Ahrens 		return (err);
8913b2aab18SMatthew Ahrens 
8923b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
8933b2aab18SMatthew Ahrens 	if (err != 0) {
8943b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
8953b2aab18SMatthew Ahrens 		return (err);
8963b2aab18SMatthew Ahrens 	}
8973b2aab18SMatthew Ahrens 
8983b2aab18SMatthew Ahrens 	if (fromsnap != 0) {
89978f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
90078f17100SMatthew Ahrens 		boolean_t is_clone;
90178f17100SMatthew Ahrens 
9023b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
9033b2aab18SMatthew Ahrens 		if (err != 0) {
9043b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
9053b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
9063b2aab18SMatthew Ahrens 			return (err);
9073b2aab18SMatthew Ahrens 		}
90878f17100SMatthew Ahrens 		if (!dsl_dataset_is_before(ds, fromds, 0))
90978f17100SMatthew Ahrens 			err = SET_ERROR(EXDEV);
910c1379625SJustin T. Gibbs 		zb.zbm_creation_time =
911c1379625SJustin T. Gibbs 		    dsl_dataset_phys(fromds)->ds_creation_time;
912c1379625SJustin T. Gibbs 		zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
913c1379625SJustin T. Gibbs 		zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
91478f17100SMatthew Ahrens 		is_clone = (fromds->ds_dir != ds->ds_dir);
91578f17100SMatthew Ahrens 		dsl_dataset_rele(fromds, FTAG);
916b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
917c1a50c7eSMatthew Ahrens 		    embedok, large_block_ok, outfd, 0, 0, vp, off);
91878f17100SMatthew Ahrens 	} else {
919b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
920c1a50c7eSMatthew Ahrens 		    embedok, large_block_ok, outfd, 0, 0, vp, off);
9213b2aab18SMatthew Ahrens 	}
92278f17100SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
92378f17100SMatthew Ahrens 	return (err);
9243b2aab18SMatthew Ahrens }
9253b2aab18SMatthew Ahrens 
9263b2aab18SMatthew Ahrens int
dmu_send(const char * tosnap,const char * fromsnap,boolean_t embedok,boolean_t large_block_ok,int outfd,uint64_t resumeobj,uint64_t resumeoff,vnode_t * vp,offset_t * off)927c1a50c7eSMatthew Ahrens dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
928c1a50c7eSMatthew Ahrens     boolean_t large_block_ok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
929c1a50c7eSMatthew Ahrens     vnode_t *vp, offset_t *off)
9303b2aab18SMatthew Ahrens {
9313b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
9323b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
9333b2aab18SMatthew Ahrens 	int err;
93478f17100SMatthew Ahrens 	boolean_t owned = B_FALSE;
9353b2aab18SMatthew Ahrens 
93678f17100SMatthew Ahrens 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
937be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
9383b2aab18SMatthew Ahrens 
9393b2aab18SMatthew Ahrens 	err = dsl_pool_hold(tosnap, FTAG, &dp);
9403b2aab18SMatthew Ahrens 	if (err != 0)
9413b2aab18SMatthew Ahrens 		return (err);
9423b2aab18SMatthew Ahrens 
94378f17100SMatthew Ahrens 	if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
94478f17100SMatthew Ahrens 		/*
94578f17100SMatthew Ahrens 		 * We are sending a filesystem or volume.  Ensure
94678f17100SMatthew Ahrens 		 * that it doesn't change by owning the dataset.
94778f17100SMatthew Ahrens 		 */
94878f17100SMatthew Ahrens 		err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
94978f17100SMatthew Ahrens 		owned = B_TRUE;
95078f17100SMatthew Ahrens 	} else {
9513b2aab18SMatthew Ahrens 		err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
95278f17100SMatthew Ahrens 	}
9533b2aab18SMatthew Ahrens 	if (err != 0) {
9543b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
9553b2aab18SMatthew Ahrens 		return (err);
9563b2aab18SMatthew Ahrens 	}
9573b2aab18SMatthew Ahrens 
9583b2aab18SMatthew Ahrens 	if (fromsnap != NULL) {
95978f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
96078f17100SMatthew Ahrens 		boolean_t is_clone = B_FALSE;
96178f17100SMatthew Ahrens 		int fsnamelen = strchr(tosnap, '@') - tosnap;
96278f17100SMatthew Ahrens 
96378f17100SMatthew Ahrens 		/*
96478f17100SMatthew Ahrens 		 * If the fromsnap is in a different filesystem, then
96578f17100SMatthew Ahrens 		 * mark the send stream as a clone.
96678f17100SMatthew Ahrens 		 */
96778f17100SMatthew Ahrens 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
96878f17100SMatthew Ahrens 		    (fromsnap[fsnamelen] != '@' &&
96978f17100SMatthew Ahrens 		    fromsnap[fsnamelen] != '#')) {
97078f17100SMatthew Ahrens 			is_clone = B_TRUE;
97178f17100SMatthew Ahrens 		}
97278f17100SMatthew Ahrens 
97378f17100SMatthew Ahrens 		if (strchr(fromsnap, '@')) {
97478f17100SMatthew Ahrens 			dsl_dataset_t *fromds;
9753b2aab18SMatthew Ahrens 			err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
97678f17100SMatthew Ahrens 			if (err == 0) {
97778f17100SMatthew Ahrens 				if (!dsl_dataset_is_before(ds, fromds, 0))
97878f17100SMatthew Ahrens 					err = SET_ERROR(EXDEV);
97978f17100SMatthew Ahrens 				zb.zbm_creation_time =
980c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_time;
98178f17100SMatthew Ahrens 				zb.zbm_creation_txg =
982c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_txg;
983c1379625SJustin T. Gibbs 				zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
98478f17100SMatthew Ahrens 				is_clone = (ds->ds_dir != fromds->ds_dir);
98578f17100SMatthew Ahrens 				dsl_dataset_rele(fromds, FTAG);
98678f17100SMatthew Ahrens 			}
98778f17100SMatthew Ahrens 		} else {
98878f17100SMatthew Ahrens 			err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
98978f17100SMatthew Ahrens 		}
9903b2aab18SMatthew Ahrens 		if (err != 0) {
9913b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
9923b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
9933b2aab18SMatthew Ahrens 			return (err);
9943b2aab18SMatthew Ahrens 		}
995b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
996c1a50c7eSMatthew Ahrens 		    embedok, large_block_ok,
997c1a50c7eSMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
99878f17100SMatthew Ahrens 	} else {
999b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1000c1a50c7eSMatthew Ahrens 		    embedok, large_block_ok,
1001c1a50c7eSMatthew Ahrens 		    outfd, resumeobj, resumeoff, vp, off);
10023b2aab18SMatthew Ahrens 	}
100378f17100SMatthew Ahrens 	if (owned)
100478f17100SMatthew Ahrens 		dsl_dataset_disown(ds, FTAG);
100578f17100SMatthew Ahrens 	else
100678f17100SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
100778f17100SMatthew Ahrens 	return (err);
10083b2aab18SMatthew Ahrens }
10093b2aab18SMatthew Ahrens 
1010643da460SMax Grossman static int
dmu_adjust_send_estimate_for_indirects(dsl_dataset_t * ds,uint64_t size,uint64_t * sizep)1011643da460SMax Grossman dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t size,
1012643da460SMax Grossman     uint64_t *sizep)
1013643da460SMax Grossman {
1014643da460SMax Grossman 	int err;
1015643da460SMax Grossman 	/*
1016643da460SMax Grossman 	 * Assume that space (both on-disk and in-stream) is dominated by
1017643da460SMax Grossman 	 * data.  We will adjust for indirect blocks and the copies property,
1018643da460SMax Grossman 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1019643da460SMax Grossman 	 */
1020643da460SMax Grossman 
1021643da460SMax Grossman 	/*
1022643da460SMax Grossman 	 * Subtract out approximate space used by indirect blocks.
1023643da460SMax Grossman 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
1024643da460SMax Grossman 	 * Assume all blocks are recordsize.  Assume ditto blocks and
1025643da460SMax Grossman 	 * internal fragmentation counter out compression.
1026643da460SMax Grossman 	 *
1027643da460SMax Grossman 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1028643da460SMax Grossman 	 * block, which we observe in practice.
1029643da460SMax Grossman 	 */
1030643da460SMax Grossman 	uint64_t recordsize;
1031643da460SMax Grossman 	err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
1032643da460SMax Grossman 	if (err != 0)
1033643da460SMax Grossman 		return (err);
1034643da460SMax Grossman 	size -= size / recordsize * sizeof (blkptr_t);
1035643da460SMax Grossman 
1036643da460SMax Grossman 	/* Add in the space for the record associated with each block. */
1037643da460SMax Grossman 	size += size / recordsize * sizeof (dmu_replay_record_t);
1038643da460SMax Grossman 
1039643da460SMax Grossman 	*sizep = size;
1040643da460SMax Grossman 
1041643da460SMax Grossman 	return (0);
1042643da460SMax Grossman }
1043643da460SMax Grossman 
10443b2aab18SMatthew Ahrens int
dmu_send_estimate(dsl_dataset_t * ds,dsl_dataset_t * fromds,uint64_t * sizep)10453b2aab18SMatthew Ahrens dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
10463b2aab18SMatthew Ahrens {
104719b94df9SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
104819b94df9SMatthew Ahrens 	int err;
104919b94df9SMatthew Ahrens 	uint64_t size;
105019b94df9SMatthew Ahrens 
10513b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
10523b2aab18SMatthew Ahrens 
105319b94df9SMatthew Ahrens 	/* tosnap must be a snapshot */
1054bc9014e6SJustin Gibbs 	if (!ds->ds_is_snapshot)
1055be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
105619b94df9SMatthew Ahrens 
105724218bebSAndriy Gapon 	/* fromsnap, if provided, must be a snapshot */
105824218bebSAndriy Gapon 	if (fromds != NULL && !fromds->ds_is_snapshot)
105924218bebSAndriy Gapon 		return (SET_ERROR(EINVAL));
106024218bebSAndriy Gapon 
10614445fffbSMatthew Ahrens 	/*
10624445fffbSMatthew Ahrens 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
10634445fffbSMatthew Ahrens 	 * or the origin's fs.
10644445fffbSMatthew Ahrens 	 */
106578f17100SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1066be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
106719b94df9SMatthew Ahrens 
106819b94df9SMatthew Ahrens 	/* Get uncompressed size estimate of changed data. */
106919b94df9SMatthew Ahrens 	if (fromds == NULL) {
1070c1379625SJustin T. Gibbs 		size = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
107119b94df9SMatthew Ahrens 	} else {
107219b94df9SMatthew Ahrens 		uint64_t used, comp;
107319b94df9SMatthew Ahrens 		err = dsl_dataset_space_written(fromds, ds,
107419b94df9SMatthew Ahrens 		    &used, &comp, &size);
10753b2aab18SMatthew Ahrens 		if (err != 0)
107619b94df9SMatthew Ahrens 			return (err);
107719b94df9SMatthew Ahrens 	}
107819b94df9SMatthew Ahrens 
1079643da460SMax Grossman 	err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
108019b94df9SMatthew Ahrens 	return (err);
1081643da460SMax Grossman }
108219b94df9SMatthew Ahrens 
1083643da460SMax Grossman /*
1084643da460SMax Grossman  * Simple callback used to traverse the blocks of a snapshot and sum their
1085643da460SMax Grossman  * uncompressed size
1086643da460SMax Grossman  */
1087643da460SMax Grossman /* ARGSUSED */
1088643da460SMax Grossman static int
dmu_calculate_send_traversal(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const dnode_phys_t * dnp,void * arg)1089643da460SMax Grossman dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1090643da460SMax Grossman     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1091643da460SMax Grossman {
1092643da460SMax Grossman 	uint64_t *spaceptr = arg;
1093643da460SMax Grossman 	if (bp != NULL && !BP_IS_HOLE(bp)) {
1094643da460SMax Grossman 		*spaceptr += BP_GET_UCSIZE(bp);
1095643da460SMax Grossman 	}
109619b94df9SMatthew Ahrens 	return (0);
109719b94df9SMatthew Ahrens }
109819b94df9SMatthew Ahrens 
1099643da460SMax Grossman /*
1100643da460SMax Grossman  * Given a desination snapshot and a TXG, calculate the approximate size of a
1101643da460SMax Grossman  * send stream sent from that TXG. from_txg may be zero, indicating that the
1102643da460SMax Grossman  * whole snapshot will be sent.
1103643da460SMax Grossman  */
1104643da460SMax Grossman int
dmu_send_estimate_from_txg(dsl_dataset_t * ds,uint64_t from_txg,uint64_t * sizep)1105643da460SMax Grossman dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
1106643da460SMax Grossman     uint64_t *sizep)
1107643da460SMax Grossman {
1108643da460SMax Grossman 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
1109643da460SMax Grossman 	int err;
1110643da460SMax Grossman 	uint64_t size = 0;
1111643da460SMax Grossman 
1112643da460SMax Grossman 	ASSERT(dsl_pool_config_held(dp));
1113643da460SMax Grossman 
1114643da460SMax Grossman 	/* tosnap must be a snapshot */
1115643da460SMax Grossman 	if (!dsl_dataset_is_snapshot(ds))
1116643da460SMax Grossman 		return (SET_ERROR(EINVAL));
1117643da460SMax Grossman 
1118643da460SMax Grossman 	/* verify that from_txg is before the provided snapshot was taken */
1119643da460SMax Grossman 	if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1120643da460SMax Grossman 		return (SET_ERROR(EXDEV));
1121643da460SMax Grossman 	}
1122643da460SMax Grossman 
1123643da460SMax Grossman 	/*
1124643da460SMax Grossman 	 * traverse the blocks of the snapshot with birth times after
1125643da460SMax Grossman 	 * from_txg, summing their uncompressed size
1126643da460SMax Grossman 	 */
1127643da460SMax Grossman 	err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1128643da460SMax Grossman 	    dmu_calculate_send_traversal, &size);
1129643da460SMax Grossman 	if (err)
1130643da460SMax Grossman 		return (err);
1131643da460SMax Grossman 
1132643da460SMax Grossman 	err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1133643da460SMax Grossman 	return (err);
1134643da460SMax Grossman }
1135643da460SMax Grossman 
11363b2aab18SMatthew Ahrens typedef struct dmu_recv_begin_arg {
11373b2aab18SMatthew Ahrens 	const char *drba_origin;
11383b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drba_cookie;
11393b2aab18SMatthew Ahrens 	cred_t *drba_cred;
114034f2f8cfSMatthew Ahrens 	uint64_t drba_snapobj;
11413b2aab18SMatthew Ahrens } dmu_recv_begin_arg_t;
1142efb80947Sahrens 
1143f18faf3fSek110237 static int
recv_begin_check_existing_impl(dmu_recv_begin_arg_t * drba,dsl_dataset_t * ds,uint64_t fromguid)11443b2aab18SMatthew Ahrens recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
11453b2aab18SMatthew Ahrens     uint64_t fromguid)
1146f18faf3fSek110237 {
11473cb34c60Sahrens 	uint64_t val;
11483b2aab18SMatthew Ahrens 	int error;
11493b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
11503cb34c60Sahrens 
11513b2aab18SMatthew Ahrens 	/* temporary clone name must not exist */
11523b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
1153c1379625SJustin T. Gibbs 	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
11543b2aab18SMatthew Ahrens 	    8, 1, &val);
11553b2aab18SMatthew Ahrens 	if (error != ENOENT)
11563b2aab18SMatthew Ahrens 		return (error == 0 ? EBUSY : error);
1157feaa74e4SMark Maybee 
11583b2aab18SMatthew Ahrens 	/* new snapshot name must not exist */
11593b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
1160c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1161c1379625SJustin T. Gibbs 	    drba->drba_cookie->drc_tosnap, 8, 1, &val);
11623b2aab18SMatthew Ahrens 	if (error != ENOENT)
11633b2aab18SMatthew Ahrens 		return (error == 0 ? EEXIST : error);
11643b2aab18SMatthew Ahrens 
1165a2afb611SJerry Jelinek 	/*
1166a2afb611SJerry Jelinek 	 * Check snapshot limit before receiving. We'll recheck again at the
1167a2afb611SJerry Jelinek 	 * end, but might as well abort before receiving if we're already over
1168a2afb611SJerry Jelinek 	 * the limit.
1169a2afb611SJerry Jelinek 	 *
1170a2afb611SJerry Jelinek 	 * Note that we do not check the file system limit with
1171a2afb611SJerry Jelinek 	 * dsl_dir_fscount_check because the temporary %clones don't count
1172a2afb611SJerry Jelinek 	 * against that limit.
1173a2afb611SJerry Jelinek 	 */
1174a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1175a2afb611SJerry Jelinek 	    NULL, drba->drba_cred);
1176a2afb611SJerry Jelinek 	if (error != 0)
1177a2afb611SJerry Jelinek 		return (error);
1178a2afb611SJerry Jelinek 
11793b2aab18SMatthew Ahrens 	if (fromguid != 0) {
118092241e0bSTom Erickson 		dsl_dataset_t *snap;
1181c1379625SJustin T. Gibbs 		uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
118234f2f8cfSMatthew Ahrens 
118334f2f8cfSMatthew Ahrens 		/* Find snapshot in this dir that matches fromguid. */
118434f2f8cfSMatthew Ahrens 		while (obj != 0) {
11853b2aab18SMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, obj, FTAG,
11863b2aab18SMatthew Ahrens 			    &snap);
11873b2aab18SMatthew Ahrens 			if (error != 0)
1188be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
118934f2f8cfSMatthew Ahrens 			if (snap->ds_dir != ds->ds_dir) {
119092241e0bSTom Erickson 				dsl_dataset_rele(snap, FTAG);
1191be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
119292241e0bSTom Erickson 			}
1193c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(snap)->ds_guid == fromguid)
119434f2f8cfSMatthew Ahrens 				break;
1195c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
119692241e0bSTom Erickson 			dsl_dataset_rele(snap, FTAG);
119792241e0bSTom Erickson 		}
119892241e0bSTom Erickson 		if (obj == 0)
1199be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
120034f2f8cfSMatthew Ahrens 
120134f2f8cfSMatthew Ahrens 		if (drba->drba_cookie->drc_force) {
120234f2f8cfSMatthew Ahrens 			drba->drba_snapobj = obj;
120334f2f8cfSMatthew Ahrens 		} else {
120434f2f8cfSMatthew Ahrens 			/*
120534f2f8cfSMatthew Ahrens 			 * If we are not forcing, there must be no
120634f2f8cfSMatthew Ahrens 			 * changes since fromsnap.
120734f2f8cfSMatthew Ahrens 			 */
120834f2f8cfSMatthew Ahrens 			if (dsl_dataset_modified_since_snap(ds, snap)) {
120934f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
121034f2f8cfSMatthew Ahrens 				return (SET_ERROR(ETXTBSY));
121192241e0bSTom Erickson 			}
121234f2f8cfSMatthew Ahrens 			drba->drba_snapobj = ds->ds_prev->ds_object;
121334f2f8cfSMatthew Ahrens 		}
121434f2f8cfSMatthew Ahrens 
121534f2f8cfSMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
1216ae46e4c7SMatthew Ahrens 	} else {
12175bae108fSAndriy Gapon 		/* if full, then must be forced */
12185bae108fSAndriy Gapon 		if (!drba->drba_cookie->drc_force)
12195bae108fSAndriy Gapon 			return (SET_ERROR(EEXIST));
12205bae108fSAndriy Gapon 		/* start from $ORIGIN@$ORIGIN, if supported */
12215bae108fSAndriy Gapon 		drba->drba_snapobj = dp->dp_origin_snap != NULL ?
12225bae108fSAndriy Gapon 		    dp->dp_origin_snap->ds_object : 0;
1223ae46e4c7SMatthew Ahrens 	}
12243cb34c60Sahrens 
12253cb34c60Sahrens 	return (0);
12263b2aab18SMatthew Ahrens 
1227f18faf3fSek110237 }
1228f18faf3fSek110237 
12293b2aab18SMatthew Ahrens static int
dmu_recv_begin_check(void * arg,dmu_tx_t * tx)12303b2aab18SMatthew Ahrens dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
1231f18faf3fSek110237 {
12323b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
12333b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
12343b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
12353b2aab18SMatthew Ahrens 	uint64_t fromguid = drrb->drr_fromguid;
12363b2aab18SMatthew Ahrens 	int flags = drrb->drr_flags;
12373b2aab18SMatthew Ahrens 	int error;
12385d7b4d43SMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
12393b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
12403b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
1241f18faf3fSek110237 
12423b2aab18SMatthew Ahrens 	/* already checked */
12433b2aab18SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1244c1a50c7eSMatthew Ahrens 	ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
12453b2aab18SMatthew Ahrens 
12463b2aab18SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
12473b2aab18SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
12483b2aab18SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
12493b2aab18SMatthew Ahrens 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1250be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12513b2aab18SMatthew Ahrens 
12523b2aab18SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
12535d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
12545d7b4d43SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
1255be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12565d7b4d43SMatthew Ahrens 
1257c1a50c7eSMatthew Ahrens 	if (drba->drba_cookie->drc_resumable &&
1258c1a50c7eSMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
1259c1a50c7eSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1260c1a50c7eSMatthew Ahrens 
12615d7b4d43SMatthew Ahrens 	/*
12625d7b4d43SMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
12635d7b4d43SMatthew Ahrens 	 * record to a plan WRITE record, so the pool must have the
12645d7b4d43SMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
12655d7b4d43SMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
12665d7b4d43SMatthew Ahrens 	 */
12675d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
12685d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
12695d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12705d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
12715d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
12725d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
12733b2aab18SMatthew Ahrens 
1274b5152584SMatthew Ahrens 	/*
1275b5152584SMatthew Ahrens 	 * The receiving code doesn't know how to translate large blocks
1276b5152584SMatthew Ahrens 	 * to smaller ones, so the pool must have the LARGE_BLOCKS
1277b5152584SMatthew Ahrens 	 * feature enabled if the stream has LARGE_BLOCKS.
1278b5152584SMatthew Ahrens 	 */
1279b5152584SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1280b5152584SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1281b5152584SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1282b5152584SMatthew Ahrens 
12833b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
12843b2aab18SMatthew Ahrens 	if (error == 0) {
12853b2aab18SMatthew Ahrens 		/* target fs already exists; recv into temp clone */
12863b2aab18SMatthew Ahrens 
12873b2aab18SMatthew Ahrens 		/* Can't recv a clone into an existing fs */
12883b2aab18SMatthew Ahrens 		if (flags & DRR_FLAG_CLONE) {
12893b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
1290be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
12913b2aab18SMatthew Ahrens 		}
12923b2aab18SMatthew Ahrens 
12933b2aab18SMatthew Ahrens 		error = recv_begin_check_existing_impl(drba, ds, fromguid);
12943b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
12953b2aab18SMatthew Ahrens 	} else if (error == ENOENT) {
12963b2aab18SMatthew Ahrens 		/* target fs does not exist; must be a full backup or clone */
1297*a1988827SMatthew Ahrens 		char buf[ZFS_MAX_DATASET_NAME_LEN];
12983b2aab18SMatthew Ahrens 
12993b2aab18SMatthew Ahrens 		/*
13003b2aab18SMatthew Ahrens 		 * If it's a non-clone incremental, we are missing the
13013b2aab18SMatthew Ahrens 		 * target fs, so fail the recv.
13023b2aab18SMatthew Ahrens 		 */
1303a2cdcdd2SPaul Dagnelie 		if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1304a2cdcdd2SPaul Dagnelie 		    drba->drba_origin))
1305be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
13063b2aab18SMatthew Ahrens 
13073b2aab18SMatthew Ahrens 		/* Open the parent of tofs */
1308*a1988827SMatthew Ahrens 		ASSERT3U(strlen(tofs), <, sizeof (buf));
13093b2aab18SMatthew Ahrens 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
13103b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
13113b2aab18SMatthew Ahrens 		if (error != 0)
13123b2aab18SMatthew Ahrens 			return (error);
13133b2aab18SMatthew Ahrens 
1314a2afb611SJerry Jelinek 		/*
1315a2afb611SJerry Jelinek 		 * Check filesystem and snapshot limits before receiving. We'll
1316a2afb611SJerry Jelinek 		 * recheck snapshot limits again at the end (we create the
1317a2afb611SJerry Jelinek 		 * filesystems and increment those counts during begin_sync).
1318a2afb611SJerry Jelinek 		 */
1319a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1320a2afb611SJerry Jelinek 		    ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1321a2afb611SJerry Jelinek 		if (error != 0) {
1322a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1323a2afb611SJerry Jelinek 			return (error);
1324a2afb611SJerry Jelinek 		}
1325a2afb611SJerry Jelinek 
1326a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1327a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1328a2afb611SJerry Jelinek 		if (error != 0) {
1329a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1330a2afb611SJerry Jelinek 			return (error);
1331a2afb611SJerry Jelinek 		}
1332a2afb611SJerry Jelinek 
13333b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
13343b2aab18SMatthew Ahrens 			dsl_dataset_t *origin;
13353b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, drba->drba_origin,
13363b2aab18SMatthew Ahrens 			    FTAG, &origin);
13373b2aab18SMatthew Ahrens 			if (error != 0) {
13383b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
13393b2aab18SMatthew Ahrens 				return (error);
13403b2aab18SMatthew Ahrens 			}
1341bc9014e6SJustin Gibbs 			if (!origin->ds_is_snapshot) {
13423b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
13433b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1344be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
13453b2aab18SMatthew Ahrens 			}
1346c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(origin)->ds_guid != fromguid) {
13473b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
13483b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1349be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
13503b2aab18SMatthew Ahrens 			}
13513b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
13523b2aab18SMatthew Ahrens 		}
13533b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
13543b2aab18SMatthew Ahrens 		error = 0;
13553b2aab18SMatthew Ahrens 	}
13563b2aab18SMatthew Ahrens 	return (error);
13573b2aab18SMatthew Ahrens }
13583b2aab18SMatthew Ahrens 
13593b2aab18SMatthew Ahrens static void
dmu_recv_begin_sync(void * arg,dmu_tx_t * tx)13603b2aab18SMatthew Ahrens dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
13613b2aab18SMatthew Ahrens {
13623b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
13633b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
1364c1a50c7eSMatthew Ahrens 	objset_t *mos = dp->dp_meta_objset;
13653b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
13663b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
13673b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *newds;
13683b2aab18SMatthew Ahrens 	uint64_t dsobj;
13693b2aab18SMatthew Ahrens 	int error;
1370c1a50c7eSMatthew Ahrens 	uint64_t crflags = 0;
13713b2aab18SMatthew Ahrens 
1372c1a50c7eSMatthew Ahrens 	if (drrb->drr_flags & DRR_FLAG_CI_DATA)
1373c1a50c7eSMatthew Ahrens 		crflags |= DS_FLAG_CI_DATASET;
13743b2aab18SMatthew Ahrens 
13753b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
13763b2aab18SMatthew Ahrens 	if (error == 0) {
13773b2aab18SMatthew Ahrens 		/* create temporary clone */
137834f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap = NULL;
137934f2f8cfSMatthew Ahrens 		if (drba->drba_snapobj != 0) {
138034f2f8cfSMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
138134f2f8cfSMatthew Ahrens 			    drba->drba_snapobj, FTAG, &snap));
138234f2f8cfSMatthew Ahrens 		}
13833b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
138434f2f8cfSMatthew Ahrens 		    snap, crflags, drba->drba_cred, tx);
1385f40b29ceSPaul Dagnelie 		if (drba->drba_snapobj != 0)
138634f2f8cfSMatthew Ahrens 			dsl_dataset_rele(snap, FTAG);
13873b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
13883b2aab18SMatthew Ahrens 	} else {
13893b2aab18SMatthew Ahrens 		dsl_dir_t *dd;
13903b2aab18SMatthew Ahrens 		const char *tail;
13913b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
13923b2aab18SMatthew Ahrens 
13933b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
13943b2aab18SMatthew Ahrens 
13953b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
13963b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
13973b2aab18SMatthew Ahrens 			    FTAG, &origin));
13983b2aab18SMatthew Ahrens 		}
13993b2aab18SMatthew Ahrens 
14003b2aab18SMatthew Ahrens 		/* Create new dataset. */
14013b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(dd,
14023b2aab18SMatthew Ahrens 		    strrchr(tofs, '/') + 1,
14033b2aab18SMatthew Ahrens 		    origin, crflags, drba->drba_cred, tx);
14043b2aab18SMatthew Ahrens 		if (origin != NULL)
14053b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
14063b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
14073b2aab18SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
14083b2aab18SMatthew Ahrens 	}
14093b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
14103b2aab18SMatthew Ahrens 
1411c1a50c7eSMatthew Ahrens 	if (drba->drba_cookie->drc_resumable) {
1412c1a50c7eSMatthew Ahrens 		dsl_dataset_zapify(newds, tx);
1413c1a50c7eSMatthew Ahrens 		if (drrb->drr_fromguid != 0) {
1414c1a50c7eSMatthew Ahrens 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
1415c1a50c7eSMatthew Ahrens 			    8, 1, &drrb->drr_fromguid, tx));
1416c1a50c7eSMatthew Ahrens 		}
1417c1a50c7eSMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
1418c1a50c7eSMatthew Ahrens 		    8, 1, &drrb->drr_toguid, tx));
1419c1a50c7eSMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
1420c1a50c7eSMatthew Ahrens 		    1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
1421c1a50c7eSMatthew Ahrens 		uint64_t one = 1;
1422c1a50c7eSMatthew Ahrens 		uint64_t zero = 0;
1423c1a50c7eSMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
1424c1a50c7eSMatthew Ahrens 		    8, 1, &one, tx));
1425c1a50c7eSMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
1426c1a50c7eSMatthew Ahrens 		    8, 1, &zero, tx));
1427c1a50c7eSMatthew Ahrens 		VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
1428c1a50c7eSMatthew Ahrens 		    8, 1, &zero, tx));
1429c1a50c7eSMatthew Ahrens 		if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1430c1a50c7eSMatthew Ahrens 		    DMU_BACKUP_FEATURE_EMBED_DATA) {
1431c1a50c7eSMatthew Ahrens 			VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
1432c1a50c7eSMatthew Ahrens 			    8, 1, &one, tx));
1433c1a50c7eSMatthew Ahrens 		}
1434c1a50c7eSMatthew Ahrens 	}
1435c1a50c7eSMatthew Ahrens 
14363b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
1437c1379625SJustin T. Gibbs 	dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
1438f18faf3fSek110237 
1439ae46e4c7SMatthew Ahrens 	/*
1440ae46e4c7SMatthew Ahrens 	 * If we actually created a non-clone, we need to create the
1441ae46e4c7SMatthew Ahrens 	 * objset in our new dataset.
1442ae46e4c7SMatthew Ahrens 	 */
14433b2aab18SMatthew Ahrens 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1444ae46e4c7SMatthew Ahrens 		(void) dmu_objset_create_impl(dp->dp_spa,
14453b2aab18SMatthew Ahrens 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1446ae46e4c7SMatthew Ahrens 	}
1447ae46e4c7SMatthew Ahrens 
14483b2aab18SMatthew Ahrens 	drba->drba_cookie->drc_ds = newds;
14493cb34c60Sahrens 
14503b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(newds, "receive", tx, "");
1451dc7cd546SMark Shellenbaum }
1452dc7cd546SMark Shellenbaum 
1453c1a50c7eSMatthew Ahrens static int
dmu_recv_resume_begin_check(void * arg,dmu_tx_t * tx)1454c1a50c7eSMatthew Ahrens dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1455c1a50c7eSMatthew Ahrens {
1456c1a50c7eSMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
1457c1a50c7eSMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
1458c1a50c7eSMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1459c1a50c7eSMatthew Ahrens 	int error;
1460c1a50c7eSMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1461c1a50c7eSMatthew Ahrens 	dsl_dataset_t *ds;
1462c1a50c7eSMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
1463c1a50c7eSMatthew Ahrens 
1464c1a50c7eSMatthew Ahrens 	/* already checked */
1465c1a50c7eSMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1466c1a50c7eSMatthew Ahrens 	ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
1467c1a50c7eSMatthew Ahrens 
1468c1a50c7eSMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1469c1a50c7eSMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
1470c1a50c7eSMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES)
1471c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1472c1a50c7eSMatthew Ahrens 
1473c1a50c7eSMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
1474c1a50c7eSMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1475c1a50c7eSMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
1476c1a50c7eSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1477c1a50c7eSMatthew Ahrens 
1478c1a50c7eSMatthew Ahrens 	/*
1479c1a50c7eSMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1480c1a50c7eSMatthew Ahrens 	 * record to a plain WRITE record, so the pool must have the
1481c1a50c7eSMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1482c1a50c7eSMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
1483c1a50c7eSMatthew Ahrens 	 */
1484c1a50c7eSMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1485c1a50c7eSMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1486c1a50c7eSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1487c1a50c7eSMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1488c1a50c7eSMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1489c1a50c7eSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1490c1a50c7eSMatthew Ahrens 
1491*a1988827SMatthew Ahrens 	/* 6 extra bytes for /%recv */
1492*a1988827SMatthew Ahrens 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1493c1a50c7eSMatthew Ahrens 
1494c1a50c7eSMatthew Ahrens 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
1495c1a50c7eSMatthew Ahrens 	    tofs, recv_clone_name);
1496c1a50c7eSMatthew Ahrens 
1497c1a50c7eSMatthew Ahrens 	if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1498c1a50c7eSMatthew Ahrens 		/* %recv does not exist; continue in tofs */
1499c1a50c7eSMatthew Ahrens 		error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1500c1a50c7eSMatthew Ahrens 		if (error != 0)
1501c1a50c7eSMatthew Ahrens 			return (error);
1502c1a50c7eSMatthew Ahrens 	}
1503c1a50c7eSMatthew Ahrens 
1504c1a50c7eSMatthew Ahrens 	/* check that ds is marked inconsistent */
1505c1a50c7eSMatthew Ahrens 	if (!DS_IS_INCONSISTENT(ds)) {
1506c1a50c7eSMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1507c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1508c1a50c7eSMatthew Ahrens 	}
1509c1a50c7eSMatthew Ahrens 
1510c1a50c7eSMatthew Ahrens 	/* check that there is resuming data, and that the toguid matches */
1511c1a50c7eSMatthew Ahrens 	if (!dsl_dataset_is_zapified(ds)) {
1512c1a50c7eSMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1513c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1514c1a50c7eSMatthew Ahrens 	}
1515c1a50c7eSMatthew Ahrens 	uint64_t val;
1516c1a50c7eSMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1517c1a50c7eSMatthew Ahrens 	    DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1518c1a50c7eSMatthew Ahrens 	if (error != 0 || drrb->drr_toguid != val) {
1519c1a50c7eSMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1520c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1521c1a50c7eSMatthew Ahrens 	}
1522c1a50c7eSMatthew Ahrens 
1523c1a50c7eSMatthew Ahrens 	/*
1524c1a50c7eSMatthew Ahrens 	 * Check if the receive is still running.  If so, it will be owned.
1525c1a50c7eSMatthew Ahrens 	 * Note that nothing else can own the dataset (e.g. after the receive
1526c1a50c7eSMatthew Ahrens 	 * fails) because it will be marked inconsistent.
1527c1a50c7eSMatthew Ahrens 	 */
1528c1a50c7eSMatthew Ahrens 	if (dsl_dataset_has_owner(ds)) {
1529c1a50c7eSMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1530c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EBUSY));
1531c1a50c7eSMatthew Ahrens 	}
1532c1a50c7eSMatthew Ahrens 
1533c1a50c7eSMatthew Ahrens 	/* There should not be any snapshots of this fs yet. */
1534c1a50c7eSMatthew Ahrens 	if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1535c1a50c7eSMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1536c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1537c1a50c7eSMatthew Ahrens 	}
1538c1a50c7eSMatthew Ahrens 
1539c1a50c7eSMatthew Ahrens 	/*
1540c1a50c7eSMatthew Ahrens 	 * Note: resume point will be checked when we process the first WRITE
1541c1a50c7eSMatthew Ahrens 	 * record.
1542c1a50c7eSMatthew Ahrens 	 */
1543c1a50c7eSMatthew Ahrens 
1544c1a50c7eSMatthew Ahrens 	/* check that the origin matches */
1545c1a50c7eSMatthew Ahrens 	val = 0;
1546c1a50c7eSMatthew Ahrens 	(void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1547c1a50c7eSMatthew Ahrens 	    DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1548c1a50c7eSMatthew Ahrens 	if (drrb->drr_fromguid != val) {
1549c1a50c7eSMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
1550c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1551c1a50c7eSMatthew Ahrens 	}
1552c1a50c7eSMatthew Ahrens 
1553c1a50c7eSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1554c1a50c7eSMatthew Ahrens 	return (0);
1555c1a50c7eSMatthew Ahrens }
1556c1a50c7eSMatthew Ahrens 
1557c1a50c7eSMatthew Ahrens static void
dmu_recv_resume_begin_sync(void * arg,dmu_tx_t * tx)1558c1a50c7eSMatthew Ahrens dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1559c1a50c7eSMatthew Ahrens {
1560c1a50c7eSMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
1561c1a50c7eSMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
1562c1a50c7eSMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
1563c1a50c7eSMatthew Ahrens 	dsl_dataset_t *ds;
1564c1a50c7eSMatthew Ahrens 	uint64_t dsobj;
1565*a1988827SMatthew Ahrens 	/* 6 extra bytes for /%recv */
1566*a1988827SMatthew Ahrens 	char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1567c1a50c7eSMatthew Ahrens 
1568c1a50c7eSMatthew Ahrens 	(void) snprintf(recvname, sizeof (recvname), "%s/%s",
1569c1a50c7eSMatthew Ahrens 	    tofs, recv_clone_name);
1570c1a50c7eSMatthew Ahrens 
1571c1a50c7eSMatthew Ahrens 	if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1572c1a50c7eSMatthew Ahrens 		/* %recv does not exist; continue in tofs */
1573c1a50c7eSMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
1574c1a50c7eSMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
1575c1a50c7eSMatthew Ahrens 	}
1576c1a50c7eSMatthew Ahrens 
1577c1a50c7eSMatthew Ahrens 	/* clear the inconsistent flag so that we can own it */
1578c1a50c7eSMatthew Ahrens 	ASSERT(DS_IS_INCONSISTENT(ds));
1579c1a50c7eSMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1580c1a50c7eSMatthew Ahrens 	dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
1581c1a50c7eSMatthew Ahrens 	dsobj = ds->ds_object;
1582c1a50c7eSMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
1583c1a50c7eSMatthew Ahrens 
1584c1a50c7eSMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
1585c1a50c7eSMatthew Ahrens 
1586c1a50c7eSMatthew Ahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1587c1a50c7eSMatthew Ahrens 	dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1588c1a50c7eSMatthew Ahrens 
1589c1a50c7eSMatthew Ahrens 	ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
1590c1a50c7eSMatthew Ahrens 
1591c1a50c7eSMatthew Ahrens 	drba->drba_cookie->drc_ds = ds;
1592c1a50c7eSMatthew Ahrens 
1593c1a50c7eSMatthew Ahrens 	spa_history_log_internal_ds(ds, "resume receive", tx, "");
1594c1a50c7eSMatthew Ahrens }
1595c1a50c7eSMatthew Ahrens 
15963cb34c60Sahrens /*
15973cb34c60Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
15983cb34c60Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
15993cb34c60Sahrens  */
16003cb34c60Sahrens int
dmu_recv_begin(char * tofs,char * tosnap,dmu_replay_record_t * drr_begin,boolean_t force,boolean_t resumable,char * origin,dmu_recv_cookie_t * drc)1601c1a50c7eSMatthew Ahrens dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
1602c1a50c7eSMatthew Ahrens     boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1603efb80947Sahrens {
16043b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t drba = { 0 };
1605ab04eb8eStimh 
16063cb34c60Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
1607c1a50c7eSMatthew Ahrens 	drc->drc_drr_begin = drr_begin;
1608c1a50c7eSMatthew Ahrens 	drc->drc_drrb = &drr_begin->drr_u.drr_begin;
16093cb34c60Sahrens 	drc->drc_tosnap = tosnap;
16103b2aab18SMatthew Ahrens 	drc->drc_tofs = tofs;
16113cb34c60Sahrens 	drc->drc_force = force;
1612c1a50c7eSMatthew Ahrens 	drc->drc_resumable = resumable;
1613a2afb611SJerry Jelinek 	drc->drc_cred = CRED();
1614efb80947Sahrens 
1615c1a50c7eSMatthew Ahrens 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
16163b2aab18SMatthew Ahrens 		drc->drc_byteswap = B_TRUE;
1617c1a50c7eSMatthew Ahrens 		fletcher_4_incremental_byteswap(drr_begin,
1618c1a50c7eSMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
1619c1a50c7eSMatthew Ahrens 		byteswap_record(drr_begin);
1620c1a50c7eSMatthew Ahrens 	} else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1621c1a50c7eSMatthew Ahrens 		fletcher_4_incremental_native(drr_begin,
16223b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
16233b2aab18SMatthew Ahrens 	} else {
1624c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1625ae46e4c7SMatthew Ahrens 	}
16263cb34c60Sahrens 
16273b2aab18SMatthew Ahrens 	drba.drba_origin = origin;
16283b2aab18SMatthew Ahrens 	drba.drba_cookie = drc;
16293b2aab18SMatthew Ahrens 	drba.drba_cred = CRED();
1630f4b94bdeSMatthew Ahrens 
1631c1a50c7eSMatthew Ahrens 	if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1632c1a50c7eSMatthew Ahrens 	    DMU_BACKUP_FEATURE_RESUMING) {
1633c1a50c7eSMatthew Ahrens 		return (dsl_sync_task(tofs,
1634c1a50c7eSMatthew Ahrens 		    dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
16357d46dc6cSMatthew Ahrens 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1636c1a50c7eSMatthew Ahrens 	} else  {
1637c1a50c7eSMatthew Ahrens 		return (dsl_sync_task(tofs,
1638c1a50c7eSMatthew Ahrens 		    dmu_recv_begin_check, dmu_recv_begin_sync,
1639c1a50c7eSMatthew Ahrens 		    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1640c1a50c7eSMatthew Ahrens 	}
16413cb34c60Sahrens }
16423cb34c60Sahrens 
1643a2cdcdd2SPaul Dagnelie struct receive_record_arg {
1644a2cdcdd2SPaul Dagnelie 	dmu_replay_record_t header;
1645a2cdcdd2SPaul Dagnelie 	void *payload; /* Pointer to a buffer containing the payload */
1646a2cdcdd2SPaul Dagnelie 	/*
1647a2cdcdd2SPaul Dagnelie 	 * If the record is a write, pointer to the arc_buf_t containing the
1648a2cdcdd2SPaul Dagnelie 	 * payload.
1649a2cdcdd2SPaul Dagnelie 	 */
1650a2cdcdd2SPaul Dagnelie 	arc_buf_t *write_buf;
1651a2cdcdd2SPaul Dagnelie 	int payload_size;
1652c1a50c7eSMatthew Ahrens 	uint64_t bytes_read; /* bytes read from stream when record created */
1653a2cdcdd2SPaul Dagnelie 	boolean_t eos_marker; /* Marks the end of the stream */
1654a2cdcdd2SPaul Dagnelie 	bqueue_node_t node;
1655a2cdcdd2SPaul Dagnelie };
165698110f08SMatthew Ahrens 
1657a2cdcdd2SPaul Dagnelie struct receive_writer_arg {
1658a2cdcdd2SPaul Dagnelie 	objset_t *os;
1659a2cdcdd2SPaul Dagnelie 	boolean_t byteswap;
1660a2cdcdd2SPaul Dagnelie 	bqueue_t q;
1661c1a50c7eSMatthew Ahrens 
1662a2cdcdd2SPaul Dagnelie 	/*
1663a2cdcdd2SPaul Dagnelie 	 * These three args are used to signal to the main thread that we're
1664a2cdcdd2SPaul Dagnelie 	 * done.
1665a2cdcdd2SPaul Dagnelie 	 */
1666a2cdcdd2SPaul Dagnelie 	kmutex_t mutex;
1667a2cdcdd2SPaul Dagnelie 	kcondvar_t cv;
1668a2cdcdd2SPaul Dagnelie 	boolean_t done;
1669c1a50c7eSMatthew Ahrens 
1670a2cdcdd2SPaul Dagnelie 	int err;
1671a2cdcdd2SPaul Dagnelie 	/* A map from guid to dataset to help handle dedup'd streams. */
1672a2cdcdd2SPaul Dagnelie 	avl_tree_t *guid_to_ds_map;
1673c1a50c7eSMatthew Ahrens 	boolean_t resumable;
1674c1a50c7eSMatthew Ahrens 	uint64_t last_object, last_offset;
1675c1a50c7eSMatthew Ahrens 	uint64_t bytes_read; /* bytes read when current record created */
1676a2cdcdd2SPaul Dagnelie };
1677a2cdcdd2SPaul Dagnelie 
1678a2cdcdd2SPaul Dagnelie struct receive_arg  {
1679a2cdcdd2SPaul Dagnelie 	objset_t *os;
1680a2cdcdd2SPaul Dagnelie 	vnode_t *vp; /* The vnode to read the stream from */
1681a2cdcdd2SPaul Dagnelie 	uint64_t voff; /* The current offset in the stream */
1682c1a50c7eSMatthew Ahrens 	uint64_t bytes_read;
1683a2cdcdd2SPaul Dagnelie 	/*
1684a2cdcdd2SPaul Dagnelie 	 * A record that has had its payload read in, but hasn't yet been handed
1685a2cdcdd2SPaul Dagnelie 	 * off to the worker thread.
1686a2cdcdd2SPaul Dagnelie 	 */
1687a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *rrd;
1688a2cdcdd2SPaul Dagnelie 	/* A record that has had its header read in, but not its payload. */
1689a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *next_rrd;
16903cb34c60Sahrens 	zio_cksum_t cksum;
169198110f08SMatthew Ahrens 	zio_cksum_t prev_cksum;
1692a2cdcdd2SPaul Dagnelie 	int err;
1693a2cdcdd2SPaul Dagnelie 	boolean_t byteswap;
1694a2cdcdd2SPaul Dagnelie 	/* Sorted list of objects not to issue prefetches for. */
1695a2cdcdd2SPaul Dagnelie 	list_t ignore_obj_list;
1696a2cdcdd2SPaul Dagnelie };
169798110f08SMatthew Ahrens 
1698a2cdcdd2SPaul Dagnelie struct receive_ign_obj_node {
1699a2cdcdd2SPaul Dagnelie 	list_node_t node;
1700a2cdcdd2SPaul Dagnelie 	uint64_t object;
17013cb34c60Sahrens };
1702efb80947Sahrens 
17039e69d7d0SLori Alt typedef struct guid_map_entry {
17049e69d7d0SLori Alt 	uint64_t	guid;
17059e69d7d0SLori Alt 	dsl_dataset_t	*gme_ds;
17069e69d7d0SLori Alt 	avl_node_t	avlnode;
17079e69d7d0SLori Alt } guid_map_entry_t;
17089e69d7d0SLori Alt 
17099e69d7d0SLori Alt static int
guid_compare(const void * arg1,const void * arg2)17109e69d7d0SLori Alt guid_compare(const void *arg1, const void *arg2)
17119e69d7d0SLori Alt {
17129e69d7d0SLori Alt 	const guid_map_entry_t *gmep1 = arg1;
17139e69d7d0SLori Alt 	const guid_map_entry_t *gmep2 = arg2;
17149e69d7d0SLori Alt 
17159e69d7d0SLori Alt 	if (gmep1->guid < gmep2->guid)
17169e69d7d0SLori Alt 		return (-1);
17179e69d7d0SLori Alt 	else if (gmep1->guid > gmep2->guid)
17189e69d7d0SLori Alt 		return (1);
17199e69d7d0SLori Alt 	return (0);
17209e69d7d0SLori Alt }
17219e69d7d0SLori Alt 
1722c99e4bdcSChris Kirby static void
free_guid_map_onexit(void * arg)1723c99e4bdcSChris Kirby free_guid_map_onexit(void *arg)
1724c99e4bdcSChris Kirby {
1725c99e4bdcSChris Kirby 	avl_tree_t *ca = arg;
1726c99e4bdcSChris Kirby 	void *cookie = NULL;
1727c99e4bdcSChris Kirby 	guid_map_entry_t *gmep;
1728c99e4bdcSChris Kirby 
1729c99e4bdcSChris Kirby 	while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
17303b2aab18SMatthew Ahrens 		dsl_dataset_long_rele(gmep->gme_ds, gmep);
1731de8d9cffSMatthew Ahrens 		dsl_dataset_rele(gmep->gme_ds, gmep);
1732c99e4bdcSChris Kirby 		kmem_free(gmep, sizeof (guid_map_entry_t));
1733c99e4bdcSChris Kirby 	}
1734c99e4bdcSChris Kirby 	avl_destroy(ca);
1735c99e4bdcSChris Kirby 	kmem_free(ca, sizeof (avl_tree_t));
1736c99e4bdcSChris Kirby }
1737c99e4bdcSChris Kirby 
173898110f08SMatthew Ahrens static int
receive_read(struct receive_arg * ra,int len,void * buf)1739a2cdcdd2SPaul Dagnelie receive_read(struct receive_arg *ra, int len, void *buf)
1740efb80947Sahrens {
17413cb34c60Sahrens 	int done = 0;
1742efb80947Sahrens 
1743efb80947Sahrens 	/* some things will require 8-byte alignment, so everything must */
1744fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
1745efb80947Sahrens 
17463cb34c60Sahrens 	while (done < len) {
1747efb80947Sahrens 		ssize_t resid;
1748efb80947Sahrens 
1749efb80947Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
175098110f08SMatthew Ahrens 		    (char *)buf + done, len - done,
1751efb80947Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
1752efb80947Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
1753efb80947Sahrens 
1754c1a50c7eSMatthew Ahrens 		if (resid == len - done) {
1755c1a50c7eSMatthew Ahrens 			/*
1756c1a50c7eSMatthew Ahrens 			 * Note: ECKSUM indicates that the receive
1757c1a50c7eSMatthew Ahrens 			 * was interrupted and can potentially be resumed.
1758c1a50c7eSMatthew Ahrens 			 */
1759c1a50c7eSMatthew Ahrens 			ra->err = SET_ERROR(ECKSUM);
1760c1a50c7eSMatthew Ahrens 		}
17613cb34c60Sahrens 		ra->voff += len - done - resid;
17623cb34c60Sahrens 		done = len - resid;
17633b2aab18SMatthew Ahrens 		if (ra->err != 0)
176498110f08SMatthew Ahrens 			return (ra->err);
1765efb80947Sahrens 	}
1766efb80947Sahrens 
1767c1a50c7eSMatthew Ahrens 	ra->bytes_read += len;
1768c1a50c7eSMatthew Ahrens 
17693cb34c60Sahrens 	ASSERT3U(done, ==, len);
177098110f08SMatthew Ahrens 	return (0);
1771efb80947Sahrens }
1772efb80947Sahrens 
1773efb80947Sahrens static void
byteswap_record(dmu_replay_record_t * drr)177498110f08SMatthew Ahrens byteswap_record(dmu_replay_record_t *drr)
1775efb80947Sahrens {
1776efb80947Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1777efb80947Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1778efb80947Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
17793cb34c60Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
178098110f08SMatthew Ahrens 
1781efb80947Sahrens 	switch (drr->drr_type) {
1782efb80947Sahrens 	case DRR_BEGIN:
1783efb80947Sahrens 		DO64(drr_begin.drr_magic);
17849e69d7d0SLori Alt 		DO64(drr_begin.drr_versioninfo);
1785efb80947Sahrens 		DO64(drr_begin.drr_creation_time);
1786efb80947Sahrens 		DO32(drr_begin.drr_type);
17873cb34c60Sahrens 		DO32(drr_begin.drr_flags);
1788efb80947Sahrens 		DO64(drr_begin.drr_toguid);
1789efb80947Sahrens 		DO64(drr_begin.drr_fromguid);
1790efb80947Sahrens 		break;
1791efb80947Sahrens 	case DRR_OBJECT:
1792efb80947Sahrens 		DO64(drr_object.drr_object);
1793efb80947Sahrens 		DO32(drr_object.drr_type);
1794efb80947Sahrens 		DO32(drr_object.drr_bonustype);
1795efb80947Sahrens 		DO32(drr_object.drr_blksz);
1796efb80947Sahrens 		DO32(drr_object.drr_bonuslen);
17979e69d7d0SLori Alt 		DO64(drr_object.drr_toguid);
1798efb80947Sahrens 		break;
1799efb80947Sahrens 	case DRR_FREEOBJECTS:
1800efb80947Sahrens 		DO64(drr_freeobjects.drr_firstobj);
1801efb80947Sahrens 		DO64(drr_freeobjects.drr_numobjs);
18029e69d7d0SLori Alt 		DO64(drr_freeobjects.drr_toguid);
1803efb80947Sahrens 		break;
1804efb80947Sahrens 	case DRR_WRITE:
1805efb80947Sahrens 		DO64(drr_write.drr_object);
1806efb80947Sahrens 		DO32(drr_write.drr_type);
1807efb80947Sahrens 		DO64(drr_write.drr_offset);
1808efb80947Sahrens 		DO64(drr_write.drr_length);
18099e69d7d0SLori Alt 		DO64(drr_write.drr_toguid);
181098110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
18118e714474SLori Alt 		DO64(drr_write.drr_key.ddk_prop);
18129e69d7d0SLori Alt 		break;
18139e69d7d0SLori Alt 	case DRR_WRITE_BYREF:
18149e69d7d0SLori Alt 		DO64(drr_write_byref.drr_object);
18159e69d7d0SLori Alt 		DO64(drr_write_byref.drr_offset);
18169e69d7d0SLori Alt 		DO64(drr_write_byref.drr_length);
18179e69d7d0SLori Alt 		DO64(drr_write_byref.drr_toguid);
18189e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refguid);
18199e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refobject);
18209e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refoffset);
182198110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
182298110f08SMatthew Ahrens 		    drr_key.ddk_cksum);
18238e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_prop);
1824efb80947Sahrens 		break;
18255d7b4d43SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
18265d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_object);
18275d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_offset);
18285d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_length);
18295d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_toguid);
18305d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_lsize);
18315d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_psize);
18325d7b4d43SMatthew Ahrens 		break;
1833efb80947Sahrens 	case DRR_FREE:
1834efb80947Sahrens 		DO64(drr_free.drr_object);
1835efb80947Sahrens 		DO64(drr_free.drr_offset);
1836efb80947Sahrens 		DO64(drr_free.drr_length);
18379e69d7d0SLori Alt 		DO64(drr_free.drr_toguid);
1838efb80947Sahrens 		break;
18390a586ceaSMark Shellenbaum 	case DRR_SPILL:
18400a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_object);
18410a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_length);
18420a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_toguid);
18430a586ceaSMark Shellenbaum 		break;
1844efb80947Sahrens 	case DRR_END:
18459e69d7d0SLori Alt 		DO64(drr_end.drr_toguid);
184698110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1847efb80947Sahrens 		break;
1848efb80947Sahrens 	}
184998110f08SMatthew Ahrens 
185098110f08SMatthew Ahrens 	if (drr->drr_type != DRR_BEGIN) {
185198110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
185298110f08SMatthew Ahrens 	}
185398110f08SMatthew Ahrens 
1854efb80947Sahrens #undef DO64
1855efb80947Sahrens #undef DO32
1856efb80947Sahrens }
1857efb80947Sahrens 
1858e77d42eaSMatthew Ahrens static inline uint8_t
deduce_nblkptr(dmu_object_type_t bonus_type,uint64_t bonus_size)1859e77d42eaSMatthew Ahrens deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1860e77d42eaSMatthew Ahrens {
1861e77d42eaSMatthew Ahrens 	if (bonus_type == DMU_OT_SA) {
1862e77d42eaSMatthew Ahrens 		return (1);
1863e77d42eaSMatthew Ahrens 	} else {
1864e77d42eaSMatthew Ahrens 		return (1 +
1865e77d42eaSMatthew Ahrens 		    ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
1866e77d42eaSMatthew Ahrens 	}
1867e77d42eaSMatthew Ahrens }
1868e77d42eaSMatthew Ahrens 
1869c1a50c7eSMatthew Ahrens static void
save_resume_state(struct receive_writer_arg * rwa,uint64_t object,uint64_t offset,dmu_tx_t * tx)1870c1a50c7eSMatthew Ahrens save_resume_state(struct receive_writer_arg *rwa,
1871c1a50c7eSMatthew Ahrens     uint64_t object, uint64_t offset, dmu_tx_t *tx)
1872c1a50c7eSMatthew Ahrens {
1873c1a50c7eSMatthew Ahrens 	int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1874c1a50c7eSMatthew Ahrens 
1875c1a50c7eSMatthew Ahrens 	if (!rwa->resumable)
1876c1a50c7eSMatthew Ahrens 		return;
1877c1a50c7eSMatthew Ahrens 
1878c1a50c7eSMatthew Ahrens 	/*
1879c1a50c7eSMatthew Ahrens 	 * We use ds_resume_bytes[] != 0 to indicate that we need to
1880c1a50c7eSMatthew Ahrens 	 * update this on disk, so it must not be 0.
1881c1a50c7eSMatthew Ahrens 	 */
1882c1a50c7eSMatthew Ahrens 	ASSERT(rwa->bytes_read != 0);
1883c1a50c7eSMatthew Ahrens 
1884c1a50c7eSMatthew Ahrens 	/*
1885c1a50c7eSMatthew Ahrens 	 * We only resume from write records, which have a valid
1886c1a50c7eSMatthew Ahrens 	 * (non-meta-dnode) object number.
1887c1a50c7eSMatthew Ahrens 	 */
1888c1a50c7eSMatthew Ahrens 	ASSERT(object != 0);
1889c1a50c7eSMatthew Ahrens 
1890c1a50c7eSMatthew Ahrens 	/*
1891c1a50c7eSMatthew Ahrens 	 * For resuming to work correctly, we must receive records in order,
1892c1a50c7eSMatthew Ahrens 	 * sorted by object,offset.  This is checked by the callers, but
1893c1a50c7eSMatthew Ahrens 	 * assert it here for good measure.
1894c1a50c7eSMatthew Ahrens 	 */
1895c1a50c7eSMatthew Ahrens 	ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
1896c1a50c7eSMatthew Ahrens 	ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
1897c1a50c7eSMatthew Ahrens 	    offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
1898c1a50c7eSMatthew Ahrens 	ASSERT3U(rwa->bytes_read, >=,
1899c1a50c7eSMatthew Ahrens 	    rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
1900c1a50c7eSMatthew Ahrens 
1901c1a50c7eSMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
1902c1a50c7eSMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
1903c1a50c7eSMatthew Ahrens 	rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
1904c1a50c7eSMatthew Ahrens }
1905c1a50c7eSMatthew Ahrens 
1906efb80947Sahrens static int
receive_object(struct receive_writer_arg * rwa,struct drr_object * drro,void * data)1907a2cdcdd2SPaul Dagnelie receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1908a2cdcdd2SPaul Dagnelie     void *data)
1909efb80947Sahrens {
1910e77d42eaSMatthew Ahrens 	dmu_object_info_t doi;
1911efb80947Sahrens 	dmu_tx_t *tx;
1912e77d42eaSMatthew Ahrens 	uint64_t object;
1913e77d42eaSMatthew Ahrens 	int err;
1914efb80947Sahrens 
1915efb80947Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
1916ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_type) ||
1917ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
19189e69d7d0SLori Alt 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1919efb80947Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1920efb80947Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1921efb80947Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
1922a2cdcdd2SPaul Dagnelie 	    drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
1923efb80947Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1924be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1925efb80947Sahrens 	}
1926efb80947Sahrens 
1927a2cdcdd2SPaul Dagnelie 	err = dmu_object_info(rwa->os, drro->drr_object, &doi);
19282bf405a2SMark Maybee 
19292bf405a2SMark Maybee 	if (err != 0 && err != ENOENT)
1930be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1931e77d42eaSMatthew Ahrens 	object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
19322bf405a2SMark Maybee 
1933e77d42eaSMatthew Ahrens 	/*
1934e77d42eaSMatthew Ahrens 	 * If we are losing blkptrs or changing the block size this must
1935e77d42eaSMatthew Ahrens 	 * be a new file instance.  We must clear out the previous file
1936e77d42eaSMatthew Ahrens 	 * contents before we can change this type of metadata in the dnode.
1937e77d42eaSMatthew Ahrens 	 */
1938e77d42eaSMatthew Ahrens 	if (err == 0) {
1939e77d42eaSMatthew Ahrens 		int nblkptr;
1940e77d42eaSMatthew Ahrens 
1941e77d42eaSMatthew Ahrens 		nblkptr = deduce_nblkptr(drro->drr_bonustype,
1942e77d42eaSMatthew Ahrens 		    drro->drr_bonuslen);
1943e77d42eaSMatthew Ahrens 
1944e77d42eaSMatthew Ahrens 		if (drro->drr_blksz != doi.doi_data_block_size ||
1945e77d42eaSMatthew Ahrens 		    nblkptr < doi.doi_nblkptr) {
1946a2cdcdd2SPaul Dagnelie 			err = dmu_free_long_range(rwa->os, drro->drr_object,
1947e77d42eaSMatthew Ahrens 			    0, DMU_OBJECT_END);
1948e77d42eaSMatthew Ahrens 			if (err != 0)
1949be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
19500a586ceaSMark Shellenbaum 		}
1951e77d42eaSMatthew Ahrens 	}
19522bf405a2SMark Maybee 
1953a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
1954e77d42eaSMatthew Ahrens 	dmu_tx_hold_bonus(tx, object);
1955efb80947Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
19563b2aab18SMatthew Ahrens 	if (err != 0) {
1957efb80947Sahrens 		dmu_tx_abort(tx);
1958efb80947Sahrens 		return (err);
1959efb80947Sahrens 	}
1960efb80947Sahrens 
1961e77d42eaSMatthew Ahrens 	if (object == DMU_NEW_OBJECT) {
1962e77d42eaSMatthew Ahrens 		/* currently free, want to be allocated */
1963a2cdcdd2SPaul Dagnelie 		err = dmu_object_claim(rwa->os, drro->drr_object,
1964e77d42eaSMatthew Ahrens 		    drro->drr_type, drro->drr_blksz,
1965e77d42eaSMatthew Ahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1966e77d42eaSMatthew Ahrens 	} else if (drro->drr_type != doi.doi_type ||
1967e77d42eaSMatthew Ahrens 	    drro->drr_blksz != doi.doi_data_block_size ||
1968e77d42eaSMatthew Ahrens 	    drro->drr_bonustype != doi.doi_bonus_type ||
1969e77d42eaSMatthew Ahrens 	    drro->drr_bonuslen != doi.doi_bonus_size) {
1970e77d42eaSMatthew Ahrens 		/* currently allocated, but with different properties */
1971a2cdcdd2SPaul Dagnelie 		err = dmu_object_reclaim(rwa->os, drro->drr_object,
1972e77d42eaSMatthew Ahrens 		    drro->drr_type, drro->drr_blksz,
1973e77d42eaSMatthew Ahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1974e77d42eaSMatthew Ahrens 	}
1975e77d42eaSMatthew Ahrens 	if (err != 0) {
1976e77d42eaSMatthew Ahrens 		dmu_tx_commit(tx);
1977e77d42eaSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1978e77d42eaSMatthew Ahrens 	}
1979e77d42eaSMatthew Ahrens 
1980a2cdcdd2SPaul Dagnelie 	dmu_object_set_checksum(rwa->os, drro->drr_object,
198198110f08SMatthew Ahrens 	    drro->drr_checksumtype, tx);
1982a2cdcdd2SPaul Dagnelie 	dmu_object_set_compress(rwa->os, drro->drr_object,
198398110f08SMatthew Ahrens 	    drro->drr_compress, tx);
1984efb80947Sahrens 
1985adee0b6fSTim Haley 	if (data != NULL) {
1986efb80947Sahrens 		dmu_buf_t *db;
1987adee0b6fSTim Haley 
1988a2cdcdd2SPaul Dagnelie 		VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
1989efb80947Sahrens 		dmu_buf_will_dirty(db, tx);
1990efb80947Sahrens 
19911934e92fSmaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
19921934e92fSmaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
1993a2cdcdd2SPaul Dagnelie 		if (rwa->byteswap) {
1994ad135b5dSChristopher Siden 			dmu_object_byteswap_t byteswap =
1995ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
1996ad135b5dSChristopher Siden 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
1997efb80947Sahrens 			    drro->drr_bonuslen);
1998efb80947Sahrens 		}
1999efb80947Sahrens 		dmu_buf_rele(db, FTAG);
2000efb80947Sahrens 	}
2001efb80947Sahrens 	dmu_tx_commit(tx);
2002c1a50c7eSMatthew Ahrens 
2003efb80947Sahrens 	return (0);
2004efb80947Sahrens }
2005efb80947Sahrens 
2006efb80947Sahrens /* ARGSUSED */
2007efb80947Sahrens static int
receive_freeobjects(struct receive_writer_arg * rwa,struct drr_freeobjects * drrfo)2008a2cdcdd2SPaul Dagnelie receive_freeobjects(struct receive_writer_arg *rwa,
2009efb80947Sahrens     struct drr_freeobjects *drrfo)
2010efb80947Sahrens {
2011efb80947Sahrens 	uint64_t obj;
2012efb80947Sahrens 
2013efb80947Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2014be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2015efb80947Sahrens 
2016efb80947Sahrens 	for (obj = drrfo->drr_firstobj;
2017432f72fdSahrens 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
2018a2cdcdd2SPaul Dagnelie 	    (void) dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2019efb80947Sahrens 		int err;
2020efb80947Sahrens 
2021a2cdcdd2SPaul Dagnelie 		if (dmu_object_info(rwa->os, obj, NULL) != 0)
2022efb80947Sahrens 			continue;
2023efb80947Sahrens 
2024a2cdcdd2SPaul Dagnelie 		err = dmu_free_long_object(rwa->os, obj);
20253b2aab18SMatthew Ahrens 		if (err != 0)
2026efb80947Sahrens 			return (err);
2027efb80947Sahrens 	}
2028c1a50c7eSMatthew Ahrens 
2029efb80947Sahrens 	return (0);
2030efb80947Sahrens }
2031efb80947Sahrens 
2032efb80947Sahrens static int
receive_write(struct receive_writer_arg * rwa,struct drr_write * drrw,arc_buf_t * abuf)2033a2cdcdd2SPaul Dagnelie receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2034a2cdcdd2SPaul Dagnelie     arc_buf_t *abuf)
2035efb80947Sahrens {
2036efb80947Sahrens 	dmu_tx_t *tx;
2037efb80947Sahrens 	int err;
2038efb80947Sahrens 
2039efb80947Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
2040ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drrw->drr_type))
2041be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2042efb80947Sahrens 
2043c1a50c7eSMatthew Ahrens 	/*
2044c1a50c7eSMatthew Ahrens 	 * For resuming to work, records must be in increasing order
2045c1a50c7eSMatthew Ahrens 	 * by (object, offset).
2046c1a50c7eSMatthew Ahrens 	 */
2047c1a50c7eSMatthew Ahrens 	if (drrw->drr_object < rwa->last_object ||
2048c1a50c7eSMatthew Ahrens 	    (drrw->drr_object == rwa->last_object &&
2049c1a50c7eSMatthew Ahrens 	    drrw->drr_offset < rwa->last_offset)) {
2050c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2051c1a50c7eSMatthew Ahrens 	}
2052c1a50c7eSMatthew Ahrens 	rwa->last_object = drrw->drr_object;
2053c1a50c7eSMatthew Ahrens 	rwa->last_offset = drrw->drr_offset;
2054c1a50c7eSMatthew Ahrens 
2055a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
2056be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2057efb80947Sahrens 
2058a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
2059efb80947Sahrens 
2060efb80947Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
2061efb80947Sahrens 	    drrw->drr_offset, drrw->drr_length);
2062efb80947Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
20633b2aab18SMatthew Ahrens 	if (err != 0) {
2064efb80947Sahrens 		dmu_tx_abort(tx);
2065efb80947Sahrens 		return (err);
2066efb80947Sahrens 	}
2067a2cdcdd2SPaul Dagnelie 	if (rwa->byteswap) {
2068ad135b5dSChristopher Siden 		dmu_object_byteswap_t byteswap =
2069ad135b5dSChristopher Siden 		    DMU_OT_BYTESWAP(drrw->drr_type);
207098110f08SMatthew Ahrens 		dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
207198110f08SMatthew Ahrens 		    drrw->drr_length);
2072ad135b5dSChristopher Siden 	}
207398110f08SMatthew Ahrens 
207498110f08SMatthew Ahrens 	dmu_buf_t *bonus;
2075a2cdcdd2SPaul Dagnelie 	if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
207698110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
20778a904709SMatthew Ahrens 	dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
2078c1a50c7eSMatthew Ahrens 
2079c1a50c7eSMatthew Ahrens 	/*
2080c1a50c7eSMatthew Ahrens 	 * Note: If the receive fails, we want the resume stream to start
2081c1a50c7eSMatthew Ahrens 	 * with the same record that we last successfully received (as opposed
2082c1a50c7eSMatthew Ahrens 	 * to the next record), so that we can verify that we are
2083c1a50c7eSMatthew Ahrens 	 * resuming from the correct location.
2084c1a50c7eSMatthew Ahrens 	 */
2085c1a50c7eSMatthew Ahrens 	save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2086efb80947Sahrens 	dmu_tx_commit(tx);
20878a904709SMatthew Ahrens 	dmu_buf_rele(bonus, FTAG);
2088c1a50c7eSMatthew Ahrens 
2089efb80947Sahrens 	return (0);
2090efb80947Sahrens }
2091efb80947Sahrens 
20929e69d7d0SLori Alt /*
20939e69d7d0SLori Alt  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
20949e69d7d0SLori Alt  * streams to refer to a copy of the data that is already on the
20959e69d7d0SLori Alt  * system because it came in earlier in the stream.  This function
20969e69d7d0SLori Alt  * finds the earlier copy of the data, and uses that copy instead of
20979e69d7d0SLori Alt  * data from the stream to fulfill this write.
20989e69d7d0SLori Alt  */
20999e69d7d0SLori Alt static int
receive_write_byref(struct receive_writer_arg * rwa,struct drr_write_byref * drrwbr)2100a2cdcdd2SPaul Dagnelie receive_write_byref(struct receive_writer_arg *rwa,
2101a2cdcdd2SPaul Dagnelie     struct drr_write_byref *drrwbr)
21029e69d7d0SLori Alt {
21039e69d7d0SLori Alt 	dmu_tx_t *tx;
21049e69d7d0SLori Alt 	int err;
21059e69d7d0SLori Alt 	guid_map_entry_t gmesrch;
21069e69d7d0SLori Alt 	guid_map_entry_t *gmep;
21079e69d7d0SLori Alt 	avl_index_t where;
21089e69d7d0SLori Alt 	objset_t *ref_os = NULL;
21099e69d7d0SLori Alt 	dmu_buf_t *dbp;
21109e69d7d0SLori Alt 
21119e69d7d0SLori Alt 	if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2112be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
21139e69d7d0SLori Alt 
21149e69d7d0SLori Alt 	/*
21159e69d7d0SLori Alt 	 * If the GUID of the referenced dataset is different from the
21169e69d7d0SLori Alt 	 * GUID of the target dataset, find the referenced dataset.
21179e69d7d0SLori Alt 	 */
21189e69d7d0SLori Alt 	if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
21199e69d7d0SLori Alt 		gmesrch.guid = drrwbr->drr_refguid;
2120a2cdcdd2SPaul Dagnelie 		if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
21219e69d7d0SLori Alt 		    &where)) == NULL) {
2122be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
21239e69d7d0SLori Alt 		}
21249e69d7d0SLori Alt 		if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2125be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
21269e69d7d0SLori Alt 	} else {
2127a2cdcdd2SPaul Dagnelie 		ref_os = rwa->os;
21289e69d7d0SLori Alt 	}
21299e69d7d0SLori Alt 
21305d7b4d43SMatthew Ahrens 	err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
21315d7b4d43SMatthew Ahrens 	    drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
21325d7b4d43SMatthew Ahrens 	if (err != 0)
21339e69d7d0SLori Alt 		return (err);
21349e69d7d0SLori Alt 
2135a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
21369e69d7d0SLori Alt 
21379e69d7d0SLori Alt 	dmu_tx_hold_write(tx, drrwbr->drr_object,
21389e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length);
21399e69d7d0SLori Alt 	err = dmu_tx_assign(tx, TXG_WAIT);
21403b2aab18SMatthew Ahrens 	if (err != 0) {
21419e69d7d0SLori Alt 		dmu_tx_abort(tx);
21429e69d7d0SLori Alt 		return (err);
21439e69d7d0SLori Alt 	}
2144a2cdcdd2SPaul Dagnelie 	dmu_write(rwa->os, drrwbr->drr_object,
21459e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
21469e69d7d0SLori Alt 	dmu_buf_rele(dbp, FTAG);
2147c1a50c7eSMatthew Ahrens 
2148c1a50c7eSMatthew Ahrens 	/* See comment in restore_write. */
2149c1a50c7eSMatthew Ahrens 	save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
21509e69d7d0SLori Alt 	dmu_tx_commit(tx);
21519e69d7d0SLori Alt 	return (0);
21529e69d7d0SLori Alt }
21539e69d7d0SLori Alt 
21540a586ceaSMark Shellenbaum static int
receive_write_embedded(struct receive_writer_arg * rwa,struct drr_write_embedded * drrwe,void * data)2155a2cdcdd2SPaul Dagnelie receive_write_embedded(struct receive_writer_arg *rwa,
2156c1a50c7eSMatthew Ahrens     struct drr_write_embedded *drrwe, void *data)
21575d7b4d43SMatthew Ahrens {
21585d7b4d43SMatthew Ahrens 	dmu_tx_t *tx;
21595d7b4d43SMatthew Ahrens 	int err;
21605d7b4d43SMatthew Ahrens 
2161c1a50c7eSMatthew Ahrens 	if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
21625d7b4d43SMatthew Ahrens 		return (EINVAL);
21635d7b4d43SMatthew Ahrens 
2164c1a50c7eSMatthew Ahrens 	if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
21655d7b4d43SMatthew Ahrens 		return (EINVAL);
21665d7b4d43SMatthew Ahrens 
2167c1a50c7eSMatthew Ahrens 	if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
21685d7b4d43SMatthew Ahrens 		return (EINVAL);
2169c1a50c7eSMatthew Ahrens 	if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
21705d7b4d43SMatthew Ahrens 		return (EINVAL);
21715d7b4d43SMatthew Ahrens 
2172a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
21735d7b4d43SMatthew Ahrens 
2174c1a50c7eSMatthew Ahrens 	dmu_tx_hold_write(tx, drrwe->drr_object,
2175c1a50c7eSMatthew Ahrens 	    drrwe->drr_offset, drrwe->drr_length);
21765d7b4d43SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
21775d7b4d43SMatthew Ahrens 	if (err != 0) {
21785d7b4d43SMatthew Ahrens 		dmu_tx_abort(tx);
21795d7b4d43SMatthew Ahrens 		return (err);
21805d7b4d43SMatthew Ahrens 	}
21815d7b4d43SMatthew Ahrens 
2182c1a50c7eSMatthew Ahrens 	dmu_write_embedded(rwa->os, drrwe->drr_object,
2183c1a50c7eSMatthew Ahrens 	    drrwe->drr_offset, data, drrwe->drr_etype,
2184c1a50c7eSMatthew Ahrens 	    drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2185a2cdcdd2SPaul Dagnelie 	    rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
21865d7b4d43SMatthew Ahrens 
2187c1a50c7eSMatthew Ahrens 	/* See comment in restore_write. */
2188c1a50c7eSMatthew Ahrens 	save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
21895d7b4d43SMatthew Ahrens 	dmu_tx_commit(tx);
21905d7b4d43SMatthew Ahrens 	return (0);
21915d7b4d43SMatthew Ahrens }
21925d7b4d43SMatthew Ahrens 
21935d7b4d43SMatthew Ahrens static int
receive_spill(struct receive_writer_arg * rwa,struct drr_spill * drrs,void * data)2194a2cdcdd2SPaul Dagnelie receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2195a2cdcdd2SPaul Dagnelie     void *data)
21960a586ceaSMark Shellenbaum {
21970a586ceaSMark Shellenbaum 	dmu_tx_t *tx;
21980a586ceaSMark Shellenbaum 	dmu_buf_t *db, *db_spill;
21990a586ceaSMark Shellenbaum 	int err;
22000a586ceaSMark Shellenbaum 
22010a586ceaSMark Shellenbaum 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2202a2cdcdd2SPaul Dagnelie 	    drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2203be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
22040a586ceaSMark Shellenbaum 
2205a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2206be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
22070a586ceaSMark Shellenbaum 
2208a2cdcdd2SPaul Dagnelie 	VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
22090a586ceaSMark Shellenbaum 	if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
22100a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
22110a586ceaSMark Shellenbaum 		return (err);
22120a586ceaSMark Shellenbaum 	}
22130a586ceaSMark Shellenbaum 
2214a2cdcdd2SPaul Dagnelie 	tx = dmu_tx_create(rwa->os);
22150a586ceaSMark Shellenbaum 
22160a586ceaSMark Shellenbaum 	dmu_tx_hold_spill(tx, db->db_object);
22170a586ceaSMark Shellenbaum 
22180a586ceaSMark Shellenbaum 	err = dmu_tx_assign(tx, TXG_WAIT);
22193b2aab18SMatthew Ahrens 	if (err != 0) {
22200a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
22210a586ceaSMark Shellenbaum 		dmu_buf_rele(db_spill, FTAG);
22220a586ceaSMark Shellenbaum 		dmu_tx_abort(tx);
22230a586ceaSMark Shellenbaum 		return (err);
22240a586ceaSMark Shellenbaum 	}
22250a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(db_spill, tx);
22260a586ceaSMark Shellenbaum 
22270a586ceaSMark Shellenbaum 	if (db_spill->db_size < drrs->drr_length)
22280a586ceaSMark Shellenbaum 		VERIFY(0 == dbuf_spill_set_blksz(db_spill,
22290a586ceaSMark Shellenbaum 		    drrs->drr_length, tx));
22300a586ceaSMark Shellenbaum 	bcopy(data, db_spill->db_data, drrs->drr_length);
22310a586ceaSMark Shellenbaum 
22320a586ceaSMark Shellenbaum 	dmu_buf_rele(db, FTAG);
22330a586ceaSMark Shellenbaum 	dmu_buf_rele(db_spill, FTAG);
22340a586ceaSMark Shellenbaum 
22350a586ceaSMark Shellenbaum 	dmu_tx_commit(tx);
22360a586ceaSMark Shellenbaum 	return (0);
22370a586ceaSMark Shellenbaum }
22380a586ceaSMark Shellenbaum 
2239efb80947Sahrens /* ARGSUSED */
2240efb80947Sahrens static int
receive_free(struct receive_writer_arg * rwa,struct drr_free * drrf)2241a2cdcdd2SPaul Dagnelie receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2242efb80947Sahrens {
2243efb80947Sahrens 	int err;
2244efb80947Sahrens 
2245efb80947Sahrens 	if (drrf->drr_length != -1ULL &&
2246efb80947Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2247be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2248efb80947Sahrens 
2249a2cdcdd2SPaul Dagnelie 	if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2250be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2251efb80947Sahrens 
2252a2cdcdd2SPaul Dagnelie 	err = dmu_free_long_range(rwa->os, drrf->drr_object,
2253efb80947Sahrens 	    drrf->drr_offset, drrf->drr_length);
2254a2cdcdd2SPaul Dagnelie 
2255efb80947Sahrens 	return (err);
2256efb80947Sahrens }
2257efb80947Sahrens 
22583b2aab18SMatthew Ahrens /* used to destroy the drc_ds on error */
22593b2aab18SMatthew Ahrens static void
dmu_recv_cleanup_ds(dmu_recv_cookie_t * drc)22603b2aab18SMatthew Ahrens dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
22613b2aab18SMatthew Ahrens {
2262c1a50c7eSMatthew Ahrens 	if (drc->drc_resumable) {
2263c1a50c7eSMatthew Ahrens 		/* wait for our resume state to be written to disk */
2264c1a50c7eSMatthew Ahrens 		txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
2265c1a50c7eSMatthew Ahrens 		dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2266c1a50c7eSMatthew Ahrens 	} else {
2267*a1988827SMatthew Ahrens 		char name[ZFS_MAX_DATASET_NAME_LEN];
22683b2aab18SMatthew Ahrens 		dsl_dataset_name(drc->drc_ds, name);
22693b2aab18SMatthew Ahrens 		dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
22703b2aab18SMatthew Ahrens 		(void) dsl_destroy_head(name);
22713b2aab18SMatthew Ahrens 	}
2272c1a50c7eSMatthew Ahrens }
22733b2aab18SMatthew Ahrens 
227498110f08SMatthew Ahrens static void
receive_cksum(struct receive_arg * ra,int len,void * buf)2275a2cdcdd2SPaul Dagnelie receive_cksum(struct receive_arg *ra, int len, void *buf)
227698110f08SMatthew Ahrens {
227798110f08SMatthew Ahrens 	if (ra->byteswap) {
227898110f08SMatthew Ahrens 		fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
227998110f08SMatthew Ahrens 	} else {
228098110f08SMatthew Ahrens 		fletcher_4_incremental_native(buf, len, &ra->cksum);
228198110f08SMatthew Ahrens 	}
228298110f08SMatthew Ahrens }
228398110f08SMatthew Ahrens 
228498110f08SMatthew Ahrens /*
2285a2cdcdd2SPaul Dagnelie  * Read the payload into a buffer of size len, and update the current record's
2286a2cdcdd2SPaul Dagnelie  * payload field.
2287a2cdcdd2SPaul Dagnelie  * Allocate ra->next_rrd and read the next record's header into
2288a2cdcdd2SPaul Dagnelie  * ra->next_rrd->header.
228998110f08SMatthew Ahrens  * Verify checksum of payload and next record.
229098110f08SMatthew Ahrens  */
229198110f08SMatthew Ahrens static int
receive_read_payload_and_next_header(struct receive_arg * ra,int len,void * buf)2292a2cdcdd2SPaul Dagnelie receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
229398110f08SMatthew Ahrens {
229498110f08SMatthew Ahrens 	int err;
229598110f08SMatthew Ahrens 
229698110f08SMatthew Ahrens 	if (len != 0) {
2297a2cdcdd2SPaul Dagnelie 		ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2298c1a50c7eSMatthew Ahrens 		err = receive_read(ra, len, buf);
229998110f08SMatthew Ahrens 		if (err != 0)
230098110f08SMatthew Ahrens 			return (err);
2301c1a50c7eSMatthew Ahrens 		receive_cksum(ra, len, buf);
2302c1a50c7eSMatthew Ahrens 
2303c1a50c7eSMatthew Ahrens 		/* note: rrd is NULL when reading the begin record's payload */
2304c1a50c7eSMatthew Ahrens 		if (ra->rrd != NULL) {
2305c1a50c7eSMatthew Ahrens 			ra->rrd->payload = buf;
2306c1a50c7eSMatthew Ahrens 			ra->rrd->payload_size = len;
2307c1a50c7eSMatthew Ahrens 			ra->rrd->bytes_read = ra->bytes_read;
2308c1a50c7eSMatthew Ahrens 		}
230998110f08SMatthew Ahrens 	}
231098110f08SMatthew Ahrens 
231198110f08SMatthew Ahrens 	ra->prev_cksum = ra->cksum;
231298110f08SMatthew Ahrens 
2313a2cdcdd2SPaul Dagnelie 	ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2314a2cdcdd2SPaul Dagnelie 	err = receive_read(ra, sizeof (ra->next_rrd->header),
2315a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header);
2316c1a50c7eSMatthew Ahrens 	ra->next_rrd->bytes_read = ra->bytes_read;
2317a2cdcdd2SPaul Dagnelie 	if (err != 0) {
2318a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2319a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
232098110f08SMatthew Ahrens 		return (err);
2321a2cdcdd2SPaul Dagnelie 	}
2322a2cdcdd2SPaul Dagnelie 	if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2323a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2324a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
232598110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
2326a2cdcdd2SPaul Dagnelie 	}
232798110f08SMatthew Ahrens 
232898110f08SMatthew Ahrens 	/*
232998110f08SMatthew Ahrens 	 * Note: checksum is of everything up to but not including the
233098110f08SMatthew Ahrens 	 * checksum itself.
233198110f08SMatthew Ahrens 	 */
233298110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
233398110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2334a2cdcdd2SPaul Dagnelie 	receive_cksum(ra,
233598110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2336a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header);
233798110f08SMatthew Ahrens 
2338a2cdcdd2SPaul Dagnelie 	zio_cksum_t cksum_orig =
2339a2cdcdd2SPaul Dagnelie 	    ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2340a2cdcdd2SPaul Dagnelie 	zio_cksum_t *cksump =
2341a2cdcdd2SPaul Dagnelie 	    &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
234298110f08SMatthew Ahrens 
234398110f08SMatthew Ahrens 	if (ra->byteswap)
2344a2cdcdd2SPaul Dagnelie 		byteswap_record(&ra->next_rrd->header);
234598110f08SMatthew Ahrens 
234698110f08SMatthew Ahrens 	if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2347a2cdcdd2SPaul Dagnelie 	    !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2348a2cdcdd2SPaul Dagnelie 		kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2349a2cdcdd2SPaul Dagnelie 		ra->next_rrd = NULL;
235098110f08SMatthew Ahrens 		return (SET_ERROR(ECKSUM));
2351a2cdcdd2SPaul Dagnelie 	}
235298110f08SMatthew Ahrens 
2353a2cdcdd2SPaul Dagnelie 	receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
235498110f08SMatthew Ahrens 
235598110f08SMatthew Ahrens 	return (0);
235698110f08SMatthew Ahrens }
235798110f08SMatthew Ahrens 
2358a2cdcdd2SPaul Dagnelie /*
2359a2cdcdd2SPaul Dagnelie  * Issue the prefetch reads for any necessary indirect blocks.
2360a2cdcdd2SPaul Dagnelie  *
2361a2cdcdd2SPaul Dagnelie  * We use the object ignore list to tell us whether or not to issue prefetches
2362a2cdcdd2SPaul Dagnelie  * for a given object.  We do this for both correctness (in case the blocksize
2363a2cdcdd2SPaul Dagnelie  * of an object has changed) and performance (if the object doesn't exist, don't
2364a2cdcdd2SPaul Dagnelie  * needlessly try to issue prefetches).  We also trim the list as we go through
2365a2cdcdd2SPaul Dagnelie  * the stream to prevent it from growing to an unbounded size.
2366a2cdcdd2SPaul Dagnelie  *
2367a2cdcdd2SPaul Dagnelie  * The object numbers within will always be in sorted order, and any write
2368a2cdcdd2SPaul Dagnelie  * records we see will also be in sorted order, but they're not sorted with
2369a2cdcdd2SPaul Dagnelie  * respect to each other (i.e. we can get several object records before
2370a2cdcdd2SPaul Dagnelie  * receiving each object's write records).  As a result, once we've reached a
2371a2cdcdd2SPaul Dagnelie  * given object number, we can safely remove any reference to lower object
2372a2cdcdd2SPaul Dagnelie  * numbers in the ignore list. In practice, we receive up to 32 object records
2373a2cdcdd2SPaul Dagnelie  * before receiving write records, so the list can have up to 32 nodes in it.
2374a2cdcdd2SPaul Dagnelie  */
2375a2cdcdd2SPaul Dagnelie /* ARGSUSED */
2376a2cdcdd2SPaul Dagnelie static void
receive_read_prefetch(struct receive_arg * ra,uint64_t object,uint64_t offset,uint64_t length)2377a2cdcdd2SPaul Dagnelie receive_read_prefetch(struct receive_arg *ra,
2378a2cdcdd2SPaul Dagnelie     uint64_t object, uint64_t offset, uint64_t length)
2379a2cdcdd2SPaul Dagnelie {
2380a2cdcdd2SPaul Dagnelie 	struct receive_ign_obj_node *node = list_head(&ra->ignore_obj_list);
2381a2cdcdd2SPaul Dagnelie 	while (node != NULL && node->object < object) {
2382a2cdcdd2SPaul Dagnelie 		VERIFY3P(node, ==, list_remove_head(&ra->ignore_obj_list));
2383a2cdcdd2SPaul Dagnelie 		kmem_free(node, sizeof (*node));
2384a2cdcdd2SPaul Dagnelie 		node = list_head(&ra->ignore_obj_list);
2385a2cdcdd2SPaul Dagnelie 	}
2386a2cdcdd2SPaul Dagnelie 	if (node == NULL || node->object > object) {
2387a2cdcdd2SPaul Dagnelie 		dmu_prefetch(ra->os, object, 1, offset, length,
2388a2cdcdd2SPaul Dagnelie 		    ZIO_PRIORITY_SYNC_READ);
2389a2cdcdd2SPaul Dagnelie 	}
2390a2cdcdd2SPaul Dagnelie }
2391a2cdcdd2SPaul Dagnelie 
2392a2cdcdd2SPaul Dagnelie /*
2393a2cdcdd2SPaul Dagnelie  * Read records off the stream, issuing any necessary prefetches.
2394a2cdcdd2SPaul Dagnelie  */
239598110f08SMatthew Ahrens static int
receive_read_record(struct receive_arg * ra)2396a2cdcdd2SPaul Dagnelie receive_read_record(struct receive_arg *ra)
239798110f08SMatthew Ahrens {
239898110f08SMatthew Ahrens 	int err;
239998110f08SMatthew Ahrens 
2400a2cdcdd2SPaul Dagnelie 	switch (ra->rrd->header.drr_type) {
240198110f08SMatthew Ahrens 	case DRR_OBJECT:
240298110f08SMatthew Ahrens 	{
2403a2cdcdd2SPaul Dagnelie 		struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2404a2cdcdd2SPaul Dagnelie 		uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2405a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(size, KM_SLEEP);
2406a2cdcdd2SPaul Dagnelie 		dmu_object_info_t doi;
2407a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, size, buf);
2408a2cdcdd2SPaul Dagnelie 		if (err != 0) {
2409a2cdcdd2SPaul Dagnelie 			kmem_free(buf, size);
241098110f08SMatthew Ahrens 			return (err);
2411a2cdcdd2SPaul Dagnelie 		}
2412a2cdcdd2SPaul Dagnelie 		err = dmu_object_info(ra->os, drro->drr_object, &doi);
2413a2cdcdd2SPaul Dagnelie 		/*
2414a2cdcdd2SPaul Dagnelie 		 * See receive_read_prefetch for an explanation why we're
2415a2cdcdd2SPaul Dagnelie 		 * storing this object in the ignore_obj_list.
2416a2cdcdd2SPaul Dagnelie 		 */
2417a2cdcdd2SPaul Dagnelie 		if (err == ENOENT ||
2418a2cdcdd2SPaul Dagnelie 		    (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2419a2cdcdd2SPaul Dagnelie 			struct receive_ign_obj_node *node =
2420a2cdcdd2SPaul Dagnelie 			    kmem_zalloc(sizeof (*node),
2421a2cdcdd2SPaul Dagnelie 			    KM_SLEEP);
2422a2cdcdd2SPaul Dagnelie 			node->object = drro->drr_object;
2423a2cdcdd2SPaul Dagnelie #ifdef ZFS_DEBUG
2424a2cdcdd2SPaul Dagnelie 			struct receive_ign_obj_node *last_object =
2425a2cdcdd2SPaul Dagnelie 			    list_tail(&ra->ignore_obj_list);
2426a2cdcdd2SPaul Dagnelie 			uint64_t last_objnum = (last_object != NULL ?
2427a2cdcdd2SPaul Dagnelie 			    last_object->object : 0);
2428a2cdcdd2SPaul Dagnelie 			ASSERT3U(node->object, >, last_objnum);
2429a2cdcdd2SPaul Dagnelie #endif
2430a2cdcdd2SPaul Dagnelie 			list_insert_tail(&ra->ignore_obj_list, node);
2431a2cdcdd2SPaul Dagnelie 			err = 0;
2432a2cdcdd2SPaul Dagnelie 		}
2433a2cdcdd2SPaul Dagnelie 		return (err);
243498110f08SMatthew Ahrens 	}
243598110f08SMatthew Ahrens 	case DRR_FREEOBJECTS:
243698110f08SMatthew Ahrens 	{
2437a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
243898110f08SMatthew Ahrens 		return (err);
243998110f08SMatthew Ahrens 	}
244098110f08SMatthew Ahrens 	case DRR_WRITE:
244198110f08SMatthew Ahrens 	{
2442a2cdcdd2SPaul Dagnelie 		struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
244398110f08SMatthew Ahrens 		arc_buf_t *abuf = arc_loan_buf(dmu_objset_spa(ra->os),
244498110f08SMatthew Ahrens 		    drrw->drr_length);
244598110f08SMatthew Ahrens 
2446a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra,
244798110f08SMatthew Ahrens 		    drrw->drr_length, abuf->b_data);
2448a2cdcdd2SPaul Dagnelie 		if (err != 0) {
244998110f08SMatthew Ahrens 			dmu_return_arcbuf(abuf);
245098110f08SMatthew Ahrens 			return (err);
245198110f08SMatthew Ahrens 		}
2452a2cdcdd2SPaul Dagnelie 		ra->rrd->write_buf = abuf;
2453a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2454a2cdcdd2SPaul Dagnelie 		    drrw->drr_length);
2455a2cdcdd2SPaul Dagnelie 		return (err);
2456a2cdcdd2SPaul Dagnelie 	}
245798110f08SMatthew Ahrens 	case DRR_WRITE_BYREF:
245898110f08SMatthew Ahrens 	{
2459a2cdcdd2SPaul Dagnelie 		struct drr_write_byref *drrwb =
2460a2cdcdd2SPaul Dagnelie 		    &ra->rrd->header.drr_u.drr_write_byref;
2461a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
2462a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2463a2cdcdd2SPaul Dagnelie 		    drrwb->drr_length);
246498110f08SMatthew Ahrens 		return (err);
246598110f08SMatthew Ahrens 	}
246698110f08SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
246798110f08SMatthew Ahrens 	{
246898110f08SMatthew Ahrens 		struct drr_write_embedded *drrwe =
2469a2cdcdd2SPaul Dagnelie 		    &ra->rrd->header.drr_u.drr_write_embedded;
2470a2cdcdd2SPaul Dagnelie 		uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2471a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(size, KM_SLEEP);
2472a2cdcdd2SPaul Dagnelie 
2473a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, size, buf);
2474a2cdcdd2SPaul Dagnelie 		if (err != 0) {
2475a2cdcdd2SPaul Dagnelie 			kmem_free(buf, size);
247698110f08SMatthew Ahrens 			return (err);
2477a2cdcdd2SPaul Dagnelie 		}
2478a2cdcdd2SPaul Dagnelie 
2479a2cdcdd2SPaul Dagnelie 		receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2480a2cdcdd2SPaul Dagnelie 		    drrwe->drr_length);
2481a2cdcdd2SPaul Dagnelie 		return (err);
248298110f08SMatthew Ahrens 	}
248398110f08SMatthew Ahrens 	case DRR_FREE:
248498110f08SMatthew Ahrens 	{
2485a2cdcdd2SPaul Dagnelie 		/*
2486a2cdcdd2SPaul Dagnelie 		 * It might be beneficial to prefetch indirect blocks here, but
2487a2cdcdd2SPaul Dagnelie 		 * we don't really have the data to decide for sure.
2488a2cdcdd2SPaul Dagnelie 		 */
2489a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, 0, NULL);
249098110f08SMatthew Ahrens 		return (err);
249198110f08SMatthew Ahrens 	}
249298110f08SMatthew Ahrens 	case DRR_END:
249398110f08SMatthew Ahrens 	{
2494a2cdcdd2SPaul Dagnelie 		struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
249598110f08SMatthew Ahrens 		if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2496c1a50c7eSMatthew Ahrens 			return (SET_ERROR(ECKSUM));
249798110f08SMatthew Ahrens 		return (0);
249898110f08SMatthew Ahrens 	}
249998110f08SMatthew Ahrens 	case DRR_SPILL:
250098110f08SMatthew Ahrens 	{
2501a2cdcdd2SPaul Dagnelie 		struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2502a2cdcdd2SPaul Dagnelie 		void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2503a2cdcdd2SPaul Dagnelie 		err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2504a2cdcdd2SPaul Dagnelie 		    buf);
250598110f08SMatthew Ahrens 		if (err != 0)
2506a2cdcdd2SPaul Dagnelie 			kmem_free(buf, drrs->drr_length);
250798110f08SMatthew Ahrens 		return (err);
250898110f08SMatthew Ahrens 	}
250998110f08SMatthew Ahrens 	default:
251098110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
251198110f08SMatthew Ahrens 	}
251298110f08SMatthew Ahrens }
251398110f08SMatthew Ahrens 
2514efb80947Sahrens /*
2515a2cdcdd2SPaul Dagnelie  * Commit the records to the pool.
2516a2cdcdd2SPaul Dagnelie  */
2517a2cdcdd2SPaul Dagnelie static int
receive_process_record(struct receive_writer_arg * rwa,struct receive_record_arg * rrd)2518a2cdcdd2SPaul Dagnelie receive_process_record(struct receive_writer_arg *rwa,
2519a2cdcdd2SPaul Dagnelie     struct receive_record_arg *rrd)
2520a2cdcdd2SPaul Dagnelie {
2521a2cdcdd2SPaul Dagnelie 	int err;
2522a2cdcdd2SPaul Dagnelie 
2523c1a50c7eSMatthew Ahrens 	/* Processing in order, therefore bytes_read should be increasing. */
2524c1a50c7eSMatthew Ahrens 	ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2525c1a50c7eSMatthew Ahrens 	rwa->bytes_read = rrd->bytes_read;
2526c1a50c7eSMatthew Ahrens 
2527a2cdcdd2SPaul Dagnelie 	switch (rrd->header.drr_type) {
2528a2cdcdd2SPaul Dagnelie 	case DRR_OBJECT:
2529a2cdcdd2SPaul Dagnelie 	{
2530a2cdcdd2SPaul Dagnelie 		struct drr_object *drro = &rrd->header.drr_u.drr_object;
2531a2cdcdd2SPaul Dagnelie 		err = receive_object(rwa, drro, rrd->payload);
2532a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2533a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2534a2cdcdd2SPaul Dagnelie 		return (err);
2535a2cdcdd2SPaul Dagnelie 	}
2536a2cdcdd2SPaul Dagnelie 	case DRR_FREEOBJECTS:
2537a2cdcdd2SPaul Dagnelie 	{
2538a2cdcdd2SPaul Dagnelie 		struct drr_freeobjects *drrfo =
2539a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_freeobjects;
2540a2cdcdd2SPaul Dagnelie 		return (receive_freeobjects(rwa, drrfo));
2541a2cdcdd2SPaul Dagnelie 	}
2542a2cdcdd2SPaul Dagnelie 	case DRR_WRITE:
2543a2cdcdd2SPaul Dagnelie 	{
2544a2cdcdd2SPaul Dagnelie 		struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2545a2cdcdd2SPaul Dagnelie 		err = receive_write(rwa, drrw, rrd->write_buf);
2546a2cdcdd2SPaul Dagnelie 		/* if receive_write() is successful, it consumes the arc_buf */
2547a2cdcdd2SPaul Dagnelie 		if (err != 0)
2548a2cdcdd2SPaul Dagnelie 			dmu_return_arcbuf(rrd->write_buf);
2549a2cdcdd2SPaul Dagnelie 		rrd->write_buf = NULL;
2550a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2551a2cdcdd2SPaul Dagnelie 		return (err);
2552a2cdcdd2SPaul Dagnelie 	}
2553a2cdcdd2SPaul Dagnelie 	case DRR_WRITE_BYREF:
2554a2cdcdd2SPaul Dagnelie 	{
2555a2cdcdd2SPaul Dagnelie 		struct drr_write_byref *drrwbr =
2556a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_write_byref;
2557a2cdcdd2SPaul Dagnelie 		return (receive_write_byref(rwa, drrwbr));
2558a2cdcdd2SPaul Dagnelie 	}
2559a2cdcdd2SPaul Dagnelie 	case DRR_WRITE_EMBEDDED:
2560a2cdcdd2SPaul Dagnelie 	{
2561a2cdcdd2SPaul Dagnelie 		struct drr_write_embedded *drrwe =
2562a2cdcdd2SPaul Dagnelie 		    &rrd->header.drr_u.drr_write_embedded;
2563a2cdcdd2SPaul Dagnelie 		err = receive_write_embedded(rwa, drrwe, rrd->payload);
2564a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2565a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2566a2cdcdd2SPaul Dagnelie 		return (err);
2567a2cdcdd2SPaul Dagnelie 	}
2568a2cdcdd2SPaul Dagnelie 	case DRR_FREE:
2569a2cdcdd2SPaul Dagnelie 	{
2570a2cdcdd2SPaul Dagnelie 		struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2571a2cdcdd2SPaul Dagnelie 		return (receive_free(rwa, drrf));
2572a2cdcdd2SPaul Dagnelie 	}
2573a2cdcdd2SPaul Dagnelie 	case DRR_SPILL:
2574a2cdcdd2SPaul Dagnelie 	{
2575a2cdcdd2SPaul Dagnelie 		struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2576a2cdcdd2SPaul Dagnelie 		err = receive_spill(rwa, drrs, rrd->payload);
2577a2cdcdd2SPaul Dagnelie 		kmem_free(rrd->payload, rrd->payload_size);
2578a2cdcdd2SPaul Dagnelie 		rrd->payload = NULL;
2579a2cdcdd2SPaul Dagnelie 		return (err);
2580a2cdcdd2SPaul Dagnelie 	}
2581a2cdcdd2SPaul Dagnelie 	default:
2582a2cdcdd2SPaul Dagnelie 		return (SET_ERROR(EINVAL));
2583a2cdcdd2SPaul Dagnelie 	}
2584a2cdcdd2SPaul Dagnelie }
2585a2cdcdd2SPaul Dagnelie 
2586a2cdcdd2SPaul Dagnelie /*
2587a2cdcdd2SPaul Dagnelie  * dmu_recv_stream's worker thread; pull records off the queue, and then call
2588a2cdcdd2SPaul Dagnelie  * receive_process_record  When we're done, signal the main thread and exit.
2589a2cdcdd2SPaul Dagnelie  */
2590a2cdcdd2SPaul Dagnelie static void
receive_writer_thread(void * arg)2591a2cdcdd2SPaul Dagnelie receive_writer_thread(void *arg)
2592a2cdcdd2SPaul Dagnelie {
2593a2cdcdd2SPaul Dagnelie 	struct receive_writer_arg *rwa = arg;
2594a2cdcdd2SPaul Dagnelie 	struct receive_record_arg *rrd;
2595a2cdcdd2SPaul Dagnelie 	for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2596a2cdcdd2SPaul Dagnelie 	    rrd = bqueue_dequeue(&rwa->q)) {
2597a2cdcdd2SPaul Dagnelie 		/*
2598a2cdcdd2SPaul Dagnelie 		 * If there's an error, the main thread will stop putting things
2599a2cdcdd2SPaul Dagnelie 		 * on the queue, but we need to clear everything in it before we
2600a2cdcdd2SPaul Dagnelie 		 * can exit.
2601a2cdcdd2SPaul Dagnelie 		 */
2602a2cdcdd2SPaul Dagnelie 		if (rwa->err == 0) {
2603a2cdcdd2SPaul Dagnelie 			rwa->err = receive_process_record(rwa, rrd);
2604a2cdcdd2SPaul Dagnelie 		} else if (rrd->write_buf != NULL) {
2605a2cdcdd2SPaul Dagnelie 			dmu_return_arcbuf(rrd->write_buf);
2606a2cdcdd2SPaul Dagnelie 			rrd->write_buf = NULL;
2607a2cdcdd2SPaul Dagnelie 			rrd->payload = NULL;
2608a2cdcdd2SPaul Dagnelie 		} else if (rrd->payload != NULL) {
2609a2cdcdd2SPaul Dagnelie 			kmem_free(rrd->payload, rrd->payload_size);
2610a2cdcdd2SPaul Dagnelie 			rrd->payload = NULL;
2611a2cdcdd2SPaul Dagnelie 		}
2612a2cdcdd2SPaul Dagnelie 		kmem_free(rrd, sizeof (*rrd));
2613a2cdcdd2SPaul Dagnelie 	}
2614a2cdcdd2SPaul Dagnelie 	kmem_free(rrd, sizeof (*rrd));
2615a2cdcdd2SPaul Dagnelie 	mutex_enter(&rwa->mutex);
2616a2cdcdd2SPaul Dagnelie 	rwa->done = B_TRUE;
2617a2cdcdd2SPaul Dagnelie 	cv_signal(&rwa->cv);
2618a2cdcdd2SPaul Dagnelie 	mutex_exit(&rwa->mutex);
2619a2cdcdd2SPaul Dagnelie }
2620a2cdcdd2SPaul Dagnelie 
2621c1a50c7eSMatthew Ahrens static int
resume_check(struct receive_arg * ra,nvlist_t * begin_nvl)2622c1a50c7eSMatthew Ahrens resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
2623c1a50c7eSMatthew Ahrens {
2624c1a50c7eSMatthew Ahrens 	uint64_t val;
2625c1a50c7eSMatthew Ahrens 	objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
2626c1a50c7eSMatthew Ahrens 	uint64_t dsobj = dmu_objset_id(ra->os);
2627c1a50c7eSMatthew Ahrens 	uint64_t resume_obj, resume_off;
2628c1a50c7eSMatthew Ahrens 
2629c1a50c7eSMatthew Ahrens 	if (nvlist_lookup_uint64(begin_nvl,
2630c1a50c7eSMatthew Ahrens 	    "resume_object", &resume_obj) != 0 ||
2631c1a50c7eSMatthew Ahrens 	    nvlist_lookup_uint64(begin_nvl,
2632c1a50c7eSMatthew Ahrens 	    "resume_offset", &resume_off) != 0) {
2633c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2634c1a50c7eSMatthew Ahrens 	}
2635c1a50c7eSMatthew Ahrens 	VERIFY0(zap_lookup(mos, dsobj,
2636c1a50c7eSMatthew Ahrens 	    DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
2637c1a50c7eSMatthew Ahrens 	if (resume_obj != val)
2638c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2639c1a50c7eSMatthew Ahrens 	VERIFY0(zap_lookup(mos, dsobj,
2640c1a50c7eSMatthew Ahrens 	    DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
2641c1a50c7eSMatthew Ahrens 	if (resume_off != val)
2642c1a50c7eSMatthew Ahrens 		return (SET_ERROR(EINVAL));
2643c1a50c7eSMatthew Ahrens 
2644c1a50c7eSMatthew Ahrens 	return (0);
2645c1a50c7eSMatthew Ahrens }
2646c1a50c7eSMatthew Ahrens 
2647c1a50c7eSMatthew Ahrens 
2648a2cdcdd2SPaul Dagnelie /*
2649a2cdcdd2SPaul Dagnelie  * Read in the stream's records, one by one, and apply them to the pool.  There
2650a2cdcdd2SPaul Dagnelie  * are two threads involved; the thread that calls this function will spin up a
2651a2cdcdd2SPaul Dagnelie  * worker thread, read the records off the stream one by one, and issue
2652a2cdcdd2SPaul Dagnelie  * prefetches for any necessary indirect blocks.  It will then push the records
2653a2cdcdd2SPaul Dagnelie  * onto an internal blocking queue.  The worker thread will pull the records off
2654a2cdcdd2SPaul Dagnelie  * the queue, and actually write the data into the DMU.  This way, the worker
2655a2cdcdd2SPaul Dagnelie  * thread doesn't have to wait for reads to complete, since everything it needs
2656a2cdcdd2SPaul Dagnelie  * (the indirect blocks) will be prefetched.
2657a2cdcdd2SPaul Dagnelie  *
26583cb34c60Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
2659efb80947Sahrens  */
26603cb34c60Sahrens int
dmu_recv_stream(dmu_recv_cookie_t * drc,vnode_t * vp,offset_t * voffp,int cleanup_fd,uint64_t * action_handlep)2661c99e4bdcSChris Kirby dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
2662c99e4bdcSChris Kirby     int cleanup_fd, uint64_t *action_handlep)
26633cb34c60Sahrens {
266498110f08SMatthew Ahrens 	int err = 0;
2665a2cdcdd2SPaul Dagnelie 	struct receive_arg ra = { 0 };
2666a2cdcdd2SPaul Dagnelie 	struct receive_writer_arg rwa = { 0 };
26679e69d7d0SLori Alt 	int featureflags;
2668c1a50c7eSMatthew Ahrens 	nvlist_t *begin_nvl = NULL;
26693cb34c60Sahrens 
26703b2aab18SMatthew Ahrens 	ra.byteswap = drc->drc_byteswap;
26713b2aab18SMatthew Ahrens 	ra.cksum = drc->drc_cksum;
26723cb34c60Sahrens 	ra.vp = vp;
26733cb34c60Sahrens 	ra.voff = *voffp;
2674c1a50c7eSMatthew Ahrens 
2675c1a50c7eSMatthew Ahrens 	if (dsl_dataset_is_zapified(drc->drc_ds)) {
2676c1a50c7eSMatthew Ahrens 		(void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
2677c1a50c7eSMatthew Ahrens 		    drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
2678c1a50c7eSMatthew Ahrens 		    sizeof (ra.bytes_read), 1, &ra.bytes_read);
2679c1a50c7eSMatthew Ahrens 	}
2680c1a50c7eSMatthew Ahrens 
2681a2cdcdd2SPaul Dagnelie 	list_create(&ra.ignore_obj_list, sizeof (struct receive_ign_obj_node),
2682a2cdcdd2SPaul Dagnelie 	    offsetof(struct receive_ign_obj_node, node));
2683efb80947Sahrens 
26843cb34c60Sahrens 	/* these were verified in dmu_recv_begin */
26853b2aab18SMatthew Ahrens 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
26869e69d7d0SLori Alt 	    DMU_SUBSTREAM);
26873b2aab18SMatthew Ahrens 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2688efb80947Sahrens 
2689efb80947Sahrens 	/*
2690efb80947Sahrens 	 * Open the objset we are modifying.
2691efb80947Sahrens 	 */
269298110f08SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
2693efb80947Sahrens 
2694c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2695efb80947Sahrens 
26969e69d7d0SLori Alt 	featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
26979e69d7d0SLori Alt 
26989e69d7d0SLori Alt 	/* if this stream is dedup'ed, set up the avl tree for guid mapping */
26999e69d7d0SLori Alt 	if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2700a7f53a56SChris Kirby 		minor_t minor;
2701a7f53a56SChris Kirby 
2702c99e4bdcSChris Kirby 		if (cleanup_fd == -1) {
2703be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EBADF);
2704c99e4bdcSChris Kirby 			goto out;
2705c99e4bdcSChris Kirby 		}
2706a7f53a56SChris Kirby 		ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
27073b2aab18SMatthew Ahrens 		if (ra.err != 0) {
2708a7f53a56SChris Kirby 			cleanup_fd = -1;
2709a7f53a56SChris Kirby 			goto out;
2710a7f53a56SChris Kirby 		}
2711a7f53a56SChris Kirby 
2712c99e4bdcSChris Kirby 		if (*action_handlep == 0) {
2713a2cdcdd2SPaul Dagnelie 			rwa.guid_to_ds_map =
2714c99e4bdcSChris Kirby 			    kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2715a2cdcdd2SPaul Dagnelie 			avl_create(rwa.guid_to_ds_map, guid_compare,
27169e69d7d0SLori Alt 			    sizeof (guid_map_entry_t),
27179e69d7d0SLori Alt 			    offsetof(guid_map_entry_t, avlnode));
271898110f08SMatthew Ahrens 			err = zfs_onexit_add_cb(minor,
2719a2cdcdd2SPaul Dagnelie 			    free_guid_map_onexit, rwa.guid_to_ds_map,
2720c99e4bdcSChris Kirby 			    action_handlep);
27213b2aab18SMatthew Ahrens 			if (ra.err != 0)
2722c99e4bdcSChris Kirby 				goto out;
2723c99e4bdcSChris Kirby 		} else {
272498110f08SMatthew Ahrens 			err = zfs_onexit_cb_data(minor, *action_handlep,
2725a2cdcdd2SPaul Dagnelie 			    (void **)&rwa.guid_to_ds_map);
27263b2aab18SMatthew Ahrens 			if (ra.err != 0)
2727c99e4bdcSChris Kirby 				goto out;
2728c99e4bdcSChris Kirby 		}
2729ec5cf9d5SAlexander Stetsenko 
2730a2cdcdd2SPaul Dagnelie 		drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
27319e69d7d0SLori Alt 	}
27329e69d7d0SLori Alt 
2733c1a50c7eSMatthew Ahrens 	uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
2734c1a50c7eSMatthew Ahrens 	void *payload = NULL;
2735c1a50c7eSMatthew Ahrens 	if (payloadlen != 0)
2736c1a50c7eSMatthew Ahrens 		payload = kmem_alloc(payloadlen, KM_SLEEP);
2737c1a50c7eSMatthew Ahrens 
2738c1a50c7eSMatthew Ahrens 	err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
2739c1a50c7eSMatthew Ahrens 	if (err != 0) {
2740c1a50c7eSMatthew Ahrens 		if (payloadlen != 0)
2741c1a50c7eSMatthew Ahrens 			kmem_free(payload, payloadlen);
274298110f08SMatthew Ahrens 		goto out;
2743c1a50c7eSMatthew Ahrens 	}
2744c1a50c7eSMatthew Ahrens 	if (payloadlen != 0) {
2745c1a50c7eSMatthew Ahrens 		err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
2746c1a50c7eSMatthew Ahrens 		kmem_free(payload, payloadlen);
2747c1a50c7eSMatthew Ahrens 		if (err != 0)
2748c1a50c7eSMatthew Ahrens 			goto out;
2749c1a50c7eSMatthew Ahrens 	}
2750c1a50c7eSMatthew Ahrens 
2751c1a50c7eSMatthew Ahrens 	if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
2752c1a50c7eSMatthew Ahrens 		err = resume_check(&ra, begin_nvl);
2753c1a50c7eSMatthew Ahrens 		if (err != 0)
2754c1a50c7eSMatthew Ahrens 			goto out;
2755c1a50c7eSMatthew Ahrens 	}
275698110f08SMatthew Ahrens 
2757a2cdcdd2SPaul Dagnelie 	(void) bqueue_init(&rwa.q, zfs_recv_queue_length,
2758a2cdcdd2SPaul Dagnelie 	    offsetof(struct receive_record_arg, node));
2759a2cdcdd2SPaul Dagnelie 	cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
2760a2cdcdd2SPaul Dagnelie 	mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
2761a2cdcdd2SPaul Dagnelie 	rwa.os = ra.os;
2762a2cdcdd2SPaul Dagnelie 	rwa.byteswap = drc->drc_byteswap;
2763c1a50c7eSMatthew Ahrens 	rwa.resumable = drc->drc_resumable;
2764a2cdcdd2SPaul Dagnelie 
2765a2cdcdd2SPaul Dagnelie 	(void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, curproc,
2766a2cdcdd2SPaul Dagnelie 	    TS_RUN, minclsyspri);
2767a2cdcdd2SPaul Dagnelie 	/*
2768a2cdcdd2SPaul Dagnelie 	 * We're reading rwa.err without locks, which is safe since we are the
2769a2cdcdd2SPaul Dagnelie 	 * only reader, and the worker thread is the only writer.  It's ok if we
2770a2cdcdd2SPaul Dagnelie 	 * miss a write for an iteration or two of the loop, since the writer
2771a2cdcdd2SPaul Dagnelie 	 * thread will keep freeing records we send it until we send it an eos
2772a2cdcdd2SPaul Dagnelie 	 * marker.
2773a2cdcdd2SPaul Dagnelie 	 *
2774a2cdcdd2SPaul Dagnelie 	 * We can leave this loop in 3 ways:  First, if rwa.err is
2775a2cdcdd2SPaul Dagnelie 	 * non-zero.  In that case, the writer thread will free the rrd we just
2776a2cdcdd2SPaul Dagnelie 	 * pushed.  Second, if  we're interrupted; in that case, either it's the
2777a2cdcdd2SPaul Dagnelie 	 * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
2778a2cdcdd2SPaul Dagnelie 	 * has been handed off to the writer thread who will free it.  Finally,
2779a2cdcdd2SPaul Dagnelie 	 * if receive_read_record fails or we're at the end of the stream, then
2780a2cdcdd2SPaul Dagnelie 	 * we free ra.rrd and exit.
2781a2cdcdd2SPaul Dagnelie 	 */
2782a2cdcdd2SPaul Dagnelie 	while (rwa.err == 0) {
2783efb80947Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
278498110f08SMatthew Ahrens 			err = SET_ERROR(EINTR);
278598110f08SMatthew Ahrens 			break;
2786efb80947Sahrens 		}
2787efb80947Sahrens 
2788a2cdcdd2SPaul Dagnelie 		ASSERT3P(ra.rrd, ==, NULL);
2789a2cdcdd2SPaul Dagnelie 		ra.rrd = ra.next_rrd;
2790a2cdcdd2SPaul Dagnelie 		ra.next_rrd = NULL;
2791a2cdcdd2SPaul Dagnelie 		/* Allocates and loads header into ra.next_rrd */
2792a2cdcdd2SPaul Dagnelie 		err = receive_read_record(&ra);
2793efb80947Sahrens 
2794a2cdcdd2SPaul Dagnelie 		if (ra.rrd->header.drr_type == DRR_END || err != 0) {
2795a2cdcdd2SPaul Dagnelie 			kmem_free(ra.rrd, sizeof (*ra.rrd));
2796a2cdcdd2SPaul Dagnelie 			ra.rrd = NULL;
2797efb80947Sahrens 			break;
2798efb80947Sahrens 		}
2799efb80947Sahrens 
2800a2cdcdd2SPaul Dagnelie 		bqueue_enqueue(&rwa.q, ra.rrd,
2801a2cdcdd2SPaul Dagnelie 		    sizeof (struct receive_record_arg) + ra.rrd->payload_size);
2802a2cdcdd2SPaul Dagnelie 		ra.rrd = NULL;
2803a2cdcdd2SPaul Dagnelie 	}
2804a2cdcdd2SPaul Dagnelie 	if (ra.next_rrd == NULL)
2805a2cdcdd2SPaul Dagnelie 		ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
2806a2cdcdd2SPaul Dagnelie 	ra.next_rrd->eos_marker = B_TRUE;
2807a2cdcdd2SPaul Dagnelie 	bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
2808a2cdcdd2SPaul Dagnelie 
2809a2cdcdd2SPaul Dagnelie 	mutex_enter(&rwa.mutex);
2810a2cdcdd2SPaul Dagnelie 	while (!rwa.done) {
2811a2cdcdd2SPaul Dagnelie 		cv_wait(&rwa.cv, &rwa.mutex);
2812a2cdcdd2SPaul Dagnelie 	}
2813a2cdcdd2SPaul Dagnelie 	mutex_exit(&rwa.mutex);
2814a2cdcdd2SPaul Dagnelie 
2815a2cdcdd2SPaul Dagnelie 	cv_destroy(&rwa.cv);
2816a2cdcdd2SPaul Dagnelie 	mutex_destroy(&rwa.mutex);
2817a2cdcdd2SPaul Dagnelie 	bqueue_destroy(&rwa.q);
2818a2cdcdd2SPaul Dagnelie 	if (err == 0)
2819a2cdcdd2SPaul Dagnelie 		err = rwa.err;
2820a2cdcdd2SPaul Dagnelie 
2821efb80947Sahrens out:
2822c1a50c7eSMatthew Ahrens 	nvlist_free(begin_nvl);
2823a7f53a56SChris Kirby 	if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2824a7f53a56SChris Kirby 		zfs_onexit_fd_rele(cleanup_fd);
2825a7f53a56SChris Kirby 
282698110f08SMatthew Ahrens 	if (err != 0) {
2827efb80947Sahrens 		/*
2828c1a50c7eSMatthew Ahrens 		 * Clean up references. If receive is not resumable,
2829c1a50c7eSMatthew Ahrens 		 * destroy what we created, so we don't leave it in
2830c1a50c7eSMatthew Ahrens 		 * the inconsistent state.
2831efb80947Sahrens 		 */
28323b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
2833efb80947Sahrens 	}
2834efb80947Sahrens 
28353cb34c60Sahrens 	*voffp = ra.voff;
2836a2cdcdd2SPaul Dagnelie 	for (struct receive_ign_obj_node *n =
2837a2cdcdd2SPaul Dagnelie 	    list_remove_head(&ra.ignore_obj_list); n != NULL;
2838a2cdcdd2SPaul Dagnelie 	    n = list_remove_head(&ra.ignore_obj_list)) {
2839a2cdcdd2SPaul Dagnelie 		kmem_free(n, sizeof (*n));
2840a2cdcdd2SPaul Dagnelie 	}
2841a2cdcdd2SPaul Dagnelie 	list_destroy(&ra.ignore_obj_list);
284298110f08SMatthew Ahrens 	return (err);
2843efb80947Sahrens }
2844f18faf3fSek110237 
28453cb34c60Sahrens static int
dmu_recv_end_check(void * arg,dmu_tx_t * tx)28463b2aab18SMatthew Ahrens dmu_recv_end_check(void *arg, dmu_tx_t *tx)
2847f18faf3fSek110237 {
28483b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
28493b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
28503b2aab18SMatthew Ahrens 	int error;
2851f18faf3fSek110237 
28523b2aab18SMatthew Ahrens 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
28533b2aab18SMatthew Ahrens 
28543b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
28553b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
28563b2aab18SMatthew Ahrens 
28573b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
28583b2aab18SMatthew Ahrens 		if (error != 0)
28593b2aab18SMatthew Ahrens 			return (error);
286034f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
286134f2f8cfSMatthew Ahrens 			/*
286234f2f8cfSMatthew Ahrens 			 * We will destroy any snapshots in tofs (i.e. before
286334f2f8cfSMatthew Ahrens 			 * origin_head) that are after the origin (which is
286434f2f8cfSMatthew Ahrens 			 * the snap before drc_ds, because drc_ds can not
286534f2f8cfSMatthew Ahrens 			 * have any snaps of its own).
286634f2f8cfSMatthew Ahrens 			 */
2867c1379625SJustin T. Gibbs 			uint64_t obj;
2868c1379625SJustin T. Gibbs 
2869c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2870c1379625SJustin T. Gibbs 			while (obj !=
2871c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
287234f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
287334f2f8cfSMatthew Ahrens 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
287434f2f8cfSMatthew Ahrens 				    &snap);
287534f2f8cfSMatthew Ahrens 				if (error != 0)
2876beddaa9cSAndriy Gapon 					break;
287734f2f8cfSMatthew Ahrens 				if (snap->ds_dir != origin_head->ds_dir)
287834f2f8cfSMatthew Ahrens 					error = SET_ERROR(EINVAL);
287934f2f8cfSMatthew Ahrens 				if (error == 0)  {
288034f2f8cfSMatthew Ahrens 					error = dsl_destroy_snapshot_check_impl(
288134f2f8cfSMatthew Ahrens 					    snap, B_FALSE);
288234f2f8cfSMatthew Ahrens 				}
2883c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
288434f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
288534f2f8cfSMatthew Ahrens 				if (error != 0)
2886beddaa9cSAndriy Gapon 					break;
2887beddaa9cSAndriy Gapon 			}
2888beddaa9cSAndriy Gapon 			if (error != 0) {
2889beddaa9cSAndriy Gapon 				dsl_dataset_rele(origin_head, FTAG);
289034f2f8cfSMatthew Ahrens 				return (error);
289134f2f8cfSMatthew Ahrens 			}
289234f2f8cfSMatthew Ahrens 		}
28933b2aab18SMatthew Ahrens 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
289491948b51SKeith M Wesolowski 		    origin_head, drc->drc_force, drc->drc_owner, tx);
28953b2aab18SMatthew Ahrens 		if (error != 0) {
28963b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin_head, FTAG);
28973b2aab18SMatthew Ahrens 			return (error);
28983b2aab18SMatthew Ahrens 		}
28993b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(origin_head,
2900a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
29013b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
29023b2aab18SMatthew Ahrens 		if (error != 0)
29033b2aab18SMatthew Ahrens 			return (error);
29043b2aab18SMatthew Ahrens 
29053b2aab18SMatthew Ahrens 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
29063b2aab18SMatthew Ahrens 	} else {
29073b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
2908a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
29093b2aab18SMatthew Ahrens 	}
29103b2aab18SMatthew Ahrens 	return (error);
29113cb34c60Sahrens }
29123cb34c60Sahrens 
29133cb34c60Sahrens static void
dmu_recv_end_sync(void * arg,dmu_tx_t * tx)29143b2aab18SMatthew Ahrens dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
29153cb34c60Sahrens {
29163b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
29173b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
29183cb34c60Sahrens 
29193b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
29203b2aab18SMatthew Ahrens 	    tx, "snap=%s", drc->drc_tosnap);
29213b2aab18SMatthew Ahrens 
29223b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
29233b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
29243b2aab18SMatthew Ahrens 
29253b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
29263b2aab18SMatthew Ahrens 		    &origin_head));
292734f2f8cfSMatthew Ahrens 
292834f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
292934f2f8cfSMatthew Ahrens 			/*
293034f2f8cfSMatthew Ahrens 			 * Destroy any snapshots of drc_tofs (origin_head)
293134f2f8cfSMatthew Ahrens 			 * after the origin (the snap before drc_ds).
293234f2f8cfSMatthew Ahrens 			 */
2933c1379625SJustin T. Gibbs 			uint64_t obj;
2934c1379625SJustin T. Gibbs 
2935c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2936c1379625SJustin T. Gibbs 			while (obj !=
2937c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
293834f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
293934f2f8cfSMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
294034f2f8cfSMatthew Ahrens 				    &snap));
294134f2f8cfSMatthew Ahrens 				ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
2942c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
294334f2f8cfSMatthew Ahrens 				dsl_destroy_snapshot_sync_impl(snap,
294434f2f8cfSMatthew Ahrens 				    B_FALSE, tx);
294534f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
294634f2f8cfSMatthew Ahrens 			}
294734f2f8cfSMatthew Ahrens 		}
294834f2f8cfSMatthew Ahrens 		VERIFY3P(drc->drc_ds->ds_prev, ==,
294934f2f8cfSMatthew Ahrens 		    origin_head->ds_prev);
295034f2f8cfSMatthew Ahrens 
29513b2aab18SMatthew Ahrens 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
29523b2aab18SMatthew Ahrens 		    origin_head, tx);
29533b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(origin_head,
29543b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
29553b2aab18SMatthew Ahrens 
29563b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
29573b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
2958c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
29593b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
2960c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
29613b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_toguid;
2962c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
29633b2aab18SMatthew Ahrens 		    ~DS_FLAG_INCONSISTENT;
29643b2aab18SMatthew Ahrens 
29653b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2966c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_flags &=
2967c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
29683b2aab18SMatthew Ahrens 
29693b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
29703b2aab18SMatthew Ahrens 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
297191948b51SKeith M Wesolowski 
297291948b51SKeith M Wesolowski 		if (drc->drc_owner != NULL)
297391948b51SKeith M Wesolowski 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
29743b2aab18SMatthew Ahrens 	} else {
29753b2aab18SMatthew Ahrens 		dsl_dataset_t *ds = drc->drc_ds;
29763b2aab18SMatthew Ahrens 
29773b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
29783cb34c60Sahrens 
29793cb34c60Sahrens 		/* set snapshot's creation time and guid */
29803cb34c60Sahrens 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
2981c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
29823b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
2983c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_guid =
2984c1379625SJustin T. Gibbs 		    drc->drc_drrb->drr_toguid;
2985c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_flags &=
2986c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
29873cb34c60Sahrens 
29883cb34c60Sahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2989c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
2990c1a50c7eSMatthew Ahrens 		if (dsl_dataset_has_resume_receive_state(ds)) {
2991c1a50c7eSMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2992c1a50c7eSMatthew Ahrens 			    DS_FIELD_RESUME_FROMGUID, tx);
2993c1a50c7eSMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2994c1a50c7eSMatthew Ahrens 			    DS_FIELD_RESUME_OBJECT, tx);
2995c1a50c7eSMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2996c1a50c7eSMatthew Ahrens 			    DS_FIELD_RESUME_OFFSET, tx);
2997c1a50c7eSMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
2998c1a50c7eSMatthew Ahrens 			    DS_FIELD_RESUME_BYTES, tx);
2999c1a50c7eSMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3000c1a50c7eSMatthew Ahrens 			    DS_FIELD_RESUME_TOGUID, tx);
3001c1a50c7eSMatthew Ahrens 			(void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3002c1a50c7eSMatthew Ahrens 			    DS_FIELD_RESUME_TONAME, tx);
3003c1a50c7eSMatthew Ahrens 		}
30043b2aab18SMatthew Ahrens 	}
3005c1379625SJustin T. Gibbs 	drc->drc_newsnapobj = dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
30063b2aab18SMatthew Ahrens 	/*
30073b2aab18SMatthew Ahrens 	 * Release the hold from dmu_recv_begin.  This must be done before
30083b2aab18SMatthew Ahrens 	 * we return to open context, so that when we free the dataset's dnode,
30093b2aab18SMatthew Ahrens 	 * we can evict its bonus buffer.
30103b2aab18SMatthew Ahrens 	 */
30113b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
30123b2aab18SMatthew Ahrens 	drc->drc_ds = NULL;
30133cb34c60Sahrens }
30143cb34c60Sahrens 
3015ae46e4c7SMatthew Ahrens static int
add_ds_to_guidmap(const char * name,avl_tree_t * guid_map,uint64_t snapobj)30163b2aab18SMatthew Ahrens add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3017ec5cf9d5SAlexander Stetsenko {
30183b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
3019ec5cf9d5SAlexander Stetsenko 	dsl_dataset_t *snapds;
3020ec5cf9d5SAlexander Stetsenko 	guid_map_entry_t *gmep;
3021ec5cf9d5SAlexander Stetsenko 	int err;
3022ec5cf9d5SAlexander Stetsenko 
3023ec5cf9d5SAlexander Stetsenko 	ASSERT(guid_map != NULL);
3024ec5cf9d5SAlexander Stetsenko 
30253b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
30263b2aab18SMatthew Ahrens 	if (err != 0)
30273b2aab18SMatthew Ahrens 		return (err);
3028de8d9cffSMatthew Ahrens 	gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3029de8d9cffSMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3030ec5cf9d5SAlexander Stetsenko 	if (err == 0) {
3031c1379625SJustin T. Gibbs 		gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3032ec5cf9d5SAlexander Stetsenko 		gmep->gme_ds = snapds;
3033ec5cf9d5SAlexander Stetsenko 		avl_add(guid_map, gmep);
30343b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(snapds, gmep);
3035de8d9cffSMatthew Ahrens 	} else {
3036de8d9cffSMatthew Ahrens 		kmem_free(gmep, sizeof (*gmep));
3037ec5cf9d5SAlexander Stetsenko 	}
3038ec5cf9d5SAlexander Stetsenko 
30393b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
3040ec5cf9d5SAlexander Stetsenko 	return (err);
3041ec5cf9d5SAlexander Stetsenko }
3042ec5cf9d5SAlexander Stetsenko 
30433b2aab18SMatthew Ahrens static int dmu_recv_end_modified_blocks = 3;
30443b2aab18SMatthew Ahrens 
3045ec5cf9d5SAlexander Stetsenko static int
dmu_recv_existing_end(dmu_recv_cookie_t * drc)3046ae46e4c7SMatthew Ahrens dmu_recv_existing_end(dmu_recv_cookie_t *drc)
30473cb34c60Sahrens {
30483b2aab18SMatthew Ahrens 	int error;
30493cb34c60Sahrens 
30503b2aab18SMatthew Ahrens #ifdef _KERNEL
30513b2aab18SMatthew Ahrens 	/*
30523b2aab18SMatthew Ahrens 	 * We will be destroying the ds; make sure its origin is unmounted if
30533b2aab18SMatthew Ahrens 	 * necessary.
30543b2aab18SMatthew Ahrens 	 */
3055*a1988827SMatthew Ahrens 	char name[ZFS_MAX_DATASET_NAME_LEN];
30563b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
30573b2aab18SMatthew Ahrens 	zfs_destroy_unmount_origin(name);
30583b2aab18SMatthew Ahrens #endif
30593cb34c60Sahrens 
30603b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
30613b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
30627d46dc6cSMatthew Ahrens 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
30633cb34c60Sahrens 
30643b2aab18SMatthew Ahrens 	if (error != 0)
30653b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
30663b2aab18SMatthew Ahrens 	return (error);
3067ae46e4c7SMatthew Ahrens }
3068ae46e4c7SMatthew Ahrens 
3069ae46e4c7SMatthew Ahrens static int
dmu_recv_new_end(dmu_recv_cookie_t * drc)3070ae46e4c7SMatthew Ahrens dmu_recv_new_end(dmu_recv_cookie_t *drc)
3071ae46e4c7SMatthew Ahrens {
30723b2aab18SMatthew Ahrens 	int error;
3073ae46e4c7SMatthew Ahrens 
30743b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
30753b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
30767d46dc6cSMatthew Ahrens 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3077ae46e4c7SMatthew Ahrens 
30783b2aab18SMatthew Ahrens 	if (error != 0) {
30793b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
30803b2aab18SMatthew Ahrens 	} else if (drc->drc_guid_to_ds_map != NULL) {
30813b2aab18SMatthew Ahrens 		(void) add_ds_to_guidmap(drc->drc_tofs,
30823b2aab18SMatthew Ahrens 		    drc->drc_guid_to_ds_map,
30833b2aab18SMatthew Ahrens 		    drc->drc_newsnapobj);
3084ae46e4c7SMatthew Ahrens 	}
30853b2aab18SMatthew Ahrens 	return (error);
3086f18faf3fSek110237 }
3087ae46e4c7SMatthew Ahrens 
3088ae46e4c7SMatthew Ahrens int
dmu_recv_end(dmu_recv_cookie_t * drc,void * owner)308991948b51SKeith M Wesolowski dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3090ae46e4c7SMatthew Ahrens {
309191948b51SKeith M Wesolowski 	drc->drc_owner = owner;
309291948b51SKeith M Wesolowski 
30933b2aab18SMatthew Ahrens 	if (drc->drc_newfs)
3094ae46e4c7SMatthew Ahrens 		return (dmu_recv_new_end(drc));
30953b2aab18SMatthew Ahrens 	else
30963b2aab18SMatthew Ahrens 		return (dmu_recv_existing_end(drc));
3097ae46e4c7SMatthew Ahrens }
30982f3d8780SMatthew Ahrens 
30992f3d8780SMatthew Ahrens /*
31002f3d8780SMatthew Ahrens  * Return TRUE if this objset is currently being received into.
31012f3d8780SMatthew Ahrens  */
31022f3d8780SMatthew Ahrens boolean_t
dmu_objset_is_receiving(objset_t * os)31032f3d8780SMatthew Ahrens dmu_objset_is_receiving(objset_t *os)
31042f3d8780SMatthew Ahrens {
31052f3d8780SMatthew Ahrens 	return (os->os_dsl_dataset != NULL &&
31062f3d8780SMatthew Ahrens 	    os->os_dsl_dataset->ds_owner == dmu_recv_tag);
31072f3d8780SMatthew Ahrens }
3108