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 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | |
37 | |
38 | |
39 | |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | |
48 | |
49 | |
50 | |
51 | |
52 | #include "qemu-common.h" |
53 | #include "block_int.h" |
54 | #include "module.h" |
55 | #include "migration.h" |
56 | |
57 | #if defined(CONFIG_UUID1) |
58 | #include <uuid/uuid.h> |
59 | #else |
60 | |
61 | #include "sysemu.h" /* UUID_FMT */ |
62 | typedef unsigned char uuid_t[16]; |
63 | void uuid_generate(uuid_t out); |
64 | int uuid_is_null(const uuid_t uu); |
65 | void uuid_unparse(const uuid_t uu, char *out); |
66 | #endif |
67 | |
68 | |
69 | |
70 | |
71 | |
72 | |
73 | |
74 | #define CONFIG_VDI_WRITE |
75 | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | #define CONFIG_VDI_STATIC_IMAGE |
83 | |
84 | |
85 | #define BLOCK_OPT_STATIC"static" "static" |
86 | |
87 | #define KiB1024 1024 |
88 | #define MiB(1024 * 1024) (KiB1024 * KiB1024) |
89 | |
90 | #define SECTOR_SIZE512 512 |
91 | #define DEFAULT_CLUSTER_SIZE(1 * (1024 * 1024)) (1 * MiB(1024 * 1024)) |
92 | |
93 | #if defined(CONFIG_VDI_DEBUG) |
94 | #define logout(fmt, ...)((void)0) \ |
95 | fprintf(stderrstderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__) |
96 | #else |
97 | #define logout(fmt, ...)((void)0) ((void)0) |
98 | #endif |
99 | |
100 | |
101 | #define VDI_SIGNATURE0xbeda107f 0xbeda107f |
102 | |
103 | |
104 | #define VDI_VERSION_1_10x00010001 0x00010001 |
105 | |
106 | |
107 | #define VDI_TYPE_DYNAMIC1 1 |
108 | #define VDI_TYPE_STATIC2 2 |
109 | |
110 | |
111 | |
112 | |
113 | |
114 | |
115 | |
116 | #define VDI_TEXT"<<< QEMU VM Virtual Disk Image >>>\n" "<<< QEMU VM Virtual Disk Image >>>\n" |
117 | |
118 | |
119 | #define VDI_UNALLOCATED0xffffffffU 0xffffffffU |
120 | |
121 | |
122 | #define VDI_DISCARDED0xfffffffeU 0xfffffffeU |
123 | |
124 | #define VDI_IS_ALLOCATED(X)((X) < 0xfffffffeU) ((X) < VDI_DISCARDED0xfffffffeU) |
125 | |
126 | #if !defined(CONFIG_UUID1) |
127 | void uuid_generate(uuid_t out) |
128 | { |
129 | memset(out, 0, sizeof(uuid_t)); |
130 | } |
131 | |
132 | int uuid_is_null(const uuid_t uu) |
133 | { |
134 | uuid_t null_uuid = { 0 }; |
135 | return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0; |
136 | } |
137 | |
138 | void uuid_unparse(const uuid_t uu, char *out) |
139 | { |
140 | snprintf(out, 37, UUID_FMT, |
141 | uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], |
142 | uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]); |
143 | } |
144 | #endif |
145 | |
146 | typedef struct { |
147 | char text[0x40]; |
148 | uint32_t signature; |
149 | uint32_t version; |
150 | uint32_t header_size; |
151 | uint32_t image_type; |
152 | uint32_t image_flags; |
153 | char description[256]; |
154 | uint32_t offset_bmap; |
155 | uint32_t offset_data; |
156 | uint32_t cylinders; |
157 | uint32_t heads; |
158 | uint32_t sectors; |
159 | uint32_t sector_size; |
160 | uint32_t unused1; |
161 | uint64_t disk_size; |
162 | uint32_t block_size; |
163 | uint32_t block_extra; |
164 | uint32_t blocks_in_image; |
165 | uint32_t blocks_allocated; |
166 | uuid_t uuid_image; |
167 | uuid_t uuid_last_snap; |
168 | uuid_t uuid_link; |
169 | uuid_t uuid_parent; |
170 | uint64_t unused2[7]; |
171 | } VdiHeader; |
172 | |
173 | typedef struct { |
174 | |
175 | uint32_t *bmap; |
176 | |
177 | uint32_t block_size; |
178 | |
179 | uint32_t block_sectors; |
180 | |
181 | uint32_t bmap_sector; |
182 | |
183 | VdiHeader header; |
184 | |
185 | Error *migration_blocker; |
186 | } BDRVVdiState; |
187 | |
188 | |
189 | |
190 | |
191 | static void uuid_convert(uuid_t uuid) |
192 | { |
193 | bswap32s((uint32_t *)&uuid[0]); |
194 | bswap16s((uint16_t *)&uuid[4]); |
195 | bswap16s((uint16_t *)&uuid[6]); |
196 | } |
197 | |
198 | static void vdi_header_to_cpu(VdiHeader *header) |
199 | { |
200 | le32_to_cpus(&header->signature); |
201 | le32_to_cpus(&header->version); |
202 | le32_to_cpus(&header->header_size); |
203 | le32_to_cpus(&header->image_type); |
204 | le32_to_cpus(&header->image_flags); |
205 | le32_to_cpus(&header->offset_bmap); |
206 | le32_to_cpus(&header->offset_data); |
207 | le32_to_cpus(&header->cylinders); |
208 | le32_to_cpus(&header->heads); |
209 | le32_to_cpus(&header->sectors); |
210 | le32_to_cpus(&header->sector_size); |
211 | le64_to_cpus(&header->disk_size); |
212 | le32_to_cpus(&header->block_size); |
213 | le32_to_cpus(&header->block_extra); |
214 | le32_to_cpus(&header->blocks_in_image); |
215 | le32_to_cpus(&header->blocks_allocated); |
216 | uuid_convert(header->uuid_image); |
217 | uuid_convert(header->uuid_last_snap); |
218 | uuid_convert(header->uuid_link); |
219 | uuid_convert(header->uuid_parent); |
220 | } |
221 | |
222 | static void vdi_header_to_le(VdiHeader *header) |
223 | { |
224 | cpu_to_le32s(&header->signature); |
225 | cpu_to_le32s(&header->version); |
226 | cpu_to_le32s(&header->header_size); |
227 | cpu_to_le32s(&header->image_type); |
228 | cpu_to_le32s(&header->image_flags); |
229 | cpu_to_le32s(&header->offset_bmap); |
230 | cpu_to_le32s(&header->offset_data); |
231 | cpu_to_le32s(&header->cylinders); |
232 | cpu_to_le32s(&header->heads); |
233 | cpu_to_le32s(&header->sectors); |
234 | cpu_to_le32s(&header->sector_size); |
235 | cpu_to_le64s(&header->disk_size); |
236 | cpu_to_le32s(&header->block_size); |
237 | cpu_to_le32s(&header->block_extra); |
238 | cpu_to_le32s(&header->blocks_in_image); |
239 | cpu_to_le32s(&header->blocks_allocated); |
240 | cpu_to_le32s(&header->blocks_allocated); |
241 | uuid_convert(header->uuid_image); |
242 | uuid_convert(header->uuid_last_snap); |
243 | uuid_convert(header->uuid_link); |
244 | uuid_convert(header->uuid_parent); |
245 | } |
246 | |
247 | #if defined(CONFIG_VDI_DEBUG) |
248 | static void vdi_header_print(VdiHeader *header) |
249 | { |
250 | char uuid[37]; |
251 | logout("text %s", header->text)((void)0); |
252 | logout("signature 0x%04x\n", header->signature)((void)0); |
253 | logout("header size 0x%04x\n", header->header_size)((void)0); |
254 | logout("image type 0x%04x\n", header->image_type)((void)0); |
255 | logout("image flags 0x%04x\n", header->image_flags)((void)0); |
256 | logout("description %s\n", header->description)((void)0); |
257 | logout("offset bmap 0x%04x\n", header->offset_bmap)((void)0); |
258 | logout("offset data 0x%04x\n", header->offset_data)((void)0); |
259 | logout("cylinders 0x%04x\n", header->cylinders)((void)0); |
260 | logout("heads 0x%04x\n", header->heads)((void)0); |
261 | logout("sectors 0x%04x\n", header->sectors)((void)0); |
262 | logout("sector size 0x%04x\n", header->sector_size)((void)0); |
263 | logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",((void)0) |
264 | header->disk_size, header->disk_size / MiB)((void)0); |
265 | logout("block size 0x%04x\n", header->block_size)((void)0); |
266 | logout("block extra 0x%04x\n", header->block_extra)((void)0); |
267 | logout("blocks tot. 0x%04x\n", header->blocks_in_image)((void)0); |
268 | logout("blocks all. 0x%04x\n", header->blocks_allocated)((void)0); |
269 | uuid_unparse(header->uuid_image, uuid); |
270 | logout("uuid image %s\n", uuid)((void)0); |
271 | uuid_unparse(header->uuid_last_snap, uuid); |
272 | logout("uuid snap %s\n", uuid)((void)0); |
273 | uuid_unparse(header->uuid_link, uuid); |
274 | logout("uuid link %s\n", uuid)((void)0); |
275 | uuid_unparse(header->uuid_parent, uuid); |
276 | logout("uuid parent %s\n", uuid)((void)0); |
277 | } |
278 | #endif |
279 | |
280 | static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res, |
281 | BdrvCheckMode fix) |
282 | { |
283 | |
284 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; |
285 | uint32_t blocks_allocated = 0; |
286 | uint32_t block; |
287 | uint32_t *bmap; |
288 | logout("\n")((void)0); |
289 | |
290 | if (fix) { |
291 | return -ENOTSUP95; |
292 | } |
293 | |
294 | bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t)); |
295 | memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t)); |
296 | |
297 | |
298 | for (block = 0; block < s->header.blocks_in_image; block++) { |
299 | uint32_t bmap_entry = le32_to_cpu(s->bmap[block]); |
300 | if (VDI_IS_ALLOCATED(bmap_entry)((bmap_entry) < 0xfffffffeU)) { |
301 | if (bmap_entry < s->header.blocks_in_image) { |
302 | blocks_allocated++; |
303 | if (!VDI_IS_ALLOCATED(bmap[bmap_entry])((bmap[bmap_entry]) < 0xfffffffeU)) { |
304 | bmap[bmap_entry] = bmap_entry; |
305 | } else { |
306 | fprintf(stderrstderr, "ERROR: block index %" PRIu32"u" |
307 | " also used by %" PRIu32"u" "\n", bmap[bmap_entry], bmap_entry); |
308 | res->corruptions++; |
309 | } |
310 | } else { |
311 | fprintf(stderrstderr, "ERROR: block index %" PRIu32"u" |
312 | " too large, is %" PRIu32"u" "\n", block, bmap_entry); |
313 | res->corruptions++; |
314 | } |
315 | } |
316 | } |
317 | if (blocks_allocated != s->header.blocks_allocated) { |
318 | fprintf(stderrstderr, "ERROR: allocated blocks mismatch, is %" PRIu32"u" |
319 | ", should be %" PRIu32"u" "\n", |
320 | blocks_allocated, s->header.blocks_allocated); |
321 | res->corruptions++; |
322 | } |
323 | |
324 | g_free(bmap); |
325 | |
326 | return 0; |
327 | } |
328 | |
329 | static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
330 | { |
331 | |
332 | |
333 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; |
334 | logout("\n")((void)0); |
335 | bdi->cluster_size = s->block_size; |
336 | bdi->vm_state_offset = 0; |
337 | return 0; |
338 | } |
339 | |
340 | static int vdi_make_empty(BlockDriverState *bs) |
341 | { |
342 | |
343 | logout("\n")((void)0); |
344 | |
345 | return 0; |
346 | } |
347 | |
348 | static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename) |
349 | { |
350 | const VdiHeader *header = (const VdiHeader *)buf; |
351 | int result = 0; |
352 | |
353 | logout("\n")((void)0); |
354 | |
355 | if (buf_size < sizeof(*header)) { |
356 | |
357 | } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE0xbeda107f) { |
358 | result = 100; |
359 | } |
360 | |
361 | if (result == 0) { |
362 | logout("no vdi image\n")((void)0); |
363 | } else { |
364 | logout("%s", header->text)((void)0); |
365 | } |
366 | |
367 | return result; |
368 | } |
369 | |
370 | static int vdi_open(BlockDriverState *bs, int flags) |
371 | { |
372 | BDRVVdiState *s = bs->opaque; |
373 | VdiHeader header; |
374 | size_t bmap_size; |
375 | |
376 | logout("\n")((void)0); |
377 | |
378 | if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) { |
379 | goto fail; |
380 | } |
381 | |
382 | vdi_header_to_cpu(&header); |
383 | #if defined(CONFIG_VDI_DEBUG) |
384 | vdi_header_print(&header); |
385 | #endif |
386 | |
387 | if (header.disk_size % SECTOR_SIZE512 != 0) { |
388 | |
389 | |
390 | |
391 | logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size)((void)0); |
392 | header.disk_size += SECTOR_SIZE512 - 1; |
393 | header.disk_size &= ~(SECTOR_SIZE512 - 1); |
394 | } |
395 | |
396 | if (header.version != VDI_VERSION_1_10x00010001) { |
397 | logout("unsupported version %u.%u\n",((void)0) |
398 | header.version >> 16, header.version & 0xffff)((void)0); |
399 | goto fail; |
400 | } else if (header.offset_bmap % SECTOR_SIZE512 != 0) { |
401 | |
402 | logout("unsupported block map offset 0x%x B\n", header.offset_bmap)((void)0); |
403 | goto fail; |
404 | } else if (header.offset_data % SECTOR_SIZE512 != 0) { |
405 | |
406 | logout("unsupported data offset 0x%x B\n", header.offset_data)((void)0); |
407 | goto fail; |
408 | } else if (header.sector_size != SECTOR_SIZE512) { |
409 | logout("unsupported sector size %u B\n", header.sector_size)((void)0); |
410 | goto fail; |
411 | } else if (header.block_size != 1 * MiB(1024 * 1024)) { |
412 | logout("unsupported block size %u B\n", header.block_size)((void)0); |
413 | goto fail; |
414 | } else if (header.disk_size > |
415 | (uint64_t)header.blocks_in_image * header.block_size) { |
416 | logout("unsupported disk size %" PRIu64 " B\n", header.disk_size)((void)0); |
417 | goto fail; |
418 | } else if (!uuid_is_null(header.uuid_link)) { |
419 | logout("link uuid != 0, unsupported\n")((void)0); |
420 | goto fail; |
421 | } else if (!uuid_is_null(header.uuid_parent)) { |
422 | logout("parent uuid != 0, unsupported\n")((void)0); |
423 | goto fail; |
424 | } |
425 | |
426 | bs->total_sectors = header.disk_size / SECTOR_SIZE512; |
427 | |
428 | s->block_size = header.block_size; |
429 | s->block_sectors = header.block_size / SECTOR_SIZE512; |
430 | s->bmap_sector = header.offset_bmap / SECTOR_SIZE512; |
431 | s->header = header; |
432 | |
433 | bmap_size = header.blocks_in_image * sizeof(uint32_t); |
434 | bmap_size = (bmap_size + SECTOR_SIZE512 - 1) / SECTOR_SIZE512; |
435 | if (bmap_size > 0) { |
436 | s->bmap = g_malloc(bmap_size * SECTOR_SIZE512); |
437 | } |
438 | if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) { |
439 | goto fail_free_bmap; |
440 | } |
441 | |
442 | |
443 | error_set(&s->migration_blocker, |
444 | QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED"{ 'class': 'BlockFormatFeatureNotSupported', 'data': { 'format': %s, 'name': %s, 'feature': %s } }", |
445 | "vdi", bs->device_name, "live migration"); |
446 | migrate_add_blocker(s->migration_blocker); |
447 | |
448 | return 0; |
449 | |
450 | fail_free_bmap: |
451 | g_free(s->bmap); |
452 | |
453 | fail: |
454 | return -1; |
455 | } |
456 | |
457 | static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs, |
458 | int64_t sector_num, int nb_sectors, int *pnum) |
459 | { |
460 | |
461 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; |
462 | size_t bmap_index = sector_num / s->block_sectors; |
463 | size_t sector_in_block = sector_num % s->block_sectors; |
464 | int n_sectors = s->block_sectors - sector_in_block; |
465 | uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]); |
466 | logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum)((void)0); |
467 | if (n_sectors > nb_sectors) { |
468 | n_sectors = nb_sectors; |
469 | } |
470 | *pnum = n_sectors; |
471 | return VDI_IS_ALLOCATED(bmap_entry)((bmap_entry) < 0xfffffffeU); |
472 | } |
473 | |
474 | static int vdi_co_read(BlockDriverState *bs, |
475 | int64_t sector_num, uint8_t *buf, int nb_sectors) |
476 | { |
477 | BDRVVdiState *s = bs->opaque; |
478 | uint32_t bmap_entry; |
479 | uint32_t block_index; |
480 | uint32_t sector_in_block; |
481 | uint32_t n_sectors; |
482 | int ret = 0; |
483 | |
484 | logout("\n")((void)0); |
485 | |
486 | while (ret >= 0 && nb_sectors > 0) { |
487 | block_index = sector_num / s->block_sectors; |
488 | sector_in_block = sector_num % s->block_sectors; |
489 | n_sectors = s->block_sectors - sector_in_block; |
490 | if (n_sectors > nb_sectors) { |
491 | n_sectors = nb_sectors; |
492 | } |
493 | |
494 | logout("will read %u sectors starting at sector %" PRIu64 "\n",((void)0) |
495 | n_sectors, sector_num)((void)0); |
496 | |
497 | |
498 | bmap_entry = le32_to_cpu(s->bmap[block_index]); |
499 | if (!VDI_IS_ALLOCATED(bmap_entry)((bmap_entry) < 0xfffffffeU)) { |
500 | |
501 | memset(buf, 0, n_sectors * SECTOR_SIZE512); |
502 | ret = 0; |
503 | } else { |
504 | uint64_t offset = s->header.offset_data / SECTOR_SIZE512 + |
505 | (uint64_t)bmap_entry * s->block_sectors + |
506 | sector_in_block; |
507 | ret = bdrv_read(bs->file, offset, buf, n_sectors); |
508 | } |
509 | logout("%u sectors read\n", n_sectors)((void)0); |
510 | |
511 | nb_sectors -= n_sectors; |
512 | sector_num += n_sectors; |
513 | buf += n_sectors * SECTOR_SIZE512; |
514 | } |
515 | |
516 | return ret; |
517 | } |
518 | |
519 | static int vdi_co_write(BlockDriverState *bs, |
520 | int64_t sector_num, const uint8_t *buf, int nb_sectors) |
521 | { |
522 | BDRVVdiState *s = bs->opaque; |
523 | uint32_t bmap_entry; |
524 | uint32_t block_index; |
525 | uint32_t sector_in_block; |
526 | uint32_t n_sectors; |
527 | uint32_t bmap_first = VDI_UNALLOCATED0xffffffffU; |
528 | uint32_t bmap_last = VDI_UNALLOCATED0xffffffffU; |
529 | uint8_t *block = NULL((void*)0); |
530 | int ret = 0; |
531 | |
532 | logout("\n")((void)0); |
533 | |
534 | while (ret >= 0 && nb_sectors > 0) { |
535 | block_index = sector_num / s->block_sectors; |
536 | sector_in_block = sector_num % s->block_sectors; |
537 | n_sectors = s->block_sectors - sector_in_block; |
538 | if (n_sectors > nb_sectors) { |
539 | n_sectors = nb_sectors; |
540 | } |
541 | |
542 | logout("will write %u sectors starting at sector %" PRIu64 "\n",((void)0) |
543 | n_sectors, sector_num)((void)0); |
544 | |
545 | |
546 | bmap_entry = le32_to_cpu(s->bmap[block_index]); |
547 | if (!VDI_IS_ALLOCATED(bmap_entry)((bmap_entry) < 0xfffffffeU)) { |
548 | |
549 | uint64_t offset; |
550 | bmap_entry = s->header.blocks_allocated; |
551 | s->bmap[block_index] = cpu_to_le32(bmap_entry); |
552 | s->header.blocks_allocated++; |
553 | offset = s->header.offset_data / SECTOR_SIZE512 + |
554 | (uint64_t)bmap_entry * s->block_sectors; |
555 | if (block == NULL((void*)0)) { |
556 | block = g_malloc(s->block_size); |
557 | bmap_first = block_index; |
558 | } |
559 | bmap_last = block_index; |
560 | |
561 | memset(block, 0, sector_in_block * SECTOR_SIZE512); |
562 | memcpy(block + sector_in_block * SECTOR_SIZE512, |
563 | buf, n_sectors * SECTOR_SIZE512); |
564 | memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE512, 0, |
565 | (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE512); |
566 | ret = bdrv_write(bs->file, offset, block, s->block_sectors); |
567 | } else { |
568 | uint64_t offset = s->header.offset_data / SECTOR_SIZE512 + |
569 | (uint64_t)bmap_entry * s->block_sectors + |
570 | sector_in_block; |
571 | ret = bdrv_write(bs->file, offset, buf, n_sectors); |
572 | } |
573 | |
574 | nb_sectors -= n_sectors; |
575 | sector_num += n_sectors; |
576 | buf += n_sectors * SECTOR_SIZE512; |
577 | |
578 | logout("%u sectors written\n", n_sectors)((void)0); |
579 | } |
580 | |
581 | logout("finished data write\n")((void)0); |
582 | if (ret < 0) { |
583 | return ret; |
584 | } |
585 | |
586 | if (block) { |
587 | |
588 | VdiHeader *header = (VdiHeader *) block; |
589 | uint8_t *base; |
590 | uint64_t offset; |
591 | |
592 | logout("now writing modified header\n")((void)0); |
593 | assert(VDI_IS_ALLOCATED(bmap_first))((((bmap_first) < 0xfffffffeU)) ? (void) (0) : __assert_fail ("((bmap_first) < 0xfffffffeU)", "/home/stefan/src/qemu/qemu.org/qemu/block/vdi.c" , 593, __PRETTY_FUNCTION__)); |
594 | *header = s->header; |
595 | vdi_header_to_le(header); |
596 | ret = bdrv_write(bs->file, 0, block, 1); |
597 | g_free(block); |
598 | block = NULL((void*)0); |
599 | |
600 | if (ret < 0) { |
601 | return ret; |
602 | } |
603 | |
604 | logout("now writing modified block map entry %u...%u\n",((void)0) |
605 | bmap_first, bmap_last)((void)0); |
606 | |
607 | bmap_first /= (SECTOR_SIZE512 / sizeof(uint32_t)); |
608 | bmap_last /= (SECTOR_SIZE512 / sizeof(uint32_t)); |
609 | n_sectors = bmap_last - bmap_first + 1; |
610 | offset = s->bmap_sector + bmap_first; |
611 | base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE512; |
612 | logout("will write %u block map sectors starting from entry %u\n",((void)0) |
613 | n_sectors, bmap_first)((void)0); |
614 | ret = bdrv_write(bs->file, offset, base, n_sectors); |
615 | } |
616 | |
617 | return ret; |
618 | } |
619 | |
620 | static int vdi_create(const char *filename, QEMUOptionParameter *options) |
621 | { |
622 | int fd; |
623 | int result = 0; |
624 | uint64_t bytes = 0; |
625 | uint32_t blocks; |
626 | size_t block_size = DEFAULT_CLUSTER_SIZE(1 * (1024 * 1024)); |
627 | uint32_t image_type = VDI_TYPE_DYNAMIC1; |
628 | VdiHeader header; |
629 | size_t i; |
630 | size_t bmap_size; |
631 | uint32_t *bmap; |
632 | |
633 | logout("\n")((void)0); |
634 | |
635 | |
636 | while (options && options->name) { |
| 1 | Loop condition is true. Entering loop body |
|
| 3 | Loop condition is true. Entering loop body |
|
| 7 | Loop condition is false. Execution continues on line 656 |
|
637 | if (!strcmp(options->name, BLOCK_OPT_SIZE"size")) { |
| |
| |
638 | bytes = options->value.n; |
639 | #if defined(CONFIG_VDI_BLOCK_SIZE) |
640 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE"cluster_size")) { |
641 | if (options->value.n) { |
642 | |
643 | block_size = options->value.n; |
644 | } |
645 | #endif |
646 | #if defined(CONFIG_VDI_STATIC_IMAGE) |
647 | } else if (!strcmp(options->name, BLOCK_OPT_STATIC"static")) { |
| |
648 | if (options->value.n) { |
| |
649 | image_type = VDI_TYPE_STATIC2; |
650 | } |
651 | #endif |
652 | } |
653 | options++; |
654 | } |
655 | |
656 | fd = open(filename, O_WRONLY01 | O_CREAT0100 | O_TRUNC01000 | O_BINARY0 | O_LARGEFILE0, |
657 | 0644); |
658 | if (fd < 0) { |
| |
659 | return -errno(*__errno_location ()); |
660 | } |
661 | |
662 | |
663 | |
664 | blocks = (bytes + block_size - 1) / block_size; |
665 | |
666 | bmap_size = blocks * sizeof(uint32_t); |
667 | bmap_size = ((bmap_size + SECTOR_SIZE512 - 1) & ~(SECTOR_SIZE512 -1)); |
668 | |
669 | memset(&header, 0, sizeof(header)); |
670 | pstrcpy(header.text, sizeof(header.text), VDI_TEXT"<<< QEMU VM Virtual Disk Image >>>\n"); |
671 | header.signature = VDI_SIGNATURE0xbeda107f; |
672 | header.version = VDI_VERSION_1_10x00010001; |
673 | header.header_size = 0x180; |
674 | header.image_type = image_type; |
675 | header.offset_bmap = 0x200; |
676 | header.offset_data = 0x200 + bmap_size; |
677 | header.sector_size = SECTOR_SIZE512; |
678 | header.disk_size = bytes; |
679 | header.block_size = block_size; |
680 | header.blocks_in_image = blocks; |
681 | if (image_type == VDI_TYPE_STATIC2) { |
| |
682 | header.blocks_allocated = blocks; |
683 | } |
684 | uuid_generate(header.uuid_image); |
685 | uuid_generate(header.uuid_last_snap); |
686 | |
687 | #if defined(CONFIG_VDI_DEBUG) |
688 | vdi_header_print(&header); |
689 | #endif |
690 | vdi_header_to_le(&header); |
691 | if (write(fd, &header, sizeof(header)) < 0) { |
| |
692 | result = -errno(*__errno_location ()); |
693 | } |
694 | |
695 | bmap = NULL((void*)0); |
| 11 | Null pointer value stored to 'bmap' |
|
696 | if (bmap_size > 0) { |
| |
697 | bmap = (uint32_t *)g_malloc0(bmap_size); |
698 | } |
699 | for (i = 0; i < blocks; i++) { |
| 13 | Loop condition is true. Entering loop body |
|
700 | if (image_type == VDI_TYPE_STATIC2) { |
| |
701 | bmap[i] = i; |
| 15 | Dereference of null pointer |
|
702 | } else { |
703 | bmap[i] = VDI_UNALLOCATED0xffffffffU; |
704 | } |
705 | } |
706 | if (write(fd, bmap, bmap_size) < 0) { |
707 | result = -errno(*__errno_location ()); |
708 | } |
709 | g_free(bmap); |
710 | if (image_type == VDI_TYPE_STATIC2) { |
711 | if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) { |
712 | result = -errno(*__errno_location ()); |
713 | } |
714 | } |
715 | |
716 | if (close(fd) < 0) { |
717 | result = -errno(*__errno_location ()); |
718 | } |
719 | |
720 | return result; |
721 | } |
722 | |
723 | static void vdi_close(BlockDriverState *bs) |
724 | { |
725 | BDRVVdiState *s = bs->opaque; |
726 | |
727 | g_free(s->bmap); |
728 | |
729 | migrate_del_blocker(s->migration_blocker); |
730 | error_free(s->migration_blocker); |
731 | } |
732 | |
733 | static QEMUOptionParameter vdi_create_options[] = { |
734 | { |
735 | .name = BLOCK_OPT_SIZE"size", |
736 | .type = OPT_SIZE, |
737 | .help = "Virtual disk size" |
738 | }, |
739 | #if defined(CONFIG_VDI_BLOCK_SIZE) |
740 | { |
741 | .name = BLOCK_OPT_CLUSTER_SIZE"cluster_size", |
742 | .type = OPT_SIZE, |
743 | .help = "VDI cluster (block) size", |
744 | .value = { .n = DEFAULT_CLUSTER_SIZE(1 * (1024 * 1024)) }, |
745 | }, |
746 | #endif |
747 | #if defined(CONFIG_VDI_STATIC_IMAGE) |
748 | { |
749 | .name = BLOCK_OPT_STATIC"static", |
750 | .type = OPT_FLAG, |
751 | .help = "VDI static (pre-allocated) image" |
752 | }, |
753 | #endif |
754 | |
755 | { NULL((void*)0) } |
756 | }; |
757 | |
758 | static BlockDriver bdrv_vdi = { |
759 | .format_name = "vdi", |
760 | .instance_size = sizeof(BDRVVdiState), |
761 | .bdrv_probe = vdi_probe, |
762 | .bdrv_open = vdi_open, |
763 | .bdrv_close = vdi_close, |
764 | .bdrv_create = vdi_create, |
765 | .bdrv_co_is_allocated = vdi_co_is_allocated, |
766 | .bdrv_make_empty = vdi_make_empty, |
767 | |
768 | .bdrv_read = vdi_co_read, |
769 | #if defined(CONFIG_VDI_WRITE) |
770 | .bdrv_write = vdi_co_write, |
771 | #endif |
772 | |
773 | .bdrv_get_info = vdi_get_info, |
774 | |
775 | .create_options = vdi_create_options, |
776 | .bdrv_check = vdi_check, |
777 | }; |
778 | |
779 | static void bdrv_vdi_init(void) |
780 | { |
781 | logout("\n")((void)0); |
782 | bdrv_register(&bdrv_vdi); |
783 | } |
784 | |
785 | block_init(bdrv_vdi_init)static void __attribute__((constructor)) do_qemu_init_bdrv_vdi_init (void) { register_module_init(bdrv_vdi_init, MODULE_INIT_BLOCK ); }; |