All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
Cc: kwolf@redhat.com, pkrempa@redhat.com, berrange@redhat.com,
	ehabkost@redhat.com, qemu-block@nongnu.org,
	libvir-list@redhat.com, dim@virtuozzo.com, igor@virtuozzo.com,
	armbru@redhat.com, qemu-devel@nongnu.org, yur@virtuozzo.com,
	nshirokovskiy@virtuozzo.com, stefanha@redhat.com, den@openvz.org,
	pbonzini@redhat.com, mreitz@redhat.com, eblake@redhat.com
Subject: Re: [PATCH 2/8] block: add BlockParentClass class
Date: Mon, 20 Sep 2021 07:28:20 +0200	[thread overview]
Message-ID: <87wnnb4xzf.fsf@dusky.pond.sub.org> (raw)
In-Reply-To: <20210802185416.50877-3-vsementsov@virtuozzo.com> (Vladimir Sementsov-Ogievskiy's message of "Mon, 2 Aug 2021 21:54:10 +0300")

Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com> writes:

> Add a class that will unify block parents for blockdev-replace
> functionality we are going to add.
>
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> ---
>  include/block/block-parent.h | 32 +++++++++++++++++
>  block/block-parent.c         | 66 ++++++++++++++++++++++++++++++++++++
>  block/meson.build            |  1 +
>  3 files changed, 99 insertions(+)
>  create mode 100644 include/block/block-parent.h
>  create mode 100644 block/block-parent.c
>
> diff --git a/include/block/block-parent.h b/include/block/block-parent.h
> new file mode 100644
> index 0000000000..bd9c9d91e6
> --- /dev/null
> +++ b/include/block/block-parent.h
> @@ -0,0 +1,32 @@
> +/*
> + * Block Parent class
> + *
> + * Copyright (c) 2021 Virtuozzo International GmbH.
> + *
> + * Authors:
> + *  Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + */
> +
> +#ifndef BLOCK_PARENT_H
> +#define BLOCK_PARENT_H
> +
> +#include "block/block.h"
> +
> +typedef struct BlockParentClass {
> +    const char *name;
> +
> +    int (*find_child)(const char *parent_id, const char *child_name,
> +                      BlockDriverState *child_bs, BdrvChild **child,
> +                      Error **errp);
> +    QTAILQ_ENTRY(BlockParentClass) next;
> +} BlockParentClass;
> +
> +void block_parent_class_register(BlockParentClass *cls);
> +
> +BdrvChild *block_find_child(const char *parent_id, const char *child_name,
> +                            BlockDriverState *child_bs, Error **errp);
> +
> +#endif /* BLOCK_PARENT_H */
> diff --git a/block/block-parent.c b/block/block-parent.c
> new file mode 100644
> index 0000000000..73b6026c42
> --- /dev/null
> +++ b/block/block-parent.c
> @@ -0,0 +1,66 @@
> +/*
> + * Block Parent class
> + *
> + * Copyright (c) 2021 Virtuozzo International GmbH.
> + *
> + * Authors:
> + *  Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
> + *
> + * This work is licensed under the terms of the GNU GPL, version 2 or later.
> + * See the COPYING file in the top-level directory.
> + */
> +
> +#include "qemu/osdep.h"
> +#include "block/block-parent.h"
> +#include "qapi/error.h"
> +
> +static QTAILQ_HEAD(, BlockParentClass) block_parent_classes =
> +    QTAILQ_HEAD_INITIALIZER(block_parent_classes);
> +
> +void block_parent_class_register(BlockParentClass *cls)
> +{
> +    QTAILQ_INSERT_HEAD(&block_parent_classes, cls, next);
> +}
> +
> +BdrvChild *block_find_child(const char *parent_id, const char *child_name,
> +                            BlockDriverState *child_bs, Error **errp)
> +{
> +    BdrvChild *found_child = NULL;
> +    BlockParentClass *found_cls = NULL, *cls;
> +
> +    QTAILQ_FOREACH(cls, &block_parent_classes, next) {
> +        int ret;
> +        BdrvChild *c;
> +
> +        /*
> +         * Note that .find_child must fail if parent is found but doesn't have
> +         * corresponding child.
> +         */
> +        ret = cls->find_child(parent_id, child_name, child_bs, &c, errp);
> +        if (ret < 0) {
> +            return NULL;

Interesting: when one method fails, the entire function fails, even when
other methods succeed.  The contract should probably spell this out.

> +        }
> +        if (ret == 0) {
> +            continue;
> +        }
> +
> +        if (!found_child) {
> +            found_cls = cls;
> +            found_child = c;
> +            continue;
> +        }
> +
> +        error_setg(errp, "{%s, %s} parent-child pair is ambiguous: it match "
> +                   "both %s and %s", parent_id, child_name, found_cls->name,
> +                   cls->name);
> +        return NULL;
> +    }

Style recommendation: if very much prefer

           if bad
               error out
           normal

over

           if ok
               normal
           bad

In this case:

           if (found_child) {
               error_setg(...);
               return 0;
           }

           found_cls = cls;
           found_child = c;
       }

> +
> +    if (!found_child) {
> +        error_setg(errp, "{%s, %s} parent-child pair not found", parent_id,
> +                   child_name);
> +        return NULL;
> +    }
> +
> +    return found_child;
> +}
> diff --git a/block/meson.build b/block/meson.build
> index 0450914c7a..5200e0ffce 100644
> --- a/block/meson.build
> +++ b/block/meson.build
> @@ -10,6 +10,7 @@ block_ss.add(files(
>    'blkverify.c',
>    'block-backend.c',
>    'block-copy.c',
> +  'block-parent.c',
>    'commit.c',
>    'copy-on-read.c',
>    'preallocate.c',



  parent reply	other threads:[~2021-09-20  5:30 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-02 18:54 [PATCH RFC 0/8] blockdev-replace Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 1/8] block-backend: blk_root(): drop const specifier on return type Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 2/8] block: add BlockParentClass class Vladimir Sementsov-Ogievskiy
2021-09-16  8:34   ` Markus Armbruster
2021-09-16 10:12     ` Vladimir Sementsov-Ogievskiy
2021-09-20  5:28   ` Markus Armbruster [this message]
2021-08-02 18:54 ` [PATCH 3/8] block: realize BlockParentClass for BlockDriverState Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 4/8] block/export: realize BlockParentClass functionality Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 5/8] qdev: improve find_device_state() to distinguish simple not found case Vladimir Sementsov-Ogievskiy
2021-09-16 10:48   ` Markus Armbruster
2021-09-16 12:54     ` Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 6/8] qdev: realize BlockParentClass Vladimir Sementsov-Ogievskiy
2021-09-20  6:08   ` Markus Armbruster
2021-08-02 18:54 ` [PATCH 7/8] block: improve bdrv_replace_node_noperm() Vladimir Sementsov-Ogievskiy
2021-08-02 18:54 ` [PATCH 8/8] qapi: add blockdev-replace command Vladimir Sementsov-Ogievskiy
2021-09-20  6:44   ` Markus Armbruster
2021-09-20 10:02     ` Vladimir Sementsov-Ogievskiy
2021-09-23 10:09       ` Markus Armbruster
2021-09-23 11:54         ` Vladimir Sementsov-Ogievskiy
2021-09-20 11:25   ` Vladimir Sementsov-Ogievskiy
2021-09-02  9:28 ` [PATCH RFC 0/8] blockdev-replace Vladimir Sementsov-Ogievskiy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wnnb4xzf.fsf@dusky.pond.sub.org \
    --to=armbru@redhat.com \
    --cc=berrange@redhat.com \
    --cc=den@openvz.org \
    --cc=dim@virtuozzo.com \
    --cc=eblake@redhat.com \
    --cc=ehabkost@redhat.com \
    --cc=igor@virtuozzo.com \
    --cc=kwolf@redhat.com \
    --cc=libvir-list@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=nshirokovskiy@virtuozzo.com \
    --cc=pbonzini@redhat.com \
    --cc=pkrempa@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=stefanha@redhat.com \
    --cc=vsementsov@virtuozzo.com \
    --cc=yur@virtuozzo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.