2016-03-14 10:45:10 +03:00
|
|
|
/*
|
|
|
|
* Block protocol for record/replay
|
|
|
|
*
|
|
|
|
* Copyright (c) 2010-2016 Institute for System Programming
|
|
|
|
* of the Russian Academy of Sciences.
|
|
|
|
*
|
|
|
|
* This work is licensed under the terms of the GNU GPL, version 2 or later.
|
|
|
|
* See the COPYING file in the top-level directory.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "qemu/osdep.h"
|
2019-05-23 17:35:07 +03:00
|
|
|
#include "qemu/module.h"
|
2022-12-21 16:35:49 +03:00
|
|
|
#include "block/block-io.h"
|
2016-03-14 10:45:10 +03:00
|
|
|
#include "block/block_int.h"
|
|
|
|
#include "sysemu/replay.h"
|
|
|
|
#include "qapi/error.h"
|
|
|
|
|
|
|
|
typedef struct Request {
|
|
|
|
Coroutine *co;
|
|
|
|
QEMUBH *bh;
|
|
|
|
} Request;
|
|
|
|
|
|
|
|
static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,
|
|
|
|
Error **errp)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Open the image file */
|
2022-07-26 23:11:21 +03:00
|
|
|
ret = bdrv_open_file_child(NULL, options, "image", bs, errp);
|
|
|
|
if (ret < 0) {
|
2016-03-14 10:45:10 +03:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-04-21 16:29:26 +03:00
|
|
|
bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
|
|
|
|
bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
|
|
|
|
|
2016-03-14 10:45:10 +03:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 18:22:02 +03:00
|
|
|
static int64_t coroutine_fn GRAPH_RDLOCK
|
|
|
|
blkreplay_co_getlength(BlockDriverState *bs)
|
2016-03-14 10:45:10 +03:00
|
|
|
{
|
2023-01-13 23:42:04 +03:00
|
|
|
return bdrv_co_getlength(bs->file->bs);
|
2016-03-14 10:45:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* This bh is used for synchronization of return from coroutines.
|
|
|
|
It continues yielded coroutine which then finishes its execution.
|
|
|
|
BH is called adjusted to some replay checkpoint, therefore
|
|
|
|
record and replay will always finish coroutines deterministically.
|
|
|
|
*/
|
|
|
|
static void blkreplay_bh_cb(void *opaque)
|
|
|
|
{
|
|
|
|
Request *req = opaque;
|
2017-02-13 16:52:31 +03:00
|
|
|
aio_co_wake(req->co);
|
2016-03-14 10:45:10 +03:00
|
|
|
qemu_bh_delete(req->bh);
|
|
|
|
g_free(req);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void block_request_create(uint64_t reqid, BlockDriverState *bs,
|
|
|
|
Coroutine *co)
|
|
|
|
{
|
|
|
|
Request *req = g_new(Request, 1);
|
|
|
|
*req = (Request) {
|
|
|
|
.co = co,
|
|
|
|
.bh = aio_bh_new(bdrv_get_aio_context(bs), blkreplay_bh_cb, req),
|
|
|
|
};
|
|
|
|
replay_block_event(req->bh, reqid);
|
|
|
|
}
|
|
|
|
|
2023-02-03 18:21:50 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
blkreplay_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
|
|
|
|
QEMUIOVector *qiov, BdrvRequestFlags flags)
|
2016-03-14 10:45:10 +03:00
|
|
|
{
|
2016-09-26 11:08:16 +03:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2016-06-20 22:31:46 +03:00
|
|
|
int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
|
2016-03-14 10:45:10 +03:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 18:21:50 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
blkreplay_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
|
|
|
|
QEMUIOVector *qiov, BdrvRequestFlags flags)
|
2016-03-14 10:45:10 +03:00
|
|
|
{
|
2016-09-26 11:08:16 +03:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2016-06-20 22:31:46 +03:00
|
|
|
int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
|
2016-03-14 10:45:10 +03:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 18:21:48 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
blkreplay_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
|
|
|
|
BdrvRequestFlags flags)
|
2016-03-14 10:45:10 +03:00
|
|
|
{
|
2016-09-26 11:08:16 +03:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2017-06-09 13:18:08 +03:00
|
|
|
int ret = bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
|
2016-03-14 10:45:10 +03:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 18:21:47 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
blkreplay_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
|
2016-03-14 10:45:10 +03:00
|
|
|
{
|
2016-09-26 11:08:16 +03:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2018-07-10 09:31:17 +03:00
|
|
|
int ret = bdrv_co_pdiscard(bs->file, offset, bytes);
|
2016-03-14 10:45:10 +03:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 18:21:46 +03:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK blkreplay_co_flush(BlockDriverState *bs)
|
2016-03-14 10:45:10 +03:00
|
|
|
{
|
2016-09-26 11:08:16 +03:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2016-03-14 10:45:10 +03:00
|
|
|
int ret = bdrv_co_flush(bs->file->bs);
|
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-17 14:57:51 +03:00
|
|
|
static int blkreplay_snapshot_goto(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id)
|
|
|
|
{
|
2023-10-27 18:53:33 +03:00
|
|
|
BlockDriverState *file_bs;
|
|
|
|
|
|
|
|
bdrv_graph_rdlock_main_loop();
|
|
|
|
file_bs = bs->file->bs;
|
|
|
|
bdrv_graph_rdunlock_main_loop();
|
|
|
|
|
|
|
|
return bdrv_snapshot_goto(file_bs, snapshot_id, NULL);
|
2019-09-17 14:57:51 +03:00
|
|
|
}
|
|
|
|
|
2016-03-14 10:45:10 +03:00
|
|
|
static BlockDriver bdrv_blkreplay = {
|
|
|
|
.format_name = "blkreplay",
|
|
|
|
.instance_size = 0,
|
2020-05-13 14:05:11 +03:00
|
|
|
.is_filter = true,
|
2016-03-14 10:45:10 +03:00
|
|
|
|
2018-03-13 01:07:52 +03:00
|
|
|
.bdrv_open = blkreplay_open,
|
2020-05-13 14:05:39 +03:00
|
|
|
.bdrv_child_perm = bdrv_default_perms,
|
2023-01-13 23:42:04 +03:00
|
|
|
.bdrv_co_getlength = blkreplay_co_getlength,
|
2016-03-14 10:45:10 +03:00
|
|
|
|
2016-05-30 15:31:59 +03:00
|
|
|
.bdrv_co_preadv = blkreplay_co_preadv,
|
|
|
|
.bdrv_co_pwritev = blkreplay_co_pwritev,
|
2016-03-14 10:45:10 +03:00
|
|
|
|
2016-06-02 00:10:07 +03:00
|
|
|
.bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes,
|
2016-07-16 02:22:59 +03:00
|
|
|
.bdrv_co_pdiscard = blkreplay_co_pdiscard,
|
2016-03-14 10:45:10 +03:00
|
|
|
.bdrv_co_flush = blkreplay_co_flush,
|
2019-09-17 14:57:51 +03:00
|
|
|
|
|
|
|
.bdrv_snapshot_goto = blkreplay_snapshot_goto,
|
2016-03-14 10:45:10 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_blkreplay_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_blkreplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_blkreplay_init);
|