Skip to contentSkip to conversation

qcow2: implement lazy refcounts optimization

RFC Superseded Stefan Hajnoczi 1 participant 8 messages Jun 22, 2012 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

cover qcow2: implement lazy refcounts optimization

This series aims to improve qcow2 performance with cache=writethrough and cache=directsync. In particular it reduces the impact of metadata updates for allocating writes.

Allocating writes are expensive because they involve updating L2 tables and refcount blocks. In addition they can also cause L2 table allocation and refcount block allocation but these remain unaffected by this optimization.

The key insight is that refcounts are not required to access data in the image. This means that we can postpone refcount updates without violating the data integrity guarantee that cache=writethrough and cache=directsync give.

The trade-off for postponing refcount updates is that the image may not be completely consistent in case of power failure or crash. If the image is dirty then it must be repaired before performing further modifications, in other words we need an fsck-like scan on startup.

I don't have performance results to share yet but I wanted to get the code out there. The bigger picture is that this optimization should help make qcow2 a good choice even for cache=writethrough and cache=directsync where QED has traditionally had an advantage due to less metadata - this allows us to converge image format development in QEMU around the qcow2v3 format.

Stefan Hajnoczi (7):
docs: add dirty bit to qcow2 specification
qcow2: introduce dirty bit
docs: add lazy refcounts bit to qcow2 specification
qemu-iotests: ignore qemu-img create lazy_refcounts output
qcow2: implement lazy refcounts
qemu-io: add "abort" command to simulate program crash
qemu-iotests: add 039 qcow2 lazy refcounts test

 block/qcow2-cluster.c        |    5 +-
 block/qcow2.c                |  111 +++++++++++++++++++++++++++++++++++++++---
 block/qcow2.h                |   11 +++++
 block_int.h                  |   26 +++++-----
 docs/specs/qcow2.txt         |   12 ++++-
 qemu-io.c                    |   12 +++++
 tests/qemu-iotests/039       |   99 +++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/039.out   |   34 +++++++++++++
 tests/qemu-iotests/common.rc |    3 +-
 tests/qemu-iotests/group     |    1 +
 10 files changed, 292 insertions(+), 22 deletions(-)
 create mode 100755 tests/qemu-iotests/039
 create mode 100644 tests/qemu-iotests/039.out
Signature · 1 line
1.7.10
#1 lore raw

RFC 1/7 docs: add dirty bit to qcow2 specification

The dirty bit will make it possible to perform lazy refcount updates, where the image file is not kept consistent all the time. Upon opening a dirty image file, it is necessary to perform a consistency check and repair any incorrect refcounts.

Therefore the dirty bit must be an incompatible feature bit. We don't want old programs accessing a file with stale refcounts.

---
 docs/specs/qcow2.txt |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
docs/specs/qcow2.txt +5 -1
index 87bf785..3a789ce 100644
@@ -75,7 +75,11 @@ in the description of a field.
                     Bitmask of incompatible features. An implementation must
                     fail to open an image if an unknown bit is set.
 
-                    Bits 0-63:  Reserved (set to 0)
+                    Bit 0: Dirty bit.  If this bit is set then refcounts may be
+                           inconsistent, make sure to scan L1/L2 tables to
+                           repair refcounts before accessing the image.
+
+                    Bits 1-63:  Reserved (set to 0)
 
          80 -  87:  compatible_features
                     Bitmask of compatible features. An implementation can
Signature · 1 line
1.7.10
#2 lore raw

RFC 2/7 qcow2: introduce dirty bit

In reply to Stefan Hajnoczi

This patch adds an incompatible feature bit to mark images that have not been closed cleanly. When a dirty image file is opened a consistency check and repair is performed.

---
 block/qcow2.c |   44 ++++++++++++++++++++++++++++++++++++++++++--
 block/qcow2.h |    3 +++
 2 files changed, 45 insertions(+), 2 deletions(-)
block/qcow2.c +42 -2
index 2c1cd0a..cc30784 100644
@@ -214,6 +214,27 @@ static void report_unsupported_feature(BlockDriverState *bs,
     }
 }
 
+/*
+ * Clears the dirty bit and flushes before if necessary.  Only call this
+ * function when there are no pending requests, it does not guard against
+ * concurrent requests dirtying the image.
+ */
+static int qcow2_mark_clean(BlockDriverState *bs)
+{
+    BDRVQcowState *s = bs->opaque;
+
+    if (s->incompatible_features & QCOW2_INCOMPATIBLE_FEAT_DIRTY) {
+        int ret = bdrv_flush(bs);
+        if (ret < 0) {
+            return ret;
+        }
+
+        s->incompatible_features &= ~QCOW2_INCOMPATIBLE_FEAT_DIRTY;
+        return qcow2_update_header(bs);
+    }
+    return 0;
+}
+
 static int qcow2_open(BlockDriverState *bs, int flags)
 {
     BDRVQcowState *s = bs->opaque;
@@ -287,12 +308,13 @@ static int qcow2_open(BlockDriverState *bs, int flags)
     s->compatible_features      = header.compatible_features;
     s->autoclear_features       = header.autoclear_features;
 
-    if (s->incompatible_features != 0) {
+    if (s->incompatible_features & ~QCOW2_INCOMPATIBLE_FEAT_MASK) {
         void *feature_table = NULL;
         qcow2_read_extensions(bs, header.header_length, ext_end,
                               &feature_table);
         report_unsupported_feature(bs, feature_table,
-                                   s->incompatible_features);
+                                   s->incompatible_features &
+                                   ~QCOW2_INCOMPATIBLE_FEAT_MASK);
         ret = -ENOTSUP;
         goto fail;
     }
@@ -412,6 +434,22 @@ static int qcow2_open(BlockDriverState *bs, int flags)
     /* Initialise locks */
     qemu_co_mutex_init(&s->lock);
 
+    /* Repair image if dirty */
+    if ((s->incompatible_features & QCOW2_INCOMPATIBLE_FEAT_DIRTY) &&
+        !bs->read_only) {
+        BdrvCheckResult result = {0};
+
+        ret = qcow2_check_refcounts(bs, &result, BDRV_FIX_ERRORS);
+        if (ret < 0) {
+            goto fail;
+        }
+
+        ret = qcow2_mark_clean(bs);
+        if (ret < 0) {
+            goto fail;
+        }
+    }
+
 #ifdef DEBUG_ALLOC
     {
         BdrvCheckResult result = {0};
@@ -788,6 +826,8 @@ static void qcow2_close(BlockDriverState *bs)
     qcow2_cache_flush(bs, s->l2_table_cache);
     qcow2_cache_flush(bs, s->refcount_block_cache);
 
+    qcow2_mark_clean(bs);
+
     qcow2_cache_destroy(bs, s->l2_table_cache);
     qcow2_cache_destroy(bs, s->refcount_block_cache);
 
block/qcow2.h +3 -0
index 455b6d7..5c7cfb6 100644
@@ -108,6 +108,9 @@ enum {
     QCOW2_FEAT_TYPE_INCOMPATIBLE    = 0,
     QCOW2_FEAT_TYPE_COMPATIBLE      = 1,
     QCOW2_FEAT_TYPE_AUTOCLEAR       = 2,
+
+    QCOW2_INCOMPATIBLE_FEAT_DIRTY   = 0x1,
+    QCOW2_INCOMPATIBLE_FEAT_MASK    = QCOW2_INCOMPATIBLE_FEAT_DIRTY,
 };
 
 typedef struct Qcow2Feature {
Signature · 1 line
1.7.10
#3 lore raw

RFC 3/7 docs: add lazy refcounts bit to qcow2 specification

In reply to Stefan Hajnoczi

The lazy refcounts bit indicates that this image can take advantage of the dirty bit and that refcount updates can be postponed.

---
 docs/specs/qcow2.txt |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
docs/specs/qcow2.txt +5 -1
index 3a789ce..8b2c0eb 100644
@@ -85,7 +85,11 @@ in the description of a field.
                     Bitmask of compatible features. An implementation can
                     safely ignore any unknown bits that are set.
 
-                    Bits 0-63:  Reserved (set to 0)
+                    Bit 0: Lazy refcounts bit.  If this bit is set then lazy
+                           refcount updates can be used.  This means postponing
+                           marking the image file dirty and postponing refcount
+                           metadata updates.
+                    Bits 1-63:  Reserved (set to 0)
 
          88 -  95:  autoclear_features
                     Bitmask of auto-clear features. An implementation may only
Signature · 1 line
1.7.10
#4 lore raw

RFC 4/7 qemu-iotests: ignore qemu-img create lazy_refcounts output

In reply to Stefan Hajnoczi

Hide the default lazy_refcounts=off output from qemu-img like we do with other image creation options. This ensures that existing golden outputs continue to pass despite the new option that has been added.

Note that this patch applies before the one that actually introduces the lazy_refcounts=on|off option. This ensures git-bisect(1) continues to work.

---
 tests/qemu-iotests/common.rc |    3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tests/qemu-iotests/common.rc +2 -1
index e535874..f496547 100644
@@ -103,7 +103,8 @@ _make_test_img()
 	sed -e "s# table_size=0##g" | \
 	sed -e "s# compat='[^']*'##g" | \
 	sed -e "s# compat6=off##g" | \
-	sed -e "s# static=off##g"
+	sed -e "s# static=off##g" | \
+	sed -e "s# lazy_refcounts=off##g"
 }
 
 _cleanup_test_img()
Signature · 1 line
1.7.10
#5 lore raw

RFC 5/7 qcow2: implement lazy refcounts

In reply to Stefan Hajnoczi

Lazy refcounts is a performance optimization for qcow2 that postpones refcount metadata updates and instead marks the image dirty. In the case of crash or power failure the image will be left in a dirty state and repaired next time it is opened.

Reducing metadata I/O is important for cache=writethrough and cache=directsync because these modes guarantee that data is on disk after each write (hence we cannot take advantage of caching updates in RAM). Refcount metadata is not needed for guest->file block address translation and therefore does not need to be on-disk at the time of write completion - this is the motivation behind the lazy refcount optimization.

The lazy refcount optimization must be enabled at image creation time:

qemu-img create -f qcow2 -o compat=1.1,lazy_refcounts=on a.qcow2 10G qemu-system-x86_64 -drive if=virtio,file=a.qcow2,cache=writethrough

---
 block/qcow2-cluster.c |    5 +++-
 block/qcow2.c         |   67 ++++++++++++++++++++++++++++++++++++++++++++++---
 block/qcow2.h         |    8 ++++++
 block_int.h           |   26 ++++++++++---------
 4 files changed, 89 insertions(+), 17 deletions(-)
block/qcow2-cluster.c +4 -1
index d7e0e19..e179211 100644
@@ -662,7 +662,10 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m)
         qcow2_cache_depends_on_flush(s->l2_table_cache);
     }
 
-    qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache);
+    if (qcow2_need_accurate_refcounts(s)) {
+        qcow2_cache_set_dependency(bs, s->l2_table_cache,
+                                   s->refcount_block_cache);
+    }
     ret = get_cluster_table(bs, m->offset, &l2_table, &l2_index);
     if (ret < 0) {
         goto err;
block/qcow2.c +63 -4

109 lines not on this page · raw message

block/qcow2.h +8 -0
index 5c7cfb6..b8c0beb 100644
@@ -111,6 +111,9 @@ enum {
 
     QCOW2_INCOMPATIBLE_FEAT_DIRTY   = 0x1,
     QCOW2_INCOMPATIBLE_FEAT_MASK    = QCOW2_INCOMPATIBLE_FEAT_DIRTY,
+
+    QCOW2_COMPATIBLE_FEAT_LAZY_REFCOUNTS = 0x1,
+    QCOW2_COMPATIBLE_FEAT_MASK      = QCOW2_COMPATIBLE_FEAT_LAZY_REFCOUNTS,
 };
 
 typedef struct Qcow2Feature {
@@ -240,6 +243,11 @@ static inline int qcow2_get_cluster_type(uint64_t l2_entry)
     }
 }
 
+/* Check whether refcounts are eager or lazy */
+static inline bool qcow2_need_accurate_refcounts(BDRVQcowState *s)
+{
+    return !(s->incompatible_features & QCOW2_INCOMPATIBLE_FEAT_DIRTY);
+}
 
 // FIXME Need qcow2_ prefix to global functions
 
block_int.h +14 -12
index 1fb5352..733aa71 100644
@@ -31,8 +31,9 @@
 #include "qemu-timer.h"
 #include "qapi-types.h"
 
-#define BLOCK_FLAG_ENCRYPT	1
-#define BLOCK_FLAG_COMPAT6	4
+#define BLOCK_FLAG_ENCRYPT          1
+#define BLOCK_FLAG_COMPAT6          4
+#define BLOCK_FLAG_LAZY_REFCOUNTS   8
 
 #define BLOCK_IO_LIMIT_READ     0
 #define BLOCK_IO_LIMIT_WRITE    1
@@ -41,16 +42,17 @@
 #define BLOCK_IO_SLICE_TIME     100000000
 #define NANOSECONDS_PER_SECOND  1000000000.0
 
-#define BLOCK_OPT_SIZE          "size"
-#define BLOCK_OPT_ENCRYPT       "encryption"
-#define BLOCK_OPT_COMPAT6       "compat6"
-#define BLOCK_OPT_BACKING_FILE  "backing_file"
-#define BLOCK_OPT_BACKING_FMT   "backing_fmt"
-#define BLOCK_OPT_CLUSTER_SIZE  "cluster_size"
-#define BLOCK_OPT_TABLE_SIZE    "table_size"
-#define BLOCK_OPT_PREALLOC      "preallocation"
-#define BLOCK_OPT_SUBFMT        "subformat"
-#define BLOCK_OPT_COMPAT_LEVEL  "compat"
+#define BLOCK_OPT_SIZE              "size"
+#define BLOCK_OPT_ENCRYPT           "encryption"
+#define BLOCK_OPT_COMPAT6           "compat6"
+#define BLOCK_OPT_BACKING_FILE      "backing_file"
+#define BLOCK_OPT_BACKING_FMT       "backing_fmt"
+#define BLOCK_OPT_CLUSTER_SIZE      "cluster_size"
+#define BLOCK_OPT_TABLE_SIZE        "table_size"
+#define BLOCK_OPT_PREALLOC          "preallocation"
+#define BLOCK_OPT_SUBFMT            "subformat"
+#define BLOCK_OPT_COMPAT_LEVEL      "compat"
+#define BLOCK_OPT_LAZY_REFCOUNTS    "lazy_refcounts"
 
 typedef struct BdrvTrackedRequest BdrvTrackedRequest;
 
Signature · 1 line
1.7.10
#6 lore raw

RFC 6/7 qemu-io: add "abort" command to simulate program crash

In reply to Stefan Hajnoczi

Avoiding data loss and corruption is the top requirement for image file formats. The qemu-io "abort" command makes it possible to simulate program crashes and does not give the image format a chance to cleanly shut down. This command is useful for data integrity test cases.

---
 qemu-io.c |   12 ++++++++++++
 1 file changed, 12 insertions(+)
qemu-io.c +12 -0
index 5882067..b6bf0bd 100644
@@ -1648,6 +1648,17 @@ static const cmdinfo_t map_cmd = {
        .oneline        = "prints the allocated areas of a file",
 };
 
+static int abort_f(int argc, char **argv)
+{
+    abort();
+}
+
+static const cmdinfo_t abort_cmd = {
+       .name           = "abort",
+       .cfunc          = abort_f,
+       .flags          = CMD_NOFILE_OK,
+       .oneline        = "simulate a program crash using abort(3)",
+};
 
 static int close_f(int argc, char **argv)
 {
@@ -1901,6 +1912,7 @@ int main(int argc, char **argv)
     add_command(&discard_cmd);
     add_command(&alloc_cmd);
     add_command(&map_cmd);
+    add_command(&abort_cmd);
 
     add_args_command(init_args_command);
     add_check_command(init_check_command);
Signature · 1 line
1.7.10
#7 lore raw

RFC 7/7 qemu-iotests: add 039 qcow2 lazy refcounts test

In reply to Stefan Hajnoczi

This tests establishes the basic post-conditions of the qcow2 lazy refcounts features:

1. If the image was closed normally, it is marked clean.

2. If an allocating write was performed and the image was not close normally, then it is marked dirty.

a. Written data can be read back successfully.
b. The image file can be repaired and will be marked clean again.

---
 tests/qemu-iotests/039     |   99 ++++++++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/039.out |   34 +++++++++++++++
 tests/qemu-iotests/group   |    1 +
 3 files changed, 134 insertions(+)
 create mode 100755 tests/qemu-iotests/039
 create mode 100644 tests/qemu-iotests/039.out
tests/qemu-iotests/039 new file +99 -0
new file mode 100755
index 0000000..f4f9347
@@ -0,0 +1,99 @@
+#!/bin/bash
+#
+# Test qcow2 lazy refcounts
+#
+# Copyright (C) 2012 Red Hat, Inc.
+# Copyright IBM, Corp. 2010
+#
+# Based on test 038.
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+#
+
+# creator
[email protected]
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+here=`pwd`
+tmp=/tmp/$$
+status=1	# failure is the default!
+
+_cleanup()
+{
+	_cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+
+_supported_fmt qcow2
+_supported_proto generic
+_supported_os Linux
+
+size=128M
+
+echo
+echo "== Checking that image is clean on shutdown =="
+
+IMGOPTS="compat=1.1,lazy_refcounts=on"
+_make_test_img $size
+
+$QEMU_IO -c "write -P 0x5a 0 512" $TEST_IMG | _filter_qemu_io
+
+# The dirty bit must not be set
+./qcow2.py $TEST_IMG dump-header | grep incompatible_features
+_check_test_img
+
+echo
+echo "== Creating a dirty image file =="
+
+IMGOPTS="compat=1.1,lazy_refcounts=on"
+_make_test_img $size
+
+old_ulimit=$(ulimit -c)
+ulimit -c 0 # do not produce a core dump on abort(3)
+$QEMU_IO -c "write -P 0x5a 0 512" -c "abort" $TEST_IMG | _filter_qemu_io
+ulimit -c "$old_ulimit"
+
+# The dirty bit must be set
+./qcow2.py $TEST_IMG dump-header | grep incompatible_features
+_check_test_img
+
+echo
+echo "== Read-only access must still work =="
+
+$QEMU_IO -r -c "read -P 0x5a 0 512" $TEST_IMG | _filter_qemu_io
+
+echo
+echo "== Repairing the image file must succeed =="
+
+$QEMU_IMG check -r all $TEST_IMG
+
+# The dirty bit must not be set
+./qcow2.py $TEST_IMG dump-header | grep incompatible_features
+
+echo
+echo "== Data should still be accessible after repair =="
+
+$QEMU_IO -c "read -P 0x5a 0 512" $TEST_IMG | _filter_qemu_io
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
+
tests/qemu-iotests/039.out new file +34 -0
new file mode 100644
index 0000000..9a23181
@@ -0,0 +1,34 @@
+QA output created by 039
+
+== Checking that image is clean on shutdown ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 lazy_refcounts=on 
+wrote 512/512 bytes at offset 0
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+incompatible_features     0x0
+No errors were found on the image.
+
+== Creating a dirty image file ==
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=134217728 lazy_refcounts=on 
+wrote 512/512 bytes at offset 0
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+incompatible_features     0x1
+ERROR OFLAG_COPIED: offset=8000000000050000 refcount=0
+ERROR cluster 5 refcount=0 reference=1
+
+2 errors were found on the image.
+Data may be corrupted, or further writes to the image may corrupt it.
+
+== Read-only access must still work ==
+read 512/512 bytes at offset 0
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+== Repairing the image file must succeed ==
+ERROR OFLAG_COPIED: offset=8000000000050000 refcount=0
+Repairing cluster 5 refcount=0 reference=1
+No errors were found on the image.
+incompatible_features     0x0
+
+== Data should still be accessible after repair ==
+read 512/512 bytes at offset 0
+512 bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+*** done
tests/qemu-iotests/group +1 -0
index 7a2c92b..ebb5ca4 100644
@@ -45,3 +45,4 @@
 036 rw auto quick
 037 rw auto backing
 038 rw auto backing
+039 rw auto
Signature · 1 line
1.7.10