All of lore.kernel.org
 help / color / mirror / Atom feed
From: Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>
To: Markus Armbruster <armbru@redhat.com>
Cc: qemu-block@nongnu.org, qemu-devel@nongnu.org,
	ehabkost@redhat.com, berrange@redhat.com, pbonzini@redhat.com,
	eblake@redhat.com, mreitz@redhat.com, kwolf@redhat.com,
	den@openvz.org, nshirokovskiy@virtuozzo.com, yur@virtuozzo.com,
	dim@virtuozzo.com, igor@virtuozzo.com, pkrempa@redhat.com,
	libvir-list@redhat.com, stefanha@redhat.com
Subject: Re: [PATCH 2/8] block: add BlockParentClass class
Date: Thu, 16 Sep 2021 13:12:15 +0300	[thread overview]
Message-ID: <d9ba6476-ab0a-6314-81c1-054519bc1469@virtuozzo.com> (raw)
In-Reply-To: <87h7ekapfw.fsf@dusky.pond.sub.org>

16.09.2021 11:34, Markus Armbruster wrote:
> 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);
> 
> Callbacks should come with a contract.

will add

> 
>> +    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;
>> +        }
>> +        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;
>> +    }
>> +
>> +    if (!found_child) {
>> +        error_setg(errp, "{%s, %s} parent-child pair not found", parent_id,
>> +                   child_name);
>> +        return NULL;
>> +    }
>> +
>> +    return found_child;
>> +}
> 
> Especially when the callback can return NULL with and without setting an
> error!

Hmm. Callback returns int.

And this block_find_child() function return NULL only together with errp set.

> 
>> 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',
> 


-- 
Best regards,
Vladimir


  reply	other threads:[~2021-09-16 10:33 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 [this message]
2021-09-20  5:28   ` Markus Armbruster
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=d9ba6476-ab0a-6314-81c1-054519bc1469@virtuozzo.com \
    --to=vsementsov@virtuozzo.com \
    --cc=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=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.