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.
31 #include <sys/dmu_impl.h>
32 #include <sys/dmu_tx.h>
34 #include <sys/dnode.h>
35 #include <sys/zfs_context.h>
36 #include <sys/dmu_objset.h>
37 #include <sys/dmu_traverse.h>
38 #include <sys/dsl_dataset.h>
39 #include <sys/dsl_dir.h>
40 #include <sys/dsl_prop.h>
41 #include <sys/dsl_pool.h>
42 #include <sys/dsl_synctask.h>
43 #include <sys/zfs_ioctl.h>
45 #include <sys/zio_checksum.h>
46 #include <sys/zfs_znode.h>
47 #include <zfs_fletcher.h>
50 #include <sys/zfs_onexit.h>
51 #include <sys/dmu_send.h>
52 #include <sys/dsl_destroy.h>
53 #include <sys/blkptr.h>
54 #include <sys/dsl_bookmark.h>
55 #include <sys/zfeature.h>
56 #include <sys/bqueue.h>
60 #define dump_write dmu_dump_write
63 /* Set this tunable to TRUE to replace corrupt data with 0x2f5baddb10c */
64 int zfs_send_corrupt_data = B_FALSE;
65 int zfs_send_queue_length = 16 * 1024 * 1024;
66 int zfs_recv_queue_length = 16 * 1024 * 1024;
68 static char *dmu_recv_tag = "dmu_recv_tag";
69 const char *recv_clone_name = "%recv";
71 #define BP_SPAN(datablkszsec, indblkshift, level) \
72 (((uint64_t)datablkszsec) << (SPA_MINBLOCKSHIFT + \
73 (level) * (indblkshift - SPA_BLKPTRSHIFT)))
75 static void byteswap_record(dmu_replay_record_t *drr);
77 struct send_thread_arg {
79 dsl_dataset_t *ds; /* Dataset to traverse */
80 uint64_t fromtxg; /* Traverse from this txg */
81 int flags; /* flags to pass to traverse_dataset */
84 zbookmark_phys_t resume;
87 struct send_block_record {
88 boolean_t eos_marker; /* Marks the end of the stream */
92 uint16_t datablkszsec;
97 dump_bytes(dmu_sendarg_t *dsp, void *buf, int len)
99 dsl_dataset_t *ds = dmu_objset_ds(dsp->dsa_os);
106 auio.uio_iov = &aiov;
108 auio.uio_resid = len;
109 auio.uio_segflg = UIO_SYSSPACE;
110 auio.uio_rw = UIO_WRITE;
111 auio.uio_offset = (off_t)-1;
112 auio.uio_td = dsp->dsa_td;
114 if (dsp->dsa_fp->f_type == DTYPE_VNODE)
116 dsp->dsa_err = fo_write(dsp->dsa_fp, &auio, dsp->dsa_td->td_ucred, 0,
119 fprintf(stderr, "%s: returning EOPNOTSUPP\n", __func__);
120 dsp->dsa_err = EOPNOTSUPP;
122 mutex_enter(&ds->ds_sendstream_lock);
123 *dsp->dsa_off += len;
124 mutex_exit(&ds->ds_sendstream_lock);
126 return (dsp->dsa_err);
130 * For all record types except BEGIN, fill in the checksum (overlaid in
131 * drr_u.drr_checksum.drr_checksum). The checksum verifies everything
132 * up to the start of the checksum itself.
135 dump_record(dmu_sendarg_t *dsp, void *payload, int payload_len)
137 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
138 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
139 fletcher_4_incremental_native(dsp->dsa_drr,
140 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
142 if (dsp->dsa_drr->drr_type != DRR_BEGIN) {
143 ASSERT(ZIO_CHECKSUM_IS_ZERO(&dsp->dsa_drr->drr_u.
144 drr_checksum.drr_checksum));
145 dsp->dsa_drr->drr_u.drr_checksum.drr_checksum = dsp->dsa_zc;
147 fletcher_4_incremental_native(&dsp->dsa_drr->
148 drr_u.drr_checksum.drr_checksum,
149 sizeof (zio_cksum_t), &dsp->dsa_zc);
150 if (dump_bytes(dsp, dsp->dsa_drr, sizeof (dmu_replay_record_t)) != 0)
151 return (SET_ERROR(EINTR));
152 if (payload_len != 0) {
153 fletcher_4_incremental_native(payload, payload_len,
155 if (dump_bytes(dsp, payload, payload_len) != 0)
156 return (SET_ERROR(EINTR));
162 * Fill in the drr_free struct, or perform aggregation if the previous record is
163 * also a free record, and the two are adjacent.
165 * Note that we send free records even for a full send, because we want to be
166 * able to receive a full send as a clone, which requires a list of all the free
167 * and freeobject records that were generated on the source.
170 dump_free(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
173 struct drr_free *drrf = &(dsp->dsa_drr->drr_u.drr_free);
176 * When we receive a free record, dbuf_free_range() assumes
177 * that the receiving system doesn't have any dbufs in the range
178 * being freed. This is always true because there is a one-record
179 * constraint: we only send one WRITE record for any given
180 * object,offset. We know that the one-record constraint is
181 * true because we always send data in increasing order by
184 * If the increasing-order constraint ever changes, we should find
185 * another way to assert that the one-record constraint is still
188 ASSERT(object > dsp->dsa_last_data_object ||
189 (object == dsp->dsa_last_data_object &&
190 offset > dsp->dsa_last_data_offset));
192 if (length != -1ULL && offset + length < offset)
196 * If there is a pending op, but it's not PENDING_FREE, push it out,
197 * since free block aggregation can only be done for blocks of the
198 * same type (i.e., DRR_FREE records can only be aggregated with
199 * other DRR_FREE records. DRR_FREEOBJECTS records can only be
200 * aggregated with other DRR_FREEOBJECTS records.
202 if (dsp->dsa_pending_op != PENDING_NONE &&
203 dsp->dsa_pending_op != PENDING_FREE) {
204 if (dump_record(dsp, NULL, 0) != 0)
205 return (SET_ERROR(EINTR));
206 dsp->dsa_pending_op = PENDING_NONE;
209 if (dsp->dsa_pending_op == PENDING_FREE) {
211 * There should never be a PENDING_FREE if length is -1
212 * (because dump_dnode is the only place where this
213 * function is called with a -1, and only after flushing
214 * any pending record).
216 ASSERT(length != -1ULL);
218 * Check to see whether this free block can be aggregated
221 if (drrf->drr_object == object && drrf->drr_offset +
222 drrf->drr_length == offset) {
223 drrf->drr_length += length;
226 /* not a continuation. Push out pending record */
227 if (dump_record(dsp, NULL, 0) != 0)
228 return (SET_ERROR(EINTR));
229 dsp->dsa_pending_op = PENDING_NONE;
232 /* create a FREE record and make it pending */
233 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
234 dsp->dsa_drr->drr_type = DRR_FREE;
235 drrf->drr_object = object;
236 drrf->drr_offset = offset;
237 drrf->drr_length = length;
238 drrf->drr_toguid = dsp->dsa_toguid;
239 if (length == -1ULL) {
240 if (dump_record(dsp, NULL, 0) != 0)
241 return (SET_ERROR(EINTR));
243 dsp->dsa_pending_op = PENDING_FREE;
250 dump_write(dmu_sendarg_t *dsp, dmu_object_type_t type,
251 uint64_t object, uint64_t offset, int blksz, const blkptr_t *bp, void *data)
253 struct drr_write *drrw = &(dsp->dsa_drr->drr_u.drr_write);
256 * We send data in increasing object, offset order.
257 * See comment in dump_free() for details.
259 ASSERT(object > dsp->dsa_last_data_object ||
260 (object == dsp->dsa_last_data_object &&
261 offset > dsp->dsa_last_data_offset));
262 dsp->dsa_last_data_object = object;
263 dsp->dsa_last_data_offset = offset + blksz - 1;
266 * If there is any kind of pending aggregation (currently either
267 * a grouping of free objects or free blocks), push it out to
268 * the stream, since aggregation can't be done across operations
269 * of different types.
271 if (dsp->dsa_pending_op != PENDING_NONE) {
272 if (dump_record(dsp, NULL, 0) != 0)
273 return (SET_ERROR(EINTR));
274 dsp->dsa_pending_op = PENDING_NONE;
276 /* write a WRITE record */
277 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
278 dsp->dsa_drr->drr_type = DRR_WRITE;
279 drrw->drr_object = object;
280 drrw->drr_type = type;
281 drrw->drr_offset = offset;
282 drrw->drr_length = blksz;
283 drrw->drr_toguid = dsp->dsa_toguid;
284 if (bp == NULL || BP_IS_EMBEDDED(bp)) {
286 * There's no pre-computed checksum for partial-block
287 * writes or embedded BP's, so (like
288 * fletcher4-checkummed blocks) userland will have to
289 * compute a dedup-capable checksum itself.
291 drrw->drr_checksumtype = ZIO_CHECKSUM_OFF;
293 drrw->drr_checksumtype = BP_GET_CHECKSUM(bp);
294 if (zio_checksum_table[drrw->drr_checksumtype].ci_flags &
295 ZCHECKSUM_FLAG_DEDUP)
296 drrw->drr_checksumflags |= DRR_CHECKSUM_DEDUP;
297 DDK_SET_LSIZE(&drrw->drr_key, BP_GET_LSIZE(bp));
298 DDK_SET_PSIZE(&drrw->drr_key, BP_GET_PSIZE(bp));
299 DDK_SET_COMPRESS(&drrw->drr_key, BP_GET_COMPRESS(bp));
300 drrw->drr_key.ddk_cksum = bp->blk_cksum;
303 if (dump_record(dsp, data, blksz) != 0)
304 return (SET_ERROR(EINTR));
309 dump_write_embedded(dmu_sendarg_t *dsp, uint64_t object, uint64_t offset,
310 int blksz, const blkptr_t *bp)
312 char buf[BPE_PAYLOAD_SIZE];
313 struct drr_write_embedded *drrw =
314 &(dsp->dsa_drr->drr_u.drr_write_embedded);
316 if (dsp->dsa_pending_op != PENDING_NONE) {
317 if (dump_record(dsp, NULL, 0) != 0)
319 dsp->dsa_pending_op = PENDING_NONE;
322 ASSERT(BP_IS_EMBEDDED(bp));
324 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
325 dsp->dsa_drr->drr_type = DRR_WRITE_EMBEDDED;
326 drrw->drr_object = object;
327 drrw->drr_offset = offset;
328 drrw->drr_length = blksz;
329 drrw->drr_toguid = dsp->dsa_toguid;
330 drrw->drr_compression = BP_GET_COMPRESS(bp);
331 drrw->drr_etype = BPE_GET_ETYPE(bp);
332 drrw->drr_lsize = BPE_GET_LSIZE(bp);
333 drrw->drr_psize = BPE_GET_PSIZE(bp);
335 decode_embedded_bp_compressed(bp, buf);
337 if (dump_record(dsp, buf, P2ROUNDUP(drrw->drr_psize, 8)) != 0)
343 dump_spill(dmu_sendarg_t *dsp, uint64_t object, int blksz, void *data)
345 struct drr_spill *drrs = &(dsp->dsa_drr->drr_u.drr_spill);
347 if (dsp->dsa_pending_op != PENDING_NONE) {
348 if (dump_record(dsp, NULL, 0) != 0)
349 return (SET_ERROR(EINTR));
350 dsp->dsa_pending_op = PENDING_NONE;
353 /* write a SPILL record */
354 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
355 dsp->dsa_drr->drr_type = DRR_SPILL;
356 drrs->drr_object = object;
357 drrs->drr_length = blksz;
358 drrs->drr_toguid = dsp->dsa_toguid;
360 if (dump_record(dsp, data, blksz) != 0)
361 return (SET_ERROR(EINTR));
366 dump_freeobjects(dmu_sendarg_t *dsp, uint64_t firstobj, uint64_t numobjs)
368 struct drr_freeobjects *drrfo = &(dsp->dsa_drr->drr_u.drr_freeobjects);
371 * If there is a pending op, but it's not PENDING_FREEOBJECTS,
372 * push it out, since free block aggregation can only be done for
373 * blocks of the same type (i.e., DRR_FREE records can only be
374 * aggregated with other DRR_FREE records. DRR_FREEOBJECTS records
375 * can only be aggregated with other DRR_FREEOBJECTS records.
377 if (dsp->dsa_pending_op != PENDING_NONE &&
378 dsp->dsa_pending_op != PENDING_FREEOBJECTS) {
379 if (dump_record(dsp, NULL, 0) != 0)
380 return (SET_ERROR(EINTR));
381 dsp->dsa_pending_op = PENDING_NONE;
383 if (dsp->dsa_pending_op == PENDING_FREEOBJECTS) {
385 * See whether this free object array can be aggregated
388 if (drrfo->drr_firstobj + drrfo->drr_numobjs == firstobj) {
389 drrfo->drr_numobjs += numobjs;
392 /* can't be aggregated. Push out pending record */
393 if (dump_record(dsp, NULL, 0) != 0)
394 return (SET_ERROR(EINTR));
395 dsp->dsa_pending_op = PENDING_NONE;
399 /* write a FREEOBJECTS record */
400 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
401 dsp->dsa_drr->drr_type = DRR_FREEOBJECTS;
402 drrfo->drr_firstobj = firstobj;
403 drrfo->drr_numobjs = numobjs;
404 drrfo->drr_toguid = dsp->dsa_toguid;
406 dsp->dsa_pending_op = PENDING_FREEOBJECTS;
412 dump_dnode(dmu_sendarg_t *dsp, uint64_t object, dnode_phys_t *dnp)
414 struct drr_object *drro = &(dsp->dsa_drr->drr_u.drr_object);
416 if (object < dsp->dsa_resume_object) {
418 * Note: when resuming, we will visit all the dnodes in
419 * the block of dnodes that we are resuming from. In
420 * this case it's unnecessary to send the dnodes prior to
421 * the one we are resuming from. We should be at most one
422 * block's worth of dnodes behind the resume point.
424 ASSERT3U(dsp->dsa_resume_object - object, <,
425 1 << (DNODE_BLOCK_SHIFT - DNODE_SHIFT));
429 if (dnp == NULL || dnp->dn_type == DMU_OT_NONE)
430 return (dump_freeobjects(dsp, object, 1));
432 if (dsp->dsa_pending_op != PENDING_NONE) {
433 if (dump_record(dsp, NULL, 0) != 0)
434 return (SET_ERROR(EINTR));
435 dsp->dsa_pending_op = PENDING_NONE;
438 /* write an OBJECT record */
439 bzero(dsp->dsa_drr, sizeof (dmu_replay_record_t));
440 dsp->dsa_drr->drr_type = DRR_OBJECT;
441 drro->drr_object = object;
442 drro->drr_type = dnp->dn_type;
443 drro->drr_bonustype = dnp->dn_bonustype;
444 drro->drr_blksz = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
445 drro->drr_bonuslen = dnp->dn_bonuslen;
446 drro->drr_checksumtype = dnp->dn_checksum;
447 drro->drr_compress = dnp->dn_compress;
448 drro->drr_toguid = dsp->dsa_toguid;
450 if (!(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
451 drro->drr_blksz > SPA_OLD_MAXBLOCKSIZE)
452 drro->drr_blksz = SPA_OLD_MAXBLOCKSIZE;
454 if (dump_record(dsp, DN_BONUS(dnp),
455 P2ROUNDUP(dnp->dn_bonuslen, 8)) != 0) {
456 return (SET_ERROR(EINTR));
459 /* Free anything past the end of the file. */
460 if (dump_free(dsp, object, (dnp->dn_maxblkid + 1) *
461 (dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT), -1ULL) != 0)
462 return (SET_ERROR(EINTR));
463 if (dsp->dsa_err != 0)
464 return (SET_ERROR(EINTR));
469 backup_do_embed(dmu_sendarg_t *dsp, const blkptr_t *bp)
471 if (!BP_IS_EMBEDDED(bp))
475 * Compression function must be legacy, or explicitly enabled.
477 if ((BP_GET_COMPRESS(bp) >= ZIO_COMPRESS_LEGACY_FUNCTIONS &&
478 !(dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4)))
482 * Embed type must be explicitly enabled.
484 switch (BPE_GET_ETYPE(bp)) {
485 case BP_EMBEDDED_TYPE_DATA:
486 if (dsp->dsa_featureflags & DMU_BACKUP_FEATURE_EMBED_DATA)
496 * This is the callback function to traverse_dataset that acts as the worker
497 * thread for dmu_send_impl.
501 send_cb(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
502 const zbookmark_phys_t *zb, const struct dnode_phys *dnp, void *arg)
504 struct send_thread_arg *sta = arg;
505 struct send_block_record *record;
506 uint64_t record_size;
509 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
510 zb->zb_object >= sta->resume.zb_object);
513 return (SET_ERROR(EINTR));
516 ASSERT3U(zb->zb_level, ==, ZB_DNODE_LEVEL);
518 } else if (zb->zb_level < 0) {
522 record = kmem_zalloc(sizeof (struct send_block_record), KM_SLEEP);
523 record->eos_marker = B_FALSE;
526 record->indblkshift = dnp->dn_indblkshift;
527 record->datablkszsec = dnp->dn_datablkszsec;
528 record_size = dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT;
529 bqueue_enqueue(&sta->q, record, record_size);
535 * This function kicks off the traverse_dataset. It also handles setting the
536 * error code of the thread in case something goes wrong, and pushes the End of
537 * Stream record when the traverse_dataset call has finished. If there is no
538 * dataset to traverse, the thread immediately pushes End of Stream marker.
541 send_traverse_thread(void *arg)
543 struct send_thread_arg *st_arg = arg;
545 struct send_block_record *data;
547 if (st_arg->ds != NULL) {
548 err = traverse_dataset_resume(st_arg->ds,
549 st_arg->fromtxg, &st_arg->resume,
550 st_arg->flags, send_cb, st_arg);
553 st_arg->error_code = err;
555 data = kmem_zalloc(sizeof (*data), KM_SLEEP);
556 data->eos_marker = B_TRUE;
557 bqueue_enqueue(&st_arg->q, data, 1);
562 * This function actually handles figuring out what kind of record needs to be
563 * dumped, reading the data (which has hopefully been prefetched), and calling
564 * the appropriate helper function.
567 do_dump(dmu_sendarg_t *dsa, struct send_block_record *data)
569 dsl_dataset_t *ds = dmu_objset_ds(dsa->dsa_os);
570 const blkptr_t *bp = &data->bp;
571 const zbookmark_phys_t *zb = &data->zb;
572 uint8_t indblkshift = data->indblkshift;
573 uint16_t dblkszsec = data->datablkszsec;
574 spa_t *spa = ds->ds_dir->dd_pool->dp_spa;
575 dmu_object_type_t type = bp ? BP_GET_TYPE(bp) : DMU_OT_NONE;
578 ASSERT3U(zb->zb_level, >=, 0);
580 ASSERT(zb->zb_object == DMU_META_DNODE_OBJECT ||
581 zb->zb_object >= dsa->dsa_resume_object);
583 if (zb->zb_object != DMU_META_DNODE_OBJECT &&
584 DMU_OBJECT_IS_SPECIAL(zb->zb_object)) {
586 } else if (BP_IS_HOLE(bp) &&
587 zb->zb_object == DMU_META_DNODE_OBJECT) {
588 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
589 uint64_t dnobj = (zb->zb_blkid * span) >> DNODE_SHIFT;
590 err = dump_freeobjects(dsa, dnobj, span >> DNODE_SHIFT);
591 } else if (BP_IS_HOLE(bp)) {
592 uint64_t span = BP_SPAN(dblkszsec, indblkshift, zb->zb_level);
593 uint64_t offset = zb->zb_blkid * span;
594 err = dump_free(dsa, zb->zb_object, offset, span);
595 } else if (zb->zb_level > 0 || type == DMU_OT_OBJSET) {
597 } else if (type == DMU_OT_DNODE) {
598 int blksz = BP_GET_LSIZE(bp);
599 arc_flags_t aflags = ARC_FLAG_WAIT;
602 ASSERT0(zb->zb_level);
604 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
605 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
607 return (SET_ERROR(EIO));
609 dnode_phys_t *blk = abuf->b_data;
610 uint64_t dnobj = zb->zb_blkid * (blksz >> DNODE_SHIFT);
611 for (int i = 0; i < blksz >> DNODE_SHIFT; i++) {
612 err = dump_dnode(dsa, dnobj + i, blk + i);
616 (void) arc_buf_remove_ref(abuf, &abuf);
617 } else if (type == DMU_OT_SA) {
618 arc_flags_t aflags = ARC_FLAG_WAIT;
620 int blksz = BP_GET_LSIZE(bp);
622 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
623 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
625 return (SET_ERROR(EIO));
627 err = dump_spill(dsa, zb->zb_object, blksz, abuf->b_data);
628 (void) arc_buf_remove_ref(abuf, &abuf);
629 } else if (backup_do_embed(dsa, bp)) {
630 /* it's an embedded level-0 block of a regular object */
631 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
632 ASSERT0(zb->zb_level);
633 err = dump_write_embedded(dsa, zb->zb_object,
634 zb->zb_blkid * blksz, blksz, bp);
636 /* it's a level-0 block of a regular object */
637 arc_flags_t aflags = ARC_FLAG_WAIT;
639 int blksz = dblkszsec << SPA_MINBLOCKSHIFT;
642 ASSERT0(zb->zb_level);
643 ASSERT(zb->zb_object > dsa->dsa_resume_object ||
644 (zb->zb_object == dsa->dsa_resume_object &&
645 zb->zb_blkid * blksz >= dsa->dsa_resume_offset));
647 if (arc_read(NULL, spa, bp, arc_getbuf_func, &abuf,
648 ZIO_PRIORITY_ASYNC_READ, ZIO_FLAG_CANFAIL,
650 if (zfs_send_corrupt_data) {
651 /* Send a block filled with 0x"zfs badd bloc" */
652 abuf = arc_buf_alloc(spa, blksz, &abuf,
655 for (ptr = abuf->b_data;
656 (char *)ptr < (char *)abuf->b_data + blksz;
658 *ptr = 0x2f5baddb10cULL;
660 return (SET_ERROR(EIO));
664 offset = zb->zb_blkid * blksz;
666 if (!(dsa->dsa_featureflags &
667 DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
668 blksz > SPA_OLD_MAXBLOCKSIZE) {
669 char *buf = abuf->b_data;
670 while (blksz > 0 && err == 0) {
671 int n = MIN(blksz, SPA_OLD_MAXBLOCKSIZE);
672 err = dump_write(dsa, type, zb->zb_object,
673 offset, n, NULL, buf);
679 err = dump_write(dsa, type, zb->zb_object,
680 offset, blksz, bp, abuf->b_data);
682 (void) arc_buf_remove_ref(abuf, &abuf);
685 ASSERT(err == 0 || err == EINTR);
690 * Pop the new data off the queue, and free the old data.
692 static struct send_block_record *
693 get_next_record(bqueue_t *bq, struct send_block_record *data)
695 struct send_block_record *tmp = bqueue_dequeue(bq);
696 kmem_free(data, sizeof (*data));
701 * Actually do the bulk of the work in a zfs send.
703 * Note: Releases dp using the specified tag.
706 dmu_send_impl(void *tag, dsl_pool_t *dp, dsl_dataset_t *to_ds,
707 zfs_bookmark_phys_t *ancestor_zb,
708 boolean_t is_clone, boolean_t embedok, boolean_t large_block_ok, int outfd,
709 uint64_t resumeobj, uint64_t resumeoff,
711 vnode_t *vp, offset_t *off)
713 struct file *fp, offset_t *off)
717 dmu_replay_record_t *drr;
720 uint64_t fromtxg = 0;
721 uint64_t featureflags = 0;
722 struct send_thread_arg to_arg = { 0 };
724 err = dmu_objset_from_ds(to_ds, &os);
726 dsl_pool_rele(dp, tag);
730 drr = kmem_zalloc(sizeof (dmu_replay_record_t), KM_SLEEP);
731 drr->drr_type = DRR_BEGIN;
732 drr->drr_u.drr_begin.drr_magic = DMU_BACKUP_MAGIC;
733 DMU_SET_STREAM_HDRTYPE(drr->drr_u.drr_begin.drr_versioninfo,
737 if (dmu_objset_type(os) == DMU_OST_ZFS) {
739 if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &version) != 0) {
740 kmem_free(drr, sizeof (dmu_replay_record_t));
741 dsl_pool_rele(dp, tag);
742 return (SET_ERROR(EINVAL));
744 if (version >= ZPL_VERSION_SA) {
745 featureflags |= DMU_BACKUP_FEATURE_SA_SPILL;
750 if (large_block_ok && to_ds->ds_feature_inuse[SPA_FEATURE_LARGE_BLOCKS])
751 featureflags |= DMU_BACKUP_FEATURE_LARGE_BLOCKS;
753 spa_feature_is_active(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA)) {
754 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA;
755 if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
756 featureflags |= DMU_BACKUP_FEATURE_EMBED_DATA_LZ4;
759 if (resumeobj != 0 || resumeoff != 0) {
760 featureflags |= DMU_BACKUP_FEATURE_RESUMING;
763 DMU_SET_FEATUREFLAGS(drr->drr_u.drr_begin.drr_versioninfo,
766 drr->drr_u.drr_begin.drr_creation_time =
767 dsl_dataset_phys(to_ds)->ds_creation_time;
768 drr->drr_u.drr_begin.drr_type = dmu_objset_type(os);
770 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CLONE;
771 drr->drr_u.drr_begin.drr_toguid = dsl_dataset_phys(to_ds)->ds_guid;
772 if (dsl_dataset_phys(to_ds)->ds_flags & DS_FLAG_CI_DATASET)
773 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_CI_DATA;
774 drr->drr_u.drr_begin.drr_flags |= DRR_FLAG_FREERECORDS;
776 if (ancestor_zb != NULL) {
777 drr->drr_u.drr_begin.drr_fromguid =
778 ancestor_zb->zbm_guid;
779 fromtxg = ancestor_zb->zbm_creation_txg;
781 dsl_dataset_name(to_ds, drr->drr_u.drr_begin.drr_toname);
782 if (!to_ds->ds_is_snapshot) {
783 (void) strlcat(drr->drr_u.drr_begin.drr_toname, "@--head--",
784 sizeof (drr->drr_u.drr_begin.drr_toname));
787 dsp = kmem_zalloc(sizeof (dmu_sendarg_t), KM_SLEEP);
790 dsp->dsa_outfd = outfd;
791 dsp->dsa_proc = curproc;
792 dsp->dsa_td = curthread;
796 dsp->dsa_toguid = dsl_dataset_phys(to_ds)->ds_guid;
797 dsp->dsa_pending_op = PENDING_NONE;
798 dsp->dsa_featureflags = featureflags;
799 dsp->dsa_resume_object = resumeobj;
800 dsp->dsa_resume_offset = resumeoff;
802 mutex_enter(&to_ds->ds_sendstream_lock);
803 list_insert_head(&to_ds->ds_sendstreams, dsp);
804 mutex_exit(&to_ds->ds_sendstream_lock);
806 dsl_dataset_long_hold(to_ds, FTAG);
807 dsl_pool_rele(dp, tag);
809 void *payload = NULL;
810 size_t payload_len = 0;
811 if (resumeobj != 0 || resumeoff != 0) {
812 dmu_object_info_t to_doi;
813 err = dmu_object_info(os, resumeobj, &to_doi);
816 SET_BOOKMARK(&to_arg.resume, to_ds->ds_object, resumeobj, 0,
817 resumeoff / to_doi.doi_data_block_size);
819 nvlist_t *nvl = fnvlist_alloc();
820 fnvlist_add_uint64(nvl, "resume_object", resumeobj);
821 fnvlist_add_uint64(nvl, "resume_offset", resumeoff);
822 payload = fnvlist_pack(nvl, &payload_len);
823 drr->drr_payloadlen = payload_len;
827 err = dump_record(dsp, payload, payload_len);
828 fnvlist_pack_free(payload, payload_len);
834 err = bqueue_init(&to_arg.q, zfs_send_queue_length,
835 offsetof(struct send_block_record, ln));
836 to_arg.error_code = 0;
837 to_arg.cancel = B_FALSE;
839 to_arg.fromtxg = fromtxg;
840 to_arg.flags = TRAVERSE_PRE | TRAVERSE_PREFETCH;
841 (void) thread_create(NULL, 0, send_traverse_thread, &to_arg, 0, &p0,
842 TS_RUN, minclsyspri);
844 struct send_block_record *to_data;
845 to_data = bqueue_dequeue(&to_arg.q);
847 while (!to_data->eos_marker && err == 0) {
848 err = do_dump(dsp, to_data);
849 to_data = get_next_record(&to_arg.q, to_data);
850 if (issig(JUSTLOOKING) && issig(FORREAL))
855 to_arg.cancel = B_TRUE;
856 while (!to_data->eos_marker) {
857 to_data = get_next_record(&to_arg.q, to_data);
860 kmem_free(to_data, sizeof (*to_data));
862 bqueue_destroy(&to_arg.q);
864 if (err == 0 && to_arg.error_code != 0)
865 err = to_arg.error_code;
870 if (dsp->dsa_pending_op != PENDING_NONE)
871 if (dump_record(dsp, NULL, 0) != 0)
872 err = SET_ERROR(EINTR);
875 if (err == EINTR && dsp->dsa_err != 0)
880 bzero(drr, sizeof (dmu_replay_record_t));
881 drr->drr_type = DRR_END;
882 drr->drr_u.drr_end.drr_checksum = dsp->dsa_zc;
883 drr->drr_u.drr_end.drr_toguid = dsp->dsa_toguid;
885 if (dump_record(dsp, NULL, 0) != 0)
889 mutex_enter(&to_ds->ds_sendstream_lock);
890 list_remove(&to_ds->ds_sendstreams, dsp);
891 mutex_exit(&to_ds->ds_sendstream_lock);
893 kmem_free(drr, sizeof (dmu_replay_record_t));
894 kmem_free(dsp, sizeof (dmu_sendarg_t));
896 dsl_dataset_long_rele(to_ds, FTAG);
902 dmu_send_obj(const char *pool, uint64_t tosnap, uint64_t fromsnap,
903 boolean_t embedok, boolean_t large_block_ok,
905 int outfd, vnode_t *vp, offset_t *off)
907 int outfd, struct file *fp, offset_t *off)
912 dsl_dataset_t *fromds = NULL;
915 err = dsl_pool_hold(pool, FTAG, &dp);
919 err = dsl_dataset_hold_obj(dp, tosnap, FTAG, &ds);
921 dsl_pool_rele(dp, FTAG);
926 zfs_bookmark_phys_t zb;
929 err = dsl_dataset_hold_obj(dp, fromsnap, FTAG, &fromds);
931 dsl_dataset_rele(ds, FTAG);
932 dsl_pool_rele(dp, FTAG);
935 if (!dsl_dataset_is_before(ds, fromds, 0))
936 err = SET_ERROR(EXDEV);
937 zb.zbm_creation_time =
938 dsl_dataset_phys(fromds)->ds_creation_time;
939 zb.zbm_creation_txg = dsl_dataset_phys(fromds)->ds_creation_txg;
940 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
941 is_clone = (fromds->ds_dir != ds->ds_dir);
942 dsl_dataset_rele(fromds, FTAG);
943 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
944 embedok, large_block_ok, outfd, 0, 0, fp, off);
946 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
947 embedok, large_block_ok, outfd, 0, 0, fp, off);
949 dsl_dataset_rele(ds, FTAG);
954 dmu_send(const char *tosnap, const char *fromsnap, boolean_t embedok,
955 boolean_t large_block_ok, int outfd, uint64_t resumeobj, uint64_t resumeoff,
957 vnode_t *vp, offset_t *off)
959 struct file *fp, offset_t *off)
965 boolean_t owned = B_FALSE;
967 if (fromsnap != NULL && strpbrk(fromsnap, "@#") == NULL)
968 return (SET_ERROR(EINVAL));
970 err = dsl_pool_hold(tosnap, FTAG, &dp);
974 if (strchr(tosnap, '@') == NULL && spa_writeable(dp->dp_spa)) {
976 * We are sending a filesystem or volume. Ensure
977 * that it doesn't change by owning the dataset.
979 err = dsl_dataset_own(dp, tosnap, FTAG, &ds);
982 err = dsl_dataset_hold(dp, tosnap, FTAG, &ds);
985 dsl_pool_rele(dp, FTAG);
989 if (fromsnap != NULL) {
990 zfs_bookmark_phys_t zb;
991 boolean_t is_clone = B_FALSE;
992 int fsnamelen = strchr(tosnap, '@') - tosnap;
995 * If the fromsnap is in a different filesystem, then
996 * mark the send stream as a clone.
998 if (strncmp(tosnap, fromsnap, fsnamelen) != 0 ||
999 (fromsnap[fsnamelen] != '@' &&
1000 fromsnap[fsnamelen] != '#')) {
1004 if (strchr(fromsnap, '@')) {
1005 dsl_dataset_t *fromds;
1006 err = dsl_dataset_hold(dp, fromsnap, FTAG, &fromds);
1008 if (!dsl_dataset_is_before(ds, fromds, 0))
1009 err = SET_ERROR(EXDEV);
1010 zb.zbm_creation_time =
1011 dsl_dataset_phys(fromds)->ds_creation_time;
1012 zb.zbm_creation_txg =
1013 dsl_dataset_phys(fromds)->ds_creation_txg;
1014 zb.zbm_guid = dsl_dataset_phys(fromds)->ds_guid;
1015 is_clone = (ds->ds_dir != fromds->ds_dir);
1016 dsl_dataset_rele(fromds, FTAG);
1019 err = dsl_bookmark_lookup(dp, fromsnap, ds, &zb);
1022 dsl_dataset_rele(ds, FTAG);
1023 dsl_pool_rele(dp, FTAG);
1026 err = dmu_send_impl(FTAG, dp, ds, &zb, is_clone,
1027 embedok, large_block_ok,
1028 outfd, resumeobj, resumeoff, fp, off);
1030 err = dmu_send_impl(FTAG, dp, ds, NULL, B_FALSE,
1031 embedok, large_block_ok,
1032 outfd, resumeobj, resumeoff, fp, off);
1035 dsl_dataset_disown(ds, FTAG);
1037 dsl_dataset_rele(ds, FTAG);
1042 dmu_adjust_send_estimate_for_indirects(dsl_dataset_t *ds, uint64_t size,
1047 * Assume that space (both on-disk and in-stream) is dominated by
1048 * data. We will adjust for indirect blocks and the copies property,
1049 * but ignore per-object space used (eg, dnodes and DRR_OBJECT records).
1053 * Subtract out approximate space used by indirect blocks.
1054 * Assume most space is used by data blocks (non-indirect, non-dnode).
1055 * Assume all blocks are recordsize. Assume ditto blocks and
1056 * internal fragmentation counter out compression.
1058 * Therefore, space used by indirect blocks is sizeof(blkptr_t) per
1059 * block, which we observe in practice.
1061 uint64_t recordsize;
1062 err = dsl_prop_get_int_ds(ds, "recordsize", &recordsize);
1065 size -= size / recordsize * sizeof (blkptr_t);
1067 /* Add in the space for the record associated with each block. */
1068 size += size / recordsize * sizeof (dmu_replay_record_t);
1076 dmu_send_estimate(dsl_dataset_t *ds, dsl_dataset_t *fromds, uint64_t *sizep)
1078 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1082 ASSERT(dsl_pool_config_held(dp));
1084 /* tosnap must be a snapshot */
1085 if (!ds->ds_is_snapshot)
1086 return (SET_ERROR(EINVAL));
1088 /* fromsnap, if provided, must be a snapshot */
1089 if (fromds != NULL && !fromds->ds_is_snapshot)
1090 return (SET_ERROR(EINVAL));
1093 * fromsnap must be an earlier snapshot from the same fs as tosnap,
1094 * or the origin's fs.
1096 if (fromds != NULL && !dsl_dataset_is_before(ds, fromds, 0))
1097 return (SET_ERROR(EXDEV));
1099 /* Get uncompressed size estimate of changed data. */
1100 if (fromds == NULL) {
1101 size = dsl_dataset_phys(ds)->ds_uncompressed_bytes;
1103 uint64_t used, comp;
1104 err = dsl_dataset_space_written(fromds, ds,
1105 &used, &comp, &size);
1110 err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1115 * Simple callback used to traverse the blocks of a snapshot and sum their
1120 dmu_calculate_send_traversal(spa_t *spa, zilog_t *zilog, const blkptr_t *bp,
1121 const zbookmark_phys_t *zb, const dnode_phys_t *dnp, void *arg)
1123 uint64_t *spaceptr = arg;
1124 if (bp != NULL && !BP_IS_HOLE(bp)) {
1125 *spaceptr += BP_GET_UCSIZE(bp);
1131 * Given a desination snapshot and a TXG, calculate the approximate size of a
1132 * send stream sent from that TXG. from_txg may be zero, indicating that the
1133 * whole snapshot will be sent.
1136 dmu_send_estimate_from_txg(dsl_dataset_t *ds, uint64_t from_txg,
1139 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1143 ASSERT(dsl_pool_config_held(dp));
1145 /* tosnap must be a snapshot */
1146 if (!dsl_dataset_is_snapshot(ds))
1147 return (SET_ERROR(EINVAL));
1149 /* verify that from_txg is before the provided snapshot was taken */
1150 if (from_txg >= dsl_dataset_phys(ds)->ds_creation_txg) {
1151 return (SET_ERROR(EXDEV));
1155 * traverse the blocks of the snapshot with birth times after
1156 * from_txg, summing their uncompressed size
1158 err = traverse_dataset(ds, from_txg, TRAVERSE_POST,
1159 dmu_calculate_send_traversal, &size);
1163 err = dmu_adjust_send_estimate_for_indirects(ds, size, sizep);
1167 typedef struct dmu_recv_begin_arg {
1168 const char *drba_origin;
1169 dmu_recv_cookie_t *drba_cookie;
1171 uint64_t drba_snapobj;
1172 } dmu_recv_begin_arg_t;
1175 recv_begin_check_existing_impl(dmu_recv_begin_arg_t *drba, dsl_dataset_t *ds,
1180 dsl_pool_t *dp = ds->ds_dir->dd_pool;
1182 /* temporary clone name must not exist */
1183 error = zap_lookup(dp->dp_meta_objset,
1184 dsl_dir_phys(ds->ds_dir)->dd_child_dir_zapobj, recv_clone_name,
1186 if (error != ENOENT)
1187 return (error == 0 ? EBUSY : error);
1189 /* new snapshot name must not exist */
1190 error = zap_lookup(dp->dp_meta_objset,
1191 dsl_dataset_phys(ds)->ds_snapnames_zapobj,
1192 drba->drba_cookie->drc_tosnap, 8, 1, &val);
1193 if (error != ENOENT)
1194 return (error == 0 ? EEXIST : error);
1197 * Check snapshot limit before receiving. We'll recheck again at the
1198 * end, but might as well abort before receiving if we're already over
1201 * Note that we do not check the file system limit with
1202 * dsl_dir_fscount_check because the temporary %clones don't count
1203 * against that limit.
1205 error = dsl_fs_ss_limit_check(ds->ds_dir, 1, ZFS_PROP_SNAPSHOT_LIMIT,
1206 NULL, drba->drba_cred);
1210 if (fromguid != 0) {
1211 dsl_dataset_t *snap;
1212 uint64_t obj = dsl_dataset_phys(ds)->ds_prev_snap_obj;
1214 /* Find snapshot in this dir that matches fromguid. */
1216 error = dsl_dataset_hold_obj(dp, obj, FTAG,
1219 return (SET_ERROR(ENODEV));
1220 if (snap->ds_dir != ds->ds_dir) {
1221 dsl_dataset_rele(snap, FTAG);
1222 return (SET_ERROR(ENODEV));
1224 if (dsl_dataset_phys(snap)->ds_guid == fromguid)
1226 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
1227 dsl_dataset_rele(snap, FTAG);
1230 return (SET_ERROR(ENODEV));
1232 if (drba->drba_cookie->drc_force) {
1233 drba->drba_snapobj = obj;
1236 * If we are not forcing, there must be no
1237 * changes since fromsnap.
1239 if (dsl_dataset_modified_since_snap(ds, snap)) {
1240 dsl_dataset_rele(snap, FTAG);
1241 return (SET_ERROR(ETXTBSY));
1243 drba->drba_snapobj = ds->ds_prev->ds_object;
1246 dsl_dataset_rele(snap, FTAG);
1248 /* if full, then must be forced */
1249 if (!drba->drba_cookie->drc_force)
1250 return (SET_ERROR(EEXIST));
1251 /* start from $ORIGIN@$ORIGIN, if supported */
1252 drba->drba_snapobj = dp->dp_origin_snap != NULL ?
1253 dp->dp_origin_snap->ds_object : 0;
1261 dmu_recv_begin_check(void *arg, dmu_tx_t *tx)
1263 dmu_recv_begin_arg_t *drba = arg;
1264 dsl_pool_t *dp = dmu_tx_pool(tx);
1265 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1266 uint64_t fromguid = drrb->drr_fromguid;
1267 int flags = drrb->drr_flags;
1269 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1271 const char *tofs = drba->drba_cookie->drc_tofs;
1273 /* already checked */
1274 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1275 ASSERT(!(featureflags & DMU_BACKUP_FEATURE_RESUMING));
1277 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1278 DMU_COMPOUNDSTREAM ||
1279 drrb->drr_type >= DMU_OST_NUMTYPES ||
1280 ((flags & DRR_FLAG_CLONE) && drba->drba_origin == NULL))
1281 return (SET_ERROR(EINVAL));
1283 /* Verify pool version supports SA if SA_SPILL feature set */
1284 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1285 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1286 return (SET_ERROR(ENOTSUP));
1288 if (drba->drba_cookie->drc_resumable &&
1289 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EXTENSIBLE_DATASET))
1290 return (SET_ERROR(ENOTSUP));
1293 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1294 * record to a plan WRITE record, so the pool must have the
1295 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1296 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1298 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1299 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1300 return (SET_ERROR(ENOTSUP));
1301 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1302 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1303 return (SET_ERROR(ENOTSUP));
1306 * The receiving code doesn't know how to translate large blocks
1307 * to smaller ones, so the pool must have the LARGE_BLOCKS
1308 * feature enabled if the stream has LARGE_BLOCKS.
1310 if ((featureflags & DMU_BACKUP_FEATURE_LARGE_BLOCKS) &&
1311 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LARGE_BLOCKS))
1312 return (SET_ERROR(ENOTSUP));
1314 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1316 /* target fs already exists; recv into temp clone */
1318 /* Can't recv a clone into an existing fs */
1319 if (flags & DRR_FLAG_CLONE || drba->drba_origin) {
1320 dsl_dataset_rele(ds, FTAG);
1321 return (SET_ERROR(EINVAL));
1324 error = recv_begin_check_existing_impl(drba, ds, fromguid);
1325 dsl_dataset_rele(ds, FTAG);
1326 } else if (error == ENOENT) {
1327 /* target fs does not exist; must be a full backup or clone */
1328 char buf[MAXNAMELEN];
1331 * If it's a non-clone incremental, we are missing the
1332 * target fs, so fail the recv.
1334 if (fromguid != 0 && !(flags & DRR_FLAG_CLONE ||
1336 return (SET_ERROR(ENOENT));
1339 * If we're receiving a full send as a clone, and it doesn't
1340 * contain all the necessary free records and freeobject
1341 * records, reject it.
1343 if (fromguid == 0 && drba->drba_origin &&
1344 !(flags & DRR_FLAG_FREERECORDS))
1345 return (SET_ERROR(EINVAL));
1347 /* Open the parent of tofs */
1348 ASSERT3U(strlen(tofs), <, MAXNAMELEN);
1349 (void) strlcpy(buf, tofs, strrchr(tofs, '/') - tofs + 1);
1350 error = dsl_dataset_hold(dp, buf, FTAG, &ds);
1355 * Check filesystem and snapshot limits before receiving. We'll
1356 * recheck snapshot limits again at the end (we create the
1357 * filesystems and increment those counts during begin_sync).
1359 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1360 ZFS_PROP_FILESYSTEM_LIMIT, NULL, drba->drba_cred);
1362 dsl_dataset_rele(ds, FTAG);
1366 error = dsl_fs_ss_limit_check(ds->ds_dir, 1,
1367 ZFS_PROP_SNAPSHOT_LIMIT, NULL, drba->drba_cred);
1369 dsl_dataset_rele(ds, FTAG);
1373 if (drba->drba_origin != NULL) {
1374 dsl_dataset_t *origin;
1375 error = dsl_dataset_hold(dp, drba->drba_origin,
1378 dsl_dataset_rele(ds, FTAG);
1381 if (!origin->ds_is_snapshot) {
1382 dsl_dataset_rele(origin, FTAG);
1383 dsl_dataset_rele(ds, FTAG);
1384 return (SET_ERROR(EINVAL));
1386 if (dsl_dataset_phys(origin)->ds_guid != fromguid &&
1388 dsl_dataset_rele(origin, FTAG);
1389 dsl_dataset_rele(ds, FTAG);
1390 return (SET_ERROR(ENODEV));
1392 dsl_dataset_rele(origin, FTAG);
1394 dsl_dataset_rele(ds, FTAG);
1401 dmu_recv_begin_sync(void *arg, dmu_tx_t *tx)
1403 dmu_recv_begin_arg_t *drba = arg;
1404 dsl_pool_t *dp = dmu_tx_pool(tx);
1405 objset_t *mos = dp->dp_meta_objset;
1406 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1407 const char *tofs = drba->drba_cookie->drc_tofs;
1408 dsl_dataset_t *ds, *newds;
1411 uint64_t crflags = 0;
1413 if (drrb->drr_flags & DRR_FLAG_CI_DATA)
1414 crflags |= DS_FLAG_CI_DATASET;
1416 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1418 /* create temporary clone */
1419 dsl_dataset_t *snap = NULL;
1420 if (drba->drba_snapobj != 0) {
1421 VERIFY0(dsl_dataset_hold_obj(dp,
1422 drba->drba_snapobj, FTAG, &snap));
1424 dsobj = dsl_dataset_create_sync(ds->ds_dir, recv_clone_name,
1425 snap, crflags, drba->drba_cred, tx);
1426 if (drba->drba_snapobj != 0)
1427 dsl_dataset_rele(snap, FTAG);
1428 dsl_dataset_rele(ds, FTAG);
1432 dsl_dataset_t *origin = NULL;
1434 VERIFY0(dsl_dir_hold(dp, tofs, FTAG, &dd, &tail));
1436 if (drba->drba_origin != NULL) {
1437 VERIFY0(dsl_dataset_hold(dp, drba->drba_origin,
1441 /* Create new dataset. */
1442 dsobj = dsl_dataset_create_sync(dd,
1443 strrchr(tofs, '/') + 1,
1444 origin, crflags, drba->drba_cred, tx);
1446 dsl_dataset_rele(origin, FTAG);
1447 dsl_dir_rele(dd, FTAG);
1448 drba->drba_cookie->drc_newfs = B_TRUE;
1450 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &newds));
1452 if (drba->drba_cookie->drc_resumable) {
1453 dsl_dataset_zapify(newds, tx);
1454 if (drrb->drr_fromguid != 0) {
1455 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_FROMGUID,
1456 8, 1, &drrb->drr_fromguid, tx));
1458 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TOGUID,
1459 8, 1, &drrb->drr_toguid, tx));
1460 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_TONAME,
1461 1, strlen(drrb->drr_toname) + 1, drrb->drr_toname, tx));
1464 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OBJECT,
1466 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_OFFSET,
1468 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_BYTES,
1470 if (DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo) &
1471 DMU_BACKUP_FEATURE_EMBED_DATA) {
1472 VERIFY0(zap_add(mos, dsobj, DS_FIELD_RESUME_EMBEDOK,
1477 dmu_buf_will_dirty(newds->ds_dbuf, tx);
1478 dsl_dataset_phys(newds)->ds_flags |= DS_FLAG_INCONSISTENT;
1481 * If we actually created a non-clone, we need to create the
1482 * objset in our new dataset.
1484 if (BP_IS_HOLE(dsl_dataset_get_blkptr(newds))) {
1485 (void) dmu_objset_create_impl(dp->dp_spa,
1486 newds, dsl_dataset_get_blkptr(newds), drrb->drr_type, tx);
1489 drba->drba_cookie->drc_ds = newds;
1491 spa_history_log_internal_ds(newds, "receive", tx, "");
1495 dmu_recv_resume_begin_check(void *arg, dmu_tx_t *tx)
1497 dmu_recv_begin_arg_t *drba = arg;
1498 dsl_pool_t *dp = dmu_tx_pool(tx);
1499 struct drr_begin *drrb = drba->drba_cookie->drc_drrb;
1501 uint64_t featureflags = DMU_GET_FEATUREFLAGS(drrb->drr_versioninfo);
1503 const char *tofs = drba->drba_cookie->drc_tofs;
1505 /* already checked */
1506 ASSERT3U(drrb->drr_magic, ==, DMU_BACKUP_MAGIC);
1507 ASSERT(featureflags & DMU_BACKUP_FEATURE_RESUMING);
1509 if (DMU_GET_STREAM_HDRTYPE(drrb->drr_versioninfo) ==
1510 DMU_COMPOUNDSTREAM ||
1511 drrb->drr_type >= DMU_OST_NUMTYPES)
1512 return (SET_ERROR(EINVAL));
1514 /* Verify pool version supports SA if SA_SPILL feature set */
1515 if ((featureflags & DMU_BACKUP_FEATURE_SA_SPILL) &&
1516 spa_version(dp->dp_spa) < SPA_VERSION_SA)
1517 return (SET_ERROR(ENOTSUP));
1520 * The receiving code doesn't know how to translate a WRITE_EMBEDDED
1521 * record to a plain WRITE record, so the pool must have the
1522 * EMBEDDED_DATA feature enabled if the stream has WRITE_EMBEDDED
1523 * records. Same with WRITE_EMBEDDED records that use LZ4 compression.
1525 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA) &&
1526 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_EMBEDDED_DATA))
1527 return (SET_ERROR(ENOTSUP));
1528 if ((featureflags & DMU_BACKUP_FEATURE_EMBED_DATA_LZ4) &&
1529 !spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_LZ4_COMPRESS))
1530 return (SET_ERROR(ENOTSUP));
1532 char recvname[ZFS_MAXNAMELEN];
1534 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1535 tofs, recv_clone_name);
1537 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1538 /* %recv does not exist; continue in tofs */
1539 error = dsl_dataset_hold(dp, tofs, FTAG, &ds);
1544 /* check that ds is marked inconsistent */
1545 if (!DS_IS_INCONSISTENT(ds)) {
1546 dsl_dataset_rele(ds, FTAG);
1547 return (SET_ERROR(EINVAL));
1550 /* check that there is resuming data, and that the toguid matches */
1551 if (!dsl_dataset_is_zapified(ds)) {
1552 dsl_dataset_rele(ds, FTAG);
1553 return (SET_ERROR(EINVAL));
1556 error = zap_lookup(dp->dp_meta_objset, ds->ds_object,
1557 DS_FIELD_RESUME_TOGUID, sizeof (val), 1, &val);
1558 if (error != 0 || drrb->drr_toguid != val) {
1559 dsl_dataset_rele(ds, FTAG);
1560 return (SET_ERROR(EINVAL));
1564 * Check if the receive is still running. If so, it will be owned.
1565 * Note that nothing else can own the dataset (e.g. after the receive
1566 * fails) because it will be marked inconsistent.
1568 if (dsl_dataset_has_owner(ds)) {
1569 dsl_dataset_rele(ds, FTAG);
1570 return (SET_ERROR(EBUSY));
1573 /* There should not be any snapshots of this fs yet. */
1574 if (ds->ds_prev != NULL && ds->ds_prev->ds_dir == ds->ds_dir) {
1575 dsl_dataset_rele(ds, FTAG);
1576 return (SET_ERROR(EINVAL));
1580 * Note: resume point will be checked when we process the first WRITE
1584 /* check that the origin matches */
1586 (void) zap_lookup(dp->dp_meta_objset, ds->ds_object,
1587 DS_FIELD_RESUME_FROMGUID, sizeof (val), 1, &val);
1588 if (drrb->drr_fromguid != val) {
1589 dsl_dataset_rele(ds, FTAG);
1590 return (SET_ERROR(EINVAL));
1593 dsl_dataset_rele(ds, FTAG);
1598 dmu_recv_resume_begin_sync(void *arg, dmu_tx_t *tx)
1600 dmu_recv_begin_arg_t *drba = arg;
1601 dsl_pool_t *dp = dmu_tx_pool(tx);
1602 const char *tofs = drba->drba_cookie->drc_tofs;
1605 char recvname[ZFS_MAXNAMELEN];
1607 (void) snprintf(recvname, sizeof (recvname), "%s/%s",
1608 tofs, recv_clone_name);
1610 if (dsl_dataset_hold(dp, recvname, FTAG, &ds) != 0) {
1611 /* %recv does not exist; continue in tofs */
1612 VERIFY0(dsl_dataset_hold(dp, tofs, FTAG, &ds));
1613 drba->drba_cookie->drc_newfs = B_TRUE;
1616 /* clear the inconsistent flag so that we can own it */
1617 ASSERT(DS_IS_INCONSISTENT(ds));
1618 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1619 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
1620 dsobj = ds->ds_object;
1621 dsl_dataset_rele(ds, FTAG);
1623 VERIFY0(dsl_dataset_own_obj(dp, dsobj, dmu_recv_tag, &ds));
1625 dmu_buf_will_dirty(ds->ds_dbuf, tx);
1626 dsl_dataset_phys(ds)->ds_flags |= DS_FLAG_INCONSISTENT;
1628 ASSERT(!BP_IS_HOLE(dsl_dataset_get_blkptr(ds)));
1630 drba->drba_cookie->drc_ds = ds;
1632 spa_history_log_internal_ds(ds, "resume receive", tx, "");
1636 * NB: callers *MUST* call dmu_recv_stream() if dmu_recv_begin()
1637 * succeeds; otherwise we will leak the holds on the datasets.
1640 dmu_recv_begin(char *tofs, char *tosnap, dmu_replay_record_t *drr_begin,
1641 boolean_t force, boolean_t resumable, char *origin, dmu_recv_cookie_t *drc)
1643 dmu_recv_begin_arg_t drba = { 0 };
1645 bzero(drc, sizeof (dmu_recv_cookie_t));
1646 drc->drc_drr_begin = drr_begin;
1647 drc->drc_drrb = &drr_begin->drr_u.drr_begin;
1648 drc->drc_tosnap = tosnap;
1649 drc->drc_tofs = tofs;
1650 drc->drc_force = force;
1651 drc->drc_resumable = resumable;
1652 drc->drc_cred = CRED();
1654 if (drc->drc_drrb->drr_magic == BSWAP_64(DMU_BACKUP_MAGIC)) {
1655 drc->drc_byteswap = B_TRUE;
1656 fletcher_4_incremental_byteswap(drr_begin,
1657 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1658 byteswap_record(drr_begin);
1659 } else if (drc->drc_drrb->drr_magic == DMU_BACKUP_MAGIC) {
1660 fletcher_4_incremental_native(drr_begin,
1661 sizeof (dmu_replay_record_t), &drc->drc_cksum);
1663 return (SET_ERROR(EINVAL));
1666 drba.drba_origin = origin;
1667 drba.drba_cookie = drc;
1668 drba.drba_cred = CRED();
1670 if (DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo) &
1671 DMU_BACKUP_FEATURE_RESUMING) {
1672 return (dsl_sync_task(tofs,
1673 dmu_recv_resume_begin_check, dmu_recv_resume_begin_sync,
1674 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1676 return (dsl_sync_task(tofs,
1677 dmu_recv_begin_check, dmu_recv_begin_sync,
1678 &drba, 5, ZFS_SPACE_CHECK_NORMAL));
1682 struct receive_record_arg {
1683 dmu_replay_record_t header;
1684 void *payload; /* Pointer to a buffer containing the payload */
1686 * If the record is a write, pointer to the arc_buf_t containing the
1689 arc_buf_t *write_buf;
1691 uint64_t bytes_read; /* bytes read from stream when record created */
1692 boolean_t eos_marker; /* Marks the end of the stream */
1696 struct receive_writer_arg {
1702 * These three args are used to signal to the main thread that we're
1710 /* A map from guid to dataset to help handle dedup'd streams. */
1711 avl_tree_t *guid_to_ds_map;
1712 boolean_t resumable;
1713 uint64_t last_object, last_offset;
1714 uint64_t bytes_read; /* bytes read when current record created */
1718 list_t list; /* List of struct receive_objnode. */
1720 * Last object looked up. Used to assert that objects are being looked
1721 * up in ascending order.
1723 uint64_t last_lookup;
1726 struct receive_objnode {
1731 struct receive_arg {
1735 uint64_t voff; /* The current offset in the stream */
1736 uint64_t bytes_read;
1738 * A record that has had its payload read in, but hasn't yet been handed
1739 * off to the worker thread.
1741 struct receive_record_arg *rrd;
1742 /* A record that has had its header read in, but not its payload. */
1743 struct receive_record_arg *next_rrd;
1745 zio_cksum_t prev_cksum;
1748 /* Sorted list of objects not to issue prefetches for. */
1749 struct objlist ignore_objlist;
1752 typedef struct guid_map_entry {
1754 dsl_dataset_t *gme_ds;
1759 guid_compare(const void *arg1, const void *arg2)
1761 const guid_map_entry_t *gmep1 = arg1;
1762 const guid_map_entry_t *gmep2 = arg2;
1764 if (gmep1->guid < gmep2->guid)
1766 else if (gmep1->guid > gmep2->guid)
1772 free_guid_map_onexit(void *arg)
1774 avl_tree_t *ca = arg;
1775 void *cookie = NULL;
1776 guid_map_entry_t *gmep;
1778 while ((gmep = avl_destroy_nodes(ca, &cookie)) != NULL) {
1779 dsl_dataset_long_rele(gmep->gme_ds, gmep);
1780 dsl_dataset_rele(gmep->gme_ds, gmep);
1781 kmem_free(gmep, sizeof (guid_map_entry_t));
1784 kmem_free(ca, sizeof (avl_tree_t));
1788 restore_bytes(struct receive_arg *ra, void *buf, int len, off_t off, ssize_t *resid)
1794 aiov.iov_base = buf;
1796 auio.uio_iov = &aiov;
1797 auio.uio_iovcnt = 1;
1798 auio.uio_resid = len;
1799 auio.uio_segflg = UIO_SYSSPACE;
1800 auio.uio_rw = UIO_READ;
1801 auio.uio_offset = off;
1802 auio.uio_td = ra->td;
1804 error = fo_read(ra->fp, &auio, ra->td->td_ucred, FOF_OFFSET, ra->td);
1806 fprintf(stderr, "%s: returning EOPNOTSUPP\n", __func__);
1809 *resid = auio.uio_resid;
1814 receive_read(struct receive_arg *ra, int len, void *buf)
1818 /* some things will require 8-byte alignment, so everything must */
1821 while (done < len) {
1824 ra->err = restore_bytes(ra, buf + done,
1825 len - done, ra->voff, &resid);
1827 if (resid == len - done) {
1829 * Note: ECKSUM indicates that the receive
1830 * was interrupted and can potentially be resumed.
1832 ra->err = SET_ERROR(ECKSUM);
1834 ra->voff += len - done - resid;
1840 ra->bytes_read += len;
1842 ASSERT3U(done, ==, len);
1847 byteswap_record(dmu_replay_record_t *drr)
1849 #define DO64(X) (drr->drr_u.X = BSWAP_64(drr->drr_u.X))
1850 #define DO32(X) (drr->drr_u.X = BSWAP_32(drr->drr_u.X))
1851 drr->drr_type = BSWAP_32(drr->drr_type);
1852 drr->drr_payloadlen = BSWAP_32(drr->drr_payloadlen);
1854 switch (drr->drr_type) {
1856 DO64(drr_begin.drr_magic);
1857 DO64(drr_begin.drr_versioninfo);
1858 DO64(drr_begin.drr_creation_time);
1859 DO32(drr_begin.drr_type);
1860 DO32(drr_begin.drr_flags);
1861 DO64(drr_begin.drr_toguid);
1862 DO64(drr_begin.drr_fromguid);
1865 DO64(drr_object.drr_object);
1866 DO32(drr_object.drr_type);
1867 DO32(drr_object.drr_bonustype);
1868 DO32(drr_object.drr_blksz);
1869 DO32(drr_object.drr_bonuslen);
1870 DO64(drr_object.drr_toguid);
1872 case DRR_FREEOBJECTS:
1873 DO64(drr_freeobjects.drr_firstobj);
1874 DO64(drr_freeobjects.drr_numobjs);
1875 DO64(drr_freeobjects.drr_toguid);
1878 DO64(drr_write.drr_object);
1879 DO32(drr_write.drr_type);
1880 DO64(drr_write.drr_offset);
1881 DO64(drr_write.drr_length);
1882 DO64(drr_write.drr_toguid);
1883 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write.drr_key.ddk_cksum);
1884 DO64(drr_write.drr_key.ddk_prop);
1886 case DRR_WRITE_BYREF:
1887 DO64(drr_write_byref.drr_object);
1888 DO64(drr_write_byref.drr_offset);
1889 DO64(drr_write_byref.drr_length);
1890 DO64(drr_write_byref.drr_toguid);
1891 DO64(drr_write_byref.drr_refguid);
1892 DO64(drr_write_byref.drr_refobject);
1893 DO64(drr_write_byref.drr_refoffset);
1894 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_write_byref.
1896 DO64(drr_write_byref.drr_key.ddk_prop);
1898 case DRR_WRITE_EMBEDDED:
1899 DO64(drr_write_embedded.drr_object);
1900 DO64(drr_write_embedded.drr_offset);
1901 DO64(drr_write_embedded.drr_length);
1902 DO64(drr_write_embedded.drr_toguid);
1903 DO32(drr_write_embedded.drr_lsize);
1904 DO32(drr_write_embedded.drr_psize);
1907 DO64(drr_free.drr_object);
1908 DO64(drr_free.drr_offset);
1909 DO64(drr_free.drr_length);
1910 DO64(drr_free.drr_toguid);
1913 DO64(drr_spill.drr_object);
1914 DO64(drr_spill.drr_length);
1915 DO64(drr_spill.drr_toguid);
1918 DO64(drr_end.drr_toguid);
1919 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_end.drr_checksum);
1923 if (drr->drr_type != DRR_BEGIN) {
1924 ZIO_CHECKSUM_BSWAP(&drr->drr_u.drr_checksum.drr_checksum);
1931 static inline uint8_t
1932 deduce_nblkptr(dmu_object_type_t bonus_type, uint64_t bonus_size)
1934 if (bonus_type == DMU_OT_SA) {
1938 ((DN_MAX_BONUSLEN - bonus_size) >> SPA_BLKPTRSHIFT));
1943 save_resume_state(struct receive_writer_arg *rwa,
1944 uint64_t object, uint64_t offset, dmu_tx_t *tx)
1946 int txgoff = dmu_tx_get_txg(tx) & TXG_MASK;
1948 if (!rwa->resumable)
1952 * We use ds_resume_bytes[] != 0 to indicate that we need to
1953 * update this on disk, so it must not be 0.
1955 ASSERT(rwa->bytes_read != 0);
1958 * We only resume from write records, which have a valid
1959 * (non-meta-dnode) object number.
1961 ASSERT(object != 0);
1964 * For resuming to work correctly, we must receive records in order,
1965 * sorted by object,offset. This is checked by the callers, but
1966 * assert it here for good measure.
1968 ASSERT3U(object, >=, rwa->os->os_dsl_dataset->ds_resume_object[txgoff]);
1969 ASSERT(object != rwa->os->os_dsl_dataset->ds_resume_object[txgoff] ||
1970 offset >= rwa->os->os_dsl_dataset->ds_resume_offset[txgoff]);
1971 ASSERT3U(rwa->bytes_read, >=,
1972 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff]);
1974 rwa->os->os_dsl_dataset->ds_resume_object[txgoff] = object;
1975 rwa->os->os_dsl_dataset->ds_resume_offset[txgoff] = offset;
1976 rwa->os->os_dsl_dataset->ds_resume_bytes[txgoff] = rwa->bytes_read;
1980 receive_object(struct receive_writer_arg *rwa, struct drr_object *drro,
1983 dmu_object_info_t doi;
1988 if (drro->drr_type == DMU_OT_NONE ||
1989 !DMU_OT_IS_VALID(drro->drr_type) ||
1990 !DMU_OT_IS_VALID(drro->drr_bonustype) ||
1991 drro->drr_checksumtype >= ZIO_CHECKSUM_FUNCTIONS ||
1992 drro->drr_compress >= ZIO_COMPRESS_FUNCTIONS ||
1993 P2PHASE(drro->drr_blksz, SPA_MINBLOCKSIZE) ||
1994 drro->drr_blksz < SPA_MINBLOCKSIZE ||
1995 drro->drr_blksz > spa_maxblocksize(dmu_objset_spa(rwa->os)) ||
1996 drro->drr_bonuslen > DN_MAX_BONUSLEN) {
1997 return (SET_ERROR(EINVAL));
2000 err = dmu_object_info(rwa->os, drro->drr_object, &doi);
2002 if (err != 0 && err != ENOENT)
2003 return (SET_ERROR(EINVAL));
2004 object = err == 0 ? drro->drr_object : DMU_NEW_OBJECT;
2007 * If we are losing blkptrs or changing the block size this must
2008 * be a new file instance. We must clear out the previous file
2009 * contents before we can change this type of metadata in the dnode.
2014 nblkptr = deduce_nblkptr(drro->drr_bonustype,
2015 drro->drr_bonuslen);
2017 if (drro->drr_blksz != doi.doi_data_block_size ||
2018 nblkptr < doi.doi_nblkptr) {
2019 err = dmu_free_long_range(rwa->os, drro->drr_object,
2022 return (SET_ERROR(EINVAL));
2026 tx = dmu_tx_create(rwa->os);
2027 dmu_tx_hold_bonus(tx, object);
2028 err = dmu_tx_assign(tx, TXG_WAIT);
2034 if (object == DMU_NEW_OBJECT) {
2035 /* currently free, want to be allocated */
2036 err = dmu_object_claim(rwa->os, drro->drr_object,
2037 drro->drr_type, drro->drr_blksz,
2038 drro->drr_bonustype, drro->drr_bonuslen, tx);
2039 } else if (drro->drr_type != doi.doi_type ||
2040 drro->drr_blksz != doi.doi_data_block_size ||
2041 drro->drr_bonustype != doi.doi_bonus_type ||
2042 drro->drr_bonuslen != doi.doi_bonus_size) {
2043 /* currently allocated, but with different properties */
2044 err = dmu_object_reclaim(rwa->os, drro->drr_object,
2045 drro->drr_type, drro->drr_blksz,
2046 drro->drr_bonustype, drro->drr_bonuslen, tx);
2050 return (SET_ERROR(EINVAL));
2053 dmu_object_set_checksum(rwa->os, drro->drr_object,
2054 drro->drr_checksumtype, tx);
2055 dmu_object_set_compress(rwa->os, drro->drr_object,
2056 drro->drr_compress, tx);
2061 VERIFY0(dmu_bonus_hold(rwa->os, drro->drr_object, FTAG, &db));
2062 dmu_buf_will_dirty(db, tx);
2064 ASSERT3U(db->db_size, >=, drro->drr_bonuslen);
2065 bcopy(data, db->db_data, drro->drr_bonuslen);
2066 if (rwa->byteswap) {
2067 dmu_object_byteswap_t byteswap =
2068 DMU_OT_BYTESWAP(drro->drr_bonustype);
2069 dmu_ot_byteswap[byteswap].ob_func(db->db_data,
2070 drro->drr_bonuslen);
2072 dmu_buf_rele(db, FTAG);
2081 receive_freeobjects(struct receive_writer_arg *rwa,
2082 struct drr_freeobjects *drrfo)
2087 if (drrfo->drr_firstobj + drrfo->drr_numobjs < drrfo->drr_firstobj)
2088 return (SET_ERROR(EINVAL));
2090 for (obj = drrfo->drr_firstobj;
2091 obj < drrfo->drr_firstobj + drrfo->drr_numobjs && next_err == 0;
2092 next_err = dmu_object_next(rwa->os, &obj, FALSE, 0)) {
2095 if (dmu_object_info(rwa->os, obj, NULL) != 0)
2098 err = dmu_free_long_object(rwa->os, obj);
2102 if (next_err != ESRCH)
2108 receive_write(struct receive_writer_arg *rwa, struct drr_write *drrw,
2114 if (drrw->drr_offset + drrw->drr_length < drrw->drr_offset ||
2115 !DMU_OT_IS_VALID(drrw->drr_type))
2116 return (SET_ERROR(EINVAL));
2119 * For resuming to work, records must be in increasing order
2120 * by (object, offset).
2122 if (drrw->drr_object < rwa->last_object ||
2123 (drrw->drr_object == rwa->last_object &&
2124 drrw->drr_offset < rwa->last_offset)) {
2125 return (SET_ERROR(EINVAL));
2127 rwa->last_object = drrw->drr_object;
2128 rwa->last_offset = drrw->drr_offset;
2130 if (dmu_object_info(rwa->os, drrw->drr_object, NULL) != 0)
2131 return (SET_ERROR(EINVAL));
2133 tx = dmu_tx_create(rwa->os);
2135 dmu_tx_hold_write(tx, drrw->drr_object,
2136 drrw->drr_offset, drrw->drr_length);
2137 err = dmu_tx_assign(tx, TXG_WAIT);
2142 if (rwa->byteswap) {
2143 dmu_object_byteswap_t byteswap =
2144 DMU_OT_BYTESWAP(drrw->drr_type);
2145 dmu_ot_byteswap[byteswap].ob_func(abuf->b_data,
2150 if (dmu_bonus_hold(rwa->os, drrw->drr_object, FTAG, &bonus) != 0)
2151 return (SET_ERROR(EINVAL));
2152 dmu_assign_arcbuf(bonus, drrw->drr_offset, abuf, tx);
2155 * Note: If the receive fails, we want the resume stream to start
2156 * with the same record that we last successfully received (as opposed
2157 * to the next record), so that we can verify that we are
2158 * resuming from the correct location.
2160 save_resume_state(rwa, drrw->drr_object, drrw->drr_offset, tx);
2162 dmu_buf_rele(bonus, FTAG);
2168 * Handle a DRR_WRITE_BYREF record. This record is used in dedup'ed
2169 * streams to refer to a copy of the data that is already on the
2170 * system because it came in earlier in the stream. This function
2171 * finds the earlier copy of the data, and uses that copy instead of
2172 * data from the stream to fulfill this write.
2175 receive_write_byref(struct receive_writer_arg *rwa,
2176 struct drr_write_byref *drrwbr)
2180 guid_map_entry_t gmesrch;
2181 guid_map_entry_t *gmep;
2183 objset_t *ref_os = NULL;
2186 if (drrwbr->drr_offset + drrwbr->drr_length < drrwbr->drr_offset)
2187 return (SET_ERROR(EINVAL));
2190 * If the GUID of the referenced dataset is different from the
2191 * GUID of the target dataset, find the referenced dataset.
2193 if (drrwbr->drr_toguid != drrwbr->drr_refguid) {
2194 gmesrch.guid = drrwbr->drr_refguid;
2195 if ((gmep = avl_find(rwa->guid_to_ds_map, &gmesrch,
2197 return (SET_ERROR(EINVAL));
2199 if (dmu_objset_from_ds(gmep->gme_ds, &ref_os))
2200 return (SET_ERROR(EINVAL));
2205 err = dmu_buf_hold(ref_os, drrwbr->drr_refobject,
2206 drrwbr->drr_refoffset, FTAG, &dbp, DMU_READ_PREFETCH);
2210 tx = dmu_tx_create(rwa->os);
2212 dmu_tx_hold_write(tx, drrwbr->drr_object,
2213 drrwbr->drr_offset, drrwbr->drr_length);
2214 err = dmu_tx_assign(tx, TXG_WAIT);
2219 dmu_write(rwa->os, drrwbr->drr_object,
2220 drrwbr->drr_offset, drrwbr->drr_length, dbp->db_data, tx);
2221 dmu_buf_rele(dbp, FTAG);
2223 /* See comment in restore_write. */
2224 save_resume_state(rwa, drrwbr->drr_object, drrwbr->drr_offset, tx);
2230 receive_write_embedded(struct receive_writer_arg *rwa,
2231 struct drr_write_embedded *drrwe, void *data)
2236 if (drrwe->drr_offset + drrwe->drr_length < drrwe->drr_offset)
2239 if (drrwe->drr_psize > BPE_PAYLOAD_SIZE)
2242 if (drrwe->drr_etype >= NUM_BP_EMBEDDED_TYPES)
2244 if (drrwe->drr_compression >= ZIO_COMPRESS_FUNCTIONS)
2247 tx = dmu_tx_create(rwa->os);
2249 dmu_tx_hold_write(tx, drrwe->drr_object,
2250 drrwe->drr_offset, drrwe->drr_length);
2251 err = dmu_tx_assign(tx, TXG_WAIT);
2257 dmu_write_embedded(rwa->os, drrwe->drr_object,
2258 drrwe->drr_offset, data, drrwe->drr_etype,
2259 drrwe->drr_compression, drrwe->drr_lsize, drrwe->drr_psize,
2260 rwa->byteswap ^ ZFS_HOST_BYTEORDER, tx);
2262 /* See comment in restore_write. */
2263 save_resume_state(rwa, drrwe->drr_object, drrwe->drr_offset, tx);
2269 receive_spill(struct receive_writer_arg *rwa, struct drr_spill *drrs,
2273 dmu_buf_t *db, *db_spill;
2276 if (drrs->drr_length < SPA_MINBLOCKSIZE ||
2277 drrs->drr_length > spa_maxblocksize(dmu_objset_spa(rwa->os)))
2278 return (SET_ERROR(EINVAL));
2280 if (dmu_object_info(rwa->os, drrs->drr_object, NULL) != 0)
2281 return (SET_ERROR(EINVAL));
2283 VERIFY0(dmu_bonus_hold(rwa->os, drrs->drr_object, FTAG, &db));
2284 if ((err = dmu_spill_hold_by_bonus(db, FTAG, &db_spill)) != 0) {
2285 dmu_buf_rele(db, FTAG);
2289 tx = dmu_tx_create(rwa->os);
2291 dmu_tx_hold_spill(tx, db->db_object);
2293 err = dmu_tx_assign(tx, TXG_WAIT);
2295 dmu_buf_rele(db, FTAG);
2296 dmu_buf_rele(db_spill, FTAG);
2300 dmu_buf_will_dirty(db_spill, tx);
2302 if (db_spill->db_size < drrs->drr_length)
2303 VERIFY(0 == dbuf_spill_set_blksz(db_spill,
2304 drrs->drr_length, tx));
2305 bcopy(data, db_spill->db_data, drrs->drr_length);
2307 dmu_buf_rele(db, FTAG);
2308 dmu_buf_rele(db_spill, FTAG);
2316 receive_free(struct receive_writer_arg *rwa, struct drr_free *drrf)
2320 if (drrf->drr_length != -1ULL &&
2321 drrf->drr_offset + drrf->drr_length < drrf->drr_offset)
2322 return (SET_ERROR(EINVAL));
2324 if (dmu_object_info(rwa->os, drrf->drr_object, NULL) != 0)
2325 return (SET_ERROR(EINVAL));
2327 err = dmu_free_long_range(rwa->os, drrf->drr_object,
2328 drrf->drr_offset, drrf->drr_length);
2333 /* used to destroy the drc_ds on error */
2335 dmu_recv_cleanup_ds(dmu_recv_cookie_t *drc)
2337 if (drc->drc_resumable) {
2338 /* wait for our resume state to be written to disk */
2339 txg_wait_synced(drc->drc_ds->ds_dir->dd_pool, 0);
2340 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2342 char name[MAXNAMELEN];
2343 dsl_dataset_name(drc->drc_ds, name);
2344 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
2345 (void) dsl_destroy_head(name);
2350 receive_cksum(struct receive_arg *ra, int len, void *buf)
2353 fletcher_4_incremental_byteswap(buf, len, &ra->cksum);
2355 fletcher_4_incremental_native(buf, len, &ra->cksum);
2360 * Read the payload into a buffer of size len, and update the current record's
2362 * Allocate ra->next_rrd and read the next record's header into
2363 * ra->next_rrd->header.
2364 * Verify checksum of payload and next record.
2367 receive_read_payload_and_next_header(struct receive_arg *ra, int len, void *buf)
2372 ASSERT3U(len, <=, SPA_MAXBLOCKSIZE);
2373 err = receive_read(ra, len, buf);
2376 receive_cksum(ra, len, buf);
2378 /* note: rrd is NULL when reading the begin record's payload */
2379 if (ra->rrd != NULL) {
2380 ra->rrd->payload = buf;
2381 ra->rrd->payload_size = len;
2382 ra->rrd->bytes_read = ra->bytes_read;
2386 ra->prev_cksum = ra->cksum;
2388 ra->next_rrd = kmem_zalloc(sizeof (*ra->next_rrd), KM_SLEEP);
2389 err = receive_read(ra, sizeof (ra->next_rrd->header),
2390 &ra->next_rrd->header);
2391 ra->next_rrd->bytes_read = ra->bytes_read;
2393 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2394 ra->next_rrd = NULL;
2397 if (ra->next_rrd->header.drr_type == DRR_BEGIN) {
2398 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2399 ra->next_rrd = NULL;
2400 return (SET_ERROR(EINVAL));
2404 * Note: checksum is of everything up to but not including the
2407 ASSERT3U(offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2408 ==, sizeof (dmu_replay_record_t) - sizeof (zio_cksum_t));
2410 offsetof(dmu_replay_record_t, drr_u.drr_checksum.drr_checksum),
2411 &ra->next_rrd->header);
2413 zio_cksum_t cksum_orig =
2414 ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2415 zio_cksum_t *cksump =
2416 &ra->next_rrd->header.drr_u.drr_checksum.drr_checksum;
2419 byteswap_record(&ra->next_rrd->header);
2421 if ((!ZIO_CHECKSUM_IS_ZERO(cksump)) &&
2422 !ZIO_CHECKSUM_EQUAL(ra->cksum, *cksump)) {
2423 kmem_free(ra->next_rrd, sizeof (*ra->next_rrd));
2424 ra->next_rrd = NULL;
2425 return (SET_ERROR(ECKSUM));
2428 receive_cksum(ra, sizeof (cksum_orig), &cksum_orig);
2434 objlist_create(struct objlist *list)
2436 list_create(&list->list, sizeof (struct receive_objnode),
2437 offsetof(struct receive_objnode, node));
2438 list->last_lookup = 0;
2442 objlist_destroy(struct objlist *list)
2444 for (struct receive_objnode *n = list_remove_head(&list->list);
2445 n != NULL; n = list_remove_head(&list->list)) {
2446 kmem_free(n, sizeof (*n));
2448 list_destroy(&list->list);
2452 * This function looks through the objlist to see if the specified object number
2453 * is contained in the objlist. In the process, it will remove all object
2454 * numbers in the list that are smaller than the specified object number. Thus,
2455 * any lookup of an object number smaller than a previously looked up object
2456 * number will always return false; therefore, all lookups should be done in
2460 objlist_exists(struct objlist *list, uint64_t object)
2462 struct receive_objnode *node = list_head(&list->list);
2463 ASSERT3U(object, >=, list->last_lookup);
2464 list->last_lookup = object;
2465 while (node != NULL && node->object < object) {
2466 VERIFY3P(node, ==, list_remove_head(&list->list));
2467 kmem_free(node, sizeof (*node));
2468 node = list_head(&list->list);
2470 return (node != NULL && node->object == object);
2474 * The objlist is a list of object numbers stored in ascending order. However,
2475 * the insertion of new object numbers does not seek out the correct location to
2476 * store a new object number; instead, it appends it to the list for simplicity.
2477 * Thus, any users must take care to only insert new object numbers in ascending
2481 objlist_insert(struct objlist *list, uint64_t object)
2483 struct receive_objnode *node = kmem_zalloc(sizeof (*node), KM_SLEEP);
2484 node->object = object;
2486 struct receive_objnode *last_object = list_tail(&list->list);
2487 uint64_t last_objnum = (last_object != NULL ? last_object->object : 0);
2488 ASSERT3U(node->object, >, last_objnum);
2490 list_insert_tail(&list->list, node);
2494 * Issue the prefetch reads for any necessary indirect blocks.
2496 * We use the object ignore list to tell us whether or not to issue prefetches
2497 * for a given object. We do this for both correctness (in case the blocksize
2498 * of an object has changed) and performance (if the object doesn't exist, don't
2499 * needlessly try to issue prefetches). We also trim the list as we go through
2500 * the stream to prevent it from growing to an unbounded size.
2502 * The object numbers within will always be in sorted order, and any write
2503 * records we see will also be in sorted order, but they're not sorted with
2504 * respect to each other (i.e. we can get several object records before
2505 * receiving each object's write records). As a result, once we've reached a
2506 * given object number, we can safely remove any reference to lower object
2507 * numbers in the ignore list. In practice, we receive up to 32 object records
2508 * before receiving write records, so the list can have up to 32 nodes in it.
2512 receive_read_prefetch(struct receive_arg *ra,
2513 uint64_t object, uint64_t offset, uint64_t length)
2515 if (!objlist_exists(&ra->ignore_objlist, object)) {
2516 dmu_prefetch(ra->os, object, 1, offset, length,
2517 ZIO_PRIORITY_SYNC_READ);
2522 * Read records off the stream, issuing any necessary prefetches.
2525 receive_read_record(struct receive_arg *ra)
2529 switch (ra->rrd->header.drr_type) {
2532 struct drr_object *drro = &ra->rrd->header.drr_u.drr_object;
2533 uint32_t size = P2ROUNDUP(drro->drr_bonuslen, 8);
2534 void *buf = kmem_zalloc(size, KM_SLEEP);
2535 dmu_object_info_t doi;
2536 err = receive_read_payload_and_next_header(ra, size, buf);
2538 kmem_free(buf, size);
2541 err = dmu_object_info(ra->os, drro->drr_object, &doi);
2543 * See receive_read_prefetch for an explanation why we're
2544 * storing this object in the ignore_obj_list.
2546 if (err == ENOENT ||
2547 (err == 0 && doi.doi_data_block_size != drro->drr_blksz)) {
2548 objlist_insert(&ra->ignore_objlist, drro->drr_object);
2553 case DRR_FREEOBJECTS:
2555 err = receive_read_payload_and_next_header(ra, 0, NULL);
2560 struct drr_write *drrw = &ra->rrd->header.drr_u.drr_write;
2561 arc_buf_t *abuf = arc_loan_buf(dmu_objset_spa(ra->os),
2564 err = receive_read_payload_and_next_header(ra,
2565 drrw->drr_length, abuf->b_data);
2567 dmu_return_arcbuf(abuf);
2570 ra->rrd->write_buf = abuf;
2571 receive_read_prefetch(ra, drrw->drr_object, drrw->drr_offset,
2575 case DRR_WRITE_BYREF:
2577 struct drr_write_byref *drrwb =
2578 &ra->rrd->header.drr_u.drr_write_byref;
2579 err = receive_read_payload_and_next_header(ra, 0, NULL);
2580 receive_read_prefetch(ra, drrwb->drr_object, drrwb->drr_offset,
2584 case DRR_WRITE_EMBEDDED:
2586 struct drr_write_embedded *drrwe =
2587 &ra->rrd->header.drr_u.drr_write_embedded;
2588 uint32_t size = P2ROUNDUP(drrwe->drr_psize, 8);
2589 void *buf = kmem_zalloc(size, KM_SLEEP);
2591 err = receive_read_payload_and_next_header(ra, size, buf);
2593 kmem_free(buf, size);
2597 receive_read_prefetch(ra, drrwe->drr_object, drrwe->drr_offset,
2604 * It might be beneficial to prefetch indirect blocks here, but
2605 * we don't really have the data to decide for sure.
2607 err = receive_read_payload_and_next_header(ra, 0, NULL);
2612 struct drr_end *drre = &ra->rrd->header.drr_u.drr_end;
2613 if (!ZIO_CHECKSUM_EQUAL(ra->prev_cksum, drre->drr_checksum))
2614 return (SET_ERROR(ECKSUM));
2619 struct drr_spill *drrs = &ra->rrd->header.drr_u.drr_spill;
2620 void *buf = kmem_zalloc(drrs->drr_length, KM_SLEEP);
2621 err = receive_read_payload_and_next_header(ra, drrs->drr_length,
2624 kmem_free(buf, drrs->drr_length);
2628 return (SET_ERROR(EINVAL));
2633 * Commit the records to the pool.
2636 receive_process_record(struct receive_writer_arg *rwa,
2637 struct receive_record_arg *rrd)
2641 /* Processing in order, therefore bytes_read should be increasing. */
2642 ASSERT3U(rrd->bytes_read, >=, rwa->bytes_read);
2643 rwa->bytes_read = rrd->bytes_read;
2645 switch (rrd->header.drr_type) {
2648 struct drr_object *drro = &rrd->header.drr_u.drr_object;
2649 err = receive_object(rwa, drro, rrd->payload);
2650 kmem_free(rrd->payload, rrd->payload_size);
2651 rrd->payload = NULL;
2654 case DRR_FREEOBJECTS:
2656 struct drr_freeobjects *drrfo =
2657 &rrd->header.drr_u.drr_freeobjects;
2658 return (receive_freeobjects(rwa, drrfo));
2662 struct drr_write *drrw = &rrd->header.drr_u.drr_write;
2663 err = receive_write(rwa, drrw, rrd->write_buf);
2664 /* if receive_write() is successful, it consumes the arc_buf */
2666 dmu_return_arcbuf(rrd->write_buf);
2667 rrd->write_buf = NULL;
2668 rrd->payload = NULL;
2671 case DRR_WRITE_BYREF:
2673 struct drr_write_byref *drrwbr =
2674 &rrd->header.drr_u.drr_write_byref;
2675 return (receive_write_byref(rwa, drrwbr));
2677 case DRR_WRITE_EMBEDDED:
2679 struct drr_write_embedded *drrwe =
2680 &rrd->header.drr_u.drr_write_embedded;
2681 err = receive_write_embedded(rwa, drrwe, rrd->payload);
2682 kmem_free(rrd->payload, rrd->payload_size);
2683 rrd->payload = NULL;
2688 struct drr_free *drrf = &rrd->header.drr_u.drr_free;
2689 return (receive_free(rwa, drrf));
2693 struct drr_spill *drrs = &rrd->header.drr_u.drr_spill;
2694 err = receive_spill(rwa, drrs, rrd->payload);
2695 kmem_free(rrd->payload, rrd->payload_size);
2696 rrd->payload = NULL;
2700 return (SET_ERROR(EINVAL));
2705 * dmu_recv_stream's worker thread; pull records off the queue, and then call
2706 * receive_process_record When we're done, signal the main thread and exit.
2709 receive_writer_thread(void *arg)
2711 struct receive_writer_arg *rwa = arg;
2712 struct receive_record_arg *rrd;
2713 for (rrd = bqueue_dequeue(&rwa->q); !rrd->eos_marker;
2714 rrd = bqueue_dequeue(&rwa->q)) {
2716 * If there's an error, the main thread will stop putting things
2717 * on the queue, but we need to clear everything in it before we
2720 if (rwa->err == 0) {
2721 rwa->err = receive_process_record(rwa, rrd);
2722 } else if (rrd->write_buf != NULL) {
2723 dmu_return_arcbuf(rrd->write_buf);
2724 rrd->write_buf = NULL;
2725 rrd->payload = NULL;
2726 } else if (rrd->payload != NULL) {
2727 kmem_free(rrd->payload, rrd->payload_size);
2728 rrd->payload = NULL;
2730 kmem_free(rrd, sizeof (*rrd));
2732 kmem_free(rrd, sizeof (*rrd));
2733 mutex_enter(&rwa->mutex);
2735 cv_signal(&rwa->cv);
2736 mutex_exit(&rwa->mutex);
2741 resume_check(struct receive_arg *ra, nvlist_t *begin_nvl)
2744 objset_t *mos = dmu_objset_pool(ra->os)->dp_meta_objset;
2745 uint64_t dsobj = dmu_objset_id(ra->os);
2746 uint64_t resume_obj, resume_off;
2748 if (nvlist_lookup_uint64(begin_nvl,
2749 "resume_object", &resume_obj) != 0 ||
2750 nvlist_lookup_uint64(begin_nvl,
2751 "resume_offset", &resume_off) != 0) {
2752 return (SET_ERROR(EINVAL));
2754 VERIFY0(zap_lookup(mos, dsobj,
2755 DS_FIELD_RESUME_OBJECT, sizeof (val), 1, &val));
2756 if (resume_obj != val)
2757 return (SET_ERROR(EINVAL));
2758 VERIFY0(zap_lookup(mos, dsobj,
2759 DS_FIELD_RESUME_OFFSET, sizeof (val), 1, &val));
2760 if (resume_off != val)
2761 return (SET_ERROR(EINVAL));
2767 * Read in the stream's records, one by one, and apply them to the pool. There
2768 * are two threads involved; the thread that calls this function will spin up a
2769 * worker thread, read the records off the stream one by one, and issue
2770 * prefetches for any necessary indirect blocks. It will then push the records
2771 * onto an internal blocking queue. The worker thread will pull the records off
2772 * the queue, and actually write the data into the DMU. This way, the worker
2773 * thread doesn't have to wait for reads to complete, since everything it needs
2774 * (the indirect blocks) will be prefetched.
2776 * NB: callers *must* call dmu_recv_end() if this succeeds.
2779 dmu_recv_stream(dmu_recv_cookie_t *drc, struct file *fp, offset_t *voffp,
2780 int cleanup_fd, uint64_t *action_handlep)
2783 struct receive_arg ra = { 0 };
2784 struct receive_writer_arg rwa = { 0 };
2786 nvlist_t *begin_nvl = NULL;
2788 ra.byteswap = drc->drc_byteswap;
2789 ra.cksum = drc->drc_cksum;
2794 if (dsl_dataset_is_zapified(drc->drc_ds)) {
2795 (void) zap_lookup(drc->drc_ds->ds_dir->dd_pool->dp_meta_objset,
2796 drc->drc_ds->ds_object, DS_FIELD_RESUME_BYTES,
2797 sizeof (ra.bytes_read), 1, &ra.bytes_read);
2800 objlist_create(&ra.ignore_objlist);
2802 /* these were verified in dmu_recv_begin */
2803 ASSERT3U(DMU_GET_STREAM_HDRTYPE(drc->drc_drrb->drr_versioninfo), ==,
2805 ASSERT3U(drc->drc_drrb->drr_type, <, DMU_OST_NUMTYPES);
2808 * Open the objset we are modifying.
2810 VERIFY0(dmu_objset_from_ds(drc->drc_ds, &ra.os));
2812 ASSERT(dsl_dataset_phys(drc->drc_ds)->ds_flags & DS_FLAG_INCONSISTENT);
2814 featureflags = DMU_GET_FEATUREFLAGS(drc->drc_drrb->drr_versioninfo);
2816 /* if this stream is dedup'ed, set up the avl tree for guid mapping */
2817 if (featureflags & DMU_BACKUP_FEATURE_DEDUP) {
2820 if (cleanup_fd == -1) {
2821 ra.err = SET_ERROR(EBADF);
2824 ra.err = zfs_onexit_fd_hold(cleanup_fd, &minor);
2830 if (*action_handlep == 0) {
2831 rwa.guid_to_ds_map =
2832 kmem_alloc(sizeof (avl_tree_t), KM_SLEEP);
2833 avl_create(rwa.guid_to_ds_map, guid_compare,
2834 sizeof (guid_map_entry_t),
2835 offsetof(guid_map_entry_t, avlnode));
2836 err = zfs_onexit_add_cb(minor,
2837 free_guid_map_onexit, rwa.guid_to_ds_map,
2842 err = zfs_onexit_cb_data(minor, *action_handlep,
2843 (void **)&rwa.guid_to_ds_map);
2848 drc->drc_guid_to_ds_map = rwa.guid_to_ds_map;
2851 uint32_t payloadlen = drc->drc_drr_begin->drr_payloadlen;
2852 void *payload = NULL;
2853 if (payloadlen != 0)
2854 payload = kmem_alloc(payloadlen, KM_SLEEP);
2856 err = receive_read_payload_and_next_header(&ra, payloadlen, payload);
2858 if (payloadlen != 0)
2859 kmem_free(payload, payloadlen);
2862 if (payloadlen != 0) {
2863 err = nvlist_unpack(payload, payloadlen, &begin_nvl, KM_SLEEP);
2864 kmem_free(payload, payloadlen);
2869 if (featureflags & DMU_BACKUP_FEATURE_RESUMING) {
2870 err = resume_check(&ra, begin_nvl);
2875 (void) bqueue_init(&rwa.q, zfs_recv_queue_length,
2876 offsetof(struct receive_record_arg, node));
2877 cv_init(&rwa.cv, NULL, CV_DEFAULT, NULL);
2878 mutex_init(&rwa.mutex, NULL, MUTEX_DEFAULT, NULL);
2880 rwa.byteswap = drc->drc_byteswap;
2881 rwa.resumable = drc->drc_resumable;
2883 (void) thread_create(NULL, 0, receive_writer_thread, &rwa, 0, &p0,
2884 TS_RUN, minclsyspri);
2886 * We're reading rwa.err without locks, which is safe since we are the
2887 * only reader, and the worker thread is the only writer. It's ok if we
2888 * miss a write for an iteration or two of the loop, since the writer
2889 * thread will keep freeing records we send it until we send it an eos
2892 * We can leave this loop in 3 ways: First, if rwa.err is
2893 * non-zero. In that case, the writer thread will free the rrd we just
2894 * pushed. Second, if we're interrupted; in that case, either it's the
2895 * first loop and ra.rrd was never allocated, or it's later, and ra.rrd
2896 * has been handed off to the writer thread who will free it. Finally,
2897 * if receive_read_record fails or we're at the end of the stream, then
2898 * we free ra.rrd and exit.
2900 while (rwa.err == 0) {
2901 if (issig(JUSTLOOKING) && issig(FORREAL)) {
2902 err = SET_ERROR(EINTR);
2906 ASSERT3P(ra.rrd, ==, NULL);
2907 ra.rrd = ra.next_rrd;
2909 /* Allocates and loads header into ra.next_rrd */
2910 err = receive_read_record(&ra);
2912 if (ra.rrd->header.drr_type == DRR_END || err != 0) {
2913 kmem_free(ra.rrd, sizeof (*ra.rrd));
2918 bqueue_enqueue(&rwa.q, ra.rrd,
2919 sizeof (struct receive_record_arg) + ra.rrd->payload_size);
2922 if (ra.next_rrd == NULL)
2923 ra.next_rrd = kmem_zalloc(sizeof (*ra.next_rrd), KM_SLEEP);
2924 ra.next_rrd->eos_marker = B_TRUE;
2925 bqueue_enqueue(&rwa.q, ra.next_rrd, 1);
2927 mutex_enter(&rwa.mutex);
2929 cv_wait(&rwa.cv, &rwa.mutex);
2931 mutex_exit(&rwa.mutex);
2933 cv_destroy(&rwa.cv);
2934 mutex_destroy(&rwa.mutex);
2935 bqueue_destroy(&rwa.q);
2940 nvlist_free(begin_nvl);
2941 if ((featureflags & DMU_BACKUP_FEATURE_DEDUP) && (cleanup_fd != -1))
2942 zfs_onexit_fd_rele(cleanup_fd);
2946 * Clean up references. If receive is not resumable,
2947 * destroy what we created, so we don't leave it in
2948 * the inconsistent state.
2950 dmu_recv_cleanup_ds(drc);
2954 objlist_destroy(&ra.ignore_objlist);
2959 dmu_recv_end_check(void *arg, dmu_tx_t *tx)
2961 dmu_recv_cookie_t *drc = arg;
2962 dsl_pool_t *dp = dmu_tx_pool(tx);
2965 ASSERT3P(drc->drc_ds->ds_owner, ==, dmu_recv_tag);
2967 if (!drc->drc_newfs) {
2968 dsl_dataset_t *origin_head;
2970 error = dsl_dataset_hold(dp, drc->drc_tofs, FTAG, &origin_head);
2973 if (drc->drc_force) {
2975 * We will destroy any snapshots in tofs (i.e. before
2976 * origin_head) that are after the origin (which is
2977 * the snap before drc_ds, because drc_ds can not
2978 * have any snaps of its own).
2982 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
2984 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
2985 dsl_dataset_t *snap;
2986 error = dsl_dataset_hold_obj(dp, obj, FTAG,
2990 if (snap->ds_dir != origin_head->ds_dir)
2991 error = SET_ERROR(EINVAL);
2993 error = dsl_destroy_snapshot_check_impl(
2996 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
2997 dsl_dataset_rele(snap, FTAG);
3002 dsl_dataset_rele(origin_head, FTAG);
3006 error = dsl_dataset_clone_swap_check_impl(drc->drc_ds,
3007 origin_head, drc->drc_force, drc->drc_owner, tx);
3009 dsl_dataset_rele(origin_head, FTAG);
3012 error = dsl_dataset_snapshot_check_impl(origin_head,
3013 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3014 dsl_dataset_rele(origin_head, FTAG);
3018 error = dsl_destroy_head_check_impl(drc->drc_ds, 1);
3020 error = dsl_dataset_snapshot_check_impl(drc->drc_ds,
3021 drc->drc_tosnap, tx, B_TRUE, 1, drc->drc_cred);
3027 dmu_recv_end_sync(void *arg, dmu_tx_t *tx)
3029 dmu_recv_cookie_t *drc = arg;
3030 dsl_pool_t *dp = dmu_tx_pool(tx);
3032 spa_history_log_internal_ds(drc->drc_ds, "finish receiving",
3033 tx, "snap=%s", drc->drc_tosnap);
3035 if (!drc->drc_newfs) {
3036 dsl_dataset_t *origin_head;
3038 VERIFY0(dsl_dataset_hold(dp, drc->drc_tofs, FTAG,
3041 if (drc->drc_force) {
3043 * Destroy any snapshots of drc_tofs (origin_head)
3044 * after the origin (the snap before drc_ds).
3048 obj = dsl_dataset_phys(origin_head)->ds_prev_snap_obj;
3050 dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj) {
3051 dsl_dataset_t *snap;
3052 VERIFY0(dsl_dataset_hold_obj(dp, obj, FTAG,
3054 ASSERT3P(snap->ds_dir, ==, origin_head->ds_dir);
3055 obj = dsl_dataset_phys(snap)->ds_prev_snap_obj;
3056 dsl_destroy_snapshot_sync_impl(snap,
3058 dsl_dataset_rele(snap, FTAG);
3061 VERIFY3P(drc->drc_ds->ds_prev, ==,
3062 origin_head->ds_prev);
3064 dsl_dataset_clone_swap_sync_impl(drc->drc_ds,
3066 dsl_dataset_snapshot_sync_impl(origin_head,
3067 drc->drc_tosnap, tx);
3069 /* set snapshot's creation time and guid */
3070 dmu_buf_will_dirty(origin_head->ds_prev->ds_dbuf, tx);
3071 dsl_dataset_phys(origin_head->ds_prev)->ds_creation_time =
3072 drc->drc_drrb->drr_creation_time;
3073 dsl_dataset_phys(origin_head->ds_prev)->ds_guid =
3074 drc->drc_drrb->drr_toguid;
3075 dsl_dataset_phys(origin_head->ds_prev)->ds_flags &=
3076 ~DS_FLAG_INCONSISTENT;
3078 dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
3079 dsl_dataset_phys(origin_head)->ds_flags &=
3080 ~DS_FLAG_INCONSISTENT;
3082 dsl_dataset_rele(origin_head, FTAG);
3083 dsl_destroy_head_sync_impl(drc->drc_ds, tx);
3085 if (drc->drc_owner != NULL)
3086 VERIFY3P(origin_head->ds_owner, ==, drc->drc_owner);
3088 dsl_dataset_t *ds = drc->drc_ds;
3090 dsl_dataset_snapshot_sync_impl(ds, drc->drc_tosnap, tx);
3092 /* set snapshot's creation time and guid */
3093 dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
3094 dsl_dataset_phys(ds->ds_prev)->ds_creation_time =
3095 drc->drc_drrb->drr_creation_time;
3096 dsl_dataset_phys(ds->ds_prev)->ds_guid =
3097 drc->drc_drrb->drr_toguid;
3098 dsl_dataset_phys(ds->ds_prev)->ds_flags &=
3099 ~DS_FLAG_INCONSISTENT;
3101 dmu_buf_will_dirty(ds->ds_dbuf, tx);
3102 dsl_dataset_phys(ds)->ds_flags &= ~DS_FLAG_INCONSISTENT;
3103 if (dsl_dataset_has_resume_receive_state(ds)) {
3104 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3105 DS_FIELD_RESUME_FROMGUID, tx);
3106 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3107 DS_FIELD_RESUME_OBJECT, tx);
3108 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3109 DS_FIELD_RESUME_OFFSET, tx);
3110 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3111 DS_FIELD_RESUME_BYTES, tx);
3112 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3113 DS_FIELD_RESUME_TOGUID, tx);
3114 (void) zap_remove(dp->dp_meta_objset, ds->ds_object,
3115 DS_FIELD_RESUME_TONAME, tx);
3118 drc->drc_newsnapobj = dsl_dataset_phys(drc->drc_ds)->ds_prev_snap_obj;
3120 * Release the hold from dmu_recv_begin. This must be done before
3121 * we return to open context, so that when we free the dataset's dnode,
3122 * we can evict its bonus buffer.
3124 dsl_dataset_disown(drc->drc_ds, dmu_recv_tag);
3129 add_ds_to_guidmap(const char *name, avl_tree_t *guid_map, uint64_t snapobj)
3132 dsl_dataset_t *snapds;
3133 guid_map_entry_t *gmep;
3136 ASSERT(guid_map != NULL);
3138 err = dsl_pool_hold(name, FTAG, &dp);
3141 gmep = kmem_alloc(sizeof (*gmep), KM_SLEEP);
3142 err = dsl_dataset_hold_obj(dp, snapobj, gmep, &snapds);
3144 gmep->guid = dsl_dataset_phys(snapds)->ds_guid;
3145 gmep->gme_ds = snapds;
3146 avl_add(guid_map, gmep);
3147 dsl_dataset_long_hold(snapds, gmep);
3149 kmem_free(gmep, sizeof (*gmep));
3151 dsl_pool_rele(dp, FTAG);
3155 static int dmu_recv_end_modified_blocks = 3;
3158 dmu_recv_existing_end(dmu_recv_cookie_t *drc)
3161 char name[MAXNAMELEN];
3165 * We will be destroying the ds; make sure its origin is unmounted if
3168 dsl_dataset_name(drc->drc_ds, name);
3169 zfs_destroy_unmount_origin(name);
3172 error = dsl_sync_task(drc->drc_tofs,
3173 dmu_recv_end_check, dmu_recv_end_sync, drc,
3174 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3177 dmu_recv_cleanup_ds(drc);
3182 dmu_recv_new_end(dmu_recv_cookie_t *drc)
3186 error = dsl_sync_task(drc->drc_tofs,
3187 dmu_recv_end_check, dmu_recv_end_sync, drc,
3188 dmu_recv_end_modified_blocks, ZFS_SPACE_CHECK_NORMAL);
3191 dmu_recv_cleanup_ds(drc);
3192 } else if (drc->drc_guid_to_ds_map != NULL) {
3193 (void) add_ds_to_guidmap(drc->drc_tofs,
3194 drc->drc_guid_to_ds_map,
3195 drc->drc_newsnapobj);
3201 dmu_recv_end(dmu_recv_cookie_t *drc, void *owner)
3203 drc->drc_owner = owner;
3206 return (dmu_recv_new_end(drc));
3208 return (dmu_recv_existing_end(drc));
3212 * Return TRUE if this objset is currently being received into.
3215 dmu_objset_is_receiving(objset_t *os)
3217 return (os->os_dsl_dataset != NULL &&
3218 os->os_dsl_dataset->ds_owner == dmu_recv_tag);