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