]> Git Repo - qemu.git/blob - block/blkreplay.c
block: Switch bdrv_write_zeroes() to byte interface
[qemu.git] / block / blkreplay.c
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-common.h"
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
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;
27
28 static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,
29                           Error **errp)
30 {
31     Error *local_err = NULL;
32     int ret;
33
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;
41     }
42
43     ret = 0;
44 fail:
45     if (ret < 0) {
46         bdrv_unref_child(bs, bs->file);
47     }
48     return ret;
49 }
50
51 static void blkreplay_close(BlockDriverState *bs)
52 {
53 }
54
55 static int64_t blkreplay_getlength(BlockDriverState *bs)
56 {
57     return bdrv_getlength(bs->file->bs);
58 }
59
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.
64 */
65 static void blkreplay_bh_cb(void *opaque)
66 {
67     Request *req = opaque;
68     qemu_coroutine_enter(req->co, NULL);
69     qemu_bh_delete(req->bh);
70     g_free(req);
71 }
72
73 static void block_request_create(uint64_t reqid, BlockDriverState *bs,
74                                  Coroutine *co)
75 {
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),
80     };
81     replay_block_event(req->bh, reqid);
82 }
83
84 static int coroutine_fn blkreplay_co_readv(BlockDriverState *bs,
85     int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
86 {
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();
91
92     return ret;
93 }
94
95 static int coroutine_fn blkreplay_co_writev(BlockDriverState *bs,
96     int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
97 {
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();
102
103     return ret;
104 }
105
106 static int coroutine_fn blkreplay_co_write_zeroes(BlockDriverState *bs,
107     int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
108 {
109     uint64_t reqid = request_id++;
110     int ret = bdrv_co_pwrite_zeroes(bs->file->bs,
111                                     sector_num << BDRV_SECTOR_BITS,
112                                     nb_sectors << BDRV_SECTOR_BITS, flags);
113     block_request_create(reqid, bs, qemu_coroutine_self());
114     qemu_coroutine_yield();
115
116     return ret;
117 }
118
119 static int coroutine_fn blkreplay_co_discard(BlockDriverState *bs,
120     int64_t sector_num, int nb_sectors)
121 {
122     uint64_t reqid = request_id++;
123     int ret = bdrv_co_discard(bs->file->bs, sector_num, nb_sectors);
124     block_request_create(reqid, bs, qemu_coroutine_self());
125     qemu_coroutine_yield();
126
127     return ret;
128 }
129
130 static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs)
131 {
132     uint64_t reqid = request_id++;
133     int ret = bdrv_co_flush(bs->file->bs);
134     block_request_create(reqid, bs, qemu_coroutine_self());
135     qemu_coroutine_yield();
136
137     return ret;
138 }
139
140 static BlockDriver bdrv_blkreplay = {
141     .format_name            = "blkreplay",
142     .protocol_name          = "blkreplay",
143     .instance_size          = 0,
144
145     .bdrv_file_open         = blkreplay_open,
146     .bdrv_close             = blkreplay_close,
147     .bdrv_getlength         = blkreplay_getlength,
148
149     .bdrv_co_readv          = blkreplay_co_readv,
150     .bdrv_co_writev         = blkreplay_co_writev,
151
152     .bdrv_co_write_zeroes   = blkreplay_co_write_zeroes,
153     .bdrv_co_discard        = blkreplay_co_discard,
154     .bdrv_co_flush          = blkreplay_co_flush,
155 };
156
157 static void bdrv_blkreplay_init(void)
158 {
159     bdrv_register(&bdrv_blkreplay);
160 }
161
162 block_init(bdrv_blkreplay_init);
This page took 0.032502 seconds and 4 git commands to generate.