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