]> Git Repo - qemu.git/blame - block/null.c
qcow2: Implement .bdrv_inactivate
[qemu.git] / block / null.c
CommitLineData
e819ab22
FZ
1/*
2 * Null block driver
3 *
4 * Authors:
5 * Fam Zheng <[email protected]>
6 *
7 * Copyright (C) 2014 Red Hat, Inc.
8 *
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.
11 */
12
80c71a24 13#include "qemu/osdep.h"
e819ab22
FZ
14#include "block/block_int.h"
15
e5e51dd3
FZ
16#define NULL_OPT_LATENCY "latency-ns"
17
e819ab22
FZ
18typedef struct {
19 int64_t length;
e5e51dd3 20 int64_t latency_ns;
e819ab22
FZ
21} BDRVNullState;
22
23static QemuOptsList runtime_opts = {
24 .name = "null",
25 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
26 .desc = {
27 {
28 .name = "filename",
29 .type = QEMU_OPT_STRING,
30 .help = "",
31 },
32 {
33 .name = BLOCK_OPT_SIZE,
34 .type = QEMU_OPT_SIZE,
35 .help = "size of the null block",
36 },
e5e51dd3
FZ
37 {
38 .name = NULL_OPT_LATENCY,
39 .type = QEMU_OPT_NUMBER,
40 .help = "nanoseconds (approximated) to wait "
41 "before completing request",
42 },
e819ab22
FZ
43 { /* end of list */ }
44 },
45};
46
47static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
48 Error **errp)
49{
50 QemuOpts *opts;
51 BDRVNullState *s = bs->opaque;
e5e51dd3 52 int ret = 0;
e819ab22
FZ
53
54 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
55 qemu_opts_absorb_qdict(opts, options, &error_abort);
56 s->length =
57 qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
e5e51dd3
FZ
58 s->latency_ns =
59 qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0);
60 if (s->latency_ns < 0) {
61 error_setg(errp, "latency-ns is invalid");
62 ret = -EINVAL;
63 }
e819ab22 64 qemu_opts_del(opts);
e5e51dd3 65 return ret;
e819ab22
FZ
66}
67
68static void null_close(BlockDriverState *bs)
69{
70}
71
72static int64_t null_getlength(BlockDriverState *bs)
73{
74 BDRVNullState *s = bs->opaque;
75 return s->length;
76}
77
e5e51dd3
FZ
78static coroutine_fn int null_co_common(BlockDriverState *bs)
79{
80 BDRVNullState *s = bs->opaque;
81
82 if (s->latency_ns) {
83 co_aio_sleep_ns(bdrv_get_aio_context(bs), QEMU_CLOCK_REALTIME,
84 s->latency_ns);
85 }
86 return 0;
87}
88
e819ab22
FZ
89static coroutine_fn int null_co_readv(BlockDriverState *bs,
90 int64_t sector_num, int nb_sectors,
91 QEMUIOVector *qiov)
92{
e5e51dd3 93 return null_co_common(bs);
e819ab22
FZ
94}
95
96static coroutine_fn int null_co_writev(BlockDriverState *bs,
97 int64_t sector_num, int nb_sectors,
98 QEMUIOVector *qiov)
99{
e5e51dd3 100 return null_co_common(bs);
e819ab22
FZ
101}
102
103static coroutine_fn int null_co_flush(BlockDriverState *bs)
104{
e5e51dd3 105 return null_co_common(bs);
e819ab22
FZ
106}
107
108typedef struct {
7c84b1b8 109 BlockAIOCB common;
e819ab22 110 QEMUBH *bh;
e5e51dd3 111 QEMUTimer timer;
e819ab22
FZ
112} NullAIOCB;
113
114static const AIOCBInfo null_aiocb_info = {
115 .aiocb_size = sizeof(NullAIOCB),
116};
117
118static void null_bh_cb(void *opaque)
119{
120 NullAIOCB *acb = opaque;
121 acb->common.cb(acb->common.opaque, 0);
122 qemu_bh_delete(acb->bh);
123 qemu_aio_unref(acb);
124}
125
e5e51dd3
FZ
126static void null_timer_cb(void *opaque)
127{
128 NullAIOCB *acb = opaque;
129 acb->common.cb(acb->common.opaque, 0);
130 timer_deinit(&acb->timer);
131 qemu_aio_unref(acb);
132}
133
7c84b1b8 134static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
097310b5 135 BlockCompletionFunc *cb,
7c84b1b8 136 void *opaque)
e819ab22
FZ
137{
138 NullAIOCB *acb;
e5e51dd3 139 BDRVNullState *s = bs->opaque;
e819ab22
FZ
140
141 acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
e5e51dd3
FZ
142 /* Only emulate latency after vcpu is running. */
143 if (s->latency_ns) {
144 aio_timer_init(bdrv_get_aio_context(bs), &acb->timer,
145 QEMU_CLOCK_REALTIME, SCALE_NS,
146 null_timer_cb, acb);
147 timer_mod_ns(&acb->timer,
148 qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
149 } else {
150 acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
151 qemu_bh_schedule(acb->bh);
152 }
e819ab22
FZ
153 return &acb->common;
154}
155
7c84b1b8
MA
156static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
157 int64_t sector_num, QEMUIOVector *qiov,
158 int nb_sectors,
097310b5 159 BlockCompletionFunc *cb,
7c84b1b8 160 void *opaque)
e819ab22
FZ
161{
162 return null_aio_common(bs, cb, opaque);
163}
164
7c84b1b8
MA
165static BlockAIOCB *null_aio_writev(BlockDriverState *bs,
166 int64_t sector_num, QEMUIOVector *qiov,
167 int nb_sectors,
097310b5 168 BlockCompletionFunc *cb,
7c84b1b8 169 void *opaque)
e819ab22
FZ
170{
171 return null_aio_common(bs, cb, opaque);
172}
173
7c84b1b8 174static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
097310b5 175 BlockCompletionFunc *cb,
7c84b1b8 176 void *opaque)
e819ab22
FZ
177{
178 return null_aio_common(bs, cb, opaque);
179}
180
1c2b49a1
FZ
181static int null_reopen_prepare(BDRVReopenState *reopen_state,
182 BlockReopenQueue *queue, Error **errp)
183{
184 return 0;
185}
186
e819ab22
FZ
187static BlockDriver bdrv_null_co = {
188 .format_name = "null-co",
189 .protocol_name = "null-co",
190 .instance_size = sizeof(BDRVNullState),
191
192 .bdrv_file_open = null_file_open,
193 .bdrv_close = null_close,
194 .bdrv_getlength = null_getlength,
195
196 .bdrv_co_readv = null_co_readv,
197 .bdrv_co_writev = null_co_writev,
198 .bdrv_co_flush_to_disk = null_co_flush,
1c2b49a1 199 .bdrv_reopen_prepare = null_reopen_prepare,
e819ab22
FZ
200};
201
202static BlockDriver bdrv_null_aio = {
203 .format_name = "null-aio",
204 .protocol_name = "null-aio",
205 .instance_size = sizeof(BDRVNullState),
206
207 .bdrv_file_open = null_file_open,
208 .bdrv_close = null_close,
209 .bdrv_getlength = null_getlength,
210
211 .bdrv_aio_readv = null_aio_readv,
212 .bdrv_aio_writev = null_aio_writev,
213 .bdrv_aio_flush = null_aio_flush,
1c2b49a1 214 .bdrv_reopen_prepare = null_reopen_prepare,
e819ab22
FZ
215};
216
217static void bdrv_null_init(void)
218{
219 bdrv_register(&bdrv_null_co);
220 bdrv_register(&bdrv_null_aio);
221}
222
223block_init(bdrv_null_init);
This page took 0.125014 seconds and 4 git commands to generate.