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