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