Support vhd type VHD_DIFFERENCING
PATCH v10 1/1 Support vhd type VHD_DIFFERENCING
Now qemu only supports vhd type VHD_FIXED and VHD_DYNAMIC, so qemu can't read snapshot volume of vhd, and can't support other storage features of vhd file.
This patch add read parent information in function "vpc_open", read bitmap in "vpc_read", and change bitmap in "vpc_write".
---
Changes since v9
- use the g_iconv* to change iconv* (Stefan Hajnoczi)
- change the value of PLATFORM_* to the value in MicroSoft's draft
- change the parent_locator_offset from int to uint64_t (Stefan Hajnoczi)
Changes since v8
- use backing_format to avoid being probed to format of raw
Changes since v7:
- use iconv to decode UTF-16LE(w2u) and UTF-8(macx) to ASCII (Stefan Hajnoczi)
- change the return value of vpc_write back to 0 (Stefan Hajnoczi)
Changes since v6:
- remove include of iconv.h (Stefan Hajnoczi)
- make sure data_length < length of backing_file (Stefan Hajnoczi)
- change big-ending of platform to cpu order (Stefan Hajnoczi)
Changes since v5:
- Change malloc to g_malloc. (Gonglei)(Stefan Hajnoczi)
- Fix the bug of free(null). (Gonglei)(Stefan Hajnoczi)
Changes since v4:
- Parse the batmap only when the version of VHD > 1.2. (Lucian Petrut)
- Add support to parent location of W2RU. (Lucian Petrut) (Philipp Hahn)
Changes since v3:
- Remove the PARENT_MAX_LOC.
Changes since v2:
- Change MACX to PLATFAORM_MACX. (Kevin Wolf)
- Return with EINVAL to parent location is W2RU and W2KU. (Kevin Wolf)
- Change -1 == ret to a natrual order of ret == -1. (Kevin Wolf)
- Get rid of the get_sector_offset_diff, get_sector_offset supports VHD_DIFF. (Kevin Wolf)
- Return code of get_sector_offset is set to, -1 for error, -2 for not allocate, -3 for in parent. (Kevin Wolf)
- Fix un init ret of vpc_write, when nb_sector == 0. (Kevin Wolf)
- Change if (diff == ture) to if (diff) and so on. (Kevin Wolf)
- Add PARENT_MAX_LOC to more understand. (Kevin Wolf)
- Restore the boundary check to write on dynamic type in get_sector_offset. (Kevin Wolf)
Changes since v1:
- Add Boundary check to any input. (Stefan Hajnoczi)
- Clean the code no used after in vpc_open. (Stefan Hajnoczi)
- Change bdrv_co_readv() to bdrv_preadv in vpc_read. (Stefan Hajnoczi)
- Added some code to make it easy to understand. (Stefan Hajnoczi
--- block/vpc.c | 554 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 490 insertions(+), 64 deletions(-)
block/vpc.c +490 -64
@@ -29,17 +29,29 @@#if defined(CONFIG_UUID )#include <uuid/uuid.h> #endif +#include <glib.h> /**************************************************************/ #define HEADER_SIZE 512 +#define DYNAMIC_HEADER_SIZE 1024 +#define PARENT_LOCATOR_NUM 8 +#define TBBATMAP_HEAD_SIZE 28 + +#define MACX_PREFIX_LEN 7/* file:// */ + +#define PLATFORM_MACX 0x4D616358 +#define PLATFORM_W2RU 0x57327275 +#define PLATFORM_W2KU 0x57326B75 + +#define VHD_VERSION (major, minor) (((major) << 16) | ((minor) & 0x0000FFFF))//#define CACHE enum vhd_type {VHD_FIXED =2 ,VHD_DYNAMIC =3 , -VHD_DIFFERENCING =4 , +VHD_DIFF =4 , };// Seconds since Jan 1, 2000 0:00:00 (UTC)
@@ -138,6 +150,15 @@ typedef struct BDRVVPCState {Error *migration_blocker; } BDRVVPCState; +typedef struct vhd_tdbatmap_header { +char magic[8 ];/* always "tdbatmap" */ + +uint64_t batmap_offset; +uint32_t batmap_size; +uint32_t batmap_version; +uint32_t checksum; +} QEMU_PACKED VHDTdBatmapHeader; +static uint32_t vpc_checksum (uint8_t * buf,size_t size) {uint32_t res =0 ;
@@ -157,6 +178,224 @@ static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename)return 0 ; } +static int vpc_decode_maxc_loc (BlockDriverState *bs,uint32_t data_length) +{ +GIConv cd; +gchar *inbuf, *outbuf, *buf; +gsize inbyteleft, outbyteleft, outbyte; +int ret =0 ; +gsize ret1; +gint ret2; + +if (!bs || !bs->backing_file || !data_length) { +return -1 ; + } + + cd =g_iconv_open ("ASCII" ,"UTF8" ); +if (cd == (GIConv) -1 ) { +return -1 ; + } + + inbuf = bs->backing_file; + outbuf = buf = (gchar *)g_malloc (data_length +1 ); +if (!outbuf) { + ret =-1 ; + goto fail2; + } + inbyteleft = outbyteleft = data_length; + + ret1 =g_iconv (cd, &inbuf, &inbyteleft, &outbuf, &outbyteleft); +if (ret1 == (gsize) -1 || inbyteleft) { + ret =-1 ; + goto fail1; + } + outbyte = data_length - outbyteleft; +if (outbyte >sizeof (bs->backing_file) -1 ) { + ret =-1 ; + goto fail1; + } +if (outbyte <MACX_PREFIX_LEN ) { + ret =-1 ; + goto fail1; + } + buf[outbyte] ='\0' ; + +pstrcpy (bs->backing_file,sizeof (bs->backing_file), buf +MACX_PREFIX_LEN ); + +fail1: +g_free (buf); + buf =NULL ; + +fail2: + ret2 =g_iconv_close (cd); +if (ret2 == (gint) -1 ) { + ret =-1 ; + } + +return ret; +} + +static int vpc_decode_w2u_loc (BlockDriverState *bs,uint32_t data_length) +{ +GIConv cd; +gchar *buf, *inbuf, *outbuf; +gsize inbyteleft, outbyteleft, outbyte; +gchar *ptr; +char len =0 ; +int ret =0 ; +gsize ret1; +gint ret2; + +if (!bs || !bs->backing_file || !data_length) { +return -1 ; + } + + cd =g_iconv_open ("ASCII" ,"UTF-16LE" ); +if (cd == (GIConv) -1 ) { +return -1 ; + } + + inbuf = bs->backing_file; + outbuf = buf = ptr = (char *)g_malloc (data_length +1 ); +if (!buf) { + ret =-1 ; + goto fail2; + } + inbyteleft = outbyteleft = data_length; + + ret1 =g_iconv (cd, &inbuf, &inbyteleft, &outbuf, &outbyteleft); +if (ret1 == (gsize) -1 || inbyteleft) { + ret =-1 ; + goto fail1; + } + outbyte = data_length - outbyteleft; +if (outbyte >sizeof (bs->backing_file) -1 ) { + ret =-1 ; + goto fail1; + } + buf[outbyte] ='\0' ; + +while (ptr != outbuf) { +if (*ptr =='\\' ) { + *ptr ='/' ; + } + ptr++; + } + ptr =strstr (buf,":" ); +if (ptr) { + *ptr ='.' ; + len = ptr - buf; + } + +pstrcpy (bs->backing_file,sizeof (bs->backing_file), buf + len); + +fail1: +g_free (buf); + buf =NULL ; + +fail2: + ret2 =g_iconv_close (cd); +if (ret2 == (gint) -1 ) { +return -1 ; + } + +return ret; +} + +static int vpc_decode_parent_loc (uint32_t platform, +BlockDriverState *bs, +uint32_t data_length) +{ +int ret; + +switch (platform) { +case PLATFORM_MACX : + ret =vpc_decode_maxc_loc (bs, data_length); +if (ret <0 ) { +return ret; + } +break ; + +case PLATFORM_W2RU : +/* fall through! */ +case PLATFORM_W2KU : + ret =vpc_decode_w2u_loc (bs, data_length); +if (ret <0 ) { +return ret; + } +break ; + +default : +return -1 ; + } + +return 0 ; +} + +static int vpc_read_backing_loc (VHDDynDiskHeader *dyndisk_header, +BlockDriverState *bs, +Error **errp) +{ +BDRVVPCState *s = bs->opaque; +uint64_t data_offset; +uint32_t data_length; +uint32_t platform; +bool done = false; +uint64_t parent_locator_offset =0 ; +int i; +int ret =0 ; + +for (i =0 ; i <PARENT_LOCATOR_NUM ; i++) { + platform = +be32_to_cpu (dyndisk_header->parent_locator[i].platform); + data_offset = +be64_to_cpu (dyndisk_header->parent_locator[i].data_offset); + data_length = +be32_to_cpu (dyndisk_header->parent_locator[i].data_length); + +/* Extend the location offset */ +if (parent_locator_offset < data_offset) { + parent_locator_offset = data_offset; + } + +if (done) { +continue ; + } + +/* Read location of backing file */ +if (data_offset > s->max_table_entries * s->block_size) { +return -1 ; + } +if (data_length >sizeof (bs->backing_file) -1 ) { +return -1 ; + } + ret =bdrv_pread (bs->file, data_offset, bs->backing_file, + data_length); +if (ret <0 ) { +return ret; + } + + bs->backing_file[data_length] ='\0' ; + +/* Decode the parent location */ + ret =vpc_decode_parent_loc (platform, bs, data_length); +if (ret <0 ) { +return ret; + } + +/* Right parent location is got */ +if (bs->backing_file[0 ] !='\0' ) { + done = true; + } + } + +if (!done) { +return -1 ; + } + +return parent_locator_offset; +} +static int vpc_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {
@@ -164,11 +403,14 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,int i;VHDFooter *footer;VHDDynDiskHeader *dyndisk_header; -uint8_t buf[HEADER_SIZE ]; +uint8_t buf[DYNAMIC_HEADER_SIZE ]; +uint8_t tdbatmap_header_buf[TBBATMAP_HEAD_SIZE ];uint32_t checksum;uint64_t computed_size; -int disk_type =VHD_DYNAMIC ; +uint32_t disk_type;int ret; +VHDTdBatmapHeader *tdbatmap_header; +uint64_t parent_locator_offset =0 ; ret =bdrv_pread (bs->file,0 , s->footer_buf,HEADER_SIZE );if (ret <0 ) {
@@ -176,6 +418,8 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags, } footer = (VHDFooter *) s->footer_buf; + disk_type =be32_to_cpu (footer->type); +if (strncmp (footer->creator,"conectix" ,8 )) {int64_t offset =bdrv_getlength (bs->file);if (offset <0 ) {
@@ -230,9 +474,9 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } -if (disk_type ==VHD_DYNAMIC ) { +if (disk_type ==VHD_DYNAMIC || disk_type ==VHD_DIFF ) { ret =bdrv_pread (bs->file,be64_to_cpu (footer->data_offset), buf, -HEADER_SIZE ); +DYNAMIC_HEADER_SIZE );if (ret <0 ) { goto fail; }
@@ -286,6 +530,47 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags, s->free_data_block_offset = (s->bat_offset + (s->max_table_entries *4 ) +511 ) & ~511 ; +/* Read tdbatmap header by offset */ +if (be32_to_cpu (footer->version) >=VHD_VERSION (1 ,2 )) { + ret =bdrv_pread (bs->file, s->free_data_block_offset, + tdbatmap_header_buf,TBBATMAP_HEAD_SIZE ); +if (ret <0 ) { + goto fail; + } + + tdbatmap_header = (VHDTdBatmapHeader *) tdbatmap_header_buf; +if (!strncmp (tdbatmap_header->magic,"tdbatmap" ,8 )) { + s->free_data_block_offset = +be32_to_cpu (tdbatmap_header->batmap_size) *512 + +be64_to_cpu (tdbatmap_header->batmap_offset); + } + } + +if (dyndisk_header->parent_name[0 ] || dyndisk_header->parent_name[1 ]) { +int len; + +/* Read parent location from dyn header table */ + ret = parent_locator_offset =vpc_read_backing_loc (dyndisk_header, + bs, errp); +if (ret <0 ) { + goto fail; + } + +/* Fix me : Set parent format to avoid probing to raw in +* format probe framework */ + len =strlen ("vpc" ); +if (sizeof (bs->backing_format) -1 < len) { + goto fail; + } +pstrcpy (bs->backing_format,sizeof (bs->backing_format),"vpc" ); + } + +if (s->free_data_block_offset < parent_locator_offset + +BDRV_SECTOR_SIZE ) { + s->free_data_block_offset = parent_locator_offset + +BDRV_SECTOR_SIZE ; + } +for (i =0 ; i < s->max_table_entries; i++) {be32_to_cpus (&s->pagetable[i]);if (s->pagetable[i] !=0xFFFFFFFF ) {
@@ -340,35 +625,76 @@ static int vpc_reopen_prepare(BDRVReopenState *state, }/* -* Returns the absolute byte offset of the given sector in the image file. -* If the sector is not allocated, -1 is returned instead. +* Returns the absolute byte offset of the given sector in the differencing +* image file. +* +* If error happened, -1 is returned. * -* The parameter write must be 1 if the offset will be used for a write -* operation (the block bitmaps is updated then), 0 otherwise. +* When write all type or read dynamic, if the sector is not allocated, -2 +* is returned instead. If the sector is allocated in current file, the block +* offset is returned. +* +* When read diff. If the sector is not allocated, -2 is returned instead. +* If the sector is allocated in the backing file, -3 is returned. If the +* sector is allocated in current file, the block offset is returned. */ static inline int64_t get_sector_offset (BlockDriverState *bs, -int64_t sector_num,int write) +int64_t sector_num,bool write,bool diff) {BDRVVPCState *s = bs->opaque; -uint64_t offset = sector_num *512 ; -uint64_t bitmap_offset, block_offset; +uint64_t offset = sector_num <<BDRV_SECTOR_BITS ; +uint64_t bitmap_offset;uint32_t pagetable_index, pageentry_index; +int64_t block_offset =LONG_MIN ; +int ret; pagetable_index = offset / s->block_size; - pageentry_index = (offset % s->block_size) /512 ; + pageentry_index = (offset % s->block_size) >>BDRV_SECTOR_BITS ; -if (pagetable_index >= s->max_table_entries || s->pagetable[pagetable_index] ==0xffffffff ) -return -1 ;// not allocated +if (pagetable_index >= s->max_table_entries) { +return -2 ; + } +if (s->pagetable[pagetable_index] ==0xffffffff ) { +if (!write && diff) { +return -3 ;/* parent allocated */ + }else { +return -2 ;/* not allocated */ + } + } - bitmap_offset =512 * (uint64_t ) s->pagetable[pagetable_index]; - block_offset = bitmap_offset + s->bitmap_size + (512 * pageentry_index); + bitmap_offset = (uint64_t ) s->pagetable[pagetable_index] + <<BDRV_SECTOR_BITS ; + +if (!diff || write) { + block_offset = bitmap_offset + s->bitmap_size + + (pageentry_index <<BDRV_SECTOR_BITS ); + }else { +uint32_t bitmap_index, bitmapentry_index; +uint8_t bitmap[s->bitmap_size]; +if (bitmap_offset > s->max_table_entries * s->block_size) { +return -1 ; + } + ret =bdrv_pread (bs->file, bitmap_offset, bitmap, s->bitmap_size); +if (ret <0 ) { +return -1 ; + } + + bitmap_index = pageentry_index /8 ; + bitmapentry_index =7 - pageentry_index %8 ; +if (bitmap[bitmap_index] &0x1 << bitmapentry_index) { + block_offset = bitmap_offset + s->bitmap_size + + (pageentry_index <<BDRV_SECTOR_BITS ); + }else { +return -3 ; + } + }// We must ensure that we don't write to any sectors which are marked as // unused in the bitmap. We get away with setting all bits in the block // bitmap each time we write to a new block. This might cause Virtual PC to // miss sparse read optimization, but it's not a problem in terms of // correctness. -if (write && (s->last_bitmap_offset != bitmap_offset)) { +if (!diff && write && (s->last_bitmap_offset != bitmap_offset)) {uint8_t bitmap[s->bitmap_size]; s->last_bitmap_offset = bitmap_offset;
@@ -437,7 +763,8 @@ 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, +bool diff) {BDRVVPCState *s = bs->opaque;int64_t bat_offset;
@@ -457,7 +784,11 @@ static int64_t alloc_block(BlockDriverState* bs, int64_t sector_num) s->pagetable[index] = s->free_data_block_offset /512 ;// Initialize the block's bitmap -memset (bitmap,0xff , s->bitmap_size); +if (diff) { +memset (bitmap,0x0 , s->bitmap_size); + }else { +memset (bitmap,0xff , s->bitmap_size); + } ret =bdrv_pwrite_sync (bs->file, s->free_data_block_offset, bitmap, s->bitmap_size);if (ret <0 ) {
@@ -477,7 +808,7 @@ static int64_t alloc_block(BlockDriverState* bs, int64_t sector_num)if (ret <0 ) goto fail; -return get_sector_offset (bs, sector_num,0 ); +return get_sector_offset (bs, sector_num, false, diff); fail: s->free_data_block_offset -= (s->block_size + s->bitmap_size);
@@ -501,37 +832,71 @@ static int vpc_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors) {BDRVVPCState *s = bs->opaque; -int ret; -int64_t offset; -int64_t sectors, sectors_per_block;VHDFooter *footer = (VHDFooter *) s->footer_buf; +int64_t sectors_per_block = s->block_size >>BDRV_SECTOR_BITS ; +int64_t offset, sectors; +int ret; -if (be32_to_cpu (footer->type) ==VHD_FIXED ) { +switch (be32_to_cpu (footer->type)) { +case VHD_FIXED :return bdrv_read (bs->file, sector_num, buf, nb_sectors); - } -while (nb_sectors >0 ) { - offset =get_sector_offset (bs, sector_num,0 ); - sectors_per_block = s->block_size >>BDRV_SECTOR_BITS ; - sectors = sectors_per_block - (sector_num % sectors_per_block); -if (sectors > nb_sectors) { - sectors = nb_sectors; +case VHD_DYNAMIC : +while (nb_sectors >0 ) { + sectors = sectors_per_block - (sector_num % sectors_per_block); +if (sectors > nb_sectors) { + sectors = nb_sectors; + } + + offset =get_sector_offset (bs, sector_num, false, false); +if (offset ==-1 ) { +return -1 ; + }else if (offset ==-2 ) { +memset (buf,0 , sectors *BDRV_SECTOR_SIZE ); + }else { + ret =bdrv_pread (bs->file, offset, buf, + sectors *BDRV_SECTOR_SIZE ); +if (ret != sectors *BDRV_SECTOR_SIZE ) { +return -1 ; + } + } + + nb_sectors -= sectors; + sector_num += sectors; + buf += sectors *BDRV_SECTOR_SIZE ; } +break ; -if (offset ==-1 ) { -memset (buf,0 , sectors *BDRV_SECTOR_SIZE ); - }else { - ret =bdrv_pread (bs->file, offset, buf, - sectors *BDRV_SECTOR_SIZE ); -if (ret != sectors *BDRV_SECTOR_SIZE ) { +case VHD_DIFF : +while (nb_sectors >0 ) { + offset =get_sector_offset (bs, sector_num, false, true); +if (offset ==-1 ) {return -1 ; + }else if (offset ==-2 ) { +memset (buf,0 ,BDRV_SECTOR_SIZE ); + }else if (offset ==-3 ) { + ret =bdrv_pread (bs->backing_hd, sector_num <<BDRV_SECTOR_BITS + , buf,BDRV_SECTOR_SIZE ); +if (ret <0 ) { +return -1 ; + } + }else { + ret =bdrv_pread (bs->file, offset, buf,BDRV_SECTOR_SIZE ); +if (ret !=BDRV_SECTOR_SIZE ) { +return -1 ; + } } + + nb_sectors--; + sector_num++; + buf +=BDRV_SECTOR_SIZE ; } +break ; - nb_sectors -= sectors; - sector_num += sectors; - buf += sectors *BDRV_SECTOR_SIZE ; +default : +return -1 ; } +return 0 ; }
@@ -546,41 +911,101 @@ static coroutine_fn int vpc_co_read(BlockDriverState *bs, int64_t sector_num,return ret; } +static inline int64_t write_bitmap (BlockDriverState *bs,int64_t sector_num, +int64_t sectors) +{ +BDRVVPCState *s = bs->opaque; +uint64_t offset = sector_num <<BDRV_SECTOR_BITS ; +uint64_t bitmap_offset; +uint32_t pagetable_index, pageentry_index; +uint8_t bitmap[s->bitmap_size]; +uint32_t bitmap_index, bitmapbit_index; +int i; +int ret; + + pagetable_index = offset / s->block_size; + pageentry_index = (offset % s->block_size) /512 ; + bitmap_offset =512 * (uint64_t ) s->pagetable[pagetable_index]; + +if (bitmap_offset > s->max_table_entries * s->block_size) { +return -1 ; + } + ret =bdrv_pread (bs->file, bitmap_offset, bitmap, s->bitmap_size); +if (ret <0 ) { +return -1 ; + } + +for (i =0 ; i < sectors; i++) { + bitmap_index = pageentry_index /8 ; + bitmapbit_index =7 - pageentry_index %8 ; + bitmap[bitmap_index] |= (0x1 << bitmapbit_index); + pageentry_index++; + } + ret =bdrv_pwrite (bs->file, bitmap_offset, bitmap, s->bitmap_size); +if (ret <0 ) { +return -1 ; + } + +return 0 ; +} +static int vpc_write (BlockDriverState *bs,int64_t sector_num,const uint8_t *buf,int nb_sectors) {BDRVVPCState *s = bs->opaque; -int64_t offset; -int64_t sectors, sectors_per_block; -int ret; -VHDFooter *footer = (VHDFooter *) s->footer_buf; +VHDFooter *footer = (VHDFooter *) s->footer_buf; +int64_t sectors_per_block = s->block_size >>BDRV_SECTOR_BITS ; +int64_t offset, sectors; +bool diff = true; +int ret =0 ; -if (be32_to_cpu (footer->type) ==VHD_FIXED ) { +switch (be32_to_cpu (footer->type)) { +case VHD_FIXED :return bdrv_write (bs->file, sector_num, buf, nb_sectors); - } -while (nb_sectors >0 ) { - offset =get_sector_offset (bs, sector_num,1 ); - sectors_per_block = s->block_size >>BDRV_SECTOR_BITS ; - sectors = sectors_per_block - (sector_num % sectors_per_block); -if (sectors > nb_sectors) { - sectors = nb_sectors; - } +case VHD_DYNAMIC : + diff = false; +/* fall-through ! */ + +case VHD_DIFF : +while (nb_sectors >0 ) { + sectors = sectors_per_block - (sector_num % sectors_per_block); +if (sectors > nb_sectors) { + sectors = nb_sectors; + } -if (offset ==-1 ) { - offset =alloc_block (bs, sector_num); -if (offset <0 ) + offset =get_sector_offset (bs, sector_num, true, diff); +if (offset ==-1 ) {return -1 ; - } + }else if (offset ==-2 ) { + offset =alloc_block (bs, sector_num, diff); +if (offset <0 ) { +return -1 ; + } + } - ret =bdrv_pwrite (bs->file, offset, buf, sectors *BDRV_SECTOR_SIZE ); -if (ret != sectors *BDRV_SECTOR_SIZE ) { -return -1 ; + ret =bdrv_pwrite (bs->file, offset, buf, + sectors *BDRV_SECTOR_SIZE ); +if (ret != sectors *BDRV_SECTOR_SIZE ) { +return -1 ; + } + +if (diff) { + ret =write_bitmap (bs, sector_num, sectors); +if (ret <0 ) { +return -1 ; + } + } + + nb_sectors -= sectors; + sector_num += sectors; + buf += sectors *BDRV_SECTOR_SIZE ; } - nb_sectors -= sectors; - sector_num += sectors; - buf += sectors *BDRV_SECTOR_SIZE ; +break ; + +default : +return -1 ; }return 0 ;
@@ -906,6 +1331,7 @@ static BlockDriver bdrv_vpc = { .bdrv_close = vpc_close, .bdrv_reopen_prepare = vpc_reopen_prepare, .bdrv_create = vpc_create, + .supports_backing = true, .bdrv_read = vpc_co_read, .bdrv_write = vpc_co_write,
Signature · 1 line
1.8.3.1
Hi,
Quoted 1 line · Xiaodong Gong
(quoted diff) +0
-0@@ -157,6 +178,224 @@ static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename)
..
Quoted 1 line
+static int
vpc_decode_maxc_loc(BlockDriverState *bs, uint32_t data_length)
...
Quoted 1 line
+ cd =
g_iconv_open("ASCII", "UTF8");
...
Quoted 1 line
+static int
vpc_decode_w2u_loc(BlockDriverState *bs, uint32_t data_length)
...
Quoted 1 line
+ cd =
g_iconv_open("ASCII", "UTF-16LE");
Please correct me if my understanding is wrong, but a hard-coded "ASCII" is AFAIK wrong, as it only contains the 7-bit characters.
For the Linux kernel the file name is just a string of bytes, but when it gets displayed to the user, the bytes are converted to characters. The conversion depends on the locale used, which now-adays is most often UTF-8 (LANG=de_DE.UTF-8, or more specifically LC_CTYPE), but some years back it was ISO-8859-1 (or what-ever).
So if I create a backing file with some non-ASCII umlauts, the conversion will break, as ß = ß = \uc39f = ISO-8859-1(0xdf)
AFAIK using nl_langinfo(CODESET) would return the codeset previously set by setlocale(LC_ALL, ""), which any main program would need to do.
Am I missing something?
Sincerely
Philipp
Quoted 5 lines · 2015年3月5日 06:15于 "Philipp Hahn" <[email protected]>写道:
Hi,
Review static int vpc_probe(const uint8_t *buf, int
Quoted 1 line · Xiaodong Gong
(quoted diff) +0
-0@@ -157,6 +178,224 @@ static int vpc_probe(const uint8_t *buf, int
buf_size, const char *filename)
Quoted 2 lines
..
Quoted 1 line
+static int vpc_decode_maxc_loc(BlockDriverState *bs,
uint32_t
data_length)
Quoted 4 lines
...
Quoted 1 line
+ cd =
g_iconv_open("ASCII", "UTF8");...
Quoted 1 line
+static int vpc_decode_w2u_loc(BlockDriverState *bs,
uint32_t
data_length)
Quoted 6 lines
...
Quoted 1 line
+ cd =
g_iconv_open("ASCII", "UTF-16LE");Please correct me if my understanding is wrong, but a hard-coded "ASCII" is AFAIK wrong, as it only contains the 7-bit characters.
Quoted 9 lines
For the Linux kernel the file name is just a string of bytes, but when it gets displayed to the user, the bytes are converted to characters. The conversion depends on the locale used, which now-adays is most often UTF-8 (LANG=de_DE.UTF-8, or more specifically
LC_CTYPE), but some years back it was ISO-8859-1 (or what-ever).So if I create a backing file with some non-ASCII umlauts, the conversion will break, as ß = ß = \uc39f = ISO-8859-1(0xdf)
the encoding type of parent location is must be utf 8,utf16e,according to the draft
ascii is the encoding type to store the string of parent location in memery and to use fopen()
ascii need to translate to other encoding type accirding to LANG when to show the infomation of the vhd file using the qemu-info and so on
Quoted 7 lines
AFAIK using
nl_langinfo(CODESET)would return the codeset previously set bysetlocale(LC_ALL, ""), which any main program would need to do.Am I missing something?
Sincerely
Philipp
Hello,
Quoted 2 lines · Xiaodong Gong
the encoding type of parent location is must be utf 8,utf16e,according to the draft
Yes, the SPEC for VPC/VHD specifies the character encoding to use, which is good for being portable.
Quoted 2 lines
ascii is the encoding type to store the string of parent location in memery and to use fopen()
No: For the (Linux) kernel the filename is a sequence of 8 bit bytes, where only '\0'=end_of_string and '/'=path_separator are handled specially. All other bytes have no special meaning and are passed in and out as is.
Only the applications are doing the character encoding. Normally this is not a problem as you setup your system once with one encoding (nowadays UTF-8) and use that consistently: If you enter ä on the keyboard, the kernels input layer returns \u00E4 as the two-byte UTF-8 sequence
Quoted 2 lines
$ echo -n ä | xxd -g 1 0000000: c3 a4
Any application can either just pass the byte sequence around as a CLOB (or use any other encoding internally - but then it must know that the input-encoding is UTF-8), but when again doing any system call, they will again pass that same byte sequence as the file-name, which the kernel will store on disk.
If you take that disk to another computer, which does NOT use Unicode, you have a problem: If, for example, that one is still using the old ISO-8859-1 encoding used in western Europe, you file will be named differently:
Quoted 2 lines
$ echo -n ä | iconv -f ISO-8859-1 -t UTF-8 ä
(The reverse is even more painful, as not any ISO-8859-1 character sequence is a valid UTF-8 byte sequence - several years back when I moved from my old ISO-8859-1 to a more modern UTF-8 setup, I had to rename lots of files to be readable again)
You can even test that locally on one system by creating a file containing an umlaut in its name and then to display that in a non-UTF-8 terminal / environment:
Quoted 6 lines
$ touch ä $ LANG=C ls -NQ "\303\244"ascii need to translate to other encoding type according to LANG when to show the information of the vhd file using the qemu-info and so on
No: your assumption that ASCII is used is IMHO wrong: ASCII is only 7 bit, but the kernel interface is 8 bit. The terminal input- and output layer nowadays are UTF-8, so as long as you're working on the console everything is fine. If you mix in GUIs and libraries doing their own encoding/decoding, things get more interesting.
But when you do explicit character conversion like you do for VHD, you must honor the user configured character encoding of the environment yourself, that is use LC_CTYPE for any conversion from input, for output which includes file names.
I checked xen/tools/blktap2/vpc/lib/libvhd.c # vhd_initialize_header_parent_name()
which also (wrongly) assumes ASCII. Because of the creating a snapshot using vhd-utils is also broken:
Quoted 3 lines
$ /usr/bin/vhd-util create -n ä.vhd -s 1 $ /usr/bin/vhd-util snapshot -n snap.vhd -p ä.vhd ; echo $? 84
Next I checked
<https://technet.microsoft.com/de-de/library/gg318052%28v=ws.10%29.aspx>
to create a VHD using umlauts with Windows 7:
Quoted 4 lines
cmd # as Admin
diskpart
create vdisk file="C:\ä.vhd" maximum=2000 type=expandable create vdisk file="C:\snap.vhd" parent="C:\ä.vhd"
But vhd-utils from Xen is broken:
Quoted 2 lines
$ /usr/bin/vhd-util read -n snap.vhd -p VHD Header Summary:
...
Quoted 1 line
Parent name : failed to read name
...
Quoted 3 lines
VHD Parent Locators:
--------------------
locator: : 0
....
Quoted 1 line
failed to read parent name
With the attached patch it works:
Quoted 2 lines
VHD Header Summary:
-------------------
...
Quoted 1 line
Parent name : /ä.vhd
...
Quoted 4 lines
VHD Parent Locators:
--------------------
locator: : 0
code :PLAT_CODE_W2KU
...
Quoted 4 lines
decoded name : /ä.vhd
locator: : 1
code :PLAT_CODE_W2RU
...
Quoted 1 line
decoded name : ./ä.vhd
Hope that clarified things.
Philipp
conectix �ӽwin Wi2k } } �? �����M�N�妟�I"� cxsparse�������� � ��㈚(���G�ā�~n�5��5 C : \ � . v h d W2ku W2ru C : \ � . v h d ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������� . \ � . v h d conectix �ӽwin Wi2k } } �? �����M�N�妟�I"�
From a6acdfff6a48dccf7df4c3d3fab94ea548fa1ff2 Mon Sep 17 00:00:00 2001
Message-Id: <a6acdfff6a48dccf7df4c3d3fab94ea548fa1ff2.1425811906.git.hahn@univention.de>
From: Philipp Hahn <[email protected]>
Date: Sun, 8 Mar 2015 10:55:38 +0100
Subject: [PATCH] VHD: Fix locale aware character encoding handling
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Organization: Univention GmbH, Bremen, Germany
To: [email protected]
ASCII is 7 bit only, which does not work in UTF-8 environments:
Quoted 1 line
failed to read parent name
Setup locale in vhd-util to parse LC_CTYPE and use the right codeset when doing file name encoding and decoding.
Increase allocation for UTF-8 buffer as one UTF-16 character might use twice as much space in UTF-8 (or more).
Don't check outbytesleft==0 as one UTF-8 characters get encoded into 1..8 bytes, so it's perfectly fine (and expected) for the output to have remaining bytes left.
Test-case:
$ ./vhd-util create -n ä.vhd -s 1 $ ./vhd-util snapshot -n snap.vhd -p ä.vhd ; echo $?
See
<http://unix.stackexchange.com/questions/48689/effect-of-lang-on-terminal>
for more information about the details of handling the encoding right.
--- tools/blktap2/vhd/lib/libvhd.c | 27 +++++++++++++++++++-------- tools/blktap2/vhd/vhd-util.c | 3 +++ 2 files changed, 22 insertions(+), 8 deletions(-)
tools/blktap2/vhd/lib/libvhd.c +19 -8
@@ -37,6 +37,7 @@#include <iconv.h> #include <sys/mman.h> #include <sys/stat.h> +#include <langinfo.h> #include "libvhd.h" #include "relative-path.h"
@@ -1296,6 +1297,7 @@ vhd_macx_encode_location(char *name, char **out, int *outlen)size_t ibl, obl;char *uri, *uri_utf8, *uri_utf8p, *ret;const char *urip; +char *codeset; err =0 ; ret =NULL ;
@@ -1304,7 +1306,7 @@ vhd_macx_encode_location(char *name, char **out, int *outlen) len =strlen (name) +strlen ("file://" ); ibl = len; - obl = len; + obl = len *2 ; urip = uri =malloc (ibl +1 ); uri_utf8 = uri_utf8p =malloc (obl);
@@ -1312,7 +1314,8 @@ vhd_macx_encode_location(char *name, char **out, int *outlen)if (!uri || !uri_utf8)return -ENOMEM ; - cd =iconv_open ("UTF-8" ,"ASCII" ); + codeset =nl_langinfo (CODESET ); + cd =iconv_open ("UTF-8" , codeset);if (cd == (iconv_t)-1 ) { err = -errno; goto out;
@@ -1325,7 +1328,7 @@ vhd_macx_encode_location(char *name, char **out, int *outlen) (char **) #endif &urip, &ibl, &uri_utf8p, &obl) == (size_t)-1 || - ibl || obl) { + ibl) { err = (errno ? -errno : -EIO ); goto out; }
@@ -1357,6 +1360,7 @@ vhd_w2u_encode_location(char *name, char **out, int *outlen)size_t ibl, obl;char *uri, *uri_utf16, *uri_utf16p, *tmp, *ret;const char *urip; +char *codeset; err =0 ; ret =NULL ;
@@ -1404,7 +1408,8 @@ vhd_w2u_encode_location(char *name, char **out, int *outlen)* MICROSOFT_COMPAT * little endian unicode here */ - cd =iconv_open ("UTF-16LE" ,"ASCII" ); + codeset =nl_langinfo (CODESET ); + cd =iconv_open ("UTF-16LE" , codeset);if (cd == (iconv_t)-1 ) { err = -errno; goto out;
@@ -1415,7 +1420,7 @@ vhd_w2u_encode_location(char *name, char **out, int *outlen) (char **) #endif &urip, &ibl, &uri_utf16p, &obl) == (size_t)-1 || - ibl || obl) { + ibl) { err = (errno ? -errno : -EIO ); goto out; }
@@ -1447,11 +1452,13 @@ vhd_macx_decode_location(const char *in, char *out, int len)iconv_t cd;char *name;size_t ibl, obl; +char *codeset; name = out; ibl = obl = len; - cd =iconv_open ("ASCII" ,"UTF-8" ); + codeset =nl_langinfo (CODESET ); + cd =iconv_open (codeset,"UTF-8" );if (cd == (iconv_t)-1 )return NULL ;
@@ -1479,11 +1486,13 @@ vhd_w2u_decode_location(const char *in, char *out, int len, char *utf_type)iconv_t cd;char *name, *tmp;size_t ibl, obl; +char *codeset; tmp = name = out; ibl = obl = len; - cd =iconv_open ("ASCII" , utf_type); + codeset =nl_langinfo (CODESET ); + cd =iconv_open (codeset, utf_type);if (cd == (iconv_t)-1 )return NULL ;
@@ -2450,6 +2459,7 @@ vhd_initialize_header_parent_name(vhd_context_t *ctx, const char *parent_path)size_t ibl, obl;char *ppath, *dst;const char *pname; +char *codeset; err =0 ; pname =NULL ;
@@ -2459,7 +2469,8 @@ vhd_initialize_header_parent_name(vhd_context_t *ctx, const char *parent_path)* MICROSOFT_COMPAT * big endian unicode here */ - cd =iconv_open (UTF_16BE ,"ASCII" ); + codeset =nl_langinfo (CODESET ); + cd =iconv_open (UTF_16BE , codeset);if (cd == (iconv_t)-1 ) { err = -errno; goto out;
tools/blktap2/vhd/vhd-util.c +3 -0
@@ -28,6 +28,8 @@#include <stdio.h> #include <stdlib.h> #include <string.h> +#include <langinfo.h> +#include <locale.h> #include "libvhd.h" #include "vhd-util.h"
@@ -114,6 +116,7 @@ main(int argc, char *argv[])if (setrlimit (RLIMIT_CORE , &rlim) <0 )fprintf (stderr,"setrlimit failed: %d\n" , errno);#endif +setlocale (LC_CTYPE ,"" ); ret =0 ;
Signature · 1 line
1.9.1
Quoted 107 lines · 2015年3月8日 18:53于 "Philipp Hahn" <[email protected]>写道:
Hello,
Quoted 2 lines · Xiaodong Gong
the encoding type of parent location is must be utf 8,utf16e,according to the draft
Yes, the SPEC for VPC/VHD specifies the character encoding to use, which is good for being portable.
Quoted 2 lines
ascii is the encoding type to store the string of parent location in memery and to use fopen()
No: For the (Linux) kernel the filename is a sequence of 8 bit bytes, where only '\0'=end_of_string and '/'=path_separator are handled specially. All other bytes have no special meaning and are passed in and out as is.
Only the applications are doing the character encoding. Normally this is not a problem as you setup your system once with one encoding (nowadays UTF-8) and use that consistently: If you enter ä on the keyboard, the kernels input layer returns \u00E4 as the two-byte UTF-8 sequence
Quoted 2 lines
$ echo -n ä | xxd -g 1 0000000: c3 a4Any application can either just pass the byte sequence around as a CLOB (or use any other encoding internally - but then it must know that the input-encoding is UTF-8), but when again doing any system call, they will again pass that same byte sequence as the file-name, which the kernel will store on disk.
If you take that disk to another computer, which does NOT use Unicode, you have a problem: If, for example, that one is still using the old ISO-8859-1 encoding used in western Europe, you file will be named differently:Quoted 2 lines
$ echo -n ä | iconv -f ISO-8859-1 -t UTF-8 ä(The reverse is even more painful, as not any ISO-8859-1 character sequence is a valid UTF-8 byte sequence - several years back when I moved from my old ISO-8859-1 to a more modern UTF-8 setup, I had to rename lots of files to be readable again)
You can even test that locally on one system by creating a file containing an umlaut in its name and then to display that in a non-UTF-8 terminal / environment:
Quoted 6 lines
$ touch ä $ LANG=C ls -NQ "\303\244"ascii need to translate to other encoding type according to LANG when to show the information of the vhd file using the qemu-info and so on
No: your assumption that ASCII is used is IMHO wrong: ASCII is only 7 bit, but the kernel interface is 8 bit. The terminal input- and output layer nowadays are UTF-8, so as long as you're working on the console everything is fine. If you mix in GUIs and libraries doing their own encoding/decoding, things get more interesting.
But when you do explicit character conversion like you do for VHD, you must honor the user configured character encoding of the environment yourself, that is use
LC_CTYPEfor any conversion from input, for output which includes file names.I checked
xen/tools/blktap2/vpc/lib/libvhd.c#vhd_initialize_header_parent_name()
which also (wrongly) assumes ASCII. Because of the creating a snapshot using vhd-utils is also broken:Quoted 3 lines
$ /usr/bin/vhd-util create -n ä.vhd -s 1 $ /usr/bin/vhd-util snapshot -n snap.vhd -p ä.vhd ; echo $? 84Next I checked
<https://technet.microsoft.com/de-de/library/gg318052%28v=ws.10%29.aspx>
to create a VHD using umlauts with Windows 7:Quoted 4 lines
cmd # as Admin
diskpart
create vdisk file="C:\ä.vhd" maximum=2000 type=expandable create vdisk file="C:\snap.vhd" parent="C:\ä.vhd"But vhd-utils from Xen is broken:
Quoted 2 lines
$ /usr/bin/vhd-util read -n snap.vhd -p VHD Header Summary:...
Quoted 1 line
Parent name : failed to read name
...
Quoted 3 lines
VHD Parent Locators:
--------------------
locator: : 0....
Quoted 1 line
failed to read parent name
With the attached patch it works:
Quoted 2 lines
VHD Header Summary:
-------------------...
Quoted 1 line
Parent name : /ä.vhd
...
Quoted 4 lines
VHD Parent Locators:
--------------------
locator: : 0
code :PLAT_CODE_W2KU...
Quoted 2 lines
decoded name : /ä.vhd
Quoted 8 lines
Quoted 2 lines
locator: : 1
code :PLAT_CODE_W2RU...
Quoted 1 line
decoded name : ./ä.vhd
Hope that clarified things.
Philipp
first,your patch is very clear,a good sample.
store ascii code in kernel that I said before is a mistake,I mean the glibc need the input of arguments of fuction such as fopen(path)is ascii code
I think:
icovn_open(utf16le,ascii)in encode
icovn_open(ascii,utf16le)in decode
icovn_open(codeset,ascii)in show
Hello,
Quoted 1 line · Xiaodong Gong
Quoted 1 line
Hope that clarified things.
...
Quoted 5 lines
first,your patch is very clear,a good sample.
store ascii code in kernel that I said before is a mistake,I mean the glibc need the input of arguments of fuction such as fopen(path)is ascii code
No:
ASCII = *7* bit, see "man 7 ascii".
Kernel = *8* bit, that is the kernel doesn't care if you use
ISO-8859-1, UTF-8, BIG5, GB2312, or any other encoding you can get by running "iconv --list".
For the kernel only '\0'=0x00 and '/'=0x2f are special, all other characters the kernel doesn't care for and passes them in and out unmodified.
Most character sets have the ASCII alphabet in their character range 0x00-0x7f, which solved the '\0' and '/' issue nicely.
So again:
- If you use opendir() and readdir(), the kernel returns to you a 8 bit byte sequence.
- To convert that into a character sequence, you must know which encoding was used when the file was created. This information is not stored explicitly in the file-system, file-name, or anywhere else.
- The only hint you get is LC_CTYPE, which is set by the user to tell you which encoding should be used to convert a byte-stream into a character-string.
- If I create a new file on the Linux text console, where I did a "unicode_start", I get an UTF-8 byte sequence from the input layer, which is passed unmodified through the getty and by shell to the create() call. You don't need to know the encoding, you just pass the data in and out unmodified.
- When typing "ls" the kernel again return that byte sequence, which gets passed through the shell to the Linux frame buffer, which translates that UTF-8 sequence to a character and picks the right glyph for being displayed on the screen.
- If I don't switch the Linux console to Unicode mode, I get a different byte sequence from the input layer. That different byte sequence would be stored on the disk when creating a file. (This translation is installed by running the "loadkeys" command.)
- If I do the same in X11, the translation from key-codes to characters and back is done by the terminal (or application). See "man 1 xterm" for "-lc" and "-u8".
- BUT when you want to create a specific character encoding, you MUST know from which encoding you start. Assuming ASCII or UTF-8 is wrong, you MUST check LC_ALL/LC_CTYPE/LANG by querying nl_langinfo(CODESET)).
So if I would give you a disk containing a file with the name "\xa1", depending on the locale you would see a different glyph:
$ for ((c=1;c<=15;c++));do printf '\xa1'|recode ISO-8859-$c..dump-with-names 2>/dev/null|tail -n 1;done 00A1 !I inverted exclamation mark 0104 A; latin capital letter a with ogonek 0126 H/ latin capital letter h with stroke 0104 A; latin capital letter a with ogonek 0401 IO cyrillic capital letter io 201B 9' single high-reversed-9 quotation mark 00A1 !I inverted exclamation mark 0104 A; latin capital letter a with ogonek 00A1 !I inverted exclamation mark 00A1 !I inverted exclamation mark 1E02 B. latin capital letter b with dot above 00A1 !I inverted exclamation mark
In an UTF-8 environment you would get an error, as "\xa1" is not a valid UTF-8 byte sequence.
Read "man 7 unicode", especially the section "Unicode Under Linux" or "man 7 charsets".
Quoted 1 line
I think:
Any program basically has two options:
1. The program does not care about different character sets ans just passes in file-names and data in and out as byte streams. That is perfectly okay and most UNIX shell commands work just fine that way.
2. The program is encoding aware, as for example it works on characters instead of bytes (like "wc --bytes" vs. "wc --chars") or does need to perform a conversion between encodings. Then the sanest thing is to
- query the encoding of the environment once (or per network connection),
- convert any input data from that encoding into a (fixed) internal format like wchar/utf-16/utf-32 including file-names, file-content, etc.
- convert the internal data back into the right format on output, which also includes calling APIs like open().
Otherwise you always have to remember if your char[] buffer contains "some byte stream, which needs to be decoded() before being used" or "already decoded character string". That is why most libraries and frame-works contain wrappers for the file, input- and output, as they internally use one data type for characters consistently and hide all the explicit conversion from you by providing wrappers.
Quoted 3 lines
icovn_open(utf16le,ascii)in encode
icovn_open(ascii,utf16le)in decode
icovn_open(codeset,ascii)in show
That would be correct ONLY if you store the file-name internally as ASCII, which would prevent you from handling file-names containing any character outside the ASCII codeset.
You should use "UTF-8" instead of "ascii", as that allows you to handle file-names containing any valid characters.
This would make the conversion in show() trivial when codeset="UTF-8", as there iconv() would not have to do anything there.
Philipp
Quoted 119 lines · 2015年3月11日 16:55于 "Philipp Hahn" <[email protected]>写道:
Hello,
Quoted 1 line · Xiaodong Gong
Quoted 1 line
Hope that clarified things.
...
Quoted 5 lines
first,your patch is very clear,a good sample.
store ascii code in kernel that I said before is a mistake,I mean the glibc need the input of arguments of fuction such as fopen(path)is ascii code
No:
ASCII = *7* bit, see "man 7 ascii".Kernel = *8* bit, that is the kernel doesn't care if you use
ISO-8859-1, UTF-8, BIG5, GB2312, or any other encoding you can get by running "iconv --list".
For the kernel only '\0'=0x00 and '/'=0x2f are special, all other characters the kernel doesn't care for and passes them in and out unmodified.Most character sets have the ASCII alphabet in their character range 0x00-0x7f, which solved the '\0' and '/' issue nicely.
So again:
- If you useopendir()andreaddir(), the kernel returns to you a 8 bit byte sequence.
- To convert that into a character sequence, you must know which encoding was used when the file was created. This information is not stored explicitly in the file-system, file-name, or anywhere else.- The only hint you get is
LC_CTYPE, which is set by the user to tell you which encoding should be used to convert a byte-stream into a character-string.- If I create a new file on the Linux text console, where I did a "
unicode_start", I get an UTF-8 byte sequence from the input layer, which is passed unmodified through the getty and by shell to thecreate()call. You don't need to know the encoding, you just pass the data in and out unmodified.- When typing "ls" the kernel again return that byte sequence, which gets passed through the shell to the Linux frame buffer, which translates that UTF-8 sequence to a character and picks the right glyph for being displayed on the screen.
- If I don't switch the Linux console to Unicode mode, I get a different byte sequence from the input layer. That different byte sequence would be stored on the disk when creating a file. (This translation is installed by running the "loadkeys" command.)
- If I do the same in X11, the translation from key-codes to characters and back is done by the terminal (or application). See "man 1 xterm" for "-lc" and "-u8".
- BUT when you want to create a specific character encoding, you MUST know from which encoding you start. Assuming ASCII or UTF-8 is wrong, you MUST check LC_ALL/LC_CTYPE/LANG by querying
nl_langinfo(CODESET)).So if I would give you a disk containing a file with the name "\xa1", depending on the locale you would see a different glyph:
$ for ((c=1;c<=15;c++));do printf '\xa1'|recode ISO-8859-$c..dump-with-names 2>/dev/null|tail -n 1;done 00A1 !I inverted exclamation mark 0104 A; latin capital letter a with ogonek 0126 H/ latin capital letter h with stroke 0104 A; latin capital letter a with ogonek 0401 IO cyrillic capital letter io 201B 9' single high-reversed-9 quotation mark 00A1 !I inverted exclamation mark 0104 A; latin capital letter a with ogonek 00A1 !I inverted exclamation mark 00A1 !I inverted exclamation mark 1E02 B. latin capital letter b with dot above 00A1 !I inverted exclamation markIn an UTF-8 environment you would get an error, as "\xa1" is not a valid UTF-8 byte sequence.
Read "man 7 unicode", especially the section "Unicode Under Linux" or "man 7 charsets".
Quoted 1 line
I think:
Any program basically has two options:
1. The program does not care about different character sets ans just passes in file-names and data in and out as byte streams. That is perfectly okay and most UNIX shell commands work just fine that way.
2. The program is encoding aware, as for example it works on characters instead of bytes (like "wc --bytes" vs. "wc --chars") or does need to perform a conversion between encodings. Then the sanest thing is to
- query the encoding of the environment once (or per network connection),
- convert any input data from that encoding into a (fixed) internal format like wchar/utf-16/utf-32 including file-names, file-content, etc.
- convert the internal data back into the right format on output, which also includes calling APIs likeopen().Otherwise you always have to remember if your char[] buffer contains "some byte stream, which needs to be
decoded()before being used" or "already decoded character string". That is why most libraries and frame-works contain wrappers for the file, input- and output, as they internally use one data type for characters consistently and hide all the explicit conversion from you by providing wrappers.Quoted 3 lines
icovn_open(utf16le,ascii)in encode
icovn_open(ascii,utf16le)in decode
icovn_open(codeset,ascii)in showThat would be correct ONLY if you store the file-name internally as ASCII, which would prevent you from handling file-names containing any character outside the ASCII codeset.
You should use "UTF-8" instead of "ascii", as that allows you to handle file-names containing any valid characters.
This would make the conversion inshow()trivial when codeset="UTF-8", as thereiconv()would not have to do anything there.
Quoted 1 line
Philipp
I get it. The Linux supported i18n a long time ago. I even forget the GB2312 :(
I will change it to what you did in your vhd_util patch.
Last, THANKS A LOT for your time.
Thank you for this patch.
I found a few places where you have 32-bit integer overflows that result in failure. They are marked inline below.
Quoted 231 lines · Xiaodong Gong
block/vpc.c +198
-1@@ -29,17 +29,29 @@#if defined(CONFIG_UUID )#include <uuid/uuid.h> #endif +#include <glib.h> /**************************************************************/ #define HEADER_SIZE 512 +#define DYNAMIC_HEADER_SIZE 1024 +#define PARENT_LOCATOR_NUM 8 +#define TBBATMAP_HEAD_SIZE 28 + +#define MACX_PREFIX_LEN 7/* file:// */ + +#define PLATFORM_MACX 0x4D616358 +#define PLATFORM_W2RU 0x57327275 +#define PLATFORM_W2KU 0x57326B75 + +#define VHD_VERSION (major, minor) (((major) << 16) | ((minor) & 0x0000FFFF))//#define CACHE enum vhd_type {VHD_FIXED =2 ,VHD_DYNAMIC =3 , -VHD_DIFFERENCING =4 , +VHD_DIFF =4 , };// Seconds since Jan 1, 2000 0:00:00 (UTC) @@ -138,6 +150,15 @@ typedef struct BDRVVPCState {Error *migration_blocker; } BDRVVPCState; +typedef struct vhd_tdbatmap_header { +char magic[8 ];/* always "tdbatmap" */ + +uint64_t batmap_offset; +uint32_t batmap_size; +uint32_t batmap_version; +uint32_t checksum; +} QEMU_PACKED VHDTdBatmapHeader; +static uint32_t vpc_checksum (uint8_t * buf,size_t size) {uint32_t res =0 ;@@ -157,6 +178,224 @@ static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename)return 0 ; } +static int vpc_decode_maxc_loc (BlockDriverState *bs,uint32_t data_length) +{ +GIConv cd; +gchar *inbuf, *outbuf, *buf; +gsize inbyteleft, outbyteleft, outbyte; +int ret =0 ; +gsize ret1; +gint ret2; + +if (!bs || !bs->backing_file || !data_length) { +return -1 ; + } + + cd =g_iconv_open ("ASCII" ,"UTF8" ); +if (cd == (GIConv) -1 ) { +return -1 ; + } + + inbuf = bs->backing_file; + outbuf = buf = (gchar *)g_malloc (data_length +1 ); +if (!outbuf) { + ret =-1 ; + goto fail2; + } + inbyteleft = outbyteleft = data_length; + + ret1 =g_iconv (cd, &inbuf, &inbyteleft, &outbuf, &outbyteleft); +if (ret1 == (gsize) -1 || inbyteleft) { + ret =-1 ; + goto fail1; + } + outbyte = data_length - outbyteleft; +if (outbyte >sizeof (bs->backing_file) -1 ) { + ret =-1 ; + goto fail1; + } +if (outbyte <MACX_PREFIX_LEN ) { + ret =-1 ; + goto fail1; + } + buf[outbyte] ='\0' ; + +pstrcpy (bs->backing_file,sizeof (bs->backing_file), buf +MACX_PREFIX_LEN ); + +fail1: +g_free (buf); + buf =NULL ; + +fail2: + ret2 =g_iconv_close (cd); +if (ret2 == (gint) -1 ) { + ret =-1 ; + } + +return ret; +} + +static int vpc_decode_w2u_loc (BlockDriverState *bs,uint32_t data_length) +{ +GIConv cd; +gchar *buf, *inbuf, *outbuf; +gsize inbyteleft, outbyteleft, outbyte; +gchar *ptr; +char len =0 ; +int ret =0 ; +gsize ret1; +gint ret2; + +if (!bs || !bs->backing_file || !data_length) { +return -1 ; + } + + cd =g_iconv_open ("ASCII" ,"UTF-16LE" ); +if (cd == (GIConv) -1 ) { +return -1 ; + } + + inbuf = bs->backing_file; + outbuf = buf = ptr = (char *)g_malloc (data_length +1 ); +if (!buf) { + ret =-1 ; + goto fail2; + } + inbyteleft = outbyteleft = data_length; + + ret1 =g_iconv (cd, &inbuf, &inbyteleft, &outbuf, &outbyteleft); +if (ret1 == (gsize) -1 || inbyteleft) { + ret =-1 ; + goto fail1; + } + outbyte = data_length - outbyteleft; +if (outbyte >sizeof (bs->backing_file) -1 ) { + ret =-1 ; + goto fail1; + } + buf[outbyte] ='\0' ; + +while (ptr != outbuf) { +if (*ptr =='\\' ) { + *ptr ='/' ; + } + ptr++; + } + ptr =strstr (buf,":" ); +if (ptr) { + *ptr ='.' ; + len = ptr - buf; + } + +pstrcpy (bs->backing_file,sizeof (bs->backing_file), buf + len); + +fail1: +g_free (buf); + buf =NULL ; + +fail2: + ret2 =g_iconv_close (cd); +if (ret2 == (gint) -1 ) { +return -1 ; + } + +return ret; +} + +static int vpc_decode_parent_loc (uint32_t platform, +BlockDriverState *bs, +uint32_t data_length) +{ +int ret; + +switch (platform) { +case PLATFORM_MACX : + ret =vpc_decode_maxc_loc (bs, data_length); +if (ret <0 ) { +return ret; + } +break ; + +case PLATFORM_W2RU : +/* fall through! */ +case PLATFORM_W2KU : + ret =vpc_decode_w2u_loc (bs, data_length); +if (ret <0 ) { +return ret; + } +break ; + +default : +return -1 ; + } + +return 0 ; +} + +static int vpc_read_backing_loc (VHDDynDiskHeader *dyndisk_header, +BlockDriverState *bs, +Error **errp) +{ +BDRVVPCState *s = bs->opaque; +uint64_t data_offset; +uint32_t data_length; +uint32_t platform; +bool done = false; +uint64_t parent_locator_offset =0 ; +int i; +int ret =0 ; + +for (i =0 ; i <PARENT_LOCATOR_NUM ; i++) { + platform = +be32_to_cpu (dyndisk_header->parent_locator[i].platform); + data_offset = +be64_to_cpu (dyndisk_header->parent_locator[i].data_offset); + data_length = +be32_to_cpu (dyndisk_header->parent_locator[i].data_length); + +/* Extend the location offset */
block/vpc.c static int vpc_probe(const uint8_t *buf, int buf_size, const char *filename) · line 366
Quoted 10 lines
block/vpc.c +10
-0+if (parent_locator_offset < data_offset) { + parent_locator_offset = data_offset; + } + +if (done) { +continue ; + } + +/* Read location of backing file */ +if (data_offset > s->max_table_entries * s->block_size) {
This multiplication overflows resulting in failure...
Quoted 175 lines
block/vpc.c +110
-16+return -1 ; + } +if (data_length >sizeof (bs->backing_file) -1 ) { +return -1 ; + } + ret =bdrv_pread (bs->file, data_offset, bs->backing_file, + data_length); +if (ret <0 ) { +return ret; + } + + bs->backing_file[data_length] ='\0' ; + +/* Decode the parent location */ + ret =vpc_decode_parent_loc (platform, bs, data_length); +if (ret <0 ) { +return ret; + } + +/* Right parent location is got */ +if (bs->backing_file[0 ] !='\0' ) { + done = true; + } + } + +if (!done) { +return -1 ; + } + +return parent_locator_offset; +} +static int vpc_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {@@ -164,11 +403,14 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags,int i;VHDFooter *footer;VHDDynDiskHeader *dyndisk_header; -uint8_t buf[HEADER_SIZE ]; +uint8_t buf[DYNAMIC_HEADER_SIZE ]; +uint8_t tdbatmap_header_buf[TBBATMAP_HEAD_SIZE ];uint32_t checksum;uint64_t computed_size; -int disk_type =VHD_DYNAMIC ; +uint32_t disk_type;int ret; +VHDTdBatmapHeader *tdbatmap_header; +uint64_t parent_locator_offset =0 ; ret =bdrv_pread (bs->file,0 , s->footer_buf,HEADER_SIZE );if (ret <0 ) {@@ -176,6 +418,8 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags, } footer = (VHDFooter *) s->footer_buf; + disk_type =be32_to_cpu (footer->type); +if (strncmp (footer->creator,"conectix" ,8 )) {int64_t offset =bdrv_getlength (bs->file);if (offset <0 ) {@@ -230,9 +474,9 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } -if (disk_type ==VHD_DYNAMIC ) { +if (disk_type ==VHD_DYNAMIC || disk_type ==VHD_DIFF ) { ret =bdrv_pread (bs->file,be64_to_cpu (footer->data_offset), buf, -HEADER_SIZE ); +DYNAMIC_HEADER_SIZE );if (ret <0 ) { goto fail; }@@ -286,6 +530,47 @@ static int vpc_open(BlockDriverState *bs, QDict *options, int flags, s->free_data_block_offset = (s->bat_offset + (s->max_table_entries *4 ) +511 ) & ~511 ; +/* Read tdbatmap header by offset */ +if (be32_to_cpu (footer->version) >=VHD_VERSION (1 ,2 )) { + ret =bdrv_pread (bs->file, s->free_data_block_offset, + tdbatmap_header_buf,TBBATMAP_HEAD_SIZE ); +if (ret <0 ) { + goto fail; + } + + tdbatmap_header = (VHDTdBatmapHeader *) tdbatmap_header_buf; +if (!strncmp (tdbatmap_header->magic,"tdbatmap" ,8 )) { + s->free_data_block_offset = +be32_to_cpu (tdbatmap_header->batmap_size) *512 + +be64_to_cpu (tdbatmap_header->batmap_offset); + } + } + +if (dyndisk_header->parent_name[0 ] || dyndisk_header->parent_name[1 ]) { +int len; + +/* Read parent location from dyn header table */ + ret = parent_locator_offset =vpc_read_backing_loc (dyndisk_header, + bs, errp); +if (ret <0 ) { + goto fail; + } + +/* Fix me : Set parent format to avoid probing to raw in +* format probe framework */ + len =strlen ("vpc" ); +if (sizeof (bs->backing_format) -1 < len) { + goto fail; + } +pstrcpy (bs->backing_format,sizeof (bs->backing_format),"vpc" ); + } + +if (s->free_data_block_offset < parent_locator_offset + +BDRV_SECTOR_SIZE ) { + s->free_data_block_offset = parent_locator_offset + +BDRV_SECTOR_SIZE ; + } +for (i =0 ; i < s->max_table_entries; i++) {be32_to_cpus (&s->pagetable[i]);if (s->pagetable[i] !=0xFFFFFFFF ) {@@ -340,35 +625,76 @@ static int vpc_reopen_prepare(BDRVReopenState *state, }/* -* Returns the absolute byte offset of the given sector in the image file. -* If the sector is not allocated, -1 is returned instead. +* Returns the absolute byte offset of the given sector in the differencing +* image file. +* +* If error happened, -1 is returned. * -* The parameter write must be 1 if the offset will be used for a write -* operation (the block bitmaps is updated then), 0 otherwise. +* When write all type or read dynamic, if the sector is not allocated, -2 +* is returned instead. If the sector is allocated in current file, the block +* offset is returned. +* +* When read diff. If the sector is not allocated, -2 is returned instead. +* If the sector is allocated in the backing file, -3 is returned. If the +* sector is allocated in current file, the block offset is returned. */ static inline int64_t get_sector_offset (BlockDriverState *bs, -int64_t sector_num,int write) +int64_t sector_num,bool write,bool diff) {BDRVVPCState *s = bs->opaque; -uint64_t offset = sector_num *512 ; -uint64_t bitmap_offset, block_offset; +uint64_t offset = sector_num <<BDRV_SECTOR_BITS ; +uint64_t bitmap_offset;uint32_t pagetable_index, pageentry_index; +int64_t block_offset =LONG_MIN ; +int ret; pagetable_index = offset / s->block_size; - pageentry_index = (offset % s->block_size) /512 ; + pageentry_index = (offset % s->block_size) >>BDRV_SECTOR_BITS ; -if (pagetable_index >= s->max_table_entries || s->pagetable[pagetable_index] ==0xffffffff ) -return -1 ;// not allocated +if (pagetable_index >= s->max_table_entries) { +return -2 ; + } +if (s->pagetable[pagetable_index] ==0xffffffff ) { +if (!write && diff) { +return -3 ;/* parent allocated */ + }else { +return -2 ;/* not allocated */ + } + } - bitmap_offset =512 * (uint64_t ) s->pagetable[pagetable_index]; - block_offset = bitmap_offset + s->bitmap_size + (512 * pageentry_index); + bitmap_offset = (uint64_t ) s->pagetable[pagetable_index]
block/vpc.c static int vpc_reopen_prepare(BDRVReopenState *state, · line 675
Quoted 10 lines
block/vpc.c +9
-0+ << BDRV_SECTOR_BITS; + +if (!diff || write) { + block_offset = bitmap_offset + s->bitmap_size + + (pageentry_index <<BDRV_SECTOR_BITS ); + }else { +uint32_t bitmap_index, bitmapentry_index; +uint8_t bitmap[s->bitmap_size]; +if (bitmap_offset > s->max_table_entries * s->block_size) {
This one too....
Quoted 161 lines
block/vpc.c +86
-24+return -1 ; + } + ret =bdrv_pread (bs->file, bitmap_offset, bitmap, s->bitmap_size); +if (ret <0 ) { +return -1 ; + } + + bitmap_index = pageentry_index /8 ; + bitmapentry_index =7 - pageentry_index %8 ; +if (bitmap[bitmap_index] &0x1 << bitmapentry_index) { + block_offset = bitmap_offset + s->bitmap_size + + (pageentry_index <<BDRV_SECTOR_BITS ); + }else { +return -3 ; + } + }// We must ensure that we don't write to any sectors which are marked as // unused in the bitmap. We get away with setting all bits in the block // bitmap each time we write to a new block. This might cause Virtual PC to // miss sparse read optimization, but it's not a problem in terms of // correctness. -if (write && (s->last_bitmap_offset != bitmap_offset)) { +if (!diff && write && (s->last_bitmap_offset != bitmap_offset)) {uint8_t bitmap[s->bitmap_size]; s->last_bitmap_offset = bitmap_offset;@@ -437,7 +763,8 @@ 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, +bool diff) {BDRVVPCState *s = bs->opaque;int64_t bat_offset;@@ -457,7 +784,11 @@ static int64_t alloc_block(BlockDriverState* bs, int64_t sector_num) s->pagetable[index] = s->free_data_block_offset /512 ;// Initialize the block's bitmap -memset (bitmap,0xff , s->bitmap_size); +if (diff) { +memset (bitmap,0x0 , s->bitmap_size); + }else { +memset (bitmap,0xff , s->bitmap_size); + } ret =bdrv_pwrite_sync (bs->file, s->free_data_block_offset, bitmap, s->bitmap_size);if (ret <0 ) {@@ -477,7 +808,7 @@ static int64_t alloc_block(BlockDriverState* bs, int64_t sector_num)if (ret <0 ) goto fail; -return get_sector_offset (bs, sector_num,0 ); +return get_sector_offset (bs, sector_num, false, diff); fail: s->free_data_block_offset -= (s->block_size + s->bitmap_size);@@ -501,37 +832,71 @@ static int vpc_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sectors) {BDRVVPCState *s = bs->opaque; -int ret; -int64_t offset; -int64_t sectors, sectors_per_block;VHDFooter *footer = (VHDFooter *) s->footer_buf; +int64_t sectors_per_block = s->block_size >>BDRV_SECTOR_BITS ; +int64_t offset, sectors; +int ret; -if (be32_to_cpu (footer->type) ==VHD_FIXED ) { +switch (be32_to_cpu (footer->type)) { +case VHD_FIXED :return bdrv_read (bs->file, sector_num, buf, nb_sectors); - } -while (nb_sectors >0 ) { - offset =get_sector_offset (bs, sector_num,0 ); - sectors_per_block = s->block_size >>BDRV_SECTOR_BITS ; - sectors = sectors_per_block - (sector_num % sectors_per_block); -if (sectors > nb_sectors) { - sectors = nb_sectors; +case VHD_DYNAMIC : +while (nb_sectors >0 ) { + sectors = sectors_per_block - (sector_num % sectors_per_block); +if (sectors > nb_sectors) { + sectors = nb_sectors; + } + + offset =get_sector_offset (bs, sector_num, false, false); +if (offset ==-1 ) { +return -1 ; + }else if (offset ==-2 ) { +memset (buf,0 , sectors *BDRV_SECTOR_SIZE ); + }else { + ret =bdrv_pread (bs->file, offset, buf, + sectors *BDRV_SECTOR_SIZE ); +if (ret != sectors *BDRV_SECTOR_SIZE ) { +return -1 ; + } + } + + nb_sectors -= sectors; + sector_num += sectors; + buf += sectors *BDRV_SECTOR_SIZE ; } +break ; -if (offset ==-1 ) { -memset (buf,0 , sectors *BDRV_SECTOR_SIZE ); - }else { - ret =bdrv_pread (bs->file, offset, buf, - sectors *BDRV_SECTOR_SIZE ); -if (ret != sectors *BDRV_SECTOR_SIZE ) { +case VHD_DIFF : +while (nb_sectors >0 ) { + offset =get_sector_offset (bs, sector_num, false, true); +if (offset ==-1 ) {return -1 ; + }else if (offset ==-2 ) { +memset (buf,0 ,BDRV_SECTOR_SIZE ); + }else if (offset ==-3 ) { + ret =bdrv_pread (bs->backing_hd, sector_num <<BDRV_SECTOR_BITS + , buf,BDRV_SECTOR_SIZE ); +if (ret <0 ) { +return -1 ; + } + }else { + ret =bdrv_pread (bs->file, offset, buf,BDRV_SECTOR_SIZE ); +if (ret !=BDRV_SECTOR_SIZE ) { +return -1 ; + } } + + nb_sectors--; + sector_num++; + buf +=BDRV_SECTOR_SIZE ; } +break ; - nb_sectors -= sectors; - sector_num += sectors; - buf += sectors *BDRV_SECTOR_SIZE ; +default : +return -1 ; } +return 0 ; }@@ -546,41 +911,101 @@ static coroutine_fn int vpc_co_read(BlockDriverState *bs, int64_t sector_num,return ret; } +static inline int64_t write_bitmap (BlockDriverState *bs,int64_t sector_num, +int64_t sectors) +{ +BDRVVPCState *s = bs->opaque; +uint64_t offset = sector_num <<BDRV_SECTOR_BITS ; +uint64_t bitmap_offset; +uint32_t pagetable_index, pageentry_index;
block/vpc.c static coroutine_fn int vpc_co_read(BlockDriverState *bs, int64_t sector_num, · line 930
Quoted 10 lines
block/vpc.c +10
-0+uint8_t bitmap[s->bitmap_size]; +uint32_t bitmap_index, bitmapbit_index; +int i; +int ret; + + pagetable_index = offset / s->block_size; + pageentry_index = (offset % s->block_size) /512 ; + bitmap_offset =512 * (uint64_t ) s->pagetable[pagetable_index]; + +if (bitmap_offset > s->max_table_entries * s->block_size) {
And here...
Quoted 112 lines
block/vpc.c +67
-23+return -1 ; + } + ret =bdrv_pread (bs->file, bitmap_offset, bitmap, s->bitmap_size); +if (ret <0 ) { +return -1 ; + } + +for (i =0 ; i < sectors; i++) { + bitmap_index = pageentry_index /8 ; + bitmapbit_index =7 - pageentry_index %8 ; + bitmap[bitmap_index] |= (0x1 << bitmapbit_index); + pageentry_index++; + } + ret =bdrv_pwrite (bs->file, bitmap_offset, bitmap, s->bitmap_size); +if (ret <0 ) { +return -1 ; + } + +return 0 ; +} +static int vpc_write (BlockDriverState *bs,int64_t sector_num,const uint8_t *buf,int nb_sectors) {BDRVVPCState *s = bs->opaque; -int64_t offset; -int64_t sectors, sectors_per_block; -int ret; -VHDFooter *footer = (VHDFooter *) s->footer_buf; +VHDFooter *footer = (VHDFooter *) s->footer_buf; +int64_t sectors_per_block = s->block_size >>BDRV_SECTOR_BITS ; +int64_t offset, sectors; +bool diff = true; +int ret =0 ; -if (be32_to_cpu (footer->type) ==VHD_FIXED ) { +switch (be32_to_cpu (footer->type)) { +case VHD_FIXED :return bdrv_write (bs->file, sector_num, buf, nb_sectors); - } -while (nb_sectors >0 ) { - offset =get_sector_offset (bs, sector_num,1 ); - sectors_per_block = s->block_size >>BDRV_SECTOR_BITS ; - sectors = sectors_per_block - (sector_num % sectors_per_block); -if (sectors > nb_sectors) { - sectors = nb_sectors; - } +case VHD_DYNAMIC : + diff = false; +/* fall-through ! */ + +case VHD_DIFF : +while (nb_sectors >0 ) { + sectors = sectors_per_block - (sector_num % sectors_per_block); +if (sectors > nb_sectors) { + sectors = nb_sectors; + } -if (offset ==-1 ) { - offset =alloc_block (bs, sector_num); -if (offset <0 ) + offset =get_sector_offset (bs, sector_num, true, diff); +if (offset ==-1 ) {return -1 ; - } + }else if (offset ==-2 ) { + offset =alloc_block (bs, sector_num, diff); +if (offset <0 ) { +return -1 ; + } + } - ret =bdrv_pwrite (bs->file, offset, buf, sectors *BDRV_SECTOR_SIZE ); -if (ret != sectors *BDRV_SECTOR_SIZE ) { -return -1 ; + ret =bdrv_pwrite (bs->file, offset, buf, + sectors *BDRV_SECTOR_SIZE ); +if (ret != sectors *BDRV_SECTOR_SIZE ) { +return -1 ; + } + +if (diff) { + ret =write_bitmap (bs, sector_num, sectors); +if (ret <0 ) { +return -1 ; + } + } + + nb_sectors -= sectors; + sector_num += sectors; + buf += sectors *BDRV_SECTOR_SIZE ; } - nb_sectors -= sectors; - sector_num += sectors; - buf += sectors *BDRV_SECTOR_SIZE ; +break ; + +default : +return -1 ; }return 0 ;@@ -906,6 +1331,7 @@ static BlockDriver bdrv_vpc = { .bdrv_close = vpc_close, .bdrv_reopen_prepare = vpc_reopen_prepare, .bdrv_create = vpc_create, + .supports_backing = true, .bdrv_read = vpc_co_read, .bdrv_write = vpc_co_write,