Skip to contentSkip to conversation

raw-posix: split hdev drivers

PATCH In review Christoph Hellwig 2 participants 5 messages May 25, 2009 – Jun 6, 2009 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 3/4 raw-posix: split hdev drivers

Instead of declaring one BlockDriver for all host devices declared one for each type: a generic one for normal disk devices, a Linux floppy driver and a CDROM driver for Linux and FreeBSD. This gets rid of a lot of messy ifdefs and switching based on the type in the various removal device methods.

block.c grows a new method to find the correct host device driver based on OS-sepcific criteria. I would love to move this into some OS-dependant file but I don't think we have a place where it fits nicely yet.

block.c +43 -17
Index: qemu/block.c
===================================================================
@@ -300,32 +300,55 @@ static BlockDriver *find_protocol(const 
     return NULL;
 }
 
-/* XXX: force raw format if block or character device ? It would
-   simplify the BSD case */
-static BlockDriver *find_image_format(const char *filename)
+/*
+ * Detect host devices. By convention, /dev/cdrom[N] is always
+ * recognized as a host CDROM.
+ */
+#ifdef _WIN32
+static BlockDriver *find_hdev_driver(const char *filename)
 {
-    int ret, score, score_max;
-    BlockDriver *drv1, *drv;
-    uint8_t buf[2048];
-    BlockDriverState *bs;
-
-    /* detect host devices. By convention, /dev/cdrom[N] is always
-       recognized as a host CDROM */
     if (strstart(filename, "/dev/cdrom", NULL))
         return bdrv_find_format("host_device");
-#ifdef _WIN32
     if (is_windows_drive(filename))
         return bdrv_find_format("host_device");
+    return NULL;
+}
+#else
+static BlockDriver *find_hdev_driver(const char *filename)
+{
+    struct stat st;
+
+#ifdef __linux__
+    if (strstart(filename, "/dev/fd", NULL))
+        return bdrv_find_format("host_floppy");
+    if (strstart(filename, "/dev/cd", NULL))
+        return bdrv_find_format("host_cdrom");
+#elif defined(__FreeBSD__)
+    if (strstart(filename, "/dev/cd", NULL) ||
+        strstart(filename, "/dev/acd", NULL)) {
+        return bdrv_find_format("host_cdrom");
+    }
 #else
-    {
-        struct stat st;
-        if (stat(filename, &st) >= 0 &&
+    if (strstart(filename, "/dev/cdrom", NULL))
+        return bdrv_find_format("host_device");
+#endif
+
+    if (stat(filename, &st) >= 0 &&
             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
-            return bdrv_find_format("host_device");
-        }
+        return bdrv_find_format("host_device");
     }
+
+    return NULL;
+}
 #endif
 
+static BlockDriver *find_image_format(const char *filename)
+{
+    int ret, score, score_max;
+    BlockDriver *drv1, *drv;
+    uint8_t buf[2048];
+    BlockDriverState *bs;
+
     drv = find_protocol(filename);
     /* no need to test disk image formats for vvfat */
     if (drv && strcmp(drv->format_name, "vvfat") == 0)
@@ -434,7 +457,10 @@ int bdrv_open2(BlockDriverState *bs, con
     if (flags & BDRV_O_FILE) {
         drv = find_protocol(filename);
     } else if (!drv) {
-        drv = find_image_format(filename);
+        drv = find_hdev_driver(filename);
+	if (!drv) {
+            drv = find_image_format(filename);
+        }
     }
     if (!drv) {
         ret = -ENOENT;
block/raw-posix.c +310 -260

640 lines not on this page · raw message

#1 lore raw
Quoted 11 lines · Christoph Hellwig

Instead of declaring one BlockDriver for all host devices declared one for each type: a generic one for normal disk devices, a Linux floppy driver and a CDROM driver for Linux and FreeBSD. This gets rid of a lot of messy ifdefs and switching based on the type in the various removal device methods.

block.c grows a new method to find the correct host device driver based on OS-sepcific criteria. I would love to move this into some OS-dependant file but I don't think we have a place where it fits nicely yet.

Add a ->probe_host_device() which accepts the filename (or maybe an fd) as a parameter. First pass does a ->probe_host_device() for all drivers that support it, second pass tries ->probe().

btw, ->probe_host_device() should use ioctls to identify the device, not the device name, which is controlled by udev (on Linux) and therefore nonstandard.

Signature · 1 line
error compiling committee.c: too many arguments to function
#2 lore raw
Quoted 12 lines · Avi Kivity
Quoted 11 lines

Instead of declaring one BlockDriver for all host devices declared one for each type: a generic one for normal disk devices, a Linux floppy driver and a CDROM driver for Linux and FreeBSD. This gets rid of a lot of messy ifdefs and switching based on the type in the various removal device methods.

block.c grows a new method to find the correct host device driver based on OS-sepcific criteria. I would love to move this into some OS-dependant file but I don't think we have a place where it fits nicely yet.

Quoted 3 lines

Add a ->probe_host_device() which accepts the filename (or maybe an fd) as a parameter. First pass does a ->probe_host_device() for all drivers that support it, second pass tries ->probe().

Yeah, we could probably do that. That means we could also move the is_windows_drive into raw-win32.c.

Quoted 3 lines

btw, ->probe_host_device() should use ioctls to identify the device, not the device name, which is controlled by udev (on Linux) and therefore nonstandard.

At least the /dev/cdrom name is a symbolic overload in qemu and should always resolve to a cdrom device, if it's for some reason not present in the host namespace we need to do similar hacks as the windows raw driver.

#3 lore raw
In reply to Avi Kivity
Quoted 13 lines · Avi Kivity
Quoted 11 lines · Christoph Hellwig

Instead of declaring one BlockDriver for all host devices declared one for each type: a generic one for normal disk devices, a Linux floppy driver and a CDROM driver for Linux and FreeBSD. This gets rid of a lot of messy ifdefs and switching based on the type in the various removal device methods.

block.c grows a new method to find the correct host device driver based on OS-sepcific criteria. I would love to move this into some OS-dependant file but I don't think we have a place where it fits nicely yet.

Quoted 3 lines

Add a ->probe_host_device() which accepts the filename (or maybe an fd) as a parameter. First pass does a ->probe_host_device() for all drivers that support it, second pass tries ->probe().

Here's a patch to implement that incrementally. I wonder why we have the is_windows_drive check in find_protocol - shouldn't that be handled entirely in find_hdev_driver?

block/raw-posix.c +43 -0
Index: qemu/block/raw-posix.c
===================================================================
@@ -946,6 +946,22 @@ kern_return_t GetBSDPath( io_iterator_t 
 
 #endif
 
+static int hdev_probe_device(const char *filename)
+{
+    struct stat st;
+
+    /* allow a dedicated CD-ROM driver to match with a higher priority */
+    if (strstart(filename, "/dev/cdrom", NULL))
+        return 50;
+
+    if (stat(filename, &st) >= 0 &&
+            (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
+        return 100;
+    }
+
+    return 0;
+}
+
 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
 {
     BDRVRawState *s = bs->opaque;
@@ -1138,6 +1154,7 @@ static int hdev_create(const char *filen
 static BlockDriver bdrv_host_device = {
     .format_name	= "host_device",
     .instance_size	= sizeof(BDRVRawState),
+    .bdrv_probe_device	= hdev_probe_device,
     .bdrv_open		= hdev_open,
     .bdrv_close		= raw_close,
     .bdrv_create        = hdev_create,
@@ -1187,6 +1204,14 @@ static int floppy_open(BlockDriverState 
     return 0;
 }
 
+static int floppy_probe_device(const char *filename)
+{
+    if (strstart(filename, "/dev/fd", NULL))
+        return 100;
+    return 0;
+}
+
+
 static int floppy_is_inserted(BlockDriverState *bs)
 {
     return fd_open(bs) >= 0;
@@ -1232,6 +1257,7 @@ static int floppy_eject(BlockDriverState
 static BlockDriver bdrv_host_floppy = {
     .format_name        = "host_floppy",
     .instance_size      = sizeof(BDRVRawState),
+    .bdrv_probe_device	= floppy_probe_device,
     .bdrv_open          = floppy_open,
     .bdrv_close         = raw_close,
     .bdrv_create        = hdev_create,
@@ -1265,6 +1291,13 @@ static int cdrom_open(BlockDriverState *
     return raw_open_common(bs, filename, flags);
 }
 
+static int cdrom_probe_device(const char *filename)
+{
+    if (strstart(filename, "/dev/cd", NULL))
+        return 100;
+    return 0;
+}
+
 static int cdrom_is_inserted(BlockDriverState *bs)
 {
     BDRVRawState *s = bs->opaque;
@@ -1309,6 +1342,7 @@ static int cdrom_set_locked(BlockDriverS
 static BlockDriver bdrv_host_cdrom = {
     .format_name        = "host_cdrom",
     .instance_size      = sizeof(BDRVRawState),
+    .bdrv_probe_device	= cdrom_probe_device,
     .bdrv_open          = cdrom_open,
     .bdrv_close         = raw_close,
     .bdrv_create        = hdev_create,
@@ -1355,6 +1389,14 @@ static int cdrom_open(BlockDriverState *
     return 0;
 }
 
+static int cdrom_probe_device(const char *filename)
+{
+    if (strstart(filename, "/dev/cd", NULL) ||
+            strstart(filename, "/dev/acd", NULL))
+        return 100;
+    return 0;
+}
+
 static int cdrom_reopen(BlockDriverState *bs)
 {
     BDRVRawState *s = bs->opaque;
@@ -1425,6 +1467,7 @@ static int cdrom_set_locked(BlockDriverS
 static BlockDriver bdrv_host_cdrom = {
     .format_name        = "host_cdrom",
     .instance_size      = sizeof(BDRVRawState),
+    .bdrv_probe_device	= cdrom_probe_device,
     .bdrv_open          = cdrom_open,
     .bdrv_close         = raw_close,
     .bdrv_create        = hdev_create,
block_int.h +5 -0
Index: qemu/block_int.h
===================================================================
@@ -48,6 +48,7 @@ struct BlockDriver {
     const char *format_name;
     int instance_size;
     int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
+    int (*bdrv_probe_device)(const char *filename);
     int (*bdrv_open)(BlockDriverState *bs, const char *filename, int flags);
     int (*bdrv_read)(BlockDriverState *bs, int64_t sector_num,
                      uint8_t *buf, int nb_sectors);
@@ -186,4 +187,8 @@ void *qemu_blockalign(BlockDriverState *
 
 extern BlockDriverState *bdrv_first;
 
+#ifdef _WIN32
+int is_windows_drive(const char *filename);
+#endif
+
 #endif /* BLOCK_INT_H */
block.c +12 -32

62 lines not on this page · raw message

block/raw-win32.c +10 -0
Index: qemu/block/raw-win32.c
===================================================================
@@ -302,6 +302,15 @@ static int find_device_type(BlockDriverS
     }
 }
 
+static int hdev_probe_device(const char *filename)
+{
+    if (strstart(filename, "/dev/cdrom", NULL))
+        return 100;
+    if (is_windows_drive(filename))
+        return 100;
+    return 0;
+}
+
 static int hdev_open(BlockDriverState *bs, const char *filename, int flags)
 {
     BDRVRawState *s = bs->opaque;
@@ -387,6 +396,7 @@ static int raw_set_locked(BlockDriverSta
 static BlockDriver bdrv_host_device = {
     .format_name	= "host_device",
     .instance_size	= sizeof(BDRVRawState),
+    .bdrv_probe_device	= hdev_probe_device,
     .bdrv_open		= hdev_open,
     .bdrv_close		= raw_close,
     .bdrv_flush		= raw_flush,
#4 lore raw
In reply to Christoph Hellwig

Updated version that applies to the current tree with the aio pool changes already applied:

--

Subject: raw-posix: split hdev drivers
From: Christoph Hellwig <[email protected]>

Instead of declaring one BlockDriver for all host devices declared one for each type: a generic one for normal disk devices, a Linux floppy driver and a CDROM driver for Linux and FreeBSD. This gets rid of a lot of messy ifdefs and switching based on the type in the various removal device methods.

block.c grows a new method to find the correct host device driver based on OS-sepcific criteria. I would love to move this into some OS-dependant file but I don't think we have a place where it fits nicely yet.

block.c +43 -17
Index: qemu/block.c
===================================================================
@@ -249,32 +249,55 @@ static BlockDriver *find_protocol(const 
     return NULL;
 }
 
-/* XXX: force raw format if block or character device ? It would
-   simplify the BSD case */
-static BlockDriver *find_image_format(const char *filename)
+/*
+ * Detect host devices. By convention, /dev/cdrom[N] is always
+ * recognized as a host CDROM.
+ */
+#ifdef _WIN32
+static BlockDriver *find_hdev_driver(const char *filename)
 {
-    int ret, score, score_max;
-    BlockDriver *drv1, *drv;
-    uint8_t buf[2048];
-    BlockDriverState *bs;
-
-    /* detect host devices. By convention, /dev/cdrom[N] is always
-       recognized as a host CDROM */
     if (strstart(filename, "/dev/cdrom", NULL))
         return bdrv_find_format("host_device");
-#ifdef _WIN32
     if (is_windows_drive(filename))
         return bdrv_find_format("host_device");
+    return NULL;
+}
+#else
+static BlockDriver *find_hdev_driver(const char *filename)
+{
+    struct stat st;
+
+#ifdef __linux__
+    if (strstart(filename, "/dev/fd", NULL))
+        return bdrv_find_format("host_floppy");
+    if (strstart(filename, "/dev/cd", NULL))
+        return bdrv_find_format("host_cdrom");
+#elif defined(__FreeBSD__)
+    if (strstart(filename, "/dev/cd", NULL) ||
+        strstart(filename, "/dev/acd", NULL)) {
+        return bdrv_find_format("host_cdrom");
+    }
 #else
-    {
-        struct stat st;
-        if (stat(filename, &st) >= 0 &&
+    if (strstart(filename, "/dev/cdrom", NULL))
+        return bdrv_find_format("host_device");
+#endif
+
+    if (stat(filename, &st) >= 0 &&
             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
-            return bdrv_find_format("host_device");
-        }
+        return bdrv_find_format("host_device");
     }
+
+    return NULL;
+}
 #endif
 
+static BlockDriver *find_image_format(const char *filename)
+{
+    int ret, score, score_max;
+    BlockDriver *drv1, *drv;
+    uint8_t buf[2048];
+    BlockDriverState *bs;
+
     drv = find_protocol(filename);
     /* no need to test disk image formats for vvfat */
     if (drv && strcmp(drv->format_name, "vvfat") == 0)
@@ -394,7 +417,10 @@ int bdrv_open2(BlockDriverState *bs, con
     if (flags & BDRV_O_FILE) {
         drv = find_protocol(filename);
     } else if (!drv) {
-        drv = find_image_format(filename);
+        drv = find_hdev_driver(filename);
+	if (!drv) {
+            drv = find_image_format(filename);
+        }
     }
     if (!drv) {
         ret = -ENOENT;
block/raw-posix.c +304 -260

634 lines not on this page · raw message