block/vpc: Code cleanup and bug fix
Bug 1105670 triggered this patch series.
I have currently problems running qemu-iotest.
Maybe someone else can test my code with it.
Regards
Stefan Weil
[PATCH 1/7] block/vpc: Fix most coding style warnings and errors [PATCH 2/7] block/vpc: Add link to available VHD documentation [PATCH 3/7] block/vpc: Replace HEADER_SIZE by FOOTER_SIZE [PATCH 4/7] block/vpc: Improve vpc_open (optimisation and error [PATCH 5/7] block/vpc: Fix size calculation
[PATCH 6/7] block/vpc: Remove workaround for geometry calculation [PATCH 7/7] block/vpc: Use macros for magic constants
PATCH 1/7 block/vpc: Fix most coding style warnings and errors
Only C99 comments remain unfixed.
--- block/vpc.c | 94 +++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 52 insertions(+), 42 deletions(-)
block/vpc.c +52 -42
@@ -136,13 +136,14 @@ typedef struct BDRVVPCState {Error *migration_blocker; } BDRVVPCState; -static uint32_t vpc_checksum (uint8_t * buf,size_t size) +static uint32_t vpc_checksum (uint8_t *buf,size_t size) {uint32_t res =0 ;int i; -for (i =0 ; i < size; i++) +for (i =0 ; i < size; i++) { res += buf[i]; + }return ~res; }
@@ -150,8 +151,9 @@ static uint32_t vpc_checksum(uint8_t* buf, size_t size)static int vpc_probe (const uint8_t *buf,int buf_size,const char *filename) { -if (buf_size >=8 && !strncmp ((char *)buf,"conectix" ,8 )) -return 100 ; +if (buf_size >=8 && !strncmp ((char *)buf,"conectix" ,8 )) { +return 100 ; + }return 0 ; }
@@ -159,17 +161,18 @@ static int vpc_open(BlockDriverState *bs, int flags) {BDRVVPCState *s = bs->opaque;int i; -struct vhd_footer * footer; -struct vhd_dyndisk_header * dyndisk_header; +struct vhd_footer *footer; +struct vhd_dyndisk_header *dyndisk_header;uint8_t buf[HEADER_SIZE ];uint32_t checksum;int err =-1 ;int disk_type =VHD_DYNAMIC ; -if (bdrv_pread (bs->file,0 , s->footer_buf,HEADER_SIZE ) !=HEADER_SIZE ) +if (bdrv_pread (bs->file,0 , s->footer_buf,HEADER_SIZE ) !=HEADER_SIZE ) { goto fail; + } - footer = (struct vhd_footer *) s->footer_buf; + footer = (struct vhd_footer *)s->footer_buf;if (strncmp (footer->creator,"conectix" ,8 )) {int64_t offset =bdrv_getlength (bs->file);if (offset <HEADER_SIZE ) {
@@ -188,9 +191,10 @@ static int vpc_open(BlockDriverState *bs, int flags) checksum =be32_to_cpu (footer->checksum); footer->checksum =0 ; -if (vpc_checksum (s->footer_buf,HEADER_SIZE ) != checksum) +if (vpc_checksum (s->footer_buf,HEADER_SIZE ) != checksum) {fprintf (stderr,"block-vpc: The header checksum of '%s' is " "incorrect.\n" , bs->filename); + }/* Write 'checksum' back to footer, or else will leave it with zero. */ footer->checksum =be32_to_cpu (checksum);
@@ -293,8 +297,10 @@ static inline int64_t get_sector_offset(BlockDriverState *bs, pagetable_index = offset / s->block_size; pageentry_index = (offset % s->block_size) /512 ; -if (pagetable_index >= s->max_table_entries || s->pagetable[pagetable_index] ==0xffffffff ) +if (pagetable_index >= s->max_table_entries || + s->pagetable[pagetable_index] ==0xffffffff ) {return -1 ;// not allocated + } bitmap_offset =512 * (uint64_t ) s->pagetable[pagetable_index]; block_offset = bitmap_offset + s->bitmap_size + (512 * pageentry_index);
@@ -312,35 +318,33 @@ static inline int64_t get_sector_offset(BlockDriverState *bs,bdrv_pwrite_sync (bs->file, bitmap_offset, bitmap, s->bitmap_size); } -// printf("sector: %" PRIx64 ", index: %x, offset: %x, bioff: %" PRIx64 ", bloff: %" PRIx64 "\n", -// sector_num, pagetable_index, pageentry_index, -// bitmap_offset, block_offset); -// disabled by reason #if 0 #ifdef CACHE -if (bitmap_offset != s->last_bitmap) - { -lseek (s->fd, bitmap_offset,SEEK_SET ); +if (bitmap_offset != s->last_bitmap) { +lseek (s->fd, bitmap_offset,SEEK_SET ); - s->last_bitmap = bitmap_offset; + s->last_bitmap = bitmap_offset; -// Scary! Bitmap is stored as big endian 32bit entries, -// while we used to look it up byte by byte -read (s->fd, s->pageentry_u8,512 ); -for (i =0 ; i <128 ; i++) -be32_to_cpus (&s->pageentry_u32[i]); +/* Scary! Bitmap is stored as big endian 32bit entries, +* while we used to look it up byte by byte */ +read (s->fd, s->pageentry_u8,512 ); +for (i =0 ; i <128 ; i++) { +be32_to_cpus (&s->pageentry_u32[i]); + } } -if ((s->pageentry_u8[pageentry_index /8 ] >> (pageentry_index %8 )) &1 ) -return -1 ; +if ((s->pageentry_u8[pageentry_index /8 ] >> (pageentry_index %8 )) &1 ) { +return -1 ; + }#else lseek (s->fd, bitmap_offset + (pageentry_index /8 ),SEEK_SET );read (s->fd, &bitmap_entry,1 ); -if ((bitmap_entry >> (pageentry_index %8 )) &1 ) -return -1 ;// not allocated +if ((bitmap_entry >> (pageentry_index %8 )) &1 ) { +return -1 ;/* not allocated */ + }#endif #endif
@@ -353,15 +357,16 @@ static inline int64_t get_sector_offset(BlockDriverState *bs,* * Returns 0 on success and < 0 on error */ -static int rewrite_footer (BlockDriverState * bs) +static int rewrite_footer (BlockDriverState *bs) {int ret;BDRVVPCState *s = bs->opaque;int64_t offset = s->free_data_block_offset; ret =bdrv_pwrite_sync (bs->file, offset, s->footer_buf,HEADER_SIZE ); -if (ret <0 ) +if (ret <0 ) {return ret; + }return 0 ; }
@@ -373,7 +378,7 @@ static int rewrite_footer(BlockDriverState* bs)* * Returns the sectors' offset in the image file on success and < 0 on error */ -static int64_t alloc_block (BlockDriverState * bs,int64_t sector_num) +static int64_t alloc_block (BlockDriverState *bs,int64_t sector_num) {BDRVVPCState *s = bs->opaque;int64_t bat_offset;
@@ -382,13 +387,15 @@ static int64_t alloc_block(BlockDriverState* bs, int64_t sector_num)uint8_t bitmap[s->bitmap_size];// Check if sector_num is valid -if ((sector_num <0 ) || (sector_num > bs->total_sectors)) +if ((sector_num <0 ) || (sector_num > bs->total_sectors)) {return -1 ; + }// Write entry into in-memory BAT index = (sector_num *512 ) / s->block_size; -if (s->pagetable[index] !=0xFFFFFFFF ) +if (s->pagetable[index] !=0xFFFFFFFF ) {return -1 ; + } s->pagetable[index] = s->free_data_block_offset /512 ;
@@ -403,15 +410,17 @@ static int64_t alloc_block(BlockDriverState* bs, int64_t sector_num)// Write new footer (the old one will be overwritten) s->free_data_block_offset += s->block_size + s->bitmap_size; ret =rewrite_footer (bs); -if (ret <0 ) +if (ret <0 ) { goto fail; + }// Write BAT entry to disk bat_offset = s->bat_offset + (4 * index); bat_value =be32_to_cpu (s->pagetable[index]); ret =bdrv_pwrite_sync (bs->file, bat_offset, &bat_value,4 ); -if (ret <0 ) +if (ret <0 ) { goto fail; + }return get_sector_offset (bs, sector_num,0 );
@@ -492,8 +501,9 @@ static int vpc_write(BlockDriverState *bs, int64_t sector_num,if (offset ==-1 ) { offset =alloc_block (bs, sector_num); -if (offset <0 ) +if (offset <0 ) {return -1 ; + } } ret =bdrv_pwrite (bs->file, offset, buf, sectors *BDRV_SECTOR_SIZE );
@@ -532,8 +542,8 @@ static coroutine_fn int vpc_co_write(BlockDriverState *bs, int64_t sector_num,* the hardware EIDE and ATA-2 limit of 16 heads (max disk size of 127 GB) * and instead allow up to 255 heads. */ -static int calculate_geometry (int64_t total_sectors,uint16_t * cyls, -uint8_t * heads,uint8_t * secs_per_cyl) +static int calculate_geometry (int64_t total_sectors,uint16_t *cyls, +uint8_t *heads,uint8_t *secs_per_cyl) {uint32_t cyls_times_heads;
@@ -555,8 +565,9 @@ static int calculate_geometry(int64_t total_sectors, uint16_t* cyls, cyls_times_heads = total_sectors / *secs_per_cyl; *heads = (cyls_times_heads +1023 ) /1024 ; -if (*heads <4 ) +if (*heads <4 ) { *heads =4 ; + }if (cyls_times_heads >= (*heads *1024 ) || *heads >16 ) { *secs_per_cyl =31 ;
@@ -578,8 +589,8 @@ static int calculate_geometry(int64_t total_sectors, uint16_t* cyls,static int create_dynamic_disk (int fd,uint8_t *buf,int64_t total_sectors) { -struct vhd_dyndisk_header * dyndisk_header = - (struct vhd_dyndisk_header *) buf; +struct vhd_dyndisk_header *dyndisk_header = + (struct vhd_dyndisk_header *)buf;size_t block_size, num_bat_entries;int i;int ret = -EIO ;
@@ -709,8 +720,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options) total_sectors = total_size /BDRV_SECTOR_SIZE ;for (i =0 ; total_sectors > (int64_t )cyls * heads * secs_per_cyl; i++) {if (calculate_geometry (total_sectors + i, &cyls, &heads, - &secs_per_cyl)) - { + &secs_per_cyl)) { ret = -EFBIG ; goto fail; }
Signature · 1 line
1.7.10.4
PATCH 2/7 block/vpc: Add link to available VHD documentation
In reply to Stefan Weil--- block/vpc.c | 3 +++ 1 file changed, 3 insertions(+)
block/vpc.c +3 -0
@@ -21,6 +21,9 @@* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. +* +* See http://en.wikipedia.org/wiki/VHD_(file_format) for more information +* and links to the VHD specification. */ #include "qemu-common.h" #include "block/block_int.h"
Signature · 1 line
1.7.10.4
PATCH 3/7 block/vpc: Replace HEADER_SIZE by FOOTER_SIZE
In reply to Stefan WeilHEADER_SIZE is the size of the footer (the last sector of the VHD image), so its correct name is FOOTER_SIZE.
For the same reason, we have a footer checksum instead of a header checksum.
--- block/vpc.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-)
block/vpc.c +16 -16
@@ -35,7 +35,7 @@/**************************************************************/ -#define HEADER_SIZE 512 +#define FOOTER_SIZE 512//#define CACHE
@@ -118,7 +118,7 @@ struct vhd_dyndisk_header { typedef struct BDRVVPCState { CoMutex lock; - uint8_t footer_buf[HEADER_SIZE]; + uint8_t footer_buf[FOOTER_SIZE]; uint64_t free_data_block_offset; int max_table_entries; uint32_t *pagetable;
@@ -166,24 +166,24 @@ static int vpc_open(BlockDriverState *bs, int flags)int i;struct vhd_footer *footer;struct vhd_dyndisk_header *dyndisk_header; -uint8_t buf[HEADER_SIZE ]; +uint8_t buf[FOOTER_SIZE ];uint32_t checksum;int err =-1 ;int disk_type =VHD_DYNAMIC ; -if (bdrv_pread (bs->file,0 , s->footer_buf,HEADER_SIZE ) !=HEADER_SIZE ) { +if (bdrv_pread (bs->file,0 , s->footer_buf,FOOTER_SIZE ) !=FOOTER_SIZE ) { goto fail; } footer = (struct vhd_footer *)s->footer_buf;if (strncmp (footer->creator,"conectix" ,8 )) {int64_t offset =bdrv_getlength (bs->file); -if (offset <HEADER_SIZE ) { +if (offset <FOOTER_SIZE ) { goto fail; }/* If a fixed disk, the footer is found only at the end of the file */ -if (bdrv_pread (bs->file, offset-HEADER_SIZE , s->footer_buf,HEADER_SIZE ) - !=HEADER_SIZE ) { +if (bdrv_pread (bs->file, offset-FOOTER_SIZE , s->footer_buf,FOOTER_SIZE ) + !=FOOTER_SIZE ) { goto fail; }if (strncmp (footer->creator,"conectix" ,8 )) {
@@ -194,8 +194,8 @@ static int vpc_open(BlockDriverState *bs, int flags) checksum =be32_to_cpu (footer->checksum); footer->checksum =0 ; -if (vpc_checksum (s->footer_buf,HEADER_SIZE ) != checksum) { -fprintf (stderr,"block-vpc: The header checksum of '%s' is " +if (vpc_checksum (s->footer_buf,FOOTER_SIZE ) != checksum) { +fprintf (stderr,"block-vpc: The footer checksum of '%s' is " "incorrect.\n" , bs->filename); }
@@ -216,7 +216,7 @@ static int vpc_open(BlockDriverState *bs, int flags)if (disk_type ==VHD_DYNAMIC ) {if (bdrv_pread (bs->file,be64_to_cpu (footer->data_offset), buf, -HEADER_SIZE ) !=HEADER_SIZE ) { +FOOTER_SIZE ) !=FOOTER_SIZE ) { goto fail; }
@@ -366,7 +366,7 @@ static int rewrite_footer(BlockDriverState *bs)BDRVVPCState *s = bs->opaque;int64_t offset = s->free_data_block_offset; - ret =bdrv_pwrite_sync (bs->file, offset, s->footer_buf,HEADER_SIZE ); + ret =bdrv_pwrite_sync (bs->file, offset, s->footer_buf,FOOTER_SIZE );if (ret <0 ) {return ret; }
@@ -602,14 +602,14 @@ static int create_dynamic_disk(int fd, uint8_t *buf, int64_t total_sectors) block_size =0x200000 ; num_bat_entries = (total_sectors + block_size /512 ) / (block_size /512 ); -if (write (fd, buf,HEADER_SIZE ) !=HEADER_SIZE ) { +if (write (fd, buf,FOOTER_SIZE ) !=FOOTER_SIZE ) { goto fail; }if (lseek (fd,1536 + ((num_bat_entries *4 +511 ) & ~511 ),SEEK_SET ) <0 ) { goto fail; } -if (write (fd, buf,HEADER_SIZE ) !=HEADER_SIZE ) { +if (write (fd, buf,FOOTER_SIZE ) !=FOOTER_SIZE ) { goto fail; }
@@ -669,7 +669,7 @@ static int create_fixed_disk(int fd, uint8_t *buf, int64_t total_size)if (lseek (fd,-512 ,SEEK_END ) <0 ) { goto fail; } -if (write (fd, buf,HEADER_SIZE ) !=HEADER_SIZE ) { +if (write (fd, buf,FOOTER_SIZE ) !=FOOTER_SIZE ) { goto fail; }
@@ -742,7 +742,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options) footer->features =be32_to_cpu (0x02 ); footer->version =be32_to_cpu (0x00010000 );if (disk_type ==VHD_DYNAMIC ) { - footer->data_offset =be64_to_cpu (HEADER_SIZE ); + footer->data_offset =be64_to_cpu (FOOTER_SIZE ); }else { footer->data_offset =be64_to_cpu (0xFFFFFFFFFFFFFFFFULL ); }
@@ -768,7 +768,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options)uuid_generate (footer->uuid);#endif - footer->checksum =be32_to_cpu (vpc_checksum (buf,HEADER_SIZE )); + footer->checksum =be32_to_cpu (vpc_checksum (buf,FOOTER_SIZE ));if (disk_type ==VHD_DYNAMIC ) { ret =create_dynamic_disk (fd, buf, total_sectors);
Signature · 1 line
1.7.10.4
PATCH 4/7 block/vpc: Improve vpc_open (optimisation and error handling)
In reply to Stefan Weil* Always read the footer at the end of the image.
The footer exists for all kinds of VHD images, so there is no need to read its copy at the start of dynamic VHD images.
* Return error codes from bdrv_pread like in other block drivers.
* Return -EMEDIUMTYPE for wrong medium (missing signature).
* It's not necessary to return 0 on success, >= 0 is sufficient.
--- block/vpc.c | 46 ++++++++++++++++++++++------------------------ 1 file changed, 22 insertions(+), 24 deletions(-)
block/vpc.c +22 -24
@@ -164,34 +164,30 @@ static int vpc_open(BlockDriverState *bs, int flags) {BDRVVPCState *s = bs->opaque;int i; -struct vhd_footer *footer; +struct vhd_footer *footer = (struct vhd_footer *)s->footer_buf;struct vhd_dyndisk_header *dyndisk_header;uint8_t buf[FOOTER_SIZE ];uint32_t checksum; -int err =-1 ; +int ret;int disk_type =VHD_DYNAMIC ; +int64_t offset =bdrv_getlength (bs->file); -if (bdrv_pread (bs->file,0 , s->footer_buf,FOOTER_SIZE ) !=FOOTER_SIZE ) { +if (offset <FOOTER_SIZE ) { + ret = -EINVAL ; goto fail; } - footer = (struct vhd_footer *)s->footer_buf; +/* The footer is always found at the end of the file. */ + ret =bdrv_pread (bs->file, offset-FOOTER_SIZE , s->footer_buf,FOOTER_SIZE ); +if (ret <0 ) { + goto fail; + }if (strncmp (footer->creator,"conectix" ,8 )) { -int64_t offset =bdrv_getlength (bs->file); -if (offset <FOOTER_SIZE ) { - goto fail; - } -/* If a fixed disk, the footer is found only at the end of the file */ -if (bdrv_pread (bs->file, offset-FOOTER_SIZE , s->footer_buf,FOOTER_SIZE ) - !=FOOTER_SIZE ) { - goto fail; - } -if (strncmp (footer->creator,"conectix" ,8 )) { - goto fail; - } - disk_type =VHD_FIXED ; + ret = -EMEDIUMTYPE ; + goto fail; } + disk_type =be32_to_cpu (footer->type); checksum =be32_to_cpu (footer->checksum); footer->checksum =0 ;if (vpc_checksum (s->footer_buf,FOOTER_SIZE ) != checksum) {
@@ -210,19 +206,21 @@ static int vpc_open(BlockDriverState *bs, int flags)/* Allow a maximum disk size of approximately 2 TB */ if (bs->total_sectors >=65535LL *255 *255 ) { - err = -EFBIG ; + ret = -EFBIG ; goto fail; }if (disk_type ==VHD_DYNAMIC ) { -if (bdrv_pread (bs->file,be64_to_cpu (footer->data_offset), buf, -FOOTER_SIZE ) !=FOOTER_SIZE ) { + ret =bdrv_pread (bs->file,be64_to_cpu (footer->data_offset), buf, +FOOTER_SIZE ); +if (ret <0 ) { goto fail; } dyndisk_header = (struct vhd_dyndisk_header *) buf;if (strncmp (dyndisk_header->magic,"cxsparse" ,8 )) { + ret = -EINVAL ; goto fail; }
@@ -233,8 +231,9 @@ static int vpc_open(BlockDriverState *bs, int flags) s->pagetable =g_malloc (s->max_table_entries *4 ); s->bat_offset =be64_to_cpu (dyndisk_header->table_offset); -if (bdrv_pread (bs->file, s->bat_offset, s->pagetable, - s->max_table_entries *4 ) != s->max_table_entries *4 ) { + ret =bdrv_pread (bs->file, s->bat_offset, s->pagetable, + s->max_table_entries *4 ); +if (ret <0 ) { goto fail; }
@@ -271,9 +270,8 @@ static int vpc_open(BlockDriverState *bs, int flags) "vpc", bs->device_name, "live migration");migrate_add_blocker (s->migration_blocker); -return 0 ; fail: -return err; +return ret; } static int vpc_reopen_prepare(BDRVReopenState *state,
Signature · 1 line
1.7.10.4
PATCH 5/7 block/vpc: Fix size calculation
In reply to Stefan WeilThe size calculated from the CHS values is not the real image (disk) size, but usually a smaller value (this is caused by rounding effects).
Only older operating systems use CHS. Such guests won't be able to use the whole disk.
This patch fixes https://bugs.launchpad.net/qemu/+bug/1105670/.
--- block/vpc.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-)
block/vpc.c +4 -5
@@ -198,11 +198,10 @@ static int vpc_open(BlockDriverState *bs, int flags)/* Write 'checksum' back to footer, or else will leave it with zero. */ footer->checksum =be32_to_cpu (checksum); -// The visible size of a image in Virtual PC depends on the geometry -// rather than on the size stored in the footer (the size in the footer -// is too large usually) - bs->total_sectors = (int64_t ) -be16_to_cpu (footer->cyls) * footer->heads * footer->secs_per_cyl; +/* The visible size of an image in Virtual PC may depend on the geometry +* rather than on the size stored in the footer (the size in the footer +* is usually larger). Nevertheless we must use the real size here. */ + bs->total_sectors =be64_to_cpu (footer->size) /512 ;/* Allow a maximum disk size of approximately 2 TB */ if (bs->total_sectors >=65535LL *255 *255 ) {
Signature · 1 line
1.7.10.4
PATCH 6/7 block/vpc: Remove workaround for geometry calculation
In reply to Stefan WeilQEMU now gets the correct size for VHD images, so this workaround is no longer needed.
--- block/vpc.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-)
block/vpc.c +4 -9
@@ -681,7 +681,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options)uint8_t buf[1024 ];struct vhd_footer *footer = (struct vhd_footer *) buf;QEMUOptionParameter *disk_type_param; -int fd, i; +int fd;uint16_t cyls =0 ;uint8_t heads =0 ;uint8_t secs_per_cyl =0 ;
@@ -718,16 +718,11 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options)* qemu-img convert doesn't truncate images, but rather rounds up. */ total_sectors = total_size /BDRV_SECTOR_SIZE ; -for (i =0 ; total_sectors > (int64_t )cyls * heads * secs_per_cyl; i++) { -if (calculate_geometry (total_sectors + i, &cyls, &heads, - &secs_per_cyl)) { - ret = -EFBIG ; - goto fail; - } +if (calculate_geometry (total_sectors, &cyls, &heads, &secs_per_cyl)) { + ret = -EFBIG ; + goto fail; } - total_sectors = (int64_t ) cyls * heads * secs_per_cyl; -/* Prepare the Hard Disk Footer */ memset (buf,0 ,1024 );
Signature · 1 line
1.7.10.4
PATCH 7/7 block/vpc: Use macros for magic constants
In reply to Stefan WeilBDRV_SECTOR_SIZE is replaced by VHD_SECTOR_SIZE when the sector size is VHD specific.
--- block/vpc.c | 59 ++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 25 deletions(-)
block/vpc.c +34 -25
@@ -37,6 +37,20 @@#define FOOTER_SIZE 512 +/* By default, VHD uses a block size of 2 MiB for dynamic disk images. */ +#define VHD_DEFAULT_BLOCK_SIZE 0x200000 + +/* VHD uses linked data structures with 64 bit links. +* 0xffffffffffffffff terminates the chain. +* Note: The spec is actually wrong here for data_offset, it says +* 0xffffffff, but MS tools expect all 64 bits to be set. */ +#define VHD_NO_NEXT UINT64_MAX + +/* Last entry in page table. */ +#define VHD_NO_PAGE UINT32_MAX + +#define VHD_SECTOR_SIZE 512 +//#define CACHE enum vhd_type {
@@ -54,7 +68,7 @@ struct vhd_footer {uint32_t features;uint32_t version; -// Offset of next header structure, 0xFFFFFFFF if none +/* Offset of next header structure or VHD_NO_NEXT */ uint64_t data_offset;// Seconds since Jan 1, 2000 0:00:00 (UTC)
@@ -87,7 +101,7 @@ struct vhd_footer {struct vhd_dyndisk_header {char magic[8 ];// "cxsparse" -// Offset of next header structure, 0xFFFFFFFF if none +/* Offset of next header structure or VHD_NO_NEXT */ uint64_t data_offset;// Offset of the Block Allocation Table (BAT)
@@ -201,7 +215,7 @@ static int vpc_open(BlockDriverState *bs, int flags)/* The visible size of an image in Virtual PC may depend on the geometry * rather than on the size stored in the footer (the size in the footer * is usually larger). Nevertheless we must use the real size here. */ - bs->total_sectors =be64_to_cpu (footer->size) /512 ; + bs->total_sectors =be64_to_cpu (footer->size) /VHD_SECTOR_SIZE ;/* Allow a maximum disk size of approximately 2 TB */ if (bs->total_sectors >=65535LL *255 *255 ) {
@@ -241,7 +255,7 @@ static int vpc_open(BlockDriverState *bs, int flags)for (i =0 ; i < s->max_table_entries; i++) {be32_to_cpus (&s->pagetable[i]); -if (s->pagetable[i] !=0xFFFFFFFF ) { +if (s->pagetable[i] !=VHD_NO_PAGE ) {int64_t next = (512 * (int64_t ) s->pagetable[i]) + s->bitmap_size + s->block_size;
@@ -290,7 +304,7 @@ static inline int64_t get_sector_offset(BlockDriverState *bs, int64_t sector_num, int write) {BDRVVPCState *s = bs->opaque; -uint64_t offset = sector_num *512 ; +uint64_t offset = sector_num *VHD_SECTOR_SIZE ;uint64_t bitmap_offset, block_offset;uint32_t pagetable_index, pageentry_index;
@@ -298,7 +312,7 @@ static inline int64_t get_sector_offset(BlockDriverState *bs, pageentry_index = (offset % s->block_size) /512 ;if (pagetable_index >= s->max_table_entries || - s->pagetable[pagetable_index] ==0xffffffff ) { + s->pagetable[pagetable_index] ==VHD_NO_PAGE ) {return -1 ;// not allocated }
@@ -392,8 +406,8 @@ static int64_t alloc_block(BlockDriverState *bs, int64_t sector_num) }// Write entry into in-memory BAT - index = (sector_num *512 ) / s->block_size; -if (s->pagetable[index] !=0xFFFFFFFF ) { + index = (sector_num *VHD_SECTOR_SIZE ) / s->block_size; +if (s->pagetable[index] !=VHD_NO_PAGE ) {return -1 ; }
@@ -451,18 +465,17 @@ static int vpc_read(BlockDriverState *bs, int64_t sector_num, }if (offset ==-1 ) { -memset (buf,0 , sectors *BDRV_SECTOR_SIZE ); +memset (buf,0 , sectors *VHD_SECTOR_SIZE ); }else { - ret =bdrv_pread (bs->file, offset, buf, - sectors *BDRV_SECTOR_SIZE ); -if (ret != sectors *BDRV_SECTOR_SIZE ) { + ret =bdrv_pread (bs->file, offset, buf, sectors *VHD_SECTOR_SIZE ); +if (ret <0 ) {return -1 ; } } nb_sectors -= sectors; sector_num += sectors; - buf += sectors *BDRV_SECTOR_SIZE ; + buf += sectors *VHD_SECTOR_SIZE ; }return 0 ; }
@@ -506,14 +519,14 @@ static int vpc_write(BlockDriverState *bs, int64_t sector_num, } } - ret =bdrv_pwrite (bs->file, offset, buf, sectors *BDRV_SECTOR_SIZE ); -if (ret != sectors *BDRV_SECTOR_SIZE ) { + ret =bdrv_pwrite (bs->file, offset, buf, sectors *VHD_SECTOR_SIZE ); +if (ret <0 ) {return -1 ; } nb_sectors -= sectors; sector_num += sectors; - buf += sectors *BDRV_SECTOR_SIZE ; + buf += sectors *VHD_SECTOR_SIZE ; }return 0 ;
@@ -596,7 +609,7 @@ static int create_dynamic_disk(int fd, uint8_t *buf, int64_t total_sectors)int ret = -EIO ;// Write the footer (twice: at the beginning and at the end) - block_size =0x200000 ; + block_size =VHD_DEFAULT_BLOCK_SIZE ; num_bat_entries = (total_sectors + block_size /512 ) / (block_size /512 );if (write (fd, buf,FOOTER_SIZE ) !=FOOTER_SIZE ) {
@@ -627,11 +640,7 @@ static int create_dynamic_disk(int fd, uint8_t *buf, int64_t total_sectors)memcpy (dyndisk_header->magic,"cxsparse" ,8 ); -/* -* Note: The spec is actually wrong here for data_offset, it says -* 0xFFFFFFFF, but MS tools expect all 64 bits to be set. -*/ - dyndisk_header->data_offset =be64_to_cpu (0xFFFFFFFFFFFFFFFFULL ); + dyndisk_header->data_offset =be64_to_cpu (VHD_NO_NEXT ); dyndisk_header->table_offset =be64_to_cpu (3 *512 ); dyndisk_header->version =be32_to_cpu (0x00010000 ); dyndisk_header->block_size =be32_to_cpu (block_size);
@@ -717,7 +726,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options)* sectors requested until we get enough (or fail). This ensures that * qemu-img convert doesn't truncate images, but rather rounds up. */ - total_sectors = total_size /BDRV_SECTOR_SIZE ; + total_sectors = total_size /512 ;if (calculate_geometry (total_sectors, &cyls, &heads, &secs_per_cyl)) { ret = -EFBIG ; goto fail;
@@ -736,7 +745,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options)if (disk_type ==VHD_DYNAMIC ) { footer->data_offset =be64_to_cpu (FOOTER_SIZE ); }else { - footer->data_offset =be64_to_cpu (0xFFFFFFFFFFFFFFFFULL ); + footer->data_offset =be64_to_cpu (VHD_NO_NEXT ); } footer->timestamp =be32_to_cpu (time (NULL ) -VHD_TIMESTAMP_BASE );
@@ -745,7 +754,7 @@ static int vpc_create(const char *filename, QEMUOptionParameter *options) footer->minor =be16_to_cpu (0x0003 );if (disk_type ==VHD_DYNAMIC ) { footer->orig_size =be64_to_cpu (total_sectors *512 ); - footer->size =be64_to_cpu (total_sectors *512 ); + footer->size =be64_to_cpu (total_sectors *VHD_SECTOR_SIZE ); }else { footer->orig_size =be64_to_cpu (total_size); footer->size =be64_to_cpu (total_size);
Signature · 1 line
1.7.10.4