Message ID | 1467732272-23368-18-git-send-email-clord@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
On 05.07.2016 17:24, 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 | 19 +++++++++++++++++++ > block/bochs.c | 1 - > block/probe/bochs.c | 25 ++++++++++++++++--------- > include/block/probe.h | 3 ++- > 4 files changed, 37 insertions(+), 11 deletions(-) As I've proposed before, maybe this would be a good place to rename the probing function to bdrv_bochs_probe() since it is no longer a static function. > > diff --git a/block.c b/block.c > index 88a05b2..eab8a6e 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[] = { > + bochs_probe, > +}; Works, but... Eh. Something like the following would suit my personal tastes (I think by now everyone should have realized that I have horrible taste) better: typedef struct BdrvProbeFunc { const char *format_name; int (*probe)(const uint8_t *buf, int buf_size, const char *filename); } BdrvProbeFunc; static BdrvProbeFunc *format_probes[] = { { "bochs", bochs_probe }, }; It just feels strange to me that the probing function always returns a constant string. (This is an optional suggestion, you don't need to follow it.) > + > 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,15 @@ 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; > + drv = bdrv_find_format(format_max); > + } > + } I think the bdrv_find_format() call should be done after the loop (otherwise we may unnecessarily load some formats which we then actually don't use). > + > return drv; > } [...] > diff --git a/block/probe/bochs.c b/block/probe/bochs.c > index 8adc09f..8206930 100644 > --- a/block/probe/bochs.c > +++ b/block/probe/bochs.c > @@ -3,19 +3,26 @@ > #include "block/probe.h" > #include "block/driver/bochs.h" > > -int bochs_probe(const uint8_t *buf, int buf_size, const char *filename) > +const char *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; > + } Ah. Seems like I've complained too early. :-) Max > > - return 0; > + return format; > }
On 05.07.2016 17:24, 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 | 19 +++++++++++++++++++ > block/bochs.c | 1 - > block/probe/bochs.c | 25 ++++++++++++++++--------- > include/block/probe.h | 3 ++- > 4 files changed, 37 insertions(+), 11 deletions(-) Oh, and another comment: I think it would be better to split this patch into a part that modifies block.c (introducing the format_probes array and using it in bdrv_probe_all()) and another one that actually does the bochs-specific stuff. Max
On 07/06/2016 11:59 AM, Max Reitz wrote: > On 05.07.2016 17:24, 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 | 19 +++++++++++++++++++ >> block/bochs.c | 1 - >> block/probe/bochs.c | 25 ++++++++++++++++--------- >> include/block/probe.h | 3 ++- >> 4 files changed, 37 insertions(+), 11 deletions(-) > > Oh, and another comment: I think it would be better to split this patch > into a part that modifies block.c (introducing the format_probes array > and using it in bdrv_probe_all()) and another one that actually does the > bochs-specific stuff. > > Max > I could do that, but if I leave the variable i as an unsigned type, I get compiler warnings/errors that the for loop condition of i < ARRAY_SIZE(format_probes) must be false since the size of format_probes will be 0 (and unsigned is always positive). Is it okay to change the type to int? Not sure how strictly this aspect of the coding style gets enforced. Colin
Am 07.07.2016 um 16:56 hat Colin Lord geschrieben: > On 07/06/2016 11:59 AM, Max Reitz wrote: > > On 05.07.2016 17:24, 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 | 19 +++++++++++++++++++ > >> block/bochs.c | 1 - > >> block/probe/bochs.c | 25 ++++++++++++++++--------- > >> include/block/probe.h | 3 ++- > >> 4 files changed, 37 insertions(+), 11 deletions(-) > > > > Oh, and another comment: I think it would be better to split this patch > > into a part that modifies block.c (introducing the format_probes array > > and using it in bdrv_probe_all()) and another one that actually does the > > bochs-specific stuff. > > > > Max > > > I could do that, but if I leave the variable i as an unsigned type, I > get compiler warnings/errors that the for loop condition of > i < ARRAY_SIZE(format_probes) > must be false since the size of format_probes will be 0 (and unsigned is > always positive). Is it okay to change the type to int? Not sure how > strictly this aspect of the coding style gets enforced. Leave it unsigned. The resulting code after the series is more important than having the nicest possible split. If this means that you need to leave both parts in the same patch, so be it. Kevin
diff --git a/block.c b/block.c index 88a05b2..eab8a6e 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[] = { + 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,15 @@ 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; + drv = bdrv_find_format(format_max); + } + } + return drv; } diff --git a/block/bochs.c b/block/bochs.c index 11da0fd..5c94bc6 100644 --- a/block/bochs.c +++ b/block/bochs.c @@ -230,7 +230,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_co_preadv = bochs_co_preadv, .bdrv_close = bochs_close, diff --git a/block/probe/bochs.c b/block/probe/bochs.c index 8adc09f..8206930 100644 --- a/block/probe/bochs.c +++ b/block/probe/bochs.c @@ -3,19 +3,26 @@ #include "block/probe.h" #include "block/driver/bochs.h" -int bochs_probe(const uint8_t *buf, int buf_size, const char *filename) +const char *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/include/block/probe.h b/include/block/probe.h index 6cf878b..13c08bd 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 *bochs_probe(const uint8_t *buf, int buf_size, const char *filename, + int *score); #endif
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 | 19 +++++++++++++++++++ block/bochs.c | 1 - block/probe/bochs.c | 25 ++++++++++++++++--------- include/block/probe.h | 3 ++- 4 files changed, 37 insertions(+), 11 deletions(-)