]> git.proxmox.com Git - qemu.git/blame - block/vdi.c
Merge remote-tracking branch 'kwolf/for-anthony' into staging
[qemu.git] / block / vdi.c
CommitLineData
9aebd98a
SW
1/*
2 * Block driver for the Virtual Disk Image (VDI) format
3 *
641543b7 4 * Copyright (c) 2009, 2012 Stefan Weil
9aebd98a
SW
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Reference:
20 * http://forums.virtualbox.org/viewtopic.php?t=8046
21 *
22 * This driver supports create / read / write operations on VDI images.
23 *
24 * Todo (see also TODO in code):
25 *
26 * Some features like snapshots are still missing.
27 *
28 * Deallocation of zero-filled blocks and shrinking images are missing, too
29 * (might be added to common block layer).
30 *
31 * Allocation of blocks could be optimized (less writes to block map and
32 * header).
33 *
34 * Read and write of adjacents blocks could be done in one operation
35 * (current code uses one operation per block (1 MiB).
36 *
37 * The code is not thread safe (missing locks for changes in header and
38 * block table, no problem with current QEMU).
39 *
40 * Hints:
41 *
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.
46 *
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.
50 */
51
52#include "qemu-common.h"
53#include "block_int.h"
54#include "module.h"
fc9d106c 55#include "migration.h"
9aebd98a 56
ee682d27 57#if defined(CONFIG_UUID)
9aebd98a
SW
58#include <uuid/uuid.h>
59#else
60/* TODO: move uuid emulation to some central place in QEMU. */
61#include "sysemu.h" /* UUID_FMT */
62typedef unsigned char uuid_t[16];
63void uuid_generate(uuid_t out);
64int uuid_is_null(const uuid_t uu);
65void uuid_unparse(const uuid_t uu, char *out);
66#endif
67
68/* Code configuration options. */
69
70/* Enable debug messages. */
71//~ #define CONFIG_VDI_DEBUG
72
73/* Support write operations on VDI images. */
74#define CONFIG_VDI_WRITE
75
76/* Support non-standard block (cluster) size. This is untested.
77 * Maybe it will be needed for very large images.
78 */
79//~ #define CONFIG_VDI_BLOCK_SIZE
80
81/* Support static (fixed, pre-allocated) images. */
82#define CONFIG_VDI_STATIC_IMAGE
83
84/* Command line option for static images. */
85#define BLOCK_OPT_STATIC "static"
86
87#define KiB 1024
88#define MiB (KiB * KiB)
89
90#define SECTOR_SIZE 512
99cce9fa 91#define DEFAULT_CLUSTER_SIZE (1 * MiB)
9aebd98a
SW
92
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
99
100/* Image signature. */
101#define VDI_SIGNATURE 0xbeda107f
102
103/* Image version. */
104#define VDI_VERSION_1_1 0x00010001
105
106/* Image type. */
107#define VDI_TYPE_DYNAMIC 1
108#define VDI_TYPE_STATIC 2
109
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.
115 */
116#define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
117
c794b4e0
ES
118/* A never-allocated block; semantically arbitrary content. */
119#define VDI_UNALLOCATED 0xffffffffU
120
121/* A discarded (no longer allocated) block; semantically zero-filled. */
122#define VDI_DISCARDED 0xfffffffeU
123
124#define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
9aebd98a 125
ee682d27 126#if !defined(CONFIG_UUID)
9aebd98a
SW
127void uuid_generate(uuid_t out)
128{
4f3669ea 129 memset(out, 0, sizeof(uuid_t));
9aebd98a
SW
130}
131
132int uuid_is_null(const uuid_t uu)
133{
134 uuid_t null_uuid = { 0 };
4f3669ea 135 return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0;
9aebd98a
SW
136}
137
138void uuid_unparse(const uuid_t uu, char *out)
139{
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]);
143}
144#endif
145
146typedef 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;
e67a64a8 161 bool is_write;
9aebd98a
SW
162 int header_modified;
163 BlockDriverAIOCB *hd_aiocb;
164 struct iovec hd_iov;
165 QEMUIOVector hd_qiov;
166 QEMUBH *bh;
167} VdiAIOCB;
168
169typedef 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;
195
196typedef struct {
9aebd98a
SW
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;
4ff9786c 205 /* VDI header (converted to host endianness). */
9aebd98a 206 VdiHeader header;
fc9d106c
KW
207
208 Error *migration_blocker;
9aebd98a
SW
209} BDRVVdiState;
210
211/* Change UUID from little endian (IPRT = VirtualBox format) to big endian
212 * format (network byte order, standard, see RFC 4122) and vice versa.
213 */
214static void uuid_convert(uuid_t uuid)
215{
216 bswap32s((uint32_t *)&uuid[0]);
217 bswap16s((uint16_t *)&uuid[4]);
218 bswap16s((uint16_t *)&uuid[6]);
219}
220
221static void vdi_header_to_cpu(VdiHeader *header)
222{
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);
243}
244
245static void vdi_header_to_le(VdiHeader *header)
246{
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);
268}
269
270#if defined(CONFIG_VDI_DEBUG)
271static void vdi_header_print(VdiHeader *header)
272{
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);
300}
301#endif
302
9ac228e0 303static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
9aebd98a
SW
304{
305 /* TODO: additional checks possible. */
306 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
9aebd98a
SW
307 uint32_t blocks_allocated = 0;
308 uint32_t block;
309 uint32_t *bmap;
310 logout("\n");
311
7267c094 312 bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
9aebd98a
SW
313 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
314
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]);
c794b4e0 318 if (VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
319 if (bmap_entry < s->header.blocks_in_image) {
320 blocks_allocated++;
c794b4e0 321 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
9aebd98a
SW
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);
9ac228e0 326 res->corruptions++;
9aebd98a
SW
327 }
328 } else {
329 fprintf(stderr, "ERROR: block index %" PRIu32
330 " too large, is %" PRIu32 "\n", block, bmap_entry);
9ac228e0 331 res->corruptions++;
9aebd98a
SW
332 }
333 }
334 }
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);
9ac228e0 339 res->corruptions++;
9aebd98a
SW
340 }
341
7267c094 342 g_free(bmap);
9aebd98a 343
9ac228e0 344 return 0;
9aebd98a
SW
345}
346
347static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
348{
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;
356}
357
358static int vdi_make_empty(BlockDriverState *bs)
359{
360 /* TODO: missing code. */
361 logout("\n");
362 /* The return value for missing code must be 0, see block.c. */
363 return 0;
364}
365
366static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
367{
368 const VdiHeader *header = (const VdiHeader *)buf;
369 int result = 0;
370
371 logout("\n");
372
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;
377 }
378
379 if (result == 0) {
380 logout("no vdi image\n");
381 } else {
382 logout("%s", header->text);
383 }
384
385 return result;
386}
387
66f82cee 388static int vdi_open(BlockDriverState *bs, int flags)
9aebd98a
SW
389{
390 BDRVVdiState *s = bs->opaque;
391 VdiHeader header;
392 size_t bmap_size;
9aebd98a
SW
393
394 logout("\n");
395
66f82cee 396 if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) {
9aebd98a
SW
397 goto fail;
398 }
399
400 vdi_header_to_cpu(&header);
401#if defined(CONFIG_VDI_DEBUG)
402 vdi_header_print(&header);
403#endif
404
f21dc3a4
SW
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);
412 }
413
9aebd98a
SW
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;
f21dc3a4
SW
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);
9aebd98a
SW
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;
442 }
443
444 bs->total_sectors = header.disk_size / SECTOR_SIZE;
445
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;
450
451 bmap_size = header.blocks_in_image * sizeof(uint32_t);
6eea90eb 452 bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
b76b6e95 453 if (bmap_size > 0) {
7267c094 454 s->bmap = g_malloc(bmap_size * SECTOR_SIZE);
b76b6e95 455 }
66f82cee 456 if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
9aebd98a
SW
457 goto fail_free_bmap;
458 }
459
fc9d106c
KW
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);
465
9aebd98a
SW
466 return 0;
467
468 fail_free_bmap:
7267c094 469 g_free(s->bmap);
9aebd98a
SW
470
471 fail:
9aebd98a
SW
472 return -1;
473}
474
e850b35a
SH
475static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs,
476 int64_t sector_num, int nb_sectors, int *pnum)
9aebd98a
SW
477{
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;
487 }
488 *pnum = n_sectors;
c794b4e0 489 return VDI_IS_ALLOCATED(bmap_entry);
9aebd98a
SW
490}
491
492static void vdi_aio_cancel(BlockDriverAIOCB *blockacb)
493{
494 /* TODO: This code is untested. How can I get it executed? */
b666d239 495 VdiAIOCB *acb = container_of(blockacb, VdiAIOCB, common);
9aebd98a
SW
496 logout("\n");
497 if (acb->hd_aiocb) {
498 bdrv_aio_cancel(acb->hd_aiocb);
499 }
500 qemu_aio_release(acb);
501}
502
503static AIOPool vdi_aio_pool = {
504 .aiocb_size = sizeof(VdiAIOCB),
505 .cancel = vdi_aio_cancel,
506};
507
508static VdiAIOCB *vdi_aio_setup(BlockDriverState *bs, int64_t sector_num,
509 QEMUIOVector *qiov, int nb_sectors,
510 BlockDriverCompletionFunc *cb, void *opaque, int is_write)
511{
512 VdiAIOCB *acb;
513
514 logout("%p, %" PRId64 ", %p, %d, %p, %p, %d\n",
515 bs, sector_num, qiov, nb_sectors, cb, opaque, is_write);
516
517 acb = qemu_aio_get(&vdi_aio_pool, bs, cb, opaque);
91977c2e
PB
518 acb->hd_aiocb = NULL;
519 acb->sector_num = sector_num;
520 acb->qiov = qiov;
521 acb->is_write = is_write;
522
523 if (qiov->niov > 1) {
524 acb->buf = qemu_blockalign(bs, qiov->size);
525 acb->orig_buf = acb->buf;
526 if (is_write) {
527 qemu_iovec_to_buffer(qiov, acb->buf);
9aebd98a 528 }
91977c2e
PB
529 } else {
530 acb->buf = (uint8_t *)qiov->iov->iov_base;
9aebd98a 531 }
91977c2e
PB
532 acb->nb_sectors = nb_sectors;
533 acb->n_sectors = 0;
534 acb->bmap_first = VDI_UNALLOCATED;
535 acb->bmap_last = VDI_UNALLOCATED;
536 acb->block_buffer = NULL;
537 acb->header_modified = 0;
9aebd98a
SW
538 return acb;
539}
540
541static int vdi_schedule_bh(QEMUBHFunc *cb, VdiAIOCB *acb)
542{
543 logout("\n");
544
545 if (acb->bh) {
546 return -EIO;
547 }
548
549 acb->bh = qemu_bh_new(cb, acb);
550 if (!acb->bh) {
551 return -EIO;
552 }
553
554 qemu_bh_schedule(acb->bh);
555
556 return 0;
557}
558
559static void vdi_aio_read_cb(void *opaque, int ret);
e67a64a8 560static void vdi_aio_write_cb(void *opaque, int ret);
9aebd98a 561
e67a64a8 562static void vdi_aio_rw_bh(void *opaque)
9aebd98a
SW
563{
564 VdiAIOCB *acb = opaque;
565 logout("\n");
566 qemu_bh_delete(acb->bh);
567 acb->bh = NULL;
e67a64a8
KW
568
569 if (acb->is_write) {
570 vdi_aio_write_cb(opaque, 0);
571 } else {
572 vdi_aio_read_cb(opaque, 0);
573 }
9aebd98a
SW
574}
575
576static void vdi_aio_read_cb(void *opaque, int ret)
577{
578 VdiAIOCB *acb = opaque;
579 BlockDriverState *bs = acb->common.bs;
580 BDRVVdiState *s = bs->opaque;
581 uint32_t bmap_entry;
582 uint32_t block_index;
583 uint32_t sector_in_block;
584 uint32_t n_sectors;
585
586 logout("%u sectors read\n", acb->n_sectors);
587
588 acb->hd_aiocb = NULL;
589
590 if (ret < 0) {
591 goto done;
592 }
593
594 acb->nb_sectors -= acb->n_sectors;
595
596 if (acb->nb_sectors == 0) {
597 /* request completed */
598 ret = 0;
599 goto done;
600 }
601
602 acb->sector_num += acb->n_sectors;
603 acb->buf += acb->n_sectors * SECTOR_SIZE;
604
605 block_index = acb->sector_num / s->block_sectors;
606 sector_in_block = acb->sector_num % s->block_sectors;
607 n_sectors = s->block_sectors - sector_in_block;
608 if (n_sectors > acb->nb_sectors) {
609 n_sectors = acb->nb_sectors;
610 }
611
612 logout("will read %u sectors starting at sector %" PRIu64 "\n",
613 n_sectors, acb->sector_num);
614
615 /* prepare next AIO request */
616 acb->n_sectors = n_sectors;
617 bmap_entry = le32_to_cpu(s->bmap[block_index]);
c794b4e0 618 if (!VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
619 /* Block not allocated, return zeros, no need to wait. */
620 memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
e67a64a8 621 ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
9aebd98a
SW
622 if (ret < 0) {
623 goto done;
624 }
625 } else {
626 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
627 (uint64_t)bmap_entry * s->block_sectors +
628 sector_in_block;
629 acb->hd_iov.iov_base = (void *)acb->buf;
630 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
631 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
66f82cee 632 acb->hd_aiocb = bdrv_aio_readv(bs->file, offset, &acb->hd_qiov,
9aebd98a 633 n_sectors, vdi_aio_read_cb, acb);
9aebd98a
SW
634 }
635 return;
636done:
637 if (acb->qiov->niov > 1) {
638 qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
639 qemu_vfree(acb->orig_buf);
640 }
641 acb->common.cb(acb->common.opaque, ret);
642 qemu_aio_release(acb);
643}
644
645static BlockDriverAIOCB *vdi_aio_readv(BlockDriverState *bs,
646 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
647 BlockDriverCompletionFunc *cb, void *opaque)
648{
649 VdiAIOCB *acb;
e67a64a8
KW
650 int ret;
651
9aebd98a
SW
652 logout("\n");
653 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
e67a64a8
KW
654 ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
655 if (ret < 0) {
656 if (acb->qiov->niov > 1) {
657 qemu_vfree(acb->orig_buf);
658 }
659 qemu_aio_release(acb);
660 return NULL;
661 }
662
9aebd98a
SW
663 return &acb->common;
664}
665
666static void vdi_aio_write_cb(void *opaque, int ret)
667{
668 VdiAIOCB *acb = opaque;
669 BlockDriverState *bs = acb->common.bs;
670 BDRVVdiState *s = bs->opaque;
671 uint32_t bmap_entry;
672 uint32_t block_index;
673 uint32_t sector_in_block;
674 uint32_t n_sectors;
675
676 acb->hd_aiocb = NULL;
677
678 if (ret < 0) {
679 goto done;
680 }
681
682 acb->nb_sectors -= acb->n_sectors;
683 acb->sector_num += acb->n_sectors;
684 acb->buf += acb->n_sectors * SECTOR_SIZE;
685
686 if (acb->nb_sectors == 0) {
687 logout("finished data write\n");
688 acb->n_sectors = 0;
689 if (acb->header_modified) {
690 VdiHeader *header = acb->block_buffer;
691 logout("now writing modified header\n");
c794b4e0 692 assert(VDI_IS_ALLOCATED(acb->bmap_first));
9aebd98a
SW
693 *header = s->header;
694 vdi_header_to_le(header);
695 acb->header_modified = 0;
696 acb->hd_iov.iov_base = acb->block_buffer;
697 acb->hd_iov.iov_len = SECTOR_SIZE;
698 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
66f82cee 699 acb->hd_aiocb = bdrv_aio_writev(bs->file, 0, &acb->hd_qiov, 1,
9aebd98a 700 vdi_aio_write_cb, acb);
9aebd98a 701 return;
c794b4e0 702 } else if (VDI_IS_ALLOCATED(acb->bmap_first)) {
9aebd98a
SW
703 /* One or more new blocks were allocated. */
704 uint64_t offset;
705 uint32_t bmap_first;
706 uint32_t bmap_last;
7267c094 707 g_free(acb->block_buffer);
9aebd98a
SW
708 acb->block_buffer = NULL;
709 bmap_first = acb->bmap_first;
710 bmap_last = acb->bmap_last;
711 logout("now writing modified block map entry %u...%u\n",
712 bmap_first, bmap_last);
713 /* Write modified sectors from block map. */
714 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
715 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
716 n_sectors = bmap_last - bmap_first + 1;
717 offset = s->bmap_sector + bmap_first;
718 acb->bmap_first = VDI_UNALLOCATED;
a2a45a26
BS
719 acb->hd_iov.iov_base = (void *)((uint8_t *)&s->bmap[0] +
720 bmap_first * SECTOR_SIZE);
9aebd98a
SW
721 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
722 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
723 logout("will write %u block map sectors starting from entry %u\n",
724 n_sectors, bmap_first);
66f82cee 725 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
9aebd98a 726 n_sectors, vdi_aio_write_cb, acb);
9aebd98a
SW
727 return;
728 }
729 ret = 0;
730 goto done;
731 }
732
733 logout("%u sectors written\n", acb->n_sectors);
734
735 block_index = acb->sector_num / s->block_sectors;
736 sector_in_block = acb->sector_num % s->block_sectors;
737 n_sectors = s->block_sectors - sector_in_block;
738 if (n_sectors > acb->nb_sectors) {
739 n_sectors = acb->nb_sectors;
740 }
741
742 logout("will write %u sectors starting at sector %" PRIu64 "\n",
743 n_sectors, acb->sector_num);
744
745 /* prepare next AIO request */
746 acb->n_sectors = n_sectors;
747 bmap_entry = le32_to_cpu(s->bmap[block_index]);
c794b4e0 748 if (!VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
749 /* Allocate new block and write to it. */
750 uint64_t offset;
751 uint8_t *block;
752 bmap_entry = s->header.blocks_allocated;
753 s->bmap[block_index] = cpu_to_le32(bmap_entry);
754 s->header.blocks_allocated++;
755 offset = s->header.offset_data / SECTOR_SIZE +
756 (uint64_t)bmap_entry * s->block_sectors;
757 block = acb->block_buffer;
758 if (block == NULL) {
641543b7 759 block = g_malloc(s->block_size);
9aebd98a
SW
760 acb->block_buffer = block;
761 acb->bmap_first = block_index;
762 assert(!acb->header_modified);
763 acb->header_modified = 1;
764 }
765 acb->bmap_last = block_index;
641543b7
SW
766 /* Copy data to be written to new block and zero unused parts. */
767 memset(block, 0, sector_in_block * SECTOR_SIZE);
9aebd98a
SW
768 memcpy(block + sector_in_block * SECTOR_SIZE,
769 acb->buf, n_sectors * SECTOR_SIZE);
641543b7
SW
770 memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0,
771 (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
a2a45a26 772 acb->hd_iov.iov_base = (void *)block;
9aebd98a
SW
773 acb->hd_iov.iov_len = s->block_size;
774 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
66f82cee 775 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset,
9aebd98a
SW
776 &acb->hd_qiov, s->block_sectors,
777 vdi_aio_write_cb, acb);
9aebd98a
SW
778 } else {
779 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
780 (uint64_t)bmap_entry * s->block_sectors +
781 sector_in_block;
a2a45a26 782 acb->hd_iov.iov_base = (void *)acb->buf;
9aebd98a
SW
783 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
784 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
66f82cee 785 acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
9aebd98a 786 n_sectors, vdi_aio_write_cb, acb);
9aebd98a
SW
787 }
788
789 return;
790
791done:
792 if (acb->qiov->niov > 1) {
793 qemu_vfree(acb->orig_buf);
794 }
795 acb->common.cb(acb->common.opaque, ret);
796 qemu_aio_release(acb);
797}
798
799static BlockDriverAIOCB *vdi_aio_writev(BlockDriverState *bs,
800 int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
801 BlockDriverCompletionFunc *cb, void *opaque)
802{
803 VdiAIOCB *acb;
e67a64a8
KW
804 int ret;
805
9aebd98a
SW
806 logout("\n");
807 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
e67a64a8
KW
808 ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
809 if (ret < 0) {
810 if (acb->qiov->niov > 1) {
811 qemu_vfree(acb->orig_buf);
812 }
813 qemu_aio_release(acb);
814 return NULL;
815 }
816
9aebd98a
SW
817 return &acb->common;
818}
819
820static int vdi_create(const char *filename, QEMUOptionParameter *options)
821{
822 int fd;
823 int result = 0;
824 uint64_t bytes = 0;
825 uint32_t blocks;
99cce9fa 826 size_t block_size = DEFAULT_CLUSTER_SIZE;
9aebd98a
SW
827 uint32_t image_type = VDI_TYPE_DYNAMIC;
828 VdiHeader header;
829 size_t i;
830 size_t bmap_size;
831 uint32_t *bmap;
832
833 logout("\n");
834
835 /* Read out options. */
836 while (options && options->name) {
837 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
838 bytes = options->value.n;
839#if defined(CONFIG_VDI_BLOCK_SIZE)
840 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
841 if (options->value.n) {
842 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
843 block_size = options->value.n;
844 }
845#endif
846#if defined(CONFIG_VDI_STATIC_IMAGE)
847 } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
6eea90eb
SW
848 if (options->value.n) {
849 image_type = VDI_TYPE_STATIC;
850 }
9aebd98a
SW
851#endif
852 }
853 options++;
854 }
855
856 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
857 0644);
858 if (fd < 0) {
859 return -errno;
860 }
861
f21dc3a4
SW
862 /* We need enough blocks to store the given disk size,
863 so always round up. */
864 blocks = (bytes + block_size - 1) / block_size;
865
9aebd98a
SW
866 bmap_size = blocks * sizeof(uint32_t);
867 bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
868
869 memset(&header, 0, sizeof(header));
1786dc15 870 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
9aebd98a
SW
871 header.signature = VDI_SIGNATURE;
872 header.version = VDI_VERSION_1_1;
873 header.header_size = 0x180;
874 header.image_type = image_type;
875 header.offset_bmap = 0x200;
876 header.offset_data = 0x200 + bmap_size;
877 header.sector_size = SECTOR_SIZE;
878 header.disk_size = bytes;
879 header.block_size = block_size;
880 header.blocks_in_image = blocks;
6eea90eb
SW
881 if (image_type == VDI_TYPE_STATIC) {
882 header.blocks_allocated = blocks;
883 }
9aebd98a
SW
884 uuid_generate(header.uuid_image);
885 uuid_generate(header.uuid_last_snap);
886 /* There is no need to set header.uuid_link or header.uuid_parent here. */
887#if defined(CONFIG_VDI_DEBUG)
888 vdi_header_print(&header);
889#endif
890 vdi_header_to_le(&header);
891 if (write(fd, &header, sizeof(header)) < 0) {
892 result = -errno;
893 }
894
b76b6e95
SW
895 bmap = NULL;
896 if (bmap_size > 0) {
7267c094 897 bmap = (uint32_t *)g_malloc0(bmap_size);
b76b6e95 898 }
9aebd98a
SW
899 for (i = 0; i < blocks; i++) {
900 if (image_type == VDI_TYPE_STATIC) {
901 bmap[i] = i;
902 } else {
903 bmap[i] = VDI_UNALLOCATED;
904 }
905 }
906 if (write(fd, bmap, bmap_size) < 0) {
907 result = -errno;
908 }
7267c094 909 g_free(bmap);
9aebd98a
SW
910 if (image_type == VDI_TYPE_STATIC) {
911 if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
912 result = -errno;
913 }
914 }
915
916 if (close(fd) < 0) {
917 result = -errno;
918 }
919
920 return result;
921}
922
923static void vdi_close(BlockDriverState *bs)
924{
fc9d106c 925 BDRVVdiState *s = bs->opaque;
6ac5f388
KW
926
927 g_free(s->bmap);
928
fc9d106c
KW
929 migrate_del_blocker(s->migration_blocker);
930 error_free(s->migration_blocker);
9aebd98a
SW
931}
932
8b94ff85 933static coroutine_fn int vdi_co_flush(BlockDriverState *bs)
9aebd98a 934{
9aebd98a 935 logout("\n");
8b94ff85 936 return bdrv_co_flush(bs->file);
9aebd98a
SW
937}
938
939
940static QEMUOptionParameter vdi_create_options[] = {
941 {
942 .name = BLOCK_OPT_SIZE,
943 .type = OPT_SIZE,
944 .help = "Virtual disk size"
945 },
946#if defined(CONFIG_VDI_BLOCK_SIZE)
947 {
948 .name = BLOCK_OPT_CLUSTER_SIZE,
949 .type = OPT_SIZE,
99cce9fa
KW
950 .help = "VDI cluster (block) size",
951 .value = { .n = DEFAULT_CLUSTER_SIZE },
9aebd98a
SW
952 },
953#endif
954#if defined(CONFIG_VDI_STATIC_IMAGE)
955 {
956 .name = BLOCK_OPT_STATIC,
957 .type = OPT_FLAG,
958 .help = "VDI static (pre-allocated) image"
959 },
960#endif
961 /* TODO: An additional option to set UUID values might be useful. */
962 { NULL }
963};
964
965static BlockDriver bdrv_vdi = {
966 .format_name = "vdi",
967 .instance_size = sizeof(BDRVVdiState),
968 .bdrv_probe = vdi_probe,
969 .bdrv_open = vdi_open,
970 .bdrv_close = vdi_close,
971 .bdrv_create = vdi_create,
c68b89ac 972 .bdrv_co_flush_to_disk = vdi_co_flush,
e850b35a 973 .bdrv_co_is_allocated = vdi_co_is_allocated,
9aebd98a
SW
974 .bdrv_make_empty = vdi_make_empty,
975
976 .bdrv_aio_readv = vdi_aio_readv,
977#if defined(CONFIG_VDI_WRITE)
978 .bdrv_aio_writev = vdi_aio_writev,
979#endif
980
981 .bdrv_get_info = vdi_get_info,
982
983 .create_options = vdi_create_options,
984 .bdrv_check = vdi_check,
985};
986
987static void bdrv_vdi_init(void)
988{
989 logout("\n");
990 bdrv_register(&bdrv_vdi);
991}
992
993block_init(bdrv_vdi_init);