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
90 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
92 #if defined(CONFIG_VDI_DEBUG)
93 #define logout(fmt, ...) \
94 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
96 #define logout(fmt, ...) ((void)0)
99 /* Image signature. */
100 #define VDI_SIGNATURE 0xbeda107f
103 #define VDI_VERSION_1_1 0x00010001
106 #define VDI_TYPE_DYNAMIC 1
107 #define VDI_TYPE_STATIC 2
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.
115 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
117 /* A never-allocated block; semantically arbitrary content. */
118 #define VDI_UNALLOCATED 0xffffffffU
120 /* A discarded (no longer allocated) block; semantically zero-filled. */
121 #define VDI_DISCARDED 0xfffffffeU
123 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
125 #if !defined(CONFIG_UUID)
126 void uuid_generate(uuid_t out
)
128 memset(out
, 0, sizeof(uuid_t
));
131 int uuid_is_null(const uuid_t uu
)
133 uuid_t null_uuid
= { 0 };
134 return memcmp(uu
, null_uuid
, sizeof(uuid_t
)) == 0;
137 void uuid_unparse(const uuid_t uu
, char *out
)
139 snprintf(out
, 37, UUID_FMT
,
140 uu
[0], uu
[1], uu
[2], uu
[3], uu
[4], uu
[5], uu
[6], uu
[7],
141 uu
[8], uu
[9], uu
[10], uu
[11], uu
[12], uu
[13], uu
[14], uu
[15]);
146 BlockDriverAIOCB common
;
150 /* Total number of sectors. */
152 /* Number of sectors for current AIO. */
154 /* New allocated block map entry. */
157 /* Buffer for new allocated block. */
162 BlockDriverAIOCB
*hd_aiocb
;
164 QEMUIOVector hd_qiov
;
172 uint32_t header_size
;
174 uint32_t image_flags
;
175 char description
[256];
176 uint32_t offset_bmap
;
177 uint32_t offset_data
;
178 uint32_t cylinders
; /* disk geometry, unused here */
179 uint32_t heads
; /* disk geometry, unused here */
180 uint32_t sectors
; /* disk geometry, unused here */
181 uint32_t sector_size
;
185 uint32_t block_extra
; /* unused here */
186 uint32_t blocks_in_image
;
187 uint32_t blocks_allocated
;
189 uuid_t uuid_last_snap
;
196 /* The block map entries are little endian (even in memory). */
198 /* Size of block (bytes). */
200 /* Size of block (sectors). */
201 uint32_t block_sectors
;
202 /* First sector of block map. */
203 uint32_t bmap_sector
;
204 /* VDI header (converted to host endianness). */
208 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
209 * format (network byte order, standard, see RFC 4122) and vice versa.
211 static void uuid_convert(uuid_t uuid
)
213 bswap32s((uint32_t *)&uuid
[0]);
214 bswap16s((uint16_t *)&uuid
[4]);
215 bswap16s((uint16_t *)&uuid
[6]);
218 static void vdi_header_to_cpu(VdiHeader
*header
)
220 le32_to_cpus(&header
->signature
);
221 le32_to_cpus(&header
->version
);
222 le32_to_cpus(&header
->header_size
);
223 le32_to_cpus(&header
->image_type
);
224 le32_to_cpus(&header
->image_flags
);
225 le32_to_cpus(&header
->offset_bmap
);
226 le32_to_cpus(&header
->offset_data
);
227 le32_to_cpus(&header
->cylinders
);
228 le32_to_cpus(&header
->heads
);
229 le32_to_cpus(&header
->sectors
);
230 le32_to_cpus(&header
->sector_size
);
231 le64_to_cpus(&header
->disk_size
);
232 le32_to_cpus(&header
->block_size
);
233 le32_to_cpus(&header
->block_extra
);
234 le32_to_cpus(&header
->blocks_in_image
);
235 le32_to_cpus(&header
->blocks_allocated
);
236 uuid_convert(header
->uuid_image
);
237 uuid_convert(header
->uuid_last_snap
);
238 uuid_convert(header
->uuid_link
);
239 uuid_convert(header
->uuid_parent
);
242 static void vdi_header_to_le(VdiHeader
*header
)
244 cpu_to_le32s(&header
->signature
);
245 cpu_to_le32s(&header
->version
);
246 cpu_to_le32s(&header
->header_size
);
247 cpu_to_le32s(&header
->image_type
);
248 cpu_to_le32s(&header
->image_flags
);
249 cpu_to_le32s(&header
->offset_bmap
);
250 cpu_to_le32s(&header
->offset_data
);
251 cpu_to_le32s(&header
->cylinders
);
252 cpu_to_le32s(&header
->heads
);
253 cpu_to_le32s(&header
->sectors
);
254 cpu_to_le32s(&header
->sector_size
);
255 cpu_to_le64s(&header
->disk_size
);
256 cpu_to_le32s(&header
->block_size
);
257 cpu_to_le32s(&header
->block_extra
);
258 cpu_to_le32s(&header
->blocks_in_image
);
259 cpu_to_le32s(&header
->blocks_allocated
);
260 cpu_to_le32s(&header
->blocks_allocated
);
261 uuid_convert(header
->uuid_image
);
262 uuid_convert(header
->uuid_last_snap
);
263 uuid_convert(header
->uuid_link
);
264 uuid_convert(header
->uuid_parent
);
267 #if defined(CONFIG_VDI_DEBUG)
268 static void vdi_header_print(VdiHeader
*header
)
271 logout("text %s", header
->text
);
272 logout("signature 0x%04x\n", header
->signature
);
273 logout("header size 0x%04x\n", header
->header_size
);
274 logout("image type 0x%04x\n", header
->image_type
);
275 logout("image flags 0x%04x\n", header
->image_flags
);
276 logout("description %s\n", header
->description
);
277 logout("offset bmap 0x%04x\n", header
->offset_bmap
);
278 logout("offset data 0x%04x\n", header
->offset_data
);
279 logout("cylinders 0x%04x\n", header
->cylinders
);
280 logout("heads 0x%04x\n", header
->heads
);
281 logout("sectors 0x%04x\n", header
->sectors
);
282 logout("sector size 0x%04x\n", header
->sector_size
);
283 logout("image size 0x%" PRIx64
" B (%" PRIu64
" MiB)\n",
284 header
->disk_size
, header
->disk_size
/ MiB
);
285 logout("block size 0x%04x\n", header
->block_size
);
286 logout("block extra 0x%04x\n", header
->block_extra
);
287 logout("blocks tot. 0x%04x\n", header
->blocks_in_image
);
288 logout("blocks all. 0x%04x\n", header
->blocks_allocated
);
289 uuid_unparse(header
->uuid_image
, uuid
);
290 logout("uuid image %s\n", uuid
);
291 uuid_unparse(header
->uuid_last_snap
, uuid
);
292 logout("uuid snap %s\n", uuid
);
293 uuid_unparse(header
->uuid_link
, uuid
);
294 logout("uuid link %s\n", uuid
);
295 uuid_unparse(header
->uuid_parent
, uuid
);
296 logout("uuid parent %s\n", uuid
);
300 static int vdi_check(BlockDriverState
*bs
, BdrvCheckResult
*res
)
302 /* TODO: additional checks possible. */
303 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
304 uint32_t blocks_allocated
= 0;
309 bmap
= g_malloc(s
->header
.blocks_in_image
* sizeof(uint32_t));
310 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
312 /* Check block map and value of blocks_allocated. */
313 for (block
= 0; block
< s
->header
.blocks_in_image
; block
++) {
314 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[block
]);
315 if (VDI_IS_ALLOCATED(bmap_entry
)) {
316 if (bmap_entry
< s
->header
.blocks_in_image
) {
318 if (!VDI_IS_ALLOCATED(bmap
[bmap_entry
])) {
319 bmap
[bmap_entry
] = bmap_entry
;
321 fprintf(stderr
, "ERROR: block index %" PRIu32
322 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
326 fprintf(stderr
, "ERROR: block index %" PRIu32
327 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
332 if (blocks_allocated
!= s
->header
.blocks_allocated
) {
333 fprintf(stderr
, "ERROR: allocated blocks mismatch, is %" PRIu32
334 ", should be %" PRIu32
"\n",
335 blocks_allocated
, s
->header
.blocks_allocated
);
344 static int vdi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
346 /* TODO: vdi_get_info would be needed for machine snapshots.
347 vm_state_offset is still missing. */
348 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
350 bdi
->cluster_size
= s
->block_size
;
351 bdi
->vm_state_offset
= 0;
355 static int vdi_make_empty(BlockDriverState
*bs
)
357 /* TODO: missing code. */
359 /* The return value for missing code must be 0, see block.c. */
363 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
365 const VdiHeader
*header
= (const VdiHeader
*)buf
;
370 if (buf_size
< sizeof(*header
)) {
371 /* Header too small, no VDI. */
372 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
377 logout("no vdi image\n");
379 logout("%s", header
->text
);
385 static int vdi_open(BlockDriverState
*bs
, int flags
)
387 BDRVVdiState
*s
= bs
->opaque
;
393 if (bdrv_read(bs
->file
, 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
.disk_size
% SECTOR_SIZE
!= 0) {
403 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
404 We accept them but round the disk size to the next multiple of
406 logout("odd disk size %" PRIu64
" B, round up\n", header
.disk_size
);
407 header
.disk_size
+= SECTOR_SIZE
- 1;
408 header
.disk_size
&= ~(SECTOR_SIZE
- 1);
411 if (header
.version
!= VDI_VERSION_1_1
) {
412 logout("unsupported version %u.%u\n",
413 header
.version
>> 16, header
.version
& 0xffff);
415 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
416 /* We only support block maps which start on a sector boundary. */
417 logout("unsupported block map offset 0x%x B\n", header
.offset_bmap
);
419 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
420 /* We only support data blocks which start on a sector boundary. */
421 logout("unsupported data offset 0x%x B\n", header
.offset_data
);
423 } else if (header
.sector_size
!= SECTOR_SIZE
) {
424 logout("unsupported sector size %u B\n", header
.sector_size
);
426 } else if (header
.block_size
!= 1 * MiB
) {
427 logout("unsupported block size %u B\n", header
.block_size
);
429 } else if (header
.disk_size
>
430 (uint64_t)header
.blocks_in_image
* header
.block_size
) {
431 logout("unsupported disk size %" PRIu64
" B\n", header
.disk_size
);
433 } else if (!uuid_is_null(header
.uuid_link
)) {
434 logout("link uuid != 0, unsupported\n");
436 } else if (!uuid_is_null(header
.uuid_parent
)) {
437 logout("parent uuid != 0, unsupported\n");
441 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
443 s
->block_size
= header
.block_size
;
444 s
->block_sectors
= header
.block_size
/ SECTOR_SIZE
;
445 s
->bmap_sector
= header
.offset_bmap
/ SECTOR_SIZE
;
448 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
449 bmap_size
= (bmap_size
+ SECTOR_SIZE
- 1) / SECTOR_SIZE
;
451 s
->bmap
= g_malloc(bmap_size
* SECTOR_SIZE
);
453 if (bdrv_read(bs
->file
, s
->bmap_sector
, (uint8_t *)s
->bmap
, bmap_size
) < 0) {
466 static int vdi_is_allocated(BlockDriverState
*bs
, int64_t sector_num
,
467 int nb_sectors
, int *pnum
)
469 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
470 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
471 size_t bmap_index
= sector_num
/ s
->block_sectors
;
472 size_t sector_in_block
= sector_num
% s
->block_sectors
;
473 int n_sectors
= s
->block_sectors
- sector_in_block
;
474 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
475 logout("%p, %" PRId64
", %d, %p\n", bs
, sector_num
, nb_sectors
, pnum
);
476 if (n_sectors
> nb_sectors
) {
477 n_sectors
= nb_sectors
;
480 return VDI_IS_ALLOCATED(bmap_entry
);
483 static void vdi_aio_cancel(BlockDriverAIOCB
*blockacb
)
485 /* TODO: This code is untested. How can I get it executed? */
486 VdiAIOCB
*acb
= container_of(blockacb
, VdiAIOCB
, common
);
489 bdrv_aio_cancel(acb
->hd_aiocb
);
491 qemu_aio_release(acb
);
494 static AIOPool vdi_aio_pool
= {
495 .aiocb_size
= sizeof(VdiAIOCB
),
496 .cancel
= vdi_aio_cancel
,
499 static VdiAIOCB
*vdi_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
500 QEMUIOVector
*qiov
, int nb_sectors
,
501 BlockDriverCompletionFunc
*cb
, void *opaque
, int is_write
)
505 logout("%p, %" PRId64
", %p, %d, %p, %p, %d\n",
506 bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, is_write
);
508 acb
= qemu_aio_get(&vdi_aio_pool
, bs
, cb
, opaque
);
510 acb
->hd_aiocb
= NULL
;
511 acb
->sector_num
= sector_num
;
513 acb
->is_write
= is_write
;
515 if (qiov
->niov
> 1) {
516 acb
->buf
= qemu_blockalign(bs
, qiov
->size
);
517 acb
->orig_buf
= acb
->buf
;
519 qemu_iovec_to_buffer(qiov
, acb
->buf
);
522 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
524 acb
->nb_sectors
= nb_sectors
;
526 acb
->bmap_first
= VDI_UNALLOCATED
;
527 acb
->bmap_last
= VDI_UNALLOCATED
;
528 acb
->block_buffer
= NULL
;
529 acb
->header_modified
= 0;
534 static int vdi_schedule_bh(QEMUBHFunc
*cb
, VdiAIOCB
*acb
)
542 acb
->bh
= qemu_bh_new(cb
, acb
);
547 qemu_bh_schedule(acb
->bh
);
552 static void vdi_aio_read_cb(void *opaque
, int ret
);
553 static void vdi_aio_write_cb(void *opaque
, int ret
);
555 static void vdi_aio_rw_bh(void *opaque
)
557 VdiAIOCB
*acb
= opaque
;
559 qemu_bh_delete(acb
->bh
);
563 vdi_aio_write_cb(opaque
, 0);
565 vdi_aio_read_cb(opaque
, 0);
569 static void vdi_aio_read_cb(void *opaque
, int ret
)
571 VdiAIOCB
*acb
= opaque
;
572 BlockDriverState
*bs
= acb
->common
.bs
;
573 BDRVVdiState
*s
= bs
->opaque
;
575 uint32_t block_index
;
576 uint32_t sector_in_block
;
579 logout("%u sectors read\n", acb
->n_sectors
);
581 acb
->hd_aiocb
= NULL
;
587 acb
->nb_sectors
-= acb
->n_sectors
;
589 if (acb
->nb_sectors
== 0) {
590 /* request completed */
595 acb
->sector_num
+= acb
->n_sectors
;
596 acb
->buf
+= acb
->n_sectors
* SECTOR_SIZE
;
598 block_index
= acb
->sector_num
/ s
->block_sectors
;
599 sector_in_block
= acb
->sector_num
% s
->block_sectors
;
600 n_sectors
= s
->block_sectors
- sector_in_block
;
601 if (n_sectors
> acb
->nb_sectors
) {
602 n_sectors
= acb
->nb_sectors
;
605 logout("will read %u sectors starting at sector %" PRIu64
"\n",
606 n_sectors
, acb
->sector_num
);
608 /* prepare next AIO request */
609 acb
->n_sectors
= n_sectors
;
610 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
611 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
612 /* Block not allocated, return zeros, no need to wait. */
613 memset(acb
->buf
, 0, n_sectors
* SECTOR_SIZE
);
614 ret
= vdi_schedule_bh(vdi_aio_rw_bh
, acb
);
619 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
620 (uint64_t)bmap_entry
* s
->block_sectors
+
622 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
623 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
624 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
625 acb
->hd_aiocb
= bdrv_aio_readv(bs
->file
, offset
, &acb
->hd_qiov
,
626 n_sectors
, vdi_aio_read_cb
, acb
);
627 if (acb
->hd_aiocb
== NULL
) {
634 if (acb
->qiov
->niov
> 1) {
635 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
636 qemu_vfree(acb
->orig_buf
);
638 acb
->common
.cb(acb
->common
.opaque
, ret
);
639 qemu_aio_release(acb
);
642 static BlockDriverAIOCB
*vdi_aio_readv(BlockDriverState
*bs
,
643 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
644 BlockDriverCompletionFunc
*cb
, void *opaque
)
650 acb
= vdi_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 0);
655 ret
= vdi_schedule_bh(vdi_aio_rw_bh
, acb
);
657 if (acb
->qiov
->niov
> 1) {
658 qemu_vfree(acb
->orig_buf
);
660 qemu_aio_release(acb
);
667 static void vdi_aio_write_cb(void *opaque
, int ret
)
669 VdiAIOCB
*acb
= opaque
;
670 BlockDriverState
*bs
= acb
->common
.bs
;
671 BDRVVdiState
*s
= bs
->opaque
;
673 uint32_t block_index
;
674 uint32_t sector_in_block
;
677 acb
->hd_aiocb
= NULL
;
683 acb
->nb_sectors
-= acb
->n_sectors
;
684 acb
->sector_num
+= acb
->n_sectors
;
685 acb
->buf
+= acb
->n_sectors
* SECTOR_SIZE
;
687 if (acb
->nb_sectors
== 0) {
688 logout("finished data write\n");
690 if (acb
->header_modified
) {
691 VdiHeader
*header
= acb
->block_buffer
;
692 logout("now writing modified header\n");
693 assert(VDI_IS_ALLOCATED(acb
->bmap_first
));
695 vdi_header_to_le(header
);
696 acb
->header_modified
= 0;
697 acb
->hd_iov
.iov_base
= acb
->block_buffer
;
698 acb
->hd_iov
.iov_len
= SECTOR_SIZE
;
699 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
700 acb
->hd_aiocb
= bdrv_aio_writev(bs
->file
, 0, &acb
->hd_qiov
, 1,
701 vdi_aio_write_cb
, acb
);
702 if (acb
->hd_aiocb
== NULL
) {
707 } else if (VDI_IS_ALLOCATED(acb
->bmap_first
)) {
708 /* One or more new blocks were allocated. */
712 g_free(acb
->block_buffer
);
713 acb
->block_buffer
= NULL
;
714 bmap_first
= acb
->bmap_first
;
715 bmap_last
= acb
->bmap_last
;
716 logout("now writing modified block map entry %u...%u\n",
717 bmap_first
, bmap_last
);
718 /* Write modified sectors from block map. */
719 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
720 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
721 n_sectors
= bmap_last
- bmap_first
+ 1;
722 offset
= s
->bmap_sector
+ bmap_first
;
723 acb
->bmap_first
= VDI_UNALLOCATED
;
724 acb
->hd_iov
.iov_base
= (void *)((uint8_t *)&s
->bmap
[0] +
725 bmap_first
* SECTOR_SIZE
);
726 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
727 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
728 logout("will write %u block map sectors starting from entry %u\n",
729 n_sectors
, bmap_first
);
730 acb
->hd_aiocb
= bdrv_aio_writev(bs
->file
, offset
, &acb
->hd_qiov
,
731 n_sectors
, vdi_aio_write_cb
, acb
);
732 if (acb
->hd_aiocb
== NULL
) {
742 logout("%u sectors written\n", acb
->n_sectors
);
744 block_index
= acb
->sector_num
/ s
->block_sectors
;
745 sector_in_block
= acb
->sector_num
% s
->block_sectors
;
746 n_sectors
= s
->block_sectors
- sector_in_block
;
747 if (n_sectors
> acb
->nb_sectors
) {
748 n_sectors
= acb
->nb_sectors
;
751 logout("will write %u sectors starting at sector %" PRIu64
"\n",
752 n_sectors
, acb
->sector_num
);
754 /* prepare next AIO request */
755 acb
->n_sectors
= n_sectors
;
756 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
757 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
758 /* Allocate new block and write to it. */
761 bmap_entry
= s
->header
.blocks_allocated
;
762 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
763 s
->header
.blocks_allocated
++;
764 offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
765 (uint64_t)bmap_entry
* s
->block_sectors
;
766 block
= acb
->block_buffer
;
768 block
= g_malloc0(s
->block_size
);
769 acb
->block_buffer
= block
;
770 acb
->bmap_first
= block_index
;
771 assert(!acb
->header_modified
);
772 acb
->header_modified
= 1;
774 acb
->bmap_last
= block_index
;
775 memcpy(block
+ sector_in_block
* SECTOR_SIZE
,
776 acb
->buf
, n_sectors
* SECTOR_SIZE
);
777 acb
->hd_iov
.iov_base
= (void *)block
;
778 acb
->hd_iov
.iov_len
= s
->block_size
;
779 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
780 acb
->hd_aiocb
= bdrv_aio_writev(bs
->file
, offset
,
781 &acb
->hd_qiov
, s
->block_sectors
,
782 vdi_aio_write_cb
, acb
);
783 if (acb
->hd_aiocb
== NULL
) {
788 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
789 (uint64_t)bmap_entry
* s
->block_sectors
+
791 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
792 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
793 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
794 acb
->hd_aiocb
= bdrv_aio_writev(bs
->file
, offset
, &acb
->hd_qiov
,
795 n_sectors
, vdi_aio_write_cb
, acb
);
796 if (acb
->hd_aiocb
== NULL
) {
805 if (acb
->qiov
->niov
> 1) {
806 qemu_vfree(acb
->orig_buf
);
808 acb
->common
.cb(acb
->common
.opaque
, ret
);
809 qemu_aio_release(acb
);
812 static BlockDriverAIOCB
*vdi_aio_writev(BlockDriverState
*bs
,
813 int64_t sector_num
, QEMUIOVector
*qiov
, int nb_sectors
,
814 BlockDriverCompletionFunc
*cb
, void *opaque
)
820 acb
= vdi_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, cb
, opaque
, 1);
825 ret
= vdi_schedule_bh(vdi_aio_rw_bh
, acb
);
827 if (acb
->qiov
->niov
> 1) {
828 qemu_vfree(acb
->orig_buf
);
830 qemu_aio_release(acb
);
837 static int vdi_create(const char *filename
, QEMUOptionParameter
*options
)
843 size_t block_size
= DEFAULT_CLUSTER_SIZE
;
844 uint32_t image_type
= VDI_TYPE_DYNAMIC
;
852 /* Read out options. */
853 while (options
&& options
->name
) {
854 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
855 bytes
= options
->value
.n
;
856 #if defined(CONFIG_VDI_BLOCK_SIZE)
857 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
858 if (options
->value
.n
) {
859 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
860 block_size
= options
->value
.n
;
863 #if defined(CONFIG_VDI_STATIC_IMAGE)
864 } else if (!strcmp(options
->name
, BLOCK_OPT_STATIC
)) {
865 if (options
->value
.n
) {
866 image_type
= VDI_TYPE_STATIC
;
873 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
879 /* We need enough blocks to store the given disk size,
880 so always round up. */
881 blocks
= (bytes
+ block_size
- 1) / block_size
;
883 bmap_size
= blocks
* sizeof(uint32_t);
884 bmap_size
= ((bmap_size
+ SECTOR_SIZE
- 1) & ~(SECTOR_SIZE
-1));
886 memset(&header
, 0, sizeof(header
));
887 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
888 header
.signature
= VDI_SIGNATURE
;
889 header
.version
= VDI_VERSION_1_1
;
890 header
.header_size
= 0x180;
891 header
.image_type
= image_type
;
892 header
.offset_bmap
= 0x200;
893 header
.offset_data
= 0x200 + bmap_size
;
894 header
.sector_size
= SECTOR_SIZE
;
895 header
.disk_size
= bytes
;
896 header
.block_size
= block_size
;
897 header
.blocks_in_image
= blocks
;
898 if (image_type
== VDI_TYPE_STATIC
) {
899 header
.blocks_allocated
= blocks
;
901 uuid_generate(header
.uuid_image
);
902 uuid_generate(header
.uuid_last_snap
);
903 /* There is no need to set header.uuid_link or header.uuid_parent here. */
904 #if defined(CONFIG_VDI_DEBUG)
905 vdi_header_print(&header
);
907 vdi_header_to_le(&header
);
908 if (write(fd
, &header
, sizeof(header
)) < 0) {
914 bmap
= (uint32_t *)g_malloc0(bmap_size
);
916 for (i
= 0; i
< blocks
; i
++) {
917 if (image_type
== VDI_TYPE_STATIC
) {
920 bmap
[i
] = VDI_UNALLOCATED
;
923 if (write(fd
, bmap
, bmap_size
) < 0) {
927 if (image_type
== VDI_TYPE_STATIC
) {
928 if (ftruncate(fd
, sizeof(header
) + bmap_size
+ blocks
* block_size
)) {
940 static void vdi_close(BlockDriverState
*bs
)
944 static coroutine_fn
int vdi_co_flush(BlockDriverState
*bs
)
947 return bdrv_co_flush(bs
->file
);
951 static QEMUOptionParameter vdi_create_options
[] = {
953 .name
= BLOCK_OPT_SIZE
,
955 .help
= "Virtual disk size"
957 #if defined(CONFIG_VDI_BLOCK_SIZE)
959 .name
= BLOCK_OPT_CLUSTER_SIZE
,
961 .help
= "VDI cluster (block) size",
962 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
965 #if defined(CONFIG_VDI_STATIC_IMAGE)
967 .name
= BLOCK_OPT_STATIC
,
969 .help
= "VDI static (pre-allocated) image"
972 /* TODO: An additional option to set UUID values might be useful. */
976 static BlockDriver bdrv_vdi
= {
977 .format_name
= "vdi",
978 .instance_size
= sizeof(BDRVVdiState
),
979 .bdrv_probe
= vdi_probe
,
980 .bdrv_open
= vdi_open
,
981 .bdrv_close
= vdi_close
,
982 .bdrv_create
= vdi_create
,
983 .bdrv_co_flush
= vdi_co_flush
,
984 .bdrv_is_allocated
= vdi_is_allocated
,
985 .bdrv_make_empty
= vdi_make_empty
,
987 .bdrv_aio_readv
= vdi_aio_readv
,
988 #if defined(CONFIG_VDI_WRITE)
989 .bdrv_aio_writev
= vdi_aio_writev
,
992 .bdrv_get_info
= vdi_get_info
,
994 .create_options
= vdi_create_options
,
995 .bdrv_check
= vdi_check
,
998 static void bdrv_vdi_init(void)
1001 bdrv_register(&bdrv_vdi
);
1004 block_init(bdrv_vdi_init
);