]> git.proxmox.com Git - qemu.git/blame - block/raw.c
vnc: rename vnc-encoding-* vnc-enc-*
[qemu.git] / block / raw.c
CommitLineData
84a12e66
CH
1
2#include "qemu-common.h"
3#include "block_int.h"
4#include "module.h"
5
66f82cee 6static int raw_open(BlockDriverState *bs, int flags)
84a12e66 7{
66f82cee
KW
8 bs->sg = bs->file->sg;
9 return 0;
84a12e66
CH
10}
11
79368c81
AL
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
84a12e66
CH
51static int raw_read(BlockDriverState *bs, int64_t sector_num,
52 uint8_t *buf, int nb_sectors)
53{
66f82cee 54 return bdrv_read(bs->file, sector_num, buf, nb_sectors);
84a12e66
CH
55}
56
79368c81
AL
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
84a12e66
CH
69static int raw_write(BlockDriverState *bs, int64_t sector_num,
70 const uint8_t *buf, int nb_sectors)
71{
79368c81
AL
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
66f82cee 88 return bdrv_write(bs->file, sector_num, buf, nb_sectors);
84a12e66
CH
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{
66f82cee 95 return bdrv_aio_readv(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
84a12e66
CH
96}
97
79368c81
AL
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);
116 qemu_free(b);
117}
118
84a12e66
CH
119static BlockDriverAIOCB *raw_aio_writev(BlockDriverState *bs,
120 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
121 BlockDriverCompletionFunc *cb, void *opaque)
122{
79368c81
AL
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
150 b = qemu_malloc(sizeof(*b));
151 b->cb = cb;
152 b->opaque = opaque;
153
154 qemu_iovec_init(&b->qiov, qiov->nalloc);
155 qemu_iovec_concat(&b->qiov, qiov, qiov->size);
156
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
66f82cee 165 return bdrv_aio_writev(bs->file, sector_num, qiov, nb_sectors, cb, opaque);
84a12e66
CH
166}
167
168static void raw_close(BlockDriverState *bs)
169{
84a12e66
CH
170}
171
172static void raw_flush(BlockDriverState *bs)
173{
66f82cee 174 bdrv_flush(bs->file);
84a12e66
CH
175}
176
177static BlockDriverAIOCB *raw_aio_flush(BlockDriverState *bs,
178 BlockDriverCompletionFunc *cb, void *opaque)
179{
66f82cee 180 return bdrv_aio_flush(bs->file, cb, opaque);
84a12e66
CH
181}
182
183static int64_t raw_getlength(BlockDriverState *bs)
184{
66f82cee 185 return bdrv_getlength(bs->file);
84a12e66
CH
186}
187
188static int raw_truncate(BlockDriverState *bs, int64_t offset)
189{
66f82cee 190 return bdrv_truncate(bs->file, offset);
84a12e66
CH
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{
66f82cee 200 return bdrv_is_inserted(bs->file);
84a12e66
CH
201}
202
203static int raw_eject(BlockDriverState *bs, int eject_flag)
204{
66f82cee 205 return bdrv_eject(bs->file, eject_flag);
84a12e66
CH
206}
207
208static int raw_set_locked(BlockDriverState *bs, int locked)
209{
66f82cee 210 bdrv_set_locked(bs->file, locked);
84a12e66
CH
211 return 0;
212}
213
214static int raw_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
215{
66f82cee 216 return bdrv_ioctl(bs->file, req, buf);
84a12e66
CH
217}
218
219static BlockDriverAIOCB *raw_aio_ioctl(BlockDriverState *bs,
220 unsigned long int req, void *buf,
221 BlockDriverCompletionFunc *cb, void *opaque)
222{
66f82cee 223 return bdrv_aio_ioctl(bs->file, req, buf, cb, opaque);
84a12e66
CH
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 BlockDriver bdrv_raw = {
241 .format_name = "raw",
242
66f82cee
KW
243 /* It's really 0, but we need to make qemu_malloc() happy */
244 .instance_size = 1,
84a12e66
CH
245
246 .bdrv_open = raw_open,
247 .bdrv_close = raw_close,
248 .bdrv_read = raw_read,
249 .bdrv_write = raw_write,
250 .bdrv_flush = raw_flush,
251 .bdrv_probe = raw_probe,
252 .bdrv_getlength = raw_getlength,
253 .bdrv_truncate = raw_truncate,
254
255 .bdrv_aio_readv = raw_aio_readv,
256 .bdrv_aio_writev = raw_aio_writev,
257 .bdrv_aio_flush = raw_aio_flush,
258
259 .bdrv_is_inserted = raw_is_inserted,
260 .bdrv_eject = raw_eject,
261 .bdrv_set_locked = raw_set_locked,
262 .bdrv_ioctl = raw_ioctl,
263 .bdrv_aio_ioctl = raw_aio_ioctl,
264
265 .bdrv_create = raw_create,
266 .create_options = raw_create_options,
267};
268
269static void bdrv_raw_init(void)
270{
271 bdrv_register(&bdrv_raw);
272}
273
274block_init(bdrv_raw_init);