Skip to contentSkip to conversation

qemu-img: Implement commit like QMP

PATCH Superseded Max Reitz 1 participant 6 messages Apr 10, 2014 Block layer

v3 of 14 Superseded Latest is v14 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

v3 cover qemu-img: Implement commit like QMP

qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. For the "commit" command, this is relatively easy, so implement it first (in the hope that indeed others will follow).

As qemu-img does not have access to QMP (due to QMP being intertwined with basically everything in qemu), we cannot directly use QMP, but at least use the functions the corresponding QMP commands are using (which would be "block-commit", in this case).

This series depends on "block-commit: speed is an optional parameter".

v3:
- Patch 1: fixed indentation of commit_start() declaration in block_int.h [Eric]
- Patch 2: moved out of this series which now depends on it [Kevin, in a sense]
- Patch 3 (now 2/5): wait for actual completion of the blockjob (CB called) and evaluate the return value [Kevin]
- Patch 4 (now 3/5): factor in the granularity of the blockjob [Kevin]
- Patch 5 (now 4/5):
- s/backing_file/base/g in documentation [Fam]
- s/unto/into/g, s/shall/will/ [Eric]
- s/filename/base/ in a certain spot [Fam]

v2 (mostly following Eric's comments):
- Patch 1:
- allow giving the granularity as 0 in order to enforce the default value
- use is_power_of_2() instead of manually inlining it
- use "if (!has_x) { x = DEFAULT_VALUE; }" (which is the common pattern in blockdev.c) instead of "x = has_x ? x : DEFAULT_VALUE;"
- Patch 2:
- "if (!has_x) { x = DEFAULT_VALUE; }" with a fixed condition
- Patch 3:
- don't use obsolete error_is_set()
- use bs->backing_hd instead of bdrv_find_base(), as the latter actually finds the very base of the backing chain which does not correspond to the current qemu-img commit behavior (which is to commit into the first backing file)
- Added patches 5 and 6

Max Reitz (5):
block-commit: Expose granularity
qemu-img: Implement commit like QMP
qemu-img: Enable progress output for commit
qemu-img: Specify backing file for commit
iotests: Commit tests for two-layer backing chains

 block/Makefile.objs        |    2 +-
 block/commit.c             |   16 +-
 block/mirror.c             |    4 +-
 blockdev.c                 |   22 +-
 include/block/block_int.h  |   10 +-
 qapi-schema.json           |    6 +-
 qemu-img-cmds.hx           |    4 +-
 qemu-img.c                 |  144 ++-
 qemu-img.texi              |    8 +-
 tests/qemu-iotests/020     |   57 +-
 tests/qemu-iotests/020.out | 2168 ++++++++++++++++++++++++++++++++++++++++++++
 11 files changed, 2397 insertions(+), 44 deletions(-)
Signature · 1 line
1.9.1
author
#1 lore raw

PATCH v3 1/5 block-commit: Expose granularity

Allow QMP users to manipulate the granularity used in the block-commit command.

---
 block/commit.c            | 16 +++++++++++++---
 block/mirror.c            |  4 ++--
 blockdev.c                | 22 ++++++++++++++++------
 include/block/block_int.h | 10 ++++++----
 qapi-schema.json          |  6 +++++-
 5 files changed, 42 insertions(+), 16 deletions(-)
block/commit.c +13 -3
index acec4ac..cf0e500 100644
@@ -37,6 +37,7 @@ typedef struct CommitBlockJob {
     BlockdevOnError on_error;
     int base_flags;
     int orig_overlay_flags;
+    int64_t granularity;
 } CommitBlockJob;
 
 static int coroutine_fn commit_populate(BlockDriverState *bs,
@@ -93,7 +94,7 @@ static void coroutine_fn commit_run(void *opaque)
     }
 
     end = s->common.len >> BDRV_SECTOR_BITS;
-    buf = qemu_blockalign(top, COMMIT_BUFFER_SIZE);
+    buf = qemu_blockalign(top, s->granularity);
 
     for (sector_num = 0; sector_num < end; sector_num += n) {
         uint64_t delay_ns = 0;
@@ -109,7 +110,7 @@ wait:
         }
         /* Copy if allocated above the base */
         ret = bdrv_is_allocated_above(top, base, sector_num,
-                                      COMMIT_BUFFER_SIZE / BDRV_SECTOR_SIZE,
+                                      s->granularity / BDRV_SECTOR_SIZE,
                                       &n);
         copy = (ret == 1);
         trace_commit_one_iteration(s, sector_num, n, ret);
@@ -180,7 +181,7 @@ static const BlockJobDriver commit_job_driver = {
 };
 
 void commit_start(BlockDriverState *bs, BlockDriverState *base,
-                  BlockDriverState *top, int64_t speed,
+                  BlockDriverState *top, int64_t speed, int64_t granularity,
                   BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
                   void *opaque, Error **errp)
 {
@@ -214,6 +215,13 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
     orig_base_flags    = bdrv_get_flags(base);
     orig_overlay_flags = bdrv_get_flags(overlay_bs);
 
+    if (!granularity) {
+        granularity = COMMIT_BUFFER_SIZE;
+    }
+
+    assert(granularity >= BDRV_SECTOR_SIZE);
+    assert(is_power_of_2(granularity));
+
     /* convert base & overlay_bs to r/w, if necessary */
     if (!(orig_base_flags & BDRV_O_RDWR)) {
         reopen_queue = bdrv_reopen_queue(reopen_queue, base,
@@ -244,6 +252,8 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
     s->base_flags          = orig_base_flags;
     s->orig_overlay_flags  = orig_overlay_flags;
 
+    s->granularity = granularity;
+
     s->on_error = on_error;
     s->common.co = qemu_coroutine_create(commit_run);
 
block/mirror.c +2 -2
index 0ef41f9..5b1ebb2 100644
@@ -632,7 +632,7 @@ void mirror_start(BlockDriverState *bs, BlockDriverState *target,
 }
 
 void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
-                         int64_t speed,
+                         int64_t speed, int64_t granularity,
                          BlockdevOnError on_error,
                          BlockDriverCompletionFunc *cb,
                          void *opaque, Error **errp)
@@ -674,7 +674,7 @@ void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
     }
 
     bdrv_ref(base);
-    mirror_start_job(bs, base, speed, 0, 0,
+    mirror_start_job(bs, base, speed, granularity, 0,
                      on_error, on_error, cb, opaque, &local_err,
                      &commit_active_job_driver, false, base);
     if (error_is_set(&local_err)) {
blockdev.c +16 -6
index 5dd01ea..9d7bd04 100644
@@ -1865,8 +1865,8 @@ void qmp_block_stream(const char *device, bool has_base,
 
 void qmp_block_commit(const char *device,
                       bool has_base, const char *base, const char *top,
-                      bool has_speed, int64_t speed,
-                      Error **errp)
+                      bool has_speed, int64_t speed, bool has_granularity,
+                      int64_t granularity, Error **errp)
 {
     BlockDriverState *bs;
     BlockDriverState *base_bs, *top_bs;
@@ -1879,6 +1879,16 @@ void qmp_block_commit(const char *device,
     if (!has_speed) {
         speed = 0;
     }
+    if (!has_granularity) {
+        granularity = 0;
+    }
+
+    if (granularity &&
+        (granularity < BDRV_SECTOR_SIZE || !is_power_of_2(granularity)))
+    {
+        error_set(errp, QERR_INVALID_PARAMETER, "granularity");
+        return;
+    }
 
     /* drain all i/o before commits */
     bdrv_drain_all();
@@ -1915,11 +1925,11 @@ void qmp_block_commit(const char *device,
     }
 
     if (top_bs == bs) {
-        commit_active_start(bs, base_bs, speed, on_error, block_job_cb,
-                            bs, &local_err);
+        commit_active_start(bs, base_bs, speed, granularity, on_error,
+                            block_job_cb, bs, &local_err);
     } else {
-        commit_start(bs, base_bs, top_bs, speed, on_error, block_job_cb, bs,
-                    &local_err);
+        commit_start(bs, base_bs, top_bs, speed, granularity, on_error,
+                     block_job_cb, bs, &local_err);
     }
     if (local_err != NULL) {
         error_propagate(errp, local_err);
include/block/block_int.h +6 -4
index cd5bc73..c01b4aa 100644
@@ -426,6 +426,7 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
  * @top: Top block device to be committed.
  * @base: Block device that will be written into, and become the new top.
  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
+ * @granularity: The granularity, in bytes, or 0 for a default value.
  * @on_error: The action to take upon error.
  * @cb: Completion function for the job.
  * @opaque: Opaque pointer value passed to @cb.
@@ -433,14 +434,15 @@ void stream_start(BlockDriverState *bs, BlockDriverState *base,
  *
  */
 void commit_start(BlockDriverState *bs, BlockDriverState *base,
-                 BlockDriverState *top, int64_t speed,
-                 BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
-                 void *opaque, Error **errp);
+                  BlockDriverState *top, int64_t speed, int64_t granularity,
+                  BlockdevOnError on_error, BlockDriverCompletionFunc *cb,
+                  void *opaque, Error **errp);
 /**
  * commit_active_start:
  * @bs: Active block device to be committed.
  * @base: Block device that will be written into, and become the new top.
  * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
+ * @granularity: The granularity, in bytes, or 0 for cluster size.
  * @on_error: The action to take upon error.
  * @cb: Completion function for the job.
  * @opaque: Opaque pointer value passed to @cb.
@@ -448,7 +450,7 @@ void commit_start(BlockDriverState *bs, BlockDriverState *base,
  *
  */
 void commit_active_start(BlockDriverState *bs, BlockDriverState *base,
-                         int64_t speed,
+                         int64_t speed, int64_t granularity,
                          BlockdevOnError on_error,
                          BlockDriverCompletionFunc *cb,
                          void *opaque, Error **errp);
qapi-schema.json +5 -1
index 391356f..151fad3 100644
@@ -2112,6 +2112,10 @@
 #
 # @speed:  #optional the maximum speed, in bytes per second
 #
+# @granularity: #optional the granularity to be used for the operation, in
+#               bytes; has to be a power of two and at least 512, or 0 for the
+#               default value (since 2.1)
+#
 # Returns: Nothing on success
 #          If commit or stream is already active on this device, DeviceInUse
 #          If @device does not exist, DeviceNotFound
@@ -2124,7 +2128,7 @@
 ##
 { 'command': 'block-commit',
   'data': { 'device': 'str', '*base': 'str', 'top': 'str',
-            '*speed': 'int' } }
+            '*speed': 'int', '*granularity': 'int' } }
 
 ##
 # @drive-backup
Signature · 1 line
1.9.1
author
#2 lore raw

PATCH v3 2/5 qemu-img: Implement commit like QMP

In reply to Max Reitz

qemu-img should use QMP commands whenever possible in order to ensure feature completeness of both online and offline image operations. As qemu-img itself has no access to QMP (since this would basically require just everything being linked into qemu-img), imitate QMP's implementation of block-commit by using commit_active_start() and then waiting for the block job to finish.

This new implementation does not empty the snapshot image, as opposed to the old implementation using bdrv_commit(). However, as QMP's block-commit apparently never did this and as qcow2 (which is probably qemu's standard image format) does not even implement the required function (bdrv_make_empty()), it does not seem necessary.

---
 block/Makefile.objs |  2 +-
 qemu-img.c          | 90 ++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 72 insertions(+), 20 deletions(-)
block/Makefile.objs +1 -1
index fd88c03..2c37e80 100644
@@ -9,6 +9,7 @@ block-obj-y += snapshot.o qapi.o
 block-obj-$(CONFIG_WIN32) += raw-win32.o win32-aio.o
 block-obj-$(CONFIG_POSIX) += raw-posix.o
 block-obj-$(CONFIG_LINUX_AIO) += linux-aio.o
+block-obj-y += mirror.o
 
 ifeq ($(CONFIG_POSIX),y)
 block-obj-y += nbd.o nbd-client.o sheepdog.o
@@ -22,7 +23,6 @@ endif
 
 common-obj-y += stream.o
 common-obj-y += commit.o
-common-obj-y += mirror.o
 common-obj-y += backup.o
 
 iscsi.o-cflags     := $(LIBISCSI_CFLAGS)
qemu-img.c +71 -19

118 lines not on this page · raw message

Signature · 1 line
1.9.1
author
#3 lore raw

PATCH v3 3/5 qemu-img: Enable progress output for commit

In reply to Max Reitz

Implement progress output for the commit command by querying the progress of the block job.

---
 qemu-img-cmds.hx |  4 ++--
 qemu-img.c       | 44 ++++++++++++++++++++++++++++++++++++++++++--
 qemu-img.texi    |  2 +-
 3 files changed, 45 insertions(+), 5 deletions(-)
qemu-img-cmds.hx +2 -2
index d029609..8bc55cd 100644
@@ -22,9 +22,9 @@ STEXI
 ETEXI
 
 DEF("commit", img_commit,
-    "commit [-q] [-f fmt] [-t cache] filename")
+    "commit [-q] [-f fmt] [-t cache] [-p] filename")
 STEXI
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
 ETEXI
 
 DEF("compare", img_compare,
qemu-img.c +42 -2

102 lines not on this page · raw message

qemu-img.texi +1 -1
index f84590e..1a9c08f 100644
@@ -140,7 +140,7 @@ this case. @var{backing_file} will never be modified unless you use the
 The size can also be specified using the @var{size} option with @code{-o},
 it doesn't need to be specified separately in this case.
 
-@item commit [-f @var{fmt}] [-t @var{cache}] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
 
 Commit the changes recorded in @var{filename} in its base image or backing file.
 If the backing file is smaller than the snapshot, then the backing file will be
Signature · 1 line
1.9.1
author
#4 lore raw

PATCH v3 4/5 qemu-img: Specify backing file for commit

In reply to Max Reitz

Introduce a new parameter for qemu-img commit which may be used to explicitly specify the backing file into which an image should be committed if the backing chain has more than a single layer.

---
 qemu-img-cmds.hx |  4 ++--
 qemu-img.c       | 22 +++++++++++++++-------
 qemu-img.texi    |  8 +++++++-
 3 files changed, 24 insertions(+), 10 deletions(-)
qemu-img-cmds.hx +2 -2
index 8bc55cd..e8a5d34 100644
@@ -22,9 +22,9 @@ STEXI
 ETEXI
 
 DEF("commit", img_commit,
-    "commit [-q] [-f fmt] [-t cache] [-p] filename")
+    "commit [-q] [-f fmt] [-t cache] [-b base] [-p] filename")
 STEXI
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{base}] [-p] @var{filename}
 ETEXI
 
 DEF("compare", img_compare,
qemu-img.c +15 -7
index 0c22b20..5052b6f 100644
@@ -751,15 +751,16 @@ static void run_block_job(BlockJob *job, struct CommonBlockJobCBInfo *cbi)
 static int img_commit(int argc, char **argv)
 {
     int c, ret, flags;
-    const char *filename, *fmt, *cache;
+    const char *filename, *fmt, *cache, *base;
     BlockDriverState *bs, *base_bs;
     bool progress = false, quiet = false;
     Error *local_err = NULL;
 
     fmt = NULL;
     cache = BDRV_DEFAULT_CACHE;
+    base = NULL;
     for(;;) {
-        c = getopt(argc, argv, "f:ht:qp");
+        c = getopt(argc, argv, "f:ht:b:qp");
         if (c == -1) {
             break;
         }
@@ -774,6 +775,9 @@ static int img_commit(int argc, char **argv)
         case 't':
             cache = optarg;
             break;
+        case 'b':
+            base = optarg;
+            break;
         case 'p':
             progress = true;
             break;
@@ -808,12 +812,16 @@ static int img_commit(int argc, char **argv)
     qemu_progress_init(progress, 1.f);
     qemu_progress_print(0.f, 100);
 
-    /* This is different from QMP, which by default uses the deepest file in the
-     * backing chain (i.e., the very base); however, the traditional behavior of
-     * qemu-img commit is using the immediate backing file. */
-    base_bs = bs->backing_hd;
+    if (base) {
+        base_bs = bdrv_find_backing_image(bs, base);
+    } else {
+        /* This is different from QMP, which by default uses the deepest file in
+         * the backing chain (i.e., the very base); however, the traditional
+         * behavior of qemu-img commit is using the immediate backing file. */
+        base_bs = bs->backing_hd;
+    }
     if (!base_bs) {
-        error_set(&local_err, QERR_BASE_NOT_FOUND, "NULL");
+        error_set(&local_err, QERR_BASE_NOT_FOUND, base ?: "NULL");
         goto done;
     }
 
qemu-img.texi +7 -1
index 1a9c08f..27b65ae 100644
@@ -140,7 +140,7 @@ this case. @var{backing_file} will never be modified unless you use the
 The size can also be specified using the @var{size} option with @code{-o},
 it doesn't need to be specified separately in this case.
 
-@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-p] @var{filename}
+@item commit [-q] [-f @var{fmt}] [-t @var{cache}] [-b @var{base}] [-p] @var{filename}
 
 Commit the changes recorded in @var{filename} in its base image or backing file.
 If the backing file is smaller than the snapshot, then the backing file will be
@@ -149,6 +149,12 @@ the backing file, the backing file will not be truncated.  If you want the
 backing file to match the size of the smaller snapshot, you can safely truncate
 it yourself once the commit operation successfully completes.
 
+If the backing chain of the given image file @var{filename} has more than one
+layer, the backing file into which the changes will be committed may be
+specified as @var{base} (which has to be part of @var{filename}'s backing
+chain). If @var{base} is not specified, the immediate backing file of the top
+image (which is @var{filename}) will be used.
+
 @item compare [-f @var{fmt}] [-F @var{fmt}] [-p] [-s] [-q] @var{filename1} @var{filename2}
 
 Check if two images have the same content. You can compare images with
Signature · 1 line
1.9.1
author
#5 lore raw

PATCH v3 5/5 iotests: Commit tests for two-layer backing chains

In reply to Max Reitz

Add tests to qemu-iotest 020 for committing through a two-layer backing file chain with qemu-img commit utilizing the -b option.

The newly added output in 020.out is basically the same as for the already existing single-layer pass, but with another image created and additional qemu-img map output.

---
 tests/qemu-iotests/020     |   57 +-
 tests/qemu-iotests/020.out | 2168 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 2223 insertions(+), 2 deletions(-)
tests/qemu-iotests/020 +55 -2
index 2f258dc..1e7327c 100755
@@ -32,6 +32,7 @@ _cleanup()
 {
 	_cleanup_test_img
     rm -f "$TEST_IMG.base"
+    rm -f "$TEST_IMG.itmd"
     rm -f "$TEST_IMG.orig"
 }
 trap "_cleanup; exit \$status" 0 1 2 3 15
@@ -56,6 +57,16 @@ fi
 
 TEST_OFFSETS="0 4294967296"
 
+# Three passes:
+#  0: Single-layer backing chain
+#  1: Two-layer backing chain, commit to upper backing file (default)
+#  2: Two-layer backing chain, commit to lower backing file
+for i in 0 1 2
+do
+
+echo "=== Test pass $i ==="
+echo
+
 _make_test_img 6G
 
 echo "Filling base image"
@@ -76,6 +87,13 @@ echo
 mv "$TEST_IMG" "$TEST_IMG.base"
 _make_test_img -b "$TEST_IMG.base" 6G
 
+if [ $i -gt 0 ]
+then
+    # Create intermediate image
+    mv "$TEST_IMG" "$TEST_IMG.itmd"
+    _make_test_img -b "$TEST_IMG.itmd" 6G
+fi
+
 echo "Filling test image"
 echo
 
@@ -88,8 +106,24 @@ for offset in $TEST_OFFSETS; do
 done
 _check_test_img
 
-$QEMU_IMG commit "$TEST_IMG"
-mv "$TEST_IMG.base" "$TEST_IMG"
+case $i in
+    0)
+        $QEMU_IMG commit "$TEST_IMG"
+        mv "$TEST_IMG.base" "$TEST_IMG"
+        ;;
+
+    1)
+        # -b "$TEST_IMG.itmd" should be the default (that is, committing to the
+        # first backing file in the chain)
+        $QEMU_IMG commit "$TEST_IMG"
+        mv "$TEST_IMG.itmd" "$TEST_IMG"
+        ;;
+
+    2)
+        $QEMU_IMG commit -b "$TEST_IMG.base" "$TEST_IMG"
+        mv "$TEST_IMG.base" "$TEST_IMG"
+        ;;
+esac
 
 echo "Reading from the backing file"
 echo
@@ -108,6 +142,25 @@ for offset in $TEST_OFFSETS; do
 done
 _check_test_img
 
+if [ $i -eq 0 ]
+then
+    continue
+fi
+
+# For two-layer chains, make sure the other image in the chain is unmodified
+case $i in
+    1)
+        $QEMU_IMG map "$TEST_IMG.base"
+        ;;
+
+    2)
+        mv "$TEST_IMG" "$TEST_IMG.base"
+        $QEMU_IMG map "$TEST_IMG.itmd"
+        ;;
+esac
+
+done
+
 # success, all done
 echo "*** done"
 rm -f $seq.full
tests/qemu-iotests/020.out +2168 -0

2,182 lines not on this page · raw message

Signature · 1 line
1.9.1