{"id":1307022,"url":"http://patchwork.ozlabs.org/api/patches/1307022/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/patch/bca361c51546a1f6d647d127675ac8b7e3b91fcc.1591801197.git.berto@igalia.com/","project":{"id":14,"url":"http://patchwork.ozlabs.org/api/projects/14/?format=json","name":"QEMU Development","link_name":"qemu-devel","list_id":"qemu-devel.nongnu.org","list_email":"qemu-devel@nongnu.org","web_url":"","scm_url":"","webscm_url":"","list_archive_url":"","list_archive_url_format":"","commit_url_format":""},"msgid":"<bca361c51546a1f6d647d127675ac8b7e3b91fcc.1591801197.git.berto@igalia.com>","list_archive_url":null,"date":"2020-06-10T15:02:43","name":"[v8,05/34] qcow2: Process QCOW2_CLUSTER_ZERO_ALLOC clusters in handle_copied()","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"e4a8adc805e0f393ab8425894ee700f21c5fa6a7","submitter":{"id":65704,"url":"http://patchwork.ozlabs.org/api/people/65704/?format=json","name":"Alberto Garcia","email":"berto@igalia.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/qemu-devel/patch/bca361c51546a1f6d647d127675ac8b7e3b91fcc.1591801197.git.berto@igalia.com/mbox/","series":[{"id":182563,"url":"http://patchwork.ozlabs.org/api/series/182563/?format=json","web_url":"http://patchwork.ozlabs.org/project/qemu-devel/list/?series=182563","date":"2020-06-10T15:02:49","name":"Add subcluster allocation to qcow2","version":8,"mbox":"http://patchwork.ozlabs.org/series/182563/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/1307022/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/1307022/checks/","tags":{},"related":[],"headers":{"Return-Path":"<qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>","X-Original-To":"incoming@patchwork.ozlabs.org","Delivered-To":"patchwork-incoming@bilbo.ozlabs.org","Authentication-Results":["ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=nongnu.org\n (client-ip=209.51.188.17; helo=lists.gnu.org;\n envelope-from=qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org;\n receiver=<UNKNOWN>)","ozlabs.org;\n dmarc=none (p=none dis=none) header.from=igalia.com","ozlabs.org;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n unprotected) header.d=igalia.com header.i=@igalia.com header.a=rsa-sha256\n header.s=20170329 header.b=MQAZFZep;\n\tdkim-atps=neutral"],"Received":["from lists.gnu.org (lists.gnu.org [209.51.188.17])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))\n\t(No client certificate requested)\n\tby ozlabs.org (Postfix) with ESMTPS id 49hrJ02n98z9sQx\n\tfor <incoming@patchwork.ozlabs.org>; Thu, 11 Jun 2020 01:19:28 +1000 (AEST)","from localhost ([::1]:39140 helo=lists1p.gnu.org)\n\tby lists.gnu.org with esmtp (Exim 4.90_1)\n\t(envelope-from <qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>)\n\tid 1jj2Vi-0006wa-0R\n\tfor incoming@patchwork.ozlabs.org; Wed, 10 Jun 2020 11:19:26 -0400","from eggs.gnu.org ([2001:470:142:3::10]:36166)\n by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)\n (Exim 4.90_1) (envelope-from <berto@igalia.com>)\n id 1jj2H0-0002nj-2p; Wed, 10 Jun 2020 11:04:14 -0400","from fanzine.igalia.com ([178.60.130.6]:58103)\n by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128)\n (Exim 4.90_1) (envelope-from <berto@igalia.com>)\n id 1jj2Gr-0006w3-Fd; Wed, 10 Jun 2020 11:04:13 -0400","from [81.0.38.199] (helo=perseus.local)\n by fanzine.igalia.com with esmtpsa\n (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim)\n id 1jj2GR-0007gj-Ew; Wed, 10 Jun 2020 17:03:39 +0200","from berto by perseus.local with local (Exim 4.92)\n (envelope-from <berto@igalia.com>)\n id 1jj2GB-0007Mc-Q0; Wed, 10 Jun 2020 17:03:23 +0200"],"DKIM-Signature":"v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com;\n s=20170329;\n h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From;\n bh=lc0SmDD2nR0N4tONgLyUW2dLkalKWUxcTiom3DEcZDk=;\n b=MQAZFZepTxYPuW0QngvscWO0tBD+EzWobnvVeeea+4/OfjhpqN+o7ocFiRy2E8KdH2weSpUm7LuIs3TX2011nEYE80RWYaQvNBss8InuNb/se8IeHPoCPgUDEaT/hB7ym1c6I5koN3Z0wo7RDn+eTMXZ+jT9/T/zL9Luz7X4FlwBMI+qBGUQezBy93bAbzuf/jHTf9LTByB3kqNHXo+swaRR5PZL9+hlkqQ3dkrTutkWO1efBE0mYn5pzR83qODN02IprZlSLkcLyuL2pG0/uo1TEulPOW0epymGfaaBE/LE5vxK/EqEChAcwkjllQfY2vF3RjEknctBs9SYmIi3gw==;","From":"Alberto Garcia <berto@igalia.com>","To":"qemu-devel@nongnu.org","Subject":"[PATCH v8 05/34] qcow2: Process QCOW2_CLUSTER_ZERO_ALLOC clusters in\n handle_copied()","Date":"Wed, 10 Jun 2020 17:02:43 +0200","Message-Id":"\n <bca361c51546a1f6d647d127675ac8b7e3b91fcc.1591801197.git.berto@igalia.com>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<cover.1591801197.git.berto@igalia.com>","References":"<cover.1591801197.git.berto@igalia.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Received-SPF":"pass client-ip=178.60.130.6; envelope-from=berto@igalia.com;\n helo=fanzine.igalia.com","X-detected-operating-system":"by eggs.gnu.org: First seen = 2020/06/10 11:03:39","X-ACL-Warn":"Detected OS   = Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy]","X-Spam_score_int":"-20","X-Spam_score":"-2.1","X-Spam_bar":"--","X-Spam_report":"(-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1,\n DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, SPF_HELO_NONE=0.001,\n SPF_PASS=-0.001 autolearn=_AUTOLEARN","X-Spam_action":"no action","X-BeenThere":"qemu-devel@nongnu.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<qemu-devel.nongnu.org>","List-Unsubscribe":"<https://lists.nongnu.org/mailman/options/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>","List-Archive":"<https://lists.nongnu.org/archive/html/qemu-devel>","List-Post":"<mailto:qemu-devel@nongnu.org>","List-Help":"<mailto:qemu-devel-request@nongnu.org?subject=help>","List-Subscribe":"<https://lists.nongnu.org/mailman/listinfo/qemu-devel>,\n <mailto:qemu-devel-request@nongnu.org?subject=subscribe>","Cc":"Kevin Wolf <kwolf@redhat.com>,\n Vladimir Sementsov-Ogievskiy <vsementsov@virtuozzo.com>,\n Alberto Garcia <berto@igalia.com>, qemu-block@nongnu.org,\n Derek Su <dereksu@qnap.com>, Max Reitz <mreitz@redhat.com>","Errors-To":"qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org","Sender":"\"Qemu-devel\"\n <qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org>"},"content":"When writing to a qcow2 file there are two functions that take a\nvirtual offset and return a host offset, possibly allocating new\nclusters if necessary:\n\n   - handle_copied() looks for normal data clusters that are already\n     allocated and have a reference count of 1. In those clusters we\n     can simply write the data and there is no need to perform any\n     copy-on-write.\n\n   - handle_alloc() looks for clusters that do need copy-on-write,\n     either because they haven't been allocated yet, because their\n     reference count is != 1 or because they are ZERO_ALLOC clusters.\n\nThe ZERO_ALLOC case is a bit special because those are clusters that\nare already allocated and they could perfectly be dealt with in\nhandle_copied() (as long as copy-on-write is performed when required).\n\nIn fact, there is extra code specifically for them in handle_alloc()\nthat tries to reuse the existing allocation if possible and frees them\notherwise.\n\nThis patch changes the handling of ZERO_ALLOC clusters so the\nsemantics of these two functions are now like this:\n\n   - handle_copied() looks for clusters that are already allocated and\n     which we can overwrite (NORMAL and ZERO_ALLOC clusters with a\n     reference count of 1).\n\n   - handle_alloc() looks for clusters for which we need a new\n     allocation (all other cases).\n\nOne important difference after this change is that clusters found\nin handle_copied() may now require copy-on-write, but this will be\nnecessary anyway once we add support for subclusters.\n\nSigned-off-by: Alberto Garcia <berto@igalia.com>\nReviewed-by: Eric Blake <eblake@redhat.com>\n---\n block/qcow2-cluster.c | 256 +++++++++++++++++++++++-------------------\n 1 file changed, 141 insertions(+), 115 deletions(-)","diff":"diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c\nindex 80f9787461..fce0be7a08 100644\n--- a/block/qcow2-cluster.c\n+++ b/block/qcow2-cluster.c\n@@ -1039,13 +1039,18 @@ void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)\n \n /*\n  * For a given write request, create a new QCowL2Meta structure, add\n- * it to @m and the BDRVQcow2State.cluster_allocs list.\n+ * it to @m and the BDRVQcow2State.cluster_allocs list. If the write\n+ * request does not need copy-on-write or changes to the L2 metadata\n+ * then this function does nothing.\n  *\n  * @host_cluster_offset points to the beginning of the first cluster.\n  *\n  * @guest_offset and @bytes indicate the offset and length of the\n  * request.\n  *\n+ * @l2_slice contains the L2 entries of all clusters involved in this\n+ * write request.\n+ *\n  * If @keep_old is true it means that the clusters were already\n  * allocated and will be overwritten. If false then the clusters are\n  * new and we have to decrease the reference count of the old ones.\n@@ -1053,15 +1058,53 @@ void qcow2_alloc_cluster_abort(BlockDriverState *bs, QCowL2Meta *m)\n static void calculate_l2_meta(BlockDriverState *bs,\n                               uint64_t host_cluster_offset,\n                               uint64_t guest_offset, unsigned bytes,\n-                              QCowL2Meta **m, bool keep_old)\n+                              uint64_t *l2_slice, QCowL2Meta **m, bool keep_old)\n {\n     BDRVQcow2State *s = bs->opaque;\n-    unsigned cow_start_from = 0;\n+    int l2_index = offset_to_l2_slice_index(s, guest_offset);\n+    uint64_t l2_entry;\n+    unsigned cow_start_from, cow_end_to;\n     unsigned cow_start_to = offset_into_cluster(s, guest_offset);\n     unsigned cow_end_from = cow_start_to + bytes;\n-    unsigned cow_end_to = ROUND_UP(cow_end_from, s->cluster_size);\n     unsigned nb_clusters = size_to_clusters(s, cow_end_from);\n     QCowL2Meta *old_m = *m;\n+    QCow2ClusterType type;\n+\n+    assert(nb_clusters <= s->l2_slice_size - l2_index);\n+\n+    /* Return if there's no COW (all clusters are normal and we keep them) */\n+    if (keep_old) {\n+        int i;\n+        for (i = 0; i < nb_clusters; i++) {\n+            l2_entry = be64_to_cpu(l2_slice[l2_index + i]);\n+            if (qcow2_get_cluster_type(bs, l2_entry) != QCOW2_CLUSTER_NORMAL) {\n+                break;\n+            }\n+        }\n+        if (i == nb_clusters) {\n+            return;\n+        }\n+    }\n+\n+    /* Get the L2 entry of the first cluster */\n+    l2_entry = be64_to_cpu(l2_slice[l2_index]);\n+    type = qcow2_get_cluster_type(bs, l2_entry);\n+\n+    if (type == QCOW2_CLUSTER_NORMAL && keep_old) {\n+        cow_start_from = cow_start_to;\n+    } else {\n+        cow_start_from = 0;\n+    }\n+\n+    /* Get the L2 entry of the last cluster */\n+    l2_entry = be64_to_cpu(l2_slice[l2_index + nb_clusters - 1]);\n+    type = qcow2_get_cluster_type(bs, l2_entry);\n+\n+    if (type == QCOW2_CLUSTER_NORMAL && keep_old) {\n+        cow_end_to = cow_end_from;\n+    } else {\n+        cow_end_to = ROUND_UP(cow_end_from, s->cluster_size);\n+    }\n \n     *m = g_malloc0(sizeof(**m));\n     **m = (QCowL2Meta) {\n@@ -1087,18 +1130,22 @@ static void calculate_l2_meta(BlockDriverState *bs,\n     QLIST_INSERT_HEAD(&s->cluster_allocs, *m, next_in_flight);\n }\n \n-/* Returns true if writing to a cluster requires COW */\n-static bool cluster_needs_cow(BlockDriverState *bs, uint64_t l2_entry)\n+/*\n+ * Returns true if writing to the cluster pointed to by @l2_entry\n+ * requires a new allocation (that is, if the cluster is unallocated\n+ * or has refcount > 1 and therefore cannot be written in-place).\n+ */\n+static bool cluster_needs_new_alloc(BlockDriverState *bs, uint64_t l2_entry)\n {\n     switch (qcow2_get_cluster_type(bs, l2_entry)) {\n     case QCOW2_CLUSTER_NORMAL:\n+    case QCOW2_CLUSTER_ZERO_ALLOC:\n         if (l2_entry & QCOW_OFLAG_COPIED) {\n             return false;\n         }\n     case QCOW2_CLUSTER_UNALLOCATED:\n     case QCOW2_CLUSTER_COMPRESSED:\n     case QCOW2_CLUSTER_ZERO_PLAIN:\n-    case QCOW2_CLUSTER_ZERO_ALLOC:\n         return true;\n     default:\n         abort();\n@@ -1106,20 +1153,38 @@ static bool cluster_needs_cow(BlockDriverState *bs, uint64_t l2_entry)\n }\n \n /*\n- * Returns the number of contiguous clusters that can be used for an allocating\n- * write, but require COW to be performed (this includes yet unallocated space,\n- * which must copy from the backing file)\n+ * Returns the number of contiguous clusters that can be written to\n+ * using one single write request, starting from @l2_index.\n+ * At most @nb_clusters are checked.\n+ *\n+ * If @new_alloc is true this counts clusters that are either\n+ * unallocated, or allocated but with refcount > 1 (so they need to be\n+ * newly allocated and COWed).\n+ *\n+ * If @new_alloc is false this counts clusters that are already\n+ * allocated and can be overwritten in-place (this includes clusters\n+ * of type QCOW2_CLUSTER_ZERO_ALLOC).\n  */\n-static int count_cow_clusters(BlockDriverState *bs, int nb_clusters,\n-    uint64_t *l2_slice, int l2_index)\n+static int count_single_write_clusters(BlockDriverState *bs, int nb_clusters,\n+                                       uint64_t *l2_slice, int l2_index,\n+                                       bool new_alloc)\n {\n+    BDRVQcow2State *s = bs->opaque;\n+    uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index]);\n+    uint64_t expected_offset = l2_entry & L2E_OFFSET_MASK;\n     int i;\n \n     for (i = 0; i < nb_clusters; i++) {\n-        uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index + i]);\n-        if (!cluster_needs_cow(bs, l2_entry)) {\n+        l2_entry = be64_to_cpu(l2_slice[l2_index + i]);\n+        if (cluster_needs_new_alloc(bs, l2_entry) != new_alloc) {\n             break;\n         }\n+        if (!new_alloc) {\n+            if (expected_offset != (l2_entry & L2E_OFFSET_MASK)) {\n+                break;\n+            }\n+            expected_offset += s->cluster_size;\n+        }\n     }\n \n     assert(i <= nb_clusters);\n@@ -1190,10 +1255,10 @@ static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,\n }\n \n /*\n- * Checks how many already allocated clusters that don't require a copy on\n- * write there are at the given guest_offset (up to *bytes). If *host_offset is\n- * not INV_OFFSET, only physically contiguous clusters beginning at this host\n- * offset are counted.\n+ * Checks how many already allocated clusters that don't require a new\n+ * allocation there are at the given guest_offset (up to *bytes).\n+ * If *host_offset is not INV_OFFSET, only physically contiguous clusters\n+ * beginning at this host offset are counted.\n  *\n  * Note that guest_offset may not be cluster aligned. In this case, the\n  * returned *host_offset points to exact byte referenced by guest_offset and\n@@ -1202,12 +1267,12 @@ static int handle_dependencies(BlockDriverState *bs, uint64_t guest_offset,\n  * Returns:\n  *   0:     if no allocated clusters are available at the given offset.\n  *          *bytes is normally unchanged. It is set to 0 if the cluster\n- *          is allocated and doesn't need COW, but doesn't have the right\n- *          physical offset.\n+ *          is allocated and can be overwritten in-place but doesn't have\n+ *          the right physical offset.\n  *\n- *   1:     if allocated clusters that don't require a COW are available at\n- *          the requested offset. *bytes may have decreased and describes\n- *          the length of the area that can be written to.\n+ *   1:     if allocated clusters that can be overwritten in place are\n+ *          available at the requested offset. *bytes may have decreased\n+ *          and describes the length of the area that can be written to.\n  *\n  *  -errno: in error cases\n  */\n@@ -1216,7 +1281,7 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,\n {\n     BDRVQcow2State *s = bs->opaque;\n     int l2_index;\n-    uint64_t cluster_offset;\n+    uint64_t l2_entry, cluster_offset;\n     uint64_t *l2_slice;\n     uint64_t nb_clusters;\n     unsigned int keep_clusters;\n@@ -1237,7 +1302,8 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,\n \n     l2_index = offset_to_l2_slice_index(s, guest_offset);\n     nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);\n-    assert(nb_clusters <= INT_MAX);\n+    /* Limit total byte count to BDRV_REQUEST_MAX_BYTES */\n+    nb_clusters = MIN(nb_clusters, BDRV_REQUEST_MAX_BYTES >> s->cluster_bits);\n \n     /* Find L2 entry for the first involved cluster */\n     ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);\n@@ -1245,41 +1311,39 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset,\n         return ret;\n     }\n \n-    cluster_offset = be64_to_cpu(l2_slice[l2_index]);\n+    l2_entry = be64_to_cpu(l2_slice[l2_index]);\n+    cluster_offset = l2_entry & L2E_OFFSET_MASK;\n+\n+    if (!cluster_needs_new_alloc(bs, l2_entry)) {\n+        if (offset_into_cluster(s, cluster_offset)) {\n+            qcow2_signal_corruption(bs, true, -1, -1, \"%s cluster offset \"\n+                                    \"%#\" PRIx64 \" unaligned (guest offset: %#\"\n+                                    PRIx64 \")\", l2_entry & QCOW_OFLAG_ZERO ?\n+                                    \"Preallocated zero\" : \"Data\",\n+                                    cluster_offset, guest_offset);\n+            ret = -EIO;\n+            goto out;\n+        }\n \n-    /* Check how many clusters are already allocated and don't need COW */\n-    if (qcow2_get_cluster_type(bs, cluster_offset) == QCOW2_CLUSTER_NORMAL\n-        && (cluster_offset & QCOW_OFLAG_COPIED))\n-    {\n         /* If a specific host_offset is required, check it */\n-        bool offset_matches =\n-            (cluster_offset & L2E_OFFSET_MASK) == *host_offset;\n-\n-        if (offset_into_cluster(s, cluster_offset & L2E_OFFSET_MASK)) {\n-            qcow2_signal_corruption(bs, true, -1, -1, \"Data cluster offset \"\n-                                    \"%#llx unaligned (guest offset: %#\" PRIx64\n-                                    \")\", cluster_offset & L2E_OFFSET_MASK,\n-                                    guest_offset);\n-            ret = -EIO;\n-            goto out;\n-        }\n-\n-        if (*host_offset != INV_OFFSET && !offset_matches) {\n+        if (*host_offset != INV_OFFSET && cluster_offset != *host_offset) {\n             *bytes = 0;\n             ret = 0;\n             goto out;\n         }\n \n         /* We keep all QCOW_OFLAG_COPIED clusters */\n-        keep_clusters =\n-            count_contiguous_clusters(bs, nb_clusters, s->cluster_size,\n-                                      &l2_slice[l2_index],\n-                                      QCOW_OFLAG_COPIED | QCOW_OFLAG_ZERO);\n+        keep_clusters = count_single_write_clusters(bs, nb_clusters, l2_slice,\n+                                                    l2_index, false);\n         assert(keep_clusters <= nb_clusters);\n \n         *bytes = MIN(*bytes,\n                  keep_clusters * s->cluster_size\n                  - offset_into_cluster(s, guest_offset));\n+        assert(*bytes != 0);\n+\n+        calculate_l2_meta(bs, cluster_offset, guest_offset,\n+                          *bytes, l2_slice, m, true);\n \n         ret = 1;\n     } else {\n@@ -1293,8 +1357,7 @@ out:\n     /* Only return a host offset if we actually made progress. Otherwise we\n      * would make requirements for handle_alloc() that it can't fulfill */\n     if (ret > 0) {\n-        *host_offset = (cluster_offset & L2E_OFFSET_MASK)\n-                     + offset_into_cluster(s, guest_offset);\n+        *host_offset = cluster_offset + offset_into_cluster(s, guest_offset);\n     }\n \n     return ret;\n@@ -1355,9 +1418,10 @@ static int do_alloc_cluster_offset(BlockDriverState *bs, uint64_t guest_offset,\n }\n \n /*\n- * Allocates new clusters for an area that either is yet unallocated or needs a\n- * copy on write. If *host_offset is not INV_OFFSET, clusters are only\n- * allocated if the new allocation can match the specified host offset.\n+ * Allocates new clusters for an area that is either still unallocated or\n+ * cannot be overwritten in-place. If *host_offset is not INV_OFFSET,\n+ * clusters are only allocated if the new allocation can match the specified\n+ * host offset.\n  *\n  * Note that guest_offset may not be cluster aligned. In this case, the\n  * returned *host_offset points to exact byte referenced by guest_offset and\n@@ -1380,12 +1444,10 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,\n     BDRVQcow2State *s = bs->opaque;\n     int l2_index;\n     uint64_t *l2_slice;\n-    uint64_t entry;\n     uint64_t nb_clusters;\n     int ret;\n-    bool keep_old_clusters = false;\n \n-    uint64_t alloc_cluster_offset = INV_OFFSET;\n+    uint64_t alloc_cluster_offset;\n \n     trace_qcow2_handle_alloc(qemu_coroutine_self(), guest_offset, *host_offset,\n                              *bytes);\n@@ -1400,10 +1462,8 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,\n \n     l2_index = offset_to_l2_slice_index(s, guest_offset);\n     nb_clusters = MIN(nb_clusters, s->l2_slice_size - l2_index);\n-    assert(nb_clusters <= INT_MAX);\n-\n-    /* Limit total allocation byte count to INT_MAX */\n-    nb_clusters = MIN(nb_clusters, INT_MAX >> s->cluster_bits);\n+    /* Limit total allocation byte count to BDRV_REQUEST_MAX_BYTES */\n+    nb_clusters = MIN(nb_clusters, BDRV_REQUEST_MAX_BYTES >> s->cluster_bits);\n \n     /* Find L2 entry for the first involved cluster */\n     ret = get_cluster_table(bs, guest_offset, &l2_slice, &l2_index);\n@@ -1411,67 +1471,32 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,\n         return ret;\n     }\n \n-    entry = be64_to_cpu(l2_slice[l2_index]);\n-    nb_clusters = count_cow_clusters(bs, nb_clusters, l2_slice, l2_index);\n+    nb_clusters = count_single_write_clusters(bs, nb_clusters,\n+                                              l2_slice, l2_index, true);\n \n     /* This function is only called when there were no non-COW clusters, so if\n      * we can't find any unallocated or COW clusters either, something is\n      * wrong with our code. */\n     assert(nb_clusters > 0);\n \n-    if (qcow2_get_cluster_type(bs, entry) == QCOW2_CLUSTER_ZERO_ALLOC &&\n-        (entry & QCOW_OFLAG_COPIED) &&\n-        (*host_offset == INV_OFFSET ||\n-         start_of_cluster(s, *host_offset) == (entry & L2E_OFFSET_MASK)))\n-    {\n-        int preallocated_nb_clusters;\n-\n-        if (offset_into_cluster(s, entry & L2E_OFFSET_MASK)) {\n-            qcow2_signal_corruption(bs, true, -1, -1, \"Preallocated zero \"\n-                                    \"cluster offset %#llx unaligned (guest \"\n-                                    \"offset: %#\" PRIx64 \")\",\n-                                    entry & L2E_OFFSET_MASK, guest_offset);\n-            ret = -EIO;\n-            goto fail;\n-        }\n-\n-        /* Try to reuse preallocated zero clusters; contiguous normal clusters\n-         * would be fine, too, but count_cow_clusters() above has limited\n-         * nb_clusters already to a range of COW clusters */\n-        preallocated_nb_clusters =\n-            count_contiguous_clusters(bs, nb_clusters, s->cluster_size,\n-                                      &l2_slice[l2_index], QCOW_OFLAG_COPIED);\n-        assert(preallocated_nb_clusters > 0);\n-\n-        nb_clusters = preallocated_nb_clusters;\n-        alloc_cluster_offset = entry & L2E_OFFSET_MASK;\n-\n-        /* We want to reuse these clusters, so qcow2_alloc_cluster_link_l2()\n-         * should not free them. */\n-        keep_old_clusters = true;\n+    /* Allocate at a given offset in the image file */\n+    alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :\n+        start_of_cluster(s, *host_offset);\n+    ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,\n+                                  &nb_clusters);\n+    if (ret < 0) {\n+        goto out;\n     }\n \n-    qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);\n-\n-    if (alloc_cluster_offset == INV_OFFSET) {\n-        /* Allocate, if necessary at a given offset in the image file */\n-        alloc_cluster_offset = *host_offset == INV_OFFSET ? INV_OFFSET :\n-                               start_of_cluster(s, *host_offset);\n-        ret = do_alloc_cluster_offset(bs, guest_offset, &alloc_cluster_offset,\n-                                      &nb_clusters);\n-        if (ret < 0) {\n-            goto fail;\n-        }\n-\n-        /* Can't extend contiguous allocation */\n-        if (nb_clusters == 0) {\n-            *bytes = 0;\n-            return 0;\n-        }\n-\n-        assert(alloc_cluster_offset != INV_OFFSET);\n+    /* Can't extend contiguous allocation */\n+    if (nb_clusters == 0) {\n+        *bytes = 0;\n+        ret = 0;\n+        goto out;\n     }\n \n+    assert(alloc_cluster_offset != INV_OFFSET);\n+\n     /*\n      * Save info needed for meta data update.\n      *\n@@ -1494,13 +1519,14 @@ static int handle_alloc(BlockDriverState *bs, uint64_t guest_offset,\n     *bytes = MIN(*bytes, nb_bytes - offset_into_cluster(s, guest_offset));\n     assert(*bytes != 0);\n \n-    calculate_l2_meta(bs, alloc_cluster_offset, guest_offset, *bytes,\n-                      m, keep_old_clusters);\n+    calculate_l2_meta(bs, alloc_cluster_offset, guest_offset, *bytes, l2_slice,\n+                      m, false);\n \n-    return 1;\n+    ret = 1;\n \n-fail:\n-    if (*m && (*m)->nb_clusters > 0) {\n+out:\n+    qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice);\n+    if (ret < 0 && *m && (*m)->nb_clusters > 0) {\n         QLIST_REMOVE(*m, next_in_flight);\n     }\n     return ret;\n","prefixes":["v8","05/34"]}