include/qemu/osdep.h: Don't include qapi/error.h
[qemu/ar7.git] / block / null.c
blob00bc6e478a975f0aaa2309fc50639674638ec802
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 "block/block_int.h"
17 #define NULL_OPT_LATENCY "latency-ns"
19 typedef struct {
20 int64_t length;
21 int64_t latency_ns;
22 } BDRVNullState;
24 static QemuOptsList runtime_opts = {
25 .name = "null",
26 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
27 .desc = {
29 .name = "filename",
30 .type = QEMU_OPT_STRING,
31 .help = "",
34 .name = BLOCK_OPT_SIZE,
35 .type = QEMU_OPT_SIZE,
36 .help = "size of the null block",
39 .name = NULL_OPT_LATENCY,
40 .type = QEMU_OPT_NUMBER,
41 .help = "nanoseconds (approximated) to wait "
42 "before completing request",
44 { /* end of list */ }
48 static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
49 Error **errp)
51 QemuOpts *opts;
52 BDRVNullState *s = bs->opaque;
53 int ret = 0;
55 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
56 qemu_opts_absorb_qdict(opts, options, &error_abort);
57 s->length =
58 qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
59 s->latency_ns =
60 qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0);
61 if (s->latency_ns < 0) {
62 error_setg(errp, "latency-ns is invalid");
63 ret = -EINVAL;
65 qemu_opts_del(opts);
66 return ret;
69 static void null_close(BlockDriverState *bs)
73 static int64_t null_getlength(BlockDriverState *bs)
75 BDRVNullState *s = bs->opaque;
76 return s->length;
79 static coroutine_fn int null_co_common(BlockDriverState *bs)
81 BDRVNullState *s = bs->opaque;
83 if (s->latency_ns) {
84 co_aio_sleep_ns(bdrv_get_aio_context(bs), QEMU_CLOCK_REALTIME,
85 s->latency_ns);
87 return 0;
90 static coroutine_fn int null_co_readv(BlockDriverState *bs,
91 int64_t sector_num, int nb_sectors,
92 QEMUIOVector *qiov)
94 return null_co_common(bs);
97 static coroutine_fn int null_co_writev(BlockDriverState *bs,
98 int64_t sector_num, int nb_sectors,
99 QEMUIOVector *qiov)
101 return null_co_common(bs);
104 static coroutine_fn int null_co_flush(BlockDriverState *bs)
106 return null_co_common(bs);
109 typedef struct {
110 BlockAIOCB common;
111 QEMUBH *bh;
112 QEMUTimer timer;
113 } NullAIOCB;
115 static const AIOCBInfo null_aiocb_info = {
116 .aiocb_size = sizeof(NullAIOCB),
119 static void null_bh_cb(void *opaque)
121 NullAIOCB *acb = opaque;
122 acb->common.cb(acb->common.opaque, 0);
123 qemu_bh_delete(acb->bh);
124 qemu_aio_unref(acb);
127 static void null_timer_cb(void *opaque)
129 NullAIOCB *acb = opaque;
130 acb->common.cb(acb->common.opaque, 0);
131 timer_deinit(&acb->timer);
132 qemu_aio_unref(acb);
135 static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
136 BlockCompletionFunc *cb,
137 void *opaque)
139 NullAIOCB *acb;
140 BDRVNullState *s = bs->opaque;
142 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
143 /* Only emulate latency after vcpu is running. */
144 if (s->latency_ns) {
145 aio_timer_init(bdrv_get_aio_context(bs), &acb->timer,
146 QEMU_CLOCK_REALTIME, SCALE_NS,
147 null_timer_cb, acb);
148 timer_mod_ns(&acb->timer,
149 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
150 } else {
151 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
152 qemu_bh_schedule(acb->bh);
154 return &acb->common;
157 static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
158 int64_t sector_num, QEMUIOVector *qiov,
159 int nb_sectors,
160 BlockCompletionFunc *cb,
161 void *opaque)
163 return null_aio_common(bs, cb, opaque);
166 static BlockAIOCB *null_aio_writev(BlockDriverState *bs,
167 int64_t sector_num, QEMUIOVector *qiov,
168 int nb_sectors,
169 BlockCompletionFunc *cb,
170 void *opaque)
172 return null_aio_common(bs, cb, opaque);
175 static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
176 BlockCompletionFunc *cb,
177 void *opaque)
179 return null_aio_common(bs, cb, opaque);
182 static int null_reopen_prepare(BDRVReopenState *reopen_state,
183 BlockReopenQueue *queue, Error **errp)
185 return 0;
188 static BlockDriver bdrv_null_co = {
189 .format_name = "null-co",
190 .protocol_name = "null-co",
191 .instance_size = sizeof(BDRVNullState),
193 .bdrv_file_open = null_file_open,
194 .bdrv_close = null_close,
195 .bdrv_getlength = null_getlength,
197 .bdrv_co_readv = null_co_readv,
198 .bdrv_co_writev = null_co_writev,
199 .bdrv_co_flush_to_disk = null_co_flush,
200 .bdrv_reopen_prepare = null_reopen_prepare,
203 static BlockDriver bdrv_null_aio = {
204 .format_name = "null-aio",
205 .protocol_name = "null-aio",
206 .instance_size = sizeof(BDRVNullState),
208 .bdrv_file_open = null_file_open,
209 .bdrv_close = null_close,
210 .bdrv_getlength = null_getlength,
212 .bdrv_aio_readv = null_aio_readv,
213 .bdrv_aio_writev = null_aio_writev,
214 .bdrv_aio_flush = null_aio_flush,
215 .bdrv_reopen_prepare = null_reopen_prepare,
218 static void bdrv_null_init(void)
220 bdrv_register(&bdrv_null_co);
221 bdrv_register(&bdrv_null_aio);
224 block_init(bdrv_null_init);