block: allow commit to unmap zero blocks
cover block: allow commit to unmap zero blocks
This patch series adds support for zero blocks in non-active commits. The first patch in the series contains the actual changes to the commit code, the second patch adds a test for the new functionality.
Vincent Vanlaer (2):
block: allow commit to unmap zero blocks
block: add test non-active commit with zeroed data
block/commit.c | 71 ++++++++++++++++++++++------ tests/qemu-iotests/315 | 95 ++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/315.out | 54 ++++++++++++++++++++++ 3 files changed, 207 insertions(+), 13 deletions(-) create mode 100755 tests/qemu-iotests/315 create mode 100644 tests/qemu-iotests/315.out
Signature · 1 line
2.42.0
PATCH 1/2 block: allow commit to unmap zero blocks
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 oother arbitrary data.
--- block/commit.c | 71 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 13 deletions(-)
block/commit.c +58 -13
@@ -12,9 +12,13 @@* */ +#include "bits/time.h" #include "qemu/osdep.h" #include "qemu/cutils.h" +#include "time.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"
@@ -126,6 +130,12 @@ static void commit_clean(Job *job)blk_unref (s->top); } +typedef enum CommitMethod { +COMMIT_METHOD_COPY , +COMMIT_METHOD_ZERO , +COMMIT_METHOD_IGNORE , +}CommitMethod ; + static int coroutine_fn commit_run(Job *job, Error **errp) {CommitBlockJob *s =container_of (job, CommitBlockJob, common.job);
@@ -156,8 +166,9 @@ 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.
@@ -167,21 +178,54 @@ 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 ); + } +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 && !(ret &BDRV_BLOCK_ALLOCATED )) { + commit_method =COMMIT_METHOD_IGNORE ; + }else if (ret >=0 && 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 ; + } + } + +if (ret >=0 ) { +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_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 : +abort (); } } +if (ret <0 ) {BlockErrorAction action = block_job_error_action(&s->common, s->on_error,
@@ -193,10 +237,11 @@ static int coroutine_fn commit_run(Job *job, Error **errp)continue ; } } +/* 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); } }
Signature · 1 line
2.42.0
Quoted 4 lines · 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_zeroesrather than writing them out as any oother arbitrary data.
Hi Vincent! Sorry for long delay.
Could you please split the commit into simpler parts?
Something like this:
1. refactor to use direct bdrv_co_common_block_status_above()
2. refactor to use CommitMethod (with two possible modes)
3. add new mode
(The idea is to split parts of no-logic-change refactoring from real logic changes and keep the latter smaller and clearer)
Quoted 127 lines
Signed-off-by Vincent Vanlaer <[email protected]>--- block/commit.c | 71 +++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 13 deletions(-)block/commit.c +58
-13@@ -12,9 +12,13 @@* */ +#include "bits/time.h" #include "qemu/osdep.h" #include "qemu/cutils.h" +#include "time.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" @@ -126,6 +130,12 @@ static void commit_clean(Job *job)blk_unref (s->top); } +typedef enum CommitMethod { +COMMIT_METHOD_COPY , +COMMIT_METHOD_ZERO , +COMMIT_METHOD_IGNORE , +}CommitMethod ; + static int coroutine_fn commit_run(Job *job, Error **errp) {CommitBlockJob *s =container_of (job, CommitBlockJob, common.job);@@ -156,8 +166,9 @@ 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. @@ -167,21 +178,54 @@ 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 ); + } +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 && !(ret &BDRV_BLOCK_ALLOCATED )) { + commit_method =COMMIT_METHOD_IGNORE ; + }else if (ret >=0 && 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 ; + } + } + +if (ret >=0 ) { +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_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 : +abort (); } } +if (ret <0 ) {BlockErrorAction action = block_job_error_action(&s->common, s->on_error,@@ -193,10 +237,11 @@ static int coroutine_fn commit_run(Job *job, Error **errp)continue ; } } +/* 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); } }
Signature · 2 lines
Best regards, Vladimir
PATCH 2/2 block: add test non-active commit with zeroed data
In reply to Vincent Vanlaer--- 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
tests/qemu-iotests/315 new file +95 -0
@@ -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 [email protected] + +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
tests/qemu-iotests/315.out new file +54 -0
54 lines not on this page · raw message
Signature · 1 line
2.42.0