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 "qapi/qmp/qdict.h"
16 #include "qapi/qmp/qstring.h"
17 #include "qemu/module.h"
18 #include "qemu/option.h"
19 #include "block/block-io.h"
20 #include "block/block_int.h"
21 #include "sysemu/replay.h"
23 #define NULL_OPT_LATENCY "latency-ns"
24 #define NULL_OPT_ZEROES "read-zeroes"
32 static QemuOptsList runtime_opts
= {
34 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
37 .name
= BLOCK_OPT_SIZE
,
38 .type
= QEMU_OPT_SIZE
,
39 .help
= "size of the null block",
42 .name
= NULL_OPT_LATENCY
,
43 .type
= QEMU_OPT_NUMBER
,
44 .help
= "nanoseconds (approximated) to wait "
45 "before completing request",
48 .name
= NULL_OPT_ZEROES
,
49 .type
= QEMU_OPT_BOOL
,
50 .help
= "return zeroes when read",
56 static void null_co_parse_filename(const char *filename
, QDict
*options
,
59 /* This functions only exists so that a null-co:// filename is accepted
60 * with the null-co driver. */
61 if (strcmp(filename
, "null-co://")) {
62 error_setg(errp
, "The only allowed filename for this driver is "
68 static void null_aio_parse_filename(const char *filename
, QDict
*options
,
71 /* This functions only exists so that a null-aio:// filename is accepted
72 * with the null-aio driver. */
73 if (strcmp(filename
, "null-aio://")) {
74 error_setg(errp
, "The only allowed filename for this driver is "
80 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
84 BDRVNullState
*s
= bs
->opaque
;
87 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
88 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
90 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
92 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
93 if (s
->latency_ns
< 0) {
94 error_setg(errp
, "latency-ns is invalid");
97 s
->read_zeroes
= qemu_opt_get_bool(opts
, NULL_OPT_ZEROES
, false);
99 bs
->supported_write_flags
= BDRV_REQ_FUA
;
103 static int64_t coroutine_fn
null_co_getlength(BlockDriverState
*bs
)
105 BDRVNullState
*s
= bs
->opaque
;
109 static coroutine_fn
int null_co_common(BlockDriverState
*bs
)
111 BDRVNullState
*s
= bs
->opaque
;
114 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, s
->latency_ns
);
119 static coroutine_fn
int null_co_preadv(BlockDriverState
*bs
,
120 int64_t offset
, int64_t bytes
,
122 BdrvRequestFlags flags
)
124 BDRVNullState
*s
= bs
->opaque
;
126 if (s
->read_zeroes
) {
127 qemu_iovec_memset(qiov
, 0, 0, bytes
);
130 return null_co_common(bs
);
133 static coroutine_fn
int null_co_pwritev(BlockDriverState
*bs
,
134 int64_t offset
, int64_t bytes
,
136 BdrvRequestFlags flags
)
138 return null_co_common(bs
);
141 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
143 return null_co_common(bs
);
151 static const AIOCBInfo null_aiocb_info
= {
152 .aiocb_size
= sizeof(NullAIOCB
),
155 static void null_bh_cb(void *opaque
)
157 NullAIOCB
*acb
= opaque
;
158 acb
->common
.cb(acb
->common
.opaque
, 0);
162 static void null_timer_cb(void *opaque
)
164 NullAIOCB
*acb
= opaque
;
165 acb
->common
.cb(acb
->common
.opaque
, 0);
166 timer_deinit(&acb
->timer
);
170 static inline BlockAIOCB
*null_aio_common(BlockDriverState
*bs
,
171 BlockCompletionFunc
*cb
,
175 BDRVNullState
*s
= bs
->opaque
;
177 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
178 /* Only emulate latency after vcpu is running. */
180 aio_timer_init(bdrv_get_aio_context(bs
), &acb
->timer
,
181 QEMU_CLOCK_REALTIME
, SCALE_NS
,
183 timer_mod_ns(&acb
->timer
,
184 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + s
->latency_ns
);
186 replay_bh_schedule_oneshot_event(bdrv_get_aio_context(bs
),
192 static BlockAIOCB
*null_aio_preadv(BlockDriverState
*bs
,
193 int64_t offset
, int64_t bytes
,
194 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
195 BlockCompletionFunc
*cb
,
198 BDRVNullState
*s
= bs
->opaque
;
200 if (s
->read_zeroes
) {
201 qemu_iovec_memset(qiov
, 0, 0, bytes
);
204 return null_aio_common(bs
, cb
, opaque
);
207 static BlockAIOCB
*null_aio_pwritev(BlockDriverState
*bs
,
208 int64_t offset
, int64_t bytes
,
209 QEMUIOVector
*qiov
, BdrvRequestFlags flags
,
210 BlockCompletionFunc
*cb
,
213 return null_aio_common(bs
, cb
, opaque
);
216 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
217 BlockCompletionFunc
*cb
,
220 return null_aio_common(bs
, cb
, opaque
);
223 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
224 BlockReopenQueue
*queue
, Error
**errp
)
229 static int coroutine_fn
null_co_block_status(BlockDriverState
*bs
,
230 bool want_zero
, int64_t offset
,
231 int64_t bytes
, int64_t *pnum
,
233 BlockDriverState
**file
)
235 BDRVNullState
*s
= bs
->opaque
;
236 int ret
= BDRV_BLOCK_OFFSET_VALID
;
242 if (s
->read_zeroes
) {
243 ret
|= BDRV_BLOCK_ZERO
;
248 static void null_refresh_filename(BlockDriverState
*bs
)
252 for (e
= qdict_first(bs
->full_open_options
); e
;
253 e
= qdict_next(bs
->full_open_options
, e
))
255 /* These options can be ignored */
256 if (strcmp(qdict_entry_key(e
), "filename") &&
257 strcmp(qdict_entry_key(e
), "driver") &&
258 strcmp(qdict_entry_key(e
), NULL_OPT_LATENCY
))
264 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
), "%s://",
265 bs
->drv
->format_name
);
268 static int64_t coroutine_fn
269 null_co_get_allocated_file_size(BlockDriverState
*bs
)
274 static const char *const null_strong_runtime_opts
[] = {
281 static BlockDriver bdrv_null_co
= {
282 .format_name
= "null-co",
283 .protocol_name
= "null-co",
284 .instance_size
= sizeof(BDRVNullState
),
286 .bdrv_file_open
= null_file_open
,
287 .bdrv_parse_filename
= null_co_parse_filename
,
288 .bdrv_co_getlength
= null_co_getlength
,
289 .bdrv_co_get_allocated_file_size
= null_co_get_allocated_file_size
,
291 .bdrv_co_preadv
= null_co_preadv
,
292 .bdrv_co_pwritev
= null_co_pwritev
,
293 .bdrv_co_flush_to_disk
= null_co_flush
,
294 .bdrv_reopen_prepare
= null_reopen_prepare
,
296 .bdrv_co_block_status
= null_co_block_status
,
298 .bdrv_refresh_filename
= null_refresh_filename
,
299 .strong_runtime_opts
= null_strong_runtime_opts
,
302 static BlockDriver bdrv_null_aio
= {
303 .format_name
= "null-aio",
304 .protocol_name
= "null-aio",
305 .instance_size
= sizeof(BDRVNullState
),
307 .bdrv_file_open
= null_file_open
,
308 .bdrv_parse_filename
= null_aio_parse_filename
,
309 .bdrv_co_getlength
= null_co_getlength
,
310 .bdrv_co_get_allocated_file_size
= null_co_get_allocated_file_size
,
312 .bdrv_aio_preadv
= null_aio_preadv
,
313 .bdrv_aio_pwritev
= null_aio_pwritev
,
314 .bdrv_aio_flush
= null_aio_flush
,
315 .bdrv_reopen_prepare
= null_reopen_prepare
,
317 .bdrv_co_block_status
= null_co_block_status
,
319 .bdrv_refresh_filename
= null_refresh_filename
,
320 .strong_runtime_opts
= null_strong_runtime_opts
,
323 static void bdrv_null_init(void)
325 bdrv_register(&bdrv_null_co
);
326 bdrv_register(&bdrv_null_aio
);
329 block_init(bdrv_null_init
);