qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] block: Catch simultaneous usage of options and


From: Markus Armbruster
Subject: Re: [Qemu-devel] [PATCH] block: Catch simultaneous usage of options and their aliases
Date: Thu, 18 Sep 2014 16:06:27 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Eric Blake <address@hidden> writes:

> On 09/18/2014 03:57 AM, Kevin Wolf wrote:
>> While thinking about precedence of conflicting block device options from
>> different sources, I noticed that you can specify both an option and its
>> legacy alias at the same time (e.g. readonly=on,read-only=off). Rather
>> than specifying the order of precedence, we should simply forbid such
>> combinations.
>> 
>> Signed-off-by: Kevin Wolf <address@hidden>
>> ---
>>  blockdev.c | 46 +++++++++++++++++++++++++++++++---------------
>>  tests/qemu-iotests/051     | 23 +++++++++++++++++++++++
>>  tests/qemu-iotests/051.out | 45 
>> +++++++++++++++++++++++++++++++++++++++++++++
>>  3 files changed, 99 insertions(+), 15 deletions(-)
>
> Reviewed-by: Eric Blake <address@hidden>
>
>>  
>> -static void qemu_opt_rename(QemuOpts *opts, const char *from, const char 
>> *to)
>> +static void qemu_opt_rename(QemuOpts *opts, const char *from, const char 
>> *to,
>> +                            Error **errp)
>>  {
>>      const char *value;
>>  
>> +    if (*errp) {
>> +        return;
>> +    }
>
> Not the most typical usage, so it might be worth a comment that this
> function can be called with errp already set.  But since it's static,
> it's not too hard to figure out as-is.

The problem with this usage is it doesn't combine well with the common
usage.  That's why I purged it from the code back in May:

commit 297a3646c2947ee64a6d42ca264039732c6218e0
Author: Markus Armbruster <address@hidden>
Date:   Wed May 7 09:53:54 2014 +0200

    qapi: Replace uncommon use of the error API by the common one
    
    We commonly use the error API like this:
    
        err = NULL;
        foo(..., &err);
        if (err) {
            goto out;
        }
        bar(..., &err);
    
    Every error source is checked separately.  The second function is only
    called when the first one succeeds.  Both functions are free to pass
    their argument to error_set().  Because error_set() asserts no error
    has been set, this effectively means they must not be called with an
    error set.
    
    The qapi-generated code uses the error API differently:
    
        // *errp was initialized to NULL somewhere up the call chain
        frob(..., errp);
        gnat(..., errp);
    
    Errors accumulate in *errp: first error wins, subsequent errors get
    dropped.  To make this work, the second function does nothing when
    called with an error set.  Requires non-null errp, or else the second
    function can't see the first one fail.
    
    This usage has also bled into visitor tests, and two device model
    object property getters rtc_get_date() and balloon_stats_get_all().
    
    With the "accumulate" technique, you need fewer error checks in
    callers, and buy that with an error check in every callee.  Can be
    nice.
    
    However, mixing the two techniques is confusing.  You can't use the
    "accumulate" technique with functions designed for the "check
    separately" technique.  You can use the "check separately" technique
    with functions designed for the "accumulate" technique, but then
    error_set() can't catch you setting an error more than once.
    
    Standardize on the "check separately" technique for now, because it's
    overwhelmingly prevalent.
    
    Signed-off-by: Markus Armbruster <address@hidden>
    Reviewed-by: Eric Blake <address@hidden>
    Signed-off-by: Luiz Capitulino <address@hidden>

See also the thread leading up to this commit:
https://lists.nongnu.org/archive/html/qemu-devel/2014-04/msg01424.html
in particular the part starting at
https://lists.nongnu.org/archive/html/qemu-devel/2014-04/msg01830.html

I guess you're about as thrilled by what the common usage does to your
code as I am:

    qemu_opt_rename(all_opts, "iops", "throttling.iops-total", &local_err);
    if (local_err) {
        goto fail_opt_rename;
    }
    qemu_opt_rename(all_opts, "iops_rd", "throttling.iops-read", &local_err);
    if (local_err) {
        goto fail_opt_rename;
    }
    qemu_opt_rename(all_opts, "iops_wr", "throttling.iops-write", &local_err);
    if (local_err) {
        goto fail_opt_rename;
    }
[11 instances of the same pattern mercifully omitted...]

fail_opt_rename:
    error_report("%s", error_get_pretty(local_err));
    error_free(local_err);
    return NULL;

But this code is annoyingly repetitive even before the patch!  Here's
what I'd do:

    static struct {
        const char *from, *to;
    } rename[] = {
        { "iops", "throttling.iops-total" },
        { "iops_rd", "throttling.iops-read" },
        { "iops_wr", "throttling.iops-write" },
        { "bps", "throttling.bps-total" },
        { "bps_rd", "throttling.bps-read" },
        { "bps_wr", "throttling.bps-write" },
        { "iops_max", "throttling.iops-total-max" },
        { "iops_rd_max", "throttling.iops-read-max" },
        { "iops_wr_max", "throttling.iops-write-max" },
        { "bps_max", "throttling.bps-total-max" },
        { "bps_rd_max", "throttling.bps-read-max" },
        { "bps_wr_max", "throttling.bps-write-max" },
        { "iops_size", "throttling.iops-size" },
        { "readonly", "read-only" },
    };
    Error *local_err = NULL;
    int i;

    for (i = 0; i < ARRAY_SIZE(rename); i++) {
        qemu_opt_rename(all_opts, rename[i].from, rename[i].to,
                        &local_err);
        if (local_err) {
            error_report("%s", error_get_pretty(local_err));
            error_free(local_err);
            return NULL;
        }
    }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]