Skip to contentSkip to conversation

mirror: Fix behavior for zero byte image

PATCH Superseded Fam Zheng 3 participants 11 messages Jun 9, 2014 – Jun 24, 2014 Block layer

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Fam Zheng

author
#0 lore raw

v2 cover mirror: Fix behavior for zero byte image

The current behavior of mirroring zero byte image is slightly different from non-zero image: the BLOCK_JOB_READY event is skipped and job is completed immediately. This is not a big problem for human user but only makes management software harder to write. Since we are focusing on an good API instead of UI, let's make the behavior more consistent and predictable.

The first patch fixes the behavior. The following two patches add test case for the involved code path.

Thanks for Eric Blake to report this!

v2: Address Stefan's comments.

- Added patch 01: block_job_yield.
- Use block_job_yield in 02.
- Fix test case updates.

Thanks to Stefan, Paolo and Eric for reviewing!

Fam

Fam Zheng (4):
blockjob: Add block_job_yield()
mirror: Go through ready -> complete process for 0 len image
qemu-iotests: Test BLOCK_JOB_READY event for 0Kb image active commit
qemu-iotests: Test 0-length image for mirror

 block/mirror.c             | 11 ++++++++++-
 blockjob.c                 | 14 ++++++++++++++
 include/block/blockjob.h   |  8 ++++++++
 tests/qemu-iotests/040     | 12 +++++++++---
 tests/qemu-iotests/040.out |  4 ++--
 tests/qemu-iotests/041     |  9 ++++++---
 tests/qemu-iotests/041.out |  4 ++--
 7 files changed, 51 insertions(+), 11 deletions(-)
Signature · 1 line
2.0.0
author
#1 lore raw

PATCH v2 1/4 blockjob: Add block_job_yield()

This will unset busy flag and put coroutine to sleep, can be used to wait for QMP complete/cancel.

---
 blockjob.c               | 14 ++++++++++++++
 include/block/blockjob.h |  8 ++++++++
 2 files changed, 22 insertions(+)
blockjob.c +14 -0
index 7d84ca1..a85146a 100644
@@ -210,6 +210,20 @@ void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns)
     job->busy = true;
 }
 
+void block_job_yield(BlockJob *job)
+{
+    assert(job->busy);
+
+    /* Check cancellation *before* setting busy = false, too!  */
+    if (block_job_is_cancelled(job)) {
+        return;
+    }
+
+    job->busy = false;
+    qemu_coroutine_yield();
+    job->busy = true;
+}
+
 BlockJobInfo *block_job_query(BlockJob *job)
 {
     BlockJobInfo *info = g_new0(BlockJobInfo, 1);
include/block/blockjob.h +8 -0
index c0a7875..ac313ea 100644
@@ -147,6 +147,14 @@ void *block_job_create(const BlockJobDriver *driver, BlockDriverState *bs,
 void block_job_sleep_ns(BlockJob *job, QEMUClockType type, int64_t ns);
 
 /**
+ * block_job_yield:
+ * @job: The job that calls the function.
+ *
+ * Yield the block job coroutine.
+ */
+void block_job_yield(BlockJob *job);
+
+/**
  * block_job_completed:
  * @job: The job being completed.
  * @ret: The status code.
Signature · 1 line
2.0.0
Reviewed-by on v2 1/4
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/4] blockjob: Add block_job_yield()

Quoted 8 lines · Fam Zheng

This will unset busy flag and put coroutine to sleep, can be used to wait for QMP complete/cancel.

---
 blockjob.c               | 14 ++++++++++++++
 include/block/blockjob.h |  8 ++++++++
 2 files changed, 22 insertions(+)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#3 lore raw

PATCH v2 2/4 mirror: Go through ready -> complete process for 0 len image

In reply to Fam Zheng

When mirroring or active committing a zero length image, BLOCK_JOB_READY is not reported now, instead the job completes because we short circuit the mirror job loop.

This is inconsistent with non-zero length images, and only confuses management software.

Let's do the same thing when seeing a 0-length image: report ready immediately; wait for block-job-cancel or block-job-complete; clear the cancel flag as existing non-zero image synced case (cancelled after ready); then jump to the exit.

---
 block/mirror.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
block/mirror.c +10 -1
index 94c8661..705260a 100644
@@ -324,9 +324,18 @@ static void coroutine_fn mirror_run(void *opaque)
     }
 
     s->common.len = bdrv_getlength(bs);
-    if (s->common.len <= 0) {
+    if (s->common.len < 0) {
         ret = s->common.len;
         goto immediate_exit;
+    } else if (s->common.len == 0) {
+        /* Report BLOCK_JOB_READY and wait for complete. */
+        block_job_ready(&s->common);
+        s->synced = true;
+        while (!block_job_is_cancelled(&s->common) && !s->should_complete) {
+            block_job_yield(&s->common);
+        }
+        s->common.cancelled = false;
+        goto immediate_exit;
     }
 
     length = DIV_ROUND_UP(s->common.len, s->granularity);
Signature · 1 line
2.0.0
Reviewed-by on v2 2/4
#4 lore raw

Re: [Qemu-devel] [PATCH v2 2/4] mirror: Go through ready -> complete process for 0 len image

Quoted 12 lines · Fam Zheng

When mirroring or active committing a zero length image, BLOCK_JOB_READY is not reported now, instead the job completes because we short circuit the mirror job loop.

This is inconsistent with non-zero length images, and only confuses management software.

Let's do the same thing when seeing a 0-length image: report ready immediately; wait for block-job-cancel or block-job-complete; clear the cancel flag as existing non-zero image synced case (cancelled after ready); then jump to the exit.

Quoted 5 lines
---
 block/mirror.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#5 lore raw

PATCH v2 3/4 qemu-iotests: Test BLOCK_JOB_READY event for 0Kb image active commit

In reply to Fam Zheng

There should be a BLOCK_JOB_READY event with active commit, regardless of image length. Let's test the 0 length image case, and make sure it goes through the ready->complete process.

---
 tests/qemu-iotests/040     | 12 +++++++++---
 tests/qemu-iotests/040.out |  4 ++--
 2 files changed, 11 insertions(+), 5 deletions(-)
tests/qemu-iotests/040 +9 -3
index 734b6a6..d166810 100755
@@ -35,12 +35,13 @@ test_img = os.path.join(iotests.test_dir, 'test.img')
 class ImageCommitTestCase(iotests.QMPTestCase):
     '''Abstract base class for image commit test cases'''
 
-    def run_commit_test(self, top, base):
+    def run_commit_test(self, top, base, need_ready=False):
         self.assert_no_active_block_jobs()
         result = self.vm.qmp('block-commit', device='drive0', top=top, base=base)
         self.assert_qmp(result, 'return', {})
 
         completed = False
+        ready = False
         while not completed:
             for event in self.vm.get_qmp_events(wait=True):
                 if event['event'] == 'BLOCK_JOB_COMPLETED':
@@ -48,8 +49,11 @@ class ImageCommitTestCase(iotests.QMPTestCase):
                     self.assert_qmp(event, 'data/device', 'drive0')
                     self.assert_qmp(event, 'data/offset', self.image_len)
                     self.assert_qmp(event, 'data/len', self.image_len)
+                    if need_ready:
+                        self.assertTrue(ready, "Expecting BLOCK_JOB_COMPLETED event")
                     completed = True
                 elif event['event'] == 'BLOCK_JOB_READY':
+                    ready = True
                     self.assert_qmp(event, 'data/type', 'commit')
                     self.assert_qmp(event, 'data/device', 'drive0')
                     self.assert_qmp(event, 'data/len', self.image_len)
@@ -63,7 +67,7 @@ class TestSingleDrive(ImageCommitTestCase):
     test_len = 1 * 1024 * 256
 
     def setUp(self):
-        iotests.create_image(backing_img, TestSingleDrive.image_len)
+        iotests.create_image(backing_img, self.image_len)
         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, mid_img)
         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % mid_img, test_img)
         qemu_io('-c', 'write -P 0xab 0 524288', backing_img)
@@ -105,7 +109,7 @@ class TestSingleDrive(ImageCommitTestCase):
         self.assert_qmp(result, 'error/desc', 'Base \'badfile\' not found')
 
     def test_top_is_active(self):
-        self.run_commit_test(test_img, backing_img)
+        self.run_commit_test(test_img, backing_img, need_ready=True)
         self.assertEqual(-1, qemu_io('-c', 'read -P 0xab 0 524288', backing_img).find("verification failed"))
         self.assertEqual(-1, qemu_io('-c', 'read -P 0xef 524288 524288', backing_img).find("verification failed"))
 
@@ -238,6 +242,8 @@ class TestSetSpeed(ImageCommitTestCase):
 
         self.cancel_and_wait(resume=True)
 
+class TestActiveZeroLengthImage(TestSingleDrive):
+    image_len = 0
 
 if __name__ == '__main__':
     iotests.main(supported_fmts=['qcow2', 'qed'])
tests/qemu-iotests/040.out +2 -2
index b6f2576..42314e9 100644
@@ -1,5 +1,5 @@
-................
+........................
 ----------------------------------------------------------------------
-Ran 16 tests
+Ran 24 tests
 
 OK
Signature · 1 line
2.0.0
author
#6 lore raw

PATCH v2 4/4 qemu-iotests: Test 0-length image for mirror

In reply to Fam Zheng

All behavior and invariant should hold for images with 0 length, so add a class to repeat all the tests in TestSingleDrive.

---
 tests/qemu-iotests/041     | 9 ++++++---
 tests/qemu-iotests/041.out | 4 ++--
 2 files changed, 8 insertions(+), 5 deletions(-)
tests/qemu-iotests/041 +6 -3
index ec470b2..b1db27c 100755
@@ -64,7 +64,7 @@ class TestSingleDrive(ImageMirroringTestCase):
     image_len = 1 * 1024 * 1024 # MB
 
     def setUp(self):
-        iotests.create_image(backing_img, TestSingleDrive.image_len)
+        iotests.create_image(backing_img, self.image_len)
         qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
         self.vm = iotests.VM().add_drive(test_img)
         self.vm.launch()
@@ -163,7 +163,7 @@ class TestSingleDrive(ImageMirroringTestCase):
         self.assert_no_active_block_jobs()
 
         qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,size=%d'
-                        % (TestSingleDrive.image_len, TestSingleDrive.image_len), target_img)
+                        % (self.image_len, self.image_len), target_img)
         result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
                              buf_size=65536, mode='existing', target=target_img)
         self.assert_qmp(result, 'return', {})
@@ -179,7 +179,7 @@ class TestSingleDrive(ImageMirroringTestCase):
         self.assert_no_active_block_jobs()
 
         qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s'
-                        % (TestSingleDrive.image_len, backing_img), target_img)
+                        % (self.image_len, backing_img), target_img)
         result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
                              mode='existing', target=target_img)
         self.assert_qmp(result, 'return', {})
@@ -206,6 +206,9 @@ class TestSingleDrive(ImageMirroringTestCase):
                              target=target_img)
         self.assert_qmp(result, 'error/class', 'DeviceNotFound')
 
+class TestSingleDriveZeroLength(TestSingleDrive):
+    image_len = 0
+
 class TestMirrorNoBacking(ImageMirroringTestCase):
     image_len = 2 * 1024 * 1024 # MB
 
tests/qemu-iotests/041.out +2 -2
index 6d9bee1..cafb816 100644
@@ -1,5 +1,5 @@
-...........................
+.....................................
 ----------------------------------------------------------------------
-Ran 27 tests
+Ran 37 tests
 
 OK
Signature · 1 line
2.0.0
Reviewed-by on v2 4/4
#7 lore raw

Re: [Qemu-devel] [PATCH v2 4/4] qemu-iotests: Test 0-length image for mirror

Quoted 8 lines · Fam Zheng

All behavior and invariant should hold for images with 0 length, so add a class to repeat all the tests in TestSingleDrive.

---
 tests/qemu-iotests/041     | 9 ++++++---
 tests/qemu-iotests/041.out | 4 ++--
 2 files changed, 8 insertions(+), 5 deletions(-)
Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#8 lore raw
In reply to Fam Zheng
Quoted 10 lines · Fam Zheng

The current behavior of mirroring zero byte image is slightly different from non-zero image: the BLOCK_JOB_READY event is skipped and job is completed immediately. This is not a big problem for human user but only makes management software harder to write. Since we are focusing on an good API instead of UI, let's make the behavior more consistent and predictable.

The first patch fixes the behavior. The following two patches add test case for the involved code path.

Thanks for Eric Blake to report this!

Ping. Shall we merge this before it rots?

Thanks,
Fam

Quoted 31 lines

v2: Address Stefan's comments.

- Added patch 01: block_job_yield.
- Use block_job_yield in 02.
- Fix test case updates.

Thanks to Stefan, Paolo and Eric for reviewing!

Fam

Fam Zheng (4):
blockjob: Add block_job_yield()
mirror: Go through ready -> complete process for 0 len image
qemu-iotests: Test BLOCK_JOB_READY event for 0Kb image active commit
qemu-iotests: Test 0-length image for mirror

 block/mirror.c             | 11 ++++++++++-
 blockjob.c                 | 14 ++++++++++++++
 include/block/blockjob.h   |  8 ++++++++
 tests/qemu-iotests/040     | 12 +++++++++---
 tests/qemu-iotests/040.out |  4 ++--
 tests/qemu-iotests/041     |  9 ++++++---
 tests/qemu-iotests/041.out |  4 ++--
 7 files changed, 51 insertions(+), 11 deletions(-)
Signature · 1 line
2.0.0
#9 lore raw

Am 24.06.2014 um 06:25 hat Fam Zheng geschrieben:

Quoted 7 lines · Fam Zheng
Quoted 6 lines

The current behavior of mirroring zero byte image is slightly different from non-zero image: the BLOCK_JOB_READY event is skipped and job is completed immediately. This is not a big problem for human user but only makes management software harder to write. Since we are focusing on an good API instead of UI, let's make the behavior more consistent and predictable.

Quoted 6 lines
Quoted 4 lines

The first patch fixes the behavior. The following two patches add test case for the involved code path.

Thanks for Eric Blake to report this!

Ping. Shall we merge this before it rots?

I had it already applied, but now test case 041 fails for me, so I'm going to remove it from my tree again:

041 7s ... [13:28:48] [13:29:27] [failed, exit status 1] - output mismatch (see 041.out.bad)

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +28 -2
@@ -1,5 +1,31 @@
-.....................................
+qemu-img: /home/kwolf/source/qemu/tests/qemu-iotests/scratch/target.img: Cluster size must be a power of two between 512 and 2048k
+qemu-img: /home/kwolf/source/qemu/tests/qemu-iotests/scratch/target.img: Cluster size must be a power of two between 512 and 2048k
+..........................F...F......
+======================================================================
+FAIL: test_large_cluster (__main__.TestSingleDriveZeroLength)
+----------------------------------------------------------------------
+Traceback (most recent call last):
+  File "./041", line 185, in test_large_cluster
+    self.assert_qmp(result, 'return', {})
+  File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 232, in assert_qmp
+    result = self.dictpath(d, path)
+  File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 211, in dictpath
+    self.fail('failed path traversal for "%s" in "%s"' % (path, str(d)))
+AssertionError: failed path traversal for "return" in "{u'error': {u'class': u'GenericError', u'desc': u"Could not open '/home/kwolf/source/qemu/tests/qemu-iotests/scratch/target.img': No such file or directory"}}"
+
+======================================================================
+FAIL: test_small_buffer2 (__main__.TestSingleDriveZeroLength)
+----------------------------------------------------------------------
+Traceback (most recent call last):
+  File "./041", line 169, in test_small_buffer2
+    self.assert_qmp(result, 'return', {})
+  File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 232, in assert_qmp
+    result = self.dictpath(d, path)
+  File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 211, in dictpath                                                                                
+    self.fail('failed path traversal for "%s" in "%s"' % (path, str(d)))                                                                                             
+AssertionError: failed path traversal for "return" in "{u'error': {u'class': u'GenericError', u'desc': u"Could not open '/home/kwolf/source/qemu/tests/qemu-iotests/scratch/target.img': No such file or directory"}}"                                                                                                                     
+                                                                                                                                                                     
 ----------------------------------------------------------------------                                                                                               
 Ran 37 tests                                                                                                                                                         
 
-OK
+FAILED (failures=2)

Failures: 041

author
#10 lore raw
Quoted 16 lines

Am 24.06.2014 um 06:25 hat Fam Zheng geschrieben:

Quoted 14 lines · Fam Zheng
Quoted 13 lines

The current behavior of mirroring zero byte image is slightly different from
non-zero image: the BLOCK_JOB_READY event is skipped and job is completed immediately. This is not a big problem for human user but only makes management
software harder to write. Since we are focusing on an good API instead of UI,
let's make the behavior more consistent and predictable.

The first patch fixes the behavior. The following two patches add test case for
the involved code path.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Thanks for Eric Blake to report this!

Ping. Shall we merge this before it rots?

I had it already applied, but now test case 041 fails for me, so I'm going to remove it from my tree again:

I'm pretty sure it passed when I sent it, so it's probably due do some recent code changes. Rebased and sent v3. Please try again!

Thanks for the effort!

Fam

Quoted 53 lines

041 7s ... [13:28:48] [13:29:27] [failed, exit status 1] - output
mismatch (see 041.out.bad)

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +1 -1
@@ -1,5 +1,31 @@
-.....................................
+qemu-img: /home/kwolf/source/qemu/tests/qemu-iotests/scratch/target.img:

Cluster size must be a power of two between 512 and 2048k +qemu-img: /home/kwolf/source/qemu/tests/qemu-iotests/scratch/target.img:
Cluster size must be a power of two between 512 and 2048k +..........................F...F......
+======================================================================

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +6 -0
+FAIL: test_large_cluster (__main__.TestSingleDriveZeroLength)
+----------------------------------------------------------------------
+Traceback (most recent call last):
+  File "./041", line 185, in test_large_cluster
+    self.assert_qmp(result, 'return', {})
+  File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 232, in

assert_qmp

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +2 -0
+    result = self.dictpath(d, path)
+  File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 211, in

dictpath

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +2 -0
+    self.fail('failed path traversal for "%s" in "%s"' % (path, str(d)))
+AssertionError: failed path traversal for "return" in "{u'error': {u'class':

u'GenericError', u'desc': u"Could not open '/home/kwolf/source/qemu/tests/qemu-iotests/scratch/target.img': No such file or directory"}}"

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +8 -0
+
+======================================================================
+FAIL: test_small_buffer2 (__main__.TestSingleDriveZeroLength)
+----------------------------------------------------------------------
+Traceback (most recent call last):
+  File "./041", line 169, in test_small_buffer2
+    self.assert_qmp(result, 'return', {})
+  File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 232, in

assert_qmp

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +2 -0
+    result = self.dictpath(d, path)
+  File "/home/kwolf/source/qemu/tests/qemu-iotests/iotests.py", line 211, in

dictpath

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +2 -0
+    self.fail('failed path traversal for "%s" in "%s"' % (path, str(d)))
+AssertionError: failed path traversal for "return" in "{u'error': {u'class':

u'GenericError', u'desc': u"Could not open '/home/kwolf/source/qemu/tests/qemu-iotests/scratch/target.img': No such file or directory"}}"

041.out.bad 2014-06-24 13:29:27.024871226 +0200 renamed from 041.out 2014-06-24 13:23:13.597949977 +0200 +2 -1
+
 ----------------------------------------------------------------------
 Ran 37 tests
 
-OK
+FAILED (failures=2)

Failures: 041