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 "block/block_int.h"
19 #define NULL_OPT_LATENCY "latency-ns"
20 #define NULL_OPT_ZEROES "read-zeroes"
28 static QemuOptsList runtime_opts
= {
30 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
33 .name
= BLOCK_OPT_SIZE
,
34 .type
= QEMU_OPT_SIZE
,
35 .help
= "size of the null block",
38 .name
= NULL_OPT_LATENCY
,
39 .type
= QEMU_OPT_NUMBER
,
40 .help
= "nanoseconds (approximated) to wait "
41 "before completing request",
44 .name
= NULL_OPT_ZEROES
,
45 .type
= QEMU_OPT_BOOL
,
46 .help
= "return zeroes when read",
52 static void null_co_parse_filename(const char *filename
, QDict
*options
,
55 /* This functions only exists so that a null-co:// filename is accepted
56 * with the null-co driver. */
57 if (strcmp(filename
, "null-co://")) {
58 error_setg(errp
, "The only allowed filename for this driver is "
64 static void null_aio_parse_filename(const char *filename
, QDict
*options
,
67 /* This functions only exists so that a null-aio:// filename is accepted
68 * with the null-aio driver. */
69 if (strcmp(filename
, "null-aio://")) {
70 error_setg(errp
, "The only allowed filename for this driver is "
76 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
80 BDRVNullState
*s
= bs
->opaque
;
83 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
84 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
86 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
88 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
89 if (s
->latency_ns
< 0) {
90 error_setg(errp
, "latency-ns is invalid");
93 s
->read_zeroes
= qemu_opt_get_bool(opts
, NULL_OPT_ZEROES
, false);
98 static void null_close(BlockDriverState
*bs
)
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_readv(BlockDriverState
*bs
,
119 int64_t sector_num
, int nb_sectors
,
122 BDRVNullState
*s
= bs
->opaque
;
124 if (s
->read_zeroes
) {
125 qemu_iovec_memset(qiov
, 0, 0, nb_sectors
* BDRV_SECTOR_SIZE
);
128 return null_co_common(bs
);
131 static coroutine_fn
int null_co_writev(BlockDriverState
*bs
,
132 int64_t sector_num
, int nb_sectors
,
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 aio_bh_schedule_oneshot(bdrv_get_aio_context(bs
), null_bh_cb
, acb
);
188 static BlockAIOCB
*null_aio_readv(BlockDriverState
*bs
,
189 int64_t sector_num
, QEMUIOVector
*qiov
,
191 BlockCompletionFunc
*cb
,
194 BDRVNullState
*s
= bs
->opaque
;
196 if (s
->read_zeroes
) {
197 qemu_iovec_memset(qiov
, 0, 0, nb_sectors
* BDRV_SECTOR_SIZE
);
200 return null_aio_common(bs
, cb
, opaque
);
203 static BlockAIOCB
*null_aio_writev(BlockDriverState
*bs
,
204 int64_t sector_num
, QEMUIOVector
*qiov
,
206 BlockCompletionFunc
*cb
,
209 return null_aio_common(bs
, cb
, opaque
);
212 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
213 BlockCompletionFunc
*cb
,
216 return null_aio_common(bs
, cb
, opaque
);
219 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
220 BlockReopenQueue
*queue
, Error
**errp
)
225 static int64_t coroutine_fn
null_co_get_block_status(BlockDriverState
*bs
,
227 int nb_sectors
, int *pnum
,
228 BlockDriverState
**file
)
230 BDRVNullState
*s
= bs
->opaque
;
231 off_t start
= sector_num
* BDRV_SECTOR_SIZE
;
236 if (s
->read_zeroes
) {
237 return BDRV_BLOCK_OFFSET_VALID
| start
| BDRV_BLOCK_ZERO
;
239 return BDRV_BLOCK_OFFSET_VALID
| start
;
243 static void null_refresh_filename(BlockDriverState
*bs
, QDict
*opts
)
246 qdict_del(opts
, "filename");
248 if (!qdict_size(opts
)) {
249 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
), "%s://",
250 bs
->drv
->format_name
);
253 qdict_put_str(opts
, "driver", bs
->drv
->format_name
);
254 bs
->full_open_options
= opts
;
257 static BlockDriver bdrv_null_co
= {
258 .format_name
= "null-co",
259 .protocol_name
= "null-co",
260 .instance_size
= sizeof(BDRVNullState
),
262 .bdrv_file_open
= null_file_open
,
263 .bdrv_parse_filename
= null_co_parse_filename
,
264 .bdrv_close
= null_close
,
265 .bdrv_getlength
= null_getlength
,
267 .bdrv_co_readv
= null_co_readv
,
268 .bdrv_co_writev
= null_co_writev
,
269 .bdrv_co_flush_to_disk
= null_co_flush
,
270 .bdrv_reopen_prepare
= null_reopen_prepare
,
272 .bdrv_co_get_block_status
= null_co_get_block_status
,
274 .bdrv_refresh_filename
= null_refresh_filename
,
277 static BlockDriver bdrv_null_aio
= {
278 .format_name
= "null-aio",
279 .protocol_name
= "null-aio",
280 .instance_size
= sizeof(BDRVNullState
),
282 .bdrv_file_open
= null_file_open
,
283 .bdrv_parse_filename
= null_aio_parse_filename
,
284 .bdrv_close
= null_close
,
285 .bdrv_getlength
= null_getlength
,
287 .bdrv_aio_readv
= null_aio_readv
,
288 .bdrv_aio_writev
= null_aio_writev
,
289 .bdrv_aio_flush
= null_aio_flush
,
290 .bdrv_reopen_prepare
= null_reopen_prepare
,
292 .bdrv_co_get_block_status
= null_co_get_block_status
,
294 .bdrv_refresh_filename
= null_refresh_filename
,
297 static void bdrv_null_init(void)
299 bdrv_register(&bdrv_null_co
);
300 bdrv_register(&bdrv_null_aio
);
303 block_init(bdrv_null_init
);