xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision 91948b51)
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.
24be6fd75aSMatthew Ahrens  * Copyright (c) 2013 by Delphix. All rights reserved.
254e3c9f44SBill Pijewski  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
26ec5cf9d5SAlexander Stetsenko  */
27efb80947Sahrens 
28efb80947Sahrens #include <sys/dmu.h>
29efb80947Sahrens #include <sys/dmu_impl.h>
30efb80947Sahrens #include <sys/dmu_tx.h>
31efb80947Sahrens #include <sys/dbuf.h>
32efb80947Sahrens #include <sys/dnode.h>
33efb80947Sahrens #include <sys/zfs_context.h>
34efb80947Sahrens #include <sys/dmu_objset.h>
35efb80947Sahrens #include <sys/dmu_traverse.h>
36efb80947Sahrens #include <sys/dsl_dataset.h>
37efb80947Sahrens #include <sys/dsl_dir.h>
3892241e0bSTom Erickson #include <sys/dsl_prop.h>
39efb80947Sahrens #include <sys/dsl_pool.h>
40efb80947Sahrens #include <sys/dsl_synctask.h>
41efb80947Sahrens #include <sys/zfs_ioctl.h>
42efb80947Sahrens #include <sys/zap.h>
43efb80947Sahrens #include <sys/zio_checksum.h>
44dc7cd546SMark Shellenbaum #include <sys/zfs_znode.h>
45cde58dbcSMatthew Ahrens #include <zfs_fletcher.h>
469e69d7d0SLori Alt #include <sys/avl.h>
478e714474SLori Alt #include <sys/ddt.h>
48c99e4bdcSChris Kirby #include <sys/zfs_onexit.h>
493b2aab18SMatthew Ahrens #include <sys/dmu_send.h>
503b2aab18SMatthew Ahrens #include <sys/dsl_destroy.h>
51efb80947Sahrens 
5219b94df9SMatthew Ahrens /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
5319b94df9SMatthew Ahrens int zfs_send_corrupt_data = B_FALSE;
5419b94df9SMatthew Ahrens 
553cb34c60Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
563b2aab18SMatthew Ahrens static const char *recv_clone_name = "%recv";
573cb34c60Sahrens 
58efb80947Sahrens static int
594e3c9f44SBill Pijewski dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
60efb80947Sahrens {
614e3c9f44SBill Pijewski 	dsl_dataset_t *ds = dsp->dsa_os->os_dsl_dataset;
62efb80947Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
63fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
64efb80947Sahrens 
654e3c9f44SBill Pijewski 	fletcher_4_incremental_native(buf, len, &dsp->dsa_zc);
664e3c9f44SBill Pijewski 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
67efb80947Sahrens 	    (caddr_t)buf, len,
68efb80947Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
694e3c9f44SBill Pijewski 
704e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
714e3c9f44SBill Pijewski 	*dsp->dsa_off += len;
724e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
734e3c9f44SBill Pijewski 
744e3c9f44SBill Pijewski 	return (dsp->dsa_err);
75efb80947Sahrens }
76efb80947Sahrens 
77efb80947Sahrens static int
784e3c9f44SBill Pijewski dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
79efb80947Sahrens     uint64_t length)
80efb80947Sahrens {
814e3c9f44SBill Pijewski 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
829e69d7d0SLori Alt 
83534029e5SSimon Klinkert 	if (length != -1ULL && offset + length < offset)
84534029e5SSimon Klinkert 		length = -1ULL;
85534029e5SSimon Klinkert 
869e69d7d0SLori Alt 	/*
879e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
889e69d7d0SLori Alt 	 * since free block aggregation can only be done for blocks of the
899e69d7d0SLori Alt 	 * same type (i.e., DRR_FREE records can only be aggregated with
909e69d7d0SLori Alt 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
919e69d7d0SLori Alt 	 * aggregated with other DRR_FREEOBJECTS records.
929e69d7d0SLori Alt 	 */
934e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
944e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREE) {
954e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
964e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
97be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
984e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
999e69d7d0SLori Alt 	}
1009e69d7d0SLori Alt 
1014e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREE) {
1029e69d7d0SLori Alt 		/*
1039e69d7d0SLori Alt 		 * There should never be a PENDING_FREE if length is -1
1049e69d7d0SLori Alt 		 * (because dump_dnode is the only place where this
1059e69d7d0SLori Alt 		 * function is called with a -1, and only after flushing
1069e69d7d0SLori Alt 		 * any pending record).
1079e69d7d0SLori Alt 		 */
1089e69d7d0SLori Alt 		ASSERT(length != -1ULL);
1099e69d7d0SLori Alt 		/*
1109e69d7d0SLori Alt 		 * Check to see whether this free block can be aggregated
1119e69d7d0SLori Alt 		 * with pending one.
1129e69d7d0SLori Alt 		 */
1139e69d7d0SLori Alt 		if (drrf->drr_object == object && drrf->drr_offset +
1149e69d7d0SLori Alt 		    drrf->drr_length == offset) {
1159e69d7d0SLori Alt 			drrf->drr_length += length;
1169e69d7d0SLori Alt 			return (0);
1179e69d7d0SLori Alt 		} else {
1189e69d7d0SLori Alt 			/* not a continuation.  Push out pending record */
1194e3c9f44SBill Pijewski 			if (dump_bytes(dsp, dsp->dsa_drr,
1209e69d7d0SLori Alt 			    sizeof (dmu_replay_record_t)) != 0)
121be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
1224e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
1239e69d7d0SLori Alt 		}
1249e69d7d0SLori Alt 	}
1259e69d7d0SLori Alt 	/* create a FREE record and make it pending */
1264e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
1274e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREE;
1289e69d7d0SLori Alt 	drrf->drr_object = object;
1299e69d7d0SLori Alt 	drrf->drr_offset = offset;
1309e69d7d0SLori Alt 	drrf->drr_length = length;
1314e3c9f44SBill Pijewski 	drrf->drr_toguid = dsp->dsa_toguid;
1329e69d7d0SLori Alt 	if (length == -1ULL) {
1334e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
1344e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
135be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1369e69d7d0SLori Alt 	} else {
1374e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_FREE;
1389e69d7d0SLori Alt 	}
139efb80947Sahrens 
140efb80947Sahrens 	return (0);
141efb80947Sahrens }
142efb80947Sahrens 
143efb80947Sahrens static int
1444e3c9f44SBill Pijewski dump_data(dmu_sendarg_t *dsp, dmu_object_type_t type,
1458e714474SLori Alt     uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
146efb80947Sahrens {
1474e3c9f44SBill Pijewski 	struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
1489e69d7d0SLori Alt 
1498e714474SLori Alt 
1509e69d7d0SLori Alt 	/*
1519e69d7d0SLori Alt 	 * If there is any kind of pending aggregation (currently either
1529e69d7d0SLori Alt 	 * a grouping of free objects or free blocks), push it out to
1539e69d7d0SLori Alt 	 * the stream, since aggregation can't be done across operations
1549e69d7d0SLori Alt 	 * of different types.
1559e69d7d0SLori Alt 	 */
1564e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
1574e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
1584e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
159be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1604e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
1619e69d7d0SLori Alt 	}
162efb80947Sahrens 	/* write a DATA record */
1634e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
1644e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_WRITE;
1659e69d7d0SLori Alt 	drrw->drr_object = object;
1669e69d7d0SLori Alt 	drrw->drr_type = type;
1679e69d7d0SLori Alt 	drrw->drr_offset = offset;
1689e69d7d0SLori Alt 	drrw->drr_length = blksz;
1694e3c9f44SBill Pijewski 	drrw->drr_toguid = dsp->dsa_toguid;
1708e714474SLori Alt 	drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
1718e714474SLori Alt 	if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup)
1728e714474SLori Alt 		drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
1738e714474SLori Alt 	DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
1748e714474SLori Alt 	DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
1758e714474SLori Alt 	DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
1768e714474SLori Alt 	drrw->drr_key.ddk_cksum = bp->blk_cksum;
177efb80947Sahrens 
1784e3c9f44SBill Pijewski 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
179be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
1804e3c9f44SBill Pijewski 	if (dump_bytes(dsp, data, blksz) != 0)
181be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
182efb80947Sahrens 	return (0);
183efb80947Sahrens }
184efb80947Sahrens 
1850a586ceaSMark Shellenbaum static int
1864e3c9f44SBill Pijewski dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
1870a586ceaSMark Shellenbaum {
1884e3c9f44SBill Pijewski 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
1890a586ceaSMark Shellenbaum 
1904e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
1914e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
1924e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
193be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1944e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
1950a586ceaSMark Shellenbaum 	}
1960a586ceaSMark Shellenbaum 
1970a586ceaSMark Shellenbaum 	/* write a SPILL record */
1984e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
1994e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_SPILL;
2000a586ceaSMark Shellenbaum 	drrs->drr_object = object;
2010a586ceaSMark Shellenbaum 	drrs->drr_length = blksz;
2024e3c9f44SBill Pijewski 	drrs->drr_toguid = dsp->dsa_toguid;
2030a586ceaSMark Shellenbaum 
2044e3c9f44SBill Pijewski 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)))
205be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
2064e3c9f44SBill Pijewski 	if (dump_bytes(dsp, data, blksz))
207be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
2080a586ceaSMark Shellenbaum 	return (0);
2090a586ceaSMark Shellenbaum }
2100a586ceaSMark Shellenbaum 
211efb80947Sahrens static int
2124e3c9f44SBill Pijewski dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
213efb80947Sahrens {
2144e3c9f44SBill Pijewski 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
2159e69d7d0SLori Alt 
2169e69d7d0SLori Alt 	/*
2179e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
2189e69d7d0SLori Alt 	 * push it out, since free block aggregation can only be done for
2199e69d7d0SLori Alt 	 * blocks of the same type (i.e., DRR_FREE records can only be
2209e69d7d0SLori Alt 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
2219e69d7d0SLori Alt 	 * can only be aggregated with other DRR_FREEOBJECTS records.
2229e69d7d0SLori Alt 	 */
2234e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
2244e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
2254e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
2264e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
227be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2284e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2299e69d7d0SLori Alt 	}
2304e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
2319e69d7d0SLori Alt 		/*
2329e69d7d0SLori Alt 		 * See whether this free object array can be aggregated
2339e69d7d0SLori Alt 		 * with pending one
2349e69d7d0SLori Alt 		 */
2359e69d7d0SLori Alt 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
2369e69d7d0SLori Alt 			drrfo->drr_numobjs += numobjs;
2379e69d7d0SLori Alt 			return (0);
2389e69d7d0SLori Alt 		} else {
2399e69d7d0SLori Alt 			/* can't be aggregated.  Push out pending record */
2404e3c9f44SBill Pijewski 			if (dump_bytes(dsp, dsp->dsa_drr,
2419e69d7d0SLori Alt 			    sizeof (dmu_replay_record_t)) != 0)
242be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
2434e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
2449e69d7d0SLori Alt 		}
2459e69d7d0SLori Alt 	}
2469e69d7d0SLori Alt 
247efb80947Sahrens 	/* write a FREEOBJECTS record */
2484e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2494e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
2509e69d7d0SLori Alt 	drrfo->drr_firstobj = firstobj;
2519e69d7d0SLori Alt 	drrfo->drr_numobjs = numobjs;
2524e3c9f44SBill Pijewski 	drrfo->drr_toguid = dsp->dsa_toguid;
2539e69d7d0SLori Alt 
2544e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
255efb80947Sahrens 
256efb80947Sahrens 	return (0);
257efb80947Sahrens }
258efb80947Sahrens 
259efb80947Sahrens static int
2604e3c9f44SBill Pijewski dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
261efb80947Sahrens {
2624e3c9f44SBill Pijewski 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
2639e69d7d0SLori Alt 
264efb80947Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
2654e3c9f44SBill Pijewski 		return (dump_freeobjects(dsp, object, 1));
266efb80947Sahrens 
2674e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
2684e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
2694e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
270be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2714e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2729e69d7d0SLori Alt 	}
2739e69d7d0SLori Alt 
274efb80947Sahrens 	/* write an OBJECT record */
2754e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2764e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_OBJECT;
2779e69d7d0SLori Alt 	drro->drr_object = object;
2789e69d7d0SLori Alt 	drro->drr_type = dnp->dn_type;
2799e69d7d0SLori Alt 	drro->drr_bonustype = dnp->dn_bonustype;
2809e69d7d0SLori Alt 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
2819e69d7d0SLori Alt 	drro->drr_bonuslen = dnp->dn_bonuslen;
2829e69d7d0SLori Alt 	drro->drr_checksumtype = dnp->dn_checksum;
2839e69d7d0SLori Alt 	drro->drr_compress = dnp->dn_compress;
2844e3c9f44SBill Pijewski 	drro->drr_toguid = dsp->dsa_toguid;
2859e69d7d0SLori Alt 
2864e3c9f44SBill Pijewski 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
287be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
288efb80947Sahrens 
2894e3c9f44SBill Pijewski 	if (dump_bytes(dsp, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0)
290be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
291efb80947Sahrens 
292efb80947Sahrens 	/* free anything past the end of the file */
2934e3c9f44SBill Pijewski 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
294efb80947Sahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL))
295be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
2963b2aab18SMatthew Ahrens 	if (dsp->dsa_err != 0)
297be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
298efb80947Sahrens 	return (0);
299efb80947Sahrens }
300efb80947Sahrens 
301efb80947Sahrens #define	BP_SPAN(dnp, level) \
302efb80947Sahrens 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
303efb80947Sahrens 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
304efb80947Sahrens 
305b24ab676SJeff Bonwick /* ARGSUSED */
306efb80947Sahrens static int
3071b912ec7SGeorge Wilson backup_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
308b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
309efb80947Sahrens {
3104e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = arg;
311efb80947Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
312efb80947Sahrens 	int err = 0;
313efb80947Sahrens 
314efb80947Sahrens 	if (issig(JUSTLOOKING) && issig(FORREAL))
315be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
316efb80947Sahrens 
317b24ab676SJeff Bonwick 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
318b24ab676SJeff Bonwick 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
31914843421SMatthew Ahrens 		return (0);
320b24ab676SJeff Bonwick 	} else if (bp == NULL && zb->zb_object == DMU_META_DNODE_OBJECT) {
32188b7b0f2SMatthew Ahrens 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
32288b7b0f2SMatthew Ahrens 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
3234e3c9f44SBill Pijewski 		err = dump_freeobjects(dsp, dnobj, span >> DNODE_SHIFT);
324efb80947Sahrens 	} else if (bp == NULL) {
32588b7b0f2SMatthew Ahrens 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
3264e3c9f44SBill Pijewski 		err = dump_free(dsp, zb->zb_object, zb->zb_blkid * span, span);
32788b7b0f2SMatthew Ahrens 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
32888b7b0f2SMatthew Ahrens 		return (0);
32988b7b0f2SMatthew Ahrens 	} else if (type == DMU_OT_DNODE) {
33088b7b0f2SMatthew Ahrens 		dnode_phys_t *blk;
331efb80947Sahrens 		int i;
332efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
33388b7b0f2SMatthew Ahrens 		uint32_t aflags = ARC_WAIT;
33488b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
335efb80947Sahrens 
3361b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
3371b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
3381b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
339be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
34088b7b0f2SMatthew Ahrens 
34188b7b0f2SMatthew Ahrens 		blk = abuf->b_data;
342efb80947Sahrens 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
34388b7b0f2SMatthew Ahrens 			uint64_t dnobj = (zb->zb_blkid <<
34488b7b0f2SMatthew Ahrens 			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
3454e3c9f44SBill Pijewski 			err = dump_dnode(dsp, dnobj, blk+i);
3463b2aab18SMatthew Ahrens 			if (err != 0)
347efb80947Sahrens 				break;
348efb80947Sahrens 		}
34988b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
3500a586ceaSMark Shellenbaum 	} else if (type == DMU_OT_SA) {
3510a586ceaSMark Shellenbaum 		uint32_t aflags = ARC_WAIT;
3520a586ceaSMark Shellenbaum 		arc_buf_t *abuf;
3530a586ceaSMark Shellenbaum 		int blksz = BP_GET_LSIZE(bp);
3540a586ceaSMark Shellenbaum 
3551b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
3561b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
3571b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
358be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
3590a586ceaSMark Shellenbaum 
3604e3c9f44SBill Pijewski 		err = dump_spill(dsp, zb->zb_object, blksz, abuf->b_data);
3610a586ceaSMark Shellenbaum 		(void) arc_buf_remove_ref(abuf, &abuf);
36288b7b0f2SMatthew Ahrens 	} else { /* it's a level-0 block of a regular object */
36388b7b0f2SMatthew Ahrens 		uint32_t aflags = ARC_WAIT;
36488b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
365efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
36688b7b0f2SMatthew Ahrens 
3671b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
3681b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
3691b912ec7SGeorge Wilson 		    &aflags, zb) != 0) {
37019b94df9SMatthew Ahrens 			if (zfs_send_corrupt_data) {
37119b94df9SMatthew Ahrens 				/* Send a block filled with 0x"zfs badd bloc" */
37219b94df9SMatthew Ahrens 				abuf = arc_buf_alloc(spa, blksz, &abuf,
37319b94df9SMatthew Ahrens 				    ARC_BUFC_DATA);
37419b94df9SMatthew Ahrens 				uint64_t *ptr;
37519b94df9SMatthew Ahrens 				for (ptr = abuf->b_data;
37619b94df9SMatthew Ahrens 				    (char *)ptr < (char *)abuf->b_data + blksz;
37719b94df9SMatthew Ahrens 				    ptr++)
37819b94df9SMatthew Ahrens 					*ptr = 0x2f5baddb10c;
37919b94df9SMatthew Ahrens 			} else {
380be6fd75aSMatthew Ahrens 				return (SET_ERROR(EIO));
38119b94df9SMatthew Ahrens 			}
38219b94df9SMatthew Ahrens 		}
38388b7b0f2SMatthew Ahrens 
3844e3c9f44SBill Pijewski 		err = dump_data(dsp, type, zb->zb_object, zb->zb_blkid * blksz,
3858e714474SLori Alt 		    blksz, bp, abuf->b_data);
38688b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
387efb80947Sahrens 	}
388efb80947Sahrens 
389efb80947Sahrens 	ASSERT(err == 0 || err == EINTR);
390efb80947Sahrens 	return (err);
391efb80947Sahrens }
392efb80947Sahrens 
3934445fffbSMatthew Ahrens /*
3943b2aab18SMatthew Ahrens  * Releases dp, ds, and fromds, using the specified tag.
3954445fffbSMatthew Ahrens  */
3963b2aab18SMatthew Ahrens static int
3973b2aab18SMatthew Ahrens dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *ds,
3983b2aab18SMatthew Ahrens     dsl_dataset_t *fromds, int outfd, vnode_t *vp, offset_t *off)
399efb80947Sahrens {
4003b2aab18SMatthew Ahrens 	objset_t *os;
401efb80947Sahrens 	dmu_replay_record_t *drr;
4024e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp;
403efb80947Sahrens 	int err;
4043cb34c60Sahrens 	uint64_t fromtxg = 0;
405efb80947Sahrens 
4063b2aab18SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds)) {
4073b2aab18SMatthew Ahrens 		dsl_dataset_rele(fromds, tag);
4083b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, tag);
4093b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
410be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
4113b2aab18SMatthew Ahrens 	}
4123b2aab18SMatthew Ahrens 
4133b2aab18SMatthew Ahrens 	err = dmu_objset_from_ds(ds, &os);
4143b2aab18SMatthew Ahrens 	if (err != 0) {
4153b2aab18SMatthew Ahrens 		if (fromds != NULL)
4163b2aab18SMatthew Ahrens 			dsl_dataset_rele(fromds, tag);
4173b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, tag);
4183b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
4193b2aab18SMatthew Ahrens 		return (err);
4203b2aab18SMatthew Ahrens 	}
421efb80947Sahrens 
422efb80947Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
423efb80947Sahrens 	drr->drr_type = DRR_BEGIN;
424efb80947Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
4259e69d7d0SLori Alt 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
4269e69d7d0SLori Alt 	    DMU_SUBSTREAM);
427dc7cd546SMark Shellenbaum 
428dc7cd546SMark Shellenbaum #ifdef _KERNEL
4293b2aab18SMatthew Ahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
430dc7cd546SMark Shellenbaum 		uint64_t version;
4313b2aab18SMatthew Ahrens 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
4324e3c9f44SBill Pijewski 			kmem_free(drr, sizeof (dmu_replay_record_t));
4333b2aab18SMatthew Ahrens 			if (fromds != NULL)
4343b2aab18SMatthew Ahrens 				dsl_dataset_rele(fromds, tag);
4353b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, tag);
4363b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, tag);
437be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4384e3c9f44SBill Pijewski 		}
4393b2aab18SMatthew Ahrens 		if (version >= ZPL_VERSION_SA) {
440dc7cd546SMark Shellenbaum 			DMU_SET_FEATUREFLAGS(
441dc7cd546SMark Shellenbaum 			    drr->drr_u.drr_begin.drr_versioninfo,
442dc7cd546SMark Shellenbaum 			    DMU_BACKUP_FEATURE_SA_SPILL);
443dc7cd546SMark Shellenbaum 		}
444dc7cd546SMark Shellenbaum 	}
445dc7cd546SMark Shellenbaum #endif
446dc7cd546SMark Shellenbaum 
447efb80947Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
448efb80947Sahrens 	    ds->ds_phys->ds_creation_time;
4493b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
4504445fffbSMatthew Ahrens 	if (fromds != NULL && ds->ds_dir != fromds->ds_dir)
4513cb34c60Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
452efb80947Sahrens 	drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
453ab04eb8eStimh 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
454ab04eb8eStimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
455ab04eb8eStimh 
4563b2aab18SMatthew Ahrens 	if (fromds != NULL)
457efb80947Sahrens 		drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid;
458efb80947Sahrens 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
459efb80947Sahrens 
4603b2aab18SMatthew Ahrens 	if (fromds != NULL) {
4613cb34c60Sahrens 		fromtxg = fromds->ds_phys->ds_creation_txg;
4623b2aab18SMatthew Ahrens 		dsl_dataset_rele(fromds, tag);
4633b2aab18SMatthew Ahrens 		fromds = NULL;
4643b2aab18SMatthew Ahrens 	}
4653cb34c60Sahrens 
4664e3c9f44SBill Pijewski 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
4674e3c9f44SBill Pijewski 
4684e3c9f44SBill Pijewski 	dsp->dsa_drr = drr;
4694e3c9f44SBill Pijewski 	dsp->dsa_vp = vp;
4704e3c9f44SBill Pijewski 	dsp->dsa_outfd = outfd;
4714e3c9f44SBill Pijewski 	dsp->dsa_proc = curproc;
4723b2aab18SMatthew Ahrens 	dsp->dsa_os = os;
4734e3c9f44SBill Pijewski 	dsp->dsa_off = off;
4744e3c9f44SBill Pijewski 	dsp->dsa_toguid = ds->ds_phys->ds_guid;
4754e3c9f44SBill Pijewski 	ZIO_SET_CHECKSUM(&dsp->dsa_zc, 0, 0, 0, 0);
4764e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_NONE;
4774e3c9f44SBill Pijewski 
4784e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
4794e3c9f44SBill Pijewski 	list_insert_head(&ds->ds_sendstreams, dsp);
4804e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
4814e3c9f44SBill Pijewski 
482de8d9cffSMatthew Ahrens 	dsl_dataset_long_hold(ds, FTAG);
483de8d9cffSMatthew Ahrens 	dsl_pool_rele(dp, tag);
484de8d9cffSMatthew Ahrens 
4854e3c9f44SBill Pijewski 	if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) {
4864e3c9f44SBill Pijewski 		err = dsp->dsa_err;
4874e3c9f44SBill Pijewski 		goto out;
488efb80947Sahrens 	}
489efb80947Sahrens 
49088b7b0f2SMatthew Ahrens 	err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH,
4914e3c9f44SBill Pijewski 	    backup_cb, dsp);
492efb80947Sahrens 
4934e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE)
4944e3c9f44SBill Pijewski 		if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0)
495be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINTR);
4969e69d7d0SLori Alt 
4973b2aab18SMatthew Ahrens 	if (err != 0) {
4983b2aab18SMatthew Ahrens 		if (err == EINTR && dsp->dsa_err != 0)
4994e3c9f44SBill Pijewski 			err = dsp->dsa_err;
5004e3c9f44SBill Pijewski 		goto out;
501efb80947Sahrens 	}
502efb80947Sahrens 
503efb80947Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
504efb80947Sahrens 	drr->drr_type = DRR_END;
5054e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
5064e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
507efb80947Sahrens 
5084e3c9f44SBill Pijewski 	if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) {
5094e3c9f44SBill Pijewski 		err = dsp->dsa_err;
5104e3c9f44SBill Pijewski 		goto out;
5117b5309bbSgw 	}
512efb80947Sahrens 
5134e3c9f44SBill Pijewski out:
5144e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
5154e3c9f44SBill Pijewski 	list_remove(&ds->ds_sendstreams, dsp);
5164e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
5174e3c9f44SBill Pijewski 
518efb80947Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
5194e3c9f44SBill Pijewski 	kmem_free(dsp, sizeof (dmu_sendarg_t));
520efb80947Sahrens 
5213b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, FTAG);
5223b2aab18SMatthew Ahrens 	dsl_dataset_rele(ds, tag);
5233b2aab18SMatthew Ahrens 
5244e3c9f44SBill Pijewski 	return (err);
525efb80947Sahrens }
526efb80947Sahrens 
52719b94df9SMatthew Ahrens int
5283b2aab18SMatthew Ahrens dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
5293b2aab18SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
5303b2aab18SMatthew Ahrens {
5313b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
5323b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
5333b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
5343b2aab18SMatthew Ahrens 	int err;
5353b2aab18SMatthew Ahrens 
5363b2aab18SMatthew Ahrens 	err = dsl_pool_hold(pool, FTAG, &dp);
5373b2aab18SMatthew Ahrens 	if (err != 0)
5383b2aab18SMatthew Ahrens 		return (err);
5393b2aab18SMatthew Ahrens 
5403b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
5413b2aab18SMatthew Ahrens 	if (err != 0) {
5423b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
5433b2aab18SMatthew Ahrens 		return (err);
5443b2aab18SMatthew Ahrens 	}
5453b2aab18SMatthew Ahrens 
5463b2aab18SMatthew Ahrens 	if (fromsnap != 0) {
5473b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
5483b2aab18SMatthew Ahrens 		if (err != 0) {
5493b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
5503b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
5513b2aab18SMatthew Ahrens 			return (err);
5523b2aab18SMatthew Ahrens 		}
5533b2aab18SMatthew Ahrens 	}
5543b2aab18SMatthew Ahrens 
5553b2aab18SMatthew Ahrens 	return (dmu_send_impl(FTAG, dp, ds, fromds, outfd, vp, off));
5563b2aab18SMatthew Ahrens }
5573b2aab18SMatthew Ahrens 
5583b2aab18SMatthew Ahrens int
5593b2aab18SMatthew Ahrens dmu_send(const char *tosnap, const char *fromsnap,
5603b2aab18SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
5613b2aab18SMatthew Ahrens {
5623b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
5633b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
5643b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
5653b2aab18SMatthew Ahrens 	int err;
5663b2aab18SMatthew Ahrens 
5673b2aab18SMatthew Ahrens 	if (strchr(tosnap, '@') == NULL)
568be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5693b2aab18SMatthew Ahrens 	if (fromsnap != NULL && strchr(fromsnap, '@') == NULL)
570be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
5713b2aab18SMatthew Ahrens 
5723b2aab18SMatthew Ahrens 	err = dsl_pool_hold(tosnap, FTAG, &dp);
5733b2aab18SMatthew Ahrens 	if (err != 0)
5743b2aab18SMatthew Ahrens 		return (err);
5753b2aab18SMatthew Ahrens 
5763b2aab18SMatthew Ahrens 	err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
5773b2aab18SMatthew Ahrens 	if (err != 0) {
5783b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
5793b2aab18SMatthew Ahrens 		return (err);
5803b2aab18SMatthew Ahrens 	}
5813b2aab18SMatthew Ahrens 
5823b2aab18SMatthew Ahrens 	if (fromsnap != NULL) {
5833b2aab18SMatthew Ahrens 		err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
5843b2aab18SMatthew Ahrens 		if (err != 0) {
5853b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
5863b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
5873b2aab18SMatthew Ahrens 			return (err);
5883b2aab18SMatthew Ahrens 		}
5893b2aab18SMatthew Ahrens 	}
5903b2aab18SMatthew Ahrens 	return (dmu_send_impl(FTAG, dp, ds, fromds, outfd, vp, off));
5913b2aab18SMatthew Ahrens }
5923b2aab18SMatthew Ahrens 
5933b2aab18SMatthew Ahrens int
5943b2aab18SMatthew Ahrens dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
59519b94df9SMatthew Ahrens {
59619b94df9SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
59719b94df9SMatthew Ahrens 	int err;
59819b94df9SMatthew Ahrens 	uint64_t size;
59919b94df9SMatthew Ahrens 
6003b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
6013b2aab18SMatthew Ahrens 
60219b94df9SMatthew Ahrens 	/* tosnap must be a snapshot */
6033b2aab18SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds))
604be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
60519b94df9SMatthew Ahrens 
6064445fffbSMatthew Ahrens 	/*
6074445fffbSMatthew Ahrens 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
6084445fffbSMatthew Ahrens 	 * or the origin's fs.
6094445fffbSMatthew Ahrens 	 */
6103b2aab18SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds))
611be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
61219b94df9SMatthew Ahrens 
61319b94df9SMatthew Ahrens 	/* Get uncompressed size estimate of changed data. */
61419b94df9SMatthew Ahrens 	if (fromds == NULL) {
61519b94df9SMatthew Ahrens 		size = ds->ds_phys->ds_uncompressed_bytes;
61619b94df9SMatthew Ahrens 	} else {
61719b94df9SMatthew Ahrens 		uint64_t used, comp;
61819b94df9SMatthew Ahrens 		err = dsl_dataset_space_written(fromds, ds,
61919b94df9SMatthew Ahrens 		    &used, &comp, &size);
6203b2aab18SMatthew Ahrens 		if (err != 0)
62119b94df9SMatthew Ahrens 			return (err);
62219b94df9SMatthew Ahrens 	}
62319b94df9SMatthew Ahrens 
62419b94df9SMatthew Ahrens 	/*
62519b94df9SMatthew Ahrens 	 * Assume that space (both on-disk and in-stream) is dominated by
62619b94df9SMatthew Ahrens 	 * data.  We will adjust for indirect blocks and the copies property,
62719b94df9SMatthew Ahrens 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
62819b94df9SMatthew Ahrens 	 */
62919b94df9SMatthew Ahrens 
63019b94df9SMatthew Ahrens 	/*
63119b94df9SMatthew Ahrens 	 * Subtract out approximate space used by indirect blocks.
63219b94df9SMatthew Ahrens 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
63319b94df9SMatthew Ahrens 	 * Assume all blocks are recordsize.  Assume ditto blocks and
63419b94df9SMatthew Ahrens 	 * internal fragmentation counter out compression.
63519b94df9SMatthew Ahrens 	 *
63619b94df9SMatthew Ahrens 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
63719b94df9SMatthew Ahrens 	 * block, which we observe in practice.
63819b94df9SMatthew Ahrens 	 */
63919b94df9SMatthew Ahrens 	uint64_t recordsize;
6403b2aab18SMatthew Ahrens 	err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
6413b2aab18SMatthew Ahrens 	if (err != 0)
64219b94df9SMatthew Ahrens 		return (err);
64319b94df9SMatthew Ahrens 	size -= size / recordsize * sizeof (blkptr_t);
64419b94df9SMatthew Ahrens 
64519b94df9SMatthew Ahrens 	/* Add in the space for the record associated with each block. */
64619b94df9SMatthew Ahrens 	size += size / recordsize * sizeof (dmu_replay_record_t);
64719b94df9SMatthew Ahrens 
64819b94df9SMatthew Ahrens 	*sizep = size;
64919b94df9SMatthew Ahrens 
65019b94df9SMatthew Ahrens 	return (0);
65119b94df9SMatthew Ahrens }
65219b94df9SMatthew Ahrens 
6533b2aab18SMatthew Ahrens typedef struct dmu_recv_begin_arg {
6543b2aab18SMatthew Ahrens 	const char *drba_origin;
6553b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drba_cookie;
6563b2aab18SMatthew Ahrens 	cred_t *drba_cred;
6573b2aab18SMatthew Ahrens } dmu_recv_begin_arg_t;
658f18faf3fSek 
659f18faf3fSek static int
6603b2aab18SMatthew Ahrens recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
6613b2aab18SMatthew Ahrens     uint64_t fromguid)
662f18faf3fSek {
6633cb34c60Sahrens 	uint64_t val;
6643b2aab18SMatthew Ahrens 	int error;
6653b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
666f18faf3fSek 
6673cb34c60Sahrens 	/* must not have any changes since most recent snapshot */
6683b2aab18SMatthew Ahrens 	if (!drba->drba_cookie->drc_force &&
6693b2aab18SMatthew Ahrens 	    dsl_dataset_modified_since_lastsnap(ds))
670be6fd75aSMatthew Ahrens 		return (SET_ERROR(ETXTBSY));
671f18faf3fSek 
6723b2aab18SMatthew Ahrens 	/* temporary clone name must not exist */
6733b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
6743b2aab18SMatthew Ahrens 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj, recv_clone_name,
6753b2aab18SMatthew Ahrens 	    8, 1, &val);
6763b2aab18SMatthew Ahrens 	if (error != ENOENT)
6773b2aab18SMatthew Ahrens 		return (error == 0 ? EBUSY : error);
6783b2aab18SMatthew Ahrens 
679feaa74e4SMark Maybee 	/* new snapshot name must not exist */
6803b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
6813b2aab18SMatthew Ahrens 	    ds->ds_phys->ds_snapnames_zapobj, drba->drba_cookie->drc_tosnap,
6823b2aab18SMatthew Ahrens 	    8, 1, &val);
6833b2aab18SMatthew Ahrens 	if (error != ENOENT)
6843b2aab18SMatthew Ahrens 		return (error == 0 ? EEXIST : error);
685feaa74e4SMark Maybee 
6863b2aab18SMatthew Ahrens 	if (fromguid != 0) {
687ae46e4c7SMatthew Ahrens 		/* if incremental, most recent snapshot must match fromguid */
688ae46e4c7SMatthew Ahrens 		if (ds->ds_prev == NULL)
689be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
69092241e0bSTom Erickson 
69192241e0bSTom Erickson 		/*
69292241e0bSTom Erickson 		 * most recent snapshot must match fromguid, or there are no
69392241e0bSTom Erickson 		 * changes since the fromguid one
69492241e0bSTom Erickson 		 */
6953b2aab18SMatthew Ahrens 		if (ds->ds_prev->ds_phys->ds_guid != fromguid) {
69692241e0bSTom Erickson 			uint64_t birth = ds->ds_prev->ds_phys->ds_bp.blk_birth;
69792241e0bSTom Erickson 			uint64_t obj = ds->ds_prev->ds_phys->ds_prev_snap_obj;
69892241e0bSTom Erickson 			while (obj != 0) {
69992241e0bSTom Erickson 				dsl_dataset_t *snap;
7003b2aab18SMatthew Ahrens 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
7013b2aab18SMatthew Ahrens 				    &snap);
7023b2aab18SMatthew Ahrens 				if (error != 0)
703be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENODEV));
70492241e0bSTom Erickson 				if (snap->ds_phys->ds_creation_txg < birth) {
70592241e0bSTom Erickson 					dsl_dataset_rele(snap, FTAG);
706be6fd75aSMatthew Ahrens 					return (SET_ERROR(ENODEV));
70792241e0bSTom Erickson 				}
7083b2aab18SMatthew Ahrens 				if (snap->ds_phys->ds_guid == fromguid) {
70992241e0bSTom Erickson 					dsl_dataset_rele(snap, FTAG);
71092241e0bSTom Erickson 					break; /* it's ok */
71192241e0bSTom Erickson 				}
71292241e0bSTom Erickson 				obj = snap->ds_phys->ds_prev_snap_obj;
71392241e0bSTom Erickson 				dsl_dataset_rele(snap, FTAG);
71492241e0bSTom Erickson 			}
71592241e0bSTom Erickson 			if (obj == 0)
716be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
71792241e0bSTom Erickson 		}
718ae46e4c7SMatthew Ahrens 	} else {
719ae46e4c7SMatthew Ahrens 		/* if full, most recent snapshot must be $ORIGIN */
720ae46e4c7SMatthew Ahrens 		if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL)
721be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
722ae46e4c7SMatthew Ahrens 	}
7233cb34c60Sahrens 
7243cb34c60Sahrens 	return (0);
7253b2aab18SMatthew Ahrens 
7263b2aab18SMatthew Ahrens }
7273b2aab18SMatthew Ahrens 
7283b2aab18SMatthew Ahrens static int
7293b2aab18SMatthew Ahrens dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
7303b2aab18SMatthew Ahrens {
7313b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
7323b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
7333b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
7343b2aab18SMatthew Ahrens 	uint64_t fromguid = drrb->drr_fromguid;
7353b2aab18SMatthew Ahrens 	int flags = drrb->drr_flags;
7363b2aab18SMatthew Ahrens 	int error;
7373b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
7383b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
7393b2aab18SMatthew Ahrens 
7403b2aab18SMatthew Ahrens 	/* already checked */
7413b2aab18SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
7423b2aab18SMatthew Ahrens 
7433b2aab18SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
7443b2aab18SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
7453b2aab18SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
7463b2aab18SMatthew Ahrens 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
747be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
7483b2aab18SMatthew Ahrens 
7493b2aab18SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
7503b2aab18SMatthew Ahrens 	if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
7513b2aab18SMatthew Ahrens 	    DMU_BACKUP_FEATURE_SA_SPILL) &&
7523b2aab18SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA) {
753be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
7543b2aab18SMatthew Ahrens 	}
7553b2aab18SMatthew Ahrens 
7563b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
7573b2aab18SMatthew Ahrens 	if (error == 0) {
7583b2aab18SMatthew Ahrens 		/* target fs already exists; recv into temp clone */
7593b2aab18SMatthew Ahrens 
7603b2aab18SMatthew Ahrens 		/* Can't recv a clone into an existing fs */
7613b2aab18SMatthew Ahrens 		if (flags & DRR_FLAG_CLONE) {
7623b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
763be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
7643b2aab18SMatthew Ahrens 		}
7653b2aab18SMatthew Ahrens 
7663b2aab18SMatthew Ahrens 		error = recv_begin_check_existing_impl(drba, ds, fromguid);
7673b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
7683b2aab18SMatthew Ahrens 	} else if (error == ENOENT) {
7693b2aab18SMatthew Ahrens 		/* target fs does not exist; must be a full backup or clone */
7703b2aab18SMatthew Ahrens 		char buf[MAXNAMELEN];
7713b2aab18SMatthew Ahrens 
7723b2aab18SMatthew Ahrens 		/*
7733b2aab18SMatthew Ahrens 		 * If it's a non-clone incremental, we are missing the
7743b2aab18SMatthew Ahrens 		 * target fs, so fail the recv.
7753b2aab18SMatthew Ahrens 		 */
7763b2aab18SMatthew Ahrens 		if (fromguid != 0 && !(flags & DRR_FLAG_CLONE))
777be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
7783b2aab18SMatthew Ahrens 
7793b2aab18SMatthew Ahrens 		/* Open the parent of tofs */
7803b2aab18SMatthew Ahrens 		ASSERT3U(strlen(tofs), <, MAXNAMELEN);
7813b2aab18SMatthew Ahrens 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
7823b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
7833b2aab18SMatthew Ahrens 		if (error != 0)
7843b2aab18SMatthew Ahrens 			return (error);
7853b2aab18SMatthew Ahrens 
7863b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
7873b2aab18SMatthew Ahrens 			dsl_dataset_t *origin;
7883b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, drba->drba_origin,
7893b2aab18SMatthew Ahrens 			    FTAG, &origin);
7903b2aab18SMatthew Ahrens 			if (error != 0) {
7913b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
7923b2aab18SMatthew Ahrens 				return (error);
7933b2aab18SMatthew Ahrens 			}
7943b2aab18SMatthew Ahrens 			if (!dsl_dataset_is_snapshot(origin)) {
7953b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
7963b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
797be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
7983b2aab18SMatthew Ahrens 			}
7993b2aab18SMatthew Ahrens 			if (origin->ds_phys->ds_guid != fromguid) {
8003b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
8013b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
802be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
8033b2aab18SMatthew Ahrens 			}
8043b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
8053b2aab18SMatthew Ahrens 		}
8063b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
8073b2aab18SMatthew Ahrens 		error = 0;
8083b2aab18SMatthew Ahrens 	}
8093b2aab18SMatthew Ahrens 	return (error);
810f18faf3fSek }
811f18faf3fSek 
812f18faf3fSek static void
8133b2aab18SMatthew Ahrens dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
814f18faf3fSek {
8153b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
8163b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8173b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
8183b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
8193b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *newds;
820f18faf3fSek 	uint64_t dsobj;
8213b2aab18SMatthew Ahrens 	int error;
8223b2aab18SMatthew Ahrens 	uint64_t crflags;
8233b2aab18SMatthew Ahrens 
8243b2aab18SMatthew Ahrens 	crflags = (drrb->drr_flags & DRR_FLAG_CI_DATA) ?
8253b2aab18SMatthew Ahrens 	    DS_FLAG_CI_DATASET : 0;
826f18faf3fSek 
8273b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
8283b2aab18SMatthew Ahrens 	if (error == 0) {
8293b2aab18SMatthew Ahrens 		/* create temporary clone */
8303b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
8313b2aab18SMatthew Ahrens 		    ds->ds_prev, crflags, drba->drba_cred, tx);
8323b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
8333b2aab18SMatthew Ahrens 	} else {
8343b2aab18SMatthew Ahrens 		dsl_dir_t *dd;
8353b2aab18SMatthew Ahrens 		const char *tail;
8363b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
8373b2aab18SMatthew Ahrens 
8383b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
8393b2aab18SMatthew Ahrens 
8403b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
8413b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
8423b2aab18SMatthew Ahrens 			    FTAG, &origin));
8433b2aab18SMatthew Ahrens 		}
8443b2aab18SMatthew Ahrens 
8453b2aab18SMatthew Ahrens 		/* Create new dataset. */
8463b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(dd,
8473b2aab18SMatthew Ahrens 		    strrchr(tofs, '/') + 1,
8483b2aab18SMatthew Ahrens 		    origin, crflags, drba->drba_cred, tx);
8493b2aab18SMatthew Ahrens 		if (origin != NULL)
8503b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
8513b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
8523b2aab18SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
8533b2aab18SMatthew Ahrens 	}
8543b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
8553b2aab18SMatthew Ahrens 
8563b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
8573b2aab18SMatthew Ahrens 	newds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
8583cb34c60Sahrens 
859ae46e4c7SMatthew Ahrens 	/*
860ae46e4c7SMatthew Ahrens 	 * If we actually created a non-clone, we need to create the
861ae46e4c7SMatthew Ahrens 	 * objset in our new dataset.
862ae46e4c7SMatthew Ahrens 	 */
8633b2aab18SMatthew Ahrens 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
864ae46e4c7SMatthew Ahrens 		(void) dmu_objset_create_impl(dp->dp_spa,
8653b2aab18SMatthew Ahrens 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
866ae46e4c7SMatthew Ahrens 	}
867ae46e4c7SMatthew Ahrens 
8683b2aab18SMatthew Ahrens 	drba->drba_cookie->drc_ds = newds;
869dc7cd546SMark Shellenbaum 
8703b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(newds, "receive", tx, "");
871dc7cd546SMark Shellenbaum }
872dc7cd546SMark Shellenbaum 
8733cb34c60Sahrens /*
8743cb34c60Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
8753cb34c60Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
8763cb34c60Sahrens  */
8773cb34c60Sahrens int
8783b2aab18SMatthew Ahrens dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
8793b2aab18SMatthew Ahrens     boolean_t force, char *origin, dmu_recv_cookie_t *drc)
880efb80947Sahrens {
8813b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t drba = { 0 };
8823b2aab18SMatthew Ahrens 	dmu_replay_record_t *drr;
883ab04eb8eStimh 
8843cb34c60Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
8853cb34c60Sahrens 	drc->drc_drrb = drrb;
8863cb34c60Sahrens 	drc->drc_tosnap = tosnap;
8873b2aab18SMatthew Ahrens 	drc->drc_tofs = tofs;
8883cb34c60Sahrens 	drc->drc_force = force;
889efb80947Sahrens 
8903b2aab18SMatthew Ahrens 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
8913b2aab18SMatthew Ahrens 		drc->drc_byteswap = B_TRUE;
8923b2aab18SMatthew Ahrens 	else if (drrb->drr_magic != DMU_BACKUP_MAGIC)
893be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
894efb80947Sahrens 
8953b2aab18SMatthew Ahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
8963b2aab18SMatthew Ahrens 	drr->drr_type = DRR_BEGIN;
8973b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin = *drc->drc_drrb;
8983b2aab18SMatthew Ahrens 	if (drc->drc_byteswap) {
8993b2aab18SMatthew Ahrens 		fletcher_4_incremental_byteswap(drr,
9003b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
9013b2aab18SMatthew Ahrens 	} else {
9023b2aab18SMatthew Ahrens 		fletcher_4_incremental_native(drr,
9033b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
9043b2aab18SMatthew Ahrens 	}
9053b2aab18SMatthew Ahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
906dc7cd546SMark Shellenbaum 
9073b2aab18SMatthew Ahrens 	if (drc->drc_byteswap) {
9083b2aab18SMatthew Ahrens 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
9093b2aab18SMatthew Ahrens 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
9103b2aab18SMatthew Ahrens 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
9113b2aab18SMatthew Ahrens 		drrb->drr_type = BSWAP_32(drrb->drr_type);
9123b2aab18SMatthew Ahrens 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
9133b2aab18SMatthew Ahrens 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
9143cb34c60Sahrens 	}
9153cb34c60Sahrens 
9163b2aab18SMatthew Ahrens 	drba.drba_origin = origin;
9173b2aab18SMatthew Ahrens 	drba.drba_cookie = drc;
9183b2aab18SMatthew Ahrens 	drba.drba_cred = CRED();
9193b2aab18SMatthew Ahrens 
9203b2aab18SMatthew Ahrens 	return (dsl_sync_task(tofs, dmu_recv_begin_check, dmu_recv_begin_sync,
9213b2aab18SMatthew Ahrens 	    &drba, 5));
922efb80947Sahrens }
923efb80947Sahrens 
9243cb34c60Sahrens struct restorearg {
9253cb34c60Sahrens 	int err;
9263b2aab18SMatthew Ahrens 	boolean_t byteswap;
9273cb34c60Sahrens 	vnode_t *vp;
9283cb34c60Sahrens 	char *buf;
9293cb34c60Sahrens 	uint64_t voff;
9303cb34c60Sahrens 	int bufsize; /* amount of memory allocated for buf */
9313cb34c60Sahrens 	zio_cksum_t cksum;
932c99e4bdcSChris Kirby 	avl_tree_t *guid_to_ds_map;
9333cb34c60Sahrens };
9343cb34c60Sahrens 
9359e69d7d0SLori Alt typedef struct guid_map_entry {
9369e69d7d0SLori Alt 	uint64_t	guid;
9379e69d7d0SLori Alt 	dsl_dataset_t	*gme_ds;
9389e69d7d0SLori Alt 	avl_node_t	avlnode;
9399e69d7d0SLori Alt } guid_map_entry_t;
9409e69d7d0SLori Alt 
9419e69d7d0SLori Alt static int
9429e69d7d0SLori Alt guid_compare(const void *arg1, const void *arg2)
9439e69d7d0SLori Alt {
9449e69d7d0SLori Alt 	const guid_map_entry_t *gmep1 = arg1;
9459e69d7d0SLori Alt 	const guid_map_entry_t *gmep2 = arg2;
9469e69d7d0SLori Alt 
9479e69d7d0SLori Alt 	if (gmep1->guid < gmep2->guid)
9489e69d7d0SLori Alt 		return (-1);
9499e69d7d0SLori Alt 	else if (gmep1->guid > gmep2->guid)
9509e69d7d0SLori Alt 		return (1);
9519e69d7d0SLori Alt 	return (0);
9529e69d7d0SLori Alt }
9539e69d7d0SLori Alt 
954c99e4bdcSChris Kirby static void
955c99e4bdcSChris Kirby free_guid_map_onexit(void *arg)
956c99e4bdcSChris Kirby {
957c99e4bdcSChris Kirby 	avl_tree_t *ca = arg;
958c99e4bdcSChris Kirby 	void *cookie = NULL;
959c99e4bdcSChris Kirby 	guid_map_entry_t *gmep;
960c99e4bdcSChris Kirby 
961c99e4bdcSChris Kirby 	while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
9623b2aab18SMatthew Ahrens 		dsl_dataset_long_rele(gmep->gme_ds, gmep);
963de8d9cffSMatthew Ahrens 		dsl_dataset_rele(gmep->gme_ds, gmep);
964c99e4bdcSChris Kirby 		kmem_free(gmep, sizeof (guid_map_entry_t));
965c99e4bdcSChris Kirby 	}
966c99e4bdcSChris Kirby 	avl_destroy(ca);
967c99e4bdcSChris Kirby 	kmem_free(ca, sizeof (avl_tree_t));
968c99e4bdcSChris Kirby }
969c99e4bdcSChris Kirby 
970efb80947Sahrens static void *
971efb80947Sahrens restore_read(struct restorearg *ra, int len)
972efb80947Sahrens {
973efb80947Sahrens 	void *rv;
9743cb34c60Sahrens 	int done = 0;
975efb80947Sahrens 
976efb80947Sahrens 	/* some things will require 8-byte alignment, so everything must */
977fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
978efb80947Sahrens 
9793cb34c60Sahrens 	while (done < len) {
980efb80947Sahrens 		ssize_t resid;
981efb80947Sahrens 
982efb80947Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
9833cb34c60Sahrens 		    (caddr_t)ra->buf + done, len - done,
984efb80947Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
985efb80947Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
986efb80947Sahrens 
9873cb34c60Sahrens 		if (resid == len - done)
988be6fd75aSMatthew Ahrens 			ra->err = SET_ERROR(EINVAL);
9893cb34c60Sahrens 		ra->voff += len - done - resid;
9903cb34c60Sahrens 		done = len - resid;
9913b2aab18SMatthew Ahrens 		if (ra->err != 0)
992efb80947Sahrens 			return (NULL);
993efb80947Sahrens 	}
994efb80947Sahrens 
9953cb34c60Sahrens 	ASSERT3U(done, ==, len);
9963cb34c60Sahrens 	rv = ra->buf;
997efb80947Sahrens 	if (ra->byteswap)
9983cb34c60Sahrens 		fletcher_4_incremental_byteswap(rv, len, &ra->cksum);
999efb80947Sahrens 	else
10003cb34c60Sahrens 		fletcher_4_incremental_native(rv, len, &ra->cksum);
1001efb80947Sahrens 	return (rv);
1002efb80947Sahrens }
1003efb80947Sahrens 
1004efb80947Sahrens static void
1005efb80947Sahrens backup_byteswap(dmu_replay_record_t *drr)
1006efb80947Sahrens {
1007efb80947Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1008efb80947Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1009efb80947Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
10103cb34c60Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
1011efb80947Sahrens 	switch (drr->drr_type) {
1012efb80947Sahrens 	case DRR_BEGIN:
1013efb80947Sahrens 		DO64(drr_begin.drr_magic);
10149e69d7d0SLori Alt 		DO64(drr_begin.drr_versioninfo);
1015efb80947Sahrens 		DO64(drr_begin.drr_creation_time);
1016efb80947Sahrens 		DO32(drr_begin.drr_type);
10173cb34c60Sahrens 		DO32(drr_begin.drr_flags);
1018efb80947Sahrens 		DO64(drr_begin.drr_toguid);
1019efb80947Sahrens 		DO64(drr_begin.drr_fromguid);
1020efb80947Sahrens 		break;
1021efb80947Sahrens 	case DRR_OBJECT:
1022efb80947Sahrens 		DO64(drr_object.drr_object);
1023efb80947Sahrens 		/* DO64(drr_object.drr_allocation_txg); */
1024efb80947Sahrens 		DO32(drr_object.drr_type);
1025efb80947Sahrens 		DO32(drr_object.drr_bonustype);
1026efb80947Sahrens 		DO32(drr_object.drr_blksz);
1027efb80947Sahrens 		DO32(drr_object.drr_bonuslen);
10289e69d7d0SLori Alt 		DO64(drr_object.drr_toguid);
1029efb80947Sahrens 		break;
1030efb80947Sahrens 	case DRR_FREEOBJECTS:
1031efb80947Sahrens 		DO64(drr_freeobjects.drr_firstobj);
1032efb80947Sahrens 		DO64(drr_freeobjects.drr_numobjs);
10339e69d7d0SLori Alt 		DO64(drr_freeobjects.drr_toguid);
1034efb80947Sahrens 		break;
1035efb80947Sahrens 	case DRR_WRITE:
1036efb80947Sahrens 		DO64(drr_write.drr_object);
1037efb80947Sahrens 		DO32(drr_write.drr_type);
1038efb80947Sahrens 		DO64(drr_write.drr_offset);
1039efb80947Sahrens 		DO64(drr_write.drr_length);
10409e69d7d0SLori Alt 		DO64(drr_write.drr_toguid);
10418e714474SLori Alt 		DO64(drr_write.drr_key.ddk_cksum.zc_word[0]);
10428e714474SLori Alt 		DO64(drr_write.drr_key.ddk_cksum.zc_word[1]);
10438e714474SLori Alt 		DO64(drr_write.drr_key.ddk_cksum.zc_word[2]);
10448e714474SLori Alt 		DO64(drr_write.drr_key.ddk_cksum.zc_word[3]);
10458e714474SLori Alt 		DO64(drr_write.drr_key.ddk_prop);
10469e69d7d0SLori Alt 		break;
10479e69d7d0SLori Alt 	case DRR_WRITE_BYREF:
10489e69d7d0SLori Alt 		DO64(drr_write_byref.drr_object);
10499e69d7d0SLori Alt 		DO64(drr_write_byref.drr_offset);
10509e69d7d0SLori Alt 		DO64(drr_write_byref.drr_length);
10519e69d7d0SLori Alt 		DO64(drr_write_byref.drr_toguid);
10529e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refguid);
10539e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refobject);
10549e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refoffset);
10558e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[0]);
10568e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[1]);
10578e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[2]);
10588e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[3]);
10598e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_prop);
1060efb80947Sahrens 		break;
1061efb80947Sahrens 	case DRR_FREE:
1062efb80947Sahrens 		DO64(drr_free.drr_object);
1063efb80947Sahrens 		DO64(drr_free.drr_offset);
1064efb80947Sahrens 		DO64(drr_free.drr_length);
10659e69d7d0SLori Alt 		DO64(drr_free.drr_toguid);
1066efb80947Sahrens 		break;
10670a586ceaSMark Shellenbaum 	case DRR_SPILL:
10680a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_object);
10690a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_length);
10700a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_toguid);
10710a586ceaSMark Shellenbaum 		break;
1072efb80947Sahrens 	case DRR_END:
1073efb80947Sahrens 		DO64(drr_end.drr_checksum.zc_word[0]);
1074efb80947Sahrens 		DO64(drr_end.drr_checksum.zc_word[1]);
1075efb80947Sahrens 		DO64(drr_end.drr_checksum.zc_word[2]);
1076efb80947Sahrens 		DO64(drr_end.drr_checksum.zc_word[3]);
10779e69d7d0SLori Alt 		DO64(drr_end.drr_toguid);
1078efb80947Sahrens 		break;
1079efb80947Sahrens 	}
1080efb80947Sahrens #undef DO64
1081efb80947Sahrens #undef DO32
1082efb80947Sahrens }
1083efb80947Sahrens 
1084efb80947Sahrens static int
1085efb80947Sahrens restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
1086efb80947Sahrens {
1087efb80947Sahrens 	int err;
1088efb80947Sahrens 	dmu_tx_t *tx;
1089adee0b6fSTim Haley 	void *data = NULL;
1090efb80947Sahrens 
1091efb80947Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
1092ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_type) ||
1093ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
10949e69d7d0SLori Alt 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1095efb80947Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1096efb80947Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1097efb80947Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
1098efb80947Sahrens 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
1099efb80947Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1100be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1101efb80947Sahrens 	}
1102efb80947Sahrens 
11032bf405a2SMark Maybee 	err = dmu_object_info(os, drro->drr_object, NULL);
11042bf405a2SMark Maybee 
11052bf405a2SMark Maybee 	if (err != 0 && err != ENOENT)
1106be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
11072bf405a2SMark Maybee 
1108adee0b6fSTim Haley 	if (drro->drr_bonuslen) {
1109adee0b6fSTim Haley 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
11103b2aab18SMatthew Ahrens 		if (ra->err != 0)
1111adee0b6fSTim Haley 			return (ra->err);
1112adee0b6fSTim Haley 	}
1113adee0b6fSTim Haley 
1114efb80947Sahrens 	if (err == ENOENT) {
1115efb80947Sahrens 		/* currently free, want to be allocated */
11162bf405a2SMark Maybee 		tx = dmu_tx_create(os);
1117efb80947Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1118efb80947Sahrens 		err = dmu_tx_assign(tx, TXG_WAIT);
11193b2aab18SMatthew Ahrens 		if (err != 0) {
1120efb80947Sahrens 			dmu_tx_abort(tx);
1121efb80947Sahrens 			return (err);
1122efb80947Sahrens 		}
1123efb80947Sahrens 		err = dmu_object_claim(os, drro->drr_object,
1124efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
1125efb80947Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
11262bf405a2SMark Maybee 		dmu_tx_commit(tx);
1127efb80947Sahrens 	} else {
1128efb80947Sahrens 		/* currently allocated, want to be allocated */
1129efb80947Sahrens 		err = dmu_object_reclaim(os, drro->drr_object,
1130efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
11312bf405a2SMark Maybee 		    drro->drr_bonustype, drro->drr_bonuslen);
1132efb80947Sahrens 	}
11333b2aab18SMatthew Ahrens 	if (err != 0) {
1134be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
11350a586ceaSMark Shellenbaum 	}
11362bf405a2SMark Maybee 
11372bf405a2SMark Maybee 	tx = dmu_tx_create(os);
11382bf405a2SMark Maybee 	dmu_tx_hold_bonus(tx, drro->drr_object);
11392bf405a2SMark Maybee 	err = dmu_tx_assign(tx, TXG_WAIT);
11403b2aab18SMatthew Ahrens 	if (err != 0) {
11412bf405a2SMark Maybee 		dmu_tx_abort(tx);
11422bf405a2SMark Maybee 		return (err);
1143efb80947Sahrens 	}
1144efb80947Sahrens 
11459e69d7d0SLori Alt 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksumtype,
11469e69d7d0SLori Alt 	    tx);
1147efb80947Sahrens 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
1148efb80947Sahrens 
1149adee0b6fSTim Haley 	if (data != NULL) {
1150efb80947Sahrens 		dmu_buf_t *db;
1151adee0b6fSTim Haley 
1152efb80947Sahrens 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
1153efb80947Sahrens 		dmu_buf_will_dirty(db, tx);
1154efb80947Sahrens 
11551934e92fSmaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
11561934e92fSmaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
1157efb80947Sahrens 		if (ra->byteswap) {
1158ad135b5dSChristopher Siden 			dmu_object_byteswap_t byteswap =
1159ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
1160ad135b5dSChristopher Siden 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
1161efb80947Sahrens 			    drro->drr_bonuslen);
1162efb80947Sahrens 		}
1163efb80947Sahrens 		dmu_buf_rele(db, FTAG);
1164efb80947Sahrens 	}
1165efb80947Sahrens 	dmu_tx_commit(tx);
1166efb80947Sahrens 	return (0);
1167efb80947Sahrens }
1168efb80947Sahrens 
1169efb80947Sahrens /* ARGSUSED */
1170efb80947Sahrens static int
1171efb80947Sahrens restore_freeobjects(struct restorearg *ra, objset_t *os,
1172efb80947Sahrens     struct drr_freeobjects *drrfo)
1173efb80947Sahrens {
1174efb80947Sahrens 	uint64_t obj;
1175efb80947Sahrens 
1176efb80947Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
1177be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1178efb80947Sahrens 
1179efb80947Sahrens 	for (obj = drrfo->drr_firstobj;
1180432f72fdSahrens 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
1181432f72fdSahrens 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
1182efb80947Sahrens 		int err;
1183efb80947Sahrens 
1184efb80947Sahrens 		if (dmu_object_info(os, obj, NULL) != 0)
1185efb80947Sahrens 			continue;
1186efb80947Sahrens 
1187cdb0ab79Smaybee 		err = dmu_free_object(os, obj);
11883b2aab18SMatthew Ahrens 		if (err != 0)
1189efb80947Sahrens 			return (err);
1190efb80947Sahrens 	}
1191efb80947Sahrens 	return (0);
1192efb80947Sahrens }
1193efb80947Sahrens 
1194efb80947Sahrens static int
1195efb80947Sahrens restore_write(struct restorearg *ra, objset_t *os,
1196efb80947Sahrens     struct drr_write *drrw)
1197efb80947Sahrens {
1198efb80947Sahrens 	dmu_tx_t *tx;
1199efb80947Sahrens 	void *data;
1200efb80947Sahrens 	int err;
1201efb80947Sahrens 
1202efb80947Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
1203ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drrw->drr_type))
1204be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1205efb80947Sahrens 
1206efb80947Sahrens 	data = restore_read(ra, drrw->drr_length);
1207efb80947Sahrens 	if (data == NULL)
1208efb80947Sahrens 		return (ra->err);
1209efb80947Sahrens 
1210efb80947Sahrens 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
1211be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1212efb80947Sahrens 
1213efb80947Sahrens 	tx = dmu_tx_create(os);
1214efb80947Sahrens 
1215efb80947Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
1216efb80947Sahrens 	    drrw->drr_offset, drrw->drr_length);
1217efb80947Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
12183b2aab18SMatthew Ahrens 	if (err != 0) {
1219efb80947Sahrens 		dmu_tx_abort(tx);
1220efb80947Sahrens 		return (err);
1221efb80947Sahrens 	}
1222ad135b5dSChristopher Siden 	if (ra->byteswap) {
1223ad135b5dSChristopher Siden 		dmu_object_byteswap_t byteswap =
1224ad135b5dSChristopher Siden 		    DMU_OT_BYTESWAP(drrw->drr_type);
1225ad135b5dSChristopher Siden 		dmu_ot_byteswap[byteswap].ob_func(data, drrw->drr_length);
1226ad135b5dSChristopher Siden 	}
1227efb80947Sahrens 	dmu_write(os, drrw->drr_object,
1228efb80947Sahrens 	    drrw->drr_offset, drrw->drr_length, data, tx);
1229efb80947Sahrens 	dmu_tx_commit(tx);
1230efb80947Sahrens 	return (0);
1231efb80947Sahrens }
1232efb80947Sahrens 
12339e69d7d0SLori Alt /*
12349e69d7d0SLori Alt  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
12359e69d7d0SLori Alt  * streams to refer to a copy of the data that is already on the
12369e69d7d0SLori Alt  * system because it came in earlier in the stream.  This function
12379e69d7d0SLori Alt  * finds the earlier copy of the data, and uses that copy instead of
12389e69d7d0SLori Alt  * data from the stream to fulfill this write.
12399e69d7d0SLori Alt  */
12409e69d7d0SLori Alt static int
12419e69d7d0SLori Alt restore_write_byref(struct restorearg *ra, objset_t *os,
12429e69d7d0SLori Alt     struct drr_write_byref *drrwbr)
12439e69d7d0SLori Alt {
12449e69d7d0SLori Alt 	dmu_tx_t *tx;
12459e69d7d0SLori Alt 	int err;
12469e69d7d0SLori Alt 	guid_map_entry_t gmesrch;
12479e69d7d0SLori Alt 	guid_map_entry_t *gmep;
12489e69d7d0SLori Alt 	avl_index_t	where;
12499e69d7d0SLori Alt 	objset_t *ref_os = NULL;
12509e69d7d0SLori Alt 	dmu_buf_t *dbp;
12519e69d7d0SLori Alt 
12529e69d7d0SLori Alt 	if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
1253be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12549e69d7d0SLori Alt 
12559e69d7d0SLori Alt 	/*
12569e69d7d0SLori Alt 	 * If the GUID of the referenced dataset is different from the
12579e69d7d0SLori Alt 	 * GUID of the target dataset, find the referenced dataset.
12589e69d7d0SLori Alt 	 */
12599e69d7d0SLori Alt 	if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
12609e69d7d0SLori Alt 		gmesrch.guid = drrwbr->drr_refguid;
1261c99e4bdcSChris Kirby 		if ((gmep = avl_find(ra->guid_to_ds_map, &gmesrch,
12629e69d7d0SLori Alt 		    &where)) == NULL) {
1263be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
12649e69d7d0SLori Alt 		}
12659e69d7d0SLori Alt 		if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
1266be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
12679e69d7d0SLori Alt 	} else {
12689e69d7d0SLori Alt 		ref_os = os;
12699e69d7d0SLori Alt 	}
12709e69d7d0SLori Alt 
12719e69d7d0SLori Alt 	if (err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
127247cb52daSJeff Bonwick 	    drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH))
12739e69d7d0SLori Alt 		return (err);
12749e69d7d0SLori Alt 
12759e69d7d0SLori Alt 	tx = dmu_tx_create(os);
12769e69d7d0SLori Alt 
12779e69d7d0SLori Alt 	dmu_tx_hold_write(tx, drrwbr->drr_object,
12789e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length);
12799e69d7d0SLori Alt 	err = dmu_tx_assign(tx, TXG_WAIT);
12803b2aab18SMatthew Ahrens 	if (err != 0) {
12819e69d7d0SLori Alt 		dmu_tx_abort(tx);
12829e69d7d0SLori Alt 		return (err);
12839e69d7d0SLori Alt 	}
12849e69d7d0SLori Alt 	dmu_write(os, drrwbr->drr_object,
12859e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
12869e69d7d0SLori Alt 	dmu_buf_rele(dbp, FTAG);
12879e69d7d0SLori Alt 	dmu_tx_commit(tx);
12889e69d7d0SLori Alt 	return (0);
12899e69d7d0SLori Alt }
12909e69d7d0SLori Alt 
12910a586ceaSMark Shellenbaum static int
12920a586ceaSMark Shellenbaum restore_spill(struct restorearg *ra, objset_t *os, struct drr_spill *drrs)
12930a586ceaSMark Shellenbaum {
12940a586ceaSMark Shellenbaum 	dmu_tx_t *tx;
12950a586ceaSMark Shellenbaum 	void *data;
12960a586ceaSMark Shellenbaum 	dmu_buf_t *db, *db_spill;
12970a586ceaSMark Shellenbaum 	int err;
12980a586ceaSMark Shellenbaum 
12990a586ceaSMark Shellenbaum 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
13000a586ceaSMark Shellenbaum 	    drrs->drr_length > SPA_MAXBLOCKSIZE)
1301be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
13020a586ceaSMark Shellenbaum 
13030a586ceaSMark Shellenbaum 	data = restore_read(ra, drrs->drr_length);
13040a586ceaSMark Shellenbaum 	if (data == NULL)
13050a586ceaSMark Shellenbaum 		return (ra->err);
13060a586ceaSMark Shellenbaum 
13070a586ceaSMark Shellenbaum 	if (dmu_object_info(os, drrs->drr_object, NULL) != 0)
1308be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
13090a586ceaSMark Shellenbaum 
13100a586ceaSMark Shellenbaum 	VERIFY(0 == dmu_bonus_hold(os, drrs->drr_object, FTAG, &db));
13110a586ceaSMark Shellenbaum 	if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
13120a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
13130a586ceaSMark Shellenbaum 		return (err);
13140a586ceaSMark Shellenbaum 	}
13150a586ceaSMark Shellenbaum 
13160a586ceaSMark Shellenbaum 	tx = dmu_tx_create(os);
13170a586ceaSMark Shellenbaum 
13180a586ceaSMark Shellenbaum 	dmu_tx_hold_spill(tx, db->db_object);
13190a586ceaSMark Shellenbaum 
13200a586ceaSMark Shellenbaum 	err = dmu_tx_assign(tx, TXG_WAIT);
13213b2aab18SMatthew Ahrens 	if (err != 0) {
13220a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
13230a586ceaSMark Shellenbaum 		dmu_buf_rele(db_spill, FTAG);
13240a586ceaSMark Shellenbaum 		dmu_tx_abort(tx);
13250a586ceaSMark Shellenbaum 		return (err);
13260a586ceaSMark Shellenbaum 	}
13270a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(db_spill, tx);
13280a586ceaSMark Shellenbaum 
13290a586ceaSMark Shellenbaum 	if (db_spill->db_size < drrs->drr_length)
13300a586ceaSMark Shellenbaum 		VERIFY(0 == dbuf_spill_set_blksz(db_spill,
13310a586ceaSMark Shellenbaum 		    drrs->drr_length, tx));
13320a586ceaSMark Shellenbaum 	bcopy(data, db_spill->db_data, drrs->drr_length);
13330a586ceaSMark Shellenbaum 
13340a586ceaSMark Shellenbaum 	dmu_buf_rele(db, FTAG);
13350a586ceaSMark Shellenbaum 	dmu_buf_rele(db_spill, FTAG);
13360a586ceaSMark Shellenbaum 
13370a586ceaSMark Shellenbaum 	dmu_tx_commit(tx);
13380a586ceaSMark Shellenbaum 	return (0);
13390a586ceaSMark Shellenbaum }
13400a586ceaSMark Shellenbaum 
1341efb80947Sahrens /* ARGSUSED */
1342efb80947Sahrens static int
1343efb80947Sahrens restore_free(struct restorearg *ra, objset_t *os,
1344efb80947Sahrens     struct drr_free *drrf)
1345efb80947Sahrens {
1346efb80947Sahrens 	int err;
1347efb80947Sahrens 
1348efb80947Sahrens 	if (drrf->drr_length != -1ULL &&
1349efb80947Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
1350be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1351efb80947Sahrens 
1352efb80947Sahrens 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
1353be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1354efb80947Sahrens 
1355cdb0ab79Smaybee 	err = dmu_free_long_range(os, drrf->drr_object,
1356efb80947Sahrens 	    drrf->drr_offset, drrf->drr_length);
1357efb80947Sahrens 	return (err);
1358efb80947Sahrens }
1359efb80947Sahrens 
13603b2aab18SMatthew Ahrens /* used to destroy the drc_ds on error */
13613b2aab18SMatthew Ahrens static void
13623b2aab18SMatthew Ahrens dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
13633b2aab18SMatthew Ahrens {
13643b2aab18SMatthew Ahrens 	char name[MAXNAMELEN];
13653b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
13663b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
13673b2aab18SMatthew Ahrens 	(void) dsl_destroy_head(name);
13683b2aab18SMatthew Ahrens }
13693b2aab18SMatthew Ahrens 
13703cb34c60Sahrens /*
13713cb34c60Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
13723cb34c60Sahrens  */
1373efb80947Sahrens int
1374c99e4bdcSChris Kirby dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
1375c99e4bdcSChris Kirby     int cleanup_fd, uint64_t *action_handlep)
1376efb80947Sahrens {
13773cb34c60Sahrens 	struct restorearg ra = { 0 };
1378efb80947Sahrens 	dmu_replay_record_t *drr;
13793cb34c60Sahrens 	objset_t *os;
13803cb34c60Sahrens 	zio_cksum_t pcksum;
13819e69d7d0SLori Alt 	int featureflags;
1382efb80947Sahrens 
13833b2aab18SMatthew Ahrens 	ra.byteswap = drc->drc_byteswap;
13843b2aab18SMatthew Ahrens 	ra.cksum = drc->drc_cksum;
13853cb34c60Sahrens 	ra.vp = vp;
13863cb34c60Sahrens 	ra.voff = *voffp;
13873cb34c60Sahrens 	ra.bufsize = 1<<20;
13883cb34c60Sahrens 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
1389efb80947Sahrens 
13903cb34c60Sahrens 	/* these were verified in dmu_recv_begin */
13913b2aab18SMatthew Ahrens 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
13929e69d7d0SLori Alt 	    DMU_SUBSTREAM);
13933b2aab18SMatthew Ahrens 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
1394efb80947Sahrens 
1395efb80947Sahrens 	/*
1396efb80947Sahrens 	 * Open the objset we are modifying.
1397efb80947Sahrens 	 */
13983b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(drc->drc_ds, &os));
1399efb80947Sahrens 
14003b2aab18SMatthew Ahrens 	ASSERT(drc->drc_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
1401efb80947Sahrens 
14029e69d7d0SLori Alt 	featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
14039e69d7d0SLori Alt 
14049e69d7d0SLori Alt 	/* if this stream is dedup'ed, set up the avl tree for guid mapping */
14059e69d7d0SLori Alt 	if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
1406a7f53a56SChris Kirby 		minor_t minor;
1407a7f53a56SChris Kirby 
1408c99e4bdcSChris Kirby 		if (cleanup_fd == -1) {
1409be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EBADF);
1410c99e4bdcSChris Kirby 			goto out;
1411c99e4bdcSChris Kirby 		}
1412a7f53a56SChris Kirby 		ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
14133b2aab18SMatthew Ahrens 		if (ra.err != 0) {
1414a7f53a56SChris Kirby 			cleanup_fd = -1;
1415a7f53a56SChris Kirby 			goto out;
1416a7f53a56SChris Kirby 		}
1417a7f53a56SChris Kirby 
1418c99e4bdcSChris Kirby 		if (*action_handlep == 0) {
1419c99e4bdcSChris Kirby 			ra.guid_to_ds_map =
1420c99e4bdcSChris Kirby 			    kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
1421c99e4bdcSChris Kirby 			avl_create(ra.guid_to_ds_map, guid_compare,
1422c99e4bdcSChris Kirby 			    sizeof (guid_map_entry_t),
1423c99e4bdcSChris Kirby 			    offsetof(guid_map_entry_t, avlnode));
1424a7f53a56SChris Kirby 			ra.err = zfs_onexit_add_cb(minor,
1425c99e4bdcSChris Kirby 			    free_guid_map_onexit, ra.guid_to_ds_map,
1426c99e4bdcSChris Kirby 			    action_handlep);
14273b2aab18SMatthew Ahrens 			if (ra.err != 0)
1428c99e4bdcSChris Kirby 				goto out;
1429c99e4bdcSChris Kirby 		} else {
1430a7f53a56SChris Kirby 			ra.err = zfs_onexit_cb_data(minor, *action_handlep,
1431c99e4bdcSChris Kirby 			    (void **)&ra.guid_to_ds_map);
14323b2aab18SMatthew Ahrens 			if (ra.err != 0)
1433c99e4bdcSChris Kirby 				goto out;
1434c99e4bdcSChris Kirby 		}
1435ec5cf9d5SAlexander Stetsenko 
1436ec5cf9d5SAlexander Stetsenko 		drc->drc_guid_to_ds_map = ra.guid_to_ds_map;
14379e69d7d0SLori Alt 	}
14389e69d7d0SLori Alt 
1439efb80947Sahrens 	/*
1440efb80947Sahrens 	 * Read records and process them.
1441efb80947Sahrens 	 */
14423cb34c60Sahrens 	pcksum = ra.cksum;
1443efb80947Sahrens 	while (ra.err == 0 &&
1444efb80947Sahrens 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
1445efb80947Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
1446be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EINTR);
1447efb80947Sahrens 			goto out;
1448efb80947Sahrens 		}
1449efb80947Sahrens 
1450efb80947Sahrens 		if (ra.byteswap)
1451efb80947Sahrens 			backup_byteswap(drr);
1452efb80947Sahrens 
1453efb80947Sahrens 		switch (drr->drr_type) {
1454efb80947Sahrens 		case DRR_OBJECT:
1455efb80947Sahrens 		{
1456efb80947Sahrens 			/*
1457efb80947Sahrens 			 * We need to make a copy of the record header,
1458efb80947Sahrens 			 * because restore_{object,write} may need to
1459efb80947Sahrens 			 * restore_read(), which will invalidate drr.
1460efb80947Sahrens 			 */
1461efb80947Sahrens 			struct drr_object drro = drr->drr_u.drr_object;
1462efb80947Sahrens 			ra.err = restore_object(&ra, os, &drro);
1463efb80947Sahrens 			break;
1464efb80947Sahrens 		}
1465efb80947Sahrens 		case DRR_FREEOBJECTS:
1466efb80947Sahrens 		{
1467efb80947Sahrens 			struct drr_freeobjects drrfo =
1468efb80947Sahrens 			    drr->drr_u.drr_freeobjects;
1469efb80947Sahrens 			ra.err = restore_freeobjects(&ra, os, &drrfo);
1470efb80947Sahrens 			break;
1471efb80947Sahrens 		}
1472efb80947Sahrens 		case DRR_WRITE:
1473efb80947Sahrens 		{
1474efb80947Sahrens 			struct drr_write drrw = drr->drr_u.drr_write;
1475efb80947Sahrens 			ra.err = restore_write(&ra, os, &drrw);
1476efb80947Sahrens 			break;
1477efb80947Sahrens 		}
14789e69d7d0SLori Alt 		case DRR_WRITE_BYREF:
14799e69d7d0SLori Alt 		{
14809e69d7d0SLori Alt 			struct drr_write_byref drrwbr =
14819e69d7d0SLori Alt 			    drr->drr_u.drr_write_byref;
14829e69d7d0SLori Alt 			ra.err = restore_write_byref(&ra, os, &drrwbr);
14839e69d7d0SLori Alt 			break;
14849e69d7d0SLori Alt 		}
1485efb80947Sahrens 		case DRR_FREE:
1486efb80947Sahrens 		{
1487efb80947Sahrens 			struct drr_free drrf = drr->drr_u.drr_free;
1488efb80947Sahrens 			ra.err = restore_free(&ra, os, &drrf);
1489efb80947Sahrens 			break;
1490efb80947Sahrens 		}
1491efb80947Sahrens 		case DRR_END:
1492efb80947Sahrens 		{
1493efb80947Sahrens 			struct drr_end drre = drr->drr_u.drr_end;
1494efb80947Sahrens 			/*
1495efb80947Sahrens 			 * We compare against the *previous* checksum
1496efb80947Sahrens 			 * value, because the stored checksum is of
1497efb80947Sahrens 			 * everything before the DRR_END record.
1498efb80947Sahrens 			 */
1499137fa067Sahrens 			if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
1500be6fd75aSMatthew Ahrens 				ra.err = SET_ERROR(ECKSUM);
1501efb80947Sahrens 			goto out;
1502efb80947Sahrens 		}
15030a586ceaSMark Shellenbaum 		case DRR_SPILL:
15040a586ceaSMark Shellenbaum 		{
15050a586ceaSMark Shellenbaum 			struct drr_spill drrs = drr->drr_u.drr_spill;
15060a586ceaSMark Shellenbaum 			ra.err = restore_spill(&ra, os, &drrs);
15070a586ceaSMark Shellenbaum 			break;
15080a586ceaSMark Shellenbaum 		}
1509efb80947Sahrens 		default:
1510be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EINVAL);
1511efb80947Sahrens 			goto out;
1512efb80947Sahrens 		}
15133cb34c60Sahrens 		pcksum = ra.cksum;
1514efb80947Sahrens 	}
1515137fa067Sahrens 	ASSERT(ra.err != 0);
1516efb80947Sahrens 
1517efb80947Sahrens out:
1518a7f53a56SChris Kirby 	if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
1519a7f53a56SChris Kirby 		zfs_onexit_fd_rele(cleanup_fd);
1520a7f53a56SChris Kirby 
15213cb34c60Sahrens 	if (ra.err != 0) {
1522efb80947Sahrens 		/*
1523f4b94bdeSMatthew Ahrens 		 * destroy what we created, so we don't leave it in the
1524f4b94bdeSMatthew Ahrens 		 * inconsistent restoring state.
1525efb80947Sahrens 		 */
15263b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
1527efb80947Sahrens 	}
1528efb80947Sahrens 
1529efb80947Sahrens 	kmem_free(ra.buf, ra.bufsize);
15303cb34c60Sahrens 	*voffp = ra.voff;
1531efb80947Sahrens 	return (ra.err);
1532efb80947Sahrens }
1533f18faf3fSek 
15343cb34c60Sahrens static int
15353b2aab18SMatthew Ahrens dmu_recv_end_check(void *arg, dmu_tx_t *tx)
15363cb34c60Sahrens {
15373b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
15383b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15393b2aab18SMatthew Ahrens 	int error;
15403b2aab18SMatthew Ahrens 
15413b2aab18SMatthew Ahrens 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
15423cb34c60Sahrens 
15433b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
15443b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
15453b2aab18SMatthew Ahrens 
15463b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
15473b2aab18SMatthew Ahrens 		if (error != 0)
15483b2aab18SMatthew Ahrens 			return (error);
15493b2aab18SMatthew Ahrens 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
1550*91948b51SKeith M Wesolowski 		    origin_head, drc->drc_force, drc->drc_owner, tx);
15513b2aab18SMatthew Ahrens 		if (error != 0) {
15523b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin_head, FTAG);
15533b2aab18SMatthew Ahrens 			return (error);
15543b2aab18SMatthew Ahrens 		}
15553b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(origin_head,
15563b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
15573b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
15583b2aab18SMatthew Ahrens 		if (error != 0)
15593b2aab18SMatthew Ahrens 			return (error);
15603b2aab18SMatthew Ahrens 
15613b2aab18SMatthew Ahrens 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
15623b2aab18SMatthew Ahrens 	} else {
15633b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
15643b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
15653b2aab18SMatthew Ahrens 	}
15663b2aab18SMatthew Ahrens 	return (error);
15673cb34c60Sahrens }
15683cb34c60Sahrens 
15693cb34c60Sahrens static void
15703b2aab18SMatthew Ahrens dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
15713cb34c60Sahrens {
15723b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
15733b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
15743b2aab18SMatthew Ahrens 
15753b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
15763b2aab18SMatthew Ahrens 	    tx, "snap=%s", drc->drc_tosnap);
15773b2aab18SMatthew Ahrens 
15783b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
15793b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
15803b2aab18SMatthew Ahrens 
15813b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
15823b2aab18SMatthew Ahrens 		    &origin_head));
15833b2aab18SMatthew Ahrens 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
15843b2aab18SMatthew Ahrens 		    origin_head, tx);
15853b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(origin_head,
15863b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
15873b2aab18SMatthew Ahrens 
15883b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
15893b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
15903b2aab18SMatthew Ahrens 		origin_head->ds_prev->ds_phys->ds_creation_time =
15913b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
15923b2aab18SMatthew Ahrens 		origin_head->ds_prev->ds_phys->ds_guid =
15933b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_toguid;
15943b2aab18SMatthew Ahrens 		origin_head->ds_prev->ds_phys->ds_flags &=
15953b2aab18SMatthew Ahrens 		    ~DS_FLAG_INCONSISTENT;
15963b2aab18SMatthew Ahrens 
15973b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
15983b2aab18SMatthew Ahrens 		origin_head->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
15993b2aab18SMatthew Ahrens 
16003b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
16013b2aab18SMatthew Ahrens 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
1602*91948b51SKeith M Wesolowski 
1603*91948b51SKeith M Wesolowski 		if (drc->drc_owner != NULL)
1604*91948b51SKeith M Wesolowski 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
16053b2aab18SMatthew Ahrens 	} else {
16063b2aab18SMatthew Ahrens 		dsl_dataset_t *ds = drc->drc_ds;
16073cb34c60Sahrens 
16083b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
16093cb34c60Sahrens 
16103b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
16113b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
16123b2aab18SMatthew Ahrens 		ds->ds_prev->ds_phys->ds_creation_time =
16133b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
16143b2aab18SMatthew Ahrens 		ds->ds_prev->ds_phys->ds_guid = drc->drc_drrb->drr_toguid;
16153b2aab18SMatthew Ahrens 		ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
16163cb34c60Sahrens 
16173b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
16183b2aab18SMatthew Ahrens 		ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
16193b2aab18SMatthew Ahrens 	}
16203b2aab18SMatthew Ahrens 	drc->drc_newsnapobj = drc->drc_ds->ds_phys->ds_prev_snap_obj;
16213b2aab18SMatthew Ahrens 	/*
16223b2aab18SMatthew Ahrens 	 * Release the hold from dmu_recv_begin.  This must be done before
16233b2aab18SMatthew Ahrens 	 * we return to open context, so that when we free the dataset's dnode,
16243b2aab18SMatthew Ahrens 	 * we can evict its bonus buffer.
16253b2aab18SMatthew Ahrens 	 */
16263b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
16273b2aab18SMatthew Ahrens 	drc->drc_ds = NULL;
16283cb34c60Sahrens }
16293cb34c60Sahrens 
1630ec5cf9d5SAlexander Stetsenko static int
16313b2aab18SMatthew Ahrens add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
1632ec5cf9d5SAlexander Stetsenko {
16333b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
1634ec5cf9d5SAlexander Stetsenko 	dsl_dataset_t *snapds;
1635ec5cf9d5SAlexander Stetsenko 	guid_map_entry_t *gmep;
1636ec5cf9d5SAlexander Stetsenko 	int err;
1637ec5cf9d5SAlexander Stetsenko 
1638ec5cf9d5SAlexander Stetsenko 	ASSERT(guid_map != NULL);
1639ec5cf9d5SAlexander Stetsenko 
16403b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
16413b2aab18SMatthew Ahrens 	if (err != 0)
16423b2aab18SMatthew Ahrens 		return (err);
1643de8d9cffSMatthew Ahrens 	gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
1644de8d9cffSMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
1645ec5cf9d5SAlexander Stetsenko 	if (err == 0) {
1646ec5cf9d5SAlexander Stetsenko 		gmep->guid = snapds->ds_phys->ds_guid;
1647ec5cf9d5SAlexander Stetsenko 		gmep->gme_ds = snapds;
1648ec5cf9d5SAlexander Stetsenko 		avl_add(guid_map, gmep);
16493b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(snapds, gmep);
1650de8d9cffSMatthew Ahrens 	} else {
1651de8d9cffSMatthew Ahrens 		kmem_free(gmep, sizeof (*gmep));
1652ec5cf9d5SAlexander Stetsenko 	}
1653ec5cf9d5SAlexander Stetsenko 
16543b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
1655ec5cf9d5SAlexander Stetsenko 	return (err);
1656ec5cf9d5SAlexander Stetsenko }
1657ec5cf9d5SAlexander Stetsenko 
16583b2aab18SMatthew Ahrens static int dmu_recv_end_modified_blocks = 3;
16593b2aab18SMatthew Ahrens 
1660ae46e4c7SMatthew Ahrens static int
1661ae46e4c7SMatthew Ahrens dmu_recv_existing_end(dmu_recv_cookie_t *drc)
1662f18faf3fSek {
16633b2aab18SMatthew Ahrens 	int error;
16643b2aab18SMatthew Ahrens 	char name[MAXNAMELEN];
16653cb34c60Sahrens 
16663b2aab18SMatthew Ahrens #ifdef _KERNEL
16673b2aab18SMatthew Ahrens 	/*
16683b2aab18SMatthew Ahrens 	 * We will be destroying the ds; make sure its origin is unmounted if
16693b2aab18SMatthew Ahrens 	 * necessary.
16703b2aab18SMatthew Ahrens 	 */
16713b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
16723b2aab18SMatthew Ahrens 	zfs_destroy_unmount_origin(name);
16733b2aab18SMatthew Ahrens #endif
16743cb34c60Sahrens 
16753b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
16763b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
16773b2aab18SMatthew Ahrens 	    dmu_recv_end_modified_blocks);
16783cb34c60Sahrens 
16793b2aab18SMatthew Ahrens 	if (error != 0)
16803b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
16813b2aab18SMatthew Ahrens 	return (error);
1682f18faf3fSek }
1683ae46e4c7SMatthew Ahrens 
1684ae46e4c7SMatthew Ahrens static int
1685ae46e4c7SMatthew Ahrens dmu_recv_new_end(dmu_recv_cookie_t *drc)
1686ae46e4c7SMatthew Ahrens {
16873b2aab18SMatthew Ahrens 	int error;
1688ae46e4c7SMatthew Ahrens 
16893b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
16903b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
16913b2aab18SMatthew Ahrens 	    dmu_recv_end_modified_blocks);
1692ae46e4c7SMatthew Ahrens 
16933b2aab18SMatthew Ahrens 	if (error != 0) {
16943b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
16953b2aab18SMatthew Ahrens 	} else if (drc->drc_guid_to_ds_map != NULL) {
16963b2aab18SMatthew Ahrens 		(void) add_ds_to_guidmap(drc->drc_tofs,
16973b2aab18SMatthew Ahrens 		    drc->drc_guid_to_ds_map,
16983b2aab18SMatthew Ahrens 		    drc->drc_newsnapobj);
1699ae46e4c7SMatthew Ahrens 	}
17003b2aab18SMatthew Ahrens 	return (error);
1701ae46e4c7SMatthew Ahrens }
1702ae46e4c7SMatthew Ahrens 
1703ae46e4c7SMatthew Ahrens int
1704*91948b51SKeith M Wesolowski dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
1705ae46e4c7SMatthew Ahrens {
1706*91948b51SKeith M Wesolowski 	drc->drc_owner = owner;
1707*91948b51SKeith M Wesolowski 
17083b2aab18SMatthew Ahrens 	if (drc->drc_newfs)
1709ae46e4c7SMatthew Ahrens 		return (dmu_recv_new_end(drc));
17103b2aab18SMatthew Ahrens 	else
17113b2aab18SMatthew Ahrens 		return (dmu_recv_existing_end(drc));
1712ae46e4c7SMatthew Ahrens }
1713