Skip to contentSkip to conversation

qcow2-refcount: Snapshot update for zero clusters

PATCH Superseded Max Reitz 3 participants 9 messages Aug 29, 2013 Block layer

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 qcow2-refcount: Snapshot update for zero clusters

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

---
 block/qcow2-refcount.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
block/qcow2-refcount.c +13 -3
index 1244693..7555242 100644
@@ -863,9 +863,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
             for(j = 0; j < s->l2_size; j++) {
                 offset = be64_to_cpu(l2_table[j]);
                 if (offset != 0) {
+                    uint64_t cluster_index;
+
                     old_offset = offset;
                     offset &= ~QCOW_OFLAG_COPIED;
-                    if (offset & QCOW_OFLAG_COMPRESSED) {
+
+                    switch (qcow2_get_cluster_type(offset)) {
+                    case QCOW2_CLUSTER_COMPRESSED:
                         nb_csectors = ((offset >> s->csize_shift) &
                                        s->csize_mask) + 1;
                         if (addend != 0) {
@@ -880,8 +884,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                         }
                         /* compressed clusters are never modified */
                         refcount = 2;
-                    } else {
-                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
+                        break;
+
+                    case QCOW2_CLUSTER_NORMAL:
+                        cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
                         if (addend != 0) {
                             refcount = update_cluster_refcount(bs, cluster_index, addend,
                                                                QCOW2_DISCARD_SNAPSHOT);
@@ -893,6 +899,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                             ret = refcount;
                             goto fail;
                         }
+                        break;
+
+                    default:
+                        refcount = 0;
                     }
 
                     if (refcount == 1) {
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 2 lines · Max Reitz

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

Why? What does this fix?

(Hint - your commit message could use more details, and you should add a testsuite addition that exposes the case that you are fixing)

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

Am 29.08.2013 14:30, schrieb Eric Blake:

Quoted 8 lines · Max Reitz
Quoted 2 lines

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

Why? What does this fix?

(Hint - your commit message could use more details, and you should add a testsuite addition that exposes the case that you are fixing)

Okay, I'll put more detail into the message.

The test case is actually kind of contained in my series "block/qcow2: Image file option amendment", but, of course, you're right, a dedicated test case won't hurt.

Max

#3 lore raw
In reply to Max Reitz

Am 29.08.2013 um 12:16 hat Max Reitz geschrieben:

Quoted 7 lines

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

---
 block/qcow2-refcount.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Please don't forget to add a test case for v2.

Quoted 33 lines
block/qcow2-refcount.c +9 -3
index 1244693..7555242 100644
@@ -863,9 +863,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
             for(j = 0; j < s->l2_size; j++) {
                 offset = be64_to_cpu(l2_table[j]);
                 if (offset != 0) {
+                    uint64_t cluster_index;
+
                     old_offset = offset;
                     offset &= ~QCOW_OFLAG_COPIED;
-                    if (offset & QCOW_OFLAG_COMPRESSED) {
+
+                    switch (qcow2_get_cluster_type(offset)) {
+                    case QCOW2_CLUSTER_COMPRESSED:
                         nb_csectors = ((offset >> s->csize_shift) &
                                        s->csize_mask) + 1;
                         if (addend != 0) {
@@ -880,8 +884,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                         }
                         /* compressed clusters are never modified */
                         refcount = 2;
-                    } else {
-                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
+                        break;
+
+                    case QCOW2_CLUSTER_NORMAL:
+                        cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
                         if (addend != 0) {
                             refcount = update_cluster_refcount(bs, cluster_index, addend,
                                                                QCOW2_DISCARD_SNAPSHOT);

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 902

Quoted 4 lines
block/qcow2-refcount.c +1 -0
@@ -893,6 +899,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                             ret = refcount;
                             goto fail;
                         }
+                        break;

I think this part isn't quite right. Even zero clusters can have an allocated offset, so I think what's really needed is that the same code runs from QCOW2_CLUSTER_ZERO. However, add an if (cluster_index == 0) case for the unallocated case.

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 905

Quoted 3 lines
block/qcow2-refcount.c +3 -0
+
+                    default:
+                        refcount = 0;

Other places enumerate the enum values explicitly and have a default label that simply abort()s. This will help catching forgotten places when a new value is added.

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 908

Quoted 3 lines
block/qcow2-refcount.c +0 -0
                     }
 
                     if (refcount == 1) {

Kevin

author
#4 lore raw

Am 29.08.2013 14:33, schrieb Kevin Wolf:

Quoted 9 lines

Am 29.08.2013 um 12:16 hat Max Reitz geschrieben:

Quoted 7 lines

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

---
  block/qcow2-refcount.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)

Please don't forget to add a test case for v2.

Okay.

Quoted 33 lines
Quoted 33 lines
block/qcow2-refcount.c +9 -3
index 1244693..7555242 100644
@@ -863,9 +863,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
              for(j = 0; j < s->l2_size; j++) {
                  offset = be64_to_cpu(l2_table[j]);
                  if (offset != 0) {
+                    uint64_t cluster_index;
+
                      old_offset = offset;
                      offset &= ~QCOW_OFLAG_COPIED;
-                    if (offset & QCOW_OFLAG_COMPRESSED) {
+
+                    switch (qcow2_get_cluster_type(offset)) {
+                    case QCOW2_CLUSTER_COMPRESSED:
                          nb_csectors = ((offset >> s->csize_shift) &
                                         s->csize_mask) + 1;
                          if (addend != 0) {
@@ -880,8 +884,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                          }
                          /* compressed clusters are never modified */
                          refcount = 2;
-                    } else {
-                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
+                        break;
+
+                    case QCOW2_CLUSTER_NORMAL:
+                        cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
                          if (addend != 0) {
                              refcount = update_cluster_refcount(bs, cluster_index, addend,
                                                                 QCOW2_DISCARD_SNAPSHOT);
@@ -893,6 +899,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
Quoted 8 lines

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 902

Quoted 4 lines
block/qcow2-refcount.c +1 -0
                              ret = refcount;
                              goto fail;
                          }
+                        break;

I think this part isn't quite right. Even zero clusters can have an allocated offset, so I think what's really needed is that the same code runs from QCOW2_CLUSTER_ZERO. However, add an if (cluster_index == 0) case for the unallocated case.

So then it would be enough to change:

- if (offset != 0) {
+ if ((offset & L2E_OFFSET_MASK) != 0) {

and just execute the same code for QCOW2_CLUSTER_NORMAL and QCOW2_CLUSTER_ZERO?

Quoted 6 lines

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 905

Quoted 3 lines
block/qcow2-refcount.c +3 -0
+
+                    default:
+                        refcount = 0;

Other places enumerate the enum values explicitly and have a default label that simply abort()s. This will help catching forgotten places when a new value is added.

Okay.

Quoted 4 lines

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 908

Quoted 3 lines
block/qcow2-refcount.c +0 -0
                      }
 
                      if (refcount == 1) {

Kevin

Max

#5 lore raw

Am 29.08.2013 um 14:38 hat Max Reitz geschrieben:

Quoted 53 lines

Am 29.08.2013 14:33, schrieb Kevin Wolf:

Quoted 9 lines

Am 29.08.2013 um 12:16 hat Max Reitz geschrieben:

Quoted 7 lines

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

---
 block/qcow2-refcount.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Please don't forget to add a test case for v2.

Okay.

Quoted 33 lines
Quoted 33 lines
block/qcow2-refcount.c +9 -3
index 1244693..7555242 100644
@@ -863,9 +863,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
             for(j = 0; j < s->l2_size; j++) {
                 offset = be64_to_cpu(l2_table[j]);
                 if (offset != 0) {
+                    uint64_t cluster_index;
+
                     old_offset = offset;
                     offset &= ~QCOW_OFLAG_COPIED;
-                    if (offset & QCOW_OFLAG_COMPRESSED) {
+
+                    switch (qcow2_get_cluster_type(offset)) {
+                    case QCOW2_CLUSTER_COMPRESSED:
                         nb_csectors = ((offset >> s->csize_shift) &
                                        s->csize_mask) + 1;
                         if (addend != 0) {
@@ -880,8 +884,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                         }
                         /* compressed clusters are never modified */
                         refcount = 2;
-                    } else {
-                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
+                        break;
+
+                    case QCOW2_CLUSTER_NORMAL:
+                        cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
                         if (addend != 0) {
                             refcount = update_cluster_refcount(bs, cluster_index, addend,
                                                                QCOW2_DISCARD_SNAPSHOT);
@@ -893,6 +899,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
Quoted 8 lines

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 902

Quoted 4 lines
block/qcow2-refcount.c +1 -0
                             ret = refcount;
                             goto fail;
                         }
+                        break;

I think this part isn't quite right. Even zero clusters can have an allocated offset, so I think what's really needed is that the same code runs from QCOW2_CLUSTER_ZERO. However, add an if (cluster_index == 0) case for the unallocated case.

Quoted 7 lines

So then it would be enough to change:

- if (offset != 0) {
+ if ((offset & L2E_OFFSET_MASK) != 0) {

and just execute the same code for QCOW2_CLUSTER_NORMAL and QCOW2_CLUSTER_ZERO?

Doing only this change in the original code might happen to work, but it's not really clean as the L2 entry format is different for compressed clusters (L2E_OFFSET_MASK isn't valid for it). So having the switch for the cluster type is a good thing, but the implementation for normal and zero clusters should be shared indeed.

The offset != 0 check ends up redundant when you introduce the switch, so in theory it could as well be removed.

Kevin

author
#6 lore raw

Am 29.08.2013 14:53, schrieb Kevin Wolf:

Quoted 59 lines

Am 29.08.2013 um 14:38 hat Max Reitz geschrieben:

Quoted 58 lines

Am 29.08.2013 14:33, schrieb Kevin Wolf:

Quoted 9 lines

Am 29.08.2013 um 12:16 hat Max Reitz geschrieben:

Quoted 7 lines

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

---
  block/qcow2-refcount.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)

Please don't forget to add a test case for v2.

Okay.

Quoted 33 lines
Quoted 33 lines
block/qcow2-refcount.c +9 -3
index 1244693..7555242 100644
@@ -863,9 +863,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
              for(j = 0; j < s->l2_size; j++) {
                  offset = be64_to_cpu(l2_table[j]);
                  if (offset != 0) {
+                    uint64_t cluster_index;
+
                      old_offset = offset;
                      offset &= ~QCOW_OFLAG_COPIED;
-                    if (offset & QCOW_OFLAG_COMPRESSED) {
+
+                    switch (qcow2_get_cluster_type(offset)) {
+                    case QCOW2_CLUSTER_COMPRESSED:
                          nb_csectors = ((offset >> s->csize_shift) &
                                         s->csize_mask) + 1;
                          if (addend != 0) {
@@ -880,8 +884,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                          }
                          /* compressed clusters are never modified */
                          refcount = 2;
-                    } else {
-                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
+                        break;
+
+                    case QCOW2_CLUSTER_NORMAL:
+                        cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
                          if (addend != 0) {
                              refcount = update_cluster_refcount(bs, cluster_index, addend,
                                                                 QCOW2_DISCARD_SNAPSHOT);
@@ -893,6 +899,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
Quoted 8 lines

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 902

Quoted 4 lines
block/qcow2-refcount.c +1 -0
                              ret = refcount;
                              goto fail;
                          }
+                        break;

I think this part isn't quite right. Even zero clusters can have an allocated offset, so I think what's really needed is that the same code runs from QCOW2_CLUSTER_ZERO. However, add an if (cluster_index == 0) case for the unallocated case.

So then it would be enough to change:

- if (offset != 0) {
+ if ((offset & L2E_OFFSET_MASK) != 0) {

Quoted 7 lines
Quoted 2 lines

and just execute the same code for QCOW2_CLUSTER_NORMAL and QCOW2_CLUSTER_ZERO?

Doing only this change in the original code might happen to work, but it's not really clean as the L2 entry format is different for compressed clusters (L2E_OFFSET_MASK isn't valid for it). So having the switch for the cluster type is a good thing, but the implementation for normal and zero clusters should be shared indeed.

Yes, of course I meant to include the switch as well.

Quoted 3 lines

The offset != 0 check ends up redundant when you introduce the switch, so in theory it could as well be removed.

Well, I don't know; for zero clusters, the if statement is required anyway (since qcow2_get_cluster_type does not distinguish between allocated and non-allocated zero clusters), so I though we might as well just leave it where it is (just slightly adjusted in the way I proposed).

Max

#7 lore raw

Am 29.08.2013 um 14:56 hat Max Reitz geschrieben:

Quoted 69 lines

Am 29.08.2013 14:53, schrieb Kevin Wolf:

Quoted 59 lines

Am 29.08.2013 um 14:38 hat Max Reitz geschrieben:

Quoted 58 lines

Am 29.08.2013 14:33, schrieb Kevin Wolf:

Quoted 9 lines

Am 29.08.2013 um 12:16 hat Max Reitz geschrieben:

Quoted 7 lines

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

---
 block/qcow2-refcount.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

Please don't forget to add a test case for v2.

Okay.

Quoted 33 lines
Quoted 33 lines
block/qcow2-refcount.c +9 -3
index 1244693..7555242 100644
@@ -863,9 +863,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
             for(j = 0; j < s->l2_size; j++) {
                 offset = be64_to_cpu(l2_table[j]);
                 if (offset != 0) {
+                    uint64_t cluster_index;
+
                     old_offset = offset;
                     offset &= ~QCOW_OFLAG_COPIED;
-                    if (offset & QCOW_OFLAG_COMPRESSED) {
+
+                    switch (qcow2_get_cluster_type(offset)) {
+                    case QCOW2_CLUSTER_COMPRESSED:
                         nb_csectors = ((offset >> s->csize_shift) &
                                        s->csize_mask) + 1;
                         if (addend != 0) {
@@ -880,8 +884,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                         }
                         /* compressed clusters are never modified */
                         refcount = 2;
-                    } else {
-                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
+                        break;
+
+                    case QCOW2_CLUSTER_NORMAL:
+                        cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
                         if (addend != 0) {
                             refcount = update_cluster_refcount(bs, cluster_index, addend,
                                                                QCOW2_DISCARD_SNAPSHOT);
@@ -893,6 +899,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
Quoted 8 lines

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 902

Quoted 4 lines
block/qcow2-refcount.c +1 -0
                             ret = refcount;
                             goto fail;
                         }
+                        break;

I think this part isn't quite right. Even zero clusters can have an allocated offset, so I think what's really needed is that the same code runs from QCOW2_CLUSTER_ZERO. However, add an if (cluster_index == 0) case for the unallocated case.

So then it would be enough to change:

- if (offset != 0) {
+ if ((offset & L2E_OFFSET_MASK) != 0) {

Quoted 7 lines
Quoted 2 lines

and just execute the same code for QCOW2_CLUSTER_NORMAL and QCOW2_CLUSTER_ZERO?

Doing only this change in the original code might happen to work, but it's not really clean as the L2 entry format is different for compressed clusters (L2E_OFFSET_MASK isn't valid for it). So having the switch for the cluster type is a good thing, but the implementation for normal and zero clusters should be shared indeed.

Yes, of course I meant to include the switch as well.

Quoted 8 lines
Quoted 3 lines

The offset != 0 check ends up redundant when you introduce the switch, so in theory it could as well be removed.

Well, I don't know; for zero clusters, the if statement is required anyway (since qcow2_get_cluster_type does not distinguish between allocated and non-allocated zero clusters), so I though we might as well just leave it where it is (just slightly adjusted in the way I proposed).

You do need a zero check, but it's only for normal/zero clusters, not for compressed ones; and it must check offset & L2E_OFFSET_MASK (or cluster_index) rather than offset itself.

So I think you end up removing the outer if (offset != 0) and adding a new inner check in case QCOW2_CLUSTER_NORMAL/ZERO.

Kevin

author
#8 lore raw

Am 29.08.2013 15:04, schrieb Kevin Wolf:

Quoted 78 lines

Am 29.08.2013 um 14:56 hat Max Reitz geschrieben:

Quoted 69 lines

Am 29.08.2013 14:53, schrieb Kevin Wolf:

Quoted 59 lines

Am 29.08.2013 um 14:38 hat Max Reitz geschrieben:

Quoted 58 lines

Am 29.08.2013 14:33, schrieb Kevin Wolf:

Quoted 9 lines

Am 29.08.2013 um 12:16 hat Max Reitz geschrieben:

Quoted 7 lines

Do not try to update the refcount for zero clusters in qcow2_update_snapshot_refcount.

---
  block/qcow2-refcount.c | 16 +++++++++++++---
  1 file changed, 13 insertions(+), 3 deletions(-)

Please don't forget to add a test case for v2.

Okay.

Quoted 33 lines
Quoted 33 lines
block/qcow2-refcount.c +9 -3
index 1244693..7555242 100644
@@ -863,9 +863,13 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
              for(j = 0; j < s->l2_size; j++) {
                  offset = be64_to_cpu(l2_table[j]);
                  if (offset != 0) {
+                    uint64_t cluster_index;
+
                      old_offset = offset;
                      offset &= ~QCOW_OFLAG_COPIED;
-                    if (offset & QCOW_OFLAG_COMPRESSED) {
+
+                    switch (qcow2_get_cluster_type(offset)) {
+                    case QCOW2_CLUSTER_COMPRESSED:
                          nb_csectors = ((offset >> s->csize_shift) &
                                         s->csize_mask) + 1;
                          if (addend != 0) {
@@ -880,8 +884,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
                          }
                          /* compressed clusters are never modified */
                          refcount = 2;
-                    } else {
-                        uint64_t cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
+                        break;
+
+                    case QCOW2_CLUSTER_NORMAL:
+                        cluster_index = (offset & L2E_OFFSET_MASK) >> s->cluster_bits;
                          if (addend != 0) {
                              refcount = update_cluster_refcount(bs, cluster_index, addend,
                                                                 QCOW2_DISCARD_SNAPSHOT);
@@ -893,6 +899,10 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
Quoted 8 lines

block/qcow2-refcount.c int qcow2_update_snapshot_refcount(BlockDriverState *bs, · line 902

Quoted 4 lines
block/qcow2-refcount.c +1 -0
                              ret = refcount;
                              goto fail;
                          }
+                        break;

I think this part isn't quite right. Even zero clusters can have an allocated offset, so I think what's really needed is that the same code runs from QCOW2_CLUSTER_ZERO. However, add an if (cluster_index == 0) case for the unallocated case.

So then it would be enough to change:

- if (offset != 0) {
+ if ((offset & L2E_OFFSET_MASK) != 0) {

Quoted 7 lines
Quoted 2 lines

and just execute the same code for QCOW2_CLUSTER_NORMAL and QCOW2_CLUSTER_ZERO?

Doing only this change in the original code might happen to work, but it's not really clean as the L2 entry format is different for compressed clusters (L2E_OFFSET_MASK isn't valid for it). So having the switch for the cluster type is a good thing, but the implementation for normal and zero clusters should be shared indeed.

Yes, of course I meant to include the switch as well.

Quoted 8 lines
Quoted 3 lines

The offset != 0 check ends up redundant when you introduce the switch, so in theory it could as well be removed.

Well, I don't know; for zero clusters, the if statement is required anyway (since qcow2_get_cluster_type does not distinguish between allocated and non-allocated zero clusters), so I though we might as well just leave it where it is (just slightly adjusted in the way I proposed).

Quoted 8 lines

You do need a zero check, but it's only for normal/zero clusters, not for compressed ones; and it must check offset & L2E_OFFSET_MASK (or cluster_index) rather than offset itself.

So I think you end up removing the outer if (offset != 0) and adding a new inner check in case QCOW2_CLUSTER_NORMAL/ZERO.

Kevin

Ah, yes, you're right.

Max