vnc: return directly if no vnc client connected
[qemu.git] / block / vdi.c
blob19701ee00d223669022cc8dc3e03c35dc4289016
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 adjacent 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 /* max blocks in image is (0xffffffff / 4) */
124 #define VDI_BLOCKS_IN_IMAGE_MAX 0x3fffffff
125 #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
126 (uint64_t)DEFAULT_CLUSTER_SIZE)
128 #if !defined(CONFIG_UUID)
129 static inline void uuid_generate(uuid_t out)
131 memset(out, 0, sizeof(uuid_t));
134 static inline int uuid_is_null(const uuid_t uu)
136 uuid_t null_uuid = { 0 };
137 return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0;
140 static inline void uuid_unparse(const uuid_t uu, char *out)
142 snprintf(out, 37, UUID_FMT,
143 uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7],
144 uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
146 #endif
148 typedef struct {
149 char text[0x40];
150 uint32_t signature;
151 uint32_t version;
152 uint32_t header_size;
153 uint32_t image_type;
154 uint32_t image_flags;
155 char description[256];
156 uint32_t offset_bmap;
157 uint32_t offset_data;
158 uint32_t cylinders; /* disk geometry, unused here */
159 uint32_t heads; /* disk geometry, unused here */
160 uint32_t sectors; /* disk geometry, unused here */
161 uint32_t sector_size;
162 uint32_t unused1;
163 uint64_t disk_size;
164 uint32_t block_size;
165 uint32_t block_extra; /* unused here */
166 uint32_t blocks_in_image;
167 uint32_t blocks_allocated;
168 uuid_t uuid_image;
169 uuid_t uuid_last_snap;
170 uuid_t uuid_link;
171 uuid_t uuid_parent;
172 uint64_t unused2[7];
173 } QEMU_PACKED VdiHeader;
175 typedef struct {
176 /* The block map entries are little endian (even in memory). */
177 uint32_t *bmap;
178 /* Size of block (bytes). */
179 uint32_t block_size;
180 /* Size of block (sectors). */
181 uint32_t block_sectors;
182 /* First sector of block map. */
183 uint32_t bmap_sector;
184 /* VDI header (converted to host endianness). */
185 VdiHeader header;
187 Error *migration_blocker;
188 } BDRVVdiState;
190 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
191 * format (network byte order, standard, see RFC 4122) and vice versa.
193 static void uuid_convert(uuid_t uuid)
195 bswap32s((uint32_t *)&uuid[0]);
196 bswap16s((uint16_t *)&uuid[4]);
197 bswap16s((uint16_t *)&uuid[6]);
200 static void vdi_header_to_cpu(VdiHeader *header)
202 le32_to_cpus(&header->signature);
203 le32_to_cpus(&header->version);
204 le32_to_cpus(&header->header_size);
205 le32_to_cpus(&header->image_type);
206 le32_to_cpus(&header->image_flags);
207 le32_to_cpus(&header->offset_bmap);
208 le32_to_cpus(&header->offset_data);
209 le32_to_cpus(&header->cylinders);
210 le32_to_cpus(&header->heads);
211 le32_to_cpus(&header->sectors);
212 le32_to_cpus(&header->sector_size);
213 le64_to_cpus(&header->disk_size);
214 le32_to_cpus(&header->block_size);
215 le32_to_cpus(&header->block_extra);
216 le32_to_cpus(&header->blocks_in_image);
217 le32_to_cpus(&header->blocks_allocated);
218 uuid_convert(header->uuid_image);
219 uuid_convert(header->uuid_last_snap);
220 uuid_convert(header->uuid_link);
221 uuid_convert(header->uuid_parent);
224 static void vdi_header_to_le(VdiHeader *header)
226 cpu_to_le32s(&header->signature);
227 cpu_to_le32s(&header->version);
228 cpu_to_le32s(&header->header_size);
229 cpu_to_le32s(&header->image_type);
230 cpu_to_le32s(&header->image_flags);
231 cpu_to_le32s(&header->offset_bmap);
232 cpu_to_le32s(&header->offset_data);
233 cpu_to_le32s(&header->cylinders);
234 cpu_to_le32s(&header->heads);
235 cpu_to_le32s(&header->sectors);
236 cpu_to_le32s(&header->sector_size);
237 cpu_to_le64s(&header->disk_size);
238 cpu_to_le32s(&header->block_size);
239 cpu_to_le32s(&header->block_extra);
240 cpu_to_le32s(&header->blocks_in_image);
241 cpu_to_le32s(&header->blocks_allocated);
242 uuid_convert(header->uuid_image);
243 uuid_convert(header->uuid_last_snap);
244 uuid_convert(header->uuid_link);
245 uuid_convert(header->uuid_parent);
248 #if defined(CONFIG_VDI_DEBUG)
249 static void vdi_header_print(VdiHeader *header)
251 char uuid[37];
252 logout("text %s", header->text);
253 logout("signature 0x%08x\n", header->signature);
254 logout("header size 0x%04x\n", header->header_size);
255 logout("image type 0x%04x\n", header->image_type);
256 logout("image flags 0x%04x\n", header->image_flags);
257 logout("description %s\n", header->description);
258 logout("offset bmap 0x%04x\n", header->offset_bmap);
259 logout("offset data 0x%04x\n", header->offset_data);
260 logout("cylinders 0x%04x\n", header->cylinders);
261 logout("heads 0x%04x\n", header->heads);
262 logout("sectors 0x%04x\n", header->sectors);
263 logout("sector size 0x%04x\n", header->sector_size);
264 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
265 header->disk_size, header->disk_size / MiB);
266 logout("block size 0x%04x\n", header->block_size);
267 logout("block extra 0x%04x\n", header->block_extra);
268 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
269 logout("blocks all. 0x%04x\n", header->blocks_allocated);
270 uuid_unparse(header->uuid_image, uuid);
271 logout("uuid image %s\n", uuid);
272 uuid_unparse(header->uuid_last_snap, uuid);
273 logout("uuid snap %s\n", uuid);
274 uuid_unparse(header->uuid_link, uuid);
275 logout("uuid link %s\n", uuid);
276 uuid_unparse(header->uuid_parent, uuid);
277 logout("uuid parent %s\n", uuid);
279 #endif
281 static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res,
282 BdrvCheckMode fix)
284 /* TODO: additional checks possible. */
285 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
286 uint32_t blocks_allocated = 0;
287 uint32_t block;
288 uint32_t *bmap;
289 logout("\n");
291 if (fix) {
292 return -ENOTSUP;
295 bmap = g_try_new(uint32_t, s->header.blocks_in_image);
296 if (s->header.blocks_in_image && bmap == NULL) {
297 res->check_errors++;
298 return -ENOMEM;
301 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
303 /* Check block map and value of blocks_allocated. */
304 for (block = 0; block < s->header.blocks_in_image; block++) {
305 uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
306 if (VDI_IS_ALLOCATED(bmap_entry)) {
307 if (bmap_entry < s->header.blocks_in_image) {
308 blocks_allocated++;
309 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
310 bmap[bmap_entry] = bmap_entry;
311 } else {
312 fprintf(stderr, "ERROR: block index %" PRIu32
313 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
314 res->corruptions++;
316 } else {
317 fprintf(stderr, "ERROR: block index %" PRIu32
318 " too large, is %" PRIu32 "\n", block, bmap_entry);
319 res->corruptions++;
323 if (blocks_allocated != s->header.blocks_allocated) {
324 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
325 ", should be %" PRIu32 "\n",
326 blocks_allocated, s->header.blocks_allocated);
327 res->corruptions++;
330 g_free(bmap);
332 return 0;
335 static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
337 /* TODO: vdi_get_info would be needed for machine snapshots.
338 vm_state_offset is still missing. */
339 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
340 logout("\n");
341 bdi->cluster_size = s->block_size;
342 bdi->vm_state_offset = 0;
343 bdi->unallocated_blocks_are_zero = true;
344 return 0;
347 static int vdi_make_empty(BlockDriverState *bs)
349 /* TODO: missing code. */
350 logout("\n");
351 /* The return value for missing code must be 0, see block.c. */
352 return 0;
355 static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
357 const VdiHeader *header = (const VdiHeader *)buf;
358 int ret = 0;
360 logout("\n");
362 if (buf_size < sizeof(*header)) {
363 /* Header too small, no VDI. */
364 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
365 ret = 100;
368 if (ret == 0) {
369 logout("no vdi image\n");
370 } else {
371 logout("%s", header->text);
374 return ret;
377 static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
378 Error **errp)
380 BDRVVdiState *s = bs->opaque;
381 VdiHeader header;
382 size_t bmap_size;
383 int ret;
385 logout("\n");
387 ret = bdrv_read(bs->file, 0, (uint8_t *)&header, 1);
388 if (ret < 0) {
389 goto fail;
392 vdi_header_to_cpu(&header);
393 #if defined(CONFIG_VDI_DEBUG)
394 vdi_header_print(&header);
395 #endif
397 if (header.disk_size > VDI_DISK_SIZE_MAX) {
398 error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
399 ", max supported is 0x%" PRIx64 ")",
400 header.disk_size, VDI_DISK_SIZE_MAX);
401 ret = -ENOTSUP;
402 goto fail;
405 if (header.disk_size % SECTOR_SIZE != 0) {
406 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
407 We accept them but round the disk size to the next multiple of
408 SECTOR_SIZE. */
409 logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
410 header.disk_size = ROUND_UP(header.disk_size, SECTOR_SIZE);
413 if (header.signature != VDI_SIGNATURE) {
414 error_setg(errp, "Image not in VDI format (bad signature %08" PRIx32
415 ")", header.signature);
416 ret = -EINVAL;
417 goto fail;
418 } else if (header.version != VDI_VERSION_1_1) {
419 error_setg(errp, "unsupported VDI image (version %" PRIu32 ".%" PRIu32
420 ")", header.version >> 16, header.version & 0xffff);
421 ret = -ENOTSUP;
422 goto fail;
423 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
424 /* We only support block maps which start on a sector boundary. */
425 error_setg(errp, "unsupported VDI image (unaligned block map offset "
426 "0x%" PRIx32 ")", header.offset_bmap);
427 ret = -ENOTSUP;
428 goto fail;
429 } else if (header.offset_data % SECTOR_SIZE != 0) {
430 /* We only support data blocks which start on a sector boundary. */
431 error_setg(errp, "unsupported VDI image (unaligned data offset 0x%"
432 PRIx32 ")", header.offset_data);
433 ret = -ENOTSUP;
434 goto fail;
435 } else if (header.sector_size != SECTOR_SIZE) {
436 error_setg(errp, "unsupported VDI image (sector size %" PRIu32
437 " is not %u)", header.sector_size, SECTOR_SIZE);
438 ret = -ENOTSUP;
439 goto fail;
440 } else if (header.block_size != DEFAULT_CLUSTER_SIZE) {
441 error_setg(errp, "unsupported VDI image (block size %" PRIu32
442 " is not %u)", header.block_size, DEFAULT_CLUSTER_SIZE);
443 ret = -ENOTSUP;
444 goto fail;
445 } else if (header.disk_size >
446 (uint64_t)header.blocks_in_image * header.block_size) {
447 error_setg(errp, "unsupported VDI image (disk size %" PRIu64 ", "
448 "image bitmap has room for %" PRIu64 ")",
449 header.disk_size,
450 (uint64_t)header.blocks_in_image * header.block_size);
451 ret = -ENOTSUP;
452 goto fail;
453 } else if (!uuid_is_null(header.uuid_link)) {
454 error_setg(errp, "unsupported VDI image (non-NULL link UUID)");
455 ret = -ENOTSUP;
456 goto fail;
457 } else if (!uuid_is_null(header.uuid_parent)) {
458 error_setg(errp, "unsupported VDI image (non-NULL parent UUID)");
459 ret = -ENOTSUP;
460 goto fail;
461 } else if (header.blocks_in_image > VDI_BLOCKS_IN_IMAGE_MAX) {
462 error_setg(errp, "unsupported VDI image "
463 "(too many blocks %u, max is %u)",
464 header.blocks_in_image, VDI_BLOCKS_IN_IMAGE_MAX);
465 ret = -ENOTSUP;
466 goto fail;
469 bs->total_sectors = header.disk_size / SECTOR_SIZE;
471 s->block_size = header.block_size;
472 s->block_sectors = header.block_size / SECTOR_SIZE;
473 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
474 s->header = header;
476 bmap_size = header.blocks_in_image * sizeof(uint32_t);
477 bmap_size = DIV_ROUND_UP(bmap_size, SECTOR_SIZE);
478 s->bmap = qemu_try_blockalign(bs->file, bmap_size * SECTOR_SIZE);
479 if (s->bmap == NULL) {
480 ret = -ENOMEM;
481 goto fail;
484 ret = bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size);
485 if (ret < 0) {
486 goto fail_free_bmap;
489 /* Disable migration when vdi images are used */
490 error_set(&s->migration_blocker,
491 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
492 "vdi", bdrv_get_device_name(bs), "live migration");
493 migrate_add_blocker(s->migration_blocker);
495 return 0;
497 fail_free_bmap:
498 qemu_vfree(s->bmap);
500 fail:
501 return ret;
504 static int vdi_reopen_prepare(BDRVReopenState *state,
505 BlockReopenQueue *queue, Error **errp)
507 return 0;
510 static int64_t coroutine_fn vdi_co_get_block_status(BlockDriverState *bs,
511 int64_t sector_num, int nb_sectors, int *pnum)
513 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
514 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
515 size_t bmap_index = sector_num / s->block_sectors;
516 size_t sector_in_block = sector_num % s->block_sectors;
517 int n_sectors = s->block_sectors - sector_in_block;
518 uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
519 uint64_t offset;
520 int result;
522 logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
523 if (n_sectors > nb_sectors) {
524 n_sectors = nb_sectors;
526 *pnum = n_sectors;
527 result = VDI_IS_ALLOCATED(bmap_entry);
528 if (!result) {
529 return 0;
532 offset = s->header.offset_data +
533 (uint64_t)bmap_entry * s->block_size +
534 sector_in_block * SECTOR_SIZE;
535 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
538 static int vdi_co_read(BlockDriverState *bs,
539 int64_t sector_num, uint8_t *buf, int nb_sectors)
541 BDRVVdiState *s = bs->opaque;
542 uint32_t bmap_entry;
543 uint32_t block_index;
544 uint32_t sector_in_block;
545 uint32_t n_sectors;
546 int ret = 0;
548 logout("\n");
550 while (ret >= 0 && nb_sectors > 0) {
551 block_index = sector_num / s->block_sectors;
552 sector_in_block = sector_num % s->block_sectors;
553 n_sectors = s->block_sectors - sector_in_block;
554 if (n_sectors > nb_sectors) {
555 n_sectors = nb_sectors;
558 logout("will read %u sectors starting at sector %" PRIu64 "\n",
559 n_sectors, sector_num);
561 /* prepare next AIO request */
562 bmap_entry = le32_to_cpu(s->bmap[block_index]);
563 if (!VDI_IS_ALLOCATED(bmap_entry)) {
564 /* Block not allocated, return zeros, no need to wait. */
565 memset(buf, 0, n_sectors * SECTOR_SIZE);
566 ret = 0;
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_read(bs->file, offset, buf, n_sectors);
573 logout("%u sectors read\n", n_sectors);
575 nb_sectors -= n_sectors;
576 sector_num += n_sectors;
577 buf += n_sectors * SECTOR_SIZE;
580 return ret;
583 static int vdi_co_write(BlockDriverState *bs,
584 int64_t sector_num, const uint8_t *buf, int nb_sectors)
586 BDRVVdiState *s = bs->opaque;
587 uint32_t bmap_entry;
588 uint32_t block_index;
589 uint32_t sector_in_block;
590 uint32_t n_sectors;
591 uint32_t bmap_first = VDI_UNALLOCATED;
592 uint32_t bmap_last = VDI_UNALLOCATED;
593 uint8_t *block = NULL;
594 int ret = 0;
596 logout("\n");
598 while (ret >= 0 && nb_sectors > 0) {
599 block_index = sector_num / s->block_sectors;
600 sector_in_block = sector_num % s->block_sectors;
601 n_sectors = s->block_sectors - sector_in_block;
602 if (n_sectors > nb_sectors) {
603 n_sectors = nb_sectors;
606 logout("will write %u sectors starting at sector %" PRIu64 "\n",
607 n_sectors, sector_num);
609 /* prepare next AIO request */
610 bmap_entry = le32_to_cpu(s->bmap[block_index]);
611 if (!VDI_IS_ALLOCATED(bmap_entry)) {
612 /* Allocate new block and write to it. */
613 uint64_t offset;
614 bmap_entry = s->header.blocks_allocated;
615 s->bmap[block_index] = cpu_to_le32(bmap_entry);
616 s->header.blocks_allocated++;
617 offset = s->header.offset_data / SECTOR_SIZE +
618 (uint64_t)bmap_entry * s->block_sectors;
619 if (block == NULL) {
620 block = g_malloc(s->block_size);
621 bmap_first = block_index;
623 bmap_last = block_index;
624 /* Copy data to be written to new block and zero unused parts. */
625 memset(block, 0, sector_in_block * SECTOR_SIZE);
626 memcpy(block + sector_in_block * SECTOR_SIZE,
627 buf, n_sectors * SECTOR_SIZE);
628 memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0,
629 (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
630 ret = bdrv_write(bs->file, offset, block, s->block_sectors);
631 } else {
632 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
633 (uint64_t)bmap_entry * s->block_sectors +
634 sector_in_block;
635 ret = bdrv_write(bs->file, offset, buf, n_sectors);
638 nb_sectors -= n_sectors;
639 sector_num += n_sectors;
640 buf += n_sectors * SECTOR_SIZE;
642 logout("%u sectors written\n", n_sectors);
645 logout("finished data write\n");
646 if (ret < 0) {
647 return ret;
650 if (block) {
651 /* One or more new blocks were allocated. */
652 VdiHeader *header = (VdiHeader *) block;
653 uint8_t *base;
654 uint64_t offset;
656 logout("now writing modified header\n");
657 assert(VDI_IS_ALLOCATED(bmap_first));
658 *header = s->header;
659 vdi_header_to_le(header);
660 ret = bdrv_write(bs->file, 0, block, 1);
661 g_free(block);
662 block = NULL;
664 if (ret < 0) {
665 return ret;
668 logout("now writing modified block map entry %u...%u\n",
669 bmap_first, bmap_last);
670 /* Write modified sectors from block map. */
671 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
672 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
673 n_sectors = bmap_last - bmap_first + 1;
674 offset = s->bmap_sector + bmap_first;
675 base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE;
676 logout("will write %u block map sectors starting from entry %u\n",
677 n_sectors, bmap_first);
678 ret = bdrv_write(bs->file, offset, base, n_sectors);
681 return ret;
684 static int vdi_create(const char *filename, QemuOpts *opts, Error **errp)
686 int ret = 0;
687 uint64_t bytes = 0;
688 uint32_t blocks;
689 size_t block_size = DEFAULT_CLUSTER_SIZE;
690 uint32_t image_type = VDI_TYPE_DYNAMIC;
691 VdiHeader header;
692 size_t i;
693 size_t bmap_size;
694 int64_t offset = 0;
695 Error *local_err = NULL;
696 BlockDriverState *bs = NULL;
697 uint32_t *bmap = NULL;
699 logout("\n");
701 /* Read out options. */
702 bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
703 BDRV_SECTOR_SIZE);
704 #if defined(CONFIG_VDI_BLOCK_SIZE)
705 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
706 block_size = qemu_opt_get_size_del(opts,
707 BLOCK_OPT_CLUSTER_SIZE,
708 DEFAULT_CLUSTER_SIZE);
709 #endif
710 #if defined(CONFIG_VDI_STATIC_IMAGE)
711 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) {
712 image_type = VDI_TYPE_STATIC;
714 #endif
716 if (bytes > VDI_DISK_SIZE_MAX) {
717 ret = -ENOTSUP;
718 error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
719 ", max supported is 0x%" PRIx64 ")",
720 bytes, VDI_DISK_SIZE_MAX);
721 goto exit;
724 ret = bdrv_create_file(filename, opts, &local_err);
725 if (ret < 0) {
726 error_propagate(errp, local_err);
727 goto exit;
729 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
730 NULL, &local_err);
731 if (ret < 0) {
732 error_propagate(errp, local_err);
733 goto exit;
736 /* We need enough blocks to store the given disk size,
737 so always round up. */
738 blocks = DIV_ROUND_UP(bytes, block_size);
740 bmap_size = blocks * sizeof(uint32_t);
741 bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE);
743 memset(&header, 0, sizeof(header));
744 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
745 header.signature = VDI_SIGNATURE;
746 header.version = VDI_VERSION_1_1;
747 header.header_size = 0x180;
748 header.image_type = image_type;
749 header.offset_bmap = 0x200;
750 header.offset_data = 0x200 + bmap_size;
751 header.sector_size = SECTOR_SIZE;
752 header.disk_size = bytes;
753 header.block_size = block_size;
754 header.blocks_in_image = blocks;
755 if (image_type == VDI_TYPE_STATIC) {
756 header.blocks_allocated = blocks;
758 uuid_generate(header.uuid_image);
759 uuid_generate(header.uuid_last_snap);
760 /* There is no need to set header.uuid_link or header.uuid_parent here. */
761 #if defined(CONFIG_VDI_DEBUG)
762 vdi_header_print(&header);
763 #endif
764 vdi_header_to_le(&header);
765 ret = bdrv_pwrite_sync(bs, offset, &header, sizeof(header));
766 if (ret < 0) {
767 error_setg(errp, "Error writing header to %s", filename);
768 goto exit;
770 offset += sizeof(header);
772 if (bmap_size > 0) {
773 bmap = g_try_malloc0(bmap_size);
774 if (bmap == NULL) {
775 ret = -ENOMEM;
776 error_setg(errp, "Could not allocate bmap");
777 goto exit;
779 for (i = 0; i < blocks; i++) {
780 if (image_type == VDI_TYPE_STATIC) {
781 bmap[i] = i;
782 } else {
783 bmap[i] = VDI_UNALLOCATED;
786 ret = bdrv_pwrite_sync(bs, offset, bmap, bmap_size);
787 if (ret < 0) {
788 error_setg(errp, "Error writing bmap to %s", filename);
789 goto exit;
791 offset += bmap_size;
794 if (image_type == VDI_TYPE_STATIC) {
795 ret = bdrv_truncate(bs, offset + blocks * block_size);
796 if (ret < 0) {
797 error_setg(errp, "Failed to statically allocate %s", filename);
798 goto exit;
802 exit:
803 bdrv_unref(bs);
804 g_free(bmap);
805 return ret;
808 static void vdi_close(BlockDriverState *bs)
810 BDRVVdiState *s = bs->opaque;
812 qemu_vfree(s->bmap);
814 migrate_del_blocker(s->migration_blocker);
815 error_free(s->migration_blocker);
818 static QemuOptsList vdi_create_opts = {
819 .name = "vdi-create-opts",
820 .head = QTAILQ_HEAD_INITIALIZER(vdi_create_opts.head),
821 .desc = {
823 .name = BLOCK_OPT_SIZE,
824 .type = QEMU_OPT_SIZE,
825 .help = "Virtual disk size"
827 #if defined(CONFIG_VDI_BLOCK_SIZE)
829 .name = BLOCK_OPT_CLUSTER_SIZE,
830 .type = QEMU_OPT_SIZE,
831 .help = "VDI cluster (block) size",
832 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
834 #endif
835 #if defined(CONFIG_VDI_STATIC_IMAGE)
837 .name = BLOCK_OPT_STATIC,
838 .type = QEMU_OPT_BOOL,
839 .help = "VDI static (pre-allocated) image",
840 .def_value_str = "off"
842 #endif
844 .name = BLOCK_OPT_NOCOW,
845 .type = QEMU_OPT_BOOL,
846 .help = "Turn off copy-on-write (valid only on btrfs)"
848 /* TODO: An additional option to set UUID values might be useful. */
849 { /* end of list */ }
853 static BlockDriver bdrv_vdi = {
854 .format_name = "vdi",
855 .instance_size = sizeof(BDRVVdiState),
856 .bdrv_probe = vdi_probe,
857 .bdrv_open = vdi_open,
858 .bdrv_close = vdi_close,
859 .bdrv_reopen_prepare = vdi_reopen_prepare,
860 .bdrv_create = vdi_create,
861 .bdrv_has_zero_init = bdrv_has_zero_init_1,
862 .bdrv_co_get_block_status = vdi_co_get_block_status,
863 .bdrv_make_empty = vdi_make_empty,
865 .bdrv_read = vdi_co_read,
866 #if defined(CONFIG_VDI_WRITE)
867 .bdrv_write = vdi_co_write,
868 #endif
870 .bdrv_get_info = vdi_get_info,
872 .create_opts = &vdi_create_opts,
873 .bdrv_check = vdi_check,
876 static void bdrv_vdi_init(void)
878 logout("\n");
879 bdrv_register(&bdrv_vdi);
882 block_init(bdrv_vdi_init);