e1000: bounds packet size against buffer size
[qemu.git] / block / vdi.c
blob02da6b44d045eb558a0ef0f3b4a613b1868e633e
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"
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 BlockDriverAIOCB common;
148 int64_t sector_num;
149 QEMUIOVector *qiov;
150 uint8_t *buf;
151 /* Total number of sectors. */
152 int nb_sectors;
153 /* Number of sectors for current AIO. */
154 int n_sectors;
155 /* New allocated block map entry. */
156 uint32_t bmap_first;
157 uint32_t bmap_last;
158 /* Buffer for new allocated block. */
159 void *block_buffer;
160 void *orig_buf;
161 bool is_write;
162 int header_modified;
163 BlockDriverAIOCB *hd_aiocb;
164 struct iovec hd_iov;
165 QEMUIOVector hd_qiov;
166 QEMUBH *bh;
167 } VdiAIOCB;
169 typedef struct {
170 char text[0x40];
171 uint32_t signature;
172 uint32_t version;
173 uint32_t header_size;
174 uint32_t image_type;
175 uint32_t image_flags;
176 char description[256];
177 uint32_t offset_bmap;
178 uint32_t offset_data;
179 uint32_t cylinders; /* disk geometry, unused here */
180 uint32_t heads; /* disk geometry, unused here */
181 uint32_t sectors; /* disk geometry, unused here */
182 uint32_t sector_size;
183 uint32_t unused1;
184 uint64_t disk_size;
185 uint32_t block_size;
186 uint32_t block_extra; /* unused here */
187 uint32_t blocks_in_image;
188 uint32_t blocks_allocated;
189 uuid_t uuid_image;
190 uuid_t uuid_last_snap;
191 uuid_t uuid_link;
192 uuid_t uuid_parent;
193 uint64_t unused2[7];
194 } VdiHeader;
196 typedef struct {
197 /* The block map entries are little endian (even in memory). */
198 uint32_t *bmap;
199 /* Size of block (bytes). */
200 uint32_t block_size;
201 /* Size of block (sectors). */
202 uint32_t block_sectors;
203 /* First sector of block map. */
204 uint32_t bmap_sector;
205 /* VDI header (converted to host endianness). */
206 VdiHeader header;
208 Error *migration_blocker;
209 } BDRVVdiState;
211 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
212 * format (network byte order, standard, see RFC 4122) and vice versa.
214 static void uuid_convert(uuid_t uuid)
216 bswap32s((uint32_t *)&uuid[0]);
217 bswap16s((uint16_t *)&uuid[4]);
218 bswap16s((uint16_t *)&uuid[6]);
221 static void vdi_header_to_cpu(VdiHeader *header)
223 le32_to_cpus(&header->signature);
224 le32_to_cpus(&header->version);
225 le32_to_cpus(&header->header_size);
226 le32_to_cpus(&header->image_type);
227 le32_to_cpus(&header->image_flags);
228 le32_to_cpus(&header->offset_bmap);
229 le32_to_cpus(&header->offset_data);
230 le32_to_cpus(&header->cylinders);
231 le32_to_cpus(&header->heads);
232 le32_to_cpus(&header->sectors);
233 le32_to_cpus(&header->sector_size);
234 le64_to_cpus(&header->disk_size);
235 le32_to_cpus(&header->block_size);
236 le32_to_cpus(&header->block_extra);
237 le32_to_cpus(&header->blocks_in_image);
238 le32_to_cpus(&header->blocks_allocated);
239 uuid_convert(header->uuid_image);
240 uuid_convert(header->uuid_last_snap);
241 uuid_convert(header->uuid_link);
242 uuid_convert(header->uuid_parent);
245 static void vdi_header_to_le(VdiHeader *header)
247 cpu_to_le32s(&header->signature);
248 cpu_to_le32s(&header->version);
249 cpu_to_le32s(&header->header_size);
250 cpu_to_le32s(&header->image_type);
251 cpu_to_le32s(&header->image_flags);
252 cpu_to_le32s(&header->offset_bmap);
253 cpu_to_le32s(&header->offset_data);
254 cpu_to_le32s(&header->cylinders);
255 cpu_to_le32s(&header->heads);
256 cpu_to_le32s(&header->sectors);
257 cpu_to_le32s(&header->sector_size);
258 cpu_to_le64s(&header->disk_size);
259 cpu_to_le32s(&header->block_size);
260 cpu_to_le32s(&header->block_extra);
261 cpu_to_le32s(&header->blocks_in_image);
262 cpu_to_le32s(&header->blocks_allocated);
263 cpu_to_le32s(&header->blocks_allocated);
264 uuid_convert(header->uuid_image);
265 uuid_convert(header->uuid_last_snap);
266 uuid_convert(header->uuid_link);
267 uuid_convert(header->uuid_parent);
270 #if defined(CONFIG_VDI_DEBUG)
271 static void vdi_header_print(VdiHeader *header)
273 char uuid[37];
274 logout("text %s", header->text);
275 logout("signature 0x%04x\n", header->signature);
276 logout("header size 0x%04x\n", header->header_size);
277 logout("image type 0x%04x\n", header->image_type);
278 logout("image flags 0x%04x\n", header->image_flags);
279 logout("description %s\n", header->description);
280 logout("offset bmap 0x%04x\n", header->offset_bmap);
281 logout("offset data 0x%04x\n", header->offset_data);
282 logout("cylinders 0x%04x\n", header->cylinders);
283 logout("heads 0x%04x\n", header->heads);
284 logout("sectors 0x%04x\n", header->sectors);
285 logout("sector size 0x%04x\n", header->sector_size);
286 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
287 header->disk_size, header->disk_size / MiB);
288 logout("block size 0x%04x\n", header->block_size);
289 logout("block extra 0x%04x\n", header->block_extra);
290 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
291 logout("blocks all. 0x%04x\n", header->blocks_allocated);
292 uuid_unparse(header->uuid_image, uuid);
293 logout("uuid image %s\n", uuid);
294 uuid_unparse(header->uuid_last_snap, uuid);
295 logout("uuid snap %s\n", uuid);
296 uuid_unparse(header->uuid_link, uuid);
297 logout("uuid link %s\n", uuid);
298 uuid_unparse(header->uuid_parent, uuid);
299 logout("uuid parent %s\n", uuid);
301 #endif
303 static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
305 /* TODO: additional checks possible. */
306 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
307 uint32_t blocks_allocated = 0;
308 uint32_t block;
309 uint32_t *bmap;
310 logout("\n");
312 bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
313 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
315 /* Check block map and value of blocks_allocated. */
316 for (block = 0; block < s->header.blocks_in_image; block++) {
317 uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
318 if (VDI_IS_ALLOCATED(bmap_entry)) {
319 if (bmap_entry < s->header.blocks_in_image) {
320 blocks_allocated++;
321 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
322 bmap[bmap_entry] = bmap_entry;
323 } else {
324 fprintf(stderr, "ERROR: block index %" PRIu32
325 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
326 res->corruptions++;
328 } else {
329 fprintf(stderr, "ERROR: block index %" PRIu32
330 " too large, is %" PRIu32 "\n", block, bmap_entry);
331 res->corruptions++;
335 if (blocks_allocated != s->header.blocks_allocated) {
336 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
337 ", should be %" PRIu32 "\n",
338 blocks_allocated, s->header.blocks_allocated);
339 res->corruptions++;
342 g_free(bmap);
344 return 0;
347 static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
349 /* TODO: vdi_get_info would be needed for machine snapshots.
350 vm_state_offset is still missing. */
351 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
352 logout("\n");
353 bdi->cluster_size = s->block_size;
354 bdi->vm_state_offset = 0;
355 return 0;
358 static int vdi_make_empty(BlockDriverState *bs)
360 /* TODO: missing code. */
361 logout("\n");
362 /* The return value for missing code must be 0, see block.c. */
363 return 0;
366 static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
368 const VdiHeader *header = (const VdiHeader *)buf;
369 int result = 0;
371 logout("\n");
373 if (buf_size < sizeof(*header)) {
374 /* Header too small, no VDI. */
375 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
376 result = 100;
379 if (result == 0) {
380 logout("no vdi image\n");
381 } else {
382 logout("%s", header->text);
385 return result;
388 static int vdi_open(BlockDriverState *bs, int flags)
390 BDRVVdiState *s = bs->opaque;
391 VdiHeader header;
392 size_t bmap_size;
394 logout("\n");
396 if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) {
397 goto fail;
400 vdi_header_to_cpu(&header);
401 #if defined(CONFIG_VDI_DEBUG)
402 vdi_header_print(&header);
403 #endif
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 += SECTOR_SIZE - 1;
411 header.disk_size &= ~(SECTOR_SIZE - 1);
414 if (header.version != VDI_VERSION_1_1) {
415 logout("unsupported version %u.%u\n",
416 header.version >> 16, header.version & 0xffff);
417 goto fail;
418 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
419 /* We only support block maps which start on a sector boundary. */
420 logout("unsupported block map offset 0x%x B\n", header.offset_bmap);
421 goto fail;
422 } else if (header.offset_data % SECTOR_SIZE != 0) {
423 /* We only support data blocks which start on a sector boundary. */
424 logout("unsupported data offset 0x%x B\n", header.offset_data);
425 goto fail;
426 } else if (header.sector_size != SECTOR_SIZE) {
427 logout("unsupported sector size %u B\n", header.sector_size);
428 goto fail;
429 } else if (header.block_size != 1 * MiB) {
430 logout("unsupported block size %u B\n", header.block_size);
431 goto fail;
432 } else if (header.disk_size >
433 (uint64_t)header.blocks_in_image * header.block_size) {
434 logout("unsupported disk size %" PRIu64 " B\n", header.disk_size);
435 goto fail;
436 } else if (!uuid_is_null(header.uuid_link)) {
437 logout("link uuid != 0, unsupported\n");
438 goto fail;
439 } else if (!uuid_is_null(header.uuid_parent)) {
440 logout("parent uuid != 0, unsupported\n");
441 goto fail;
444 bs->total_sectors = header.disk_size / SECTOR_SIZE;
446 s->block_size = header.block_size;
447 s->block_sectors = header.block_size / SECTOR_SIZE;
448 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
449 s->header = header;
451 bmap_size = header.blocks_in_image * sizeof(uint32_t);
452 bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
453 if (bmap_size > 0) {
454 s->bmap = g_malloc(bmap_size * SECTOR_SIZE);
456 if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
457 goto fail_free_bmap;
460 /* Disable migration when vdi images are used */
461 error_set(&s->migration_blocker,
462 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
463 "vdi", bs->device_name, "live migration");
464 migrate_add_blocker(s->migration_blocker);
466 return 0;
468 fail_free_bmap:
469 g_free(s->bmap);
471 fail:
472 return -1;
475 static int vdi_is_allocated(BlockDriverState *bs, int64_t sector_num,
476 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 logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
485 if (n_sectors > nb_sectors) {
486 n_sectors = nb_sectors;
488 *pnum = n_sectors;
489 return VDI_IS_ALLOCATED(bmap_entry);
492 static void vdi_aio_cancel(BlockDriverAIOCB *blockacb)
494 /* TODO: This code is untested. How can I get it executed? */
495 VdiAIOCB *acb = container_of(blockacb, VdiAIOCB, common);
496 logout("\n");
497 if (acb->hd_aiocb) {
498 bdrv_aio_cancel(acb->hd_aiocb);
500 qemu_aio_release(acb);
503 static AIOPool vdi_aio_pool = {
504 .aiocb_size = sizeof(VdiAIOCB),
505 .cancel = vdi_aio_cancel,
508 static VdiAIOCB *vdi_aio_setup(BlockDriverState *bs, int64_t sector_num,
509 QEMUIOVector *qiov, int nb_sectors,
510 BlockDriverCompletionFunc *cb, void *opaque, int is_write)
512 VdiAIOCB *acb;
514 logout("%p, %" PRId64 ", %p, %d, %p, %p, %d\n",
515 bs, sector_num, qiov, nb_sectors, cb, opaque, is_write);
517 acb = qemu_aio_get(&vdi_aio_pool, bs, cb, opaque);
518 if (acb) {
519 acb->hd_aiocb = NULL;
520 acb->sector_num = sector_num;
521 acb->qiov = qiov;
522 acb->is_write = is_write;
524 if (qiov->niov > 1) {
525 acb->buf = qemu_blockalign(bs, qiov->size);
526 acb->orig_buf = acb->buf;
527 if (is_write) {
528 qemu_iovec_to_buffer(qiov, acb->buf);
530 } else {
531 acb->buf = (uint8_t *)qiov->iov->iov_base;
533 acb->nb_sectors = nb_sectors;
534 acb->n_sectors = 0;
535 acb->bmap_first = VDI_UNALLOCATED;
536 acb->bmap_last = VDI_UNALLOCATED;
537 acb->block_buffer = NULL;
538 acb->header_modified = 0;
540 return acb;
543 static int vdi_schedule_bh(QEMUBHFunc *cb, VdiAIOCB *acb)
545 logout("\n");
547 if (acb->bh) {
548 return -EIO;
551 acb->bh = qemu_bh_new(cb, acb);
552 if (!acb->bh) {
553 return -EIO;
556 qemu_bh_schedule(acb->bh);
558 return 0;
561 static void vdi_aio_read_cb(void *opaque, int ret);
562 static void vdi_aio_write_cb(void *opaque, int ret);
564 static void vdi_aio_rw_bh(void *opaque)
566 VdiAIOCB *acb = opaque;
567 logout("\n");
568 qemu_bh_delete(acb->bh);
569 acb->bh = NULL;
571 if (acb->is_write) {
572 vdi_aio_write_cb(opaque, 0);
573 } else {
574 vdi_aio_read_cb(opaque, 0);
578 static void vdi_aio_read_cb(void *opaque, int ret)
580 VdiAIOCB *acb = opaque;
581 BlockDriverState *bs = acb->common.bs;
582 BDRVVdiState *s = bs->opaque;
583 uint32_t bmap_entry;
584 uint32_t block_index;
585 uint32_t sector_in_block;
586 uint32_t n_sectors;
588 logout("%u sectors read\n", acb->n_sectors);
590 acb->hd_aiocb = NULL;
592 if (ret < 0) {
593 goto done;
596 acb->nb_sectors -= acb->n_sectors;
598 if (acb->nb_sectors == 0) {
599 /* request completed */
600 ret = 0;
601 goto done;
604 acb->sector_num += acb->n_sectors;
605 acb->buf += acb->n_sectors * SECTOR_SIZE;
607 block_index = acb->sector_num / s->block_sectors;
608 sector_in_block = acb->sector_num % s->block_sectors;
609 n_sectors = s->block_sectors - sector_in_block;
610 if (n_sectors > acb->nb_sectors) {
611 n_sectors = acb->nb_sectors;
614 logout("will read %u sectors starting at sector %" PRIu64 "\n",
615 n_sectors, acb->sector_num);
617 /* prepare next AIO request */
618 acb->n_sectors = n_sectors;
619 bmap_entry = le32_to_cpu(s->bmap[block_index]);
620 if (!VDI_IS_ALLOCATED(bmap_entry)) {
621 /* Block not allocated, return zeros, no need to wait. */
622 memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
623 ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
624 if (ret < 0) {
625 goto done;
627 } else {
628 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
629 (uint64_t)bmap_entry * s->block_sectors +
630 sector_in_block;
631 acb->hd_iov.iov_base = (void *)acb->buf;
632 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
633 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
634 acb->hd_aiocb = bdrv_aio_readv(bs->file, offset, &acb->hd_qiov,
635 n_sectors, vdi_aio_read_cb, acb);
636 if (acb->hd_aiocb == NULL) {
637 ret = -EIO;
638 goto done;
641 return;
642 done:
643 if (acb->qiov->niov > 1) {
644 qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
645 qemu_vfree(acb->orig_buf);
647 acb->common.cb(acb->common.opaque, ret);
648 qemu_aio_release(acb);
651 static BlockDriverAIOCB *vdi_aio_readv(BlockDriverState *bs,
652 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
653 BlockDriverCompletionFunc *cb, void *opaque)
655 VdiAIOCB *acb;
656 int ret;
658 logout("\n");
659 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
660 if (!acb) {
661 return NULL;
664 ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
665 if (ret < 0) {
666 if (acb->qiov->niov > 1) {
667 qemu_vfree(acb->orig_buf);
669 qemu_aio_release(acb);
670 return NULL;
673 return &acb->common;
676 static void vdi_aio_write_cb(void *opaque, int ret)
678 VdiAIOCB *acb = opaque;
679 BlockDriverState *bs = acb->common.bs;
680 BDRVVdiState *s = bs->opaque;
681 uint32_t bmap_entry;
682 uint32_t block_index;
683 uint32_t sector_in_block;
684 uint32_t n_sectors;
686 acb->hd_aiocb = NULL;
688 if (ret < 0) {
689 goto done;
692 acb->nb_sectors -= acb->n_sectors;
693 acb->sector_num += acb->n_sectors;
694 acb->buf += acb->n_sectors * SECTOR_SIZE;
696 if (acb->nb_sectors == 0) {
697 logout("finished data write\n");
698 acb->n_sectors = 0;
699 if (acb->header_modified) {
700 VdiHeader *header = acb->block_buffer;
701 logout("now writing modified header\n");
702 assert(VDI_IS_ALLOCATED(acb->bmap_first));
703 *header = s->header;
704 vdi_header_to_le(header);
705 acb->header_modified = 0;
706 acb->hd_iov.iov_base = acb->block_buffer;
707 acb->hd_iov.iov_len = SECTOR_SIZE;
708 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
709 acb->hd_aiocb = bdrv_aio_writev(bs->file, 0, &acb->hd_qiov, 1,
710 vdi_aio_write_cb, acb);
711 if (acb->hd_aiocb == NULL) {
712 ret = -EIO;
713 goto done;
715 return;
716 } else if (VDI_IS_ALLOCATED(acb->bmap_first)) {
717 /* One or more new blocks were allocated. */
718 uint64_t offset;
719 uint32_t bmap_first;
720 uint32_t bmap_last;
721 g_free(acb->block_buffer);
722 acb->block_buffer = NULL;
723 bmap_first = acb->bmap_first;
724 bmap_last = acb->bmap_last;
725 logout("now writing modified block map entry %u...%u\n",
726 bmap_first, bmap_last);
727 /* Write modified sectors from block map. */
728 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
729 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
730 n_sectors = bmap_last - bmap_first + 1;
731 offset = s->bmap_sector + bmap_first;
732 acb->bmap_first = VDI_UNALLOCATED;
733 acb->hd_iov.iov_base = (void *)((uint8_t *)&s->bmap[0] +
734 bmap_first * SECTOR_SIZE);
735 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
736 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
737 logout("will write %u block map sectors starting from entry %u\n",
738 n_sectors, bmap_first);
739 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
740 n_sectors, vdi_aio_write_cb, acb);
741 if (acb->hd_aiocb == NULL) {
742 ret = -EIO;
743 goto done;
745 return;
747 ret = 0;
748 goto done;
751 logout("%u sectors written\n", acb->n_sectors);
753 block_index = acb->sector_num / s->block_sectors;
754 sector_in_block = acb->sector_num % s->block_sectors;
755 n_sectors = s->block_sectors - sector_in_block;
756 if (n_sectors > acb->nb_sectors) {
757 n_sectors = acb->nb_sectors;
760 logout("will write %u sectors starting at sector %" PRIu64 "\n",
761 n_sectors, acb->sector_num);
763 /* prepare next AIO request */
764 acb->n_sectors = n_sectors;
765 bmap_entry = le32_to_cpu(s->bmap[block_index]);
766 if (!VDI_IS_ALLOCATED(bmap_entry)) {
767 /* Allocate new block and write to it. */
768 uint64_t offset;
769 uint8_t *block;
770 bmap_entry = s->header.blocks_allocated;
771 s->bmap[block_index] = cpu_to_le32(bmap_entry);
772 s->header.blocks_allocated++;
773 offset = s->header.offset_data / SECTOR_SIZE +
774 (uint64_t)bmap_entry * s->block_sectors;
775 block = acb->block_buffer;
776 if (block == NULL) {
777 block = g_malloc0(s->block_size);
778 acb->block_buffer = block;
779 acb->bmap_first = block_index;
780 assert(!acb->header_modified);
781 acb->header_modified = 1;
783 acb->bmap_last = block_index;
784 memcpy(block + sector_in_block * SECTOR_SIZE,
785 acb->buf, n_sectors * SECTOR_SIZE);
786 acb->hd_iov.iov_base = (void *)block;
787 acb->hd_iov.iov_len = s->block_size;
788 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
789 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset,
790 &acb->hd_qiov, s->block_sectors,
791 vdi_aio_write_cb, acb);
792 if (acb->hd_aiocb == NULL) {
793 ret = -EIO;
794 goto done;
796 } else {
797 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
798 (uint64_t)bmap_entry * s->block_sectors +
799 sector_in_block;
800 acb->hd_iov.iov_base = (void *)acb->buf;
801 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
802 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
803 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
804 n_sectors, vdi_aio_write_cb, acb);
805 if (acb->hd_aiocb == NULL) {
806 ret = -EIO;
807 goto done;
811 return;
813 done:
814 if (acb->qiov->niov > 1) {
815 qemu_vfree(acb->orig_buf);
817 acb->common.cb(acb->common.opaque, ret);
818 qemu_aio_release(acb);
821 static BlockDriverAIOCB *vdi_aio_writev(BlockDriverState *bs,
822 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
823 BlockDriverCompletionFunc *cb, void *opaque)
825 VdiAIOCB *acb;
826 int ret;
828 logout("\n");
829 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
830 if (!acb) {
831 return NULL;
834 ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
835 if (ret < 0) {
836 if (acb->qiov->niov > 1) {
837 qemu_vfree(acb->orig_buf);
839 qemu_aio_release(acb);
840 return NULL;
843 return &acb->common;
846 static int vdi_create(const char *filename, QEMUOptionParameter *options)
848 int fd;
849 int result = 0;
850 uint64_t bytes = 0;
851 uint32_t blocks;
852 size_t block_size = DEFAULT_CLUSTER_SIZE;
853 uint32_t image_type = VDI_TYPE_DYNAMIC;
854 VdiHeader header;
855 size_t i;
856 size_t bmap_size;
857 uint32_t *bmap;
859 logout("\n");
861 /* Read out options. */
862 while (options && options->name) {
863 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
864 bytes = options->value.n;
865 #if defined(CONFIG_VDI_BLOCK_SIZE)
866 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
867 if (options->value.n) {
868 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
869 block_size = options->value.n;
871 #endif
872 #if defined(CONFIG_VDI_STATIC_IMAGE)
873 } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
874 if (options->value.n) {
875 image_type = VDI_TYPE_STATIC;
877 #endif
879 options++;
882 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
883 0644);
884 if (fd < 0) {
885 return -errno;
888 /* We need enough blocks to store the given disk size,
889 so always round up. */
890 blocks = (bytes + block_size - 1) / block_size;
892 bmap_size = blocks * sizeof(uint32_t);
893 bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
895 memset(&header, 0, sizeof(header));
896 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
897 header.signature = VDI_SIGNATURE;
898 header.version = VDI_VERSION_1_1;
899 header.header_size = 0x180;
900 header.image_type = image_type;
901 header.offset_bmap = 0x200;
902 header.offset_data = 0x200 + bmap_size;
903 header.sector_size = SECTOR_SIZE;
904 header.disk_size = bytes;
905 header.block_size = block_size;
906 header.blocks_in_image = blocks;
907 if (image_type == VDI_TYPE_STATIC) {
908 header.blocks_allocated = blocks;
910 uuid_generate(header.uuid_image);
911 uuid_generate(header.uuid_last_snap);
912 /* There is no need to set header.uuid_link or header.uuid_parent here. */
913 #if defined(CONFIG_VDI_DEBUG)
914 vdi_header_print(&header);
915 #endif
916 vdi_header_to_le(&header);
917 if (write(fd, &header, sizeof(header)) < 0) {
918 result = -errno;
921 bmap = NULL;
922 if (bmap_size > 0) {
923 bmap = (uint32_t *)g_malloc0(bmap_size);
925 for (i = 0; i < blocks; i++) {
926 if (image_type == VDI_TYPE_STATIC) {
927 bmap[i] = i;
928 } else {
929 bmap[i] = VDI_UNALLOCATED;
932 if (write(fd, bmap, bmap_size) < 0) {
933 result = -errno;
935 g_free(bmap);
936 if (image_type == VDI_TYPE_STATIC) {
937 if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
938 result = -errno;
942 if (close(fd) < 0) {
943 result = -errno;
946 return result;
949 static void vdi_close(BlockDriverState *bs)
951 BDRVVdiState *s = bs->opaque;
953 g_free(s->bmap);
955 migrate_del_blocker(s->migration_blocker);
956 error_free(s->migration_blocker);
959 static coroutine_fn int vdi_co_flush(BlockDriverState *bs)
961 logout("\n");
962 return bdrv_co_flush(bs->file);
966 static QEMUOptionParameter vdi_create_options[] = {
968 .name = BLOCK_OPT_SIZE,
969 .type = OPT_SIZE,
970 .help = "Virtual disk size"
972 #if defined(CONFIG_VDI_BLOCK_SIZE)
974 .name = BLOCK_OPT_CLUSTER_SIZE,
975 .type = OPT_SIZE,
976 .help = "VDI cluster (block) size",
977 .value = { .n = DEFAULT_CLUSTER_SIZE },
979 #endif
980 #if defined(CONFIG_VDI_STATIC_IMAGE)
982 .name = BLOCK_OPT_STATIC,
983 .type = OPT_FLAG,
984 .help = "VDI static (pre-allocated) image"
986 #endif
987 /* TODO: An additional option to set UUID values might be useful. */
988 { NULL }
991 static BlockDriver bdrv_vdi = {
992 .format_name = "vdi",
993 .instance_size = sizeof(BDRVVdiState),
994 .bdrv_probe = vdi_probe,
995 .bdrv_open = vdi_open,
996 .bdrv_close = vdi_close,
997 .bdrv_create = vdi_create,
998 .bdrv_co_flush_to_disk = vdi_co_flush,
999 .bdrv_is_allocated = vdi_is_allocated,
1000 .bdrv_make_empty = vdi_make_empty,
1002 .bdrv_aio_readv = vdi_aio_readv,
1003 #if defined(CONFIG_VDI_WRITE)
1004 .bdrv_aio_writev = vdi_aio_writev,
1005 #endif
1007 .bdrv_get_info = vdi_get_info,
1009 .create_options = vdi_create_options,
1010 .bdrv_check = vdi_check,
1013 static void bdrv_vdi_init(void)
1015 logout("\n");
1016 bdrv_register(&bdrv_vdi);
1019 block_init(bdrv_vdi_init);