vmdk: align end of file to a sector boundary
PATCH 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.
--- block/vmdk.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)
block/vmdk.c +18 -0
@@ -1698,6 +1698,24 @@ static int coroutine_fn vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov) { +if (bytes ==0 ) { +/* align end of file 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; + } + 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.17.0
Ping!
[email protected] 於 2018-08-28 11:18 寫道:
Quoted 1 line
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. Signed-off-by: yuchenlin <[email protected]> ---
block/vmdk.c| 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --gita/block/vmdk.cb/block/vmdk.cindexa9d0084e36..a8ae7c65d2100644 ---a/block/vmdk.c+++b/block/vmdk.c@@ -1698,6 +1698,24 @@ static intcoroutine_fnvmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov){ + if (bytes == 0) { + /* align end of file to a sector boundary. */ + BDRVVmdkState *s =bs->opaque; + int i, ret; +int64_tlength; + + for (i = 0; i <s->num_extents; i++) { + length =bdrv_getlength(s->extents[i].file->bs); + if (length < 0) { + return length; + } + ret =bdrv_truncate(s->extents[i].file, length, PREALLOC_MODE_OFF, NULL); + if (ret < 0) { + return ret; + } + } + return 0; + } returnvmdk_co_pwritev(bs, offset, bytes, qiov, 0); } -- 2.17.0
Quoted 5 lines · On Tue, 08/28 11:17, [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.
I don't understand. Doesn't it mean that if you force the alignment by truncating out the extra bytes, some data is lost?
Quoted 11 lines
Signed-off-by yuchenlin <[email protected]>--- block/vmdk.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)block/vmdk.c +0
-0
block/vmdk.c static int coroutine_fn · line 1701
Quoted 4 lines
block/vmdk.c +1
-0@@ -1698,6 +1698,24 @@ static int coroutine_fn vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov) { +if (bytes ==0 ) {
Where is this bytes == 0 condition from?
Quoted 23 lines
block/vmdk.c +17
-0+/* align end of file 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; + } + 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.17.0
Fam
Fam Zheng <[email protected]> 於 2018-09-12 17:34 寫道:
Quoted 1 line
On Tue, 08/28 11:17, [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. I don't understand. Doesn't it mean that if you force the alignment by truncating out the extra bytes, some data is lost?
You can take qcow2_co_pwritev_compressed in block/qcow2.c as an example.
The bdrv_getlength will return the length in bytes which is always a multiple of BDRV_SECTOR_SIZE. After truncates this size, the vmdk is extended to align sector size.
Quoted 1 line
From the end of convert_do_copy in qemu-img.c.
if (s->compressed && !s->ret) {
/* signal EOF to align */
ret = blk_pwrite_compressed(s->target, 0, NULL, 0); if (ret < 0) {
return ret;
}
}
It signals the EOF to the block driver.
Quoted 1 line
Quoted 1 line
+ /* align end of file to a sector boundary. */ > + BDRVVmdkState *s =
bs->opaque; > + int i, ret; > +int64_tlength; > + > + for (i = 0; i <s->num_extents; i++) { > + length =bdrv_getlength(s->extents[i].file->bs); > + if (length < 0) { > + return length; > + } > + ret =bdrv_truncate(s->extents[i].file, length, PREALLOC_MODE_OFF, NULL); > + if (ret < 0) { > + return ret; > + } > + } > + return 0; > + } > returnvmdk_co_pwritev(bs, offset, bytes, qiov, 0); > } > > -- > 2.17.0 > Fam
yuchenlin
Quoted 11 lines · yuchenlin
Fam Zheng <[email protected]> 於 2018-09-12 17:34 寫道:
Quoted 1 line
On Tue, 08/28 11:17, [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. I don't understand. Doesn't it mean that if you force the alignment by truncating out the extra bytes, some data is lost?
You can take
qcow2_co_pwritev_compressedinblock/qcow2.cas an example.
Thebdrv_getlengthwill return the length in bytes which is always a multiple ofBDRV_SECTOR_SIZE. After truncates this size, the vmdk is extended to align sector size.Quoted 1 line
From the end of
convert_do_copyinqemu-img.c.
Quoted 7 lines
if (
s->compressed&& !s->ret) {
/* signal EOF to align */
ret =blk_pwrite_compressed(s->target, 0, NULL, 0); if (ret < 0) {
return ret;
}
}
I see. Please mention this in the comment of the if check.
Fam
Quoted 7 lines
It signals the EOF to the block driver.
Quoted 1 line
Quoted 1 line
+ /* align end of file to a sector boundary. */ > + BDRVVmdkState *s =
bs->opaque; > + int i, ret; > +int64_tlength; > + > + for (i = 0; i <s->num_extents; i++) { > + length =bdrv_getlength(s->extents[i].file->bs); > + if (length < 0) { > + return length; > + } > + ret =bdrv_truncate(s->extents[i].file, length, PREALLOC_MODE_OFF, NULL); > + if (ret < 0) { > + return ret; > + } > + } > + return 0; > + } > returnvmdk_co_pwritev(bs, offset, bytes, qiov, 0); > } > > -- > 2.17.0 > Famyuchenlin
Quoted 20 lines · On Tue, 08/28 11:17, [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.
Signed-off-by yuchenlin <[email protected]>--- block/vmdk.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)block/vmdk.c +1
-0@@ -1698,6 +1698,24 @@ static int coroutine_fn vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov) { +if (bytes ==0 ) {
block/vmdk.c static int coroutine_fn · line 1711
Quoted 10 lines
block/vmdk.c +10
-0+/* align end of file 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; + }
Could you add "length = QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE);" to show the intention more clearly?
Fam
Quoted 13 lines
block/vmdk.c +7
-0+ 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.17.0
Quoted 35 lines · Fam Zheng
Quoted 20 lines · On Tue, 08/28 11:17, [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.
Signed-off-by yuchenlin <[email protected]>--- block/vmdk.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+)block/vmdk.c +1
-0@@ -1698,6 +1698,24 @@ static int coroutine_fn vmdk_co_pwritev_compressed(BlockDriverState *bs, uint64_t offset, uint64_t bytes, QEMUIOVector *qiov) { +if (bytes ==0 ) {block/vmdk.c static int coroutine_fn · line 1711
Quoted 10 lines
block/vmdk.c +10
-0+/* align end of file 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; + }Could you add "length =
QEMU_ALIGN_UP(length, BDRV_SECTOR_SIZE);" to show the intention more clearly?
Quoted 2 lines
Fam
Thank you for your effort, I will do it.
yuchenlin
Quoted 14 lines
Quoted 14 lines
block/vmdk.c +7
-1+ 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.17.0