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