]> git.proxmox.com Git - mirror_qemu.git/blame - block/vdi.c
vdi: merge aio_read_cb and aio_write_cb into callers
[mirror_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
4de659e8
PB
526static int vdi_co_readv(BlockDriverState *bs,
527 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
9aebd98a 528{
4de659e8 529 VdiAIOCB *acb;
9aebd98a
SW
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;
4de659e8
PB
535 int ret;
536
537 logout("\n");
538 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, 0);
9aebd98a 539
3d46a75a 540restart:
9aebd98a
SW
541 block_index = acb->sector_num / s->block_sectors;
542 sector_in_block = acb->sector_num % s->block_sectors;
543 n_sectors = s->block_sectors - sector_in_block;
544 if (n_sectors > acb->nb_sectors) {
545 n_sectors = acb->nb_sectors;
546 }
547
548 logout("will read %u sectors starting at sector %" PRIu64 "\n",
549 n_sectors, acb->sector_num);
550
551 /* prepare next AIO request */
552 acb->n_sectors = n_sectors;
553 bmap_entry = le32_to_cpu(s->bmap[block_index]);
c794b4e0 554 if (!VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
555 /* Block not allocated, return zeros, no need to wait. */
556 memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
3d46a75a 557 ret = 0;
9aebd98a
SW
558 } else {
559 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
560 (uint64_t)bmap_entry * s->block_sectors +
561 sector_in_block;
562 acb->hd_iov.iov_base = (void *)acb->buf;
563 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
564 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
3d46a75a
PB
565 ret = bdrv_co_readv(bs->file, offset, n_sectors, &acb->hd_qiov);
566 }
0c7bfc32
PB
567 logout("%u sectors read\n", acb->n_sectors);
568
569 acb->nb_sectors -= acb->n_sectors;
570 acb->sector_num += acb->n_sectors;
571 acb->buf += acb->n_sectors * SECTOR_SIZE;
572
573 if (ret >= 0 && acb->nb_sectors > 0) {
3d46a75a 574 goto restart;
9aebd98a 575 }
3d46a75a 576
9aebd98a
SW
577 if (acb->qiov->niov > 1) {
578 qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
579 qemu_vfree(acb->orig_buf);
580 }
9aebd98a 581 qemu_aio_release(acb);
3d46a75a 582 return ret;
9aebd98a
SW
583}
584
4de659e8 585static int vdi_co_writev(BlockDriverState *bs,
3d46a75a 586 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
9aebd98a
SW
587{
588 VdiAIOCB *acb;
9aebd98a
SW
589 BDRVVdiState *s = bs->opaque;
590 uint32_t bmap_entry;
591 uint32_t block_index;
592 uint32_t sector_in_block;
593 uint32_t n_sectors;
4de659e8
PB
594 int ret;
595
596 logout("\n");
597 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, 1);
9aebd98a 598
3d46a75a 599restart:
9aebd98a
SW
600 block_index = acb->sector_num / s->block_sectors;
601 sector_in_block = acb->sector_num % s->block_sectors;
602 n_sectors = s->block_sectors - sector_in_block;
603 if (n_sectors > acb->nb_sectors) {
604 n_sectors = acb->nb_sectors;
605 }
606
607 logout("will write %u sectors starting at sector %" PRIu64 "\n",
608 n_sectors, acb->sector_num);
609
610 /* prepare next AIO request */
611 acb->n_sectors = n_sectors;
612 bmap_entry = le32_to_cpu(s->bmap[block_index]);
c794b4e0 613 if (!VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
614 /* Allocate new block and write to it. */
615 uint64_t offset;
616 uint8_t *block;
617 bmap_entry = s->header.blocks_allocated;
618 s->bmap[block_index] = cpu_to_le32(bmap_entry);
619 s->header.blocks_allocated++;
620 offset = s->header.offset_data / SECTOR_SIZE +
621 (uint64_t)bmap_entry * s->block_sectors;
622 block = acb->block_buffer;
623 if (block == NULL) {
641543b7 624 block = g_malloc(s->block_size);
9aebd98a
SW
625 acb->block_buffer = block;
626 acb->bmap_first = block_index;
627 assert(!acb->header_modified);
628 acb->header_modified = 1;
629 }
630 acb->bmap_last = block_index;
641543b7
SW
631 /* Copy data to be written to new block and zero unused parts. */
632 memset(block, 0, sector_in_block * SECTOR_SIZE);
9aebd98a
SW
633 memcpy(block + sector_in_block * SECTOR_SIZE,
634 acb->buf, n_sectors * SECTOR_SIZE);
641543b7
SW
635 memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0,
636 (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
a2a45a26 637 acb->hd_iov.iov_base = (void *)block;
9aebd98a
SW
638 acb->hd_iov.iov_len = s->block_size;
639 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
3d46a75a 640 ret = bdrv_co_writev(bs->file, offset, s->block_sectors, &acb->hd_qiov);
9aebd98a
SW
641 } else {
642 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
643 (uint64_t)bmap_entry * s->block_sectors +
644 sector_in_block;
a2a45a26 645 acb->hd_iov.iov_base = (void *)acb->buf;
9aebd98a
SW
646 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
647 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
3d46a75a
PB
648 ret = bdrv_co_writev(bs->file, offset, n_sectors, &acb->hd_qiov);
649 }
0c7bfc32
PB
650
651 acb->nb_sectors -= acb->n_sectors;
652 acb->sector_num += acb->n_sectors;
653 acb->buf += acb->n_sectors * SECTOR_SIZE;
654
655 logout("%u sectors written\n", acb->n_sectors);
656 if (ret >= 0 && acb->nb_sectors > 0) {
3d46a75a 657 goto restart;
9aebd98a 658 }
9aebd98a 659
0c7bfc32
PB
660 logout("finished data write\n");
661 if (ret >= 0) {
662 ret = 0;
663 if (acb->header_modified) {
664 VdiHeader *header = acb->block_buffer;
665 logout("now writing modified header\n");
666 assert(VDI_IS_ALLOCATED(acb->bmap_first));
667 *header = s->header;
668 vdi_header_to_le(header);
669 acb->header_modified = 0;
670 acb->hd_iov.iov_base = acb->block_buffer;
671 acb->hd_iov.iov_len = SECTOR_SIZE;
672 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
673 ret = bdrv_co_writev(bs->file, 0, 1, &acb->hd_qiov);
674 }
675 if (ret >= 0 && VDI_IS_ALLOCATED(acb->bmap_first)) {
676 /* One or more new blocks were allocated. */
677 uint64_t offset;
678 uint32_t bmap_first;
679 uint32_t bmap_last;
680 g_free(acb->block_buffer);
681 acb->block_buffer = NULL;
682 bmap_first = acb->bmap_first;
683 bmap_last = acb->bmap_last;
684 logout("now writing modified block map entry %u...%u\n",
685 bmap_first, bmap_last);
686 /* Write modified sectors from block map. */
687 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
688 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
689 n_sectors = bmap_last - bmap_first + 1;
690 offset = s->bmap_sector + bmap_first;
691 acb->bmap_first = VDI_UNALLOCATED;
692 acb->hd_iov.iov_base = (void *)((uint8_t *)&s->bmap[0] +
693 bmap_first * SECTOR_SIZE);
694 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
695 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
696 logout("will write %u block map sectors starting from entry %u\n",
697 n_sectors, bmap_first);
698 ret = bdrv_co_writev(bs->file, offset, n_sectors, &acb->hd_qiov);
699 }
700 }
701
9aebd98a
SW
702 if (acb->qiov->niov > 1) {
703 qemu_vfree(acb->orig_buf);
704 }
9aebd98a 705 qemu_aio_release(acb);
3d46a75a 706 return ret;
9aebd98a
SW
707}
708
9aebd98a
SW
709static int vdi_create(const char *filename, QEMUOptionParameter *options)
710{
711 int fd;
712 int result = 0;
713 uint64_t bytes = 0;
714 uint32_t blocks;
99cce9fa 715 size_t block_size = DEFAULT_CLUSTER_SIZE;
9aebd98a
SW
716 uint32_t image_type = VDI_TYPE_DYNAMIC;
717 VdiHeader header;
718 size_t i;
719 size_t bmap_size;
720 uint32_t *bmap;
721
722 logout("\n");
723
724 /* Read out options. */
725 while (options && options->name) {
726 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
727 bytes = options->value.n;
728#if defined(CONFIG_VDI_BLOCK_SIZE)
729 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
730 if (options->value.n) {
731 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
732 block_size = options->value.n;
733 }
734#endif
735#if defined(CONFIG_VDI_STATIC_IMAGE)
736 } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
6eea90eb
SW
737 if (options->value.n) {
738 image_type = VDI_TYPE_STATIC;
739 }
9aebd98a
SW
740#endif
741 }
742 options++;
743 }
744
745 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
746 0644);
747 if (fd < 0) {
748 return -errno;
749 }
750
f21dc3a4
SW
751 /* We need enough blocks to store the given disk size,
752 so always round up. */
753 blocks = (bytes + block_size - 1) / block_size;
754
9aebd98a
SW
755 bmap_size = blocks * sizeof(uint32_t);
756 bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
757
758 memset(&header, 0, sizeof(header));
1786dc15 759 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
9aebd98a
SW
760 header.signature = VDI_SIGNATURE;
761 header.version = VDI_VERSION_1_1;
762 header.header_size = 0x180;
763 header.image_type = image_type;
764 header.offset_bmap = 0x200;
765 header.offset_data = 0x200 + bmap_size;
766 header.sector_size = SECTOR_SIZE;
767 header.disk_size = bytes;
768 header.block_size = block_size;
769 header.blocks_in_image = blocks;
6eea90eb
SW
770 if (image_type == VDI_TYPE_STATIC) {
771 header.blocks_allocated = blocks;
772 }
9aebd98a
SW
773 uuid_generate(header.uuid_image);
774 uuid_generate(header.uuid_last_snap);
775 /* There is no need to set header.uuid_link or header.uuid_parent here. */
776#if defined(CONFIG_VDI_DEBUG)
777 vdi_header_print(&header);
778#endif
779 vdi_header_to_le(&header);
780 if (write(fd, &header, sizeof(header)) < 0) {
781 result = -errno;
782 }
783
b76b6e95
SW
784 bmap = NULL;
785 if (bmap_size > 0) {
7267c094 786 bmap = (uint32_t *)g_malloc0(bmap_size);
b76b6e95 787 }
9aebd98a
SW
788 for (i = 0; i < blocks; i++) {
789 if (image_type == VDI_TYPE_STATIC) {
790 bmap[i] = i;
791 } else {
792 bmap[i] = VDI_UNALLOCATED;
793 }
794 }
795 if (write(fd, bmap, bmap_size) < 0) {
796 result = -errno;
797 }
7267c094 798 g_free(bmap);
9aebd98a
SW
799 if (image_type == VDI_TYPE_STATIC) {
800 if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
801 result = -errno;
802 }
803 }
804
805 if (close(fd) < 0) {
806 result = -errno;
807 }
808
809 return result;
810}
811
812static void vdi_close(BlockDriverState *bs)
813{
fc9d106c 814 BDRVVdiState *s = bs->opaque;
6ac5f388
KW
815
816 g_free(s->bmap);
817
fc9d106c
KW
818 migrate_del_blocker(s->migration_blocker);
819 error_free(s->migration_blocker);
9aebd98a
SW
820}
821
9aebd98a
SW
822static QEMUOptionParameter vdi_create_options[] = {
823 {
824 .name = BLOCK_OPT_SIZE,
825 .type = OPT_SIZE,
826 .help = "Virtual disk size"
827 },
828#if defined(CONFIG_VDI_BLOCK_SIZE)
829 {
830 .name = BLOCK_OPT_CLUSTER_SIZE,
831 .type = OPT_SIZE,
99cce9fa
KW
832 .help = "VDI cluster (block) size",
833 .value = { .n = DEFAULT_CLUSTER_SIZE },
9aebd98a
SW
834 },
835#endif
836#if defined(CONFIG_VDI_STATIC_IMAGE)
837 {
838 .name = BLOCK_OPT_STATIC,
839 .type = OPT_FLAG,
840 .help = "VDI static (pre-allocated) image"
841 },
842#endif
843 /* TODO: An additional option to set UUID values might be useful. */
844 { NULL }
845};
846
847static BlockDriver bdrv_vdi = {
848 .format_name = "vdi",
849 .instance_size = sizeof(BDRVVdiState),
850 .bdrv_probe = vdi_probe,
851 .bdrv_open = vdi_open,
852 .bdrv_close = vdi_close,
853 .bdrv_create = vdi_create,
e850b35a 854 .bdrv_co_is_allocated = vdi_co_is_allocated,
9aebd98a
SW
855 .bdrv_make_empty = vdi_make_empty,
856
3d46a75a 857 .bdrv_co_readv = vdi_co_readv,
9aebd98a 858#if defined(CONFIG_VDI_WRITE)
3d46a75a 859 .bdrv_co_writev = vdi_co_writev,
9aebd98a
SW
860#endif
861
862 .bdrv_get_info = vdi_get_info,
863
864 .create_options = vdi_create_options,
865 .bdrv_check = vdi_check,
866};
867
868static void bdrv_vdi_init(void)
869{
870 logout("\n");
871 bdrv_register(&bdrv_vdi);
872}
873
874block_init(bdrv_vdi_init);