]> git.proxmox.com Git - mirror_qemu.git/blame - block/null.c
Merge tag 'pull-riscv-to-apply-20231107' of https://github.com/alistair23/qemu into...
[mirror_qemu.git] / block / null.c
CommitLineData
e819ab22
FZ
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
80c71a24 13#include "qemu/osdep.h"
da34e65c 14#include "qapi/error.h"
67882b15
HR
15#include "qapi/qmp/qdict.h"
16#include "qapi/qmp/qstring.h"
0b8fa32f 17#include "qemu/module.h"
922a01a0 18#include "qemu/option.h"
e2c1c34f 19#include "block/block-io.h"
e819ab22 20#include "block/block_int.h"
e4ec5ad4 21#include "sysemu/replay.h"
e819ab22 22
e5e51dd3 23#define NULL_OPT_LATENCY "latency-ns"
cd219eb1 24#define NULL_OPT_ZEROES "read-zeroes"
e5e51dd3 25
e819ab22
FZ
26typedef struct {
27 int64_t length;
e5e51dd3 28 int64_t latency_ns;
cd219eb1 29 bool read_zeroes;
e819ab22
FZ
30} BDRVNullState;
31
32static QemuOptsList runtime_opts = {
33 .name = "null",
34 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
35 .desc = {
e819ab22
FZ
36 {
37 .name = BLOCK_OPT_SIZE,
38 .type = QEMU_OPT_SIZE,
39 .help = "size of the null block",
40 },
e5e51dd3
FZ
41 {
42 .name = NULL_OPT_LATENCY,
43 .type = QEMU_OPT_NUMBER,
44 .help = "nanoseconds (approximated) to wait "
45 "before completing request",
46 },
cd219eb1
HR
47 {
48 .name = NULL_OPT_ZEROES,
49 .type = QEMU_OPT_BOOL,
50 .help = "return zeroes when read",
51 },
e819ab22
FZ
52 { /* end of list */ }
53 },
54};
55
809eb70e
KW
56static void null_co_parse_filename(const char *filename, QDict *options,
57 Error **errp)
58{
59 /* This functions only exists so that a null-co:// filename is accepted
60 * with the null-co driver. */
61 if (strcmp(filename, "null-co://")) {
62 error_setg(errp, "The only allowed filename for this driver is "
63 "'null-co://'");
64 return;
65 }
66}
67
68static void null_aio_parse_filename(const char *filename, QDict *options,
69 Error **errp)
70{
71 /* This functions only exists so that a null-aio:// filename is accepted
72 * with the null-aio driver. */
73 if (strcmp(filename, "null-aio://")) {
74 error_setg(errp, "The only allowed filename for this driver is "
75 "'null-aio://'");
76 return;
77 }
78}
79
e819ab22
FZ
80static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
81 Error **errp)
82{
83 QemuOpts *opts;
84 BDRVNullState *s = bs->opaque;
e5e51dd3 85 int ret = 0;
e819ab22
FZ
86
87 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
88 qemu_opts_absorb_qdict(opts, options, &error_abort);
89 s->length =
90 qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
e5e51dd3
FZ
91 s->latency_ns =
92 qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0);
93 if (s->latency_ns < 0) {
94 error_setg(errp, "latency-ns is invalid");
95 ret = -EINVAL;
96 }
cd219eb1 97 s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
e819ab22 98 qemu_opts_del(opts);
b3241e92 99 bs->supported_write_flags = BDRV_REQ_FUA;
e5e51dd3 100 return ret;
e819ab22
FZ
101}
102
c86422c5 103static int64_t coroutine_fn null_co_getlength(BlockDriverState *bs)
e819ab22
FZ
104{
105 BDRVNullState *s = bs->opaque;
106 return s->length;
107}
108
e5e51dd3
FZ
109static coroutine_fn int null_co_common(BlockDriverState *bs)
110{
111 BDRVNullState *s = bs->opaque;
112
113 if (s->latency_ns) {
78f1d3d6 114 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, s->latency_ns);
e5e51dd3
FZ
115 }
116 return 0;
117}
118
b3241e92 119static coroutine_fn int null_co_preadv(BlockDriverState *bs,
f7ef38dd
VSO
120 int64_t offset, int64_t bytes,
121 QEMUIOVector *qiov,
122 BdrvRequestFlags flags)
e819ab22 123{
cd219eb1
HR
124 BDRVNullState *s = bs->opaque;
125
126 if (s->read_zeroes) {
b3241e92 127 qemu_iovec_memset(qiov, 0, 0, bytes);
cd219eb1
HR
128 }
129
e5e51dd3 130 return null_co_common(bs);
e819ab22
FZ
131}
132
b3241e92 133static coroutine_fn int null_co_pwritev(BlockDriverState *bs,
e75abeda
VSO
134 int64_t offset, int64_t bytes,
135 QEMUIOVector *qiov,
136 BdrvRequestFlags flags)
e819ab22 137{
e5e51dd3 138 return null_co_common(bs);
e819ab22
FZ
139}
140
141static coroutine_fn int null_co_flush(BlockDriverState *bs)
142{
e5e51dd3 143 return null_co_common(bs);
e819ab22
FZ
144}
145
146typedef struct {
7c84b1b8 147 BlockAIOCB common;
e5e51dd3 148 QEMUTimer timer;
e819ab22
FZ
149} NullAIOCB;
150
151static const AIOCBInfo null_aiocb_info = {
152 .aiocb_size = sizeof(NullAIOCB),
153};
154
155static void null_bh_cb(void *opaque)
156{
157 NullAIOCB *acb = opaque;
158 acb->common.cb(acb->common.opaque, 0);
e819ab22
FZ
159 qemu_aio_unref(acb);
160}
161
e5e51dd3
FZ
162static void null_timer_cb(void *opaque)
163{
164 NullAIOCB *acb = opaque;
165 acb->common.cb(acb->common.opaque, 0);
166 timer_deinit(&acb->timer);
167 qemu_aio_unref(acb);
168}
169
7c84b1b8 170static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
097310b5 171 BlockCompletionFunc *cb,
7c84b1b8 172 void *opaque)
e819ab22
FZ
173{
174 NullAIOCB *acb;
e5e51dd3 175 BDRVNullState *s = bs->opaque;
e819ab22
FZ
176
177 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
e5e51dd3
FZ
178 /* Only emulate latency after vcpu is running. */
179 if (s->latency_ns) {
180 aio_timer_init(bdrv_get_aio_context(bs), &acb->timer,
181 QEMU_CLOCK_REALTIME, SCALE_NS,
182 null_timer_cb, acb);
183 timer_mod_ns(&acb->timer,
184 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
185 } else {
e4ec5ad4
PD
186 replay_bh_schedule_oneshot_event(bdrv_get_aio_context(bs),
187 null_bh_cb, acb);
e5e51dd3 188 }
e819ab22
FZ
189 return &acb->common;
190}
191
b3241e92 192static BlockAIOCB *null_aio_preadv(BlockDriverState *bs,
f7ef38dd
VSO
193 int64_t offset, int64_t bytes,
194 QEMUIOVector *qiov, BdrvRequestFlags flags,
b3241e92
EB
195 BlockCompletionFunc *cb,
196 void *opaque)
e819ab22 197{
cd219eb1
HR
198 BDRVNullState *s = bs->opaque;
199
200 if (s->read_zeroes) {
b3241e92 201 qemu_iovec_memset(qiov, 0, 0, bytes);
cd219eb1
HR
202 }
203
e819ab22
FZ
204 return null_aio_common(bs, cb, opaque);
205}
206
b3241e92 207static BlockAIOCB *null_aio_pwritev(BlockDriverState *bs,
e75abeda
VSO
208 int64_t offset, int64_t bytes,
209 QEMUIOVector *qiov, BdrvRequestFlags flags,
b3241e92
EB
210 BlockCompletionFunc *cb,
211 void *opaque)
e819ab22
FZ
212{
213 return null_aio_common(bs, cb, opaque);
214}
215
7c84b1b8 216static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
097310b5 217 BlockCompletionFunc *cb,
7c84b1b8 218 void *opaque)
e819ab22
FZ
219{
220 return null_aio_common(bs, cb, opaque);
221}
222
1c2b49a1
FZ
223static int null_reopen_prepare(BDRVReopenState *reopen_state,
224 BlockReopenQueue *queue, Error **errp)
225{
226 return 0;
227}
228
05c33f10
EB
229static int coroutine_fn null_co_block_status(BlockDriverState *bs,
230 bool want_zero, int64_t offset,
231 int64_t bytes, int64_t *pnum,
232 int64_t *map,
233 BlockDriverState **file)
a9063927
HR
234{
235 BDRVNullState *s = bs->opaque;
05c33f10 236 int ret = BDRV_BLOCK_OFFSET_VALID;
a9063927 237
05c33f10
EB
238 *pnum = bytes;
239 *map = offset;
a9063927
HR
240 *file = bs;
241
242 if (s->read_zeroes) {
05c33f10 243 ret |= BDRV_BLOCK_ZERO;
a9063927 244 }
05c33f10 245 return ret;
a9063927
HR
246}
247
998b3a1e 248static void null_refresh_filename(BlockDriverState *bs)
67882b15 249{
998b3a1e
HR
250 const QDictEntry *e;
251
252 for (e = qdict_first(bs->full_open_options); e;
253 e = qdict_next(bs->full_open_options, e))
254 {
255 /* These options can be ignored */
256 if (strcmp(qdict_entry_key(e), "filename") &&
1e47cb7f
HR
257 strcmp(qdict_entry_key(e), "driver") &&
258 strcmp(qdict_entry_key(e), NULL_OPT_LATENCY))
998b3a1e
HR
259 {
260 return;
261 }
67882b15
HR
262 }
263
998b3a1e
HR
264 snprintf(bs->exact_filename, sizeof(bs->exact_filename), "%s://",
265 bs->drv->format_name);
67882b15
HR
266}
267
82618d7b
EGE
268static int64_t coroutine_fn
269null_co_get_allocated_file_size(BlockDriverState *bs)
07cd7b65
HR
270{
271 return 0;
272}
273
2654267c
HR
274static const char *const null_strong_runtime_opts[] = {
275 BLOCK_OPT_SIZE,
276 NULL_OPT_ZEROES,
277
278 NULL
279};
280
e819ab22
FZ
281static BlockDriver bdrv_null_co = {
282 .format_name = "null-co",
283 .protocol_name = "null-co",
284 .instance_size = sizeof(BDRVNullState),
285
286 .bdrv_file_open = null_file_open,
809eb70e 287 .bdrv_parse_filename = null_co_parse_filename,
c86422c5 288 .bdrv_co_getlength = null_co_getlength,
82618d7b 289 .bdrv_co_get_allocated_file_size = null_co_get_allocated_file_size,
e819ab22 290
b3241e92
EB
291 .bdrv_co_preadv = null_co_preadv,
292 .bdrv_co_pwritev = null_co_pwritev,
e819ab22 293 .bdrv_co_flush_to_disk = null_co_flush,
1c2b49a1 294 .bdrv_reopen_prepare = null_reopen_prepare,
a9063927 295
05c33f10 296 .bdrv_co_block_status = null_co_block_status,
67882b15
HR
297
298 .bdrv_refresh_filename = null_refresh_filename,
2654267c 299 .strong_runtime_opts = null_strong_runtime_opts,
e819ab22
FZ
300};
301
302static BlockDriver bdrv_null_aio = {
303 .format_name = "null-aio",
304 .protocol_name = "null-aio",
305 .instance_size = sizeof(BDRVNullState),
306
307 .bdrv_file_open = null_file_open,
809eb70e 308 .bdrv_parse_filename = null_aio_parse_filename,
c86422c5 309 .bdrv_co_getlength = null_co_getlength,
82618d7b 310 .bdrv_co_get_allocated_file_size = null_co_get_allocated_file_size,
e819ab22 311
b3241e92
EB
312 .bdrv_aio_preadv = null_aio_preadv,
313 .bdrv_aio_pwritev = null_aio_pwritev,
e819ab22 314 .bdrv_aio_flush = null_aio_flush,
1c2b49a1 315 .bdrv_reopen_prepare = null_reopen_prepare,
a9063927 316
05c33f10 317 .bdrv_co_block_status = null_co_block_status,
67882b15
HR
318
319 .bdrv_refresh_filename = null_refresh_filename,
2654267c 320 .strong_runtime_opts = null_strong_runtime_opts,
e819ab22
FZ
321};
322
323static void bdrv_null_init(void)
324{
325 bdrv_register(&bdrv_null_co);
326 bdrv_register(&bdrv_null_aio);
327}
328
329block_init(bdrv_null_init);