memory: Fix wrong end address dump
[qemu/ar7.git] / block / blkreplay.c
blob76a0b8d12ae148a1d75b7047c0b5a15e59f28e56
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_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 static int blkreplay_open(BlockDriverState *bs, QDict *options, int flags,
24 Error **errp)
26 int ret;
28 /* Open the image file */
29 ret = bdrv_open_file_child(NULL, options, "image", bs, errp);
30 if (ret < 0) {
31 goto fail;
34 bs->supported_write_flags = BDRV_REQ_WRITE_UNCHANGED;
35 bs->supported_zero_flags = BDRV_REQ_WRITE_UNCHANGED;
37 ret = 0;
38 fail:
39 return ret;
42 static int64_t blkreplay_getlength(BlockDriverState *bs)
44 return bdrv_getlength(bs->file->bs);
47 /* This bh is used for synchronization of return from coroutines.
48 It continues yielded coroutine which then finishes its execution.
49 BH is called adjusted to some replay checkpoint, therefore
50 record and replay will always finish coroutines deterministically.
52 static void blkreplay_bh_cb(void *opaque)
54 Request *req = opaque;
55 aio_co_wake(req->co);
56 qemu_bh_delete(req->bh);
57 g_free(req);
60 static void block_request_create(uint64_t reqid, BlockDriverState *bs,
61 Coroutine *co)
63 Request *req = g_new(Request, 1);
64 *req = (Request) {
65 .co = co,
66 .bh = aio_bh_new(bdrv_get_aio_context(bs), blkreplay_bh_cb, req),
68 replay_block_event(req->bh, reqid);
71 static int coroutine_fn blkreplay_co_preadv(BlockDriverState *bs,
72 int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags)
74 uint64_t reqid = blkreplay_next_id();
75 int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
76 block_request_create(reqid, bs, qemu_coroutine_self());
77 qemu_coroutine_yield();
79 return ret;
82 static int coroutine_fn blkreplay_co_pwritev(BlockDriverState *bs,
83 int64_t offset, int64_t bytes, QEMUIOVector *qiov, BdrvRequestFlags flags)
85 uint64_t reqid = blkreplay_next_id();
86 int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags);
87 block_request_create(reqid, bs, qemu_coroutine_self());
88 qemu_coroutine_yield();
90 return ret;
93 static int coroutine_fn blkreplay_co_pwrite_zeroes(BlockDriverState *bs,
94 int64_t offset, int64_t bytes, BdrvRequestFlags flags)
96 uint64_t reqid = blkreplay_next_id();
97 int ret = bdrv_co_pwrite_zeroes(bs->file, offset, bytes, flags);
98 block_request_create(reqid, bs, qemu_coroutine_self());
99 qemu_coroutine_yield();
101 return ret;
104 static int coroutine_fn blkreplay_co_pdiscard(BlockDriverState *bs,
105 int64_t offset, int64_t bytes)
107 uint64_t reqid = blkreplay_next_id();
108 int ret = bdrv_co_pdiscard(bs->file, offset, bytes);
109 block_request_create(reqid, bs, qemu_coroutine_self());
110 qemu_coroutine_yield();
112 return ret;
115 static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs)
117 uint64_t reqid = blkreplay_next_id();
118 int ret = bdrv_co_flush(bs->file->bs);
119 block_request_create(reqid, bs, qemu_coroutine_self());
120 qemu_coroutine_yield();
122 return ret;
125 static int blkreplay_snapshot_goto(BlockDriverState *bs,
126 const char *snapshot_id)
128 return bdrv_snapshot_goto(bs->file->bs, snapshot_id, NULL);
131 static BlockDriver bdrv_blkreplay = {
132 .format_name = "blkreplay",
133 .instance_size = 0,
134 .is_filter = true,
136 .bdrv_open = blkreplay_open,
137 .bdrv_child_perm = bdrv_default_perms,
138 .bdrv_getlength = blkreplay_getlength,
140 .bdrv_co_preadv = blkreplay_co_preadv,
141 .bdrv_co_pwritev = blkreplay_co_pwritev,
143 .bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes,
144 .bdrv_co_pdiscard = blkreplay_co_pdiscard,
145 .bdrv_co_flush = blkreplay_co_flush,
147 .bdrv_snapshot_goto = blkreplay_snapshot_goto,
150 static void bdrv_blkreplay_init(void)
152 bdrv_register(&bdrv_blkreplay);
155 block_init(bdrv_blkreplay_init);