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
{
23 static int blkreplay_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
26 Error
*local_err
= NULL
;
29 /* Open the image file */
30 bs
->file
= bdrv_open_child(NULL
, options
, "image",
31 bs
, &child_file
, false, &local_err
);
34 error_propagate(errp
, local_err
);
43 static void blkreplay_close(BlockDriverState
*bs
)
47 static int64_t blkreplay_getlength(BlockDriverState
*bs
)
49 return bdrv_getlength(bs
->file
->bs
);
52 /* This bh is used for synchronization of return from coroutines.
53 It continues yielded coroutine which then finishes its execution.
54 BH is called adjusted to some replay checkpoint, therefore
55 record and replay will always finish coroutines deterministically.
57 static void blkreplay_bh_cb(void *opaque
)
59 Request
*req
= opaque
;
61 qemu_bh_delete(req
->bh
);
65 static void block_request_create(uint64_t reqid
, BlockDriverState
*bs
,
68 Request
*req
= g_new(Request
, 1);
71 .bh
= aio_bh_new(bdrv_get_aio_context(bs
), blkreplay_bh_cb
, req
),
73 replay_block_event(req
->bh
, reqid
);
76 static int coroutine_fn
blkreplay_co_preadv(BlockDriverState
*bs
,
77 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
79 uint64_t reqid
= blkreplay_next_id();
80 int ret
= bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
81 block_request_create(reqid
, bs
, qemu_coroutine_self());
82 qemu_coroutine_yield();
87 static int coroutine_fn
blkreplay_co_pwritev(BlockDriverState
*bs
,
88 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
)
90 uint64_t reqid
= blkreplay_next_id();
91 int ret
= bdrv_co_pwritev(bs
->file
, offset
, bytes
, qiov
, flags
);
92 block_request_create(reqid
, bs
, qemu_coroutine_self());
93 qemu_coroutine_yield();
98 static int coroutine_fn
blkreplay_co_pwrite_zeroes(BlockDriverState
*bs
,
99 int64_t offset
, int bytes
, BdrvRequestFlags flags
)
101 uint64_t reqid
= blkreplay_next_id();
102 int ret
= bdrv_co_pwrite_zeroes(bs
->file
, offset
, bytes
, flags
);
103 block_request_create(reqid
, bs
, qemu_coroutine_self());
104 qemu_coroutine_yield();
109 static int coroutine_fn
blkreplay_co_pdiscard(BlockDriverState
*bs
,
110 int64_t offset
, int bytes
)
112 uint64_t reqid
= blkreplay_next_id();
113 int ret
= bdrv_co_pdiscard(bs
->file
->bs
, offset
, bytes
);
114 block_request_create(reqid
, bs
, qemu_coroutine_self());
115 qemu_coroutine_yield();
120 static int coroutine_fn
blkreplay_co_flush(BlockDriverState
*bs
)
122 uint64_t reqid
= blkreplay_next_id();
123 int ret
= bdrv_co_flush(bs
->file
->bs
);
124 block_request_create(reqid
, bs
, qemu_coroutine_self());
125 qemu_coroutine_yield();
130 static BlockDriver bdrv_blkreplay
= {
131 .format_name
= "blkreplay",
134 .bdrv_open
= blkreplay_open
,
135 .bdrv_close
= blkreplay_close
,
136 .bdrv_child_perm
= bdrv_filter_default_perms
,
137 .bdrv_getlength
= blkreplay_getlength
,
139 .bdrv_co_preadv
= blkreplay_co_preadv
,
140 .bdrv_co_pwritev
= blkreplay_co_pwritev
,
142 .bdrv_co_pwrite_zeroes
= blkreplay_co_pwrite_zeroes
,
143 .bdrv_co_pdiscard
= blkreplay_co_pdiscard
,
144 .bdrv_co_flush
= blkreplay_co_flush
,
147 static void bdrv_blkreplay_init(void)
149 bdrv_register(&bdrv_blkreplay
);
152 block_init(bdrv_blkreplay_init
);