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