diff mbox

[v4,17/32] blockdev: Separate bochs probe from its driver

Message ID 1468523008-30013-18-git-send-email-clord@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

clord@redhat.com July 14, 2016, 7:03 p.m. UTC
Modifies the bochs probe to return the format name as well as the
score as the final step of separating the probe function from the
driver. This keeps the probe completely independent of the driver,
making future modularization easier to accomplish. Returning the format
name as well as the score allows the score to be correlated to the
driver without the probe function needing to be part of the driver.

Signed-off-by: Colin Lord <clord@redhat.com>
---
 block.c               | 20 ++++++++++++++++++++
 block/bochs-probe.c   | 25 ++++++++++++++++---------
 block/bochs.c         |  1 -
 include/block/probe.h |  3 ++-
 4 files changed, 38 insertions(+), 11 deletions(-)

Comments

Max Reitz July 18, 2016, 4:28 p.m. UTC | #1
On 14.07.2016 21:03, Colin Lord wrote:
> Modifies the bochs probe to return the format name as well as the
> score as the final step of separating the probe function from the
> driver. This keeps the probe completely independent of the driver,
> making future modularization easier to accomplish. Returning the format
> name as well as the score allows the score to be correlated to the
> driver without the probe function needing to be part of the driver.
> 
> Signed-off-by: Colin Lord <clord@redhat.com>
> ---
>  block.c               | 20 ++++++++++++++++++++
>  block/bochs-probe.c   | 25 ++++++++++++++++---------
>  block/bochs.c         |  1 -
>  include/block/probe.h |  3 ++-
>  4 files changed, 38 insertions(+), 11 deletions(-)
> 
> diff --git a/block.c b/block.c
> index 3a0dc19..b16f8cf 100644
> --- a/block.c
> +++ b/block.c
> @@ -25,6 +25,7 @@
>  #include "trace.h"
>  #include "block/block_int.h"
>  #include "block/blockjob.h"
> +#include "block/probe.h"
>  #include "qemu/error-report.h"
>  #include "module_block.h"
>  #include "qemu/module.h"
> @@ -56,6 +57,13 @@
>  
>  #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
>  
> +typedef const char *BdrvProbeFunc(const uint8_t *buf, int buf_size,
> +                                  const char *filename, int *score);
> +
> +static BdrvProbeFunc *format_probes[] = {
> +    bdrv_bochs_probe,
> +};
> +

I really can't convince you of my "struct concept", can I? :-)

Unless maybe I can:

Reviewed-by: Max Reitz <mreitz@redhat.com>
clord@redhat.com July 18, 2016, 7:44 p.m. UTC | #2
On 07/18/2016 12:28 PM, Max Reitz wrote:
> On 14.07.2016 21:03, Colin Lord wrote:
>> Modifies the bochs probe to return the format name as well as the
>> score as the final step of separating the probe function from the
>> driver. This keeps the probe completely independent of the driver,
>> making future modularization easier to accomplish. Returning the format
>> name as well as the score allows the score to be correlated to the
>> driver without the probe function needing to be part of the driver.
>>
>> Signed-off-by: Colin Lord <clord@redhat.com>
>> ---
>>  block.c               | 20 ++++++++++++++++++++
>>  block/bochs-probe.c   | 25 ++++++++++++++++---------
>>  block/bochs.c         |  1 -
>>  include/block/probe.h |  3 ++-
>>  4 files changed, 38 insertions(+), 11 deletions(-)
>>
>> diff --git a/block.c b/block.c
>> index 3a0dc19..b16f8cf 100644
>> --- a/block.c
>> +++ b/block.c
>> @@ -25,6 +25,7 @@
>>  #include "trace.h"
>>  #include "block/block_int.h"
>>  #include "block/blockjob.h"
>> +#include "block/probe.h"
>>  #include "qemu/error-report.h"
>>  #include "module_block.h"
>>  #include "qemu/module.h"
>> @@ -56,6 +57,13 @@
>>  
>>  #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
>>  
>> +typedef const char *BdrvProbeFunc(const uint8_t *buf, int buf_size,
>> +                                  const char *filename, int *score);
>> +
>> +static BdrvProbeFunc *format_probes[] = {
>> +    bdrv_bochs_probe,
>> +};
>> +
> 
> I really can't convince you of my "struct concept", can I? :-)
> 
> Unless maybe I can:
> 
> Reviewed-by: Max Reitz <mreitz@redhat.com>
> 
If you really want me to I could go back and change it. I do have to
agree that it's a little odd to be returning the same string every time,
but it also strikes me as pretty weird if the probing functions
themselves can't even be associated with their drivers without the help
of another struct. It doesn't seem quite right to me if the functions
can't be used independently of the struct containing the mappings.
Technically of course they could still be used, but without the mappings
they are practically indistinguishable from each other so they're
basically useless.

Since both options seem to have their own quirks, I'm not too excited to
go back and edit all the patches only to end up with something I see as
equally weird. But again, if you (or others) really want me to change
it, or show me why having a bunch of indistinguishable probing functions
isn't as weird as I think, I could certainly do so.

Colin
diff mbox

Patch

diff --git a/block.c b/block.c
index 3a0dc19..b16f8cf 100644
--- a/block.c
+++ b/block.c
@@ -25,6 +25,7 @@ 
 #include "trace.h"
 #include "block/block_int.h"
 #include "block/blockjob.h"
+#include "block/probe.h"
 #include "qemu/error-report.h"
 #include "module_block.h"
 #include "qemu/module.h"
@@ -56,6 +57,13 @@ 
 
 #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
 
+typedef const char *BdrvProbeFunc(const uint8_t *buf, int buf_size,
+                                  const char *filename, int *score);
+
+static BdrvProbeFunc *format_probes[] = {
+    bdrv_bochs_probe,
+};
+
 static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
     QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
 
@@ -576,6 +584,8 @@  BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
                             const char *filename)
 {
     int score_max = 0, score;
+    const char *format_max = NULL;
+    const char *format;
     size_t i;
     BlockDriver *drv = NULL, *d;
 
@@ -595,6 +605,16 @@  BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size,
         }
     }
 
+    for (i = 0; i < ARRAY_SIZE(format_probes); i++) {
+        format = format_probes[i](buf, buf_size, filename, &score);
+        if (score > score_max) {
+            score_max = score;
+            format_max = format;
+            /* TODO: move call to find_format outside this loop */
+            drv = bdrv_find_format(format_max);
+        }
+    }
+
     return drv;
 }
 
diff --git a/block/bochs-probe.c b/block/bochs-probe.c
index befe2cf..57161dc 100644
--- a/block/bochs-probe.c
+++ b/block/bochs-probe.c
@@ -3,19 +3,26 @@ 
 #include "block/probe.h"
 #include "bochs.h"
 
-int bochs_probe(const uint8_t *buf, int buf_size, const char *filename)
+const char *bdrv_bochs_probe(const uint8_t *buf, int buf_size,
+                             const char *filename, int *score)
 {
+    const char *format = "bochs";
     const struct bochs_header *bochs = (const void *)buf;
+    assert(score);
+    *score = 0;
 
-    if (buf_size < HEADER_SIZE)
-	return 0;
+    if (buf_size < HEADER_SIZE) {
+        return format;
+    }
 
     if (!strcmp(bochs->magic, HEADER_MAGIC) &&
-	!strcmp(bochs->type, REDOLOG_TYPE) &&
-	!strcmp(bochs->subtype, GROWING_TYPE) &&
-	((le32_to_cpu(bochs->version) == HEADER_VERSION) ||
-	(le32_to_cpu(bochs->version) == HEADER_V1)))
-	return 100;
+        !strcmp(bochs->type, REDOLOG_TYPE) &&
+        !strcmp(bochs->subtype, GROWING_TYPE) &&
+        ((le32_to_cpu(bochs->version) == HEADER_VERSION) ||
+        (le32_to_cpu(bochs->version) == HEADER_V1))) {
+        *score = 100;
+        return format;
+    }
 
-    return 0;
+    return format;
 }
diff --git a/block/bochs.c b/block/bochs.c
index 5c9a696..7051ffc 100644
--- a/block/bochs.c
+++ b/block/bochs.c
@@ -234,7 +234,6 @@  static void bochs_close(BlockDriverState *bs)
 static BlockDriver bdrv_bochs = {
     .format_name	= "bochs",
     .instance_size	= sizeof(BDRVBochsState),
-    .bdrv_probe		= bochs_probe,
     .bdrv_open		= bochs_open,
     .bdrv_refresh_limits = bochs_refresh_limits,
     .bdrv_co_preadv = bochs_co_preadv,
diff --git a/include/block/probe.h b/include/block/probe.h
index 6cf878b..aac1fca 100644
--- a/include/block/probe.h
+++ b/include/block/probe.h
@@ -1,7 +1,6 @@ 
 #ifndef PROBE_H
 #define PROBE_H
 
-int bochs_probe(const uint8_t *buf, int buf_size, const char *filename);
 int cloop_probe(const uint8_t *buf, int buf_size, const char *filename);
 int block_crypto_probe_luks(const uint8_t *buf, int buf_size,
                             const char *filename);
@@ -15,5 +14,7 @@  int vdi_probe(const uint8_t *buf, int buf_size, const char *filename);
 int vhdx_probe(const uint8_t *buf, int buf_size, const char *filename);
 int vmdk_probe(const uint8_t *buf, int buf_size, const char *filename);
 int vpc_probe(const uint8_t *buf, int buf_size, const char *filename);
+const char *bdrv_bochs_probe(const uint8_t *buf, int buf_size,
+                             const char *filename, int *score);
 
 #endif