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