]>
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 | * | |
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" | |
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 | |
ee682d27 | 123 | #if !defined(CONFIG_UUID) |
8ba2aae3 | 124 | static inline void uuid_generate(uuid_t out) |
9aebd98a | 125 | { |
4f3669ea | 126 | memset(out, 0, sizeof(uuid_t)); |
9aebd98a SW |
127 | } |
128 | ||
8ba2aae3 | 129 | static inline int uuid_is_null(const uuid_t uu) |
9aebd98a SW |
130 | { |
131 | uuid_t null_uuid = { 0 }; | |
4f3669ea | 132 | return memcmp(uu, null_uuid, sizeof(uuid_t)) == 0; |
9aebd98a SW |
133 | } |
134 | ||
8ba2aae3 | 135 | static inline void uuid_unparse(const uuid_t uu, char *out) |
9aebd98a SW |
136 | { |
137 | snprintf(out, 37, UUID_FMT, | |
138 | uu[0], uu[1], uu[2], uu[3], uu[4], uu[5], uu[6], uu[7], | |
139 | uu[8], uu[9], uu[10], uu[11], uu[12], uu[13], uu[14], uu[15]); | |
140 | } | |
141 | #endif | |
142 | ||
9aebd98a SW |
143 | typedef struct { |
144 | char text[0x40]; | |
145 | uint32_t signature; | |
146 | uint32_t version; | |
147 | uint32_t header_size; | |
148 | uint32_t image_type; | |
149 | uint32_t image_flags; | |
150 | char description[256]; | |
151 | uint32_t offset_bmap; | |
152 | uint32_t offset_data; | |
153 | uint32_t cylinders; /* disk geometry, unused here */ | |
154 | uint32_t heads; /* disk geometry, unused here */ | |
155 | uint32_t sectors; /* disk geometry, unused here */ | |
156 | uint32_t sector_size; | |
157 | uint32_t unused1; | |
158 | uint64_t disk_size; | |
159 | uint32_t block_size; | |
160 | uint32_t block_extra; /* unused here */ | |
161 | uint32_t blocks_in_image; | |
162 | uint32_t blocks_allocated; | |
163 | uuid_t uuid_image; | |
164 | uuid_t uuid_last_snap; | |
165 | uuid_t uuid_link; | |
166 | uuid_t uuid_parent; | |
167 | uint64_t unused2[7]; | |
168 | } VdiHeader; | |
169 | ||
170 | typedef struct { | |
9aebd98a SW |
171 | /* The block map entries are little endian (even in memory). */ |
172 | uint32_t *bmap; | |
173 | /* Size of block (bytes). */ | |
174 | uint32_t block_size; | |
175 | /* Size of block (sectors). */ | |
176 | uint32_t block_sectors; | |
177 | /* First sector of block map. */ | |
178 | uint32_t bmap_sector; | |
4ff9786c | 179 | /* VDI header (converted to host endianness). */ |
9aebd98a | 180 | VdiHeader header; |
fc9d106c KW |
181 | |
182 | Error *migration_blocker; | |
9aebd98a SW |
183 | } BDRVVdiState; |
184 | ||
185 | /* Change UUID from little endian (IPRT = VirtualBox format) to big endian | |
186 | * format (network byte order, standard, see RFC 4122) and vice versa. | |
187 | */ | |
188 | static void uuid_convert(uuid_t uuid) | |
189 | { | |
190 | bswap32s((uint32_t *)&uuid[0]); | |
191 | bswap16s((uint16_t *)&uuid[4]); | |
192 | bswap16s((uint16_t *)&uuid[6]); | |
193 | } | |
194 | ||
195 | static void vdi_header_to_cpu(VdiHeader *header) | |
196 | { | |
197 | le32_to_cpus(&header->signature); | |
198 | le32_to_cpus(&header->version); | |
199 | le32_to_cpus(&header->header_size); | |
200 | le32_to_cpus(&header->image_type); | |
201 | le32_to_cpus(&header->image_flags); | |
202 | le32_to_cpus(&header->offset_bmap); | |
203 | le32_to_cpus(&header->offset_data); | |
204 | le32_to_cpus(&header->cylinders); | |
205 | le32_to_cpus(&header->heads); | |
206 | le32_to_cpus(&header->sectors); | |
207 | le32_to_cpus(&header->sector_size); | |
208 | le64_to_cpus(&header->disk_size); | |
209 | le32_to_cpus(&header->block_size); | |
210 | le32_to_cpus(&header->block_extra); | |
211 | le32_to_cpus(&header->blocks_in_image); | |
212 | le32_to_cpus(&header->blocks_allocated); | |
213 | uuid_convert(header->uuid_image); | |
214 | uuid_convert(header->uuid_last_snap); | |
215 | uuid_convert(header->uuid_link); | |
216 | uuid_convert(header->uuid_parent); | |
217 | } | |
218 | ||
219 | static void vdi_header_to_le(VdiHeader *header) | |
220 | { | |
221 | cpu_to_le32s(&header->signature); | |
222 | cpu_to_le32s(&header->version); | |
223 | cpu_to_le32s(&header->header_size); | |
224 | cpu_to_le32s(&header->image_type); | |
225 | cpu_to_le32s(&header->image_flags); | |
226 | cpu_to_le32s(&header->offset_bmap); | |
227 | cpu_to_le32s(&header->offset_data); | |
228 | cpu_to_le32s(&header->cylinders); | |
229 | cpu_to_le32s(&header->heads); | |
230 | cpu_to_le32s(&header->sectors); | |
231 | cpu_to_le32s(&header->sector_size); | |
232 | cpu_to_le64s(&header->disk_size); | |
233 | cpu_to_le32s(&header->block_size); | |
234 | cpu_to_le32s(&header->block_extra); | |
235 | cpu_to_le32s(&header->blocks_in_image); | |
236 | cpu_to_le32s(&header->blocks_allocated); | |
237 | cpu_to_le32s(&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 | #if defined(CONFIG_VDI_DEBUG) | |
245 | static void vdi_header_print(VdiHeader *header) | |
246 | { | |
247 | char uuid[37]; | |
248 | logout("text %s", header->text); | |
9f0470bb | 249 | logout("signature 0x%08x\n", header->signature); |
9aebd98a SW |
250 | logout("header size 0x%04x\n", header->header_size); |
251 | logout("image type 0x%04x\n", header->image_type); | |
252 | logout("image flags 0x%04x\n", header->image_flags); | |
253 | logout("description %s\n", header->description); | |
254 | logout("offset bmap 0x%04x\n", header->offset_bmap); | |
255 | logout("offset data 0x%04x\n", header->offset_data); | |
256 | logout("cylinders 0x%04x\n", header->cylinders); | |
257 | logout("heads 0x%04x\n", header->heads); | |
258 | logout("sectors 0x%04x\n", header->sectors); | |
259 | logout("sector size 0x%04x\n", header->sector_size); | |
260 | logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n", | |
261 | header->disk_size, header->disk_size / MiB); | |
262 | logout("block size 0x%04x\n", header->block_size); | |
263 | logout("block extra 0x%04x\n", header->block_extra); | |
264 | logout("blocks tot. 0x%04x\n", header->blocks_in_image); | |
265 | logout("blocks all. 0x%04x\n", header->blocks_allocated); | |
266 | uuid_unparse(header->uuid_image, uuid); | |
267 | logout("uuid image %s\n", uuid); | |
268 | uuid_unparse(header->uuid_last_snap, uuid); | |
269 | logout("uuid snap %s\n", uuid); | |
270 | uuid_unparse(header->uuid_link, uuid); | |
271 | logout("uuid link %s\n", uuid); | |
272 | uuid_unparse(header->uuid_parent, uuid); | |
273 | logout("uuid parent %s\n", uuid); | |
274 | } | |
275 | #endif | |
276 | ||
4534ff54 KW |
277 | static int vdi_check(BlockDriverState *bs, BdrvCheckResult *res, |
278 | BdrvCheckMode fix) | |
9aebd98a SW |
279 | { |
280 | /* TODO: additional checks possible. */ | |
281 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; | |
9aebd98a SW |
282 | uint32_t blocks_allocated = 0; |
283 | uint32_t block; | |
284 | uint32_t *bmap; | |
285 | logout("\n"); | |
286 | ||
4534ff54 KW |
287 | if (fix) { |
288 | return -ENOTSUP; | |
289 | } | |
290 | ||
7267c094 | 291 | bmap = g_malloc(s->header.blocks_in_image * sizeof(uint32_t)); |
9aebd98a SW |
292 | memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t)); |
293 | ||
294 | /* Check block map and value of blocks_allocated. */ | |
295 | for (block = 0; block < s->header.blocks_in_image; block++) { | |
296 | uint32_t bmap_entry = le32_to_cpu(s->bmap[block]); | |
c794b4e0 | 297 | if (VDI_IS_ALLOCATED(bmap_entry)) { |
9aebd98a SW |
298 | if (bmap_entry < s->header.blocks_in_image) { |
299 | blocks_allocated++; | |
c794b4e0 | 300 | if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) { |
9aebd98a SW |
301 | bmap[bmap_entry] = bmap_entry; |
302 | } else { | |
303 | fprintf(stderr, "ERROR: block index %" PRIu32 | |
304 | " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry); | |
9ac228e0 | 305 | res->corruptions++; |
9aebd98a SW |
306 | } |
307 | } else { | |
308 | fprintf(stderr, "ERROR: block index %" PRIu32 | |
309 | " too large, is %" PRIu32 "\n", block, bmap_entry); | |
9ac228e0 | 310 | res->corruptions++; |
9aebd98a SW |
311 | } |
312 | } | |
313 | } | |
314 | if (blocks_allocated != s->header.blocks_allocated) { | |
315 | fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32 | |
316 | ", should be %" PRIu32 "\n", | |
317 | blocks_allocated, s->header.blocks_allocated); | |
9ac228e0 | 318 | res->corruptions++; |
9aebd98a SW |
319 | } |
320 | ||
7267c094 | 321 | g_free(bmap); |
9aebd98a | 322 | |
9ac228e0 | 323 | return 0; |
9aebd98a SW |
324 | } |
325 | ||
326 | static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
327 | { | |
328 | /* TODO: vdi_get_info would be needed for machine snapshots. | |
329 | vm_state_offset is still missing. */ | |
330 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; | |
331 | logout("\n"); | |
332 | bdi->cluster_size = s->block_size; | |
333 | bdi->vm_state_offset = 0; | |
334 | return 0; | |
335 | } | |
336 | ||
337 | static int vdi_make_empty(BlockDriverState *bs) | |
338 | { | |
339 | /* TODO: missing code. */ | |
340 | logout("\n"); | |
341 | /* The return value for missing code must be 0, see block.c. */ | |
342 | return 0; | |
343 | } | |
344 | ||
345 | static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename) | |
346 | { | |
347 | const VdiHeader *header = (const VdiHeader *)buf; | |
348 | int result = 0; | |
349 | ||
350 | logout("\n"); | |
351 | ||
352 | if (buf_size < sizeof(*header)) { | |
353 | /* Header too small, no VDI. */ | |
354 | } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) { | |
355 | result = 100; | |
356 | } | |
357 | ||
358 | if (result == 0) { | |
359 | logout("no vdi image\n"); | |
360 | } else { | |
361 | logout("%s", header->text); | |
362 | } | |
363 | ||
364 | return result; | |
365 | } | |
366 | ||
66f82cee | 367 | static int vdi_open(BlockDriverState *bs, int flags) |
9aebd98a SW |
368 | { |
369 | BDRVVdiState *s = bs->opaque; | |
370 | VdiHeader header; | |
371 | size_t bmap_size; | |
8937f822 | 372 | int ret; |
9aebd98a SW |
373 | |
374 | logout("\n"); | |
375 | ||
8937f822 SW |
376 | ret = bdrv_read(bs->file, 0, (uint8_t *)&header, 1); |
377 | if (ret < 0) { | |
9aebd98a SW |
378 | goto fail; |
379 | } | |
380 | ||
381 | vdi_header_to_cpu(&header); | |
382 | #if defined(CONFIG_VDI_DEBUG) | |
383 | vdi_header_print(&header); | |
384 | #endif | |
385 | ||
f21dc3a4 SW |
386 | if (header.disk_size % SECTOR_SIZE != 0) { |
387 | /* 'VBoxManage convertfromraw' can create images with odd disk sizes. | |
388 | We accept them but round the disk size to the next multiple of | |
389 | SECTOR_SIZE. */ | |
390 | logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size); | |
391 | header.disk_size += SECTOR_SIZE - 1; | |
392 | header.disk_size &= ~(SECTOR_SIZE - 1); | |
393 | } | |
394 | ||
9aebd98a SW |
395 | if (header.version != VDI_VERSION_1_1) { |
396 | logout("unsupported version %u.%u\n", | |
397 | header.version >> 16, header.version & 0xffff); | |
8937f822 | 398 | ret = -ENOTSUP; |
9aebd98a SW |
399 | goto fail; |
400 | } else if (header.offset_bmap % SECTOR_SIZE != 0) { | |
401 | /* We only support block maps which start on a sector boundary. */ | |
402 | logout("unsupported block map offset 0x%x B\n", header.offset_bmap); | |
8937f822 | 403 | ret = -ENOTSUP; |
9aebd98a SW |
404 | goto fail; |
405 | } else if (header.offset_data % SECTOR_SIZE != 0) { | |
406 | /* We only support data blocks which start on a sector boundary. */ | |
407 | logout("unsupported data offset 0x%x B\n", header.offset_data); | |
8937f822 | 408 | ret = -ENOTSUP; |
9aebd98a SW |
409 | goto fail; |
410 | } else if (header.sector_size != SECTOR_SIZE) { | |
411 | logout("unsupported sector size %u B\n", header.sector_size); | |
8937f822 | 412 | ret = -ENOTSUP; |
9aebd98a SW |
413 | goto fail; |
414 | } else if (header.block_size != 1 * MiB) { | |
415 | logout("unsupported block size %u B\n", header.block_size); | |
8937f822 | 416 | ret = -ENOTSUP; |
9aebd98a | 417 | goto fail; |
f21dc3a4 SW |
418 | } else if (header.disk_size > |
419 | (uint64_t)header.blocks_in_image * header.block_size) { | |
420 | logout("unsupported disk size %" PRIu64 " B\n", header.disk_size); | |
8937f822 | 421 | ret = -ENOTSUP; |
9aebd98a SW |
422 | goto fail; |
423 | } else if (!uuid_is_null(header.uuid_link)) { | |
424 | logout("link uuid != 0, unsupported\n"); | |
8937f822 | 425 | ret = -ENOTSUP; |
9aebd98a SW |
426 | goto fail; |
427 | } else if (!uuid_is_null(header.uuid_parent)) { | |
428 | logout("parent uuid != 0, unsupported\n"); | |
8937f822 | 429 | ret = -ENOTSUP; |
9aebd98a SW |
430 | goto fail; |
431 | } | |
432 | ||
433 | bs->total_sectors = header.disk_size / SECTOR_SIZE; | |
434 | ||
435 | s->block_size = header.block_size; | |
436 | s->block_sectors = header.block_size / SECTOR_SIZE; | |
437 | s->bmap_sector = header.offset_bmap / SECTOR_SIZE; | |
438 | s->header = header; | |
439 | ||
440 | bmap_size = header.blocks_in_image * sizeof(uint32_t); | |
6eea90eb | 441 | bmap_size = (bmap_size + SECTOR_SIZE - 1) / SECTOR_SIZE; |
b76b6e95 | 442 | if (bmap_size > 0) { |
7267c094 | 443 | s->bmap = g_malloc(bmap_size * SECTOR_SIZE); |
b76b6e95 | 444 | } |
8937f822 SW |
445 | ret = bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, bmap_size); |
446 | if (ret < 0) { | |
9aebd98a SW |
447 | goto fail_free_bmap; |
448 | } | |
449 | ||
fc9d106c KW |
450 | /* Disable migration when vdi images are used */ |
451 | error_set(&s->migration_blocker, | |
452 | QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, | |
453 | "vdi", bs->device_name, "live migration"); | |
454 | migrate_add_blocker(s->migration_blocker); | |
455 | ||
9aebd98a SW |
456 | return 0; |
457 | ||
458 | fail_free_bmap: | |
7267c094 | 459 | g_free(s->bmap); |
9aebd98a SW |
460 | |
461 | fail: | |
8937f822 | 462 | return ret; |
9aebd98a SW |
463 | } |
464 | ||
ecfe2bba JC |
465 | static int vdi_reopen_prepare(BDRVReopenState *state, |
466 | BlockReopenQueue *queue, Error **errp) | |
467 | { | |
468 | return 0; | |
469 | } | |
470 | ||
e850b35a SH |
471 | static int coroutine_fn vdi_co_is_allocated(BlockDriverState *bs, |
472 | int64_t sector_num, int nb_sectors, int *pnum) | |
9aebd98a SW |
473 | { |
474 | /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */ | |
475 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; | |
476 | size_t bmap_index = sector_num / s->block_sectors; | |
477 | size_t sector_in_block = sector_num % s->block_sectors; | |
478 | int n_sectors = s->block_sectors - sector_in_block; | |
479 | uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]); | |
480 | logout("%p, %" PRId64 ", %d, %p\n", bs, sector_num, nb_sectors, pnum); | |
481 | if (n_sectors > nb_sectors) { | |
482 | n_sectors = nb_sectors; | |
483 | } | |
484 | *pnum = n_sectors; | |
c794b4e0 | 485 | return VDI_IS_ALLOCATED(bmap_entry); |
9aebd98a SW |
486 | } |
487 | ||
a7a43aa1 PB |
488 | static int vdi_co_read(BlockDriverState *bs, |
489 | int64_t sector_num, uint8_t *buf, int nb_sectors) | |
9aebd98a | 490 | { |
9aebd98a SW |
491 | BDRVVdiState *s = bs->opaque; |
492 | uint32_t bmap_entry; | |
493 | uint32_t block_index; | |
494 | uint32_t sector_in_block; | |
495 | uint32_t n_sectors; | |
eb9566d1 | 496 | int ret = 0; |
4de659e8 PB |
497 | |
498 | logout("\n"); | |
9aebd98a | 499 | |
eb9566d1 PB |
500 | while (ret >= 0 && nb_sectors > 0) { |
501 | block_index = sector_num / s->block_sectors; | |
502 | sector_in_block = sector_num % s->block_sectors; | |
503 | n_sectors = s->block_sectors - sector_in_block; | |
504 | if (n_sectors > nb_sectors) { | |
505 | n_sectors = nb_sectors; | |
506 | } | |
0c7bfc32 | 507 | |
eb9566d1 PB |
508 | logout("will read %u sectors starting at sector %" PRIu64 "\n", |
509 | n_sectors, sector_num); | |
510 | ||
511 | /* prepare next AIO request */ | |
512 | bmap_entry = le32_to_cpu(s->bmap[block_index]); | |
513 | if (!VDI_IS_ALLOCATED(bmap_entry)) { | |
514 | /* Block not allocated, return zeros, no need to wait. */ | |
515 | memset(buf, 0, n_sectors * SECTOR_SIZE); | |
516 | ret = 0; | |
517 | } else { | |
518 | uint64_t offset = s->header.offset_data / SECTOR_SIZE + | |
519 | (uint64_t)bmap_entry * s->block_sectors + | |
520 | sector_in_block; | |
521 | ret = bdrv_read(bs->file, offset, buf, n_sectors); | |
522 | } | |
523 | logout("%u sectors read\n", n_sectors); | |
0c7bfc32 | 524 | |
eb9566d1 PB |
525 | nb_sectors -= n_sectors; |
526 | sector_num += n_sectors; | |
527 | buf += n_sectors * SECTOR_SIZE; | |
9aebd98a | 528 | } |
3d46a75a | 529 | |
3d46a75a | 530 | return ret; |
9aebd98a SW |
531 | } |
532 | ||
a7a43aa1 PB |
533 | static int vdi_co_write(BlockDriverState *bs, |
534 | int64_t sector_num, const uint8_t *buf, int nb_sectors) | |
9aebd98a | 535 | { |
9aebd98a SW |
536 | BDRVVdiState *s = bs->opaque; |
537 | uint32_t bmap_entry; | |
538 | uint32_t block_index; | |
539 | uint32_t sector_in_block; | |
540 | uint32_t n_sectors; | |
bfc45fc1 PB |
541 | uint32_t bmap_first = VDI_UNALLOCATED; |
542 | uint32_t bmap_last = VDI_UNALLOCATED; | |
bfc45fc1 | 543 | uint8_t *block = NULL; |
eb9566d1 | 544 | int ret = 0; |
4de659e8 PB |
545 | |
546 | logout("\n"); | |
9aebd98a | 547 | |
eb9566d1 PB |
548 | while (ret >= 0 && nb_sectors > 0) { |
549 | block_index = sector_num / s->block_sectors; | |
550 | sector_in_block = sector_num % s->block_sectors; | |
551 | n_sectors = s->block_sectors - sector_in_block; | |
552 | if (n_sectors > nb_sectors) { | |
553 | n_sectors = nb_sectors; | |
554 | } | |
9aebd98a | 555 | |
eb9566d1 PB |
556 | logout("will write %u sectors starting at sector %" PRIu64 "\n", |
557 | n_sectors, sector_num); | |
558 | ||
559 | /* prepare next AIO request */ | |
560 | bmap_entry = le32_to_cpu(s->bmap[block_index]); | |
561 | if (!VDI_IS_ALLOCATED(bmap_entry)) { | |
562 | /* Allocate new block and write to it. */ | |
563 | uint64_t offset; | |
564 | bmap_entry = s->header.blocks_allocated; | |
565 | s->bmap[block_index] = cpu_to_le32(bmap_entry); | |
566 | s->header.blocks_allocated++; | |
567 | offset = s->header.offset_data / SECTOR_SIZE + | |
568 | (uint64_t)bmap_entry * s->block_sectors; | |
569 | if (block == NULL) { | |
570 | block = g_malloc(s->block_size); | |
571 | bmap_first = block_index; | |
572 | } | |
573 | bmap_last = block_index; | |
574 | /* Copy data to be written to new block and zero unused parts. */ | |
575 | memset(block, 0, sector_in_block * SECTOR_SIZE); | |
576 | memcpy(block + sector_in_block * SECTOR_SIZE, | |
577 | buf, n_sectors * SECTOR_SIZE); | |
578 | memset(block + (sector_in_block + n_sectors) * SECTOR_SIZE, 0, | |
579 | (s->block_sectors - n_sectors - sector_in_block) * SECTOR_SIZE); | |
580 | ret = bdrv_write(bs->file, offset, block, s->block_sectors); | |
581 | } else { | |
582 | uint64_t offset = s->header.offset_data / SECTOR_SIZE + | |
583 | (uint64_t)bmap_entry * s->block_sectors + | |
584 | sector_in_block; | |
585 | ret = bdrv_write(bs->file, offset, buf, n_sectors); | |
9aebd98a | 586 | } |
0c7bfc32 | 587 | |
eb9566d1 PB |
588 | nb_sectors -= n_sectors; |
589 | sector_num += n_sectors; | |
590 | buf += n_sectors * SECTOR_SIZE; | |
0c7bfc32 | 591 | |
eb9566d1 | 592 | logout("%u sectors written\n", n_sectors); |
9aebd98a | 593 | } |
9aebd98a | 594 | |
0c7bfc32 | 595 | logout("finished data write\n"); |
4eea78e6 PB |
596 | if (ret < 0) { |
597 | return ret; | |
598 | } | |
599 | ||
600 | if (block) { | |
601 | /* One or more new blocks were allocated. */ | |
602 | VdiHeader *header = (VdiHeader *) block; | |
603 | uint8_t *base; | |
604 | uint64_t offset; | |
605 | ||
606 | logout("now writing modified header\n"); | |
607 | assert(VDI_IS_ALLOCATED(bmap_first)); | |
608 | *header = s->header; | |
609 | vdi_header_to_le(header); | |
610 | ret = bdrv_write(bs->file, 0, block, 1); | |
bfc45fc1 PB |
611 | g_free(block); |
612 | block = NULL; | |
4eea78e6 PB |
613 | |
614 | if (ret < 0) { | |
615 | return ret; | |
0c7bfc32 | 616 | } |
4eea78e6 PB |
617 | |
618 | logout("now writing modified block map entry %u...%u\n", | |
619 | bmap_first, bmap_last); | |
620 | /* Write modified sectors from block map. */ | |
621 | bmap_first /= (SECTOR_SIZE / sizeof(uint32_t)); | |
622 | bmap_last /= (SECTOR_SIZE / sizeof(uint32_t)); | |
623 | n_sectors = bmap_last - bmap_first + 1; | |
624 | offset = s->bmap_sector + bmap_first; | |
625 | base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE; | |
626 | logout("will write %u block map sectors starting from entry %u\n", | |
627 | n_sectors, bmap_first); | |
628 | ret = bdrv_write(bs->file, offset, base, n_sectors); | |
0c7bfc32 PB |
629 | } |
630 | ||
3d46a75a | 631 | return ret; |
9aebd98a SW |
632 | } |
633 | ||
9aebd98a SW |
634 | static int vdi_create(const char *filename, QEMUOptionParameter *options) |
635 | { | |
636 | int fd; | |
637 | int result = 0; | |
638 | uint64_t bytes = 0; | |
639 | uint32_t blocks; | |
99cce9fa | 640 | size_t block_size = DEFAULT_CLUSTER_SIZE; |
9aebd98a SW |
641 | uint32_t image_type = VDI_TYPE_DYNAMIC; |
642 | VdiHeader header; | |
643 | size_t i; | |
644 | size_t bmap_size; | |
9aebd98a SW |
645 | |
646 | logout("\n"); | |
647 | ||
648 | /* Read out options. */ | |
649 | while (options && options->name) { | |
650 | if (!strcmp(options->name, BLOCK_OPT_SIZE)) { | |
651 | bytes = options->value.n; | |
652 | #if defined(CONFIG_VDI_BLOCK_SIZE) | |
653 | } else if (!strcmp(options->name, BLOCK_OPT_CLUSTER_SIZE)) { | |
654 | if (options->value.n) { | |
655 | /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */ | |
656 | block_size = options->value.n; | |
657 | } | |
658 | #endif | |
659 | #if defined(CONFIG_VDI_STATIC_IMAGE) | |
660 | } else if (!strcmp(options->name, BLOCK_OPT_STATIC)) { | |
6eea90eb SW |
661 | if (options->value.n) { |
662 | image_type = VDI_TYPE_STATIC; | |
663 | } | |
9aebd98a SW |
664 | #endif |
665 | } | |
666 | options++; | |
667 | } | |
668 | ||
6165f4d8 CB |
669 | fd = qemu_open(filename, |
670 | O_WRONLY | O_CREAT | O_TRUNC | O_BINARY | O_LARGEFILE, | |
671 | 0644); | |
9aebd98a SW |
672 | if (fd < 0) { |
673 | return -errno; | |
674 | } | |
675 | ||
f21dc3a4 SW |
676 | /* We need enough blocks to store the given disk size, |
677 | so always round up. */ | |
678 | blocks = (bytes + block_size - 1) / block_size; | |
679 | ||
9aebd98a SW |
680 | bmap_size = blocks * sizeof(uint32_t); |
681 | bmap_size = ((bmap_size + SECTOR_SIZE - 1) & ~(SECTOR_SIZE -1)); | |
682 | ||
683 | memset(&header, 0, sizeof(header)); | |
1786dc15 | 684 | pstrcpy(header.text, sizeof(header.text), VDI_TEXT); |
9aebd98a SW |
685 | header.signature = VDI_SIGNATURE; |
686 | header.version = VDI_VERSION_1_1; | |
687 | header.header_size = 0x180; | |
688 | header.image_type = image_type; | |
689 | header.offset_bmap = 0x200; | |
690 | header.offset_data = 0x200 + bmap_size; | |
691 | header.sector_size = SECTOR_SIZE; | |
692 | header.disk_size = bytes; | |
693 | header.block_size = block_size; | |
694 | header.blocks_in_image = blocks; | |
6eea90eb SW |
695 | if (image_type == VDI_TYPE_STATIC) { |
696 | header.blocks_allocated = blocks; | |
697 | } | |
9aebd98a SW |
698 | uuid_generate(header.uuid_image); |
699 | uuid_generate(header.uuid_last_snap); | |
700 | /* There is no need to set header.uuid_link or header.uuid_parent here. */ | |
701 | #if defined(CONFIG_VDI_DEBUG) | |
702 | vdi_header_print(&header); | |
703 | #endif | |
704 | vdi_header_to_le(&header); | |
705 | if (write(fd, &header, sizeof(header)) < 0) { | |
706 | result = -errno; | |
707 | } | |
708 | ||
b76b6e95 | 709 | if (bmap_size > 0) { |
514f21a5 SW |
710 | uint32_t *bmap = g_malloc0(bmap_size); |
711 | for (i = 0; i < blocks; i++) { | |
712 | if (image_type == VDI_TYPE_STATIC) { | |
713 | bmap[i] = i; | |
714 | } else { | |
715 | bmap[i] = VDI_UNALLOCATED; | |
716 | } | |
9aebd98a | 717 | } |
514f21a5 SW |
718 | if (write(fd, bmap, bmap_size) < 0) { |
719 | result = -errno; | |
720 | } | |
721 | g_free(bmap); | |
9aebd98a | 722 | } |
514f21a5 | 723 | |
9aebd98a SW |
724 | if (image_type == VDI_TYPE_STATIC) { |
725 | if (ftruncate(fd, sizeof(header) + bmap_size + blocks * block_size)) { | |
726 | result = -errno; | |
727 | } | |
728 | } | |
729 | ||
730 | if (close(fd) < 0) { | |
731 | result = -errno; | |
732 | } | |
733 | ||
734 | return result; | |
735 | } | |
736 | ||
737 | static void vdi_close(BlockDriverState *bs) | |
738 | { | |
fc9d106c | 739 | BDRVVdiState *s = bs->opaque; |
6ac5f388 KW |
740 | |
741 | g_free(s->bmap); | |
742 | ||
fc9d106c KW |
743 | migrate_del_blocker(s->migration_blocker); |
744 | error_free(s->migration_blocker); | |
9aebd98a SW |
745 | } |
746 | ||
9aebd98a SW |
747 | static QEMUOptionParameter vdi_create_options[] = { |
748 | { | |
749 | .name = BLOCK_OPT_SIZE, | |
750 | .type = OPT_SIZE, | |
751 | .help = "Virtual disk size" | |
752 | }, | |
753 | #if defined(CONFIG_VDI_BLOCK_SIZE) | |
754 | { | |
755 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
756 | .type = OPT_SIZE, | |
99cce9fa KW |
757 | .help = "VDI cluster (block) size", |
758 | .value = { .n = DEFAULT_CLUSTER_SIZE }, | |
9aebd98a SW |
759 | }, |
760 | #endif | |
761 | #if defined(CONFIG_VDI_STATIC_IMAGE) | |
762 | { | |
763 | .name = BLOCK_OPT_STATIC, | |
764 | .type = OPT_FLAG, | |
765 | .help = "VDI static (pre-allocated) image" | |
766 | }, | |
767 | #endif | |
768 | /* TODO: An additional option to set UUID values might be useful. */ | |
769 | { NULL } | |
770 | }; | |
771 | ||
772 | static BlockDriver bdrv_vdi = { | |
773 | .format_name = "vdi", | |
774 | .instance_size = sizeof(BDRVVdiState), | |
775 | .bdrv_probe = vdi_probe, | |
776 | .bdrv_open = vdi_open, | |
777 | .bdrv_close = vdi_close, | |
ecfe2bba | 778 | .bdrv_reopen_prepare = vdi_reopen_prepare, |
9aebd98a | 779 | .bdrv_create = vdi_create, |
e850b35a | 780 | .bdrv_co_is_allocated = vdi_co_is_allocated, |
9aebd98a SW |
781 | .bdrv_make_empty = vdi_make_empty, |
782 | ||
a7a43aa1 | 783 | .bdrv_read = vdi_co_read, |
9aebd98a | 784 | #if defined(CONFIG_VDI_WRITE) |
a7a43aa1 | 785 | .bdrv_write = vdi_co_write, |
9aebd98a SW |
786 | #endif |
787 | ||
788 | .bdrv_get_info = vdi_get_info, | |
789 | ||
790 | .create_options = vdi_create_options, | |
791 | .bdrv_check = vdi_check, | |
792 | }; | |
793 | ||
794 | static void bdrv_vdi_init(void) | |
795 | { | |
796 | logout("\n"); | |
797 | bdrv_register(&bdrv_vdi); | |
798 | } | |
799 | ||
800 | block_init(bdrv_vdi_init); |