Patchwork [3/9] block: bdrv_reopen_prepare(): use error_setg_file_open()

login
register
mail settings
Submitter Luiz Capitulino
Date June 7, 2013, 7:52 p.m.
Message ID <1370634755-18132-4-git-send-email-lcapitulino@redhat.com>
Download mbox | patch
Permalink /patch/249803/
State New
Headers show

Comments

Luiz Capitulino - June 7, 2013, 7:52 p.m.
Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
---
 block.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
Stefan Hajnoczi - June 10, 2013, 8:39 a.m.
On Fri, Jun 07, 2013 at 03:52:29PM -0400, Luiz Capitulino wrote:
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> ---
>  block.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/block.c b/block.c
> index 79ad33d..c78f152 100644
> --- a/block.c
> +++ b/block.c
> @@ -1291,8 +1291,7 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
>              if (local_err != NULL) {
>                  error_propagate(errp, local_err);
>              } else {
> -                error_set(errp, QERR_OPEN_FILE_FAILED,
> -                          reopen_state->bs->filename);
> +                error_setg_file_open(errp, errno, reopen_state->bs->filename);

s/errno/-ret/
Stefan Hajnoczi - June 10, 2013, 8:43 a.m.
On Fri, Jun 07, 2013 at 03:52:29PM -0400, Luiz Capitulino wrote:
> Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> ---
>  block.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/block.c b/block.c
> index 79ad33d..c78f152 100644
> --- a/block.c
> +++ b/block.c
> @@ -1291,8 +1291,7 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
>              if (local_err != NULL) {
>                  error_propagate(errp, local_err);
>              } else {
> -                error_set(errp, QERR_OPEN_FILE_FAILED,
> -                          reopen_state->bs->filename);
> +                error_setg_file_open(errp, errno, reopen_state->bs->filename);

Looking closer, my suggestion was wrong too.

I think QERR_OPEN_FILE_FAILED is simply the wrong error here.  We don't
know that the error occurred when trying to open a file.

errno does not necessarily contain the error value!

Stefan
Luiz Capitulino - June 10, 2013, 1:21 p.m.
On Mon, 10 Jun 2013 10:43:47 +0200
Stefan Hajnoczi <stefanha@gmail.com> wrote:

> On Fri, Jun 07, 2013 at 03:52:29PM -0400, Luiz Capitulino wrote:
> > Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> > ---
> >  block.c | 3 +--
> >  1 file changed, 1 insertion(+), 2 deletions(-)
> > 
> > diff --git a/block.c b/block.c
> > index 79ad33d..c78f152 100644
> > --- a/block.c
> > +++ b/block.c
> > @@ -1291,8 +1291,7 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
> >              if (local_err != NULL) {
> >                  error_propagate(errp, local_err);
> >              } else {
> > -                error_set(errp, QERR_OPEN_FILE_FAILED,
> > -                          reopen_state->bs->filename);
> > +                error_setg_file_open(errp, errno, reopen_state->bs->filename);
> 
> Looking closer, my suggestion was wrong too.
> 
> I think QERR_OPEN_FILE_FAILED is simply the wrong error here.  We don't
> know that the error occurred when trying to open a file.

Right.

> errno does not necessarily contain the error value!

There are two ways to fix it (and they're not mutually exclusive):

 1. We could review all bdrv_reopen_prepare() methods and make sure they
    set errno on failure

 2. We set errno=0 before calling the bdrv_reopen_prepare() method and if
    there's an error and if errno != 0 we use it, otherwise we set a generic
    "failed to prepare to reopen image" error

Option 1 goes a bit beyond the time I'd like to spent on this series.
Option 2 is quite reasonable.

What do you think?
Kevin Wolf - June 10, 2013, 1:54 p.m.
Am 10.06.2013 um 15:21 hat Luiz Capitulino geschrieben:
> On Mon, 10 Jun 2013 10:43:47 +0200
> Stefan Hajnoczi <stefanha@gmail.com> wrote:
> 
> > On Fri, Jun 07, 2013 at 03:52:29PM -0400, Luiz Capitulino wrote:
> > > Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> > > ---
> > >  block.c | 3 +--
> > >  1 file changed, 1 insertion(+), 2 deletions(-)
> > > 
> > > diff --git a/block.c b/block.c
> > > index 79ad33d..c78f152 100644
> > > --- a/block.c
> > > +++ b/block.c
> > > @@ -1291,8 +1291,7 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
> > >              if (local_err != NULL) {
> > >                  error_propagate(errp, local_err);
> > >              } else {
> > > -                error_set(errp, QERR_OPEN_FILE_FAILED,
> > > -                          reopen_state->bs->filename);
> > > +                error_setg_file_open(errp, errno, reopen_state->bs->filename);
> > 
> > Looking closer, my suggestion was wrong too.
> > 
> > I think QERR_OPEN_FILE_FAILED is simply the wrong error here.  We don't
> > know that the error occurred when trying to open a file.
> 
> Right.
> 
> > errno does not necessarily contain the error value!
> 
> There are two ways to fix it (and they're not mutually exclusive):
> 
>  1. We could review all bdrv_reopen_prepare() methods and make sure they
>     set errno on failure
> 
>  2. We set errno=0 before calling the bdrv_reopen_prepare() method and if
>     there's an error and if errno != 0 we use it, otherwise we set a generic
>     "failed to prepare to reopen image" error
> 
> Option 1 goes a bit beyond the time I'd like to spent on this series.
> Option 2 is quite reasonable.
> 
> What do you think?

errno is definitely not reliable here and won't be. You must use -ret if
you want a meaningful error message.

I think Stefan's point was more that "Could not open" might not be the
right message for a reopen, so we'd have to use error_setg_errno()
directly with a different message here, like "Could not prepare reopen
for '%s'".

The _real_ solution, of course, is to make bdrv_prepare_reopen() a void
function and always set errp when something goes wrong.

Kevin
Luiz Capitulino - June 10, 2013, 2:02 p.m.
On Mon, 10 Jun 2013 15:54:10 +0200
Kevin Wolf <kwolf@redhat.com> wrote:

> Am 10.06.2013 um 15:21 hat Luiz Capitulino geschrieben:
> > On Mon, 10 Jun 2013 10:43:47 +0200
> > Stefan Hajnoczi <stefanha@gmail.com> wrote:
> > 
> > > On Fri, Jun 07, 2013 at 03:52:29PM -0400, Luiz Capitulino wrote:
> > > > Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> > > > ---
> > > >  block.c | 3 +--
> > > >  1 file changed, 1 insertion(+), 2 deletions(-)
> > > > 
> > > > diff --git a/block.c b/block.c
> > > > index 79ad33d..c78f152 100644
> > > > --- a/block.c
> > > > +++ b/block.c
> > > > @@ -1291,8 +1291,7 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
> > > >              if (local_err != NULL) {
> > > >                  error_propagate(errp, local_err);
> > > >              } else {
> > > > -                error_set(errp, QERR_OPEN_FILE_FAILED,
> > > > -                          reopen_state->bs->filename);
> > > > +                error_setg_file_open(errp, errno, reopen_state->bs->filename);
> > > 
> > > Looking closer, my suggestion was wrong too.
> > > 
> > > I think QERR_OPEN_FILE_FAILED is simply the wrong error here.  We don't
> > > know that the error occurred when trying to open a file.
> > 
> > Right.
> > 
> > > errno does not necessarily contain the error value!
> > 
> > There are two ways to fix it (and they're not mutually exclusive):
> > 
> >  1. We could review all bdrv_reopen_prepare() methods and make sure they
> >     set errno on failure
> > 
> >  2. We set errno=0 before calling the bdrv_reopen_prepare() method and if
> >     there's an error and if errno != 0 we use it, otherwise we set a generic
> >     "failed to prepare to reopen image" error
> > 
> > Option 1 goes a bit beyond the time I'd like to spent on this series.
> > Option 2 is quite reasonable.
> > 
> > What do you think?
> 
> errno is definitely not reliable here and won't be. You must use -ret if
> you want a meaningful error message.
> 
> I think Stefan's point was more that "Could not open" might not be the
> right message for a reopen, so we'd have to use error_setg_errno()
> directly with a different message here, like "Could not prepare reopen
> for '%s'".

Ok, but Stefan also said that -ret is not reliable. And after quickly
checking the code I see he's right, as there are methods that return -1.

I think the safest thing to do is to have a generic error message for
for this now and in the future we should propagate errp or return -errno.
Kevin Wolf - June 10, 2013, 2:06 p.m.
Am 10.06.2013 um 16:02 hat Luiz Capitulino geschrieben:
> On Mon, 10 Jun 2013 15:54:10 +0200
> Kevin Wolf <kwolf@redhat.com> wrote:
> 
> > Am 10.06.2013 um 15:21 hat Luiz Capitulino geschrieben:
> > > On Mon, 10 Jun 2013 10:43:47 +0200
> > > Stefan Hajnoczi <stefanha@gmail.com> wrote:
> > > 
> > > > On Fri, Jun 07, 2013 at 03:52:29PM -0400, Luiz Capitulino wrote:
> > > > > Signed-off-by: Luiz Capitulino <lcapitulino@redhat.com>
> > > > > ---
> > > > >  block.c | 3 +--
> > > > >  1 file changed, 1 insertion(+), 2 deletions(-)
> > > > > 
> > > > > diff --git a/block.c b/block.c
> > > > > index 79ad33d..c78f152 100644
> > > > > --- a/block.c
> > > > > +++ b/block.c
> > > > > @@ -1291,8 +1291,7 @@ int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
> > > > >              if (local_err != NULL) {
> > > > >                  error_propagate(errp, local_err);
> > > > >              } else {
> > > > > -                error_set(errp, QERR_OPEN_FILE_FAILED,
> > > > > -                          reopen_state->bs->filename);
> > > > > +                error_setg_file_open(errp, errno, reopen_state->bs->filename);
> > > > 
> > > > Looking closer, my suggestion was wrong too.
> > > > 
> > > > I think QERR_OPEN_FILE_FAILED is simply the wrong error here.  We don't
> > > > know that the error occurred when trying to open a file.
> > > 
> > > Right.
> > > 
> > > > errno does not necessarily contain the error value!
> > > 
> > > There are two ways to fix it (and they're not mutually exclusive):
> > > 
> > >  1. We could review all bdrv_reopen_prepare() methods and make sure they
> > >     set errno on failure
> > > 
> > >  2. We set errno=0 before calling the bdrv_reopen_prepare() method and if
> > >     there's an error and if errno != 0 we use it, otherwise we set a generic
> > >     "failed to prepare to reopen image" error
> > > 
> > > Option 1 goes a bit beyond the time I'd like to spent on this series.
> > > Option 2 is quite reasonable.
> > > 
> > > What do you think?
> > 
> > errno is definitely not reliable here and won't be. You must use -ret if
> > you want a meaningful error message.
> > 
> > I think Stefan's point was more that "Could not open" might not be the
> > right message for a reopen, so we'd have to use error_setg_errno()
> > directly with a different message here, like "Could not prepare reopen
> > for '%s'".
> 
> Ok, but Stefan also said that -ret is not reliable. And after quickly
> checking the code I see he's right, as there are methods that return -1.
> 
> I think the safest thing to do is to have a generic error message for
> for this now and in the future we should propagate errp or return -errno.

Ah, yes. I agree, let's make it error_setg() without errno for now.

Kevin

Patch

diff --git a/block.c b/block.c
index 79ad33d..c78f152 100644
--- a/block.c
+++ b/block.c
@@ -1291,8 +1291,7 @@  int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
             if (local_err != NULL) {
                 error_propagate(errp, local_err);
             } else {
-                error_set(errp, QERR_OPEN_FILE_FAILED,
-                          reopen_state->bs->filename);
+                error_setg_file_open(errp, errno, reopen_state->bs->filename);
             }
             goto error;
         }