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/option.h"
18 #include "block/block_int.h"
20 #define NULL_OPT_LATENCY "latency-ns"
21 #define NULL_OPT_ZEROES "read-zeroes"
29 static QemuOptsList runtime_opts
= {
31 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
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",
45 .name
= NULL_OPT_ZEROES
,
46 .type
= QEMU_OPT_BOOL
,
47 .help
= "return zeroes when read",
53 static void null_co_parse_filename(const char *filename
, QDict
*options
,
56 /* This functions only exists so that a null-co:// filename is accepted
57 * with the null-co driver. */
58 if (strcmp(filename
, "null-co://")) {
59 error_setg(errp
, "The only allowed filename for this driver is "
65 static void null_aio_parse_filename(const char *filename
, QDict
*options
,
68 /* This functions only exists so that a null-aio:// filename is accepted
69 * with the null-aio driver. */
70 if (strcmp(filename
, "null-aio://")) {
71 error_setg(errp
, "The only allowed filename for this driver is "
77 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
81 BDRVNullState
*s
= bs
->opaque
;
84 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
85 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
87 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
89 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
90 if (s
->latency_ns
< 0) {
91 error_setg(errp
, "latency-ns is invalid");
94 s
->read_zeroes
= qemu_opt_get_bool(opts
, NULL_OPT_ZEROES
, false);
96 bs
->supported_write_flags
= BDRV_REQ_FUA
;
100 static int64_t null_getlength(BlockDriverState
*bs
)
102 BDRVNullState
*s
= bs
->opaque
;
106 static coroutine_fn
int null_co_common(BlockDriverState
*bs
)
108 BDRVNullState
*s
= bs
->opaque
;
111 qemu_co_sleep_ns(QEMU_CLOCK_REALTIME
, s
->latency_ns
);
116 static coroutine_fn
int null_co_preadv(BlockDriverState
*bs
,
117 uint64_t offset
, uint64_t bytes
,
118 QEMUIOVector
*qiov
, int flags
)
120 BDRVNullState
*s
= bs
->opaque
;
122 if (s
->read_zeroes
) {
123 qemu_iovec_memset(qiov
, 0, 0, bytes
);
126 return null_co_common(bs
);
129 static coroutine_fn
int null_co_pwritev(BlockDriverState
*bs
,
130 uint64_t offset
, uint64_t bytes
,
131 QEMUIOVector
*qiov
, int flags
)
133 return null_co_common(bs
);
136 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
138 return null_co_common(bs
);
146 static const AIOCBInfo null_aiocb_info
= {
147 .aiocb_size
= sizeof(NullAIOCB
),
150 static void null_bh_cb(void *opaque
)
152 NullAIOCB
*acb
= opaque
;
153 acb
->common
.cb(acb
->common
.opaque
, 0);
157 static void null_timer_cb(void *opaque
)
159 NullAIOCB
*acb
= opaque
;
160 acb
->common
.cb(acb
->common
.opaque
, 0);
161 timer_deinit(&acb
->timer
);
165 static inline BlockAIOCB
*null_aio_common(BlockDriverState
*bs
,
166 BlockCompletionFunc
*cb
,
170 BDRVNullState
*s
= bs
->opaque
;
172 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
173 /* Only emulate latency after vcpu is running. */
175 aio_timer_init(bdrv_get_aio_context(bs
), &acb
->timer
,
176 QEMU_CLOCK_REALTIME
, SCALE_NS
,
178 timer_mod_ns(&acb
->timer
,
179 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + s
->latency_ns
);
181 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
), null_bh_cb
, acb
);
186 static BlockAIOCB
*null_aio_preadv(BlockDriverState
*bs
,
187 uint64_t offset
, uint64_t bytes
,
188 QEMUIOVector
*qiov
, int flags
,
189 BlockCompletionFunc
*cb
,
192 BDRVNullState
*s
= bs
->opaque
;
194 if (s
->read_zeroes
) {
195 qemu_iovec_memset(qiov
, 0, 0, bytes
);
198 return null_aio_common(bs
, cb
, opaque
);
201 static BlockAIOCB
*null_aio_pwritev(BlockDriverState
*bs
,
202 uint64_t offset
, uint64_t bytes
,
203 QEMUIOVector
*qiov
, int flags
,
204 BlockCompletionFunc
*cb
,
207 return null_aio_common(bs
, cb
, opaque
);
210 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
211 BlockCompletionFunc
*cb
,
214 return null_aio_common(bs
, cb
, opaque
);
217 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
218 BlockReopenQueue
*queue
, Error
**errp
)
223 static int coroutine_fn
null_co_block_status(BlockDriverState
*bs
,
224 bool want_zero
, int64_t offset
,
225 int64_t bytes
, int64_t *pnum
,
227 BlockDriverState
**file
)
229 BDRVNullState
*s
= bs
->opaque
;
230 int ret
= BDRV_BLOCK_OFFSET_VALID
;
236 if (s
->read_zeroes
) {
237 ret
|= BDRV_BLOCK_ZERO
;
242 static void null_refresh_filename(BlockDriverState
*bs
, QDict
*opts
)
244 qdict_del(opts
, "filename");
246 if (!qdict_size(opts
)) {
247 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
), "%s://",
248 bs
->drv
->format_name
);
251 qdict_put_str(opts
, "driver", bs
->drv
->format_name
);
252 bs
->full_open_options
= qobject_ref(opts
);
255 static BlockDriver bdrv_null_co
= {
256 .format_name
= "null-co",
257 .protocol_name
= "null-co",
258 .instance_size
= sizeof(BDRVNullState
),
260 .bdrv_file_open
= null_file_open
,
261 .bdrv_parse_filename
= null_co_parse_filename
,
262 .bdrv_getlength
= null_getlength
,
264 .bdrv_co_preadv
= null_co_preadv
,
265 .bdrv_co_pwritev
= null_co_pwritev
,
266 .bdrv_co_flush_to_disk
= null_co_flush
,
267 .bdrv_reopen_prepare
= null_reopen_prepare
,
269 .bdrv_co_block_status
= null_co_block_status
,
271 .bdrv_refresh_filename
= null_refresh_filename
,
274 static BlockDriver bdrv_null_aio
= {
275 .format_name
= "null-aio",
276 .protocol_name
= "null-aio",
277 .instance_size
= sizeof(BDRVNullState
),
279 .bdrv_file_open
= null_file_open
,
280 .bdrv_parse_filename
= null_aio_parse_filename
,
281 .bdrv_getlength
= null_getlength
,
283 .bdrv_aio_preadv
= null_aio_preadv
,
284 .bdrv_aio_pwritev
= null_aio_pwritev
,
285 .bdrv_aio_flush
= null_aio_flush
,
286 .bdrv_reopen_prepare
= null_reopen_prepare
,
288 .bdrv_co_block_status
= null_co_block_status
,
290 .bdrv_refresh_filename
= null_refresh_filename
,
293 static void bdrv_null_init(void)
295 bdrv_register(&bdrv_null_co
);
296 bdrv_register(&bdrv_null_aio
);
299 block_init(bdrv_null_init
);