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 "block/block_int.h"
15 #define NULL_OPT_LATENCY "latency-ns"
22 static QemuOptsList runtime_opts
= {
24 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
28 .type
= QEMU_OPT_STRING
,
32 .name
= BLOCK_OPT_SIZE
,
33 .type
= QEMU_OPT_SIZE
,
34 .help
= "size of the null block",
37 .name
= NULL_OPT_LATENCY
,
38 .type
= QEMU_OPT_NUMBER
,
39 .help
= "nanoseconds (approximated) to wait "
40 "before completing request",
46 static int null_file_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
50 BDRVNullState
*s
= bs
->opaque
;
53 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
54 qemu_opts_absorb_qdict(opts
, options
, &error_abort
);
56 qemu_opt_get_size(opts
, BLOCK_OPT_SIZE
, 1 << 30);
58 qemu_opt_get_number(opts
, NULL_OPT_LATENCY
, 0);
59 if (s
->latency_ns
< 0) {
60 error_setg(errp
, "latency-ns is invalid");
67 static void null_close(BlockDriverState
*bs
)
71 static int64_t null_getlength(BlockDriverState
*bs
)
73 BDRVNullState
*s
= bs
->opaque
;
77 static coroutine_fn
int null_co_common(BlockDriverState
*bs
)
79 BDRVNullState
*s
= bs
->opaque
;
82 co_aio_sleep_ns(bdrv_get_aio_context(bs
), QEMU_CLOCK_REALTIME
,
88 static coroutine_fn
int null_co_readv(BlockDriverState
*bs
,
89 int64_t sector_num
, int nb_sectors
,
92 return null_co_common(bs
);
95 static coroutine_fn
int null_co_writev(BlockDriverState
*bs
,
96 int64_t sector_num
, int nb_sectors
,
99 return null_co_common(bs
);
102 static coroutine_fn
int null_co_flush(BlockDriverState
*bs
)
104 return null_co_common(bs
);
113 static const AIOCBInfo null_aiocb_info
= {
114 .aiocb_size
= sizeof(NullAIOCB
),
117 static void null_bh_cb(void *opaque
)
119 NullAIOCB
*acb
= opaque
;
120 acb
->common
.cb(acb
->common
.opaque
, 0);
121 qemu_bh_delete(acb
->bh
);
125 static void null_timer_cb(void *opaque
)
127 NullAIOCB
*acb
= opaque
;
128 acb
->common
.cb(acb
->common
.opaque
, 0);
129 timer_deinit(&acb
->timer
);
133 static inline BlockAIOCB
*null_aio_common(BlockDriverState
*bs
,
134 BlockCompletionFunc
*cb
,
138 BDRVNullState
*s
= bs
->opaque
;
140 acb
= qemu_aio_get(&null_aiocb_info
, bs
, cb
, opaque
);
141 /* Only emulate latency after vcpu is running. */
143 aio_timer_init(bdrv_get_aio_context(bs
), &acb
->timer
,
144 QEMU_CLOCK_REALTIME
, SCALE_NS
,
146 timer_mod_ns(&acb
->timer
,
147 qemu_clock_get_ns(QEMU_CLOCK_REALTIME
) + s
->latency_ns
);
149 acb
->bh
= aio_bh_new(bdrv_get_aio_context(bs
), null_bh_cb
, acb
);
150 qemu_bh_schedule(acb
->bh
);
155 static BlockAIOCB
*null_aio_readv(BlockDriverState
*bs
,
156 int64_t sector_num
, QEMUIOVector
*qiov
,
158 BlockCompletionFunc
*cb
,
161 return null_aio_common(bs
, cb
, opaque
);
164 static BlockAIOCB
*null_aio_writev(BlockDriverState
*bs
,
165 int64_t sector_num
, QEMUIOVector
*qiov
,
167 BlockCompletionFunc
*cb
,
170 return null_aio_common(bs
, cb
, opaque
);
173 static BlockAIOCB
*null_aio_flush(BlockDriverState
*bs
,
174 BlockCompletionFunc
*cb
,
177 return null_aio_common(bs
, cb
, opaque
);
180 static int null_reopen_prepare(BDRVReopenState
*reopen_state
,
181 BlockReopenQueue
*queue
, Error
**errp
)
186 static BlockDriver bdrv_null_co
= {
187 .format_name
= "null-co",
188 .protocol_name
= "null-co",
189 .instance_size
= sizeof(BDRVNullState
),
191 .bdrv_file_open
= null_file_open
,
192 .bdrv_close
= null_close
,
193 .bdrv_getlength
= null_getlength
,
195 .bdrv_co_readv
= null_co_readv
,
196 .bdrv_co_writev
= null_co_writev
,
197 .bdrv_co_flush_to_disk
= null_co_flush
,
198 .bdrv_reopen_prepare
= null_reopen_prepare
,
201 static BlockDriver bdrv_null_aio
= {
202 .format_name
= "null-aio",
203 .protocol_name
= "null-aio",
204 .instance_size
= sizeof(BDRVNullState
),
206 .bdrv_file_open
= null_file_open
,
207 .bdrv_close
= null_close
,
208 .bdrv_getlength
= null_getlength
,
210 .bdrv_aio_readv
= null_aio_readv
,
211 .bdrv_aio_writev
= null_aio_writev
,
212 .bdrv_aio_flush
= null_aio_flush
,
213 .bdrv_reopen_prepare
= null_reopen_prepare
,
216 static void bdrv_null_init(void)
218 bdrv_register(&bdrv_null_co
);
219 bdrv_register(&bdrv_null_aio
);
222 block_init(bdrv_null_init
);