Browse Source

qcow2: Define and use QCOW2_COMPRESSED_SECTOR_SIZE

When an L2 table entry points to a compressed cluster the space used
by the data is specified in 512-byte sectors. This size is independent
from BDRV_SECTOR_SIZE and is specific to the qcow2 file format.

The QCOW2_COMPRESSED_SECTOR_SIZE constant defined in this patch makes
this explicit.

Signed-off-by: Alberto Garcia <berto@igalia.com>
Signed-off-by: Kevin Wolf <kwolf@redhat.com>
tags/v4.1.0-rc0
Alberto Garcia 2 months ago
parent
commit
b6c246942b
4 changed files with 23 additions and 14 deletions
  1. 3
    2
      block/qcow2-cluster.c
  2. 14
    11
      block/qcow2-refcount.c
  3. 2
    1
      block/qcow2.c
  4. 4
    0
      block/qcow2.h

+ 3
- 2
block/qcow2-cluster.c View File

@@ -796,8 +796,9 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs,
796 796
         return cluster_offset;
797 797
     }
798 798
 
799
-    nb_csectors = ((cluster_offset + compressed_size - 1) >> 9) -
800
-                  (cluster_offset >> 9);
799
+    nb_csectors =
800
+        (cluster_offset + compressed_size - 1) / QCOW2_COMPRESSED_SECTOR_SIZE -
801
+        (cluster_offset / QCOW2_COMPRESSED_SECTOR_SIZE);
801 802
 
802 803
     cluster_offset |= QCOW_OFLAG_COMPRESSED |
803 804
                       ((uint64_t)nb_csectors << s->csize_shift);

+ 14
- 11
block/qcow2-refcount.c View File

@@ -1172,12 +1172,11 @@ void qcow2_free_any_clusters(BlockDriverState *bs, uint64_t l2_entry,
1172 1172
     switch (ctype) {
1173 1173
     case QCOW2_CLUSTER_COMPRESSED:
1174 1174
         {
1175
-            int nb_csectors;
1176
-            nb_csectors = ((l2_entry >> s->csize_shift) &
1177
-                           s->csize_mask) + 1;
1178
-            qcow2_free_clusters(bs,
1179
-                (l2_entry & s->cluster_offset_mask) & ~511,
1180
-                nb_csectors * 512, type);
1175
+            int64_t offset = (l2_entry & s->cluster_offset_mask)
1176
+                & QCOW2_COMPRESSED_SECTOR_MASK;
1177
+            int size = QCOW2_COMPRESSED_SECTOR_SIZE *
1178
+                (((l2_entry >> s->csize_shift) & s->csize_mask) + 1);
1179
+            qcow2_free_clusters(bs, offset, size, type);
1181 1180
         }
1182 1181
         break;
1183 1182
     case QCOW2_CLUSTER_NORMAL:
@@ -1317,9 +1316,12 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs,
1317 1316
                         nb_csectors = ((entry >> s->csize_shift) &
1318 1317
                                        s->csize_mask) + 1;
1319 1318
                         if (addend != 0) {
1319
+                            uint64_t coffset = (entry & s->cluster_offset_mask)
1320
+                                & QCOW2_COMPRESSED_SECTOR_MASK;
1320 1321
                             ret = update_refcount(
1321
-                                bs, (entry & s->cluster_offset_mask) & ~511,
1322
-                                nb_csectors * 512, abs(addend), addend < 0,
1322
+                                bs, coffset,
1323
+                                nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE,
1324
+                                abs(addend), addend < 0,
1323 1325
                                 QCOW2_DISCARD_SNAPSHOT);
1324 1326
                             if (ret < 0) {
1325 1327
                                 goto fail;
@@ -1635,9 +1637,10 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res,
1635 1637
             nb_csectors = ((l2_entry >> s->csize_shift) &
1636 1638
                            s->csize_mask) + 1;
1637 1639
             l2_entry &= s->cluster_offset_mask;
1638
-            ret = qcow2_inc_refcounts_imrt(bs, res,
1639
-                                           refcount_table, refcount_table_size,
1640
-                                           l2_entry & ~511, nb_csectors * 512);
1640
+            ret = qcow2_inc_refcounts_imrt(
1641
+                bs, res, refcount_table, refcount_table_size,
1642
+                l2_entry & QCOW2_COMPRESSED_SECTOR_MASK,
1643
+                nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE);
1641 1644
             if (ret < 0) {
1642 1645
                 goto fail;
1643 1646
             }

+ 2
- 1
block/qcow2.c View File

@@ -4187,7 +4187,8 @@ qcow2_co_preadv_compressed(BlockDriverState *bs,
4187 4187
 
4188 4188
     coffset = file_cluster_offset & s->cluster_offset_mask;
4189 4189
     nb_csectors = ((file_cluster_offset >> s->csize_shift) & s->csize_mask) + 1;
4190
-    csize = nb_csectors * 512 - (coffset & 511);
4190
+    csize = nb_csectors * QCOW2_COMPRESSED_SECTOR_SIZE -
4191
+        (coffset & ~QCOW2_COMPRESSED_SECTOR_MASK);
4191 4192
 
4192 4193
     buf = g_try_malloc(csize);
4193 4194
     if (!buf) {

+ 4
- 0
block/qcow2.h View File

@@ -74,6 +74,10 @@
74 74
 #define MIN_CLUSTER_BITS 9
75 75
 #define MAX_CLUSTER_BITS 21
76 76
 
77
+/* Defined in the qcow2 spec (compressed cluster descriptor) */
78
+#define QCOW2_COMPRESSED_SECTOR_SIZE 512U
79
+#define QCOW2_COMPRESSED_SECTOR_MASK (~(QCOW2_COMPRESSED_SECTOR_SIZE - 1))
80
+
77 81
 /* Must be at least 2 to cover COW */
78 82
 #define MIN_L2_CACHE_SIZE 2 /* cache entries */
79 83
 

Loading…
Cancel
Save