]> rtime.felk.cvut.cz Git - lisovros/qemu_apohw.git/blob - block/vdi.c
31cdfabdea4a27083bb2109987bf02fce27cd89f
[lisovros/qemu_apohw.git] / block / vdi.c
1 /*
2  * Block driver for the Virtual Disk Image (VDI) format
3  *
4  * Copyright (c) 2009 Stefan Weil
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"
55 #include "migration.h"
56
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
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
91 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
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
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)
125
126 #if !defined(CONFIG_UUID)
127 void uuid_generate(uuid_t out)
128 {
129     memset(out, 0, sizeof(uuid_t));
130 }
131
132 int uuid_is_null(const uuid_t uu)
133 {
134     uuid_t null_uuid = { 0 };
135     return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0;
136 }
137
138 void 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
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;
168
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;
195
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;
207
208     Error *migration_blocker;
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  */
214 static 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
221 static 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
245 static 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)
271 static 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
303 static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
304 {
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");
311
312     bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
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]);
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++;
327                 }
328             } else {
329                 fprintf(stderr, "ERROR: block index %" PRIu32
330                         " too large, is %" PRIu32 "\n", block, bmap_entry);
331                 res->corruptions++;
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);
339         res->corruptions++;
340     }
341
342     g_free(bmap);
343
344     return 0;
345 }
346
347 static 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
358 static 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
366 static 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
388 static int vdi_open(BlockDriverState *bs, int flags)
389 {
390     BDRVVdiState *s = bs->opaque;
391     VdiHeader header;
392     size_t bmap_size;
393
394     logout("\n");
395
396     if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) {
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
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
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;
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);
452     bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
453     if (bmap_size > 0) {
454         s->bmap = g_malloc(bmap_size * SECTOR_SIZE);
455     }
456     if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
457         goto fail_free_bmap;
458     }
459
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
466     return 0;
467
468  fail_free_bmap:
469     g_free(s->bmap);
470
471  fail:
472     return -1;
473 }
474
475 static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs,
476         int64_t sector_num, int nb_sectors, int *pnum)
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;
489     return VDI_IS_ALLOCATED(bmap_entry);
490 }
491
492 static void vdi_aio_cancel(BlockDriverAIOCB *blockacb)
493 {
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);
499     }
500     qemu_aio_release(acb);
501 }
502
503 static AIOPool vdi_aio_pool = {
504     .aiocb_size = sizeof(VdiAIOCB),
505     .cancel = vdi_aio_cancel,
506 };
507
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)
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);
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);
528         }
529     } else {
530         acb->buf = (uint8_t *)qiov->iov->iov_base;
531     }
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;
538     return acb;
539 }
540
541 static 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
559 static void vdi_aio_read_cb(void *opaque, int ret);
560 static void vdi_aio_write_cb(void *opaque, int ret);
561
562 static void vdi_aio_rw_bh(void *opaque)
563 {
564     VdiAIOCB *acb = opaque;
565     logout("\n");
566     qemu_bh_delete(acb->bh);
567     acb->bh = NULL;
568
569     if (acb->is_write) {
570         vdi_aio_write_cb(opaque, 0);
571     } else {
572         vdi_aio_read_cb(opaque, 0);
573     }
574 }
575
576 static 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]);
618     if (!VDI_IS_ALLOCATED(bmap_entry)) {
619         /* Block not allocated, return zeros, no need to wait. */
620         memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
621         ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
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);
632         acb->hd_aiocb = bdrv_aio_readv(bs->file, offset, &acb->hd_qiov,
633                                        n_sectors, vdi_aio_read_cb, acb);
634     }
635     return;
636 done:
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
645 static 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;
650     int ret;
651
652     logout("\n");
653     acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
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
663     return &acb->common;
664 }
665
666 static 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");
692             assert(VDI_IS_ALLOCATED(acb->bmap_first));
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);
699             acb->hd_aiocb = bdrv_aio_writev(bs->file, 0, &acb->hd_qiov, 1,
700                                             vdi_aio_write_cb, acb);
701             return;
702         } else if (VDI_IS_ALLOCATED(acb->bmap_first)) {
703             /* One or more new blocks were allocated. */
704             uint64_t offset;
705             uint32_t bmap_first;
706             uint32_t bmap_last;
707             g_free(acb->block_buffer);
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;
719             acb->hd_iov.iov_base = (void *)((uint8_t *)&s->bmap[0] +
720                                             bmap_first * SECTOR_SIZE);
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);
725             acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
726                                             n_sectors, vdi_aio_write_cb, acb);
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]);
748     if (!VDI_IS_ALLOCATED(bmap_entry)) {
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) {
759             block = g_malloc0(s->block_size);
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;
766         memcpy(block + sector_in_block * SECTOR_SIZE,
767                acb->buf, n_sectors * SECTOR_SIZE);
768         acb->hd_iov.iov_base = (void *)block;
769         acb->hd_iov.iov_len = s->block_size;
770         qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
771         acb->hd_aiocb = bdrv_aio_writev(bs->file, offset,
772                                         &acb->hd_qiov, s->block_sectors,
773                                         vdi_aio_write_cb, acb);
774     } else {
775         uint64_t offset = s->header.offset_data / SECTOR_SIZE +
776                           (uint64_t)bmap_entry * s->block_sectors +
777                           sector_in_block;
778         acb->hd_iov.iov_base = (void *)acb->buf;
779         acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
780         qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
781         acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
782                                         n_sectors, vdi_aio_write_cb, acb);
783     }
784
785     return;
786
787 done:
788     if (acb->qiov->niov > 1) {
789         qemu_vfree(acb->orig_buf);
790     }
791     acb->common.cb(acb->common.opaque, ret);
792     qemu_aio_release(acb);
793 }
794
795 static BlockDriverAIOCB *vdi_aio_writev(BlockDriverState *bs,
796         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
797         BlockDriverCompletionFunc *cb, void *opaque)
798 {
799     VdiAIOCB *acb;
800     int ret;
801
802     logout("\n");
803     acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
804     ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
805     if (ret < 0) {
806         if (acb->qiov->niov > 1) {
807             qemu_vfree(acb->orig_buf);
808         }
809         qemu_aio_release(acb);
810         return NULL;
811     }
812
813     return &acb->common;
814 }
815
816 static int vdi_create(const char *filename, QEMUOptionParameter *options)
817 {
818     int fd;
819     int result = 0;
820     uint64_t bytes = 0;
821     uint32_t blocks;
822     size_t block_size = DEFAULT_CLUSTER_SIZE;
823     uint32_t image_type = VDI_TYPE_DYNAMIC;
824     VdiHeader header;
825     size_t i;
826     size_t bmap_size;
827     uint32_t *bmap;
828
829     logout("\n");
830
831     /* Read out options. */
832     while (options && options->name) {
833         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
834             bytes = options->value.n;
835 #if defined(CONFIG_VDI_BLOCK_SIZE)
836         } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
837             if (options->value.n) {
838                 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
839                 block_size = options->value.n;
840             }
841 #endif
842 #if defined(CONFIG_VDI_STATIC_IMAGE)
843         } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
844             if (options->value.n) {
845                 image_type = VDI_TYPE_STATIC;
846             }
847 #endif
848         }
849         options++;
850     }
851
852     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
853               0644);
854     if (fd < 0) {
855         return -errno;
856     }
857
858     /* We need enough blocks to store the given disk size,
859        so always round up. */
860     blocks = (bytes + block_size - 1) / block_size;
861
862     bmap_size = blocks * sizeof(uint32_t);
863     bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
864
865     memset(&header, 0, sizeof(header));
866     pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
867     header.signature = VDI_SIGNATURE;
868     header.version = VDI_VERSION_1_1;
869     header.header_size = 0x180;
870     header.image_type = image_type;
871     header.offset_bmap = 0x200;
872     header.offset_data = 0x200 + bmap_size;
873     header.sector_size = SECTOR_SIZE;
874     header.disk_size = bytes;
875     header.block_size = block_size;
876     header.blocks_in_image = blocks;
877     if (image_type == VDI_TYPE_STATIC) {
878         header.blocks_allocated = blocks;
879     }
880     uuid_generate(header.uuid_image);
881     uuid_generate(header.uuid_last_snap);
882     /* There is no need to set header.uuid_link or header.uuid_parent here. */
883 #if defined(CONFIG_VDI_DEBUG)
884     vdi_header_print(&header);
885 #endif
886     vdi_header_to_le(&header);
887     if (write(fd, &header, sizeof(header)) < 0) {
888         result = -errno;
889     }
890
891     bmap = NULL;
892     if (bmap_size > 0) {
893         bmap = (uint32_t *)g_malloc0(bmap_size);
894     }
895     for (i = 0; i < blocks; i++) {
896         if (image_type == VDI_TYPE_STATIC) {
897             bmap[i] = i;
898         } else {
899             bmap[i] = VDI_UNALLOCATED;
900         }
901     }
902     if (write(fd, bmap, bmap_size) < 0) {
903         result = -errno;
904     }
905     g_free(bmap);
906     if (image_type == VDI_TYPE_STATIC) {
907         if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
908             result = -errno;
909         }
910     }
911
912     if (close(fd) < 0) {
913         result = -errno;
914     }
915
916     return result;
917 }
918
919 static void vdi_close(BlockDriverState *bs)
920 {
921     BDRVVdiState *s = bs->opaque;
922
923     g_free(s->bmap);
924
925     migrate_del_blocker(s->migration_blocker);
926     error_free(s->migration_blocker);
927 }
928
929 static coroutine_fn int vdi_co_flush(BlockDriverState *bs)
930 {
931     logout("\n");
932     return bdrv_co_flush(bs->file);
933 }
934
935
936 static QEMUOptionParameter vdi_create_options[] = {
937     {
938         .name = BLOCK_OPT_SIZE,
939         .type = OPT_SIZE,
940         .help = "Virtual disk size"
941     },
942 #if defined(CONFIG_VDI_BLOCK_SIZE)
943     {
944         .name = BLOCK_OPT_CLUSTER_SIZE,
945         .type = OPT_SIZE,
946         .help = "VDI cluster (block) size",
947         .value = { .n = DEFAULT_CLUSTER_SIZE },
948     },
949 #endif
950 #if defined(CONFIG_VDI_STATIC_IMAGE)
951     {
952         .name = BLOCK_OPT_STATIC,
953         .type = OPT_FLAG,
954         .help = "VDI static (pre-allocated) image"
955     },
956 #endif
957     /* TODO: An additional option to set UUID values might be useful. */
958     { NULL }
959 };
960
961 static BlockDriver bdrv_vdi = {
962     .format_name = "vdi",
963     .instance_size = sizeof(BDRVVdiState),
964     .bdrv_probe = vdi_probe,
965     .bdrv_open = vdi_open,
966     .bdrv_close = vdi_close,
967     .bdrv_create = vdi_create,
968     .bdrv_co_flush_to_disk = vdi_co_flush,
969     .bdrv_co_is_allocated = vdi_co_is_allocated,
970     .bdrv_make_empty = vdi_make_empty,
971
972     .bdrv_aio_readv = vdi_aio_readv,
973 #if defined(CONFIG_VDI_WRITE)
974     .bdrv_aio_writev = vdi_aio_writev,
975 #endif
976
977     .bdrv_get_info = vdi_get_info,
978
979     .create_options = vdi_create_options,
980     .bdrv_check = vdi_check,
981 };
982
983 static void bdrv_vdi_init(void)
984 {
985     logout("\n");
986     bdrv_register(&bdrv_vdi);
987 }
988
989 block_init(bdrv_vdi_init);