]> git.proxmox.com Git - qemu.git/blame - block/vdi.c
vdi: move end-of-I/O handling at the end
[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 162 int header_modified;
9aebd98a
SW
163 struct iovec hd_iov;
164 QEMUIOVector hd_qiov;
165 QEMUBH *bh;
166} VdiAIOCB;
167
168typedef struct {
169 char text[0x40];
170 uint32_t signature;
171 uint32_t version;
172 uint32_t header_size;
173 uint32_t image_type;
174 uint32_t image_flags;
175 char description[256];
176 uint32_t offset_bmap;
177 uint32_t offset_data;
178 uint32_t cylinders; /* disk geometry, unused here */
179 uint32_t heads; /* disk geometry, unused here */
180 uint32_t sectors; /* disk geometry, unused here */
181 uint32_t sector_size;
182 uint32_t unused1;
183 uint64_t disk_size;
184 uint32_t block_size;
185 uint32_t block_extra; /* unused here */
186 uint32_t blocks_in_image;
187 uint32_t blocks_allocated;
188 uuid_t uuid_image;
189 uuid_t uuid_last_snap;
190 uuid_t uuid_link;
191 uuid_t uuid_parent;
192 uint64_t unused2[7];
193} VdiHeader;
194
195typedef struct {
9aebd98a
SW
196 /* The block map entries are little endian (even in memory). */
197 uint32_t *bmap;
198 /* Size of block (bytes). */
199 uint32_t block_size;
200 /* Size of block (sectors). */
201 uint32_t block_sectors;
202 /* First sector of block map. */
203 uint32_t bmap_sector;
4ff9786c 204 /* VDI header (converted to host endianness). */
9aebd98a 205 VdiHeader header;
fc9d106c
KW
206
207 Error *migration_blocker;
9aebd98a
SW
208} BDRVVdiState;
209
210/* Change UUID from little endian (IPRT = VirtualBox format) to big endian
211 * format (network byte order, standard, see RFC 4122) and vice versa.
212 */
213static void uuid_convert(uuid_t uuid)
214{
215 bswap32s((uint32_t *)&uuid[0]);
216 bswap16s((uint16_t *)&uuid[4]);
217 bswap16s((uint16_t *)&uuid[6]);
218}
219
220static void vdi_header_to_cpu(VdiHeader *header)
221{
222 le32_to_cpus(&header->signature);
223 le32_to_cpus(&header->version);
224 le32_to_cpus(&header->header_size);
225 le32_to_cpus(&header->image_type);
226 le32_to_cpus(&header->image_flags);
227 le32_to_cpus(&header->offset_bmap);
228 le32_to_cpus(&header->offset_data);
229 le32_to_cpus(&header->cylinders);
230 le32_to_cpus(&header->heads);
231 le32_to_cpus(&header->sectors);
232 le32_to_cpus(&header->sector_size);
233 le64_to_cpus(&header->disk_size);
234 le32_to_cpus(&header->block_size);
235 le32_to_cpus(&header->block_extra);
236 le32_to_cpus(&header->blocks_in_image);
237 le32_to_cpus(&header->blocks_allocated);
238 uuid_convert(header->uuid_image);
239 uuid_convert(header->uuid_last_snap);
240 uuid_convert(header->uuid_link);
241 uuid_convert(header->uuid_parent);
242}
243
244static void vdi_header_to_le(VdiHeader *header)
245{
246 cpu_to_le32s(&header->signature);
247 cpu_to_le32s(&header->version);
248 cpu_to_le32s(&header->header_size);
249 cpu_to_le32s(&header->image_type);
250 cpu_to_le32s(&header->image_flags);
251 cpu_to_le32s(&header->offset_bmap);
252 cpu_to_le32s(&header->offset_data);
253 cpu_to_le32s(&header->cylinders);
254 cpu_to_le32s(&header->heads);
255 cpu_to_le32s(&header->sectors);
256 cpu_to_le32s(&header->sector_size);
257 cpu_to_le64s(&header->disk_size);
258 cpu_to_le32s(&header->block_size);
259 cpu_to_le32s(&header->block_extra);
260 cpu_to_le32s(&header->blocks_in_image);
261 cpu_to_le32s(&header->blocks_allocated);
262 cpu_to_le32s(&header->blocks_allocated);
263 uuid_convert(header->uuid_image);
264 uuid_convert(header->uuid_last_snap);
265 uuid_convert(header->uuid_link);
266 uuid_convert(header->uuid_parent);
267}
268
269#if defined(CONFIG_VDI_DEBUG)
270static void vdi_header_print(VdiHeader *header)
271{
272 char uuid[37];
273 logout("text %s", header->text);
274 logout("signature 0x%04x\n", header->signature);
275 logout("header size 0x%04x\n", header->header_size);
276 logout("image type 0x%04x\n", header->image_type);
277 logout("image flags 0x%04x\n", header->image_flags);
278 logout("description %s\n", header->description);
279 logout("offset bmap 0x%04x\n", header->offset_bmap);
280 logout("offset data 0x%04x\n", header->offset_data);
281 logout("cylinders 0x%04x\n", header->cylinders);
282 logout("heads 0x%04x\n", header->heads);
283 logout("sectors 0x%04x\n", header->sectors);
284 logout("sector size 0x%04x\n", header->sector_size);
285 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
286 header->disk_size, header->disk_size / MiB);
287 logout("block size 0x%04x\n", header->block_size);
288 logout("block extra 0x%04x\n", header->block_extra);
289 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
290 logout("blocks all. 0x%04x\n", header->blocks_allocated);
291 uuid_unparse(header->uuid_image, uuid);
292 logout("uuid image %s\n", uuid);
293 uuid_unparse(header->uuid_last_snap, uuid);
294 logout("uuid snap %s\n", uuid);
295 uuid_unparse(header->uuid_link, uuid);
296 logout("uuid link %s\n", uuid);
297 uuid_unparse(header->uuid_parent, uuid);
298 logout("uuid parent %s\n", uuid);
299}
300#endif
301
9ac228e0 302static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
9aebd98a
SW
303{
304 /* TODO: additional checks possible. */
305 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
9aebd98a
SW
306 uint32_t blocks_allocated = 0;
307 uint32_t block;
308 uint32_t *bmap;
309 logout("\n");
310
7267c094 311 bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
9aebd98a
SW
312 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
313
314 /* Check block map and value of blocks_allocated. */
315 for (block = 0; block < s->header.blocks_in_image; block++) {
316 uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
c794b4e0 317 if (VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
318 if (bmap_entry < s->header.blocks_in_image) {
319 blocks_allocated++;
c794b4e0 320 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
9aebd98a
SW
321 bmap[bmap_entry] = bmap_entry;
322 } else {
323 fprintf(stderr, "ERROR: block index %" PRIu32
324 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
9ac228e0 325 res->corruptions++;
9aebd98a
SW
326 }
327 } else {
328 fprintf(stderr, "ERROR: block index %" PRIu32
329 " too large, is %" PRIu32 "\n", block, bmap_entry);
9ac228e0 330 res->corruptions++;
9aebd98a
SW
331 }
332 }
333 }
334 if (blocks_allocated != s->header.blocks_allocated) {
335 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
336 ", should be %" PRIu32 "\n",
337 blocks_allocated, s->header.blocks_allocated);
9ac228e0 338 res->corruptions++;
9aebd98a
SW
339 }
340
7267c094 341 g_free(bmap);
9aebd98a 342
9ac228e0 343 return 0;
9aebd98a
SW
344}
345
346static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
347{
348 /* TODO: vdi_get_info would be needed for machine snapshots.
349 vm_state_offset is still missing. */
350 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
351 logout("\n");
352 bdi->cluster_size = s->block_size;
353 bdi->vm_state_offset = 0;
354 return 0;
355}
356
357static int vdi_make_empty(BlockDriverState *bs)
358{
359 /* TODO: missing code. */
360 logout("\n");
361 /* The return value for missing code must be 0, see block.c. */
362 return 0;
363}
364
365static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
366{
367 const VdiHeader *header = (const VdiHeader *)buf;
368 int result = 0;
369
370 logout("\n");
371
372 if (buf_size < sizeof(*header)) {
373 /* Header too small, no VDI. */
374 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
375 result = 100;
376 }
377
378 if (result == 0) {
379 logout("no vdi image\n");
380 } else {
381 logout("%s", header->text);
382 }
383
384 return result;
385}
386
66f82cee 387static int vdi_open(BlockDriverState *bs, int flags)
9aebd98a
SW
388{
389 BDRVVdiState *s = bs->opaque;
390 VdiHeader header;
391 size_t bmap_size;
9aebd98a
SW
392
393 logout("\n");
394
66f82cee 395 if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) {
9aebd98a
SW
396 goto fail;
397 }
398
399 vdi_header_to_cpu(&header);
400#if defined(CONFIG_VDI_DEBUG)
401 vdi_header_print(&header);
402#endif
403
f21dc3a4
SW
404 if (header.disk_size % SECTOR_SIZE != 0) {
405 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
406 We accept them but round the disk size to the next multiple of
407 SECTOR_SIZE. */
408 logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
409 header.disk_size += SECTOR_SIZE - 1;
410 header.disk_size &= ~(SECTOR_SIZE - 1);
411 }
412
9aebd98a
SW
413 if (header.version != VDI_VERSION_1_1) {
414 logout("unsupported version %u.%u\n",
415 header.version >> 16, header.version & 0xffff);
416 goto fail;
417 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
418 /* We only support block maps which start on a sector boundary. */
419 logout("unsupported block map offset 0x%x B\n", header.offset_bmap);
420 goto fail;
421 } else if (header.offset_data % SECTOR_SIZE != 0) {
422 /* We only support data blocks which start on a sector boundary. */
423 logout("unsupported data offset 0x%x B\n", header.offset_data);
424 goto fail;
425 } else if (header.sector_size != SECTOR_SIZE) {
426 logout("unsupported sector size %u B\n", header.sector_size);
427 goto fail;
428 } else if (header.block_size != 1 * MiB) {
429 logout("unsupported block size %u B\n", header.block_size);
430 goto fail;
f21dc3a4
SW
431 } else if (header.disk_size >
432 (uint64_t)header.blocks_in_image * header.block_size) {
433 logout("unsupported disk size %" PRIu64 " B\n", header.disk_size);
9aebd98a
SW
434 goto fail;
435 } else if (!uuid_is_null(header.uuid_link)) {
436 logout("link uuid != 0, unsupported\n");
437 goto fail;
438 } else if (!uuid_is_null(header.uuid_parent)) {
439 logout("parent uuid != 0, unsupported\n");
440 goto fail;
441 }
442
443 bs->total_sectors = header.disk_size / SECTOR_SIZE;
444
445 s->block_size = header.block_size;
446 s->block_sectors = header.block_size / SECTOR_SIZE;
447 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
448 s->header = header;
449
450 bmap_size = header.blocks_in_image * sizeof(uint32_t);
6eea90eb 451 bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
b76b6e95 452 if (bmap_size > 0) {
7267c094 453 s->bmap = g_malloc(bmap_size * SECTOR_SIZE);
b76b6e95 454 }
66f82cee 455 if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
9aebd98a
SW
456 goto fail_free_bmap;
457 }
458
fc9d106c
KW
459 /* Disable migration when vdi images are used */
460 error_set(&s->migration_blocker,
461 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
462 "vdi", bs->device_name, "live migration");
463 migrate_add_blocker(s->migration_blocker);
464
9aebd98a
SW
465 return 0;
466
467 fail_free_bmap:
7267c094 468 g_free(s->bmap);
9aebd98a
SW
469
470 fail:
9aebd98a
SW
471 return -1;
472}
473
e850b35a
SH
474static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs,
475 int64_t sector_num, int nb_sectors, int *pnum)
9aebd98a
SW
476{
477 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
478 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
479 size_t bmap_index = sector_num / s->block_sectors;
480 size_t sector_in_block = sector_num % s->block_sectors;
481 int n_sectors = s->block_sectors - sector_in_block;
482 uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
483 logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
484 if (n_sectors > nb_sectors) {
485 n_sectors = nb_sectors;
486 }
487 *pnum = n_sectors;
c794b4e0 488 return VDI_IS_ALLOCATED(bmap_entry);
9aebd98a
SW
489}
490
9aebd98a
SW
491static AIOPool vdi_aio_pool = {
492 .aiocb_size = sizeof(VdiAIOCB),
9aebd98a
SW
493};
494
495static VdiAIOCB *vdi_aio_setup(BlockDriverState *bs, int64_t sector_num,
3d46a75a 496 QEMUIOVector *qiov, int nb_sectors, int is_write)
9aebd98a
SW
497{
498 VdiAIOCB *acb;
499
500 logout("%p, %" PRId64 ", %p, %d, %p, %p, %d\n",
3d46a75a 501 bs, sector_num, qiov, nb_sectors, is_write);
9aebd98a 502
3d46a75a 503 acb = qemu_aio_get(&vdi_aio_pool, bs, NULL, NULL);
91977c2e
PB
504 acb->sector_num = sector_num;
505 acb->qiov = qiov;
506 acb->is_write = is_write;
507
508 if (qiov->niov > 1) {
509 acb->buf = qemu_blockalign(bs, qiov->size);
510 acb->orig_buf = acb->buf;
511 if (is_write) {
512 qemu_iovec_to_buffer(qiov, acb->buf);
9aebd98a 513 }
91977c2e
PB
514 } else {
515 acb->buf = (uint8_t *)qiov->iov->iov_base;
9aebd98a 516 }
91977c2e
PB
517 acb->nb_sectors = nb_sectors;
518 acb->n_sectors = 0;
519 acb->bmap_first = VDI_UNALLOCATED;
520 acb->bmap_last = VDI_UNALLOCATED;
521 acb->block_buffer = NULL;
522 acb->header_modified = 0;
9aebd98a
SW
523 return acb;
524}
525
3d46a75a 526static int vdi_aio_read_cb(void *opaque, int ret)
9aebd98a
SW
527{
528 VdiAIOCB *acb = opaque;
529 BlockDriverState *bs = acb->common.bs;
530 BDRVVdiState *s = bs->opaque;
531 uint32_t bmap_entry;
532 uint32_t block_index;
533 uint32_t sector_in_block;
534 uint32_t n_sectors;
535
3d46a75a 536restart:
9aebd98a
SW
537 block_index = acb->sector_num / s->block_sectors;
538 sector_in_block = acb->sector_num % s->block_sectors;
539 n_sectors = s->block_sectors - sector_in_block;
540 if (n_sectors > acb->nb_sectors) {
541 n_sectors = acb->nb_sectors;
542 }
543
544 logout("will read %u sectors starting at sector %" PRIu64 "\n",
545 n_sectors, acb->sector_num);
546
547 /* prepare next AIO request */
548 acb->n_sectors = n_sectors;
549 bmap_entry = le32_to_cpu(s->bmap[block_index]);
c794b4e0 550 if (!VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
551 /* Block not allocated, return zeros, no need to wait. */
552 memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
3d46a75a 553 ret = 0;
9aebd98a
SW
554 } else {
555 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
556 (uint64_t)bmap_entry * s->block_sectors +
557 sector_in_block;
558 acb->hd_iov.iov_base = (void *)acb->buf;
559 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
560 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
3d46a75a
PB
561 ret = bdrv_co_readv(bs->file, offset, n_sectors, &acb->hd_qiov);
562 }
0c7bfc32
PB
563 logout("%u sectors read\n", acb->n_sectors);
564
565 acb->nb_sectors -= acb->n_sectors;
566 acb->sector_num += acb->n_sectors;
567 acb->buf += acb->n_sectors * SECTOR_SIZE;
568
569 if (ret >= 0 && acb->nb_sectors > 0) {
3d46a75a 570 goto restart;
9aebd98a 571 }
3d46a75a 572
9aebd98a
SW
573 if (acb->qiov->niov > 1) {
574 qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
575 qemu_vfree(acb->orig_buf);
576 }
9aebd98a 577 qemu_aio_release(acb);
3d46a75a 578 return ret;
9aebd98a
SW
579}
580
3d46a75a
PB
581static int vdi_co_readv(BlockDriverState *bs,
582 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
9aebd98a
SW
583{
584 VdiAIOCB *acb;
e67a64a8
KW
585 int ret;
586
9aebd98a 587 logout("\n");
3d46a75a
PB
588 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, 0);
589 ret = vdi_aio_read_cb(acb, 0);
590 return ret;
9aebd98a
SW
591}
592
3d46a75a 593static int vdi_aio_write_cb(void *opaque, int ret)
9aebd98a
SW
594{
595 VdiAIOCB *acb = opaque;
596 BlockDriverState *bs = acb->common.bs;
597 BDRVVdiState *s = bs->opaque;
598 uint32_t bmap_entry;
599 uint32_t block_index;
600 uint32_t sector_in_block;
601 uint32_t n_sectors;
602
3d46a75a 603restart:
9aebd98a
SW
604 block_index = acb->sector_num / s->block_sectors;
605 sector_in_block = acb->sector_num % s->block_sectors;
606 n_sectors = s->block_sectors - sector_in_block;
607 if (n_sectors > acb->nb_sectors) {
608 n_sectors = acb->nb_sectors;
609 }
610
611 logout("will write %u sectors starting at sector %" PRIu64 "\n",
612 n_sectors, acb->sector_num);
613
614 /* prepare next AIO request */
615 acb->n_sectors = n_sectors;
616 bmap_entry = le32_to_cpu(s->bmap[block_index]);
c794b4e0 617 if (!VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
618 /* Allocate new block and write to it. */
619 uint64_t offset;
620 uint8_t *block;
621 bmap_entry = s->header.blocks_allocated;
622 s->bmap[block_index] = cpu_to_le32(bmap_entry);
623 s->header.blocks_allocated++;
624 offset = s->header.offset_data / SECTOR_SIZE +
625 (uint64_t)bmap_entry * s->block_sectors;
626 block = acb->block_buffer;
627 if (block == NULL) {
641543b7 628 block = g_malloc(s->block_size);
9aebd98a
SW
629 acb->block_buffer = block;
630 acb->bmap_first = block_index;
631 assert(!acb->header_modified);
632 acb->header_modified = 1;
633 }
634 acb->bmap_last = block_index;
641543b7
SW
635 /* Copy data to be written to new block and zero unused parts. */
636 memset(block, 0, sector_in_block * SECTOR_SIZE);
9aebd98a
SW
637 memcpy(block + sector_in_block * SECTOR_SIZE,
638 acb->buf, n_sectors * SECTOR_SIZE);
641543b7
SW
639 memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0,
640 (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
a2a45a26 641 acb->hd_iov.iov_base = (void *)block;
9aebd98a
SW
642 acb->hd_iov.iov_len = s->block_size;
643 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
3d46a75a 644 ret = bdrv_co_writev(bs->file, offset, s->block_sectors, &acb->hd_qiov);
9aebd98a
SW
645 } else {
646 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
647 (uint64_t)bmap_entry * s->block_sectors +
648 sector_in_block;
a2a45a26 649 acb->hd_iov.iov_base = (void *)acb->buf;
9aebd98a
SW
650 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
651 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
3d46a75a
PB
652 ret = bdrv_co_writev(bs->file, offset, n_sectors, &acb->hd_qiov);
653 }
0c7bfc32
PB
654
655 acb->nb_sectors -= acb->n_sectors;
656 acb->sector_num += acb->n_sectors;
657 acb->buf += acb->n_sectors * SECTOR_SIZE;
658
659 logout("%u sectors written\n", acb->n_sectors);
660 if (ret >= 0 && acb->nb_sectors > 0) {
3d46a75a 661 goto restart;
9aebd98a 662 }
9aebd98a 663
0c7bfc32
PB
664 logout("finished data write\n");
665 if (ret >= 0) {
666 ret = 0;
667 if (acb->header_modified) {
668 VdiHeader *header = acb->block_buffer;
669 logout("now writing modified header\n");
670 assert(VDI_IS_ALLOCATED(acb->bmap_first));
671 *header = s->header;
672 vdi_header_to_le(header);
673 acb->header_modified = 0;
674 acb->hd_iov.iov_base = acb->block_buffer;
675 acb->hd_iov.iov_len = SECTOR_SIZE;
676 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
677 ret = bdrv_co_writev(bs->file, 0, 1, &acb->hd_qiov);
678 }
679 if (ret >= 0 && VDI_IS_ALLOCATED(acb->bmap_first)) {
680 /* One or more new blocks were allocated. */
681 uint64_t offset;
682 uint32_t bmap_first;
683 uint32_t bmap_last;
684 g_free(acb->block_buffer);
685 acb->block_buffer = NULL;
686 bmap_first = acb->bmap_first;
687 bmap_last = acb->bmap_last;
688 logout("now writing modified block map entry %u...%u\n",
689 bmap_first, bmap_last);
690 /* Write modified sectors from block map. */
691 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
692 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
693 n_sectors = bmap_last - bmap_first + 1;
694 offset = s->bmap_sector + bmap_first;
695 acb->bmap_first = VDI_UNALLOCATED;
696 acb->hd_iov.iov_base = (void *)((uint8_t *)&s->bmap[0] +
697 bmap_first * SECTOR_SIZE);
698 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
699 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
700 logout("will write %u block map sectors starting from entry %u\n",
701 n_sectors, bmap_first);
702 ret = bdrv_co_writev(bs->file, offset, n_sectors, &acb->hd_qiov);
703 }
704 }
705
9aebd98a
SW
706 if (acb->qiov->niov > 1) {
707 qemu_vfree(acb->orig_buf);
708 }
9aebd98a 709 qemu_aio_release(acb);
3d46a75a 710 return ret;
9aebd98a
SW
711}
712
3d46a75a
PB
713static int vdi_co_writev(BlockDriverState *bs,
714 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
9aebd98a
SW
715{
716 VdiAIOCB *acb;
e67a64a8
KW
717 int ret;
718
9aebd98a 719 logout("\n");
3d46a75a
PB
720 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, 1);
721 ret = vdi_aio_write_cb(acb, 0);
722 return ret;
9aebd98a
SW
723}
724
725static int vdi_create(const char *filename, QEMUOptionParameter *options)
726{
727 int fd;
728 int result = 0;
729 uint64_t bytes = 0;
730 uint32_t blocks;
99cce9fa 731 size_t block_size = DEFAULT_CLUSTER_SIZE;
9aebd98a
SW
732 uint32_t image_type = VDI_TYPE_DYNAMIC;
733 VdiHeader header;
734 size_t i;
735 size_t bmap_size;
736 uint32_t *bmap;
737
738 logout("\n");
739
740 /* Read out options. */
741 while (options && options->name) {
742 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
743 bytes = options->value.n;
744#if defined(CONFIG_VDI_BLOCK_SIZE)
745 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
746 if (options->value.n) {
747 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
748 block_size = options->value.n;
749 }
750#endif
751#if defined(CONFIG_VDI_STATIC_IMAGE)
752 } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
6eea90eb
SW
753 if (options->value.n) {
754 image_type = VDI_TYPE_STATIC;
755 }
9aebd98a
SW
756#endif
757 }
758 options++;
759 }
760
761 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
762 0644);
763 if (fd < 0) {
764 return -errno;
765 }
766
f21dc3a4
SW
767 /* We need enough blocks to store the given disk size,
768 so always round up. */
769 blocks = (bytes + block_size - 1) / block_size;
770
9aebd98a
SW
771 bmap_size = blocks * sizeof(uint32_t);
772 bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
773
774 memset(&header, 0, sizeof(header));
1786dc15 775 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
9aebd98a
SW
776 header.signature = VDI_SIGNATURE;
777 header.version = VDI_VERSION_1_1;
778 header.header_size = 0x180;
779 header.image_type = image_type;
780 header.offset_bmap = 0x200;
781 header.offset_data = 0x200 + bmap_size;
782 header.sector_size = SECTOR_SIZE;
783 header.disk_size = bytes;
784 header.block_size = block_size;
785 header.blocks_in_image = blocks;
6eea90eb
SW
786 if (image_type == VDI_TYPE_STATIC) {
787 header.blocks_allocated = blocks;
788 }
9aebd98a
SW
789 uuid_generate(header.uuid_image);
790 uuid_generate(header.uuid_last_snap);
791 /* There is no need to set header.uuid_link or header.uuid_parent here. */
792#if defined(CONFIG_VDI_DEBUG)
793 vdi_header_print(&header);
794#endif
795 vdi_header_to_le(&header);
796 if (write(fd, &header, sizeof(header)) < 0) {
797 result = -errno;
798 }
799
b76b6e95
SW
800 bmap = NULL;
801 if (bmap_size > 0) {
7267c094 802 bmap = (uint32_t *)g_malloc0(bmap_size);
b76b6e95 803 }
9aebd98a
SW
804 for (i = 0; i < blocks; i++) {
805 if (image_type == VDI_TYPE_STATIC) {
806 bmap[i] = i;
807 } else {
808 bmap[i] = VDI_UNALLOCATED;
809 }
810 }
811 if (write(fd, bmap, bmap_size) < 0) {
812 result = -errno;
813 }
7267c094 814 g_free(bmap);
9aebd98a
SW
815 if (image_type == VDI_TYPE_STATIC) {
816 if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
817 result = -errno;
818 }
819 }
820
821 if (close(fd) < 0) {
822 result = -errno;
823 }
824
825 return result;
826}
827
828static void vdi_close(BlockDriverState *bs)
829{
fc9d106c 830 BDRVVdiState *s = bs->opaque;
6ac5f388
KW
831
832 g_free(s->bmap);
833
fc9d106c
KW
834 migrate_del_blocker(s->migration_blocker);
835 error_free(s->migration_blocker);
9aebd98a
SW
836}
837
9aebd98a
SW
838static QEMUOptionParameter vdi_create_options[] = {
839 {
840 .name = BLOCK_OPT_SIZE,
841 .type = OPT_SIZE,
842 .help = "Virtual disk size"
843 },
844#if defined(CONFIG_VDI_BLOCK_SIZE)
845 {
846 .name = BLOCK_OPT_CLUSTER_SIZE,
847 .type = OPT_SIZE,
99cce9fa
KW
848 .help = "VDI cluster (block) size",
849 .value = { .n = DEFAULT_CLUSTER_SIZE },
9aebd98a
SW
850 },
851#endif
852#if defined(CONFIG_VDI_STATIC_IMAGE)
853 {
854 .name = BLOCK_OPT_STATIC,
855 .type = OPT_FLAG,
856 .help = "VDI static (pre-allocated) image"
857 },
858#endif
859 /* TODO: An additional option to set UUID values might be useful. */
860 { NULL }
861};
862
863static BlockDriver bdrv_vdi = {
864 .format_name = "vdi",
865 .instance_size = sizeof(BDRVVdiState),
866 .bdrv_probe = vdi_probe,
867 .bdrv_open = vdi_open,
868 .bdrv_close = vdi_close,
869 .bdrv_create = vdi_create,
e850b35a 870 .bdrv_co_is_allocated = vdi_co_is_allocated,
9aebd98a
SW
871 .bdrv_make_empty = vdi_make_empty,
872
3d46a75a 873 .bdrv_co_readv = vdi_co_readv,
9aebd98a 874#if defined(CONFIG_VDI_WRITE)
3d46a75a 875 .bdrv_co_writev = vdi_co_writev,
9aebd98a
SW
876#endif
877
878 .bdrv_get_info = vdi_get_info,
879
880 .create_options = vdi_create_options,
881 .bdrv_check = vdi_check,
882};
883
884static void bdrv_vdi_init(void)
885{
886 logout("\n");
887 bdrv_register(&bdrv_vdi);
888}
889
890block_init(bdrv_vdi_init);