blob: f2434d6153e1a9b8bc947f131a8c488c1a24ef46 [file] [log] [blame]
Stefan Weil9aebd982009-07-31 21:45:56 +02001/*
2 * Block driver for the Virtual Disk Image (VDI) format
3 *
Stefan Weil641543b2012-01-21 13:54:24 +01004 * Copyright (c) 2009, 2012 Stefan Weil
Stefan Weil9aebd982009-07-31 21:45:56 +02005 *
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 *
Deepak Kathayatdc6fb732014-03-24 16:30:17 +080034 * Read and write of adjacent blocks could be done in one operation
Stefan Weil9aebd982009-07-31 21:45:56 +020035 * (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
Peter Maydell80c71a22016-01-18 18:01:42 +000052#include "qemu/osdep.h"
Philippe Mathieu-Daudéf0435682018-06-25 09:41:54 -030053#include "qemu/units.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010054#include "qapi/error.h"
Max Reitz49858b52018-03-12 17:55:26 +010055#include "qapi/qobject-input-visitor.h"
56#include "qapi/qapi-visit-block-core.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010057#include "block/block_int.h"
Markus Armbrusterf8534652018-06-14 21:14:34 +020058#include "block/qdict.h"
Kevin Wolfa08f0c32016-03-08 15:57:05 +010059#include "sysemu/block-backend.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010060#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010061#include "qemu/option.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010062#include "qemu/bswap.h"
Juan Quintela795c40b2017-04-06 12:00:28 +020063#include "migration/blocker.h"
Daniel P. Berrange10817bf2015-09-01 14:48:02 +010064#include "qemu/coroutine.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020065#include "qemu/cutils.h"
Fam Zheng7c6f55b2016-09-21 12:27:17 +080066#include "qemu/uuid.h"
Peter Maydell5df022c2022-02-26 18:07:23 +000067#include "qemu/memalign.h"
Stefan Weil9aebd982009-07-31 21:45:56 +020068
69/* Code configuration options. */
70
71/* Enable debug messages. */
72//~ #define CONFIG_VDI_DEBUG
73
74/* Support write operations on VDI images. */
75#define CONFIG_VDI_WRITE
76
77/* Support non-standard block (cluster) size. This is untested.
78 * Maybe it will be needed for very large images.
79 */
80//~ #define CONFIG_VDI_BLOCK_SIZE
81
82/* Support static (fixed, pre-allocated) images. */
83#define CONFIG_VDI_STATIC_IMAGE
84
85/* Command line option for static images. */
86#define BLOCK_OPT_STATIC "static"
87
Stefan Weil9aebd982009-07-31 21:45:56 +020088#define SECTOR_SIZE 512
Markus Armbruster14632122019-01-11 20:14:01 +010089#define DEFAULT_CLUSTER_SIZE 1048576
90/* Note: can't use 1 * MiB, because it's passed to stringify() */
Stefan Weil9aebd982009-07-31 21:45:56 +020091
92#if defined(CONFIG_VDI_DEBUG)
Eric Blakeb80666b2018-02-13 14:26:56 -060093#define VDI_DEBUG 1
Stefan Weil9aebd982009-07-31 21:45:56 +020094#else
Eric Blakeb80666b2018-02-13 14:26:56 -060095#define VDI_DEBUG 0
Stefan Weil9aebd982009-07-31 21:45:56 +020096#endif
97
Eric Blakeb80666b2018-02-13 14:26:56 -060098#define logout(fmt, ...) \
99 do { \
100 if (VDI_DEBUG) { \
101 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \
102 } \
103 } while (0)
104
Stefan Weil9aebd982009-07-31 21:45:56 +0200105/* Image signature. */
106#define VDI_SIGNATURE 0xbeda107f
107
108/* Image version. */
109#define VDI_VERSION_1_1 0x00010001
110
111/* Image type. */
112#define VDI_TYPE_DYNAMIC 1
113#define VDI_TYPE_STATIC 2
114
115/* Innotek / SUN images use these strings in header.text:
116 * "<<< innotek VirtualBox Disk Image >>>\n"
117 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
118 * "<<< Sun VirtualBox Disk Image >>>\n"
119 * The value does not matter, so QEMU created images use a different text.
120 */
121#define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
122
Eric Sunshinec794b4e2011-10-26 15:51:18 -0400123/* A never-allocated block; semantically arbitrary content. */
124#define VDI_UNALLOCATED 0xffffffffU
125
126/* A discarded (no longer allocated) block; semantically zero-filled. */
127#define VDI_DISCARDED 0xfffffffeU
128
129#define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
Stefan Weil9aebd982009-07-31 21:45:56 +0200130
Max Reitzd20418e2014-10-28 11:12:32 +0100131/* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since
132 * the bmap is read and written in a single operation, its size needs to be
133 * limited to INT_MAX; furthermore, when opening an image, the bmap size is
134 * rounded up to be aligned on BDRV_SECTOR_SIZE.
135 * Therefore this should satisfy the following:
136 * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1
137 * (INT_MAX + 1 is the first value not representable as an int)
138 * This guarantees that any value below or equal to the constant will, when
139 * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary,
140 * still be below or equal to INT_MAX. */
141#define VDI_BLOCKS_IN_IMAGE_MAX \
142 ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t)))
Jeff Cody63fa06d2014-03-28 11:42:24 -0400143#define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \
144 (uint64_t)DEFAULT_CLUSTER_SIZE)
145
Max Reitz49858b52018-03-12 17:55:26 +0100146static QemuOptsList vdi_create_opts;
147
Stefan Weil9aebd982009-07-31 21:45:56 +0200148typedef struct {
Stefan Weil9aebd982009-07-31 21:45:56 +0200149 char text[0x40];
150 uint32_t signature;
151 uint32_t version;
152 uint32_t header_size;
153 uint32_t image_type;
154 uint32_t image_flags;
155 char description[256];
156 uint32_t offset_bmap;
157 uint32_t offset_data;
158 uint32_t cylinders; /* disk geometry, unused here */
159 uint32_t heads; /* disk geometry, unused here */
160 uint32_t sectors; /* disk geometry, unused here */
161 uint32_t sector_size;
162 uint32_t unused1;
163 uint64_t disk_size;
164 uint32_t block_size;
165 uint32_t block_extra; /* unused here */
166 uint32_t blocks_in_image;
167 uint32_t blocks_allocated;
Fam Zheng7c6f55b2016-09-21 12:27:17 +0800168 QemuUUID uuid_image;
169 QemuUUID uuid_last_snap;
170 QemuUUID uuid_link;
171 QemuUUID uuid_parent;
Stefan Weil9aebd982009-07-31 21:45:56 +0200172 uint64_t unused2[7];
Jeff Cody8368feb2013-09-25 12:08:48 -0400173} QEMU_PACKED VdiHeader;
Stefan Weil9aebd982009-07-31 21:45:56 +0200174
Alberto Garciad4f18972019-05-01 21:13:56 +0300175QEMU_BUILD_BUG_ON(sizeof(VdiHeader) != 512);
176
Stefan Weil9aebd982009-07-31 21:45:56 +0200177typedef struct {
Stefan Weil9aebd982009-07-31 21:45:56 +0200178 /* The block map entries are little endian (even in memory). */
179 uint32_t *bmap;
180 /* Size of block (bytes). */
181 uint32_t block_size;
Stefan Weil9aebd982009-07-31 21:45:56 +0200182 /* First sector of block map. */
183 uint32_t bmap_sector;
Stefan Weil4ff97862011-03-13 15:44:02 +0100184 /* VDI header (converted to host endianness). */
Stefan Weil9aebd982009-07-31 21:45:56 +0200185 VdiHeader header;
Kevin Wolffc9d1062011-11-22 16:46:26 +0100186
Paolo Bonzini1e886632017-06-29 15:27:41 +0200187 CoRwlock bmap_lock;
Max Reitzf0ab6f12015-02-27 14:54:39 -0500188
Kevin Wolffc9d1062011-11-22 16:46:26 +0100189 Error *migration_blocker;
Stefan Weil9aebd982009-07-31 21:45:56 +0200190} BDRVVdiState;
191
Stefan Weil9aebd982009-07-31 21:45:56 +0200192static void vdi_header_to_cpu(VdiHeader *header)
193{
Peter Maydell09190182018-10-16 18:25:03 +0100194 header->signature = le32_to_cpu(header->signature);
195 header->version = le32_to_cpu(header->version);
196 header->header_size = le32_to_cpu(header->header_size);
197 header->image_type = le32_to_cpu(header->image_type);
198 header->image_flags = le32_to_cpu(header->image_flags);
199 header->offset_bmap = le32_to_cpu(header->offset_bmap);
200 header->offset_data = le32_to_cpu(header->offset_data);
201 header->cylinders = le32_to_cpu(header->cylinders);
202 header->heads = le32_to_cpu(header->heads);
203 header->sectors = le32_to_cpu(header->sectors);
204 header->sector_size = le32_to_cpu(header->sector_size);
205 header->disk_size = le64_to_cpu(header->disk_size);
206 header->block_size = le32_to_cpu(header->block_size);
207 header->block_extra = le32_to_cpu(header->block_extra);
208 header->blocks_in_image = le32_to_cpu(header->blocks_in_image);
209 header->blocks_allocated = le32_to_cpu(header->blocks_allocated);
Peter Maydell1324f062018-12-10 11:26:49 +0000210 header->uuid_image = qemu_uuid_bswap(header->uuid_image);
211 header->uuid_last_snap = qemu_uuid_bswap(header->uuid_last_snap);
212 header->uuid_link = qemu_uuid_bswap(header->uuid_link);
213 header->uuid_parent = qemu_uuid_bswap(header->uuid_parent);
Stefan Weil9aebd982009-07-31 21:45:56 +0200214}
215
216static void vdi_header_to_le(VdiHeader *header)
217{
Peter Maydell09190182018-10-16 18:25:03 +0100218 header->signature = cpu_to_le32(header->signature);
219 header->version = cpu_to_le32(header->version);
220 header->header_size = cpu_to_le32(header->header_size);
221 header->image_type = cpu_to_le32(header->image_type);
222 header->image_flags = cpu_to_le32(header->image_flags);
223 header->offset_bmap = cpu_to_le32(header->offset_bmap);
224 header->offset_data = cpu_to_le32(header->offset_data);
225 header->cylinders = cpu_to_le32(header->cylinders);
226 header->heads = cpu_to_le32(header->heads);
227 header->sectors = cpu_to_le32(header->sectors);
228 header->sector_size = cpu_to_le32(header->sector_size);
229 header->disk_size = cpu_to_le64(header->disk_size);
230 header->block_size = cpu_to_le32(header->block_size);
231 header->block_extra = cpu_to_le32(header->block_extra);
232 header->blocks_in_image = cpu_to_le32(header->blocks_in_image);
233 header->blocks_allocated = cpu_to_le32(header->blocks_allocated);
Peter Maydell1324f062018-12-10 11:26:49 +0000234 header->uuid_image = qemu_uuid_bswap(header->uuid_image);
235 header->uuid_last_snap = qemu_uuid_bswap(header->uuid_last_snap);
236 header->uuid_link = qemu_uuid_bswap(header->uuid_link);
237 header->uuid_parent = qemu_uuid_bswap(header->uuid_parent);
Stefan Weil9aebd982009-07-31 21:45:56 +0200238}
239
Stefan Weil9aebd982009-07-31 21:45:56 +0200240static void vdi_header_print(VdiHeader *header)
241{
Peter Maydellac928b82018-12-10 11:26:48 +0000242 char uuidstr[37];
243 QemuUUID uuid;
Stefan Weil9aebd982009-07-31 21:45:56 +0200244 logout("text %s", header->text);
Stefan Weil9f0470b2013-01-17 21:45:26 +0100245 logout("signature 0x%08x\n", header->signature);
Stefan Weil9aebd982009-07-31 21:45:56 +0200246 logout("header size 0x%04x\n", header->header_size);
247 logout("image type 0x%04x\n", header->image_type);
248 logout("image flags 0x%04x\n", header->image_flags);
249 logout("description %s\n", header->description);
250 logout("offset bmap 0x%04x\n", header->offset_bmap);
251 logout("offset data 0x%04x\n", header->offset_data);
252 logout("cylinders 0x%04x\n", header->cylinders);
253 logout("heads 0x%04x\n", header->heads);
254 logout("sectors 0x%04x\n", header->sectors);
255 logout("sector size 0x%04x\n", header->sector_size);
256 logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n",
257 header->disk_size, header->disk_size / MiB);
258 logout("block size 0x%04x\n", header->block_size);
259 logout("block extra 0x%04x\n", header->block_extra);
260 logout("blocks tot. 0x%04x\n", header->blocks_in_image);
261 logout("blocks all. 0x%04x\n", header->blocks_allocated);
Peter Maydellac928b82018-12-10 11:26:48 +0000262 uuid = header->uuid_image;
263 qemu_uuid_unparse(&uuid, uuidstr);
264 logout("uuid image %s\n", uuidstr);
265 uuid = header->uuid_last_snap;
266 qemu_uuid_unparse(&uuid, uuidstr);
267 logout("uuid snap %s\n", uuidstr);
268 uuid = header->uuid_link;
269 qemu_uuid_unparse(&uuid, uuidstr);
270 logout("uuid link %s\n", uuidstr);
271 uuid = header->uuid_parent;
272 qemu_uuid_unparse(&uuid, uuidstr);
273 logout("uuid parent %s\n", uuidstr);
Stefan Weil9aebd982009-07-31 21:45:56 +0200274}
Stefan Weil9aebd982009-07-31 21:45:56 +0200275
Paolo Bonzini2fd61632018-03-01 17:36:19 +0100276static int coroutine_fn vdi_co_check(BlockDriverState *bs, BdrvCheckResult *res,
277 BdrvCheckMode fix)
Stefan Weil9aebd982009-07-31 21:45:56 +0200278{
279 /* TODO: additional checks possible. */
280 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
Stefan Weil9aebd982009-07-31 21:45:56 +0200281 uint32_t blocks_allocated = 0;
282 uint32_t block;
283 uint32_t *bmap;
284 logout("\n");
285
Kevin Wolf4534ff52012-05-11 16:07:02 +0200286 if (fix) {
287 return -ENOTSUP;
288 }
289
Markus Armbruster5839e532014-08-19 10:31:08 +0200290 bmap = g_try_new(uint32_t, s->header.blocks_in_image);
Kevin Wolf17cce732014-05-20 13:25:43 +0200291 if (s->header.blocks_in_image && bmap == NULL) {
292 res->check_errors++;
293 return -ENOMEM;
294 }
295
Stefan Weil9aebd982009-07-31 21:45:56 +0200296 memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t));
297
298 /* Check block map and value of blocks_allocated. */
299 for (block = 0; block < s->header.blocks_in_image; block++) {
300 uint32_t bmap_entry = le32_to_cpu(s->bmap[block]);
Eric Sunshinec794b4e2011-10-26 15:51:18 -0400301 if (VDI_IS_ALLOCATED(bmap_entry)) {
Stefan Weil9aebd982009-07-31 21:45:56 +0200302 if (bmap_entry < s->header.blocks_in_image) {
303 blocks_allocated++;
Eric Sunshinec794b4e2011-10-26 15:51:18 -0400304 if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) {
Stefan Weil9aebd982009-07-31 21:45:56 +0200305 bmap[bmap_entry] = bmap_entry;
306 } else {
307 fprintf(stderr, "ERROR: block index %" PRIu32
308 " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry);
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200309 res->corruptions++;
Stefan Weil9aebd982009-07-31 21:45:56 +0200310 }
311 } else {
312 fprintf(stderr, "ERROR: block index %" PRIu32
313 " too large, is %" PRIu32 "\n", block, bmap_entry);
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200314 res->corruptions++;
Stefan Weil9aebd982009-07-31 21:45:56 +0200315 }
316 }
317 }
318 if (blocks_allocated != s->header.blocks_allocated) {
319 fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32
320 ", should be %" PRIu32 "\n",
321 blocks_allocated, s->header.blocks_allocated);
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200322 res->corruptions++;
Stefan Weil9aebd982009-07-31 21:45:56 +0200323 }
324
Anthony Liguori7267c092011-08-20 22:09:37 -0500325 g_free(bmap);
Stefan Weil9aebd982009-07-31 21:45:56 +0200326
Kevin Wolf9ac228e2010-06-29 12:37:54 +0200327 return 0;
Stefan Weil9aebd982009-07-31 21:45:56 +0200328}
329
Emanuele Giuseppe Esposito3d47eb02023-01-13 21:42:08 +0100330static int coroutine_fn
331vdi_co_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
Stefan Weil9aebd982009-07-31 21:45:56 +0200332{
Emanuele Giuseppe Esposito3d47eb02023-01-13 21:42:08 +0100333 /* TODO: vdi_co_get_info would be needed for machine snapshots.
Stefan Weil9aebd982009-07-31 21:45:56 +0200334 vm_state_offset is still missing. */
335 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
336 logout("\n");
337 bdi->cluster_size = s->block_size;
338 bdi->vm_state_offset = 0;
339 return 0;
340}
341
342static int vdi_make_empty(BlockDriverState *bs)
343{
344 /* TODO: missing code. */
345 logout("\n");
346 /* The return value for missing code must be 0, see block.c. */
347 return 0;
348}
349
350static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename)
351{
352 const VdiHeader *header = (const VdiHeader *)buf;
Jeff Codydddc7752014-07-23 17:22:59 -0400353 int ret = 0;
Stefan Weil9aebd982009-07-31 21:45:56 +0200354
355 logout("\n");
356
357 if (buf_size < sizeof(*header)) {
358 /* Header too small, no VDI. */
359 } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) {
Jeff Codydddc7752014-07-23 17:22:59 -0400360 ret = 100;
Stefan Weil9aebd982009-07-31 21:45:56 +0200361 }
362
Jeff Codydddc7752014-07-23 17:22:59 -0400363 if (ret == 0) {
Stefan Weil9aebd982009-07-31 21:45:56 +0200364 logout("no vdi image\n");
365 } else {
366 logout("%s", header->text);
367 }
368
Jeff Codydddc7752014-07-23 17:22:59 -0400369 return ret;
Stefan Weil9aebd982009-07-31 21:45:56 +0200370}
371
Max Reitz015a1032013-09-05 14:22:29 +0200372static int vdi_open(BlockDriverState *bs, QDict *options, int flags,
373 Error **errp)
Stefan Weil9aebd982009-07-31 21:45:56 +0200374{
375 BDRVVdiState *s = bs->opaque;
376 VdiHeader header;
377 size_t bmap_size;
Stefan Weil8937f822013-01-17 21:45:27 +0100378 int ret;
Peter Maydellac928b82018-12-10 11:26:48 +0000379 QemuUUID uuid_link, uuid_parent;
Stefan Weil9aebd982009-07-31 21:45:56 +0200380
Vladimir Sementsov-Ogievskiy83930782022-07-26 23:11:21 +0300381 ret = bdrv_open_file_child(NULL, options, "file", bs, errp);
382 if (ret < 0) {
383 return ret;
Kevin Wolf4e4bf5c2016-12-16 18:52:37 +0100384 }
385
Stefan Weil9aebd982009-07-31 21:45:56 +0200386 logout("\n");
387
Alberto Faria32cc71d2022-06-09 16:27:36 +0100388 ret = bdrv_pread(bs->file, 0, sizeof(header), &header, 0);
Stefan Weil8937f822013-01-17 21:45:27 +0100389 if (ret < 0) {
Stefan Weil9aebd982009-07-31 21:45:56 +0200390 goto fail;
391 }
392
393 vdi_header_to_cpu(&header);
Kevin Wolf95a14d52018-03-20 14:41:53 +0100394 if (VDI_DEBUG) {
395 vdi_header_print(&header);
396 }
Stefan Weil9aebd982009-07-31 21:45:56 +0200397
Jeff Cody63fa06d2014-03-28 11:42:24 -0400398 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
Peter Maydellac928b82018-12-10 11:26:48 +0000406 uuid_link = header.uuid_link;
407 uuid_parent = header.uuid_parent;
408
Stefan Weilf21dc3a2010-05-12 20:25:45 +0200409 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);
Max Reitze9082e42014-10-21 10:51:25 +0200414 header.disk_size = ROUND_UP(header.disk_size, SECTOR_SIZE);
Stefan Weilf21dc3a2010-05-12 20:25:45 +0200415 }
416
Stefan Weil0e87ba22013-01-17 21:45:28 +0100417 if (header.signature != VDI_SIGNATURE) {
Max Reitz521b2b52014-04-29 19:03:12 +0200418 error_setg(errp, "Image not in VDI format (bad signature %08" PRIx32
419 ")", header.signature);
Paolo Bonzini76abe402014-02-17 14:44:06 +0100420 ret = -EINVAL;
Stefan Weil0e87ba22013-01-17 21:45:28 +0100421 goto fail;
422 } else if (header.version != VDI_VERSION_1_1) {
Max Reitz521b2b52014-04-29 19:03:12 +0200423 error_setg(errp, "unsupported VDI image (version %" PRIu32 ".%" PRIu32
424 ")", header.version >> 16, header.version & 0xffff);
Stefan Weil8937f822013-01-17 21:45:27 +0100425 ret = -ENOTSUP;
Stefan Weil9aebd982009-07-31 21:45:56 +0200426 goto fail;
427 } else if (header.offset_bmap % SECTOR_SIZE != 0) {
428 /* We only support block maps which start on a sector boundary. */
Paolo Bonzini5b7aa9b2014-02-17 14:44:07 +0100429 error_setg(errp, "unsupported VDI image (unaligned block map offset "
Max Reitz521b2b52014-04-29 19:03:12 +0200430 "0x%" PRIx32 ")", header.offset_bmap);
Stefan Weil8937f822013-01-17 21:45:27 +0100431 ret = -ENOTSUP;
Stefan Weil9aebd982009-07-31 21:45:56 +0200432 goto fail;
433 } else if (header.offset_data % SECTOR_SIZE != 0) {
434 /* We only support data blocks which start on a sector boundary. */
Max Reitz521b2b52014-04-29 19:03:12 +0200435 error_setg(errp, "unsupported VDI image (unaligned data offset 0x%"
436 PRIx32 ")", header.offset_data);
Stefan Weil8937f822013-01-17 21:45:27 +0100437 ret = -ENOTSUP;
Stefan Weil9aebd982009-07-31 21:45:56 +0200438 goto fail;
439 } else if (header.sector_size != SECTOR_SIZE) {
Max Reitz521b2b52014-04-29 19:03:12 +0200440 error_setg(errp, "unsupported VDI image (sector size %" PRIu32
441 " is not %u)", header.sector_size, SECTOR_SIZE);
Stefan Weil8937f822013-01-17 21:45:27 +0100442 ret = -ENOTSUP;
Stefan Weil9aebd982009-07-31 21:45:56 +0200443 goto fail;
Jeff Cody63fa06d2014-03-28 11:42:24 -0400444 } else if (header.block_size != DEFAULT_CLUSTER_SIZE) {
Max Reitz521b2b52014-04-29 19:03:12 +0200445 error_setg(errp, "unsupported VDI image (block size %" PRIu32
Leonid Bloch3dd5b8f2018-11-04 18:09:28 +0000446 " is not %" PRIu32 ")",
Philippe Mathieu-Daudéf0435682018-06-25 09:41:54 -0300447 header.block_size, DEFAULT_CLUSTER_SIZE);
Stefan Weil8937f822013-01-17 21:45:27 +0100448 ret = -ENOTSUP;
Stefan Weil9aebd982009-07-31 21:45:56 +0200449 goto fail;
Stefan Weilf21dc3a2010-05-12 20:25:45 +0200450 } else if (header.disk_size >
451 (uint64_t)header.blocks_in_image * header.block_size) {
Paolo Bonzini5b7aa9b2014-02-17 14:44:07 +0100452 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);
Stefan Weil8937f822013-01-17 21:45:27 +0100456 ret = -ENOTSUP;
Stefan Weil9aebd982009-07-31 21:45:56 +0200457 goto fail;
Peter Maydellac928b82018-12-10 11:26:48 +0000458 } else if (!qemu_uuid_is_null(&uuid_link)) {
Paolo Bonzini5b7aa9b2014-02-17 14:44:07 +0100459 error_setg(errp, "unsupported VDI image (non-NULL link UUID)");
Stefan Weil8937f822013-01-17 21:45:27 +0100460 ret = -ENOTSUP;
Stefan Weil9aebd982009-07-31 21:45:56 +0200461 goto fail;
Peter Maydellac928b82018-12-10 11:26:48 +0000462 } else if (!qemu_uuid_is_null(&uuid_parent)) {
Paolo Bonzini5b7aa9b2014-02-17 14:44:07 +0100463 error_setg(errp, "unsupported VDI image (non-NULL parent UUID)");
Stefan Weil8937f822013-01-17 21:45:27 +0100464 ret = -ENOTSUP;
Stefan Weil9aebd982009-07-31 21:45:56 +0200465 goto fail;
Jeff Cody63fa06d2014-03-28 11:42:24 -0400466 } 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;
Stefan Weil9aebd982009-07-31 21:45:56 +0200472 }
473
474 bs->total_sectors = header.disk_size / SECTOR_SIZE;
475
476 s->block_size = header.block_size;
Stefan Weil9aebd982009-07-31 21:45:56 +0200477 s->bmap_sector = header.offset_bmap / SECTOR_SIZE;
478 s->header = header;
479
480 bmap_size = header.blocks_in_image * sizeof(uint32_t);
Max Reitze9082e42014-10-21 10:51:25 +0200481 bmap_size = DIV_ROUND_UP(bmap_size, SECTOR_SIZE);
Kevin Wolf9a4f4c32015-06-16 14:19:22 +0200482 s->bmap = qemu_try_blockalign(bs->file->bs, bmap_size * SECTOR_SIZE);
Kevin Wolf17cce732014-05-20 13:25:43 +0200483 if (s->bmap == NULL) {
484 ret = -ENOMEM;
485 goto fail;
486 }
487
Alberto Faria32cc71d2022-06-09 16:27:36 +0100488 ret = bdrv_pread(bs->file, header.offset_bmap, bmap_size * SECTOR_SIZE,
489 s->bmap, 0);
Stefan Weil8937f822013-01-17 21:45:27 +0100490 if (ret < 0) {
Stefan Weil9aebd982009-07-31 21:45:56 +0200491 goto fail_free_bmap;
492 }
493
Kevin Wolffc9d1062011-11-22 16:46:26 +0100494 /* Disable migration when vdi images are used */
Alberto Garcia81e5f782015-04-08 12:29:19 +0300495 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));
Markus Armbruster386f6c02020-07-07 18:06:10 +0200498 ret = migrate_add_blocker(s->migration_blocker, errp);
499 if (ret < 0) {
Ashijeet Acharyafe44dc92017-01-16 17:01:53 +0530500 error_free(s->migration_blocker);
501 goto fail_free_bmap;
502 }
Kevin Wolffc9d1062011-11-22 16:46:26 +0100503
Paolo Bonzini1e886632017-06-29 15:27:41 +0200504 qemu_co_rwlock_init(&s->bmap_lock);
Max Reitzf0ab6f12015-02-27 14:54:39 -0500505
Stefan Weil9aebd982009-07-31 21:45:56 +0200506 return 0;
507
508 fail_free_bmap:
Kevin Wolf17cce732014-05-20 13:25:43 +0200509 qemu_vfree(s->bmap);
Stefan Weil9aebd982009-07-31 21:45:56 +0200510
511 fail:
Stefan Weil8937f822013-01-17 21:45:27 +0100512 return ret;
Stefan Weil9aebd982009-07-31 21:45:56 +0200513}
514
Jeff Codyecfe2bb2012-09-20 15:13:32 -0400515static int vdi_reopen_prepare(BDRVReopenState *state,
516 BlockReopenQueue *queue, Error **errp)
517{
518 return 0;
519}
520
Eric Blake67635f62018-02-13 14:26:57 -0600521static int coroutine_fn vdi_co_block_status(BlockDriverState *bs,
522 bool want_zero,
523 int64_t offset, int64_t bytes,
524 int64_t *pnum, int64_t *map,
525 BlockDriverState **file)
Stefan Weil9aebd982009-07-31 21:45:56 +0200526{
Stefan Weil9aebd982009-07-31 21:45:56 +0200527 BDRVVdiState *s = (BDRVVdiState *)bs->opaque;
Eric Blake67635f62018-02-13 14:26:57 -0600528 size_t bmap_index = offset / s->block_size;
529 size_t index_in_block = offset % s->block_size;
Stefan Weil9aebd982009-07-31 21:45:56 +0200530 uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]);
Paolo Bonzini4bc74be2013-09-04 19:00:30 +0200531 int result;
532
Eric Blake67635f62018-02-13 14:26:57 -0600533 logout("%p, %" PRId64 ", %" PRId64 ", %p\n", bs, offset, bytes, pnum);
534 *pnum = MIN(s->block_size - index_in_block, bytes);
Paolo Bonzini4bc74be2013-09-04 19:00:30 +0200535 result = VDI_IS_ALLOCATED(bmap_entry);
536 if (!result) {
Vladimir Sementsov-Ogievskiy2ea03322020-05-28 12:43:58 +0300537 return BDRV_BLOCK_ZERO;
Paolo Bonzini4bc74be2013-09-04 19:00:30 +0200538 }
539
Eric Blake67635f62018-02-13 14:26:57 -0600540 *map = s->header.offset_data + (uint64_t)bmap_entry * s->block_size +
541 index_in_block;
Fam Zheng8bfb1372016-01-26 11:58:56 +0800542 *file = bs->file->bs;
Max Reitzad6434d2019-07-25 17:55:10 +0200543 return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID |
544 (s->header.image_type == VDI_TYPE_STATIC ? BDRV_BLOCK_RECURSE : 0);
Stefan Weil9aebd982009-07-31 21:45:56 +0200545}
546
Kevin Wolfb9b10c32023-02-03 16:21:50 +0100547static int coroutine_fn GRAPH_RDLOCK
Vladimir Sementsov-Ogievskiyf7ef38d2021-09-03 13:27:59 +0300548vdi_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
549 QEMUIOVector *qiov, BdrvRequestFlags flags)
Stefan Weil9aebd982009-07-31 21:45:56 +0200550{
Stefan Weil9aebd982009-07-31 21:45:56 +0200551 BDRVVdiState *s = bs->opaque;
Kevin Wolf0865bb62016-04-25 16:22:39 +0200552 QEMUIOVector local_qiov;
Stefan Weil9aebd982009-07-31 21:45:56 +0200553 uint32_t bmap_entry;
554 uint32_t block_index;
Kevin Wolf0865bb62016-04-25 16:22:39 +0200555 uint32_t offset_in_block;
556 uint32_t n_bytes;
557 uint64_t bytes_done = 0;
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100558 int ret = 0;
Paolo Bonzini4de659e2012-03-19 18:07:47 +0100559
560 logout("\n");
Stefan Weil9aebd982009-07-31 21:45:56 +0200561
Kevin Wolf0865bb62016-04-25 16:22:39 +0200562 qemu_iovec_init(&local_qiov, qiov->niov);
Stefan Weil9aebd982009-07-31 21:45:56 +0200563
Kevin Wolf0865bb62016-04-25 16:22:39 +0200564 while (ret >= 0 && bytes > 0) {
565 block_index = offset / s->block_size;
566 offset_in_block = offset % s->block_size;
567 n_bytes = MIN(bytes, s->block_size - offset_in_block);
568
569 logout("will read %u bytes starting at offset %" PRIu64 "\n",
570 n_bytes, offset);
Stefan Weil9aebd982009-07-31 21:45:56 +0200571
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100572 /* prepare next AIO request */
Paolo Bonzini1e886632017-06-29 15:27:41 +0200573 qemu_co_rwlock_rdlock(&s->bmap_lock);
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100574 bmap_entry = le32_to_cpu(s->bmap[block_index]);
Paolo Bonzini1e886632017-06-29 15:27:41 +0200575 qemu_co_rwlock_unlock(&s->bmap_lock);
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100576 if (!VDI_IS_ALLOCATED(bmap_entry)) {
577 /* Block not allocated, return zeros, no need to wait. */
Kevin Wolf0865bb62016-04-25 16:22:39 +0200578 qemu_iovec_memset(qiov, bytes_done, 0, n_bytes);
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100579 ret = 0;
580 } else {
Kevin Wolf0865bb62016-04-25 16:22:39 +0200581 uint64_t data_offset = s->header.offset_data +
582 (uint64_t)bmap_entry * s->block_size +
583 offset_in_block;
Paolo Bonzini0c7bfc32012-03-19 18:07:46 +0100584
Kevin Wolf0865bb62016-04-25 16:22:39 +0200585 qemu_iovec_reset(&local_qiov);
586 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
587
Kevin Wolfa03ef882016-06-20 21:31:46 +0200588 ret = bdrv_co_preadv(bs->file, data_offset, n_bytes,
Kevin Wolf0865bb62016-04-25 16:22:39 +0200589 &local_qiov, 0);
590 }
591 logout("%u bytes read\n", n_bytes);
592
593 bytes -= n_bytes;
594 offset += n_bytes;
595 bytes_done += n_bytes;
Paolo Bonzini3d46a752012-03-19 18:07:45 +0100596 }
597
Kevin Wolf0865bb62016-04-25 16:22:39 +0200598 qemu_iovec_destroy(&local_qiov);
599
Paolo Bonzini3d46a752012-03-19 18:07:45 +0100600 return ret;
Stefan Weil9aebd982009-07-31 21:45:56 +0200601}
602
Kevin Wolfb9b10c32023-02-03 16:21:50 +0100603static int coroutine_fn GRAPH_RDLOCK
Vladimir Sementsov-Ogievskiye75abed2021-09-03 13:28:00 +0300604vdi_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
605 QEMUIOVector *qiov, BdrvRequestFlags flags)
Stefan Weil9aebd982009-07-31 21:45:56 +0200606{
Stefan Weil9aebd982009-07-31 21:45:56 +0200607 BDRVVdiState *s = bs->opaque;
Kevin Wolffde9d562016-04-25 16:22:39 +0200608 QEMUIOVector local_qiov;
Stefan Weil9aebd982009-07-31 21:45:56 +0200609 uint32_t bmap_entry;
610 uint32_t block_index;
Kevin Wolffde9d562016-04-25 16:22:39 +0200611 uint32_t offset_in_block;
612 uint32_t n_bytes;
Paolo Bonzini1e886632017-06-29 15:27:41 +0200613 uint64_t data_offset;
Paolo Bonzinibfc45fc2012-03-19 18:07:48 +0100614 uint32_t bmap_first = VDI_UNALLOCATED;
615 uint32_t bmap_last = VDI_UNALLOCATED;
Paolo Bonzinibfc45fc2012-03-19 18:07:48 +0100616 uint8_t *block = NULL;
Kevin Wolffde9d562016-04-25 16:22:39 +0200617 uint64_t bytes_done = 0;
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100618 int ret = 0;
Paolo Bonzini4de659e2012-03-19 18:07:47 +0100619
620 logout("\n");
Stefan Weil9aebd982009-07-31 21:45:56 +0200621
Kevin Wolffde9d562016-04-25 16:22:39 +0200622 qemu_iovec_init(&local_qiov, qiov->niov);
Paolo Bonzini0c7bfc32012-03-19 18:07:46 +0100623
Kevin Wolffde9d562016-04-25 16:22:39 +0200624 while (ret >= 0 && bytes > 0) {
625 block_index = offset / s->block_size;
626 offset_in_block = offset % s->block_size;
627 n_bytes = MIN(bytes, s->block_size - offset_in_block);
628
629 logout("will write %u bytes starting at offset %" PRIu64 "\n",
630 n_bytes, offset);
Paolo Bonzini0c7bfc32012-03-19 18:07:46 +0100631
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100632 /* prepare next AIO request */
Paolo Bonzini1e886632017-06-29 15:27:41 +0200633 qemu_co_rwlock_rdlock(&s->bmap_lock);
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100634 bmap_entry = le32_to_cpu(s->bmap[block_index]);
635 if (!VDI_IS_ALLOCATED(bmap_entry)) {
636 /* Allocate new block and write to it. */
Kevin Wolffde9d562016-04-25 16:22:39 +0200637 uint64_t data_offset;
Paolo Bonzini1e886632017-06-29 15:27:41 +0200638 qemu_co_rwlock_upgrade(&s->bmap_lock);
639 bmap_entry = le32_to_cpu(s->bmap[block_index]);
640 if (VDI_IS_ALLOCATED(bmap_entry)) {
641 /* A concurrent allocation did the work for us. */
642 qemu_co_rwlock_downgrade(&s->bmap_lock);
643 goto nonallocating_write;
644 }
645
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100646 bmap_entry = s->header.blocks_allocated;
647 s->bmap[block_index] = cpu_to_le32(bmap_entry);
648 s->header.blocks_allocated++;
Kevin Wolffde9d562016-04-25 16:22:39 +0200649 data_offset = s->header.offset_data +
650 (uint64_t)bmap_entry * s->block_size;
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100651 if (block == NULL) {
652 block = g_malloc(s->block_size);
653 bmap_first = block_index;
654 }
655 bmap_last = block_index;
656 /* Copy data to be written to new block and zero unused parts. */
Kevin Wolffde9d562016-04-25 16:22:39 +0200657 memset(block, 0, offset_in_block);
658 qemu_iovec_to_buf(qiov, bytes_done, block + offset_in_block,
659 n_bytes);
660 memset(block + offset_in_block + n_bytes, 0,
661 s->block_size - n_bytes - offset_in_block);
Max Reitzf0ab6f12015-02-27 14:54:39 -0500662
Paolo Bonzini1e886632017-06-29 15:27:41 +0200663 /* Write the new block under CoRwLock write-side protection,
664 * so this full-cluster write does not overlap a partial write
665 * of the same cluster, issued from the "else" branch.
666 */
Alberto Faria3f653022022-10-13 14:37:08 +0200667 ret = bdrv_co_pwrite(bs->file, data_offset, s->block_size, block,
668 0);
Paolo Bonzini1e886632017-06-29 15:27:41 +0200669 qemu_co_rwlock_unlock(&s->bmap_lock);
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100670 } else {
Paolo Bonzini1e886632017-06-29 15:27:41 +0200671nonallocating_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);
Kevin Wolffde9d562016-04-25 16:22:39 +0200676
677 qemu_iovec_reset(&local_qiov);
678 qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes);
679
Kevin Wolfa03ef882016-06-20 21:31:46 +0200680 ret = bdrv_co_pwritev(bs->file, data_offset, n_bytes,
Kevin Wolffde9d562016-04-25 16:22:39 +0200681 &local_qiov, 0);
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100682 }
683
Kevin Wolffde9d562016-04-25 16:22:39 +0200684 bytes -= n_bytes;
685 offset += n_bytes;
686 bytes_done += n_bytes;
Paolo Bonzinieb9566d2012-03-19 18:07:51 +0100687
Kevin Wolffde9d562016-04-25 16:22:39 +0200688 logout("%u bytes written\n", n_bytes);
Paolo Bonzini3d46a752012-03-19 18:07:45 +0100689 }
Stefan Weil9aebd982009-07-31 21:45:56 +0200690
Kevin Wolffde9d562016-04-25 16:22:39 +0200691 qemu_iovec_destroy(&local_qiov);
692
Paolo Bonzini0c7bfc32012-03-19 18:07:46 +0100693 logout("finished data write\n");
Paolo Bonzini4eea78e2012-03-19 18:07:50 +0100694 if (ret < 0) {
David Edmondson574b8302021-03-25 12:29:36 +0100695 g_free(block);
Paolo Bonzini4eea78e2012-03-19 18:07:50 +0100696 return ret;
697 }
698
699 if (block) {
700 /* One or more new blocks were allocated. */
David Edmondson07ee2ab2021-03-25 12:29:37 +0100701 VdiHeader *header;
Paolo Bonzini4eea78e2012-03-19 18:07:50 +0100702 uint8_t *base;
703 uint64_t offset;
Kevin Wolffde9d562016-04-25 16:22:39 +0200704 uint32_t n_sectors;
Paolo Bonzini4eea78e2012-03-19 18:07:50 +0100705
David Edmondson07ee2ab2021-03-25 12:29:37 +0100706 g_free(block);
707 header = g_malloc(sizeof(*header));
708
Paolo Bonzini4eea78e2012-03-19 18:07:50 +0100709 logout("now writing modified header\n");
710 assert(VDI_IS_ALLOCATED(bmap_first));
711 *header = s->header;
712 vdi_header_to_le(header);
Alberto Faria3f653022022-10-13 14:37:08 +0200713 ret = bdrv_co_pwrite(bs->file, 0, sizeof(*header), header, 0);
David Edmondson07ee2ab2021-03-25 12:29:37 +0100714 g_free(header);
Paolo Bonzini4eea78e2012-03-19 18:07:50 +0100715
716 if (ret < 0) {
717 return ret;
Paolo Bonzini0c7bfc32012-03-19 18:07:46 +0100718 }
Paolo Bonzini4eea78e2012-03-19 18:07:50 +0100719
720 logout("now writing modified block map entry %u...%u\n",
721 bmap_first, bmap_last);
722 /* Write modified sectors from block map. */
723 bmap_first /= (SECTOR_SIZE / sizeof(uint32_t));
724 bmap_last /= (SECTOR_SIZE / sizeof(uint32_t));
725 n_sectors = bmap_last - bmap_first + 1;
726 offset = s->bmap_sector + bmap_first;
727 base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE;
728 logout("will write %u block map sectors starting from entry %u\n",
729 n_sectors, bmap_first);
Alberto Faria3f653022022-10-13 14:37:08 +0200730 ret = bdrv_co_pwrite(bs->file, offset * SECTOR_SIZE,
731 n_sectors * SECTOR_SIZE, base, 0);
Paolo Bonzini0c7bfc32012-03-19 18:07:46 +0100732 }
733
Alberto Faria353a5d82022-06-09 16:27:37 +0100734 return ret;
Stefan Weil9aebd982009-07-31 21:45:56 +0200735}
736
Max Reitze3810572018-03-12 17:55:28 +0100737static int coroutine_fn vdi_co_do_create(BlockdevCreateOptions *create_options,
Max Reitz49858b52018-03-12 17:55:26 +0100738 size_t block_size, Error **errp)
Stefan Weil9aebd982009-07-31 21:45:56 +0200739{
Max Reitze3810572018-03-12 17:55:28 +0100740 BlockdevCreateOptionsVdi *vdi_opts;
Jeff Codydddc7752014-07-23 17:22:59 -0400741 int ret = 0;
Stefan Weil9aebd982009-07-31 21:45:56 +0200742 uint64_t bytes = 0;
743 uint32_t blocks;
Kevin Wolf61fa6482018-03-20 15:08:00 +0100744 uint32_t image_type;
Stefan Weil9aebd982009-07-31 21:45:56 +0200745 VdiHeader header;
746 size_t i;
747 size_t bmap_size;
Jeff Cody70747862014-07-23 17:22:58 -0400748 int64_t offset = 0;
Max Reitzec73f062018-03-12 17:55:27 +0100749 BlockDriverState *bs_file = NULL;
Kevin Wolfa08f0c32016-03-08 15:57:05 +0100750 BlockBackend *blk = NULL;
Jeff Cody70747862014-07-23 17:22:58 -0400751 uint32_t *bmap = NULL;
Peter Maydellac928b82018-12-10 11:26:48 +0000752 QemuUUID uuid;
Stefan Weil9aebd982009-07-31 21:45:56 +0200753
Max Reitze3810572018-03-12 17:55:28 +0100754 assert(create_options->driver == BLOCKDEV_DRIVER_VDI);
755 vdi_opts = &create_options->u.vdi;
756
Stefan Weil9aebd982009-07-31 21:45:56 +0200757 logout("\n");
758
Kevin Wolfda232482018-03-09 19:53:19 +0100759 /* Validate options and set default values */
Max Reitz49858b52018-03-12 17:55:26 +0100760 bytes = vdi_opts->size;
Kevin Wolf61fa6482018-03-20 15:08:00 +0100761
762 if (!vdi_opts->has_preallocation) {
763 vdi_opts->preallocation = PREALLOC_MODE_OFF;
Stefan Weil9aebd982009-07-31 21:45:56 +0200764 }
Kevin Wolf61fa6482018-03-20 15:08:00 +0100765 switch (vdi_opts->preallocation) {
766 case PREALLOC_MODE_OFF:
767 image_type = VDI_TYPE_DYNAMIC;
768 break;
769 case PREALLOC_MODE_METADATA:
770 image_type = VDI_TYPE_STATIC;
771 break;
772 default:
773 error_setg(errp, "Preallocation mode not supported for vdi");
774 return -EINVAL;
775 }
776
Max Reitz49858b52018-03-12 17:55:26 +0100777#ifndef CONFIG_VDI_STATIC_IMAGE
778 if (image_type == VDI_TYPE_STATIC) {
779 ret = -ENOTSUP;
780 error_setg(errp, "Statically allocated images cannot be created in "
781 "this build");
782 goto exit;
783 }
784#endif
785#ifndef CONFIG_VDI_BLOCK_SIZE
786 if (block_size != DEFAULT_CLUSTER_SIZE) {
787 ret = -ENOTSUP;
788 error_setg(errp,
789 "A non-default cluster size is not supported in this build");
790 goto exit;
791 }
Chunyan Liu004b7f22014-06-05 17:21:07 +0800792#endif
Stefan Weil9aebd982009-07-31 21:45:56 +0200793
Jeff Cody63fa06d2014-03-28 11:42:24 -0400794 if (bytes > VDI_DISK_SIZE_MAX) {
Jeff Codydddc7752014-07-23 17:22:59 -0400795 ret = -ENOTSUP;
Jeff Cody63fa06d2014-03-28 11:42:24 -0400796 error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64
797 ", max supported is 0x%" PRIx64 ")",
798 bytes, VDI_DISK_SIZE_MAX);
799 goto exit;
800 }
801
Kevin Wolfda232482018-03-09 19:53:19 +0100802 /* Create BlockBackend to write to the image */
Kevin Wolf13dd6322023-01-26 18:24:27 +0100803 bs_file = bdrv_co_open_blockdev_ref(vdi_opts->file, errp);
Max Reitzec73f062018-03-12 17:55:27 +0100804 if (!bs_file) {
805 ret = -EIO;
Jeff Cody63fa06d2014-03-28 11:42:24 -0400806 goto exit;
Stefan Weil9aebd982009-07-31 21:45:56 +0200807 }
Kevin Wolfa08f0c32016-03-08 15:57:05 +0100808
Kevin Wolf13dd6322023-01-26 18:24:27 +0100809 blk = blk_co_new_with_bs(bs_file, BLK_PERM_WRITE | BLK_PERM_RESIZE,
810 BLK_PERM_ALL, errp);
Eric Blakea3aeeab2020-04-28 14:26:46 -0500811 if (!blk) {
812 ret = -EPERM;
Jeff Cody70747862014-07-23 17:22:58 -0400813 goto exit;
Chunyan Liu4ab15592014-06-30 14:29:58 +0800814 }
815
Kevin Wolfa08f0c32016-03-08 15:57:05 +0100816 blk_set_allow_write_beyond_eof(blk, true);
817
Stefan Weilf21dc3a2010-05-12 20:25:45 +0200818 /* We need enough blocks to store the given disk size,
819 so always round up. */
Max Reitze9082e42014-10-21 10:51:25 +0200820 blocks = DIV_ROUND_UP(bytes, block_size);
Stefan Weilf21dc3a2010-05-12 20:25:45 +0200821
Stefan Weil9aebd982009-07-31 21:45:56 +0200822 bmap_size = blocks * sizeof(uint32_t);
Max Reitze9082e42014-10-21 10:51:25 +0200823 bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE);
Stefan Weil9aebd982009-07-31 21:45:56 +0200824
825 memset(&header, 0, sizeof(header));
Blue Swirl1786dc12009-08-15 11:33:58 +0000826 pstrcpy(header.text, sizeof(header.text), VDI_TEXT);
Stefan Weil9aebd982009-07-31 21:45:56 +0200827 header.signature = VDI_SIGNATURE;
828 header.version = VDI_VERSION_1_1;
829 header.header_size = 0x180;
830 header.image_type = image_type;
831 header.offset_bmap = 0x200;
832 header.offset_data = 0x200 + bmap_size;
833 header.sector_size = SECTOR_SIZE;
834 header.disk_size = bytes;
835 header.block_size = block_size;
836 header.blocks_in_image = blocks;
Stefan Weil6eea90e2009-08-14 21:50:02 +0200837 if (image_type == VDI_TYPE_STATIC) {
838 header.blocks_allocated = blocks;
839 }
Peter Maydellac928b82018-12-10 11:26:48 +0000840 qemu_uuid_generate(&uuid);
841 header.uuid_image = uuid;
842 qemu_uuid_generate(&uuid);
843 header.uuid_last_snap = uuid;
Stefan Weil9aebd982009-07-31 21:45:56 +0200844 /* There is no need to set header.uuid_link or header.uuid_parent here. */
Kevin Wolf95a14d52018-03-20 14:41:53 +0100845 if (VDI_DEBUG) {
846 vdi_header_print(&header);
847 }
Stefan Weil9aebd982009-07-31 21:45:56 +0200848 vdi_header_to_le(&header);
Alberto Faria3f653022022-10-13 14:37:08 +0200849 ret = blk_co_pwrite(blk, offset, sizeof(header), &header, 0);
Jeff Codydddc7752014-07-23 17:22:59 -0400850 if (ret < 0) {
Max Reitzec73f062018-03-12 17:55:27 +0100851 error_setg(errp, "Error writing header");
Jeff Cody70747862014-07-23 17:22:58 -0400852 goto exit;
Stefan Weil9aebd982009-07-31 21:45:56 +0200853 }
Jeff Cody70747862014-07-23 17:22:58 -0400854 offset += sizeof(header);
Stefan Weil9aebd982009-07-31 21:45:56 +0200855
Stefan Weilb76b6e92010-05-06 20:53:47 +0200856 if (bmap_size > 0) {
Kevin Wolf17cce732014-05-20 13:25:43 +0200857 bmap = g_try_malloc0(bmap_size);
858 if (bmap == NULL) {
859 ret = -ENOMEM;
860 error_setg(errp, "Could not allocate bmap");
861 goto exit;
862 }
Stefan Weil514f21a2012-08-17 15:23:24 +0200863 for (i = 0; i < blocks; i++) {
864 if (image_type == VDI_TYPE_STATIC) {
865 bmap[i] = i;
866 } else {
867 bmap[i] = VDI_UNALLOCATED;
868 }
Stefan Weil9aebd982009-07-31 21:45:56 +0200869 }
Alberto Faria3f653022022-10-13 14:37:08 +0200870 ret = blk_co_pwrite(blk, offset, bmap_size, bmap, 0);
Jeff Codydddc7752014-07-23 17:22:59 -0400871 if (ret < 0) {
Max Reitzec73f062018-03-12 17:55:27 +0100872 error_setg(errp, "Error writing bmap");
Jeff Cody70747862014-07-23 17:22:58 -0400873 goto exit;
Stefan Weil514f21a2012-08-17 15:23:24 +0200874 }
Jeff Cody70747862014-07-23 17:22:58 -0400875 offset += bmap_size;
Stefan Weil9aebd982009-07-31 21:45:56 +0200876 }
Stefan Weil514f21a2012-08-17 15:23:24 +0200877
Stefan Weil9aebd982009-07-31 21:45:56 +0200878 if (image_type == VDI_TYPE_STATIC) {
Alberto Faria3f653022022-10-13 14:37:08 +0200879 ret = blk_co_truncate(blk, offset + blocks * block_size, false,
880 PREALLOC_MODE_OFF, 0, errp);
Jeff Codydddc7752014-07-23 17:22:59 -0400881 if (ret < 0) {
Max Reitzec73f062018-03-12 17:55:27 +0100882 error_prepend(errp, "Failed to statically allocate file");
Jeff Cody70747862014-07-23 17:22:58 -0400883 goto exit;
Stefan Weil9aebd982009-07-31 21:45:56 +0200884 }
885 }
886
Kevin Wolf53618dd2018-05-25 14:48:16 +0200887 ret = 0;
Jeff Cody63fa06d2014-03-28 11:42:24 -0400888exit:
Kevin Wolfa08f0c32016-03-08 15:57:05 +0100889 blk_unref(blk);
Max Reitzec73f062018-03-12 17:55:27 +0100890 bdrv_unref(bs_file);
Jeff Cody70747862014-07-23 17:22:58 -0400891 g_free(bmap);
Jeff Codydddc7752014-07-23 17:22:59 -0400892 return ret;
Stefan Weil9aebd982009-07-31 21:45:56 +0200893}
894
Max Reitze3810572018-03-12 17:55:28 +0100895static int coroutine_fn vdi_co_create(BlockdevCreateOptions *create_options,
896 Error **errp)
897{
898 return vdi_co_do_create(create_options, DEFAULT_CLUSTER_SIZE, errp);
899}
900
Kevin Wolf4ec8df02023-02-03 16:21:55 +0100901static int coroutine_fn GRAPH_RDLOCK
902vdi_co_create_opts(BlockDriver *drv, const char *filename,
903 QemuOpts *opts, Error **errp)
Max Reitz49858b52018-03-12 17:55:26 +0100904{
905 QDict *qdict = NULL;
Max Reitze3810572018-03-12 17:55:28 +0100906 BlockdevCreateOptions *create_options = NULL;
Max Reitzec73f062018-03-12 17:55:27 +0100907 BlockDriverState *bs_file = NULL;
Max Reitz49858b52018-03-12 17:55:26 +0100908 uint64_t block_size = DEFAULT_CLUSTER_SIZE;
Kevin Wolf61fa6482018-03-20 15:08:00 +0100909 bool is_static = false;
Max Reitz49858b52018-03-12 17:55:26 +0100910 Visitor *v;
Max Reitz49858b52018-03-12 17:55:26 +0100911 int ret;
912
Kevin Wolfda232482018-03-09 19:53:19 +0100913 /* Parse options and convert legacy syntax.
914 *
915 * Since CONFIG_VDI_BLOCK_SIZE is disabled by default,
Max Reitz49858b52018-03-12 17:55:26 +0100916 * cluster-size is not part of the QAPI schema; therefore we have
917 * to parse it before creating the QAPI object. */
918#if defined(CONFIG_VDI_BLOCK_SIZE)
919 block_size = qemu_opt_get_size_del(opts,
920 BLOCK_OPT_CLUSTER_SIZE,
921 DEFAULT_CLUSTER_SIZE);
922 if (block_size < BDRV_SECTOR_SIZE || block_size > UINT32_MAX ||
923 !is_power_of_2(block_size))
924 {
925 error_setg(errp, "Invalid cluster size");
926 ret = -EINVAL;
927 goto done;
928 }
929#endif
Kevin Wolf61fa6482018-03-20 15:08:00 +0100930 if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) {
931 is_static = true;
932 }
Max Reitz49858b52018-03-12 17:55:26 +0100933
934 qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, true);
935
Kevin Wolfda232482018-03-09 19:53:19 +0100936 /* Create and open the file (protocol layer) */
Emanuele Giuseppe Esposito2475a0d2022-11-28 09:23:31 -0500937 ret = bdrv_co_create_file(filename, opts, errp);
Max Reitzec73f062018-03-12 17:55:27 +0100938 if (ret < 0) {
939 goto done;
940 }
941
Kevin Wolf13dd6322023-01-26 18:24:27 +0100942 bs_file = bdrv_co_open(filename, NULL, NULL,
943 BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp);
Max Reitzec73f062018-03-12 17:55:27 +0100944 if (!bs_file) {
945 ret = -EIO;
946 goto done;
947 }
948
Max Reitze3810572018-03-12 17:55:28 +0100949 qdict_put_str(qdict, "driver", "vdi");
Max Reitzec73f062018-03-12 17:55:27 +0100950 qdict_put_str(qdict, "file", bs_file->node_name);
Kevin Wolf61fa6482018-03-20 15:08:00 +0100951 if (is_static) {
952 qdict_put_str(qdict, "preallocation", "metadata");
953 }
Max Reitz49858b52018-03-12 17:55:26 +0100954
955 /* Get the QAPI object */
Markus Armbrusterf8534652018-06-14 21:14:34 +0200956 v = qobject_input_visitor_new_flat_confused(qdict, errp);
957 if (!v) {
958 ret = -EINVAL;
959 goto done;
960 }
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200961 visit_type_BlockdevCreateOptions(v, NULL, &create_options, errp);
Max Reitz49858b52018-03-12 17:55:26 +0100962 visit_free(v);
Markus Armbrusterb11a0932020-07-07 18:06:07 +0200963 if (!create_options) {
Max Reitz49858b52018-03-12 17:55:26 +0100964 ret = -EINVAL;
965 goto done;
966 }
967
Kevin Wolfda232482018-03-09 19:53:19 +0100968 /* Silently round up size */
Max Reitze3810572018-03-12 17:55:28 +0100969 assert(create_options->driver == BLOCKDEV_DRIVER_VDI);
970 create_options->u.vdi.size = ROUND_UP(create_options->u.vdi.size,
971 BDRV_SECTOR_SIZE);
Max Reitz49858b52018-03-12 17:55:26 +0100972
Kevin Wolfda232482018-03-09 19:53:19 +0100973 /* Create the vdi image (format layer) */
Max Reitzec73f062018-03-12 17:55:27 +0100974 ret = vdi_co_do_create(create_options, block_size, errp);
Max Reitz49858b52018-03-12 17:55:26 +0100975done:
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200976 qobject_unref(qdict);
Max Reitze3810572018-03-12 17:55:28 +0100977 qapi_free_BlockdevCreateOptions(create_options);
Max Reitzec73f062018-03-12 17:55:27 +0100978 bdrv_unref(bs_file);
Max Reitz49858b52018-03-12 17:55:26 +0100979 return ret;
980}
981
Stefan Weil9aebd982009-07-31 21:45:56 +0200982static void vdi_close(BlockDriverState *bs)
983{
Kevin Wolffc9d1062011-11-22 16:46:26 +0100984 BDRVVdiState *s = bs->opaque;
Kevin Wolf6ac5f382011-11-22 16:57:34 +0100985
Kevin Wolf17cce732014-05-20 13:25:43 +0200986 qemu_vfree(s->bmap);
Kevin Wolf6ac5f382011-11-22 16:57:34 +0100987
Kevin Wolffc9d1062011-11-22 16:46:26 +0100988 migrate_del_blocker(s->migration_blocker);
989 error_free(s->migration_blocker);
Stefan Weil9aebd982009-07-31 21:45:56 +0200990}
991
Max Reitz0a28bf22019-07-24 19:12:35 +0200992static int vdi_has_zero_init(BlockDriverState *bs)
993{
994 BDRVVdiState *s = bs->opaque;
995
996 if (s->header.image_type == VDI_TYPE_STATIC) {
997 return bdrv_has_zero_init(bs->file->bs);
998 } else {
999 return 1;
1000 }
1001}
1002
Chunyan Liu004b7f22014-06-05 17:21:07 +08001003static QemuOptsList vdi_create_opts = {
1004 .name = "vdi-create-opts",
1005 .head = QTAILQ_HEAD_INITIALIZER(vdi_create_opts.head),
1006 .desc = {
1007 {
1008 .name = BLOCK_OPT_SIZE,
1009 .type = QEMU_OPT_SIZE,
1010 .help = "Virtual disk size"
1011 },
Stefan Weil9aebd982009-07-31 21:45:56 +02001012#if defined(CONFIG_VDI_BLOCK_SIZE)
Chunyan Liu004b7f22014-06-05 17:21:07 +08001013 {
1014 .name = BLOCK_OPT_CLUSTER_SIZE,
1015 .type = QEMU_OPT_SIZE,
1016 .help = "VDI cluster (block) size",
1017 .def_value_str = stringify(DEFAULT_CLUSTER_SIZE)
1018 },
Stefan Weil9aebd982009-07-31 21:45:56 +02001019#endif
1020#if defined(CONFIG_VDI_STATIC_IMAGE)
Chunyan Liu004b7f22014-06-05 17:21:07 +08001021 {
1022 .name = BLOCK_OPT_STATIC,
1023 .type = QEMU_OPT_BOOL,
1024 .help = "VDI static (pre-allocated) image",
1025 .def_value_str = "off"
1026 },
Stefan Weil9aebd982009-07-31 21:45:56 +02001027#endif
Chunyan Liu004b7f22014-06-05 17:21:07 +08001028 /* TODO: An additional option to set UUID values might be useful. */
1029 { /* end of list */ }
1030 }
Stefan Weil9aebd982009-07-31 21:45:56 +02001031};
1032
1033static BlockDriver bdrv_vdi = {
1034 .format_name = "vdi",
1035 .instance_size = sizeof(BDRVVdiState),
1036 .bdrv_probe = vdi_probe,
1037 .bdrv_open = vdi_open,
1038 .bdrv_close = vdi_close,
Jeff Codyecfe2bb2012-09-20 15:13:32 -04001039 .bdrv_reopen_prepare = vdi_reopen_prepare,
Max Reitz69dca432020-05-13 13:05:39 +02001040 .bdrv_child_perm = bdrv_default_perms,
Max Reitze3810572018-03-12 17:55:28 +01001041 .bdrv_co_create = vdi_co_create,
Kevin Wolfda232482018-03-09 19:53:19 +01001042 .bdrv_co_create_opts = vdi_co_create_opts,
Max Reitz0a28bf22019-07-24 19:12:35 +02001043 .bdrv_has_zero_init = vdi_has_zero_init,
Eric Blake67635f62018-02-13 14:26:57 -06001044 .bdrv_co_block_status = vdi_co_block_status,
Stefan Weil9aebd982009-07-31 21:45:56 +02001045 .bdrv_make_empty = vdi_make_empty,
1046
Kevin Wolf0865bb62016-04-25 16:22:39 +02001047 .bdrv_co_preadv = vdi_co_preadv,
Stefan Weil9aebd982009-07-31 21:45:56 +02001048#if defined(CONFIG_VDI_WRITE)
Kevin Wolffde9d562016-04-25 16:22:39 +02001049 .bdrv_co_pwritev = vdi_co_pwritev,
Stefan Weil9aebd982009-07-31 21:45:56 +02001050#endif
1051
Emanuele Giuseppe Esposito3d47eb02023-01-13 21:42:08 +01001052 .bdrv_co_get_info = vdi_co_get_info,
Stefan Weil9aebd982009-07-31 21:45:56 +02001053
Max Reitzd67066d2020-05-13 13:05:12 +02001054 .is_format = true,
Chunyan Liu004b7f22014-06-05 17:21:07 +08001055 .create_opts = &vdi_create_opts,
Paolo Bonzini2fd61632018-03-01 17:36:19 +01001056 .bdrv_co_check = vdi_co_check,
Stefan Weil9aebd982009-07-31 21:45:56 +02001057};
1058
1059static void bdrv_vdi_init(void)
1060{
1061 logout("\n");
1062 bdrv_register(&bdrv_vdi);
1063}
1064
1065block_init(bdrv_vdi_init);