xref: /titanic_41/usr/src/uts/common/fs/zfs/dmu_send.c (revision 7d1e83948cb684521e72cab96020be241508f449)
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 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <sys/dmu.h>
27 #include <sys/dmu_impl.h>
28 #include <sys/dmu_tx.h>
29 #include <sys/dbuf.h>
30 #include <sys/dnode.h>
31 #include <sys/zfs_context.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/dmu_traverse.h>
34 #include <sys/dsl_dataset.h>
35 #include <sys/dsl_dir.h>
36 #include <sys/dsl_pool.h>
37 #include <sys/dsl_synctask.h>
38 #include <sys/zfs_ioctl.h>
39 #include <sys/zap.h>
40 #include <sys/zio_checksum.h>
41 
42 static char *dmu_recv_tag = "dmu_recv_tag";
43 
44 struct backuparg {
45 	dmu_replay_record_t *drr;
46 	vnode_t *vp;
47 	offset_t *off;
48 	objset_t *os;
49 	zio_cksum_t zc;
50 	int err;
51 };
52 
53 static int
54 dump_bytes(struct backuparg *ba, void *buf, int len)
55 {
56 	ssize_t resid; /* have to get resid to get detailed errno */
57 	ASSERT3U(len % 8, ==, 0);
58 
59 	fletcher_4_incremental_native(buf, len, &ba->zc);
60 	ba->err = vn_rdwr(UIO_WRITE, ba->vp,
61 	    (caddr_t)buf, len,
62 	    0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
63 	*ba->off += len;
64 	return (ba->err);
65 }
66 
67 static int
68 dump_free(struct backuparg *ba, uint64_t object, uint64_t offset,
69     uint64_t length)
70 {
71 	/* write a FREE record */
72 	bzero(ba->drr, sizeof (dmu_replay_record_t));
73 	ba->drr->drr_type = DRR_FREE;
74 	ba->drr->drr_u.drr_free.drr_object = object;
75 	ba->drr->drr_u.drr_free.drr_offset = offset;
76 	ba->drr->drr_u.drr_free.drr_length = length;
77 
78 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
79 		return (EINTR);
80 	return (0);
81 }
82 
83 static int
84 dump_data(struct backuparg *ba, dmu_object_type_t type,
85     uint64_t object, uint64_t offset, int blksz, void *data)
86 {
87 	/* write a DATA record */
88 	bzero(ba->drr, sizeof (dmu_replay_record_t));
89 	ba->drr->drr_type = DRR_WRITE;
90 	ba->drr->drr_u.drr_write.drr_object = object;
91 	ba->drr->drr_u.drr_write.drr_type = type;
92 	ba->drr->drr_u.drr_write.drr_offset = offset;
93 	ba->drr->drr_u.drr_write.drr_length = blksz;
94 
95 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
96 		return (EINTR);
97 	if (dump_bytes(ba, data, blksz))
98 		return (EINTR);
99 	return (0);
100 }
101 
102 static int
103 dump_freeobjects(struct backuparg *ba, uint64_t firstobj, uint64_t numobjs)
104 {
105 	/* write a FREEOBJECTS record */
106 	bzero(ba->drr, sizeof (dmu_replay_record_t));
107 	ba->drr->drr_type = DRR_FREEOBJECTS;
108 	ba->drr->drr_u.drr_freeobjects.drr_firstobj = firstobj;
109 	ba->drr->drr_u.drr_freeobjects.drr_numobjs = numobjs;
110 
111 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
112 		return (EINTR);
113 	return (0);
114 }
115 
116 static int
117 dump_dnode(struct backuparg *ba, uint64_t object, dnode_phys_t *dnp)
118 {
119 	if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
120 		return (dump_freeobjects(ba, object, 1));
121 
122 	/* write an OBJECT record */
123 	bzero(ba->drr, sizeof (dmu_replay_record_t));
124 	ba->drr->drr_type = DRR_OBJECT;
125 	ba->drr->drr_u.drr_object.drr_object = object;
126 	ba->drr->drr_u.drr_object.drr_type = dnp->dn_type;
127 	ba->drr->drr_u.drr_object.drr_bonustype = dnp->dn_bonustype;
128 	ba->drr->drr_u.drr_object.drr_blksz =
129 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
130 	ba->drr->drr_u.drr_object.drr_bonuslen = dnp->dn_bonuslen;
131 	ba->drr->drr_u.drr_object.drr_checksum = dnp->dn_checksum;
132 	ba->drr->drr_u.drr_object.drr_compress = dnp->dn_compress;
133 
134 	if (dump_bytes(ba, ba->drr, sizeof (dmu_replay_record_t)))
135 		return (EINTR);
136 
137 	if (dump_bytes(ba, DN_BONUS(dnp), P2ROUNDUP(dnp->dn_bonuslen, 8)))
138 		return (EINTR);
139 
140 	/* free anything past the end of the file */
141 	if (dump_free(ba, object, (dnp->dn_maxblkid + 1) *
142 	    (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL))
143 		return (EINTR);
144 	if (ba->err)
145 		return (EINTR);
146 	return (0);
147 }
148 
149 #define	BP_SPAN(dnp, level) \
150 	(((uint64_t)dnp->dn_datablkszsec) << (SPA_MINBLOCKSHIFT + \
151 	(level) * (dnp->dn_indblkshift - SPA_BLKPTRSHIFT)))
152 
153 static int
154 backup_cb(spa_t *spa, blkptr_t *bp, const zbookmark_t *zb,
155     const dnode_phys_t *dnp, void *arg)
156 {
157 	struct backuparg *ba = arg;
158 	dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
159 	int err = 0;
160 
161 	if (issig(JUSTLOOKING) && issig(FORREAL))
162 		return (EINTR);
163 
164 	if (zb->zb_object != 0 && DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
165 		return (0);
166 	} else if (bp == NULL && zb->zb_object == 0) {
167 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
168 		uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
169 		err = dump_freeobjects(ba, dnobj, span >> DNODE_SHIFT);
170 	} else if (bp == NULL) {
171 		uint64_t span = BP_SPAN(dnp, zb->zb_level);
172 		err = dump_free(ba, zb->zb_object, zb->zb_blkid * span, span);
173 	} else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
174 		return (0);
175 	} else if (type == DMU_OT_DNODE) {
176 		dnode_phys_t *blk;
177 		int i;
178 		int blksz = BP_GET_LSIZE(bp);
179 		uint32_t aflags = ARC_WAIT;
180 		arc_buf_t *abuf;
181 
182 		if (arc_read_nolock(NULL, spa, bp,
183 		    arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
184 		    ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
185 			return (EIO);
186 
187 		blk = abuf->b_data;
188 		for (i = 0; i < blksz >> DNODE_SHIFT; i++) {
189 			uint64_t dnobj = (zb->zb_blkid <<
190 			    (DNODE_BLOCK_SHIFT - DNODE_SHIFT)) + i;
191 			err = dump_dnode(ba, dnobj, blk+i);
192 			if (err)
193 				break;
194 		}
195 		(void) arc_buf_remove_ref(abuf, &abuf);
196 	} else { /* it's a level-0 block of a regular object */
197 		uint32_t aflags = ARC_WAIT;
198 		arc_buf_t *abuf;
199 		int blksz = BP_GET_LSIZE(bp);
200 
201 		if (arc_read_nolock(NULL, spa, bp,
202 		    arc_getbuf_func, &abuf, ZIO_PRIORITY_ASYNC_READ,
203 		    ZIO_FLAG_CANFAIL, &aflags, zb) != 0)
204 			return (EIO);
205 
206 		err = dump_data(ba, type, zb->zb_object, zb->zb_blkid * blksz,
207 		    blksz, abuf->b_data);
208 		(void) arc_buf_remove_ref(abuf, &abuf);
209 	}
210 
211 	ASSERT(err == 0 || err == EINTR);
212 	return (err);
213 }
214 
215 int
216 dmu_sendbackup(objset_t *tosnap, objset_t *fromsnap, boolean_t fromorigin,
217     vnode_t *vp, offset_t *off)
218 {
219 	dsl_dataset_t *ds = tosnap->os->os_dsl_dataset;
220 	dsl_dataset_t *fromds = fromsnap ? fromsnap->os->os_dsl_dataset : NULL;
221 	dmu_replay_record_t *drr;
222 	struct backuparg ba;
223 	int err;
224 	uint64_t fromtxg = 0;
225 
226 	/* tosnap must be a snapshot */
227 	if (ds->ds_phys->ds_next_snap_obj == 0)
228 		return (EINVAL);
229 
230 	/* fromsnap must be an earlier snapshot from the same fs as tosnap */
231 	if (fromds && (ds->ds_dir != fromds->ds_dir ||
232 	    fromds->ds_phys->ds_creation_txg >= ds->ds_phys->ds_creation_txg))
233 		return (EXDEV);
234 
235 	if (fromorigin) {
236 		dsl_pool_t *dp = ds->ds_dir->dd_pool;
237 
238 		if (fromsnap)
239 			return (EINVAL);
240 
241 		if (dsl_dir_is_clone(ds->ds_dir)) {
242 			rw_enter(&dp->dp_config_rwlock, RW_READER);
243 			err = dsl_dataset_hold_obj(dp,
244 			    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &fromds);
245 			rw_exit(&dp->dp_config_rwlock);
246 			if (err)
247 				return (err);
248 		} else {
249 			fromorigin = B_FALSE;
250 		}
251 	}
252 
253 
254 	drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
255 	drr->drr_type = DRR_BEGIN;
256 	drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
257 	drr->drr_u.drr_begin.drr_version = DMU_BACKUP_STREAM_VERSION;
258 	drr->drr_u.drr_begin.drr_creation_time =
259 	    ds->ds_phys->ds_creation_time;
260 	drr->drr_u.drr_begin.drr_type = tosnap->os->os_phys->os_type;
261 	if (fromorigin)
262 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
263 	drr->drr_u.drr_begin.drr_toguid = ds->ds_phys->ds_guid;
264 	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
265 		drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
266 
267 	if (fromds)
268 		drr->drr_u.drr_begin.drr_fromguid = fromds->ds_phys->ds_guid;
269 	dsl_dataset_name(ds, drr->drr_u.drr_begin.drr_toname);
270 
271 	if (fromds)
272 		fromtxg = fromds->ds_phys->ds_creation_txg;
273 	if (fromorigin)
274 		dsl_dataset_rele(fromds, FTAG);
275 
276 	ba.drr = drr;
277 	ba.vp = vp;
278 	ba.os = tosnap;
279 	ba.off = off;
280 	ZIO_SET_CHECKSUM(&ba.zc, 0, 0, 0, 0);
281 
282 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
283 		kmem_free(drr, sizeof (dmu_replay_record_t));
284 		return (ba.err);
285 	}
286 
287 	err = traverse_dataset(ds, fromtxg, TRAVERSE_PRE | TRAVERSE_PREFETCH,
288 	    backup_cb, &ba);
289 
290 	if (err) {
291 		if (err == EINTR && ba.err)
292 			err = ba.err;
293 		kmem_free(drr, sizeof (dmu_replay_record_t));
294 		return (err);
295 	}
296 
297 	bzero(drr, sizeof (dmu_replay_record_t));
298 	drr->drr_type = DRR_END;
299 	drr->drr_u.drr_end.drr_checksum = ba.zc;
300 
301 	if (dump_bytes(&ba, drr, sizeof (dmu_replay_record_t))) {
302 		kmem_free(drr, sizeof (dmu_replay_record_t));
303 		return (ba.err);
304 	}
305 
306 	kmem_free(drr, sizeof (dmu_replay_record_t));
307 
308 	return (0);
309 }
310 
311 struct recvbeginsyncarg {
312 	const char *tofs;
313 	const char *tosnap;
314 	dsl_dataset_t *origin;
315 	uint64_t fromguid;
316 	dmu_objset_type_t type;
317 	void *tag;
318 	boolean_t force;
319 	uint64_t dsflags;
320 	char clonelastname[MAXNAMELEN];
321 	dsl_dataset_t *ds; /* the ds to recv into; returned from the syncfunc */
322 };
323 
324 static dsl_dataset_t *
325 recv_full_sync_impl(dsl_pool_t *dp, uint64_t dsobj, dmu_objset_type_t type,
326     cred_t *cr, dmu_tx_t *tx)
327 {
328 	dsl_dataset_t *ds;
329 
330 	/* This should always work, since we just created it */
331 	/* XXX - create should return an owned ds */
332 	VERIFY(0 == dsl_dataset_own_obj(dp, dsobj,
333 	    DS_MODE_INCONSISTENT, dmu_recv_tag, &ds));
334 
335 	if (type != DMU_OST_NONE) {
336 		(void) dmu_objset_create_impl(dp->dp_spa,
337 		    ds, &ds->ds_phys->ds_bp, type, tx);
338 	}
339 
340 	spa_history_internal_log(LOG_DS_REPLAY_FULL_SYNC,
341 	    dp->dp_spa, tx, cr, "dataset = %lld", dsobj);
342 
343 	return (ds);
344 }
345 
346 /* ARGSUSED */
347 static int
348 recv_full_check(void *arg1, void *arg2, dmu_tx_t *tx)
349 {
350 	dsl_dir_t *dd = arg1;
351 	struct recvbeginsyncarg *rbsa = arg2;
352 	objset_t *mos = dd->dd_pool->dp_meta_objset;
353 	uint64_t val;
354 	int err;
355 
356 	err = zap_lookup(mos, dd->dd_phys->dd_child_dir_zapobj,
357 	    strrchr(rbsa->tofs, '/') + 1, sizeof (uint64_t), 1, &val);
358 
359 	if (err != ENOENT)
360 		return (err ? err : EEXIST);
361 
362 	if (rbsa->origin) {
363 		/* make sure it's a snap in the same pool */
364 		if (rbsa->origin->ds_dir->dd_pool != dd->dd_pool)
365 			return (EXDEV);
366 		if (rbsa->origin->ds_phys->ds_num_children == 0)
367 			return (EINVAL);
368 		if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid)
369 			return (ENODEV);
370 	}
371 
372 	return (0);
373 }
374 
375 static void
376 recv_full_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
377 {
378 	dsl_dir_t *dd = arg1;
379 	struct recvbeginsyncarg *rbsa = arg2;
380 	uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
381 	uint64_t dsobj;
382 
383 	dsobj = dsl_dataset_create_sync(dd, strrchr(rbsa->tofs, '/') + 1,
384 	    rbsa->origin, flags, cr, tx);
385 
386 	rbsa->ds = recv_full_sync_impl(dd->dd_pool, dsobj,
387 	    rbsa->origin ? DMU_OST_NONE : rbsa->type, cr, tx);
388 }
389 
390 static int
391 recv_full_existing_check(void *arg1, void *arg2, dmu_tx_t *tx)
392 {
393 	dsl_dataset_t *ds = arg1;
394 	struct recvbeginsyncarg *rbsa = arg2;
395 	int err;
396 
397 	/* must be a head ds */
398 	if (ds->ds_phys->ds_next_snap_obj != 0)
399 		return (EINVAL);
400 
401 	/* must not be a clone ds */
402 	if (dsl_dir_is_clone(ds->ds_dir))
403 		return (EINVAL);
404 
405 	err = dsl_dataset_destroy_check(ds, rbsa->tag, tx);
406 	if (err)
407 		return (err);
408 
409 	if (rbsa->origin) {
410 		/* make sure it's a snap in the same pool */
411 		if (rbsa->origin->ds_dir->dd_pool != ds->ds_dir->dd_pool)
412 			return (EXDEV);
413 		if (rbsa->origin->ds_phys->ds_num_children == 0)
414 			return (EINVAL);
415 		if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid)
416 			return (ENODEV);
417 	}
418 
419 	return (0);
420 }
421 
422 static void
423 recv_full_existing_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
424 {
425 	dsl_dataset_t *ds = arg1;
426 	struct recvbeginsyncarg *rbsa = arg2;
427 	dsl_dir_t *dd = ds->ds_dir;
428 	uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
429 	uint64_t dsobj;
430 
431 	/*
432 	 * NB: caller must provide an extra hold on the dsl_dir_t, so it
433 	 * won't go away when dsl_dataset_destroy_sync() closes the
434 	 * dataset.
435 	 */
436 	dsl_dataset_destroy_sync(ds, rbsa->tag, cr, tx);
437 
438 	dsobj = dsl_dataset_create_sync_dd(dd, rbsa->origin, flags, tx);
439 
440 	rbsa->ds = recv_full_sync_impl(dd->dd_pool, dsobj,
441 	    rbsa->origin ? DMU_OST_NONE : rbsa->type, cr, tx);
442 }
443 
444 /* ARGSUSED */
445 static int
446 recv_incremental_check(void *arg1, void *arg2, dmu_tx_t *tx)
447 {
448 	dsl_dataset_t *ds = arg1;
449 	struct recvbeginsyncarg *rbsa = arg2;
450 	int err;
451 	uint64_t val;
452 
453 	/* must not have any changes since most recent snapshot */
454 	if (!rbsa->force && dsl_dataset_modified_since_lastsnap(ds))
455 		return (ETXTBSY);
456 
457 	/* must already be a snapshot of this fs */
458 	if (ds->ds_phys->ds_prev_snap_obj == 0)
459 		return (ENODEV);
460 
461 	/* most recent snapshot must match fromguid */
462 	if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid)
463 		return (ENODEV);
464 
465 	/* temporary clone name must not exist */
466 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
467 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj,
468 	    rbsa->clonelastname, 8, 1, &val);
469 	if (err == 0)
470 		return (EEXIST);
471 	if (err != ENOENT)
472 		return (err);
473 
474 	/* new snapshot name must not exist */
475 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
476 	    ds->ds_phys->ds_snapnames_zapobj, rbsa->tosnap, 8, 1, &val);
477 	if (err == 0)
478 		return (EEXIST);
479 	if (err != ENOENT)
480 		return (err);
481 	return (0);
482 }
483 
484 /* ARGSUSED */
485 static void
486 recv_online_incremental_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
487 {
488 	dsl_dataset_t *ohds = arg1;
489 	struct recvbeginsyncarg *rbsa = arg2;
490 	dsl_pool_t *dp = ohds->ds_dir->dd_pool;
491 	dsl_dataset_t *ods, *cds;
492 	uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
493 	uint64_t dsobj;
494 
495 	/* create the temporary clone */
496 	VERIFY(0 == dsl_dataset_hold_obj(dp, ohds->ds_phys->ds_prev_snap_obj,
497 	    FTAG, &ods));
498 	dsobj = dsl_dataset_create_sync(ohds->ds_dir,
499 	    rbsa->clonelastname, ods, flags, cr, tx);
500 	dsl_dataset_rele(ods, FTAG);
501 
502 	/* open the temporary clone */
503 	VERIFY(0 == dsl_dataset_own_obj(dp, dsobj,
504 	    DS_MODE_INCONSISTENT, dmu_recv_tag, &cds));
505 
506 	/* copy the refquota from the target fs to the clone */
507 	if (ohds->ds_quota > 0)
508 		dsl_dataset_set_quota_sync(cds, &ohds->ds_quota, cr, tx);
509 
510 	rbsa->ds = cds;
511 
512 	spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC,
513 	    dp->dp_spa, tx, cr, "dataset = %lld", dsobj);
514 }
515 
516 /* ARGSUSED */
517 static void
518 recv_offline_incremental_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
519 {
520 	dsl_dataset_t *ds = arg1;
521 
522 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
523 	ds->ds_phys->ds_flags |= DS_FLAG_INCONSISTENT;
524 
525 	spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC,
526 	    ds->ds_dir->dd_pool->dp_spa, tx, cr, "dataset = %lld",
527 	    ds->ds_object);
528 }
529 
530 /*
531  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
532  * succeeds; otherwise we will leak the holds on the datasets.
533  */
534 int
535 dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
536     boolean_t force, objset_t *origin, boolean_t online, dmu_recv_cookie_t *drc)
537 {
538 	int err = 0;
539 	boolean_t byteswap;
540 	struct recvbeginsyncarg rbsa;
541 	uint64_t version;
542 	int flags;
543 	dsl_dataset_t *ds;
544 
545 	if (drrb->drr_magic == DMU_BACKUP_MAGIC)
546 		byteswap = FALSE;
547 	else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
548 		byteswap = TRUE;
549 	else
550 		return (EINVAL);
551 
552 	rbsa.tofs = tofs;
553 	rbsa.tosnap = tosnap;
554 	rbsa.origin = origin ? origin->os->os_dsl_dataset : NULL;
555 	rbsa.fromguid = drrb->drr_fromguid;
556 	rbsa.type = drrb->drr_type;
557 	rbsa.tag = FTAG;
558 	rbsa.dsflags = 0;
559 	version = drrb->drr_version;
560 	flags = drrb->drr_flags;
561 
562 	if (byteswap) {
563 		rbsa.type = BSWAP_32(rbsa.type);
564 		rbsa.fromguid = BSWAP_64(rbsa.fromguid);
565 		version = BSWAP_64(version);
566 		flags = BSWAP_32(flags);
567 	}
568 
569 	if (version != DMU_BACKUP_STREAM_VERSION ||
570 	    rbsa.type >= DMU_OST_NUMTYPES ||
571 	    ((flags & DRR_FLAG_CLONE) && origin == NULL))
572 		return (EINVAL);
573 
574 	if (flags & DRR_FLAG_CI_DATA)
575 		rbsa.dsflags = DS_FLAG_CI_DATASET;
576 
577 	bzero(drc, sizeof (dmu_recv_cookie_t));
578 	drc->drc_drrb = drrb;
579 	drc->drc_tosnap = tosnap;
580 	drc->drc_force = force;
581 
582 	/*
583 	 * Process the begin in syncing context.
584 	 */
585 	if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE) && !online) {
586 		/* offline incremental receive */
587 		err = dsl_dataset_own(tofs, 0, dmu_recv_tag, &ds);
588 		if (err)
589 			return (err);
590 
591 		/*
592 		 * Only do the rollback if the most recent snapshot
593 		 * matches the incremental source
594 		 */
595 		if (force) {
596 			if (ds->ds_prev == NULL ||
597 			    ds->ds_prev->ds_phys->ds_guid !=
598 			    rbsa.fromguid) {
599 				dsl_dataset_disown(ds, dmu_recv_tag);
600 				return (ENODEV);
601 			}
602 			(void) dsl_dataset_rollback(ds, DMU_OST_NONE);
603 		}
604 		rbsa.force = B_FALSE;
605 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
606 		    recv_incremental_check,
607 		    recv_offline_incremental_sync, ds, &rbsa, 1);
608 		if (err) {
609 			dsl_dataset_disown(ds, dmu_recv_tag);
610 			return (err);
611 		}
612 		drc->drc_logical_ds = drc->drc_real_ds = ds;
613 	} else if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE)) {
614 		/* online incremental receive */
615 
616 		/* tmp clone name is: tofs/%tosnap" */
617 		(void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname),
618 		    "%%%s", tosnap);
619 
620 		/* open the dataset we are logically receiving into */
621 		err = dsl_dataset_hold(tofs, dmu_recv_tag, &ds);
622 		if (err)
623 			return (err);
624 
625 		rbsa.force = force;
626 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
627 		    recv_incremental_check,
628 		    recv_online_incremental_sync, ds, &rbsa, 5);
629 		if (err) {
630 			dsl_dataset_rele(ds, dmu_recv_tag);
631 			return (err);
632 		}
633 		drc->drc_logical_ds = ds;
634 		drc->drc_real_ds = rbsa.ds;
635 	} else {
636 		/* create new fs -- full backup or clone */
637 		dsl_dir_t *dd = NULL;
638 		const char *tail;
639 
640 		err = dsl_dir_open(tofs, FTAG, &dd, &tail);
641 		if (err)
642 			return (err);
643 		if (tail == NULL) {
644 			if (!force) {
645 				dsl_dir_close(dd, FTAG);
646 				return (EEXIST);
647 			}
648 
649 			rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
650 			err = dsl_dataset_own_obj(dd->dd_pool,
651 			    dd->dd_phys->dd_head_dataset_obj,
652 			    DS_MODE_INCONSISTENT, FTAG, &ds);
653 			rw_exit(&dd->dd_pool->dp_config_rwlock);
654 			if (err) {
655 				dsl_dir_close(dd, FTAG);
656 				return (err);
657 			}
658 
659 			dsl_dataset_make_exclusive(ds, FTAG);
660 			err = dsl_sync_task_do(dd->dd_pool,
661 			    recv_full_existing_check,
662 			    recv_full_existing_sync, ds, &rbsa, 5);
663 			dsl_dataset_disown(ds, FTAG);
664 		} else {
665 			err = dsl_sync_task_do(dd->dd_pool, recv_full_check,
666 			    recv_full_sync, dd, &rbsa, 5);
667 		}
668 		dsl_dir_close(dd, FTAG);
669 		if (err)
670 			return (err);
671 		drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds;
672 		drc->drc_newfs = B_TRUE;
673 	}
674 
675 	return (0);
676 }
677 
678 struct restorearg {
679 	int err;
680 	int byteswap;
681 	vnode_t *vp;
682 	char *buf;
683 	uint64_t voff;
684 	int bufsize; /* amount of memory allocated for buf */
685 	zio_cksum_t cksum;
686 };
687 
688 static void *
689 restore_read(struct restorearg *ra, int len)
690 {
691 	void *rv;
692 	int done = 0;
693 
694 	/* some things will require 8-byte alignment, so everything must */
695 	ASSERT3U(len % 8, ==, 0);
696 
697 	while (done < len) {
698 		ssize_t resid;
699 
700 		ra->err = vn_rdwr(UIO_READ, ra->vp,
701 		    (caddr_t)ra->buf + done, len - done,
702 		    ra->voff, UIO_SYSSPACE, FAPPEND,
703 		    RLIM64_INFINITY, CRED(), &resid);
704 
705 		if (resid == len - done)
706 			ra->err = EINVAL;
707 		ra->voff += len - done - resid;
708 		done = len - resid;
709 		if (ra->err)
710 			return (NULL);
711 	}
712 
713 	ASSERT3U(done, ==, len);
714 	rv = ra->buf;
715 	if (ra->byteswap)
716 		fletcher_4_incremental_byteswap(rv, len, &ra->cksum);
717 	else
718 		fletcher_4_incremental_native(rv, len, &ra->cksum);
719 	return (rv);
720 }
721 
722 static void
723 backup_byteswap(dmu_replay_record_t *drr)
724 {
725 #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
726 #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
727 	drr->drr_type = BSWAP_32(drr->drr_type);
728 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
729 	switch (drr->drr_type) {
730 	case DRR_BEGIN:
731 		DO64(drr_begin.drr_magic);
732 		DO64(drr_begin.drr_version);
733 		DO64(drr_begin.drr_creation_time);
734 		DO32(drr_begin.drr_type);
735 		DO32(drr_begin.drr_flags);
736 		DO64(drr_begin.drr_toguid);
737 		DO64(drr_begin.drr_fromguid);
738 		break;
739 	case DRR_OBJECT:
740 		DO64(drr_object.drr_object);
741 		/* DO64(drr_object.drr_allocation_txg); */
742 		DO32(drr_object.drr_type);
743 		DO32(drr_object.drr_bonustype);
744 		DO32(drr_object.drr_blksz);
745 		DO32(drr_object.drr_bonuslen);
746 		break;
747 	case DRR_FREEOBJECTS:
748 		DO64(drr_freeobjects.drr_firstobj);
749 		DO64(drr_freeobjects.drr_numobjs);
750 		break;
751 	case DRR_WRITE:
752 		DO64(drr_write.drr_object);
753 		DO32(drr_write.drr_type);
754 		DO64(drr_write.drr_offset);
755 		DO64(drr_write.drr_length);
756 		break;
757 	case DRR_FREE:
758 		DO64(drr_free.drr_object);
759 		DO64(drr_free.drr_offset);
760 		DO64(drr_free.drr_length);
761 		break;
762 	case DRR_END:
763 		DO64(drr_end.drr_checksum.zc_word[0]);
764 		DO64(drr_end.drr_checksum.zc_word[1]);
765 		DO64(drr_end.drr_checksum.zc_word[2]);
766 		DO64(drr_end.drr_checksum.zc_word[3]);
767 		break;
768 	}
769 #undef DO64
770 #undef DO32
771 }
772 
773 static int
774 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
775 {
776 	int err;
777 	dmu_tx_t *tx;
778 	void *data = NULL;
779 
780 	if (drro->drr_type == DMU_OT_NONE ||
781 	    drro->drr_type >= DMU_OT_NUMTYPES ||
782 	    drro->drr_bonustype >= DMU_OT_NUMTYPES ||
783 	    drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS ||
784 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
785 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
786 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
787 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
788 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
789 		return (EINVAL);
790 	}
791 
792 	err = dmu_object_info(os, drro->drr_object, NULL);
793 
794 	if (err != 0 && err != ENOENT)
795 		return (EINVAL);
796 
797 	if (drro->drr_bonuslen) {
798 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
799 		if (ra->err)
800 			return (ra->err);
801 	}
802 
803 	if (err == ENOENT) {
804 		/* currently free, want to be allocated */
805 		tx = dmu_tx_create(os);
806 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
807 		err = dmu_tx_assign(tx, TXG_WAIT);
808 		if (err) {
809 			dmu_tx_abort(tx);
810 			return (err);
811 		}
812 		err = dmu_object_claim(os, drro->drr_object,
813 		    drro->drr_type, drro->drr_blksz,
814 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
815 		dmu_tx_commit(tx);
816 	} else {
817 		/* currently allocated, want to be allocated */
818 		err = dmu_object_reclaim(os, drro->drr_object,
819 		    drro->drr_type, drro->drr_blksz,
820 		    drro->drr_bonustype, drro->drr_bonuslen);
821 	}
822 	if (err)
823 		return (EINVAL);
824 
825 	tx = dmu_tx_create(os);
826 	dmu_tx_hold_bonus(tx, drro->drr_object);
827 	err = dmu_tx_assign(tx, TXG_WAIT);
828 	if (err) {
829 		dmu_tx_abort(tx);
830 		return (err);
831 	}
832 
833 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx);
834 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
835 
836 	if (data != NULL) {
837 		dmu_buf_t *db;
838 
839 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
840 		dmu_buf_will_dirty(db, tx);
841 
842 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
843 		bcopy(data, db->db_data, drro->drr_bonuslen);
844 		if (ra->byteswap) {
845 			dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
846 			    drro->drr_bonuslen);
847 		}
848 		dmu_buf_rele(db, FTAG);
849 	}
850 	dmu_tx_commit(tx);
851 	return (0);
852 }
853 
854 /* ARGSUSED */
855 static int
856 restore_freeobjects(struct restorearg *ra, objset_t *os,
857     struct drr_freeobjects *drrfo)
858 {
859 	uint64_t obj;
860 
861 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
862 		return (EINVAL);
863 
864 	for (obj = drrfo->drr_firstobj;
865 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
866 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
867 		int err;
868 
869 		if (dmu_object_info(os, obj, NULL) != 0)
870 			continue;
871 
872 		err = dmu_free_object(os, obj);
873 		if (err)
874 			return (err);
875 	}
876 	return (0);
877 }
878 
879 static int
880 restore_write(struct restorearg *ra, objset_t *os,
881     struct drr_write *drrw)
882 {
883 	dmu_tx_t *tx;
884 	void *data;
885 	int err;
886 
887 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
888 	    drrw->drr_type >= DMU_OT_NUMTYPES)
889 		return (EINVAL);
890 
891 	data = restore_read(ra, drrw->drr_length);
892 	if (data == NULL)
893 		return (ra->err);
894 
895 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
896 		return (EINVAL);
897 
898 	tx = dmu_tx_create(os);
899 
900 	dmu_tx_hold_write(tx, drrw->drr_object,
901 	    drrw->drr_offset, drrw->drr_length);
902 	err = dmu_tx_assign(tx, TXG_WAIT);
903 	if (err) {
904 		dmu_tx_abort(tx);
905 		return (err);
906 	}
907 	if (ra->byteswap)
908 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
909 	dmu_write(os, drrw->drr_object,
910 	    drrw->drr_offset, drrw->drr_length, data, tx);
911 	dmu_tx_commit(tx);
912 	return (0);
913 }
914 
915 /* ARGSUSED */
916 static int
917 restore_free(struct restorearg *ra, objset_t *os,
918     struct drr_free *drrf)
919 {
920 	int err;
921 
922 	if (drrf->drr_length != -1ULL &&
923 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
924 		return (EINVAL);
925 
926 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
927 		return (EINVAL);
928 
929 	err = dmu_free_long_range(os, drrf->drr_object,
930 	    drrf->drr_offset, drrf->drr_length);
931 	return (err);
932 }
933 
934 void
935 dmu_recv_abort_cleanup(dmu_recv_cookie_t *drc)
936 {
937 	if (drc->drc_newfs || drc->drc_real_ds != drc->drc_logical_ds) {
938 		/*
939 		 * online incremental or new fs: destroy the fs (which
940 		 * may be a clone) that we created
941 		 */
942 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag);
943 		if (drc->drc_real_ds != drc->drc_logical_ds)
944 			dsl_dataset_rele(drc->drc_logical_ds, dmu_recv_tag);
945 	} else {
946 		/*
947 		 * offline incremental: rollback to most recent snapshot.
948 		 */
949 		(void) dsl_dataset_rollback(drc->drc_real_ds, DMU_OST_NONE);
950 		dsl_dataset_disown(drc->drc_real_ds, dmu_recv_tag);
951 	}
952 }
953 
954 /*
955  * NB: callers *must* call dmu_recv_end() if this succeeds.
956  */
957 int
958 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp)
959 {
960 	struct restorearg ra = { 0 };
961 	dmu_replay_record_t *drr;
962 	objset_t *os;
963 	zio_cksum_t pcksum;
964 
965 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
966 		ra.byteswap = TRUE;
967 
968 	{
969 		/* compute checksum of drr_begin record */
970 		dmu_replay_record_t *drr;
971 		drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
972 
973 		drr->drr_type = DRR_BEGIN;
974 		drr->drr_u.drr_begin = *drc->drc_drrb;
975 		if (ra.byteswap) {
976 			fletcher_4_incremental_byteswap(drr,
977 			    sizeof (dmu_replay_record_t), &ra.cksum);
978 		} else {
979 			fletcher_4_incremental_native(drr,
980 			    sizeof (dmu_replay_record_t), &ra.cksum);
981 		}
982 		kmem_free(drr, sizeof (dmu_replay_record_t));
983 	}
984 
985 	if (ra.byteswap) {
986 		struct drr_begin *drrb = drc->drc_drrb;
987 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
988 		drrb->drr_version = BSWAP_64(drrb->drr_version);
989 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
990 		drrb->drr_type = BSWAP_32(drrb->drr_type);
991 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
992 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
993 	}
994 
995 	ra.vp = vp;
996 	ra.voff = *voffp;
997 	ra.bufsize = 1<<20;
998 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
999 
1000 	/* these were verified in dmu_recv_begin */
1001 	ASSERT(drc->drc_drrb->drr_version == DMU_BACKUP_STREAM_VERSION);
1002 	ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES);
1003 
1004 	/*
1005 	 * Open the objset we are modifying.
1006 	 */
1007 	VERIFY(dmu_objset_open_ds(drc->drc_real_ds, DMU_OST_ANY, &os) == 0);
1008 
1009 	ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
1010 
1011 	/*
1012 	 * Read records and process them.
1013 	 */
1014 	pcksum = ra.cksum;
1015 	while (ra.err == 0 &&
1016 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
1017 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
1018 			ra.err = EINTR;
1019 			goto out;
1020 		}
1021 
1022 		if (ra.byteswap)
1023 			backup_byteswap(drr);
1024 
1025 		switch (drr->drr_type) {
1026 		case DRR_OBJECT:
1027 		{
1028 			/*
1029 			 * We need to make a copy of the record header,
1030 			 * because restore_{object,write} may need to
1031 			 * restore_read(), which will invalidate drr.
1032 			 */
1033 			struct drr_object drro = drr->drr_u.drr_object;
1034 			ra.err = restore_object(&ra, os, &drro);
1035 			break;
1036 		}
1037 		case DRR_FREEOBJECTS:
1038 		{
1039 			struct drr_freeobjects drrfo =
1040 			    drr->drr_u.drr_freeobjects;
1041 			ra.err = restore_freeobjects(&ra, os, &drrfo);
1042 			break;
1043 		}
1044 		case DRR_WRITE:
1045 		{
1046 			struct drr_write drrw = drr->drr_u.drr_write;
1047 			ra.err = restore_write(&ra, os, &drrw);
1048 			break;
1049 		}
1050 		case DRR_FREE:
1051 		{
1052 			struct drr_free drrf = drr->drr_u.drr_free;
1053 			ra.err = restore_free(&ra, os, &drrf);
1054 			break;
1055 		}
1056 		case DRR_END:
1057 		{
1058 			struct drr_end drre = drr->drr_u.drr_end;
1059 			/*
1060 			 * We compare against the *previous* checksum
1061 			 * value, because the stored checksum is of
1062 			 * everything before the DRR_END record.
1063 			 */
1064 			if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
1065 				ra.err = ECKSUM;
1066 			goto out;
1067 		}
1068 		default:
1069 			ra.err = EINVAL;
1070 			goto out;
1071 		}
1072 		pcksum = ra.cksum;
1073 	}
1074 	ASSERT(ra.err != 0);
1075 
1076 out:
1077 	dmu_objset_close(os);
1078 
1079 	if (ra.err != 0) {
1080 		/*
1081 		 * rollback or destroy what we created, so we don't
1082 		 * leave it in the restoring state.
1083 		 */
1084 		txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0);
1085 		dmu_recv_abort_cleanup(drc);
1086 	}
1087 
1088 	kmem_free(ra.buf, ra.bufsize);
1089 	*voffp = ra.voff;
1090 	return (ra.err);
1091 }
1092 
1093 struct recvendsyncarg {
1094 	char *tosnap;
1095 	uint64_t creation_time;
1096 	uint64_t toguid;
1097 };
1098 
1099 static int
1100 recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx)
1101 {
1102 	dsl_dataset_t *ds = arg1;
1103 	struct recvendsyncarg *resa = arg2;
1104 
1105 	return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx));
1106 }
1107 
1108 static void
1109 recv_end_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1110 {
1111 	dsl_dataset_t *ds = arg1;
1112 	struct recvendsyncarg *resa = arg2;
1113 
1114 	dsl_dataset_snapshot_sync(ds, resa->tosnap, cr, tx);
1115 
1116 	/* set snapshot's creation time and guid */
1117 	dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1118 	ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time;
1119 	ds->ds_prev->ds_phys->ds_guid = resa->toguid;
1120 	ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
1121 
1122 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1123 	ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
1124 }
1125 
1126 int
1127 dmu_recv_end(dmu_recv_cookie_t *drc)
1128 {
1129 	struct recvendsyncarg resa;
1130 	dsl_dataset_t *ds = drc->drc_logical_ds;
1131 	int err;
1132 
1133 	/*
1134 	 * XXX hack; seems the ds is still dirty and
1135 	 * dsl_pool_zil_clean() expects it to have a ds_user_ptr
1136 	 * (and zil), but clone_swap() can close it.
1137 	 */
1138 	txg_wait_synced(ds->ds_dir->dd_pool, 0);
1139 
1140 	if (ds != drc->drc_real_ds) {
1141 		/* we are doing an online recv */
1142 		if (dsl_dataset_tryown(ds, FALSE, dmu_recv_tag)) {
1143 			err = dsl_dataset_clone_swap(drc->drc_real_ds, ds,
1144 			    drc->drc_force);
1145 			if (err)
1146 				dsl_dataset_disown(ds, dmu_recv_tag);
1147 		} else {
1148 			err = EBUSY;
1149 			dsl_dataset_rele(ds, dmu_recv_tag);
1150 		}
1151 		/* dsl_dataset_destroy() will disown the ds */
1152 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag);
1153 		if (err)
1154 			return (err);
1155 	}
1156 
1157 	resa.creation_time = drc->drc_drrb->drr_creation_time;
1158 	resa.toguid = drc->drc_drrb->drr_toguid;
1159 	resa.tosnap = drc->drc_tosnap;
1160 
1161 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1162 	    recv_end_check, recv_end_sync, ds, &resa, 3);
1163 	if (err) {
1164 		if (drc->drc_newfs) {
1165 			ASSERT(ds == drc->drc_real_ds);
1166 			(void) dsl_dataset_destroy(ds, dmu_recv_tag);
1167 			return (err);
1168 		} else {
1169 			(void) dsl_dataset_rollback(ds, DMU_OST_NONE);
1170 		}
1171 	}
1172 
1173 	/* release the hold from dmu_recv_begin */
1174 	dsl_dataset_disown(ds, dmu_recv_tag);
1175 	return (err);
1176 }
1177