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 | { |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 33 | .name = BLOCK_OPT_SIZE, |
| 34 | .type = QEMU_OPT_SIZE, |
| 35 | .help = "size of the null block", |
| 36 | }, |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 37 | { |
| 38 | .name = NULL_OPT_LATENCY, |
| 39 | .type = QEMU_OPT_NUMBER, |
| 40 | .help = "nanoseconds (approximated) to wait " |
| 41 | "before completing request", |
| 42 | }, |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 43 | { |
| 44 | .name = NULL_OPT_ZEROES, |
| 45 | .type = QEMU_OPT_BOOL, |
| 46 | .help = "return zeroes when read", |
| 47 | }, |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 48 | { /* end of list */ } |
| 49 | }, |
| 50 | }; |
| 51 | |
Kevin Wolf | 809eb70 | 2017-08-04 12:44:22 +0200 | [diff] [blame] | 52 | static void null_co_parse_filename(const char *filename, QDict *options, |
| 53 | Error **errp) |
| 54 | { |
| 55 | /* This functions only exists so that a null-co:// filename is accepted |
| 56 | * with the null-co driver. */ |
| 57 | if (strcmp(filename, "null-co://")) { |
| 58 | error_setg(errp, "The only allowed filename for this driver is " |
| 59 | "'null-co://'"); |
| 60 | return; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | static void null_aio_parse_filename(const char *filename, QDict *options, |
| 65 | Error **errp) |
| 66 | { |
| 67 | /* This functions only exists so that a null-aio:// filename is accepted |
| 68 | * with the null-aio driver. */ |
| 69 | if (strcmp(filename, "null-aio://")) { |
| 70 | error_setg(errp, "The only allowed filename for this driver is " |
| 71 | "'null-aio://'"); |
| 72 | return; |
| 73 | } |
| 74 | } |
| 75 | |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 76 | static int null_file_open(BlockDriverState *bs, QDict *options, int flags, |
| 77 | Error **errp) |
| 78 | { |
| 79 | QemuOpts *opts; |
| 80 | BDRVNullState *s = bs->opaque; |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 81 | int ret = 0; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 82 | |
| 83 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); |
| 84 | qemu_opts_absorb_qdict(opts, options, &error_abort); |
| 85 | s->length = |
| 86 | qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30); |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 87 | s->latency_ns = |
| 88 | qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0); |
| 89 | if (s->latency_ns < 0) { |
| 90 | error_setg(errp, "latency-ns is invalid"); |
| 91 | ret = -EINVAL; |
| 92 | } |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 93 | s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 94 | qemu_opts_del(opts); |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 95 | return ret; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void null_close(BlockDriverState *bs) |
| 99 | { |
| 100 | } |
| 101 | |
| 102 | static int64_t null_getlength(BlockDriverState *bs) |
| 103 | { |
| 104 | BDRVNullState *s = bs->opaque; |
| 105 | return s->length; |
| 106 | } |
| 107 | |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 108 | static coroutine_fn int null_co_common(BlockDriverState *bs) |
| 109 | { |
| 110 | BDRVNullState *s = bs->opaque; |
| 111 | |
| 112 | if (s->latency_ns) { |
Stefan Hajnoczi | 78f1d3d | 2017-11-09 10:26:52 +0000 | [diff] [blame] | 113 | qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, s->latency_ns); |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 114 | } |
| 115 | return 0; |
| 116 | } |
| 117 | |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 118 | static coroutine_fn int null_co_readv(BlockDriverState *bs, |
| 119 | int64_t sector_num, int nb_sectors, |
| 120 | QEMUIOVector *qiov) |
| 121 | { |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 122 | BDRVNullState *s = bs->opaque; |
| 123 | |
| 124 | if (s->read_zeroes) { |
| 125 | qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE); |
| 126 | } |
| 127 | |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 128 | return null_co_common(bs); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static coroutine_fn int null_co_writev(BlockDriverState *bs, |
| 132 | int64_t sector_num, int nb_sectors, |
| 133 | QEMUIOVector *qiov) |
| 134 | { |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 135 | return null_co_common(bs); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | static coroutine_fn int null_co_flush(BlockDriverState *bs) |
| 139 | { |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 140 | return null_co_common(bs); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | typedef struct { |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 144 | BlockAIOCB common; |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 145 | QEMUTimer timer; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 146 | } NullAIOCB; |
| 147 | |
| 148 | static const AIOCBInfo null_aiocb_info = { |
| 149 | .aiocb_size = sizeof(NullAIOCB), |
| 150 | }; |
| 151 | |
| 152 | static void null_bh_cb(void *opaque) |
| 153 | { |
| 154 | NullAIOCB *acb = opaque; |
| 155 | acb->common.cb(acb->common.opaque, 0); |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 156 | qemu_aio_unref(acb); |
| 157 | } |
| 158 | |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 159 | static void null_timer_cb(void *opaque) |
| 160 | { |
| 161 | NullAIOCB *acb = opaque; |
| 162 | acb->common.cb(acb->common.opaque, 0); |
| 163 | timer_deinit(&acb->timer); |
| 164 | qemu_aio_unref(acb); |
| 165 | } |
| 166 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 167 | static inline BlockAIOCB *null_aio_common(BlockDriverState *bs, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 168 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 169 | void *opaque) |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 170 | { |
| 171 | NullAIOCB *acb; |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 172 | BDRVNullState *s = bs->opaque; |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 173 | |
| 174 | acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque); |
Fam Zheng | e5e51dd | 2015-04-01 09:45:38 +0800 | [diff] [blame] | 175 | /* Only emulate latency after vcpu is running. */ |
| 176 | if (s->latency_ns) { |
| 177 | aio_timer_init(bdrv_get_aio_context(bs), &acb->timer, |
| 178 | QEMU_CLOCK_REALTIME, SCALE_NS, |
| 179 | null_timer_cb, acb); |
| 180 | timer_mod_ns(&acb->timer, |
| 181 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns); |
| 182 | } else { |
Paolo Bonzini | fffb6e1 | 2016-10-03 18:14:16 +0200 | [diff] [blame] | 183 | 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] | 184 | } |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 185 | return &acb->common; |
| 186 | } |
| 187 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 188 | static BlockAIOCB *null_aio_readv(BlockDriverState *bs, |
| 189 | int64_t sector_num, QEMUIOVector *qiov, |
| 190 | int nb_sectors, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 191 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 192 | void *opaque) |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 193 | { |
Max Reitz | cd219eb | 2016-03-24 23:33:58 +0100 | [diff] [blame] | 194 | BDRVNullState *s = bs->opaque; |
| 195 | |
| 196 | if (s->read_zeroes) { |
| 197 | qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE); |
| 198 | } |
| 199 | |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 200 | return null_aio_common(bs, cb, opaque); |
| 201 | } |
| 202 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 203 | static BlockAIOCB *null_aio_writev(BlockDriverState *bs, |
| 204 | int64_t sector_num, QEMUIOVector *qiov, |
| 205 | int nb_sectors, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 206 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 207 | void *opaque) |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 208 | { |
| 209 | return null_aio_common(bs, cb, opaque); |
| 210 | } |
| 211 | |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 212 | static BlockAIOCB *null_aio_flush(BlockDriverState *bs, |
Markus Armbruster | 097310b | 2014-10-07 13:59:15 +0200 | [diff] [blame] | 213 | BlockCompletionFunc *cb, |
Markus Armbruster | 7c84b1b | 2014-10-07 13:59:14 +0200 | [diff] [blame] | 214 | void *opaque) |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 215 | { |
| 216 | return null_aio_common(bs, cb, opaque); |
| 217 | } |
| 218 | |
Fam Zheng | 1c2b49a | 2015-04-01 09:45:39 +0800 | [diff] [blame] | 219 | static int null_reopen_prepare(BDRVReopenState *reopen_state, |
| 220 | BlockReopenQueue *queue, Error **errp) |
| 221 | { |
| 222 | return 0; |
| 223 | } |
| 224 | |
Max Reitz | a906392 | 2016-03-24 23:33:59 +0100 | [diff] [blame] | 225 | static int64_t coroutine_fn null_co_get_block_status(BlockDriverState *bs, |
| 226 | int64_t sector_num, |
| 227 | int nb_sectors, int *pnum, |
| 228 | BlockDriverState **file) |
| 229 | { |
| 230 | BDRVNullState *s = bs->opaque; |
| 231 | off_t start = sector_num * BDRV_SECTOR_SIZE; |
| 232 | |
| 233 | *pnum = nb_sectors; |
| 234 | *file = bs; |
| 235 | |
| 236 | if (s->read_zeroes) { |
| 237 | return BDRV_BLOCK_OFFSET_VALID | start | BDRV_BLOCK_ZERO; |
| 238 | } else { |
| 239 | return BDRV_BLOCK_OFFSET_VALID | start; |
| 240 | } |
| 241 | } |
| 242 | |
Max Reitz | 67882b1 | 2016-06-10 20:57:48 +0200 | [diff] [blame] | 243 | static void null_refresh_filename(BlockDriverState *bs, QDict *opts) |
| 244 | { |
| 245 | QINCREF(opts); |
| 246 | qdict_del(opts, "filename"); |
| 247 | |
| 248 | if (!qdict_size(opts)) { |
| 249 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), "%s://", |
| 250 | bs->drv->format_name); |
| 251 | } |
| 252 | |
Eric Blake | 46f5ac2 | 2017-04-27 16:58:17 -0500 | [diff] [blame] | 253 | qdict_put_str(opts, "driver", bs->drv->format_name); |
Max Reitz | 67882b1 | 2016-06-10 20:57:48 +0200 | [diff] [blame] | 254 | bs->full_open_options = opts; |
| 255 | } |
| 256 | |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 257 | static BlockDriver bdrv_null_co = { |
| 258 | .format_name = "null-co", |
| 259 | .protocol_name = "null-co", |
| 260 | .instance_size = sizeof(BDRVNullState), |
| 261 | |
| 262 | .bdrv_file_open = null_file_open, |
Kevin Wolf | 809eb70 | 2017-08-04 12:44:22 +0200 | [diff] [blame] | 263 | .bdrv_parse_filename = null_co_parse_filename, |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 264 | .bdrv_close = null_close, |
| 265 | .bdrv_getlength = null_getlength, |
| 266 | |
| 267 | .bdrv_co_readv = null_co_readv, |
| 268 | .bdrv_co_writev = null_co_writev, |
| 269 | .bdrv_co_flush_to_disk = null_co_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 BlockDriver bdrv_null_aio = { |
| 278 | .format_name = "null-aio", |
| 279 | .protocol_name = "null-aio", |
| 280 | .instance_size = sizeof(BDRVNullState), |
| 281 | |
| 282 | .bdrv_file_open = null_file_open, |
Kevin Wolf | 809eb70 | 2017-08-04 12:44:22 +0200 | [diff] [blame] | 283 | .bdrv_parse_filename = null_aio_parse_filename, |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 284 | .bdrv_close = null_close, |
| 285 | .bdrv_getlength = null_getlength, |
| 286 | |
| 287 | .bdrv_aio_readv = null_aio_readv, |
| 288 | .bdrv_aio_writev = null_aio_writev, |
| 289 | .bdrv_aio_flush = null_aio_flush, |
Fam Zheng | 1c2b49a | 2015-04-01 09:45:39 +0800 | [diff] [blame] | 290 | .bdrv_reopen_prepare = null_reopen_prepare, |
Max Reitz | a906392 | 2016-03-24 23:33:59 +0100 | [diff] [blame] | 291 | |
| 292 | .bdrv_co_get_block_status = null_co_get_block_status, |
Max Reitz | 67882b1 | 2016-06-10 20:57:48 +0200 | [diff] [blame] | 293 | |
| 294 | .bdrv_refresh_filename = null_refresh_filename, |
Fam Zheng | e819ab2 | 2014-09-11 14:09:56 +0800 | [diff] [blame] | 295 | }; |
| 296 | |
| 297 | static void bdrv_null_init(void) |
| 298 | { |
| 299 | bdrv_register(&bdrv_null_co); |
| 300 | bdrv_register(&bdrv_null_aio); |
| 301 | } |
| 302 | |
| 303 | block_init(bdrv_null_init); |