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