Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Null block driver |
| 3 | * |
| 4 | * Authors: |
| 5 | * Fam Zheng <famz@redhat.com> |
| 6 | * |
| 7 | * Copyright (C) 2014 Red Hat, Inc. |
| 8 | * |
| 9 | * This work is licensed under the terms of the GNU GPL, version 2 or later. |
| 10 | * See the COPYING file in the top-level directory. |
| 11 | */ |
| 12 | |
Peter Maydell | 80c71a2 | 2016-01-18 18:01:42 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 14 | #include "qapi/error.h" |
Max Reitz | 67882b1 | 2016-06-10 20:57:48 +0200 | [diff] [blame] | 15 | #include "qapi/qmp/qdict.h" |
| 16 | #include "qapi/qmp/qstring.h" |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 17 | #include "block/block_int.h" |
| 18 | |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 19 | #define NULL_OPT_LATENCY "latency-ns" |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 20 | #define NULL_OPT_ZEROES "read-zeroes" |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 21 | |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 22 | typedef struct { |
| 23 | int64_t length; |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 24 | int64_t latency_ns; |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 25 | bool read_zeroes; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 26 | } BDRVNullState; |
| 27 | |
| 28 | static QemuOptsList runtime_opts = { |
| 29 | .name = "null", |
| 30 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), |
| 31 | .desc = { |
| 32 | { |
| 33 | .name = "filename", |
| 34 | .type = QEMU_OPT_STRING, |
| 35 | .help = "", |
| 36 | }, |
| 37 | { |
| 38 | .name = BLOCK_OPT_SIZE, |
| 39 | .type = QEMU_OPT_SIZE, |
| 40 | .help = "size of the null block", |
| 41 | }, |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 42 | { |
| 43 | .name = NULL_OPT_LATENCY, |
| 44 | .type = QEMU_OPT_NUMBER, |
| 45 | .help = "nanoseconds (approximated) to wait " |
| 46 | "before completing request", |
| 47 | }, |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 48 | { |
| 49 | .name = NULL_OPT_ZEROES, |
| 50 | .type = QEMU_OPT_BOOL, |
| 51 | .help = "return zeroes when read", |
| 52 | }, |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 53 | { /* end of list */ } |
| 54 | }, |
| 55 | }; |
| 56 | |
| 57 | static int null_file_open(BlockDriverState *bs, QDict *options, int flags, |
| 58 | Error **errp) |
| 59 | { |
| 60 | QemuOpts *opts; |
| 61 | BDRVNullState *s = bs->opaque; |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 62 | int ret = 0; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 63 | |
| 64 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
| 65 | qemu_opts_absorb_qdict(opts, options, &error_abort); |
| 66 | s->length = |
| 67 | qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30); |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 68 | s->latency_ns = |
| 69 | qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0); |
| 70 | if (s->latency_ns < 0) { |
| 71 | error_setg(errp, "latency-ns is invalid"); |
| 72 | ret = -EINVAL; |
| 73 | } |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 74 | s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 75 | qemu_opts_del(opts); |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 76 | return ret; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | static void null_close(BlockDriverState *bs) |
| 80 | { |
| 81 | } |
| 82 | |
| 83 | static int64_t null_getlength(BlockDriverState *bs) |
| 84 | { |
| 85 | BDRVNullState *s = bs->opaque; |
| 86 | return s->length; |
| 87 | } |
| 88 | |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 89 | static coroutine_fn int null_co_common(BlockDriverState *bs) |
| 90 | { |
| 91 | BDRVNullState *s = bs->opaque; |
| 92 | |
| 93 | if (s->latency_ns) { |
| 94 | co_aio_sleep_ns(bdrv_get_aio_context(bs), QEMU_CLOCK_REALTIME, |
| 95 | s->latency_ns); |
| 96 | } |
| 97 | return 0; |
| 98 | } |
| 99 | |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 100 | static coroutine_fn int null_co_readv(BlockDriverState *bs, |
| 101 | int64_t sector_num, int nb_sectors, |
| 102 | QEMUIOVector *qiov) |
| 103 | { |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 104 | BDRVNullState *s = bs->opaque; |
| 105 | |
| 106 | if (s->read_zeroes) { |
| 107 | qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE); |
| 108 | } |
| 109 | |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 110 | return null_co_common(bs); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static coroutine_fn int null_co_writev(BlockDriverState *bs, |
| 114 | int64_t sector_num, int nb_sectors, |
| 115 | QEMUIOVector *qiov) |
| 116 | { |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 117 | return null_co_common(bs); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static coroutine_fn int null_co_flush(BlockDriverState *bs) |
| 121 | { |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 122 | return null_co_common(bs); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | typedef struct { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 126 | BlockAIOCB common; |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 127 | QEMUTimer timer; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 128 | } NullAIOCB; |
| 129 | |
| 130 | static const AIOCBInfo null_aiocb_info = { |
| 131 | .aiocb_size = sizeof(NullAIOCB), |
| 132 | }; |
| 133 | |
| 134 | static void null_bh_cb(void *opaque) |
| 135 | { |
| 136 | NullAIOCB *acb = opaque; |
| 137 | acb->common.cb(acb->common.opaque, 0); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 138 | qemu_aio_unref(acb); |
| 139 | } |
| 140 | |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 141 | static void null_timer_cb(void *opaque) |
| 142 | { |
| 143 | NullAIOCB *acb = opaque; |
| 144 | acb->common.cb(acb->common.opaque, 0); |
| 145 | timer_deinit(&acb->timer); |
| 146 | qemu_aio_unref(acb); |
| 147 | } |
| 148 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 149 | static inline BlockAIOCB *null_aio_common(BlockDriverState *bs, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 150 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 151 | void *opaque) |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 152 | { |
| 153 | NullAIOCB *acb; |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 154 | BDRVNullState *s = bs->opaque; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 155 | |
| 156 | acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque); |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 157 | /* Only emulate latency after vcpu is running. */ |
| 158 | if (s->latency_ns) { |
| 159 | aio_timer_init(bdrv_get_aio_context(bs), &acb->timer, |
| 160 | QEMU_CLOCK_REALTIME, SCALE_NS, |
| 161 | null_timer_cb, acb); |
| 162 | timer_mod_ns(&acb->timer, |
| 163 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns); |
| 164 | } else { |
Paolo Bonzini | fffb6e1 | 2016-10-03 18:14:16 +0200 | [diff] [blame] | 165 | aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), null_bh_cb, acb); |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 166 | } |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 167 | return &acb->common; |
| 168 | } |
| 169 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 170 | static BlockAIOCB *null_aio_readv(BlockDriverState *bs, |
| 171 | int64_t sector_num, QEMUIOVector *qiov, |
| 172 | int nb_sectors, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 173 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 174 | void *opaque) |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 175 | { |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 176 | BDRVNullState *s = bs->opaque; |
| 177 | |
| 178 | if (s->read_zeroes) { |
| 179 | qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE); |
| 180 | } |
| 181 | |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 182 | return null_aio_common(bs, cb, opaque); |
| 183 | } |
| 184 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 185 | static BlockAIOCB *null_aio_writev(BlockDriverState *bs, |
| 186 | int64_t sector_num, QEMUIOVector *qiov, |
| 187 | int nb_sectors, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 188 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 189 | void *opaque) |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 190 | { |
| 191 | return null_aio_common(bs, cb, opaque); |
| 192 | } |
| 193 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 194 | static BlockAIOCB *null_aio_flush(BlockDriverState *bs, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 195 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 196 | void *opaque) |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 197 | { |
| 198 | return null_aio_common(bs, cb, opaque); |
| 199 | } |
| 200 | |
Fam Zheng | 1c2b49a | 2015-04-01 09:45:39 +0800 | [diff] [blame] | 201 | static int null_reopen_prepare(BDRVReopenState *reopen_state, |
| 202 | BlockReopenQueue *queue, Error **errp) |
| 203 | { |
| 204 | return 0; |
| 205 | } |
| 206 | |
Max Reitz | a906392 | 2016-03-24 23:33:59 +0100 | [diff] [blame] | 207 | static int64_t coroutine_fn null_co_get_block_status(BlockDriverState *bs, |
| 208 | int64_t sector_num, |
| 209 | int nb_sectors, int *pnum, |
| 210 | BlockDriverState **file) |
| 211 | { |
| 212 | BDRVNullState *s = bs->opaque; |
| 213 | off_t start = sector_num * BDRV_SECTOR_SIZE; |
| 214 | |
| 215 | *pnum = nb_sectors; |
| 216 | *file = bs; |
| 217 | |
| 218 | if (s->read_zeroes) { |
| 219 | return BDRV_BLOCK_OFFSET_VALID | start | BDRV_BLOCK_ZERO; |
| 220 | } else { |
| 221 | return BDRV_BLOCK_OFFSET_VALID | start; |
| 222 | } |
| 223 | } |
| 224 | |
Max Reitz | 67882b1 | 2016-06-10 20:57:48 +0200 | [diff] [blame] | 225 | static void null_refresh_filename(BlockDriverState *bs, QDict *opts) |
| 226 | { |
| 227 | QINCREF(opts); |
| 228 | qdict_del(opts, "filename"); |
| 229 | |
| 230 | if (!qdict_size(opts)) { |
| 231 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), "%s://", |
| 232 | bs->drv->format_name); |
| 233 | } |
| 234 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 235 | qdict_put_str(opts, "driver", bs->drv->format_name); |
Max Reitz | 67882b1 | 2016-06-10 20:57:48 +0200 | [diff] [blame] | 236 | bs->full_open_options = opts; |
| 237 | } |
| 238 | |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 239 | static BlockDriver bdrv_null_co = { |
| 240 | .format_name = "null-co", |
| 241 | .protocol_name = "null-co", |
| 242 | .instance_size = sizeof(BDRVNullState), |
| 243 | |
| 244 | .bdrv_file_open = null_file_open, |
| 245 | .bdrv_close = null_close, |
| 246 | .bdrv_getlength = null_getlength, |
| 247 | |
| 248 | .bdrv_co_readv = null_co_readv, |
| 249 | .bdrv_co_writev = null_co_writev, |
| 250 | .bdrv_co_flush_to_disk = null_co_flush, |
Fam Zheng | 1c2b49a | 2015-04-01 09:45:39 +0800 | [diff] [blame] | 251 | .bdrv_reopen_prepare = null_reopen_prepare, |
Max Reitz | a906392 | 2016-03-24 23:33:59 +0100 | [diff] [blame] | 252 | |
| 253 | .bdrv_co_get_block_status = null_co_get_block_status, |
Max Reitz | 67882b1 | 2016-06-10 20:57:48 +0200 | [diff] [blame] | 254 | |
| 255 | .bdrv_refresh_filename = null_refresh_filename, |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 256 | }; |
| 257 | |
| 258 | static BlockDriver bdrv_null_aio = { |
| 259 | .format_name = "null-aio", |
| 260 | .protocol_name = "null-aio", |
| 261 | .instance_size = sizeof(BDRVNullState), |
| 262 | |
| 263 | .bdrv_file_open = null_file_open, |
| 264 | .bdrv_close = null_close, |
| 265 | .bdrv_getlength = null_getlength, |
| 266 | |
| 267 | .bdrv_aio_readv = null_aio_readv, |
| 268 | .bdrv_aio_writev = null_aio_writev, |
| 269 | .bdrv_aio_flush = null_aio_flush, |
Fam Zheng | 1c2b49a | 2015-04-01 09:45:39 +0800 | [diff] [blame] | 270 | .bdrv_reopen_prepare = null_reopen_prepare, |
Max Reitz | a906392 | 2016-03-24 23:33:59 +0100 | [diff] [blame] | 271 | |
| 272 | .bdrv_co_get_block_status = null_co_get_block_status, |
Max Reitz | 67882b1 | 2016-06-10 20:57:48 +0200 | [diff] [blame] | 273 | |
| 274 | .bdrv_refresh_filename = null_refresh_filename, |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 275 | }; |
| 276 | |
| 277 | static void bdrv_null_init(void) |
| 278 | { |
| 279 | bdrv_register(&bdrv_null_co); |
| 280 | bdrv_register(&bdrv_null_aio); |
| 281 | } |
| 282 | |
| 283 | block_init(bdrv_null_init); |