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