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