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