2 * Block driver for the Virtual Disk Image (VDI) format
4 * Copyright (c) 2009, 2012 Stefan Weil
6 * SPDX-License-Identifier: GPL-2.0-or-later
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 * http://forums.virtualbox.org/viewtopic.php?t=8046
24 * This driver supports create / read / write operations on VDI images.
26 * Todo (see also TODO in code):
28 * Some features like snapshots are still missing.
30 * Deallocation of zero-filled blocks and shrinking images are missing, too
31 * (might be added to common block layer).
33 * Allocation of blocks could be optimized (less writes to block map and
36 * Read and write of adjacent blocks could be done in one operation
37 * (current code uses one operation per block (1 MiB).
39 * The code is not thread safe (missing locks for changes in header and
40 * block table, no problem with current QEMU).
44 * Blocks (VDI documentation) correspond to clusters (QEMU).
45 * QEMU's backing files could be implemented using VDI snapshot files (TODO).
46 * VDI snapshot files may also contain the complete machine state.
47 * Maybe this machine state can be converted to QEMU PC machine snapshot data.
49 * The driver keeps a block cache (little endian entries) in memory.
50 * For the standard block size (1 MiB), a 1 TiB disk will use 4 MiB RAM,
51 * so this seems to be reasonable.
54 #include "qemu/osdep.h"
55 #include "qemu/units.h"
56 #include "qapi/error.h"
57 #include "qapi/qobject-input-visitor.h"
58 #include "qapi/qapi-visit-block-core.h"
59 #include "block/block_int.h"
60 #include "block/qdict.h"
61 #include "sysemu/block-backend.h"
62 #include "qemu/module.h"
63 #include "qemu/option.h"
64 #include "qemu/bswap.h"
65 #include "migration/blocker.h"
66 #include "qemu/coroutine.h"
67 #include "qemu/cutils.h"
68 #include "qemu/uuid.h"
69 #include "qemu/memalign.h"
71 /* Code configuration options. */
73 /* Enable debug messages. */
74 //~ #define CONFIG_VDI_DEBUG
76 /* Support write operations on VDI images. */
77 #define CONFIG_VDI_WRITE
79 /* Support non-standard block (cluster) size. This is untested.
80 * Maybe it will be needed for very large images.
82 //~ #define CONFIG_VDI_BLOCK_SIZE
84 /* Support static (fixed, pre-allocated) images. */
85 #define CONFIG_VDI_STATIC_IMAGE
87 /* Command line option for static images. */
88 #define BLOCK_OPT_STATIC "static"
90 #define SECTOR_SIZE 512
91 #define DEFAULT_CLUSTER_SIZE 1048576
92 /* Note: can't use 1 * MiB, because it's passed to stringify() */
94 #if defined(CONFIG_VDI_DEBUG)
100 #define logout(fmt, ...) \
103 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \
107 /* Image signature. */
108 #define VDI_SIGNATURE 0xbeda107f
111 #define VDI_VERSION_1_1 0x00010001
114 #define VDI_TYPE_DYNAMIC 1
115 #define VDI_TYPE_STATIC 2
117 /* Innotek / SUN images use these strings in header.text:
118 * "<<< innotek VirtualBox Disk Image >>>\n"
119 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
120 * "<<< Sun VirtualBox Disk Image >>>\n"
121 * The value does not matter, so QEMU created images use a different text.
123 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
125 /* A never-allocated block; semantically arbitrary content. */
126 #define VDI_UNALLOCATED 0xffffffffU
128 /* A discarded (no longer allocated) block; semantically zero-filled. */
129 #define VDI_DISCARDED 0xfffffffeU
131 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
133 /* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since
134 * the bmap is read and written in a single operation, its size needs to be
135 * limited to INT_MAX; furthermore, when opening an image, the bmap size is
136 * rounded up to be aligned on BDRV_SECTOR_SIZE.
137 * Therefore this should satisfy the following:
138 * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1
139 * (INT_MAX + 1 is the first value not representable as an int)
140 * This guarantees that any value below or equal to the constant will, when
141 * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary,
142 * still be below or equal to INT_MAX. */
143 #define VDI_BLOCKS_IN_IMAGE_MAX \
144 ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t)))
145 #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
146 (uint64_t)DEFAULT_CLUSTER_SIZE)
148 static QemuOptsList vdi_create_opts
;
154 uint32_t header_size
;
156 uint32_t image_flags
;
157 char description
[256];
158 uint32_t offset_bmap
;
159 uint32_t offset_data
;
160 uint32_t cylinders
; /* disk geometry, unused here */
161 uint32_t heads
; /* disk geometry, unused here */
162 uint32_t sectors
; /* disk geometry, unused here */
163 uint32_t sector_size
;
167 uint32_t block_extra
; /* unused here */
168 uint32_t blocks_in_image
;
169 uint32_t blocks_allocated
;
171 QemuUUID uuid_last_snap
;
173 QemuUUID uuid_parent
;
175 } QEMU_PACKED VdiHeader
;
177 QEMU_BUILD_BUG_ON(sizeof(VdiHeader
) != 512);
180 /* The block map entries are little endian (even in memory). */
182 /* Size of block (bytes). */
184 /* First sector of block map. */
185 uint32_t bmap_sector
;
186 /* VDI header (converted to host endianness). */
191 Error
*migration_blocker
;
194 static void vdi_header_to_cpu(VdiHeader
*header
)
196 header
->signature
= le32_to_cpu(header
->signature
);
197 header
->version
= le32_to_cpu(header
->version
);
198 header
->header_size
= le32_to_cpu(header
->header_size
);
199 header
->image_type
= le32_to_cpu(header
->image_type
);
200 header
->image_flags
= le32_to_cpu(header
->image_flags
);
201 header
->offset_bmap
= le32_to_cpu(header
->offset_bmap
);
202 header
->offset_data
= le32_to_cpu(header
->offset_data
);
203 header
->cylinders
= le32_to_cpu(header
->cylinders
);
204 header
->heads
= le32_to_cpu(header
->heads
);
205 header
->sectors
= le32_to_cpu(header
->sectors
);
206 header
->sector_size
= le32_to_cpu(header
->sector_size
);
207 header
->disk_size
= le64_to_cpu(header
->disk_size
);
208 header
->block_size
= le32_to_cpu(header
->block_size
);
209 header
->block_extra
= le32_to_cpu(header
->block_extra
);
210 header
->blocks_in_image
= le32_to_cpu(header
->blocks_in_image
);
211 header
->blocks_allocated
= le32_to_cpu(header
->blocks_allocated
);
212 header
->uuid_image
= qemu_uuid_bswap(header
->uuid_image
);
213 header
->uuid_last_snap
= qemu_uuid_bswap(header
->uuid_last_snap
);
214 header
->uuid_link
= qemu_uuid_bswap(header
->uuid_link
);
215 header
->uuid_parent
= qemu_uuid_bswap(header
->uuid_parent
);
218 static void vdi_header_to_le(VdiHeader
*header
)
220 header
->signature
= cpu_to_le32(header
->signature
);
221 header
->version
= cpu_to_le32(header
->version
);
222 header
->header_size
= cpu_to_le32(header
->header_size
);
223 header
->image_type
= cpu_to_le32(header
->image_type
);
224 header
->image_flags
= cpu_to_le32(header
->image_flags
);
225 header
->offset_bmap
= cpu_to_le32(header
->offset_bmap
);
226 header
->offset_data
= cpu_to_le32(header
->offset_data
);
227 header
->cylinders
= cpu_to_le32(header
->cylinders
);
228 header
->heads
= cpu_to_le32(header
->heads
);
229 header
->sectors
= cpu_to_le32(header
->sectors
);
230 header
->sector_size
= cpu_to_le32(header
->sector_size
);
231 header
->disk_size
= cpu_to_le64(header
->disk_size
);
232 header
->block_size
= cpu_to_le32(header
->block_size
);
233 header
->block_extra
= cpu_to_le32(header
->block_extra
);
234 header
->blocks_in_image
= cpu_to_le32(header
->blocks_in_image
);
235 header
->blocks_allocated
= cpu_to_le32(header
->blocks_allocated
);
236 header
->uuid_image
= qemu_uuid_bswap(header
->uuid_image
);
237 header
->uuid_last_snap
= qemu_uuid_bswap(header
->uuid_last_snap
);
238 header
->uuid_link
= qemu_uuid_bswap(header
->uuid_link
);
239 header
->uuid_parent
= qemu_uuid_bswap(header
->uuid_parent
);
242 static void vdi_header_print(VdiHeader
*header
)
244 char uuidstr
[UUID_STR_LEN
];
246 logout("text %s", header
->text
);
247 logout("signature 0x%08x\n", header
->signature
);
248 logout("header size 0x%04x\n", header
->header_size
);
249 logout("image type 0x%04x\n", header
->image_type
);
250 logout("image flags 0x%04x\n", header
->image_flags
);
251 logout("description %s\n", header
->description
);
252 logout("offset bmap 0x%04x\n", header
->offset_bmap
);
253 logout("offset data 0x%04x\n", header
->offset_data
);
254 logout("cylinders 0x%04x\n", header
->cylinders
);
255 logout("heads 0x%04x\n", header
->heads
);
256 logout("sectors 0x%04x\n", header
->sectors
);
257 logout("sector size 0x%04x\n", header
->sector_size
);
258 logout("image size 0x%" PRIx64
" B (%" PRIu64
" MiB)\n",
259 header
->disk_size
, header
->disk_size
/ MiB
);
260 logout("block size 0x%04x\n", header
->block_size
);
261 logout("block extra 0x%04x\n", header
->block_extra
);
262 logout("blocks tot. 0x%04x\n", header
->blocks_in_image
);
263 logout("blocks all. 0x%04x\n", header
->blocks_allocated
);
264 uuid
= header
->uuid_image
;
265 qemu_uuid_unparse(&uuid
, uuidstr
);
266 logout("uuid image %s\n", uuidstr
);
267 uuid
= header
->uuid_last_snap
;
268 qemu_uuid_unparse(&uuid
, uuidstr
);
269 logout("uuid snap %s\n", uuidstr
);
270 uuid
= header
->uuid_link
;
271 qemu_uuid_unparse(&uuid
, uuidstr
);
272 logout("uuid link %s\n", uuidstr
);
273 uuid
= header
->uuid_parent
;
274 qemu_uuid_unparse(&uuid
, uuidstr
);
275 logout("uuid parent %s\n", uuidstr
);
278 static int coroutine_fn
vdi_co_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
281 /* TODO: additional checks possible. */
282 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
283 uint32_t blocks_allocated
= 0;
292 bmap
= g_try_new(uint32_t, s
->header
.blocks_in_image
);
293 if (s
->header
.blocks_in_image
&& bmap
== NULL
) {
298 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
300 /* Check block map and value of blocks_allocated. */
301 for (block
= 0; block
< s
->header
.blocks_in_image
; block
++) {
302 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[block
]);
303 if (VDI_IS_ALLOCATED(bmap_entry
)) {
304 if (bmap_entry
< s
->header
.blocks_in_image
) {
306 if (!VDI_IS_ALLOCATED(bmap
[bmap_entry
])) {
307 bmap
[bmap_entry
] = bmap_entry
;
309 fprintf(stderr
, "ERROR: block index %" PRIu32
310 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
314 fprintf(stderr
, "ERROR: block index %" PRIu32
315 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
320 if (blocks_allocated
!= s
->header
.blocks_allocated
) {
321 fprintf(stderr
, "ERROR: allocated blocks mismatch, is %" PRIu32
322 ", should be %" PRIu32
"\n",
323 blocks_allocated
, s
->header
.blocks_allocated
);
332 static int coroutine_fn
333 vdi_co_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
335 /* TODO: vdi_co_get_info would be needed for machine snapshots.
336 vm_state_offset is still missing. */
337 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
339 bdi
->cluster_size
= s
->block_size
;
340 bdi
->vm_state_offset
= 0;
344 static int vdi_make_empty(BlockDriverState
*bs
)
346 /* TODO: missing code. */
348 /* The return value for missing code must be 0, see block.c. */
352 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
354 const VdiHeader
*header
= (const VdiHeader
*)buf
;
359 if (buf_size
< sizeof(*header
)) {
360 /* Header too small, no VDI. */
361 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
366 logout("no vdi image\n");
368 logout("%s", header
->text
);
374 static int vdi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
377 BDRVVdiState
*s
= bs
->opaque
;
381 QemuUUID uuid_link
, uuid_parent
;
383 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
388 GRAPH_RDLOCK_GUARD_MAINLOOP();
392 ret
= bdrv_pread(bs
->file
, 0, sizeof(header
), &header
, 0);
397 vdi_header_to_cpu(&header
);
399 vdi_header_print(&header
);
402 if (header
.disk_size
> VDI_DISK_SIZE_MAX
) {
403 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
404 ", max supported is 0x%" PRIx64
")",
405 header
.disk_size
, VDI_DISK_SIZE_MAX
);
410 uuid_link
= header
.uuid_link
;
411 uuid_parent
= header
.uuid_parent
;
413 if (header
.disk_size
% SECTOR_SIZE
!= 0) {
414 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
415 We accept them but round the disk size to the next multiple of
417 logout("odd disk size %" PRIu64
" B, round up\n", header
.disk_size
);
418 header
.disk_size
= ROUND_UP(header
.disk_size
, SECTOR_SIZE
);
421 if (header
.signature
!= VDI_SIGNATURE
) {
422 error_setg(errp
, "Image not in VDI format (bad signature %08" PRIx32
423 ")", header
.signature
);
426 } else if (header
.version
!= VDI_VERSION_1_1
) {
427 error_setg(errp
, "unsupported VDI image (version %" PRIu32
".%" PRIu32
428 ")", header
.version
>> 16, header
.version
& 0xffff);
431 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
432 /* We only support block maps which start on a sector boundary. */
433 error_setg(errp
, "unsupported VDI image (unaligned block map offset "
434 "0x%" PRIx32
")", header
.offset_bmap
);
437 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
438 /* We only support data blocks which start on a sector boundary. */
439 error_setg(errp
, "unsupported VDI image (unaligned data offset 0x%"
440 PRIx32
")", header
.offset_data
);
443 } else if (header
.sector_size
!= SECTOR_SIZE
) {
444 error_setg(errp
, "unsupported VDI image (sector size %" PRIu32
445 " is not %u)", header
.sector_size
, SECTOR_SIZE
);
448 } else if (header
.block_size
!= DEFAULT_CLUSTER_SIZE
) {
449 error_setg(errp
, "unsupported VDI image (block size %" PRIu32
450 " is not %" PRIu32
")",
451 header
.block_size
, DEFAULT_CLUSTER_SIZE
);
454 } else if (header
.disk_size
>
455 (uint64_t)header
.blocks_in_image
* header
.block_size
) {
456 error_setg(errp
, "unsupported VDI image (disk size %" PRIu64
", "
457 "image bitmap has room for %" PRIu64
")",
459 (uint64_t)header
.blocks_in_image
* header
.block_size
);
462 } else if (!qemu_uuid_is_null(&uuid_link
)) {
463 error_setg(errp
, "unsupported VDI image (non-NULL link UUID)");
466 } else if (!qemu_uuid_is_null(&uuid_parent
)) {
467 error_setg(errp
, "unsupported VDI image (non-NULL parent UUID)");
470 } else if (header
.blocks_in_image
> VDI_BLOCKS_IN_IMAGE_MAX
) {
471 error_setg(errp
, "unsupported VDI image "
472 "(too many blocks %u, max is %u)",
473 header
.blocks_in_image
, VDI_BLOCKS_IN_IMAGE_MAX
);
478 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
480 s
->block_size
= header
.block_size
;
481 s
->bmap_sector
= header
.offset_bmap
/ SECTOR_SIZE
;
484 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
485 bmap_size
= DIV_ROUND_UP(bmap_size
, SECTOR_SIZE
);
486 s
->bmap
= qemu_try_blockalign(bs
->file
->bs
, bmap_size
* SECTOR_SIZE
);
487 if (s
->bmap
== NULL
) {
492 ret
= bdrv_pread(bs
->file
, header
.offset_bmap
, bmap_size
* SECTOR_SIZE
,
498 /* Disable migration when vdi images are used */
499 error_setg(&s
->migration_blocker
, "The vdi format used by node '%s' "
500 "does not support live migration",
501 bdrv_get_device_or_node_name(bs
));
503 ret
= migrate_add_blocker_normal(&s
->migration_blocker
, errp
);
508 qemu_co_rwlock_init(&s
->bmap_lock
);
519 static int vdi_reopen_prepare(BDRVReopenState
*state
,
520 BlockReopenQueue
*queue
, Error
**errp
)
525 static int coroutine_fn GRAPH_RDLOCK
526 vdi_co_block_status(BlockDriverState
*bs
, bool want_zero
, int64_t offset
,
527 int64_t bytes
, int64_t *pnum
, int64_t *map
,
528 BlockDriverState
**file
)
530 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
531 size_t bmap_index
= offset
/ s
->block_size
;
532 size_t index_in_block
= offset
% s
->block_size
;
533 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
536 logout("%p, %" PRId64
", %" PRId64
", %p\n", bs
, offset
, bytes
, pnum
);
537 *pnum
= MIN(s
->block_size
- index_in_block
, bytes
);
538 result
= VDI_IS_ALLOCATED(bmap_entry
);
540 return BDRV_BLOCK_ZERO
;
543 *map
= s
->header
.offset_data
+ (uint64_t)bmap_entry
* s
->block_size
+
545 *file
= bs
->file
->bs
;
546 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
|
547 (s
->header
.image_type
== VDI_TYPE_STATIC
? BDRV_BLOCK_RECURSE
: 0);
550 static int coroutine_fn GRAPH_RDLOCK
551 vdi_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
552 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
554 BDRVVdiState
*s
= bs
->opaque
;
555 QEMUIOVector local_qiov
;
557 uint32_t block_index
;
558 uint32_t offset_in_block
;
560 uint64_t bytes_done
= 0;
565 qemu_iovec_init(&local_qiov
, qiov
->niov
);
567 while (ret
>= 0 && bytes
> 0) {
568 block_index
= offset
/ s
->block_size
;
569 offset_in_block
= offset
% s
->block_size
;
570 n_bytes
= MIN(bytes
, s
->block_size
- offset_in_block
);
572 logout("will read %u bytes starting at offset %" PRIu64
"\n",
575 /* prepare next AIO request */
576 qemu_co_rwlock_rdlock(&s
->bmap_lock
);
577 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
578 qemu_co_rwlock_unlock(&s
->bmap_lock
);
579 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
580 /* Block not allocated, return zeros, no need to wait. */
581 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
584 uint64_t data_offset
= s
->header
.offset_data
+
585 (uint64_t)bmap_entry
* s
->block_size
+
588 qemu_iovec_reset(&local_qiov
);
589 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
591 ret
= bdrv_co_preadv(bs
->file
, data_offset
, n_bytes
,
594 logout("%u bytes read\n", n_bytes
);
598 bytes_done
+= n_bytes
;
601 qemu_iovec_destroy(&local_qiov
);
606 static int coroutine_fn GRAPH_RDLOCK
607 vdi_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
608 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
610 BDRVVdiState
*s
= bs
->opaque
;
611 QEMUIOVector local_qiov
;
613 uint32_t block_index
;
614 uint32_t offset_in_block
;
616 uint64_t data_offset
;
617 uint32_t bmap_first
= VDI_UNALLOCATED
;
618 uint32_t bmap_last
= VDI_UNALLOCATED
;
619 uint8_t *block
= NULL
;
620 uint64_t bytes_done
= 0;
625 qemu_iovec_init(&local_qiov
, qiov
->niov
);
627 while (ret
>= 0 && bytes
> 0) {
628 block_index
= offset
/ s
->block_size
;
629 offset_in_block
= offset
% s
->block_size
;
630 n_bytes
= MIN(bytes
, s
->block_size
- offset_in_block
);
632 logout("will write %u bytes starting at offset %" PRIu64
"\n",
635 /* prepare next AIO request */
636 qemu_co_rwlock_rdlock(&s
->bmap_lock
);
637 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
638 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
639 /* Allocate new block and write to it. */
640 qemu_co_rwlock_upgrade(&s
->bmap_lock
);
641 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
642 if (VDI_IS_ALLOCATED(bmap_entry
)) {
643 /* A concurrent allocation did the work for us. */
644 qemu_co_rwlock_downgrade(&s
->bmap_lock
);
645 goto nonallocating_write
;
648 bmap_entry
= s
->header
.blocks_allocated
;
649 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
650 s
->header
.blocks_allocated
++;
651 data_offset
= s
->header
.offset_data
+
652 (uint64_t)bmap_entry
* s
->block_size
;
654 block
= g_malloc(s
->block_size
);
655 bmap_first
= block_index
;
657 bmap_last
= block_index
;
658 /* Copy data to be written to new block and zero unused parts. */
659 memset(block
, 0, offset_in_block
);
660 qemu_iovec_to_buf(qiov
, bytes_done
, block
+ offset_in_block
,
662 memset(block
+ offset_in_block
+ n_bytes
, 0,
663 s
->block_size
- n_bytes
- offset_in_block
);
665 /* Write the new block under CoRwLock write-side protection,
666 * so this full-cluster write does not overlap a partial write
667 * of the same cluster, issued from the "else" branch.
669 ret
= bdrv_co_pwrite(bs
->file
, data_offset
, s
->block_size
, block
,
671 qemu_co_rwlock_unlock(&s
->bmap_lock
);
674 data_offset
= s
->header
.offset_data
+
675 (uint64_t)bmap_entry
* s
->block_size
+
677 qemu_co_rwlock_unlock(&s
->bmap_lock
);
679 qemu_iovec_reset(&local_qiov
);
680 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
682 ret
= bdrv_co_pwritev(bs
->file
, data_offset
, n_bytes
,
688 bytes_done
+= n_bytes
;
690 logout("%u bytes written\n", n_bytes
);
693 qemu_iovec_destroy(&local_qiov
);
695 logout("finished data write\n");
702 /* One or more new blocks were allocated. */
705 uint64_t bmap_offset
;
709 header
= g_malloc(sizeof(*header
));
711 logout("now writing modified header\n");
712 assert(VDI_IS_ALLOCATED(bmap_first
));
714 vdi_header_to_le(header
);
715 ret
= bdrv_co_pwrite(bs
->file
, 0, sizeof(*header
), header
, 0);
722 logout("now writing modified block map entry %u...%u\n",
723 bmap_first
, bmap_last
);
724 /* Write modified sectors from block map. */
725 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
726 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
727 n_sectors
= bmap_last
- bmap_first
+ 1;
728 bmap_offset
= s
->bmap_sector
+ bmap_first
;
729 base
= ((uint8_t *)&s
->bmap
[0]) + bmap_first
* SECTOR_SIZE
;
730 logout("will write %u block map sectors starting from entry %u\n",
731 n_sectors
, bmap_first
);
732 ret
= bdrv_co_pwrite(bs
->file
, bmap_offset
* SECTOR_SIZE
,
733 n_sectors
* SECTOR_SIZE
, base
, 0);
739 static int coroutine_fn GRAPH_UNLOCKED
740 vdi_co_do_create(BlockdevCreateOptions
*create_options
, size_t block_size
,
744 BlockdevCreateOptionsVdi
*vdi_opts
;
753 BlockDriverState
*bs_file
= NULL
;
754 BlockBackend
*blk
= NULL
;
755 uint32_t *bmap
= NULL
;
758 assert(create_options
->driver
== BLOCKDEV_DRIVER_VDI
);
759 vdi_opts
= &create_options
->u
.vdi
;
763 /* Validate options and set default values */
764 bytes
= vdi_opts
->size
;
766 if (!vdi_opts
->has_preallocation
) {
767 vdi_opts
->preallocation
= PREALLOC_MODE_OFF
;
769 switch (vdi_opts
->preallocation
) {
770 case PREALLOC_MODE_OFF
:
771 image_type
= VDI_TYPE_DYNAMIC
;
773 case PREALLOC_MODE_METADATA
:
774 image_type
= VDI_TYPE_STATIC
;
777 error_setg(errp
, "Preallocation mode not supported for vdi");
781 #ifndef CONFIG_VDI_STATIC_IMAGE
782 if (image_type
== VDI_TYPE_STATIC
) {
784 error_setg(errp
, "Statically allocated images cannot be created in "
789 #ifndef CONFIG_VDI_BLOCK_SIZE
790 if (block_size
!= DEFAULT_CLUSTER_SIZE
) {
793 "A non-default cluster size is not supported in this build");
798 if (bytes
> VDI_DISK_SIZE_MAX
) {
800 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
801 ", max supported is 0x%" PRIx64
")",
802 bytes
, VDI_DISK_SIZE_MAX
);
806 /* Create BlockBackend to write to the image */
807 bs_file
= bdrv_co_open_blockdev_ref(vdi_opts
->file
, errp
);
813 blk
= blk_co_new_with_bs(bs_file
, BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
820 blk_set_allow_write_beyond_eof(blk
, true);
822 /* We need enough blocks to store the given disk size,
823 so always round up. */
824 blocks
= DIV_ROUND_UP(bytes
, block_size
);
826 bmap_size
= blocks
* sizeof(uint32_t);
827 bmap_size
= ROUND_UP(bmap_size
, SECTOR_SIZE
);
829 memset(&header
, 0, sizeof(header
));
830 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
831 header
.signature
= VDI_SIGNATURE
;
832 header
.version
= VDI_VERSION_1_1
;
833 header
.header_size
= 0x180;
834 header
.image_type
= image_type
;
835 header
.offset_bmap
= 0x200;
836 header
.offset_data
= 0x200 + bmap_size
;
837 header
.sector_size
= SECTOR_SIZE
;
838 header
.disk_size
= bytes
;
839 header
.block_size
= block_size
;
840 header
.blocks_in_image
= blocks
;
841 if (image_type
== VDI_TYPE_STATIC
) {
842 header
.blocks_allocated
= blocks
;
844 qemu_uuid_generate(&uuid
);
845 header
.uuid_image
= uuid
;
846 qemu_uuid_generate(&uuid
);
847 header
.uuid_last_snap
= uuid
;
848 /* There is no need to set header.uuid_link or header.uuid_parent here. */
850 vdi_header_print(&header
);
852 vdi_header_to_le(&header
);
853 ret
= blk_co_pwrite(blk
, offset
, sizeof(header
), &header
, 0);
855 error_setg(errp
, "Error writing header");
858 offset
+= sizeof(header
);
861 bmap
= g_try_malloc0(bmap_size
);
864 error_setg(errp
, "Could not allocate bmap");
867 for (i
= 0; i
< blocks
; i
++) {
868 if (image_type
== VDI_TYPE_STATIC
) {
871 bmap
[i
] = VDI_UNALLOCATED
;
874 ret
= blk_co_pwrite(blk
, offset
, bmap_size
, bmap
, 0);
876 error_setg(errp
, "Error writing bmap");
882 if (image_type
== VDI_TYPE_STATIC
) {
883 ret
= blk_co_truncate(blk
, offset
+ blocks
* block_size
, false,
884 PREALLOC_MODE_OFF
, 0, errp
);
886 error_prepend(errp
, "Failed to statically allocate file");
894 bdrv_co_unref(bs_file
);
899 static int coroutine_fn GRAPH_UNLOCKED
900 vdi_co_create(BlockdevCreateOptions
*create_options
, Error
**errp
)
902 return vdi_co_do_create(create_options
, DEFAULT_CLUSTER_SIZE
, errp
);
905 static int coroutine_fn GRAPH_UNLOCKED
906 vdi_co_create_opts(BlockDriver
*drv
, const char *filename
,
907 QemuOpts
*opts
, Error
**errp
)
910 BlockdevCreateOptions
*create_options
= NULL
;
911 BlockDriverState
*bs_file
= NULL
;
912 uint64_t block_size
= DEFAULT_CLUSTER_SIZE
;
913 bool is_static
= false;
917 /* Parse options and convert legacy syntax.
919 * Since CONFIG_VDI_BLOCK_SIZE is disabled by default,
920 * cluster-size is not part of the QAPI schema; therefore we have
921 * to parse it before creating the QAPI object. */
922 #if defined(CONFIG_VDI_BLOCK_SIZE)
923 block_size
= qemu_opt_get_size_del(opts
,
924 BLOCK_OPT_CLUSTER_SIZE
,
925 DEFAULT_CLUSTER_SIZE
);
926 if (block_size
< BDRV_SECTOR_SIZE
|| block_size
> UINT32_MAX
||
927 !is_power_of_2(block_size
))
929 error_setg(errp
, "Invalid cluster size");
934 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_STATIC
, false)) {
938 qdict
= qemu_opts_to_qdict_filtered(opts
, NULL
, &vdi_create_opts
, true);
940 /* Create and open the file (protocol layer) */
941 ret
= bdrv_co_create_file(filename
, opts
, errp
);
946 bs_file
= bdrv_co_open(filename
, NULL
, NULL
,
947 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
953 qdict_put_str(qdict
, "driver", "vdi");
954 qdict_put_str(qdict
, "file", bs_file
->node_name
);
956 qdict_put_str(qdict
, "preallocation", "metadata");
959 /* Get the QAPI object */
960 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
965 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, errp
);
967 if (!create_options
) {
972 /* Silently round up size */
973 assert(create_options
->driver
== BLOCKDEV_DRIVER_VDI
);
974 create_options
->u
.vdi
.size
= ROUND_UP(create_options
->u
.vdi
.size
,
977 /* Create the vdi image (format layer) */
978 ret
= vdi_co_do_create(create_options
, block_size
, errp
);
980 qobject_unref(qdict
);
981 qapi_free_BlockdevCreateOptions(create_options
);
982 bdrv_co_unref(bs_file
);
986 static void vdi_close(BlockDriverState
*bs
)
988 BDRVVdiState
*s
= bs
->opaque
;
992 migrate_del_blocker(&s
->migration_blocker
);
995 static int GRAPH_RDLOCK
vdi_has_zero_init(BlockDriverState
*bs
)
997 BDRVVdiState
*s
= bs
->opaque
;
999 if (s
->header
.image_type
== VDI_TYPE_STATIC
) {
1000 return bdrv_has_zero_init(bs
->file
->bs
);
1006 static QemuOptsList vdi_create_opts
= {
1007 .name
= "vdi-create-opts",
1008 .head
= QTAILQ_HEAD_INITIALIZER(vdi_create_opts
.head
),
1011 .name
= BLOCK_OPT_SIZE
,
1012 .type
= QEMU_OPT_SIZE
,
1013 .help
= "Virtual disk size"
1015 #if defined(CONFIG_VDI_BLOCK_SIZE)
1017 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1018 .type
= QEMU_OPT_SIZE
,
1019 .help
= "VDI cluster (block) size",
1020 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
1023 #if defined(CONFIG_VDI_STATIC_IMAGE)
1025 .name
= BLOCK_OPT_STATIC
,
1026 .type
= QEMU_OPT_BOOL
,
1027 .help
= "VDI static (pre-allocated) image",
1028 .def_value_str
= "off"
1031 /* TODO: An additional option to set UUID values might be useful. */
1032 { /* end of list */ }
1036 static BlockDriver bdrv_vdi
= {
1037 .format_name
= "vdi",
1038 .instance_size
= sizeof(BDRVVdiState
),
1039 .bdrv_probe
= vdi_probe
,
1040 .bdrv_open
= vdi_open
,
1041 .bdrv_close
= vdi_close
,
1042 .bdrv_reopen_prepare
= vdi_reopen_prepare
,
1043 .bdrv_child_perm
= bdrv_default_perms
,
1044 .bdrv_co_create
= vdi_co_create
,
1045 .bdrv_co_create_opts
= vdi_co_create_opts
,
1046 .bdrv_has_zero_init
= vdi_has_zero_init
,
1047 .bdrv_co_block_status
= vdi_co_block_status
,
1048 .bdrv_make_empty
= vdi_make_empty
,
1050 .bdrv_co_preadv
= vdi_co_preadv
,
1051 #if defined(CONFIG_VDI_WRITE)
1052 .bdrv_co_pwritev
= vdi_co_pwritev
,
1055 .bdrv_co_get_info
= vdi_co_get_info
,
1058 .create_opts
= &vdi_create_opts
,
1059 .bdrv_co_check
= vdi_co_check
,
1062 static void bdrv_vdi_init(void)
1065 bdrv_register(&bdrv_vdi
);
1068 block_init(bdrv_vdi_init
);