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