Skip to contentSkip to conversation

qcow2: free allocated cluster on fail in qcow2_write_snapshots()

PATCH Superseded Wenchao Xia 2 participants 5 messages May 15, 2013 – May 17, 2013 Block layer

v1 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#1 lore raw

PATCH 2/2 qcow2: cancel the modification on fail in qcow2_snapshot_create()

In reply to Wenchao Xia
---
 block/qcow2-snapshot.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
block/qcow2-snapshot.c +11 -0
index a6065a9..ad030f9 100644
@@ -410,6 +410,17 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 #endif
     return 0;
 
+restore_refcount:
+    ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
+                                         s->l1_size, -1);
+    if (ret < 0) {
+        /* Nothing can be done none now, need image check later */
+        error_report("qcow2: Error in restoring refcount in snapshot");
+    }
+
+dealloc_cluster:
+    qcow2_free_clusters(bs, sn->l1_table_offset, sn->l1_size);
+
 fail:
     g_free(sn->id_str);
     g_free(sn->name);
Signature · 1 line
1.7.1
#0 lore raw

PATCH 1/2 qcow2: free allocated cluster on fail in qcow2_write_snapshots()

---
 block/qcow2-snapshot.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
block/qcow2-snapshot.c +2 -0
index 992a5c8..a6065a9 100644
@@ -268,6 +268,8 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
     return 0;
 
 fail:
+    /* free the new snapshot table */
+    qcow2_free_clusters(bs, snapshots_offset, snapshots_size);
     return ret;
 }
 
Signature · 1 line
1.7.1
#3 lore raw
Quoted 10 lines · Wenchao Xia
---
 block/qcow2-snapshot.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
block/qcow2-snapshot.c +0 -0
index 992a5c8..a6065a9 100644

block/qcow2-snapshot.c static int qcow2_write_snapshots(BlockDriverState *bs) · line 274

Quoted 7 lines
block/qcow2-snapshot.c +2 -0
@@ -268,6 +268,8 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
     return 0;
 
 fail:
+    /* free the new snapshot table */
+    qcow2_free_clusters(bs, snapshots_offset, snapshots_size);
     return ret;
 }

snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size); offset = snapshots_offset;

if (offset < 0) {
    return offset;
}
ret = bdrv_flush(bs);
if (ret < 0) {
    return ret;
}

For completeness the bdrv_flush() return ret should be change to a goto fail so that we cover all failure cases.

#2 lore raw

Re: [Qemu-devel] [PATCH 2/2] qcow2: cancel the modification on fail in qcow2_snapshot_create()

In reply to Wenchao Xia
Quoted 17 lines · Wenchao Xia
---
 block/qcow2-snapshot.c |   11 +++++++++++
 1 files changed, 11 insertions(+), 0 deletions(-)
block/qcow2-snapshot.c +4 -0
index a6065a9..ad030f9 100644
@@ -410,6 +410,17 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)
 #endif
     return 0;
 
+restore_refcount:
+    ret = qcow2_update_snapshot_refcount(bs, s->l1_table_offset,
+                                         s->l1_size, -1);
+    if (ret < 0) {

block/qcow2-snapshot.c int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) · line 426

Quoted 10 lines
block/qcow2-snapshot.c +7 -0
+        /* Nothing can be done none now, need image check later */
+        error_report("qcow2: Error in restoring refcount in snapshot");
+    }
+
+dealloc_cluster:
+    qcow2_free_clusters(bs, sn->l1_table_offset, sn->l1_size);
+
 fail:
     g_free(sn->id_str);
     g_free(sn->name);

This patch is missing goto restore_refcount and goto dealloc_cluster.

#4 lore raw
In reply to Stefan Hajnoczi
Quoted 29 lines · Stefan Hajnoczi
Quoted 10 lines · Wenchao Xia
---
  block/qcow2-snapshot.c |    2 ++
  1 files changed, 2 insertions(+), 0 deletions(-)
block/qcow2-snapshot.c +0 -0
index 992a5c8..a6065a9 100644

block/qcow2-snapshot.c static int qcow2_write_snapshots(BlockDriverState *bs) · line 274

Quoted 7 lines
block/qcow2-snapshot.c +2 -0
@@ -268,6 +268,8 @@ static int qcow2_write_snapshots(BlockDriverState *bs)
      return 0;
 
  fail:
+    /* free the new snapshot table */
+    qcow2_free_clusters(bs, snapshots_offset, snapshots_size);
      return ret;
  }

snapshots_offset = qcow2_alloc_clusters(bs, snapshots_size); offset = snapshots_offset;

if (offset < 0) {
     return offset;
}
ret = bdrv_flush(bs);
if (ret < 0) {
     return ret;
}
Quoted 3 lines

For completeness the bdrv_flush() return ret should be change to a goto fail so that we cover all failure cases.

Ok, will add those.

Signature · 3 lines
Best Regards

Wenchao Xia