2 * Block protocol for record/replay
4 * Copyright (c) 2010-2016 Institute for System Programming
5 * of the Russian Academy of Sciences.
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.
12 #include "qemu/osdep.h"
13 #include "qemu/module.h"
14 #include "block/block_int.h"
15 #include "sysemu/replay.h"
16 #include "qapi/error.h"
18 typedef struct Request
{
23 static int blkreplay_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
28 /* Open the image file */
29 bs
->file
= bdrv_open_child(NULL
, options
, "image", bs
, &child_of_bds
,
30 BDRV_CHILD_FILTERED
| BDRV_CHILD_PRIMARY
,
37 bs
->supported_write_flags
= BDRV_REQ_WRITE_UNCHANGED
;
38 bs
->supported_zero_flags
= BDRV_REQ_WRITE_UNCHANGED
;
45 static int64_t blkreplay_getlength(BlockDriverState
*bs
)
47 return bdrv_getlength(bs
->file
->bs
);
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.
55 static void blkreplay_bh_cb(void *opaque
)
57 Request
*req
= opaque
;
59 qemu_bh_delete(req
->bh
);
63 static void block_request_create(uint64_t reqid
, BlockDriverState
*bs
,
66 Request
*req
= g_new(Request
, 1);
69 .bh
= aio_bh_new(bdrv_get_aio_context(bs
), blkreplay_bh_cb
, req
),
71 replay_block_event(req
->bh
, reqid
);
74 static int coroutine_fn
blkreplay_co_preadv(BlockDriverState
*bs
,
75 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
77 uint64_t reqid
= blkreplay_next_id();
78 int ret
= bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
79 block_request_create(reqid
, bs
, qemu_coroutine_self());
80 qemu_coroutine_yield();
85 static int coroutine_fn
blkreplay_co_pwritev(BlockDriverState
*bs
,
86 int64_t offset
, int64_t bytes
, QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
88 uint64_t reqid
= blkreplay_next_id();
89 int ret
= bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
, flags
);
90 block_request_create(reqid
, bs
, qemu_coroutine_self());
91 qemu_coroutine_yield();
96 static int coroutine_fn
blkreplay_co_pwrite_zeroes(BlockDriverState
*bs
,
97 int64_t offset
, int64_t bytes
, BdrvRequestFlags flags
)
99 uint64_t reqid
= blkreplay_next_id();
100 int ret
= bdrv_co_pwrite_zeroes(bs
->file
, offset
, bytes
, flags
);
101 block_request_create(reqid
, bs
, qemu_coroutine_self());
102 qemu_coroutine_yield();
107 static int coroutine_fn
blkreplay_co_pdiscard(BlockDriverState
*bs
,
108 int64_t offset
, int64_t bytes
)
110 uint64_t reqid
= blkreplay_next_id();
111 int ret
= bdrv_co_pdiscard(bs
->file
, offset
, bytes
);
112 block_request_create(reqid
, bs
, qemu_coroutine_self());
113 qemu_coroutine_yield();
118 static int coroutine_fn
blkreplay_co_flush(BlockDriverState
*bs
)
120 uint64_t reqid
= blkreplay_next_id();
121 int ret
= bdrv_co_flush(bs
->file
->bs
);
122 block_request_create(reqid
, bs
, qemu_coroutine_self());
123 qemu_coroutine_yield();
128 static int blkreplay_snapshot_goto(BlockDriverState
*bs
,
129 const char *snapshot_id
)
131 return bdrv_snapshot_goto(bs
->file
->bs
, snapshot_id
, NULL
);
134 static BlockDriver bdrv_blkreplay
= {
135 .format_name
= "blkreplay",
139 .bdrv_open
= blkreplay_open
,
140 .bdrv_child_perm
= bdrv_default_perms
,
141 .bdrv_getlength
= blkreplay_getlength
,
143 .bdrv_co_preadv
= blkreplay_co_preadv
,
144 .bdrv_co_pwritev
= blkreplay_co_pwritev
,
146 .bdrv_co_pwrite_zeroes
= blkreplay_co_pwrite_zeroes
,
147 .bdrv_co_pdiscard
= blkreplay_co_pdiscard
,
148 .bdrv_co_flush
= blkreplay_co_flush
,
150 .bdrv_snapshot_goto
= blkreplay_snapshot_goto
,
153 static void bdrv_blkreplay_init(void)
155 bdrv_register(&bdrv_blkreplay
);
158 block_init(bdrv_blkreplay_init
);