block: Fix drive-mirror with image size unaligned with granularity
cover block: Fix drive-mirror with image size unaligned with granularity
This fixes the bug introduced in e5b43573e28 and lately noticed by Kevin.
Fam Zheng (3):
mirror: Don't extend the last sub-chunk
iotests: Add iotests.image_size
iotests: Test case for drive-mirror with unaligned image size
block/mirror.c | 10 +++++++++ tests/qemu-iotests/109.out | 44 ++++++++++++++++++------------------- tests/qemu-iotests/152 | 51 +++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/152.out | 5 +++++ tests/qemu-iotests/group | 1 + tests/qemu-iotests/iotests.py | 6 +++++ 6 files changed, 95 insertions(+), 22 deletions(-) create mode 100644 tests/qemu-iotests/152 create mode 100644 tests/qemu-iotests/152.out
Signature · 1 line
2.8.0
PATCH 1/3 mirror: Don't extend the last sub-chunk
The last sub-chunk is rounded up to the copy granularity in the target image, resulting in a larger size than the source.
Add a function to clip the copied sectors to the end.
This undoes the "wrong" changes to tests/qemu-iotests/109.out in e5b43573e28. The remaining two offset changes are okay.
--- block/mirror.c | 10 ++++++++++ tests/qemu-iotests/109.out | 44 ++++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 22 deletions(-)
block/mirror.c +10 -0
@@ -205,6 +205,14 @@ static inline void mirror_wait_for_io(MirrorBlockJob *s) s->waiting_for_io = false; } +static inline void mirror_clip_sectors (MirrorBlockJob *s, +int64_t sector_num, +int *nb_sectors) +{ + *nb_sectors =MIN (*nb_sectors, + s->bdev_length /BDRV_SECTOR_SIZE - sector_num); +} +/* Submit async read while handling COW. * Returns: nb_sectors if no alignment is necessary, or * (new_end - sector_num) if tail is rounded up or down due to
@@ -240,6 +248,7 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num,mirror_wait_for_io (s); } +mirror_clip_sectors (s, sector_num, &nb_sectors);/* Allocate a MirrorOp that is used as an AIO callback. */ op =g_new (MirrorOp,1 ); op->s = s;
@@ -276,6 +285,7 @@ static void mirror_do_zero_or_discard(MirrorBlockJob *s, {MirrorOp *op; +mirror_clip_sectors (s, sector_num, &nb_sectors);/* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed * so the freeing in mirror_iteration_done is nop. */ op =g_new0 (MirrorOp,1 );
tests/qemu-iotests/109.out +22 -22
120 lines not on this page · raw message
Signature · 1 line
2.8.0
Re: [Qemu-devel] [PATCH 1/3] mirror: Don't extend the last sub-chunk
Quoted 35 lines · Fam Zheng
The last sub-chunk is rounded up to the copy granularity in the target image, resulting in a larger size than the source.
Add a function to clip the copied sectors to the end.
This undoes the "wrong" changes to tests/qemu-iotests/109.out in
e5b43573e28. The remaining two offset changes are okay.Reported-by Kevin Wolf <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block/mirror.c | 10 ++++++++++ tests/qemu-iotests/109.out | 44 ++++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 22 deletions(-)block/mirror.c +8
-0@@ -205,6 +205,14 @@ static inline void mirror_wait_for_io(MirrorBlockJob *s) s->waiting_for_io = false; } +static inline void mirror_clip_sectors (MirrorBlockJob *s, +int64_t sector_num, +int *nb_sectors) +{ + *nb_sectors =MIN (*nb_sectors, + s->bdev_length /BDRV_SECTOR_SIZE - sector_num); +} +/* Submit async read while handling COW. * Returns: nb_sectors if no alignment is necessary, or * (new_end - sector_num) if tail is rounded up or down due to
block/mirror.c static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num, · line 254
Quoted 7 lines
block/mirror.c +1
-0@@ -240,6 +248,7 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num,mirror_wait_for_io (s); } +mirror_clip_sectors (s, sector_num, &nb_sectors);/* Allocate a MirrorOp that is used as an AIO callback. */ op =g_new (MirrorOp,1 ); op->s = s;
I think you want to adjust the ret value, too. It doesn't really matter in practice (the caller just overshoots the end of the image instead of getting precisely to its end), but I wouldn't rely on this.
block/mirror.c static void mirror_do_zero_or_discard(MirrorBlockJob *s, · line 291
Quoted 8 lines
block/mirror.c +1
-0@@ -276,6 +285,7 @@ static void mirror_do_zero_or_discard(MirrorBlockJob *s, {MirrorOp *op; +mirror_clip_sectors (s, sector_num, &nb_sectors);/* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed * so the freeing in mirror_iteration_done is nop. */ op =g_new0 (MirrorOp,1 );
I think it would be best to just pull out the mirror_clip_sectors() from these functions and put it above the "switch (mirror_method)" in mirror_iteration().
We'd just have to make sure that mirror_cow_align() will not increase nb_sectors such that it points beyond the image end. It can do that, because and image's size does not need to be aligned to its cluster size. But just putting a mirror_clip_sectors() below the bdrv_round_to_clusters() call in mirror_cow_align() should fix that.
Then you wouldn't need to worry about fixing the ret value in mirror_do_read().
Max
Re: [Qemu-devel] [PATCH 1/3] mirror: Don't extend the last sub-chunk
Quoted 61 lines · Max Reitz
Quoted 35 lines · Fam Zheng
The last sub-chunk is rounded up to the copy granularity in the target image, resulting in a larger size than the source.
Add a function to clip the copied sectors to the end.
This undoes the "wrong" changes to tests/qemu-iotests/109.out in
e5b43573e28. The remaining two offset changes are okay.Reported-by Kevin Wolf <[email protected]>Signed-off-by Fam Zheng <[email protected]>--- block/mirror.c | 10 ++++++++++ tests/qemu-iotests/109.out | 44 ++++++++++++++++++++++---------------------- 2 files changed, 32 insertions(+), 22 deletions(-)block/mirror.c +8
-0@@ -205,6 +205,14 @@ static inline void mirror_wait_for_io(MirrorBlockJob *s) s->waiting_for_io = false; } +static inline void mirror_clip_sectors (MirrorBlockJob *s, +int64_t sector_num, +int *nb_sectors) +{ + *nb_sectors =MIN (*nb_sectors, + s->bdev_length /BDRV_SECTOR_SIZE - sector_num); +} +/* Submit async read while handling COW. * Returns: nb_sectors if no alignment is necessary, or * (new_end - sector_num) if tail is rounded up or down due to block/mirror.c static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num, · line 254
Quoted 7 lines
block/mirror.c +1
-0@@ -240,6 +248,7 @@ static int mirror_do_read(MirrorBlockJob *s, int64_t sector_num,mirror_wait_for_io (s); } +mirror_clip_sectors (s, sector_num, &nb_sectors);/* Allocate a MirrorOp that is used as an AIO callback. */ op =g_new (MirrorOp,1 ); op->s = s;I think you want to adjust the ret value, too. It doesn't really matter in practice (the caller just overshoots the end of the image instead of getting precisely to its end), but I wouldn't rely on this.
block/mirror.c static void mirror_do_zero_or_discard(MirrorBlockJob *s, · line 291
Quoted 8 lines
block/mirror.c +1
-0@@ -276,6 +285,7 @@ static void mirror_do_zero_or_discard(MirrorBlockJob *s, {MirrorOp *op; +mirror_clip_sectors (s, sector_num, &nb_sectors);/* Allocate a MirrorOp that is used as an AIO callback. The qiov is zeroed * so the freeing in mirror_iteration_done is nop. */ op =g_new0 (MirrorOp,1 );I think it would be best to just pull out the
mirror_clip_sectors()from these functions and put it above the "switch (mirror_method)" inmirror_iteration().
Quoted 8 lines
We'd just have to make sure that
mirror_cow_align()will not increasenb_sectorssuch that it points beyond the image end. It can do that, because and image's size does not need to be aligned to its cluster size. But just putting amirror_clip_sectors()below thebdrv_round_to_clusters()call inmirror_cow_align()should fix that.Then you wouldn't need to worry about fixing the ret value in
mirror_do_read().
Sounds good, will do this.
Thanks,
Fam
PATCH 2/3 iotests: Add iotests.image_size
In reply to Fam ZhengThis retrieves the virtual size of the image out of qemu-img info.
--- tests/qemu-iotests/iotests.py | 6 ++++++ 1 file changed, 6 insertions(+)
tests/qemu-iotests/iotests.py +6 -0
@@ -28,6 +28,7 @@ sys.path.append(os.path.join(os.path.dirname(__file__), '..', '..', 'scripts', ' import qmp import qtest import struct +import json # This will not work if arguments contain spaces but is necessary if we
@@ -103,6 +104,11 @@ def create_image(name, size): i = i + 512 file.close() +def image_size(img): + '''Return image's virtual size''' + r = qemu_img_pipe('info', '--output=json', '-f', imgfmt, img) + return json.loads(r)['virtual-size'] + test_dir_re = re.compile(r"%s" % test_dir) def filter_test_dir(msg): return test_dir_re.sub("TEST_DIR", msg)
Signature · 1 line
2.8.0
Re: [Qemu-devel] [PATCH 2/3] iotests: Add iotests.image_size
Quoted 6 lines · Fam Zheng
This retrieves the virtual size of the image out of qemu-img info.
Signed-off-by Fam Zheng <[email protected]>--- tests/qemu-iotests/iotests.py | 6 ++++++ 1 file changed, 6 insertions(+)
PATCH 3/3 iotests: Test case for drive-mirror with unaligned image size
In reply to Fam ZhengThis is the regression test for the virtual size mismatch issue between target and source images.
--- tests/qemu-iotests/152 | 51 ++++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/152.out | 5 +++++ tests/qemu-iotests/group | 1 + 3 files changed, 57 insertions(+) create mode 100644 tests/qemu-iotests/152 create mode 100644 tests/qemu-iotests/152.out
tests/qemu-iotests/152 new file +51 -0
@@ -0,0 +1,51 @@ +#!/usr/bin/env python +# +# Tests for drive-mirror with source size unaligned to granularity +# +# Copyright (C) 2016 Red Hat, Inc. +# +# 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/>. +# + +import os +import iotests +from iotests import qemu_img + +test_img = os.path.join(iotests.test_dir, 'test.img') +target_img = os.path.join(iotests.test_dir, 'target.img') + +class TestUnaligned(iotests.QMPTestCase): + def setUp(self): + qemu_img('create', '-f', iotests.imgfmt, test_img, '512') + self.vm = iotests.VM().add_drive(test_img) + self.vm.launch() + + def tearDown(self): + self.vm.shutdown() + os.remove(test_img) + try: + os.remove(target_img) + except OSError: + pass + + def test_unaligned(self): + result = self.vm.qmp('drive-mirror', device='drive0', sync='full', + granularity=65536, target=target_img) + self.complete_and_wait() + self.vm.shutdown() + self.assertEqual(iotests.image_size(test_img), iotests.image_size(target_img), + "Target size doesn't match source when granularity when unaligend") + +if __name__ == '__main__': + iotests.main(supported_fmts=['raw', 'qcow2'])
tests/qemu-iotests/152.out new file +5 -0
@@ -0,0 +1,5 @@ +. +---------------------------------------------------------------------- +Ran 1 tests + +OK
tests/qemu-iotests/group +1 -0
@@ -152,3 +152,4 @@ 148 rw auto quick 149 rw auto sudo 150 rw auto quick +152 rw auto quick
Signature · 1 line
2.8.0
Re: [Qemu-devel] [PATCH 3/3] iotests: Test case for drive-mirror with unaligned image size
Quoted 11 lines · Fam Zheng
This is the regression test for the virtual size mismatch issue between target and source images.
Signed-off-by Fam Zheng <[email protected]>--- tests/qemu-iotests/152 | 51 ++++++++++++++++++++++++++++++++++++++++++++++ tests/qemu-iotests/152.out | 5 +++++ tests/qemu-iotests/group | 1 + 3 files changed, 57 insertions(+) create mode 100644 tests/qemu-iotests/152 create mode 100644 tests/qemu-iotests/152.out