2 * Block driver for the Virtual Disk Image (VDI) format
4 * Copyright (c) 2009, 2012 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"
55 #include "migration.h"
57 #if defined(CONFIG_UUID)
58 #include <uuid/uuid.h>
60 /* TODO: move uuid emulation to some central place in QEMU. */
61 #include "sysemu.h" /* UUID_FMT */
62 typedef unsigned char uuid_t
[16];
63 void uuid_generate(uuid_t out
);
64 int uuid_is_null(const uuid_t uu
);
65 void uuid_unparse(const uuid_t uu
, char *out
);
68 /* Code configuration options. */
70 /* Enable debug messages. */
71 //~ #define CONFIG_VDI_DEBUG
73 /* Support write operations on VDI images. */
74 #define CONFIG_VDI_WRITE
76 /* Support non-standard block (cluster) size. This is untested.
77 * Maybe it will be needed for very large images.
79 //~ #define CONFIG_VDI_BLOCK_SIZE
81 /* Support static (fixed, pre-allocated) images. */
82 #define CONFIG_VDI_STATIC_IMAGE
84 /* Command line option for static images. */
85 #define BLOCK_OPT_STATIC "static"
88 #define MiB (KiB * KiB)
90 #define SECTOR_SIZE 512
91 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
93 #if defined(CONFIG_VDI_DEBUG)
94 #define logout(fmt, ...) \
95 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
97 #define logout(fmt, ...) ((void)0)
100 /* Image signature. */
101 #define VDI_SIGNATURE 0xbeda107f
104 #define VDI_VERSION_1_1 0x00010001
107 #define VDI_TYPE_DYNAMIC 1
108 #define VDI_TYPE_STATIC 2
110 /* Innotek / SUN images use these strings in header.text:
111 * "<<< innotek VirtualBox Disk Image >>>\n"
112 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
113 * "<<< Sun VirtualBox Disk Image >>>\n"
114 * The value does not matter, so QEMU created images use a different text.
116 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
118 /* A never-allocated block; semantically arbitrary content. */
119 #define VDI_UNALLOCATED 0xffffffffU
121 /* A discarded (no longer allocated) block; semantically zero-filled. */
122 #define VDI_DISCARDED 0xfffffffeU
124 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
126 #if !defined(CONFIG_UUID)
127 void uuid_generate(uuid_t out
)
129 memset(out
, 0, sizeof(uuid_t
));
132 int uuid_is_null(const uuid_t uu
)
134 uuid_t null_uuid
= { 0 };
135 return memcmp(uu
, null_uuid
, sizeof(uuid_t
)) == 0;
138 void uuid_unparse(const uuid_t uu
, char *out
)
140 snprintf(out
, 37, UUID_FMT
,
141 uu
[0], uu
[1], uu
[2], uu
[3], uu
[4], uu
[5], uu
[6], uu
[7],
142 uu
[8], uu
[9], uu
[10], uu
[11], uu
[12], uu
[13], uu
[14], uu
[15]);
147 BlockDriverAIOCB common
;
151 /* Total number of sectors. */
153 /* Number of sectors for current AIO. */
155 /* New allocated block map entry. */
158 /* Buffer for new allocated block. */
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). */
207 Error
*migration_blocker
;
210 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
211 * format (network byte order, standard, see RFC 4122) and vice versa.
213 static void uuid_convert(uuid_t uuid
)
215 bswap32s((uint32_t *)&uuid
[0]);
216 bswap16s((uint16_t *)&uuid
[4]);
217 bswap16s((uint16_t *)&uuid
[6]);
220 static void vdi_header_to_cpu(VdiHeader
*header
)
222 le32_to_cpus(&header
->signature
);
223 le32_to_cpus(&header
->version
);
224 le32_to_cpus(&header
->header_size
);
225 le32_to_cpus(&header
->image_type
);
226 le32_to_cpus(&header
->image_flags
);
227 le32_to_cpus(&header
->offset_bmap
);
228 le32_to_cpus(&header
->offset_data
);
229 le32_to_cpus(&header
->cylinders
);
230 le32_to_cpus(&header
->heads
);
231 le32_to_cpus(&header
->sectors
);
232 le32_to_cpus(&header
->sector_size
);
233 le64_to_cpus(&header
->disk_size
);
234 le32_to_cpus(&header
->block_size
);
235 le32_to_cpus(&header
->block_extra
);
236 le32_to_cpus(&header
->blocks_in_image
);
237 le32_to_cpus(&header
->blocks_allocated
);
238 uuid_convert(header
->uuid_image
);
239 uuid_convert(header
->uuid_last_snap
);
240 uuid_convert(header
->uuid_link
);
241 uuid_convert(header
->uuid_parent
);
244 static void vdi_header_to_le(VdiHeader
*header
)
246 cpu_to_le32s(&header
->signature
);
247 cpu_to_le32s(&header
->version
);
248 cpu_to_le32s(&header
->header_size
);
249 cpu_to_le32s(&header
->image_type
);
250 cpu_to_le32s(&header
->image_flags
);
251 cpu_to_le32s(&header
->offset_bmap
);
252 cpu_to_le32s(&header
->offset_data
);
253 cpu_to_le32s(&header
->cylinders
);
254 cpu_to_le32s(&header
->heads
);
255 cpu_to_le32s(&header
->sectors
);
256 cpu_to_le32s(&header
->sector_size
);
257 cpu_to_le64s(&header
->disk_size
);
258 cpu_to_le32s(&header
->block_size
);
259 cpu_to_le32s(&header
->block_extra
);
260 cpu_to_le32s(&header
->blocks_in_image
);
261 cpu_to_le32s(&header
->blocks_allocated
);
262 cpu_to_le32s(&header
->blocks_allocated
);
263 uuid_convert(header
->uuid_image
);
264 uuid_convert(header
->uuid_last_snap
);
265 uuid_convert(header
->uuid_link
);
266 uuid_convert(header
->uuid_parent
);
269 #if defined(CONFIG_VDI_DEBUG)
270 static void vdi_header_print(VdiHeader
*header
)
273 logout("text %s", header
->text
);
274 logout("signature 0x%04x\n", header
->signature
);
275 logout("header size 0x%04x\n", header
->header_size
);
276 logout("image type 0x%04x\n", header
->image_type
);
277 logout("image flags 0x%04x\n", header
->image_flags
);
278 logout("description %s\n", header
->description
);
279 logout("offset bmap 0x%04x\n", header
->offset_bmap
);
280 logout("offset data 0x%04x\n", header
->offset_data
);
281 logout("cylinders 0x%04x\n", header
->cylinders
);
282 logout("heads 0x%04x\n", header
->heads
);
283 logout("sectors 0x%04x\n", header
->sectors
);
284 logout("sector size 0x%04x\n", header
->sector_size
);
285 logout("image size 0x%" PRIx64
" B (%" PRIu64
" MiB)\n",
286 header
->disk_size
, header
->disk_size
/ MiB
);
287 logout("block size 0x%04x\n", header
->block_size
);
288 logout("block extra 0x%04x\n", header
->block_extra
);
289 logout("blocks tot. 0x%04x\n", header
->blocks_in_image
);
290 logout("blocks all. 0x%04x\n", header
->blocks_allocated
);
291 uuid_unparse(header
->uuid_image
, uuid
);
292 logout("uuid image %s\n", uuid
);
293 uuid_unparse(header
->uuid_last_snap
, uuid
);
294 logout("uuid snap %s\n", uuid
);
295 uuid_unparse(header
->uuid_link
, uuid
);
296 logout("uuid link %s\n", uuid
);
297 uuid_unparse(header
->uuid_parent
, uuid
);
298 logout("uuid parent %s\n", uuid
);
302 static int vdi_check(BlockDriverState
*bs
, BdrvCheckResult
*res
)
304 /* TODO: additional checks possible. */
305 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
306 uint32_t blocks_allocated
= 0;
311 bmap
= g_malloc(s
->header
.blocks_in_image
* sizeof(uint32_t));
312 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
314 /* Check block map and value of blocks_allocated. */
315 for (block
= 0; block
< s
->header
.blocks_in_image
; block
++) {
316 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[block
]);
317 if (VDI_IS_ALLOCATED(bmap_entry
)) {
318 if (bmap_entry
< s
->header
.blocks_in_image
) {
320 if (!VDI_IS_ALLOCATED(bmap
[bmap_entry
])) {
321 bmap
[bmap_entry
] = bmap_entry
;
323 fprintf(stderr
, "ERROR: block index %" PRIu32
324 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
328 fprintf(stderr
, "ERROR: block index %" PRIu32
329 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
334 if (blocks_allocated
!= s
->header
.blocks_allocated
) {
335 fprintf(stderr
, "ERROR: allocated blocks mismatch, is %" PRIu32
336 ", should be %" PRIu32
"\n",
337 blocks_allocated
, s
->header
.blocks_allocated
);
346 static int vdi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
348 /* TODO: vdi_get_info would be needed for machine snapshots.
349 vm_state_offset is still missing. */
350 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
352 bdi
->cluster_size
= s
->block_size
;
353 bdi
->vm_state_offset
= 0;
357 static int vdi_make_empty(BlockDriverState
*bs
)
359 /* TODO: missing code. */
361 /* The return value for missing code must be 0, see block.c. */
365 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
367 const VdiHeader
*header
= (const VdiHeader
*)buf
;
372 if (buf_size
< sizeof(*header
)) {
373 /* Header too small, no VDI. */
374 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
379 logout("no vdi image\n");
381 logout("%s", header
->text
);
387 static int vdi_open(BlockDriverState
*bs
, int flags
)
389 BDRVVdiState
*s
= bs
->opaque
;
395 if (bdrv_read(bs
->file
, 0, (uint8_t *)&header
, 1) < 0) {
399 vdi_header_to_cpu(&header
);
400 #if defined(CONFIG_VDI_DEBUG)
401 vdi_header_print(&header
);
404 if (header
.disk_size
% SECTOR_SIZE
!= 0) {
405 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
406 We accept them but round the disk size to the next multiple of
408 logout("odd disk size %" PRIu64
" B, round up\n", header
.disk_size
);
409 header
.disk_size
+= SECTOR_SIZE
- 1;
410 header
.disk_size
&= ~(SECTOR_SIZE
- 1);
413 if (header
.version
!= VDI_VERSION_1_1
) {
414 logout("unsupported version %u.%u\n",
415 header
.version
>> 16, header
.version
& 0xffff);
417 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
418 /* We only support block maps which start on a sector boundary. */
419 logout("unsupported block map offset 0x%x B\n", header
.offset_bmap
);
421 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
422 /* We only support data blocks which start on a sector boundary. */
423 logout("unsupported data offset 0x%x B\n", header
.offset_data
);
425 } else if (header
.sector_size
!= SECTOR_SIZE
) {
426 logout("unsupported sector size %u B\n", header
.sector_size
);
428 } else if (header
.block_size
!= 1 * MiB
) {
429 logout("unsupported block size %u B\n", header
.block_size
);
431 } else if (header
.disk_size
>
432 (uint64_t)header
.blocks_in_image
* header
.block_size
) {
433 logout("unsupported disk size %" PRIu64
" B\n", header
.disk_size
);
435 } else if (!uuid_is_null(header
.uuid_link
)) {
436 logout("link uuid != 0, unsupported\n");
438 } else if (!uuid_is_null(header
.uuid_parent
)) {
439 logout("parent uuid != 0, unsupported\n");
443 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
445 s
->block_size
= header
.block_size
;
446 s
->block_sectors
= header
.block_size
/ SECTOR_SIZE
;
447 s
->bmap_sector
= header
.offset_bmap
/ SECTOR_SIZE
;
450 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
451 bmap_size
= (bmap_size
+ SECTOR_SIZE
- 1) / SECTOR_SIZE
;
453 s
->bmap
= g_malloc(bmap_size
* SECTOR_SIZE
);
455 if (bdrv_read(bs
->file
, s
->bmap_sector
, (uint8_t *)s
->bmap
, bmap_size
) < 0) {
459 /* Disable migration when vdi images are used */
460 error_set(&s
->migration_blocker
,
461 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
462 "vdi", bs
->device_name
, "live migration");
463 migrate_add_blocker(s
->migration_blocker
);
474 static int coroutine_fn
vdi_co_is_allocated(BlockDriverState
*bs
,
475 int64_t sector_num
, int nb_sectors
, int *pnum
)
477 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
478 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
479 size_t bmap_index
= sector_num
/ s
->block_sectors
;
480 size_t sector_in_block
= sector_num
% s
->block_sectors
;
481 int n_sectors
= s
->block_sectors
- sector_in_block
;
482 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
483 logout("%p, %" PRId64
", %d, %p\n", bs
, sector_num
, nb_sectors
, pnum
);
484 if (n_sectors
> nb_sectors
) {
485 n_sectors
= nb_sectors
;
488 return VDI_IS_ALLOCATED(bmap_entry
);
491 static AIOPool vdi_aio_pool
= {
492 .aiocb_size
= sizeof(VdiAIOCB
),
495 static VdiAIOCB
*vdi_aio_setup(BlockDriverState
*bs
, int64_t sector_num
,
496 QEMUIOVector
*qiov
, int nb_sectors
, int is_write
)
500 logout("%p, %" PRId64
", %p, %d, %p, %p, %d\n",
501 bs
, sector_num
, qiov
, nb_sectors
, is_write
);
503 acb
= qemu_aio_get(&vdi_aio_pool
, bs
, NULL
, NULL
);
504 acb
->sector_num
= sector_num
;
506 acb
->is_write
= is_write
;
508 if (qiov
->niov
> 1) {
509 acb
->buf
= qemu_blockalign(bs
, qiov
->size
);
510 acb
->orig_buf
= acb
->buf
;
512 qemu_iovec_to_buffer(qiov
, acb
->buf
);
515 acb
->buf
= (uint8_t *)qiov
->iov
->iov_base
;
517 acb
->nb_sectors
= nb_sectors
;
519 acb
->bmap_first
= VDI_UNALLOCATED
;
520 acb
->bmap_last
= VDI_UNALLOCATED
;
521 acb
->block_buffer
= NULL
;
522 acb
->header_modified
= 0;
526 static int vdi_aio_read_cb(void *opaque
, int ret
)
528 VdiAIOCB
*acb
= opaque
;
529 BlockDriverState
*bs
= acb
->common
.bs
;
530 BDRVVdiState
*s
= bs
->opaque
;
532 uint32_t block_index
;
533 uint32_t sector_in_block
;
536 logout("%u sectors read\n", acb
->n_sectors
);
539 acb
->nb_sectors
-= acb
->n_sectors
;
541 if (acb
->nb_sectors
== 0) {
542 /* request completed */
547 acb
->sector_num
+= acb
->n_sectors
;
548 acb
->buf
+= acb
->n_sectors
* SECTOR_SIZE
;
550 block_index
= acb
->sector_num
/ s
->block_sectors
;
551 sector_in_block
= acb
->sector_num
% s
->block_sectors
;
552 n_sectors
= s
->block_sectors
- sector_in_block
;
553 if (n_sectors
> acb
->nb_sectors
) {
554 n_sectors
= acb
->nb_sectors
;
557 logout("will read %u sectors starting at sector %" PRIu64
"\n",
558 n_sectors
, acb
->sector_num
);
560 /* prepare next AIO request */
561 acb
->n_sectors
= n_sectors
;
562 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
563 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
564 /* Block not allocated, return zeros, no need to wait. */
565 memset(acb
->buf
, 0, n_sectors
* SECTOR_SIZE
);
568 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
569 (uint64_t)bmap_entry
* s
->block_sectors
+
571 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
572 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
573 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
574 ret
= bdrv_co_readv(bs
->file
, offset
, n_sectors
, &acb
->hd_qiov
);
581 if (acb
->qiov
->niov
> 1) {
582 qemu_iovec_from_buffer(acb
->qiov
, acb
->orig_buf
, acb
->qiov
->size
);
583 qemu_vfree(acb
->orig_buf
);
585 qemu_aio_release(acb
);
589 static int vdi_co_readv(BlockDriverState
*bs
,
590 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
596 acb
= vdi_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, 0);
597 ret
= vdi_aio_read_cb(acb
, 0);
601 static int vdi_aio_write_cb(void *opaque
, int ret
)
603 VdiAIOCB
*acb
= opaque
;
604 BlockDriverState
*bs
= acb
->common
.bs
;
605 BDRVVdiState
*s
= bs
->opaque
;
607 uint32_t block_index
;
608 uint32_t sector_in_block
;
612 acb
->nb_sectors
-= acb
->n_sectors
;
613 acb
->sector_num
+= acb
->n_sectors
;
614 acb
->buf
+= acb
->n_sectors
* SECTOR_SIZE
;
616 if (acb
->nb_sectors
== 0) {
617 logout("finished data write\n");
620 if (acb
->header_modified
) {
621 VdiHeader
*header
= acb
->block_buffer
;
622 logout("now writing modified header\n");
623 assert(VDI_IS_ALLOCATED(acb
->bmap_first
));
625 vdi_header_to_le(header
);
626 acb
->header_modified
= 0;
627 acb
->hd_iov
.iov_base
= acb
->block_buffer
;
628 acb
->hd_iov
.iov_len
= SECTOR_SIZE
;
629 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
630 ret
= bdrv_co_writev(bs
->file
, 0, 1, &acb
->hd_qiov
);
632 if (ret
>= 0 && VDI_IS_ALLOCATED(acb
->bmap_first
)) {
633 /* One or more new blocks were allocated. */
637 g_free(acb
->block_buffer
);
638 acb
->block_buffer
= NULL
;
639 bmap_first
= acb
->bmap_first
;
640 bmap_last
= acb
->bmap_last
;
641 logout("now writing modified block map entry %u...%u\n",
642 bmap_first
, bmap_last
);
643 /* Write modified sectors from block map. */
644 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
645 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
646 n_sectors
= bmap_last
- bmap_first
+ 1;
647 offset
= s
->bmap_sector
+ bmap_first
;
648 acb
->bmap_first
= VDI_UNALLOCATED
;
649 acb
->hd_iov
.iov_base
= (void *)((uint8_t *)&s
->bmap
[0] +
650 bmap_first
* SECTOR_SIZE
);
651 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
652 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
653 logout("will write %u block map sectors starting from entry %u\n",
654 n_sectors
, bmap_first
);
655 ret
= bdrv_co_writev(bs
->file
, offset
, n_sectors
, &acb
->hd_qiov
);
660 logout("%u sectors written\n", acb
->n_sectors
);
662 block_index
= acb
->sector_num
/ s
->block_sectors
;
663 sector_in_block
= acb
->sector_num
% s
->block_sectors
;
664 n_sectors
= s
->block_sectors
- sector_in_block
;
665 if (n_sectors
> acb
->nb_sectors
) {
666 n_sectors
= acb
->nb_sectors
;
669 logout("will write %u sectors starting at sector %" PRIu64
"\n",
670 n_sectors
, acb
->sector_num
);
672 /* prepare next AIO request */
673 acb
->n_sectors
= n_sectors
;
674 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
675 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
676 /* Allocate new block and write to it. */
679 bmap_entry
= s
->header
.blocks_allocated
;
680 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
681 s
->header
.blocks_allocated
++;
682 offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
683 (uint64_t)bmap_entry
* s
->block_sectors
;
684 block
= acb
->block_buffer
;
686 block
= g_malloc(s
->block_size
);
687 acb
->block_buffer
= block
;
688 acb
->bmap_first
= block_index
;
689 assert(!acb
->header_modified
);
690 acb
->header_modified
= 1;
692 acb
->bmap_last
= block_index
;
693 /* Copy data to be written to new block and zero unused parts. */
694 memset(block
, 0, sector_in_block
* SECTOR_SIZE
);
695 memcpy(block
+ sector_in_block
* SECTOR_SIZE
,
696 acb
->buf
, n_sectors
* SECTOR_SIZE
);
697 memset(block
+ (sector_in_block
+ n_sectors
) * SECTOR_SIZE
, 0,
698 (s
->block_sectors
- n_sectors
- sector_in_block
) * SECTOR_SIZE
);
699 acb
->hd_iov
.iov_base
= (void *)block
;
700 acb
->hd_iov
.iov_len
= s
->block_size
;
701 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
702 ret
= bdrv_co_writev(bs
->file
, offset
, s
->block_sectors
, &acb
->hd_qiov
);
704 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
705 (uint64_t)bmap_entry
* s
->block_sectors
+
707 acb
->hd_iov
.iov_base
= (void *)acb
->buf
;
708 acb
->hd_iov
.iov_len
= n_sectors
* SECTOR_SIZE
;
709 qemu_iovec_init_external(&acb
->hd_qiov
, &acb
->hd_iov
, 1);
710 ret
= bdrv_co_writev(bs
->file
, offset
, n_sectors
, &acb
->hd_qiov
);
717 if (acb
->qiov
->niov
> 1) {
718 qemu_vfree(acb
->orig_buf
);
720 qemu_aio_release(acb
);
724 static int vdi_co_writev(BlockDriverState
*bs
,
725 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
)
731 acb
= vdi_aio_setup(bs
, sector_num
, qiov
, nb_sectors
, 1);
732 ret
= vdi_aio_write_cb(acb
, 0);
736 static int vdi_create(const char *filename
, QEMUOptionParameter
*options
)
742 size_t block_size
= DEFAULT_CLUSTER_SIZE
;
743 uint32_t image_type
= VDI_TYPE_DYNAMIC
;
751 /* Read out options. */
752 while (options
&& options
->name
) {
753 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
754 bytes
= options
->value
.n
;
755 #if defined(CONFIG_VDI_BLOCK_SIZE)
756 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
757 if (options
->value
.n
) {
758 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
759 block_size
= options
->value
.n
;
762 #if defined(CONFIG_VDI_STATIC_IMAGE)
763 } else if (!strcmp(options
->name
, BLOCK_OPT_STATIC
)) {
764 if (options
->value
.n
) {
765 image_type
= VDI_TYPE_STATIC
;
772 fd
= open(filename
, O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
778 /* We need enough blocks to store the given disk size,
779 so always round up. */
780 blocks
= (bytes
+ block_size
- 1) / block_size
;
782 bmap_size
= blocks
* sizeof(uint32_t);
783 bmap_size
= ((bmap_size
+ SECTOR_SIZE
- 1) & ~(SECTOR_SIZE
-1));
785 memset(&header
, 0, sizeof(header
));
786 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
787 header
.signature
= VDI_SIGNATURE
;
788 header
.version
= VDI_VERSION_1_1
;
789 header
.header_size
= 0x180;
790 header
.image_type
= image_type
;
791 header
.offset_bmap
= 0x200;
792 header
.offset_data
= 0x200 + bmap_size
;
793 header
.sector_size
= SECTOR_SIZE
;
794 header
.disk_size
= bytes
;
795 header
.block_size
= block_size
;
796 header
.blocks_in_image
= blocks
;
797 if (image_type
== VDI_TYPE_STATIC
) {
798 header
.blocks_allocated
= blocks
;
800 uuid_generate(header
.uuid_image
);
801 uuid_generate(header
.uuid_last_snap
);
802 /* There is no need to set header.uuid_link or header.uuid_parent here. */
803 #if defined(CONFIG_VDI_DEBUG)
804 vdi_header_print(&header
);
806 vdi_header_to_le(&header
);
807 if (write(fd
, &header
, sizeof(header
)) < 0) {
813 bmap
= (uint32_t *)g_malloc0(bmap_size
);
815 for (i
= 0; i
< blocks
; i
++) {
816 if (image_type
== VDI_TYPE_STATIC
) {
819 bmap
[i
] = VDI_UNALLOCATED
;
822 if (write(fd
, bmap
, bmap_size
) < 0) {
826 if (image_type
== VDI_TYPE_STATIC
) {
827 if (ftruncate(fd
, sizeof(header
) + bmap_size
+ blocks
* block_size
)) {
839 static void vdi_close(BlockDriverState
*bs
)
841 BDRVVdiState
*s
= bs
->opaque
;
845 migrate_del_blocker(s
->migration_blocker
);
846 error_free(s
->migration_blocker
);
849 static QEMUOptionParameter vdi_create_options
[] = {
851 .name
= BLOCK_OPT_SIZE
,
853 .help
= "Virtual disk size"
855 #if defined(CONFIG_VDI_BLOCK_SIZE)
857 .name
= BLOCK_OPT_CLUSTER_SIZE
,
859 .help
= "VDI cluster (block) size",
860 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
863 #if defined(CONFIG_VDI_STATIC_IMAGE)
865 .name
= BLOCK_OPT_STATIC
,
867 .help
= "VDI static (pre-allocated) image"
870 /* TODO: An additional option to set UUID values might be useful. */
874 static BlockDriver bdrv_vdi
= {
875 .format_name
= "vdi",
876 .instance_size
= sizeof(BDRVVdiState
),
877 .bdrv_probe
= vdi_probe
,
878 .bdrv_open
= vdi_open
,
879 .bdrv_close
= vdi_close
,
880 .bdrv_create
= vdi_create
,
881 .bdrv_co_is_allocated
= vdi_co_is_allocated
,
882 .bdrv_make_empty
= vdi_make_empty
,
884 .bdrv_co_readv
= vdi_co_readv
,
885 #if defined(CONFIG_VDI_WRITE)
886 .bdrv_co_writev
= vdi_co_writev
,
889 .bdrv_get_info
= vdi_get_info
,
891 .create_options
= vdi_create_options
,
892 .bdrv_check
= vdi_check
,
895 static void bdrv_vdi_init(void)
898 bdrv_register(&bdrv_vdi
);
901 block_init(bdrv_vdi_init
);