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";
79 #define BP_SPAN(datablkszsec, indblkshift, level) \
80 (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
81 (level) * (indblkshift - SPA_BLKPTRSHIFT)))
83 static void byteswap_record(dmu_replay_record_t *drr);
85 struct send_thread_arg {
87 dsl_dataset_t *ds; /* Dataset to traverse */
88 uint64_t fromtxg; /* Traverse from this txg */
89 int flags; /* flags to pass to traverse_dataset */
92 zbookmark_phys_t resume;
95 struct send_block_record {
96 boolean_t eos_marker; /* Marks the end of the stream */
100 uint16_t datablkszsec;
105 dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
107 dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
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).
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.
127 auio.uio_iov = &aiov;
129 auio.uio_resid = len;
130 auio.uio_segflg = UIO_SYSSPACE;
131 auio.uio_rw = UIO_WRITE;
132 auio.uio_offset = (off_t)-1;
133 auio.uio_td = dsp->dsa_td;
135 if (dsp->dsa_fp->f_type == DTYPE_VNODE)
137 dsp->dsa_err = fo_write(dsp->dsa_fp, &auio, dsp->dsa_td->td_ucred, 0,
140 fprintf(stderr, "%s: returning EOPNOTSUPP\n", __func__);
141 dsp->dsa_err = EOPNOTSUPP;
143 mutex_enter(&ds->ds_sendstream_lock);
144 *dsp->dsa_off += len;
145 mutex_exit(&ds->ds_sendstream_lock);
147 return (dsp->dsa_err);
151 * For all record types except BEGIN, fill in the checksum (overlaid in
152 * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
153 * up to the start of the checksum itself.
156 dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
158 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
159 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
160 fletcher_4_incremental_native(dsp->dsa_drr,
161 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
163 if (dsp->dsa_drr->drr_type == DRR_BEGIN) {
164 dsp->dsa_sent_begin = B_TRUE;
166 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
167 drr_checksum.drr_checksum));
168 dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
170 if (dsp->dsa_drr->drr_type == DRR_END) {
171 dsp->dsa_sent_end = B_TRUE;
173 fletcher_4_incremental_native(&dsp->dsa_drr->
174 drr_u.drr_checksum.drr_checksum,
175 sizeof (zio_cksum_t), &dsp->dsa_zc);
176 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
177 return (SET_ERROR(EINTR));
178 if (payload_len != 0) {
179 fletcher_4_incremental_native(payload, payload_len,
181 if (dump_bytes(dsp, payload, payload_len) != 0)
182 return (SET_ERROR(EINTR));
188 * Fill in the drr_free struct, or perform aggregation if the previous record is
189 * also a free record, and the two are adjacent.
191 * Note that we send free records even for a full send, because we want to be
192 * able to receive a full send as a clone, which requires a list of all the free
193 * and freeobject records that were generated on the source.
196 dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
199 struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
202 * When we receive a free record, dbuf_free_range() assumes
203 * that the receiving system doesn't have any dbufs in the range
204 * being freed. This is always true because there is a one-record
205 * constraint: we only send one WRITE record for any given
206 * object,offset. We know that the one-record constraint is
207 * true because we always send data in increasing order by
210 * If the increasing-order constraint ever changes, we should find
211 * another way to assert that the one-record constraint is still
214 ASSERT(object > dsp->dsa_last_data_object ||
215 (object == dsp->dsa_last_data_object &&
216 offset > dsp->dsa_last_data_offset));
218 if (length != -1ULL && offset + length < offset)
222 * If there is a pending op, but it's not PENDING_FREE, push it out,
223 * since free block aggregation can only be done for blocks of the
224 * same type (i.e., DRR_FREE records can only be aggregated with
225 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
226 * aggregated with other DRR_FREEOBJECTS records.
228 if (dsp->dsa_pending_op != PENDING_NONE &&
229 dsp->dsa_pending_op != PENDING_FREE) {
230 if (dump_record(dsp, NULL, 0) != 0)
231 return (SET_ERROR(EINTR));
232 dsp->dsa_pending_op = PENDING_NONE;
235 if (dsp->dsa_pending_op == PENDING_FREE) {
237 * There should never be a PENDING_FREE if length is -1
238 * (because dump_dnode is the only place where this
239 * function is called with a -1, and only after flushing
240 * any pending record).
242 ASSERT(length != -1ULL);
244 * Check to see whether this free block can be aggregated
247 if (drrf->drr_object == object && drrf->drr_offset +
248 drrf->drr_length == offset) {
249 drrf->drr_length += length;
252 /* not a continuation. Push out pending record */
253 if (dump_record(dsp, NULL, 0) != 0)
254 return (SET_ERROR(EINTR));
255 dsp->dsa_pending_op = PENDING_NONE;
258 /* create a FREE record and make it pending */
259 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
260 dsp->dsa_drr->drr_type = DRR_FREE;
261 drrf->drr_object = object;
262 drrf->drr_offset = offset;
263 drrf->drr_length = length;
264 drrf->drr_toguid = dsp->dsa_toguid;
265 if (length == -1ULL) {
266 if (dump_record(dsp, NULL, 0) != 0)
267 return (SET_ERROR(EINTR));
269 dsp->dsa_pending_op = PENDING_FREE;
276 dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
277 uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
279 struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
282 * We send data in increasing object, offset order.
283 * See comment in dump_free() for details.
285 ASSERT(object > dsp->dsa_last_data_object ||
286 (object == dsp->dsa_last_data_object &&
287 offset > dsp->dsa_last_data_offset));
288 dsp->dsa_last_data_object = object;
289 dsp->dsa_last_data_offset = offset + blksz - 1;
292 * If there is any kind of pending aggregation (currently either
293 * a grouping of free objects or free blocks), push it out to
294 * the stream, since aggregation can't be done across operations
295 * of different types.
297 if (dsp->dsa_pending_op != PENDING_NONE) {
298 if (dump_record(dsp, NULL, 0) != 0)
299 return (SET_ERROR(EINTR));
300 dsp->dsa_pending_op = PENDING_NONE;
302 /* write a WRITE record */
303 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
304 dsp->dsa_drr->drr_type = DRR_WRITE;
305 drrw->drr_object = object;
306 drrw->drr_type = type;
307 drrw->drr_offset = offset;
308 drrw->drr_length = blksz;
309 drrw->drr_toguid = dsp->dsa_toguid;
310 if (bp == NULL || BP_IS_EMBEDDED(bp)) {
312 * There's no pre-computed checksum for partial-block
313 * writes or embedded BP's, so (like
314 * fletcher4-checkummed blocks) userland will have to
315 * compute a dedup-capable checksum itself.
317 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
319 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
320 if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
321 ZCHECKSUM_FLAG_DEDUP)
322 drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
323 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
324 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
325 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
326 drrw->drr_key.ddk_cksum = bp->blk_cksum;
329 if (dump_record(dsp, data, blksz) != 0)
330 return (SET_ERROR(EINTR));
335 dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
336 int blksz, const blkptr_t *bp)
338 char buf[BPE_PAYLOAD_SIZE];
339 struct drr_write_embedded *drrw =
340 &(dsp->dsa_drr->drr_u.drr_write_embedded);
342 if (dsp->dsa_pending_op != PENDING_NONE) {
343 if (dump_record(dsp, NULL, 0) != 0)
345 dsp->dsa_pending_op = PENDING_NONE;
348 ASSERT(BP_IS_EMBEDDED(bp));
350 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
351 dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
352 drrw->drr_object = object;
353 drrw->drr_offset = offset;
354 drrw->drr_length = blksz;
355 drrw->drr_toguid = dsp->dsa_toguid;
356 drrw->drr_compression = BP_GET_COMPRESS(bp);
357 drrw->drr_etype = BPE_GET_ETYPE(bp);
358 drrw->drr_lsize = BPE_GET_LSIZE(bp);
359 drrw->drr_psize = BPE_GET_PSIZE(bp);
361 decode_embedded_bp_compressed(bp, buf);
363 if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
369 dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
371 struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
373 if (dsp->dsa_pending_op != PENDING_NONE) {
374 if (dump_record(dsp, NULL, 0) != 0)
375 return (SET_ERROR(EINTR));
376 dsp->dsa_pending_op = PENDING_NONE;
379 /* write a SPILL record */
380 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
381 dsp->dsa_drr->drr_type = DRR_SPILL;
382 drrs->drr_object = object;
383 drrs->drr_length = blksz;
384 drrs->drr_toguid = dsp->dsa_toguid;
386 if (dump_record(dsp, data, blksz) != 0)
387 return (SET_ERROR(EINTR));
392 dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
394 struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
397 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
398 * push it out, since free block aggregation can only be done for
399 * blocks of the same type (i.e., DRR_FREE records can only be
400 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
401 * can only be aggregated with other DRR_FREEOBJECTS records.
403 if (dsp->dsa_pending_op != PENDING_NONE &&
404 dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
405 if (dump_record(dsp, NULL, 0) != 0)
406 return (SET_ERROR(EINTR));
407 dsp->dsa_pending_op = PENDING_NONE;
409 if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
411 * See whether this free object array can be aggregated
414 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
415 drrfo->drr_numobjs += numobjs;
418 /* can't be aggregated. Push out pending record */
419 if (dump_record(dsp, NULL, 0) != 0)
420 return (SET_ERROR(EINTR));
421 dsp->dsa_pending_op = PENDING_NONE;
425 /* write a FREEOBJECTS record */
426 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
427 dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
428 drrfo->drr_firstobj = firstobj;
429 drrfo->drr_numobjs = numobjs;
430 drrfo->drr_toguid = dsp->dsa_toguid;
432 dsp->dsa_pending_op = PENDING_FREEOBJECTS;
438 dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
440 struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
442 if (object < dsp->dsa_resume_object) {
444 * Note: when resuming, we will visit all the dnodes in
445 * the block of dnodes that we are resuming from. In
446 * this case it's unnecessary to send the dnodes prior to
447 * the one we are resuming from. We should be at most one
448 * block's worth of dnodes behind the resume point.
450 ASSERT3U(dsp->dsa_resume_object - object, <,
451 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
455 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
456 return (dump_freeobjects(dsp, object, 1));
458 if (dsp->dsa_pending_op != PENDING_NONE) {
459 if (dump_record(dsp, NULL, 0) != 0)
460 return (SET_ERROR(EINTR));
461 dsp->dsa_pending_op = PENDING_NONE;
464 /* write an OBJECT record */
465 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
466 dsp->dsa_drr->drr_type = DRR_OBJECT;
467 drro->drr_object = object;
468 drro->drr_type = dnp->dn_type;
469 drro->drr_bonustype = dnp->dn_bonustype;
470 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
471 drro->drr_bonuslen = dnp->dn_bonuslen;
472 drro->drr_checksumtype = dnp->dn_checksum;
473 drro->drr_compress = dnp->dn_compress;
474 drro->drr_toguid = dsp->dsa_toguid;
476 if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
477 drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
478 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
480 if (dump_record(dsp, DN_BONUS(dnp),
481 P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
482 return (SET_ERROR(EINTR));
485 /* Free anything past the end of the file. */
486 if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
487 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
488 return (SET_ERROR(EINTR));
489 if (dsp->dsa_err != 0)
490 return (SET_ERROR(EINTR));
495 backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
497 if (!BP_IS_EMBEDDED(bp))
501 * Compression function must be legacy, or explicitly enabled.
503 if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
504 !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
508 * Embed type must be explicitly enabled.
510 switch (BPE_GET_ETYPE(bp)) {
511 case BP_EMBEDDED_TYPE_DATA:
512 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
522 * This is the callback function to traverse_dataset that acts as the worker
523 * thread for dmu_send_impl.
527 send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
528 const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
530 struct send_thread_arg *sta = arg;
531 struct send_block_record *record;
532 uint64_t record_size;
535 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
536 zb->zb_object >= sta->resume.zb_object);
539 return (SET_ERROR(EINTR));
542 ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
544 } else if (zb->zb_level < 0) {
548 record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
549 record->eos_marker = B_FALSE;
552 record->indblkshift = dnp->dn_indblkshift;
553 record->datablkszsec = dnp->dn_datablkszsec;
554 record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
555 bqueue_enqueue(&sta->q, record, record_size);
561 * This function kicks off the traverse_dataset. It also handles setting the
562 * error code of the thread in case something goes wrong, and pushes the End of
563 * Stream record when the traverse_dataset call has finished. If there is no
564 * dataset to traverse, the thread immediately pushes End of Stream marker.
567 send_traverse_thread(void *arg)
569 struct send_thread_arg *st_arg = arg;
571 struct send_block_record *data;
573 if (st_arg->ds != NULL) {
574 err = traverse_dataset_resume(st_arg->ds,
575 st_arg->fromtxg, &st_arg->resume,
576 st_arg->flags, send_cb, st_arg);
579 st_arg->error_code = err;
581 data = kmem_zalloc(sizeof (*data), KM_SLEEP);
582 data->eos_marker = B_TRUE;
583 bqueue_enqueue(&st_arg->q, data, 1);
588 * This function actually handles figuring out what kind of record needs to be
589 * dumped, reading the data (which has hopefully been prefetched), and calling
590 * the appropriate helper function.
593 do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
595 dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
596 const blkptr_t *bp = &data->bp;
597 const zbookmark_phys_t *zb = &data->zb;
598 uint8_t indblkshift = data->indblkshift;
599 uint16_t dblkszsec = data->datablkszsec;
600 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
601 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
604 ASSERT3U(zb->zb_level, >=, 0);
606 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
607 zb->zb_object >= dsa->dsa_resume_object);
609 if (zb->zb_object != DMU_META_DNODE_OBJECT &&
610 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
612 } else if (BP_IS_HOLE(bp) &&
613 zb->zb_object == DMU_META_DNODE_OBJECT) {
614 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
615 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
616 err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
617 } else if (BP_IS_HOLE(bp)) {
618 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
619 uint64_t offset = zb->zb_blkid * span;
620 err = dump_free(dsa, zb->zb_object, offset, span);
621 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
623 } else if (type == DMU_OT_DNODE) {
624 int blksz = BP_GET_LSIZE(bp);
625 arc_flags_t aflags = ARC_FLAG_WAIT;
628 ASSERT0(zb->zb_level);
630 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
631 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
633 return (SET_ERROR(EIO));
635 dnode_phys_t *blk = abuf->b_data;
636 uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
637 for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
638 err = dump_dnode(dsa, dnobj + i, blk + i);
642 arc_buf_destroy(abuf, &abuf);
643 } else if (type == DMU_OT_SA) {
644 arc_flags_t aflags = ARC_FLAG_WAIT;
646 int blksz = BP_GET_LSIZE(bp);
648 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
649 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
651 return (SET_ERROR(EIO));
653 err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
654 arc_buf_destroy(abuf, &abuf);
655 } else if (backup_do_embed(dsa, bp)) {
656 /* it's an embedded level-0 block of a regular object */
657 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
658 ASSERT0(zb->zb_level);
659 err = dump_write_embedded(dsa, zb->zb_object,
660 zb->zb_blkid * blksz, blksz, bp);
662 /* it's a level-0 block of a regular object */
663 arc_flags_t aflags = ARC_FLAG_WAIT;
665 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
668 ASSERT0(zb->zb_level);
669 ASSERT(zb->zb_object > dsa->dsa_resume_object ||
670 (zb->zb_object == dsa->dsa_resume_object &&
671 zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
673 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
674 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
676 if (zfs_send_corrupt_data) {
677 /* Send a block filled with 0x"zfs badd bloc" */
678 abuf = arc_alloc_buf(spa, blksz, &abuf,
681 for (ptr = abuf->b_data;
682 (char *)ptr < (char *)abuf->b_data + blksz;
684 *ptr = 0x2f5baddb10cULL;
686 return (SET_ERROR(EIO));
690 offset = zb->zb_blkid * blksz;
692 if (!(dsa->dsa_featureflags &
693 DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
694 blksz > SPA_OLD_MAXBLOCKSIZE) {
695 char *buf = abuf->b_data;
696 while (blksz > 0 && err == 0) {
697 int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
698 err = dump_write(dsa, type, zb->zb_object,
699 offset, n, NULL, buf);
705 err = dump_write(dsa, type, zb->zb_object,
706 offset, blksz, bp, abuf->b_data);
708 arc_buf_destroy(abuf, &abuf);
711 ASSERT(err == 0 || err == EINTR);
716 * Pop the new data off the queue, and free the old data.
718 static struct send_block_record *
719 get_next_record(bqueue_t *bq, struct send_block_record *data)
721 struct send_block_record *tmp = bqueue_dequeue(bq);
722 kmem_free(data, sizeof (*data));
727 * Actually do the bulk of the work in a zfs send.
729 * Note: Releases dp using the specified tag.
732 dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
733 zfs_bookmark_phys_t *ancestor_zb,
734 boolean_t is_clone, boolean_t embedok, boolean_t large_block_ok, int outfd,
735 uint64_t resumeobj, uint64_t resumeoff,
737 vnode_t *vp, offset_t *off)
739 struct file *fp, offset_t *off)
743 dmu_replay_record_t *drr;
746 uint64_t fromtxg = 0;
747 uint64_t featureflags = 0;
748 struct send_thread_arg to_arg = { 0 };
750 err = dmu_objset_from_ds(to_ds, &os);
752 dsl_pool_rele(dp, tag);
756 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
757 drr->drr_type = DRR_BEGIN;
758 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
759 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
763 if (dmu_objset_type(os) == DMU_OST_ZFS) {
765 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
766 kmem_free(drr, sizeof (dmu_replay_record_t));
767 dsl_pool_rele(dp, tag);
768 return (SET_ERROR(EINVAL));
770 if (version >= ZPL_VERSION_SA) {
771 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
776 if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
777 featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
779 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
780 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
781 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
782 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
785 if (resumeobj != 0 || resumeoff != 0) {
786 featureflags |= DMU_BACKUP_FEATURE_RESUMING;
789 DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
792 drr->drr_u.drr_begin.drr_creation_time =
793 dsl_dataset_phys(to_ds)->ds_creation_time;
794 drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
796 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
797 drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
798 if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
799 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
800 if (zfs_send_set_freerecords_bit)
801 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
803 if (ancestor_zb != NULL) {
804 drr->drr_u.drr_begin.drr_fromguid =
805 ancestor_zb->zbm_guid;
806 fromtxg = ancestor_zb->zbm_creation_txg;
808 dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
809 if (!to_ds->ds_is_snapshot) {
810 (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
811 sizeof (drr->drr_u.drr_begin.drr_toname));
814 dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
817 dsp->dsa_outfd = outfd;
818 dsp->dsa_proc = curproc;
819 dsp->dsa_td = curthread;
823 dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
824 dsp->dsa_pending_op = PENDING_NONE;
825 dsp->dsa_featureflags = featureflags;
826 dsp->dsa_resume_object = resumeobj;
827 dsp->dsa_resume_offset = resumeoff;
829 mutex_enter(&to_ds->ds_sendstream_lock);
830 list_insert_head(&to_ds->ds_sendstreams, dsp);
831 mutex_exit(&to_ds->ds_sendstream_lock);
833 dsl_dataset_long_hold(to_ds, FTAG);
834 dsl_pool_rele(dp, tag);
836 void *payload = NULL;
837 size_t payload_len = 0;
838 if (resumeobj != 0 || resumeoff != 0) {
839 dmu_object_info_t to_doi;
840 err = dmu_object_info(os, resumeobj, &to_doi);
843 SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
844 resumeoff / to_doi.doi_data_block_size);
846 nvlist_t *nvl = fnvlist_alloc();
847 fnvlist_add_uint64(nvl, "resume_object", resumeobj);
848 fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
849 payload = fnvlist_pack(nvl, &payload_len);
850 drr->drr_payloadlen = payload_len;
854 err = dump_record(dsp, payload, payload_len);
855 fnvlist_pack_free(payload, payload_len);
861 err = bqueue_init(&to_arg.q, zfs_send_queue_length,
862 offsetof(struct send_block_record, ln));
863 to_arg.error_code = 0;
864 to_arg.cancel = B_FALSE;
866 to_arg.fromtxg = fromtxg;
867 to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
868 (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, &p0,
869 TS_RUN, minclsyspri);
871 struct send_block_record *to_data;
872 to_data = bqueue_dequeue(&to_arg.q);
874 while (!to_data->eos_marker && err == 0) {
875 err = do_dump(dsp, to_data);
876 to_data = get_next_record(&to_arg.q, to_data);
877 if (issig(JUSTLOOKING) && issig(FORREAL))
882 to_arg.cancel = B_TRUE;
883 while (!to_data->eos_marker) {
884 to_data = get_next_record(&to_arg.q, to_data);
887 kmem_free(to_data, sizeof (*to_data));
889 bqueue_destroy(&to_arg.q);
891 if (err == 0 && to_arg.error_code != 0)
892 err = to_arg.error_code;
897 if (dsp->dsa_pending_op != PENDING_NONE)
898 if (dump_record(dsp, NULL, 0) != 0)
899 err = SET_ERROR(EINTR);
902 if (err == EINTR && dsp->dsa_err != 0)
907 bzero(drr, sizeof (dmu_replay_record_t));
908 drr->drr_type = DRR_END;
909 drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
910 drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
912 if (dump_record(dsp, NULL, 0) != 0)
916 mutex_enter(&to_ds->ds_sendstream_lock);
917 list_remove(&to_ds->ds_sendstreams, dsp);
918 mutex_exit(&to_ds->ds_sendstream_lock);
920 VERIFY(err != 0 || (dsp->dsa_sent_begin && dsp->dsa_sent_end));
922 kmem_free(drr, sizeof (dmu_replay_record_t));
923 kmem_free(dsp, sizeof (dmu_sendarg_t));
925 dsl_dataset_long_rele(to_ds, FTAG);
931 dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
932 boolean_t embedok, boolean_t large_block_ok,
934 int outfd, vnode_t *vp, offset_t *off)
936 int outfd, struct file *fp, offset_t *off)
941 dsl_dataset_t *fromds = NULL;
944 err = dsl_pool_hold(pool, FTAG, &dp);
948 err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
950 dsl_pool_rele(dp, FTAG);
955 zfs_bookmark_phys_t zb;
958 err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
960 dsl_dataset_rele(ds, FTAG);
961 dsl_pool_rele(dp, FTAG);
964 if (!dsl_dataset_is_before(ds, fromds, 0))
965 err = SET_ERROR(EXDEV);
966 zb.zbm_creation_time =
967 dsl_dataset_phys(fromds)->ds_creation_time;
968 zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
969 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
970 is_clone = (fromds->ds_dir != ds->ds_dir);
971 dsl_dataset_rele(fromds, FTAG);
972 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
973 embedok, large_block_ok, outfd, 0, 0, fp, off);
975 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
976 embedok, large_block_ok, outfd, 0, 0, fp, off);
978 dsl_dataset_rele(ds, FTAG);
983 dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
984 boolean_t large_block_ok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
986 vnode_t *vp, offset_t *off)
988 struct file *fp, offset_t *off)
994 boolean_t owned = B_FALSE;
996 if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
997 return (SET_ERROR(EINVAL));
999 err = dsl_pool_hold(tosnap, FTAG, &dp);
1003 if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
1005 * We are sending a filesystem or volume. Ensure
1006 * that it doesn't change by owning the dataset.
1008 err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
1011 err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
1014 dsl_pool_rele(dp, FTAG);
1018 if (fromsnap != NULL) {
1019 zfs_bookmark_phys_t zb;
1020 boolean_t is_clone = B_FALSE;
1021 int fsnamelen = strchr(tosnap, '@') - tosnap;
1024 * If the fromsnap is in a different filesystem, then
1025 * mark the send stream as a clone.
1027 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
1028 (fromsnap[fsnamelen] != '@' &&
1029 fromsnap[fsnamelen] != '#')) {
1033 if (strchr(fromsnap, '@')) {
1034 dsl_dataset_t *fromds;
1035 err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
1037 if (!dsl_dataset_is_before(ds, fromds, 0))
1038 err = SET_ERROR(EXDEV);
1039 zb.zbm_creation_time =
1040 dsl_dataset_phys(fromds)->ds_creation_time;
1041 zb.zbm_creation_txg =
1042 dsl_dataset_phys(fromds)->ds_creation_txg;
1043 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
1044 is_clone = (ds->ds_dir != fromds->ds_dir);
1045 dsl_dataset_rele(fromds, FTAG);
1048 err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
1051 dsl_dataset_rele(ds, FTAG);
1052 dsl_pool_rele(dp, FTAG);
1055 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
1056 embedok, large_block_ok,
1057 outfd, resumeobj, resumeoff, fp, off);
1059 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1060 embedok, large_block_ok,
1061 outfd, resumeobj, resumeoff, fp, off);
1064 dsl_dataset_disown(ds, FTAG);
1066 dsl_dataset_rele(ds, FTAG);
1071 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t size,
1076 * Assume that space (both on-disk and in-stream) is dominated by
1077 * data. We will adjust for indirect blocks and the copies property,
1078 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1082 * Subtract out approximate space used by indirect blocks.
1083 * Assume most space is used by data blocks (non-indirect, non-dnode).
1084 * Assume all blocks are recordsize. Assume ditto blocks and
1085 * internal fragmentation counter out compression.
1087 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1088 * block, which we observe in practice.
1090 uint64_t recordsize;
1091 err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
1094 size -= size / recordsize * sizeof (blkptr_t);
1096 /* Add in the space for the record associated with each block. */
1097 size += size / recordsize * sizeof (dmu_replay_record_t);
1105 dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
1107 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1111 ASSERT(dsl_pool_config_held(dp));
1113 /* tosnap must be a snapshot */
1114 if (!ds->ds_is_snapshot)
1115 return (SET_ERROR(EINVAL));
1117 /* fromsnap, if provided, must be a snapshot */
1118 if (fromds != NULL && !fromds->ds_is_snapshot)
1119 return (SET_ERROR(EINVAL));
1122 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1123 * or the origin's fs.
1125 if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1126 return (SET_ERROR(EXDEV));
1128 /* Get uncompressed size estimate of changed data. */
1129 if (fromds == NULL) {
1130 size = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1132 uint64_t used, comp;
1133 err = dsl_dataset_space_written(fromds, ds,
1134 &used, &comp, &size);
1139 err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1144 * Simple callback used to traverse the blocks of a snapshot and sum their
1149 dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1150 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1152 uint64_t *spaceptr = arg;
1153 if (bp != NULL && !BP_IS_HOLE(bp)) {
1154 *spaceptr += BP_GET_UCSIZE(bp);
1160 * Given a desination snapshot and a TXG, calculate the approximate size of a
1161 * send stream sent from that TXG. from_txg may be zero, indicating that the
1162 * whole snapshot will be sent.
1165 dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
1168 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1172 ASSERT(dsl_pool_config_held(dp));
1174 /* tosnap must be a snapshot */
1175 if (!dsl_dataset_is_snapshot(ds))
1176 return (SET_ERROR(EINVAL));
1178 /* verify that from_txg is before the provided snapshot was taken */
1179 if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1180 return (SET_ERROR(EXDEV));
1184 * traverse the blocks of the snapshot with birth times after
1185 * from_txg, summing their uncompressed size
1187 err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1188 dmu_calculate_send_traversal, &size);
1192 err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1196 typedef struct dmu_recv_begin_arg {
1197 const char *drba_origin;
1198 dmu_recv_cookie_t *drba_cookie;
1200 uint64_t drba_snapobj;
1201 } dmu_recv_begin_arg_t;
1204 recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
1209 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1211 /* temporary clone name must not exist */
1212 error = zap_lookup(dp->dp_meta_objset,
1213 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
1215 if (error != ENOENT)
1216 return (error == 0 ? EBUSY : error);
1218 /* new snapshot name must not exist */
1219 error = zap_lookup(dp->dp_meta_objset,
1220 dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1221 drba->drba_cookie->drc_tosnap, 8, 1, &val);
1222 if (error != ENOENT)
1223 return (error == 0 ? EEXIST : error);
1226 * Check snapshot limit before receiving. We'll recheck again at the
1227 * end, but might as well abort before receiving if we're already over
1230 * Note that we do not check the file system limit with
1231 * dsl_dir_fscount_check because the temporary %clones don't count
1232 * against that limit.
1234 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1235 NULL, drba->drba_cred);
1239 if (fromguid != 0) {
1240 dsl_dataset_t *snap;
1241 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1243 /* Find snapshot in this dir that matches fromguid. */
1245 error = dsl_dataset_hold_obj(dp, obj, FTAG,
1248 return (SET_ERROR(ENODEV));
1249 if (snap->ds_dir != ds->ds_dir) {
1250 dsl_dataset_rele(snap, FTAG);
1251 return (SET_ERROR(ENODEV));
1253 if (dsl_dataset_phys(snap)->ds_guid == fromguid)
1255 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
1256 dsl_dataset_rele(snap, FTAG);
1259 return (SET_ERROR(ENODEV));
1261 if (drba->drba_cookie->drc_force) {
1262 drba->drba_snapobj = obj;
1265 * If we are not forcing, there must be no
1266 * changes since fromsnap.
1268 if (dsl_dataset_modified_since_snap(ds, snap)) {
1269 dsl_dataset_rele(snap, FTAG);
1270 return (SET_ERROR(ETXTBSY));
1272 drba->drba_snapobj = ds->ds_prev->ds_object;
1275 dsl_dataset_rele(snap, FTAG);
1277 /* if full, then must be forced */
1278 if (!drba->drba_cookie->drc_force)
1279 return (SET_ERROR(EEXIST));
1280 /* start from $ORIGIN@$ORIGIN, if supported */
1281 drba->drba_snapobj = dp->dp_origin_snap != NULL ?
1282 dp->dp_origin_snap->ds_object : 0;
1290 dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
1292 dmu_recv_begin_arg_t *drba = arg;
1293 dsl_pool_t *dp = dmu_tx_pool(tx);
1294 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1295 uint64_t fromguid = drrb->drr_fromguid;
1296 int flags = drrb->drr_flags;
1298 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1300 const char *tofs = drba->drba_cookie->drc_tofs;
1302 /* already checked */
1303 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1304 ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
1306 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1307 DMU_COMPOUNDSTREAM ||
1308 drrb->drr_type >= DMU_OST_NUMTYPES ||
1309 ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1310 return (SET_ERROR(EINVAL));
1312 /* Verify pool version supports SA if SA_SPILL feature set */
1313 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1314 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1315 return (SET_ERROR(ENOTSUP));
1317 if (drba->drba_cookie->drc_resumable &&
1318 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
1319 return (SET_ERROR(ENOTSUP));
1322 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1323 * record to a plan WRITE record, so the pool must have the
1324 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1325 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1327 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1328 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1329 return (SET_ERROR(ENOTSUP));
1330 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1331 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1332 return (SET_ERROR(ENOTSUP));
1335 * The receiving code doesn't know how to translate large blocks
1336 * to smaller ones, so the pool must have the LARGE_BLOCKS
1337 * feature enabled if the stream has LARGE_BLOCKS.
1339 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1340 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1341 return (SET_ERROR(ENOTSUP));
1343 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1345 /* target fs already exists; recv into temp clone */
1347 /* Can't recv a clone into an existing fs */
1348 if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
1349 dsl_dataset_rele(ds, FTAG);
1350 return (SET_ERROR(EINVAL));
1353 error = recv_begin_check_existing_impl(drba, ds, fromguid);
1354 dsl_dataset_rele(ds, FTAG);
1355 } else if (error == ENOENT) {
1356 /* target fs does not exist; must be a full backup or clone */
1357 char buf[ZFS_MAX_DATASET_NAME_LEN];
1360 * If it's a non-clone incremental, we are missing the
1361 * target fs, so fail the recv.
1363 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1365 return (SET_ERROR(ENOENT));
1368 * If we're receiving a full send as a clone, and it doesn't
1369 * contain all the necessary free records and freeobject
1370 * records, reject it.
1372 if (fromguid == 0 && drba->drba_origin &&
1373 !(flags & DRR_FLAG_FREERECORDS))
1374 return (SET_ERROR(EINVAL));
1376 /* Open the parent of tofs */
1377 ASSERT3U(strlen(tofs), <, sizeof (buf));
1378 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
1379 error = dsl_dataset_hold(dp, buf, FTAG, &ds);
1384 * Check filesystem and snapshot limits before receiving. We'll
1385 * recheck snapshot limits again at the end (we create the
1386 * filesystems and increment those counts during begin_sync).
1388 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1389 ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1391 dsl_dataset_rele(ds, FTAG);
1395 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1396 ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1398 dsl_dataset_rele(ds, FTAG);
1402 if (drba->drba_origin != NULL) {
1403 dsl_dataset_t *origin;
1404 error = dsl_dataset_hold(dp, drba->drba_origin,
1407 dsl_dataset_rele(ds, FTAG);
1410 if (!origin->ds_is_snapshot) {
1411 dsl_dataset_rele(origin, FTAG);
1412 dsl_dataset_rele(ds, FTAG);
1413 return (SET_ERROR(EINVAL));
1415 if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
1417 dsl_dataset_rele(origin, FTAG);
1418 dsl_dataset_rele(ds, FTAG);
1419 return (SET_ERROR(ENODEV));
1421 dsl_dataset_rele(origin, FTAG);
1423 dsl_dataset_rele(ds, FTAG);
1430 dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1432 dmu_recv_begin_arg_t *drba = arg;
1433 dsl_pool_t *dp = dmu_tx_pool(tx);
1434 objset_t *mos = dp->dp_meta_objset;
1435 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1436 const char *tofs = drba->drba_cookie->drc_tofs;
1437 dsl_dataset_t *ds, *newds;
1440 uint64_t crflags = 0;
1442 if (drrb->drr_flags & DRR_FLAG_CI_DATA)
1443 crflags |= DS_FLAG_CI_DATASET;
1445 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1447 /* create temporary clone */
1448 dsl_dataset_t *snap = NULL;
1449 if (drba->drba_snapobj != 0) {
1450 VERIFY0(dsl_dataset_hold_obj(dp,
1451 drba->drba_snapobj, FTAG, &snap));
1453 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
1454 snap, crflags, drba->drba_cred, tx);
1455 if (drba->drba_snapobj != 0)
1456 dsl_dataset_rele(snap, FTAG);
1457 dsl_dataset_rele(ds, FTAG);
1461 dsl_dataset_t *origin = NULL;
1463 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
1465 if (drba->drba_origin != NULL) {
1466 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
1470 /* Create new dataset. */
1471 dsobj = dsl_dataset_create_sync(dd,
1472 strrchr(tofs, '/') + 1,
1473 origin, crflags, drba->drba_cred, tx);
1475 dsl_dataset_rele(origin, FTAG);
1476 dsl_dir_rele(dd, FTAG);
1477 drba->drba_cookie->drc_newfs = B_TRUE;
1479 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
1481 if (drba->drba_cookie->drc_resumable) {
1482 dsl_dataset_zapify(newds, tx);
1483 if (drrb->drr_fromguid != 0) {
1484 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
1485 8, 1, &drrb->drr_fromguid, tx));
1487 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
1488 8, 1, &drrb->drr_toguid, tx));
1489 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
1490 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
1493 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
1495 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
1497 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
1499 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1500 DMU_BACKUP_FEATURE_EMBED_DATA) {
1501 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
1506 dmu_buf_will_dirty(newds->ds_dbuf, tx);
1507 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
1510 * If we actually created a non-clone, we need to create the
1511 * objset in our new dataset.
1513 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1514 (void) dmu_objset_create_impl(dp->dp_spa,
1515 newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1518 drba->drba_cookie->drc_ds = newds;
1520 spa_history_log_internal_ds(newds, "receive", tx, "");
1524 dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1526 dmu_recv_begin_arg_t *drba = arg;
1527 dsl_pool_t *dp = dmu_tx_pool(tx);
1528 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1530 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1532 const char *tofs = drba->drba_cookie->drc_tofs;
1534 /* already checked */
1535 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1536 ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
1538 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1539 DMU_COMPOUNDSTREAM ||
1540 drrb->drr_type >= DMU_OST_NUMTYPES)
1541 return (SET_ERROR(EINVAL));
1543 /* Verify pool version supports SA if SA_SPILL feature set */
1544 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1545 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1546 return (SET_ERROR(ENOTSUP));
1549 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1550 * record to a plain WRITE record, so the pool must have the
1551 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1552 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1554 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1555 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1556 return (SET_ERROR(ENOTSUP));
1557 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1558 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1559 return (SET_ERROR(ENOTSUP));
1561 /* 6 extra bytes for /%recv */
1562 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1564 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1565 tofs, recv_clone_name);
1567 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1568 /* %recv does not exist; continue in tofs */
1569 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1574 /* check that ds is marked inconsistent */
1575 if (!DS_IS_INCONSISTENT(ds)) {
1576 dsl_dataset_rele(ds, FTAG);
1577 return (SET_ERROR(EINVAL));
1580 /* check that there is resuming data, and that the toguid matches */
1581 if (!dsl_dataset_is_zapified(ds)) {
1582 dsl_dataset_rele(ds, FTAG);
1583 return (SET_ERROR(EINVAL));
1586 error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1587 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1588 if (error != 0 || drrb->drr_toguid != val) {
1589 dsl_dataset_rele(ds, FTAG);
1590 return (SET_ERROR(EINVAL));
1594 * Check if the receive is still running. If so, it will be owned.
1595 * Note that nothing else can own the dataset (e.g. after the receive
1596 * fails) because it will be marked inconsistent.
1598 if (dsl_dataset_has_owner(ds)) {
1599 dsl_dataset_rele(ds, FTAG);
1600 return (SET_ERROR(EBUSY));
1603 /* There should not be any snapshots of this fs yet. */
1604 if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1605 dsl_dataset_rele(ds, FTAG);
1606 return (SET_ERROR(EINVAL));
1610 * Note: resume point will be checked when we process the first WRITE
1614 /* check that the origin matches */
1616 (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1617 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1618 if (drrb->drr_fromguid != val) {
1619 dsl_dataset_rele(ds, FTAG);
1620 return (SET_ERROR(EINVAL));
1623 dsl_dataset_rele(ds, FTAG);
1628 dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1630 dmu_recv_begin_arg_t *drba = arg;
1631 dsl_pool_t *dp = dmu_tx_pool(tx);
1632 const char *tofs = drba->drba_cookie->drc_tofs;
1635 /* 6 extra bytes for /%recv */
1636 char recvname[ZFS_MAX_DATASET_NAME_LEN + 6];
1638 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1639 tofs, recv_clone_name);
1641 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1642 /* %recv does not exist; continue in tofs */
1643 VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
1644 drba->drba_cookie->drc_newfs = B_TRUE;
1647 /* clear the inconsistent flag so that we can own it */
1648 ASSERT(DS_IS_INCONSISTENT(ds));
1649 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1650 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
1651 dsobj = ds->ds_object;
1652 dsl_dataset_rele(ds, FTAG);
1654 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
1656 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1657 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1659 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
1661 drba->drba_cookie->drc_ds = ds;
1663 spa_history_log_internal_ds(ds, "resume receive", tx, "");
1667 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1668 * succeeds; otherwise we will leak the holds on the datasets.
1671 dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
1672 boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1674 dmu_recv_begin_arg_t drba = { 0 };
1676 bzero(drc, sizeof (dmu_recv_cookie_t));
1677 drc->drc_drr_begin = drr_begin;
1678 drc->drc_drrb = &drr_begin->drr_u.drr_begin;
1679 drc->drc_tosnap = tosnap;
1680 drc->drc_tofs = tofs;
1681 drc->drc_force = force;
1682 drc->drc_resumable = resumable;
1683 drc->drc_cred = CRED();
1685 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1686 drc->drc_byteswap = B_TRUE;
1687 fletcher_4_incremental_byteswap(drr_begin,
1688 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1689 byteswap_record(drr_begin);
1690 } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1691 fletcher_4_incremental_native(drr_begin,
1692 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1694 return (SET_ERROR(EINVAL));
1697 drba.drba_origin = origin;
1698 drba.drba_cookie = drc;
1699 drba.drba_cred = CRED();
1701 if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1702 DMU_BACKUP_FEATURE_RESUMING) {
1703 return (dsl_sync_task(tofs,
1704 dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
1705 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1707 return (dsl_sync_task(tofs,
1708 dmu_recv_begin_check, dmu_recv_begin_sync,
1709 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1713 struct receive_record_arg {
1714 dmu_replay_record_t header;
1715 void *payload; /* Pointer to a buffer containing the payload */
1717 * If the record is a write, pointer to the arc_buf_t containing the
1720 arc_buf_t *write_buf;
1722 uint64_t bytes_read; /* bytes read from stream when record created */
1723 boolean_t eos_marker; /* Marks the end of the stream */
1727 struct receive_writer_arg {
1733 * These three args are used to signal to the main thread that we're
1741 /* A map from guid to dataset to help handle dedup'd streams. */
1742 avl_tree_t *guid_to_ds_map;
1743 boolean_t resumable;
1744 uint64_t last_object, last_offset;
1745 uint64_t bytes_read; /* bytes read when current record created */
1749 list_t list; /* List of struct receive_objnode. */
1751 * Last object looked up. Used to assert that objects are being looked
1752 * up in ascending order.
1754 uint64_t last_lookup;
1757 struct receive_objnode {
1762 struct receive_arg {
1766 uint64_t voff; /* The current offset in the stream */
1767 uint64_t bytes_read;
1769 * A record that has had its payload read in, but hasn't yet been handed
1770 * off to the worker thread.
1772 struct receive_record_arg *rrd;
1773 /* A record that has had its header read in, but not its payload. */
1774 struct receive_record_arg *next_rrd;
1776 zio_cksum_t prev_cksum;
1779 /* Sorted list of objects not to issue prefetches for. */
1780 struct objlist ignore_objlist;
1783 typedef struct guid_map_entry {
1785 dsl_dataset_t *gme_ds;
1790 guid_compare(const void *arg1, const void *arg2)
1792 const guid_map_entry_t *gmep1 = arg1;
1793 const guid_map_entry_t *gmep2 = arg2;
1795 if (gmep1->guid < gmep2->guid)
1797 else if (gmep1->guid > gmep2->guid)
1803 free_guid_map_onexit(void *arg)
1805 avl_tree_t *ca = arg;
1806 void *cookie = NULL;
1807 guid_map_entry_t *gmep;
1809 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
1810 dsl_dataset_long_rele(gmep->gme_ds, gmep);
1811 dsl_dataset_rele(gmep->gme_ds, gmep);
1812 kmem_free(gmep, sizeof (guid_map_entry_t));
1815 kmem_free(ca, sizeof (avl_tree_t));
1819 restore_bytes(struct receive_arg *ra, void *buf, int len, off_t off, ssize_t *resid)
1825 aiov.iov_base = buf;
1827 auio.uio_iov = &aiov;
1828 auio.uio_iovcnt = 1;
1829 auio.uio_resid = len;
1830 auio.uio_segflg = UIO_SYSSPACE;
1831 auio.uio_rw = UIO_READ;
1832 auio.uio_offset = off;
1833 auio.uio_td = ra->td;
1835 error = fo_read(ra->fp, &auio, ra->td->td_ucred, FOF_OFFSET, ra->td);
1837 fprintf(stderr, "%s: returning EOPNOTSUPP\n", __func__);
1840 *resid = auio.uio_resid;
1845 receive_read(struct receive_arg *ra, int len, void *buf)
1850 * The code doesn't rely on this (lengths being multiples of 8). See
1851 * comment in dump_bytes.
1855 while (done < len) {
1858 ra->err = restore_bytes(ra, buf + done,
1859 len - done, ra->voff, &resid);
1861 if (resid == len - done) {
1863 * Note: ECKSUM indicates that the receive
1864 * was interrupted and can potentially be resumed.
1866 ra->err = SET_ERROR(ECKSUM);
1868 ra->voff += len - done - resid;
1874 ra->bytes_read += len;
1876 ASSERT3U(done, ==, len);
1881 byteswap_record(dmu_replay_record_t *drr)
1883 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1884 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1885 drr->drr_type = BSWAP_32(drr->drr_type);
1886 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
1888 switch (drr->drr_type) {
1890 DO64(drr_begin.drr_magic);
1891 DO64(drr_begin.drr_versioninfo);
1892 DO64(drr_begin.drr_creation_time);
1893 DO32(drr_begin.drr_type);
1894 DO32(drr_begin.drr_flags);
1895 DO64(drr_begin.drr_toguid);
1896 DO64(drr_begin.drr_fromguid);
1899 DO64(drr_object.drr_object);
1900 DO32(drr_object.drr_type);
1901 DO32(drr_object.drr_bonustype);
1902 DO32(drr_object.drr_blksz);
1903 DO32(drr_object.drr_bonuslen);
1904 DO64(drr_object.drr_toguid);
1906 case DRR_FREEOBJECTS:
1907 DO64(drr_freeobjects.drr_firstobj);
1908 DO64(drr_freeobjects.drr_numobjs);
1909 DO64(drr_freeobjects.drr_toguid);
1912 DO64(drr_write.drr_object);
1913 DO32(drr_write.drr_type);
1914 DO64(drr_write.drr_offset);
1915 DO64(drr_write.drr_length);
1916 DO64(drr_write.drr_toguid);
1917 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
1918 DO64(drr_write.drr_key.ddk_prop);
1920 case DRR_WRITE_BYREF:
1921 DO64(drr_write_byref.drr_object);
1922 DO64(drr_write_byref.drr_offset);
1923 DO64(drr_write_byref.drr_length);
1924 DO64(drr_write_byref.drr_toguid);
1925 DO64(drr_write_byref.drr_refguid);
1926 DO64(drr_write_byref.drr_refobject);
1927 DO64(drr_write_byref.drr_refoffset);
1928 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
1930 DO64(drr_write_byref.drr_key.ddk_prop);
1932 case DRR_WRITE_EMBEDDED:
1933 DO64(drr_write_embedded.drr_object);
1934 DO64(drr_write_embedded.drr_offset);
1935 DO64(drr_write_embedded.drr_length);
1936 DO64(drr_write_embedded.drr_toguid);
1937 DO32(drr_write_embedded.drr_lsize);
1938 DO32(drr_write_embedded.drr_psize);
1941 DO64(drr_free.drr_object);
1942 DO64(drr_free.drr_offset);
1943 DO64(drr_free.drr_length);
1944 DO64(drr_free.drr_toguid);
1947 DO64(drr_spill.drr_object);
1948 DO64(drr_spill.drr_length);
1949 DO64(drr_spill.drr_toguid);
1952 DO64(drr_end.drr_toguid);
1953 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1957 if (drr->drr_type != DRR_BEGIN) {
1958 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
1965 static inline uint8_t
1966 deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1968 if (bonus_type == DMU_OT_SA) {
1972 ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
1977 save_resume_state(struct receive_writer_arg *rwa,
1978 uint64_t object, uint64_t offset, dmu_tx_t *tx)
1980 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1982 if (!rwa->resumable)
1986 * We use ds_resume_bytes[] != 0 to indicate that we need to
1987 * update this on disk, so it must not be 0.
1989 ASSERT(rwa->bytes_read != 0);
1992 * We only resume from write records, which have a valid
1993 * (non-meta-dnode) object number.
1995 ASSERT(object != 0);
1998 * For resuming to work correctly, we must receive records in order,
1999 * sorted by object,offset. This is checked by the callers, but
2000 * assert it here for good measure.
2002 ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
2003 ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
2004 offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
2005 ASSERT3U(rwa->bytes_read, >=,
2006 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
2008 rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
2009 rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
2010 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
2014 receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
2017 dmu_object_info_t doi;
2022 if (drro->drr_type == DMU_OT_NONE ||
2023 !DMU_OT_IS_VALID(drro->drr_type) ||
2024 !DMU_OT_IS_VALID(drro->drr_bonustype) ||
2025 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
2026 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
2027 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
2028 drro->drr_blksz < SPA_MINBLOCKSIZE ||
2029 drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
2030 drro->drr_bonuslen > DN_MAX_BONUSLEN) {
2031 return (SET_ERROR(EINVAL));
2034 err = dmu_object_info(rwa->os, drro->drr_object, &doi);
2036 if (err != 0 && err != ENOENT)
2037 return (SET_ERROR(EINVAL));
2038 object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
2041 * If we are losing blkptrs or changing the block size this must
2042 * be a new file instance. We must clear out the previous file
2043 * contents before we can change this type of metadata in the dnode.
2048 nblkptr = deduce_nblkptr(drro->drr_bonustype,
2049 drro->drr_bonuslen);
2051 if (drro->drr_blksz != doi.doi_data_block_size ||
2052 nblkptr < doi.doi_nblkptr) {
2053 err = dmu_free_long_range(rwa->os, drro->drr_object,
2056 return (SET_ERROR(EINVAL));
2060 tx = dmu_tx_create(rwa->os);
2061 dmu_tx_hold_bonus(tx, object);
2062 err = dmu_tx_assign(tx, TXG_WAIT);
2068 if (object == DMU_NEW_OBJECT) {
2069 /* currently free, want to be allocated */
2070 err = dmu_object_claim(rwa->os, drro->drr_object,
2071 drro->drr_type, drro->drr_blksz,
2072 drro->drr_bonustype, drro->drr_bonuslen, tx);
2073 } else if (drro->drr_type != doi.doi_type ||
2074 drro->drr_blksz != doi.doi_data_block_size ||
2075 drro->drr_bonustype != doi.doi_bonus_type ||
2076 drro->drr_bonuslen != doi.doi_bonus_size) {
2077 /* currently allocated, but with different properties */
2078 err = dmu_object_reclaim(rwa->os, drro->drr_object,
2079 drro->drr_type, drro->drr_blksz,
2080 drro->drr_bonustype, drro->drr_bonuslen, tx);
2084 return (SET_ERROR(EINVAL));
2087 dmu_object_set_checksum(rwa->os, drro->drr_object,
2088 drro->drr_checksumtype, tx);
2089 dmu_object_set_compress(rwa->os, drro->drr_object,
2090 drro->drr_compress, tx);
2095 VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
2096 dmu_buf_will_dirty(db, tx);
2098 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
2099 bcopy(data, db->db_data, drro->drr_bonuslen);
2100 if (rwa->byteswap) {
2101 dmu_object_byteswap_t byteswap =
2102 DMU_OT_BYTESWAP(drro->drr_bonustype);
2103 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2104 drro->drr_bonuslen);
2106 dmu_buf_rele(db, FTAG);
2115 receive_freeobjects(struct receive_writer_arg *rwa,
2116 struct drr_freeobjects *drrfo)
2121 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2122 return (SET_ERROR(EINVAL));
2124 for (obj = drrfo->drr_firstobj;
2125 obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
2126 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2129 if (dmu_object_info(rwa->os, obj, NULL) != 0)
2132 err = dmu_free_long_object(rwa->os, obj);
2136 if (next_err != ESRCH)
2142 receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2148 if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
2149 !DMU_OT_IS_VALID(drrw->drr_type))
2150 return (SET_ERROR(EINVAL));
2153 * For resuming to work, records must be in increasing order
2154 * by (object, offset).
2156 if (drrw->drr_object < rwa->last_object ||
2157 (drrw->drr_object == rwa->last_object &&
2158 drrw->drr_offset < rwa->last_offset)) {
2159 return (SET_ERROR(EINVAL));
2161 rwa->last_object = drrw->drr_object;
2162 rwa->last_offset = drrw->drr_offset;
2164 if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
2165 return (SET_ERROR(EINVAL));
2167 tx = dmu_tx_create(rwa->os);
2169 dmu_tx_hold_write(tx, drrw->drr_object,
2170 drrw->drr_offset, drrw->drr_length);
2171 err = dmu_tx_assign(tx, TXG_WAIT);
2176 if (rwa->byteswap) {
2177 dmu_object_byteswap_t byteswap =
2178 DMU_OT_BYTESWAP(drrw->drr_type);
2179 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
2184 if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
2185 return (SET_ERROR(EINVAL));
2186 dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
2189 * Note: If the receive fails, we want the resume stream to start
2190 * with the same record that we last successfully received (as opposed
2191 * to the next record), so that we can verify that we are
2192 * resuming from the correct location.
2194 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2196 dmu_buf_rele(bonus, FTAG);
2202 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2203 * streams to refer to a copy of the data that is already on the
2204 * system because it came in earlier in the stream. This function
2205 * finds the earlier copy of the data, and uses that copy instead of
2206 * data from the stream to fulfill this write.
2209 receive_write_byref(struct receive_writer_arg *rwa,
2210 struct drr_write_byref *drrwbr)
2214 guid_map_entry_t gmesrch;
2215 guid_map_entry_t *gmep;
2217 objset_t *ref_os = NULL;
2220 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2221 return (SET_ERROR(EINVAL));
2224 * If the GUID of the referenced dataset is different from the
2225 * GUID of the target dataset, find the referenced dataset.
2227 if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
2228 gmesrch.guid = drrwbr->drr_refguid;
2229 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
2231 return (SET_ERROR(EINVAL));
2233 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2234 return (SET_ERROR(EINVAL));
2239 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
2240 drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
2244 tx = dmu_tx_create(rwa->os);
2246 dmu_tx_hold_write(tx, drrwbr->drr_object,
2247 drrwbr->drr_offset, drrwbr->drr_length);
2248 err = dmu_tx_assign(tx, TXG_WAIT);
2253 dmu_write(rwa->os, drrwbr->drr_object,
2254 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
2255 dmu_buf_rele(dbp, FTAG);
2257 /* See comment in restore_write. */
2258 save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
2264 receive_write_embedded(struct receive_writer_arg *rwa,
2265 struct drr_write_embedded *drrwe, void *data)
2270 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
2273 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
2276 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
2278 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
2281 tx = dmu_tx_create(rwa->os);
2283 dmu_tx_hold_write(tx, drrwe->drr_object,
2284 drrwe->drr_offset, drrwe->drr_length);
2285 err = dmu_tx_assign(tx, TXG_WAIT);
2291 dmu_write_embedded(rwa->os, drrwe->drr_object,
2292 drrwe->drr_offset, data, drrwe->drr_etype,
2293 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2294 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
2296 /* See comment in restore_write. */
2297 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
2303 receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2307 dmu_buf_t *db, *db_spill;
2310 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2311 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2312 return (SET_ERROR(EINVAL));
2314 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2315 return (SET_ERROR(EINVAL));
2317 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
2318 if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
2319 dmu_buf_rele(db, FTAG);
2323 tx = dmu_tx_create(rwa->os);
2325 dmu_tx_hold_spill(tx, db->db_object);
2327 err = dmu_tx_assign(tx, TXG_WAIT);
2329 dmu_buf_rele(db, FTAG);
2330 dmu_buf_rele(db_spill, FTAG);
2334 dmu_buf_will_dirty(db_spill, tx);
2336 if (db_spill->db_size < drrs->drr_length)
2337 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
2338 drrs->drr_length, tx));
2339 bcopy(data, db_spill->db_data, drrs->drr_length);
2341 dmu_buf_rele(db, FTAG);
2342 dmu_buf_rele(db_spill, FTAG);
2350 receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2354 if (drrf->drr_length != -1ULL &&
2355 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2356 return (SET_ERROR(EINVAL));
2358 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2359 return (SET_ERROR(EINVAL));
2361 err = dmu_free_long_range(rwa->os, drrf->drr_object,
2362 drrf->drr_offset, drrf->drr_length);
2367 /* used to destroy the drc_ds on error */
2369 dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
2371 if (drc->drc_resumable) {
2372 /* wait for our resume state to be written to disk */
2373 txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
2374 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2376 char name[ZFS_MAX_DATASET_NAME_LEN];
2377 dsl_dataset_name(drc->drc_ds, name);
2378 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2379 (void) dsl_destroy_head(name);
2384 receive_cksum(struct receive_arg *ra, int len, void *buf)
2387 fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
2389 fletcher_4_incremental_native(buf, len, &ra->cksum);
2394 * Read the payload into a buffer of size len, and update the current record's
2396 * Allocate ra->next_rrd and read the next record's header into
2397 * ra->next_rrd->header.
2398 * Verify checksum of payload and next record.
2401 receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
2406 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2407 err = receive_read(ra, len, buf);
2410 receive_cksum(ra, len, buf);
2412 /* note: rrd is NULL when reading the begin record's payload */
2413 if (ra->rrd != NULL) {
2414 ra->rrd->payload = buf;
2415 ra->rrd->payload_size = len;
2416 ra->rrd->bytes_read = ra->bytes_read;
2420 ra->prev_cksum = ra->cksum;
2422 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2423 err = receive_read(ra, sizeof (ra->next_rrd->header),
2424 &ra->next_rrd->header);
2425 ra->next_rrd->bytes_read = ra->bytes_read;
2427 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2428 ra->next_rrd = NULL;
2431 if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2432 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2433 ra->next_rrd = NULL;
2434 return (SET_ERROR(EINVAL));
2438 * Note: checksum is of everything up to but not including the
2441 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2442 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2444 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2445 &ra->next_rrd->header);
2447 zio_cksum_t cksum_orig =
2448 ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2449 zio_cksum_t *cksump =
2450 &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2453 byteswap_record(&ra->next_rrd->header);
2455 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2456 !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2457 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2458 ra->next_rrd = NULL;
2459 return (SET_ERROR(ECKSUM));
2462 receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
2468 objlist_create(struct objlist *list)
2470 list_create(&list->list, sizeof (struct receive_objnode),
2471 offsetof(struct receive_objnode, node));
2472 list->last_lookup = 0;
2476 objlist_destroy(struct objlist *list)
2478 for (struct receive_objnode *n = list_remove_head(&list->list);
2479 n != NULL; n = list_remove_head(&list->list)) {
2480 kmem_free(n, sizeof (*n));
2482 list_destroy(&list->list);
2486 * This function looks through the objlist to see if the specified object number
2487 * is contained in the objlist. In the process, it will remove all object
2488 * numbers in the list that are smaller than the specified object number. Thus,
2489 * any lookup of an object number smaller than a previously looked up object
2490 * number will always return false; therefore, all lookups should be done in
2494 objlist_exists(struct objlist *list, uint64_t object)
2496 struct receive_objnode *node = list_head(&list->list);
2497 ASSERT3U(object, >=, list->last_lookup);
2498 list->last_lookup = object;
2499 while (node != NULL && node->object < object) {
2500 VERIFY3P(node, ==, list_remove_head(&list->list));
2501 kmem_free(node, sizeof (*node));
2502 node = list_head(&list->list);
2504 return (node != NULL && node->object == object);
2508 * The objlist is a list of object numbers stored in ascending order. However,
2509 * the insertion of new object numbers does not seek out the correct location to
2510 * store a new object number; instead, it appends it to the list for simplicity.
2511 * Thus, any users must take care to only insert new object numbers in ascending
2515 objlist_insert(struct objlist *list, uint64_t object)
2517 struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
2518 node->object = object;
2520 struct receive_objnode *last_object = list_tail(&list->list);
2521 uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
2522 ASSERT3U(node->object, >, last_objnum);
2524 list_insert_tail(&list->list, node);
2528 * Issue the prefetch reads for any necessary indirect blocks.
2530 * We use the object ignore list to tell us whether or not to issue prefetches
2531 * for a given object. We do this for both correctness (in case the blocksize
2532 * of an object has changed) and performance (if the object doesn't exist, don't
2533 * needlessly try to issue prefetches). We also trim the list as we go through
2534 * the stream to prevent it from growing to an unbounded size.
2536 * The object numbers within will always be in sorted order, and any write
2537 * records we see will also be in sorted order, but they're not sorted with
2538 * respect to each other (i.e. we can get several object records before
2539 * receiving each object's write records). As a result, once we've reached a
2540 * given object number, we can safely remove any reference to lower object
2541 * numbers in the ignore list. In practice, we receive up to 32 object records
2542 * before receiving write records, so the list can have up to 32 nodes in it.
2546 receive_read_prefetch(struct receive_arg *ra,
2547 uint64_t object, uint64_t offset, uint64_t length)
2549 if (!objlist_exists(&ra->ignore_objlist, object)) {
2550 dmu_prefetch(ra->os, object, 1, offset, length,
2551 ZIO_PRIORITY_SYNC_READ);
2556 * Read records off the stream, issuing any necessary prefetches.
2559 receive_read_record(struct receive_arg *ra)
2563 switch (ra->rrd->header.drr_type) {
2566 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2567 uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2568 void *buf = kmem_zalloc(size, KM_SLEEP);
2569 dmu_object_info_t doi;
2570 err = receive_read_payload_and_next_header(ra, size, buf);
2572 kmem_free(buf, size);
2575 err = dmu_object_info(ra->os, drro->drr_object, &doi);
2577 * See receive_read_prefetch for an explanation why we're
2578 * storing this object in the ignore_obj_list.
2580 if (err == ENOENT ||
2581 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2582 objlist_insert(&ra->ignore_objlist, drro->drr_object);
2587 case DRR_FREEOBJECTS:
2589 err = receive_read_payload_and_next_header(ra, 0, NULL);
2594 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2595 arc_buf_t *abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2598 err = receive_read_payload_and_next_header(ra,
2599 drrw->drr_length, abuf->b_data);
2601 dmu_return_arcbuf(abuf);
2604 ra->rrd->write_buf = abuf;
2605 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2609 case DRR_WRITE_BYREF:
2611 struct drr_write_byref *drrwb =
2612 &ra->rrd->header.drr_u.drr_write_byref;
2613 err = receive_read_payload_and_next_header(ra, 0, NULL);
2614 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2618 case DRR_WRITE_EMBEDDED:
2620 struct drr_write_embedded *drrwe =
2621 &ra->rrd->header.drr_u.drr_write_embedded;
2622 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2623 void *buf = kmem_zalloc(size, KM_SLEEP);
2625 err = receive_read_payload_and_next_header(ra, size, buf);
2627 kmem_free(buf, size);
2631 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2638 * It might be beneficial to prefetch indirect blocks here, but
2639 * we don't really have the data to decide for sure.
2641 err = receive_read_payload_and_next_header(ra, 0, NULL);
2646 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
2647 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2648 return (SET_ERROR(ECKSUM));
2653 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2654 void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2655 err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2658 kmem_free(buf, drrs->drr_length);
2662 return (SET_ERROR(EINVAL));
2667 * Commit the records to the pool.
2670 receive_process_record(struct receive_writer_arg *rwa,
2671 struct receive_record_arg *rrd)
2675 /* Processing in order, therefore bytes_read should be increasing. */
2676 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2677 rwa->bytes_read = rrd->bytes_read;
2679 switch (rrd->header.drr_type) {
2682 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2683 err = receive_object(rwa, drro, rrd->payload);
2684 kmem_free(rrd->payload, rrd->payload_size);
2685 rrd->payload = NULL;
2688 case DRR_FREEOBJECTS:
2690 struct drr_freeobjects *drrfo =
2691 &rrd->header.drr_u.drr_freeobjects;
2692 return (receive_freeobjects(rwa, drrfo));
2696 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2697 err = receive_write(rwa, drrw, rrd->write_buf);
2698 /* if receive_write() is successful, it consumes the arc_buf */
2700 dmu_return_arcbuf(rrd->write_buf);
2701 rrd->write_buf = NULL;
2702 rrd->payload = NULL;
2705 case DRR_WRITE_BYREF:
2707 struct drr_write_byref *drrwbr =
2708 &rrd->header.drr_u.drr_write_byref;
2709 return (receive_write_byref(rwa, drrwbr));
2711 case DRR_WRITE_EMBEDDED:
2713 struct drr_write_embedded *drrwe =
2714 &rrd->header.drr_u.drr_write_embedded;
2715 err = receive_write_embedded(rwa, drrwe, rrd->payload);
2716 kmem_free(rrd->payload, rrd->payload_size);
2717 rrd->payload = NULL;
2722 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2723 return (receive_free(rwa, drrf));
2727 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2728 err = receive_spill(rwa, drrs, rrd->payload);
2729 kmem_free(rrd->payload, rrd->payload_size);
2730 rrd->payload = NULL;
2734 return (SET_ERROR(EINVAL));
2739 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2740 * receive_process_record When we're done, signal the main thread and exit.
2743 receive_writer_thread(void *arg)
2745 struct receive_writer_arg *rwa = arg;
2746 struct receive_record_arg *rrd;
2747 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2748 rrd = bqueue_dequeue(&rwa->q)) {
2750 * If there's an error, the main thread will stop putting things
2751 * on the queue, but we need to clear everything in it before we
2754 if (rwa->err == 0) {
2755 rwa->err = receive_process_record(rwa, rrd);
2756 } else if (rrd->write_buf != NULL) {
2757 dmu_return_arcbuf(rrd->write_buf);
2758 rrd->write_buf = NULL;
2759 rrd->payload = NULL;
2760 } else if (rrd->payload != NULL) {
2761 kmem_free(rrd->payload, rrd->payload_size);
2762 rrd->payload = NULL;
2764 kmem_free(rrd, sizeof (*rrd));
2766 kmem_free(rrd, sizeof (*rrd));
2767 mutex_enter(&rwa->mutex);
2769 cv_signal(&rwa->cv);
2770 mutex_exit(&rwa->mutex);
2775 resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
2778 objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
2779 uint64_t dsobj = dmu_objset_id(ra->os);
2780 uint64_t resume_obj, resume_off;
2782 if (nvlist_lookup_uint64(begin_nvl,
2783 "resume_object", &resume_obj) != 0 ||
2784 nvlist_lookup_uint64(begin_nvl,
2785 "resume_offset", &resume_off) != 0) {
2786 return (SET_ERROR(EINVAL));
2788 VERIFY0(zap_lookup(mos, dsobj,
2789 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
2790 if (resume_obj != val)
2791 return (SET_ERROR(EINVAL));
2792 VERIFY0(zap_lookup(mos, dsobj,
2793 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
2794 if (resume_off != val)
2795 return (SET_ERROR(EINVAL));
2801 * Read in the stream's records, one by one, and apply them to the pool. There
2802 * are two threads involved; the thread that calls this function will spin up a
2803 * worker thread, read the records off the stream one by one, and issue
2804 * prefetches for any necessary indirect blocks. It will then push the records
2805 * onto an internal blocking queue. The worker thread will pull the records off
2806 * the queue, and actually write the data into the DMU. This way, the worker
2807 * thread doesn't have to wait for reads to complete, since everything it needs
2808 * (the indirect blocks) will be prefetched.
2810 * NB: callers *must* call dmu_recv_end() if this succeeds.
2813 dmu_recv_stream(dmu_recv_cookie_t *drc, struct file *fp, offset_t *voffp,
2814 int cleanup_fd, uint64_t *action_handlep)
2817 struct receive_arg ra = { 0 };
2818 struct receive_writer_arg rwa = { 0 };
2820 nvlist_t *begin_nvl = NULL;
2822 ra.byteswap = drc->drc_byteswap;
2823 ra.cksum = drc->drc_cksum;
2828 if (dsl_dataset_is_zapified(drc->drc_ds)) {
2829 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
2830 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
2831 sizeof (ra.bytes_read), 1, &ra.bytes_read);
2834 objlist_create(&ra.ignore_objlist);
2836 /* these were verified in dmu_recv_begin */
2837 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
2839 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2842 * Open the objset we are modifying.
2844 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
2846 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2848 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
2850 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2851 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2854 if (cleanup_fd == -1) {
2855 ra.err = SET_ERROR(EBADF);
2858 ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
2864 if (*action_handlep == 0) {
2865 rwa.guid_to_ds_map =
2866 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2867 avl_create(rwa.guid_to_ds_map, guid_compare,
2868 sizeof (guid_map_entry_t),
2869 offsetof(guid_map_entry_t, avlnode));
2870 err = zfs_onexit_add_cb(minor,
2871 free_guid_map_onexit, rwa.guid_to_ds_map,
2876 err = zfs_onexit_cb_data(minor, *action_handlep,
2877 (void **)&rwa.guid_to_ds_map);
2882 drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
2885 uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
2886 void *payload = NULL;
2887 if (payloadlen != 0)
2888 payload = kmem_alloc(payloadlen, KM_SLEEP);
2890 err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
2892 if (payloadlen != 0)
2893 kmem_free(payload, payloadlen);
2896 if (payloadlen != 0) {
2897 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
2898 kmem_free(payload, payloadlen);
2903 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
2904 err = resume_check(&ra, begin_nvl);
2909 (void) bqueue_init(&rwa.q, zfs_recv_queue_length,
2910 offsetof(struct receive_record_arg, node));
2911 cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
2912 mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
2914 rwa.byteswap = drc->drc_byteswap;
2915 rwa.resumable = drc->drc_resumable;
2917 (void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, &p0,
2918 TS_RUN, minclsyspri);
2920 * We're reading rwa.err without locks, which is safe since we are the
2921 * only reader, and the worker thread is the only writer. It's ok if we
2922 * miss a write for an iteration or two of the loop, since the writer
2923 * thread will keep freeing records we send it until we send it an eos
2926 * We can leave this loop in 3 ways: First, if rwa.err is
2927 * non-zero. In that case, the writer thread will free the rrd we just
2928 * pushed. Second, if we're interrupted; in that case, either it's the
2929 * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
2930 * has been handed off to the writer thread who will free it. Finally,
2931 * if receive_read_record fails or we're at the end of the stream, then
2932 * we free ra.rrd and exit.
2934 while (rwa.err == 0) {
2935 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2936 err = SET_ERROR(EINTR);
2940 ASSERT3P(ra.rrd, ==, NULL);
2941 ra.rrd = ra.next_rrd;
2943 /* Allocates and loads header into ra.next_rrd */
2944 err = receive_read_record(&ra);
2946 if (ra.rrd->header.drr_type == DRR_END || err != 0) {
2947 kmem_free(ra.rrd, sizeof (*ra.rrd));
2952 bqueue_enqueue(&rwa.q, ra.rrd,
2953 sizeof (struct receive_record_arg) + ra.rrd->payload_size);
2956 if (ra.next_rrd == NULL)
2957 ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
2958 ra.next_rrd->eos_marker = B_TRUE;
2959 bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
2961 mutex_enter(&rwa.mutex);
2963 cv_wait(&rwa.cv, &rwa.mutex);
2965 mutex_exit(&rwa.mutex);
2967 cv_destroy(&rwa.cv);
2968 mutex_destroy(&rwa.mutex);
2969 bqueue_destroy(&rwa.q);
2974 nvlist_free(begin_nvl);
2975 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2976 zfs_onexit_fd_rele(cleanup_fd);
2980 * Clean up references. If receive is not resumable,
2981 * destroy what we created, so we don't leave it in
2982 * the inconsistent state.
2984 dmu_recv_cleanup_ds(drc);
2988 objlist_destroy(&ra.ignore_objlist);
2993 dmu_recv_end_check(void *arg, dmu_tx_t *tx)
2995 dmu_recv_cookie_t *drc = arg;
2996 dsl_pool_t *dp = dmu_tx_pool(tx);
2999 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
3001 if (!drc->drc_newfs) {
3002 dsl_dataset_t *origin_head;
3004 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
3007 if (drc->drc_force) {
3009 * We will destroy any snapshots in tofs (i.e. before
3010 * origin_head) that are after the origin (which is
3011 * the snap before drc_ds, because drc_ds can not
3012 * have any snaps of its own).
3016 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3018 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3019 dsl_dataset_t *snap;
3020 error = dsl_dataset_hold_obj(dp, obj, FTAG,
3024 if (snap->ds_dir != origin_head->ds_dir)
3025 error = SET_ERROR(EINVAL);
3027 error = dsl_destroy_snapshot_check_impl(
3030 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3031 dsl_dataset_rele(snap, FTAG);
3036 dsl_dataset_rele(origin_head, FTAG);
3040 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
3041 origin_head, drc->drc_force, drc->drc_owner, tx);
3043 dsl_dataset_rele(origin_head, FTAG);
3046 error = dsl_dataset_snapshot_check_impl(origin_head,
3047 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3048 dsl_dataset_rele(origin_head, FTAG);
3052 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
3054 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
3055 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3061 dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
3063 dmu_recv_cookie_t *drc = arg;
3064 dsl_pool_t *dp = dmu_tx_pool(tx);
3066 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
3067 tx, "snap=%s", drc->drc_tosnap);
3069 if (!drc->drc_newfs) {
3070 dsl_dataset_t *origin_head;
3072 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3075 if (drc->drc_force) {
3077 * Destroy any snapshots of drc_tofs (origin_head)
3078 * after the origin (the snap before drc_ds).
3082 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3084 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3085 dsl_dataset_t *snap;
3086 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
3088 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3089 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3090 dsl_destroy_snapshot_sync_impl(snap,
3092 dsl_dataset_rele(snap, FTAG);
3095 VERIFY3P(drc->drc_ds->ds_prev, ==,
3096 origin_head->ds_prev);
3098 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3100 dsl_dataset_snapshot_sync_impl(origin_head,
3101 drc->drc_tosnap, tx);
3103 /* set snapshot's creation time and guid */
3104 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3105 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
3106 drc->drc_drrb->drr_creation_time;
3107 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
3108 drc->drc_drrb->drr_toguid;
3109 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
3110 ~DS_FLAG_INCONSISTENT;
3112 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3113 dsl_dataset_phys(origin_head)->ds_flags &=
3114 ~DS_FLAG_INCONSISTENT;
3116 drc->drc_newsnapobj =
3117 dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3119 dsl_dataset_rele(origin_head, FTAG);
3120 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
3122 if (drc->drc_owner != NULL)
3123 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3125 dsl_dataset_t *ds = drc->drc_ds;
3127 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
3129 /* set snapshot's creation time and guid */
3130 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3131 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
3132 drc->drc_drrb->drr_creation_time;
3133 dsl_dataset_phys(ds->ds_prev)->ds_guid =
3134 drc->drc_drrb->drr_toguid;
3135 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3136 ~DS_FLAG_INCONSISTENT;
3138 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3139 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
3140 if (dsl_dataset_has_resume_receive_state(ds)) {
3141 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3142 DS_FIELD_RESUME_FROMGUID, tx);
3143 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3144 DS_FIELD_RESUME_OBJECT, tx);
3145 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3146 DS_FIELD_RESUME_OFFSET, tx);
3147 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3148 DS_FIELD_RESUME_BYTES, tx);
3149 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3150 DS_FIELD_RESUME_TOGUID, tx);
3151 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3152 DS_FIELD_RESUME_TONAME, tx);
3154 drc->drc_newsnapobj =
3155 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
3158 * Release the hold from dmu_recv_begin. This must be done before
3159 * we return to open context, so that when we free the dataset's dnode,
3160 * we can evict its bonus buffer.
3162 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
3167 add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3170 dsl_dataset_t *snapds;
3171 guid_map_entry_t *gmep;
3174 ASSERT(guid_map != NULL);
3176 err = dsl_pool_hold(name, FTAG, &dp);
3179 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3180 err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3182 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3183 gmep->gme_ds = snapds;
3184 avl_add(guid_map, gmep);
3185 dsl_dataset_long_hold(snapds, gmep);
3187 kmem_free(gmep, sizeof (*gmep));
3189 dsl_pool_rele(dp, FTAG);
3193 static int dmu_recv_end_modified_blocks = 3;
3196 dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3200 * We will be destroying the ds; make sure its origin is unmounted if
3203 char name[ZFS_MAX_DATASET_NAME_LEN];
3204 dsl_dataset_name(drc->drc_ds, name);
3205 zfs_destroy_unmount_origin(name);
3208 return (dsl_sync_task(drc->drc_tofs,
3209 dmu_recv_end_check, dmu_recv_end_sync, drc,
3210 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3214 dmu_recv_new_end(dmu_recv_cookie_t *drc)
3216 return (dsl_sync_task(drc->drc_tofs,
3217 dmu_recv_end_check, dmu_recv_end_sync, drc,
3218 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL));
3222 dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3226 drc->drc_owner = owner;
3229 error = dmu_recv_new_end(drc);
3231 error = dmu_recv_existing_end(drc);
3234 dmu_recv_cleanup_ds(drc);
3235 } else if (drc->drc_guid_to_ds_map != NULL) {
3236 (void) add_ds_to_guidmap(drc->drc_tofs,
3237 drc->drc_guid_to_ds_map,
3238 drc->drc_newsnapobj);
3244 * Return TRUE if this objset is currently being received into.
3247 dmu_objset_is_receiving(objset_t *os)
3249 return (os->os_dsl_dataset != NULL &&
3250 os->os_dsl_dataset->ds_owner == dmu_recv_tag);