Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/block/null.c
Line
Count
Source
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 "qobject/qdict.h"
16
#include "qobject/qstring.h"
17
#include "qemu/module.h"
18
#include "qemu/option.h"
19
#include "block/block-io.h"
20
#include "block/block_int.h"
21
#include "system/replay.h"
22
23
18
#define NULL_OPT_LATENCY "latency-ns"
24
16
#define NULL_OPT_ZEROES  "read-zeroes"
25
26
typedef struct {
27
    int64_t length;
28
    int64_t latency_ns;
29
    bool read_zeroes;
30
} BDRVNullState;
31
32
static QemuOptsList runtime_opts = {
33
    .name = "null",
34
    .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
35
    .desc = {
36
        {
37
            .name = BLOCK_OPT_SIZE,
38
            .type = QEMU_OPT_SIZE,
39
            .help = "size of the null block",
40
        },
41
        {
42
            .name = NULL_OPT_LATENCY,
43
            .type = QEMU_OPT_NUMBER,
44
            .help = "nanoseconds (approximated) to wait "
45
                    "before completing request",
46
        },
47
        {
48
            .name = NULL_OPT_ZEROES,
49
            .type = QEMU_OPT_BOOL,
50
            .help = "return zeroes when read",
51
        },
52
        { /* end of list */ }
53
    },
54
};
55
56
static void null_co_parse_filename(const char *filename, QDict *options,
57
                                   Error **errp)
58
15
{
59
    /* This functions only exists so that a null-co:// filename is accepted
60
     * with the null-co driver. */
61
15
    if (strcmp(filename, "null-co://")) {
62
0
        error_setg(errp, "The only allowed filename for this driver is "
63
0
                         "'null-co://'");
64
0
        return;
65
0
    }
66
15
}
67
68
static void null_aio_parse_filename(const char *filename, QDict *options,
69
                                    Error **errp)
70
0
{
71
    /* This functions only exists so that a null-aio:// filename is accepted
72
     * with the null-aio driver. */
73
0
    if (strcmp(filename, "null-aio://")) {
74
0
        error_setg(errp, "The only allowed filename for this driver is "
75
0
                         "'null-aio://'");
76
0
        return;
77
0
    }
78
0
}
79
80
static int null_open(BlockDriverState *bs, QDict *options, int flags,
81
                     Error **errp)
82
16
{
83
16
    QemuOpts *opts;
84
16
    BDRVNullState *s = bs->opaque;
85
16
    int ret = 0;
86
87
16
    opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
88
16
    qemu_opts_absorb_qdict(opts, options, &error_abort);
89
16
    s->length =
90
16
        qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
91
16
    s->latency_ns =
92
16
        qemu_opt_get_number(opts, NULL_OPT_LATENCY, 0);
93
16
    if (s->latency_ns < 0) {
94
0
        error_setg(errp, "latency-ns is invalid");
95
0
        ret = -EINVAL;
96
0
    }
97
16
    s->read_zeroes = qemu_opt_get_bool(opts, NULL_OPT_ZEROES, false);
98
16
    qemu_opts_del(opts);
99
16
    bs->supported_write_flags = BDRV_REQ_FUA;
100
16
    return ret;
101
16
}
102
103
static int64_t coroutine_fn null_co_getlength(BlockDriverState *bs)
104
16
{
105
16
    BDRVNullState *s = bs->opaque;
106
16
    return s->length;
107
16
}
108
109
static coroutine_fn int null_co_common(BlockDriverState *bs)
110
20.1M
{
111
20.1M
    BDRVNullState *s = bs->opaque;
112
113
20.1M
    if (s->latency_ns) {
114
0
        qemu_co_sleep_ns(QEMU_CLOCK_REALTIME, s->latency_ns);
115
0
    }
116
20.1M
    return 0;
117
20.1M
}
118
119
static coroutine_fn int null_co_preadv(BlockDriverState *bs,
120
                                       int64_t offset, int64_t bytes,
121
                                       QEMUIOVector *qiov,
122
                                       BdrvRequestFlags flags)
123
19.3M
{
124
19.3M
    BDRVNullState *s = bs->opaque;
125
126
19.3M
    if (s->read_zeroes) {
127
19.2M
        qemu_iovec_memset(qiov, 0, 0, bytes);
128
19.2M
    }
129
130
19.3M
    return null_co_common(bs);
131
19.3M
}
132
133
static coroutine_fn int null_co_pwritev(BlockDriverState *bs,
134
                                        int64_t offset, int64_t bytes,
135
                                        QEMUIOVector *qiov,
136
                                        BdrvRequestFlags flags)
137
813k
{
138
813k
    return null_co_common(bs);
139
813k
}
140
141
static coroutine_fn int null_co_flush(BlockDriverState *bs)
142
877
{
143
877
    return null_co_common(bs);
144
877
}
145
146
typedef struct {
147
    BlockAIOCB common;
148
    QEMUTimer timer;
149
} NullAIOCB;
150
151
static const AIOCBInfo null_aiocb_info = {
152
    .aiocb_size = sizeof(NullAIOCB),
153
};
154
155
static void null_bh_cb(void *opaque)
156
0
{
157
0
    NullAIOCB *acb = opaque;
158
0
    acb->common.cb(acb->common.opaque, 0);
159
0
    qemu_aio_unref(acb);
160
0
}
161
162
static void null_timer_cb(void *opaque)
163
0
{
164
0
    NullAIOCB *acb = opaque;
165
0
    acb->common.cb(acb->common.opaque, 0);
166
0
    timer_deinit(&acb->timer);
167
0
    qemu_aio_unref(acb);
168
0
}
169
170
static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
171
                                          BlockCompletionFunc *cb,
172
                                          void *opaque)
173
0
{
174
0
    NullAIOCB *acb;
175
0
    BDRVNullState *s = bs->opaque;
176
0
    AioContext *ctx = qemu_get_current_aio_context();
177
178
0
    acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
179
    /* Only emulate latency after vcpu is running. */
180
0
    if (s->latency_ns) {
181
0
        aio_timer_init(ctx, &acb->timer, QEMU_CLOCK_REALTIME, SCALE_NS,
182
0
                       null_timer_cb, acb);
183
0
        timer_mod_ns(&acb->timer,
184
0
                     qemu_clock_get_ns(QEMU_CLOCK_REALTIME) + s->latency_ns);
185
0
    } else {
186
0
        replay_bh_schedule_oneshot_event(ctx, null_bh_cb, acb);
187
0
    }
188
0
    return &acb->common;
189
0
}
190
191
static BlockAIOCB *null_aio_preadv(BlockDriverState *bs,
192
                                   int64_t offset, int64_t bytes,
193
                                   QEMUIOVector *qiov, BdrvRequestFlags flags,
194
                                   BlockCompletionFunc *cb,
195
                                   void *opaque)
196
0
{
197
0
    BDRVNullState *s = bs->opaque;
198
199
0
    if (s->read_zeroes) {
200
0
        qemu_iovec_memset(qiov, 0, 0, bytes);
201
0
    }
202
203
0
    return null_aio_common(bs, cb, opaque);
204
0
}
205
206
static BlockAIOCB *null_aio_pwritev(BlockDriverState *bs,
207
                                    int64_t offset, int64_t bytes,
208
                                    QEMUIOVector *qiov, BdrvRequestFlags flags,
209
                                    BlockCompletionFunc *cb,
210
                                    void *opaque)
211
0
{
212
0
    return null_aio_common(bs, cb, opaque);
213
0
}
214
215
static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
216
                                  BlockCompletionFunc *cb,
217
                                  void *opaque)
218
0
{
219
0
    return null_aio_common(bs, cb, opaque);
220
0
}
221
222
static int null_reopen_prepare(BDRVReopenState *reopen_state,
223
                               BlockReopenQueue *queue, Error **errp)
224
0
{
225
0
    return 0;
226
0
}
227
228
static int coroutine_fn null_co_block_status(BlockDriverState *bs,
229
                                             unsigned int mode,
230
                                             int64_t offset, int64_t bytes,
231
                                             int64_t *pnum, int64_t *map,
232
                                             BlockDriverState **file)
233
0
{
234
0
    BDRVNullState *s = bs->opaque;
235
0
    int ret = BDRV_BLOCK_OFFSET_VALID;
236
237
0
    *pnum = bytes;
238
0
    *map = offset;
239
0
    *file = bs;
240
241
0
    if (s->read_zeroes) {
242
0
        ret |= BDRV_BLOCK_ZERO;
243
0
    }
244
0
    return ret;
245
0
}
246
247
static void null_refresh_filename(BlockDriverState *bs)
248
15
{
249
15
    const QDictEntry *e;
250
251
28
    for (e = qdict_first(bs->full_open_options); e;
252
15
         e = qdict_next(bs->full_open_options, e))
253
15
    {
254
        /* These options can be ignored */
255
15
        if (strcmp(qdict_entry_key(e), "filename") &&
256
15
            strcmp(qdict_entry_key(e), "driver") &&
257
2
            strcmp(qdict_entry_key(e), NULL_OPT_LATENCY))
258
2
        {
259
2
            return;
260
2
        }
261
15
    }
262
263
13
    snprintf(bs->exact_filename, sizeof(bs->exact_filename), "%s://",
264
13
             bs->drv->format_name);
265
13
}
266
267
static int64_t coroutine_fn
268
null_co_get_allocated_file_size(BlockDriverState *bs)
269
0
{
270
0
    return 0;
271
0
}
272
273
static const char *const null_strong_runtime_opts[] = {
274
    BLOCK_OPT_SIZE,
275
    NULL_OPT_ZEROES,
276
277
    NULL
278
};
279
280
static BlockDriver bdrv_null_co = {
281
    .format_name            = "null-co",
282
    .protocol_name          = "null-co",
283
    .instance_size          = sizeof(BDRVNullState),
284
285
    .bdrv_open              = null_open,
286
    .bdrv_parse_filename    = null_co_parse_filename,
287
    .bdrv_co_getlength      = null_co_getlength,
288
    .bdrv_co_get_allocated_file_size = null_co_get_allocated_file_size,
289
290
    .bdrv_co_preadv         = null_co_preadv,
291
    .bdrv_co_pwritev        = null_co_pwritev,
292
    .bdrv_co_flush_to_disk  = null_co_flush,
293
    .bdrv_reopen_prepare    = null_reopen_prepare,
294
295
    .bdrv_co_block_status   = null_co_block_status,
296
297
    .bdrv_refresh_filename  = null_refresh_filename,
298
    .strong_runtime_opts    = null_strong_runtime_opts,
299
};
300
301
static BlockDriver bdrv_null_aio = {
302
    .format_name            = "null-aio",
303
    .protocol_name          = "null-aio",
304
    .instance_size          = sizeof(BDRVNullState),
305
306
    .bdrv_open              = null_open,
307
    .bdrv_parse_filename    = null_aio_parse_filename,
308
    .bdrv_co_getlength      = null_co_getlength,
309
    .bdrv_co_get_allocated_file_size = null_co_get_allocated_file_size,
310
311
    .bdrv_aio_preadv        = null_aio_preadv,
312
    .bdrv_aio_pwritev       = null_aio_pwritev,
313
    .bdrv_aio_flush         = null_aio_flush,
314
    .bdrv_reopen_prepare    = null_reopen_prepare,
315
316
    .bdrv_co_block_status   = null_co_block_status,
317
318
    .bdrv_refresh_filename  = null_refresh_filename,
319
    .strong_runtime_opts    = null_strong_runtime_opts,
320
};
321
322
static void bdrv_null_init(void)
323
63
{
324
63
    bdrv_register(&bdrv_null_co);
325
63
    bdrv_register(&bdrv_null_aio);
326
63
}
327
328
block_init(bdrv_null_init);