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"
26 static QemuOptsList runtime_opts = {
28 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
32 .type = QEMU_OPT_STRING,
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",
55 static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
59 BDRVNullState *s = bs->opaque;
62 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
63 qemu_opts_absorb_qdict(opts, options, &error_abort);
65 qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
67 qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0);
68 if (s->latency_ns < 0) {
69 error_setg(errp, "latency-ns is invalid");
72 s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
77 static void null_close(BlockDriverState *bs)
81 static int64_t null_getlength(BlockDriverState *bs)
83 BDRVNullState *s = bs->opaque;
87 static coroutine_fn int null_co_common(BlockDriverState *bs)
89 BDRVNullState *s = bs->opaque;
92 co_aio_sleep_ns(bdrv_get_aio_context(bs), QEMU_CLOCK_REALTIME,
98 static coroutine_fn int null_co_readv(BlockDriverState *bs,
99 int64_t sector_num, int nb_sectors,
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,
115 return null_co_common(bs);
118 static coroutine_fn int null_co_flush(BlockDriverState *bs)
120 return null_co_common(bs);
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);
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);
149 static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
150 BlockCompletionFunc *cb,
154 BDRVNullState *s = bs->opaque;
156 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
157 /* Only emulate latency after vcpu is running. */
159 aio_timer_init(bdrv_get_aio_context(bs), &acb->timer,
160 QEMU_CLOCK_REALTIME, SCALE_NS,
162 timer_mod_ns(&acb->timer,
163 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
165 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
166 qemu_bh_schedule(acb->bh);
171 static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
172 int64_t sector_num, QEMUIOVector *qiov,
174 BlockCompletionFunc *cb,
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,
189 BlockCompletionFunc *cb,
192 return null_aio_common(bs, cb, opaque);
195 static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
196 BlockCompletionFunc *cb,
199 return null_aio_common(bs, cb, opaque);
202 static int null_reopen_prepare(BDRVReopenState *reopen_state,
203 BlockReopenQueue *queue, Error **errp)
208 static int64_t coroutine_fn null_co_get_block_status(BlockDriverState *bs,
210 int nb_sectors, int *pnum,
211 BlockDriverState **file)
213 BDRVNullState *s = bs->opaque;
214 off_t start = sector_num * BDRV_SECTOR_SIZE;
219 if (s->read_zeroes) {
220 return BDRV_BLOCK_OFFSET_VALID | start | BDRV_BLOCK_ZERO;
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);