diff mbox

[5/5] qcow2: merge is_zero_cluster helpers into qcow2_co_write_zeroes

Message ID 1463476543-3087-6-git-send-email-den@openvz.org (mailing list archive)
State New, archived
Headers show

Commit Message

Denis V. Lunev May 17, 2016, 9:15 a.m. UTC
They are used once only. This makes code more compact.

The patch also improves comments in the code.

Signed-off-by: Denis V. Lunev <den@openvz.org>
CC: Kevin Wolf <kwolf@redhat.com>
---
 block/qcow2.c | 39 +++++++++++++++------------------------
 1 file changed, 15 insertions(+), 24 deletions(-)

Comments

Eric Blake May 24, 2016, 6:14 p.m. UTC | #1
On 05/17/2016 03:15 AM, Denis V. Lunev wrote:
> They are used once only. This makes code more compact.
> 
> The patch also improves comments in the code.
> 
> Signed-off-by: Denis V. Lunev <den@openvz.org>
> CC: Kevin Wolf <kwolf@redhat.com>
> ---
>  block/qcow2.c | 39 +++++++++++++++------------------------
>  1 file changed, 15 insertions(+), 24 deletions(-)
> 

> @@ -2439,20 +2418,32 @@ static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
>                                         nb_sectors);
>  
>      if (head != 0 || tail != 0) {
> -        int64_t cl_start = sector_num - head;
> +        BlockDriverState *file;
> +        uint64_t off;
> +        int nr;
> +
> +        int64_t cl_start = sector_num - head, res;
>  
>          assert(cl_start + s->cluster_sectors >= sector_num + nb_sectors);
>  
>          sector_num = cl_start;
>          nb_sectors = s->cluster_sectors;
>  
> -        if (!is_zero_cluster(bs, sector_num)) {
> +        /* check that the cluster is zeroed taking into account entire
> +           backing chain */
> +        nr = s->cluster_sectors;
> +        res = bdrv_get_block_status_above(bs, NULL, cl_start,
> +                                          s->cluster_sectors, &nr, &file);
> +        if (res < 0 || !(res & BDRV_BLOCK_ZERO)) {
>              return -ENOTSUP;
>          }

This is somewhat pessimistic in certain cases.  Suppose I have the
following cluster (borrowing from Kevin's nice notation in 154):

4k: -- -- XX --

and issue a 'write -z 2k 1k'

As written, the code will see that bdrv_get_block_status_above() does
NOT have all ZERO for the cluster, so it will return -ENOTSUP; we will
then call into the fallbacks and write explicit zeroes, so that the top
file now has an allocated cluster full of zero.  But if we were a bit
smarter, we would only check the allocation status of the backing
sectors that do not overlap with our write_zeroes request, because the
remaining sectors are about to be overwritten; in this case, we can
specifically optimize to write a zero cluster without allocation.

You'll also need to rebase this series on top of my pending work to
rewrite bdrv_co_write_zeroes to instead be bdrv_co_pwrite_zeroes with a
byte interface (patches to be posted later today), if mine gets reviewed
first.
diff mbox

Patch

diff --git a/block/qcow2.c b/block/qcow2.c
index 05beb64..feaf146 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2405,27 +2405,6 @@  finish:
 }
 
 
-static bool is_zero_cluster(BlockDriverState *bs, int64_t start)
-{
-    BDRVQcow2State *s = bs->opaque;
-    int nr;
-    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);
-}
-
-static bool is_zero_cluster_top_locked(BlockDriverState *bs, int64_t start)
-{
-    BDRVQcow2State *s = bs->opaque;
-    int nr = s->cluster_sectors;
-    uint64_t off;
-    int ret;
-
-    ret = qcow2_get_cluster_offset(bs, start << BDRV_SECTOR_BITS, &nr, &off);
-    return ret == QCOW2_CLUSTER_UNALLOCATED || ret == QCOW2_CLUSTER_ZERO;
-}
-
 static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
     int64_t sector_num, int nb_sectors, BdrvRequestFlags flags)
 {
@@ -2439,20 +2418,32 @@  static coroutine_fn int qcow2_co_write_zeroes(BlockDriverState *bs,
                                        nb_sectors);
 
     if (head != 0 || tail != 0) {
-        int64_t cl_start = sector_num - head;
+        BlockDriverState *file;
+        uint64_t off;
+        int nr;
+
+        int64_t cl_start = sector_num - head, res;
 
         assert(cl_start + s->cluster_sectors >= sector_num + nb_sectors);
 
         sector_num = cl_start;
         nb_sectors = s->cluster_sectors;
 
-        if (!is_zero_cluster(bs, sector_num)) {
+        /* check that the cluster is zeroed taking into account entire
+           backing chain */
+        nr = s->cluster_sectors;
+        res = bdrv_get_block_status_above(bs, NULL, cl_start,
+                                          s->cluster_sectors, &nr, &file);
+        if (res < 0 || !(res & BDRV_BLOCK_ZERO)) {
             return -ENOTSUP;
         }
 
         qemu_co_mutex_lock(&s->lock);
         /* We can have new write after previous check */
-        if (!is_zero_cluster_top_locked(bs, sector_num)) {
+        nr = s->cluster_sectors;
+        ret = qcow2_get_cluster_offset(bs, cl_start << BDRV_SECTOR_BITS,
+                                       &nr, &off);
+        if (ret != QCOW2_CLUSTER_UNALLOCATED && ret != QCOW2_CLUSTER_ZERO) {
             qemu_co_mutex_unlock(&s->lock);
             return -ENOTSUP;
         }