From: Colin Lord <clord@redhat.com>
To: qemu-devel@nongnu.org
Cc: kwolf@redhat.com, qemu-block@nongnu.org, mreitz@redhat.com,
Colin Lord <clord@redhat.com>
Subject: [Qemu-devel] [PATCH v3 32/32] blockdev: Remove bdrv_probe_device field from BlockDriver
Date: Tue, 5 Jul 2016 11:24:32 -0400 [thread overview]
Message-ID: <1467732272-23368-33-git-send-email-clord@redhat.com> (raw)
In-Reply-To: <1467732272-23368-1-git-send-email-clord@redhat.com>
This commit finalizes the separation of the BlockDriver from its
device probing function. Now the accesses to these functions in block.c
occur through the protocol_probes array, and each function returns a
score and protocol name with which to find the corresponding driver.
Signed-off-by: Colin Lord <clord@redhat.com>
---
block.c | 46 ++++++++++++++++++++++++++++++-----------
block/probe/host_cdrom.c | 23 ++++++++++++++-------
block/probe/host_device.c | 34 ++++++++++++++++++++----------
block/raw-posix.c | 3 ---
block/raw-win32.c | 1 -
include/block/block_int.h | 2 --
include/block/probe.h | 4 ++--
scripts/modules/module_block.py | 12 ++---------
8 files changed, 76 insertions(+), 49 deletions(-)
diff --git a/block.c b/block.c
index 7e441fe..bc1046b 100644
--- a/block.c
+++ b/block.c
@@ -59,6 +59,7 @@
typedef const char *BdrvProbeFunc(const uint8_t *buf, int buf_size,
const char *filename, int *score);
+typedef const char *BdrvProbeDevFunc(const char *filename, int *score);
static BdrvProbeFunc *format_probes[] = {
bochs_probe,
@@ -76,6 +77,13 @@ static BdrvProbeFunc *format_probes[] = {
vpc_probe
};
+static BdrvProbeDevFunc *protocol_probes[] = {
+ hdev_probe_device,
+#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__linux__)
+ cdrom_probe_device
+#endif
+};
+
static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states =
QTAILQ_HEAD_INITIALIZER(graph_bdrv_states);
@@ -95,6 +103,8 @@ static BlockDriverState *bdrv_open_inherit(const char *filename,
/* If non-zero, use only whitelisted block drivers */
static int use_bdrv_whitelist;
+static BlockDriver *bdrv_do_find_protocol(const char *protocol);
+
#ifdef _WIN32
static int is_windows_drive_prefix(const char *filename)
{
@@ -487,25 +497,37 @@ int get_tmp_filename(char *filename, int size)
static BlockDriver *find_hdev_driver(const char *filename)
{
int score_max = 0, score;
+ const char *protocol_max = NULL;
+ const char *protocol;
+ BlockDriver *drv;
size_t i;
- BlockDriver *drv = NULL, *d;
+
+ for (i = 0; i < ARRAY_SIZE(protocol_probes); i++) {
+ protocol = protocol_probes[i](filename, &score);
+ if (score > score_max) {
+ protocol_max = protocol;
+ score_max = score;
+ }
+ }
+
+ if (!protocol_max) {
+ return NULL;
+ }
+
+ drv = bdrv_do_find_protocol(protocol_max);
+ if (drv) {
+ return drv;
+ }
for (i = 0; i < ARRAY_SIZE(block_driver_modules); ++i) {
- if (block_driver_modules[i].has_probe_device) {
+ if (block_driver_modules[i].protocol_name &&
+ !strcmp(block_driver_modules[i].protocol_name, protocol_max)) {
block_module_load_one(block_driver_modules[i].library_name);
+ break;
}
}
- QLIST_FOREACH(d, &bdrv_drivers, list) {
- if (d->bdrv_probe_device) {
- score = d->bdrv_probe_device(filename);
- if (score > score_max) {
- score_max = score;
- drv = d;
- }
- }
- }
-
+ drv = bdrv_do_find_protocol(protocol);
return drv;
}
diff --git a/block/probe/host_cdrom.c b/block/probe/host_cdrom.c
index 1886cad..3f7d863 100644
--- a/block/probe/host_cdrom.c
+++ b/block/probe/host_cdrom.c
@@ -1,22 +1,28 @@
#include "qemu/osdep.h"
#include "block/probe.h"
+static const char *protocol = "host_cdrom";
+
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
-int cdrom_probe_device(const char *filename)
+const char *cdrom_probe_device(const char *filename, int *score)
{
+ assert(score);
if (strstart(filename, "/dev/cd", NULL) ||
- strstart(filename, "/dev/acd", NULL))
- return 100;
+ strstart(filename, "/dev/acd", NULL)) {
+ *score = 100;
+ return protocol;
+ }
return 0;
}
#elif defined(__linux__)
#include <sys/ioctl.h>
#include <linux/cdrom.h>
-int cdrom_probe_device(const char *filename)
+const char *cdrom_probe_device(const char *filename, int *score)
{
int fd, ret;
- int prio = 0;
struct stat st;
+ assert(score);
+ *score = 0;
fd = qemu_open(filename, O_RDONLY | O_NONBLOCK);
if (fd < 0) {
@@ -29,12 +35,13 @@ int cdrom_probe_device(const char *filename)
/* Attempt to detect via a CDROM specific ioctl */
ret = ioctl(fd, CDROM_DRIVE_STATUS, CDSL_CURRENT);
- if (ret >= 0)
- prio = 100;
+ if (ret >= 0) {
+ *score = 100;
+ }
outc:
qemu_close(fd);
out:
- return prio;
+ return protocol;
}
#endif
diff --git a/block/probe/host_device.c b/block/probe/host_device.c
index ebd969b..b4e4d20 100644
--- a/block/probe/host_device.c
+++ b/block/probe/host_device.c
@@ -2,29 +2,41 @@
#include "block/probe.h"
#include "qemu/cutils.h"
+static const char *protocol = "host_device";
+
#ifdef _WIN32
-int hdev_probe_device(const char *filename)
+const char *hdev_probe_device(const char *filename, int *score)
{
- if (strstart(filename, "/dev/cdrom", NULL))
- return 100;
- if (is_windows_drive(filename))
- return 100;
- return 0;
+ assert(score);
+ *score = 100;
+ if (strstart(filename, "/dev/cdrom", NULL)) {
+ return protocol;
+ }
+ if (is_windows_drive(filename)) {
+ return protocol
+ }
+ *score = 0;
+ return protocol;
}
#else
-int hdev_probe_device(const char *filename)
+const char *hdev_probe_device(const char *filename, int *score)
{
struct stat st;
+ assert(score);
/* allow a dedicated CD-ROM driver to match with a higher priority */
- if (strstart(filename, "/dev/cdrom", NULL))
- return 50;
+ if (strstart(filename, "/dev/cdrom", NULL)) {
+ *score = 50;
+ return protocol;
+ }
if (stat(filename, &st) >= 0 &&
(S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
- return 100;
+ *score = 100;
+ return protocol;
}
- return 0;
+ *score = 0;
+ return protocol;
}
#endif
diff --git a/block/raw-posix.c b/block/raw-posix.c
index 56b2952..9497f5b 100644
--- a/block/raw-posix.c
+++ b/block/raw-posix.c
@@ -2367,7 +2367,6 @@ static BlockDriver bdrv_host_device = {
.protocol_name = "host_device",
.instance_size = sizeof(BDRVRawState),
.bdrv_needs_filename = true,
- .bdrv_probe_device = hdev_probe_device,
.bdrv_parse_filename = hdev_parse_filename,
.bdrv_file_open = hdev_open,
.bdrv_close = raw_close,
@@ -2466,7 +2465,6 @@ static BlockDriver bdrv_host_cdrom = {
.protocol_name = "host_cdrom",
.instance_size = sizeof(BDRVRawState),
.bdrv_needs_filename = true,
- .bdrv_probe_device = cdrom_probe_device,
.bdrv_parse_filename = cdrom_parse_filename,
.bdrv_file_open = cdrom_open,
.bdrv_close = raw_close,
@@ -2592,7 +2590,6 @@ static BlockDriver bdrv_host_cdrom = {
.protocol_name = "host_cdrom",
.instance_size = sizeof(BDRVRawState),
.bdrv_needs_filename = true,
- .bdrv_probe_device = cdrom_probe_device,
.bdrv_parse_filename = cdrom_parse_filename,
.bdrv_file_open = cdrom_open,
.bdrv_close = raw_close,
diff --git a/block/raw-win32.c b/block/raw-win32.c
index 44cb503..8c9a3f8 100644
--- a/block/raw-win32.c
+++ b/block/raw-win32.c
@@ -696,7 +696,6 @@ static BlockDriver bdrv_host_device = {
.instance_size = sizeof(BDRVRawState),
.bdrv_needs_filename = true,
.bdrv_parse_filename = hdev_parse_filename,
- .bdrv_probe_device = hdev_probe_device,
.bdrv_file_open = hdev_open,
.bdrv_close = raw_close,
diff --git a/include/block/block_int.h b/include/block/block_int.h
index 2bca115..f0340c6 100644
--- a/include/block/block_int.h
+++ b/include/block/block_int.h
@@ -99,8 +99,6 @@ struct BlockDriver {
bool (*bdrv_recurse_is_first_non_filter)(BlockDriverState *bs,
BlockDriverState *candidate);
- int (*bdrv_probe_device)(const char *filename);
-
/* Any driver implementing this callback is expected to be able to handle
* NULL file names in its .bdrv_open() implementation */
void (*bdrv_parse_filename)(const char *filename, QDict *options, Error **errp);
diff --git a/include/block/probe.h b/include/block/probe.h
index 2732f56..c974414 100644
--- a/include/block/probe.h
+++ b/include/block/probe.h
@@ -27,7 +27,7 @@ const char *vmdk_probe(const uint8_t *buf, int buf_size, const char *filename,
int *score);
const char *vpc_probe(const uint8_t *buf, int buf_size, const char *filename,
int *score);
-int hdev_probe_device(const char *filename);
-int cdrom_probe_device(const char *filename);
+const char *hdev_probe_device(const char *filename, int *score);
+const char *cdrom_probe_device(const char *filename, int *score);
#endif
diff --git a/scripts/modules/module_block.py b/scripts/modules/module_block.py
index 18200e2..e0f5896 100644
--- a/scripts/modules/module_block.py
+++ b/scripts/modules/module_block.py
@@ -23,16 +23,13 @@ def get_string_struct(line):
return data[2].replace('"', '')[:-1]
-def add_module(fheader, library, format_name, protocol_name,
- probe_device):
+def add_module(fheader, library, format_name, protocol_name):
lines = []
lines.append('.library_name = "' + library + '",')
if format_name != "":
lines.append('.format_name = "' + format_name + '",')
if protocol_name != "":
lines.append('.protocol_name = "' + protocol_name + '",')
- if probe_device:
- lines.append('.has_probe_device = true,')
text = '\n\t'.join(lines)
fheader.write('\n\t{\n\t' + text + '\n\t},')
@@ -50,18 +47,14 @@ def process_file(fheader, filename):
format_name = get_string_struct(line)
elif line.find(".protocol_name") != -1:
protocol_name = get_string_struct(line)
- elif line.find(".bdrv_probe_device") != -1:
- probe_device = True
elif line == "};":
- add_module(fheader, library, format_name, protocol_name,
- probe_device)
+ add_module(fheader, library, format_name, protocol_name)
found_start = False
elif line.find("static BlockDriver") != -1:
found_something = True
found_start = True
format_name = ""
protocol_name = ""
- probe_device = False
if not found_something:
print("No BlockDriver struct found in " + filename + ". \
@@ -88,7 +81,6 @@ static const struct {
const char *format_name;
const char *protocol_name;
const char *library_name;
- bool has_probe_device;
} block_driver_modules[] = {''')
def print_bottom(fheader):
--
2.5.5
next prev parent reply other threads:[~2016-07-05 15:25 UTC|newest]
Thread overview: 89+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-07-05 15:24 [Qemu-devel] [PATCH v3 00/32] Dynamic module loading for block drivers Colin Lord
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 01/32] blockdev: prepare iSCSI block driver for dynamic loading Colin Lord
2016-07-06 2:41 ` Fam Zheng
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 02/32] blockdev: Add dynamic generation of module_block.h Colin Lord
2016-07-06 13:17 ` Max Reitz
2016-07-06 16:49 ` Colin Lord
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 03/32] blockdev: Add dynamic module loading for block drivers Colin Lord
2016-07-06 14:01 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 04/32] blockdev: Move bochs probe into separate file Colin Lord
2016-07-05 15:49 ` Daniel P. Berrange
2016-07-05 20:50 ` [Qemu-devel] [Qemu-block] " John Snow
2016-07-05 21:00 ` Max Reitz
2016-07-05 21:12 ` John Snow
2016-07-06 12:39 ` Max Reitz
2016-07-06 8:24 ` Kevin Wolf
2016-07-06 16:09 ` John Snow
2016-07-07 6:36 ` Markus Armbruster
2016-07-07 15:45 ` John Snow
2016-07-07 16:01 ` [Qemu-devel] " Paolo Bonzini
2016-07-07 16:14 ` John Snow
2016-07-08 9:31 ` Kevin Wolf
2016-07-07 20:32 ` [Qemu-devel] [Qemu-block] " Colin Lord
2016-07-08 9:37 ` Kevin Wolf
2016-07-08 7:17 ` Markus Armbruster
2016-07-07 15:59 ` [Qemu-devel] " Paolo Bonzini
2016-07-06 14:19 ` Max Reitz
2016-07-06 15:41 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 05/32] blockdev: Move cloop probe to its own file Colin Lord
2016-07-06 14:33 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 06/32] blockdev: Move luks " Colin Lord
2016-07-05 15:50 ` Daniel P. Berrange
2016-07-06 14:36 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 07/32] blockdev: Move dmg " Colin Lord
2016-07-06 14:39 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 08/32] blockdev: Move parallels " Colin Lord
2016-07-06 14:46 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 09/32] blockdev: Move qcow " Colin Lord
2016-07-06 14:49 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 10/32] blockdev: Move qcow2 " Colin Lord
2016-07-06 14:50 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 11/32] blockdev: Move qed " Colin Lord
2016-07-06 15:16 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 12/32] blockdev: Move raw " Colin Lord
2016-07-06 15:17 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 13/32] blockdev: Move vdi " Colin Lord
2016-07-06 15:21 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 14/32] blockdev: Move vhdx " Colin Lord
2016-07-06 15:22 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 15/32] blockdev: Move vmdk " Colin Lord
2016-07-06 15:27 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 16/32] blockdev: Move vpc " Colin Lord
2016-07-06 15:29 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 17/32] blockdev: Separate bochs probe from its driver Colin Lord
2016-07-06 15:43 ` Max Reitz
2016-07-06 15:59 ` Max Reitz
2016-07-07 14:56 ` Colin Lord
2016-07-08 9:57 ` Kevin Wolf
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 18/32] blockdev: Separate cloop " Colin Lord
2016-07-06 16:00 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 19/32] blockdev: Separate luks " Colin Lord
2016-07-06 16:03 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 20/32] blockdev: Separate dmg " Colin Lord
2016-07-06 16:05 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 21/32] blockdev: Separate parallels " Colin Lord
2016-07-06 16:08 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 22/32] blockdev: Separate qcow " Colin Lord
2016-07-06 16:09 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 23/32] blockdev: Separate qcow2 " Colin Lord
2016-07-06 16:10 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 24/32] blockdev: Separate qed " Colin Lord
2016-07-06 16:11 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 25/32] blockdev: Separate raw " Colin Lord
2016-07-06 16:11 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 26/32] blockdev: Separate vdi " Colin Lord
2016-07-06 16:12 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 27/32] blockdev: Separate vhdx " Colin Lord
2016-07-06 16:12 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 28/32] blockdev: Separate vmdk " Colin Lord
2016-07-06 16:13 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 29/32] blockdev: Separate vpc " Colin Lord
2016-07-06 16:14 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 30/32] blockdev: Remove the .bdrv_probe field from BlockDrivers Colin Lord
2016-07-06 16:17 ` Max Reitz
2016-07-05 15:24 ` [Qemu-devel] [PATCH v3 31/32] blockdev: Separate out bdrv_probe_device functions Colin Lord
2016-07-06 16:29 ` Max Reitz
2016-07-05 15:24 ` Colin Lord [this message]
2016-07-06 16:44 ` [Qemu-devel] [PATCH v3 32/32] blockdev: Remove bdrv_probe_device field from BlockDriver Max Reitz
2016-07-07 20:01 ` [Qemu-devel] [Qemu-block] [PATCH v3 00/32] Dynamic module loading for block drivers John Snow
2016-07-14 12:17 ` Stefan Hajnoczi
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=1467732272-23368-33-git-send-email-clord@redhat.com \
--to=clord@redhat.com \
--cc=kwolf@redhat.com \
--cc=mreitz@redhat.com \
--cc=qemu-block@nongnu.org \
--cc=qemu-devel@nongnu.org \
/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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).