xref: /onnv-gate/usr/src/uts/common/fs/zfs/dmu_send.c (revision 10272:a0669934e974)
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 {
2192743Sahrens 	dsl_dataset_t *ds = tosnap->os->os_dsl_dataset;
2202743Sahrens 	dsl_dataset_t *fromds = fromsnap ? fromsnap->os->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;
2602743Sahrens 	drr->drr_u.drr_begin.drr_type = tosnap->os->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
326*10272SMatthew.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);
344*10272SMatthew.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
354*10272SMatthew.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 
361*10272SMatthew.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);
364*10272SMatthew.Ahrens@Sun.COM 	VERIFY(0 == dsl_dataset_own_obj(dd->dd_pool, dsobj,
365*10272SMatthew.Ahrens@Sun.COM 	    DS_MODE_INCONSISTENT, dmu_recv_tag, &rbsa->ds));
3665367Sahrens 
367*10272SMatthew.Ahrens@Sun.COM 	if (rbsa->origin == NULL) {
368*10272SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_create_impl(dd->dd_pool->dp_spa,
369*10272SMatthew.Ahrens@Sun.COM 		    rbsa->ds, &rbsa->ds->ds_phys->ds_bp, rbsa->type, tx);
3705367Sahrens 	}
3715367Sahrens 
372*10272SMatthew.Ahrens@Sun.COM 	spa_history_internal_log(LOG_DS_REPLAY_FULL_SYNC,
373*10272SMatthew.Ahrens@Sun.COM 	    dd->dd_pool->dp_spa, tx, cr, "dataset = %lld", dsobj);
3745367Sahrens }
3755367Sahrens 
3765367Sahrens /* ARGSUSED */
3775367Sahrens static int
378*10272SMatthew.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 
389*10272SMatthew.Ahrens@Sun.COM 	if (rbsa->fromguid) {
390*10272SMatthew.Ahrens@Sun.COM 		/* if incremental, most recent snapshot must match fromguid */
391*10272SMatthew.Ahrens@Sun.COM 		if (ds->ds_prev == NULL)
392*10272SMatthew.Ahrens@Sun.COM 			return (ENODEV);
393*10272SMatthew.Ahrens@Sun.COM 		if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid)
394*10272SMatthew.Ahrens@Sun.COM 			return (ENODEV);
395*10272SMatthew.Ahrens@Sun.COM 	} else {
396*10272SMatthew.Ahrens@Sun.COM 		/* if full, most recent snapshot must be $ORIGIN */
397*10272SMatthew.Ahrens@Sun.COM 		if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL)
398*10272SMatthew.Ahrens@Sun.COM 			return (ENODEV);
399*10272SMatthew.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
422*10272SMatthew.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;
427*10272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *cds;
4286689Smaybee 	uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
4295367Sahrens 	uint64_t dsobj;
4305326Sek110237 
431*10272SMatthew.Ahrens@Sun.COM 	/* create and open the temporary clone */
432*10272SMatthew.Ahrens@Sun.COM 	dsobj = dsl_dataset_create_sync(ohds->ds_dir, rbsa->clonelastname,
433*10272SMatthew.Ahrens@Sun.COM 	    ohds->ds_prev, flags, cr, tx);
4346689Smaybee 	VERIFY(0 == dsl_dataset_own_obj(dp, dsobj,
4356689Smaybee 	    DS_MODE_INCONSISTENT, dmu_recv_tag, &cds));
4365367Sahrens 
437*10272SMatthew.Ahrens@Sun.COM 	/*
438*10272SMatthew.Ahrens@Sun.COM 	 * If we actually created a non-clone, we need to create the
439*10272SMatthew.Ahrens@Sun.COM 	 * objset in our new dataset.
440*10272SMatthew.Ahrens@Sun.COM 	 */
441*10272SMatthew.Ahrens@Sun.COM 	if (BP_IS_HOLE(dsl_dataset_get_blkptr(cds))) {
442*10272SMatthew.Ahrens@Sun.COM 		(void) dmu_objset_create_impl(dp->dp_spa,
443*10272SMatthew.Ahrens@Sun.COM 		    cds, dsl_dataset_get_blkptr(cds), rbsa->type, tx);
444*10272SMatthew.Ahrens@Sun.COM 	}
445*10272SMatthew.Ahrens@Sun.COM 
4465378Sck153898 	/* copy the refquota from the target fs to the clone */
4475378Sck153898 	if (ohds->ds_quota > 0)
4485378Sck153898 		dsl_dataset_set_quota_sync(cds, &ohds->ds_quota, cr, tx);
4495378Sck153898 
4505367Sahrens 	rbsa->ds = cds;
4515367Sahrens 
4525367Sahrens 	spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC,
4536689Smaybee 	    dp->dp_spa, tx, cr, "dataset = %lld", dsobj);
4545326Sek110237 }
4555326Sek110237 
4565367Sahrens /*
4575367Sahrens  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
4585367Sahrens  * succeeds; otherwise we will leak the holds on the datasets.
4595367Sahrens  */
4605367Sahrens int
4615367Sahrens dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
46210204SMatthew.Ahrens@Sun.COM     boolean_t force, objset_t *origin, dmu_recv_cookie_t *drc)
4632743Sahrens {
4645367Sahrens 	int err = 0;
4655367Sahrens 	boolean_t byteswap;
466*10272SMatthew.Ahrens@Sun.COM 	struct recvbeginsyncarg rbsa = { 0 };
4675367Sahrens 	uint64_t version;
4685367Sahrens 	int flags;
4695367Sahrens 	dsl_dataset_t *ds;
4705367Sahrens 
4715367Sahrens 	if (drrb->drr_magic == DMU_BACKUP_MAGIC)
4725367Sahrens 		byteswap = FALSE;
4735367Sahrens 	else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
4745367Sahrens 		byteswap = TRUE;
4755367Sahrens 	else
4765367Sahrens 		return (EINVAL);
4775367Sahrens 
4785367Sahrens 	rbsa.tofs = tofs;
4795367Sahrens 	rbsa.tosnap = tosnap;
4805367Sahrens 	rbsa.origin = origin ? origin->os->os_dsl_dataset : NULL;
4815367Sahrens 	rbsa.fromguid = drrb->drr_fromguid;
4825367Sahrens 	rbsa.type = drrb->drr_type;
4835367Sahrens 	rbsa.tag = FTAG;
4846492Stimh 	rbsa.dsflags = 0;
4855367Sahrens 	version = drrb->drr_version;
4865367Sahrens 	flags = drrb->drr_flags;
4875367Sahrens 
4885367Sahrens 	if (byteswap) {
4895367Sahrens 		rbsa.type = BSWAP_32(rbsa.type);
4905367Sahrens 		rbsa.fromguid = BSWAP_64(rbsa.fromguid);
4915367Sahrens 		version = BSWAP_64(version);
4925367Sahrens 		flags = BSWAP_32(flags);
4935367Sahrens 	}
4945367Sahrens 
4955367Sahrens 	if (version != DMU_BACKUP_STREAM_VERSION ||
4965367Sahrens 	    rbsa.type >= DMU_OST_NUMTYPES ||
4975367Sahrens 	    ((flags & DRR_FLAG_CLONE) && origin == NULL))
4985367Sahrens 		return (EINVAL);
4995367Sahrens 
5006492Stimh 	if (flags & DRR_FLAG_CI_DATA)
5016492Stimh 		rbsa.dsflags = DS_FLAG_CI_DATASET;
5026492Stimh 
5035367Sahrens 	bzero(drc, sizeof (dmu_recv_cookie_t));
5045367Sahrens 	drc->drc_drrb = drrb;
5055367Sahrens 	drc->drc_tosnap = tosnap;
5065367Sahrens 	drc->drc_force = force;
5075367Sahrens 
5085367Sahrens 	/*
5095367Sahrens 	 * Process the begin in syncing context.
5105367Sahrens 	 */
511*10272SMatthew.Ahrens@Sun.COM 
512*10272SMatthew.Ahrens@Sun.COM 	/* open the dataset we are logically receiving into */
513*10272SMatthew.Ahrens@Sun.COM 	err = dsl_dataset_hold(tofs, dmu_recv_tag, &ds);
514*10272SMatthew.Ahrens@Sun.COM 	if (err == 0) {
515*10272SMatthew.Ahrens@Sun.COM 		/* target fs already exists; recv into temp clone */
5165367Sahrens 
517*10272SMatthew.Ahrens@Sun.COM 		/* Can't recv a clone into an existing fs */
518*10272SMatthew.Ahrens@Sun.COM 		if (flags & DRR_FLAG_CLONE) {
519*10272SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(ds, dmu_recv_tag);
520*10272SMatthew.Ahrens@Sun.COM 			return (EINVAL);
521*10272SMatthew.Ahrens@Sun.COM 		}
5222743Sahrens 
52310204SMatthew.Ahrens@Sun.COM 		/* must not have an incremental recv already in progress */
52410204SMatthew.Ahrens@Sun.COM 		if (!mutex_tryenter(&ds->ds_recvlock)) {
52510204SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(ds, dmu_recv_tag);
52610204SMatthew.Ahrens@Sun.COM 			return (EBUSY);
52710204SMatthew.Ahrens@Sun.COM 		}
52810204SMatthew.Ahrens@Sun.COM 
529*10272SMatthew.Ahrens@Sun.COM 		/* tmp clone name is: tofs/%tosnap" */
530*10272SMatthew.Ahrens@Sun.COM 		(void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname),
531*10272SMatthew.Ahrens@Sun.COM 		    "%%%s", tosnap);
5325367Sahrens 		rbsa.force = force;
5335367Sahrens 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
534*10272SMatthew.Ahrens@Sun.COM 		    recv_existing_check, recv_existing_sync, ds, &rbsa, 5);
5355367Sahrens 		if (err) {
53610204SMatthew.Ahrens@Sun.COM 			mutex_exit(&ds->ds_recvlock);
5376689Smaybee 			dsl_dataset_rele(ds, dmu_recv_tag);
5385367Sahrens 			return (err);
5395367Sahrens 		}
5405367Sahrens 		drc->drc_logical_ds = ds;
5415367Sahrens 		drc->drc_real_ds = rbsa.ds;
542*10272SMatthew.Ahrens@Sun.COM 	} else if (err == ENOENT) {
543*10272SMatthew.Ahrens@Sun.COM 		/* target fs does not exist; must be a full backup or clone */
544*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_t *parent;
545*10272SMatthew.Ahrens@Sun.COM 		char *cp;
5465367Sahrens 
547*10272SMatthew.Ahrens@Sun.COM 		/*
548*10272SMatthew.Ahrens@Sun.COM 		 * If it's a non-clone incremental, we are missing the
549*10272SMatthew.Ahrens@Sun.COM 		 * target fs, so fail the recv.
550*10272SMatthew.Ahrens@Sun.COM 		 */
551*10272SMatthew.Ahrens@Sun.COM 		if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE))
552*10272SMatthew.Ahrens@Sun.COM 			return (ENOENT);
553*10272SMatthew.Ahrens@Sun.COM 
554*10272SMatthew.Ahrens@Sun.COM 		/* Open the parent of tofs */
555*10272SMatthew.Ahrens@Sun.COM 		cp = strrchr(tofs, '/');
556*10272SMatthew.Ahrens@Sun.COM 		*cp = '\0';
557*10272SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_hold(tofs, FTAG, &parent);
558*10272SMatthew.Ahrens@Sun.COM 		*cp = '/';
5595367Sahrens 		if (err)
5605367Sahrens 			return (err);
5615367Sahrens 
562*10272SMatthew.Ahrens@Sun.COM 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
563*10272SMatthew.Ahrens@Sun.COM 		    recv_new_check, recv_new_sync, ds->ds_dir, &rbsa, 5);
564*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(parent, FTAG);
5655367Sahrens 		if (err)
5665367Sahrens 			return (err);
5675367Sahrens 		drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds;
5685367Sahrens 		drc->drc_newfs = B_TRUE;
5695367Sahrens 	}
5705367Sahrens 
571*10272SMatthew.Ahrens@Sun.COM 	return (err);
5722743Sahrens }
5732743Sahrens 
5745367Sahrens struct restorearg {
5755367Sahrens 	int err;
5765367Sahrens 	int byteswap;
5775367Sahrens 	vnode_t *vp;
5785367Sahrens 	char *buf;
5795367Sahrens 	uint64_t voff;
5805367Sahrens 	int bufsize; /* amount of memory allocated for buf */
5815367Sahrens 	zio_cksum_t cksum;
5825326Sek110237 };
5835326Sek110237 
5842743Sahrens static void *
5852743Sahrens restore_read(struct restorearg *ra, int len)
5862743Sahrens {
5872743Sahrens 	void *rv;
5885367Sahrens 	int done = 0;
5892743Sahrens 
5902743Sahrens 	/* some things will require 8-byte alignment, so everything must */
5912743Sahrens 	ASSERT3U(len % 8, ==, 0);
5922743Sahrens 
5935367Sahrens 	while (done < len) {
5942743Sahrens 		ssize_t resid;
5952743Sahrens 
5962743Sahrens 		ra->err = vn_rdwr(UIO_READ, ra->vp,
5975367Sahrens 		    (caddr_t)ra->buf + done, len - done,
5982743Sahrens 		    ra->voff, UIO_SYSSPACE, FAPPEND,
5992743Sahrens 		    RLIM64_INFINITY, CRED(), &resid);
6002743Sahrens 
6015367Sahrens 		if (resid == len - done)
6022743Sahrens 			ra->err = EINVAL;
6035367Sahrens 		ra->voff += len - done - resid;
6045367Sahrens 		done = len - resid;
6052743Sahrens 		if (ra->err)
6062743Sahrens 			return (NULL);
6072743Sahrens 	}
6082743Sahrens 
6095367Sahrens 	ASSERT3U(done, ==, len);
6105367Sahrens 	rv = ra->buf;
6112743Sahrens 	if (ra->byteswap)
6125367Sahrens 		fletcher_4_incremental_byteswap(rv, len, &ra->cksum);
6132743Sahrens 	else
6145367Sahrens 		fletcher_4_incremental_native(rv, len, &ra->cksum);
6152743Sahrens 	return (rv);
6162743Sahrens }
6172743Sahrens 
6182743Sahrens static void
6192743Sahrens backup_byteswap(dmu_replay_record_t *drr)
6202743Sahrens {
6212743Sahrens #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
6222743Sahrens #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
6232743Sahrens 	drr->drr_type = BSWAP_32(drr->drr_type);
6245367Sahrens 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
6252743Sahrens 	switch (drr->drr_type) {
6262743Sahrens 	case DRR_BEGIN:
6272743Sahrens 		DO64(drr_begin.drr_magic);
6282743Sahrens 		DO64(drr_begin.drr_version);
6292743Sahrens 		DO64(drr_begin.drr_creation_time);
6302743Sahrens 		DO32(drr_begin.drr_type);
6315367Sahrens 		DO32(drr_begin.drr_flags);
6322743Sahrens 		DO64(drr_begin.drr_toguid);
6332743Sahrens 		DO64(drr_begin.drr_fromguid);
6342743Sahrens 		break;
6352743Sahrens 	case DRR_OBJECT:
6362743Sahrens 		DO64(drr_object.drr_object);
6372743Sahrens 		/* DO64(drr_object.drr_allocation_txg); */
6382743Sahrens 		DO32(drr_object.drr_type);
6392743Sahrens 		DO32(drr_object.drr_bonustype);
6402743Sahrens 		DO32(drr_object.drr_blksz);
6412743Sahrens 		DO32(drr_object.drr_bonuslen);
6422743Sahrens 		break;
6432743Sahrens 	case DRR_FREEOBJECTS:
6442743Sahrens 		DO64(drr_freeobjects.drr_firstobj);
6452743Sahrens 		DO64(drr_freeobjects.drr_numobjs);
6462743Sahrens 		break;
6472743Sahrens 	case DRR_WRITE:
6482743Sahrens 		DO64(drr_write.drr_object);
6492743Sahrens 		DO32(drr_write.drr_type);
6502743Sahrens 		DO64(drr_write.drr_offset);
6512743Sahrens 		DO64(drr_write.drr_length);
6522743Sahrens 		break;
6532743Sahrens 	case DRR_FREE:
6542743Sahrens 		DO64(drr_free.drr_object);
6552743Sahrens 		DO64(drr_free.drr_offset);
6562743Sahrens 		DO64(drr_free.drr_length);
6572743Sahrens 		break;
6582743Sahrens 	case DRR_END:
6592743Sahrens 		DO64(drr_end.drr_checksum.zc_word[0]);
6602743Sahrens 		DO64(drr_end.drr_checksum.zc_word[1]);
6612743Sahrens 		DO64(drr_end.drr_checksum.zc_word[2]);
6622743Sahrens 		DO64(drr_end.drr_checksum.zc_word[3]);
6632743Sahrens 		break;
6642743Sahrens 	}
6652743Sahrens #undef DO64
6662743Sahrens #undef DO32
6672743Sahrens }
6682743Sahrens 
6692743Sahrens static int
6702743Sahrens restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
6712743Sahrens {
6722743Sahrens 	int err;
6732743Sahrens 	dmu_tx_t *tx;
6747994STim.Haley@Sun.COM 	void *data = NULL;
6752743Sahrens 
6762743Sahrens 	if (drro->drr_type == DMU_OT_NONE ||
6772743Sahrens 	    drro->drr_type >= DMU_OT_NUMTYPES ||
6782743Sahrens 	    drro->drr_bonustype >= DMU_OT_NUMTYPES ||
6792743Sahrens 	    drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS ||
6802743Sahrens 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
6812743Sahrens 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
6822743Sahrens 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
6832743Sahrens 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
6842743Sahrens 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
6852743Sahrens 		return (EINVAL);
6862743Sahrens 	}
6872743Sahrens 
6888986SMark.Maybee@Sun.COM 	err = dmu_object_info(os, drro->drr_object, NULL);
6898986SMark.Maybee@Sun.COM 
6908986SMark.Maybee@Sun.COM 	if (err != 0 && err != ENOENT)
6918986SMark.Maybee@Sun.COM 		return (EINVAL);
6928986SMark.Maybee@Sun.COM 
6937994STim.Haley@Sun.COM 	if (drro->drr_bonuslen) {
6947994STim.Haley@Sun.COM 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
6957994STim.Haley@Sun.COM 		if (ra->err)
6967994STim.Haley@Sun.COM 			return (ra->err);
6977994STim.Haley@Sun.COM 	}
6987994STim.Haley@Sun.COM 
6992743Sahrens 	if (err == ENOENT) {
7002743Sahrens 		/* currently free, want to be allocated */
7018986SMark.Maybee@Sun.COM 		tx = dmu_tx_create(os);
7022743Sahrens 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
7032743Sahrens 		err = dmu_tx_assign(tx, TXG_WAIT);
7042743Sahrens 		if (err) {
7052743Sahrens 			dmu_tx_abort(tx);
7062743Sahrens 			return (err);
7072743Sahrens 		}
7082743Sahrens 		err = dmu_object_claim(os, drro->drr_object,
7092743Sahrens 		    drro->drr_type, drro->drr_blksz,
7102743Sahrens 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
7118986SMark.Maybee@Sun.COM 		dmu_tx_commit(tx);
7122743Sahrens 	} else {
7132743Sahrens 		/* currently allocated, want to be allocated */
7142743Sahrens 		err = dmu_object_reclaim(os, drro->drr_object,
7152743Sahrens 		    drro->drr_type, drro->drr_blksz,
7168986SMark.Maybee@Sun.COM 		    drro->drr_bonustype, drro->drr_bonuslen);
7172743Sahrens 	}
7188986SMark.Maybee@Sun.COM 	if (err)
7198986SMark.Maybee@Sun.COM 		return (EINVAL);
7208986SMark.Maybee@Sun.COM 
7218986SMark.Maybee@Sun.COM 	tx = dmu_tx_create(os);
7228986SMark.Maybee@Sun.COM 	dmu_tx_hold_bonus(tx, drro->drr_object);
7238986SMark.Maybee@Sun.COM 	err = dmu_tx_assign(tx, TXG_WAIT);
7242743Sahrens 	if (err) {
7258986SMark.Maybee@Sun.COM 		dmu_tx_abort(tx);
7268986SMark.Maybee@Sun.COM 		return (err);
7272743Sahrens 	}
7282743Sahrens 
7292743Sahrens 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx);
7302743Sahrens 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
7312743Sahrens 
7327994STim.Haley@Sun.COM 	if (data != NULL) {
7332743Sahrens 		dmu_buf_t *db;
7347994STim.Haley@Sun.COM 
7352743Sahrens 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
7362743Sahrens 		dmu_buf_will_dirty(db, tx);
7372743Sahrens 
7384944Smaybee 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
7394944Smaybee 		bcopy(data, db->db_data, drro->drr_bonuslen);
7402743Sahrens 		if (ra->byteswap) {
7412743Sahrens 			dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
7422743Sahrens 			    drro->drr_bonuslen);
7432743Sahrens 		}
7442743Sahrens 		dmu_buf_rele(db, FTAG);
7452743Sahrens 	}
7462743Sahrens 	dmu_tx_commit(tx);
7472743Sahrens 	return (0);
7482743Sahrens }
7492743Sahrens 
7502743Sahrens /* ARGSUSED */
7512743Sahrens static int
7522743Sahrens restore_freeobjects(struct restorearg *ra, objset_t *os,
7532743Sahrens     struct drr_freeobjects *drrfo)
7542743Sahrens {
7552743Sahrens 	uint64_t obj;
7562743Sahrens 
7572743Sahrens 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
7582743Sahrens 		return (EINVAL);
7592743Sahrens 
7602743Sahrens 	for (obj = drrfo->drr_firstobj;
7613087Sahrens 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
7623087Sahrens 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
7632743Sahrens 		int err;
7642743Sahrens 
7652743Sahrens 		if (dmu_object_info(os, obj, NULL) != 0)
7662743Sahrens 			continue;
7672743Sahrens 
7686992Smaybee 		err = dmu_free_object(os, obj);
7696992Smaybee 		if (err)
7702743Sahrens 			return (err);
7712743Sahrens 	}
7722743Sahrens 	return (0);
7732743Sahrens }
7742743Sahrens 
7752743Sahrens static int
7762743Sahrens restore_write(struct restorearg *ra, objset_t *os,
7772743Sahrens     struct drr_write *drrw)
7782743Sahrens {
7792743Sahrens 	dmu_tx_t *tx;
7802743Sahrens 	void *data;
7812743Sahrens 	int err;
7822743Sahrens 
7832743Sahrens 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
7842743Sahrens 	    drrw->drr_type >= DMU_OT_NUMTYPES)
7852743Sahrens 		return (EINVAL);
7862743Sahrens 
7872743Sahrens 	data = restore_read(ra, drrw->drr_length);
7882743Sahrens 	if (data == NULL)
7892743Sahrens 		return (ra->err);
7902743Sahrens 
7912743Sahrens 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
7922743Sahrens 		return (EINVAL);
7932743Sahrens 
7942743Sahrens 	tx = dmu_tx_create(os);
7952743Sahrens 
7962743Sahrens 	dmu_tx_hold_write(tx, drrw->drr_object,
7972743Sahrens 	    drrw->drr_offset, drrw->drr_length);
7982743Sahrens 	err = dmu_tx_assign(tx, TXG_WAIT);
7992743Sahrens 	if (err) {
8002743Sahrens 		dmu_tx_abort(tx);
8012743Sahrens 		return (err);
8022743Sahrens 	}
8032743Sahrens 	if (ra->byteswap)
8042743Sahrens 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
8052743Sahrens 	dmu_write(os, drrw->drr_object,
8062743Sahrens 	    drrw->drr_offset, drrw->drr_length, data, tx);
8072743Sahrens 	dmu_tx_commit(tx);
8082743Sahrens 	return (0);
8092743Sahrens }
8102743Sahrens 
8112743Sahrens /* ARGSUSED */
8122743Sahrens static int
8132743Sahrens restore_free(struct restorearg *ra, objset_t *os,
8142743Sahrens     struct drr_free *drrf)
8152743Sahrens {
8162743Sahrens 	int err;
8172743Sahrens 
8182743Sahrens 	if (drrf->drr_length != -1ULL &&
8192743Sahrens 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
8202743Sahrens 		return (EINVAL);
8212743Sahrens 
8222743Sahrens 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
8232743Sahrens 		return (EINVAL);
8242743Sahrens 
8256992Smaybee 	err = dmu_free_long_range(os, drrf->drr_object,
8262743Sahrens 	    drrf->drr_offset, drrf->drr_length);
8272743Sahrens 	return (err);
8282743Sahrens }
8292743Sahrens 
8305367Sahrens /*
8315367Sahrens  * NB: callers *must* call dmu_recv_end() if this succeeds.
8325367Sahrens  */
8335367Sahrens int
8345367Sahrens dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp)
8355367Sahrens {
8365367Sahrens 	struct restorearg ra = { 0 };
8375367Sahrens 	dmu_replay_record_t *drr;
8385367Sahrens 	objset_t *os;
8395367Sahrens 	zio_cksum_t pcksum;
8405367Sahrens 
8415367Sahrens 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
8425367Sahrens 		ra.byteswap = TRUE;
8432743Sahrens 
8445367Sahrens 	{
8455367Sahrens 		/* compute checksum of drr_begin record */
8465367Sahrens 		dmu_replay_record_t *drr;
8475367Sahrens 		drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
8485367Sahrens 
8495367Sahrens 		drr->drr_type = DRR_BEGIN;
8505367Sahrens 		drr->drr_u.drr_begin = *drc->drc_drrb;
8515367Sahrens 		if (ra.byteswap) {
8525367Sahrens 			fletcher_4_incremental_byteswap(drr,
8535367Sahrens 			    sizeof (dmu_replay_record_t), &ra.cksum);
8545367Sahrens 		} else {
8555367Sahrens 			fletcher_4_incremental_native(drr,
8565367Sahrens 			    sizeof (dmu_replay_record_t), &ra.cksum);
8575367Sahrens 		}
8585367Sahrens 		kmem_free(drr, sizeof (dmu_replay_record_t));
8592743Sahrens 	}
8602743Sahrens 
8612743Sahrens 	if (ra.byteswap) {
8625367Sahrens 		struct drr_begin *drrb = drc->drc_drrb;
8632743Sahrens 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
8642743Sahrens 		drrb->drr_version = BSWAP_64(drrb->drr_version);
8652743Sahrens 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
8662743Sahrens 		drrb->drr_type = BSWAP_32(drrb->drr_type);
8672743Sahrens 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
8682743Sahrens 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
8692743Sahrens 	}
8702743Sahrens 
8715367Sahrens 	ra.vp = vp;
8725367Sahrens 	ra.voff = *voffp;
8735367Sahrens 	ra.bufsize = 1<<20;
8745367Sahrens 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
8755326Sek110237 
8765367Sahrens 	/* these were verified in dmu_recv_begin */
8775367Sahrens 	ASSERT(drc->drc_drrb->drr_version == DMU_BACKUP_STREAM_VERSION);
8785367Sahrens 	ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES);
8792743Sahrens 
8802743Sahrens 	/*
8812743Sahrens 	 * Open the objset we are modifying.
8822743Sahrens 	 */
8835367Sahrens 	VERIFY(dmu_objset_open_ds(drc->drc_real_ds, DMU_OST_ANY, &os) == 0);
8842743Sahrens 
8855367Sahrens 	ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
8862743Sahrens 
8872743Sahrens 	/*
8882743Sahrens 	 * Read records and process them.
8892743Sahrens 	 */
8905367Sahrens 	pcksum = ra.cksum;
8912743Sahrens 	while (ra.err == 0 &&
8922743Sahrens 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
8932743Sahrens 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
8942743Sahrens 			ra.err = EINTR;
8952743Sahrens 			goto out;
8962743Sahrens 		}
8972743Sahrens 
8982743Sahrens 		if (ra.byteswap)
8992743Sahrens 			backup_byteswap(drr);
9002743Sahrens 
9012743Sahrens 		switch (drr->drr_type) {
9022743Sahrens 		case DRR_OBJECT:
9032743Sahrens 		{
9042743Sahrens 			/*
9052743Sahrens 			 * We need to make a copy of the record header,
9062743Sahrens 			 * because restore_{object,write} may need to
9072743Sahrens 			 * restore_read(), which will invalidate drr.
9082743Sahrens 			 */
9092743Sahrens 			struct drr_object drro = drr->drr_u.drr_object;
9102743Sahrens 			ra.err = restore_object(&ra, os, &drro);
9112743Sahrens 			break;
9122743Sahrens 		}
9132743Sahrens 		case DRR_FREEOBJECTS:
9142743Sahrens 		{
9152743Sahrens 			struct drr_freeobjects drrfo =
9162743Sahrens 			    drr->drr_u.drr_freeobjects;
9172743Sahrens 			ra.err = restore_freeobjects(&ra, os, &drrfo);
9182743Sahrens 			break;
9192743Sahrens 		}
9202743Sahrens 		case DRR_WRITE:
9212743Sahrens 		{
9222743Sahrens 			struct drr_write drrw = drr->drr_u.drr_write;
9232743Sahrens 			ra.err = restore_write(&ra, os, &drrw);
9242743Sahrens 			break;
9252743Sahrens 		}
9262743Sahrens 		case DRR_FREE:
9272743Sahrens 		{
9282743Sahrens 			struct drr_free drrf = drr->drr_u.drr_free;
9292743Sahrens 			ra.err = restore_free(&ra, os, &drrf);
9302743Sahrens 			break;
9312743Sahrens 		}
9322743Sahrens 		case DRR_END:
9332743Sahrens 		{
9342743Sahrens 			struct drr_end drre = drr->drr_u.drr_end;
9352743Sahrens 			/*
9362743Sahrens 			 * We compare against the *previous* checksum
9372743Sahrens 			 * value, because the stored checksum is of
9382743Sahrens 			 * everything before the DRR_END record.
9392743Sahrens 			 */
9406479Sahrens 			if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
9412743Sahrens 				ra.err = ECKSUM;
9422743Sahrens 			goto out;
9432743Sahrens 		}
9442743Sahrens 		default:
9452743Sahrens 			ra.err = EINVAL;
9462743Sahrens 			goto out;
9472743Sahrens 		}
9485367Sahrens 		pcksum = ra.cksum;
9492743Sahrens 	}
9506479Sahrens 	ASSERT(ra.err != 0);
9512743Sahrens 
9522743Sahrens out:
9535367Sahrens 	dmu_objset_close(os);
9542743Sahrens 
9555367Sahrens 	if (ra.err != 0) {
9562743Sahrens 		/*
95710204SMatthew.Ahrens@Sun.COM 		 * destroy what we created, so we don't leave it in the
95810204SMatthew.Ahrens@Sun.COM 		 * inconsistent restoring state.
9592743Sahrens 		 */
9605367Sahrens 		txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0);
96110204SMatthew.Ahrens@Sun.COM 
96210242Schris.kirby@sun.com 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag,
96310242Schris.kirby@sun.com 		    B_FALSE);
96410204SMatthew.Ahrens@Sun.COM 		if (drc->drc_real_ds != drc->drc_logical_ds) {
96510204SMatthew.Ahrens@Sun.COM 			mutex_exit(&drc->drc_logical_ds->ds_recvlock);
96610204SMatthew.Ahrens@Sun.COM 			dsl_dataset_rele(drc->drc_logical_ds, dmu_recv_tag);
96710204SMatthew.Ahrens@Sun.COM 		}
9682743Sahrens 	}
9692743Sahrens 
9702743Sahrens 	kmem_free(ra.buf, ra.bufsize);
9715367Sahrens 	*voffp = ra.voff;
9722743Sahrens 	return (ra.err);
9732743Sahrens }
9745326Sek110237 
9755367Sahrens struct recvendsyncarg {
9765367Sahrens 	char *tosnap;
9775367Sahrens 	uint64_t creation_time;
9785367Sahrens 	uint64_t toguid;
9795367Sahrens };
9805367Sahrens 
9815367Sahrens static int
9825367Sahrens recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx)
9835367Sahrens {
9845367Sahrens 	dsl_dataset_t *ds = arg1;
9855367Sahrens 	struct recvendsyncarg *resa = arg2;
9865367Sahrens 
9875367Sahrens 	return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx));
9885367Sahrens }
9895367Sahrens 
9905367Sahrens static void
9915367Sahrens recv_end_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
9925326Sek110237 {
9935367Sahrens 	dsl_dataset_t *ds = arg1;
9945367Sahrens 	struct recvendsyncarg *resa = arg2;
9955367Sahrens 
9965367Sahrens 	dsl_dataset_snapshot_sync(ds, resa->tosnap, cr, tx);
9975367Sahrens 
9985367Sahrens 	/* set snapshot's creation time and guid */
9995367Sahrens 	dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
10005367Sahrens 	ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time;
10015367Sahrens 	ds->ds_prev->ds_phys->ds_guid = resa->toguid;
10025367Sahrens 	ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
10035367Sahrens 
10045367Sahrens 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
10055367Sahrens 	ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
10065367Sahrens }
10075367Sahrens 
1008*10272SMatthew.Ahrens@Sun.COM static int
1009*10272SMatthew.Ahrens@Sun.COM dmu_recv_existing_end(dmu_recv_cookie_t *drc)
10105367Sahrens {
10116689Smaybee 	struct recvendsyncarg resa;
10126689Smaybee 	dsl_dataset_t *ds = drc->drc_logical_ds;
10136689Smaybee 	int err;
10145367Sahrens 
10155367Sahrens 	/*
1016*10272SMatthew.Ahrens@Sun.COM 	 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean()
1017*10272SMatthew.Ahrens@Sun.COM 	 * expects it to have a ds_user_ptr (and zil), but clone_swap()
1018*10272SMatthew.Ahrens@Sun.COM 	 * can close it.
10195367Sahrens 	 */
10206689Smaybee 	txg_wait_synced(ds->ds_dir->dd_pool, 0);
10215326Sek110237 
1022*10272SMatthew.Ahrens@Sun.COM 	if (dsl_dataset_tryown(ds, FALSE, dmu_recv_tag)) {
1023*10272SMatthew.Ahrens@Sun.COM 		err = dsl_dataset_clone_swap(drc->drc_real_ds, ds,
1024*10272SMatthew.Ahrens@Sun.COM 		    drc->drc_force);
1025*10272SMatthew.Ahrens@Sun.COM 		if (err)
1026*10272SMatthew.Ahrens@Sun.COM 			goto out;
1027*10272SMatthew.Ahrens@Sun.COM 	} else {
1028*10272SMatthew.Ahrens@Sun.COM 		mutex_exit(&ds->ds_recvlock);
1029*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_rele(ds, dmu_recv_tag);
103010242Schris.kirby@sun.com 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag,
103110242Schris.kirby@sun.com 		    B_FALSE);
1032*10272SMatthew.Ahrens@Sun.COM 		return (EBUSY);
10335367Sahrens 	}
10345367Sahrens 
10356689Smaybee 	resa.creation_time = drc->drc_drrb->drr_creation_time;
10366689Smaybee 	resa.toguid = drc->drc_drrb->drr_toguid;
10376689Smaybee 	resa.tosnap = drc->drc_tosnap;
10386689Smaybee 
10396689Smaybee 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
10406689Smaybee 	    recv_end_check, recv_end_sync, ds, &resa, 3);
10416689Smaybee 	if (err) {
1042*10272SMatthew.Ahrens@Sun.COM 		/* swap back */
1043*10272SMatthew.Ahrens@Sun.COM 		(void) dsl_dataset_clone_swap(drc->drc_real_ds, ds, B_TRUE);
10445367Sahrens 	}
10455367Sahrens 
1046*10272SMatthew.Ahrens@Sun.COM out:
1047*10272SMatthew.Ahrens@Sun.COM 	mutex_exit(&ds->ds_recvlock);
10486689Smaybee 	dsl_dataset_disown(ds, dmu_recv_tag);
1049*10272SMatthew.Ahrens@Sun.COM 	(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, B_FALSE);
10505326Sek110237 	return (err);
10515326Sek110237 }
1052*10272SMatthew.Ahrens@Sun.COM 
1053*10272SMatthew.Ahrens@Sun.COM static int
1054*10272SMatthew.Ahrens@Sun.COM dmu_recv_new_end(dmu_recv_cookie_t *drc)
1055*10272SMatthew.Ahrens@Sun.COM {
1056*10272SMatthew.Ahrens@Sun.COM 	struct recvendsyncarg resa;
1057*10272SMatthew.Ahrens@Sun.COM 	dsl_dataset_t *ds = drc->drc_logical_ds;
1058*10272SMatthew.Ahrens@Sun.COM 	int err;
1059*10272SMatthew.Ahrens@Sun.COM 
1060*10272SMatthew.Ahrens@Sun.COM 	/*
1061*10272SMatthew.Ahrens@Sun.COM 	 * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean()
1062*10272SMatthew.Ahrens@Sun.COM 	 * expects it to have a ds_user_ptr (and zil), but clone_swap()
1063*10272SMatthew.Ahrens@Sun.COM 	 * can close it.
1064*10272SMatthew.Ahrens@Sun.COM 	 */
1065*10272SMatthew.Ahrens@Sun.COM 	txg_wait_synced(ds->ds_dir->dd_pool, 0);
1066*10272SMatthew.Ahrens@Sun.COM 
1067*10272SMatthew.Ahrens@Sun.COM 	resa.creation_time = drc->drc_drrb->drr_creation_time;
1068*10272SMatthew.Ahrens@Sun.COM 	resa.toguid = drc->drc_drrb->drr_toguid;
1069*10272SMatthew.Ahrens@Sun.COM 	resa.tosnap = drc->drc_tosnap;
1070*10272SMatthew.Ahrens@Sun.COM 
1071*10272SMatthew.Ahrens@Sun.COM 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1072*10272SMatthew.Ahrens@Sun.COM 	    recv_end_check, recv_end_sync, ds, &resa, 3);
1073*10272SMatthew.Ahrens@Sun.COM 	if (err) {
1074*10272SMatthew.Ahrens@Sun.COM 		/* clean up the fs we just recv'd into */
1075*10272SMatthew.Ahrens@Sun.COM 		(void) dsl_dataset_destroy(ds, dmu_recv_tag, B_FALSE);
1076*10272SMatthew.Ahrens@Sun.COM 	} else {
1077*10272SMatthew.Ahrens@Sun.COM 		/* release the hold from dmu_recv_begin */
1078*10272SMatthew.Ahrens@Sun.COM 		dsl_dataset_disown(ds, dmu_recv_tag);
1079*10272SMatthew.Ahrens@Sun.COM 	}
1080*10272SMatthew.Ahrens@Sun.COM 	return (err);
1081*10272SMatthew.Ahrens@Sun.COM }
1082*10272SMatthew.Ahrens@Sun.COM 
1083*10272SMatthew.Ahrens@Sun.COM int
1084*10272SMatthew.Ahrens@Sun.COM dmu_recv_end(dmu_recv_cookie_t *drc)
1085*10272SMatthew.Ahrens@Sun.COM {
1086*10272SMatthew.Ahrens@Sun.COM 	if (drc->drc_logical_ds != drc->drc_real_ds)
1087*10272SMatthew.Ahrens@Sun.COM 		return (dmu_recv_existing_end(drc));
1088*10272SMatthew.Ahrens@Sun.COM 	else
1089*10272SMatthew.Ahrens@Sun.COM 		return (dmu_recv_new_end(drc));
1090*10272SMatthew.Ahrens@Sun.COM }
1091