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 adjacent 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/osdep.h"
53 #include "qapi/error.h"
54 #include "block/block_int.h"
55 #include "sysemu/block-backend.h"
56 #include "qemu/module.h"
57 #include "qemu/option.h"
58 #include "qemu/bswap.h"
59 #include "migration/blocker.h"
60 #include "qemu/coroutine.h"
61 #include "qemu/cutils.h"
62 #include "qemu/uuid.h"
64 /* Code configuration options. */
66 /* Enable debug messages. */
67 //~ #define CONFIG_VDI_DEBUG
69 /* Support write operations on VDI images. */
70 #define CONFIG_VDI_WRITE
72 /* Support non-standard block (cluster) size. This is untested.
73 * Maybe it will be needed for very large images.
75 //~ #define CONFIG_VDI_BLOCK_SIZE
77 /* Support static (fixed, pre-allocated) images. */
78 #define CONFIG_VDI_STATIC_IMAGE
80 /* Command line option for static images. */
81 #define BLOCK_OPT_STATIC "static"
84 #define MiB (KiB * KiB)
86 #define SECTOR_SIZE 512
87 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
89 #if defined(CONFIG_VDI_DEBUG)
90 #define logout(fmt, ...) \
91 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
93 #define logout(fmt, ...) ((void)0)
96 /* Image signature. */
97 #define VDI_SIGNATURE 0xbeda107f
100 #define VDI_VERSION_1_1 0x00010001
103 #define VDI_TYPE_DYNAMIC 1
104 #define VDI_TYPE_STATIC 2
106 /* Innotek / SUN images use these strings in header.text:
107 * "<<< innotek VirtualBox Disk Image >>>\n"
108 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
109 * "<<< Sun VirtualBox Disk Image >>>\n"
110 * The value does not matter, so QEMU created images use a different text.
112 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
114 /* A never-allocated block; semantically arbitrary content. */
115 #define VDI_UNALLOCATED 0xffffffffU
117 /* A discarded (no longer allocated) block; semantically zero-filled. */
118 #define VDI_DISCARDED 0xfffffffeU
120 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
122 /* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since
123 * the bmap is read and written in a single operation, its size needs to be
124 * limited to INT_MAX; furthermore, when opening an image, the bmap size is
125 * rounded up to be aligned on BDRV_SECTOR_SIZE.
126 * Therefore this should satisfy the following:
127 * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1
128 * (INT_MAX + 1 is the first value not representable as an int)
129 * This guarantees that any value below or equal to the constant will, when
130 * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary,
131 * still be below or equal to INT_MAX. */
132 #define VDI_BLOCKS_IN_IMAGE_MAX \
133 ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t)))
134 #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
135 (uint64_t)DEFAULT_CLUSTER_SIZE)
141 uint32_t header_size
;
143 uint32_t image_flags
;
144 char description
[256];
145 uint32_t offset_bmap
;
146 uint32_t offset_data
;
147 uint32_t cylinders
; /* disk geometry, unused here */
148 uint32_t heads
; /* disk geometry, unused here */
149 uint32_t sectors
; /* disk geometry, unused here */
150 uint32_t sector_size
;
154 uint32_t block_extra
; /* unused here */
155 uint32_t blocks_in_image
;
156 uint32_t blocks_allocated
;
158 QemuUUID uuid_last_snap
;
160 QemuUUID uuid_parent
;
162 } QEMU_PACKED VdiHeader
;
165 /* The block map entries are little endian (even in memory). */
167 /* Size of block (bytes). */
169 /* Size of block (sectors). */
170 uint32_t block_sectors
;
171 /* First sector of block map. */
172 uint32_t bmap_sector
;
173 /* VDI header (converted to host endianness). */
178 Error
*migration_blocker
;
181 static void vdi_header_to_cpu(VdiHeader
*header
)
183 le32_to_cpus(&header
->signature
);
184 le32_to_cpus(&header
->version
);
185 le32_to_cpus(&header
->header_size
);
186 le32_to_cpus(&header
->image_type
);
187 le32_to_cpus(&header
->image_flags
);
188 le32_to_cpus(&header
->offset_bmap
);
189 le32_to_cpus(&header
->offset_data
);
190 le32_to_cpus(&header
->cylinders
);
191 le32_to_cpus(&header
->heads
);
192 le32_to_cpus(&header
->sectors
);
193 le32_to_cpus(&header
->sector_size
);
194 le64_to_cpus(&header
->disk_size
);
195 le32_to_cpus(&header
->block_size
);
196 le32_to_cpus(&header
->block_extra
);
197 le32_to_cpus(&header
->blocks_in_image
);
198 le32_to_cpus(&header
->blocks_allocated
);
199 qemu_uuid_bswap(&header
->uuid_image
);
200 qemu_uuid_bswap(&header
->uuid_last_snap
);
201 qemu_uuid_bswap(&header
->uuid_link
);
202 qemu_uuid_bswap(&header
->uuid_parent
);
205 static void vdi_header_to_le(VdiHeader
*header
)
207 cpu_to_le32s(&header
->signature
);
208 cpu_to_le32s(&header
->version
);
209 cpu_to_le32s(&header
->header_size
);
210 cpu_to_le32s(&header
->image_type
);
211 cpu_to_le32s(&header
->image_flags
);
212 cpu_to_le32s(&header
->offset_bmap
);
213 cpu_to_le32s(&header
->offset_data
);
214 cpu_to_le32s(&header
->cylinders
);
215 cpu_to_le32s(&header
->heads
);
216 cpu_to_le32s(&header
->sectors
);
217 cpu_to_le32s(&header
->sector_size
);
218 cpu_to_le64s(&header
->disk_size
);
219 cpu_to_le32s(&header
->block_size
);
220 cpu_to_le32s(&header
->block_extra
);
221 cpu_to_le32s(&header
->blocks_in_image
);
222 cpu_to_le32s(&header
->blocks_allocated
);
223 qemu_uuid_bswap(&header
->uuid_image
);
224 qemu_uuid_bswap(&header
->uuid_last_snap
);
225 qemu_uuid_bswap(&header
->uuid_link
);
226 qemu_uuid_bswap(&header
->uuid_parent
);
229 #if defined(CONFIG_VDI_DEBUG)
230 static void vdi_header_print(VdiHeader
*header
)
233 logout("text %s", header
->text
);
234 logout("signature 0x%08x\n", header
->signature
);
235 logout("header size 0x%04x\n", header
->header_size
);
236 logout("image type 0x%04x\n", header
->image_type
);
237 logout("image flags 0x%04x\n", header
->image_flags
);
238 logout("description %s\n", header
->description
);
239 logout("offset bmap 0x%04x\n", header
->offset_bmap
);
240 logout("offset data 0x%04x\n", header
->offset_data
);
241 logout("cylinders 0x%04x\n", header
->cylinders
);
242 logout("heads 0x%04x\n", header
->heads
);
243 logout("sectors 0x%04x\n", header
->sectors
);
244 logout("sector size 0x%04x\n", header
->sector_size
);
245 logout("image size 0x%" PRIx64
" B (%" PRIu64
" MiB)\n",
246 header
->disk_size
, header
->disk_size
/ MiB
);
247 logout("block size 0x%04x\n", header
->block_size
);
248 logout("block extra 0x%04x\n", header
->block_extra
);
249 logout("blocks tot. 0x%04x\n", header
->blocks_in_image
);
250 logout("blocks all. 0x%04x\n", header
->blocks_allocated
);
251 uuid_unparse(header
->uuid_image
, uuid
);
252 logout("uuid image %s\n", uuid
);
253 uuid_unparse(header
->uuid_last_snap
, uuid
);
254 logout("uuid snap %s\n", uuid
);
255 uuid_unparse(header
->uuid_link
, uuid
);
256 logout("uuid link %s\n", uuid
);
257 uuid_unparse(header
->uuid_parent
, uuid
);
258 logout("uuid parent %s\n", uuid
);
262 static int vdi_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
265 /* TODO: additional checks possible. */
266 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
267 uint32_t blocks_allocated
= 0;
276 bmap
= g_try_new(uint32_t, s
->header
.blocks_in_image
);
277 if (s
->header
.blocks_in_image
&& bmap
== NULL
) {
282 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
284 /* Check block map and value of blocks_allocated. */
285 for (block
= 0; block
< s
->header
.blocks_in_image
; block
++) {
286 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[block
]);
287 if (VDI_IS_ALLOCATED(bmap_entry
)) {
288 if (bmap_entry
< s
->header
.blocks_in_image
) {
290 if (!VDI_IS_ALLOCATED(bmap
[bmap_entry
])) {
291 bmap
[bmap_entry
] = bmap_entry
;
293 fprintf(stderr
, "ERROR: block index %" PRIu32
294 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
298 fprintf(stderr
, "ERROR: block index %" PRIu32
299 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
304 if (blocks_allocated
!= s
->header
.blocks_allocated
) {
305 fprintf(stderr
, "ERROR: allocated blocks mismatch, is %" PRIu32
306 ", should be %" PRIu32
"\n",
307 blocks_allocated
, s
->header
.blocks_allocated
);
316 static int vdi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
318 /* TODO: vdi_get_info would be needed for machine snapshots.
319 vm_state_offset is still missing. */
320 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
322 bdi
->cluster_size
= s
->block_size
;
323 bdi
->vm_state_offset
= 0;
324 bdi
->unallocated_blocks_are_zero
= true;
328 static int vdi_make_empty(BlockDriverState
*bs
)
330 /* TODO: missing code. */
332 /* The return value for missing code must be 0, see block.c. */
336 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
338 const VdiHeader
*header
= (const VdiHeader
*)buf
;
343 if (buf_size
< sizeof(*header
)) {
344 /* Header too small, no VDI. */
345 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
350 logout("no vdi image\n");
352 logout("%s", header
->text
);
358 static int vdi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
361 BDRVVdiState
*s
= bs
->opaque
;
365 Error
*local_err
= NULL
;
367 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
375 ret
= bdrv_read(bs
->file
, 0, (uint8_t *)&header
, 1);
380 vdi_header_to_cpu(&header
);
381 #if defined(CONFIG_VDI_DEBUG)
382 vdi_header_print(&header
);
385 if (header
.disk_size
> VDI_DISK_SIZE_MAX
) {
386 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
387 ", max supported is 0x%" PRIx64
")",
388 header
.disk_size
, VDI_DISK_SIZE_MAX
);
393 if (header
.disk_size
% SECTOR_SIZE
!= 0) {
394 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
395 We accept them but round the disk size to the next multiple of
397 logout("odd disk size %" PRIu64
" B, round up\n", header
.disk_size
);
398 header
.disk_size
= ROUND_UP(header
.disk_size
, SECTOR_SIZE
);
401 if (header
.signature
!= VDI_SIGNATURE
) {
402 error_setg(errp
, "Image not in VDI format (bad signature %08" PRIx32
403 ")", header
.signature
);
406 } else if (header
.version
!= VDI_VERSION_1_1
) {
407 error_setg(errp
, "unsupported VDI image (version %" PRIu32
".%" PRIu32
408 ")", header
.version
>> 16, header
.version
& 0xffff);
411 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
412 /* We only support block maps which start on a sector boundary. */
413 error_setg(errp
, "unsupported VDI image (unaligned block map offset "
414 "0x%" PRIx32
")", header
.offset_bmap
);
417 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
418 /* We only support data blocks which start on a sector boundary. */
419 error_setg(errp
, "unsupported VDI image (unaligned data offset 0x%"
420 PRIx32
")", header
.offset_data
);
423 } else if (header
.sector_size
!= SECTOR_SIZE
) {
424 error_setg(errp
, "unsupported VDI image (sector size %" PRIu32
425 " is not %u)", header
.sector_size
, SECTOR_SIZE
);
428 } else if (header
.block_size
!= DEFAULT_CLUSTER_SIZE
) {
429 error_setg(errp
, "unsupported VDI image (block size %" PRIu32
430 " is not %u)", header
.block_size
, DEFAULT_CLUSTER_SIZE
);
433 } else if (header
.disk_size
>
434 (uint64_t)header
.blocks_in_image
* header
.block_size
) {
435 error_setg(errp
, "unsupported VDI image (disk size %" PRIu64
", "
436 "image bitmap has room for %" PRIu64
")",
438 (uint64_t)header
.blocks_in_image
* header
.block_size
);
441 } else if (!qemu_uuid_is_null(&header
.uuid_link
)) {
442 error_setg(errp
, "unsupported VDI image (non-NULL link UUID)");
445 } else if (!qemu_uuid_is_null(&header
.uuid_parent
)) {
446 error_setg(errp
, "unsupported VDI image (non-NULL parent UUID)");
449 } else if (header
.blocks_in_image
> VDI_BLOCKS_IN_IMAGE_MAX
) {
450 error_setg(errp
, "unsupported VDI image "
451 "(too many blocks %u, max is %u)",
452 header
.blocks_in_image
, VDI_BLOCKS_IN_IMAGE_MAX
);
457 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
459 s
->block_size
= header
.block_size
;
460 s
->block_sectors
= header
.block_size
/ SECTOR_SIZE
;
461 s
->bmap_sector
= header
.offset_bmap
/ SECTOR_SIZE
;
464 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
465 bmap_size
= DIV_ROUND_UP(bmap_size
, SECTOR_SIZE
);
466 s
->bmap
= qemu_try_blockalign(bs
->file
->bs
, bmap_size
* SECTOR_SIZE
);
467 if (s
->bmap
== NULL
) {
472 ret
= bdrv_read(bs
->file
, s
->bmap_sector
, (uint8_t *)s
->bmap
,
478 /* Disable migration when vdi images are used */
479 error_setg(&s
->migration_blocker
, "The vdi format used by node '%s' "
480 "does not support live migration",
481 bdrv_get_device_or_node_name(bs
));
482 ret
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
484 error_propagate(errp
, local_err
);
485 error_free(s
->migration_blocker
);
489 qemu_co_rwlock_init(&s
->bmap_lock
);
500 static int vdi_reopen_prepare(BDRVReopenState
*state
,
501 BlockReopenQueue
*queue
, Error
**errp
)
506 static int64_t coroutine_fn
vdi_co_get_block_status(BlockDriverState
*bs
,
507 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
509 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
510 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
511 size_t bmap_index
= sector_num
/ s
->block_sectors
;
512 size_t sector_in_block
= sector_num
% s
->block_sectors
;
513 int n_sectors
= s
->block_sectors
- sector_in_block
;
514 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
518 logout("%p, %" PRId64
", %d, %p\n", bs
, sector_num
, nb_sectors
, pnum
);
519 if (n_sectors
> nb_sectors
) {
520 n_sectors
= nb_sectors
;
523 result
= VDI_IS_ALLOCATED(bmap_entry
);
528 offset
= s
->header
.offset_data
+
529 (uint64_t)bmap_entry
* s
->block_size
+
530 sector_in_block
* SECTOR_SIZE
;
531 *file
= bs
->file
->bs
;
532 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
535 static int coroutine_fn
536 vdi_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
537 QEMUIOVector
*qiov
, int flags
)
539 BDRVVdiState
*s
= bs
->opaque
;
540 QEMUIOVector local_qiov
;
542 uint32_t block_index
;
543 uint32_t offset_in_block
;
545 uint64_t bytes_done
= 0;
550 qemu_iovec_init(&local_qiov
, qiov
->niov
);
552 while (ret
>= 0 && bytes
> 0) {
553 block_index
= offset
/ s
->block_size
;
554 offset_in_block
= offset
% s
->block_size
;
555 n_bytes
= MIN(bytes
, s
->block_size
- offset_in_block
);
557 logout("will read %u bytes starting at offset %" PRIu64
"\n",
560 /* prepare next AIO request */
561 qemu_co_rwlock_rdlock(&s
->bmap_lock
);
562 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
563 qemu_co_rwlock_unlock(&s
->bmap_lock
);
564 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
565 /* Block not allocated, return zeros, no need to wait. */
566 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
569 uint64_t data_offset
= s
->header
.offset_data
+
570 (uint64_t)bmap_entry
* s
->block_size
+
573 qemu_iovec_reset(&local_qiov
);
574 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
576 ret
= bdrv_co_preadv(bs
->file
, data_offset
, n_bytes
,
579 logout("%u bytes read\n", n_bytes
);
583 bytes_done
+= n_bytes
;
586 qemu_iovec_destroy(&local_qiov
);
591 static int coroutine_fn
592 vdi_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
593 QEMUIOVector
*qiov
, int flags
)
595 BDRVVdiState
*s
= bs
->opaque
;
596 QEMUIOVector local_qiov
;
598 uint32_t block_index
;
599 uint32_t offset_in_block
;
601 uint64_t data_offset
;
602 uint32_t bmap_first
= VDI_UNALLOCATED
;
603 uint32_t bmap_last
= VDI_UNALLOCATED
;
604 uint8_t *block
= NULL
;
605 uint64_t bytes_done
= 0;
610 qemu_iovec_init(&local_qiov
, qiov
->niov
);
612 while (ret
>= 0 && bytes
> 0) {
613 block_index
= offset
/ s
->block_size
;
614 offset_in_block
= offset
% s
->block_size
;
615 n_bytes
= MIN(bytes
, s
->block_size
- offset_in_block
);
617 logout("will write %u bytes starting at offset %" PRIu64
"\n",
620 /* prepare next AIO request */
621 qemu_co_rwlock_rdlock(&s
->bmap_lock
);
622 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
623 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
624 /* Allocate new block and write to it. */
625 uint64_t data_offset
;
626 qemu_co_rwlock_upgrade(&s
->bmap_lock
);
627 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
628 if (VDI_IS_ALLOCATED(bmap_entry
)) {
629 /* A concurrent allocation did the work for us. */
630 qemu_co_rwlock_downgrade(&s
->bmap_lock
);
631 goto nonallocating_write
;
634 bmap_entry
= s
->header
.blocks_allocated
;
635 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
636 s
->header
.blocks_allocated
++;
637 data_offset
= s
->header
.offset_data
+
638 (uint64_t)bmap_entry
* s
->block_size
;
640 block
= g_malloc(s
->block_size
);
641 bmap_first
= block_index
;
643 bmap_last
= block_index
;
644 /* Copy data to be written to new block and zero unused parts. */
645 memset(block
, 0, offset_in_block
);
646 qemu_iovec_to_buf(qiov
, bytes_done
, block
+ offset_in_block
,
648 memset(block
+ offset_in_block
+ n_bytes
, 0,
649 s
->block_size
- n_bytes
- offset_in_block
);
651 /* Write the new block under CoRwLock write-side protection,
652 * so this full-cluster write does not overlap a partial write
653 * of the same cluster, issued from the "else" branch.
655 ret
= bdrv_pwrite(bs
->file
, data_offset
, block
, s
->block_size
);
656 qemu_co_rwlock_unlock(&s
->bmap_lock
);
659 data_offset
= s
->header
.offset_data
+
660 (uint64_t)bmap_entry
* s
->block_size
+
662 qemu_co_rwlock_unlock(&s
->bmap_lock
);
664 qemu_iovec_reset(&local_qiov
);
665 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
667 ret
= bdrv_co_pwritev(bs
->file
, data_offset
, n_bytes
,
673 bytes_done
+= n_bytes
;
675 logout("%u bytes written\n", n_bytes
);
678 qemu_iovec_destroy(&local_qiov
);
680 logout("finished data write\n");
686 /* One or more new blocks were allocated. */
687 VdiHeader
*header
= (VdiHeader
*) block
;
692 logout("now writing modified header\n");
693 assert(VDI_IS_ALLOCATED(bmap_first
));
695 vdi_header_to_le(header
);
696 ret
= bdrv_write(bs
->file
, 0, block
, 1);
704 logout("now writing modified block map entry %u...%u\n",
705 bmap_first
, bmap_last
);
706 /* Write modified sectors from block map. */
707 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
708 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
709 n_sectors
= bmap_last
- bmap_first
+ 1;
710 offset
= s
->bmap_sector
+ bmap_first
;
711 base
= ((uint8_t *)&s
->bmap
[0]) + bmap_first
* SECTOR_SIZE
;
712 logout("will write %u block map sectors starting from entry %u\n",
713 n_sectors
, bmap_first
);
714 ret
= bdrv_write(bs
->file
, offset
, base
, n_sectors
);
720 static int vdi_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
725 size_t block_size
= DEFAULT_CLUSTER_SIZE
;
726 uint32_t image_type
= VDI_TYPE_DYNAMIC
;
731 Error
*local_err
= NULL
;
732 BlockBackend
*blk
= NULL
;
733 uint32_t *bmap
= NULL
;
737 /* Read out options. */
738 bytes
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
740 #if defined(CONFIG_VDI_BLOCK_SIZE)
741 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
742 block_size
= qemu_opt_get_size_del(opts
,
743 BLOCK_OPT_CLUSTER_SIZE
,
744 DEFAULT_CLUSTER_SIZE
);
746 #if defined(CONFIG_VDI_STATIC_IMAGE)
747 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_STATIC
, false)) {
748 image_type
= VDI_TYPE_STATIC
;
752 if (bytes
> VDI_DISK_SIZE_MAX
) {
754 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
755 ", max supported is 0x%" PRIx64
")",
756 bytes
, VDI_DISK_SIZE_MAX
);
760 ret
= bdrv_create_file(filename
, opts
, &local_err
);
762 error_propagate(errp
, local_err
);
766 blk
= blk_new_open(filename
, NULL
, NULL
,
767 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
,
770 error_propagate(errp
, local_err
);
775 blk_set_allow_write_beyond_eof(blk
, true);
777 /* We need enough blocks to store the given disk size,
778 so always round up. */
779 blocks
= DIV_ROUND_UP(bytes
, block_size
);
781 bmap_size
= blocks
* sizeof(uint32_t);
782 bmap_size
= ROUND_UP(bmap_size
, SECTOR_SIZE
);
784 memset(&header
, 0, sizeof(header
));
785 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
786 header
.signature
= VDI_SIGNATURE
;
787 header
.version
= VDI_VERSION_1_1
;
788 header
.header_size
= 0x180;
789 header
.image_type
= image_type
;
790 header
.offset_bmap
= 0x200;
791 header
.offset_data
= 0x200 + bmap_size
;
792 header
.sector_size
= SECTOR_SIZE
;
793 header
.disk_size
= bytes
;
794 header
.block_size
= block_size
;
795 header
.blocks_in_image
= blocks
;
796 if (image_type
== VDI_TYPE_STATIC
) {
797 header
.blocks_allocated
= blocks
;
799 qemu_uuid_generate(&header
.uuid_image
);
800 qemu_uuid_generate(&header
.uuid_last_snap
);
801 /* There is no need to set header.uuid_link or header.uuid_parent here. */
802 #if defined(CONFIG_VDI_DEBUG)
803 vdi_header_print(&header
);
805 vdi_header_to_le(&header
);
806 ret
= blk_pwrite(blk
, offset
, &header
, sizeof(header
), 0);
808 error_setg(errp
, "Error writing header to %s", filename
);
811 offset
+= sizeof(header
);
814 bmap
= g_try_malloc0(bmap_size
);
817 error_setg(errp
, "Could not allocate bmap");
820 for (i
= 0; i
< blocks
; i
++) {
821 if (image_type
== VDI_TYPE_STATIC
) {
824 bmap
[i
] = VDI_UNALLOCATED
;
827 ret
= blk_pwrite(blk
, offset
, bmap
, bmap_size
, 0);
829 error_setg(errp
, "Error writing bmap to %s", filename
);
835 if (image_type
== VDI_TYPE_STATIC
) {
836 ret
= blk_truncate(blk
, offset
+ blocks
* block_size
,
837 PREALLOC_MODE_OFF
, errp
);
839 error_prepend(errp
, "Failed to statically allocate %s", filename
);
850 static void vdi_close(BlockDriverState
*bs
)
852 BDRVVdiState
*s
= bs
->opaque
;
856 migrate_del_blocker(s
->migration_blocker
);
857 error_free(s
->migration_blocker
);
860 static QemuOptsList vdi_create_opts
= {
861 .name
= "vdi-create-opts",
862 .head
= QTAILQ_HEAD_INITIALIZER(vdi_create_opts
.head
),
865 .name
= BLOCK_OPT_SIZE
,
866 .type
= QEMU_OPT_SIZE
,
867 .help
= "Virtual disk size"
869 #if defined(CONFIG_VDI_BLOCK_SIZE)
871 .name
= BLOCK_OPT_CLUSTER_SIZE
,
872 .type
= QEMU_OPT_SIZE
,
873 .help
= "VDI cluster (block) size",
874 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
877 #if defined(CONFIG_VDI_STATIC_IMAGE)
879 .name
= BLOCK_OPT_STATIC
,
880 .type
= QEMU_OPT_BOOL
,
881 .help
= "VDI static (pre-allocated) image",
882 .def_value_str
= "off"
885 /* TODO: An additional option to set UUID values might be useful. */
886 { /* end of list */ }
890 static BlockDriver bdrv_vdi
= {
891 .format_name
= "vdi",
892 .instance_size
= sizeof(BDRVVdiState
),
893 .bdrv_probe
= vdi_probe
,
894 .bdrv_open
= vdi_open
,
895 .bdrv_close
= vdi_close
,
896 .bdrv_reopen_prepare
= vdi_reopen_prepare
,
897 .bdrv_child_perm
= bdrv_format_default_perms
,
898 .bdrv_create
= vdi_create
,
899 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
900 .bdrv_co_get_block_status
= vdi_co_get_block_status
,
901 .bdrv_make_empty
= vdi_make_empty
,
903 .bdrv_co_preadv
= vdi_co_preadv
,
904 #if defined(CONFIG_VDI_WRITE)
905 .bdrv_co_pwritev
= vdi_co_pwritev
,
908 .bdrv_get_info
= vdi_get_info
,
910 .create_opts
= &vdi_create_opts
,
911 .bdrv_check
= vdi_check
,
914 static void bdrv_vdi_init(void)
917 bdrv_register(&bdrv_vdi
);
920 block_init(bdrv_vdi_init
);