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
),
34 .type
= QEMU_OPT_STRING
,
38 .name
= BLOCK_OPT_SIZE
,
39 .type
= QEMU_OPT_SIZE
,
40 .help
= "size of the null block",
43 .name
= NULL_OPT_LATENCY
,
44 .type
= QEMU_OPT_NUMBER
,
45 .help
= "nanoseconds (approximated) to wait "
46 "before completing request",
49 .name
= NULL_OPT_ZEROES
,
50 .type
= QEMU_OPT_BOOL
,
51 .help
= "return zeroes when read",
57 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
61 BDRVNullState
*s
= bs
->opaque
;
64 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
65 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
67 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
69 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
70 if (s
->latency_ns
< 0) {
71 error_setg(errp
, "latency-ns is invalid");
74 s
->read_zeroes
= qemu_opt_get_bool(opts
, NULL_OPT_ZEROES
, false);
79 static void null_close(BlockDriverState
*bs
)
83 static int64_t null_getlength(BlockDriverState
*bs
)
85 BDRVNullState
*s
= bs
->opaque
;
89 static coroutine_fn
int null_co_common(BlockDriverState
*bs
)
91 BDRVNullState
*s
= bs
->opaque
;
94 co_aio_sleep_ns(bdrv_get_aio_context(bs
), QEMU_CLOCK_REALTIME
,
100 static coroutine_fn
int null_co_readv(BlockDriverState
*bs
,
101 int64_t sector_num
, int nb_sectors
,
104 BDRVNullState
*s
= bs
->opaque
;
106 if (s
->read_zeroes
) {
107 qemu_iovec_memset(qiov
, 0, 0, nb_sectors
* BDRV_SECTOR_SIZE
);
110 return null_co_common(bs
);
113 static coroutine_fn
int null_co_writev(BlockDriverState
*bs
,
114 int64_t sector_num
, int nb_sectors
,
117 return null_co_common(bs
);
120 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
122 return null_co_common(bs
);
131 static const AIOCBInfo null_aiocb_info
= {
132 .aiocb_size
= sizeof(NullAIOCB
),
135 static void null_bh_cb(void *opaque
)
137 NullAIOCB
*acb
= opaque
;
138 acb
->common
.cb(acb
->common
.opaque
, 0);
139 qemu_bh_delete(acb
->bh
);
143 static void null_timer_cb(void *opaque
)
145 NullAIOCB
*acb
= opaque
;
146 acb
->common
.cb(acb
->common
.opaque
, 0);
147 timer_deinit(&acb
->timer
);
151 static inline BlockAIOCB
*null_aio_common(BlockDriverState
*bs
,
152 BlockCompletionFunc
*cb
,
156 BDRVNullState
*s
= bs
->opaque
;
158 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
159 /* Only emulate latency after vcpu is running. */
161 aio_timer_init(bdrv_get_aio_context(bs
), &acb
->timer
,
162 QEMU_CLOCK_REALTIME
, SCALE_NS
,
164 timer_mod_ns(&acb
->timer
,
165 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + s
->latency_ns
);
167 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), null_bh_cb
, acb
);
168 qemu_bh_schedule(acb
->bh
);
173 static BlockAIOCB
*null_aio_readv(BlockDriverState
*bs
,
174 int64_t sector_num
, QEMUIOVector
*qiov
,
176 BlockCompletionFunc
*cb
,
179 BDRVNullState
*s
= bs
->opaque
;
181 if (s
->read_zeroes
) {
182 qemu_iovec_memset(qiov
, 0, 0, nb_sectors
* BDRV_SECTOR_SIZE
);
185 return null_aio_common(bs
, cb
, opaque
);
188 static BlockAIOCB
*null_aio_writev(BlockDriverState
*bs
,
189 int64_t sector_num
, QEMUIOVector
*qiov
,
191 BlockCompletionFunc
*cb
,
194 return null_aio_common(bs
, cb
, opaque
);
197 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
198 BlockCompletionFunc
*cb
,
201 return null_aio_common(bs
, cb
, opaque
);
204 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
205 BlockReopenQueue
*queue
, Error
**errp
)
210 static int64_t coroutine_fn
null_co_get_block_status(BlockDriverState
*bs
,
212 int nb_sectors
, int *pnum
,
213 BlockDriverState
**file
)
215 BDRVNullState
*s
= bs
->opaque
;
216 off_t start
= sector_num
* BDRV_SECTOR_SIZE
;
221 if (s
->read_zeroes
) {
222 return BDRV_BLOCK_OFFSET_VALID
| start
| BDRV_BLOCK_ZERO
;
224 return BDRV_BLOCK_OFFSET_VALID
| start
;
228 static void null_refresh_filename(BlockDriverState
*bs
, QDict
*opts
)
231 qdict_del(opts
, "filename");
233 if (!qdict_size(opts
)) {
234 snprintf(bs
->exact_filename
, sizeof(bs
->exact_filename
), "%s://",
235 bs
->drv
->format_name
);
238 qdict_put(opts
, "driver", qstring_from_str(bs
->drv
->format_name
));
239 bs
->full_open_options
= opts
;
242 static BlockDriver bdrv_null_co
= {
243 .format_name
= "null-co",
244 .protocol_name
= "null-co",
245 .instance_size
= sizeof(BDRVNullState
),
247 .bdrv_file_open
= null_file_open
,
248 .bdrv_close
= null_close
,
249 .bdrv_getlength
= null_getlength
,
251 .bdrv_co_readv
= null_co_readv
,
252 .bdrv_co_writev
= null_co_writev
,
253 .bdrv_co_flush_to_disk
= null_co_flush
,
254 .bdrv_reopen_prepare
= null_reopen_prepare
,
256 .bdrv_co_get_block_status
= null_co_get_block_status
,
258 .bdrv_refresh_filename
= null_refresh_filename
,
261 static BlockDriver bdrv_null_aio
= {
262 .format_name
= "null-aio",
263 .protocol_name
= "null-aio",
264 .instance_size
= sizeof(BDRVNullState
),
266 .bdrv_file_open
= null_file_open
,
267 .bdrv_close
= null_close
,
268 .bdrv_getlength
= null_getlength
,
270 .bdrv_aio_readv
= null_aio_readv
,
271 .bdrv_aio_writev
= null_aio_writev
,
272 .bdrv_aio_flush
= null_aio_flush
,
273 .bdrv_reopen_prepare
= null_reopen_prepare
,
275 .bdrv_co_get_block_status
= null_co_get_block_status
,
277 .bdrv_refresh_filename
= null_refresh_filename
,
280 static void bdrv_null_init(void)
282 bdrv_register(&bdrv_null_co
);
283 bdrv_register(&bdrv_null_aio
);
286 block_init(bdrv_null_init
);