]>
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" |
f043568f | 53 | #include "qemu/units.h" |
da34e65c | 54 | #include "qapi/error.h" |
49858b50 HR |
55 | #include "qapi/qobject-input-visitor.h" |
56 | #include "qapi/qapi-visit-block-core.h" | |
737e150e | 57 | #include "block/block_int.h" |
f853465a | 58 | #include "block/qdict.h" |
a08f0c3b | 59 | #include "sysemu/block-backend.h" |
1de7afc9 | 60 | #include "qemu/module.h" |
922a01a0 | 61 | #include "qemu/option.h" |
58369e22 | 62 | #include "qemu/bswap.h" |
795c40b8 | 63 | #include "migration/blocker.h" |
10817bf0 | 64 | #include "qemu/coroutine.h" |
f348b6d1 | 65 | #include "qemu/cutils.h" |
7c6f55b6 | 66 | #include "qemu/uuid.h" |
9aebd98a SW |
67 | |
68 | /* Code configuration options. */ | |
69 | ||
70 | /* Enable debug messages. */ | |
71 | //~ #define CONFIG_VDI_DEBUG | |
72 | ||
73 | /* Support write operations on VDI images. */ | |
74 | #define CONFIG_VDI_WRITE | |
75 | ||
76 | /* Support non-standard block (cluster) size. This is untested. | |
77 | * Maybe it will be needed for very large images. | |
78 | */ | |
79 | //~ #define CONFIG_VDI_BLOCK_SIZE | |
80 | ||
81 | /* Support static (fixed, pre-allocated) images. */ | |
82 | #define CONFIG_VDI_STATIC_IMAGE | |
83 | ||
84 | /* Command line option for static images. */ | |
85 | #define BLOCK_OPT_STATIC "static" | |
86 | ||
9aebd98a | 87 | #define SECTOR_SIZE 512 |
14632122 MA |
88 | #define DEFAULT_CLUSTER_SIZE 1048576 |
89 | /* Note: can't use 1 * MiB, because it's passed to stringify() */ | |
9aebd98a SW |
90 | |
91 | #if defined(CONFIG_VDI_DEBUG) | |
b80666bf | 92 | #define VDI_DEBUG 1 |
9aebd98a | 93 | #else |
b80666bf | 94 | #define VDI_DEBUG 0 |
9aebd98a SW |
95 | #endif |
96 | ||
b80666bf EB |
97 | #define logout(fmt, ...) \ |
98 | do { \ | |
99 | if (VDI_DEBUG) { \ | |
100 | fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__); \ | |
101 | } \ | |
102 | } while (0) | |
103 | ||
9aebd98a SW |
104 | /* Image signature. */ |
105 | #define VDI_SIGNATURE 0xbeda107f | |
106 | ||
107 | /* Image version. */ | |
108 | #define VDI_VERSION_1_1 0x00010001 | |
109 | ||
110 | /* Image type. */ | |
111 | #define VDI_TYPE_DYNAMIC 1 | |
112 | #define VDI_TYPE_STATIC 2 | |
113 | ||
114 | /* Innotek / SUN images use these strings in header.text: | |
115 | * "<<< innotek VirtualBox Disk Image >>>\n" | |
116 | * "<<< Sun xVM VirtualBox Disk Image >>>\n" | |
117 | * "<<< Sun VirtualBox Disk Image >>>\n" | |
118 | * The value does not matter, so QEMU created images use a different text. | |
119 | */ | |
120 | #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n" | |
121 | ||
c794b4e0 ES |
122 | /* A never-allocated block; semantically arbitrary content. */ |
123 | #define VDI_UNALLOCATED 0xffffffffU | |
124 | ||
125 | /* A discarded (no longer allocated) block; semantically zero-filled. */ | |
126 | #define VDI_DISCARDED 0xfffffffeU | |
127 | ||
128 | #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED) | |
9aebd98a | 129 | |
d20418ee HR |
130 | /* The bmap will take up VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) bytes; since |
131 | * the bmap is read and written in a single operation, its size needs to be | |
132 | * limited to INT_MAX; furthermore, when opening an image, the bmap size is | |
133 | * rounded up to be aligned on BDRV_SECTOR_SIZE. | |
134 | * Therefore this should satisfy the following: | |
135 | * VDI_BLOCKS_IN_IMAGE_MAX * sizeof(uint32_t) + BDRV_SECTOR_SIZE == INT_MAX + 1 | |
136 | * (INT_MAX + 1 is the first value not representable as an int) | |
137 | * This guarantees that any value below or equal to the constant will, when | |
138 | * multiplied by sizeof(uint32_t) and rounded up to a BDRV_SECTOR_SIZE boundary, | |
139 | * still be below or equal to INT_MAX. */ | |
140 | #define VDI_BLOCKS_IN_IMAGE_MAX \ | |
141 | ((unsigned)((INT_MAX + 1u - BDRV_SECTOR_SIZE) / sizeof(uint32_t))) | |
63fa06dc JC |
142 | #define VDI_DISK_SIZE_MAX ((uint64_t)VDI_BLOCKS_IN_IMAGE_MAX * \ |
143 | (uint64_t)DEFAULT_CLUSTER_SIZE) | |
144 | ||
49858b50 HR |
145 | static QemuOptsList vdi_create_opts; |
146 | ||
9aebd98a SW |
147 | typedef struct { |
148 | char text[0x40]; | |
149 | uint32_t signature; | |
150 | uint32_t version; | |
151 | uint32_t header_size; | |
152 | uint32_t image_type; | |
153 | uint32_t image_flags; | |
154 | char description[256]; | |
155 | uint32_t offset_bmap; | |
156 | uint32_t offset_data; | |
157 | uint32_t cylinders; /* disk geometry, unused here */ | |
158 | uint32_t heads; /* disk geometry, unused here */ | |
159 | uint32_t sectors; /* disk geometry, unused here */ | |
160 | uint32_t sector_size; | |
161 | uint32_t unused1; | |
162 | uint64_t disk_size; | |
163 | uint32_t block_size; | |
164 | uint32_t block_extra; /* unused here */ | |
165 | uint32_t blocks_in_image; | |
166 | uint32_t blocks_allocated; | |
7c6f55b6 FZ |
167 | QemuUUID uuid_image; |
168 | QemuUUID uuid_last_snap; | |
169 | QemuUUID uuid_link; | |
170 | QemuUUID uuid_parent; | |
9aebd98a | 171 | uint64_t unused2[7]; |
8368febd | 172 | } QEMU_PACKED VdiHeader; |
9aebd98a SW |
173 | |
174 | typedef struct { | |
9aebd98a SW |
175 | /* The block map entries are little endian (even in memory). */ |
176 | uint32_t *bmap; | |
177 | /* Size of block (bytes). */ | |
178 | uint32_t block_size; | |
9aebd98a SW |
179 | /* First sector of block map. */ |
180 | uint32_t bmap_sector; | |
4ff9786c | 181 | /* VDI header (converted to host endianness). */ |
9aebd98a | 182 | VdiHeader header; |
fc9d106c | 183 | |
1e886639 | 184 | CoRwlock bmap_lock; |
f0ab6f10 | 185 | |
fc9d106c | 186 | Error *migration_blocker; |
9aebd98a SW |
187 | } BDRVVdiState; |
188 | ||
9aebd98a SW |
189 | static void vdi_header_to_cpu(VdiHeader *header) |
190 | { | |
09190184 PM |
191 | header->signature = le32_to_cpu(header->signature); |
192 | header->version = le32_to_cpu(header->version); | |
193 | header->header_size = le32_to_cpu(header->header_size); | |
194 | header->image_type = le32_to_cpu(header->image_type); | |
195 | header->image_flags = le32_to_cpu(header->image_flags); | |
196 | header->offset_bmap = le32_to_cpu(header->offset_bmap); | |
197 | header->offset_data = le32_to_cpu(header->offset_data); | |
198 | header->cylinders = le32_to_cpu(header->cylinders); | |
199 | header->heads = le32_to_cpu(header->heads); | |
200 | header->sectors = le32_to_cpu(header->sectors); | |
201 | header->sector_size = le32_to_cpu(header->sector_size); | |
202 | header->disk_size = le64_to_cpu(header->disk_size); | |
203 | header->block_size = le32_to_cpu(header->block_size); | |
204 | header->block_extra = le32_to_cpu(header->block_extra); | |
205 | header->blocks_in_image = le32_to_cpu(header->blocks_in_image); | |
206 | header->blocks_allocated = le32_to_cpu(header->blocks_allocated); | |
1324f063 PM |
207 | header->uuid_image = qemu_uuid_bswap(header->uuid_image); |
208 | header->uuid_last_snap = qemu_uuid_bswap(header->uuid_last_snap); | |
209 | header->uuid_link = qemu_uuid_bswap(header->uuid_link); | |
210 | header->uuid_parent = qemu_uuid_bswap(header->uuid_parent); | |
9aebd98a SW |
211 | } |
212 | ||
213 | static void vdi_header_to_le(VdiHeader *header) | |
214 | { | |
09190184 PM |
215 | header->signature = cpu_to_le32(header->signature); |
216 | header->version = cpu_to_le32(header->version); | |
217 | header->header_size = cpu_to_le32(header->header_size); | |
218 | header->image_type = cpu_to_le32(header->image_type); | |
219 | header->image_flags = cpu_to_le32(header->image_flags); | |
220 | header->offset_bmap = cpu_to_le32(header->offset_bmap); | |
221 | header->offset_data = cpu_to_le32(header->offset_data); | |
222 | header->cylinders = cpu_to_le32(header->cylinders); | |
223 | header->heads = cpu_to_le32(header->heads); | |
224 | header->sectors = cpu_to_le32(header->sectors); | |
225 | header->sector_size = cpu_to_le32(header->sector_size); | |
226 | header->disk_size = cpu_to_le64(header->disk_size); | |
227 | header->block_size = cpu_to_le32(header->block_size); | |
228 | header->block_extra = cpu_to_le32(header->block_extra); | |
229 | header->blocks_in_image = cpu_to_le32(header->blocks_in_image); | |
230 | header->blocks_allocated = cpu_to_le32(header->blocks_allocated); | |
1324f063 PM |
231 | header->uuid_image = qemu_uuid_bswap(header->uuid_image); |
232 | header->uuid_last_snap = qemu_uuid_bswap(header->uuid_last_snap); | |
233 | header->uuid_link = qemu_uuid_bswap(header->uuid_link); | |
234 | header->uuid_parent = qemu_uuid_bswap(header->uuid_parent); | |
9aebd98a SW |
235 | } |
236 | ||
9aebd98a SW |
237 | static void vdi_header_print(VdiHeader *header) |
238 | { | |
ac928b8e PM |
239 | char uuidstr[37]; |
240 | QemuUUID uuid; | |
9aebd98a | 241 | logout("text %s", header->text); |
9f0470bb | 242 | logout("signature 0x%08x\n", header->signature); |
9aebd98a SW |
243 | logout("header size 0x%04x\n", header->header_size); |
244 | logout("image type 0x%04x\n", header->image_type); | |
245 | logout("image flags 0x%04x\n", header->image_flags); | |
246 | logout("description %s\n", header->description); | |
247 | logout("offset bmap 0x%04x\n", header->offset_bmap); | |
248 | logout("offset data 0x%04x\n", header->offset_data); | |
249 | logout("cylinders 0x%04x\n", header->cylinders); | |
250 | logout("heads 0x%04x\n", header->heads); | |
251 | logout("sectors 0x%04x\n", header->sectors); | |
252 | logout("sector size 0x%04x\n", header->sector_size); | |
253 | logout("image size 0x%" PRIx64 " B (%" PRIu64 " MiB)\n", | |
254 | header->disk_size, header->disk_size / MiB); | |
255 | logout("block size 0x%04x\n", header->block_size); | |
256 | logout("block extra 0x%04x\n", header->block_extra); | |
257 | logout("blocks tot. 0x%04x\n", header->blocks_in_image); | |
258 | logout("blocks all. 0x%04x\n", header->blocks_allocated); | |
ac928b8e PM |
259 | uuid = header->uuid_image; |
260 | qemu_uuid_unparse(&uuid, uuidstr); | |
261 | logout("uuid image %s\n", uuidstr); | |
262 | uuid = header->uuid_last_snap; | |
263 | qemu_uuid_unparse(&uuid, uuidstr); | |
264 | logout("uuid snap %s\n", uuidstr); | |
265 | uuid = header->uuid_link; | |
266 | qemu_uuid_unparse(&uuid, uuidstr); | |
267 | logout("uuid link %s\n", uuidstr); | |
268 | uuid = header->uuid_parent; | |
269 | qemu_uuid_unparse(&uuid, uuidstr); | |
270 | logout("uuid parent %s\n", uuidstr); | |
9aebd98a | 271 | } |
9aebd98a | 272 | |
2fd61638 PB |
273 | static int coroutine_fn vdi_co_check(BlockDriverState *bs, BdrvCheckResult *res, |
274 | BdrvCheckMode fix) | |
9aebd98a SW |
275 | { |
276 | /* TODO: additional checks possible. */ | |
277 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; | |
9aebd98a SW |
278 | uint32_t blocks_allocated = 0; |
279 | uint32_t block; | |
280 | uint32_t *bmap; | |
281 | logout("\n"); | |
282 | ||
4534ff54 KW |
283 | if (fix) { |
284 | return -ENOTSUP; | |
285 | } | |
286 | ||
5839e53b | 287 | bmap = g_try_new(uint32_t, s->header.blocks_in_image); |
17cce735 KW |
288 | if (s->header.blocks_in_image && bmap == NULL) { |
289 | res->check_errors++; | |
290 | return -ENOMEM; | |
291 | } | |
292 | ||
9aebd98a SW |
293 | memset(bmap, 0xff, s->header.blocks_in_image * sizeof(uint32_t)); |
294 | ||
295 | /* Check block map and value of blocks_allocated. */ | |
296 | for (block = 0; block < s->header.blocks_in_image; block++) { | |
297 | uint32_t bmap_entry = le32_to_cpu(s->bmap[block]); | |
c794b4e0 | 298 | if (VDI_IS_ALLOCATED(bmap_entry)) { |
9aebd98a SW |
299 | if (bmap_entry < s->header.blocks_in_image) { |
300 | blocks_allocated++; | |
c794b4e0 | 301 | if (!VDI_IS_ALLOCATED(bmap[bmap_entry])) { |
9aebd98a SW |
302 | bmap[bmap_entry] = bmap_entry; |
303 | } else { | |
304 | fprintf(stderr, "ERROR: block index %" PRIu32 | |
305 | " also used by %" PRIu32 "\n", bmap[bmap_entry], bmap_entry); | |
9ac228e0 | 306 | res->corruptions++; |
9aebd98a SW |
307 | } |
308 | } else { | |
309 | fprintf(stderr, "ERROR: block index %" PRIu32 | |
310 | " too large, is %" PRIu32 "\n", block, bmap_entry); | |
9ac228e0 | 311 | res->corruptions++; |
9aebd98a SW |
312 | } |
313 | } | |
314 | } | |
315 | if (blocks_allocated != s->header.blocks_allocated) { | |
316 | fprintf(stderr, "ERROR: allocated blocks mismatch, is %" PRIu32 | |
317 | ", should be %" PRIu32 "\n", | |
318 | blocks_allocated, s->header.blocks_allocated); | |
9ac228e0 | 319 | res->corruptions++; |
9aebd98a SW |
320 | } |
321 | ||
7267c094 | 322 | g_free(bmap); |
9aebd98a | 323 | |
9ac228e0 | 324 | return 0; |
9aebd98a SW |
325 | } |
326 | ||
327 | static int vdi_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) | |
328 | { | |
329 | /* TODO: vdi_get_info would be needed for machine snapshots. | |
330 | vm_state_offset is still missing. */ | |
331 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; | |
332 | logout("\n"); | |
333 | bdi->cluster_size = s->block_size; | |
334 | bdi->vm_state_offset = 0; | |
95de6d70 | 335 | bdi->unallocated_blocks_are_zero = true; |
9aebd98a SW |
336 | return 0; |
337 | } | |
338 | ||
339 | static int vdi_make_empty(BlockDriverState *bs) | |
340 | { | |
341 | /* TODO: missing code. */ | |
342 | logout("\n"); | |
343 | /* The return value for missing code must be 0, see block.c. */ | |
344 | return 0; | |
345 | } | |
346 | ||
347 | static int vdi_probe(const uint8_t *buf, int buf_size, const char *filename) | |
348 | { | |
349 | const VdiHeader *header = (const VdiHeader *)buf; | |
dddc7750 | 350 | int ret = 0; |
9aebd98a SW |
351 | |
352 | logout("\n"); | |
353 | ||
354 | if (buf_size < sizeof(*header)) { | |
355 | /* Header too small, no VDI. */ | |
356 | } else if (le32_to_cpu(header->signature) == VDI_SIGNATURE) { | |
dddc7750 | 357 | ret = 100; |
9aebd98a SW |
358 | } |
359 | ||
dddc7750 | 360 | if (ret == 0) { |
9aebd98a SW |
361 | logout("no vdi image\n"); |
362 | } else { | |
363 | logout("%s", header->text); | |
364 | } | |
365 | ||
dddc7750 | 366 | return ret; |
9aebd98a SW |
367 | } |
368 | ||
015a1036 HR |
369 | static int vdi_open(BlockDriverState *bs, QDict *options, int flags, |
370 | Error **errp) | |
9aebd98a SW |
371 | { |
372 | BDRVVdiState *s = bs->opaque; | |
373 | VdiHeader header; | |
374 | size_t bmap_size; | |
8937f822 | 375 | int ret; |
fe44dc91 | 376 | Error *local_err = NULL; |
ac928b8e | 377 | QemuUUID uuid_link, uuid_parent; |
9aebd98a | 378 | |
4e4bf5c4 KW |
379 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, |
380 | false, errp); | |
381 | if (!bs->file) { | |
382 | return -EINVAL; | |
383 | } | |
384 | ||
9aebd98a SW |
385 | logout("\n"); |
386 | ||
fbcbbf4e | 387 | ret = bdrv_read(bs->file, 0, (uint8_t *)&header, 1); |
8937f822 | 388 | if (ret < 0) { |
9aebd98a SW |
389 | goto fail; |
390 | } | |
391 | ||
392 | vdi_header_to_cpu(&header); | |
95a14d51 KW |
393 | if (VDI_DEBUG) { |
394 | vdi_header_print(&header); | |
395 | } | |
9aebd98a | 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 | ||
ac928b8e PM |
405 | uuid_link = header.uuid_link; |
406 | uuid_parent = header.uuid_parent; | |
407 | ||
f21dc3a4 SW |
408 | if (header.disk_size % SECTOR_SIZE != 0) { |
409 | /* 'VBoxManage convertfromraw' can create images with odd disk sizes. | |
410 | We accept them but round the disk size to the next multiple of | |
411 | SECTOR_SIZE. */ | |
412 | logout("odd disk size %" PRIu64 " B, round up\n", header.disk_size); | |
e9082e47 | 413 | header.disk_size = ROUND_UP(header.disk_size, SECTOR_SIZE); |
f21dc3a4 SW |
414 | } |
415 | ||
0e87ba2c | 416 | if (header.signature != VDI_SIGNATURE) { |
521b2b5d HR |
417 | error_setg(errp, "Image not in VDI format (bad signature %08" PRIx32 |
418 | ")", header.signature); | |
76abe407 | 419 | ret = -EINVAL; |
0e87ba2c SW |
420 | goto fail; |
421 | } else if (header.version != VDI_VERSION_1_1) { | |
521b2b5d HR |
422 | error_setg(errp, "unsupported VDI image (version %" PRIu32 ".%" PRIu32 |
423 | ")", header.version >> 16, header.version & 0xffff); | |
8937f822 | 424 | ret = -ENOTSUP; |
9aebd98a SW |
425 | goto fail; |
426 | } else if (header.offset_bmap % SECTOR_SIZE != 0) { | |
427 | /* We only support block maps which start on a sector boundary. */ | |
5b7aa9b5 | 428 | error_setg(errp, "unsupported VDI image (unaligned block map offset " |
521b2b5d | 429 | "0x%" PRIx32 ")", header.offset_bmap); |
8937f822 | 430 | ret = -ENOTSUP; |
9aebd98a SW |
431 | goto fail; |
432 | } else if (header.offset_data % SECTOR_SIZE != 0) { | |
433 | /* We only support data blocks which start on a sector boundary. */ | |
521b2b5d HR |
434 | error_setg(errp, "unsupported VDI image (unaligned data offset 0x%" |
435 | PRIx32 ")", header.offset_data); | |
8937f822 | 436 | ret = -ENOTSUP; |
9aebd98a SW |
437 | goto fail; |
438 | } else if (header.sector_size != SECTOR_SIZE) { | |
521b2b5d HR |
439 | error_setg(errp, "unsupported VDI image (sector size %" PRIu32 |
440 | " is not %u)", header.sector_size, SECTOR_SIZE); | |
8937f822 | 441 | ret = -ENOTSUP; |
9aebd98a | 442 | goto fail; |
63fa06dc | 443 | } else if (header.block_size != DEFAULT_CLUSTER_SIZE) { |
521b2b5d | 444 | error_setg(errp, "unsupported VDI image (block size %" PRIu32 |
3dd5b8f4 | 445 | " is not %" PRIu32 ")", |
f043568f | 446 | header.block_size, DEFAULT_CLUSTER_SIZE); |
8937f822 | 447 | ret = -ENOTSUP; |
9aebd98a | 448 | goto fail; |
f21dc3a4 SW |
449 | } else if (header.disk_size > |
450 | (uint64_t)header.blocks_in_image * header.block_size) { | |
5b7aa9b5 PB |
451 | error_setg(errp, "unsupported VDI image (disk size %" PRIu64 ", " |
452 | "image bitmap has room for %" PRIu64 ")", | |
453 | header.disk_size, | |
454 | (uint64_t)header.blocks_in_image * header.block_size); | |
8937f822 | 455 | ret = -ENOTSUP; |
9aebd98a | 456 | goto fail; |
ac928b8e | 457 | } else if (!qemu_uuid_is_null(&uuid_link)) { |
5b7aa9b5 | 458 | error_setg(errp, "unsupported VDI image (non-NULL link UUID)"); |
8937f822 | 459 | ret = -ENOTSUP; |
9aebd98a | 460 | goto fail; |
ac928b8e | 461 | } else if (!qemu_uuid_is_null(&uuid_parent)) { |
5b7aa9b5 | 462 | error_setg(errp, "unsupported VDI image (non-NULL parent UUID)"); |
8937f822 | 463 | ret = -ENOTSUP; |
9aebd98a | 464 | goto fail; |
63fa06dc JC |
465 | } else if (header.blocks_in_image > VDI_BLOCKS_IN_IMAGE_MAX) { |
466 | error_setg(errp, "unsupported VDI image " | |
467 | "(too many blocks %u, max is %u)", | |
468 | header.blocks_in_image, VDI_BLOCKS_IN_IMAGE_MAX); | |
469 | ret = -ENOTSUP; | |
470 | goto fail; | |
9aebd98a SW |
471 | } |
472 | ||
473 | bs->total_sectors = header.disk_size / SECTOR_SIZE; | |
474 | ||
475 | s->block_size = header.block_size; | |
9aebd98a SW |
476 | s->bmap_sector = header.offset_bmap / SECTOR_SIZE; |
477 | s->header = header; | |
478 | ||
479 | bmap_size = header.blocks_in_image * sizeof(uint32_t); | |
e9082e47 | 480 | bmap_size = DIV_ROUND_UP(bmap_size, SECTOR_SIZE); |
9a4f4c31 | 481 | s->bmap = qemu_try_blockalign(bs->file->bs, bmap_size * SECTOR_SIZE); |
17cce735 KW |
482 | if (s->bmap == NULL) { |
483 | ret = -ENOMEM; | |
484 | goto fail; | |
485 | } | |
486 | ||
fbcbbf4e | 487 | ret = bdrv_read(bs->file, s->bmap_sector, (uint8_t *)s->bmap, |
9a4f4c31 | 488 | bmap_size); |
8937f822 | 489 | if (ret < 0) { |
9aebd98a SW |
490 | goto fail_free_bmap; |
491 | } | |
492 | ||
fc9d106c | 493 | /* Disable migration when vdi images are used */ |
81e5f78a AG |
494 | error_setg(&s->migration_blocker, "The vdi format used by node '%s' " |
495 | "does not support live migration", | |
496 | bdrv_get_device_or_node_name(bs)); | |
fe44dc91 AA |
497 | ret = migrate_add_blocker(s->migration_blocker, &local_err); |
498 | if (local_err) { | |
499 | error_propagate(errp, local_err); | |
500 | error_free(s->migration_blocker); | |
501 | goto fail_free_bmap; | |
502 | } | |
fc9d106c | 503 | |
1e886639 | 504 | qemu_co_rwlock_init(&s->bmap_lock); |
f0ab6f10 | 505 | |
9aebd98a SW |
506 | return 0; |
507 | ||
508 | fail_free_bmap: | |
17cce735 | 509 | qemu_vfree(s->bmap); |
9aebd98a SW |
510 | |
511 | fail: | |
8937f822 | 512 | return ret; |
9aebd98a SW |
513 | } |
514 | ||
ecfe2bba JC |
515 | static int vdi_reopen_prepare(BDRVReopenState *state, |
516 | BlockReopenQueue *queue, Error **errp) | |
517 | { | |
518 | return 0; | |
519 | } | |
520 | ||
67635f6a EB |
521 | static int coroutine_fn vdi_co_block_status(BlockDriverState *bs, |
522 | bool want_zero, | |
523 | int64_t offset, int64_t bytes, | |
524 | int64_t *pnum, int64_t *map, | |
525 | BlockDriverState **file) | |
9aebd98a | 526 | { |
9aebd98a | 527 | BDRVVdiState *s = (BDRVVdiState *)bs->opaque; |
67635f6a EB |
528 | size_t bmap_index = offset / s->block_size; |
529 | size_t index_in_block = offset % s->block_size; | |
9aebd98a | 530 | uint32_t bmap_entry = le32_to_cpu(s->bmap[bmap_index]); |
4bc74be9 PB |
531 | int result; |
532 | ||
67635f6a EB |
533 | logout("%p, %" PRId64 ", %" PRId64 ", %p\n", bs, offset, bytes, pnum); |
534 | *pnum = MIN(s->block_size - index_in_block, bytes); | |
4bc74be9 PB |
535 | result = VDI_IS_ALLOCATED(bmap_entry); |
536 | if (!result) { | |
537 | return 0; | |
538 | } | |
539 | ||
67635f6a EB |
540 | *map = s->header.offset_data + (uint64_t)bmap_entry * s->block_size + |
541 | index_in_block; | |
8bfb1371 | 542 | *file = bs->file->bs; |
67635f6a | 543 | return BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; |
9aebd98a SW |
544 | } |
545 | ||
0865bb6f KW |
546 | static int coroutine_fn |
547 | vdi_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
548 | QEMUIOVector *qiov, int flags) | |
9aebd98a | 549 | { |
9aebd98a | 550 | BDRVVdiState *s = bs->opaque; |
0865bb6f | 551 | QEMUIOVector local_qiov; |
9aebd98a SW |
552 | uint32_t bmap_entry; |
553 | uint32_t block_index; | |
0865bb6f KW |
554 | uint32_t offset_in_block; |
555 | uint32_t n_bytes; | |
556 | uint64_t bytes_done = 0; | |
eb9566d1 | 557 | int ret = 0; |
4de659e8 PB |
558 | |
559 | logout("\n"); | |
9aebd98a | 560 | |
0865bb6f | 561 | qemu_iovec_init(&local_qiov, qiov->niov); |
0c7bfc32 | 562 | |
0865bb6f KW |
563 | while (ret >= 0 && bytes > 0) { |
564 | block_index = offset / s->block_size; | |
565 | offset_in_block = offset % s->block_size; | |
566 | n_bytes = MIN(bytes, s->block_size - offset_in_block); | |
567 | ||
568 | logout("will read %u bytes starting at offset %" PRIu64 "\n", | |
569 | n_bytes, offset); | |
eb9566d1 PB |
570 | |
571 | /* prepare next AIO request */ | |
1e886639 | 572 | qemu_co_rwlock_rdlock(&s->bmap_lock); |
eb9566d1 | 573 | bmap_entry = le32_to_cpu(s->bmap[block_index]); |
1e886639 | 574 | qemu_co_rwlock_unlock(&s->bmap_lock); |
eb9566d1 PB |
575 | if (!VDI_IS_ALLOCATED(bmap_entry)) { |
576 | /* Block not allocated, return zeros, no need to wait. */ | |
0865bb6f | 577 | qemu_iovec_memset(qiov, bytes_done, 0, n_bytes); |
eb9566d1 PB |
578 | ret = 0; |
579 | } else { | |
0865bb6f KW |
580 | uint64_t data_offset = s->header.offset_data + |
581 | (uint64_t)bmap_entry * s->block_size + | |
582 | offset_in_block; | |
583 | ||
584 | qemu_iovec_reset(&local_qiov); | |
585 | qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); | |
586 | ||
a03ef88f | 587 | ret = bdrv_co_preadv(bs->file, data_offset, n_bytes, |
0865bb6f | 588 | &local_qiov, 0); |
eb9566d1 | 589 | } |
0865bb6f | 590 | logout("%u bytes read\n", n_bytes); |
0c7bfc32 | 591 | |
0865bb6f KW |
592 | bytes -= n_bytes; |
593 | offset += n_bytes; | |
594 | bytes_done += n_bytes; | |
9aebd98a | 595 | } |
3d46a75a | 596 | |
0865bb6f KW |
597 | qemu_iovec_destroy(&local_qiov); |
598 | ||
3d46a75a | 599 | return ret; |
9aebd98a SW |
600 | } |
601 | ||
fde9d56f KW |
602 | static int coroutine_fn |
603 | vdi_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
604 | QEMUIOVector *qiov, int flags) | |
9aebd98a | 605 | { |
9aebd98a | 606 | BDRVVdiState *s = bs->opaque; |
fde9d56f | 607 | QEMUIOVector local_qiov; |
9aebd98a SW |
608 | uint32_t bmap_entry; |
609 | uint32_t block_index; | |
fde9d56f KW |
610 | uint32_t offset_in_block; |
611 | uint32_t n_bytes; | |
1e886639 | 612 | uint64_t data_offset; |
bfc45fc1 PB |
613 | uint32_t bmap_first = VDI_UNALLOCATED; |
614 | uint32_t bmap_last = VDI_UNALLOCATED; | |
bfc45fc1 | 615 | uint8_t *block = NULL; |
fde9d56f | 616 | uint64_t bytes_done = 0; |
eb9566d1 | 617 | int ret = 0; |
4de659e8 PB |
618 | |
619 | logout("\n"); | |
9aebd98a | 620 | |
fde9d56f | 621 | qemu_iovec_init(&local_qiov, qiov->niov); |
9aebd98a | 622 | |
fde9d56f KW |
623 | while (ret >= 0 && bytes > 0) { |
624 | block_index = offset / s->block_size; | |
625 | offset_in_block = offset % s->block_size; | |
626 | n_bytes = MIN(bytes, s->block_size - offset_in_block); | |
627 | ||
628 | logout("will write %u bytes starting at offset %" PRIu64 "\n", | |
629 | n_bytes, offset); | |
eb9566d1 PB |
630 | |
631 | /* prepare next AIO request */ | |
1e886639 | 632 | qemu_co_rwlock_rdlock(&s->bmap_lock); |
eb9566d1 PB |
633 | bmap_entry = le32_to_cpu(s->bmap[block_index]); |
634 | if (!VDI_IS_ALLOCATED(bmap_entry)) { | |
635 | /* Allocate new block and write to it. */ | |
fde9d56f | 636 | uint64_t data_offset; |
1e886639 PB |
637 | qemu_co_rwlock_upgrade(&s->bmap_lock); |
638 | bmap_entry = le32_to_cpu(s->bmap[block_index]); | |
639 | if (VDI_IS_ALLOCATED(bmap_entry)) { | |
640 | /* A concurrent allocation did the work for us. */ | |
641 | qemu_co_rwlock_downgrade(&s->bmap_lock); | |
642 | goto nonallocating_write; | |
643 | } | |
644 | ||
eb9566d1 PB |
645 | bmap_entry = s->header.blocks_allocated; |
646 | s->bmap[block_index] = cpu_to_le32(bmap_entry); | |
647 | s->header.blocks_allocated++; | |
fde9d56f KW |
648 | data_offset = s->header.offset_data + |
649 | (uint64_t)bmap_entry * s->block_size; | |
eb9566d1 PB |
650 | if (block == NULL) { |
651 | block = g_malloc(s->block_size); | |
652 | bmap_first = block_index; | |
653 | } | |
654 | bmap_last = block_index; | |
655 | /* Copy data to be written to new block and zero unused parts. */ | |
fde9d56f KW |
656 | memset(block, 0, offset_in_block); |
657 | qemu_iovec_to_buf(qiov, bytes_done, block + offset_in_block, | |
658 | n_bytes); | |
659 | memset(block + offset_in_block + n_bytes, 0, | |
660 | s->block_size - n_bytes - offset_in_block); | |
f0ab6f10 | 661 | |
1e886639 PB |
662 | /* Write the new block under CoRwLock write-side protection, |
663 | * so this full-cluster write does not overlap a partial write | |
664 | * of the same cluster, issued from the "else" branch. | |
665 | */ | |
d9ca2ea2 | 666 | ret = bdrv_pwrite(bs->file, data_offset, block, s->block_size); |
1e886639 | 667 | qemu_co_rwlock_unlock(&s->bmap_lock); |
eb9566d1 | 668 | } else { |
1e886639 PB |
669 | nonallocating_write: |
670 | data_offset = s->header.offset_data + | |
671 | (uint64_t)bmap_entry * s->block_size + | |
672 | offset_in_block; | |
673 | qemu_co_rwlock_unlock(&s->bmap_lock); | |
fde9d56f KW |
674 | |
675 | qemu_iovec_reset(&local_qiov); | |
676 | qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); | |
677 | ||
a03ef88f | 678 | ret = bdrv_co_pwritev(bs->file, data_offset, n_bytes, |
fde9d56f | 679 | &local_qiov, 0); |
9aebd98a | 680 | } |
0c7bfc32 | 681 | |
fde9d56f KW |
682 | bytes -= n_bytes; |
683 | offset += n_bytes; | |
684 | bytes_done += n_bytes; | |
0c7bfc32 | 685 | |
fde9d56f | 686 | logout("%u bytes written\n", n_bytes); |
9aebd98a | 687 | } |
9aebd98a | 688 | |
fde9d56f KW |
689 | qemu_iovec_destroy(&local_qiov); |
690 | ||
0c7bfc32 | 691 | logout("finished data write\n"); |
4eea78e6 PB |
692 | if (ret < 0) { |
693 | return ret; | |
694 | } | |
695 | ||
696 | if (block) { | |
697 | /* One or more new blocks were allocated. */ | |
698 | VdiHeader *header = (VdiHeader *) block; | |
699 | uint8_t *base; | |
700 | uint64_t offset; | |
fde9d56f | 701 | uint32_t n_sectors; |
4eea78e6 PB |
702 | |
703 | logout("now writing modified header\n"); | |
704 | assert(VDI_IS_ALLOCATED(bmap_first)); | |
705 | *header = s->header; | |
706 | vdi_header_to_le(header); | |
18d51c4b | 707 | ret = bdrv_write(bs->file, 0, block, 1); |
bfc45fc1 PB |
708 | g_free(block); |
709 | block = NULL; | |
4eea78e6 PB |
710 | |
711 | if (ret < 0) { | |
712 | return ret; | |
0c7bfc32 | 713 | } |
4eea78e6 PB |
714 | |
715 | logout("now writing modified block map entry %u...%u\n", | |
716 | bmap_first, bmap_last); | |
717 | /* Write modified sectors from block map. */ | |
718 | bmap_first /= (SECTOR_SIZE / sizeof(uint32_t)); | |
719 | bmap_last /= (SECTOR_SIZE / sizeof(uint32_t)); | |
720 | n_sectors = bmap_last - bmap_first + 1; | |
721 | offset = s->bmap_sector + bmap_first; | |
722 | base = ((uint8_t *)&s->bmap[0]) + bmap_first * SECTOR_SIZE; | |
723 | logout("will write %u block map sectors starting from entry %u\n", | |
724 | n_sectors, bmap_first); | |
18d51c4b | 725 | ret = bdrv_write(bs->file, offset, base, n_sectors); |
0c7bfc32 PB |
726 | } |
727 | ||
3d46a75a | 728 | return ret; |
9aebd98a SW |
729 | } |
730 | ||
e3810574 | 731 | static int coroutine_fn vdi_co_do_create(BlockdevCreateOptions *create_options, |
49858b50 | 732 | size_t block_size, Error **errp) |
9aebd98a | 733 | { |
e3810574 | 734 | BlockdevCreateOptionsVdi *vdi_opts; |
dddc7750 | 735 | int ret = 0; |
9aebd98a SW |
736 | uint64_t bytes = 0; |
737 | uint32_t blocks; | |
61fa6487 | 738 | uint32_t image_type; |
9aebd98a SW |
739 | VdiHeader header; |
740 | size_t i; | |
741 | size_t bmap_size; | |
70747862 | 742 | int64_t offset = 0; |
ec73f060 | 743 | BlockDriverState *bs_file = NULL; |
a08f0c3b | 744 | BlockBackend *blk = NULL; |
70747862 | 745 | uint32_t *bmap = NULL; |
ac928b8e | 746 | QemuUUID uuid; |
9aebd98a | 747 | |
e3810574 HR |
748 | assert(create_options->driver == BLOCKDEV_DRIVER_VDI); |
749 | vdi_opts = &create_options->u.vdi; | |
750 | ||
9aebd98a SW |
751 | logout("\n"); |
752 | ||
da23248f | 753 | /* Validate options and set default values */ |
49858b50 | 754 | bytes = vdi_opts->size; |
61fa6487 KW |
755 | |
756 | if (!vdi_opts->has_preallocation) { | |
757 | vdi_opts->preallocation = PREALLOC_MODE_OFF; | |
758 | } | |
759 | switch (vdi_opts->preallocation) { | |
760 | case PREALLOC_MODE_OFF: | |
761 | image_type = VDI_TYPE_DYNAMIC; | |
762 | break; | |
763 | case PREALLOC_MODE_METADATA: | |
004b7f25 | 764 | image_type = VDI_TYPE_STATIC; |
61fa6487 KW |
765 | break; |
766 | default: | |
767 | error_setg(errp, "Preallocation mode not supported for vdi"); | |
768 | return -EINVAL; | |
9aebd98a | 769 | } |
61fa6487 | 770 | |
49858b50 HR |
771 | #ifndef CONFIG_VDI_STATIC_IMAGE |
772 | if (image_type == VDI_TYPE_STATIC) { | |
773 | ret = -ENOTSUP; | |
774 | error_setg(errp, "Statically allocated images cannot be created in " | |
775 | "this build"); | |
776 | goto exit; | |
777 | } | |
778 | #endif | |
779 | #ifndef CONFIG_VDI_BLOCK_SIZE | |
780 | if (block_size != DEFAULT_CLUSTER_SIZE) { | |
781 | ret = -ENOTSUP; | |
782 | error_setg(errp, | |
783 | "A non-default cluster size is not supported in this build"); | |
784 | goto exit; | |
785 | } | |
004b7f25 | 786 | #endif |
9aebd98a | 787 | |
63fa06dc | 788 | if (bytes > VDI_DISK_SIZE_MAX) { |
dddc7750 | 789 | ret = -ENOTSUP; |
63fa06dc JC |
790 | error_setg(errp, "Unsupported VDI image size (size is 0x%" PRIx64 |
791 | ", max supported is 0x%" PRIx64 ")", | |
792 | bytes, VDI_DISK_SIZE_MAX); | |
793 | goto exit; | |
794 | } | |
795 | ||
da23248f | 796 | /* Create BlockBackend to write to the image */ |
ec73f060 HR |
797 | bs_file = bdrv_open_blockdev_ref(vdi_opts->file, errp); |
798 | if (!bs_file) { | |
799 | ret = -EIO; | |
63fa06dc | 800 | goto exit; |
9aebd98a | 801 | } |
a08f0c3b | 802 | |
ec73f060 HR |
803 | blk = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); |
804 | ret = blk_insert_bs(blk, bs_file, errp); | |
805 | if (ret < 0) { | |
70747862 | 806 | goto exit; |
4ab15590 CL |
807 | } |
808 | ||
a08f0c3b KW |
809 | blk_set_allow_write_beyond_eof(blk, true); |
810 | ||
f21dc3a4 SW |
811 | /* We need enough blocks to store the given disk size, |
812 | so always round up. */ | |
e9082e47 | 813 | blocks = DIV_ROUND_UP(bytes, block_size); |
f21dc3a4 | 814 | |
9aebd98a | 815 | bmap_size = blocks * sizeof(uint32_t); |
e9082e47 | 816 | bmap_size = ROUND_UP(bmap_size, SECTOR_SIZE); |
9aebd98a SW |
817 | |
818 | memset(&header, 0, sizeof(header)); | |
1786dc15 | 819 | pstrcpy(header.text, sizeof(header.text), VDI_TEXT); |
9aebd98a SW |
820 | header.signature = VDI_SIGNATURE; |
821 | header.version = VDI_VERSION_1_1; | |
822 | header.header_size = 0x180; | |
823 | header.image_type = image_type; | |
824 | header.offset_bmap = 0x200; | |
825 | header.offset_data = 0x200 + bmap_size; | |
826 | header.sector_size = SECTOR_SIZE; | |
827 | header.disk_size = bytes; | |
828 | header.block_size = block_size; | |
829 | header.blocks_in_image = blocks; | |
6eea90eb SW |
830 | if (image_type == VDI_TYPE_STATIC) { |
831 | header.blocks_allocated = blocks; | |
832 | } | |
ac928b8e PM |
833 | qemu_uuid_generate(&uuid); |
834 | header.uuid_image = uuid; | |
835 | qemu_uuid_generate(&uuid); | |
836 | header.uuid_last_snap = uuid; | |
9aebd98a | 837 | /* There is no need to set header.uuid_link or header.uuid_parent here. */ |
95a14d51 KW |
838 | if (VDI_DEBUG) { |
839 | vdi_header_print(&header); | |
840 | } | |
9aebd98a | 841 | vdi_header_to_le(&header); |
8341f00d | 842 | ret = blk_pwrite(blk, offset, &header, sizeof(header), 0); |
dddc7750 | 843 | if (ret < 0) { |
ec73f060 | 844 | error_setg(errp, "Error writing header"); |
70747862 | 845 | goto exit; |
9aebd98a | 846 | } |
70747862 | 847 | offset += sizeof(header); |
9aebd98a | 848 | |
b76b6e95 | 849 | if (bmap_size > 0) { |
17cce735 KW |
850 | bmap = g_try_malloc0(bmap_size); |
851 | if (bmap == NULL) { | |
852 | ret = -ENOMEM; | |
853 | error_setg(errp, "Could not allocate bmap"); | |
854 | goto exit; | |
855 | } | |
514f21a5 SW |
856 | for (i = 0; i < blocks; i++) { |
857 | if (image_type == VDI_TYPE_STATIC) { | |
858 | bmap[i] = i; | |
859 | } else { | |
860 | bmap[i] = VDI_UNALLOCATED; | |
861 | } | |
9aebd98a | 862 | } |
8341f00d | 863 | ret = blk_pwrite(blk, offset, bmap, bmap_size, 0); |
dddc7750 | 864 | if (ret < 0) { |
ec73f060 | 865 | error_setg(errp, "Error writing bmap"); |
70747862 | 866 | goto exit; |
514f21a5 | 867 | } |
70747862 | 868 | offset += bmap_size; |
9aebd98a | 869 | } |
514f21a5 | 870 | |
9aebd98a | 871 | if (image_type == VDI_TYPE_STATIC) { |
3a691c50 HR |
872 | ret = blk_truncate(blk, offset + blocks * block_size, |
873 | PREALLOC_MODE_OFF, errp); | |
dddc7750 | 874 | if (ret < 0) { |
ec73f060 | 875 | error_prepend(errp, "Failed to statically allocate file"); |
70747862 | 876 | goto exit; |
9aebd98a SW |
877 | } |
878 | } | |
879 | ||
53618dd8 | 880 | ret = 0; |
63fa06dc | 881 | exit: |
a08f0c3b | 882 | blk_unref(blk); |
ec73f060 | 883 | bdrv_unref(bs_file); |
70747862 | 884 | g_free(bmap); |
dddc7750 | 885 | return ret; |
9aebd98a SW |
886 | } |
887 | ||
e3810574 HR |
888 | static int coroutine_fn vdi_co_create(BlockdevCreateOptions *create_options, |
889 | Error **errp) | |
890 | { | |
891 | return vdi_co_do_create(create_options, DEFAULT_CLUSTER_SIZE, errp); | |
892 | } | |
893 | ||
49858b50 HR |
894 | static int coroutine_fn vdi_co_create_opts(const char *filename, QemuOpts *opts, |
895 | Error **errp) | |
896 | { | |
897 | QDict *qdict = NULL; | |
e3810574 | 898 | BlockdevCreateOptions *create_options = NULL; |
ec73f060 | 899 | BlockDriverState *bs_file = NULL; |
49858b50 | 900 | uint64_t block_size = DEFAULT_CLUSTER_SIZE; |
61fa6487 | 901 | bool is_static = false; |
49858b50 HR |
902 | Visitor *v; |
903 | Error *local_err = NULL; | |
904 | int ret; | |
905 | ||
da23248f KW |
906 | /* Parse options and convert legacy syntax. |
907 | * | |
908 | * Since CONFIG_VDI_BLOCK_SIZE is disabled by default, | |
49858b50 HR |
909 | * cluster-size is not part of the QAPI schema; therefore we have |
910 | * to parse it before creating the QAPI object. */ | |
911 | #if defined(CONFIG_VDI_BLOCK_SIZE) | |
912 | block_size = qemu_opt_get_size_del(opts, | |
913 | BLOCK_OPT_CLUSTER_SIZE, | |
914 | DEFAULT_CLUSTER_SIZE); | |
915 | if (block_size < BDRV_SECTOR_SIZE || block_size > UINT32_MAX || | |
916 | !is_power_of_2(block_size)) | |
917 | { | |
918 | error_setg(errp, "Invalid cluster size"); | |
919 | ret = -EINVAL; | |
920 | goto done; | |
921 | } | |
922 | #endif | |
61fa6487 KW |
923 | if (qemu_opt_get_bool_del(opts, BLOCK_OPT_STATIC, false)) { |
924 | is_static = true; | |
925 | } | |
49858b50 HR |
926 | |
927 | qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vdi_create_opts, true); | |
928 | ||
da23248f | 929 | /* Create and open the file (protocol layer) */ |
ec73f060 HR |
930 | ret = bdrv_create_file(filename, opts, errp); |
931 | if (ret < 0) { | |
932 | goto done; | |
933 | } | |
934 | ||
935 | bs_file = bdrv_open(filename, NULL, NULL, | |
936 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); | |
937 | if (!bs_file) { | |
938 | ret = -EIO; | |
939 | goto done; | |
940 | } | |
941 | ||
e3810574 | 942 | qdict_put_str(qdict, "driver", "vdi"); |
ec73f060 | 943 | qdict_put_str(qdict, "file", bs_file->node_name); |
61fa6487 KW |
944 | if (is_static) { |
945 | qdict_put_str(qdict, "preallocation", "metadata"); | |
946 | } | |
49858b50 HR |
947 | |
948 | /* Get the QAPI object */ | |
f853465a MA |
949 | v = qobject_input_visitor_new_flat_confused(qdict, errp); |
950 | if (!v) { | |
951 | ret = -EINVAL; | |
952 | goto done; | |
953 | } | |
e3810574 | 954 | visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); |
49858b50 HR |
955 | visit_free(v); |
956 | ||
957 | if (local_err) { | |
958 | error_propagate(errp, local_err); | |
959 | ret = -EINVAL; | |
960 | goto done; | |
961 | } | |
962 | ||
da23248f | 963 | /* Silently round up size */ |
e3810574 HR |
964 | assert(create_options->driver == BLOCKDEV_DRIVER_VDI); |
965 | create_options->u.vdi.size = ROUND_UP(create_options->u.vdi.size, | |
966 | BDRV_SECTOR_SIZE); | |
49858b50 | 967 | |
da23248f | 968 | /* Create the vdi image (format layer) */ |
ec73f060 | 969 | ret = vdi_co_do_create(create_options, block_size, errp); |
49858b50 | 970 | done: |
cb3e7f08 | 971 | qobject_unref(qdict); |
e3810574 | 972 | qapi_free_BlockdevCreateOptions(create_options); |
ec73f060 | 973 | bdrv_unref(bs_file); |
49858b50 HR |
974 | return ret; |
975 | } | |
976 | ||
9aebd98a SW |
977 | static void vdi_close(BlockDriverState *bs) |
978 | { | |
fc9d106c | 979 | BDRVVdiState *s = bs->opaque; |
6ac5f388 | 980 | |
17cce735 | 981 | qemu_vfree(s->bmap); |
6ac5f388 | 982 | |
fc9d106c KW |
983 | migrate_del_blocker(s->migration_blocker); |
984 | error_free(s->migration_blocker); | |
9aebd98a SW |
985 | } |
986 | ||
004b7f25 CL |
987 | static QemuOptsList vdi_create_opts = { |
988 | .name = "vdi-create-opts", | |
989 | .head = QTAILQ_HEAD_INITIALIZER(vdi_create_opts.head), | |
990 | .desc = { | |
991 | { | |
992 | .name = BLOCK_OPT_SIZE, | |
993 | .type = QEMU_OPT_SIZE, | |
994 | .help = "Virtual disk size" | |
995 | }, | |
9aebd98a | 996 | #if defined(CONFIG_VDI_BLOCK_SIZE) |
004b7f25 CL |
997 | { |
998 | .name = BLOCK_OPT_CLUSTER_SIZE, | |
999 | .type = QEMU_OPT_SIZE, | |
1000 | .help = "VDI cluster (block) size", | |
1001 | .def_value_str = stringify(DEFAULT_CLUSTER_SIZE) | |
1002 | }, | |
9aebd98a SW |
1003 | #endif |
1004 | #if defined(CONFIG_VDI_STATIC_IMAGE) | |
004b7f25 CL |
1005 | { |
1006 | .name = BLOCK_OPT_STATIC, | |
1007 | .type = QEMU_OPT_BOOL, | |
1008 | .help = "VDI static (pre-allocated) image", | |
1009 | .def_value_str = "off" | |
1010 | }, | |
9aebd98a | 1011 | #endif |
004b7f25 CL |
1012 | /* TODO: An additional option to set UUID values might be useful. */ |
1013 | { /* end of list */ } | |
1014 | } | |
9aebd98a SW |
1015 | }; |
1016 | ||
1017 | static BlockDriver bdrv_vdi = { | |
1018 | .format_name = "vdi", | |
1019 | .instance_size = sizeof(BDRVVdiState), | |
1020 | .bdrv_probe = vdi_probe, | |
1021 | .bdrv_open = vdi_open, | |
1022 | .bdrv_close = vdi_close, | |
ecfe2bba | 1023 | .bdrv_reopen_prepare = vdi_reopen_prepare, |
862f215f | 1024 | .bdrv_child_perm = bdrv_format_default_perms, |
e3810574 | 1025 | .bdrv_co_create = vdi_co_create, |
da23248f | 1026 | .bdrv_co_create_opts = vdi_co_create_opts, |
3ac21627 | 1027 | .bdrv_has_zero_init = bdrv_has_zero_init_1, |
67635f6a | 1028 | .bdrv_co_block_status = vdi_co_block_status, |
9aebd98a SW |
1029 | .bdrv_make_empty = vdi_make_empty, |
1030 | ||
0865bb6f | 1031 | .bdrv_co_preadv = vdi_co_preadv, |
9aebd98a | 1032 | #if defined(CONFIG_VDI_WRITE) |
fde9d56f | 1033 | .bdrv_co_pwritev = vdi_co_pwritev, |
9aebd98a SW |
1034 | #endif |
1035 | ||
1036 | .bdrv_get_info = vdi_get_info, | |
1037 | ||
004b7f25 | 1038 | .create_opts = &vdi_create_opts, |
2fd61638 | 1039 | .bdrv_co_check = vdi_co_check, |
9aebd98a SW |
1040 | }; |
1041 | ||
1042 | static void bdrv_vdi_init(void) | |
1043 | { | |
1044 | logout("\n"); | |
1045 | bdrv_register(&bdrv_vdi); | |
1046 | } | |
1047 | ||
1048 | block_init(bdrv_vdi_init); |