xref: /illumos-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision 78f171005391b928aaf1642b3206c534ed644332)
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>
51*78f17100SMatthew Ahrens #include <sys/dsl_bookmark.h>
52efb80947Sahrens 
5319b94df9SMatthew Ahrens /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
5419b94df9SMatthew Ahrens int zfs_send_corrupt_data = B_FALSE;
5519b94df9SMatthew Ahrens 
563cb34c60Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
573b2aab18SMatthew Ahrens static const char *recv_clone_name = "%recv";
583cb34c60Sahrens 
59efb80947Sahrens static int
604e3c9f44SBill Pijewski dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
61efb80947Sahrens {
624e3c9f44SBill Pijewski 	dsl_dataset_t *ds = dsp->dsa_os->os_dsl_dataset;
63efb80947Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
64fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
65efb80947Sahrens 
664e3c9f44SBill Pijewski 	fletcher_4_incremental_native(buf, len, &dsp->dsa_zc);
674e3c9f44SBill Pijewski 	dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
68efb80947Sahrens 	    (caddr_t)buf, len,
69efb80947Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
704e3c9f44SBill Pijewski 
714e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
724e3c9f44SBill Pijewski 	*dsp->dsa_off += len;
734e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
744e3c9f44SBill Pijewski 
754e3c9f44SBill Pijewski 	return (dsp->dsa_err);
76efb80947Sahrens }
77efb80947Sahrens 
78efb80947Sahrens static int
794e3c9f44SBill Pijewski dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
80efb80947Sahrens     uint64_t length)
81efb80947Sahrens {
824e3c9f44SBill Pijewski 	struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
839e69d7d0SLori Alt 
842f3d8780SMatthew Ahrens 	/*
852f3d8780SMatthew Ahrens 	 * When we receive a free record, dbuf_free_range() assumes
862f3d8780SMatthew Ahrens 	 * that the receiving system doesn't have any dbufs in the range
872f3d8780SMatthew Ahrens 	 * being freed.  This is always true because there is a one-record
882f3d8780SMatthew Ahrens 	 * constraint: we only send one WRITE record for any given
892f3d8780SMatthew Ahrens 	 * object+offset.  We know that the one-record constraint is
902f3d8780SMatthew Ahrens 	 * true because we always send data in increasing order by
912f3d8780SMatthew Ahrens 	 * object,offset.
922f3d8780SMatthew Ahrens 	 *
932f3d8780SMatthew Ahrens 	 * If the increasing-order constraint ever changes, we should find
942f3d8780SMatthew Ahrens 	 * another way to assert that the one-record constraint is still
952f3d8780SMatthew Ahrens 	 * satisfied.
962f3d8780SMatthew Ahrens 	 */
972f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
982f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
992f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
1002f3d8780SMatthew Ahrens 
1012f3d8780SMatthew Ahrens 	/*
1022f3d8780SMatthew Ahrens 	 * If we are doing a non-incremental send, then there can't
1032f3d8780SMatthew Ahrens 	 * be any data in the dataset we're receiving into.  Therefore
1042f3d8780SMatthew Ahrens 	 * a free record would simply be a no-op.  Save space by not
1052f3d8780SMatthew Ahrens 	 * sending it to begin with.
1062f3d8780SMatthew Ahrens 	 */
1072f3d8780SMatthew Ahrens 	if (!dsp->dsa_incremental)
1082f3d8780SMatthew Ahrens 		return (0);
1092f3d8780SMatthew Ahrens 
110534029e5SSimon Klinkert 	if (length != -1ULL && offset + length < offset)
111534029e5SSimon Klinkert 		length = -1ULL;
112534029e5SSimon Klinkert 
1139e69d7d0SLori Alt 	/*
1149e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREE, push it out,
1159e69d7d0SLori Alt 	 * since free block aggregation can only be done for blocks of the
1169e69d7d0SLori Alt 	 * same type (i.e., DRR_FREE records can only be aggregated with
1179e69d7d0SLori Alt 	 * other DRR_FREE records.  DRR_FREEOBJECTS records can only be
1189e69d7d0SLori Alt 	 * aggregated with other DRR_FREEOBJECTS records.
1199e69d7d0SLori Alt 	 */
1204e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
1214e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREE) {
1224e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
1234e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
124be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1254e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
1269e69d7d0SLori Alt 	}
1279e69d7d0SLori Alt 
1284e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREE) {
1299e69d7d0SLori Alt 		/*
1309e69d7d0SLori Alt 		 * There should never be a PENDING_FREE if length is -1
1319e69d7d0SLori Alt 		 * (because dump_dnode is the only place where this
1329e69d7d0SLori Alt 		 * function is called with a -1, and only after flushing
1339e69d7d0SLori Alt 		 * any pending record).
1349e69d7d0SLori Alt 		 */
1359e69d7d0SLori Alt 		ASSERT(length != -1ULL);
1369e69d7d0SLori Alt 		/*
1379e69d7d0SLori Alt 		 * Check to see whether this free block can be aggregated
1389e69d7d0SLori Alt 		 * with pending one.
1399e69d7d0SLori Alt 		 */
1409e69d7d0SLori Alt 		if (drrf->drr_object == object && drrf->drr_offset +
1419e69d7d0SLori Alt 		    drrf->drr_length == offset) {
1429e69d7d0SLori Alt 			drrf->drr_length += length;
1439e69d7d0SLori Alt 			return (0);
1449e69d7d0SLori Alt 		} else {
1459e69d7d0SLori Alt 			/* not a continuation.  Push out pending record */
1464e3c9f44SBill Pijewski 			if (dump_bytes(dsp, dsp->dsa_drr,
1479e69d7d0SLori Alt 			    sizeof (dmu_replay_record_t)) != 0)
148be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
1494e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
1509e69d7d0SLori Alt 		}
1519e69d7d0SLori Alt 	}
1529e69d7d0SLori Alt 	/* create a FREE record and make it pending */
1534e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
1544e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREE;
1559e69d7d0SLori Alt 	drrf->drr_object = object;
1569e69d7d0SLori Alt 	drrf->drr_offset = offset;
1579e69d7d0SLori Alt 	drrf->drr_length = length;
1584e3c9f44SBill Pijewski 	drrf->drr_toguid = dsp->dsa_toguid;
1599e69d7d0SLori Alt 	if (length == -1ULL) {
1604e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
1614e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
162be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1639e69d7d0SLori Alt 	} else {
1644e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_FREE;
1659e69d7d0SLori Alt 	}
166efb80947Sahrens 
167efb80947Sahrens 	return (0);
168efb80947Sahrens }
169efb80947Sahrens 
170efb80947Sahrens static int
1714e3c9f44SBill Pijewski dump_data(dmu_sendarg_t *dsp, dmu_object_type_t type,
1728e714474SLori Alt     uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
173efb80947Sahrens {
1744e3c9f44SBill Pijewski 	struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
1759e69d7d0SLori Alt 
1762f3d8780SMatthew Ahrens 	/*
1772f3d8780SMatthew Ahrens 	 * We send data in increasing object, offset order.
1782f3d8780SMatthew Ahrens 	 * See comment in dump_free() for details.
1792f3d8780SMatthew Ahrens 	 */
1802f3d8780SMatthew Ahrens 	ASSERT(object > dsp->dsa_last_data_object ||
1812f3d8780SMatthew Ahrens 	    (object == dsp->dsa_last_data_object &&
1822f3d8780SMatthew Ahrens 	    offset > dsp->dsa_last_data_offset));
1832f3d8780SMatthew Ahrens 	dsp->dsa_last_data_object = object;
1842f3d8780SMatthew Ahrens 	dsp->dsa_last_data_offset = offset + blksz - 1;
1858e714474SLori Alt 
1869e69d7d0SLori Alt 	/*
1879e69d7d0SLori Alt 	 * If there is any kind of pending aggregation (currently either
1889e69d7d0SLori Alt 	 * a grouping of free objects or free blocks), push it out to
1899e69d7d0SLori Alt 	 * the stream, since aggregation can't be done across operations
1909e69d7d0SLori Alt 	 * of different types.
1919e69d7d0SLori Alt 	 */
1924e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
1934e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
1944e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
195be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
1964e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
1979e69d7d0SLori Alt 	}
198efb80947Sahrens 	/* write a DATA record */
1994e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2004e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_WRITE;
2019e69d7d0SLori Alt 	drrw->drr_object = object;
2029e69d7d0SLori Alt 	drrw->drr_type = type;
2039e69d7d0SLori Alt 	drrw->drr_offset = offset;
2049e69d7d0SLori Alt 	drrw->drr_length = blksz;
2054e3c9f44SBill Pijewski 	drrw->drr_toguid = dsp->dsa_toguid;
2068e714474SLori Alt 	drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
2078e714474SLori Alt 	if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup)
2088e714474SLori Alt 		drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
2098e714474SLori Alt 	DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
2108e714474SLori Alt 	DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
2118e714474SLori Alt 	DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
2128e714474SLori Alt 	drrw->drr_key.ddk_cksum = bp->blk_cksum;
213efb80947Sahrens 
2144e3c9f44SBill Pijewski 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
215be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
2164e3c9f44SBill Pijewski 	if (dump_bytes(dsp, data, blksz) != 0)
217be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
218efb80947Sahrens 	return (0);
219efb80947Sahrens }
220efb80947Sahrens 
2210a586ceaSMark Shellenbaum static int
2224e3c9f44SBill Pijewski dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
2230a586ceaSMark Shellenbaum {
2244e3c9f44SBill Pijewski 	struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
2250a586ceaSMark Shellenbaum 
2264e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
2274e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
2284e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
229be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2304e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2310a586ceaSMark Shellenbaum 	}
2320a586ceaSMark Shellenbaum 
2330a586ceaSMark Shellenbaum 	/* write a SPILL record */
2344e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2354e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_SPILL;
2360a586ceaSMark Shellenbaum 	drrs->drr_object = object;
2370a586ceaSMark Shellenbaum 	drrs->drr_length = blksz;
2384e3c9f44SBill Pijewski 	drrs->drr_toguid = dsp->dsa_toguid;
2390a586ceaSMark Shellenbaum 
2404e3c9f44SBill Pijewski 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)))
241be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
2424e3c9f44SBill Pijewski 	if (dump_bytes(dsp, data, blksz))
243be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
2440a586ceaSMark Shellenbaum 	return (0);
2450a586ceaSMark Shellenbaum }
2460a586ceaSMark Shellenbaum 
247efb80947Sahrens static int
2484e3c9f44SBill Pijewski dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
249efb80947Sahrens {
2504e3c9f44SBill Pijewski 	struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
2519e69d7d0SLori Alt 
2522f3d8780SMatthew Ahrens 	/* See comment in dump_free(). */
2532f3d8780SMatthew Ahrens 	if (!dsp->dsa_incremental)
2542f3d8780SMatthew Ahrens 		return (0);
2552f3d8780SMatthew Ahrens 
2569e69d7d0SLori Alt 	/*
2579e69d7d0SLori Alt 	 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
2589e69d7d0SLori Alt 	 * push it out, since free block aggregation can only be done for
2599e69d7d0SLori Alt 	 * blocks of the same type (i.e., DRR_FREE records can only be
2609e69d7d0SLori Alt 	 * aggregated with other DRR_FREE records.  DRR_FREEOBJECTS records
2619e69d7d0SLori Alt 	 * can only be aggregated with other DRR_FREEOBJECTS records.
2629e69d7d0SLori Alt 	 */
2634e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE &&
2644e3c9f44SBill Pijewski 	    dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
2654e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
2664e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
267be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
2684e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
2699e69d7d0SLori Alt 	}
2704e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
2719e69d7d0SLori Alt 		/*
2729e69d7d0SLori Alt 		 * See whether this free object array can be aggregated
2739e69d7d0SLori Alt 		 * with pending one
2749e69d7d0SLori Alt 		 */
2759e69d7d0SLori Alt 		if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
2769e69d7d0SLori Alt 			drrfo->drr_numobjs += numobjs;
2779e69d7d0SLori Alt 			return (0);
2789e69d7d0SLori Alt 		} else {
2799e69d7d0SLori Alt 			/* can't be aggregated.  Push out pending record */
2804e3c9f44SBill Pijewski 			if (dump_bytes(dsp, dsp->dsa_drr,
2819e69d7d0SLori Alt 			    sizeof (dmu_replay_record_t)) != 0)
282be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINTR));
2834e3c9f44SBill Pijewski 			dsp->dsa_pending_op = PENDING_NONE;
2849e69d7d0SLori Alt 		}
2859e69d7d0SLori Alt 	}
2869e69d7d0SLori Alt 
287efb80947Sahrens 	/* write a FREEOBJECTS record */
2884e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
2894e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
2909e69d7d0SLori Alt 	drrfo->drr_firstobj = firstobj;
2919e69d7d0SLori Alt 	drrfo->drr_numobjs = numobjs;
2924e3c9f44SBill Pijewski 	drrfo->drr_toguid = dsp->dsa_toguid;
2939e69d7d0SLori Alt 
2944e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_FREEOBJECTS;
295efb80947Sahrens 
296efb80947Sahrens 	return (0);
297efb80947Sahrens }
298efb80947Sahrens 
299efb80947Sahrens static int
3004e3c9f44SBill Pijewski dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
301efb80947Sahrens {
3024e3c9f44SBill Pijewski 	struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
3039e69d7d0SLori Alt 
304efb80947Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
3054e3c9f44SBill Pijewski 		return (dump_freeobjects(dsp, object, 1));
306efb80947Sahrens 
3074e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE) {
3084e3c9f44SBill Pijewski 		if (dump_bytes(dsp, dsp->dsa_drr,
3094e3c9f44SBill Pijewski 		    sizeof (dmu_replay_record_t)) != 0)
310be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINTR));
3114e3c9f44SBill Pijewski 		dsp->dsa_pending_op = PENDING_NONE;
3129e69d7d0SLori Alt 	}
3139e69d7d0SLori Alt 
314efb80947Sahrens 	/* write an OBJECT record */
3154e3c9f44SBill Pijewski 	bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
3164e3c9f44SBill Pijewski 	dsp->dsa_drr->drr_type = DRR_OBJECT;
3179e69d7d0SLori Alt 	drro->drr_object = object;
3189e69d7d0SLori Alt 	drro->drr_type = dnp->dn_type;
3199e69d7d0SLori Alt 	drro->drr_bonustype = dnp->dn_bonustype;
3209e69d7d0SLori Alt 	drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
3219e69d7d0SLori Alt 	drro->drr_bonuslen = dnp->dn_bonuslen;
3229e69d7d0SLori Alt 	drro->drr_checksumtype = dnp->dn_checksum;
3239e69d7d0SLori Alt 	drro->drr_compress = dnp->dn_compress;
3244e3c9f44SBill Pijewski 	drro->drr_toguid = dsp->dsa_toguid;
3259e69d7d0SLori Alt 
3264e3c9f44SBill Pijewski 	if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
327be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
328efb80947Sahrens 
3294e3c9f44SBill Pijewski 	if (dump_bytes(dsp, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0)
330be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
331efb80947Sahrens 
3322f3d8780SMatthew Ahrens 	/* Free anything past the end of the file. */
3334e3c9f44SBill Pijewski 	if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
3342f3d8780SMatthew Ahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
335be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
3363b2aab18SMatthew Ahrens 	if (dsp->dsa_err != 0)
337be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
338efb80947Sahrens 	return (0);
339efb80947Sahrens }
340efb80947Sahrens 
341efb80947Sahrens #define	BP_SPAN(dnp, level) \
342efb80947Sahrens 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
343efb80947Sahrens 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
344efb80947Sahrens 
345b24ab676SJeff Bonwick /* ARGSUSED */
346efb80947Sahrens static int
3471b912ec7SGeorge Wilson backup_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
348b24ab676SJeff Bonwick     const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
349efb80947Sahrens {
3504e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp = arg;
351efb80947Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
352efb80947Sahrens 	int err = 0;
353efb80947Sahrens 
354efb80947Sahrens 	if (issig(JUSTLOOKING) && issig(FORREAL))
355be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINTR));
356efb80947Sahrens 
357b24ab676SJeff Bonwick 	if (zb->zb_object != DMU_META_DNODE_OBJECT &&
358b24ab676SJeff Bonwick 	    DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
35914843421SMatthew Ahrens 		return (0);
360*78f17100SMatthew Ahrens 	} else if (zb->zb_level == ZB_ZIL_LEVEL) {
361*78f17100SMatthew Ahrens 		/*
362*78f17100SMatthew Ahrens 		 * If we are sending a non-snapshot (which is allowed on
363*78f17100SMatthew Ahrens 		 * read-only pools), it may have a ZIL, which must be ignored.
364*78f17100SMatthew Ahrens 		 */
365*78f17100SMatthew Ahrens 		return (0);
36643466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp) &&
36743466aaeSMax Grossman 	    zb->zb_object == DMU_META_DNODE_OBJECT) {
36888b7b0f2SMatthew Ahrens 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
36988b7b0f2SMatthew Ahrens 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
3704e3c9f44SBill Pijewski 		err = dump_freeobjects(dsp, dnobj, span >> DNODE_SHIFT);
37143466aaeSMax Grossman 	} else if (BP_IS_HOLE(bp)) {
37288b7b0f2SMatthew Ahrens 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
3734e3c9f44SBill Pijewski 		err = dump_free(dsp, zb->zb_object, zb->zb_blkid * span, span);
37488b7b0f2SMatthew Ahrens 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
37588b7b0f2SMatthew Ahrens 		return (0);
37688b7b0f2SMatthew Ahrens 	} else if (type == DMU_OT_DNODE) {
37788b7b0f2SMatthew Ahrens 		dnode_phys_t *blk;
378efb80947Sahrens 		int i;
379efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
38088b7b0f2SMatthew Ahrens 		uint32_t aflags = ARC_WAIT;
38188b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
382efb80947Sahrens 
3831b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
3841b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
3851b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
386be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
38788b7b0f2SMatthew Ahrens 
38888b7b0f2SMatthew Ahrens 		blk = abuf->b_data;
389efb80947Sahrens 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
39088b7b0f2SMatthew Ahrens 			uint64_t dnobj = (zb->zb_blkid <<
39188b7b0f2SMatthew Ahrens 			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
3924e3c9f44SBill Pijewski 			err = dump_dnode(dsp, dnobj, blk+i);
3933b2aab18SMatthew Ahrens 			if (err != 0)
394efb80947Sahrens 				break;
395efb80947Sahrens 		}
39688b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
3970a586ceaSMark Shellenbaum 	} else if (type == DMU_OT_SA) {
3980a586ceaSMark Shellenbaum 		uint32_t aflags = ARC_WAIT;
3990a586ceaSMark Shellenbaum 		arc_buf_t *abuf;
4000a586ceaSMark Shellenbaum 		int blksz = BP_GET_LSIZE(bp);
4010a586ceaSMark Shellenbaum 
4021b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
4031b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
4041b912ec7SGeorge Wilson 		    &aflags, zb) != 0)
405be6fd75aSMatthew Ahrens 			return (SET_ERROR(EIO));
4060a586ceaSMark Shellenbaum 
4074e3c9f44SBill Pijewski 		err = dump_spill(dsp, zb->zb_object, blksz, abuf->b_data);
4080a586ceaSMark Shellenbaum 		(void) arc_buf_remove_ref(abuf, &abuf);
40988b7b0f2SMatthew Ahrens 	} else { /* it's a level-0 block of a regular object */
41088b7b0f2SMatthew Ahrens 		uint32_t aflags = ARC_WAIT;
41188b7b0f2SMatthew Ahrens 		arc_buf_t *abuf;
412efb80947Sahrens 		int blksz = BP_GET_LSIZE(bp);
41388b7b0f2SMatthew Ahrens 
414*78f17100SMatthew Ahrens 		ASSERT0(zb->zb_level);
4151b912ec7SGeorge Wilson 		if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
4161b912ec7SGeorge Wilson 		    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
4171b912ec7SGeorge Wilson 		    &aflags, zb) != 0) {
41819b94df9SMatthew Ahrens 			if (zfs_send_corrupt_data) {
41919b94df9SMatthew Ahrens 				/* Send a block filled with 0x"zfs badd bloc" */
42019b94df9SMatthew Ahrens 				abuf = arc_buf_alloc(spa, blksz, &abuf,
42119b94df9SMatthew Ahrens 				    ARC_BUFC_DATA);
42219b94df9SMatthew Ahrens 				uint64_t *ptr;
42319b94df9SMatthew Ahrens 				for (ptr = abuf->b_data;
42419b94df9SMatthew Ahrens 				    (char *)ptr < (char *)abuf->b_data + blksz;
42519b94df9SMatthew Ahrens 				    ptr++)
42619b94df9SMatthew Ahrens 					*ptr = 0x2f5baddb10c;
42719b94df9SMatthew Ahrens 			} else {
428be6fd75aSMatthew Ahrens 				return (SET_ERROR(EIO));
42919b94df9SMatthew Ahrens 			}
43019b94df9SMatthew Ahrens 		}
43188b7b0f2SMatthew Ahrens 
4324e3c9f44SBill Pijewski 		err = dump_data(dsp, type, zb->zb_object, zb->zb_blkid * blksz,
4338e714474SLori Alt 		    blksz, bp, abuf->b_data);
43488b7b0f2SMatthew Ahrens 		(void) arc_buf_remove_ref(abuf, &abuf);
435efb80947Sahrens 	}
436efb80947Sahrens 
437efb80947Sahrens 	ASSERT(err == 0 || err == EINTR);
438efb80947Sahrens 	return (err);
439efb80947Sahrens }
440efb80947Sahrens 
4414445fffbSMatthew Ahrens /*
442*78f17100SMatthew Ahrens  * Releases dp using the specified tag.
4434445fffbSMatthew Ahrens  */
4443b2aab18SMatthew Ahrens static int
4453b2aab18SMatthew Ahrens dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *ds,
446*78f17100SMatthew Ahrens     zfs_bookmark_phys_t *fromzb, boolean_t is_clone, int outfd,
447*78f17100SMatthew Ahrens     vnode_t *vp, offset_t *off)
448efb80947Sahrens {
4493b2aab18SMatthew Ahrens 	objset_t *os;
450efb80947Sahrens 	dmu_replay_record_t *drr;
4514e3c9f44SBill Pijewski 	dmu_sendarg_t *dsp;
452efb80947Sahrens 	int err;
4533cb34c60Sahrens 	uint64_t fromtxg = 0;
454efb80947Sahrens 
4553b2aab18SMatthew Ahrens 	err = dmu_objset_from_ds(ds, &os);
4563b2aab18SMatthew Ahrens 	if (err != 0) {
4573b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, tag);
4583b2aab18SMatthew Ahrens 		return (err);
4593b2aab18SMatthew Ahrens 	}
460efb80947Sahrens 
461efb80947Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
462efb80947Sahrens 	drr->drr_type = DRR_BEGIN;
463efb80947Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
4649e69d7d0SLori Alt 	DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
4659e69d7d0SLori Alt 	    DMU_SUBSTREAM);
466dc7cd546SMark Shellenbaum 
467dc7cd546SMark Shellenbaum #ifdef _KERNEL
4683b2aab18SMatthew Ahrens 	if (dmu_objset_type(os) == DMU_OST_ZFS) {
469dc7cd546SMark Shellenbaum 		uint64_t version;
4703b2aab18SMatthew Ahrens 		if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
4714e3c9f44SBill Pijewski 			kmem_free(drr, sizeof (dmu_replay_record_t));
4723b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, tag);
473be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
4744e3c9f44SBill Pijewski 		}
4753b2aab18SMatthew Ahrens 		if (version >= ZPL_VERSION_SA) {
476dc7cd546SMark Shellenbaum 			DMU_SET_FEATUREFLAGS(
477dc7cd546SMark Shellenbaum 			    drr->drr_u.drr_begin.drr_versioninfo,
478dc7cd546SMark Shellenbaum 			    DMU_BACKUP_FEATURE_SA_SPILL);
479dc7cd546SMark Shellenbaum 		}
480dc7cd546SMark Shellenbaum 	}
481dc7cd546SMark Shellenbaum #endif
482dc7cd546SMark Shellenbaum 
483efb80947Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
484efb80947Sahrens 	    ds->ds_phys->ds_creation_time;
4853b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
486*78f17100SMatthew Ahrens 	if (is_clone)
4873cb34c60Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
488efb80947Sahrens 	drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
489ab04eb8eStimh 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
490ab04eb8eStimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
491ab04eb8eStimh 
492*78f17100SMatthew Ahrens 	if (fromzb != NULL) {
493*78f17100SMatthew Ahrens 		drr->drr_u.drr_begin.drr_fromguid = fromzb->zbm_guid;
494*78f17100SMatthew Ahrens 		fromtxg = fromzb->zbm_creation_txg;
495*78f17100SMatthew Ahrens 	}
496efb80947Sahrens 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
497*78f17100SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds)) {
498*78f17100SMatthew Ahrens 		(void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
499*78f17100SMatthew Ahrens 		    sizeof (drr->drr_u.drr_begin.drr_toname));
5003b2aab18SMatthew Ahrens 	}
5013cb34c60Sahrens 
5024e3c9f44SBill Pijewski 	dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
5034e3c9f44SBill Pijewski 
5044e3c9f44SBill Pijewski 	dsp->dsa_drr = drr;
5054e3c9f44SBill Pijewski 	dsp->dsa_vp = vp;
5064e3c9f44SBill Pijewski 	dsp->dsa_outfd = outfd;
5074e3c9f44SBill Pijewski 	dsp->dsa_proc = curproc;
5083b2aab18SMatthew Ahrens 	dsp->dsa_os = os;
5094e3c9f44SBill Pijewski 	dsp->dsa_off = off;
5104e3c9f44SBill Pijewski 	dsp->dsa_toguid = ds->ds_phys->ds_guid;
5114e3c9f44SBill Pijewski 	ZIO_SET_CHECKSUM(&dsp->dsa_zc, 0, 0, 0, 0);
5124e3c9f44SBill Pijewski 	dsp->dsa_pending_op = PENDING_NONE;
513*78f17100SMatthew Ahrens 	dsp->dsa_incremental = (fromzb != NULL);
5144e3c9f44SBill Pijewski 
5154e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
5164e3c9f44SBill Pijewski 	list_insert_head(&ds->ds_sendstreams, dsp);
5174e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
5184e3c9f44SBill Pijewski 
519de8d9cffSMatthew Ahrens 	dsl_dataset_long_hold(ds, FTAG);
520de8d9cffSMatthew Ahrens 	dsl_pool_rele(dp, tag);
521de8d9cffSMatthew Ahrens 
5224e3c9f44SBill Pijewski 	if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) {
5234e3c9f44SBill Pijewski 		err = dsp->dsa_err;
5244e3c9f44SBill Pijewski 		goto out;
525efb80947Sahrens 	}
526efb80947Sahrens 
52788b7b0f2SMatthew Ahrens 	err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH,
5284e3c9f44SBill Pijewski 	    backup_cb, dsp);
529efb80947Sahrens 
5304e3c9f44SBill Pijewski 	if (dsp->dsa_pending_op != PENDING_NONE)
5314e3c9f44SBill Pijewski 		if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0)
532be6fd75aSMatthew Ahrens 			err = SET_ERROR(EINTR);
5339e69d7d0SLori Alt 
5343b2aab18SMatthew Ahrens 	if (err != 0) {
5353b2aab18SMatthew Ahrens 		if (err == EINTR && dsp->dsa_err != 0)
5364e3c9f44SBill Pijewski 			err = dsp->dsa_err;
5374e3c9f44SBill Pijewski 		goto out;
538efb80947Sahrens 	}
539efb80947Sahrens 
540efb80947Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
541efb80947Sahrens 	drr->drr_type = DRR_END;
5424e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
5434e3c9f44SBill Pijewski 	drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
544efb80947Sahrens 
5454e3c9f44SBill Pijewski 	if (dump_bytes(dsp, drr, sizeof (dmu_replay_record_t)) != 0) {
5464e3c9f44SBill Pijewski 		err = dsp->dsa_err;
5474e3c9f44SBill Pijewski 		goto out;
5487b5309bbSgw 	}
549efb80947Sahrens 
5504e3c9f44SBill Pijewski out:
5514e3c9f44SBill Pijewski 	mutex_enter(&ds->ds_sendstream_lock);
5524e3c9f44SBill Pijewski 	list_remove(&ds->ds_sendstreams, dsp);
5534e3c9f44SBill Pijewski 	mutex_exit(&ds->ds_sendstream_lock);
5544e3c9f44SBill Pijewski 
555efb80947Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
5564e3c9f44SBill Pijewski 	kmem_free(dsp, sizeof (dmu_sendarg_t));
557efb80947Sahrens 
5583b2aab18SMatthew Ahrens 	dsl_dataset_long_rele(ds, FTAG);
5593b2aab18SMatthew Ahrens 
5604e3c9f44SBill Pijewski 	return (err);
561efb80947Sahrens }
562efb80947Sahrens 
56319b94df9SMatthew Ahrens int
5643b2aab18SMatthew Ahrens dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
5653b2aab18SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
5663b2aab18SMatthew Ahrens {
5673b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
5683b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
5693b2aab18SMatthew Ahrens 	dsl_dataset_t *fromds = NULL;
5703b2aab18SMatthew Ahrens 	int err;
5713b2aab18SMatthew Ahrens 
5723b2aab18SMatthew Ahrens 	err = dsl_pool_hold(pool, FTAG, &dp);
5733b2aab18SMatthew Ahrens 	if (err != 0)
5743b2aab18SMatthew Ahrens 		return (err);
5753b2aab18SMatthew Ahrens 
5763b2aab18SMatthew Ahrens 	err = dsl_dataset_hold_obj(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 != 0) {
583*78f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
584*78f17100SMatthew Ahrens 		boolean_t is_clone;
585*78f17100SMatthew Ahrens 
5863b2aab18SMatthew Ahrens 		err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
5873b2aab18SMatthew Ahrens 		if (err != 0) {
5883b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
5893b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
5903b2aab18SMatthew Ahrens 			return (err);
5913b2aab18SMatthew Ahrens 		}
592*78f17100SMatthew Ahrens 		if (!dsl_dataset_is_before(ds, fromds, 0))
593*78f17100SMatthew Ahrens 			err = SET_ERROR(EXDEV);
594*78f17100SMatthew Ahrens 		zb.zbm_creation_time = fromds->ds_phys->ds_creation_time;
595*78f17100SMatthew Ahrens 		zb.zbm_creation_txg = fromds->ds_phys->ds_creation_txg;
596*78f17100SMatthew Ahrens 		zb.zbm_guid = fromds->ds_phys->ds_guid;
597*78f17100SMatthew Ahrens 		is_clone = (fromds->ds_dir != ds->ds_dir);
598*78f17100SMatthew Ahrens 		dsl_dataset_rele(fromds, FTAG);
599*78f17100SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
600*78f17100SMatthew Ahrens 		    outfd, vp, off);
601*78f17100SMatthew Ahrens 	} else {
602*78f17100SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
603*78f17100SMatthew Ahrens 		    outfd, vp, off);
6043b2aab18SMatthew Ahrens 	}
605*78f17100SMatthew Ahrens 	dsl_dataset_rele(ds, FTAG);
606*78f17100SMatthew Ahrens 	return (err);
6073b2aab18SMatthew Ahrens }
6083b2aab18SMatthew Ahrens 
6093b2aab18SMatthew Ahrens int
6103b2aab18SMatthew Ahrens dmu_send(const char *tosnap, const char *fromsnap,
6113b2aab18SMatthew Ahrens     int outfd, vnode_t *vp, offset_t *off)
6123b2aab18SMatthew Ahrens {
6133b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
6143b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
6153b2aab18SMatthew Ahrens 	int err;
616*78f17100SMatthew Ahrens 	boolean_t owned = B_FALSE;
6173b2aab18SMatthew Ahrens 
618*78f17100SMatthew Ahrens 	if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
619be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
6203b2aab18SMatthew Ahrens 
6213b2aab18SMatthew Ahrens 	err = dsl_pool_hold(tosnap, FTAG, &dp);
6223b2aab18SMatthew Ahrens 	if (err != 0)
6233b2aab18SMatthew Ahrens 		return (err);
6243b2aab18SMatthew Ahrens 
625*78f17100SMatthew Ahrens 	if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
626*78f17100SMatthew Ahrens 		/*
627*78f17100SMatthew Ahrens 		 * We are sending a filesystem or volume.  Ensure
628*78f17100SMatthew Ahrens 		 * that it doesn't change by owning the dataset.
629*78f17100SMatthew Ahrens 		 */
630*78f17100SMatthew Ahrens 		err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
631*78f17100SMatthew Ahrens 		owned = B_TRUE;
632*78f17100SMatthew Ahrens 	} else {
633*78f17100SMatthew Ahrens 		err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
634*78f17100SMatthew Ahrens 	}
6353b2aab18SMatthew Ahrens 	if (err != 0) {
6363b2aab18SMatthew Ahrens 		dsl_pool_rele(dp, FTAG);
6373b2aab18SMatthew Ahrens 		return (err);
6383b2aab18SMatthew Ahrens 	}
6393b2aab18SMatthew Ahrens 
6403b2aab18SMatthew Ahrens 	if (fromsnap != NULL) {
641*78f17100SMatthew Ahrens 		zfs_bookmark_phys_t zb;
642*78f17100SMatthew Ahrens 		boolean_t is_clone = B_FALSE;
643*78f17100SMatthew Ahrens 		int fsnamelen = strchr(tosnap, '@') - tosnap;
644*78f17100SMatthew Ahrens 
645*78f17100SMatthew Ahrens 		/*
646*78f17100SMatthew Ahrens 		 * If the fromsnap is in a different filesystem, then
647*78f17100SMatthew Ahrens 		 * mark the send stream as a clone.
648*78f17100SMatthew Ahrens 		 */
649*78f17100SMatthew Ahrens 		if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
650*78f17100SMatthew Ahrens 		    (fromsnap[fsnamelen] != '@' &&
651*78f17100SMatthew Ahrens 		    fromsnap[fsnamelen] != '#')) {
652*78f17100SMatthew Ahrens 			is_clone = B_TRUE;
653*78f17100SMatthew Ahrens 		}
654*78f17100SMatthew Ahrens 
655*78f17100SMatthew Ahrens 		if (strchr(fromsnap, '@')) {
656*78f17100SMatthew Ahrens 			dsl_dataset_t *fromds;
657*78f17100SMatthew Ahrens 			err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
658*78f17100SMatthew Ahrens 			if (err == 0) {
659*78f17100SMatthew Ahrens 				if (!dsl_dataset_is_before(ds, fromds, 0))
660*78f17100SMatthew Ahrens 					err = SET_ERROR(EXDEV);
661*78f17100SMatthew Ahrens 				zb.zbm_creation_time =
662*78f17100SMatthew Ahrens 				    fromds->ds_phys->ds_creation_time;
663*78f17100SMatthew Ahrens 				zb.zbm_creation_txg =
664*78f17100SMatthew Ahrens 				    fromds->ds_phys->ds_creation_txg;
665*78f17100SMatthew Ahrens 				zb.zbm_guid = fromds->ds_phys->ds_guid;
666*78f17100SMatthew Ahrens 				is_clone = (ds->ds_dir != fromds->ds_dir);
667*78f17100SMatthew Ahrens 				dsl_dataset_rele(fromds, FTAG);
668*78f17100SMatthew Ahrens 			}
669*78f17100SMatthew Ahrens 		} else {
670*78f17100SMatthew Ahrens 			err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
671*78f17100SMatthew Ahrens 		}
6723b2aab18SMatthew Ahrens 		if (err != 0) {
6733b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
6743b2aab18SMatthew Ahrens 			dsl_pool_rele(dp, FTAG);
6753b2aab18SMatthew Ahrens 			return (err);
6763b2aab18SMatthew Ahrens 		}
677*78f17100SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
678*78f17100SMatthew Ahrens 		    outfd, vp, off);
679*78f17100SMatthew Ahrens 	} else {
680*78f17100SMatthew Ahrens 		err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
681*78f17100SMatthew Ahrens 		    outfd, vp, off);
6823b2aab18SMatthew Ahrens 	}
683*78f17100SMatthew Ahrens 	if (owned)
684*78f17100SMatthew Ahrens 		dsl_dataset_disown(ds, FTAG);
685*78f17100SMatthew Ahrens 	else
686*78f17100SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
687*78f17100SMatthew Ahrens 	return (err);
6883b2aab18SMatthew Ahrens }
6893b2aab18SMatthew Ahrens 
6903b2aab18SMatthew Ahrens int
6913b2aab18SMatthew Ahrens dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
69219b94df9SMatthew Ahrens {
69319b94df9SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
69419b94df9SMatthew Ahrens 	int err;
69519b94df9SMatthew Ahrens 	uint64_t size;
69619b94df9SMatthew Ahrens 
6973b2aab18SMatthew Ahrens 	ASSERT(dsl_pool_config_held(dp));
6983b2aab18SMatthew Ahrens 
69919b94df9SMatthew Ahrens 	/* tosnap must be a snapshot */
7003b2aab18SMatthew Ahrens 	if (!dsl_dataset_is_snapshot(ds))
701be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
70219b94df9SMatthew Ahrens 
7034445fffbSMatthew Ahrens 	/*
7044445fffbSMatthew Ahrens 	 * fromsnap must be an earlier snapshot from the same fs as tosnap,
7054445fffbSMatthew Ahrens 	 * or the origin's fs.
7064445fffbSMatthew Ahrens 	 */
707*78f17100SMatthew Ahrens 	if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
708be6fd75aSMatthew Ahrens 		return (SET_ERROR(EXDEV));
70919b94df9SMatthew Ahrens 
71019b94df9SMatthew Ahrens 	/* Get uncompressed size estimate of changed data. */
71119b94df9SMatthew Ahrens 	if (fromds == NULL) {
71219b94df9SMatthew Ahrens 		size = ds->ds_phys->ds_uncompressed_bytes;
71319b94df9SMatthew Ahrens 	} else {
71419b94df9SMatthew Ahrens 		uint64_t used, comp;
71519b94df9SMatthew Ahrens 		err = dsl_dataset_space_written(fromds, ds,
71619b94df9SMatthew Ahrens 		    &used, &comp, &size);
7173b2aab18SMatthew Ahrens 		if (err != 0)
71819b94df9SMatthew Ahrens 			return (err);
71919b94df9SMatthew Ahrens 	}
72019b94df9SMatthew Ahrens 
72119b94df9SMatthew Ahrens 	/*
72219b94df9SMatthew Ahrens 	 * Assume that space (both on-disk and in-stream) is dominated by
72319b94df9SMatthew Ahrens 	 * data.  We will adjust for indirect blocks and the copies property,
72419b94df9SMatthew Ahrens 	 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
72519b94df9SMatthew Ahrens 	 */
72619b94df9SMatthew Ahrens 
72719b94df9SMatthew Ahrens 	/*
72819b94df9SMatthew Ahrens 	 * Subtract out approximate space used by indirect blocks.
72919b94df9SMatthew Ahrens 	 * Assume most space is used by data blocks (non-indirect, non-dnode).
73019b94df9SMatthew Ahrens 	 * Assume all blocks are recordsize.  Assume ditto blocks and
73119b94df9SMatthew Ahrens 	 * internal fragmentation counter out compression.
73219b94df9SMatthew Ahrens 	 *
73319b94df9SMatthew Ahrens 	 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
73419b94df9SMatthew Ahrens 	 * block, which we observe in practice.
73519b94df9SMatthew Ahrens 	 */
73619b94df9SMatthew Ahrens 	uint64_t recordsize;
7373b2aab18SMatthew Ahrens 	err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
7383b2aab18SMatthew Ahrens 	if (err != 0)
73919b94df9SMatthew Ahrens 		return (err);
74019b94df9SMatthew Ahrens 	size -= size / recordsize * sizeof (blkptr_t);
74119b94df9SMatthew Ahrens 
74219b94df9SMatthew Ahrens 	/* Add in the space for the record associated with each block. */
74319b94df9SMatthew Ahrens 	size += size / recordsize * sizeof (dmu_replay_record_t);
74419b94df9SMatthew Ahrens 
74519b94df9SMatthew Ahrens 	*sizep = size;
74619b94df9SMatthew Ahrens 
74719b94df9SMatthew Ahrens 	return (0);
74819b94df9SMatthew Ahrens }
74919b94df9SMatthew Ahrens 
7503b2aab18SMatthew Ahrens typedef struct dmu_recv_begin_arg {
7513b2aab18SMatthew Ahrens 	const char *drba_origin;
7523b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drba_cookie;
7533b2aab18SMatthew Ahrens 	cred_t *drba_cred;
75434f2f8cfSMatthew Ahrens 	uint64_t drba_snapobj;
7553b2aab18SMatthew Ahrens } dmu_recv_begin_arg_t;
756f18faf3fSek 
757f18faf3fSek static int
7583b2aab18SMatthew Ahrens recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
7593b2aab18SMatthew Ahrens     uint64_t fromguid)
760f18faf3fSek {
7613cb34c60Sahrens 	uint64_t val;
7623b2aab18SMatthew Ahrens 	int error;
7633b2aab18SMatthew Ahrens 	dsl_pool_t *dp = ds->ds_dir->dd_pool;
764f18faf3fSek 
7653b2aab18SMatthew Ahrens 	/* temporary clone name must not exist */
7663b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
7673b2aab18SMatthew Ahrens 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj, recv_clone_name,
7683b2aab18SMatthew Ahrens 	    8, 1, &val);
7693b2aab18SMatthew Ahrens 	if (error != ENOENT)
7703b2aab18SMatthew Ahrens 		return (error == 0 ? EBUSY : error);
7713b2aab18SMatthew Ahrens 
772feaa74e4SMark Maybee 	/* new snapshot name must not exist */
7733b2aab18SMatthew Ahrens 	error = zap_lookup(dp->dp_meta_objset,
7743b2aab18SMatthew Ahrens 	    ds->ds_phys->ds_snapnames_zapobj, drba->drba_cookie->drc_tosnap,
7753b2aab18SMatthew Ahrens 	    8, 1, &val);
7763b2aab18SMatthew Ahrens 	if (error != ENOENT)
7773b2aab18SMatthew Ahrens 		return (error == 0 ? EEXIST : error);
778feaa74e4SMark Maybee 
7793b2aab18SMatthew Ahrens 	if (fromguid != 0) {
78034f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap;
78134f2f8cfSMatthew Ahrens 		uint64_t obj = ds->ds_phys->ds_prev_snap_obj;
78234f2f8cfSMatthew Ahrens 
78334f2f8cfSMatthew Ahrens 		/* Find snapshot in this dir that matches fromguid. */
78434f2f8cfSMatthew Ahrens 		while (obj != 0) {
78534f2f8cfSMatthew Ahrens 			error = dsl_dataset_hold_obj(dp, obj, FTAG,
78634f2f8cfSMatthew Ahrens 			    &snap);
78734f2f8cfSMatthew Ahrens 			if (error != 0)
78834f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
78934f2f8cfSMatthew Ahrens 			if (snap->ds_dir != ds->ds_dir) {
79034f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
79134f2f8cfSMatthew Ahrens 				return (SET_ERROR(ENODEV));
79234f2f8cfSMatthew Ahrens 			}
79334f2f8cfSMatthew Ahrens 			if (snap->ds_phys->ds_guid == fromguid)
79434f2f8cfSMatthew Ahrens 				break;
79534f2f8cfSMatthew Ahrens 			obj = snap->ds_phys->ds_prev_snap_obj;
79634f2f8cfSMatthew Ahrens 			dsl_dataset_rele(snap, FTAG);
79734f2f8cfSMatthew Ahrens 		}
79834f2f8cfSMatthew Ahrens 		if (obj == 0)
799be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
80092241e0bSTom Erickson 
80134f2f8cfSMatthew Ahrens 		if (drba->drba_cookie->drc_force) {
80234f2f8cfSMatthew Ahrens 			drba->drba_snapobj = obj;
80334f2f8cfSMatthew Ahrens 		} else {
80434f2f8cfSMatthew Ahrens 			/*
80534f2f8cfSMatthew Ahrens 			 * If we are not forcing, there must be no
80634f2f8cfSMatthew Ahrens 			 * changes since fromsnap.
80734f2f8cfSMatthew Ahrens 			 */
80834f2f8cfSMatthew Ahrens 			if (dsl_dataset_modified_since_snap(ds, snap)) {
80992241e0bSTom Erickson 				dsl_dataset_rele(snap, FTAG);
81034f2f8cfSMatthew Ahrens 				return (SET_ERROR(ETXTBSY));
81192241e0bSTom Erickson 			}
81234f2f8cfSMatthew Ahrens 			drba->drba_snapobj = ds->ds_prev->ds_object;
81392241e0bSTom Erickson 		}
81434f2f8cfSMatthew Ahrens 
81534f2f8cfSMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
816ae46e4c7SMatthew Ahrens 	} else {
817ae46e4c7SMatthew Ahrens 		/* if full, most recent snapshot must be $ORIGIN */
818ae46e4c7SMatthew Ahrens 		if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL)
819be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENODEV));
82034f2f8cfSMatthew Ahrens 		drba->drba_snapobj = ds->ds_phys->ds_prev_snap_obj;
821ae46e4c7SMatthew Ahrens 	}
8223cb34c60Sahrens 
8233cb34c60Sahrens 	return (0);
8243b2aab18SMatthew Ahrens 
8253b2aab18SMatthew Ahrens }
8263b2aab18SMatthew Ahrens 
8273b2aab18SMatthew Ahrens static int
8283b2aab18SMatthew Ahrens dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
8293b2aab18SMatthew Ahrens {
8303b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
8313b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
8323b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
8333b2aab18SMatthew Ahrens 	uint64_t fromguid = drrb->drr_fromguid;
8343b2aab18SMatthew Ahrens 	int flags = drrb->drr_flags;
8353b2aab18SMatthew Ahrens 	int error;
8363b2aab18SMatthew Ahrens 	dsl_dataset_t *ds;
8373b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
8383b2aab18SMatthew Ahrens 
8393b2aab18SMatthew Ahrens 	/* already checked */
8403b2aab18SMatthew Ahrens 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
8413b2aab18SMatthew Ahrens 
8423b2aab18SMatthew Ahrens 	if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
8433b2aab18SMatthew Ahrens 	    DMU_COMPOUNDSTREAM ||
8443b2aab18SMatthew Ahrens 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
8453b2aab18SMatthew Ahrens 	    ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
846be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
8473b2aab18SMatthew Ahrens 
8483b2aab18SMatthew Ahrens 	/* Verify pool version supports SA if SA_SPILL feature set */
8493b2aab18SMatthew Ahrens 	if ((DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
8503b2aab18SMatthew Ahrens 	    DMU_BACKUP_FEATURE_SA_SPILL) &&
8513b2aab18SMatthew Ahrens 	    spa_version(dp->dp_spa) < SPA_VERSION_SA) {
852be6fd75aSMatthew Ahrens 		return (SET_ERROR(ENOTSUP));
8533b2aab18SMatthew Ahrens 	}
8543b2aab18SMatthew Ahrens 
8553b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
8563b2aab18SMatthew Ahrens 	if (error == 0) {
8573b2aab18SMatthew Ahrens 		/* target fs already exists; recv into temp clone */
8583b2aab18SMatthew Ahrens 
8593b2aab18SMatthew Ahrens 		/* Can't recv a clone into an existing fs */
8603b2aab18SMatthew Ahrens 		if (flags & DRR_FLAG_CLONE) {
8613b2aab18SMatthew Ahrens 			dsl_dataset_rele(ds, FTAG);
862be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
8633b2aab18SMatthew Ahrens 		}
8643b2aab18SMatthew Ahrens 
8653b2aab18SMatthew Ahrens 		error = recv_begin_check_existing_impl(drba, ds, fromguid);
8663b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
8673b2aab18SMatthew Ahrens 	} else if (error == ENOENT) {
8683b2aab18SMatthew Ahrens 		/* target fs does not exist; must be a full backup or clone */
8693b2aab18SMatthew Ahrens 		char buf[MAXNAMELEN];
8703b2aab18SMatthew Ahrens 
8713b2aab18SMatthew Ahrens 		/*
8723b2aab18SMatthew Ahrens 		 * If it's a non-clone incremental, we are missing the
8733b2aab18SMatthew Ahrens 		 * target fs, so fail the recv.
8743b2aab18SMatthew Ahrens 		 */
8753b2aab18SMatthew Ahrens 		if (fromguid != 0 && !(flags & DRR_FLAG_CLONE))
876be6fd75aSMatthew Ahrens 			return (SET_ERROR(ENOENT));
8773b2aab18SMatthew Ahrens 
8783b2aab18SMatthew Ahrens 		/* Open the parent of tofs */
8793b2aab18SMatthew Ahrens 		ASSERT3U(strlen(tofs), <, MAXNAMELEN);
8803b2aab18SMatthew Ahrens 		(void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
8813b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, buf, FTAG, &ds);
8823b2aab18SMatthew Ahrens 		if (error != 0)
8833b2aab18SMatthew Ahrens 			return (error);
8843b2aab18SMatthew Ahrens 
8853b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
8863b2aab18SMatthew Ahrens 			dsl_dataset_t *origin;
8873b2aab18SMatthew Ahrens 			error = dsl_dataset_hold(dp, drba->drba_origin,
8883b2aab18SMatthew Ahrens 			    FTAG, &origin);
8893b2aab18SMatthew Ahrens 			if (error != 0) {
8903b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
8913b2aab18SMatthew Ahrens 				return (error);
8923b2aab18SMatthew Ahrens 			}
8933b2aab18SMatthew Ahrens 			if (!dsl_dataset_is_snapshot(origin)) {
8943b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
8953b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
896be6fd75aSMatthew Ahrens 				return (SET_ERROR(EINVAL));
8973b2aab18SMatthew Ahrens 			}
8983b2aab18SMatthew Ahrens 			if (origin->ds_phys->ds_guid != fromguid) {
8993b2aab18SMatthew Ahrens 				dsl_dataset_rele(origin, FTAG);
9003b2aab18SMatthew Ahrens 				dsl_dataset_rele(ds, FTAG);
901be6fd75aSMatthew Ahrens 				return (SET_ERROR(ENODEV));
9023b2aab18SMatthew Ahrens 			}
9033b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
9043b2aab18SMatthew Ahrens 		}
9053b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
9063b2aab18SMatthew Ahrens 		error = 0;
9073b2aab18SMatthew Ahrens 	}
9083b2aab18SMatthew Ahrens 	return (error);
909f18faf3fSek }
910f18faf3fSek 
911f18faf3fSek static void
9123b2aab18SMatthew Ahrens dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
913f18faf3fSek {
9143b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t *drba = arg;
9153b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
9163b2aab18SMatthew Ahrens 	struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
9173b2aab18SMatthew Ahrens 	const char *tofs = drba->drba_cookie->drc_tofs;
9183b2aab18SMatthew Ahrens 	dsl_dataset_t *ds, *newds;
919f18faf3fSek 	uint64_t dsobj;
9203b2aab18SMatthew Ahrens 	int error;
9213b2aab18SMatthew Ahrens 	uint64_t crflags;
9223b2aab18SMatthew Ahrens 
9233b2aab18SMatthew Ahrens 	crflags = (drrb->drr_flags & DRR_FLAG_CI_DATA) ?
9243b2aab18SMatthew Ahrens 	    DS_FLAG_CI_DATASET : 0;
925f18faf3fSek 
9263b2aab18SMatthew Ahrens 	error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
9273b2aab18SMatthew Ahrens 	if (error == 0) {
9283b2aab18SMatthew Ahrens 		/* create temporary clone */
92934f2f8cfSMatthew Ahrens 		dsl_dataset_t *snap = NULL;
93034f2f8cfSMatthew Ahrens 		if (drba->drba_snapobj != 0) {
93134f2f8cfSMatthew Ahrens 			VERIFY0(dsl_dataset_hold_obj(dp,
93234f2f8cfSMatthew Ahrens 			    drba->drba_snapobj, FTAG, &snap));
93334f2f8cfSMatthew Ahrens 		}
9343b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
93534f2f8cfSMatthew Ahrens 		    snap, crflags, drba->drba_cred, tx);
93634f2f8cfSMatthew Ahrens 		dsl_dataset_rele(snap, FTAG);
9373b2aab18SMatthew Ahrens 		dsl_dataset_rele(ds, FTAG);
9383b2aab18SMatthew Ahrens 	} else {
9393b2aab18SMatthew Ahrens 		dsl_dir_t *dd;
9403b2aab18SMatthew Ahrens 		const char *tail;
9413b2aab18SMatthew Ahrens 		dsl_dataset_t *origin = NULL;
9423b2aab18SMatthew Ahrens 
9433b2aab18SMatthew Ahrens 		VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
9443b2aab18SMatthew Ahrens 
9453b2aab18SMatthew Ahrens 		if (drba->drba_origin != NULL) {
9463b2aab18SMatthew Ahrens 			VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
9473b2aab18SMatthew Ahrens 			    FTAG, &origin));
9483b2aab18SMatthew Ahrens 		}
9493b2aab18SMatthew Ahrens 
9503b2aab18SMatthew Ahrens 		/* Create new dataset. */
9513b2aab18SMatthew Ahrens 		dsobj = dsl_dataset_create_sync(dd,
9523b2aab18SMatthew Ahrens 		    strrchr(tofs, '/') + 1,
9533b2aab18SMatthew Ahrens 		    origin, crflags, drba->drba_cred, tx);
9543b2aab18SMatthew Ahrens 		if (origin != NULL)
9553b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin, FTAG);
9563b2aab18SMatthew Ahrens 		dsl_dir_rele(dd, FTAG);
9573b2aab18SMatthew Ahrens 		drba->drba_cookie->drc_newfs = B_TRUE;
9583b2aab18SMatthew Ahrens 	}
9593b2aab18SMatthew Ahrens 	VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
9603b2aab18SMatthew Ahrens 
9613b2aab18SMatthew Ahrens 	dmu_buf_will_dirty(newds->ds_dbuf, tx);
9623b2aab18SMatthew Ahrens 	newds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
9633cb34c60Sahrens 
964ae46e4c7SMatthew Ahrens 	/*
965ae46e4c7SMatthew Ahrens 	 * If we actually created a non-clone, we need to create the
966ae46e4c7SMatthew Ahrens 	 * objset in our new dataset.
967ae46e4c7SMatthew Ahrens 	 */
9683b2aab18SMatthew Ahrens 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
969ae46e4c7SMatthew Ahrens 		(void) dmu_objset_create_impl(dp->dp_spa,
9703b2aab18SMatthew Ahrens 		    newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
971ae46e4c7SMatthew Ahrens 	}
972ae46e4c7SMatthew Ahrens 
9733b2aab18SMatthew Ahrens 	drba->drba_cookie->drc_ds = newds;
974dc7cd546SMark Shellenbaum 
9753b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(newds, "receive", tx, "");
976dc7cd546SMark Shellenbaum }
977dc7cd546SMark Shellenbaum 
9783cb34c60Sahrens /*
9793cb34c60Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
9803cb34c60Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
9813cb34c60Sahrens  */
9823cb34c60Sahrens int
9833b2aab18SMatthew Ahrens dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
9843b2aab18SMatthew Ahrens     boolean_t force, char *origin, dmu_recv_cookie_t *drc)
985efb80947Sahrens {
9863b2aab18SMatthew Ahrens 	dmu_recv_begin_arg_t drba = { 0 };
9873b2aab18SMatthew Ahrens 	dmu_replay_record_t *drr;
988ab04eb8eStimh 
9893cb34c60Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
9903cb34c60Sahrens 	drc->drc_drrb = drrb;
9913cb34c60Sahrens 	drc->drc_tosnap = tosnap;
9923b2aab18SMatthew Ahrens 	drc->drc_tofs = tofs;
9933cb34c60Sahrens 	drc->drc_force = force;
994efb80947Sahrens 
9953b2aab18SMatthew Ahrens 	if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
9963b2aab18SMatthew Ahrens 		drc->drc_byteswap = B_TRUE;
9973b2aab18SMatthew Ahrens 	else if (drrb->drr_magic != DMU_BACKUP_MAGIC)
998be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
999efb80947Sahrens 
10003b2aab18SMatthew Ahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
10013b2aab18SMatthew Ahrens 	drr->drr_type = DRR_BEGIN;
10023b2aab18SMatthew Ahrens 	drr->drr_u.drr_begin = *drc->drc_drrb;
10033b2aab18SMatthew Ahrens 	if (drc->drc_byteswap) {
10043b2aab18SMatthew Ahrens 		fletcher_4_incremental_byteswap(drr,
10053b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
10063b2aab18SMatthew Ahrens 	} else {
10073b2aab18SMatthew Ahrens 		fletcher_4_incremental_native(drr,
10083b2aab18SMatthew Ahrens 		    sizeof (dmu_replay_record_t), &drc->drc_cksum);
10093b2aab18SMatthew Ahrens 	}
10103b2aab18SMatthew Ahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
1011dc7cd546SMark Shellenbaum 
10123b2aab18SMatthew Ahrens 	if (drc->drc_byteswap) {
10133b2aab18SMatthew Ahrens 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
10143b2aab18SMatthew Ahrens 		drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
10153b2aab18SMatthew Ahrens 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
10163b2aab18SMatthew Ahrens 		drrb->drr_type = BSWAP_32(drrb->drr_type);
10173b2aab18SMatthew Ahrens 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
10183b2aab18SMatthew Ahrens 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
10193cb34c60Sahrens 	}
10203cb34c60Sahrens 
10213b2aab18SMatthew Ahrens 	drba.drba_origin = origin;
10223b2aab18SMatthew Ahrens 	drba.drba_cookie = drc;
10233b2aab18SMatthew Ahrens 	drba.drba_cred = CRED();
10243b2aab18SMatthew Ahrens 
10253b2aab18SMatthew Ahrens 	return (dsl_sync_task(tofs, dmu_recv_begin_check, dmu_recv_begin_sync,
10263b2aab18SMatthew Ahrens 	    &drba, 5));
1027efb80947Sahrens }
1028efb80947Sahrens 
10293cb34c60Sahrens struct restorearg {
10303cb34c60Sahrens 	int err;
10313b2aab18SMatthew Ahrens 	boolean_t byteswap;
10323cb34c60Sahrens 	vnode_t *vp;
10333cb34c60Sahrens 	char *buf;
10343cb34c60Sahrens 	uint64_t voff;
10353cb34c60Sahrens 	int bufsize; /* amount of memory allocated for buf */
10363cb34c60Sahrens 	zio_cksum_t cksum;
1037c99e4bdcSChris Kirby 	avl_tree_t *guid_to_ds_map;
10383cb34c60Sahrens };
10393cb34c60Sahrens 
10409e69d7d0SLori Alt typedef struct guid_map_entry {
10419e69d7d0SLori Alt 	uint64_t	guid;
10429e69d7d0SLori Alt 	dsl_dataset_t	*gme_ds;
10439e69d7d0SLori Alt 	avl_node_t	avlnode;
10449e69d7d0SLori Alt } guid_map_entry_t;
10459e69d7d0SLori Alt 
10469e69d7d0SLori Alt static int
10479e69d7d0SLori Alt guid_compare(const void *arg1, const void *arg2)
10489e69d7d0SLori Alt {
10499e69d7d0SLori Alt 	const guid_map_entry_t *gmep1 = arg1;
10509e69d7d0SLori Alt 	const guid_map_entry_t *gmep2 = arg2;
10519e69d7d0SLori Alt 
10529e69d7d0SLori Alt 	if (gmep1->guid < gmep2->guid)
10539e69d7d0SLori Alt 		return (-1);
10549e69d7d0SLori Alt 	else if (gmep1->guid > gmep2->guid)
10559e69d7d0SLori Alt 		return (1);
10569e69d7d0SLori Alt 	return (0);
10579e69d7d0SLori Alt }
10589e69d7d0SLori Alt 
1059c99e4bdcSChris Kirby static void
1060c99e4bdcSChris Kirby free_guid_map_onexit(void *arg)
1061c99e4bdcSChris Kirby {
1062c99e4bdcSChris Kirby 	avl_tree_t *ca = arg;
1063c99e4bdcSChris Kirby 	void *cookie = NULL;
1064c99e4bdcSChris Kirby 	guid_map_entry_t *gmep;
1065c99e4bdcSChris Kirby 
1066c99e4bdcSChris Kirby 	while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
10673b2aab18SMatthew Ahrens 		dsl_dataset_long_rele(gmep->gme_ds, gmep);
1068de8d9cffSMatthew Ahrens 		dsl_dataset_rele(gmep->gme_ds, gmep);
1069c99e4bdcSChris Kirby 		kmem_free(gmep, sizeof (guid_map_entry_t));
1070c99e4bdcSChris Kirby 	}
1071c99e4bdcSChris Kirby 	avl_destroy(ca);
1072c99e4bdcSChris Kirby 	kmem_free(ca, sizeof (avl_tree_t));
1073c99e4bdcSChris Kirby }
1074c99e4bdcSChris Kirby 
1075efb80947Sahrens static void *
1076efb80947Sahrens restore_read(struct restorearg *ra, int len)
1077efb80947Sahrens {
1078efb80947Sahrens 	void *rv;
10793cb34c60Sahrens 	int done = 0;
1080efb80947Sahrens 
1081efb80947Sahrens 	/* some things will require 8-byte alignment, so everything must */
1082fb09f5aaSMadhav Suresh 	ASSERT0(len % 8);
1083efb80947Sahrens 
10843cb34c60Sahrens 	while (done < len) {
1085efb80947Sahrens 		ssize_t resid;
1086efb80947Sahrens 
1087efb80947Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
10883cb34c60Sahrens 		    (caddr_t)ra->buf + done, len - done,
1089efb80947Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
1090efb80947Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
1091efb80947Sahrens 
10923cb34c60Sahrens 		if (resid == len - done)
1093be6fd75aSMatthew Ahrens 			ra->err = SET_ERROR(EINVAL);
10943cb34c60Sahrens 		ra->voff += len - done - resid;
10953cb34c60Sahrens 		done = len - resid;
10963b2aab18SMatthew Ahrens 		if (ra->err != 0)
1097efb80947Sahrens 			return (NULL);
1098efb80947Sahrens 	}
1099efb80947Sahrens 
11003cb34c60Sahrens 	ASSERT3U(done, ==, len);
11013cb34c60Sahrens 	rv = ra->buf;
1102efb80947Sahrens 	if (ra->byteswap)
11033cb34c60Sahrens 		fletcher_4_incremental_byteswap(rv, len, &ra->cksum);
1104efb80947Sahrens 	else
11053cb34c60Sahrens 		fletcher_4_incremental_native(rv, len, &ra->cksum);
1106efb80947Sahrens 	return (rv);
1107efb80947Sahrens }
1108efb80947Sahrens 
1109efb80947Sahrens static void
1110efb80947Sahrens backup_byteswap(dmu_replay_record_t *drr)
1111efb80947Sahrens {
1112efb80947Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1113efb80947Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1114efb80947Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
11153cb34c60Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
1116efb80947Sahrens 	switch (drr->drr_type) {
1117efb80947Sahrens 	case DRR_BEGIN:
1118efb80947Sahrens 		DO64(drr_begin.drr_magic);
11199e69d7d0SLori Alt 		DO64(drr_begin.drr_versioninfo);
1120efb80947Sahrens 		DO64(drr_begin.drr_creation_time);
1121efb80947Sahrens 		DO32(drr_begin.drr_type);
11223cb34c60Sahrens 		DO32(drr_begin.drr_flags);
1123efb80947Sahrens 		DO64(drr_begin.drr_toguid);
1124efb80947Sahrens 		DO64(drr_begin.drr_fromguid);
1125efb80947Sahrens 		break;
1126efb80947Sahrens 	case DRR_OBJECT:
1127efb80947Sahrens 		DO64(drr_object.drr_object);
1128efb80947Sahrens 		/* DO64(drr_object.drr_allocation_txg); */
1129efb80947Sahrens 		DO32(drr_object.drr_type);
1130efb80947Sahrens 		DO32(drr_object.drr_bonustype);
1131efb80947Sahrens 		DO32(drr_object.drr_blksz);
1132efb80947Sahrens 		DO32(drr_object.drr_bonuslen);
11339e69d7d0SLori Alt 		DO64(drr_object.drr_toguid);
1134efb80947Sahrens 		break;
1135efb80947Sahrens 	case DRR_FREEOBJECTS:
1136efb80947Sahrens 		DO64(drr_freeobjects.drr_firstobj);
1137efb80947Sahrens 		DO64(drr_freeobjects.drr_numobjs);
11389e69d7d0SLori Alt 		DO64(drr_freeobjects.drr_toguid);
1139efb80947Sahrens 		break;
1140efb80947Sahrens 	case DRR_WRITE:
1141efb80947Sahrens 		DO64(drr_write.drr_object);
1142efb80947Sahrens 		DO32(drr_write.drr_type);
1143efb80947Sahrens 		DO64(drr_write.drr_offset);
1144efb80947Sahrens 		DO64(drr_write.drr_length);
11459e69d7d0SLori Alt 		DO64(drr_write.drr_toguid);
11468e714474SLori Alt 		DO64(drr_write.drr_key.ddk_cksum.zc_word[0]);
11478e714474SLori Alt 		DO64(drr_write.drr_key.ddk_cksum.zc_word[1]);
11488e714474SLori Alt 		DO64(drr_write.drr_key.ddk_cksum.zc_word[2]);
11498e714474SLori Alt 		DO64(drr_write.drr_key.ddk_cksum.zc_word[3]);
11508e714474SLori Alt 		DO64(drr_write.drr_key.ddk_prop);
11519e69d7d0SLori Alt 		break;
11529e69d7d0SLori Alt 	case DRR_WRITE_BYREF:
11539e69d7d0SLori Alt 		DO64(drr_write_byref.drr_object);
11549e69d7d0SLori Alt 		DO64(drr_write_byref.drr_offset);
11559e69d7d0SLori Alt 		DO64(drr_write_byref.drr_length);
11569e69d7d0SLori Alt 		DO64(drr_write_byref.drr_toguid);
11579e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refguid);
11589e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refobject);
11599e69d7d0SLori Alt 		DO64(drr_write_byref.drr_refoffset);
11608e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[0]);
11618e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[1]);
11628e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[2]);
11638e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[3]);
11648e714474SLori Alt 		DO64(drr_write_byref.drr_key.ddk_prop);
1165efb80947Sahrens 		break;
1166efb80947Sahrens 	case DRR_FREE:
1167efb80947Sahrens 		DO64(drr_free.drr_object);
1168efb80947Sahrens 		DO64(drr_free.drr_offset);
1169efb80947Sahrens 		DO64(drr_free.drr_length);
11709e69d7d0SLori Alt 		DO64(drr_free.drr_toguid);
1171efb80947Sahrens 		break;
11720a586ceaSMark Shellenbaum 	case DRR_SPILL:
11730a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_object);
11740a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_length);
11750a586ceaSMark Shellenbaum 		DO64(drr_spill.drr_toguid);
11760a586ceaSMark Shellenbaum 		break;
1177efb80947Sahrens 	case DRR_END:
1178efb80947Sahrens 		DO64(drr_end.drr_checksum.zc_word[0]);
1179efb80947Sahrens 		DO64(drr_end.drr_checksum.zc_word[1]);
1180efb80947Sahrens 		DO64(drr_end.drr_checksum.zc_word[2]);
1181efb80947Sahrens 		DO64(drr_end.drr_checksum.zc_word[3]);
11829e69d7d0SLori Alt 		DO64(drr_end.drr_toguid);
1183efb80947Sahrens 		break;
1184efb80947Sahrens 	}
1185efb80947Sahrens #undef DO64
1186efb80947Sahrens #undef DO32
1187efb80947Sahrens }
1188efb80947Sahrens 
1189efb80947Sahrens static int
1190efb80947Sahrens restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
1191efb80947Sahrens {
1192efb80947Sahrens 	int err;
1193efb80947Sahrens 	dmu_tx_t *tx;
1194adee0b6fSTim Haley 	void *data = NULL;
1195efb80947Sahrens 
1196efb80947Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
1197ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_type) ||
1198ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drro->drr_bonustype) ||
11999e69d7d0SLori Alt 	    drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1200efb80947Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1201efb80947Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1202efb80947Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
1203efb80947Sahrens 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
1204efb80947Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1205be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1206efb80947Sahrens 	}
1207efb80947Sahrens 
12082bf405a2SMark Maybee 	err = dmu_object_info(os, drro->drr_object, NULL);
12092bf405a2SMark Maybee 
12102bf405a2SMark Maybee 	if (err != 0 && err != ENOENT)
1211be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12122bf405a2SMark Maybee 
1213adee0b6fSTim Haley 	if (drro->drr_bonuslen) {
1214adee0b6fSTim Haley 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
12153b2aab18SMatthew Ahrens 		if (ra->err != 0)
1216adee0b6fSTim Haley 			return (ra->err);
1217adee0b6fSTim Haley 	}
1218adee0b6fSTim Haley 
1219efb80947Sahrens 	if (err == ENOENT) {
1220efb80947Sahrens 		/* currently free, want to be allocated */
12212bf405a2SMark Maybee 		tx = dmu_tx_create(os);
1222efb80947Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
1223efb80947Sahrens 		err = dmu_tx_assign(tx, TXG_WAIT);
12243b2aab18SMatthew Ahrens 		if (err != 0) {
1225efb80947Sahrens 			dmu_tx_abort(tx);
1226efb80947Sahrens 			return (err);
1227efb80947Sahrens 		}
1228efb80947Sahrens 		err = dmu_object_claim(os, drro->drr_object,
1229efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
1230efb80947Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
12312bf405a2SMark Maybee 		dmu_tx_commit(tx);
1232efb80947Sahrens 	} else {
1233efb80947Sahrens 		/* currently allocated, want to be allocated */
1234efb80947Sahrens 		err = dmu_object_reclaim(os, drro->drr_object,
1235efb80947Sahrens 		    drro->drr_type, drro->drr_blksz,
12362bf405a2SMark Maybee 		    drro->drr_bonustype, drro->drr_bonuslen);
1237efb80947Sahrens 	}
12383b2aab18SMatthew Ahrens 	if (err != 0) {
1239be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
12400a586ceaSMark Shellenbaum 	}
12412bf405a2SMark Maybee 
12422bf405a2SMark Maybee 	tx = dmu_tx_create(os);
12432bf405a2SMark Maybee 	dmu_tx_hold_bonus(tx, drro->drr_object);
12442bf405a2SMark Maybee 	err = dmu_tx_assign(tx, TXG_WAIT);
12453b2aab18SMatthew Ahrens 	if (err != 0) {
12462bf405a2SMark Maybee 		dmu_tx_abort(tx);
12472bf405a2SMark Maybee 		return (err);
1248efb80947Sahrens 	}
1249efb80947Sahrens 
12509e69d7d0SLori Alt 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksumtype,
12519e69d7d0SLori Alt 	    tx);
1252efb80947Sahrens 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
1253efb80947Sahrens 
1254adee0b6fSTim Haley 	if (data != NULL) {
1255efb80947Sahrens 		dmu_buf_t *db;
1256adee0b6fSTim Haley 
1257efb80947Sahrens 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
1258efb80947Sahrens 		dmu_buf_will_dirty(db, tx);
1259efb80947Sahrens 
12601934e92fSmaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
12611934e92fSmaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
1262efb80947Sahrens 		if (ra->byteswap) {
1263ad135b5dSChristopher Siden 			dmu_object_byteswap_t byteswap =
1264ad135b5dSChristopher Siden 			    DMU_OT_BYTESWAP(drro->drr_bonustype);
1265ad135b5dSChristopher Siden 			dmu_ot_byteswap[byteswap].ob_func(db->db_data,
1266efb80947Sahrens 			    drro->drr_bonuslen);
1267efb80947Sahrens 		}
1268efb80947Sahrens 		dmu_buf_rele(db, FTAG);
1269efb80947Sahrens 	}
1270efb80947Sahrens 	dmu_tx_commit(tx);
1271efb80947Sahrens 	return (0);
1272efb80947Sahrens }
1273efb80947Sahrens 
1274efb80947Sahrens /* ARGSUSED */
1275efb80947Sahrens static int
1276efb80947Sahrens restore_freeobjects(struct restorearg *ra, objset_t *os,
1277efb80947Sahrens     struct drr_freeobjects *drrfo)
1278efb80947Sahrens {
1279efb80947Sahrens 	uint64_t obj;
1280efb80947Sahrens 
1281efb80947Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
1282be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1283efb80947Sahrens 
1284efb80947Sahrens 	for (obj = drrfo->drr_firstobj;
1285432f72fdSahrens 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
1286432f72fdSahrens 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
1287efb80947Sahrens 		int err;
1288efb80947Sahrens 
1289efb80947Sahrens 		if (dmu_object_info(os, obj, NULL) != 0)
1290efb80947Sahrens 			continue;
1291efb80947Sahrens 
1292713d6c20SMatthew Ahrens 		err = dmu_free_long_object(os, obj);
12933b2aab18SMatthew Ahrens 		if (err != 0)
1294efb80947Sahrens 			return (err);
1295efb80947Sahrens 	}
1296efb80947Sahrens 	return (0);
1297efb80947Sahrens }
1298efb80947Sahrens 
1299efb80947Sahrens static int
1300efb80947Sahrens restore_write(struct restorearg *ra, objset_t *os,
1301efb80947Sahrens     struct drr_write *drrw)
1302efb80947Sahrens {
1303efb80947Sahrens 	dmu_tx_t *tx;
1304efb80947Sahrens 	void *data;
1305efb80947Sahrens 	int err;
1306efb80947Sahrens 
1307efb80947Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
1308ad135b5dSChristopher Siden 	    !DMU_OT_IS_VALID(drrw->drr_type))
1309be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1310efb80947Sahrens 
1311efb80947Sahrens 	data = restore_read(ra, drrw->drr_length);
1312efb80947Sahrens 	if (data == NULL)
1313efb80947Sahrens 		return (ra->err);
1314efb80947Sahrens 
1315efb80947Sahrens 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
1316be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1317efb80947Sahrens 
1318efb80947Sahrens 	tx = dmu_tx_create(os);
1319efb80947Sahrens 
1320efb80947Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
1321efb80947Sahrens 	    drrw->drr_offset, drrw->drr_length);
1322efb80947Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
13233b2aab18SMatthew Ahrens 	if (err != 0) {
1324efb80947Sahrens 		dmu_tx_abort(tx);
1325efb80947Sahrens 		return (err);
1326efb80947Sahrens 	}
1327ad135b5dSChristopher Siden 	if (ra->byteswap) {
1328ad135b5dSChristopher Siden 		dmu_object_byteswap_t byteswap =
1329ad135b5dSChristopher Siden 		    DMU_OT_BYTESWAP(drrw->drr_type);
1330ad135b5dSChristopher Siden 		dmu_ot_byteswap[byteswap].ob_func(data, drrw->drr_length);
1331ad135b5dSChristopher Siden 	}
1332efb80947Sahrens 	dmu_write(os, drrw->drr_object,
1333efb80947Sahrens 	    drrw->drr_offset, drrw->drr_length, data, tx);
1334efb80947Sahrens 	dmu_tx_commit(tx);
1335efb80947Sahrens 	return (0);
1336efb80947Sahrens }
1337efb80947Sahrens 
13389e69d7d0SLori Alt /*
13399e69d7d0SLori Alt  * Handle a DRR_WRITE_BYREF record.  This record is used in dedup'ed
13409e69d7d0SLori Alt  * streams to refer to a copy of the data that is already on the
13419e69d7d0SLori Alt  * system because it came in earlier in the stream.  This function
13429e69d7d0SLori Alt  * finds the earlier copy of the data, and uses that copy instead of
13439e69d7d0SLori Alt  * data from the stream to fulfill this write.
13449e69d7d0SLori Alt  */
13459e69d7d0SLori Alt static int
13469e69d7d0SLori Alt restore_write_byref(struct restorearg *ra, objset_t *os,
13479e69d7d0SLori Alt     struct drr_write_byref *drrwbr)
13489e69d7d0SLori Alt {
13499e69d7d0SLori Alt 	dmu_tx_t *tx;
13509e69d7d0SLori Alt 	int err;
13519e69d7d0SLori Alt 	guid_map_entry_t gmesrch;
13529e69d7d0SLori Alt 	guid_map_entry_t *gmep;
13539e69d7d0SLori Alt 	avl_index_t	where;
13549e69d7d0SLori Alt 	objset_t *ref_os = NULL;
13559e69d7d0SLori Alt 	dmu_buf_t *dbp;
13569e69d7d0SLori Alt 
13579e69d7d0SLori Alt 	if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
1358be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
13599e69d7d0SLori Alt 
13609e69d7d0SLori Alt 	/*
13619e69d7d0SLori Alt 	 * If the GUID of the referenced dataset is different from the
13629e69d7d0SLori Alt 	 * GUID of the target dataset, find the referenced dataset.
13639e69d7d0SLori Alt 	 */
13649e69d7d0SLori Alt 	if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
13659e69d7d0SLori Alt 		gmesrch.guid = drrwbr->drr_refguid;
1366c99e4bdcSChris Kirby 		if ((gmep = avl_find(ra->guid_to_ds_map, &gmesrch,
13679e69d7d0SLori Alt 		    &where)) == NULL) {
1368be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
13699e69d7d0SLori Alt 		}
13709e69d7d0SLori Alt 		if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
1371be6fd75aSMatthew Ahrens 			return (SET_ERROR(EINVAL));
13729e69d7d0SLori Alt 	} else {
13739e69d7d0SLori Alt 		ref_os = os;
13749e69d7d0SLori Alt 	}
13759e69d7d0SLori Alt 
13769e69d7d0SLori Alt 	if (err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
137747cb52daSJeff Bonwick 	    drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH))
13789e69d7d0SLori Alt 		return (err);
13799e69d7d0SLori Alt 
13809e69d7d0SLori Alt 	tx = dmu_tx_create(os);
13819e69d7d0SLori Alt 
13829e69d7d0SLori Alt 	dmu_tx_hold_write(tx, drrwbr->drr_object,
13839e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length);
13849e69d7d0SLori Alt 	err = dmu_tx_assign(tx, TXG_WAIT);
13853b2aab18SMatthew Ahrens 	if (err != 0) {
13869e69d7d0SLori Alt 		dmu_tx_abort(tx);
13879e69d7d0SLori Alt 		return (err);
13889e69d7d0SLori Alt 	}
13899e69d7d0SLori Alt 	dmu_write(os, drrwbr->drr_object,
13909e69d7d0SLori Alt 	    drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
13919e69d7d0SLori Alt 	dmu_buf_rele(dbp, FTAG);
13929e69d7d0SLori Alt 	dmu_tx_commit(tx);
13939e69d7d0SLori Alt 	return (0);
13949e69d7d0SLori Alt }
13959e69d7d0SLori Alt 
13960a586ceaSMark Shellenbaum static int
13970a586ceaSMark Shellenbaum restore_spill(struct restorearg *ra, objset_t *os, struct drr_spill *drrs)
13980a586ceaSMark Shellenbaum {
13990a586ceaSMark Shellenbaum 	dmu_tx_t *tx;
14000a586ceaSMark Shellenbaum 	void *data;
14010a586ceaSMark Shellenbaum 	dmu_buf_t *db, *db_spill;
14020a586ceaSMark Shellenbaum 	int err;
14030a586ceaSMark Shellenbaum 
14040a586ceaSMark Shellenbaum 	if (drrs->drr_length < SPA_MINBLOCKSIZE ||
14050a586ceaSMark Shellenbaum 	    drrs->drr_length > SPA_MAXBLOCKSIZE)
1406be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
14070a586ceaSMark Shellenbaum 
14080a586ceaSMark Shellenbaum 	data = restore_read(ra, drrs->drr_length);
14090a586ceaSMark Shellenbaum 	if (data == NULL)
14100a586ceaSMark Shellenbaum 		return (ra->err);
14110a586ceaSMark Shellenbaum 
14120a586ceaSMark Shellenbaum 	if (dmu_object_info(os, drrs->drr_object, NULL) != 0)
1413be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
14140a586ceaSMark Shellenbaum 
14150a586ceaSMark Shellenbaum 	VERIFY(0 == dmu_bonus_hold(os, drrs->drr_object, FTAG, &db));
14160a586ceaSMark Shellenbaum 	if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
14170a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
14180a586ceaSMark Shellenbaum 		return (err);
14190a586ceaSMark Shellenbaum 	}
14200a586ceaSMark Shellenbaum 
14210a586ceaSMark Shellenbaum 	tx = dmu_tx_create(os);
14220a586ceaSMark Shellenbaum 
14230a586ceaSMark Shellenbaum 	dmu_tx_hold_spill(tx, db->db_object);
14240a586ceaSMark Shellenbaum 
14250a586ceaSMark Shellenbaum 	err = dmu_tx_assign(tx, TXG_WAIT);
14263b2aab18SMatthew Ahrens 	if (err != 0) {
14270a586ceaSMark Shellenbaum 		dmu_buf_rele(db, FTAG);
14280a586ceaSMark Shellenbaum 		dmu_buf_rele(db_spill, FTAG);
14290a586ceaSMark Shellenbaum 		dmu_tx_abort(tx);
14300a586ceaSMark Shellenbaum 		return (err);
14310a586ceaSMark Shellenbaum 	}
14320a586ceaSMark Shellenbaum 	dmu_buf_will_dirty(db_spill, tx);
14330a586ceaSMark Shellenbaum 
14340a586ceaSMark Shellenbaum 	if (db_spill->db_size < drrs->drr_length)
14350a586ceaSMark Shellenbaum 		VERIFY(0 == dbuf_spill_set_blksz(db_spill,
14360a586ceaSMark Shellenbaum 		    drrs->drr_length, tx));
14370a586ceaSMark Shellenbaum 	bcopy(data, db_spill->db_data, drrs->drr_length);
14380a586ceaSMark Shellenbaum 
14390a586ceaSMark Shellenbaum 	dmu_buf_rele(db, FTAG);
14400a586ceaSMark Shellenbaum 	dmu_buf_rele(db_spill, FTAG);
14410a586ceaSMark Shellenbaum 
14420a586ceaSMark Shellenbaum 	dmu_tx_commit(tx);
14430a586ceaSMark Shellenbaum 	return (0);
14440a586ceaSMark Shellenbaum }
14450a586ceaSMark Shellenbaum 
1446efb80947Sahrens /* ARGSUSED */
1447efb80947Sahrens static int
1448efb80947Sahrens restore_free(struct restorearg *ra, objset_t *os,
1449efb80947Sahrens     struct drr_free *drrf)
1450efb80947Sahrens {
1451efb80947Sahrens 	int err;
1452efb80947Sahrens 
1453efb80947Sahrens 	if (drrf->drr_length != -1ULL &&
1454efb80947Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
1455be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1456efb80947Sahrens 
1457efb80947Sahrens 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
1458be6fd75aSMatthew Ahrens 		return (SET_ERROR(EINVAL));
1459efb80947Sahrens 
1460cdb0ab79Smaybee 	err = dmu_free_long_range(os, drrf->drr_object,
1461efb80947Sahrens 	    drrf->drr_offset, drrf->drr_length);
1462efb80947Sahrens 	return (err);
1463efb80947Sahrens }
1464efb80947Sahrens 
14653b2aab18SMatthew Ahrens /* used to destroy the drc_ds on error */
14663b2aab18SMatthew Ahrens static void
14673b2aab18SMatthew Ahrens dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
14683b2aab18SMatthew Ahrens {
14693b2aab18SMatthew Ahrens 	char name[MAXNAMELEN];
14703b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
14713b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
14723b2aab18SMatthew Ahrens 	(void) dsl_destroy_head(name);
14733b2aab18SMatthew Ahrens }
14743b2aab18SMatthew Ahrens 
14753cb34c60Sahrens /*
14763cb34c60Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
14773cb34c60Sahrens  */
1478efb80947Sahrens int
1479c99e4bdcSChris Kirby dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
1480c99e4bdcSChris Kirby     int cleanup_fd, uint64_t *action_handlep)
1481efb80947Sahrens {
14823cb34c60Sahrens 	struct restorearg ra = { 0 };
1483efb80947Sahrens 	dmu_replay_record_t *drr;
14843cb34c60Sahrens 	objset_t *os;
14853cb34c60Sahrens 	zio_cksum_t pcksum;
14869e69d7d0SLori Alt 	int featureflags;
1487efb80947Sahrens 
14883b2aab18SMatthew Ahrens 	ra.byteswap = drc->drc_byteswap;
14893b2aab18SMatthew Ahrens 	ra.cksum = drc->drc_cksum;
14903cb34c60Sahrens 	ra.vp = vp;
14913cb34c60Sahrens 	ra.voff = *voffp;
14923cb34c60Sahrens 	ra.bufsize = 1<<20;
14933cb34c60Sahrens 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
1494efb80947Sahrens 
14953cb34c60Sahrens 	/* these were verified in dmu_recv_begin */
14963b2aab18SMatthew Ahrens 	ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
14979e69d7d0SLori Alt 	    DMU_SUBSTREAM);
14983b2aab18SMatthew Ahrens 	ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
1499efb80947Sahrens 
1500efb80947Sahrens 	/*
1501efb80947Sahrens 	 * Open the objset we are modifying.
1502efb80947Sahrens 	 */
15033b2aab18SMatthew Ahrens 	VERIFY0(dmu_objset_from_ds(drc->drc_ds, &os));
1504efb80947Sahrens 
15053b2aab18SMatthew Ahrens 	ASSERT(drc->drc_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
1506efb80947Sahrens 
15079e69d7d0SLori Alt 	featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
15089e69d7d0SLori Alt 
15099e69d7d0SLori Alt 	/* if this stream is dedup'ed, set up the avl tree for guid mapping */
15109e69d7d0SLori Alt 	if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
1511a7f53a56SChris Kirby 		minor_t minor;
1512a7f53a56SChris Kirby 
1513c99e4bdcSChris Kirby 		if (cleanup_fd == -1) {
1514be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EBADF);
1515c99e4bdcSChris Kirby 			goto out;
1516c99e4bdcSChris Kirby 		}
1517a7f53a56SChris Kirby 		ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
15183b2aab18SMatthew Ahrens 		if (ra.err != 0) {
1519a7f53a56SChris Kirby 			cleanup_fd = -1;
1520a7f53a56SChris Kirby 			goto out;
1521a7f53a56SChris Kirby 		}
1522a7f53a56SChris Kirby 
1523c99e4bdcSChris Kirby 		if (*action_handlep == 0) {
1524c99e4bdcSChris Kirby 			ra.guid_to_ds_map =
1525c99e4bdcSChris Kirby 			    kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
1526c99e4bdcSChris Kirby 			avl_create(ra.guid_to_ds_map, guid_compare,
1527c99e4bdcSChris Kirby 			    sizeof (guid_map_entry_t),
1528c99e4bdcSChris Kirby 			    offsetof(guid_map_entry_t, avlnode));
1529a7f53a56SChris Kirby 			ra.err = zfs_onexit_add_cb(minor,
1530c99e4bdcSChris Kirby 			    free_guid_map_onexit, ra.guid_to_ds_map,
1531c99e4bdcSChris Kirby 			    action_handlep);
15323b2aab18SMatthew Ahrens 			if (ra.err != 0)
1533c99e4bdcSChris Kirby 				goto out;
1534c99e4bdcSChris Kirby 		} else {
1535a7f53a56SChris Kirby 			ra.err = zfs_onexit_cb_data(minor, *action_handlep,
1536c99e4bdcSChris Kirby 			    (void **)&ra.guid_to_ds_map);
15373b2aab18SMatthew Ahrens 			if (ra.err != 0)
1538c99e4bdcSChris Kirby 				goto out;
1539c99e4bdcSChris Kirby 		}
1540ec5cf9d5SAlexander Stetsenko 
1541ec5cf9d5SAlexander Stetsenko 		drc->drc_guid_to_ds_map = ra.guid_to_ds_map;
15429e69d7d0SLori Alt 	}
15439e69d7d0SLori Alt 
1544efb80947Sahrens 	/*
1545efb80947Sahrens 	 * Read records and process them.
1546efb80947Sahrens 	 */
15473cb34c60Sahrens 	pcksum = ra.cksum;
1548efb80947Sahrens 	while (ra.err == 0 &&
1549efb80947Sahrens 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
1550efb80947Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
1551be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EINTR);
1552efb80947Sahrens 			goto out;
1553efb80947Sahrens 		}
1554efb80947Sahrens 
1555efb80947Sahrens 		if (ra.byteswap)
1556efb80947Sahrens 			backup_byteswap(drr);
1557efb80947Sahrens 
1558efb80947Sahrens 		switch (drr->drr_type) {
1559efb80947Sahrens 		case DRR_OBJECT:
1560efb80947Sahrens 		{
1561efb80947Sahrens 			/*
1562efb80947Sahrens 			 * We need to make a copy of the record header,
1563efb80947Sahrens 			 * because restore_{object,write} may need to
1564efb80947Sahrens 			 * restore_read(), which will invalidate drr.
1565efb80947Sahrens 			 */
1566efb80947Sahrens 			struct drr_object drro = drr->drr_u.drr_object;
1567efb80947Sahrens 			ra.err = restore_object(&ra, os, &drro);
1568efb80947Sahrens 			break;
1569efb80947Sahrens 		}
1570efb80947Sahrens 		case DRR_FREEOBJECTS:
1571efb80947Sahrens 		{
1572efb80947Sahrens 			struct drr_freeobjects drrfo =
1573efb80947Sahrens 			    drr->drr_u.drr_freeobjects;
1574efb80947Sahrens 			ra.err = restore_freeobjects(&ra, os, &drrfo);
1575efb80947Sahrens 			break;
1576efb80947Sahrens 		}
1577efb80947Sahrens 		case DRR_WRITE:
1578efb80947Sahrens 		{
1579efb80947Sahrens 			struct drr_write drrw = drr->drr_u.drr_write;
1580efb80947Sahrens 			ra.err = restore_write(&ra, os, &drrw);
1581efb80947Sahrens 			break;
1582efb80947Sahrens 		}
15839e69d7d0SLori Alt 		case DRR_WRITE_BYREF:
15849e69d7d0SLori Alt 		{
15859e69d7d0SLori Alt 			struct drr_write_byref drrwbr =
15869e69d7d0SLori Alt 			    drr->drr_u.drr_write_byref;
15879e69d7d0SLori Alt 			ra.err = restore_write_byref(&ra, os, &drrwbr);
15889e69d7d0SLori Alt 			break;
15899e69d7d0SLori Alt 		}
1590efb80947Sahrens 		case DRR_FREE:
1591efb80947Sahrens 		{
1592efb80947Sahrens 			struct drr_free drrf = drr->drr_u.drr_free;
1593efb80947Sahrens 			ra.err = restore_free(&ra, os, &drrf);
1594efb80947Sahrens 			break;
1595efb80947Sahrens 		}
1596efb80947Sahrens 		case DRR_END:
1597efb80947Sahrens 		{
1598efb80947Sahrens 			struct drr_end drre = drr->drr_u.drr_end;
1599efb80947Sahrens 			/*
1600efb80947Sahrens 			 * We compare against the *previous* checksum
1601efb80947Sahrens 			 * value, because the stored checksum is of
1602efb80947Sahrens 			 * everything before the DRR_END record.
1603efb80947Sahrens 			 */
1604137fa067Sahrens 			if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
1605be6fd75aSMatthew Ahrens 				ra.err = SET_ERROR(ECKSUM);
1606efb80947Sahrens 			goto out;
1607efb80947Sahrens 		}
16080a586ceaSMark Shellenbaum 		case DRR_SPILL:
16090a586ceaSMark Shellenbaum 		{
16100a586ceaSMark Shellenbaum 			struct drr_spill drrs = drr->drr_u.drr_spill;
16110a586ceaSMark Shellenbaum 			ra.err = restore_spill(&ra, os, &drrs);
16120a586ceaSMark Shellenbaum 			break;
16130a586ceaSMark Shellenbaum 		}
1614efb80947Sahrens 		default:
1615be6fd75aSMatthew Ahrens 			ra.err = SET_ERROR(EINVAL);
1616efb80947Sahrens 			goto out;
1617efb80947Sahrens 		}
16183cb34c60Sahrens 		pcksum = ra.cksum;
1619efb80947Sahrens 	}
1620137fa067Sahrens 	ASSERT(ra.err != 0);
1621efb80947Sahrens 
1622efb80947Sahrens out:
1623a7f53a56SChris Kirby 	if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
1624a7f53a56SChris Kirby 		zfs_onexit_fd_rele(cleanup_fd);
1625a7f53a56SChris Kirby 
16263cb34c60Sahrens 	if (ra.err != 0) {
1627efb80947Sahrens 		/*
1628f4b94bdeSMatthew Ahrens 		 * destroy what we created, so we don't leave it in the
1629f4b94bdeSMatthew Ahrens 		 * inconsistent restoring state.
1630efb80947Sahrens 		 */
16313b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
1632efb80947Sahrens 	}
1633efb80947Sahrens 
1634efb80947Sahrens 	kmem_free(ra.buf, ra.bufsize);
16353cb34c60Sahrens 	*voffp = ra.voff;
1636efb80947Sahrens 	return (ra.err);
1637efb80947Sahrens }
1638f18faf3fSek 
16393cb34c60Sahrens static int
16403b2aab18SMatthew Ahrens dmu_recv_end_check(void *arg, dmu_tx_t *tx)
16413cb34c60Sahrens {
16423b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
16433b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
16443b2aab18SMatthew Ahrens 	int error;
16453b2aab18SMatthew Ahrens 
16463b2aab18SMatthew Ahrens 	ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
16473cb34c60Sahrens 
16483b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
16493b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
16503b2aab18SMatthew Ahrens 
16513b2aab18SMatthew Ahrens 		error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
16523b2aab18SMatthew Ahrens 		if (error != 0)
16533b2aab18SMatthew Ahrens 			return (error);
165434f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
165534f2f8cfSMatthew Ahrens 			/*
165634f2f8cfSMatthew Ahrens 			 * We will destroy any snapshots in tofs (i.e. before
165734f2f8cfSMatthew Ahrens 			 * origin_head) that are after the origin (which is
165834f2f8cfSMatthew Ahrens 			 * the snap before drc_ds, because drc_ds can not
165934f2f8cfSMatthew Ahrens 			 * have any snaps of its own).
166034f2f8cfSMatthew Ahrens 			 */
166134f2f8cfSMatthew Ahrens 			uint64_t obj = origin_head->ds_phys->ds_prev_snap_obj;
166234f2f8cfSMatthew Ahrens 			while (obj != drc->drc_ds->ds_phys->ds_prev_snap_obj) {
166334f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
166434f2f8cfSMatthew Ahrens 				error = dsl_dataset_hold_obj(dp, obj, FTAG,
166534f2f8cfSMatthew Ahrens 				    &snap);
166634f2f8cfSMatthew Ahrens 				if (error != 0)
166734f2f8cfSMatthew Ahrens 					return (error);
166834f2f8cfSMatthew Ahrens 				if (snap->ds_dir != origin_head->ds_dir)
166934f2f8cfSMatthew Ahrens 					error = SET_ERROR(EINVAL);
167034f2f8cfSMatthew Ahrens 				if (error == 0)  {
167134f2f8cfSMatthew Ahrens 					error = dsl_destroy_snapshot_check_impl(
167234f2f8cfSMatthew Ahrens 					    snap, B_FALSE);
167334f2f8cfSMatthew Ahrens 				}
167434f2f8cfSMatthew Ahrens 				obj = snap->ds_phys->ds_prev_snap_obj;
167534f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
167634f2f8cfSMatthew Ahrens 				if (error != 0)
167734f2f8cfSMatthew Ahrens 					return (error);
167834f2f8cfSMatthew Ahrens 			}
167934f2f8cfSMatthew Ahrens 		}
16803b2aab18SMatthew Ahrens 		error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
168191948b51SKeith M Wesolowski 		    origin_head, drc->drc_force, drc->drc_owner, tx);
16823b2aab18SMatthew Ahrens 		if (error != 0) {
16833b2aab18SMatthew Ahrens 			dsl_dataset_rele(origin_head, FTAG);
16843b2aab18SMatthew Ahrens 			return (error);
16853b2aab18SMatthew Ahrens 		}
16863b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(origin_head,
1687ca48f36fSKeith M Wesolowski 		    drc->drc_tosnap, tx, B_TRUE);
16883b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
16893b2aab18SMatthew Ahrens 		if (error != 0)
16903b2aab18SMatthew Ahrens 			return (error);
16913b2aab18SMatthew Ahrens 
16923b2aab18SMatthew Ahrens 		error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
16933b2aab18SMatthew Ahrens 	} else {
16943b2aab18SMatthew Ahrens 		error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
1695ca48f36fSKeith M Wesolowski 		    drc->drc_tosnap, tx, B_TRUE);
16963b2aab18SMatthew Ahrens 	}
16973b2aab18SMatthew Ahrens 	return (error);
16983cb34c60Sahrens }
16993cb34c60Sahrens 
17003cb34c60Sahrens static void
17013b2aab18SMatthew Ahrens dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
17023cb34c60Sahrens {
17033b2aab18SMatthew Ahrens 	dmu_recv_cookie_t *drc = arg;
17043b2aab18SMatthew Ahrens 	dsl_pool_t *dp = dmu_tx_pool(tx);
17053b2aab18SMatthew Ahrens 
17063b2aab18SMatthew Ahrens 	spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
17073b2aab18SMatthew Ahrens 	    tx, "snap=%s", drc->drc_tosnap);
17083b2aab18SMatthew Ahrens 
17093b2aab18SMatthew Ahrens 	if (!drc->drc_newfs) {
17103b2aab18SMatthew Ahrens 		dsl_dataset_t *origin_head;
17113b2aab18SMatthew Ahrens 
17123b2aab18SMatthew Ahrens 		VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
17133b2aab18SMatthew Ahrens 		    &origin_head));
171434f2f8cfSMatthew Ahrens 
171534f2f8cfSMatthew Ahrens 		if (drc->drc_force) {
171634f2f8cfSMatthew Ahrens 			/*
171734f2f8cfSMatthew Ahrens 			 * Destroy any snapshots of drc_tofs (origin_head)
171834f2f8cfSMatthew Ahrens 			 * after the origin (the snap before drc_ds).
171934f2f8cfSMatthew Ahrens 			 */
172034f2f8cfSMatthew Ahrens 			uint64_t obj = origin_head->ds_phys->ds_prev_snap_obj;
172134f2f8cfSMatthew Ahrens 			while (obj != drc->drc_ds->ds_phys->ds_prev_snap_obj) {
172234f2f8cfSMatthew Ahrens 				dsl_dataset_t *snap;
172334f2f8cfSMatthew Ahrens 				VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
172434f2f8cfSMatthew Ahrens 				    &snap));
172534f2f8cfSMatthew Ahrens 				ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
172634f2f8cfSMatthew Ahrens 				obj = snap->ds_phys->ds_prev_snap_obj;
172734f2f8cfSMatthew Ahrens 				dsl_destroy_snapshot_sync_impl(snap,
172834f2f8cfSMatthew Ahrens 				    B_FALSE, tx);
172934f2f8cfSMatthew Ahrens 				dsl_dataset_rele(snap, FTAG);
173034f2f8cfSMatthew Ahrens 			}
173134f2f8cfSMatthew Ahrens 		}
173234f2f8cfSMatthew Ahrens 		VERIFY3P(drc->drc_ds->ds_prev, ==,
173334f2f8cfSMatthew Ahrens 		    origin_head->ds_prev);
173434f2f8cfSMatthew Ahrens 
17353b2aab18SMatthew Ahrens 		dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
17363b2aab18SMatthew Ahrens 		    origin_head, tx);
17373b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(origin_head,
17383b2aab18SMatthew Ahrens 		    drc->drc_tosnap, tx);
17393b2aab18SMatthew Ahrens 
17403b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
17413b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
17423b2aab18SMatthew Ahrens 		origin_head->ds_prev->ds_phys->ds_creation_time =
17433b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
17443b2aab18SMatthew Ahrens 		origin_head->ds_prev->ds_phys->ds_guid =
17453b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_toguid;
17463b2aab18SMatthew Ahrens 		origin_head->ds_prev->ds_phys->ds_flags &=
17473b2aab18SMatthew Ahrens 		    ~DS_FLAG_INCONSISTENT;
17483b2aab18SMatthew Ahrens 
17493b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
17503b2aab18SMatthew Ahrens 		origin_head->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
17513b2aab18SMatthew Ahrens 
17523b2aab18SMatthew Ahrens 		dsl_dataset_rele(origin_head, FTAG);
17533b2aab18SMatthew Ahrens 		dsl_destroy_head_sync_impl(drc->drc_ds, tx);
175491948b51SKeith M Wesolowski 
175591948b51SKeith M Wesolowski 		if (drc->drc_owner != NULL)
175691948b51SKeith M Wesolowski 			VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
17573b2aab18SMatthew Ahrens 	} else {
17583b2aab18SMatthew Ahrens 		dsl_dataset_t *ds = drc->drc_ds;
17593cb34c60Sahrens 
17603b2aab18SMatthew Ahrens 		dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
17613cb34c60Sahrens 
17623b2aab18SMatthew Ahrens 		/* set snapshot's creation time and guid */
17633b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
17643b2aab18SMatthew Ahrens 		ds->ds_prev->ds_phys->ds_creation_time =
17653b2aab18SMatthew Ahrens 		    drc->drc_drrb->drr_creation_time;
17663b2aab18SMatthew Ahrens 		ds->ds_prev->ds_phys->ds_guid = drc->drc_drrb->drr_toguid;
17673b2aab18SMatthew Ahrens 		ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
17683cb34c60Sahrens 
17693b2aab18SMatthew Ahrens 		dmu_buf_will_dirty(ds->ds_dbuf, tx);
17703b2aab18SMatthew Ahrens 		ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
17713b2aab18SMatthew Ahrens 	}
17723b2aab18SMatthew Ahrens 	drc->drc_newsnapobj = drc->drc_ds->ds_phys->ds_prev_snap_obj;
17733b2aab18SMatthew Ahrens 	/*
17743b2aab18SMatthew Ahrens 	 * Release the hold from dmu_recv_begin.  This must be done before
17753b2aab18SMatthew Ahrens 	 * we return to open context, so that when we free the dataset's dnode,
17763b2aab18SMatthew Ahrens 	 * we can evict its bonus buffer.
17773b2aab18SMatthew Ahrens 	 */
17783b2aab18SMatthew Ahrens 	dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
17793b2aab18SMatthew Ahrens 	drc->drc_ds = NULL;
17803cb34c60Sahrens }
17813cb34c60Sahrens 
1782ec5cf9d5SAlexander Stetsenko static int
17833b2aab18SMatthew Ahrens add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
1784ec5cf9d5SAlexander Stetsenko {
17853b2aab18SMatthew Ahrens 	dsl_pool_t *dp;
1786ec5cf9d5SAlexander Stetsenko 	dsl_dataset_t *snapds;
1787ec5cf9d5SAlexander Stetsenko 	guid_map_entry_t *gmep;
1788ec5cf9d5SAlexander Stetsenko 	int err;
1789ec5cf9d5SAlexander Stetsenko 
1790ec5cf9d5SAlexander Stetsenko 	ASSERT(guid_map != NULL);
1791ec5cf9d5SAlexander Stetsenko 
17923b2aab18SMatthew Ahrens 	err = dsl_pool_hold(name, FTAG, &dp);
17933b2aab18SMatthew Ahrens 	if (err != 0)
17943b2aab18SMatthew Ahrens 		return (err);
1795de8d9cffSMatthew Ahrens 	gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
1796de8d9cffSMatthew Ahrens 	err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
1797ec5cf9d5SAlexander Stetsenko 	if (err == 0) {
1798ec5cf9d5SAlexander Stetsenko 		gmep->guid = snapds->ds_phys->ds_guid;
1799ec5cf9d5SAlexander Stetsenko 		gmep->gme_ds = snapds;
1800ec5cf9d5SAlexander Stetsenko 		avl_add(guid_map, gmep);
18013b2aab18SMatthew Ahrens 		dsl_dataset_long_hold(snapds, gmep);
1802de8d9cffSMatthew Ahrens 	} else {
1803de8d9cffSMatthew Ahrens 		kmem_free(gmep, sizeof (*gmep));
1804ec5cf9d5SAlexander Stetsenko 	}
1805ec5cf9d5SAlexander Stetsenko 
18063b2aab18SMatthew Ahrens 	dsl_pool_rele(dp, FTAG);
1807ec5cf9d5SAlexander Stetsenko 	return (err);
1808ec5cf9d5SAlexander Stetsenko }
1809ec5cf9d5SAlexander Stetsenko 
18103b2aab18SMatthew Ahrens static int dmu_recv_end_modified_blocks = 3;
18113b2aab18SMatthew Ahrens 
1812ae46e4c7SMatthew Ahrens static int
1813ae46e4c7SMatthew Ahrens dmu_recv_existing_end(dmu_recv_cookie_t *drc)
1814f18faf3fSek {
18153b2aab18SMatthew Ahrens 	int error;
18163b2aab18SMatthew Ahrens 	char name[MAXNAMELEN];
18173cb34c60Sahrens 
18183b2aab18SMatthew Ahrens #ifdef _KERNEL
18193b2aab18SMatthew Ahrens 	/*
18203b2aab18SMatthew Ahrens 	 * We will be destroying the ds; make sure its origin is unmounted if
18213b2aab18SMatthew Ahrens 	 * necessary.
18223b2aab18SMatthew Ahrens 	 */
18233b2aab18SMatthew Ahrens 	dsl_dataset_name(drc->drc_ds, name);
18243b2aab18SMatthew Ahrens 	zfs_destroy_unmount_origin(name);
18253b2aab18SMatthew Ahrens #endif
18263cb34c60Sahrens 
18273b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
18283b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
18293b2aab18SMatthew Ahrens 	    dmu_recv_end_modified_blocks);
18303cb34c60Sahrens 
18313b2aab18SMatthew Ahrens 	if (error != 0)
18323b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
18333b2aab18SMatthew Ahrens 	return (error);
1834f18faf3fSek }
1835ae46e4c7SMatthew Ahrens 
1836ae46e4c7SMatthew Ahrens static int
1837ae46e4c7SMatthew Ahrens dmu_recv_new_end(dmu_recv_cookie_t *drc)
1838ae46e4c7SMatthew Ahrens {
18393b2aab18SMatthew Ahrens 	int error;
1840ae46e4c7SMatthew Ahrens 
18413b2aab18SMatthew Ahrens 	error = dsl_sync_task(drc->drc_tofs,
18423b2aab18SMatthew Ahrens 	    dmu_recv_end_check, dmu_recv_end_sync, drc,
18433b2aab18SMatthew Ahrens 	    dmu_recv_end_modified_blocks);
1844ae46e4c7SMatthew Ahrens 
18453b2aab18SMatthew Ahrens 	if (error != 0) {
18463b2aab18SMatthew Ahrens 		dmu_recv_cleanup_ds(drc);
18473b2aab18SMatthew Ahrens 	} else if (drc->drc_guid_to_ds_map != NULL) {
18483b2aab18SMatthew Ahrens 		(void) add_ds_to_guidmap(drc->drc_tofs,
18493b2aab18SMatthew Ahrens 		    drc->drc_guid_to_ds_map,
18503b2aab18SMatthew Ahrens 		    drc->drc_newsnapobj);
1851ae46e4c7SMatthew Ahrens 	}
18523b2aab18SMatthew Ahrens 	return (error);
1853ae46e4c7SMatthew Ahrens }
1854ae46e4c7SMatthew Ahrens 
1855ae46e4c7SMatthew Ahrens int
185691948b51SKeith M Wesolowski dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
1857ae46e4c7SMatthew Ahrens {
185891948b51SKeith M Wesolowski 	drc->drc_owner = owner;
185991948b51SKeith M Wesolowski 
18603b2aab18SMatthew Ahrens 	if (drc->drc_newfs)
1861ae46e4c7SMatthew Ahrens 		return (dmu_recv_new_end(drc));
18623b2aab18SMatthew Ahrens 	else
18633b2aab18SMatthew Ahrens 		return (dmu_recv_existing_end(drc));
1864ae46e4c7SMatthew Ahrens }
18652f3d8780SMatthew Ahrens 
18662f3d8780SMatthew Ahrens /*
18672f3d8780SMatthew Ahrens  * Return TRUE if this objset is currently being received into.
18682f3d8780SMatthew Ahrens  */
18692f3d8780SMatthew Ahrens boolean_t
18702f3d8780SMatthew Ahrens dmu_objset_is_receiving(objset_t *os)
18712f3d8780SMatthew Ahrens {
18722f3d8780SMatthew Ahrens 	return (os->os_dsl_dataset != NULL &&
18732f3d8780SMatthew Ahrens 	    os->os_dsl_dataset->ds_owner == dmu_recv_tag);
18742f3d8780SMatthew Ahrens }
1875