]> git.proxmox.com Git - qemu.git/blob - block/vdi.c
vdi: basic conversion to coroutines
[qemu.git] / block / vdi.c
1 /*
2 * Block driver for the Virtual Disk Image (VDI) format
3 *
4 * Copyright (c) 2009, 2012 Stefan Weil
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 *
34 * Read and write of adjacents blocks could be done in one operation
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"
53 #include "block_int.h"
54 #include "module.h"
55 #include "migration.h"
56
57 #if defined(CONFIG_UUID)
58 #include <uuid/uuid.h>
59 #else
60 /* TODO: move uuid emulation to some central place in QEMU. */
61 #include "sysemu.h" /* UUID_FMT */
62 typedef unsigned char uuid_t[16];
63 void uuid_generate(uuid_t out);
64 int uuid_is_null(const uuid_t uu);
65 void uuid_unparse(const uuid_t uu, char *out);
66 #endif
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
87 #define KiB 1024
88 #define MiB (KiB * KiB)
89
90 #define SECTOR_SIZE 512
91 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
92
93 #if defined(CONFIG_VDI_DEBUG)
94 #define logout(fmt, ...) \
95 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
96 #else
97 #define logout(fmt, ...) ((void)0)
98 #endif
99
100 /* Image signature. */
101 #define VDI_SIGNATURE 0xbeda107f
102
103 /* Image version. */
104 #define VDI_VERSION_1_1 0x00010001
105
106 /* Image type. */
107 #define VDI_TYPE_DYNAMIC 1
108 #define VDI_TYPE_STATIC 2
109
110 /* Innotek / SUN images use these strings in header.text:
111 * "<<< innotek VirtualBox Disk Image >>>\n"
112 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
113 * "<<< Sun VirtualBox Disk Image >>>\n"
114 * The value does not matter, so QEMU created images use a different text.
115 */
116 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
117
118 /* A never-allocated block; semantically arbitrary content. */
119 #define VDI_UNALLOCATED 0xffffffffU
120
121 /* A discarded (no longer allocated) block; semantically zero-filled. */
122 #define VDI_DISCARDED 0xfffffffeU
123
124 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
125
126 #if !defined(CONFIG_UUID)
127 void uuid_generate(uuid_t out)
128 {
129 memset(out, 0, sizeof(uuid_t));
130 }
131
132 int uuid_is_null(const uuid_t uu)
133 {
134 uuid_t null_uuid = { 0 };
135 return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0;
136 }
137
138 void uuid_unparse(const uuid_t uu, char *out)
139 {
140 snprintf(out, 37, UUID_FMT,
141 uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7],
142 uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]);
143 }
144 #endif
145
146 typedef struct {
147 BlockDriverAIOCB common;
148 int64_t sector_num;
149 QEMUIOVector *qiov;
150 uint8_t *buf;
151 /* Total number of sectors. */
152 int nb_sectors;
153 /* Number of sectors for current AIO. */
154 int n_sectors;
155 /* New allocated block map entry. */
156 uint32_t bmap_first;
157 uint32_t bmap_last;
158 /* Buffer for new allocated block. */
159 void *block_buffer;
160 void *orig_buf;
161 bool is_write;
162 int header_modified;
163 struct iovec hd_iov;
164 QEMUIOVector hd_qiov;
165 QEMUBH *bh;
166 } VdiAIOCB;
167
168 typedef struct {
169 char text[0x40];
170 uint32_t signature;
171 uint32_t version;
172 uint32_t header_size;
173 uint32_t image_type;
174 uint32_t image_flags;
175 char description[256];
176 uint32_t offset_bmap;
177 uint32_t offset_data;
178 uint32_t cylinders; /* disk geometry, unused here */
179 uint32_t heads; /* disk geometry, unused here */
180 uint32_t sectors; /* disk geometry, unused here */
181 uint32_t sector_size;
182 uint32_t unused1;
183 uint64_t disk_size;
184 uint32_t block_size;
185 uint32_t block_extra; /* unused here */
186 uint32_t blocks_in_image;
187 uint32_t blocks_allocated;
188 uuid_t uuid_image;
189 uuid_t uuid_last_snap;
190 uuid_t uuid_link;
191 uuid_t uuid_parent;
192 uint64_t unused2[7];
193 } VdiHeader;
194
195 typedef struct {
196 /* The block map entries are little endian (even in memory). */
197 uint32_t *bmap;
198 /* Size of block (bytes). */
199 uint32_t block_size;
200 /* Size of block (sectors). */
201 uint32_t block_sectors;
202 /* First sector of block map. */
203 uint32_t bmap_sector;
204 /* VDI header (converted to host endianness). */
205 VdiHeader header;
206
207 Error *migration_blocker;
208 } BDRVVdiState;
209
210 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
211 * format (network byte order, standard, see RFC 4122) and vice versa.
212 */
213 static void uuid_convert(uuid_t uuid)
214 {
215 bswap32s((uint32_t *)&uuid[0]);
216 bswap16s((uint16_t *)&uuid[4]);
217 bswap16s((uint16_t *)&uuid[6]);
218 }
219
220 static void vdi_header_to_cpu(VdiHeader *header)
221 {
222 le32_to_cpus(&header->signature);
223 le32_to_cpus(&header->version);
224 le32_to_cpus(&header->header_size);
225 le32_to_cpus(&header->image_type);
226 le32_to_cpus(&header->image_flags);
227 le32_to_cpus(&header->offset_bmap);
228 le32_to_cpus(&header->offset_data);
229 le32_to_cpus(&header->cylinders);
230 le32_to_cpus(&header->heads);
231 le32_to_cpus(&header->sectors);
232 le32_to_cpus(&header->sector_size);
233 le64_to_cpus(&header->disk_size);
234 le32_to_cpus(&header->block_size);
235 le32_to_cpus(&header->block_extra);
236 le32_to_cpus(&header->blocks_in_image);
237 le32_to_cpus(&header->blocks_allocated);
238 uuid_convert(header->uuid_image);
239 uuid_convert(header->uuid_last_snap);
240 uuid_convert(header->uuid_link);
241 uuid_convert(header->uuid_parent);
242 }
243
244 static void vdi_header_to_le(VdiHeader *header)
245 {
246 cpu_to_le32s(&header->signature);
247 cpu_to_le32s(&header->version);
248 cpu_to_le32s(&header->header_size);
249 cpu_to_le32s(&header->image_type);
250 cpu_to_le32s(&header->image_flags);
251 cpu_to_le32s(&header->offset_bmap);
252 cpu_to_le32s(&header->offset_data);
253 cpu_to_le32s(&header->cylinders);
254 cpu_to_le32s(&header->heads);
255 cpu_to_le32s(&header->sectors);
256 cpu_to_le32s(&header->sector_size);
257 cpu_to_le64s(&header->disk_size);
258 cpu_to_le32s(&header->block_size);
259 cpu_to_le32s(&header->block_extra);
260 cpu_to_le32s(&header->blocks_in_image);
261 cpu_to_le32s(&header->blocks_allocated);
262 cpu_to_le32s(&header->blocks_allocated);
263 uuid_convert(header->uuid_image);
264 uuid_convert(header->uuid_last_snap);
265 uuid_convert(header->uuid_link);
266 uuid_convert(header->uuid_parent);
267 }
268
269 #if defined(CONFIG_VDI_DEBUG)
270 static void vdi_header_print(VdiHeader *header)
271 {
272 char uuid[37];
273 logout("text %s", header->text);
274 logout("signature 0x%04x\n", header->signature);
275 logout("header size 0x%04x\n", header->header_size);
276 logout("image type 0x%04x\n", header->image_type);
277 logout("image flags 0x%04x\n", header->image_flags);
278 logout("description %s\n", header->description);
279 logout("offset bmap 0x%04x\n", header->offset_bmap);
280 logout("offset data 0x%04x\n", header->offset_data);
281 logout("cylinders 0x%04x\n", header->cylinders);
282 logout("heads 0x%04x\n", header->heads);
283 logout("sectors 0x%04x\n", header->sectors);
284 logout("sector size 0x%04x\n", header->sector_size);
285 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
286 header->disk_size, header->disk_size / MiB);
287 logout("block size 0x%04x\n", header->block_size);
288 logout("block extra 0x%04x\n", header->block_extra);
289 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
290 logout("blocks all. 0x%04x\n", header->blocks_allocated);
291 uuid_unparse(header->uuid_image, uuid);
292 logout("uuid image %s\n", uuid);
293 uuid_unparse(header->uuid_last_snap, uuid);
294 logout("uuid snap %s\n", uuid);
295 uuid_unparse(header->uuid_link, uuid);
296 logout("uuid link %s\n", uuid);
297 uuid_unparse(header->uuid_parent, uuid);
298 logout("uuid parent %s\n", uuid);
299 }
300 #endif
301
302 static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res)
303 {
304 /* TODO: additional checks possible. */
305 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
306 uint32_t blocks_allocated = 0;
307 uint32_t block;
308 uint32_t *bmap;
309 logout("\n");
310
311 bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t));
312 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
313
314 /* Check block map and value of blocks_allocated. */
315 for (block = 0; block < s->header.blocks_in_image; block++) {
316 uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
317 if (VDI_IS_ALLOCATED(bmap_entry)) {
318 if (bmap_entry < s->header.blocks_in_image) {
319 blocks_allocated++;
320 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
321 bmap[bmap_entry] = bmap_entry;
322 } else {
323 fprintf(stderr, "ERROR: block index %" PRIu32
324 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
325 res->corruptions++;
326 }
327 } else {
328 fprintf(stderr, "ERROR: block index %" PRIu32
329 " too large, is %" PRIu32 "\n", block, bmap_entry);
330 res->corruptions++;
331 }
332 }
333 }
334 if (blocks_allocated != s->header.blocks_allocated) {
335 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
336 ", should be %" PRIu32 "\n",
337 blocks_allocated, s->header.blocks_allocated);
338 res->corruptions++;
339 }
340
341 g_free(bmap);
342
343 return 0;
344 }
345
346 static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
347 {
348 /* TODO: vdi_get_info would be needed for machine snapshots.
349 vm_state_offset is still missing. */
350 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
351 logout("\n");
352 bdi->cluster_size = s->block_size;
353 bdi->vm_state_offset = 0;
354 return 0;
355 }
356
357 static int vdi_make_empty(BlockDriverState *bs)
358 {
359 /* TODO: missing code. */
360 logout("\n");
361 /* The return value for missing code must be 0, see block.c. */
362 return 0;
363 }
364
365 static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
366 {
367 const VdiHeader *header = (const VdiHeader *)buf;
368 int result = 0;
369
370 logout("\n");
371
372 if (buf_size < sizeof(*header)) {
373 /* Header too small, no VDI. */
374 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
375 result = 100;
376 }
377
378 if (result == 0) {
379 logout("no vdi image\n");
380 } else {
381 logout("%s", header->text);
382 }
383
384 return result;
385 }
386
387 static int vdi_open(BlockDriverState *bs, int flags)
388 {
389 BDRVVdiState *s = bs->opaque;
390 VdiHeader header;
391 size_t bmap_size;
392
393 logout("\n");
394
395 if (bdrv_read(bs->file, 0, (uint8_t *)&header, 1) < 0) {
396 goto fail;
397 }
398
399 vdi_header_to_cpu(&header);
400 #if defined(CONFIG_VDI_DEBUG)
401 vdi_header_print(&header);
402 #endif
403
404 if (header.disk_size % SECTOR_SIZE != 0) {
405 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
406 We accept them but round the disk size to the next multiple of
407 SECTOR_SIZE. */
408 logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size);
409 header.disk_size += SECTOR_SIZE - 1;
410 header.disk_size &= ~(SECTOR_SIZE - 1);
411 }
412
413 if (header.version != VDI_VERSION_1_1) {
414 logout("unsupported version %u.%u\n",
415 header.version >> 16, header.version & 0xffff);
416 goto fail;
417 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
418 /* We only support block maps which start on a sector boundary. */
419 logout("unsupported block map offset 0x%x B\n", header.offset_bmap);
420 goto fail;
421 } else if (header.offset_data % SECTOR_SIZE != 0) {
422 /* We only support data blocks which start on a sector boundary. */
423 logout("unsupported data offset 0x%x B\n", header.offset_data);
424 goto fail;
425 } else if (header.sector_size != SECTOR_SIZE) {
426 logout("unsupported sector size %u B\n", header.sector_size);
427 goto fail;
428 } else if (header.block_size != 1 * MiB) {
429 logout("unsupported block size %u B\n", header.block_size);
430 goto fail;
431 } else if (header.disk_size >
432 (uint64_t)header.blocks_in_image * header.block_size) {
433 logout("unsupported disk size %" PRIu64 " B\n", header.disk_size);
434 goto fail;
435 } else if (!uuid_is_null(header.uuid_link)) {
436 logout("link uuid != 0, unsupported\n");
437 goto fail;
438 } else if (!uuid_is_null(header.uuid_parent)) {
439 logout("parent uuid != 0, unsupported\n");
440 goto fail;
441 }
442
443 bs->total_sectors = header.disk_size / SECTOR_SIZE;
444
445 s->block_size = header.block_size;
446 s->block_sectors = header.block_size / SECTOR_SIZE;
447 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
448 s->header = header;
449
450 bmap_size = header.blocks_in_image * sizeof(uint32_t);
451 bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE;
452 if (bmap_size > 0) {
453 s->bmap = g_malloc(bmap_size * SECTOR_SIZE);
454 }
455 if (bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size) < 0) {
456 goto fail_free_bmap;
457 }
458
459 /* Disable migration when vdi images are used */
460 error_set(&s->migration_blocker,
461 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
462 "vdi", bs->device_name, "live migration");
463 migrate_add_blocker(s->migration_blocker);
464
465 return 0;
466
467 fail_free_bmap:
468 g_free(s->bmap);
469
470 fail:
471 return -1;
472 }
473
474 static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs,
475 int64_t sector_num, int nb_sectors, int *pnum)
476 {
477 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
478 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
479 size_t bmap_index = sector_num / s->block_sectors;
480 size_t sector_in_block = sector_num % s->block_sectors;
481 int n_sectors = s->block_sectors - sector_in_block;
482 uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
483 logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum);
484 if (n_sectors > nb_sectors) {
485 n_sectors = nb_sectors;
486 }
487 *pnum = n_sectors;
488 return VDI_IS_ALLOCATED(bmap_entry);
489 }
490
491 static AIOPool vdi_aio_pool = {
492 .aiocb_size = sizeof(VdiAIOCB),
493 };
494
495 static VdiAIOCB *vdi_aio_setup(BlockDriverState *bs, int64_t sector_num,
496 QEMUIOVector *qiov, int nb_sectors, int is_write)
497 {
498 VdiAIOCB *acb;
499
500 logout("%p, %" PRId64 ", %p, %d, %p, %p, %d\n",
501 bs, sector_num, qiov, nb_sectors, is_write);
502
503 acb = qemu_aio_get(&vdi_aio_pool, bs, NULL, NULL);
504 acb->sector_num = sector_num;
505 acb->qiov = qiov;
506 acb->is_write = is_write;
507
508 if (qiov->niov > 1) {
509 acb->buf = qemu_blockalign(bs, qiov->size);
510 acb->orig_buf = acb->buf;
511 if (is_write) {
512 qemu_iovec_to_buffer(qiov, acb->buf);
513 }
514 } else {
515 acb->buf = (uint8_t *)qiov->iov->iov_base;
516 }
517 acb->nb_sectors = nb_sectors;
518 acb->n_sectors = 0;
519 acb->bmap_first = VDI_UNALLOCATED;
520 acb->bmap_last = VDI_UNALLOCATED;
521 acb->block_buffer = NULL;
522 acb->header_modified = 0;
523 return acb;
524 }
525
526 static int vdi_aio_read_cb(void *opaque, int ret)
527 {
528 VdiAIOCB *acb = opaque;
529 BlockDriverState *bs = acb->common.bs;
530 BDRVVdiState *s = bs->opaque;
531 uint32_t bmap_entry;
532 uint32_t block_index;
533 uint32_t sector_in_block;
534 uint32_t n_sectors;
535
536 logout("%u sectors read\n", acb->n_sectors);
537
538 restart:
539 acb->nb_sectors -= acb->n_sectors;
540
541 if (acb->nb_sectors == 0) {
542 /* request completed */
543 ret = 0;
544 goto done;
545 }
546
547 acb->sector_num += acb->n_sectors;
548 acb->buf += acb->n_sectors * SECTOR_SIZE;
549
550 block_index = acb->sector_num / s->block_sectors;
551 sector_in_block = acb->sector_num % s->block_sectors;
552 n_sectors = s->block_sectors - sector_in_block;
553 if (n_sectors > acb->nb_sectors) {
554 n_sectors = acb->nb_sectors;
555 }
556
557 logout("will read %u sectors starting at sector %" PRIu64 "\n",
558 n_sectors, acb->sector_num);
559
560 /* prepare next AIO request */
561 acb->n_sectors = n_sectors;
562 bmap_entry = le32_to_cpu(s->bmap[block_index]);
563 if (!VDI_IS_ALLOCATED(bmap_entry)) {
564 /* Block not allocated, return zeros, no need to wait. */
565 memset(acb->buf, 0, n_sectors * SECTOR_SIZE);
566 ret = 0;
567 } else {
568 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
569 (uint64_t)bmap_entry * s->block_sectors +
570 sector_in_block;
571 acb->hd_iov.iov_base = (void *)acb->buf;
572 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
573 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
574 ret = bdrv_co_readv(bs->file, offset, n_sectors, &acb->hd_qiov);
575 }
576 if (ret >= 0) {
577 goto restart;
578 }
579
580 done:
581 if (acb->qiov->niov > 1) {
582 qemu_iovec_from_buffer(acb->qiov, acb->orig_buf, acb->qiov->size);
583 qemu_vfree(acb->orig_buf);
584 }
585 qemu_aio_release(acb);
586 return ret;
587 }
588
589 static int vdi_co_readv(BlockDriverState *bs,
590 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
591 {
592 VdiAIOCB *acb;
593 int ret;
594
595 logout("\n");
596 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, 0);
597 ret = vdi_aio_read_cb(acb, 0);
598 return ret;
599 }
600
601 static int vdi_aio_write_cb(void *opaque, int ret)
602 {
603 VdiAIOCB *acb = opaque;
604 BlockDriverState *bs = acb->common.bs;
605 BDRVVdiState *s = bs->opaque;
606 uint32_t bmap_entry;
607 uint32_t block_index;
608 uint32_t sector_in_block;
609 uint32_t n_sectors;
610
611 restart:
612 acb->nb_sectors -= acb->n_sectors;
613 acb->sector_num += acb->n_sectors;
614 acb->buf += acb->n_sectors * SECTOR_SIZE;
615
616 if (acb->nb_sectors == 0) {
617 logout("finished data write\n");
618 acb->n_sectors = 0;
619 ret = 0;
620 if (acb->header_modified) {
621 VdiHeader *header = acb->block_buffer;
622 logout("now writing modified header\n");
623 assert(VDI_IS_ALLOCATED(acb->bmap_first));
624 *header = s->header;
625 vdi_header_to_le(header);
626 acb->header_modified = 0;
627 acb->hd_iov.iov_base = acb->block_buffer;
628 acb->hd_iov.iov_len = SECTOR_SIZE;
629 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
630 ret = bdrv_co_writev(bs->file, 0, 1, &acb->hd_qiov);
631 }
632 if (ret >= 0 && VDI_IS_ALLOCATED(acb->bmap_first)) {
633 /* One or more new blocks were allocated. */
634 uint64_t offset;
635 uint32_t bmap_first;
636 uint32_t bmap_last;
637 g_free(acb->block_buffer);
638 acb->block_buffer = NULL;
639 bmap_first = acb->bmap_first;
640 bmap_last = acb->bmap_last;
641 logout("now writing modified block map entry %u...%u\n",
642 bmap_first, bmap_last);
643 /* Write modified sectors from block map. */
644 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
645 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
646 n_sectors = bmap_last - bmap_first + 1;
647 offset = s->bmap_sector + bmap_first;
648 acb->bmap_first = VDI_UNALLOCATED;
649 acb->hd_iov.iov_base = (void *)((uint8_t *)&s->bmap[0] +
650 bmap_first * SECTOR_SIZE);
651 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
652 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
653 logout("will write %u block map sectors starting from entry %u\n",
654 n_sectors, bmap_first);
655 ret = bdrv_co_writev(bs->file, offset, n_sectors, &acb->hd_qiov);
656 }
657 goto done;
658 }
659
660 logout("%u sectors written\n", acb->n_sectors);
661
662 block_index = acb->sector_num / s->block_sectors;
663 sector_in_block = acb->sector_num % s->block_sectors;
664 n_sectors = s->block_sectors - sector_in_block;
665 if (n_sectors > acb->nb_sectors) {
666 n_sectors = acb->nb_sectors;
667 }
668
669 logout("will write %u sectors starting at sector %" PRIu64 "\n",
670 n_sectors, acb->sector_num);
671
672 /* prepare next AIO request */
673 acb->n_sectors = n_sectors;
674 bmap_entry = le32_to_cpu(s->bmap[block_index]);
675 if (!VDI_IS_ALLOCATED(bmap_entry)) {
676 /* Allocate new block and write to it. */
677 uint64_t offset;
678 uint8_t *block;
679 bmap_entry = s->header.blocks_allocated;
680 s->bmap[block_index] = cpu_to_le32(bmap_entry);
681 s->header.blocks_allocated++;
682 offset = s->header.offset_data / SECTOR_SIZE +
683 (uint64_t)bmap_entry * s->block_sectors;
684 block = acb->block_buffer;
685 if (block == NULL) {
686 block = g_malloc(s->block_size);
687 acb->block_buffer = block;
688 acb->bmap_first = block_index;
689 assert(!acb->header_modified);
690 acb->header_modified = 1;
691 }
692 acb->bmap_last = block_index;
693 /* Copy data to be written to new block and zero unused parts. */
694 memset(block, 0, sector_in_block * SECTOR_SIZE);
695 memcpy(block + sector_in_block * SECTOR_SIZE,
696 acb->buf, n_sectors * SECTOR_SIZE);
697 memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0,
698 (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE);
699 acb->hd_iov.iov_base = (void *)block;
700 acb->hd_iov.iov_len = s->block_size;
701 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
702 ret = bdrv_co_writev(bs->file, offset, s->block_sectors, &acb->hd_qiov);
703 } else {
704 uint64_t offset = s->header.offset_data / SECTOR_SIZE +
705 (uint64_t)bmap_entry * s->block_sectors +
706 sector_in_block;
707 acb->hd_iov.iov_base = (void *)acb->buf;
708 acb->hd_iov.iov_len = n_sectors * SECTOR_SIZE;
709 qemu_iovec_init_external(&acb->hd_qiov, &acb->hd_iov, 1);
710 ret = bdrv_co_writev(bs->file, offset, n_sectors, &acb->hd_qiov);
711 }
712 if (ret >= 0) {
713 goto restart;
714 }
715
716 done:
717 if (acb->qiov->niov > 1) {
718 qemu_vfree(acb->orig_buf);
719 }
720 qemu_aio_release(acb);
721 return ret;
722 }
723
724 static int vdi_co_writev(BlockDriverState *bs,
725 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
726 {
727 VdiAIOCB *acb;
728 int ret;
729
730 logout("\n");
731 acb = vdi_aio_setup(bs, sector_num, qiov, nb_sectors, 1);
732 ret = vdi_aio_write_cb(acb, 0);
733 return ret;
734 }
735
736 static int vdi_create(const char *filename, QEMUOptionParameter *options)
737 {
738 int fd;
739 int result = 0;
740 uint64_t bytes = 0;
741 uint32_t blocks;
742 size_t block_size = DEFAULT_CLUSTER_SIZE;
743 uint32_t image_type = VDI_TYPE_DYNAMIC;
744 VdiHeader header;
745 size_t i;
746 size_t bmap_size;
747 uint32_t *bmap;
748
749 logout("\n");
750
751 /* Read out options. */
752 while (options && options->name) {
753 if (!strcmp(options->name, BLOCK_OPT_SIZE)) {
754 bytes = options->value.n;
755 #if defined(CONFIG_VDI_BLOCK_SIZE)
756 } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) {
757 if (options->value.n) {
758 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
759 block_size = options->value.n;
760 }
761 #endif
762 #if defined(CONFIG_VDI_STATIC_IMAGE)
763 } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) {
764 if (options->value.n) {
765 image_type = VDI_TYPE_STATIC;
766 }
767 #endif
768 }
769 options++;
770 }
771
772 fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE,
773 0644);
774 if (fd < 0) {
775 return -errno;
776 }
777
778 /* We need enough blocks to store the given disk size,
779 so always round up. */
780 blocks = (bytes + block_size - 1) / block_size;
781
782 bmap_size = blocks * sizeof(uint32_t);
783 bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1));
784
785 memset(&header, 0, sizeof(header));
786 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
787 header.signature = VDI_SIGNATURE;
788 header.version = VDI_VERSION_1_1;
789 header.header_size = 0x180;
790 header.image_type = image_type;
791 header.offset_bmap = 0x200;
792 header.offset_data = 0x200 + bmap_size;
793 header.sector_size = SECTOR_SIZE;
794 header.disk_size = bytes;
795 header.block_size = block_size;
796 header.blocks_in_image = blocks;
797 if (image_type == VDI_TYPE_STATIC) {
798 header.blocks_allocated = blocks;
799 }
800 uuid_generate(header.uuid_image);
801 uuid_generate(header.uuid_last_snap);
802 /* There is no need to set header.uuid_link or header.uuid_parent here. */
803 #if defined(CONFIG_VDI_DEBUG)
804 vdi_header_print(&header);
805 #endif
806 vdi_header_to_le(&header);
807 if (write(fd, &header, sizeof(header)) < 0) {
808 result = -errno;
809 }
810
811 bmap = NULL;
812 if (bmap_size > 0) {
813 bmap = (uint32_t *)g_malloc0(bmap_size);
814 }
815 for (i = 0; i < blocks; i++) {
816 if (image_type == VDI_TYPE_STATIC) {
817 bmap[i] = i;
818 } else {
819 bmap[i] = VDI_UNALLOCATED;
820 }
821 }
822 if (write(fd, bmap, bmap_size) < 0) {
823 result = -errno;
824 }
825 g_free(bmap);
826 if (image_type == VDI_TYPE_STATIC) {
827 if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) {
828 result = -errno;
829 }
830 }
831
832 if (close(fd) < 0) {
833 result = -errno;
834 }
835
836 return result;
837 }
838
839 static void vdi_close(BlockDriverState *bs)
840 {
841 BDRVVdiState *s = bs->opaque;
842
843 g_free(s->bmap);
844
845 migrate_del_blocker(s->migration_blocker);
846 error_free(s->migration_blocker);
847 }
848
849 static QEMUOptionParameter vdi_create_options[] = {
850 {
851 .name = BLOCK_OPT_SIZE,
852 .type = OPT_SIZE,
853 .help = "Virtual disk size"
854 },
855 #if defined(CONFIG_VDI_BLOCK_SIZE)
856 {
857 .name = BLOCK_OPT_CLUSTER_SIZE,
858 .type = OPT_SIZE,
859 .help = "VDI cluster (block) size",
860 .value = { .n = DEFAULT_CLUSTER_SIZE },
861 },
862 #endif
863 #if defined(CONFIG_VDI_STATIC_IMAGE)
864 {
865 .name = BLOCK_OPT_STATIC,
866 .type = OPT_FLAG,
867 .help = "VDI static (pre-allocated) image"
868 },
869 #endif
870 /* TODO: An additional option to set UUID values might be useful. */
871 { NULL }
872 };
873
874 static BlockDriver bdrv_vdi = {
875 .format_name = "vdi",
876 .instance_size = sizeof(BDRVVdiState),
877 .bdrv_probe = vdi_probe,
878 .bdrv_open = vdi_open,
879 .bdrv_close = vdi_close,
880 .bdrv_create = vdi_create,
881 .bdrv_co_is_allocated = vdi_co_is_allocated,
882 .bdrv_make_empty = vdi_make_empty,
883
884 .bdrv_co_readv = vdi_co_readv,
885 #if defined(CONFIG_VDI_WRITE)
886 .bdrv_co_writev = vdi_co_writev,
887 #endif
888
889 .bdrv_get_info = vdi_get_info,
890
891 .create_options = vdi_create_options,
892 .bdrv_check = vdi_check,
893 };
894
895 static void bdrv_vdi_init(void)
896 {
897 logout("\n");
898 bdrv_register(&bdrv_vdi);
899 }
900
901 block_init(bdrv_vdi_init);