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 "migration/migration.h"
58 #include "qemu/coroutine.h"
59 #include "qemu/cutils.h"
61 #if defined(CONFIG_UUID)
62 #include <uuid/uuid.h>
64 /* TODO: move uuid emulation to some central place in QEMU. */
65 #include "sysemu/sysemu.h" /* UUID_FMT */
66 typedef unsigned char uuid_t
[16];
69 /* Code configuration options. */
71 /* Enable debug messages. */
72 //~ #define CONFIG_VDI_DEBUG
74 /* Support write operations on VDI images. */
75 #define CONFIG_VDI_WRITE
77 /* Support non-standard block (cluster) size. This is untested.
78 * Maybe it will be needed for very large images.
80 //~ #define CONFIG_VDI_BLOCK_SIZE
82 /* Support static (fixed, pre-allocated) images. */
83 #define CONFIG_VDI_STATIC_IMAGE
85 /* Command line option for static images. */
86 #define BLOCK_OPT_STATIC "static"
89 #define MiB (KiB * KiB)
91 #define SECTOR_SIZE 512
92 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
94 #if defined(CONFIG_VDI_DEBUG)
95 #define logout(fmt, ...) \
96 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
98 #define logout(fmt, ...) ((void)0)
101 /* Image signature. */
102 #define VDI_SIGNATURE 0xbeda107f
105 #define VDI_VERSION_1_1 0x00010001
108 #define VDI_TYPE_DYNAMIC 1
109 #define VDI_TYPE_STATIC 2
111 /* Innotek / SUN images use these strings in header.text:
112 * "<<< innotek VirtualBox Disk Image >>>\n"
113 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
114 * "<<< Sun VirtualBox Disk Image >>>\n"
115 * The value does not matter, so QEMU created images use a different text.
117 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
119 /* A never-allocated block; semantically arbitrary content. */
120 #define VDI_UNALLOCATED 0xffffffffU
122 /* A discarded (no longer allocated) block; semantically zero-filled. */
123 #define VDI_DISCARDED 0xfffffffeU
125 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
127 /* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since
128 * the bmap is read and written in a single operation, its size needs to be
129 * limited to INT_MAX; furthermore, when opening an image, the bmap size is
130 * rounded up to be aligned on BDRV_SECTOR_SIZE.
131 * Therefore this should satisfy the following:
132 * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1
133 * (INT_MAX + 1 is the first value not representable as an int)
134 * This guarantees that any value below or equal to the constant will, when
135 * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary,
136 * still be below or equal to INT_MAX. */
137 #define VDI_BLOCKS_IN_IMAGE_MAX \
138 ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t)))
139 #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
140 (uint64_t)DEFAULT_CLUSTER_SIZE)
142 #if !defined(CONFIG_UUID)
143 static inline void uuid_generate(uuid_t out
)
145 memset(out
, 0, sizeof(uuid_t
));
148 static inline int uuid_is_null(const uuid_t uu
)
150 uuid_t null_uuid
= { 0 };
151 return memcmp(uu
, null_uuid
, sizeof(uuid_t
)) == 0;
154 # if defined(CONFIG_VDI_DEBUG)
155 static inline void uuid_unparse(const uuid_t uu
, char *out
)
157 snprintf(out
, 37, UUID_FMT
,
158 uu
[0], uu
[1], uu
[2], uu
[3], uu
[4], uu
[5], uu
[6], uu
[7],
159 uu
[8], uu
[9], uu
[10], uu
[11], uu
[12], uu
[13], uu
[14], uu
[15]);
168 uint32_t header_size
;
170 uint32_t image_flags
;
171 char description
[256];
172 uint32_t offset_bmap
;
173 uint32_t offset_data
;
174 uint32_t cylinders
; /* disk geometry, unused here */
175 uint32_t heads
; /* disk geometry, unused here */
176 uint32_t sectors
; /* disk geometry, unused here */
177 uint32_t sector_size
;
181 uint32_t block_extra
; /* unused here */
182 uint32_t blocks_in_image
;
183 uint32_t blocks_allocated
;
185 uuid_t uuid_last_snap
;
189 } QEMU_PACKED VdiHeader
;
192 /* The block map entries are little endian (even in memory). */
194 /* Size of block (bytes). */
196 /* Size of block (sectors). */
197 uint32_t block_sectors
;
198 /* First sector of block map. */
199 uint32_t bmap_sector
;
200 /* VDI header (converted to host endianness). */
205 Error
*migration_blocker
;
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 uuid_convert(header
->uuid_image
);
261 uuid_convert(header
->uuid_last_snap
);
262 uuid_convert(header
->uuid_link
);
263 uuid_convert(header
->uuid_parent
);
266 #if defined(CONFIG_VDI_DEBUG)
267 static void vdi_header_print(VdiHeader
*header
)
270 logout("text %s", header
->text
);
271 logout("signature 0x%08x\n", header
->signature
);
272 logout("header size 0x%04x\n", header
->header_size
);
273 logout("image type 0x%04x\n", header
->image_type
);
274 logout("image flags 0x%04x\n", header
->image_flags
);
275 logout("description %s\n", header
->description
);
276 logout("offset bmap 0x%04x\n", header
->offset_bmap
);
277 logout("offset data 0x%04x\n", header
->offset_data
);
278 logout("cylinders 0x%04x\n", header
->cylinders
);
279 logout("heads 0x%04x\n", header
->heads
);
280 logout("sectors 0x%04x\n", header
->sectors
);
281 logout("sector size 0x%04x\n", header
->sector_size
);
282 logout("image size 0x%" PRIx64
" B (%" PRIu64
" MiB)\n",
283 header
->disk_size
, header
->disk_size
/ MiB
);
284 logout("block size 0x%04x\n", header
->block_size
);
285 logout("block extra 0x%04x\n", header
->block_extra
);
286 logout("blocks tot. 0x%04x\n", header
->blocks_in_image
);
287 logout("blocks all. 0x%04x\n", header
->blocks_allocated
);
288 uuid_unparse(header
->uuid_image
, uuid
);
289 logout("uuid image %s\n", uuid
);
290 uuid_unparse(header
->uuid_last_snap
, uuid
);
291 logout("uuid snap %s\n", uuid
);
292 uuid_unparse(header
->uuid_link
, uuid
);
293 logout("uuid link %s\n", uuid
);
294 uuid_unparse(header
->uuid_parent
, uuid
);
295 logout("uuid parent %s\n", uuid
);
299 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;
313 bmap
= g_try_new(uint32_t, s
->header
.blocks_in_image
);
314 if (s
->header
.blocks_in_image
&& bmap
== NULL
) {
319 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
321 /* Check block map and value of blocks_allocated. */
322 for (block
= 0; block
< s
->header
.blocks_in_image
; block
++) {
323 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[block
]);
324 if (VDI_IS_ALLOCATED(bmap_entry
)) {
325 if (bmap_entry
< s
->header
.blocks_in_image
) {
327 if (!VDI_IS_ALLOCATED(bmap
[bmap_entry
])) {
328 bmap
[bmap_entry
] = bmap_entry
;
330 fprintf(stderr
, "ERROR: block index %" PRIu32
331 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
335 fprintf(stderr
, "ERROR: block index %" PRIu32
336 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
341 if (blocks_allocated
!= s
->header
.blocks_allocated
) {
342 fprintf(stderr
, "ERROR: allocated blocks mismatch, is %" PRIu32
343 ", should be %" PRIu32
"\n",
344 blocks_allocated
, s
->header
.blocks_allocated
);
353 static int vdi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
355 /* TODO: vdi_get_info would be needed for machine snapshots.
356 vm_state_offset is still missing. */
357 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
359 bdi
->cluster_size
= s
->block_size
;
360 bdi
->vm_state_offset
= 0;
361 bdi
->unallocated_blocks_are_zero
= true;
365 static int vdi_make_empty(BlockDriverState
*bs
)
367 /* TODO: missing code. */
369 /* The return value for missing code must be 0, see block.c. */
373 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
375 const VdiHeader
*header
= (const VdiHeader
*)buf
;
380 if (buf_size
< sizeof(*header
)) {
381 /* Header too small, no VDI. */
382 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
387 logout("no vdi image\n");
389 logout("%s", header
->text
);
395 static int vdi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
398 BDRVVdiState
*s
= bs
->opaque
;
405 ret
= bdrv_read(bs
->file
->bs
, 0, (uint8_t *)&header
, 1);
410 vdi_header_to_cpu(&header
);
411 #if defined(CONFIG_VDI_DEBUG)
412 vdi_header_print(&header
);
415 if (header
.disk_size
> VDI_DISK_SIZE_MAX
) {
416 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
417 ", max supported is 0x%" PRIx64
")",
418 header
.disk_size
, VDI_DISK_SIZE_MAX
);
423 if (header
.disk_size
% SECTOR_SIZE
!= 0) {
424 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
425 We accept them but round the disk size to the next multiple of
427 logout("odd disk size %" PRIu64
" B, round up\n", header
.disk_size
);
428 header
.disk_size
= ROUND_UP(header
.disk_size
, SECTOR_SIZE
);
431 if (header
.signature
!= VDI_SIGNATURE
) {
432 error_setg(errp
, "Image not in VDI format (bad signature %08" PRIx32
433 ")", header
.signature
);
436 } else if (header
.version
!= VDI_VERSION_1_1
) {
437 error_setg(errp
, "unsupported VDI image (version %" PRIu32
".%" PRIu32
438 ")", header
.version
>> 16, header
.version
& 0xffff);
441 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
442 /* We only support block maps which start on a sector boundary. */
443 error_setg(errp
, "unsupported VDI image (unaligned block map offset "
444 "0x%" PRIx32
")", header
.offset_bmap
);
447 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
448 /* We only support data blocks which start on a sector boundary. */
449 error_setg(errp
, "unsupported VDI image (unaligned data offset 0x%"
450 PRIx32
")", header
.offset_data
);
453 } else if (header
.sector_size
!= SECTOR_SIZE
) {
454 error_setg(errp
, "unsupported VDI image (sector size %" PRIu32
455 " is not %u)", header
.sector_size
, SECTOR_SIZE
);
458 } else if (header
.block_size
!= DEFAULT_CLUSTER_SIZE
) {
459 error_setg(errp
, "unsupported VDI image (block size %" PRIu32
460 " is not %u)", header
.block_size
, DEFAULT_CLUSTER_SIZE
);
463 } else if (header
.disk_size
>
464 (uint64_t)header
.blocks_in_image
* header
.block_size
) {
465 error_setg(errp
, "unsupported VDI image (disk size %" PRIu64
", "
466 "image bitmap has room for %" PRIu64
")",
468 (uint64_t)header
.blocks_in_image
* header
.block_size
);
471 } else if (!uuid_is_null(header
.uuid_link
)) {
472 error_setg(errp
, "unsupported VDI image (non-NULL link UUID)");
475 } else if (!uuid_is_null(header
.uuid_parent
)) {
476 error_setg(errp
, "unsupported VDI image (non-NULL parent UUID)");
479 } else if (header
.blocks_in_image
> VDI_BLOCKS_IN_IMAGE_MAX
) {
480 error_setg(errp
, "unsupported VDI image "
481 "(too many blocks %u, max is %u)",
482 header
.blocks_in_image
, VDI_BLOCKS_IN_IMAGE_MAX
);
487 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
489 s
->block_size
= header
.block_size
;
490 s
->block_sectors
= header
.block_size
/ SECTOR_SIZE
;
491 s
->bmap_sector
= header
.offset_bmap
/ SECTOR_SIZE
;
494 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
495 bmap_size
= DIV_ROUND_UP(bmap_size
, SECTOR_SIZE
);
496 s
->bmap
= qemu_try_blockalign(bs
->file
->bs
, bmap_size
* SECTOR_SIZE
);
497 if (s
->bmap
== NULL
) {
502 ret
= bdrv_read(bs
->file
->bs
, s
->bmap_sector
, (uint8_t *)s
->bmap
,
508 /* Disable migration when vdi images are used */
509 error_setg(&s
->migration_blocker
, "The vdi format used by node '%s' "
510 "does not support live migration",
511 bdrv_get_device_or_node_name(bs
));
512 migrate_add_blocker(s
->migration_blocker
);
514 qemu_co_mutex_init(&s
->write_lock
);
525 static int vdi_reopen_prepare(BDRVReopenState
*state
,
526 BlockReopenQueue
*queue
, Error
**errp
)
531 static int64_t coroutine_fn
vdi_co_get_block_status(BlockDriverState
*bs
,
532 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
534 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
535 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
536 size_t bmap_index
= sector_num
/ s
->block_sectors
;
537 size_t sector_in_block
= sector_num
% s
->block_sectors
;
538 int n_sectors
= s
->block_sectors
- sector_in_block
;
539 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
543 logout("%p, %" PRId64
", %d, %p\n", bs
, sector_num
, nb_sectors
, pnum
);
544 if (n_sectors
> nb_sectors
) {
545 n_sectors
= nb_sectors
;
548 result
= VDI_IS_ALLOCATED(bmap_entry
);
553 offset
= s
->header
.offset_data
+
554 (uint64_t)bmap_entry
* s
->block_size
+
555 sector_in_block
* SECTOR_SIZE
;
556 *file
= bs
->file
->bs
;
557 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| offset
;
560 static int vdi_co_read(BlockDriverState
*bs
,
561 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
563 BDRVVdiState
*s
= bs
->opaque
;
565 uint32_t block_index
;
566 uint32_t sector_in_block
;
572 while (ret
>= 0 && nb_sectors
> 0) {
573 block_index
= sector_num
/ s
->block_sectors
;
574 sector_in_block
= sector_num
% s
->block_sectors
;
575 n_sectors
= s
->block_sectors
- sector_in_block
;
576 if (n_sectors
> nb_sectors
) {
577 n_sectors
= nb_sectors
;
580 logout("will read %u sectors starting at sector %" PRIu64
"\n",
581 n_sectors
, sector_num
);
583 /* prepare next AIO request */
584 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
585 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
586 /* Block not allocated, return zeros, no need to wait. */
587 memset(buf
, 0, n_sectors
* SECTOR_SIZE
);
590 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
591 (uint64_t)bmap_entry
* s
->block_sectors
+
593 ret
= bdrv_read(bs
->file
->bs
, offset
, buf
, n_sectors
);
595 logout("%u sectors read\n", n_sectors
);
597 nb_sectors
-= n_sectors
;
598 sector_num
+= n_sectors
;
599 buf
+= n_sectors
* SECTOR_SIZE
;
605 static int vdi_co_write(BlockDriverState
*bs
,
606 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
)
608 BDRVVdiState
*s
= bs
->opaque
;
610 uint32_t block_index
;
611 uint32_t sector_in_block
;
613 uint32_t bmap_first
= VDI_UNALLOCATED
;
614 uint32_t bmap_last
= VDI_UNALLOCATED
;
615 uint8_t *block
= NULL
;
620 while (ret
>= 0 && nb_sectors
> 0) {
621 block_index
= sector_num
/ s
->block_sectors
;
622 sector_in_block
= sector_num
% s
->block_sectors
;
623 n_sectors
= s
->block_sectors
- sector_in_block
;
624 if (n_sectors
> nb_sectors
) {
625 n_sectors
= nb_sectors
;
628 logout("will write %u sectors starting at sector %" PRIu64
"\n",
629 n_sectors
, sector_num
);
631 /* prepare next AIO request */
632 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
633 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
634 /* Allocate new block and write to it. */
636 bmap_entry
= s
->header
.blocks_allocated
;
637 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
638 s
->header
.blocks_allocated
++;
639 offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
640 (uint64_t)bmap_entry
* s
->block_sectors
;
642 block
= g_malloc(s
->block_size
);
643 bmap_first
= block_index
;
645 bmap_last
= block_index
;
646 /* Copy data to be written to new block and zero unused parts. */
647 memset(block
, 0, sector_in_block
* SECTOR_SIZE
);
648 memcpy(block
+ sector_in_block
* SECTOR_SIZE
,
649 buf
, n_sectors
* SECTOR_SIZE
);
650 memset(block
+ (sector_in_block
+ n_sectors
) * SECTOR_SIZE
, 0,
651 (s
->block_sectors
- n_sectors
- sector_in_block
) * SECTOR_SIZE
);
653 /* Note that this coroutine does not yield anywhere from reading the
654 * bmap entry until here, so in regards to all the coroutines trying
655 * to write to this cluster, the one doing the allocation will
656 * always be the first to try to acquire the lock.
657 * Therefore, it is also the first that will actually be able to
658 * acquire the lock and thus the padded cluster is written before
659 * the other coroutines can write to the affected area. */
660 qemu_co_mutex_lock(&s
->write_lock
);
661 ret
= bdrv_write(bs
->file
->bs
, offset
, block
, s
->block_sectors
);
662 qemu_co_mutex_unlock(&s
->write_lock
);
664 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
665 (uint64_t)bmap_entry
* s
->block_sectors
+
667 qemu_co_mutex_lock(&s
->write_lock
);
668 /* This lock is only used to make sure the following write operation
669 * is executed after the write issued by the coroutine allocating
670 * this cluster, therefore we do not need to keep it locked.
671 * As stated above, the allocating coroutine will always try to lock
672 * the mutex before all the other concurrent accesses to that
673 * cluster, therefore at this point we can be absolutely certain
674 * that that write operation has returned (there may be other writes
675 * in flight, but they do not concern this very operation). */
676 qemu_co_mutex_unlock(&s
->write_lock
);
677 ret
= bdrv_write(bs
->file
->bs
, offset
, buf
, n_sectors
);
680 nb_sectors
-= n_sectors
;
681 sector_num
+= n_sectors
;
682 buf
+= n_sectors
* SECTOR_SIZE
;
684 logout("%u sectors written\n", n_sectors
);
687 logout("finished data write\n");
693 /* One or more new blocks were allocated. */
694 VdiHeader
*header
= (VdiHeader
*) block
;
698 logout("now writing modified header\n");
699 assert(VDI_IS_ALLOCATED(bmap_first
));
701 vdi_header_to_le(header
);
702 ret
= bdrv_write(bs
->file
->bs
, 0, block
, 1);
710 logout("now writing modified block map entry %u...%u\n",
711 bmap_first
, bmap_last
);
712 /* Write modified sectors from block map. */
713 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
714 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
715 n_sectors
= bmap_last
- bmap_first
+ 1;
716 offset
= s
->bmap_sector
+ bmap_first
;
717 base
= ((uint8_t *)&s
->bmap
[0]) + bmap_first
* SECTOR_SIZE
;
718 logout("will write %u block map sectors starting from entry %u\n",
719 n_sectors
, bmap_first
);
720 ret
= bdrv_write(bs
->file
->bs
, offset
, base
, n_sectors
);
726 static int vdi_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
731 size_t block_size
= DEFAULT_CLUSTER_SIZE
;
732 uint32_t image_type
= VDI_TYPE_DYNAMIC
;
737 Error
*local_err
= NULL
;
738 BlockBackend
*blk
= NULL
;
739 uint32_t *bmap
= NULL
;
743 /* Read out options. */
744 bytes
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
746 #if defined(CONFIG_VDI_BLOCK_SIZE)
747 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
748 block_size
= qemu_opt_get_size_del(opts
,
749 BLOCK_OPT_CLUSTER_SIZE
,
750 DEFAULT_CLUSTER_SIZE
);
752 #if defined(CONFIG_VDI_STATIC_IMAGE)
753 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_STATIC
, false)) {
754 image_type
= VDI_TYPE_STATIC
;
758 if (bytes
> VDI_DISK_SIZE_MAX
) {
760 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
761 ", max supported is 0x%" PRIx64
")",
762 bytes
, VDI_DISK_SIZE_MAX
);
766 ret
= bdrv_create_file(filename
, opts
, &local_err
);
768 error_propagate(errp
, local_err
);
772 blk
= blk_new_open(filename
, NULL
, NULL
,
773 BDRV_O_RDWR
| BDRV_O_CACHE_WB
| BDRV_O_PROTOCOL
,
776 error_propagate(errp
, local_err
);
781 blk_set_allow_write_beyond_eof(blk
, true);
783 /* We need enough blocks to store the given disk size,
784 so always round up. */
785 blocks
= DIV_ROUND_UP(bytes
, block_size
);
787 bmap_size
= blocks
* sizeof(uint32_t);
788 bmap_size
= ROUND_UP(bmap_size
, SECTOR_SIZE
);
790 memset(&header
, 0, sizeof(header
));
791 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
792 header
.signature
= VDI_SIGNATURE
;
793 header
.version
= VDI_VERSION_1_1
;
794 header
.header_size
= 0x180;
795 header
.image_type
= image_type
;
796 header
.offset_bmap
= 0x200;
797 header
.offset_data
= 0x200 + bmap_size
;
798 header
.sector_size
= SECTOR_SIZE
;
799 header
.disk_size
= bytes
;
800 header
.block_size
= block_size
;
801 header
.blocks_in_image
= blocks
;
802 if (image_type
== VDI_TYPE_STATIC
) {
803 header
.blocks_allocated
= blocks
;
805 uuid_generate(header
.uuid_image
);
806 uuid_generate(header
.uuid_last_snap
);
807 /* There is no need to set header.uuid_link or header.uuid_parent here. */
808 #if defined(CONFIG_VDI_DEBUG)
809 vdi_header_print(&header
);
811 vdi_header_to_le(&header
);
812 ret
= blk_pwrite(blk
, offset
, &header
, sizeof(header
));
814 error_setg(errp
, "Error writing header to %s", filename
);
817 offset
+= sizeof(header
);
820 bmap
= g_try_malloc0(bmap_size
);
823 error_setg(errp
, "Could not allocate bmap");
826 for (i
= 0; i
< blocks
; i
++) {
827 if (image_type
== VDI_TYPE_STATIC
) {
830 bmap
[i
] = VDI_UNALLOCATED
;
833 ret
= blk_pwrite(blk
, offset
, bmap
, bmap_size
);
835 error_setg(errp
, "Error writing bmap to %s", filename
);
841 if (image_type
== VDI_TYPE_STATIC
) {
842 ret
= blk_truncate(blk
, offset
+ blocks
* block_size
);
844 error_setg(errp
, "Failed to statically allocate %s", filename
);
855 static void vdi_close(BlockDriverState
*bs
)
857 BDRVVdiState
*s
= bs
->opaque
;
861 migrate_del_blocker(s
->migration_blocker
);
862 error_free(s
->migration_blocker
);
865 static QemuOptsList vdi_create_opts
= {
866 .name
= "vdi-create-opts",
867 .head
= QTAILQ_HEAD_INITIALIZER(vdi_create_opts
.head
),
870 .name
= BLOCK_OPT_SIZE
,
871 .type
= QEMU_OPT_SIZE
,
872 .help
= "Virtual disk size"
874 #if defined(CONFIG_VDI_BLOCK_SIZE)
876 .name
= BLOCK_OPT_CLUSTER_SIZE
,
877 .type
= QEMU_OPT_SIZE
,
878 .help
= "VDI cluster (block) size",
879 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
882 #if defined(CONFIG_VDI_STATIC_IMAGE)
884 .name
= BLOCK_OPT_STATIC
,
885 .type
= QEMU_OPT_BOOL
,
886 .help
= "VDI static (pre-allocated) image",
887 .def_value_str
= "off"
890 /* TODO: An additional option to set UUID values might be useful. */
891 { /* end of list */ }
895 static BlockDriver bdrv_vdi
= {
896 .format_name
= "vdi",
897 .instance_size
= sizeof(BDRVVdiState
),
898 .bdrv_probe
= vdi_probe
,
899 .bdrv_open
= vdi_open
,
900 .bdrv_close
= vdi_close
,
901 .bdrv_reopen_prepare
= vdi_reopen_prepare
,
902 .bdrv_create
= vdi_create
,
903 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
904 .bdrv_co_get_block_status
= vdi_co_get_block_status
,
905 .bdrv_make_empty
= vdi_make_empty
,
907 .bdrv_read
= vdi_co_read
,
908 #if defined(CONFIG_VDI_WRITE)
909 .bdrv_write
= vdi_co_write
,
912 .bdrv_get_info
= vdi_get_info
,
914 .create_opts
= &vdi_create_opts
,
915 .bdrv_check
= vdi_check
,
918 static void bdrv_vdi_init(void)
921 bdrv_register(&bdrv_vdi
);
924 block_init(bdrv_vdi_init
);