Skip to contentSkip to conversation

parallels: Refactor the code of images checks and fix a bug

PATCH Superseded Alexander Ivanov 3 participants 31 messages Aug 22, 2022 – Aug 24, 2022 Block layer

v5 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v5 cover parallels: Refactor the code of images checks and fix a bug

Fix image inflation when offset in BAT is out of image.

Replace whole BAT syncing by flushing only dirty blocks.

Move all the checks outside the main check function in separate functions

Use WITH_QEMU_LOCK_GUARD for simplier code.

v5:
2: Change the way of data_end fixing.
6,7: Move data_end check to parallels_check_leak().

v4:
1: Move s->data_end fix to parallels_co_check(). Split the check
in parallels_open() and the fix in parallels_co_check() to two patches.
2: A new patch - a part of the patch 1.
Add a fix for data_end to parallels_co_check().
3: Move offset convertation to parallels_set_bat_entry().
4: Fix 'ret' rewriting by bdrv_co_flush() results.
7: Keep 'i' as uint32_t.

v3:

1-8: Fix commit message.

v2:

2: A new patch - a part of the splitted patch 2.
3: Patch order was changed so the replacement is done in parallels_co_check.
Now we use a helper to set BAT entry and mark the block dirty.
4: Revert the condition with s->header_unclean.
5: Move unrelated helper parallels_set_bat_entry creation to a separate patch. 7: Move fragmentation counting code to this function too.
8: Fix an incorrect usage of WITH_QEMU_LOCK_GUARD.

Alexander Ivanov (9):
parallels: Out of image offset in BAT leads to image inflation
parallels: Fix data_end field value in parallels_co_check()
parallels: create parallels_set_bat_entry_helper() to assign BAT value
parallels: Use generic infrastructure for BAT writing in
parallels_co_check()
parallels: Move check of unclean image to a separate function
parallels: Move check of cluster outside image to a separate function
parallels: Move check of leaks to a separate function
parallels: Move statistic collection to a separate function
parallels: Replace qemu_co_mutex_lock by WITH_QEMU_LOCK_GUARD

 block/parallels.c | 194 +++++++++++++++++++++++++++++++++-------------
 1 file changed, 138 insertions(+), 56 deletions(-)
Signature · 1 line
2.34.1
#1 lore raw

PATCH v5 1/9 parallels: Out of image offset in BAT leads to image inflation

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
 block/parallels.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
block/parallels.c +14 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
     BDRVParallelsState *s = bs->opaque;
     ParallelsHeader ph;
     int ret, size, i;
+    int64_t file_size;
     QemuOpts *opts = NULL;
     Error *local_err = NULL;
     char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         }
     }
 
+    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }
+
     if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
         /* Image was not closed correctly. The check is mandatory */
         s->header_unclean = true;
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 30 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
block/parallels.c +3 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
      BDRVParallelsState *s = bs->opaque;
      ParallelsHeader ph;
      int ret, size, i;
+    int64_t file_size;
      QemuOpts *opts = NULL;
      Error *local_err = NULL;
      char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
          }
      }
 
+    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 826

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

Quoted 4 lines
block/parallels.c +1 -0
+
      if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
          /* Image was not closed correctly. The check is mandatory */
          s->header_unclean = true;
Signature · 2 lines
Best regards,
Vladimir
#3 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 45 lines · Vladimir Sementsov-Ogievskiy
Quoted 31 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +1 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;

      char *buf;

block/parallels.c +1 -0
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, · line 826

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

Quoted 7 lines
Quoted 5 lines
block/parallels.c +1 -0
+
       if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
           /* Image was not closed correctly. The check is mandatory */

          s->header_unclean = true;

#4 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 7 lines · Alexander Ivanov
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Quoted 4 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.

Actually, it's correct, as don't break the spec at docs/interop/parallels.txt.

So, it's bad but still correct.. So, we probably can restrict it in Qemu if we want. Do we?

Now I doubt. We are going to refuse images with leaks even for read. That means, qemu-img convert will stop working for images with leaks, you'll have to fix the image first, which may fail.

Quoted 38 lines
Quoted 38 lines
Quoted 25 lines

Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +2 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;
       char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 825

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 5 lines
Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

Hmm, or even to cluster_size? When last cluster is unallocated. But cluster boundary is not required to be aligned to cluster size..

Anyway, now I think it's wrong to restrict opening file with leaks of any kind. That breaks qemu-img convert, and that's not how other formats behave.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
block/parallels.c +1 -0
+
       if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
           /* Image was not closed correctly. The check is mandatory */
           s->header_unclean = true;
Signature · 2 lines
Best regards,
Vladimir
#5 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 55 lines · Vladimir Sementsov-Ogievskiy
Quoted 7 lines · Alexander Ivanov
Quoted 5 lines · Vladimir Sementsov-Ogievskiy
Quoted 4 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.

Actually, it's correct, as don't break the spec at docs/interop/parallels.txt.

So, it's bad but still correct.. So, we probably can restrict it in Qemu if we want. Do we?

Now I doubt. We are going to refuse images with leaks even for read. That means, qemu-img convert will stop working for images with leaks, you'll have to fix the image first, which may fail.

Quoted 40 lines
Quoted 40 lines
Quoted 25 lines

Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +2 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;
       char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 825

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

Quoted 18 lines
Quoted 3 lines
Quoted 2 lines

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

Hmm, or even to cluster_size? When last cluster is unallocated. But cluster boundary is not required to be aligned to cluster size..

Anyway, now I think it's wrong to restrict opening file with leaks of any kind. That breaks qemu-img convert, and that's not how other formats behave.

Quoted 7 lines
Quoted 7 lines
Quoted 5 lines
block/parallels.c +1 -0
+
       if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
           /* Image was not closed correctly. The check is mandatory */
           s->header_unclean = true;

Oops, ignore this. We are not about leaks (when file size > data_end), we are about wrong clusters when data_end > file_size.

Signature · 2 lines
Best regards,
Vladimir
#6 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

In reply to Alexander Ivanov
Quoted 47 lines · Alexander Ivanov
Quoted 45 lines · Vladimir Sementsov-Ogievskiy
Quoted 31 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +1 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;

      char *buf;

block/parallels.c +1 -0
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, · line 826

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 5 lines
Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

I would say that we need to align not on sector size but on cluster size. That would worth additional check.

#7 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 48 lines · Denis V. Lunev
Quoted 47 lines · Alexander Ivanov
Quoted 45 lines · Vladimir Sementsov-Ogievskiy
Quoted 31 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +1 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;

      char *buf;

block/parallels.c +1 -0
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState 
 *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState · line 826

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

I would say that we need to align not on sector size but on cluster size. That would worth additional check.

I agree on it for "WithouFreSpacExt" image format.

For "WithoutFreeSpace" format there are two restrictions:

1) the result of (cluster offset - data offset) must be aligned to cluster size:

2) data_off should be aligned to sector size (0 in data_off leads to the same restriction).

So file size can be aligned on sector size, but not aligned on cluster size.

Should we check dependent on image format?

#8 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

In reply to Denis V. Lunev
Quoted 46 lines · Denis V. Lunev
Quoted 45 lines · Alexander Ivanov
Quoted 43 lines · Vladimir Sementsov-Ogievskiy
Quoted 29 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +2 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;
       char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 825

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

I would say that we need to align not on sector size but on cluster size. That would worth additional check.

And not simply align, as data_offset is not necessarily aligned to cluster size.

Finally, what should we check?

I suggest

block/parallels.c +13 -0
index 6d4ed77f16..b882ea1200 100644
@@ -725,6 +725,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
     BDRVParallelsState *s = bs->opaque;
     ParallelsHeader ph;
     int ret, size, i;
+    int64_t file_size;
     QemuOpts *opts = NULL;
     Error *local_err = NULL;
     char *buf;
@@ -735,6 +736,11 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         return -EINVAL;
     }
 
+    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        return file_size;
+    }
+
     ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
     if (ret < 0) {
         goto fail;
@@ -798,6 +804,13 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
 
     for (i = 0; i < s->bat_size; i++) {
         int64_t off = bat2sect(s, i);
+        if (off >= file_size) {
+            error_setg(errp, "parallels: Offset %" PRIi64 " in BAT[%d] entry "
+                       "is larger than file size (%" PRIi64 ")",
+                       off, i, file_size);
+            ret = -EINVAL;
+            goto fail;
+        }
         if (off >= s->data_end) {
             s->data_end = off + s->tracks;
         }

- better error message, and we check exactly what's written in the spec (docs/interop/parallels.c):

Cluster offsets specified by BAT entries must meet the following requirements:
[...]
- the value must be lower than the desired file size,

Signature · 2 lines
Best regards,
Vladimir
#9 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 93 lines · Vladimir Sementsov-Ogievskiy
Quoted 49 lines · Denis V. Lunev
Quoted 48 lines · Alexander Ivanov
Quoted 46 lines · Vladimir Sementsov-Ogievskiy
Quoted 32 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +1 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState 
 *bs, QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;

      char *buf;

block/parallels.c +2 -0
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState 
 *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        ret = file_size;

block/parallels.c static int parallels_open(BlockDriverState · line 827

Quoted 10 lines
block/parallels.c +9 -0
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of 
 image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

I would say that we need to align not on sector size but on cluster size. That would worth additional check.

And not simply align, as data_offset is not necessarily aligned to cluster size.

Finally, what should we check?

I suggest

block/parallels.c +1 -0
index 6d4ed77f16..b882ea1200 100644
@@ -725,6 +725,7 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
      BDRVParallelsState *s = bs->opaque;
      ParallelsHeader ph;
      int ret, size, i;
+    int64_t file_size;
      QemuOpts *opts = NULL;
      Error *local_err = NULL;

     char *buf;

block/parallels.c +5 -0
@@ -735,6 +736,11 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
          return -EINVAL;
      }
 
+    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        return file_size;
+    }
+
      ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
      if (ret < 0) {

         goto fail;

block/parallels.c static int parallels_open(BlockDriverState *bs, · line 808

Quoted 5 lines
block/parallels.c +1 -0
@@ -798,6 +804,13 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
 
      for (i = 0; i < s->bat_size; i++) {
          int64_t off = bat2sect(s, i);
+        if (off >= file_size) {

Like this, especially >= check which we have had missed. Though this would break the repair. We need additional

if (flags & BDRV_O_CHECK) {
    continue;
}

No incorrect data_end assignment, which would be
very welcome.

Den

Quoted 22 lines
block/parallels.c +6 -0
+ error_setg(errp, "parallels: Offset %" PRIi64 " in BAT[%d] entry "
+                       "is larger than file size (%" PRIi64 ")",
+                       off, i, file_size);
+            ret = -EINVAL;
+            goto fail;
+        }
          if (off >= s->data_end) {
              s->data_end = off + s->tracks;

         }

- better error message, and we check exactly what's written in the spec (docs/interop/parallels.c):

  Cluster offsets specified by BAT entries must meet the following requirements:
      [...]
      - the value must be lower than the desired file size,

#10 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 96 lines · Denis V. Lunev
Quoted 88 lines · Vladimir Sementsov-Ogievskiy
Quoted 46 lines · Denis V. Lunev
Quoted 45 lines · Alexander Ivanov
Quoted 43 lines · Vladimir Sementsov-Ogievskiy
Quoted 29 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +2 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;
       char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 825

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

I would say that we need to align not on sector size but on cluster size. That would worth additional check.

And not simply align, as data_offset is not necessarily aligned to cluster size.

Finally, what should we check?

I suggest

block/parallels.c +6 -0
index 6d4ed77f16..b882ea1200 100644
@@ -725,6 +725,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
      BDRVParallelsState *s = bs->opaque;
      ParallelsHeader ph;
      int ret, size, i;
+    int64_t file_size;
      QemuOpts *opts = NULL;
      Error *local_err = NULL;
      char *buf;
@@ -735,6 +736,11 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
          return -EINVAL;
      }
 
+    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        return file_size;
+    }
+
      ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
      if (ret < 0) {
          goto fail;

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 807

Quoted 4 lines
block/parallels.c +1 -0
@@ -798,6 +804,13 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
 
      for (i = 0; i < s->bat_size; i++) {
          int64_t off = bat2sect(s, i);
+        if (off >= file_size) {

Like this, especially >= check which we have had missed. Though this would break the repair. We need additional

Quoted 8 lines

if (flags & BDRV_O_CHECK) {
    continue;
}

No incorrect data_end assignment, which would be
very welcome.

Den

'continue' here will change the logic around data_end. We'll drop "wrong" clusters from calculation of data_end, and should check, how it affects further logic.

What about:

   for (i = 0; i < s->bat_size; i++) {
        int64_t off = bat2sect(s, i);
        if (off >= file_size && !(flags & BDRV_O_CHECK)) {
            error_setg(errp, "parallels: Offset %" PRIi64 " in BAT[%d] entry "
                       "is larger than file size (%" PRIi64 ")",
                       off, i, file_size);
            ret = -EINVAL;
            goto fail;
        }
        if (off >= s->data_end) {
            s->data_end = off + s->tracks;
        }
    }

- this we simply add new error-out on no-O_CHECK path.

Quoted 23 lines
Quoted 22 lines
block/parallels.c +6 -1
+ error_setg(errp, "parallels: Offset %" PRIi64 " in BAT[%d] entry "
+                       "is larger than file size (%" PRIi64 ")",
+                       off, i, file_size);
+            ret = -EINVAL;
+            goto fail;
+        }
          if (off >= s->data_end) {
              s->data_end = off + s->tracks;
          }
 
 
 
- better error message, and we check exactly what's written in the spec (docs/interop/parallels.c):

  Cluster offsets specified by BAT entries must meet the following requirements:
      [...]
      - the value must be lower than the desired file size,

Signature · 2 lines
Best regards,
Vladimir
#11 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

In reply to Vladimir Sementsov-Ogievskiy
Quoted 93 lines · Vladimir Sementsov-Ogievskiy
Quoted 49 lines · Denis V. Lunev
Quoted 48 lines · Alexander Ivanov
Quoted 46 lines · Vladimir Sementsov-Ogievskiy
Quoted 32 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +1 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState 
 *bs, QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;

      char *buf;

block/parallels.c +2 -0
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState 
 *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        ret = file_size;

block/parallels.c static int parallels_open(BlockDriverState · line 827

Quoted 10 lines
block/parallels.c +9 -0
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of 
 image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

I would say that we need to align not on sector size but on cluster size. That would worth additional check.

And not simply align, as data_offset is not necessarily aligned to cluster size.

Finally, what should we check?

I suggest

block/parallels.c +1 -0
index 6d4ed77f16..b882ea1200 100644
@@ -725,6 +725,7 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
      BDRVParallelsState *s = bs->opaque;
      ParallelsHeader ph;
      int ret, size, i;
+    int64_t file_size;
      QemuOpts *opts = NULL;
      Error *local_err = NULL;

     char *buf;

block/parallels.c +5 -0
@@ -735,6 +736,11 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
          return -EINVAL;
      }
 
+    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        return file_size;
+    }
+
      ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
      if (ret < 0) {

         goto fail;

block/parallels.c static int parallels_open(BlockDriverState *bs, · line 808

Quoted 5 lines
block/parallels.c +1 -0
@@ -798,6 +804,13 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
 
      for (i = 0; i < s->bat_size; i++) {
          int64_t off = bat2sect(s, i);
+        if (off >= file_size) {

Do we really want to compare file size with cluster offset, not with cluster end?

Also I would leave the comparison outside the loop, because I'm going to move highest offset calculation to a helper (there are two places with the same code).

Quoted 22 lines
block/parallels.c +6 -0
+ error_setg(errp, "parallels: Offset %" PRIi64 " in BAT[%d] entry "
+                       "is larger than file size (%" PRIi64 ")",
+                       off, i, file_size);
+            ret = -EINVAL;
+            goto fail;
+        }
          if (off >= s->data_end) {
              s->data_end = off + s->tracks;

         }

- better error message, and we check exactly what's written in the spec (docs/interop/parallels.c):

  Cluster offsets specified by BAT entries must meet the following requirements:
      [...]
      - the value must be lower than the desired file size,

#12 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 91 lines · Alexander Ivanov
Quoted 89 lines · Vladimir Sementsov-Ogievskiy
Quoted 46 lines · Denis V. Lunev
Quoted 45 lines · Alexander Ivanov
Quoted 43 lines · Vladimir Sementsov-Ogievskiy
Quoted 29 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +2 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;
       char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 825

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 8 lines
Quoted 5 lines
Quoted 4 lines

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

I think you are right, it would be better to align image size up to sector size.

I would say that we need to align not on sector size but on cluster size. That would worth additional check.

And not simply align, as data_offset is not necessarily aligned to cluster size.

Finally, what should we check?

I suggest

block/parallels.c +6 -0
index 6d4ed77f16..b882ea1200 100644
@@ -725,6 +725,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
      BDRVParallelsState *s = bs->opaque;
      ParallelsHeader ph;
      int ret, size, i;
+    int64_t file_size;
      QemuOpts *opts = NULL;
      Error *local_err = NULL;
      char *buf;
@@ -735,6 +736,11 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
          return -EINVAL;
      }
 
+    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        return file_size;
+    }
+
      ret = bdrv_pread(bs->file, 0, &ph, sizeof(ph));
      if (ret < 0) {
          goto fail;
@@ -798,6 +804,13 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
 
Quoted 5 lines

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 807

Quoted 3 lines
block/parallels.c +1 -0
      for (i = 0; i < s->bat_size; i++) {
          int64_t off = bat2sect(s, i);
+        if (off >= file_size) {

Do we really want to compare file size with cluster offset, not with cluster end?

the spec say:
Cluster offsets specified by BAT entries must meet the following requirements:
[...]
- the value must be lower than the desired file size,

Quoted 2 lines

Also I would leave the comparison outside the loop, because I'm going to move highest offset calculation to a helper (there are two places with the same code).

benefits of check inside the loop:

1. we get good error message with BAT index and problematic offset
2. we are sure that data_end is produced by real cluster. After the loop you'll have to consider image with no clusters separately

Quoted 22 lines
Quoted 22 lines
block/parallels.c +6 -1
+ error_setg(errp, "parallels: Offset %" PRIi64 " in BAT[%d] entry "
+                       "is larger than file size (%" PRIi64 ")",
+                       off, i, file_size);
+            ret = -EINVAL;
+            goto fail;
+        }
          if (off >= s->data_end) {
              s->data_end = off + s->tracks;
          }
 
 
 
- better error message, and we check exactly what's written in the spec (docs/interop/parallels.c):

  Cluster offsets specified by BAT entries must meet the following requirements:
      [...]
      - the value must be lower than the desired file size,

Signature · 2 lines
Best regards,
Vladimir
#13 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

In reply to Vladimir Sementsov-Ogievskiy
Quoted 45 lines · Vladimir Sementsov-Ogievskiy
Quoted 31 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +1 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;

      char *buf;

block/parallels.c +1 -0
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, · line 826

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 1 line

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

That is a different check. We need to be sure that file_size is authentic, which worth additional check.

At my opinion, this worth additional patch later on. Let us agree here, queue and proceed with further pending fixes.

We should use something like the following

    data_start = le32_to_cpu(s->header->data_off);
    if (data_start == 0) {
        data_start = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE); }
    if ((file_size - data_start) % cluster_size != 0) {
        error();
    }

Quoted 10 lines

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

Quoted 4 lines
block/parallels.c +1 -0
+
       if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
           /* Image was not closed correctly. The check is mandatory */

          s->header_unclean = true;

#14 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 53 lines · Denis V. Lunev
Quoted 43 lines · Vladimir Sementsov-Ogievskiy
Quoted 29 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +2 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;
       char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 825

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 1 line

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

That is a different check. We need to be sure that file_size is authentic, which worth additional check.

At my opinion, this worth additional patch later on. Let us agree here, queue and proceed with further pending fixes.

We should use something like the following

Quoted 7 lines

    data_start = le32_to_cpu(s->header->data_off);
    if (data_start == 0) {
        data_start = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE); }
    if ((file_size - data_start) % cluster_size != 0) {
        error();
    }

Do you think that we should error-out in such conditions? It doesn't break the spec, is it? Can the last cluster be half allocated?

Quoted 12 lines
Quoted 11 lines

It's hardly possible to get such image on valid scenarios with Qemu (keeping in mind bdrv_truncate() call in parallels_close()). But it still may be possible to have such images produced by another software or by some failure path.

Quoted 4 lines
block/parallels.c +1 -0
+
       if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
           /* Image was not closed correctly. The check is mandatory */
           s->header_unclean = true;
Signature · 2 lines
Best regards,
Vladimir
#15 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

Quoted 64 lines · Vladimir Sementsov-Ogievskiy
Quoted 55 lines · Denis V. Lunev
Quoted 45 lines · Vladimir Sementsov-Ogievskiy
Quoted 31 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++

  1 file changed, 14 insertions(+)
block/parallels.c +1 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, 
 QDict *options, int flags,
       BDRVParallelsState *s = bs->opaque;
       ParallelsHeader ph;
       int ret, size, i;
+    int64_t file_size;
       QemuOpts *opts = NULL;
       Error *local_err = NULL;

      char *buf;

block/parallels.c +1 -0
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState 
 *bs, QDict *options, int flags,
           }
       }
   +    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {

block/parallels.c static int parallels_open(BlockDriverState · line 826

Quoted 10 lines
block/parallels.c +10 -0
+        ret = file_size;
+        goto fail;
+    }
+
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }

If image is unaligned to sector size, and image size is less than s->data_end, but the difference itself is less than sector, the error message would be misleading.

Quoted 1 line

Should we consider "file_size = DIV_ROUND_UP(file_size, BDRV_SECTOR_SIZE)" instead of "file_size >>= BDRV_SECTOR_BITS"?

That is a different check. We need to be sure that file_size is authentic, which worth additional check.

At my opinion, this worth additional patch later on. Let us agree here, queue and proceed with further pending fixes.

We should use something like the following

Quoted 7 lines

     data_start = le32_to_cpu(s->header->data_off);
     if (data_start == 0) {
         data_start = ROUND_UP(bat_entry_off(s->bat_size), BDRV_SECTOR_SIZE); }
     if ((file_size - data_start) % cluster_size != 0) {
         error();
     }

Quoted 1 line

Do you think that we should error-out in such conditions? It doesn't break the spec, is it? Can the last cluster be half allocated?

absolutely no!

We should adjust the spec on this.

Den

Reviewed-by on v5 1/9
#16 lore raw

Re: [PATCH v5 1/9] parallels: Out of image offset in BAT leads to image inflation

In reply to Alexander Ivanov
Quoted 34 lines · Alexander Ivanov

data_end field in BDRVParallelsState is set to the biggest offset present in BAT. If this offset is outside of the image, any further write will create the cluster at this offset and/or the image will be truncated to this offset on close. This is definitely not correct.
Raise an error in parallels_open() if data_end points outside the image and it is not a check (let the check to repaire the image).

---
  block/parallels.c | 14 ++++++++++++++
  1 file changed, 14 insertions(+)
block/parallels.c +7 -0
index a229c06f25..c245ca35cd 100644
@@ -732,6 +732,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
      BDRVParallelsState *s = bs->opaque;
      ParallelsHeader ph;
      int ret, size, i;
+    int64_t file_size;
      QemuOpts *opts = NULL;
      Error *local_err = NULL;
      char *buf;
@@ -811,6 +812,19 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
          }
      }
 
+    file_size = bdrv_getlength(bs->file->bs);
+    if (file_size < 0) {
+        ret = file_size;
+        goto fail;
+    }
+

block/parallels.c static int parallels_open(BlockDriverState *bs, QDict *options, int flags, · line 830

Quoted 10 lines
block/parallels.c +7 -0
+    file_size >>= BDRV_SECTOR_BITS;
+    if (s->data_end > file_size && !(flags & BDRV_O_CHECK)) {
+        error_setg(errp, "parallels: Offset in BAT is out of image");
+        ret = -EINVAL;
+        goto fail;
+    }
+
      if (le32_to_cpu(ph.inuse) == HEADER_INUSE_MAGIC) {
          /* Image was not closed correctly. The check is mandatory */
          s->header_unclean = true;
#17 lore raw

PATCH v5 2/9 parallels: Fix data_end field value in parallels_co_check()

In reply to Alexander Ivanov

Make data_end pointing to the end of the last cluster if a leak was fixed. Otherwise set the file size to data_end.

---
 block/parallels.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)
block/parallels.c +9 -1
index c245ca35cd..2be56871bc 100644
@@ -513,7 +513,15 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
             res->leaks_fixed += count;
         }
     }
-
+    /*
+     * If res->image_end_offset less than the file size,
+     * a leak was fixed and we should set the new offset to s->data_end.
+     * Otherwise set the file size to s->data_end.
+     */
+    if (res->image_end_offset < size && fix & BDRV_FIX_LEAKS) {
+        size = res->image_end_offset;
+    }
+    s->data_end = size >> BDRV_SECTOR_BITS;
 out:
     qemu_co_mutex_unlock(&s->lock);
     return ret;
Signature · 1 line
2.34.1
#18 lore raw

Re: [PATCH v5 2/9] parallels: Fix data_end field value in parallels_co_check()

Quoted 13 lines · Alexander Ivanov

Make data_end pointing to the end of the last cluster if a leak was fixed. Otherwise set the file size to data_end.

---
  block/parallels.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
block/parallels.c +0 -0
index c245ca35cd..2be56871bc 100644

block/parallels.c static int coroutine_fn parallels_co_check(BlockDriverState *bs, · line 519

Quoted 8 lines
block/parallels.c +4 -1
@@ -513,7 +513,15 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
              res->leaks_fixed += count;
          }
      }
-
+    /*
+     * If res->image_end_offset less than the file size,
+     * a leak was fixed and we should set the new offset to s->data_end.
+     * Otherwise set the file size to s->data_end.

I'm not sure about English :) For me "set A to B" means A := B, but you use it visa versa..

block/parallels.c static int coroutine_fn parallels_co_check(BlockDriverState *bs, · line 524

Quoted 5 lines
block/parallels.c +5 -0
+     */
+    if (res->image_end_offset < size && fix & BDRV_FIX_LEAKS) {
+        size = res->image_end_offset;
+    }
+    s->data_end = size >> BDRV_SECTOR_BITS;

Hmm, actually, when size < data_end, you can shrink data_end only if (fix & BDRV_FIX_ERRORS), otherwise BAT is still not fixed.

block/parallels.c static int coroutine_fn parallels_co_check(BlockDriverState *bs, · line 527

Quoted 3 lines
block/parallels.c +0 -0
  out:
      qemu_co_mutex_unlock(&s->lock);
      return ret;

Actually I think we only need to modify s->data_end after successful BAT fixing above. Then, bdrv_truncate is formally unrelated to s->data_end and shouldn't touch it.

So, I think, more correct is something like

block/parallels.c +4 -1
index 2be56871bc..b268788974 100644
@@ -479,20 +479,24 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
         prev_off = off;
     }
 
     ret = 0;
     if (flush_bat) {
         ret = bdrv_co_pwrite_sync(bs->file, 0, s->header_size, s->header, 0);
         if (ret < 0) {
             res->check_errors++;
             goto out;
         }
+
+        /* We have dropped some wrong clusters, update data_end */
+        assert(s->data_end * BDRV_SECTOR_SIZE >= high_off + s->cluster_size);
+        s->data_end = (high_off + s->cluster_size) / BDRV_SECTOR_SIZE;
     }
 
     res->image_end_offset = high_off + s->cluster_size;
     if (size > res->image_end_offset) {
 
 
 
 
-- 

Best regards,
Vladimir

#19 lore raw

Re: [PATCH v5 2/9] parallels: Fix data_end field value in parallels_co_check()

Quoted 18 lines · Vladimir Sementsov-Ogievskiy
Quoted 17 lines · Alexander Ivanov

Make data_end pointing to the end of the last cluster if a leak was fixed. Otherwise set the file size to data_end.

---
  block/parallels.c | 10 +++++++++-

  1 file changed, 9 insertions(+), 1 deletion(-)
block/parallels.c +0 -0
index c245ca35cd..2be56871bc 100644
@@ -513,7 +513,15 @@ static int coroutine_fn 
 parallels_co_check(BlockDriverState *bs,
               res->leaks_fixed += count;
           }
       }
Quoted 8 lines
Quoted 6 lines
block/parallels.c +3 -1
-
+    /*
+     * If res->image_end_offset less than the file size,
+     * a leak was fixed and we should set the new offset to 

s->data_end.
+     * Otherwise set the file size to s->data_end.

I'm not sure about English :) For me "set A to B" means A := B, but you use it visa versa..

I hesitated about this. I saw both variants and used the incorrect one probably. =)

Quoted 8 lines
Quoted 6 lines

+     */
+    if (res->image_end_offset < size && fix & BDRV_FIX_LEAKS) { +        size = res->image_end_offset;
+    }
+    s->data_end = size >> BDRV_SECTOR_BITS;

Hmm, actually, when size < data_end, you can shrink data_end only if (fix & BDRV_FIX_ERRORS), otherwise BAT is still not fixed.

We shrink an image if there is a leak and (fix & BDRV_FIX_LEAKS). Otherwise we set data_end to the file size. In such a way we don't change the file size in parallels_close() even if we have out-of-image offsets in BAT.

Quoted 26 lines
Quoted 4 lines

  out:
      qemu_co_mutex_unlock(&s->lock);
      return ret;

Actually I think we only need to modify s->data_end after successful BAT fixing above. Then, bdrv_truncate is formally unrelated to s->data_end and shouldn't touch it.

So, I think, more correct is something like

block/parallels.c +0 -0
index 2be56871bc..b268788974 100644
@@ -479,20 +479,24 @@ static int coroutine_fn 
 parallels_co_check(BlockDriverState *bs,
          prev_off = off;
      }
 
      ret = 0;
      if (flush_bat) {
          ret = bdrv_co_pwrite_sync(bs->file, 0, s->header_size, s->header, 0);
          if (ret < 0) {
              res->check_errors++;
              goto out;
          }

block/parallels.c static int coroutine_fn · line 499

Quoted 10 lines
block/parallels.c +4 -0
+
+        /* We have dropped some wrong clusters, update data_end */
+        assert(s->data_end * BDRV_SECTOR_SIZE >= high_off + 
 s->cluster_size);
+        s->data_end = (high_off + s->cluster_size) / BDRV_SECTOR_SIZE;
      }
 
      res->image_end_offset = high_off + s->cluster_size;
      if (size > res->image_end_offset) {
 

If an image was opened in RW mode for checking without fixing (I don't know if it's possible), data_end can be set outside the image and the image will be expanded in parallels_close().

OK, I would propose to fix data_end in two places:

1) after out-of-image check set data_end to the highest offset with (off + cluster_size <= file_size) condition, independent on fix flags;

2) after leak check, only if (size > res->image_end_offset).

In such a way we can have only one inconsistence after any check: out-of-image offsets isn't fixed and we have offsets after data_end. But it is much better than if we set data_end to petabytes, for example.

#20 lore raw

Re: [PATCH v5 2/9] parallels: Fix data_end field value in parallels_co_check()

In reply to Alexander Ivanov
Quoted 13 lines · Alexander Ivanov

Make data_end pointing to the end of the last cluster if a leak was fixed. Otherwise set the file size to data_end.

---
  block/parallels.c | 10 +++++++++-
  1 file changed, 9 insertions(+), 1 deletion(-)
block/parallels.c +0 -0
index c245ca35cd..2be56871bc 100644

block/parallels.c static int coroutine_fn parallels_co_check(BlockDriverState *bs, · line 521

Quoted 10 lines
block/parallels.c +6 -1
@@ -513,7 +513,15 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
              res->leaks_fixed += count;
          }
      }
-
+    /*
+     * If res->image_end_offset less than the file size,
+     * a leak was fixed and we should set the new offset to s->data_end.
+     * Otherwise set the file size to s->data_end.
+     */
+    if (res->image_end_offset < size && fix & BDRV_FIX_LEAKS) {

technically this is correct, but please add braces around (fix & BDRV_FIX_LEAKS) - the code would look better

Quoted 6 lines
block/parallels.c +3 -0
+        size = res->image_end_offset;
+    }
+    s->data_end = size >> BDRV_SECTOR_BITS;
  out:
      qemu_co_mutex_unlock(&s->lock);
      return ret;
#21 lore raw

PATCH v5 3/9 parallels: create parallels_set_bat_entry_helper() to assign BAT value

In reply to Alexander Ivanov

This helper will be reused in next patches during parallels_co_check rework to simplify its code.

---
 block/parallels.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
block/parallels.c +8 -3
index 2be56871bc..8733ef8a70 100644
@@ -165,6 +165,13 @@ static int64_t block_status(BDRVParallelsState *s, int64_t sector_num,
     return start_off;
 }
 
+static void parallels_set_bat_entry(BDRVParallelsState *s,
+                                    uint32_t index, uint32_t offset)
+{
+    s->bat_bitmap[index] = cpu_to_le32(offset);
+    bitmap_set(s->bat_dirty_bmap, bat_entry_off(index) / s->bat_dirty_block, 1);
+}
+
 static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
                                  int nb_sectors, int *pnum)
 {
@@ -250,10 +257,8 @@ static int64_t allocate_clusters(BlockDriverState *bs, int64_t sector_num,
     }
 
     for (i = 0; i < to_allocate; i++) {
-        s->bat_bitmap[idx + i] = cpu_to_le32(s->data_end / s->off_multiplier);
+        parallels_set_bat_entry(s, idx + i, s->data_end / s->off_multiplier);
         s->data_end += s->tracks;
-        bitmap_set(s->bat_dirty_bmap,
-                   bat_entry_off(idx + i) / s->bat_dirty_block, 1);
     }
 
     return bat2sect(s, idx) + sector_num % s->tracks;
Signature · 1 line
2.34.1
#22 lore raw

PATCH v5 4/9 parallels: Use generic infrastructure for BAT writing in parallels_co_check()

In reply to Alexander Ivanov

BAT is written in the context of conventional operations over the image inside bdrv_co_flush() when it calls parallels_co_flush_to_os() callback. Thus we should not modify BAT array directly, but call parallels_set_bat_entry() helper and bdrv_co_flush() further on. After that there is no need to manually write BAT and track its modification.

This makes code more generic and allows to split parallels_set_bat_entry() for independent pieces.

---
 block/parallels.c | 23 ++++++++++-------------
 1 file changed, 10 insertions(+), 13 deletions(-)
block/parallels.c +10 -13
index 8733ef8a70..357c345517 100644
@@ -425,9 +425,8 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
 {
     BDRVParallelsState *s = bs->opaque;
     int64_t size, prev_off, high_off;
-    int ret;
+    int ret = 0;
     uint32_t i;
-    bool flush_bat = false;
 
     size = bdrv_getlength(bs->file->bs);
     if (size < 0) {
@@ -466,9 +465,8 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
             res->corruptions++;
             if (fix & BDRV_FIX_ERRORS) {
                 prev_off = 0;
-                s->bat_bitmap[i] = 0;
+                parallels_set_bat_entry(s, i, 0);
                 res->corruptions_fixed++;
-                flush_bat = true;
                 continue;
             }
         }
@@ -484,15 +482,6 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
         prev_off = off;
     }
 
-    ret = 0;
-    if (flush_bat) {
-        ret = bdrv_co_pwrite_sync(bs->file, 0, s->header_size, s->header, 0);
-        if (ret < 0) {
-            res->check_errors++;
-            goto out;
-        }
-    }
-
     res->image_end_offset = high_off + s->cluster_size;
     if (size > res->image_end_offset) {
         int64_t count;
@@ -529,6 +518,14 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
     s->data_end = size >> BDRV_SECTOR_BITS;
 out:
     qemu_co_mutex_unlock(&s->lock);
+
+    if (ret == 0) {
+        ret = bdrv_co_flush(bs);
+        if (ret < 0) {
+            res->check_errors++;
+        }
+    }
+
     return ret;
 }
 
Signature · 1 line
2.34.1
Reviewed-by on v5 4/9
#23 lore raw

Re: [PATCH v5 4/9] parallels: Use generic infrastructure for BAT writing in parallels_co_check()

Quoted 62 lines · Alexander Ivanov

BAT is written in the context of conventional operations over the image inside bdrv_co_flush() when it calls parallels_co_flush_to_os() callback. Thus we should not modify BAT array directly, but call parallels_set_bat_entry() helper and bdrv_co_flush() further on. After that there is no need to manually write BAT and track its modification.

This makes code more generic and allows to split parallels_set_bat_entry() for independent pieces.

---
  block/parallels.c | 23 ++++++++++-------------
  1 file changed, 10 insertions(+), 13 deletions(-)
block/parallels.c +3 -13
index 8733ef8a70..357c345517 100644
@@ -425,9 +425,8 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
  {
      BDRVParallelsState *s = bs->opaque;
      int64_t size, prev_off, high_off;
-    int ret;
+    int ret = 0;
      uint32_t i;
-    bool flush_bat = false;
 
      size = bdrv_getlength(bs->file->bs);
      if (size < 0) {
@@ -466,9 +465,8 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
              res->corruptions++;
              if (fix & BDRV_FIX_ERRORS) {
                  prev_off = 0;
-                s->bat_bitmap[i] = 0;
+                parallels_set_bat_entry(s, i, 0);
                  res->corruptions_fixed++;
-                flush_bat = true;
                  continue;
              }
          }
@@ -484,15 +482,6 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
          prev_off = off;
      }
 
-    ret = 0;
-    if (flush_bat) {
-        ret = bdrv_co_pwrite_sync(bs->file, 0, s->header_size, s->header, 0);
-        if (ret < 0) {
-            res->check_errors++;
-            goto out;
-        }
-    }
-
      res->image_end_offset = high_off + s->cluster_size;
      if (size > res->image_end_offset) {
          int64_t count;
@@ -529,6 +518,14 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
      s->data_end = size >> BDRV_SECTOR_BITS;
  out:
      qemu_co_mutex_unlock(&s->lock);
+

block/parallels.c static int coroutine_fn parallels_co_check(BlockDriverState *bs, · line 531

Quoted 10 lines
block/parallels.c +7 -0
+    if (ret == 0) {
+        ret = bdrv_co_flush(bs);
+        if (ret < 0) {
+            res->check_errors++;
+        }
+    }
+
      return ret;
  }
 
#24 lore raw

PATCH v5 5/9 parallels: Move check of unclean image to a separate function

In reply to Alexander Ivanov

We will add more and more checks so we need a better code structure in parallels_co_check. Let each check performs in a separate loop in a separate helper.

---
 block/parallels.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)
block/parallels.c +21 -10
index 357c345517..a35035e231 100644
@@ -418,6 +418,25 @@ static coroutine_fn int parallels_co_readv(BlockDriverState *bs,
     return ret;
 }
 
+static void parallels_check_unclean(BlockDriverState *bs,
+                                    BdrvCheckResult *res,
+                                    BdrvCheckMode fix)
+{
+    BDRVParallelsState *s = bs->opaque;
+
+    if (!s->header_unclean) {
+        return;
+    }
+
+    fprintf(stderr, "%s image was not closed correctly\n",
+            fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
+    res->corruptions++;
+    if (fix & BDRV_FIX_ERRORS) {
+        /* parallels_close will do the job right */
+        res->corruptions_fixed++;
+        s->header_unclean = false;
+    }
+}
 
 static int coroutine_fn parallels_co_check(BlockDriverState *bs,
                                            BdrvCheckResult *res,
@@ -435,16 +454,8 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
     }
 
     qemu_co_mutex_lock(&s->lock);
-    if (s->header_unclean) {
-        fprintf(stderr, "%s image was not closed correctly\n",
-                fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR");
-        res->corruptions++;
-        if (fix & BDRV_FIX_ERRORS) {
-            /* parallels_close will do the job right */
-            res->corruptions_fixed++;
-            s->header_unclean = false;
-        }
-    }
+
+    parallels_check_unclean(bs, res, fix);
 
     res->bfi.total_clusters = s->bat_size;
     res->bfi.compressed_clusters = 0; /* compression is not supported */
Signature · 1 line
2.34.1
#25 lore raw

PATCH v5 6/9 parallels: Move check of cluster outside image to a separate function

In reply to Alexander Ivanov

We will add more and more checks so we need a better code structure in parallels_co_check. Let each check performs in a separate loop in a separate helper.

---
 block/parallels.c | 50 ++++++++++++++++++++++++++++++++++-------------
 1 file changed, 36 insertions(+), 14 deletions(-)
block/parallels.c +36 -14
index a35035e231..bf074f247c 100644
@@ -438,13 +438,43 @@ static void parallels_check_unclean(BlockDriverState *bs,
     }
 }
 
+static int parallels_check_outside_image(BlockDriverState *bs,
+                                         BdrvCheckResult *res,
+                                         BdrvCheckMode fix)
+{
+    BDRVParallelsState *s = bs->opaque;
+    uint32_t i;
+    int64_t off, size;
+
+    size = bdrv_getlength(bs->file->bs);
+    if (size < 0) {
+        res->check_errors++;
+        return size;
+    }
+
+    for (i = 0; i < s->bat_size; i++) {
+        off = bat2sect(s, i) << BDRV_SECTOR_BITS;
+        if (off > size) {
+            fprintf(stderr, "%s cluster %u is outside image\n",
+                    fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
+            res->corruptions++;
+            if (fix & BDRV_FIX_ERRORS) {
+                parallels_set_bat_entry(s, i, 0);
+                res->corruptions_fixed++;
+            }
+        }
+    }
+
+    return 0;
+}
+
 static int coroutine_fn parallels_co_check(BlockDriverState *bs,
                                            BdrvCheckResult *res,
                                            BdrvCheckMode fix)
 {
     BDRVParallelsState *s = bs->opaque;
     int64_t size, prev_off, high_off;
-    int ret = 0;
+    int ret;
     uint32_t i;
 
     size = bdrv_getlength(bs->file->bs);
@@ -457,6 +487,11 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
 
     parallels_check_unclean(bs, res, fix);
 
+    ret = parallels_check_outside_image(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
     res->bfi.total_clusters = s->bat_size;
     res->bfi.compressed_clusters = 0; /* compression is not supported */
 
@@ -469,19 +504,6 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
             continue;
         }
 
-        /* cluster outside the image */
-        if (off > size) {
-            fprintf(stderr, "%s cluster %u is outside image\n",
-                    fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", i);
-            res->corruptions++;
-            if (fix & BDRV_FIX_ERRORS) {
-                prev_off = 0;
-                parallels_set_bat_entry(s, i, 0);
-                res->corruptions_fixed++;
-                continue;
-            }
-        }
-
         res->bfi.allocated_clusters++;
         if (off > high_off) {
             high_off = off;
Signature · 1 line
2.34.1
#26 lore raw

PATCH v5 7/9 parallels: Move check of leaks to a separate function

In reply to Alexander Ivanov

We will add more and more checks so we need a better code structure in parallels_co_check. Let each check performs in a separate loop in a separate helper.

---
 block/parallels.c | 87 +++++++++++++++++++++++++++++------------------
 1 file changed, 54 insertions(+), 33 deletions(-)
block/parallels.c +54 -33
index bf074f247c..12e8d382f3 100644
@@ -468,14 +468,13 @@ static int parallels_check_outside_image(BlockDriverState *bs,
     return 0;
 }
 
-static int coroutine_fn parallels_co_check(BlockDriverState *bs,
-                                           BdrvCheckResult *res,
-                                           BdrvCheckMode fix)
+static int parallels_check_leak(BlockDriverState *bs,
+                                BdrvCheckResult *res,
+                                BdrvCheckMode fix)
 {
     BDRVParallelsState *s = bs->opaque;
-    int64_t size, prev_off, high_off;
-    int ret;
-    uint32_t i;
+    int64_t size, off, high_off, count;
+    int i, ret;
 
     size = bdrv_getlength(bs->file->bs);
     if (size < 0) {
@@ -483,41 +482,16 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
         return size;
     }
 
-    qemu_co_mutex_lock(&s->lock);
-
-    parallels_check_unclean(bs, res, fix);
-
-    ret = parallels_check_outside_image(bs, res, fix);
-    if (ret < 0) {
-        goto out;
-    }
-
-    res->bfi.total_clusters = s->bat_size;
-    res->bfi.compressed_clusters = 0; /* compression is not supported */
-
     high_off = 0;
-    prev_off = 0;
     for (i = 0; i < s->bat_size; i++) {
-        int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
-        if (off == 0) {
-            prev_off = 0;
-            continue;
-        }
-
-        res->bfi.allocated_clusters++;
+        off = bat2sect(s, i) << BDRV_SECTOR_BITS;
         if (off > high_off) {
             high_off = off;
         }
-
-        if (prev_off != 0 && (prev_off + s->cluster_size) != off) {
-            res->bfi.fragmented_clusters++;
-        }
-        prev_off = off;
     }
 
     res->image_end_offset = high_off + s->cluster_size;
     if (size > res->image_end_offset) {
-        int64_t count;
         count = DIV_ROUND_UP(size - res->image_end_offset, s->cluster_size);
         fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
                 fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
@@ -535,11 +509,12 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
             if (ret < 0) {
                 error_report_err(local_err);
                 res->check_errors++;
-                goto out;
+                return ret;
             }
             res->leaks_fixed += count;
         }
     }
+
     /*
      * If res->image_end_offset less than the file size,
      * a leak was fixed and we should set the new offset to s->data_end.
@@ -549,6 +524,52 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
         size = res->image_end_offset;
     }
     s->data_end = size >> BDRV_SECTOR_BITS;
+
+    return 0;
+}
+
+static int coroutine_fn parallels_co_check(BlockDriverState *bs,
+                                           BdrvCheckResult *res,
+                                           BdrvCheckMode fix)
+{
+    BDRVParallelsState *s = bs->opaque;
+    int64_t prev_off;
+    int ret;
+    uint32_t i;
+
+    qemu_co_mutex_lock(&s->lock);
+
+    parallels_check_unclean(bs, res, fix);
+
+    ret = parallels_check_outside_image(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
+    ret = parallels_check_leak(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
+    res->bfi.total_clusters = s->bat_size;
+    res->bfi.compressed_clusters = 0; /* compression is not supported */
+
+    prev_off = 0;
+    for (i = 0; i < s->bat_size; i++) {
+        int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
+        if (off == 0) {
+            prev_off = 0;
+            continue;
+        }
+
+        res->bfi.allocated_clusters++;
+
+        if (prev_off != 0 && (prev_off + s->cluster_size) != off) {
+            res->bfi.fragmented_clusters++;
+        }
+        prev_off = off;
+    }
+
 out:
     qemu_co_mutex_unlock(&s->lock);
 
Signature · 1 line
2.34.1
#27 lore raw

Re: [PATCH v5 7/9] parallels: Move check of leaks to a separate function

Quoted 20 lines · Alexander Ivanov

We will add more and more checks so we need a better code structure in parallels_co_check. Let each check performs in a separate loop in a separate helper.

---
  block/parallels.c | 87 +++++++++++++++++++++++++++++------------------
  1 file changed, 54 insertions(+), 33 deletions(-)
block/parallels.c +0 -3
index bf074f247c..12e8d382f3 100644
@@ -468,14 +468,13 @@ static int parallels_check_outside_image(BlockDriverState *bs,
      return 0;
  }
 
-static int coroutine_fn parallels_co_check(BlockDriverState *bs,
-                                           BdrvCheckResult *res,
-                                           BdrvCheckMode fix)

block/parallels.c static int parallels_check_outside_image(BlockDriverState *bs, · line 477

Quoted 10 lines
block/parallels.c +5 -3
+static int parallels_check_leak(BlockDriverState *bs,
+                                BdrvCheckResult *res,
+                                BdrvCheckMode fix)
  {
      BDRVParallelsState *s = bs->opaque;
-    int64_t size, prev_off, high_off;
-    int ret;
-    uint32_t i;
+    int64_t size, off, high_off, count;
+    int i, ret;

This is not we have agreed on. 'i' should be uint32_t You have fixed parallels_co_check, but this needs the fix too.

Quoted 113 lines
block/parallels.c +49 -27
      size = bdrv_getlength(bs->file->bs);
      if (size < 0) {
@@ -483,41 +482,16 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
          return size;
      }
 
-    qemu_co_mutex_lock(&s->lock);
-
-    parallels_check_unclean(bs, res, fix);
-
-    ret = parallels_check_outside_image(bs, res, fix);
-    if (ret < 0) {
-        goto out;
-    }
-
-    res->bfi.total_clusters = s->bat_size;
-    res->bfi.compressed_clusters = 0; /* compression is not supported */
-
      high_off = 0;
-    prev_off = 0;
      for (i = 0; i < s->bat_size; i++) {
-        int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
-        if (off == 0) {
-            prev_off = 0;
-            continue;
-        }
-
-        res->bfi.allocated_clusters++;
+        off = bat2sect(s, i) << BDRV_SECTOR_BITS;
          if (off > high_off) {
              high_off = off;
          }
-
-        if (prev_off != 0 && (prev_off + s->cluster_size) != off) {
-            res->bfi.fragmented_clusters++;
-        }
-        prev_off = off;
      }
 
      res->image_end_offset = high_off + s->cluster_size;
      if (size > res->image_end_offset) {
-        int64_t count;
          count = DIV_ROUND_UP(size - res->image_end_offset, s->cluster_size);
          fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n",
                  fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",
@@ -535,11 +509,12 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
              if (ret < 0) {
                  error_report_err(local_err);
                  res->check_errors++;
-                goto out;
+                return ret;
              }
              res->leaks_fixed += count;
          }
      }
+
      /*
       * If res->image_end_offset less than the file size,
       * a leak was fixed and we should set the new offset to s->data_end.
@@ -549,6 +524,52 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
          size = res->image_end_offset;
      }
      s->data_end = size >> BDRV_SECTOR_BITS;
+
+    return 0;
+}
+
+static int coroutine_fn parallels_co_check(BlockDriverState *bs,
+                                           BdrvCheckResult *res,
+                                           BdrvCheckMode fix)
+{
+    BDRVParallelsState *s = bs->opaque;
+    int64_t prev_off;
+    int ret;
+    uint32_t i;
+
+    qemu_co_mutex_lock(&s->lock);
+
+    parallels_check_unclean(bs, res, fix);
+
+    ret = parallels_check_outside_image(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
+    ret = parallels_check_leak(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
+    res->bfi.total_clusters = s->bat_size;
+    res->bfi.compressed_clusters = 0; /* compression is not supported */
+
+    prev_off = 0;
+    for (i = 0; i < s->bat_size; i++) {
+        int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
+        if (off == 0) {
+            prev_off = 0;
+            continue;
+        }
+
+        res->bfi.allocated_clusters++;
+
+        if (prev_off != 0 && (prev_off + s->cluster_size) != off) {
+            res->bfi.fragmented_clusters++;
+        }
+        prev_off = off;
+    }
+
  out:
      qemu_co_mutex_unlock(&s->lock);
 
#28 lore raw

Re: [PATCH v5 7/9] parallels: Move check of leaks to a separate function

Quoted 26 lines · Denis V. Lunev
Quoted 25 lines · Alexander Ivanov

We will add more and more checks so we need a better code structure in parallels_co_check. Let each check performs in a separate loop in a separate helper.

---
  block/parallels.c | 87 +++++++++++++++++++++++++++++------------------

  1 file changed, 54 insertions(+), 33 deletions(-)
block/parallels.c +3 -2
index bf074f247c..12e8d382f3 100644
@@ -468,14 +468,13 @@ static int 
 parallels_check_outside_image(BlockDriverState *bs,
       return 0;
   }
   -static int coroutine_fn parallels_co_check(BlockDriverState *bs,
-                                           BdrvCheckResult *res,
-                                           BdrvCheckMode fix)
+static int parallels_check_leak(BlockDriverState *bs,
+                                BdrvCheckResult *res,
+                                BdrvCheckMode fix)
   {
       BDRVParallelsState *s = bs->opaque;
Quoted 8 lines

block/parallels.c static int parallels_check_outside_image(BlockDriverState *bs, · line 477

Quoted 5 lines
block/parallels.c +2 -3
-    int64_t size, prev_off, high_off;
-    int ret;
-    uint32_t i;
+    int64_t size, off, high_off, count;
+    int i, ret;

This is not we have agreed on. 'i' should be uint32_t You have fixed parallels_co_check, but this needs the fix too.

Damn! Missed it when rebased on changed patches.

Quoted 116 lines
Quoted 115 lines
block/parallels.c +0 -10
         size = bdrv_getlength(bs->file->bs);
       if (size < 0) {
@@ -483,41 +482,16 @@ static int coroutine_fn 
 parallels_co_check(BlockDriverState *bs,
           return size;
       }
   -    qemu_co_mutex_lock(&s->lock);
-
-    parallels_check_unclean(bs, res, fix);
-
-    ret = parallels_check_outside_image(bs, res, fix);
-    if (ret < 0) {
-        goto out;
-    }
-
-    res->bfi.total_clusters = s->bat_size;
-    res->bfi.compressed_clusters = 0; /* compression is not 

supported */
-
      high_off = 0;
-    prev_off = 0;
      for (i = 0; i < s->bat_size; i++) {
-        int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
-        if (off == 0) {
-            prev_off = 0;
-            continue;
-        }
-
-        res->bfi.allocated_clusters++;
+        off = bat2sect(s, i) << BDRV_SECTOR_BITS;
          if (off > high_off) {
              high_off = off;
          }
-
-        if (prev_off != 0 && (prev_off + s->cluster_size) != off) { -            res->bfi.fragmented_clusters++;
-        }
-        prev_off = off;
      }
        res->image_end_offset = high_off + s->cluster_size;
      if (size > res->image_end_offset) {
-        int64_t count;
          count = DIV_ROUND_UP(size - res->image_end_offset, s->cluster_size); fprintf(stderr, "%s space leaked at the end of the image %" PRId64 "\n", fix & BDRV_FIX_LEAKS ? "Repairing" : "ERROR",

block/parallels.c +2 -1
@@ -535,11 +509,12 @@ static int coroutine_fn 
 parallels_co_check(BlockDriverState *bs,
               if (ret < 0) {
                   error_report_err(local_err);
                   res->check_errors++;
-                goto out;
+                return ret;
               }
               res->leaks_fixed += count;
           }
       }
+
       /*
        * If res->image_end_offset less than the file size,

       * a leak was fixed and we should set the new offset to s->data_end.

block/parallels.c +29 -0
@@ -549,6 +524,52 @@ static int coroutine_fn 
 parallels_co_check(BlockDriverState *bs,
           size = res->image_end_offset;
       }
       s->data_end = size >> BDRV_SECTOR_BITS;
+
+    return 0;
+}
+
+static int coroutine_fn parallels_co_check(BlockDriverState *bs,
+                                           BdrvCheckResult *res,
+                                           BdrvCheckMode fix)
+{
+    BDRVParallelsState *s = bs->opaque;
+    int64_t prev_off;
+    int ret;
+    uint32_t i;
+
+    qemu_co_mutex_lock(&s->lock);
+
+    parallels_check_unclean(bs, res, fix);
+
+    ret = parallels_check_outside_image(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
+    ret = parallels_check_leak(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
+    res->bfi.total_clusters = s->bat_size;
+    res->bfi.compressed_clusters = 0; /* compression is not 

supported */

block/parallels.c +17 -0
+
+    prev_off = 0;
+    for (i = 0; i < s->bat_size; i++) {
+        int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
+        if (off == 0) {
+            prev_off = 0;
+            continue;
+        }
+
+        res->bfi.allocated_clusters++;
+
+        if (prev_off != 0 && (prev_off + s->cluster_size) != off) {
+            res->bfi.fragmented_clusters++;
+        }
+        prev_off = off;
+    }
+

  out:
      qemu_co_mutex_unlock(&s->lock);

#29 lore raw

PATCH v5 8/9 parallels: Move statistic collection to a separate function

In reply to Alexander Ivanov

We will add more and more checks so we need a better code structure in parallels_co_check. Let each check performs in a separate loop in a separate helper.

---
 block/parallels.c | 53 +++++++++++++++++++++++++++--------------------
 1 file changed, 31 insertions(+), 22 deletions(-)
block/parallels.c +31 -22
index 12e8d382f3..0fe6dd3237 100644
@@ -528,47 +528,56 @@ static int parallels_check_leak(BlockDriverState *bs,
     return 0;
 }
 
-static int coroutine_fn parallels_co_check(BlockDriverState *bs,
-                                           BdrvCheckResult *res,
-                                           BdrvCheckMode fix)
+static void parallels_collect_statistics(BlockDriverState *bs,
+                                         BdrvCheckResult *res,
+                                         BdrvCheckMode fix)
 {
     BDRVParallelsState *s = bs->opaque;
-    int64_t prev_off;
-    int ret;
+    int64_t off, prev_off;
     uint32_t i;
 
-    qemu_co_mutex_lock(&s->lock);
-
-    parallels_check_unclean(bs, res, fix);
-
-    ret = parallels_check_outside_image(bs, res, fix);
-    if (ret < 0) {
-        goto out;
-    }
-
-    ret = parallels_check_leak(bs, res, fix);
-    if (ret < 0) {
-        goto out;
-    }
-
     res->bfi.total_clusters = s->bat_size;
     res->bfi.compressed_clusters = 0; /* compression is not supported */
 
     prev_off = 0;
     for (i = 0; i < s->bat_size; i++) {
-        int64_t off = bat2sect(s, i) << BDRV_SECTOR_BITS;
+        off = bat2sect(s, i) << BDRV_SECTOR_BITS;
         if (off == 0) {
             prev_off = 0;
             continue;
         }
 
-        res->bfi.allocated_clusters++;
-
         if (prev_off != 0 && (prev_off + s->cluster_size) != off) {
             res->bfi.fragmented_clusters++;
         }
+
         prev_off = off;
+        res->bfi.allocated_clusters++;
     }
+}
+
+static int coroutine_fn parallels_co_check(BlockDriverState *bs,
+                                           BdrvCheckResult *res,
+                                           BdrvCheckMode fix)
+{
+    BDRVParallelsState *s = bs->opaque;
+    int ret;
+
+    qemu_co_mutex_lock(&s->lock);
+
+    parallels_check_unclean(bs, res, fix);
+
+    ret = parallels_check_outside_image(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
+    ret = parallels_check_leak(bs, res, fix);
+    if (ret < 0) {
+        goto out;
+    }
+
+    parallels_collect_statistics(bs, res, fix);
 
 out:
     qemu_co_mutex_unlock(&s->lock);
Signature · 1 line
2.34.1
#30 lore raw

PATCH v5 9/9 parallels: Replace qemu_co_mutex_lock by WITH_QEMU_LOCK_GUARD

In reply to Alexander Ivanov

Replace the way we use mutex in parallels_co_check() for simplier and less error prone code.

---
 block/parallels.c | 33 ++++++++++++++-------------------
 1 file changed, 14 insertions(+), 19 deletions(-)
block/parallels.c +14 -19
index 0fe6dd3237..47ef25b193 100644
@@ -563,30 +563,25 @@ static int coroutine_fn parallels_co_check(BlockDriverState *bs,
     BDRVParallelsState *s = bs->opaque;
     int ret;
 
-    qemu_co_mutex_lock(&s->lock);
+    WITH_QEMU_LOCK_GUARD(&s->lock) {
+        parallels_check_unclean(bs, res, fix);
 
-    parallels_check_unclean(bs, res, fix);
+        ret = parallels_check_outside_image(bs, res, fix);
+        if (ret < 0) {
+            return ret;
+        }
 
-    ret = parallels_check_outside_image(bs, res, fix);
-    if (ret < 0) {
-        goto out;
-    }
+        ret = parallels_check_leak(bs, res, fix);
+        if (ret < 0) {
+            return ret;
+        }
 
-    ret = parallels_check_leak(bs, res, fix);
-    if (ret < 0) {
-        goto out;
+        parallels_collect_statistics(bs, res, fix);
     }
 
-    parallels_collect_statistics(bs, res, fix);
-
-out:
-    qemu_co_mutex_unlock(&s->lock);
-
-    if (ret == 0) {
-        ret = bdrv_co_flush(bs);
-        if (ret < 0) {
-            res->check_errors++;
-        }
+    ret = bdrv_co_flush(bs);
+    if (ret < 0) {
+        res->check_errors++;
     }
 
     return ret;
Signature · 1 line
2.34.1