diff mbox

[2/3] qcow2: Fix write_zeroes with partially allocated backing file cluster

Message ID 1463503863-19009-3-git-send-email-kwolf@redhat.com
State New
Headers show

Commit Message

Kevin Wolf May 17, 2016, 4:51 p.m. UTC
In order to correctly check whether a given cluster is read as zero, we
don't only need to check whether bdrv_get_block_status_above() sets
BDRV_BLOCK_ZERO, but also if all sectors for the whole cluster have the
same status.

Signed-off-by: Kevin Wolf <kwolf@redhat.com>
---
 block/qcow2.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Eric Blake May 17, 2016, 5:22 p.m. UTC | #1
On 05/17/2016 10:51 AM, Kevin Wolf wrote:
> In order to correctly check whether a given cluster is read as zero, we
> don't only need to check whether bdrv_get_block_status_above() sets
> BDRV_BLOCK_ZERO, but also if all sectors for the whole cluster have the
> same status.
> 
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>  block/qcow2.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 

Reviewed-by: Eric Blake <eblake@redhat.com>
Denis V. Lunev May 17, 2016, 7:20 p.m. UTC | #2
On 05/17/2016 07:51 PM, Kevin Wolf wrote:
> In order to correctly check whether a given cluster is read as zero, we
> don't only need to check whether bdrv_get_block_status_above() sets
> BDRV_BLOCK_ZERO, but also if all sectors for the whole cluster have the
> same status.
>
> Signed-off-by: Kevin Wolf <kwolf@redhat.com>
> ---
>   block/qcow2.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
>
> diff --git a/block/qcow2.c b/block/qcow2.c
> index a6012dc..c197ff3 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -2412,7 +2412,7 @@ static bool is_zero_cluster(BlockDriverState *bs, int64_t start)
>       BlockDriverState *file;
>       int64_t res = bdrv_get_block_status_above(bs, NULL, start,
>                                                 s->cluster_sectors, &nr, &file);
> -    return res >= 0 && (res & BDRV_BLOCK_ZERO);
> +    return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == s->cluster_sectors;
>   }
>   
>   static bool is_zero_cluster_top_locked(BlockDriverState *bs, int64_t start)
> @@ -2423,6 +2423,7 @@ static bool is_zero_cluster_top_locked(BlockDriverState *bs, int64_t start)
>       int ret;
>   
>       ret = qcow2_get_cluster_offset(bs, start << BDRV_SECTOR_BITS, &nr, &off);
> +    assert(nr == s->cluster_sectors);
>       return ret == QCOW2_CLUSTER_UNALLOCATED || ret == QCOW2_CLUSTER_ZERO;
>   }
>   
Reviewed-by: Denis V. Lunev <den@openvz.org>

I have had this kludge in the revision one and I have lost them in 
revision 3 :(
diff mbox

Patch

diff --git a/block/qcow2.c b/block/qcow2.c
index a6012dc..c197ff3 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2412,7 +2412,7 @@  static bool is_zero_cluster(BlockDriverState *bs, int64_t start)
     BlockDriverState *file;
     int64_t res = bdrv_get_block_status_above(bs, NULL, start,
                                               s->cluster_sectors, &nr, &file);
-    return res >= 0 && (res & BDRV_BLOCK_ZERO);
+    return res >= 0 && (res & BDRV_BLOCK_ZERO) && nr == s->cluster_sectors;
 }
 
 static bool is_zero_cluster_top_locked(BlockDriverState *bs, int64_t start)
@@ -2423,6 +2423,7 @@  static bool is_zero_cluster_top_locked(BlockDriverState *bs, int64_t start)
     int ret;
 
     ret = qcow2_get_cluster_offset(bs, start << BDRV_SECTOR_BITS, &nr, &off);
+    assert(nr == s->cluster_sectors);
     return ret == QCOW2_CLUSTER_UNALLOCATED || ret == QCOW2_CLUSTER_ZERO;
 }