]> git.proxmox.com Git - mirror_qemu.git/blame - block/null.c
async: add aio_bh_schedule_oneshot
[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"
e819ab22
FZ
17#include "block/block_int.h"
18
e5e51dd3 19#define NULL_OPT_LATENCY "latency-ns"
cd219eb1 20#define NULL_OPT_ZEROES "read-zeroes"
e5e51dd3 21
e819ab22
FZ
22typedef struct {
23 int64_t length;
e5e51dd3 24 int64_t latency_ns;
cd219eb1 25 bool read_zeroes;
e819ab22
FZ
26} BDRVNullState;
27
28static 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 },
e5e51dd3
FZ
42 {
43 .name = NULL_OPT_LATENCY,
44 .type = QEMU_OPT_NUMBER,
45 .help = "nanoseconds (approximated) to wait "
46 "before completing request",
47 },
cd219eb1
HR
48 {
49 .name = NULL_OPT_ZEROES,
50 .type = QEMU_OPT_BOOL,
51 .help = "return zeroes when read",
52 },
e819ab22
FZ
53 { /* end of list */ }
54 },
55};
56
57static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
58 Error **errp)
59{
60 QemuOpts *opts;
61 BDRVNullState *s = bs->opaque;
e5e51dd3 62 int ret = 0;
e819ab22
FZ
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);
e5e51dd3
FZ
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 }
cd219eb1 74 s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
e819ab22 75 qemu_opts_del(opts);
e5e51dd3 76 return ret;
e819ab22
FZ
77}
78
79static void null_close(BlockDriverState *bs)
80{
81}
82
83static int64_t null_getlength(BlockDriverState *bs)
84{
85 BDRVNullState *s = bs->opaque;
86 return s->length;
87}
88
e5e51dd3
FZ
89static 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
e819ab22
FZ
100static coroutine_fn int null_co_readv(BlockDriverState *bs,
101 int64_t sector_num, int nb_sectors,
102 QEMUIOVector *qiov)
103{
cd219eb1
HR
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
e5e51dd3 110 return null_co_common(bs);
e819ab22
FZ
111}
112
113static coroutine_fn int null_co_writev(BlockDriverState *bs,
114 int64_t sector_num, int nb_sectors,
115 QEMUIOVector *qiov)
116{
e5e51dd3 117 return null_co_common(bs);
e819ab22
FZ
118}
119
120static coroutine_fn int null_co_flush(BlockDriverState *bs)
121{
e5e51dd3 122 return null_co_common(bs);
e819ab22
FZ
123}
124
125typedef struct {
7c84b1b8 126 BlockAIOCB common;
e819ab22 127 QEMUBH *bh;
e5e51dd3 128 QEMUTimer timer;
e819ab22
FZ
129} NullAIOCB;
130
131static const AIOCBInfo null_aiocb_info = {
132 .aiocb_size = sizeof(NullAIOCB),
133};
134
135static void null_bh_cb(void *opaque)
136{
137 NullAIOCB *acb = opaque;
138 acb->common.cb(acb->common.opaque, 0);
139 qemu_bh_delete(acb->bh);
140 qemu_aio_unref(acb);
141}
142
e5e51dd3
FZ
143static void null_timer_cb(void *opaque)
144{
145 NullAIOCB *acb = opaque;
146 acb->common.cb(acb->common.opaque, 0);
147 timer_deinit(&acb->timer);
148 qemu_aio_unref(acb);
149}
150
7c84b1b8 151static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
097310b5 152 BlockCompletionFunc *cb,
7c84b1b8 153 void *opaque)
e819ab22
FZ
154{
155 NullAIOCB *acb;
e5e51dd3 156 BDRVNullState *s = bs->opaque;
e819ab22
FZ
157
158 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
e5e51dd3
FZ
159 /* Only emulate latency after vcpu is running. */
160 if (s->latency_ns) {
161 aio_timer_init(bdrv_get_aio_context(bs), &acb->timer,
162 QEMU_CLOCK_REALTIME, SCALE_NS,
163 null_timer_cb, acb);
164 timer_mod_ns(&acb->timer,
165 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
166 } else {
167 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
168 qemu_bh_schedule(acb->bh);
169 }
e819ab22
FZ
170 return &acb->common;
171}
172
7c84b1b8
MA
173static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
174 int64_t sector_num, QEMUIOVector *qiov,
175 int nb_sectors,
097310b5 176 BlockCompletionFunc *cb,
7c84b1b8 177 void *opaque)
e819ab22 178{
cd219eb1
HR
179 BDRVNullState *s = bs->opaque;
180
181 if (s->read_zeroes) {
182 qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
183 }
184
e819ab22
FZ
185 return null_aio_common(bs, cb, opaque);
186}
187
7c84b1b8
MA
188static BlockAIOCB *null_aio_writev(BlockDriverState *bs,
189 int64_t sector_num, QEMUIOVector *qiov,
190 int nb_sectors,
097310b5 191 BlockCompletionFunc *cb,
7c84b1b8 192 void *opaque)
e819ab22
FZ
193{
194 return null_aio_common(bs, cb, opaque);
195}
196
7c84b1b8 197static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
097310b5 198 BlockCompletionFunc *cb,
7c84b1b8 199 void *opaque)
e819ab22
FZ
200{
201 return null_aio_common(bs, cb, opaque);
202}
203
1c2b49a1
FZ
204static int null_reopen_prepare(BDRVReopenState *reopen_state,
205 BlockReopenQueue *queue, Error **errp)
206{
207 return 0;
208}
209
a9063927
HR
210static int64_t coroutine_fn null_co_get_block_status(BlockDriverState *bs,
211 int64_t sector_num,
212 int nb_sectors, int *pnum,
213 BlockDriverState **file)
214{
215 BDRVNullState *s = bs->opaque;
216 off_t start = sector_num * BDRV_SECTOR_SIZE;
217
218 *pnum = nb_sectors;
219 *file = bs;
220
221 if (s->read_zeroes) {
222 return BDRV_BLOCK_OFFSET_VALID | start | BDRV_BLOCK_ZERO;
223 } else {
224 return BDRV_BLOCK_OFFSET_VALID | start;
225 }
226}
227
67882b15
HR
228static void null_refresh_filename(BlockDriverState *bs, QDict *opts)
229{
230 QINCREF(opts);
231 qdict_del(opts, "filename");
232
233 if (!qdict_size(opts)) {
234 snprintf(bs->exact_filename, sizeof(bs->exact_filename), "%s://",
235 bs->drv->format_name);
236 }
237
238 qdict_put(opts, "driver", qstring_from_str(bs->drv->format_name));
239 bs->full_open_options = opts;
240}
241
e819ab22
FZ
242static BlockDriver bdrv_null_co = {
243 .format_name = "null-co",
244 .protocol_name = "null-co",
245 .instance_size = sizeof(BDRVNullState),
246
247 .bdrv_file_open = null_file_open,
248 .bdrv_close = null_close,
249 .bdrv_getlength = null_getlength,
250
251 .bdrv_co_readv = null_co_readv,
252 .bdrv_co_writev = null_co_writev,
253 .bdrv_co_flush_to_disk = null_co_flush,
1c2b49a1 254 .bdrv_reopen_prepare = null_reopen_prepare,
a9063927
HR
255
256 .bdrv_co_get_block_status = null_co_get_block_status,
67882b15
HR
257
258 .bdrv_refresh_filename = null_refresh_filename,
e819ab22
FZ
259};
260
261static BlockDriver bdrv_null_aio = {
262 .format_name = "null-aio",
263 .protocol_name = "null-aio",
264 .instance_size = sizeof(BDRVNullState),
265
266 .bdrv_file_open = null_file_open,
267 .bdrv_close = null_close,
268 .bdrv_getlength = null_getlength,
269
270 .bdrv_aio_readv = null_aio_readv,
271 .bdrv_aio_writev = null_aio_writev,
272 .bdrv_aio_flush = null_aio_flush,
1c2b49a1 273 .bdrv_reopen_prepare = null_reopen_prepare,
a9063927
HR
274
275 .bdrv_co_get_block_status = null_co_get_block_status,
67882b15
HR
276
277 .bdrv_refresh_filename = null_refresh_filename,
e819ab22
FZ
278};
279
280static void bdrv_null_init(void)
281{
282 bdrv_register(&bdrv_null_co);
283 bdrv_register(&bdrv_null_aio);
284}
285
286block_init(bdrv_null_init);