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"
21 #define NULL_OPT_LATENCY "latency-ns"
22 #define NULL_OPT_ZEROES "read-zeroes"
30 static QemuOptsList runtime_opts
= {
32 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
35 .name
= BLOCK_OPT_SIZE
,
36 .type
= QEMU_OPT_SIZE
,
37 .help
= "size of the null block",
40 .name
= NULL_OPT_LATENCY
,
41 .type
= QEMU_OPT_NUMBER
,
42 .help
= "nanoseconds (approximated) to wait "
43 "before completing request",
46 .name
= NULL_OPT_ZEROES
,
47 .type
= QEMU_OPT_BOOL
,
48 .help
= "return zeroes when read",
54 static void null_co_parse_filename(const char *filename
, QDict
*options
,
57 /* This functions only exists so that a null-co:// filename is accepted
58 * with the null-co driver. */
59 if (strcmp(filename
, "null-co://")) {
60 error_setg(errp
, "The only allowed filename for this driver is "
66 static void null_aio_parse_filename(const char *filename
, QDict
*options
,
69 /* This functions only exists so that a null-aio:// filename is accepted
70 * with the null-aio driver. */
71 if (strcmp(filename
, "null-aio://")) {
72 error_setg(errp
, "The only allowed filename for this driver is "
78 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
82 BDRVNullState
*s
= bs
->opaque
;
85 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
86 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
88 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
90 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
91 if (s
->latency_ns
< 0) {
92 error_setg(errp
, "latency-ns is invalid");
95 s
->read_zeroes
= qemu_opt_get_bool(opts
, NULL_OPT_ZEROES
, false);
97 bs
->supported_write_flags
= BDRV_REQ_FUA
;
101 static int64_t null_getlength(BlockDriverState
*bs
)
103 BDRVNullState
*s
= bs
->opaque
;
107 static coroutine_fn
int null_co_common(BlockDriverState
*bs
)
109 BDRVNullState
*s
= bs
->opaque
;
112 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, s
->latency_ns
);
117 static coroutine_fn
int null_co_preadv(BlockDriverState
*bs
,
118 uint64_t offset
, uint64_t bytes
,
119 QEMUIOVector
*qiov
, int flags
)
121 BDRVNullState
*s
= bs
->opaque
;
123 if (s
->read_zeroes
) {
124 qemu_iovec_memset(qiov
, 0, 0, bytes
);
127 return null_co_common(bs
);
130 static coroutine_fn
int null_co_pwritev(BlockDriverState
*bs
,
131 uint64_t offset
, uint64_t bytes
,
132 QEMUIOVector
*qiov
, int flags
)
134 return null_co_common(bs
);
137 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
139 return null_co_common(bs
);
147 static const AIOCBInfo null_aiocb_info
= {
148 .aiocb_size
= sizeof(NullAIOCB
),
151 static void null_bh_cb(void *opaque
)
153 NullAIOCB
*acb
= opaque
;
154 acb
->common
.cb(acb
->common
.opaque
, 0);
158 static void null_timer_cb(void *opaque
)
160 NullAIOCB
*acb
= opaque
;
161 acb
->common
.cb(acb
->common
.opaque
, 0);
162 timer_deinit(&acb
->timer
);
166 static inline BlockAIOCB
*null_aio_common(BlockDriverState
*bs
,
167 BlockCompletionFunc
*cb
,
171 BDRVNullState
*s
= bs
->opaque
;
173 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
174 /* Only emulate latency after vcpu is running. */
176 aio_timer_init(bdrv_get_aio_context(bs
), &acb
->timer
,
177 QEMU_CLOCK_REALTIME
, SCALE_NS
,
179 timer_mod_ns(&acb
->timer
,
180 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + s
->latency_ns
);
182 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
), null_bh_cb
, acb
);
187 static BlockAIOCB
*null_aio_preadv(BlockDriverState
*bs
,
188 uint64_t offset
, uint64_t bytes
,
189 QEMUIOVector
*qiov
, int flags
,
190 BlockCompletionFunc
*cb
,
193 BDRVNullState
*s
= bs
->opaque
;
195 if (s
->read_zeroes
) {
196 qemu_iovec_memset(qiov
, 0, 0, bytes
);
199 return null_aio_common(bs
, cb
, opaque
);
202 static BlockAIOCB
*null_aio_pwritev(BlockDriverState
*bs
,
203 uint64_t offset
, uint64_t bytes
,
204 QEMUIOVector
*qiov
, int flags
,
205 BlockCompletionFunc
*cb
,
208 return null_aio_common(bs
, cb
, opaque
);
211 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
212 BlockCompletionFunc
*cb
,
215 return null_aio_common(bs
, cb
, opaque
);
218 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
219 BlockReopenQueue
*queue
, Error
**errp
)
224 static int coroutine_fn
null_co_block_status(BlockDriverState
*bs
,
225 bool want_zero
, int64_t offset
,
226 int64_t bytes
, int64_t *pnum
,
228 BlockDriverState
**file
)
230 BDRVNullState
*s
= bs
->opaque
;
231 int ret
= BDRV_BLOCK_OFFSET_VALID
;
237 if (s
->read_zeroes
) {
238 ret
|= BDRV_BLOCK_ZERO
;
243 static void null_refresh_filename(BlockDriverState
*bs
)
247 for (e
= qdict_first(bs
->full_open_options
); e
;
248 e
= qdict_next(bs
->full_open_options
, e
))
250 /* These options can be ignored */
251 if (strcmp(qdict_entry_key(e
), "filename") &&
252 strcmp(qdict_entry_key(e
), "driver") &&
253 strcmp(qdict_entry_key(e
), NULL_OPT_LATENCY
))
259 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
), "%s://",
260 bs
->drv
->format_name
);
263 static const char *const null_strong_runtime_opts
[] = {
270 static BlockDriver bdrv_null_co
= {
271 .format_name
= "null-co",
272 .protocol_name
= "null-co",
273 .instance_size
= sizeof(BDRVNullState
),
275 .bdrv_file_open
= null_file_open
,
276 .bdrv_parse_filename
= null_co_parse_filename
,
277 .bdrv_getlength
= null_getlength
,
279 .bdrv_co_preadv
= null_co_preadv
,
280 .bdrv_co_pwritev
= null_co_pwritev
,
281 .bdrv_co_flush_to_disk
= null_co_flush
,
282 .bdrv_reopen_prepare
= null_reopen_prepare
,
284 .bdrv_co_block_status
= null_co_block_status
,
286 .bdrv_refresh_filename
= null_refresh_filename
,
287 .strong_runtime_opts
= null_strong_runtime_opts
,
290 static BlockDriver bdrv_null_aio
= {
291 .format_name
= "null-aio",
292 .protocol_name
= "null-aio",
293 .instance_size
= sizeof(BDRVNullState
),
295 .bdrv_file_open
= null_file_open
,
296 .bdrv_parse_filename
= null_aio_parse_filename
,
297 .bdrv_getlength
= null_getlength
,
299 .bdrv_aio_preadv
= null_aio_preadv
,
300 .bdrv_aio_pwritev
= null_aio_pwritev
,
301 .bdrv_aio_flush
= null_aio_flush
,
302 .bdrv_reopen_prepare
= null_reopen_prepare
,
304 .bdrv_co_block_status
= null_co_block_status
,
306 .bdrv_refresh_filename
= null_refresh_filename
,
307 .strong_runtime_opts
= null_strong_runtime_opts
,
310 static void bdrv_null_init(void)
312 bdrv_register(&bdrv_null_co
);
313 bdrv_register(&bdrv_null_aio
);
316 block_init(bdrv_null_init
);