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