xref: /titanic_52/usr/src/uts/common/fs/zfs/dmu_send.c (revision 1d842814faabbb6af3e2fe30a4bd61aa4a70eeb3)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/dmu.h>
29 #include <sys/dmu_impl.h>
30 #include <sys/dmu_tx.h>
31 #include <sys/dbuf.h>
32 #include <sys/dnode.h>
33 #include <sys/zfs_context.h>
34 #include <sys/dmu_objset.h>
35 #include <sys/dmu_traverse.h>
36 #include <sys/dsl_dataset.h>
37 #include <sys/dsl_dir.h>
38 #include <sys/dsl_pool.h>
39 #include <sys/dsl_synctask.h>
40 #include <sys/zfs_ioctl.h>
41 #include <sys/zap.h>
42 #include <sys/zio_checksum.h>
43 
44 struct backuparg {
45 	dmu_replay_record_t *drr;
46 	vnode_t *vp;
47 	objset_t *os;
48 	zio_cksum_t zc;
49 	int err;
50 };
51 
52 static int
53 dump_bytes(struct backuparg *ba, void *buf, int len)
54 {
55 	ssize_t resid; /* have to get resid to get detailed errno */
56 	ASSERT3U(len % 8, ==, 0);
57 
58 	fletcher_4_incremental_native(buf, len, &ba->zc);
59 	ba->err = vn_rdwr(UIO_WRITE, ba->vp,
60 	    (caddr_t)buf, len,
61 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
62 	return (ba->err);
63 }
64 
65 static int
66 dump_free(struct backuparg *ba, uint64_t object, uint64_t offset,
67     uint64_t length)
68 {
69 	/* write a FREE record */
70 	bzero(ba->drr, sizeof (dmu_replay_record_t));
71 	ba->drr->drr_type = DRR_FREE;
72 	ba->drr->drr_u.drr_free.drr_object = object;
73 	ba->drr->drr_u.drr_free.drr_offset = offset;
74 	ba->drr->drr_u.drr_free.drr_length = length;
75 
76 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
77 		return (EINTR);
78 	return (0);
79 }
80 
81 static int
82 dump_data(struct backuparg *ba, dmu_object_type_t type,
83     uint64_t object, uint64_t offset, int blksz, void *data)
84 {
85 	/* write a DATA record */
86 	bzero(ba->drr, sizeof (dmu_replay_record_t));
87 	ba->drr->drr_type = DRR_WRITE;
88 	ba->drr->drr_u.drr_write.drr_object = object;
89 	ba->drr->drr_u.drr_write.drr_type = type;
90 	ba->drr->drr_u.drr_write.drr_offset = offset;
91 	ba->drr->drr_u.drr_write.drr_length = blksz;
92 
93 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
94 		return (EINTR);
95 	if (dump_bytes(ba, data, blksz))
96 		return (EINTR);
97 	return (0);
98 }
99 
100 static int
101 dump_freeobjects(struct backuparg *ba, uint64_t firstobj, uint64_t numobjs)
102 {
103 	/* write a FREEOBJECTS record */
104 	bzero(ba->drr, sizeof (dmu_replay_record_t));
105 	ba->drr->drr_type = DRR_FREEOBJECTS;
106 	ba->drr->drr_u.drr_freeobjects.drr_firstobj = firstobj;
107 	ba->drr->drr_u.drr_freeobjects.drr_numobjs = numobjs;
108 
109 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
110 		return (EINTR);
111 	return (0);
112 }
113 
114 static int
115 dump_dnode(struct backuparg *ba, uint64_t object, dnode_phys_t *dnp)
116 {
117 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
118 		return (dump_freeobjects(ba, object, 1));
119 
120 	/* write an OBJECT record */
121 	bzero(ba->drr, sizeof (dmu_replay_record_t));
122 	ba->drr->drr_type = DRR_OBJECT;
123 	ba->drr->drr_u.drr_object.drr_object = object;
124 	ba->drr->drr_u.drr_object.drr_type = dnp->dn_type;
125 	ba->drr->drr_u.drr_object.drr_bonustype = dnp->dn_bonustype;
126 	ba->drr->drr_u.drr_object.drr_blksz =
127 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
128 	ba->drr->drr_u.drr_object.drr_bonuslen = dnp->dn_bonuslen;
129 	ba->drr->drr_u.drr_object.drr_checksum = dnp->dn_checksum;
130 	ba->drr->drr_u.drr_object.drr_compress = dnp->dn_compress;
131 
132 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
133 		return (EINTR);
134 
135 	if (dump_bytes(ba, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)))
136 		return (EINTR);
137 
138 	/* free anything past the end of the file */
139 	if (dump_free(ba, object, (dnp->dn_maxblkid + 1) *
140 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL))
141 		return (EINTR);
142 	if (ba->err)
143 		return (EINTR);
144 	return (0);
145 }
146 
147 #define	BP_SPAN(dnp, level) \
148 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
149 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
150 
151 static int
152 backup_cb(traverse_blk_cache_t *bc, spa_t *spa, void *arg)
153 {
154 	struct backuparg *ba = arg;
155 	uint64_t object = bc->bc_bookmark.zb_object;
156 	int level = bc->bc_bookmark.zb_level;
157 	uint64_t blkid = bc->bc_bookmark.zb_blkid;
158 	blkptr_t *bp = bc->bc_blkptr.blk_birth ? &bc->bc_blkptr : NULL;
159 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
160 	void *data = bc->bc_data;
161 	int err = 0;
162 
163 	if (issig(JUSTLOOKING) && issig(FORREAL))
164 		return (EINTR);
165 
166 	ASSERT(data || bp == NULL);
167 
168 	if (bp == NULL && object == 0) {
169 		uint64_t span = BP_SPAN(bc->bc_dnode, level);
170 		uint64_t dnobj = (blkid * span) >> DNODE_SHIFT;
171 		err = dump_freeobjects(ba, dnobj, span >> DNODE_SHIFT);
172 	} else if (bp == NULL) {
173 		uint64_t span = BP_SPAN(bc->bc_dnode, level);
174 		err = dump_free(ba, object, blkid * span, span);
175 	} else if (data && level == 0 && type == DMU_OT_DNODE) {
176 		dnode_phys_t *blk = data;
177 		int i;
178 		int blksz = BP_GET_LSIZE(bp);
179 
180 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
181 			uint64_t dnobj =
182 			    (blkid << (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
183 			err = dump_dnode(ba, dnobj, blk+i);
184 			if (err)
185 				break;
186 		}
187 	} else if (level == 0 &&
188 	    type != DMU_OT_DNODE && type != DMU_OT_OBJSET) {
189 		int blksz = BP_GET_LSIZE(bp);
190 		if (data == NULL) {
191 			uint32_t aflags = ARC_WAIT;
192 			arc_buf_t *abuf;
193 			zbookmark_t zb;
194 
195 			zb.zb_objset = ba->os->os->os_dsl_dataset->ds_object;
196 			zb.zb_object = object;
197 			zb.zb_level = level;
198 			zb.zb_blkid = blkid;
199 			(void) arc_read(NULL, spa, bp,
200 			    dmu_ot[type].ot_byteswap, arc_getbuf_func, &abuf,
201 			    ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_MUSTSUCCEED,
202 			    &aflags, &zb);
203 
204 			if (abuf) {
205 				err = dump_data(ba, type, object, blkid * blksz,
206 				    blksz, abuf->b_data);
207 				(void) arc_buf_remove_ref(abuf, &abuf);
208 			}
209 		} else {
210 			err = dump_data(ba, type, object, blkid * blksz,
211 			    blksz, data);
212 		}
213 	}
214 
215 	ASSERT(err == 0 || err == EINTR);
216 	return (err);
217 }
218 
219 int
220 dmu_sendbackup(objset_t *tosnap, objset_t *fromsnap, vnode_t *vp)
221 {
222 	dsl_dataset_t *ds = tosnap->os->os_dsl_dataset;
223 	dsl_dataset_t *fromds = fromsnap ? fromsnap->os->os_dsl_dataset : NULL;
224 	dmu_replay_record_t *drr;
225 	struct backuparg ba;
226 	int err;
227 
228 	/* tosnap must be a snapshot */
229 	if (ds->ds_phys->ds_next_snap_obj == 0)
230 		return (EINVAL);
231 
232 	/* fromsnap must be an earlier snapshot from the same fs as tosnap */
233 	if (fromds && (ds->ds_dir != fromds->ds_dir ||
234 	    fromds->ds_phys->ds_creation_txg >=
235 	    ds->ds_phys->ds_creation_txg))
236 		return (EXDEV);
237 
238 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
239 	drr->drr_type = DRR_BEGIN;
240 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
241 	drr->drr_u.drr_begin.drr_version = DMU_BACKUP_VERSION;
242 	drr->drr_u.drr_begin.drr_creation_time =
243 	    ds->ds_phys->ds_creation_time;
244 	drr->drr_u.drr_begin.drr_type = tosnap->os->os_phys->os_type;
245 	drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
246 	if (fromds)
247 		drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid;
248 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
249 
250 	ba.drr = drr;
251 	ba.vp = vp;
252 	ba.os = tosnap;
253 	ZIO_SET_CHECKSUM(&ba.zc, 0, 0, 0, 0);
254 
255 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
256 		kmem_free(drr, sizeof (dmu_replay_record_t));
257 		return (ba.err);
258 	}
259 
260 	err = traverse_dsl_dataset(ds,
261 	    fromds ? fromds->ds_phys->ds_creation_txg : 0,
262 	    ADVANCE_PRE | ADVANCE_HOLES | ADVANCE_DATA | ADVANCE_NOLOCK,
263 	    backup_cb, &ba);
264 
265 	if (err) {
266 		if (err == EINTR && ba.err)
267 			err = ba.err;
268 		return (err);
269 	}
270 
271 	bzero(drr, sizeof (dmu_replay_record_t));
272 	drr->drr_type = DRR_END;
273 	drr->drr_u.drr_end.drr_checksum = ba.zc;
274 
275 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t)))
276 		return (ba.err);
277 
278 	kmem_free(drr, sizeof (dmu_replay_record_t));
279 
280 	return (0);
281 }
282 
283 struct restorearg {
284 	int err;
285 	int byteswap;
286 	vnode_t *vp;
287 	char *buf;
288 	uint64_t voff;
289 	int buflen; /* number of valid bytes in buf */
290 	int bufoff; /* next offset to read */
291 	int bufsize; /* amount of memory allocated for buf */
292 	zio_cksum_t zc;
293 };
294 
295 /* ARGSUSED */
296 static int
297 replay_incremental_check(void *arg1, void *arg2, dmu_tx_t *tx)
298 {
299 	dsl_dataset_t *ds = arg1;
300 	struct drr_begin *drrb = arg2;
301 	const char *snapname;
302 	int err;
303 	uint64_t val;
304 
305 	/* must already be a snapshot of this fs */
306 	if (ds->ds_phys->ds_prev_snap_obj == 0)
307 		return (ENODEV);
308 
309 	/* most recent snapshot must match fromguid */
310 	if (ds->ds_prev->ds_phys->ds_guid != drrb->drr_fromguid)
311 		return (ENODEV);
312 	/* must not have any changes since most recent snapshot */
313 	if (ds->ds_phys->ds_bp.blk_birth >
314 	    ds->ds_prev->ds_phys->ds_creation_txg)
315 		return (ETXTBSY);
316 
317 	/* new snapshot name must not exist */
318 	snapname = strrchr(drrb->drr_toname, '@');
319 	if (snapname == NULL)
320 		return (EEXIST);
321 
322 	snapname++;
323 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
324 	    ds->ds_phys->ds_snapnames_zapobj, snapname, 8, 1, &val);
325 	if (err == 0)
326 		return (EEXIST);
327 	if (err != ENOENT)
328 		return (err);
329 
330 	return (0);
331 }
332 
333 /* ARGSUSED */
334 static void
335 replay_incremental_sync(void *arg1, void *arg2, dmu_tx_t *tx)
336 {
337 	dsl_dataset_t *ds = arg1;
338 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
339 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
340 }
341 
342 /* ARGSUSED */
343 static int
344 replay_full_check(void *arg1, void *arg2, dmu_tx_t *tx)
345 {
346 	dsl_dir_t *dd = arg1;
347 	struct drr_begin *drrb = arg2;
348 	objset_t *mos = dd->dd_pool->dp_meta_objset;
349 	char *cp;
350 	uint64_t val;
351 	int err;
352 
353 	cp = strchr(drrb->drr_toname, '@');
354 	*cp = '\0';
355 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
356 	    strrchr(drrb->drr_toname, '/') + 1,
357 	    sizeof (uint64_t), 1, &val);
358 	*cp = '@';
359 
360 	if (err != ENOENT)
361 		return (err ? err : EEXIST);
362 
363 	return (0);
364 }
365 
366 static void
367 replay_full_sync(void *arg1, void *arg2, dmu_tx_t *tx)
368 {
369 	dsl_dir_t *dd = arg1;
370 	struct drr_begin *drrb = arg2;
371 	char *cp;
372 	dsl_dataset_t *ds;
373 	uint64_t dsobj;
374 
375 	cp = strchr(drrb->drr_toname, '@');
376 	*cp = '\0';
377 	dsobj = dsl_dataset_create_sync(dd, strrchr(drrb->drr_toname, '/') + 1,
378 	    NULL, tx);
379 	*cp = '@';
380 
381 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL,
382 	    DS_MODE_EXCLUSIVE, FTAG, &ds));
383 
384 	(void) dmu_objset_create_impl(dsl_dataset_get_spa(ds),
385 	    ds, drrb->drr_type, tx);
386 
387 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
388 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
389 
390 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
391 }
392 
393 static int
394 replay_end_check(void *arg1, void *arg2, dmu_tx_t *tx)
395 {
396 	objset_t *os = arg1;
397 	struct drr_begin *drrb = arg2;
398 	char *snapname;
399 
400 	/* XXX verify that drr_toname is in dd */
401 
402 	snapname = strchr(drrb->drr_toname, '@');
403 	if (snapname == NULL)
404 		return (EINVAL);
405 	snapname++;
406 
407 	return (dsl_dataset_snapshot_check(os, snapname, tx));
408 }
409 
410 static void
411 replay_end_sync(void *arg1, void *arg2, dmu_tx_t *tx)
412 {
413 	objset_t *os = arg1;
414 	struct drr_begin *drrb = arg2;
415 	char *snapname;
416 	dsl_dataset_t *ds, *hds;
417 
418 	snapname = strchr(drrb->drr_toname, '@') + 1;
419 
420 	dsl_dataset_snapshot_sync(os, snapname, tx);
421 
422 	/* set snapshot's creation time and guid */
423 	hds = os->os->os_dsl_dataset;
424 	VERIFY(0 == dsl_dataset_open_obj(hds->ds_dir->dd_pool,
425 	    hds->ds_phys->ds_prev_snap_obj, NULL,
426 	    DS_MODE_PRIMARY | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
427 	    FTAG, &ds));
428 
429 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
430 	ds->ds_phys->ds_creation_time = drrb->drr_creation_time;
431 	ds->ds_phys->ds_guid = drrb->drr_toguid;
432 	ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
433 
434 	dsl_dataset_close(ds, DS_MODE_PRIMARY, FTAG);
435 
436 	dmu_buf_will_dirty(hds->ds_dbuf, tx);
437 	hds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
438 }
439 
440 static void *
441 restore_read(struct restorearg *ra, int len)
442 {
443 	void *rv;
444 
445 	/* some things will require 8-byte alignment, so everything must */
446 	ASSERT3U(len % 8, ==, 0);
447 
448 	while (ra->buflen - ra->bufoff < len) {
449 		ssize_t resid;
450 		int leftover = ra->buflen - ra->bufoff;
451 
452 		(void) memmove(ra->buf, ra->buf + ra->bufoff, leftover);
453 		ra->err = vn_rdwr(UIO_READ, ra->vp,
454 		    (caddr_t)ra->buf + leftover, ra->bufsize - leftover,
455 		    ra->voff, UIO_SYSSPACE, FAPPEND,
456 		    RLIM64_INFINITY, CRED(), &resid);
457 
458 		ra->voff += ra->bufsize - leftover - resid;
459 		ra->buflen = ra->bufsize - resid;
460 		ra->bufoff = 0;
461 		if (resid == ra->bufsize - leftover)
462 			ra->err = EINVAL;
463 		if (ra->err)
464 			return (NULL);
465 		/* Could compute checksum here? */
466 	}
467 
468 	ASSERT3U(ra->bufoff % 8, ==, 0);
469 	ASSERT3U(ra->buflen - ra->bufoff, >=, len);
470 	rv = ra->buf + ra->bufoff;
471 	ra->bufoff += len;
472 	if (ra->byteswap)
473 		fletcher_4_incremental_byteswap(rv, len, &ra->zc);
474 	else
475 		fletcher_4_incremental_native(rv, len, &ra->zc);
476 	return (rv);
477 }
478 
479 static void
480 backup_byteswap(dmu_replay_record_t *drr)
481 {
482 #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
483 #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
484 	drr->drr_type = BSWAP_32(drr->drr_type);
485 	switch (drr->drr_type) {
486 	case DRR_BEGIN:
487 		DO64(drr_begin.drr_magic);
488 		DO64(drr_begin.drr_version);
489 		DO64(drr_begin.drr_creation_time);
490 		DO32(drr_begin.drr_type);
491 		DO64(drr_begin.drr_toguid);
492 		DO64(drr_begin.drr_fromguid);
493 		break;
494 	case DRR_OBJECT:
495 		DO64(drr_object.drr_object);
496 		/* DO64(drr_object.drr_allocation_txg); */
497 		DO32(drr_object.drr_type);
498 		DO32(drr_object.drr_bonustype);
499 		DO32(drr_object.drr_blksz);
500 		DO32(drr_object.drr_bonuslen);
501 		break;
502 	case DRR_FREEOBJECTS:
503 		DO64(drr_freeobjects.drr_firstobj);
504 		DO64(drr_freeobjects.drr_numobjs);
505 		break;
506 	case DRR_WRITE:
507 		DO64(drr_write.drr_object);
508 		DO32(drr_write.drr_type);
509 		DO64(drr_write.drr_offset);
510 		DO64(drr_write.drr_length);
511 		break;
512 	case DRR_FREE:
513 		DO64(drr_free.drr_object);
514 		DO64(drr_free.drr_offset);
515 		DO64(drr_free.drr_length);
516 		break;
517 	case DRR_END:
518 		DO64(drr_end.drr_checksum.zc_word[0]);
519 		DO64(drr_end.drr_checksum.zc_word[1]);
520 		DO64(drr_end.drr_checksum.zc_word[2]);
521 		DO64(drr_end.drr_checksum.zc_word[3]);
522 		break;
523 	}
524 #undef DO64
525 #undef DO32
526 }
527 
528 static int
529 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
530 {
531 	int err;
532 	dmu_tx_t *tx;
533 
534 	err = dmu_object_info(os, drro->drr_object, NULL);
535 
536 	if (err != 0 && err != ENOENT)
537 		return (EINVAL);
538 
539 	if (drro->drr_type == DMU_OT_NONE ||
540 	    drro->drr_type >= DMU_OT_NUMTYPES ||
541 	    drro->drr_bonustype >= DMU_OT_NUMTYPES ||
542 	    drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS ||
543 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
544 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
545 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
546 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
547 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
548 		return (EINVAL);
549 	}
550 
551 	tx = dmu_tx_create(os);
552 
553 	if (err == ENOENT) {
554 		/* currently free, want to be allocated */
555 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
556 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 1);
557 		err = dmu_tx_assign(tx, TXG_WAIT);
558 		if (err) {
559 			dmu_tx_abort(tx);
560 			return (err);
561 		}
562 		err = dmu_object_claim(os, drro->drr_object,
563 		    drro->drr_type, drro->drr_blksz,
564 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
565 	} else {
566 		/* currently allocated, want to be allocated */
567 		dmu_tx_hold_bonus(tx, drro->drr_object);
568 		/*
569 		 * We may change blocksize, so need to
570 		 * hold_write
571 		 */
572 		dmu_tx_hold_write(tx, drro->drr_object, 0, 1);
573 		err = dmu_tx_assign(tx, TXG_WAIT);
574 		if (err) {
575 			dmu_tx_abort(tx);
576 			return (err);
577 		}
578 
579 		err = dmu_object_reclaim(os, drro->drr_object,
580 		    drro->drr_type, drro->drr_blksz,
581 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
582 	}
583 	if (err) {
584 		dmu_tx_commit(tx);
585 		return (EINVAL);
586 	}
587 
588 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx);
589 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
590 
591 	if (drro->drr_bonuslen) {
592 		dmu_buf_t *db;
593 		void *data;
594 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
595 		dmu_buf_will_dirty(db, tx);
596 
597 		ASSERT3U(db->db_size, ==, drro->drr_bonuslen);
598 		data = restore_read(ra, P2ROUNDUP(db->db_size, 8));
599 		if (data == NULL) {
600 			dmu_tx_commit(tx);
601 			return (ra->err);
602 		}
603 		bcopy(data, db->db_data, db->db_size);
604 		if (ra->byteswap) {
605 			dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
606 			    drro->drr_bonuslen);
607 		}
608 		dmu_buf_rele(db, FTAG);
609 	}
610 	dmu_tx_commit(tx);
611 	return (0);
612 }
613 
614 /* ARGSUSED */
615 static int
616 restore_freeobjects(struct restorearg *ra, objset_t *os,
617     struct drr_freeobjects *drrfo)
618 {
619 	uint64_t obj;
620 
621 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
622 		return (EINVAL);
623 
624 	for (obj = drrfo->drr_firstobj;
625 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
626 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
627 		dmu_tx_t *tx;
628 		int err;
629 
630 		if (dmu_object_info(os, obj, NULL) != 0)
631 			continue;
632 
633 		tx = dmu_tx_create(os);
634 		dmu_tx_hold_bonus(tx, obj);
635 		err = dmu_tx_assign(tx, TXG_WAIT);
636 		if (err) {
637 			dmu_tx_abort(tx);
638 			return (err);
639 		}
640 		err = dmu_object_free(os, obj, tx);
641 		dmu_tx_commit(tx);
642 		if (err && err != ENOENT)
643 			return (EINVAL);
644 	}
645 	return (0);
646 }
647 
648 static int
649 restore_write(struct restorearg *ra, objset_t *os,
650     struct drr_write *drrw)
651 {
652 	dmu_tx_t *tx;
653 	void *data;
654 	int err;
655 
656 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
657 	    drrw->drr_type >= DMU_OT_NUMTYPES)
658 		return (EINVAL);
659 
660 	data = restore_read(ra, drrw->drr_length);
661 	if (data == NULL)
662 		return (ra->err);
663 
664 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
665 		return (EINVAL);
666 
667 	tx = dmu_tx_create(os);
668 
669 	dmu_tx_hold_write(tx, drrw->drr_object,
670 	    drrw->drr_offset, drrw->drr_length);
671 	err = dmu_tx_assign(tx, TXG_WAIT);
672 	if (err) {
673 		dmu_tx_abort(tx);
674 		return (err);
675 	}
676 	if (ra->byteswap)
677 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
678 	dmu_write(os, drrw->drr_object,
679 	    drrw->drr_offset, drrw->drr_length, data, tx);
680 	dmu_tx_commit(tx);
681 	return (0);
682 }
683 
684 /* ARGSUSED */
685 static int
686 restore_free(struct restorearg *ra, objset_t *os,
687     struct drr_free *drrf)
688 {
689 	dmu_tx_t *tx;
690 	int err;
691 
692 	if (drrf->drr_length != -1ULL &&
693 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
694 		return (EINVAL);
695 
696 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
697 		return (EINVAL);
698 
699 	tx = dmu_tx_create(os);
700 
701 	dmu_tx_hold_free(tx, drrf->drr_object,
702 	    drrf->drr_offset, drrf->drr_length);
703 	err = dmu_tx_assign(tx, TXG_WAIT);
704 	if (err) {
705 		dmu_tx_abort(tx);
706 		return (err);
707 	}
708 	err = dmu_free_range(os, drrf->drr_object,
709 	    drrf->drr_offset, drrf->drr_length, tx);
710 	dmu_tx_commit(tx);
711 	return (err);
712 }
713 
714 int
715 dmu_recvbackup(char *tosnap, struct drr_begin *drrb, uint64_t *sizep,
716     boolean_t force, vnode_t *vp, uint64_t voffset)
717 {
718 	struct restorearg ra;
719 	dmu_replay_record_t *drr;
720 	char *cp;
721 	objset_t *os = NULL;
722 	zio_cksum_t pzc;
723 
724 	bzero(&ra, sizeof (ra));
725 	ra.vp = vp;
726 	ra.voff = voffset;
727 	ra.bufsize = 1<<20;
728 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
729 
730 	if (drrb->drr_magic == DMU_BACKUP_MAGIC) {
731 		ra.byteswap = FALSE;
732 	} else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
733 		ra.byteswap = TRUE;
734 	} else {
735 		ra.err = EINVAL;
736 		goto out;
737 	}
738 
739 	/*
740 	 * NB: this assumes that struct drr_begin will be the largest in
741 	 * dmu_replay_record_t's drr_u, and thus we don't need to pad it
742 	 * with zeros to make it the same length as we wrote out.
743 	 */
744 	((dmu_replay_record_t *)ra.buf)->drr_type = DRR_BEGIN;
745 	((dmu_replay_record_t *)ra.buf)->drr_pad = 0;
746 	((dmu_replay_record_t *)ra.buf)->drr_u.drr_begin = *drrb;
747 	if (ra.byteswap) {
748 		fletcher_4_incremental_byteswap(ra.buf,
749 		    sizeof (dmu_replay_record_t), &ra.zc);
750 	} else {
751 		fletcher_4_incremental_native(ra.buf,
752 		    sizeof (dmu_replay_record_t), &ra.zc);
753 	}
754 	(void) strcpy(drrb->drr_toname, tosnap); /* for the sync funcs */
755 
756 	if (ra.byteswap) {
757 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
758 		drrb->drr_version = BSWAP_64(drrb->drr_version);
759 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
760 		drrb->drr_type = BSWAP_32(drrb->drr_type);
761 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
762 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
763 	}
764 
765 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
766 
767 	if (drrb->drr_version != DMU_BACKUP_VERSION ||
768 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
769 	    strchr(drrb->drr_toname, '@') == NULL) {
770 		ra.err = EINVAL;
771 		goto out;
772 	}
773 
774 	/*
775 	 * Process the begin in syncing context.
776 	 */
777 	if (drrb->drr_fromguid) {
778 		/* incremental backup */
779 		dsl_dataset_t *ds = NULL;
780 
781 		cp = strchr(tosnap, '@');
782 		*cp = '\0';
783 		ra.err = dsl_dataset_open(tosnap, DS_MODE_EXCLUSIVE, FTAG, &ds);
784 		*cp = '@';
785 		if (ra.err)
786 			goto out;
787 
788 		/*
789 		 * Only do the rollback if the most recent snapshot
790 		 * matches the incremental source
791 		 */
792 		if (force) {
793 			if (ds->ds_prev == NULL ||
794 			    ds->ds_prev->ds_phys->ds_guid !=
795 			    drrb->drr_fromguid) {
796 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
797 				return (ENODEV);
798 			}
799 			(void) dsl_dataset_rollback(ds);
800 		}
801 		ra.err = dsl_sync_task_do(ds->ds_dir->dd_pool,
802 		    replay_incremental_check, replay_incremental_sync,
803 		    ds, drrb, 1);
804 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
805 	} else {
806 		/* full backup */
807 		dsl_dir_t *dd = NULL;
808 		const char *tail;
809 
810 		/* can't restore full backup into topmost fs, for now */
811 		if (strrchr(drrb->drr_toname, '/') == NULL) {
812 			ra.err = EINVAL;
813 			goto out;
814 		}
815 
816 		cp = strchr(tosnap, '@');
817 		*cp = '\0';
818 		ra.err = dsl_dir_open(tosnap, FTAG, &dd, &tail);
819 		*cp = '@';
820 		if (ra.err)
821 			goto out;
822 		if (tail == NULL) {
823 			ra.err = EEXIST;
824 			goto out;
825 		}
826 
827 		ra.err = dsl_sync_task_do(dd->dd_pool, replay_full_check,
828 		    replay_full_sync, dd, drrb, 5);
829 		dsl_dir_close(dd, FTAG);
830 	}
831 	if (ra.err)
832 		goto out;
833 
834 	/*
835 	 * Open the objset we are modifying.
836 	 */
837 
838 	cp = strchr(tosnap, '@');
839 	*cp = '\0';
840 	ra.err = dmu_objset_open(tosnap, DMU_OST_ANY,
841 	    DS_MODE_PRIMARY | DS_MODE_INCONSISTENT, &os);
842 	*cp = '@';
843 	ASSERT3U(ra.err, ==, 0);
844 
845 	/*
846 	 * Read records and process them.
847 	 */
848 	pzc = ra.zc;
849 	while (ra.err == 0 &&
850 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
851 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
852 			ra.err = EINTR;
853 			goto out;
854 		}
855 
856 		if (ra.byteswap)
857 			backup_byteswap(drr);
858 
859 		switch (drr->drr_type) {
860 		case DRR_OBJECT:
861 		{
862 			/*
863 			 * We need to make a copy of the record header,
864 			 * because restore_{object,write} may need to
865 			 * restore_read(), which will invalidate drr.
866 			 */
867 			struct drr_object drro = drr->drr_u.drr_object;
868 			ra.err = restore_object(&ra, os, &drro);
869 			break;
870 		}
871 		case DRR_FREEOBJECTS:
872 		{
873 			struct drr_freeobjects drrfo =
874 			    drr->drr_u.drr_freeobjects;
875 			ra.err = restore_freeobjects(&ra, os, &drrfo);
876 			break;
877 		}
878 		case DRR_WRITE:
879 		{
880 			struct drr_write drrw = drr->drr_u.drr_write;
881 			ra.err = restore_write(&ra, os, &drrw);
882 			break;
883 		}
884 		case DRR_FREE:
885 		{
886 			struct drr_free drrf = drr->drr_u.drr_free;
887 			ra.err = restore_free(&ra, os, &drrf);
888 			break;
889 		}
890 		case DRR_END:
891 		{
892 			struct drr_end drre = drr->drr_u.drr_end;
893 			/*
894 			 * We compare against the *previous* checksum
895 			 * value, because the stored checksum is of
896 			 * everything before the DRR_END record.
897 			 */
898 			if (drre.drr_checksum.zc_word[0] != 0 &&
899 			    !ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pzc)) {
900 				ra.err = ECKSUM;
901 				goto out;
902 			}
903 
904 			ra.err = dsl_sync_task_do(dmu_objset_ds(os)->
905 			    ds_dir->dd_pool, replay_end_check, replay_end_sync,
906 			    os, drrb, 3);
907 			goto out;
908 		}
909 		default:
910 			ra.err = EINVAL;
911 			goto out;
912 		}
913 		pzc = ra.zc;
914 	}
915 
916 out:
917 	if (os)
918 		dmu_objset_close(os);
919 
920 	/*
921 	 * Make sure we don't rollback/destroy unless we actually
922 	 * processed the begin properly.  'os' will only be set if this
923 	 * is the case.
924 	 */
925 	if (ra.err && os && tosnap && strchr(tosnap, '@')) {
926 		/*
927 		 * rollback or destroy what we created, so we don't
928 		 * leave it in the restoring state.
929 		 */
930 		dsl_dataset_t *ds;
931 		int err;
932 
933 		cp = strchr(tosnap, '@');
934 		*cp = '\0';
935 		err = dsl_dataset_open(tosnap,
936 		    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT,
937 		    FTAG, &ds);
938 		if (err == 0) {
939 			txg_wait_synced(ds->ds_dir->dd_pool, 0);
940 			if (drrb->drr_fromguid) {
941 				/* incremental: rollback to most recent snap */
942 				(void) dsl_dataset_rollback(ds);
943 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
944 			} else {
945 				/* full: destroy whole fs */
946 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
947 				(void) dsl_dataset_destroy(tosnap);
948 			}
949 		}
950 		*cp = '@';
951 	}
952 
953 	kmem_free(ra.buf, ra.bufsize);
954 	if (sizep)
955 		*sizep = ra.voff;
956 	return (ra.err);
957 }
958