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 "qemu/units.h"
54 #include "qapi/error.h"
55 #include "qapi/qobject-input-visitor.h"
56 #include "qapi/qapi-visit-block-core.h"
57 #include "block/block_int.h"
58 #include "block/qdict.h"
59 #include "sysemu/block-backend.h"
60 #include "qemu/module.h"
61 #include "qemu/option.h"
62 #include "qemu/bswap.h"
63 #include "migration/blocker.h"
64 #include "qemu/coroutine.h"
65 #include "qemu/cutils.h"
66 #include "qemu/uuid.h"
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"
87 #define SECTOR_SIZE 512
88 #define DEFAULT_CLUSTER_SIZE 1048576
89 /* Note: can't use 1 * MiB, because it's passed to stringify() */
91 #if defined(CONFIG_VDI_DEBUG)
97 #define logout(fmt, ...) \
100 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \
104 /* Image signature. */
105 #define VDI_SIGNATURE 0xbeda107f
108 #define VDI_VERSION_1_1 0x00010001
111 #define VDI_TYPE_DYNAMIC 1
112 #define VDI_TYPE_STATIC 2
114 /* Innotek / SUN images use these strings in header.text:
115 * "<<< innotek VirtualBox Disk Image >>>\n"
116 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
117 * "<<< Sun VirtualBox Disk Image >>>\n"
118 * The value does not matter, so QEMU created images use a different text.
120 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
122 /* A never-allocated block; semantically arbitrary content. */
123 #define VDI_UNALLOCATED 0xffffffffU
125 /* A discarded (no longer allocated) block; semantically zero-filled. */
126 #define VDI_DISCARDED 0xfffffffeU
128 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
130 /* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since
131 * the bmap is read and written in a single operation, its size needs to be
132 * limited to INT_MAX; furthermore, when opening an image, the bmap size is
133 * rounded up to be aligned on BDRV_SECTOR_SIZE.
134 * Therefore this should satisfy the following:
135 * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1
136 * (INT_MAX + 1 is the first value not representable as an int)
137 * This guarantees that any value below or equal to the constant will, when
138 * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary,
139 * still be below or equal to INT_MAX. */
140 #define VDI_BLOCKS_IN_IMAGE_MAX \
141 ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t)))
142 #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
143 (uint64_t)DEFAULT_CLUSTER_SIZE)
145 static QemuOptsList vdi_create_opts
;
151 uint32_t header_size
;
153 uint32_t image_flags
;
154 char description
[256];
155 uint32_t offset_bmap
;
156 uint32_t offset_data
;
157 uint32_t cylinders
; /* disk geometry, unused here */
158 uint32_t heads
; /* disk geometry, unused here */
159 uint32_t sectors
; /* disk geometry, unused here */
160 uint32_t sector_size
;
164 uint32_t block_extra
; /* unused here */
165 uint32_t blocks_in_image
;
166 uint32_t blocks_allocated
;
168 QemuUUID uuid_last_snap
;
170 QemuUUID uuid_parent
;
172 } QEMU_PACKED VdiHeader
;
174 QEMU_BUILD_BUG_ON(sizeof(VdiHeader
) != 512);
177 /* The block map entries are little endian (even in memory). */
179 /* Size of block (bytes). */
181 /* First sector of block map. */
182 uint32_t bmap_sector
;
183 /* VDI header (converted to host endianness). */
188 Error
*migration_blocker
;
191 static void vdi_header_to_cpu(VdiHeader
*header
)
193 header
->signature
= le32_to_cpu(header
->signature
);
194 header
->version
= le32_to_cpu(header
->version
);
195 header
->header_size
= le32_to_cpu(header
->header_size
);
196 header
->image_type
= le32_to_cpu(header
->image_type
);
197 header
->image_flags
= le32_to_cpu(header
->image_flags
);
198 header
->offset_bmap
= le32_to_cpu(header
->offset_bmap
);
199 header
->offset_data
= le32_to_cpu(header
->offset_data
);
200 header
->cylinders
= le32_to_cpu(header
->cylinders
);
201 header
->heads
= le32_to_cpu(header
->heads
);
202 header
->sectors
= le32_to_cpu(header
->sectors
);
203 header
->sector_size
= le32_to_cpu(header
->sector_size
);
204 header
->disk_size
= le64_to_cpu(header
->disk_size
);
205 header
->block_size
= le32_to_cpu(header
->block_size
);
206 header
->block_extra
= le32_to_cpu(header
->block_extra
);
207 header
->blocks_in_image
= le32_to_cpu(header
->blocks_in_image
);
208 header
->blocks_allocated
= le32_to_cpu(header
->blocks_allocated
);
209 header
->uuid_image
= qemu_uuid_bswap(header
->uuid_image
);
210 header
->uuid_last_snap
= qemu_uuid_bswap(header
->uuid_last_snap
);
211 header
->uuid_link
= qemu_uuid_bswap(header
->uuid_link
);
212 header
->uuid_parent
= qemu_uuid_bswap(header
->uuid_parent
);
215 static void vdi_header_to_le(VdiHeader
*header
)
217 header
->signature
= cpu_to_le32(header
->signature
);
218 header
->version
= cpu_to_le32(header
->version
);
219 header
->header_size
= cpu_to_le32(header
->header_size
);
220 header
->image_type
= cpu_to_le32(header
->image_type
);
221 header
->image_flags
= cpu_to_le32(header
->image_flags
);
222 header
->offset_bmap
= cpu_to_le32(header
->offset_bmap
);
223 header
->offset_data
= cpu_to_le32(header
->offset_data
);
224 header
->cylinders
= cpu_to_le32(header
->cylinders
);
225 header
->heads
= cpu_to_le32(header
->heads
);
226 header
->sectors
= cpu_to_le32(header
->sectors
);
227 header
->sector_size
= cpu_to_le32(header
->sector_size
);
228 header
->disk_size
= cpu_to_le64(header
->disk_size
);
229 header
->block_size
= cpu_to_le32(header
->block_size
);
230 header
->block_extra
= cpu_to_le32(header
->block_extra
);
231 header
->blocks_in_image
= cpu_to_le32(header
->blocks_in_image
);
232 header
->blocks_allocated
= cpu_to_le32(header
->blocks_allocated
);
233 header
->uuid_image
= qemu_uuid_bswap(header
->uuid_image
);
234 header
->uuid_last_snap
= qemu_uuid_bswap(header
->uuid_last_snap
);
235 header
->uuid_link
= qemu_uuid_bswap(header
->uuid_link
);
236 header
->uuid_parent
= qemu_uuid_bswap(header
->uuid_parent
);
239 static void vdi_header_print(VdiHeader
*header
)
243 logout("text %s", header
->text
);
244 logout("signature 0x%08x\n", header
->signature
);
245 logout("header size 0x%04x\n", header
->header_size
);
246 logout("image type 0x%04x\n", header
->image_type
);
247 logout("image flags 0x%04x\n", header
->image_flags
);
248 logout("description %s\n", header
->description
);
249 logout("offset bmap 0x%04x\n", header
->offset_bmap
);
250 logout("offset data 0x%04x\n", header
->offset_data
);
251 logout("cylinders 0x%04x\n", header
->cylinders
);
252 logout("heads 0x%04x\n", header
->heads
);
253 logout("sectors 0x%04x\n", header
->sectors
);
254 logout("sector size 0x%04x\n", header
->sector_size
);
255 logout("image size 0x%" PRIx64
" B (%" PRIu64
" MiB)\n",
256 header
->disk_size
, header
->disk_size
/ MiB
);
257 logout("block size 0x%04x\n", header
->block_size
);
258 logout("block extra 0x%04x\n", header
->block_extra
);
259 logout("blocks tot. 0x%04x\n", header
->blocks_in_image
);
260 logout("blocks all. 0x%04x\n", header
->blocks_allocated
);
261 uuid
= header
->uuid_image
;
262 qemu_uuid_unparse(&uuid
, uuidstr
);
263 logout("uuid image %s\n", uuidstr
);
264 uuid
= header
->uuid_last_snap
;
265 qemu_uuid_unparse(&uuid
, uuidstr
);
266 logout("uuid snap %s\n", uuidstr
);
267 uuid
= header
->uuid_link
;
268 qemu_uuid_unparse(&uuid
, uuidstr
);
269 logout("uuid link %s\n", uuidstr
);
270 uuid
= header
->uuid_parent
;
271 qemu_uuid_unparse(&uuid
, uuidstr
);
272 logout("uuid parent %s\n", uuidstr
);
275 static int coroutine_fn
vdi_co_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
278 /* TODO: additional checks possible. */
279 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
280 uint32_t blocks_allocated
= 0;
289 bmap
= g_try_new(uint32_t, s
->header
.blocks_in_image
);
290 if (s
->header
.blocks_in_image
&& bmap
== NULL
) {
295 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
297 /* Check block map and value of blocks_allocated. */
298 for (block
= 0; block
< s
->header
.blocks_in_image
; block
++) {
299 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[block
]);
300 if (VDI_IS_ALLOCATED(bmap_entry
)) {
301 if (bmap_entry
< s
->header
.blocks_in_image
) {
303 if (!VDI_IS_ALLOCATED(bmap
[bmap_entry
])) {
304 bmap
[bmap_entry
] = bmap_entry
;
306 fprintf(stderr
, "ERROR: block index %" PRIu32
307 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
311 fprintf(stderr
, "ERROR: block index %" PRIu32
312 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
317 if (blocks_allocated
!= s
->header
.blocks_allocated
) {
318 fprintf(stderr
, "ERROR: allocated blocks mismatch, is %" PRIu32
319 ", should be %" PRIu32
"\n",
320 blocks_allocated
, s
->header
.blocks_allocated
);
329 static int vdi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
331 /* TODO: vdi_get_info would be needed for machine snapshots.
332 vm_state_offset is still missing. */
333 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
335 bdi
->cluster_size
= s
->block_size
;
336 bdi
->vm_state_offset
= 0;
337 bdi
->unallocated_blocks_are_zero
= true;
341 static int vdi_make_empty(BlockDriverState
*bs
)
343 /* TODO: missing code. */
345 /* The return value for missing code must be 0, see block.c. */
349 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
351 const VdiHeader
*header
= (const VdiHeader
*)buf
;
356 if (buf_size
< sizeof(*header
)) {
357 /* Header too small, no VDI. */
358 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
363 logout("no vdi image\n");
365 logout("%s", header
->text
);
371 static int vdi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
374 BDRVVdiState
*s
= bs
->opaque
;
378 Error
*local_err
= NULL
;
379 QemuUUID uuid_link
, uuid_parent
;
381 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
,
389 ret
= bdrv_pread(bs
->file
, 0, &header
, sizeof(header
));
394 vdi_header_to_cpu(&header
);
396 vdi_header_print(&header
);
399 if (header
.disk_size
> VDI_DISK_SIZE_MAX
) {
400 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
401 ", max supported is 0x%" PRIx64
")",
402 header
.disk_size
, VDI_DISK_SIZE_MAX
);
407 uuid_link
= header
.uuid_link
;
408 uuid_parent
= header
.uuid_parent
;
410 if (header
.disk_size
% SECTOR_SIZE
!= 0) {
411 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
412 We accept them but round the disk size to the next multiple of
414 logout("odd disk size %" PRIu64
" B, round up\n", header
.disk_size
);
415 header
.disk_size
= ROUND_UP(header
.disk_size
, SECTOR_SIZE
);
418 if (header
.signature
!= VDI_SIGNATURE
) {
419 error_setg(errp
, "Image not in VDI format (bad signature %08" PRIx32
420 ")", header
.signature
);
423 } else if (header
.version
!= VDI_VERSION_1_1
) {
424 error_setg(errp
, "unsupported VDI image (version %" PRIu32
".%" PRIu32
425 ")", header
.version
>> 16, header
.version
& 0xffff);
428 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
429 /* We only support block maps which start on a sector boundary. */
430 error_setg(errp
, "unsupported VDI image (unaligned block map offset "
431 "0x%" PRIx32
")", header
.offset_bmap
);
434 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
435 /* We only support data blocks which start on a sector boundary. */
436 error_setg(errp
, "unsupported VDI image (unaligned data offset 0x%"
437 PRIx32
")", header
.offset_data
);
440 } else if (header
.sector_size
!= SECTOR_SIZE
) {
441 error_setg(errp
, "unsupported VDI image (sector size %" PRIu32
442 " is not %u)", header
.sector_size
, SECTOR_SIZE
);
445 } else if (header
.block_size
!= DEFAULT_CLUSTER_SIZE
) {
446 error_setg(errp
, "unsupported VDI image (block size %" PRIu32
447 " is not %" PRIu32
")",
448 header
.block_size
, DEFAULT_CLUSTER_SIZE
);
451 } else if (header
.disk_size
>
452 (uint64_t)header
.blocks_in_image
* header
.block_size
) {
453 error_setg(errp
, "unsupported VDI image (disk size %" PRIu64
", "
454 "image bitmap has room for %" PRIu64
")",
456 (uint64_t)header
.blocks_in_image
* header
.block_size
);
459 } else if (!qemu_uuid_is_null(&uuid_link
)) {
460 error_setg(errp
, "unsupported VDI image (non-NULL link UUID)");
463 } else if (!qemu_uuid_is_null(&uuid_parent
)) {
464 error_setg(errp
, "unsupported VDI image (non-NULL parent UUID)");
467 } else if (header
.blocks_in_image
> VDI_BLOCKS_IN_IMAGE_MAX
) {
468 error_setg(errp
, "unsupported VDI image "
469 "(too many blocks %u, max is %u)",
470 header
.blocks_in_image
, VDI_BLOCKS_IN_IMAGE_MAX
);
475 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
477 s
->block_size
= header
.block_size
;
478 s
->bmap_sector
= header
.offset_bmap
/ SECTOR_SIZE
;
481 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
482 bmap_size
= DIV_ROUND_UP(bmap_size
, SECTOR_SIZE
);
483 s
->bmap
= qemu_try_blockalign(bs
->file
->bs
, bmap_size
* SECTOR_SIZE
);
484 if (s
->bmap
== NULL
) {
489 ret
= bdrv_pread(bs
->file
, header
.offset_bmap
, s
->bmap
,
490 bmap_size
* SECTOR_SIZE
);
495 /* Disable migration when vdi images are used */
496 error_setg(&s
->migration_blocker
, "The vdi format used by node '%s' "
497 "does not support live migration",
498 bdrv_get_device_or_node_name(bs
));
499 ret
= migrate_add_blocker(s
->migration_blocker
, &local_err
);
501 error_propagate(errp
, local_err
);
502 error_free(s
->migration_blocker
);
506 qemu_co_rwlock_init(&s
->bmap_lock
);
517 static int vdi_reopen_prepare(BDRVReopenState
*state
,
518 BlockReopenQueue
*queue
, Error
**errp
)
523 static int coroutine_fn
vdi_co_block_status(BlockDriverState
*bs
,
525 int64_t offset
, int64_t bytes
,
526 int64_t *pnum
, int64_t *map
,
527 BlockDriverState
**file
)
529 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
530 size_t bmap_index
= offset
/ s
->block_size
;
531 size_t index_in_block
= offset
% s
->block_size
;
532 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
535 logout("%p, %" PRId64
", %" PRId64
", %p\n", bs
, offset
, bytes
, pnum
);
536 *pnum
= MIN(s
->block_size
- index_in_block
, bytes
);
537 result
= VDI_IS_ALLOCATED(bmap_entry
);
542 *map
= s
->header
.offset_data
+ (uint64_t)bmap_entry
* s
->block_size
+
544 *file
= bs
->file
->bs
;
545 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
;
548 static int coroutine_fn
549 vdi_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
550 QEMUIOVector
*qiov
, int flags
)
552 BDRVVdiState
*s
= bs
->opaque
;
553 QEMUIOVector local_qiov
;
555 uint32_t block_index
;
556 uint32_t offset_in_block
;
558 uint64_t bytes_done
= 0;
563 qemu_iovec_init(&local_qiov
, qiov
->niov
);
565 while (ret
>= 0 && bytes
> 0) {
566 block_index
= offset
/ s
->block_size
;
567 offset_in_block
= offset
% s
->block_size
;
568 n_bytes
= MIN(bytes
, s
->block_size
- offset_in_block
);
570 logout("will read %u bytes starting at offset %" PRIu64
"\n",
573 /* prepare next AIO request */
574 qemu_co_rwlock_rdlock(&s
->bmap_lock
);
575 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
576 qemu_co_rwlock_unlock(&s
->bmap_lock
);
577 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
578 /* Block not allocated, return zeros, no need to wait. */
579 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
582 uint64_t data_offset
= s
->header
.offset_data
+
583 (uint64_t)bmap_entry
* s
->block_size
+
586 qemu_iovec_reset(&local_qiov
);
587 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
589 ret
= bdrv_co_preadv(bs
->file
, data_offset
, n_bytes
,
592 logout("%u bytes read\n", n_bytes
);
596 bytes_done
+= n_bytes
;
599 qemu_iovec_destroy(&local_qiov
);
604 static int coroutine_fn
605 vdi_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
606 QEMUIOVector
*qiov
, int flags
)
608 BDRVVdiState
*s
= bs
->opaque
;
609 QEMUIOVector local_qiov
;
611 uint32_t block_index
;
612 uint32_t offset_in_block
;
614 uint64_t data_offset
;
615 uint32_t bmap_first
= VDI_UNALLOCATED
;
616 uint32_t bmap_last
= VDI_UNALLOCATED
;
617 uint8_t *block
= NULL
;
618 uint64_t bytes_done
= 0;
623 qemu_iovec_init(&local_qiov
, qiov
->niov
);
625 while (ret
>= 0 && bytes
> 0) {
626 block_index
= offset
/ s
->block_size
;
627 offset_in_block
= offset
% s
->block_size
;
628 n_bytes
= MIN(bytes
, s
->block_size
- offset_in_block
);
630 logout("will write %u bytes starting at offset %" PRIu64
"\n",
633 /* prepare next AIO request */
634 qemu_co_rwlock_rdlock(&s
->bmap_lock
);
635 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
636 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
637 /* Allocate new block and write to it. */
638 uint64_t data_offset
;
639 qemu_co_rwlock_upgrade(&s
->bmap_lock
);
640 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
641 if (VDI_IS_ALLOCATED(bmap_entry
)) {
642 /* A concurrent allocation did the work for us. */
643 qemu_co_rwlock_downgrade(&s
->bmap_lock
);
644 goto nonallocating_write
;
647 bmap_entry
= s
->header
.blocks_allocated
;
648 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
649 s
->header
.blocks_allocated
++;
650 data_offset
= s
->header
.offset_data
+
651 (uint64_t)bmap_entry
* s
->block_size
;
653 block
= g_malloc(s
->block_size
);
654 bmap_first
= block_index
;
656 bmap_last
= block_index
;
657 /* Copy data to be written to new block and zero unused parts. */
658 memset(block
, 0, offset_in_block
);
659 qemu_iovec_to_buf(qiov
, bytes_done
, block
+ offset_in_block
,
661 memset(block
+ offset_in_block
+ n_bytes
, 0,
662 s
->block_size
- n_bytes
- offset_in_block
);
664 /* Write the new block under CoRwLock write-side protection,
665 * so this full-cluster write does not overlap a partial write
666 * of the same cluster, issued from the "else" branch.
668 ret
= bdrv_pwrite(bs
->file
, data_offset
, block
, s
->block_size
);
669 qemu_co_rwlock_unlock(&s
->bmap_lock
);
672 data_offset
= s
->header
.offset_data
+
673 (uint64_t)bmap_entry
* s
->block_size
+
675 qemu_co_rwlock_unlock(&s
->bmap_lock
);
677 qemu_iovec_reset(&local_qiov
);
678 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
680 ret
= bdrv_co_pwritev(bs
->file
, data_offset
, n_bytes
,
686 bytes_done
+= n_bytes
;
688 logout("%u bytes written\n", n_bytes
);
691 qemu_iovec_destroy(&local_qiov
);
693 logout("finished data write\n");
699 /* One or more new blocks were allocated. */
700 VdiHeader
*header
= (VdiHeader
*) block
;
705 logout("now writing modified header\n");
706 assert(VDI_IS_ALLOCATED(bmap_first
));
708 vdi_header_to_le(header
);
709 ret
= bdrv_pwrite(bs
->file
, 0, block
, sizeof(VdiHeader
));
717 logout("now writing modified block map entry %u...%u\n",
718 bmap_first
, bmap_last
);
719 /* Write modified sectors from block map. */
720 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
721 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
722 n_sectors
= bmap_last
- bmap_first
+ 1;
723 offset
= s
->bmap_sector
+ bmap_first
;
724 base
= ((uint8_t *)&s
->bmap
[0]) + bmap_first
* SECTOR_SIZE
;
725 logout("will write %u block map sectors starting from entry %u\n",
726 n_sectors
, bmap_first
);
727 ret
= bdrv_pwrite(bs
->file
, offset
* SECTOR_SIZE
, base
,
728 n_sectors
* SECTOR_SIZE
);
731 return ret
< 0 ? ret
: 0;
734 static int coroutine_fn
vdi_co_do_create(BlockdevCreateOptions
*create_options
,
735 size_t block_size
, Error
**errp
)
737 BlockdevCreateOptionsVdi
*vdi_opts
;
746 BlockDriverState
*bs_file
= NULL
;
747 BlockBackend
*blk
= NULL
;
748 uint32_t *bmap
= NULL
;
751 assert(create_options
->driver
== BLOCKDEV_DRIVER_VDI
);
752 vdi_opts
= &create_options
->u
.vdi
;
756 /* Validate options and set default values */
757 bytes
= vdi_opts
->size
;
759 if (!vdi_opts
->has_preallocation
) {
760 vdi_opts
->preallocation
= PREALLOC_MODE_OFF
;
762 switch (vdi_opts
->preallocation
) {
763 case PREALLOC_MODE_OFF
:
764 image_type
= VDI_TYPE_DYNAMIC
;
766 case PREALLOC_MODE_METADATA
:
767 image_type
= VDI_TYPE_STATIC
;
770 error_setg(errp
, "Preallocation mode not supported for vdi");
774 #ifndef CONFIG_VDI_STATIC_IMAGE
775 if (image_type
== VDI_TYPE_STATIC
) {
777 error_setg(errp
, "Statically allocated images cannot be created in "
782 #ifndef CONFIG_VDI_BLOCK_SIZE
783 if (block_size
!= DEFAULT_CLUSTER_SIZE
) {
786 "A non-default cluster size is not supported in this build");
791 if (bytes
> VDI_DISK_SIZE_MAX
) {
793 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
794 ", max supported is 0x%" PRIx64
")",
795 bytes
, VDI_DISK_SIZE_MAX
);
799 /* Create BlockBackend to write to the image */
800 bs_file
= bdrv_open_blockdev_ref(vdi_opts
->file
, errp
);
806 blk
= blk_new(bdrv_get_aio_context(bs_file
),
807 BLK_PERM_WRITE
| BLK_PERM_RESIZE
, BLK_PERM_ALL
);
808 ret
= blk_insert_bs(blk
, bs_file
, errp
);
813 blk_set_allow_write_beyond_eof(blk
, true);
815 /* We need enough blocks to store the given disk size,
816 so always round up. */
817 blocks
= DIV_ROUND_UP(bytes
, block_size
);
819 bmap_size
= blocks
* sizeof(uint32_t);
820 bmap_size
= ROUND_UP(bmap_size
, SECTOR_SIZE
);
822 memset(&header
, 0, sizeof(header
));
823 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
824 header
.signature
= VDI_SIGNATURE
;
825 header
.version
= VDI_VERSION_1_1
;
826 header
.header_size
= 0x180;
827 header
.image_type
= image_type
;
828 header
.offset_bmap
= 0x200;
829 header
.offset_data
= 0x200 + bmap_size
;
830 header
.sector_size
= SECTOR_SIZE
;
831 header
.disk_size
= bytes
;
832 header
.block_size
= block_size
;
833 header
.blocks_in_image
= blocks
;
834 if (image_type
== VDI_TYPE_STATIC
) {
835 header
.blocks_allocated
= blocks
;
837 qemu_uuid_generate(&uuid
);
838 header
.uuid_image
= uuid
;
839 qemu_uuid_generate(&uuid
);
840 header
.uuid_last_snap
= uuid
;
841 /* There is no need to set header.uuid_link or header.uuid_parent here. */
843 vdi_header_print(&header
);
845 vdi_header_to_le(&header
);
846 ret
= blk_pwrite(blk
, offset
, &header
, sizeof(header
), 0);
848 error_setg(errp
, "Error writing header");
851 offset
+= sizeof(header
);
854 bmap
= g_try_malloc0(bmap_size
);
857 error_setg(errp
, "Could not allocate bmap");
860 for (i
= 0; i
< blocks
; i
++) {
861 if (image_type
== VDI_TYPE_STATIC
) {
864 bmap
[i
] = VDI_UNALLOCATED
;
867 ret
= blk_pwrite(blk
, offset
, bmap
, bmap_size
, 0);
869 error_setg(errp
, "Error writing bmap");
875 if (image_type
== VDI_TYPE_STATIC
) {
876 ret
= blk_truncate(blk
, offset
+ blocks
* block_size
,
877 PREALLOC_MODE_OFF
, errp
);
879 error_prepend(errp
, "Failed to statically allocate file");
892 static int coroutine_fn
vdi_co_create(BlockdevCreateOptions
*create_options
,
895 return vdi_co_do_create(create_options
, DEFAULT_CLUSTER_SIZE
, errp
);
898 static int coroutine_fn
vdi_co_create_opts(const char *filename
, QemuOpts
*opts
,
902 BlockdevCreateOptions
*create_options
= NULL
;
903 BlockDriverState
*bs_file
= NULL
;
904 uint64_t block_size
= DEFAULT_CLUSTER_SIZE
;
905 bool is_static
= false;
907 Error
*local_err
= NULL
;
910 /* Parse options and convert legacy syntax.
912 * Since CONFIG_VDI_BLOCK_SIZE is disabled by default,
913 * cluster-size is not part of the QAPI schema; therefore we have
914 * to parse it before creating the QAPI object. */
915 #if defined(CONFIG_VDI_BLOCK_SIZE)
916 block_size
= qemu_opt_get_size_del(opts
,
917 BLOCK_OPT_CLUSTER_SIZE
,
918 DEFAULT_CLUSTER_SIZE
);
919 if (block_size
< BDRV_SECTOR_SIZE
|| block_size
> UINT32_MAX
||
920 !is_power_of_2(block_size
))
922 error_setg(errp
, "Invalid cluster size");
927 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_STATIC
, false)) {
931 qdict
= qemu_opts_to_qdict_filtered(opts
, NULL
, &vdi_create_opts
, true);
933 /* Create and open the file (protocol layer) */
934 ret
= bdrv_create_file(filename
, opts
, errp
);
939 bs_file
= bdrv_open(filename
, NULL
, NULL
,
940 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
946 qdict_put_str(qdict
, "driver", "vdi");
947 qdict_put_str(qdict
, "file", bs_file
->node_name
);
949 qdict_put_str(qdict
, "preallocation", "metadata");
952 /* Get the QAPI object */
953 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
958 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, &local_err
);
962 error_propagate(errp
, local_err
);
967 /* Silently round up size */
968 assert(create_options
->driver
== BLOCKDEV_DRIVER_VDI
);
969 create_options
->u
.vdi
.size
= ROUND_UP(create_options
->u
.vdi
.size
,
972 /* Create the vdi image (format layer) */
973 ret
= vdi_co_do_create(create_options
, block_size
, errp
);
975 qobject_unref(qdict
);
976 qapi_free_BlockdevCreateOptions(create_options
);
981 static void vdi_close(BlockDriverState
*bs
)
983 BDRVVdiState
*s
= bs
->opaque
;
987 migrate_del_blocker(s
->migration_blocker
);
988 error_free(s
->migration_blocker
);
991 static QemuOptsList vdi_create_opts
= {
992 .name
= "vdi-create-opts",
993 .head
= QTAILQ_HEAD_INITIALIZER(vdi_create_opts
.head
),
996 .name
= BLOCK_OPT_SIZE
,
997 .type
= QEMU_OPT_SIZE
,
998 .help
= "Virtual disk size"
1000 #if defined(CONFIG_VDI_BLOCK_SIZE)
1002 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1003 .type
= QEMU_OPT_SIZE
,
1004 .help
= "VDI cluster (block) size",
1005 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
1008 #if defined(CONFIG_VDI_STATIC_IMAGE)
1010 .name
= BLOCK_OPT_STATIC
,
1011 .type
= QEMU_OPT_BOOL
,
1012 .help
= "VDI static (pre-allocated) image",
1013 .def_value_str
= "off"
1016 /* TODO: An additional option to set UUID values might be useful. */
1017 { /* end of list */ }
1021 static BlockDriver bdrv_vdi
= {
1022 .format_name
= "vdi",
1023 .instance_size
= sizeof(BDRVVdiState
),
1024 .bdrv_probe
= vdi_probe
,
1025 .bdrv_open
= vdi_open
,
1026 .bdrv_close
= vdi_close
,
1027 .bdrv_reopen_prepare
= vdi_reopen_prepare
,
1028 .bdrv_child_perm
= bdrv_format_default_perms
,
1029 .bdrv_co_create
= vdi_co_create
,
1030 .bdrv_co_create_opts
= vdi_co_create_opts
,
1031 .bdrv_has_zero_init
= bdrv_has_zero_init_1
,
1032 .bdrv_co_block_status
= vdi_co_block_status
,
1033 .bdrv_make_empty
= vdi_make_empty
,
1035 .bdrv_co_preadv
= vdi_co_preadv
,
1036 #if defined(CONFIG_VDI_WRITE)
1037 .bdrv_co_pwritev
= vdi_co_pwritev
,
1040 .bdrv_get_info
= vdi_get_info
,
1042 .create_opts
= &vdi_create_opts
,
1043 .bdrv_co_check
= vdi_co_check
,
1046 static void bdrv_vdi_init(void)
1049 bdrv_register(&bdrv_vdi
);
1052 block_init(bdrv_vdi_init
);