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