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 /*
2212070SMark.Shellenbaum@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
232743Sahrens */
242743Sahrens
252743Sahrens #include <sys/dmu.h>
262743Sahrens #include <sys/dmu_impl.h>
272743Sahrens #include <sys/dmu_tx.h>
282743Sahrens #include <sys/dbuf.h>
292743Sahrens #include <sys/dnode.h>
302743Sahrens #include <sys/zfs_context.h>
312743Sahrens #include <sys/dmu_objset.h>
322743Sahrens #include <sys/dmu_traverse.h>
332743Sahrens #include <sys/dsl_dataset.h>
342743Sahrens #include <sys/dsl_dir.h>
3511022STom.Erickson@Sun.COM #include <sys/dsl_prop.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>
4112070SMark.Shellenbaum@Sun.COM #include <sys/zfs_znode.h>
4212470SMatthew.Ahrens@Sun.COM #include <zfs_fletcher.h>
4311007SLori.Alt@Sun.COM #include <sys/avl.h>
4411381SLori.Alt@Sun.COM #include <sys/ddt.h>
4512527SChris.Kirby@oracle.com #include <sys/zfs_onexit.h>
462743Sahrens
475367Sahrens static char *dmu_recv_tag = "dmu_recv_tag";
485367Sahrens
4911007SLori.Alt@Sun.COM /*
5011007SLori.Alt@Sun.COM * The list of data whose inclusion in a send stream can be pending from
5111007SLori.Alt@Sun.COM * one call to backup_cb to another. Multiple calls to dump_free() and
5211007SLori.Alt@Sun.COM * dump_freeobjects() can be aggregated into a single DRR_FREE or
5311007SLori.Alt@Sun.COM * DRR_FREEOBJECTS replay record.
5411007SLori.Alt@Sun.COM */
5511007SLori.Alt@Sun.COM typedef enum {
5611007SLori.Alt@Sun.COM PENDING_NONE,
5711007SLori.Alt@Sun.COM PENDING_FREE,
5811007SLori.Alt@Sun.COM PENDING_FREEOBJECTS
5911007SLori.Alt@Sun.COM } pendop_t;
6011007SLori.Alt@Sun.COM
612743Sahrens struct backuparg {
622743Sahrens dmu_replay_record_t *drr;
632743Sahrens vnode_t *vp;
645367Sahrens offset_t *off;
652743Sahrens objset_t *os;
662743Sahrens zio_cksum_t zc;
6711007SLori.Alt@Sun.COM uint64_t toguid;
682743Sahrens int err;
6911007SLori.Alt@Sun.COM pendop_t pending_op;
702743Sahrens };
712743Sahrens
722743Sahrens static int
dump_bytes(struct backuparg * ba,void * buf,int len)732743Sahrens dump_bytes(struct backuparg *ba, void *buf, int len)
742743Sahrens {
752743Sahrens ssize_t resid; /* have to get resid to get detailed errno */
762743Sahrens ASSERT3U(len % 8, ==, 0);
772743Sahrens
782743Sahrens fletcher_4_incremental_native(buf, len, &ba->zc);
792743Sahrens ba->err = vn_rdwr(UIO_WRITE, ba->vp,
802743Sahrens (caddr_t)buf, len,
812743Sahrens 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
825367Sahrens *ba->off += len;
832743Sahrens return (ba->err);
842743Sahrens }
852743Sahrens
862743Sahrens static int
dump_free(struct backuparg * ba,uint64_t object,uint64_t offset,uint64_t length)872743Sahrens dump_free(struct backuparg *ba, uint64_t object, uint64_t offset,
882743Sahrens uint64_t length)
892743Sahrens {
9011007SLori.Alt@Sun.COM struct drr_free *drrf = &(ba->drr->drr_u.drr_free);
9111007SLori.Alt@Sun.COM
9211007SLori.Alt@Sun.COM /*
9311007SLori.Alt@Sun.COM * If there is a pending op, but it's not PENDING_FREE, push it out,
9411007SLori.Alt@Sun.COM * since free block aggregation can only be done for blocks of the
9511007SLori.Alt@Sun.COM * same type (i.e., DRR_FREE records can only be aggregated with
9611007SLori.Alt@Sun.COM * other DRR_FREE records. DRR_FREEOBJECTS records can only be
9711007SLori.Alt@Sun.COM * aggregated with other DRR_FREEOBJECTS records.
9811007SLori.Alt@Sun.COM */
9911007SLori.Alt@Sun.COM if (ba->pending_op != PENDING_NONE && ba->pending_op != PENDING_FREE) {
10011007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0)
10111007SLori.Alt@Sun.COM return (EINTR);
10211007SLori.Alt@Sun.COM ba->pending_op = PENDING_NONE;
10311007SLori.Alt@Sun.COM }
10411007SLori.Alt@Sun.COM
10511007SLori.Alt@Sun.COM if (ba->pending_op == PENDING_FREE) {
10611007SLori.Alt@Sun.COM /*
10711007SLori.Alt@Sun.COM * There should never be a PENDING_FREE if length is -1
10811007SLori.Alt@Sun.COM * (because dump_dnode is the only place where this
10911007SLori.Alt@Sun.COM * function is called with a -1, and only after flushing
11011007SLori.Alt@Sun.COM * any pending record).
11111007SLori.Alt@Sun.COM */
11211007SLori.Alt@Sun.COM ASSERT(length != -1ULL);
11311007SLori.Alt@Sun.COM /*
11411007SLori.Alt@Sun.COM * Check to see whether this free block can be aggregated
11511007SLori.Alt@Sun.COM * with pending one.
11611007SLori.Alt@Sun.COM */
11711007SLori.Alt@Sun.COM if (drrf->drr_object == object && drrf->drr_offset +
11811007SLori.Alt@Sun.COM drrf->drr_length == offset) {
11911007SLori.Alt@Sun.COM drrf->drr_length += length;
12011007SLori.Alt@Sun.COM return (0);
12111007SLori.Alt@Sun.COM } else {
12211007SLori.Alt@Sun.COM /* not a continuation. Push out pending record */
12311007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr,
12411007SLori.Alt@Sun.COM sizeof (dmu_replay_record_t)) != 0)
12511007SLori.Alt@Sun.COM return (EINTR);
12611007SLori.Alt@Sun.COM ba->pending_op = PENDING_NONE;
12711007SLori.Alt@Sun.COM }
12811007SLori.Alt@Sun.COM }
12911007SLori.Alt@Sun.COM /* create a FREE record and make it pending */
1302743Sahrens bzero(ba->drr, sizeof (dmu_replay_record_t));
1312743Sahrens ba->drr->drr_type = DRR_FREE;
13211007SLori.Alt@Sun.COM drrf->drr_object = object;
13311007SLori.Alt@Sun.COM drrf->drr_offset = offset;
13411007SLori.Alt@Sun.COM drrf->drr_length = length;
13511007SLori.Alt@Sun.COM drrf->drr_toguid = ba->toguid;
13611007SLori.Alt@Sun.COM if (length == -1ULL) {
13711007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0)
13811007SLori.Alt@Sun.COM return (EINTR);
13911007SLori.Alt@Sun.COM } else {
14011007SLori.Alt@Sun.COM ba->pending_op = PENDING_FREE;
14111007SLori.Alt@Sun.COM }
1422743Sahrens
1432743Sahrens return (0);
1442743Sahrens }
1452743Sahrens
1462743Sahrens static int
dump_data(struct backuparg * ba,dmu_object_type_t type,uint64_t object,uint64_t offset,int blksz,const blkptr_t * bp,void * data)1472743Sahrens dump_data(struct backuparg *ba, dmu_object_type_t type,
14811381SLori.Alt@Sun.COM uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
1492743Sahrens {
15011007SLori.Alt@Sun.COM struct drr_write *drrw = &(ba->drr->drr_u.drr_write);
15111007SLori.Alt@Sun.COM
15211381SLori.Alt@Sun.COM
15311007SLori.Alt@Sun.COM /*
15411007SLori.Alt@Sun.COM * If there is any kind of pending aggregation (currently either
15511007SLori.Alt@Sun.COM * a grouping of free objects or free blocks), push it out to
15611007SLori.Alt@Sun.COM * the stream, since aggregation can't be done across operations
15711007SLori.Alt@Sun.COM * of different types.
15811007SLori.Alt@Sun.COM */
15911007SLori.Alt@Sun.COM if (ba->pending_op != PENDING_NONE) {
16011007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0)
16111007SLori.Alt@Sun.COM return (EINTR);
16211007SLori.Alt@Sun.COM ba->pending_op = PENDING_NONE;
16311007SLori.Alt@Sun.COM }
1642743Sahrens /* write a DATA record */
1652743Sahrens bzero(ba->drr, sizeof (dmu_replay_record_t));
1662743Sahrens ba->drr->drr_type = DRR_WRITE;
16711007SLori.Alt@Sun.COM drrw->drr_object = object;
16811007SLori.Alt@Sun.COM drrw->drr_type = type;
16911007SLori.Alt@Sun.COM drrw->drr_offset = offset;
17011007SLori.Alt@Sun.COM drrw->drr_length = blksz;
17111007SLori.Alt@Sun.COM drrw->drr_toguid = ba->toguid;
17211381SLori.Alt@Sun.COM drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
17311381SLori.Alt@Sun.COM if (zio_checksum_table[drrw->drr_checksumtype].ci_dedup)
17411381SLori.Alt@Sun.COM drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
17511381SLori.Alt@Sun.COM DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
17611381SLori.Alt@Sun.COM DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
17711381SLori.Alt@Sun.COM DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
17811381SLori.Alt@Sun.COM drrw->drr_key.ddk_cksum = bp->blk_cksum;
1792743Sahrens
18011007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0)
1812743Sahrens return (EINTR);
18211007SLori.Alt@Sun.COM if (dump_bytes(ba, data, blksz) != 0)
1832743Sahrens return (EINTR);
1842743Sahrens return (0);
1852743Sahrens }
1862743Sahrens
1872743Sahrens static int
dump_spill(struct backuparg * ba,uint64_t object,int blksz,void * data)18811935SMark.Shellenbaum@Sun.COM dump_spill(struct backuparg *ba, uint64_t object, int blksz, void *data)
18911935SMark.Shellenbaum@Sun.COM {
19011935SMark.Shellenbaum@Sun.COM struct drr_spill *drrs = &(ba->drr->drr_u.drr_spill);
19111935SMark.Shellenbaum@Sun.COM
19211935SMark.Shellenbaum@Sun.COM if (ba->pending_op != PENDING_NONE) {
19311935SMark.Shellenbaum@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0)
19411935SMark.Shellenbaum@Sun.COM return (EINTR);
19511935SMark.Shellenbaum@Sun.COM ba->pending_op = PENDING_NONE;
19611935SMark.Shellenbaum@Sun.COM }
19711935SMark.Shellenbaum@Sun.COM
19811935SMark.Shellenbaum@Sun.COM /* write a SPILL record */
19911935SMark.Shellenbaum@Sun.COM bzero(ba->drr, sizeof (dmu_replay_record_t));
20011935SMark.Shellenbaum@Sun.COM ba->drr->drr_type = DRR_SPILL;
20111935SMark.Shellenbaum@Sun.COM drrs->drr_object = object;
20211935SMark.Shellenbaum@Sun.COM drrs->drr_length = blksz;
20311935SMark.Shellenbaum@Sun.COM drrs->drr_toguid = ba->toguid;
20411935SMark.Shellenbaum@Sun.COM
20511935SMark.Shellenbaum@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
20611935SMark.Shellenbaum@Sun.COM return (EINTR);
20711935SMark.Shellenbaum@Sun.COM if (dump_bytes(ba, data, blksz))
20811935SMark.Shellenbaum@Sun.COM return (EINTR);
20911935SMark.Shellenbaum@Sun.COM return (0);
21011935SMark.Shellenbaum@Sun.COM }
21111935SMark.Shellenbaum@Sun.COM
21211935SMark.Shellenbaum@Sun.COM static int
dump_freeobjects(struct backuparg * ba,uint64_t firstobj,uint64_t numobjs)2132743Sahrens dump_freeobjects(struct backuparg *ba, uint64_t firstobj, uint64_t numobjs)
2142743Sahrens {
21511007SLori.Alt@Sun.COM struct drr_freeobjects *drrfo = &(ba->drr->drr_u.drr_freeobjects);
21611007SLori.Alt@Sun.COM
21711007SLori.Alt@Sun.COM /*
21811007SLori.Alt@Sun.COM * If there is a pending op, but it's not PENDING_FREEOBJECTS,
21911007SLori.Alt@Sun.COM * push it out, since free block aggregation can only be done for
22011007SLori.Alt@Sun.COM * blocks of the same type (i.e., DRR_FREE records can only be
22111007SLori.Alt@Sun.COM * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
22211007SLori.Alt@Sun.COM * can only be aggregated with other DRR_FREEOBJECTS records.
22311007SLori.Alt@Sun.COM */
22411007SLori.Alt@Sun.COM if (ba->pending_op != PENDING_NONE &&
22511007SLori.Alt@Sun.COM ba->pending_op != PENDING_FREEOBJECTS) {
22611007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0)
22711007SLori.Alt@Sun.COM return (EINTR);
22811007SLori.Alt@Sun.COM ba->pending_op = PENDING_NONE;
22911007SLori.Alt@Sun.COM }
23011007SLori.Alt@Sun.COM if (ba->pending_op == PENDING_FREEOBJECTS) {
23111007SLori.Alt@Sun.COM /*
23211007SLori.Alt@Sun.COM * See whether this free object array can be aggregated
23311007SLori.Alt@Sun.COM * with pending one
23411007SLori.Alt@Sun.COM */
23511007SLori.Alt@Sun.COM if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
23611007SLori.Alt@Sun.COM drrfo->drr_numobjs += numobjs;
23711007SLori.Alt@Sun.COM return (0);
23811007SLori.Alt@Sun.COM } else {
23911007SLori.Alt@Sun.COM /* can't be aggregated. Push out pending record */
24011007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr,
24111007SLori.Alt@Sun.COM sizeof (dmu_replay_record_t)) != 0)
24211007SLori.Alt@Sun.COM return (EINTR);
24311007SLori.Alt@Sun.COM ba->pending_op = PENDING_NONE;
24411007SLori.Alt@Sun.COM }
24511007SLori.Alt@Sun.COM }
24611007SLori.Alt@Sun.COM
2472743Sahrens /* write a FREEOBJECTS record */
2482743Sahrens bzero(ba->drr, sizeof (dmu_replay_record_t));
2492743Sahrens ba->drr->drr_type = DRR_FREEOBJECTS;
25011007SLori.Alt@Sun.COM drrfo->drr_firstobj = firstobj;
25111007SLori.Alt@Sun.COM drrfo->drr_numobjs = numobjs;
25211007SLori.Alt@Sun.COM drrfo->drr_toguid = ba->toguid;
2532743Sahrens
25411007SLori.Alt@Sun.COM ba->pending_op = PENDING_FREEOBJECTS;
25511007SLori.Alt@Sun.COM
2562743Sahrens return (0);
2572743Sahrens }
2582743Sahrens
2592743Sahrens static int
dump_dnode(struct backuparg * ba,uint64_t object,dnode_phys_t * dnp)2602743Sahrens dump_dnode(struct backuparg *ba, uint64_t object, dnode_phys_t *dnp)
2612743Sahrens {
26211007SLori.Alt@Sun.COM struct drr_object *drro = &(ba->drr->drr_u.drr_object);
26311007SLori.Alt@Sun.COM
2642743Sahrens if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
2652743Sahrens return (dump_freeobjects(ba, object, 1));
2662743Sahrens
26711007SLori.Alt@Sun.COM if (ba->pending_op != PENDING_NONE) {
26811007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0)
26911007SLori.Alt@Sun.COM return (EINTR);
27011007SLori.Alt@Sun.COM ba->pending_op = PENDING_NONE;
27111007SLori.Alt@Sun.COM }
27211007SLori.Alt@Sun.COM
2732743Sahrens /* write an OBJECT record */
2742743Sahrens bzero(ba->drr, sizeof (dmu_replay_record_t));
2752743Sahrens ba->drr->drr_type = DRR_OBJECT;
27611007SLori.Alt@Sun.COM drro->drr_object = object;
27711007SLori.Alt@Sun.COM drro->drr_type = dnp->dn_type;
27811007SLori.Alt@Sun.COM drro->drr_bonustype = dnp->dn_bonustype;
27911007SLori.Alt@Sun.COM drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
28011007SLori.Alt@Sun.COM drro->drr_bonuslen = dnp->dn_bonuslen;
28111007SLori.Alt@Sun.COM drro->drr_checksumtype = dnp->dn_checksum;
28211007SLori.Alt@Sun.COM drro->drr_compress = dnp->dn_compress;
28311007SLori.Alt@Sun.COM drro->drr_toguid = ba->toguid;
2842743Sahrens
28511007SLori.Alt@Sun.COM if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)) != 0)
2862743Sahrens return (EINTR);
2872743Sahrens
28811007SLori.Alt@Sun.COM if (dump_bytes(ba, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0)
2892743Sahrens return (EINTR);
2902743Sahrens
2912743Sahrens /* free anything past the end of the file */
2922743Sahrens if (dump_free(ba, object, (dnp->dn_maxblkid + 1) *
2932743Sahrens (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL))
2942743Sahrens return (EINTR);
2952743Sahrens if (ba->err)
2962743Sahrens return (EINTR);
2972743Sahrens return (0);
2982743Sahrens }
2992743Sahrens
3002743Sahrens #define BP_SPAN(dnp, level) \
3012743Sahrens (((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
3022743Sahrens (level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
3032743Sahrens
30410922SJeff.Bonwick@Sun.COM /* ARGSUSED */
3052743Sahrens static int
backup_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,arc_buf_t * pbuf,const zbookmark_t * zb,const dnode_phys_t * dnp,void * arg)30612296SLin.Ling@Sun.COM backup_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp, arc_buf_t *pbuf,
30710922SJeff.Bonwick@Sun.COM const zbookmark_t *zb, const dnode_phys_t *dnp, void *arg)
3082743Sahrens {
3092743Sahrens struct backuparg *ba = arg;
3102743Sahrens dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
3112743Sahrens int err = 0;
3122743Sahrens
3132743Sahrens if (issig(JUSTLOOKING) && issig(FORREAL))
3142743Sahrens return (EINTR);
3152743Sahrens
31610922SJeff.Bonwick@Sun.COM if (zb->zb_object != DMU_META_DNODE_OBJECT &&
31710922SJeff.Bonwick@Sun.COM DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
3189396SMatthew.Ahrens@Sun.COM return (0);
31910922SJeff.Bonwick@Sun.COM } else if (bp == NULL && zb->zb_object == DMU_META_DNODE_OBJECT) {
3207837SMatthew.Ahrens@Sun.COM uint64_t span = BP_SPAN(dnp, zb->zb_level);
3217837SMatthew.Ahrens@Sun.COM uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
3222743Sahrens err = dump_freeobjects(ba, dnobj, span >> DNODE_SHIFT);
3232743Sahrens } else if (bp == NULL) {
3247837SMatthew.Ahrens@Sun.COM uint64_t span = BP_SPAN(dnp, zb->zb_level);
3257837SMatthew.Ahrens@Sun.COM err = dump_free(ba, zb->zb_object, zb->zb_blkid * span, span);
3267837SMatthew.Ahrens@Sun.COM } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
3277837SMatthew.Ahrens@Sun.COM return (0);
3287837SMatthew.Ahrens@Sun.COM } else if (type == DMU_OT_DNODE) {
3297837SMatthew.Ahrens@Sun.COM dnode_phys_t *blk;
3302743Sahrens int i;
3312743Sahrens int blksz = BP_GET_LSIZE(bp);
3327837SMatthew.Ahrens@Sun.COM uint32_t aflags = ARC_WAIT;
3337837SMatthew.Ahrens@Sun.COM arc_buf_t *abuf;
3342743Sahrens
33512296SLin.Ling@Sun.COM if (dsl_read(NULL, spa, bp, pbuf,
3367837SMatthew.Ahrens@Sun.COM arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
3377837SMatthew.Ahrens@Sun.COM ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
3387837SMatthew.Ahrens@Sun.COM return (EIO);
3397837SMatthew.Ahrens@Sun.COM
3407837SMatthew.Ahrens@Sun.COM blk = abuf->b_data;
3412743Sahrens for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
3427837SMatthew.Ahrens@Sun.COM uint64_t dnobj = (zb->zb_blkid <<
3437837SMatthew.Ahrens@Sun.COM (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
3442743Sahrens err = dump_dnode(ba, dnobj, blk+i);
3452743Sahrens if (err)
3462743Sahrens break;
3472743Sahrens }
3487837SMatthew.Ahrens@Sun.COM (void) arc_buf_remove_ref(abuf, &abuf);
34911935SMark.Shellenbaum@Sun.COM } else if (type == DMU_OT_SA) {
35011935SMark.Shellenbaum@Sun.COM uint32_t aflags = ARC_WAIT;
35111935SMark.Shellenbaum@Sun.COM arc_buf_t *abuf;
35211935SMark.Shellenbaum@Sun.COM int blksz = BP_GET_LSIZE(bp);
35311935SMark.Shellenbaum@Sun.COM
35411935SMark.Shellenbaum@Sun.COM if (arc_read_nolock(NULL, spa, bp,
35511935SMark.Shellenbaum@Sun.COM arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
35611935SMark.Shellenbaum@Sun.COM ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
35711935SMark.Shellenbaum@Sun.COM return (EIO);
35811935SMark.Shellenbaum@Sun.COM
35911935SMark.Shellenbaum@Sun.COM err = dump_spill(ba, zb->zb_object, blksz, abuf->b_data);
36011935SMark.Shellenbaum@Sun.COM (void) arc_buf_remove_ref(abuf, &abuf);
3617837SMatthew.Ahrens@Sun.COM } else { /* it's a level-0 block of a regular object */
3627837SMatthew.Ahrens@Sun.COM uint32_t aflags = ARC_WAIT;
3637837SMatthew.Ahrens@Sun.COM arc_buf_t *abuf;
3642743Sahrens int blksz = BP_GET_LSIZE(bp);
3652743Sahrens
36612296SLin.Ling@Sun.COM if (dsl_read(NULL, spa, bp, pbuf,
3677837SMatthew.Ahrens@Sun.COM arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
3687837SMatthew.Ahrens@Sun.COM ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
3697837SMatthew.Ahrens@Sun.COM return (EIO);
3702743Sahrens
3717837SMatthew.Ahrens@Sun.COM err = dump_data(ba, type, zb->zb_object, zb->zb_blkid * blksz,
37211381SLori.Alt@Sun.COM blksz, bp, abuf->b_data);
3737837SMatthew.Ahrens@Sun.COM (void) arc_buf_remove_ref(abuf, &abuf);
3742743Sahrens }
3752743Sahrens
3762743Sahrens ASSERT(err == 0 || err == EINTR);
3772743Sahrens return (err);
3782743Sahrens }
3792743Sahrens
3802743Sahrens int
dmu_sendbackup(objset_t * tosnap,objset_t * fromsnap,boolean_t fromorigin,vnode_t * vp,offset_t * off)3815367Sahrens dmu_sendbackup(objset_t *tosnap, objset_t *fromsnap, boolean_t fromorigin,
3825367Sahrens vnode_t *vp, offset_t *off)
3832743Sahrens {
38410298SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds = tosnap->os_dsl_dataset;
38510298SMatthew.Ahrens@Sun.COM dsl_dataset_t *fromds = fromsnap ? fromsnap->os_dsl_dataset : NULL;
3862743Sahrens dmu_replay_record_t *drr;
3872743Sahrens struct backuparg ba;
3882743Sahrens int err;
3895367Sahrens uint64_t fromtxg = 0;
3902743Sahrens
3912743Sahrens /* tosnap must be a snapshot */
3922743Sahrens if (ds->ds_phys->ds_next_snap_obj == 0)
3932743Sahrens return (EINVAL);
3942743Sahrens
3952743Sahrens /* fromsnap must be an earlier snapshot from the same fs as tosnap */
3962743Sahrens if (fromds && (ds->ds_dir != fromds->ds_dir ||
3975367Sahrens fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg))
3982743Sahrens return (EXDEV);
3992743Sahrens
4005367Sahrens if (fromorigin) {
4017046Sahrens dsl_pool_t *dp = ds->ds_dir->dd_pool;
4027046Sahrens
4035367Sahrens if (fromsnap)
4045367Sahrens return (EINVAL);
4055367Sahrens
4067046Sahrens if (dsl_dir_is_clone(ds->ds_dir)) {
4075367Sahrens rw_enter(&dp->dp_config_rwlock, RW_READER);
4086689Smaybee err = dsl_dataset_hold_obj(dp,
4096689Smaybee ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &fromds);
4105367Sahrens rw_exit(&dp->dp_config_rwlock);
4115367Sahrens if (err)
4125367Sahrens return (err);
4135367Sahrens } else {
4145367Sahrens fromorigin = B_FALSE;
4155367Sahrens }
4165367Sahrens }
4175367Sahrens
4185367Sahrens
4192743Sahrens drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
4202743Sahrens drr->drr_type = DRR_BEGIN;
4212743Sahrens drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
42211007SLori.Alt@Sun.COM DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
42311007SLori.Alt@Sun.COM DMU_SUBSTREAM);
42412070SMark.Shellenbaum@Sun.COM
42512070SMark.Shellenbaum@Sun.COM #ifdef _KERNEL
42612070SMark.Shellenbaum@Sun.COM if (dmu_objset_type(tosnap) == DMU_OST_ZFS) {
42712070SMark.Shellenbaum@Sun.COM uint64_t version;
42812070SMark.Shellenbaum@Sun.COM if (zfs_get_zplprop(tosnap, ZFS_PROP_VERSION, &version) != 0)
42912070SMark.Shellenbaum@Sun.COM return (EINVAL);
43012070SMark.Shellenbaum@Sun.COM if (version == ZPL_VERSION_SA) {
43112070SMark.Shellenbaum@Sun.COM DMU_SET_FEATUREFLAGS(
43212070SMark.Shellenbaum@Sun.COM drr->drr_u.drr_begin.drr_versioninfo,
43312070SMark.Shellenbaum@Sun.COM DMU_BACKUP_FEATURE_SA_SPILL);
43412070SMark.Shellenbaum@Sun.COM }
43512070SMark.Shellenbaum@Sun.COM }
43612070SMark.Shellenbaum@Sun.COM #endif
43712070SMark.Shellenbaum@Sun.COM
4382743Sahrens drr->drr_u.drr_begin.drr_creation_time =
4392743Sahrens ds->ds_phys->ds_creation_time;
44010298SMatthew.Ahrens@Sun.COM drr->drr_u.drr_begin.drr_type = tosnap->os_phys->os_type;
4415367Sahrens if (fromorigin)
4425367Sahrens drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
4432743Sahrens drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
4446492Stimh if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
4456492Stimh drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
4466492Stimh
4472743Sahrens if (fromds)
4482743Sahrens drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid;
4492743Sahrens dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
4502743Sahrens
4515367Sahrens if (fromds)
4525367Sahrens fromtxg = fromds->ds_phys->ds_creation_txg;
4535367Sahrens if (fromorigin)
4546689Smaybee dsl_dataset_rele(fromds, FTAG);
4555367Sahrens
4562743Sahrens ba.drr = drr;
4572743Sahrens ba.vp = vp;
4582743Sahrens ba.os = tosnap;
4595367Sahrens ba.off = off;
46011007SLori.Alt@Sun.COM ba.toguid = ds->ds_phys->ds_guid;
4612743Sahrens ZIO_SET_CHECKSUM(&ba.zc, 0, 0, 0, 0);
46211007SLori.Alt@Sun.COM ba.pending_op = PENDING_NONE;
4632743Sahrens
46411007SLori.Alt@Sun.COM if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t)) != 0) {
4652743Sahrens kmem_free(drr, sizeof (dmu_replay_record_t));
4662743Sahrens return (ba.err);
4672743Sahrens }
4682743Sahrens
4697837SMatthew.Ahrens@Sun.COM err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH,
4702743Sahrens backup_cb, &ba);
4712743Sahrens
47211007SLori.Alt@Sun.COM if (ba.pending_op != PENDING_NONE)
47311007SLori.Alt@Sun.COM if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t)) != 0)
47411007SLori.Alt@Sun.COM err = EINTR;
47511007SLori.Alt@Sun.COM
4762743Sahrens if (err) {
4772743Sahrens if (err == EINTR && ba.err)
4782743Sahrens err = ba.err;
4793655Sgw25295 kmem_free(drr, sizeof (dmu_replay_record_t));
4802743Sahrens return (err);
4812743Sahrens }
4822743Sahrens
4832743Sahrens bzero(drr, sizeof (dmu_replay_record_t));
4842743Sahrens drr->drr_type = DRR_END;
4852743Sahrens drr->drr_u.drr_end.drr_checksum = ba.zc;
48611007SLori.Alt@Sun.COM drr->drr_u.drr_end.drr_toguid = ba.toguid;
4872743Sahrens
48811007SLori.Alt@Sun.COM if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t)) != 0) {
4893655Sgw25295 kmem_free(drr, sizeof (dmu_replay_record_t));
4902743Sahrens return (ba.err);
4913655Sgw25295 }
4922743Sahrens
4932743Sahrens kmem_free(drr, sizeof (dmu_replay_record_t));
4942743Sahrens
4952743Sahrens return (0);
4962743Sahrens }
4972743Sahrens
4985367Sahrens struct recvbeginsyncarg {
4995367Sahrens const char *tofs;
5005367Sahrens const char *tosnap;
5015367Sahrens dsl_dataset_t *origin;
5025367Sahrens uint64_t fromguid;
5035367Sahrens dmu_objset_type_t type;
5045367Sahrens void *tag;
5055367Sahrens boolean_t force;
5066492Stimh uint64_t dsflags;
5075367Sahrens char clonelastname[MAXNAMELEN];
5085367Sahrens dsl_dataset_t *ds; /* the ds to recv into; returned from the syncfunc */
50912296SLin.Ling@Sun.COM cred_t *cr;
5102743Sahrens };
5112743Sahrens
5125367Sahrens /* ARGSUSED */
5132743Sahrens static int
recv_new_check(void * arg1,void * arg2,dmu_tx_t * tx)51410272SMatthew.Ahrens@Sun.COM recv_new_check(void *arg1, void *arg2, dmu_tx_t *tx)
5155367Sahrens {
5165367Sahrens dsl_dir_t *dd = arg1;
5175367Sahrens struct recvbeginsyncarg *rbsa = arg2;
5185367Sahrens objset_t *mos = dd->dd_pool->dp_meta_objset;
5195367Sahrens uint64_t val;
5205367Sahrens int err;
5215367Sahrens
5225367Sahrens err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
5235367Sahrens strrchr(rbsa->tofs, '/') + 1, sizeof (uint64_t), 1, &val);
5245367Sahrens
5255367Sahrens if (err != ENOENT)
5265367Sahrens return (err ? err : EEXIST);
5275367Sahrens
5285367Sahrens if (rbsa->origin) {
5295367Sahrens /* make sure it's a snap in the same pool */
5305367Sahrens if (rbsa->origin->ds_dir->dd_pool != dd->dd_pool)
5315367Sahrens return (EXDEV);
53210272SMatthew.Ahrens@Sun.COM if (!dsl_dataset_is_snapshot(rbsa->origin))
5335367Sahrens return (EINVAL);
5345367Sahrens if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid)
5355367Sahrens return (ENODEV);
5365367Sahrens }
5375367Sahrens
5385367Sahrens return (0);
5395367Sahrens }
5405367Sahrens
5415367Sahrens static void
recv_new_sync(void * arg1,void * arg2,dmu_tx_t * tx)54212296SLin.Ling@Sun.COM recv_new_sync(void *arg1, void *arg2, dmu_tx_t *tx)
5435367Sahrens {
5445367Sahrens dsl_dir_t *dd = arg1;
5455367Sahrens struct recvbeginsyncarg *rbsa = arg2;
5466689Smaybee uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
5475367Sahrens uint64_t dsobj;
5485367Sahrens
54910272SMatthew.Ahrens@Sun.COM /* Create and open new dataset. */
5505367Sahrens dsobj = dsl_dataset_create_sync(dd, strrchr(rbsa->tofs, '/') + 1,
55112296SLin.Ling@Sun.COM rbsa->origin, flags, rbsa->cr, tx);
55210272SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_own_obj(dd->dd_pool, dsobj,
55310298SMatthew.Ahrens@Sun.COM B_TRUE, dmu_recv_tag, &rbsa->ds));
5545367Sahrens
55510272SMatthew.Ahrens@Sun.COM if (rbsa->origin == NULL) {
55610272SMatthew.Ahrens@Sun.COM (void) dmu_objset_create_impl(dd->dd_pool->dp_spa,
55710272SMatthew.Ahrens@Sun.COM rbsa->ds, &rbsa->ds->ds_phys->ds_bp, rbsa->type, tx);
5585367Sahrens }
5595367Sahrens
56012296SLin.Ling@Sun.COM spa_history_log_internal(LOG_DS_REPLAY_FULL_SYNC,
56112296SLin.Ling@Sun.COM dd->dd_pool->dp_spa, tx, "dataset = %lld", dsobj);
5625367Sahrens }
5635367Sahrens
5645367Sahrens /* ARGSUSED */
5655367Sahrens static int
recv_existing_check(void * arg1,void * arg2,dmu_tx_t * tx)56610272SMatthew.Ahrens@Sun.COM recv_existing_check(void *arg1, void *arg2, dmu_tx_t *tx)
5675367Sahrens {
5685367Sahrens dsl_dataset_t *ds = arg1;
5695367Sahrens struct recvbeginsyncarg *rbsa = arg2;
5702743Sahrens int err;
5712743Sahrens uint64_t val;
5722743Sahrens
5735367Sahrens /* must not have any changes since most recent snapshot */
5745367Sahrens if (!rbsa->force && dsl_dataset_modified_since_lastsnap(ds))
5755367Sahrens return (ETXTBSY);
5765367Sahrens
577*12970SMark.Maybee@Sun.COM /* new snapshot name must not exist */
578*12970SMark.Maybee@Sun.COM err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
579*12970SMark.Maybee@Sun.COM ds->ds_phys->ds_snapnames_zapobj, rbsa->tosnap, 8, 1, &val);
580*12970SMark.Maybee@Sun.COM if (err == 0)
581*12970SMark.Maybee@Sun.COM return (EEXIST);
582*12970SMark.Maybee@Sun.COM if (err != ENOENT)
583*12970SMark.Maybee@Sun.COM return (err);
584*12970SMark.Maybee@Sun.COM
58510272SMatthew.Ahrens@Sun.COM if (rbsa->fromguid) {
58610272SMatthew.Ahrens@Sun.COM /* if incremental, most recent snapshot must match fromguid */
58710272SMatthew.Ahrens@Sun.COM if (ds->ds_prev == NULL)
58810272SMatthew.Ahrens@Sun.COM return (ENODEV);
58911022STom.Erickson@Sun.COM
59011022STom.Erickson@Sun.COM /*
59111022STom.Erickson@Sun.COM * most recent snapshot must match fromguid, or there are no
59211022STom.Erickson@Sun.COM * changes since the fromguid one
59311022STom.Erickson@Sun.COM */
59411022STom.Erickson@Sun.COM if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid) {
59511022STom.Erickson@Sun.COM uint64_t birth = ds->ds_prev->ds_phys->ds_bp.blk_birth;
59611022STom.Erickson@Sun.COM uint64_t obj = ds->ds_prev->ds_phys->ds_prev_snap_obj;
59711022STom.Erickson@Sun.COM while (obj != 0) {
59811022STom.Erickson@Sun.COM dsl_dataset_t *snap;
59911022STom.Erickson@Sun.COM err = dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
60011022STom.Erickson@Sun.COM obj, FTAG, &snap);
60111022STom.Erickson@Sun.COM if (err)
60211022STom.Erickson@Sun.COM return (ENODEV);
60311022STom.Erickson@Sun.COM if (snap->ds_phys->ds_creation_txg < birth) {
60411022STom.Erickson@Sun.COM dsl_dataset_rele(snap, FTAG);
60511022STom.Erickson@Sun.COM return (ENODEV);
60611022STom.Erickson@Sun.COM }
60711022STom.Erickson@Sun.COM if (snap->ds_phys->ds_guid == rbsa->fromguid) {
60811022STom.Erickson@Sun.COM dsl_dataset_rele(snap, FTAG);
60911022STom.Erickson@Sun.COM break; /* it's ok */
61011022STom.Erickson@Sun.COM }
61111022STom.Erickson@Sun.COM obj = snap->ds_phys->ds_prev_snap_obj;
61211022STom.Erickson@Sun.COM dsl_dataset_rele(snap, FTAG);
61311022STom.Erickson@Sun.COM }
61411022STom.Erickson@Sun.COM if (obj == 0)
61511022STom.Erickson@Sun.COM return (ENODEV);
61611022STom.Erickson@Sun.COM }
61710272SMatthew.Ahrens@Sun.COM } else {
61810272SMatthew.Ahrens@Sun.COM /* if full, most recent snapshot must be $ORIGIN */
61910272SMatthew.Ahrens@Sun.COM if (ds->ds_phys->ds_prev_snap_txg >= TXG_INITIAL)
62010272SMatthew.Ahrens@Sun.COM return (ENODEV);
62110272SMatthew.Ahrens@Sun.COM }
6222743Sahrens
6236083Sek110237 /* temporary clone name must not exist */
6246083Sek110237 err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
6256083Sek110237 ds->ds_dir->dd_phys->dd_child_dir_zapobj,
6266083Sek110237 rbsa->clonelastname, 8, 1, &val);
6276083Sek110237 if (err == 0)
6286083Sek110237 return (EEXIST);
6296083Sek110237 if (err != ENOENT)
6306083Sek110237 return (err);
6316083Sek110237
6322743Sahrens return (0);
6332743Sahrens }
6342743Sahrens
6352743Sahrens /* ARGSUSED */
6365367Sahrens static void
recv_existing_sync(void * arg1,void * arg2,dmu_tx_t * tx)63712296SLin.Ling@Sun.COM recv_existing_sync(void *arg1, void *arg2, dmu_tx_t *tx)
6385326Sek110237 {
6395367Sahrens dsl_dataset_t *ohds = arg1;
6405367Sahrens struct recvbeginsyncarg *rbsa = arg2;
6415367Sahrens dsl_pool_t *dp = ohds->ds_dir->dd_pool;
64210272SMatthew.Ahrens@Sun.COM dsl_dataset_t *cds;
6436689Smaybee uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
6445367Sahrens uint64_t dsobj;
6455326Sek110237
64610272SMatthew.Ahrens@Sun.COM /* create and open the temporary clone */
64710272SMatthew.Ahrens@Sun.COM dsobj = dsl_dataset_create_sync(ohds->ds_dir, rbsa->clonelastname,
64812296SLin.Ling@Sun.COM ohds->ds_prev, flags, rbsa->cr, tx);
64910298SMatthew.Ahrens@Sun.COM VERIFY(0 == dsl_dataset_own_obj(dp, dsobj, B_TRUE, dmu_recv_tag, &cds));
6505367Sahrens
65110272SMatthew.Ahrens@Sun.COM /*
65210272SMatthew.Ahrens@Sun.COM * If we actually created a non-clone, we need to create the
65310272SMatthew.Ahrens@Sun.COM * objset in our new dataset.
65410272SMatthew.Ahrens@Sun.COM */
65510272SMatthew.Ahrens@Sun.COM if (BP_IS_HOLE(dsl_dataset_get_blkptr(cds))) {
65610272SMatthew.Ahrens@Sun.COM (void) dmu_objset_create_impl(dp->dp_spa,
65710272SMatthew.Ahrens@Sun.COM cds, dsl_dataset_get_blkptr(cds), rbsa->type, tx);
65810272SMatthew.Ahrens@Sun.COM }
65910272SMatthew.Ahrens@Sun.COM
6605367Sahrens rbsa->ds = cds;
6615367Sahrens
66212296SLin.Ling@Sun.COM spa_history_log_internal(LOG_DS_REPLAY_INC_SYNC,
66312296SLin.Ling@Sun.COM dp->dp_spa, tx, "dataset = %lld", dsobj);
6645326Sek110237 }
6655326Sek110237
66612070SMark.Shellenbaum@Sun.COM static boolean_t
dmu_recv_verify_features(dsl_dataset_t * ds,struct drr_begin * drrb)66712070SMark.Shellenbaum@Sun.COM dmu_recv_verify_features(dsl_dataset_t *ds, struct drr_begin *drrb)
66812070SMark.Shellenbaum@Sun.COM {
66912070SMark.Shellenbaum@Sun.COM int featureflags;
67012070SMark.Shellenbaum@Sun.COM
67112070SMark.Shellenbaum@Sun.COM featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
67212070SMark.Shellenbaum@Sun.COM
67312070SMark.Shellenbaum@Sun.COM /* Verify pool version supports SA if SA_SPILL feature set */
67412070SMark.Shellenbaum@Sun.COM return ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
67512070SMark.Shellenbaum@Sun.COM (spa_version(dsl_dataset_get_spa(ds)) < SPA_VERSION_SA));
67612070SMark.Shellenbaum@Sun.COM }
67712070SMark.Shellenbaum@Sun.COM
6785367Sahrens /*
6795367Sahrens * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
6805367Sahrens * succeeds; otherwise we will leak the holds on the datasets.
6815367Sahrens */
6825367Sahrens int
dmu_recv_begin(char * tofs,char * tosnap,char * top_ds,struct drr_begin * drrb,boolean_t force,objset_t * origin,dmu_recv_cookie_t * drc)68311007SLori.Alt@Sun.COM dmu_recv_begin(char *tofs, char *tosnap, char *top_ds, struct drr_begin *drrb,
68410204SMatthew.Ahrens@Sun.COM boolean_t force, objset_t *origin, dmu_recv_cookie_t *drc)
6852743Sahrens {
6865367Sahrens int err = 0;
6875367Sahrens boolean_t byteswap;
68810272SMatthew.Ahrens@Sun.COM struct recvbeginsyncarg rbsa = { 0 };
68911007SLori.Alt@Sun.COM uint64_t versioninfo;
6905367Sahrens int flags;
6915367Sahrens dsl_dataset_t *ds;
6925367Sahrens
6935367Sahrens if (drrb->drr_magic == DMU_BACKUP_MAGIC)
6945367Sahrens byteswap = FALSE;
6955367Sahrens else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
6965367Sahrens byteswap = TRUE;
6975367Sahrens else
6985367Sahrens return (EINVAL);
6995367Sahrens
7005367Sahrens rbsa.tofs = tofs;
7015367Sahrens rbsa.tosnap = tosnap;
70210298SMatthew.Ahrens@Sun.COM rbsa.origin = origin ? origin->os_dsl_dataset : NULL;
7035367Sahrens rbsa.fromguid = drrb->drr_fromguid;
7045367Sahrens rbsa.type = drrb->drr_type;
7055367Sahrens rbsa.tag = FTAG;
7066492Stimh rbsa.dsflags = 0;
70712296SLin.Ling@Sun.COM rbsa.cr = CRED();
70811007SLori.Alt@Sun.COM versioninfo = drrb->drr_versioninfo;
7095367Sahrens flags = drrb->drr_flags;
7105367Sahrens
7115367Sahrens if (byteswap) {
7125367Sahrens rbsa.type = BSWAP_32(rbsa.type);
7135367Sahrens rbsa.fromguid = BSWAP_64(rbsa.fromguid);
71411007SLori.Alt@Sun.COM versioninfo = BSWAP_64(versioninfo);
7155367Sahrens flags = BSWAP_32(flags);
7165367Sahrens }
7175367Sahrens
71811007SLori.Alt@Sun.COM if (DMU_GET_STREAM_HDRTYPE(versioninfo) == DMU_COMPOUNDSTREAM ||
7195367Sahrens rbsa.type >= DMU_OST_NUMTYPES ||
7205367Sahrens ((flags & DRR_FLAG_CLONE) && origin == NULL))
7215367Sahrens return (EINVAL);
7225367Sahrens
7236492Stimh if (flags & DRR_FLAG_CI_DATA)
7246492Stimh rbsa.dsflags = DS_FLAG_CI_DATASET;
7256492Stimh
7265367Sahrens bzero(drc, sizeof (dmu_recv_cookie_t));
7275367Sahrens drc->drc_drrb = drrb;
7285367Sahrens drc->drc_tosnap = tosnap;
72911007SLori.Alt@Sun.COM drc->drc_top_ds = top_ds;
7305367Sahrens drc->drc_force = force;
7315367Sahrens
7325367Sahrens /*
7335367Sahrens * Process the begin in syncing context.
7345367Sahrens */
73510272SMatthew.Ahrens@Sun.COM
73610272SMatthew.Ahrens@Sun.COM /* open the dataset we are logically receiving into */
73710272SMatthew.Ahrens@Sun.COM err = dsl_dataset_hold(tofs, dmu_recv_tag, &ds);
73810272SMatthew.Ahrens@Sun.COM if (err == 0) {
73912070SMark.Shellenbaum@Sun.COM if (dmu_recv_verify_features(ds, drrb)) {
74012070SMark.Shellenbaum@Sun.COM dsl_dataset_rele(ds, dmu_recv_tag);
74112070SMark.Shellenbaum@Sun.COM return (ENOTSUP);
74212070SMark.Shellenbaum@Sun.COM }
74310272SMatthew.Ahrens@Sun.COM /* target fs already exists; recv into temp clone */
7445367Sahrens
74510272SMatthew.Ahrens@Sun.COM /* Can't recv a clone into an existing fs */
74610272SMatthew.Ahrens@Sun.COM if (flags & DRR_FLAG_CLONE) {
74710272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, dmu_recv_tag);
74810272SMatthew.Ahrens@Sun.COM return (EINVAL);
74910272SMatthew.Ahrens@Sun.COM }
7502743Sahrens
75110204SMatthew.Ahrens@Sun.COM /* must not have an incremental recv already in progress */
75210204SMatthew.Ahrens@Sun.COM if (!mutex_tryenter(&ds->ds_recvlock)) {
75310204SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, dmu_recv_tag);
75410204SMatthew.Ahrens@Sun.COM return (EBUSY);
75510204SMatthew.Ahrens@Sun.COM }
75610204SMatthew.Ahrens@Sun.COM
75710272SMatthew.Ahrens@Sun.COM /* tmp clone name is: tofs/%tosnap" */
75810272SMatthew.Ahrens@Sun.COM (void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname),
75910272SMatthew.Ahrens@Sun.COM "%%%s", tosnap);
7605367Sahrens rbsa.force = force;
7615367Sahrens err = dsl_sync_task_do(ds->ds_dir->dd_pool,
76210272SMatthew.Ahrens@Sun.COM recv_existing_check, recv_existing_sync, ds, &rbsa, 5);
7635367Sahrens if (err) {
76410204SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_recvlock);
7656689Smaybee dsl_dataset_rele(ds, dmu_recv_tag);
7665367Sahrens return (err);
7675367Sahrens }
7685367Sahrens drc->drc_logical_ds = ds;
7695367Sahrens drc->drc_real_ds = rbsa.ds;
77010272SMatthew.Ahrens@Sun.COM } else if (err == ENOENT) {
77110272SMatthew.Ahrens@Sun.COM /* target fs does not exist; must be a full backup or clone */
77210272SMatthew.Ahrens@Sun.COM char *cp;
7735367Sahrens
77410272SMatthew.Ahrens@Sun.COM /*
77510272SMatthew.Ahrens@Sun.COM * If it's a non-clone incremental, we are missing the
77610272SMatthew.Ahrens@Sun.COM * target fs, so fail the recv.
77710272SMatthew.Ahrens@Sun.COM */
77810272SMatthew.Ahrens@Sun.COM if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE))
77910272SMatthew.Ahrens@Sun.COM return (ENOENT);
78010272SMatthew.Ahrens@Sun.COM
78110272SMatthew.Ahrens@Sun.COM /* Open the parent of tofs */
78210272SMatthew.Ahrens@Sun.COM cp = strrchr(tofs, '/');
78310272SMatthew.Ahrens@Sun.COM *cp = '\0';
78410819SChris.Kirby@sun.com err = dsl_dataset_hold(tofs, FTAG, &ds);
78510272SMatthew.Ahrens@Sun.COM *cp = '/';
7865367Sahrens if (err)
7875367Sahrens return (err);
7885367Sahrens
78912070SMark.Shellenbaum@Sun.COM if (dmu_recv_verify_features(ds, drrb)) {
79012727SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, FTAG);
79112070SMark.Shellenbaum@Sun.COM return (ENOTSUP);
79212070SMark.Shellenbaum@Sun.COM }
79312070SMark.Shellenbaum@Sun.COM
79410272SMatthew.Ahrens@Sun.COM err = dsl_sync_task_do(ds->ds_dir->dd_pool,
79510272SMatthew.Ahrens@Sun.COM recv_new_check, recv_new_sync, ds->ds_dir, &rbsa, 5);
79610819SChris.Kirby@sun.com dsl_dataset_rele(ds, FTAG);
7975367Sahrens if (err)
7985367Sahrens return (err);
7995367Sahrens drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds;
8005367Sahrens drc->drc_newfs = B_TRUE;
8015367Sahrens }
8025367Sahrens
80310272SMatthew.Ahrens@Sun.COM return (err);
8042743Sahrens }
8052743Sahrens
8065367Sahrens struct restorearg {
8075367Sahrens int err;
8085367Sahrens int byteswap;
8095367Sahrens vnode_t *vp;
8105367Sahrens char *buf;
8115367Sahrens uint64_t voff;
8125367Sahrens int bufsize; /* amount of memory allocated for buf */
8135367Sahrens zio_cksum_t cksum;
81412527SChris.Kirby@oracle.com avl_tree_t *guid_to_ds_map;
8155326Sek110237 };
8165326Sek110237
81711007SLori.Alt@Sun.COM typedef struct guid_map_entry {
81811007SLori.Alt@Sun.COM uint64_t guid;
81911007SLori.Alt@Sun.COM dsl_dataset_t *gme_ds;
82011007SLori.Alt@Sun.COM avl_node_t avlnode;
82111007SLori.Alt@Sun.COM } guid_map_entry_t;
82211007SLori.Alt@Sun.COM
82311007SLori.Alt@Sun.COM static int
guid_compare(const void * arg1,const void * arg2)82411007SLori.Alt@Sun.COM guid_compare(const void *arg1, const void *arg2)
82511007SLori.Alt@Sun.COM {
82611007SLori.Alt@Sun.COM const guid_map_entry_t *gmep1 = arg1;
82711007SLori.Alt@Sun.COM const guid_map_entry_t *gmep2 = arg2;
82811007SLori.Alt@Sun.COM
82911007SLori.Alt@Sun.COM if (gmep1->guid < gmep2->guid)
83011007SLori.Alt@Sun.COM return (-1);
83111007SLori.Alt@Sun.COM else if (gmep1->guid > gmep2->guid)
83211007SLori.Alt@Sun.COM return (1);
83311007SLori.Alt@Sun.COM return (0);
83411007SLori.Alt@Sun.COM }
83511007SLori.Alt@Sun.COM
83611007SLori.Alt@Sun.COM /*
83711007SLori.Alt@Sun.COM * This function is a callback used by dmu_objset_find() (which
83811007SLori.Alt@Sun.COM * enumerates the object sets) to build an avl tree that maps guids
83911007SLori.Alt@Sun.COM * to datasets. The resulting table is used when processing DRR_WRITE_BYREF
84011007SLori.Alt@Sun.COM * send stream records. These records, which are used in dedup'ed
84111007SLori.Alt@Sun.COM * streams, do not contain data themselves, but refer to a copy
84211007SLori.Alt@Sun.COM * of the data block that has already been written because it was
84311007SLori.Alt@Sun.COM * earlier in the stream. That previous copy is identified by the
84411007SLori.Alt@Sun.COM * guid of the dataset with the referenced data.
84511007SLori.Alt@Sun.COM */
84611007SLori.Alt@Sun.COM int
find_ds_by_guid(const char * name,void * arg)84711209SMatthew.Ahrens@Sun.COM find_ds_by_guid(const char *name, void *arg)
84811007SLori.Alt@Sun.COM {
84911209SMatthew.Ahrens@Sun.COM avl_tree_t *guid_map = arg;
85011007SLori.Alt@Sun.COM dsl_dataset_t *ds, *snapds;
85111007SLori.Alt@Sun.COM guid_map_entry_t *gmep;
85211209SMatthew.Ahrens@Sun.COM dsl_pool_t *dp;
85311007SLori.Alt@Sun.COM int err;
85411007SLori.Alt@Sun.COM uint64_t lastobj, firstobj;
85511007SLori.Alt@Sun.COM
85611007SLori.Alt@Sun.COM if (dsl_dataset_hold(name, FTAG, &ds) != 0)
85711007SLori.Alt@Sun.COM return (0);
85811007SLori.Alt@Sun.COM
85911007SLori.Alt@Sun.COM dp = ds->ds_dir->dd_pool;
86011007SLori.Alt@Sun.COM rw_enter(&dp->dp_config_rwlock, RW_READER);
86111007SLori.Alt@Sun.COM firstobj = ds->ds_dir->dd_phys->dd_origin_obj;
86211007SLori.Alt@Sun.COM lastobj = ds->ds_phys->ds_prev_snap_obj;
86311007SLori.Alt@Sun.COM
86411007SLori.Alt@Sun.COM while (lastobj != firstobj) {
86511007SLori.Alt@Sun.COM err = dsl_dataset_hold_obj(dp, lastobj, guid_map, &snapds);
86611007SLori.Alt@Sun.COM if (err) {
86711007SLori.Alt@Sun.COM /*
86811007SLori.Alt@Sun.COM * Skip this snapshot and move on. It's not
86911007SLori.Alt@Sun.COM * clear why this would ever happen, but the
87011007SLori.Alt@Sun.COM * remainder of the snapshot streadm can be
87111007SLori.Alt@Sun.COM * processed.
87211007SLori.Alt@Sun.COM */
87311007SLori.Alt@Sun.COM rw_exit(&dp->dp_config_rwlock);
87411007SLori.Alt@Sun.COM dsl_dataset_rele(ds, FTAG);
87511007SLori.Alt@Sun.COM return (0);
87611007SLori.Alt@Sun.COM }
87711007SLori.Alt@Sun.COM
87811007SLori.Alt@Sun.COM gmep = kmem_alloc(sizeof (guid_map_entry_t), KM_SLEEP);
87911007SLori.Alt@Sun.COM gmep->guid = snapds->ds_phys->ds_guid;
88011007SLori.Alt@Sun.COM gmep->gme_ds = snapds;
88111007SLori.Alt@Sun.COM avl_add(guid_map, gmep);
88211007SLori.Alt@Sun.COM lastobj = snapds->ds_phys->ds_prev_snap_obj;
88311007SLori.Alt@Sun.COM }
88411007SLori.Alt@Sun.COM
88511007SLori.Alt@Sun.COM rw_exit(&dp->dp_config_rwlock);
88611007SLori.Alt@Sun.COM dsl_dataset_rele(ds, FTAG);
88711007SLori.Alt@Sun.COM
88811007SLori.Alt@Sun.COM return (0);
88911007SLori.Alt@Sun.COM }
89011007SLori.Alt@Sun.COM
89112527SChris.Kirby@oracle.com static void
free_guid_map_onexit(void * arg)89212527SChris.Kirby@oracle.com free_guid_map_onexit(void *arg)
89312527SChris.Kirby@oracle.com {
89412527SChris.Kirby@oracle.com avl_tree_t *ca = arg;
89512527SChris.Kirby@oracle.com void *cookie = NULL;
89612527SChris.Kirby@oracle.com guid_map_entry_t *gmep;
89712527SChris.Kirby@oracle.com
89812527SChris.Kirby@oracle.com while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
89912527SChris.Kirby@oracle.com dsl_dataset_rele(gmep->gme_ds, ca);
90012527SChris.Kirby@oracle.com kmem_free(gmep, sizeof (guid_map_entry_t));
90112527SChris.Kirby@oracle.com }
90212527SChris.Kirby@oracle.com avl_destroy(ca);
90312527SChris.Kirby@oracle.com kmem_free(ca, sizeof (avl_tree_t));
90412527SChris.Kirby@oracle.com }
90512527SChris.Kirby@oracle.com
9062743Sahrens static void *
restore_read(struct restorearg * ra,int len)9072743Sahrens restore_read(struct restorearg *ra, int len)
9082743Sahrens {
9092743Sahrens void *rv;
9105367Sahrens int done = 0;
9112743Sahrens
9122743Sahrens /* some things will require 8-byte alignment, so everything must */
9132743Sahrens ASSERT3U(len % 8, ==, 0);
9142743Sahrens
9155367Sahrens while (done < len) {
9162743Sahrens ssize_t resid;
9172743Sahrens
9182743Sahrens ra->err = vn_rdwr(UIO_READ, ra->vp,
9195367Sahrens (caddr_t)ra->buf + done, len - done,
9202743Sahrens ra->voff, UIO_SYSSPACE, FAPPEND,
9212743Sahrens RLIM64_INFINITY, CRED(), &resid);
9222743Sahrens
9235367Sahrens if (resid == len - done)
9242743Sahrens ra->err = EINVAL;
9255367Sahrens ra->voff += len - done - resid;
9265367Sahrens done = len - resid;
9272743Sahrens if (ra->err)
9282743Sahrens return (NULL);
9292743Sahrens }
9302743Sahrens
9315367Sahrens ASSERT3U(done, ==, len);
9325367Sahrens rv = ra->buf;
9332743Sahrens if (ra->byteswap)
9345367Sahrens fletcher_4_incremental_byteswap(rv, len, &ra->cksum);
9352743Sahrens else
9365367Sahrens fletcher_4_incremental_native(rv, len, &ra->cksum);
9372743Sahrens return (rv);
9382743Sahrens }
9392743Sahrens
9402743Sahrens static void
backup_byteswap(dmu_replay_record_t * drr)9412743Sahrens backup_byteswap(dmu_replay_record_t *drr)
9422743Sahrens {
9432743Sahrens #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
9442743Sahrens #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
9452743Sahrens drr->drr_type = BSWAP_32(drr->drr_type);
9465367Sahrens drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
9472743Sahrens switch (drr->drr_type) {
9482743Sahrens case DRR_BEGIN:
9492743Sahrens DO64(drr_begin.drr_magic);
95011007SLori.Alt@Sun.COM DO64(drr_begin.drr_versioninfo);
9512743Sahrens DO64(drr_begin.drr_creation_time);
9522743Sahrens DO32(drr_begin.drr_type);
9535367Sahrens DO32(drr_begin.drr_flags);
9542743Sahrens DO64(drr_begin.drr_toguid);
9552743Sahrens DO64(drr_begin.drr_fromguid);
9562743Sahrens break;
9572743Sahrens case DRR_OBJECT:
9582743Sahrens DO64(drr_object.drr_object);
9592743Sahrens /* DO64(drr_object.drr_allocation_txg); */
9602743Sahrens DO32(drr_object.drr_type);
9612743Sahrens DO32(drr_object.drr_bonustype);
9622743Sahrens DO32(drr_object.drr_blksz);
9632743Sahrens DO32(drr_object.drr_bonuslen);
96411007SLori.Alt@Sun.COM DO64(drr_object.drr_toguid);
9652743Sahrens break;
9662743Sahrens case DRR_FREEOBJECTS:
9672743Sahrens DO64(drr_freeobjects.drr_firstobj);
9682743Sahrens DO64(drr_freeobjects.drr_numobjs);
96911007SLori.Alt@Sun.COM DO64(drr_freeobjects.drr_toguid);
9702743Sahrens break;
9712743Sahrens case DRR_WRITE:
9722743Sahrens DO64(drr_write.drr_object);
9732743Sahrens DO32(drr_write.drr_type);
9742743Sahrens DO64(drr_write.drr_offset);
9752743Sahrens DO64(drr_write.drr_length);
97611007SLori.Alt@Sun.COM DO64(drr_write.drr_toguid);
97711381SLori.Alt@Sun.COM DO64(drr_write.drr_key.ddk_cksum.zc_word[0]);
97811381SLori.Alt@Sun.COM DO64(drr_write.drr_key.ddk_cksum.zc_word[1]);
97911381SLori.Alt@Sun.COM DO64(drr_write.drr_key.ddk_cksum.zc_word[2]);
98011381SLori.Alt@Sun.COM DO64(drr_write.drr_key.ddk_cksum.zc_word[3]);
98111381SLori.Alt@Sun.COM DO64(drr_write.drr_key.ddk_prop);
98211007SLori.Alt@Sun.COM break;
98311007SLori.Alt@Sun.COM case DRR_WRITE_BYREF:
98411007SLori.Alt@Sun.COM DO64(drr_write_byref.drr_object);
98511007SLori.Alt@Sun.COM DO64(drr_write_byref.drr_offset);
98611007SLori.Alt@Sun.COM DO64(drr_write_byref.drr_length);
98711007SLori.Alt@Sun.COM DO64(drr_write_byref.drr_toguid);
98811007SLori.Alt@Sun.COM DO64(drr_write_byref.drr_refguid);
98911007SLori.Alt@Sun.COM DO64(drr_write_byref.drr_refobject);
99011007SLori.Alt@Sun.COM DO64(drr_write_byref.drr_refoffset);
99111381SLori.Alt@Sun.COM DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[0]);
99211381SLori.Alt@Sun.COM DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[1]);
99311381SLori.Alt@Sun.COM DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[2]);
99411381SLori.Alt@Sun.COM DO64(drr_write_byref.drr_key.ddk_cksum.zc_word[3]);
99511381SLori.Alt@Sun.COM DO64(drr_write_byref.drr_key.ddk_prop);
9962743Sahrens break;
9972743Sahrens case DRR_FREE:
9982743Sahrens DO64(drr_free.drr_object);
9992743Sahrens DO64(drr_free.drr_offset);
10002743Sahrens DO64(drr_free.drr_length);
100111007SLori.Alt@Sun.COM DO64(drr_free.drr_toguid);
10022743Sahrens break;
100311935SMark.Shellenbaum@Sun.COM case DRR_SPILL:
100411935SMark.Shellenbaum@Sun.COM DO64(drr_spill.drr_object);
100511935SMark.Shellenbaum@Sun.COM DO64(drr_spill.drr_length);
100611935SMark.Shellenbaum@Sun.COM DO64(drr_spill.drr_toguid);
100711935SMark.Shellenbaum@Sun.COM break;
10082743Sahrens case DRR_END:
10092743Sahrens DO64(drr_end.drr_checksum.zc_word[0]);
10102743Sahrens DO64(drr_end.drr_checksum.zc_word[1]);
10112743Sahrens DO64(drr_end.drr_checksum.zc_word[2]);
10122743Sahrens DO64(drr_end.drr_checksum.zc_word[3]);
101311007SLori.Alt@Sun.COM DO64(drr_end.drr_toguid);
10142743Sahrens break;
10152743Sahrens }
10162743Sahrens #undef DO64
10172743Sahrens #undef DO32
10182743Sahrens }
10192743Sahrens
10202743Sahrens static int
restore_object(struct restorearg * ra,objset_t * os,struct drr_object * drro)10212743Sahrens restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
10222743Sahrens {
10232743Sahrens int err;
10242743Sahrens dmu_tx_t *tx;
10257994STim.Haley@Sun.COM void *data = NULL;
10262743Sahrens
10272743Sahrens if (drro->drr_type == DMU_OT_NONE ||
10282743Sahrens drro->drr_type >= DMU_OT_NUMTYPES ||
10292743Sahrens drro->drr_bonustype >= DMU_OT_NUMTYPES ||
103011007SLori.Alt@Sun.COM drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
10312743Sahrens drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
10322743Sahrens P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
10332743Sahrens drro->drr_blksz < SPA_MINBLOCKSIZE ||
10342743Sahrens drro->drr_blksz > SPA_MAXBLOCKSIZE ||
10352743Sahrens drro->drr_bonuslen > DN_MAX_BONUSLEN) {
10362743Sahrens return (EINVAL);
10372743Sahrens }
10382743Sahrens
10398986SMark.Maybee@Sun.COM err = dmu_object_info(os, drro->drr_object, NULL);
10408986SMark.Maybee@Sun.COM
10418986SMark.Maybee@Sun.COM if (err != 0 && err != ENOENT)
10428986SMark.Maybee@Sun.COM return (EINVAL);
10438986SMark.Maybee@Sun.COM
10447994STim.Haley@Sun.COM if (drro->drr_bonuslen) {
10457994STim.Haley@Sun.COM data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
10467994STim.Haley@Sun.COM if (ra->err)
10477994STim.Haley@Sun.COM return (ra->err);
10487994STim.Haley@Sun.COM }
10497994STim.Haley@Sun.COM
10502743Sahrens if (err == ENOENT) {
10512743Sahrens /* currently free, want to be allocated */
10528986SMark.Maybee@Sun.COM tx = dmu_tx_create(os);
10532743Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
10542743Sahrens err = dmu_tx_assign(tx, TXG_WAIT);
10552743Sahrens if (err) {
10562743Sahrens dmu_tx_abort(tx);
10572743Sahrens return (err);
10582743Sahrens }
10592743Sahrens err = dmu_object_claim(os, drro->drr_object,
10602743Sahrens drro->drr_type, drro->drr_blksz,
10612743Sahrens drro->drr_bonustype, drro->drr_bonuslen, tx);
10628986SMark.Maybee@Sun.COM dmu_tx_commit(tx);
10632743Sahrens } else {
10642743Sahrens /* currently allocated, want to be allocated */
10652743Sahrens err = dmu_object_reclaim(os, drro->drr_object,
10662743Sahrens drro->drr_type, drro->drr_blksz,
10678986SMark.Maybee@Sun.COM drro->drr_bonustype, drro->drr_bonuslen);
10682743Sahrens }
106911935SMark.Shellenbaum@Sun.COM if (err) {
10708986SMark.Maybee@Sun.COM return (EINVAL);
107111935SMark.Shellenbaum@Sun.COM }
10728986SMark.Maybee@Sun.COM
10738986SMark.Maybee@Sun.COM tx = dmu_tx_create(os);
10748986SMark.Maybee@Sun.COM dmu_tx_hold_bonus(tx, drro->drr_object);
10758986SMark.Maybee@Sun.COM err = dmu_tx_assign(tx, TXG_WAIT);
10762743Sahrens if (err) {
10778986SMark.Maybee@Sun.COM dmu_tx_abort(tx);
10788986SMark.Maybee@Sun.COM return (err);
10792743Sahrens }
10802743Sahrens
108111007SLori.Alt@Sun.COM dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksumtype,
108211007SLori.Alt@Sun.COM tx);
10832743Sahrens dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
10842743Sahrens
10857994STim.Haley@Sun.COM if (data != NULL) {
10862743Sahrens dmu_buf_t *db;
10877994STim.Haley@Sun.COM
10882743Sahrens VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
10892743Sahrens dmu_buf_will_dirty(db, tx);
10902743Sahrens
10914944Smaybee ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
10924944Smaybee bcopy(data, db->db_data, drro->drr_bonuslen);
10932743Sahrens if (ra->byteswap) {
10942743Sahrens dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
10952743Sahrens drro->drr_bonuslen);
10962743Sahrens }
10972743Sahrens dmu_buf_rele(db, FTAG);
10982743Sahrens }
10992743Sahrens dmu_tx_commit(tx);
11002743Sahrens return (0);
11012743Sahrens }
11022743Sahrens
11032743Sahrens /* ARGSUSED */
11042743Sahrens static int
restore_freeobjects(struct restorearg * ra,objset_t * os,struct drr_freeobjects * drrfo)11052743Sahrens restore_freeobjects(struct restorearg *ra, objset_t *os,
11062743Sahrens struct drr_freeobjects *drrfo)
11072743Sahrens {
11082743Sahrens uint64_t obj;
11092743Sahrens
11102743Sahrens if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
11112743Sahrens return (EINVAL);
11122743Sahrens
11132743Sahrens for (obj = drrfo->drr_firstobj;
11143087Sahrens obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
11153087Sahrens (void) dmu_object_next(os, &obj, FALSE, 0)) {
11162743Sahrens int err;
11172743Sahrens
11182743Sahrens if (dmu_object_info(os, obj, NULL) != 0)
11192743Sahrens continue;
11202743Sahrens
11216992Smaybee err = dmu_free_object(os, obj);
11226992Smaybee if (err)
11232743Sahrens return (err);
11242743Sahrens }
11252743Sahrens return (0);
11262743Sahrens }
11272743Sahrens
11282743Sahrens static int
restore_write(struct restorearg * ra,objset_t * os,struct drr_write * drrw)11292743Sahrens restore_write(struct restorearg *ra, objset_t *os,
11302743Sahrens struct drr_write *drrw)
11312743Sahrens {
11322743Sahrens dmu_tx_t *tx;
11332743Sahrens void *data;
11342743Sahrens int err;
11352743Sahrens
11362743Sahrens if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
11372743Sahrens drrw->drr_type >= DMU_OT_NUMTYPES)
11382743Sahrens return (EINVAL);
11392743Sahrens
11402743Sahrens data = restore_read(ra, drrw->drr_length);
11412743Sahrens if (data == NULL)
11422743Sahrens return (ra->err);
11432743Sahrens
11442743Sahrens if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
11452743Sahrens return (EINVAL);
11462743Sahrens
11472743Sahrens tx = dmu_tx_create(os);
11482743Sahrens
11492743Sahrens dmu_tx_hold_write(tx, drrw->drr_object,
11502743Sahrens drrw->drr_offset, drrw->drr_length);
11512743Sahrens err = dmu_tx_assign(tx, TXG_WAIT);
11522743Sahrens if (err) {
11532743Sahrens dmu_tx_abort(tx);
11542743Sahrens return (err);
11552743Sahrens }
11562743Sahrens if (ra->byteswap)
11572743Sahrens dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
11582743Sahrens dmu_write(os, drrw->drr_object,
11592743Sahrens drrw->drr_offset, drrw->drr_length, data, tx);
11602743Sahrens dmu_tx_commit(tx);
11612743Sahrens return (0);
11622743Sahrens }
11632743Sahrens
116411007SLori.Alt@Sun.COM /*
116511007SLori.Alt@Sun.COM * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
116611007SLori.Alt@Sun.COM * streams to refer to a copy of the data that is already on the
116711007SLori.Alt@Sun.COM * system because it came in earlier in the stream. This function
116811007SLori.Alt@Sun.COM * finds the earlier copy of the data, and uses that copy instead of
116911007SLori.Alt@Sun.COM * data from the stream to fulfill this write.
117011007SLori.Alt@Sun.COM */
117111007SLori.Alt@Sun.COM static int
restore_write_byref(struct restorearg * ra,objset_t * os,struct drr_write_byref * drrwbr)117211007SLori.Alt@Sun.COM restore_write_byref(struct restorearg *ra, objset_t *os,
117311007SLori.Alt@Sun.COM struct drr_write_byref *drrwbr)
117411007SLori.Alt@Sun.COM {
117511007SLori.Alt@Sun.COM dmu_tx_t *tx;
117611007SLori.Alt@Sun.COM int err;
117711007SLori.Alt@Sun.COM guid_map_entry_t gmesrch;
117811007SLori.Alt@Sun.COM guid_map_entry_t *gmep;
117911007SLori.Alt@Sun.COM avl_index_t where;
118011007SLori.Alt@Sun.COM objset_t *ref_os = NULL;
118111007SLori.Alt@Sun.COM dmu_buf_t *dbp;
118211007SLori.Alt@Sun.COM
118311007SLori.Alt@Sun.COM if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
118411007SLori.Alt@Sun.COM return (EINVAL);
118511007SLori.Alt@Sun.COM
118611007SLori.Alt@Sun.COM /*
118711007SLori.Alt@Sun.COM * If the GUID of the referenced dataset is different from the
118811007SLori.Alt@Sun.COM * GUID of the target dataset, find the referenced dataset.
118911007SLori.Alt@Sun.COM */
119011007SLori.Alt@Sun.COM if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
119111007SLori.Alt@Sun.COM gmesrch.guid = drrwbr->drr_refguid;
119212527SChris.Kirby@oracle.com if ((gmep = avl_find(ra->guid_to_ds_map, &gmesrch,
119311007SLori.Alt@Sun.COM &where)) == NULL) {
119411007SLori.Alt@Sun.COM return (EINVAL);
119511007SLori.Alt@Sun.COM }
119611007SLori.Alt@Sun.COM if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
119711007SLori.Alt@Sun.COM return (EINVAL);
119811007SLori.Alt@Sun.COM } else {
119911007SLori.Alt@Sun.COM ref_os = os;
120011007SLori.Alt@Sun.COM }
120111007SLori.Alt@Sun.COM
120211007SLori.Alt@Sun.COM if (err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
120312285SJeff.Bonwick@Sun.COM drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH))
120411007SLori.Alt@Sun.COM return (err);
120511007SLori.Alt@Sun.COM
120611007SLori.Alt@Sun.COM tx = dmu_tx_create(os);
120711007SLori.Alt@Sun.COM
120811007SLori.Alt@Sun.COM dmu_tx_hold_write(tx, drrwbr->drr_object,
120911007SLori.Alt@Sun.COM drrwbr->drr_offset, drrwbr->drr_length);
121011007SLori.Alt@Sun.COM err = dmu_tx_assign(tx, TXG_WAIT);
121111007SLori.Alt@Sun.COM if (err) {
121211007SLori.Alt@Sun.COM dmu_tx_abort(tx);
121311007SLori.Alt@Sun.COM return (err);
121411007SLori.Alt@Sun.COM }
121511007SLori.Alt@Sun.COM dmu_write(os, drrwbr->drr_object,
121611007SLori.Alt@Sun.COM drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
121711007SLori.Alt@Sun.COM dmu_buf_rele(dbp, FTAG);
121811007SLori.Alt@Sun.COM dmu_tx_commit(tx);
121911007SLori.Alt@Sun.COM return (0);
122011007SLori.Alt@Sun.COM }
122111007SLori.Alt@Sun.COM
122211935SMark.Shellenbaum@Sun.COM static int
restore_spill(struct restorearg * ra,objset_t * os,struct drr_spill * drrs)122311935SMark.Shellenbaum@Sun.COM restore_spill(struct restorearg *ra, objset_t *os, struct drr_spill *drrs)
122411935SMark.Shellenbaum@Sun.COM {
122511935SMark.Shellenbaum@Sun.COM dmu_tx_t *tx;
122611935SMark.Shellenbaum@Sun.COM void *data;
122711935SMark.Shellenbaum@Sun.COM dmu_buf_t *db, *db_spill;
122811935SMark.Shellenbaum@Sun.COM int err;
122911935SMark.Shellenbaum@Sun.COM
123011935SMark.Shellenbaum@Sun.COM if (drrs->drr_length < SPA_MINBLOCKSIZE ||
123111935SMark.Shellenbaum@Sun.COM drrs->drr_length > SPA_MAXBLOCKSIZE)
123211935SMark.Shellenbaum@Sun.COM return (EINVAL);
123311935SMark.Shellenbaum@Sun.COM
123411935SMark.Shellenbaum@Sun.COM data = restore_read(ra, drrs->drr_length);
123511935SMark.Shellenbaum@Sun.COM if (data == NULL)
123611935SMark.Shellenbaum@Sun.COM return (ra->err);
123711935SMark.Shellenbaum@Sun.COM
123811935SMark.Shellenbaum@Sun.COM if (dmu_object_info(os, drrs->drr_object, NULL) != 0)
123911935SMark.Shellenbaum@Sun.COM return (EINVAL);
124011935SMark.Shellenbaum@Sun.COM
124111935SMark.Shellenbaum@Sun.COM VERIFY(0 == dmu_bonus_hold(os, drrs->drr_object, FTAG, &db));
124211935SMark.Shellenbaum@Sun.COM if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
124311935SMark.Shellenbaum@Sun.COM dmu_buf_rele(db, FTAG);
124411935SMark.Shellenbaum@Sun.COM return (err);
124511935SMark.Shellenbaum@Sun.COM }
124611935SMark.Shellenbaum@Sun.COM
124711935SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(os);
124811935SMark.Shellenbaum@Sun.COM
124911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_spill(tx, db->db_object);
125011935SMark.Shellenbaum@Sun.COM
125111935SMark.Shellenbaum@Sun.COM err = dmu_tx_assign(tx, TXG_WAIT);
125211935SMark.Shellenbaum@Sun.COM if (err) {
125311935SMark.Shellenbaum@Sun.COM dmu_buf_rele(db, FTAG);
125411935SMark.Shellenbaum@Sun.COM dmu_buf_rele(db_spill, FTAG);
125511935SMark.Shellenbaum@Sun.COM dmu_tx_abort(tx);
125611935SMark.Shellenbaum@Sun.COM return (err);
125711935SMark.Shellenbaum@Sun.COM }
125811935SMark.Shellenbaum@Sun.COM dmu_buf_will_dirty(db_spill, tx);
125911935SMark.Shellenbaum@Sun.COM
126011935SMark.Shellenbaum@Sun.COM if (db_spill->db_size < drrs->drr_length)
126111935SMark.Shellenbaum@Sun.COM VERIFY(0 == dbuf_spill_set_blksz(db_spill,
126211935SMark.Shellenbaum@Sun.COM drrs->drr_length, tx));
126311935SMark.Shellenbaum@Sun.COM bcopy(data, db_spill->db_data, drrs->drr_length);
126411935SMark.Shellenbaum@Sun.COM
126511935SMark.Shellenbaum@Sun.COM dmu_buf_rele(db, FTAG);
126611935SMark.Shellenbaum@Sun.COM dmu_buf_rele(db_spill, FTAG);
126711935SMark.Shellenbaum@Sun.COM
126811935SMark.Shellenbaum@Sun.COM dmu_tx_commit(tx);
126911935SMark.Shellenbaum@Sun.COM return (0);
127011935SMark.Shellenbaum@Sun.COM }
127111935SMark.Shellenbaum@Sun.COM
12722743Sahrens /* ARGSUSED */
12732743Sahrens static int
restore_free(struct restorearg * ra,objset_t * os,struct drr_free * drrf)12742743Sahrens restore_free(struct restorearg *ra, objset_t *os,
12752743Sahrens struct drr_free *drrf)
12762743Sahrens {
12772743Sahrens int err;
12782743Sahrens
12792743Sahrens if (drrf->drr_length != -1ULL &&
12802743Sahrens drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
12812743Sahrens return (EINVAL);
12822743Sahrens
12832743Sahrens if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
12842743Sahrens return (EINVAL);
12852743Sahrens
12866992Smaybee err = dmu_free_long_range(os, drrf->drr_object,
12872743Sahrens drrf->drr_offset, drrf->drr_length);
12882743Sahrens return (err);
12892743Sahrens }
12902743Sahrens
12915367Sahrens /*
12925367Sahrens * NB: callers *must* call dmu_recv_end() if this succeeds.
12935367Sahrens */
12945367Sahrens int
dmu_recv_stream(dmu_recv_cookie_t * drc,vnode_t * vp,offset_t * voffp,int cleanup_fd,uint64_t * action_handlep)129512527SChris.Kirby@oracle.com dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
129612527SChris.Kirby@oracle.com int cleanup_fd, uint64_t *action_handlep)
12975367Sahrens {
12985367Sahrens struct restorearg ra = { 0 };
12995367Sahrens dmu_replay_record_t *drr;
13005367Sahrens objset_t *os;
13015367Sahrens zio_cksum_t pcksum;
130211007SLori.Alt@Sun.COM int featureflags;
13035367Sahrens
13045367Sahrens if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
13055367Sahrens ra.byteswap = TRUE;
13062743Sahrens
13075367Sahrens {
13085367Sahrens /* compute checksum of drr_begin record */
13095367Sahrens dmu_replay_record_t *drr;
13105367Sahrens drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
13115367Sahrens
13125367Sahrens drr->drr_type = DRR_BEGIN;
13135367Sahrens drr->drr_u.drr_begin = *drc->drc_drrb;
13145367Sahrens if (ra.byteswap) {
13155367Sahrens fletcher_4_incremental_byteswap(drr,
13165367Sahrens sizeof (dmu_replay_record_t), &ra.cksum);
13175367Sahrens } else {
13185367Sahrens fletcher_4_incremental_native(drr,
13195367Sahrens sizeof (dmu_replay_record_t), &ra.cksum);
13205367Sahrens }
13215367Sahrens kmem_free(drr, sizeof (dmu_replay_record_t));
13222743Sahrens }
13232743Sahrens
13242743Sahrens if (ra.byteswap) {
13255367Sahrens struct drr_begin *drrb = drc->drc_drrb;
13262743Sahrens drrb->drr_magic = BSWAP_64(drrb->drr_magic);
132711007SLori.Alt@Sun.COM drrb->drr_versioninfo = BSWAP_64(drrb->drr_versioninfo);
13282743Sahrens drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
13292743Sahrens drrb->drr_type = BSWAP_32(drrb->drr_type);
13302743Sahrens drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
13312743Sahrens drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
13322743Sahrens }
13332743Sahrens
13345367Sahrens ra.vp = vp;
13355367Sahrens ra.voff = *voffp;
13365367Sahrens ra.bufsize = 1<<20;
13375367Sahrens ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
13385326Sek110237
13395367Sahrens /* these were verified in dmu_recv_begin */
134011007SLori.Alt@Sun.COM ASSERT(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo) ==
134111007SLori.Alt@Sun.COM DMU_SUBSTREAM);
13425367Sahrens ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES);
13432743Sahrens
13442743Sahrens /*
13452743Sahrens * Open the objset we are modifying.
13462743Sahrens */
134710298SMatthew.Ahrens@Sun.COM VERIFY(dmu_objset_from_ds(drc->drc_real_ds, &os) == 0);
13482743Sahrens
13495367Sahrens ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
13502743Sahrens
135111007SLori.Alt@Sun.COM featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
135211007SLori.Alt@Sun.COM
135311007SLori.Alt@Sun.COM /* if this stream is dedup'ed, set up the avl tree for guid mapping */
135411007SLori.Alt@Sun.COM if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
135512786SChris.Kirby@oracle.com minor_t minor;
135612786SChris.Kirby@oracle.com
135712527SChris.Kirby@oracle.com if (cleanup_fd == -1) {
135812527SChris.Kirby@oracle.com ra.err = EBADF;
135912527SChris.Kirby@oracle.com goto out;
136012527SChris.Kirby@oracle.com }
136112786SChris.Kirby@oracle.com ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
136212786SChris.Kirby@oracle.com if (ra.err) {
136312786SChris.Kirby@oracle.com cleanup_fd = -1;
136412786SChris.Kirby@oracle.com goto out;
136512786SChris.Kirby@oracle.com }
136612786SChris.Kirby@oracle.com
136712527SChris.Kirby@oracle.com if (*action_handlep == 0) {
136812527SChris.Kirby@oracle.com ra.guid_to_ds_map =
136912527SChris.Kirby@oracle.com kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
137012527SChris.Kirby@oracle.com avl_create(ra.guid_to_ds_map, guid_compare,
137112527SChris.Kirby@oracle.com sizeof (guid_map_entry_t),
137212527SChris.Kirby@oracle.com offsetof(guid_map_entry_t, avlnode));
137312527SChris.Kirby@oracle.com (void) dmu_objset_find(drc->drc_top_ds, find_ds_by_guid,
137412527SChris.Kirby@oracle.com (void *)ra.guid_to_ds_map,
137512527SChris.Kirby@oracle.com DS_FIND_CHILDREN);
137612786SChris.Kirby@oracle.com ra.err = zfs_onexit_add_cb(minor,
137712527SChris.Kirby@oracle.com free_guid_map_onexit, ra.guid_to_ds_map,
137812527SChris.Kirby@oracle.com action_handlep);
137912527SChris.Kirby@oracle.com if (ra.err)
138012527SChris.Kirby@oracle.com goto out;
138112527SChris.Kirby@oracle.com } else {
138212786SChris.Kirby@oracle.com ra.err = zfs_onexit_cb_data(minor, *action_handlep,
138312527SChris.Kirby@oracle.com (void **)&ra.guid_to_ds_map);
138412527SChris.Kirby@oracle.com if (ra.err)
138512527SChris.Kirby@oracle.com goto out;
138612527SChris.Kirby@oracle.com }
138711007SLori.Alt@Sun.COM }
138811007SLori.Alt@Sun.COM
13892743Sahrens /*
13902743Sahrens * Read records and process them.
13912743Sahrens */
13925367Sahrens pcksum = ra.cksum;
13932743Sahrens while (ra.err == 0 &&
13942743Sahrens NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
13952743Sahrens if (issig(JUSTLOOKING) && issig(FORREAL)) {
13962743Sahrens ra.err = EINTR;
13972743Sahrens goto out;
13982743Sahrens }
13992743Sahrens
14002743Sahrens if (ra.byteswap)
14012743Sahrens backup_byteswap(drr);
14022743Sahrens
14032743Sahrens switch (drr->drr_type) {
14042743Sahrens case DRR_OBJECT:
14052743Sahrens {
14062743Sahrens /*
14072743Sahrens * We need to make a copy of the record header,
14082743Sahrens * because restore_{object,write} may need to
14092743Sahrens * restore_read(), which will invalidate drr.
14102743Sahrens */
14112743Sahrens struct drr_object drro = drr->drr_u.drr_object;
14122743Sahrens ra.err = restore_object(&ra, os, &drro);
14132743Sahrens break;
14142743Sahrens }
14152743Sahrens case DRR_FREEOBJECTS:
14162743Sahrens {
14172743Sahrens struct drr_freeobjects drrfo =
14182743Sahrens drr->drr_u.drr_freeobjects;
14192743Sahrens ra.err = restore_freeobjects(&ra, os, &drrfo);
14202743Sahrens break;
14212743Sahrens }
14222743Sahrens case DRR_WRITE:
14232743Sahrens {
14242743Sahrens struct drr_write drrw = drr->drr_u.drr_write;
14252743Sahrens ra.err = restore_write(&ra, os, &drrw);
14262743Sahrens break;
14272743Sahrens }
142811007SLori.Alt@Sun.COM case DRR_WRITE_BYREF:
142911007SLori.Alt@Sun.COM {
143011007SLori.Alt@Sun.COM struct drr_write_byref drrwbr =
143111007SLori.Alt@Sun.COM drr->drr_u.drr_write_byref;
143211007SLori.Alt@Sun.COM ra.err = restore_write_byref(&ra, os, &drrwbr);
143311007SLori.Alt@Sun.COM break;
143411007SLori.Alt@Sun.COM }
14352743Sahrens case DRR_FREE:
14362743Sahrens {
14372743Sahrens struct drr_free drrf = drr->drr_u.drr_free;
14382743Sahrens ra.err = restore_free(&ra, os, &drrf);
14392743Sahrens break;
14402743Sahrens }
14412743Sahrens case DRR_END:
14422743Sahrens {
14432743Sahrens struct drr_end drre = drr->drr_u.drr_end;
14442743Sahrens /*
14452743Sahrens * We compare against the *previous* checksum
14462743Sahrens * value, because the stored checksum is of
14472743Sahrens * everything before the DRR_END record.
14482743Sahrens */
14496479Sahrens if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
14502743Sahrens ra.err = ECKSUM;
14512743Sahrens goto out;
14522743Sahrens }
145311935SMark.Shellenbaum@Sun.COM case DRR_SPILL:
145411935SMark.Shellenbaum@Sun.COM {
145511935SMark.Shellenbaum@Sun.COM struct drr_spill drrs = drr->drr_u.drr_spill;
145611935SMark.Shellenbaum@Sun.COM ra.err = restore_spill(&ra, os, &drrs);
145711935SMark.Shellenbaum@Sun.COM break;
145811935SMark.Shellenbaum@Sun.COM }
14592743Sahrens default:
14602743Sahrens ra.err = EINVAL;
14612743Sahrens goto out;
14622743Sahrens }
14635367Sahrens pcksum = ra.cksum;
14642743Sahrens }
14656479Sahrens ASSERT(ra.err != 0);
14662743Sahrens
14672743Sahrens out:
146812786SChris.Kirby@oracle.com if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
146912786SChris.Kirby@oracle.com zfs_onexit_fd_rele(cleanup_fd);
147012786SChris.Kirby@oracle.com
14715367Sahrens if (ra.err != 0) {
14722743Sahrens /*
147310204SMatthew.Ahrens@Sun.COM * destroy what we created, so we don't leave it in the
147410204SMatthew.Ahrens@Sun.COM * inconsistent restoring state.
14752743Sahrens */
14765367Sahrens txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0);
147710204SMatthew.Ahrens@Sun.COM
147810242Schris.kirby@sun.com (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag,
147910242Schris.kirby@sun.com B_FALSE);
148010204SMatthew.Ahrens@Sun.COM if (drc->drc_real_ds != drc->drc_logical_ds) {
148110204SMatthew.Ahrens@Sun.COM mutex_exit(&drc->drc_logical_ds->ds_recvlock);
148210204SMatthew.Ahrens@Sun.COM dsl_dataset_rele(drc->drc_logical_ds, dmu_recv_tag);
148310204SMatthew.Ahrens@Sun.COM }
14842743Sahrens }
14852743Sahrens
14862743Sahrens kmem_free(ra.buf, ra.bufsize);
14875367Sahrens *voffp = ra.voff;
14882743Sahrens return (ra.err);
14892743Sahrens }
14905326Sek110237
14915367Sahrens struct recvendsyncarg {
14925367Sahrens char *tosnap;
14935367Sahrens uint64_t creation_time;
14945367Sahrens uint64_t toguid;
14955367Sahrens };
14965367Sahrens
14975367Sahrens static int
recv_end_check(void * arg1,void * arg2,dmu_tx_t * tx)14985367Sahrens recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx)
14995367Sahrens {
15005367Sahrens dsl_dataset_t *ds = arg1;
15015367Sahrens struct recvendsyncarg *resa = arg2;
15025367Sahrens
15035367Sahrens return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx));
15045367Sahrens }
15055367Sahrens
15065367Sahrens static void
recv_end_sync(void * arg1,void * arg2,dmu_tx_t * tx)150712296SLin.Ling@Sun.COM recv_end_sync(void *arg1, void *arg2, dmu_tx_t *tx)
15085326Sek110237 {
15095367Sahrens dsl_dataset_t *ds = arg1;
15105367Sahrens struct recvendsyncarg *resa = arg2;
15115367Sahrens
151212296SLin.Ling@Sun.COM dsl_dataset_snapshot_sync(ds, resa->tosnap, tx);
15135367Sahrens
15145367Sahrens /* set snapshot's creation time and guid */
15155367Sahrens dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
15165367Sahrens ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time;
15175367Sahrens ds->ds_prev->ds_phys->ds_guid = resa->toguid;
15185367Sahrens ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
15195367Sahrens
15205367Sahrens dmu_buf_will_dirty(ds->ds_dbuf, tx);
15215367Sahrens ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
15225367Sahrens }
15235367Sahrens
152410272SMatthew.Ahrens@Sun.COM static int
dmu_recv_existing_end(dmu_recv_cookie_t * drc)152510272SMatthew.Ahrens@Sun.COM dmu_recv_existing_end(dmu_recv_cookie_t *drc)
15265367Sahrens {
15276689Smaybee struct recvendsyncarg resa;
15286689Smaybee dsl_dataset_t *ds = drc->drc_logical_ds;
15296689Smaybee int err;
15305367Sahrens
15315367Sahrens /*
153210272SMatthew.Ahrens@Sun.COM * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean()
153310272SMatthew.Ahrens@Sun.COM * expects it to have a ds_user_ptr (and zil), but clone_swap()
153410272SMatthew.Ahrens@Sun.COM * can close it.
15355367Sahrens */
15366689Smaybee txg_wait_synced(ds->ds_dir->dd_pool, 0);
15375326Sek110237
153810272SMatthew.Ahrens@Sun.COM if (dsl_dataset_tryown(ds, FALSE, dmu_recv_tag)) {
153910272SMatthew.Ahrens@Sun.COM err = dsl_dataset_clone_swap(drc->drc_real_ds, ds,
154010272SMatthew.Ahrens@Sun.COM drc->drc_force);
154110272SMatthew.Ahrens@Sun.COM if (err)
154210272SMatthew.Ahrens@Sun.COM goto out;
154310272SMatthew.Ahrens@Sun.COM } else {
154410272SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_recvlock);
154510272SMatthew.Ahrens@Sun.COM dsl_dataset_rele(ds, dmu_recv_tag);
154610242Schris.kirby@sun.com (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag,
154710242Schris.kirby@sun.com B_FALSE);
154810272SMatthew.Ahrens@Sun.COM return (EBUSY);
15495367Sahrens }
15505367Sahrens
15516689Smaybee resa.creation_time = drc->drc_drrb->drr_creation_time;
15526689Smaybee resa.toguid = drc->drc_drrb->drr_toguid;
15536689Smaybee resa.tosnap = drc->drc_tosnap;
15546689Smaybee
15556689Smaybee err = dsl_sync_task_do(ds->ds_dir->dd_pool,
15566689Smaybee recv_end_check, recv_end_sync, ds, &resa, 3);
15576689Smaybee if (err) {
155810272SMatthew.Ahrens@Sun.COM /* swap back */
155910272SMatthew.Ahrens@Sun.COM (void) dsl_dataset_clone_swap(drc->drc_real_ds, ds, B_TRUE);
15605367Sahrens }
15615367Sahrens
156210272SMatthew.Ahrens@Sun.COM out:
156310272SMatthew.Ahrens@Sun.COM mutex_exit(&ds->ds_recvlock);
15646689Smaybee dsl_dataset_disown(ds, dmu_recv_tag);
156510272SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag, B_FALSE);
15665326Sek110237 return (err);
15675326Sek110237 }
156810272SMatthew.Ahrens@Sun.COM
156910272SMatthew.Ahrens@Sun.COM static int
dmu_recv_new_end(dmu_recv_cookie_t * drc)157010272SMatthew.Ahrens@Sun.COM dmu_recv_new_end(dmu_recv_cookie_t *drc)
157110272SMatthew.Ahrens@Sun.COM {
157210272SMatthew.Ahrens@Sun.COM struct recvendsyncarg resa;
157310272SMatthew.Ahrens@Sun.COM dsl_dataset_t *ds = drc->drc_logical_ds;
157410272SMatthew.Ahrens@Sun.COM int err;
157510272SMatthew.Ahrens@Sun.COM
157610272SMatthew.Ahrens@Sun.COM /*
157710272SMatthew.Ahrens@Sun.COM * XXX hack; seems the ds is still dirty and dsl_pool_zil_clean()
157810272SMatthew.Ahrens@Sun.COM * expects it to have a ds_user_ptr (and zil), but clone_swap()
157910272SMatthew.Ahrens@Sun.COM * can close it.
158010272SMatthew.Ahrens@Sun.COM */
158110272SMatthew.Ahrens@Sun.COM txg_wait_synced(ds->ds_dir->dd_pool, 0);
158210272SMatthew.Ahrens@Sun.COM
158310272SMatthew.Ahrens@Sun.COM resa.creation_time = drc->drc_drrb->drr_creation_time;
158410272SMatthew.Ahrens@Sun.COM resa.toguid = drc->drc_drrb->drr_toguid;
158510272SMatthew.Ahrens@Sun.COM resa.tosnap = drc->drc_tosnap;
158610272SMatthew.Ahrens@Sun.COM
158710272SMatthew.Ahrens@Sun.COM err = dsl_sync_task_do(ds->ds_dir->dd_pool,
158810272SMatthew.Ahrens@Sun.COM recv_end_check, recv_end_sync, ds, &resa, 3);
158910272SMatthew.Ahrens@Sun.COM if (err) {
159010272SMatthew.Ahrens@Sun.COM /* clean up the fs we just recv'd into */
159110272SMatthew.Ahrens@Sun.COM (void) dsl_dataset_destroy(ds, dmu_recv_tag, B_FALSE);
159210272SMatthew.Ahrens@Sun.COM } else {
159310272SMatthew.Ahrens@Sun.COM /* release the hold from dmu_recv_begin */
159410272SMatthew.Ahrens@Sun.COM dsl_dataset_disown(ds, dmu_recv_tag);
159510272SMatthew.Ahrens@Sun.COM }
159610272SMatthew.Ahrens@Sun.COM return (err);
159710272SMatthew.Ahrens@Sun.COM }
159810272SMatthew.Ahrens@Sun.COM
159910272SMatthew.Ahrens@Sun.COM int
dmu_recv_end(dmu_recv_cookie_t * drc)160010272SMatthew.Ahrens@Sun.COM dmu_recv_end(dmu_recv_cookie_t *drc)
160110272SMatthew.Ahrens@Sun.COM {
160210272SMatthew.Ahrens@Sun.COM if (drc->drc_logical_ds != drc->drc_real_ds)
160310272SMatthew.Ahrens@Sun.COM return (dmu_recv_existing_end(drc));
160410272SMatthew.Ahrens@Sun.COM else
160510272SMatthew.Ahrens@Sun.COM return (dmu_recv_new_end(drc));
160610272SMatthew.Ahrens@Sun.COM }
1607