Skip to contentSkip to conversation

block/file-posix.c: Fix unused variable warning on OpenBSD

PATCH Superseded Peter Maydell 4 participants 5 messages Mar 20, 2017 – Mar 22, 2017 Block layer

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 block/file-posix.c: Fix unused variable warning on OpenBSD

On OpenBSD none of the ioctls probe_logical_blocksize() tries exist, so the variable sector_size is unused. Refactor the code to avoid this (and reduce the duplicated code).

---
The alternative would be to move the variable so it was local to a code block inside each #ifdef, but this seemed a bit nicer anyway.

Tentatively tagged 'for-2.9' just because this is the only warning in the OpenBSD build, but I don't insist on it.

I've opted to retain the existing behaviour of "try every ioctl available and use the last one that works" rather than "stop as soon as something worked".

---
 block/file-posix.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
block/file-posix.c +14 -14
index 53febd3..b980d23 100644
@@ -219,28 +219,28 @@ static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
 {
     unsigned int sector_size;
     bool success = false;
+    int i;
 
     errno = ENOTSUP;
-
-    /* Try a few ioctls to get the right size */
+    unsigned long ioctl_list[] = {
 #ifdef BLKSSZGET
-    if (ioctl(fd, BLKSSZGET, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        BLKSSZGET,
 #endif
 #ifdef DKIOCGETBLOCKSIZE
-    if (ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DKIOCGETBLOCKSIZE,
 #endif
 #ifdef DIOCGSECTORSIZE
-    if (ioctl(fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DIOCGSECTORSIZE,
 #endif
+    };
+
+    /* Try a few ioctls to get the right size */
+    for (i = 0; i < ARRAY_SIZE(ioctl_list); i++) {
+        if (ioctl(fd, ioctl_list[i], &sector_size) >= 0) {
+            *sector_size_p = sector_size;
+            success = true;
+        }
+    }
 
     return success ? 0 : -errno;
 }
Signature · 1 line
2.7.4
Reviewed-by on v1 1/1
#1 lore raw
Quoted 72 lines · Peter Maydell

On OpenBSD none of the ioctls probe_logical_blocksize() tries exist, so the variable sector_size is unused. Refactor the code to avoid this (and reduce the duplicated code).

---
The alternative would be to move the variable so it was local to a code block inside each #ifdef, but this seemed a bit nicer anyway.

Tentatively tagged 'for-2.9' just because this is the only warning in the OpenBSD build, but I don't insist on it.

I've opted to retain the existing behaviour of "try every ioctl available and use the last one that works" rather than "stop as soon as something worked".

---
 block/file-posix.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
block/file-posix.c +14 -14
index 53febd3..b980d23 100644
@@ -219,28 +219,28 @@ static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
 {
     unsigned int sector_size;
     bool success = false;
+    int i;
 
     errno = ENOTSUP;
-
-    /* Try a few ioctls to get the right size */
+    unsigned long ioctl_list[] = {
 #ifdef BLKSSZGET
-    if (ioctl(fd, BLKSSZGET, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        BLKSSZGET,
 #endif
 #ifdef DKIOCGETBLOCKSIZE
-    if (ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DKIOCGETBLOCKSIZE,
 #endif
 #ifdef DIOCGSECTORSIZE
-    if (ioctl(fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DIOCGSECTORSIZE,
 #endif
+    };
+
+    /* Try a few ioctls to get the right size */
+    for (i = 0; i < ARRAY_SIZE(ioctl_list); i++) {
+        if (ioctl(fd, ioctl_list[i], &sector_size) >= 0) {
+            *sector_size_p = sector_size;
+            success = true;
+        }
+    }
 
     return success ? 0 : -errno;
 }
Signature · 1 line
2.7.4
#2 lore raw
Quoted 32 lines · Jeff Cody
Quoted 31 lines · Peter Maydell

On OpenBSD none of the ioctls probe_logical_blocksize() tries exist, so the variable sector_size is unused. Refactor the code to avoid this (and reduce the duplicated code).

---
The alternative would be to move the variable so it was local to a code block inside each #ifdef, but this seemed a bit nicer anyway.

Tentatively tagged 'for-2.9' just because this is the only warning in the OpenBSD build, but I don't insist on it.

I've opted to retain the existing behaviour of "try every ioctl available and use the last one that works" rather than "stop as soon as something worked".

---
 block/file-posix.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
block/file-posix.c +1 -0
index 53febd3..b980d23 100644
@@ -219,28 +219,28 @@ static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
 {
     unsigned int sector_size;
     bool success = false;
+    int i;
 
Quoted 4 lines

block/file-posix.c static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) · line 225

Quoted 4 lines
block/file-posix.c +1 -2
     errno = ENOTSUP;
-
-    /* Try a few ioctls to get the right size */
+    unsigned long ioctl_list[] = {

this can be const but not a bit win.

Quoted 31 lines
Quoted 31 lines
block/file-posix.c +12 -12
 #ifdef BLKSSZGET
-    if (ioctl(fd, BLKSSZGET, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        BLKSSZGET,
 #endif
 #ifdef DKIOCGETBLOCKSIZE
-    if (ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DKIOCGETBLOCKSIZE,
 #endif
 #ifdef DIOCGSECTORSIZE
-    if (ioctl(fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DIOCGSECTORSIZE,
 #endif
+    };
+
+    /* Try a few ioctls to get the right size */
+    for (i = 0; i < ARRAY_SIZE(ioctl_list); i++) {
+        if (ioctl(fd, ioctl_list[i], &sector_size) >= 0) {
+            *sector_size_p = sector_size;
+            success = true;
+        }
+    }
 
Quoted 8 lines
Quoted 6 lines
block/file-posix.c +0 -1
     return success ? 0 : -errno;
 }
--

2.7.4

#3 lore raw
Quoted 34 lines · Philippe Mathieu-Daudé
Quoted 33 lines · Jeff Cody
Quoted 32 lines · Peter Maydell

On OpenBSD none of the ioctls probe_logical_blocksize() tries exist, so the variable sector_size is unused. Refactor the code to avoid this (and reduce the duplicated code).

---
The alternative would be to move the variable so it was local to a code block inside each #ifdef, but this seemed a bit nicer anyway.

Tentatively tagged 'for-2.9' just because this is the only warning in the OpenBSD build, but I don't insist on it.

I've opted to retain the existing behaviour of "try every ioctl available and use the last one that works" rather than "stop as soon as something worked".

---
 block/file-posix.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
block/file-posix.c +1 -0
index 53febd3..b980d23 100644
@@ -219,28 +219,28 @@ static int probe_logical_blocksize(int fd,
 unsigned int *sector_size_p)
 {
     unsigned int sector_size;
     bool success = false;
+    int i;
 
Quoted 6 lines
Quoted 4 lines

block/file-posix.c static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) · line 225

Quoted 4 lines
block/file-posix.c +1 -2
     errno = ENOTSUP;
-
-    /* Try a few ioctls to get the right size */
+    unsigned long ioctl_list[] = {

this can be const but not a bit win.

I mean static const, sorry.

Quoted 42 lines
Quoted 32 lines
Quoted 32 lines
block/file-posix.c +12 -12
 #ifdef BLKSSZGET
-    if (ioctl(fd, BLKSSZGET, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        BLKSSZGET,
 #endif
 #ifdef DKIOCGETBLOCKSIZE
-    if (ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DKIOCGETBLOCKSIZE,
 #endif
 #ifdef DIOCGSECTORSIZE
-    if (ioctl(fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DIOCGSECTORSIZE,
 #endif
+    };
+
+    /* Try a few ioctls to get the right size */
+    for (i = 0; i < ARRAY_SIZE(ioctl_list); i++) {
+        if (ioctl(fd, ioctl_list[i], &sector_size) >= 0) {
+            *sector_size_p = sector_size;
+            success = true;
+        }
+    }
 
Quoted 8 lines
Quoted 6 lines
block/file-posix.c +0 -0
     return success ? 0 : -errno;

}
--
2.7.4

#4 lore raw
In reply to Peter Maydell
Quoted 4 lines · Peter Maydell

On OpenBSD none of the ioctls probe_logical_blocksize() tries exist, so the variable sector_size is unused. Refactor the code to avoid this (and reduce the duplicated code).

Quoted 8 lines

---
The alternative would be to move the variable so it was local to a code block inside each #ifdef, but this seemed a bit nicer anyway.

Tentatively tagged 'for-2.9' just because this is the only warning in the OpenBSD build, but I don't insist on it.

I'm fine with targeting 2.9.

Quoted 37 lines

I've opted to retain the existing behaviour of "try every ioctl available and use the last one that works" rather than "stop as soon as something worked".

---
 block/file-posix.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
block/file-posix.c +4 -10
index 53febd3..b980d23 100644
@@ -219,28 +219,28 @@ static int probe_logical_blocksize(int fd, unsigned int *sector_size_p)
 {
     unsigned int sector_size;
     bool success = false;
+    int i;
 
     errno = ENOTSUP;
-
-    /* Try a few ioctls to get the right size */
+    unsigned long ioctl_list[] = {
 #ifdef BLKSSZGET
-    if (ioctl(fd, BLKSSZGET, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        BLKSSZGET,
 #endif
 #ifdef DKIOCGETBLOCKSIZE
-    if (ioctl(fd, DKIOCGETBLOCKSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DKIOCGETBLOCKSIZE,
 #endif
 #ifdef DIOCGSECTORSIZE

block/file-posix.c static int probe_logical_blocksize(int fd, unsigned int *sector_size_p) · line 238

Quoted 10 lines
block/file-posix.c +5 -4
-    if (ioctl(fd, DIOCGSECTORSIZE, &sector_size) >= 0) {
-        *sector_size_p = sector_size;
-        success = true;
-    }
+        DIOCGSECTORSIZE,
 #endif
+    };
+
+    /* Try a few ioctls to get the right size */
+    for (i = 0; i < ARRAY_SIZE(ioctl_list); i++) {

On 32-bit, though:

./block/file-posix.c: In function ‘probe_logical_blocksize’:
./qemu/block/file-posix.c:229:19: error: comparison of unsigned expression < 0 is always false [-Werror=type-limits]

A cast (int)ARRAY_SIZE() would fix that (I had the same problem once...).

Max

Quoted 9 lines
block/file-posix.c +5 -0
+        if (ioctl(fd, ioctl_list[i], &sector_size) >= 0) {
+            *sector_size_p = sector_size;
+            success = true;
+        }
+    }
 
     return success ? 0 : -errno;
 }