Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 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 | |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 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 */ |
| 30 | bs->file = bdrv_open_child(NULL, options, "image", |
| 31 | bs, &child_file, false, &local_err); |
| 32 | if (local_err) { |
| 33 | ret = -EINVAL; |
| 34 | error_propagate(errp, local_err); |
| 35 | goto fail; |
| 36 | } |
| 37 | |
| 38 | ret = 0; |
| 39 | fail: |
| 40 | if (ret < 0) { |
| 41 | bdrv_unref_child(bs, bs->file); |
| 42 | } |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static void blkreplay_close(BlockDriverState *bs) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | static int64_t blkreplay_getlength(BlockDriverState *bs) |
| 51 | { |
| 52 | return bdrv_getlength(bs->file->bs); |
| 53 | } |
| 54 | |
| 55 | /* This bh is used for synchronization of return from coroutines. |
| 56 | It continues yielded coroutine which then finishes its execution. |
| 57 | BH is called adjusted to some replay checkpoint, therefore |
| 58 | record and replay will always finish coroutines deterministically. |
| 59 | */ |
| 60 | static void blkreplay_bh_cb(void *opaque) |
| 61 | { |
| 62 | Request *req = opaque; |
Paolo Bonzini | 1919631 | 2017-02-13 14:52:31 +0100 | [diff] [blame] | 63 | aio_co_wake(req->co); |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 64 | qemu_bh_delete(req->bh); |
| 65 | g_free(req); |
| 66 | } |
| 67 | |
| 68 | static void block_request_create(uint64_t reqid, BlockDriverState *bs, |
| 69 | Coroutine *co) |
| 70 | { |
| 71 | Request *req = g_new(Request, 1); |
| 72 | *req = (Request) { |
| 73 | .co = co, |
| 74 | .bh = aio_bh_new(bdrv_get_aio_context(bs), blkreplay_bh_cb, req), |
| 75 | }; |
| 76 | replay_block_event(req->bh, reqid); |
| 77 | } |
| 78 | |
Kevin Wolf | e858a97 | 2016-05-30 14:31:59 +0200 | [diff] [blame] | 79 | static int coroutine_fn blkreplay_co_preadv(BlockDriverState *bs, |
| 80 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 81 | { |
Pavel Dovgalyuk | 6d0ceb8 | 2016-09-26 11:08:16 +0300 | [diff] [blame] | 82 | uint64_t reqid = blkreplay_next_id(); |
Kevin Wolf | a03ef88 | 2016-06-20 21:31:46 +0200 | [diff] [blame] | 83 | int ret = bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 84 | block_request_create(reqid, bs, qemu_coroutine_self()); |
| 85 | qemu_coroutine_yield(); |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
Kevin Wolf | e858a97 | 2016-05-30 14:31:59 +0200 | [diff] [blame] | 90 | static int coroutine_fn blkreplay_co_pwritev(BlockDriverState *bs, |
| 91 | uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags) |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 92 | { |
Pavel Dovgalyuk | 6d0ceb8 | 2016-09-26 11:08:16 +0300 | [diff] [blame] | 93 | uint64_t reqid = blkreplay_next_id(); |
Kevin Wolf | a03ef88 | 2016-06-20 21:31:46 +0200 | [diff] [blame] | 94 | int ret = bdrv_co_pwritev(bs->file, offset, bytes, qiov, flags); |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 95 | block_request_create(reqid, bs, qemu_coroutine_self()); |
| 96 | qemu_coroutine_yield(); |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
Eric Blake | 9c21a42 | 2016-06-01 15:10:07 -0600 | [diff] [blame] | 101 | static int coroutine_fn blkreplay_co_pwrite_zeroes(BlockDriverState *bs, |
| 102 | int64_t offset, int count, BdrvRequestFlags flags) |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 103 | { |
Pavel Dovgalyuk | 6d0ceb8 | 2016-09-26 11:08:16 +0300 | [diff] [blame] | 104 | uint64_t reqid = blkreplay_next_id(); |
Kevin Wolf | a03ef88 | 2016-06-20 21:31:46 +0200 | [diff] [blame] | 105 | int ret = bdrv_co_pwrite_zeroes(bs->file, offset, count, flags); |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 106 | block_request_create(reqid, bs, qemu_coroutine_self()); |
| 107 | qemu_coroutine_yield(); |
| 108 | |
| 109 | return ret; |
| 110 | } |
| 111 | |
Eric Blake | aba76e2 | 2016-07-15 17:22:59 -0600 | [diff] [blame] | 112 | static int coroutine_fn blkreplay_co_pdiscard(BlockDriverState *bs, |
| 113 | int64_t offset, int count) |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 114 | { |
Pavel Dovgalyuk | 6d0ceb8 | 2016-09-26 11:08:16 +0300 | [diff] [blame] | 115 | uint64_t reqid = blkreplay_next_id(); |
Eric Blake | aba76e2 | 2016-07-15 17:22:59 -0600 | [diff] [blame] | 116 | int ret = bdrv_co_pdiscard(bs->file->bs, offset, count); |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 117 | block_request_create(reqid, bs, qemu_coroutine_self()); |
| 118 | qemu_coroutine_yield(); |
| 119 | |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | static int coroutine_fn blkreplay_co_flush(BlockDriverState *bs) |
| 124 | { |
Pavel Dovgalyuk | 6d0ceb8 | 2016-09-26 11:08:16 +0300 | [diff] [blame] | 125 | uint64_t reqid = blkreplay_next_id(); |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 126 | int ret = bdrv_co_flush(bs->file->bs); |
| 127 | block_request_create(reqid, bs, qemu_coroutine_self()); |
| 128 | qemu_coroutine_yield(); |
| 129 | |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | static BlockDriver bdrv_blkreplay = { |
| 134 | .format_name = "blkreplay", |
| 135 | .protocol_name = "blkreplay", |
| 136 | .instance_size = 0, |
| 137 | |
| 138 | .bdrv_file_open = blkreplay_open, |
| 139 | .bdrv_close = blkreplay_close, |
Kevin Wolf | d7010df | 2016-12-15 12:28:58 +0100 | [diff] [blame] | 140 | .bdrv_child_perm = bdrv_filter_default_perms, |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 141 | .bdrv_getlength = blkreplay_getlength, |
| 142 | |
Kevin Wolf | e858a97 | 2016-05-30 14:31:59 +0200 | [diff] [blame] | 143 | .bdrv_co_preadv = blkreplay_co_preadv, |
| 144 | .bdrv_co_pwritev = blkreplay_co_pwritev, |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 145 | |
Eric Blake | 9c21a42 | 2016-06-01 15:10:07 -0600 | [diff] [blame] | 146 | .bdrv_co_pwrite_zeroes = blkreplay_co_pwrite_zeroes, |
Eric Blake | aba76e2 | 2016-07-15 17:22:59 -0600 | [diff] [blame] | 147 | .bdrv_co_pdiscard = blkreplay_co_pdiscard, |
Pavel Dovgalyuk | 6378567 | 2016-03-14 10:45:10 +0300 | [diff] [blame] | 148 | .bdrv_co_flush = blkreplay_co_flush, |
| 149 | }; |
| 150 | |
| 151 | static void bdrv_blkreplay_init(void) |
| 152 | { |
| 153 | bdrv_register(&bdrv_blkreplay); |
| 154 | } |
| 155 | |
| 156 | block_init(bdrv_blkreplay_init); |