vmdk: align end of file to a sector boundary
PATCH v2 1/1 vmdk: align end of file to a sector boundary
From: yuchenlin <[email protected]>
There is a rare case which the size of last compressed cluster is larger than the cluster size, which will cause the file is not aligned at the sector boundary.
---
v1 -> v2:
* Add more detail comment.
* Add QEMU_ALIGN_UP to show the intention more clearly.
* Add newline in the end of bdrv_truncate.
thanks
block/vmdk.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
block/vmdk.c +21 -0
@@ -1698,6 +1698,27 @@ static int coroutine_fn vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov) { +if (bytes ==0 ) { +/* The caller will write bytes 0 to signal EOF. +* When receive it, we align EOF to a sector boundary. */ +BDRVVmdkState *s = bs->opaque; +int i, ret; +int64_t length; + +for (i =0 ; i < s->num_extents; i++) { + length =bdrv_getlength (s->extents[i].file->bs); +if (length <0 ) { +return length; + } + length =QEMU_ALIGN_UP (length,BDRV_SECTOR_SIZE ); + ret =bdrv_truncate (s->extents[i].file, length, +PREALLOC_MODE_OFF ,NULL ); +if (ret <0 ) { +return ret; + } + } +return 0 ; + }return vmdk_co_pwritev (bs, offset, bytes, qiov,0 ); }
Signature · 1 line
2.18.0
Quoted 5 lines · On Thu, 09/13 10:31, [email protected] wrote:
From: yuchenlin <[email protected]>
There is a rare case which the size of last compressed cluster is larger than the cluster size, which will cause the file is not aligned at the sector boundary.
The code looks good to me. Can you also explain why it is important to align file size to sector boundary in the comment?
Fam
Quoted 48 lines
Signed-off-by yuchenlin <[email protected]>---
v1 -> v2:
* Add more detail comment.
* AddQEMU_ALIGN_UPto show the intention more clearly.
* Add newline in the end ofbdrv_truncate.thanks
block/vmdk.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)block/vmdk.c +21
-0@@ -1698,6 +1698,27 @@ static int coroutine_fn vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov) { +if (bytes ==0 ) { +/* The caller will write bytes 0 to signal EOF. +* When receive it, we align EOF to a sector boundary. */ +BDRVVmdkState *s = bs->opaque; +int i, ret; +int64_t length; + +for (i =0 ; i < s->num_extents; i++) { + length =bdrv_getlength (s->extents[i].file->bs); +if (length <0 ) { +return length; + } + length =QEMU_ALIGN_UP (length,BDRV_SECTOR_SIZE ); + ret =bdrv_truncate (s->extents[i].file, length, +PREALLOC_MODE_OFF ,NULL ); +if (ret <0 ) { +return ret; + } + } +return 0 ; + }return vmdk_co_pwritev (bs, offset, bytes, qiov,0 ); }Signature · 1 line
2.18.0
Quoted 12 lines · Fam Zheng
Quoted 5 lines · On Thu, 09/13 10:31, [email protected] wrote:
From: yuchenlin <[email protected]>
There is a rare case which the size of last compressed cluster is larger than the cluster size, which will cause the file is not aligned at the sector boundary.
The code looks good to me. Can you also explain why it is important to align file size to sector boundary in the comment?
Fam
In my opinion, there are three reasons to do it. First, if vmdk doesn't align at the sector boundary, there may be many undefined behaviors, such as in vbox it will show VMDK: Compressed image is corrupted '88-disk1.vmdk' (VERR_ZIP_CORRUPTED) when we try to import an ova with unaligned vmdk. Second, all the cluster_sector is aligned to sector, the last one should be like this, too. Third, it ease reading with sector based I/Os.
What do you think?
yuchenlin
Quoted 48 lines
Quoted 48 lines
Signed-off-by yuchenlin <[email protected]>---
v1 -> v2:
* Add more detail comment.
* AddQEMU_ALIGN_UPto show the intention more clearly.
* Add newline in the end ofbdrv_truncate.thanks
block/vmdk.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)block/vmdk.c +21
-1@@ -1698,6 +1698,27 @@ static int coroutine_fn vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov) { +if (bytes ==0 ) { +/* The caller will write bytes 0 to signal EOF. +* When receive it, we align EOF to a sector boundary. */ +BDRVVmdkState *s = bs->opaque; +int i, ret; +int64_t length; + +for (i =0 ; i < s->num_extents; i++) { + length =bdrv_getlength (s->extents[i].file->bs); +if (length <0 ) { +return length; + } + length =QEMU_ALIGN_UP (length,BDRV_SECTOR_SIZE ); + ret =bdrv_truncate (s->extents[i].file, length, +PREALLOC_MODE_OFF ,NULL ); +if (ret <0 ) { +return ret; + } + } +return 0 ; + }return vmdk_co_pwritev (bs, offset, bytes, qiov,0 ); } --2.18.0
Quoted 23 lines · yuchenlin
Quoted 7 lines · Fam Zheng
Quoted 5 lines · On Thu, 09/13 10:31, [email protected] wrote:
From: yuchenlin <[email protected]>
There is a rare case which the size of last compressed cluster is larger than the cluster size, which will cause the file is not aligned at the sector boundary.
Quoted 6 lines
The code looks good to me. Can you also explain why it is important to align
file size to sector boundary in the comment?Fam
In my opinion, there are three reasons to do it. First, if vmdk doesn't align at the sector boundary, there may be many undefined behaviors, such as in vbox it will show VMDK: Compressed image is corrupted '88-disk1.vmdk' (
VERR_ZIP_CORRUPTED) when we try to import an ova with unaligned vmdk. Second, all thecluster_sectoris aligned
to sector, the last one should be like this, too. Third, it ease reading with sector based I/Os.
Quoted 1 line
What do you think?
Yes, it would be nice if you could add this information to the commit message or the code comment.
Fam
Quoted 52 lines
yuchenlin
Quoted 48 lines
Quoted 48 lines
Signed-off-by yuchenlin <[email protected]>---
v1 -> v2:
* Add more detail comment.
* AddQEMU_ALIGN_UPto show the intention more clearly.
* Add newline in the end ofbdrv_truncate.thanks
block/vmdk.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)block/vmdk.c +21
-1@@ -1698,6 +1698,27 @@ static int coroutine_fn vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov) { +if (bytes ==0 ) { +/* The caller will write bytes 0 to signal EOF. +* When receive it, we align EOF to a sector boundary. */ +BDRVVmdkState *s = bs->opaque; +int i, ret; +int64_t length; + +for (i =0 ; i < s->num_extents; i++) { + length =bdrv_getlength (s->extents[i].file->bs); +if (length <0 ) { +return length; + } + length =QEMU_ALIGN_UP (length,BDRV_SECTOR_SIZE ); + ret =bdrv_truncate (s->extents[i].file, length, +PREALLOC_MODE_OFF ,NULL ); +if (ret <0 ) { +return ret; + } + } +return 0 ; + }return vmdk_co_pwritev (bs, offset, bytes, qiov,0 ); } --2.18.0