]>
Commit | Line | Data |
---|---|---|
6a0f9e82 | 1 | /* |
cc2040f8 | 2 | * Block driver for Connectix / Microsoft Virtual PC images |
5fafdf24 | 3 | * |
6a0f9e82 | 4 | * Copyright (c) 2005 Alex Beregszaszi |
15d35bc5 | 5 | * Copyright (c) 2009 Kevin Wolf <[email protected]> |
5fafdf24 | 6 | * |
6a0f9e82 FB |
7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
8 | * of this software and associated documentation files (the "Software"), to deal | |
9 | * in the Software without restriction, including without limitation the rights | |
10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
11 | * copies of the Software, and to permit persons to whom the Software is | |
12 | * furnished to do so, subject to the following conditions: | |
13 | * | |
14 | * The above copyright notice and this permission notice shall be included in | |
15 | * all copies or substantial portions of the Software. | |
16 | * | |
17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
23 | * THE SOFTWARE. | |
24 | */ | |
922a01a0 | 25 | |
80c71a24 | 26 | #include "qemu/osdep.h" |
da34e65c | 27 | #include "qapi/error.h" |
737e150e | 28 | #include "block/block_int.h" |
609f45ea | 29 | #include "block/qdict.h" |
b8f45cdf | 30 | #include "sysemu/block-backend.h" |
1de7afc9 | 31 | #include "qemu/module.h" |
922a01a0 | 32 | #include "qemu/option.h" |
795c40b8 | 33 | #include "migration/blocker.h" |
58369e22 | 34 | #include "qemu/bswap.h" |
38440a21 | 35 | #include "qemu/uuid.h" |
182c8835 KW |
36 | #include "qapi/qmp/qdict.h" |
37 | #include "qapi/qobject-input-visitor.h" | |
38 | #include "qapi/qapi-visit-block-core.h" | |
6a0f9e82 FB |
39 | |
40 | /**************************************************************/ | |
41 | ||
42 | #define HEADER_SIZE 512 | |
43 | ||
44 | //#define CACHE | |
45 | ||
2cfacb62 AL |
46 | enum vhd_type { |
47 | VHD_FIXED = 2, | |
48 | VHD_DYNAMIC = 3, | |
49 | VHD_DIFFERENCING = 4, | |
50 | }; | |
51 | ||
9c057d0b | 52 | /* Seconds since Jan 1, 2000 0:00:00 (UTC) */ |
57c7d9e5 AL |
53 | #define VHD_TIMESTAMP_BASE 946684800 |
54 | ||
fb9245c2 JC |
55 | #define VHD_CHS_MAX_C 65535LL |
56 | #define VHD_CHS_MAX_H 16 | |
57 | #define VHD_CHS_MAX_S 255 | |
58 | ||
c23fb11b | 59 | #define VHD_MAX_SECTORS 0xff000000 /* 2040 GiB max image size */ |
fb9245c2 JC |
60 | #define VHD_MAX_GEOMETRY (VHD_CHS_MAX_C * VHD_CHS_MAX_H * VHD_CHS_MAX_S) |
61 | ||
62 | #define VPC_OPT_FORCE_SIZE "force_size" | |
97f1c45c | 63 | |
9c057d0b | 64 | /* always big-endian */ |
e54835c0 | 65 | typedef struct vhd_footer { |
9c057d0b | 66 | char creator[8]; /* "conectix" */ |
2cfacb62 AL |
67 | uint32_t features; |
68 | uint32_t version; | |
69 | ||
9c057d0b | 70 | /* Offset of next header structure, 0xFFFFFFFF if none */ |
2cfacb62 AL |
71 | uint64_t data_offset; |
72 | ||
9c057d0b | 73 | /* Seconds since Jan 1, 2000 0:00:00 (UTC) */ |
2cfacb62 AL |
74 | uint32_t timestamp; |
75 | ||
9c057d0b | 76 | char creator_app[4]; /* e.g., "vpc " */ |
2cfacb62 AL |
77 | uint16_t major; |
78 | uint16_t minor; | |
9c057d0b | 79 | char creator_os[4]; /* "Wi2k" */ |
2cfacb62 AL |
80 | |
81 | uint64_t orig_size; | |
03671ded | 82 | uint64_t current_size; |
2cfacb62 AL |
83 | |
84 | uint16_t cyls; | |
85 | uint8_t heads; | |
86 | uint8_t secs_per_cyl; | |
87 | ||
88 | uint32_t type; | |
89 | ||
9c057d0b JC |
90 | /* Checksum of the Hard Disk Footer ("one's complement of the sum of all |
91 | the bytes in the footer without the checksum field") */ | |
2cfacb62 AL |
92 | uint32_t checksum; |
93 | ||
9c057d0b | 94 | /* UUID used to identify a parent hard disk (backing file) */ |
38440a21 | 95 | QemuUUID uuid; |
2cfacb62 AL |
96 | |
97 | uint8_t in_saved_state; | |
e54835c0 | 98 | } QEMU_PACKED VHDFooter; |
b9fa33a6 | 99 | |
e54835c0 | 100 | typedef struct vhd_dyndisk_header { |
9c057d0b | 101 | char magic[8]; /* "cxsparse" */ |
2cfacb62 | 102 | |
9c057d0b | 103 | /* Offset of next header structure, 0xFFFFFFFF if none */ |
2cfacb62 AL |
104 | uint64_t data_offset; |
105 | ||
9c057d0b | 106 | /* Offset of the Block Allocation Table (BAT) */ |
2cfacb62 AL |
107 | uint64_t table_offset; |
108 | ||
109 | uint32_t version; | |
9c057d0b | 110 | uint32_t max_table_entries; /* 32bit/entry */ |
2cfacb62 | 111 | |
9c057d0b | 112 | /* 2 MB by default, must be a power of two */ |
2cfacb62 AL |
113 | uint32_t block_size; |
114 | ||
115 | uint32_t checksum; | |
116 | uint8_t parent_uuid[16]; | |
117 | uint32_t parent_timestamp; | |
118 | uint32_t reserved; | |
119 | ||
9c057d0b | 120 | /* Backing file name (in UTF-16) */ |
2cfacb62 AL |
121 | uint8_t parent_name[512]; |
122 | ||
123 | struct { | |
124 | uint32_t platform; | |
125 | uint32_t data_space; | |
126 | uint32_t data_length; | |
127 | uint32_t reserved; | |
128 | uint64_t data_offset; | |
129 | } parent_locator[8]; | |
e54835c0 | 130 | } QEMU_PACKED VHDDynDiskHeader; |
6a0f9e82 FB |
131 | |
132 | typedef struct BDRVVPCState { | |
848c66e8 | 133 | CoMutex lock; |
15d35bc5 AL |
134 | uint8_t footer_buf[HEADER_SIZE]; |
135 | uint64_t free_data_block_offset; | |
2cfacb62 | 136 | int max_table_entries; |
6a0f9e82 | 137 | uint32_t *pagetable; |
15d35bc5 AL |
138 | uint64_t bat_offset; |
139 | uint64_t last_bitmap_offset; | |
6a0f9e82 | 140 | |
2cfacb62 | 141 | uint32_t block_size; |
15d35bc5 | 142 | uint32_t bitmap_size; |
c540d53a JC |
143 | bool force_use_chs; |
144 | bool force_use_sz; | |
15d35bc5 | 145 | |
6a0f9e82 FB |
146 | #ifdef CACHE |
147 | uint8_t *pageentry_u8; | |
148 | uint32_t *pageentry_u32; | |
149 | uint16_t *pageentry_u16; | |
3b46e624 | 150 | |
6a0f9e82 FB |
151 | uint64_t last_bitmap; |
152 | #endif | |
612ff3d8 KW |
153 | |
154 | Error *migration_blocker; | |
6a0f9e82 FB |
155 | } BDRVVPCState; |
156 | ||
c540d53a JC |
157 | #define VPC_OPT_SIZE_CALC "force_size_calc" |
158 | static QemuOptsList vpc_runtime_opts = { | |
159 | .name = "vpc-runtime-opts", | |
160 | .head = QTAILQ_HEAD_INITIALIZER(vpc_runtime_opts.head), | |
161 | .desc = { | |
162 | { | |
163 | .name = VPC_OPT_SIZE_CALC, | |
164 | .type = QEMU_OPT_STRING, | |
165 | .help = "Force disk size calculation to use either CHS geometry, " | |
166 | "or use the disk current_size specified in the VHD footer. " | |
167 | "{chs, current_size}" | |
168 | }, | |
169 | { /* end of list */ } | |
170 | } | |
171 | }; | |
172 | ||
182c8835 KW |
173 | static QemuOptsList vpc_create_opts; |
174 | ||
57c7d9e5 AL |
175 | static uint32_t vpc_checksum(uint8_t* buf, size_t size) |
176 | { | |
177 | uint32_t res = 0; | |
178 | int i; | |
179 | ||
180 | for (i = 0; i < size; i++) | |
181 | res += buf[i]; | |
182 | ||
183 | return ~res; | |
184 | } | |
185 | ||
186 | ||
6a0f9e82 FB |
187 | static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename) |
188 | { | |
ffe8ab83 | 189 | if (buf_size >= 8 && !strncmp((char *)buf, "conectix", 8)) |
6a0f9e82 | 190 | return 100; |
6a0f9e82 FB |
191 | return 0; |
192 | } | |
193 | ||
c540d53a JC |
194 | static void vpc_parse_options(BlockDriverState *bs, QemuOpts *opts, |
195 | Error **errp) | |
196 | { | |
197 | BDRVVPCState *s = bs->opaque; | |
198 | const char *size_calc; | |
199 | ||
200 | size_calc = qemu_opt_get(opts, VPC_OPT_SIZE_CALC); | |
201 | ||
202 | if (!size_calc) { | |
203 | /* no override, use autodetect only */ | |
204 | } else if (!strcmp(size_calc, "current_size")) { | |
205 | s->force_use_sz = true; | |
206 | } else if (!strcmp(size_calc, "chs")) { | |
207 | s->force_use_chs = true; | |
208 | } else { | |
209 | error_setg(errp, "Invalid size calculation mode: '%s'", size_calc); | |
210 | } | |
211 | } | |
212 | ||
015a1036 HR |
213 | static int vpc_open(BlockDriverState *bs, QDict *options, int flags, |
214 | Error **errp) | |
6a0f9e82 FB |
215 | { |
216 | BDRVVPCState *s = bs->opaque; | |
66f82cee | 217 | int i; |
e54835c0 JC |
218 | VHDFooter *footer; |
219 | VHDDynDiskHeader *dyndisk_header; | |
c540d53a JC |
220 | QemuOpts *opts = NULL; |
221 | Error *local_err = NULL; | |
222 | bool use_chs; | |
b9fa33a6 | 223 | uint8_t buf[HEADER_SIZE]; |
57c7d9e5 | 224 | uint32_t checksum; |
97f1c45c | 225 | uint64_t computed_size; |
b15deac7 | 226 | uint64_t pagetable_size; |
24da78db | 227 | int disk_type = VHD_DYNAMIC; |
59294e46 | 228 | int ret; |
81caa3cc | 229 | int64_t bs_size; |
6a0f9e82 | 230 | |
4e4bf5c4 KW |
231 | bs->file = bdrv_open_child(NULL, options, "file", bs, &child_file, |
232 | false, errp); | |
233 | if (!bs->file) { | |
234 | return -EINVAL; | |
235 | } | |
236 | ||
c540d53a JC |
237 | opts = qemu_opts_create(&vpc_runtime_opts, NULL, 0, &error_abort); |
238 | qemu_opts_absorb_qdict(opts, options, &local_err); | |
239 | if (local_err) { | |
240 | error_propagate(errp, local_err); | |
241 | ret = -EINVAL; | |
242 | goto fail; | |
243 | } | |
244 | ||
245 | vpc_parse_options(bs, opts, &local_err); | |
246 | if (local_err) { | |
247 | error_propagate(errp, local_err); | |
248 | ret = -EINVAL; | |
249 | goto fail; | |
250 | } | |
251 | ||
cf2ab8fc | 252 | ret = bdrv_pread(bs->file, 0, s->footer_buf, HEADER_SIZE); |
59294e46 | 253 | if (ret < 0) { |
32f6439c | 254 | error_setg(errp, "Unable to read VHD header"); |
6a0f9e82 | 255 | goto fail; |
59294e46 | 256 | } |
6a0f9e82 | 257 | |
e54835c0 | 258 | footer = (VHDFooter *) s->footer_buf; |
24da78db | 259 | if (strncmp(footer->creator, "conectix", 8)) { |
9a4f4c31 | 260 | int64_t offset = bdrv_getlength(bs->file->bs); |
59294e46 KW |
261 | if (offset < 0) { |
262 | ret = offset; | |
32f6439c | 263 | error_setg(errp, "Invalid file size"); |
59294e46 KW |
264 | goto fail; |
265 | } else if (offset < HEADER_SIZE) { | |
266 | ret = -EINVAL; | |
32f6439c | 267 | error_setg(errp, "File too small for a VHD header"); |
24da78db CA |
268 | goto fail; |
269 | } | |
59294e46 | 270 | |
24da78db | 271 | /* If a fixed disk, the footer is found only at the end of the file */ |
cf2ab8fc | 272 | ret = bdrv_pread(bs->file, offset-HEADER_SIZE, s->footer_buf, |
59294e46 KW |
273 | HEADER_SIZE); |
274 | if (ret < 0) { | |
24da78db CA |
275 | goto fail; |
276 | } | |
277 | if (strncmp(footer->creator, "conectix", 8)) { | |
76abe407 PB |
278 | error_setg(errp, "invalid VPC image"); |
279 | ret = -EINVAL; | |
24da78db CA |
280 | goto fail; |
281 | } | |
282 | disk_type = VHD_FIXED; | |
283 | } | |
6a0f9e82 | 284 | |
57c7d9e5 AL |
285 | checksum = be32_to_cpu(footer->checksum); |
286 | footer->checksum = 0; | |
287 | if (vpc_checksum(s->footer_buf, HEADER_SIZE) != checksum) | |
288 | fprintf(stderr, "block-vpc: The header checksum of '%s' is " | |
66f82cee | 289 | "incorrect.\n", bs->filename); |
57c7d9e5 | 290 | |
c088b691 | 291 | /* Write 'checksum' back to footer, or else will leave it with zero. */ |
a4127c42 | 292 | footer->checksum = cpu_to_be32(checksum); |
c088b691 | 293 | |
9c057d0b JC |
294 | /* The visible size of a image in Virtual PC depends on the geometry |
295 | rather than on the size stored in the footer (the size in the footer | |
296 | is too large usually) */ | |
33ccf667 SH |
297 | bs->total_sectors = (int64_t) |
298 | be16_to_cpu(footer->cyls) * footer->heads * footer->secs_per_cyl; | |
1fa79228 | 299 | |
c540d53a JC |
300 | /* Microsoft Virtual PC and Microsoft Hyper-V produce and read |
301 | * VHD image sizes differently. VPC will rely on CHS geometry, | |
302 | * while Hyper-V and disk2vhd use the size specified in the footer. | |
303 | * | |
304 | * We use a couple of approaches to try and determine the correct method: | |
305 | * look at the Creator App field, and look for images that have CHS | |
306 | * geometry that is the maximum value. | |
307 | * | |
308 | * If the CHS geometry is the maximum CHS geometry, then we assume that | |
309 | * the size is the footer->current_size to avoid truncation. Otherwise, | |
310 | * we follow the table based on footer->creator_app: | |
311 | * | |
312 | * Known creator apps: | |
313 | * 'vpc ' : CHS Virtual PC (uses disk geometry) | |
314 | * 'qemu' : CHS QEMU (uses disk geometry) | |
fb9245c2 | 315 | * 'qem2' : current_size QEMU (uses current_size) |
c540d53a JC |
316 | * 'win ' : current_size Hyper-V |
317 | * 'd2v ' : current_size Disk2vhd | |
9bdfb9e8 | 318 | * 'tap\0' : current_size XenServer |
bab246db | 319 | * 'CTXS' : current_size XenConverter |
c540d53a JC |
320 | * |
321 | * The user can override the table values via drive options, however | |
322 | * even with an override we will still use current_size for images | |
323 | * that have CHS geometry of the maximum size. | |
324 | */ | |
325 | use_chs = (!!strncmp(footer->creator_app, "win ", 4) && | |
fb9245c2 | 326 | !!strncmp(footer->creator_app, "qem2", 4) && |
9bdfb9e8 | 327 | !!strncmp(footer->creator_app, "d2v ", 4) && |
bab246db | 328 | !!strncmp(footer->creator_app, "CTXS", 4) && |
9bdfb9e8 | 329 | !!memcmp(footer->creator_app, "tap", 4)) || s->force_use_chs; |
c540d53a JC |
330 | |
331 | if (!use_chs || bs->total_sectors == VHD_MAX_GEOMETRY || s->force_use_sz) { | |
03671ded | 332 | bs->total_sectors = be64_to_cpu(footer->current_size) / |
c540d53a | 333 | BDRV_SECTOR_SIZE; |
0173e7bb PL |
334 | } |
335 | ||
c23fb11b JC |
336 | /* Allow a maximum disk size of 2040 GiB */ |
337 | if (bs->total_sectors > VHD_MAX_SECTORS) { | |
59294e46 | 338 | ret = -EFBIG; |
efc8243d SH |
339 | goto fail; |
340 | } | |
341 | ||
24da78db | 342 | if (disk_type == VHD_DYNAMIC) { |
cf2ab8fc | 343 | ret = bdrv_pread(bs->file, be64_to_cpu(footer->data_offset), buf, |
59294e46 KW |
344 | HEADER_SIZE); |
345 | if (ret < 0) { | |
32f6439c | 346 | error_setg(errp, "Error reading dynamic VHD header"); |
24da78db CA |
347 | goto fail; |
348 | } | |
b9fa33a6 | 349 | |
e54835c0 | 350 | dyndisk_header = (VHDDynDiskHeader *) buf; |
6a0f9e82 | 351 | |
24da78db | 352 | if (strncmp(dyndisk_header->magic, "cxsparse", 8)) { |
32f6439c | 353 | error_setg(errp, "Invalid header magic"); |
59294e46 | 354 | ret = -EINVAL; |
24da78db CA |
355 | goto fail; |
356 | } | |
6a0f9e82 | 357 | |
24da78db | 358 | s->block_size = be32_to_cpu(dyndisk_header->block_size); |
5e71dfad KW |
359 | if (!is_power_of_2(s->block_size) || s->block_size < BDRV_SECTOR_SIZE) { |
360 | error_setg(errp, "Invalid block size %" PRIu32, s->block_size); | |
361 | ret = -EINVAL; | |
362 | goto fail; | |
363 | } | |
24da78db | 364 | s->bitmap_size = ((s->block_size / (8 * 512)) + 511) & ~511; |
15d35bc5 | 365 | |
24da78db | 366 | s->max_table_entries = be32_to_cpu(dyndisk_header->max_table_entries); |
97f1c45c JC |
367 | |
368 | if ((bs->total_sectors * 512) / s->block_size > 0xffffffffU) { | |
32f6439c | 369 | error_setg(errp, "Too many blocks"); |
97f1c45c JC |
370 | ret = -EINVAL; |
371 | goto fail; | |
372 | } | |
97f1c45c JC |
373 | |
374 | computed_size = (uint64_t) s->max_table_entries * s->block_size; | |
375 | if (computed_size < bs->total_sectors * 512) { | |
32f6439c | 376 | error_setg(errp, "Page table too small"); |
97f1c45c JC |
377 | ret = -EINVAL; |
378 | goto fail; | |
379 | } | |
380 | ||
b15deac7 JC |
381 | if (s->max_table_entries > SIZE_MAX / 4 || |
382 | s->max_table_entries > (int) INT_MAX / 4) { | |
383 | error_setg(errp, "Max Table Entries too large (%" PRId32 ")", | |
384 | s->max_table_entries); | |
385 | ret = -EINVAL; | |
386 | goto fail; | |
387 | } | |
388 | ||
389 | pagetable_size = (uint64_t) s->max_table_entries * 4; | |
390 | ||
9a4f4c31 | 391 | s->pagetable = qemu_try_blockalign(bs->file->bs, pagetable_size); |
5fb09cd5 | 392 | if (s->pagetable == NULL) { |
32f6439c | 393 | error_setg(errp, "Unable to allocate memory for page table"); |
5fb09cd5 KW |
394 | ret = -ENOMEM; |
395 | goto fail; | |
396 | } | |
b71d1c2e | 397 | |
24da78db | 398 | s->bat_offset = be64_to_cpu(dyndisk_header->table_offset); |
59294e46 | 399 | |
cf2ab8fc | 400 | ret = bdrv_pread(bs->file, s->bat_offset, s->pagetable, |
9a4f4c31 | 401 | pagetable_size); |
59294e46 | 402 | if (ret < 0) { |
32f6439c | 403 | error_setg(errp, "Error reading pagetable"); |
24da78db CA |
404 | goto fail; |
405 | } | |
b71d1c2e | 406 | |
24da78db | 407 | s->free_data_block_offset = |
b15deac7 | 408 | ROUND_UP(s->bat_offset + pagetable_size, 512); |
15d35bc5 | 409 | |
24da78db CA |
410 | for (i = 0; i < s->max_table_entries; i++) { |
411 | be32_to_cpus(&s->pagetable[i]); | |
412 | if (s->pagetable[i] != 0xFFFFFFFF) { | |
413 | int64_t next = (512 * (int64_t) s->pagetable[i]) + | |
414 | s->bitmap_size + s->block_size; | |
15d35bc5 | 415 | |
24da78db CA |
416 | if (next > s->free_data_block_offset) { |
417 | s->free_data_block_offset = next; | |
418 | } | |
419 | } | |
15d35bc5 | 420 | } |
15d35bc5 | 421 | |
81caa3cc EB |
422 | bs_size = bdrv_getlength(bs->file->bs); |
423 | if (bs_size < 0) { | |
424 | error_setg_errno(errp, -bs_size, "Unable to learn image size"); | |
425 | ret = bs_size; | |
426 | goto fail; | |
427 | } | |
428 | if (s->free_data_block_offset > bs_size) { | |
fb8fe35f PL |
429 | error_setg(errp, "block-vpc: free_data_block_offset points after " |
430 | "the end of file. The image has been truncated."); | |
431 | ret = -EINVAL; | |
432 | goto fail; | |
433 | } | |
434 | ||
24da78db | 435 | s->last_bitmap_offset = (int64_t) -1; |
6a0f9e82 | 436 | |
6a0f9e82 | 437 | #ifdef CACHE |
24da78db CA |
438 | s->pageentry_u8 = g_malloc(512); |
439 | s->pageentry_u32 = s->pageentry_u8; | |
440 | s->pageentry_u16 = s->pageentry_u8; | |
441 | s->last_pagetable = -1; | |
6a0f9e82 | 442 | #endif |
24da78db | 443 | } |
6a0f9e82 | 444 | |
612ff3d8 | 445 | /* Disable migration when VHD images are used */ |
81e5f78a AG |
446 | error_setg(&s->migration_blocker, "The vpc format used by node '%s' " |
447 | "does not support live migration", | |
448 | bdrv_get_device_or_node_name(bs)); | |
fe44dc91 AA |
449 | ret = migrate_add_blocker(s->migration_blocker, &local_err); |
450 | if (local_err) { | |
451 | error_propagate(errp, local_err); | |
452 | error_free(s->migration_blocker); | |
453 | goto fail; | |
454 | } | |
455 | ||
456 | qemu_co_mutex_init(&s->lock); | |
612ff3d8 | 457 | |
6a0f9e82 | 458 | return 0; |
59294e46 KW |
459 | |
460 | fail: | |
97f1c45c | 461 | qemu_vfree(s->pagetable); |
59294e46 KW |
462 | #ifdef CACHE |
463 | g_free(s->pageentry_u8); | |
464 | #endif | |
465 | return ret; | |
6a0f9e82 FB |
466 | } |
467 | ||
3fe4b700 JC |
468 | static int vpc_reopen_prepare(BDRVReopenState *state, |
469 | BlockReopenQueue *queue, Error **errp) | |
470 | { | |
471 | return 0; | |
472 | } | |
473 | ||
b71d1c2e AL |
474 | /* |
475 | * Returns the absolute byte offset of the given sector in the image file. | |
476 | * If the sector is not allocated, -1 is returned instead. | |
cfc87e00 PM |
477 | * If an error occurred trying to write an updated block bitmap back to |
478 | * the file, -2 is returned, and the error value is written to *err. | |
479 | * This can only happen for a write operation. | |
15d35bc5 AL |
480 | * |
481 | * The parameter write must be 1 if the offset will be used for a write | |
482 | * operation (the block bitmaps is updated then), 0 otherwise. | |
cfc87e00 | 483 | * If write is true then err must not be NULL. |
b71d1c2e | 484 | */ |
d46b7cc6 | 485 | static inline int64_t get_image_offset(BlockDriverState *bs, uint64_t offset, |
cfc87e00 | 486 | bool write, int *err) |
6a0f9e82 FB |
487 | { |
488 | BDRVVPCState *s = bs->opaque; | |
6a0f9e82 | 489 | uint64_t bitmap_offset, block_offset; |
d46b7cc6 | 490 | uint32_t pagetable_index, offset_in_block; |
6a0f9e82 | 491 | |
cfc87e00 PM |
492 | assert(!(write && err == NULL)); |
493 | ||
2cfacb62 | 494 | pagetable_index = offset / s->block_size; |
d46b7cc6 | 495 | offset_in_block = offset % s->block_size; |
3b46e624 | 496 | |
15d35bc5 | 497 | if (pagetable_index >= s->max_table_entries || s->pagetable[pagetable_index] == 0xffffffff) |
9c057d0b | 498 | return -1; /* not allocated */ |
6a0f9e82 | 499 | |
378e2aea | 500 | bitmap_offset = 512 * (uint64_t) s->pagetable[pagetable_index]; |
d46b7cc6 | 501 | block_offset = bitmap_offset + s->bitmap_size + offset_in_block; |
15d35bc5 | 502 | |
9c057d0b JC |
503 | /* We must ensure that we don't write to any sectors which are marked as |
504 | unused in the bitmap. We get away with setting all bits in the block | |
505 | bitmap each time we write to a new block. This might cause Virtual PC to | |
506 | miss sparse read optimization, but it's not a problem in terms of | |
507 | correctness. */ | |
15d35bc5 AL |
508 | if (write && (s->last_bitmap_offset != bitmap_offset)) { |
509 | uint8_t bitmap[s->bitmap_size]; | |
cfc87e00 | 510 | int r; |
15d35bc5 AL |
511 | |
512 | s->last_bitmap_offset = bitmap_offset; | |
513 | memset(bitmap, 0xff, s->bitmap_size); | |
cfc87e00 PM |
514 | r = bdrv_pwrite_sync(bs->file, bitmap_offset, bitmap, s->bitmap_size); |
515 | if (r < 0) { | |
516 | *err = r; | |
517 | return -2; | |
518 | } | |
15d35bc5 | 519 | } |
3b46e624 | 520 | |
b71d1c2e | 521 | return block_offset; |
6a0f9e82 FB |
522 | } |
523 | ||
15d35bc5 AL |
524 | /* |
525 | * Writes the footer to the end of the image file. This is needed when the | |
526 | * file grows as it overwrites the old footer | |
527 | * | |
528 | * Returns 0 on success and < 0 on error | |
529 | */ | |
530 | static int rewrite_footer(BlockDriverState* bs) | |
531 | { | |
532 | int ret; | |
533 | BDRVVPCState *s = bs->opaque; | |
534 | int64_t offset = s->free_data_block_offset; | |
535 | ||
d9ca2ea2 | 536 | ret = bdrv_pwrite_sync(bs->file, offset, s->footer_buf, HEADER_SIZE); |
15d35bc5 AL |
537 | if (ret < 0) |
538 | return ret; | |
539 | ||
540 | return 0; | |
541 | } | |
542 | ||
543 | /* | |
544 | * Allocates a new block. This involves writing a new footer and updating | |
545 | * the Block Allocation Table to use the space at the old end of the image | |
546 | * file (overwriting the old footer) | |
547 | * | |
548 | * Returns the sectors' offset in the image file on success and < 0 on error | |
549 | */ | |
513b0f02 | 550 | static int64_t alloc_block(BlockDriverState* bs, int64_t offset) |
15d35bc5 AL |
551 | { |
552 | BDRVVPCState *s = bs->opaque; | |
553 | int64_t bat_offset; | |
554 | uint32_t index, bat_value; | |
555 | int ret; | |
556 | uint8_t bitmap[s->bitmap_size]; | |
557 | ||
9c057d0b | 558 | /* Check if sector_num is valid */ |
513b0f02 KW |
559 | if ((offset < 0) || (offset > bs->total_sectors * BDRV_SECTOR_SIZE)) { |
560 | return -EINVAL; | |
561 | } | |
15d35bc5 | 562 | |
9c057d0b | 563 | /* Write entry into in-memory BAT */ |
513b0f02 KW |
564 | index = offset / s->block_size; |
565 | assert(s->pagetable[index] == 0xFFFFFFFF); | |
15d35bc5 AL |
566 | s->pagetable[index] = s->free_data_block_offset / 512; |
567 | ||
9c057d0b | 568 | /* Initialize the block's bitmap */ |
15d35bc5 | 569 | memset(bitmap, 0xff, s->bitmap_size); |
d9ca2ea2 | 570 | ret = bdrv_pwrite_sync(bs->file, s->free_data_block_offset, bitmap, |
078a458e | 571 | s->bitmap_size); |
5bb1cbac KW |
572 | if (ret < 0) { |
573 | return ret; | |
574 | } | |
15d35bc5 | 575 | |
9c057d0b | 576 | /* Write new footer (the old one will be overwritten) */ |
15d35bc5 AL |
577 | s->free_data_block_offset += s->block_size + s->bitmap_size; |
578 | ret = rewrite_footer(bs); | |
579 | if (ret < 0) | |
580 | goto fail; | |
581 | ||
9c057d0b | 582 | /* Write BAT entry to disk */ |
15d35bc5 | 583 | bat_offset = s->bat_offset + (4 * index); |
a4127c42 | 584 | bat_value = cpu_to_be32(s->pagetable[index]); |
d9ca2ea2 | 585 | ret = bdrv_pwrite_sync(bs->file, bat_offset, &bat_value, 4); |
15d35bc5 AL |
586 | if (ret < 0) |
587 | goto fail; | |
588 | ||
cfc87e00 | 589 | return get_image_offset(bs, offset, false, NULL); |
15d35bc5 AL |
590 | |
591 | fail: | |
592 | s->free_data_block_offset -= (s->block_size + s->bitmap_size); | |
513b0f02 | 593 | return ret; |
15d35bc5 AL |
594 | } |
595 | ||
97b00e28 PB |
596 | static int vpc_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) |
597 | { | |
598 | BDRVVPCState *s = (BDRVVPCState *)bs->opaque; | |
599 | VHDFooter *footer = (VHDFooter *) s->footer_buf; | |
600 | ||
0d4cc3e7 | 601 | if (be32_to_cpu(footer->type) != VHD_FIXED) { |
97b00e28 PB |
602 | bdi->cluster_size = s->block_size; |
603 | } | |
604 | ||
95de6d70 | 605 | bdi->unallocated_blocks_are_zero = true; |
97b00e28 PB |
606 | return 0; |
607 | } | |
608 | ||
d46b7cc6 KW |
609 | static int coroutine_fn |
610 | vpc_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
611 | QEMUIOVector *qiov, int flags) | |
6a0f9e82 | 612 | { |
6c6ea921 | 613 | BDRVVPCState *s = bs->opaque; |
6a0f9e82 | 614 | int ret; |
d46b7cc6 KW |
615 | int64_t image_offset; |
616 | int64_t n_bytes; | |
617 | int64_t bytes_done = 0; | |
e54835c0 | 618 | VHDFooter *footer = (VHDFooter *) s->footer_buf; |
d46b7cc6 | 619 | QEMUIOVector local_qiov; |
6a0f9e82 | 620 | |
0d4cc3e7 | 621 | if (be32_to_cpu(footer->type) == VHD_FIXED) { |
a03ef88f | 622 | return bdrv_co_preadv(bs->file, offset, bytes, qiov, 0); |
24da78db | 623 | } |
b71d1c2e | 624 | |
d46b7cc6 KW |
625 | qemu_co_mutex_lock(&s->lock); |
626 | qemu_iovec_init(&local_qiov, qiov->niov); | |
6c6ea921 | 627 | |
d46b7cc6 | 628 | while (bytes > 0) { |
cfc87e00 | 629 | image_offset = get_image_offset(bs, offset, false, NULL); |
d46b7cc6 KW |
630 | n_bytes = MIN(bytes, s->block_size - (offset % s->block_size)); |
631 | ||
632 | if (image_offset == -1) { | |
633 | qemu_iovec_memset(qiov, bytes_done, 0, n_bytes); | |
b71d1c2e | 634 | } else { |
d46b7cc6 KW |
635 | qemu_iovec_reset(&local_qiov); |
636 | qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); | |
637 | ||
a03ef88f | 638 | ret = bdrv_co_preadv(bs->file, image_offset, n_bytes, |
d46b7cc6 KW |
639 | &local_qiov, 0); |
640 | if (ret < 0) { | |
641 | goto fail; | |
6c6ea921 | 642 | } |
b71d1c2e AL |
643 | } |
644 | ||
d46b7cc6 KW |
645 | bytes -= n_bytes; |
646 | offset += n_bytes; | |
647 | bytes_done += n_bytes; | |
6a0f9e82 | 648 | } |
6a0f9e82 | 649 | |
d46b7cc6 KW |
650 | ret = 0; |
651 | fail: | |
652 | qemu_iovec_destroy(&local_qiov); | |
2914caa0 | 653 | qemu_co_mutex_unlock(&s->lock); |
d46b7cc6 | 654 | |
2914caa0 PB |
655 | return ret; |
656 | } | |
657 | ||
513b0f02 KW |
658 | static int coroutine_fn |
659 | vpc_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes, | |
660 | QEMUIOVector *qiov, int flags) | |
15d35bc5 | 661 | { |
6c6ea921 | 662 | BDRVVPCState *s = bs->opaque; |
513b0f02 KW |
663 | int64_t image_offset; |
664 | int64_t n_bytes; | |
665 | int64_t bytes_done = 0; | |
c8115f8e | 666 | int ret = 0; |
e54835c0 | 667 | VHDFooter *footer = (VHDFooter *) s->footer_buf; |
513b0f02 | 668 | QEMUIOVector local_qiov; |
15d35bc5 | 669 | |
0d4cc3e7 | 670 | if (be32_to_cpu(footer->type) == VHD_FIXED) { |
a03ef88f | 671 | return bdrv_co_pwritev(bs->file, offset, bytes, qiov, 0); |
24da78db | 672 | } |
15d35bc5 | 673 | |
513b0f02 KW |
674 | qemu_co_mutex_lock(&s->lock); |
675 | qemu_iovec_init(&local_qiov, qiov->niov); | |
676 | ||
677 | while (bytes > 0) { | |
cfc87e00 PM |
678 | image_offset = get_image_offset(bs, offset, true, &ret); |
679 | if (image_offset == -2) { | |
680 | /* Failed to write block bitmap: can't proceed with write */ | |
681 | goto fail; | |
682 | } | |
513b0f02 | 683 | n_bytes = MIN(bytes, s->block_size - (offset % s->block_size)); |
6c6ea921 | 684 | |
513b0f02 KW |
685 | if (image_offset == -1) { |
686 | image_offset = alloc_block(bs, offset); | |
687 | if (image_offset < 0) { | |
688 | ret = image_offset; | |
689 | goto fail; | |
690 | } | |
15d35bc5 AL |
691 | } |
692 | ||
513b0f02 KW |
693 | qemu_iovec_reset(&local_qiov); |
694 | qemu_iovec_concat(&local_qiov, qiov, bytes_done, n_bytes); | |
695 | ||
a03ef88f | 696 | ret = bdrv_co_pwritev(bs->file, image_offset, n_bytes, |
513b0f02 KW |
697 | &local_qiov, 0); |
698 | if (ret < 0) { | |
699 | goto fail; | |
6c6ea921 | 700 | } |
15d35bc5 | 701 | |
513b0f02 KW |
702 | bytes -= n_bytes; |
703 | offset += n_bytes; | |
704 | bytes_done += n_bytes; | |
15d35bc5 AL |
705 | } |
706 | ||
513b0f02 KW |
707 | ret = 0; |
708 | fail: | |
709 | qemu_iovec_destroy(&local_qiov); | |
e183ef75 | 710 | qemu_co_mutex_unlock(&s->lock); |
513b0f02 | 711 | |
e183ef75 PB |
712 | return ret; |
713 | } | |
714 | ||
2f83673b EB |
715 | static int coroutine_fn vpc_co_block_status(BlockDriverState *bs, |
716 | bool want_zero, | |
717 | int64_t offset, int64_t bytes, | |
718 | int64_t *pnum, int64_t *map, | |
719 | BlockDriverState **file) | |
0cc84887 KW |
720 | { |
721 | BDRVVPCState *s = bs->opaque; | |
722 | VHDFooter *footer = (VHDFooter*) s->footer_buf; | |
2f83673b | 723 | int64_t image_offset; |
0cc84887 | 724 | bool allocated; |
2f83673b EB |
725 | int ret; |
726 | int64_t n; | |
0cc84887 KW |
727 | |
728 | if (be32_to_cpu(footer->type) == VHD_FIXED) { | |
2f83673b EB |
729 | *pnum = bytes; |
730 | *map = offset; | |
7429e207 | 731 | *file = bs->file->bs; |
2f83673b | 732 | return BDRV_BLOCK_RAW | BDRV_BLOCK_OFFSET_VALID; |
0cc84887 KW |
733 | } |
734 | ||
778b087e PB |
735 | qemu_co_mutex_lock(&s->lock); |
736 | ||
2f83673b EB |
737 | image_offset = get_image_offset(bs, offset, false, NULL); |
738 | allocated = (image_offset != -1); | |
0cc84887 | 739 | *pnum = 0; |
778b087e | 740 | ret = 0; |
0cc84887 KW |
741 | |
742 | do { | |
743 | /* All sectors in a block are contiguous (without using the bitmap) */ | |
2f83673b EB |
744 | n = ROUND_UP(offset + 1, s->block_size) - offset; |
745 | n = MIN(n, bytes); | |
0cc84887 KW |
746 | |
747 | *pnum += n; | |
2f83673b EB |
748 | offset += n; |
749 | bytes -= n; | |
2ec711dc PL |
750 | /* *pnum can't be greater than one block for allocated |
751 | * sectors since there is always a bitmap in between. */ | |
752 | if (allocated) { | |
7429e207 | 753 | *file = bs->file->bs; |
2f83673b EB |
754 | *map = image_offset; |
755 | ret = BDRV_BLOCK_DATA | BDRV_BLOCK_OFFSET_VALID; | |
778b087e | 756 | break; |
2ec711dc | 757 | } |
2f83673b | 758 | if (bytes == 0) { |
0cc84887 KW |
759 | break; |
760 | } | |
2f83673b EB |
761 | image_offset = get_image_offset(bs, offset, false, NULL); |
762 | } while (image_offset == -1); | |
0cc84887 | 763 | |
778b087e PB |
764 | qemu_co_mutex_unlock(&s->lock); |
765 | return ret; | |
0cc84887 KW |
766 | } |
767 | ||
57c7d9e5 AL |
768 | /* |
769 | * Calculates the number of cylinders, heads and sectors per cylinder | |
770 | * based on a given number of sectors. This is the algorithm described | |
771 | * in the VHD specification. | |
772 | * | |
773 | * Note that the geometry doesn't always exactly match total_sectors but | |
774 | * may round it down. | |
6e9ea0c0 | 775 | * |
c23fb11b | 776 | * Returns 0 on success, -EFBIG if the size is larger than 2040 GiB. Override |
258d2edb CA |
777 | * the hardware EIDE and ATA-2 limit of 16 heads (max disk size of 127 GB) |
778 | * and instead allow up to 255 heads. | |
57c7d9e5 | 779 | */ |
6e9ea0c0 | 780 | static int calculate_geometry(int64_t total_sectors, uint16_t* cyls, |
57c7d9e5 AL |
781 | uint8_t* heads, uint8_t* secs_per_cyl) |
782 | { | |
783 | uint32_t cyls_times_heads; | |
784 | ||
690cbb09 | 785 | total_sectors = MIN(total_sectors, VHD_MAX_GEOMETRY); |
57c7d9e5 | 786 | |
690cbb09 | 787 | if (total_sectors >= 65535LL * 16 * 63) { |
57c7d9e5 | 788 | *secs_per_cyl = 255; |
690cbb09 | 789 | *heads = 16; |
57c7d9e5 AL |
790 | cyls_times_heads = total_sectors / *secs_per_cyl; |
791 | } else { | |
792 | *secs_per_cyl = 17; | |
793 | cyls_times_heads = total_sectors / *secs_per_cyl; | |
13f1493f | 794 | *heads = DIV_ROUND_UP(cyls_times_heads, 1024); |
57c7d9e5 | 795 | |
690cbb09 | 796 | if (*heads < 4) { |
57c7d9e5 | 797 | *heads = 4; |
690cbb09 | 798 | } |
57c7d9e5 AL |
799 | |
800 | if (cyls_times_heads >= (*heads * 1024) || *heads > 16) { | |
801 | *secs_per_cyl = 31; | |
802 | *heads = 16; | |
803 | cyls_times_heads = total_sectors / *secs_per_cyl; | |
804 | } | |
805 | ||
806 | if (cyls_times_heads >= (*heads * 1024)) { | |
807 | *secs_per_cyl = 63; | |
808 | *heads = 16; | |
809 | cyls_times_heads = total_sectors / *secs_per_cyl; | |
810 | } | |
811 | } | |
812 | ||
dede4188 | 813 | *cyls = cyls_times_heads / *heads; |
6e9ea0c0 AJ |
814 | |
815 | return 0; | |
57c7d9e5 AL |
816 | } |
817 | ||
b8f45cdf | 818 | static int create_dynamic_disk(BlockBackend *blk, uint8_t *buf, |
fef6070e | 819 | int64_t total_sectors) |
57c7d9e5 | 820 | { |
e54835c0 JC |
821 | VHDDynDiskHeader *dyndisk_header = |
822 | (VHDDynDiskHeader *) buf; | |
57c7d9e5 | 823 | size_t block_size, num_bat_entries; |
24da78db | 824 | int i; |
fef6070e JC |
825 | int ret; |
826 | int64_t offset = 0; | |
57c7d9e5 | 827 | |
9c057d0b | 828 | /* Write the footer (twice: at the beginning and at the end) */ |
57c7d9e5 AL |
829 | block_size = 0x200000; |
830 | num_bat_entries = (total_sectors + block_size / 512) / (block_size / 512); | |
831 | ||
8341f00d | 832 | ret = blk_pwrite(blk, offset, buf, HEADER_SIZE, 0); |
40a99aac | 833 | if (ret < 0) { |
f0ff243a BS |
834 | goto fail; |
835 | } | |
57c7d9e5 | 836 | |
fef6070e | 837 | offset = 1536 + ((num_bat_entries * 4 + 511) & ~511); |
8341f00d | 838 | ret = blk_pwrite(blk, offset, buf, HEADER_SIZE, 0); |
fef6070e | 839 | if (ret < 0) { |
f0ff243a BS |
840 | goto fail; |
841 | } | |
57c7d9e5 | 842 | |
9c057d0b | 843 | /* Write the initial BAT */ |
fef6070e | 844 | offset = 3 * 512; |
57c7d9e5 AL |
845 | |
846 | memset(buf, 0xFF, 512); | |
13f1493f | 847 | for (i = 0; i < DIV_ROUND_UP(num_bat_entries * 4, 512); i++) { |
8341f00d | 848 | ret = blk_pwrite(blk, offset, buf, 512, 0); |
fef6070e | 849 | if (ret < 0) { |
f0ff243a BS |
850 | goto fail; |
851 | } | |
fef6070e | 852 | offset += 512; |
f0ff243a | 853 | } |
57c7d9e5 | 854 | |
9c057d0b | 855 | /* Prepare the Dynamic Disk Header */ |
57c7d9e5 AL |
856 | memset(buf, 0, 1024); |
857 | ||
5ec4d682 | 858 | memcpy(dyndisk_header->magic, "cxsparse", 8); |
57c7d9e5 | 859 | |
78439f6a CA |
860 | /* |
861 | * Note: The spec is actually wrong here for data_offset, it says | |
862 | * 0xFFFFFFFF, but MS tools expect all 64 bits to be set. | |
863 | */ | |
a4127c42 SH |
864 | dyndisk_header->data_offset = cpu_to_be64(0xFFFFFFFFFFFFFFFFULL); |
865 | dyndisk_header->table_offset = cpu_to_be64(3 * 512); | |
866 | dyndisk_header->version = cpu_to_be32(0x00010000); | |
867 | dyndisk_header->block_size = cpu_to_be32(block_size); | |
868 | dyndisk_header->max_table_entries = cpu_to_be32(num_bat_entries); | |
57c7d9e5 | 869 | |
a4127c42 | 870 | dyndisk_header->checksum = cpu_to_be32(vpc_checksum(buf, 1024)); |
57c7d9e5 | 871 | |
9c057d0b | 872 | /* Write the header */ |
fef6070e | 873 | offset = 512; |
57c7d9e5 | 874 | |
8341f00d | 875 | ret = blk_pwrite(blk, offset, buf, 1024, 0); |
fef6070e | 876 | if (ret < 0) { |
f0ff243a BS |
877 | goto fail; |
878 | } | |
f0ff243a | 879 | |
24da78db CA |
880 | fail: |
881 | return ret; | |
882 | } | |
883 | ||
b8f45cdf | 884 | static int create_fixed_disk(BlockBackend *blk, uint8_t *buf, |
ed3d2ec9 | 885 | int64_t total_size, Error **errp) |
24da78db | 886 | { |
fef6070e | 887 | int ret; |
24da78db CA |
888 | |
889 | /* Add footer to total size */ | |
fef6070e JC |
890 | total_size += HEADER_SIZE; |
891 | ||
3a691c50 | 892 | ret = blk_truncate(blk, total_size, PREALLOC_MODE_OFF, errp); |
fef6070e JC |
893 | if (ret < 0) { |
894 | return ret; | |
24da78db CA |
895 | } |
896 | ||
8341f00d | 897 | ret = blk_pwrite(blk, total_size - HEADER_SIZE, buf, HEADER_SIZE, 0); |
fef6070e | 898 | if (ret < 0) { |
ed3d2ec9 | 899 | error_setg_errno(errp, -ret, "Unable to write VHD header"); |
fef6070e JC |
900 | return ret; |
901 | } | |
24da78db | 902 | |
24da78db CA |
903 | return ret; |
904 | } | |
905 | ||
1cfeaf38 KW |
906 | static int calculate_rounded_image_size(BlockdevCreateOptionsVpc *vpc_opts, |
907 | uint16_t *out_cyls, | |
908 | uint8_t *out_heads, | |
909 | uint8_t *out_secs_per_cyl, | |
910 | int64_t *out_total_sectors, | |
911 | Error **errp) | |
912 | { | |
913 | int64_t total_size = vpc_opts->size; | |
914 | uint16_t cyls = 0; | |
915 | uint8_t heads = 0; | |
916 | uint8_t secs_per_cyl = 0; | |
917 | int64_t total_sectors; | |
918 | int i; | |
919 | ||
920 | /* | |
921 | * Calculate matching total_size and geometry. Increase the number of | |
922 | * sectors requested until we get enough (or fail). This ensures that | |
923 | * qemu-img convert doesn't truncate images, but rather rounds up. | |
924 | * | |
925 | * If the image size can't be represented by a spec conformant CHS geometry, | |
926 | * we set the geometry to 65535 x 16 x 255 (CxHxS) sectors and use | |
927 | * the image size from the VHD footer to calculate total_sectors. | |
928 | */ | |
929 | if (vpc_opts->force_size) { | |
930 | /* This will force the use of total_size for sector count, below */ | |
931 | cyls = VHD_CHS_MAX_C; | |
932 | heads = VHD_CHS_MAX_H; | |
933 | secs_per_cyl = VHD_CHS_MAX_S; | |
934 | } else { | |
935 | total_sectors = MIN(VHD_MAX_GEOMETRY, total_size / BDRV_SECTOR_SIZE); | |
936 | for (i = 0; total_sectors > (int64_t)cyls * heads * secs_per_cyl; i++) { | |
937 | calculate_geometry(total_sectors + i, &cyls, &heads, &secs_per_cyl); | |
938 | } | |
939 | } | |
940 | ||
941 | if ((int64_t)cyls * heads * secs_per_cyl == VHD_MAX_GEOMETRY) { | |
942 | total_sectors = total_size / BDRV_SECTOR_SIZE; | |
943 | /* Allow a maximum disk size of 2040 GiB */ | |
944 | if (total_sectors > VHD_MAX_SECTORS) { | |
945 | error_setg(errp, "Disk size is too large, max size is 2040 GiB"); | |
946 | return -EFBIG; | |
947 | } | |
948 | } else { | |
949 | total_sectors = (int64_t) cyls * heads * secs_per_cyl; | |
950 | } | |
951 | ||
952 | *out_total_sectors = total_sectors; | |
953 | if (out_cyls) { | |
954 | *out_cyls = cyls; | |
955 | *out_heads = heads; | |
956 | *out_secs_per_cyl = secs_per_cyl; | |
957 | } | |
958 | ||
959 | return 0; | |
960 | } | |
961 | ||
182c8835 KW |
962 | static int coroutine_fn vpc_co_create(BlockdevCreateOptions *opts, |
963 | Error **errp) | |
24da78db | 964 | { |
182c8835 KW |
965 | BlockdevCreateOptionsVpc *vpc_opts; |
966 | BlockBackend *blk = NULL; | |
967 | BlockDriverState *bs = NULL; | |
968 | ||
24da78db | 969 | uint8_t buf[1024]; |
e54835c0 | 970 | VHDFooter *footer = (VHDFooter *) buf; |
24da78db CA |
971 | uint16_t cyls = 0; |
972 | uint8_t heads = 0; | |
973 | uint8_t secs_per_cyl = 0; | |
974 | int64_t total_sectors; | |
975 | int64_t total_size; | |
976 | int disk_type; | |
977 | int ret = -EIO; | |
978 | ||
182c8835 KW |
979 | assert(opts->driver == BLOCKDEV_DRIVER_VPC); |
980 | vpc_opts = &opts->u.vpc; | |
981 | ||
982 | /* Validate options and set default values */ | |
983 | total_size = vpc_opts->size; | |
984 | ||
985 | if (!vpc_opts->has_subformat) { | |
986 | vpc_opts->subformat = BLOCKDEV_VPC_SUBFORMAT_DYNAMIC; | |
987 | } | |
988 | switch (vpc_opts->subformat) { | |
989 | case BLOCKDEV_VPC_SUBFORMAT_DYNAMIC: | |
24da78db | 990 | disk_type = VHD_DYNAMIC; |
182c8835 KW |
991 | break; |
992 | case BLOCKDEV_VPC_SUBFORMAT_FIXED: | |
993 | disk_type = VHD_FIXED; | |
994 | break; | |
995 | default: | |
996 | g_assert_not_reached(); | |
24da78db CA |
997 | } |
998 | ||
182c8835 KW |
999 | /* Create BlockBackend to write to the image */ |
1000 | bs = bdrv_open_blockdev_ref(vpc_opts->file, errp); | |
1001 | if (bs == NULL) { | |
1002 | return -EIO; | |
24da78db | 1003 | } |
b8f45cdf | 1004 | |
182c8835 KW |
1005 | blk = blk_new(BLK_PERM_WRITE | BLK_PERM_RESIZE, BLK_PERM_ALL); |
1006 | ret = blk_insert_bs(blk, bs, errp); | |
1007 | if (ret < 0) { | |
fef6070e | 1008 | goto out; |
4ab15590 | 1009 | } |
b8f45cdf KW |
1010 | blk_set_allow_write_beyond_eof(blk, true); |
1011 | ||
1cfeaf38 KW |
1012 | /* Get geometry and check that it matches the image size*/ |
1013 | ret = calculate_rounded_image_size(vpc_opts, &cyls, &heads, &secs_per_cyl, | |
1014 | &total_sectors, errp); | |
1015 | if (ret < 0) { | |
1016 | goto out; | |
690cbb09 PL |
1017 | } |
1018 | ||
1cfeaf38 KW |
1019 | if (total_size != total_sectors * BDRV_SECTOR_SIZE) { |
1020 | error_setg(errp, "The requested image size cannot be represented in " | |
1021 | "CHS geometry"); | |
1022 | error_append_hint(errp, "Try size=%llu or force-size=on (the " | |
1023 | "latter makes the image incompatible with " | |
1024 | "Virtual PC)", | |
1025 | total_sectors * BDRV_SECTOR_SIZE); | |
1026 | ret = -EINVAL; | |
1027 | goto out; | |
24da78db | 1028 | } |
ecd880d9 | 1029 | |
24da78db CA |
1030 | /* Prepare the Hard Disk Footer */ |
1031 | memset(buf, 0, 1024); | |
1032 | ||
1033 | memcpy(footer->creator, "conectix", 8); | |
182c8835 | 1034 | if (vpc_opts->force_size) { |
fb9245c2 JC |
1035 | memcpy(footer->creator_app, "qem2", 4); |
1036 | } else { | |
1037 | memcpy(footer->creator_app, "qemu", 4); | |
1038 | } | |
24da78db CA |
1039 | memcpy(footer->creator_os, "Wi2k", 4); |
1040 | ||
a4127c42 SH |
1041 | footer->features = cpu_to_be32(0x02); |
1042 | footer->version = cpu_to_be32(0x00010000); | |
24da78db | 1043 | if (disk_type == VHD_DYNAMIC) { |
a4127c42 | 1044 | footer->data_offset = cpu_to_be64(HEADER_SIZE); |
24da78db | 1045 | } else { |
a4127c42 | 1046 | footer->data_offset = cpu_to_be64(0xFFFFFFFFFFFFFFFFULL); |
24da78db | 1047 | } |
a4127c42 | 1048 | footer->timestamp = cpu_to_be32(time(NULL) - VHD_TIMESTAMP_BASE); |
24da78db CA |
1049 | |
1050 | /* Version of Virtual PC 2007 */ | |
a4127c42 SH |
1051 | footer->major = cpu_to_be16(0x0005); |
1052 | footer->minor = cpu_to_be16(0x0003); | |
3f3f20dc | 1053 | footer->orig_size = cpu_to_be64(total_size); |
03671ded | 1054 | footer->current_size = cpu_to_be64(total_size); |
a4127c42 | 1055 | footer->cyls = cpu_to_be16(cyls); |
24da78db CA |
1056 | footer->heads = heads; |
1057 | footer->secs_per_cyl = secs_per_cyl; | |
1058 | ||
a4127c42 | 1059 | footer->type = cpu_to_be32(disk_type); |
24da78db | 1060 | |
38440a21 | 1061 | qemu_uuid_generate(&footer->uuid); |
24da78db | 1062 | |
a4127c42 | 1063 | footer->checksum = cpu_to_be32(vpc_checksum(buf, HEADER_SIZE)); |
24da78db CA |
1064 | |
1065 | if (disk_type == VHD_DYNAMIC) { | |
b8f45cdf | 1066 | ret = create_dynamic_disk(blk, buf, total_sectors); |
ed3d2ec9 HR |
1067 | if (ret < 0) { |
1068 | error_setg(errp, "Unable to create or write VHD header"); | |
1069 | } | |
24da78db | 1070 | } else { |
ed3d2ec9 | 1071 | ret = create_fixed_disk(blk, buf, total_size, errp); |
0211b9be | 1072 | } |
24da78db | 1073 | |
fec9921f | 1074 | out: |
b8f45cdf | 1075 | blk_unref(blk); |
182c8835 KW |
1076 | bdrv_unref(bs); |
1077 | return ret; | |
1078 | } | |
1079 | ||
1080 | static int coroutine_fn vpc_co_create_opts(const char *filename, | |
1081 | QemuOpts *opts, Error **errp) | |
1082 | { | |
1083 | BlockdevCreateOptions *create_options = NULL; | |
92adf9db | 1084 | QDict *qdict; |
182c8835 KW |
1085 | Visitor *v; |
1086 | BlockDriverState *bs = NULL; | |
1087 | Error *local_err = NULL; | |
1088 | int ret; | |
1089 | ||
1090 | static const QDictRenames opt_renames[] = { | |
1091 | { VPC_OPT_FORCE_SIZE, "force-size" }, | |
1092 | { NULL, NULL }, | |
1093 | }; | |
1094 | ||
1095 | /* Parse options and convert legacy syntax */ | |
1096 | qdict = qemu_opts_to_qdict_filtered(opts, NULL, &vpc_create_opts, true); | |
1097 | ||
1098 | if (!qdict_rename_keys(qdict, opt_renames, errp)) { | |
1099 | ret = -EINVAL; | |
1100 | goto fail; | |
1101 | } | |
1102 | ||
1103 | /* Create and open the file (protocol layer) */ | |
1104 | ret = bdrv_create_file(filename, opts, &local_err); | |
1105 | if (ret < 0) { | |
1106 | error_propagate(errp, local_err); | |
1107 | goto fail; | |
1108 | } | |
1109 | ||
1110 | bs = bdrv_open(filename, NULL, NULL, | |
1111 | BDRV_O_RDWR | BDRV_O_RESIZE | BDRV_O_PROTOCOL, errp); | |
1112 | if (bs == NULL) { | |
1113 | ret = -EIO; | |
1114 | goto fail; | |
1115 | } | |
1116 | ||
1117 | /* Now get the QAPI type BlockdevCreateOptions */ | |
1118 | qdict_put_str(qdict, "driver", "vpc"); | |
1119 | qdict_put_str(qdict, "file", bs->node_name); | |
1120 | ||
af91062e MA |
1121 | v = qobject_input_visitor_new_flat_confused(qdict, errp); |
1122 | if (!v) { | |
182c8835 KW |
1123 | ret = -EINVAL; |
1124 | goto fail; | |
1125 | } | |
1126 | ||
182c8835 KW |
1127 | visit_type_BlockdevCreateOptions(v, NULL, &create_options, &local_err); |
1128 | visit_free(v); | |
1129 | ||
1130 | if (local_err) { | |
1131 | error_propagate(errp, local_err); | |
1132 | ret = -EINVAL; | |
1133 | goto fail; | |
1134 | } | |
1135 | ||
1136 | /* Silently round up size */ | |
1137 | assert(create_options->driver == BLOCKDEV_DRIVER_VPC); | |
1138 | create_options->u.vpc.size = | |
1139 | ROUND_UP(create_options->u.vpc.size, BDRV_SECTOR_SIZE); | |
1140 | ||
1cfeaf38 KW |
1141 | if (!create_options->u.vpc.force_size) { |
1142 | int64_t total_sectors; | |
1143 | ret = calculate_rounded_image_size(&create_options->u.vpc, NULL, NULL, | |
1144 | NULL, &total_sectors, errp); | |
1145 | if (ret < 0) { | |
1146 | goto fail; | |
1147 | } | |
1148 | ||
1149 | create_options->u.vpc.size = total_sectors * BDRV_SECTOR_SIZE; | |
1150 | } | |
1151 | ||
1152 | ||
182c8835 KW |
1153 | /* Create the vpc image (format layer) */ |
1154 | ret = vpc_co_create(create_options, errp); | |
1155 | ||
1156 | fail: | |
cb3e7f08 | 1157 | qobject_unref(qdict); |
182c8835 KW |
1158 | bdrv_unref(bs); |
1159 | qapi_free_BlockdevCreateOptions(create_options); | |
f0ff243a | 1160 | return ret; |
57c7d9e5 AL |
1161 | } |
1162 | ||
182c8835 | 1163 | |
72c6cc94 KW |
1164 | static int vpc_has_zero_init(BlockDriverState *bs) |
1165 | { | |
1166 | BDRVVPCState *s = bs->opaque; | |
e54835c0 | 1167 | VHDFooter *footer = (VHDFooter *) s->footer_buf; |
72c6cc94 | 1168 | |
0d4cc3e7 | 1169 | if (be32_to_cpu(footer->type) == VHD_FIXED) { |
9a4f4c31 | 1170 | return bdrv_has_zero_init(bs->file->bs); |
72c6cc94 KW |
1171 | } else { |
1172 | return 1; | |
1173 | } | |
1174 | } | |
1175 | ||
6a0f9e82 FB |
1176 | static void vpc_close(BlockDriverState *bs) |
1177 | { | |
1178 | BDRVVPCState *s = bs->opaque; | |
97f1c45c | 1179 | qemu_vfree(s->pagetable); |
6a0f9e82 | 1180 | #ifdef CACHE |
7267c094 | 1181 | g_free(s->pageentry_u8); |
6a0f9e82 | 1182 | #endif |
612ff3d8 KW |
1183 | |
1184 | migrate_del_blocker(s->migration_blocker); | |
1185 | error_free(s->migration_blocker); | |
6a0f9e82 FB |
1186 | } |
1187 | ||
fec9921f CL |
1188 | static QemuOptsList vpc_create_opts = { |
1189 | .name = "vpc-create-opts", | |
1190 | .head = QTAILQ_HEAD_INITIALIZER(vpc_create_opts.head), | |
1191 | .desc = { | |
1192 | { | |
1193 | .name = BLOCK_OPT_SIZE, | |
1194 | .type = QEMU_OPT_SIZE, | |
1195 | .help = "Virtual disk size" | |
1196 | }, | |
1197 | { | |
1198 | .name = BLOCK_OPT_SUBFMT, | |
1199 | .type = QEMU_OPT_STRING, | |
1200 | .help = | |
1201 | "Type of virtual hard disk format. Supported formats are " | |
1202 | "{dynamic (default) | fixed} " | |
1203 | }, | |
fb9245c2 JC |
1204 | { |
1205 | .name = VPC_OPT_FORCE_SIZE, | |
1206 | .type = QEMU_OPT_BOOL, | |
1207 | .help = "Force disk size calculation to use the actual size " | |
1208 | "specified, rather than using the nearest CHS-based " | |
1209 | "calculation" | |
1210 | }, | |
fec9921f CL |
1211 | { /* end of list */ } |
1212 | } | |
0e7e1989 KW |
1213 | }; |
1214 | ||
5efa9d5a | 1215 | static BlockDriver bdrv_vpc = { |
4a411185 KW |
1216 | .format_name = "vpc", |
1217 | .instance_size = sizeof(BDRVVPCState), | |
c68b89ac | 1218 | |
72c6cc94 KW |
1219 | .bdrv_probe = vpc_probe, |
1220 | .bdrv_open = vpc_open, | |
1221 | .bdrv_close = vpc_close, | |
1222 | .bdrv_reopen_prepare = vpc_reopen_prepare, | |
862f215f | 1223 | .bdrv_child_perm = bdrv_format_default_perms, |
182c8835 | 1224 | .bdrv_co_create = vpc_co_create, |
efc75e2a | 1225 | .bdrv_co_create_opts = vpc_co_create_opts, |
0e7e1989 | 1226 | |
d46b7cc6 | 1227 | .bdrv_co_preadv = vpc_co_preadv, |
513b0f02 | 1228 | .bdrv_co_pwritev = vpc_co_pwritev, |
2f83673b | 1229 | .bdrv_co_block_status = vpc_co_block_status, |
c68b89ac | 1230 | |
97b00e28 PB |
1231 | .bdrv_get_info = vpc_get_info, |
1232 | ||
fec9921f | 1233 | .create_opts = &vpc_create_opts, |
72c6cc94 | 1234 | .bdrv_has_zero_init = vpc_has_zero_init, |
6a0f9e82 | 1235 | }; |
5efa9d5a AL |
1236 | |
1237 | static void bdrv_vpc_init(void) | |
1238 | { | |
1239 | bdrv_register(&bdrv_vpc); | |
1240 | } | |
1241 | ||
1242 | block_init(bdrv_vpc_init); |