slirp: Enforce host-side user of smb share
[qemu/ar7.git] / block / vdi.c
blob57325d65c4a87e8331f2cfac1d4c776c415e7790
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_int.h"
54 #include "module.h"
55 #include "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.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
68 /* Code configuration options. */
70 /* Enable debug messages. */
71 //~ #define CONFIG_VDI_DEBUG
73 /* Support write operations on VDI images. */
74 #define CONFIG_VDI_WRITE
76 /* Support non-standard block (cluster) size. This is untested.
77 * Maybe it will be needed for very large images.
79 //~ #define CONFIG_VDI_BLOCK_SIZE
81 /* Support static (fixed, pre-allocated) images. */
82 #define CONFIG_VDI_STATIC_IMAGE
84 /* Command line option for static images. */
85 #define BLOCK_OPT_STATIC "static"
87 #define KiB 1024
88 #define MiB (KiB * KiB)
90 #define SECTOR_SIZE 512
91 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
93 #if defined(CONFIG_VDI_DEBUG)
94 #define logout(fmt, ...) \
95 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
96 #else
97 #define logout(fmt, ...) ((void)0)
98 #endif
100 /* Image signature. */
101 #define VDI_SIGNATURE 0xbeda107f
103 /* Image version. */
104 #define VDI_VERSION_1_1 0x00010001
106 /* Image type. */
107 #define VDI_TYPE_DYNAMIC 1
108 #define VDI_TYPE_STATIC 2
110 /* Innotek / SUN images use these strings in header.text:
111 * "<<< innotek VirtualBox Disk Image >>>\n"
112 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
113 * "<<< Sun VirtualBox Disk Image >>>\n"
114 * The value does not matter, so QEMU created images use a different text.
116 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
118 /* A never-allocated block; semantically arbitrary content. */
119 #define VDI_UNALLOCATED 0xffffffffU
121 /* A discarded (no longer allocated) block; semantically zero-filled. */
122 #define VDI_DISCARDED 0xfffffffeU
124 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
126 #if !defined(CONFIG_UUID)
127 void uuid_generate(uuid_t out)
129 memset(out, 0, sizeof(uuid_t));
132 int uuid_is_null(const uuid_t uu)
134 uuid_t null_uuid = { 0 };
135 return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0;
138 void uuid_unparse(const uuid_t uu, char *out)
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]);
144 #endif
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; /* disk geometry, unused here */
157 uint32_t heads; /* disk geometry, unused here */
158 uint32_t sectors; /* disk geometry, unused here */
159 uint32_t sector_size;
160 uint32_t unused1;
161 uint64_t disk_size;
162 uint32_t block_size;
163 uint32_t block_extra; /* unused here */
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;
173 typedef struct {
174 /* The block map entries are little endian (even in memory). */
175 uint32_t *bmap;
176 /* Size of block (bytes). */
177 uint32_t block_size;
178 /* Size of block (sectors). */
179 uint32_t block_sectors;
180 /* First sector of block map. */
181 uint32_t bmap_sector;
182 /* VDI header (converted to host endianness). */
183 VdiHeader header;
185 Error *migration_blocker;
186 } BDRVVdiState;
188 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
189 * format (network byte order, standard, see RFC 4122) and vice versa.
191 static void uuid_convert(uuid_t uuid)
193 bswap32s((uint32_t *)&uuid[0]);
194 bswap16s((uint16_t *)&uuid[4]);
195 bswap16s((uint16_t *)&uuid[6]);
198 static void vdi_header_to_cpu(VdiHeader *header)
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);
222 static void vdi_header_to_le(VdiHeader *header)
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);
247 #if defined(CONFIG_VDI_DEBUG)
248 static void vdi_header_print(VdiHeader *header)
250 char uuid[37];
251 logout("text %s", header->text);
252 logout("signature 0x%04x\n", header->signature);
253 logout("header size 0x%04x\n", header->header_size);
254 logout("image type 0x%04x\n", header->image_type);
255 logout("image flags 0x%04x\n", header->image_flags);
256 logout("description %s\n", header->description);
257 logout("offset bmap 0x%04x\n", header->offset_bmap);
258 logout("offset data 0x%04x\n", header->offset_data);
259 logout("cylinders 0x%04x\n", header->cylinders);
260 logout("heads 0x%04x\n", header->heads);
261 logout("sectors 0x%04x\n", header->sectors);
262 logout("sector size 0x%04x\n", header->sector_size);
263 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
264 header->disk_size, header->disk_size / MiB);
265 logout("block size 0x%04x\n", header->block_size);
266 logout("block extra 0x%04x\n", header->block_extra);
267 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
268 logout("blocks all. 0x%04x\n", header->blocks_allocated);
269 uuid_unparse(header->uuid_image, uuid);
270 logout("uuid image %s\n", uuid);
271 uuid_unparse(header->uuid_last_snap, uuid);
272 logout("uuid snap %s\n", uuid);
273 uuid_unparse(header->uuid_link, uuid);
274 logout("uuid link %s\n", uuid);
275 uuid_unparse(header->uuid_parent, uuid);
276 logout("uuid parent %s\n", uuid);
278 #endif
280 static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res,
281 BdrvCheckMode fix)
283 /* TODO: additional checks possible. */
284 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
285 uint32_t blocks_allocated = 0;
286 uint32_t block;
287 uint32_t *bmap;
288 logout("\n");
290 if (fix) {
291 return -ENOTSUP;
294 bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
295 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
297 /* Check block map and value of blocks_allocated. */
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)) {
301 if (bmap_entry < s->header.blocks_in_image) {
302 blocks_allocated++;
303 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
304 bmap[bmap_entry] = bmap_entry;
305 } else {
306 fprintf(stderr, "ERROR: block index %" PRIu32
307 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
308 res->corruptions++;
310 } else {
311 fprintf(stderr, "ERROR: block index %" PRIu32
312 " too large, is %" PRIu32 "\n", block, bmap_entry);
313 res->corruptions++;
317 if (blocks_allocated != s->header.blocks_allocated) {
318 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
319 ", should be %" PRIu32 "\n",
320 blocks_allocated, s->header.blocks_allocated);
321 res->corruptions++;
324 g_free(bmap);
326 return 0;
329 static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
331 /* TODO: vdi_get_info would be needed for machine snapshots.
332 vm_state_offset is still missing. */
333 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
334 logout("\n");
335 bdi->cluster_size = s->block_size;
336 bdi->vm_state_offset = 0;
337 return 0;
340 static int vdi_make_empty(BlockDriverState *bs)
342 /* TODO: missing code. */
343 logout("\n");
344 /* The return value for missing code must be 0, see block.c. */
345 return 0;
348 static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
350 const VdiHeader *header = (const VdiHeader *)buf;
351 int result = 0;
353 logout("\n");
355 if (buf_size < sizeof(*header)) {
356 /* Header too small, no VDI. */
357 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
358 result = 100;
361 if (result == 0) {
362 logout("no vdi image\n");
363 } else {
364 logout("%s", header->text);
367 return result;
370 static int vdi_open(BlockDriverState *bs, int flags)
372 BDRVVdiState *s = bs->opaque;
373 VdiHeader header;
374 size_t bmap_size;
376 logout("\n");
378 if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) {
379 goto fail;
382 vdi_header_to_cpu(&header);
383 #if defined(CONFIG_VDI_DEBUG)
384 vdi_header_print(&header);
385 #endif
387 if (header.disk_size % SECTOR_SIZE != 0) {
388 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
389 We accept them but round the disk size to the next multiple of
390 SECTOR_SIZE. */
391 logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
392 header.disk_size += SECTOR_SIZE - 1;
393 header.disk_size &= ~(SECTOR_SIZE - 1);
396 if (header.version != VDI_VERSION_1_1) {
397 logout("unsupported version %u.%u\n",
398 header.version >> 16, header.version & 0xffff);
399 goto fail;
400 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
401 /* We only support block maps which start on a sector boundary. */
402 logout("unsupported block map offset 0x%x B\n", header.offset_bmap);
403 goto fail;
404 } else if (header.offset_data % SECTOR_SIZE != 0) {
405 /* We only support data blocks which start on a sector boundary. */
406 logout("unsupported data offset 0x%x B\n", header.offset_data);
407 goto fail;
408 } else if (header.sector_size != SECTOR_SIZE) {
409 logout("unsupported sector size %u B\n", header.sector_size);
410 goto fail;
411 } else if (header.block_size != 1 * MiB) {
412 logout("unsupported block size %u B\n", header.block_size);
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);
417 goto fail;
418 } else if (!uuid_is_null(header.uuid_link)) {
419 logout("link uuid != 0, unsupported\n");
420 goto fail;
421 } else if (!uuid_is_null(header.uuid_parent)) {
422 logout("parent uuid != 0, unsupported\n");
423 goto fail;
426 bs->total_sectors = header.disk_size / SECTOR_SIZE;
428 s->block_size = header.block_size;
429 s->block_sectors = header.block_size / SECTOR_SIZE;
430 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
431 s->header = header;
433 bmap_size = header.blocks_in_image * sizeof(uint32_t);
434 bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
435 if (bmap_size > 0) {
436 s->bmap = g_malloc(bmap_size * SECTOR_SIZE);
438 if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
439 goto fail_free_bmap;
442 /* Disable migration when vdi images are used */
443 error_set(&s->migration_blocker,
444 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
445 "vdi", bs->device_name, "live migration");
446 migrate_add_blocker(s->migration_blocker);
448 return 0;
450 fail_free_bmap:
451 g_free(s->bmap);
453 fail:
454 return -1;
457 static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs,
458 int64_t sector_num, int nb_sectors, int *pnum)
460 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
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);
467 if (n_sectors > nb_sectors) {
468 n_sectors = nb_sectors;
470 *pnum = n_sectors;
471 return VDI_IS_ALLOCATED(bmap_entry);
474 static int vdi_co_read(BlockDriverState *bs,
475 int64_t sector_num, uint8_t *buf, int nb_sectors)
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;
484 logout("\n");
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;
494 logout("will read %u sectors starting at sector %" PRIu64 "\n",
495 n_sectors, sector_num);
497 /* prepare next AIO request */
498 bmap_entry = le32_to_cpu(s->bmap[block_index]);
499 if (!VDI_IS_ALLOCATED(bmap_entry)) {
500 /* Block not allocated, return zeros, no need to wait. */
501 memset(buf, 0, n_sectors * SECTOR_SIZE);
502 ret = 0;
503 } else {
504 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
505 (uint64_t)bmap_entry * s->block_sectors +
506 sector_in_block;
507 ret = bdrv_read(bs->file, offset, buf, n_sectors);
509 logout("%u sectors read\n", n_sectors);
511 nb_sectors -= n_sectors;
512 sector_num += n_sectors;
513 buf += n_sectors * SECTOR_SIZE;
516 return ret;
519 static int vdi_co_write(BlockDriverState *bs,
520 int64_t sector_num, const uint8_t *buf, int nb_sectors)
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_UNALLOCATED;
528 uint32_t bmap_last = VDI_UNALLOCATED;
529 uint8_t *block = NULL;
530 int ret = 0;
532 logout("\n");
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;
542 logout("will write %u sectors starting at sector %" PRIu64 "\n",
543 n_sectors, sector_num);
545 /* prepare next AIO request */
546 bmap_entry = le32_to_cpu(s->bmap[block_index]);
547 if (!VDI_IS_ALLOCATED(bmap_entry)) {
548 /* Allocate new block and write to it. */
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_SIZE +
554 (uint64_t)bmap_entry * s->block_sectors;
555 if (block == NULL) {
556 block = g_malloc(s->block_size);
557 bmap_first = block_index;
559 bmap_last = block_index;
560 /* Copy data to be written to new block and zero unused parts. */
561 memset(block, 0, sector_in_block * SECTOR_SIZE);
562 memcpy(block + sector_in_block * SECTOR_SIZE,
563 buf, n_sectors * SECTOR_SIZE);
564 memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0,
565 (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
566 ret = bdrv_write(bs->file, offset, block, s->block_sectors);
567 } else {
568 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
569 (uint64_t)bmap_entry * s->block_sectors +
570 sector_in_block;
571 ret = bdrv_write(bs->file, offset, buf, n_sectors);
574 nb_sectors -= n_sectors;
575 sector_num += n_sectors;
576 buf += n_sectors * SECTOR_SIZE;
578 logout("%u sectors written\n", n_sectors);
581 logout("finished data write\n");
582 if (ret < 0) {
583 return ret;
586 if (block) {
587 /* One or more new blocks were allocated. */
588 VdiHeader *header = (VdiHeader *) block;
589 uint8_t *base;
590 uint64_t offset;
592 logout("now writing modified header\n");
593 assert(VDI_IS_ALLOCATED(bmap_first));
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;
600 if (ret < 0) {
601 return ret;
604 logout("now writing modified block map entry %u...%u\n",
605 bmap_first, bmap_last);
606 /* Write modified sectors from block map. */
607 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
608 bmap_last /= (SECTOR_SIZE / 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_SIZE;
612 logout("will write %u block map sectors starting from entry %u\n",
613 n_sectors, bmap_first);
614 ret = bdrv_write(bs->file, offset, base, n_sectors);
617 return ret;
620 static int vdi_create(const char *filename, QEMUOptionParameter *options)
622 int fd;
623 int result = 0;
624 uint64_t bytes = 0;
625 uint32_t blocks;
626 size_t block_size = DEFAULT_CLUSTER_SIZE;
627 uint32_t image_type = VDI_TYPE_DYNAMIC;
628 VdiHeader header;
629 size_t i;
630 size_t bmap_size;
631 uint32_t *bmap;
633 logout("\n");
635 /* Read out options. */
636 while (options && options->name) {
637 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
638 bytes = options->value.n;
639 #if defined(CONFIG_VDI_BLOCK_SIZE)
640 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
641 if (options->value.n) {
642 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
643 block_size = options->value.n;
645 #endif
646 #if defined(CONFIG_VDI_STATIC_IMAGE)
647 } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
648 if (options->value.n) {
649 image_type = VDI_TYPE_STATIC;
651 #endif
653 options++;
656 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
657 0644);
658 if (fd < 0) {
659 return -errno;
662 /* We need enough blocks to store the given disk size,
663 so always round up. */
664 blocks = (bytes + block_size - 1) / block_size;
666 bmap_size = blocks * sizeof(uint32_t);
667 bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
669 memset(&header, 0, sizeof(header));
670 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
671 header.signature = VDI_SIGNATURE;
672 header.version = VDI_VERSION_1_1;
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_SIZE;
678 header.disk_size = bytes;
679 header.block_size = block_size;
680 header.blocks_in_image = blocks;
681 if (image_type == VDI_TYPE_STATIC) {
682 header.blocks_allocated = blocks;
684 uuid_generate(header.uuid_image);
685 uuid_generate(header.uuid_last_snap);
686 /* There is no need to set header.uuid_link or header.uuid_parent here. */
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;
695 bmap = NULL;
696 if (bmap_size > 0) {
697 bmap = (uint32_t *)g_malloc0(bmap_size);
699 for (i = 0; i < blocks; i++) {
700 if (image_type == VDI_TYPE_STATIC) {
701 bmap[i] = i;
702 } else {
703 bmap[i] = VDI_UNALLOCATED;
706 if (write(fd, bmap, bmap_size) < 0) {
707 result = -errno;
709 g_free(bmap);
710 if (image_type == VDI_TYPE_STATIC) {
711 if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
712 result = -errno;
716 if (close(fd) < 0) {
717 result = -errno;
720 return result;
723 static void vdi_close(BlockDriverState *bs)
725 BDRVVdiState *s = bs->opaque;
727 g_free(s->bmap);
729 migrate_del_blocker(s->migration_blocker);
730 error_free(s->migration_blocker);
733 static QEMUOptionParameter vdi_create_options[] = {
735 .name = BLOCK_OPT_SIZE,
736 .type = OPT_SIZE,
737 .help = "Virtual disk size"
739 #if defined(CONFIG_VDI_BLOCK_SIZE)
741 .name = BLOCK_OPT_CLUSTER_SIZE,
742 .type = OPT_SIZE,
743 .help = "VDI cluster (block) size",
744 .value = { .n = DEFAULT_CLUSTER_SIZE },
746 #endif
747 #if defined(CONFIG_VDI_STATIC_IMAGE)
749 .name = BLOCK_OPT_STATIC,
750 .type = OPT_FLAG,
751 .help = "VDI static (pre-allocated) image"
753 #endif
754 /* TODO: An additional option to set UUID values might be useful. */
755 { NULL }
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,
768 .bdrv_read = vdi_co_read,
769 #if defined(CONFIG_VDI_WRITE)
770 .bdrv_write = vdi_co_write,
771 #endif
773 .bdrv_get_info = vdi_get_info,
775 .create_options = vdi_create_options,
776 .bdrv_check = vdi_check,
779 static void bdrv_vdi_init(void)
781 logout("\n");
782 bdrv_register(&bdrv_vdi);
785 block_init(bdrv_vdi_init);