exec: Make ldq/ldub_*_phys input an AddressSpace
[qemu/ar7.git] / block / vdi.c
blob2d7490f173db0e717b0fdf3b03172f4420a39f42
1 /*
2 * Block driver for the Virtual Disk Image (VDI) format
4 * Copyright (c) 2009, 2012 Stefan Weil
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) version 3 or any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * Reference:
20 * http://forums.virtualbox.org/viewtopic.php?t=8046
22 * This driver supports create / read / write operations on VDI images.
24 * Todo (see also TODO in code):
26 * Some features like snapshots are still missing.
28 * Deallocation of zero-filled blocks and shrinking images are missing, too
29 * (might be added to common block layer).
31 * Allocation of blocks could be optimized (less writes to block map and
32 * header).
34 * Read and write of adjacents blocks could be done in one operation
35 * (current code uses one operation per block (1 MiB).
37 * The code is not thread safe (missing locks for changes in header and
38 * block table, no problem with current QEMU).
40 * Hints:
42 * Blocks (VDI documentation) correspond to clusters (QEMU).
43 * QEMU's backing files could be implemented using VDI snapshot files (TODO).
44 * VDI snapshot files may also contain the complete machine state.
45 * Maybe this machine state can be converted to QEMU PC machine snapshot data.
47 * The driver keeps a block cache (little endian entries) in memory.
48 * For the standard block size (1 MiB), a 1 TiB disk will use 4 MiB RAM,
49 * so this seems to be reasonable.
52 #include "qemu-common.h"
53 #include "block/block_int.h"
54 #include "qemu/module.h"
55 #include "migration/migration.h"
57 #if defined(CONFIG_UUID)
58 #include <uuid/uuid.h>
59 #else
60 /* TODO: move uuid emulation to some central place in QEMU. */
61 #include "sysemu/sysemu.h" /* UUID_FMT */
62 typedef unsigned char uuid_t[16];
63 #endif
65 /* Code configuration options. */
67 /* Enable debug messages. */
68 //~ #define CONFIG_VDI_DEBUG
70 /* Support write operations on VDI images. */
71 #define CONFIG_VDI_WRITE
73 /* Support non-standard block (cluster) size. This is untested.
74 * Maybe it will be needed for very large images.
76 //~ #define CONFIG_VDI_BLOCK_SIZE
78 /* Support static (fixed, pre-allocated) images. */
79 #define CONFIG_VDI_STATIC_IMAGE
81 /* Command line option for static images. */
82 #define BLOCK_OPT_STATIC "static"
84 #define KiB 1024
85 #define MiB (KiB * KiB)
87 #define SECTOR_SIZE 512
88 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
90 #if defined(CONFIG_VDI_DEBUG)
91 #define logout(fmt, ...) \
92 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
93 #else
94 #define logout(fmt, ...) ((void)0)
95 #endif
97 /* Image signature. */
98 #define VDI_SIGNATURE 0xbeda107f
100 /* Image version. */
101 #define VDI_VERSION_1_1 0x00010001
103 /* Image type. */
104 #define VDI_TYPE_DYNAMIC 1
105 #define VDI_TYPE_STATIC 2
107 /* Innotek / SUN images use these strings in header.text:
108 * "<<< innotek VirtualBox Disk Image >>>\n"
109 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
110 * "<<< Sun VirtualBox Disk Image >>>\n"
111 * The value does not matter, so QEMU created images use a different text.
113 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
115 /* A never-allocated block; semantically arbitrary content. */
116 #define VDI_UNALLOCATED 0xffffffffU
118 /* A discarded (no longer allocated) block; semantically zero-filled. */
119 #define VDI_DISCARDED 0xfffffffeU
121 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
123 #if !defined(CONFIG_UUID)
124 static inline void uuid_generate(uuid_t out)
126 memset(out, 0, sizeof(uuid_t));
129 static inline int uuid_is_null(const uuid_t uu)
131 uuid_t null_uuid = { 0 };
132 return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0;
135 static inline void uuid_unparse(const uuid_t uu, char *out)
137 snprintf(out, 37, UUID_FMT,
138 uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7],
139 uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
141 #endif
143 typedef struct {
144 char text[0x40];
145 uint32_t signature;
146 uint32_t version;
147 uint32_t header_size;
148 uint32_t image_type;
149 uint32_t image_flags;
150 char description[256];
151 uint32_t offset_bmap;
152 uint32_t offset_data;
153 uint32_t cylinders; /* disk geometry, unused here */
154 uint32_t heads; /* disk geometry, unused here */
155 uint32_t sectors; /* disk geometry, unused here */
156 uint32_t sector_size;
157 uint32_t unused1;
158 uint64_t disk_size;
159 uint32_t block_size;
160 uint32_t block_extra; /* unused here */
161 uint32_t blocks_in_image;
162 uint32_t blocks_allocated;
163 uuid_t uuid_image;
164 uuid_t uuid_last_snap;
165 uuid_t uuid_link;
166 uuid_t uuid_parent;
167 uint64_t unused2[7];
168 } QEMU_PACKED VdiHeader;
170 typedef struct {
171 /* The block map entries are little endian (even in memory). */
172 uint32_t *bmap;
173 /* Size of block (bytes). */
174 uint32_t block_size;
175 /* Size of block (sectors). */
176 uint32_t block_sectors;
177 /* First sector of block map. */
178 uint32_t bmap_sector;
179 /* VDI header (converted to host endianness). */
180 VdiHeader header;
182 Error *migration_blocker;
183 } BDRVVdiState;
185 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
186 * format (network byte order, standard, see RFC 4122) and vice versa.
188 static void uuid_convert(uuid_t uuid)
190 bswap32s((uint32_t *)&uuid[0]);
191 bswap16s((uint16_t *)&uuid[4]);
192 bswap16s((uint16_t *)&uuid[6]);
195 static void vdi_header_to_cpu(VdiHeader *header)
197 le32_to_cpus(&header->signature);
198 le32_to_cpus(&header->version);
199 le32_to_cpus(&header->header_size);
200 le32_to_cpus(&header->image_type);
201 le32_to_cpus(&header->image_flags);
202 le32_to_cpus(&header->offset_bmap);
203 le32_to_cpus(&header->offset_data);
204 le32_to_cpus(&header->cylinders);
205 le32_to_cpus(&header->heads);
206 le32_to_cpus(&header->sectors);
207 le32_to_cpus(&header->sector_size);
208 le64_to_cpus(&header->disk_size);
209 le32_to_cpus(&header->block_size);
210 le32_to_cpus(&header->block_extra);
211 le32_to_cpus(&header->blocks_in_image);
212 le32_to_cpus(&header->blocks_allocated);
213 uuid_convert(header->uuid_image);
214 uuid_convert(header->uuid_last_snap);
215 uuid_convert(header->uuid_link);
216 uuid_convert(header->uuid_parent);
219 static void vdi_header_to_le(VdiHeader *header)
221 cpu_to_le32s(&header->signature);
222 cpu_to_le32s(&header->version);
223 cpu_to_le32s(&header->header_size);
224 cpu_to_le32s(&header->image_type);
225 cpu_to_le32s(&header->image_flags);
226 cpu_to_le32s(&header->offset_bmap);
227 cpu_to_le32s(&header->offset_data);
228 cpu_to_le32s(&header->cylinders);
229 cpu_to_le32s(&header->heads);
230 cpu_to_le32s(&header->sectors);
231 cpu_to_le32s(&header->sector_size);
232 cpu_to_le64s(&header->disk_size);
233 cpu_to_le32s(&header->block_size);
234 cpu_to_le32s(&header->block_extra);
235 cpu_to_le32s(&header->blocks_in_image);
236 cpu_to_le32s(&header->blocks_allocated);
237 cpu_to_le32s(&header->blocks_allocated);
238 uuid_convert(header->uuid_image);
239 uuid_convert(header->uuid_last_snap);
240 uuid_convert(header->uuid_link);
241 uuid_convert(header->uuid_parent);
244 #if defined(CONFIG_VDI_DEBUG)
245 static void vdi_header_print(VdiHeader *header)
247 char uuid[37];
248 logout("text %s", header->text);
249 logout("signature 0x%08x\n", header->signature);
250 logout("header size 0x%04x\n", header->header_size);
251 logout("image type 0x%04x\n", header->image_type);
252 logout("image flags 0x%04x\n", header->image_flags);
253 logout("description %s\n", header->description);
254 logout("offset bmap 0x%04x\n", header->offset_bmap);
255 logout("offset data 0x%04x\n", header->offset_data);
256 logout("cylinders 0x%04x\n", header->cylinders);
257 logout("heads 0x%04x\n", header->heads);
258 logout("sectors 0x%04x\n", header->sectors);
259 logout("sector size 0x%04x\n", header->sector_size);
260 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
261 header->disk_size, header->disk_size / MiB);
262 logout("block size 0x%04x\n", header->block_size);
263 logout("block extra 0x%04x\n", header->block_extra);
264 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
265 logout("blocks all. 0x%04x\n", header->blocks_allocated);
266 uuid_unparse(header->uuid_image, uuid);
267 logout("uuid image %s\n", uuid);
268 uuid_unparse(header->uuid_last_snap, uuid);
269 logout("uuid snap %s\n", uuid);
270 uuid_unparse(header->uuid_link, uuid);
271 logout("uuid link %s\n", uuid);
272 uuid_unparse(header->uuid_parent, uuid);
273 logout("uuid parent %s\n", uuid);
275 #endif
277 static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res,
278 BdrvCheckMode fix)
280 /* TODO: additional checks possible. */
281 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
282 uint32_t blocks_allocated = 0;
283 uint32_t block;
284 uint32_t *bmap;
285 logout("\n");
287 if (fix) {
288 return -ENOTSUP;
291 bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
292 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
294 /* Check block map and value of blocks_allocated. */
295 for (block = 0; block < s->header.blocks_in_image; block++) {
296 uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
297 if (VDI_IS_ALLOCATED(bmap_entry)) {
298 if (bmap_entry < s->header.blocks_in_image) {
299 blocks_allocated++;
300 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
301 bmap[bmap_entry] = bmap_entry;
302 } else {
303 fprintf(stderr, "ERROR: block index %" PRIu32
304 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
305 res->corruptions++;
307 } else {
308 fprintf(stderr, "ERROR: block index %" PRIu32
309 " too large, is %" PRIu32 "\n", block, bmap_entry);
310 res->corruptions++;
314 if (blocks_allocated != s->header.blocks_allocated) {
315 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
316 ", should be %" PRIu32 "\n",
317 blocks_allocated, s->header.blocks_allocated);
318 res->corruptions++;
321 g_free(bmap);
323 return 0;
326 static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
328 /* TODO: vdi_get_info would be needed for machine snapshots.
329 vm_state_offset is still missing. */
330 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
331 logout("\n");
332 bdi->cluster_size = s->block_size;
333 bdi->vm_state_offset = 0;
334 bdi->unallocated_blocks_are_zero = true;
335 return 0;
338 static int vdi_make_empty(BlockDriverState *bs)
340 /* TODO: missing code. */
341 logout("\n");
342 /* The return value for missing code must be 0, see block.c. */
343 return 0;
346 static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
348 const VdiHeader *header = (const VdiHeader *)buf;
349 int result = 0;
351 logout("\n");
353 if (buf_size < sizeof(*header)) {
354 /* Header too small, no VDI. */
355 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
356 result = 100;
359 if (result == 0) {
360 logout("no vdi image\n");
361 } else {
362 logout("%s", header->text);
365 return result;
368 static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
369 Error **errp)
371 BDRVVdiState *s = bs->opaque;
372 VdiHeader header;
373 size_t bmap_size;
374 int ret;
376 logout("\n");
378 ret = bdrv_read(bs->file, 0, (uint8_t *)&header, 1);
379 if (ret < 0) {
380 goto fail;
383 vdi_header_to_cpu(&header);
384 #if defined(CONFIG_VDI_DEBUG)
385 vdi_header_print(&header);
386 #endif
388 if (header.disk_size % SECTOR_SIZE != 0) {
389 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
390 We accept them but round the disk size to the next multiple of
391 SECTOR_SIZE. */
392 logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
393 header.disk_size += SECTOR_SIZE - 1;
394 header.disk_size &= ~(SECTOR_SIZE - 1);
397 if (header.signature != VDI_SIGNATURE) {
398 logout("bad vdi signature %08x\n", header.signature);
399 ret = -EMEDIUMTYPE;
400 goto fail;
401 } else if (header.version != VDI_VERSION_1_1) {
402 logout("unsupported version %u.%u\n",
403 header.version >> 16, header.version & 0xffff);
404 ret = -ENOTSUP;
405 goto fail;
406 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
407 /* We only support block maps which start on a sector boundary. */
408 logout("unsupported block map offset 0x%x B\n", header.offset_bmap);
409 ret = -ENOTSUP;
410 goto fail;
411 } else if (header.offset_data % SECTOR_SIZE != 0) {
412 /* We only support data blocks which start on a sector boundary. */
413 logout("unsupported data offset 0x%x B\n", header.offset_data);
414 ret = -ENOTSUP;
415 goto fail;
416 } else if (header.sector_size != SECTOR_SIZE) {
417 logout("unsupported sector size %u B\n", header.sector_size);
418 ret = -ENOTSUP;
419 goto fail;
420 } else if (header.block_size != 1 * MiB) {
421 logout("unsupported block size %u B\n", header.block_size);
422 ret = -ENOTSUP;
423 goto fail;
424 } else if (header.disk_size >
425 (uint64_t)header.blocks_in_image * header.block_size) {
426 logout("unsupported disk size %" PRIu64 " B\n", header.disk_size);
427 ret = -ENOTSUP;
428 goto fail;
429 } else if (!uuid_is_null(header.uuid_link)) {
430 logout("link uuid != 0, unsupported\n");
431 ret = -ENOTSUP;
432 goto fail;
433 } else if (!uuid_is_null(header.uuid_parent)) {
434 logout("parent uuid != 0, unsupported\n");
435 ret = -ENOTSUP;
436 goto fail;
439 bs->total_sectors = header.disk_size / SECTOR_SIZE;
441 s->block_size = header.block_size;
442 s->block_sectors = header.block_size / SECTOR_SIZE;
443 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
444 s->header = header;
446 bmap_size = header.blocks_in_image * sizeof(uint32_t);
447 bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
448 s->bmap = g_malloc(bmap_size * SECTOR_SIZE);
449 ret = bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size);
450 if (ret < 0) {
451 goto fail_free_bmap;
454 /* Disable migration when vdi images are used */
455 error_set(&s->migration_blocker,
456 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
457 "vdi", bs->device_name, "live migration");
458 migrate_add_blocker(s->migration_blocker);
460 return 0;
462 fail_free_bmap:
463 g_free(s->bmap);
465 fail:
466 return ret;
469 static int vdi_reopen_prepare(BDRVReopenState *state,
470 BlockReopenQueue *queue, Error **errp)
472 return 0;
475 static int64_t coroutine_fn vdi_co_get_block_status(BlockDriverState *bs,
476 int64_t sector_num, int nb_sectors, int *pnum)
478 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
479 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
480 size_t bmap_index = sector_num / s->block_sectors;
481 size_t sector_in_block = sector_num % s->block_sectors;
482 int n_sectors = s->block_sectors - sector_in_block;
483 uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
484 uint64_t offset;
485 int result;
487 logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
488 if (n_sectors > nb_sectors) {
489 n_sectors = nb_sectors;
491 *pnum = n_sectors;
492 result = VDI_IS_ALLOCATED(bmap_entry);
493 if (!result) {
494 return 0;
497 offset = s->header.offset_data +
498 (uint64_t)bmap_entry * s->block_size +
499 sector_in_block * SECTOR_SIZE;
500 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
503 static int vdi_co_read(BlockDriverState *bs,
504 int64_t sector_num, uint8_t *buf, int nb_sectors)
506 BDRVVdiState *s = bs->opaque;
507 uint32_t bmap_entry;
508 uint32_t block_index;
509 uint32_t sector_in_block;
510 uint32_t n_sectors;
511 int ret = 0;
513 logout("\n");
515 while (ret >= 0 && nb_sectors > 0) {
516 block_index = sector_num / s->block_sectors;
517 sector_in_block = sector_num % s->block_sectors;
518 n_sectors = s->block_sectors - sector_in_block;
519 if (n_sectors > nb_sectors) {
520 n_sectors = nb_sectors;
523 logout("will read %u sectors starting at sector %" PRIu64 "\n",
524 n_sectors, sector_num);
526 /* prepare next AIO request */
527 bmap_entry = le32_to_cpu(s->bmap[block_index]);
528 if (!VDI_IS_ALLOCATED(bmap_entry)) {
529 /* Block not allocated, return zeros, no need to wait. */
530 memset(buf, 0, n_sectors * SECTOR_SIZE);
531 ret = 0;
532 } else {
533 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
534 (uint64_t)bmap_entry * s->block_sectors +
535 sector_in_block;
536 ret = bdrv_read(bs->file, offset, buf, n_sectors);
538 logout("%u sectors read\n", n_sectors);
540 nb_sectors -= n_sectors;
541 sector_num += n_sectors;
542 buf += n_sectors * SECTOR_SIZE;
545 return ret;
548 static int vdi_co_write(BlockDriverState *bs,
549 int64_t sector_num, const uint8_t *buf, int nb_sectors)
551 BDRVVdiState *s = bs->opaque;
552 uint32_t bmap_entry;
553 uint32_t block_index;
554 uint32_t sector_in_block;
555 uint32_t n_sectors;
556 uint32_t bmap_first = VDI_UNALLOCATED;
557 uint32_t bmap_last = VDI_UNALLOCATED;
558 uint8_t *block = NULL;
559 int ret = 0;
561 logout("\n");
563 while (ret >= 0 && nb_sectors > 0) {
564 block_index = sector_num / s->block_sectors;
565 sector_in_block = sector_num % s->block_sectors;
566 n_sectors = s->block_sectors - sector_in_block;
567 if (n_sectors > nb_sectors) {
568 n_sectors = nb_sectors;
571 logout("will write %u sectors starting at sector %" PRIu64 "\n",
572 n_sectors, sector_num);
574 /* prepare next AIO request */
575 bmap_entry = le32_to_cpu(s->bmap[block_index]);
576 if (!VDI_IS_ALLOCATED(bmap_entry)) {
577 /* Allocate new block and write to it. */
578 uint64_t offset;
579 bmap_entry = s->header.blocks_allocated;
580 s->bmap[block_index] = cpu_to_le32(bmap_entry);
581 s->header.blocks_allocated++;
582 offset = s->header.offset_data / SECTOR_SIZE +
583 (uint64_t)bmap_entry * s->block_sectors;
584 if (block == NULL) {
585 block = g_malloc(s->block_size);
586 bmap_first = block_index;
588 bmap_last = block_index;
589 /* Copy data to be written to new block and zero unused parts. */
590 memset(block, 0, sector_in_block * SECTOR_SIZE);
591 memcpy(block + sector_in_block * SECTOR_SIZE,
592 buf, n_sectors * SECTOR_SIZE);
593 memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0,
594 (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
595 ret = bdrv_write(bs->file, offset, block, s->block_sectors);
596 } else {
597 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
598 (uint64_t)bmap_entry * s->block_sectors +
599 sector_in_block;
600 ret = bdrv_write(bs->file, offset, buf, n_sectors);
603 nb_sectors -= n_sectors;
604 sector_num += n_sectors;
605 buf += n_sectors * SECTOR_SIZE;
607 logout("%u sectors written\n", n_sectors);
610 logout("finished data write\n");
611 if (ret < 0) {
612 return ret;
615 if (block) {
616 /* One or more new blocks were allocated. */
617 VdiHeader *header = (VdiHeader *) block;
618 uint8_t *base;
619 uint64_t offset;
621 logout("now writing modified header\n");
622 assert(VDI_IS_ALLOCATED(bmap_first));
623 *header = s->header;
624 vdi_header_to_le(header);
625 ret = bdrv_write(bs->file, 0, block, 1);
626 g_free(block);
627 block = NULL;
629 if (ret < 0) {
630 return ret;
633 logout("now writing modified block map entry %u...%u\n",
634 bmap_first, bmap_last);
635 /* Write modified sectors from block map. */
636 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
637 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
638 n_sectors = bmap_last - bmap_first + 1;
639 offset = s->bmap_sector + bmap_first;
640 base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE;
641 logout("will write %u block map sectors starting from entry %u\n",
642 n_sectors, bmap_first);
643 ret = bdrv_write(bs->file, offset, base, n_sectors);
646 return ret;
649 static int vdi_create(const char *filename, QEMUOptionParameter *options,
650 Error **errp)
652 int fd;
653 int result = 0;
654 uint64_t bytes = 0;
655 uint32_t blocks;
656 size_t block_size = DEFAULT_CLUSTER_SIZE;
657 uint32_t image_type = VDI_TYPE_DYNAMIC;
658 VdiHeader header;
659 size_t i;
660 size_t bmap_size;
662 logout("\n");
664 /* Read out options. */
665 while (options && options->name) {
666 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
667 bytes = options->value.n;
668 #if defined(CONFIG_VDI_BLOCK_SIZE)
669 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
670 if (options->value.n) {
671 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
672 block_size = options->value.n;
674 #endif
675 #if defined(CONFIG_VDI_STATIC_IMAGE)
676 } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
677 if (options->value.n) {
678 image_type = VDI_TYPE_STATIC;
680 #endif
682 options++;
685 fd = qemu_open(filename,
686 O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
687 0644);
688 if (fd < 0) {
689 return -errno;
692 /* We need enough blocks to store the given disk size,
693 so always round up. */
694 blocks = (bytes + block_size - 1) / block_size;
696 bmap_size = blocks * sizeof(uint32_t);
697 bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
699 memset(&header, 0, sizeof(header));
700 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
701 header.signature = VDI_SIGNATURE;
702 header.version = VDI_VERSION_1_1;
703 header.header_size = 0x180;
704 header.image_type = image_type;
705 header.offset_bmap = 0x200;
706 header.offset_data = 0x200 + bmap_size;
707 header.sector_size = SECTOR_SIZE;
708 header.disk_size = bytes;
709 header.block_size = block_size;
710 header.blocks_in_image = blocks;
711 if (image_type == VDI_TYPE_STATIC) {
712 header.blocks_allocated = blocks;
714 uuid_generate(header.uuid_image);
715 uuid_generate(header.uuid_last_snap);
716 /* There is no need to set header.uuid_link or header.uuid_parent here. */
717 #if defined(CONFIG_VDI_DEBUG)
718 vdi_header_print(&header);
719 #endif
720 vdi_header_to_le(&header);
721 if (write(fd, &header, sizeof(header)) < 0) {
722 result = -errno;
725 if (bmap_size > 0) {
726 uint32_t *bmap = g_malloc0(bmap_size);
727 for (i = 0; i < blocks; i++) {
728 if (image_type == VDI_TYPE_STATIC) {
729 bmap[i] = i;
730 } else {
731 bmap[i] = VDI_UNALLOCATED;
734 if (write(fd, bmap, bmap_size) < 0) {
735 result = -errno;
737 g_free(bmap);
740 if (image_type == VDI_TYPE_STATIC) {
741 if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
742 result = -errno;
746 if (close(fd) < 0) {
747 result = -errno;
750 return result;
753 static void vdi_close(BlockDriverState *bs)
755 BDRVVdiState *s = bs->opaque;
757 g_free(s->bmap);
759 migrate_del_blocker(s->migration_blocker);
760 error_free(s->migration_blocker);
763 static QEMUOptionParameter vdi_create_options[] = {
765 .name = BLOCK_OPT_SIZE,
766 .type = OPT_SIZE,
767 .help = "Virtual disk size"
769 #if defined(CONFIG_VDI_BLOCK_SIZE)
771 .name = BLOCK_OPT_CLUSTER_SIZE,
772 .type = OPT_SIZE,
773 .help = "VDI cluster (block) size",
774 .value = { .n = DEFAULT_CLUSTER_SIZE },
776 #endif
777 #if defined(CONFIG_VDI_STATIC_IMAGE)
779 .name = BLOCK_OPT_STATIC,
780 .type = OPT_FLAG,
781 .help = "VDI static (pre-allocated) image"
783 #endif
784 /* TODO: An additional option to set UUID values might be useful. */
785 { NULL }
788 static BlockDriver bdrv_vdi = {
789 .format_name = "vdi",
790 .instance_size = sizeof(BDRVVdiState),
791 .bdrv_probe = vdi_probe,
792 .bdrv_open = vdi_open,
793 .bdrv_close = vdi_close,
794 .bdrv_reopen_prepare = vdi_reopen_prepare,
795 .bdrv_create = vdi_create,
796 .bdrv_has_zero_init = bdrv_has_zero_init_1,
797 .bdrv_co_get_block_status = vdi_co_get_block_status,
798 .bdrv_make_empty = vdi_make_empty,
800 .bdrv_read = vdi_co_read,
801 #if defined(CONFIG_VDI_WRITE)
802 .bdrv_write = vdi_co_write,
803 #endif
805 .bdrv_get_info = vdi_get_info,
807 .create_options = vdi_create_options,
808 .bdrv_check = vdi_check,
811 static void bdrv_vdi_init(void)
813 logout("\n");
814 bdrv_register(&bdrv_vdi);
817 block_init(bdrv_vdi_init);