2 * Block driver for the Virtual Disk Image (VDI) format
4 * Copyright (c) 2009 Stefan Weil
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.
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.
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/>.
20 * http://forums.virtualbox.org/viewtopic.php?t=8046
22 * This driver supports create / read / write operations on VDI images.
24 * Todo (see also TODO in code):
26 * Some features like snapshots are still missing.
28 * Deallocation of zero-filled blocks and shrinking images are missing, too
29 * (might be added to common block layer).
31 * Allocation of blocks could be optimized (less writes to block map and
34 * Read and write of adjacents blocks could be done in one operation
35 * (current code uses one operation per block (1 MiB).
37 * The code is not thread safe (missing locks for changes in header and
38 * block table, no problem with current QEMU).
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.
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.
52 #include "qemu-common.h"
53 #include "block_int.h"
56 #if defined(CONFIG_UUID)
57 #include <uuid/uuid.h>
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
);
67 /* Code configuration options. */
69 /* Enable debug messages. */
70 //~ #define CONFIG_VDI_DEBUG
72 /* Support write operations on VDI images. */
73 #define CONFIG_VDI_WRITE
75 /* Support non-standard block (cluster) size. This is untested.
76 * Maybe it will be needed for very large images.
78 //~ #define CONFIG_VDI_BLOCK_SIZE
80 /* Support static (fixed, pre-allocated) images. */
81 #define CONFIG_VDI_STATIC_IMAGE
83 /* Command line option for static images. */
84 #define BLOCK_OPT_STATIC "static"
87 #define MiB (KiB * KiB)
89 #define SECTOR_SIZE 512
91 #if defined(CONFIG_VDI_DEBUG)
92 #define logout(fmt, ...) \
93 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
95 #define logout(fmt, ...) ((void)0)
98 /* Image signature. */
99 #define VDI_SIGNATURE 0xbeda107f
102 #define VDI_VERSION_1_1 0x00010001
105 #define VDI_TYPE_DYNAMIC 1
106 #define VDI_TYPE_STATIC 2
108 /* Innotek / SUN images use these strings in header.text:
109 * "<<< innotek VirtualBox Disk Image >>>\n"
110 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
111 * "<<< Sun VirtualBox Disk Image >>>\n"
112 * The value does not matter, so QEMU created images use a different text.
114 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
116 /* Unallocated blocks use this index (no need to convert endianess). */
117 #define VDI_UNALLOCATED UINT32_MAX
119 #if !defined(CONFIG_UUID)
120 void uuid_generate(uuid_t out
)
122 memset(out
, 0, sizeof(out
));
125 int uuid_is_null(const uuid_t uu
)
127 uuid_t null_uuid
= { 0 };
128 return memcmp(uu
, null_uuid
, sizeof(uu
)) == 0;
131 void uuid_unparse(const uuid_t uu
, char *out
)
133 snprintf(out
, 37, UUID_FMT
,
134 uu
[0], uu
[1], uu
[2], uu
[3], uu
[4], uu
[5], uu
[6], uu
[7],
135 uu
[8], uu
[9], uu
[10], uu
[11], uu
[12], uu
[13], uu
[14], uu
[15]);
140 BlockDriverAIOCB common
;
144 /* Total number of sectors. */
146 /* Number of sectors for current AIO. */
148 /* New allocated block map entry. */
151 /* Buffer for new allocated block. */
155 BlockDriverAIOCB
*hd_aiocb
;
157 QEMUIOVector hd_qiov
;
165 uint32_t header_size
;
167 uint32_t image_flags
;
168 char description
[256];
169 uint32_t offset_bmap
;
170 uint32_t offset_data
;
171 uint32_t cylinders
; /* disk geometry, unused here */
172 uint32_t heads
; /* disk geometry, unused here */
173 uint32_t sectors
; /* disk geometry, unused here */
174 uint32_t sector_size
;
178 uint32_t block_extra
; /* unused here */
179 uint32_t blocks_in_image
;
180 uint32_t blocks_allocated
;
182 uuid_t uuid_last_snap
;
189 BlockDriverState
*hd
;
190 /* The block map entries are little endian (even in memory). */
192 /* Size of block (bytes). */
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 endianess). */
202 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
203 * format (network byte order, standard, see RFC 4122) and vice versa.
205 static void uuid_convert(uuid_t uuid
)
207 bswap32s((uint32_t *)&uuid
[0]);
208 bswap16s((uint16_t *)&uuid
[4]);
209 bswap16s((uint16_t *)&uuid
[6]);
212 static void vdi_header_to_cpu(VdiHeader
*header
)
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
);
236 static void vdi_header_to_le(VdiHeader
*header
)
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
);
261 #if defined(CONFIG_VDI_DEBUG)
262 static void vdi_header_print(VdiHeader
*header
)
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
);
294 static int vdi_check(BlockDriverState
*bs
)
296 /* TODO: additional checks possible. */
297 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
299 uint32_t blocks_allocated
= 0;
304 bmap
= qemu_malloc(s
->header
.blocks_in_image
* sizeof(uint32_t));
305 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
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
) {
313 if (bmap
[bmap_entry
] == VDI_UNALLOCATED
) {
314 bmap
[bmap_entry
] = bmap_entry
;
316 fprintf(stderr
, "ERROR: block index %" PRIu32
317 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
320 fprintf(stderr
, "ERROR: block index %" PRIu32
321 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
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
);
338 static int vdi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
340 /* TODO: vdi_get_info would be needed for machine snapshots.
341 vm_state_offset is still missing. */
342 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
344 bdi
->cluster_size
= s
->block_size
;
345 bdi
->vm_state_offset
= 0;
349 static int vdi_make_empty(BlockDriverState
*bs
)
351 /* TODO: missing code. */
353 /* The return value for missing code must be 0, see block.c. */
357 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
359 const VdiHeader
*header
= (const VdiHeader
*)buf
;
364 if (buf_size
< sizeof(*header
)) {
365 /* Header too small, no VDI. */
366 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
371 logout("no vdi image\n");
373 logout("%s", header
->text
);
379 static int vdi_open(BlockDriverState
*bs
, const char *filename
, int flags
)
381 BDRVVdiState
*s
= bs
->opaque
;
388 ret
= bdrv_file_open(&s
->hd
, filename
, flags
);
393 if (bdrv_read(s
->hd
, 0, (uint8_t *)&header
, 1) < 0) {
397 vdi_header_to_cpu(&header
);
398 #if defined(CONFIG_VDI_DEBUG)
399 vdi_header_print(&header
);
402 if (header
.version
!= VDI_VERSION_1_1
) {
403 logout("unsupported version %u.%u\n",
404 header
.version
>> 16, header
.version
& 0xffff);
406 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
407 /* We only support block maps which start on a sector boundary. */
408 logout("unsupported block map offset 0x%x B\n", header
.offset_bmap
);
410 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
411 /* We only support data blocks which start on a sector boundary. */
412 logout("unsupported data offset 0x%x B\n", header
.offset_data
);
414 } else if (header
.disk_size
% SECTOR_SIZE
!= 0) {
415 logout("unsupported disk size %" PRIu64
" B\n", header
.disk_size
);
417 } else if (header
.sector_size
!= SECTOR_SIZE
) {
418 logout("unsupported sector size %u B\n", header
.sector_size
);
420 } else if (header
.block_size
!= 1 * MiB
) {
421 logout("unsupported block size %u B\n", header
.block_size
);
423 } else if ((header
.disk_size
+ header
.block_size
- 1) / header
.block_size
!=
424 (uint64_t)header
.blocks_in_image
) {
425 logout("unexpected block number %u B\n", header
.blocks_in_image
);
427 } else if (!uuid_is_null(header
.uuid_link
)) {
428 logout("link uuid != 0, unsupported\n");
430 } else if (!uuid_is_null(header
.uuid_parent
)) {
431 logout("parent uuid != 0, unsupported\n");
435 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
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
;
442 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
443 bmap_size
= (bmap_size
+ SECTOR_SIZE
- 1) / SECTOR_SIZE
;
444 s
->bmap
= qemu_malloc(bmap_size
* SECTOR_SIZE
);
445 if (bdrv_read(s
->hd
, s
->bmap_sector
, (uint8_t *)s
->bmap
, bmap_size
) < 0) {
459 static int vdi_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
460 int nb_sectors
, int *pnum
)
462 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
463 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
464 size_t bmap_index
= sector_num
/ s
->block_sectors
;
465 size_t sector_in_block
= sector_num
% s
->block_sectors
;
466 int n_sectors
= s
->block_sectors
- sector_in_block
;
467 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
468 logout("%p, %" PRId64
", %d, %p\n", bs
, sector_num
, nb_sectors
, pnum
);
469 if (n_sectors
> nb_sectors
) {
470 n_sectors
= nb_sectors
;
473 return bmap_entry
!= VDI_UNALLOCATED
;
476 static void vdi_aio_cancel(BlockDriverAIOCB
*blockacb
)
478 /* TODO: This code is untested. How can I get it executed? */
479 VdiAIOCB
*acb
= (VdiAIOCB
*)blockacb
;
482 bdrv_aio_cancel(acb
->hd_aiocb
);
484 qemu_aio_release(acb
);
487 static AIOPool vdi_aio_pool
= {
488 .aiocb_size
= sizeof(VdiAIOCB
),
489 .cancel
= vdi_aio_cancel
,
492 static VdiAIOCB
*vdi_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
493 QEMUIOVector
*qiov
, int nb_sectors
,
494 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
498 logout("%p, %" PRId64
", %p, %d, %p, %p, %d\n",
499 bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, is_write
);
501 acb
= qemu_aio_get(&vdi_aio_pool
, bs
, cb
, opaque
);
503 acb
->hd_aiocb
= NULL
;
504 acb
->sector_num
= sector_num
;
506 if (qiov
->niov
> 1) {
507 acb
->buf
= qemu_blockalign(bs
, qiov
->size
);
508 acb
->orig_buf
= acb
->buf
;
510 qemu_iovec_to_buffer(qiov
, acb
->buf
);
513 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
515 acb
->nb_sectors
= nb_sectors
;
517 acb
->bmap_first
= VDI_UNALLOCATED
;
518 acb
->bmap_last
= VDI_UNALLOCATED
;
519 acb
->block_buffer
= NULL
;
520 acb
->header_modified
= 0;
525 static int vdi_schedule_bh(QEMUBHFunc
*cb
, VdiAIOCB
*acb
)
533 acb
->bh
= qemu_bh_new(cb
, acb
);
538 qemu_bh_schedule(acb
->bh
);
543 static void vdi_aio_read_cb(void *opaque
, int ret
);
545 static void vdi_aio_read_bh(void *opaque
)
547 VdiAIOCB
*acb
= opaque
;
549 qemu_bh_delete(acb
->bh
);
551 vdi_aio_read_cb(opaque
, 0);
554 static void vdi_aio_read_cb(void *opaque
, int ret
)
556 VdiAIOCB
*acb
= opaque
;
557 BlockDriverState
*bs
= acb
->common
.bs
;
558 BDRVVdiState
*s
= bs
->opaque
;
560 uint32_t block_index
;
561 uint32_t sector_in_block
;
564 logout("%u sectors read\n", acb
->n_sectors
);
566 acb
->hd_aiocb
= NULL
;
572 acb
->nb_sectors
-= acb
->n_sectors
;
574 if (acb
->nb_sectors
== 0) {
575 /* request completed */
580 acb
->sector_num
+= acb
->n_sectors
;
581 acb
->buf
+= acb
->n_sectors
* SECTOR_SIZE
;
583 block_index
= acb
->sector_num
/ s
->block_sectors
;
584 sector_in_block
= acb
->sector_num
% s
->block_sectors
;
585 n_sectors
= s
->block_sectors
- sector_in_block
;
586 if (n_sectors
> acb
->nb_sectors
) {
587 n_sectors
= acb
->nb_sectors
;
590 logout("will read %u sectors starting at sector %" PRIu64
"\n",
591 n_sectors
, acb
->sector_num
);
593 /* prepare next AIO request */
594 acb
->n_sectors
= n_sectors
;
595 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
596 if (bmap_entry
== VDI_UNALLOCATED
) {
597 /* Block not allocated, return zeros, no need to wait. */
598 memset(acb
->buf
, 0, n_sectors
* SECTOR_SIZE
);
599 ret
= vdi_schedule_bh(vdi_aio_read_bh
, acb
);
604 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
605 (uint64_t)bmap_entry
* s
->block_sectors
+
607 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
608 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
609 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
610 acb
->hd_aiocb
= bdrv_aio_readv(s
->hd
, offset
, &acb
->hd_qiov
,
611 n_sectors
, vdi_aio_read_cb
, acb
);
612 if (acb
->hd_aiocb
== NULL
) {
618 if (acb
->qiov
->niov
> 1) {
619 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
620 qemu_vfree(acb
->orig_buf
);
622 acb
->common
.cb(acb
->common
.opaque
, ret
);
623 qemu_aio_release(acb
);
626 static BlockDriverAIOCB
*vdi_aio_readv(BlockDriverState
*bs
,
627 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
628 BlockDriverCompletionFunc
*cb
, void *opaque
)
632 acb
= vdi_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
636 vdi_aio_read_cb(acb
, 0);
640 static void vdi_aio_write_cb(void *opaque
, int ret
)
642 VdiAIOCB
*acb
= opaque
;
643 BlockDriverState
*bs
= acb
->common
.bs
;
644 BDRVVdiState
*s
= bs
->opaque
;
646 uint32_t block_index
;
647 uint32_t sector_in_block
;
650 acb
->hd_aiocb
= NULL
;
656 acb
->nb_sectors
-= acb
->n_sectors
;
657 acb
->sector_num
+= acb
->n_sectors
;
658 acb
->buf
+= acb
->n_sectors
* SECTOR_SIZE
;
660 if (acb
->nb_sectors
== 0) {
661 logout("finished data write\n");
663 if (acb
->header_modified
) {
664 VdiHeader
*header
= acb
->block_buffer
;
665 logout("now writing modified header\n");
666 assert(acb
->bmap_first
!= VDI_UNALLOCATED
);
668 vdi_header_to_le(header
);
669 acb
->header_modified
= 0;
670 acb
->hd_iov
.iov_base
= acb
->block_buffer
;
671 acb
->hd_iov
.iov_len
= SECTOR_SIZE
;
672 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
673 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
, 0, &acb
->hd_qiov
, 1,
674 vdi_aio_write_cb
, acb
);
675 if (acb
->hd_aiocb
== NULL
) {
679 } else if (acb
->bmap_first
!= VDI_UNALLOCATED
) {
680 /* One or more new blocks were allocated. */
684 qemu_free(acb
->block_buffer
);
685 acb
->block_buffer
= NULL
;
686 bmap_first
= acb
->bmap_first
;
687 bmap_last
= acb
->bmap_last
;
688 logout("now writing modified block map entry %u...%u\n",
689 bmap_first
, bmap_last
);
690 /* Write modified sectors from block map. */
691 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
692 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
693 n_sectors
= bmap_last
- bmap_first
+ 1;
694 offset
= s
->bmap_sector
+ bmap_first
;
695 acb
->bmap_first
= VDI_UNALLOCATED
;
696 acb
->hd_iov
.iov_base
= (void *)((uint8_t *)&s
->bmap
[0] +
697 bmap_first
* SECTOR_SIZE
);
698 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
699 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
700 logout("will write %u block map sectors starting from entry %u\n",
701 n_sectors
, bmap_first
);
702 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
, offset
, &acb
->hd_qiov
,
703 n_sectors
, vdi_aio_write_cb
, acb
);
704 if (acb
->hd_aiocb
== NULL
) {
713 logout("%u sectors written\n", acb
->n_sectors
);
715 block_index
= acb
->sector_num
/ s
->block_sectors
;
716 sector_in_block
= acb
->sector_num
% s
->block_sectors
;
717 n_sectors
= s
->block_sectors
- sector_in_block
;
718 if (n_sectors
> acb
->nb_sectors
) {
719 n_sectors
= acb
->nb_sectors
;
722 logout("will write %u sectors starting at sector %" PRIu64
"\n",
723 n_sectors
, acb
->sector_num
);
725 /* prepare next AIO request */
726 acb
->n_sectors
= n_sectors
;
727 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
728 if (bmap_entry
== VDI_UNALLOCATED
) {
729 /* Allocate new block and write to it. */
732 bmap_entry
= s
->header
.blocks_allocated
;
733 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
734 s
->header
.blocks_allocated
++;
735 offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
736 (uint64_t)bmap_entry
* s
->block_sectors
;
737 block
= acb
->block_buffer
;
739 block
= qemu_mallocz(s
->block_size
);
740 acb
->block_buffer
= block
;
741 acb
->bmap_first
= block_index
;
742 assert(!acb
->header_modified
);
743 acb
->header_modified
= 1;
745 acb
->bmap_last
= block_index
;
746 memcpy(block
+ sector_in_block
* SECTOR_SIZE
,
747 acb
->buf
, n_sectors
* SECTOR_SIZE
);
748 acb
->hd_iov
.iov_base
= (void *)block
;
749 acb
->hd_iov
.iov_len
= s
->block_size
;
750 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
751 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
, offset
,
752 &acb
->hd_qiov
, s
->block_sectors
,
753 vdi_aio_write_cb
, acb
);
754 if (acb
->hd_aiocb
== NULL
) {
758 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
759 (uint64_t)bmap_entry
* s
->block_sectors
+
761 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
762 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
763 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
764 acb
->hd_aiocb
= bdrv_aio_writev(s
->hd
, offset
, &acb
->hd_qiov
,
765 n_sectors
, vdi_aio_write_cb
, acb
);
766 if (acb
->hd_aiocb
== NULL
) {
774 if (acb
->qiov
->niov
> 1) {
775 qemu_vfree(acb
->orig_buf
);
777 acb
->common
.cb(acb
->common
.opaque
, ret
);
778 qemu_aio_release(acb
);
781 static BlockDriverAIOCB
*vdi_aio_writev(BlockDriverState
*bs
,
782 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
783 BlockDriverCompletionFunc
*cb
, void *opaque
)
787 acb
= vdi_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
791 vdi_aio_write_cb(acb
, 0);
795 static int vdi_create(const char *filename
, QEMUOptionParameter
*options
)
801 size_t block_size
= 1 * MiB
;
802 uint32_t image_type
= VDI_TYPE_DYNAMIC
;
810 /* Read out options. */
811 while (options
&& options
->name
) {
812 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
813 bytes
= options
->value
.n
;
814 #if defined(CONFIG_VDI_BLOCK_SIZE)
815 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
816 if (options
->value
.n
) {
817 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
818 block_size
= options
->value
.n
;
821 #if defined(CONFIG_VDI_STATIC_IMAGE)
822 } else if (!strcmp(options
->name
, BLOCK_OPT_STATIC
)) {
823 if (options
->value
.n
) {
824 image_type
= VDI_TYPE_STATIC
;
831 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
837 blocks
= bytes
/ block_size
;
838 bmap_size
= blocks
* sizeof(uint32_t);
839 bmap_size
= ((bmap_size
+ SECTOR_SIZE
- 1) & ~(SECTOR_SIZE
-1));
841 memset(&header
, 0, sizeof(header
));
842 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
843 header
.signature
= VDI_SIGNATURE
;
844 header
.version
= VDI_VERSION_1_1
;
845 header
.header_size
= 0x180;
846 header
.image_type
= image_type
;
847 header
.offset_bmap
= 0x200;
848 header
.offset_data
= 0x200 + bmap_size
;
849 header
.sector_size
= SECTOR_SIZE
;
850 header
.disk_size
= bytes
;
851 header
.block_size
= block_size
;
852 header
.blocks_in_image
= blocks
;
853 if (image_type
== VDI_TYPE_STATIC
) {
854 header
.blocks_allocated
= blocks
;
856 uuid_generate(header
.uuid_image
);
857 uuid_generate(header
.uuid_last_snap
);
858 /* There is no need to set header.uuid_link or header.uuid_parent here. */
859 #if defined(CONFIG_VDI_DEBUG)
860 vdi_header_print(&header
);
862 vdi_header_to_le(&header
);
863 if (write(fd
, &header
, sizeof(header
)) < 0) {
867 bmap
= (uint32_t *)qemu_mallocz(bmap_size
);
868 for (i
= 0; i
< blocks
; i
++) {
869 if (image_type
== VDI_TYPE_STATIC
) {
872 bmap
[i
] = VDI_UNALLOCATED
;
875 if (write(fd
, bmap
, bmap_size
) < 0) {
879 if (image_type
== VDI_TYPE_STATIC
) {
880 if (ftruncate(fd
, sizeof(header
) + bmap_size
+ blocks
* block_size
)) {
892 static void vdi_close(BlockDriverState
*bs
)
894 BDRVVdiState
*s
= bs
->opaque
;
899 static void vdi_flush(BlockDriverState
*bs
)
901 BDRVVdiState
*s
= bs
->opaque
;
907 static QEMUOptionParameter vdi_create_options
[] = {
909 .name
= BLOCK_OPT_SIZE
,
911 .help
= "Virtual disk size"
913 #if defined(CONFIG_VDI_BLOCK_SIZE)
915 .name
= BLOCK_OPT_CLUSTER_SIZE
,
917 .help
= "VDI cluster (block) size"
920 #if defined(CONFIG_VDI_STATIC_IMAGE)
922 .name
= BLOCK_OPT_STATIC
,
924 .help
= "VDI static (pre-allocated) image"
927 /* TODO: An additional option to set UUID values might be useful. */
931 static BlockDriver bdrv_vdi
= {
932 .format_name
= "vdi",
933 .instance_size
= sizeof(BDRVVdiState
),
934 .bdrv_probe
= vdi_probe
,
935 .bdrv_open
= vdi_open
,
936 .bdrv_close
= vdi_close
,
937 .bdrv_create
= vdi_create
,
938 .bdrv_flush
= vdi_flush
,
939 .bdrv_is_allocated
= vdi_is_allocated
,
940 .bdrv_make_empty
= vdi_make_empty
,
942 .bdrv_aio_readv
= vdi_aio_readv
,
943 #if defined(CONFIG_VDI_WRITE)
944 .bdrv_aio_writev
= vdi_aio_writev
,
947 .bdrv_get_info
= vdi_get_info
,
949 .create_options
= vdi_create_options
,
950 .bdrv_check
= vdi_check
,
953 static void bdrv_vdi_init(void)
956 bdrv_register(&bdrv_vdi
);
959 block_init(bdrv_vdi_init
);