26ba25
From e7b1acac8246a203ed0ed55a83cec29a4fa6252c Mon Sep 17 00:00:00 2001
26ba25
From: John Snow <jsnow@redhat.com>
26ba25
Date: Wed, 18 Jul 2018 22:54:45 +0200
26ba25
Subject: [PATCH 227/268] qcow2: add overlap check for bitmap directory
26ba25
26ba25
RH-Author: John Snow <jsnow@redhat.com>
26ba25
Message-id: <20180718225511.14878-10-jsnow@redhat.com>
26ba25
Patchwork-id: 81394
26ba25
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 09/35] qcow2: add overlap check for bitmap directory
26ba25
Bugzilla: 1207657
26ba25
RH-Acked-by: Eric Blake <eblake@redhat.com>
26ba25
RH-Acked-by: Stefan Hajnoczi <stefanha@redhat.com>
26ba25
RH-Acked-by: Fam Zheng <famz@redhat.com>
26ba25
26ba25
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
26ba25
26ba25
Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
26ba25
Message-id: 20180705151515.779173-1-vsementsov@virtuozzo.com
26ba25
Signed-off-by: Max Reitz <mreitz@redhat.com>
26ba25
(cherry picked from commit 0e4e4318eaa56c831001bdf617094807ec6d451c)
26ba25
Signed-off-by: John Snow <jsnow@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 block/qcow2-bitmap.c   |  7 ++++++-
26ba25
 block/qcow2-refcount.c | 10 ++++++++++
26ba25
 block/qcow2.c          | 22 ++++++++++++++--------
26ba25
 block/qcow2.h          | 45 ++++++++++++++++++++++++---------------------
26ba25
 qapi/block-core.json   | 21 ++++++++++++---------
26ba25
 5 files changed, 66 insertions(+), 39 deletions(-)
26ba25
26ba25
diff --git a/block/qcow2-bitmap.c b/block/qcow2-bitmap.c
26ba25
index 3e4e4e4..14050eb 100644
26ba25
--- a/block/qcow2-bitmap.c
26ba25
+++ b/block/qcow2-bitmap.c
26ba25
@@ -775,7 +775,12 @@ static int bitmap_list_store(BlockDriverState *bs, Qcow2BitmapList *bm_list,
26ba25
         }
26ba25
     }
26ba25
 
26ba25
-    ret = qcow2_pre_write_overlap_check(bs, 0, dir_offset, dir_size);
26ba25
+    /* Actually, even in in-place case ignoring QCOW2_OL_BITMAP_DIRECTORY is not
26ba25
+     * necessary, because we drop QCOW2_AUTOCLEAR_BITMAPS when updating bitmap
26ba25
+     * directory in-place (actually, turn-off the extension), which is checked
26ba25
+     * in qcow2_check_metadata_overlap() */
26ba25
+    ret = qcow2_pre_write_overlap_check(
26ba25
+            bs, in_place ? QCOW2_OL_BITMAP_DIRECTORY : 0, dir_offset, dir_size);
26ba25
     if (ret < 0) {
26ba25
         goto fail;
26ba25
     }
26ba25
diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c
26ba25
index 4e14c0a..1307069 100644
26ba25
--- a/block/qcow2-refcount.c
26ba25
+++ b/block/qcow2-refcount.c
26ba25
@@ -2705,6 +2705,16 @@ int qcow2_check_metadata_overlap(BlockDriverState *bs, int ign, int64_t offset,
26ba25
         }
26ba25
     }
26ba25
 
26ba25
+    if ((chk & QCOW2_OL_BITMAP_DIRECTORY) &&
26ba25
+        (s->autoclear_features & QCOW2_AUTOCLEAR_BITMAPS))
26ba25
+    {
26ba25
+        if (overlaps_with(s->bitmap_directory_offset,
26ba25
+                          s->bitmap_directory_size))
26ba25
+        {
26ba25
+            return QCOW2_OL_BITMAP_DIRECTORY;
26ba25
+        }
26ba25
+    }
26ba25
+
26ba25
     return 0;
26ba25
 }
26ba25
 
26ba25
diff --git a/block/qcow2.c b/block/qcow2.c
26ba25
index 71fbfcd..c5341a4 100644
26ba25
--- a/block/qcow2.c
26ba25
+++ b/block/qcow2.c
26ba25
@@ -676,6 +676,11 @@ static QemuOptsList qcow2_runtime_opts = {
26ba25
             .help = "Check for unintended writes into an inactive L2 table",
26ba25
         },
26ba25
         {
26ba25
+            .name = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
26ba25
+            .type = QEMU_OPT_BOOL,
26ba25
+            .help = "Check for unintended writes into the bitmap directory",
26ba25
+        },
26ba25
+        {
26ba25
             .name = QCOW2_OPT_CACHE_SIZE,
26ba25
             .type = QEMU_OPT_SIZE,
26ba25
             .help = "Maximum combined metadata (L2 tables and refcount blocks) "
26ba25
@@ -708,14 +713,15 @@ static QemuOptsList qcow2_runtime_opts = {
26ba25
 };
26ba25
 
26ba25
 static const char *overlap_bool_option_names[QCOW2_OL_MAX_BITNR] = {
26ba25
-    [QCOW2_OL_MAIN_HEADER_BITNR]    = QCOW2_OPT_OVERLAP_MAIN_HEADER,
26ba25
-    [QCOW2_OL_ACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L1,
26ba25
-    [QCOW2_OL_ACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_ACTIVE_L2,
26ba25
-    [QCOW2_OL_REFCOUNT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
26ba25
-    [QCOW2_OL_REFCOUNT_BLOCK_BITNR] = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
26ba25
-    [QCOW2_OL_SNAPSHOT_TABLE_BITNR] = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
26ba25
-    [QCOW2_OL_INACTIVE_L1_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L1,
26ba25
-    [QCOW2_OL_INACTIVE_L2_BITNR]    = QCOW2_OPT_OVERLAP_INACTIVE_L2,
26ba25
+    [QCOW2_OL_MAIN_HEADER_BITNR]      = QCOW2_OPT_OVERLAP_MAIN_HEADER,
26ba25
+    [QCOW2_OL_ACTIVE_L1_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L1,
26ba25
+    [QCOW2_OL_ACTIVE_L2_BITNR]        = QCOW2_OPT_OVERLAP_ACTIVE_L2,
26ba25
+    [QCOW2_OL_REFCOUNT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_TABLE,
26ba25
+    [QCOW2_OL_REFCOUNT_BLOCK_BITNR]   = QCOW2_OPT_OVERLAP_REFCOUNT_BLOCK,
26ba25
+    [QCOW2_OL_SNAPSHOT_TABLE_BITNR]   = QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE,
26ba25
+    [QCOW2_OL_INACTIVE_L1_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L1,
26ba25
+    [QCOW2_OL_INACTIVE_L2_BITNR]      = QCOW2_OPT_OVERLAP_INACTIVE_L2,
26ba25
+    [QCOW2_OL_BITMAP_DIRECTORY_BITNR] = QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY,
26ba25
 };
26ba25
 
26ba25
 static void cache_clean_timer_cb(void *opaque)
26ba25
diff --git a/block/qcow2.h b/block/qcow2.h
26ba25
index b5e2aa3..d2c63e4 100644
26ba25
--- a/block/qcow2.h
26ba25
+++ b/block/qcow2.h
26ba25
@@ -98,6 +98,7 @@
26ba25
 #define QCOW2_OPT_OVERLAP_SNAPSHOT_TABLE "overlap-check.snapshot-table"
26ba25
 #define QCOW2_OPT_OVERLAP_INACTIVE_L1 "overlap-check.inactive-l1"
26ba25
 #define QCOW2_OPT_OVERLAP_INACTIVE_L2 "overlap-check.inactive-l2"
26ba25
+#define QCOW2_OPT_OVERLAP_BITMAP_DIRECTORY "overlap-check.bitmap-directory"
26ba25
 #define QCOW2_OPT_CACHE_SIZE "cache-size"
26ba25
 #define QCOW2_OPT_L2_CACHE_SIZE "l2-cache-size"
26ba25
 #define QCOW2_OPT_L2_CACHE_ENTRY_SIZE "l2-cache-entry-size"
26ba25
@@ -401,34 +402,36 @@ typedef enum QCow2ClusterType {
26ba25
 } QCow2ClusterType;
26ba25
 
26ba25
 typedef enum QCow2MetadataOverlap {
26ba25
-    QCOW2_OL_MAIN_HEADER_BITNR    = 0,
26ba25
-    QCOW2_OL_ACTIVE_L1_BITNR      = 1,
26ba25
-    QCOW2_OL_ACTIVE_L2_BITNR      = 2,
26ba25
-    QCOW2_OL_REFCOUNT_TABLE_BITNR = 3,
26ba25
-    QCOW2_OL_REFCOUNT_BLOCK_BITNR = 4,
26ba25
-    QCOW2_OL_SNAPSHOT_TABLE_BITNR = 5,
26ba25
-    QCOW2_OL_INACTIVE_L1_BITNR    = 6,
26ba25
-    QCOW2_OL_INACTIVE_L2_BITNR    = 7,
26ba25
-
26ba25
-    QCOW2_OL_MAX_BITNR            = 8,
26ba25
-
26ba25
-    QCOW2_OL_NONE           = 0,
26ba25
-    QCOW2_OL_MAIN_HEADER    = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
26ba25
-    QCOW2_OL_ACTIVE_L1      = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
26ba25
-    QCOW2_OL_ACTIVE_L2      = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
26ba25
-    QCOW2_OL_REFCOUNT_TABLE = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
26ba25
-    QCOW2_OL_REFCOUNT_BLOCK = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
26ba25
-    QCOW2_OL_SNAPSHOT_TABLE = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
26ba25
-    QCOW2_OL_INACTIVE_L1    = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
26ba25
+    QCOW2_OL_MAIN_HEADER_BITNR      = 0,
26ba25
+    QCOW2_OL_ACTIVE_L1_BITNR        = 1,
26ba25
+    QCOW2_OL_ACTIVE_L2_BITNR        = 2,
26ba25
+    QCOW2_OL_REFCOUNT_TABLE_BITNR   = 3,
26ba25
+    QCOW2_OL_REFCOUNT_BLOCK_BITNR   = 4,
26ba25
+    QCOW2_OL_SNAPSHOT_TABLE_BITNR   = 5,
26ba25
+    QCOW2_OL_INACTIVE_L1_BITNR      = 6,
26ba25
+    QCOW2_OL_INACTIVE_L2_BITNR      = 7,
26ba25
+    QCOW2_OL_BITMAP_DIRECTORY_BITNR = 8,
26ba25
+
26ba25
+    QCOW2_OL_MAX_BITNR              = 9,
26ba25
+
26ba25
+    QCOW2_OL_NONE             = 0,
26ba25
+    QCOW2_OL_MAIN_HEADER      = (1 << QCOW2_OL_MAIN_HEADER_BITNR),
26ba25
+    QCOW2_OL_ACTIVE_L1        = (1 << QCOW2_OL_ACTIVE_L1_BITNR),
26ba25
+    QCOW2_OL_ACTIVE_L2        = (1 << QCOW2_OL_ACTIVE_L2_BITNR),
26ba25
+    QCOW2_OL_REFCOUNT_TABLE   = (1 << QCOW2_OL_REFCOUNT_TABLE_BITNR),
26ba25
+    QCOW2_OL_REFCOUNT_BLOCK   = (1 << QCOW2_OL_REFCOUNT_BLOCK_BITNR),
26ba25
+    QCOW2_OL_SNAPSHOT_TABLE   = (1 << QCOW2_OL_SNAPSHOT_TABLE_BITNR),
26ba25
+    QCOW2_OL_INACTIVE_L1      = (1 << QCOW2_OL_INACTIVE_L1_BITNR),
26ba25
     /* NOTE: Checking overlaps with inactive L2 tables will result in bdrv
26ba25
      * reads. */
26ba25
-    QCOW2_OL_INACTIVE_L2    = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
26ba25
+    QCOW2_OL_INACTIVE_L2      = (1 << QCOW2_OL_INACTIVE_L2_BITNR),
26ba25
+    QCOW2_OL_BITMAP_DIRECTORY = (1 << QCOW2_OL_BITMAP_DIRECTORY_BITNR),
26ba25
 } QCow2MetadataOverlap;
26ba25
 
26ba25
 /* Perform all overlap checks which can be done in constant time */
26ba25
 #define QCOW2_OL_CONSTANT \
26ba25
     (QCOW2_OL_MAIN_HEADER | QCOW2_OL_ACTIVE_L1 | QCOW2_OL_REFCOUNT_TABLE | \
26ba25
-     QCOW2_OL_SNAPSHOT_TABLE)
26ba25
+     QCOW2_OL_SNAPSHOT_TABLE | QCOW2_OL_BITMAP_DIRECTORY)
26ba25
 
26ba25
 /* Perform all overlap checks which don't require disk access */
26ba25
 #define QCOW2_OL_CACHED \
26ba25
diff --git a/qapi/block-core.json b/qapi/block-core.json
26ba25
index 9a9cfa0..b2de7af 100644
26ba25
--- a/qapi/block-core.json
26ba25
+++ b/qapi/block-core.json
26ba25
@@ -2666,18 +2666,21 @@
26ba25
 # @template: Specifies a template mode which can be adjusted using the other
26ba25
 #            flags, defaults to 'cached'
26ba25
 #
26ba25
+# @bitmap-directory: since 3.0
26ba25
+#
26ba25
 # Since: 2.9
26ba25
 ##
26ba25
 { 'struct': 'Qcow2OverlapCheckFlags',
26ba25
-  'data': { '*template':       'Qcow2OverlapCheckMode',
26ba25
-            '*main-header':    'bool',
26ba25
-            '*active-l1':      'bool',
26ba25
-            '*active-l2':      'bool',
26ba25
-            '*refcount-table': 'bool',
26ba25
-            '*refcount-block': 'bool',
26ba25
-            '*snapshot-table': 'bool',
26ba25
-            '*inactive-l1':    'bool',
26ba25
-            '*inactive-l2':    'bool' } }
26ba25
+  'data': { '*template':         'Qcow2OverlapCheckMode',
26ba25
+            '*main-header':      'bool',
26ba25
+            '*active-l1':        'bool',
26ba25
+            '*active-l2':        'bool',
26ba25
+            '*refcount-table':   'bool',
26ba25
+            '*refcount-block':   'bool',
26ba25
+            '*snapshot-table':   'bool',
26ba25
+            '*inactive-l1':      'bool',
26ba25
+            '*inactive-l2':      'bool',
26ba25
+            '*bitmap-directory': 'bool' } }
26ba25
 
26ba25
 ##
26ba25
 # @Qcow2OverlapChecks:
26ba25
-- 
26ba25
1.8.3.1
26ba25