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"
|
|
|
|
#include "qemu-common.h"
|
|
|
|
#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)
|
|
|
|
{
|
|
|
|
Error *local_err = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Open the image file */
|
|
|
|
bs->file = bdrv_open_child(NULL, options, "image",
|
|
|
|
bs, &child_file, false, &local_err);
|
|
|
|
if (local_err) {
|
|
|
|
ret = -EINVAL;
|
|
|
|
error_propagate(errp, local_err);
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
fail:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void blkreplay_close(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static int64_t blkreplay_getlength(BlockDriverState *bs)
|
|
|
|
{
|
|
|
|
return bdrv_getlength(bs->file->bs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
}
|
|
|
|
|
2016-05-30 14:31:59 +02:00
|
|
|
static int coroutine_fn blkreplay_co_preadv(BlockDriverState *bs,
|
|
|
|
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int 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;
|
|
|
|
}
|
|
|
|
|
2016-05-30 14:31:59 +02:00
|
|
|
static int coroutine_fn blkreplay_co_pwritev(BlockDriverState *bs,
|
|
|
|
uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int 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;
|
|
|
|
}
|
|
|
|
|
2016-06-01 23:10:07 +02:00
|
|
|
static int coroutine_fn blkreplay_co_pwrite_zeroes(BlockDriverState *bs,
|
2017-06-09 12:18:08 +02:00
|
|
|
int64_t offset, int 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;
|
|
|
|
}
|
|
|
|
|
2016-07-16 01:22:59 +02:00
|
|
|
static int coroutine_fn blkreplay_co_pdiscard(BlockDriverState *bs,
|
2017-06-09 12:18:08 +02:00
|
|
|
int64_t offset, int bytes)
|
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_pdiscard(bs->file->bs, offset, bytes);
|
2016-03-14 08:45:10 +01:00
|
|
|
block_request_create(reqid, bs, qemu_coroutine_self());
|
|
|
|
qemu_coroutine_yield();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs)
|
|
|
|
{
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
static BlockDriver bdrv_blkreplay = {
|
|
|
|
.format_name = "blkreplay",
|
|
|
|
.protocol_name = "blkreplay",
|
|
|
|
.instance_size = 0,
|
|
|
|
|
|
|
|
.bdrv_file_open = blkreplay_open,
|
|
|
|
.bdrv_close = blkreplay_close,
|
2016-12-15 12:28:58 +01:00
|
|
|
.bdrv_child_perm = bdrv_filter_default_perms,
|
2016-03-14 08:45:10 +01:00
|
|
|
.bdrv_getlength = blkreplay_getlength,
|
|
|
|
|
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,
|
|
|
|
};
|
|
|
|
|
|
|
|
static void bdrv_blkreplay_init(void)
|
|
|
|
{
|
|
|
|
bdrv_register(&bdrv_blkreplay);
|
|
|
|
}
|
|
|
|
|
|
|
|
block_init(bdrv_blkreplay_init);
|