Skip to contentSkip to conversation

qcow2: don't leave partially initialized file on image creation

PATCH Superseded Maxim Levitsky 2 participants 5 messages Oct 11, 2020 – Oct 13, 2020 Block layer

v2 of 6 Superseded Latest is v6 →

lore
Share & tools
lore
#0 lore raw

v2 cover qcow2: don't leave partially initialized file on image creation

Use the bdrv_co_delete_file interface to delete the underlying file if qcow2 initilization fails (e.g due to bad encryption secret)

This gives the qcow2 the same treatment as to luks.

V2: added a patch to fix a memory leak.

Maxim Levitsky (2):
crypto: luks: fix tiny memory leak
block: qcow2: remove the created file on initialization error

 block/crypto.c |  1 +
 block/qcow2.c  | 12 ++++++++++++
 2 files changed, 13 insertions(+)
Signature · 1 line
2.26.2
#1 lore raw

PATCH v2 1/2 crypto: luks: fix tiny memory leak

In the case when underlying block device doesn't support the bdrv_co_delete_file interface, an 'Error' wasn't freed.

---
 block/crypto.c | 1 +
 1 file changed, 1 insertion(+)
block/crypto.c +1 -0
index 0807557763..9b61fd4aa8 100644
@@ -736,6 +736,7 @@ fail:
         if ((r_del < 0) && (r_del != -ENOTSUP)) {
             error_report_err(local_delete_err);
         }
+        error_free(local_delete_err);
     }
 
     bdrv_unref(bs);
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH v2 1/2] crypto: luks: fix tiny memory leak

Quoted 13 lines · Maxim Levitsky

In the case when underlying block device doesn't support the bdrv_co_delete_file interface, an 'Error' wasn't freed.

---
 block/crypto.c | 1 +
 1 file changed, 1 insertion(+)
block/crypto.c +0 -0
index 0807557763..9b61fd4aa8 100644

block/crypto.c fail: · line 740

Quoted 5 lines
block/crypto.c +1 -0
@@ -736,6 +736,7 @@ fail:
         if ((r_del < 0) && (r_del != -ENOTSUP)) {
             error_report_err(local_delete_err);
         }
+        error_free(local_delete_err);
     }

error_report_err() already calls error_free().

Berto

#3 lore raw

Re: [PATCH v2 1/2] crypto: luks: fix tiny memory leak

Quoted 9 lines · Alberto Garcia
Quoted 8 lines · Maxim Levitsky

In the case when underlying block device doesn't support the bdrv_co_delete_file interface, an 'Error' wasn't freed.

---
 block/crypto.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

block/crypto.c fail: · line 740

Quoted 10 lines
block/crypto.c +1 -0
index 0807557763..9b61fd4aa8 100644
@@ -736,6 +736,7 @@ fail:
         if ((r_del < 0) && (r_del != -ENOTSUP)) {
             error_report_err(local_delete_err);
         }
+        error_free(local_delete_err);
     }

error_report_err() already calls error_free().

Thanks. I didn't knew this.
Will send V3 soon.

Best regards,
Maxim Levitsky

Quoted 3 lines

Berto

#4 lore raw

PATCH v2 2/2 block: qcow2: remove the created file on initialization error

In reply to Maxim Levitsky

If the qcow initialization fails after we created the storage file, we should remove it to avoid leaving stale files around.

We already do this for luks raw images.

---
 block/qcow2.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
block/qcow2.c +12 -0
index b05512718c..4dc6102df8 100644
@@ -3834,6 +3834,18 @@ static int coroutine_fn qcow2_co_create_opts(BlockDriver *drv,
     /* Create the qcow2 image (format layer) */
     ret = qcow2_co_create(create_options, errp);
     if (ret < 0) {
+
+        Error *local_delete_err = NULL;
+        int r_del = bdrv_co_delete_file(bs, &local_delete_err);
+        /*
+         * ENOTSUP will happen if the block driver doesn't support
+         * the 'bdrv_co_delete_file' interface. This is a predictable
+         * scenario and shouldn't be reported back to the user.
+         */
+        if ((r_del < 0) && (r_del != -ENOTSUP)) {
+            error_report_err(local_delete_err);
+        }
+        error_free(local_delete_err);
         goto finish;
     }
 
Signature · 1 line
2.26.2