2016-03-14 08:45:10 +01: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 16:35:07 +02:00
|
|
|
#include "qemu/module.h"
|
2022-12-21 14:35:49 +01:00
|
|
|
#include "block/block-io.h"
|
2016-03-14 08:45:10 +01: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 22:11:21 +02:00
|
|
|
ret = bdrv_open_file_child(NULL, options, "image", bs, errp);
|
|
|
|
if (ret < 0) {
|
2016-03-14 08:45:10 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2018-04-21 15:29:26 +02:00
|
|
|
bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
|
|
|
|
bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
|
|
|
|
|
2016-03-14 08:45:10 +01:00
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:22:02 +01:00
|
|
|
static int64_t coroutine_fn GRAPH_RDLOCK
|
|
|
|
blkreplay_co_getlength(BlockDriverState *bs)
|
2016-03-14 08:45:10 +01:00
|
|
|
{
|
2023-01-13 21:42:04 +01:00
|
|
|
return bdrv_co_getlength(bs->file->bs);
|
2016-03-14 08:45:10 +01: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 14:52:31 +01:00
|
|
|
aio_co_wake(req->co);
|
2016-03-14 08:45:10 +01: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 16:21:50 +01: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 08:45:10 +01:00
|
|
|
{
|
2016-09-26 10:08:16 +02:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2016-06-20 21:31:46 +02:00
|
|
|
int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
|
2016-03-14 08:45:10 +01:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:50 +01: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 08:45:10 +01:00
|
|
|
{
|
2016-09-26 10:08:16 +02:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2016-06-20 21:31:46 +02:00
|
|
|
int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
|
2016-03-14 08:45:10 +01:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:48 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
blkreplay_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
|
|
|
|
BdrvRequestFlags flags)
|
2016-03-14 08:45:10 +01:00
|
|
|
{
|
2016-09-26 10:08:16 +02:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2017-06-09 12:18:08 +02:00
|
|
|
int ret = bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
|
2016-03-14 08:45:10 +01:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:47 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK
|
|
|
|
blkreplay_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
|
2016-03-14 08:45:10 +01:00
|
|
|
{
|
2016-09-26 10:08:16 +02:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2018-07-10 08:31:17 +02:00
|
|
|
int ret = bdrv_co_pdiscard(bs->file, offset, bytes);
|
2016-03-14 08:45:10 +01:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2023-02-03 16:21:46 +01:00
|
|
|
static int coroutine_fn GRAPH_RDLOCK blkreplay_co_flush(BlockDriverState *bs)
|
2016-03-14 08:45:10 +01:00
|
|
|
{
|
2016-09-26 10:08:16 +02:00
|
|
|
uint64_t reqid = blkreplay_next_id();
|
2016-03-14 08:45:10 +01: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 13:57:51 +02:00
|
|
|
static int blkreplay_snapshot_goto(BlockDriverState *bs,
|
|
|
|
const char *snapshot_id)
|
|
|
|
{
|
|
|
|
return bdrv_snapshot_goto(bs->file->bs, snapshot_id, NULL);
|
|
|
|
}
|
|
|
|
|
2016-03-14 08:45:10 +01:00
|
|
|
static BlockDriver bdrv_blkreplay = {
|
|
|
|
.format_name = "blkreplay",
|
|
|
|
.instance_size = 0,
|
2020-05-13 13:05:11 +02:00
|
|
|
.is_filter = true,
|
2016-03-14 08:45:10 +01:00
|
|
|
|
2018-03-12 23:07:52 +01:00
|
|
|
.bdrv_open = blkreplay_open,
|
2020-05-13 13:05:39 +02:00
|
|
|
.bdrv_child_perm = bdrv_default_perms,
|
2023-01-13 21:42:04 +01:00
|
|
|
.bdrv_co_getlength = blkreplay_co_getlength,
|
2016-03-14 08:45:10 +01:00
|
|
|
|
2016-05-30 14:31:59 +02:00
|
|
|
.bdrv_co_preadv = blkreplay_co_preadv,
|
|
|
|
.bdrv_co_pwritev = blkreplay_co_pwritev,
|
2016-03-14 08:45:10 +01:00
|
|
|
|
2016-06-01 23:10:07 +02:00
|
|
|
.bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes,
|
2016-07-16 01:22:59 +02:00
|
|
|
.bdrv_co_pdiscard = blkreplay_co_pdiscard,
|
2016-03-14 08:45:10 +01:00
|
|
|
.bdrv_co_flush = blkreplay_co_flush,
|
2019-09-17 13:57:51 +02:00
|
|
|
|
|
|
|
.bdrv_snapshot_goto = blkreplay_snapshot_goto,
|
2016-03-14 08:45:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_blkreplay_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_blkreplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_blkreplay_init);
|