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_preadv(BlockDriverState *bs, uint64_t offset,
54 uint64_t bytes, QEMUIOVector *qiov,
55 int flags)
56{
57 BLKDBG_EVENT(bs->file, BLKDBG_READ_AIO);
58 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
59}
60
61static int coroutine_fn raw_co_pwritev(BlockDriverState *bs, uint64_t offset,
62 uint64_t bytes, QEMUIOVector *qiov,
63 int flags)
64{
65 void *buf = NULL;
66 BlockDriver *drv;
67 QEMUIOVector local_qiov;
68 int ret;
69
70 if (bs->probed && offset < BLOCK_PROBE_BUF_SIZE && bytes) {
71
72
73
74 QEMU_BUILD_BUG_ON(BLOCK_PROBE_BUF_SIZE != 512);
75 QEMU_BUILD_BUG_ON(BDRV_SECTOR_SIZE != 512);
76 assert(offset == 0 && bytes >= BLOCK_PROBE_BUF_SIZE);
77
78 buf = qemu_try_blockalign(bs->file->bs, 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_pwritev(bs->file, offset, bytes, qiov, flags);
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 BlockDriverState **file)
119{
120 *pnum = nb_sectors;
121 *file = bs->file->bs;
122 return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID | BDRV_BLOCK_DATA |
123 (sector_num << BDRV_SECTOR_BITS);
124}
125
126static int coroutine_fn raw_co_pwrite_zeroes(BlockDriverState *bs,
127 int64_t offset, int count,
128 BdrvRequestFlags flags)
129{
130 return bdrv_co_pwrite_zeroes(bs->file, offset, count, flags);
131}
132
133static int coroutine_fn raw_co_pdiscard(BlockDriverState *bs,
134 int64_t offset, int count)
135{
136 return bdrv_co_pdiscard(bs->file->bs, offset, count);
137}
138
139static int64_t raw_getlength(BlockDriverState *bs)
140{
141 return bdrv_getlength(bs->file->bs);
142}
143
144static int raw_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
145{
146 return bdrv_get_info(bs->file->bs, bdi);
147}
148
149static void raw_refresh_limits(BlockDriverState *bs, Error **errp)
150{
151 if (bs->probed) {
152
153
154
155 bs->bl.request_alignment = BDRV_SECTOR_SIZE;
156 }
157}
158
159static int raw_truncate(BlockDriverState *bs, int64_t offset)
160{
161 return bdrv_truncate(bs->file->bs, offset);
162}
163
164static int raw_media_changed(BlockDriverState *bs)
165{
166 return bdrv_media_changed(bs->file->bs);
167}
168
169static void raw_eject(BlockDriverState *bs, bool eject_flag)
170{
171 bdrv_eject(bs->file->bs, eject_flag);
172}
173
174static void raw_lock_medium(BlockDriverState *bs, bool locked)
175{
176 bdrv_lock_medium(bs->file->bs, locked);
177}
178
179static BlockAIOCB *raw_aio_ioctl(BlockDriverState *bs,
180 unsigned long int req, void *buf,
181 BlockCompletionFunc *cb,
182 void *opaque)
183{
184 return bdrv_aio_ioctl(bs->file->bs, req, buf, cb, opaque);
185}
186
187static int raw_has_zero_init(BlockDriverState *bs)
188{
189 return bdrv_has_zero_init(bs->file->bs);
190}
191
192static int raw_create(const char *filename, QemuOpts *opts, Error **errp)
193{
194 return bdrv_create_file(filename, opts, errp);
195}
196
197static int raw_open(BlockDriverState *bs, QDict *options, int flags,
198 Error **errp)
199{
200 bs->sg = bs->file->bs->sg;
201 bs->supported_write_flags = BDRV_REQ_FUA &
202 bs->file->bs->supported_write_flags;
203 bs->supported_zero_flags = (BDRV_REQ_FUA | BDRV_REQ_MAY_UNMAP) &
204 bs->file->bs->supported_zero_flags;
205
206 if (bs->probed && !bdrv_is_read_only(bs)) {
207 fprintf(stderr,
208 "WARNING: Image format was not specified for '%s' and probing "
209 "guessed raw.\n"
210 " Automatically detecting the format is dangerous for "
211 "raw images, write operations on block 0 will be restricted.\n"
212 " Specify the 'raw' format explicitly to remove the "
213 "restrictions.\n",
214 bs->file->bs->filename);
215 }
216
217 return 0;
218}
219
220static void raw_close(BlockDriverState *bs)
221{
222}
223
224static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
225{
226
227
228
229 return 1;
230}
231
232static int raw_probe_blocksizes(BlockDriverState *bs, BlockSizes *bsz)
233{
234 return bdrv_probe_blocksizes(bs->file->bs, bsz);
235}
236
237static int raw_probe_geometry(BlockDriverState *bs, HDGeometry *geo)
238{
239 return bdrv_probe_geometry(bs->file->bs, geo);
240}
241
242BlockDriver bdrv_raw = {
243 .format_name = "raw",
244 .bdrv_probe = &raw_probe,
245 .bdrv_reopen_prepare = &raw_reopen_prepare,
246 .bdrv_open = &raw_open,
247 .bdrv_close = &raw_close,
248 .bdrv_create = &raw_create,
249 .bdrv_co_preadv = &raw_co_preadv,
250 .bdrv_co_pwritev = &raw_co_pwritev,
251 .bdrv_co_pwrite_zeroes = &raw_co_pwrite_zeroes,
252 .bdrv_co_pdiscard = &raw_co_pdiscard,
253 .bdrv_co_get_block_status = &raw_co_get_block_status,
254 .bdrv_truncate = &raw_truncate,
255 .bdrv_getlength = &raw_getlength,
256 .has_variable_length = true,
257 .bdrv_get_info = &raw_get_info,
258 .bdrv_refresh_limits = &raw_refresh_limits,
259 .bdrv_probe_blocksizes = &raw_probe_blocksizes,
260 .bdrv_probe_geometry = &raw_probe_geometry,
261 .bdrv_media_changed = &raw_media_changed,
262 .bdrv_eject = &raw_eject,
263 .bdrv_lock_medium = &raw_lock_medium,
264 .bdrv_aio_ioctl = &raw_aio_ioctl,
265 .create_opts = &raw_create_opts,
266 .bdrv_has_zero_init = &raw_has_zero_init
267};
268
269static void bdrv_raw_init(void)
270{
271 bdrv_register(&bdrv_raw);
272}
273
274block_init(bdrv_raw_init);
275