block/vdi: Allow disk images of size 0
[qemu.git] / block / vdi.c
blob3ea41033412b94239c5e04c9e83a9a4833fe1215
1 /*
2 * Block driver for the Virtual Disk Image (VDI) format
4 * Copyright (c) 2009 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"
56 #if defined(CONFIG_UUID)
57 #include <uuid/uuid.h>
58 #else
59 /* TODO: move uuid emulation to some central place in QEMU. */
60 #include "sysemu.h" /* UUID_FMT */
61 typedef unsigned char uuid_t[16];
62 void uuid_generate(uuid_t out);
63 int uuid_is_null(const uuid_t uu);
64 void uuid_unparse(const uuid_t uu, char *out);
65 #endif
67 /* Code configuration options. */
69 /* Enable debug messages. */
70 //~ #define CONFIG_VDI_DEBUG
72 /* Support write operations on VDI images. */
73 #define CONFIG_VDI_WRITE
75 /* Support non-standard block (cluster) size. This is untested.
76 * Maybe it will be needed for very large images.
78 //~ #define CONFIG_VDI_BLOCK_SIZE
80 /* Support static (fixed, pre-allocated) images. */
81 #define CONFIG_VDI_STATIC_IMAGE
83 /* Command line option for static images. */
84 #define BLOCK_OPT_STATIC "static"
86 #define KiB 1024
87 #define MiB (KiB * KiB)
89 #define SECTOR_SIZE 512
91 #if defined(CONFIG_VDI_DEBUG)
92 #define logout(fmt, ...) \
93 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
94 #else
95 #define logout(fmt, ...) ((void)0)
96 #endif
98 /* Image signature. */
99 #define VDI_SIGNATURE 0xbeda107f
101 /* Image version. */
102 #define VDI_VERSION_1_1 0x00010001
104 /* Image type. */
105 #define VDI_TYPE_DYNAMIC 1
106 #define VDI_TYPE_STATIC 2
108 /* Innotek / SUN images use these strings in header.text:
109 * "<<< innotek VirtualBox Disk Image >>>\n"
110 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
111 * "<<< Sun VirtualBox Disk Image >>>\n"
112 * The value does not matter, so QEMU created images use a different text.
114 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
116 /* Unallocated blocks use this index (no need to convert endianess). */
117 #define VDI_UNALLOCATED UINT32_MAX
119 #if !defined(CONFIG_UUID)
120 void uuid_generate(uuid_t out)
122 memset(out, 0, sizeof(out));
125 int uuid_is_null(const uuid_t uu)
127 uuid_t null_uuid = { 0 };
128 return memcmp(uu, null_uuid, sizeof(uu)) == 0;
131 void uuid_unparse(const uuid_t uu, char *out)
133 snprintf(out, 37, UUID_FMT,
134 uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7],
135 uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
137 #endif
139 typedef struct {
140 BlockDriverAIOCB common;
141 int64_t sector_num;
142 QEMUIOVector *qiov;
143 uint8_t *buf;
144 /* Total number of sectors. */
145 int nb_sectors;
146 /* Number of sectors for current AIO. */
147 int n_sectors;
148 /* New allocated block map entry. */
149 uint32_t bmap_first;
150 uint32_t bmap_last;
151 /* Buffer for new allocated block. */
152 void *block_buffer;
153 void *orig_buf;
154 int header_modified;
155 BlockDriverAIOCB *hd_aiocb;
156 struct iovec hd_iov;
157 QEMUIOVector hd_qiov;
158 QEMUBH *bh;
159 } VdiAIOCB;
161 typedef struct {
162 char text[0x40];
163 uint32_t signature;
164 uint32_t version;
165 uint32_t header_size;
166 uint32_t image_type;
167 uint32_t image_flags;
168 char description[256];
169 uint32_t offset_bmap;
170 uint32_t offset_data;
171 uint32_t cylinders; /* disk geometry, unused here */
172 uint32_t heads; /* disk geometry, unused here */
173 uint32_t sectors; /* disk geometry, unused here */
174 uint32_t sector_size;
175 uint32_t unused1;
176 uint64_t disk_size;
177 uint32_t block_size;
178 uint32_t block_extra; /* unused here */
179 uint32_t blocks_in_image;
180 uint32_t blocks_allocated;
181 uuid_t uuid_image;
182 uuid_t uuid_last_snap;
183 uuid_t uuid_link;
184 uuid_t uuid_parent;
185 uint64_t unused2[7];
186 } VdiHeader;
188 typedef struct {
189 BlockDriverState *hd;
190 /* The block map entries are little endian (even in memory). */
191 uint32_t *bmap;
192 /* Size of block (bytes). */
193 uint32_t block_size;
194 /* Size of block (sectors). */
195 uint32_t block_sectors;
196 /* First sector of block map. */
197 uint32_t bmap_sector;
198 /* VDI header (converted to host endianess). */
199 VdiHeader header;
200 } BDRVVdiState;
202 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
203 * format (network byte order, standard, see RFC 4122) and vice versa.
205 static void uuid_convert(uuid_t uuid)
207 bswap32s((uint32_t *)&uuid[0]);
208 bswap16s((uint16_t *)&uuid[4]);
209 bswap16s((uint16_t *)&uuid[6]);
212 static void vdi_header_to_cpu(VdiHeader *header)
214 le32_to_cpus(&header->signature);
215 le32_to_cpus(&header->version);
216 le32_to_cpus(&header->header_size);
217 le32_to_cpus(&header->image_type);
218 le32_to_cpus(&header->image_flags);
219 le32_to_cpus(&header->offset_bmap);
220 le32_to_cpus(&header->offset_data);
221 le32_to_cpus(&header->cylinders);
222 le32_to_cpus(&header->heads);
223 le32_to_cpus(&header->sectors);
224 le32_to_cpus(&header->sector_size);
225 le64_to_cpus(&header->disk_size);
226 le32_to_cpus(&header->block_size);
227 le32_to_cpus(&header->block_extra);
228 le32_to_cpus(&header->blocks_in_image);
229 le32_to_cpus(&header->blocks_allocated);
230 uuid_convert(header->uuid_image);
231 uuid_convert(header->uuid_last_snap);
232 uuid_convert(header->uuid_link);
233 uuid_convert(header->uuid_parent);
236 static void vdi_header_to_le(VdiHeader *header)
238 cpu_to_le32s(&header->signature);
239 cpu_to_le32s(&header->version);
240 cpu_to_le32s(&header->header_size);
241 cpu_to_le32s(&header->image_type);
242 cpu_to_le32s(&header->image_flags);
243 cpu_to_le32s(&header->offset_bmap);
244 cpu_to_le32s(&header->offset_data);
245 cpu_to_le32s(&header->cylinders);
246 cpu_to_le32s(&header->heads);
247 cpu_to_le32s(&header->sectors);
248 cpu_to_le32s(&header->sector_size);
249 cpu_to_le64s(&header->disk_size);
250 cpu_to_le32s(&header->block_size);
251 cpu_to_le32s(&header->block_extra);
252 cpu_to_le32s(&header->blocks_in_image);
253 cpu_to_le32s(&header->blocks_allocated);
254 cpu_to_le32s(&header->blocks_allocated);
255 uuid_convert(header->uuid_image);
256 uuid_convert(header->uuid_last_snap);
257 uuid_convert(header->uuid_link);
258 uuid_convert(header->uuid_parent);
261 #if defined(CONFIG_VDI_DEBUG)
262 static void vdi_header_print(VdiHeader *header)
264 char uuid[37];
265 logout("text %s", header->text);
266 logout("signature 0x%04x\n", header->signature);
267 logout("header size 0x%04x\n", header->header_size);
268 logout("image type 0x%04x\n", header->image_type);
269 logout("image flags 0x%04x\n", header->image_flags);
270 logout("description %s\n", header->description);
271 logout("offset bmap 0x%04x\n", header->offset_bmap);
272 logout("offset data 0x%04x\n", header->offset_data);
273 logout("cylinders 0x%04x\n", header->cylinders);
274 logout("heads 0x%04x\n", header->heads);
275 logout("sectors 0x%04x\n", header->sectors);
276 logout("sector size 0x%04x\n", header->sector_size);
277 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
278 header->disk_size, header->disk_size / MiB);
279 logout("block size 0x%04x\n", header->block_size);
280 logout("block extra 0x%04x\n", header->block_extra);
281 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
282 logout("blocks all. 0x%04x\n", header->blocks_allocated);
283 uuid_unparse(header->uuid_image, uuid);
284 logout("uuid image %s\n", uuid);
285 uuid_unparse(header->uuid_last_snap, uuid);
286 logout("uuid snap %s\n", uuid);
287 uuid_unparse(header->uuid_link, uuid);
288 logout("uuid link %s\n", uuid);
289 uuid_unparse(header->uuid_parent, uuid);
290 logout("uuid parent %s\n", uuid);
292 #endif
294 static int vdi_check(BlockDriverState *bs)
296 /* TODO: additional checks possible. */
297 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
298 int n_errors = 0;
299 uint32_t blocks_allocated = 0;
300 uint32_t block;
301 uint32_t *bmap;
302 logout("\n");
304 bmap = qemu_malloc(s->header.blocks_in_image * sizeof(uint32_t));
305 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
307 /* Check block map and value of blocks_allocated. */
308 for (block = 0; block < s->header.blocks_in_image; block++) {
309 uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
310 if (bmap_entry != VDI_UNALLOCATED) {
311 if (bmap_entry < s->header.blocks_in_image) {
312 blocks_allocated++;
313 if (bmap[bmap_entry] == VDI_UNALLOCATED) {
314 bmap[bmap_entry] = bmap_entry;
315 } else {
316 fprintf(stderr, "ERROR: block index %" PRIu32
317 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
319 } else {
320 fprintf(stderr, "ERROR: block index %" PRIu32
321 " too large, is %" PRIu32 "\n", block, bmap_entry);
322 n_errors++;
326 if (blocks_allocated != s->header.blocks_allocated) {
327 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
328 ", should be %" PRIu32 "\n",
329 blocks_allocated, s->header.blocks_allocated);
330 n_errors++;
333 qemu_free(bmap);
335 return n_errors;
338 static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
340 /* TODO: vdi_get_info would be needed for machine snapshots.
341 vm_state_offset is still missing. */
342 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
343 logout("\n");
344 bdi->cluster_size = s->block_size;
345 bdi->vm_state_offset = 0;
346 return 0;
349 static int vdi_make_empty(BlockDriverState *bs)
351 /* TODO: missing code. */
352 logout("\n");
353 /* The return value for missing code must be 0, see block.c. */
354 return 0;
357 static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
359 const VdiHeader *header = (const VdiHeader *)buf;
360 int result = 0;
362 logout("\n");
364 if (buf_size < sizeof(*header)) {
365 /* Header too small, no VDI. */
366 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
367 result = 100;
370 if (result == 0) {
371 logout("no vdi image\n");
372 } else {
373 logout("%s", header->text);
376 return result;
379 static int vdi_open(BlockDriverState *bs, int flags)
381 BDRVVdiState *s = bs->opaque;
382 VdiHeader header;
383 size_t bmap_size;
385 logout("\n");
387 if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) {
388 goto fail;
391 vdi_header_to_cpu(&header);
392 #if defined(CONFIG_VDI_DEBUG)
393 vdi_header_print(&header);
394 #endif
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.disk_size % SECTOR_SIZE != 0) {
409 logout("unsupported disk size %" PRIu64 " B\n", header.disk_size);
410 goto fail;
411 } else if (header.sector_size != SECTOR_SIZE) {
412 logout("unsupported sector size %u B\n", header.sector_size);
413 goto fail;
414 } else if (header.block_size != 1 * MiB) {
415 logout("unsupported block size %u B\n", header.block_size);
416 goto fail;
417 } else if ((header.disk_size + header.block_size - 1) / header.block_size !=
418 (uint64_t)header.blocks_in_image) {
419 logout("unexpected block number %u B\n", header.blocks_in_image);
420 goto fail;
421 } else if (!uuid_is_null(header.uuid_link)) {
422 logout("link uuid != 0, unsupported\n");
423 goto fail;
424 } else if (!uuid_is_null(header.uuid_parent)) {
425 logout("parent uuid != 0, unsupported\n");
426 goto fail;
429 bs->total_sectors = header.disk_size / SECTOR_SIZE;
431 s->block_size = header.block_size;
432 s->block_sectors = header.block_size / SECTOR_SIZE;
433 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
434 s->header = header;
436 bmap_size = header.blocks_in_image * sizeof(uint32_t);
437 bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
438 if (bmap_size > 0) {
439 s->bmap = qemu_malloc(bmap_size * SECTOR_SIZE);
441 if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
442 goto fail_free_bmap;
445 return 0;
447 fail_free_bmap:
448 qemu_free(s->bmap);
450 fail:
451 return -1;
454 static int vdi_is_allocated(BlockDriverState *bs, int64_t sector_num,
455 int nb_sectors, int *pnum)
457 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
458 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
459 size_t bmap_index = sector_num / s->block_sectors;
460 size_t sector_in_block = sector_num % s->block_sectors;
461 int n_sectors = s->block_sectors - sector_in_block;
462 uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
463 logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
464 if (n_sectors > nb_sectors) {
465 n_sectors = nb_sectors;
467 *pnum = n_sectors;
468 return bmap_entry != VDI_UNALLOCATED;
471 static void vdi_aio_cancel(BlockDriverAIOCB *blockacb)
473 /* TODO: This code is untested. How can I get it executed? */
474 VdiAIOCB *acb = container_of(blockacb, VdiAIOCB, common);
475 logout("\n");
476 if (acb->hd_aiocb) {
477 bdrv_aio_cancel(acb->hd_aiocb);
479 qemu_aio_release(acb);
482 static AIOPool vdi_aio_pool = {
483 .aiocb_size = sizeof(VdiAIOCB),
484 .cancel = vdi_aio_cancel,
487 static VdiAIOCB *vdi_aio_setup(BlockDriverState *bs, int64_t sector_num,
488 QEMUIOVector *qiov, int nb_sectors,
489 BlockDriverCompletionFunc *cb, void *opaque, int is_write)
491 VdiAIOCB *acb;
493 logout("%p, %" PRId64 ", %p, %d, %p, %p, %d\n",
494 bs, sector_num, qiov, nb_sectors, cb, opaque, is_write);
496 acb = qemu_aio_get(&vdi_aio_pool, bs, cb, opaque);
497 if (acb) {
498 acb->hd_aiocb = NULL;
499 acb->sector_num = sector_num;
500 acb->qiov = qiov;
501 if (qiov->niov > 1) {
502 acb->buf = qemu_blockalign(bs, qiov->size);
503 acb->orig_buf = acb->buf;
504 if (is_write) {
505 qemu_iovec_to_buffer(qiov, acb->buf);
507 } else {
508 acb->buf = (uint8_t *)qiov->iov->iov_base;
510 acb->nb_sectors = nb_sectors;
511 acb->n_sectors = 0;
512 acb->bmap_first = VDI_UNALLOCATED;
513 acb->bmap_last = VDI_UNALLOCATED;
514 acb->block_buffer = NULL;
515 acb->header_modified = 0;
517 return acb;
520 static int vdi_schedule_bh(QEMUBHFunc *cb, VdiAIOCB *acb)
522 logout("\n");
524 if (acb->bh) {
525 return -EIO;
528 acb->bh = qemu_bh_new(cb, acb);
529 if (!acb->bh) {
530 return -EIO;
533 qemu_bh_schedule(acb->bh);
535 return 0;
538 static void vdi_aio_read_cb(void *opaque, int ret);
540 static void vdi_aio_read_bh(void *opaque)
542 VdiAIOCB *acb = opaque;
543 logout("\n");
544 qemu_bh_delete(acb->bh);
545 acb->bh = NULL;
546 vdi_aio_read_cb(opaque, 0);
549 static void vdi_aio_read_cb(void *opaque, int ret)
551 VdiAIOCB *acb = opaque;
552 BlockDriverState *bs = acb->common.bs;
553 BDRVVdiState *s = bs->opaque;
554 uint32_t bmap_entry;
555 uint32_t block_index;
556 uint32_t sector_in_block;
557 uint32_t n_sectors;
559 logout("%u sectors read\n", acb->n_sectors);
561 acb->hd_aiocb = NULL;
563 if (ret < 0) {
564 goto done;
567 acb->nb_sectors -= acb->n_sectors;
569 if (acb->nb_sectors == 0) {
570 /* request completed */
571 ret = 0;
572 goto done;
575 acb->sector_num += acb->n_sectors;
576 acb->buf += acb->n_sectors * SECTOR_SIZE;
578 block_index = acb->sector_num / s->block_sectors;
579 sector_in_block = acb->sector_num % s->block_sectors;
580 n_sectors = s->block_sectors - sector_in_block;
581 if (n_sectors > acb->nb_sectors) {
582 n_sectors = acb->nb_sectors;
585 logout("will read %u sectors starting at sector %" PRIu64 "\n",
586 n_sectors, acb->sector_num);
588 /* prepare next AIO request */
589 acb->n_sectors = n_sectors;
590 bmap_entry = le32_to_cpu(s->bmap[block_index]);
591 if (bmap_entry == VDI_UNALLOCATED) {
592 /* Block not allocated, return zeros, no need to wait. */
593 memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
594 ret = vdi_schedule_bh(vdi_aio_read_bh, acb);
595 if (ret < 0) {
596 goto done;
598 } else {
599 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
600 (uint64_t)bmap_entry * s->block_sectors +
601 sector_in_block;
602 acb->hd_iov.iov_base = (void *)acb->buf;
603 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
604 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
605 acb->hd_aiocb = bdrv_aio_readv(bs->file, offset, &acb->hd_qiov,
606 n_sectors, vdi_aio_read_cb, acb);
607 if (acb->hd_aiocb == NULL) {
608 goto done;
611 return;
612 done:
613 if (acb->qiov->niov > 1) {
614 qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
615 qemu_vfree(acb->orig_buf);
617 acb->common.cb(acb->common.opaque, ret);
618 qemu_aio_release(acb);
621 static BlockDriverAIOCB *vdi_aio_readv(BlockDriverState *bs,
622 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
623 BlockDriverCompletionFunc *cb, void *opaque)
625 VdiAIOCB *acb;
626 logout("\n");
627 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
628 if (!acb) {
629 return NULL;
631 vdi_aio_read_cb(acb, 0);
632 return &acb->common;
635 static void vdi_aio_write_cb(void *opaque, int ret)
637 VdiAIOCB *acb = opaque;
638 BlockDriverState *bs = acb->common.bs;
639 BDRVVdiState *s = bs->opaque;
640 uint32_t bmap_entry;
641 uint32_t block_index;
642 uint32_t sector_in_block;
643 uint32_t n_sectors;
645 acb->hd_aiocb = NULL;
647 if (ret < 0) {
648 goto done;
651 acb->nb_sectors -= acb->n_sectors;
652 acb->sector_num += acb->n_sectors;
653 acb->buf += acb->n_sectors * SECTOR_SIZE;
655 if (acb->nb_sectors == 0) {
656 logout("finished data write\n");
657 acb->n_sectors = 0;
658 if (acb->header_modified) {
659 VdiHeader *header = acb->block_buffer;
660 logout("now writing modified header\n");
661 assert(acb->bmap_first != VDI_UNALLOCATED);
662 *header = s->header;
663 vdi_header_to_le(header);
664 acb->header_modified = 0;
665 acb->hd_iov.iov_base = acb->block_buffer;
666 acb->hd_iov.iov_len = SECTOR_SIZE;
667 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
668 acb->hd_aiocb = bdrv_aio_writev(bs->file, 0, &acb->hd_qiov, 1,
669 vdi_aio_write_cb, acb);
670 if (acb->hd_aiocb == NULL) {
671 goto done;
673 return;
674 } else if (acb->bmap_first != VDI_UNALLOCATED) {
675 /* One or more new blocks were allocated. */
676 uint64_t offset;
677 uint32_t bmap_first;
678 uint32_t bmap_last;
679 qemu_free(acb->block_buffer);
680 acb->block_buffer = NULL;
681 bmap_first = acb->bmap_first;
682 bmap_last = acb->bmap_last;
683 logout("now writing modified block map entry %u...%u\n",
684 bmap_first, bmap_last);
685 /* Write modified sectors from block map. */
686 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
687 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
688 n_sectors = bmap_last - bmap_first + 1;
689 offset = s->bmap_sector + bmap_first;
690 acb->bmap_first = VDI_UNALLOCATED;
691 acb->hd_iov.iov_base = (void *)((uint8_t *)&s->bmap[0] +
692 bmap_first * SECTOR_SIZE);
693 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
694 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
695 logout("will write %u block map sectors starting from entry %u\n",
696 n_sectors, bmap_first);
697 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
698 n_sectors, vdi_aio_write_cb, acb);
699 if (acb->hd_aiocb == NULL) {
700 goto done;
702 return;
704 ret = 0;
705 goto done;
708 logout("%u sectors written\n", acb->n_sectors);
710 block_index = acb->sector_num / s->block_sectors;
711 sector_in_block = acb->sector_num % s->block_sectors;
712 n_sectors = s->block_sectors - sector_in_block;
713 if (n_sectors > acb->nb_sectors) {
714 n_sectors = acb->nb_sectors;
717 logout("will write %u sectors starting at sector %" PRIu64 "\n",
718 n_sectors, acb->sector_num);
720 /* prepare next AIO request */
721 acb->n_sectors = n_sectors;
722 bmap_entry = le32_to_cpu(s->bmap[block_index]);
723 if (bmap_entry == VDI_UNALLOCATED) {
724 /* Allocate new block and write to it. */
725 uint64_t offset;
726 uint8_t *block;
727 bmap_entry = s->header.blocks_allocated;
728 s->bmap[block_index] = cpu_to_le32(bmap_entry);
729 s->header.blocks_allocated++;
730 offset = s->header.offset_data / SECTOR_SIZE +
731 (uint64_t)bmap_entry * s->block_sectors;
732 block = acb->block_buffer;
733 if (block == NULL) {
734 block = qemu_mallocz(s->block_size);
735 acb->block_buffer = block;
736 acb->bmap_first = block_index;
737 assert(!acb->header_modified);
738 acb->header_modified = 1;
740 acb->bmap_last = block_index;
741 memcpy(block + sector_in_block * SECTOR_SIZE,
742 acb->buf, n_sectors * SECTOR_SIZE);
743 acb->hd_iov.iov_base = (void *)block;
744 acb->hd_iov.iov_len = s->block_size;
745 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
746 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset,
747 &acb->hd_qiov, s->block_sectors,
748 vdi_aio_write_cb, acb);
749 if (acb->hd_aiocb == NULL) {
750 goto done;
752 } else {
753 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
754 (uint64_t)bmap_entry * s->block_sectors +
755 sector_in_block;
756 acb->hd_iov.iov_base = (void *)acb->buf;
757 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
758 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
759 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
760 n_sectors, vdi_aio_write_cb, acb);
761 if (acb->hd_aiocb == NULL) {
762 goto done;
766 return;
768 done:
769 if (acb->qiov->niov > 1) {
770 qemu_vfree(acb->orig_buf);
772 acb->common.cb(acb->common.opaque, ret);
773 qemu_aio_release(acb);
776 static BlockDriverAIOCB *vdi_aio_writev(BlockDriverState *bs,
777 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
778 BlockDriverCompletionFunc *cb, void *opaque)
780 VdiAIOCB *acb;
781 logout("\n");
782 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
783 if (!acb) {
784 return NULL;
786 vdi_aio_write_cb(acb, 0);
787 return &acb->common;
790 static int vdi_create(const char *filename, QEMUOptionParameter *options)
792 int fd;
793 int result = 0;
794 uint64_t bytes = 0;
795 uint32_t blocks;
796 size_t block_size = 1 * MiB;
797 uint32_t image_type = VDI_TYPE_DYNAMIC;
798 VdiHeader header;
799 size_t i;
800 size_t bmap_size;
801 uint32_t *bmap;
803 logout("\n");
805 /* Read out options. */
806 while (options && options->name) {
807 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
808 bytes = options->value.n;
809 #if defined(CONFIG_VDI_BLOCK_SIZE)
810 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
811 if (options->value.n) {
812 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
813 block_size = options->value.n;
815 #endif
816 #if defined(CONFIG_VDI_STATIC_IMAGE)
817 } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
818 if (options->value.n) {
819 image_type = VDI_TYPE_STATIC;
821 #endif
823 options++;
826 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
827 0644);
828 if (fd < 0) {
829 return -errno;
832 blocks = bytes / block_size;
833 bmap_size = blocks * sizeof(uint32_t);
834 bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
836 memset(&header, 0, sizeof(header));
837 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
838 header.signature = VDI_SIGNATURE;
839 header.version = VDI_VERSION_1_1;
840 header.header_size = 0x180;
841 header.image_type = image_type;
842 header.offset_bmap = 0x200;
843 header.offset_data = 0x200 + bmap_size;
844 header.sector_size = SECTOR_SIZE;
845 header.disk_size = bytes;
846 header.block_size = block_size;
847 header.blocks_in_image = blocks;
848 if (image_type == VDI_TYPE_STATIC) {
849 header.blocks_allocated = blocks;
851 uuid_generate(header.uuid_image);
852 uuid_generate(header.uuid_last_snap);
853 /* There is no need to set header.uuid_link or header.uuid_parent here. */
854 #if defined(CONFIG_VDI_DEBUG)
855 vdi_header_print(&header);
856 #endif
857 vdi_header_to_le(&header);
858 if (write(fd, &header, sizeof(header)) < 0) {
859 result = -errno;
862 bmap = NULL;
863 if (bmap_size > 0) {
864 bmap = (uint32_t *)qemu_mallocz(bmap_size);
866 for (i = 0; i < blocks; i++) {
867 if (image_type == VDI_TYPE_STATIC) {
868 bmap[i] = i;
869 } else {
870 bmap[i] = VDI_UNALLOCATED;
873 if (write(fd, bmap, bmap_size) < 0) {
874 result = -errno;
876 qemu_free(bmap);
877 if (image_type == VDI_TYPE_STATIC) {
878 if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
879 result = -errno;
883 if (close(fd) < 0) {
884 result = -errno;
887 return result;
890 static void vdi_close(BlockDriverState *bs)
894 static void vdi_flush(BlockDriverState *bs)
896 logout("\n");
897 bdrv_flush(bs->file);
901 static QEMUOptionParameter vdi_create_options[] = {
903 .name = BLOCK_OPT_SIZE,
904 .type = OPT_SIZE,
905 .help = "Virtual disk size"
907 #if defined(CONFIG_VDI_BLOCK_SIZE)
909 .name = BLOCK_OPT_CLUSTER_SIZE,
910 .type = OPT_SIZE,
911 .help = "VDI cluster (block) size"
913 #endif
914 #if defined(CONFIG_VDI_STATIC_IMAGE)
916 .name = BLOCK_OPT_STATIC,
917 .type = OPT_FLAG,
918 .help = "VDI static (pre-allocated) image"
920 #endif
921 /* TODO: An additional option to set UUID values might be useful. */
922 { NULL }
925 static BlockDriver bdrv_vdi = {
926 .format_name = "vdi",
927 .instance_size = sizeof(BDRVVdiState),
928 .bdrv_probe = vdi_probe,
929 .bdrv_open = vdi_open,
930 .bdrv_close = vdi_close,
931 .bdrv_create = vdi_create,
932 .bdrv_flush = vdi_flush,
933 .bdrv_is_allocated = vdi_is_allocated,
934 .bdrv_make_empty = vdi_make_empty,
936 .bdrv_aio_readv = vdi_aio_readv,
937 #if defined(CONFIG_VDI_WRITE)
938 .bdrv_aio_writev = vdi_aio_writev,
939 #endif
941 .bdrv_get_info = vdi_get_info,
943 .create_options = vdi_create_options,
944 .bdrv_check = vdi_check,
947 static void bdrv_vdi_init(void)
949 logout("\n");
950 bdrv_register(&bdrv_vdi);
953 block_init(bdrv_vdi_init);