]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/dmu_send.c
New upstream version 0.7.2
[mirror_zfs-debian.git] / module / zfs / dmu_send.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
8d35c149 23 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
cae5b340 24 * Copyright (c) 2011, 2015 by Delphix. All rights reserved.
e10b0808 25 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
cae5b340
AX
26 * Copyright 2014 HybridCluster. All rights reserved.
27 * Copyright 2016 RackTop Systems.
4e820b5a 28 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
8d35c149 29 */
34dc7c2f 30
34dc7c2f
BB
31#include <sys/dmu.h>
32#include <sys/dmu_impl.h>
33#include <sys/dmu_tx.h>
34#include <sys/dbuf.h>
35#include <sys/dnode.h>
36#include <sys/zfs_context.h>
37#include <sys/dmu_objset.h>
38#include <sys/dmu_traverse.h>
39#include <sys/dsl_dataset.h>
40#include <sys/dsl_dir.h>
428870ff 41#include <sys/dsl_prop.h>
34dc7c2f
BB
42#include <sys/dsl_pool.h>
43#include <sys/dsl_synctask.h>
c06d4368 44#include <sys/spa_impl.h>
34dc7c2f
BB
45#include <sys/zfs_ioctl.h>
46#include <sys/zap.h>
47#include <sys/zio_checksum.h>
428870ff
BB
48#include <sys/zfs_znode.h>
49#include <zfs_fletcher.h>
50#include <sys/avl.h>
51#include <sys/ddt.h>
572e2857 52#include <sys/zfs_onexit.h>
a08ee875
LG
53#include <sys/dmu_send.h>
54#include <sys/dsl_destroy.h>
ea04106b
AX
55#include <sys/blkptr.h>
56#include <sys/dsl_bookmark.h>
57#include <sys/zfeature.h>
cae5b340 58#include <sys/bqueue.h>
4e820b5a 59#include <sys/zvol.h>
cae5b340 60#include <sys/policy.h>
34dc7c2f 61
330d06f9
MA
62/* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
63int zfs_send_corrupt_data = B_FALSE;
cae5b340
AX
64int zfs_send_queue_length = 16 * 1024 * 1024;
65int zfs_recv_queue_length = 16 * 1024 * 1024;
66/* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
67int zfs_send_set_freerecords_bit = B_TRUE;
330d06f9 68
34dc7c2f 69static char *dmu_recv_tag = "dmu_recv_tag";
cae5b340
AX
70const char *recv_clone_name = "%recv";
71
72#define BP_SPAN(datablkszsec, indblkshift, level) \
73 (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
74 (level) * (indblkshift - SPA_BLKPTRSHIFT)))
75
76static void byteswap_record(dmu_replay_record_t *drr);
77
78struct send_thread_arg {
79 bqueue_t q;
80 dsl_dataset_t *ds; /* Dataset to traverse */
81 uint64_t fromtxg; /* Traverse from this txg */
82 int flags; /* flags to pass to traverse_dataset */
83 int error_code;
84 boolean_t cancel;
85 zbookmark_phys_t resume;
86};
87
88struct send_block_record {
89 boolean_t eos_marker; /* Marks the end of the stream */
90 blkptr_t bp;
91 zbookmark_phys_t zb;
92 uint8_t indblkshift;
93 uint16_t datablkszsec;
94 bqueue_node_t ln;
95};
34dc7c2f 96
c06d4368
AX
97typedef struct dump_bytes_io {
98 dmu_sendarg_t *dbi_dsp;
99 void *dbi_buf;
100 int dbi_len;
101} dump_bytes_io_t;
102
103static void
68d83c55 104dump_bytes_cb(void *arg)
34dc7c2f 105{
c06d4368
AX
106 dump_bytes_io_t *dbi = (dump_bytes_io_t *)arg;
107 dmu_sendarg_t *dsp = dbi->dbi_dsp;
cae5b340 108 dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
34dc7c2f 109 ssize_t resid; /* have to get resid to get detailed errno */
cae5b340
AX
110
111 /*
112 * The code does not rely on this (len being a multiple of 8). We keep
113 * this assertion because of the corresponding assertion in
114 * receive_read(). Keeping this assertion ensures that we do not
115 * inadvertently break backwards compatibility (causing the assertion
116 * in receive_read() to trigger on old software).
117 *
118 * Removing the assertions could be rolled into a new feature that uses
119 * data that isn't 8-byte aligned; if the assertions were removed, a
120 * feature flag would have to be added.
121 */
122
c06d4368 123 ASSERT0(dbi->dbi_len % 8);
34dc7c2f 124
37abac6d 125 dsp->dsa_err = vn_rdwr(UIO_WRITE, dsp->dsa_vp,
c06d4368 126 (caddr_t)dbi->dbi_buf, dbi->dbi_len,
34dc7c2f 127 0, UIO_SYSSPACE, FAPPEND, RLIM64_INFINITY, CRED(), &resid);
37abac6d
BP
128
129 mutex_enter(&ds->ds_sendstream_lock);
c06d4368 130 *dsp->dsa_off += dbi->dbi_len;
37abac6d 131 mutex_exit(&ds->ds_sendstream_lock);
c06d4368
AX
132}
133
134static int
135dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
136{
137 dump_bytes_io_t dbi;
138
139 dbi.dbi_dsp = dsp;
140 dbi.dbi_buf = buf;
141 dbi.dbi_len = len;
142
68d83c55
AX
143#if defined(HAVE_LARGE_STACKS)
144 dump_bytes_cb(&dbi);
145#else
c06d4368
AX
146 /*
147 * The vn_rdwr() call is performed in a taskq to ensure that there is
148 * always enough stack space to write safely to the target filesystem.
149 * The ZIO_TYPE_FREE threads are used because there can be a lot of
150 * them and they are used in vdev_file.c for a similar purpose.
151 */
152 spa_taskq_dispatch_sync(dmu_objset_spa(dsp->dsa_os), ZIO_TYPE_FREE,
68d83c55
AX
153 ZIO_TASKQ_ISSUE, dump_bytes_cb, &dbi, TQ_SLEEP);
154#endif /* HAVE_LARGE_STACKS */
37abac6d
BP
155
156 return (dsp->dsa_err);
34dc7c2f
BB
157}
158
cae5b340
AX
159/*
160 * For all record types except BEGIN, fill in the checksum (overlaid in
161 * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
162 * up to the start of the checksum itself.
163 */
164static int
165dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
166{
167 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
168 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
169 (void) fletcher_4_incremental_native(dsp->dsa_drr,
170 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
171 &dsp->dsa_zc);
172 if (dsp->dsa_drr->drr_type == DRR_BEGIN) {
173 dsp->dsa_sent_begin = B_TRUE;
174 } else {
175 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
176 drr_checksum.drr_checksum));
177 dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
178 }
179 if (dsp->dsa_drr->drr_type == DRR_END) {
180 dsp->dsa_sent_end = B_TRUE;
181 }
182 (void) fletcher_4_incremental_native(&dsp->dsa_drr->
183 drr_u.drr_checksum.drr_checksum,
184 sizeof (zio_cksum_t), &dsp->dsa_zc);
185 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
186 return (SET_ERROR(EINTR));
187 if (payload_len != 0) {
188 (void) fletcher_4_incremental_native(payload, payload_len,
189 &dsp->dsa_zc);
190 if (dump_bytes(dsp, payload, payload_len) != 0)
191 return (SET_ERROR(EINTR));
192 }
193 return (0);
194}
195
196/*
197 * Fill in the drr_free struct, or perform aggregation if the previous record is
198 * also a free record, and the two are adjacent.
199 *
200 * Note that we send free records even for a full send, because we want to be
201 * able to receive a full send as a clone, which requires a list of all the free
202 * and freeobject records that were generated on the source.
203 */
34dc7c2f 204static int
37abac6d 205dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
34dc7c2f
BB
206 uint64_t length)
207{
37abac6d 208 struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
428870ff 209
a08ee875
LG
210 /*
211 * When we receive a free record, dbuf_free_range() assumes
212 * that the receiving system doesn't have any dbufs in the range
213 * being freed. This is always true because there is a one-record
214 * constraint: we only send one WRITE record for any given
cae5b340 215 * object,offset. We know that the one-record constraint is
a08ee875
LG
216 * true because we always send data in increasing order by
217 * object,offset.
218 *
219 * If the increasing-order constraint ever changes, we should find
220 * another way to assert that the one-record constraint is still
221 * satisfied.
222 */
223 ASSERT(object > dsp->dsa_last_data_object ||
224 (object == dsp->dsa_last_data_object &&
225 offset > dsp->dsa_last_data_offset));
226
c578f007
SK
227 if (length != -1ULL && offset + length < offset)
228 length = -1ULL;
229
428870ff
BB
230 /*
231 * If there is a pending op, but it's not PENDING_FREE, push it out,
232 * since free block aggregation can only be done for blocks of the
233 * same type (i.e., DRR_FREE records can only be aggregated with
234 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
235 * aggregated with other DRR_FREEOBJECTS records.
236 */
37abac6d
BP
237 if (dsp->dsa_pending_op != PENDING_NONE &&
238 dsp->dsa_pending_op != PENDING_FREE) {
cae5b340 239 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 240 return (SET_ERROR(EINTR));
37abac6d 241 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
242 }
243
37abac6d 244 if (dsp->dsa_pending_op == PENDING_FREE) {
428870ff
BB
245 /*
246 * There should never be a PENDING_FREE if length is -1
247 * (because dump_dnode is the only place where this
248 * function is called with a -1, and only after flushing
249 * any pending record).
250 */
251 ASSERT(length != -1ULL);
252 /*
253 * Check to see whether this free block can be aggregated
254 * with pending one.
255 */
256 if (drrf->drr_object == object && drrf->drr_offset +
257 drrf->drr_length == offset) {
258 drrf->drr_length += length;
259 return (0);
260 } else {
261 /* not a continuation. Push out pending record */
cae5b340 262 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 263 return (SET_ERROR(EINTR));
37abac6d 264 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
265 }
266 }
267 /* create a FREE record and make it pending */
37abac6d
BP
268 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
269 dsp->dsa_drr->drr_type = DRR_FREE;
428870ff
BB
270 drrf->drr_object = object;
271 drrf->drr_offset = offset;
272 drrf->drr_length = length;
37abac6d 273 drrf->drr_toguid = dsp->dsa_toguid;
428870ff 274 if (length == -1ULL) {
cae5b340 275 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 276 return (SET_ERROR(EINTR));
428870ff 277 } else {
37abac6d 278 dsp->dsa_pending_op = PENDING_FREE;
428870ff 279 }
34dc7c2f 280
34dc7c2f
BB
281 return (0);
282}
283
284static int
ea04106b 285dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
cae5b340
AX
286 uint64_t object, uint64_t offset, int lsize, int psize, const blkptr_t *bp,
287 void *data)
34dc7c2f 288{
cae5b340 289 uint64_t payload_size;
37abac6d 290 struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
428870ff 291
a08ee875
LG
292 /*
293 * We send data in increasing object, offset order.
294 * See comment in dump_free() for details.
295 */
296 ASSERT(object > dsp->dsa_last_data_object ||
297 (object == dsp->dsa_last_data_object &&
298 offset > dsp->dsa_last_data_offset));
299 dsp->dsa_last_data_object = object;
cae5b340 300 dsp->dsa_last_data_offset = offset + lsize - 1;
428870ff
BB
301
302 /*
303 * If there is any kind of pending aggregation (currently either
304 * a grouping of free objects or free blocks), push it out to
305 * the stream, since aggregation can't be done across operations
306 * of different types.
307 */
37abac6d 308 if (dsp->dsa_pending_op != PENDING_NONE) {
cae5b340 309 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 310 return (SET_ERROR(EINTR));
37abac6d 311 dsp->dsa_pending_op = PENDING_NONE;
428870ff 312 }
cae5b340 313 /* write a WRITE record */
37abac6d
BP
314 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
315 dsp->dsa_drr->drr_type = DRR_WRITE;
428870ff
BB
316 drrw->drr_object = object;
317 drrw->drr_type = type;
318 drrw->drr_offset = offset;
37abac6d 319 drrw->drr_toguid = dsp->dsa_toguid;
cae5b340
AX
320 drrw->drr_logical_size = lsize;
321
322 /* only set the compression fields if the buf is compressed */
323 if (lsize != psize) {
324 ASSERT(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED);
325 ASSERT(!BP_IS_EMBEDDED(bp));
326 ASSERT(!BP_SHOULD_BYTESWAP(bp));
327 ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)));
328 ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF);
329 ASSERT3S(psize, >, 0);
330 ASSERT3S(lsize, >=, psize);
331
332 drrw->drr_compressiontype = BP_GET_COMPRESS(bp);
333 drrw->drr_compressed_size = psize;
334 payload_size = drrw->drr_compressed_size;
335 } else {
336 payload_size = drrw->drr_logical_size;
337 }
338
e10b0808 339 if (bp == NULL || BP_IS_EMBEDDED(bp)) {
ea04106b 340 /*
e10b0808
AX
341 * There's no pre-computed checksum for partial-block
342 * writes or embedded BP's, so (like
343 * fletcher4-checkummed blocks) userland will have to
344 * compute a dedup-capable checksum itself.
ea04106b
AX
345 */
346 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
347 } else {
348 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
cae5b340
AX
349 if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
350 ZCHECKSUM_FLAG_DEDUP)
ea04106b
AX
351 drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
352 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
353 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
354 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
355 drrw->drr_key.ddk_cksum = bp->blk_cksum;
356 }
428870ff 357
cae5b340 358 if (dump_record(dsp, data, payload_size) != 0)
a08ee875 359 return (SET_ERROR(EINTR));
428870ff
BB
360 return (0);
361}
362
ea04106b
AX
363static int
364dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
365 int blksz, const blkptr_t *bp)
366{
367 char buf[BPE_PAYLOAD_SIZE];
368 struct drr_write_embedded *drrw =
369 &(dsp->dsa_drr->drr_u.drr_write_embedded);
370
371 if (dsp->dsa_pending_op != PENDING_NONE) {
cae5b340 372 if (dump_record(dsp, NULL, 0) != 0)
ea04106b
AX
373 return (EINTR);
374 dsp->dsa_pending_op = PENDING_NONE;
375 }
376
377 ASSERT(BP_IS_EMBEDDED(bp));
378
379 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
380 dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
381 drrw->drr_object = object;
382 drrw->drr_offset = offset;
383 drrw->drr_length = blksz;
384 drrw->drr_toguid = dsp->dsa_toguid;
385 drrw->drr_compression = BP_GET_COMPRESS(bp);
386 drrw->drr_etype = BPE_GET_ETYPE(bp);
387 drrw->drr_lsize = BPE_GET_LSIZE(bp);
388 drrw->drr_psize = BPE_GET_PSIZE(bp);
389
390 decode_embedded_bp_compressed(bp, buf);
391
cae5b340 392 if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
ea04106b
AX
393 return (EINTR);
394 return (0);
395}
396
428870ff 397static int
37abac6d 398dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
428870ff 399{
37abac6d 400 struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
428870ff 401
37abac6d 402 if (dsp->dsa_pending_op != PENDING_NONE) {
cae5b340 403 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 404 return (SET_ERROR(EINTR));
37abac6d 405 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
406 }
407
408 /* write a SPILL record */
37abac6d
BP
409 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
410 dsp->dsa_drr->drr_type = DRR_SPILL;
428870ff
BB
411 drrs->drr_object = object;
412 drrs->drr_length = blksz;
37abac6d 413 drrs->drr_toguid = dsp->dsa_toguid;
34dc7c2f 414
cae5b340 415 if (dump_record(dsp, data, blksz) != 0)
a08ee875 416 return (SET_ERROR(EINTR));
34dc7c2f
BB
417 return (0);
418}
419
420static int
37abac6d 421dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
34dc7c2f 422{
37abac6d 423 struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
428870ff
BB
424
425 /*
426 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
427 * push it out, since free block aggregation can only be done for
428 * blocks of the same type (i.e., DRR_FREE records can only be
429 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
430 * can only be aggregated with other DRR_FREEOBJECTS records.
431 */
37abac6d
BP
432 if (dsp->dsa_pending_op != PENDING_NONE &&
433 dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
cae5b340 434 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 435 return (SET_ERROR(EINTR));
37abac6d 436 dsp->dsa_pending_op = PENDING_NONE;
428870ff 437 }
37abac6d 438 if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
428870ff
BB
439 /*
440 * See whether this free object array can be aggregated
441 * with pending one
442 */
443 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
444 drrfo->drr_numobjs += numobjs;
445 return (0);
446 } else {
447 /* can't be aggregated. Push out pending record */
cae5b340 448 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 449 return (SET_ERROR(EINTR));
37abac6d 450 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
451 }
452 }
453
34dc7c2f 454 /* write a FREEOBJECTS record */
37abac6d
BP
455 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
456 dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
428870ff
BB
457 drrfo->drr_firstobj = firstobj;
458 drrfo->drr_numobjs = numobjs;
37abac6d 459 drrfo->drr_toguid = dsp->dsa_toguid;
428870ff 460
37abac6d 461 dsp->dsa_pending_op = PENDING_FREEOBJECTS;
34dc7c2f 462
34dc7c2f
BB
463 return (0);
464}
465
466static int
37abac6d 467dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
34dc7c2f 468{
37abac6d 469 struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
428870ff 470
cae5b340
AX
471 if (object < dsp->dsa_resume_object) {
472 /*
473 * Note: when resuming, we will visit all the dnodes in
474 * the block of dnodes that we are resuming from. In
475 * this case it's unnecessary to send the dnodes prior to
476 * the one we are resuming from. We should be at most one
477 * block's worth of dnodes behind the resume point.
478 */
479 ASSERT3U(dsp->dsa_resume_object - object, <,
480 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
481 return (0);
482 }
483
34dc7c2f 484 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
37abac6d 485 return (dump_freeobjects(dsp, object, 1));
34dc7c2f 486
37abac6d 487 if (dsp->dsa_pending_op != PENDING_NONE) {
cae5b340 488 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 489 return (SET_ERROR(EINTR));
37abac6d 490 dsp->dsa_pending_op = PENDING_NONE;
428870ff
BB
491 }
492
34dc7c2f 493 /* write an OBJECT record */
37abac6d
BP
494 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
495 dsp->dsa_drr->drr_type = DRR_OBJECT;
428870ff
BB
496 drro->drr_object = object;
497 drro->drr_type = dnp->dn_type;
498 drro->drr_bonustype = dnp->dn_bonustype;
499 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
500 drro->drr_bonuslen = dnp->dn_bonuslen;
cae5b340 501 drro->drr_dn_slots = dnp->dn_extra_slots + 1;
428870ff
BB
502 drro->drr_checksumtype = dnp->dn_checksum;
503 drro->drr_compress = dnp->dn_compress;
37abac6d 504 drro->drr_toguid = dsp->dsa_toguid;
428870ff 505
e10b0808
AX
506 if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
507 drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
508 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
509
cae5b340
AX
510 if (dump_record(dsp, DN_BONUS(dnp),
511 P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
a08ee875 512 return (SET_ERROR(EINTR));
cae5b340 513 }
34dc7c2f 514
a08ee875 515 /* Free anything past the end of the file. */
37abac6d 516 if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
a08ee875
LG
517 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
518 return (SET_ERROR(EINTR));
519 if (dsp->dsa_err != 0)
520 return (SET_ERROR(EINTR));
34dc7c2f
BB
521 return (0);
522}
523
ea04106b
AX
524static boolean_t
525backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
526{
527 if (!BP_IS_EMBEDDED(bp))
528 return (B_FALSE);
529
530 /*
531 * Compression function must be legacy, or explicitly enabled.
532 */
533 if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
cae5b340 534 !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LZ4)))
ea04106b
AX
535 return (B_FALSE);
536
537 /*
538 * Embed type must be explicitly enabled.
539 */
540 switch (BPE_GET_ETYPE(bp)) {
541 case BP_EMBEDDED_TYPE_DATA:
542 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
543 return (B_TRUE);
544 break;
545 default:
546 return (B_FALSE);
547 }
548 return (B_FALSE);
549}
550
cae5b340
AX
551/*
552 * This is the callback function to traverse_dataset that acts as the worker
553 * thread for dmu_send_impl.
554 */
555/*ARGSUSED*/
556static int
557send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
558 const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
559{
560 struct send_thread_arg *sta = arg;
561 struct send_block_record *record;
562 uint64_t record_size;
563 int err = 0;
564
565 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
566 zb->zb_object >= sta->resume.zb_object);
34dc7c2f 567
cae5b340
AX
568 if (sta->cancel)
569 return (SET_ERROR(EINTR));
570
571 if (bp == NULL) {
572 ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
573 return (0);
574 } else if (zb->zb_level < 0) {
575 return (0);
576 }
577
578 record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
579 record->eos_marker = B_FALSE;
580 record->bp = *bp;
581 record->zb = *zb;
582 record->indblkshift = dnp->dn_indblkshift;
583 record->datablkszsec = dnp->dn_datablkszsec;
584 record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
585 bqueue_enqueue(&sta->q, record, record_size);
586
587 return (err);
588}
589
590/*
591 * This function kicks off the traverse_dataset. It also handles setting the
592 * error code of the thread in case something goes wrong, and pushes the End of
593 * Stream record when the traverse_dataset call has finished. If there is no
594 * dataset to traverse, the thread immediately pushes End of Stream marker.
595 */
596static void
597send_traverse_thread(void *arg)
598{
599 struct send_thread_arg *st_arg = arg;
600 int err;
601 struct send_block_record *data;
602 fstrans_cookie_t cookie = spl_fstrans_mark();
603
604 if (st_arg->ds != NULL) {
605 err = traverse_dataset_resume(st_arg->ds,
606 st_arg->fromtxg, &st_arg->resume,
607 st_arg->flags, send_cb, st_arg);
608
609 if (err != EINTR)
610 st_arg->error_code = err;
611 }
612 data = kmem_zalloc(sizeof (*data), KM_SLEEP);
613 data->eos_marker = B_TRUE;
614 bqueue_enqueue(&st_arg->q, data, 1);
615 spl_fstrans_unmark(cookie);
616 thread_exit();
617}
618
619/*
620 * This function actually handles figuring out what kind of record needs to be
621 * dumped, reading the data (which has hopefully been prefetched), and calling
622 * the appropriate helper function.
623 */
34dc7c2f 624static int
cae5b340 625do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
34dc7c2f 626{
cae5b340
AX
627 dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
628 const blkptr_t *bp = &data->bp;
629 const zbookmark_phys_t *zb = &data->zb;
630 uint8_t indblkshift = data->indblkshift;
631 uint16_t dblkszsec = data->datablkszsec;
632 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
34dc7c2f 633 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
34dc7c2f 634 int err = 0;
cae5b340 635 uint64_t dnobj;
34dc7c2f 636
cae5b340
AX
637 ASSERT3U(zb->zb_level, >=, 0);
638
639 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
640 zb->zb_object >= dsa->dsa_resume_object);
34dc7c2f 641
428870ff
BB
642 if (zb->zb_object != DMU_META_DNODE_OBJECT &&
643 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
9babb374 644 return (0);
ea04106b
AX
645 } else if (BP_IS_HOLE(bp) &&
646 zb->zb_object == DMU_META_DNODE_OBJECT) {
cae5b340 647 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
b128c09f 648 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
cae5b340 649 err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
ea04106b 650 } else if (BP_IS_HOLE(bp)) {
cae5b340
AX
651 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
652 uint64_t offset = zb->zb_blkid * span;
653 err = dump_free(dsa, zb->zb_object, offset, span);
b128c09f
BB
654 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
655 return (0);
656 } else if (type == DMU_OT_DNODE) {
657 dnode_phys_t *blk;
cae5b340 658 int epb = BP_GET_LSIZE(bp) >> DNODE_SHIFT;
e10b0808 659 arc_flags_t aflags = ARC_FLAG_WAIT;
b128c09f 660 arc_buf_t *abuf;
cae5b340
AX
661 int i;
662
663 ASSERT0(zb->zb_level);
b128c09f 664
c06d4368
AX
665 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
666 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
667 &aflags, zb) != 0)
a08ee875 668 return (SET_ERROR(EIO));
34dc7c2f 669
b128c09f 670 blk = abuf->b_data;
cae5b340
AX
671 dnobj = zb->zb_blkid * epb;
672 for (i = 0; i < epb; i += blk[i].dn_extra_slots + 1) {
673 err = dump_dnode(dsa, dnobj + i, blk + i);
a08ee875 674 if (err != 0)
34dc7c2f
BB
675 break;
676 }
cae5b340 677 arc_buf_destroy(abuf, &abuf);
428870ff 678 } else if (type == DMU_OT_SA) {
e10b0808 679 arc_flags_t aflags = ARC_FLAG_WAIT;
b128c09f 680 arc_buf_t *abuf;
34dc7c2f 681 int blksz = BP_GET_LSIZE(bp);
b128c09f 682
c06d4368
AX
683 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
684 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
685 &aflags, zb) != 0)
a08ee875 686 return (SET_ERROR(EIO));
b128c09f 687
cae5b340
AX
688 err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
689 arc_buf_destroy(abuf, &abuf);
690 } else if (backup_do_embed(dsa, bp)) {
ea04106b 691 /* it's an embedded level-0 block of a regular object */
cae5b340
AX
692 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
693 ASSERT0(zb->zb_level);
694 err = dump_write_embedded(dsa, zb->zb_object,
ea04106b 695 zb->zb_blkid * blksz, blksz, bp);
cae5b340
AX
696 } else {
697 /* it's a level-0 block of a regular object */
e10b0808 698 arc_flags_t aflags = ARC_FLAG_WAIT;
428870ff 699 arc_buf_t *abuf;
cae5b340
AX
700 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
701 uint64_t offset;
702
703 /*
704 * If we have large blocks stored on disk but the send flags
705 * don't allow us to send large blocks, we split the data from
706 * the arc buf into chunks.
707 */
708 boolean_t split_large_blocks = blksz > SPA_OLD_MAXBLOCKSIZE &&
709 !(dsa->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS);
710 /*
711 * We should only request compressed data from the ARC if all
712 * the following are true:
713 * - stream compression was requested
714 * - we aren't splitting large blocks into smaller chunks
715 * - the data won't need to be byteswapped before sending
716 * - this isn't an embedded block
717 * - this isn't metadata (if receiving on a different endian
718 * system it can be byteswapped more easily)
719 */
720 boolean_t request_compressed =
721 (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED) &&
722 !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) &&
723 !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp));
428870ff 724
ea04106b 725 ASSERT0(zb->zb_level);
cae5b340
AX
726 ASSERT(zb->zb_object > dsa->dsa_resume_object ||
727 (zb->zb_object == dsa->dsa_resume_object &&
728 zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
729
730 ASSERT3U(blksz, ==, BP_GET_LSIZE(bp));
731
732 enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
733 if (request_compressed)
734 zioflags |= ZIO_FLAG_RAW;
735
c06d4368 736 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
cae5b340 737 ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) {
330d06f9 738 if (zfs_send_corrupt_data) {
330d06f9 739 /* Send a block filled with 0x"zfs badd bloc" */
cae5b340
AX
740 abuf = arc_alloc_buf(spa, &abuf, ARC_BUFC_DATA,
741 blksz);
742 uint64_t *ptr;
330d06f9
MA
743 for (ptr = abuf->b_data;
744 (char *)ptr < (char *)abuf->b_data + blksz;
745 ptr++)
dd26aa53 746 *ptr = 0x2f5baddb10cULL;
330d06f9 747 } else {
a08ee875 748 return (SET_ERROR(EIO));
330d06f9
MA
749 }
750 }
428870ff 751
e10b0808
AX
752 offset = zb->zb_blkid * blksz;
753
cae5b340
AX
754 if (split_large_blocks) {
755 ASSERT3U(arc_get_compression(abuf), ==,
756 ZIO_COMPRESS_OFF);
e10b0808
AX
757 char *buf = abuf->b_data;
758 while (blksz > 0 && err == 0) {
759 int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
cae5b340
AX
760 err = dump_write(dsa, type, zb->zb_object,
761 offset, n, n, NULL, buf);
e10b0808
AX
762 offset += n;
763 buf += n;
764 blksz -= n;
765 }
766 } else {
cae5b340
AX
767 err = dump_write(dsa, type, zb->zb_object, offset,
768 blksz, arc_buf_size(abuf), bp,
769 abuf->b_data);
e10b0808 770 }
cae5b340 771 arc_buf_destroy(abuf, &abuf);
34dc7c2f
BB
772 }
773
774 ASSERT(err == 0 || err == EINTR);
775 return (err);
776}
777
a08ee875 778/*
cae5b340
AX
779 * Pop the new data off the queue, and free the old data.
780 */
781static struct send_block_record *
782get_next_record(bqueue_t *bq, struct send_block_record *data)
783{
784 struct send_block_record *tmp = bqueue_dequeue(bq);
785 kmem_free(data, sizeof (*data));
786 return (tmp);
787}
788
789/*
790 * Actually do the bulk of the work in a zfs send.
791 *
792 * Note: Releases dp using the specified tag.
a08ee875
LG
793 */
794static int
cae5b340
AX
795dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
796 zfs_bookmark_phys_t *ancestor_zb, boolean_t is_clone,
797 boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
798 int outfd, uint64_t resumeobj, uint64_t resumeoff,
799 vnode_t *vp, offset_t *off)
34dc7c2f 800{
a08ee875 801 objset_t *os;
34dc7c2f 802 dmu_replay_record_t *drr;
37abac6d 803 dmu_sendarg_t *dsp;
34dc7c2f
BB
804 int err;
805 uint64_t fromtxg = 0;
ea04106b 806 uint64_t featureflags = 0;
cae5b340
AX
807 struct send_thread_arg to_arg;
808 void *payload = NULL;
809 size_t payload_len = 0;
810 struct send_block_record *to_data;
34dc7c2f 811
cae5b340 812 err = dmu_objset_from_ds(to_ds, &os);
a08ee875 813 if (err != 0) {
a08ee875
LG
814 dsl_pool_rele(dp, tag);
815 return (err);
816 }
34dc7c2f
BB
817
818 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
819 drr->drr_type = DRR_BEGIN;
820 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
428870ff
BB
821 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
822 DMU_SUBSTREAM);
823
cae5b340
AX
824 bzero(&to_arg, sizeof (to_arg));
825
428870ff 826#ifdef _KERNEL
a08ee875 827 if (dmu_objset_type(os) == DMU_OST_ZFS) {
428870ff 828 uint64_t version;
a08ee875 829 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
37abac6d 830 kmem_free(drr, sizeof (dmu_replay_record_t));
a08ee875
LG
831 dsl_pool_rele(dp, tag);
832 return (SET_ERROR(EINVAL));
37abac6d 833 }
a08ee875 834 if (version >= ZPL_VERSION_SA) {
ea04106b 835 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
428870ff
BB
836 }
837 }
838#endif
839
cae5b340 840 if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
e10b0808 841 featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
cae5b340
AX
842 if (to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_DNODE])
843 featureflags |= DMU_BACKUP_FEATURE_LARGE_DNODE;
ea04106b
AX
844 if (embedok &&
845 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
846 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
cae5b340
AX
847 }
848 if (compressok) {
849 featureflags |= DMU_BACKUP_FEATURE_COMPRESSED;
850 }
851 if ((featureflags &
852 (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED)) !=
853 0 && spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) {
854 featureflags |= DMU_BACKUP_FEATURE_LZ4;
855 }
856
857 if (resumeobj != 0 || resumeoff != 0) {
858 featureflags |= DMU_BACKUP_FEATURE_RESUMING;
ea04106b
AX
859 }
860
861 DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
862 featureflags);
863
34dc7c2f 864 drr->drr_u.drr_begin.drr_creation_time =
cae5b340 865 dsl_dataset_phys(to_ds)->ds_creation_time;
a08ee875 866 drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
ea04106b 867 if (is_clone)
34dc7c2f 868 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
cae5b340
AX
869 drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
870 if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
34dc7c2f 871 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
cae5b340
AX
872 if (zfs_send_set_freerecords_bit)
873 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
34dc7c2f 874
cae5b340
AX
875 if (ancestor_zb != NULL) {
876 drr->drr_u.drr_begin.drr_fromguid =
877 ancestor_zb->zbm_guid;
878 fromtxg = ancestor_zb->zbm_creation_txg;
ea04106b 879 }
cae5b340
AX
880 dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
881 if (!to_ds->ds_is_snapshot) {
ea04106b
AX
882 (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
883 sizeof (drr->drr_u.drr_begin.drr_toname));
a08ee875 884 }
34dc7c2f 885
37abac6d
BP
886 dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
887
888 dsp->dsa_drr = drr;
889 dsp->dsa_vp = vp;
890 dsp->dsa_outfd = outfd;
891 dsp->dsa_proc = curproc;
a08ee875 892 dsp->dsa_os = os;
37abac6d 893 dsp->dsa_off = off;
cae5b340 894 dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
37abac6d 895 dsp->dsa_pending_op = PENDING_NONE;
ea04106b 896 dsp->dsa_featureflags = featureflags;
cae5b340
AX
897 dsp->dsa_resume_object = resumeobj;
898 dsp->dsa_resume_offset = resumeoff;
37abac6d 899
cae5b340
AX
900 mutex_enter(&to_ds->ds_sendstream_lock);
901 list_insert_head(&to_ds->ds_sendstreams, dsp);
902 mutex_exit(&to_ds->ds_sendstream_lock);
37abac6d 903
cae5b340 904 dsl_dataset_long_hold(to_ds, FTAG);
a08ee875
LG
905 dsl_pool_rele(dp, tag);
906
cae5b340
AX
907 if (resumeobj != 0 || resumeoff != 0) {
908 dmu_object_info_t to_doi;
909 nvlist_t *nvl;
910 err = dmu_object_info(os, resumeobj, &to_doi);
911 if (err != 0)
912 goto out;
913 SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
914 resumeoff / to_doi.doi_data_block_size);
915
916 nvl = fnvlist_alloc();
917 fnvlist_add_uint64(nvl, "resume_object", resumeobj);
918 fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
919 payload = fnvlist_pack(nvl, &payload_len);
920 drr->drr_payloadlen = payload_len;
921 fnvlist_free(nvl);
922 }
923
924 err = dump_record(dsp, payload, payload_len);
925 fnvlist_pack_free(payload, payload_len);
926 if (err != 0) {
37abac6d
BP
927 err = dsp->dsa_err;
928 goto out;
34dc7c2f
BB
929 }
930
cae5b340
AX
931 err = bqueue_init(&to_arg.q, zfs_send_queue_length,
932 offsetof(struct send_block_record, ln));
933 to_arg.error_code = 0;
934 to_arg.cancel = B_FALSE;
935 to_arg.ds = to_ds;
936 to_arg.fromtxg = fromtxg;
937 to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
938 (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, curproc,
939 TS_RUN, minclsyspri);
940
941 to_data = bqueue_dequeue(&to_arg.q);
942
943 while (!to_data->eos_marker && err == 0) {
944 err = do_dump(dsp, to_data);
945 to_data = get_next_record(&to_arg.q, to_data);
946 if (issig(JUSTLOOKING) && issig(FORREAL))
947 err = EINTR;
948 }
949
950 if (err != 0) {
951 to_arg.cancel = B_TRUE;
952 while (!to_data->eos_marker) {
953 to_data = get_next_record(&to_arg.q, to_data);
954 }
955 }
956 kmem_free(to_data, sizeof (*to_data));
957
958 bqueue_destroy(&to_arg.q);
959
960 if (err == 0 && to_arg.error_code != 0)
961 err = to_arg.error_code;
962
963 if (err != 0)
964 goto out;
34dc7c2f 965
37abac6d 966 if (dsp->dsa_pending_op != PENDING_NONE)
cae5b340 967 if (dump_record(dsp, NULL, 0) != 0)
a08ee875 968 err = SET_ERROR(EINTR);
428870ff 969
a08ee875
LG
970 if (err != 0) {
971 if (err == EINTR && dsp->dsa_err != 0)
37abac6d
BP
972 err = dsp->dsa_err;
973 goto out;
34dc7c2f
BB
974 }
975
976 bzero(drr, sizeof (dmu_replay_record_t));
977 drr->drr_type = DRR_END;
37abac6d
BP
978 drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
979 drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
34dc7c2f 980
cae5b340 981 if (dump_record(dsp, NULL, 0) != 0)
37abac6d 982 err = dsp->dsa_err;
34dc7c2f 983
37abac6d 984out:
cae5b340
AX
985 mutex_enter(&to_ds->ds_sendstream_lock);
986 list_remove(&to_ds->ds_sendstreams, dsp);
987 mutex_exit(&to_ds->ds_sendstream_lock);
988
989 VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end));
37abac6d 990
34dc7c2f 991 kmem_free(drr, sizeof (dmu_replay_record_t));
37abac6d 992 kmem_free(dsp, sizeof (dmu_sendarg_t));
34dc7c2f 993
cae5b340 994 dsl_dataset_long_rele(to_ds, FTAG);
a08ee875 995
37abac6d 996 return (err);
34dc7c2f
BB
997}
998
330d06f9 999int
a08ee875 1000dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
cae5b340 1001 boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
e10b0808 1002 int outfd, vnode_t *vp, offset_t *off)
330d06f9 1003{
a08ee875
LG
1004 dsl_pool_t *dp;
1005 dsl_dataset_t *ds;
1006 dsl_dataset_t *fromds = NULL;
330d06f9 1007 int err;
330d06f9 1008
a08ee875
LG
1009 err = dsl_pool_hold(pool, FTAG, &dp);
1010 if (err != 0)
1011 return (err);
1012
1013 err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
1014 if (err != 0) {
1015 dsl_pool_rele(dp, FTAG);
1016 return (err);
1017 }
1018
1019 if (fromsnap != 0) {
ea04106b
AX
1020 zfs_bookmark_phys_t zb;
1021 boolean_t is_clone;
1022
a08ee875
LG
1023 err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
1024 if (err != 0) {
1025 dsl_dataset_rele(ds, FTAG);
1026 dsl_pool_rele(dp, FTAG);
1027 return (err);
1028 }
ea04106b
AX
1029 if (!dsl_dataset_is_before(ds, fromds, 0))
1030 err = SET_ERROR(EXDEV);
e10b0808
AX
1031 zb.zbm_creation_time =
1032 dsl_dataset_phys(fromds)->ds_creation_time;
1033 zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
1034 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
ea04106b
AX
1035 is_clone = (fromds->ds_dir != ds->ds_dir);
1036 dsl_dataset_rele(fromds, FTAG);
e10b0808 1037 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
cae5b340 1038 embedok, large_block_ok, compressok, outfd, 0, 0, vp, off);
ea04106b 1039 } else {
e10b0808 1040 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
cae5b340 1041 embedok, large_block_ok, compressok, outfd, 0, 0, vp, off);
a08ee875 1042 }
ea04106b
AX
1043 dsl_dataset_rele(ds, FTAG);
1044 return (err);
a08ee875
LG
1045}
1046
1047int
cae5b340
AX
1048dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
1049 boolean_t large_block_ok, boolean_t compressok, int outfd,
1050 uint64_t resumeobj, uint64_t resumeoff,
1051 vnode_t *vp, offset_t *off)
a08ee875
LG
1052{
1053 dsl_pool_t *dp;
1054 dsl_dataset_t *ds;
a08ee875 1055 int err;
ea04106b 1056 boolean_t owned = B_FALSE;
a08ee875 1057
ea04106b 1058 if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
a08ee875
LG
1059 return (SET_ERROR(EINVAL));
1060
1061 err = dsl_pool_hold(tosnap, FTAG, &dp);
1062 if (err != 0)
1063 return (err);
1064
ea04106b
AX
1065 if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
1066 /*
1067 * We are sending a filesystem or volume. Ensure
1068 * that it doesn't change by owning the dataset.
1069 */
1070 err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
1071 owned = B_TRUE;
1072 } else {
1073 err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
1074 }
a08ee875
LG
1075 if (err != 0) {
1076 dsl_pool_rele(dp, FTAG);
1077 return (err);
1078 }
1079
1080 if (fromsnap != NULL) {
ea04106b
AX
1081 zfs_bookmark_phys_t zb;
1082 boolean_t is_clone = B_FALSE;
1083 int fsnamelen = strchr(tosnap, '@') - tosnap;
1084
1085 /*
1086 * If the fromsnap is in a different filesystem, then
1087 * mark the send stream as a clone.
1088 */
1089 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
1090 (fromsnap[fsnamelen] != '@' &&
1091 fromsnap[fsnamelen] != '#')) {
1092 is_clone = B_TRUE;
1093 }
1094
1095 if (strchr(fromsnap, '@')) {
1096 dsl_dataset_t *fromds;
1097 err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
1098 if (err == 0) {
1099 if (!dsl_dataset_is_before(ds, fromds, 0))
1100 err = SET_ERROR(EXDEV);
1101 zb.zbm_creation_time =
e10b0808 1102 dsl_dataset_phys(fromds)->ds_creation_time;
ea04106b 1103 zb.zbm_creation_txg =
e10b0808
AX
1104 dsl_dataset_phys(fromds)->ds_creation_txg;
1105 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
ea04106b
AX
1106 is_clone = (ds->ds_dir != fromds->ds_dir);
1107 dsl_dataset_rele(fromds, FTAG);
1108 }
1109 } else {
1110 err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
1111 }
a08ee875
LG
1112 if (err != 0) {
1113 dsl_dataset_rele(ds, FTAG);
1114 dsl_pool_rele(dp, FTAG);
1115 return (err);
330d06f9 1116 }
e10b0808 1117 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
cae5b340
AX
1118 embedok, large_block_ok, compressok,
1119 outfd, resumeobj, resumeoff, vp, off);
ea04106b 1120 } else {
e10b0808 1121 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
cae5b340
AX
1122 embedok, large_block_ok, compressok,
1123 outfd, resumeobj, resumeoff, vp, off);
330d06f9 1124 }
ea04106b
AX
1125 if (owned)
1126 dsl_dataset_disown(ds, FTAG);
1127 else
1128 dsl_dataset_rele(ds, FTAG);
1129 return (err);
a08ee875
LG
1130}
1131
e10b0808 1132static int
cae5b340
AX
1133dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed,
1134 uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep)
e10b0808
AX
1135{
1136 int err;
cae5b340 1137 uint64_t size;
e10b0808
AX
1138 /*
1139 * Assume that space (both on-disk and in-stream) is dominated by
1140 * data. We will adjust for indirect blocks and the copies property,
1141 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1142 */
1143
cae5b340
AX
1144 uint64_t recordsize;
1145 uint64_t record_count;
1146 objset_t *os;
1147 VERIFY0(dmu_objset_from_ds(ds, &os));
1148
1149 /* Assume all (uncompressed) blocks are recordsize. */
1150 if (os->os_phys->os_type == DMU_OST_ZVOL) {
1151 err = dsl_prop_get_int_ds(ds,
1152 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &recordsize);
1153 } else {
1154 err = dsl_prop_get_int_ds(ds,
1155 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), &recordsize);
1156 }
1157 if (err != 0)
1158 return (err);
1159 record_count = uncompressed / recordsize;
1160
1161 /*
1162 * If we're estimating a send size for a compressed stream, use the
1163 * compressed data size to estimate the stream size. Otherwise, use the
1164 * uncompressed data size.
1165 */
1166 size = stream_compressed ? compressed : uncompressed;
1167
e10b0808
AX
1168 /*
1169 * Subtract out approximate space used by indirect blocks.
1170 * Assume most space is used by data blocks (non-indirect, non-dnode).
cae5b340 1171 * Assume no ditto blocks or internal fragmentation.
e10b0808
AX
1172 *
1173 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
cae5b340 1174 * block.
e10b0808 1175 */
cae5b340 1176 size -= record_count * sizeof (blkptr_t);
e10b0808
AX
1177
1178 /* Add in the space for the record associated with each block. */
cae5b340 1179 size += record_count * sizeof (dmu_replay_record_t);
e10b0808
AX
1180
1181 *sizep = size;
1182
1183 return (0);
1184}
1185
a08ee875 1186int
cae5b340
AX
1187dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds,
1188 boolean_t stream_compressed, uint64_t *sizep)
a08ee875
LG
1189{
1190 int err;
cae5b340 1191 uint64_t uncomp, comp;
a08ee875 1192
e10b0808 1193 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
a08ee875
LG
1194
1195 /* tosnap must be a snapshot */
e10b0808
AX
1196 if (!ds->ds_is_snapshot)
1197 return (SET_ERROR(EINVAL));
1198
1199 /* fromsnap, if provided, must be a snapshot */
1200 if (fromds != NULL && !fromds->ds_is_snapshot)
a08ee875
LG
1201 return (SET_ERROR(EINVAL));
1202
1203 /*
1204 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1205 * or the origin's fs.
1206 */
ea04106b 1207 if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
a08ee875 1208 return (SET_ERROR(EXDEV));
330d06f9 1209
cae5b340 1210 /* Get compressed and uncompressed size estimates of changed data. */
330d06f9 1211 if (fromds == NULL) {
cae5b340
AX
1212 uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1213 comp = dsl_dataset_phys(ds)->ds_compressed_bytes;
330d06f9 1214 } else {
cae5b340 1215 uint64_t used;
330d06f9 1216 err = dsl_dataset_space_written(fromds, ds,
cae5b340 1217 &used, &comp, &uncomp);
a08ee875 1218 if (err != 0)
330d06f9
MA
1219 return (err);
1220 }
1221
cae5b340
AX
1222 err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp,
1223 stream_compressed, sizep);
1224 /*
1225 * Add the size of the BEGIN and END records to the estimate.
1226 */
1227 *sizep += 2 * sizeof (dmu_replay_record_t);
e10b0808
AX
1228 return (err);
1229}
1230
cae5b340
AX
1231struct calculate_send_arg {
1232 uint64_t uncompressed;
1233 uint64_t compressed;
1234};
1235
e10b0808
AX
1236/*
1237 * Simple callback used to traverse the blocks of a snapshot and sum their
cae5b340 1238 * uncompressed and compressed sizes.
e10b0808
AX
1239 */
1240/* ARGSUSED */
1241static int
1242dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1243 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1244{
cae5b340 1245 struct calculate_send_arg *space = arg;
e10b0808 1246 if (bp != NULL && !BP_IS_HOLE(bp)) {
cae5b340
AX
1247 space->uncompressed += BP_GET_UCSIZE(bp);
1248 space->compressed += BP_GET_PSIZE(bp);
e10b0808
AX
1249 }
1250 return (0);
1251}
1252
1253/*
1254 * Given a desination snapshot and a TXG, calculate the approximate size of a
1255 * send stream sent from that TXG. from_txg may be zero, indicating that the
1256 * whole snapshot will be sent.
1257 */
1258int
1259dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
cae5b340 1260 boolean_t stream_compressed, uint64_t *sizep)
e10b0808
AX
1261{
1262 int err;
cae5b340 1263 struct calculate_send_arg size = { 0 };
e10b0808
AX
1264
1265 ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
1266
1267 /* tosnap must be a snapshot */
1268 if (!dsl_dataset_is_snapshot(ds))
1269 return (SET_ERROR(EINVAL));
330d06f9 1270
e10b0808
AX
1271 /* verify that from_txg is before the provided snapshot was taken */
1272 if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1273 return (SET_ERROR(EXDEV));
1274 }
330d06f9 1275 /*
e10b0808
AX
1276 * traverse the blocks of the snapshot with birth times after
1277 * from_txg, summing their uncompressed size
330d06f9 1278 */
e10b0808
AX
1279 err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1280 dmu_calculate_send_traversal, &size);
cae5b340 1281
e10b0808 1282 if (err)
330d06f9 1283 return (err);
330d06f9 1284
cae5b340
AX
1285 err = dmu_adjust_send_estimate_for_indirects(ds, size.uncompressed,
1286 size.compressed, stream_compressed, sizep);
e10b0808 1287 return (err);
330d06f9
MA
1288}
1289
a08ee875
LG
1290typedef struct dmu_recv_begin_arg {
1291 const char *drba_origin;
1292 dmu_recv_cookie_t *drba_cookie;
1293 cred_t *drba_cred;
1294 uint64_t drba_snapobj;
1295} dmu_recv_begin_arg_t;
34dc7c2f 1296
34dc7c2f 1297static int
a08ee875
LG
1298recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
1299 uint64_t fromguid)
34dc7c2f 1300{
34dc7c2f 1301 uint64_t val;
a08ee875
LG
1302 int error;
1303 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1304
1305 /* temporary clone name must not exist */
1306 error = zap_lookup(dp->dp_meta_objset,
e10b0808 1307 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
a08ee875
LG
1308 8, 1, &val);
1309 if (error != ENOENT)
1310 return (error == 0 ? EBUSY : error);
34dc7c2f 1311
a08ee875
LG
1312 /* new snapshot name must not exist */
1313 error = zap_lookup(dp->dp_meta_objset,
e10b0808
AX
1314 dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1315 drba->drba_cookie->drc_tosnap, 8, 1, &val);
a08ee875
LG
1316 if (error != ENOENT)
1317 return (error == 0 ? EEXIST : error);
1318
e10b0808
AX
1319 /*
1320 * Check snapshot limit before receiving. We'll recheck again at the
1321 * end, but might as well abort before receiving if we're already over
1322 * the limit.
1323 *
1324 * Note that we do not check the file system limit with
1325 * dsl_dir_fscount_check because the temporary %clones don't count
1326 * against that limit.
1327 */
1328 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1329 NULL, drba->drba_cred);
1330 if (error != 0)
1331 return (error);
1332
a08ee875
LG
1333 if (fromguid != 0) {
1334 dsl_dataset_t *snap;
e10b0808 1335 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
a08ee875
LG
1336
1337 /* Find snapshot in this dir that matches fromguid. */
1338 while (obj != 0) {
1339 error = dsl_dataset_hold_obj(dp, obj, FTAG,
1340 &snap);
1341 if (error != 0)
1342 return (SET_ERROR(ENODEV));
1343 if (snap->ds_dir != ds->ds_dir) {
1344 dsl_dataset_rele(snap, FTAG);
1345 return (SET_ERROR(ENODEV));
1346 }
e10b0808 1347 if (dsl_dataset_phys(snap)->ds_guid == fromguid)
a08ee875 1348 break;
e10b0808 1349 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
a08ee875
LG
1350 dsl_dataset_rele(snap, FTAG);
1351 }
1352 if (obj == 0)
1353 return (SET_ERROR(ENODEV));
34dc7c2f 1354
a08ee875
LG
1355 if (drba->drba_cookie->drc_force) {
1356 drba->drba_snapobj = obj;
1357 } else {
1358 /*
1359 * If we are not forcing, there must be no
1360 * changes since fromsnap.
1361 */
1362 if (dsl_dataset_modified_since_snap(ds, snap)) {
1363 dsl_dataset_rele(snap, FTAG);
1364 return (SET_ERROR(ETXTBSY));
1365 }
1366 drba->drba_snapobj = ds->ds_prev->ds_object;
1367 }
34dc7c2f 1368
a08ee875
LG
1369 dsl_dataset_rele(snap, FTAG);
1370 } else {
e10b0808
AX
1371 /* if full, then must be forced */
1372 if (!drba->drba_cookie->drc_force)
1373 return (SET_ERROR(EEXIST));
1374 /* start from $ORIGIN@$ORIGIN, if supported */
1375 drba->drba_snapobj = dp->dp_origin_snap != NULL ?
1376 dp->dp_origin_snap->ds_object : 0;
34dc7c2f
BB
1377 }
1378
1379 return (0);
a08ee875 1380
34dc7c2f
BB
1381}
1382
a08ee875
LG
1383static int
1384dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1385{
a08ee875
LG
1386 dmu_recv_begin_arg_t *drba = arg;
1387 dsl_pool_t *dp = dmu_tx_pool(tx);
1388 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1389 uint64_t fromguid = drrb->drr_fromguid;
1390 int flags = drrb->drr_flags;
1391 int error;
ea04106b 1392 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
a08ee875
LG
1393 dsl_dataset_t *ds;
1394 const char *tofs = drba->drba_cookie->drc_tofs;
34dc7c2f 1395
a08ee875
LG
1396 /* already checked */
1397 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
cae5b340 1398 ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
34dc7c2f 1399
a08ee875
LG
1400 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1401 DMU_COMPOUNDSTREAM ||
1402 drrb->drr_type >= DMU_OST_NUMTYPES ||
1403 ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1404 return (SET_ERROR(EINVAL));
34dc7c2f 1405
a08ee875 1406 /* Verify pool version supports SA if SA_SPILL feature set */
ea04106b
AX
1407 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1408 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1409 return (SET_ERROR(ENOTSUP));
1410
cae5b340
AX
1411 if (drba->drba_cookie->drc_resumable &&
1412 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
1413 return (SET_ERROR(ENOTSUP));
1414
ea04106b
AX
1415 /*
1416 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
cae5b340 1417 * record to a plain WRITE record, so the pool must have the
ea04106b
AX
1418 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1419 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1420 */
1421 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1422 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1423 return (SET_ERROR(ENOTSUP));
cae5b340 1424 if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
ea04106b 1425 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
a08ee875 1426 return (SET_ERROR(ENOTSUP));
34dc7c2f 1427
e10b0808
AX
1428 /*
1429 * The receiving code doesn't know how to translate large blocks
1430 * to smaller ones, so the pool must have the LARGE_BLOCKS
cae5b340
AX
1431 * feature enabled if the stream has LARGE_BLOCKS. Same with
1432 * large dnodes.
e10b0808
AX
1433 */
1434 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1435 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1436 return (SET_ERROR(ENOTSUP));
cae5b340
AX
1437 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
1438 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
1439 return (SET_ERROR(ENOTSUP));
e10b0808 1440
a08ee875
LG
1441 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1442 if (error == 0) {
1443 /* target fs already exists; recv into temp clone */
34dc7c2f 1444
a08ee875 1445 /* Can't recv a clone into an existing fs */
cae5b340 1446 if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
a08ee875
LG
1447 dsl_dataset_rele(ds, FTAG);
1448 return (SET_ERROR(EINVAL));
1449 }
572e2857 1450
a08ee875
LG
1451 error = recv_begin_check_existing_impl(drba, ds, fromguid);
1452 dsl_dataset_rele(ds, FTAG);
1453 } else if (error == ENOENT) {
1454 /* target fs does not exist; must be a full backup or clone */
cae5b340 1455 char buf[ZFS_MAX_DATASET_NAME_LEN];
34dc7c2f 1456
428870ff 1457 /*
a08ee875
LG
1458 * If it's a non-clone incremental, we are missing the
1459 * target fs, so fail the recv.
428870ff 1460 */
cae5b340
AX
1461 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1462 drba->drba_origin))
a08ee875
LG
1463 return (SET_ERROR(ENOENT));
1464
cae5b340
AX
1465 /*
1466 * If we're receiving a full send as a clone, and it doesn't
1467 * contain all the necessary free records and freeobject
1468 * records, reject it.
1469 */
1470 if (fromguid == 0 && drba->drba_origin &&
1471 !(flags & DRR_FLAG_FREERECORDS))
1472 return (SET_ERROR(EINVAL));
1473
a08ee875 1474 /* Open the parent of tofs */
cae5b340 1475 ASSERT3U(strlen(tofs), <, sizeof (buf));
a08ee875
LG
1476 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
1477 error = dsl_dataset_hold(dp, buf, FTAG, &ds);
1478 if (error != 0)
1479 return (error);
1480
e10b0808
AX
1481 /*
1482 * Check filesystem and snapshot limits before receiving. We'll
1483 * recheck snapshot limits again at the end (we create the
1484 * filesystems and increment those counts during begin_sync).
1485 */
1486 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1487 ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1488 if (error != 0) {
1489 dsl_dataset_rele(ds, FTAG);
1490 return (error);
1491 }
1492
1493 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1494 ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1495 if (error != 0) {
1496 dsl_dataset_rele(ds, FTAG);
1497 return (error);
1498 }
1499
a08ee875
LG
1500 if (drba->drba_origin != NULL) {
1501 dsl_dataset_t *origin;
1502 error = dsl_dataset_hold(dp, drba->drba_origin,
1503 FTAG, &origin);
1504 if (error != 0) {
1505 dsl_dataset_rele(ds, FTAG);
1506 return (error);
1507 }
e10b0808 1508 if (!origin->ds_is_snapshot) {
a08ee875
LG
1509 dsl_dataset_rele(origin, FTAG);
1510 dsl_dataset_rele(ds, FTAG);
1511 return (SET_ERROR(EINVAL));
1512 }
cae5b340
AX
1513 if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
1514 fromguid != 0) {
a08ee875
LG
1515 dsl_dataset_rele(origin, FTAG);
1516 dsl_dataset_rele(ds, FTAG);
1517 return (SET_ERROR(ENODEV));
428870ff 1518 }
a08ee875 1519 dsl_dataset_rele(origin, FTAG);
428870ff 1520 }
a08ee875
LG
1521 dsl_dataset_rele(ds, FTAG);
1522 error = 0;
428870ff 1523 }
a08ee875 1524 return (error);
34dc7c2f
BB
1525}
1526
34dc7c2f 1527static void
a08ee875 1528dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 1529{
a08ee875
LG
1530 dmu_recv_begin_arg_t *drba = arg;
1531 dsl_pool_t *dp = dmu_tx_pool(tx);
cae5b340 1532 objset_t *mos = dp->dp_meta_objset;
a08ee875
LG
1533 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1534 const char *tofs = drba->drba_cookie->drc_tofs;
1535 dsl_dataset_t *ds, *newds;
34dc7c2f 1536 uint64_t dsobj;
a08ee875 1537 int error;
cae5b340 1538 uint64_t crflags = 0;
a08ee875 1539
cae5b340
AX
1540 if (drrb->drr_flags & DRR_FLAG_CI_DATA)
1541 crflags |= DS_FLAG_CI_DATASET;
a08ee875
LG
1542
1543 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1544 if (error == 0) {
1545 /* create temporary clone */
1546 dsl_dataset_t *snap = NULL;
1547 if (drba->drba_snapobj != 0) {
1548 VERIFY0(dsl_dataset_hold_obj(dp,
1549 drba->drba_snapobj, FTAG, &snap));
1550 }
1551 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
1552 snap, crflags, drba->drba_cred, tx);
cae5b340
AX
1553 if (drba->drba_snapobj != 0)
1554 dsl_dataset_rele(snap, FTAG);
a08ee875
LG
1555 dsl_dataset_rele(ds, FTAG);
1556 } else {
1557 dsl_dir_t *dd;
1558 const char *tail;
1559 dsl_dataset_t *origin = NULL;
1560
1561 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
1562
1563 if (drba->drba_origin != NULL) {
1564 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
1565 FTAG, &origin));
1566 }
1567
1568 /* Create new dataset. */
1569 dsobj = dsl_dataset_create_sync(dd,
1570 strrchr(tofs, '/') + 1,
1571 origin, crflags, drba->drba_cred, tx);
1572 if (origin != NULL)
1573 dsl_dataset_rele(origin, FTAG);
1574 dsl_dir_rele(dd, FTAG);
1575 drba->drba_cookie->drc_newfs = B_TRUE;
1576 }
1577 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
34dc7c2f 1578
cae5b340
AX
1579 if (drba->drba_cookie->drc_resumable) {
1580 uint64_t one = 1;
1581 uint64_t zero = 0;
1582
1583 dsl_dataset_zapify(newds, tx);
1584 if (drrb->drr_fromguid != 0) {
1585 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
1586 8, 1, &drrb->drr_fromguid, tx));
1587 }
1588 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
1589 8, 1, &drrb->drr_toguid, tx));
1590 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
1591 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
1592 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
1593 8, 1, &one, tx));
1594 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
1595 8, 1, &zero, tx));
1596 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
1597 8, 1, &zero, tx));
1598 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1599 DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
1600 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
1601 8, 1, &one, tx));
1602 }
1603 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1604 DMU_BACKUP_FEATURE_EMBED_DATA) {
1605 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
1606 8, 1, &one, tx));
1607 }
1608 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1609 DMU_BACKUP_FEATURE_COMPRESSED) {
1610 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
1611 8, 1, &one, tx));
1612 }
e10b0808
AX
1613 }
1614
a08ee875 1615 dmu_buf_will_dirty(newds->ds_dbuf, tx);
e10b0808 1616 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
34dc7c2f 1617
428870ff
BB
1618 /*
1619 * If we actually created a non-clone, we need to create the
1620 * objset in our new dataset.
1621 */
cae5b340 1622 rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
a08ee875 1623 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
428870ff 1624 (void) dmu_objset_create_impl(dp->dp_spa,
a08ee875 1625 newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
428870ff 1626 }
cae5b340 1627 rrw_exit(&newds->ds_bp_rwlock, FTAG);
34dc7c2f 1628
a08ee875 1629 drba->drba_cookie->drc_ds = newds;
34dc7c2f 1630
a08ee875 1631 spa_history_log_internal_ds(newds, "receive", tx, "");
34dc7c2f
BB
1632}
1633
cae5b340
AX
1634static int
1635dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
34dc7c2f 1636{
cae5b340
AX
1637 dmu_recv_begin_arg_t *drba = arg;
1638 dsl_pool_t *dp = dmu_tx_pool(tx);
1639 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1640 int error;
1641 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1642 dsl_dataset_t *ds;
1643 const char *tofs = drba->drba_cookie->drc_tofs;
1644 uint64_t val;
34dc7c2f 1645
cae5b340
AX
1646 /* 6 extra bytes for /%recv */
1647 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
34dc7c2f 1648
cae5b340
AX
1649 /* already checked */
1650 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1651 ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
34dc7c2f 1652
cae5b340
AX
1653 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1654 DMU_COMPOUNDSTREAM ||
1655 drrb->drr_type >= DMU_OST_NUMTYPES)
1656 return (SET_ERROR(EINVAL));
428870ff 1657
cae5b340
AX
1658 /* Verify pool version supports SA if SA_SPILL feature set */
1659 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1660 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1661 return (SET_ERROR(ENOTSUP));
34dc7c2f 1662
cae5b340
AX
1663 /*
1664 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1665 * record to a plain WRITE record, so the pool must have the
1666 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1667 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1668 */
1669 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1670 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1671 return (SET_ERROR(ENOTSUP));
1672 if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
1673 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1674 return (SET_ERROR(ENOTSUP));
a08ee875 1675
cae5b340
AX
1676 /*
1677 * The receiving code doesn't know how to translate large blocks
1678 * to smaller ones, so the pool must have the LARGE_BLOCKS
1679 * feature enabled if the stream has LARGE_BLOCKS. Same with
1680 * large dnodes.
1681 */
1682 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1683 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1684 return (SET_ERROR(ENOTSUP));
1685 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_DNODE) &&
1686 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_DNODE))
1687 return (SET_ERROR(ENOTSUP));
34dc7c2f 1688
cae5b340
AX
1689 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1690 tofs, recv_clone_name);
34dc7c2f 1691
cae5b340
AX
1692 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1693 /* %recv does not exist; continue in tofs */
1694 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1695 if (error != 0)
1696 return (error);
1697 }
428870ff 1698
cae5b340
AX
1699 /* check that ds is marked inconsistent */
1700 if (!DS_IS_INCONSISTENT(ds)) {
1701 dsl_dataset_rele(ds, FTAG);
1702 return (SET_ERROR(EINVAL));
1703 }
428870ff 1704
cae5b340
AX
1705 /* check that there is resuming data, and that the toguid matches */
1706 if (!dsl_dataset_is_zapified(ds)) {
1707 dsl_dataset_rele(ds, FTAG);
1708 return (SET_ERROR(EINVAL));
1709 }
1710 error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1711 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1712 if (error != 0 || drrb->drr_toguid != val) {
1713 dsl_dataset_rele(ds, FTAG);
1714 return (SET_ERROR(EINVAL));
1715 }
1716
1717 /*
1718 * Check if the receive is still running. If so, it will be owned.
1719 * Note that nothing else can own the dataset (e.g. after the receive
1720 * fails) because it will be marked inconsistent.
1721 */
1722 if (dsl_dataset_has_owner(ds)) {
1723 dsl_dataset_rele(ds, FTAG);
1724 return (SET_ERROR(EBUSY));
1725 }
1726
1727 /* There should not be any snapshots of this fs yet. */
1728 if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1729 dsl_dataset_rele(ds, FTAG);
1730 return (SET_ERROR(EINVAL));
1731 }
1732
1733 /*
1734 * Note: resume point will be checked when we process the first WRITE
1735 * record.
1736 */
1737
1738 /* check that the origin matches */
1739 val = 0;
1740 (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1741 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1742 if (drrb->drr_fromguid != val) {
1743 dsl_dataset_rele(ds, FTAG);
1744 return (SET_ERROR(EINVAL));
1745 }
1746
1747 dsl_dataset_rele(ds, FTAG);
428870ff
BB
1748 return (0);
1749}
1750
cae5b340
AX
1751static void
1752dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1753{
1754 dmu_recv_begin_arg_t *drba = arg;
1755 dsl_pool_t *dp = dmu_tx_pool(tx);
1756 const char *tofs = drba->drba_cookie->drc_tofs;
1757 dsl_dataset_t *ds;
1758 uint64_t dsobj;
1759 /* 6 extra bytes for /%recv */
1760 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1761
1762 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1763 tofs, recv_clone_name);
1764
1765 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1766 /* %recv does not exist; continue in tofs */
1767 VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
1768 drba->drba_cookie->drc_newfs = B_TRUE;
1769 }
1770
1771 /* clear the inconsistent flag so that we can own it */
1772 ASSERT(DS_IS_INCONSISTENT(ds));
1773 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1774 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
1775 dsobj = ds->ds_object;
1776 dsl_dataset_rele(ds, FTAG);
1777
1778 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
1779
1780 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1781 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1782
1783 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1784 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
1785 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1786
1787 drba->drba_cookie->drc_ds = ds;
1788
1789 spa_history_log_internal_ds(ds, "resume receive", tx, "");
1790}
1791
1792/*
1793 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1794 * succeeds; otherwise we will leak the holds on the datasets.
1795 */
1796int
1797dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
1798 boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1799{
1800 dmu_recv_begin_arg_t drba = { 0 };
1801
1802 bzero(drc, sizeof (dmu_recv_cookie_t));
1803 drc->drc_drr_begin = drr_begin;
1804 drc->drc_drrb = &drr_begin->drr_u.drr_begin;
1805 drc->drc_tosnap = tosnap;
1806 drc->drc_tofs = tofs;
1807 drc->drc_force = force;
1808 drc->drc_resumable = resumable;
1809 drc->drc_cred = CRED();
1810
1811 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1812 drc->drc_byteswap = B_TRUE;
1813 (void) fletcher_4_incremental_byteswap(drr_begin,
1814 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1815 byteswap_record(drr_begin);
1816 } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1817 (void) fletcher_4_incremental_native(drr_begin,
1818 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1819 } else {
1820 return (SET_ERROR(EINVAL));
1821 }
1822
1823 drba.drba_origin = origin;
1824 drba.drba_cookie = drc;
1825 drba.drba_cred = CRED();
1826
1827 if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1828 DMU_BACKUP_FEATURE_RESUMING) {
1829 return (dsl_sync_task(tofs,
1830 dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
1831 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1832 } else {
1833 return (dsl_sync_task(tofs,
1834 dmu_recv_begin_check, dmu_recv_begin_sync,
1835 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1836 }
1837}
1838
1839struct receive_record_arg {
1840 dmu_replay_record_t header;
1841 void *payload; /* Pointer to a buffer containing the payload */
1842 /*
1843 * If the record is a write, pointer to the arc_buf_t containing the
1844 * payload.
1845 */
1846 arc_buf_t *write_buf;
1847 int payload_size;
1848 uint64_t bytes_read; /* bytes read from stream when record created */
1849 boolean_t eos_marker; /* Marks the end of the stream */
1850 bqueue_node_t node;
1851};
1852
1853struct receive_writer_arg {
1854 objset_t *os;
1855 boolean_t byteswap;
1856 bqueue_t q;
1857
1858 /*
1859 * These three args are used to signal to the main thread that we're
1860 * done.
1861 */
1862 kmutex_t mutex;
1863 kcondvar_t cv;
1864 boolean_t done;
1865
1866 int err;
1867 /* A map from guid to dataset to help handle dedup'd streams. */
1868 avl_tree_t *guid_to_ds_map;
1869 boolean_t resumable;
1870 uint64_t last_object, last_offset;
1871 uint64_t bytes_read; /* bytes read when current record created */
1872};
1873
1874struct objlist {
1875 list_t list; /* List of struct receive_objnode. */
1876 /*
1877 * Last object looked up. Used to assert that objects are being looked
1878 * up in ascending order.
1879 */
1880 uint64_t last_lookup;
1881};
1882
1883struct receive_objnode {
1884 list_node_t node;
1885 uint64_t object;
1886};
1887
1888struct receive_arg {
1889 objset_t *os;
1890 vnode_t *vp; /* The vnode to read the stream from */
1891 uint64_t voff; /* The current offset in the stream */
1892 uint64_t bytes_read;
1893 /*
1894 * A record that has had its payload read in, but hasn't yet been handed
1895 * off to the worker thread.
1896 */
1897 struct receive_record_arg *rrd;
1898 /* A record that has had its header read in, but not its payload. */
1899 struct receive_record_arg *next_rrd;
1900 zio_cksum_t cksum;
1901 zio_cksum_t prev_cksum;
1902 int err;
1903 boolean_t byteswap;
1904 /* Sorted list of objects not to issue prefetches for. */
1905 struct objlist ignore_objlist;
1906};
1907
1908typedef struct guid_map_entry {
1909 uint64_t guid;
1910 dsl_dataset_t *gme_ds;
1911 avl_node_t avlnode;
1912} guid_map_entry_t;
1913
1914static int
1915guid_compare(const void *arg1, const void *arg2)
1916{
1917 const guid_map_entry_t *gmep1 = (const guid_map_entry_t *)arg1;
1918 const guid_map_entry_t *gmep2 = (const guid_map_entry_t *)arg2;
1919
1920 return (AVL_CMP(gmep1->guid, gmep2->guid));
1921}
1922
572e2857
BB
1923static void
1924free_guid_map_onexit(void *arg)
1925{
1926 avl_tree_t *ca = arg;
1927 void *cookie = NULL;
1928 guid_map_entry_t *gmep;
1929
1930 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
a08ee875
LG
1931 dsl_dataset_long_rele(gmep->gme_ds, gmep);
1932 dsl_dataset_rele(gmep->gme_ds, gmep);
572e2857
BB
1933 kmem_free(gmep, sizeof (guid_map_entry_t));
1934 }
1935 avl_destroy(ca);
1936 kmem_free(ca, sizeof (avl_tree_t));
1937}
1938
cae5b340
AX
1939static int
1940receive_read(struct receive_arg *ra, int len, void *buf)
34dc7c2f 1941{
34dc7c2f
BB
1942 int done = 0;
1943
cae5b340
AX
1944 /*
1945 * The code doesn't rely on this (lengths being multiples of 8). See
1946 * comment in dump_bytes.
1947 */
c06d4368 1948 ASSERT0(len % 8);
34dc7c2f
BB
1949
1950 while (done < len) {
1951 ssize_t resid;
1952
1953 ra->err = vn_rdwr(UIO_READ, ra->vp,
cae5b340 1954 (char *)buf + done, len - done,
34dc7c2f
BB
1955 ra->voff, UIO_SYSSPACE, FAPPEND,
1956 RLIM64_INFINITY, CRED(), &resid);
1957
cae5b340
AX
1958 if (resid == len - done) {
1959 /*
1960 * Note: ECKSUM indicates that the receive
1961 * was interrupted and can potentially be resumed.
1962 */
1963 ra->err = SET_ERROR(ECKSUM);
1964 }
34dc7c2f
BB
1965 ra->voff += len - done - resid;
1966 done = len - resid;
a08ee875 1967 if (ra->err != 0)
cae5b340 1968 return (ra->err);
34dc7c2f
BB
1969 }
1970
cae5b340
AX
1971 ra->bytes_read += len;
1972
34dc7c2f 1973 ASSERT3U(done, ==, len);
cae5b340 1974 return (0);
34dc7c2f
BB
1975}
1976
60948de1 1977noinline static void
cae5b340 1978byteswap_record(dmu_replay_record_t *drr)
34dc7c2f
BB
1979{
1980#define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1981#define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1982 drr->drr_type = BSWAP_32(drr->drr_type);
1983 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
cae5b340 1984
34dc7c2f
BB
1985 switch (drr->drr_type) {
1986 case DRR_BEGIN:
1987 DO64(drr_begin.drr_magic);
428870ff 1988 DO64(drr_begin.drr_versioninfo);
34dc7c2f
BB
1989 DO64(drr_begin.drr_creation_time);
1990 DO32(drr_begin.drr_type);
1991 DO32(drr_begin.drr_flags);
1992 DO64(drr_begin.drr_toguid);
1993 DO64(drr_begin.drr_fromguid);
1994 break;
1995 case DRR_OBJECT:
1996 DO64(drr_object.drr_object);
34dc7c2f
BB
1997 DO32(drr_object.drr_type);
1998 DO32(drr_object.drr_bonustype);
1999 DO32(drr_object.drr_blksz);
2000 DO32(drr_object.drr_bonuslen);
428870ff 2001 DO64(drr_object.drr_toguid);
34dc7c2f
BB
2002 break;
2003 case DRR_FREEOBJECTS:
2004 DO64(drr_freeobjects.drr_firstobj);
2005 DO64(drr_freeobjects.drr_numobjs);
428870ff 2006 DO64(drr_freeobjects.drr_toguid);
34dc7c2f
BB
2007 break;
2008 case DRR_WRITE:
2009 DO64(drr_write.drr_object);
2010 DO32(drr_write.drr_type);
2011 DO64(drr_write.drr_offset);
cae5b340 2012 DO64(drr_write.drr_logical_size);
428870ff 2013 DO64(drr_write.drr_toguid);
cae5b340 2014 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
428870ff 2015 DO64(drr_write.drr_key.ddk_prop);
cae5b340 2016 DO64(drr_write.drr_compressed_size);
428870ff
BB
2017 break;
2018 case DRR_WRITE_BYREF:
2019 DO64(drr_write_byref.drr_object);
2020 DO64(drr_write_byref.drr_offset);
2021 DO64(drr_write_byref.drr_length);
2022 DO64(drr_write_byref.drr_toguid);
2023 DO64(drr_write_byref.drr_refguid);
2024 DO64(drr_write_byref.drr_refobject);
2025 DO64(drr_write_byref.drr_refoffset);
cae5b340
AX
2026 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
2027 drr_key.ddk_cksum);
428870ff 2028 DO64(drr_write_byref.drr_key.ddk_prop);
34dc7c2f 2029 break;
ea04106b
AX
2030 case DRR_WRITE_EMBEDDED:
2031 DO64(drr_write_embedded.drr_object);
2032 DO64(drr_write_embedded.drr_offset);
2033 DO64(drr_write_embedded.drr_length);
2034 DO64(drr_write_embedded.drr_toguid);
2035 DO32(drr_write_embedded.drr_lsize);
2036 DO32(drr_write_embedded.drr_psize);
2037 break;
34dc7c2f
BB
2038 case DRR_FREE:
2039 DO64(drr_free.drr_object);
2040 DO64(drr_free.drr_offset);
2041 DO64(drr_free.drr_length);
428870ff
BB
2042 DO64(drr_free.drr_toguid);
2043 break;
2044 case DRR_SPILL:
2045 DO64(drr_spill.drr_object);
2046 DO64(drr_spill.drr_length);
2047 DO64(drr_spill.drr_toguid);
34dc7c2f
BB
2048 break;
2049 case DRR_END:
428870ff 2050 DO64(drr_end.drr_toguid);
cae5b340 2051 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
34dc7c2f 2052 break;
e75c13c3
BB
2053 default:
2054 break;
34dc7c2f 2055 }
cae5b340
AX
2056
2057 if (drr->drr_type != DRR_BEGIN) {
2058 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
2059 }
2060
34dc7c2f
BB
2061#undef DO64
2062#undef DO32
2063}
2064
ea04106b
AX
2065static inline uint8_t
2066deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
2067{
2068 if (bonus_type == DMU_OT_SA) {
2069 return (1);
2070 } else {
2071 return (1 +
cae5b340
AX
2072 ((DN_OLD_MAX_BONUSLEN -
2073 MIN(DN_OLD_MAX_BONUSLEN, bonus_size)) >> SPA_BLKPTRSHIFT));
ea04106b
AX
2074 }
2075}
2076
cae5b340
AX
2077static void
2078save_resume_state(struct receive_writer_arg *rwa,
2079 uint64_t object, uint64_t offset, dmu_tx_t *tx)
2080{
2081 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
2082
2083 if (!rwa->resumable)
2084 return;
2085
2086 /*
2087 * We use ds_resume_bytes[] != 0 to indicate that we need to
2088 * update this on disk, so it must not be 0.
2089 */
2090 ASSERT(rwa->bytes_read != 0);
2091
2092 /*
2093 * We only resume from write records, which have a valid
2094 * (non-meta-dnode) object number.
2095 */
2096 ASSERT(object != 0);
2097
2098 /*
2099 * For resuming to work correctly, we must receive records in order,
2100 * sorted by object,offset. This is checked by the callers, but
2101 * assert it here for good measure.
2102 */
2103 ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
2104 ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
2105 offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
2106 ASSERT3U(rwa->bytes_read, >=,
2107 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
2108
2109 rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
2110 rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
2111 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
2112}
2113
60948de1 2114noinline static int
cae5b340
AX
2115receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
2116 void *data)
34dc7c2f 2117{
ea04106b 2118 dmu_object_info_t doi;
34dc7c2f 2119 dmu_tx_t *tx;
ea04106b
AX
2120 uint64_t object;
2121 int err;
34dc7c2f 2122
34dc7c2f 2123 if (drro->drr_type == DMU_OT_NONE ||
9ae529ec
CS
2124 !DMU_OT_IS_VALID(drro->drr_type) ||
2125 !DMU_OT_IS_VALID(drro->drr_bonustype) ||
428870ff 2126 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
34dc7c2f
BB
2127 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
2128 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
2129 drro->drr_blksz < SPA_MINBLOCKSIZE ||
cae5b340
AX
2130 drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
2131 drro->drr_bonuslen >
2132 DN_BONUS_SIZE(spa_maxdnodesize(dmu_objset_spa(rwa->os))) ||
2133 drro->drr_dn_slots >
2134 (spa_maxdnodesize(dmu_objset_spa(rwa->os)) >> DNODE_SHIFT)) {
a08ee875 2135 return (SET_ERROR(EINVAL));
34dc7c2f
BB
2136 }
2137
cae5b340 2138 err = dmu_object_info(rwa->os, drro->drr_object, &doi);
9babb374
BB
2139
2140 if (err != 0 && err != ENOENT)
a08ee875 2141 return (SET_ERROR(EINVAL));
ea04106b 2142 object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
9babb374 2143
ea04106b
AX
2144 /*
2145 * If we are losing blkptrs or changing the block size this must
2146 * be a new file instance. We must clear out the previous file
2147 * contents before we can change this type of metadata in the dnode.
2148 */
2149 if (err == 0) {
2150 int nblkptr;
2151
2152 nblkptr = deduce_nblkptr(drro->drr_bonustype,
2153 drro->drr_bonuslen);
2154
2155 if (drro->drr_blksz != doi.doi_data_block_size ||
2156 nblkptr < doi.doi_nblkptr) {
cae5b340 2157 err = dmu_free_long_range(rwa->os, drro->drr_object,
ea04106b
AX
2158 0, DMU_OBJECT_END);
2159 if (err != 0)
2160 return (SET_ERROR(EINVAL));
34dc7c2f 2161 }
ea04106b
AX
2162 }
2163
cae5b340 2164 tx = dmu_tx_create(rwa->os);
ea04106b
AX
2165 dmu_tx_hold_bonus(tx, object);
2166 err = dmu_tx_assign(tx, TXG_WAIT);
2167 if (err != 0) {
2168 dmu_tx_abort(tx);
2169 return (err);
2170 }
2171
2172 if (object == DMU_NEW_OBJECT) {
2173 /* currently free, want to be allocated */
cae5b340 2174 err = dmu_object_claim_dnsize(rwa->os, drro->drr_object,
34dc7c2f 2175 drro->drr_type, drro->drr_blksz,
cae5b340
AX
2176 drro->drr_bonustype, drro->drr_bonuslen,
2177 drro->drr_dn_slots << DNODE_SHIFT, tx);
ea04106b
AX
2178 } else if (drro->drr_type != doi.doi_type ||
2179 drro->drr_blksz != doi.doi_data_block_size ||
2180 drro->drr_bonustype != doi.doi_bonus_type ||
2181 drro->drr_bonuslen != doi.doi_bonus_size) {
2182 /* currently allocated, but with different properties */
cae5b340 2183 err = dmu_object_reclaim(rwa->os, drro->drr_object,
34dc7c2f 2184 drro->drr_type, drro->drr_blksz,
ea04106b 2185 drro->drr_bonustype, drro->drr_bonuslen, tx);
34dc7c2f 2186 }
a08ee875 2187 if (err != 0) {
ea04106b 2188 dmu_tx_commit(tx);
a08ee875 2189 return (SET_ERROR(EINVAL));
428870ff 2190 }
9babb374 2191
cae5b340
AX
2192 dmu_object_set_checksum(rwa->os, drro->drr_object,
2193 drro->drr_checksumtype, tx);
2194 dmu_object_set_compress(rwa->os, drro->drr_object,
2195 drro->drr_compress, tx);
34dc7c2f 2196
b128c09f 2197 if (data != NULL) {
34dc7c2f 2198 dmu_buf_t *db;
b128c09f 2199
cae5b340 2200 VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
34dc7c2f
BB
2201 dmu_buf_will_dirty(db, tx);
2202
2203 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
34dc7c2f 2204 bcopy(data, db->db_data, drro->drr_bonuslen);
cae5b340 2205 if (rwa->byteswap) {
9ae529ec
CS
2206 dmu_object_byteswap_t byteswap =
2207 DMU_OT_BYTESWAP(drro->drr_bonustype);
2208 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
34dc7c2f
BB
2209 drro->drr_bonuslen);
2210 }
2211 dmu_buf_rele(db, FTAG);
2212 }
2213 dmu_tx_commit(tx);
cae5b340 2214
34dc7c2f
BB
2215 return (0);
2216}
2217
2218/* ARGSUSED */
60948de1 2219noinline static int
cae5b340 2220receive_freeobjects(struct receive_writer_arg *rwa,
34dc7c2f
BB
2221 struct drr_freeobjects *drrfo)
2222{
2223 uint64_t obj;
cae5b340 2224 int next_err = 0;
34dc7c2f
BB
2225
2226 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
a08ee875 2227 return (SET_ERROR(EINVAL));
34dc7c2f 2228
cae5b340
AX
2229 for (obj = drrfo->drr_firstobj == 0 ? 1 : drrfo->drr_firstobj;
2230 obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
2231 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2232 dmu_object_info_t doi;
34dc7c2f
BB
2233 int err;
2234
cae5b340
AX
2235 err = dmu_object_info(rwa->os, obj, &doi);
2236 if (err == ENOENT) {
2237 obj++;
34dc7c2f 2238 continue;
cae5b340
AX
2239 } else if (err != 0) {
2240 return (err);
2241 }
34dc7c2f 2242
cae5b340 2243 err = dmu_free_long_object(rwa->os, obj);
a08ee875 2244 if (err != 0)
34dc7c2f 2245 return (err);
34dc7c2f 2246 }
cae5b340
AX
2247 if (next_err != ESRCH)
2248 return (next_err);
34dc7c2f
BB
2249 return (0);
2250}
2251
60948de1 2252noinline static int
cae5b340
AX
2253receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2254 arc_buf_t *abuf)
34dc7c2f
BB
2255{
2256 dmu_tx_t *tx;
ea04106b 2257 dmu_buf_t *bonus;
34dc7c2f
BB
2258 int err;
2259
cae5b340 2260 if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
9ae529ec 2261 !DMU_OT_IS_VALID(drrw->drr_type))
a08ee875 2262 return (SET_ERROR(EINVAL));
34dc7c2f 2263
cae5b340
AX
2264 /*
2265 * For resuming to work, records must be in increasing order
2266 * by (object, offset).
2267 */
2268 if (drrw->drr_object < rwa->last_object ||
2269 (drrw->drr_object == rwa->last_object &&
2270 drrw->drr_offset < rwa->last_offset)) {
a08ee875 2271 return (SET_ERROR(EINVAL));
cae5b340
AX
2272 }
2273 rwa->last_object = drrw->drr_object;
2274 rwa->last_offset = drrw->drr_offset;
34dc7c2f 2275
cae5b340 2276 if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
ea04106b
AX
2277 return (SET_ERROR(EINVAL));
2278
cae5b340 2279 tx = dmu_tx_create(rwa->os);
34dc7c2f
BB
2280
2281 dmu_tx_hold_write(tx, drrw->drr_object,
cae5b340 2282 drrw->drr_offset, drrw->drr_logical_size);
34dc7c2f 2283 err = dmu_tx_assign(tx, TXG_WAIT);
a08ee875 2284 if (err != 0) {
34dc7c2f
BB
2285 dmu_tx_abort(tx);
2286 return (err);
2287 }
cae5b340 2288 if (rwa->byteswap) {
9ae529ec
CS
2289 dmu_object_byteswap_t byteswap =
2290 DMU_OT_BYTESWAP(drrw->drr_type);
cae5b340
AX
2291 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
2292 DRR_WRITE_PAYLOAD_SIZE(drrw));
9ae529ec 2293 }
cae5b340
AX
2294
2295 /* use the bonus buf to look up the dnode in dmu_assign_arcbuf */
2296 if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
2297 return (SET_ERROR(EINVAL));
ea04106b 2298 dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
cae5b340
AX
2299
2300 /*
2301 * Note: If the receive fails, we want the resume stream to start
2302 * with the same record that we last successfully received (as opposed
2303 * to the next record), so that we can verify that we are
2304 * resuming from the correct location.
2305 */
2306 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
34dc7c2f 2307 dmu_tx_commit(tx);
ea04106b 2308 dmu_buf_rele(bonus, FTAG);
cae5b340 2309
34dc7c2f
BB
2310 return (0);
2311}
2312
428870ff
BB
2313/*
2314 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2315 * streams to refer to a copy of the data that is already on the
2316 * system because it came in earlier in the stream. This function
2317 * finds the earlier copy of the data, and uses that copy instead of
2318 * data from the stream to fulfill this write.
2319 */
2320static int
cae5b340 2321receive_write_byref(struct receive_writer_arg *rwa,
428870ff
BB
2322 struct drr_write_byref *drrwbr)
2323{
2324 dmu_tx_t *tx;
2325 int err;
2326 guid_map_entry_t gmesrch;
2327 guid_map_entry_t *gmep;
ea04106b 2328 avl_index_t where;
428870ff
BB
2329 objset_t *ref_os = NULL;
2330 dmu_buf_t *dbp;
2331
2332 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
a08ee875 2333 return (SET_ERROR(EINVAL));
428870ff
BB
2334
2335 /*
2336 * If the GUID of the referenced dataset is different from the
2337 * GUID of the target dataset, find the referenced dataset.
2338 */
2339 if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
2340 gmesrch.guid = drrwbr->drr_refguid;
cae5b340 2341 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
428870ff 2342 &where)) == NULL) {
a08ee875 2343 return (SET_ERROR(EINVAL));
428870ff
BB
2344 }
2345 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
a08ee875 2346 return (SET_ERROR(EINVAL));
428870ff 2347 } else {
cae5b340 2348 ref_os = rwa->os;
428870ff
BB
2349 }
2350
c65aa5b2
BB
2351 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
2352 drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
ea04106b 2353 if (err != 0)
428870ff
BB
2354 return (err);
2355
cae5b340 2356 tx = dmu_tx_create(rwa->os);
428870ff
BB
2357
2358 dmu_tx_hold_write(tx, drrwbr->drr_object,
2359 drrwbr->drr_offset, drrwbr->drr_length);
2360 err = dmu_tx_assign(tx, TXG_WAIT);
a08ee875 2361 if (err != 0) {
428870ff
BB
2362 dmu_tx_abort(tx);
2363 return (err);
2364 }
cae5b340 2365 dmu_write(rwa->os, drrwbr->drr_object,
428870ff
BB
2366 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
2367 dmu_buf_rele(dbp, FTAG);
cae5b340
AX
2368
2369 /* See comment in restore_write. */
2370 save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
428870ff
BB
2371 dmu_tx_commit(tx);
2372 return (0);
2373}
2374
ea04106b 2375static int
cae5b340
AX
2376receive_write_embedded(struct receive_writer_arg *rwa,
2377 struct drr_write_embedded *drrwe, void *data)
ea04106b
AX
2378{
2379 dmu_tx_t *tx;
2380 int err;
ea04106b 2381
cae5b340 2382 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
ea04106b
AX
2383 return (EINVAL);
2384
cae5b340 2385 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
ea04106b
AX
2386 return (EINVAL);
2387
cae5b340 2388 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
ea04106b 2389 return (EINVAL);
cae5b340 2390 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
ea04106b
AX
2391 return (EINVAL);
2392
cae5b340 2393 tx = dmu_tx_create(rwa->os);
ea04106b 2394
cae5b340
AX
2395 dmu_tx_hold_write(tx, drrwe->drr_object,
2396 drrwe->drr_offset, drrwe->drr_length);
ea04106b
AX
2397 err = dmu_tx_assign(tx, TXG_WAIT);
2398 if (err != 0) {
2399 dmu_tx_abort(tx);
2400 return (err);
2401 }
2402
cae5b340
AX
2403 dmu_write_embedded(rwa->os, drrwe->drr_object,
2404 drrwe->drr_offset, data, drrwe->drr_etype,
2405 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2406 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
ea04106b 2407
cae5b340
AX
2408 /* See comment in restore_write. */
2409 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
ea04106b
AX
2410 dmu_tx_commit(tx);
2411 return (0);
2412}
2413
428870ff 2414static int
cae5b340
AX
2415receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2416 void *data)
428870ff
BB
2417{
2418 dmu_tx_t *tx;
428870ff
BB
2419 dmu_buf_t *db, *db_spill;
2420 int err;
2421
2422 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
cae5b340 2423 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
a08ee875 2424 return (SET_ERROR(EINVAL));
428870ff 2425
cae5b340 2426 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
a08ee875 2427 return (SET_ERROR(EINVAL));
428870ff 2428
cae5b340 2429 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
428870ff
BB
2430 if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
2431 dmu_buf_rele(db, FTAG);
2432 return (err);
2433 }
2434
cae5b340 2435 tx = dmu_tx_create(rwa->os);
428870ff
BB
2436
2437 dmu_tx_hold_spill(tx, db->db_object);
2438
2439 err = dmu_tx_assign(tx, TXG_WAIT);
a08ee875 2440 if (err != 0) {
428870ff
BB
2441 dmu_buf_rele(db, FTAG);
2442 dmu_buf_rele(db_spill, FTAG);
2443 dmu_tx_abort(tx);
2444 return (err);
2445 }
2446 dmu_buf_will_dirty(db_spill, tx);
2447
2448 if (db_spill->db_size < drrs->drr_length)
2449 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
2450 drrs->drr_length, tx));
2451 bcopy(data, db_spill->db_data, drrs->drr_length);
2452
2453 dmu_buf_rele(db, FTAG);
2454 dmu_buf_rele(db_spill, FTAG);
2455
2456 dmu_tx_commit(tx);
2457 return (0);
2458}
2459
34dc7c2f 2460/* ARGSUSED */
60948de1 2461noinline static int
cae5b340 2462receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
34dc7c2f 2463{
34dc7c2f
BB
2464 int err;
2465
2466 if (drrf->drr_length != -1ULL &&
2467 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
a08ee875 2468 return (SET_ERROR(EINVAL));
34dc7c2f 2469
cae5b340 2470 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
a08ee875 2471 return (SET_ERROR(EINVAL));
34dc7c2f 2472
cae5b340 2473 err = dmu_free_long_range(rwa->os, drrf->drr_object,
34dc7c2f 2474 drrf->drr_offset, drrf->drr_length);
cae5b340 2475
34dc7c2f
BB
2476 return (err);
2477}
2478
a08ee875
LG
2479/* used to destroy the drc_ds on error */
2480static void
2481dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
2482{
cae5b340
AX
2483 if (drc->drc_resumable) {
2484 /* wait for our resume state to be written to disk */
2485 txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
2486 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2487 } else {
2488 char name[ZFS_MAX_DATASET_NAME_LEN];
2489 dsl_dataset_name(drc->drc_ds, name);
2490 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2491 (void) dsl_destroy_head(name);
2492 }
2493}
2494
2495static void
2496receive_cksum(struct receive_arg *ra, int len, void *buf)
2497{
2498 if (ra->byteswap) {
2499 (void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
2500 } else {
2501 (void) fletcher_4_incremental_native(buf, len, &ra->cksum);
2502 }
2503}
2504
2505/*
2506 * Read the payload into a buffer of size len, and update the current record's
2507 * payload field.
2508 * Allocate ra->next_rrd and read the next record's header into
2509 * ra->next_rrd->header.
2510 * Verify checksum of payload and next record.
2511 */
2512static int
2513receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
2514{
2515 int err;
2516 zio_cksum_t cksum_orig;
2517 zio_cksum_t *cksump;
2518
2519 if (len != 0) {
2520 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2521 err = receive_read(ra, len, buf);
2522 if (err != 0)
2523 return (err);
2524 receive_cksum(ra, len, buf);
2525
2526 /* note: rrd is NULL when reading the begin record's payload */
2527 if (ra->rrd != NULL) {
2528 ra->rrd->payload = buf;
2529 ra->rrd->payload_size = len;
2530 ra->rrd->bytes_read = ra->bytes_read;
2531 }
2532 }
2533
2534 ra->prev_cksum = ra->cksum;
2535
2536 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2537 err = receive_read(ra, sizeof (ra->next_rrd->header),
2538 &ra->next_rrd->header);
2539 ra->next_rrd->bytes_read = ra->bytes_read;
2540 if (err != 0) {
2541 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2542 ra->next_rrd = NULL;
2543 return (err);
2544 }
2545 if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2546 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2547 ra->next_rrd = NULL;
2548 return (SET_ERROR(EINVAL));
2549 }
2550
2551 /*
2552 * Note: checksum is of everything up to but not including the
2553 * checksum itself.
2554 */
2555 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2556 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2557 receive_cksum(ra,
2558 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2559 &ra->next_rrd->header);
2560
2561 cksum_orig = ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2562 cksump = &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2563
2564 if (ra->byteswap)
2565 byteswap_record(&ra->next_rrd->header);
2566
2567 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2568 !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2569 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2570 ra->next_rrd = NULL;
2571 return (SET_ERROR(ECKSUM));
2572 }
2573
2574 receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
2575
2576 return (0);
2577}
2578
2579static void
2580objlist_create(struct objlist *list)
2581{
2582 list_create(&list->list, sizeof (struct receive_objnode),
2583 offsetof(struct receive_objnode, node));
2584 list->last_lookup = 0;
2585}
2586
2587static void
2588objlist_destroy(struct objlist *list)
2589{
2590 struct receive_objnode *n;
2591
2592 for (n = list_remove_head(&list->list);
2593 n != NULL; n = list_remove_head(&list->list)) {
2594 kmem_free(n, sizeof (*n));
2595 }
2596 list_destroy(&list->list);
2597}
2598
2599/*
2600 * This function looks through the objlist to see if the specified object number
2601 * is contained in the objlist. In the process, it will remove all object
2602 * numbers in the list that are smaller than the specified object number. Thus,
2603 * any lookup of an object number smaller than a previously looked up object
2604 * number will always return false; therefore, all lookups should be done in
2605 * ascending order.
2606 */
2607static boolean_t
2608objlist_exists(struct objlist *list, uint64_t object)
2609{
2610 struct receive_objnode *node = list_head(&list->list);
2611 ASSERT3U(object, >=, list->last_lookup);
2612 list->last_lookup = object;
2613 while (node != NULL && node->object < object) {
2614 VERIFY3P(node, ==, list_remove_head(&list->list));
2615 kmem_free(node, sizeof (*node));
2616 node = list_head(&list->list);
2617 }
2618 return (node != NULL && node->object == object);
a08ee875
LG
2619}
2620
34dc7c2f 2621/*
cae5b340
AX
2622 * The objlist is a list of object numbers stored in ascending order. However,
2623 * the insertion of new object numbers does not seek out the correct location to
2624 * store a new object number; instead, it appends it to the list for simplicity.
2625 * Thus, any users must take care to only insert new object numbers in ascending
2626 * order.
2627 */
2628static void
2629objlist_insert(struct objlist *list, uint64_t object)
2630{
2631 struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
2632 node->object = object;
2633#ifdef ZFS_DEBUG
2634 {
2635 struct receive_objnode *last_object = list_tail(&list->list);
2636 uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
2637 ASSERT3U(node->object, >, last_objnum);
2638 }
2639#endif
2640 list_insert_tail(&list->list, node);
2641}
2642
2643/*
2644 * Issue the prefetch reads for any necessary indirect blocks.
2645 *
2646 * We use the object ignore list to tell us whether or not to issue prefetches
2647 * for a given object. We do this for both correctness (in case the blocksize
2648 * of an object has changed) and performance (if the object doesn't exist, don't
2649 * needlessly try to issue prefetches). We also trim the list as we go through
2650 * the stream to prevent it from growing to an unbounded size.
2651 *
2652 * The object numbers within will always be in sorted order, and any write
2653 * records we see will also be in sorted order, but they're not sorted with
2654 * respect to each other (i.e. we can get several object records before
2655 * receiving each object's write records). As a result, once we've reached a
2656 * given object number, we can safely remove any reference to lower object
2657 * numbers in the ignore list. In practice, we receive up to 32 object records
2658 * before receiving write records, so the list can have up to 32 nodes in it.
2659 */
2660/* ARGSUSED */
2661static void
2662receive_read_prefetch(struct receive_arg *ra,
2663 uint64_t object, uint64_t offset, uint64_t length)
2664{
2665 if (!objlist_exists(&ra->ignore_objlist, object)) {
2666 dmu_prefetch(ra->os, object, 1, offset, length,
2667 ZIO_PRIORITY_SYNC_READ);
2668 }
2669}
2670
2671/*
2672 * Read records off the stream, issuing any necessary prefetches.
2673 */
2674static int
2675receive_read_record(struct receive_arg *ra)
2676{
2677 int err;
2678
2679 switch (ra->rrd->header.drr_type) {
2680 case DRR_OBJECT:
2681 {
2682 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2683 uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2684 void *buf = kmem_zalloc(size, KM_SLEEP);
2685 dmu_object_info_t doi;
2686 err = receive_read_payload_and_next_header(ra, size, buf);
2687 if (err != 0) {
2688 kmem_free(buf, size);
2689 return (err);
2690 }
2691 err = dmu_object_info(ra->os, drro->drr_object, &doi);
2692 /*
2693 * See receive_read_prefetch for an explanation why we're
2694 * storing this object in the ignore_obj_list.
2695 */
2696 if (err == ENOENT ||
2697 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2698 objlist_insert(&ra->ignore_objlist, drro->drr_object);
2699 err = 0;
2700 }
2701 return (err);
2702 }
2703 case DRR_FREEOBJECTS:
2704 {
2705 err = receive_read_payload_and_next_header(ra, 0, NULL);
2706 return (err);
2707 }
2708 case DRR_WRITE:
2709 {
2710 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2711 arc_buf_t *abuf;
2712 boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
2713 if (DRR_WRITE_COMPRESSED(drrw)) {
2714 ASSERT3U(drrw->drr_compressed_size, >, 0);
2715 ASSERT3U(drrw->drr_logical_size, >=,
2716 drrw->drr_compressed_size);
2717 ASSERT(!is_meta);
2718 abuf = arc_loan_compressed_buf(
2719 dmu_objset_spa(ra->os),
2720 drrw->drr_compressed_size, drrw->drr_logical_size,
2721 drrw->drr_compressiontype);
2722 } else {
2723 abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2724 is_meta, drrw->drr_logical_size);
2725 }
2726
2727 err = receive_read_payload_and_next_header(ra,
2728 DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
2729 if (err != 0) {
2730 dmu_return_arcbuf(abuf);
2731 return (err);
2732 }
2733 ra->rrd->write_buf = abuf;
2734 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2735 drrw->drr_logical_size);
2736 return (err);
2737 }
2738 case DRR_WRITE_BYREF:
2739 {
2740 struct drr_write_byref *drrwb =
2741 &ra->rrd->header.drr_u.drr_write_byref;
2742 err = receive_read_payload_and_next_header(ra, 0, NULL);
2743 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2744 drrwb->drr_length);
2745 return (err);
2746 }
2747 case DRR_WRITE_EMBEDDED:
2748 {
2749 struct drr_write_embedded *drrwe =
2750 &ra->rrd->header.drr_u.drr_write_embedded;
2751 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2752 void *buf = kmem_zalloc(size, KM_SLEEP);
2753
2754 err = receive_read_payload_and_next_header(ra, size, buf);
2755 if (err != 0) {
2756 kmem_free(buf, size);
2757 return (err);
2758 }
2759
2760 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2761 drrwe->drr_length);
2762 return (err);
2763 }
2764 case DRR_FREE:
2765 {
2766 /*
2767 * It might be beneficial to prefetch indirect blocks here, but
2768 * we don't really have the data to decide for sure.
2769 */
2770 err = receive_read_payload_and_next_header(ra, 0, NULL);
2771 return (err);
2772 }
2773 case DRR_END:
2774 {
2775 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
2776 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2777 return (SET_ERROR(ECKSUM));
2778 return (0);
2779 }
2780 case DRR_SPILL:
2781 {
2782 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2783 void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2784 err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2785 buf);
2786 if (err != 0)
2787 kmem_free(buf, drrs->drr_length);
2788 return (err);
2789 }
2790 default:
2791 return (SET_ERROR(EINVAL));
2792 }
2793}
2794
2795static void
2796dprintf_drr(struct receive_record_arg *rrd, int err)
2797{
2798 switch (rrd->header.drr_type) {
2799 case DRR_OBJECT:
2800 {
2801 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2802 dprintf("drr_type = OBJECT obj = %llu type = %u "
2803 "bonustype = %u blksz = %u bonuslen = %u cksumtype = %u "
2804 "compress = %u dn_slots = %u err = %d\n",
2805 drro->drr_object, drro->drr_type, drro->drr_bonustype,
2806 drro->drr_blksz, drro->drr_bonuslen,
2807 drro->drr_checksumtype, drro->drr_compress,
2808 drro->drr_dn_slots, err);
2809 break;
2810 }
2811 case DRR_FREEOBJECTS:
2812 {
2813 struct drr_freeobjects *drrfo =
2814 &rrd->header.drr_u.drr_freeobjects;
2815 dprintf("drr_type = FREEOBJECTS firstobj = %llu "
2816 "numobjs = %llu err = %d\n",
2817 drrfo->drr_firstobj, drrfo->drr_numobjs, err);
2818 break;
2819 }
2820 case DRR_WRITE:
2821 {
2822 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2823 dprintf("drr_type = WRITE obj = %llu type = %u offset = %llu "
2824 "lsize = %llu cksumtype = %u cksumflags = %u "
2825 "compress = %u psize = %llu err = %d\n",
2826 drrw->drr_object, drrw->drr_type, drrw->drr_offset,
2827 drrw->drr_logical_size, drrw->drr_checksumtype,
2828 drrw->drr_checksumflags, drrw->drr_compressiontype,
2829 drrw->drr_compressed_size, err);
2830 break;
2831 }
2832 case DRR_WRITE_BYREF:
2833 {
2834 struct drr_write_byref *drrwbr =
2835 &rrd->header.drr_u.drr_write_byref;
2836 dprintf("drr_type = WRITE_BYREF obj = %llu offset = %llu "
2837 "length = %llu toguid = %llx refguid = %llx "
2838 "refobject = %llu refoffset = %llu cksumtype = %u "
2839 "cksumflags = %u err = %d\n",
2840 drrwbr->drr_object, drrwbr->drr_offset,
2841 drrwbr->drr_length, drrwbr->drr_toguid,
2842 drrwbr->drr_refguid, drrwbr->drr_refobject,
2843 drrwbr->drr_refoffset, drrwbr->drr_checksumtype,
2844 drrwbr->drr_checksumflags, err);
2845 break;
2846 }
2847 case DRR_WRITE_EMBEDDED:
2848 {
2849 struct drr_write_embedded *drrwe =
2850 &rrd->header.drr_u.drr_write_embedded;
2851 dprintf("drr_type = WRITE_EMBEDDED obj = %llu offset = %llu "
2852 "length = %llu compress = %u etype = %u lsize = %u "
2853 "psize = %u err = %d\n",
2854 drrwe->drr_object, drrwe->drr_offset, drrwe->drr_length,
2855 drrwe->drr_compression, drrwe->drr_etype,
2856 drrwe->drr_lsize, drrwe->drr_psize, err);
2857 break;
2858 }
2859 case DRR_FREE:
2860 {
2861 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2862 dprintf("drr_type = FREE obj = %llu offset = %llu "
2863 "length = %lld err = %d\n",
2864 drrf->drr_object, drrf->drr_offset, drrf->drr_length,
2865 err);
2866 break;
2867 }
2868 case DRR_SPILL:
2869 {
2870 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2871 dprintf("drr_type = SPILL obj = %llu length = %llu "
2872 "err = %d\n", drrs->drr_object, drrs->drr_length, err);
2873 break;
2874 }
2875 default:
2876 return;
2877 }
2878}
2879
2880/*
2881 * Commit the records to the pool.
2882 */
2883static int
2884receive_process_record(struct receive_writer_arg *rwa,
2885 struct receive_record_arg *rrd)
2886{
2887 int err;
2888
2889 /* Processing in order, therefore bytes_read should be increasing. */
2890 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2891 rwa->bytes_read = rrd->bytes_read;
2892
2893 switch (rrd->header.drr_type) {
2894 case DRR_OBJECT:
2895 {
2896 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2897 err = receive_object(rwa, drro, rrd->payload);
2898 kmem_free(rrd->payload, rrd->payload_size);
2899 rrd->payload = NULL;
2900 break;
2901 }
2902 case DRR_FREEOBJECTS:
2903 {
2904 struct drr_freeobjects *drrfo =
2905 &rrd->header.drr_u.drr_freeobjects;
2906 err = receive_freeobjects(rwa, drrfo);
2907 break;
2908 }
2909 case DRR_WRITE:
2910 {
2911 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2912 err = receive_write(rwa, drrw, rrd->write_buf);
2913 /* if receive_write() is successful, it consumes the arc_buf */
2914 if (err != 0)
2915 dmu_return_arcbuf(rrd->write_buf);
2916 rrd->write_buf = NULL;
2917 rrd->payload = NULL;
2918 break;
2919 }
2920 case DRR_WRITE_BYREF:
2921 {
2922 struct drr_write_byref *drrwbr =
2923 &rrd->header.drr_u.drr_write_byref;
2924 err = receive_write_byref(rwa, drrwbr);
2925 break;
2926 }
2927 case DRR_WRITE_EMBEDDED:
2928 {
2929 struct drr_write_embedded *drrwe =
2930 &rrd->header.drr_u.drr_write_embedded;
2931 err = receive_write_embedded(rwa, drrwe, rrd->payload);
2932 kmem_free(rrd->payload, rrd->payload_size);
2933 rrd->payload = NULL;
2934 break;
2935 }
2936 case DRR_FREE:
2937 {
2938 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2939 err = receive_free(rwa, drrf);
2940 break;
2941 }
2942 case DRR_SPILL:
2943 {
2944 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2945 err = receive_spill(rwa, drrs, rrd->payload);
2946 kmem_free(rrd->payload, rrd->payload_size);
2947 rrd->payload = NULL;
2948 break;
2949 }
2950 default:
2951 return (SET_ERROR(EINVAL));
2952 }
2953
2954 if (err != 0)
2955 dprintf_drr(rrd, err);
2956
2957 return (err);
2958}
2959
2960/*
2961 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2962 * receive_process_record When we're done, signal the main thread and exit.
2963 */
2964static void
2965receive_writer_thread(void *arg)
2966{
2967 struct receive_writer_arg *rwa = arg;
2968 struct receive_record_arg *rrd;
2969 fstrans_cookie_t cookie = spl_fstrans_mark();
2970
2971 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2972 rrd = bqueue_dequeue(&rwa->q)) {
2973 /*
2974 * If there's an error, the main thread will stop putting things
2975 * on the queue, but we need to clear everything in it before we
2976 * can exit.
2977 */
2978 if (rwa->err == 0) {
2979 rwa->err = receive_process_record(rwa, rrd);
2980 } else if (rrd->write_buf != NULL) {
2981 dmu_return_arcbuf(rrd->write_buf);
2982 rrd->write_buf = NULL;
2983 rrd->payload = NULL;
2984 } else if (rrd->payload != NULL) {
2985 kmem_free(rrd->payload, rrd->payload_size);
2986 rrd->payload = NULL;
2987 }
2988 kmem_free(rrd, sizeof (*rrd));
2989 }
2990 kmem_free(rrd, sizeof (*rrd));
2991 mutex_enter(&rwa->mutex);
2992 rwa->done = B_TRUE;
2993 cv_signal(&rwa->cv);
2994 mutex_exit(&rwa->mutex);
2995 spl_fstrans_unmark(cookie);
2996 thread_exit();
2997}
2998
2999static int
3000resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
3001{
3002 uint64_t val;
3003 objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
3004 uint64_t dsobj = dmu_objset_id(ra->os);
3005 uint64_t resume_obj, resume_off;
3006
3007 if (nvlist_lookup_uint64(begin_nvl,
3008 "resume_object", &resume_obj) != 0 ||
3009 nvlist_lookup_uint64(begin_nvl,
3010 "resume_offset", &resume_off) != 0) {
3011 return (SET_ERROR(EINVAL));
3012 }
3013 VERIFY0(zap_lookup(mos, dsobj,
3014 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
3015 if (resume_obj != val)
3016 return (SET_ERROR(EINVAL));
3017 VERIFY0(zap_lookup(mos, dsobj,
3018 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
3019 if (resume_off != val)
3020 return (SET_ERROR(EINVAL));
3021
3022 return (0);
3023}
3024
3025/*
3026 * Read in the stream's records, one by one, and apply them to the pool. There
3027 * are two threads involved; the thread that calls this function will spin up a
3028 * worker thread, read the records off the stream one by one, and issue
3029 * prefetches for any necessary indirect blocks. It will then push the records
3030 * onto an internal blocking queue. The worker thread will pull the records off
3031 * the queue, and actually write the data into the DMU. This way, the worker
3032 * thread doesn't have to wait for reads to complete, since everything it needs
3033 * (the indirect blocks) will be prefetched.
3034 *
34dc7c2f
BB
3035 * NB: callers *must* call dmu_recv_end() if this succeeds.
3036 */
3037int
572e2857
BB
3038dmu_recv_stream(dmu_recv_cookie_t *drc, vnode_t *vp, offset_t *voffp,
3039 int cleanup_fd, uint64_t *action_handlep)
34dc7c2f 3040{
cae5b340
AX
3041 int err = 0;
3042 struct receive_arg *ra;
3043 struct receive_writer_arg *rwa;
428870ff 3044 int featureflags;
cae5b340
AX
3045 uint32_t payloadlen;
3046 void *payload;
3047 nvlist_t *begin_nvl = NULL;
3048
3049 ra = kmem_zalloc(sizeof (*ra), KM_SLEEP);
3050 rwa = kmem_zalloc(sizeof (*rwa), KM_SLEEP);
3051
3052 ra->byteswap = drc->drc_byteswap;
3053 ra->cksum = drc->drc_cksum;
3054 ra->vp = vp;
3055 ra->voff = *voffp;
3056
3057 if (dsl_dataset_is_zapified(drc->drc_ds)) {
3058 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
3059 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
3060 sizeof (ra->bytes_read), 1, &ra->bytes_read);
3061 }
34dc7c2f 3062
cae5b340 3063 objlist_create(&ra->ignore_objlist);
34dc7c2f
BB
3064
3065 /* these were verified in dmu_recv_begin */
a08ee875 3066 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
428870ff 3067 DMU_SUBSTREAM);
a08ee875 3068 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
34dc7c2f
BB
3069
3070 /*
3071 * Open the objset we are modifying.
3072 */
cae5b340 3073 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra->os));
34dc7c2f 3074
e10b0808 3075 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
34dc7c2f 3076
428870ff
BB
3077 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
3078
3079 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
3080 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
572e2857
BB
3081 minor_t minor;
3082
3083 if (cleanup_fd == -1) {
cae5b340 3084 ra->err = SET_ERROR(EBADF);
572e2857
BB
3085 goto out;
3086 }
cae5b340
AX
3087 ra->err = zfs_onexit_fd_hold(cleanup_fd, &minor);
3088 if (ra->err != 0) {
572e2857
BB
3089 cleanup_fd = -1;
3090 goto out;
3091 }
3092
3093 if (*action_handlep == 0) {
cae5b340 3094 rwa->guid_to_ds_map =
572e2857 3095 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
cae5b340 3096 avl_create(rwa->guid_to_ds_map, guid_compare,
572e2857
BB
3097 sizeof (guid_map_entry_t),
3098 offsetof(guid_map_entry_t, avlnode));
cae5b340
AX
3099 err = zfs_onexit_add_cb(minor,
3100 free_guid_map_onexit, rwa->guid_to_ds_map,
572e2857 3101 action_handlep);
cae5b340 3102 if (ra->err != 0)
572e2857
BB
3103 goto out;
3104 } else {
cae5b340
AX
3105 err = zfs_onexit_cb_data(minor, *action_handlep,
3106 (void **)&rwa->guid_to_ds_map);
3107 if (ra->err != 0)
572e2857
BB
3108 goto out;
3109 }
8d35c149 3110
cae5b340
AX
3111 drc->drc_guid_to_ds_map = rwa->guid_to_ds_map;
3112 }
3113
3114 payloadlen = drc->drc_drr_begin->drr_payloadlen;
3115 payload = NULL;
3116 if (payloadlen != 0)
3117 payload = kmem_alloc(payloadlen, KM_SLEEP);
3118
3119 err = receive_read_payload_and_next_header(ra, payloadlen, payload);
3120 if (err != 0) {
3121 if (payloadlen != 0)
3122 kmem_free(payload, payloadlen);
3123 goto out;
3124 }
3125 if (payloadlen != 0) {
3126 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
3127 kmem_free(payload, payloadlen);
3128 if (err != 0)
3129 goto out;
3130 }
3131
3132 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
3133 err = resume_check(ra, begin_nvl);
3134 if (err != 0)
3135 goto out;
428870ff
BB
3136 }
3137
cae5b340
AX
3138 (void) bqueue_init(&rwa->q, zfs_recv_queue_length,
3139 offsetof(struct receive_record_arg, node));
3140 cv_init(&rwa->cv, NULL, CV_DEFAULT, NULL);
3141 mutex_init(&rwa->mutex, NULL, MUTEX_DEFAULT, NULL);
3142 rwa->os = ra->os;
3143 rwa->byteswap = drc->drc_byteswap;
3144 rwa->resumable = drc->drc_resumable;
3145
3146 (void) thread_create(NULL, 0, receive_writer_thread, rwa, 0, curproc,
3147 TS_RUN, minclsyspri);
34dc7c2f 3148 /*
cae5b340
AX
3149 * We're reading rwa->err without locks, which is safe since we are the
3150 * only reader, and the worker thread is the only writer. It's ok if we
3151 * miss a write for an iteration or two of the loop, since the writer
3152 * thread will keep freeing records we send it until we send it an eos
3153 * marker.
3154 *
3155 * We can leave this loop in 3 ways: First, if rwa->err is
3156 * non-zero. In that case, the writer thread will free the rrd we just
3157 * pushed. Second, if we're interrupted; in that case, either it's the
3158 * first loop and ra->rrd was never allocated, or it's later and ra->rrd
3159 * has been handed off to the writer thread who will free it. Finally,
3160 * if receive_read_record fails or we're at the end of the stream, then
3161 * we free ra->rrd and exit.
34dc7c2f 3162 */
cae5b340 3163 while (rwa->err == 0) {
34dc7c2f 3164 if (issig(JUSTLOOKING) && issig(FORREAL)) {
cae5b340
AX
3165 err = SET_ERROR(EINTR);
3166 break;
34dc7c2f
BB
3167 }
3168
cae5b340
AX
3169 ASSERT3P(ra->rrd, ==, NULL);
3170 ra->rrd = ra->next_rrd;
3171 ra->next_rrd = NULL;
3172 /* Allocates and loads header into ra->next_rrd */
3173 err = receive_read_record(ra);
34dc7c2f 3174
cae5b340
AX
3175 if (ra->rrd->header.drr_type == DRR_END || err != 0) {
3176 kmem_free(ra->rrd, sizeof (*ra->rrd));
3177 ra->rrd = NULL;
34dc7c2f
BB
3178 break;
3179 }
cae5b340
AX
3180
3181 bqueue_enqueue(&rwa->q, ra->rrd,
3182 sizeof (struct receive_record_arg) + ra->rrd->payload_size);
3183 ra->rrd = NULL;
3184 }
3185 if (ra->next_rrd == NULL)
3186 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
3187 ra->next_rrd->eos_marker = B_TRUE;
3188 bqueue_enqueue(&rwa->q, ra->next_rrd, 1);
3189
3190 mutex_enter(&rwa->mutex);
3191 while (!rwa->done) {
3192 cv_wait(&rwa->cv, &rwa->mutex);
34dc7c2f 3193 }
cae5b340
AX
3194 mutex_exit(&rwa->mutex);
3195
3196 cv_destroy(&rwa->cv);
3197 mutex_destroy(&rwa->mutex);
3198 bqueue_destroy(&rwa->q);
3199 if (err == 0)
3200 err = rwa->err;
34dc7c2f
BB
3201
3202out:
cae5b340 3203 nvlist_free(begin_nvl);
572e2857
BB
3204 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
3205 zfs_onexit_fd_rele(cleanup_fd);
3206
cae5b340 3207 if (err != 0) {
34dc7c2f 3208 /*
cae5b340
AX
3209 * Clean up references. If receive is not resumable,
3210 * destroy what we created, so we don't leave it in
3211 * the inconsistent state.
34dc7c2f 3212 */
a08ee875 3213 dmu_recv_cleanup_ds(drc);
34dc7c2f
BB
3214 }
3215
cae5b340
AX
3216 *voffp = ra->voff;
3217 objlist_destroy(&ra->ignore_objlist);
3218 kmem_free(ra, sizeof (*ra));
3219 kmem_free(rwa, sizeof (*rwa));
3220 return (err);
34dc7c2f
BB
3221}
3222
34dc7c2f 3223static int
a08ee875 3224dmu_recv_end_check(void *arg, dmu_tx_t *tx)
34dc7c2f 3225{
a08ee875
LG
3226 dmu_recv_cookie_t *drc = arg;
3227 dsl_pool_t *dp = dmu_tx_pool(tx);
3228 int error;
3229
3230 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
3231
3232 if (!drc->drc_newfs) {
3233 dsl_dataset_t *origin_head;
3234
3235 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
3236 if (error != 0)
3237 return (error);
3238 if (drc->drc_force) {
3239 /*
3240 * We will destroy any snapshots in tofs (i.e. before
3241 * origin_head) that are after the origin (which is
3242 * the snap before drc_ds, because drc_ds can not
3243 * have any snaps of its own).
3244 */
e10b0808
AX
3245 uint64_t obj;
3246
3247 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3248 while (obj !=
3249 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
a08ee875
LG
3250 dsl_dataset_t *snap;
3251 error = dsl_dataset_hold_obj(dp, obj, FTAG,
3252 &snap);
3253 if (error != 0)
e10b0808 3254 break;
a08ee875
LG
3255 if (snap->ds_dir != origin_head->ds_dir)
3256 error = SET_ERROR(EINVAL);
3257 if (error == 0) {
3258 error = dsl_destroy_snapshot_check_impl(
3259 snap, B_FALSE);
3260 }
e10b0808 3261 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
a08ee875
LG
3262 dsl_dataset_rele(snap, FTAG);
3263 if (error != 0)
e10b0808
AX
3264 break;
3265 }
3266 if (error != 0) {
3267 dsl_dataset_rele(origin_head, FTAG);
3268 return (error);
a08ee875
LG
3269 }
3270 }
3271 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
3272 origin_head, drc->drc_force, drc->drc_owner, tx);
3273 if (error != 0) {
3274 dsl_dataset_rele(origin_head, FTAG);
3275 return (error);
3276 }
3277 error = dsl_dataset_snapshot_check_impl(origin_head,
e10b0808 3278 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
a08ee875
LG
3279 dsl_dataset_rele(origin_head, FTAG);
3280 if (error != 0)
3281 return (error);
34dc7c2f 3282
a08ee875
LG
3283 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
3284 } else {
3285 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
e10b0808 3286 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
a08ee875
LG
3287 }
3288 return (error);
34dc7c2f
BB
3289}
3290
3291static void
a08ee875 3292dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
34dc7c2f 3293{
a08ee875
LG
3294 dmu_recv_cookie_t *drc = arg;
3295 dsl_pool_t *dp = dmu_tx_pool(tx);
3296
3297 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
3298 tx, "snap=%s", drc->drc_tosnap);
3299
3300 if (!drc->drc_newfs) {
3301 dsl_dataset_t *origin_head;
3302
3303 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3304 &origin_head));
3305
3306 if (drc->drc_force) {
3307 /*
3308 * Destroy any snapshots of drc_tofs (origin_head)
3309 * after the origin (the snap before drc_ds).
3310 */
e10b0808
AX
3311 uint64_t obj;
3312
3313 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3314 while (obj !=
3315 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
a08ee875
LG
3316 dsl_dataset_t *snap;
3317 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
3318 &snap));
3319 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
e10b0808 3320 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
a08ee875
LG
3321 dsl_destroy_snapshot_sync_impl(snap,
3322 B_FALSE, tx);
3323 dsl_dataset_rele(snap, FTAG);
3324 }
3325 }
3326 VERIFY3P(drc->drc_ds->ds_prev, ==,
3327 origin_head->ds_prev);
3328
3329 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3330 origin_head, tx);
3331 dsl_dataset_snapshot_sync_impl(origin_head,
3332 drc->drc_tosnap, tx);
3333
3334 /* set snapshot's creation time and guid */
3335 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
e10b0808 3336 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
a08ee875 3337 drc->drc_drrb->drr_creation_time;
e10b0808 3338 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
a08ee875 3339 drc->drc_drrb->drr_toguid;
e10b0808 3340 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
a08ee875
LG
3341 ~DS_FLAG_INCONSISTENT;
3342
3343 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
e10b0808
AX
3344 dsl_dataset_phys(origin_head)->ds_flags &=
3345 ~DS_FLAG_INCONSISTENT;
a08ee875 3346
cae5b340
AX
3347 drc->drc_newsnapobj =
3348 dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3349
a08ee875
LG
3350 dsl_dataset_rele(origin_head, FTAG);
3351 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
3352
3353 if (drc->drc_owner != NULL)
3354 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3355 } else {
3356 dsl_dataset_t *ds = drc->drc_ds;
34dc7c2f 3357
a08ee875 3358 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
34dc7c2f 3359
a08ee875
LG
3360 /* set snapshot's creation time and guid */
3361 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
e10b0808 3362 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
a08ee875 3363 drc->drc_drrb->drr_creation_time;
e10b0808
AX
3364 dsl_dataset_phys(ds->ds_prev)->ds_guid =
3365 drc->drc_drrb->drr_toguid;
3366 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3367 ~DS_FLAG_INCONSISTENT;
34dc7c2f 3368
a08ee875 3369 dmu_buf_will_dirty(ds->ds_dbuf, tx);
e10b0808 3370 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
cae5b340
AX
3371 if (dsl_dataset_has_resume_receive_state(ds)) {
3372 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3373 DS_FIELD_RESUME_FROMGUID, tx);
3374 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3375 DS_FIELD_RESUME_OBJECT, tx);
3376 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3377 DS_FIELD_RESUME_OFFSET, tx);
3378 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3379 DS_FIELD_RESUME_BYTES, tx);
3380 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3381 DS_FIELD_RESUME_TOGUID, tx);
3382 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3383 DS_FIELD_RESUME_TONAME, tx);
3384 }
3385 drc->drc_newsnapobj =
3386 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
a08ee875 3387 }
4e820b5a 3388 zvol_create_minors(dp->dp_spa, drc->drc_tofs, B_TRUE);
a08ee875
LG
3389 /*
3390 * Release the hold from dmu_recv_begin. This must be done before
3391 * we return to open context, so that when we free the dataset's dnode,
3392 * we can evict its bonus buffer.
3393 */
3394 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
3395 drc->drc_ds = NULL;
34dc7c2f
BB
3396}
3397
8d35c149 3398static int
a08ee875 3399add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
8d35c149 3400{
a08ee875 3401 dsl_pool_t *dp;
8d35c149
AS
3402 dsl_dataset_t *snapds;
3403 guid_map_entry_t *gmep;
3404 int err;
3405
3406 ASSERT(guid_map != NULL);
3407
a08ee875
LG
3408 err = dsl_pool_hold(name, FTAG, &dp);
3409 if (err != 0)
3410 return (err);
3411 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3412 err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
8d35c149 3413 if (err == 0) {
e10b0808 3414 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
8d35c149
AS
3415 gmep->gme_ds = snapds;
3416 avl_add(guid_map, gmep);
a08ee875
LG
3417 dsl_dataset_long_hold(snapds, gmep);
3418 } else {
3419 kmem_free(gmep, sizeof (*gmep));
8d35c149
AS
3420 }
3421
a08ee875 3422 dsl_pool_rele(dp, FTAG);
8d35c149
AS
3423 return (err);
3424}
3425
a08ee875
LG
3426static int dmu_recv_end_modified_blocks = 3;
3427
428870ff
BB
3428static int
3429dmu_recv_existing_end(dmu_recv_cookie_t *drc)
34dc7c2f 3430{
a08ee875 3431#ifdef _KERNEL
a08ee875
LG
3432 /*
3433 * We will be destroying the ds; make sure its origin is unmounted if
3434 * necessary.
3435 */
cae5b340 3436 char name[ZFS_MAX_DATASET_NAME_LEN];
a08ee875
LG
3437 dsl_dataset_name(drc->drc_ds, name);
3438 zfs_destroy_unmount_origin(name);
a08ee875 3439#endif
34dc7c2f 3440
cae5b340 3441 return (dsl_sync_task(drc->drc_tofs,
a08ee875 3442 dmu_recv_end_check, dmu_recv_end_sync, drc,
cae5b340 3443 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
34dc7c2f 3444}
428870ff
BB
3445
3446static int
3447dmu_recv_new_end(dmu_recv_cookie_t *drc)
cae5b340
AX
3448{
3449 return (dsl_sync_task(drc->drc_tofs,
3450 dmu_recv_end_check, dmu_recv_end_sync, drc,
3451 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3452}
3453
3454int
3455dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
428870ff 3456{
a08ee875
LG
3457 int error;
3458
cae5b340
AX
3459 drc->drc_owner = owner;
3460
3461 if (drc->drc_newfs)
3462 error = dmu_recv_new_end(drc);
3463 else
3464 error = dmu_recv_existing_end(drc);
a08ee875
LG
3465
3466 if (error != 0) {
3467 dmu_recv_cleanup_ds(drc);
3468 } else if (drc->drc_guid_to_ds_map != NULL) {
3469 (void) add_ds_to_guidmap(drc->drc_tofs,
3470 drc->drc_guid_to_ds_map,
3471 drc->drc_newsnapobj);
428870ff 3472 }
a08ee875 3473 return (error);
428870ff
BB
3474}
3475
a08ee875
LG
3476/*
3477 * Return TRUE if this objset is currently being received into.
3478 */
3479boolean_t
3480dmu_objset_is_receiving(objset_t *os)
3481{
3482 return (os->os_dsl_dataset != NULL &&
3483 os->os_dsl_dataset->ds_owner == dmu_recv_tag);
3484}
3485
3486#if defined(_KERNEL)
3487module_param(zfs_send_corrupt_data, int, 0644);
3488MODULE_PARM_DESC(zfs_send_corrupt_data, "Allow sending corrupt data");
3489#endif