1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29#include "qemu/osdep.h"
30#include "block/block_int.h"
31#include "qapi/error.h"
32#include "qemu/option.h"
33
34static QemuOptsList raw_create_opts = {
35 .name = "raw-create-opts",
36 .head = QTAILQ_HEAD_INITIALIZER(raw_create_opts.head),
37 .desc = {
38 {
39 .name = BLOCK_OPT_SIZE,
40 .type = QEMU_OPT_SIZE,
41 .help = "Virtual disk size"
42 },
43 { }
44 }
45};
46
47static int raw_reopen_prepare(BDRVReopenState *reopen_state,
48 BlockReopenQueue *queue, Error **errp)
49{
50 return 0;
51}
52
53static int coroutine_fn raw_co_readv(BlockDriverState *bs, int64_t sector_num,
54 int nb_sectors, QEMUIOVector *qiov)
55{
56 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
57 return bdrv_co_readv(bs->file->bs, sector_num, nb_sectors, qiov);
58}
59
60static int coroutine_fn
61raw_co_writev_flags(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
62 QEMUIOVector *qiov, int flags)
63{
64 void *buf = NULL;
65 BlockDriver *drv;
66 QEMUIOVector local_qiov;
67 int ret;
68
69 if (bs->probed && sector_num == 0) {
70
71
72 QEMU_BUILD_BUG_ON(BLOCK_PROBE_BUF_SIZE != 512);
73 QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != 512);
74
75 if (nb_sectors == 0) {
76
77
78 return 0;
79 }
80
81 buf = qemu_try_blockalign(bs->file->bs, 512);
82 if (!buf) {
83 ret = -ENOMEM;
84 goto fail;
85 }
86
87 ret = qemu_iovec_to_buf(qiov, 0, buf, 512);
88 if (ret != 512) {
89 ret = -EINVAL;
90 goto fail;
91 }
92
93 drv = bdrv_probe_all(buf, 512, NULL);
94 if (drv != bs->drv) {
95 ret = -EPERM;
96 goto fail;
97 }
98
99
100
101 qemu_iovec_init(&local_qiov, qiov->niov + 1);
102 qemu_iovec_add(&local_qiov, buf, 512);
103 qemu_iovec_concat(&local_qiov, qiov, 512, qiov->size - 512);
104 qiov = &local_qiov;
105 }
106
107 BLKDBG_EVENT(bs->file, BLKDBG_WRITE_AIO);
108 ret = bdrv_co_do_pwritev(bs->file->bs, sector_num * BDRV_SECTOR_SIZE,
109 nb_sectors * BDRV_SECTOR_SIZE, qiov, flags);
110
111fail:
112 if (qiov == &local_qiov) {
113 qemu_iovec_destroy(&local_qiov);
114 }
115 qemu_vfree(buf);
116 return ret;
117}
118
119static int coroutine_fn
120raw_co_writev(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
121 QEMUIOVector *qiov)
122{
123 return raw_co_writev_flags(bs, sector_num, nb_sectors, qiov, 0);
124}
125
126static int64_t coroutine_fn raw_co_get_block_status(BlockDriverState *bs,
127 int64_t sector_num,
128 int nb_sectors, int *pnum,
129 BlockDriverState **file)
130{
131 *pnum = nb_sectors;
132 *file = bs->file->bs;
133 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
134 (sector_num << BDRV_SECTOR_BITS);
135}
136
137static int coroutine_fn raw_co_write_zeroes(BlockDriverState *bs,
138 int64_t sector_num, int nb_sectors,
139 BdrvRequestFlags flags)
140{
141 return bdrv_co_write_zeroes(bs->file->bs, sector_num, nb_sectors, flags);
142}
143
144static int coroutine_fn raw_co_discard(BlockDriverState *bs,
145 int64_t sector_num, int nb_sectors)
146{
147 return bdrv_co_discard(bs->file->bs, sector_num, nb_sectors);
148}
149
150static int64_t raw_getlength(BlockDriverState *bs)
151{
152 return bdrv_getlength(bs->file->bs);
153}
154
155static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
156{
157 return bdrv_get_info(bs->file->bs, bdi);
158}
159
160static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
161{
162 bs->bl = bs->file->bs->bl;
163}
164
165static int raw_truncate(BlockDriverState *bs, int64_t offset)
166{
167 return bdrv_truncate(bs->file->bs, offset);
168}
169
170static int raw_media_changed(BlockDriverState *bs)
171{
172 return bdrv_media_changed(bs->file->bs);
173}
174
175static void raw_eject(BlockDriverState *bs, bool eject_flag)
176{
177 bdrv_eject(bs->file->bs, eject_flag);
178}
179
180static void raw_lock_medium(BlockDriverState *bs, bool locked)
181{
182 bdrv_lock_medium(bs->file->bs, locked);
183}
184
185static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs,
186 unsigned long int req, void *buf,
187 BlockCompletionFunc *cb,
188 void *opaque)
189{
190 return bdrv_aio_ioctl(bs->file->bs, req, buf, cb, opaque);
191}
192
193static int raw_has_zero_init(BlockDriverState *bs)
194{
195 return bdrv_has_zero_init(bs->file->bs);
196}
197
198static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
199{
200 Error *local_err = NULL;
201 int ret;
202
203 ret = bdrv_create_file(filename, opts, &local_err);
204 if (local_err) {
205 error_propagate(errp, local_err);
206 }
207 return ret;
208}
209
210static int raw_open(BlockDriverState *bs, QDict *options, int flags,
211 Error **errp)
212{
213 bs->sg = bs->file->bs->sg;
214
215 if (bs->probed && !bdrv_is_read_only(bs)) {
216 fprintf(stderr,
217 "WARNING: Image format was not specified for '%s' and probing "
218 "guessed raw.\n"
219 " Automatically detecting the format is dangerous for "
220 "raw images, write operations on block 0 will be restricted.\n"
221 " Specify the 'raw' format explicitly to remove the "
222 "restrictions.\n",
223 bs->file->bs->filename);
224 }
225
226 return 0;
227}
228
229static void raw_close(BlockDriverState *bs)
230{
231}
232
233static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
234{
235
236
237
238 return 1;
239}
240
241static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
242{
243 return bdrv_probe_blocksizes(bs->file->bs, bsz);
244}
245
246static int raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
247{
248 return bdrv_probe_geometry(bs->file->bs, geo);
249}
250
251BlockDriver bdrv_raw = {
252 .format_name = "raw",
253 .bdrv_probe = &raw_probe,
254 .bdrv_reopen_prepare = &raw_reopen_prepare,
255 .bdrv_open = &raw_open,
256 .bdrv_close = &raw_close,
257 .bdrv_create = &raw_create,
258 .bdrv_co_readv = &raw_co_readv,
259 .bdrv_co_writev = &raw_co_writev,
260 .bdrv_co_writev_flags = &raw_co_writev_flags,
261 .supported_write_flags = BDRV_REQ_FUA,
262 .bdrv_co_write_zeroes = &raw_co_write_zeroes,
263 .bdrv_co_discard = &raw_co_discard,
264 .bdrv_co_get_block_status = &raw_co_get_block_status,
265 .bdrv_truncate = &raw_truncate,
266 .bdrv_getlength = &raw_getlength,
267 .has_variable_length = true,
268 .bdrv_get_info = &raw_get_info,
269 .bdrv_refresh_limits = &raw_refresh_limits,
270 .bdrv_probe_blocksizes = &raw_probe_blocksizes,
271 .bdrv_probe_geometry = &raw_probe_geometry,
272 .bdrv_media_changed = &raw_media_changed,
273 .bdrv_eject = &raw_eject,
274 .bdrv_lock_medium = &raw_lock_medium,
275 .bdrv_aio_ioctl = &raw_aio_ioctl,
276 .create_opts = &raw_create_opts,
277 .bdrv_has_zero_init = &raw_has_zero_init
278};
279
280static void bdrv_raw_init(void)
281{
282 bdrv_register(&bdrv_raw);
283}
284
285block_init(bdrv_raw_init);
286