]> Git Repo - qemu.git/blame - block/null.c
block/null-{co,aio}: Allow reading zeroes
[qemu.git] / block / null.c
CommitLineData
e819ab22
FZ
1/*
2 * Null block driver
3 *
4 * Authors:
5 * Fam Zheng <[email protected]>
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"
e819ab22
FZ
15#include "block/block_int.h"
16
e5e51dd3 17#define NULL_OPT_LATENCY "latency-ns"
cd219eb1 18#define NULL_OPT_ZEROES "read-zeroes"
e5e51dd3 19
e819ab22
FZ
20typedef struct {
21 int64_t length;
e5e51dd3 22 int64_t latency_ns;
cd219eb1 23 bool read_zeroes;
e819ab22
FZ
24} BDRVNullState;
25
26static QemuOptsList runtime_opts = {
27 .name = "null",
28 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
29 .desc = {
30 {
31 .name = "filename",
32 .type = QEMU_OPT_STRING,
33 .help = "",
34 },
35 {
36 .name = BLOCK_OPT_SIZE,
37 .type = QEMU_OPT_SIZE,
38 .help = "size of the null block",
39 },
e5e51dd3
FZ
40 {
41 .name = NULL_OPT_LATENCY,
42 .type = QEMU_OPT_NUMBER,
43 .help = "nanoseconds (approximated) to wait "
44 "before completing request",
45 },
cd219eb1
HR
46 {
47 .name = NULL_OPT_ZEROES,
48 .type = QEMU_OPT_BOOL,
49 .help = "return zeroes when read",
50 },
e819ab22
FZ
51 { /* end of list */ }
52 },
53};
54
55static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
56 Error **errp)
57{
58 QemuOpts *opts;
59 BDRVNullState *s = bs->opaque;
e5e51dd3 60 int ret = 0;
e819ab22
FZ
61
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);
e5e51dd3
FZ
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;
71 }
cd219eb1 72 s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
e819ab22 73 qemu_opts_del(opts);
e5e51dd3 74 return ret;
e819ab22
FZ
75}
76
77static void null_close(BlockDriverState *bs)
78{
79}
80
81static int64_t null_getlength(BlockDriverState *bs)
82{
83 BDRVNullState *s = bs->opaque;
84 return s->length;
85}
86
e5e51dd3
FZ
87static coroutine_fn int null_co_common(BlockDriverState *bs)
88{
89 BDRVNullState *s = bs->opaque;
90
91 if (s->latency_ns) {
92 co_aio_sleep_ns(bdrv_get_aio_context(bs), QEMU_CLOCK_REALTIME,
93 s->latency_ns);
94 }
95 return 0;
96}
97
e819ab22
FZ
98static coroutine_fn int null_co_readv(BlockDriverState *bs,
99 int64_t sector_num, int nb_sectors,
100 QEMUIOVector *qiov)
101{
cd219eb1
HR
102 BDRVNullState *s = bs->opaque;
103
104 if (s->read_zeroes) {
105 qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
106 }
107
e5e51dd3 108 return null_co_common(bs);
e819ab22
FZ
109}
110
111static coroutine_fn int null_co_writev(BlockDriverState *bs,
112 int64_t sector_num, int nb_sectors,
113 QEMUIOVector *qiov)
114{
e5e51dd3 115 return null_co_common(bs);
e819ab22
FZ
116}
117
118static coroutine_fn int null_co_flush(BlockDriverState *bs)
119{
e5e51dd3 120 return null_co_common(bs);
e819ab22
FZ
121}
122
123typedef struct {
7c84b1b8 124 BlockAIOCB common;
e819ab22 125 QEMUBH *bh;
e5e51dd3 126 QEMUTimer timer;
e819ab22
FZ
127} NullAIOCB;
128
129static const AIOCBInfo null_aiocb_info = {
130 .aiocb_size = sizeof(NullAIOCB),
131};
132
133static void null_bh_cb(void *opaque)
134{
135 NullAIOCB *acb = opaque;
136 acb->common.cb(acb->common.opaque, 0);
137 qemu_bh_delete(acb->bh);
138 qemu_aio_unref(acb);
139}
140
e5e51dd3
FZ
141static void null_timer_cb(void *opaque)
142{
143 NullAIOCB *acb = opaque;
144 acb->common.cb(acb->common.opaque, 0);
145 timer_deinit(&acb->timer);
146 qemu_aio_unref(acb);
147}
148
7c84b1b8 149static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
097310b5 150 BlockCompletionFunc *cb,
7c84b1b8 151 void *opaque)
e819ab22
FZ
152{
153 NullAIOCB *acb;
e5e51dd3 154 BDRVNullState *s = bs->opaque;
e819ab22
FZ
155
156 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
e5e51dd3
FZ
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);
167 }
e819ab22
FZ
168 return &acb->common;
169}
170
7c84b1b8
MA
171static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
172 int64_t sector_num, QEMUIOVector *qiov,
173 int nb_sectors,
097310b5 174 BlockCompletionFunc *cb,
7c84b1b8 175 void *opaque)
e819ab22 176{
cd219eb1
HR
177 BDRVNullState *s = bs->opaque;
178
179 if (s->read_zeroes) {
180 qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE);
181 }
182
e819ab22
FZ
183 return null_aio_common(bs, cb, opaque);
184}
185
7c84b1b8
MA
186static BlockAIOCB *null_aio_writev(BlockDriverState *bs,
187 int64_t sector_num, QEMUIOVector *qiov,
188 int nb_sectors,
097310b5 189 BlockCompletionFunc *cb,
7c84b1b8 190 void *opaque)
e819ab22
FZ
191{
192 return null_aio_common(bs, cb, opaque);
193}
194
7c84b1b8 195static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
097310b5 196 BlockCompletionFunc *cb,
7c84b1b8 197 void *opaque)
e819ab22
FZ
198{
199 return null_aio_common(bs, cb, opaque);
200}
201
1c2b49a1
FZ
202static int null_reopen_prepare(BDRVReopenState *reopen_state,
203 BlockReopenQueue *queue, Error **errp)
204{
205 return 0;
206}
207
e819ab22
FZ
208static BlockDriver bdrv_null_co = {
209 .format_name = "null-co",
210 .protocol_name = "null-co",
211 .instance_size = sizeof(BDRVNullState),
212
213 .bdrv_file_open = null_file_open,
214 .bdrv_close = null_close,
215 .bdrv_getlength = null_getlength,
216
217 .bdrv_co_readv = null_co_readv,
218 .bdrv_co_writev = null_co_writev,
219 .bdrv_co_flush_to_disk = null_co_flush,
1c2b49a1 220 .bdrv_reopen_prepare = null_reopen_prepare,
e819ab22
FZ
221};
222
223static BlockDriver bdrv_null_aio = {
224 .format_name = "null-aio",
225 .protocol_name = "null-aio",
226 .instance_size = sizeof(BDRVNullState),
227
228 .bdrv_file_open = null_file_open,
229 .bdrv_close = null_close,
230 .bdrv_getlength = null_getlength,
231
232 .bdrv_aio_readv = null_aio_readv,
233 .bdrv_aio_writev = null_aio_writev,
234 .bdrv_aio_flush = null_aio_flush,
1c2b49a1 235 .bdrv_reopen_prepare = null_reopen_prepare,
e819ab22
FZ
236};
237
238static void bdrv_null_init(void)
239{
240 bdrv_register(&bdrv_null_co);
241 bdrv_register(&bdrv_null_aio);
242}
243
244block_init(bdrv_null_init);
This page took 0.104283 seconds and 4 git commands to generate.