hw/arm/aspeed_ast10x0: Add various unimplemented peripherals
[qemu/kevin.git] / block / blkreplay.c
blob16543f585ad02264c414a38c8023a24c8cd3b446
1 /*
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/module.h"
14 #include "block/block-io.h"
15 #include "block/block_int.h"
16 #include "sysemu/replay.h"
17 #include "qapi/error.h"
19 typedef struct Request {
20 Coroutine *co;
21 QEMUBH *bh;
22 } Request;
24 static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,
25 Error **errp)
27 int ret;
29 /* Open the image file */
30 ret = bdrv_open_file_child(NULL, options, "image", bs, errp);
31 if (ret < 0) {
32 goto fail;
35 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
36 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
38 ret = 0;
39 fail:
40 return ret;
43 static int64_t coroutine_fn blkreplay_co_getlength(BlockDriverState *bs)
45 return bdrv_co_getlength(bs->file->bs);
48 /* This bh is used for synchronization of return from coroutines.
49 It continues yielded coroutine which then finishes its execution.
50 BH is called adjusted to some replay checkpoint, therefore
51 record and replay will always finish coroutines deterministically.
53 static void blkreplay_bh_cb(void *opaque)
55 Request *req = opaque;
56 aio_co_wake(req->co);
57 qemu_bh_delete(req->bh);
58 g_free(req);
61 static void block_request_create(uint64_t reqid, BlockDriverState *bs,
62 Coroutine *co)
64 Request *req = g_new(Request, 1);
65 *req = (Request) {
66 .co = co,
67 .bh = aio_bh_new(bdrv_get_aio_context(bs), blkreplay_bh_cb, req),
69 replay_block_event(req->bh, reqid);
72 static int coroutine_fn blkreplay_co_preadv(BlockDriverState *bs,
73 int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags)
75 uint64_t reqid = blkreplay_next_id();
76 int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
77 block_request_create(reqid, bs, qemu_coroutine_self());
78 qemu_coroutine_yield();
80 return ret;
83 static int coroutine_fn blkreplay_co_pwritev(BlockDriverState *bs,
84 int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags)
86 uint64_t reqid = blkreplay_next_id();
87 int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
88 block_request_create(reqid, bs, qemu_coroutine_self());
89 qemu_coroutine_yield();
91 return ret;
94 static int coroutine_fn blkreplay_co_pwrite_zeroes(BlockDriverState *bs,
95 int64_t offset, int64_t bytes, BdrvRequestFlags flags)
97 uint64_t reqid = blkreplay_next_id();
98 int ret = bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
99 block_request_create(reqid, bs, qemu_coroutine_self());
100 qemu_coroutine_yield();
102 return ret;
105 static int coroutine_fn blkreplay_co_pdiscard(BlockDriverState *bs,
106 int64_t offset, int64_t bytes)
108 uint64_t reqid = blkreplay_next_id();
109 int ret = bdrv_co_pdiscard(bs->file, offset, bytes);
110 block_request_create(reqid, bs, qemu_coroutine_self());
111 qemu_coroutine_yield();
113 return ret;
116 static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs)
118 uint64_t reqid = blkreplay_next_id();
119 int ret = bdrv_co_flush(bs->file->bs);
120 block_request_create(reqid, bs, qemu_coroutine_self());
121 qemu_coroutine_yield();
123 return ret;
126 static int blkreplay_snapshot_goto(BlockDriverState *bs,
127 const char *snapshot_id)
129 return bdrv_snapshot_goto(bs->file->bs, snapshot_id, NULL);
132 static BlockDriver bdrv_blkreplay = {
133 .format_name = "blkreplay",
134 .instance_size = 0,
135 .is_filter = true,
137 .bdrv_open = blkreplay_open,
138 .bdrv_child_perm = bdrv_default_perms,
139 .bdrv_co_getlength = blkreplay_co_getlength,
141 .bdrv_co_preadv = blkreplay_co_preadv,
142 .bdrv_co_pwritev = blkreplay_co_pwritev,
144 .bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes,
145 .bdrv_co_pdiscard = blkreplay_co_pdiscard,
146 .bdrv_co_flush = blkreplay_co_flush,
148 .bdrv_snapshot_goto = blkreplay_snapshot_goto,
151 static void bdrv_blkreplay_init(void)
153 bdrv_register(&bdrv_blkreplay);
156 block_init(bdrv_blkreplay_init);