]>
Commit | Line | Data |
---|---|---|
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" |
da34e65c | 14 | #include "qapi/error.h" |
67882b15 HR |
15 | #include "qapi/qmp/qdict.h" |
16 | #include "qapi/qmp/qstring.h" | |
e819ab22 FZ |
17 | #include "block/block_int.h" |
18 | ||
e5e51dd3 | 19 | #define NULL_OPT_LATENCY "latency-ns" |
cd219eb1 | 20 | #define NULL_OPT_ZEROES "read-zeroes" |
e5e51dd3 | 21 | |
e819ab22 FZ |
22 | typedef struct { |
23 | int64_t length; | |
e5e51dd3 | 24 | int64_t latency_ns; |
cd219eb1 | 25 | bool read_zeroes; |
e819ab22 FZ |
26 | } BDRVNullState; |
27 | ||
28 | static QemuOptsList runtime_opts = { | |
29 | .name = "null", | |
30 | .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), | |
31 | .desc = { | |
e819ab22 FZ |
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 | }, | |
cd219eb1 HR |
43 | { |
44 | .name = NULL_OPT_ZEROES, | |
45 | .type = QEMU_OPT_BOOL, | |
46 | .help = "return zeroes when read", | |
47 | }, | |
e819ab22 FZ |
48 | { /* end of list */ } |
49 | }, | |
50 | }; | |
51 | ||
809eb70e KW |
52 | static void null_co_parse_filename(const char *filename, QDict *options, |
53 | Error **errp) | |
54 | { | |
55 | /* This functions only exists so that a null-co:// filename is accepted | |
56 | * with the null-co driver. */ | |
57 | if (strcmp(filename, "null-co://")) { | |
58 | error_setg(errp, "The only allowed filename for this driver is " | |
59 | "'null-co://'"); | |
60 | return; | |
61 | } | |
62 | } | |
63 | ||
64 | static void null_aio_parse_filename(const char *filename, QDict *options, | |
65 | Error **errp) | |
66 | { | |
67 | /* This functions only exists so that a null-aio:// filename is accepted | |
68 | * with the null-aio driver. */ | |
69 | if (strcmp(filename, "null-aio://")) { | |
70 | error_setg(errp, "The only allowed filename for this driver is " | |
71 | "'null-aio://'"); | |
72 | return; | |
73 | } | |
74 | } | |
75 | ||
e819ab22 FZ |
76 | static int null_file_open(BlockDriverState *bs, QDict *options, int flags, |
77 | Error **errp) | |
78 | { | |
79 | QemuOpts *opts; | |
80 | BDRVNullState *s = bs->opaque; | |
e5e51dd3 | 81 | int ret = 0; |
e819ab22 FZ |
82 | |
83 | opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); | |
84 | qemu_opts_absorb_qdict(opts, options, &error_abort); | |
85 | s->length = | |
86 | qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30); | |
e5e51dd3 FZ |
87 | s->latency_ns = |
88 | qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0); | |
89 | if (s->latency_ns < 0) { | |
90 | error_setg(errp, "latency-ns is invalid"); | |
91 | ret = -EINVAL; | |
92 | } | |
cd219eb1 | 93 | s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false); |
e819ab22 | 94 | qemu_opts_del(opts); |
e5e51dd3 | 95 | return ret; |
e819ab22 FZ |
96 | } |
97 | ||
98 | static void null_close(BlockDriverState *bs) | |
99 | { | |
100 | } | |
101 | ||
102 | static int64_t null_getlength(BlockDriverState *bs) | |
103 | { | |
104 | BDRVNullState *s = bs->opaque; | |
105 | return s->length; | |
106 | } | |
107 | ||
e5e51dd3 FZ |
108 | static coroutine_fn int null_co_common(BlockDriverState *bs) |
109 | { | |
110 | BDRVNullState *s = bs->opaque; | |
111 | ||
112 | if (s->latency_ns) { | |
113 | co_aio_sleep_ns(bdrv_get_aio_context(bs), QEMU_CLOCK_REALTIME, | |
114 | s->latency_ns); | |
115 | } | |
116 | return 0; | |
117 | } | |
118 | ||
e819ab22 FZ |
119 | static coroutine_fn int null_co_readv(BlockDriverState *bs, |
120 | int64_t sector_num, int nb_sectors, | |
121 | QEMUIOVector *qiov) | |
122 | { | |
cd219eb1 HR |
123 | BDRVNullState *s = bs->opaque; |
124 | ||
125 | if (s->read_zeroes) { | |
126 | qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE); | |
127 | } | |
128 | ||
e5e51dd3 | 129 | return null_co_common(bs); |
e819ab22 FZ |
130 | } |
131 | ||
132 | static coroutine_fn int null_co_writev(BlockDriverState *bs, | |
133 | int64_t sector_num, int nb_sectors, | |
134 | QEMUIOVector *qiov) | |
135 | { | |
e5e51dd3 | 136 | return null_co_common(bs); |
e819ab22 FZ |
137 | } |
138 | ||
139 | static coroutine_fn int null_co_flush(BlockDriverState *bs) | |
140 | { | |
e5e51dd3 | 141 | return null_co_common(bs); |
e819ab22 FZ |
142 | } |
143 | ||
144 | typedef struct { | |
7c84b1b8 | 145 | BlockAIOCB common; |
e5e51dd3 | 146 | QEMUTimer timer; |
e819ab22 FZ |
147 | } NullAIOCB; |
148 | ||
149 | static const AIOCBInfo null_aiocb_info = { | |
150 | .aiocb_size = sizeof(NullAIOCB), | |
151 | }; | |
152 | ||
153 | static void null_bh_cb(void *opaque) | |
154 | { | |
155 | NullAIOCB *acb = opaque; | |
156 | acb->common.cb(acb->common.opaque, 0); | |
e819ab22 FZ |
157 | qemu_aio_unref(acb); |
158 | } | |
159 | ||
e5e51dd3 FZ |
160 | static void null_timer_cb(void *opaque) |
161 | { | |
162 | NullAIOCB *acb = opaque; | |
163 | acb->common.cb(acb->common.opaque, 0); | |
164 | timer_deinit(&acb->timer); | |
165 | qemu_aio_unref(acb); | |
166 | } | |
167 | ||
7c84b1b8 | 168 | static inline BlockAIOCB *null_aio_common(BlockDriverState *bs, |
097310b5 | 169 | BlockCompletionFunc *cb, |
7c84b1b8 | 170 | void *opaque) |
e819ab22 FZ |
171 | { |
172 | NullAIOCB *acb; | |
e5e51dd3 | 173 | BDRVNullState *s = bs->opaque; |
e819ab22 FZ |
174 | |
175 | acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque); | |
e5e51dd3 FZ |
176 | /* Only emulate latency after vcpu is running. */ |
177 | if (s->latency_ns) { | |
178 | aio_timer_init(bdrv_get_aio_context(bs), &acb->timer, | |
179 | QEMU_CLOCK_REALTIME, SCALE_NS, | |
180 | null_timer_cb, acb); | |
181 | timer_mod_ns(&acb->timer, | |
182 | qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns); | |
183 | } else { | |
fffb6e12 | 184 | aio_bh_schedule_oneshot(bdrv_get_aio_context(bs), null_bh_cb, acb); |
e5e51dd3 | 185 | } |
e819ab22 FZ |
186 | return &acb->common; |
187 | } | |
188 | ||
7c84b1b8 MA |
189 | static BlockAIOCB *null_aio_readv(BlockDriverState *bs, |
190 | int64_t sector_num, QEMUIOVector *qiov, | |
191 | int nb_sectors, | |
097310b5 | 192 | BlockCompletionFunc *cb, |
7c84b1b8 | 193 | void *opaque) |
e819ab22 | 194 | { |
cd219eb1 HR |
195 | BDRVNullState *s = bs->opaque; |
196 | ||
197 | if (s->read_zeroes) { | |
198 | qemu_iovec_memset(qiov, 0, 0, nb_sectors * BDRV_SECTOR_SIZE); | |
199 | } | |
200 | ||
e819ab22 FZ |
201 | return null_aio_common(bs, cb, opaque); |
202 | } | |
203 | ||
7c84b1b8 MA |
204 | static BlockAIOCB *null_aio_writev(BlockDriverState *bs, |
205 | int64_t sector_num, QEMUIOVector *qiov, | |
206 | int nb_sectors, | |
097310b5 | 207 | BlockCompletionFunc *cb, |
7c84b1b8 | 208 | void *opaque) |
e819ab22 FZ |
209 | { |
210 | return null_aio_common(bs, cb, opaque); | |
211 | } | |
212 | ||
7c84b1b8 | 213 | static BlockAIOCB *null_aio_flush(BlockDriverState *bs, |
097310b5 | 214 | BlockCompletionFunc *cb, |
7c84b1b8 | 215 | void *opaque) |
e819ab22 FZ |
216 | { |
217 | return null_aio_common(bs, cb, opaque); | |
218 | } | |
219 | ||
1c2b49a1 FZ |
220 | static int null_reopen_prepare(BDRVReopenState *reopen_state, |
221 | BlockReopenQueue *queue, Error **errp) | |
222 | { | |
223 | return 0; | |
224 | } | |
225 | ||
a9063927 HR |
226 | static int64_t coroutine_fn null_co_get_block_status(BlockDriverState *bs, |
227 | int64_t sector_num, | |
228 | int nb_sectors, int *pnum, | |
229 | BlockDriverState **file) | |
230 | { | |
231 | BDRVNullState *s = bs->opaque; | |
232 | off_t start = sector_num * BDRV_SECTOR_SIZE; | |
233 | ||
234 | *pnum = nb_sectors; | |
235 | *file = bs; | |
236 | ||
237 | if (s->read_zeroes) { | |
238 | return BDRV_BLOCK_OFFSET_VALID | start | BDRV_BLOCK_ZERO; | |
239 | } else { | |
240 | return BDRV_BLOCK_OFFSET_VALID | start; | |
241 | } | |
242 | } | |
243 | ||
67882b15 HR |
244 | static void null_refresh_filename(BlockDriverState *bs, QDict *opts) |
245 | { | |
246 | QINCREF(opts); | |
247 | qdict_del(opts, "filename"); | |
248 | ||
249 | if (!qdict_size(opts)) { | |
250 | snprintf(bs->exact_filename, sizeof(bs->exact_filename), "%s://", | |
251 | bs->drv->format_name); | |
252 | } | |
253 | ||
46f5ac20 | 254 | qdict_put_str(opts, "driver", bs->drv->format_name); |
67882b15 HR |
255 | bs->full_open_options = opts; |
256 | } | |
257 | ||
e819ab22 FZ |
258 | static BlockDriver bdrv_null_co = { |
259 | .format_name = "null-co", | |
260 | .protocol_name = "null-co", | |
261 | .instance_size = sizeof(BDRVNullState), | |
262 | ||
263 | .bdrv_file_open = null_file_open, | |
809eb70e | 264 | .bdrv_parse_filename = null_co_parse_filename, |
e819ab22 FZ |
265 | .bdrv_close = null_close, |
266 | .bdrv_getlength = null_getlength, | |
267 | ||
268 | .bdrv_co_readv = null_co_readv, | |
269 | .bdrv_co_writev = null_co_writev, | |
270 | .bdrv_co_flush_to_disk = null_co_flush, | |
1c2b49a1 | 271 | .bdrv_reopen_prepare = null_reopen_prepare, |
a9063927 HR |
272 | |
273 | .bdrv_co_get_block_status = null_co_get_block_status, | |
67882b15 HR |
274 | |
275 | .bdrv_refresh_filename = null_refresh_filename, | |
e819ab22 FZ |
276 | }; |
277 | ||
278 | static BlockDriver bdrv_null_aio = { | |
279 | .format_name = "null-aio", | |
280 | .protocol_name = "null-aio", | |
281 | .instance_size = sizeof(BDRVNullState), | |
282 | ||
283 | .bdrv_file_open = null_file_open, | |
809eb70e | 284 | .bdrv_parse_filename = null_aio_parse_filename, |
e819ab22 FZ |
285 | .bdrv_close = null_close, |
286 | .bdrv_getlength = null_getlength, | |
287 | ||
288 | .bdrv_aio_readv = null_aio_readv, | |
289 | .bdrv_aio_writev = null_aio_writev, | |
290 | .bdrv_aio_flush = null_aio_flush, | |
1c2b49a1 | 291 | .bdrv_reopen_prepare = null_reopen_prepare, |
a9063927 HR |
292 | |
293 | .bdrv_co_get_block_status = null_co_get_block_status, | |
67882b15 HR |
294 | |
295 | .bdrv_refresh_filename = null_refresh_filename, | |
e819ab22 FZ |
296 | }; |
297 | ||
298 | static void bdrv_null_init(void) | |
299 | { | |
300 | bdrv_register(&bdrv_null_co); | |
301 | bdrv_register(&bdrv_null_aio); | |
302 | } | |
303 | ||
304 | block_init(bdrv_null_init); |