qcow2: try cancel the modification on fail in snapshot creation
v2 cover qcow2: try cancel the modification on fail in snapshot creation
V2:
1/2: all fail case will goto fail section.
2/2: add the goto code.
Wenchao Xia (2):
1 qcow2: free allocated cluster on fail in qcow2_write_snapshots() 2 qcow2: cancel the modification on fail in qcow2_snapshot_create()
block/qcow2-snapshot.c | 27 ++++++++++++++++++++------- 1 files changed, 20 insertions(+), 7 deletions(-)
PATCH v2 1/2 qcow2: free allocated cluster on fail in qcow2_write_snapshots()
--- block/qcow2-snapshot.c | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-)
block/qcow2-snapshot.c +6 -4
@@ -180,13 +180,13 @@ static int qcow2_write_snapshots(BlockDriverState *bs)/* Allocate space for the new snapshot list */ snapshots_offset =qcow2_alloc_clusters (bs, snapshots_size); - offset = snapshots_offset; -if (offset <0 ) { -return offset; + ret = offset = snapshots_offset; +if (ret <0 ) { + goto fail; } ret =bdrv_flush (bs);if (ret <0 ) { -return ret; + goto fail; }/* Write all snapshots to the new list */
@@ -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
Re: [Qemu-devel] [PATCH V2 1/2] qcow2: free allocated cluster on fail in qcow2_write_snapshots()
Quoted 10 lines · Wenchao Xia
Signed-off-by Wenchao Xia <[email protected]>--- block/qcow2-snapshot.c | 10 ++++++---- 1 files changed, 6 insertions(+), 4 deletions(-)block/qcow2-snapshot.c +0
-0
block/qcow2-snapshot.c static int qcow2_write_snapshots(BlockDriverState *bs) · line 186
Quoted 10 lines
block/qcow2-snapshot.c +3
-3@@ -180,13 +180,13 @@ static int qcow2_write_snapshots(BlockDriverState *bs)/* Allocate space for the new snapshot list */ snapshots_offset =qcow2_alloc_clusters (bs, snapshots_size); - offset = snapshots_offset; -if (offset <0 ) { -return offset; + ret = offset = snapshots_offset; +if (ret <0 ) { + goto fail; }
If qcow2_alloc_clusters() failed then we must not call qcow2_free_clusters().
PATCH v2 2/2 qcow2: cancel the modification on fail in qcow2_snapshot_create()
In reply to Wenchao Xia--- block/qcow2-snapshot.c | 17 ++++++++++++++--- 1 files changed, 14 insertions(+), 3 deletions(-)
block/qcow2-snapshot.c +14 -3
@@ -367,7 +367,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) ret =bdrv_pwrite (bs->file, sn->l1_table_offset, l1_table, s->l1_size *sizeof (uint64_t ));if (ret <0 ) { - goto fail; + goto dealloc_cluster; }g_free (l1_table);
@@ -380,7 +380,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)*/ ret =qcow2_update_snapshot_refcount (bs, s->l1_table_offset, s->l1_size,1 );if (ret <0 ) { - goto fail; + goto dealloc_cluster; }/* Append the new snapshot to the snapshot list */
@@ -397,7 +397,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)if (ret <0 ) {g_free (s->snapshots); s->snapshots = old_snapshot_list; - goto fail; + goto restore_refcount; }g_free (old_snapshot_list);
@@ -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
Re: [Qemu-devel] [PATCH V2 2/2] qcow2: cancel the modification on fail in qcow2_snapshot_create()
Quoted 40 lines · Wenchao Xia
Signed-off-by Wenchao Xia <[email protected]>--- block/qcow2-snapshot.c | 17 ++++++++++++++--- 1 files changed, 14 insertions(+), 3 deletions(-)block/qcow2-snapshot.c +3
-3@@ -367,7 +367,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) ret =bdrv_pwrite (bs->file, sn->l1_table_offset, l1_table, s->l1_size *sizeof (uint64_t ));if (ret <0 ) { - goto fail; + goto dealloc_cluster; }g_free (l1_table);@@ -380,7 +380,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)*/ ret =qcow2_update_snapshot_refcount (bs, s->l1_table_offset, s->l1_size,1 );if (ret <0 ) { - goto fail; + goto dealloc_cluster; }/* Append the new snapshot to the snapshot list */ @@ -397,7 +397,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)if (ret <0 ) {g_free (s->snapshots); s->snapshots = old_snapshot_list; - goto fail; + goto restore_refcount; }g_free (old_snapshot_list);@@ -410,6 +410,17 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)#endif return 0 ;
block/qcow2-snapshot.c int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) · line 422
Quoted 10 lines
block/qcow2-snapshot.c +10
-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);
sn->l1_size is in uint64_t units, not in bytes.
Do you have test cases to exercise these code paths? Use blkdebug to inject errors at the right point so that the error code path is taken.
Re: [Qemu-devel] [PATCH V2 2/2] qcow2: cancel the modification on fail in qcow2_snapshot_create()
Quoted 54 lines · Stefan Hajnoczi
Quoted 40 lines · Wenchao Xia
Signed-off-by Wenchao Xia <[email protected]>--- block/qcow2-snapshot.c | 17 ++++++++++++++--- 1 files changed, 14 insertions(+), 3 deletions(-)block/qcow2-snapshot.c +3
-3@@ -367,7 +367,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) ret =bdrv_pwrite (bs->file, sn->l1_table_offset, l1_table, s->l1_size *sizeof (uint64_t ));if (ret <0 ) { - goto fail; + goto dealloc_cluster; }g_free (l1_table);@@ -380,7 +380,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)*/ ret =qcow2_update_snapshot_refcount (bs, s->l1_table_offset, s->l1_size,1 );if (ret <0 ) { - goto fail; + goto dealloc_cluster; }/* Append the new snapshot to the snapshot list */ @@ -397,7 +397,7 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)if (ret <0 ) {g_free (s->snapshots); s->snapshots = old_snapshot_list; - goto fail; + goto restore_refcount; }g_free (old_snapshot_list);@@ -410,6 +410,17 @@ int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info)#endif return 0 ;block/qcow2-snapshot.c int qcow2_snapshot_create(BlockDriverState *bs, QEMUSnapshotInfo *sn_info) · line 422
Quoted 10 lines
block/qcow2-snapshot.c +10
-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);
sn->l1_sizeis inuint64_tunits, not in bytes.
Quoted 3 lines
Do you have test cases to exercise these code paths? Use blkdebug to inject errors at the right point so that the error code path is taken.
OK, I'll try it.
Signature · 3 lines
Best Regards Wenchao Xia