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.
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.
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]
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 (c) 2012, Martin Matuska <mm@FreeBSD.org>. All rights reserved.
27 * Copyright 2014 HybridCluster. All rights reserved.
28 * Copyright 2016 RackTop Systems.
29 * Copyright (c) 2014 Integros [integros.com]
33 #include <sys/dmu_impl.h>
34 #include <sys/dmu_tx.h>
36 #include <sys/dnode.h>
37 #include <sys/zfs_context.h>
38 #include <sys/dmu_objset.h>
39 #include <sys/dmu_traverse.h>
40 #include <sys/dsl_dataset.h>
41 #include <sys/dsl_dir.h>
42 #include <sys/dsl_prop.h>
43 #include <sys/dsl_pool.h>
44 #include <sys/dsl_synctask.h>
45 #include <sys/zfs_ioctl.h>
47 #include <sys/zio_checksum.h>
48 #include <sys/zfs_znode.h>
49 #include <zfs_fletcher.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>
62 #define dump_write dmu_dump_write
65 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
66 int zfs_send_corrupt_data = B_FALSE;
67 int zfs_send_queue_length = 16 * 1024 * 1024;
68 int zfs_recv_queue_length = 16 * 1024 * 1024;
69 /* Set this tunable to FALSE to disable setting of DRR_FLAG_FREERECORDS */
70 int zfs_send_set_freerecords_bit = B_TRUE;
73 TUNABLE_INT("vfs.zfs.send_set_freerecords_bit", &zfs_send_set_freerecords_bit);
76 static char *dmu_recv_tag = "dmu_recv_tag";
77 const char *recv_clone_name = "%recv";
80 * Use this to override the recordsize calculation for fast zfs send estimates.
82 uint64_t zfs_override_estimate_recordsize = 0;
84 #define BP_SPAN(datablkszsec, indblkshift, level) \
85 (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
86 (level) * (indblkshift - SPA_BLKPTRSHIFT)))
88 static void byteswap_record(dmu_replay_record_t *drr);
90 struct send_thread_arg {
92 dsl_dataset_t *ds; /* Dataset to traverse */
93 uint64_t fromtxg; /* Traverse from this txg */
94 int flags; /* flags to pass to traverse_dataset */
97 zbookmark_phys_t resume;
100 struct send_block_record {
101 boolean_t eos_marker; /* Marks the end of the stream */
105 uint16_t datablkszsec;
110 dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
112 dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
117 * The code does not rely on this (len being a multiple of 8). We keep
118 * this assertion because of the corresponding assertion in
119 * receive_read(). Keeping this assertion ensures that we do not
120 * inadvertently break backwards compatibility (causing the assertion
121 * in receive_read() to trigger on old software).
123 * Removing the assertions could be rolled into a new feature that uses
124 * data that isn't 8-byte aligned; if the assertions were removed, a
125 * feature flag would have to be added.
132 auio.uio_iov = &aiov;
134 auio.uio_resid = len;
135 auio.uio_segflg = UIO_SYSSPACE;
136 auio.uio_rw = UIO_WRITE;
137 auio.uio_offset = (off_t)-1;
138 auio.uio_td = dsp->dsa_td;
140 if (dsp->dsa_fp->f_type == DTYPE_VNODE)
142 dsp->dsa_err = fo_write(dsp->dsa_fp, &auio, dsp->dsa_td->td_ucred, 0,
145 fprintf(stderr, "%s: returning EOPNOTSUPP\n", __func__);
146 dsp->dsa_err = EOPNOTSUPP;
148 mutex_enter(&ds->ds_sendstream_lock);
149 *dsp->dsa_off += len;
150 mutex_exit(&ds->ds_sendstream_lock);
152 return (dsp->dsa_err);
156 * For all record types except BEGIN, fill in the checksum (overlaid in
157 * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
158 * up to the start of the checksum itself.
161 dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
163 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
164 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
165 (void) fletcher_4_incremental_native(dsp->dsa_drr,
166 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
168 if (dsp->dsa_drr->drr_type == DRR_BEGIN) {
169 dsp->dsa_sent_begin = B_TRUE;
171 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
172 drr_checksum.drr_checksum));
173 dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
175 if (dsp->dsa_drr->drr_type == DRR_END) {
176 dsp->dsa_sent_end = B_TRUE;
178 (void) fletcher_4_incremental_native(&dsp->dsa_drr->
179 drr_u.drr_checksum.drr_checksum,
180 sizeof (zio_cksum_t), &dsp->dsa_zc);
181 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
182 return (SET_ERROR(EINTR));
183 if (payload_len != 0) {
184 (void) fletcher_4_incremental_native(payload, payload_len,
186 if (dump_bytes(dsp, payload, payload_len) != 0)
187 return (SET_ERROR(EINTR));
193 * Fill in the drr_free struct, or perform aggregation if the previous record is
194 * also a free record, and the two are adjacent.
196 * Note that we send free records even for a full send, because we want to be
197 * able to receive a full send as a clone, which requires a list of all the free
198 * and freeobject records that were generated on the source.
201 dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
204 struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
207 * When we receive a free record, dbuf_free_range() assumes
208 * that the receiving system doesn't have any dbufs in the range
209 * being freed. This is always true because there is a one-record
210 * constraint: we only send one WRITE record for any given
211 * object,offset. We know that the one-record constraint is
212 * true because we always send data in increasing order by
215 * If the increasing-order constraint ever changes, we should find
216 * another way to assert that the one-record constraint is still
219 ASSERT(object > dsp->dsa_last_data_object ||
220 (object == dsp->dsa_last_data_object &&
221 offset > dsp->dsa_last_data_offset));
223 if (length != -1ULL && offset + length < offset)
227 * If there is a pending op, but it's not PENDING_FREE, push it out,
228 * since free block aggregation can only be done for blocks of the
229 * same type (i.e., DRR_FREE records can only be aggregated with
230 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
231 * aggregated with other DRR_FREEOBJECTS records.
233 if (dsp->dsa_pending_op != PENDING_NONE &&
234 dsp->dsa_pending_op != PENDING_FREE) {
235 if (dump_record(dsp, NULL, 0) != 0)
236 return (SET_ERROR(EINTR));
237 dsp->dsa_pending_op = PENDING_NONE;
240 if (dsp->dsa_pending_op == PENDING_FREE) {
242 * There should never be a PENDING_FREE if length is -1
243 * (because dump_dnode is the only place where this
244 * function is called with a -1, and only after flushing
245 * any pending record).
247 ASSERT(length != -1ULL);
249 * Check to see whether this free block can be aggregated
252 if (drrf->drr_object == object && drrf->drr_offset +
253 drrf->drr_length == offset) {
254 drrf->drr_length += length;
257 /* not a continuation. Push out pending record */
258 if (dump_record(dsp, NULL, 0) != 0)
259 return (SET_ERROR(EINTR));
260 dsp->dsa_pending_op = PENDING_NONE;
263 /* create a FREE record and make it pending */
264 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
265 dsp->dsa_drr->drr_type = DRR_FREE;
266 drrf->drr_object = object;
267 drrf->drr_offset = offset;
268 drrf->drr_length = length;
269 drrf->drr_toguid = dsp->dsa_toguid;
270 if (length == -1ULL) {
271 if (dump_record(dsp, NULL, 0) != 0)
272 return (SET_ERROR(EINTR));
274 dsp->dsa_pending_op = PENDING_FREE;
281 dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
282 uint64_t object, uint64_t offset, int lsize, int psize, const blkptr_t *bp,
285 uint64_t payload_size;
286 struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
289 * We send data in increasing object, offset order.
290 * See comment in dump_free() for details.
292 ASSERT(object > dsp->dsa_last_data_object ||
293 (object == dsp->dsa_last_data_object &&
294 offset > dsp->dsa_last_data_offset));
295 dsp->dsa_last_data_object = object;
296 dsp->dsa_last_data_offset = offset + lsize - 1;
299 * If there is any kind of pending aggregation (currently either
300 * a grouping of free objects or free blocks), push it out to
301 * the stream, since aggregation can't be done across operations
302 * of different types.
304 if (dsp->dsa_pending_op != PENDING_NONE) {
305 if (dump_record(dsp, NULL, 0) != 0)
306 return (SET_ERROR(EINTR));
307 dsp->dsa_pending_op = PENDING_NONE;
309 /* write a WRITE record */
310 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
311 dsp->dsa_drr->drr_type = DRR_WRITE;
312 drrw->drr_object = object;
313 drrw->drr_type = type;
314 drrw->drr_offset = offset;
315 drrw->drr_toguid = dsp->dsa_toguid;
316 drrw->drr_logical_size = lsize;
318 /* only set the compression fields if the buf is compressed */
319 if (lsize != psize) {
320 ASSERT(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED);
321 ASSERT(!BP_IS_EMBEDDED(bp));
322 ASSERT(!BP_SHOULD_BYTESWAP(bp));
323 ASSERT(!DMU_OT_IS_METADATA(BP_GET_TYPE(bp)));
324 ASSERT3U(BP_GET_COMPRESS(bp), !=, ZIO_COMPRESS_OFF);
325 ASSERT3S(psize, >, 0);
326 ASSERT3S(lsize, >=, psize);
328 drrw->drr_compressiontype = BP_GET_COMPRESS(bp);
329 drrw->drr_compressed_size = psize;
330 payload_size = drrw->drr_compressed_size;
332 payload_size = drrw->drr_logical_size;
335 if (bp == NULL || BP_IS_EMBEDDED(bp)) {
337 * There's no pre-computed checksum for partial-block
338 * writes or embedded BP's, so (like
339 * fletcher4-checkummed blocks) userland will have to
340 * compute a dedup-capable checksum itself.
342 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
344 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
345 if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
346 ZCHECKSUM_FLAG_DEDUP)
347 drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
348 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
349 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
350 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
351 drrw->drr_key.ddk_cksum = bp->blk_cksum;
354 if (dump_record(dsp, data, payload_size) != 0)
355 return (SET_ERROR(EINTR));
360 dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
361 int blksz, const blkptr_t *bp)
363 char buf[BPE_PAYLOAD_SIZE];
364 struct drr_write_embedded *drrw =
365 &(dsp->dsa_drr->drr_u.drr_write_embedded);
367 if (dsp->dsa_pending_op != PENDING_NONE) {
368 if (dump_record(dsp, NULL, 0) != 0)
370 dsp->dsa_pending_op = PENDING_NONE;
373 ASSERT(BP_IS_EMBEDDED(bp));
375 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
376 dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
377 drrw->drr_object = object;
378 drrw->drr_offset = offset;
379 drrw->drr_length = blksz;
380 drrw->drr_toguid = dsp->dsa_toguid;
381 drrw->drr_compression = BP_GET_COMPRESS(bp);
382 drrw->drr_etype = BPE_GET_ETYPE(bp);
383 drrw->drr_lsize = BPE_GET_LSIZE(bp);
384 drrw->drr_psize = BPE_GET_PSIZE(bp);
386 decode_embedded_bp_compressed(bp, buf);
388 if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
394 dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
396 struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
398 if (dsp->dsa_pending_op != PENDING_NONE) {
399 if (dump_record(dsp, NULL, 0) != 0)
400 return (SET_ERROR(EINTR));
401 dsp->dsa_pending_op = PENDING_NONE;
404 /* write a SPILL record */
405 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
406 dsp->dsa_drr->drr_type = DRR_SPILL;
407 drrs->drr_object = object;
408 drrs->drr_length = blksz;
409 drrs->drr_toguid = dsp->dsa_toguid;
411 if (dump_record(dsp, data, blksz) != 0)
412 return (SET_ERROR(EINTR));
417 dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
419 struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
422 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
423 * push it out, since free block aggregation can only be done for
424 * blocks of the same type (i.e., DRR_FREE records can only be
425 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
426 * can only be aggregated with other DRR_FREEOBJECTS records.
428 if (dsp->dsa_pending_op != PENDING_NONE &&
429 dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
430 if (dump_record(dsp, NULL, 0) != 0)
431 return (SET_ERROR(EINTR));
432 dsp->dsa_pending_op = PENDING_NONE;
434 if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
436 * See whether this free object array can be aggregated
439 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
440 drrfo->drr_numobjs += numobjs;
443 /* can't be aggregated. Push out pending record */
444 if (dump_record(dsp, NULL, 0) != 0)
445 return (SET_ERROR(EINTR));
446 dsp->dsa_pending_op = PENDING_NONE;
450 /* write a FREEOBJECTS record */
451 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
452 dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
453 drrfo->drr_firstobj = firstobj;
454 drrfo->drr_numobjs = numobjs;
455 drrfo->drr_toguid = dsp->dsa_toguid;
457 dsp->dsa_pending_op = PENDING_FREEOBJECTS;
463 dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
465 struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
467 if (object < dsp->dsa_resume_object) {
469 * Note: when resuming, we will visit all the dnodes in
470 * the block of dnodes that we are resuming from. In
471 * this case it's unnecessary to send the dnodes prior to
472 * the one we are resuming from. We should be at most one
473 * block's worth of dnodes behind the resume point.
475 ASSERT3U(dsp->dsa_resume_object - object, <,
476 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
480 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
481 return (dump_freeobjects(dsp, object, 1));
483 if (dsp->dsa_pending_op != PENDING_NONE) {
484 if (dump_record(dsp, NULL, 0) != 0)
485 return (SET_ERROR(EINTR));
486 dsp->dsa_pending_op = PENDING_NONE;
489 /* write an OBJECT record */
490 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
491 dsp->dsa_drr->drr_type = DRR_OBJECT;
492 drro->drr_object = object;
493 drro->drr_type = dnp->dn_type;
494 drro->drr_bonustype = dnp->dn_bonustype;
495 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
496 drro->drr_bonuslen = dnp->dn_bonuslen;
497 drro->drr_checksumtype = dnp->dn_checksum;
498 drro->drr_compress = dnp->dn_compress;
499 drro->drr_toguid = dsp->dsa_toguid;
501 if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
502 drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
503 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
505 if (dump_record(dsp, DN_BONUS(dnp),
506 P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
507 return (SET_ERROR(EINTR));
510 /* Free anything past the end of the file. */
511 if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
512 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
513 return (SET_ERROR(EINTR));
514 if (dsp->dsa_err != 0)
515 return (SET_ERROR(EINTR));
520 backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
522 if (!BP_IS_EMBEDDED(bp))
526 * Compression function must be legacy, or explicitly enabled.
528 if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
529 !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LZ4)))
533 * Embed type must be explicitly enabled.
535 switch (BPE_GET_ETYPE(bp)) {
536 case BP_EMBEDDED_TYPE_DATA:
537 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
547 * This is the callback function to traverse_dataset that acts as the worker
548 * thread for dmu_send_impl.
552 send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
553 const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
555 struct send_thread_arg *sta = arg;
556 struct send_block_record *record;
557 uint64_t record_size;
560 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
561 zb->zb_object >= sta->resume.zb_object);
564 return (SET_ERROR(EINTR));
567 ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
569 } else if (zb->zb_level < 0) {
573 record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
574 record->eos_marker = B_FALSE;
577 record->indblkshift = dnp->dn_indblkshift;
578 record->datablkszsec = dnp->dn_datablkszsec;
579 record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
580 bqueue_enqueue(&sta->q, record, record_size);
586 * This function kicks off the traverse_dataset. It also handles setting the
587 * error code of the thread in case something goes wrong, and pushes the End of
588 * Stream record when the traverse_dataset call has finished. If there is no
589 * dataset to traverse, the thread immediately pushes End of Stream marker.
592 send_traverse_thread(void *arg)
594 struct send_thread_arg *st_arg = arg;
596 struct send_block_record *data;
598 if (st_arg->ds != NULL) {
599 err = traverse_dataset_resume(st_arg->ds,
600 st_arg->fromtxg, &st_arg->resume,
601 st_arg->flags, send_cb, st_arg);
604 st_arg->error_code = err;
606 data = kmem_zalloc(sizeof (*data), KM_SLEEP);
607 data->eos_marker = B_TRUE;
608 bqueue_enqueue(&st_arg->q, data, 1);
613 * This function actually handles figuring out what kind of record needs to be
614 * dumped, reading the data (which has hopefully been prefetched), and calling
615 * the appropriate helper function.
618 do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
620 dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
621 const blkptr_t *bp = &data->bp;
622 const zbookmark_phys_t *zb = &data->zb;
623 uint8_t indblkshift = data->indblkshift;
624 uint16_t dblkszsec = data->datablkszsec;
625 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
626 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
629 ASSERT3U(zb->zb_level, >=, 0);
631 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
632 zb->zb_object >= dsa->dsa_resume_object);
634 if (zb->zb_object != DMU_META_DNODE_OBJECT &&
635 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
637 } else if (BP_IS_HOLE(bp) &&
638 zb->zb_object == DMU_META_DNODE_OBJECT) {
639 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
640 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
641 err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
642 } else if (BP_IS_HOLE(bp)) {
643 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
644 uint64_t offset = zb->zb_blkid * span;
645 err = dump_free(dsa, zb->zb_object, offset, span);
646 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
648 } else if (type == DMU_OT_DNODE) {
649 int blksz = BP_GET_LSIZE(bp);
650 arc_flags_t aflags = ARC_FLAG_WAIT;
653 ASSERT0(zb->zb_level);
655 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
656 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
658 return (SET_ERROR(EIO));
660 dnode_phys_t *blk = abuf->b_data;
661 uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
662 for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
663 err = dump_dnode(dsa, dnobj + i, blk + i);
667 arc_buf_destroy(abuf, &abuf);
668 } else if (type == DMU_OT_SA) {
669 arc_flags_t aflags = ARC_FLAG_WAIT;
671 int blksz = BP_GET_LSIZE(bp);
673 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
674 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
676 return (SET_ERROR(EIO));
678 err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
679 arc_buf_destroy(abuf, &abuf);
680 } else if (backup_do_embed(dsa, bp)) {
681 /* it's an embedded level-0 block of a regular object */
682 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
683 ASSERT0(zb->zb_level);
684 err = dump_write_embedded(dsa, zb->zb_object,
685 zb->zb_blkid * blksz, blksz, bp);
687 /* it's a level-0 block of a regular object */
688 arc_flags_t aflags = ARC_FLAG_WAIT;
690 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
694 * If we have large blocks stored on disk but the send flags
695 * don't allow us to send large blocks, we split the data from
696 * the arc buf into chunks.
698 boolean_t split_large_blocks = blksz > SPA_OLD_MAXBLOCKSIZE &&
699 !(dsa->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS);
701 * We should only request compressed data from the ARC if all
702 * the following are true:
703 * - stream compression was requested
704 * - we aren't splitting large blocks into smaller chunks
705 * - the data won't need to be byteswapped before sending
706 * - this isn't an embedded block
707 * - this isn't metadata (if receiving on a different endian
708 * system it can be byteswapped more easily)
710 boolean_t request_compressed =
711 (dsa->dsa_featureflags & DMU_BACKUP_FEATURE_COMPRESSED) &&
712 !split_large_blocks && !BP_SHOULD_BYTESWAP(bp) &&
713 !BP_IS_EMBEDDED(bp) && !DMU_OT_IS_METADATA(BP_GET_TYPE(bp));
715 ASSERT0(zb->zb_level);
716 ASSERT(zb->zb_object > dsa->dsa_resume_object ||
717 (zb->zb_object == dsa->dsa_resume_object &&
718 zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
720 ASSERT0(zb->zb_level);
721 ASSERT(zb->zb_object > dsa->dsa_resume_object ||
722 (zb->zb_object == dsa->dsa_resume_object &&
723 zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
725 ASSERT3U(blksz, ==, BP_GET_LSIZE(bp));
727 enum zio_flag zioflags = ZIO_FLAG_CANFAIL;
728 if (request_compressed)
729 zioflags |= ZIO_FLAG_RAW;
730 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
731 ZIO_PRIORITY_ASYNC_READ, zioflags, &aflags, zb) != 0) {
732 if (zfs_send_corrupt_data) {
733 /* Send a block filled with 0x"zfs badd bloc" */
734 abuf = arc_alloc_buf(spa, &abuf, ARC_BUFC_DATA,
737 for (ptr = abuf->b_data;
738 (char *)ptr < (char *)abuf->b_data + blksz;
740 *ptr = 0x2f5baddb10cULL;
742 return (SET_ERROR(EIO));
746 offset = zb->zb_blkid * blksz;
748 if (split_large_blocks) {
749 ASSERT3U(arc_get_compression(abuf), ==,
751 char *buf = abuf->b_data;
752 while (blksz > 0 && err == 0) {
753 int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
754 err = dump_write(dsa, type, zb->zb_object,
755 offset, n, n, NULL, buf);
761 err = dump_write(dsa, type, zb->zb_object, offset,
762 blksz, arc_buf_size(abuf), bp, abuf->b_data);
764 arc_buf_destroy(abuf, &abuf);
767 ASSERT(err == 0 || err == EINTR);
772 * Pop the new data off the queue, and free the old data.
774 static struct send_block_record *
775 get_next_record(bqueue_t *bq, struct send_block_record *data)
777 struct send_block_record *tmp = bqueue_dequeue(bq);
778 kmem_free(data, sizeof (*data));
783 * Actually do the bulk of the work in a zfs send.
785 * Note: Releases dp using the specified tag.
788 dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
789 zfs_bookmark_phys_t *ancestor_zb, boolean_t is_clone,
790 boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
791 int outfd, uint64_t resumeobj, uint64_t resumeoff,
793 vnode_t *vp, offset_t *off)
795 struct file *fp, offset_t *off)
799 dmu_replay_record_t *drr;
802 uint64_t fromtxg = 0;
803 uint64_t featureflags = 0;
804 struct send_thread_arg to_arg = { 0 };
806 err = dmu_objset_from_ds(to_ds, &os);
808 dsl_pool_rele(dp, tag);
812 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
813 drr->drr_type = DRR_BEGIN;
814 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
815 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
819 if (dmu_objset_type(os) == DMU_OST_ZFS) {
821 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
822 kmem_free(drr, sizeof (dmu_replay_record_t));
823 dsl_pool_rele(dp, tag);
824 return (SET_ERROR(EINVAL));
826 if (version >= ZPL_VERSION_SA) {
827 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
832 if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
833 featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
835 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
836 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
837 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
838 featureflags |= DMU_BACKUP_FEATURE_LZ4;
841 featureflags |= DMU_BACKUP_FEATURE_COMPRESSED;
844 (DMU_BACKUP_FEATURE_EMBED_DATA | DMU_BACKUP_FEATURE_COMPRESSED)) !=
845 0 && spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS)) {
846 featureflags |= DMU_BACKUP_FEATURE_LZ4;
849 if (resumeobj != 0 || resumeoff != 0) {
850 featureflags |= DMU_BACKUP_FEATURE_RESUMING;
853 DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
856 drr->drr_u.drr_begin.drr_creation_time =
857 dsl_dataset_phys(to_ds)->ds_creation_time;
858 drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
860 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
861 drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
862 if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
863 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
864 if (zfs_send_set_freerecords_bit)
865 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
867 if (ancestor_zb != NULL) {
868 drr->drr_u.drr_begin.drr_fromguid =
869 ancestor_zb->zbm_guid;
870 fromtxg = ancestor_zb->zbm_creation_txg;
872 dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
873 if (!to_ds->ds_is_snapshot) {
874 (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
875 sizeof (drr->drr_u.drr_begin.drr_toname));
878 dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
881 dsp->dsa_outfd = outfd;
882 dsp->dsa_proc = curproc;
883 dsp->dsa_td = curthread;
887 dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
888 dsp->dsa_pending_op = PENDING_NONE;
889 dsp->dsa_featureflags = featureflags;
890 dsp->dsa_resume_object = resumeobj;
891 dsp->dsa_resume_offset = resumeoff;
893 mutex_enter(&to_ds->ds_sendstream_lock);
894 list_insert_head(&to_ds->ds_sendstreams, dsp);
895 mutex_exit(&to_ds->ds_sendstream_lock);
897 dsl_dataset_long_hold(to_ds, FTAG);
898 dsl_pool_rele(dp, tag);
900 void *payload = NULL;
901 size_t payload_len = 0;
902 if (resumeobj != 0 || resumeoff != 0) {
903 dmu_object_info_t to_doi;
904 err = dmu_object_info(os, resumeobj, &to_doi);
907 SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
908 resumeoff / to_doi.doi_data_block_size);
910 nvlist_t *nvl = fnvlist_alloc();
911 fnvlist_add_uint64(nvl, "resume_object", resumeobj);
912 fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
913 payload = fnvlist_pack(nvl, &payload_len);
914 drr->drr_payloadlen = payload_len;
918 err = dump_record(dsp, payload, payload_len);
919 fnvlist_pack_free(payload, payload_len);
925 err = bqueue_init(&to_arg.q, zfs_send_queue_length,
926 offsetof(struct send_block_record, ln));
927 to_arg.error_code = 0;
928 to_arg.cancel = B_FALSE;
930 to_arg.fromtxg = fromtxg;
931 to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
932 (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, &p0,
933 TS_RUN, minclsyspri);
935 struct send_block_record *to_data;
936 to_data = bqueue_dequeue(&to_arg.q);
938 while (!to_data->eos_marker && err == 0) {
939 err = do_dump(dsp, to_data);
940 to_data = get_next_record(&to_arg.q, to_data);
941 if (issig(JUSTLOOKING) && issig(FORREAL))
946 to_arg.cancel = B_TRUE;
947 while (!to_data->eos_marker) {
948 to_data = get_next_record(&to_arg.q, to_data);
951 kmem_free(to_data, sizeof (*to_data));
953 bqueue_destroy(&to_arg.q);
955 if (err == 0 && to_arg.error_code != 0)
956 err = to_arg.error_code;
961 if (dsp->dsa_pending_op != PENDING_NONE)
962 if (dump_record(dsp, NULL, 0) != 0)
963 err = SET_ERROR(EINTR);
966 if (err == EINTR && dsp->dsa_err != 0)
971 bzero(drr, sizeof (dmu_replay_record_t));
972 drr->drr_type = DRR_END;
973 drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
974 drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
976 if (dump_record(dsp, NULL, 0) != 0)
980 mutex_enter(&to_ds->ds_sendstream_lock);
981 list_remove(&to_ds->ds_sendstreams, dsp);
982 mutex_exit(&to_ds->ds_sendstream_lock);
984 VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end));
986 kmem_free(drr, sizeof (dmu_replay_record_t));
987 kmem_free(dsp, sizeof (dmu_sendarg_t));
989 dsl_dataset_long_rele(to_ds, FTAG);
995 dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
996 boolean_t embedok, boolean_t large_block_ok, boolean_t compressok,
998 int outfd, vnode_t *vp, offset_t *off)
1000 int outfd, struct file *fp, offset_t *off)
1005 dsl_dataset_t *fromds = NULL;
1008 err = dsl_pool_hold(pool, FTAG, &dp);
1012 err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
1014 dsl_pool_rele(dp, FTAG);
1018 if (fromsnap != 0) {
1019 zfs_bookmark_phys_t zb;
1022 err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
1024 dsl_dataset_rele(ds, FTAG);
1025 dsl_pool_rele(dp, FTAG);
1028 if (!dsl_dataset_is_before(ds, fromds, 0))
1029 err = SET_ERROR(EXDEV);
1030 zb.zbm_creation_time =
1031 dsl_dataset_phys(fromds)->ds_creation_time;
1032 zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
1033 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
1034 is_clone = (fromds->ds_dir != ds->ds_dir);
1035 dsl_dataset_rele(fromds, FTAG);
1036 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
1037 embedok, large_block_ok, compressok, outfd, 0, 0, fp, off);
1039 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1040 embedok, large_block_ok, compressok, outfd, 0, 0, fp, off);
1042 dsl_dataset_rele(ds, FTAG);
1047 dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
1048 boolean_t large_block_ok, boolean_t compressok, int outfd,
1049 uint64_t resumeobj, uint64_t resumeoff,
1051 vnode_t *vp, offset_t *off)
1053 struct file *fp, offset_t *off)
1059 boolean_t owned = B_FALSE;
1061 if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
1062 return (SET_ERROR(EINVAL));
1064 err = dsl_pool_hold(tosnap, FTAG, &dp);
1068 if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
1070 * We are sending a filesystem or volume. Ensure
1071 * that it doesn't change by owning the dataset.
1073 err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
1076 err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
1079 dsl_pool_rele(dp, FTAG);
1083 if (fromsnap != NULL) {
1084 zfs_bookmark_phys_t zb;
1085 boolean_t is_clone = B_FALSE;
1086 int fsnamelen = strchr(tosnap, '@') - tosnap;
1089 * If the fromsnap is in a different filesystem, then
1090 * mark the send stream as a clone.
1092 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
1093 (fromsnap[fsnamelen] != '@' &&
1094 fromsnap[fsnamelen] != '#')) {
1098 if (strchr(fromsnap, '@')) {
1099 dsl_dataset_t *fromds;
1100 err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
1102 if (!dsl_dataset_is_before(ds, fromds, 0))
1103 err = SET_ERROR(EXDEV);
1104 zb.zbm_creation_time =
1105 dsl_dataset_phys(fromds)->ds_creation_time;
1106 zb.zbm_creation_txg =
1107 dsl_dataset_phys(fromds)->ds_creation_txg;
1108 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
1109 is_clone = (ds->ds_dir != fromds->ds_dir);
1110 dsl_dataset_rele(fromds, FTAG);
1113 err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
1116 dsl_dataset_rele(ds, FTAG);
1117 dsl_pool_rele(dp, FTAG);
1120 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
1121 embedok, large_block_ok, compressok,
1122 outfd, resumeobj, resumeoff, fp, off);
1124 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1125 embedok, large_block_ok, compressok,
1126 outfd, resumeobj, resumeoff, fp, off);
1129 dsl_dataset_disown(ds, FTAG);
1131 dsl_dataset_rele(ds, FTAG);
1136 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t uncompressed,
1137 uint64_t compressed, boolean_t stream_compressed, uint64_t *sizep)
1142 * Assume that space (both on-disk and in-stream) is dominated by
1143 * data. We will adjust for indirect blocks and the copies property,
1144 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1146 uint64_t recordsize;
1147 uint64_t record_count;
1149 VERIFY0(dmu_objset_from_ds(ds, &os));
1151 /* Assume all (uncompressed) blocks are recordsize. */
1152 if (zfs_override_estimate_recordsize != 0) {
1153 recordsize = zfs_override_estimate_recordsize;
1154 } else if (os->os_phys->os_type == DMU_OST_ZVOL) {
1155 err = dsl_prop_get_int_ds(ds,
1156 zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), &recordsize);
1158 err = dsl_prop_get_int_ds(ds,
1159 zfs_prop_to_name(ZFS_PROP_RECORDSIZE), &recordsize);
1163 record_count = uncompressed / recordsize;
1166 * If we're estimating a send size for a compressed stream, use the
1167 * compressed data size to estimate the stream size. Otherwise, use the
1168 * uncompressed data size.
1170 size = stream_compressed ? compressed : uncompressed;
1173 * Subtract out approximate space used by indirect blocks.
1174 * Assume most space is used by data blocks (non-indirect, non-dnode).
1175 * Assume no ditto blocks or internal fragmentation.
1177 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1180 size -= record_count * sizeof (blkptr_t);
1182 /* Add in the space for the record associated with each block. */
1183 size += record_count * sizeof (dmu_replay_record_t);
1191 dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds,
1192 boolean_t stream_compressed, uint64_t *sizep)
1194 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1196 uint64_t uncomp, comp;
1198 ASSERT(dsl_pool_config_held(dp));
1200 /* tosnap must be a snapshot */
1201 if (!ds->ds_is_snapshot)
1202 return (SET_ERROR(EINVAL));
1204 /* fromsnap, if provided, must be a snapshot */
1205 if (fromds != NULL && !fromds->ds_is_snapshot)
1206 return (SET_ERROR(EINVAL));
1209 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1210 * or the origin's fs.
1212 if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1213 return (SET_ERROR(EXDEV));
1215 /* Get compressed and uncompressed size estimates of changed data. */
1216 if (fromds == NULL) {
1217 uncomp = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1218 comp = dsl_dataset_phys(ds)->ds_compressed_bytes;
1221 err = dsl_dataset_space_written(fromds, ds,
1222 &used, &comp, &uncomp);
1227 err = dmu_adjust_send_estimate_for_indirects(ds, uncomp, comp,
1228 stream_compressed, sizep);
1230 * Add the size of the BEGIN and END records to the estimate.
1232 *sizep += 2 * sizeof (dmu_replay_record_t);
1236 struct calculate_send_arg {
1237 uint64_t uncompressed;
1238 uint64_t compressed;
1242 * Simple callback used to traverse the blocks of a snapshot and sum their
1243 * uncompressed and compressed sizes.
1247 dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1248 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1250 struct calculate_send_arg *space = arg;
1251 if (bp != NULL && !BP_IS_HOLE(bp)) {
1252 space->uncompressed += BP_GET_UCSIZE(bp);
1253 space->compressed += BP_GET_PSIZE(bp);
1259 * Given a desination snapshot and a TXG, calculate the approximate size of a
1260 * send stream sent from that TXG. from_txg may be zero, indicating that the
1261 * whole snapshot will be sent.
1264 dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
1265 boolean_t stream_compressed, uint64_t *sizep)
1267 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1269 struct calculate_send_arg size = { 0 };
1271 ASSERT(dsl_pool_config_held(dp));
1273 /* tosnap must be a snapshot */
1274 if (!ds->ds_is_snapshot)
1275 return (SET_ERROR(EINVAL));
1277 /* verify that from_txg is before the provided snapshot was taken */
1278 if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1279 return (SET_ERROR(EXDEV));
1283 * traverse the blocks of the snapshot with birth times after
1284 * from_txg, summing their uncompressed size
1286 err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1287 dmu_calculate_send_traversal, &size);
1291 err = dmu_adjust_send_estimate_for_indirects(ds, size.uncompressed,
1292 size.compressed, stream_compressed, sizep);
1296 typedef struct dmu_recv_begin_arg {
1297 const char *drba_origin;
1298 dmu_recv_cookie_t *drba_cookie;
1300 uint64_t drba_snapobj;
1301 } dmu_recv_begin_arg_t;
1304 recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
1309 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1311 /* temporary clone name must not exist */
1312 error = zap_lookup(dp->dp_meta_objset,
1313 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
1315 if (error != ENOENT)
1316 return (error == 0 ? EBUSY : error);
1318 /* new snapshot name must not exist */
1319 error = zap_lookup(dp->dp_meta_objset,
1320 dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1321 drba->drba_cookie->drc_tosnap, 8, 1, &val);
1322 if (error != ENOENT)
1323 return (error == 0 ? EEXIST : error);
1326 * Check snapshot limit before receiving. We'll recheck again at the
1327 * end, but might as well abort before receiving if we're already over
1330 * Note that we do not check the file system limit with
1331 * dsl_dir_fscount_check because the temporary %clones don't count
1332 * against that limit.
1334 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1335 NULL, drba->drba_cred);
1339 if (fromguid != 0) {
1340 dsl_dataset_t *snap;
1341 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1343 /* Find snapshot in this dir that matches fromguid. */
1345 error = dsl_dataset_hold_obj(dp, obj, FTAG,
1348 return (SET_ERROR(ENODEV));
1349 if (snap->ds_dir != ds->ds_dir) {
1350 dsl_dataset_rele(snap, FTAG);
1351 return (SET_ERROR(ENODEV));
1353 if (dsl_dataset_phys(snap)->ds_guid == fromguid)
1355 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
1356 dsl_dataset_rele(snap, FTAG);
1359 return (SET_ERROR(ENODEV));
1361 if (drba->drba_cookie->drc_force) {
1362 drba->drba_snapobj = obj;
1365 * If we are not forcing, there must be no
1366 * changes since fromsnap.
1368 if (dsl_dataset_modified_since_snap(ds, snap)) {
1369 dsl_dataset_rele(snap, FTAG);
1370 return (SET_ERROR(ETXTBSY));
1372 drba->drba_snapobj = ds->ds_prev->ds_object;
1375 dsl_dataset_rele(snap, FTAG);
1377 /* if full, then must be forced */
1378 if (!drba->drba_cookie->drc_force)
1379 return (SET_ERROR(EEXIST));
1380 /* start from $ORIGIN@$ORIGIN, if supported */
1381 drba->drba_snapobj = dp->dp_origin_snap != NULL ?
1382 dp->dp_origin_snap->ds_object : 0;
1390 dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
1392 dmu_recv_begin_arg_t *drba = arg;
1393 dsl_pool_t *dp = dmu_tx_pool(tx);
1394 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1395 uint64_t fromguid = drrb->drr_fromguid;
1396 int flags = drrb->drr_flags;
1398 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1400 const char *tofs = drba->drba_cookie->drc_tofs;
1402 /* already checked */
1403 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1404 ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
1406 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1407 DMU_COMPOUNDSTREAM ||
1408 drrb->drr_type >= DMU_OST_NUMTYPES ||
1409 ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1410 return (SET_ERROR(EINVAL));
1412 /* Verify pool version supports SA if SA_SPILL feature set */
1413 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1414 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1415 return (SET_ERROR(ENOTSUP));
1417 if (drba->drba_cookie->drc_resumable &&
1418 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
1419 return (SET_ERROR(ENOTSUP));
1422 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1423 * record to a plain WRITE record, so the pool must have the
1424 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1425 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1427 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1428 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1429 return (SET_ERROR(ENOTSUP));
1430 if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
1431 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1432 return (SET_ERROR(ENOTSUP));
1435 * The receiving code doesn't know how to translate large blocks
1436 * to smaller ones, so the pool must have the LARGE_BLOCKS
1437 * feature enabled if the stream has LARGE_BLOCKS.
1439 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1440 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1441 return (SET_ERROR(ENOTSUP));
1443 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1445 /* target fs already exists; recv into temp clone */
1447 /* Can't recv a clone into an existing fs */
1448 if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
1449 dsl_dataset_rele(ds, FTAG);
1450 return (SET_ERROR(EINVAL));
1453 error = recv_begin_check_existing_impl(drba, ds, fromguid);
1454 dsl_dataset_rele(ds, FTAG);
1455 } else if (error == ENOENT) {
1456 /* target fs does not exist; must be a full backup or clone */
1457 char buf[ZFS_MAX_DATASET_NAME_LEN];
1460 * If it's a non-clone incremental, we are missing the
1461 * target fs, so fail the recv.
1463 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1465 return (SET_ERROR(ENOENT));
1468 * If we're receiving a full send as a clone, and it doesn't
1469 * contain all the necessary free records and freeobject
1470 * records, reject it.
1472 if (fromguid == 0 && drba->drba_origin &&
1473 !(flags & DRR_FLAG_FREERECORDS))
1474 return (SET_ERROR(EINVAL));
1476 /* Open the parent of tofs */
1477 ASSERT3U(strlen(tofs), <, sizeof (buf));
1478 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
1479 error = dsl_dataset_hold(dp, buf, FTAG, &ds);
1484 * Check filesystem and snapshot limits before receiving. We'll
1485 * recheck snapshot limits again at the end (we create the
1486 * filesystems and increment those counts during begin_sync).
1488 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1489 ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1491 dsl_dataset_rele(ds, FTAG);
1495 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1496 ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1498 dsl_dataset_rele(ds, FTAG);
1502 if (drba->drba_origin != NULL) {
1503 dsl_dataset_t *origin;
1504 error = dsl_dataset_hold(dp, drba->drba_origin,
1507 dsl_dataset_rele(ds, FTAG);
1510 if (!origin->ds_is_snapshot) {
1511 dsl_dataset_rele(origin, FTAG);
1512 dsl_dataset_rele(ds, FTAG);
1513 return (SET_ERROR(EINVAL));
1515 if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
1517 dsl_dataset_rele(origin, FTAG);
1518 dsl_dataset_rele(ds, FTAG);
1519 return (SET_ERROR(ENODEV));
1521 dsl_dataset_rele(origin, FTAG);
1523 dsl_dataset_rele(ds, FTAG);
1530 dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1532 dmu_recv_begin_arg_t *drba = arg;
1533 dsl_pool_t *dp = dmu_tx_pool(tx);
1534 objset_t *mos = dp->dp_meta_objset;
1535 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1536 const char *tofs = drba->drba_cookie->drc_tofs;
1537 dsl_dataset_t *ds, *newds;
1540 uint64_t crflags = 0;
1542 if (drrb->drr_flags & DRR_FLAG_CI_DATA)
1543 crflags |= DS_FLAG_CI_DATASET;
1545 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1547 /* create temporary clone */
1548 dsl_dataset_t *snap = NULL;
1549 if (drba->drba_snapobj != 0) {
1550 VERIFY0(dsl_dataset_hold_obj(dp,
1551 drba->drba_snapobj, FTAG, &snap));
1553 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
1554 snap, crflags, drba->drba_cred, tx);
1555 if (drba->drba_snapobj != 0)
1556 dsl_dataset_rele(snap, FTAG);
1557 dsl_dataset_rele(ds, FTAG);
1561 dsl_dataset_t *origin = NULL;
1563 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
1565 if (drba->drba_origin != NULL) {
1566 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
1570 /* Create new dataset. */
1571 dsobj = dsl_dataset_create_sync(dd,
1572 strrchr(tofs, '/') + 1,
1573 origin, crflags, drba->drba_cred, tx);
1575 dsl_dataset_rele(origin, FTAG);
1576 dsl_dir_rele(dd, FTAG);
1577 drba->drba_cookie->drc_newfs = B_TRUE;
1579 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
1581 if (drba->drba_cookie->drc_resumable) {
1582 dsl_dataset_zapify(newds, tx);
1583 if (drrb->drr_fromguid != 0) {
1584 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
1585 8, 1, &drrb->drr_fromguid, tx));
1587 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
1588 8, 1, &drrb->drr_toguid, tx));
1589 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
1590 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
1593 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
1595 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
1597 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
1599 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1600 DMU_BACKUP_FEATURE_LARGE_BLOCKS) {
1601 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_LARGEBLOCK,
1604 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1605 DMU_BACKUP_FEATURE_EMBED_DATA) {
1606 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
1609 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1610 DMU_BACKUP_FEATURE_COMPRESSED) {
1611 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_COMPRESSOK,
1616 dmu_buf_will_dirty(newds->ds_dbuf, tx);
1617 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
1620 * If we actually created a non-clone, we need to create the
1621 * objset in our new dataset.
1623 rrw_enter(&newds->ds_bp_rwlock, RW_READER, FTAG);
1624 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1625 (void) dmu_objset_create_impl(dp->dp_spa,
1626 newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1628 rrw_exit(&newds->ds_bp_rwlock, FTAG);
1630 drba->drba_cookie->drc_ds = newds;
1632 spa_history_log_internal_ds(newds, "receive", tx, "");
1636 dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1638 dmu_recv_begin_arg_t *drba = arg;
1639 dsl_pool_t *dp = dmu_tx_pool(tx);
1640 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1642 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1644 const char *tofs = drba->drba_cookie->drc_tofs;
1646 /* already checked */
1647 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1648 ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
1650 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1651 DMU_COMPOUNDSTREAM ||
1652 drrb->drr_type >= DMU_OST_NUMTYPES)
1653 return (SET_ERROR(EINVAL));
1655 /* Verify pool version supports SA if SA_SPILL feature set */
1656 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1657 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1658 return (SET_ERROR(ENOTSUP));
1661 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1662 * record to a plain WRITE record, so the pool must have the
1663 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1664 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1666 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1667 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1668 return (SET_ERROR(ENOTSUP));
1669 if ((featureflags & DMU_BACKUP_FEATURE_LZ4) &&
1670 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1671 return (SET_ERROR(ENOTSUP));
1673 /* 6 extra bytes for /%recv */
1674 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1676 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1677 tofs, recv_clone_name);
1679 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1680 /* %recv does not exist; continue in tofs */
1681 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1686 /* check that ds is marked inconsistent */
1687 if (!DS_IS_INCONSISTENT(ds)) {
1688 dsl_dataset_rele(ds, FTAG);
1689 return (SET_ERROR(EINVAL));
1692 /* check that there is resuming data, and that the toguid matches */
1693 if (!dsl_dataset_is_zapified(ds)) {
1694 dsl_dataset_rele(ds, FTAG);
1695 return (SET_ERROR(EINVAL));
1698 error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1699 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1700 if (error != 0 || drrb->drr_toguid != val) {
1701 dsl_dataset_rele(ds, FTAG);
1702 return (SET_ERROR(EINVAL));
1706 * Check if the receive is still running. If so, it will be owned.
1707 * Note that nothing else can own the dataset (e.g. after the receive
1708 * fails) because it will be marked inconsistent.
1710 if (dsl_dataset_has_owner(ds)) {
1711 dsl_dataset_rele(ds, FTAG);
1712 return (SET_ERROR(EBUSY));
1715 /* There should not be any snapshots of this fs yet. */
1716 if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1717 dsl_dataset_rele(ds, FTAG);
1718 return (SET_ERROR(EINVAL));
1722 * Note: resume point will be checked when we process the first WRITE
1726 /* check that the origin matches */
1728 (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1729 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1730 if (drrb->drr_fromguid != val) {
1731 dsl_dataset_rele(ds, FTAG);
1732 return (SET_ERROR(EINVAL));
1735 dsl_dataset_rele(ds, FTAG);
1740 dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1742 dmu_recv_begin_arg_t *drba = arg;
1743 dsl_pool_t *dp = dmu_tx_pool(tx);
1744 const char *tofs = drba->drba_cookie->drc_tofs;
1747 /* 6 extra bytes for /%recv */
1748 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1750 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1751 tofs, recv_clone_name);
1753 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1754 /* %recv does not exist; continue in tofs */
1755 VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
1756 drba->drba_cookie->drc_newfs = B_TRUE;
1759 /* clear the inconsistent flag so that we can own it */
1760 ASSERT(DS_IS_INCONSISTENT(ds));
1761 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1762 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
1763 dsobj = ds->ds_object;
1764 dsl_dataset_rele(ds, FTAG);
1766 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
1768 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1769 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1771 rrw_enter(&ds->ds_bp_rwlock, RW_READER, FTAG);
1772 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
1773 rrw_exit(&ds->ds_bp_rwlock, FTAG);
1775 drba->drba_cookie->drc_ds = ds;
1777 spa_history_log_internal_ds(ds, "resume receive", tx, "");
1781 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1782 * succeeds; otherwise we will leak the holds on the datasets.
1785 dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
1786 boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1788 dmu_recv_begin_arg_t drba = { 0 };
1790 bzero(drc, sizeof (dmu_recv_cookie_t));
1791 drc->drc_drr_begin = drr_begin;
1792 drc->drc_drrb = &drr_begin->drr_u.drr_begin;
1793 drc->drc_tosnap = tosnap;
1794 drc->drc_tofs = tofs;
1795 drc->drc_force = force;
1796 drc->drc_resumable = resumable;
1797 drc->drc_cred = CRED();
1799 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1800 drc->drc_byteswap = B_TRUE;
1801 (void) fletcher_4_incremental_byteswap(drr_begin,
1802 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1803 byteswap_record(drr_begin);
1804 } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1805 (void) fletcher_4_incremental_native(drr_begin,
1806 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1808 return (SET_ERROR(EINVAL));
1811 drba.drba_origin = origin;
1812 drba.drba_cookie = drc;
1813 drba.drba_cred = CRED();
1815 if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1816 DMU_BACKUP_FEATURE_RESUMING) {
1817 return (dsl_sync_task(tofs,
1818 dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
1819 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1821 return (dsl_sync_task(tofs,
1822 dmu_recv_begin_check, dmu_recv_begin_sync,
1823 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1827 struct receive_record_arg {
1828 dmu_replay_record_t header;
1829 void *payload; /* Pointer to a buffer containing the payload */
1831 * If the record is a write, pointer to the arc_buf_t containing the
1834 arc_buf_t *write_buf;
1836 uint64_t bytes_read; /* bytes read from stream when record created */
1837 boolean_t eos_marker; /* Marks the end of the stream */
1841 struct receive_writer_arg {
1847 * These three args are used to signal to the main thread that we're
1855 /* A map from guid to dataset to help handle dedup'd streams. */
1856 avl_tree_t *guid_to_ds_map;
1857 boolean_t resumable;
1858 uint64_t last_object, last_offset;
1859 uint64_t bytes_read; /* bytes read when current record created */
1863 list_t list; /* List of struct receive_objnode. */
1865 * Last object looked up. Used to assert that objects are being looked
1866 * up in ascending order.
1868 uint64_t last_lookup;
1871 struct receive_objnode {
1876 struct receive_arg {
1880 uint64_t voff; /* The current offset in the stream */
1881 uint64_t bytes_read;
1883 * A record that has had its payload read in, but hasn't yet been handed
1884 * off to the worker thread.
1886 struct receive_record_arg *rrd;
1887 /* A record that has had its header read in, but not its payload. */
1888 struct receive_record_arg *next_rrd;
1890 zio_cksum_t prev_cksum;
1893 /* Sorted list of objects not to issue prefetches for. */
1894 struct objlist ignore_objlist;
1897 typedef struct guid_map_entry {
1899 dsl_dataset_t *gme_ds;
1904 guid_compare(const void *arg1, const void *arg2)
1906 const guid_map_entry_t *gmep1 = arg1;
1907 const guid_map_entry_t *gmep2 = arg2;
1909 if (gmep1->guid < gmep2->guid)
1911 else if (gmep1->guid > gmep2->guid)
1917 free_guid_map_onexit(void *arg)
1919 avl_tree_t *ca = arg;
1920 void *cookie = NULL;
1921 guid_map_entry_t *gmep;
1923 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
1924 dsl_dataset_long_rele(gmep->gme_ds, gmep);
1925 dsl_dataset_rele(gmep->gme_ds, gmep);
1926 kmem_free(gmep, sizeof (guid_map_entry_t));
1929 kmem_free(ca, sizeof (avl_tree_t));
1933 restore_bytes(struct receive_arg *ra, void *buf, int len, off_t off, ssize_t *resid)
1939 aiov.iov_base = buf;
1941 auio.uio_iov = &aiov;
1942 auio.uio_iovcnt = 1;
1943 auio.uio_resid = len;
1944 auio.uio_segflg = UIO_SYSSPACE;
1945 auio.uio_rw = UIO_READ;
1946 auio.uio_offset = off;
1947 auio.uio_td = ra->td;
1949 error = fo_read(ra->fp, &auio, ra->td->td_ucred, FOF_OFFSET, ra->td);
1951 fprintf(stderr, "%s: returning EOPNOTSUPP\n", __func__);
1954 *resid = auio.uio_resid;
1959 receive_read(struct receive_arg *ra, int len, void *buf)
1964 * The code doesn't rely on this (lengths being multiples of 8). See
1965 * comment in dump_bytes.
1969 while (done < len) {
1972 ra->err = restore_bytes(ra, buf + done,
1973 len - done, ra->voff, &resid);
1975 if (resid == len - done) {
1977 * Note: ECKSUM indicates that the receive
1978 * was interrupted and can potentially be resumed.
1980 ra->err = SET_ERROR(ECKSUM);
1982 ra->voff += len - done - resid;
1988 ra->bytes_read += len;
1990 ASSERT3U(done, ==, len);
1995 byteswap_record(dmu_replay_record_t *drr)
1997 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1998 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1999 drr->drr_type = BSWAP_32(drr->drr_type);
2000 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
2002 switch (drr->drr_type) {
2004 DO64(drr_begin.drr_magic);
2005 DO64(drr_begin.drr_versioninfo);
2006 DO64(drr_begin.drr_creation_time);
2007 DO32(drr_begin.drr_type);
2008 DO32(drr_begin.drr_flags);
2009 DO64(drr_begin.drr_toguid);
2010 DO64(drr_begin.drr_fromguid);
2013 DO64(drr_object.drr_object);
2014 DO32(drr_object.drr_type);
2015 DO32(drr_object.drr_bonustype);
2016 DO32(drr_object.drr_blksz);
2017 DO32(drr_object.drr_bonuslen);
2018 DO64(drr_object.drr_toguid);
2020 case DRR_FREEOBJECTS:
2021 DO64(drr_freeobjects.drr_firstobj);
2022 DO64(drr_freeobjects.drr_numobjs);
2023 DO64(drr_freeobjects.drr_toguid);
2026 DO64(drr_write.drr_object);
2027 DO32(drr_write.drr_type);
2028 DO64(drr_write.drr_offset);
2029 DO64(drr_write.drr_logical_size);
2030 DO64(drr_write.drr_toguid);
2031 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
2032 DO64(drr_write.drr_key.ddk_prop);
2033 DO64(drr_write.drr_compressed_size);
2035 case DRR_WRITE_BYREF:
2036 DO64(drr_write_byref.drr_object);
2037 DO64(drr_write_byref.drr_offset);
2038 DO64(drr_write_byref.drr_length);
2039 DO64(drr_write_byref.drr_toguid);
2040 DO64(drr_write_byref.drr_refguid);
2041 DO64(drr_write_byref.drr_refobject);
2042 DO64(drr_write_byref.drr_refoffset);
2043 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
2045 DO64(drr_write_byref.drr_key.ddk_prop);
2047 case DRR_WRITE_EMBEDDED:
2048 DO64(drr_write_embedded.drr_object);
2049 DO64(drr_write_embedded.drr_offset);
2050 DO64(drr_write_embedded.drr_length);
2051 DO64(drr_write_embedded.drr_toguid);
2052 DO32(drr_write_embedded.drr_lsize);
2053 DO32(drr_write_embedded.drr_psize);
2056 DO64(drr_free.drr_object);
2057 DO64(drr_free.drr_offset);
2058 DO64(drr_free.drr_length);
2059 DO64(drr_free.drr_toguid);
2062 DO64(drr_spill.drr_object);
2063 DO64(drr_spill.drr_length);
2064 DO64(drr_spill.drr_toguid);
2067 DO64(drr_end.drr_toguid);
2068 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
2072 if (drr->drr_type != DRR_BEGIN) {
2073 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
2080 static inline uint8_t
2081 deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
2083 if (bonus_type == DMU_OT_SA) {
2087 ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
2092 save_resume_state(struct receive_writer_arg *rwa,
2093 uint64_t object, uint64_t offset, dmu_tx_t *tx)
2095 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
2097 if (!rwa->resumable)
2101 * We use ds_resume_bytes[] != 0 to indicate that we need to
2102 * update this on disk, so it must not be 0.
2104 ASSERT(rwa->bytes_read != 0);
2107 * We only resume from write records, which have a valid
2108 * (non-meta-dnode) object number.
2110 ASSERT(object != 0);
2113 * For resuming to work correctly, we must receive records in order,
2114 * sorted by object,offset. This is checked by the callers, but
2115 * assert it here for good measure.
2117 ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
2118 ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
2119 offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
2120 ASSERT3U(rwa->bytes_read, >=,
2121 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
2123 rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
2124 rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
2125 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
2129 receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
2132 dmu_object_info_t doi;
2137 if (drro->drr_type == DMU_OT_NONE ||
2138 !DMU_OT_IS_VALID(drro->drr_type) ||
2139 !DMU_OT_IS_VALID(drro->drr_bonustype) ||
2140 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
2141 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
2142 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
2143 drro->drr_blksz < SPA_MINBLOCKSIZE ||
2144 drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
2145 drro->drr_bonuslen > DN_MAX_BONUSLEN) {
2146 return (SET_ERROR(EINVAL));
2149 err = dmu_object_info(rwa->os, drro->drr_object, &doi);
2151 if (err != 0 && err != ENOENT)
2152 return (SET_ERROR(EINVAL));
2153 object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
2156 * If we are losing blkptrs or changing the block size this must
2157 * be a new file instance. We must clear out the previous file
2158 * contents before we can change this type of metadata in the dnode.
2163 nblkptr = deduce_nblkptr(drro->drr_bonustype,
2164 drro->drr_bonuslen);
2166 if (drro->drr_blksz != doi.doi_data_block_size ||
2167 nblkptr < doi.doi_nblkptr) {
2168 err = dmu_free_long_range(rwa->os, drro->drr_object,
2171 return (SET_ERROR(EINVAL));
2175 tx = dmu_tx_create(rwa->os);
2176 dmu_tx_hold_bonus(tx, object);
2177 err = dmu_tx_assign(tx, TXG_WAIT);
2183 if (object == DMU_NEW_OBJECT) {
2184 /* currently free, want to be allocated */
2185 err = dmu_object_claim(rwa->os, drro->drr_object,
2186 drro->drr_type, drro->drr_blksz,
2187 drro->drr_bonustype, drro->drr_bonuslen, tx);
2188 } else if (drro->drr_type != doi.doi_type ||
2189 drro->drr_blksz != doi.doi_data_block_size ||
2190 drro->drr_bonustype != doi.doi_bonus_type ||
2191 drro->drr_bonuslen != doi.doi_bonus_size) {
2192 /* currently allocated, but with different properties */
2193 err = dmu_object_reclaim(rwa->os, drro->drr_object,
2194 drro->drr_type, drro->drr_blksz,
2195 drro->drr_bonustype, drro->drr_bonuslen, tx);
2199 return (SET_ERROR(EINVAL));
2202 dmu_object_set_checksum(rwa->os, drro->drr_object,
2203 drro->drr_checksumtype, tx);
2204 dmu_object_set_compress(rwa->os, drro->drr_object,
2205 drro->drr_compress, tx);
2210 VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
2211 dmu_buf_will_dirty(db, tx);
2213 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
2214 bcopy(data, db->db_data, drro->drr_bonuslen);
2215 if (rwa->byteswap) {
2216 dmu_object_byteswap_t byteswap =
2217 DMU_OT_BYTESWAP(drro->drr_bonustype);
2218 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2219 drro->drr_bonuslen);
2221 dmu_buf_rele(db, FTAG);
2230 receive_freeobjects(struct receive_writer_arg *rwa,
2231 struct drr_freeobjects *drrfo)
2236 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2237 return (SET_ERROR(EINVAL));
2239 for (obj = drrfo->drr_firstobj;
2240 obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
2241 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2244 if (dmu_object_info(rwa->os, obj, NULL) != 0)
2247 err = dmu_free_long_object(rwa->os, obj);
2251 if (next_err != ESRCH)
2257 receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2263 if (drrw->drr_offset + drrw->drr_logical_size < drrw->drr_offset ||
2264 !DMU_OT_IS_VALID(drrw->drr_type))
2265 return (SET_ERROR(EINVAL));
2268 * For resuming to work, records must be in increasing order
2269 * by (object, offset).
2271 if (drrw->drr_object < rwa->last_object ||
2272 (drrw->drr_object == rwa->last_object &&
2273 drrw->drr_offset < rwa->last_offset)) {
2274 return (SET_ERROR(EINVAL));
2276 rwa->last_object = drrw->drr_object;
2277 rwa->last_offset = drrw->drr_offset;
2279 if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
2280 return (SET_ERROR(EINVAL));
2282 tx = dmu_tx_create(rwa->os);
2284 dmu_tx_hold_write(tx, drrw->drr_object,
2285 drrw->drr_offset, drrw->drr_logical_size);
2286 err = dmu_tx_assign(tx, TXG_WAIT);
2291 if (rwa->byteswap) {
2292 dmu_object_byteswap_t byteswap =
2293 DMU_OT_BYTESWAP(drrw->drr_type);
2294 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
2295 DRR_WRITE_PAYLOAD_SIZE(drrw));
2298 /* use the bonus buf to look up the dnode in dmu_assign_arcbuf */
2300 if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
2301 return (SET_ERROR(EINVAL));
2302 dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
2305 * Note: If the receive fails, we want the resume stream to start
2306 * with the same record that we last successfully received (as opposed
2307 * to the next record), so that we can verify that we are
2308 * resuming from the correct location.
2310 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2312 dmu_buf_rele(bonus, FTAG);
2318 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2319 * streams to refer to a copy of the data that is already on the
2320 * system because it came in earlier in the stream. This function
2321 * finds the earlier copy of the data, and uses that copy instead of
2322 * data from the stream to fulfill this write.
2325 receive_write_byref(struct receive_writer_arg *rwa,
2326 struct drr_write_byref *drrwbr)
2330 guid_map_entry_t gmesrch;
2331 guid_map_entry_t *gmep;
2333 objset_t *ref_os = NULL;
2336 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2337 return (SET_ERROR(EINVAL));
2340 * If the GUID of the referenced dataset is different from the
2341 * GUID of the target dataset, find the referenced dataset.
2343 if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
2344 gmesrch.guid = drrwbr->drr_refguid;
2345 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
2347 return (SET_ERROR(EINVAL));
2349 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2350 return (SET_ERROR(EINVAL));
2355 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
2356 drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
2360 tx = dmu_tx_create(rwa->os);
2362 dmu_tx_hold_write(tx, drrwbr->drr_object,
2363 drrwbr->drr_offset, drrwbr->drr_length);
2364 err = dmu_tx_assign(tx, TXG_WAIT);
2369 dmu_write(rwa->os, drrwbr->drr_object,
2370 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
2371 dmu_buf_rele(dbp, FTAG);
2373 /* See comment in restore_write. */
2374 save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
2380 receive_write_embedded(struct receive_writer_arg *rwa,
2381 struct drr_write_embedded *drrwe, void *data)
2386 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
2389 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
2392 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
2394 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
2397 tx = dmu_tx_create(rwa->os);
2399 dmu_tx_hold_write(tx, drrwe->drr_object,
2400 drrwe->drr_offset, drrwe->drr_length);
2401 err = dmu_tx_assign(tx, TXG_WAIT);
2407 dmu_write_embedded(rwa->os, drrwe->drr_object,
2408 drrwe->drr_offset, data, drrwe->drr_etype,
2409 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2410 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
2412 /* See comment in restore_write. */
2413 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
2419 receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2423 dmu_buf_t *db, *db_spill;
2426 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2427 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2428 return (SET_ERROR(EINVAL));
2430 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2431 return (SET_ERROR(EINVAL));
2433 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
2434 if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
2435 dmu_buf_rele(db, FTAG);
2439 tx = dmu_tx_create(rwa->os);
2441 dmu_tx_hold_spill(tx, db->db_object);
2443 err = dmu_tx_assign(tx, TXG_WAIT);
2445 dmu_buf_rele(db, FTAG);
2446 dmu_buf_rele(db_spill, FTAG);
2450 dmu_buf_will_dirty(db_spill, tx);
2452 if (db_spill->db_size < drrs->drr_length)
2453 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
2454 drrs->drr_length, tx));
2455 bcopy(data, db_spill->db_data, drrs->drr_length);
2457 dmu_buf_rele(db, FTAG);
2458 dmu_buf_rele(db_spill, FTAG);
2466 receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2470 if (drrf->drr_length != -1ULL &&
2471 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2472 return (SET_ERROR(EINVAL));
2474 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2475 return (SET_ERROR(EINVAL));
2477 err = dmu_free_long_range(rwa->os, drrf->drr_object,
2478 drrf->drr_offset, drrf->drr_length);
2483 /* used to destroy the drc_ds on error */
2485 dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
2487 if (drc->drc_resumable) {
2488 /* wait for our resume state to be written to disk */
2489 txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
2490 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2492 char name[ZFS_MAX_DATASET_NAME_LEN];
2493 dsl_dataset_name(drc->drc_ds, name);
2494 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2495 (void) dsl_destroy_head(name);
2500 receive_cksum(struct receive_arg *ra, int len, void *buf)
2503 (void) fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
2505 (void) fletcher_4_incremental_native(buf, len, &ra->cksum);
2510 * Read the payload into a buffer of size len, and update the current record's
2512 * Allocate ra->next_rrd and read the next record's header into
2513 * ra->next_rrd->header.
2514 * Verify checksum of payload and next record.
2517 receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
2522 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2523 err = receive_read(ra, len, buf);
2526 receive_cksum(ra, len, buf);
2528 /* note: rrd is NULL when reading the begin record's payload */
2529 if (ra->rrd != NULL) {
2530 ra->rrd->payload = buf;
2531 ra->rrd->payload_size = len;
2532 ra->rrd->bytes_read = ra->bytes_read;
2536 ra->prev_cksum = ra->cksum;
2538 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2539 err = receive_read(ra, sizeof (ra->next_rrd->header),
2540 &ra->next_rrd->header);
2541 ra->next_rrd->bytes_read = ra->bytes_read;
2543 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2544 ra->next_rrd = NULL;
2547 if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2548 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2549 ra->next_rrd = NULL;
2550 return (SET_ERROR(EINVAL));
2554 * Note: checksum is of everything up to but not including the
2557 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2558 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2560 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2561 &ra->next_rrd->header);
2563 zio_cksum_t cksum_orig =
2564 ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2565 zio_cksum_t *cksump =
2566 &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2569 byteswap_record(&ra->next_rrd->header);
2571 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2572 !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2573 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2574 ra->next_rrd = NULL;
2575 return (SET_ERROR(ECKSUM));
2578 receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
2584 objlist_create(struct objlist *list)
2586 list_create(&list->list, sizeof (struct receive_objnode),
2587 offsetof(struct receive_objnode, node));
2588 list->last_lookup = 0;
2592 objlist_destroy(struct objlist *list)
2594 for (struct receive_objnode *n = list_remove_head(&list->list);
2595 n != NULL; n = list_remove_head(&list->list)) {
2596 kmem_free(n, sizeof (*n));
2598 list_destroy(&list->list);
2602 * This function looks through the objlist to see if the specified object number
2603 * is contained in the objlist. In the process, it will remove all object
2604 * numbers in the list that are smaller than the specified object number. Thus,
2605 * any lookup of an object number smaller than a previously looked up object
2606 * number will always return false; therefore, all lookups should be done in
2610 objlist_exists(struct objlist *list, uint64_t object)
2612 struct receive_objnode *node = list_head(&list->list);
2613 ASSERT3U(object, >=, list->last_lookup);
2614 list->last_lookup = object;
2615 while (node != NULL && node->object < object) {
2616 VERIFY3P(node, ==, list_remove_head(&list->list));
2617 kmem_free(node, sizeof (*node));
2618 node = list_head(&list->list);
2620 return (node != NULL && node->object == object);
2624 * The objlist is a list of object numbers stored in ascending order. However,
2625 * the insertion of new object numbers does not seek out the correct location to
2626 * store a new object number; instead, it appends it to the list for simplicity.
2627 * Thus, any users must take care to only insert new object numbers in ascending
2631 objlist_insert(struct objlist *list, uint64_t object)
2633 struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
2634 node->object = object;
2636 struct receive_objnode *last_object = list_tail(&list->list);
2637 uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
2638 ASSERT3U(node->object, >, last_objnum);
2640 list_insert_tail(&list->list, node);
2644 * Issue the prefetch reads for any necessary indirect blocks.
2646 * We use the object ignore list to tell us whether or not to issue prefetches
2647 * for a given object. We do this for both correctness (in case the blocksize
2648 * of an object has changed) and performance (if the object doesn't exist, don't
2649 * needlessly try to issue prefetches). We also trim the list as we go through
2650 * the stream to prevent it from growing to an unbounded size.
2652 * The object numbers within will always be in sorted order, and any write
2653 * records we see will also be in sorted order, but they're not sorted with
2654 * respect to each other (i.e. we can get several object records before
2655 * receiving each object's write records). As a result, once we've reached a
2656 * given object number, we can safely remove any reference to lower object
2657 * numbers in the ignore list. In practice, we receive up to 32 object records
2658 * before receiving write records, so the list can have up to 32 nodes in it.
2662 receive_read_prefetch(struct receive_arg *ra,
2663 uint64_t object, uint64_t offset, uint64_t length)
2665 if (!objlist_exists(&ra->ignore_objlist, object)) {
2666 dmu_prefetch(ra->os, object, 1, offset, length,
2667 ZIO_PRIORITY_SYNC_READ);
2672 * Read records off the stream, issuing any necessary prefetches.
2675 receive_read_record(struct receive_arg *ra)
2679 switch (ra->rrd->header.drr_type) {
2682 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2683 uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2684 void *buf = kmem_zalloc(size, KM_SLEEP);
2685 dmu_object_info_t doi;
2686 err = receive_read_payload_and_next_header(ra, size, buf);
2688 kmem_free(buf, size);
2691 err = dmu_object_info(ra->os, drro->drr_object, &doi);
2693 * See receive_read_prefetch for an explanation why we're
2694 * storing this object in the ignore_obj_list.
2696 if (err == ENOENT ||
2697 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2698 objlist_insert(&ra->ignore_objlist, drro->drr_object);
2703 case DRR_FREEOBJECTS:
2705 err = receive_read_payload_and_next_header(ra, 0, NULL);
2710 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2712 boolean_t is_meta = DMU_OT_IS_METADATA(drrw->drr_type);
2713 if (DRR_WRITE_COMPRESSED(drrw)) {
2714 ASSERT3U(drrw->drr_compressed_size, >, 0);
2715 ASSERT3U(drrw->drr_logical_size, >=,
2716 drrw->drr_compressed_size);
2718 abuf = arc_loan_compressed_buf(
2719 dmu_objset_spa(ra->os),
2720 drrw->drr_compressed_size, drrw->drr_logical_size,
2721 drrw->drr_compressiontype);
2723 abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2724 is_meta, drrw->drr_logical_size);
2727 err = receive_read_payload_and_next_header(ra,
2728 DRR_WRITE_PAYLOAD_SIZE(drrw), abuf->b_data);
2730 dmu_return_arcbuf(abuf);
2733 ra->rrd->write_buf = abuf;
2734 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2735 drrw->drr_logical_size);
2738 case DRR_WRITE_BYREF:
2740 struct drr_write_byref *drrwb =
2741 &ra->rrd->header.drr_u.drr_write_byref;
2742 err = receive_read_payload_and_next_header(ra, 0, NULL);
2743 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2747 case DRR_WRITE_EMBEDDED:
2749 struct drr_write_embedded *drrwe =
2750 &ra->rrd->header.drr_u.drr_write_embedded;
2751 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2752 void *buf = kmem_zalloc(size, KM_SLEEP);
2754 err = receive_read_payload_and_next_header(ra, size, buf);
2756 kmem_free(buf, size);
2760 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2767 * It might be beneficial to prefetch indirect blocks here, but
2768 * we don't really have the data to decide for sure.
2770 err = receive_read_payload_and_next_header(ra, 0, NULL);
2775 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
2776 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2777 return (SET_ERROR(ECKSUM));
2782 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2783 void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2784 err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2787 kmem_free(buf, drrs->drr_length);
2791 return (SET_ERROR(EINVAL));
2796 * Commit the records to the pool.
2799 receive_process_record(struct receive_writer_arg *rwa,
2800 struct receive_record_arg *rrd)
2804 /* Processing in order, therefore bytes_read should be increasing. */
2805 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2806 rwa->bytes_read = rrd->bytes_read;
2808 switch (rrd->header.drr_type) {
2811 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2812 err = receive_object(rwa, drro, rrd->payload);
2813 kmem_free(rrd->payload, rrd->payload_size);
2814 rrd->payload = NULL;
2817 case DRR_FREEOBJECTS:
2819 struct drr_freeobjects *drrfo =
2820 &rrd->header.drr_u.drr_freeobjects;
2821 return (receive_freeobjects(rwa, drrfo));
2825 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2826 err = receive_write(rwa, drrw, rrd->write_buf);
2827 /* if receive_write() is successful, it consumes the arc_buf */
2829 dmu_return_arcbuf(rrd->write_buf);
2830 rrd->write_buf = NULL;
2831 rrd->payload = NULL;
2834 case DRR_WRITE_BYREF:
2836 struct drr_write_byref *drrwbr =
2837 &rrd->header.drr_u.drr_write_byref;
2838 return (receive_write_byref(rwa, drrwbr));
2840 case DRR_WRITE_EMBEDDED:
2842 struct drr_write_embedded *drrwe =
2843 &rrd->header.drr_u.drr_write_embedded;
2844 err = receive_write_embedded(rwa, drrwe, rrd->payload);
2845 kmem_free(rrd->payload, rrd->payload_size);
2846 rrd->payload = NULL;
2851 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2852 return (receive_free(rwa, drrf));
2856 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2857 err = receive_spill(rwa, drrs, rrd->payload);
2858 kmem_free(rrd->payload, rrd->payload_size);
2859 rrd->payload = NULL;
2863 return (SET_ERROR(EINVAL));
2868 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2869 * receive_process_record When we're done, signal the main thread and exit.
2872 receive_writer_thread(void *arg)
2874 struct receive_writer_arg *rwa = arg;
2875 struct receive_record_arg *rrd;
2876 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2877 rrd = bqueue_dequeue(&rwa->q)) {
2879 * If there's an error, the main thread will stop putting things
2880 * on the queue, but we need to clear everything in it before we
2883 if (rwa->err == 0) {
2884 rwa->err = receive_process_record(rwa, rrd);
2885 } else if (rrd->write_buf != NULL) {
2886 dmu_return_arcbuf(rrd->write_buf);
2887 rrd->write_buf = NULL;
2888 rrd->payload = NULL;
2889 } else if (rrd->payload != NULL) {
2890 kmem_free(rrd->payload, rrd->payload_size);
2891 rrd->payload = NULL;
2893 kmem_free(rrd, sizeof (*rrd));
2895 kmem_free(rrd, sizeof (*rrd));
2896 mutex_enter(&rwa->mutex);
2898 cv_signal(&rwa->cv);
2899 mutex_exit(&rwa->mutex);
2904 resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
2907 objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
2908 uint64_t dsobj = dmu_objset_id(ra->os);
2909 uint64_t resume_obj, resume_off;
2911 if (nvlist_lookup_uint64(begin_nvl,
2912 "resume_object", &resume_obj) != 0 ||
2913 nvlist_lookup_uint64(begin_nvl,
2914 "resume_offset", &resume_off) != 0) {
2915 return (SET_ERROR(EINVAL));
2917 VERIFY0(zap_lookup(mos, dsobj,
2918 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
2919 if (resume_obj != val)
2920 return (SET_ERROR(EINVAL));
2921 VERIFY0(zap_lookup(mos, dsobj,
2922 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
2923 if (resume_off != val)
2924 return (SET_ERROR(EINVAL));
2930 * Read in the stream's records, one by one, and apply them to the pool. There
2931 * are two threads involved; the thread that calls this function will spin up a
2932 * worker thread, read the records off the stream one by one, and issue
2933 * prefetches for any necessary indirect blocks. It will then push the records
2934 * onto an internal blocking queue. The worker thread will pull the records off
2935 * the queue, and actually write the data into the DMU. This way, the worker
2936 * thread doesn't have to wait for reads to complete, since everything it needs
2937 * (the indirect blocks) will be prefetched.
2939 * NB: callers *must* call dmu_recv_end() if this succeeds.
2942 dmu_recv_stream(dmu_recv_cookie_t *drc, struct file *fp, offset_t *voffp,
2943 int cleanup_fd, uint64_t *action_handlep)
2946 struct receive_arg ra = { 0 };
2947 struct receive_writer_arg rwa = { 0 };
2949 nvlist_t *begin_nvl = NULL;
2951 ra.byteswap = drc->drc_byteswap;
2952 ra.cksum = drc->drc_cksum;
2957 if (dsl_dataset_is_zapified(drc->drc_ds)) {
2958 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
2959 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
2960 sizeof (ra.bytes_read), 1, &ra.bytes_read);
2963 objlist_create(&ra.ignore_objlist);
2965 /* these were verified in dmu_recv_begin */
2966 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
2968 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2971 * Open the objset we are modifying.
2973 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
2975 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2977 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
2979 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2980 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2983 if (cleanup_fd == -1) {
2984 ra.err = SET_ERROR(EBADF);
2987 ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
2993 if (*action_handlep == 0) {
2994 rwa.guid_to_ds_map =
2995 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2996 avl_create(rwa.guid_to_ds_map, guid_compare,
2997 sizeof (guid_map_entry_t),
2998 offsetof(guid_map_entry_t, avlnode));
2999 err = zfs_onexit_add_cb(minor,
3000 free_guid_map_onexit, rwa.guid_to_ds_map,
3005 err = zfs_onexit_cb_data(minor, *action_handlep,
3006 (void **)&rwa.guid_to_ds_map);
3011 drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
3014 uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
3015 void *payload = NULL;
3016 if (payloadlen != 0)
3017 payload = kmem_alloc(payloadlen, KM_SLEEP);
3019 err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
3021 if (payloadlen != 0)
3022 kmem_free(payload, payloadlen);
3025 if (payloadlen != 0) {
3026 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
3027 kmem_free(payload, payloadlen);
3032 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
3033 err = resume_check(&ra, begin_nvl);
3038 (void) bqueue_init(&rwa.q, zfs_recv_queue_length,
3039 offsetof(struct receive_record_arg, node));
3040 cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
3041 mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
3043 rwa.byteswap = drc->drc_byteswap;
3044 rwa.resumable = drc->drc_resumable;
3046 (void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, &p0,
3047 TS_RUN, minclsyspri);
3049 * We're reading rwa.err without locks, which is safe since we are the
3050 * only reader, and the worker thread is the only writer. It's ok if we
3051 * miss a write for an iteration or two of the loop, since the writer
3052 * thread will keep freeing records we send it until we send it an eos
3055 * We can leave this loop in 3 ways: First, if rwa.err is
3056 * non-zero. In that case, the writer thread will free the rrd we just
3057 * pushed. Second, if we're interrupted; in that case, either it's the
3058 * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
3059 * has been handed off to the writer thread who will free it. Finally,
3060 * if receive_read_record fails or we're at the end of the stream, then
3061 * we free ra.rrd and exit.
3063 while (rwa.err == 0) {
3064 if (issig(JUSTLOOKING) && issig(FORREAL)) {
3065 err = SET_ERROR(EINTR);
3069 ASSERT3P(ra.rrd, ==, NULL);
3070 ra.rrd = ra.next_rrd;
3072 /* Allocates and loads header into ra.next_rrd */
3073 err = receive_read_record(&ra);
3075 if (ra.rrd->header.drr_type == DRR_END || err != 0) {
3076 kmem_free(ra.rrd, sizeof (*ra.rrd));
3081 bqueue_enqueue(&rwa.q, ra.rrd,
3082 sizeof (struct receive_record_arg) + ra.rrd->payload_size);
3085 if (ra.next_rrd == NULL)
3086 ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
3087 ra.next_rrd->eos_marker = B_TRUE;
3088 bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
3090 mutex_enter(&rwa.mutex);
3092 cv_wait(&rwa.cv, &rwa.mutex);
3094 mutex_exit(&rwa.mutex);
3096 cv_destroy(&rwa.cv);
3097 mutex_destroy(&rwa.mutex);
3098 bqueue_destroy(&rwa.q);
3103 nvlist_free(begin_nvl);
3104 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
3105 zfs_onexit_fd_rele(cleanup_fd);
3109 * Clean up references. If receive is not resumable,
3110 * destroy what we created, so we don't leave it in
3111 * the inconsistent state.
3113 dmu_recv_cleanup_ds(drc);
3117 objlist_destroy(&ra.ignore_objlist);
3122 dmu_recv_end_check(void *arg, dmu_tx_t *tx)
3124 dmu_recv_cookie_t *drc = arg;
3125 dsl_pool_t *dp = dmu_tx_pool(tx);
3128 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
3130 if (!drc->drc_newfs) {
3131 dsl_dataset_t *origin_head;
3133 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
3136 if (drc->drc_force) {
3138 * We will destroy any snapshots in tofs (i.e. before
3139 * origin_head) that are after the origin (which is
3140 * the snap before drc_ds, because drc_ds can not
3141 * have any snaps of its own).
3145 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3147 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3148 dsl_dataset_t *snap;
3149 error = dsl_dataset_hold_obj(dp, obj, FTAG,
3153 if (snap->ds_dir != origin_head->ds_dir)
3154 error = SET_ERROR(EINVAL);
3156 error = dsl_destroy_snapshot_check_impl(
3159 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3160 dsl_dataset_rele(snap, FTAG);
3165 dsl_dataset_rele(origin_head, FTAG);
3169 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
3170 origin_head, drc->drc_force, drc->drc_owner, tx);
3172 dsl_dataset_rele(origin_head, FTAG);
3175 error = dsl_dataset_snapshot_check_impl(origin_head,
3176 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3177 dsl_dataset_rele(origin_head, FTAG);
3181 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
3183 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
3184 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3190 dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
3192 dmu_recv_cookie_t *drc = arg;
3193 dsl_pool_t *dp = dmu_tx_pool(tx);
3195 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
3196 tx, "snap=%s", drc->drc_tosnap);
3198 if (!drc->drc_newfs) {
3199 dsl_dataset_t *origin_head;
3201 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3204 if (drc->drc_force) {
3206 * Destroy any snapshots of drc_tofs (origin_head)
3207 * after the origin (the snap before drc_ds).
3211 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3213 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3214 dsl_dataset_t *snap;
3215 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
3217 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3218 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3219 dsl_destroy_snapshot_sync_impl(snap,
3221 dsl_dataset_rele(snap, FTAG);
3224 VERIFY3P(drc->drc_ds->ds_prev, ==,
3225 origin_head->ds_prev);
3227 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3229 dsl_dataset_snapshot_sync_impl(origin_head,
3230 drc->drc_tosnap, tx);
3232 /* set snapshot's creation time and guid */
3233 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3234 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
3235 drc->drc_drrb->drr_creation_time;
3236 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
3237 drc->drc_drrb->drr_toguid;
3238 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
3239 ~DS_FLAG_INCONSISTENT;
3241 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3242 dsl_dataset_phys(origin_head)->ds_flags &=
3243 ~DS_FLAG_INCONSISTENT;
3245 drc->drc_newsnapobj =
3246 dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3248 dsl_dataset_rele(origin_head, FTAG);
3249 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
3251 if (drc->drc_owner != NULL)
3252 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3254 dsl_dataset_t *ds = drc->drc_ds;
3256 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
3258 /* set snapshot's creation time and guid */
3259 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3260 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
3261 drc->drc_drrb->drr_creation_time;
3262 dsl_dataset_phys(ds->ds_prev)->ds_guid =
3263 drc->drc_drrb->drr_toguid;
3264 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3265 ~DS_FLAG_INCONSISTENT;
3267 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3268 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
3269 if (dsl_dataset_has_resume_receive_state(ds)) {
3270 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3271 DS_FIELD_RESUME_FROMGUID, tx);
3272 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3273 DS_FIELD_RESUME_OBJECT, tx);
3274 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3275 DS_FIELD_RESUME_OFFSET, tx);
3276 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3277 DS_FIELD_RESUME_BYTES, tx);
3278 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3279 DS_FIELD_RESUME_TOGUID, tx);
3280 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3281 DS_FIELD_RESUME_TONAME, tx);
3283 drc->drc_newsnapobj =
3284 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
3287 * Release the hold from dmu_recv_begin. This must be done before
3288 * we return to open context, so that when we free the dataset's dnode,
3289 * we can evict its bonus buffer.
3291 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
3296 add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3299 dsl_dataset_t *snapds;
3300 guid_map_entry_t *gmep;
3303 ASSERT(guid_map != NULL);
3305 err = dsl_pool_hold(name, FTAG, &dp);
3308 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3309 err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3311 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3312 gmep->gme_ds = snapds;
3313 avl_add(guid_map, gmep);
3314 dsl_dataset_long_hold(snapds, gmep);
3316 kmem_free(gmep, sizeof (*gmep));
3318 dsl_pool_rele(dp, FTAG);
3322 static int dmu_recv_end_modified_blocks = 3;
3325 dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3329 * We will be destroying the ds; make sure its origin is unmounted if
3332 char name[ZFS_MAX_DATASET_NAME_LEN];
3333 dsl_dataset_name(drc->drc_ds, name);
3334 zfs_destroy_unmount_origin(name);
3337 return (dsl_sync_task(drc->drc_tofs,
3338 dmu_recv_end_check, dmu_recv_end_sync, drc,
3339 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3343 dmu_recv_new_end(dmu_recv_cookie_t *drc)
3345 return (dsl_sync_task(drc->drc_tofs,
3346 dmu_recv_end_check, dmu_recv_end_sync, drc,
3347 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3351 dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3355 drc->drc_owner = owner;
3358 error = dmu_recv_new_end(drc);
3360 error = dmu_recv_existing_end(drc);
3363 dmu_recv_cleanup_ds(drc);
3364 } else if (drc->drc_guid_to_ds_map != NULL) {
3365 (void) add_ds_to_guidmap(drc->drc_tofs,
3366 drc->drc_guid_to_ds_map,
3367 drc->drc_newsnapobj);
3373 * Return TRUE if this objset is currently being received into.
3376 dmu_objset_is_receiving(objset_t *os)
3378 return (os->os_dsl_dataset != NULL &&
3379 os->os_dsl_dataset->ds_owner == dmu_recv_tag);