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"
24 static QemuOptsList runtime_opts
= {
26 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
30 .type
= QEMU_OPT_STRING
,
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",
48 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
52 BDRVNullState
*s
= bs
->opaque
;
55 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
56 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
58 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
60 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
61 if (s
->latency_ns
< 0) {
62 error_setg(errp
, "latency-ns is invalid");
69 static void null_close(BlockDriverState
*bs
)
73 static int64_t null_getlength(BlockDriverState
*bs
)
75 BDRVNullState
*s
= bs
->opaque
;
79 static coroutine_fn
int null_co_common(BlockDriverState
*bs
)
81 BDRVNullState
*s
= bs
->opaque
;
84 co_aio_sleep_ns(bdrv_get_aio_context(bs
), QEMU_CLOCK_REALTIME
,
90 static coroutine_fn
int null_co_readv(BlockDriverState
*bs
,
91 int64_t sector_num
, int nb_sectors
,
94 return null_co_common(bs
);
97 static coroutine_fn
int null_co_writev(BlockDriverState
*bs
,
98 int64_t sector_num
, int nb_sectors
,
101 return null_co_common(bs
);
104 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
106 return null_co_common(bs
);
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
);
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
);
135 static inline BlockAIOCB
*null_aio_common(BlockDriverState
*bs
,
136 BlockCompletionFunc
*cb
,
140 BDRVNullState
*s
= bs
->opaque
;
142 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
143 /* Only emulate latency after vcpu is running. */
145 aio_timer_init(bdrv_get_aio_context(bs
), &acb
->timer
,
146 QEMU_CLOCK_REALTIME
, SCALE_NS
,
148 timer_mod_ns(&acb
->timer
,
149 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + s
->latency_ns
);
151 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), null_bh_cb
, acb
);
152 qemu_bh_schedule(acb
->bh
);
157 static BlockAIOCB
*null_aio_readv(BlockDriverState
*bs
,
158 int64_t sector_num
, QEMUIOVector
*qiov
,
160 BlockCompletionFunc
*cb
,
163 return null_aio_common(bs
, cb
, opaque
);
166 static BlockAIOCB
*null_aio_writev(BlockDriverState
*bs
,
167 int64_t sector_num
, QEMUIOVector
*qiov
,
169 BlockCompletionFunc
*cb
,
172 return null_aio_common(bs
, cb
, opaque
);
175 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
176 BlockCompletionFunc
*cb
,
179 return null_aio_common(bs
, cb
, opaque
);
182 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
183 BlockReopenQueue
*queue
, Error
**errp
)
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
);