]> git.proxmox.com Git - mirror_qemu.git/blame - block/vdi.c
Merge tag 'pull-maintainer-may24-160524-2' of https://gitlab.com/stsquad/qemu into...
[mirror_qemu.git] / block / vdi.c
CommitLineData
9aebd98a
SW
1/*
2 * Block driver for the Virtual Disk Image (VDI) format
3 *
641543b7 4 * Copyright (c) 2009, 2012 Stefan Weil
9aebd98a
SW
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Reference:
20 * http://forums.virtualbox.org/viewtopic.php?t=8046
21 *
22 * This driver supports create / read / write operations on VDI images.
23 *
24 * Todo (see also TODO in code):
25 *
26 * Some features like snapshots are still missing.
27 *
28 * Deallocation of zero-filled blocks and shrinking images are missing, too
29 * (might be added to common block layer).
30 *
31 * Allocation of blocks could be optimized (less writes to block map and
32 * header).
33 *
dc6fb73d 34 * Read and write of adjacent blocks could be done in one operation
9aebd98a
SW
35 * (current code uses one operation per block (1 MiB).
36 *
37 * The code is not thread safe (missing locks for changes in header and
38 * block table, no problem with current QEMU).
39 *
40 * Hints:
41 *
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.
46 *
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.
50 */
51
80c71a24 52#include "qemu/osdep.h"
f043568f 53#include "qemu/units.h"
da34e65c 54#include "qapi/error.h"
49858b50
HR
55#include "qapi/qobject-input-visitor.h"
56#include "qapi/qapi-visit-block-core.h"
737e150e 57#include "block/block_int.h"
f853465a 58#include "block/qdict.h"
a08f0c3b 59#include "sysemu/block-backend.h"
1de7afc9 60#include "qemu/module.h"
922a01a0 61#include "qemu/option.h"
58369e22 62#include "qemu/bswap.h"
795c40b8 63#include "migration/blocker.h"
10817bf0 64#include "qemu/coroutine.h"
f348b6d1 65#include "qemu/cutils.h"
7c6f55b6 66#include "qemu/uuid.h"
5df022cf 67#include "qemu/memalign.h"
9aebd98a
SW
68
69/* Code configuration options. */
70
71/* Enable debug messages. */
72//~ #define CONFIG_VDI_DEBUG
73
74/* Support write operations on VDI images. */
75#define CONFIG_VDI_WRITE
76
77/* Support non-standard block (cluster) size. This is untested.
78 * Maybe it will be needed for very large images.
79 */
80//~ #define CONFIG_VDI_BLOCK_SIZE
81
82/* Support static (fixed, pre-allocated) images. */
83#define CONFIG_VDI_STATIC_IMAGE
84
85/* Command line option for static images. */
86#define BLOCK_OPT_STATIC "static"
87
9aebd98a 88#define SECTOR_SIZE 512
14632122
MA
89#define DEFAULT_CLUSTER_SIZE 1048576
90/* Note: can't use 1 * MiB, because it's passed to stringify() */
9aebd98a
SW
91
92#if defined(CONFIG_VDI_DEBUG)
b80666bf 93#define VDI_DEBUG 1
9aebd98a 94#else
b80666bf 95#define VDI_DEBUG 0
9aebd98a
SW
96#endif
97
b80666bf
EB
98#define logout(fmt, ...) \
99 do { \
100 if (VDI_DEBUG) { \
101 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \
102 } \
103 } while (0)
104
9aebd98a
SW
105/* Image signature. */
106#define VDI_SIGNATURE 0xbeda107f
107
108/* Image version. */
109#define VDI_VERSION_1_1 0x00010001
110
111/* Image type. */
112#define VDI_TYPE_DYNAMIC 1
113#define VDI_TYPE_STATIC 2
114
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.
120 */
121#define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
122
c794b4e0
ES
123/* A never-allocated block; semantically arbitrary content. */
124#define VDI_UNALLOCATED 0xffffffffU
125
126/* A discarded (no longer allocated) block; semantically zero-filled. */
127#define VDI_DISCARDED 0xfffffffeU
128
129#define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
9aebd98a 130
d20418ee
HR
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)))
63fa06dc
JC
143#define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
144 (uint64_t)DEFAULT_CLUSTER_SIZE)
145
49858b50
HR
146static QemuOptsList vdi_create_opts;
147
9aebd98a
SW
148typedef struct {
149 char text[0x40];
150 uint32_t signature;
151 uint32_t version;
152 uint32_t header_size;
153 uint32_t image_type;
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;
162 uint32_t unused1;
163 uint64_t disk_size;
164 uint32_t block_size;
165 uint32_t block_extra; /* unused here */
166 uint32_t blocks_in_image;
167 uint32_t blocks_allocated;
7c6f55b6
FZ
168 QemuUUID uuid_image;
169 QemuUUID uuid_last_snap;
170 QemuUUID uuid_link;
171 QemuUUID uuid_parent;
9aebd98a 172 uint64_t unused2[7];
8368febd 173} QEMU_PACKED VdiHeader;
9aebd98a 174
d4f18971
AG
175QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512);
176
9aebd98a 177typedef struct {
9aebd98a
SW
178 /* The block map entries are little endian (even in memory). */
179 uint32_t *bmap;
180 /* Size of block (bytes). */
181 uint32_t block_size;
9aebd98a
SW
182 /* First sector of block map. */
183 uint32_t bmap_sector;
4ff9786c 184 /* VDI header (converted to host endianness). */
9aebd98a 185 VdiHeader header;
fc9d106c 186
1e886639 187 CoRwlock bmap_lock;
f0ab6f10 188
fc9d106c 189 Error *migration_blocker;
9aebd98a
SW
190} BDRVVdiState;
191
9aebd98a
SW
192static void vdi_header_to_cpu(VdiHeader *header)
193{
09190184
PM
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);
1324f063
PM
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);
9aebd98a
SW
214}
215
216static void vdi_header_to_le(VdiHeader *header)
217{
09190184
PM
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);
1324f063
PM
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);
9aebd98a
SW
238}
239
9aebd98a
SW
240static void vdi_header_print(VdiHeader *header)
241{
721da039 242 char uuidstr[UUID_STR_LEN];
ac928b8e 243 QemuUUID uuid;
9aebd98a 244 logout("text %s", header->text);
9f0470bb 245 logout("signature 0x%08x\n", header->signature);
9aebd98a
SW
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);
ac928b8e
PM
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);
9aebd98a 274}
9aebd98a 275
2fd61638
PB
276static int coroutine_fn vdi_co_check(BlockDriverState *bs, BdrvCheckResult *res,
277 BdrvCheckMode fix)
9aebd98a
SW
278{
279 /* TODO: additional checks possible. */
280 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
9aebd98a
SW
281 uint32_t blocks_allocated = 0;
282 uint32_t block;
283 uint32_t *bmap;
284 logout("\n");
285
4534ff54
KW
286 if (fix) {
287 return -ENOTSUP;
288 }
289
5839e53b 290 bmap = g_try_new(uint32_t, s->header.blocks_in_image);
17cce735
KW
291 if (s->header.blocks_in_image && bmap == NULL) {
292 res->check_errors++;
293 return -ENOMEM;
294 }
295
9aebd98a
SW
296 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
297
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]);
c794b4e0 301 if (VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
302 if (bmap_entry < s->header.blocks_in_image) {
303 blocks_allocated++;
c794b4e0 304 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
9aebd98a
SW
305 bmap[bmap_entry] = bmap_entry;
306 } else {
307 fprintf(stderr, "ERROR: block index %" PRIu32
308 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
9ac228e0 309 res->corruptions++;
9aebd98a
SW
310 }
311 } else {
312 fprintf(stderr, "ERROR: block index %" PRIu32
313 " too large, is %" PRIu32 "\n", block, bmap_entry);
9ac228e0 314 res->corruptions++;
9aebd98a
SW
315 }
316 }
317 }
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);
9ac228e0 322 res->corruptions++;
9aebd98a
SW
323 }
324
7267c094 325 g_free(bmap);
9aebd98a 326
9ac228e0 327 return 0;
9aebd98a
SW
328}
329
3d47eb0a
EGE
330static int coroutine_fn
331vdi_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
9aebd98a 332{
3d47eb0a 333 /* TODO: vdi_co_get_info would be needed for machine snapshots.
9aebd98a
SW
334 vm_state_offset is still missing. */
335 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
336 logout("\n");
337 bdi->cluster_size = s->block_size;
338 bdi->vm_state_offset = 0;
339 return 0;
340}
341
342static int vdi_make_empty(BlockDriverState *bs)
343{
344 /* TODO: missing code. */
345 logout("\n");
346 /* The return value for missing code must be 0, see block.c. */
347 return 0;
348}
349
350static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
351{
352 const VdiHeader *header = (const VdiHeader *)buf;
dddc7750 353 int ret = 0;
9aebd98a
SW
354
355 logout("\n");
356
357 if (buf_size < sizeof(*header)) {
358 /* Header too small, no VDI. */
359 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
dddc7750 360 ret = 100;
9aebd98a
SW
361 }
362
dddc7750 363 if (ret == 0) {
9aebd98a
SW
364 logout("no vdi image\n");
365 } else {
366 logout("%s", header->text);
367 }
368
dddc7750 369 return ret;
9aebd98a
SW
370}
371
015a1036
HR
372static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
373 Error **errp)
9aebd98a
SW
374{
375 BDRVVdiState *s = bs->opaque;
376 VdiHeader header;
377 size_t bmap_size;
8937f822 378 int ret;
ac928b8e 379 QemuUUID uuid_link, uuid_parent;
9aebd98a 380
83930780
VSO
381 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
382 if (ret < 0) {
383 return ret;
4e4bf5c4
KW
384 }
385
a4b740db
KW
386 GRAPH_RDLOCK_GUARD_MAINLOOP();
387
9aebd98a
SW
388 logout("\n");
389
32cc71de 390 ret = bdrv_pread(bs->file, 0, sizeof(header), &header, 0);
8937f822 391 if (ret < 0) {
9aebd98a
SW
392 goto fail;
393 }
394
395 vdi_header_to_cpu(&header);
95a14d51
KW
396 if (VDI_DEBUG) {
397 vdi_header_print(&header);
398 }
9aebd98a 399
63fa06dc
JC
400 if (header.disk_size > VDI_DISK_SIZE_MAX) {
401 error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
402 ", max supported is 0x%" PRIx64 ")",
403 header.disk_size, VDI_DISK_SIZE_MAX);
404 ret = -ENOTSUP;
405 goto fail;
406 }
407
ac928b8e
PM
408 uuid_link = header.uuid_link;
409 uuid_parent = header.uuid_parent;
410
f21dc3a4
SW
411 if (header.disk_size % SECTOR_SIZE != 0) {
412 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
413 We accept them but round the disk size to the next multiple of
414 SECTOR_SIZE. */
415 logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
e9082e47 416 header.disk_size = ROUND_UP(header.disk_size, SECTOR_SIZE);
f21dc3a4
SW
417 }
418
0e87ba2c 419 if (header.signature != VDI_SIGNATURE) {
521b2b5d
HR
420 error_setg(errp, "Image not in VDI format (bad signature %08" PRIx32
421 ")", header.signature);
76abe407 422 ret = -EINVAL;
0e87ba2c
SW
423 goto fail;
424 } else if (header.version != VDI_VERSION_1_1) {
521b2b5d
HR
425 error_setg(errp, "unsupported VDI image (version %" PRIu32 ".%" PRIu32
426 ")", header.version >> 16, header.version & 0xffff);
8937f822 427 ret = -ENOTSUP;
9aebd98a
SW
428 goto fail;
429 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
430 /* We only support block maps which start on a sector boundary. */
5b7aa9b5 431 error_setg(errp, "unsupported VDI image (unaligned block map offset "
521b2b5d 432 "0x%" PRIx32 ")", header.offset_bmap);
8937f822 433 ret = -ENOTSUP;
9aebd98a
SW
434 goto fail;
435 } else if (header.offset_data % SECTOR_SIZE != 0) {
436 /* We only support data blocks which start on a sector boundary. */
521b2b5d
HR
437 error_setg(errp, "unsupported VDI image (unaligned data offset 0x%"
438 PRIx32 ")", header.offset_data);
8937f822 439 ret = -ENOTSUP;
9aebd98a
SW
440 goto fail;
441 } else if (header.sector_size != SECTOR_SIZE) {
521b2b5d
HR
442 error_setg(errp, "unsupported VDI image (sector size %" PRIu32
443 " is not %u)", header.sector_size, SECTOR_SIZE);
8937f822 444 ret = -ENOTSUP;
9aebd98a 445 goto fail;
63fa06dc 446 } else if (header.block_size != DEFAULT_CLUSTER_SIZE) {
521b2b5d 447 error_setg(errp, "unsupported VDI image (block size %" PRIu32
3dd5b8f4 448 " is not %" PRIu32 ")",
f043568f 449 header.block_size, DEFAULT_CLUSTER_SIZE);
8937f822 450 ret = -ENOTSUP;
9aebd98a 451 goto fail;
f21dc3a4
SW
452 } else if (header.disk_size >
453 (uint64_t)header.blocks_in_image * header.block_size) {
5b7aa9b5
PB
454 error_setg(errp, "unsupported VDI image (disk size %" PRIu64 ", "
455 "image bitmap has room for %" PRIu64 ")",
456 header.disk_size,
457 (uint64_t)header.blocks_in_image * header.block_size);
8937f822 458 ret = -ENOTSUP;
9aebd98a 459 goto fail;
ac928b8e 460 } else if (!qemu_uuid_is_null(&uuid_link)) {
5b7aa9b5 461 error_setg(errp, "unsupported VDI image (non-NULL link UUID)");
8937f822 462 ret = -ENOTSUP;
9aebd98a 463 goto fail;
ac928b8e 464 } else if (!qemu_uuid_is_null(&uuid_parent)) {
5b7aa9b5 465 error_setg(errp, "unsupported VDI image (non-NULL parent UUID)");
8937f822 466 ret = -ENOTSUP;
9aebd98a 467 goto fail;
63fa06dc
JC
468 } else if (header.blocks_in_image > VDI_BLOCKS_IN_IMAGE_MAX) {
469 error_setg(errp, "unsupported VDI image "
470 "(too many blocks %u, max is %u)",
471 header.blocks_in_image, VDI_BLOCKS_IN_IMAGE_MAX);
472 ret = -ENOTSUP;
473 goto fail;
9aebd98a
SW
474 }
475
476 bs->total_sectors = header.disk_size / SECTOR_SIZE;
477
478 s->block_size = header.block_size;
9aebd98a
SW
479 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
480 s->header = header;
481
482 bmap_size = header.blocks_in_image * sizeof(uint32_t);
e9082e47 483 bmap_size = DIV_ROUND_UP(bmap_size, SECTOR_SIZE);
9a4f4c31 484 s->bmap = qemu_try_blockalign(bs->file->bs, bmap_size * SECTOR_SIZE);
17cce735
KW
485 if (s->bmap == NULL) {
486 ret = -ENOMEM;
487 goto fail;
488 }
489
32cc71de
AF
490 ret = bdrv_pread(bs->file, header.offset_bmap, bmap_size * SECTOR_SIZE,
491 s->bmap, 0);
8937f822 492 if (ret < 0) {
9aebd98a
SW
493 goto fail_free_bmap;
494 }
495
fc9d106c 496 /* Disable migration when vdi images are used */
81e5f78a
AG
497 error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
498 "does not support live migration",
499 bdrv_get_device_or_node_name(bs));
4026f1c4 500
e0ee3a8f 501 ret = migrate_add_blocker_normal(&s->migration_blocker, errp);
386f6c07 502 if (ret < 0) {
fe44dc91
AA
503 goto fail_free_bmap;
504 }
fc9d106c 505
1e886639 506 qemu_co_rwlock_init(&s->bmap_lock);
f0ab6f10 507
9aebd98a
SW
508 return 0;
509
510 fail_free_bmap:
17cce735 511 qemu_vfree(s->bmap);
9aebd98a
SW
512
513 fail:
8937f822 514 return ret;
9aebd98a
SW
515}
516
ecfe2bba
JC
517static int vdi_reopen_prepare(BDRVReopenState *state,
518 BlockReopenQueue *queue, Error **errp)
519{
520 return 0;
521}
522
79a55866
KW
523static int coroutine_fn GRAPH_RDLOCK
524vdi_co_block_status(BlockDriverState *bs, bool want_zero, int64_t offset,
525 int64_t bytes, int64_t *pnum, int64_t *map,
526 BlockDriverState **file)
9aebd98a 527{
9aebd98a 528 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
67635f6a
EB
529 size_t bmap_index = offset / s->block_size;
530 size_t index_in_block = offset % s->block_size;
9aebd98a 531 uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
4bc74be9
PB
532 int result;
533
67635f6a
EB
534 logout("%p, %" PRId64 ", %" PRId64 ", %p\n", bs, offset, bytes, pnum);
535 *pnum = MIN(s->block_size - index_in_block, bytes);
4bc74be9
PB
536 result = VDI_IS_ALLOCATED(bmap_entry);
537 if (!result) {
2ea0332f 538 return BDRV_BLOCK_ZERO;
4bc74be9
PB
539 }
540
67635f6a
EB
541 *map = s->header.offset_data + (uint64_t)bmap_entry * s->block_size +
542 index_in_block;
8bfb1371 543 *file = bs->file->bs;
ad6434dc
HR
544 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID |
545 (s->header.image_type == VDI_TYPE_STATIC ? BDRV_BLOCK_RECURSE : 0);
9aebd98a
SW
546}
547
b9b10c35 548static int coroutine_fn GRAPH_RDLOCK
f7ef38dd
VSO
549vdi_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
550 QEMUIOVector *qiov, BdrvRequestFlags flags)
9aebd98a 551{
9aebd98a 552 BDRVVdiState *s = bs->opaque;
0865bb6f 553 QEMUIOVector local_qiov;
9aebd98a
SW
554 uint32_t bmap_entry;
555 uint32_t block_index;
0865bb6f
KW
556 uint32_t offset_in_block;
557 uint32_t n_bytes;
558 uint64_t bytes_done = 0;
eb9566d1 559 int ret = 0;
4de659e8
PB
560
561 logout("\n");
9aebd98a 562
0865bb6f 563 qemu_iovec_init(&local_qiov, qiov->niov);
0c7bfc32 564
0865bb6f
KW
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);
569
570 logout("will read %u bytes starting at offset %" PRIu64 "\n",
571 n_bytes, offset);
eb9566d1
PB
572
573 /* prepare next AIO request */
1e886639 574 qemu_co_rwlock_rdlock(&s->bmap_lock);
eb9566d1 575 bmap_entry = le32_to_cpu(s->bmap[block_index]);
1e886639 576 qemu_co_rwlock_unlock(&s->bmap_lock);
eb9566d1
PB
577 if (!VDI_IS_ALLOCATED(bmap_entry)) {
578 /* Block not allocated, return zeros, no need to wait. */
0865bb6f 579 qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
eb9566d1
PB
580 ret = 0;
581 } else {
0865bb6f
KW
582 uint64_t data_offset = s->header.offset_data +
583 (uint64_t)bmap_entry * s->block_size +
584 offset_in_block;
585
586 qemu_iovec_reset(&local_qiov);
587 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
588
a03ef88f 589 ret = bdrv_co_preadv(bs->file, data_offset, n_bytes,
0865bb6f 590 &local_qiov, 0);
eb9566d1 591 }
0865bb6f 592 logout("%u bytes read\n", n_bytes);
0c7bfc32 593
0865bb6f
KW
594 bytes -= n_bytes;
595 offset += n_bytes;
596 bytes_done += n_bytes;
9aebd98a 597 }
3d46a75a 598
0865bb6f
KW
599 qemu_iovec_destroy(&local_qiov);
600
3d46a75a 601 return ret;
9aebd98a
SW
602}
603
b9b10c35 604static int coroutine_fn GRAPH_RDLOCK
e75abeda
VSO
605vdi_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
606 QEMUIOVector *qiov, BdrvRequestFlags flags)
9aebd98a 607{
9aebd98a 608 BDRVVdiState *s = bs->opaque;
fde9d56f 609 QEMUIOVector local_qiov;
9aebd98a
SW
610 uint32_t bmap_entry;
611 uint32_t block_index;
fde9d56f
KW
612 uint32_t offset_in_block;
613 uint32_t n_bytes;
1e886639 614 uint64_t data_offset;
bfc45fc1
PB
615 uint32_t bmap_first = VDI_UNALLOCATED;
616 uint32_t bmap_last = VDI_UNALLOCATED;
bfc45fc1 617 uint8_t *block = NULL;
fde9d56f 618 uint64_t bytes_done = 0;
eb9566d1 619 int ret = 0;
4de659e8
PB
620
621 logout("\n");
9aebd98a 622
fde9d56f 623 qemu_iovec_init(&local_qiov, qiov->niov);
9aebd98a 624
fde9d56f
KW
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);
629
630 logout("will write %u bytes starting at offset %" PRIu64 "\n",
631 n_bytes, offset);
eb9566d1
PB
632
633 /* prepare next AIO request */
1e886639 634 qemu_co_rwlock_rdlock(&s->bmap_lock);
eb9566d1
PB
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. */
1e886639
PB
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;
644 }
645
eb9566d1
PB
646 bmap_entry = s->header.blocks_allocated;
647 s->bmap[block_index] = cpu_to_le32(bmap_entry);
648 s->header.blocks_allocated++;
fde9d56f
KW
649 data_offset = s->header.offset_data +
650 (uint64_t)bmap_entry * s->block_size;
eb9566d1
PB
651 if (block == NULL) {
652 block = g_malloc(s->block_size);
653 bmap_first = block_index;
654 }
655 bmap_last = block_index;
656 /* Copy data to be written to new block and zero unused parts. */
fde9d56f
KW
657 memset(block, 0, offset_in_block);
658 qemu_iovec_to_buf(qiov, bytes_done, block + offset_in_block,
659 n_bytes);
660 memset(block + offset_in_block + n_bytes, 0,
661 s->block_size - n_bytes - offset_in_block);
f0ab6f10 662
1e886639
PB
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.
666 */
3f653028
AF
667 ret = bdrv_co_pwrite(bs->file, data_offset, s->block_size, block,
668 0);
1e886639 669 qemu_co_rwlock_unlock(&s->bmap_lock);
eb9566d1 670 } else {
1e886639
PB
671nonallocating_write:
672 data_offset = s->header.offset_data +
673 (uint64_t)bmap_entry * s->block_size +
674 offset_in_block;
675 qemu_co_rwlock_unlock(&s->bmap_lock);
fde9d56f
KW
676
677 qemu_iovec_reset(&local_qiov);
678 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
679
a03ef88f 680 ret = bdrv_co_pwritev(bs->file, data_offset, n_bytes,
fde9d56f 681 &local_qiov, 0);
9aebd98a 682 }
0c7bfc32 683
fde9d56f
KW
684 bytes -= n_bytes;
685 offset += n_bytes;
686 bytes_done += n_bytes;
0c7bfc32 687
fde9d56f 688 logout("%u bytes written\n", n_bytes);
9aebd98a 689 }
9aebd98a 690
fde9d56f
KW
691 qemu_iovec_destroy(&local_qiov);
692
0c7bfc32 693 logout("finished data write\n");
4eea78e6 694 if (ret < 0) {
574b8304 695 g_free(block);
4eea78e6
PB
696 return ret;
697 }
698
699 if (block) {
700 /* One or more new blocks were allocated. */
07ee2ab4 701 VdiHeader *header;
4eea78e6 702 uint8_t *base;
d25b99c7 703 uint64_t bmap_offset;
fde9d56f 704 uint32_t n_sectors;
4eea78e6 705
07ee2ab4
DE
706 g_free(block);
707 header = g_malloc(sizeof(*header));
708
4eea78e6
PB
709 logout("now writing modified header\n");
710 assert(VDI_IS_ALLOCATED(bmap_first));
711 *header = s->header;
712 vdi_header_to_le(header);
3f653028 713 ret = bdrv_co_pwrite(bs->file, 0, sizeof(*header), header, 0);
07ee2ab4 714 g_free(header);
4eea78e6
PB
715
716 if (ret < 0) {
717 return ret;
0c7bfc32 718 }
4eea78e6
PB
719
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;
d25b99c7 726 bmap_offset = s->bmap_sector + bmap_first;
4eea78e6
PB
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);
d25b99c7 730 ret = bdrv_co_pwrite(bs->file, bmap_offset * SECTOR_SIZE,
3f653028 731 n_sectors * SECTOR_SIZE, base, 0);
0c7bfc32
PB
732 }
733
353a5d84 734 return ret;
9aebd98a
SW
735}
736
4db7ba3b
KW
737static int coroutine_fn GRAPH_UNLOCKED
738vdi_co_do_create(BlockdevCreateOptions *create_options, size_t block_size,
739 Error **errp)
9aebd98a 740{
f5ec96c9 741 ERRP_GUARD();
e3810574 742 BlockdevCreateOptionsVdi *vdi_opts;
dddc7750 743 int ret = 0;
9aebd98a
SW
744 uint64_t bytes = 0;
745 uint32_t blocks;
61fa6487 746 uint32_t image_type;
9aebd98a
SW
747 VdiHeader header;
748 size_t i;
749 size_t bmap_size;
70747862 750 int64_t offset = 0;
ec73f060 751 BlockDriverState *bs_file = NULL;
a08f0c3b 752 BlockBackend *blk = NULL;
70747862 753 uint32_t *bmap = NULL;
ac928b8e 754 QemuUUID uuid;
9aebd98a 755
e3810574
HR
756 assert(create_options->driver == BLOCKDEV_DRIVER_VDI);
757 vdi_opts = &create_options->u.vdi;
758
9aebd98a
SW
759 logout("\n");
760
da23248f 761 /* Validate options and set default values */
49858b50 762 bytes = vdi_opts->size;
61fa6487
KW
763
764 if (!vdi_opts->has_preallocation) {
765 vdi_opts->preallocation = PREALLOC_MODE_OFF;
766 }
767 switch (vdi_opts->preallocation) {
768 case PREALLOC_MODE_OFF:
769 image_type = VDI_TYPE_DYNAMIC;
770 break;
771 case PREALLOC_MODE_METADATA:
004b7f25 772 image_type = VDI_TYPE_STATIC;
61fa6487
KW
773 break;
774 default:
775 error_setg(errp, "Preallocation mode not supported for vdi");
776 return -EINVAL;
9aebd98a 777 }
61fa6487 778
49858b50
HR
779#ifndef CONFIG_VDI_STATIC_IMAGE
780 if (image_type == VDI_TYPE_STATIC) {
781 ret = -ENOTSUP;
782 error_setg(errp, "Statically allocated images cannot be created in "
783 "this build");
784 goto exit;
785 }
786#endif
787#ifndef CONFIG_VDI_BLOCK_SIZE
788 if (block_size != DEFAULT_CLUSTER_SIZE) {
789 ret = -ENOTSUP;
790 error_setg(errp,
791 "A non-default cluster size is not supported in this build");
792 goto exit;
793 }
004b7f25 794#endif
9aebd98a 795
63fa06dc 796 if (bytes > VDI_DISK_SIZE_MAX) {
dddc7750 797 ret = -ENOTSUP;
63fa06dc
JC
798 error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
799 ", max supported is 0x%" PRIx64 ")",
800 bytes, VDI_DISK_SIZE_MAX);
801 goto exit;
802 }
803
da23248f 804 /* Create BlockBackend to write to the image */
13dd6327 805 bs_file = bdrv_co_open_blockdev_ref(vdi_opts->file, errp);
ec73f060
HR
806 if (!bs_file) {
807 ret = -EIO;
63fa06dc 808 goto exit;
9aebd98a 809 }
a08f0c3b 810
13dd6327
KW
811 blk = blk_co_new_with_bs(bs_file, BLK_PERM_WRITE | BLK_PERM_RESIZE,
812 BLK_PERM_ALL, errp);
a3aeeab5
EB
813 if (!blk) {
814 ret = -EPERM;
70747862 815 goto exit;
4ab15590
CL
816 }
817
a08f0c3b
KW
818 blk_set_allow_write_beyond_eof(blk, true);
819
f21dc3a4
SW
820 /* We need enough blocks to store the given disk size,
821 so always round up. */
e9082e47 822 blocks = DIV_ROUND_UP(bytes, block_size);
f21dc3a4 823
9aebd98a 824 bmap_size = blocks * sizeof(uint32_t);
e9082e47 825 bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE);
9aebd98a
SW
826
827 memset(&header, 0, sizeof(header));
1786dc15 828 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
9aebd98a
SW
829 header.signature = VDI_SIGNATURE;
830 header.version = VDI_VERSION_1_1;
831 header.header_size = 0x180;
832 header.image_type = image_type;
833 header.offset_bmap = 0x200;
834 header.offset_data = 0x200 + bmap_size;
835 header.sector_size = SECTOR_SIZE;
836 header.disk_size = bytes;
837 header.block_size = block_size;
838 header.blocks_in_image = blocks;
6eea90eb
SW
839 if (image_type == VDI_TYPE_STATIC) {
840 header.blocks_allocated = blocks;
841 }
ac928b8e
PM
842 qemu_uuid_generate(&uuid);
843 header.uuid_image = uuid;
844 qemu_uuid_generate(&uuid);
845 header.uuid_last_snap = uuid;
9aebd98a 846 /* There is no need to set header.uuid_link or header.uuid_parent here. */
95a14d51
KW
847 if (VDI_DEBUG) {
848 vdi_header_print(&header);
849 }
9aebd98a 850 vdi_header_to_le(&header);
3f653028 851 ret = blk_co_pwrite(blk, offset, sizeof(header), &header, 0);
dddc7750 852 if (ret < 0) {
ec73f060 853 error_setg(errp, "Error writing header");
70747862 854 goto exit;
9aebd98a 855 }
70747862 856 offset += sizeof(header);
9aebd98a 857
b76b6e95 858 if (bmap_size > 0) {
17cce735
KW
859 bmap = g_try_malloc0(bmap_size);
860 if (bmap == NULL) {
861 ret = -ENOMEM;
862 error_setg(errp, "Could not allocate bmap");
863 goto exit;
864 }
514f21a5
SW
865 for (i = 0; i < blocks; i++) {
866 if (image_type == VDI_TYPE_STATIC) {
867 bmap[i] = i;
868 } else {
869 bmap[i] = VDI_UNALLOCATED;
870 }
9aebd98a 871 }
3f653028 872 ret = blk_co_pwrite(blk, offset, bmap_size, bmap, 0);
dddc7750 873 if (ret < 0) {
ec73f060 874 error_setg(errp, "Error writing bmap");
70747862 875 goto exit;
514f21a5 876 }
70747862 877 offset += bmap_size;
9aebd98a 878 }
514f21a5 879
9aebd98a 880 if (image_type == VDI_TYPE_STATIC) {
3f653028
AF
881 ret = blk_co_truncate(blk, offset + blocks * block_size, false,
882 PREALLOC_MODE_OFF, 0, errp);
dddc7750 883 if (ret < 0) {
ec73f060 884 error_prepend(errp, "Failed to statically allocate file");
70747862 885 goto exit;
9aebd98a
SW
886 }
887 }
888
53618dd8 889 ret = 0;
63fa06dc 890exit:
b2ab5f54
KW
891 blk_co_unref(blk);
892 bdrv_co_unref(bs_file);
70747862 893 g_free(bmap);
dddc7750 894 return ret;
9aebd98a
SW
895}
896
4db7ba3b
KW
897static int coroutine_fn GRAPH_UNLOCKED
898vdi_co_create(BlockdevCreateOptions *create_options, Error **errp)
e3810574
HR
899{
900 return vdi_co_do_create(create_options, DEFAULT_CLUSTER_SIZE, errp);
901}
902
4db7ba3b 903static int coroutine_fn GRAPH_UNLOCKED
4ec8df01
KW
904vdi_co_create_opts(BlockDriver *drv, const char *filename,
905 QemuOpts *opts, Error **errp)
49858b50
HR
906{
907 QDict *qdict = NULL;
e3810574 908 BlockdevCreateOptions *create_options = NULL;
ec73f060 909 BlockDriverState *bs_file = NULL;
49858b50 910 uint64_t block_size = DEFAULT_CLUSTER_SIZE;
61fa6487 911 bool is_static = false;
49858b50 912 Visitor *v;
49858b50
HR
913 int ret;
914
da23248f
KW
915 /* Parse options and convert legacy syntax.
916 *
917 * Since CONFIG_VDI_BLOCK_SIZE is disabled by default,
49858b50
HR
918 * cluster-size is not part of the QAPI schema; therefore we have
919 * to parse it before creating the QAPI object. */
920#if defined(CONFIG_VDI_BLOCK_SIZE)
921 block_size = qemu_opt_get_size_del(opts,
922 BLOCK_OPT_CLUSTER_SIZE,
923 DEFAULT_CLUSTER_SIZE);
924 if (block_size < BDRV_SECTOR_SIZE || block_size > UINT32_MAX ||
925 !is_power_of_2(block_size))
926 {
927 error_setg(errp, "Invalid cluster size");
928 ret = -EINVAL;
929 goto done;
930 }
931#endif
61fa6487
KW
932 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) {
933 is_static = true;
934 }
49858b50
HR
935
936 qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, true);
937
da23248f 938 /* Create and open the file (protocol layer) */
2475a0d0 939 ret = bdrv_co_create_file(filename, opts, errp);
ec73f060
HR
940 if (ret < 0) {
941 goto done;
942 }
943
13dd6327
KW
944 bs_file = bdrv_co_open(filename, NULL, NULL,
945 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
ec73f060
HR
946 if (!bs_file) {
947 ret = -EIO;
948 goto done;
949 }
950
e3810574 951 qdict_put_str(qdict, "driver", "vdi");
ec73f060 952 qdict_put_str(qdict, "file", bs_file->node_name);
61fa6487
KW
953 if (is_static) {
954 qdict_put_str(qdict, "preallocation", "metadata");
955 }
49858b50
HR
956
957 /* Get the QAPI object */
f853465a
MA
958 v = qobject_input_visitor_new_flat_confused(qdict, errp);
959 if (!v) {
960 ret = -EINVAL;
961 goto done;
962 }
b11a093c 963 visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
49858b50 964 visit_free(v);
b11a093c 965 if (!create_options) {
49858b50
HR
966 ret = -EINVAL;
967 goto done;
968 }
969
da23248f 970 /* Silently round up size */
e3810574
HR
971 assert(create_options->driver == BLOCKDEV_DRIVER_VDI);
972 create_options->u.vdi.size = ROUND_UP(create_options->u.vdi.size,
973 BDRV_SECTOR_SIZE);
49858b50 974
da23248f 975 /* Create the vdi image (format layer) */
ec73f060 976 ret = vdi_co_do_create(create_options, block_size, errp);
49858b50 977done:
cb3e7f08 978 qobject_unref(qdict);
e3810574 979 qapi_free_BlockdevCreateOptions(create_options);
b2ab5f54 980 bdrv_co_unref(bs_file);
49858b50
HR
981 return ret;
982}
983
9aebd98a
SW
984static void vdi_close(BlockDriverState *bs)
985{
fc9d106c 986 BDRVVdiState *s = bs->opaque;
6ac5f388 987
17cce735 988 qemu_vfree(s->bmap);
6ac5f388 989
c8a7fc51 990 migrate_del_blocker(&s->migration_blocker);
9aebd98a
SW
991}
992
06717986 993static int GRAPH_RDLOCK vdi_has_zero_init(BlockDriverState *bs)
0a28bf28
HR
994{
995 BDRVVdiState *s = bs->opaque;
996
997 if (s->header.image_type == VDI_TYPE_STATIC) {
998 return bdrv_has_zero_init(bs->file->bs);
999 } else {
1000 return 1;
1001 }
1002}
1003
004b7f25
CL
1004static QemuOptsList vdi_create_opts = {
1005 .name = "vdi-create-opts",
1006 .head = QTAILQ_HEAD_INITIALIZER(vdi_create_opts.head),
1007 .desc = {
1008 {
1009 .name = BLOCK_OPT_SIZE,
1010 .type = QEMU_OPT_SIZE,
1011 .help = "Virtual disk size"
1012 },
9aebd98a 1013#if defined(CONFIG_VDI_BLOCK_SIZE)
004b7f25
CL
1014 {
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)
1019 },
9aebd98a
SW
1020#endif
1021#if defined(CONFIG_VDI_STATIC_IMAGE)
004b7f25
CL
1022 {
1023 .name = BLOCK_OPT_STATIC,
1024 .type = QEMU_OPT_BOOL,
1025 .help = "VDI static (pre-allocated) image",
1026 .def_value_str = "off"
1027 },
9aebd98a 1028#endif
004b7f25
CL
1029 /* TODO: An additional option to set UUID values might be useful. */
1030 { /* end of list */ }
1031 }
9aebd98a
SW
1032};
1033
1034static 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,
ecfe2bba 1040 .bdrv_reopen_prepare = vdi_reopen_prepare,
69dca43d 1041 .bdrv_child_perm = bdrv_default_perms,
e3810574 1042 .bdrv_co_create = vdi_co_create,
da23248f 1043 .bdrv_co_create_opts = vdi_co_create_opts,
0a28bf28 1044 .bdrv_has_zero_init = vdi_has_zero_init,
67635f6a 1045 .bdrv_co_block_status = vdi_co_block_status,
9aebd98a
SW
1046 .bdrv_make_empty = vdi_make_empty,
1047
0865bb6f 1048 .bdrv_co_preadv = vdi_co_preadv,
9aebd98a 1049#if defined(CONFIG_VDI_WRITE)
fde9d56f 1050 .bdrv_co_pwritev = vdi_co_pwritev,
9aebd98a
SW
1051#endif
1052
3d47eb0a 1053 .bdrv_co_get_info = vdi_co_get_info,
9aebd98a 1054
d67066d8 1055 .is_format = true,
004b7f25 1056 .create_opts = &vdi_create_opts,
2fd61638 1057 .bdrv_co_check = vdi_co_check,
9aebd98a
SW
1058};
1059
1060static void bdrv_vdi_init(void)
1061{
1062 logout("\n");
1063 bdrv_register(&bdrv_vdi);
1064}
1065
1066block_init(bdrv_vdi_init);