diff mbox series

[v4,14/30] qcow2: Add cluster type parameter to qcow2_get_host_offset()

Message ID 348c0b057802b5b35eefe3dc7cc8ef2964024ed5.1584468723.git.berto@igalia.com (mailing list archive)
State New, archived
Headers show
Series Add subcluster allocation to qcow2 | expand

Commit Message

Alberto Garcia March 17, 2020, 6:16 p.m. UTC
This function returns an integer that can be either an error code or a
cluster type (a value from the QCow2ClusterType enum).

We are going to start using subcluster types instead of cluster types
in some functions so it's better to use the exact data types instead
of integers for clarity and in order to detect errors more easily.

This patch makes qcow2_get_host_offset() return 0 on success and
puts the returned cluster type in a separate parameter. There are no
semantic changes.

Signed-off-by: Alberto Garcia <berto@igalia.com>
---
 block/qcow2.h         |  3 ++-
 block/qcow2-cluster.c | 11 +++++++----
 block/qcow2.c         | 37 ++++++++++++++++++++++---------------
 3 files changed, 31 insertions(+), 20 deletions(-)

Comments

Max Reitz April 8, 2020, 12:15 p.m. UTC | #1
On 17.03.20 19:16, Alberto Garcia wrote:
> This function returns an integer that can be either an error code or a
> cluster type (a value from the QCow2ClusterType enum).
> 
> We are going to start using subcluster types instead of cluster types
> in some functions so it's better to use the exact data types instead
> of integers for clarity and in order to detect errors more easily.
> 
> This patch makes qcow2_get_host_offset() return 0 on success and
> puts the returned cluster type in a separate parameter. There are no
> semantic changes.
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>  block/qcow2.h         |  3 ++-
>  block/qcow2-cluster.c | 11 +++++++----
>  block/qcow2.c         | 37 ++++++++++++++++++++++---------------
>  3 files changed, 31 insertions(+), 20 deletions(-)

Reviewed-by: Max Reitz <mreitz@redhat.com>
Vladimir Sementsov-Ogievskiy April 14, 2020, 12:30 p.m. UTC | #2
17.03.2020 21:16, Alberto Garcia wrote:
> This function returns an integer that can be either an error code or a
> cluster type (a value from the QCow2ClusterType enum).
> 
> We are going to start using subcluster types instead of cluster types
> in some functions so it's better to use the exact data types instead
> of integers for clarity and in order to detect errors more easily.
> 
> This patch makes qcow2_get_host_offset() return 0 on success and
> puts the returned cluster type in a separate parameter. There are no
> semantic changes.
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>   block/qcow2.h         |  3 ++-
>   block/qcow2-cluster.c | 11 +++++++----
>   block/qcow2.c         | 37 ++++++++++++++++++++++---------------
>   3 files changed, 31 insertions(+), 20 deletions(-)
> 
> diff --git a/block/qcow2.h b/block/qcow2.h
> index 52865787ee..6b7b286b91 100644
> --- a/block/qcow2.h
> +++ b/block/qcow2.h


[..]

> @@ -3716,6 +3719,7 @@ static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
>       if (head || tail) {
>           uint64_t off;
>           unsigned int nr;
> +        QCow2ClusterType type;
>   
>           assert(head + bytes <= s->cluster_size);
>   
> @@ -3731,10 +3735,11 @@ static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
>           offset = QEMU_ALIGN_DOWN(offset, s->cluster_size);
>           bytes = s->cluster_size;
>           nr = s->cluster_size;
> -        ret = qcow2_get_host_offset(bs, offset, &nr, &off);
> -        if (ret != QCOW2_CLUSTER_UNALLOCATED &&
> -            ret != QCOW2_CLUSTER_ZERO_PLAIN &&
> -            ret != QCOW2_CLUSTER_ZERO_ALLOC) {
> +        ret = qcow2_get_host_offset(bs, offset, &nr, &off, &type);

pre-patch, but probably better to return original errno on qcow2_get_host_offset failure, instead of masking it.

> +        if (ret < 0 ||
> +            (type != QCOW2_CLUSTER_UNALLOCATED &&
> +             type != QCOW2_CLUSTER_ZERO_PLAIN &&
> +             type != QCOW2_CLUSTER_ZERO_ALLOC)) {
>               qemu_co_mutex_unlock(&s->lock);
>               return -ENOTSUP;
>           }
> @@ -3792,16 +3797,18 @@ qcow2_co_copy_range_from(BlockDriverState *bs,
>   
>       while (bytes != 0) {
>           uint64_t copy_offset = 0;
> +        QCow2ClusterType type;
>           /* prepare next request */
>           cur_bytes = MIN(bytes, INT_MAX);
>           cur_write_flags = write_flags;
>   
> -        ret = qcow2_get_host_offset(bs, src_offset, &cur_bytes, &copy_offset);
> +        ret = qcow2_get_host_offset(bs, src_offset, &cur_bytes,
> +                                    &copy_offset, &type);
>           if (ret < 0) {
>               goto out;
>           }
>   
> -        switch (ret) {
> +        switch (type) {
>           case QCOW2_CLUSTER_UNALLOCATED:
>               if (bs->backing && bs->backing->bs) {
>                   int64_t backing_length = bdrv_getlength(bs->backing->bs);
> 
Hmm, just noted that in case of bdrv_co_copy_range_from failure below, we do mutex lock/unlock for nothing.

I think, we want mutex lock/unlock just around qcow2_co_preadv_part() call, like in  qcow2_co_preadv_part above().

I can send a refactoring patch..

Anyway, patch itself is OK:
Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Alberto Garcia April 14, 2020, 12:38 p.m. UTC | #3
On Tue 14 Apr 2020 02:30:30 PM CEST, Vladimir Sementsov-Ogievskiy wrote:
>> -        ret = qcow2_get_host_offset(bs, offset, &nr, &off);
>> -        if (ret != QCOW2_CLUSTER_UNALLOCATED &&
>> -            ret != QCOW2_CLUSTER_ZERO_PLAIN &&
>> -            ret != QCOW2_CLUSTER_ZERO_ALLOC) {
>> +        ret = qcow2_get_host_offset(bs, offset, &nr, &off, &type);
>
> pre-patch, but probably better to return original errno on
> qcow2_get_host_offset failure, instead of masking it.

Yeah, I think you're right. I can take care of that on a separate patch.

Berto
diff mbox series

Patch

diff --git a/block/qcow2.h b/block/qcow2.h
index 52865787ee..6b7b286b91 100644
--- a/block/qcow2.h
+++ b/block/qcow2.h
@@ -859,7 +859,8 @@  int qcow2_encrypt_sectors(BDRVQcow2State *s, int64_t sector_num,
                           uint8_t *buf, int nb_sectors, bool enc, Error **errp);
 
 int qcow2_get_host_offset(BlockDriverState *bs, uint64_t offset,
-                          unsigned int *bytes, uint64_t *host_offset);
+                          unsigned int *bytes, uint64_t *host_offset,
+                          QCow2ClusterType *cluster_type);
 int qcow2_alloc_cluster_offset(BlockDriverState *bs, uint64_t offset,
                                unsigned int *bytes, uint64_t *host_offset,
                                QCowL2Meta **m);
diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 41a23c5305..acfcf8ea4c 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -514,13 +514,14 @@  static int coroutine_fn do_perform_cow_write(BlockDriverState *bs,
  *
  * On exit, *bytes is the number of bytes starting at offset that have the same
  * cluster type and (if applicable) are stored contiguously in the image file.
+ * The cluster type is stored in *cluster_type.
  * Compressed clusters are always returned one by one.
  *
- * Returns the cluster type (QCOW2_CLUSTER_*) on success, -errno in error
- * cases.
+ * Returns 0 on success, -errno in error cases.
  */
 int qcow2_get_host_offset(BlockDriverState *bs, uint64_t offset,
-                          unsigned int *bytes, uint64_t *host_offset)
+                          unsigned int *bytes, uint64_t *host_offset,
+                          QCow2ClusterType *cluster_type)
 {
     BDRVQcow2State *s = bs->opaque;
     unsigned int l2_index;
@@ -663,7 +664,9 @@  out:
     assert(bytes_available - offset_in_cluster <= UINT_MAX);
     *bytes = bytes_available - offset_in_cluster;
 
-    return type;
+    *cluster_type = type;
+
+    return 0;
 
 fail:
     qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
diff --git a/block/qcow2.c b/block/qcow2.c
index d3b8581aed..48e188152c 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -1971,6 +1971,7 @@  static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
     BDRVQcow2State *s = bs->opaque;
     uint64_t host_offset;
     unsigned int bytes;
+    QCow2ClusterType type;
     int ret, status = 0;
 
     qemu_co_mutex_lock(&s->lock);
@@ -1982,7 +1983,7 @@  static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
     }
 
     bytes = MIN(INT_MAX, count);
-    ret = qcow2_get_host_offset(bs, offset, &bytes, &host_offset);
+    ret = qcow2_get_host_offset(bs, offset, &bytes, &host_offset, &type);
     qemu_co_mutex_unlock(&s->lock);
     if (ret < 0) {
         return ret;
@@ -1990,15 +1991,15 @@  static int coroutine_fn qcow2_co_block_status(BlockDriverState *bs,
 
     *pnum = bytes;
 
-    if ((ret == QCOW2_CLUSTER_NORMAL || ret == QCOW2_CLUSTER_ZERO_ALLOC) &&
+    if ((type == QCOW2_CLUSTER_NORMAL || type == QCOW2_CLUSTER_ZERO_ALLOC) &&
         !s->crypto) {
         *map = host_offset;
         *file = s->data_file->bs;
         status |= BDRV_BLOCK_OFFSET_VALID;
     }
-    if (ret == QCOW2_CLUSTER_ZERO_PLAIN || ret == QCOW2_CLUSTER_ZERO_ALLOC) {
+    if (type == QCOW2_CLUSTER_ZERO_PLAIN || type == QCOW2_CLUSTER_ZERO_ALLOC) {
         status |= BDRV_BLOCK_ZERO;
-    } else if (ret != QCOW2_CLUSTER_UNALLOCATED) {
+    } else if (type != QCOW2_CLUSTER_UNALLOCATED) {
         status |= BDRV_BLOCK_DATA;
     }
     if (s->metadata_preallocation && (status & BDRV_BLOCK_DATA) &&
@@ -2207,6 +2208,7 @@  static coroutine_fn int qcow2_co_preadv_part(BlockDriverState *bs,
     int ret = 0;
     unsigned int cur_bytes; /* number of bytes in current iteration */
     uint64_t host_offset = 0;
+    QCow2ClusterType type;
     AioTaskPool *aio = NULL;
 
     while (bytes != 0 && aio_task_pool_status(aio) == 0) {
@@ -2218,22 +2220,23 @@  static coroutine_fn int qcow2_co_preadv_part(BlockDriverState *bs,
         }
 
         qemu_co_mutex_lock(&s->lock);
-        ret = qcow2_get_host_offset(bs, offset, &cur_bytes, &host_offset);
+        ret = qcow2_get_host_offset(bs, offset, &cur_bytes,
+                                    &host_offset, &type);
         qemu_co_mutex_unlock(&s->lock);
         if (ret < 0) {
             goto out;
         }
 
-        if (ret == QCOW2_CLUSTER_ZERO_PLAIN ||
-            ret == QCOW2_CLUSTER_ZERO_ALLOC ||
-            (ret == QCOW2_CLUSTER_UNALLOCATED && !bs->backing))
+        if (type == QCOW2_CLUSTER_ZERO_PLAIN ||
+            type == QCOW2_CLUSTER_ZERO_ALLOC ||
+            (type == QCOW2_CLUSTER_UNALLOCATED && !bs->backing))
         {
             qemu_iovec_memset(qiov, qiov_offset, 0, cur_bytes);
         } else {
             if (!aio && cur_bytes != bytes) {
                 aio = aio_task_pool_new(QCOW2_MAX_WORKERS);
             }
-            ret = qcow2_add_task(bs, aio, qcow2_co_preadv_task_entry, ret,
+            ret = qcow2_add_task(bs, aio, qcow2_co_preadv_task_entry, type,
                                  host_offset, offset, cur_bytes,
                                  qiov, qiov_offset, NULL);
             if (ret < 0) {
@@ -3716,6 +3719,7 @@  static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
     if (head || tail) {
         uint64_t off;
         unsigned int nr;
+        QCow2ClusterType type;
 
         assert(head + bytes <= s->cluster_size);
 
@@ -3731,10 +3735,11 @@  static coroutine_fn int qcow2_co_pwrite_zeroes(BlockDriverState *bs,
         offset = QEMU_ALIGN_DOWN(offset, s->cluster_size);
         bytes = s->cluster_size;
         nr = s->cluster_size;
-        ret = qcow2_get_host_offset(bs, offset, &nr, &off);
-        if (ret != QCOW2_CLUSTER_UNALLOCATED &&
-            ret != QCOW2_CLUSTER_ZERO_PLAIN &&
-            ret != QCOW2_CLUSTER_ZERO_ALLOC) {
+        ret = qcow2_get_host_offset(bs, offset, &nr, &off, &type);
+        if (ret < 0 ||
+            (type != QCOW2_CLUSTER_UNALLOCATED &&
+             type != QCOW2_CLUSTER_ZERO_PLAIN &&
+             type != QCOW2_CLUSTER_ZERO_ALLOC)) {
             qemu_co_mutex_unlock(&s->lock);
             return -ENOTSUP;
         }
@@ -3792,16 +3797,18 @@  qcow2_co_copy_range_from(BlockDriverState *bs,
 
     while (bytes != 0) {
         uint64_t copy_offset = 0;
+        QCow2ClusterType type;
         /* prepare next request */
         cur_bytes = MIN(bytes, INT_MAX);
         cur_write_flags = write_flags;
 
-        ret = qcow2_get_host_offset(bs, src_offset, &cur_bytes, &copy_offset);
+        ret = qcow2_get_host_offset(bs, src_offset, &cur_bytes,
+                                    &copy_offset, &type);
         if (ret < 0) {
             goto out;
         }
 
-        switch (ret) {
+        switch (type) {
         case QCOW2_CLUSTER_UNALLOCATED:
             if (bs->backing && bs->backing->bs) {
                 int64_t backing_length = bdrv_getlength(bs->backing->bs);