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