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 "block/block_int.h"
16 #define NULL_OPT_LATENCY "latency-ns"
23 static QemuOptsList runtime_opts
= {
25 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
29 .type
= QEMU_OPT_STRING
,
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",
47 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
51 BDRVNullState
*s
= bs
->opaque
;
54 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
55 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
57 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
59 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
60 if (s
->latency_ns
< 0) {
61 error_setg(errp
, "latency-ns is invalid");
68 static void null_close(BlockDriverState
*bs
)
72 static int64_t null_getlength(BlockDriverState
*bs
)
74 BDRVNullState
*s
= bs
->opaque
;
78 static coroutine_fn
int null_co_common(BlockDriverState
*bs
)
80 BDRVNullState
*s
= bs
->opaque
;
83 co_aio_sleep_ns(bdrv_get_aio_context(bs
), QEMU_CLOCK_REALTIME
,
89 static coroutine_fn
int null_co_readv(BlockDriverState
*bs
,
90 int64_t sector_num
, int nb_sectors
,
93 return null_co_common(bs
);
96 static coroutine_fn
int null_co_writev(BlockDriverState
*bs
,
97 int64_t sector_num
, int nb_sectors
,
100 return null_co_common(bs
);
103 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
105 return null_co_common(bs
);
114 static const AIOCBInfo null_aiocb_info
= {
115 .aiocb_size
= sizeof(NullAIOCB
),
118 static void null_bh_cb(void *opaque
)
120 NullAIOCB
*acb
= opaque
;
121 acb
->common
.cb(acb
->common
.opaque
, 0);
122 qemu_bh_delete(acb
->bh
);
126 static void null_timer_cb(void *opaque
)
128 NullAIOCB
*acb
= opaque
;
129 acb
->common
.cb(acb
->common
.opaque
, 0);
130 timer_deinit(&acb
->timer
);
134 static inline BlockAIOCB
*null_aio_common(BlockDriverState
*bs
,
135 BlockCompletionFunc
*cb
,
139 BDRVNullState
*s
= bs
->opaque
;
141 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
142 /* Only emulate latency after vcpu is running. */
144 aio_timer_init(bdrv_get_aio_context(bs
), &acb
->timer
,
145 QEMU_CLOCK_REALTIME
, SCALE_NS
,
147 timer_mod_ns(&acb
->timer
,
148 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + s
->latency_ns
);
150 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), null_bh_cb
, acb
);
151 qemu_bh_schedule(acb
->bh
);
156 static BlockAIOCB
*null_aio_readv(BlockDriverState
*bs
,
157 int64_t sector_num
, QEMUIOVector
*qiov
,
159 BlockCompletionFunc
*cb
,
162 return null_aio_common(bs
, cb
, opaque
);
165 static BlockAIOCB
*null_aio_writev(BlockDriverState
*bs
,
166 int64_t sector_num
, QEMUIOVector
*qiov
,
168 BlockCompletionFunc
*cb
,
171 return null_aio_common(bs
, cb
, opaque
);
174 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
175 BlockCompletionFunc
*cb
,
178 return null_aio_common(bs
, cb
, opaque
);
181 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
182 BlockReopenQueue
*queue
, Error
**errp
)
187 static BlockDriver bdrv_null_co
= {
188 .format_name
= "null-co",
189 .protocol_name
= "null-co",
190 .instance_size
= sizeof(BDRVNullState
),
192 .bdrv_file_open
= null_file_open
,
193 .bdrv_close
= null_close
,
194 .bdrv_getlength
= null_getlength
,
196 .bdrv_co_readv
= null_co_readv
,
197 .bdrv_co_writev
= null_co_writev
,
198 .bdrv_co_flush_to_disk
= null_co_flush
,
199 .bdrv_reopen_prepare
= null_reopen_prepare
,
202 static BlockDriver bdrv_null_aio
= {
203 .format_name
= "null-aio",
204 .protocol_name
= "null-aio",
205 .instance_size
= sizeof(BDRVNullState
),
207 .bdrv_file_open
= null_file_open
,
208 .bdrv_close
= null_close
,
209 .bdrv_getlength
= null_getlength
,
211 .bdrv_aio_readv
= null_aio_readv
,
212 .bdrv_aio_writev
= null_aio_writev
,
213 .bdrv_aio_flush
= null_aio_flush
,
214 .bdrv_reopen_prepare
= null_reopen_prepare
,
217 static void bdrv_null_init(void)
219 bdrv_register(&bdrv_null_co
);
220 bdrv_register(&bdrv_null_aio
);
223 block_init(bdrv_null_init
);