qapi: Reject blank 'if' conditions in addition to empty ones
[qemu/kevin.git] / block / null.c
blob699aa295cb587c2f79ec0152997386fd743e09cb
1 /*
2 * Null block driver
4 * Authors:
5 * Fam Zheng <famz@redhat.com>
7 * Copyright (C) 2014 Red Hat, Inc.
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.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qapi/qmp/qdict.h"
16 #include "qapi/qmp/qstring.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
19 #include "block/block_int.h"
21 #define NULL_OPT_LATENCY "latency-ns"
22 #define NULL_OPT_ZEROES "read-zeroes"
24 typedef struct {
25 int64_t length;
26 int64_t latency_ns;
27 bool read_zeroes;
28 } BDRVNullState;
30 static QemuOptsList runtime_opts = {
31 .name = "null",
32 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
33 .desc = {
35 .name = BLOCK_OPT_SIZE,
36 .type = QEMU_OPT_SIZE,
37 .help = "size of the null block",
40 .name = NULL_OPT_LATENCY,
41 .type = QEMU_OPT_NUMBER,
42 .help = "nanoseconds (approximated) to wait "
43 "before completing request",
46 .name = NULL_OPT_ZEROES,
47 .type = QEMU_OPT_BOOL,
48 .help = "return zeroes when read",
50 { /* end of list */ }
54 static void null_co_parse_filename(const char *filename, QDict *options,
55 Error **errp)
57 /* This functions only exists so that a null-co:// filename is accepted
58 * with the null-co driver. */
59 if (strcmp(filename, "null-co://")) {
60 error_setg(errp, "The only allowed filename for this driver is "
61 "'null-co://'");
62 return;
66 static void null_aio_parse_filename(const char *filename, QDict *options,
67 Error **errp)
69 /* This functions only exists so that a null-aio:// filename is accepted
70 * with the null-aio driver. */
71 if (strcmp(filename, "null-aio://")) {
72 error_setg(errp, "The only allowed filename for this driver is "
73 "'null-aio://'");
74 return;
78 static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
79 Error **errp)
81 QemuOpts *opts;
82 BDRVNullState *s = bs->opaque;
83 int ret = 0;
85 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
86 qemu_opts_absorb_qdict(opts, options, &error_abort);
87 s->length =
88 qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
89 s->latency_ns =
90 qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0);
91 if (s->latency_ns < 0) {
92 error_setg(errp, "latency-ns is invalid");
93 ret = -EINVAL;
95 s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
96 qemu_opts_del(opts);
97 bs->supported_write_flags = BDRV_REQ_FUA;
98 return ret;
101 static int64_t null_getlength(BlockDriverState *bs)
103 BDRVNullState *s = bs->opaque;
104 return s->length;
107 static coroutine_fn int null_co_common(BlockDriverState *bs)
109 BDRVNullState *s = bs->opaque;
111 if (s->latency_ns) {
112 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, s->latency_ns);
114 return 0;
117 static coroutine_fn int null_co_preadv(BlockDriverState *bs,
118 uint64_t offset, uint64_t bytes,
119 QEMUIOVector *qiov, int flags)
121 BDRVNullState *s = bs->opaque;
123 if (s->read_zeroes) {
124 qemu_iovec_memset(qiov, 0, 0, bytes);
127 return null_co_common(bs);
130 static coroutine_fn int null_co_pwritev(BlockDriverState *bs,
131 uint64_t offset, uint64_t bytes,
132 QEMUIOVector *qiov, int flags)
134 return null_co_common(bs);
137 static coroutine_fn int null_co_flush(BlockDriverState *bs)
139 return null_co_common(bs);
142 typedef struct {
143 BlockAIOCB common;
144 QEMUTimer timer;
145 } NullAIOCB;
147 static const AIOCBInfo null_aiocb_info = {
148 .aiocb_size = sizeof(NullAIOCB),
151 static void null_bh_cb(void *opaque)
153 NullAIOCB *acb = opaque;
154 acb->common.cb(acb->common.opaque, 0);
155 qemu_aio_unref(acb);
158 static void null_timer_cb(void *opaque)
160 NullAIOCB *acb = opaque;
161 acb->common.cb(acb->common.opaque, 0);
162 timer_deinit(&acb->timer);
163 qemu_aio_unref(acb);
166 static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
167 BlockCompletionFunc *cb,
168 void *opaque)
170 NullAIOCB *acb;
171 BDRVNullState *s = bs->opaque;
173 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
174 /* Only emulate latency after vcpu is running. */
175 if (s->latency_ns) {
176 aio_timer_init(bdrv_get_aio_context(bs), &acb->timer,
177 QEMU_CLOCK_REALTIME, SCALE_NS,
178 null_timer_cb, acb);
179 timer_mod_ns(&acb->timer,
180 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
181 } else {
182 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), null_bh_cb, acb);
184 return &acb->common;
187 static BlockAIOCB *null_aio_preadv(BlockDriverState *bs,
188 uint64_t offset, uint64_t bytes,
189 QEMUIOVector *qiov, int flags,
190 BlockCompletionFunc *cb,
191 void *opaque)
193 BDRVNullState *s = bs->opaque;
195 if (s->read_zeroes) {
196 qemu_iovec_memset(qiov, 0, 0, bytes);
199 return null_aio_common(bs, cb, opaque);
202 static BlockAIOCB *null_aio_pwritev(BlockDriverState *bs,
203 uint64_t offset, uint64_t bytes,
204 QEMUIOVector *qiov, int flags,
205 BlockCompletionFunc *cb,
206 void *opaque)
208 return null_aio_common(bs, cb, opaque);
211 static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
212 BlockCompletionFunc *cb,
213 void *opaque)
215 return null_aio_common(bs, cb, opaque);
218 static int null_reopen_prepare(BDRVReopenState *reopen_state,
219 BlockReopenQueue *queue, Error **errp)
221 return 0;
224 static int coroutine_fn null_co_block_status(BlockDriverState *bs,
225 bool want_zero, int64_t offset,
226 int64_t bytes, int64_t *pnum,
227 int64_t *map,
228 BlockDriverState **file)
230 BDRVNullState *s = bs->opaque;
231 int ret = BDRV_BLOCK_OFFSET_VALID;
233 *pnum = bytes;
234 *map = offset;
235 *file = bs;
237 if (s->read_zeroes) {
238 ret |= BDRV_BLOCK_ZERO;
240 return ret;
243 static void null_refresh_filename(BlockDriverState *bs)
245 const QDictEntry *e;
247 for (e = qdict_first(bs->full_open_options); e;
248 e = qdict_next(bs->full_open_options, e))
250 /* These options can be ignored */
251 if (strcmp(qdict_entry_key(e), "filename") &&
252 strcmp(qdict_entry_key(e), "driver") &&
253 strcmp(qdict_entry_key(e), NULL_OPT_LATENCY))
255 return;
259 snprintf(bs->exact_filename, sizeof(bs->exact_filename), "%s://",
260 bs->drv->format_name);
263 static const char *const null_strong_runtime_opts[] = {
264 BLOCK_OPT_SIZE,
265 NULL_OPT_ZEROES,
267 NULL
270 static BlockDriver bdrv_null_co = {
271 .format_name = "null-co",
272 .protocol_name = "null-co",
273 .instance_size = sizeof(BDRVNullState),
275 .bdrv_file_open = null_file_open,
276 .bdrv_parse_filename = null_co_parse_filename,
277 .bdrv_getlength = null_getlength,
279 .bdrv_co_preadv = null_co_preadv,
280 .bdrv_co_pwritev = null_co_pwritev,
281 .bdrv_co_flush_to_disk = null_co_flush,
282 .bdrv_reopen_prepare = null_reopen_prepare,
284 .bdrv_co_block_status = null_co_block_status,
286 .bdrv_refresh_filename = null_refresh_filename,
287 .strong_runtime_opts = null_strong_runtime_opts,
290 static BlockDriver bdrv_null_aio = {
291 .format_name = "null-aio",
292 .protocol_name = "null-aio",
293 .instance_size = sizeof(BDRVNullState),
295 .bdrv_file_open = null_file_open,
296 .bdrv_parse_filename = null_aio_parse_filename,
297 .bdrv_getlength = null_getlength,
299 .bdrv_aio_preadv = null_aio_preadv,
300 .bdrv_aio_pwritev = null_aio_pwritev,
301 .bdrv_aio_flush = null_aio_flush,
302 .bdrv_reopen_prepare = null_reopen_prepare,
304 .bdrv_co_block_status = null_co_block_status,
306 .bdrv_refresh_filename = null_refresh_filename,
307 .strong_runtime_opts = null_strong_runtime_opts,
310 static void bdrv_null_init(void)
312 bdrv_register(&bdrv_null_co);
313 bdrv_register(&bdrv_null_aio);
316 block_init(bdrv_null_init);