Skip to contentSkip to conversation

block/raw-posix.c: Fixes raw_getlength() on Mac OS X so that it reports the correct length of a real CD

PATCH Superseded Programmingkid 4 participants 9 messages Jan 13, 2015 – Jan 19, 2015 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 block/raw-posix.c: Fixes raw_getlength() on Mac OS X so that it reports the correct length of a real CD

Allows QEMU on Mac OS X to use a real cdrom again.

---
Added fallback code - uses lseek() if ioctl() fails.

 block/raw-posix.c |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)
block/raw-posix.c +24 -1
index e51293a..5815707 100644
@@ -1312,7 +1312,30 @@ again:
         if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+    {
+        uint64_t sectors = 0;
+        uint32_t sector_size = 0;
+        bool ioctl_problem = false;
+        ret = 0;
+
+        /* Query the number of sectors on the disk */
+        ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
+        if (ret != 0)
+            ioctl_problem = true;
+
+        /* Query the size of each sector */
+        ret = ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size);
+        if (ret != 0)
+            ioctl_problem = true;
+
+        /* If everything is ok */
+        if (ioctl_problem == false)
+            size = sectors * sector_size;
+
+        /* If a problem occurred with ioctl(), fallback to lseek() */
+        else
+            size = lseek(fd, 0LL, SEEK_END);
+    }
 #else
         size = lseek(fd, 0LL, SEEK_END);
         if (size < 0) {
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 34 lines · Programmingkid

Allows QEMU on Mac OS X to use a real cdrom again.

---
Added fallback code - uses lseek() if ioctl() fails.

 block/raw-posix.c |   25 ++++++++++++++++++++++++-
 1 files changed, 24 insertions(+), 1 deletions(-)
block/raw-posix.c +15 -1
index e51293a..5815707 100644
@@ -1312,7 +1312,30 @@ again:
         if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+    {
+        uint64_t sectors = 0;
+        uint32_t sector_size = 0;
+        bool ioctl_problem = false;
+        ret = 0;
+
+        /* Query the number of sectors on the disk */
+        ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
+        if (ret != 0)
+            ioctl_problem = true;
+
+        /* Query the size of each sector */
+        ret = ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size);
+        if (ret != 0)
+            ioctl_problem = true;

block/raw-posix.c again: · line 1339

Quoted 10 lines
block/raw-posix.c +9 -0
+
+        /* If everything is ok */
+        if (ioctl_problem == false)
+            size = sectors * sector_size;
+
+        /* If a problem occurred with ioctl(), fallback to lseek() */
+        else
+            size = lseek(fd, 0LL, SEEK_END);
+    }
 #else

This fixes the "make check" problem, but you're not catching the possibility of lseek failing. (Also the if() statements are all missing braces our coding style requires.) You can avoid that bool flag like this:

    {
        uint64_t sectors = 0;
        uint32_t sector_size = 0;

        if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
            && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
            size = sectors * sector_size;
        } else {
            size = lseek(fd, 0LL, SEEK_END);
            if (size < 0) {
                return -errno;
            }
        }
    }

thanks
-- PMM

#2 lore raw
Quoted 64 lines · Peter Maydell
Quoted 34 lines · Programmingkid

Allows QEMU on Mac OS X to use a real cdrom again.

---
Added fallback code - uses lseek() if ioctl() fails.

block/raw-posix.c | 25 ++++++++++++++++++++++++-

1 files changed, 24 insertions(+), 1 deletions(-)
block/raw-posix.c +15 -1
index e51293a..5815707 100644
@@ -1312,7 +1312,30 @@ again:
        if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+    {
+        uint64_t sectors = 0;
+        uint32_t sector_size = 0;
+        bool ioctl_problem = false;
+        ret = 0;
+
+        /* Query the number of sectors on the disk */
+        ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
+        if (ret != 0)
+            ioctl_problem = true;
+
+        /* Query the size of each sector */
+        ret = ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size);
+        if (ret != 0)
+            ioctl_problem = true;

block/raw-posix.c again: · line 1339

Quoted 10 lines
block/raw-posix.c +9 -0
+
+        /* If everything is ok */
+        if (ioctl_problem == false)
+            size = sectors * sector_size;
+
+        /* If a problem occurred with ioctl(), fallback to lseek() */
+        else
+            size = lseek(fd, 0LL, SEEK_END);
+    }
 #else

This fixes the "make check" problem, but you're not catching the possibility of lseek failing. (Also the if() statements are all missing braces our coding style requires.) You can avoid that bool flag like this:

   {
       uint64_t sectors = 0;
       uint32_t sector_size = 0;
       if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
           && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
           size = sectors * sector_size;
       } else {
           size = lseek(fd, 0LL, SEEK_END);
           if (size < 0) {
               return -errno;
           }
       }
Quoted 1 line

}

Yeah, your code looks great. I will make a patch for it if you need me to.

#3 lore raw

PATCH v7 1/1 block/raw-posix.c: Fixes raw_getlength() on Mac OS X so that it reports the correct length of a real CD

In reply to Peter Maydell

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

---
Totally rewritten.

 block/raw-posix.c |   15 ++++++++++++++-
 1 files changed, 14 insertions(+), 1 deletions(-)
block/raw-posix.c +14 -1
index e51293a..a95cfcf 100644
@@ -1312,7 +1312,20 @@ again:
         if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+   {
+       uint64_t sectors = 0;
+       uint32_t sector_size = 0;
+
+       if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0
+           && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
+           size = sectors * sector_size;
+       } else {
+           size = lseek(fd, 0LL, SEEK_END);
+           if (size < 0) {
+               return -errno;
+           }
+       }
+   }
 #else
         size = lseek(fd, 0LL, SEEK_END);
         if (size < 0) {
Signature · 1 line
1.7.5.4
#4 lore raw
Quoted 3 lines · Programmingkid

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

Subject line is too long, and the body lacks line breaks. Suggest something like:

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

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

#5 lore raw
Quoted 10 lines · Markus Armbruster
Quoted 3 lines · Programmingkid

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

Subject line is too long, and the body lacks line breaks. Suggest something like:

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

Unfortunately I can't change the subject line after posting the first version of the patch. Others have to know the history of this patch. I will try to include the file name in the future.

Quoted 5 lines

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

Ok. I just don't know when to break it. I guess I will stick to pinky length.

#6 lore raw
Quoted 7 lines · Programmingkid
Quoted 4 lines · Markus Armbruster
Quoted 2 lines · Programmingkid

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

Quoted 8 lines
Quoted 6 lines
Quoted 1 line

Subject line is too long, and the body lacks line breaks. Suggest something like:

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

Unfortunately I can't change the subject line after posting the first version of the patch. Others have to know the history of this patch. I will try to include the file name in the future.

Yes, you can. It's a bit trickier, in that you'll probably want to include a mailing list archive link in v8 and explicitly point out that the subject has changed, as well as a followup to v7 that points to the list archive of v8 to help people find the changed name. But there is nothing technically stopping you from renaming a patch to something more appropriate.

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
#7 lore raw
In reply to Programmingkid
Quoted 11 lines · Programmingkid
Quoted 9 lines · Markus Armbruster
Quoted 6 lines · Programmingkid

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

Quoted 8 lines
Quoted 4 lines

Subject line is too long, and the body lacks line breaks. Suggest something like:

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

Unfortunately I can't change the subject line after posting the first version of the patch. Others have to know the history of this patch. I will try to include the file name in the future.

Oh yes, you can!

If you care about patch history, do this:

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

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

---
v7:
* Totally rewritten.
* Subject changed from "block/raw-posix.c: Fixes raw_getlength() on Mac OS X so that it reports the correct length of a real CD"
v6:
[more history, if you care...]

A good commit message is worth a little inconvenience.

Quoted 6 lines
Quoted 4 lines

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

Ok. I just don't know when to break it. I guess I will stick to pinky length.

I recommend to limit line length to 70 characters.

Reviewed-by on v7 1/1
#8 lore raw
In reply to Programmingkid
Quoted 24 lines · Programmingkid

This patch allows Mac OS X to use a real CDROM disc in QEMU. Testing this patch will require using QEMU v2.2.0 because the current git version has a bug in it that prevents /dev/cdrom from being used. "make check" did pass and my Debian boot disc did work.

---
Totally rewritten.

 block/raw-posix.c |   15 ++++++++++++++-
 1 files changed, 14 insertions(+), 1 deletions(-)
block/raw-posix.c +5 -1
index e51293a..a95cfcf 100644
@@ -1312,7 +1312,20 @@ again:
         if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+   {
+       uint64_t sectors = 0;
+       uint32_t sector_size = 0;
+
+       if (ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors) == 0

block/raw-posix.c again: · line 1329

Quoted 10 lines
block/raw-posix.c +9 -0
+           && ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) == 0) {
+           size = sectors * sector_size;
+       } else {
+           size = lseek(fd, 0LL, SEEK_END);
+           if (size < 0) {
+               return -errno;
+           }
+       }
+   }
 #else

You have the indentation here wrong -- the first '{' should be at the same indent as the 'size = ' line you've deleted; and as Markus says the commit message could be neatened up. Otherwise, code looks good and it passes make check, so

-- PMM