[PATCH v9 2/7] block/export: add blk_by_export_id()

Vladimir Sementsov-Ogievskiy posted 7 patches 5 months ago
Maintainers: Kevin Wolf <kwolf@redhat.com>, Hanna Reitz <hreitz@redhat.com>, Eric Blake <eblake@redhat.com>, Markus Armbruster <armbru@redhat.com>, Paolo Bonzini <pbonzini@redhat.com>
[PATCH v9 2/7] block/export: add blk_by_export_id()
Posted by Vladimir Sementsov-Ogievskiy 5 months ago
We need it for further blockdev-replace functionality.

Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
---
 block/export/export.c                       | 18 ++++++++++++++++++
 include/sysemu/block-backend-global-state.h |  1 +
 2 files changed, 19 insertions(+)

diff --git a/block/export/export.c b/block/export/export.c
index 6d51ae8ed7..57beae7982 100644
--- a/block/export/export.c
+++ b/block/export/export.c
@@ -355,3 +355,21 @@ BlockExportInfoList *qmp_query_block_exports(Error **errp)
 
     return head;
 }
+
+BlockBackend *blk_by_export_id(const char *id, Error **errp)
+{
+    BlockExport *exp;
+
+    exp = blk_exp_find(id);
+    if (exp == NULL) {
+        error_setg(errp, "Export '%s' not found", id);
+        return NULL;
+    }
+
+    if (!exp->blk) {
+        error_setg(errp, "Export '%s' is empty", id);
+        return NULL;
+    }
+
+    return exp->blk;
+}
diff --git a/include/sysemu/block-backend-global-state.h b/include/sysemu/block-backend-global-state.h
index ccb35546a1..410d0cc5c7 100644
--- a/include/sysemu/block-backend-global-state.h
+++ b/include/sysemu/block-backend-global-state.h
@@ -74,6 +74,7 @@ void blk_detach_dev(BlockBackend *blk, DeviceState *dev);
 DeviceState *blk_get_attached_dev(BlockBackend *blk);
 BlockBackend *blk_by_dev(void *dev);
 BlockBackend *blk_by_qdev_id(const char *id, Error **errp);
+BlockBackend *blk_by_export_id(const char *id, Error **errp);
 void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, void *opaque);
 
 void blk_activate(BlockBackend *blk, Error **errp);
-- 
2.34.1
Re: [PATCH v9 2/7] block/export: add blk_by_export_id()
Posted by Markus Armbruster 4 months, 1 week ago
Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> writes:

> We need it for further blockdev-replace functionality.
>
> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
> ---
>  block/export/export.c                       | 18 ++++++++++++++++++
>  include/sysemu/block-backend-global-state.h |  1 +
>  2 files changed, 19 insertions(+)
>
> diff --git a/block/export/export.c b/block/export/export.c
> index 6d51ae8ed7..57beae7982 100644
> --- a/block/export/export.c
> +++ b/block/export/export.c
> @@ -355,3 +355,21 @@ BlockExportInfoList *qmp_query_block_exports(Error **errp)
>  
>      return head;
>  }
> +
> +BlockBackend *blk_by_export_id(const char *id, Error **errp)
> +{
> +    BlockExport *exp;
> +
> +    exp = blk_exp_find(id);
> +    if (exp == NULL) {
> +        error_setg(errp, "Export '%s' not found", id);
> +        return NULL;
> +    }
> +
> +    if (!exp->blk) {
> +        error_setg(errp, "Export '%s' is empty", id);

Can this happen?

> +        return NULL;
> +    }
> +
> +    return exp->blk;
> +}
> diff --git a/include/sysemu/block-backend-global-state.h b/include/sysemu/block-backend-global-state.h
> index ccb35546a1..410d0cc5c7 100644
> --- a/include/sysemu/block-backend-global-state.h
> +++ b/include/sysemu/block-backend-global-state.h
> @@ -74,6 +74,7 @@ void blk_detach_dev(BlockBackend *blk, DeviceState *dev);
>  DeviceState *blk_get_attached_dev(BlockBackend *blk);
>  BlockBackend *blk_by_dev(void *dev);
>  BlockBackend *blk_by_qdev_id(const char *id, Error **errp);
> +BlockBackend *blk_by_export_id(const char *id, Error **errp);
>  void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, void *opaque);
>  
>  void blk_activate(BlockBackend *blk, Error **errp);
Re: [PATCH v9 2/7] block/export: add blk_by_export_id()
Posted by Vladimir Sementsov-Ogievskiy 4 months, 1 week ago
On 18.07.24 14:48, Markus Armbruster wrote:
> Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru> writes:
> 
>> We need it for further blockdev-replace functionality.
>>
>> Signed-off-by: Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>
>> ---
>>   block/export/export.c                       | 18 ++++++++++++++++++
>>   include/sysemu/block-backend-global-state.h |  1 +
>>   2 files changed, 19 insertions(+)
>>
>> diff --git a/block/export/export.c b/block/export/export.c
>> index 6d51ae8ed7..57beae7982 100644
>> --- a/block/export/export.c
>> +++ b/block/export/export.c
>> @@ -355,3 +355,21 @@ BlockExportInfoList *qmp_query_block_exports(Error **errp)
>>   
>>       return head;
>>   }
>> +
>> +BlockBackend *blk_by_export_id(const char *id, Error **errp)
>> +{
>> +    BlockExport *exp;
>> +
>> +    exp = blk_exp_find(id);
>> +    if (exp == NULL) {
>> +        error_setg(errp, "Export '%s' not found", id);
>> +        return NULL;
>> +    }
>> +
>> +    if (!exp->blk) {
>> +        error_setg(errp, "Export '%s' is empty", id);
> 
> Can this happen?
> 

Hmm, looking at the code in blk_exp_add:

     assert(exp->blk != NULL);
                                                                                  
     QLIST_INSERT_HEAD(&block_exports, exp, next);
     return exp;


seems not. And I can't find anything changing exp->blk except for blk_exp_add().

Will switch to assertion.

>> +        return NULL;
>> +    }
>> +
>> +    return exp->blk;
>> +}
>> diff --git a/include/sysemu/block-backend-global-state.h b/include/sysemu/block-backend-global-state.h
>> index ccb35546a1..410d0cc5c7 100644
>> --- a/include/sysemu/block-backend-global-state.h
>> +++ b/include/sysemu/block-backend-global-state.h
>> @@ -74,6 +74,7 @@ void blk_detach_dev(BlockBackend *blk, DeviceState *dev);
>>   DeviceState *blk_get_attached_dev(BlockBackend *blk);
>>   BlockBackend *blk_by_dev(void *dev);
>>   BlockBackend *blk_by_qdev_id(const char *id, Error **errp);
>> +BlockBackend *blk_by_export_id(const char *id, Error **errp);
>>   void blk_set_dev_ops(BlockBackend *blk, const BlockDevOps *ops, void *opaque);
>>   
>>   void blk_activate(BlockBackend *blk, Error **errp);
> 

-- 
Best regards,
Vladimir