blob: bfe0915f9d9fb9f2326cef74d4800a2bb56e1d7e [file] [log] [blame]
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01001
2#include "qemu-common.h"
David 'Digit' Turnere1e03df2013-12-15 00:42:21 +01003#include "block/block_int.h"
David 'Digit' Turnere90d6652013-12-14 14:55:12 +01004#include "qemu/module.h"
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +01005
6static int raw_open(BlockDriverState *bs, int flags)
7{
8 bs->sg = bs->file->sg;
9 return 0;
10}
11
12/* check for the user attempting to write something that looks like a
13 block format header to the beginning of the image and fail out.
14*/
15static int check_for_block_signature(BlockDriverState *bs, const uint8_t *buf)
16{
17 static const uint8_t signatures[][4] = {
18 { 'Q', 'F', 'I', 0xfb }, /* qcow/qcow2 */
19 { 'C', 'O', 'W', 'D' }, /* VMDK3 */
20 { 'V', 'M', 'D', 'K' }, /* VMDK4 */
21 { 'O', 'O', 'O', 'M' }, /* UML COW */
22 {}
23 };
24 int i;
25
26 for (i = 0; signatures[i][0] != 0; i++) {
27 if (memcmp(buf, signatures[i], 4) == 0) {
28 return 1;
29 }
30 }
31
32 return 0;
33}
34
35static int check_write_unsafe(BlockDriverState *bs, int64_t sector_num,
36 const uint8_t *buf, int nb_sectors)
37{
38 /* assume that if the user specifies the format explicitly, then assume
39 that they will continue to do so and provide no safety net */
40 if (!bs->probed) {
41 return 0;
42 }
43
44 if (sector_num == 0 && nb_sectors > 0) {
45 return check_for_block_signature(bs, buf);
46 }
47
48 return 0;
49}
50
51static int raw_read(BlockDriverState *bs, int64_t sector_num,
52 uint8_t *buf, int nb_sectors)
53{
54 return bdrv_read(bs->file, sector_num, buf, nb_sectors);
55}
56
57static int raw_write_scrubbed_bootsect(BlockDriverState *bs,
58 const uint8_t *buf)
59{
60 uint8_t bootsect[512];
61
62 /* scrub the dangerous signature */
63 memcpy(bootsect, buf, 512);
64 memset(bootsect, 0, 4);
65
66 return bdrv_write(bs->file, 0, bootsect, 1);
67}
68
69static int raw_write(BlockDriverState *bs, int64_t sector_num,
70 const uint8_t *buf, int nb_sectors)
71{
72 if (check_write_unsafe(bs, sector_num, buf, nb_sectors)) {
73 int ret;
74
75 ret = raw_write_scrubbed_bootsect(bs, buf);
76 if (ret < 0) {
77 return ret;
78 }
79
80 ret = bdrv_write(bs->file, 1, buf + 512, nb_sectors - 1);
81 if (ret < 0) {
82 return ret;
83 }
84
85 return ret + 512;
86 }
87
88 return bdrv_write(bs->file, sector_num, buf, nb_sectors);
89}
90
91static BlockDriverAIOCB *raw_aio_readv(BlockDriverState *bs,
92 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
93 BlockDriverCompletionFunc *cb, void *opaque)
94{
95 return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
96}
97
98typedef struct RawScrubberBounce
99{
100 BlockDriverCompletionFunc *cb;
101 void *opaque;
102 QEMUIOVector qiov;
103} RawScrubberBounce;
104
105static void raw_aio_writev_scrubbed(void *opaque, int ret)
106{
107 RawScrubberBounce *b = opaque;
108
109 if (ret < 0) {
110 b->cb(b->opaque, ret);
111 } else {
112 b->cb(b->opaque, ret + 512);
113 }
114
115 qemu_iovec_destroy(&b->qiov);
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100116 g_free(b);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100117}
118
119static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
120 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
121 BlockDriverCompletionFunc *cb, void *opaque)
122{
123 const uint8_t *first_buf;
124 int first_buf_index = 0, i;
125
126 /* This is probably being paranoid, but handle cases of zero size
127 vectors. */
128 for (i = 0; i < qiov->niov; i++) {
129 if (qiov->iov[i].iov_len) {
130 assert(qiov->iov[i].iov_len >= 512);
131 first_buf_index = i;
132 break;
133 }
134 }
135
136 first_buf = qiov->iov[first_buf_index].iov_base;
137
138 if (check_write_unsafe(bs, sector_num, first_buf, nb_sectors)) {
139 RawScrubberBounce *b;
140 int ret;
141
142 /* write the first sector using sync I/O */
143 ret = raw_write_scrubbed_bootsect(bs, first_buf);
144 if (ret < 0) {
145 return NULL;
146 }
147
148 /* adjust request to be everything but first sector */
149
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100150 b = g_malloc(sizeof(*b));
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100151 b->cb = cb;
152 b->opaque = opaque;
153
154 qemu_iovec_init(&b->qiov, qiov->nalloc);
David 'Digit' Turner1befd342014-01-15 17:56:45 +0100155 qemu_iovec_concat(&b->qiov, qiov, 0, qiov->size);
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100156
157 b->qiov.size -= 512;
158 b->qiov.iov[first_buf_index].iov_base += 512;
159 b->qiov.iov[first_buf_index].iov_len -= 512;
160
161 return bdrv_aio_writev(bs->file, sector_num + 1, &b->qiov,
162 nb_sectors - 1, raw_aio_writev_scrubbed, b);
163 }
164
165 return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
166}
167
168static void raw_close(BlockDriverState *bs)
169{
170}
171
172static void raw_flush(BlockDriverState *bs)
173{
174 bdrv_flush(bs->file);
175}
176
177static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
178 BlockDriverCompletionFunc *cb, void *opaque)
179{
180 return bdrv_aio_flush(bs->file, cb, opaque);
181}
182
183static int64_t raw_getlength(BlockDriverState *bs)
184{
185 return bdrv_getlength(bs->file);
186}
187
188static int raw_truncate(BlockDriverState *bs, int64_t offset)
189{
190 return bdrv_truncate(bs->file, offset);
191}
192
193static int raw_probe(const uint8_t *buf, int buf_size, const char *filename)
194{
195 return 1; /* everything can be opened as raw image */
196}
197
198static int raw_is_inserted(BlockDriverState *bs)
199{
200 return bdrv_is_inserted(bs->file);
201}
202
203static int raw_eject(BlockDriverState *bs, int eject_flag)
204{
205 return bdrv_eject(bs->file, eject_flag);
206}
207
208static int raw_set_locked(BlockDriverState *bs, int locked)
209{
210 bdrv_set_locked(bs->file, locked);
211 return 0;
212}
213
214static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
215{
216 return bdrv_ioctl(bs->file, req, buf);
217}
218
219static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
220 unsigned long int req, void *buf,
221 BlockDriverCompletionFunc *cb, void *opaque)
222{
223 return bdrv_aio_ioctl(bs->file, req, buf, cb, opaque);
224}
225
226static int raw_create(const char *filename, QEMUOptionParameter *options)
227{
228 return bdrv_create_file(filename, options);
229}
230
231static QEMUOptionParameter raw_create_options[] = {
232 {
233 .name = BLOCK_OPT_SIZE,
234 .type = OPT_SIZE,
235 .help = "Virtual disk size"
236 },
237 { NULL }
238};
239
240static int raw_has_zero_init(BlockDriverState *bs)
241{
242 return bdrv_has_zero_init(bs->file);
243}
244
245static BlockDriver bdrv_raw = {
246 .format_name = "raw",
247
David 'Digit' Turneraa8236d2014-01-10 17:02:29 +0100248 /* It's really 0, but we need to make g_malloc() happy */
David 'Digit' Turnercb42a1b2010-12-23 02:54:08 +0100249 .instance_size = 1,
250
251 .bdrv_open = raw_open,
252 .bdrv_close = raw_close,
253 .bdrv_read = raw_read,
254 .bdrv_write = raw_write,
255 .bdrv_flush = raw_flush,
256 .bdrv_probe = raw_probe,
257 .bdrv_getlength = raw_getlength,
258 .bdrv_truncate = raw_truncate,
259
260 .bdrv_aio_readv = raw_aio_readv,
261 .bdrv_aio_writev = raw_aio_writev,
262 .bdrv_aio_flush = raw_aio_flush,
263
264 .bdrv_is_inserted = raw_is_inserted,
265 .bdrv_eject = raw_eject,
266 .bdrv_set_locked = raw_set_locked,
267 .bdrv_ioctl = raw_ioctl,
268 .bdrv_aio_ioctl = raw_aio_ioctl,
269
270 .bdrv_create = raw_create,
271 .create_options = raw_create_options,
272 .bdrv_has_zero_init = raw_has_zero_init,
273};
274
275static void bdrv_raw_init(void)
276{
277 bdrv_register(&bdrv_raw);
278}
279
280block_init(bdrv_raw_init);