xref: /openbmc/qemu/block/null.c (revision 2822c1b6)
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 "block/block_int.h"
14 
15 typedef struct {
16     int64_t length;
17 } BDRVNullState;
18 
19 static QemuOptsList runtime_opts = {
20     .name = "null",
21     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
22     .desc = {
23         {
24             .name = "filename",
25             .type = QEMU_OPT_STRING,
26             .help = "",
27         },
28         {
29             .name = BLOCK_OPT_SIZE,
30             .type = QEMU_OPT_SIZE,
31             .help = "size of the null block",
32         },
33         { /* end of list */ }
34     },
35 };
36 
37 static int null_file_open(BlockDriverState *bs, QDict *options, int flags,
38                           Error **errp)
39 {
40     QemuOpts *opts;
41     BDRVNullState *s = bs->opaque;
42 
43     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
44     qemu_opts_absorb_qdict(opts, options, &error_abort);
45     s->length =
46         qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 1 << 30);
47     qemu_opts_del(opts);
48     return 0;
49 }
50 
51 static void null_close(BlockDriverState *bs)
52 {
53 }
54 
55 static int64_t null_getlength(BlockDriverState *bs)
56 {
57     BDRVNullState *s = bs->opaque;
58     return s->length;
59 }
60 
61 static coroutine_fn int null_co_readv(BlockDriverState *bs,
62                                       int64_t sector_num, int nb_sectors,
63                                       QEMUIOVector *qiov)
64 {
65     return 0;
66 }
67 
68 static coroutine_fn int null_co_writev(BlockDriverState *bs,
69                                        int64_t sector_num, int nb_sectors,
70                                        QEMUIOVector *qiov)
71 {
72     return 0;
73 }
74 
75 static coroutine_fn int null_co_flush(BlockDriverState *bs)
76 {
77     return 0;
78 }
79 
80 typedef struct {
81     BlockAIOCB common;
82     QEMUBH *bh;
83 } NullAIOCB;
84 
85 static const AIOCBInfo null_aiocb_info = {
86     .aiocb_size = sizeof(NullAIOCB),
87 };
88 
89 static void null_bh_cb(void *opaque)
90 {
91     NullAIOCB *acb = opaque;
92     acb->common.cb(acb->common.opaque, 0);
93     qemu_bh_delete(acb->bh);
94     qemu_aio_unref(acb);
95 }
96 
97 static inline BlockAIOCB *null_aio_common(BlockDriverState *bs,
98                                           BlockCompletionFunc *cb,
99                                           void *opaque)
100 {
101     NullAIOCB *acb;
102 
103     acb = qemu_aio_get(&null_aiocb_info, bs, cb, opaque);
104     acb->bh = aio_bh_new(bdrv_get_aio_context(bs), null_bh_cb, acb);
105     qemu_bh_schedule(acb->bh);
106     return &acb->common;
107 }
108 
109 static BlockAIOCB *null_aio_readv(BlockDriverState *bs,
110                                   int64_t sector_num, QEMUIOVector *qiov,
111                                   int nb_sectors,
112                                   BlockCompletionFunc *cb,
113                                   void *opaque)
114 {
115     return null_aio_common(bs, cb, opaque);
116 }
117 
118 static BlockAIOCB *null_aio_writev(BlockDriverState *bs,
119                                    int64_t sector_num, QEMUIOVector *qiov,
120                                    int nb_sectors,
121                                    BlockCompletionFunc *cb,
122                                    void *opaque)
123 {
124     return null_aio_common(bs, cb, opaque);
125 }
126 
127 static BlockAIOCB *null_aio_flush(BlockDriverState *bs,
128                                   BlockCompletionFunc *cb,
129                                   void *opaque)
130 {
131     return null_aio_common(bs, cb, opaque);
132 }
133 
134 static BlockDriver bdrv_null_co = {
135     .format_name            = "null-co",
136     .protocol_name          = "null-co",
137     .instance_size          = sizeof(BDRVNullState),
138 
139     .bdrv_file_open         = null_file_open,
140     .bdrv_close             = null_close,
141     .bdrv_getlength         = null_getlength,
142 
143     .bdrv_co_readv          = null_co_readv,
144     .bdrv_co_writev         = null_co_writev,
145     .bdrv_co_flush_to_disk  = null_co_flush,
146 };
147 
148 static BlockDriver bdrv_null_aio = {
149     .format_name            = "null-aio",
150     .protocol_name          = "null-aio",
151     .instance_size          = sizeof(BDRVNullState),
152 
153     .bdrv_file_open         = null_file_open,
154     .bdrv_close             = null_close,
155     .bdrv_getlength         = null_getlength,
156 
157     .bdrv_aio_readv         = null_aio_readv,
158     .bdrv_aio_writev        = null_aio_writev,
159     .bdrv_aio_flush         = null_aio_flush,
160 };
161 
162 static void bdrv_null_init(void)
163 {
164     bdrv_register(&bdrv_null_co);
165     bdrv_register(&bdrv_null_aio);
166 }
167 
168 block_init(bdrv_null_init);
169