Skip to contentSkip to conversation

qcow2_create() error handling fixes

PATCH In review Eduardo Habkost 3 participants 9 messages Oct 7, 2010 – Oct 8, 2010 Block layer

lore
Share & tools
lore
#0 lore raw

cover qcow2_create() error handling fixes

From: Eduardo Habkost <[email protected]>

Hi,

Here are two small fixes on qcow2_create() error handling.

Eduardo Habkost (2):
fix fd leak on a qcow2_create2() error path
check for close() errors on qcow2_create()

 block/qcow2.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)
#1 lore raw

PATCH 1/2 fix fd leak on one qcow2_create2() error path

When getting an invalid cluster size, the open fd must be closed before qcow2_create() returns an error.

---
 block/qcow2.c |    5 ++++-
 1 files changed, 4 insertions(+), 1 deletions(-)
block/qcow2.c +4 -1
index ee3481b..c5fb28e 100644
@@ -918,7 +918,8 @@ static int qcow_create2(const char *filename, int64_t total_size,
             "%d and %dk\n",
             1 << MIN_CLUSTER_BITS,
             1 << (MAX_CLUSTER_BITS - 10));
-        return -EINVAL;
+        ret = -EINVAL;
+        goto exit_close;
     }
     s->cluster_size = 1 << s->cluster_bits;
 
@@ -1052,6 +1053,8 @@ static int qcow_create2(const char *filename, int64_t total_size,
 exit:
     qemu_free(s->refcount_table);
     qemu_free(s->refcount_block);
+
+exit_close:
     close(fd);
 
     /* Preallocate metadata */
Signature · 1 line
1.6.5.5
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] fix fd leak on one qcow2_create2() error path

Quoted 7 lines · Eduardo Habkost

When getting an invalid cluster size, the open fd must be closed before qcow2_create() returns an error.

---
 block/qcow2.c |    5 ++++-

 1 files changed, 4 insertions(+), 1 deletions(-)

Looks good.

Stefan

#3 lore raw

PATCH 2/2 check for close() errors on qcow2_create()

In reply to Eduardo Habkost

Errors when closing the file we just created should not be ignored. I/O errors may happen and "qemu-img create" should fail in those cases.

If we are already exiting due to an error, we will still return the original error number, though.

---
 block/qcow2.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
block/qcow2.c +4 -2
index c5fb28e..d3a056b 100644
@@ -882,7 +882,7 @@ static int qcow_create2(const char *filename, int64_t total_size,
     uint64_t old_ref_clusters;
     QCowCreateState s1, *s = &s1;
     QCowExtension ext_bf = {0, 0};
-    int ret;
+    int ret, cret;
 
     memset(s, 0, sizeof(*s));
 
@@ -1055,7 +1055,9 @@ exit:
     qemu_free(s->refcount_block);
 
 exit_close:
-    close(fd);
+    cret = close(fd);
+    if (ret == 0 && cret < 0)
+        ret = -errno;
 
     /* Preallocate metadata */
     if (ret == 0 && prealloc) {
Signature · 1 line
1.6.5.5
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] check for close() errors on qcow2_create()

Quoted 10 lines · Eduardo Habkost

Errors when closing the file we just created should not be ignored. I/O errors may happen and "qemu-img create" should fail in those cases.

If we are already exiting due to an error, we will still return the original error number, though.

---
 block/qcow2.c |    6 ++++--

 1 files changed, 4 insertions(+), 2 deletions(-)

Sounds good.

Quoted 16 lines
block/qcow2.c +1 -1
index c5fb28e..d3a056b 100644
@@ -882,7 +882,7 @@ static int qcow_create2(const char *filename, int64_t total_size,
     uint64_t old_ref_clusters;
     QCowCreateState s1, *s = &s1;
     QCowExtension ext_bf = {0, 0};
-    int ret;
+    int ret, cret;
 
     memset(s, 0, sizeof(*s));
 
@@ -1055,7 +1055,9 @@ exit:
     qemu_free(s->refcount_block);
 
Quoted 4 lines

 exit_close:
-    close(fd);
+    cret = close(fd);
+    if (ret == 0 && cret < 0)

if (close(fd) < 0 && ret == 0) {

Does the same without variable cret.

Quoted 9 lines

+        ret = -errno;

    /* Preallocate metadata */
    if (ret == 0 && prealloc) {
--
1.6.5.5

#5 lore raw

Re: [Qemu-devel] [PATCH 2/2] check for close() errors on qcow2_create()

Quoted 8 lines · Stefan Hajnoczi
Quoted 4 lines

 exit_close:
-    close(fd);
+    cret = close(fd);
+    if (ret == 0 && cret < 0)

if (close(fd) < 0 && ret == 0) {

Does the same without variable cret.

Yes. I used the variable just for readability. I personally don't like having side-effects inside a if condition.

Signature · 1 line
Eduardo
#6 lore raw

[Qemu-devel] Re: [PATCH 2/2] check for close() errors on qcow2_create()

In reply to Eduardo Habkost

Am 07.10.2010 22:25, schrieb Eduardo Habkost:

Quoted 25 lines

Errors when closing the file we just created should not be ignored. I/O errors may happen and "qemu-img create" should fail in those cases.

If we are already exiting due to an error, we will still return the original error number, though.

---
 block/qcow2.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
block/qcow2.c +1 -1
index c5fb28e..d3a056b 100644
@@ -882,7 +882,7 @@ static int qcow_create2(const char *filename, int64_t total_size,
     uint64_t old_ref_clusters;
     QCowCreateState s1, *s = &s1;
     QCowExtension ext_bf = {0, 0};
-    int ret;
+    int ret, cret;
 
     memset(s, 0, sizeof(*s));
 

block/qcow2.c exit: · line 1060

Quoted 7 lines
block/qcow2.c +3 -1
@@ -1055,7 +1055,9 @@ exit:
     qemu_free(s->refcount_block);
 
 exit_close:
-    close(fd);
+    cret = close(fd);
+    if (ret == 0 && cret < 0)
+        ret = -errno;

Braces are missing here.

Kevin

#7 lore raw

[Qemu-devel] Re: [PATCH 2/2] check for close() errors on qcow2_create()

Quoted 6 lines · Kevin Wolf

Am 07.10.2010 22:25, schrieb Eduardo Habkost:

Quoted 5 lines

Errors when closing the file we just created should not be ignored. I/O errors may happen and "qemu-img create" should fail in those cases.

If we are already exiting due to an error, we will still return the original error number, though.

[...]

Quoted 7 lines

Review

Quoted 5 lines

exit_close:

(quoted diff) +3 -1
-    close(fd);
+    cret = close(fd);
+    if (ret == 0 && cret < 0)
+        ret = -errno;

Braces are missing here.

Updated patch below.

I won't resubmit the series as a new thread, as it depends on deciding what to do about the qcow2_create() rewrite.

---
 block/qcow2.c |    7 +++++--
 1 files changed, 5 insertions(+), 2 deletions(-)
block/qcow2.c +5 -2
index c5fb28e..e2e9a95 100644
@@ -882,7 +882,7 @@ static int qcow_create2(const char *filename, int64_t total_size,
     uint64_t old_ref_clusters;
     QCowCreateState s1, *s = &s1;
     QCowExtension ext_bf = {0, 0};
-    int ret;
+    int ret, cret;
 
     memset(s, 0, sizeof(*s));
 
@@ -1055,7 +1055,10 @@ exit:
     qemu_free(s->refcount_block);
 
 exit_close:
-    close(fd);
+    cret = close(fd);
+    if (ret == 0 && cret < 0) {
+        ret = -errno;
+    }
 
     /* Preallocate metadata */
     if (ret == 0 && prealloc) {
Signature · 1 line
1.6.5.5
#8 lore raw
In reply to Eduardo Habkost

Am 07.10.2010 22:25, schrieb Eduardo Habkost:

Quoted 9 lines

From: Eduardo Habkost <[email protected]>

Hi,

Here are two small fixes on qcow2_create() error handling.

Eduardo Habkost (2):
fix fd leak on a qcow2_create2() error path
check for close() errors on qcow2_create()

A while ago I sent a patch to completely rewrite qcow_create using qemu block layer functions. I didn't submit it for inclusion yet because it makes some assumptions in qemu-iotests invalid and the test cases need to be fixed first.

In the new version, your first patch wouldn't be needed. However, for the second one, I think we have a problem today:

    void bdrv_close(BlockDriverState *bs);

We need to convert bdrv_close to be able to return error values and to pass them on up to the first caller (which is qemu-img in this case).

I'll have a look at fixing the test cases and bdrv_close. If I can't get it fixed easily, I'll consider your patches.

Kevin