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-common.h"
14 #include "block/block_int.h"
15 #include "sysemu/replay.h"
16 #include "qapi/error.h"
18 typedef struct Request
{
24 This counter is global, because requests from different
25 block devices should not get overlapping ids. */
26 static uint64_t request_id
;
28 static int blkreplay_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
31 Error
*local_err
= NULL
;
34 /* Open the image file */
35 bs
->file
= bdrv_open_child(NULL
, options
, "image",
36 bs
, &child_file
, false, &local_err
);
39 error_propagate(errp
, local_err
);
46 bdrv_unref_child(bs
, bs
->file
);
51 static void blkreplay_close(BlockDriverState
*bs
)
55 static int64_t blkreplay_getlength(BlockDriverState
*bs
)
57 return bdrv_getlength(bs
->file
->bs
);
60 /* This bh is used for synchronization of return from coroutines.
61 It continues yielded coroutine which then finishes its execution.
62 BH is called adjusted to some replay checkpoint, therefore
63 record and replay will always finish coroutines deterministically.
65 static void blkreplay_bh_cb(void *opaque
)
67 Request
*req
= opaque
;
68 qemu_coroutine_enter(req
->co
);
69 qemu_bh_delete(req
->bh
);
73 static void block_request_create(uint64_t reqid
, BlockDriverState
*bs
,
76 Request
*req
= g_new(Request
, 1);
79 .bh
= aio_bh_new(bdrv_get_aio_context(bs
), blkreplay_bh_cb
, req
),
81 replay_block_event(req
->bh
, reqid
);
84 static int coroutine_fn
blkreplay_co_preadv(BlockDriverState
*bs
,
85 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
87 uint64_t reqid
= request_id
++;
88 int ret
= bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
89 block_request_create(reqid
, bs
, qemu_coroutine_self());
90 qemu_coroutine_yield();
95 static int coroutine_fn
blkreplay_co_pwritev(BlockDriverState
*bs
,
96 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
98 uint64_t reqid
= request_id
++;
99 int ret
= bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
, flags
);
100 block_request_create(reqid
, bs
, qemu_coroutine_self());
101 qemu_coroutine_yield();
106 static int coroutine_fn
blkreplay_co_pwrite_zeroes(BlockDriverState
*bs
,
107 int64_t offset
, int count
, BdrvRequestFlags flags
)
109 uint64_t reqid
= request_id
++;
110 int ret
= bdrv_co_pwrite_zeroes(bs
->file
, offset
, count
, flags
);
111 block_request_create(reqid
, bs
, qemu_coroutine_self());
112 qemu_coroutine_yield();
117 static int coroutine_fn
blkreplay_co_discard(BlockDriverState
*bs
,
118 int64_t sector_num
, int nb_sectors
)
120 uint64_t reqid
= request_id
++;
121 int ret
= bdrv_co_discard(bs
->file
->bs
, sector_num
, nb_sectors
);
122 block_request_create(reqid
, bs
, qemu_coroutine_self());
123 qemu_coroutine_yield();
128 static int coroutine_fn
blkreplay_co_flush(BlockDriverState
*bs
)
130 uint64_t reqid
= request_id
++;
131 int ret
= bdrv_co_flush(bs
->file
->bs
);
132 block_request_create(reqid
, bs
, qemu_coroutine_self());
133 qemu_coroutine_yield();
138 static BlockDriver bdrv_blkreplay
= {
139 .format_name
= "blkreplay",
140 .protocol_name
= "blkreplay",
143 .bdrv_file_open
= blkreplay_open
,
144 .bdrv_close
= blkreplay_close
,
145 .bdrv_getlength
= blkreplay_getlength
,
147 .bdrv_co_preadv
= blkreplay_co_preadv
,
148 .bdrv_co_pwritev
= blkreplay_co_pwritev
,
150 .bdrv_co_pwrite_zeroes
= blkreplay_co_pwrite_zeroes
,
151 .bdrv_co_discard
= blkreplay_co_discard
,
152 .bdrv_co_flush
= blkreplay_co_flush
,
155 static void bdrv_blkreplay_init(void)
157 bdrv_register(&bdrv_blkreplay
);
160 block_init(bdrv_blkreplay_init
);