qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/4] block: allow commit to unmap zero blocks
@ 2024-07-13 21:56 Vincent Vanlaer
  2024-07-13 21:56 ` [PATCH v2 1/4] block: get type of block allocation in commit_run Vincent Vanlaer
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: Vincent Vanlaer @ 2024-07-13 21:56 UTC (permalink / raw)
  To: qemu-devel
  Cc: John Snow, Vladimir Sementsov-Ogievskiy, Kevin Wolf, qemu-block,
	Hanna Reitz, Vincent Vanlaer

This patch series adds support for zero blocks in non-active commits.
The first three patches in the series contains the actual changes to the
commit code, the last patch adds a test for the new functionality.

---

Changes since v1:
- split up the implementation in three separate commits
- removed accidentally left over includes from testing

Vincent Vanlaer (4):
  block: get type of block allocation in commit_run
  block: refactor commit_run for multiple write types
  block: allow commit to unmap zero blocks
  block: add test non-active commit with zeroed data

 block/commit.c             | 68 +++++++++++++++++++++------
 tests/qemu-iotests/315     | 95 ++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/315.out | 54 ++++++++++++++++++++++
 3 files changed, 204 insertions(+), 13 deletions(-)
 create mode 100755 tests/qemu-iotests/315
 create mode 100644 tests/qemu-iotests/315.out

-- 
2.44.1



^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH v2 1/4] block: get type of block allocation in commit_run
  2024-07-13 21:56 [PATCH v2 0/4] block: allow commit to unmap zero blocks Vincent Vanlaer
@ 2024-07-13 21:56 ` Vincent Vanlaer
  2024-08-02 10:09   ` Vladimir Sementsov-Ogievskiy
  2024-07-13 21:56 ` [PATCH v2 2/4] block: refactor commit_run for multiple write types Vincent Vanlaer
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 9+ messages in thread
From: Vincent Vanlaer @ 2024-07-13 21:56 UTC (permalink / raw)
  To: qemu-devel
  Cc: John Snow, Vladimir Sementsov-Ogievskiy, Kevin Wolf, qemu-block,
	Hanna Reitz, Vincent Vanlaer

bdrv_co_common_block_status_above not only returns whether the block is
allocated, but also if it contains zeroes.

Signed-off-by: Vincent Vanlaer <libvirt-e6954efa@volkihar.be>
---
 block/commit.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/block/commit.c b/block/commit.c
index 7c3fdcb0ca..8dee25b313 100644
--- a/block/commit.c
+++ b/block/commit.c
@@ -15,6 +15,8 @@
 #include "qemu/osdep.h"
 #include "qemu/cutils.h"
 #include "trace.h"
+#include "block/block-common.h"
+#include "block/coroutines.h"
 #include "block/block_int.h"
 #include "block/blockjob_int.h"
 #include "qapi/error.h"
@@ -167,9 +169,13 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
             break;
         }
         /* Copy if allocated above the base */
-        ret = blk_co_is_allocated_above(s->top, s->base_overlay, true,
-                                        offset, COMMIT_BUFFER_SIZE, &n);
-        copy = (ret > 0);
+        WITH_GRAPH_RDLOCK_GUARD() {
+            ret = bdrv_co_common_block_status_above(blk_bs(s->top),
+                s->base_overlay, true, true, offset, COMMIT_BUFFER_SIZE,
+                &n, NULL, NULL, NULL);
+        }
+
+        copy = (ret >= 0 && ret & BDRV_BLOCK_ALLOCATED);
         trace_commit_one_iteration(s, offset, n, ret);
         if (copy) {
             assert(n < SIZE_MAX);
-- 
2.44.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 2/4] block: refactor commit_run for multiple write types
  2024-07-13 21:56 [PATCH v2 0/4] block: allow commit to unmap zero blocks Vincent Vanlaer
  2024-07-13 21:56 ` [PATCH v2 1/4] block: get type of block allocation in commit_run Vincent Vanlaer
@ 2024-07-13 21:56 ` Vincent Vanlaer
  2024-08-02 10:22   ` Vladimir Sementsov-Ogievskiy
  2024-07-13 21:56 ` [PATCH v2 3/4] block: allow commit to unmap zero blocks Vincent Vanlaer
  2024-07-13 21:56 ` [PATCH v2 4/4] block: add test non-active commit with zeroed data Vincent Vanlaer
  3 siblings, 1 reply; 9+ messages in thread
From: Vincent Vanlaer @ 2024-07-13 21:56 UTC (permalink / raw)
  To: qemu-devel
  Cc: John Snow, Vladimir Sementsov-Ogievskiy, Kevin Wolf, qemu-block,
	Hanna Reitz, Vincent Vanlaer

Signed-off-by: Vincent Vanlaer <libvirt-e6954efa@volkihar.be>
---
 block/commit.c | 39 ++++++++++++++++++++++++++++-----------
 1 file changed, 28 insertions(+), 11 deletions(-)

diff --git a/block/commit.c b/block/commit.c
index 8dee25b313..fb54fc9560 100644
--- a/block/commit.c
+++ b/block/commit.c
@@ -128,6 +128,11 @@ static void commit_clean(Job *job)
     blk_unref(s->top);
 }
 
+typedef enum CommitMethod {
+    COMMIT_METHOD_COPY,
+    COMMIT_METHOD_IGNORE,
+} CommitMethod;
+
 static int coroutine_fn commit_run(Job *job, Error **errp)
 {
     CommitBlockJob *s = container_of(job, CommitBlockJob, common.job);
@@ -158,8 +163,8 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
     buf = blk_blockalign(s->top, COMMIT_BUFFER_SIZE);
 
     for (offset = 0; offset < len; offset += n) {
-        bool copy;
         bool error_in_source = true;
+        CommitMethod commit_method = COMMIT_METHOD_COPY;
 
         /* Note that even when no rate limit is applied we need to yield
          * with no pending I/O here so that bdrv_drain_all() returns.
@@ -175,19 +180,31 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
                 &n, NULL, NULL, NULL);
         }
 
-        copy = (ret >= 0 && ret & BDRV_BLOCK_ALLOCATED);
         trace_commit_one_iteration(s, offset, n, ret);
-        if (copy) {
-            assert(n < SIZE_MAX);
-
-            ret = blk_co_pread(s->top, offset, n, buf, 0);
-            if (ret >= 0) {
-                ret = blk_co_pwrite(s->base, offset, n, buf, 0);
-                if (ret < 0) {
-                    error_in_source = false;
+
+        if (ret >= 0) {
+            if (!(ret & BDRV_BLOCK_ALLOCATED)) {
+                commit_method = COMMIT_METHOD_IGNORE;
+            }
+
+            switch (commit_method) {
+            case COMMIT_METHOD_COPY:
+                assert(n < SIZE_MAX);
+                ret = blk_co_pread(s->top, offset, n, buf, 0);
+                if (ret >= 0) {
+                    ret = blk_co_pwrite(s->base, offset, n, buf, 0);
+                    if (ret < 0) {
+                        error_in_source = false;
+                    }
                 }
+                break;
+            case COMMIT_METHOD_IGNORE:
+                break;
+            default:
+                abort();
             }
         }
+
         if (ret < 0) {
             BlockErrorAction action =
                 block_job_error_action(&s->common, s->on_error,
@@ -202,7 +219,7 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
         /* Publish progress */
         job_progress_update(&s->common.job, n);
 
-        if (copy) {
+        if (commit_method == COMMIT_METHOD_COPY) {
             block_job_ratelimit_processed_bytes(&s->common, n);
         }
     }
-- 
2.44.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 3/4] block: allow commit to unmap zero blocks
  2024-07-13 21:56 [PATCH v2 0/4] block: allow commit to unmap zero blocks Vincent Vanlaer
  2024-07-13 21:56 ` [PATCH v2 1/4] block: get type of block allocation in commit_run Vincent Vanlaer
  2024-07-13 21:56 ` [PATCH v2 2/4] block: refactor commit_run for multiple write types Vincent Vanlaer
@ 2024-07-13 21:56 ` Vincent Vanlaer
  2024-08-02 10:58   ` Vladimir Sementsov-Ogievskiy
  2024-07-13 21:56 ` [PATCH v2 4/4] block: add test non-active commit with zeroed data Vincent Vanlaer
  3 siblings, 1 reply; 9+ messages in thread
From: Vincent Vanlaer @ 2024-07-13 21:56 UTC (permalink / raw)
  To: qemu-devel
  Cc: John Snow, Vladimir Sementsov-Ogievskiy, Kevin Wolf, qemu-block,
	Hanna Reitz, Vincent Vanlaer

Non-active block commits do not discard blocks only containing zeros,
causing images to lose sparseness after the commit. This commit fixes
that by writing zero blocks using blk_co_pwrite_zeroes rather than
writing them out as any other arbitrary data.

Signed-off-by: Vincent Vanlaer <libvirt-e6954efa@volkihar.be>
---
 block/commit.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/block/commit.c b/block/commit.c
index fb54fc9560..6ce30927ac 100644
--- a/block/commit.c
+++ b/block/commit.c
@@ -130,6 +130,7 @@ static void commit_clean(Job *job)
 
 typedef enum CommitMethod {
     COMMIT_METHOD_COPY,
+    COMMIT_METHOD_ZERO,
     COMMIT_METHOD_IGNORE,
 } CommitMethod;
 
@@ -185,6 +186,18 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
         if (ret >= 0) {
             if (!(ret & BDRV_BLOCK_ALLOCATED)) {
                 commit_method = COMMIT_METHOD_IGNORE;
+            } else if (ret & BDRV_BLOCK_ZERO) {
+                int64_t target_offset;
+                int64_t target_bytes;
+                WITH_GRAPH_RDLOCK_GUARD() {
+                    bdrv_round_to_subclusters(s->base_bs, offset, n,
+                                           &target_offset, &target_bytes);
+                }
+
+                if (target_offset == offset &&
+                    target_bytes == n) {
+                    commit_method = COMMIT_METHOD_ZERO;
+                }
             }
 
             switch (commit_method) {
@@ -198,6 +211,11 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
                     }
                 }
                 break;
+            case COMMIT_METHOD_ZERO:
+                ret = blk_co_pwrite_zeroes(s->base, offset, n,
+                    BDRV_REQ_MAY_UNMAP);
+                error_in_source = false;
+                break;
             case COMMIT_METHOD_IGNORE:
                 break;
             default:
@@ -216,6 +234,7 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
                 continue;
             }
         }
+
         /* Publish progress */
         job_progress_update(&s->common.job, n);
 
-- 
2.44.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH v2 4/4] block: add test non-active commit with zeroed data
  2024-07-13 21:56 [PATCH v2 0/4] block: allow commit to unmap zero blocks Vincent Vanlaer
                   ` (2 preceding siblings ...)
  2024-07-13 21:56 ` [PATCH v2 3/4] block: allow commit to unmap zero blocks Vincent Vanlaer
@ 2024-07-13 21:56 ` Vincent Vanlaer
  3 siblings, 0 replies; 9+ messages in thread
From: Vincent Vanlaer @ 2024-07-13 21:56 UTC (permalink / raw)
  To: qemu-devel
  Cc: John Snow, Vladimir Sementsov-Ogievskiy, Kevin Wolf, qemu-block,
	Hanna Reitz, Vincent Vanlaer

Signed-off-by: Vincent Vanlaer <libvirt-e6954efa@volkihar.be>
---
 tests/qemu-iotests/315     | 95 ++++++++++++++++++++++++++++++++++++++
 tests/qemu-iotests/315.out | 54 ++++++++++++++++++++++
 2 files changed, 149 insertions(+)
 create mode 100755 tests/qemu-iotests/315
 create mode 100644 tests/qemu-iotests/315.out

diff --git a/tests/qemu-iotests/315 b/tests/qemu-iotests/315
new file mode 100755
index 0000000000..84865f8001
--- /dev/null
+++ b/tests/qemu-iotests/315
@@ -0,0 +1,95 @@
+#!/usr/bin/env bash
+# group: rw quick
+#
+# Test for commit of discarded blocks
+#
+# This tests committing a live snapshot where some of the blocks that
+# are present in the base image are discarded in the intermediate image.
+# This intends to check that these blocks are also discarded in the base
+# image after the commit.
+#
+# Copyright (C) 2024 Vincent Vanlaer.
+#
+# 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
+owner=libvirt-e6954efa@volkihar.be
+
+seq=`basename $0`
+echo "QA output created by $seq"
+
+status=1	# failure is the default!
+
+_cleanup()
+{
+    _cleanup_qemu
+    _rm_test_img "${TEST_IMG}.base"
+    _rm_test_img "${TEST_IMG}.mid"
+    _cleanup_test_img
+}
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+# get standard environment, filters and checks
+. ./common.rc
+. ./common.filter
+. ./common.qemu
+
+_supported_fmt qcow2
+_supported_proto file
+
+size="1M"
+
+TEST_IMG="$TEST_IMG.base" _make_test_img $size
+TEST_IMG="$TEST_IMG.mid" _make_test_img -b "$TEST_IMG.base" -F $IMGFMT $size
+_make_test_img -b "${TEST_IMG}.mid" -F $IMGFMT $size
+
+$QEMU_IO -c "write -P 0x01 64k 128k" "$TEST_IMG.base" | _filter_qemu_io
+$QEMU_IO -c "discard 64k 64k" "$TEST_IMG.mid" | _filter_qemu_io
+
+echo
+echo "=== Base image info before commit ==="
+TEST_IMG="${TEST_IMG}.base" _img_info | _filter_img_info
+$QEMU_IMG map --output=json "$TEST_IMG.base" | _filter_qemu_img_map
+
+echo
+echo "=== Middle image info before commit ==="
+TEST_IMG="${TEST_IMG}.mid" _img_info | _filter_img_info
+$QEMU_IMG map --output=json "$TEST_IMG.mid" | _filter_qemu_img_map
+
+echo
+echo === Running QEMU Live Commit Test ===
+echo
+
+qemu_comm_method="qmp"
+_launch_qemu -drive file="${TEST_IMG}",if=virtio,id=test
+h=$QEMU_HANDLE
+
+_send_qemu_cmd $h "{ 'execute': 'qmp_capabilities' }" "return"
+
+_send_qemu_cmd $h "{ 'execute': 'block-commit',
+                                 'arguments': { 'device': 'test',
+                                 'top': '"${TEST_IMG}.mid"',
+                                 'base': '"${TEST_IMG}.base"'} }" '"status": "null"'
+
+_cleanup_qemu
+
+echo
+echo "=== Base image info after commit ==="
+TEST_IMG="${TEST_IMG}.base" _img_info | _filter_img_info
+$QEMU_IMG map --output=json "$TEST_IMG.base" | _filter_qemu_img_map
+
+# success, all done
+echo "*** done"
+rm -f $seq.full
+status=0
diff --git a/tests/qemu-iotests/315.out b/tests/qemu-iotests/315.out
new file mode 100644
index 0000000000..146ec117fb
--- /dev/null
+++ b/tests/qemu-iotests/315.out
@@ -0,0 +1,54 @@
+QA output created by 315
+Formatting 'TEST_DIR/t.IMGFMT.base', fmt=IMGFMT size=1048576
+Formatting 'TEST_DIR/t.IMGFMT.mid', fmt=IMGFMT size=1048576 backing_file=TEST_DIR/t.IMGFMT.base backing_fmt=IMGFMT
+Formatting 'TEST_DIR/t.IMGFMT', fmt=IMGFMT size=1048576 backing_file=TEST_DIR/t.IMGFMT.mid backing_fmt=IMGFMT
+wrote 131072/131072 bytes at offset 65536
+128 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+discard 65536/65536 bytes at offset 65536
+64 KiB, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+
+=== Base image info before commit ===
+image: TEST_DIR/t.IMGFMT.base
+file format: IMGFMT
+virtual size: 1 MiB (1048576 bytes)
+[{ "start": 0, "length": 65536, "depth": 0, "present": false, "zero": true, "data": false, "compressed": false},
+{ "start": 65536, "length": 131072, "depth": 0, "present": true, "zero": false, "data": true, "compressed": false, "offset": OFFSET},
+{ "start": 196608, "length": 851968, "depth": 0, "present": false, "zero": true, "data": false, "compressed": false}]
+
+=== Middle image info before commit ===
+image: TEST_DIR/t.IMGFMT.mid
+file format: IMGFMT
+virtual size: 1 MiB (1048576 bytes)
+backing file: TEST_DIR/t.IMGFMT.base
+backing file format: IMGFMT
+[{ "start": 0, "length": 65536, "depth": 1, "present": false, "zero": true, "data": false, "compressed": false},
+{ "start": 65536, "length": 65536, "depth": 0, "present": true, "zero": true, "data": false, "compressed": false},
+{ "start": 131072, "length": 65536, "depth": 1, "present": true, "zero": false, "data": true, "compressed": false, "offset": OFFSET},
+{ "start": 196608, "length": 851968, "depth": 1, "present": false, "zero": true, "data": false, "compressed": false}]
+
+=== Running QEMU Live Commit Test ===
+
+{ 'execute': 'qmp_capabilities' }
+{"return": {}}
+{ 'execute': 'block-commit',
+                                 'arguments': { 'device': 'test',
+                                 'top': 'TEST_DIR/t.IMGFMT.mid',
+                                 'base': 'TEST_DIR/t.IMGFMT.base'} }
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "JOB_STATUS_CHANGE", "data": {"status": "created", "id": "test"}}
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "JOB_STATUS_CHANGE", "data": {"status": "running", "id": "test"}}
+{"return": {}}
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "JOB_STATUS_CHANGE", "data": {"status": "waiting", "id": "test"}}
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "JOB_STATUS_CHANGE", "data": {"status": "pending", "id": "test"}}
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "BLOCK_JOB_COMPLETED", "data": {"device": "test", "len": 1048576, "offset": 1048576, "speed": 0, "type": "commit"}}
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "JOB_STATUS_CHANGE", "data": {"status": "concluded", "id": "test"}}
+{"timestamp": {"seconds":  TIMESTAMP, "microseconds":  TIMESTAMP}, "event": "JOB_STATUS_CHANGE", "data": {"status": "null", "id": "test"}}
+
+=== Base image info after commit ===
+image: TEST_DIR/t.IMGFMT.base
+file format: IMGFMT
+virtual size: 1 MiB (1048576 bytes)
+[{ "start": 0, "length": 65536, "depth": 0, "present": false, "zero": true, "data": false, "compressed": false},
+{ "start": 65536, "length": 65536, "depth": 0, "present": true, "zero": true, "data": false, "compressed": false},
+{ "start": 131072, "length": 65536, "depth": 0, "present": true, "zero": false, "data": true, "compressed": false, "offset": OFFSET},
+{ "start": 196608, "length": 851968, "depth": 0, "present": false, "zero": true, "data": false, "compressed": false}]
+*** done
-- 
2.44.1



^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 1/4] block: get type of block allocation in commit_run
  2024-07-13 21:56 ` [PATCH v2 1/4] block: get type of block allocation in commit_run Vincent Vanlaer
@ 2024-08-02 10:09   ` Vladimir Sementsov-Ogievskiy
  0 siblings, 0 replies; 9+ messages in thread
From: Vladimir Sementsov-Ogievskiy @ 2024-08-02 10:09 UTC (permalink / raw)
  To: Vincent Vanlaer, qemu-devel
  Cc: John Snow, Kevin Wolf, qemu-block, Hanna Reitz

On 14.07.24 00:56, Vincent Vanlaer wrote:
> bdrv_co_common_block_status_above not only returns whether the block is
> allocated, but also if it contains zeroes.
> 
> Signed-off-by: Vincent Vanlaer<libvirt-e6954efa@volkihar.be>

Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>

-- 
Best regards,
Vladimir



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 2/4] block: refactor commit_run for multiple write types
  2024-07-13 21:56 ` [PATCH v2 2/4] block: refactor commit_run for multiple write types Vincent Vanlaer
@ 2024-08-02 10:22   ` Vladimir Sementsov-Ogievskiy
  0 siblings, 0 replies; 9+ messages in thread
From: Vladimir Sementsov-Ogievskiy @ 2024-08-02 10:22 UTC (permalink / raw)
  To: Vincent Vanlaer, qemu-devel
  Cc: John Snow, Kevin Wolf, qemu-block, Hanna Reitz

On 14.07.24 00:56, Vincent Vanlaer wrote:
> Signed-off-by: Vincent Vanlaer<libvirt-e6954efa@volkihar.be>

Reviewed-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>

Honestly, I don't like this (mostly preexisting, but your patch make the problem more obvious) code for its "nested success path"

if (ret >= 0) {
   ret = ...
   if (ret >= 0) {

    ...


That's because we have the same complex error handling for all these errors. If refactor the commit_run(), I'd move get-block-status together with "if (copy)" block to separate function commmit_iteration(), which would have more readable style of error reporting, like:

ret = ...
if (ret < 0) {
    return ret;
}

ret = ...
if (ret < 0) {
    return ret;
}

  
-- 
Best regards,
Vladimir



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 3/4] block: allow commit to unmap zero blocks
  2024-07-13 21:56 ` [PATCH v2 3/4] block: allow commit to unmap zero blocks Vincent Vanlaer
@ 2024-08-02 10:58   ` Vladimir Sementsov-Ogievskiy
  2024-09-01 10:25     ` Vincent Vanlaer
  0 siblings, 1 reply; 9+ messages in thread
From: Vladimir Sementsov-Ogievskiy @ 2024-08-02 10:58 UTC (permalink / raw)
  To: Vincent Vanlaer, qemu-devel
  Cc: John Snow, Kevin Wolf, qemu-block, Hanna Reitz

On 14.07.24 00:56, Vincent Vanlaer wrote:
> Non-active block commits do not discard blocks only containing zeros,
> causing images to lose sparseness after the commit. This commit fixes
> that by writing zero blocks using blk_co_pwrite_zeroes rather than
> writing them out as any other arbitrary data.
> 
> Signed-off-by: Vincent Vanlaer <libvirt-e6954efa@volkihar.be>
> ---
>   block/commit.c | 19 +++++++++++++++++++
>   1 file changed, 19 insertions(+)
> 
> diff --git a/block/commit.c b/block/commit.c
> index fb54fc9560..6ce30927ac 100644
> --- a/block/commit.c
> +++ b/block/commit.c
> @@ -130,6 +130,7 @@ static void commit_clean(Job *job)
>   
>   typedef enum CommitMethod {
>       COMMIT_METHOD_COPY,
> +    COMMIT_METHOD_ZERO,
>       COMMIT_METHOD_IGNORE,
>   } CommitMethod;
>   
> @@ -185,6 +186,18 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
>           if (ret >= 0) {
>               if (!(ret & BDRV_BLOCK_ALLOCATED)) {
>                   commit_method = COMMIT_METHOD_IGNORE;
> +            } else if (ret & BDRV_BLOCK_ZERO) {
> +                int64_t target_offset;
> +                int64_t target_bytes;
> +                WITH_GRAPH_RDLOCK_GUARD() {
> +                    bdrv_round_to_subclusters(s->base_bs, offset, n,
> +                                           &target_offset, &target_bytes);

indentation broken

> +                }
> +
> +                if (target_offset == offset &&
> +                    target_bytes == n) {
> +                    commit_method = COMMIT_METHOD_ZERO;

Why this is needed? Could we blindly do write-zeroes at original (offset, n)? Underlying logic would use any possiblity to write zeroes effectively, and unaligned tails (if any) would be written as data.

> +                }
>               }
>   
>               switch (commit_method) {
> @@ -198,6 +211,11 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
>                       }
>                   }
>                   break;
> +            case COMMIT_METHOD_ZERO:
> +                ret = blk_co_pwrite_zeroes(s->base, offset, n,
> +                    BDRV_REQ_MAY_UNMAP);
> +                error_in_source = false;
> +                break;
>               case COMMIT_METHOD_IGNORE:
>                   break;
>               default:
> @@ -216,6 +234,7 @@ static int coroutine_fn commit_run(Job *job, Error **errp)
>                   continue;
>               }
>           }
> +

extra unrelated hunk for style, I'd drop it

>           /* Publish progress */
>           job_progress_update(&s->common.job, n);
>   

-- 
Best regards,
Vladimir



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH v2 3/4] block: allow commit to unmap zero blocks
  2024-08-02 10:58   ` Vladimir Sementsov-Ogievskiy
@ 2024-09-01 10:25     ` Vincent Vanlaer
  0 siblings, 0 replies; 9+ messages in thread
From: Vincent Vanlaer @ 2024-09-01 10:25 UTC (permalink / raw)
  To: Vladimir Sementsov-Ogievskiy, qemu-devel
  Cc: John Snow, Kevin Wolf, qemu-block, Hanna Reitz

On 2/08/2024 12:58, Vladimir Sementsov-Ogievskiy wrote:
> On 14.07.24 00:56, Vincent Vanlaer wrote:
>> Non-active block commits do not discard blocks only containing zeros,
>> causing images to lose sparseness after the commit. This commit fixes
>> that by writing zero blocks using blk_co_pwrite_zeroes rather than
>> writing them out as any other arbitrary data.
>>
>> Signed-off-by: Vincent Vanlaer <libvirt-e6954efa@volkihar.be>
>> ---
>>   block/commit.c | 19 +++++++++++++++++++
>>   1 file changed, 19 insertions(+)
>>
>> diff --git a/block/commit.c b/block/commit.c
>> index fb54fc9560..6ce30927ac 100644
>> --- a/block/commit.c
>> +++ b/block/commit.c
>> @@ -130,6 +130,7 @@ static void commit_clean(Job *job)
>>     typedef enum CommitMethod {
>>       COMMIT_METHOD_COPY,
>> +    COMMIT_METHOD_ZERO,
>>       COMMIT_METHOD_IGNORE,
>>   } CommitMethod;
>>   @@ -185,6 +186,18 @@ static int coroutine_fn commit_run(Job *job, 
>> Error **errp)
>>           if (ret >= 0) {
>>               if (!(ret & BDRV_BLOCK_ALLOCATED)) {
>>                   commit_method = COMMIT_METHOD_IGNORE;
>> +            } else if (ret & BDRV_BLOCK_ZERO) {
>> +                int64_t target_offset;
>> +                int64_t target_bytes;
>> +                WITH_GRAPH_RDLOCK_GUARD() {
>> +                    bdrv_round_to_subclusters(s->base_bs, offset, n,
>> +                                           &target_offset, 
>> &target_bytes);
>
> indentation broken
>
>> +                }
>> +
>> +                if (target_offset == offset &&
>> +                    target_bytes == n) {
>> +                    commit_method = COMMIT_METHOD_ZERO;
>
> Why this is needed? Could we blindly do write-zeroes at original 
> (offset, n)? Underlying logic would use any possiblity to write zeroes 
> effectively, and unaligned tails (if any) would be written as data.
>
This originates from the mirroring code. I did some testing and it 
indeed is not necessary in this case. Letting it the underlying code 
handle it also simplifies this code quite a bit.

>> +                }
>>               }
>>                 switch (commit_method) {
>> @@ -198,6 +211,11 @@ static int coroutine_fn commit_run(Job *job, 
>> Error **errp)
>>                       }
>>                   }
>>                   break;
>> +            case COMMIT_METHOD_ZERO:
>> +                ret = blk_co_pwrite_zeroes(s->base, offset, n,
>> +                    BDRV_REQ_MAY_UNMAP);
>> +                error_in_source = false;
>> +                break;
>>               case COMMIT_METHOD_IGNORE:
>>                   break;
>>               default:
>> @@ -216,6 +234,7 @@ static int coroutine_fn commit_run(Job *job, 
>> Error **errp)
>>                   continue;
>>               }
>>           }
>> +
>
> extra unrelated hunk for style, I'd drop it
>
>>           /* Publish progress */
>>           job_progress_update(&s->common.job, n);
>



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-09-01 10:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-07-13 21:56 [PATCH v2 0/4] block: allow commit to unmap zero blocks Vincent Vanlaer
2024-07-13 21:56 ` [PATCH v2 1/4] block: get type of block allocation in commit_run Vincent Vanlaer
2024-08-02 10:09   ` Vladimir Sementsov-Ogievskiy
2024-07-13 21:56 ` [PATCH v2 2/4] block: refactor commit_run for multiple write types Vincent Vanlaer
2024-08-02 10:22   ` Vladimir Sementsov-Ogievskiy
2024-07-13 21:56 ` [PATCH v2 3/4] block: allow commit to unmap zero blocks Vincent Vanlaer
2024-08-02 10:58   ` Vladimir Sementsov-Ogievskiy
2024-09-01 10:25     ` Vincent Vanlaer
2024-07-13 21:56 ` [PATCH v2 4/4] block: add test non-active commit with zeroed data Vincent Vanlaer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).