xref: /titanic_52/usr/src/uts/common/fs/zfs/dmu_send.c (revision 9df258ac654d4990e45ba8bff1661d2d0d3b0d2a)
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; obj++) {
626 		dmu_tx_t *tx;
627 		int err;
628 
629 		if (dmu_object_info(os, obj, NULL) != 0)
630 			continue;
631 
632 		tx = dmu_tx_create(os);
633 		dmu_tx_hold_bonus(tx, obj);
634 		err = dmu_tx_assign(tx, TXG_WAIT);
635 		if (err) {
636 			dmu_tx_abort(tx);
637 			return (err);
638 		}
639 		err = dmu_object_free(os, obj, tx);
640 		dmu_tx_commit(tx);
641 		if (err && err != ENOENT)
642 			return (EINVAL);
643 	}
644 	return (0);
645 }
646 
647 static int
648 restore_write(struct restorearg *ra, objset_t *os,
649     struct drr_write *drrw)
650 {
651 	dmu_tx_t *tx;
652 	void *data;
653 	int err;
654 
655 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
656 	    drrw->drr_type >= DMU_OT_NUMTYPES)
657 		return (EINVAL);
658 
659 	data = restore_read(ra, drrw->drr_length);
660 	if (data == NULL)
661 		return (ra->err);
662 
663 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
664 		return (EINVAL);
665 
666 	tx = dmu_tx_create(os);
667 
668 	dmu_tx_hold_write(tx, drrw->drr_object,
669 	    drrw->drr_offset, drrw->drr_length);
670 	err = dmu_tx_assign(tx, TXG_WAIT);
671 	if (err) {
672 		dmu_tx_abort(tx);
673 		return (err);
674 	}
675 	if (ra->byteswap)
676 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
677 	dmu_write(os, drrw->drr_object,
678 	    drrw->drr_offset, drrw->drr_length, data, tx);
679 	dmu_tx_commit(tx);
680 	return (0);
681 }
682 
683 /* ARGSUSED */
684 static int
685 restore_free(struct restorearg *ra, objset_t *os,
686     struct drr_free *drrf)
687 {
688 	dmu_tx_t *tx;
689 	int err;
690 
691 	if (drrf->drr_length != -1ULL &&
692 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
693 		return (EINVAL);
694 
695 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
696 		return (EINVAL);
697 
698 	tx = dmu_tx_create(os);
699 
700 	dmu_tx_hold_free(tx, drrf->drr_object,
701 	    drrf->drr_offset, drrf->drr_length);
702 	err = dmu_tx_assign(tx, TXG_WAIT);
703 	if (err) {
704 		dmu_tx_abort(tx);
705 		return (err);
706 	}
707 	err = dmu_free_range(os, drrf->drr_object,
708 	    drrf->drr_offset, drrf->drr_length, tx);
709 	dmu_tx_commit(tx);
710 	return (err);
711 }
712 
713 int
714 dmu_recvbackup(char *tosnap, struct drr_begin *drrb, uint64_t *sizep,
715     boolean_t force, vnode_t *vp, uint64_t voffset)
716 {
717 	struct restorearg ra;
718 	dmu_replay_record_t *drr;
719 	char *cp;
720 	objset_t *os = NULL;
721 	zio_cksum_t pzc;
722 
723 	bzero(&ra, sizeof (ra));
724 	ra.vp = vp;
725 	ra.voff = voffset;
726 	ra.bufsize = 1<<20;
727 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
728 
729 	if (drrb->drr_magic == DMU_BACKUP_MAGIC) {
730 		ra.byteswap = FALSE;
731 	} else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
732 		ra.byteswap = TRUE;
733 	} else {
734 		ra.err = EINVAL;
735 		goto out;
736 	}
737 
738 	/*
739 	 * NB: this assumes that struct drr_begin will be the largest in
740 	 * dmu_replay_record_t's drr_u, and thus we don't need to pad it
741 	 * with zeros to make it the same length as we wrote out.
742 	 */
743 	((dmu_replay_record_t *)ra.buf)->drr_type = DRR_BEGIN;
744 	((dmu_replay_record_t *)ra.buf)->drr_pad = 0;
745 	((dmu_replay_record_t *)ra.buf)->drr_u.drr_begin = *drrb;
746 	if (ra.byteswap) {
747 		fletcher_4_incremental_byteswap(ra.buf,
748 		    sizeof (dmu_replay_record_t), &ra.zc);
749 	} else {
750 		fletcher_4_incremental_native(ra.buf,
751 		    sizeof (dmu_replay_record_t), &ra.zc);
752 	}
753 	(void) strcpy(drrb->drr_toname, tosnap); /* for the sync funcs */
754 
755 	if (ra.byteswap) {
756 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
757 		drrb->drr_version = BSWAP_64(drrb->drr_version);
758 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
759 		drrb->drr_type = BSWAP_32(drrb->drr_type);
760 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
761 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
762 	}
763 
764 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
765 
766 	if (drrb->drr_version != DMU_BACKUP_VERSION ||
767 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
768 	    strchr(drrb->drr_toname, '@') == NULL) {
769 		ra.err = EINVAL;
770 		goto out;
771 	}
772 
773 	/*
774 	 * Process the begin in syncing context.
775 	 */
776 	if (drrb->drr_fromguid) {
777 		/* incremental backup */
778 		dsl_dataset_t *ds = NULL;
779 
780 		cp = strchr(tosnap, '@');
781 		*cp = '\0';
782 		ra.err = dsl_dataset_open(tosnap, DS_MODE_EXCLUSIVE, FTAG, &ds);
783 		*cp = '@';
784 		if (ra.err)
785 			goto out;
786 
787 		/*
788 		 * Only do the rollback if the most recent snapshot
789 		 * matches the incremental source
790 		 */
791 		if (force) {
792 			if (ds->ds_prev->ds_phys->ds_guid !=
793 			    drrb->drr_fromguid) {
794 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
795 				return (ENODEV);
796 			}
797 			(void) dsl_dataset_rollback(ds);
798 		}
799 		ra.err = dsl_sync_task_do(ds->ds_dir->dd_pool,
800 		    replay_incremental_check, replay_incremental_sync,
801 		    ds, drrb, 1);
802 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
803 	} else {
804 		/* full backup */
805 		dsl_dir_t *dd = NULL;
806 		const char *tail;
807 
808 		/* can't restore full backup into topmost fs, for now */
809 		if (strrchr(drrb->drr_toname, '/') == NULL) {
810 			ra.err = EINVAL;
811 			goto out;
812 		}
813 
814 		cp = strchr(tosnap, '@');
815 		*cp = '\0';
816 		ra.err = dsl_dir_open(tosnap, FTAG, &dd, &tail);
817 		*cp = '@';
818 		if (ra.err)
819 			goto out;
820 		if (tail == NULL) {
821 			ra.err = EEXIST;
822 			goto out;
823 		}
824 
825 		ra.err = dsl_sync_task_do(dd->dd_pool, replay_full_check,
826 		    replay_full_sync, dd, drrb, 5);
827 		dsl_dir_close(dd, FTAG);
828 	}
829 	if (ra.err)
830 		goto out;
831 
832 	/*
833 	 * Open the objset we are modifying.
834 	 */
835 
836 	cp = strchr(tosnap, '@');
837 	*cp = '\0';
838 	ra.err = dmu_objset_open(tosnap, DMU_OST_ANY,
839 	    DS_MODE_PRIMARY | DS_MODE_INCONSISTENT, &os);
840 	*cp = '@';
841 	ASSERT3U(ra.err, ==, 0);
842 
843 	/*
844 	 * Read records and process them.
845 	 */
846 	pzc = ra.zc;
847 	while (ra.err == 0 &&
848 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
849 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
850 			ra.err = EINTR;
851 			goto out;
852 		}
853 
854 		if (ra.byteswap)
855 			backup_byteswap(drr);
856 
857 		switch (drr->drr_type) {
858 		case DRR_OBJECT:
859 		{
860 			/*
861 			 * We need to make a copy of the record header,
862 			 * because restore_{object,write} may need to
863 			 * restore_read(), which will invalidate drr.
864 			 */
865 			struct drr_object drro = drr->drr_u.drr_object;
866 			ra.err = restore_object(&ra, os, &drro);
867 			break;
868 		}
869 		case DRR_FREEOBJECTS:
870 		{
871 			struct drr_freeobjects drrfo =
872 			    drr->drr_u.drr_freeobjects;
873 			ra.err = restore_freeobjects(&ra, os, &drrfo);
874 			break;
875 		}
876 		case DRR_WRITE:
877 		{
878 			struct drr_write drrw = drr->drr_u.drr_write;
879 			ra.err = restore_write(&ra, os, &drrw);
880 			break;
881 		}
882 		case DRR_FREE:
883 		{
884 			struct drr_free drrf = drr->drr_u.drr_free;
885 			ra.err = restore_free(&ra, os, &drrf);
886 			break;
887 		}
888 		case DRR_END:
889 		{
890 			struct drr_end drre = drr->drr_u.drr_end;
891 			/*
892 			 * We compare against the *previous* checksum
893 			 * value, because the stored checksum is of
894 			 * everything before the DRR_END record.
895 			 */
896 			if (drre.drr_checksum.zc_word[0] != 0 &&
897 			    ((drre.drr_checksum.zc_word[0] - pzc.zc_word[0]) |
898 			    (drre.drr_checksum.zc_word[1] - pzc.zc_word[1]) |
899 			    (drre.drr_checksum.zc_word[2] - pzc.zc_word[2]) |
900 			    (drre.drr_checksum.zc_word[3] - pzc.zc_word[3]))) {
901 				ra.err = ECKSUM;
902 				goto out;
903 			}
904 
905 			ra.err = dsl_sync_task_do(dmu_objset_ds(os)->
906 			    ds_dir->dd_pool, replay_end_check, replay_end_sync,
907 			    os, drrb, 3);
908 			goto out;
909 		}
910 		default:
911 			ra.err = EINVAL;
912 			goto out;
913 		}
914 		pzc = ra.zc;
915 	}
916 
917 out:
918 	if (os)
919 		dmu_objset_close(os);
920 
921 	/*
922 	 * Make sure we don't rollback/destroy unless we actually
923 	 * processed the begin properly.  'os' will only be set if this
924 	 * is the case.
925 	 */
926 	if (ra.err && os && tosnap && strchr(tosnap, '@')) {
927 		/*
928 		 * rollback or destroy what we created, so we don't
929 		 * leave it in the restoring state.
930 		 */
931 		dsl_dataset_t *ds;
932 		int err;
933 
934 		cp = strchr(tosnap, '@');
935 		*cp = '\0';
936 		err = dsl_dataset_open(tosnap,
937 		    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT,
938 		    FTAG, &ds);
939 		if (err == 0) {
940 			txg_wait_synced(ds->ds_dir->dd_pool, 0);
941 			if (drrb->drr_fromguid) {
942 				/* incremental: rollback to most recent snap */
943 				(void) dsl_dataset_rollback(ds);
944 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
945 			} else {
946 				/* full: destroy whole fs */
947 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
948 				(void) dsl_dataset_destroy(tosnap);
949 			}
950 		}
951 		*cp = '@';
952 	}
953 
954 	kmem_free(ra.buf, ra.bufsize);
955 	if (sizep)
956 		*sizep = ra.voff;
957 	return (ra.err);
958 }
959