diff mbox series

[v3,02/13] block: use return status of bdrv_append()

Message ID 20201016171057.6182-3-vsementsov@virtuozzo.com
State Superseded
Headers show
Series block: deal with errp: part I | expand

Commit Message

Vladimir Sementsov-Ogievskiy Oct. 16, 2020, 5:10 p.m. UTC
Now bdrv_append returns status and we can drop all the local_err things
around it.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Reviewed-by: Greg Kurz <groug@kaod.org>
Reviewed-by: Alberto Garcia <berto@igalia.com>
---
 block.c                     |  5 +----
 block/backup-top.c          | 20 ++++++++------------
 block/commit.c              |  5 +----
 block/mirror.c              |  6 ++----
 blockdev.c                  |  4 +---
 tests/test-bdrv-graph-mod.c |  6 +++---
 6 files changed, 16 insertions(+), 30 deletions(-)

Comments

Kevin Wolf Oct. 19, 2020, 12:13 p.m. UTC | #1
Am 16.10.2020 um 19:10 hat Vladimir Sementsov-Ogievskiy geschrieben:
> Now bdrv_append returns status and we can drop all the local_err things

> around it.

> 

> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

> Reviewed-by: Greg Kurz <groug@kaod.org>

> Reviewed-by: Alberto Garcia <berto@igalia.com>

> ---

>  block.c                     |  5 +----

>  block/backup-top.c          | 20 ++++++++------------

>  block/commit.c              |  5 +----

>  block/mirror.c              |  6 ++----

>  blockdev.c                  |  4 +---

>  tests/test-bdrv-graph-mod.c |  6 +++---

>  6 files changed, 16 insertions(+), 30 deletions(-)

> 

> diff --git a/block.c b/block.c

> index b05fbff42d..7b6818c681 100644

> --- a/block.c

> +++ b/block.c

> @@ -3161,7 +3161,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,

>      int64_t total_size;

>      QemuOpts *opts = NULL;

>      BlockDriverState *bs_snapshot = NULL;

> -    Error *local_err = NULL;

>      int ret;

>  

>      /* if snapshot, we create a temporary backing file and open it

> @@ -3208,9 +3207,7 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,

>       * order to be able to return one, we have to increase

>       * bs_snapshot's refcount here */

>      bdrv_ref(bs_snapshot);

> -    bdrv_append(bs_snapshot, bs, &local_err);

> -    if (local_err) {

> -        error_propagate(errp, local_err);

> +    if (bdrv_append(bs_snapshot, bs, errp) < 0) {


We generally avoid calling functions with side effects inside a
comparison. Let's use the usual pattern:

    ret = bdrv_append(bs_snapshot, bs, errp);
    if (ret < 0) {
        ...
    }

(I mention it only once, but there are multiple instances in this
patch.)

>          bs_snapshot = NULL;

>          goto out;

>      }


> diff --git a/tests/test-bdrv-graph-mod.c b/tests/test-bdrv-graph-mod.c

> index 8cff13830e..2b71601c24 100644

> --- a/tests/test-bdrv-graph-mod.c

> +++ b/tests/test-bdrv-graph-mod.c

> @@ -101,7 +101,7 @@ static BlockDriverState *pass_through_node(const char *name)

>   */

>  static void test_update_perm_tree(void)

>  {

> -    Error *local_err = NULL;

> +    int ret;

>  

>      BlockBackend *root = blk_new(qemu_get_aio_context(),

>                                   BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ,

> @@ -114,8 +114,8 @@ static void test_update_perm_tree(void)

>      bdrv_attach_child(filter, bs, "child", &child_of_bds,

>                        BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, &error_abort);

>  

> -    bdrv_append(filter, bs, &local_err);

> -    error_free_or_abort(&local_err);

> +    ret = bdrv_append(filter, bs, NULL);

> +    assert(ret < 0);


Usually, test cases use g_assert_cmpint() which prints the expected and
actual value if it fails.

Kevin
Markus Armbruster Oct. 19, 2020, 3:45 p.m. UTC | #2
Kevin Wolf <kwolf@redhat.com> writes:

> Am 16.10.2020 um 19:10 hat Vladimir Sementsov-Ogievskiy geschrieben:

>> Now bdrv_append returns status and we can drop all the local_err things

>> around it.

>> 

>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

>> Reviewed-by: Greg Kurz <groug@kaod.org>

>> Reviewed-by: Alberto Garcia <berto@igalia.com>

>> ---

>>  block.c                     |  5 +----

>>  block/backup-top.c          | 20 ++++++++------------

>>  block/commit.c              |  5 +----

>>  block/mirror.c              |  6 ++----

>>  blockdev.c                  |  4 +---

>>  tests/test-bdrv-graph-mod.c |  6 +++---

>>  6 files changed, 16 insertions(+), 30 deletions(-)

>> 

>> diff --git a/block.c b/block.c

>> index b05fbff42d..7b6818c681 100644

>> --- a/block.c

>> +++ b/block.c

>> @@ -3161,7 +3161,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,

>>      int64_t total_size;

>>      QemuOpts *opts = NULL;

>>      BlockDriverState *bs_snapshot = NULL;

>> -    Error *local_err = NULL;

>>      int ret;

>>  

>>      /* if snapshot, we create a temporary backing file and open it

>> @@ -3208,9 +3207,7 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,

>>       * order to be able to return one, we have to increase

>>       * bs_snapshot's refcount here */

>>      bdrv_ref(bs_snapshot);

>> -    bdrv_append(bs_snapshot, bs, &local_err);

>> -    if (local_err) {

>> -        error_propagate(errp, local_err);

>> +    if (bdrv_append(bs_snapshot, bs, errp) < 0) {

>

> We generally avoid calling functions with side effects inside a

> comparison. Let's use the usual pattern:

>

>     ret = bdrv_append(bs_snapshot, bs, errp);

>     if (ret < 0) {

>         ...

>     }


I'd also advice against buring side effects too deep, but calling a
function in a failure-checking conditional is pretty benign.  It's also
common:

    $ git-grep 'if ([a-z].*) < 0) {'

coughs up several hundred instances.

That said, there is none in block.c.  Local consistency matters.

[...]
Kevin Wolf Oct. 19, 2020, 4:10 p.m. UTC | #3
Am 19.10.2020 um 17:45 hat Markus Armbruster geschrieben:
> Kevin Wolf <kwolf@redhat.com> writes:

> 

> > Am 16.10.2020 um 19:10 hat Vladimir Sementsov-Ogievskiy geschrieben:

> >> Now bdrv_append returns status and we can drop all the local_err things

> >> around it.

> >> 

> >> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>

> >> Reviewed-by: Greg Kurz <groug@kaod.org>

> >> Reviewed-by: Alberto Garcia <berto@igalia.com>

> >> ---

> >>  block.c                     |  5 +----

> >>  block/backup-top.c          | 20 ++++++++------------

> >>  block/commit.c              |  5 +----

> >>  block/mirror.c              |  6 ++----

> >>  blockdev.c                  |  4 +---

> >>  tests/test-bdrv-graph-mod.c |  6 +++---

> >>  6 files changed, 16 insertions(+), 30 deletions(-)

> >> 

> >> diff --git a/block.c b/block.c

> >> index b05fbff42d..7b6818c681 100644

> >> --- a/block.c

> >> +++ b/block.c

> >> @@ -3161,7 +3161,6 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,

> >>      int64_t total_size;

> >>      QemuOpts *opts = NULL;

> >>      BlockDriverState *bs_snapshot = NULL;

> >> -    Error *local_err = NULL;

> >>      int ret;

> >>  

> >>      /* if snapshot, we create a temporary backing file and open it

> >> @@ -3208,9 +3207,7 @@ static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,

> >>       * order to be able to return one, we have to increase

> >>       * bs_snapshot's refcount here */

> >>      bdrv_ref(bs_snapshot);

> >> -    bdrv_append(bs_snapshot, bs, &local_err);

> >> -    if (local_err) {

> >> -        error_propagate(errp, local_err);

> >> +    if (bdrv_append(bs_snapshot, bs, errp) < 0) {

> >

> > We generally avoid calling functions with side effects inside a

> > comparison. Let's use the usual pattern:

> >

> >     ret = bdrv_append(bs_snapshot, bs, errp);

> >     if (ret < 0) {

> >         ...

> >     }

> 

> I'd also advice against buring side effects too deep, but calling a

> function in a failure-checking conditional is pretty benign.  It's also

> common:

> 

>     $ git-grep 'if ([a-z].*) < 0) {'

> 

> coughs up several hundred instances.

> 

> That said, there is none in block.c.  Local consistency matters.


Actually, after looking at the rest of the series, I also need to be a
bit more specific: We do have boolean if (!foo()), it's just the
negative errno ones that aren't very common in the block layer.

And the reason why it's not very common is probably that 0/-errno is
very common in the block layer and a check for < 0 just throws the
specific errno away. We had a lot of such code, which was wrong because
it lost information instead of passing the real error to the caller.
I guess this is the real origin of the block layer habits to avoid it.

Kevin
diff mbox series

Patch

diff --git a/block.c b/block.c
index b05fbff42d..7b6818c681 100644
--- a/block.c
+++ b/block.c
@@ -3161,7 +3161,6 @@  static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
     int64_t total_size;
     QemuOpts *opts = NULL;
     BlockDriverState *bs_snapshot = NULL;
-    Error *local_err = NULL;
     int ret;
 
     /* if snapshot, we create a temporary backing file and open it
@@ -3208,9 +3207,7 @@  static BlockDriverState *bdrv_append_temp_snapshot(BlockDriverState *bs,
      * order to be able to return one, we have to increase
      * bs_snapshot's refcount here */
     bdrv_ref(bs_snapshot);
-    bdrv_append(bs_snapshot, bs, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    if (bdrv_append(bs_snapshot, bs, errp) < 0) {
         bs_snapshot = NULL;
         goto out;
     }
diff --git a/block/backup-top.c b/block/backup-top.c
index fe6883cc97..eb6a34b726 100644
--- a/block/backup-top.c
+++ b/block/backup-top.c
@@ -190,7 +190,7 @@  BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
                                          BlockCopyState **bcs,
                                          Error **errp)
 {
-    Error *local_err = NULL;
+    ERRP_GUARD();
     BDRVBackupTopState *state;
     BlockDriverState *top;
     bool appended = false;
@@ -223,9 +223,8 @@  BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
     bdrv_drained_begin(source);
 
     bdrv_ref(top);
-    bdrv_append(top, source, &local_err);
-    if (local_err) {
-        error_prepend(&local_err, "Cannot append backup-top filter: ");
+    if (bdrv_append(top, source, errp) < 0) {
+        error_prepend(errp, "Cannot append backup-top filter: ");
         goto fail;
     }
     appended = true;
@@ -235,18 +234,16 @@  BlockDriverState *bdrv_backup_top_append(BlockDriverState *source,
      * we want.
      */
     state->active = true;
-    bdrv_child_refresh_perms(top, top->backing, &local_err);
-    if (local_err) {
-        error_prepend(&local_err,
-                      "Cannot set permissions for backup-top filter: ");
+    if (bdrv_child_refresh_perms(top, top->backing, errp) < 0) {
+        error_prepend(errp, "Cannot set permissions for backup-top filter: ");
         goto fail;
     }
 
     state->cluster_size = cluster_size;
     state->bcs = block_copy_state_new(top->backing, state->target,
-                                      cluster_size, write_flags, &local_err);
-    if (local_err) {
-        error_prepend(&local_err, "Cannot create block-copy-state: ");
+                                      cluster_size, write_flags, errp);
+    if (!state->bcs) {
+        error_prepend(errp, "Cannot create block-copy-state: ");
         goto fail;
     }
     *bcs = state->bcs;
@@ -264,7 +261,6 @@  fail:
     }
 
     bdrv_drained_end(source);
-    error_propagate(errp, local_err);
 
     return NULL;
 }
diff --git a/block/commit.c b/block/commit.c
index 1e85c306cc..6da0902f9d 100644
--- a/block/commit.c
+++ b/block/commit.c
@@ -254,7 +254,6 @@  void commit_start(const char *job_id, BlockDriverState *bs,
     BlockDriverState *iter;
     BlockDriverState *commit_top_bs = NULL;
     BlockDriverState *filtered_base;
-    Error *local_err = NULL;
     int64_t base_size, top_size;
     uint64_t base_perms, iter_shared_perms;
     int ret;
@@ -312,10 +311,8 @@  void commit_start(const char *job_id, BlockDriverState *bs,
 
     commit_top_bs->total_sectors = top->total_sectors;
 
-    bdrv_append(commit_top_bs, top, &local_err);
-    if (local_err) {
+    if (bdrv_append(commit_top_bs, top, errp) < 0) {
         commit_top_bs = NULL;
-        error_propagate(errp, local_err);
         goto fail;
     }
 
diff --git a/block/mirror.c b/block/mirror.c
index 26acf4af6f..b3778248b8 100644
--- a/block/mirror.c
+++ b/block/mirror.c
@@ -1560,7 +1560,6 @@  static BlockJob *mirror_start_job(
     BlockDriverState *mirror_top_bs;
     bool target_is_backing;
     uint64_t target_perms, target_shared_perms;
-    Error *local_err = NULL;
     int ret;
 
     if (granularity == 0) {
@@ -1609,12 +1608,11 @@  static BlockJob *mirror_start_job(
      * it alive until block_job_create() succeeds even if bs has no parent. */
     bdrv_ref(mirror_top_bs);
     bdrv_drained_begin(bs);
-    bdrv_append(mirror_top_bs, bs, &local_err);
+    ret = bdrv_append(mirror_top_bs, bs, errp);
     bdrv_drained_end(bs);
 
-    if (local_err) {
+    if (ret < 0) {
         bdrv_unref(mirror_top_bs);
-        error_propagate(errp, local_err);
         return NULL;
     }
 
diff --git a/blockdev.c b/blockdev.c
index fe6fb5dc1d..3a896181fd 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -1591,9 +1591,7 @@  static void external_snapshot_prepare(BlkActionState *common,
      * can fail, so we need to do it in .prepare; undoing it for abort is
      * always possible. */
     bdrv_ref(state->new_bs);
-    bdrv_append(state->new_bs, state->old_bs, &local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
+    if (bdrv_append(state->new_bs, state->old_bs, errp) < 0) {
         goto out;
     }
     state->overlay_appended = true;
diff --git a/tests/test-bdrv-graph-mod.c b/tests/test-bdrv-graph-mod.c
index 8cff13830e..2b71601c24 100644
--- a/tests/test-bdrv-graph-mod.c
+++ b/tests/test-bdrv-graph-mod.c
@@ -101,7 +101,7 @@  static BlockDriverState *pass_through_node(const char *name)
  */
 static void test_update_perm_tree(void)
 {
-    Error *local_err = NULL;
+    int ret;
 
     BlockBackend *root = blk_new(qemu_get_aio_context(),
                                  BLK_PERM_WRITE | BLK_PERM_CONSISTENT_READ,
@@ -114,8 +114,8 @@  static void test_update_perm_tree(void)
     bdrv_attach_child(filter, bs, "child", &child_of_bds,
                       BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, &error_abort);
 
-    bdrv_append(filter, bs, &local_err);
-    error_free_or_abort(&local_err);
+    ret = bdrv_append(filter, bs, NULL);
+    assert(ret < 0);
 
     blk_unref(root);
 }