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_int.h"
20 #include "sysemu/replay.h"
22 #define NULL_OPT_LATENCY "latency-ns"
23 #define NULL_OPT_ZEROES "read-zeroes"
31 static QemuOptsList runtime_opts
= {
33 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
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 void null_co_parse_filename(const char *filename
, QDict
*options
,
58 /* This functions only exists so that a null-co:// filename is accepted
59 * with the null-co driver. */
60 if (strcmp(filename
, "null-co://")) {
61 error_setg(errp
, "The only allowed filename for this driver is "
67 static void null_aio_parse_filename(const char *filename
, QDict
*options
,
70 /* This functions only exists so that a null-aio:// filename is accepted
71 * with the null-aio driver. */
72 if (strcmp(filename
, "null-aio://")) {
73 error_setg(errp
, "The only allowed filename for this driver is "
79 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
83 BDRVNullState
*s
= bs
->opaque
;
86 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
87 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
89 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
91 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
92 if (s
->latency_ns
< 0) {
93 error_setg(errp
, "latency-ns is invalid");
96 s
->read_zeroes
= qemu_opt_get_bool(opts
, NULL_OPT_ZEROES
, false);
98 bs
->supported_write_flags
= BDRV_REQ_FUA
;
102 static int64_t null_getlength(BlockDriverState
*bs
)
104 BDRVNullState
*s
= bs
->opaque
;
108 static coroutine_fn
int null_co_common(BlockDriverState
*bs
)
110 BDRVNullState
*s
= bs
->opaque
;
113 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, s
->latency_ns
);
118 static coroutine_fn
int null_co_preadv(BlockDriverState
*bs
,
119 uint64_t offset
, uint64_t bytes
,
120 QEMUIOVector
*qiov
, int flags
)
122 BDRVNullState
*s
= bs
->opaque
;
124 if (s
->read_zeroes
) {
125 qemu_iovec_memset(qiov
, 0, 0, bytes
);
128 return null_co_common(bs
);
131 static coroutine_fn
int null_co_pwritev(BlockDriverState
*bs
,
132 uint64_t offset
, uint64_t bytes
,
133 QEMUIOVector
*qiov
, int flags
)
135 return null_co_common(bs
);
138 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
140 return null_co_common(bs
);
148 static const AIOCBInfo null_aiocb_info
= {
149 .aiocb_size
= sizeof(NullAIOCB
),
152 static void null_bh_cb(void *opaque
)
154 NullAIOCB
*acb
= opaque
;
155 acb
->common
.cb(acb
->common
.opaque
, 0);
159 static void null_timer_cb(void *opaque
)
161 NullAIOCB
*acb
= opaque
;
162 acb
->common
.cb(acb
->common
.opaque
, 0);
163 timer_deinit(&acb
->timer
);
167 static inline BlockAIOCB
*null_aio_common(BlockDriverState
*bs
,
168 BlockCompletionFunc
*cb
,
172 BDRVNullState
*s
= bs
->opaque
;
174 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
175 /* Only emulate latency after vcpu is running. */
177 aio_timer_init(bdrv_get_aio_context(bs
), &acb
->timer
,
178 QEMU_CLOCK_REALTIME
, SCALE_NS
,
180 timer_mod_ns(&acb
->timer
,
181 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + s
->latency_ns
);
183 replay_bh_schedule_oneshot_event(bdrv_get_aio_context(bs
),
189 static BlockAIOCB
*null_aio_preadv(BlockDriverState
*bs
,
190 uint64_t offset
, uint64_t bytes
,
191 QEMUIOVector
*qiov
, int flags
,
192 BlockCompletionFunc
*cb
,
195 BDRVNullState
*s
= bs
->opaque
;
197 if (s
->read_zeroes
) {
198 qemu_iovec_memset(qiov
, 0, 0, bytes
);
201 return null_aio_common(bs
, cb
, opaque
);
204 static BlockAIOCB
*null_aio_pwritev(BlockDriverState
*bs
,
205 uint64_t offset
, uint64_t bytes
,
206 QEMUIOVector
*qiov
, int flags
,
207 BlockCompletionFunc
*cb
,
210 return null_aio_common(bs
, cb
, opaque
);
213 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
214 BlockCompletionFunc
*cb
,
217 return null_aio_common(bs
, cb
, opaque
);
220 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
221 BlockReopenQueue
*queue
, Error
**errp
)
226 static int coroutine_fn
null_co_block_status(BlockDriverState
*bs
,
227 bool want_zero
, int64_t offset
,
228 int64_t bytes
, int64_t *pnum
,
230 BlockDriverState
**file
)
232 BDRVNullState
*s
= bs
->opaque
;
233 int ret
= BDRV_BLOCK_OFFSET_VALID
;
239 if (s
->read_zeroes
) {
240 ret
|= BDRV_BLOCK_ZERO
;
245 static void null_refresh_filename(BlockDriverState
*bs
)
249 for (e
= qdict_first(bs
->full_open_options
); e
;
250 e
= qdict_next(bs
->full_open_options
, e
))
252 /* These options can be ignored */
253 if (strcmp(qdict_entry_key(e
), "filename") &&
254 strcmp(qdict_entry_key(e
), "driver") &&
255 strcmp(qdict_entry_key(e
), NULL_OPT_LATENCY
))
261 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
), "%s://",
262 bs
->drv
->format_name
);
265 static int64_t null_allocated_file_size(BlockDriverState
*bs
)
270 static const char *const null_strong_runtime_opts
[] = {
277 static BlockDriver bdrv_null_co
= {
278 .format_name
= "null-co",
279 .protocol_name
= "null-co",
280 .instance_size
= sizeof(BDRVNullState
),
282 .bdrv_file_open
= null_file_open
,
283 .bdrv_parse_filename
= null_co_parse_filename
,
284 .bdrv_getlength
= null_getlength
,
285 .bdrv_get_allocated_file_size
= null_allocated_file_size
,
287 .bdrv_co_preadv
= null_co_preadv
,
288 .bdrv_co_pwritev
= null_co_pwritev
,
289 .bdrv_co_flush_to_disk
= null_co_flush
,
290 .bdrv_reopen_prepare
= null_reopen_prepare
,
292 .bdrv_co_block_status
= null_co_block_status
,
294 .bdrv_refresh_filename
= null_refresh_filename
,
295 .strong_runtime_opts
= null_strong_runtime_opts
,
298 static BlockDriver bdrv_null_aio
= {
299 .format_name
= "null-aio",
300 .protocol_name
= "null-aio",
301 .instance_size
= sizeof(BDRVNullState
),
303 .bdrv_file_open
= null_file_open
,
304 .bdrv_parse_filename
= null_aio_parse_filename
,
305 .bdrv_getlength
= null_getlength
,
306 .bdrv_get_allocated_file_size
= null_allocated_file_size
,
308 .bdrv_aio_preadv
= null_aio_preadv
,
309 .bdrv_aio_pwritev
= null_aio_pwritev
,
310 .bdrv_aio_flush
= null_aio_flush
,
311 .bdrv_reopen_prepare
= null_reopen_prepare
,
313 .bdrv_co_block_status
= null_co_block_status
,
315 .bdrv_refresh_filename
= null_refresh_filename
,
316 .strong_runtime_opts
= null_strong_runtime_opts
,
319 static void bdrv_null_init(void)
321 bdrv_register(&bdrv_null_co
);
322 bdrv_register(&bdrv_null_aio
);
325 block_init(bdrv_null_init
);