]> Git Repo - qemu.git/blame - block/vdi.c
target-i386: fix pcmpxstrx equal-ordered (strstr) mode
[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
52#include "qemu-common.h"
737e150e 53#include "block/block_int.h"
1de7afc9 54#include "qemu/module.h"
caf71f86 55#include "migration/migration.h"
10817bf0 56#include "qemu/coroutine.h"
9aebd98a 57
ee682d27 58#if defined(CONFIG_UUID)
9aebd98a
SW
59#include <uuid/uuid.h>
60#else
61/* TODO: move uuid emulation to some central place in QEMU. */
9c17d615 62#include "sysemu/sysemu.h" /* UUID_FMT */
9aebd98a 63typedef unsigned char uuid_t[16];
9aebd98a
SW
64#endif
65
66/* Code configuration options. */
67
68/* Enable debug messages. */
69//~ #define CONFIG_VDI_DEBUG
70
71/* Support write operations on VDI images. */
72#define CONFIG_VDI_WRITE
73
74/* Support non-standard block (cluster) size. This is untested.
75 * Maybe it will be needed for very large images.
76 */
77//~ #define CONFIG_VDI_BLOCK_SIZE
78
79/* Support static (fixed, pre-allocated) images. */
80#define CONFIG_VDI_STATIC_IMAGE
81
82/* Command line option for static images. */
83#define BLOCK_OPT_STATIC "static"
84
85#define KiB 1024
86#define MiB (KiB * KiB)
87
88#define SECTOR_SIZE 512
99cce9fa 89#define DEFAULT_CLUSTER_SIZE (1 * MiB)
9aebd98a
SW
90
91#if defined(CONFIG_VDI_DEBUG)
92#define logout(fmt, ...) \
93 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
94#else
95#define logout(fmt, ...) ((void)0)
96#endif
97
98/* Image signature. */
99#define VDI_SIGNATURE 0xbeda107f
100
101/* Image version. */
102#define VDI_VERSION_1_1 0x00010001
103
104/* Image type. */
105#define VDI_TYPE_DYNAMIC 1
106#define VDI_TYPE_STATIC 2
107
108/* Innotek / SUN images use these strings in header.text:
109 * "<<< innotek VirtualBox Disk Image >>>\n"
110 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
111 * "<<< Sun VirtualBox Disk Image >>>\n"
112 * The value does not matter, so QEMU created images use a different text.
113 */
114#define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
115
c794b4e0
ES
116/* A never-allocated block; semantically arbitrary content. */
117#define VDI_UNALLOCATED 0xffffffffU
118
119/* A discarded (no longer allocated) block; semantically zero-filled. */
120#define VDI_DISCARDED 0xfffffffeU
121
122#define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
9aebd98a 123
d20418ee
HR
124/* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since
125 * the bmap is read and written in a single operation, its size needs to be
126 * limited to INT_MAX; furthermore, when opening an image, the bmap size is
127 * rounded up to be aligned on BDRV_SECTOR_SIZE.
128 * Therefore this should satisfy the following:
129 * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1
130 * (INT_MAX + 1 is the first value not representable as an int)
131 * This guarantees that any value below or equal to the constant will, when
132 * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary,
133 * still be below or equal to INT_MAX. */
134#define VDI_BLOCKS_IN_IMAGE_MAX \
135 ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t)))
63fa06dc
JC
136#define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
137 (uint64_t)DEFAULT_CLUSTER_SIZE)
138
ee682d27 139#if !defined(CONFIG_UUID)
8ba2aae3 140static inline void uuid_generate(uuid_t out)
9aebd98a 141{
4f3669ea 142 memset(out, 0, sizeof(uuid_t));
9aebd98a
SW
143}
144
8ba2aae3 145static inline int uuid_is_null(const uuid_t uu)
9aebd98a
SW
146{
147 uuid_t null_uuid = { 0 };
4f3669ea 148 return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0;
9aebd98a
SW
149}
150
f18a768e 151# if defined(CONFIG_VDI_DEBUG)
8ba2aae3 152static inline void uuid_unparse(const uuid_t uu, char *out)
9aebd98a
SW
153{
154 snprintf(out, 37, UUID_FMT,
155 uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7],
156 uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
157}
f18a768e 158# endif
9aebd98a
SW
159#endif
160
9aebd98a
SW
161typedef struct {
162 char text[0x40];
163 uint32_t signature;
164 uint32_t version;
165 uint32_t header_size;
166 uint32_t image_type;
167 uint32_t image_flags;
168 char description[256];
169 uint32_t offset_bmap;
170 uint32_t offset_data;
171 uint32_t cylinders; /* disk geometry, unused here */
172 uint32_t heads; /* disk geometry, unused here */
173 uint32_t sectors; /* disk geometry, unused here */
174 uint32_t sector_size;
175 uint32_t unused1;
176 uint64_t disk_size;
177 uint32_t block_size;
178 uint32_t block_extra; /* unused here */
179 uint32_t blocks_in_image;
180 uint32_t blocks_allocated;
181 uuid_t uuid_image;
182 uuid_t uuid_last_snap;
183 uuid_t uuid_link;
184 uuid_t uuid_parent;
185 uint64_t unused2[7];
8368febd 186} QEMU_PACKED VdiHeader;
9aebd98a
SW
187
188typedef struct {
9aebd98a
SW
189 /* The block map entries are little endian (even in memory). */
190 uint32_t *bmap;
191 /* Size of block (bytes). */
192 uint32_t block_size;
193 /* Size of block (sectors). */
194 uint32_t block_sectors;
195 /* First sector of block map. */
196 uint32_t bmap_sector;
4ff9786c 197 /* VDI header (converted to host endianness). */
9aebd98a 198 VdiHeader header;
fc9d106c 199
f0ab6f10
HR
200 CoMutex write_lock;
201
fc9d106c 202 Error *migration_blocker;
9aebd98a
SW
203} BDRVVdiState;
204
205/* Change UUID from little endian (IPRT = VirtualBox format) to big endian
206 * format (network byte order, standard, see RFC 4122) and vice versa.
207 */
208static void uuid_convert(uuid_t uuid)
209{
210 bswap32s((uint32_t *)&uuid[0]);
211 bswap16s((uint16_t *)&uuid[4]);
212 bswap16s((uint16_t *)&uuid[6]);
213}
214
215static void vdi_header_to_cpu(VdiHeader *header)
216{
217 le32_to_cpus(&header->signature);
218 le32_to_cpus(&header->version);
219 le32_to_cpus(&header->header_size);
220 le32_to_cpus(&header->image_type);
221 le32_to_cpus(&header->image_flags);
222 le32_to_cpus(&header->offset_bmap);
223 le32_to_cpus(&header->offset_data);
224 le32_to_cpus(&header->cylinders);
225 le32_to_cpus(&header->heads);
226 le32_to_cpus(&header->sectors);
227 le32_to_cpus(&header->sector_size);
228 le64_to_cpus(&header->disk_size);
229 le32_to_cpus(&header->block_size);
230 le32_to_cpus(&header->block_extra);
231 le32_to_cpus(&header->blocks_in_image);
232 le32_to_cpus(&header->blocks_allocated);
233 uuid_convert(header->uuid_image);
234 uuid_convert(header->uuid_last_snap);
235 uuid_convert(header->uuid_link);
236 uuid_convert(header->uuid_parent);
237}
238
239static void vdi_header_to_le(VdiHeader *header)
240{
241 cpu_to_le32s(&header->signature);
242 cpu_to_le32s(&header->version);
243 cpu_to_le32s(&header->header_size);
244 cpu_to_le32s(&header->image_type);
245 cpu_to_le32s(&header->image_flags);
246 cpu_to_le32s(&header->offset_bmap);
247 cpu_to_le32s(&header->offset_data);
248 cpu_to_le32s(&header->cylinders);
249 cpu_to_le32s(&header->heads);
250 cpu_to_le32s(&header->sectors);
251 cpu_to_le32s(&header->sector_size);
252 cpu_to_le64s(&header->disk_size);
253 cpu_to_le32s(&header->block_size);
254 cpu_to_le32s(&header->block_extra);
255 cpu_to_le32s(&header->blocks_in_image);
256 cpu_to_le32s(&header->blocks_allocated);
9aebd98a
SW
257 uuid_convert(header->uuid_image);
258 uuid_convert(header->uuid_last_snap);
259 uuid_convert(header->uuid_link);
260 uuid_convert(header->uuid_parent);
261}
262
263#if defined(CONFIG_VDI_DEBUG)
264static void vdi_header_print(VdiHeader *header)
265{
266 char uuid[37];
267 logout("text %s", header->text);
9f0470bb 268 logout("signature 0x%08x\n", header->signature);
9aebd98a
SW
269 logout("header size 0x%04x\n", header->header_size);
270 logout("image type 0x%04x\n", header->image_type);
271 logout("image flags 0x%04x\n", header->image_flags);
272 logout("description %s\n", header->description);
273 logout("offset bmap 0x%04x\n", header->offset_bmap);
274 logout("offset data 0x%04x\n", header->offset_data);
275 logout("cylinders 0x%04x\n", header->cylinders);
276 logout("heads 0x%04x\n", header->heads);
277 logout("sectors 0x%04x\n", header->sectors);
278 logout("sector size 0x%04x\n", header->sector_size);
279 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
280 header->disk_size, header->disk_size / MiB);
281 logout("block size 0x%04x\n", header->block_size);
282 logout("block extra 0x%04x\n", header->block_extra);
283 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
284 logout("blocks all. 0x%04x\n", header->blocks_allocated);
285 uuid_unparse(header->uuid_image, uuid);
286 logout("uuid image %s\n", uuid);
287 uuid_unparse(header->uuid_last_snap, uuid);
288 logout("uuid snap %s\n", uuid);
289 uuid_unparse(header->uuid_link, uuid);
290 logout("uuid link %s\n", uuid);
291 uuid_unparse(header->uuid_parent, uuid);
292 logout("uuid parent %s\n", uuid);
293}
294#endif
295
4534ff54
KW
296static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res,
297 BdrvCheckMode fix)
9aebd98a
SW
298{
299 /* TODO: additional checks possible. */
300 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
9aebd98a
SW
301 uint32_t blocks_allocated = 0;
302 uint32_t block;
303 uint32_t *bmap;
304 logout("\n");
305
4534ff54
KW
306 if (fix) {
307 return -ENOTSUP;
308 }
309
5839e53b 310 bmap = g_try_new(uint32_t, s->header.blocks_in_image);
17cce735
KW
311 if (s->header.blocks_in_image && bmap == NULL) {
312 res->check_errors++;
313 return -ENOMEM;
314 }
315
9aebd98a
SW
316 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
317
318 /* Check block map and value of blocks_allocated. */
319 for (block = 0; block < s->header.blocks_in_image; block++) {
320 uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
c794b4e0 321 if (VDI_IS_ALLOCATED(bmap_entry)) {
9aebd98a
SW
322 if (bmap_entry < s->header.blocks_in_image) {
323 blocks_allocated++;
c794b4e0 324 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
9aebd98a
SW
325 bmap[bmap_entry] = bmap_entry;
326 } else {
327 fprintf(stderr, "ERROR: block index %" PRIu32
328 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
9ac228e0 329 res->corruptions++;
9aebd98a
SW
330 }
331 } else {
332 fprintf(stderr, "ERROR: block index %" PRIu32
333 " too large, is %" PRIu32 "\n", block, bmap_entry);
9ac228e0 334 res->corruptions++;
9aebd98a
SW
335 }
336 }
337 }
338 if (blocks_allocated != s->header.blocks_allocated) {
339 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
340 ", should be %" PRIu32 "\n",
341 blocks_allocated, s->header.blocks_allocated);
9ac228e0 342 res->corruptions++;
9aebd98a
SW
343 }
344
7267c094 345 g_free(bmap);
9aebd98a 346
9ac228e0 347 return 0;
9aebd98a
SW
348}
349
350static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
351{
352 /* TODO: vdi_get_info would be needed for machine snapshots.
353 vm_state_offset is still missing. */
354 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
355 logout("\n");
356 bdi->cluster_size = s->block_size;
357 bdi->vm_state_offset = 0;
95de6d70 358 bdi->unallocated_blocks_are_zero = true;
9aebd98a
SW
359 return 0;
360}
361
362static int vdi_make_empty(BlockDriverState *bs)
363{
364 /* TODO: missing code. */
365 logout("\n");
366 /* The return value for missing code must be 0, see block.c. */
367 return 0;
368}
369
370static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
371{
372 const VdiHeader *header = (const VdiHeader *)buf;
dddc7750 373 int ret = 0;
9aebd98a
SW
374
375 logout("\n");
376
377 if (buf_size < sizeof(*header)) {
378 /* Header too small, no VDI. */
379 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
dddc7750 380 ret = 100;
9aebd98a
SW
381 }
382
dddc7750 383 if (ret == 0) {
9aebd98a
SW
384 logout("no vdi image\n");
385 } else {
386 logout("%s", header->text);
387 }
388
dddc7750 389 return ret;
9aebd98a
SW
390}
391
015a1036
HR
392static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
393 Error **errp)
9aebd98a
SW
394{
395 BDRVVdiState *s = bs->opaque;
396 VdiHeader header;
397 size_t bmap_size;
8937f822 398 int ret;
9aebd98a
SW
399
400 logout("\n");
401
9a4f4c31 402 ret = bdrv_read(bs->file->bs, 0, (uint8_t *)&header, 1);
8937f822 403 if (ret < 0) {
9aebd98a
SW
404 goto fail;
405 }
406
407 vdi_header_to_cpu(&header);
408#if defined(CONFIG_VDI_DEBUG)
409 vdi_header_print(&header);
410#endif
411
63fa06dc
JC
412 if (header.disk_size > VDI_DISK_SIZE_MAX) {
413 error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
414 ", max supported is 0x%" PRIx64 ")",
415 header.disk_size, VDI_DISK_SIZE_MAX);
416 ret = -ENOTSUP;
417 goto fail;
418 }
419
f21dc3a4
SW
420 if (header.disk_size % SECTOR_SIZE != 0) {
421 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
422 We accept them but round the disk size to the next multiple of
423 SECTOR_SIZE. */
424 logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
e9082e47 425 header.disk_size = ROUND_UP(header.disk_size, SECTOR_SIZE);
f21dc3a4
SW
426 }
427
0e87ba2c 428 if (header.signature != VDI_SIGNATURE) {
521b2b5d
HR
429 error_setg(errp, "Image not in VDI format (bad signature %08" PRIx32
430 ")", header.signature);
76abe407 431 ret = -EINVAL;
0e87ba2c
SW
432 goto fail;
433 } else if (header.version != VDI_VERSION_1_1) {
521b2b5d
HR
434 error_setg(errp, "unsupported VDI image (version %" PRIu32 ".%" PRIu32
435 ")", header.version >> 16, header.version & 0xffff);
8937f822 436 ret = -ENOTSUP;
9aebd98a
SW
437 goto fail;
438 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
439 /* We only support block maps which start on a sector boundary. */
5b7aa9b5 440 error_setg(errp, "unsupported VDI image (unaligned block map offset "
521b2b5d 441 "0x%" PRIx32 ")", header.offset_bmap);
8937f822 442 ret = -ENOTSUP;
9aebd98a
SW
443 goto fail;
444 } else if (header.offset_data % SECTOR_SIZE != 0) {
445 /* We only support data blocks which start on a sector boundary. */
521b2b5d
HR
446 error_setg(errp, "unsupported VDI image (unaligned data offset 0x%"
447 PRIx32 ")", header.offset_data);
8937f822 448 ret = -ENOTSUP;
9aebd98a
SW
449 goto fail;
450 } else if (header.sector_size != SECTOR_SIZE) {
521b2b5d
HR
451 error_setg(errp, "unsupported VDI image (sector size %" PRIu32
452 " is not %u)", header.sector_size, SECTOR_SIZE);
8937f822 453 ret = -ENOTSUP;
9aebd98a 454 goto fail;
63fa06dc 455 } else if (header.block_size != DEFAULT_CLUSTER_SIZE) {
521b2b5d
HR
456 error_setg(errp, "unsupported VDI image (block size %" PRIu32
457 " is not %u)", header.block_size, DEFAULT_CLUSTER_SIZE);
8937f822 458 ret = -ENOTSUP;
9aebd98a 459 goto fail;
f21dc3a4
SW
460 } else if (header.disk_size >
461 (uint64_t)header.blocks_in_image * header.block_size) {
5b7aa9b5
PB
462 error_setg(errp, "unsupported VDI image (disk size %" PRIu64 ", "
463 "image bitmap has room for %" PRIu64 ")",
464 header.disk_size,
465 (uint64_t)header.blocks_in_image * header.block_size);
8937f822 466 ret = -ENOTSUP;
9aebd98a
SW
467 goto fail;
468 } else if (!uuid_is_null(header.uuid_link)) {
5b7aa9b5 469 error_setg(errp, "unsupported VDI image (non-NULL link UUID)");
8937f822 470 ret = -ENOTSUP;
9aebd98a
SW
471 goto fail;
472 } else if (!uuid_is_null(header.uuid_parent)) {
5b7aa9b5 473 error_setg(errp, "unsupported VDI image (non-NULL parent UUID)");
8937f822 474 ret = -ENOTSUP;
9aebd98a 475 goto fail;
63fa06dc
JC
476 } else if (header.blocks_in_image > VDI_BLOCKS_IN_IMAGE_MAX) {
477 error_setg(errp, "unsupported VDI image "
478 "(too many blocks %u, max is %u)",
479 header.blocks_in_image, VDI_BLOCKS_IN_IMAGE_MAX);
480 ret = -ENOTSUP;
481 goto fail;
9aebd98a
SW
482 }
483
484 bs->total_sectors = header.disk_size / SECTOR_SIZE;
485
486 s->block_size = header.block_size;
487 s->block_sectors = header.block_size / SECTOR_SIZE;
488 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
489 s->header = header;
490
491 bmap_size = header.blocks_in_image * sizeof(uint32_t);
e9082e47 492 bmap_size = DIV_ROUND_UP(bmap_size, SECTOR_SIZE);
9a4f4c31 493 s->bmap = qemu_try_blockalign(bs->file->bs, bmap_size * SECTOR_SIZE);
17cce735
KW
494 if (s->bmap == NULL) {
495 ret = -ENOMEM;
496 goto fail;
497 }
498
9a4f4c31
KW
499 ret = bdrv_read(bs->file->bs, s->bmap_sector, (uint8_t *)s->bmap,
500 bmap_size);
8937f822 501 if (ret < 0) {
9aebd98a
SW
502 goto fail_free_bmap;
503 }
504
fc9d106c 505 /* Disable migration when vdi images are used */
81e5f78a
AG
506 error_setg(&s->migration_blocker, "The vdi format used by node '%s' "
507 "does not support live migration",
508 bdrv_get_device_or_node_name(bs));
fc9d106c
KW
509 migrate_add_blocker(s->migration_blocker);
510
f0ab6f10
HR
511 qemu_co_mutex_init(&s->write_lock);
512
9aebd98a
SW
513 return 0;
514
515 fail_free_bmap:
17cce735 516 qemu_vfree(s->bmap);
9aebd98a
SW
517
518 fail:
8937f822 519 return ret;
9aebd98a
SW
520}
521
ecfe2bba
JC
522static int vdi_reopen_prepare(BDRVReopenState *state,
523 BlockReopenQueue *queue, Error **errp)
524{
525 return 0;
526}
527
b6b8a333 528static int64_t coroutine_fn vdi_co_get_block_status(BlockDriverState *bs,
e850b35a 529 int64_t sector_num, int nb_sectors, int *pnum)
9aebd98a
SW
530{
531 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
532 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
533 size_t bmap_index = sector_num / s->block_sectors;
534 size_t sector_in_block = sector_num % s->block_sectors;
535 int n_sectors = s->block_sectors - sector_in_block;
536 uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
4bc74be9
PB
537 uint64_t offset;
538 int result;
539
9aebd98a
SW
540 logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
541 if (n_sectors > nb_sectors) {
542 n_sectors = nb_sectors;
543 }
544 *pnum = n_sectors;
4bc74be9
PB
545 result = VDI_IS_ALLOCATED(bmap_entry);
546 if (!result) {
547 return 0;
548 }
549
550 offset = s->header.offset_data +
551 (uint64_t)bmap_entry * s->block_size +
552 sector_in_block * SECTOR_SIZE;
553 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID | offset;
9aebd98a
SW
554}
555
a7a43aa1
PB
556static int vdi_co_read(BlockDriverState *bs,
557 int64_t sector_num, uint8_t *buf, int nb_sectors)
9aebd98a 558{
9aebd98a
SW
559 BDRVVdiState *s = bs->opaque;
560 uint32_t bmap_entry;
561 uint32_t block_index;
562 uint32_t sector_in_block;
563 uint32_t n_sectors;
eb9566d1 564 int ret = 0;
4de659e8
PB
565
566 logout("\n");
9aebd98a 567
eb9566d1
PB
568 while (ret >= 0 && nb_sectors > 0) {
569 block_index = sector_num / s->block_sectors;
570 sector_in_block = sector_num % s->block_sectors;
571 n_sectors = s->block_sectors - sector_in_block;
572 if (n_sectors > nb_sectors) {
573 n_sectors = nb_sectors;
574 }
0c7bfc32 575
eb9566d1
PB
576 logout("will read %u sectors starting at sector %" PRIu64 "\n",
577 n_sectors, sector_num);
578
579 /* prepare next AIO request */
580 bmap_entry = le32_to_cpu(s->bmap[block_index]);
581 if (!VDI_IS_ALLOCATED(bmap_entry)) {
582 /* Block not allocated, return zeros, no need to wait. */
583 memset(buf, 0, n_sectors * SECTOR_SIZE);
584 ret = 0;
585 } else {
586 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
587 (uint64_t)bmap_entry * s->block_sectors +
588 sector_in_block;
9a4f4c31 589 ret = bdrv_read(bs->file->bs, offset, buf, n_sectors);
eb9566d1
PB
590 }
591 logout("%u sectors read\n", n_sectors);
0c7bfc32 592
eb9566d1
PB
593 nb_sectors -= n_sectors;
594 sector_num += n_sectors;
595 buf += n_sectors * SECTOR_SIZE;
9aebd98a 596 }
3d46a75a 597
3d46a75a 598 return ret;
9aebd98a
SW
599}
600
a7a43aa1
PB
601static int vdi_co_write(BlockDriverState *bs,
602 int64_t sector_num, const uint8_t *buf, int nb_sectors)
9aebd98a 603{
9aebd98a
SW
604 BDRVVdiState *s = bs->opaque;
605 uint32_t bmap_entry;
606 uint32_t block_index;
607 uint32_t sector_in_block;
608 uint32_t n_sectors;
bfc45fc1
PB
609 uint32_t bmap_first = VDI_UNALLOCATED;
610 uint32_t bmap_last = VDI_UNALLOCATED;
bfc45fc1 611 uint8_t *block = NULL;
eb9566d1 612 int ret = 0;
4de659e8
PB
613
614 logout("\n");
9aebd98a 615
eb9566d1
PB
616 while (ret >= 0 && nb_sectors > 0) {
617 block_index = sector_num / s->block_sectors;
618 sector_in_block = sector_num % s->block_sectors;
619 n_sectors = s->block_sectors - sector_in_block;
620 if (n_sectors > nb_sectors) {
621 n_sectors = nb_sectors;
622 }
9aebd98a 623
eb9566d1
PB
624 logout("will write %u sectors starting at sector %" PRIu64 "\n",
625 n_sectors, sector_num);
626
627 /* prepare next AIO request */
628 bmap_entry = le32_to_cpu(s->bmap[block_index]);
629 if (!VDI_IS_ALLOCATED(bmap_entry)) {
630 /* Allocate new block and write to it. */
631 uint64_t offset;
632 bmap_entry = s->header.blocks_allocated;
633 s->bmap[block_index] = cpu_to_le32(bmap_entry);
634 s->header.blocks_allocated++;
635 offset = s->header.offset_data / SECTOR_SIZE +
636 (uint64_t)bmap_entry * s->block_sectors;
637 if (block == NULL) {
638 block = g_malloc(s->block_size);
639 bmap_first = block_index;
640 }
641 bmap_last = block_index;
642 /* Copy data to be written to new block and zero unused parts. */
643 memset(block, 0, sector_in_block * SECTOR_SIZE);
644 memcpy(block + sector_in_block * SECTOR_SIZE,
645 buf, n_sectors * SECTOR_SIZE);
646 memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0,
647 (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
f0ab6f10
HR
648
649 /* Note that this coroutine does not yield anywhere from reading the
650 * bmap entry until here, so in regards to all the coroutines trying
651 * to write to this cluster, the one doing the allocation will
652 * always be the first to try to acquire the lock.
653 * Therefore, it is also the first that will actually be able to
654 * acquire the lock and thus the padded cluster is written before
655 * the other coroutines can write to the affected area. */
656 qemu_co_mutex_lock(&s->write_lock);
9a4f4c31 657 ret = bdrv_write(bs->file->bs, offset, block, s->block_sectors);
f0ab6f10 658 qemu_co_mutex_unlock(&s->write_lock);
eb9566d1
PB
659 } else {
660 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
661 (uint64_t)bmap_entry * s->block_sectors +
662 sector_in_block;
f0ab6f10
HR
663 qemu_co_mutex_lock(&s->write_lock);
664 /* This lock is only used to make sure the following write operation
665 * is executed after the write issued by the coroutine allocating
666 * this cluster, therefore we do not need to keep it locked.
667 * As stated above, the allocating coroutine will always try to lock
668 * the mutex before all the other concurrent accesses to that
669 * cluster, therefore at this point we can be absolutely certain
670 * that that write operation has returned (there may be other writes
671 * in flight, but they do not concern this very operation). */
672 qemu_co_mutex_unlock(&s->write_lock);
9a4f4c31 673 ret = bdrv_write(bs->file->bs, offset, buf, n_sectors);
9aebd98a 674 }
0c7bfc32 675
eb9566d1
PB
676 nb_sectors -= n_sectors;
677 sector_num += n_sectors;
678 buf += n_sectors * SECTOR_SIZE;
0c7bfc32 679
eb9566d1 680 logout("%u sectors written\n", n_sectors);
9aebd98a 681 }
9aebd98a 682
0c7bfc32 683 logout("finished data write\n");
4eea78e6
PB
684 if (ret < 0) {
685 return ret;
686 }
687
688 if (block) {
689 /* One or more new blocks were allocated. */
690 VdiHeader *header = (VdiHeader *) block;
691 uint8_t *base;
692 uint64_t offset;
693
694 logout("now writing modified header\n");
695 assert(VDI_IS_ALLOCATED(bmap_first));
696 *header = s->header;
697 vdi_header_to_le(header);
9a4f4c31 698 ret = bdrv_write(bs->file->bs, 0, block, 1);
bfc45fc1
PB
699 g_free(block);
700 block = NULL;
4eea78e6
PB
701
702 if (ret < 0) {
703 return ret;
0c7bfc32 704 }
4eea78e6
PB
705
706 logout("now writing modified block map entry %u...%u\n",
707 bmap_first, bmap_last);
708 /* Write modified sectors from block map. */
709 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
710 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
711 n_sectors = bmap_last - bmap_first + 1;
712 offset = s->bmap_sector + bmap_first;
713 base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE;
714 logout("will write %u block map sectors starting from entry %u\n",
715 n_sectors, bmap_first);
9a4f4c31 716 ret = bdrv_write(bs->file->bs, offset, base, n_sectors);
0c7bfc32
PB
717 }
718
3d46a75a 719 return ret;
9aebd98a
SW
720}
721
004b7f25 722static int vdi_create(const char *filename, QemuOpts *opts, Error **errp)
9aebd98a 723{
dddc7750 724 int ret = 0;
9aebd98a
SW
725 uint64_t bytes = 0;
726 uint32_t blocks;
99cce9fa 727 size_t block_size = DEFAULT_CLUSTER_SIZE;
9aebd98a
SW
728 uint32_t image_type = VDI_TYPE_DYNAMIC;
729 VdiHeader header;
730 size_t i;
731 size_t bmap_size;
70747862
JC
732 int64_t offset = 0;
733 Error *local_err = NULL;
734 BlockDriverState *bs = NULL;
735 uint32_t *bmap = NULL;
9aebd98a
SW
736
737 logout("\n");
738
739 /* Read out options. */
c2eb918e
HT
740 bytes = ROUND_UP(qemu_opt_get_size_del(opts, BLOCK_OPT_SIZE, 0),
741 BDRV_SECTOR_SIZE);
9aebd98a 742#if defined(CONFIG_VDI_BLOCK_SIZE)
004b7f25
CL
743 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
744 block_size = qemu_opt_get_size_del(opts,
745 BLOCK_OPT_CLUSTER_SIZE,
746 DEFAULT_CLUSTER_SIZE);
9aebd98a
SW
747#endif
748#if defined(CONFIG_VDI_STATIC_IMAGE)
004b7f25
CL
749 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) {
750 image_type = VDI_TYPE_STATIC;
9aebd98a 751 }
004b7f25 752#endif
9aebd98a 753
63fa06dc 754 if (bytes > VDI_DISK_SIZE_MAX) {
dddc7750 755 ret = -ENOTSUP;
63fa06dc
JC
756 error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
757 ", max supported is 0x%" PRIx64 ")",
758 bytes, VDI_DISK_SIZE_MAX);
759 goto exit;
760 }
761
dddc7750
JC
762 ret = bdrv_create_file(filename, opts, &local_err);
763 if (ret < 0) {
70747862 764 error_propagate(errp, local_err);
63fa06dc 765 goto exit;
9aebd98a 766 }
dddc7750 767 ret = bdrv_open(&bs, filename, NULL, NULL, BDRV_O_RDWR | BDRV_O_PROTOCOL,
6ebf9aa2 768 &local_err);
dddc7750 769 if (ret < 0) {
70747862
JC
770 error_propagate(errp, local_err);
771 goto exit;
4ab15590
CL
772 }
773
f21dc3a4
SW
774 /* We need enough blocks to store the given disk size,
775 so always round up. */
e9082e47 776 blocks = DIV_ROUND_UP(bytes, block_size);
f21dc3a4 777
9aebd98a 778 bmap_size = blocks * sizeof(uint32_t);
e9082e47 779 bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE);
9aebd98a
SW
780
781 memset(&header, 0, sizeof(header));
1786dc15 782 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
9aebd98a
SW
783 header.signature = VDI_SIGNATURE;
784 header.version = VDI_VERSION_1_1;
785 header.header_size = 0x180;
786 header.image_type = image_type;
787 header.offset_bmap = 0x200;
788 header.offset_data = 0x200 + bmap_size;
789 header.sector_size = SECTOR_SIZE;
790 header.disk_size = bytes;
791 header.block_size = block_size;
792 header.blocks_in_image = blocks;
6eea90eb
SW
793 if (image_type == VDI_TYPE_STATIC) {
794 header.blocks_allocated = blocks;
795 }
9aebd98a
SW
796 uuid_generate(header.uuid_image);
797 uuid_generate(header.uuid_last_snap);
798 /* There is no need to set header.uuid_link or header.uuid_parent here. */
799#if defined(CONFIG_VDI_DEBUG)
800 vdi_header_print(&header);
801#endif
802 vdi_header_to_le(&header);
dddc7750
JC
803 ret = bdrv_pwrite_sync(bs, offset, &header, sizeof(header));
804 if (ret < 0) {
70747862
JC
805 error_setg(errp, "Error writing header to %s", filename);
806 goto exit;
9aebd98a 807 }
70747862 808 offset += sizeof(header);
9aebd98a 809
b76b6e95 810 if (bmap_size > 0) {
17cce735
KW
811 bmap = g_try_malloc0(bmap_size);
812 if (bmap == NULL) {
813 ret = -ENOMEM;
814 error_setg(errp, "Could not allocate bmap");
815 goto exit;
816 }
514f21a5
SW
817 for (i = 0; i < blocks; i++) {
818 if (image_type == VDI_TYPE_STATIC) {
819 bmap[i] = i;
820 } else {
821 bmap[i] = VDI_UNALLOCATED;
822 }
9aebd98a 823 }
dddc7750
JC
824 ret = bdrv_pwrite_sync(bs, offset, bmap, bmap_size);
825 if (ret < 0) {
70747862
JC
826 error_setg(errp, "Error writing bmap to %s", filename);
827 goto exit;
514f21a5 828 }
70747862 829 offset += bmap_size;
9aebd98a 830 }
514f21a5 831
9aebd98a 832 if (image_type == VDI_TYPE_STATIC) {
dddc7750
JC
833 ret = bdrv_truncate(bs, offset + blocks * block_size);
834 if (ret < 0) {
70747862
JC
835 error_setg(errp, "Failed to statically allocate %s", filename);
836 goto exit;
9aebd98a
SW
837 }
838 }
839
63fa06dc 840exit:
70747862
JC
841 bdrv_unref(bs);
842 g_free(bmap);
dddc7750 843 return ret;
9aebd98a
SW
844}
845
846static void vdi_close(BlockDriverState *bs)
847{
fc9d106c 848 BDRVVdiState *s = bs->opaque;
6ac5f388 849
17cce735 850 qemu_vfree(s->bmap);
6ac5f388 851
fc9d106c
KW
852 migrate_del_blocker(s->migration_blocker);
853 error_free(s->migration_blocker);
9aebd98a
SW
854}
855
004b7f25
CL
856static QemuOptsList vdi_create_opts = {
857 .name = "vdi-create-opts",
858 .head = QTAILQ_HEAD_INITIALIZER(vdi_create_opts.head),
859 .desc = {
860 {
861 .name = BLOCK_OPT_SIZE,
862 .type = QEMU_OPT_SIZE,
863 .help = "Virtual disk size"
864 },
9aebd98a 865#if defined(CONFIG_VDI_BLOCK_SIZE)
004b7f25
CL
866 {
867 .name = BLOCK_OPT_CLUSTER_SIZE,
868 .type = QEMU_OPT_SIZE,
869 .help = "VDI cluster (block) size",
870 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
871 },
9aebd98a
SW
872#endif
873#if defined(CONFIG_VDI_STATIC_IMAGE)
004b7f25
CL
874 {
875 .name = BLOCK_OPT_STATIC,
876 .type = QEMU_OPT_BOOL,
877 .help = "VDI static (pre-allocated) image",
878 .def_value_str = "off"
879 },
9aebd98a 880#endif
004b7f25
CL
881 /* TODO: An additional option to set UUID values might be useful. */
882 { /* end of list */ }
883 }
9aebd98a
SW
884};
885
886static BlockDriver bdrv_vdi = {
887 .format_name = "vdi",
888 .instance_size = sizeof(BDRVVdiState),
889 .bdrv_probe = vdi_probe,
890 .bdrv_open = vdi_open,
891 .bdrv_close = vdi_close,
ecfe2bba 892 .bdrv_reopen_prepare = vdi_reopen_prepare,
c282e1fd 893 .bdrv_create = vdi_create,
3ac21627 894 .bdrv_has_zero_init = bdrv_has_zero_init_1,
b6b8a333 895 .bdrv_co_get_block_status = vdi_co_get_block_status,
9aebd98a
SW
896 .bdrv_make_empty = vdi_make_empty,
897
a7a43aa1 898 .bdrv_read = vdi_co_read,
9aebd98a 899#if defined(CONFIG_VDI_WRITE)
a7a43aa1 900 .bdrv_write = vdi_co_write,
9aebd98a
SW
901#endif
902
903 .bdrv_get_info = vdi_get_info,
904
004b7f25 905 .create_opts = &vdi_create_opts,
9aebd98a
SW
906 .bdrv_check = vdi_check,
907};
908
909static void bdrv_vdi_init(void)
910{
911 logout("\n");
912 bdrv_register(&bdrv_vdi);
913}
914
915block_init(bdrv_vdi_init);
This page took 0.551332 seconds and 4 git commands to generate.