qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
* [Qemu-devel] [PATCH] file-posix: Use error API properly
@ 2018-10-22 14:02 Fam Zheng
  2018-10-23  5:09 ` Markus Armbruster
  0 siblings, 1 reply; 2+ messages in thread
From: Fam Zheng @ 2018-10-22 14:02 UTC (permalink / raw)
  To: qemu-devel; +Cc: Kevin Wolf, Max Reitz, qemu-block, armbru

Use error_report for situations that affect user operation (i.e.  we're
actually returning error), and warn_report/warn_report_err when some
less critical error happened but the user operation can still carry on.

Suggested-by: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Fam Zheng <famz@redhat.com>
---
 block/file-posix.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/block/file-posix.c b/block/file-posix.c
index 2da3a76355..2a46899313 100644
--- a/block/file-posix.c
+++ b/block/file-posix.c
@@ -214,8 +214,7 @@ static int raw_normalize_devicepath(const char **filename)
     fname = *filename;
     dp = strrchr(fname, '/');
     if (lstat(fname, &sb) < 0) {
-        fprintf(stderr, "%s: stat failed: %s\n",
-            fname, strerror(errno));
+        error_report("%s: stat failed: %s", fname, strerror(errno));
         return -errno;
     }
 
@@ -229,9 +228,8 @@ static int raw_normalize_devicepath(const char **filename)
         snprintf(namebuf, PATH_MAX, "%.*s/r%s",
             (int)(dp - fname), fname, dp + 1);
     }
-    fprintf(stderr, "%s is a block device", fname);
     *filename = namebuf;
-    fprintf(stderr, ", using %s\n", *filename);
+    warn_report("%s is a block device, using %s", fname, *filename);
 
     return 0;
 }
@@ -492,11 +490,11 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
     case ON_OFF_AUTO_ON:
         s->use_lock = true;
         if (!qemu_has_ofd_lock()) {
-            fprintf(stderr,
+            warn_report(
                     "File lock requested but OFD locking syscall is "
-                    "unavailable, falling back to POSIX file locks.\n"
+                    "unavailable, falling back to POSIX file locks. "
                     "Due to the implementation, locks can be lost "
-                    "unexpectedly.\n");
+                    "unexpectedly.");
         }
         break;
     case ON_OFF_AUTO_OFF:
@@ -805,7 +803,7 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
             /* Theoretically the above call only unlocks bytes and it cannot
              * fail. Something weird happened, report it.
              */
-            error_report_err(local_err);
+            warn_report_err(local_err);
         }
         break;
     case RAW_PL_COMMIT:
@@ -815,7 +813,7 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
             /* Theoretically the above call only unlocks bytes and it cannot
              * fail. Something weird happened, report it.
              */
-            error_report_err(local_err);
+            warn_report_err(local_err);
         }
         break;
     }
@@ -1775,7 +1773,7 @@ static int aio_worker(void *arg)
         ret = handle_aiocb_truncate(aiocb);
         break;
     default:
-        fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
+        error_report("invalid aio request (0x%x)", aiocb->aio_type);
         ret = -EINVAL;
         break;
     }
@@ -2263,7 +2261,7 @@ out_unlock:
          * not mean the whole creation operation has failed.  So
          * report it the user for their convenience, but do not report
          * it to the caller. */
-        error_report_err(local_err);
+        warn_report_err(local_err);
     }
 
 out_close:
-- 
2.17.1

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [Qemu-devel] [PATCH] file-posix: Use error API properly
  2018-10-22 14:02 [Qemu-devel] [PATCH] file-posix: Use error API properly Fam Zheng
@ 2018-10-23  5:09 ` Markus Armbruster
  0 siblings, 0 replies; 2+ messages in thread
From: Markus Armbruster @ 2018-10-23  5:09 UTC (permalink / raw)
  To: Fam Zheng; +Cc: qemu-devel, Kevin Wolf, qemu-block, Max Reitz

Fam Zheng <famz@redhat.com> writes:

> Use error_report for situations that affect user operation (i.e.  we're
> actually returning error), and warn_report/warn_report_err when some
> less critical error happened but the user operation can still carry on.
>
> Suggested-by: Markus Armbruster <armbru@redhat.com>
> Signed-off-by: Fam Zheng <famz@redhat.com>
> ---
>  block/file-posix.c | 20 +++++++++-----------
>  1 file changed, 9 insertions(+), 11 deletions(-)
>
> diff --git a/block/file-posix.c b/block/file-posix.c
> index 2da3a76355..2a46899313 100644
> --- a/block/file-posix.c
> +++ b/block/file-posix.c
> @@ -214,8 +214,7 @@ static int raw_normalize_devicepath(const char **filename)
>      fname = *filename;
>      dp = strrchr(fname, '/');
>      if (lstat(fname, &sb) < 0) {
> -        fprintf(stderr, "%s: stat failed: %s\n",
> -            fname, strerror(errno));
> +        error_report("%s: stat failed: %s", fname, strerror(errno));
>          return -errno;
>      }

raw_normalize_devicepath() is called from functions taking an Error
** argument, like this:

    ret = raw_normalize_devicepath(&filename);
    if (ret != 0) {
        error_setg_errno(errp, -ret, "Could not normalize device path");
        goto fail;
    }

If it fails, we get two error messages, first a specific one from
raw_normalize_devicepath(), then a generic one from whatever reports the
Error created by its caller.

The first message goes to stderr or the current HMP monitor.

The second could go to QMP instead, or be suppressed entirely.

You should convert raw_normalize_devicepath() to Error so you can create
just an Error.  This patch is an improvement even without that, of
course, and I'm therefore not withholding my r-by just for that.

Please also check the other functions that use error_report().

>  
> @@ -229,9 +228,8 @@ static int raw_normalize_devicepath(const char **filename)
>          snprintf(namebuf, PATH_MAX, "%.*s/r%s",
>              (int)(dp - fname), fname, dp + 1);
>      }
> -    fprintf(stderr, "%s is a block device", fname);
>      *filename = namebuf;
> -    fprintf(stderr, ", using %s\n", *filename);
> +    warn_report("%s is a block device, using %s", fname, *filename);
>  
>      return 0;
>  }
> @@ -492,11 +490,11 @@ static int raw_open_common(BlockDriverState *bs, QDict *options,
>      case ON_OFF_AUTO_ON:
>          s->use_lock = true;
>          if (!qemu_has_ofd_lock()) {
> -            fprintf(stderr,
> +            warn_report(
>                      "File lock requested but OFD locking syscall is "
> -                    "unavailable, falling back to POSIX file locks.\n"
> +                    "unavailable, falling back to POSIX file locks. "
>                      "Due to the implementation, locks can be lost "
> -                    "unexpectedly.\n");
> +                    "unexpectedly.");

warn_report()'s contract stipulates "The resulting message should be a
single phrase, with no newline or trailing punctuation."  The message
should be split into a warning that complies with the contract and
additional hints.  For an example see my "[PATCH v4 04/38] cpus hw
target: Use warn_report() & friends to report warnings".

>          }
>          break;
>      case ON_OFF_AUTO_OFF:
> @@ -805,7 +803,7 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
>              /* Theoretically the above call only unlocks bytes and it cannot
>               * fail. Something weird happened, report it.
>               */
> -            error_report_err(local_err);
> +            warn_report_err(local_err);
>          }
>          break;
>      case RAW_PL_COMMIT:
> @@ -815,7 +813,7 @@ static int raw_handle_perm_lock(BlockDriverState *bs,
>              /* Theoretically the above call only unlocks bytes and it cannot
>               * fail. Something weird happened, report it.
>               */
> -            error_report_err(local_err);
> +            warn_report_err(local_err);
>          }
>          break;
>      }
> @@ -1775,7 +1773,7 @@ static int aio_worker(void *arg)
>          ret = handle_aiocb_truncate(aiocb);
>          break;
>      default:
> -        fprintf(stderr, "invalid aio request (0x%x)\n", aiocb->aio_type);
> +        error_report("invalid aio request (0x%x)", aiocb->aio_type);
>          ret = -EINVAL;
>          break;
>      }
> @@ -2263,7 +2261,7 @@ out_unlock:
>           * not mean the whole creation operation has failed.  So
>           * report it the user for their convenience, but do not report
>           * it to the caller. */
> -        error_report_err(local_err);
> +        warn_report_err(local_err);
>      }
>  
>  out_close:

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2018-10-23  5:22 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-10-22 14:02 [Qemu-devel] [PATCH] file-posix: Use error API properly Fam Zheng
2018-10-23  5:09 ` 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).