Skip to contentSkip to conversation

block/parallels: 2TB+ parallels images support

PATCH Superseded Denis V. Lunev 3 participants 14 messages Jul 22, 2014 – Jul 25, 2014 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover block/parallels: 2TB+ parallels images support

Parallels has released in the recent updates of Parallels Server 5/6 new addition to his image format. Images with signature WithouFreSpacExt have offsets in the catalog coded not as offsets in sectors (multiple of 512 bytes) but offsets coded in blocks (i.e. header->tracks * 512)

In this case to code the virtual disk size for such images nb_sectors field is extended to 64 bits. The reader of older images with signature WithoutFreeSpace must manually zero most valuable bits of nb_sectors on open.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
#1 lore raw

PATCH 1/4 block/parallels: extend parallels format header with actual data values

Parallels image format has several additional fields inside:
- nb_sectors is actually 64 bit wide. Upper 32bits are not used for images with signature "WithoutFreeSpace" and must be explicitely zeroed according to Parallels. They will be used for images with signature "WithouFreSpacExt"
- inuse is magic which means that the image is currently opened for read/write or was not closed correctly, the magic is 0x746f6e59
- data_off is the location of the first data block. It can be zero and in this case

This patch adds these values to struct parallels_header and adds proper handling of nb_sectors for currently supported WithoutFreeSpace images.

WithouFreSpacExt will be covered in the next patch.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
block/parallels.c +5 -3
index 1a5bd35..c44df87 100644
@@ -41,8 +41,10 @@ struct parallels_header {
     uint32_t cylinders;
     uint32_t tracks;
     uint32_t catalog_entries;
-    uint32_t nb_sectors;
-    char padding[24];
+    uint64_t nb_sectors;
+    uint32_t inuse;
+    uint32_t data_off;
+    char padding[12];
 } QEMU_PACKED;
 
 typedef struct BDRVParallelsState {
@@ -90,7 +92,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
-    bs->total_sectors = le32_to_cpu(ph.nb_sectors);
+    bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
 
     s->tracks = le32_to_cpu(ph.tracks);
     if (s->tracks == 0) {
Signature · 1 line
1.9.1
#2 lore raw

Re: [Qemu-devel] [PATCH 1/4] block/parallels: extend parallels format header with actual data values

Quoted 3 lines · Denis V. Lunev

Parallels image format has several additional fields inside:
- nb_sectors is actually 64 bit wide. Upper 32bits are not used for images with signature "WithoutFreeSpace" and must be explicitely

s/explicitely/explicitly

Quoted 6 lines

zeroed according to Parallels. They will be used for images with signature "WithouFreSpacExt"
- inuse is magic which means that the image is currently opened for read/write or was not closed correctly, the magic is 0x746f6e59
- data_off is the location of the first data block. It can be zero and in this case

I think you may have forgotten to finish this sentence :)

Quoted 32 lines

This patch adds these values to struct parallels_header and adds proper handling of nb_sectors for currently supported WithoutFreeSpace images.

WithouFreSpacExt will be covered in the next patch.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
block/parallels.c +4 -2
index 1a5bd35..c44df87 100644
@@ -41,8 +41,10 @@ struct parallels_header {
     uint32_t cylinders;
     uint32_t tracks;
     uint32_t catalog_entries;
-    uint32_t nb_sectors;
-    char padding[24];
+    uint64_t nb_sectors;
+    uint32_t inuse;
+    uint32_t data_off;
+    char padding[12];
 } QEMU_PACKED;
 
 typedef struct BDRVParallelsState {

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

Quoted 5 lines
block/parallels.c +1 -1
@@ -90,7 +92,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
-    bs->total_sectors = le32_to_cpu(ph.nb_sectors);
+    bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);

I think an explicit bit mask on the upper 32 bits would fit better here than a cast, especially since neither 'bs->total_sectors' nor 'ph.nb_sectors' is a uint32_t. E.g.:

    bs->total_sectors = 0xffffffff & le64_to_cpu(ph.nb_sectors);
Quoted 7 lines
block/parallels.c +0 -0
     s->tracks = le32_to_cpu(ph.tracks);
     if (s->tracks == 0) {
Signature · 1 line
1.9.1
#3 lore raw

Re: [Qemu-devel] [PATCH 1/4] block/parallels: extend parallels format header with actual data values

Quoted 6 lines · Jeff Cody
Quoted 3 lines · Denis V. Lunev

Parallels image format has several additional fields inside:
- nb_sectors is actually 64 bit wide. Upper 32bits are not used for images with signature "WithoutFreeSpace" and must be explicitely

s/explicitely/explicitly

Quoted 7 lines
Quoted 6 lines

zeroed according to Parallels. They will be used for images with signature "WithouFreSpacExt"
- inuse is magic which means that the image is currently opened for read/write or was not closed correctly, the magic is 0x746f6e59
- data_off is the location of the first data block. It can be zero and in this case

I think you may have forgotten to finish this sentence :)

ok

Quoted 35 lines
Quoted 35 lines

This patch adds these values to struct parallels_header and adds proper handling of nb_sectors for currently supported WithoutFreeSpace images.

WithouFreSpacExt will be covered in the next patch.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
  block/parallels.c | 8 +++++---
  1 file changed, 5 insertions(+), 3 deletions(-)
block/parallels.c +4 -2
index 1a5bd35..c44df87 100644
@@ -41,8 +41,10 @@ struct parallels_header {
      uint32_t cylinders;
      uint32_t tracks;
      uint32_t catalog_entries;
-    uint32_t nb_sectors;
-    char padding[24];
+    uint64_t nb_sectors;
+    uint32_t inuse;
+    uint32_t data_off;
+    char padding[12];
  } QEMU_PACKED;
 
  typedef struct BDRVParallelsState {
@@ -90,7 +92,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
          goto fail;
      }
 
Quoted 8 lines

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

Quoted 2 lines
block/parallels.c +1 -1
-    bs->total_sectors = le32_to_cpu(ph.nb_sectors);
+    bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);

I think an explicit bit mask on the upper 32 bits would fit better here than a cast, especially since neither 'bs->total_sectors' nor 'ph.nb_sectors' is a uint32_t. E.g.:

     bs->total_sectors = 0xffffffff & le64_to_cpu(ph.nb_sectors);

ok, will do

#4 lore raw

PATCH 2/4 block/parallels: replace tabs with spaces in block/parallels.c

In reply to Denis V. Lunev
CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
block/parallels.c +5 -5
index c44df87..8f9ec8a 100644
@@ -61,11 +61,11 @@ static int parallels_probe(const uint8_t *buf, int buf_size, const char *filenam
     const struct parallels_header *ph = (const void *)buf;
 
     if (buf_size < HEADER_SIZE)
-	return 0;
+        return 0;
 
     if (!memcmp(ph->magic, HEADER_MAGIC, 16) &&
-	(le32_to_cpu(ph->version) == HEADER_VERSION))
-	return 100;
+        (le32_to_cpu(ph->version) == HEADER_VERSION))
+        return 100;
 
     return 0;
 }
@@ -115,7 +115,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
     }
 
     for (i = 0; i < s->catalog_size; i++)
-	le32_to_cpus(&s->catalog_bitmap[i]);
+        le32_to_cpus(&s->catalog_bitmap[i]);
 
     qemu_co_mutex_init(&s->lock);
     return 0;
@@ -135,7 +135,7 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 
     /* not allocated */
     if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
-	return -1;
+        return -1;
     return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
 }
 
Signature · 1 line
1.9.1
Reviewed-by on v1 2/4
#5 lore raw

Re: [Qemu-devel] [PATCH 2/4] block/parallels: replace tabs with spaces in block/parallels.c

Quoted 48 lines · Denis V. Lunev
CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
block/parallels.c +5 -5
index c44df87..8f9ec8a 100644
@@ -61,11 +61,11 @@ static int parallels_probe(const uint8_t *buf, int buf_size, const char *filenam
     const struct parallels_header *ph = (const void *)buf;
 
     if (buf_size < HEADER_SIZE)
-	return 0;
+        return 0;
 
     if (!memcmp(ph->magic, HEADER_MAGIC, 16) &&
-	(le32_to_cpu(ph->version) == HEADER_VERSION))
-	return 100;
+        (le32_to_cpu(ph->version) == HEADER_VERSION))
+        return 100;
 
     return 0;
 }
@@ -115,7 +115,7 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
     }
 
     for (i = 0; i < s->catalog_size; i++)
-	le32_to_cpus(&s->catalog_bitmap[i]);
+        le32_to_cpus(&s->catalog_bitmap[i]);
 
     qemu_co_mutex_init(&s->lock);
     return 0;
@@ -135,7 +135,7 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
 
     /* not allocated */
     if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
-	return -1;
+        return -1;
     return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
 }
 
Signature · 1 line
1.9.1
#6 lore raw

PATCH 3/4 block/parallels: split check for parallels format in parallels_open

In reply to Denis V. Lunev

and rework error path a bit. There is no difference at the moment, but the code will be definitely shorter when additional processing will be required for WithouFreSpacExt

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
block/parallels.c +7 -6
index 8f9ec8a..02739cf 100644
@@ -85,12 +85,10 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
-    if (memcmp(ph.magic, HEADER_MAGIC, 16) ||
-        (le32_to_cpu(ph.version) != HEADER_VERSION)) {
-        error_setg(errp, "Image not in Parallels format");
-        ret = -EINVAL;
-        goto fail;
-    }
+    if (le32_to_cpu(ph.version) != HEADER_VERSION)
+        goto fail_format;
+    if (memcmp(ph.magic, HEADER_MAGIC, 16))
+        goto fail_format;
 
     bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
 
@@ -120,6 +118,9 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
     qemu_co_mutex_init(&s->lock);
     return 0;
 
+fail_format:
+    error_setg(errp, "Image not in Parallels format");
+    ret = -EINVAL;
 fail:
     g_free(s->catalog_bitmap);
     return ret;
Signature · 1 line
1.9.1
#7 lore raw

Re: [Qemu-devel] [PATCH 3/4] block/parallels: split check for parallels format in parallels_open

Quoted 19 lines · Denis V. Lunev

and rework error path a bit. There is no difference at the moment, but the code will be definitely shorter when additional processing will be required for WithouFreSpacExt

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
block/parallels.c +0 -0
index 8f9ec8a..02739cf 100644
@@ -85,12 +85,10 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 

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

Quoted 10 lines
block/parallels.c +4 -6
-    if (memcmp(ph.magic, HEADER_MAGIC, 16) ||
-        (le32_to_cpu(ph.version) != HEADER_VERSION)) {
-        error_setg(errp, "Image not in Parallels format");
-        ret = -EINVAL;
-        goto fail;
-    }
+    if (le32_to_cpu(ph.version) != HEADER_VERSION)
+        goto fail_format;
+    if (memcmp(ph.magic, HEADER_MAGIC, 16))
+        goto fail_format;

QEMU coding style dictates these statements have curly braces, even though they are just one liners. (If you run your patches against scripts/checkpatch.pl, it should catch most style issues).

I think this patch could also just be squashed into patch 4, if desired.

Quoted 17 lines
block/parallels.c +3 -0
     bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
 
@@ -120,6 +118,9 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
     qemu_co_mutex_init(&s->lock);
     return 0;
 
+fail_format:
+    error_setg(errp, "Image not in Parallels format");
+    ret = -EINVAL;
 fail:
     g_free(s->catalog_bitmap);
     return ret;
Signature · 1 line
1.9.1
#8 lore raw

Re: [Qemu-devel] [PATCH 3/4] block/parallels: split check for parallels format in parallels_open

Quoted 20 lines · Jeff Cody
Quoted 19 lines · Denis V. Lunev

and rework error path a bit. There is no difference at the moment, but the code will be definitely shorter when additional processing will be required for WithouFreSpacExt

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
  block/parallels.c | 13 +++++++------
  1 file changed, 7 insertions(+), 6 deletions(-)
block/parallels.c +0 -0
index 8f9ec8a..02739cf 100644
@@ -85,12 +85,10 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
          goto fail;
      }
 
Quoted 13 lines

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

Quoted 10 lines
block/parallels.c +4 -6
-    if (memcmp(ph.magic, HEADER_MAGIC, 16) ||
-        (le32_to_cpu(ph.version) != HEADER_VERSION)) {
-        error_setg(errp, "Image not in Parallels format");
-        ret = -EINVAL;
-        goto fail;
-    }
+    if (le32_to_cpu(ph.version) != HEADER_VERSION)
+        goto fail_format;
+    if (memcmp(ph.magic, HEADER_MAGIC, 16))
+        goto fail_format;

QEMU coding style dictates these statements have curly braces, even though they are just one liners. (If you run your patches against scripts/checkpatch.pl, it should catch most style issues).

ok, I just used a kernel convention. Will redo this here and in the next path. This is not a problem :) Thank you for pointing this out.

Quoted 2 lines

I think this patch could also just be squashed into patch 4, if desired.

I'd prefer not to do this to have behavior changing and behavior non-changing stuff separated.

Quoted 17 lines
Quoted 17 lines
block/parallels.c +3 -0
      bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
 
@@ -120,6 +118,9 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
      qemu_co_mutex_init(&s->lock);
      return 0;
 
+fail_format:
+    error_setg(errp, "Image not in Parallels format");
+    ret = -EINVAL;
  fail:
      g_free(s->catalog_bitmap);
      return ret;
Signature · 1 line
1.9.1
#9 lore raw

PATCH 4/4 block/parallels: 2TB+ parallels images support

In reply to Denis V. Lunev

Parallels has released in the recent updates of Parallels Server 5/6 new addition to his image format. Images with signature WithouFreSpacExt have offsets in the catalog coded not as offsets in sectors (multiple of 512 bytes) but offsets coded in blocks (i.e. header->tracks * 512)

In this case all 64 bits of header->nb_sectors are used for image size.

This patch implements support of this for qemu-img.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
block/parallels.c +15 -5
index 02739cf..d9cb04f 100644
@@ -30,6 +30,7 @@
 /**************************************************************/
 
 #define HEADER_MAGIC "WithoutFreeSpace"
+#define HEADER_MAGIC2 "WithouFreSpacExt"
 #define HEADER_VERSION 2
 #define HEADER_SIZE 64
 
@@ -54,6 +55,8 @@ typedef struct BDRVParallelsState {
     unsigned int catalog_size;
 
     unsigned int tracks;
+
+    unsigned int off_multiplier;
 } BDRVParallelsState;
 
 static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename)
@@ -63,7 +66,8 @@ static int parallels_probe(const uint8_t *buf, int buf_size, const char *filenam
     if (buf_size < HEADER_SIZE)
         return 0;
 
-    if (!memcmp(ph->magic, HEADER_MAGIC, 16) &&
+    if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
+        !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
         (le32_to_cpu(ph->version) == HEADER_VERSION))
         return 100;
 
@@ -85,13 +89,18 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
+    bs->total_sectors = le64_to_cpu(ph.nb_sectors);
+
     if (le32_to_cpu(ph.version) != HEADER_VERSION)
         goto fail_format;
-    if (memcmp(ph.magic, HEADER_MAGIC, 16))
+    if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
+        s->off_multiplier = 1;
+        bs->total_sectors = (uint32_t)bs->total_sectors;
+    } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16))
+        s->off_multiplier = le32_to_cpu(ph.tracks);
+    else
         goto fail_format;
 
-    bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
-
     s->tracks = le32_to_cpu(ph.tracks);
     if (s->tracks == 0) {
         error_setg(errp, "Invalid image: Zero sectors per track");
@@ -137,7 +146,8 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
     /* not allocated */
     if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
         return -1;
-    return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
+    return
+        ((uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset) * 512;
 }
 
 static int parallels_read(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.9.1
#10 lore raw
Quoted 48 lines · Denis V. Lunev

Parallels has released in the recent updates of Parallels Server 5/6 new addition to his image format. Images with signature WithouFreSpacExt have offsets in the catalog coded not as offsets in sectors (multiple of 512 bytes) but offsets coded in blocks (i.e. header->tracks * 512)

In this case all 64 bits of header->nb_sectors are used for image size.

This patch implements support of this for qemu-img.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
block/parallels.c +5 -1
index 02739cf..d9cb04f 100644
@@ -30,6 +30,7 @@
 /**************************************************************/
 
 #define HEADER_MAGIC "WithoutFreeSpace"
+#define HEADER_MAGIC2 "WithouFreSpacExt"
 #define HEADER_VERSION 2
 #define HEADER_SIZE 64
 
@@ -54,6 +55,8 @@ typedef struct BDRVParallelsState {
     unsigned int catalog_size;
 
     unsigned int tracks;
+
+    unsigned int off_multiplier;
 } BDRVParallelsState;
 
 static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename)
@@ -63,7 +66,8 @@ static int parallels_probe(const uint8_t *buf, int buf_size, const char *filenam
     if (buf_size < HEADER_SIZE)
         return 0;
 
-    if (!memcmp(ph->magic, HEADER_MAGIC, 16) &&
+    if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
+        !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
         (le32_to_cpu(ph->version) == HEADER_VERSION))
         return 100;
 

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

Quoted 5 lines
block/parallels.c +2 -0
@@ -85,13 +89,18 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
+    bs->total_sectors = le64_to_cpu(ph.nb_sectors);
+

bs->total_sectors is a int64_t, and ph.nb_sectors is a uint64_t. Should we do a sanity check here on the max number of sectors?

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

Quoted 6 lines
block/parallels.c +3 -1
     if (le32_to_cpu(ph.version) != HEADER_VERSION)
         goto fail_format;
-    if (memcmp(ph.magic, HEADER_MAGIC, 16))
+    if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
+        s->off_multiplier = 1;
+        bs->total_sectors = (uint32_t)bs->total_sectors;

(same comment as in patch 1, w.r.t. cast vs. bitmask)

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

Quoted 2 lines
block/parallels.c +2 -0
+    } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16))
+        s->off_multiplier = le32_to_cpu(ph.tracks);

Is there a maximum size in the specification for ph.tracks?

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

Quoted 2 lines
block/parallels.c +1 -0
+    else
         goto fail_format;

(same comment as the last patch, w.r.t. braces)

block/parallels.c static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num) · line 150

Quoted 13 lines
block/parallels.c +2 -3
-    bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
-
     s->tracks = le32_to_cpu(ph.tracks);
     if (s->tracks == 0) {
         error_setg(errp, "Invalid image: Zero sectors per track");
@@ -137,7 +146,8 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
     /* not allocated */
     if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
         return -1;
-    return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
+    return
+        ((uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset) * 512;

Do we need to worry about overflow here, depending on the value of off_multiplier?

Also, (and this existed prior to this patch), - we are casting to uint64_t, although the function returns int64_t.

Quoted 7 lines
block/parallels.c +0 -0
 }
 
 static int parallels_read(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.9.1
#11 lore raw
Quoted 48 lines · Jeff Cody
Quoted 47 lines · Denis V. Lunev

Parallels has released in the recent updates of Parallels Server 5/6 new addition to his image format. Images with signature WithouFreSpacExt have offsets in the catalog coded not as offsets in sectors (multiple of 512 bytes) but offsets coded in blocks (i.e. header->tracks * 512)

In this case all 64 bits of header->nb_sectors are used for image size.

This patch implements support of this for qemu-img.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
  block/parallels.c | 20 +++++++++++++++-----
  1 file changed, 15 insertions(+), 5 deletions(-)
block/parallels.c +5 -1
index 02739cf..d9cb04f 100644
@@ -30,6 +30,7 @@
  /**************************************************************/
 
  #define HEADER_MAGIC "WithoutFreeSpace"
+#define HEADER_MAGIC2 "WithouFreSpacExt"
  #define HEADER_VERSION 2
  #define HEADER_SIZE 64
 
@@ -54,6 +55,8 @@ typedef struct BDRVParallelsState {
      unsigned int catalog_size;
 
      unsigned int tracks;
+
+    unsigned int off_multiplier;
  } BDRVParallelsState;
 
  static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename)
@@ -63,7 +66,8 @@ static int parallels_probe(const uint8_t *buf, int buf_size, const char *filenam
      if (buf_size < HEADER_SIZE)
          return 0;
 
-    if (!memcmp(ph->magic, HEADER_MAGIC, 16) &&
+    if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
+        !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
          (le32_to_cpu(ph->version) == HEADER_VERSION))
          return 100;
 
Quoted 8 lines

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

Quoted 6 lines
block/parallels.c +2 -0
@@ -85,13 +89,18 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
          goto fail;
      }
 
+    bs->total_sectors = le64_to_cpu(ph.nb_sectors);
+

bs->total_sectors is a int64_t, and ph.nb_sectors is a uint64_t. Should we do a sanity check here on the max number of sectors?

in reality such image will not be usable as we will later have to multiply this count with 512 to calculate offset in the file

Quoted 7 lines

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

Quoted 6 lines
block/parallels.c +3 -1
      if (le32_to_cpu(ph.version) != HEADER_VERSION)
          goto fail_format;
-    if (memcmp(ph.magic, HEADER_MAGIC, 16))
+    if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
+        s->off_multiplier = 1;
+        bs->total_sectors = (uint32_t)bs->total_sectors;

(same comment as in patch 1, w.r.t. cast vs. bitmask)

ok

Quoted 3 lines

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

Quoted 2 lines
block/parallels.c +2 -0
+    } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16))
+        s->off_multiplier = le32_to_cpu(ph.tracks);

Is there a maximum size in the specification for ph.tracks?

no, there is no limitation. Though in reality I have seen the following images only:
252 sectors, 63 sectors, 256 sectors, 2048 sectors
and only 2048 was used with this new header.

This is just an observation.

Quoted 20 lines

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

Quoted 2 lines
block/parallels.c +1 -0
+    else
          goto fail_format;

(same comment as the last patch, w.r.t. braces)

block/parallels.c static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num) · line 150

Quoted 13 lines
block/parallels.c +2 -3
-    bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
-
      s->tracks = le32_to_cpu(ph.tracks);
      if (s->tracks == 0) {
          error_setg(errp, "Invalid image: Zero sectors per track");
@@ -137,7 +146,8 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
      /* not allocated */
      if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
          return -1;
-    return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
+    return
+        ((uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset) * 512;

Do we need to worry about overflow here, depending on the value of off_multiplier?

Quoted 2 lines

Also, (and this existed prior to this patch), - we are casting to uint64_t, although the function returns int64_t.

good catch. I'll change the declaration to uint64_t and will return 0 from previous if aka

if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))

It is not possible to obtain 0 as a real offset of any sector as this is an offset of the header.

Regarding overflow check. Do you think that we should return error to the upper layer or we could silently fill result data with zeroes as was done originally for the following case 'index > s->catalog_size' ?

Quoted 7 lines
Quoted 7 lines
block/parallels.c +0 -0
  }
 
  static int parallels_read(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.9.1
#12 lore raw
Quoted 89 lines · Denis V. Lunev
Quoted 41 lines · Jeff Cody
Quoted 40 lines · Denis V. Lunev

Parallels has released in the recent updates of Parallels Server 5/6 new addition to his image format. Images with signature WithouFreSpacExt have offsets in the catalog coded not as offsets in sectors (multiple of 512 bytes) but offsets coded in blocks (i.e. header->tracks * 512)

In this case all 64 bits of header->nb_sectors are used for image size.

This patch implements support of this for qemu-img.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
 block/parallels.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
block/parallels.c +5 -1
index 02739cf..d9cb04f 100644
@@ -30,6 +30,7 @@
 /**************************************************************/
 #define HEADER_MAGIC "WithoutFreeSpace"
+#define HEADER_MAGIC2 "WithouFreSpacExt"
 #define HEADER_VERSION 2
 #define HEADER_SIZE 64
@@ -54,6 +55,8 @@ typedef struct BDRVParallelsState {
     unsigned int catalog_size;
     unsigned int tracks;
+
+    unsigned int off_multiplier;
 } BDRVParallelsState;
 static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename)
@@ -63,7 +66,8 @@ static int parallels_probe(const uint8_t *buf, int buf_size, const char *filenam
     if (buf_size < HEADER_SIZE)
         return 0;
-    if (!memcmp(ph->magic, HEADER_MAGIC, 16) &&
+    if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
+        !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
         (le32_to_cpu(ph->version) == HEADER_VERSION))
Quoted 8 lines

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

Quoted 6 lines
block/parallels.c +2 -0
         return 100;
@@ -85,13 +89,18 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
+    bs->total_sectors = le64_to_cpu(ph.nb_sectors);
+

bs->total_sectors is a int64_t, and ph.nb_sectors is a uint64_t. Should we do a sanity check here on the max number of sectors?

in reality such image will not be usable as we will later have to multiply this count with 512 to calculate offset in the file

Quoted 7 lines

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

Quoted 6 lines
block/parallels.c +3 -1
     if (le32_to_cpu(ph.version) != HEADER_VERSION)
         goto fail_format;
-    if (memcmp(ph.magic, HEADER_MAGIC, 16))
+    if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
+        s->off_multiplier = 1;
+        bs->total_sectors = (uint32_t)bs->total_sectors;

(same comment as in patch 1, w.r.t. cast vs. bitmask)

ok

Quoted 3 lines

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

Quoted 2 lines
block/parallels.c +2 -0
+    } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16))
+        s->off_multiplier = le32_to_cpu(ph.tracks);

Is there a maximum size in the specification for ph.tracks?

no, there is no limitation. Though in reality I have seen the following images only:
252 sectors, 63 sectors, 256 sectors, 2048 sectors
and only 2048 was used with this new header.

This is just an observation.

Quoted 19 lines

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

Quoted 2 lines
block/parallels.c +1 -0
+    else
         goto fail_format;

(same comment as the last patch, w.r.t. braces)

block/parallels.c static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num) · line 150

Quoted 12 lines
block/parallels.c +2 -3
-    bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
-
     s->tracks = le32_to_cpu(ph.tracks);
     if (s->tracks == 0) {
         error_setg(errp, "Invalid image: Zero sectors per track");
@@ -137,7 +146,8 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
     /* not allocated */
     if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
         return -1;
-    return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
+    return
+        ((uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset) * 512;

Do we need to worry about overflow here, depending on the value of off_multiplier?

Quoted 5 lines
Quoted 2 lines

Also, (and this existed prior to this patch), - we are casting to uint64_t, although the function returns int64_t.

good catch. I'll change the declaration to uint64_t and will return 0 from previous if aka

Thanks.

I'm not sure that is the best option though - the only place the return value from this function is used is in parallels_read(), which passes the output into bdrv_pread() as the offset. The offset parameter for bdrv_pread/pwrite is an int64_t, not a uint64_t.

Quoted 9 lines

if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))

It is not possible to obtain 0 as a real offset of any sector as this is an offset of the header.

Regarding overflow check. Do you think that we should return error to the upper layer or we could silently fill result data with zeroes as was done originally for the following case 'index > s->catalog_size' ?

I think it would be best to return error (anything < 0 will also cause bdrv_pread to return -EIO, and it is also checked for in parallels_read).

I also don't mean to over-complicate things here for you, which is why I asked about the max value of ph.tracks. If that has a reasonable bounds check, then we don't need to worry about overflow at all, and can just change the cast to an int64_t instead of uint64_t.

I think we are safe so long as ph.tracks <= (INT32_MAX/513), and it that seems like it would be a reasonably flexible bounds limit check when first opening the image (since QEMU can't really support higher than that, anyway).

Thanks,
Jeff

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
block/parallels.c +0 -0
 }
 static int parallels_read(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.9.1
#13 lore raw
Quoted 121 lines · Jeff Cody
Quoted 89 lines · Denis V. Lunev
Quoted 41 lines · Jeff Cody
Quoted 40 lines · Denis V. Lunev

Parallels has released in the recent updates of Parallels Server 5/6 new addition to his image format. Images with signature WithouFreSpacExt have offsets in the catalog coded not as offsets in sectors (multiple of 512 bytes) but offsets coded in blocks (i.e. header->tracks * 512)

In this case all 64 bits of header->nb_sectors are used for image size.

This patch implements support of this for qemu-img.

CC Kevin Wolf <[email protected]>
CC Stefan Hajnoczi <[email protected]>
---
  block/parallels.c | 20 +++++++++++++++-----
  1 file changed, 15 insertions(+), 5 deletions(-)
block/parallels.c +5 -1
index 02739cf..d9cb04f 100644
@@ -30,6 +30,7 @@
  /**************************************************************/
  #define HEADER_MAGIC "WithoutFreeSpace"
+#define HEADER_MAGIC2 "WithouFreSpacExt"
  #define HEADER_VERSION 2
  #define HEADER_SIZE 64
@@ -54,6 +55,8 @@ typedef struct BDRVParallelsState {
      unsigned int catalog_size;
      unsigned int tracks;
+
+    unsigned int off_multiplier;
  } BDRVParallelsState;
  static int parallels_probe(const uint8_t *buf, int buf_size, const char *filename)
@@ -63,7 +66,8 @@ static int parallels_probe(const uint8_t *buf, int buf_size, const char *filenam
      if (buf_size < HEADER_SIZE)
          return 0;
-    if (!memcmp(ph->magic, HEADER_MAGIC, 16) &&
+    if ((!memcmp(ph->magic, HEADER_MAGIC, 16) ||
+        !memcmp(ph->magic, HEADER_MAGIC2, 16)) &&
          (le32_to_cpu(ph->version) == HEADER_VERSION))
Quoted 8 lines

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

Quoted 6 lines
block/parallels.c +2 -0
          return 100;
@@ -85,13 +89,18 @@ static int parallels_open(BlockDriverState *bs, QDict *options, int flags,
          goto fail;
      }
+    bs->total_sectors = le64_to_cpu(ph.nb_sectors);
+

bs->total_sectors is a int64_t, and ph.nb_sectors is a uint64_t. Should we do a sanity check here on the max number of sectors?

in reality such image will not be usable as we will later have to multiply this count with 512 to calculate offset in the file

Quoted 7 lines

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

Quoted 6 lines
block/parallels.c +3 -1
      if (le32_to_cpu(ph.version) != HEADER_VERSION)
          goto fail_format;
-    if (memcmp(ph.magic, HEADER_MAGIC, 16))
+    if (!memcmp(ph.magic, HEADER_MAGIC, 16)) {
+        s->off_multiplier = 1;
+        bs->total_sectors = (uint32_t)bs->total_sectors;

(same comment as in patch 1, w.r.t. cast vs. bitmask)

ok

Quoted 3 lines

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

Quoted 2 lines
block/parallels.c +2 -0
+    } else if (!memcmp(ph.magic, HEADER_MAGIC2, 16))
+        s->off_multiplier = le32_to_cpu(ph.tracks);

Is there a maximum size in the specification for ph.tracks?

no, there is no limitation. Though in reality I have seen the following images only:

   252 sectors, 63 sectors, 256 sectors, 2048 sectors

and only 2048 was used with this new header.

This is just an observation.

Quoted 19 lines

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

Quoted 2 lines
block/parallels.c +1 -0
+    else
          goto fail_format;

(same comment as the last patch, w.r.t. braces)

block/parallels.c static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num) · line 150

Quoted 12 lines
block/parallels.c +2 -3
-    bs->total_sectors = (uint32_t)le64_to_cpu(ph.nb_sectors);
-
      s->tracks = le32_to_cpu(ph.tracks);
      if (s->tracks == 0) {
          error_setg(errp, "Invalid image: Zero sectors per track");
@@ -137,7 +146,8 @@ static int64_t seek_to_sector(BlockDriverState *bs, int64_t sector_num)
      /* not allocated */
      if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))
          return -1;
-    return (uint64_t)(s->catalog_bitmap[index] + offset) * 512;
+    return
+        ((uint64_t)s->catalog_bitmap[index] * s->off_multiplier + offset) * 512;

Do we need to worry about overflow here, depending on the value of off_multiplier?

Quoted 5 lines
Quoted 2 lines

Also, (and this existed prior to this patch), - we are casting to uint64_t, although the function returns int64_t.

good catch. I'll change the declaration to uint64_t and will return 0 from previous if aka

Thanks.

I'm not sure that is the best option though - the only place the return value from this function is used is in parallels_read(), which passes the output into bdrv_pread() as the offset. The offset parameter for bdrv_pread/pwrite is an int64_t, not a uint64_t.

Quoted 9 lines

if ((index > s->catalog_size) || (s->catalog_bitmap[index] == 0))

It is not possible to obtain 0 as a real offset of any sector as this is an offset of the header.

Regarding overflow check. Do you think that we should return error to the upper layer or we could silently fill result data with zeroes as was done originally for the following case 'index > s->catalog_size' ?

I think it would be best to return error (anything < 0 will also cause bdrv_pread to return -EIO, and it is also checked for in parallels_read).

I also don't mean to over-complicate things here for you, which is why I asked about the max value of ph.tracks. If that has a reasonable bounds check, then we don't need to worry about overflow at all, and can just change the cast to an int64_t instead of uint64_t.

Quoted 4 lines

I think we are safe so long as ph.tracks <= (INT32_MAX/513), and it that seems like it would be a reasonably flexible bounds limit check when first opening the image (since QEMU can't really support higher than

sounds good to me

Thank you for an idea :)

Quoted 11 lines

that, anyway).

Thanks,
Jeff

Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
block/parallels.c +0 -0
  }
  static int parallels_read(BlockDriverState *bs, int64_t sector_num,
Signature · 1 line
1.9.1