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 2 participants 6 messages Dec 28, 2014 Block layer

v1 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

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

The raw_getlength() function under Mac OS X incorrectly returned a constant value instead of calculating the size of a real CD-ROM disc. This patch fixes this problem and makes booting from a real CD-ROM disc possible again under Mac OS X.

---
 block/raw-posix.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
block/raw-posix.c +10 -1
index e51293a..d723133 100644
@@ -1312,7 +1312,16 @@ again:
         if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+        // Query the number of sectors on the disk
+        uint64_t sectors = 0;
+        ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
+
+        // Query the size of each sector
+        uint32_t sectorSize = 0;
+        ioctl(fd, DKIOCGETBLOCKSIZE, &sectorSize);
+
+        size = sectors * sectorSize;
+        //printf("size of disc = %d MB\n", size/(1024*1024));
 #else
         size = lseek(fd, 0LL, SEEK_END);
         if (size < 0) {
Signature · 1 line
1.7.5.4
#1 lore raw
Quoted 3 lines · Programmingkid

The raw_getlength() function under Mac OS X incorrectly returned a constant value instead of calculating the size of a real CD-ROM disc. This patch fixes this problem and makes booting from a real CD-ROM disc possible again under Mac OS X.

Thanks. (My Mac doesn't have a cdrom...)

Quoted 9 lines
---
 block/raw-posix.c |   11 ++++++++++-
 1 files changed, 10 insertions(+), 1 deletions(-)
block/raw-posix.c +0 -0
index e51293a..d723133 100644

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

Quoted 5 lines
block/raw-posix.c +1 -1
@@ -1312,7 +1312,16 @@ again:
         if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+        // Query the number of sectors on the disk

I note in passing that this whole function is a total mess -- look at that "if (size == 0)" in the previous #ifdef... However since I think it's impossible to both have the previous #ifdef and this one enabled it won't cause an issue.

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

Quoted 2 lines
block/raw-posix.c +2 -0
+        uint64_t sectors = 0;
+        ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);

You need to check the error return from these ioctl calls.

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

Quoted 10 lines
block/raw-posix.c +7 -0
+
+        // Query the size of each sector
+        uint32_t sectorSize = 0;
+        ioctl(fd, DKIOCGETBLOCKSIZE, &sectorSize);
+
+        size = sectors * sectorSize;
+        //printf("size of disc = %d MB\n", size/(1024*1024));
 #else
         size = lseek(fd, 0LL, SEEK_END);
         if (size < 0) {

Minor style issues you might want to fix for v2:
* use /* */ comments, not //
* declare variables only at the top of {} code blocks
* don't leave commented out debug printfs in code

thanks
-- PMM

#2 lore raw
Quoted 11 lines · Peter Maydell
Quoted 3 lines · Programmingkid

The raw_getlength() function under Mac OS X incorrectly returned a constant value instead of calculating the size of a real CD-ROM disc. This patch fixes this problem and makes booting from a real CD-ROM disc possible again under Mac OS X.

Thanks. (My Mac doesn't have a cdrom...)

Quoted 4 lines

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

1 files changed, 10 insertions(+), 1 deletions(-)
Quoted 15 lines

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

Quoted 10 lines
block/raw-posix.c +1 -1
index e51293a..d723133 100644
@@ -1312,7 +1312,16 @@ again:
        if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+        // Query the number of sectors on the disk

I note in passing that this whole function is a total mess -- look at that "if (size == 0)" in the previous #ifdef... However since I think it's impossible to both have the previous #ifdef and this one enabled it won't cause an issue.

I agree. This file needs a lot of work.

Quoted 22 lines

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

Quoted 3 lines
block/raw-posix.c +2 -0
+        uint64_t sectors = 0;
+        ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);

You need to check the error return from these ioctl calls.

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

Quoted 10 lines
block/raw-posix.c +7 -0
+
+        // Query the size of each sector
+        uint32_t sectorSize = 0;
+        ioctl(fd, DKIOCGETBLOCKSIZE, &sectorSize);
+
+        size = sectors * sectorSize;
+        //printf("size of disc = %d MB\n", size/(1024*1024));
 #else
        size = lseek(fd, 0LL, SEEK_END);
        if (size < 0) {

Minor style issues you might want to fix for v2:
* use /* */ comments, not //
* declare variables only at the top of {} code blocks
* don't leave commented out debug printfs in code

Quoted 2 lines

thanks
-- PMM

Here is version 2 of the patch. All the suggestions have been implemented.

---
 block/raw-posix.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
block/raw-posix.c +19 -1
index e51293a..0148161 100644
@@ -1312,7 +1312,25 @@ again:
         if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+        #define IOCTL_ERROR_VALUE -1
+        uint64_t sectors = 0;
+        uint32_t sectorSize = 0;
+        int ret;
+
+        /* Query the number of sectors on the disk */
+        ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
+        if(ret == IOCTL_ERROR_VALUE) {
+           printf("\n\nWarning: problem detected retrieving sector count!\n\n");
+           return -errno;
+        }
+
+        /* Query the size of each sector */
+        ret = ioctl(fd, DKIOCGETBLOCKSIZE, &sectorSize);
+        if(ret == IOCTL_ERROR_VALUE) {
+           printf("\n\nWarning: problem detected retrieving sector size!\n\n");
+           return -errno;
+        }
+        size = sectors * sectorSize;
 #else
         size = lseek(fd, 0LL, SEEK_END);
         if (size < 0) {
Signature · 1 line
1.7.5.4
#3 lore raw
Quoted 1 line · Programmingkid

Here is version 2 of the patch. All the suggestions have been implemented.

Thanks.

Last round of nits, but the rest of the change is fine. If you post v3 as its own email that will make it easier to apply (most of our patch-handling tools assume patches come in their own emails, rather than embedded in other threads).

Quoted 11 lines
---
 block/raw-posix.c |   20 +++++++++++++++++++-
 1 files changed, 19 insertions(+), 1 deletions(-)
block/raw-posix.c +0 -0
index e51293a..0148161 100644

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

Quoted 5 lines
block/raw-posix.c +1 -1
@@ -1312,7 +1312,25 @@ again:
         if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+        #define IOCTL_ERROR_VALUE -1

You don't need this -- just compare against -1.

Open brace here.

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

Quoted 7 lines
block/raw-posix.c +7 -0
+        uint64_t sectors = 0;
+        uint32_t sectorSize = 0;
+        int ret;
+
+        /* Query the number of sectors on the disk */
+        ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
+        if(ret == IOCTL_ERROR_VALUE) {

Spaces needed between "if" and "(".

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

Quoted 1 line
block/raw-posix.c +1 -0
+           printf("\n\nWarning: problem detected retrieving sector count!\n\n");

Delete these printfs.

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

Quoted 10 lines
block/raw-posix.c +10 -0
+           return -errno;
+        }
+
+        /* Query the size of each sector */
+        ret = ioctl(fd, DKIOCGETBLOCKSIZE, &sectorSize);
+        if(ret == IOCTL_ERROR_VALUE) {
+           printf("\n\nWarning: problem detected retrieving sector size!\n\n");
+           return -errno;
+        }
+        size = sectors * sectorSize;

Close brace here.

Quoted 5 lines
block/raw-posix.c +0 -1
 #else
         size = lseek(fd, 0LL, SEEK_END);
         if (size < 0) {
--

1.7.5.4

thanks
-- PMM

#4 lore raw
Quoted 10 lines · Peter Maydell
Quoted 1 line · Programmingkid

Here is version 2 of the patch. All the suggestions have been implemented.

Thanks.

Last round of nits, but the rest of the change is fine. If you post v3 as its own email that will make it easier to apply (most of our patch-handling tools assume patches come in their own emails, rather than embedded in other threads).

Ok. Not a problem.

Quoted 6 lines
Quoted 6 lines

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

1 files changed, 19 insertions(+), 1 deletions(-)
Quoted 12 lines

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

Quoted 10 lines
block/raw-posix.c +1 -1
index e51293a..0148161 100644
@@ -1312,7 +1312,25 @@ again:
        if (size == 0)
 #endif
 #if defined(__APPLE__) && defined(__MACH__)
-        size = LLONG_MAX;
+        #define IOCTL_ERROR_VALUE -1

You don't need this -- just compare against -1.

The value -1 does communicate the fact that I am looking for an error value. The code is more self documenting with the constant.

Quoted 11 lines

Open brace here.

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

Quoted 7 lines
block/raw-posix.c +7 -0
+        uint64_t sectors = 0;
+        uint32_t sectorSize = 0;
+        int ret;
+
+        /* Query the number of sectors on the disk */
+        ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
+        if(ret == IOCTL_ERROR_VALUE) {
Quoted 1 line

Spaces needed between "if" and "(".

Ok, it would look nicer with the space.

Quoted 4 lines

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

Quoted 2 lines
block/raw-posix.c +1 -0
+           printf("\n\nWarning: problem detected retrieving sector count!\n\n");

Delete these printfs.

If we did that, how would the user know something went wrong? If something did go wrong, the user would see these messages and be able to trace the problem directly to the source.

Quoted 22 lines

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

Quoted 11 lines
block/raw-posix.c +10 -0
+           return -errno;
+        }
+
+        /* Query the size of each sector */
+        ret = ioctl(fd, DKIOCGETBLOCKSIZE, &sectorSize);
+        if(ret == IOCTL_ERROR_VALUE) {
+           printf("\n\nWarning: problem detected retrieving sector size!\n\n");
+           return -errno;
+        }
+        size = sectors * sectorSize;

Close brace here.

Quoted 5 lines
block/raw-posix.c +0 -1
 #else
        size = lseek(fd, 0LL, SEEK_END);
        if (size < 0) {
--

1.7.5.4

thanks
-- PMM

#5 lore raw
Quoted 11 lines · Programmingkid
Quoted 6 lines · Peter Maydell

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

Quoted 3 lines · Programmingkid

#if defined(__APPLE__) && defined(__MACH__)

block/raw-posix.c +1 -1
-        size = LLONG_MAX;
+        #define IOCTL_ERROR_VALUE -1

You don't need this -- just compare against -1.

The value -1 does communicate the fact that I am looking for an error value. The code is more self documenting with the constant.

QEMU general style is to assume that -1 is well known as an error return from POSIX functions -- we check for and return raw "-1" everywhere. (This is also pretty widespread practice for most unix C programs I think.)

Quoted 10 lines
Quoted 10 lines

Open brace here.

Quoted 7 lines
block/raw-posix.c +7 -0
+        uint64_t sectors = 0;
+        uint32_t sectorSize = 0;
+        int ret;
+
+        /* Query the number of sectors on the disk */
+        ret = ioctl(fd, DKIOCGETBLOCKCOUNT, &sectors);
+        if(ret == IOCTL_ERROR_VALUE) {
Quoted 3 lines
Quoted 1 line

Spaces needed between "if" and "(".

Ok, it would look nicer with the space.

NB that scripts/checkpatch.pl can find most of this kind of style nit (though it is not always right).

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

+ printf("\n\nWarning: problem detected retrieving sector count!\n\n");

Delete these printfs.

If we did that, how would the user know something went wrong? If something did go wrong, the user would see these messages and be able to trace the problem directly to the source.

Reporting the error is the responsibility of the caller. (For instance, if the user triggered this operation via the QEMU monitor then we need to send the error message there rather than standard output.) It is true that only returning the errno is potentially throwing away a little information about the cause of the error, but is it really much more helpful for the user to know whether it's the sector count or the sector size that couldn't be retrieved, compared to a generic message about not being able to determine the size of the block device? Upper levels of the program are usually better placed to provide an error message that relates to what the user was trying to do, which is why low level functions defer to them.

(We do have a mechanism for passing more error information than just an errno, which is the Error **errp idiom. In theory we could change the API of this function to take an errp, but it's not clear it's worth the effort, and in any case that would be a separate patch.)

thanks
-- PMM