]>
Commit | Line | Data |
---|---|---|
63785678 PD |
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" | |
0b8fa32f | 13 | #include "qemu/module.h" |
63785678 PD |
14 | #include "block/block_int.h" |
15 | #include "sysemu/replay.h" | |
16 | #include "qapi/error.h" | |
17 | ||
18 | typedef struct Request { | |
19 | Coroutine *co; | |
20 | QEMUBH *bh; | |
21 | } Request; | |
22 | ||
63785678 PD |
23 | static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags, |
24 | Error **errp) | |
25 | { | |
26 | Error *local_err = NULL; | |
27 | int ret; | |
28 | ||
29 | /* Open the image file */ | |
b3af2af4 HR |
30 | bs->file = bdrv_open_child(NULL, options, "image", bs, &child_of_bds, |
31 | BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, | |
32 | false, &local_err); | |
63785678 PD |
33 | if (local_err) { |
34 | ret = -EINVAL; | |
35 | error_propagate(errp, local_err); | |
36 | goto fail; | |
37 | } | |
38 | ||
228345bf HR |
39 | bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED; |
40 | bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED; | |
41 | ||
63785678 PD |
42 | ret = 0; |
43 | fail: | |
63785678 PD |
44 | return ret; |
45 | } | |
46 | ||
63785678 PD |
47 | static int64_t blkreplay_getlength(BlockDriverState *bs) |
48 | { | |
49 | return bdrv_getlength(bs->file->bs); | |
50 | } | |
51 | ||
52 | /* This bh is used for synchronization of return from coroutines. | |
53 | It continues yielded coroutine which then finishes its execution. | |
54 | BH is called adjusted to some replay checkpoint, therefore | |
55 | record and replay will always finish coroutines deterministically. | |
56 | */ | |
57 | static void blkreplay_bh_cb(void *opaque) | |
58 | { | |
59 | Request *req = opaque; | |
1919631e | 60 | aio_co_wake(req->co); |
63785678 PD |
61 | qemu_bh_delete(req->bh); |
62 | g_free(req); | |
63 | } | |
64 | ||
65 | static void block_request_create(uint64_t reqid, BlockDriverState *bs, | |
66 | Coroutine *co) | |
67 | { | |
68 | Request *req = g_new(Request, 1); | |
69 | *req = (Request) { | |
70 | .co = co, | |
71 | .bh = aio_bh_new(bdrv_get_aio_context(bs), blkreplay_bh_cb, req), | |
72 | }; | |
73 | replay_block_event(req->bh, reqid); | |
74 | } | |
75 | ||
e858a970 KW |
76 | static int coroutine_fn blkreplay_co_preadv(BlockDriverState *bs, |
77 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
63785678 | 78 | { |
6d0ceb80 | 79 | uint64_t reqid = blkreplay_next_id(); |
a03ef88f | 80 | int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
63785678 PD |
81 | block_request_create(reqid, bs, qemu_coroutine_self()); |
82 | qemu_coroutine_yield(); | |
83 | ||
84 | return ret; | |
85 | } | |
86 | ||
e858a970 KW |
87 | static int coroutine_fn blkreplay_co_pwritev(BlockDriverState *bs, |
88 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) | |
63785678 | 89 | { |
6d0ceb80 | 90 | uint64_t reqid = blkreplay_next_id(); |
a03ef88f | 91 | int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
63785678 PD |
92 | block_request_create(reqid, bs, qemu_coroutine_self()); |
93 | qemu_coroutine_yield(); | |
94 | ||
95 | return ret; | |
96 | } | |
97 | ||
9c21a422 | 98 | static int coroutine_fn blkreplay_co_pwrite_zeroes(BlockDriverState *bs, |
f5a5ca79 | 99 | int64_t offset, int bytes, BdrvRequestFlags flags) |
63785678 | 100 | { |
6d0ceb80 | 101 | uint64_t reqid = blkreplay_next_id(); |
f5a5ca79 | 102 | int ret = bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags); |
63785678 PD |
103 | block_request_create(reqid, bs, qemu_coroutine_self()); |
104 | qemu_coroutine_yield(); | |
105 | ||
106 | return ret; | |
107 | } | |
108 | ||
aba76e2f | 109 | static int coroutine_fn blkreplay_co_pdiscard(BlockDriverState *bs, |
f5a5ca79 | 110 | int64_t offset, int bytes) |
63785678 | 111 | { |
6d0ceb80 | 112 | uint64_t reqid = blkreplay_next_id(); |
0b9fd3f4 | 113 | int ret = bdrv_co_pdiscard(bs->file, offset, bytes); |
63785678 PD |
114 | block_request_create(reqid, bs, qemu_coroutine_self()); |
115 | qemu_coroutine_yield(); | |
116 | ||
117 | return ret; | |
118 | } | |
119 | ||
120 | static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs) | |
121 | { | |
6d0ceb80 | 122 | uint64_t reqid = blkreplay_next_id(); |
63785678 PD |
123 | int ret = bdrv_co_flush(bs->file->bs); |
124 | block_request_create(reqid, bs, qemu_coroutine_self()); | |
125 | qemu_coroutine_yield(); | |
126 | ||
127 | return ret; | |
128 | } | |
129 | ||
3c6c4348 PD |
130 | static int blkreplay_snapshot_goto(BlockDriverState *bs, |
131 | const char *snapshot_id) | |
132 | { | |
133 | return bdrv_snapshot_goto(bs->file->bs, snapshot_id, NULL); | |
134 | } | |
135 | ||
63785678 PD |
136 | static BlockDriver bdrv_blkreplay = { |
137 | .format_name = "blkreplay", | |
63785678 | 138 | .instance_size = 0, |
6540fd15 | 139 | .is_filter = true, |
63785678 | 140 | |
8140e786 | 141 | .bdrv_open = blkreplay_open, |
69dca43d | 142 | .bdrv_child_perm = bdrv_default_perms, |
63785678 PD |
143 | .bdrv_getlength = blkreplay_getlength, |
144 | ||
e858a970 KW |
145 | .bdrv_co_preadv = blkreplay_co_preadv, |
146 | .bdrv_co_pwritev = blkreplay_co_pwritev, | |
63785678 | 147 | |
9c21a422 | 148 | .bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes, |
aba76e2f | 149 | .bdrv_co_pdiscard = blkreplay_co_pdiscard, |
63785678 | 150 | .bdrv_co_flush = blkreplay_co_flush, |
3c6c4348 PD |
151 | |
152 | .bdrv_snapshot_goto = blkreplay_snapshot_goto, | |
63785678 PD |
153 | }; |
154 | ||
155 | static void bdrv_blkreplay_init(void) | |
156 | { | |
157 | bdrv_register(&bdrv_blkreplay); | |
158 | } | |
159 | ||
160 | block_init(bdrv_blkreplay_init); |