diff mbox series

[3/3] qcow2: Use BDRV_SECTOR_SIZE instead of the hardcoded value

Message ID f6dcb0d7b17cddb475e3a018a5b802490c7bc21d.1578505678.git.berto@igalia.com
State New
Headers show
Series qcow2: Misc BDRV_SECTOR_SIZE updates | expand

Commit Message

Alberto Garcia Jan. 8, 2020, 5:49 p.m. UTC
This replaces all remaining instances in the qcow2 code.

Signed-off-by: Alberto Garcia <berto@igalia.com>
---
 block/qcow2-cluster.c |  2 +-
 block/qcow2.c         | 16 +++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)

Comments

Nir Soffer Jan. 8, 2020, 7:33 p.m. UTC | #1
On Wed, Jan 8, 2020 at 7:50 PM Alberto Garcia <berto@igalia.com> wrote:
>
> This replaces all remaining instances in the qcow2 code.
>
> Signed-off-by: Alberto Garcia <berto@igalia.com>
> ---
>  block/qcow2-cluster.c |  2 +-
>  block/qcow2.c         | 16 +++++++++-------
>  2 files changed, 10 insertions(+), 8 deletions(-)
>
> diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
> index 932fc48919..777ca2d409 100644
> --- a/block/qcow2-cluster.c
> +++ b/block/qcow2-cluster.c
> @@ -219,7 +219,7 @@ static int l2_load(BlockDriverState *bs, uint64_t offset,
>   * Writes one sector of the L1 table to the disk (can't update single entries
>   * and we really don't want bdrv_pread to perform a read-modify-write)
>   */
> -#define L1_ENTRIES_PER_SECTOR (512 / 8)
> +#define L1_ENTRIES_PER_SECTOR (BDRV_SECTOR_SIZE / 8)
>  int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
>  {
>      BDRVQcow2State *s = bs->opaque;
> diff --git a/block/qcow2.c b/block/qcow2.c
> index e8ce966f7f..6427c75409 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -2175,7 +2175,7 @@ static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs,
>                                            offset, bytes, qiov, qiov_offset);
>
>      case QCOW2_CLUSTER_NORMAL:
> -        if ((file_cluster_offset & 511) != 0) {
> +        if ((file_cluster_offset % BDRV_SECTOR_SIZE) != 0) {

All the alignment checks should use QEMU_IS_ALIGNED.

>              return -EIO;
>          }
>
> @@ -2514,7 +2514,7 @@ static coroutine_fn int qcow2_co_pwritev_part(
>              goto out_locked;
>          }
>
> -        assert((cluster_offset & 511) == 0);
> +        assert((cluster_offset % BDRV_SECTOR_SIZE) == 0);
>
>          ret = qcow2_pre_write_overlap_check(bs, 0,
>                                              cluster_offset + offset_in_cluster,
> @@ -3283,7 +3283,8 @@ qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
>
>      /* Validate options and set default values */
>      if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) {
> -        error_setg(errp, "Image size must be a multiple of 512 bytes");
> +        error_setg(errp, "Image size must be a multiple of %u bytes",
> +                   (unsigned) BDRV_SECTOR_SIZE);
>          ret = -EINVAL;
>          goto out;
>      }
> @@ -3839,7 +3840,7 @@ qcow2_co_copy_range_from(BlockDriverState *bs,
>          case QCOW2_CLUSTER_NORMAL:
>              child = s->data_file;
>              copy_offset += offset_into_cluster(s, src_offset);
> -            if ((copy_offset & 511) != 0) {
> +            if ((copy_offset % BDRV_SECTOR_SIZE) != 0) {
>                  ret = -EIO;
>                  goto out;
>              }
> @@ -3904,7 +3905,7 @@ qcow2_co_copy_range_to(BlockDriverState *bs,
>              goto fail;
>          }
>
> -        assert((cluster_offset & 511) == 0);
> +        assert((cluster_offset % BDRV_SECTOR_SIZE) == 0);
>
>          ret = qcow2_pre_write_overlap_check(bs, 0,
>                  cluster_offset + offset_in_cluster, cur_bytes, true);
> @@ -3961,8 +3962,9 @@ static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
>          return -ENOTSUP;
>      }
>
> -    if (offset & 511) {
> -        error_setg(errp, "The new size must be a multiple of 512");
> +    if (offset % BDRV_SECTOR_SIZE) {
> +        error_setg(errp, "The new size must be a multiple of %u",
> +                   (unsigned) BDRV_SECTOR_SIZE);
>          return -EINVAL;
>      }
>
> --
> 2.20.1

Otherwise looks good.

Nir
Kevin Wolf Jan. 9, 2020, 12:19 p.m. UTC | #2
Am 08.01.2020 um 18:49 hat Alberto Garcia geschrieben:
> This replaces all remaining instances in the qcow2 code.
> 
> Signed-off-by: Alberto Garcia <berto@igalia.com>

> diff --git a/block/qcow2.c b/block/qcow2.c
> index e8ce966f7f..6427c75409 100644
> --- a/block/qcow2.c
> +++ b/block/qcow2.c
> @@ -2175,7 +2175,7 @@ static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs,
>                                            offset, bytes, qiov, qiov_offset);
>  
>      case QCOW2_CLUSTER_NORMAL:
> -        if ((file_cluster_offset & 511) != 0) {
> +        if ((file_cluster_offset % BDRV_SECTOR_SIZE) != 0) {
>              return -EIO;
>          }

Hm, unrelated to your change, but why do we test for 512 byte alignment
here? file_cluster_offset should certainly be cluster aligned for normal
clusters. And if the check fails, that's actually an image corruption
and not just an I/O error. Am I missing something?

Kevin
Alberto Garcia Jan. 9, 2020, 12:30 p.m. UTC | #3
On Thu 09 Jan 2020 01:19:00 PM CET, Kevin Wolf wrote:
>> diff --git a/block/qcow2.c b/block/qcow2.c
>> index e8ce966f7f..6427c75409 100644
>> --- a/block/qcow2.c
>> +++ b/block/qcow2.c
>> @@ -2175,7 +2175,7 @@ static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs,
>>                                            offset, bytes, qiov, qiov_offset);
>>  
>>      case QCOW2_CLUSTER_NORMAL:
>> -        if ((file_cluster_offset & 511) != 0) {
>> +        if ((file_cluster_offset % BDRV_SECTOR_SIZE) != 0) {
>>              return -EIO;
>>          }
>
> Hm, unrelated to your change, but why do we test for 512 byte
> alignment here? file_cluster_offset should certainly be cluster
> aligned for normal clusters. And if the check fails, that's actually
> an image corruption and not just an I/O error. Am I missing something?

I actually suspect that this is just an old, obsolete check that we have
kept during these years. file_cluster_offset should be not just sector
aligned but also cluster aligned if I'm not wrong, and if not then
qcow2_alloc_cluster_offset() and qcow2_get_cluster_offset() should
return an error.

I can simply remove that check, or replace it with an assertion.

Berto
Kevin Wolf Jan. 9, 2020, 12:43 p.m. UTC | #4
Am 09.01.2020 um 13:30 hat Alberto Garcia geschrieben:
> On Thu 09 Jan 2020 01:19:00 PM CET, Kevin Wolf wrote:
> >> diff --git a/block/qcow2.c b/block/qcow2.c
> >> index e8ce966f7f..6427c75409 100644
> >> --- a/block/qcow2.c
> >> +++ b/block/qcow2.c
> >> @@ -2175,7 +2175,7 @@ static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs,
> >>                                            offset, bytes, qiov, qiov_offset);
> >>  
> >>      case QCOW2_CLUSTER_NORMAL:
> >> -        if ((file_cluster_offset & 511) != 0) {
> >> +        if ((file_cluster_offset % BDRV_SECTOR_SIZE) != 0) {
> >>              return -EIO;
> >>          }
> >
> > Hm, unrelated to your change, but why do we test for 512 byte
> > alignment here? file_cluster_offset should certainly be cluster
> > aligned for normal clusters. And if the check fails, that's actually
> > an image corruption and not just an I/O error. Am I missing something?
> 
> I actually suspect that this is just an old, obsolete check that we have
> kept during these years. file_cluster_offset should be not just sector
> aligned but also cluster aligned if I'm not wrong, and if not then
> qcow2_alloc_cluster_offset() and qcow2_get_cluster_offset() should
> return an error.

Right, they already check it, and don't only return an error, but also
call qcow2_signal_corruption() as they should.

> I can simply remove that check, or replace it with an assertion.

Sounds good to me (and with cluster size instead of 512).

Kevin
diff mbox series

Patch

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 932fc48919..777ca2d409 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -219,7 +219,7 @@  static int l2_load(BlockDriverState *bs, uint64_t offset,
  * Writes one sector of the L1 table to the disk (can't update single entries
  * and we really don't want bdrv_pread to perform a read-modify-write)
  */
-#define L1_ENTRIES_PER_SECTOR (512 / 8)
+#define L1_ENTRIES_PER_SECTOR (BDRV_SECTOR_SIZE / 8)
 int qcow2_write_l1_entry(BlockDriverState *bs, int l1_index)
 {
     BDRVQcow2State *s = bs->opaque;
diff --git a/block/qcow2.c b/block/qcow2.c
index e8ce966f7f..6427c75409 100644
--- a/block/qcow2.c
+++ b/block/qcow2.c
@@ -2175,7 +2175,7 @@  static coroutine_fn int qcow2_co_preadv_task(BlockDriverState *bs,
                                           offset, bytes, qiov, qiov_offset);
 
     case QCOW2_CLUSTER_NORMAL:
-        if ((file_cluster_offset & 511) != 0) {
+        if ((file_cluster_offset % BDRV_SECTOR_SIZE) != 0) {
             return -EIO;
         }
 
@@ -2514,7 +2514,7 @@  static coroutine_fn int qcow2_co_pwritev_part(
             goto out_locked;
         }
 
-        assert((cluster_offset & 511) == 0);
+        assert((cluster_offset % BDRV_SECTOR_SIZE) == 0);
 
         ret = qcow2_pre_write_overlap_check(bs, 0,
                                             cluster_offset + offset_in_cluster,
@@ -3283,7 +3283,8 @@  qcow2_co_create(BlockdevCreateOptions *create_options, Error **errp)
 
     /* Validate options and set default values */
     if (!QEMU_IS_ALIGNED(qcow2_opts->size, BDRV_SECTOR_SIZE)) {
-        error_setg(errp, "Image size must be a multiple of 512 bytes");
+        error_setg(errp, "Image size must be a multiple of %u bytes",
+                   (unsigned) BDRV_SECTOR_SIZE);
         ret = -EINVAL;
         goto out;
     }
@@ -3839,7 +3840,7 @@  qcow2_co_copy_range_from(BlockDriverState *bs,
         case QCOW2_CLUSTER_NORMAL:
             child = s->data_file;
             copy_offset += offset_into_cluster(s, src_offset);
-            if ((copy_offset & 511) != 0) {
+            if ((copy_offset % BDRV_SECTOR_SIZE) != 0) {
                 ret = -EIO;
                 goto out;
             }
@@ -3904,7 +3905,7 @@  qcow2_co_copy_range_to(BlockDriverState *bs,
             goto fail;
         }
 
-        assert((cluster_offset & 511) == 0);
+        assert((cluster_offset % BDRV_SECTOR_SIZE) == 0);
 
         ret = qcow2_pre_write_overlap_check(bs, 0,
                 cluster_offset + offset_in_cluster, cur_bytes, true);
@@ -3961,8 +3962,9 @@  static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t offset,
         return -ENOTSUP;
     }
 
-    if (offset & 511) {
-        error_setg(errp, "The new size must be a multiple of 512");
+    if (offset % BDRV_SECTOR_SIZE) {
+        error_setg(errp, "The new size must be a multiple of %u",
+                   (unsigned) BDRV_SECTOR_SIZE);
         return -EINVAL;
     }