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"
67 #include "qemu/memalign.h"
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"
88 #define SECTOR_SIZE 512
89 #define DEFAULT_CLUSTER_SIZE 1048576
90 /* Note: can't use 1 * MiB, because it's passed to stringify() */
92 #if defined(CONFIG_VDI_DEBUG)
98 #define logout(fmt, ...) \
101 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \
105 /* Image signature. */
106 #define VDI_SIGNATURE 0xbeda107f
109 #define VDI_VERSION_1_1 0x00010001
112 #define VDI_TYPE_DYNAMIC 1
113 #define VDI_TYPE_STATIC 2
115 /* Innotek / SUN images use these strings in header.text:
116 * "<<< innotek VirtualBox Disk Image >>>\n"
117 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
118 * "<<< Sun VirtualBox Disk Image >>>\n"
119 * The value does not matter, so QEMU created images use a different text.
121 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
123 /* A never-allocated block; semantically arbitrary content. */
124 #define VDI_UNALLOCATED 0xffffffffU
126 /* A discarded (no longer allocated) block; semantically zero-filled. */
127 #define VDI_DISCARDED 0xfffffffeU
129 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
131 /* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since
132 * the bmap is read and written in a single operation, its size needs to be
133 * limited to INT_MAX; furthermore, when opening an image, the bmap size is
134 * rounded up to be aligned on BDRV_SECTOR_SIZE.
135 * Therefore this should satisfy the following:
136 * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1
137 * (INT_MAX + 1 is the first value not representable as an int)
138 * This guarantees that any value below or equal to the constant will, when
139 * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary,
140 * still be below or equal to INT_MAX. */
141 #define VDI_BLOCKS_IN_IMAGE_MAX \
142 ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t)))
143 #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
144 (uint64_t)DEFAULT_CLUSTER_SIZE)
146 static QemuOptsList vdi_create_opts
;
152 uint32_t header_size
;
154 uint32_t image_flags
;
155 char description
[256];
156 uint32_t offset_bmap
;
157 uint32_t offset_data
;
158 uint32_t cylinders
; /* disk geometry, unused here */
159 uint32_t heads
; /* disk geometry, unused here */
160 uint32_t sectors
; /* disk geometry, unused here */
161 uint32_t sector_size
;
165 uint32_t block_extra
; /* unused here */
166 uint32_t blocks_in_image
;
167 uint32_t blocks_allocated
;
169 QemuUUID uuid_last_snap
;
171 QemuUUID uuid_parent
;
173 } QEMU_PACKED VdiHeader
;
175 QEMU_BUILD_BUG_ON(sizeof(VdiHeader
) != 512);
178 /* The block map entries are little endian (even in memory). */
180 /* Size of block (bytes). */
182 /* First sector of block map. */
183 uint32_t bmap_sector
;
184 /* VDI header (converted to host endianness). */
189 Error
*migration_blocker
;
192 static void vdi_header_to_cpu(VdiHeader
*header
)
194 header
->signature
= le32_to_cpu(header
->signature
);
195 header
->version
= le32_to_cpu(header
->version
);
196 header
->header_size
= le32_to_cpu(header
->header_size
);
197 header
->image_type
= le32_to_cpu(header
->image_type
);
198 header
->image_flags
= le32_to_cpu(header
->image_flags
);
199 header
->offset_bmap
= le32_to_cpu(header
->offset_bmap
);
200 header
->offset_data
= le32_to_cpu(header
->offset_data
);
201 header
->cylinders
= le32_to_cpu(header
->cylinders
);
202 header
->heads
= le32_to_cpu(header
->heads
);
203 header
->sectors
= le32_to_cpu(header
->sectors
);
204 header
->sector_size
= le32_to_cpu(header
->sector_size
);
205 header
->disk_size
= le64_to_cpu(header
->disk_size
);
206 header
->block_size
= le32_to_cpu(header
->block_size
);
207 header
->block_extra
= le32_to_cpu(header
->block_extra
);
208 header
->blocks_in_image
= le32_to_cpu(header
->blocks_in_image
);
209 header
->blocks_allocated
= le32_to_cpu(header
->blocks_allocated
);
210 header
->uuid_image
= qemu_uuid_bswap(header
->uuid_image
);
211 header
->uuid_last_snap
= qemu_uuid_bswap(header
->uuid_last_snap
);
212 header
->uuid_link
= qemu_uuid_bswap(header
->uuid_link
);
213 header
->uuid_parent
= qemu_uuid_bswap(header
->uuid_parent
);
216 static void vdi_header_to_le(VdiHeader
*header
)
218 header
->signature
= cpu_to_le32(header
->signature
);
219 header
->version
= cpu_to_le32(header
->version
);
220 header
->header_size
= cpu_to_le32(header
->header_size
);
221 header
->image_type
= cpu_to_le32(header
->image_type
);
222 header
->image_flags
= cpu_to_le32(header
->image_flags
);
223 header
->offset_bmap
= cpu_to_le32(header
->offset_bmap
);
224 header
->offset_data
= cpu_to_le32(header
->offset_data
);
225 header
->cylinders
= cpu_to_le32(header
->cylinders
);
226 header
->heads
= cpu_to_le32(header
->heads
);
227 header
->sectors
= cpu_to_le32(header
->sectors
);
228 header
->sector_size
= cpu_to_le32(header
->sector_size
);
229 header
->disk_size
= cpu_to_le64(header
->disk_size
);
230 header
->block_size
= cpu_to_le32(header
->block_size
);
231 header
->block_extra
= cpu_to_le32(header
->block_extra
);
232 header
->blocks_in_image
= cpu_to_le32(header
->blocks_in_image
);
233 header
->blocks_allocated
= cpu_to_le32(header
->blocks_allocated
);
234 header
->uuid_image
= qemu_uuid_bswap(header
->uuid_image
);
235 header
->uuid_last_snap
= qemu_uuid_bswap(header
->uuid_last_snap
);
236 header
->uuid_link
= qemu_uuid_bswap(header
->uuid_link
);
237 header
->uuid_parent
= qemu_uuid_bswap(header
->uuid_parent
);
240 static void vdi_header_print(VdiHeader
*header
)
244 logout("text %s", header
->text
);
245 logout("signature 0x%08x\n", header
->signature
);
246 logout("header size 0x%04x\n", header
->header_size
);
247 logout("image type 0x%04x\n", header
->image_type
);
248 logout("image flags 0x%04x\n", header
->image_flags
);
249 logout("description %s\n", header
->description
);
250 logout("offset bmap 0x%04x\n", header
->offset_bmap
);
251 logout("offset data 0x%04x\n", header
->offset_data
);
252 logout("cylinders 0x%04x\n", header
->cylinders
);
253 logout("heads 0x%04x\n", header
->heads
);
254 logout("sectors 0x%04x\n", header
->sectors
);
255 logout("sector size 0x%04x\n", header
->sector_size
);
256 logout("image size 0x%" PRIx64
" B (%" PRIu64
" MiB)\n",
257 header
->disk_size
, header
->disk_size
/ MiB
);
258 logout("block size 0x%04x\n", header
->block_size
);
259 logout("block extra 0x%04x\n", header
->block_extra
);
260 logout("blocks tot. 0x%04x\n", header
->blocks_in_image
);
261 logout("blocks all. 0x%04x\n", header
->blocks_allocated
);
262 uuid
= header
->uuid_image
;
263 qemu_uuid_unparse(&uuid
, uuidstr
);
264 logout("uuid image %s\n", uuidstr
);
265 uuid
= header
->uuid_last_snap
;
266 qemu_uuid_unparse(&uuid
, uuidstr
);
267 logout("uuid snap %s\n", uuidstr
);
268 uuid
= header
->uuid_link
;
269 qemu_uuid_unparse(&uuid
, uuidstr
);
270 logout("uuid link %s\n", uuidstr
);
271 uuid
= header
->uuid_parent
;
272 qemu_uuid_unparse(&uuid
, uuidstr
);
273 logout("uuid parent %s\n", uuidstr
);
276 static int coroutine_fn
vdi_co_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
279 /* TODO: additional checks possible. */
280 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
281 uint32_t blocks_allocated
= 0;
290 bmap
= g_try_new(uint32_t, s
->header
.blocks_in_image
);
291 if (s
->header
.blocks_in_image
&& bmap
== NULL
) {
296 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
298 /* Check block map and value of blocks_allocated. */
299 for (block
= 0; block
< s
->header
.blocks_in_image
; block
++) {
300 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[block
]);
301 if (VDI_IS_ALLOCATED(bmap_entry
)) {
302 if (bmap_entry
< s
->header
.blocks_in_image
) {
304 if (!VDI_IS_ALLOCATED(bmap
[bmap_entry
])) {
305 bmap
[bmap_entry
] = bmap_entry
;
307 fprintf(stderr
, "ERROR: block index %" PRIu32
308 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
312 fprintf(stderr
, "ERROR: block index %" PRIu32
313 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
318 if (blocks_allocated
!= s
->header
.blocks_allocated
) {
319 fprintf(stderr
, "ERROR: allocated blocks mismatch, is %" PRIu32
320 ", should be %" PRIu32
"\n",
321 blocks_allocated
, s
->header
.blocks_allocated
);
330 static int coroutine_fn
331 vdi_co_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
333 /* TODO: vdi_co_get_info would be needed for machine snapshots.
334 vm_state_offset is still missing. */
335 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
337 bdi
->cluster_size
= s
->block_size
;
338 bdi
->vm_state_offset
= 0;
342 static int vdi_make_empty(BlockDriverState
*bs
)
344 /* TODO: missing code. */
346 /* The return value for missing code must be 0, see block.c. */
350 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
352 const VdiHeader
*header
= (const VdiHeader
*)buf
;
357 if (buf_size
< sizeof(*header
)) {
358 /* Header too small, no VDI. */
359 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
364 logout("no vdi image\n");
366 logout("%s", header
->text
);
372 static int vdi_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
375 BDRVVdiState
*s
= bs
->opaque
;
379 QemuUUID uuid_link
, uuid_parent
;
381 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
388 ret
= bdrv_pread(bs
->file
, 0, sizeof(header
), &header
, 0);
393 vdi_header_to_cpu(&header
);
395 vdi_header_print(&header
);
398 if (header
.disk_size
> VDI_DISK_SIZE_MAX
) {
399 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
400 ", max supported is 0x%" PRIx64
")",
401 header
.disk_size
, VDI_DISK_SIZE_MAX
);
406 uuid_link
= header
.uuid_link
;
407 uuid_parent
= header
.uuid_parent
;
409 if (header
.disk_size
% SECTOR_SIZE
!= 0) {
410 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
411 We accept them but round the disk size to the next multiple of
413 logout("odd disk size %" PRIu64
" B, round up\n", header
.disk_size
);
414 header
.disk_size
= ROUND_UP(header
.disk_size
, SECTOR_SIZE
);
417 if (header
.signature
!= VDI_SIGNATURE
) {
418 error_setg(errp
, "Image not in VDI format (bad signature %08" PRIx32
419 ")", header
.signature
);
422 } else if (header
.version
!= VDI_VERSION_1_1
) {
423 error_setg(errp
, "unsupported VDI image (version %" PRIu32
".%" PRIu32
424 ")", header
.version
>> 16, header
.version
& 0xffff);
427 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
428 /* We only support block maps which start on a sector boundary. */
429 error_setg(errp
, "unsupported VDI image (unaligned block map offset "
430 "0x%" PRIx32
")", header
.offset_bmap
);
433 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
434 /* We only support data blocks which start on a sector boundary. */
435 error_setg(errp
, "unsupported VDI image (unaligned data offset 0x%"
436 PRIx32
")", header
.offset_data
);
439 } else if (header
.sector_size
!= SECTOR_SIZE
) {
440 error_setg(errp
, "unsupported VDI image (sector size %" PRIu32
441 " is not %u)", header
.sector_size
, SECTOR_SIZE
);
444 } else if (header
.block_size
!= DEFAULT_CLUSTER_SIZE
) {
445 error_setg(errp
, "unsupported VDI image (block size %" PRIu32
446 " is not %" PRIu32
")",
447 header
.block_size
, DEFAULT_CLUSTER_SIZE
);
450 } else if (header
.disk_size
>
451 (uint64_t)header
.blocks_in_image
* header
.block_size
) {
452 error_setg(errp
, "unsupported VDI image (disk size %" PRIu64
", "
453 "image bitmap has room for %" PRIu64
")",
455 (uint64_t)header
.blocks_in_image
* header
.block_size
);
458 } else if (!qemu_uuid_is_null(&uuid_link
)) {
459 error_setg(errp
, "unsupported VDI image (non-NULL link UUID)");
462 } else if (!qemu_uuid_is_null(&uuid_parent
)) {
463 error_setg(errp
, "unsupported VDI image (non-NULL parent UUID)");
466 } else if (header
.blocks_in_image
> VDI_BLOCKS_IN_IMAGE_MAX
) {
467 error_setg(errp
, "unsupported VDI image "
468 "(too many blocks %u, max is %u)",
469 header
.blocks_in_image
, VDI_BLOCKS_IN_IMAGE_MAX
);
474 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
476 s
->block_size
= header
.block_size
;
477 s
->bmap_sector
= header
.offset_bmap
/ SECTOR_SIZE
;
480 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
481 bmap_size
= DIV_ROUND_UP(bmap_size
, SECTOR_SIZE
);
482 s
->bmap
= qemu_try_blockalign(bs
->file
->bs
, bmap_size
* SECTOR_SIZE
);
483 if (s
->bmap
== NULL
) {
488 ret
= bdrv_pread(bs
->file
, header
.offset_bmap
, bmap_size
* SECTOR_SIZE
,
494 /* Disable migration when vdi images are used */
495 error_setg(&s
->migration_blocker
, "The vdi format used by node '%s' "
496 "does not support live migration",
497 bdrv_get_device_or_node_name(bs
));
498 ret
= migrate_add_blocker(s
->migration_blocker
, errp
);
500 error_free(s
->migration_blocker
);
504 qemu_co_rwlock_init(&s
->bmap_lock
);
515 static int vdi_reopen_prepare(BDRVReopenState
*state
,
516 BlockReopenQueue
*queue
, Error
**errp
)
521 static int coroutine_fn
vdi_co_block_status(BlockDriverState
*bs
,
523 int64_t offset
, int64_t bytes
,
524 int64_t *pnum
, int64_t *map
,
525 BlockDriverState
**file
)
527 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
528 size_t bmap_index
= offset
/ s
->block_size
;
529 size_t index_in_block
= offset
% s
->block_size
;
530 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
533 logout("%p, %" PRId64
", %" PRId64
", %p\n", bs
, offset
, bytes
, pnum
);
534 *pnum
= MIN(s
->block_size
- index_in_block
, bytes
);
535 result
= VDI_IS_ALLOCATED(bmap_entry
);
537 return BDRV_BLOCK_ZERO
;
540 *map
= s
->header
.offset_data
+ (uint64_t)bmap_entry
* s
->block_size
+
542 *file
= bs
->file
->bs
;
543 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
|
544 (s
->header
.image_type
== VDI_TYPE_STATIC
? BDRV_BLOCK_RECURSE
: 0);
547 static int coroutine_fn
548 vdi_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
549 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
551 BDRVVdiState
*s
= bs
->opaque
;
552 QEMUIOVector local_qiov
;
554 uint32_t block_index
;
555 uint32_t offset_in_block
;
557 uint64_t bytes_done
= 0;
562 qemu_iovec_init(&local_qiov
, qiov
->niov
);
564 while (ret
>= 0 && bytes
> 0) {
565 block_index
= offset
/ s
->block_size
;
566 offset_in_block
= offset
% s
->block_size
;
567 n_bytes
= MIN(bytes
, s
->block_size
- offset_in_block
);
569 logout("will read %u bytes starting at offset %" PRIu64
"\n",
572 /* prepare next AIO request */
573 qemu_co_rwlock_rdlock(&s
->bmap_lock
);
574 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
575 qemu_co_rwlock_unlock(&s
->bmap_lock
);
576 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
577 /* Block not allocated, return zeros, no need to wait. */
578 qemu_iovec_memset(qiov
, bytes_done
, 0, n_bytes
);
581 uint64_t data_offset
= s
->header
.offset_data
+
582 (uint64_t)bmap_entry
* s
->block_size
+
585 qemu_iovec_reset(&local_qiov
);
586 qemu_iovec_concat(&local_qiov
, qiov
, bytes_done
, n_bytes
);
588 ret
= bdrv_co_preadv(bs
->file
, data_offset
, n_bytes
,
591 logout("%u bytes read\n", n_bytes
);
595 bytes_done
+= n_bytes
;
598 qemu_iovec_destroy(&local_qiov
);
603 static int coroutine_fn
604 vdi_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
605 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
607 BDRVVdiState
*s
= bs
->opaque
;
608 QEMUIOVector local_qiov
;
610 uint32_t block_index
;
611 uint32_t offset_in_block
;
613 uint64_t data_offset
;
614 uint32_t bmap_first
= VDI_UNALLOCATED
;
615 uint32_t bmap_last
= VDI_UNALLOCATED
;
616 uint8_t *block
= NULL
;
617 uint64_t bytes_done
= 0;
622 qemu_iovec_init(&local_qiov
, qiov
->niov
);
624 while (ret
>= 0 && bytes
> 0) {
625 block_index
= offset
/ s
->block_size
;
626 offset_in_block
= offset
% s
->block_size
;
627 n_bytes
= MIN(bytes
, s
->block_size
- offset_in_block
);
629 logout("will write %u bytes starting at offset %" PRIu64
"\n",
632 /* prepare next AIO request */
633 qemu_co_rwlock_rdlock(&s
->bmap_lock
);
634 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
635 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
636 /* Allocate new block and write to it. */
637 uint64_t data_offset
;
638 qemu_co_rwlock_upgrade(&s
->bmap_lock
);
639 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
640 if (VDI_IS_ALLOCATED(bmap_entry
)) {
641 /* A concurrent allocation did the work for us. */
642 qemu_co_rwlock_downgrade(&s
->bmap_lock
);
643 goto nonallocating_write
;
646 bmap_entry
= s
->header
.blocks_allocated
;
647 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
648 s
->header
.blocks_allocated
++;
649 data_offset
= s
->header
.offset_data
+
650 (uint64_t)bmap_entry
* s
->block_size
;
652 block
= g_malloc(s
->block_size
);
653 bmap_first
= block_index
;
655 bmap_last
= block_index
;
656 /* Copy data to be written to new block and zero unused parts. */
657 memset(block
, 0, offset_in_block
);
658 qemu_iovec_to_buf(qiov
, bytes_done
, block
+ offset_in_block
,
660 memset(block
+ offset_in_block
+ n_bytes
, 0,
661 s
->block_size
- n_bytes
- offset_in_block
);
663 /* Write the new block under CoRwLock write-side protection,
664 * so this full-cluster write does not overlap a partial write
665 * of the same cluster, issued from the "else" branch.
667 ret
= bdrv_co_pwrite(bs
->file
, data_offset
, s
->block_size
, block
,
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");
700 /* One or more new blocks were allocated. */
707 header
= g_malloc(sizeof(*header
));
709 logout("now writing modified header\n");
710 assert(VDI_IS_ALLOCATED(bmap_first
));
712 vdi_header_to_le(header
);
713 ret
= bdrv_co_pwrite(bs
->file
, 0, sizeof(*header
), header
, 0);
720 logout("now writing modified block map entry %u...%u\n",
721 bmap_first
, bmap_last
);
722 /* Write modified sectors from block map. */
723 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
724 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
725 n_sectors
= bmap_last
- bmap_first
+ 1;
726 offset
= s
->bmap_sector
+ bmap_first
;
727 base
= ((uint8_t *)&s
->bmap
[0]) + bmap_first
* SECTOR_SIZE
;
728 logout("will write %u block map sectors starting from entry %u\n",
729 n_sectors
, bmap_first
);
730 ret
= bdrv_co_pwrite(bs
->file
, offset
* SECTOR_SIZE
,
731 n_sectors
* SECTOR_SIZE
, base
, 0);
737 static int coroutine_fn
vdi_co_do_create(BlockdevCreateOptions
*create_options
,
738 size_t block_size
, Error
**errp
)
740 BlockdevCreateOptionsVdi
*vdi_opts
;
749 BlockDriverState
*bs_file
= NULL
;
750 BlockBackend
*blk
= NULL
;
751 uint32_t *bmap
= NULL
;
754 assert(create_options
->driver
== BLOCKDEV_DRIVER_VDI
);
755 vdi_opts
= &create_options
->u
.vdi
;
759 /* Validate options and set default values */
760 bytes
= vdi_opts
->size
;
762 if (!vdi_opts
->has_preallocation
) {
763 vdi_opts
->preallocation
= PREALLOC_MODE_OFF
;
765 switch (vdi_opts
->preallocation
) {
766 case PREALLOC_MODE_OFF
:
767 image_type
= VDI_TYPE_DYNAMIC
;
769 case PREALLOC_MODE_METADATA
:
770 image_type
= VDI_TYPE_STATIC
;
773 error_setg(errp
, "Preallocation mode not supported for vdi");
777 #ifndef CONFIG_VDI_STATIC_IMAGE
778 if (image_type
== VDI_TYPE_STATIC
) {
780 error_setg(errp
, "Statically allocated images cannot be created in "
785 #ifndef CONFIG_VDI_BLOCK_SIZE
786 if (block_size
!= DEFAULT_CLUSTER_SIZE
) {
789 "A non-default cluster size is not supported in this build");
794 if (bytes
> VDI_DISK_SIZE_MAX
) {
796 error_setg(errp
, "Unsupported VDI image size (size is 0x%" PRIx64
797 ", max supported is 0x%" PRIx64
")",
798 bytes
, VDI_DISK_SIZE_MAX
);
802 /* Create BlockBackend to write to the image */
803 bs_file
= bdrv_open_blockdev_ref(vdi_opts
->file
, errp
);
809 blk
= blk_new_with_bs(bs_file
, BLK_PERM_WRITE
| BLK_PERM_RESIZE
,
816 blk_set_allow_write_beyond_eof(blk
, true);
818 /* We need enough blocks to store the given disk size,
819 so always round up. */
820 blocks
= DIV_ROUND_UP(bytes
, block_size
);
822 bmap_size
= blocks
* sizeof(uint32_t);
823 bmap_size
= ROUND_UP(bmap_size
, SECTOR_SIZE
);
825 memset(&header
, 0, sizeof(header
));
826 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
827 header
.signature
= VDI_SIGNATURE
;
828 header
.version
= VDI_VERSION_1_1
;
829 header
.header_size
= 0x180;
830 header
.image_type
= image_type
;
831 header
.offset_bmap
= 0x200;
832 header
.offset_data
= 0x200 + bmap_size
;
833 header
.sector_size
= SECTOR_SIZE
;
834 header
.disk_size
= bytes
;
835 header
.block_size
= block_size
;
836 header
.blocks_in_image
= blocks
;
837 if (image_type
== VDI_TYPE_STATIC
) {
838 header
.blocks_allocated
= blocks
;
840 qemu_uuid_generate(&uuid
);
841 header
.uuid_image
= uuid
;
842 qemu_uuid_generate(&uuid
);
843 header
.uuid_last_snap
= uuid
;
844 /* There is no need to set header.uuid_link or header.uuid_parent here. */
846 vdi_header_print(&header
);
848 vdi_header_to_le(&header
);
849 ret
= blk_co_pwrite(blk
, offset
, sizeof(header
), &header
, 0);
851 error_setg(errp
, "Error writing header");
854 offset
+= sizeof(header
);
857 bmap
= g_try_malloc0(bmap_size
);
860 error_setg(errp
, "Could not allocate bmap");
863 for (i
= 0; i
< blocks
; i
++) {
864 if (image_type
== VDI_TYPE_STATIC
) {
867 bmap
[i
] = VDI_UNALLOCATED
;
870 ret
= blk_co_pwrite(blk
, offset
, bmap_size
, bmap
, 0);
872 error_setg(errp
, "Error writing bmap");
878 if (image_type
== VDI_TYPE_STATIC
) {
879 ret
= blk_co_truncate(blk
, offset
+ blocks
* block_size
, false,
880 PREALLOC_MODE_OFF
, 0, errp
);
882 error_prepend(errp
, "Failed to statically allocate file");
895 static int coroutine_fn
vdi_co_create(BlockdevCreateOptions
*create_options
,
898 return vdi_co_do_create(create_options
, DEFAULT_CLUSTER_SIZE
, errp
);
901 static int coroutine_fn
vdi_co_create_opts(BlockDriver
*drv
,
902 const char *filename
,
907 BlockdevCreateOptions
*create_options
= NULL
;
908 BlockDriverState
*bs_file
= NULL
;
909 uint64_t block_size
= DEFAULT_CLUSTER_SIZE
;
910 bool is_static
= false;
914 /* Parse options and convert legacy syntax.
916 * Since CONFIG_VDI_BLOCK_SIZE is disabled by default,
917 * cluster-size is not part of the QAPI schema; therefore we have
918 * to parse it before creating the QAPI object. */
919 #if defined(CONFIG_VDI_BLOCK_SIZE)
920 block_size
= qemu_opt_get_size_del(opts
,
921 BLOCK_OPT_CLUSTER_SIZE
,
922 DEFAULT_CLUSTER_SIZE
);
923 if (block_size
< BDRV_SECTOR_SIZE
|| block_size
> UINT32_MAX
||
924 !is_power_of_2(block_size
))
926 error_setg(errp
, "Invalid cluster size");
931 if (qemu_opt_get_bool_del(opts
, BLOCK_OPT_STATIC
, false)) {
935 qdict
= qemu_opts_to_qdict_filtered(opts
, NULL
, &vdi_create_opts
, true);
937 /* Create and open the file (protocol layer) */
938 ret
= bdrv_co_create_file(filename
, opts
, errp
);
943 bs_file
= bdrv_open(filename
, NULL
, NULL
,
944 BDRV_O_RDWR
| BDRV_O_RESIZE
| BDRV_O_PROTOCOL
, errp
);
950 qdict_put_str(qdict
, "driver", "vdi");
951 qdict_put_str(qdict
, "file", bs_file
->node_name
);
953 qdict_put_str(qdict
, "preallocation", "metadata");
956 /* Get the QAPI object */
957 v
= qobject_input_visitor_new_flat_confused(qdict
, errp
);
962 visit_type_BlockdevCreateOptions(v
, NULL
, &create_options
, errp
);
964 if (!create_options
) {
969 /* Silently round up size */
970 assert(create_options
->driver
== BLOCKDEV_DRIVER_VDI
);
971 create_options
->u
.vdi
.size
= ROUND_UP(create_options
->u
.vdi
.size
,
974 /* Create the vdi image (format layer) */
975 ret
= vdi_co_do_create(create_options
, block_size
, errp
);
977 qobject_unref(qdict
);
978 qapi_free_BlockdevCreateOptions(create_options
);
983 static void vdi_close(BlockDriverState
*bs
)
985 BDRVVdiState
*s
= bs
->opaque
;
989 migrate_del_blocker(s
->migration_blocker
);
990 error_free(s
->migration_blocker
);
993 static int vdi_has_zero_init(BlockDriverState
*bs
)
995 BDRVVdiState
*s
= bs
->opaque
;
997 if (s
->header
.image_type
== VDI_TYPE_STATIC
) {
998 return bdrv_has_zero_init(bs
->file
->bs
);
1004 static QemuOptsList vdi_create_opts
= {
1005 .name
= "vdi-create-opts",
1006 .head
= QTAILQ_HEAD_INITIALIZER(vdi_create_opts
.head
),
1009 .name
= BLOCK_OPT_SIZE
,
1010 .type
= QEMU_OPT_SIZE
,
1011 .help
= "Virtual disk size"
1013 #if defined(CONFIG_VDI_BLOCK_SIZE)
1015 .name
= BLOCK_OPT_CLUSTER_SIZE
,
1016 .type
= QEMU_OPT_SIZE
,
1017 .help
= "VDI cluster (block) size",
1018 .def_value_str
= stringify(DEFAULT_CLUSTER_SIZE
)
1021 #if defined(CONFIG_VDI_STATIC_IMAGE)
1023 .name
= BLOCK_OPT_STATIC
,
1024 .type
= QEMU_OPT_BOOL
,
1025 .help
= "VDI static (pre-allocated) image",
1026 .def_value_str
= "off"
1029 /* TODO: An additional option to set UUID values might be useful. */
1030 { /* end of list */ }
1034 static BlockDriver bdrv_vdi
= {
1035 .format_name
= "vdi",
1036 .instance_size
= sizeof(BDRVVdiState
),
1037 .bdrv_probe
= vdi_probe
,
1038 .bdrv_open
= vdi_open
,
1039 .bdrv_close
= vdi_close
,
1040 .bdrv_reopen_prepare
= vdi_reopen_prepare
,
1041 .bdrv_child_perm
= bdrv_default_perms
,
1042 .bdrv_co_create
= vdi_co_create
,
1043 .bdrv_co_create_opts
= vdi_co_create_opts
,
1044 .bdrv_has_zero_init
= vdi_has_zero_init
,
1045 .bdrv_co_block_status
= vdi_co_block_status
,
1046 .bdrv_make_empty
= vdi_make_empty
,
1048 .bdrv_co_preadv
= vdi_co_preadv
,
1049 #if defined(CONFIG_VDI_WRITE)
1050 .bdrv_co_pwritev
= vdi_co_pwritev
,
1053 .bdrv_co_get_info
= vdi_co_get_info
,
1056 .create_opts
= &vdi_create_opts
,
1057 .bdrv_co_check
= vdi_co_check
,
1060 static void bdrv_vdi_init(void)
1063 bdrv_register(&bdrv_vdi
);
1066 block_init(bdrv_vdi_init
);