blob: 4a247752fd8ffa5717c814e693d125b60861c201 [file] [log] [blame]
Pavel Dovgalyuk63785672016-03-14 10:45:10 +03001/*
2 * Block protocol for record/replay
3 *
4 * Copyright (c) 2010-2016 Institute for System Programming
5 * of the Russian Academy of Sciences.
6 *
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
9 *
10 */
11
12#include "qemu/osdep.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020013#include "qemu/module.h"
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030014#include "block/block_int.h"
15#include "sysemu/replay.h"
16#include "qapi/error.h"
17
18typedef struct Request {
19 Coroutine *co;
20 QEMUBH *bh;
21} Request;
22
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030023static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,
24 Error **errp)
25{
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030026 int ret;
27
28 /* Open the image file */
Max Reitzb3af2af2020-05-13 13:05:36 +020029 bs->file = bdrv_open_child(NULL, options, "image", bs, &child_of_bds,
30 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY,
Vladimir Sementsov-Ogievskiybc520242021-02-02 15:49:45 +030031 false, errp);
32 if (!bs->file) {
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030033 ret = -EINVAL;
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030034 goto fail;
35 }
36
Max Reitz228345b2018-04-21 15:29:26 +020037 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
38 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
39
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030040 ret = 0;
41fail:
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030042 return ret;
43}
44
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030045static int64_t blkreplay_getlength(BlockDriverState *bs)
46{
47 return bdrv_getlength(bs->file->bs);
48}
49
50/* This bh is used for synchronization of return from coroutines.
51 It continues yielded coroutine which then finishes its execution.
52 BH is called adjusted to some replay checkpoint, therefore
53 record and replay will always finish coroutines deterministically.
54*/
55static void blkreplay_bh_cb(void *opaque)
56{
57 Request *req = opaque;
Paolo Bonzini19196312017-02-13 14:52:31 +010058 aio_co_wake(req->co);
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030059 qemu_bh_delete(req->bh);
60 g_free(req);
61}
62
63static void block_request_create(uint64_t reqid, BlockDriverState *bs,
64 Coroutine *co)
65{
66 Request *req = g_new(Request, 1);
67 *req = (Request) {
68 .co = co,
69 .bh = aio_bh_new(bdrv_get_aio_context(bs), blkreplay_bh_cb, req),
70 };
71 replay_block_event(req->bh, reqid);
72}
73
Kevin Wolfe858a972016-05-30 14:31:59 +020074static int coroutine_fn blkreplay_co_preadv(BlockDriverState *bs,
75 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030076{
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +030077 uint64_t reqid = blkreplay_next_id();
Kevin Wolfa03ef882016-06-20 21:31:46 +020078 int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030079 block_request_create(reqid, bs, qemu_coroutine_self());
80 qemu_coroutine_yield();
81
82 return ret;
83}
84
Kevin Wolfe858a972016-05-30 14:31:59 +020085static int coroutine_fn blkreplay_co_pwritev(BlockDriverState *bs,
86 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags)
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030087{
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +030088 uint64_t reqid = blkreplay_next_id();
Kevin Wolfa03ef882016-06-20 21:31:46 +020089 int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030090 block_request_create(reqid, bs, qemu_coroutine_self());
91 qemu_coroutine_yield();
92
93 return ret;
94}
95
Eric Blake9c21a422016-06-01 15:10:07 -060096static int coroutine_fn blkreplay_co_pwrite_zeroes(BlockDriverState *bs,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +030097 int64_t offset, int bytes, BdrvRequestFlags flags)
Pavel Dovgalyuk63785672016-03-14 10:45:10 +030098{
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +030099 uint64_t reqid = blkreplay_next_id();
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300100 int ret = bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300101 block_request_create(reqid, bs, qemu_coroutine_self());
102 qemu_coroutine_yield();
103
104 return ret;
105}
106
Eric Blakeaba76e22016-07-15 17:22:59 -0600107static int coroutine_fn blkreplay_co_pdiscard(BlockDriverState *bs,
Manos Pitsidianakisf5a5ca72017-06-09 13:18:08 +0300108 int64_t offset, int bytes)
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300109{
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +0300110 uint64_t reqid = blkreplay_next_id();
Fam Zheng0b9fd3f2018-07-10 14:31:17 +0800111 int ret = bdrv_co_pdiscard(bs->file, offset, bytes);
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300112 block_request_create(reqid, bs, qemu_coroutine_self());
113 qemu_coroutine_yield();
114
115 return ret;
116}
117
118static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs)
119{
Pavel Dovgalyuk6d0ceb82016-09-26 11:08:16 +0300120 uint64_t reqid = blkreplay_next_id();
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300121 int ret = bdrv_co_flush(bs->file->bs);
122 block_request_create(reqid, bs, qemu_coroutine_self());
123 qemu_coroutine_yield();
124
125 return ret;
126}
127
Pavel Dovgalyuk3c6c4342019-09-17 14:57:51 +0300128static int blkreplay_snapshot_goto(BlockDriverState *bs,
129 const char *snapshot_id)
130{
131 return bdrv_snapshot_goto(bs->file->bs, snapshot_id, NULL);
132}
133
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300134static BlockDriver bdrv_blkreplay = {
135 .format_name = "blkreplay",
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300136 .instance_size = 0,
Max Reitz6540fd12020-05-13 13:05:11 +0200137 .is_filter = true,
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300138
Fabiano Rosas8140e782018-03-12 19:07:52 -0300139 .bdrv_open = blkreplay_open,
Max Reitz69dca432020-05-13 13:05:39 +0200140 .bdrv_child_perm = bdrv_default_perms,
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300141 .bdrv_getlength = blkreplay_getlength,
142
Kevin Wolfe858a972016-05-30 14:31:59 +0200143 .bdrv_co_preadv = blkreplay_co_preadv,
144 .bdrv_co_pwritev = blkreplay_co_pwritev,
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300145
Eric Blake9c21a422016-06-01 15:10:07 -0600146 .bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes,
Eric Blakeaba76e22016-07-15 17:22:59 -0600147 .bdrv_co_pdiscard = blkreplay_co_pdiscard,
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300148 .bdrv_co_flush = blkreplay_co_flush,
Pavel Dovgalyuk3c6c4342019-09-17 14:57:51 +0300149
150 .bdrv_snapshot_goto = blkreplay_snapshot_goto,
Pavel Dovgalyuk63785672016-03-14 10:45:10 +0300151};
152
153static void bdrv_blkreplay_init(void)
154{
155 bdrv_register(&bdrv_blkreplay);
156}
157
158block_init(bdrv_blkreplay_init);