]> Git Repo - qemu.git/blame - block/vdi.c
block/parallels: Simplify parallels_open() after previous commit
[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"
9aebd98a
SW
67
68/* Code configuration options. */
69
70/* Enable debug messages. */
71//~ #define CONFIG_VDI_DEBUG
72
73/* Support write operations on VDI images. */
74#define CONFIG_VDI_WRITE
75
76/* Support non-standard block (cluster) size. This is untested.
77 * Maybe it will be needed for very large images.
78 */
79//~ #define CONFIG_VDI_BLOCK_SIZE
80
81/* Support static (fixed, pre-allocated) images. */
82#define CONFIG_VDI_STATIC_IMAGE
83
84/* Command line option for static images. */
85#define BLOCK_OPT_STATIC "static"
86
9aebd98a 87#define SECTOR_SIZE 512
14632122
MA
88#define DEFAULT_CLUSTER_SIZE 1048576
89/* Note: can't use 1 * MiB, because it's passed to stringify() */
9aebd98a
SW
90
91#if defined(CONFIG_VDI_DEBUG)
b80666bf 92#define VDI_DEBUG 1
9aebd98a 93#else
b80666bf 94#define VDI_DEBUG 0
9aebd98a
SW
95#endif
96
b80666bf
EB
97#define logout(fmt, ...) \
98 do { \
99 if (VDI_DEBUG) { \
100 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \
101 } \
102 } while (0)
103
9aebd98a
SW
104/* Image signature. */
105#define VDI_SIGNATURE 0xbeda107f
106
107/* Image version. */
108#define VDI_VERSION_1_1 0x00010001
109
110/* Image type. */
111#define VDI_TYPE_DYNAMIC 1
112#define VDI_TYPE_STATIC 2
113
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.
119 */
120#define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
121
c794b4e0
ES
122/* A never-allocated block; semantically arbitrary content. */
123#define VDI_UNALLOCATED 0xffffffffU
124
125/* A discarded (no longer allocated) block; semantically zero-filled. */
126#define VDI_DISCARDED 0xfffffffeU
127
128#define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
9aebd98a 129
d20418ee
HR
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)))
63fa06dc
JC
142#define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
143 (uint64_t)DEFAULT_CLUSTER_SIZE)
144
49858b50
HR
145static QemuOptsList vdi_create_opts;
146
9aebd98a
SW
147typedef struct {
148 char text[0x40];
149 uint32_t signature;
150 uint32_t version;
151 uint32_t header_size;
152 uint32_t image_type;
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;
161 uint32_t unused1;
162 uint64_t disk_size;
163 uint32_t block_size;
164 uint32_t block_extra; /* unused here */
165 uint32_t blocks_in_image;
166 uint32_t blocks_allocated;
7c6f55b6
FZ
167 QemuUUID uuid_image;
168 QemuUUID uuid_last_snap;
169 QemuUUID uuid_link;
170 QemuUUID uuid_parent;
9aebd98a 171 uint64_t unused2[7];
8368febd 172} QEMU_PACKED VdiHeader;
9aebd98a 173
d4f18971
AG
174QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512);
175
9aebd98a 176typedef struct {
9aebd98a
SW
177 /* The block map entries are little endian (even in memory). */
178 uint32_t *bmap;
179 /* Size of block (bytes). */
180 uint32_t block_size;
9aebd98a
SW
181 /* First sector of block map. */
182 uint32_t bmap_sector;
4ff9786c 183 /* VDI header (converted to host endianness). */
9aebd98a 184 VdiHeader header;
fc9d106c 185
1e886639 186 CoRwlock bmap_lock;
f0ab6f10 187
fc9d106c 188 Error *migration_blocker;
9aebd98a
SW
189} BDRVVdiState;
190
9aebd98a
SW
191static void vdi_header_to_cpu(VdiHeader *header)
192{
09190184
PM
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);
1324f063
PM
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);
9aebd98a
SW
213}
214
215static void vdi_header_to_le(VdiHeader *header)
216{
09190184
PM
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);
1324f063
PM
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);
9aebd98a
SW
237}
238
9aebd98a
SW
239static void vdi_header_print(VdiHeader *header)
240{
ac928b8e
PM
241 char uuidstr[37];
242 QemuUUID uuid;
9aebd98a 243 logout("text %s", header->text);
9f0470bb 244 logout("signature 0x%08x\n", header->signature);
9aebd98a
SW
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);
ac928b8e
PM
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);
9aebd98a 273}
9aebd98a 274
2fd61638
PB
275static int coroutine_fn vdi_co_check(BlockDriverState *bs, BdrvCheckResult *res,
276 BdrvCheckMode fix)
9aebd98a
SW
277{
278 /* TODO: additional checks possible. */
279 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
9aebd98a
SW
280 uint32_t blocks_allocated = 0;
281 uint32_t block;
282 uint32_t *bmap;
283 logout("\n");
284
4534ff54
KW
285 if (fix) {
286 return -ENOTSUP;
287 }
288
5839e53b 289 bmap = g_try_new(uint32_t, s->header.blocks_in_image);
17cce735
KW
290 if (s->header.blocks_in_image && bmap == NULL) {
291 res->check_errors++;
292 return -ENOMEM;
293 }
294
9aebd98a
SW
295 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
296
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]);
c794b4e0 300 if (VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
301 if (bmap_entry < s->header.blocks_in_image) {
302 blocks_allocated++;
c794b4e0 303 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
9aebd98a
SW
304 bmap[bmap_entry] = bmap_entry;
305 } else {
306 fprintf(stderr, "ERROR: block index %" PRIu32
307 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
9ac228e0 308 res->corruptions++;
9aebd98a
SW
309 }
310 } else {
311 fprintf(stderr, "ERROR: block index %" PRIu32
312 " too large, is %" PRIu32 "\n", block, bmap_entry);
9ac228e0 313 res->corruptions++;
9aebd98a
SW
314 }
315 }
316 }
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);
9ac228e0 321 res->corruptions++;
9aebd98a
SW
322 }
323
7267c094 324 g_free(bmap);
9aebd98a 325
9ac228e0 326 return 0;
9aebd98a
SW
327}
328
329static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
330{
331 /* TODO: vdi_get_info would be needed for machine snapshots.
332 vm_state_offset is still missing. */
333 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
334 logout("\n");
335 bdi->cluster_size = s->block_size;
336 bdi->vm_state_offset = 0;
337 return 0;
338}
339
340static int vdi_make_empty(BlockDriverState *bs)
341{
342 /* TODO: missing code. */
343 logout("\n");
344 /* The return value for missing code must be 0, see block.c. */
345 return 0;
346}
347
348static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
349{
350 const VdiHeader *header = (const VdiHeader *)buf;
dddc7750 351 int ret = 0;
9aebd98a
SW
352
353 logout("\n");
354
355 if (buf_size < sizeof(*header)) {
356 /* Header too small, no VDI. */
357 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
dddc7750 358 ret = 100;
9aebd98a
SW
359 }
360
dddc7750 361 if (ret == 0) {
9aebd98a
SW
362 logout("no vdi image\n");
363 } else {
364 logout("%s", header->text);
365 }
366
dddc7750 367 return ret;
9aebd98a
SW
368}
369
015a1036
HR
370static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
371 Error **errp)
9aebd98a
SW
372{
373 BDRVVdiState *s = bs->opaque;
374 VdiHeader header;
375 size_t bmap_size;
8937f822 376 int ret;
fe44dc91 377 Error *local_err = NULL;
ac928b8e 378 QemuUUID uuid_link, uuid_parent;
9aebd98a 379
8b1869da
HR
380 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
381 BDRV_CHILD_IMAGE, false, errp);
4e4bf5c4
KW
382 if (!bs->file) {
383 return -EINVAL;
384 }
385
9aebd98a
SW
386 logout("\n");
387
d4f18971 388 ret = bdrv_pread(bs->file, 0, &header, sizeof(header));
8937f822 389 if (ret < 0) {
9aebd98a
SW
390 goto fail;
391 }
392
393 vdi_header_to_cpu(&header);
95a14d51
KW
394 if (VDI_DEBUG) {
395 vdi_header_print(&header);
396 }
9aebd98a 397
63fa06dc
JC
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);
402 ret = -ENOTSUP;
403 goto fail;
404 }
405
ac928b8e
PM
406 uuid_link = header.uuid_link;
407 uuid_parent = header.uuid_parent;
408
f21dc3a4
SW
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
412 SECTOR_SIZE. */
413 logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
e9082e47 414 header.disk_size = ROUND_UP(header.disk_size, SECTOR_SIZE);
f21dc3a4
SW
415 }
416
0e87ba2c 417 if (header.signature != VDI_SIGNATURE) {
521b2b5d
HR
418 error_setg(errp, "Image not in VDI format (bad signature %08" PRIx32
419 ")", header.signature);
76abe407 420 ret = -EINVAL;
0e87ba2c
SW
421 goto fail;
422 } else if (header.version != VDI_VERSION_1_1) {
521b2b5d
HR
423 error_setg(errp, "unsupported VDI image (version %" PRIu32 ".%" PRIu32
424 ")", header.version >> 16, header.version & 0xffff);
8937f822 425 ret = -ENOTSUP;
9aebd98a
SW
426 goto fail;
427 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
428 /* We only support block maps which start on a sector boundary. */
5b7aa9b5 429 error_setg(errp, "unsupported VDI image (unaligned block map offset "
521b2b5d 430 "0x%" PRIx32 ")", header.offset_bmap);
8937f822 431 ret = -ENOTSUP;
9aebd98a
SW
432 goto fail;
433 } else if (header.offset_data % SECTOR_SIZE != 0) {
434 /* We only support data blocks which start on a sector boundary. */
521b2b5d
HR
435 error_setg(errp, "unsupported VDI image (unaligned data offset 0x%"
436 PRIx32 ")", header.offset_data);
8937f822 437 ret = -ENOTSUP;
9aebd98a
SW
438 goto fail;
439 } else if (header.sector_size != SECTOR_SIZE) {
521b2b5d
HR
440 error_setg(errp, "unsupported VDI image (sector size %" PRIu32
441 " is not %u)", header.sector_size, SECTOR_SIZE);
8937f822 442 ret = -ENOTSUP;
9aebd98a 443 goto fail;
63fa06dc 444 } else if (header.block_size != DEFAULT_CLUSTER_SIZE) {
521b2b5d 445 error_setg(errp, "unsupported VDI image (block size %" PRIu32
3dd5b8f4 446 " is not %" PRIu32 ")",
f043568f 447 header.block_size, DEFAULT_CLUSTER_SIZE);
8937f822 448 ret = -ENOTSUP;
9aebd98a 449 goto fail;
f21dc3a4
SW
450 } else if (header.disk_size >
451 (uint64_t)header.blocks_in_image * header.block_size) {
5b7aa9b5
PB
452 error_setg(errp, "unsupported VDI image (disk size %" PRIu64 ", "
453 "image bitmap has room for %" PRIu64 ")",
454 header.disk_size,
455 (uint64_t)header.blocks_in_image * header.block_size);
8937f822 456 ret = -ENOTSUP;
9aebd98a 457 goto fail;
ac928b8e 458 } else if (!qemu_uuid_is_null(&uuid_link)) {
5b7aa9b5 459 error_setg(errp, "unsupported VDI image (non-NULL link UUID)");
8937f822 460 ret = -ENOTSUP;
9aebd98a 461 goto fail;
ac928b8e 462 } else if (!qemu_uuid_is_null(&uuid_parent)) {
5b7aa9b5 463 error_setg(errp, "unsupported VDI image (non-NULL parent UUID)");
8937f822 464 ret = -ENOTSUP;
9aebd98a 465 goto fail;
63fa06dc
JC
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);
470 ret = -ENOTSUP;
471 goto fail;
9aebd98a
SW
472 }
473
474 bs->total_sectors = header.disk_size / SECTOR_SIZE;
475
476 s->block_size = header.block_size;
9aebd98a
SW
477 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
478 s->header = header;
479
480 bmap_size = header.blocks_in_image * sizeof(uint32_t);
e9082e47 481 bmap_size = DIV_ROUND_UP(bmap_size, SECTOR_SIZE);
9a4f4c31 482 s->bmap = qemu_try_blockalign(bs->file->bs, bmap_size * SECTOR_SIZE);
17cce735
KW
483 if (s->bmap == NULL) {
484 ret = -ENOMEM;
485 goto fail;
486 }
487
d4f18971
AG
488 ret = bdrv_pread(bs->file, header.offset_bmap, s->bmap,
489 bmap_size * SECTOR_SIZE);
8937f822 490 if (ret < 0) {
9aebd98a
SW
491 goto fail_free_bmap;
492 }
493
fc9d106c 494 /* Disable migration when vdi images are used */
81e5f78a
AG
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));
fe44dc91
AA
498 ret = migrate_add_blocker(s->migration_blocker, &local_err);
499 if (local_err) {
500 error_propagate(errp, local_err);
501 error_free(s->migration_blocker);
502 goto fail_free_bmap;
503 }
fc9d106c 504
1e886639 505 qemu_co_rwlock_init(&s->bmap_lock);
f0ab6f10 506
9aebd98a
SW
507 return 0;
508
509 fail_free_bmap:
17cce735 510 qemu_vfree(s->bmap);
9aebd98a
SW
511
512 fail:
8937f822 513 return ret;
9aebd98a
SW
514}
515
ecfe2bba
JC
516static int vdi_reopen_prepare(BDRVReopenState *state,
517 BlockReopenQueue *queue, Error **errp)
518{
519 return 0;
520}
521
67635f6a
EB
522static int coroutine_fn vdi_co_block_status(BlockDriverState *bs,
523 bool want_zero,
524 int64_t offset, int64_t bytes,
525 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
0865bb6f
KW
548static int coroutine_fn
549vdi_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
550 QEMUIOVector *qiov, int 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
fde9d56f
KW
604static int coroutine_fn
605vdi_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
606 QEMUIOVector *qiov, int 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. */
fde9d56f 638 uint64_t data_offset;
1e886639
PB
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;
645 }
646
eb9566d1
PB
647 bmap_entry = s->header.blocks_allocated;
648 s->bmap[block_index] = cpu_to_le32(bmap_entry);
649 s->header.blocks_allocated++;
fde9d56f
KW
650 data_offset = s->header.offset_data +
651 (uint64_t)bmap_entry * s->block_size;
eb9566d1
PB
652 if (block == NULL) {
653 block = g_malloc(s->block_size);
654 bmap_first = block_index;
655 }
656 bmap_last = block_index;
657 /* Copy data to be written to new block and zero unused parts. */
fde9d56f
KW
658 memset(block, 0, offset_in_block);
659 qemu_iovec_to_buf(qiov, bytes_done, block + offset_in_block,
660 n_bytes);
661 memset(block + offset_in_block + n_bytes, 0,
662 s->block_size - n_bytes - offset_in_block);
f0ab6f10 663
1e886639
PB
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.
667 */
d9ca2ea2 668 ret = bdrv_pwrite(bs->file, data_offset, block, s->block_size);
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
PB
694 if (ret < 0) {
695 return ret;
696 }
697
698 if (block) {
699 /* One or more new blocks were allocated. */
700 VdiHeader *header = (VdiHeader *) block;
701 uint8_t *base;
702 uint64_t offset;
fde9d56f 703 uint32_t n_sectors;
4eea78e6
PB
704
705 logout("now writing modified header\n");
706 assert(VDI_IS_ALLOCATED(bmap_first));
707 *header = s->header;
708 vdi_header_to_le(header);
d4f18971 709 ret = bdrv_pwrite(bs->file, 0, block, sizeof(VdiHeader));
bfc45fc1
PB
710 g_free(block);
711 block = NULL;
4eea78e6
PB
712
713 if (ret < 0) {
714 return ret;
0c7bfc32 715 }
4eea78e6
PB
716
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);
d4f18971
AG
727 ret = bdrv_pwrite(bs->file, offset * SECTOR_SIZE, base,
728 n_sectors * SECTOR_SIZE);
0c7bfc32
PB
729 }
730
d4f18971 731 return ret < 0 ? ret : 0;
9aebd98a
SW
732}
733
e3810574 734static int coroutine_fn vdi_co_do_create(BlockdevCreateOptions *create_options,
49858b50 735 size_t block_size, Error **errp)
9aebd98a 736{
e3810574 737 BlockdevCreateOptionsVdi *vdi_opts;
dddc7750 738 int ret = 0;
9aebd98a
SW
739 uint64_t bytes = 0;
740 uint32_t blocks;
61fa6487 741 uint32_t image_type;
9aebd98a
SW
742 VdiHeader header;
743 size_t i;
744 size_t bmap_size;
70747862 745 int64_t offset = 0;
ec73f060 746 BlockDriverState *bs_file = NULL;
a08f0c3b 747 BlockBackend *blk = NULL;
70747862 748 uint32_t *bmap = NULL;
ac928b8e 749 QemuUUID uuid;
9aebd98a 750
e3810574
HR
751 assert(create_options->driver == BLOCKDEV_DRIVER_VDI);
752 vdi_opts = &create_options->u.vdi;
753
9aebd98a
SW
754 logout("\n");
755
da23248f 756 /* Validate options and set default values */
49858b50 757 bytes = vdi_opts->size;
61fa6487
KW
758
759 if (!vdi_opts->has_preallocation) {
760 vdi_opts->preallocation = PREALLOC_MODE_OFF;
761 }
762 switch (vdi_opts->preallocation) {
763 case PREALLOC_MODE_OFF:
764 image_type = VDI_TYPE_DYNAMIC;
765 break;
766 case PREALLOC_MODE_METADATA:
004b7f25 767 image_type = VDI_TYPE_STATIC;
61fa6487
KW
768 break;
769 default:
770 error_setg(errp, "Preallocation mode not supported for vdi");
771 return -EINVAL;
9aebd98a 772 }
61fa6487 773
49858b50
HR
774#ifndef CONFIG_VDI_STATIC_IMAGE
775 if (image_type == VDI_TYPE_STATIC) {
776 ret = -ENOTSUP;
777 error_setg(errp, "Statically allocated images cannot be created in "
778 "this build");
779 goto exit;
780 }
781#endif
782#ifndef CONFIG_VDI_BLOCK_SIZE
783 if (block_size != DEFAULT_CLUSTER_SIZE) {
784 ret = -ENOTSUP;
785 error_setg(errp,
786 "A non-default cluster size is not supported in this build");
787 goto exit;
788 }
004b7f25 789#endif
9aebd98a 790
63fa06dc 791 if (bytes > VDI_DISK_SIZE_MAX) {
dddc7750 792 ret = -ENOTSUP;
63fa06dc
JC
793 error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
794 ", max supported is 0x%" PRIx64 ")",
795 bytes, VDI_DISK_SIZE_MAX);
796 goto exit;
797 }
798
da23248f 799 /* Create BlockBackend to write to the image */
ec73f060
HR
800 bs_file = bdrv_open_blockdev_ref(vdi_opts->file, errp);
801 if (!bs_file) {
802 ret = -EIO;
63fa06dc 803 goto exit;
9aebd98a 804 }
a08f0c3b 805
a3aeeab5
EB
806 blk = blk_new_with_bs(bs_file, BLK_PERM_WRITE | BLK_PERM_RESIZE,
807 BLK_PERM_ALL, errp);
808 if (!blk) {
809 ret = -EPERM;
70747862 810 goto exit;
4ab15590
CL
811 }
812
a08f0c3b
KW
813 blk_set_allow_write_beyond_eof(blk, true);
814
f21dc3a4
SW
815 /* We need enough blocks to store the given disk size,
816 so always round up. */
e9082e47 817 blocks = DIV_ROUND_UP(bytes, block_size);
f21dc3a4 818
9aebd98a 819 bmap_size = blocks * sizeof(uint32_t);
e9082e47 820 bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE);
9aebd98a
SW
821
822 memset(&header, 0, sizeof(header));
1786dc15 823 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
9aebd98a
SW
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;
6eea90eb
SW
834 if (image_type == VDI_TYPE_STATIC) {
835 header.blocks_allocated = blocks;
836 }
ac928b8e
PM
837 qemu_uuid_generate(&uuid);
838 header.uuid_image = uuid;
839 qemu_uuid_generate(&uuid);
840 header.uuid_last_snap = uuid;
9aebd98a 841 /* There is no need to set header.uuid_link or header.uuid_parent here. */
95a14d51
KW
842 if (VDI_DEBUG) {
843 vdi_header_print(&header);
844 }
9aebd98a 845 vdi_header_to_le(&header);
8341f00d 846 ret = blk_pwrite(blk, offset, &header, sizeof(header), 0);
dddc7750 847 if (ret < 0) {
ec73f060 848 error_setg(errp, "Error writing header");
70747862 849 goto exit;
9aebd98a 850 }
70747862 851 offset += sizeof(header);
9aebd98a 852
b76b6e95 853 if (bmap_size > 0) {
17cce735
KW
854 bmap = g_try_malloc0(bmap_size);
855 if (bmap == NULL) {
856 ret = -ENOMEM;
857 error_setg(errp, "Could not allocate bmap");
858 goto exit;
859 }
514f21a5
SW
860 for (i = 0; i < blocks; i++) {
861 if (image_type == VDI_TYPE_STATIC) {
862 bmap[i] = i;
863 } else {
864 bmap[i] = VDI_UNALLOCATED;
865 }
9aebd98a 866 }
8341f00d 867 ret = blk_pwrite(blk, offset, bmap, bmap_size, 0);
dddc7750 868 if (ret < 0) {
ec73f060 869 error_setg(errp, "Error writing bmap");
70747862 870 goto exit;
514f21a5 871 }
70747862 872 offset += bmap_size;
9aebd98a 873 }
514f21a5 874
9aebd98a 875 if (image_type == VDI_TYPE_STATIC) {
c80d8b06 876 ret = blk_truncate(blk, offset + blocks * block_size, false,
8c6242b6 877 PREALLOC_MODE_OFF, 0, errp);
dddc7750 878 if (ret < 0) {
ec73f060 879 error_prepend(errp, "Failed to statically allocate file");
70747862 880 goto exit;
9aebd98a
SW
881 }
882 }
883
53618dd8 884 ret = 0;
63fa06dc 885exit:
a08f0c3b 886 blk_unref(blk);
ec73f060 887 bdrv_unref(bs_file);
70747862 888 g_free(bmap);
dddc7750 889 return ret;
9aebd98a
SW
890}
891
e3810574
HR
892static int coroutine_fn vdi_co_create(BlockdevCreateOptions *create_options,
893 Error **errp)
894{
895 return vdi_co_do_create(create_options, DEFAULT_CLUSTER_SIZE, errp);
896}
897
b92902df
ML
898static int coroutine_fn vdi_co_create_opts(BlockDriver *drv,
899 const char *filename,
900 QemuOpts *opts,
49858b50
HR
901 Error **errp)
902{
903 QDict *qdict = NULL;
e3810574 904 BlockdevCreateOptions *create_options = NULL;
ec73f060 905 BlockDriverState *bs_file = NULL;
49858b50 906 uint64_t block_size = DEFAULT_CLUSTER_SIZE;
61fa6487 907 bool is_static = false;
49858b50
HR
908 Visitor *v;
909 Error *local_err = NULL;
910 int ret;
911
da23248f
KW
912 /* Parse options and convert legacy syntax.
913 *
914 * Since CONFIG_VDI_BLOCK_SIZE is disabled by default,
49858b50
HR
915 * cluster-size is not part of the QAPI schema; therefore we have
916 * to parse it before creating the QAPI object. */
917#if defined(CONFIG_VDI_BLOCK_SIZE)
918 block_size = qemu_opt_get_size_del(opts,
919 BLOCK_OPT_CLUSTER_SIZE,
920 DEFAULT_CLUSTER_SIZE);
921 if (block_size < BDRV_SECTOR_SIZE || block_size > UINT32_MAX ||
922 !is_power_of_2(block_size))
923 {
924 error_setg(errp, "Invalid cluster size");
925 ret = -EINVAL;
926 goto done;
927 }
928#endif
61fa6487
KW
929 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) {
930 is_static = true;
931 }
49858b50
HR
932
933 qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, true);
934
da23248f 935 /* Create and open the file (protocol layer) */
ec73f060
HR
936 ret = bdrv_create_file(filename, opts, errp);
937 if (ret < 0) {
938 goto done;
939 }
940
941 bs_file = bdrv_open(filename, NULL, NULL,
942 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
943 if (!bs_file) {
944 ret = -EIO;
945 goto done;
946 }
947
e3810574 948 qdict_put_str(qdict, "driver", "vdi");
ec73f060 949 qdict_put_str(qdict, "file", bs_file->node_name);
61fa6487
KW
950 if (is_static) {
951 qdict_put_str(qdict, "preallocation", "metadata");
952 }
49858b50
HR
953
954 /* Get the QAPI object */
f853465a
MA
955 v = qobject_input_visitor_new_flat_confused(qdict, errp);
956 if (!v) {
957 ret = -EINVAL;
958 goto done;
959 }
e3810574 960 visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err);
49858b50
HR
961 visit_free(v);
962
963 if (local_err) {
964 error_propagate(errp, local_err);
965 ret = -EINVAL;
966 goto done;
967 }
968
da23248f 969 /* Silently round up size */
e3810574
HR
970 assert(create_options->driver == BLOCKDEV_DRIVER_VDI);
971 create_options->u.vdi.size = ROUND_UP(create_options->u.vdi.size,
972 BDRV_SECTOR_SIZE);
49858b50 973
da23248f 974 /* Create the vdi image (format layer) */
ec73f060 975 ret = vdi_co_do_create(create_options, block_size, errp);
49858b50 976done:
cb3e7f08 977 qobject_unref(qdict);
e3810574 978 qapi_free_BlockdevCreateOptions(create_options);
ec73f060 979 bdrv_unref(bs_file);
49858b50
HR
980 return ret;
981}
982
9aebd98a
SW
983static void vdi_close(BlockDriverState *bs)
984{
fc9d106c 985 BDRVVdiState *s = bs->opaque;
6ac5f388 986
17cce735 987 qemu_vfree(s->bmap);
6ac5f388 988
fc9d106c
KW
989 migrate_del_blocker(s->migration_blocker);
990 error_free(s->migration_blocker);
9aebd98a
SW
991}
992
0a28bf28
HR
993static int vdi_has_zero_init(BlockDriverState *bs)
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
1053 .bdrv_get_info = vdi_get_info,
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);
This page took 0.773534 seconds and 4 git commands to generate.