Skip to contentSkip to conversation

qcow2: Restore total_sectors value in save_vmstate

PATCH In review Max Reitz 3 participants 4 messages Oct 20, 2013 – Oct 24, 2013 Block layer

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 qcow2: Restore total_sectors value in save_vmstate

Since df2a6f29a5, bdrv_co_do_writev increases the total_sectors value of a growable block devices on writes after the current end. This leads to the virtual disk apparently growing in qcow2_save_vmstate, which in turn affects the disk size captured by the internal snapshot taken directly afterwards through e.g. the HMP savevm command. Such a "grown" snapshot cannot be loaded after reopening the qcow2 image, since its disk size differs from the actual virtual disk size (writing a VM state does not actually increase the virtual disk size).

Fix this by restoring total_sectors at the end of qcow2_save_vmstate.

---
 block/qcow2.c | 5 +++++
 1 file changed, 5 insertions(+)
block/qcow2.c +5 -0
index c1abaff..5c05bb5 100644
@@ -1939,6 +1939,7 @@ static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
                               int64_t pos)
 {
     BDRVQcowState *s = bs->opaque;
+    int64_t total_sectors = bs->total_sectors;
     int growable = bs->growable;
     int ret;
 
@@ -1946,6 +1947,10 @@ static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
     bs->growable = 1;
     ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
     bs->growable = growable;
+    // bdrv_co_do_writev will have increased the total_sectors value to include
+    // the VM state - the VM state is however not an actual part of the block
+    // device, therefore, we need to restore the old value.
+    bs->total_sectors = total_sectors;
 
     return ret;
 }
Signature · 1 line
1.8.3.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 18 lines · Max Reitz

Since df2a6f29a5, bdrv_co_do_writev increases the total_sectors value of a growable block devices on writes after the current end. This leads to the virtual disk apparently growing in qcow2_save_vmstate, which in turn affects the disk size captured by the internal snapshot taken directly afterwards through e.g. the HMP savevm command. Such a "grown" snapshot cannot be loaded after reopening the qcow2 image, since its disk size differs from the actual virtual disk size (writing a VM state does not actually increase the virtual disk size).

Fix this by restoring total_sectors at the end of qcow2_save_vmstate.

---
 block/qcow2.c | 5 +++++
 1 file changed, 5 insertions(+)

block/qcow2.c static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, · line 1953

Quoted 7 lines
block/qcow2.c +4 -0
@@ -1946,6 +1947,10 @@ static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
     bs->growable = 1;
     ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
     bs->growable = growable;
+    // bdrv_co_do_writev will have increased the total_sectors value to include
+    // the VM state - the VM state is however not an actual part of the block
+    // device, therefore, we need to restore the old value.
+    bs->total_sectors = total_sectors;

It looks like // comments aren't forbidden, but also uncommon; I don't know if /**/ would be better. At any rate:

Signature · 2 lines
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org
author
#2 lore raw
Quoted 17 lines · Eric Blake
Quoted 16 lines · Max Reitz

Since df2a6f29a5, bdrv_co_do_writev increases the total_sectors value of a growable block devices on writes after the current end. This leads to the virtual disk apparently growing in qcow2_save_vmstate, which in turn affects the disk size captured by the internal snapshot taken directly afterwards through e.g. the HMP savevm command. Such a "grown" snapshot cannot be loaded after reopening the qcow2 image, since its disk size differs from the actual virtual disk size (writing a VM state does not actually increase the virtual disk size).

Fix this by restoring total_sectors at the end of qcow2_save_vmstate.

---
  block/qcow2.c | 5 +++++
  1 file changed, 5 insertions(+)
Quoted 10 lines

block/qcow2.c static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, · line 1953

Quoted 8 lines
block/qcow2.c +4 -0
@@ -1946,6 +1947,10 @@ static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
      bs->growable = 1;
      ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
      bs->growable = growable;
+    // bdrv_co_do_writev will have increased the total_sectors value to include
+    // the VM state - the VM state is however not an actual part of the block
+    // device, therefore, we need to restore the old value.
+    bs->total_sectors = total_sectors;

It looks like // comments aren't forbidden, but also uncommon; I don't know if /**/ would be better. At any rate:

Ah, right, sorry, I forgot.

Max

#3 lore raw

Am 23.10.2013 um 19:03 hat Max Reitz geschrieben:

Quoted 29 lines · Eric Blake
Quoted 17 lines · Max Reitz
Quoted 16 lines

Since df2a6f29a5, bdrv_co_do_writev increases the total_sectors value of a growable block devices on writes after the current end. This leads to the virtual disk apparently growing in qcow2_save_vmstate, which in turn affects the disk size captured by the internal snapshot taken directly afterwards through e.g. the HMP savevm command. Such a "grown" snapshot cannot be loaded after reopening the qcow2 image, since its disk size differs from the actual virtual disk size (writing a VM state does not actually increase the virtual disk size).

Fix this by restoring total_sectors at the end of qcow2_save_vmstate.

---
 block/qcow2.c | 5 +++++
 1 file changed, 5 insertions(+)
Quoted 10 lines

block/qcow2.c static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, · line 1953

Quoted 8 lines
block/qcow2.c +4 -0
@@ -1946,6 +1947,10 @@ static int qcow2_save_vmstate(BlockDriverState *bs, QEMUIOVector *qiov,
     bs->growable = 1;
     ret = bdrv_pwritev(bs, qcow2_vm_state_offset(s) + pos, qiov);
     bs->growable = growable;
+    // bdrv_co_do_writev will have increased the total_sectors value to include
+    // the VM state - the VM state is however not an actual part of the block
+    // device, therefore, we need to restore the old value.
+    bs->total_sectors = total_sectors;

It looks like // comments aren't forbidden, but also uncommon; I don't know if /**/ would be better. At any rate:

Quoted 1 line

Ah, right, sorry, I forgot.

Thanks, fixed up the command and applied to the block branch.

Kevin