Skip to contentSkip to conversation

raw-posix.c: cd_is_inserted() implementation for Mac OS X

PATCH In review Programmingkid 3 participants 7 messages Jun 29, 2015 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 raw-posix.c: cd_is_inserted() implementation for Mac OS X

Fix a problem with QEMU not being able to use real cd's on Mac OS X hosts. Implements a function called cd_is_inserted().

---
 block/raw-posix.c |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)
block/raw-posix.c +24 -1
index a967464..9420602 100644
@@ -2018,7 +2018,26 @@ kern_return_t GetBSDPath( io_iterator_t mediaIterator, char *bsdPath, CFIndex ma
     return kernResult;
 }
 
-#endif
+/*
+ * Determines if a real cdrom is inserted into the host computer's optical
+ * drive. Uses the fact that find_image_format() calls this function first
+ * in order to go around a bug involving trying to determine a real cd's
+ * format.
+ */
+static int cdrom_is_inserted(BlockDriverState *bs)
+{
+    static int count;
+    int returnValue = (raw_getlength(bs) > 0) ? 1 : 0;
+
+    if (count == 0) {
+        count++;
+        returnValue = 0; /* get around find_image_format() issue */
+    }
+
+    return returnValue;
+}
+
+#endif  /* __APPLE__ */
 
 static int hdev_probe_device(const char *filename)
 {
@@ -2365,6 +2384,10 @@ static BlockDriver bdrv_host_device = {
     .bdrv_ioctl         = hdev_ioctl,
     .bdrv_aio_ioctl     = hdev_aio_ioctl,
 #endif
+
+#ifdef __APPLE__
+    .bdrv_is_inserted   = cdrom_is_inserted,
+#endif
 };
 
 #ifdef __linux__
Signature · 1 line
1.7.5.4
#1 lore raw

block/raw-posix.c static BlockDriver bdrv_host_device = { · line 2390

Quoted 8 lines · Programmingkid
block/raw-posix.c +4 -0
@@ -2365,6 +2384,10 @@ static BlockDriver bdrv_host_device = {
     .bdrv_ioctl         = hdev_ioctl,
     .bdrv_aio_ioctl     = hdev_aio_ioctl,
 #endif
+
+#ifdef __APPLE__
+    .bdrv_is_inserted   = cdrom_is_inserted,
+#endif

Why isn't this handled by having a bdrv_host_cdrom, like Linux and FreeBSD do for their CDROM support?

-- PMM

#2 lore raw
Quoted 12 lines · Peter Maydell

block/raw-posix.c static BlockDriver bdrv_host_device = { · line 2390

Quoted 8 lines · Programmingkid
block/raw-posix.c +4 -0
@@ -2365,6 +2384,10 @@ static BlockDriver bdrv_host_device = {
    .bdrv_ioctl         = hdev_ioctl,
    .bdrv_aio_ioctl     = hdev_aio_ioctl,
 #endif
+
+#ifdef __APPLE__
+    .bdrv_is_inserted   = cdrom_is_inserted,
+#endif

Why isn't this handled by having a bdrv_host_cdrom, like Linux and FreeBSD do for their CDROM support?

That would involve a lot of unnecessary work and modifications. This small change is all that is needed.

#3 lore raw
Quoted 16 lines · Programmingkid
Quoted 12 lines · Peter Maydell

block/raw-posix.c static BlockDriver bdrv_host_device = { · line 2390

Quoted 8 lines · Programmingkid
block/raw-posix.c +4 -0
@@ -2365,6 +2384,10 @@ static BlockDriver bdrv_host_device = {
    .bdrv_ioctl         = hdev_ioctl,
    .bdrv_aio_ioctl     = hdev_aio_ioctl,
 #endif
+
+#ifdef __APPLE__
+    .bdrv_is_inserted   = cdrom_is_inserted,
+#endif

Why isn't this handled by having a bdrv_host_cdrom, like Linux and FreeBSD do for their CDROM support?

Quoted 2 lines

That would involve a lot of unnecessary work and modifications. This small change is all that is needed.

Yes, but it's obviously wrong, because this:

+ if (count == 0) {
+ count++;
+ returnValue = 0; /* get around find_image_format() issue */
+ }

makes no sense at all -- this means that we'll always report "drive empty" the first time this function is called. We should always report the correct answer, regardless of who's calling us.

If you find yourself writing this kind of weird workaround, it generally suggests that the change is a "this happens to make it work" patch, not the correct fix for the problem. We need clean fixes in QEMU, because if we allow "happens to make it work" patches to pile up then the whole system becomes unmaintainable. Yes, this often means that the amount of work required to fix a bug is more than a handful of lines. That doesn't mean that the work is unnecessary.

(For instance, what happens if somebody changes some other part of QEMU so that it happens that find_image_format() is not the first thing to call this function?)

We know the correct way to support host cdrom drives, because we're already doing that on Linux. We should consistently support host cdrom drives the same way for all hosts.

thanks
-- PMM

#4 lore raw
Quoted 40 lines · Peter Maydell
Quoted 16 lines · Programmingkid
Quoted 12 lines · Peter Maydell

block/raw-posix.c static BlockDriver bdrv_host_device = { · line 2390

Quoted 8 lines · Programmingkid
block/raw-posix.c +4 -0
@@ -2365,6 +2384,10 @@ static BlockDriver bdrv_host_device = {
   .bdrv_ioctl         = hdev_ioctl,
   .bdrv_aio_ioctl     = hdev_aio_ioctl,
 #endif
+
+#ifdef __APPLE__
+    .bdrv_is_inserted   = cdrom_is_inserted,
+#endif

Why isn't this handled by having a bdrv_host_cdrom, like Linux and FreeBSD do for their CDROM support?

Quoted 2 lines

That would involve a lot of unnecessary work and modifications. This small change is all that is needed.

Yes, but it's obviously wrong, because this:

block/raw-posix.c +4 -0
+    if (count == 0) {
+        count++;
+        returnValue = 0; /* get around find_image_format() issue */
+    }

makes no sense at all -- this means that we'll always report "drive empty" the first time this function is called. We should always report the correct answer, regardless of who's calling us.

If you find yourself writing this kind of weird workaround, it generally suggests that the change is a "this happens to make it work" patch, not the correct fix for the problem. We need clean fixes in QEMU, because if we allow "happens to make it work" patches to pile up then the whole system becomes unmaintainable. Yes, this often means that the amount of work required to fix a bug is more than a handful of lines. That doesn't mean that the work is unnecessary.

Quoted 7 lines

(For instance, what happens if somebody changes some other part of QEMU so that it happens that find_image_format() is not the first thing to call this function?)

We know the correct way to support host cdrom drives, because we're already doing that on Linux. We should consistently support host cdrom drives the same way for all hosts.

I have really tried to find out what was wrong. It is a asynchronous, multi-threaded mess. Trying to follow where QEMU messes up was hard. The closest I came to was to a function called bdrv_co_io_em(). It was returning a value of -22.

If some change does happen to make this patch to
not work anymore, I can easily fix it.

#5 lore raw
Quoted 53 lines · Programmingkid
Quoted 42 lines · Peter Maydell
Quoted 17 lines · On 29 June 2015 at 19:04, Programmingkid <[email protected]…
Quoted 13 lines · Peter Maydell

block/raw-posix.c static BlockDriver bdrv_host_device = { · line 2390

Quoted 8 lines · On 29 June 2015 at 17:54, Programmingkid <[email protected]…
block/raw-posix.c +4 -0
@@ -2365,6 +2384,10 @@ static BlockDriver bdrv_host_device = {
   .bdrv_ioctl         = hdev_ioctl,
   .bdrv_aio_ioctl     = hdev_aio_ioctl,
 #endif
+
+#ifdef __APPLE__
+    .bdrv_is_inserted   = cdrom_is_inserted,
+#endif

Why isn't this handled by having a bdrv_host_cdrom, like Linux and FreeBSD do for their CDROM support?

Quoted 2 lines

That would involve a lot of unnecessary work and modifications. This small change is all that is needed.

Yes, but it's obviously wrong, because this:

block/raw-posix.c +4 -0
+    if (count == 0) {
+        count++;
+        returnValue = 0; /* get around find_image_format() issue */
+    }

makes no sense at all -- this means that we'll always report "drive empty" the first time this function is called. We should always report the correct answer, regardless of who's calling us.

If you find yourself writing this kind of weird workaround, it generally suggests that the change is a "this happens to make it work" patch, not the correct fix for the problem. We need clean fixes in QEMU, because if we allow "happens to make it work" patches to pile up then the whole system becomes unmaintainable. Yes, this often means that the amount of work required to fix a bug is more than a handful of lines. That doesn't mean that the work is unnecessary.

Quoted 7 lines

(For instance, what happens if somebody changes some other part of QEMU so that it happens that find_image_format() is not the first thing to call this function?)

We know the correct way to support host cdrom drives, because we're already doing that on Linux. We should consistently support host cdrom drives the same way for all hosts.

Quoted 7 lines

I have really tried to find out what was wrong. It is a asynchronous, multi-threaded mess. Trying to follow where QEMU messes up was hard. The closest I came to was to a function called bdrv_co_io_em(). It was returning a value of -22.

If some change does happen to make this patch to
not work anymore, I can easily fix it.

Frankly, I don't understand you.

The only thing you have to do is to write:

static int cdrom_is_inserted(BlockDriverState *bs)

{
    return raw_getlength(bs) > 0;
}

You have introduced yourself the support for raw_getlength() for MacOS X:

commit 728dacbda817b2ca259e9d337fab06bcf14e94a6

Author: Programmingkid <[email protected]>
Date:   Mon Jan 19 17:12:55 2015 -0500

block/raw-posix.c: Fix raw_getlength() on Mac OS X block devices

This patch replaces the dummy code in raw_getlength() for block devices on OS X, which always returned LLONG_MAX, with a real implementation that returns the actual block device size.

Then, just "#ifdef CONFIG_BSD" around the existing bdrv_host_cdrom of FreeBSD (minus cdrom_eject and cdrom_lock_medium) and bdrv_register().

Laurent

#6 lore raw
Quoted 64 lines · Laurent Vivier
Quoted 53 lines · Programmingkid
Quoted 42 lines · Peter Maydell
Quoted 17 lines · On 29 June 2015 at 19:04, Programmingkid <[email protected]…
Quoted 13 lines · Peter Maydell

block/raw-posix.c static BlockDriver bdrv_host_device = { · line 2390

Quoted 8 lines · On 29 June 2015 at 17:54, Programmingkid <[email protected]…
block/raw-posix.c +4 -0
@@ -2365,6 +2384,10 @@ static BlockDriver bdrv_host_device = {
  .bdrv_ioctl         = hdev_ioctl,
  .bdrv_aio_ioctl     = hdev_aio_ioctl,
 #endif
+
+#ifdef __APPLE__
+    .bdrv_is_inserted   = cdrom_is_inserted,
+#endif

Why isn't this handled by having a bdrv_host_cdrom, like Linux and FreeBSD do for their CDROM support?

Quoted 2 lines

That would involve a lot of unnecessary work and modifications. This small change is all that is needed.

Yes, but it's obviously wrong, because this:

block/raw-posix.c +4 -0
+    if (count == 0) {
+        count++;
+        returnValue = 0; /* get around find_image_format() issue */
+    }

makes no sense at all -- this means that we'll always report "drive empty" the first time this function is called. We should always report the correct answer, regardless of who's calling us.

If you find yourself writing this kind of weird workaround, it generally suggests that the change is a "this happens to make it work" patch, not the correct fix for the problem. We need clean fixes in QEMU, because if we allow "happens to make it work" patches to pile up then the whole system becomes unmaintainable. Yes, this often means that the amount of work required to fix a bug is more than a handful of lines. That doesn't mean that the work is unnecessary.

Quoted 7 lines

(For instance, what happens if somebody changes some other part of QEMU so that it happens that find_image_format() is not the first thing to call this function?)

We know the correct way to support host cdrom drives, because we're already doing that on Linux. We should consistently support host cdrom drives the same way for all hosts.

Quoted 7 lines

I have really tried to find out what was wrong. It is a asynchronous, multi-threaded mess. Trying to follow where QEMU messes up was hard. The closest I came to was to a function called bdrv_co_io_em(). It was returning a value of -22.

If some change does happen to make this patch to
not work anymore, I can easily fix it.

Quoted 8 lines

Frankly, I don't understand you.

The only thing you have to do is to write:

static int cdrom_is_inserted(BlockDriverState *bs)

{
   return raw_getlength(bs) > 0;
}

Yes, this is probably the correct implementation for cdrom_is_inserted(), but what I'm trying to do is to make a real cdrom work in QEMU. This implementation of cdrom_is_inserted() doesn't solve the problem with find_image_format(). The problem still causes QEMU to quit when using the option "-cdrom /dev/cdrom".

My patch I sent you before does fix things, but it is viewed as a hack. I was hoping the patch might be temporarily accepted until better solution was made. That is not going to happen :(