2 * Block driver for Connectix / Microsoft Virtual PC images
4 * Copyright (c) 2005 Alex Beregszaszi
5 * Copyright (c) 2009 Kevin Wolf <kwolf@suse.de>
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
25 #include "qemu/osdep.h"
26 #include "qemu-common.h"
27 #include "block/block_int.h"
28 #include "qemu/module.h"
29 #include "migration/migration.h"
30 #if defined(CONFIG_UUID)
31 #include <uuid/uuid.h>
34 /**************************************************************/
36 #define HEADER_SIZE 512
46 // Seconds since Jan 1, 2000 0:00:00 (UTC)
47 #define VHD_TIMESTAMP_BASE 946684800
49 #define VHD_MAX_SECTORS (65535LL * 255 * 255)
50 #define VHD_MAX_GEOMETRY (65535LL * 16 * 255)
53 typedef struct vhd_footer
{
54 char creator
[8]; // "conectix"
58 // Offset of next header structure, 0xFFFFFFFF if none
61 // Seconds since Jan 1, 2000 0:00:00 (UTC)
64 char creator_app
[4]; // "vpc "
67 char creator_os
[4]; // "Wi2k"
70 uint64_t current_size
;
78 // Checksum of the Hard Disk Footer ("one's complement of the sum of all
79 // the bytes in the footer without the checksum field")
82 // UUID used to identify a parent hard disk (backing file)
85 uint8_t in_saved_state
;
86 } QEMU_PACKED VHDFooter
;
88 typedef struct vhd_dyndisk_header
{
89 char magic
[8]; // "cxsparse"
91 // Offset of next header structure, 0xFFFFFFFF if none
94 // Offset of the Block Allocation Table (BAT)
95 uint64_t table_offset
;
98 uint32_t max_table_entries
; // 32bit/entry
100 // 2 MB by default, must be a power of two
104 uint8_t parent_uuid
[16];
105 uint32_t parent_timestamp
;
108 // Backing file name (in UTF-16)
109 uint8_t parent_name
[512];
114 uint32_t data_length
;
116 uint64_t data_offset
;
118 } QEMU_PACKED VHDDynDiskHeader
;
120 typedef struct BDRVVPCState
{
122 uint8_t footer_buf
[HEADER_SIZE
];
123 uint64_t free_data_block_offset
;
124 int max_table_entries
;
127 uint64_t last_bitmap_offset
;
130 uint32_t bitmap_size
;
133 uint8_t *pageentry_u8
;
134 uint32_t *pageentry_u32
;
135 uint16_t *pageentry_u16
;
137 uint64_t last_bitmap
;
140 Error
*migration_blocker
;
143 static uint32_t vpc_checksum(uint8_t* buf
, size_t size
)
148 for (i
= 0; i
< size
; i
++)
155 static int vpc_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
157 if (buf_size
>= 8 && !strncmp((char *)buf
, "conectix", 8))
162 static int vpc_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
165 BDRVVPCState
*s
= bs
->opaque
;
168 VHDDynDiskHeader
*dyndisk_header
;
169 uint8_t buf
[HEADER_SIZE
];
171 uint64_t computed_size
;
172 uint64_t pagetable_size
;
173 int disk_type
= VHD_DYNAMIC
;
176 ret
= bdrv_pread(bs
->file
->bs
, 0, s
->footer_buf
, HEADER_SIZE
);
181 footer
= (VHDFooter
*) s
->footer_buf
;
182 if (strncmp(footer
->creator
, "conectix", 8)) {
183 int64_t offset
= bdrv_getlength(bs
->file
->bs
);
187 } else if (offset
< HEADER_SIZE
) {
192 /* If a fixed disk, the footer is found only at the end of the file */
193 ret
= bdrv_pread(bs
->file
->bs
, offset
-HEADER_SIZE
, s
->footer_buf
,
198 if (strncmp(footer
->creator
, "conectix", 8)) {
199 error_setg(errp
, "invalid VPC image");
203 disk_type
= VHD_FIXED
;
206 checksum
= be32_to_cpu(footer
->checksum
);
207 footer
->checksum
= 0;
208 if (vpc_checksum(s
->footer_buf
, HEADER_SIZE
) != checksum
)
209 fprintf(stderr
, "block-vpc: The header checksum of '%s' is "
210 "incorrect.\n", bs
->filename
);
212 /* Write 'checksum' back to footer, or else will leave it with zero. */
213 footer
->checksum
= cpu_to_be32(checksum
);
215 // The visible size of a image in Virtual PC depends on the geometry
216 // rather than on the size stored in the footer (the size in the footer
217 // is too large usually)
218 bs
->total_sectors
= (int64_t)
219 be16_to_cpu(footer
->cyls
) * footer
->heads
* footer
->secs_per_cyl
;
221 /* Images that have exactly the maximum geometry are probably bigger and
222 * would be truncated if we adhered to the geometry for them. Rely on
223 * footer->current_size for them. */
224 if (bs
->total_sectors
== VHD_MAX_GEOMETRY
) {
225 bs
->total_sectors
= be64_to_cpu(footer
->current_size
) /
229 /* Allow a maximum disk size of approximately 2 TB */
230 if (bs
->total_sectors
>= VHD_MAX_SECTORS
) {
235 if (disk_type
== VHD_DYNAMIC
) {
236 ret
= bdrv_pread(bs
->file
->bs
, be64_to_cpu(footer
->data_offset
), buf
,
242 dyndisk_header
= (VHDDynDiskHeader
*) buf
;
244 if (strncmp(dyndisk_header
->magic
, "cxsparse", 8)) {
249 s
->block_size
= be32_to_cpu(dyndisk_header
->block_size
);
250 if (!is_power_of_2(s
->block_size
) || s
->block_size
< BDRV_SECTOR_SIZE
) {
251 error_setg(errp
, "Invalid block size %" PRIu32
, s
->block_size
);
255 s
->bitmap_size
= ((s
->block_size
/ (8 * 512)) + 511) & ~511;
257 s
->max_table_entries
= be32_to_cpu(dyndisk_header
->max_table_entries
);
259 if ((bs
->total_sectors
* 512) / s
->block_size
> 0xffffffffU
) {
263 if (s
->max_table_entries
> (VHD_MAX_SECTORS
* 512) / s
->block_size
) {
268 computed_size
= (uint64_t) s
->max_table_entries
* s
->block_size
;
269 if (computed_size
< bs
->total_sectors
* 512) {
274 if (s
->max_table_entries
> SIZE_MAX
/ 4 ||
275 s
->max_table_entries
> (int) INT_MAX
/ 4) {
276 error_setg(errp
, "Max Table Entries too large (%" PRId32
")",
277 s
->max_table_entries
);
282 pagetable_size
= (uint64_t) s
->max_table_entries
* 4;
284 s
->pagetable
= qemu_try_blockalign(bs
->file
->bs
, pagetable_size
);
285 if (s
->pagetable
== NULL
) {
290 s
->bat_offset
= be64_to_cpu(dyndisk_header
->table_offset
);
292 ret
= bdrv_pread(bs
->file
->bs
, s
->bat_offset
, s
->pagetable
,
298 s
->free_data_block_offset
=
299 ROUND_UP(s
->bat_offset
+ pagetable_size
, 512);
301 for (i
= 0; i
< s
->max_table_entries
; i
++) {
302 be32_to_cpus(&s
->pagetable
[i
]);
303 if (s
->pagetable
[i
] != 0xFFFFFFFF) {
304 int64_t next
= (512 * (int64_t) s
->pagetable
[i
]) +
305 s
->bitmap_size
+ s
->block_size
;
307 if (next
> s
->free_data_block_offset
) {
308 s
->free_data_block_offset
= next
;
313 if (s
->free_data_block_offset
> bdrv_getlength(bs
->file
->bs
)) {
314 error_setg(errp
, "block-vpc: free_data_block_offset points after "
315 "the end of file. The image has been truncated.");
320 s
->last_bitmap_offset
= (int64_t) -1;
323 s
->pageentry_u8
= g_malloc(512);
324 s
->pageentry_u32
= s
->pageentry_u8
;
325 s
->pageentry_u16
= s
->pageentry_u8
;
326 s
->last_pagetable
= -1;
330 qemu_co_mutex_init(&s
->lock
);
332 /* Disable migration when VHD images are used */
333 error_setg(&s
->migration_blocker
, "The vpc format used by node '%s' "
334 "does not support live migration",
335 bdrv_get_device_or_node_name(bs
));
336 migrate_add_blocker(s
->migration_blocker
);
341 qemu_vfree(s
->pagetable
);
343 g_free(s
->pageentry_u8
);
348 static int vpc_reopen_prepare(BDRVReopenState
*state
,
349 BlockReopenQueue
*queue
, Error
**errp
)
355 * Returns the absolute byte offset of the given sector in the image file.
356 * If the sector is not allocated, -1 is returned instead.
358 * The parameter write must be 1 if the offset will be used for a write
359 * operation (the block bitmaps is updated then), 0 otherwise.
361 static inline int64_t get_sector_offset(BlockDriverState
*bs
,
362 int64_t sector_num
, int write
)
364 BDRVVPCState
*s
= bs
->opaque
;
365 uint64_t offset
= sector_num
* 512;
366 uint64_t bitmap_offset
, block_offset
;
367 uint32_t pagetable_index
, pageentry_index
;
369 pagetable_index
= offset
/ s
->block_size
;
370 pageentry_index
= (offset
% s
->block_size
) / 512;
372 if (pagetable_index
>= s
->max_table_entries
|| s
->pagetable
[pagetable_index
] == 0xffffffff)
373 return -1; // not allocated
375 bitmap_offset
= 512 * (uint64_t) s
->pagetable
[pagetable_index
];
376 block_offset
= bitmap_offset
+ s
->bitmap_size
+ (512 * pageentry_index
);
378 // We must ensure that we don't write to any sectors which are marked as
379 // unused in the bitmap. We get away with setting all bits in the block
380 // bitmap each time we write to a new block. This might cause Virtual PC to
381 // miss sparse read optimization, but it's not a problem in terms of
383 if (write
&& (s
->last_bitmap_offset
!= bitmap_offset
)) {
384 uint8_t bitmap
[s
->bitmap_size
];
386 s
->last_bitmap_offset
= bitmap_offset
;
387 memset(bitmap
, 0xff, s
->bitmap_size
);
388 bdrv_pwrite_sync(bs
->file
->bs
, bitmap_offset
, bitmap
, s
->bitmap_size
);
395 * Writes the footer to the end of the image file. This is needed when the
396 * file grows as it overwrites the old footer
398 * Returns 0 on success and < 0 on error
400 static int rewrite_footer(BlockDriverState
* bs
)
403 BDRVVPCState
*s
= bs
->opaque
;
404 int64_t offset
= s
->free_data_block_offset
;
406 ret
= bdrv_pwrite_sync(bs
->file
->bs
, offset
, s
->footer_buf
, HEADER_SIZE
);
414 * Allocates a new block. This involves writing a new footer and updating
415 * the Block Allocation Table to use the space at the old end of the image
416 * file (overwriting the old footer)
418 * Returns the sectors' offset in the image file on success and < 0 on error
420 static int64_t alloc_block(BlockDriverState
* bs
, int64_t sector_num
)
422 BDRVVPCState
*s
= bs
->opaque
;
424 uint32_t index
, bat_value
;
426 uint8_t bitmap
[s
->bitmap_size
];
428 // Check if sector_num is valid
429 if ((sector_num
< 0) || (sector_num
> bs
->total_sectors
))
432 // Write entry into in-memory BAT
433 index
= (sector_num
* 512) / s
->block_size
;
434 if (s
->pagetable
[index
] != 0xFFFFFFFF)
437 s
->pagetable
[index
] = s
->free_data_block_offset
/ 512;
439 // Initialize the block's bitmap
440 memset(bitmap
, 0xff, s
->bitmap_size
);
441 ret
= bdrv_pwrite_sync(bs
->file
->bs
, s
->free_data_block_offset
, bitmap
,
447 // Write new footer (the old one will be overwritten)
448 s
->free_data_block_offset
+= s
->block_size
+ s
->bitmap_size
;
449 ret
= rewrite_footer(bs
);
453 // Write BAT entry to disk
454 bat_offset
= s
->bat_offset
+ (4 * index
);
455 bat_value
= cpu_to_be32(s
->pagetable
[index
]);
456 ret
= bdrv_pwrite_sync(bs
->file
->bs
, bat_offset
, &bat_value
, 4);
460 return get_sector_offset(bs
, sector_num
, 0);
463 s
->free_data_block_offset
-= (s
->block_size
+ s
->bitmap_size
);
467 static int vpc_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
469 BDRVVPCState
*s
= (BDRVVPCState
*)bs
->opaque
;
470 VHDFooter
*footer
= (VHDFooter
*) s
->footer_buf
;
472 if (be32_to_cpu(footer
->type
) != VHD_FIXED
) {
473 bdi
->cluster_size
= s
->block_size
;
476 bdi
->unallocated_blocks_are_zero
= true;
480 static int vpc_read(BlockDriverState
*bs
, int64_t sector_num
,
481 uint8_t *buf
, int nb_sectors
)
483 BDRVVPCState
*s
= bs
->opaque
;
486 int64_t sectors
, sectors_per_block
;
487 VHDFooter
*footer
= (VHDFooter
*) s
->footer_buf
;
489 if (be32_to_cpu(footer
->type
) == VHD_FIXED
) {
490 return bdrv_read(bs
->file
->bs
, sector_num
, buf
, nb_sectors
);
492 while (nb_sectors
> 0) {
493 offset
= get_sector_offset(bs
, sector_num
, 0);
495 sectors_per_block
= s
->block_size
>> BDRV_SECTOR_BITS
;
496 sectors
= sectors_per_block
- (sector_num
% sectors_per_block
);
497 if (sectors
> nb_sectors
) {
498 sectors
= nb_sectors
;
502 memset(buf
, 0, sectors
* BDRV_SECTOR_SIZE
);
504 ret
= bdrv_pread(bs
->file
->bs
, offset
, buf
,
505 sectors
* BDRV_SECTOR_SIZE
);
506 if (ret
!= sectors
* BDRV_SECTOR_SIZE
) {
511 nb_sectors
-= sectors
;
512 sector_num
+= sectors
;
513 buf
+= sectors
* BDRV_SECTOR_SIZE
;
518 static coroutine_fn
int vpc_co_read(BlockDriverState
*bs
, int64_t sector_num
,
519 uint8_t *buf
, int nb_sectors
)
522 BDRVVPCState
*s
= bs
->opaque
;
523 qemu_co_mutex_lock(&s
->lock
);
524 ret
= vpc_read(bs
, sector_num
, buf
, nb_sectors
);
525 qemu_co_mutex_unlock(&s
->lock
);
529 static int vpc_write(BlockDriverState
*bs
, int64_t sector_num
,
530 const uint8_t *buf
, int nb_sectors
)
532 BDRVVPCState
*s
= bs
->opaque
;
534 int64_t sectors
, sectors_per_block
;
536 VHDFooter
*footer
= (VHDFooter
*) s
->footer_buf
;
538 if (be32_to_cpu(footer
->type
) == VHD_FIXED
) {
539 return bdrv_write(bs
->file
->bs
, sector_num
, buf
, nb_sectors
);
541 while (nb_sectors
> 0) {
542 offset
= get_sector_offset(bs
, sector_num
, 1);
544 sectors_per_block
= s
->block_size
>> BDRV_SECTOR_BITS
;
545 sectors
= sectors_per_block
- (sector_num
% sectors_per_block
);
546 if (sectors
> nb_sectors
) {
547 sectors
= nb_sectors
;
551 offset
= alloc_block(bs
, sector_num
);
556 ret
= bdrv_pwrite(bs
->file
->bs
, offset
, buf
,
557 sectors
* BDRV_SECTOR_SIZE
);
558 if (ret
!= sectors
* BDRV_SECTOR_SIZE
) {
562 nb_sectors
-= sectors
;
563 sector_num
+= sectors
;
564 buf
+= sectors
* BDRV_SECTOR_SIZE
;
570 static coroutine_fn
int vpc_co_write(BlockDriverState
*bs
, int64_t sector_num
,
571 const uint8_t *buf
, int nb_sectors
)
574 BDRVVPCState
*s
= bs
->opaque
;
575 qemu_co_mutex_lock(&s
->lock
);
576 ret
= vpc_write(bs
, sector_num
, buf
, nb_sectors
);
577 qemu_co_mutex_unlock(&s
->lock
);
581 static int64_t coroutine_fn
vpc_co_get_block_status(BlockDriverState
*bs
,
582 int64_t sector_num
, int nb_sectors
, int *pnum
, BlockDriverState
**file
)
584 BDRVVPCState
*s
= bs
->opaque
;
585 VHDFooter
*footer
= (VHDFooter
*) s
->footer_buf
;
586 int64_t start
, offset
;
590 if (be32_to_cpu(footer
->type
) == VHD_FIXED
) {
592 *file
= bs
->file
->bs
;
593 return BDRV_BLOCK_RAW
| BDRV_BLOCK_OFFSET_VALID
| BDRV_BLOCK_DATA
|
594 (sector_num
<< BDRV_SECTOR_BITS
);
597 offset
= get_sector_offset(bs
, sector_num
, 0);
599 allocated
= (offset
!= -1);
603 /* All sectors in a block are contiguous (without using the bitmap) */
604 n
= ROUND_UP(sector_num
+ 1, s
->block_size
/ BDRV_SECTOR_SIZE
)
606 n
= MIN(n
, nb_sectors
);
611 /* *pnum can't be greater than one block for allocated
612 * sectors since there is always a bitmap in between. */
614 *file
= bs
->file
->bs
;
615 return BDRV_BLOCK_DATA
| BDRV_BLOCK_OFFSET_VALID
| start
;
617 if (nb_sectors
== 0) {
620 offset
= get_sector_offset(bs
, sector_num
, 0);
621 } while (offset
== -1);
627 * Calculates the number of cylinders, heads and sectors per cylinder
628 * based on a given number of sectors. This is the algorithm described
629 * in the VHD specification.
631 * Note that the geometry doesn't always exactly match total_sectors but
634 * Returns 0 on success, -EFBIG if the size is larger than ~2 TB. Override
635 * the hardware EIDE and ATA-2 limit of 16 heads (max disk size of 127 GB)
636 * and instead allow up to 255 heads.
638 static int calculate_geometry(int64_t total_sectors
, uint16_t* cyls
,
639 uint8_t* heads
, uint8_t* secs_per_cyl
)
641 uint32_t cyls_times_heads
;
643 total_sectors
= MIN(total_sectors
, VHD_MAX_GEOMETRY
);
645 if (total_sectors
>= 65535LL * 16 * 63) {
648 cyls_times_heads
= total_sectors
/ *secs_per_cyl
;
651 cyls_times_heads
= total_sectors
/ *secs_per_cyl
;
652 *heads
= (cyls_times_heads
+ 1023) / 1024;
658 if (cyls_times_heads
>= (*heads
* 1024) || *heads
> 16) {
661 cyls_times_heads
= total_sectors
/ *secs_per_cyl
;
664 if (cyls_times_heads
>= (*heads
* 1024)) {
667 cyls_times_heads
= total_sectors
/ *secs_per_cyl
;
671 *cyls
= cyls_times_heads
/ *heads
;
676 static int create_dynamic_disk(BlockDriverState
*bs
, uint8_t *buf
,
677 int64_t total_sectors
)
679 VHDDynDiskHeader
*dyndisk_header
=
680 (VHDDynDiskHeader
*) buf
;
681 size_t block_size
, num_bat_entries
;
686 // Write the footer (twice: at the beginning and at the end)
687 block_size
= 0x200000;
688 num_bat_entries
= (total_sectors
+ block_size
/ 512) / (block_size
/ 512);
690 ret
= bdrv_pwrite_sync(bs
, offset
, buf
, HEADER_SIZE
);
695 offset
= 1536 + ((num_bat_entries
* 4 + 511) & ~511);
696 ret
= bdrv_pwrite_sync(bs
, offset
, buf
, HEADER_SIZE
);
701 // Write the initial BAT
704 memset(buf
, 0xFF, 512);
705 for (i
= 0; i
< (num_bat_entries
* 4 + 511) / 512; i
++) {
706 ret
= bdrv_pwrite_sync(bs
, offset
, buf
, 512);
713 // Prepare the Dynamic Disk Header
714 memset(buf
, 0, 1024);
716 memcpy(dyndisk_header
->magic
, "cxsparse", 8);
719 * Note: The spec is actually wrong here for data_offset, it says
720 * 0xFFFFFFFF, but MS tools expect all 64 bits to be set.
722 dyndisk_header
->data_offset
= cpu_to_be64(0xFFFFFFFFFFFFFFFFULL
);
723 dyndisk_header
->table_offset
= cpu_to_be64(3 * 512);
724 dyndisk_header
->version
= cpu_to_be32(0x00010000);
725 dyndisk_header
->block_size
= cpu_to_be32(block_size
);
726 dyndisk_header
->max_table_entries
= cpu_to_be32(num_bat_entries
);
728 dyndisk_header
->checksum
= cpu_to_be32(vpc_checksum(buf
, 1024));
733 ret
= bdrv_pwrite_sync(bs
, offset
, buf
, 1024);
742 static int create_fixed_disk(BlockDriverState
*bs
, uint8_t *buf
,
747 /* Add footer to total size */
748 total_size
+= HEADER_SIZE
;
750 ret
= bdrv_truncate(bs
, total_size
);
755 ret
= bdrv_pwrite_sync(bs
, total_size
- HEADER_SIZE
, buf
, HEADER_SIZE
);
763 static int vpc_create(const char *filename
, QemuOpts
*opts
, Error
**errp
)
766 VHDFooter
*footer
= (VHDFooter
*) buf
;
767 char *disk_type_param
;
771 uint8_t secs_per_cyl
= 0;
772 int64_t total_sectors
;
776 Error
*local_err
= NULL
;
777 BlockDriverState
*bs
= NULL
;
779 /* Read out options */
780 total_size
= ROUND_UP(qemu_opt_get_size_del(opts
, BLOCK_OPT_SIZE
, 0),
782 disk_type_param
= qemu_opt_get_del(opts
, BLOCK_OPT_SUBFMT
);
783 if (disk_type_param
) {
784 if (!strcmp(disk_type_param
, "dynamic")) {
785 disk_type
= VHD_DYNAMIC
;
786 } else if (!strcmp(disk_type_param
, "fixed")) {
787 disk_type
= VHD_FIXED
;
793 disk_type
= VHD_DYNAMIC
;
796 ret
= bdrv_create_file(filename
, opts
, &local_err
);
798 error_propagate(errp
, local_err
);
801 ret
= bdrv_open(&bs
, filename
, NULL
, NULL
, BDRV_O_RDWR
| BDRV_O_PROTOCOL
,
804 error_propagate(errp
, local_err
);
809 * Calculate matching total_size and geometry. Increase the number of
810 * sectors requested until we get enough (or fail). This ensures that
811 * qemu-img convert doesn't truncate images, but rather rounds up.
813 * If the image size can't be represented by a spec conform CHS geometry,
814 * we set the geometry to 65535 x 16 x 255 (CxHxS) sectors and use
815 * the image size from the VHD footer to calculate total_sectors.
817 total_sectors
= MIN(VHD_MAX_GEOMETRY
, total_size
/ BDRV_SECTOR_SIZE
);
818 for (i
= 0; total_sectors
> (int64_t)cyls
* heads
* secs_per_cyl
; i
++) {
819 calculate_geometry(total_sectors
+ i
, &cyls
, &heads
, &secs_per_cyl
);
822 if ((int64_t)cyls
* heads
* secs_per_cyl
== VHD_MAX_GEOMETRY
) {
823 total_sectors
= total_size
/ BDRV_SECTOR_SIZE
;
824 /* Allow a maximum disk size of approximately 2 TB */
825 if (total_sectors
> VHD_MAX_SECTORS
) {
830 total_sectors
= (int64_t)cyls
* heads
* secs_per_cyl
;
831 total_size
= total_sectors
* BDRV_SECTOR_SIZE
;
834 /* Prepare the Hard Disk Footer */
835 memset(buf
, 0, 1024);
837 memcpy(footer
->creator
, "conectix", 8);
838 /* TODO Check if "qemu" creator_app is ok for VPC */
839 memcpy(footer
->creator_app
, "qemu", 4);
840 memcpy(footer
->creator_os
, "Wi2k", 4);
842 footer
->features
= cpu_to_be32(0x02);
843 footer
->version
= cpu_to_be32(0x00010000);
844 if (disk_type
== VHD_DYNAMIC
) {
845 footer
->data_offset
= cpu_to_be64(HEADER_SIZE
);
847 footer
->data_offset
= cpu_to_be64(0xFFFFFFFFFFFFFFFFULL
);
849 footer
->timestamp
= cpu_to_be32(time(NULL
) - VHD_TIMESTAMP_BASE
);
851 /* Version of Virtual PC 2007 */
852 footer
->major
= cpu_to_be16(0x0005);
853 footer
->minor
= cpu_to_be16(0x0003);
854 footer
->orig_size
= cpu_to_be64(total_size
);
855 footer
->current_size
= cpu_to_be64(total_size
);
856 footer
->cyls
= cpu_to_be16(cyls
);
857 footer
->heads
= heads
;
858 footer
->secs_per_cyl
= secs_per_cyl
;
860 footer
->type
= cpu_to_be32(disk_type
);
862 #if defined(CONFIG_UUID)
863 uuid_generate(footer
->uuid
);
866 footer
->checksum
= cpu_to_be32(vpc_checksum(buf
, HEADER_SIZE
));
868 if (disk_type
== VHD_DYNAMIC
) {
869 ret
= create_dynamic_disk(bs
, buf
, total_sectors
);
871 ret
= create_fixed_disk(bs
, buf
, total_size
);
876 g_free(disk_type_param
);
880 static int vpc_has_zero_init(BlockDriverState
*bs
)
882 BDRVVPCState
*s
= bs
->opaque
;
883 VHDFooter
*footer
= (VHDFooter
*) s
->footer_buf
;
885 if (be32_to_cpu(footer
->type
) == VHD_FIXED
) {
886 return bdrv_has_zero_init(bs
->file
->bs
);
892 static void vpc_close(BlockDriverState
*bs
)
894 BDRVVPCState
*s
= bs
->opaque
;
895 qemu_vfree(s
->pagetable
);
897 g_free(s
->pageentry_u8
);
900 migrate_del_blocker(s
->migration_blocker
);
901 error_free(s
->migration_blocker
);
904 static QemuOptsList vpc_create_opts
= {
905 .name
= "vpc-create-opts",
906 .head
= QTAILQ_HEAD_INITIALIZER(vpc_create_opts
.head
),
909 .name
= BLOCK_OPT_SIZE
,
910 .type
= QEMU_OPT_SIZE
,
911 .help
= "Virtual disk size"
914 .name
= BLOCK_OPT_SUBFMT
,
915 .type
= QEMU_OPT_STRING
,
917 "Type of virtual hard disk format. Supported formats are "
918 "{dynamic (default) | fixed} "
920 { /* end of list */ }
924 static BlockDriver bdrv_vpc
= {
925 .format_name
= "vpc",
926 .instance_size
= sizeof(BDRVVPCState
),
928 .bdrv_probe
= vpc_probe
,
929 .bdrv_open
= vpc_open
,
930 .bdrv_close
= vpc_close
,
931 .bdrv_reopen_prepare
= vpc_reopen_prepare
,
932 .bdrv_create
= vpc_create
,
934 .bdrv_read
= vpc_co_read
,
935 .bdrv_write
= vpc_co_write
,
936 .bdrv_co_get_block_status
= vpc_co_get_block_status
,
938 .bdrv_get_info
= vpc_get_info
,
940 .create_opts
= &vpc_create_opts
,
941 .bdrv_has_zero_init
= vpc_has_zero_init
,
944 static void bdrv_vpc_init(void)
946 bdrv_register(&bdrv_vpc
);
949 block_init(bdrv_vpc_init
);