Skip to contentSkip to conversation

qcow2: add overlap check for bitmap directory

PATCH Superseded Vladimir Sementsov-Ogievskiy 4 participants 16 messages Dec 1, 2017 – Mar 19, 2018 1 message not in the archive Block layer

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#1 lore raw

PATCH 1/2 qcow2: add overlap check for bitmap directory

In reply to Vladimir Sementsov-Ogievskiy
---
 block/qcow2.h          |  7 +++++--
 block/qcow2-refcount.c | 12 ++++++++++++
 block/qcow2.c          |  6 ++++++
 3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +5 -2
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
+#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
 #define QCOW2_OPT_CACHE_SIZE "cache-size"
 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
     QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
     QCOW2_OL_INACTIVE_L1_BITNR    = 6,
     QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
 
-    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
 
     QCOW2_OL_NONE           = 0,
     QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
     /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
      * reads. */
     QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
+    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
 } QCow2MetadataOverlap;
 
 /* Perform all overlap checks which can be done in constant time */
 #define QCOW2_OL_CONSTANT \
     (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
 
 /* Perform all overlap checks which don't require disk access */
 #define QCOW2_OL_CACHED \
block/qcow2-refcount.c +12 -0
index 3de1ab51ba..a7a2703f26 100644
@@ -2585,6 +2585,18 @@ int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
         }
     }
 
+    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS))
+    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */
+        if (overlaps_with(s->bitmap_directory_offset,
+                          s->bitmap_directory_size))
+        {
+            return QCOW2_OL_BITMAP_DIRECTORY;
+        }
+    }
+
     return 0;
 }
 
block/qcow2.c +6 -0
index 1914a940e5..8278c0e124 100644
@@ -655,6 +655,11 @@ static QemuOptsList qcow2_runtime_opts = {
             .help = "Check for unintended writes into an inactive L2 table",
         },
         {
+            .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
+            .type = QEMU_OPT_BOOL,
+            .help = "Check for unintended writes into the bitmap directory",
+        },
+        {
             .name = QCOW2_OPT_CACHE_SIZE,
             .type = QEMU_OPT_SIZE,
             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
@@ -690,6 +695,7 @@ static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
     [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
     [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
     [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
+    [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
 };
 
 static void cache_clean_timer_cb(void *opaque)
Signature · 1 line
2.11.1
#0 lore raw

cover qcow2: add overlap check for bitmap directory

Add simple constant overlap check.

Vladimir Sementsov-Ogievskiy (2):
qcow2: add overlap check for bitmap directory
qcow2: fix indentation after previous patch

 block/qcow2.h          | 45 ++++++++++++++++++++++++---------------------
 block/qcow2-refcount.c | 12 ++++++++++++
 block/qcow2.c          | 22 ++++++++++++++--------
 3 files changed, 50 insertions(+), 29 deletions(-)
Signature · 1 line
2.11.1
#12 lore raw

PATCH 2/2 qcow2: fix indentation after previous patch

---
 block/qcow2.h | 34 +++++++++++++++++-----------------
 block/qcow2.c | 16 ++++++++--------
 2 files changed, 25 insertions(+), 25 deletions(-)
block/qcow2.h +17 -17
index 8f226a3609..896ad08e5b 100644
@@ -399,29 +399,29 @@ typedef enum QCow2ClusterType {
 } QCow2ClusterType;
 
 typedef enum QCow2MetadataOverlap {
-    QCOW2_OL_MAIN_HEADER_BITNR    = 0,
-    QCOW2_OL_ACTIVE_L1_BITNR      = 1,
-    QCOW2_OL_ACTIVE_L2_BITNR      = 2,
-    QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
-    QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
-    QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
-    QCOW2_OL_INACTIVE_L1_BITNR    = 6,
-    QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_MAIN_HEADER_BITNR      = 0,
+    QCOW2_OL_ACTIVE_L1_BITNR        = 1,
+    QCOW2_OL_ACTIVE_L2_BITNR        = 2,
+    QCOW2_OL_REFCOUNT_TABLE_BITNR   = 3,
+    QCOW2_OL_REFCOUNT_BLOCK_BITNR   = 4,
+    QCOW2_OL_SNAPSHOT_TABLE_BITNR   = 5,
+    QCOW2_OL_INACTIVE_L1_BITNR      = 6,
+    QCOW2_OL_INACTIVE_L2_BITNR      = 7,
     QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
 
     QCOW2_OL_MAX_BITNR              = 9,
 
-    QCOW2_OL_NONE           = 0,
-    QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
-    QCOW2_OL_ACTIVE_L1      = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
-    QCOW2_OL_ACTIVE_L2      = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
-    QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
-    QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
-    QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
-    QCOW2_OL_INACTIVE_L1    = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
+    QCOW2_OL_NONE             = 0,
+    QCOW2_OL_MAIN_HEADER      = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
+    QCOW2_OL_ACTIVE_L1        = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
+    QCOW2_OL_ACTIVE_L2        = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
+    QCOW2_OL_REFCOUNT_TABLE   = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
+    QCOW2_OL_REFCOUNT_BLOCK   = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
+    QCOW2_OL_SNAPSHOT_TABLE   = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
+    QCOW2_OL_INACTIVE_L1      = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
     /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
      * reads. */
-    QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
+    QCOW2_OL_INACTIVE_L2      = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
     QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
 } QCow2MetadataOverlap;
 
block/qcow2.c +8 -8
index 8278c0e124..23f36a67c7 100644
@@ -687,14 +687,14 @@ static QemuOptsList qcow2_runtime_opts = {
 };
 
 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
-    [QCOW2_OL_MAIN_HEADER_BITNR]    = QCOW2_OPT_OVERLAP_MAIN_HEADER,
-    [QCOW2_OL_ACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L1,
-    [QCOW2_OL_ACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L2,
-    [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
-    [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
-    [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
-    [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
-    [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
+    [QCOW2_OL_MAIN_HEADER_BITNR]      = QCOW2_OPT_OVERLAP_MAIN_HEADER,
+    [QCOW2_OL_ACTIVE_L1_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L1,
+    [QCOW2_OL_ACTIVE_L2_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L2,
+    [QCOW2_OL_REFCOUNT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
+    [QCOW2_OL_REFCOUNT_BLOCK_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
+    [QCOW2_OL_SNAPSHOT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
+    [QCOW2_OL_INACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L1,
+    [QCOW2_OL_INACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L2,
     [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
 };
 
Signature · 1 line
2.11.1
#13 lore raw

Re: [Qemu-devel] [PATCH 2/2] qcow2: fix indentation after previous patch

Quoted 14 lines · Vladimir Sementsov-Ogievskiy
---
  block/qcow2.h | 34 +++++++++++++++++-----------------
  block/qcow2.c | 16 ++++++++--------
  2 files changed, 25 insertions(+), 25 deletions(-)
block/qcow2.h +0 -0
index 8f226a3609..896ad08e5b 100644
@@ -399,29 +399,29 @@ typedef enum QCow2ClusterType {
  } QCow2ClusterType;
 
  typedef enum QCow2MetadataOverlap {

block/qcow2.h typedef enum QCow2ClusterType { · line 403

Quoted 10 lines
block/qcow2.h +2 -8
-    QCOW2_OL_MAIN_HEADER_BITNR    = 0,
-    QCOW2_OL_ACTIVE_L1_BITNR      = 1,
-    QCOW2_OL_ACTIVE_L2_BITNR      = 2,
-    QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
-    QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
-    QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
-    QCOW2_OL_INACTIVE_L1_BITNR    = 6,
-    QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_MAIN_HEADER_BITNR      = 0,
+    QCOW2_OL_ACTIVE_L1_BITNR        = 1,

This demonstrates why I'm a fan of single space before =:

    foo = 1,
    blah = 2,

because it is needless churn to realign everything if you add a new longer line that doesn't fit with the earlier alignment of =. But we don't have a clear preference for either code style, and I'm also okay if you just squash this in with the previous patch or omit it altogether.

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#2 lore raw
In reply to Vladimir Sementsov-Ogievskiy

[adding Dan in cc]

Quoted 7 lines · Vladimir Sementsov-Ogievskiy
---
  block/qcow2.h          |  7 +++++--
  block/qcow2-refcount.c | 12 ++++++++++++
  block/qcow2.c          |  6 ++++++
  3 files changed, 23 insertions(+), 2 deletions(-)

Does the LUKS encryption header need similar overlap protection checks?

Quoted 7 lines
block/qcow2.h +1 -0
index 6f0ff15dd0..8f226a3609 100644
 
+++ b/block/qcow2-refcount.c

block/qcow2.h int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, · line 2592

Quoted 8 lines
block/qcow2.h +5 -0
@@ -2585,6 +2585,18 @@ int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
          }
      }
 
+    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS))
+    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */

Wording is awkward, how about:

The autoclear flag was previously dropped by update_ext_header_and_dir_in_place, so this will not fail

but I'm not sure if that is the intended meaning.

block/qcow2.h int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, · line 2601

Quoted 9 lines
block/qcow2.h +7 -0
+        if (overlaps_with(s->bitmap_directory_offset,
+                          s->bitmap_directory_size))
+        {
+            return QCOW2_OL_BITMAP_DIRECTORY;
+        }
+    }
+
      return 0;
  }

Do we need to add/update any iotests to cover this?

Signature · 3 lines
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org
#3 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 55 lines · Vladimir Sementsov-Ogievskiy
---
 block/qcow2.h          |  7 +++++--
 block/qcow2-refcount.c | 12 ++++++++++++
 block/qcow2.c          |  6 ++++++
 3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +5 -2
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
+#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
 #define QCOW2_OPT_CACHE_SIZE "cache-size"
 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
     QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
     QCOW2_OL_INACTIVE_L1_BITNR    = 6,
     QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
 
-    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
 
     QCOW2_OL_NONE           = 0,
     QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
     /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
      * reads. */
     QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
+    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
 } QCow2MetadataOverlap;
 
 /* Perform all overlap checks which can be done in constant time */
 #define QCOW2_OL_CONSTANT \
     (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
 
 /* Perform all overlap checks which don't require disk access */
 #define QCOW2_OL_CACHED \
block/qcow2-refcount.c +2 -0
index 3de1ab51ba..a7a2703f26 100644
@@ -2585,6 +2585,18 @@ int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
         }
     }
 
+    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS))

block/qcow2-refcount.c int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, · line 2599

Quoted 10 lines
block/qcow2-refcount.c +10 -0
+    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */
+        if (overlaps_with(s->bitmap_directory_offset,
+                          s->bitmap_directory_size))
+        {
+            return QCOW2_OL_BITMAP_DIRECTORY;
+        }
+    }
+

Isn't the purpose of this function to test if a given offset conflicts with known regions of the file? I don't see you actually utilize the 'offset' parameter here, but maybe I don't understand what you're trying to accomplish.

Quoted 28 lines
block/qcow2-refcount.c +0 -0
     return 0;
 }
 
block/qcow2.c +6 -0
index 1914a940e5..8278c0e124 100644
@@ -655,6 +655,11 @@ static QemuOptsList qcow2_runtime_opts = {
             .help = "Check for unintended writes into an inactive L2 table",
         },
         {
+            .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
+            .type = QEMU_OPT_BOOL,
+            .help = "Check for unintended writes into the bitmap directory",
+        },
+        {
             .name = QCOW2_OPT_CACHE_SIZE,
             .type = QEMU_OPT_SIZE,
             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
@@ -690,6 +695,7 @@ static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
     [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
     [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
     [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
+    [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
 };
 
 static void cache_clean_timer_cb(void *opaque)
Reviewed-by
#5 lore raw
In reply to a message not in the archive
Quoted 75 lines · Vladimir Sementsov-Ogievskiy
Quoted 65 lines · John Snow
Quoted 63 lines · Vladimir Sementsov-Ogievskiy

---
  block/qcow2.h          |  7 +++++--
  block/qcow2-refcount.c | 12 ++++++++++++
  block/qcow2.c          |  6 ++++++

  3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +0 -0
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
   #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE

"overlap-check.snapshot-table"
  #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1" #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2" +#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
  #define QCOW2_OPT_CACHE_SIZE "cache-size"
  #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size" #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"

block/qcow2.h +2 -0
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
       QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
       QCOW2_OL_INACTIVE_L1_BITNR    = 6,
       QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
   -    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
         QCOW2_OL_NONE           = 0,
       QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
       /* NOTE: Checking overlaps with inactive L2 tables will result

in bdrv
       * reads. */
      QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR), +    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR), } QCow2MetadataOverlap;
    /* Perform all overlap checks which can be done in constant time */ #define QCOW2_OL_CONSTANT \
      (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY) /* Perform all overlap checks which don't require disk access */ #define QCOW2_OL_CACHED \

block/qcow2-refcount.c +0 -0
index 3de1ab51ba..a7a2703f26 100644
@@ -2585,6 +2585,18 @@ int

qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
          }
      }
  +    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) +    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */
+        if (overlaps_with(s->bitmap_directory_offset,
+                          s->bitmap_directory_size))
+        {

Quoted 8 lines
Quoted 4 lines

+            return QCOW2_OL_BITMAP_DIRECTORY;
+        }
+    }
+

Isn't the purpose of this function to test if a given offset conflicts with known regions of the file? I don't see you actually utilize the 'offset' parameter here, but maybe I don't understand what you're trying to accomplish.

Quoted 5 lines

#define overlaps_with(ofs, sz) \
    ranges_overlap(offset, size, ofs, sz)

I've just copied one of similar blocks in qcow2_check_metadata_overlap()

Sigh, I didn't realize that was a macro. I don't really like lowercase macros, but you didn't add it.

Carry on...

Reviewed-by on v1 2/2
#14 lore raw

Re: [Qemu-devel] [Qemu-block] [PATCH 2/2] qcow2: fix indentation after previous patch

In reply to Vladimir Sementsov-Ogievskiy
Quoted 75 lines · Vladimir Sementsov-Ogievskiy
---
 block/qcow2.h | 34 +++++++++++++++++-----------------
 block/qcow2.c | 16 ++++++++--------
 2 files changed, 25 insertions(+), 25 deletions(-)
block/qcow2.h +17 -17
index 8f226a3609..896ad08e5b 100644
@@ -399,29 +399,29 @@ typedef enum QCow2ClusterType {
 } QCow2ClusterType;
 
 typedef enum QCow2MetadataOverlap {
-    QCOW2_OL_MAIN_HEADER_BITNR    = 0,
-    QCOW2_OL_ACTIVE_L1_BITNR      = 1,
-    QCOW2_OL_ACTIVE_L2_BITNR      = 2,
-    QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
-    QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
-    QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
-    QCOW2_OL_INACTIVE_L1_BITNR    = 6,
-    QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_MAIN_HEADER_BITNR      = 0,
+    QCOW2_OL_ACTIVE_L1_BITNR        = 1,
+    QCOW2_OL_ACTIVE_L2_BITNR        = 2,
+    QCOW2_OL_REFCOUNT_TABLE_BITNR   = 3,
+    QCOW2_OL_REFCOUNT_BLOCK_BITNR   = 4,
+    QCOW2_OL_SNAPSHOT_TABLE_BITNR   = 5,
+    QCOW2_OL_INACTIVE_L1_BITNR      = 6,
+    QCOW2_OL_INACTIVE_L2_BITNR      = 7,
     QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
 
     QCOW2_OL_MAX_BITNR              = 9,
 
-    QCOW2_OL_NONE           = 0,
-    QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
-    QCOW2_OL_ACTIVE_L1      = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
-    QCOW2_OL_ACTIVE_L2      = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
-    QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
-    QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
-    QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
-    QCOW2_OL_INACTIVE_L1    = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
+    QCOW2_OL_NONE             = 0,
+    QCOW2_OL_MAIN_HEADER      = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
+    QCOW2_OL_ACTIVE_L1        = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
+    QCOW2_OL_ACTIVE_L2        = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
+    QCOW2_OL_REFCOUNT_TABLE   = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
+    QCOW2_OL_REFCOUNT_BLOCK   = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
+    QCOW2_OL_SNAPSHOT_TABLE   = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
+    QCOW2_OL_INACTIVE_L1      = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
     /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
      * reads. */
-    QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
+    QCOW2_OL_INACTIVE_L2      = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
     QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
 } QCow2MetadataOverlap;
 
block/qcow2.c +1 -8
index 8278c0e124..23f36a67c7 100644
@@ -687,14 +687,14 @@ static QemuOptsList qcow2_runtime_opts = {
 };
 
 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
-    [QCOW2_OL_MAIN_HEADER_BITNR]    = QCOW2_OPT_OVERLAP_MAIN_HEADER,
-    [QCOW2_OL_ACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L1,
-    [QCOW2_OL_ACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L2,
-    [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
-    [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
-    [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
-    [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
-    [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
+    [QCOW2_OL_MAIN_HEADER_BITNR]      = QCOW2_OPT_OVERLAP_MAIN_HEADER,

block/qcow2.c static QemuOptsList qcow2_runtime_opts = { · line 700

Quoted 10 lines
block/qcow2.c +7 -0
+    [QCOW2_OL_ACTIVE_L1_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L1,
+    [QCOW2_OL_ACTIVE_L2_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L2,
+    [QCOW2_OL_REFCOUNT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
+    [QCOW2_OL_REFCOUNT_BLOCK_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
+    [QCOW2_OL_SNAPSHOT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
+    [QCOW2_OL_INACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L1,
+    [QCOW2_OL_INACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L2,
     [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
 };
 

Squash it in, or don't, either way:

#6 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 50 lines · Vladimir Sementsov-Ogievskiy
---
 block/qcow2.h          |  7 +++++--
 block/qcow2-refcount.c | 12 ++++++++++++
 block/qcow2.c          |  6 ++++++
 3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +5 -2
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
+#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
 #define QCOW2_OPT_CACHE_SIZE "cache-size"
 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
 #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
     QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
     QCOW2_OL_INACTIVE_L1_BITNR    = 6,
     QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
 
-    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
 
     QCOW2_OL_NONE           = 0,
     QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
     /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
      * reads. */
     QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
+    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
 } QCow2MetadataOverlap;
 
 /* Perform all overlap checks which can be done in constant time */
 #define QCOW2_OL_CONSTANT \
     (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
 
 /* Perform all overlap checks which don't require disk access */
 #define QCOW2_OL_CACHED \
block/qcow2-refcount.c +0 -0
index 3de1ab51ba..a7a2703f26 100644

block/qcow2-refcount.c int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, · line 2592

Quoted 8 lines
block/qcow2-refcount.c +5 -0
@@ -2585,6 +2585,18 @@ int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
         }
     }
 
+    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS))
+    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */

That's really not an argument. bitmap_list_store() has to pass QCOW2_OL_BITMAP_DIRECTORY to @ign anyway. (Because there is no reason not to.)

Max

Quoted 35 lines
block/qcow2-refcount.c +7 -0
+        if (overlaps_with(s->bitmap_directory_offset,
+                          s->bitmap_directory_size))
+        {
+            return QCOW2_OL_BITMAP_DIRECTORY;
+        }
+    }
+
     return 0;
 }
 
block/qcow2.c +6 -0
index 1914a940e5..8278c0e124 100644
@@ -655,6 +655,11 @@ static QemuOptsList qcow2_runtime_opts = {
             .help = "Check for unintended writes into an inactive L2 table",
         },
         {
+            .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
+            .type = QEMU_OPT_BOOL,
+            .help = "Check for unintended writes into the bitmap directory",
+        },
+        {
             .name = QCOW2_OPT_CACHE_SIZE,
             .type = QEMU_OPT_SIZE,
             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
@@ -690,6 +695,7 @@ static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
     [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
     [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
     [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
+    [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
 };
 
 static void cache_clean_timer_cb(void *opaque)
#7 lore raw
Quoted 54 lines · Max Reitz
Quoted 53 lines · Vladimir Sementsov-Ogievskiy
---
  block/qcow2.h          |  7 +++++--
  block/qcow2-refcount.c | 12 ++++++++++++
  block/qcow2.c          |  6 ++++++
  3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +5 -2
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
  #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
  #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
  #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
+#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
  #define QCOW2_OPT_CACHE_SIZE "cache-size"
  #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
  #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
      QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
      QCOW2_OL_INACTIVE_L1_BITNR    = 6,
      QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
 
-    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
 
      QCOW2_OL_NONE           = 0,
      QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
      /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
       * reads. */
      QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
+    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
  } QCow2MetadataOverlap;
 
  /* Perform all overlap checks which can be done in constant time */
  #define QCOW2_OL_CONSTANT \
      (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
 
  /* Perform all overlap checks which don't require disk access */
  #define QCOW2_OL_CACHED \
block/qcow2-refcount.c +0 -0
index 3de1ab51ba..a7a2703f26 100644
@@ -2585,6 +2585,18 @@ int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
          }
      }
 
Quoted 8 lines

block/qcow2-refcount.c int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset, · line 2592

Quoted 5 lines
block/qcow2-refcount.c +5 -0
+    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS))
+    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */

That's really not an argument. bitmap_list_store() has to pass QCOW2_OL_BITMAP_DIRECTORY to @ign anyway. (Because there is no reason not to.)

in_place is a reason. When we store directory in_place, it definitely overlaps with current directory. But this is done with cleared autoclear flag (to make it safe), so we will skip this check and will not fail.

Quoted 39 lines

Max

Quoted 35 lines
block/qcow2-refcount.c +7 -0
+        if (overlaps_with(s->bitmap_directory_offset,
+                          s->bitmap_directory_size))
+        {
+            return QCOW2_OL_BITMAP_DIRECTORY;
+        }
+    }
+
      return 0;
  }
 
block/qcow2.c +6 -0
index 1914a940e5..8278c0e124 100644
@@ -655,6 +655,11 @@ static QemuOptsList qcow2_runtime_opts = {
              .help = "Check for unintended writes into an inactive L2 table",
          },
          {
+            .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
+            .type = QEMU_OPT_BOOL,
+            .help = "Check for unintended writes into the bitmap directory",
+        },
+        {
              .name = QCOW2_OPT_CACHE_SIZE,
              .type = QEMU_OPT_SIZE,
              .help = "Maximum combined metadata (L2 tables and refcount blocks) "
@@ -690,6 +695,7 @@ static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
      [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
      [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
      [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
+    [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
  };
 
  static void cache_clean_timer_cb(void *opaque)
Signature · 2 lines
Best regards,
Vladimir
#11 lore raw
In reply to Max Reitz
Quoted 74 lines · Max Reitz
Quoted 73 lines · Vladimir Sementsov-Ogievskiy
Quoted 72 lines · Max Reitz
Quoted 67 lines · Vladimir Sementsov-Ogievskiy
Quoted 58 lines · Max Reitz
Quoted 57 lines · Vladimir Sementsov-Ogievskiy
---
    block/qcow2.h          |  7 +++++--
    block/qcow2-refcount.c | 12 ++++++++++++
    block/qcow2.c          |  6 ++++++
    3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +0 -0
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
    #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE

"overlap-check.snapshot-table"
   #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1" #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
+#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
   #define QCOW2_OPT_CACHE_SIZE "cache-size"
   #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size" #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"

block/qcow2.h +2 -0
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
        QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
        QCOW2_OL_INACTIVE_L1_BITNR    = 6,
        QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
    -    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
          QCOW2_OL_NONE           = 0,
        QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
        /* NOTE: Checking overlaps with inactive L2 tables will result

in bdrv
        * reads. */
       QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
+    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
   } QCow2MetadataOverlap;
     /* Perform all overlap checks which can be done in constant
time */
   #define QCOW2_OL_CONSTANT \
       (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 |
QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
     /* Perform all overlap checks which don't require disk access */ #define QCOW2_OL_CACHED \

block/qcow2-refcount.c +0 -0
index 3de1ab51ba..a7a2703f26 100644
@@ -2585,6 +2585,18 @@ int

qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
           }
       }
   +    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&

Quoted 8 lines
Quoted 5 lines

+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) +    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */

That's really not an argument.  bitmap_list_store() has to pass QCOW2_OL_BITMAP_DIRECTORY to @ign anyway.  (Because there is no reason not to.)

Quoted 2 lines

in_place is a reason. When we store directory in_place, it definitely overlaps with current directory.

Well, then you just pass QCOW2_OL_BITMAP_DIRECTORY to @ign, which is what that argument is for? :-)

Quoted 8 lines
Quoted 2 lines

hmm. but actually, I should not, because of zeroed autoclear flag. So, do you think, it is better to pass it, anyway?

Yes. That flag describes what kind of metadata structures you are planning to overwrite, and you *are* planning to overwrite the bitmap directory, so you should set it.

Max

Ok, reasonable. I'll respin with that fixed.

Signature · 2 lines
Best regards,
Vladimir
#9 lore raw
In reply to Max Reitz
Quoted 66 lines · Max Reitz
Quoted 65 lines · Vladimir Sementsov-Ogievskiy
Quoted 64 lines · Max Reitz
Quoted 52 lines · Vladimir Sementsov-Ogievskiy
---
   block/qcow2.h          |  7 +++++--
   block/qcow2-refcount.c | 12 ++++++++++++
   block/qcow2.c          |  6 ++++++
   3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +0 -0
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
   #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE

"overlap-check.snapshot-table"
  #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1" #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
+#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
  #define QCOW2_OPT_CACHE_SIZE "cache-size"
  #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size" #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"

block/qcow2.h +2 -0
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
       QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
       QCOW2_OL_INACTIVE_L1_BITNR    = 6,
       QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
   -    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
         QCOW2_OL_NONE           = 0,
       QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
       /* NOTE: Checking overlaps with inactive L2 tables will result

in bdrv
       * reads. */
      QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
+    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
  } QCow2MetadataOverlap;
    /* Perform all overlap checks which can be done in constant time */ #define QCOW2_OL_CONSTANT \
      (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 |
QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
    /* Perform all overlap checks which don't require disk access */ #define QCOW2_OL_CACHED \

block/qcow2-refcount.c +0 -0
index 3de1ab51ba..a7a2703f26 100644
@@ -2585,6 +2585,18 @@ int

qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,

Quoted 8 lines

          }
      }
  +    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) +    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */

That's really not an argument.  bitmap_list_store() has to pass QCOW2_OL_BITMAP_DIRECTORY to @ign anyway.  (Because there is no reason not to.)

Quoted 4 lines
Quoted 2 lines

in_place is a reason. When we store directory in_place, it definitely overlaps with current directory.

Well, then you just pass QCOW2_OL_BITMAP_DIRECTORY to @ign, which is what that argument is for? :-)

hmm. but actually, I should not, because of zeroed autoclear flag. So, do you think, it is better to pass it, anyway?

Quoted 6 lines

Max

Quoted 3 lines

But this is done with cleared autoclear flag (to make it safe), so we will skip this check and will not
fail.

Signature · 2 lines
Best regards,
Vladimir
#8 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 66 lines · Vladimir Sementsov-Ogievskiy
Quoted 56 lines · Max Reitz
Quoted 55 lines · Vladimir Sementsov-Ogievskiy

---
  block/qcow2.h          |  7 +++++--
  block/qcow2-refcount.c | 12 ++++++++++++
  block/qcow2.c          |  6 ++++++

  3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +0 -0
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
   #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE

"overlap-check.snapshot-table"
  #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1" #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2" +#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
  #define QCOW2_OPT_CACHE_SIZE "cache-size"
  #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size" #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"

block/qcow2.h +2 -0
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
       QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
       QCOW2_OL_INACTIVE_L1_BITNR    = 6,
       QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
   -    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
         QCOW2_OL_NONE           = 0,
       QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
       /* NOTE: Checking overlaps with inactive L2 tables will result

in bdrv
       * reads. */
      QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR), +    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR), } QCow2MetadataOverlap;
    /* Perform all overlap checks which can be done in constant time */ #define QCOW2_OL_CONSTANT \
      (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY) /* Perform all overlap checks which don't require disk access */ #define QCOW2_OL_CACHED \

block/qcow2-refcount.c +0 -0
index 3de1ab51ba..a7a2703f26 100644
@@ -2585,6 +2585,18 @@ int

qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
          }
      }
  +    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&

Quoted 8 lines
Quoted 5 lines

+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) +    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */

That's really not an argument.  bitmap_list_store() has to pass QCOW2_OL_BITMAP_DIRECTORY to @ign anyway.  (Because there is no reason not to.)

Quoted 2 lines

in_place is a reason. When we store directory in_place, it definitely overlaps with current directory.

Well, then you just pass QCOW2_OL_BITMAP_DIRECTORY to @ign, which is what that argument is for? :-)

Max

Quoted 3 lines

But this is done with cleared autoclear flag (to make it safe), so we will skip this check and will not
fail.

#10 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 69 lines · Vladimir Sementsov-Ogievskiy
Quoted 68 lines · Max Reitz
Quoted 67 lines · Vladimir Sementsov-Ogievskiy
Quoted 66 lines · Max Reitz
Quoted 54 lines · Vladimir Sementsov-Ogievskiy

---
   block/qcow2.h          |  7 +++++--
   block/qcow2-refcount.c | 12 ++++++++++++
   block/qcow2.c          |  6 ++++++

   3 files changed, 23 insertions(+), 2 deletions(-)
block/qcow2.h +0 -0
index 6f0ff15dd0..8f226a3609 100644
@@ -98,6 +98,7 @@
    #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE

"overlap-check.snapshot-table"
   #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1" #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2" +#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
   #define QCOW2_OPT_CACHE_SIZE "cache-size"
   #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size" #define QCOW2_OPT_REFCOUNT_CACHE_SIZE "refcount-cache-size"

block/qcow2.h +2 -0
@@ -406,8 +407,9 @@ typedef enum QCow2MetadataOverlap {
        QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
        QCOW2_OL_INACTIVE_L1_BITNR    = 6,
        QCOW2_OL_INACTIVE_L2_BITNR    = 7,
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
    -    QCOW2_OL_MAX_BITNR            = 8,
+    QCOW2_OL_MAX_BITNR              = 9,
          QCOW2_OL_NONE           = 0,
        QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
@@ -420,12 +422,13 @@ typedef enum QCow2MetadataOverlap {
        /* NOTE: Checking overlaps with inactive L2 tables will result

in bdrv
        * reads. */
       QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR), +    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
   } QCow2MetadataOverlap;
     /* Perform all overlap checks which can be done in constant time */
   #define QCOW2_OL_CONSTANT \
       (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
-     QCOW2_OL_SNAPSHOT_TABLE)
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY) /* Perform all overlap checks which don't require disk access */ #define QCOW2_OL_CACHED \

block/qcow2-refcount.c +0 -0
index 3de1ab51ba..a7a2703f26 100644
@@ -2585,6 +2585,18 @@ int

qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,

Quoted 8 lines

           }
       }
   +    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS)) +    {
+        /* update_ext_header_and_dir_in_place firstly drop autoclear flag,
+         * so it will not fail */

That's really not an argument.  bitmap_list_store() has to pass QCOW2_OL_BITMAP_DIRECTORY to @ign anyway.  (Because there is no reason not to.)

Quoted 7 lines
Quoted 4 lines
Quoted 2 lines

in_place is a reason. When we store directory in_place, it definitely overlaps with current directory.

Well, then you just pass QCOW2_OL_BITMAP_DIRECTORY to @ign, which is what that argument is for? :-)

hmm. but actually, I should not, because of zeroed autoclear flag. So, do you think, it is better to pass it, anyway?

Yes. That flag describes what kind of metadata structures you are planning to overwrite, and you *are* planning to overwrite the bitmap directory, so you should set it.

Max

#15 lore raw
In reply to Vladimir Sementsov-Ogievskiy
Quoted 11 lines · Vladimir Sementsov-Ogievskiy

Add simple constant overlap check.

Vladimir Sementsov-Ogievskiy (2):
qcow2: add overlap check for bitmap directory
qcow2: fix indentation after previous patch

 block/qcow2.h          | 45 ++++++++++++++++++++++++---------------------
 block/qcow2-refcount.c | 12 ++++++++++++
 block/qcow2.c          | 22 ++++++++++++++--------
 3 files changed, 50 insertions(+), 29 deletions(-)

Vladimir, do we need this for 2.12 still?
How about "fix bitmaps migration through shared storage"?

#16 lore raw
Quoted 8 lines · John Snow
Quoted 6 lines · Vladimir Sementsov-Ogievskiy

Add simple constant overlap check.

Vladimir Sementsov-Ogievskiy (2):
qcow2: add overlap check for bitmap directory
qcow2: fix indentation after previous patch

Quoted 7 lines
Quoted 5 lines
  block/qcow2.h          | 45 ++++++++++++++++++++++++---------------------
  block/qcow2-refcount.c | 12 ++++++++++++
  block/qcow2.c          | 22 ++++++++++++++--------
  3 files changed, 50 insertions(+), 29 deletions(-)

Vladimir, do we need this for 2.12 still?
How about "fix bitmaps migration through shared storage"?

Ohh, my fault. Will look at them today.

Signature · 2 lines
Best regards,
Vladimir