diff mbox series

[18/31] qcow2: Update qcow2_get_cluster_offset() to support L2 slices

Message ID 65c2b2b32a686d3084a1579583334a85b706edcb.1507813391.git.berto@igalia.com
State New
Headers show
Series Allow configuring the qcow2 L2 cache entry size | expand

Commit Message

Alberto Garcia Oct. 12, 2017, 1:05 p.m. UTC
qcow2_get_cluster_offset() checks how many contiguous bytes are
available at a given offset. The returned number of bytes is limited
by the amount that can be addressed without having to load more than
one L2 table.

Since we'll be loading L2 slices instead of full tables this patch
changes the limit accordingly using the size of the L2 slice for the
calculations instead of the full table size.

The l2_table variable is also renamed to l2_slice to reflect this, and
offset_to_l2_index() is replaced with offset_to_l2_slice_index().

Signed-off-by: Alberto Garcia <berto@igalia.com>
---
 block/qcow2-cluster.c | 29 ++++++++++++++---------------
 1 file changed, 14 insertions(+), 15 deletions(-)

Comments

Alberto Garcia Oct. 13, 2017, 12:52 p.m. UTC | #1
On Thu, Oct 12, 2017 at 04:05:32PM +0300, Alberto Garcia wrote:
> @@ -522,8 +522,8 @@ int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
>  {
>      BDRVQcow2State *s = bs->opaque;
>      unsigned int l2_index;
> -    uint64_t l1_index, l2_offset, *l2_table;
> -    int l1_bits, c;
> +    uint64_t l1_index, l2_offset, *l2_slice;
> +    int c;
>      unsigned int offset_in_cluster;
>      uint64_t bytes_available, bytes_needed, nb_clusters;
>      QCow2ClusterType type;
> @@ -532,12 +532,11 @@ int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
>      offset_in_cluster = offset_into_cluster(s, offset);
>      bytes_needed = (uint64_t) *bytes + offset_in_cluster;
>  
> -    l1_bits = s->l2_bits + s->cluster_bits;
> -
>      /* compute how many bytes there are between the start of the cluster
> -     * containing offset and the end of the l1 entry */
> -    bytes_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1))
> -                    + offset_in_cluster;
> +     * containing offset and the end of the l2 slice that contains
> +     * the entry pointing to it */
> +    bytes_available = (s->l2_slice_size - offset_to_l2_slice_index(s, offset))
> +        << s->cluster_bits;

There's a mistake here, this should be cast to uint64_t before doing
the shift, else it overflows if the cluster size is large:

    bytes_available =
        ((uint64_t) (s->l2_slice_size - offset_to_l2_slice_index(s, offset)))
        << s->cluster_bits;

The next revision will have this correction, but I'm not sending it
now, so if you want to test/review this one please go ahead.

Berto
diff mbox series

Patch

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 580fb50869..860e56ec7c 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -522,8 +522,8 @@  int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
 {
     BDRVQcow2State *s = bs->opaque;
     unsigned int l2_index;
-    uint64_t l1_index, l2_offset, *l2_table;
-    int l1_bits, c;
+    uint64_t l1_index, l2_offset, *l2_slice;
+    int c;
     unsigned int offset_in_cluster;
     uint64_t bytes_available, bytes_needed, nb_clusters;
     QCow2ClusterType type;
@@ -532,12 +532,11 @@  int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
     offset_in_cluster = offset_into_cluster(s, offset);
     bytes_needed = (uint64_t) *bytes + offset_in_cluster;
 
-    l1_bits = s->l2_bits + s->cluster_bits;
-
     /* compute how many bytes there are between the start of the cluster
-     * containing offset and the end of the l1 entry */
-    bytes_available = (1ULL << l1_bits) - (offset & ((1ULL << l1_bits) - 1))
-                    + offset_in_cluster;
+     * containing offset and the end of the l2 slice that contains
+     * the entry pointing to it */
+    bytes_available = (s->l2_slice_size - offset_to_l2_slice_index(s, offset))
+        << s->cluster_bits;
 
     if (bytes_needed > bytes_available) {
         bytes_needed = bytes_available;
@@ -566,17 +565,17 @@  int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
         return -EIO;
     }
 
-    /* load the l2 table in memory */
+    /* load the l2 slice in memory */
 
-    ret = l2_load(bs, offset, l2_offset, &l2_table);
+    ret = l2_load(bs, offset, l2_offset, &l2_slice);
     if (ret < 0) {
         return ret;
     }
 
     /* find the cluster offset for the given disk offset */
 
-    l2_index = offset_to_l2_index(s, offset);
-    *cluster_offset = be64_to_cpu(l2_table[l2_index]);
+    l2_index = offset_to_l2_slice_index(s, offset);
+    *cluster_offset = be64_to_cpu(l2_slice[l2_index]);
 
     nb_clusters = size_to_clusters(s, bytes_needed);
     /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned
@@ -603,14 +602,14 @@  int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
     case QCOW2_CLUSTER_UNALLOCATED:
         /* how many empty clusters ? */
         c = count_contiguous_clusters_unallocated(nb_clusters,
-                                                  &l2_table[l2_index], type);
+                                                  &l2_slice[l2_index], type);
         *cluster_offset = 0;
         break;
     case QCOW2_CLUSTER_ZERO_ALLOC:
     case QCOW2_CLUSTER_NORMAL:
         /* how many allocated clusters ? */
         c = count_contiguous_clusters(nb_clusters, s->cluster_size,
-                                      &l2_table[l2_index], QCOW_OFLAG_ZERO);
+                                      &l2_slice[l2_index], QCOW_OFLAG_ZERO);
         *cluster_offset &= L2E_OFFSET_MASK;
         if (offset_into_cluster(s, *cluster_offset)) {
             qcow2_signal_corruption(bs, true, -1, -1,
@@ -626,7 +625,7 @@  int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset,
         abort();
     }
 
-    qcow2_cache_put(s->l2_table_cache, (void **) &l2_table);
+    qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
 
     bytes_available = (int64_t)c * s->cluster_size;
 
@@ -644,7 +643,7 @@  out:
     return type;
 
 fail:
-    qcow2_cache_put(s->l2_table_cache, (void **)&l2_table);
+    qcow2_cache_put(s->l2_table_cache, (void **)&l2_slice);
     return ret;
 }