master
c 167 lines 4.59 KB
Raw
1 /*
2 * Block protocol for record/replay
3 *
4 * Copyright (c) 2010-2016 Institute for System Programming
5 * of the Russian Academy of Sciences.
6 *
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.
9 *
10 */
11
12 #include "qemu/osdep.h"
13 #include "qemu/module.h"
14 #include "block/block-io.h"
15 #include "block/block_int.h"
16 #include "system/replay.h"
17 #include "qapi/error.h"
18
19 typedef struct Request {
20 Coroutine *co;
21 QEMUBH *bh;
22 } Request;
23
24 static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,
25 Error **errp)
26 {
27 int ret;
28
29 /* Open the image file */
30 ret = bdrv_open_file_child(NULL, options, "image", bs, errp);
31 if (ret < 0) {
32 goto fail;
33 }
34
35 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
36 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
37
38 ret = 0;
39 fail:
40 return ret;
41 }
42
43 static int64_t coroutine_fn GRAPH_RDLOCK
44 blkreplay_co_getlength(BlockDriverState *bs)
45 {
46 return bdrv_co_getlength(bs->file->bs);
47 }
48
49 /* This bh is used for synchronization of return from coroutines.
50 It continues yielded coroutine which then finishes its execution.
51 BH is called adjusted to some replay checkpoint, therefore
52 record and replay will always finish coroutines deterministically.
53 */
54 static void blkreplay_bh_cb(void *opaque)
55 {
56 Request *req = opaque;
57 aio_co_wake(req->co);
58 qemu_bh_delete(req->bh);
59 g_free(req);
60 }
61
62 static void block_request_create(uint64_t reqid, BlockDriverState *bs,
63 Coroutine *co)
64 {
65 Request *req = g_new(Request, 1);
66 AioContext *ctx = qemu_coroutine_get_aio_context(co);
67 *req = (Request) {
68 .co = co,
69 .bh = aio_bh_new(ctx, blkreplay_bh_cb, req),
70 };
71 replay_block_event(req->bh, reqid);
72 }
73
74 static int coroutine_fn GRAPH_RDLOCK
75 blkreplay_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
76 QEMUIOVector *qiov, BdrvRequestFlags flags)
77 {
78 uint64_t reqid = blkreplay_next_id();
79 int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
80 block_request_create(reqid, bs, qemu_coroutine_self());
81 qemu_coroutine_yield();
82
83 return ret;
84 }
85
86 static int coroutine_fn GRAPH_RDLOCK
87 blkreplay_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
88 QEMUIOVector *qiov, BdrvRequestFlags flags)
89 {
90 uint64_t reqid = blkreplay_next_id();
91 int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
92 block_request_create(reqid, bs, qemu_coroutine_self());
93 qemu_coroutine_yield();
94
95 return ret;
96 }
97
98 static int coroutine_fn GRAPH_RDLOCK
99 blkreplay_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int64_t bytes,
100 BdrvRequestFlags flags)
101 {
102 uint64_t reqid = blkreplay_next_id();
103 int ret = bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
104 block_request_create(reqid, bs, qemu_coroutine_self());
105 qemu_coroutine_yield();
106
107 return ret;
108 }
109
110 static int coroutine_fn GRAPH_RDLOCK
111 blkreplay_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
112 {
113 uint64_t reqid = blkreplay_next_id();
114 int ret = bdrv_co_pdiscard(bs->file, offset, bytes);
115 block_request_create(reqid, bs, qemu_coroutine_self());
116 qemu_coroutine_yield();
117
118 return ret;
119 }
120
121 static int coroutine_fn GRAPH_RDLOCK blkreplay_co_flush(BlockDriverState *bs)
122 {
123 uint64_t reqid = blkreplay_next_id();
124 int ret = bdrv_co_flush(bs->file->bs);
125 block_request_create(reqid, bs, qemu_coroutine_self());
126 qemu_coroutine_yield();
127
128 return ret;
129 }
130
131 static int blkreplay_snapshot_goto(BlockDriverState *bs,
132 const char *snapshot_id)
133 {
134 BlockDriverState *file_bs;
135
136 bdrv_graph_rdlock_main_loop();
137 file_bs = bs->file->bs;
138 bdrv_graph_rdunlock_main_loop();
139
140 return bdrv_snapshot_goto(file_bs, snapshot_id, NULL);
141 }
142
143 static BlockDriver bdrv_blkreplay = {
144 .format_name = "blkreplay",
145 .instance_size = 0,
146 .is_filter = true,
147
148 .bdrv_open = blkreplay_open,
149 .bdrv_child_perm = bdrv_default_perms,
150 .bdrv_co_getlength = blkreplay_co_getlength,
151
152 .bdrv_co_preadv = blkreplay_co_preadv,
153 .bdrv_co_pwritev = blkreplay_co_pwritev,
154
155 .bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes,
156 .bdrv_co_pdiscard = blkreplay_co_pdiscard,
157 .bdrv_co_flush = blkreplay_co_flush,
158
159 .bdrv_snapshot_goto = blkreplay_snapshot_goto,
160 };
161
162 static void bdrv_blkreplay_init(void)
163 {
164 bdrv_register(&bdrv_blkreplay);
165 }
166
167 block_init(bdrv_blkreplay_init);