1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12 /*
13 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
14 * Copyright 2011 Nexenta Systems, Inc. All rights reserved.
15 * Copyright (c) 2011, 2018 by Delphix. All rights reserved.
16 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
17 * Copyright 2014 HybridCluster. All rights reserved.
18 * Copyright 2016 RackTop Systems.
19 * Copyright (c) 2016 Actifio, Inc. All rights reserved.
20 * Copyright (c) 2019, 2024, Klara, Inc.
21 * Copyright (c) 2019, Allan Jude
22 */
23
24 #include <sys/dmu.h>
25 #include <sys/dmu_impl.h>
26 #include <sys/dmu_tx.h>
27 #include <sys/dbuf.h>
28 #include <sys/dnode.h>
29 #include <sys/zfs_context.h>
30 #include <sys/dmu_objset.h>
31 #include <sys/dmu_traverse.h>
32 #include <sys/dsl_dataset.h>
33 #include <sys/dsl_dir.h>
34 #include <sys/dsl_prop.h>
35 #include <sys/dsl_pool.h>
36 #include <sys/dsl_synctask.h>
37 #include <sys/spa_impl.h>
38 #include <sys/zfs_ioctl.h>
39 #include <sys/zap.h>
40 #include <sys/zio_checksum.h>
41 #include <sys/zfs_znode.h>
42 #include <zfs_fletcher.h>
43 #include <sys/avl.h>
44 #include <sys/ddt.h>
45 #include <sys/zfs_onexit.h>
46 #include <sys/dmu_send.h>
47 #include <sys/dmu_recv.h>
48 #include <sys/dsl_destroy.h>
49 #include <sys/blkptr.h>
50 #include <sys/dsl_bookmark.h>
51 #include <sys/zfeature.h>
52 #include <sys/bqueue.h>
53 #include <sys/zvol.h>
54 #include <sys/policy.h>
55 #include <sys/objlist.h>
56 #ifdef _KERNEL
57 #include <sys/zfs_vfsops.h>
58 #endif
59
60 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
61 static int zfs_send_corrupt_data = B_FALSE;
62 /*
63 * This tunable controls the amount of data (measured in bytes) that will be
64 * prefetched by zfs send. If the main thread is blocking on reads that haven't
65 * completed, this variable might need to be increased. If instead the main
66 * thread is issuing new reads because the prefetches have fallen out of the
67 * cache, this may need to be decreased.
68 */
69 static uint_t zfs_send_queue_length = SPA_MAXBLOCKSIZE;
70 /*
71 * This tunable controls the length of the queues that zfs send worker threads
72 * use to communicate. If the send_main_thread is blocking on these queues,
73 * this variable may need to be increased. If there is a significant slowdown
74 * at the start of a send as these threads consume all the available IO
75 * resources, this variable may need to be decreased.
76 */
77 static uint_t zfs_send_no_prefetch_queue_length = 1024 * 1024;
78 /*
79 * These tunables control the fill fraction of the queues by zfs send. The fill
80 * fraction controls the frequency with which threads have to be cv_signaled.
81 * If a lot of cpu time is being spent on cv_signal, then these should be tuned
82 * down. If the queues empty before the signalled thread can catch up, then
83 * these should be tuned up.
84 */
85 static uint_t zfs_send_queue_ff = 20;
86 static uint_t zfs_send_no_prefetch_queue_ff = 20;
87
88 /*
89 * Use this to override the recordsize calculation for fast zfs send estimates.
90 */
91 static uint_t zfs_override_estimate_recordsize = 0;
92
93 /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
94 static const boolean_t zfs_send_set_freerecords_bit = B_TRUE;
95
96 /* Set this tunable to FALSE is disable sending unmodified spill blocks. */
97 static int zfs_send_unmodified_spill_blocks = B_TRUE;
98
99 static inline boolean_t
overflow_multiply(uint64_t a,uint64_t b,uint64_t * c)100 overflow_multiply(uint64_t a, uint64_t b, uint64_t *c)
101 {
102 uint64_t temp = a * b;
103 if (b != 0 && temp / b != a)
104 return (B_FALSE);
105 *c = temp;
106 return (B_TRUE);
107 }
108
109 struct send_thread_arg {
110 bqueue_t q;
111 objset_t *os; /* Objset to traverse */
112 uint64_t fromtxg; /* Traverse from this txg */
113 int flags; /* flags to pass to traverse_dataset */
114 int error_code;
115 boolean_t cancel;
116 zbookmark_phys_t resume;
117 uint64_t *num_blocks_visited;
118 };
119
120 struct redact_list_thread_arg {
121 boolean_t cancel;
122 bqueue_t q;
123 zbookmark_phys_t resume;
124 redaction_list_t *rl;
125 boolean_t mark_redact;
126 int error_code;
127 uint64_t *num_blocks_visited;
128 };
129
130 struct send_merge_thread_arg {
131 bqueue_t q;
132 objset_t *os;
133 struct redact_list_thread_arg *from_arg;
134 struct send_thread_arg *to_arg;
135 struct redact_list_thread_arg *redact_arg;
136 int error;
137 boolean_t cancel;
138 };
139
140 struct send_range {
141 boolean_t eos_marker; /* Marks the end of the stream */
142 uint64_t object;
143 uint64_t start_blkid;
144 uint64_t end_blkid;
145 bqueue_node_t ln;
146 enum type {DATA, HOLE, OBJECT, OBJECT_RANGE, REDACT,
147 PREVIOUSLY_REDACTED} type;
148 union {
149 struct srd {
150 dmu_object_type_t obj_type;
151 uint32_t datablksz; // logical size
152 uint32_t datasz; // payload size
153 blkptr_t bp;
154 arc_buf_t *abuf;
155 abd_t *abd;
156 kmutex_t lock;
157 kcondvar_t cv;
158 boolean_t io_outstanding;
159 boolean_t io_compressed;
160 int io_err;
161 } data;
162 struct srh {
163 uint32_t datablksz;
164 } hole;
165 struct sro {
166 /*
167 * This is a pointer because embedding it in the
168 * struct causes these structures to be massively larger
169 * for all range types; this makes the code much less
170 * memory efficient.
171 */
172 dnode_phys_t *dnp;
173 blkptr_t bp;
174 /* Piggyback unmodified spill block */
175 struct send_range *spill_range;
176 } object;
177 struct srr {
178 uint32_t datablksz;
179 } redact;
180 struct sror {
181 blkptr_t bp;
182 } object_range;
183 } sru;
184 };
185
186 /*
187 * The list of data whose inclusion in a send stream can be pending from
188 * one call to backup_cb to another. Multiple calls to dump_free(),
189 * dump_freeobjects(), and dump_redact() can be aggregated into a single
190 * DRR_FREE, DRR_FREEOBJECTS, or DRR_REDACT replay record.
191 */
192 typedef enum {
193 PENDING_NONE,
194 PENDING_FREE,
195 PENDING_FREEOBJECTS,
196 PENDING_REDACT
197 } dmu_pendop_t;
198
199 typedef struct dmu_send_cookie {
200 dmu_replay_record_t *dsc_drr;
201 dmu_send_outparams_t *dsc_dso;
202 offset_t *dsc_off;
203 objset_t *dsc_os;
204 zio_cksum_t dsc_zc;
205 uint64_t dsc_toguid;
206 uint64_t dsc_fromtxg;
207 int dsc_err;
208 dmu_pendop_t dsc_pending_op;
209 uint64_t dsc_featureflags;
210 uint64_t dsc_last_data_object;
211 uint64_t dsc_last_data_offset;
212 uint64_t dsc_resume_object;
213 uint64_t dsc_resume_offset;
214 boolean_t dsc_sent_begin;
215 boolean_t dsc_sent_end;
216 } dmu_send_cookie_t;
217
218 static int do_dump(dmu_send_cookie_t *dscp, struct send_range *range);
219
220 static void
range_free(struct send_range * range)221 range_free(struct send_range *range)
222 {
223 if (range->type == OBJECT) {
224 size_t size = sizeof (dnode_phys_t) *
225 (range->sru.object.dnp->dn_extra_slots + 1);
226 kmem_free(range->sru.object.dnp, size);
227 if (range->sru.object.spill_range)
228 range_free(range->sru.object.spill_range);
229 } else if (range->type == DATA) {
230 mutex_enter(&range->sru.data.lock);
231 while (range->sru.data.io_outstanding)
232 cv_wait(&range->sru.data.cv, &range->sru.data.lock);
233 if (range->sru.data.abd != NULL)
234 abd_free(range->sru.data.abd);
235 if (range->sru.data.abuf != NULL) {
236 arc_buf_destroy(range->sru.data.abuf,
237 &range->sru.data.abuf);
238 }
239 mutex_exit(&range->sru.data.lock);
240
241 cv_destroy(&range->sru.data.cv);
242 mutex_destroy(&range->sru.data.lock);
243 }
244 kmem_free(range, sizeof (*range));
245 }
246
247 /*
248 * For all record types except BEGIN, fill in the checksum (overlaid in
249 * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
250 * up to the start of the checksum itself.
251 */
252 static int
dump_record(dmu_send_cookie_t * dscp,void * payload,int payload_len)253 dump_record(dmu_send_cookie_t *dscp, void *payload, int payload_len)
254 {
255 dmu_send_outparams_t *dso = dscp->dsc_dso;
256 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
257 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
258 (void) fletcher_4_incremental_native(dscp->dsc_drr,
259 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
260 &dscp->dsc_zc);
261 if (dscp->dsc_drr->drr_type == DRR_BEGIN) {
262 dscp->dsc_sent_begin = B_TRUE;
263 } else {
264 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dscp->dsc_drr->drr_u.
265 drr_checksum.drr_checksum));
266 dscp->dsc_drr->drr_u.drr_checksum.drr_checksum = dscp->dsc_zc;
267 }
268 if (dscp->dsc_drr->drr_type == DRR_END) {
269 dscp->dsc_sent_end = B_TRUE;
270 }
271 (void) fletcher_4_incremental_native(&dscp->dsc_drr->
272 drr_u.drr_checksum.drr_checksum,
273 sizeof (zio_cksum_t), &dscp->dsc_zc);
274 *dscp->dsc_off += sizeof (dmu_replay_record_t);
275 dscp->dsc_err = dso->dso_outfunc(dscp->dsc_os, dscp->dsc_drr,
276 sizeof (dmu_replay_record_t), dso->dso_arg);
277 if (dscp->dsc_err != 0)
278 return (SET_ERROR(EINTR));
279 if (payload_len != 0) {
280 *dscp->dsc_off += payload_len;
281 /*
282 * payload is null when dso_dryrun == B_TRUE (i.e. when we're
283 * doing a send size calculation)
284 */
285 if (payload != NULL) {
286 (void) fletcher_4_incremental_native(
287 payload, payload_len, &dscp->dsc_zc);
288 }
289
290 /*
291 * The code does not rely on this (len being a multiple of 8).
292 * We keep this assertion because of the corresponding assertion
293 * in receive_read(). Keeping this assertion ensures that we do
294 * not inadvertently break backwards compatibility (causing the
295 * assertion in receive_read() to trigger on old software).
296 *
297 * Raw sends cannot be received on old software, and so can
298 * bypass this assertion.
299 */
300
301 ASSERT((payload_len % 8 == 0) ||
302 (dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW));
303
304 dscp->dsc_err = dso->dso_outfunc(dscp->dsc_os, payload,
305 payload_len, dso->dso_arg);
306 if (dscp->dsc_err != 0)
307 return (SET_ERROR(EINTR));
308 }
309 return (0);
310 }
311
312 /*
313 * Fill in the drr_free struct, or perform aggregation if the previous record is
314 * also a free record, and the two are adjacent.
315 *
316 * Note that we send free records even for a full send, because we want to be
317 * able to receive a full send as a clone, which requires a list of all the free
318 * and freeobject records that were generated on the source.
319 */
320 static int
dump_free(dmu_send_cookie_t * dscp,uint64_t object,uint64_t offset,uint64_t length)321 dump_free(dmu_send_cookie_t *dscp, uint64_t object, uint64_t offset,
322 uint64_t length)
323 {
324 struct drr_free *drrf = &(dscp->dsc_drr->drr_u.drr_free);
325
326 /*
327 * When we receive a free record, dbuf_free_range() assumes
328 * that the receiving system doesn't have any dbufs in the range
329 * being freed. This is always true because there is a one-record
330 * constraint: we only send one WRITE record for any given
331 * object,offset. We know that the one-record constraint is
332 * true because we always send data in increasing order by
333 * object,offset.
334 *
335 * If the increasing-order constraint ever changes, we should find
336 * another way to assert that the one-record constraint is still
337 * satisfied.
338 */
339 ASSERT(object > dscp->dsc_last_data_object ||
340 (object == dscp->dsc_last_data_object &&
341 offset > dscp->dsc_last_data_offset));
342
343 /*
344 * If there is a pending op, but it's not PENDING_FREE, push it out,
345 * since free block aggregation can only be done for blocks of the
346 * same type (i.e., DRR_FREE records can only be aggregated with
347 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
348 * aggregated with other DRR_FREEOBJECTS records).
349 */
350 if (dscp->dsc_pending_op != PENDING_NONE &&
351 dscp->dsc_pending_op != PENDING_FREE) {
352 if (dump_record(dscp, NULL, 0) != 0)
353 return (SET_ERROR(EINTR));
354 dscp->dsc_pending_op = PENDING_NONE;
355 }
356
357 if (dscp->dsc_pending_op == PENDING_FREE) {
358 /*
359 * Check to see whether this free block can be aggregated
360 * with pending one.
361 */
362 if (drrf->drr_object == object && drrf->drr_offset +
363 drrf->drr_length == offset) {
364 if (offset + length < offset || length == UINT64_MAX)
365 drrf->drr_length = UINT64_MAX;
366 else
367 drrf->drr_length += length;
368 return (0);
369 } else {
370 /* not a continuation. Push out pending record */
371 if (dump_record(dscp, NULL, 0) != 0)
372 return (SET_ERROR(EINTR));
373 dscp->dsc_pending_op = PENDING_NONE;
374 }
375 }
376 /* create a FREE record and make it pending */
377 memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
378 dscp->dsc_drr->drr_type = DRR_FREE;
379 drrf->drr_object = object;
380 drrf->drr_offset = offset;
381 if (offset + length < offset)
382 drrf->drr_length = DMU_OBJECT_END;
383 else
384 drrf->drr_length = length;
385 drrf->drr_toguid = dscp->dsc_toguid;
386 if (length == DMU_OBJECT_END) {
387 if (dump_record(dscp, NULL, 0) != 0)
388 return (SET_ERROR(EINTR));
389 } else {
390 dscp->dsc_pending_op = PENDING_FREE;
391 }
392
393 return (0);
394 }
395
396 /*
397 * Fill in the drr_redact struct, or perform aggregation if the previous record
398 * is also a redaction record, and the two are adjacent.
399 */
400 static int
dump_redact(dmu_send_cookie_t * dscp,uint64_t object,uint64_t offset,uint64_t length)401 dump_redact(dmu_send_cookie_t *dscp, uint64_t object, uint64_t offset,
402 uint64_t length)
403 {
404 struct drr_redact *drrr = &dscp->dsc_drr->drr_u.drr_redact;
405
406 /*
407 * If there is a pending op, but it's not PENDING_REDACT, push it out,
408 * since free block aggregation can only be done for blocks of the
409 * same type (i.e., DRR_REDACT records can only be aggregated with
410 * other DRR_REDACT records).
411 */
412 if (dscp->dsc_pending_op != PENDING_NONE &&
413 dscp->dsc_pending_op != PENDING_REDACT) {
414 if (dump_record(dscp, NULL, 0) != 0)
415 return (SET_ERROR(EINTR));
416 dscp->dsc_pending_op = PENDING_NONE;
417 }
418
419 if (dscp->dsc_pending_op == PENDING_REDACT) {
420 /*
421 * Check to see whether this redacted block can be aggregated
422 * with pending one.
423 */
424 if (drrr->drr_object == object && drrr->drr_offset +
425 drrr->drr_length == offset) {
426 drrr->drr_length += length;
427 return (0);
428 } else {
429 /* not a continuation. Push out pending record */
430 if (dump_record(dscp, NULL, 0) != 0)
431 return (SET_ERROR(EINTR));
432 dscp->dsc_pending_op = PENDING_NONE;
433 }
434 }
435 /* create a REDACT record and make it pending */
436 memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
437 dscp->dsc_drr->drr_type = DRR_REDACT;
438 drrr->drr_object = object;
439 drrr->drr_offset = offset;
440 drrr->drr_length = length;
441 drrr->drr_toguid = dscp->dsc_toguid;
442 dscp->dsc_pending_op = PENDING_REDACT;
443
444 return (0);
445 }
446
447 static int
dmu_dump_write(dmu_send_cookie_t * dscp,dmu_object_type_t type,uint64_t object,uint64_t offset,int lsize,int psize,const blkptr_t * bp,boolean_t io_compressed,void * data)448 dmu_dump_write(dmu_send_cookie_t *dscp, dmu_object_type_t type, uint64_t object,
449 uint64_t offset, int lsize, int psize, const blkptr_t *bp,
450 boolean_t io_compressed, void *data)
451 {
452 uint64_t payload_size;
453 boolean_t raw = (dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW);
454 struct drr_write *drrw = &(dscp->dsc_drr->drr_u.drr_write);
455
456 /*
457 * We send data in increasing object, offset order.
458 * See comment in dump_free() for details.
459 */
460 ASSERT(object > dscp->dsc_last_data_object ||
461 (object == dscp->dsc_last_data_object &&
462 offset > dscp->dsc_last_data_offset));
463 dscp->dsc_last_data_object = object;
464 dscp->dsc_last_data_offset = offset + lsize - 1;
465
466 /*
467 * If there is any kind of pending aggregation (currently either
468 * a grouping of free objects or free blocks), push it out to
469 * the stream, since aggregation can't be done across operations
470 * of different types.
471 */
472 if (dscp->dsc_pending_op != PENDING_NONE) {
473 if (dump_record(dscp, NULL, 0) != 0)
474 return (SET_ERROR(EINTR));
475 dscp->dsc_pending_op = PENDING_NONE;
476 }
477 /* write a WRITE record */
478 memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
479 dscp->dsc_drr->drr_type = DRR_WRITE;
480 drrw->drr_object = object;
481 drrw->drr_type = type;
482 drrw->drr_offset = offset;
483 drrw->drr_toguid = dscp->dsc_toguid;
484 drrw->drr_logical_size = lsize;
485
486 /* only set the compression fields if the buf is compressed or raw */
487 boolean_t compressed =
488 (bp != NULL ? BP_GET_COMPRESS(bp) != ZIO_COMPRESS_OFF &&
489 io_compressed : lsize != psize);
490 if (raw || compressed) {
491 ASSERT(bp != NULL);
492 ASSERT(raw || dscp->dsc_featureflags &
493 DMU_BACKUP_FEATURE_COMPRESSED);
494 ASSERT(!BP_IS_EMBEDDED(bp));
495 ASSERT3S(psize, >, 0);
496
497 if (raw) {
498 ASSERT(BP_IS_PROTECTED(bp));
499
500 /*
501 * This is a raw protected block so we need to pass
502 * along everything the receiving side will need to
503 * interpret this block, including the byteswap, salt,
504 * IV, and MAC.
505 */
506 if (BP_SHOULD_BYTESWAP(bp))
507 drrw->drr_flags |= DRR_RAW_BYTESWAP;
508 zio_crypt_decode_params_bp(bp, drrw->drr_salt,
509 drrw->drr_iv);
510 zio_crypt_decode_mac_bp(bp, drrw->drr_mac);
511 } else {
512 /* this is a compressed block */
513 ASSERT(dscp->dsc_featureflags &
514 DMU_BACKUP_FEATURE_COMPRESSED);
515 ASSERT(!BP_SHOULD_BYTESWAP(bp));
516 ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)));
517 ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF);
518 ASSERT3S(lsize, >=, psize);
519 }
520
521 /* set fields common to compressed and raw sends */
522 drrw->drr_compressiontype = BP_GET_COMPRESS(bp);
523 drrw->drr_compressed_size = psize;
524 payload_size = drrw->drr_compressed_size;
525 } else {
526 payload_size = drrw->drr_logical_size;
527 }
528
529 if (bp == NULL || BP_IS_EMBEDDED(bp) || (BP_IS_PROTECTED(bp) && !raw)) {
530 /*
531 * There's no pre-computed checksum for partial-block writes,
532 * embedded BP's, or encrypted BP's that are being sent as
533 * plaintext, so (like fletcher4-checksummed blocks) userland
534 * will have to compute a dedup-capable checksum itself.
535 */
536 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
537 } else {
538 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
539 if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
540 ZCHECKSUM_FLAG_DEDUP)
541 drrw->drr_flags |= DRR_CHECKSUM_DEDUP;
542 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
543 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
544 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
545 DDK_SET_CRYPT(&drrw->drr_key, BP_IS_PROTECTED(bp));
546 drrw->drr_key.ddk_cksum = bp->blk_cksum;
547 }
548
549 if (dump_record(dscp, data, payload_size) != 0)
550 return (SET_ERROR(EINTR));
551 return (0);
552 }
553
554 static int
dump_write_embedded(dmu_send_cookie_t * dscp,uint64_t object,uint64_t offset,int blksz,const blkptr_t * bp)555 dump_write_embedded(dmu_send_cookie_t *dscp, uint64_t object, uint64_t offset,
556 int blksz, const blkptr_t *bp)
557 {
558 char buf[BPE_PAYLOAD_SIZE];
559 struct drr_write_embedded *drrw =
560 &(dscp->dsc_drr->drr_u.drr_write_embedded);
561
562 if (dscp->dsc_pending_op != PENDING_NONE) {
563 if (dump_record(dscp, NULL, 0) != 0)
564 return (SET_ERROR(EINTR));
565 dscp->dsc_pending_op = PENDING_NONE;
566 }
567
568 ASSERT(BP_IS_EMBEDDED(bp));
569
570 memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
571 dscp->dsc_drr->drr_type = DRR_WRITE_EMBEDDED;
572 drrw->drr_object = object;
573 drrw->drr_offset = offset;
574 drrw->drr_length = blksz;
575 drrw->drr_toguid = dscp->dsc_toguid;
576 drrw->drr_compression = BP_GET_COMPRESS(bp);
577 drrw->drr_etype = BPE_GET_ETYPE(bp);
578 drrw->drr_lsize = BPE_GET_LSIZE(bp);
579 drrw->drr_psize = BPE_GET_PSIZE(bp);
580
581 decode_embedded_bp_compressed(bp, buf);
582
583 uint32_t psize = drrw->drr_psize;
584 uint32_t rsize = P2ROUNDUP(psize, 8);
585
586 if (psize != rsize)
587 memset(buf + psize, 0, rsize - psize);
588
589 if (dump_record(dscp, buf, rsize) != 0)
590 return (SET_ERROR(EINTR));
591 return (0);
592 }
593
594 static int
dump_spill(dmu_send_cookie_t * dscp,const blkptr_t * bp,uint64_t object,void * data)595 dump_spill(dmu_send_cookie_t *dscp, const blkptr_t *bp, uint64_t object,
596 void *data)
597 {
598 struct drr_spill *drrs = &(dscp->dsc_drr->drr_u.drr_spill);
599 uint64_t blksz = BP_GET_LSIZE(bp);
600 uint64_t payload_size = blksz;
601
602 if (dscp->dsc_pending_op != PENDING_NONE) {
603 if (dump_record(dscp, NULL, 0) != 0)
604 return (SET_ERROR(EINTR));
605 dscp->dsc_pending_op = PENDING_NONE;
606 }
607
608 /* write a SPILL record */
609 memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
610 dscp->dsc_drr->drr_type = DRR_SPILL;
611 drrs->drr_object = object;
612 drrs->drr_length = blksz;
613 drrs->drr_toguid = dscp->dsc_toguid;
614
615 /* See comment in piggyback_unmodified_spill() for full details */
616 if (zfs_send_unmodified_spill_blocks &&
617 (BP_GET_LOGICAL_BIRTH(bp) <= dscp->dsc_fromtxg)) {
618 drrs->drr_flags |= DRR_SPILL_UNMODIFIED;
619 }
620
621 /* handle raw send fields */
622 if (dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW) {
623 ASSERT(BP_IS_PROTECTED(bp));
624
625 if (BP_SHOULD_BYTESWAP(bp))
626 drrs->drr_flags |= DRR_RAW_BYTESWAP;
627 drrs->drr_compressiontype = BP_GET_COMPRESS(bp);
628 drrs->drr_compressed_size = BP_GET_PSIZE(bp);
629 zio_crypt_decode_params_bp(bp, drrs->drr_salt, drrs->drr_iv);
630 zio_crypt_decode_mac_bp(bp, drrs->drr_mac);
631 payload_size = drrs->drr_compressed_size;
632 }
633
634 if (dump_record(dscp, data, payload_size) != 0)
635 return (SET_ERROR(EINTR));
636 return (0);
637 }
638
639 static int
dump_freeobjects(dmu_send_cookie_t * dscp,uint64_t firstobj,uint64_t numobjs)640 dump_freeobjects(dmu_send_cookie_t *dscp, uint64_t firstobj, uint64_t numobjs)
641 {
642 struct drr_freeobjects *drrfo = &(dscp->dsc_drr->drr_u.drr_freeobjects);
643 uint64_t maxobj = DNODES_PER_BLOCK *
644 (DMU_META_DNODE(dscp->dsc_os)->dn_maxblkid + 1);
645
646 /*
647 * ZoL < 0.7 does not handle large FREEOBJECTS records correctly,
648 * leading to zfs recv never completing. to avoid this issue, don't
649 * send FREEOBJECTS records for object IDs which cannot exist on the
650 * receiving side.
651 */
652 if (maxobj > 0) {
653 if (maxobj <= firstobj)
654 return (0);
655
656 if (maxobj < firstobj + numobjs)
657 numobjs = maxobj - firstobj;
658 }
659
660 /*
661 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
662 * push it out, since free block aggregation can only be done for
663 * blocks of the same type (i.e., DRR_FREE records can only be
664 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
665 * can only be aggregated with other DRR_FREEOBJECTS records).
666 */
667 if (dscp->dsc_pending_op != PENDING_NONE &&
668 dscp->dsc_pending_op != PENDING_FREEOBJECTS) {
669 if (dump_record(dscp, NULL, 0) != 0)
670 return (SET_ERROR(EINTR));
671 dscp->dsc_pending_op = PENDING_NONE;
672 }
673
674 if (dscp->dsc_pending_op == PENDING_FREEOBJECTS) {
675 /*
676 * See whether this free object array can be aggregated
677 * with pending one
678 */
679 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
680 drrfo->drr_numobjs += numobjs;
681 return (0);
682 } else {
683 /* can't be aggregated. Push out pending record */
684 if (dump_record(dscp, NULL, 0) != 0)
685 return (SET_ERROR(EINTR));
686 dscp->dsc_pending_op = PENDING_NONE;
687 }
688 }
689
690 /* write a FREEOBJECTS record */
691 memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
692 dscp->dsc_drr->drr_type = DRR_FREEOBJECTS;
693 drrfo->drr_firstobj = firstobj;
694 drrfo->drr_numobjs = numobjs;
695 drrfo->drr_toguid = dscp->dsc_toguid;
696
697 dscp->dsc_pending_op = PENDING_FREEOBJECTS;
698
699 return (0);
700 }
701
702 static int
dump_dnode(dmu_send_cookie_t * dscp,const blkptr_t * bp,uint64_t object,dnode_phys_t * dnp)703 dump_dnode(dmu_send_cookie_t *dscp, const blkptr_t *bp, uint64_t object,
704 dnode_phys_t *dnp)
705 {
706 struct drr_object *drro = &(dscp->dsc_drr->drr_u.drr_object);
707 int bonuslen;
708
709 if (object < dscp->dsc_resume_object) {
710 /*
711 * Note: when resuming, we will visit all the dnodes in
712 * the block of dnodes that we are resuming from. In
713 * this case it's unnecessary to send the dnodes prior to
714 * the one we are resuming from. We should be at most one
715 * block's worth of dnodes behind the resume point.
716 */
717 ASSERT3U(dscp->dsc_resume_object - object, <,
718 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
719 return (0);
720 }
721
722 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
723 return (dump_freeobjects(dscp, object, 1));
724
725 if (dscp->dsc_pending_op != PENDING_NONE) {
726 if (dump_record(dscp, NULL, 0) != 0)
727 return (SET_ERROR(EINTR));
728 dscp->dsc_pending_op = PENDING_NONE;
729 }
730
731 /* write an OBJECT record */
732 memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
733 dscp->dsc_drr->drr_type = DRR_OBJECT;
734 drro->drr_object = object;
735 drro->drr_type = dnp->dn_type;
736 drro->drr_bonustype = dnp->dn_bonustype;
737 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
738 drro->drr_bonuslen = dnp->dn_bonuslen;
739 drro->drr_dn_slots = dnp->dn_extra_slots + 1;
740 drro->drr_checksumtype = dnp->dn_checksum;
741 drro->drr_compress = dnp->dn_compress;
742 drro->drr_toguid = dscp->dsc_toguid;
743
744 if (!(dscp->dsc_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
745 drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
746 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
747
748 bonuslen = P2ROUNDUP(dnp->dn_bonuslen, 8);
749
750 if ((dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW)) {
751 ASSERT(BP_IS_ENCRYPTED(bp));
752
753 if (BP_SHOULD_BYTESWAP(bp))
754 drro->drr_flags |= DRR_RAW_BYTESWAP;
755
756 /* needed for reconstructing dnp on recv side */
757 drro->drr_maxblkid = dnp->dn_maxblkid;
758 drro->drr_indblkshift = dnp->dn_indblkshift;
759 drro->drr_nlevels = dnp->dn_nlevels;
760 drro->drr_nblkptr = dnp->dn_nblkptr;
761
762 /*
763 * Since we encrypt the entire bonus area, the (raw) part
764 * beyond the bonuslen is actually nonzero, so we need
765 * to send it.
766 */
767 if (bonuslen != 0) {
768 if (drro->drr_bonuslen > DN_MAX_BONUS_LEN(dnp))
769 return (SET_ERROR(EINVAL));
770 drro->drr_raw_bonuslen = DN_MAX_BONUS_LEN(dnp);
771 bonuslen = drro->drr_raw_bonuslen;
772 }
773 }
774
775 /*
776 * DRR_OBJECT_SPILL is set for every dnode which references a
777 * spill block. This allows the receiving pool to definitively
778 * determine when a spill block should be kept or freed.
779 */
780 if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR)
781 drro->drr_flags |= DRR_OBJECT_SPILL;
782
783 if (dump_record(dscp, DN_BONUS(dnp), bonuslen) != 0)
784 return (SET_ERROR(EINTR));
785
786 /* Free anything past the end of the file. */
787 if (dump_free(dscp, object, (dnp->dn_maxblkid + 1) *
788 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), DMU_OBJECT_END) != 0)
789 return (SET_ERROR(EINTR));
790
791 if (dscp->dsc_err != 0)
792 return (SET_ERROR(EINTR));
793
794 return (0);
795 }
796
797 static int
dump_object_range(dmu_send_cookie_t * dscp,const blkptr_t * bp,uint64_t firstobj,uint64_t numslots)798 dump_object_range(dmu_send_cookie_t *dscp, const blkptr_t *bp,
799 uint64_t firstobj, uint64_t numslots)
800 {
801 struct drr_object_range *drror =
802 &(dscp->dsc_drr->drr_u.drr_object_range);
803
804 /* we only use this record type for raw sends */
805 ASSERT(BP_IS_PROTECTED(bp));
806 ASSERT(dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW);
807 ASSERT3U(BP_GET_COMPRESS(bp), ==, ZIO_COMPRESS_OFF);
808 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_DNODE);
809 ASSERT0(BP_GET_LEVEL(bp));
810
811 if (dscp->dsc_pending_op != PENDING_NONE) {
812 if (dump_record(dscp, NULL, 0) != 0)
813 return (SET_ERROR(EINTR));
814 dscp->dsc_pending_op = PENDING_NONE;
815 }
816
817 memset(dscp->dsc_drr, 0, sizeof (dmu_replay_record_t));
818 dscp->dsc_drr->drr_type = DRR_OBJECT_RANGE;
819 drror->drr_firstobj = firstobj;
820 drror->drr_numslots = numslots;
821 drror->drr_toguid = dscp->dsc_toguid;
822 if (BP_SHOULD_BYTESWAP(bp))
823 drror->drr_flags |= DRR_RAW_BYTESWAP;
824 zio_crypt_decode_params_bp(bp, drror->drr_salt, drror->drr_iv);
825 zio_crypt_decode_mac_bp(bp, drror->drr_mac);
826
827 if (dump_record(dscp, NULL, 0) != 0)
828 return (SET_ERROR(EINTR));
829 return (0);
830 }
831
832 static boolean_t
send_do_embed(const blkptr_t * bp,uint64_t featureflags)833 send_do_embed(const blkptr_t *bp, uint64_t featureflags)
834 {
835 if (!BP_IS_EMBEDDED(bp))
836 return (B_FALSE);
837
838 /*
839 * Compression function must be legacy, or explicitly enabled.
840 */
841 if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
842 !(featureflags & DMU_BACKUP_FEATURE_LZ4)))
843 return (B_FALSE);
844
845 /*
846 * If we have not set the ZSTD feature flag, we can't send ZSTD
847 * compressed embedded blocks, as the receiver may not support them.
848 */
849 if ((BP_GET_COMPRESS(bp) == ZIO_COMPRESS_ZSTD &&
850 !(featureflags & DMU_BACKUP_FEATURE_ZSTD)))
851 return (B_FALSE);
852
853 /*
854 * Embed type must be explicitly enabled.
855 */
856 switch (BPE_GET_ETYPE(bp)) {
857 case BP_EMBEDDED_TYPE_DATA:
858 if (featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
859 return (B_TRUE);
860 break;
861 default:
862 return (B_FALSE);
863 }
864 return (B_FALSE);
865 }
866
867 /*
868 * This function actually handles figuring out what kind of record needs to be
869 * dumped, and calling the appropriate helper function. In most cases,
870 * the data has already been read by send_reader_thread().
871 */
872 static int
do_dump(dmu_send_cookie_t * dscp,struct send_range * range)873 do_dump(dmu_send_cookie_t *dscp, struct send_range *range)
874 {
875 int err = 0;
876 switch (range->type) {
877 case OBJECT:
878 err = dump_dnode(dscp, &range->sru.object.bp, range->object,
879 range->sru.object.dnp);
880 /* Dump piggybacked unmodified spill block */
881 if (!err && range->sru.object.spill_range)
882 err = do_dump(dscp, range->sru.object.spill_range);
883 return (err);
884 case OBJECT_RANGE: {
885 ASSERT3U(range->start_blkid + 1, ==, range->end_blkid);
886 if (!(dscp->dsc_featureflags & DMU_BACKUP_FEATURE_RAW)) {
887 return (0);
888 }
889 uint64_t epb = BP_GET_LSIZE(&range->sru.object_range.bp) >>
890 DNODE_SHIFT;
891 uint64_t firstobj = range->start_blkid * epb;
892 err = dump_object_range(dscp, &range->sru.object_range.bp,
893 firstobj, epb);
894 break;
895 }
896 case REDACT: {
897 struct srr *srrp = &range->sru.redact;
898 err = dump_redact(dscp, range->object, range->start_blkid *
899 srrp->datablksz, (range->end_blkid - range->start_blkid) *
900 srrp->datablksz);
901 return (err);
902 }
903 case DATA: {
904 struct srd *srdp = &range->sru.data;
905 blkptr_t *bp = &srdp->bp;
906 spa_t *spa =
907 dmu_objset_spa(dscp->dsc_os);
908
909 ASSERT3U(srdp->datablksz, ==, BP_GET_LSIZE(bp));
910 ASSERT3U(range->start_blkid + 1, ==, range->end_blkid);
911
912 if (send_do_embed(bp, dscp->dsc_featureflags)) {
913 err = dump_write_embedded(dscp, range->object,
914 range->start_blkid * srdp->datablksz,
915 srdp->datablksz, bp);
916 return (err);
917 }
918 ASSERT(range->object > dscp->dsc_resume_object ||
919 (range->object == dscp->dsc_resume_object &&
920 (range->start_blkid == DMU_SPILL_BLKID ||
921 range->start_blkid * srdp->datablksz >=
922 dscp->dsc_resume_offset)));
923 /* it's a level-0 block of a regular object */
924
925 mutex_enter(&srdp->lock);
926 while (srdp->io_outstanding)
927 cv_wait(&srdp->cv, &srdp->lock);
928 err = srdp->io_err;
929 mutex_exit(&srdp->lock);
930
931 if (err != 0) {
932 if (zfs_send_corrupt_data &&
933 !dscp->dsc_dso->dso_dryrun) {
934 /*
935 * Send a block filled with 0x"zfs badd bloc"
936 */
937 srdp->abuf = arc_alloc_buf(spa, &srdp->abuf,
938 ARC_BUFC_DATA, srdp->datablksz);
939 uint64_t *ptr;
940 for (ptr = srdp->abuf->b_data;
941 (char *)ptr < (char *)srdp->abuf->b_data +
942 srdp->datablksz; ptr++)
943 *ptr = 0x2f5baddb10cULL;
944 } else {
945 return (SET_ERROR(EIO));
946 }
947 }
948
949 ASSERT(dscp->dsc_dso->dso_dryrun ||
950 srdp->abuf != NULL || srdp->abd != NULL);
951
952 char *data = NULL;
953 if (srdp->abd != NULL) {
954 data = abd_to_buf(srdp->abd);
955 ASSERT0P(srdp->abuf);
956 } else if (srdp->abuf != NULL) {
957 data = srdp->abuf->b_data;
958 }
959
960 if (BP_GET_TYPE(bp) == DMU_OT_SA) {
961 ASSERT3U(range->start_blkid, ==, DMU_SPILL_BLKID);
962 err = dump_spill(dscp, bp, range->object, data);
963 return (err);
964 }
965
966 uint64_t offset = range->start_blkid * srdp->datablksz;
967
968 /*
969 * If we have large blocks stored on disk but the send flags
970 * don't allow us to send large blocks, we split the data from
971 * the arc buf into chunks.
972 */
973 if (srdp->datablksz > SPA_OLD_MAXBLOCKSIZE &&
974 !(dscp->dsc_featureflags &
975 DMU_BACKUP_FEATURE_LARGE_BLOCKS)) {
976 while (srdp->datablksz > 0 && err == 0) {
977 int n = MIN(srdp->datablksz,
978 SPA_OLD_MAXBLOCKSIZE);
979 err = dmu_dump_write(dscp, srdp->obj_type,
980 range->object, offset, n, n, NULL, B_FALSE,
981 data);
982 offset += n;
983 /*
984 * When doing dry run, data==NULL is used as a
985 * sentinel value by
986 * dmu_dump_write()->dump_record().
987 */
988 if (data != NULL)
989 data += n;
990 srdp->datablksz -= n;
991 }
992 } else {
993 err = dmu_dump_write(dscp, srdp->obj_type,
994 range->object, offset,
995 srdp->datablksz, srdp->datasz, bp,
996 srdp->io_compressed, data);
997 }
998 return (err);
999 }
1000 case HOLE: {
1001 struct srh *srhp = &range->sru.hole;
1002 if (range->object == DMU_META_DNODE_OBJECT) {
1003 uint32_t span = srhp->datablksz >> DNODE_SHIFT;
1004 uint64_t first_obj = range->start_blkid * span;
1005 uint64_t numobj = range->end_blkid * span - first_obj;
1006 return (dump_freeobjects(dscp, first_obj, numobj));
1007 }
1008 uint64_t offset = 0;
1009
1010 /*
1011 * If this multiply overflows, we don't need to send this block.
1012 * Even if it has a birth time, it can never not be a hole, so
1013 * we don't need to send records for it.
1014 */
1015 if (!overflow_multiply(range->start_blkid, srhp->datablksz,
1016 &offset)) {
1017 return (0);
1018 }
1019 uint64_t len = 0;
1020
1021 if (!overflow_multiply(range->end_blkid, srhp->datablksz, &len))
1022 len = UINT64_MAX;
1023 len = len - offset;
1024 return (dump_free(dscp, range->object, offset, len));
1025 }
1026 default:
1027 panic("Invalid range type in do_dump: %d", range->type);
1028 }
1029 return (err);
1030 }
1031
1032 static struct send_range *
range_alloc(enum type type,uint64_t object,uint64_t start_blkid,uint64_t end_blkid,boolean_t eos)1033 range_alloc(enum type type, uint64_t object, uint64_t start_blkid,
1034 uint64_t end_blkid, boolean_t eos)
1035 {
1036 struct send_range *range = kmem_alloc(sizeof (*range), KM_SLEEP);
1037 range->type = type;
1038 range->object = object;
1039 range->start_blkid = start_blkid;
1040 range->end_blkid = end_blkid;
1041 range->eos_marker = eos;
1042 if (type == DATA) {
1043 range->sru.data.abd = NULL;
1044 range->sru.data.abuf = NULL;
1045 mutex_init(&range->sru.data.lock, NULL, MUTEX_DEFAULT, NULL);
1046 cv_init(&range->sru.data.cv, NULL, CV_DEFAULT, NULL);
1047 range->sru.data.io_outstanding = 0;
1048 range->sru.data.io_err = 0;
1049 range->sru.data.io_compressed = B_FALSE;
1050 } else if (type == OBJECT) {
1051 range->sru.object.spill_range = NULL;
1052 }
1053 return (range);
1054 }
1055
1056 /*
1057 * This is the callback function to traverse_dataset that acts as a worker
1058 * thread for dmu_send_impl.
1059 */
1060 static int
send_cb(spa_t * spa,zilog_t * zilog,const blkptr_t * bp,const zbookmark_phys_t * zb,const struct dnode_phys * dnp,void * arg)1061 send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1062 const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
1063 {
1064 (void) zilog;
1065 struct send_thread_arg *sta = arg;
1066 struct send_range *record;
1067
1068 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
1069 zb->zb_object >= sta->resume.zb_object);
1070
1071 /*
1072 * All bps of an encrypted os should have the encryption bit set.
1073 * If this is not true it indicates tampering and we report an error.
1074 */
1075 if (sta->os->os_encrypted &&
1076 !BP_IS_HOLE(bp) && !BP_USES_CRYPT(bp)) {
1077 spa_log_error(spa, zb, BP_GET_PHYSICAL_BIRTH(bp));
1078 return (SET_ERROR(EIO));
1079 }
1080
1081 if (sta->cancel)
1082 return (SET_ERROR(EINTR));
1083 if (zb->zb_object != DMU_META_DNODE_OBJECT &&
1084 DMU_OBJECT_IS_SPECIAL(zb->zb_object))
1085 return (0);
1086 atomic_inc_64(sta->num_blocks_visited);
1087
1088 if (zb->zb_level == ZB_DNODE_LEVEL) {
1089 if (zb->zb_object == DMU_META_DNODE_OBJECT)
1090 return (0);
1091 record = range_alloc(OBJECT, zb->zb_object, 0, 0, B_FALSE);
1092 record->sru.object.bp = *bp;
1093 size_t size = sizeof (*dnp) * (dnp->dn_extra_slots + 1);
1094 record->sru.object.dnp = kmem_alloc(size, KM_SLEEP);
1095 memcpy(record->sru.object.dnp, dnp, size);
1096 bqueue_enqueue(&sta->q, record, sizeof (*record));
1097 return (0);
1098 }
1099 if (zb->zb_level == 0 && zb->zb_object == DMU_META_DNODE_OBJECT &&
1100 !BP_IS_HOLE(bp)) {
1101 record = range_alloc(OBJECT_RANGE, 0, zb->zb_blkid,
1102 zb->zb_blkid + 1, B_FALSE);
1103 record->sru.object_range.bp = *bp;
1104 bqueue_enqueue(&sta->q, record, sizeof (*record));
1105 return (0);
1106 }
1107 if (zb->zb_level < 0 || (zb->zb_level > 0 && !BP_IS_HOLE(bp)))
1108 return (0);
1109 if (zb->zb_object == DMU_META_DNODE_OBJECT && !BP_IS_HOLE(bp))
1110 return (0);
1111
1112 uint64_t span = bp_span_in_blocks(dnp->dn_indblkshift, zb->zb_level);
1113 uint64_t start;
1114
1115 /*
1116 * If this multiply overflows, we don't need to send this block.
1117 * Even if it has a birth time, it can never not be a hole, so
1118 * we don't need to send records for it.
1119 */
1120 if (!overflow_multiply(span, zb->zb_blkid, &start) || (!(zb->zb_blkid ==
1121 DMU_SPILL_BLKID || DMU_OT_IS_METADATA(dnp->dn_type)) &&
1122 span * zb->zb_blkid > dnp->dn_maxblkid)) {
1123 ASSERT(BP_IS_HOLE(bp));
1124 return (0);
1125 }
1126
1127 if (zb->zb_blkid == DMU_SPILL_BLKID)
1128 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_SA);
1129
1130 enum type record_type = DATA;
1131 if (BP_IS_HOLE(bp))
1132 record_type = HOLE;
1133 else if (BP_IS_REDACTED(bp))
1134 record_type = REDACT;
1135 else
1136 record_type = DATA;
1137
1138 record = range_alloc(record_type, zb->zb_object, start,
1139 (start + span < start ? 0 : start + span), B_FALSE);
1140
1141 uint64_t datablksz = (zb->zb_blkid == DMU_SPILL_BLKID ?
1142 BP_GET_LSIZE(bp) : dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT);
1143
1144 if (BP_IS_HOLE(bp)) {
1145 record->sru.hole.datablksz = datablksz;
1146 } else if (BP_IS_REDACTED(bp)) {
1147 record->sru.redact.datablksz = datablksz;
1148 } else {
1149 record->sru.data.datablksz = datablksz;
1150 record->sru.data.obj_type = dnp->dn_type;
1151 record->sru.data.bp = *bp;
1152 }
1153
1154 bqueue_enqueue(&sta->q, record, sizeof (*record));
1155 return (0);
1156 }
1157
1158 struct redact_list_cb_arg {
1159 uint64_t *num_blocks_visited;
1160 bqueue_t *q;
1161 boolean_t *cancel;
1162 boolean_t mark_redact;
1163 };
1164
1165 static int
redact_list_cb(redact_block_phys_t * rb,void * arg)1166 redact_list_cb(redact_block_phys_t *rb, void *arg)
1167 {
1168 struct redact_list_cb_arg *rlcap = arg;
1169
1170 atomic_inc_64(rlcap->num_blocks_visited);
1171 if (*rlcap->cancel)
1172 return (-1);
1173
1174 struct send_range *data = range_alloc(REDACT, rb->rbp_object,
1175 rb->rbp_blkid, rb->rbp_blkid + redact_block_get_count(rb), B_FALSE);
1176 ASSERT3U(data->end_blkid, >, rb->rbp_blkid);
1177 if (rlcap->mark_redact) {
1178 data->type = REDACT;
1179 data->sru.redact.datablksz = redact_block_get_size(rb);
1180 } else {
1181 data->type = PREVIOUSLY_REDACTED;
1182 }
1183 bqueue_enqueue(rlcap->q, data, sizeof (*data));
1184
1185 return (0);
1186 }
1187
1188 /*
1189 * This function kicks off the traverse_dataset. It also handles setting the
1190 * error code of the thread in case something goes wrong, and pushes the End of
1191 * Stream record when the traverse_dataset call has finished.
1192 */
1193 static __attribute__((noreturn)) void
send_traverse_thread(void * arg)1194 send_traverse_thread(void *arg)
1195 {
1196 struct send_thread_arg *st_arg = arg;
1197 int err = 0;
1198 struct send_range *data;
1199 fstrans_cookie_t cookie = spl_fstrans_mark();
1200
1201 err = traverse_dataset_resume(st_arg->os->os_dsl_dataset,
1202 st_arg->fromtxg, &st_arg->resume,
1203 st_arg->flags | TRAVERSE_LOGICAL, send_cb, st_arg);
1204
1205 if (err != EINTR)
1206 st_arg->error_code = err;
1207 data = range_alloc(DATA, 0, 0, 0, B_TRUE);
1208 bqueue_enqueue_flush(&st_arg->q, data, sizeof (*data));
1209 spl_fstrans_unmark(cookie);
1210 thread_exit();
1211 }
1212
1213 /*
1214 * Utility function that causes End of Stream records to compare after of all
1215 * others, so that other threads' comparison logic can stay simple.
1216 */
1217 static int __attribute__((unused))
send_range_after(const struct send_range * from,const struct send_range * to)1218 send_range_after(const struct send_range *from, const struct send_range *to)
1219 {
1220 if (from->eos_marker == B_TRUE)
1221 return (1);
1222 if (to->eos_marker == B_TRUE)
1223 return (-1);
1224
1225 uint64_t from_obj = from->object;
1226 uint64_t from_end_obj = from->object + 1;
1227 uint64_t to_obj = to->object;
1228 uint64_t to_end_obj = to->object + 1;
1229 if (from_obj == 0) {
1230 ASSERT(from->type == HOLE || from->type == OBJECT_RANGE);
1231 from_obj = from->start_blkid << DNODES_PER_BLOCK_SHIFT;
1232 from_end_obj = from->end_blkid << DNODES_PER_BLOCK_SHIFT;
1233 }
1234 if (to_obj == 0) {
1235 ASSERT(to->type == HOLE || to->type == OBJECT_RANGE);
1236 to_obj = to->start_blkid << DNODES_PER_BLOCK_SHIFT;
1237 to_end_obj = to->end_blkid << DNODES_PER_BLOCK_SHIFT;
1238 }
1239
1240 if (from_end_obj <= to_obj)
1241 return (-1);
1242 if (from_obj >= to_end_obj)
1243 return (1);
1244 int64_t cmp = TREE_CMP(to->type == OBJECT_RANGE, from->type ==
1245 OBJECT_RANGE);
1246 if (unlikely(cmp))
1247 return (cmp);
1248 cmp = TREE_CMP(to->type == OBJECT, from->type == OBJECT);
1249 if (unlikely(cmp))
1250 return (cmp);
1251 if (from->end_blkid <= to->start_blkid)
1252 return (-1);
1253 if (from->start_blkid >= to->end_blkid)
1254 return (1);
1255 return (0);
1256 }
1257
1258 /*
1259 * Pop the new data off the queue, check that the records we receive are in
1260 * the right order, but do not free the old data. This is used so that the
1261 * records can be sent on to the main thread without copying the data.
1262 */
1263 static struct send_range *
get_next_range_nofree(bqueue_t * bq,struct send_range * prev)1264 get_next_range_nofree(bqueue_t *bq, struct send_range *prev)
1265 {
1266 struct send_range *next = bqueue_dequeue(bq);
1267 ASSERT3S(send_range_after(prev, next), ==, -1);
1268 return (next);
1269 }
1270
1271 /*
1272 * Pop the new data off the queue, check that the records we receive are in
1273 * the right order, and free the old data.
1274 */
1275 static struct send_range *
get_next_range(bqueue_t * bq,struct send_range * prev)1276 get_next_range(bqueue_t *bq, struct send_range *prev)
1277 {
1278 struct send_range *next = get_next_range_nofree(bq, prev);
1279 range_free(prev);
1280 return (next);
1281 }
1282
1283 static __attribute__((noreturn)) void
redact_list_thread(void * arg)1284 redact_list_thread(void *arg)
1285 {
1286 struct redact_list_thread_arg *rlt_arg = arg;
1287 struct send_range *record;
1288 fstrans_cookie_t cookie = spl_fstrans_mark();
1289 if (rlt_arg->rl != NULL) {
1290 struct redact_list_cb_arg rlcba = {0};
1291 rlcba.cancel = &rlt_arg->cancel;
1292 rlcba.q = &rlt_arg->q;
1293 rlcba.num_blocks_visited = rlt_arg->num_blocks_visited;
1294 rlcba.mark_redact = rlt_arg->mark_redact;
1295 int err = dsl_redaction_list_traverse(rlt_arg->rl,
1296 &rlt_arg->resume, redact_list_cb, &rlcba);
1297 if (err != EINTR)
1298 rlt_arg->error_code = err;
1299 }
1300 record = range_alloc(DATA, 0, 0, 0, B_TRUE);
1301 bqueue_enqueue_flush(&rlt_arg->q, record, sizeof (*record));
1302 spl_fstrans_unmark(cookie);
1303
1304 thread_exit();
1305 }
1306
1307 /*
1308 * Compare the start point of the two provided ranges. End of stream ranges
1309 * compare last, objects compare before any data or hole inside that object and
1310 * multi-object holes that start at the same object.
1311 */
1312 static int
send_range_start_compare(struct send_range * r1,struct send_range * r2)1313 send_range_start_compare(struct send_range *r1, struct send_range *r2)
1314 {
1315 uint64_t r1_objequiv = r1->object;
1316 uint64_t r1_l0equiv = r1->start_blkid;
1317 uint64_t r2_objequiv = r2->object;
1318 uint64_t r2_l0equiv = r2->start_blkid;
1319 int64_t cmp = TREE_CMP(r1->eos_marker, r2->eos_marker);
1320 if (unlikely(cmp))
1321 return (cmp);
1322 if (r1->object == 0) {
1323 r1_objequiv = r1->start_blkid * DNODES_PER_BLOCK;
1324 r1_l0equiv = 0;
1325 }
1326 if (r2->object == 0) {
1327 r2_objequiv = r2->start_blkid * DNODES_PER_BLOCK;
1328 r2_l0equiv = 0;
1329 }
1330
1331 cmp = TREE_CMP(r1_objequiv, r2_objequiv);
1332 if (likely(cmp))
1333 return (cmp);
1334 cmp = TREE_CMP(r2->type == OBJECT_RANGE, r1->type == OBJECT_RANGE);
1335 if (unlikely(cmp))
1336 return (cmp);
1337 cmp = TREE_CMP(r2->type == OBJECT, r1->type == OBJECT);
1338 if (unlikely(cmp))
1339 return (cmp);
1340
1341 return (TREE_CMP(r1_l0equiv, r2_l0equiv));
1342 }
1343
1344 enum q_idx {
1345 REDACT_IDX = 0,
1346 TO_IDX,
1347 FROM_IDX,
1348 NUM_THREADS
1349 };
1350
1351 /*
1352 * This function returns the next range the send_merge_thread should operate on.
1353 * The inputs are two arrays; the first one stores the range at the front of the
1354 * queues stored in the second one. The ranges are sorted in descending
1355 * priority order; the metadata from earlier ranges overrules metadata from
1356 * later ranges. out_mask is used to return which threads the ranges came from;
1357 * bit i is set if ranges[i] started at the same place as the returned range.
1358 *
1359 * This code is not hardcoded to compare a specific number of threads; it could
1360 * be used with any number, just by changing the q_idx enum.
1361 *
1362 * The "next range" is the one with the earliest start; if two starts are equal,
1363 * the highest-priority range is the next to operate on. If a higher-priority
1364 * range starts in the middle of the first range, then the first range will be
1365 * truncated to end where the higher-priority range starts, and we will operate
1366 * on that one next time. In this way, we make sure that each block covered by
1367 * some range gets covered by a returned range, and each block covered is
1368 * returned using the metadata of the highest-priority range it appears in.
1369 *
1370 * For example, if the three ranges at the front of the queues were [2,4),
1371 * [3,5), and [1,3), then the ranges returned would be [1,2) with the metadata
1372 * from the third range, [2,4) with the metadata from the first range, and then
1373 * [4,5) with the metadata from the second.
1374 */
1375 static struct send_range *
find_next_range(struct send_range ** ranges,bqueue_t ** qs,uint64_t * out_mask)1376 find_next_range(struct send_range **ranges, bqueue_t **qs, uint64_t *out_mask)
1377 {
1378 int idx = 0; // index of the range with the earliest start
1379 int i;
1380 uint64_t bmask = 0;
1381 for (i = 1; i < NUM_THREADS; i++) {
1382 if (send_range_start_compare(ranges[i], ranges[idx]) < 0)
1383 idx = i;
1384 }
1385 if (ranges[idx]->eos_marker) {
1386 struct send_range *ret = range_alloc(DATA, 0, 0, 0, B_TRUE);
1387 *out_mask = 0;
1388 return (ret);
1389 }
1390 /*
1391 * Find all the ranges that start at that same point.
1392 */
1393 for (i = 0; i < NUM_THREADS; i++) {
1394 if (send_range_start_compare(ranges[i], ranges[idx]) == 0)
1395 bmask |= 1 << i;
1396 }
1397 *out_mask = bmask;
1398 /*
1399 * OBJECT_RANGE records only come from the TO thread, and should always
1400 * be treated as overlapping with nothing and sent on immediately. They
1401 * are only used in raw sends, and are never redacted.
1402 */
1403 if (ranges[idx]->type == OBJECT_RANGE) {
1404 ASSERT3U(idx, ==, TO_IDX);
1405 ASSERT3U(*out_mask, ==, 1 << TO_IDX);
1406 struct send_range *ret = ranges[idx];
1407 ranges[idx] = get_next_range_nofree(qs[idx], ranges[idx]);
1408 return (ret);
1409 }
1410 /*
1411 * Find the first start or end point after the start of the first range.
1412 */
1413 uint64_t first_change = ranges[idx]->end_blkid;
1414 for (i = 0; i < NUM_THREADS; i++) {
1415 if (i == idx || ranges[i]->eos_marker ||
1416 ranges[i]->object > ranges[idx]->object ||
1417 ranges[i]->object == DMU_META_DNODE_OBJECT)
1418 continue;
1419 ASSERT3U(ranges[i]->object, ==, ranges[idx]->object);
1420 if (first_change > ranges[i]->start_blkid &&
1421 (bmask & (1 << i)) == 0)
1422 first_change = ranges[i]->start_blkid;
1423 else if (first_change > ranges[i]->end_blkid)
1424 first_change = ranges[i]->end_blkid;
1425 }
1426 /*
1427 * Update all ranges to no longer overlap with the range we're
1428 * returning. All such ranges must start at the same place as the range
1429 * being returned, and end at or after first_change. Thus we update
1430 * their start to first_change. If that makes them size 0, then free
1431 * them and pull a new range from that thread.
1432 */
1433 for (i = 0; i < NUM_THREADS; i++) {
1434 if (i == idx || (bmask & (1 << i)) == 0)
1435 continue;
1436 ASSERT3U(first_change, >, ranges[i]->start_blkid);
1437 ranges[i]->start_blkid = first_change;
1438 ASSERT3U(ranges[i]->start_blkid, <=, ranges[i]->end_blkid);
1439 if (ranges[i]->start_blkid == ranges[i]->end_blkid)
1440 ranges[i] = get_next_range(qs[i], ranges[i]);
1441 }
1442 /*
1443 * Short-circuit the simple case; if the range doesn't overlap with
1444 * anything else, or it only overlaps with things that start at the same
1445 * place and are longer, send it on.
1446 */
1447 if (first_change == ranges[idx]->end_blkid) {
1448 struct send_range *ret = ranges[idx];
1449 ranges[idx] = get_next_range_nofree(qs[idx], ranges[idx]);
1450 return (ret);
1451 }
1452
1453 /*
1454 * Otherwise, return a truncated copy of ranges[idx] and move the start
1455 * of ranges[idx] back to first_change.
1456 */
1457 struct send_range *ret = kmem_alloc(sizeof (*ret), KM_SLEEP);
1458 *ret = *ranges[idx];
1459 ret->end_blkid = first_change;
1460 ranges[idx]->start_blkid = first_change;
1461 return (ret);
1462 }
1463
1464 #define FROM_AND_REDACT_BITS ((1 << REDACT_IDX) | (1 << FROM_IDX))
1465
1466 /*
1467 * Merge the results from the from thread and the to thread, and then hand the
1468 * records off to send_prefetch_thread to prefetch them. If this is not a
1469 * send from a redaction bookmark, the from thread will push an end of stream
1470 * record and stop, and we'll just send everything that was changed in the
1471 * to_ds since the ancestor's creation txg. If it is, then since
1472 * traverse_dataset has a canonical order, we can compare each change as
1473 * they're pulled off the queues. That will give us a stream that is
1474 * appropriately sorted, and covers all records. In addition, we pull the
1475 * data from the redact_list_thread and use that to determine which blocks
1476 * should be redacted.
1477 */
1478 static __attribute__((noreturn)) void
send_merge_thread(void * arg)1479 send_merge_thread(void *arg)
1480 {
1481 struct send_merge_thread_arg *smt_arg = arg;
1482 struct send_range *front_ranges[NUM_THREADS];
1483 bqueue_t *queues[NUM_THREADS];
1484 int err = 0;
1485 fstrans_cookie_t cookie = spl_fstrans_mark();
1486
1487 if (smt_arg->redact_arg == NULL) {
1488 front_ranges[REDACT_IDX] =
1489 kmem_zalloc(sizeof (struct send_range), KM_SLEEP);
1490 front_ranges[REDACT_IDX]->eos_marker = B_TRUE;
1491 front_ranges[REDACT_IDX]->type = REDACT;
1492 queues[REDACT_IDX] = NULL;
1493 } else {
1494 front_ranges[REDACT_IDX] =
1495 bqueue_dequeue(&smt_arg->redact_arg->q);
1496 queues[REDACT_IDX] = &smt_arg->redact_arg->q;
1497 }
1498 front_ranges[TO_IDX] = bqueue_dequeue(&smt_arg->to_arg->q);
1499 queues[TO_IDX] = &smt_arg->to_arg->q;
1500 front_ranges[FROM_IDX] = bqueue_dequeue(&smt_arg->from_arg->q);
1501 queues[FROM_IDX] = &smt_arg->from_arg->q;
1502 uint64_t mask = 0;
1503 struct send_range *range;
1504 for (range = find_next_range(front_ranges, queues, &mask);
1505 !range->eos_marker && err == 0 && !smt_arg->cancel;
1506 range = find_next_range(front_ranges, queues, &mask)) {
1507 /*
1508 * If the range in question was in both the from redact bookmark
1509 * and the bookmark we're using to redact, then don't send it.
1510 * It's already redacted on the receiving system, so a redaction
1511 * record would be redundant.
1512 */
1513 if ((mask & FROM_AND_REDACT_BITS) == FROM_AND_REDACT_BITS) {
1514 ASSERT3U(range->type, ==, REDACT);
1515 range_free(range);
1516 continue;
1517 }
1518 bqueue_enqueue(&smt_arg->q, range, sizeof (*range));
1519
1520 if (smt_arg->to_arg->error_code != 0) {
1521 err = smt_arg->to_arg->error_code;
1522 } else if (smt_arg->from_arg->error_code != 0) {
1523 err = smt_arg->from_arg->error_code;
1524 } else if (smt_arg->redact_arg != NULL &&
1525 smt_arg->redact_arg->error_code != 0) {
1526 err = smt_arg->redact_arg->error_code;
1527 }
1528 }
1529 if (smt_arg->cancel && err == 0)
1530 err = SET_ERROR(EINTR);
1531 smt_arg->error = err;
1532 if (smt_arg->error != 0) {
1533 smt_arg->to_arg->cancel = B_TRUE;
1534 smt_arg->from_arg->cancel = B_TRUE;
1535 if (smt_arg->redact_arg != NULL)
1536 smt_arg->redact_arg->cancel = B_TRUE;
1537 }
1538 for (int i = 0; i < NUM_THREADS; i++) {
1539 while (!front_ranges[i]->eos_marker) {
1540 front_ranges[i] = get_next_range(queues[i],
1541 front_ranges[i]);
1542 }
1543 range_free(front_ranges[i]);
1544 }
1545 range->eos_marker = B_TRUE;
1546 bqueue_enqueue_flush(&smt_arg->q, range, 1);
1547 spl_fstrans_unmark(cookie);
1548 thread_exit();
1549 }
1550
1551 struct send_reader_thread_arg {
1552 struct send_merge_thread_arg *smta;
1553 bqueue_t q;
1554 boolean_t cancel;
1555 boolean_t issue_reads;
1556 uint64_t featureflags;
1557 int error;
1558 };
1559
1560 static void
dmu_send_read_done(zio_t * zio)1561 dmu_send_read_done(zio_t *zio)
1562 {
1563 struct send_range *range = zio->io_private;
1564
1565 mutex_enter(&range->sru.data.lock);
1566 if (zio->io_error != 0) {
1567 abd_free(range->sru.data.abd);
1568 range->sru.data.abd = NULL;
1569 range->sru.data.io_err = zio->io_error;
1570 }
1571
1572 ASSERT(range->sru.data.io_outstanding);
1573 range->sru.data.io_outstanding = B_FALSE;
1574 cv_broadcast(&range->sru.data.cv);
1575 mutex_exit(&range->sru.data.lock);
1576 }
1577
1578 static void
issue_data_read(struct send_reader_thread_arg * srta,struct send_range * range)1579 issue_data_read(struct send_reader_thread_arg *srta, struct send_range *range)
1580 {
1581 struct srd *srdp = &range->sru.data;
1582 blkptr_t *bp = &srdp->bp;
1583 objset_t *os = srta->smta->os;
1584
1585 ASSERT3U(range->type, ==, DATA);
1586 ASSERT3U(range->start_blkid + 1, ==, range->end_blkid);
1587 /*
1588 * If we have large blocks stored on disk but
1589 * the send flags don't allow us to send large
1590 * blocks, we split the data from the arc buf
1591 * into chunks.
1592 */
1593 boolean_t split_large_blocks =
1594 srdp->datablksz > SPA_OLD_MAXBLOCKSIZE &&
1595 !(srta->featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS);
1596 /*
1597 * We should only request compressed data from the ARC if all
1598 * the following are true:
1599 * - stream compression was requested
1600 * - we aren't splitting large blocks into smaller chunks
1601 * - the data won't need to be byteswapped before sending
1602 * - this isn't an embedded block
1603 * - this isn't metadata (if receiving on a different endian
1604 * system it can be byteswapped more easily)
1605 */
1606 boolean_t request_compressed =
1607 (srta->featureflags & DMU_BACKUP_FEATURE_COMPRESSED) &&
1608 !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) &&
1609 !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp));
1610
1611 zio_flag_t zioflags = ZIO_FLAG_CANFAIL;
1612
1613 if (srta->featureflags & DMU_BACKUP_FEATURE_RAW) {
1614 zioflags |= ZIO_FLAG_RAW;
1615 srdp->io_compressed = B_TRUE;
1616 } else if (request_compressed) {
1617 zioflags |= ZIO_FLAG_RAW_COMPRESS;
1618 srdp->io_compressed = B_TRUE;
1619 }
1620
1621 srdp->datasz = (zioflags & ZIO_FLAG_RAW_COMPRESS) ?
1622 BP_GET_PSIZE(bp) : BP_GET_LSIZE(bp);
1623
1624 if (!srta->issue_reads)
1625 return;
1626 if (BP_IS_REDACTED(bp))
1627 return;
1628 if (send_do_embed(bp, srta->featureflags))
1629 return;
1630
1631 zbookmark_phys_t zb = {
1632 .zb_objset = dmu_objset_id(os),
1633 .zb_object = range->object,
1634 .zb_level = 0,
1635 .zb_blkid = range->start_blkid,
1636 };
1637
1638 arc_flags_t aflags = ARC_FLAG_CACHED_ONLY;
1639
1640 int arc_err = arc_read(NULL, os->os_spa, bp,
1641 arc_getbuf_func, &srdp->abuf, ZIO_PRIORITY_ASYNC_READ,
1642 zioflags, &aflags, &zb);
1643 /*
1644 * If the data is not already cached in the ARC, we read directly
1645 * from zio. This avoids the performance overhead of adding a new
1646 * entry to the ARC, and we also avoid polluting the ARC cache with
1647 * data that is not likely to be used in the future.
1648 */
1649 if (arc_err != 0) {
1650 srdp->abd = abd_alloc_linear(srdp->datasz, B_FALSE);
1651 srdp->io_outstanding = B_TRUE;
1652 zio_nowait(zio_read(NULL, os->os_spa, bp, srdp->abd,
1653 srdp->datasz, dmu_send_read_done, range,
1654 ZIO_PRIORITY_ASYNC_READ, zioflags, &zb));
1655 }
1656 }
1657
1658 /*
1659 * Create a new record with the given values.
1660 */
1661 static void
enqueue_range(struct send_reader_thread_arg * srta,bqueue_t * q,dnode_t * dn,uint64_t blkid,uint64_t count,const blkptr_t * bp,uint32_t datablksz)1662 enqueue_range(struct send_reader_thread_arg *srta, bqueue_t *q, dnode_t *dn,
1663 uint64_t blkid, uint64_t count, const blkptr_t *bp, uint32_t datablksz)
1664 {
1665 enum type range_type = (bp == NULL || BP_IS_HOLE(bp) ? HOLE :
1666 (BP_IS_REDACTED(bp) ? REDACT : DATA));
1667
1668 struct send_range *range = range_alloc(range_type, dn->dn_object,
1669 blkid, blkid + count, B_FALSE);
1670
1671 if (blkid == DMU_SPILL_BLKID) {
1672 ASSERT3P(bp, !=, NULL);
1673 ASSERT3U(BP_GET_TYPE(bp), ==, DMU_OT_SA);
1674 }
1675
1676 switch (range_type) {
1677 case HOLE:
1678 range->sru.hole.datablksz = datablksz;
1679 break;
1680 case DATA:
1681 ASSERT3U(count, ==, 1);
1682 range->sru.data.datablksz = datablksz;
1683 range->sru.data.obj_type = dn->dn_type;
1684 range->sru.data.bp = *bp;
1685 issue_data_read(srta, range);
1686 break;
1687 case REDACT:
1688 range->sru.redact.datablksz = datablksz;
1689 break;
1690 default:
1691 break;
1692 }
1693 bqueue_enqueue(q, range, datablksz);
1694 }
1695
1696 /*
1697 * Send DRR_SPILL records for unmodified spill blocks. This is useful
1698 * because changing certain attributes of the object (e.g. blocksize)
1699 * can cause old versions of ZFS to incorrectly remove a spill block.
1700 * Including these records in the stream forces an up to date version
1701 * to always be written ensuring they're never lost. Current versions
1702 * of the code which understand the DRR_FLAG_SPILL_BLOCK feature can
1703 * ignore these unmodified spill blocks.
1704 *
1705 * We piggyback the spill_range to dnode range instead of enqueueing it
1706 * so send_range_after won't complain.
1707 */
1708 static uint64_t
piggyback_unmodified_spill(struct send_reader_thread_arg * srta,struct send_range * range)1709 piggyback_unmodified_spill(struct send_reader_thread_arg *srta,
1710 struct send_range *range)
1711 {
1712 ASSERT3U(range->type, ==, OBJECT);
1713
1714 dnode_phys_t *dnp = range->sru.object.dnp;
1715 uint64_t fromtxg = srta->smta->to_arg->fromtxg;
1716
1717 if (!zfs_send_unmodified_spill_blocks ||
1718 !(dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) ||
1719 !(BP_GET_LOGICAL_BIRTH(DN_SPILL_BLKPTR(dnp)) <= fromtxg))
1720 return (0);
1721
1722 blkptr_t *bp = DN_SPILL_BLKPTR(dnp);
1723 struct send_range *spill_range = range_alloc(DATA, range->object,
1724 DMU_SPILL_BLKID, DMU_SPILL_BLKID+1, B_FALSE);
1725 spill_range->sru.data.bp = *bp;
1726 spill_range->sru.data.obj_type = dnp->dn_type;
1727 spill_range->sru.data.datablksz = BP_GET_LSIZE(bp);
1728
1729 issue_data_read(srta, spill_range);
1730 range->sru.object.spill_range = spill_range;
1731
1732 return (BP_GET_LSIZE(bp));
1733 }
1734
1735 /*
1736 * This thread is responsible for two things: First, it retrieves the correct
1737 * blkptr in the to ds if we need to send the data because of something from
1738 * the from thread. As a result of this, we're the first ones to discover that
1739 * some indirect blocks can be discarded because they're not holes. Second,
1740 * it issues prefetches for the data we need to send.
1741 */
1742 static __attribute__((noreturn)) void
send_reader_thread(void * arg)1743 send_reader_thread(void *arg)
1744 {
1745 struct send_reader_thread_arg *srta = arg;
1746 struct send_merge_thread_arg *smta = srta->smta;
1747 bqueue_t *inq = &smta->q;
1748 bqueue_t *outq = &srta->q;
1749 objset_t *os = smta->os;
1750 fstrans_cookie_t cookie = spl_fstrans_mark();
1751 struct send_range *range = bqueue_dequeue(inq);
1752 int err = 0;
1753
1754 /*
1755 * If the record we're analyzing is from a redaction bookmark from the
1756 * fromds, then we need to know whether or not it exists in the tods so
1757 * we know whether to create records for it or not. If it does, we need
1758 * the datablksz so we can generate an appropriate record for it.
1759 * Finally, if it isn't redacted, we need the blkptr so that we can send
1760 * a WRITE record containing the actual data.
1761 */
1762 uint64_t last_obj = UINT64_MAX;
1763 uint64_t last_obj_exists = B_TRUE;
1764 while (!range->eos_marker && !srta->cancel && smta->error == 0 &&
1765 err == 0) {
1766 uint64_t spill = 0;
1767 switch (range->type) {
1768 case DATA:
1769 issue_data_read(srta, range);
1770 bqueue_enqueue(outq, range, range->sru.data.datablksz);
1771 range = get_next_range_nofree(inq, range);
1772 break;
1773 case OBJECT:
1774 spill = piggyback_unmodified_spill(srta, range);
1775 zfs_fallthrough;
1776 case HOLE:
1777 case OBJECT_RANGE:
1778 case REDACT: // Redacted blocks must exist
1779 bqueue_enqueue(outq, range, sizeof (*range) + spill);
1780 range = get_next_range_nofree(inq, range);
1781 break;
1782 case PREVIOUSLY_REDACTED: {
1783 /*
1784 * This entry came from the "from bookmark" when
1785 * sending from a bookmark that has a redaction
1786 * list. We need to check if this object/blkid
1787 * exists in the target ("to") dataset, and if
1788 * not then we drop this entry. We also need
1789 * to fill in the block pointer so that we know
1790 * what to prefetch.
1791 *
1792 * To accomplish the above, we first cache whether or
1793 * not the last object we examined exists. If it
1794 * doesn't, we can drop this record. If it does, we hold
1795 * the dnode and use it to call dbuf_dnode_findbp. We do
1796 * this instead of dbuf_bookmark_findbp because we will
1797 * often operate on large ranges, and holding the dnode
1798 * once is more efficient.
1799 */
1800 boolean_t object_exists = B_TRUE;
1801 /*
1802 * If the data is redacted, we only care if it exists,
1803 * so that we don't send records for objects that have
1804 * been deleted.
1805 */
1806 dnode_t *dn;
1807 if (range->object == last_obj && !last_obj_exists) {
1808 /*
1809 * If we're still examining the same object as
1810 * previously, and it doesn't exist, we don't
1811 * need to call dbuf_bookmark_findbp.
1812 */
1813 object_exists = B_FALSE;
1814 } else {
1815 err = dnode_hold(os, range->object, FTAG, &dn);
1816 if (err == ENOENT) {
1817 object_exists = B_FALSE;
1818 err = 0;
1819 }
1820 last_obj = range->object;
1821 last_obj_exists = object_exists;
1822 }
1823
1824 if (err != 0) {
1825 break;
1826 } else if (!object_exists) {
1827 /*
1828 * The block was modified, but doesn't
1829 * exist in the to dataset; if it was
1830 * deleted in the to dataset, then we'll
1831 * visit the hole bp for it at some point.
1832 */
1833 range = get_next_range(inq, range);
1834 continue;
1835 }
1836 uint64_t file_max =
1837 MIN(dn->dn_maxblkid + 1, range->end_blkid);
1838 /*
1839 * The object exists, so we need to try to find the
1840 * blkptr for each block in the range we're processing.
1841 */
1842 rw_enter(&dn->dn_struct_rwlock, RW_READER);
1843 for (uint64_t blkid = range->start_blkid;
1844 blkid < file_max; blkid++) {
1845 blkptr_t bp;
1846 uint32_t datablksz =
1847 dn->dn_phys->dn_datablkszsec <<
1848 SPA_MINBLOCKSHIFT;
1849 uint64_t offset = blkid * datablksz;
1850 /*
1851 * This call finds the next non-hole block in
1852 * the object. This is to prevent a
1853 * performance problem where we're unredacting
1854 * a large hole. Using dnode_next_offset to
1855 * skip over the large hole avoids iterating
1856 * over every block in it.
1857 */
1858 err = dnode_next_offset(dn, DNODE_FIND_HAVELOCK,
1859 &offset, 1, 1, 0);
1860 if (err == ESRCH) {
1861 offset = UINT64_MAX;
1862 err = 0;
1863 } else if (err != 0) {
1864 break;
1865 }
1866 if (offset != blkid * datablksz) {
1867 /*
1868 * if there is a hole from here
1869 * (blkid) to offset
1870 */
1871 offset = MIN(offset, file_max *
1872 datablksz);
1873 uint64_t nblks = (offset / datablksz) -
1874 blkid;
1875 enqueue_range(srta, outq, dn, blkid,
1876 nblks, NULL, datablksz);
1877 blkid += nblks;
1878 }
1879 if (blkid >= file_max)
1880 break;
1881 err = dbuf_dnode_findbp(dn, 0, blkid, &bp,
1882 NULL, NULL);
1883 if (err != 0)
1884 break;
1885 ASSERT(!BP_IS_HOLE(&bp));
1886 enqueue_range(srta, outq, dn, blkid, 1, &bp,
1887 datablksz);
1888 }
1889 rw_exit(&dn->dn_struct_rwlock);
1890 dnode_rele(dn, FTAG);
1891 range = get_next_range(inq, range);
1892 }
1893 }
1894 }
1895 if (srta->cancel || err != 0) {
1896 smta->cancel = B_TRUE;
1897 srta->error = err;
1898 } else if (smta->error != 0) {
1899 srta->error = smta->error;
1900 }
1901 while (!range->eos_marker)
1902 range = get_next_range(inq, range);
1903
1904 bqueue_enqueue_flush(outq, range, 1);
1905 spl_fstrans_unmark(cookie);
1906 thread_exit();
1907 }
1908
1909 #define NUM_SNAPS_NOT_REDACTED UINT64_MAX
1910
1911 struct dmu_send_params {
1912 /* Pool args */
1913 const void *tag; // Tag dp was held with, will be used to release dp.
1914 dsl_pool_t *dp;
1915 /* To snapshot args */
1916 const char *tosnap;
1917 dsl_dataset_t *to_ds;
1918 /* From snapshot args */
1919 zfs_bookmark_phys_t ancestor_zb;
1920 uint64_t *fromredactsnaps;
1921 /* NUM_SNAPS_NOT_REDACTED if not sending from redaction bookmark */
1922 uint64_t numfromredactsnaps;
1923 /* Stream params */
1924 boolean_t is_clone;
1925 boolean_t embedok;
1926 boolean_t large_block_ok;
1927 boolean_t compressok;
1928 boolean_t rawok;
1929 boolean_t savedok;
1930 uint64_t resumeobj;
1931 uint64_t resumeoff;
1932 uint64_t saved_guid;
1933 zfs_bookmark_phys_t *redactbook;
1934 /* Stream output params */
1935 dmu_send_outparams_t *dso;
1936
1937 /* Stream progress params */
1938 offset_t *off;
1939 int outfd;
1940 char saved_toname[MAXNAMELEN];
1941 };
1942
1943 static int
setup_featureflags(struct dmu_send_params * dspp,objset_t * os,uint64_t * featureflags)1944 setup_featureflags(struct dmu_send_params *dspp, objset_t *os,
1945 uint64_t *featureflags)
1946 {
1947 dsl_dataset_t *to_ds = dspp->to_ds;
1948 dsl_pool_t *dp = dspp->dp;
1949
1950 if (dmu_objset_type(os) == DMU_OST_ZFS) {
1951 uint64_t version;
1952 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0)
1953 return (SET_ERROR(EINVAL));
1954
1955 if (version >= ZPL_VERSION_SA)
1956 *featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
1957 }
1958
1959 /* raw sends imply large_block_ok */
1960 if ((dspp->rawok || dspp->large_block_ok) &&
1961 dsl_dataset_feature_is_active(to_ds, SPA_FEATURE_LARGE_BLOCKS)) {
1962 *featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
1963 }
1964
1965 /* encrypted datasets will not have embedded blocks */
1966 if ((dspp->embedok || dspp->rawok) && !os->os_encrypted &&
1967 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
1968 *featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
1969 }
1970
1971 /* raw send implies compressok */
1972 if (dspp->compressok || dspp->rawok)
1973 *featureflags |= DMU_BACKUP_FEATURE_COMPRESSED;
1974
1975 if (dspp->rawok && os->os_encrypted)
1976 *featureflags |= DMU_BACKUP_FEATURE_RAW;
1977
1978 if ((*featureflags &
1979 (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED |
1980 DMU_BACKUP_FEATURE_RAW)) != 0 &&
1981 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) {
1982 *featureflags |= DMU_BACKUP_FEATURE_LZ4;
1983 }
1984
1985 /*
1986 * We specifically do not include DMU_BACKUP_FEATURE_EMBED_DATA here to
1987 * allow sending ZSTD compressed datasets to a receiver that does not
1988 * support ZSTD
1989 */
1990 if ((*featureflags &
1991 (DMU_BACKUP_FEATURE_COMPRESSED | DMU_BACKUP_FEATURE_RAW)) != 0 &&
1992 dsl_dataset_feature_is_active(to_ds, SPA_FEATURE_ZSTD_COMPRESS)) {
1993 *featureflags |= DMU_BACKUP_FEATURE_ZSTD;
1994 }
1995
1996 if (dspp->resumeobj != 0 || dspp->resumeoff != 0) {
1997 *featureflags |= DMU_BACKUP_FEATURE_RESUMING;
1998 }
1999
2000 if (dspp->redactbook != NULL) {
2001 *featureflags |= DMU_BACKUP_FEATURE_REDACTED;
2002 }
2003
2004 if (dsl_dataset_feature_is_active(to_ds, SPA_FEATURE_LARGE_DNODE)) {
2005 *featureflags |= DMU_BACKUP_FEATURE_LARGE_DNODE;
2006 }
2007
2008 if (dsl_dataset_feature_is_active(to_ds, SPA_FEATURE_LONGNAME)) {
2009 *featureflags |= DMU_BACKUP_FEATURE_LONGNAME;
2010 }
2011
2012 if (dsl_dataset_feature_is_active(to_ds, SPA_FEATURE_LARGE_MICROZAP)) {
2013 /*
2014 * We must never split a large microzap block, so we can only
2015 * send large microzaps if LARGE_BLOCKS is already enabled.
2016 */
2017 if (!(*featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS))
2018 return (SET_ERROR(ZFS_ERR_STREAM_LARGE_MICROZAP));
2019 *featureflags |= DMU_BACKUP_FEATURE_LARGE_MICROZAP;
2020 }
2021
2022 return (0);
2023 }
2024
2025 static dmu_replay_record_t *
create_begin_record(struct dmu_send_params * dspp,objset_t * os,uint64_t featureflags)2026 create_begin_record(struct dmu_send_params *dspp, objset_t *os,
2027 uint64_t featureflags)
2028 {
2029 dmu_replay_record_t *drr = kmem_zalloc(sizeof (dmu_replay_record_t),
2030 KM_SLEEP);
2031 drr->drr_type = DRR_BEGIN;
2032
2033 struct drr_begin *drrb = &drr->drr_u.drr_begin;
2034 dsl_dataset_t *to_ds = dspp->to_ds;
2035
2036 drrb->drr_magic = DMU_BACKUP_MAGIC;
2037 drrb->drr_creation_time = dsl_dataset_phys(to_ds)->ds_creation_time;
2038 drrb->drr_type = dmu_objset_type(os);
2039 drrb->drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
2040 drrb->drr_fromguid = dspp->ancestor_zb.zbm_guid;
2041
2042 DMU_SET_STREAM_HDRTYPE(drrb->drr_versioninfo, DMU_SUBSTREAM);
2043 DMU_SET_FEATUREFLAGS(drrb->drr_versioninfo, featureflags);
2044
2045 if (dspp->is_clone)
2046 drrb->drr_flags |= DRR_FLAG_CLONE;
2047 if (dsl_dataset_phys(dspp->to_ds)->ds_flags & DS_FLAG_CI_DATASET)
2048 drrb->drr_flags |= DRR_FLAG_CI_DATA;
2049 if (zfs_send_set_freerecords_bit)
2050 drrb->drr_flags |= DRR_FLAG_FREERECORDS;
2051 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_SPILL_BLOCK;
2052
2053 if (dspp->savedok) {
2054 drrb->drr_toguid = dspp->saved_guid;
2055 strlcpy(drrb->drr_toname, dspp->saved_toname,
2056 sizeof (drrb->drr_toname));
2057 } else {
2058 dsl_dataset_name(to_ds, drrb->drr_toname);
2059 if (!to_ds->ds_is_snapshot) {
2060 (void) strlcat(drrb->drr_toname, "@--head--",
2061 sizeof (drrb->drr_toname));
2062 }
2063 }
2064 return (drr);
2065 }
2066
2067 static void
setup_to_thread(struct send_thread_arg * to_arg,objset_t * to_os,dmu_sendstatus_t * dssp,uint64_t fromtxg,boolean_t rawok)2068 setup_to_thread(struct send_thread_arg *to_arg, objset_t *to_os,
2069 dmu_sendstatus_t *dssp, uint64_t fromtxg, boolean_t rawok)
2070 {
2071 VERIFY0(bqueue_init(&to_arg->q, zfs_send_no_prefetch_queue_ff,
2072 MAX(zfs_send_no_prefetch_queue_length, 2 * zfs_max_recordsize),
2073 offsetof(struct send_range, ln)));
2074 to_arg->error_code = 0;
2075 to_arg->cancel = B_FALSE;
2076 to_arg->os = to_os;
2077 to_arg->fromtxg = fromtxg;
2078 to_arg->flags = TRAVERSE_PRE | TRAVERSE_PREFETCH_METADATA;
2079 if (rawok)
2080 to_arg->flags |= TRAVERSE_NO_DECRYPT;
2081 if (zfs_send_corrupt_data)
2082 to_arg->flags |= TRAVERSE_HARD;
2083 to_arg->num_blocks_visited = &dssp->dss_blocks;
2084 (void) thread_create(NULL, 0, send_traverse_thread, to_arg, 0,
2085 curproc, TS_RUN, minclsyspri);
2086 }
2087
2088 static void
setup_from_thread(struct redact_list_thread_arg * from_arg,redaction_list_t * from_rl,dmu_sendstatus_t * dssp)2089 setup_from_thread(struct redact_list_thread_arg *from_arg,
2090 redaction_list_t *from_rl, dmu_sendstatus_t *dssp)
2091 {
2092 VERIFY0(bqueue_init(&from_arg->q, zfs_send_no_prefetch_queue_ff,
2093 MAX(zfs_send_no_prefetch_queue_length, 2 * zfs_max_recordsize),
2094 offsetof(struct send_range, ln)));
2095 from_arg->error_code = 0;
2096 from_arg->cancel = B_FALSE;
2097 from_arg->rl = from_rl;
2098 from_arg->mark_redact = B_FALSE;
2099 from_arg->num_blocks_visited = &dssp->dss_blocks;
2100 /*
2101 * If from_ds is null, send_traverse_thread just returns success and
2102 * enqueues an eos marker.
2103 */
2104 (void) thread_create(NULL, 0, redact_list_thread, from_arg, 0,
2105 curproc, TS_RUN, minclsyspri);
2106 }
2107
2108 static void
setup_redact_list_thread(struct redact_list_thread_arg * rlt_arg,struct dmu_send_params * dspp,redaction_list_t * rl,dmu_sendstatus_t * dssp)2109 setup_redact_list_thread(struct redact_list_thread_arg *rlt_arg,
2110 struct dmu_send_params *dspp, redaction_list_t *rl, dmu_sendstatus_t *dssp)
2111 {
2112 if (dspp->redactbook == NULL)
2113 return;
2114
2115 rlt_arg->cancel = B_FALSE;
2116 VERIFY0(bqueue_init(&rlt_arg->q, zfs_send_no_prefetch_queue_ff,
2117 MAX(zfs_send_no_prefetch_queue_length, 2 * zfs_max_recordsize),
2118 offsetof(struct send_range, ln)));
2119 rlt_arg->error_code = 0;
2120 rlt_arg->mark_redact = B_TRUE;
2121 rlt_arg->rl = rl;
2122 rlt_arg->num_blocks_visited = &dssp->dss_blocks;
2123
2124 (void) thread_create(NULL, 0, redact_list_thread, rlt_arg, 0,
2125 curproc, TS_RUN, minclsyspri);
2126 }
2127
2128 static void
setup_merge_thread(struct send_merge_thread_arg * smt_arg,struct dmu_send_params * dspp,struct redact_list_thread_arg * from_arg,struct send_thread_arg * to_arg,struct redact_list_thread_arg * rlt_arg,objset_t * os)2129 setup_merge_thread(struct send_merge_thread_arg *smt_arg,
2130 struct dmu_send_params *dspp, struct redact_list_thread_arg *from_arg,
2131 struct send_thread_arg *to_arg, struct redact_list_thread_arg *rlt_arg,
2132 objset_t *os)
2133 {
2134 VERIFY0(bqueue_init(&smt_arg->q, zfs_send_no_prefetch_queue_ff,
2135 MAX(zfs_send_no_prefetch_queue_length, 2 * zfs_max_recordsize),
2136 offsetof(struct send_range, ln)));
2137 smt_arg->cancel = B_FALSE;
2138 smt_arg->error = 0;
2139 smt_arg->from_arg = from_arg;
2140 smt_arg->to_arg = to_arg;
2141 if (dspp->redactbook != NULL)
2142 smt_arg->redact_arg = rlt_arg;
2143
2144 smt_arg->os = os;
2145 (void) thread_create(NULL, 0, send_merge_thread, smt_arg, 0, curproc,
2146 TS_RUN, minclsyspri);
2147 }
2148
2149 static void
setup_reader_thread(struct send_reader_thread_arg * srt_arg,struct dmu_send_params * dspp,struct send_merge_thread_arg * smt_arg,uint64_t featureflags)2150 setup_reader_thread(struct send_reader_thread_arg *srt_arg,
2151 struct dmu_send_params *dspp, struct send_merge_thread_arg *smt_arg,
2152 uint64_t featureflags)
2153 {
2154 VERIFY0(bqueue_init(&srt_arg->q, zfs_send_queue_ff,
2155 MAX(zfs_send_queue_length, 2 * zfs_max_recordsize),
2156 offsetof(struct send_range, ln)));
2157 srt_arg->smta = smt_arg;
2158 srt_arg->issue_reads = !dspp->dso->dso_dryrun;
2159 srt_arg->featureflags = featureflags;
2160 (void) thread_create(NULL, 0, send_reader_thread, srt_arg, 0,
2161 curproc, TS_RUN, minclsyspri);
2162 }
2163
2164 static int
setup_resume_points(struct dmu_send_params * dspp,struct send_thread_arg * to_arg,struct redact_list_thread_arg * from_arg,struct redact_list_thread_arg * rlt_arg,struct send_merge_thread_arg * smt_arg,boolean_t resuming,objset_t * os,redaction_list_t * redact_rl,nvlist_t * nvl)2165 setup_resume_points(struct dmu_send_params *dspp,
2166 struct send_thread_arg *to_arg, struct redact_list_thread_arg *from_arg,
2167 struct redact_list_thread_arg *rlt_arg,
2168 struct send_merge_thread_arg *smt_arg, boolean_t resuming, objset_t *os,
2169 redaction_list_t *redact_rl, nvlist_t *nvl)
2170 {
2171 (void) smt_arg;
2172 dsl_dataset_t *to_ds = dspp->to_ds;
2173 int err = 0;
2174
2175 uint64_t obj = 0;
2176 uint64_t blkid = 0;
2177 if (resuming) {
2178 obj = dspp->resumeobj;
2179 dmu_object_info_t to_doi;
2180 err = dmu_object_info(os, obj, &to_doi);
2181 if (err != 0)
2182 return (err);
2183
2184 blkid = dspp->resumeoff / to_doi.doi_data_block_size;
2185 }
2186 /*
2187 * If we're resuming a redacted send, we can skip to the appropriate
2188 * point in the redaction bookmark by binary searching through it.
2189 */
2190 if (redact_rl != NULL) {
2191 SET_BOOKMARK(&rlt_arg->resume, to_ds->ds_object, obj, 0, blkid);
2192 }
2193
2194 SET_BOOKMARK(&to_arg->resume, to_ds->ds_object, obj, 0, blkid);
2195 if (nvlist_exists(nvl, BEGINNV_REDACT_FROM_SNAPS)) {
2196 uint64_t objset = dspp->ancestor_zb.zbm_redaction_obj;
2197 /*
2198 * Note: If the resume point is in an object whose
2199 * blocksize is different in the from vs to snapshots,
2200 * we will have divided by the "wrong" blocksize.
2201 * However, in this case fromsnap's send_cb() will
2202 * detect that the blocksize has changed and therefore
2203 * ignore this object.
2204 *
2205 * If we're resuming a send from a redaction bookmark,
2206 * we still cannot accidentally suggest blocks behind
2207 * the to_ds. In addition, we know that any blocks in
2208 * the object in the to_ds will have to be sent, since
2209 * the size changed. Therefore, we can't cause any harm
2210 * this way either.
2211 */
2212 SET_BOOKMARK(&from_arg->resume, objset, obj, 0, blkid);
2213 }
2214 if (resuming) {
2215 fnvlist_add_uint64(nvl, BEGINNV_RESUME_OBJECT, dspp->resumeobj);
2216 fnvlist_add_uint64(nvl, BEGINNV_RESUME_OFFSET, dspp->resumeoff);
2217 }
2218 return (0);
2219 }
2220
2221 static dmu_sendstatus_t *
setup_send_progress(struct dmu_send_params * dspp)2222 setup_send_progress(struct dmu_send_params *dspp)
2223 {
2224 dmu_sendstatus_t *dssp = kmem_zalloc(sizeof (*dssp), KM_SLEEP);
2225 dssp->dss_outfd = dspp->outfd;
2226 dssp->dss_off = dspp->off;
2227 dssp->dss_proc = curproc;
2228 mutex_enter(&dspp->to_ds->ds_sendstream_lock);
2229 list_insert_head(&dspp->to_ds->ds_sendstreams, dssp);
2230 mutex_exit(&dspp->to_ds->ds_sendstream_lock);
2231 return (dssp);
2232 }
2233
2234 /*
2235 * Payloads must be multiples of 8 bytes for historical compatibility, but
2236 * XDR-encoded nvlists are sized in multiples of 4 bytes and may need padding.
2237 *
2238 * Here we do the simplest possible thing and copy the data to a separate
2239 * buffer. Not ideal in terms of performance and memory use, but most BEGIN
2240 * nvlists are small or absent, the allocation is momentary, and we'll need
2241 * to do this at most once per dataset.
2242 *
2243 * It's OK if there is extra data after a packed nvlist on the receiving
2244 * side because packed nvlists have an internal end-of-list marker.
2245 *
2246 * The new buffer is allocated with kmem_alloc() and can be freed with
2247 * fnvlist_pack_free(), like the original.
2248 */
2249 static inline void
pad_packed_nvlist(char ** buffer,size_t * size)2250 pad_packed_nvlist(char **buffer, size_t *size)
2251 {
2252 size_t size_in = *size;
2253 size_t extra_bytes = P2ROUNDUP(size_in, 8) - size_in;
2254 if (extra_bytes != 0) {
2255 size_t expanded_size = size_in + extra_bytes;
2256 char *longbuf = kmem_alloc(expanded_size, KM_SLEEP);
2257 memcpy(longbuf, *buffer, size_in);
2258 memset(longbuf + size_in, 0, extra_bytes);
2259 fnvlist_pack_free(*buffer, size_in);
2260 *buffer = longbuf;
2261 *size = expanded_size;
2262 }
2263 }
2264
2265 /*
2266 * Actually do the bulk of the work in a zfs send.
2267 *
2268 * The idea is that we want to do a send from ancestor_zb to to_ds. We also
2269 * want to not send any data that has been modified by all the datasets in
2270 * redactsnaparr, and store the list of blocks that are redacted in this way in
2271 * a bookmark named redactbook, created on the to_ds. We do this by creating
2272 * several worker threads, whose function is described below.
2273 *
2274 * There are three cases.
2275 * The first case is a redacted zfs send. In this case there are 5 threads.
2276 * The first thread is the to_ds traversal thread: it calls dataset_traverse on
2277 * the to_ds and finds all the blocks that have changed since ancestor_zb (if
2278 * it's a full send, that's all blocks in the dataset). It then sends those
2279 * blocks on to the send merge thread. The redact list thread takes the data
2280 * from the redaction bookmark and sends those blocks on to the send merge
2281 * thread. The send merge thread takes the data from the to_ds traversal
2282 * thread, and combines it with the redaction records from the redact list
2283 * thread. If a block appears in both the to_ds's data and the redaction data,
2284 * the send merge thread will mark it as redacted and send it on to the prefetch
2285 * thread. Otherwise, the send merge thread will send the block on to the
2286 * prefetch thread unchanged. The prefetch thread will issue prefetch reads for
2287 * any data that isn't redacted, and then send the data on to the main thread.
2288 * The main thread behaves the same as in a normal send case, issuing demand
2289 * reads for data blocks and sending out records over the network
2290 *
2291 * The graphic below diagrams the flow of data in the case of a redacted zfs
2292 * send. Each box represents a thread, and each line represents the flow of
2293 * data.
2294 *
2295 * Records from the |
2296 * redaction bookmark |
2297 * +--------------------+ | +---------------------------+
2298 * | | v | Send Merge Thread |
2299 * | Redact List Thread +----------> Apply redaction marks to |
2300 * | | | records as specified by |
2301 * +--------------------+ | redaction ranges |
2302 * +----^---------------+------+
2303 * | | Merged data
2304 * | |
2305 * | +------------v--------+
2306 * | | Prefetch Thread |
2307 * +--------------------+ | | Issues prefetch |
2308 * | to_ds Traversal | | | reads of data blocks|
2309 * | Thread (finds +---------------+ +------------+--------+
2310 * | candidate blocks) | Blocks modified | Prefetched data
2311 * +--------------------+ by to_ds since |
2312 * ancestor_zb +------------v----+
2313 * | Main Thread | File Descriptor
2314 * | Sends data over +->(to zfs receive)
2315 * | wire |
2316 * +-----------------+
2317 *
2318 * The second case is an incremental send from a redaction bookmark. The to_ds
2319 * traversal thread and the main thread behave the same as in the redacted
2320 * send case. The new thread is the from bookmark traversal thread. It
2321 * iterates over the redaction list in the redaction bookmark, and enqueues
2322 * records for each block that was redacted in the original send. The send
2323 * merge thread now has to merge the data from the two threads. For details
2324 * about that process, see the header comment of send_merge_thread(). Any data
2325 * it decides to send on will be prefetched by the prefetch thread. Note that
2326 * you can perform a redacted send from a redaction bookmark; in that case,
2327 * the data flow behaves very similarly to the flow in the redacted send case,
2328 * except with the addition of the bookmark traversal thread iterating over the
2329 * redaction bookmark. The send_merge_thread also has to take on the
2330 * responsibility of merging the redact list thread's records, the bookmark
2331 * traversal thread's records, and the to_ds records.
2332 *
2333 * +---------------------+
2334 * | |
2335 * | Redact List Thread +--------------+
2336 * | | |
2337 * +---------------------+ |
2338 * Blocks in redaction list | Ranges modified by every secure snap
2339 * of from bookmark | (or EOS if not readcted)
2340 * |
2341 * +---------------------+ | +----v----------------------+
2342 * | bookmark Traversal | v | Send Merge Thread |
2343 * | Thread (finds +---------> Merges bookmark, rlt, and |
2344 * | candidate blocks) | | to_ds send records |
2345 * +---------------------+ +----^---------------+------+
2346 * | | Merged data
2347 * | +------------v--------+
2348 * | | Prefetch Thread |
2349 * +--------------------+ | | Issues prefetch |
2350 * | to_ds Traversal | | | reads of data blocks|
2351 * | Thread (finds +---------------+ +------------+--------+
2352 * | candidate blocks) | Blocks modified | Prefetched data
2353 * +--------------------+ by to_ds since +------------v----+
2354 * ancestor_zb | Main Thread | File Descriptor
2355 * | Sends data over +->(to zfs receive)
2356 * | wire |
2357 * +-----------------+
2358 *
2359 * The final case is a simple zfs full or incremental send. The to_ds traversal
2360 * thread behaves the same as always. The redact list thread is never started.
2361 * The send merge thread takes all the blocks that the to_ds traversal thread
2362 * sends it, prefetches the data, and sends the blocks on to the main thread.
2363 * The main thread sends the data over the wire.
2364 *
2365 * To keep performance acceptable, we want to prefetch the data in the worker
2366 * threads. While the to_ds thread could simply use the TRAVERSE_PREFETCH
2367 * feature built into traverse_dataset, the combining and deletion of records
2368 * due to redaction and sends from redaction bookmarks mean that we could
2369 * issue many unnecessary prefetches. As a result, we only prefetch data
2370 * after we've determined that the record is not going to be redacted. To
2371 * prevent the prefetching from getting too far ahead of the main thread, the
2372 * blocking queues that are used for communication are capped not by the
2373 * number of entries in the queue, but by the sum of the size of the
2374 * prefetches associated with them. The limit on the amount of data that the
2375 * thread can prefetch beyond what the main thread has reached is controlled
2376 * by the global variable zfs_send_queue_length. In addition, to prevent poor
2377 * performance in the beginning of a send, we also limit the distance ahead
2378 * that the traversal threads can be. That distance is controlled by the
2379 * zfs_send_no_prefetch_queue_length tunable.
2380 *
2381 * Note: Releases dp using the specified tag.
2382 */
2383 static int
dmu_send_impl(struct dmu_send_params * dspp)2384 dmu_send_impl(struct dmu_send_params *dspp)
2385 {
2386 objset_t *os;
2387 dmu_replay_record_t *drr;
2388 dmu_sendstatus_t *dssp;
2389 dmu_send_cookie_t dsc = {0};
2390 int err;
2391 uint64_t fromtxg = dspp->ancestor_zb.zbm_creation_txg;
2392 uint64_t featureflags = 0;
2393 struct redact_list_thread_arg *from_arg;
2394 struct send_thread_arg *to_arg;
2395 struct redact_list_thread_arg *rlt_arg;
2396 struct send_merge_thread_arg *smt_arg;
2397 struct send_reader_thread_arg *srt_arg;
2398 struct send_range *range;
2399 redaction_list_t *from_rl = NULL;
2400 redaction_list_t *redact_rl = NULL;
2401 boolean_t resuming = (dspp->resumeobj != 0 || dspp->resumeoff != 0);
2402 boolean_t book_resuming = resuming;
2403
2404 dsl_dataset_t *to_ds = dspp->to_ds;
2405 zfs_bookmark_phys_t *ancestor_zb = &dspp->ancestor_zb;
2406 dsl_pool_t *dp = dspp->dp;
2407 const void *tag = dspp->tag;
2408
2409 err = dmu_objset_from_ds(to_ds, &os);
2410 if (err != 0) {
2411 dsl_pool_rele(dp, tag);
2412 return (err);
2413 }
2414
2415 /*
2416 * If this is a non-raw send of an encrypted ds, we can ensure that
2417 * the objset_phys_t is authenticated. This is safe because this is
2418 * either a snapshot or we have owned the dataset, ensuring that
2419 * it can't be modified.
2420 */
2421 if (!dspp->rawok && os->os_encrypted &&
2422 arc_is_unauthenticated(os->os_phys_buf)) {
2423 zbookmark_phys_t zb;
2424
2425 SET_BOOKMARK(&zb, to_ds->ds_object, ZB_ROOT_OBJECT,
2426 ZB_ROOT_LEVEL, ZB_ROOT_BLKID);
2427 err = arc_untransform(os->os_phys_buf, os->os_spa,
2428 &zb, B_FALSE);
2429 if (err != 0) {
2430 dsl_pool_rele(dp, tag);
2431 return (err);
2432 }
2433
2434 ASSERT0(arc_is_unauthenticated(os->os_phys_buf));
2435 }
2436
2437 if ((err = setup_featureflags(dspp, os, &featureflags)) != 0) {
2438 dsl_pool_rele(dp, tag);
2439 return (err);
2440 }
2441
2442 /*
2443 * If we're doing a redacted send, hold the bookmark's redaction list.
2444 */
2445 if (dspp->redactbook != NULL) {
2446 err = dsl_redaction_list_hold_obj(dp,
2447 dspp->redactbook->zbm_redaction_obj, FTAG,
2448 &redact_rl);
2449 if (err != 0) {
2450 dsl_pool_rele(dp, tag);
2451 return (SET_ERROR(EINVAL));
2452 }
2453 dsl_redaction_list_long_hold(dp, redact_rl, FTAG);
2454 }
2455
2456 /*
2457 * If we're sending from a redaction bookmark, hold the redaction list
2458 * so that we can consider sending the redacted blocks.
2459 */
2460 if (ancestor_zb->zbm_redaction_obj != 0) {
2461 err = dsl_redaction_list_hold_obj(dp,
2462 ancestor_zb->zbm_redaction_obj, FTAG, &from_rl);
2463 if (err != 0) {
2464 if (redact_rl != NULL) {
2465 dsl_redaction_list_long_rele(redact_rl, FTAG);
2466 dsl_redaction_list_rele(redact_rl, FTAG);
2467 }
2468 dsl_pool_rele(dp, tag);
2469 return (SET_ERROR(EINVAL));
2470 }
2471 dsl_redaction_list_long_hold(dp, from_rl, FTAG);
2472 }
2473
2474 dsl_dataset_long_hold(to_ds, FTAG);
2475
2476 from_arg = kmem_zalloc(sizeof (*from_arg), KM_SLEEP);
2477 to_arg = kmem_zalloc(sizeof (*to_arg), KM_SLEEP);
2478 rlt_arg = kmem_zalloc(sizeof (*rlt_arg), KM_SLEEP);
2479 smt_arg = kmem_zalloc(sizeof (*smt_arg), KM_SLEEP);
2480 srt_arg = kmem_zalloc(sizeof (*srt_arg), KM_SLEEP);
2481
2482 drr = create_begin_record(dspp, os, featureflags);
2483 dssp = setup_send_progress(dspp);
2484
2485 dsc.dsc_drr = drr;
2486 dsc.dsc_dso = dspp->dso;
2487 dsc.dsc_os = os;
2488 dsc.dsc_off = dspp->off;
2489 dsc.dsc_toguid = dsl_dataset_phys(to_ds)->ds_guid;
2490 dsc.dsc_fromtxg = fromtxg;
2491 dsc.dsc_pending_op = PENDING_NONE;
2492 dsc.dsc_featureflags = featureflags;
2493 dsc.dsc_resume_object = dspp->resumeobj;
2494 dsc.dsc_resume_offset = dspp->resumeoff;
2495
2496 dsl_pool_rele(dp, tag);
2497
2498 char *payload = NULL;
2499 size_t payload_len = 0;
2500 nvlist_t *nvl = fnvlist_alloc();
2501
2502 /*
2503 * If we're doing a redacted send, we include the snapshots we're
2504 * redacted with respect to so that the target system knows what send
2505 * streams can be correctly received on top of this dataset. If we're
2506 * instead sending a redacted dataset, we include the snapshots that the
2507 * dataset was created with respect to.
2508 */
2509 if (dspp->redactbook != NULL) {
2510 fnvlist_add_uint64_array(nvl, BEGINNV_REDACT_SNAPS,
2511 redact_rl->rl_phys->rlp_snaps,
2512 redact_rl->rl_phys->rlp_num_snaps);
2513 } else if (dsl_dataset_feature_is_active(to_ds,
2514 SPA_FEATURE_REDACTED_DATASETS)) {
2515 uint64_t *tods_guids;
2516 uint64_t length;
2517 VERIFY(dsl_dataset_get_uint64_array_feature(to_ds,
2518 SPA_FEATURE_REDACTED_DATASETS, &length, &tods_guids));
2519 fnvlist_add_uint64_array(nvl, BEGINNV_REDACT_SNAPS, tods_guids,
2520 length);
2521 }
2522
2523 /*
2524 * If we're sending from a redaction bookmark, then we should retrieve
2525 * the guids of that bookmark so we can send them over the wire.
2526 */
2527 if (from_rl != NULL) {
2528 fnvlist_add_uint64_array(nvl, BEGINNV_REDACT_FROM_SNAPS,
2529 from_rl->rl_phys->rlp_snaps,
2530 from_rl->rl_phys->rlp_num_snaps);
2531 }
2532
2533 /*
2534 * If the snapshot we're sending from is redacted, include the redaction
2535 * list in the stream.
2536 */
2537 if (dspp->numfromredactsnaps != NUM_SNAPS_NOT_REDACTED) {
2538 ASSERT0P(from_rl);
2539 fnvlist_add_uint64_array(nvl, BEGINNV_REDACT_FROM_SNAPS,
2540 dspp->fromredactsnaps, (uint_t)dspp->numfromredactsnaps);
2541 if (dspp->numfromredactsnaps > 0) {
2542 kmem_free(dspp->fromredactsnaps,
2543 dspp->numfromredactsnaps * sizeof (uint64_t));
2544 dspp->fromredactsnaps = NULL;
2545 }
2546 }
2547
2548 if (resuming || book_resuming) {
2549 err = setup_resume_points(dspp, to_arg, from_arg,
2550 rlt_arg, smt_arg, resuming, os, redact_rl, nvl);
2551 if (err != 0)
2552 goto out;
2553 }
2554
2555 if (featureflags & DMU_BACKUP_FEATURE_RAW) {
2556 uint64_t ivset_guid = ancestor_zb->zbm_ivset_guid;
2557 nvlist_t *keynvl = NULL;
2558 ASSERT(os->os_encrypted);
2559
2560 err = dsl_crypto_populate_key_nvlist(os, ivset_guid,
2561 &keynvl);
2562 if (err != 0) {
2563 fnvlist_free(nvl);
2564 goto out;
2565 }
2566
2567 fnvlist_add_nvlist(nvl, "crypt_keydata", keynvl);
2568 fnvlist_free(keynvl);
2569 }
2570
2571 if (!nvlist_empty(nvl)) {
2572 VERIFY0(nvlist_pack(nvl, &payload, &payload_len,
2573 NV_ENCODE_XDR, KM_SLEEP));
2574 pad_packed_nvlist(&payload, &payload_len);
2575 drr->drr_payloadlen = payload_len;
2576 }
2577
2578 fnvlist_free(nvl);
2579 err = dump_record(&dsc, payload, payload_len);
2580 fnvlist_pack_free(payload, payload_len);
2581 if (err != 0) {
2582 err = dsc.dsc_err;
2583 goto out;
2584 }
2585
2586 setup_to_thread(to_arg, os, dssp, fromtxg, dspp->rawok);
2587 setup_from_thread(from_arg, from_rl, dssp);
2588 setup_redact_list_thread(rlt_arg, dspp, redact_rl, dssp);
2589 setup_merge_thread(smt_arg, dspp, from_arg, to_arg, rlt_arg, os);
2590 setup_reader_thread(srt_arg, dspp, smt_arg, featureflags);
2591
2592 range = bqueue_dequeue(&srt_arg->q);
2593 while (err == 0 && !range->eos_marker) {
2594 err = do_dump(&dsc, range);
2595 range = get_next_range(&srt_arg->q, range);
2596 if (issig())
2597 err = SET_ERROR(EINTR);
2598 }
2599
2600 /*
2601 * If we hit an error or are interrupted, cancel our worker threads and
2602 * clear the queue of any pending records. The threads will pass the
2603 * cancel up the tree of worker threads, and each one will clean up any
2604 * pending records before exiting.
2605 */
2606 if (err != 0) {
2607 srt_arg->cancel = B_TRUE;
2608 while (!range->eos_marker) {
2609 range = get_next_range(&srt_arg->q, range);
2610 }
2611 }
2612 range_free(range);
2613
2614 bqueue_destroy(&srt_arg->q);
2615 bqueue_destroy(&smt_arg->q);
2616 if (dspp->redactbook != NULL)
2617 bqueue_destroy(&rlt_arg->q);
2618 bqueue_destroy(&to_arg->q);
2619 bqueue_destroy(&from_arg->q);
2620
2621 if (err == 0 && srt_arg->error != 0)
2622 err = srt_arg->error;
2623
2624 if (err != 0)
2625 goto out;
2626
2627 if (dsc.dsc_pending_op != PENDING_NONE)
2628 if (dump_record(&dsc, NULL, 0) != 0)
2629 err = SET_ERROR(EINTR);
2630
2631 if (err != 0) {
2632 if (err == EINTR && dsc.dsc_err != 0)
2633 err = dsc.dsc_err;
2634 goto out;
2635 }
2636
2637 /*
2638 * Send the DRR_END record if this is not a saved stream.
2639 * Otherwise, the omitted DRR_END record will signal to
2640 * the receive side that the stream is incomplete.
2641 */
2642 if (!dspp->savedok) {
2643 memset(drr, 0, sizeof (dmu_replay_record_t));
2644 drr->drr_type = DRR_END;
2645 drr->drr_u.drr_end.drr_checksum = dsc.dsc_zc;
2646 drr->drr_u.drr_end.drr_toguid = dsc.dsc_toguid;
2647
2648 if (dump_record(&dsc, NULL, 0) != 0)
2649 err = dsc.dsc_err;
2650 }
2651 out:
2652 mutex_enter(&to_ds->ds_sendstream_lock);
2653 list_remove(&to_ds->ds_sendstreams, dssp);
2654 mutex_exit(&to_ds->ds_sendstream_lock);
2655
2656 VERIFY(err != 0 || (dsc.dsc_sent_begin &&
2657 (dsc.dsc_sent_end || dspp->savedok)));
2658
2659 kmem_free(drr, sizeof (dmu_replay_record_t));
2660 kmem_free(dssp, sizeof (dmu_sendstatus_t));
2661 kmem_free(from_arg, sizeof (*from_arg));
2662 kmem_free(to_arg, sizeof (*to_arg));
2663 kmem_free(rlt_arg, sizeof (*rlt_arg));
2664 kmem_free(smt_arg, sizeof (*smt_arg));
2665 kmem_free(srt_arg, sizeof (*srt_arg));
2666
2667 dsl_dataset_long_rele(to_ds, FTAG);
2668 if (from_rl != NULL) {
2669 dsl_redaction_list_long_rele(from_rl, FTAG);
2670 dsl_redaction_list_rele(from_rl, FTAG);
2671 }
2672 if (redact_rl != NULL) {
2673 dsl_redaction_list_long_rele(redact_rl, FTAG);
2674 dsl_redaction_list_rele(redact_rl, FTAG);
2675 }
2676
2677 return (err);
2678 }
2679
2680 int
dmu_send_obj(const char * pool,uint64_t tosnap,uint64_t fromsnap,boolean_t embedok,boolean_t large_block_ok,boolean_t compressok,boolean_t rawok,boolean_t savedok,int outfd,offset_t * off,dmu_send_outparams_t * dsop)2681 dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
2682 boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
2683 boolean_t rawok, boolean_t savedok, int outfd, offset_t *off,
2684 dmu_send_outparams_t *dsop)
2685 {
2686 int err;
2687 dsl_dataset_t *fromds;
2688 ds_hold_flags_t dsflags;
2689 struct dmu_send_params dspp = {0};
2690 dspp.embedok = embedok;
2691 dspp.large_block_ok = large_block_ok;
2692 dspp.compressok = compressok;
2693 dspp.outfd = outfd;
2694 dspp.off = off;
2695 dspp.dso = dsop;
2696 dspp.tag = FTAG;
2697 dspp.rawok = rawok;
2698 dspp.savedok = savedok;
2699
2700 dsflags = (rawok) ? DS_HOLD_FLAG_NONE : DS_HOLD_FLAG_DECRYPT;
2701 err = dsl_pool_hold(pool, FTAG, &dspp.dp);
2702 if (err != 0)
2703 return (err);
2704
2705 err = dsl_dataset_hold_obj_flags(dspp.dp, tosnap, dsflags, FTAG,
2706 &dspp.to_ds);
2707 if (err != 0) {
2708 dsl_pool_rele(dspp.dp, FTAG);
2709 return (err);
2710 }
2711
2712 if (fromsnap != 0) {
2713 err = dsl_dataset_hold_obj(dspp.dp, fromsnap, FTAG, &fromds);
2714
2715 if (err != 0) {
2716 dsl_dataset_rele_flags(dspp.to_ds, dsflags, FTAG);
2717 dsl_pool_rele(dspp.dp, FTAG);
2718 return (err);
2719 }
2720 dspp.ancestor_zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
2721 dspp.ancestor_zb.zbm_creation_txg =
2722 dsl_dataset_phys(fromds)->ds_creation_txg;
2723 dspp.ancestor_zb.zbm_creation_time =
2724 dsl_dataset_phys(fromds)->ds_creation_time;
2725
2726 if (dsl_dataset_is_zapified(fromds)) {
2727 (void) zap_lookup(dspp.dp->dp_meta_objset,
2728 fromds->ds_object, DS_FIELD_IVSET_GUID, 8, 1,
2729 &dspp.ancestor_zb.zbm_ivset_guid);
2730 }
2731
2732 /* See dmu_send for the reasons behind this. */
2733 uint64_t *fromredact;
2734
2735 if (!dsl_dataset_get_uint64_array_feature(fromds,
2736 SPA_FEATURE_REDACTED_DATASETS,
2737 &dspp.numfromredactsnaps,
2738 &fromredact)) {
2739 dspp.numfromredactsnaps = NUM_SNAPS_NOT_REDACTED;
2740 } else if (dspp.numfromredactsnaps > 0) {
2741 uint64_t size = dspp.numfromredactsnaps *
2742 sizeof (uint64_t);
2743 dspp.fromredactsnaps = kmem_zalloc(size, KM_SLEEP);
2744 memcpy(dspp.fromredactsnaps, fromredact, size);
2745 }
2746
2747 boolean_t is_before =
2748 dsl_dataset_is_before(dspp.to_ds, fromds, 0);
2749 dspp.is_clone = (dspp.to_ds->ds_dir !=
2750 fromds->ds_dir);
2751 dsl_dataset_rele(fromds, FTAG);
2752 if (!is_before) {
2753 dsl_pool_rele(dspp.dp, FTAG);
2754 err = SET_ERROR(EXDEV);
2755 } else {
2756 err = dmu_send_impl(&dspp);
2757 }
2758 } else {
2759 dspp.numfromredactsnaps = NUM_SNAPS_NOT_REDACTED;
2760 err = dmu_send_impl(&dspp);
2761 }
2762 if (dspp.fromredactsnaps)
2763 kmem_free(dspp.fromredactsnaps,
2764 dspp.numfromredactsnaps * sizeof (uint64_t));
2765
2766 dsl_dataset_rele_flags(dspp.to_ds, dsflags, FTAG);
2767 return (err);
2768 }
2769
2770 int
dmu_send(const char * tosnap,const char * fromsnap,boolean_t embedok,boolean_t large_block_ok,boolean_t compressok,boolean_t rawok,boolean_t savedok,uint64_t resumeobj,uint64_t resumeoff,const char * redactbook,int outfd,offset_t * off,dmu_send_outparams_t * dsop)2771 dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
2772 boolean_t large_block_ok, boolean_t compressok, boolean_t rawok,
2773 boolean_t savedok, uint64_t resumeobj, uint64_t resumeoff,
2774 const char *redactbook, int outfd, offset_t *off,
2775 dmu_send_outparams_t *dsop)
2776 {
2777 int err = 0;
2778 ds_hold_flags_t dsflags;
2779 boolean_t owned = B_FALSE;
2780 dsl_dataset_t *fromds = NULL;
2781 zfs_bookmark_phys_t book = {0};
2782 struct dmu_send_params dspp = {0};
2783
2784 dsflags = (rawok) ? DS_HOLD_FLAG_NONE : DS_HOLD_FLAG_DECRYPT;
2785 dspp.tosnap = tosnap;
2786 dspp.embedok = embedok;
2787 dspp.large_block_ok = large_block_ok;
2788 dspp.compressok = compressok;
2789 dspp.outfd = outfd;
2790 dspp.off = off;
2791 dspp.dso = dsop;
2792 dspp.tag = FTAG;
2793 dspp.resumeobj = resumeobj;
2794 dspp.resumeoff = resumeoff;
2795 dspp.rawok = rawok;
2796 dspp.savedok = savedok;
2797
2798 if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
2799 return (SET_ERROR(EINVAL));
2800
2801 err = dsl_pool_hold(tosnap, FTAG, &dspp.dp);
2802 if (err != 0)
2803 return (err);
2804
2805 if (strchr(tosnap, '@') == NULL && spa_writeable(dspp.dp->dp_spa)) {
2806 /*
2807 * We are sending a filesystem or volume. Ensure
2808 * that it doesn't change by owning the dataset.
2809 */
2810
2811 if (savedok) {
2812 /*
2813 * We are looking for the dataset that represents the
2814 * partially received send stream. If this stream was
2815 * received as a new snapshot of an existing dataset,
2816 * this will be saved in a hidden clone named
2817 * "<pool>/<dataset>/%recv". Otherwise, the stream
2818 * will be saved in the live dataset itself. In
2819 * either case we need to use dsl_dataset_own_force()
2820 * because the stream is marked as inconsistent,
2821 * which would normally make it unavailable to be
2822 * owned.
2823 */
2824 char *name = kmem_asprintf("%s/%s", tosnap,
2825 recv_clone_name);
2826 err = dsl_dataset_own_force(dspp.dp, name, dsflags,
2827 FTAG, &dspp.to_ds);
2828 if (err == ENOENT) {
2829 err = dsl_dataset_own_force(dspp.dp, tosnap,
2830 dsflags, FTAG, &dspp.to_ds);
2831 }
2832
2833 if (err == 0) {
2834 owned = B_TRUE;
2835 err = zap_lookup(dspp.dp->dp_meta_objset,
2836 dspp.to_ds->ds_object,
2837 DS_FIELD_RESUME_TOGUID, 8, 1,
2838 &dspp.saved_guid);
2839 }
2840
2841 if (err == 0) {
2842 err = zap_lookup(dspp.dp->dp_meta_objset,
2843 dspp.to_ds->ds_object,
2844 DS_FIELD_RESUME_TONAME, 1,
2845 sizeof (dspp.saved_toname),
2846 dspp.saved_toname);
2847 }
2848 /* Only disown if there was an error in the lookups */
2849 if (owned && (err != 0))
2850 dsl_dataset_disown(dspp.to_ds, dsflags, FTAG);
2851
2852 kmem_strfree(name);
2853 } else {
2854 err = dsl_dataset_own(dspp.dp, tosnap, dsflags,
2855 FTAG, &dspp.to_ds);
2856 if (err == 0)
2857 owned = B_TRUE;
2858 }
2859 } else {
2860 err = dsl_dataset_hold_flags(dspp.dp, tosnap, dsflags, FTAG,
2861 &dspp.to_ds);
2862 }
2863
2864 if (err != 0) {
2865 /* Note: dsl dataset is not owned at this point */
2866 dsl_pool_rele(dspp.dp, FTAG);
2867 return (err);
2868 }
2869
2870 if (redactbook != NULL) {
2871 char path[ZFS_MAX_DATASET_NAME_LEN];
2872 (void) strlcpy(path, tosnap, sizeof (path));
2873 char *at = strchr(path, '@');
2874 if (at == NULL) {
2875 err = EINVAL;
2876 } else {
2877 (void) snprintf(at, sizeof (path) - (at - path), "#%s",
2878 redactbook);
2879 err = dsl_bookmark_lookup(dspp.dp, path,
2880 NULL, &book);
2881 dspp.redactbook = &book;
2882 }
2883 }
2884
2885 if (err != 0) {
2886 dsl_pool_rele(dspp.dp, FTAG);
2887 if (owned)
2888 dsl_dataset_disown(dspp.to_ds, dsflags, FTAG);
2889 else
2890 dsl_dataset_rele_flags(dspp.to_ds, dsflags, FTAG);
2891 return (err);
2892 }
2893
2894 if (fromsnap != NULL) {
2895 zfs_bookmark_phys_t *zb = &dspp.ancestor_zb;
2896 int fsnamelen;
2897 if (strpbrk(tosnap, "@#") != NULL)
2898 fsnamelen = strpbrk(tosnap, "@#") - tosnap;
2899 else
2900 fsnamelen = strlen(tosnap);
2901
2902 /*
2903 * If the fromsnap is in a different filesystem, then
2904 * mark the send stream as a clone.
2905 */
2906 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
2907 (fromsnap[fsnamelen] != '@' &&
2908 fromsnap[fsnamelen] != '#')) {
2909 dspp.is_clone = B_TRUE;
2910 }
2911
2912 if (strchr(fromsnap, '@') != NULL) {
2913 err = dsl_dataset_hold(dspp.dp, fromsnap, FTAG,
2914 &fromds);
2915
2916 if (err != 0) {
2917 ASSERT0P(fromds);
2918 } else {
2919 /*
2920 * We need to make a deep copy of the redact
2921 * snapshots of the from snapshot, because the
2922 * array will be freed when we evict from_ds.
2923 */
2924 uint64_t *fromredact;
2925 if (!dsl_dataset_get_uint64_array_feature(
2926 fromds, SPA_FEATURE_REDACTED_DATASETS,
2927 &dspp.numfromredactsnaps,
2928 &fromredact)) {
2929 dspp.numfromredactsnaps =
2930 NUM_SNAPS_NOT_REDACTED;
2931 } else if (dspp.numfromredactsnaps > 0) {
2932 uint64_t size =
2933 dspp.numfromredactsnaps *
2934 sizeof (uint64_t);
2935 dspp.fromredactsnaps = kmem_zalloc(size,
2936 KM_SLEEP);
2937 memcpy(dspp.fromredactsnaps, fromredact,
2938 size);
2939 }
2940 if (!dsl_dataset_is_before(dspp.to_ds, fromds,
2941 0)) {
2942 err = SET_ERROR(EXDEV);
2943 } else {
2944 zb->zbm_creation_txg =
2945 dsl_dataset_phys(fromds)->
2946 ds_creation_txg;
2947 zb->zbm_creation_time =
2948 dsl_dataset_phys(fromds)->
2949 ds_creation_time;
2950 zb->zbm_guid =
2951 dsl_dataset_phys(fromds)->ds_guid;
2952 zb->zbm_redaction_obj = 0;
2953
2954 if (dsl_dataset_is_zapified(fromds)) {
2955 (void) zap_lookup(
2956 dspp.dp->dp_meta_objset,
2957 fromds->ds_object,
2958 DS_FIELD_IVSET_GUID, 8, 1,
2959 &zb->zbm_ivset_guid);
2960 }
2961 }
2962 dsl_dataset_rele(fromds, FTAG);
2963 }
2964 } else {
2965 dspp.numfromredactsnaps = NUM_SNAPS_NOT_REDACTED;
2966 err = dsl_bookmark_lookup(dspp.dp, fromsnap, dspp.to_ds,
2967 zb);
2968 if (err == EXDEV && zb->zbm_redaction_obj != 0 &&
2969 zb->zbm_guid ==
2970 dsl_dataset_phys(dspp.to_ds)->ds_guid)
2971 err = 0;
2972 }
2973
2974 if (err == 0) {
2975 /* dmu_send_impl will call dsl_pool_rele for us. */
2976 err = dmu_send_impl(&dspp);
2977 } else {
2978 if (dspp.fromredactsnaps)
2979 kmem_free(dspp.fromredactsnaps,
2980 dspp.numfromredactsnaps *
2981 sizeof (uint64_t));
2982 dsl_pool_rele(dspp.dp, FTAG);
2983 }
2984 } else {
2985 dspp.numfromredactsnaps = NUM_SNAPS_NOT_REDACTED;
2986 err = dmu_send_impl(&dspp);
2987 }
2988 if (owned)
2989 dsl_dataset_disown(dspp.to_ds, dsflags, FTAG);
2990 else
2991 dsl_dataset_rele_flags(dspp.to_ds, dsflags, FTAG);
2992 return (err);
2993 }
2994
2995 static int
dmu_adjust_send_estimate_for_indirects(dsl_dataset_t * ds,uint64_t uncompressed,uint64_t compressed,boolean_t stream_compressed,uint64_t * sizep)2996 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed,
2997 uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep)
2998 {
2999 int err = 0;
3000 uint64_t size;
3001 /*
3002 * Assume that space (both on-disk and in-stream) is dominated by
3003 * data. We will adjust for indirect blocks and the copies property,
3004 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
3005 */
3006
3007 uint64_t recordsize;
3008 uint64_t record_count;
3009 objset_t *os;
3010 VERIFY0(dmu_objset_from_ds(ds, &os));
3011
3012 /* Assume all (uncompressed) blocks are recordsize. */
3013 if (zfs_override_estimate_recordsize != 0) {
3014 recordsize = zfs_override_estimate_recordsize;
3015 } else if (os->os_phys->os_type == DMU_OST_ZVOL) {
3016 err = dsl_prop_get_int_ds(ds,
3017 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &recordsize);
3018 } else {
3019 err = dsl_prop_get_int_ds(ds,
3020 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), &recordsize);
3021 }
3022 if (err != 0)
3023 return (err);
3024 record_count = uncompressed / recordsize;
3025
3026 /*
3027 * If we're estimating a send size for a compressed stream, use the
3028 * compressed data size to estimate the stream size. Otherwise, use the
3029 * uncompressed data size.
3030 */
3031 size = stream_compressed ? compressed : uncompressed;
3032
3033 /*
3034 * Subtract out approximate space used by indirect blocks.
3035 * Assume most space is used by data blocks (non-indirect, non-dnode).
3036 * Assume no ditto blocks or internal fragmentation.
3037 *
3038 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
3039 * block.
3040 */
3041 size -= record_count * sizeof (blkptr_t);
3042
3043 /* Add in the space for the record associated with each block. */
3044 size += record_count * sizeof (dmu_replay_record_t);
3045
3046 *sizep = size;
3047
3048 return (0);
3049 }
3050
3051 int
dmu_send_estimate_fast(dsl_dataset_t * origds,dsl_dataset_t * fromds,zfs_bookmark_phys_t * frombook,boolean_t stream_compressed,boolean_t saved,uint64_t * sizep)3052 dmu_send_estimate_fast(dsl_dataset_t *origds, dsl_dataset_t *fromds,
3053 zfs_bookmark_phys_t *frombook, boolean_t stream_compressed,
3054 boolean_t saved, uint64_t *sizep)
3055 {
3056 int err;
3057 dsl_dataset_t *ds = origds;
3058 uint64_t uncomp, comp;
3059
3060 ASSERT(dsl_pool_config_held(origds->ds_dir->dd_pool));
3061 ASSERT(fromds == NULL || frombook == NULL);
3062
3063 /*
3064 * If this is a saved send we may actually be sending
3065 * from the %recv clone used for resuming.
3066 */
3067 if (saved) {
3068 objset_t *mos = origds->ds_dir->dd_pool->dp_meta_objset;
3069 uint64_t guid;
3070 char dsname[ZFS_MAX_DATASET_NAME_LEN + 6];
3071
3072 dsl_dataset_name(origds, dsname);
3073 (void) strcat(dsname, "/");
3074 (void) strlcat(dsname, recv_clone_name, sizeof (dsname));
3075
3076 err = dsl_dataset_hold(origds->ds_dir->dd_pool,
3077 dsname, FTAG, &ds);
3078 if (err != ENOENT && err != 0) {
3079 return (err);
3080 } else if (err == ENOENT) {
3081 ds = origds;
3082 }
3083
3084 /* check that this dataset has partially received data */
3085 err = zap_lookup(mos, ds->ds_object,
3086 DS_FIELD_RESUME_TOGUID, 8, 1, &guid);
3087 if (err != 0) {
3088 err = SET_ERROR(err == ENOENT ? EINVAL : err);
3089 goto out;
3090 }
3091
3092 err = zap_lookup(mos, ds->ds_object,
3093 DS_FIELD_RESUME_TONAME, 1, sizeof (dsname), dsname);
3094 if (err != 0) {
3095 err = SET_ERROR(err == ENOENT ? EINVAL : err);
3096 goto out;
3097 }
3098 }
3099
3100 /* tosnap must be a snapshot or the target of a saved send */
3101 if (!ds->ds_is_snapshot && ds == origds)
3102 return (SET_ERROR(EINVAL));
3103
3104 if (fromds != NULL) {
3105 uint64_t used;
3106 if (!fromds->ds_is_snapshot) {
3107 err = SET_ERROR(EINVAL);
3108 goto out;
3109 }
3110
3111 if (!dsl_dataset_is_before(ds, fromds, 0)) {
3112 err = SET_ERROR(EXDEV);
3113 goto out;
3114 }
3115
3116 err = dsl_dataset_space_written(fromds, ds, &used, &comp,
3117 &uncomp);
3118 if (err != 0)
3119 goto out;
3120 } else if (frombook != NULL) {
3121 uint64_t used;
3122 err = dsl_dataset_space_written_bookmark(frombook, ds, &used,
3123 &comp, &uncomp);
3124 if (err != 0)
3125 goto out;
3126 } else {
3127 uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
3128 comp = dsl_dataset_phys(ds)->ds_compressed_bytes;
3129 }
3130
3131 err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp,
3132 stream_compressed, sizep);
3133 /*
3134 * Add the size of the BEGIN and END records to the estimate.
3135 */
3136 *sizep += 2 * sizeof (dmu_replay_record_t);
3137
3138 out:
3139 if (ds != origds)
3140 dsl_dataset_rele(ds, FTAG);
3141 return (err);
3142 }
3143
3144 ZFS_MODULE_PARAM(zfs_send, zfs_send_, corrupt_data, INT, ZMOD_RW,
3145 "Allow sending corrupt data");
3146
3147 ZFS_MODULE_PARAM(zfs_send, zfs_send_, queue_length, UINT, ZMOD_RW,
3148 "Maximum send queue length");
3149
3150 ZFS_MODULE_PARAM(zfs_send, zfs_send_, unmodified_spill_blocks, INT, ZMOD_RW,
3151 "Send unmodified spill blocks");
3152
3153 ZFS_MODULE_PARAM(zfs_send, zfs_send_, no_prefetch_queue_length, UINT, ZMOD_RW,
3154 "Maximum send queue length for non-prefetch queues");
3155
3156 ZFS_MODULE_PARAM(zfs_send, zfs_send_, queue_ff, UINT, ZMOD_RW,
3157 "Send queue fill fraction");
3158
3159 ZFS_MODULE_PARAM(zfs_send, zfs_send_, no_prefetch_queue_ff, UINT, ZMOD_RW,
3160 "Send queue fill fraction for non-prefetch queues");
3161
3162 ZFS_MODULE_PARAM(zfs_send, zfs_, override_estimate_recordsize, UINT, ZMOD_RW,
3163 "Override block size estimate with fixed size");
3164