* [Qemu-devel] [PATCH v2] blockdev: clean up error handling in do_open_tray
@ 2016-06-03 19:39 Colin Lord
2016-06-03 20:07 ` Kevin Wolf
` (2 more replies)
0 siblings, 3 replies; 4+ messages in thread
From: Colin Lord @ 2016-06-03 19:39 UTC (permalink / raw)
To: qemu-devel; +Cc: kwolf, armbru, qemu-block, mreitz, Colin Lord
Returns negative error codes and accompanying error messages in cases where
the device has no tray or the tray is locked and isn't forced open. This
extra information should result in better flexibility in functions that
call do_open_tray.
Signed-off-by: Colin Lord <clord@redhat.com>
---
v2: fix function documentation, improve commit wording, and remove
unnecessary null check
blockdev.c | 36 +++++++++++++++++++++---------------
1 file changed, 21 insertions(+), 15 deletions(-)
diff --git a/blockdev.c b/blockdev.c
index 717785e..d50a2a5 100644
--- a/blockdev.c
+++ b/blockdev.c
@@ -2286,17 +2286,14 @@ void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
}
rc = do_open_tray(device, force, &local_err);
- if (local_err) {
+ if (rc == -ENOSYS) {
+ error_free(local_err);
+ local_err = NULL;
+ } else if (local_err) {
error_propagate(errp, local_err);
return;
}
- if (rc == EINPROGRESS) {
- error_setg(errp, "Device '%s' is locked and force was not specified, "
- "wait for tray to open and try again", device);
- return;
- }
-
qmp_x_blockdev_remove_medium(device, errp);
}
@@ -2325,10 +2322,9 @@ void qmp_block_passwd(bool has_device, const char *device,
}
/**
- * returns -errno on fatal error, +errno for non-fatal situations.
- * errp will always be set when the return code is negative.
- * May return +ENOSYS if the device has no tray,
- * or +EINPROGRESS if the tray is locked and the guest has been notified.
+ * returns -errno on all errors, and errp will be set on error
+ * May return the non-fatal error codes -ENOSYS if the device has no tray,
+ * or -EINPROGRESS if the tray is locked and the guest has been notified.
*/
static int do_open_tray(const char *device, bool force, Error **errp)
{
@@ -2348,8 +2344,8 @@ static int do_open_tray(const char *device, bool force, Error **errp)
}
if (!blk_dev_has_tray(blk)) {
- /* Ignore this command on tray-less devices */
- return ENOSYS;
+ error_setg(errp, "Device '%s' does not have a tray", device);
+ return -ENOSYS;
}
if (blk_dev_is_tray_open(blk)) {
@@ -2366,7 +2362,9 @@ static int do_open_tray(const char *device, bool force, Error **errp)
}
if (locked && !force) {
- return EINPROGRESS;
+ error_setg(errp, "Device '%s' is locked and force was not specified, "
+ "wait for tray to open and try again", device);
+ return -EINPROGRESS;
}
return 0;
@@ -2375,10 +2373,18 @@ static int do_open_tray(const char *device, bool force, Error **errp)
void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
Error **errp)
{
+ Error *local_err = NULL;
+ int rc;
+
if (!has_force) {
force = false;
}
- do_open_tray(device, force, errp);
+ rc = do_open_tray(device, force, &local_err);
+ if (rc == -EINPROGRESS || rc == -ENOSYS) {
+ error_free(local_err);
+ } else {
+ error_propagate(errp, local_err);
+ }
}
void qmp_blockdev_close_tray(const char *device, Error **errp)
--
2.5.5
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH v2] blockdev: clean up error handling in do_open_tray
2016-06-03 19:39 [Qemu-devel] [PATCH v2] blockdev: clean up error handling in do_open_tray Colin Lord
@ 2016-06-03 20:07 ` Kevin Wolf
2016-06-03 20:19 ` Eric Blake
2016-06-06 7:59 ` Markus Armbruster
2 siblings, 0 replies; 4+ messages in thread
From: Kevin Wolf @ 2016-06-03 20:07 UTC (permalink / raw)
To: Colin Lord; +Cc: qemu-devel, armbru, qemu-block, mreitz
Am 03.06.2016 um 21:39 hat Colin Lord geschrieben:
> Returns negative error codes and accompanying error messages in cases where
> the device has no tray or the tray is locked and isn't forced open. This
> extra information should result in better flexibility in functions that
> call do_open_tray.
>
> Signed-off-by: Colin Lord <clord@redhat.com>
Thanks, applied to the block branch.
Kevin
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH v2] blockdev: clean up error handling in do_open_tray
2016-06-03 19:39 [Qemu-devel] [PATCH v2] blockdev: clean up error handling in do_open_tray Colin Lord
2016-06-03 20:07 ` Kevin Wolf
@ 2016-06-03 20:19 ` Eric Blake
2016-06-06 7:59 ` Markus Armbruster
2 siblings, 0 replies; 4+ messages in thread
From: Eric Blake @ 2016-06-03 20:19 UTC (permalink / raw)
To: Colin Lord, qemu-devel; +Cc: kwolf, armbru, qemu-block, mreitz
[-- Attachment #1: Type: text/plain, Size: 2400 bytes --]
On 06/03/2016 01:39 PM, Colin Lord wrote:
> Returns negative error codes and accompanying error messages in cases where
> the device has no tray or the tray is locked and isn't forced open. This
> extra information should result in better flexibility in functions that
> call do_open_tray.
>
> Signed-off-by: Colin Lord <clord@redhat.com>
> ---
> v2: fix function documentation, improve commit wording, and remove
> unnecessary null check
> blockdev.c | 36 +++++++++++++++++++++---------------
> 1 file changed, 21 insertions(+), 15 deletions(-)
>
> +++ b/blockdev.c
> @@ -2286,17 +2286,14 @@ void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
> }
>
> rc = do_open_tray(device, force, &local_err);
> - if (local_err) {
> + if (rc == -ENOSYS) {
> + error_free(local_err);
> + local_err = NULL;
> + } else if (local_err) {
> error_propagate(errp, local_err);
> return;
> }
>
> - if (rc == EINPROGRESS) {
> - error_setg(errp, "Device '%s' is locked and force was not specified, "
> - "wait for tray to open and try again", device);
> - return;
> - }
> -
> qmp_x_blockdev_remove_medium(device, errp);
> }
The local_err = NULL line is dead code, since nothing else references
local_err before it goes out of scope. Maintainer could fix that on commit.
>
> @@ -2325,10 +2322,9 @@ void qmp_block_passwd(bool has_device, const char *device,
> }
>
> /**
> - * returns -errno on fatal error, +errno for non-fatal situations.
> - * errp will always be set when the return code is negative.
> - * May return +ENOSYS if the device has no tray,
> - * or +EINPROGRESS if the tray is locked and the guest has been notified.
> + * returns -errno on all errors, and errp will be set on error
> + * May return the non-fatal error codes -ENOSYS if the device has no tray,
> + * or -EINPROGRESS if the tray is locked and the guest has been notified.
Maybe:
Callers may choose whether to treat -ENOSYS (device has no tray) or
-EINPROGRESS (tray is locked but guest has been notified) as non-fatal
errors.
But what you have works, enough that I'm okay with:
Reviewed-by: Eric Blake <eblake@redhat.com>
--
Eric Blake eblake redhat com +1-919-301-3266
Libvirt virtualization library http://libvirt.org
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 604 bytes --]
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [Qemu-devel] [PATCH v2] blockdev: clean up error handling in do_open_tray
2016-06-03 19:39 [Qemu-devel] [PATCH v2] blockdev: clean up error handling in do_open_tray Colin Lord
2016-06-03 20:07 ` Kevin Wolf
2016-06-03 20:19 ` Eric Blake
@ 2016-06-06 7:59 ` Markus Armbruster
2 siblings, 0 replies; 4+ messages in thread
From: Markus Armbruster @ 2016-06-06 7:59 UTC (permalink / raw)
To: Colin Lord; +Cc: qemu-devel, kwolf, qemu-block, mreitz
Colin Lord <clord@redhat.com> writes:
> Returns negative error codes and accompanying error messages in cases where
> the device has no tray or the tray is locked and isn't forced open. This
> extra information should result in better flexibility in functions that
> call do_open_tray.
>
> Signed-off-by: Colin Lord <clord@redhat.com>
> ---
> v2: fix function documentation, improve commit wording, and remove
> unnecessary null check
> blockdev.c | 36 +++++++++++++++++++++---------------
> 1 file changed, 21 insertions(+), 15 deletions(-)
>
> diff --git a/blockdev.c b/blockdev.c
> index 717785e..d50a2a5 100644
> --- a/blockdev.c
> +++ b/blockdev.c
> @@ -2286,17 +2286,14 @@ void qmp_eject(const char *device, bool has_force, bool force, Error **errp)
> }
>
> rc = do_open_tray(device, force, &local_err);
> - if (local_err) {
> + if (rc == -ENOSYS) {
> + error_free(local_err);
> + local_err = NULL;
> + } else if (local_err) {
> error_propagate(errp, local_err);
> return;
> }
I like to put the error case in a conditional, and leave the normal flow
unindented, because it makes the normal flow easier to follow:
if (rc && rc != -ENOSYS) {
error_propagate(errp, local_err);
return;
}
error_free(local_err);
error_free(NULL) is safe.
>
> - if (rc == EINPROGRESS) {
> - error_setg(errp, "Device '%s' is locked and force was not specified, "
> - "wait for tray to open and try again", device);
> - return;
> - }
> -
> qmp_x_blockdev_remove_medium(device, errp);
> }
>
> @@ -2325,10 +2322,9 @@ void qmp_block_passwd(bool has_device, const char *device,
> }
>
> /**
> - * returns -errno on fatal error, +errno for non-fatal situations.
> - * errp will always be set when the return code is negative.
> - * May return +ENOSYS if the device has no tray,
> - * or +EINPROGRESS if the tray is locked and the guest has been notified.
> + * returns -errno on all errors, and errp will be set on error
> + * May return the non-fatal error codes -ENOSYS if the device has no tray,
> + * or -EINPROGRESS if the tray is locked and the guest has been notified.
> */
What does the function do?
What does it return on success?
Suggest imperative mood.
Here's my try:
/*
* Attempt to open the tray of @device.
* If @force, ignore its tray lock.
* Else, if the tray is locked, don't open it, but ask the guest to
* open it.
* On error, store an error through @errp and return -errno.
* If @device does not exist, return -ENODEV.
* If it has no removable media, return -ENOTSUP.
* If it has no tray, return -ENOSYS.
* If the guest was asked to open the tray, return -EINPROGRESS.
* Else, return 0.
*/
> static int do_open_tray(const char *device, bool force, Error **errp)
> {
> @@ -2348,8 +2344,8 @@ static int do_open_tray(const char *device, bool force, Error **errp)
> }
>
> if (!blk_dev_has_tray(blk)) {
> - /* Ignore this command on tray-less devices */
> - return ENOSYS;
> + error_setg(errp, "Device '%s' does not have a tray", device);
> + return -ENOSYS;
> }
>
> if (blk_dev_is_tray_open(blk)) {
> @@ -2366,7 +2362,9 @@ static int do_open_tray(const char *device, bool force, Error **errp)
> }
>
> if (locked && !force) {
> - return EINPROGRESS;
> + error_setg(errp, "Device '%s' is locked and force was not specified, "
> + "wait for tray to open and try again", device);
> + return -EINPROGRESS;
> }
>
> return 0;
> @@ -2375,10 +2373,18 @@ static int do_open_tray(const char *device, bool force, Error **errp)
> void qmp_blockdev_open_tray(const char *device, bool has_force, bool force,
> Error **errp)
> {
> + Error *local_err = NULL;
> + int rc;
> +
> if (!has_force) {
> force = false;
> }
> - do_open_tray(device, force, errp);
> + rc = do_open_tray(device, force, &local_err);
> + if (rc == -EINPROGRESS || rc == -ENOSYS) {
> + error_free(local_err);
> + } else {
> + error_propagate(errp, local_err);
> + }
> }
Likewise:
if (rc && rc != -ENOSYS && rc != EINPROGRESS) {
error_propagate(errp, local_err);
return;
}
error_free(local_err);
>
> void qmp_blockdev_close_tray(const char *device, Error **errp)
While you're cleaning up: mind moving the forward declaration of
do_open_tray() to the beginning?
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2016-06-06 7:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-06-03 19:39 [Qemu-devel] [PATCH v2] blockdev: clean up error handling in do_open_tray Colin Lord
2016-06-03 20:07 ` Kevin Wolf
2016-06-03 20:19 ` Eric Blake
2016-06-06 7:59 ` Markus Armbruster
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).