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