xref: /openbmc/qemu/block/vdi.c (revision 2345c77c)
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 
56 #if defined(CONFIG_UUID)
57 #include <uuid/uuid.h>
58 #else
59 /* TODO: move uuid emulation to some central place in QEMU. */
60 #include "sysemu.h"     /* UUID_FMT */
61 typedef unsigned char uuid_t[16];
62 void uuid_generate(uuid_t out);
63 int uuid_is_null(const uuid_t uu);
64 void uuid_unparse(const uuid_t uu, char *out);
65 #endif
66 
67 /* Code configuration options. */
68 
69 /* Enable debug messages. */
70 //~ #define CONFIG_VDI_DEBUG
71 
72 /* Support write operations on VDI images. */
73 #define CONFIG_VDI_WRITE
74 
75 /* Support non-standard block (cluster) size. This is untested.
76  * Maybe it will be needed for very large images.
77  */
78 //~ #define CONFIG_VDI_BLOCK_SIZE
79 
80 /* Support static (fixed, pre-allocated) images. */
81 #define CONFIG_VDI_STATIC_IMAGE
82 
83 /* Command line option for static images. */
84 #define BLOCK_OPT_STATIC "static"
85 
86 #define KiB     1024
87 #define MiB     (KiB * KiB)
88 
89 #define SECTOR_SIZE 512
90 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
91 
92 #if defined(CONFIG_VDI_DEBUG)
93 #define logout(fmt, ...) \
94                 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
95 #else
96 #define logout(fmt, ...) ((void)0)
97 #endif
98 
99 /* Image signature. */
100 #define VDI_SIGNATURE 0xbeda107f
101 
102 /* Image version. */
103 #define VDI_VERSION_1_1 0x00010001
104 
105 /* Image type. */
106 #define VDI_TYPE_DYNAMIC 1
107 #define VDI_TYPE_STATIC  2
108 
109 /* Innotek / SUN images use these strings in header.text:
110  * "<<< innotek VirtualBox Disk Image >>>\n"
111  * "<<< Sun xVM VirtualBox Disk Image >>>\n"
112  * "<<< Sun VirtualBox Disk Image >>>\n"
113  * The value does not matter, so QEMU created images use a different text.
114  */
115 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
116 
117 /* Unallocated blocks use this index (no need to convert endianness). */
118 #define VDI_UNALLOCATED UINT32_MAX
119 
120 #if !defined(CONFIG_UUID)
121 void uuid_generate(uuid_t out)
122 {
123     memset(out, 0, sizeof(uuid_t));
124 }
125 
126 int uuid_is_null(const uuid_t uu)
127 {
128     uuid_t null_uuid = { 0 };
129     return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0;
130 }
131 
132 void uuid_unparse(const uuid_t uu, char *out)
133 {
134     snprintf(out, 37, UUID_FMT,
135             uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7],
136             uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
137 }
138 #endif
139 
140 typedef struct {
141     BlockDriverAIOCB common;
142     int64_t sector_num;
143     QEMUIOVector *qiov;
144     uint8_t *buf;
145     /* Total number of sectors. */
146     int nb_sectors;
147     /* Number of sectors for current AIO. */
148     int n_sectors;
149     /* New allocated block map entry. */
150     uint32_t bmap_first;
151     uint32_t bmap_last;
152     /* Buffer for new allocated block. */
153     void *block_buffer;
154     void *orig_buf;
155     bool is_write;
156     int header_modified;
157     BlockDriverAIOCB *hd_aiocb;
158     struct iovec hd_iov;
159     QEMUIOVector hd_qiov;
160     QEMUBH *bh;
161 } VdiAIOCB;
162 
163 typedef struct {
164     char text[0x40];
165     uint32_t signature;
166     uint32_t version;
167     uint32_t header_size;
168     uint32_t image_type;
169     uint32_t image_flags;
170     char description[256];
171     uint32_t offset_bmap;
172     uint32_t offset_data;
173     uint32_t cylinders;         /* disk geometry, unused here */
174     uint32_t heads;             /* disk geometry, unused here */
175     uint32_t sectors;           /* disk geometry, unused here */
176     uint32_t sector_size;
177     uint32_t unused1;
178     uint64_t disk_size;
179     uint32_t block_size;
180     uint32_t block_extra;       /* unused here */
181     uint32_t blocks_in_image;
182     uint32_t blocks_allocated;
183     uuid_t uuid_image;
184     uuid_t uuid_last_snap;
185     uuid_t uuid_link;
186     uuid_t uuid_parent;
187     uint64_t unused2[7];
188 } VdiHeader;
189 
190 typedef struct {
191     /* The block map entries are little endian (even in memory). */
192     uint32_t *bmap;
193     /* Size of block (bytes). */
194     uint32_t block_size;
195     /* Size of block (sectors). */
196     uint32_t block_sectors;
197     /* First sector of block map. */
198     uint32_t bmap_sector;
199     /* VDI header (converted to host endianness). */
200     VdiHeader header;
201 } BDRVVdiState;
202 
203 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
204  * format (network byte order, standard, see RFC 4122) and vice versa.
205  */
206 static void uuid_convert(uuid_t uuid)
207 {
208     bswap32s((uint32_t *)&uuid[0]);
209     bswap16s((uint16_t *)&uuid[4]);
210     bswap16s((uint16_t *)&uuid[6]);
211 }
212 
213 static void vdi_header_to_cpu(VdiHeader *header)
214 {
215     le32_to_cpus(&header->signature);
216     le32_to_cpus(&header->version);
217     le32_to_cpus(&header->header_size);
218     le32_to_cpus(&header->image_type);
219     le32_to_cpus(&header->image_flags);
220     le32_to_cpus(&header->offset_bmap);
221     le32_to_cpus(&header->offset_data);
222     le32_to_cpus(&header->cylinders);
223     le32_to_cpus(&header->heads);
224     le32_to_cpus(&header->sectors);
225     le32_to_cpus(&header->sector_size);
226     le64_to_cpus(&header->disk_size);
227     le32_to_cpus(&header->block_size);
228     le32_to_cpus(&header->block_extra);
229     le32_to_cpus(&header->blocks_in_image);
230     le32_to_cpus(&header->blocks_allocated);
231     uuid_convert(header->uuid_image);
232     uuid_convert(header->uuid_last_snap);
233     uuid_convert(header->uuid_link);
234     uuid_convert(header->uuid_parent);
235 }
236 
237 static void vdi_header_to_le(VdiHeader *header)
238 {
239     cpu_to_le32s(&header->signature);
240     cpu_to_le32s(&header->version);
241     cpu_to_le32s(&header->header_size);
242     cpu_to_le32s(&header->image_type);
243     cpu_to_le32s(&header->image_flags);
244     cpu_to_le32s(&header->offset_bmap);
245     cpu_to_le32s(&header->offset_data);
246     cpu_to_le32s(&header->cylinders);
247     cpu_to_le32s(&header->heads);
248     cpu_to_le32s(&header->sectors);
249     cpu_to_le32s(&header->sector_size);
250     cpu_to_le64s(&header->disk_size);
251     cpu_to_le32s(&header->block_size);
252     cpu_to_le32s(&header->block_extra);
253     cpu_to_le32s(&header->blocks_in_image);
254     cpu_to_le32s(&header->blocks_allocated);
255     cpu_to_le32s(&header->blocks_allocated);
256     uuid_convert(header->uuid_image);
257     uuid_convert(header->uuid_last_snap);
258     uuid_convert(header->uuid_link);
259     uuid_convert(header->uuid_parent);
260 }
261 
262 #if defined(CONFIG_VDI_DEBUG)
263 static void vdi_header_print(VdiHeader *header)
264 {
265     char uuid[37];
266     logout("text        %s", header->text);
267     logout("signature   0x%04x\n", header->signature);
268     logout("header size 0x%04x\n", header->header_size);
269     logout("image type  0x%04x\n", header->image_type);
270     logout("image flags 0x%04x\n", header->image_flags);
271     logout("description %s\n", header->description);
272     logout("offset bmap 0x%04x\n", header->offset_bmap);
273     logout("offset data 0x%04x\n", header->offset_data);
274     logout("cylinders   0x%04x\n", header->cylinders);
275     logout("heads       0x%04x\n", header->heads);
276     logout("sectors     0x%04x\n", header->sectors);
277     logout("sector size 0x%04x\n", header->sector_size);
278     logout("image size  0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
279            header->disk_size, header->disk_size / MiB);
280     logout("block size  0x%04x\n", header->block_size);
281     logout("block extra 0x%04x\n", header->block_extra);
282     logout("blocks tot. 0x%04x\n", header->blocks_in_image);
283     logout("blocks all. 0x%04x\n", header->blocks_allocated);
284     uuid_unparse(header->uuid_image, uuid);
285     logout("uuid image  %s\n", uuid);
286     uuid_unparse(header->uuid_last_snap, uuid);
287     logout("uuid snap   %s\n", uuid);
288     uuid_unparse(header->uuid_link, uuid);
289     logout("uuid link   %s\n", uuid);
290     uuid_unparse(header->uuid_parent, uuid);
291     logout("uuid parent %s\n", uuid);
292 }
293 #endif
294 
295 static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
296 {
297     /* TODO: additional checks possible. */
298     BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
299     uint32_t blocks_allocated = 0;
300     uint32_t block;
301     uint32_t *bmap;
302     logout("\n");
303 
304     bmap = qemu_malloc(s->header.blocks_in_image * sizeof(uint32_t));
305     memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
306 
307     /* Check block map and value of blocks_allocated. */
308     for (block = 0; block < s->header.blocks_in_image; block++) {
309         uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
310         if (bmap_entry != VDI_UNALLOCATED) {
311             if (bmap_entry < s->header.blocks_in_image) {
312                 blocks_allocated++;
313                 if (bmap[bmap_entry] == VDI_UNALLOCATED) {
314                     bmap[bmap_entry] = bmap_entry;
315                 } else {
316                     fprintf(stderr, "ERROR: block index %" PRIu32
317                             " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
318                     res->corruptions++;
319                 }
320             } else {
321                 fprintf(stderr, "ERROR: block index %" PRIu32
322                         " too large, is %" PRIu32 "\n", block, bmap_entry);
323                 res->corruptions++;
324             }
325         }
326     }
327     if (blocks_allocated != s->header.blocks_allocated) {
328         fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
329                ", should be %" PRIu32 "\n",
330                blocks_allocated, s->header.blocks_allocated);
331         res->corruptions++;
332     }
333 
334     qemu_free(bmap);
335 
336     return 0;
337 }
338 
339 static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
340 {
341     /* TODO: vdi_get_info would be needed for machine snapshots.
342        vm_state_offset is still missing. */
343     BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
344     logout("\n");
345     bdi->cluster_size = s->block_size;
346     bdi->vm_state_offset = 0;
347     return 0;
348 }
349 
350 static int vdi_make_empty(BlockDriverState *bs)
351 {
352     /* TODO: missing code. */
353     logout("\n");
354     /* The return value for missing code must be 0, see block.c. */
355     return 0;
356 }
357 
358 static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
359 {
360     const VdiHeader *header = (const VdiHeader *)buf;
361     int result = 0;
362 
363     logout("\n");
364 
365     if (buf_size < sizeof(*header)) {
366         /* Header too small, no VDI. */
367     } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
368         result = 100;
369     }
370 
371     if (result == 0) {
372         logout("no vdi image\n");
373     } else {
374         logout("%s", header->text);
375     }
376 
377     return result;
378 }
379 
380 static int vdi_open(BlockDriverState *bs, int flags)
381 {
382     BDRVVdiState *s = bs->opaque;
383     VdiHeader header;
384     size_t bmap_size;
385 
386     logout("\n");
387 
388     if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) {
389         goto fail;
390     }
391 
392     vdi_header_to_cpu(&header);
393 #if defined(CONFIG_VDI_DEBUG)
394     vdi_header_print(&header);
395 #endif
396 
397     if (header.disk_size % SECTOR_SIZE != 0) {
398         /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
399            We accept them but round the disk size to the next multiple of
400            SECTOR_SIZE. */
401         logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
402         header.disk_size += SECTOR_SIZE - 1;
403         header.disk_size &= ~(SECTOR_SIZE - 1);
404     }
405 
406     if (header.version != VDI_VERSION_1_1) {
407         logout("unsupported version %u.%u\n",
408                header.version >> 16, header.version & 0xffff);
409         goto fail;
410     } else if (header.offset_bmap % SECTOR_SIZE != 0) {
411         /* We only support block maps which start on a sector boundary. */
412         logout("unsupported block map offset 0x%x B\n", header.offset_bmap);
413         goto fail;
414     } else if (header.offset_data % SECTOR_SIZE != 0) {
415         /* We only support data blocks which start on a sector boundary. */
416         logout("unsupported data offset 0x%x B\n", header.offset_data);
417         goto fail;
418     } else if (header.sector_size != SECTOR_SIZE) {
419         logout("unsupported sector size %u B\n", header.sector_size);
420         goto fail;
421     } else if (header.block_size != 1 * MiB) {
422         logout("unsupported block size %u B\n", header.block_size);
423         goto fail;
424     } else if (header.disk_size >
425                (uint64_t)header.blocks_in_image * header.block_size) {
426         logout("unsupported disk size %" PRIu64 " B\n", header.disk_size);
427         goto fail;
428     } else if (!uuid_is_null(header.uuid_link)) {
429         logout("link uuid != 0, unsupported\n");
430         goto fail;
431     } else if (!uuid_is_null(header.uuid_parent)) {
432         logout("parent uuid != 0, unsupported\n");
433         goto fail;
434     }
435 
436     bs->total_sectors = header.disk_size / SECTOR_SIZE;
437 
438     s->block_size = header.block_size;
439     s->block_sectors = header.block_size / SECTOR_SIZE;
440     s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
441     s->header = header;
442 
443     bmap_size = header.blocks_in_image * sizeof(uint32_t);
444     bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
445     if (bmap_size > 0) {
446         s->bmap = qemu_malloc(bmap_size * SECTOR_SIZE);
447     }
448     if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
449         goto fail_free_bmap;
450     }
451 
452     return 0;
453 
454  fail_free_bmap:
455     qemu_free(s->bmap);
456 
457  fail:
458     return -1;
459 }
460 
461 static int vdi_is_allocated(BlockDriverState *bs, int64_t sector_num,
462                              int nb_sectors, int *pnum)
463 {
464     /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
465     BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
466     size_t bmap_index = sector_num / s->block_sectors;
467     size_t sector_in_block = sector_num % s->block_sectors;
468     int n_sectors = s->block_sectors - sector_in_block;
469     uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
470     logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
471     if (n_sectors > nb_sectors) {
472         n_sectors = nb_sectors;
473     }
474     *pnum = n_sectors;
475     return bmap_entry != VDI_UNALLOCATED;
476 }
477 
478 static void vdi_aio_cancel(BlockDriverAIOCB *blockacb)
479 {
480     /* TODO: This code is untested. How can I get it executed? */
481     VdiAIOCB *acb = container_of(blockacb, VdiAIOCB, common);
482     logout("\n");
483     if (acb->hd_aiocb) {
484         bdrv_aio_cancel(acb->hd_aiocb);
485     }
486     qemu_aio_release(acb);
487 }
488 
489 static AIOPool vdi_aio_pool = {
490     .aiocb_size = sizeof(VdiAIOCB),
491     .cancel = vdi_aio_cancel,
492 };
493 
494 static VdiAIOCB *vdi_aio_setup(BlockDriverState *bs, int64_t sector_num,
495         QEMUIOVector *qiov, int nb_sectors,
496         BlockDriverCompletionFunc *cb, void *opaque, int is_write)
497 {
498     VdiAIOCB *acb;
499 
500     logout("%p, %" PRId64 ", %p, %d, %p, %p, %d\n",
501            bs, sector_num, qiov, nb_sectors, cb, opaque, is_write);
502 
503     acb = qemu_aio_get(&vdi_aio_pool, bs, cb, opaque);
504     if (acb) {
505         acb->hd_aiocb = NULL;
506         acb->sector_num = sector_num;
507         acb->qiov = qiov;
508         acb->is_write = is_write;
509 
510         if (qiov->niov > 1) {
511             acb->buf = qemu_blockalign(bs, qiov->size);
512             acb->orig_buf = acb->buf;
513             if (is_write) {
514                 qemu_iovec_to_buffer(qiov, acb->buf);
515             }
516         } else {
517             acb->buf = (uint8_t *)qiov->iov->iov_base;
518         }
519         acb->nb_sectors = nb_sectors;
520         acb->n_sectors = 0;
521         acb->bmap_first = VDI_UNALLOCATED;
522         acb->bmap_last = VDI_UNALLOCATED;
523         acb->block_buffer = NULL;
524         acb->header_modified = 0;
525     }
526     return acb;
527 }
528 
529 static int vdi_schedule_bh(QEMUBHFunc *cb, VdiAIOCB *acb)
530 {
531     logout("\n");
532 
533     if (acb->bh) {
534         return -EIO;
535     }
536 
537     acb->bh = qemu_bh_new(cb, acb);
538     if (!acb->bh) {
539         return -EIO;
540     }
541 
542     qemu_bh_schedule(acb->bh);
543 
544     return 0;
545 }
546 
547 static void vdi_aio_read_cb(void *opaque, int ret);
548 static void vdi_aio_write_cb(void *opaque, int ret);
549 
550 static void vdi_aio_rw_bh(void *opaque)
551 {
552     VdiAIOCB *acb = opaque;
553     logout("\n");
554     qemu_bh_delete(acb->bh);
555     acb->bh = NULL;
556 
557     if (acb->is_write) {
558         vdi_aio_write_cb(opaque, 0);
559     } else {
560         vdi_aio_read_cb(opaque, 0);
561     }
562 }
563 
564 static void vdi_aio_read_cb(void *opaque, int ret)
565 {
566     VdiAIOCB *acb = opaque;
567     BlockDriverState *bs = acb->common.bs;
568     BDRVVdiState *s = bs->opaque;
569     uint32_t bmap_entry;
570     uint32_t block_index;
571     uint32_t sector_in_block;
572     uint32_t n_sectors;
573 
574     logout("%u sectors read\n", acb->n_sectors);
575 
576     acb->hd_aiocb = NULL;
577 
578     if (ret < 0) {
579         goto done;
580     }
581 
582     acb->nb_sectors -= acb->n_sectors;
583 
584     if (acb->nb_sectors == 0) {
585         /* request completed */
586         ret = 0;
587         goto done;
588     }
589 
590     acb->sector_num += acb->n_sectors;
591     acb->buf += acb->n_sectors * SECTOR_SIZE;
592 
593     block_index = acb->sector_num / s->block_sectors;
594     sector_in_block = acb->sector_num % s->block_sectors;
595     n_sectors = s->block_sectors - sector_in_block;
596     if (n_sectors > acb->nb_sectors) {
597         n_sectors = acb->nb_sectors;
598     }
599 
600     logout("will read %u sectors starting at sector %" PRIu64 "\n",
601            n_sectors, acb->sector_num);
602 
603     /* prepare next AIO request */
604     acb->n_sectors = n_sectors;
605     bmap_entry = le32_to_cpu(s->bmap[block_index]);
606     if (bmap_entry == VDI_UNALLOCATED) {
607         /* Block not allocated, return zeros, no need to wait. */
608         memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
609         ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
610         if (ret < 0) {
611             goto done;
612         }
613     } else {
614         uint64_t offset = s->header.offset_data / SECTOR_SIZE +
615                           (uint64_t)bmap_entry * s->block_sectors +
616                           sector_in_block;
617         acb->hd_iov.iov_base = (void *)acb->buf;
618         acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
619         qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
620         acb->hd_aiocb = bdrv_aio_readv(bs->file, offset, &acb->hd_qiov,
621                                        n_sectors, vdi_aio_read_cb, acb);
622         if (acb->hd_aiocb == NULL) {
623             ret = -EIO;
624             goto done;
625         }
626     }
627     return;
628 done:
629     if (acb->qiov->niov > 1) {
630         qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
631         qemu_vfree(acb->orig_buf);
632     }
633     acb->common.cb(acb->common.opaque, ret);
634     qemu_aio_release(acb);
635 }
636 
637 static BlockDriverAIOCB *vdi_aio_readv(BlockDriverState *bs,
638         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
639         BlockDriverCompletionFunc *cb, void *opaque)
640 {
641     VdiAIOCB *acb;
642     int ret;
643 
644     logout("\n");
645     acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
646     if (!acb) {
647         return NULL;
648     }
649 
650     ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
651     if (ret < 0) {
652         if (acb->qiov->niov > 1) {
653             qemu_vfree(acb->orig_buf);
654         }
655         qemu_aio_release(acb);
656         return NULL;
657     }
658 
659     return &acb->common;
660 }
661 
662 static void vdi_aio_write_cb(void *opaque, int ret)
663 {
664     VdiAIOCB *acb = opaque;
665     BlockDriverState *bs = acb->common.bs;
666     BDRVVdiState *s = bs->opaque;
667     uint32_t bmap_entry;
668     uint32_t block_index;
669     uint32_t sector_in_block;
670     uint32_t n_sectors;
671 
672     acb->hd_aiocb = NULL;
673 
674     if (ret < 0) {
675         goto done;
676     }
677 
678     acb->nb_sectors -= acb->n_sectors;
679     acb->sector_num += acb->n_sectors;
680     acb->buf += acb->n_sectors * SECTOR_SIZE;
681 
682     if (acb->nb_sectors == 0) {
683         logout("finished data write\n");
684         acb->n_sectors = 0;
685         if (acb->header_modified) {
686             VdiHeader *header = acb->block_buffer;
687             logout("now writing modified header\n");
688             assert(acb->bmap_first != VDI_UNALLOCATED);
689             *header = s->header;
690             vdi_header_to_le(header);
691             acb->header_modified = 0;
692             acb->hd_iov.iov_base = acb->block_buffer;
693             acb->hd_iov.iov_len = SECTOR_SIZE;
694             qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
695             acb->hd_aiocb = bdrv_aio_writev(bs->file, 0, &acb->hd_qiov, 1,
696                                             vdi_aio_write_cb, acb);
697             if (acb->hd_aiocb == NULL) {
698                 ret = -EIO;
699                 goto done;
700             }
701             return;
702         } else if (acb->bmap_first != VDI_UNALLOCATED) {
703             /* One or more new blocks were allocated. */
704             uint64_t offset;
705             uint32_t bmap_first;
706             uint32_t bmap_last;
707             qemu_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             if (acb->hd_aiocb == NULL) {
728                 ret = -EIO;
729                 goto done;
730             }
731             return;
732         }
733         ret = 0;
734         goto done;
735     }
736 
737     logout("%u sectors written\n", acb->n_sectors);
738 
739     block_index = acb->sector_num / s->block_sectors;
740     sector_in_block = acb->sector_num % s->block_sectors;
741     n_sectors = s->block_sectors - sector_in_block;
742     if (n_sectors > acb->nb_sectors) {
743         n_sectors = acb->nb_sectors;
744     }
745 
746     logout("will write %u sectors starting at sector %" PRIu64 "\n",
747            n_sectors, acb->sector_num);
748 
749     /* prepare next AIO request */
750     acb->n_sectors = n_sectors;
751     bmap_entry = le32_to_cpu(s->bmap[block_index]);
752     if (bmap_entry == VDI_UNALLOCATED) {
753         /* Allocate new block and write to it. */
754         uint64_t offset;
755         uint8_t *block;
756         bmap_entry = s->header.blocks_allocated;
757         s->bmap[block_index] = cpu_to_le32(bmap_entry);
758         s->header.blocks_allocated++;
759         offset = s->header.offset_data / SECTOR_SIZE +
760                  (uint64_t)bmap_entry * s->block_sectors;
761         block = acb->block_buffer;
762         if (block == NULL) {
763             block = qemu_mallocz(s->block_size);
764             acb->block_buffer = block;
765             acb->bmap_first = block_index;
766             assert(!acb->header_modified);
767             acb->header_modified = 1;
768         }
769         acb->bmap_last = block_index;
770         memcpy(block + sector_in_block * SECTOR_SIZE,
771                acb->buf, n_sectors * SECTOR_SIZE);
772         acb->hd_iov.iov_base = (void *)block;
773         acb->hd_iov.iov_len = s->block_size;
774         qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
775         acb->hd_aiocb = bdrv_aio_writev(bs->file, offset,
776                                         &acb->hd_qiov, s->block_sectors,
777                                         vdi_aio_write_cb, acb);
778         if (acb->hd_aiocb == NULL) {
779             ret = -EIO;
780             goto done;
781         }
782     } else {
783         uint64_t offset = s->header.offset_data / SECTOR_SIZE +
784                           (uint64_t)bmap_entry * s->block_sectors +
785                           sector_in_block;
786         acb->hd_iov.iov_base = (void *)acb->buf;
787         acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
788         qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
789         acb->hd_aiocb = bdrv_aio_writev(bs->file, offset, &acb->hd_qiov,
790                                         n_sectors, vdi_aio_write_cb, acb);
791         if (acb->hd_aiocb == NULL) {
792             ret = -EIO;
793             goto done;
794         }
795     }
796 
797     return;
798 
799 done:
800     if (acb->qiov->niov > 1) {
801         qemu_vfree(acb->orig_buf);
802     }
803     acb->common.cb(acb->common.opaque, ret);
804     qemu_aio_release(acb);
805 }
806 
807 static BlockDriverAIOCB *vdi_aio_writev(BlockDriverState *bs,
808         int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
809         BlockDriverCompletionFunc *cb, void *opaque)
810 {
811     VdiAIOCB *acb;
812     int ret;
813 
814     logout("\n");
815     acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
816     if (!acb) {
817         return NULL;
818     }
819 
820     ret = vdi_schedule_bh(vdi_aio_rw_bh, acb);
821     if (ret < 0) {
822         if (acb->qiov->niov > 1) {
823             qemu_vfree(acb->orig_buf);
824         }
825         qemu_aio_release(acb);
826         return NULL;
827     }
828 
829     return &acb->common;
830 }
831 
832 static int vdi_create(const char *filename, QEMUOptionParameter *options)
833 {
834     int fd;
835     int result = 0;
836     uint64_t bytes = 0;
837     uint32_t blocks;
838     size_t block_size = DEFAULT_CLUSTER_SIZE;
839     uint32_t image_type = VDI_TYPE_DYNAMIC;
840     VdiHeader header;
841     size_t i;
842     size_t bmap_size;
843     uint32_t *bmap;
844 
845     logout("\n");
846 
847     /* Read out options. */
848     while (options && options->name) {
849         if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
850             bytes = options->value.n;
851 #if defined(CONFIG_VDI_BLOCK_SIZE)
852         } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
853             if (options->value.n) {
854                 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
855                 block_size = options->value.n;
856             }
857 #endif
858 #if defined(CONFIG_VDI_STATIC_IMAGE)
859         } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
860             if (options->value.n) {
861                 image_type = VDI_TYPE_STATIC;
862             }
863 #endif
864         }
865         options++;
866     }
867 
868     fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
869               0644);
870     if (fd < 0) {
871         return -errno;
872     }
873 
874     /* We need enough blocks to store the given disk size,
875        so always round up. */
876     blocks = (bytes + block_size - 1) / block_size;
877 
878     bmap_size = blocks * sizeof(uint32_t);
879     bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
880 
881     memset(&header, 0, sizeof(header));
882     pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
883     header.signature = VDI_SIGNATURE;
884     header.version = VDI_VERSION_1_1;
885     header.header_size = 0x180;
886     header.image_type = image_type;
887     header.offset_bmap = 0x200;
888     header.offset_data = 0x200 + bmap_size;
889     header.sector_size = SECTOR_SIZE;
890     header.disk_size = bytes;
891     header.block_size = block_size;
892     header.blocks_in_image = blocks;
893     if (image_type == VDI_TYPE_STATIC) {
894         header.blocks_allocated = blocks;
895     }
896     uuid_generate(header.uuid_image);
897     uuid_generate(header.uuid_last_snap);
898     /* There is no need to set header.uuid_link or header.uuid_parent here. */
899 #if defined(CONFIG_VDI_DEBUG)
900     vdi_header_print(&header);
901 #endif
902     vdi_header_to_le(&header);
903     if (write(fd, &header, sizeof(header)) < 0) {
904         result = -errno;
905     }
906 
907     bmap = NULL;
908     if (bmap_size > 0) {
909         bmap = (uint32_t *)qemu_mallocz(bmap_size);
910     }
911     for (i = 0; i < blocks; i++) {
912         if (image_type == VDI_TYPE_STATIC) {
913             bmap[i] = i;
914         } else {
915             bmap[i] = VDI_UNALLOCATED;
916         }
917     }
918     if (write(fd, bmap, bmap_size) < 0) {
919         result = -errno;
920     }
921     qemu_free(bmap);
922     if (image_type == VDI_TYPE_STATIC) {
923         if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
924             result = -errno;
925         }
926     }
927 
928     if (close(fd) < 0) {
929         result = -errno;
930     }
931 
932     return result;
933 }
934 
935 static void vdi_close(BlockDriverState *bs)
936 {
937 }
938 
939 static int vdi_flush(BlockDriverState *bs)
940 {
941     logout("\n");
942     return bdrv_flush(bs->file);
943 }
944 
945 
946 static QEMUOptionParameter vdi_create_options[] = {
947     {
948         .name = BLOCK_OPT_SIZE,
949         .type = OPT_SIZE,
950         .help = "Virtual disk size"
951     },
952 #if defined(CONFIG_VDI_BLOCK_SIZE)
953     {
954         .name = BLOCK_OPT_CLUSTER_SIZE,
955         .type = OPT_SIZE,
956         .help = "VDI cluster (block) size",
957         .value = { .n = DEFAULT_CLUSTER_SIZE },
958     },
959 #endif
960 #if defined(CONFIG_VDI_STATIC_IMAGE)
961     {
962         .name = BLOCK_OPT_STATIC,
963         .type = OPT_FLAG,
964         .help = "VDI static (pre-allocated) image"
965     },
966 #endif
967     /* TODO: An additional option to set UUID values might be useful. */
968     { NULL }
969 };
970 
971 static BlockDriver bdrv_vdi = {
972     .format_name = "vdi",
973     .instance_size = sizeof(BDRVVdiState),
974     .bdrv_probe = vdi_probe,
975     .bdrv_open = vdi_open,
976     .bdrv_close = vdi_close,
977     .bdrv_create = vdi_create,
978     .bdrv_flush = vdi_flush,
979     .bdrv_is_allocated = vdi_is_allocated,
980     .bdrv_make_empty = vdi_make_empty,
981 
982     .bdrv_aio_readv = vdi_aio_readv,
983 #if defined(CONFIG_VDI_WRITE)
984     .bdrv_aio_writev = vdi_aio_writev,
985 #endif
986 
987     .bdrv_get_info = vdi_get_info,
988 
989     .create_options = vdi_create_options,
990     .bdrv_check = vdi_check,
991 };
992 
993 static void bdrv_vdi_init(void)
994 {
995     logout("\n");
996     bdrv_register(&bdrv_vdi);
997 }
998 
999 block_init(bdrv_vdi_init);
1000