xref: /titanic_50/usr/src/uts/common/fs/zfs/dmu_send.c (revision 9a4611f412a6b1f7a0bc7d53d2bb046a95daa4bc)
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 	struct dsl_ds_destroyarg dsda = {0};
397 
398 	/* must be a head ds */
399 	if (ds->ds_phys->ds_next_snap_obj != 0)
400 		return (EINVAL);
401 
402 	/* must not be a clone ds */
403 	if (dsl_dir_is_clone(ds->ds_dir))
404 		return (EINVAL);
405 
406 	dsda.ds = ds;
407 	err = dsl_dataset_destroy_check(&dsda, rbsa->tag, tx);
408 	if (err)
409 		return (err);
410 
411 	if (rbsa->origin) {
412 		/* make sure it's a snap in the same pool */
413 		if (rbsa->origin->ds_dir->dd_pool != ds->ds_dir->dd_pool)
414 			return (EXDEV);
415 		if (rbsa->origin->ds_phys->ds_num_children == 0)
416 			return (EINVAL);
417 		if (rbsa->origin->ds_phys->ds_guid != rbsa->fromguid)
418 			return (ENODEV);
419 	}
420 
421 	return (0);
422 }
423 
424 static void
425 recv_full_existing_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
426 {
427 	dsl_dataset_t *ds = arg1;
428 	struct recvbeginsyncarg *rbsa = arg2;
429 	dsl_dir_t *dd = ds->ds_dir;
430 	uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
431 	uint64_t dsobj;
432 	struct dsl_ds_destroyarg dsda = {0};
433 
434 	/*
435 	 * NB: caller must provide an extra hold on the dsl_dir_t, so it
436 	 * won't go away when dsl_dataset_destroy_sync() closes the
437 	 * dataset.
438 	 */
439 	dsda.ds = ds;
440 	dsl_dataset_destroy_sync(&dsda, rbsa->tag, cr, tx);
441 	ASSERT3P(dsda.rm_origin, ==, NULL);
442 
443 	dsobj = dsl_dataset_create_sync_dd(dd, rbsa->origin, flags, tx);
444 
445 	rbsa->ds = recv_full_sync_impl(dd->dd_pool, dsobj,
446 	    rbsa->origin ? DMU_OST_NONE : rbsa->type, cr, tx);
447 }
448 
449 /* ARGSUSED */
450 static int
451 recv_incremental_check(void *arg1, void *arg2, dmu_tx_t *tx)
452 {
453 	dsl_dataset_t *ds = arg1;
454 	struct recvbeginsyncarg *rbsa = arg2;
455 	int err;
456 	uint64_t val;
457 
458 	/* must not have any changes since most recent snapshot */
459 	if (!rbsa->force && dsl_dataset_modified_since_lastsnap(ds))
460 		return (ETXTBSY);
461 
462 	/* must already be a snapshot of this fs */
463 	if (ds->ds_phys->ds_prev_snap_obj == 0)
464 		return (ENODEV);
465 
466 	/* most recent snapshot must match fromguid */
467 	if (ds->ds_prev->ds_phys->ds_guid != rbsa->fromguid)
468 		return (ENODEV);
469 
470 	/* temporary clone name must not exist */
471 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
472 	    ds->ds_dir->dd_phys->dd_child_dir_zapobj,
473 	    rbsa->clonelastname, 8, 1, &val);
474 	if (err == 0)
475 		return (EEXIST);
476 	if (err != ENOENT)
477 		return (err);
478 
479 	/* new snapshot name must not exist */
480 	err = zap_lookup(ds->ds_dir->dd_pool->dp_meta_objset,
481 	    ds->ds_phys->ds_snapnames_zapobj, rbsa->tosnap, 8, 1, &val);
482 	if (err == 0)
483 		return (EEXIST);
484 	if (err != ENOENT)
485 		return (err);
486 	return (0);
487 }
488 
489 /* ARGSUSED */
490 static void
491 recv_incremental_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
492 {
493 	dsl_dataset_t *ohds = arg1;
494 	struct recvbeginsyncarg *rbsa = arg2;
495 	dsl_pool_t *dp = ohds->ds_dir->dd_pool;
496 	dsl_dataset_t *ods, *cds;
497 	uint64_t flags = DS_FLAG_INCONSISTENT | rbsa->dsflags;
498 	uint64_t dsobj;
499 
500 	/* create the temporary clone */
501 	VERIFY(0 == dsl_dataset_hold_obj(dp, ohds->ds_phys->ds_prev_snap_obj,
502 	    FTAG, &ods));
503 	dsobj = dsl_dataset_create_sync(ohds->ds_dir,
504 	    rbsa->clonelastname, ods, flags, cr, tx);
505 	dsl_dataset_rele(ods, FTAG);
506 
507 	/* open the temporary clone */
508 	VERIFY(0 == dsl_dataset_own_obj(dp, dsobj,
509 	    DS_MODE_INCONSISTENT, dmu_recv_tag, &cds));
510 
511 	/* copy the refquota from the target fs to the clone */
512 	if (ohds->ds_quota > 0)
513 		dsl_dataset_set_quota_sync(cds, &ohds->ds_quota, cr, tx);
514 
515 	rbsa->ds = cds;
516 
517 	spa_history_internal_log(LOG_DS_REPLAY_INC_SYNC,
518 	    dp->dp_spa, tx, cr, "dataset = %lld", dsobj);
519 }
520 
521 /*
522  * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
523  * succeeds; otherwise we will leak the holds on the datasets.
524  */
525 int
526 dmu_recv_begin(char *tofs, char *tosnap, struct drr_begin *drrb,
527     boolean_t force, objset_t *origin, dmu_recv_cookie_t *drc)
528 {
529 	int err = 0;
530 	boolean_t byteswap;
531 	struct recvbeginsyncarg rbsa;
532 	uint64_t version;
533 	int flags;
534 	dsl_dataset_t *ds;
535 
536 	if (drrb->drr_magic == DMU_BACKUP_MAGIC)
537 		byteswap = FALSE;
538 	else if (drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
539 		byteswap = TRUE;
540 	else
541 		return (EINVAL);
542 
543 	rbsa.tofs = tofs;
544 	rbsa.tosnap = tosnap;
545 	rbsa.origin = origin ? origin->os->os_dsl_dataset : NULL;
546 	rbsa.fromguid = drrb->drr_fromguid;
547 	rbsa.type = drrb->drr_type;
548 	rbsa.tag = FTAG;
549 	rbsa.dsflags = 0;
550 	version = drrb->drr_version;
551 	flags = drrb->drr_flags;
552 
553 	if (byteswap) {
554 		rbsa.type = BSWAP_32(rbsa.type);
555 		rbsa.fromguid = BSWAP_64(rbsa.fromguid);
556 		version = BSWAP_64(version);
557 		flags = BSWAP_32(flags);
558 	}
559 
560 	if (version != DMU_BACKUP_STREAM_VERSION ||
561 	    rbsa.type >= DMU_OST_NUMTYPES ||
562 	    ((flags & DRR_FLAG_CLONE) && origin == NULL))
563 		return (EINVAL);
564 
565 	if (flags & DRR_FLAG_CI_DATA)
566 		rbsa.dsflags = DS_FLAG_CI_DATASET;
567 
568 	bzero(drc, sizeof (dmu_recv_cookie_t));
569 	drc->drc_drrb = drrb;
570 	drc->drc_tosnap = tosnap;
571 	drc->drc_force = force;
572 
573 	/*
574 	 * Process the begin in syncing context.
575 	 */
576 	if (rbsa.fromguid && !(flags & DRR_FLAG_CLONE)) {
577 		/* incremental receive */
578 
579 		/* tmp clone name is: tofs/%tosnap" */
580 		(void) snprintf(rbsa.clonelastname, sizeof (rbsa.clonelastname),
581 		    "%%%s", tosnap);
582 
583 		/* open the dataset we are logically receiving into */
584 		err = dsl_dataset_hold(tofs, dmu_recv_tag, &ds);
585 		if (err)
586 			return (err);
587 
588 		/* must not have an incremental recv already in progress */
589 		if (!mutex_tryenter(&ds->ds_recvlock)) {
590 			dsl_dataset_rele(ds, dmu_recv_tag);
591 			return (EBUSY);
592 		}
593 
594 		rbsa.force = force;
595 		err = dsl_sync_task_do(ds->ds_dir->dd_pool,
596 		    recv_incremental_check,
597 		    recv_incremental_sync, ds, &rbsa, 5);
598 		if (err) {
599 			mutex_exit(&ds->ds_recvlock);
600 			dsl_dataset_rele(ds, dmu_recv_tag);
601 			return (err);
602 		}
603 		drc->drc_logical_ds = ds;
604 		drc->drc_real_ds = rbsa.ds;
605 	} else {
606 		/* create new fs -- full backup or clone */
607 		dsl_dir_t *dd = NULL;
608 		const char *tail;
609 
610 		err = dsl_dir_open(tofs, FTAG, &dd, &tail);
611 		if (err)
612 			return (err);
613 		if (tail == NULL) {
614 			if (!force) {
615 				dsl_dir_close(dd, FTAG);
616 				return (EEXIST);
617 			}
618 
619 			rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER);
620 			err = dsl_dataset_own_obj(dd->dd_pool,
621 			    dd->dd_phys->dd_head_dataset_obj,
622 			    DS_MODE_INCONSISTENT, FTAG, &ds);
623 			rw_exit(&dd->dd_pool->dp_config_rwlock);
624 			if (err) {
625 				dsl_dir_close(dd, FTAG);
626 				return (err);
627 			}
628 
629 			dsl_dataset_make_exclusive(ds, FTAG);
630 			err = dsl_sync_task_do(dd->dd_pool,
631 			    recv_full_existing_check,
632 			    recv_full_existing_sync, ds, &rbsa, 5);
633 			dsl_dataset_disown(ds, FTAG);
634 		} else {
635 			err = dsl_sync_task_do(dd->dd_pool, recv_full_check,
636 			    recv_full_sync, dd, &rbsa, 5);
637 		}
638 		dsl_dir_close(dd, FTAG);
639 		if (err)
640 			return (err);
641 		drc->drc_logical_ds = drc->drc_real_ds = rbsa.ds;
642 		drc->drc_newfs = B_TRUE;
643 	}
644 
645 	return (0);
646 }
647 
648 struct restorearg {
649 	int err;
650 	int byteswap;
651 	vnode_t *vp;
652 	char *buf;
653 	uint64_t voff;
654 	int bufsize; /* amount of memory allocated for buf */
655 	zio_cksum_t cksum;
656 };
657 
658 static void *
659 restore_read(struct restorearg *ra, int len)
660 {
661 	void *rv;
662 	int done = 0;
663 
664 	/* some things will require 8-byte alignment, so everything must */
665 	ASSERT3U(len % 8, ==, 0);
666 
667 	while (done < len) {
668 		ssize_t resid;
669 
670 		ra->err = vn_rdwr(UIO_READ, ra->vp,
671 		    (caddr_t)ra->buf + done, len - done,
672 		    ra->voff, UIO_SYSSPACE, FAPPEND,
673 		    RLIM64_INFINITY, CRED(), &resid);
674 
675 		if (resid == len - done)
676 			ra->err = EINVAL;
677 		ra->voff += len - done - resid;
678 		done = len - resid;
679 		if (ra->err)
680 			return (NULL);
681 	}
682 
683 	ASSERT3U(done, ==, len);
684 	rv = ra->buf;
685 	if (ra->byteswap)
686 		fletcher_4_incremental_byteswap(rv, len, &ra->cksum);
687 	else
688 		fletcher_4_incremental_native(rv, len, &ra->cksum);
689 	return (rv);
690 }
691 
692 static void
693 backup_byteswap(dmu_replay_record_t *drr)
694 {
695 #define	DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
696 #define	DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
697 	drr->drr_type = BSWAP_32(drr->drr_type);
698 	drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
699 	switch (drr->drr_type) {
700 	case DRR_BEGIN:
701 		DO64(drr_begin.drr_magic);
702 		DO64(drr_begin.drr_version);
703 		DO64(drr_begin.drr_creation_time);
704 		DO32(drr_begin.drr_type);
705 		DO32(drr_begin.drr_flags);
706 		DO64(drr_begin.drr_toguid);
707 		DO64(drr_begin.drr_fromguid);
708 		break;
709 	case DRR_OBJECT:
710 		DO64(drr_object.drr_object);
711 		/* DO64(drr_object.drr_allocation_txg); */
712 		DO32(drr_object.drr_type);
713 		DO32(drr_object.drr_bonustype);
714 		DO32(drr_object.drr_blksz);
715 		DO32(drr_object.drr_bonuslen);
716 		break;
717 	case DRR_FREEOBJECTS:
718 		DO64(drr_freeobjects.drr_firstobj);
719 		DO64(drr_freeobjects.drr_numobjs);
720 		break;
721 	case DRR_WRITE:
722 		DO64(drr_write.drr_object);
723 		DO32(drr_write.drr_type);
724 		DO64(drr_write.drr_offset);
725 		DO64(drr_write.drr_length);
726 		break;
727 	case DRR_FREE:
728 		DO64(drr_free.drr_object);
729 		DO64(drr_free.drr_offset);
730 		DO64(drr_free.drr_length);
731 		break;
732 	case DRR_END:
733 		DO64(drr_end.drr_checksum.zc_word[0]);
734 		DO64(drr_end.drr_checksum.zc_word[1]);
735 		DO64(drr_end.drr_checksum.zc_word[2]);
736 		DO64(drr_end.drr_checksum.zc_word[3]);
737 		break;
738 	}
739 #undef DO64
740 #undef DO32
741 }
742 
743 static int
744 restore_object(struct restorearg *ra, objset_t *os, struct drr_object *drro)
745 {
746 	int err;
747 	dmu_tx_t *tx;
748 	void *data = NULL;
749 
750 	if (drro->drr_type == DMU_OT_NONE ||
751 	    drro->drr_type >= DMU_OT_NUMTYPES ||
752 	    drro->drr_bonustype >= DMU_OT_NUMTYPES ||
753 	    drro->drr_checksum >= ZIO_CHECKSUM_FUNCTIONS ||
754 	    drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
755 	    P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
756 	    drro->drr_blksz < SPA_MINBLOCKSIZE ||
757 	    drro->drr_blksz > SPA_MAXBLOCKSIZE ||
758 	    drro->drr_bonuslen > DN_MAX_BONUSLEN) {
759 		return (EINVAL);
760 	}
761 
762 	err = dmu_object_info(os, drro->drr_object, NULL);
763 
764 	if (err != 0 && err != ENOENT)
765 		return (EINVAL);
766 
767 	if (drro->drr_bonuslen) {
768 		data = restore_read(ra, P2ROUNDUP(drro->drr_bonuslen, 8));
769 		if (ra->err)
770 			return (ra->err);
771 	}
772 
773 	if (err == ENOENT) {
774 		/* currently free, want to be allocated */
775 		tx = dmu_tx_create(os);
776 		dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT);
777 		err = dmu_tx_assign(tx, TXG_WAIT);
778 		if (err) {
779 			dmu_tx_abort(tx);
780 			return (err);
781 		}
782 		err = dmu_object_claim(os, drro->drr_object,
783 		    drro->drr_type, drro->drr_blksz,
784 		    drro->drr_bonustype, drro->drr_bonuslen, tx);
785 		dmu_tx_commit(tx);
786 	} else {
787 		/* currently allocated, want to be allocated */
788 		err = dmu_object_reclaim(os, drro->drr_object,
789 		    drro->drr_type, drro->drr_blksz,
790 		    drro->drr_bonustype, drro->drr_bonuslen);
791 	}
792 	if (err)
793 		return (EINVAL);
794 
795 	tx = dmu_tx_create(os);
796 	dmu_tx_hold_bonus(tx, drro->drr_object);
797 	err = dmu_tx_assign(tx, TXG_WAIT);
798 	if (err) {
799 		dmu_tx_abort(tx);
800 		return (err);
801 	}
802 
803 	dmu_object_set_checksum(os, drro->drr_object, drro->drr_checksum, tx);
804 	dmu_object_set_compress(os, drro->drr_object, drro->drr_compress, tx);
805 
806 	if (data != NULL) {
807 		dmu_buf_t *db;
808 
809 		VERIFY(0 == dmu_bonus_hold(os, drro->drr_object, FTAG, &db));
810 		dmu_buf_will_dirty(db, tx);
811 
812 		ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
813 		bcopy(data, db->db_data, drro->drr_bonuslen);
814 		if (ra->byteswap) {
815 			dmu_ot[drro->drr_bonustype].ot_byteswap(db->db_data,
816 			    drro->drr_bonuslen);
817 		}
818 		dmu_buf_rele(db, FTAG);
819 	}
820 	dmu_tx_commit(tx);
821 	return (0);
822 }
823 
824 /* ARGSUSED */
825 static int
826 restore_freeobjects(struct restorearg *ra, objset_t *os,
827     struct drr_freeobjects *drrfo)
828 {
829 	uint64_t obj;
830 
831 	if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
832 		return (EINVAL);
833 
834 	for (obj = drrfo->drr_firstobj;
835 	    obj < drrfo->drr_firstobj + drrfo->drr_numobjs;
836 	    (void) dmu_object_next(os, &obj, FALSE, 0)) {
837 		int err;
838 
839 		if (dmu_object_info(os, obj, NULL) != 0)
840 			continue;
841 
842 		err = dmu_free_object(os, obj);
843 		if (err)
844 			return (err);
845 	}
846 	return (0);
847 }
848 
849 static int
850 restore_write(struct restorearg *ra, objset_t *os,
851     struct drr_write *drrw)
852 {
853 	dmu_tx_t *tx;
854 	void *data;
855 	int err;
856 
857 	if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
858 	    drrw->drr_type >= DMU_OT_NUMTYPES)
859 		return (EINVAL);
860 
861 	data = restore_read(ra, drrw->drr_length);
862 	if (data == NULL)
863 		return (ra->err);
864 
865 	if (dmu_object_info(os, drrw->drr_object, NULL) != 0)
866 		return (EINVAL);
867 
868 	tx = dmu_tx_create(os);
869 
870 	dmu_tx_hold_write(tx, drrw->drr_object,
871 	    drrw->drr_offset, drrw->drr_length);
872 	err = dmu_tx_assign(tx, TXG_WAIT);
873 	if (err) {
874 		dmu_tx_abort(tx);
875 		return (err);
876 	}
877 	if (ra->byteswap)
878 		dmu_ot[drrw->drr_type].ot_byteswap(data, drrw->drr_length);
879 	dmu_write(os, drrw->drr_object,
880 	    drrw->drr_offset, drrw->drr_length, data, tx);
881 	dmu_tx_commit(tx);
882 	return (0);
883 }
884 
885 /* ARGSUSED */
886 static int
887 restore_free(struct restorearg *ra, objset_t *os,
888     struct drr_free *drrf)
889 {
890 	int err;
891 
892 	if (drrf->drr_length != -1ULL &&
893 	    drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
894 		return (EINVAL);
895 
896 	if (dmu_object_info(os, drrf->drr_object, NULL) != 0)
897 		return (EINVAL);
898 
899 	err = dmu_free_long_range(os, drrf->drr_object,
900 	    drrf->drr_offset, drrf->drr_length);
901 	return (err);
902 }
903 
904 /*
905  * NB: callers *must* call dmu_recv_end() if this succeeds.
906  */
907 int
908 dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp)
909 {
910 	struct restorearg ra = { 0 };
911 	dmu_replay_record_t *drr;
912 	objset_t *os;
913 	zio_cksum_t pcksum;
914 
915 	if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC))
916 		ra.byteswap = TRUE;
917 
918 	{
919 		/* compute checksum of drr_begin record */
920 		dmu_replay_record_t *drr;
921 		drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
922 
923 		drr->drr_type = DRR_BEGIN;
924 		drr->drr_u.drr_begin = *drc->drc_drrb;
925 		if (ra.byteswap) {
926 			fletcher_4_incremental_byteswap(drr,
927 			    sizeof (dmu_replay_record_t), &ra.cksum);
928 		} else {
929 			fletcher_4_incremental_native(drr,
930 			    sizeof (dmu_replay_record_t), &ra.cksum);
931 		}
932 		kmem_free(drr, sizeof (dmu_replay_record_t));
933 	}
934 
935 	if (ra.byteswap) {
936 		struct drr_begin *drrb = drc->drc_drrb;
937 		drrb->drr_magic = BSWAP_64(drrb->drr_magic);
938 		drrb->drr_version = BSWAP_64(drrb->drr_version);
939 		drrb->drr_creation_time = BSWAP_64(drrb->drr_creation_time);
940 		drrb->drr_type = BSWAP_32(drrb->drr_type);
941 		drrb->drr_toguid = BSWAP_64(drrb->drr_toguid);
942 		drrb->drr_fromguid = BSWAP_64(drrb->drr_fromguid);
943 	}
944 
945 	ra.vp = vp;
946 	ra.voff = *voffp;
947 	ra.bufsize = 1<<20;
948 	ra.buf = kmem_alloc(ra.bufsize, KM_SLEEP);
949 
950 	/* these were verified in dmu_recv_begin */
951 	ASSERT(drc->drc_drrb->drr_version == DMU_BACKUP_STREAM_VERSION);
952 	ASSERT(drc->drc_drrb->drr_type < DMU_OST_NUMTYPES);
953 
954 	/*
955 	 * Open the objset we are modifying.
956 	 */
957 	VERIFY(dmu_objset_open_ds(drc->drc_real_ds, DMU_OST_ANY, &os) == 0);
958 
959 	ASSERT(drc->drc_real_ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT);
960 
961 	/*
962 	 * Read records and process them.
963 	 */
964 	pcksum = ra.cksum;
965 	while (ra.err == 0 &&
966 	    NULL != (drr = restore_read(&ra, sizeof (*drr)))) {
967 		if (issig(JUSTLOOKING) && issig(FORREAL)) {
968 			ra.err = EINTR;
969 			goto out;
970 		}
971 
972 		if (ra.byteswap)
973 			backup_byteswap(drr);
974 
975 		switch (drr->drr_type) {
976 		case DRR_OBJECT:
977 		{
978 			/*
979 			 * We need to make a copy of the record header,
980 			 * because restore_{object,write} may need to
981 			 * restore_read(), which will invalidate drr.
982 			 */
983 			struct drr_object drro = drr->drr_u.drr_object;
984 			ra.err = restore_object(&ra, os, &drro);
985 			break;
986 		}
987 		case DRR_FREEOBJECTS:
988 		{
989 			struct drr_freeobjects drrfo =
990 			    drr->drr_u.drr_freeobjects;
991 			ra.err = restore_freeobjects(&ra, os, &drrfo);
992 			break;
993 		}
994 		case DRR_WRITE:
995 		{
996 			struct drr_write drrw = drr->drr_u.drr_write;
997 			ra.err = restore_write(&ra, os, &drrw);
998 			break;
999 		}
1000 		case DRR_FREE:
1001 		{
1002 			struct drr_free drrf = drr->drr_u.drr_free;
1003 			ra.err = restore_free(&ra, os, &drrf);
1004 			break;
1005 		}
1006 		case DRR_END:
1007 		{
1008 			struct drr_end drre = drr->drr_u.drr_end;
1009 			/*
1010 			 * We compare against the *previous* checksum
1011 			 * value, because the stored checksum is of
1012 			 * everything before the DRR_END record.
1013 			 */
1014 			if (!ZIO_CHECKSUM_EQUAL(drre.drr_checksum, pcksum))
1015 				ra.err = ECKSUM;
1016 			goto out;
1017 		}
1018 		default:
1019 			ra.err = EINVAL;
1020 			goto out;
1021 		}
1022 		pcksum = ra.cksum;
1023 	}
1024 	ASSERT(ra.err != 0);
1025 
1026 out:
1027 	dmu_objset_close(os);
1028 
1029 	if (ra.err != 0) {
1030 		/*
1031 		 * destroy what we created, so we don't leave it in the
1032 		 * inconsistent restoring state.
1033 		 */
1034 		txg_wait_synced(drc->drc_real_ds->ds_dir->dd_pool, 0);
1035 
1036 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag,
1037 		    B_FALSE);
1038 		if (drc->drc_real_ds != drc->drc_logical_ds) {
1039 			mutex_exit(&drc->drc_logical_ds->ds_recvlock);
1040 			dsl_dataset_rele(drc->drc_logical_ds, dmu_recv_tag);
1041 		}
1042 	}
1043 
1044 	kmem_free(ra.buf, ra.bufsize);
1045 	*voffp = ra.voff;
1046 	return (ra.err);
1047 }
1048 
1049 struct recvendsyncarg {
1050 	char *tosnap;
1051 	uint64_t creation_time;
1052 	uint64_t toguid;
1053 };
1054 
1055 static int
1056 recv_end_check(void *arg1, void *arg2, dmu_tx_t *tx)
1057 {
1058 	dsl_dataset_t *ds = arg1;
1059 	struct recvendsyncarg *resa = arg2;
1060 
1061 	return (dsl_dataset_snapshot_check(ds, resa->tosnap, tx));
1062 }
1063 
1064 static void
1065 recv_end_sync(void *arg1, void *arg2, cred_t *cr, dmu_tx_t *tx)
1066 {
1067 	dsl_dataset_t *ds = arg1;
1068 	struct recvendsyncarg *resa = arg2;
1069 
1070 	dsl_dataset_snapshot_sync(ds, resa->tosnap, cr, tx);
1071 
1072 	/* set snapshot's creation time and guid */
1073 	dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
1074 	ds->ds_prev->ds_phys->ds_creation_time = resa->creation_time;
1075 	ds->ds_prev->ds_phys->ds_guid = resa->toguid;
1076 	ds->ds_prev->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
1077 
1078 	dmu_buf_will_dirty(ds->ds_dbuf, tx);
1079 	ds->ds_phys->ds_flags &= ~DS_FLAG_INCONSISTENT;
1080 }
1081 
1082 int
1083 dmu_recv_end(dmu_recv_cookie_t *drc)
1084 {
1085 	struct recvendsyncarg resa;
1086 	dsl_dataset_t *ds = drc->drc_logical_ds;
1087 	int err;
1088 
1089 	/*
1090 	 * XXX hack; seems the ds is still dirty and
1091 	 * dsl_pool_zil_clean() expects it to have a ds_user_ptr
1092 	 * (and zil), but clone_swap() can close it.
1093 	 */
1094 	txg_wait_synced(ds->ds_dir->dd_pool, 0);
1095 
1096 	if (ds != drc->drc_real_ds) {
1097 		/* we are doing an online recv */
1098 		if (dsl_dataset_tryown(ds, FALSE, dmu_recv_tag)) {
1099 			err = dsl_dataset_clone_swap(drc->drc_real_ds, ds,
1100 			    drc->drc_force);
1101 			if (err)
1102 				dsl_dataset_disown(ds, dmu_recv_tag);
1103 		} else {
1104 			err = EBUSY;
1105 			dsl_dataset_rele(ds, dmu_recv_tag);
1106 		}
1107 		/* dsl_dataset_destroy() will disown the ds */
1108 		(void) dsl_dataset_destroy(drc->drc_real_ds, dmu_recv_tag,
1109 		    B_FALSE);
1110 		mutex_exit(&drc->drc_logical_ds->ds_recvlock);
1111 		if (err)
1112 			return (err);
1113 	}
1114 
1115 	resa.creation_time = drc->drc_drrb->drr_creation_time;
1116 	resa.toguid = drc->drc_drrb->drr_toguid;
1117 	resa.tosnap = drc->drc_tosnap;
1118 
1119 	err = dsl_sync_task_do(ds->ds_dir->dd_pool,
1120 	    recv_end_check, recv_end_sync, ds, &resa, 3);
1121 	if (err) {
1122 		if (drc->drc_newfs) {
1123 			ASSERT(ds == drc->drc_real_ds);
1124 			(void) dsl_dataset_destroy(ds, dmu_recv_tag,
1125 			    B_FALSE);
1126 			return (err);
1127 		} else {
1128 			(void) dsl_dataset_rollback(ds, DMU_OST_NONE);
1129 		}
1130 	}
1131 
1132 	/* release the hold from dmu_recv_begin */
1133 	dsl_dataset_disown(ds, dmu_recv_tag);
1134 	return (err);
1135 }
1136