xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision 10298:a0d52501437c)
12743Sahrens /*
22743Sahrens  * CDDL HEADER START
32743Sahrens  *
42743Sahrens  * The contents of this file are subject to the terms of the
52743Sahrens  * Common Development and Distribution License (the "License").
62743Sahrens  * You may not use this file except in compliance with the License.
72743Sahrens  *
82743Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
92743Sahrens  * or http://www.opensolaris.org/os/licensing.
102743Sahrens  * See the License for the specific language governing permissions
112743Sahrens  * and limitations under the License.
122743Sahrens  *
132743Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
142743Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
152743Sahrens  * If applicable, add the following below this CDDL HEADER, with the
162743Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
172743Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
182743Sahrens  *
192743Sahrens  * CDDL HEADER END
202743Sahrens  */
212743Sahrens /*
228644SMark.Maybee@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
232743Sahrens  * Use is subject to license terms.
242743Sahrens  */
252743Sahrens 
262743Sahrens #include <sys/dmu.h>
272743Sahrens #include <sys/dmu_impl.h>
282743Sahrens #include <sys/dmu_tx.h>
292743Sahrens #include <sys/dbuf.h>
302743Sahrens #include <sys/dnode.h>
312743Sahrens #include <sys/zfs_context.h>
322743Sahrens #include <sys/dmu_objset.h>
332743Sahrens #include <sys/dmu_traverse.h>
342743Sahrens #include <sys/dsl_dataset.h>
352743Sahrens #include <sys/dsl_dir.h>
362743Sahrens #include <sys/dsl_pool.h>
372743Sahrens #include <sys/dsl_synctask.h>
382743Sahrens #include <sys/zfs_ioctl.h>
392743Sahrens #include <sys/zap.h>
402743Sahrens #include <sys/zio_checksum.h>
412743Sahrens 
425367Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
435367Sahrens 
442743Sahrens struct backuparg {
452743Sahrens 	dmu_replay_record_t *drr;
462743Sahrens 	vnode_t *vp;
475367Sahrens 	offset_t *off;
482743Sahrens 	objset_t *os;
492743Sahrens 	zio_cksum_t zc;
502743Sahrens 	int err;
512743Sahrens };
522743Sahrens 
532743Sahrens static int
542743Sahrens dump_bytes(struct backuparg *ba, void *buf, int len)
552743Sahrens {
562743Sahrens 	ssize_t resid; /* have to get resid to get detailed errno */
572743Sahrens 	ASSERT3U(len % 8, ==, 0);
582743Sahrens 
592743Sahrens 	fletcher_4_incremental_native(buf, len, &ba->zc);
602743Sahrens 	ba->err = vn_rdwr(UIO_WRITE, ba->vp,
612743Sahrens 	    (caddr_t)buf, len,
622743Sahrens 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
635367Sahrens 	*ba->off += len;
642743Sahrens 	return (ba->err);
652743Sahrens }
662743Sahrens 
672743Sahrens static int
682743Sahrens dump_free(struct backuparg *ba, uint64_t object, uint64_t offset,
692743Sahrens     uint64_t length)
702743Sahrens {
712743Sahrens 	/* write a FREE record */
722743Sahrens 	bzero(ba->drr, sizeof (dmu_replay_record_t));
732743Sahrens 	ba->drr->drr_type = DRR_FREE;
742743Sahrens 	ba->drr->drr_u.drr_free.drr_object = object;
752743Sahrens 	ba->drr->drr_u.drr_free.drr_offset = offset;
762743Sahrens 	ba->drr->drr_u.drr_free.drr_length = length;
772743Sahrens 
782743Sahrens 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
792743Sahrens 		return (EINTR);
802743Sahrens 	return (0);
812743Sahrens }
822743Sahrens 
832743Sahrens static int
842743Sahrens dump_data(struct backuparg *ba, dmu_object_type_t type,
852743Sahrens     uint64_t object, uint64_t offset, int blksz, void *data)
862743Sahrens {
872743Sahrens 	/* write a DATA record */
882743Sahrens 	bzero(ba->drr, sizeof (dmu_replay_record_t));
892743Sahrens 	ba->drr->drr_type = DRR_WRITE;
902743Sahrens 	ba->drr->drr_u.drr_write.drr_object = object;
912743Sahrens 	ba->drr->drr_u.drr_write.drr_type = type;
922743Sahrens 	ba->drr->drr_u.drr_write.drr_offset = offset;
932743Sahrens 	ba->drr->drr_u.drr_write.drr_length = blksz;
942743Sahrens 
952743Sahrens 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
962743Sahrens 		return (EINTR);
972743Sahrens 	if (dump_bytes(ba, data, blksz))
982743Sahrens 		return (EINTR);
992743Sahrens 	return (0);
1002743Sahrens }
1012743Sahrens 
1022743Sahrens static int
1032743Sahrens dump_freeobjects(struct backuparg *ba, uint64_t firstobj, uint64_t numobjs)
1042743Sahrens {
1052743Sahrens 	/* write a FREEOBJECTS record */
1062743Sahrens 	bzero(ba->drr, sizeof (dmu_replay_record_t));
1072743Sahrens 	ba->drr->drr_type = DRR_FREEOBJECTS;
1082743Sahrens 	ba->drr->drr_u.drr_freeobjects.drr_firstobj = firstobj;
1092743Sahrens 	ba->drr->drr_u.drr_freeobjects.drr_numobjs = numobjs;
1102743Sahrens 
1112743Sahrens 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
1122743Sahrens 		return (EINTR);
1132743Sahrens 	return (0);
1142743Sahrens }
1152743Sahrens 
1162743Sahrens static int
1172743Sahrens dump_dnode(struct backuparg *ba, uint64_t object, dnode_phys_t *dnp)
1182743Sahrens {
1192743Sahrens 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
1202743Sahrens 		return (dump_freeobjects(ba, object, 1));
1212743Sahrens 
1222743Sahrens 	/* write an OBJECT record */
1232743Sahrens 	bzero(ba->drr, sizeof (dmu_replay_record_t));
1242743Sahrens 	ba->drr->drr_type = DRR_OBJECT;
1252743Sahrens 	ba->drr->drr_u.drr_object.drr_object = object;
1262743Sahrens 	ba->drr->drr_u.drr_object.drr_type = dnp->dn_type;
1272743Sahrens 	ba->drr->drr_u.drr_object.drr_bonustype = dnp->dn_bonustype;
1282743Sahrens 	ba->drr->drr_u.drr_object.drr_blksz =
1292743Sahrens 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
1302743Sahrens 	ba->drr->drr_u.drr_object.drr_bonuslen = dnp->dn_bonuslen;
1312743Sahrens 	ba->drr->drr_u.drr_object.drr_checksum = dnp->dn_checksum;
1322743Sahrens 	ba->drr->drr_u.drr_object.drr_compress = dnp->dn_compress;
1332743Sahrens 
1342743Sahrens 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
1352743Sahrens 		return (EINTR);
1362743Sahrens 
1372743Sahrens 	if (dump_bytes(ba, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)))
1382743Sahrens 		return (EINTR);
1392743Sahrens 
1402743Sahrens 	/* free anything past the end of the file */
1412743Sahrens 	if (dump_free(ba, object, (dnp->dn_maxblkid + 1) *
1422743Sahrens 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL))
1432743Sahrens 		return (EINTR);
1442743Sahrens 	if (ba->err)
1452743Sahrens 		return (EINTR);
1462743Sahrens 	return (0);
1472743Sahrens }
1482743Sahrens 
1492743Sahrens #define	BP_SPAN(dnp, level) \
1502743Sahrens 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
1512743Sahrens 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
1522743Sahrens 
1532743Sahrens static int
1547837SMatthew.Ahrens@Sun.COM backup_cb(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
1557837SMatthew.Ahrens@Sun.COM     const dnode_phys_t *dnp, void *arg)
1562743Sahrens {
1572743Sahrens 	struct backuparg *ba = arg;
1582743Sahrens 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
1592743Sahrens 	int err = 0;
1602743Sahrens 
1612743Sahrens 	if (issig(JUSTLOOKING) && issig(FORREAL))
1622743Sahrens 		return (EINTR);
1632743Sahrens 
1649396SMatthew.Ahrens@Sun.COM 	if (zb->zb_object != 0 && DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
1659396SMatthew.Ahrens@Sun.COM 		return (0);
1669396SMatthew.Ahrens@Sun.COM 	} else if (bp == NULL && zb->zb_object == 0) {
1677837SMatthew.Ahrens@Sun.COM 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
1687837SMatthew.Ahrens@Sun.COM 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
1692743Sahrens 		err = dump_freeobjects(ba, dnobj, span >> DNODE_SHIFT);
1702743Sahrens 	} else if (bp == NULL) {
1717837SMatthew.Ahrens@Sun.COM 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
1727837SMatthew.Ahrens@Sun.COM 		err = dump_free(ba, zb->zb_object, zb->zb_blkid * span, span);
1737837SMatthew.Ahrens@Sun.COM 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
1747837SMatthew.Ahrens@Sun.COM 		return (0);
1757837SMatthew.Ahrens@Sun.COM 	} else if (type == DMU_OT_DNODE) {
1767837SMatthew.Ahrens@Sun.COM 		dnode_phys_t *blk;
1772743Sahrens 		int i;
1782743Sahrens 		int blksz = BP_GET_LSIZE(bp);
1797837SMatthew.Ahrens@Sun.COM 		uint32_t aflags = ARC_WAIT;
1807837SMatthew.Ahrens@Sun.COM 		arc_buf_t *abuf;
1812743Sahrens 
1827837SMatthew.Ahrens@Sun.COM 		if (arc_read_nolock(NULL, spa, bp,
1837837SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
1847837SMatthew.Ahrens@Sun.COM 		    ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
1857837SMatthew.Ahrens@Sun.COM 			return (EIO);
1867837SMatthew.Ahrens@Sun.COM 
1877837SMatthew.Ahrens@Sun.COM 		blk = abuf->b_data;
1882743Sahrens 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
1897837SMatthew.Ahrens@Sun.COM 			uint64_t dnobj = (zb->zb_blkid <<
1907837SMatthew.Ahrens@Sun.COM 			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
1912743Sahrens 			err = dump_dnode(ba, dnobj, blk+i);
1922743Sahrens 			if (err)
1932743Sahrens 				break;
1942743Sahrens 		}
1957837SMatthew.Ahrens@Sun.COM 		(void) arc_buf_remove_ref(abuf, &abuf);
1967837SMatthew.Ahrens@Sun.COM 	} else { /* it's a level-0 block of a regular object */
1977837SMatthew.Ahrens@Sun.COM 		uint32_t aflags = ARC_WAIT;
1987837SMatthew.Ahrens@Sun.COM 		arc_buf_t *abuf;
1992743Sahrens 		int blksz = BP_GET_LSIZE(bp);
2002743Sahrens 
2017837SMatthew.Ahrens@Sun.COM 		if (arc_read_nolock(NULL, spa, bp,
2027837SMatthew.Ahrens@Sun.COM 		    arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
2037837SMatthew.Ahrens@Sun.COM 		    ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
2047837SMatthew.Ahrens@Sun.COM 			return (EIO);
2052743Sahrens 
2067837SMatthew.Ahrens@Sun.COM 		err = dump_data(ba, type, zb->zb_object, zb->zb_blkid * blksz,
2077837SMatthew.Ahrens@Sun.COM 		    blksz, abuf->b_data);
2087837SMatthew.Ahrens@Sun.COM 		(void) arc_buf_remove_ref(abuf, &abuf);
2092743Sahrens 	}
2102743Sahrens 
2112743Sahrens 	ASSERT(err == 0 || err == EINTR);
2122743Sahrens 	return (err);
2132743Sahrens }
2142743Sahrens 
2152743Sahrens int
2165367Sahrens dmu_sendbackup(objset_t *tosnap, objset_t *fromsnap, boolean_t fromorigin,
2175367Sahrens     vnode_t *vp, offset_t *off)
2182743Sahrens {
219*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = tosnap->os_dsl_dataset;
220*10298SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *fromds = fromsnap ? fromsnap->os_dsl_dataset : NULL;
2212743Sahrens 	dmu_replay_record_t *drr;
2222743Sahrens 	struct backuparg ba;
2232743Sahrens 	int err;
2245367Sahrens 	uint64_t fromtxg = 0;
2252743Sahrens 
2262743Sahrens 	/* tosnap must be a snapshot */
2272743Sahrens 	if (ds->ds_phys->ds_next_snap_obj == 0)
2282743Sahrens 		return (EINVAL);
2292743Sahrens 
2302743Sahrens 	/* fromsnap must be an earlier snapshot from the same fs as tosnap */
2312743Sahrens 	if (fromds && (ds->ds_dir != fromds->ds_dir ||
2325367Sahrens 	    fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg))
2332743Sahrens 		return (EXDEV);
2342743Sahrens 
2355367Sahrens 	if (fromorigin) {
2367046Sahrens 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
2377046Sahrens 
2385367Sahrens 		if (fromsnap)
2395367Sahrens 			return (EINVAL);
2405367Sahrens 
2417046Sahrens 		if (dsl_dir_is_clone(ds->ds_dir)) {
2425367Sahrens 			rw_enter(&dp->dp_config_rwlock, RW_READER);
2436689Smaybee 			err = dsl_dataset_hold_obj(dp,
2446689Smaybee 			    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &fromds);
2455367Sahrens 			rw_exit(&dp->dp_config_rwlock);
2465367Sahrens 			if (err)
2475367Sahrens 				return (err);
2485367Sahrens 		} else {
2495367Sahrens 			fromorigin = B_FALSE;
2505367Sahrens 		}
2515367Sahrens 	}
2525367Sahrens 
2535367Sahrens 
2542743Sahrens 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
2552743Sahrens 	drr->drr_type = DRR_BEGIN;
2562743Sahrens 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
2575367Sahrens 	drr->drr_u.drr_begin.drr_version = DMU_BACKUP_STREAM_VERSION;
2582743Sahrens 	drr->drr_u.drr_begin.drr_creation_time =
2592743Sahrens 	    ds->ds_phys->ds_creation_time;
260*10298SMatthew.Ahrens@Sun.COM 	drr->drr_u.drr_begin.drr_type = tosnap->os_phys->os_type;
2615367Sahrens 	if (fromorigin)
2625367Sahrens 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
2632743Sahrens 	drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
2646492Stimh 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
2656492Stimh 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
2666492Stimh 
2672743Sahrens 	if (fromds)
2682743Sahrens 		drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid;
2692743Sahrens 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
2702743Sahrens 
2715367Sahrens 	if (fromds)
2725367Sahrens 		fromtxg = fromds->ds_phys->ds_creation_txg;
2735367Sahrens 	if (fromorigin)
2746689Smaybee 		dsl_dataset_rele(fromds, FTAG);
2755367Sahrens 
2762743Sahrens 	ba.drr = drr;
2772743Sahrens 	ba.vp = vp;
2782743Sahrens 	ba.os = tosnap;
2795367Sahrens 	ba.off = off;
2802743Sahrens 	ZIO_SET_CHECKSUM(&ba.zc, 0, 0, 0, 0);
2812743Sahrens 
2822743Sahrens 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
2832743Sahrens 		kmem_free(drr, sizeof (dmu_replay_record_t));
2842743Sahrens 		return (ba.err);
2852743Sahrens 	}
2862743Sahrens 
2877837SMatthew.Ahrens@Sun.COM 	err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH,
2882743Sahrens 	    backup_cb, &ba);
2892743Sahrens 
2902743Sahrens 	if (err) {
2912743Sahrens 		if (err == EINTR && ba.err)
2922743Sahrens 			err = ba.err;
2933655Sgw25295 		kmem_free(drr, sizeof (dmu_replay_record_t));
2942743Sahrens 		return (err);
2952743Sahrens 	}
2962743Sahrens 
2972743Sahrens 	bzero(drr, sizeof (dmu_replay_record_t));
2982743Sahrens 	drr->drr_type = DRR_END;
2992743Sahrens 	drr->drr_u.drr_end.drr_checksum = ba.zc;
3002743Sahrens 
3013655Sgw25295 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
3023655Sgw25295 		kmem_free(drr, sizeof (dmu_replay_record_t));
3032743Sahrens 		return (ba.err);
3043655Sgw25295 	}
3052743Sahrens 
3062743Sahrens 	kmem_free(drr, sizeof (dmu_replay_record_t));
3072743Sahrens 
3082743Sahrens 	return (0);
3092743Sahrens }
3102743Sahrens 
3115367Sahrens struct recvbeginsyncarg {
3125367Sahrens 	const char *tofs;
3135367Sahrens 	const char *tosnap;
3145367Sahrens 	dsl_dataset_t *origin;
3155367Sahrens 	uint64_t fromguid;
3165367Sahrens 	dmu_objset_type_t type;
3175367Sahrens 	void *tag;
3185367Sahrens 	boolean_t force;
3196492Stimh 	uint64_t dsflags;
3205367Sahrens 	char clonelastname[MAXNAMELEN];
3215367Sahrens 	dsl_dataset_t *ds; /* the ds to recv into; returned from the syncfunc */
3222743Sahrens };
3232743Sahrens 
3245367Sahrens /* ARGSUSED */
3252743Sahrens static int
32610272SMatthew.Ahrens@Sun.COM recv_new_check(void *arg1, void *arg2, dmu_tx_t *tx)
3275367Sahrens {
3285367Sahrens 	dsl_dir_t *dd = arg1;
3295367Sahrens 	struct recvbeginsyncarg *rbsa = arg2;
3305367Sahrens 	objset_t *mos = dd->dd_pool->dp_meta_objset;
3315367Sahrens 	uint64_t val;
3325367Sahrens 	int err;
3335367Sahrens 
3345367Sahrens 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
3355367Sahrens 	    strrchr(rbsa->tofs, '/') + 1, sizeof (uint64_t), 1, &val);
3365367Sahrens 
3375367Sahrens 	if (err != ENOENT)
3385367Sahrens 		return (err ? err : EEXIST);
3395367Sahrens 
3405367Sahrens 	if (rbsa->origin) {
3415367Sahrens 		/* make sure it's a snap in the same pool */
3425367Sahrens 		if (rbsa->origin->ds_dir->dd_pool != dd->dd_pool)
3435367Sahrens 			return (EXDEV);
34410272SMatthew.Ahrens@Sun.COM 		if (!dsl_dataset_is_snapshot(rbsa->origin))
3455367Sahrens 			return (EINVAL);
3465367Sahrens 		if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid)
3475367Sahrens 			return (ENODEV);
3485367Sahrens 	}
3495367Sahrens 
3505367Sahrens 	return (0);
3515367Sahrens }
3525367Sahrens 
3535367Sahrens static void
35410272SMatthew.Ahrens@Sun.COM recv_new_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
3555367Sahrens {
3565367Sahrens 	dsl_dir_t *dd = arg1;
3575367Sahrens 	struct recvbeginsyncarg *rbsa = arg2;
3586689Smaybee 	uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
3595367Sahrens 	uint64_t dsobj;
3605367Sahrens 
36110272SMatthew.Ahrens@Sun.COM 	/* Create and open new dataset. */
3625367Sahrens 	dsobj = dsl_dataset_create_sync(dd, strrchr(rbsa->tofs, '/') + 1,
3636492Stimh 	    rbsa->origin, flags, cr, tx);
36410272SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dsl_dataset_own_obj(dd->dd_pool, dsobj,
365*10298SMatthew.Ahrens@Sun.COM 	    B_TRUE, dmu_recv_tag, &rbsa->ds));
3665367Sahrens 
36710272SMatthew.Ahrens@Sun.COM 	if (rbsa->origin == NULL) {
36810272SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_create_impl(dd->dd_pool->dp_spa,
36910272SMatthew.Ahrens@Sun.COM 		    rbsa->ds, &rbsa->ds->ds_phys->ds_bp, rbsa->type, tx);
3705367Sahrens 	}
3715367Sahrens 
37210272SMatthew.Ahrens@Sun.COM 	spa_history_internal_log(LOG_DS_REPLAY_FULL_SYNC,
37310272SMatthew.Ahrens@Sun.COM 	    dd->dd_pool->dp_spa, tx, cr, "dataset = %lld", dsobj);
3745367Sahrens }
3755367Sahrens 
3765367Sahrens /* ARGSUSED */
3775367Sahrens static int
37810272SMatthew.Ahrens@Sun.COM recv_existing_check(void *arg1, void *arg2, dmu_tx_t *tx)
3795367Sahrens {
3805367Sahrens 	dsl_dataset_t *ds = arg1;
3815367Sahrens 	struct recvbeginsyncarg *rbsa = arg2;
3822743Sahrens 	int err;
3832743Sahrens 	uint64_t val;
3842743Sahrens 
3855367Sahrens 	/* must not have any changes since most recent snapshot */
3865367Sahrens 	if (!rbsa->force && dsl_dataset_modified_since_lastsnap(ds))
3875367Sahrens 		return (ETXTBSY);
3885367Sahrens 
38910272SMatthew.Ahrens@Sun.COM 	if (rbsa->fromguid) {
39010272SMatthew.Ahrens@Sun.COM 		/* if incremental, most recent snapshot must match fromguid */
39110272SMatthew.Ahrens@Sun.COM 		if (ds->ds_prev == NULL)
39210272SMatthew.Ahrens@Sun.COM 			return (ENODEV);
39310272SMatthew.Ahrens@Sun.COM 		if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid)
39410272SMatthew.Ahrens@Sun.COM 			return (ENODEV);
39510272SMatthew.Ahrens@Sun.COM 	} else {
39610272SMatthew.Ahrens@Sun.COM 		/* if full, most recent snapshot must be $ORIGIN */
39710272SMatthew.Ahrens@Sun.COM 		if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL)
39810272SMatthew.Ahrens@Sun.COM 			return (ENODEV);
39910272SMatthew.Ahrens@Sun.COM 	}
4002743Sahrens 
4016083Sek110237 	/* temporary clone name must not exist */
4026083Sek110237 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
4036083Sek110237 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj,
4046083Sek110237 	    rbsa->clonelastname, 8, 1, &val);
4056083Sek110237 	if (err == 0)
4066083Sek110237 		return (EEXIST);
4076083Sek110237 	if (err != ENOENT)
4086083Sek110237 		return (err);
4096083Sek110237 
4102743Sahrens 	/* new snapshot name must not exist */
4115367Sahrens 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
4125367Sahrens 	    ds->ds_phys->ds_snapnames_zapobj, rbsa->tosnap, 8, 1, &val);
4135367Sahrens 	if (err == 0)
4142743Sahrens 		return (EEXIST);
4152743Sahrens 	if (err != ENOENT)
4165367Sahrens 		return (err);
4172743Sahrens 	return (0);
4182743Sahrens }
4192743Sahrens 
4202743Sahrens /* ARGSUSED */
4215367Sahrens static void
42210272SMatthew.Ahrens@Sun.COM recv_existing_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
4235326Sek110237 {
4245367Sahrens 	dsl_dataset_t *ohds = arg1;
4255367Sahrens 	struct recvbeginsyncarg *rbsa = arg2;
4265367Sahrens 	dsl_pool_t *dp = ohds->ds_dir->dd_pool;
42710272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *cds;
4286689Smaybee 	uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
4295367Sahrens 	uint64_t dsobj;
4305326Sek110237 
43110272SMatthew.Ahrens@Sun.COM 	/* create and open the temporary clone */
43210272SMatthew.Ahrens@Sun.COM 	dsobj = dsl_dataset_create_sync(ohds->ds_dir, rbsa->clonelastname,
43310272SMatthew.Ahrens@Sun.COM 	    ohds->ds_prev, flags, cr, tx);
434*10298SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dsl_dataset_own_obj(dp, dsobj, B_TRUE, dmu_recv_tag, &cds));
4355367Sahrens 
43610272SMatthew.Ahrens@Sun.COM 	/*
43710272SMatthew.Ahrens@Sun.COM 	 * If we actually created a non-clone, we need to create the
43810272SMatthew.Ahrens@Sun.COM 	 * objset in our new dataset.
43910272SMatthew.Ahrens@Sun.COM 	 */
44010272SMatthew.Ahrens@Sun.COM 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(cds))) {
44110272SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_create_impl(dp->dp_spa,
44210272SMatthew.Ahrens@Sun.COM 		    cds, dsl_dataset_get_blkptr(cds), rbsa->type, tx);
44310272SMatthew.Ahrens@Sun.COM 	}
44410272SMatthew.Ahrens@Sun.COM 
4455378Sck153898 	/* copy the refquota from the target fs to the clone */
4465378Sck153898 	if (ohds->ds_quota > 0)
4475378Sck153898 		dsl_dataset_set_quota_sync(cds, &ohds->ds_quota, cr, tx);
4485378Sck153898 
4495367Sahrens 	rbsa->ds = cds;
4505367Sahrens 
4515367Sahrens 	spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC,
4526689Smaybee 	    dp->dp_spa, tx, cr, "dataset = %lld", dsobj);
4535326Sek110237 }
4545326Sek110237 
4555367Sahrens /*
4565367Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
4575367Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
4585367Sahrens  */
4595367Sahrens int
4605367Sahrens dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
46110204SMatthew.Ahrens@Sun.COM     boolean_t force, objset_t *origin, dmu_recv_cookie_t *drc)
4622743Sahrens {
4635367Sahrens 	int err = 0;
4645367Sahrens 	boolean_t byteswap;
46510272SMatthew.Ahrens@Sun.COM 	struct recvbeginsyncarg rbsa = { 0 };
4665367Sahrens 	uint64_t version;
4675367Sahrens 	int flags;
4685367Sahrens 	dsl_dataset_t *ds;
4695367Sahrens 
4705367Sahrens 	if (drrb->drr_magic == DMU_BACKUP_MAGIC)
4715367Sahrens 		byteswap = FALSE;
4725367Sahrens 	else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
4735367Sahrens 		byteswap = TRUE;
4745367Sahrens 	else
4755367Sahrens 		return (EINVAL);
4765367Sahrens 
4775367Sahrens 	rbsa.tofs = tofs;
4785367Sahrens 	rbsa.tosnap = tosnap;
479*10298SMatthew.Ahrens@Sun.COM 	rbsa.origin = origin ? origin->os_dsl_dataset : NULL;
4805367Sahrens 	rbsa.fromguid = drrb->drr_fromguid;
4815367Sahrens 	rbsa.type = drrb->drr_type;
4825367Sahrens 	rbsa.tag = FTAG;
4836492Stimh 	rbsa.dsflags = 0;
4845367Sahrens 	version = drrb->drr_version;
4855367Sahrens 	flags = drrb->drr_flags;
4865367Sahrens 
4875367Sahrens 	if (byteswap) {
4885367Sahrens 		rbsa.type = BSWAP_32(rbsa.type);
4895367Sahrens 		rbsa.fromguid = BSWAP_64(rbsa.fromguid);
4905367Sahrens 		version = BSWAP_64(version);
4915367Sahrens 		flags = BSWAP_32(flags);
4925367Sahrens 	}
4935367Sahrens 
4945367Sahrens 	if (version != DMU_BACKUP_STREAM_VERSION ||
4955367Sahrens 	    rbsa.type >= DMU_OST_NUMTYPES ||
4965367Sahrens 	    ((flags & DRR_FLAG_CLONE) && origin == NULL))
4975367Sahrens 		return (EINVAL);
4985367Sahrens 
4996492Stimh 	if (flags & DRR_FLAG_CI_DATA)
5006492Stimh 		rbsa.dsflags = DS_FLAG_CI_DATASET;
5016492Stimh 
5025367Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
5035367Sahrens 	drc->drc_drrb = drrb;
5045367Sahrens 	drc->drc_tosnap = tosnap;
5055367Sahrens 	drc->drc_force = force;
5065367Sahrens 
5075367Sahrens 	/*
5085367Sahrens 	 * Process the begin in syncing context.
5095367Sahrens 	 */
51010272SMatthew.Ahrens@Sun.COM 
51110272SMatthew.Ahrens@Sun.COM 	/* open the dataset we are logically receiving into */
51210272SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(tofs, dmu_recv_tag, &ds);
51310272SMatthew.Ahrens@Sun.COM 	if (err == 0) {
51410272SMatthew.Ahrens@Sun.COM 		/* target fs already exists; recv into temp clone */
5155367Sahrens 
51610272SMatthew.Ahrens@Sun.COM 		/* Can't recv a clone into an existing fs */
51710272SMatthew.Ahrens@Sun.COM 		if (flags & DRR_FLAG_CLONE) {
51810272SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(ds, dmu_recv_tag);
51910272SMatthew.Ahrens@Sun.COM 			return (EINVAL);
52010272SMatthew.Ahrens@Sun.COM 		}
5212743Sahrens 
52210204SMatthew.Ahrens@Sun.COM 		/* must not have an incremental recv already in progress */
52310204SMatthew.Ahrens@Sun.COM 		if (!mutex_tryenter(&ds->ds_recvlock)) {
52410204SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(ds, dmu_recv_tag);
52510204SMatthew.Ahrens@Sun.COM 			return (EBUSY);
52610204SMatthew.Ahrens@Sun.COM 		}
52710204SMatthew.Ahrens@Sun.COM 
52810272SMatthew.Ahrens@Sun.COM 		/* tmp clone name is: tofs/%tosnap" */
52910272SMatthew.Ahrens@Sun.COM 		(void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname),
53010272SMatthew.Ahrens@Sun.COM 		    "%%%s", tosnap);
5315367Sahrens 		rbsa.force = force;
5325367Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
53310272SMatthew.Ahrens@Sun.COM 		    recv_existing_check, recv_existing_sync, ds, &rbsa, 5);
5345367Sahrens 		if (err) {
53510204SMatthew.Ahrens@Sun.COM 			mutex_exit(&ds->ds_recvlock);
5366689Smaybee 			dsl_dataset_rele(ds, dmu_recv_tag);
5375367Sahrens 			return (err);
5385367Sahrens 		}
5395367Sahrens 		drc->drc_logical_ds = ds;
5405367Sahrens 		drc->drc_real_ds = rbsa.ds;
54110272SMatthew.Ahrens@Sun.COM 	} else if (err == ENOENT) {
54210272SMatthew.Ahrens@Sun.COM 		/* target fs does not exist; must be a full backup or clone */
54310272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *parent;
54410272SMatthew.Ahrens@Sun.COM 		char *cp;
5455367Sahrens 
54610272SMatthew.Ahrens@Sun.COM 		/*
54710272SMatthew.Ahrens@Sun.COM 		 * If it's a non-clone incremental, we are missing the
54810272SMatthew.Ahrens@Sun.COM 		 * target fs, so fail the recv.
54910272SMatthew.Ahrens@Sun.COM 		 */
55010272SMatthew.Ahrens@Sun.COM 		if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE))
55110272SMatthew.Ahrens@Sun.COM 			return (ENOENT);
55210272SMatthew.Ahrens@Sun.COM 
55310272SMatthew.Ahrens@Sun.COM 		/* Open the parent of tofs */
55410272SMatthew.Ahrens@Sun.COM 		cp = strrchr(tofs, '/');
55510272SMatthew.Ahrens@Sun.COM 		*cp = '\0';
55610272SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_hold(tofs, FTAG, &parent);
55710272SMatthew.Ahrens@Sun.COM 		*cp = '/';
5585367Sahrens 		if (err)
5595367Sahrens 			return (err);
5605367Sahrens 
56110272SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
56210272SMatthew.Ahrens@Sun.COM 		    recv_new_check, recv_new_sync, ds->ds_dir, &rbsa, 5);
56310272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(parent, FTAG);
5645367Sahrens 		if (err)
5655367Sahrens 			return (err);
5665367Sahrens 		drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds;
5675367Sahrens 		drc->drc_newfs = B_TRUE;
5685367Sahrens 	}
5695367Sahrens 
57010272SMatthew.Ahrens@Sun.COM 	return (err);
5712743Sahrens }
5722743Sahrens 
5735367Sahrens struct restorearg {
5745367Sahrens 	int err;
5755367Sahrens 	int byteswap;
5765367Sahrens 	vnode_t *vp;
5775367Sahrens 	char *buf;
5785367Sahrens 	uint64_t voff;
5795367Sahrens 	int bufsize; /* amount of memory allocated for buf */
5805367Sahrens 	zio_cksum_t cksum;
5815326Sek110237 };
5825326Sek110237 
5832743Sahrens static void *
5842743Sahrens restore_read(struct restorearg *ra, int len)
5852743Sahrens {
5862743Sahrens 	void *rv;
5875367Sahrens 	int done = 0;
5882743Sahrens 
5892743Sahrens 	/* some things will require 8-byte alignment, so everything must */
5902743Sahrens 	ASSERT3U(len % 8, ==, 0);
5912743Sahrens 
5925367Sahrens 	while (done < len) {
5932743Sahrens 		ssize_t resid;
5942743Sahrens 
5952743Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
5965367Sahrens 		    (caddr_t)ra->buf + done, len - done,
5972743Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
5982743Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
5992743Sahrens 
6005367Sahrens 		if (resid == len - done)
6012743Sahrens 			ra->err = EINVAL;
6025367Sahrens 		ra->voff += len - done - resid;
6035367Sahrens 		done = len - resid;
6042743Sahrens 		if (ra->err)
6052743Sahrens 			return (NULL);
6062743Sahrens 	}
6072743Sahrens 
6085367Sahrens 	ASSERT3U(done, ==, len);
6095367Sahrens 	rv = ra->buf;
6102743Sahrens 	if (ra->byteswap)
6115367Sahrens 		fletcher_4_incremental_byteswap(rv, len, &ra->cksum);
6122743Sahrens 	else
6135367Sahrens 		fletcher_4_incremental_native(rv, len, &ra->cksum);
6142743Sahrens 	return (rv);
6152743Sahrens }
6162743Sahrens 
6172743Sahrens static void
6182743Sahrens backup_byteswap(dmu_replay_record_t *drr)
6192743Sahrens {
6202743Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
6212743Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
6222743Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
6235367Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
6242743Sahrens 	switch (drr->drr_type) {
6252743Sahrens 	case DRR_BEGIN:
6262743Sahrens 		DO64(drr_begin.drr_magic);
6272743Sahrens 		DO64(drr_begin.drr_version);
6282743Sahrens 		DO64(drr_begin.drr_creation_time);
6292743Sahrens 		DO32(drr_begin.drr_type);
6305367Sahrens 		DO32(drr_begin.drr_flags);
6312743Sahrens 		DO64(drr_begin.drr_toguid);
6322743Sahrens 		DO64(drr_begin.drr_fromguid);
6332743Sahrens 		break;
6342743Sahrens 	case DRR_OBJECT:
6352743Sahrens 		DO64(drr_object.drr_object);
6362743Sahrens 		/* DO64(drr_object.drr_allocation_txg); */
6372743Sahrens 		DO32(drr_object.drr_type);
6382743Sahrens 		DO32(drr_object.drr_bonustype);
6392743Sahrens 		DO32(drr_object.drr_blksz);
6402743Sahrens 		DO32(drr_object.drr_bonuslen);
6412743Sahrens 		break;
6422743Sahrens 	case DRR_FREEOBJECTS:
6432743Sahrens 		DO64(drr_freeobjects.drr_firstobj);
6442743Sahrens 		DO64(drr_freeobjects.drr_numobjs);
6452743Sahrens 		break;
6462743Sahrens 	case DRR_WRITE:
6472743Sahrens 		DO64(drr_write.drr_object);
6482743Sahrens 		DO32(drr_write.drr_type);
6492743Sahrens 		DO64(drr_write.drr_offset);
6502743Sahrens 		DO64(drr_write.drr_length);
6512743Sahrens 		break;
6522743Sahrens 	case DRR_FREE:
6532743Sahrens 		DO64(drr_free.drr_object);
6542743Sahrens 		DO64(drr_free.drr_offset);
6552743Sahrens 		DO64(drr_free.drr_length);
6562743Sahrens 		break;
6572743Sahrens 	case DRR_END:
6582743Sahrens 		DO64(drr_end.drr_checksum.zc_word[0]);
6592743Sahrens 		DO64(drr_end.drr_checksum.zc_word[1]);
6602743Sahrens 		DO64(drr_end.drr_checksum.zc_word[2]);
6612743Sahrens 		DO64(drr_end.drr_checksum.zc_word[3]);
6622743Sahrens 		break;
6632743Sahrens 	}
6642743Sahrens #undef DO64
6652743Sahrens #undef DO32
6662743Sahrens }
6672743Sahrens 
6682743Sahrens static int
6692743Sahrens restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
6702743Sahrens {
6712743Sahrens 	int err;
6722743Sahrens 	dmu_tx_t *tx;
6737994STim.Haley@Sun.COM 	void *data = NULL;
6742743Sahrens 
6752743Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
6762743Sahrens 	    drro->drr_type >= DMU_OT_NUMTYPES ||
6772743Sahrens 	    drro->drr_bonustype >= DMU_OT_NUMTYPES ||
6782743Sahrens 	    drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS ||
6792743Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
6802743Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
6812743Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
6822743Sahrens 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
6832743Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
6842743Sahrens 		return (EINVAL);
6852743Sahrens 	}
6862743Sahrens 
6878986SMark.Maybee@Sun.COM 	err = dmu_object_info(os, drro->drr_object, NULL);
6888986SMark.Maybee@Sun.COM 
6898986SMark.Maybee@Sun.COM 	if (err != 0 && err != ENOENT)
6908986SMark.Maybee@Sun.COM 		return (EINVAL);
6918986SMark.Maybee@Sun.COM 
6927994STim.Haley@Sun.COM 	if (drro->drr_bonuslen) {
6937994STim.Haley@Sun.COM 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
6947994STim.Haley@Sun.COM 		if (ra->err)
6957994STim.Haley@Sun.COM 			return (ra->err);
6967994STim.Haley@Sun.COM 	}
6977994STim.Haley@Sun.COM 
6982743Sahrens 	if (err == ENOENT) {
6992743Sahrens 		/* currently free, want to be allocated */
7008986SMark.Maybee@Sun.COM 		tx = dmu_tx_create(os);
7012743Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
7022743Sahrens 		err = dmu_tx_assign(tx, TXG_WAIT);
7032743Sahrens 		if (err) {
7042743Sahrens 			dmu_tx_abort(tx);
7052743Sahrens 			return (err);
7062743Sahrens 		}
7072743Sahrens 		err = dmu_object_claim(os, drro->drr_object,
7082743Sahrens 		    drro->drr_type, drro->drr_blksz,
7092743Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
7108986SMark.Maybee@Sun.COM 		dmu_tx_commit(tx);
7112743Sahrens 	} else {
7122743Sahrens 		/* currently allocated, want to be allocated */
7132743Sahrens 		err = dmu_object_reclaim(os, drro->drr_object,
7142743Sahrens 		    drro->drr_type, drro->drr_blksz,
7158986SMark.Maybee@Sun.COM 		    drro->drr_bonustype, drro->drr_bonuslen);
7162743Sahrens 	}
7178986SMark.Maybee@Sun.COM 	if (err)
7188986SMark.Maybee@Sun.COM 		return (EINVAL);
7198986SMark.Maybee@Sun.COM 
7208986SMark.Maybee@Sun.COM 	tx = dmu_tx_create(os);
7218986SMark.Maybee@Sun.COM 	dmu_tx_hold_bonus(tx, drro->drr_object);
7228986SMark.Maybee@Sun.COM 	err = dmu_tx_assign(tx, TXG_WAIT);
7232743Sahrens 	if (err) {
7248986SMark.Maybee@Sun.COM 		dmu_tx_abort(tx);
7258986SMark.Maybee@Sun.COM 		return (err);
7262743Sahrens 	}
7272743Sahrens 
7282743Sahrens 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx);
7292743Sahrens 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
7302743Sahrens 
7317994STim.Haley@Sun.COM 	if (data != NULL) {
7322743Sahrens 		dmu_buf_t *db;
7337994STim.Haley@Sun.COM 
7342743Sahrens 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
7352743Sahrens 		dmu_buf_will_dirty(db, tx);
7362743Sahrens 
7374944Smaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
7384944Smaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
7392743Sahrens 		if (ra->byteswap) {
7402743Sahrens 			dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
7412743Sahrens 			    drro->drr_bonuslen);
7422743Sahrens 		}
7432743Sahrens 		dmu_buf_rele(db, FTAG);
7442743Sahrens 	}
7452743Sahrens 	dmu_tx_commit(tx);
7462743Sahrens 	return (0);
7472743Sahrens }
7482743Sahrens 
7492743Sahrens /* ARGSUSED */
7502743Sahrens static int
7512743Sahrens restore_freeobjects(struct restorearg *ra, objset_t *os,
7522743Sahrens     struct drr_freeobjects *drrfo)
7532743Sahrens {
7542743Sahrens 	uint64_t obj;
7552743Sahrens 
7562743Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
7572743Sahrens 		return (EINVAL);
7582743Sahrens 
7592743Sahrens 	for (obj = drrfo->drr_firstobj;
7603087Sahrens 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
7613087Sahrens 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
7622743Sahrens 		int err;
7632743Sahrens 
7642743Sahrens 		if (dmu_object_info(os, obj, NULL) != 0)
7652743Sahrens 			continue;
7662743Sahrens 
7676992Smaybee 		err = dmu_free_object(os, obj);
7686992Smaybee 		if (err)
7692743Sahrens 			return (err);
7702743Sahrens 	}
7712743Sahrens 	return (0);
7722743Sahrens }
7732743Sahrens 
7742743Sahrens static int
7752743Sahrens restore_write(struct restorearg *ra, objset_t *os,
7762743Sahrens     struct drr_write *drrw)
7772743Sahrens {
7782743Sahrens 	dmu_tx_t *tx;
7792743Sahrens 	void *data;
7802743Sahrens 	int err;
7812743Sahrens 
7822743Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
7832743Sahrens 	    drrw->drr_type >= DMU_OT_NUMTYPES)
7842743Sahrens 		return (EINVAL);
7852743Sahrens 
7862743Sahrens 	data = restore_read(ra, drrw->drr_length);
7872743Sahrens 	if (data == NULL)
7882743Sahrens 		return (ra->err);
7892743Sahrens 
7902743Sahrens 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
7912743Sahrens 		return (EINVAL);
7922743Sahrens 
7932743Sahrens 	tx = dmu_tx_create(os);
7942743Sahrens 
7952743Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
7962743Sahrens 	    drrw->drr_offset, drrw->drr_length);
7972743Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
7982743Sahrens 	if (err) {
7992743Sahrens 		dmu_tx_abort(tx);
8002743Sahrens 		return (err);
8012743Sahrens 	}
8022743Sahrens 	if (ra->byteswap)
8032743Sahrens 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
8042743Sahrens 	dmu_write(os, drrw->drr_object,
8052743Sahrens 	    drrw->drr_offset, drrw->drr_length, data, tx);
8062743Sahrens 	dmu_tx_commit(tx);
8072743Sahrens 	return (0);
8082743Sahrens }
8092743Sahrens 
8102743Sahrens /* ARGSUSED */
8112743Sahrens static int
8122743Sahrens restore_free(struct restorearg *ra, objset_t *os,
8132743Sahrens     struct drr_free *drrf)
8142743Sahrens {
8152743Sahrens 	int err;
8162743Sahrens 
8172743Sahrens 	if (drrf->drr_length != -1ULL &&
8182743Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
8192743Sahrens 		return (EINVAL);
8202743Sahrens 
8212743Sahrens 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
8222743Sahrens 		return (EINVAL);
8232743Sahrens 
8246992Smaybee 	err = dmu_free_long_range(os, drrf->drr_object,
8252743Sahrens 	    drrf->drr_offset, drrf->drr_length);
8262743Sahrens 	return (err);
8272743Sahrens }
8282743Sahrens 
8295367Sahrens /*
8305367Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
8315367Sahrens  */
8325367Sahrens int
8335367Sahrens dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp)
8345367Sahrens {
8355367Sahrens 	struct restorearg ra = { 0 };
8365367Sahrens 	dmu_replay_record_t *drr;
8375367Sahrens 	objset_t *os;
8385367Sahrens 	zio_cksum_t pcksum;
8395367Sahrens 
8405367Sahrens 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
8415367Sahrens 		ra.byteswap = TRUE;
8422743Sahrens 
8435367Sahrens 	{
8445367Sahrens 		/* compute checksum of drr_begin record */
8455367Sahrens 		dmu_replay_record_t *drr;
8465367Sahrens 		drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
8475367Sahrens 
8485367Sahrens 		drr->drr_type = DRR_BEGIN;
8495367Sahrens 		drr->drr_u.drr_begin = *drc->drc_drrb;
8505367Sahrens 		if (ra.byteswap) {
8515367Sahrens 			fletcher_4_incremental_byteswap(drr,
8525367Sahrens 			    sizeof (dmu_replay_record_t), &ra.cksum);
8535367Sahrens 		} else {
8545367Sahrens 			fletcher_4_incremental_native(drr,
8555367Sahrens 			    sizeof (dmu_replay_record_t), &ra.cksum);
8565367Sahrens 		}
8575367Sahrens 		kmem_free(drr, sizeof (dmu_replay_record_t));
8582743Sahrens 	}
8592743Sahrens 
8602743Sahrens 	if (ra.byteswap) {
8615367Sahrens 		struct drr_begin *drrb = drc->drc_drrb;
8622743Sahrens 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
8632743Sahrens 		drrb->drr_version = BSWAP_64(drrb->drr_version);
8642743Sahrens 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
8652743Sahrens 		drrb->drr_type = BSWAP_32(drrb->drr_type);
8662743Sahrens 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
8672743Sahrens 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
8682743Sahrens 	}
8692743Sahrens 
8705367Sahrens 	ra.vp = vp;
8715367Sahrens 	ra.voff = *voffp;
8725367Sahrens 	ra.bufsize = 1<<20;
8735367Sahrens 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
8745326Sek110237 
8755367Sahrens 	/* these were verified in dmu_recv_begin */
8765367Sahrens 	ASSERT(drc->drc_drrb->drr_version == DMU_BACKUP_STREAM_VERSION);
8775367Sahrens 	ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES);
8782743Sahrens 
8792743Sahrens 	/*
8802743Sahrens 	 * Open the objset we are modifying.
8812743Sahrens 	 */
882*10298SMatthew.Ahrens@Sun.COM 	VERIFY(dmu_objset_from_ds(drc->drc_real_ds, &os) == 0);
8832743Sahrens 
8845367Sahrens 	ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
8852743Sahrens 
8862743Sahrens 	/*
8872743Sahrens 	 * Read records and process them.
8882743Sahrens 	 */
8895367Sahrens 	pcksum = ra.cksum;
8902743Sahrens 	while (ra.err == 0 &&
8912743Sahrens 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
8922743Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
8932743Sahrens 			ra.err = EINTR;
8942743Sahrens 			goto out;
8952743Sahrens 		}
8962743Sahrens 
8972743Sahrens 		if (ra.byteswap)
8982743Sahrens 			backup_byteswap(drr);
8992743Sahrens 
9002743Sahrens 		switch (drr->drr_type) {
9012743Sahrens 		case DRR_OBJECT:
9022743Sahrens 		{
9032743Sahrens 			/*
9042743Sahrens 			 * We need to make a copy of the record header,
9052743Sahrens 			 * because restore_{object,write} may need to
9062743Sahrens 			 * restore_read(), which will invalidate drr.
9072743Sahrens 			 */
9082743Sahrens 			struct drr_object drro = drr->drr_u.drr_object;
9092743Sahrens 			ra.err = restore_object(&ra, os, &drro);
9102743Sahrens 			break;
9112743Sahrens 		}
9122743Sahrens 		case DRR_FREEOBJECTS:
9132743Sahrens 		{
9142743Sahrens 			struct drr_freeobjects drrfo =
9152743Sahrens 			    drr->drr_u.drr_freeobjects;
9162743Sahrens 			ra.err = restore_freeobjects(&ra, os, &drrfo);
9172743Sahrens 			break;
9182743Sahrens 		}
9192743Sahrens 		case DRR_WRITE:
9202743Sahrens 		{
9212743Sahrens 			struct drr_write drrw = drr->drr_u.drr_write;
9222743Sahrens 			ra.err = restore_write(&ra, os, &drrw);
9232743Sahrens 			break;
9242743Sahrens 		}
9252743Sahrens 		case DRR_FREE:
9262743Sahrens 		{
9272743Sahrens 			struct drr_free drrf = drr->drr_u.drr_free;
9282743Sahrens 			ra.err = restore_free(&ra, os, &drrf);
9292743Sahrens 			break;
9302743Sahrens 		}
9312743Sahrens 		case DRR_END:
9322743Sahrens 		{
9332743Sahrens 			struct drr_end drre = drr->drr_u.drr_end;
9342743Sahrens 			/*
9352743Sahrens 			 * We compare against the *previous* checksum
9362743Sahrens 			 * value, because the stored checksum is of
9372743Sahrens 			 * everything before the DRR_END record.
9382743Sahrens 			 */
9396479Sahrens 			if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
9402743Sahrens 				ra.err = ECKSUM;
9412743Sahrens 			goto out;
9422743Sahrens 		}
9432743Sahrens 		default:
9442743Sahrens 			ra.err = EINVAL;
9452743Sahrens 			goto out;
9462743Sahrens 		}
9475367Sahrens 		pcksum = ra.cksum;
9482743Sahrens 	}
9496479Sahrens 	ASSERT(ra.err != 0);
9502743Sahrens 
9512743Sahrens out:
9525367Sahrens 	if (ra.err != 0) {
9532743Sahrens 		/*
95410204SMatthew.Ahrens@Sun.COM 		 * destroy what we created, so we don't leave it in the
95510204SMatthew.Ahrens@Sun.COM 		 * inconsistent restoring state.
9562743Sahrens 		 */
9575367Sahrens 		txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0);
95810204SMatthew.Ahrens@Sun.COM 
95910242Schris.kirby@sun.com 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag,
96010242Schris.kirby@sun.com 		    B_FALSE);
96110204SMatthew.Ahrens@Sun.COM 		if (drc->drc_real_ds != drc->drc_logical_ds) {
96210204SMatthew.Ahrens@Sun.COM 			mutex_exit(&drc->drc_logical_ds->ds_recvlock);
96310204SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(drc->drc_logical_ds, dmu_recv_tag);
96410204SMatthew.Ahrens@Sun.COM 		}
9652743Sahrens 	}
9662743Sahrens 
9672743Sahrens 	kmem_free(ra.buf, ra.bufsize);
9685367Sahrens 	*voffp = ra.voff;
9692743Sahrens 	return (ra.err);
9702743Sahrens }
9715326Sek110237 
9725367Sahrens struct recvendsyncarg {
9735367Sahrens 	char *tosnap;
9745367Sahrens 	uint64_t creation_time;
9755367Sahrens 	uint64_t toguid;
9765367Sahrens };
9775367Sahrens 
9785367Sahrens static int
9795367Sahrens recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx)
9805367Sahrens {
9815367Sahrens 	dsl_dataset_t *ds = arg1;
9825367Sahrens 	struct recvendsyncarg *resa = arg2;
9835367Sahrens 
9845367Sahrens 	return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx));
9855367Sahrens }
9865367Sahrens 
9875367Sahrens static void
9885367Sahrens recv_end_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
9895326Sek110237 {
9905367Sahrens 	dsl_dataset_t *ds = arg1;
9915367Sahrens 	struct recvendsyncarg *resa = arg2;
9925367Sahrens 
9935367Sahrens 	dsl_dataset_snapshot_sync(ds, resa->tosnap, cr, tx);
9945367Sahrens 
9955367Sahrens 	/* set snapshot's creation time and guid */
9965367Sahrens 	dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
9975367Sahrens 	ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time;
9985367Sahrens 	ds->ds_prev->ds_phys->ds_guid = resa->toguid;
9995367Sahrens 	ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
10005367Sahrens 
10015367Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
10025367Sahrens 	ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
10035367Sahrens }
10045367Sahrens 
100510272SMatthew.Ahrens@Sun.COM static int
100610272SMatthew.Ahrens@Sun.COM dmu_recv_existing_end(dmu_recv_cookie_t *drc)
10075367Sahrens {
10086689Smaybee 	struct recvendsyncarg resa;
10096689Smaybee 	dsl_dataset_t *ds = drc->drc_logical_ds;
10106689Smaybee 	int err;
10115367Sahrens 
10125367Sahrens 	/*
101310272SMatthew.Ahrens@Sun.COM 	 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean()
101410272SMatthew.Ahrens@Sun.COM 	 * expects it to have a ds_user_ptr (and zil), but clone_swap()
101510272SMatthew.Ahrens@Sun.COM 	 * can close it.
10165367Sahrens 	 */
10176689Smaybee 	txg_wait_synced(ds->ds_dir->dd_pool, 0);
10185326Sek110237 
101910272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_tryown(ds, FALSE, dmu_recv_tag)) {
102010272SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_clone_swap(drc->drc_real_ds, ds,
102110272SMatthew.Ahrens@Sun.COM 		    drc->drc_force);
102210272SMatthew.Ahrens@Sun.COM 		if (err)
102310272SMatthew.Ahrens@Sun.COM 			goto out;
102410272SMatthew.Ahrens@Sun.COM 	} else {
102510272SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_recvlock);
102610272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, dmu_recv_tag);
102710242Schris.kirby@sun.com 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag,
102810242Schris.kirby@sun.com 		    B_FALSE);
102910272SMatthew.Ahrens@Sun.COM 		return (EBUSY);
10305367Sahrens 	}
10315367Sahrens 
10326689Smaybee 	resa.creation_time = drc->drc_drrb->drr_creation_time;
10336689Smaybee 	resa.toguid = drc->drc_drrb->drr_toguid;
10346689Smaybee 	resa.tosnap = drc->drc_tosnap;
10356689Smaybee 
10366689Smaybee 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
10376689Smaybee 	    recv_end_check, recv_end_sync, ds, &resa, 3);
10386689Smaybee 	if (err) {
103910272SMatthew.Ahrens@Sun.COM 		/* swap back */
104010272SMatthew.Ahrens@Sun.COM 		(void) dsl_dataset_clone_swap(drc->drc_real_ds, ds, B_TRUE);
10415367Sahrens 	}
10425367Sahrens 
104310272SMatthew.Ahrens@Sun.COM out:
104410272SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_recvlock);
10456689Smaybee 	dsl_dataset_disown(ds, dmu_recv_tag);
104610272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, B_FALSE);
10475326Sek110237 	return (err);
10485326Sek110237 }
104910272SMatthew.Ahrens@Sun.COM 
105010272SMatthew.Ahrens@Sun.COM static int
105110272SMatthew.Ahrens@Sun.COM dmu_recv_new_end(dmu_recv_cookie_t *drc)
105210272SMatthew.Ahrens@Sun.COM {
105310272SMatthew.Ahrens@Sun.COM 	struct recvendsyncarg resa;
105410272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = drc->drc_logical_ds;
105510272SMatthew.Ahrens@Sun.COM 	int err;
105610272SMatthew.Ahrens@Sun.COM 
105710272SMatthew.Ahrens@Sun.COM 	/*
105810272SMatthew.Ahrens@Sun.COM 	 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean()
105910272SMatthew.Ahrens@Sun.COM 	 * expects it to have a ds_user_ptr (and zil), but clone_swap()
106010272SMatthew.Ahrens@Sun.COM 	 * can close it.
106110272SMatthew.Ahrens@Sun.COM 	 */
106210272SMatthew.Ahrens@Sun.COM 	txg_wait_synced(ds->ds_dir->dd_pool, 0);
106310272SMatthew.Ahrens@Sun.COM 
106410272SMatthew.Ahrens@Sun.COM 	resa.creation_time = drc->drc_drrb->drr_creation_time;
106510272SMatthew.Ahrens@Sun.COM 	resa.toguid = drc->drc_drrb->drr_toguid;
106610272SMatthew.Ahrens@Sun.COM 	resa.tosnap = drc->drc_tosnap;
106710272SMatthew.Ahrens@Sun.COM 
106810272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
106910272SMatthew.Ahrens@Sun.COM 	    recv_end_check, recv_end_sync, ds, &resa, 3);
107010272SMatthew.Ahrens@Sun.COM 	if (err) {
107110272SMatthew.Ahrens@Sun.COM 		/* clean up the fs we just recv'd into */
107210272SMatthew.Ahrens@Sun.COM 		(void) dsl_dataset_destroy(ds, dmu_recv_tag, B_FALSE);
107310272SMatthew.Ahrens@Sun.COM 	} else {
107410272SMatthew.Ahrens@Sun.COM 		/* release the hold from dmu_recv_begin */
107510272SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, dmu_recv_tag);
107610272SMatthew.Ahrens@Sun.COM 	}
107710272SMatthew.Ahrens@Sun.COM 	return (err);
107810272SMatthew.Ahrens@Sun.COM }
107910272SMatthew.Ahrens@Sun.COM 
108010272SMatthew.Ahrens@Sun.COM int
108110272SMatthew.Ahrens@Sun.COM dmu_recv_end(dmu_recv_cookie_t *drc)
108210272SMatthew.Ahrens@Sun.COM {
108310272SMatthew.Ahrens@Sun.COM 	if (drc->drc_logical_ds != drc->drc_real_ds)
108410272SMatthew.Ahrens@Sun.COM 		return (dmu_recv_existing_end(drc));
108510272SMatthew.Ahrens@Sun.COM 	else
108610272SMatthew.Ahrens@Sun.COM 		return (dmu_recv_new_end(drc));
108710272SMatthew.Ahrens@Sun.COM }
1088