All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alberto Garcia <berto@igalia.com>
To: qemu-devel@nongnu.org
Cc: Alberto Garcia <berto@igalia.com>,
	qemu-block@nongnu.org, Max Reitz <mreitz@redhat.com>,
	Kevin Wolf <kwolf@redhat.com>, Eric Blake <eblake@redhat.com>,
	Anton Nefedov <anton.nefedov@virtuozzo.com>,
	"Denis V . Lunev" <den@openvz.org>
Subject: [Qemu-devel] [PATCH v3 30/39] qcow2: Update expand_zero_clusters_in_l1() to support L2 slices
Date: Fri, 26 Jan 2018 16:59:57 +0200	[thread overview]
Message-ID: <7d8d80bfde60d29e8a0abe04db8241b754714329.1516978645.git.berto@igalia.com> (raw)
In-Reply-To: <cover.1516978645.git.berto@igalia.com>
In-Reply-To: <cover.1516978645.git.berto@igalia.com>

expand_zero_clusters_in_l1() expands zero clusters as a necessary step
to downgrade qcow2 images to a version that doesn't support metadata
zero clusters. This function takes an L1 table (which may or may not
be active) and iterates over all its L2 tables looking for zero
clusters.

Since we'll be loading L2 slices instead of full tables we need to add
an extra loop that iterates over all slices of each L2 table, and we
should also use the slice size when allocating the buffer used when
the L1 table is not active.

This function doesn't need any additional changes so apart from that
this patch simply updates the variable name from l2_table to l2_slice.

Finally, and since we have to touch the bdrv_read() / bdrv_write()
calls anyway, this patch takes the opportunity to replace them with
the byte-based bdrv_pread() / bdrv_pwrite().

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

diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c
index 6042ec69e3..659830ad4d 100644
--- a/block/qcow2-cluster.c
+++ b/block/qcow2-cluster.c
@@ -1864,22 +1864,25 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
 {
     BDRVQcow2State *s = bs->opaque;
     bool is_active_l1 = (l1_table == s->l1_table);
-    uint64_t *l2_table = NULL;
+    uint64_t *l2_slice = NULL;
+    unsigned slice, slice_size2, n_slices;
     int ret;
     int i, j;
 
+    slice_size2 = s->l2_slice_size * sizeof(uint64_t);
+    n_slices = s->cluster_size / slice_size2;
+
     if (!is_active_l1) {
         /* inactive L2 tables require a buffer to be stored in when loading
          * them from disk */
-        l2_table = qemu_try_blockalign(bs->file->bs, s->cluster_size);
-        if (l2_table == NULL) {
+        l2_slice = qemu_try_blockalign(bs->file->bs, slice_size2);
+        if (l2_slice == NULL) {
             return -ENOMEM;
         }
     }
 
     for (i = 0; i < l1_size; i++) {
         uint64_t l2_offset = l1_table[i] & L1E_OFFSET_MASK;
-        bool l2_dirty = false;
         uint64_t l2_refcount;
 
         if (!l2_offset) {
@@ -1905,22 +1908,24 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
             goto fail;
         }
 
-        {
+        for (slice = 0; slice < n_slices; slice++) {
+            uint64_t slice_offset = l2_offset + slice * slice_size2;
+            bool l2_dirty = false;
             if (is_active_l1) {
                 /* get active L2 tables from cache */
-                ret = qcow2_cache_get(bs, s->l2_table_cache, l2_offset,
-                                      (void **)&l2_table);
+                ret = qcow2_cache_get(bs, s->l2_table_cache, slice_offset,
+                                      (void **)&l2_slice);
             } else {
                 /* load inactive L2 tables from disk */
-                ret = bdrv_read(bs->file, l2_offset / BDRV_SECTOR_SIZE,
-                                (void *)l2_table, s->cluster_sectors);
+                ret = bdrv_pread(bs->file, slice_offset,
+                                 (void *)l2_slice, slice_size2);
             }
             if (ret < 0) {
                 goto fail;
             }
 
-            for (j = 0; j < s->l2_size; j++) {
-                uint64_t l2_entry = be64_to_cpu(l2_table[j]);
+            for (j = 0; j < s->l2_slice_size; j++) {
+                uint64_t l2_entry = be64_to_cpu(l2_slice[j]);
                 int64_t offset = l2_entry & L2E_OFFSET_MASK;
                 QCow2ClusterType cluster_type =
                     qcow2_get_cluster_type(l2_entry);
@@ -1934,7 +1939,7 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
                     if (!bs->backing) {
                         /* not backed; therefore we can simply deallocate the
                          * cluster */
-                        l2_table[j] = 0;
+                        l2_slice[j] = 0;
                         l2_dirty = true;
                         continue;
                     }
@@ -1961,12 +1966,13 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
                 }
 
                 if (offset_into_cluster(s, offset)) {
+                    int l2_index = slice * s->l2_slice_size + j;
                     qcow2_signal_corruption(
                         bs, true, -1, -1,
                         "Cluster allocation offset "
                         "%#" PRIx64 " unaligned (L2 offset: %#"
                         PRIx64 ", L2 index: %#x)", offset,
-                        l2_offset, j);
+                        l2_offset, l2_index);
                     if (cluster_type == QCOW2_CLUSTER_ZERO_PLAIN) {
                         qcow2_free_clusters(bs, offset, s->cluster_size,
                                             QCOW2_DISCARD_ALWAYS);
@@ -1995,30 +2001,30 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
                 }
 
                 if (l2_refcount == 1) {
-                    l2_table[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
+                    l2_slice[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED);
                 } else {
-                    l2_table[j] = cpu_to_be64(offset);
+                    l2_slice[j] = cpu_to_be64(offset);
                 }
                 l2_dirty = true;
             }
 
             if (is_active_l1) {
                 if (l2_dirty) {
-                    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_table);
+                    qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice);
                     qcow2_cache_depends_on_flush(s->l2_table_cache);
                 }
-                qcow2_cache_put(s->l2_table_cache, (void **) &l2_table);
+                qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
             } else {
                 if (l2_dirty) {
                     ret = qcow2_pre_write_overlap_check(
                         bs, QCOW2_OL_INACTIVE_L2 | QCOW2_OL_ACTIVE_L2,
-                        l2_offset, s->cluster_size);
+                        slice_offset, slice_size2);
                     if (ret < 0) {
                         goto fail;
                     }
 
-                    ret = bdrv_write(bs->file, l2_offset / BDRV_SECTOR_SIZE,
-                                     (void *)l2_table, s->cluster_sectors);
+                    ret = bdrv_pwrite(bs->file, slice_offset,
+                                      (void *)l2_slice, slice_size2);
                     if (ret < 0) {
                         goto fail;
                     }
@@ -2035,11 +2041,11 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table,
     ret = 0;
 
 fail:
-    if (l2_table) {
+    if (l2_slice) {
         if (!is_active_l1) {
-            qemu_vfree(l2_table);
+            qemu_vfree(l2_slice);
         } else {
-            qcow2_cache_put(s->l2_table_cache, (void **) &l2_table);
+            qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);
         }
     }
     return ret;
-- 
2.11.0

  parent reply	other threads:[~2018-01-26 15:57 UTC|newest]

Thread overview: 108+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-26 14:59 [Qemu-devel] [PATCH v3 00/39] Allow configuring the qcow2 L2 cache entry size Alberto Garcia
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 01/39] qcow2: Fix documentation of get_cluster_table() Alberto Garcia
2018-01-31 19:16   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 02/39] qcow2: Add table size field to Qcow2Cache Alberto Garcia
2018-01-31 19:25   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 03/39] qcow2: Remove BDS parameter from qcow2_cache_get_table_addr() Alberto Garcia
2018-01-31 19:26   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 04/39] qcow2: Remove BDS parameter from qcow2_cache_get_table_idx() Alberto Garcia
2018-01-31 19:27   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 05/39] qcow2: Remove BDS parameter from qcow2_cache_table_release() Alberto Garcia
2018-01-31 19:28   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 06/39] qcow2: Remove BDS parameter from qcow2_cache_entry_mark_dirty() Alberto Garcia
2018-01-31 19:30   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 07/39] qcow2: Remove BDS parameter from qcow2_cache_put() Alberto Garcia
2018-01-31 19:33   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 08/39] qcow2: Remove BDS parameter from qcow2_cache_destroy() Alberto Garcia
2018-01-31 19:35   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 09/39] qcow2: Remove BDS parameter from qcow2_cache_clean_unused() Alberto Garcia
2018-01-31 19:37   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 10/39] qcow2: Remove BDS parameter from qcow2_cache_discard() Alberto Garcia
2018-01-31 19:38   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 11/39] qcow2: Remove BDS parameter from qcow2_cache_is_table_offset() Alberto Garcia
2018-01-31 19:39   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 12/39] qcow2: Add offset_to_l1_index() Alberto Garcia
2018-01-31 19:43   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 13/39] qcow2: Add l2_slice_size field to BDRVQcow2State Alberto Garcia
2018-01-31 19:48   ` Max Reitz
2018-02-01  9:51     ` Alberto Garcia
2018-02-01 18:09       ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 14/39] qcow2: Add offset_to_l2_slice_index() Alberto Garcia
2018-01-31 19:49   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 15/39] qcow2: Update l2_load() to support L2 slices Alberto Garcia
2018-01-31 19:56   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 16/39] qcow2: Prepare l2_allocate() for adding L2 slice support Alberto Garcia
2018-01-26 16:24   ` Eric Blake
2018-01-31 19:57   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 17/39] qcow2: Update l2_allocate() to support L2 slices Alberto Garcia
2018-01-26 16:30   ` Eric Blake
2018-01-31 20:07   ` Max Reitz
2018-02-01 13:13     ` Alberto Garcia
2018-02-01 15:23       ` Anton Nefedov
2018-02-01 15:43         ` Alberto Garcia
2018-02-01 18:22           ` Max Reitz
2018-02-02  8:08             ` Alberto Garcia
2018-02-01 18:15       ` Max Reitz
2018-02-02  9:41         ` Alberto Garcia
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 18/39] qcow2: Refactor get_cluster_table() Alberto Garcia
2018-01-26 16:37   ` Eric Blake
2018-01-31 20:11   ` Max Reitz
2018-02-01 10:40     ` Alberto Garcia
2018-02-01 18:10       ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 19/39] qcow2: Update get_cluster_table() to support L2 slices Alberto Garcia
2018-01-26 16:39   ` Eric Blake
2018-01-31 20:14   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 20/39] qcow2: Update qcow2_get_cluster_offset() " Alberto Garcia
2018-01-31 20:24   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 21/39] qcow2: Update qcow2_alloc_cluster_link_l2() " Alberto Garcia
2018-02-01 18:44   ` Max Reitz
2018-02-02  9:43     ` Alberto Garcia
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 22/39] qcow2: Update handle_copied() " Alberto Garcia
2018-02-01 18:54   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 23/39] qcow2: Update handle_alloc() " Alberto Garcia
2018-02-01 18:56   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 24/39] qcow2: Update discard_single_l2() " Alberto Garcia
2018-02-01 19:07   ` Max Reitz
2018-02-02  9:46     ` Alberto Garcia
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 25/39] qcow2: Update zero_single_l2() " Alberto Garcia
2018-02-01 19:08   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 26/39] qcow2: Prepare qcow2_update_snapshot_refcount() for adding L2 slice support Alberto Garcia
2018-01-26 19:38   ` Eric Blake
2018-02-01 19:21   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 27/39] qcow2: Update qcow2_update_snapshot_refcount() to support L2 slices Alberto Garcia
2018-01-26 19:39   ` Eric Blake
2018-02-01 19:26   ` Max Reitz
2018-02-02  9:56     ` Alberto Garcia
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 28/39] qcow2: Read refcount before L2 table in expand_zero_clusters_in_l1() Alberto Garcia
2018-01-26 19:41   ` Eric Blake
2018-02-01 19:29   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 29/39] qcow2: Prepare expand_zero_clusters_in_l1() for adding L2 slice support Alberto Garcia
2018-01-26 19:42   ` Eric Blake
2018-02-01 19:34   ` Max Reitz
2018-01-26 14:59 ` Alberto Garcia [this message]
2018-01-26 19:46   ` [Qemu-devel] [PATCH v3 30/39] qcow2: Update expand_zero_clusters_in_l1() to support L2 slices Eric Blake
2018-01-29 12:06     ` Alberto Garcia
2018-02-01 19:44   ` Max Reitz
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 31/39] qcow2: Update qcow2_truncate() " Alberto Garcia
2018-02-01 19:46   ` Max Reitz
2018-02-02 10:10     ` Alberto Garcia
2018-01-26 14:59 ` [Qemu-devel] [PATCH v3 32/39] qcow2: Rename l2_table in qcow2_alloc_compressed_cluster_offset() Alberto Garcia
2018-02-01 19:48   ` Max Reitz
2018-01-26 15:00 ` [Qemu-devel] [PATCH v3 33/39] qcow2: Rename l2_table in count_contiguous_clusters() Alberto Garcia
2018-02-01 19:49   ` Max Reitz
2018-01-26 15:00 ` [Qemu-devel] [PATCH v3 34/39] qcow2: Rename l2_table in count_contiguous_clusters_unallocated() Alberto Garcia
2018-02-01 19:49   ` Max Reitz
2018-01-26 15:00 ` [Qemu-devel] [PATCH v3 35/39] qcow2: Rename l2_table in count_cow_clusters() Alberto Garcia
2018-02-01 19:50   ` Max Reitz
2018-01-26 15:00 ` [Qemu-devel] [PATCH v3 36/39] qcow2: Allow configuring the L2 slice size Alberto Garcia
2018-01-31 19:20   ` Eric Blake
2018-02-01 20:04   ` Max Reitz
2018-01-26 15:00 ` [Qemu-devel] [PATCH v3 37/39] iotests: Test valid values of l2-cache-entry-size Alberto Garcia
2018-01-31 19:21   ` Eric Blake
2018-02-01 20:05   ` Max Reitz
2018-01-26 15:00 ` [Qemu-devel] [PATCH v3 38/39] iotests: Test downgrading an image using a small L2 slice size Alberto Garcia
2018-01-31 19:23   ` Eric Blake
2018-02-01 20:11   ` Max Reitz
2018-01-26 15:00 ` [Qemu-devel] [PATCH v3 39/39] iotests: Add l2-cache-entry-size to iotest 137 Alberto Garcia
2018-01-31 19:23   ` Eric Blake
2018-02-01 20:12   ` Max Reitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7d8d80bfde60d29e8a0abe04db8241b754714329.1516978645.git.berto@igalia.com \
    --to=berto@igalia.com \
    --cc=anton.nefedov@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=eblake@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=mreitz@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.