xref: /titanic_50/usr/src/uts/common/fs/zfs/dmu_send.c (revision 20c794b39650d115e17a15983b6b82e46238cf45)
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 2007 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 		kmem_free(drr, sizeof (dmu_replay_record_t));
269 		return (err);
270 	}
271 
272 	bzero(drr, sizeof (dmu_replay_record_t));
273 	drr->drr_type = DRR_END;
274 	drr->drr_u.drr_end.drr_checksum = ba.zc;
275 
276 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
277 		kmem_free(drr, sizeof (dmu_replay_record_t));
278 		return (ba.err);
279 	}
280 
281 	kmem_free(drr, sizeof (dmu_replay_record_t));
282 
283 	return (0);
284 }
285 
286 struct restorearg {
287 	int err;
288 	int byteswap;
289 	vnode_t *vp;
290 	char *buf;
291 	uint64_t voff;
292 	int buflen; /* number of valid bytes in buf */
293 	int bufoff; /* next offset to read */
294 	int bufsize; /* amount of memory allocated for buf */
295 	zio_cksum_t zc;
296 };
297 
298 /* ARGSUSED */
299 static int
300 replay_incremental_check(void *arg1, void *arg2, dmu_tx_t *tx)
301 {
302 	dsl_dataset_t *ds = arg1;
303 	struct drr_begin *drrb = arg2;
304 	const char *snapname;
305 	int err;
306 	uint64_t val;
307 
308 	/* must already be a snapshot of this fs */
309 	if (ds->ds_phys->ds_prev_snap_obj == 0)
310 		return (ENODEV);
311 
312 	/* most recent snapshot must match fromguid */
313 	if (ds->ds_prev->ds_phys->ds_guid != drrb->drr_fromguid)
314 		return (ENODEV);
315 	/* must not have any changes since most recent snapshot */
316 	if (ds->ds_phys->ds_bp.blk_birth >
317 	    ds->ds_prev->ds_phys->ds_creation_txg)
318 		return (ETXTBSY);
319 
320 	/* new snapshot name must not exist */
321 	snapname = strrchr(drrb->drr_toname, '@');
322 	if (snapname == NULL)
323 		return (EEXIST);
324 
325 	snapname++;
326 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
327 	    ds->ds_phys->ds_snapnames_zapobj, snapname, 8, 1, &val);
328 	if (err == 0)
329 		return (EEXIST);
330 	if (err != ENOENT)
331 		return (err);
332 
333 	return (0);
334 }
335 
336 /* ARGSUSED */
337 static void
338 replay_incremental_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
339 {
340 	dsl_dataset_t *ds = arg1;
341 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
342 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
343 
344 	spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC,
345 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "dataset = %lld",
346 	    ds->ds_phys->ds_dir_obj);
347 }
348 
349 /* ARGSUSED */
350 static int
351 replay_full_check(void *arg1, void *arg2, dmu_tx_t *tx)
352 {
353 	dsl_dir_t *dd = arg1;
354 	struct drr_begin *drrb = arg2;
355 	objset_t *mos = dd->dd_pool->dp_meta_objset;
356 	char *cp;
357 	uint64_t val;
358 	int err;
359 
360 	cp = strchr(drrb->drr_toname, '@');
361 	*cp = '\0';
362 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
363 	    strrchr(drrb->drr_toname, '/') + 1,
364 	    sizeof (uint64_t), 1, &val);
365 	*cp = '@';
366 
367 	if (err != ENOENT)
368 		return (err ? err : EEXIST);
369 
370 	return (0);
371 }
372 
373 static void
374 replay_full_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
375 {
376 	dsl_dir_t *dd = arg1;
377 	struct drr_begin *drrb = arg2;
378 	char *cp;
379 	dsl_dataset_t *ds;
380 	uint64_t dsobj;
381 
382 	cp = strchr(drrb->drr_toname, '@');
383 	*cp = '\0';
384 	dsobj = dsl_dataset_create_sync(dd, strrchr(drrb->drr_toname, '/') + 1,
385 	    NULL, tx);
386 
387 	VERIFY(0 == dsl_dataset_open_obj(dd->dd_pool, dsobj, NULL,
388 	    DS_MODE_EXCLUSIVE, FTAG, &ds));
389 
390 	(void) dmu_objset_create_impl(dsl_dataset_get_spa(ds),
391 	    ds, &ds->ds_phys->ds_bp, drrb->drr_type, tx);
392 
393 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
394 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
395 
396 	spa_history_internal_log(LOG_DS_REPLAY_FULL_SYNC,
397 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "dataset = %lld",
398 	    ds->ds_phys->ds_dir_obj);
399 
400 	*cp = '@';
401 
402 	dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
403 }
404 
405 static int
406 replay_end_check(void *arg1, void *arg2, dmu_tx_t *tx)
407 {
408 	objset_t *os = arg1;
409 	struct drr_begin *drrb = arg2;
410 	char *snapname;
411 
412 	/* XXX verify that drr_toname is in dd */
413 
414 	snapname = strchr(drrb->drr_toname, '@');
415 	if (snapname == NULL)
416 		return (EINVAL);
417 	snapname++;
418 
419 	return (dsl_dataset_snapshot_check(os, snapname, tx));
420 }
421 
422 static void
423 replay_end_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
424 {
425 	objset_t *os = arg1;
426 	struct drr_begin *drrb = arg2;
427 	char *snapname;
428 	dsl_dataset_t *ds, *hds;
429 
430 	snapname = strchr(drrb->drr_toname, '@') + 1;
431 
432 	dsl_dataset_snapshot_sync(os, snapname, cr, tx);
433 
434 	/* set snapshot's creation time and guid */
435 	hds = os->os->os_dsl_dataset;
436 	VERIFY(0 == dsl_dataset_open_obj(hds->ds_dir->dd_pool,
437 	    hds->ds_phys->ds_prev_snap_obj, NULL,
438 	    DS_MODE_PRIMARY | DS_MODE_READONLY | DS_MODE_INCONSISTENT,
439 	    FTAG, &ds));
440 
441 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
442 	ds->ds_phys->ds_creation_time = drrb->drr_creation_time;
443 	ds->ds_phys->ds_guid = drrb->drr_toguid;
444 	ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
445 
446 	/* log the end of the receive */
447 	spa_history_internal_log(LOG_DS_RECEIVE, ds->ds_dir->dd_pool->dp_spa,
448 	    tx, cr, "dataset = %llu", ds->ds_phys->ds_dir_obj);
449 
450 	dsl_dataset_close(ds, DS_MODE_PRIMARY, FTAG);
451 
452 	dmu_buf_will_dirty(hds->ds_dbuf, tx);
453 	hds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
454 }
455 
456 static void *
457 restore_read(struct restorearg *ra, int len)
458 {
459 	void *rv;
460 
461 	/* some things will require 8-byte alignment, so everything must */
462 	ASSERT3U(len % 8, ==, 0);
463 
464 	while (ra->buflen - ra->bufoff < len) {
465 		ssize_t resid;
466 		int leftover = ra->buflen - ra->bufoff;
467 
468 		(void) memmove(ra->buf, ra->buf + ra->bufoff, leftover);
469 		ra->err = vn_rdwr(UIO_READ, ra->vp,
470 		    (caddr_t)ra->buf + leftover, ra->bufsize - leftover,
471 		    ra->voff, UIO_SYSSPACE, FAPPEND,
472 		    RLIM64_INFINITY, CRED(), &resid);
473 
474 		ra->voff += ra->bufsize - leftover - resid;
475 		ra->buflen = ra->bufsize - resid;
476 		ra->bufoff = 0;
477 		if (resid == ra->bufsize - leftover)
478 			ra->err = EINVAL;
479 		if (ra->err)
480 			return (NULL);
481 		/* Could compute checksum here? */
482 	}
483 
484 	ASSERT3U(ra->bufoff % 8, ==, 0);
485 	ASSERT3U(ra->buflen - ra->bufoff, >=, len);
486 	rv = ra->buf + ra->bufoff;
487 	ra->bufoff += len;
488 	if (ra->byteswap)
489 		fletcher_4_incremental_byteswap(rv, len, &ra->zc);
490 	else
491 		fletcher_4_incremental_native(rv, len, &ra->zc);
492 	return (rv);
493 }
494 
495 static void
496 backup_byteswap(dmu_replay_record_t *drr)
497 {
498 #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
499 #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
500 	drr->drr_type = BSWAP_32(drr->drr_type);
501 	switch (drr->drr_type) {
502 	case DRR_BEGIN:
503 		DO64(drr_begin.drr_magic);
504 		DO64(drr_begin.drr_version);
505 		DO64(drr_begin.drr_creation_time);
506 		DO32(drr_begin.drr_type);
507 		DO64(drr_begin.drr_toguid);
508 		DO64(drr_begin.drr_fromguid);
509 		break;
510 	case DRR_OBJECT:
511 		DO64(drr_object.drr_object);
512 		/* DO64(drr_object.drr_allocation_txg); */
513 		DO32(drr_object.drr_type);
514 		DO32(drr_object.drr_bonustype);
515 		DO32(drr_object.drr_blksz);
516 		DO32(drr_object.drr_bonuslen);
517 		break;
518 	case DRR_FREEOBJECTS:
519 		DO64(drr_freeobjects.drr_firstobj);
520 		DO64(drr_freeobjects.drr_numobjs);
521 		break;
522 	case DRR_WRITE:
523 		DO64(drr_write.drr_object);
524 		DO32(drr_write.drr_type);
525 		DO64(drr_write.drr_offset);
526 		DO64(drr_write.drr_length);
527 		break;
528 	case DRR_FREE:
529 		DO64(drr_free.drr_object);
530 		DO64(drr_free.drr_offset);
531 		DO64(drr_free.drr_length);
532 		break;
533 	case DRR_END:
534 		DO64(drr_end.drr_checksum.zc_word[0]);
535 		DO64(drr_end.drr_checksum.zc_word[1]);
536 		DO64(drr_end.drr_checksum.zc_word[2]);
537 		DO64(drr_end.drr_checksum.zc_word[3]);
538 		break;
539 	}
540 #undef DO64
541 #undef DO32
542 }
543 
544 static int
545 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
546 {
547 	int err;
548 	dmu_tx_t *tx;
549 
550 	err = dmu_object_info(os, drro->drr_object, NULL);
551 
552 	if (err != 0 && err != ENOENT)
553 		return (EINVAL);
554 
555 	if (drro->drr_type == DMU_OT_NONE ||
556 	    drro->drr_type >= DMU_OT_NUMTYPES ||
557 	    drro->drr_bonustype >= DMU_OT_NUMTYPES ||
558 	    drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS ||
559 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
560 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
561 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
562 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
563 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
564 		return (EINVAL);
565 	}
566 
567 	tx = dmu_tx_create(os);
568 
569 	if (err == ENOENT) {
570 		/* currently free, want to be allocated */
571 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
572 		dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 1);
573 		err = dmu_tx_assign(tx, TXG_WAIT);
574 		if (err) {
575 			dmu_tx_abort(tx);
576 			return (err);
577 		}
578 		err = dmu_object_claim(os, drro->drr_object,
579 		    drro->drr_type, drro->drr_blksz,
580 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
581 	} else {
582 		/* currently allocated, want to be allocated */
583 		dmu_tx_hold_bonus(tx, drro->drr_object);
584 		/*
585 		 * We may change blocksize, so need to
586 		 * hold_write
587 		 */
588 		dmu_tx_hold_write(tx, drro->drr_object, 0, 1);
589 		err = dmu_tx_assign(tx, TXG_WAIT);
590 		if (err) {
591 			dmu_tx_abort(tx);
592 			return (err);
593 		}
594 
595 		err = dmu_object_reclaim(os, drro->drr_object,
596 		    drro->drr_type, drro->drr_blksz,
597 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
598 	}
599 	if (err) {
600 		dmu_tx_commit(tx);
601 		return (EINVAL);
602 	}
603 
604 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx);
605 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
606 
607 	if (drro->drr_bonuslen) {
608 		dmu_buf_t *db;
609 		void *data;
610 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
611 		dmu_buf_will_dirty(db, tx);
612 
613 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
614 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
615 		if (data == NULL) {
616 			dmu_tx_commit(tx);
617 			return (ra->err);
618 		}
619 		bcopy(data, db->db_data, drro->drr_bonuslen);
620 		if (ra->byteswap) {
621 			dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
622 			    drro->drr_bonuslen);
623 		}
624 		dmu_buf_rele(db, FTAG);
625 	}
626 	dmu_tx_commit(tx);
627 	return (0);
628 }
629 
630 /* ARGSUSED */
631 static int
632 restore_freeobjects(struct restorearg *ra, objset_t *os,
633     struct drr_freeobjects *drrfo)
634 {
635 	uint64_t obj;
636 
637 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
638 		return (EINVAL);
639 
640 	for (obj = drrfo->drr_firstobj;
641 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
642 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
643 		dmu_tx_t *tx;
644 		int err;
645 
646 		if (dmu_object_info(os, obj, NULL) != 0)
647 			continue;
648 
649 		tx = dmu_tx_create(os);
650 		dmu_tx_hold_bonus(tx, obj);
651 		err = dmu_tx_assign(tx, TXG_WAIT);
652 		if (err) {
653 			dmu_tx_abort(tx);
654 			return (err);
655 		}
656 		err = dmu_object_free(os, obj, tx);
657 		dmu_tx_commit(tx);
658 		if (err && err != ENOENT)
659 			return (EINVAL);
660 	}
661 	return (0);
662 }
663 
664 static int
665 restore_write(struct restorearg *ra, objset_t *os,
666     struct drr_write *drrw)
667 {
668 	dmu_tx_t *tx;
669 	void *data;
670 	int err;
671 
672 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
673 	    drrw->drr_type >= DMU_OT_NUMTYPES)
674 		return (EINVAL);
675 
676 	data = restore_read(ra, drrw->drr_length);
677 	if (data == NULL)
678 		return (ra->err);
679 
680 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
681 		return (EINVAL);
682 
683 	tx = dmu_tx_create(os);
684 
685 	dmu_tx_hold_write(tx, drrw->drr_object,
686 	    drrw->drr_offset, drrw->drr_length);
687 	err = dmu_tx_assign(tx, TXG_WAIT);
688 	if (err) {
689 		dmu_tx_abort(tx);
690 		return (err);
691 	}
692 	if (ra->byteswap)
693 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
694 	dmu_write(os, drrw->drr_object,
695 	    drrw->drr_offset, drrw->drr_length, data, tx);
696 	dmu_tx_commit(tx);
697 	return (0);
698 }
699 
700 /* ARGSUSED */
701 static int
702 restore_free(struct restorearg *ra, objset_t *os,
703     struct drr_free *drrf)
704 {
705 	dmu_tx_t *tx;
706 	int err;
707 
708 	if (drrf->drr_length != -1ULL &&
709 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
710 		return (EINVAL);
711 
712 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
713 		return (EINVAL);
714 
715 	tx = dmu_tx_create(os);
716 
717 	dmu_tx_hold_free(tx, drrf->drr_object,
718 	    drrf->drr_offset, drrf->drr_length);
719 	err = dmu_tx_assign(tx, TXG_WAIT);
720 	if (err) {
721 		dmu_tx_abort(tx);
722 		return (err);
723 	}
724 	err = dmu_free_range(os, drrf->drr_object,
725 	    drrf->drr_offset, drrf->drr_length, tx);
726 	dmu_tx_commit(tx);
727 	return (err);
728 }
729 
730 int
731 dmu_recvbackup(char *tosnap, struct drr_begin *drrb, uint64_t *sizep,
732     boolean_t force, vnode_t *vp, uint64_t voffset)
733 {
734 	struct restorearg ra;
735 	dmu_replay_record_t *drr;
736 	char *cp;
737 	objset_t *os = NULL;
738 	zio_cksum_t pzc;
739 
740 	bzero(&ra, sizeof (ra));
741 	ra.vp = vp;
742 	ra.voff = voffset;
743 	ra.bufsize = 1<<20;
744 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
745 
746 	if (drrb->drr_magic == DMU_BACKUP_MAGIC) {
747 		ra.byteswap = FALSE;
748 	} else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
749 		ra.byteswap = TRUE;
750 	} else {
751 		ra.err = EINVAL;
752 		goto out;
753 	}
754 
755 	/*
756 	 * NB: this assumes that struct drr_begin will be the largest in
757 	 * dmu_replay_record_t's drr_u, and thus we don't need to pad it
758 	 * with zeros to make it the same length as we wrote out.
759 	 */
760 	((dmu_replay_record_t *)ra.buf)->drr_type = DRR_BEGIN;
761 	((dmu_replay_record_t *)ra.buf)->drr_pad = 0;
762 	((dmu_replay_record_t *)ra.buf)->drr_u.drr_begin = *drrb;
763 	if (ra.byteswap) {
764 		fletcher_4_incremental_byteswap(ra.buf,
765 		    sizeof (dmu_replay_record_t), &ra.zc);
766 	} else {
767 		fletcher_4_incremental_native(ra.buf,
768 		    sizeof (dmu_replay_record_t), &ra.zc);
769 	}
770 	(void) strcpy(drrb->drr_toname, tosnap); /* for the sync funcs */
771 
772 	if (ra.byteswap) {
773 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
774 		drrb->drr_version = BSWAP_64(drrb->drr_version);
775 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
776 		drrb->drr_type = BSWAP_32(drrb->drr_type);
777 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
778 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
779 	}
780 
781 	ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
782 
783 	if (drrb->drr_version != DMU_BACKUP_VERSION ||
784 	    drrb->drr_type >= DMU_OST_NUMTYPES ||
785 	    strchr(drrb->drr_toname, '@') == NULL) {
786 		ra.err = EINVAL;
787 		goto out;
788 	}
789 
790 	/*
791 	 * Process the begin in syncing context.
792 	 */
793 	if (drrb->drr_fromguid) {
794 		/* incremental backup */
795 		dsl_dataset_t *ds = NULL;
796 
797 		cp = strchr(tosnap, '@');
798 		*cp = '\0';
799 		ra.err = dsl_dataset_open(tosnap, DS_MODE_EXCLUSIVE, FTAG, &ds);
800 		*cp = '@';
801 		if (ra.err)
802 			goto out;
803 
804 		/*
805 		 * Only do the rollback if the most recent snapshot
806 		 * matches the incremental source
807 		 */
808 		if (force) {
809 			if (ds->ds_prev == NULL ||
810 			    ds->ds_prev->ds_phys->ds_guid !=
811 			    drrb->drr_fromguid) {
812 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
813 				kmem_free(ra.buf, ra.bufsize);
814 				return (ENODEV);
815 			}
816 			(void) dsl_dataset_rollback(ds);
817 		}
818 		ra.err = dsl_sync_task_do(ds->ds_dir->dd_pool,
819 		    replay_incremental_check, replay_incremental_sync,
820 		    ds, drrb, 1);
821 		dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
822 	} else {
823 		/* full backup */
824 		dsl_dir_t *dd = NULL;
825 		const char *tail;
826 
827 		/* can't restore full backup into topmost fs, for now */
828 		if (strrchr(drrb->drr_toname, '/') == NULL) {
829 			ra.err = EINVAL;
830 			goto out;
831 		}
832 
833 		cp = strchr(tosnap, '@');
834 		*cp = '\0';
835 		ra.err = dsl_dir_open(tosnap, FTAG, &dd, &tail);
836 		*cp = '@';
837 		if (ra.err)
838 			goto out;
839 		if (tail == NULL) {
840 			ra.err = EEXIST;
841 			goto out;
842 		}
843 
844 		ra.err = dsl_sync_task_do(dd->dd_pool, replay_full_check,
845 		    replay_full_sync, dd, drrb, 5);
846 		dsl_dir_close(dd, FTAG);
847 	}
848 	if (ra.err)
849 		goto out;
850 
851 	/*
852 	 * Open the objset we are modifying.
853 	 */
854 
855 	cp = strchr(tosnap, '@');
856 	*cp = '\0';
857 	ra.err = dmu_objset_open(tosnap, DMU_OST_ANY,
858 	    DS_MODE_PRIMARY | DS_MODE_INCONSISTENT, &os);
859 	*cp = '@';
860 	ASSERT3U(ra.err, ==, 0);
861 
862 	/*
863 	 * Read records and process them.
864 	 */
865 	pzc = ra.zc;
866 	while (ra.err == 0 &&
867 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
868 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
869 			ra.err = EINTR;
870 			goto out;
871 		}
872 
873 		if (ra.byteswap)
874 			backup_byteswap(drr);
875 
876 		switch (drr->drr_type) {
877 		case DRR_OBJECT:
878 		{
879 			/*
880 			 * We need to make a copy of the record header,
881 			 * because restore_{object,write} may need to
882 			 * restore_read(), which will invalidate drr.
883 			 */
884 			struct drr_object drro = drr->drr_u.drr_object;
885 			ra.err = restore_object(&ra, os, &drro);
886 			break;
887 		}
888 		case DRR_FREEOBJECTS:
889 		{
890 			struct drr_freeobjects drrfo =
891 			    drr->drr_u.drr_freeobjects;
892 			ra.err = restore_freeobjects(&ra, os, &drrfo);
893 			break;
894 		}
895 		case DRR_WRITE:
896 		{
897 			struct drr_write drrw = drr->drr_u.drr_write;
898 			ra.err = restore_write(&ra, os, &drrw);
899 			break;
900 		}
901 		case DRR_FREE:
902 		{
903 			struct drr_free drrf = drr->drr_u.drr_free;
904 			ra.err = restore_free(&ra, os, &drrf);
905 			break;
906 		}
907 		case DRR_END:
908 		{
909 			struct drr_end drre = drr->drr_u.drr_end;
910 			/*
911 			 * We compare against the *previous* checksum
912 			 * value, because the stored checksum is of
913 			 * everything before the DRR_END record.
914 			 */
915 			if (drre.drr_checksum.zc_word[0] != 0 &&
916 			    !ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pzc)) {
917 				ra.err = ECKSUM;
918 				goto out;
919 			}
920 
921 			ra.err = dsl_sync_task_do(dmu_objset_ds(os)->
922 			    ds_dir->dd_pool, replay_end_check, replay_end_sync,
923 			    os, drrb, 3);
924 			goto out;
925 		}
926 		default:
927 			ra.err = EINVAL;
928 			goto out;
929 		}
930 		pzc = ra.zc;
931 	}
932 
933 out:
934 	if (os)
935 		dmu_objset_close(os);
936 
937 	/*
938 	 * Make sure we don't rollback/destroy unless we actually
939 	 * processed the begin properly.  'os' will only be set if this
940 	 * is the case.
941 	 */
942 	if (ra.err && os && tosnap && strchr(tosnap, '@')) {
943 		/*
944 		 * rollback or destroy what we created, so we don't
945 		 * leave it in the restoring state.
946 		 */
947 		dsl_dataset_t *ds;
948 		int err;
949 
950 		cp = strchr(tosnap, '@');
951 		*cp = '\0';
952 		err = dsl_dataset_open(tosnap,
953 		    DS_MODE_EXCLUSIVE | DS_MODE_INCONSISTENT,
954 		    FTAG, &ds);
955 		if (err == 0) {
956 			txg_wait_synced(ds->ds_dir->dd_pool, 0);
957 			if (drrb->drr_fromguid) {
958 				/* incremental: rollback to most recent snap */
959 				(void) dsl_dataset_rollback(ds);
960 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
961 			} else {
962 				/* full: destroy whole fs */
963 				dsl_dataset_close(ds, DS_MODE_EXCLUSIVE, FTAG);
964 				(void) dsl_dataset_destroy(tosnap);
965 			}
966 		}
967 		*cp = '@';
968 	}
969 
970 	kmem_free(ra.buf, ra.bufsize);
971 	if (sizep)
972 		*sizep = ra.voff;
973 	return (ra.err);
974 }
975