s390x: move .needed functions for subsections to machine.c
[qemu/ar7.git] / block / null.c
blob396500babddee914a86ba215a7ce64576514094f
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"
18 #define NULL_OPT_ZEROES "read-zeroes"
20 typedef struct {
21 int64_t length;
22 int64_t latency_ns;
23 bool read_zeroes;
24 } BDRVNullState;
26 static QemuOptsList runtime_opts = {
27 .name = "null",
28 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
29 .desc = {
31 .name = "filename",
32 .type = QEMU_OPT_STRING,
33 .help = "",
36 .name = BLOCK_OPT_SIZE,
37 .type = QEMU_OPT_SIZE,
38 .help = "size of the null block",
41 .name = NULL_OPT_LATENCY,
42 .type = QEMU_OPT_NUMBER,
43 .help = "nanoseconds (approximated) to wait "
44 "before completing request",
47 .name = NULL_OPT_ZEROES,
48 .type = QEMU_OPT_BOOL,
49 .help = "return zeroes when read",
51 { /* end of list */ }
55 static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
56 Error **errp)
58 QemuOpts *opts;
59 BDRVNullState *s = bs->opaque;
60 int ret = 0;
62 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
63 qemu_opts_absorb_qdict(opts, options, &error_abort);
64 s->length =
65 qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
66 s->latency_ns =
67 qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0);
68 if (s->latency_ns < 0) {
69 error_setg(errp, "latency-ns is invalid");
70 ret = -EINVAL;
72 s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
73 qemu_opts_del(opts);
74 return ret;
77 static void null_close(BlockDriverState *bs)
81 static int64_t null_getlength(BlockDriverState *bs)
83 BDRVNullState *s = bs->opaque;
84 return s->length;
87 static coroutine_fn int null_co_common(BlockDriverState *bs)
89 BDRVNullState *s = bs->opaque;
91 if (s->latency_ns) {
92 co_aio_sleep_ns(bdrv_get_aio_context(bs), QEMU_CLOCK_REALTIME,
93 s->latency_ns);
95 return 0;
98 static coroutine_fn int null_co_readv(BlockDriverState *bs,
99 int64_t sector_num, int nb_sectors,
100 QEMUIOVector *qiov)
102 BDRVNullState *s = bs->opaque;
104 if (s->read_zeroes) {
105 qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
108 return null_co_common(bs);
111 static coroutine_fn int null_co_writev(BlockDriverState *bs,
112 int64_t sector_num, int nb_sectors,
113 QEMUIOVector *qiov)
115 return null_co_common(bs);
118 static coroutine_fn int null_co_flush(BlockDriverState *bs)
120 return null_co_common(bs);
123 typedef struct {
124 BlockAIOCB common;
125 QEMUBH *bh;
126 QEMUTimer timer;
127 } NullAIOCB;
129 static const AIOCBInfo null_aiocb_info = {
130 .aiocb_size = sizeof(NullAIOCB),
133 static void null_bh_cb(void *opaque)
135 NullAIOCB *acb = opaque;
136 acb->common.cb(acb->common.opaque, 0);
137 qemu_bh_delete(acb->bh);
138 qemu_aio_unref(acb);
141 static void null_timer_cb(void *opaque)
143 NullAIOCB *acb = opaque;
144 acb->common.cb(acb->common.opaque, 0);
145 timer_deinit(&acb->timer);
146 qemu_aio_unref(acb);
149 static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
150 BlockCompletionFunc *cb,
151 void *opaque)
153 NullAIOCB *acb;
154 BDRVNullState *s = bs->opaque;
156 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
157 /* Only emulate latency after vcpu is running. */
158 if (s->latency_ns) {
159 aio_timer_init(bdrv_get_aio_context(bs), &acb->timer,
160 QEMU_CLOCK_REALTIME, SCALE_NS,
161 null_timer_cb, acb);
162 timer_mod_ns(&acb->timer,
163 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
164 } else {
165 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
166 qemu_bh_schedule(acb->bh);
168 return &acb->common;
171 static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
172 int64_t sector_num, QEMUIOVector *qiov,
173 int nb_sectors,
174 BlockCompletionFunc *cb,
175 void *opaque)
177 BDRVNullState *s = bs->opaque;
179 if (s->read_zeroes) {
180 qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
183 return null_aio_common(bs, cb, opaque);
186 static BlockAIOCB *null_aio_writev(BlockDriverState *bs,
187 int64_t sector_num, QEMUIOVector *qiov,
188 int nb_sectors,
189 BlockCompletionFunc *cb,
190 void *opaque)
192 return null_aio_common(bs, cb, opaque);
195 static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
196 BlockCompletionFunc *cb,
197 void *opaque)
199 return null_aio_common(bs, cb, opaque);
202 static int null_reopen_prepare(BDRVReopenState *reopen_state,
203 BlockReopenQueue *queue, Error **errp)
205 return 0;
208 static int64_t coroutine_fn null_co_get_block_status(BlockDriverState *bs,
209 int64_t sector_num,
210 int nb_sectors, int *pnum,
211 BlockDriverState **file)
213 BDRVNullState *s = bs->opaque;
214 off_t start = sector_num * BDRV_SECTOR_SIZE;
216 *pnum = nb_sectors;
217 *file = bs;
219 if (s->read_zeroes) {
220 return BDRV_BLOCK_OFFSET_VALID | start | BDRV_BLOCK_ZERO;
221 } else {
222 return BDRV_BLOCK_OFFSET_VALID | start;
226 static BlockDriver bdrv_null_co = {
227 .format_name = "null-co",
228 .protocol_name = "null-co",
229 .instance_size = sizeof(BDRVNullState),
231 .bdrv_file_open = null_file_open,
232 .bdrv_close = null_close,
233 .bdrv_getlength = null_getlength,
235 .bdrv_co_readv = null_co_readv,
236 .bdrv_co_writev = null_co_writev,
237 .bdrv_co_flush_to_disk = null_co_flush,
238 .bdrv_reopen_prepare = null_reopen_prepare,
240 .bdrv_co_get_block_status = null_co_get_block_status,
243 static BlockDriver bdrv_null_aio = {
244 .format_name = "null-aio",
245 .protocol_name = "null-aio",
246 .instance_size = sizeof(BDRVNullState),
248 .bdrv_file_open = null_file_open,
249 .bdrv_close = null_close,
250 .bdrv_getlength = null_getlength,
252 .bdrv_aio_readv = null_aio_readv,
253 .bdrv_aio_writev = null_aio_writev,
254 .bdrv_aio_flush = null_aio_flush,
255 .bdrv_reopen_prepare = null_reopen_prepare,
257 .bdrv_co_get_block_status = null_co_get_block_status,
260 static void bdrv_null_init(void)
262 bdrv_register(&bdrv_null_co);
263 bdrv_register(&bdrv_null_aio);
266 block_init(bdrv_null_init);