trace: split out trace events for hw/nvram/ directory
[qemu/kevin.git] / block / blkreplay.c
blob525c2d54e0b46a58855fd9adc3783c3ac480415e
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-common.h"
14 #include "block/block_int.h"
15 #include "sysemu/replay.h"
16 #include "qapi/error.h"
18 typedef struct Request {
19 Coroutine *co;
20 QEMUBH *bh;
21 } Request;
23 /* Next request id.
24 This counter is global, because requests from different
25 block devices should not get overlapping ids. */
26 static uint64_t request_id;
28 static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,
29 Error **errp)
31 Error *local_err = NULL;
32 int ret;
34 /* Open the image file */
35 bs->file = bdrv_open_child(NULL, options, "image",
36 bs, &child_file, false, &local_err);
37 if (local_err) {
38 ret = -EINVAL;
39 error_propagate(errp, local_err);
40 goto fail;
43 ret = 0;
44 fail:
45 if (ret < 0) {
46 bdrv_unref_child(bs, bs->file);
48 return ret;
51 static void blkreplay_close(BlockDriverState *bs)
55 static int64_t blkreplay_getlength(BlockDriverState *bs)
57 return bdrv_getlength(bs->file->bs);
60 /* This bh is used for synchronization of return from coroutines.
61 It continues yielded coroutine which then finishes its execution.
62 BH is called adjusted to some replay checkpoint, therefore
63 record and replay will always finish coroutines deterministically.
65 static void blkreplay_bh_cb(void *opaque)
67 Request *req = opaque;
68 qemu_coroutine_enter(req->co, NULL);
69 qemu_bh_delete(req->bh);
70 g_free(req);
73 static void block_request_create(uint64_t reqid, BlockDriverState *bs,
74 Coroutine *co)
76 Request *req = g_new(Request, 1);
77 *req = (Request) {
78 .co = co,
79 .bh = aio_bh_new(bdrv_get_aio_context(bs), blkreplay_bh_cb, req),
81 replay_block_event(req->bh, reqid);
84 static int coroutine_fn blkreplay_co_readv(BlockDriverState *bs,
85 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
87 uint64_t reqid = request_id++;
88 int ret = bdrv_co_readv(bs->file->bs, sector_num, nb_sectors, qiov);
89 block_request_create(reqid, bs, qemu_coroutine_self());
90 qemu_coroutine_yield();
92 return ret;
95 static int coroutine_fn blkreplay_co_writev(BlockDriverState *bs,
96 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
98 uint64_t reqid = request_id++;
99 int ret = bdrv_co_writev(bs->file->bs, sector_num, nb_sectors, qiov);
100 block_request_create(reqid, bs, qemu_coroutine_self());
101 qemu_coroutine_yield();
103 return ret;
106 static int coroutine_fn blkreplay_co_pwrite_zeroes(BlockDriverState *bs,
107 int64_t offset, int count, BdrvRequestFlags flags)
109 uint64_t reqid = request_id++;
110 int ret = bdrv_co_pwrite_zeroes(bs->file->bs, offset, count, flags);
111 block_request_create(reqid, bs, qemu_coroutine_self());
112 qemu_coroutine_yield();
114 return ret;
117 static int coroutine_fn blkreplay_co_discard(BlockDriverState *bs,
118 int64_t sector_num, int nb_sectors)
120 uint64_t reqid = request_id++;
121 int ret = bdrv_co_discard(bs->file->bs, sector_num, nb_sectors);
122 block_request_create(reqid, bs, qemu_coroutine_self());
123 qemu_coroutine_yield();
125 return ret;
128 static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs)
130 uint64_t reqid = request_id++;
131 int ret = bdrv_co_flush(bs->file->bs);
132 block_request_create(reqid, bs, qemu_coroutine_self());
133 qemu_coroutine_yield();
135 return ret;
138 static BlockDriver bdrv_blkreplay = {
139 .format_name = "blkreplay",
140 .protocol_name = "blkreplay",
141 .instance_size = 0,
143 .bdrv_file_open = blkreplay_open,
144 .bdrv_close = blkreplay_close,
145 .bdrv_getlength = blkreplay_getlength,
147 .bdrv_co_readv = blkreplay_co_readv,
148 .bdrv_co_writev = blkreplay_co_writev,
150 .bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes,
151 .bdrv_co_discard = blkreplay_co_discard,
152 .bdrv_co_flush = blkreplay_co_flush,
155 static void bdrv_blkreplay_init(void)
157 bdrv_register(&bdrv_blkreplay);
160 block_init(bdrv_blkreplay_init);