xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision 98110f08fa182032082d98be2ddb9391fcd62bf1)
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.
247802d7bfSMatthew Ahrens  * Copyright (c) 2011, 2014 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>
55efb80947Sahrens 
5619b94df9SMatthew Ahrens /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
5719b94df9SMatthew Ahrens int zfs_send_corrupt_data = B_FALSE;
5819b94df9SMatthew Ahrens 
593cb34c60Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
603b2aab18SMatthew Ahrens static const char *recv_clone_name = "%recv";
613cb34c60Sahrens 
62efb80947Sahrens static int
634e3c9f44SBill Pijewski dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
64efb80947Sahrens {
654e3c9f44SBill Pijewski 	dsl_dataset_t *ds = dsp->dsa_os->os_dsl_dataset;
66efb80947Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
67fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
68efb80947Sahrens 
694e3c9f44SBill Pijewski 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
70efb80947Sahrens 	    (caddr_t)buf, len,
71efb80947Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
724e3c9f44SBill Pijewski 
734e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
744e3c9f44SBill Pijewski 	*dsp->dsa_off += len;
754e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
764e3c9f44SBill Pijewski 
774e3c9f44SBill Pijewski 	return (dsp->dsa_err);
78efb80947Sahrens }
79efb80947Sahrens 
80*98110f08SMatthew Ahrens /*
81*98110f08SMatthew Ahrens  * For all record types except BEGIN, fill in the checksum (overlaid in
82*98110f08SMatthew Ahrens  * drr_u.drr_checksum.drr_checksum).  The checksum verifies everything
83*98110f08SMatthew Ahrens  * up to the start of the checksum itself.
84*98110f08SMatthew Ahrens  */
85*98110f08SMatthew Ahrens static int
86*98110f08SMatthew Ahrens dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
87*98110f08SMatthew Ahrens {
88*98110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
89*98110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
90*98110f08SMatthew Ahrens 	fletcher_4_incremental_native(dsp->dsa_drr,
91*98110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
92*98110f08SMatthew Ahrens 	    &dsp->dsa_zc);
93*98110f08SMatthew Ahrens 	if (dsp->dsa_drr->drr_type != DRR_BEGIN) {
94*98110f08SMatthew Ahrens 		ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
95*98110f08SMatthew Ahrens 		    drr_checksum.drr_checksum));
96*98110f08SMatthew Ahrens 		dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
97*98110f08SMatthew Ahrens 	}
98*98110f08SMatthew Ahrens 	fletcher_4_incremental_native(&dsp->dsa_drr->
99*98110f08SMatthew Ahrens 	    drr_u.drr_checksum.drr_checksum,
100*98110f08SMatthew Ahrens 	    sizeof (zio_cksum_t), &dsp->dsa_zc);
101*98110f08SMatthew Ahrens 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
102*98110f08SMatthew Ahrens 		return (SET_ERROR(EINTR));
103*98110f08SMatthew Ahrens 	if (payload_len != 0) {
104*98110f08SMatthew Ahrens 		fletcher_4_incremental_native(payload, payload_len,
105*98110f08SMatthew Ahrens 		    &dsp->dsa_zc);
106*98110f08SMatthew Ahrens 		if (dump_bytes(dsp, payload, payload_len) != 0)
107*98110f08SMatthew Ahrens 			return (SET_ERROR(EINTR));
108*98110f08SMatthew Ahrens 	}
109*98110f08SMatthew Ahrens 	return (0);
110*98110f08SMatthew Ahrens }
111*98110f08SMatthew Ahrens 
112efb80947Sahrens static int
1134e3c9f44SBill Pijewski dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
114efb80947Sahrens     uint64_t length)
115efb80947Sahrens {
1164e3c9f44SBill Pijewski 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
1179e69d7d0SLori Alt 
1182f3d8780SMatthew Ahrens 	/*
1192f3d8780SMatthew Ahrens 	 * When we receive a free record, dbuf_free_range() assumes
1202f3d8780SMatthew Ahrens 	 * that the receiving system doesn't have any dbufs in the range
1212f3d8780SMatthew Ahrens 	 * being freed.  This is always true because there is a one-record
1222f3d8780SMatthew Ahrens 	 * constraint: we only send one WRITE record for any given
1232f3d8780SMatthew Ahrens 	 * object+offset.  We know that the one-record constraint is
1242f3d8780SMatthew Ahrens 	 * true because we always send data in increasing order by
1252f3d8780SMatthew Ahrens 	 * object,offset.
1262f3d8780SMatthew Ahrens 	 *
1272f3d8780SMatthew Ahrens 	 * If the increasing-order constraint ever changes, we should find
1282f3d8780SMatthew Ahrens 	 * another way to assert that the one-record constraint is still
1292f3d8780SMatthew Ahrens 	 * satisfied.
1302f3d8780SMatthew Ahrens 	 */
1312f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
1322f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
1332f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
1342f3d8780SMatthew Ahrens 
1352f3d8780SMatthew Ahrens 	/*
1362f3d8780SMatthew Ahrens 	 * If we are doing a non-incremental send, then there can't
1372f3d8780SMatthew Ahrens 	 * be any data in the dataset we're receiving into.  Therefore
1382f3d8780SMatthew Ahrens 	 * a free record would simply be a no-op.  Save space by not
1392f3d8780SMatthew Ahrens 	 * sending it to begin with.
1402f3d8780SMatthew Ahrens 	 */
1412f3d8780SMatthew Ahrens 	if (!dsp->dsa_incremental)
1422f3d8780SMatthew Ahrens 		return (0);
1432f3d8780SMatthew Ahrens 
144534029e5SSimon Klinkert 	if (length != -1ULL && offset + length < offset)
145534029e5SSimon Klinkert 		length = -1ULL;
146534029e5SSimon Klinkert 
1479e69d7d0SLori Alt 	/*
1489e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
1499e69d7d0SLori Alt 	 * since free block aggregation can only be done for blocks of the
1509e69d7d0SLori Alt 	 * same type (i.e., DRR_FREE records can only be aggregated with
1519e69d7d0SLori Alt 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
1529e69d7d0SLori Alt 	 * aggregated with other DRR_FREEOBJECTS records.
1539e69d7d0SLori Alt 	 */
1544e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
1554e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREE) {
156*98110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
157be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1584e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
1599e69d7d0SLori Alt 	}
1609e69d7d0SLori Alt 
1614e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREE) {
1629e69d7d0SLori Alt 		/*
1639e69d7d0SLori Alt 		 * There should never be a PENDING_FREE if length is -1
1649e69d7d0SLori Alt 		 * (because dump_dnode is the only place where this
1659e69d7d0SLori Alt 		 * function is called with a -1, and only after flushing
1669e69d7d0SLori Alt 		 * any pending record).
1679e69d7d0SLori Alt 		 */
1689e69d7d0SLori Alt 		ASSERT(length != -1ULL);
1699e69d7d0SLori Alt 		/*
1709e69d7d0SLori Alt 		 * Check to see whether this free block can be aggregated
1719e69d7d0SLori Alt 		 * with pending one.
1729e69d7d0SLori Alt 		 */
1739e69d7d0SLori Alt 		if (drrf->drr_object == object && drrf->drr_offset +
1749e69d7d0SLori Alt 		    drrf->drr_length == offset) {
1759e69d7d0SLori Alt 			drrf->drr_length += length;
1769e69d7d0SLori Alt 			return (0);
1779e69d7d0SLori Alt 		} else {
1789e69d7d0SLori Alt 			/* not a continuation.  Push out pending record */
179*98110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
180be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
1814e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
1829e69d7d0SLori Alt 		}
1839e69d7d0SLori Alt 	}
1849e69d7d0SLori Alt 	/* create a FREE record and make it pending */
1854e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
1864e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREE;
1879e69d7d0SLori Alt 	drrf->drr_object = object;
1889e69d7d0SLori Alt 	drrf->drr_offset = offset;
1899e69d7d0SLori Alt 	drrf->drr_length = length;
1904e3c9f44SBill Pijewski 	drrf->drr_toguid = dsp->dsa_toguid;
1919e69d7d0SLori Alt 	if (length == -1ULL) {
192*98110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
193be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1949e69d7d0SLori Alt 	} else {
1954e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_FREE;
1969e69d7d0SLori Alt 	}
197efb80947Sahrens 
198efb80947Sahrens 	return (0);
199efb80947Sahrens }
200efb80947Sahrens 
201efb80947Sahrens static int
2025d7b4d43SMatthew Ahrens dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
2038e714474SLori Alt     uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
204efb80947Sahrens {
2054e3c9f44SBill Pijewski 	struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
2069e69d7d0SLori Alt 
2072f3d8780SMatthew Ahrens 	/*
2082f3d8780SMatthew Ahrens 	 * We send data in increasing object, offset order.
2092f3d8780SMatthew Ahrens 	 * See comment in dump_free() for details.
2102f3d8780SMatthew Ahrens 	 */
2112f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
2122f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
2132f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
2142f3d8780SMatthew Ahrens 	dsp->dsa_last_data_object = object;
2152f3d8780SMatthew Ahrens 	dsp->dsa_last_data_offset = offset + blksz - 1;
2168e714474SLori Alt 
2179e69d7d0SLori Alt 	/*
2189e69d7d0SLori Alt 	 * If there is any kind of pending aggregation (currently either
2199e69d7d0SLori Alt 	 * a grouping of free objects or free blocks), push it out to
2209e69d7d0SLori Alt 	 * the stream, since aggregation can't be done across operations
2219e69d7d0SLori Alt 	 * of different types.
2229e69d7d0SLori Alt 	 */
2234e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
224*98110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
225be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2264e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2279e69d7d0SLori Alt 	}
228*98110f08SMatthew Ahrens 	/* write a WRITE record */
2294e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2304e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_WRITE;
2319e69d7d0SLori Alt 	drrw->drr_object = object;
2329e69d7d0SLori Alt 	drrw->drr_type = type;
2339e69d7d0SLori Alt 	drrw->drr_offset = offset;
2349e69d7d0SLori Alt 	drrw->drr_length = blksz;
2354e3c9f44SBill Pijewski 	drrw->drr_toguid = dsp->dsa_toguid;
236b5152584SMatthew Ahrens 	if (bp == NULL || BP_IS_EMBEDDED(bp)) {
2375d7b4d43SMatthew Ahrens 		/*
238b5152584SMatthew Ahrens 		 * There's no pre-computed checksum for partial-block
239b5152584SMatthew Ahrens 		 * writes or embedded BP's, so (like
240b5152584SMatthew Ahrens 		 * fletcher4-checkummed blocks) userland will have to
241b5152584SMatthew Ahrens 		 * compute a dedup-capable checksum itself.
2425d7b4d43SMatthew Ahrens 		 */
2435d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
2445d7b4d43SMatthew Ahrens 	} else {
2455d7b4d43SMatthew Ahrens 		drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
2465d7b4d43SMatthew Ahrens 		if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup)
2475d7b4d43SMatthew Ahrens 			drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
2485d7b4d43SMatthew Ahrens 		DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
2495d7b4d43SMatthew Ahrens 		DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
2505d7b4d43SMatthew Ahrens 		DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
2515d7b4d43SMatthew Ahrens 		drrw->drr_key.ddk_cksum = bp->blk_cksum;
2525d7b4d43SMatthew Ahrens 	}
253efb80947Sahrens 
254*98110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
255be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
256efb80947Sahrens 	return (0);
257efb80947Sahrens }
258efb80947Sahrens 
2595d7b4d43SMatthew Ahrens static int
2605d7b4d43SMatthew Ahrens dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
2615d7b4d43SMatthew Ahrens     int blksz, const blkptr_t *bp)
2625d7b4d43SMatthew Ahrens {
2635d7b4d43SMatthew Ahrens 	char buf[BPE_PAYLOAD_SIZE];
2645d7b4d43SMatthew Ahrens 	struct drr_write_embedded *drrw =
2655d7b4d43SMatthew Ahrens 	    &(dsp->dsa_drr->drr_u.drr_write_embedded);
2665d7b4d43SMatthew Ahrens 
2675d7b4d43SMatthew Ahrens 	if (dsp->dsa_pending_op != PENDING_NONE) {
268*98110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
2695d7b4d43SMatthew Ahrens 			return (EINTR);
2705d7b4d43SMatthew Ahrens 		dsp->dsa_pending_op = PENDING_NONE;
2715d7b4d43SMatthew Ahrens 	}
2725d7b4d43SMatthew Ahrens 
2735d7b4d43SMatthew Ahrens 	ASSERT(BP_IS_EMBEDDED(bp));
2745d7b4d43SMatthew Ahrens 
2755d7b4d43SMatthew Ahrens 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2765d7b4d43SMatthew Ahrens 	dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
2775d7b4d43SMatthew Ahrens 	drrw->drr_object = object;
2785d7b4d43SMatthew Ahrens 	drrw->drr_offset = offset;
2795d7b4d43SMatthew Ahrens 	drrw->drr_length = blksz;
2805d7b4d43SMatthew Ahrens 	drrw->drr_toguid = dsp->dsa_toguid;
2815d7b4d43SMatthew Ahrens 	drrw->drr_compression = BP_GET_COMPRESS(bp);
2825d7b4d43SMatthew Ahrens 	drrw->drr_etype = BPE_GET_ETYPE(bp);
2835d7b4d43SMatthew Ahrens 	drrw->drr_lsize = BPE_GET_LSIZE(bp);
2845d7b4d43SMatthew Ahrens 	drrw->drr_psize = BPE_GET_PSIZE(bp);
2855d7b4d43SMatthew Ahrens 
2865d7b4d43SMatthew Ahrens 	decode_embedded_bp_compressed(bp, buf);
2875d7b4d43SMatthew Ahrens 
288*98110f08SMatthew Ahrens 	if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
2895d7b4d43SMatthew Ahrens 		return (EINTR);
2905d7b4d43SMatthew Ahrens 	return (0);
2915d7b4d43SMatthew Ahrens }
2925d7b4d43SMatthew Ahrens 
2930a586ceaSMark Shellenbaum static int
2944e3c9f44SBill Pijewski dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
2950a586ceaSMark Shellenbaum {
2964e3c9f44SBill Pijewski 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
2970a586ceaSMark Shellenbaum 
2984e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
299*98110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
300be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3014e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3020a586ceaSMark Shellenbaum 	}
3030a586ceaSMark Shellenbaum 
3040a586ceaSMark Shellenbaum 	/* write a SPILL record */
3054e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3064e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_SPILL;
3070a586ceaSMark Shellenbaum 	drrs->drr_object = object;
3080a586ceaSMark Shellenbaum 	drrs->drr_length = blksz;
3094e3c9f44SBill Pijewski 	drrs->drr_toguid = dsp->dsa_toguid;
3100a586ceaSMark Shellenbaum 
311*98110f08SMatthew Ahrens 	if (dump_record(dsp, data, blksz) != 0)
312be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
3130a586ceaSMark Shellenbaum 	return (0);
3140a586ceaSMark Shellenbaum }
3150a586ceaSMark Shellenbaum 
316efb80947Sahrens static int
3174e3c9f44SBill Pijewski dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
318efb80947Sahrens {
3194e3c9f44SBill Pijewski 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
3209e69d7d0SLori Alt 
3212f3d8780SMatthew Ahrens 	/* See comment in dump_free(). */
3222f3d8780SMatthew Ahrens 	if (!dsp->dsa_incremental)
3232f3d8780SMatthew Ahrens 		return (0);
3242f3d8780SMatthew Ahrens 
3259e69d7d0SLori Alt 	/*
3269e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
3279e69d7d0SLori Alt 	 * push it out, since free block aggregation can only be done for
3289e69d7d0SLori Alt 	 * blocks of the same type (i.e., DRR_FREE records can only be
3299e69d7d0SLori Alt 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
3309e69d7d0SLori Alt 	 * can only be aggregated with other DRR_FREEOBJECTS records.
3319e69d7d0SLori Alt 	 */
3324e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
3334e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
334*98110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
335be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3364e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3379e69d7d0SLori Alt 	}
3384e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
3399e69d7d0SLori Alt 		/*
3409e69d7d0SLori Alt 		 * See whether this free object array can be aggregated
3419e69d7d0SLori Alt 		 * with pending one
3429e69d7d0SLori Alt 		 */
3439e69d7d0SLori Alt 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
3449e69d7d0SLori Alt 			drrfo->drr_numobjs += numobjs;
3459e69d7d0SLori Alt 			return (0);
3469e69d7d0SLori Alt 		} else {
3479e69d7d0SLori Alt 			/* can't be aggregated.  Push out pending record */
348*98110f08SMatthew Ahrens 			if (dump_record(dsp, NULL, 0) != 0)
349be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
3504e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
3519e69d7d0SLori Alt 		}
3529e69d7d0SLori Alt 	}
3539e69d7d0SLori Alt 
354efb80947Sahrens 	/* write a FREEOBJECTS record */
3554e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3564e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
3579e69d7d0SLori Alt 	drrfo->drr_firstobj = firstobj;
3589e69d7d0SLori Alt 	drrfo->drr_numobjs = numobjs;
3594e3c9f44SBill Pijewski 	drrfo->drr_toguid = dsp->dsa_toguid;
3609e69d7d0SLori Alt 
3614e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
362efb80947Sahrens 
363efb80947Sahrens 	return (0);
364efb80947Sahrens }
365efb80947Sahrens 
366efb80947Sahrens static int
3674e3c9f44SBill Pijewski dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
368efb80947Sahrens {
3694e3c9f44SBill Pijewski 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
3709e69d7d0SLori Alt 
371efb80947Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
3724e3c9f44SBill Pijewski 		return (dump_freeobjects(dsp, object, 1));
373efb80947Sahrens 
3744e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
375*98110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
376be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3774e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3789e69d7d0SLori Alt 	}
3799e69d7d0SLori Alt 
380efb80947Sahrens 	/* write an OBJECT record */
3814e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3824e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_OBJECT;
3839e69d7d0SLori Alt 	drro->drr_object = object;
3849e69d7d0SLori Alt 	drro->drr_type = dnp->dn_type;
3859e69d7d0SLori Alt 	drro->drr_bonustype = dnp->dn_bonustype;
3869e69d7d0SLori Alt 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3879e69d7d0SLori Alt 	drro->drr_bonuslen = dnp->dn_bonuslen;
3889e69d7d0SLori Alt 	drro->drr_checksumtype = dnp->dn_checksum;
3899e69d7d0SLori Alt 	drro->drr_compress = dnp->dn_compress;
3904e3c9f44SBill Pijewski 	drro->drr_toguid = dsp->dsa_toguid;
3919e69d7d0SLori Alt 
392b5152584SMatthew Ahrens 	if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
393b5152584SMatthew Ahrens 	    drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
394b5152584SMatthew Ahrens 		drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
395b5152584SMatthew Ahrens 
396*98110f08SMatthew Ahrens 	if (dump_record(dsp, DN_BONUS(dnp),
397*98110f08SMatthew Ahrens 	    P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
398be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
399*98110f08SMatthew Ahrens 	}
400efb80947Sahrens 
4012f3d8780SMatthew Ahrens 	/* Free anything past the end of the file. */
4024e3c9f44SBill Pijewski 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
4032f3d8780SMatthew Ahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
404be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
4053b2aab18SMatthew Ahrens 	if (dsp->dsa_err != 0)
406be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
407efb80947Sahrens 	return (0);
408efb80947Sahrens }
409efb80947Sahrens 
4105d7b4d43SMatthew Ahrens static boolean_t
4115d7b4d43SMatthew Ahrens backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
4125d7b4d43SMatthew Ahrens {
4135d7b4d43SMatthew Ahrens 	if (!BP_IS_EMBEDDED(bp))
4145d7b4d43SMatthew Ahrens 		return (B_FALSE);
4155d7b4d43SMatthew Ahrens 
4165d7b4d43SMatthew Ahrens 	/*
4175d7b4d43SMatthew Ahrens 	 * Compression function must be legacy, or explicitly enabled.
4185d7b4d43SMatthew Ahrens 	 */
4195d7b4d43SMatthew Ahrens 	if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
4205d7b4d43SMatthew Ahrens 	    !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
4215d7b4d43SMatthew Ahrens 		return (B_FALSE);
4225d7b4d43SMatthew Ahrens 
4235d7b4d43SMatthew Ahrens 	/*
4245d7b4d43SMatthew Ahrens 	 * Embed type must be explicitly enabled.
4255d7b4d43SMatthew Ahrens 	 */
4265d7b4d43SMatthew Ahrens 	switch (BPE_GET_ETYPE(bp)) {
4275d7b4d43SMatthew Ahrens 	case BP_EMBEDDED_TYPE_DATA:
4285d7b4d43SMatthew Ahrens 		if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
4295d7b4d43SMatthew Ahrens 			return (B_TRUE);
4305d7b4d43SMatthew Ahrens 		break;
4315d7b4d43SMatthew Ahrens 	default:
4325d7b4d43SMatthew Ahrens 		return (B_FALSE);
4335d7b4d43SMatthew Ahrens 	}
4345d7b4d43SMatthew Ahrens 	return (B_FALSE);
4355d7b4d43SMatthew Ahrens }
4365d7b4d43SMatthew Ahrens 
437efb80947Sahrens #define	BP_SPAN(dnp, level) \
438efb80947Sahrens 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
439efb80947Sahrens 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
440efb80947Sahrens 
441b24ab676SJeff Bonwick /* ARGSUSED */
442efb80947Sahrens static int
4431b912ec7SGeorge Wilson backup_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
4447802d7bfSMatthew Ahrens     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
445efb80947Sahrens {
4464e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = arg;
447efb80947Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
448efb80947Sahrens 	int err = 0;
449efb80947Sahrens 
450efb80947Sahrens 	if (issig(JUSTLOOKING) && issig(FORREAL))
451be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
452efb80947Sahrens 
453b24ab676SJeff Bonwick 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
454b24ab676SJeff Bonwick 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
45514843421SMatthew Ahrens 		return (0);
45678f17100SMatthew Ahrens 	} else if (zb->zb_level == ZB_ZIL_LEVEL) {
45778f17100SMatthew Ahrens 		/*
45878f17100SMatthew Ahrens 		 * If we are sending a non-snapshot (which is allowed on
45978f17100SMatthew Ahrens 		 * read-only pools), it may have a ZIL, which must be ignored.
46078f17100SMatthew Ahrens 		 */
46178f17100SMatthew Ahrens 		return (0);
46243466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp) &&
46343466aaeSMax Grossman 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
46488b7b0f2SMatthew Ahrens 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
46588b7b0f2SMatthew Ahrens 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
4664e3c9f44SBill Pijewski 		err = dump_freeobjects(dsp, dnobj, span >> DNODE_SHIFT);
46743466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp)) {
46888b7b0f2SMatthew Ahrens 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
4694e3c9f44SBill Pijewski 		err = dump_free(dsp, zb->zb_object, zb->zb_blkid * span, span);
47088b7b0f2SMatthew Ahrens 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
47188b7b0f2SMatthew Ahrens 		return (0);
47288b7b0f2SMatthew Ahrens 	} else if (type == DMU_OT_DNODE) {
47388b7b0f2SMatthew Ahrens 		dnode_phys_t *blk;
474efb80947Sahrens 		int i;
475efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
4767adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
47788b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
478efb80947Sahrens 
4791b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
4801b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
4811b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
482be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
48388b7b0f2SMatthew Ahrens 
48488b7b0f2SMatthew Ahrens 		blk = abuf->b_data;
485efb80947Sahrens 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
48688b7b0f2SMatthew Ahrens 			uint64_t dnobj = (zb->zb_blkid <<
48788b7b0f2SMatthew Ahrens 			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
4884e3c9f44SBill Pijewski 			err = dump_dnode(dsp, dnobj, blk+i);
4893b2aab18SMatthew Ahrens 			if (err != 0)
490efb80947Sahrens 				break;
491efb80947Sahrens 		}
49288b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
4930a586ceaSMark Shellenbaum 	} else if (type == DMU_OT_SA) {
4947adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
4950a586ceaSMark Shellenbaum 		arc_buf_t *abuf;
4960a586ceaSMark Shellenbaum 		int blksz = BP_GET_LSIZE(bp);
4970a586ceaSMark Shellenbaum 
4981b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
4991b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
5001b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
501be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
5020a586ceaSMark Shellenbaum 
5034e3c9f44SBill Pijewski 		err = dump_spill(dsp, zb->zb_object, blksz, abuf->b_data);
5040a586ceaSMark Shellenbaum 		(void) arc_buf_remove_ref(abuf, &abuf);
5055d7b4d43SMatthew Ahrens 	} else if (backup_do_embed(dsp, bp)) {
5065d7b4d43SMatthew Ahrens 		/* it's an embedded level-0 block of a regular object */
5075d7b4d43SMatthew Ahrens 		int blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
5085d7b4d43SMatthew Ahrens 		err = dump_write_embedded(dsp, zb->zb_object,
5095d7b4d43SMatthew Ahrens 		    zb->zb_blkid * blksz, blksz, bp);
51088b7b0f2SMatthew Ahrens 	} else { /* it's a level-0 block of a regular object */
5117adb730bSGeorge Wilson 		arc_flags_t aflags = ARC_FLAG_WAIT;
51288b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
513efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
514b5152584SMatthew Ahrens 		uint64_t offset;
51588b7b0f2SMatthew Ahrens 
5165d7b4d43SMatthew Ahrens 		ASSERT3U(blksz, ==, dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
51778f17100SMatthew Ahrens 		ASSERT0(zb->zb_level);
5181b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
5191b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
5201b912ec7SGeorge Wilson 		    &aflags, zb) != 0) {
52119b94df9SMatthew Ahrens 			if (zfs_send_corrupt_data) {
52219b94df9SMatthew Ahrens 				/* Send a block filled with 0x"zfs badd bloc" */
52319b94df9SMatthew Ahrens 				abuf = arc_buf_alloc(spa, blksz, &abuf,
52419b94df9SMatthew Ahrens 				    ARC_BUFC_DATA);
52519b94df9SMatthew Ahrens 				uint64_t *ptr;
52619b94df9SMatthew Ahrens 				for (ptr = abuf->b_data;
52719b94df9SMatthew Ahrens 				    (char *)ptr < (char *)abuf->b_data + blksz;
52819b94df9SMatthew Ahrens 				    ptr++)
5298c76e076SBrian Behlendorf 					*ptr = 0x2f5baddb10cULL;
53019b94df9SMatthew Ahrens 			} else {
531be6fd75aSMatthew Ahrens 				return (SET_ERROR(EIO));
53219b94df9SMatthew Ahrens 			}
53319b94df9SMatthew Ahrens 		}
53488b7b0f2SMatthew Ahrens 
535b5152584SMatthew Ahrens 		offset = zb->zb_blkid * blksz;
536b5152584SMatthew Ahrens 
537b5152584SMatthew Ahrens 		if (!(dsp->dsa_featureflags &
538b5152584SMatthew Ahrens 		    DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
539b5152584SMatthew Ahrens 		    blksz > SPA_OLD_MAXBLOCKSIZE) {
540b5152584SMatthew Ahrens 			char *buf = abuf->b_data;
541b5152584SMatthew Ahrens 			while (blksz > 0 && err == 0) {
542b5152584SMatthew Ahrens 				int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
543b5152584SMatthew Ahrens 				err = dump_write(dsp, type, zb->zb_object,
544b5152584SMatthew Ahrens 				    offset, n, NULL, buf);
545b5152584SMatthew Ahrens 				offset += n;
546b5152584SMatthew Ahrens 				buf += n;
547b5152584SMatthew Ahrens 				blksz -= n;
548b5152584SMatthew Ahrens 			}
549b5152584SMatthew Ahrens 		} else {
550b5152584SMatthew Ahrens 			err = dump_write(dsp, type, zb->zb_object,
551b5152584SMatthew Ahrens 			    offset, blksz, bp, abuf->b_data);
552b5152584SMatthew Ahrens 		}
55388b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
554efb80947Sahrens 	}
555efb80947Sahrens 
556efb80947Sahrens 	ASSERT(err == 0 || err == EINTR);
557efb80947Sahrens 	return (err);
558efb80947Sahrens }
559efb80947Sahrens 
5604445fffbSMatthew Ahrens /*
56178f17100SMatthew Ahrens  * Releases dp using the specified tag.
5624445fffbSMatthew Ahrens  */
5633b2aab18SMatthew Ahrens static int
5643b2aab18SMatthew Ahrens dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *ds,
5655d7b4d43SMatthew Ahrens     zfs_bookmark_phys_t *fromzb, boolean_t is_clone, boolean_t embedok,
566b5152584SMatthew Ahrens     boolean_t large_block_ok, int outfd, vnode_t *vp, offset_t *off)
567efb80947Sahrens {
5683b2aab18SMatthew Ahrens 	objset_t *os;
569efb80947Sahrens 	dmu_replay_record_t *drr;
5704e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp;
571efb80947Sahrens 	int err;
5723cb34c60Sahrens 	uint64_t fromtxg = 0;
5735d7b4d43SMatthew Ahrens 	uint64_t featureflags = 0;
574efb80947Sahrens 
5753b2aab18SMatthew Ahrens 	err = dmu_objset_from_ds(ds, &os);
5763b2aab18SMatthew Ahrens 	if (err != 0) {
5773b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
5783b2aab18SMatthew Ahrens 		return (err);
5793b2aab18SMatthew Ahrens 	}
580efb80947Sahrens 
581efb80947Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
582efb80947Sahrens 	drr->drr_type = DRR_BEGIN;
583efb80947Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
5849e69d7d0SLori Alt 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
5859e69d7d0SLori Alt 	    DMU_SUBSTREAM);
586dc7cd546SMark Shellenbaum 
587dc7cd546SMark Shellenbaum #ifdef _KERNEL
5883b2aab18SMatthew Ahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
589dc7cd546SMark Shellenbaum 		uint64_t version;
5903b2aab18SMatthew Ahrens 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
5914e3c9f44SBill Pijewski 			kmem_free(drr, sizeof (dmu_replay_record_t));
5923b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, tag);
593be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
5944e3c9f44SBill Pijewski 		}
5953b2aab18SMatthew Ahrens 		if (version >= ZPL_VERSION_SA) {
5965d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
597dc7cd546SMark Shellenbaum 		}
598dc7cd546SMark Shellenbaum 	}
599dc7cd546SMark Shellenbaum #endif
600dc7cd546SMark Shellenbaum 
601b5152584SMatthew Ahrens 	if (large_block_ok && ds->ds_large_blocks)
602b5152584SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
6035d7b4d43SMatthew Ahrens 	if (embedok &&
6045d7b4d43SMatthew Ahrens 	    spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
6055d7b4d43SMatthew Ahrens 		featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
6065d7b4d43SMatthew Ahrens 		if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
6075d7b4d43SMatthew Ahrens 			featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
6085d7b4d43SMatthew Ahrens 	} else {
6095d7b4d43SMatthew Ahrens 		embedok = B_FALSE;
6105d7b4d43SMatthew Ahrens 	}
6115d7b4d43SMatthew Ahrens 
6125d7b4d43SMatthew Ahrens 	DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
6135d7b4d43SMatthew Ahrens 	    featureflags);
6145d7b4d43SMatthew Ahrens 
615efb80947Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
616c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_creation_time;
6173b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
61878f17100SMatthew Ahrens 	if (is_clone)
6193cb34c60Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
620c1379625SJustin T. Gibbs 	drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(ds)->ds_guid;
621c1379625SJustin T. Gibbs 	if (dsl_dataset_phys(ds)->ds_flags & DS_FLAG_CI_DATASET)
622ab04eb8eStimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
623ab04eb8eStimh 
62478f17100SMatthew Ahrens 	if (fromzb != NULL) {
62578f17100SMatthew Ahrens 		drr->drr_u.drr_begin.drr_fromguid = fromzb->zbm_guid;
62678f17100SMatthew Ahrens 		fromtxg = fromzb->zbm_creation_txg;
62778f17100SMatthew Ahrens 	}
628efb80947Sahrens 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
629bc9014e6SJustin Gibbs 	if (!ds->ds_is_snapshot) {
63078f17100SMatthew Ahrens 		(void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
63178f17100SMatthew Ahrens 		    sizeof (drr->drr_u.drr_begin.drr_toname));
6323b2aab18SMatthew Ahrens 	}
6333cb34c60Sahrens 
6344e3c9f44SBill Pijewski 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
6354e3c9f44SBill Pijewski 
6364e3c9f44SBill Pijewski 	dsp->dsa_drr = drr;
6374e3c9f44SBill Pijewski 	dsp->dsa_vp = vp;
6384e3c9f44SBill Pijewski 	dsp->dsa_outfd = outfd;
6394e3c9f44SBill Pijewski 	dsp->dsa_proc = curproc;
6403b2aab18SMatthew Ahrens 	dsp->dsa_os = os;
6414e3c9f44SBill Pijewski 	dsp->dsa_off = off;
642c1379625SJustin T. Gibbs 	dsp->dsa_toguid = dsl_dataset_phys(ds)->ds_guid;
6434e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_NONE;
64478f17100SMatthew Ahrens 	dsp->dsa_incremental = (fromzb != NULL);
6455d7b4d43SMatthew Ahrens 	dsp->dsa_featureflags = featureflags;
6464e3c9f44SBill Pijewski 
6474e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
6484e3c9f44SBill Pijewski 	list_insert_head(&ds->ds_sendstreams, dsp);
6494e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
6504e3c9f44SBill Pijewski 
651de8d9cffSMatthew Ahrens 	dsl_dataset_long_hold(ds, FTAG);
652de8d9cffSMatthew Ahrens 	dsl_pool_rele(dp, tag);
653de8d9cffSMatthew Ahrens 
654*98110f08SMatthew Ahrens 	if (dump_record(dsp, NULL, 0) != 0) {
6554e3c9f44SBill Pijewski 		err = dsp->dsa_err;
6564e3c9f44SBill Pijewski 		goto out;
657efb80947Sahrens 	}
658efb80947Sahrens 
65988b7b0f2SMatthew Ahrens 	err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH,
6604e3c9f44SBill Pijewski 	    backup_cb, dsp);
661efb80947Sahrens 
6624e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE)
663*98110f08SMatthew Ahrens 		if (dump_record(dsp, NULL, 0) != 0)
664be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINTR);
6659e69d7d0SLori Alt 
6663b2aab18SMatthew Ahrens 	if (err != 0) {
6673b2aab18SMatthew Ahrens 		if (err == EINTR && dsp->dsa_err != 0)
6684e3c9f44SBill Pijewski 			err = dsp->dsa_err;
6694e3c9f44SBill Pijewski 		goto out;
670efb80947Sahrens 	}
671efb80947Sahrens 
672efb80947Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
673efb80947Sahrens 	drr->drr_type = DRR_END;
6744e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
6754e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
676efb80947Sahrens 
677*98110f08SMatthew Ahrens 	if (dump_record(dsp, NULL, 0) != 0) {
6784e3c9f44SBill Pijewski 		err = dsp->dsa_err;
6794e3c9f44SBill Pijewski 		goto out;
6807b5309bbSgw 	}
681efb80947Sahrens 
6824e3c9f44SBill Pijewski out:
6834e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
6844e3c9f44SBill Pijewski 	list_remove(&ds->ds_sendstreams, dsp);
6854e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
6864e3c9f44SBill Pijewski 
687efb80947Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
6884e3c9f44SBill Pijewski 	kmem_free(dsp, sizeof (dmu_sendarg_t));
689efb80947Sahrens 
6903b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, FTAG);
6913b2aab18SMatthew Ahrens 
6924e3c9f44SBill Pijewski 	return (err);
693efb80947Sahrens }
694efb80947Sahrens 
69519b94df9SMatthew Ahrens int
6963b2aab18SMatthew Ahrens dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
697b5152584SMatthew Ahrens     boolean_t embedok, boolean_t large_block_ok,
698b5152584SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
6993b2aab18SMatthew Ahrens {
7003b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
7013b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
7023b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
7033b2aab18SMatthew Ahrens 	int err;
7043b2aab18SMatthew Ahrens 
7053b2aab18SMatthew Ahrens 	err = dsl_pool_hold(pool, FTAG, &dp);
7063b2aab18SMatthew Ahrens 	if (err != 0)
7073b2aab18SMatthew Ahrens 		return (err);
7083b2aab18SMatthew Ahrens 
7093b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
7103b2aab18SMatthew Ahrens 	if (err != 0) {
7113b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
7123b2aab18SMatthew Ahrens 		return (err);
7133b2aab18SMatthew Ahrens 	}
7143b2aab18SMatthew Ahrens 
7153b2aab18SMatthew Ahrens 	if (fromsnap != 0) {
71678f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
71778f17100SMatthew Ahrens 		boolean_t is_clone;
71878f17100SMatthew Ahrens 
7193b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
7203b2aab18SMatthew Ahrens 		if (err != 0) {
7213b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
7223b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
7233b2aab18SMatthew Ahrens 			return (err);
7243b2aab18SMatthew Ahrens 		}
72578f17100SMatthew Ahrens 		if (!dsl_dataset_is_before(ds, fromds, 0))
72678f17100SMatthew Ahrens 			err = SET_ERROR(EXDEV);
727c1379625SJustin T. Gibbs 		zb.zbm_creation_time =
728c1379625SJustin T. Gibbs 		    dsl_dataset_phys(fromds)->ds_creation_time;
729c1379625SJustin T. Gibbs 		zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
730c1379625SJustin T. Gibbs 		zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
73178f17100SMatthew Ahrens 		is_clone = (fromds->ds_dir != ds->ds_dir);
73278f17100SMatthew Ahrens 		dsl_dataset_rele(fromds, FTAG);
733b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
734b5152584SMatthew Ahrens 		    embedok, large_block_ok, outfd, vp, off);
73578f17100SMatthew Ahrens 	} else {
736b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
737b5152584SMatthew Ahrens 		    embedok, large_block_ok, outfd, vp, off);
7383b2aab18SMatthew Ahrens 	}
73978f17100SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
74078f17100SMatthew Ahrens 	return (err);
7413b2aab18SMatthew Ahrens }
7423b2aab18SMatthew Ahrens 
7433b2aab18SMatthew Ahrens int
744b5152584SMatthew Ahrens dmu_send(const char *tosnap, const char *fromsnap,
745b5152584SMatthew Ahrens     boolean_t embedok, boolean_t large_block_ok,
7463b2aab18SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
7473b2aab18SMatthew Ahrens {
7483b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
7493b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
7503b2aab18SMatthew Ahrens 	int err;
75178f17100SMatthew Ahrens 	boolean_t owned = B_FALSE;
7523b2aab18SMatthew Ahrens 
75378f17100SMatthew Ahrens 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
754be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7553b2aab18SMatthew Ahrens 
7563b2aab18SMatthew Ahrens 	err = dsl_pool_hold(tosnap, FTAG, &dp);
7573b2aab18SMatthew Ahrens 	if (err != 0)
7583b2aab18SMatthew Ahrens 		return (err);
7593b2aab18SMatthew Ahrens 
76078f17100SMatthew Ahrens 	if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
76178f17100SMatthew Ahrens 		/*
76278f17100SMatthew Ahrens 		 * We are sending a filesystem or volume.  Ensure
76378f17100SMatthew Ahrens 		 * that it doesn't change by owning the dataset.
76478f17100SMatthew Ahrens 		 */
76578f17100SMatthew Ahrens 		err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
76678f17100SMatthew Ahrens 		owned = B_TRUE;
76778f17100SMatthew Ahrens 	} else {
76878f17100SMatthew Ahrens 		err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
76978f17100SMatthew Ahrens 	}
7703b2aab18SMatthew Ahrens 	if (err != 0) {
7713b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
7723b2aab18SMatthew Ahrens 		return (err);
7733b2aab18SMatthew Ahrens 	}
7743b2aab18SMatthew Ahrens 
7753b2aab18SMatthew Ahrens 	if (fromsnap != NULL) {
77678f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
77778f17100SMatthew Ahrens 		boolean_t is_clone = B_FALSE;
77878f17100SMatthew Ahrens 		int fsnamelen = strchr(tosnap, '@') - tosnap;
77978f17100SMatthew Ahrens 
78078f17100SMatthew Ahrens 		/*
78178f17100SMatthew Ahrens 		 * If the fromsnap is in a different filesystem, then
78278f17100SMatthew Ahrens 		 * mark the send stream as a clone.
78378f17100SMatthew Ahrens 		 */
78478f17100SMatthew Ahrens 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
78578f17100SMatthew Ahrens 		    (fromsnap[fsnamelen] != '@' &&
78678f17100SMatthew Ahrens 		    fromsnap[fsnamelen] != '#')) {
78778f17100SMatthew Ahrens 			is_clone = B_TRUE;
78878f17100SMatthew Ahrens 		}
78978f17100SMatthew Ahrens 
79078f17100SMatthew Ahrens 		if (strchr(fromsnap, '@')) {
79178f17100SMatthew Ahrens 			dsl_dataset_t *fromds;
79278f17100SMatthew Ahrens 			err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
79378f17100SMatthew Ahrens 			if (err == 0) {
79478f17100SMatthew Ahrens 				if (!dsl_dataset_is_before(ds, fromds, 0))
79578f17100SMatthew Ahrens 					err = SET_ERROR(EXDEV);
79678f17100SMatthew Ahrens 				zb.zbm_creation_time =
797c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_time;
79878f17100SMatthew Ahrens 				zb.zbm_creation_txg =
799c1379625SJustin T. Gibbs 				    dsl_dataset_phys(fromds)->ds_creation_txg;
800c1379625SJustin T. Gibbs 				zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
80178f17100SMatthew Ahrens 				is_clone = (ds->ds_dir != fromds->ds_dir);
80278f17100SMatthew Ahrens 				dsl_dataset_rele(fromds, FTAG);
80378f17100SMatthew Ahrens 			}
80478f17100SMatthew Ahrens 		} else {
80578f17100SMatthew Ahrens 			err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
80678f17100SMatthew Ahrens 		}
8073b2aab18SMatthew Ahrens 		if (err != 0) {
8083b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
8093b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
8103b2aab18SMatthew Ahrens 			return (err);
8113b2aab18SMatthew Ahrens 		}
812b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
813b5152584SMatthew Ahrens 		    embedok, large_block_ok, outfd, vp, off);
81478f17100SMatthew Ahrens 	} else {
815b5152584SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
816b5152584SMatthew Ahrens 		    embedok, large_block_ok, outfd, vp, off);
8173b2aab18SMatthew Ahrens 	}
81878f17100SMatthew Ahrens 	if (owned)
81978f17100SMatthew Ahrens 		dsl_dataset_disown(ds, FTAG);
82078f17100SMatthew Ahrens 	else
82178f17100SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
82278f17100SMatthew Ahrens 	return (err);
8233b2aab18SMatthew Ahrens }
8243b2aab18SMatthew Ahrens 
825643da460SMax Grossman static int
826643da460SMax Grossman dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t size,
827643da460SMax Grossman     uint64_t *sizep)
828643da460SMax Grossman {
829643da460SMax Grossman 	int err;
830643da460SMax Grossman 	/*
831643da460SMax Grossman 	 * Assume that space (both on-disk and in-stream) is dominated by
832643da460SMax Grossman 	 * data.  We will adjust for indirect blocks and the copies property,
833643da460SMax Grossman 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
834643da460SMax Grossman 	 */
835643da460SMax Grossman 
836643da460SMax Grossman 	/*
837643da460SMax Grossman 	 * Subtract out approximate space used by indirect blocks.
838643da460SMax Grossman 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
839643da460SMax Grossman 	 * Assume all blocks are recordsize.  Assume ditto blocks and
840643da460SMax Grossman 	 * internal fragmentation counter out compression.
841643da460SMax Grossman 	 *
842643da460SMax Grossman 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
843643da460SMax Grossman 	 * block, which we observe in practice.
844643da460SMax Grossman 	 */
845643da460SMax Grossman 	uint64_t recordsize;
846643da460SMax Grossman 	err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
847643da460SMax Grossman 	if (err != 0)
848643da460SMax Grossman 		return (err);
849643da460SMax Grossman 	size -= size / recordsize * sizeof (blkptr_t);
850643da460SMax Grossman 
851643da460SMax Grossman 	/* Add in the space for the record associated with each block. */
852643da460SMax Grossman 	size += size / recordsize * sizeof (dmu_replay_record_t);
853643da460SMax Grossman 
854643da460SMax Grossman 	*sizep = size;
855643da460SMax Grossman 
856643da460SMax Grossman 	return (0);
857643da460SMax Grossman }
858643da460SMax Grossman 
8593b2aab18SMatthew Ahrens int
8603b2aab18SMatthew Ahrens dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
86119b94df9SMatthew Ahrens {
86219b94df9SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
86319b94df9SMatthew Ahrens 	int err;
86419b94df9SMatthew Ahrens 	uint64_t size;
86519b94df9SMatthew Ahrens 
8663b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
8673b2aab18SMatthew Ahrens 
86819b94df9SMatthew Ahrens 	/* tosnap must be a snapshot */
869bc9014e6SJustin Gibbs 	if (!ds->ds_is_snapshot)
870be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
87119b94df9SMatthew Ahrens 
8724445fffbSMatthew Ahrens 	/*
8734445fffbSMatthew Ahrens 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
8744445fffbSMatthew Ahrens 	 * or the origin's fs.
8754445fffbSMatthew Ahrens 	 */
87678f17100SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
877be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
87819b94df9SMatthew Ahrens 
87919b94df9SMatthew Ahrens 	/* Get uncompressed size estimate of changed data. */
88019b94df9SMatthew Ahrens 	if (fromds == NULL) {
881c1379625SJustin T. Gibbs 		size = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
88219b94df9SMatthew Ahrens 	} else {
88319b94df9SMatthew Ahrens 		uint64_t used, comp;
88419b94df9SMatthew Ahrens 		err = dsl_dataset_space_written(fromds, ds,
88519b94df9SMatthew Ahrens 		    &used, &comp, &size);
8863b2aab18SMatthew Ahrens 		if (err != 0)
88719b94df9SMatthew Ahrens 			return (err);
88819b94df9SMatthew Ahrens 	}
88919b94df9SMatthew Ahrens 
890643da460SMax Grossman 	err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
891643da460SMax Grossman 	return (err);
892643da460SMax Grossman }
893643da460SMax Grossman 
894643da460SMax Grossman /*
895643da460SMax Grossman  * Simple callback used to traverse the blocks of a snapshot and sum their
896643da460SMax Grossman  * uncompressed size
897643da460SMax Grossman  */
898643da460SMax Grossman /* ARGSUSED */
899643da460SMax Grossman static int
900643da460SMax Grossman dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
901643da460SMax Grossman     const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
902643da460SMax Grossman {
903643da460SMax Grossman 	uint64_t *spaceptr = arg;
904643da460SMax Grossman 	if (bp != NULL && !BP_IS_HOLE(bp)) {
905643da460SMax Grossman 		*spaceptr += BP_GET_UCSIZE(bp);
906643da460SMax Grossman 	}
907643da460SMax Grossman 	return (0);
908643da460SMax Grossman }
909643da460SMax Grossman 
910643da460SMax Grossman /*
911643da460SMax Grossman  * Given a desination snapshot and a TXG, calculate the approximate size of a
912643da460SMax Grossman  * send stream sent from that TXG. from_txg may be zero, indicating that the
913643da460SMax Grossman  * whole snapshot will be sent.
914643da460SMax Grossman  */
915643da460SMax Grossman int
916643da460SMax Grossman dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
917643da460SMax Grossman     uint64_t *sizep)
918643da460SMax Grossman {
919643da460SMax Grossman 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
920643da460SMax Grossman 	int err;
921643da460SMax Grossman 	uint64_t size = 0;
922643da460SMax Grossman 
923643da460SMax Grossman 	ASSERT(dsl_pool_config_held(dp));
924643da460SMax Grossman 
925643da460SMax Grossman 	/* tosnap must be a snapshot */
926643da460SMax Grossman 	if (!dsl_dataset_is_snapshot(ds))
927643da460SMax Grossman 		return (SET_ERROR(EINVAL));
928643da460SMax Grossman 
929643da460SMax Grossman 	/* verify that from_txg is before the provided snapshot was taken */
930643da460SMax Grossman 	if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
931643da460SMax Grossman 		return (SET_ERROR(EXDEV));
932643da460SMax Grossman 	}
93319b94df9SMatthew Ahrens 
93419b94df9SMatthew Ahrens 	/*
935643da460SMax Grossman 	 * traverse the blocks of the snapshot with birth times after
936643da460SMax Grossman 	 * from_txg, summing their uncompressed size
93719b94df9SMatthew Ahrens 	 */
938643da460SMax Grossman 	err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
939643da460SMax Grossman 	    dmu_calculate_send_traversal, &size);
940643da460SMax Grossman 	if (err)
94119b94df9SMatthew Ahrens 		return (err);
94219b94df9SMatthew Ahrens 
943643da460SMax Grossman 	err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
944643da460SMax Grossman 	return (err);
94519b94df9SMatthew Ahrens }
94619b94df9SMatthew Ahrens 
9473b2aab18SMatthew Ahrens typedef struct dmu_recv_begin_arg {
9483b2aab18SMatthew Ahrens 	const char *drba_origin;
9493b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drba_cookie;
9503b2aab18SMatthew Ahrens 	cred_t *drba_cred;
95134f2f8cfSMatthew Ahrens 	uint64_t drba_snapobj;
9523b2aab18SMatthew Ahrens } dmu_recv_begin_arg_t;
953f18faf3fSek 
954f18faf3fSek static int
9553b2aab18SMatthew Ahrens recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
9563b2aab18SMatthew Ahrens     uint64_t fromguid)
957f18faf3fSek {
9583cb34c60Sahrens 	uint64_t val;
9593b2aab18SMatthew Ahrens 	int error;
9603b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
961f18faf3fSek 
9623b2aab18SMatthew Ahrens 	/* temporary clone name must not exist */
9633b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
964c1379625SJustin T. Gibbs 	    dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
9653b2aab18SMatthew Ahrens 	    8, 1, &val);
9663b2aab18SMatthew Ahrens 	if (error != ENOENT)
9673b2aab18SMatthew Ahrens 		return (error == 0 ? EBUSY : error);
9683b2aab18SMatthew Ahrens 
969feaa74e4SMark Maybee 	/* new snapshot name must not exist */
9703b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
971c1379625SJustin T. Gibbs 	    dsl_dataset_phys(ds)->ds_snapnames_zapobj,
972c1379625SJustin T. Gibbs 	    drba->drba_cookie->drc_tosnap, 8, 1, &val);
9733b2aab18SMatthew Ahrens 	if (error != ENOENT)
9743b2aab18SMatthew Ahrens 		return (error == 0 ? EEXIST : error);
975feaa74e4SMark Maybee 
976a2afb611SJerry Jelinek 	/*
977a2afb611SJerry Jelinek 	 * Check snapshot limit before receiving. We'll recheck again at the
978a2afb611SJerry Jelinek 	 * end, but might as well abort before receiving if we're already over
979a2afb611SJerry Jelinek 	 * the limit.
980a2afb611SJerry Jelinek 	 *
981a2afb611SJerry Jelinek 	 * Note that we do not check the file system limit with
982a2afb611SJerry Jelinek 	 * dsl_dir_fscount_check because the temporary %clones don't count
983a2afb611SJerry Jelinek 	 * against that limit.
984a2afb611SJerry Jelinek 	 */
985a2afb611SJerry Jelinek 	error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
986a2afb611SJerry Jelinek 	    NULL, drba->drba_cred);
987a2afb611SJerry Jelinek 	if (error != 0)
988a2afb611SJerry Jelinek 		return (error);
989a2afb611SJerry Jelinek 
9903b2aab18SMatthew Ahrens 	if (fromguid != 0) {
99134f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap;
992c1379625SJustin T. Gibbs 		uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
99334f2f8cfSMatthew Ahrens 
99434f2f8cfSMatthew Ahrens 		/* Find snapshot in this dir that matches fromguid. */
99534f2f8cfSMatthew Ahrens 		while (obj != 0) {
99634f2f8cfSMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, obj, FTAG,
99734f2f8cfSMatthew Ahrens 			    &snap);
99834f2f8cfSMatthew Ahrens 			if (error != 0)
99934f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
100034f2f8cfSMatthew Ahrens 			if (snap->ds_dir != ds->ds_dir) {
100134f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
100234f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
100334f2f8cfSMatthew Ahrens 			}
1004c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(snap)->ds_guid == fromguid)
100534f2f8cfSMatthew Ahrens 				break;
1006c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
100734f2f8cfSMatthew Ahrens 			dsl_dataset_rele(snap, FTAG);
100834f2f8cfSMatthew Ahrens 		}
100934f2f8cfSMatthew Ahrens 		if (obj == 0)
1010be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
101192241e0bSTom Erickson 
101234f2f8cfSMatthew Ahrens 		if (drba->drba_cookie->drc_force) {
101334f2f8cfSMatthew Ahrens 			drba->drba_snapobj = obj;
101434f2f8cfSMatthew Ahrens 		} else {
101534f2f8cfSMatthew Ahrens 			/*
101634f2f8cfSMatthew Ahrens 			 * If we are not forcing, there must be no
101734f2f8cfSMatthew Ahrens 			 * changes since fromsnap.
101834f2f8cfSMatthew Ahrens 			 */
101934f2f8cfSMatthew Ahrens 			if (dsl_dataset_modified_since_snap(ds, snap)) {
102092241e0bSTom Erickson 				dsl_dataset_rele(snap, FTAG);
102134f2f8cfSMatthew Ahrens 				return (SET_ERROR(ETXTBSY));
102292241e0bSTom Erickson 			}
102334f2f8cfSMatthew Ahrens 			drba->drba_snapobj = ds->ds_prev->ds_object;
102492241e0bSTom Erickson 		}
102534f2f8cfSMatthew Ahrens 
102634f2f8cfSMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
1027ae46e4c7SMatthew Ahrens 	} else {
1028ae46e4c7SMatthew Ahrens 		/* if full, most recent snapshot must be $ORIGIN */
1029c1379625SJustin T. Gibbs 		if (dsl_dataset_phys(ds)->ds_prev_snap_txg >= TXG_INITIAL)
1030be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
1031c1379625SJustin T. Gibbs 		drba->drba_snapobj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1032ae46e4c7SMatthew Ahrens 	}
10333cb34c60Sahrens 
10343cb34c60Sahrens 	return (0);
10353b2aab18SMatthew Ahrens 
10363b2aab18SMatthew Ahrens }
10373b2aab18SMatthew Ahrens 
10383b2aab18SMatthew Ahrens static int
10393b2aab18SMatthew Ahrens dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
10403b2aab18SMatthew Ahrens {
10413b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
10423b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
10433b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
10443b2aab18SMatthew Ahrens 	uint64_t fromguid = drrb->drr_fromguid;
10453b2aab18SMatthew Ahrens 	int flags = drrb->drr_flags;
10463b2aab18SMatthew Ahrens 	int error;
10475d7b4d43SMatthew Ahrens 	uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
10483b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
10493b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
10503b2aab18SMatthew Ahrens 
10513b2aab18SMatthew Ahrens 	/* already checked */
10523b2aab18SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
10533b2aab18SMatthew Ahrens 
10543b2aab18SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
10553b2aab18SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
10563b2aab18SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
10573b2aab18SMatthew Ahrens 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1058be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
10593b2aab18SMatthew Ahrens 
10603b2aab18SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
10615d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
10625d7b4d43SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA)
10635d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
10645d7b4d43SMatthew Ahrens 
10655d7b4d43SMatthew Ahrens 	/*
10665d7b4d43SMatthew Ahrens 	 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
10675d7b4d43SMatthew Ahrens 	 * record to a plan WRITE record, so the pool must have the
10685d7b4d43SMatthew Ahrens 	 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
10695d7b4d43SMatthew Ahrens 	 * records.  Same with WRITE_EMBEDDED records that use LZ4 compression.
10705d7b4d43SMatthew Ahrens 	 */
10715d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
10725d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
10735d7b4d43SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
10745d7b4d43SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
10755d7b4d43SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1076be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
10773b2aab18SMatthew Ahrens 
1078b5152584SMatthew Ahrens 	/*
1079b5152584SMatthew Ahrens 	 * The receiving code doesn't know how to translate large blocks
1080b5152584SMatthew Ahrens 	 * to smaller ones, so the pool must have the LARGE_BLOCKS
1081b5152584SMatthew Ahrens 	 * feature enabled if the stream has LARGE_BLOCKS.
1082b5152584SMatthew Ahrens 	 */
1083b5152584SMatthew Ahrens 	if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1084b5152584SMatthew Ahrens 	    !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1085b5152584SMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
1086b5152584SMatthew Ahrens 
10873b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
10883b2aab18SMatthew Ahrens 	if (error == 0) {
10893b2aab18SMatthew Ahrens 		/* target fs already exists; recv into temp clone */
10903b2aab18SMatthew Ahrens 
10913b2aab18SMatthew Ahrens 		/* Can't recv a clone into an existing fs */
10923b2aab18SMatthew Ahrens 		if (flags & DRR_FLAG_CLONE) {
10933b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
1094be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
10953b2aab18SMatthew Ahrens 		}
10963b2aab18SMatthew Ahrens 
10973b2aab18SMatthew Ahrens 		error = recv_begin_check_existing_impl(drba, ds, fromguid);
10983b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
10993b2aab18SMatthew Ahrens 	} else if (error == ENOENT) {
11003b2aab18SMatthew Ahrens 		/* target fs does not exist; must be a full backup or clone */
11013b2aab18SMatthew Ahrens 		char buf[MAXNAMELEN];
11023b2aab18SMatthew Ahrens 
11033b2aab18SMatthew Ahrens 		/*
11043b2aab18SMatthew Ahrens 		 * If it's a non-clone incremental, we are missing the
11053b2aab18SMatthew Ahrens 		 * target fs, so fail the recv.
11063b2aab18SMatthew Ahrens 		 */
11073b2aab18SMatthew Ahrens 		if (fromguid != 0 && !(flags & DRR_FLAG_CLONE))
1108be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
11093b2aab18SMatthew Ahrens 
11103b2aab18SMatthew Ahrens 		/* Open the parent of tofs */
11113b2aab18SMatthew Ahrens 		ASSERT3U(strlen(tofs), <, MAXNAMELEN);
11123b2aab18SMatthew Ahrens 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
11133b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
11143b2aab18SMatthew Ahrens 		if (error != 0)
11153b2aab18SMatthew Ahrens 			return (error);
11163b2aab18SMatthew Ahrens 
1117a2afb611SJerry Jelinek 		/*
1118a2afb611SJerry Jelinek 		 * Check filesystem and snapshot limits before receiving. We'll
1119a2afb611SJerry Jelinek 		 * recheck snapshot limits again at the end (we create the
1120a2afb611SJerry Jelinek 		 * filesystems and increment those counts during begin_sync).
1121a2afb611SJerry Jelinek 		 */
1122a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1123a2afb611SJerry Jelinek 		    ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1124a2afb611SJerry Jelinek 		if (error != 0) {
1125a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1126a2afb611SJerry Jelinek 			return (error);
1127a2afb611SJerry Jelinek 		}
1128a2afb611SJerry Jelinek 
1129a2afb611SJerry Jelinek 		error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1130a2afb611SJerry Jelinek 		    ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1131a2afb611SJerry Jelinek 		if (error != 0) {
1132a2afb611SJerry Jelinek 			dsl_dataset_rele(ds, FTAG);
1133a2afb611SJerry Jelinek 			return (error);
1134a2afb611SJerry Jelinek 		}
1135a2afb611SJerry Jelinek 
11363b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
11373b2aab18SMatthew Ahrens 			dsl_dataset_t *origin;
11383b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, drba->drba_origin,
11393b2aab18SMatthew Ahrens 			    FTAG, &origin);
11403b2aab18SMatthew Ahrens 			if (error != 0) {
11413b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
11423b2aab18SMatthew Ahrens 				return (error);
11433b2aab18SMatthew Ahrens 			}
1144bc9014e6SJustin Gibbs 			if (!origin->ds_is_snapshot) {
11453b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
11463b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1147be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
11483b2aab18SMatthew Ahrens 			}
1149c1379625SJustin T. Gibbs 			if (dsl_dataset_phys(origin)->ds_guid != fromguid) {
11503b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
11513b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
1152be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
11533b2aab18SMatthew Ahrens 			}
11543b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
11553b2aab18SMatthew Ahrens 		}
11563b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
11573b2aab18SMatthew Ahrens 		error = 0;
11583b2aab18SMatthew Ahrens 	}
11593b2aab18SMatthew Ahrens 	return (error);
1160f18faf3fSek }
1161f18faf3fSek 
1162f18faf3fSek static void
11633b2aab18SMatthew Ahrens dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1164f18faf3fSek {
11653b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
11663b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
11673b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
11683b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
11693b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *newds;
1170f18faf3fSek 	uint64_t dsobj;
11713b2aab18SMatthew Ahrens 	int error;
11723b2aab18SMatthew Ahrens 	uint64_t crflags;
11733b2aab18SMatthew Ahrens 
11743b2aab18SMatthew Ahrens 	crflags = (drrb->drr_flags & DRR_FLAG_CI_DATA) ?
11753b2aab18SMatthew Ahrens 	    DS_FLAG_CI_DATASET : 0;
1176f18faf3fSek 
11773b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
11783b2aab18SMatthew Ahrens 	if (error == 0) {
11793b2aab18SMatthew Ahrens 		/* create temporary clone */
118034f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap = NULL;
118134f2f8cfSMatthew Ahrens 		if (drba->drba_snapobj != 0) {
118234f2f8cfSMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
118334f2f8cfSMatthew Ahrens 			    drba->drba_snapobj, FTAG, &snap));
118434f2f8cfSMatthew Ahrens 		}
11853b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
118634f2f8cfSMatthew Ahrens 		    snap, crflags, drba->drba_cred, tx);
118734f2f8cfSMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
11883b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
11893b2aab18SMatthew Ahrens 	} else {
11903b2aab18SMatthew Ahrens 		dsl_dir_t *dd;
11913b2aab18SMatthew Ahrens 		const char *tail;
11923b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
11933b2aab18SMatthew Ahrens 
11943b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
11953b2aab18SMatthew Ahrens 
11963b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
11973b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
11983b2aab18SMatthew Ahrens 			    FTAG, &origin));
11993b2aab18SMatthew Ahrens 		}
12003b2aab18SMatthew Ahrens 
12013b2aab18SMatthew Ahrens 		/* Create new dataset. */
12023b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(dd,
12033b2aab18SMatthew Ahrens 		    strrchr(tofs, '/') + 1,
12043b2aab18SMatthew Ahrens 		    origin, crflags, drba->drba_cred, tx);
12053b2aab18SMatthew Ahrens 		if (origin != NULL)
12063b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
12073b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
12083b2aab18SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
12093b2aab18SMatthew Ahrens 	}
12103b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
12113b2aab18SMatthew Ahrens 
1212b5152584SMatthew Ahrens 	if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1213b5152584SMatthew Ahrens 	    DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1214b5152584SMatthew Ahrens 	    !newds->ds_large_blocks) {
1215b5152584SMatthew Ahrens 		dsl_dataset_activate_large_blocks_sync_impl(dsobj, tx);
1216b5152584SMatthew Ahrens 		newds->ds_large_blocks = B_TRUE;
1217b5152584SMatthew Ahrens 	}
1218b5152584SMatthew Ahrens 
12193b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
1220c1379625SJustin T. Gibbs 	dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
12213cb34c60Sahrens 
1222ae46e4c7SMatthew Ahrens 	/*
1223ae46e4c7SMatthew Ahrens 	 * If we actually created a non-clone, we need to create the
1224ae46e4c7SMatthew Ahrens 	 * objset in our new dataset.
1225ae46e4c7SMatthew Ahrens 	 */
12263b2aab18SMatthew Ahrens 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1227ae46e4c7SMatthew Ahrens 		(void) dmu_objset_create_impl(dp->dp_spa,
12283b2aab18SMatthew Ahrens 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1229ae46e4c7SMatthew Ahrens 	}
1230ae46e4c7SMatthew Ahrens 
12313b2aab18SMatthew Ahrens 	drba->drba_cookie->drc_ds = newds;
1232dc7cd546SMark Shellenbaum 
12333b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(newds, "receive", tx, "");
1234dc7cd546SMark Shellenbaum }
1235dc7cd546SMark Shellenbaum 
12363cb34c60Sahrens /*
12373cb34c60Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
12383cb34c60Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
12393cb34c60Sahrens  */
12403cb34c60Sahrens int
12413b2aab18SMatthew Ahrens dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
12423b2aab18SMatthew Ahrens     boolean_t force, char *origin, dmu_recv_cookie_t *drc)
1243efb80947Sahrens {
12443b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t drba = { 0 };
12453b2aab18SMatthew Ahrens 	dmu_replay_record_t *drr;
1246ab04eb8eStimh 
12473cb34c60Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
12483cb34c60Sahrens 	drc->drc_drrb = drrb;
12493cb34c60Sahrens 	drc->drc_tosnap = tosnap;
12503b2aab18SMatthew Ahrens 	drc->drc_tofs = tofs;
12513cb34c60Sahrens 	drc->drc_force = force;
1252a2afb611SJerry Jelinek 	drc->drc_cred = CRED();
1253efb80947Sahrens 
12543b2aab18SMatthew Ahrens 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
12553b2aab18SMatthew Ahrens 		drc->drc_byteswap = B_TRUE;
12563b2aab18SMatthew Ahrens 	else if (drrb->drr_magic != DMU_BACKUP_MAGIC)
1257be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1258efb80947Sahrens 
12593b2aab18SMatthew Ahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
12603b2aab18SMatthew Ahrens 	drr->drr_type = DRR_BEGIN;
12613b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin = *drc->drc_drrb;
12623b2aab18SMatthew Ahrens 	if (drc->drc_byteswap) {
12633b2aab18SMatthew Ahrens 		fletcher_4_incremental_byteswap(drr,
12643b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
12653b2aab18SMatthew Ahrens 	} else {
12663b2aab18SMatthew Ahrens 		fletcher_4_incremental_native(drr,
12673b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
12683b2aab18SMatthew Ahrens 	}
12693b2aab18SMatthew Ahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
1270dc7cd546SMark Shellenbaum 
12713b2aab18SMatthew Ahrens 	if (drc->drc_byteswap) {
12723b2aab18SMatthew Ahrens 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
12733b2aab18SMatthew Ahrens 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
12743b2aab18SMatthew Ahrens 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
12753b2aab18SMatthew Ahrens 		drrb->drr_type = BSWAP_32(drrb->drr_type);
12763b2aab18SMatthew Ahrens 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
12773b2aab18SMatthew Ahrens 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
12783cb34c60Sahrens 	}
12793cb34c60Sahrens 
12803b2aab18SMatthew Ahrens 	drba.drba_origin = origin;
12813b2aab18SMatthew Ahrens 	drba.drba_cookie = drc;
12823b2aab18SMatthew Ahrens 	drba.drba_cred = CRED();
12833b2aab18SMatthew Ahrens 
12843b2aab18SMatthew Ahrens 	return (dsl_sync_task(tofs, dmu_recv_begin_check, dmu_recv_begin_sync,
12857d46dc6cSMatthew Ahrens 	    &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1286efb80947Sahrens }
1287efb80947Sahrens 
12883cb34c60Sahrens struct restorearg {
1289*98110f08SMatthew Ahrens 	objset_t *os;
12903cb34c60Sahrens 	int err;
12913b2aab18SMatthew Ahrens 	boolean_t byteswap;
12923cb34c60Sahrens 	vnode_t *vp;
12933cb34c60Sahrens 	uint64_t voff;
12943cb34c60Sahrens 	int bufsize; /* amount of memory allocated for buf */
1295*98110f08SMatthew Ahrens 
1296*98110f08SMatthew Ahrens 	dmu_replay_record_t *drr;
1297*98110f08SMatthew Ahrens 	dmu_replay_record_t *next_drr;
1298*98110f08SMatthew Ahrens 	char *buf;
12993cb34c60Sahrens 	zio_cksum_t cksum;
1300*98110f08SMatthew Ahrens 	zio_cksum_t prev_cksum;
1301*98110f08SMatthew Ahrens 
1302c99e4bdcSChris Kirby 	avl_tree_t *guid_to_ds_map;
13033cb34c60Sahrens };
13043cb34c60Sahrens 
13059e69d7d0SLori Alt typedef struct guid_map_entry {
13069e69d7d0SLori Alt 	uint64_t	guid;
13079e69d7d0SLori Alt 	dsl_dataset_t	*gme_ds;
13089e69d7d0SLori Alt 	avl_node_t	avlnode;
13099e69d7d0SLori Alt } guid_map_entry_t;
13109e69d7d0SLori Alt 
13119e69d7d0SLori Alt static int
13129e69d7d0SLori Alt guid_compare(const void *arg1, const void *arg2)
13139e69d7d0SLori Alt {
13149e69d7d0SLori Alt 	const guid_map_entry_t *gmep1 = arg1;
13159e69d7d0SLori Alt 	const guid_map_entry_t *gmep2 = arg2;
13169e69d7d0SLori Alt 
13179e69d7d0SLori Alt 	if (gmep1->guid < gmep2->guid)
13189e69d7d0SLori Alt 		return (-1);
13199e69d7d0SLori Alt 	else if (gmep1->guid > gmep2->guid)
13209e69d7d0SLori Alt 		return (1);
13219e69d7d0SLori Alt 	return (0);
13229e69d7d0SLori Alt }
13239e69d7d0SLori Alt 
1324c99e4bdcSChris Kirby static void
1325c99e4bdcSChris Kirby free_guid_map_onexit(void *arg)
1326c99e4bdcSChris Kirby {
1327c99e4bdcSChris Kirby 	avl_tree_t *ca = arg;
1328c99e4bdcSChris Kirby 	void *cookie = NULL;
1329c99e4bdcSChris Kirby 	guid_map_entry_t *gmep;
1330c99e4bdcSChris Kirby 
1331c99e4bdcSChris Kirby 	while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
13323b2aab18SMatthew Ahrens 		dsl_dataset_long_rele(gmep->gme_ds, gmep);
1333de8d9cffSMatthew Ahrens 		dsl_dataset_rele(gmep->gme_ds, gmep);
1334c99e4bdcSChris Kirby 		kmem_free(gmep, sizeof (guid_map_entry_t));
1335c99e4bdcSChris Kirby 	}
1336c99e4bdcSChris Kirby 	avl_destroy(ca);
1337c99e4bdcSChris Kirby 	kmem_free(ca, sizeof (avl_tree_t));
1338c99e4bdcSChris Kirby }
1339c99e4bdcSChris Kirby 
1340*98110f08SMatthew Ahrens static int
1341*98110f08SMatthew Ahrens restore_read(struct restorearg *ra, int len, void *buf)
1342efb80947Sahrens {
13433cb34c60Sahrens 	int done = 0;
1344efb80947Sahrens 
1345efb80947Sahrens 	/* some things will require 8-byte alignment, so everything must */
1346fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
1347b5152584SMatthew Ahrens 	ASSERT3U(len, <=, ra->bufsize);
1348efb80947Sahrens 
13493cb34c60Sahrens 	while (done < len) {
1350efb80947Sahrens 		ssize_t resid;
1351efb80947Sahrens 
1352efb80947Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
1353*98110f08SMatthew Ahrens 		    (char *)buf + done, len - done,
1354efb80947Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
1355efb80947Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
1356efb80947Sahrens 
13573cb34c60Sahrens 		if (resid == len - done)
1358be6fd75aSMatthew Ahrens 			ra->err = SET_ERROR(EINVAL);
13593cb34c60Sahrens 		ra->voff += len - done - resid;
13603cb34c60Sahrens 		done = len - resid;
13613b2aab18SMatthew Ahrens 		if (ra->err != 0)
1362*98110f08SMatthew Ahrens 			return (ra->err);
1363efb80947Sahrens 	}
1364efb80947Sahrens 
13653cb34c60Sahrens 	ASSERT3U(done, ==, len);
1366*98110f08SMatthew Ahrens 	return (0);
1367efb80947Sahrens }
1368efb80947Sahrens 
1369efb80947Sahrens static void
1370*98110f08SMatthew Ahrens byteswap_record(dmu_replay_record_t *drr)
1371efb80947Sahrens {
1372efb80947Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1373efb80947Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1374efb80947Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
13753cb34c60Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
1376*98110f08SMatthew Ahrens 
1377efb80947Sahrens 	switch (drr->drr_type) {
1378efb80947Sahrens 	case DRR_BEGIN:
1379efb80947Sahrens 		DO64(drr_begin.drr_magic);
13809e69d7d0SLori Alt 		DO64(drr_begin.drr_versioninfo);
1381efb80947Sahrens 		DO64(drr_begin.drr_creation_time);
1382efb80947Sahrens 		DO32(drr_begin.drr_type);
13833cb34c60Sahrens 		DO32(drr_begin.drr_flags);
1384efb80947Sahrens 		DO64(drr_begin.drr_toguid);
1385efb80947Sahrens 		DO64(drr_begin.drr_fromguid);
1386efb80947Sahrens 		break;
1387efb80947Sahrens 	case DRR_OBJECT:
1388efb80947Sahrens 		DO64(drr_object.drr_object);
1389efb80947Sahrens 		DO32(drr_object.drr_type);
1390efb80947Sahrens 		DO32(drr_object.drr_bonustype);
1391efb80947Sahrens 		DO32(drr_object.drr_blksz);
1392efb80947Sahrens 		DO32(drr_object.drr_bonuslen);
13939e69d7d0SLori Alt 		DO64(drr_object.drr_toguid);
1394efb80947Sahrens 		break;
1395efb80947Sahrens 	case DRR_FREEOBJECTS:
1396efb80947Sahrens 		DO64(drr_freeobjects.drr_firstobj);
1397efb80947Sahrens 		DO64(drr_freeobjects.drr_numobjs);
13989e69d7d0SLori Alt 		DO64(drr_freeobjects.drr_toguid);
1399efb80947Sahrens 		break;
1400efb80947Sahrens 	case DRR_WRITE:
1401efb80947Sahrens 		DO64(drr_write.drr_object);
1402efb80947Sahrens 		DO32(drr_write.drr_type);
1403efb80947Sahrens 		DO64(drr_write.drr_offset);
1404efb80947Sahrens 		DO64(drr_write.drr_length);
14059e69d7d0SLori Alt 		DO64(drr_write.drr_toguid);
1406*98110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
14078e714474SLori Alt 		DO64(drr_write.drr_key.ddk_prop);
14089e69d7d0SLori Alt 		break;
14099e69d7d0SLori Alt 	case DRR_WRITE_BYREF:
14109e69d7d0SLori Alt 		DO64(drr_write_byref.drr_object);
14119e69d7d0SLori Alt 		DO64(drr_write_byref.drr_offset);
14129e69d7d0SLori Alt 		DO64(drr_write_byref.drr_length);
14139e69d7d0SLori Alt 		DO64(drr_write_byref.drr_toguid);
14149e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refguid);
14159e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refobject);
14169e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refoffset);
1417*98110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
1418*98110f08SMatthew Ahrens 		    drr_key.ddk_cksum);
14198e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_prop);
1420efb80947Sahrens 		break;
14215d7b4d43SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
14225d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_object);
14235d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_offset);
14245d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_length);
14255d7b4d43SMatthew Ahrens 		DO64(drr_write_embedded.drr_toguid);
14265d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_lsize);
14275d7b4d43SMatthew Ahrens 		DO32(drr_write_embedded.drr_psize);
14285d7b4d43SMatthew Ahrens 		break;
1429efb80947Sahrens 	case DRR_FREE:
1430efb80947Sahrens 		DO64(drr_free.drr_object);
1431efb80947Sahrens 		DO64(drr_free.drr_offset);
1432efb80947Sahrens 		DO64(drr_free.drr_length);
14339e69d7d0SLori Alt 		DO64(drr_free.drr_toguid);
1434efb80947Sahrens 		break;
14350a586ceaSMark Shellenbaum 	case DRR_SPILL:
14360a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_object);
14370a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_length);
14380a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_toguid);
14390a586ceaSMark Shellenbaum 		break;
1440efb80947Sahrens 	case DRR_END:
14419e69d7d0SLori Alt 		DO64(drr_end.drr_toguid);
1442*98110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1443efb80947Sahrens 		break;
1444efb80947Sahrens 	}
1445*98110f08SMatthew Ahrens 
1446*98110f08SMatthew Ahrens 	if (drr->drr_type != DRR_BEGIN) {
1447*98110f08SMatthew Ahrens 		ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
1448*98110f08SMatthew Ahrens 	}
1449*98110f08SMatthew Ahrens 
1450efb80947Sahrens #undef DO64
1451efb80947Sahrens #undef DO32
1452efb80947Sahrens }
1453efb80947Sahrens 
1454e77d42eaSMatthew Ahrens static inline uint8_t
1455e77d42eaSMatthew Ahrens deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1456e77d42eaSMatthew Ahrens {
1457e77d42eaSMatthew Ahrens 	if (bonus_type == DMU_OT_SA) {
1458e77d42eaSMatthew Ahrens 		return (1);
1459e77d42eaSMatthew Ahrens 	} else {
1460e77d42eaSMatthew Ahrens 		return (1 +
1461e77d42eaSMatthew Ahrens 		    ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
1462e77d42eaSMatthew Ahrens 	}
1463e77d42eaSMatthew Ahrens }
1464e77d42eaSMatthew Ahrens 
1465efb80947Sahrens static int
1466*98110f08SMatthew Ahrens restore_object(struct restorearg *ra, struct drr_object *drro, void *data)
1467efb80947Sahrens {
1468e77d42eaSMatthew Ahrens 	dmu_object_info_t doi;
1469efb80947Sahrens 	dmu_tx_t *tx;
1470e77d42eaSMatthew Ahrens 	uint64_t object;
1471e77d42eaSMatthew Ahrens 	int err;
1472efb80947Sahrens 
1473efb80947Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
1474ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_type) ||
1475ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
14769e69d7d0SLori Alt 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1477efb80947Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1478efb80947Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1479efb80947Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
1480*98110f08SMatthew Ahrens 	    drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(ra->os)) ||
1481efb80947Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1482be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1483efb80947Sahrens 	}
1484efb80947Sahrens 
1485*98110f08SMatthew Ahrens 	err = dmu_object_info(ra->os, drro->drr_object, &doi);
14862bf405a2SMark Maybee 
14872bf405a2SMark Maybee 	if (err != 0 && err != ENOENT)
1488be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1489e77d42eaSMatthew Ahrens 	object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
14902bf405a2SMark Maybee 
1491e77d42eaSMatthew Ahrens 	/*
1492e77d42eaSMatthew Ahrens 	 * If we are losing blkptrs or changing the block size this must
1493e77d42eaSMatthew Ahrens 	 * be a new file instance.  We must clear out the previous file
1494e77d42eaSMatthew Ahrens 	 * contents before we can change this type of metadata in the dnode.
1495e77d42eaSMatthew Ahrens 	 */
1496e77d42eaSMatthew Ahrens 	if (err == 0) {
1497e77d42eaSMatthew Ahrens 		int nblkptr;
1498e77d42eaSMatthew Ahrens 
1499e77d42eaSMatthew Ahrens 		nblkptr = deduce_nblkptr(drro->drr_bonustype,
1500e77d42eaSMatthew Ahrens 		    drro->drr_bonuslen);
1501e77d42eaSMatthew Ahrens 
1502e77d42eaSMatthew Ahrens 		if (drro->drr_blksz != doi.doi_data_block_size ||
1503e77d42eaSMatthew Ahrens 		    nblkptr < doi.doi_nblkptr) {
1504*98110f08SMatthew Ahrens 			err = dmu_free_long_range(ra->os, drro->drr_object,
1505e77d42eaSMatthew Ahrens 			    0, DMU_OBJECT_END);
1506e77d42eaSMatthew Ahrens 			if (err != 0)
1507e77d42eaSMatthew Ahrens 				return (SET_ERROR(EINVAL));
1508efb80947Sahrens 		}
1509e77d42eaSMatthew Ahrens 	}
1510e77d42eaSMatthew Ahrens 
1511*98110f08SMatthew Ahrens 	tx = dmu_tx_create(ra->os);
1512e77d42eaSMatthew Ahrens 	dmu_tx_hold_bonus(tx, object);
1513e77d42eaSMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
1514e77d42eaSMatthew Ahrens 	if (err != 0) {
1515e77d42eaSMatthew Ahrens 		dmu_tx_abort(tx);
1516e77d42eaSMatthew Ahrens 		return (err);
1517e77d42eaSMatthew Ahrens 	}
1518e77d42eaSMatthew Ahrens 
1519e77d42eaSMatthew Ahrens 	if (object == DMU_NEW_OBJECT) {
1520e77d42eaSMatthew Ahrens 		/* currently free, want to be allocated */
1521*98110f08SMatthew Ahrens 		err = dmu_object_claim(ra->os, drro->drr_object,
1522efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
1523efb80947Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1524e77d42eaSMatthew Ahrens 	} else if (drro->drr_type != doi.doi_type ||
1525e77d42eaSMatthew Ahrens 	    drro->drr_blksz != doi.doi_data_block_size ||
1526e77d42eaSMatthew Ahrens 	    drro->drr_bonustype != doi.doi_bonus_type ||
1527e77d42eaSMatthew Ahrens 	    drro->drr_bonuslen != doi.doi_bonus_size) {
1528e77d42eaSMatthew Ahrens 		/* currently allocated, but with different properties */
1529*98110f08SMatthew Ahrens 		err = dmu_object_reclaim(ra->os, drro->drr_object,
1530efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
1531e77d42eaSMatthew Ahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
1532efb80947Sahrens 	}
15333b2aab18SMatthew Ahrens 	if (err != 0) {
1534e77d42eaSMatthew Ahrens 		dmu_tx_commit(tx);
1535be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
15360a586ceaSMark Shellenbaum 	}
15372bf405a2SMark Maybee 
1538*98110f08SMatthew Ahrens 	dmu_object_set_checksum(ra->os, drro->drr_object,
1539*98110f08SMatthew Ahrens 	    drro->drr_checksumtype, tx);
1540*98110f08SMatthew Ahrens 	dmu_object_set_compress(ra->os, drro->drr_object,
1541*98110f08SMatthew Ahrens 	    drro->drr_compress, tx);
1542efb80947Sahrens 
1543adee0b6fSTim Haley 	if (data != NULL) {
1544efb80947Sahrens 		dmu_buf_t *db;
1545adee0b6fSTim Haley 
1546*98110f08SMatthew Ahrens 		VERIFY0(dmu_bonus_hold(ra->os, drro->drr_object, FTAG, &db));
1547efb80947Sahrens 		dmu_buf_will_dirty(db, tx);
1548efb80947Sahrens 
15491934e92fSmaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
15501934e92fSmaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
1551efb80947Sahrens 		if (ra->byteswap) {
1552ad135b5dSChristopher Siden 			dmu_object_byteswap_t byteswap =
1553ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
1554ad135b5dSChristopher Siden 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
1555efb80947Sahrens 			    drro->drr_bonuslen);
1556efb80947Sahrens 		}
1557efb80947Sahrens 		dmu_buf_rele(db, FTAG);
1558efb80947Sahrens 	}
1559efb80947Sahrens 	dmu_tx_commit(tx);
1560efb80947Sahrens 	return (0);
1561efb80947Sahrens }
1562efb80947Sahrens 
1563efb80947Sahrens /* ARGSUSED */
1564efb80947Sahrens static int
1565*98110f08SMatthew Ahrens restore_freeobjects(struct restorearg *ra,
1566efb80947Sahrens     struct drr_freeobjects *drrfo)
1567efb80947Sahrens {
1568efb80947Sahrens 	uint64_t obj;
1569efb80947Sahrens 
1570efb80947Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
1571be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1572efb80947Sahrens 
1573efb80947Sahrens 	for (obj = drrfo->drr_firstobj;
1574432f72fdSahrens 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
1575*98110f08SMatthew Ahrens 	    (void) dmu_object_next(ra->os, &obj, FALSE, 0)) {
1576efb80947Sahrens 		int err;
1577efb80947Sahrens 
1578*98110f08SMatthew Ahrens 		if (dmu_object_info(ra->os, obj, NULL) != 0)
1579efb80947Sahrens 			continue;
1580efb80947Sahrens 
1581*98110f08SMatthew Ahrens 		err = dmu_free_long_object(ra->os, obj);
15823b2aab18SMatthew Ahrens 		if (err != 0)
1583efb80947Sahrens 			return (err);
1584efb80947Sahrens 	}
1585efb80947Sahrens 	return (0);
1586efb80947Sahrens }
1587efb80947Sahrens 
1588efb80947Sahrens static int
1589*98110f08SMatthew Ahrens restore_write(struct restorearg *ra, struct drr_write *drrw, arc_buf_t *abuf)
1590efb80947Sahrens {
1591efb80947Sahrens 	dmu_tx_t *tx;
1592efb80947Sahrens 	int err;
1593efb80947Sahrens 
1594efb80947Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
1595ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drrw->drr_type))
1596be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1597efb80947Sahrens 
1598*98110f08SMatthew Ahrens 	if (dmu_object_info(ra->os, drrw->drr_object, NULL) != 0)
15998a904709SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16008a904709SMatthew Ahrens 
1601*98110f08SMatthew Ahrens 	tx = dmu_tx_create(ra->os);
1602efb80947Sahrens 
1603efb80947Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
1604efb80947Sahrens 	    drrw->drr_offset, drrw->drr_length);
1605efb80947Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
16063b2aab18SMatthew Ahrens 	if (err != 0) {
1607efb80947Sahrens 		dmu_tx_abort(tx);
1608efb80947Sahrens 		return (err);
1609efb80947Sahrens 	}
1610ad135b5dSChristopher Siden 	if (ra->byteswap) {
1611ad135b5dSChristopher Siden 		dmu_object_byteswap_t byteswap =
1612ad135b5dSChristopher Siden 		    DMU_OT_BYTESWAP(drrw->drr_type);
1613*98110f08SMatthew Ahrens 		dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
1614*98110f08SMatthew Ahrens 		    drrw->drr_length);
1615ad135b5dSChristopher Siden 	}
1616*98110f08SMatthew Ahrens 
1617*98110f08SMatthew Ahrens 	dmu_buf_t *bonus;
1618*98110f08SMatthew Ahrens 	if (dmu_bonus_hold(ra->os, drrw->drr_object, FTAG, &bonus) != 0)
1619*98110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
16208a904709SMatthew Ahrens 	dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
1621efb80947Sahrens 	dmu_tx_commit(tx);
16228a904709SMatthew Ahrens 	dmu_buf_rele(bonus, FTAG);
1623efb80947Sahrens 	return (0);
1624efb80947Sahrens }
1625efb80947Sahrens 
16269e69d7d0SLori Alt /*
16279e69d7d0SLori Alt  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
16289e69d7d0SLori Alt  * streams to refer to a copy of the data that is already on the
16299e69d7d0SLori Alt  * system because it came in earlier in the stream.  This function
16309e69d7d0SLori Alt  * finds the earlier copy of the data, and uses that copy instead of
16319e69d7d0SLori Alt  * data from the stream to fulfill this write.
16329e69d7d0SLori Alt  */
16339e69d7d0SLori Alt static int
1634*98110f08SMatthew Ahrens restore_write_byref(struct restorearg *ra, struct drr_write_byref *drrwbr)
16359e69d7d0SLori Alt {
16369e69d7d0SLori Alt 	dmu_tx_t *tx;
16379e69d7d0SLori Alt 	int err;
16389e69d7d0SLori Alt 	guid_map_entry_t gmesrch;
16399e69d7d0SLori Alt 	guid_map_entry_t *gmep;
16405d7b4d43SMatthew Ahrens 	avl_index_t where;
16419e69d7d0SLori Alt 	objset_t *ref_os = NULL;
16429e69d7d0SLori Alt 	dmu_buf_t *dbp;
16439e69d7d0SLori Alt 
16449e69d7d0SLori Alt 	if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
1645be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
16469e69d7d0SLori Alt 
16479e69d7d0SLori Alt 	/*
16489e69d7d0SLori Alt 	 * If the GUID of the referenced dataset is different from the
16499e69d7d0SLori Alt 	 * GUID of the target dataset, find the referenced dataset.
16509e69d7d0SLori Alt 	 */
16519e69d7d0SLori Alt 	if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
16529e69d7d0SLori Alt 		gmesrch.guid = drrwbr->drr_refguid;
1653c99e4bdcSChris Kirby 		if ((gmep = avl_find(ra->guid_to_ds_map, &gmesrch,
16549e69d7d0SLori Alt 		    &where)) == NULL) {
1655be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
16569e69d7d0SLori Alt 		}
16579e69d7d0SLori Alt 		if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
1658be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
16599e69d7d0SLori Alt 	} else {
1660*98110f08SMatthew Ahrens 		ref_os = ra->os;
16619e69d7d0SLori Alt 	}
16629e69d7d0SLori Alt 
16635d7b4d43SMatthew Ahrens 	err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
16645d7b4d43SMatthew Ahrens 	    drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
16655d7b4d43SMatthew Ahrens 	if (err != 0)
16669e69d7d0SLori Alt 		return (err);
16679e69d7d0SLori Alt 
1668*98110f08SMatthew Ahrens 	tx = dmu_tx_create(ra->os);
16699e69d7d0SLori Alt 
16709e69d7d0SLori Alt 	dmu_tx_hold_write(tx, drrwbr->drr_object,
16719e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length);
16729e69d7d0SLori Alt 	err = dmu_tx_assign(tx, TXG_WAIT);
16733b2aab18SMatthew Ahrens 	if (err != 0) {
16749e69d7d0SLori Alt 		dmu_tx_abort(tx);
16759e69d7d0SLori Alt 		return (err);
16769e69d7d0SLori Alt 	}
1677*98110f08SMatthew Ahrens 	dmu_write(ra->os, drrwbr->drr_object,
16789e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
16799e69d7d0SLori Alt 	dmu_buf_rele(dbp, FTAG);
16809e69d7d0SLori Alt 	dmu_tx_commit(tx);
16819e69d7d0SLori Alt 	return (0);
16829e69d7d0SLori Alt }
16839e69d7d0SLori Alt 
16845d7b4d43SMatthew Ahrens static int
1685*98110f08SMatthew Ahrens restore_write_embedded(struct restorearg *ra,
1686*98110f08SMatthew Ahrens     struct drr_write_embedded *drrwnp, void *data)
16875d7b4d43SMatthew Ahrens {
16885d7b4d43SMatthew Ahrens 	dmu_tx_t *tx;
16895d7b4d43SMatthew Ahrens 	int err;
16905d7b4d43SMatthew Ahrens 
16915d7b4d43SMatthew Ahrens 	if (drrwnp->drr_offset + drrwnp->drr_length < drrwnp->drr_offset)
16925d7b4d43SMatthew Ahrens 		return (EINVAL);
16935d7b4d43SMatthew Ahrens 
16945d7b4d43SMatthew Ahrens 	if (drrwnp->drr_psize > BPE_PAYLOAD_SIZE)
16955d7b4d43SMatthew Ahrens 		return (EINVAL);
16965d7b4d43SMatthew Ahrens 
16975d7b4d43SMatthew Ahrens 	if (drrwnp->drr_etype >= NUM_BP_EMBEDDED_TYPES)
16985d7b4d43SMatthew Ahrens 		return (EINVAL);
16995d7b4d43SMatthew Ahrens 	if (drrwnp->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
17005d7b4d43SMatthew Ahrens 		return (EINVAL);
17015d7b4d43SMatthew Ahrens 
1702*98110f08SMatthew Ahrens 	tx = dmu_tx_create(ra->os);
17035d7b4d43SMatthew Ahrens 
17045d7b4d43SMatthew Ahrens 	dmu_tx_hold_write(tx, drrwnp->drr_object,
17055d7b4d43SMatthew Ahrens 	    drrwnp->drr_offset, drrwnp->drr_length);
17065d7b4d43SMatthew Ahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
17075d7b4d43SMatthew Ahrens 	if (err != 0) {
17085d7b4d43SMatthew Ahrens 		dmu_tx_abort(tx);
17095d7b4d43SMatthew Ahrens 		return (err);
17105d7b4d43SMatthew Ahrens 	}
17115d7b4d43SMatthew Ahrens 
1712*98110f08SMatthew Ahrens 	dmu_write_embedded(ra->os, drrwnp->drr_object,
17135d7b4d43SMatthew Ahrens 	    drrwnp->drr_offset, data, drrwnp->drr_etype,
17145d7b4d43SMatthew Ahrens 	    drrwnp->drr_compression, drrwnp->drr_lsize, drrwnp->drr_psize,
17155d7b4d43SMatthew Ahrens 	    ra->byteswap ^ ZFS_HOST_BYTEORDER, tx);
17165d7b4d43SMatthew Ahrens 
17175d7b4d43SMatthew Ahrens 	dmu_tx_commit(tx);
17185d7b4d43SMatthew Ahrens 	return (0);
17195d7b4d43SMatthew Ahrens }
17205d7b4d43SMatthew Ahrens 
17210a586ceaSMark Shellenbaum static int
1722*98110f08SMatthew Ahrens restore_spill(struct restorearg *ra, struct drr_spill *drrs, void *data)
17230a586ceaSMark Shellenbaum {
17240a586ceaSMark Shellenbaum 	dmu_tx_t *tx;
17250a586ceaSMark Shellenbaum 	dmu_buf_t *db, *db_spill;
17260a586ceaSMark Shellenbaum 	int err;
17270a586ceaSMark Shellenbaum 
17280a586ceaSMark Shellenbaum 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
1729*98110f08SMatthew Ahrens 	    drrs->drr_length > spa_maxblocksize(dmu_objset_spa(ra->os)))
1730be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
17310a586ceaSMark Shellenbaum 
1732*98110f08SMatthew Ahrens 	if (dmu_object_info(ra->os, drrs->drr_object, NULL) != 0)
1733be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
17340a586ceaSMark Shellenbaum 
1735*98110f08SMatthew Ahrens 	VERIFY0(dmu_bonus_hold(ra->os, drrs->drr_object, FTAG, &db));
17360a586ceaSMark Shellenbaum 	if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
17370a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
17380a586ceaSMark Shellenbaum 		return (err);
17390a586ceaSMark Shellenbaum 	}
17400a586ceaSMark Shellenbaum 
1741*98110f08SMatthew Ahrens 	tx = dmu_tx_create(ra->os);
17420a586ceaSMark Shellenbaum 
17430a586ceaSMark Shellenbaum 	dmu_tx_hold_spill(tx, db->db_object);
17440a586ceaSMark Shellenbaum 
17450a586ceaSMark Shellenbaum 	err = dmu_tx_assign(tx, TXG_WAIT);
17463b2aab18SMatthew Ahrens 	if (err != 0) {
17470a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
17480a586ceaSMark Shellenbaum 		dmu_buf_rele(db_spill, FTAG);
17490a586ceaSMark Shellenbaum 		dmu_tx_abort(tx);
17500a586ceaSMark Shellenbaum 		return (err);
17510a586ceaSMark Shellenbaum 	}
17520a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(db_spill, tx);
17530a586ceaSMark Shellenbaum 
17540a586ceaSMark Shellenbaum 	if (db_spill->db_size < drrs->drr_length)
17550a586ceaSMark Shellenbaum 		VERIFY(0 == dbuf_spill_set_blksz(db_spill,
17560a586ceaSMark Shellenbaum 		    drrs->drr_length, tx));
17570a586ceaSMark Shellenbaum 	bcopy(data, db_spill->db_data, drrs->drr_length);
17580a586ceaSMark Shellenbaum 
17590a586ceaSMark Shellenbaum 	dmu_buf_rele(db, FTAG);
17600a586ceaSMark Shellenbaum 	dmu_buf_rele(db_spill, FTAG);
17610a586ceaSMark Shellenbaum 
17620a586ceaSMark Shellenbaum 	dmu_tx_commit(tx);
17630a586ceaSMark Shellenbaum 	return (0);
17640a586ceaSMark Shellenbaum }
17650a586ceaSMark Shellenbaum 
1766efb80947Sahrens /* ARGSUSED */
1767efb80947Sahrens static int
1768*98110f08SMatthew Ahrens restore_free(struct restorearg *ra, struct drr_free *drrf)
1769efb80947Sahrens {
1770efb80947Sahrens 	int err;
1771efb80947Sahrens 
1772efb80947Sahrens 	if (drrf->drr_length != -1ULL &&
1773efb80947Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
1774be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1775efb80947Sahrens 
1776*98110f08SMatthew Ahrens 	if (dmu_object_info(ra->os, drrf->drr_object, NULL) != 0)
1777be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1778efb80947Sahrens 
1779*98110f08SMatthew Ahrens 	err = dmu_free_long_range(ra->os, drrf->drr_object,
1780efb80947Sahrens 	    drrf->drr_offset, drrf->drr_length);
1781efb80947Sahrens 	return (err);
1782efb80947Sahrens }
1783efb80947Sahrens 
17843b2aab18SMatthew Ahrens /* used to destroy the drc_ds on error */
17853b2aab18SMatthew Ahrens static void
17863b2aab18SMatthew Ahrens dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
17873b2aab18SMatthew Ahrens {
17883b2aab18SMatthew Ahrens 	char name[MAXNAMELEN];
17893b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
17903b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
17913b2aab18SMatthew Ahrens 	(void) dsl_destroy_head(name);
17923b2aab18SMatthew Ahrens }
17933b2aab18SMatthew Ahrens 
1794*98110f08SMatthew Ahrens static void
1795*98110f08SMatthew Ahrens restore_cksum(struct restorearg *ra, int len, void *buf)
1796*98110f08SMatthew Ahrens {
1797*98110f08SMatthew Ahrens 	if (ra->byteswap) {
1798*98110f08SMatthew Ahrens 		fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
1799*98110f08SMatthew Ahrens 	} else {
1800*98110f08SMatthew Ahrens 		fletcher_4_incremental_native(buf, len, &ra->cksum);
1801*98110f08SMatthew Ahrens 	}
1802*98110f08SMatthew Ahrens }
1803*98110f08SMatthew Ahrens 
1804*98110f08SMatthew Ahrens /*
1805*98110f08SMatthew Ahrens  * If len != 0, read payload into buf.
1806*98110f08SMatthew Ahrens  * Read next record's header into ra->next_drr.
1807*98110f08SMatthew Ahrens  * Verify checksum of payload and next record.
1808*98110f08SMatthew Ahrens  */
1809*98110f08SMatthew Ahrens static int
1810*98110f08SMatthew Ahrens restore_read_payload_and_next_header(struct restorearg *ra, int len, void *buf)
1811*98110f08SMatthew Ahrens {
1812*98110f08SMatthew Ahrens 	int err;
1813*98110f08SMatthew Ahrens 
1814*98110f08SMatthew Ahrens 	if (len != 0) {
1815*98110f08SMatthew Ahrens 		ASSERT3U(len, <=, ra->bufsize);
1816*98110f08SMatthew Ahrens 		err = restore_read(ra, len, buf);
1817*98110f08SMatthew Ahrens 		if (err != 0)
1818*98110f08SMatthew Ahrens 			return (err);
1819*98110f08SMatthew Ahrens 		restore_cksum(ra, len, buf);
1820*98110f08SMatthew Ahrens 	}
1821*98110f08SMatthew Ahrens 
1822*98110f08SMatthew Ahrens 	ra->prev_cksum = ra->cksum;
1823*98110f08SMatthew Ahrens 
1824*98110f08SMatthew Ahrens 	err = restore_read(ra, sizeof (*ra->next_drr), ra->next_drr);
1825*98110f08SMatthew Ahrens 	if (err != 0)
1826*98110f08SMatthew Ahrens 		return (err);
1827*98110f08SMatthew Ahrens 	if (ra->next_drr->drr_type == DRR_BEGIN)
1828*98110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
1829*98110f08SMatthew Ahrens 
1830*98110f08SMatthew Ahrens 	/*
1831*98110f08SMatthew Ahrens 	 * Note: checksum is of everything up to but not including the
1832*98110f08SMatthew Ahrens 	 * checksum itself.
1833*98110f08SMatthew Ahrens 	 */
1834*98110f08SMatthew Ahrens 	ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
1835*98110f08SMatthew Ahrens 	    ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
1836*98110f08SMatthew Ahrens 	restore_cksum(ra,
1837*98110f08SMatthew Ahrens 	    offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
1838*98110f08SMatthew Ahrens 	    ra->next_drr);
1839*98110f08SMatthew Ahrens 
1840*98110f08SMatthew Ahrens 	zio_cksum_t cksum_orig = ra->next_drr->drr_u.drr_checksum.drr_checksum;
1841*98110f08SMatthew Ahrens 	zio_cksum_t *cksump = &ra->next_drr->drr_u.drr_checksum.drr_checksum;
1842*98110f08SMatthew Ahrens 
1843*98110f08SMatthew Ahrens 	if (ra->byteswap)
1844*98110f08SMatthew Ahrens 		byteswap_record(ra->next_drr);
1845*98110f08SMatthew Ahrens 
1846*98110f08SMatthew Ahrens 	if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
1847*98110f08SMatthew Ahrens 	    !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump))
1848*98110f08SMatthew Ahrens 		return (SET_ERROR(ECKSUM));
1849*98110f08SMatthew Ahrens 
1850*98110f08SMatthew Ahrens 	restore_cksum(ra, sizeof (cksum_orig), &cksum_orig);
1851*98110f08SMatthew Ahrens 
1852*98110f08SMatthew Ahrens 	return (0);
1853*98110f08SMatthew Ahrens }
1854*98110f08SMatthew Ahrens 
1855*98110f08SMatthew Ahrens static int
1856*98110f08SMatthew Ahrens restore_process_record(struct restorearg *ra)
1857*98110f08SMatthew Ahrens {
1858*98110f08SMatthew Ahrens 	int err;
1859*98110f08SMatthew Ahrens 
1860*98110f08SMatthew Ahrens 	switch (ra->drr->drr_type) {
1861*98110f08SMatthew Ahrens 	case DRR_OBJECT:
1862*98110f08SMatthew Ahrens 	{
1863*98110f08SMatthew Ahrens 		struct drr_object *drro = &ra->drr->drr_u.drr_object;
1864*98110f08SMatthew Ahrens 		err = restore_read_payload_and_next_header(ra,
1865*98110f08SMatthew Ahrens 		    P2ROUNDUP(drro->drr_bonuslen, 8), ra->buf);
1866*98110f08SMatthew Ahrens 		if (err != 0)
1867*98110f08SMatthew Ahrens 			return (err);
1868*98110f08SMatthew Ahrens 		return (restore_object(ra, drro, ra->buf));
1869*98110f08SMatthew Ahrens 	}
1870*98110f08SMatthew Ahrens 	case DRR_FREEOBJECTS:
1871*98110f08SMatthew Ahrens 	{
1872*98110f08SMatthew Ahrens 		struct drr_freeobjects *drrfo =
1873*98110f08SMatthew Ahrens 		    &ra->drr->drr_u.drr_freeobjects;
1874*98110f08SMatthew Ahrens 		err = restore_read_payload_and_next_header(ra, 0, NULL);
1875*98110f08SMatthew Ahrens 		if (err != 0)
1876*98110f08SMatthew Ahrens 			return (err);
1877*98110f08SMatthew Ahrens 		return (restore_freeobjects(ra, drrfo));
1878*98110f08SMatthew Ahrens 	}
1879*98110f08SMatthew Ahrens 	case DRR_WRITE:
1880*98110f08SMatthew Ahrens 	{
1881*98110f08SMatthew Ahrens 		struct drr_write *drrw = &ra->drr->drr_u.drr_write;
1882*98110f08SMatthew Ahrens 		arc_buf_t *abuf = arc_loan_buf(dmu_objset_spa(ra->os),
1883*98110f08SMatthew Ahrens 		    drrw->drr_length);
1884*98110f08SMatthew Ahrens 
1885*98110f08SMatthew Ahrens 		err = restore_read_payload_and_next_header(ra,
1886*98110f08SMatthew Ahrens 		    drrw->drr_length, abuf->b_data);
1887*98110f08SMatthew Ahrens 		if (err != 0)
1888*98110f08SMatthew Ahrens 			return (err);
1889*98110f08SMatthew Ahrens 		err = restore_write(ra, drrw, abuf);
1890*98110f08SMatthew Ahrens 		/* if restore_write() is successful, it consumes the arc_buf */
1891*98110f08SMatthew Ahrens 		if (err != 0)
1892*98110f08SMatthew Ahrens 			dmu_return_arcbuf(abuf);
1893*98110f08SMatthew Ahrens 		return (err);
1894*98110f08SMatthew Ahrens 	}
1895*98110f08SMatthew Ahrens 	case DRR_WRITE_BYREF:
1896*98110f08SMatthew Ahrens 	{
1897*98110f08SMatthew Ahrens 		struct drr_write_byref *drrwbr =
1898*98110f08SMatthew Ahrens 		    &ra->drr->drr_u.drr_write_byref;
1899*98110f08SMatthew Ahrens 		err = restore_read_payload_and_next_header(ra, 0, NULL);
1900*98110f08SMatthew Ahrens 		if (err != 0)
1901*98110f08SMatthew Ahrens 			return (err);
1902*98110f08SMatthew Ahrens 		return (restore_write_byref(ra, drrwbr));
1903*98110f08SMatthew Ahrens 	}
1904*98110f08SMatthew Ahrens 	case DRR_WRITE_EMBEDDED:
1905*98110f08SMatthew Ahrens 	{
1906*98110f08SMatthew Ahrens 		struct drr_write_embedded *drrwe =
1907*98110f08SMatthew Ahrens 		    &ra->drr->drr_u.drr_write_embedded;
1908*98110f08SMatthew Ahrens 		err = restore_read_payload_and_next_header(ra,
1909*98110f08SMatthew Ahrens 		    P2ROUNDUP(drrwe->drr_psize, 8), ra->buf);
1910*98110f08SMatthew Ahrens 		if (err != 0)
1911*98110f08SMatthew Ahrens 			return (err);
1912*98110f08SMatthew Ahrens 		return (restore_write_embedded(ra, drrwe, ra->buf));
1913*98110f08SMatthew Ahrens 	}
1914*98110f08SMatthew Ahrens 	case DRR_FREE:
1915*98110f08SMatthew Ahrens 	{
1916*98110f08SMatthew Ahrens 		struct drr_free *drrf = &ra->drr->drr_u.drr_free;
1917*98110f08SMatthew Ahrens 		err = restore_read_payload_and_next_header(ra, 0, NULL);
1918*98110f08SMatthew Ahrens 		if (err != 0)
1919*98110f08SMatthew Ahrens 			return (err);
1920*98110f08SMatthew Ahrens 		return (restore_free(ra, drrf));
1921*98110f08SMatthew Ahrens 	}
1922*98110f08SMatthew Ahrens 	case DRR_END:
1923*98110f08SMatthew Ahrens 	{
1924*98110f08SMatthew Ahrens 		struct drr_end *drre = &ra->drr->drr_u.drr_end;
1925*98110f08SMatthew Ahrens 		if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
1926*98110f08SMatthew Ahrens 			return (SET_ERROR(EINVAL));
1927*98110f08SMatthew Ahrens 		return (0);
1928*98110f08SMatthew Ahrens 	}
1929*98110f08SMatthew Ahrens 	case DRR_SPILL:
1930*98110f08SMatthew Ahrens 	{
1931*98110f08SMatthew Ahrens 		struct drr_spill *drrs = &ra->drr->drr_u.drr_spill;
1932*98110f08SMatthew Ahrens 		err = restore_read_payload_and_next_header(ra,
1933*98110f08SMatthew Ahrens 		    drrs->drr_length, ra->buf);
1934*98110f08SMatthew Ahrens 		if (err != 0)
1935*98110f08SMatthew Ahrens 			return (err);
1936*98110f08SMatthew Ahrens 		return (restore_spill(ra, drrs, ra->buf));
1937*98110f08SMatthew Ahrens 	}
1938*98110f08SMatthew Ahrens 	default:
1939*98110f08SMatthew Ahrens 		return (SET_ERROR(EINVAL));
1940*98110f08SMatthew Ahrens 	}
1941*98110f08SMatthew Ahrens }
1942*98110f08SMatthew Ahrens 
19433cb34c60Sahrens /*
19443cb34c60Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
19453cb34c60Sahrens  */
1946efb80947Sahrens int
1947c99e4bdcSChris Kirby dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
1948c99e4bdcSChris Kirby     int cleanup_fd, uint64_t *action_handlep)
1949efb80947Sahrens {
1950*98110f08SMatthew Ahrens 	int err = 0;
19513cb34c60Sahrens 	struct restorearg ra = { 0 };
19529e69d7d0SLori Alt 	int featureflags;
1953efb80947Sahrens 
19543b2aab18SMatthew Ahrens 	ra.byteswap = drc->drc_byteswap;
19553b2aab18SMatthew Ahrens 	ra.cksum = drc->drc_cksum;
19563cb34c60Sahrens 	ra.vp = vp;
19573cb34c60Sahrens 	ra.voff = *voffp;
1958b5152584SMatthew Ahrens 	ra.bufsize = SPA_MAXBLOCKSIZE;
1959*98110f08SMatthew Ahrens 	ra.drr = kmem_alloc(sizeof (*ra.drr), KM_SLEEP);
19603cb34c60Sahrens 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
1961*98110f08SMatthew Ahrens 	ra.next_drr = kmem_alloc(sizeof (*ra.next_drr), KM_SLEEP);
1962efb80947Sahrens 
19633cb34c60Sahrens 	/* these were verified in dmu_recv_begin */
19643b2aab18SMatthew Ahrens 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
19659e69d7d0SLori Alt 	    DMU_SUBSTREAM);
19663b2aab18SMatthew Ahrens 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
1967efb80947Sahrens 
1968efb80947Sahrens 	/*
1969efb80947Sahrens 	 * Open the objset we are modifying.
1970efb80947Sahrens 	 */
1971*98110f08SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
1972efb80947Sahrens 
1973c1379625SJustin T. Gibbs 	ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
1974efb80947Sahrens 
19759e69d7d0SLori Alt 	featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
19769e69d7d0SLori Alt 
19779e69d7d0SLori Alt 	/* if this stream is dedup'ed, set up the avl tree for guid mapping */
19789e69d7d0SLori Alt 	if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
1979a7f53a56SChris Kirby 		minor_t minor;
1980a7f53a56SChris Kirby 
1981c99e4bdcSChris Kirby 		if (cleanup_fd == -1) {
1982be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EBADF);
1983c99e4bdcSChris Kirby 			goto out;
1984c99e4bdcSChris Kirby 		}
1985a7f53a56SChris Kirby 		ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
19863b2aab18SMatthew Ahrens 		if (ra.err != 0) {
1987a7f53a56SChris Kirby 			cleanup_fd = -1;
1988a7f53a56SChris Kirby 			goto out;
1989a7f53a56SChris Kirby 		}
1990a7f53a56SChris Kirby 
1991c99e4bdcSChris Kirby 		if (*action_handlep == 0) {
1992c99e4bdcSChris Kirby 			ra.guid_to_ds_map =
1993c99e4bdcSChris Kirby 			    kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
1994c99e4bdcSChris Kirby 			avl_create(ra.guid_to_ds_map, guid_compare,
1995c99e4bdcSChris Kirby 			    sizeof (guid_map_entry_t),
1996c99e4bdcSChris Kirby 			    offsetof(guid_map_entry_t, avlnode));
1997*98110f08SMatthew Ahrens 			err = zfs_onexit_add_cb(minor,
1998c99e4bdcSChris Kirby 			    free_guid_map_onexit, ra.guid_to_ds_map,
1999c99e4bdcSChris Kirby 			    action_handlep);
20003b2aab18SMatthew Ahrens 			if (ra.err != 0)
2001c99e4bdcSChris Kirby 				goto out;
2002c99e4bdcSChris Kirby 		} else {
2003*98110f08SMatthew Ahrens 			err = zfs_onexit_cb_data(minor, *action_handlep,
2004c99e4bdcSChris Kirby 			    (void **)&ra.guid_to_ds_map);
20053b2aab18SMatthew Ahrens 			if (ra.err != 0)
2006c99e4bdcSChris Kirby 				goto out;
2007c99e4bdcSChris Kirby 		}
2008ec5cf9d5SAlexander Stetsenko 
2009ec5cf9d5SAlexander Stetsenko 		drc->drc_guid_to_ds_map = ra.guid_to_ds_map;
20109e69d7d0SLori Alt 	}
20119e69d7d0SLori Alt 
2012*98110f08SMatthew Ahrens 	err = restore_read_payload_and_next_header(&ra, 0, NULL);
2013*98110f08SMatthew Ahrens 	if (err != 0)
2014*98110f08SMatthew Ahrens 		goto out;
2015*98110f08SMatthew Ahrens 	for (;;) {
2016*98110f08SMatthew Ahrens 		void *tmp;
2017*98110f08SMatthew Ahrens 
2018efb80947Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
2019*98110f08SMatthew Ahrens 			err = SET_ERROR(EINTR);
2020*98110f08SMatthew Ahrens 			break;
2021efb80947Sahrens 		}
2022efb80947Sahrens 
2023*98110f08SMatthew Ahrens 		tmp = ra.next_drr;
2024*98110f08SMatthew Ahrens 		ra.next_drr = ra.drr;
2025*98110f08SMatthew Ahrens 		ra.drr = tmp;
2026efb80947Sahrens 
2027*98110f08SMatthew Ahrens 		/* process ra.drr, read in ra.next_drr */
2028*98110f08SMatthew Ahrens 		err = restore_process_record(&ra);
2029*98110f08SMatthew Ahrens 		if (err != 0)
2030efb80947Sahrens 			break;
2031*98110f08SMatthew Ahrens 		if (ra.drr->drr_type == DRR_END)
20320a586ceaSMark Shellenbaum 			break;
2033efb80947Sahrens 	}
2034efb80947Sahrens 
2035efb80947Sahrens out:
2036a7f53a56SChris Kirby 	if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2037a7f53a56SChris Kirby 		zfs_onexit_fd_rele(cleanup_fd);
2038a7f53a56SChris Kirby 
2039*98110f08SMatthew Ahrens 	if (err != 0) {
2040efb80947Sahrens 		/*
2041f4b94bdeSMatthew Ahrens 		 * destroy what we created, so we don't leave it in the
2042f4b94bdeSMatthew Ahrens 		 * inconsistent restoring state.
2043efb80947Sahrens 		 */
20443b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
2045efb80947Sahrens 	}
2046efb80947Sahrens 
2047*98110f08SMatthew Ahrens 	kmem_free(ra.drr, sizeof (*ra.drr));
2048efb80947Sahrens 	kmem_free(ra.buf, ra.bufsize);
2049*98110f08SMatthew Ahrens 	kmem_free(ra.next_drr, sizeof (*ra.next_drr));
20503cb34c60Sahrens 	*voffp = ra.voff;
2051*98110f08SMatthew Ahrens 	return (err);
2052efb80947Sahrens }
2053f18faf3fSek 
20543cb34c60Sahrens static int
20553b2aab18SMatthew Ahrens dmu_recv_end_check(void *arg, dmu_tx_t *tx)
20563cb34c60Sahrens {
20573b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
20583b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
20593b2aab18SMatthew Ahrens 	int error;
20603b2aab18SMatthew Ahrens 
20613b2aab18SMatthew Ahrens 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
20623cb34c60Sahrens 
20633b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
20643b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
20653b2aab18SMatthew Ahrens 
20663b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
20673b2aab18SMatthew Ahrens 		if (error != 0)
20683b2aab18SMatthew Ahrens 			return (error);
206934f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
207034f2f8cfSMatthew Ahrens 			/*
207134f2f8cfSMatthew Ahrens 			 * We will destroy any snapshots in tofs (i.e. before
207234f2f8cfSMatthew Ahrens 			 * origin_head) that are after the origin (which is
207334f2f8cfSMatthew Ahrens 			 * the snap before drc_ds, because drc_ds can not
207434f2f8cfSMatthew Ahrens 			 * have any snaps of its own).
207534f2f8cfSMatthew Ahrens 			 */
2076c1379625SJustin T. Gibbs 			uint64_t obj;
2077c1379625SJustin T. Gibbs 
2078c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2079c1379625SJustin T. Gibbs 			while (obj !=
2080c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
208134f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
208234f2f8cfSMatthew Ahrens 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
208334f2f8cfSMatthew Ahrens 				    &snap);
208434f2f8cfSMatthew Ahrens 				if (error != 0)
208534f2f8cfSMatthew Ahrens 					return (error);
208634f2f8cfSMatthew Ahrens 				if (snap->ds_dir != origin_head->ds_dir)
208734f2f8cfSMatthew Ahrens 					error = SET_ERROR(EINVAL);
208834f2f8cfSMatthew Ahrens 				if (error == 0)  {
208934f2f8cfSMatthew Ahrens 					error = dsl_destroy_snapshot_check_impl(
209034f2f8cfSMatthew Ahrens 					    snap, B_FALSE);
209134f2f8cfSMatthew Ahrens 				}
2092c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
209334f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
209434f2f8cfSMatthew Ahrens 				if (error != 0)
209534f2f8cfSMatthew Ahrens 					return (error);
209634f2f8cfSMatthew Ahrens 			}
209734f2f8cfSMatthew Ahrens 		}
20983b2aab18SMatthew Ahrens 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
209991948b51SKeith M Wesolowski 		    origin_head, drc->drc_force, drc->drc_owner, tx);
21003b2aab18SMatthew Ahrens 		if (error != 0) {
21013b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin_head, FTAG);
21023b2aab18SMatthew Ahrens 			return (error);
21033b2aab18SMatthew Ahrens 		}
21043b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(origin_head,
2105a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
21063b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
21073b2aab18SMatthew Ahrens 		if (error != 0)
21083b2aab18SMatthew Ahrens 			return (error);
21093b2aab18SMatthew Ahrens 
21103b2aab18SMatthew Ahrens 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
21113b2aab18SMatthew Ahrens 	} else {
21123b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
2113a2afb611SJerry Jelinek 		    drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
21143b2aab18SMatthew Ahrens 	}
21153b2aab18SMatthew Ahrens 	return (error);
21163cb34c60Sahrens }
21173cb34c60Sahrens 
21183cb34c60Sahrens static void
21193b2aab18SMatthew Ahrens dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
21203cb34c60Sahrens {
21213b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
21223b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
21233b2aab18SMatthew Ahrens 
21243b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
21253b2aab18SMatthew Ahrens 	    tx, "snap=%s", drc->drc_tosnap);
21263b2aab18SMatthew Ahrens 
21273b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
21283b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
21293b2aab18SMatthew Ahrens 
21303b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
21313b2aab18SMatthew Ahrens 		    &origin_head));
213234f2f8cfSMatthew Ahrens 
213334f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
213434f2f8cfSMatthew Ahrens 			/*
213534f2f8cfSMatthew Ahrens 			 * Destroy any snapshots of drc_tofs (origin_head)
213634f2f8cfSMatthew Ahrens 			 * after the origin (the snap before drc_ds).
213734f2f8cfSMatthew Ahrens 			 */
2138c1379625SJustin T. Gibbs 			uint64_t obj;
2139c1379625SJustin T. Gibbs 
2140c1379625SJustin T. Gibbs 			obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2141c1379625SJustin T. Gibbs 			while (obj !=
2142c1379625SJustin T. Gibbs 			    dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
214334f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
214434f2f8cfSMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
214534f2f8cfSMatthew Ahrens 				    &snap));
214634f2f8cfSMatthew Ahrens 				ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
2147c1379625SJustin T. Gibbs 				obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
214834f2f8cfSMatthew Ahrens 				dsl_destroy_snapshot_sync_impl(snap,
214934f2f8cfSMatthew Ahrens 				    B_FALSE, tx);
215034f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
215134f2f8cfSMatthew Ahrens 			}
215234f2f8cfSMatthew Ahrens 		}
215334f2f8cfSMatthew Ahrens 		VERIFY3P(drc->drc_ds->ds_prev, ==,
215434f2f8cfSMatthew Ahrens 		    origin_head->ds_prev);
215534f2f8cfSMatthew Ahrens 
21563b2aab18SMatthew Ahrens 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
21573b2aab18SMatthew Ahrens 		    origin_head, tx);
21583b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(origin_head,
21593b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
21603b2aab18SMatthew Ahrens 
21613b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
21623b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
2163c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
21643b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
2165c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
21663b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_toguid;
2167c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
21683b2aab18SMatthew Ahrens 		    ~DS_FLAG_INCONSISTENT;
21693b2aab18SMatthew Ahrens 
21703b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
2171c1379625SJustin T. Gibbs 		dsl_dataset_phys(origin_head)->ds_flags &=
2172c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
21733b2aab18SMatthew Ahrens 
21743b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
21753b2aab18SMatthew Ahrens 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
217691948b51SKeith M Wesolowski 
217791948b51SKeith M Wesolowski 		if (drc->drc_owner != NULL)
217891948b51SKeith M Wesolowski 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
21793b2aab18SMatthew Ahrens 	} else {
21803b2aab18SMatthew Ahrens 		dsl_dataset_t *ds = drc->drc_ds;
21813cb34c60Sahrens 
21823b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
21833cb34c60Sahrens 
21843b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
21853b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
2186c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
21873b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
2188c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_guid =
2189c1379625SJustin T. Gibbs 		    drc->drc_drrb->drr_toguid;
2190c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds->ds_prev)->ds_flags &=
2191c1379625SJustin T. Gibbs 		    ~DS_FLAG_INCONSISTENT;
21923cb34c60Sahrens 
21933b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
2194c1379625SJustin T. Gibbs 		dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
21953b2aab18SMatthew Ahrens 	}
2196c1379625SJustin T. Gibbs 	drc->drc_newsnapobj = dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
21973b2aab18SMatthew Ahrens 	/*
21983b2aab18SMatthew Ahrens 	 * Release the hold from dmu_recv_begin.  This must be done before
21993b2aab18SMatthew Ahrens 	 * we return to open context, so that when we free the dataset's dnode,
22003b2aab18SMatthew Ahrens 	 * we can evict its bonus buffer.
22013b2aab18SMatthew Ahrens 	 */
22023b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
22033b2aab18SMatthew Ahrens 	drc->drc_ds = NULL;
22043cb34c60Sahrens }
22053cb34c60Sahrens 
2206ec5cf9d5SAlexander Stetsenko static int
22073b2aab18SMatthew Ahrens add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
2208ec5cf9d5SAlexander Stetsenko {
22093b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
2210ec5cf9d5SAlexander Stetsenko 	dsl_dataset_t *snapds;
2211ec5cf9d5SAlexander Stetsenko 	guid_map_entry_t *gmep;
2212ec5cf9d5SAlexander Stetsenko 	int err;
2213ec5cf9d5SAlexander Stetsenko 
2214ec5cf9d5SAlexander Stetsenko 	ASSERT(guid_map != NULL);
2215ec5cf9d5SAlexander Stetsenko 
22163b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
22173b2aab18SMatthew Ahrens 	if (err != 0)
22183b2aab18SMatthew Ahrens 		return (err);
2219de8d9cffSMatthew Ahrens 	gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
2220de8d9cffSMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
2221ec5cf9d5SAlexander Stetsenko 	if (err == 0) {
2222c1379625SJustin T. Gibbs 		gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
2223ec5cf9d5SAlexander Stetsenko 		gmep->gme_ds = snapds;
2224ec5cf9d5SAlexander Stetsenko 		avl_add(guid_map, gmep);
22253b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(snapds, gmep);
2226de8d9cffSMatthew Ahrens 	} else {
2227de8d9cffSMatthew Ahrens 		kmem_free(gmep, sizeof (*gmep));
2228ec5cf9d5SAlexander Stetsenko 	}
2229ec5cf9d5SAlexander Stetsenko 
22303b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
2231ec5cf9d5SAlexander Stetsenko 	return (err);
2232ec5cf9d5SAlexander Stetsenko }
2233ec5cf9d5SAlexander Stetsenko 
22343b2aab18SMatthew Ahrens static int dmu_recv_end_modified_blocks = 3;
22353b2aab18SMatthew Ahrens 
2236ae46e4c7SMatthew Ahrens static int
2237ae46e4c7SMatthew Ahrens dmu_recv_existing_end(dmu_recv_cookie_t *drc)
2238f18faf3fSek {
22393b2aab18SMatthew Ahrens 	int error;
22403b2aab18SMatthew Ahrens 	char name[MAXNAMELEN];
22413cb34c60Sahrens 
22423b2aab18SMatthew Ahrens #ifdef _KERNEL
22433b2aab18SMatthew Ahrens 	/*
22443b2aab18SMatthew Ahrens 	 * We will be destroying the ds; make sure its origin is unmounted if
22453b2aab18SMatthew Ahrens 	 * necessary.
22463b2aab18SMatthew Ahrens 	 */
22473b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
22483b2aab18SMatthew Ahrens 	zfs_destroy_unmount_origin(name);
22493b2aab18SMatthew Ahrens #endif
22503cb34c60Sahrens 
22513b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
22523b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
22537d46dc6cSMatthew Ahrens 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
22543cb34c60Sahrens 
22553b2aab18SMatthew Ahrens 	if (error != 0)
22563b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
22573b2aab18SMatthew Ahrens 	return (error);
2258f18faf3fSek }
2259ae46e4c7SMatthew Ahrens 
2260ae46e4c7SMatthew Ahrens static int
2261ae46e4c7SMatthew Ahrens dmu_recv_new_end(dmu_recv_cookie_t *drc)
2262ae46e4c7SMatthew Ahrens {
22633b2aab18SMatthew Ahrens 	int error;
2264ae46e4c7SMatthew Ahrens 
22653b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
22663b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
22677d46dc6cSMatthew Ahrens 	    dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
2268ae46e4c7SMatthew Ahrens 
22693b2aab18SMatthew Ahrens 	if (error != 0) {
22703b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
22713b2aab18SMatthew Ahrens 	} else if (drc->drc_guid_to_ds_map != NULL) {
22723b2aab18SMatthew Ahrens 		(void) add_ds_to_guidmap(drc->drc_tofs,
22733b2aab18SMatthew Ahrens 		    drc->drc_guid_to_ds_map,
22743b2aab18SMatthew Ahrens 		    drc->drc_newsnapobj);
2275ae46e4c7SMatthew Ahrens 	}
22763b2aab18SMatthew Ahrens 	return (error);
2277ae46e4c7SMatthew Ahrens }
2278ae46e4c7SMatthew Ahrens 
2279ae46e4c7SMatthew Ahrens int
228091948b51SKeith M Wesolowski dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
2281ae46e4c7SMatthew Ahrens {
228291948b51SKeith M Wesolowski 	drc->drc_owner = owner;
228391948b51SKeith M Wesolowski 
22843b2aab18SMatthew Ahrens 	if (drc->drc_newfs)
2285ae46e4c7SMatthew Ahrens 		return (dmu_recv_new_end(drc));
22863b2aab18SMatthew Ahrens 	else
22873b2aab18SMatthew Ahrens 		return (dmu_recv_existing_end(drc));
2288ae46e4c7SMatthew Ahrens }
22892f3d8780SMatthew Ahrens 
22902f3d8780SMatthew Ahrens /*
22912f3d8780SMatthew Ahrens  * Return TRUE if this objset is currently being received into.
22922f3d8780SMatthew Ahrens  */
22932f3d8780SMatthew Ahrens boolean_t
22942f3d8780SMatthew Ahrens dmu_objset_is_receiving(objset_t *os)
22952f3d8780SMatthew Ahrens {
22962f3d8780SMatthew Ahrens 	return (os->os_dsl_dataset != NULL &&
22972f3d8780SMatthew Ahrens 	    os->os_dsl_dataset->ds_owner == dmu_recv_tag);
22982f3d8780SMatthew Ahrens }
2299