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