From patchwork Sun Dec 22 11:36:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alberto Garcia X-Patchwork-Id: 11307393 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 3A9F413B6 for ; Sun, 22 Dec 2019 11:54:01 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id F358720643 for ; Sun, 22 Dec 2019 11:54:00 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=igalia.com header.i=@igalia.com header.b="gzdAXrK5" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F358720643 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=igalia.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Received: from localhost ([::1]:46904 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iizo7-0002rN-RE for patchwork-qemu-devel@patchwork.kernel.org; Sun, 22 Dec 2019 06:53:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]:44469) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iizZF-0003YM-Vo for qemu-devel@nongnu.org; Sun, 22 Dec 2019 06:38:40 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iizZC-00077I-VJ for qemu-devel@nongnu.org; Sun, 22 Dec 2019 06:38:37 -0500 Received: from fanzine.igalia.com ([178.60.130.6]:35055) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iizZB-0004oi-JR; Sun, 22 Dec 2019 06:38:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From; bh=QRXi6MJ1Q6YuXaj80jS4cUlvS90lVmbKyazl1lEuJc0=; b=gzdAXrK5+bPE+pMGoesXXgaOFyTcxlskCXQ9yD/y757VC7sevp/moZXw2wgHcSLw7731brfPMawqUzS3unNEFRPIq4Tiqjo2gEQZ5olLF2VYTrDv7Q7P29jMqjzkH/ygpmqBaNG5il0qfzJ5+TQxnCdrG4EGixtuns2N6ElDZxx0GOSeHTo7EYOCW6tBKkSxqWpHGGsEC6C8wEbpb2kfsLpDRB25HV2XhYx3tmA0sNSSsZWob2d0sMh5dV15kKgWKHkpQ0+iLVgvZ/44/TQ23PjHiRhJbDrtoV084Eps941HPFm6Vs7q7+lNXu36UIQe74T+dXKQF3g7+jz4bukv9g==; Received: from [80.30.182.172] (helo=perseus.local) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1iizYU-0005d6-GM; Sun, 22 Dec 2019 12:37:52 +0100 Received: from berto by perseus.local with local (Exim 4.92) (envelope-from ) id 1iizXu-0001Va-Aw; Sun, 22 Dec 2019 12:37:14 +0100 From: Alberto Garcia To: qemu-devel@nongnu.org Subject: [RFC PATCH v3 04/27] qcow2: Add get_l2_entry() and set_l2_entry() Date: Sun, 22 Dec 2019 12:36:45 +0100 Message-Id: X-Mailer: git-send-email 2.20.1 In-Reply-To: References: MIME-Version: 1.0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Anton Nefedov , Alberto Garcia , qemu-block@nongnu.org, Max Reitz , Vladimir Sementsov-Ogievskiy , "Denis V . Lunev" Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" The size of an L2 entry is 64 bits, but if we want to have subclusters we need extended L2 entries. This means that we have to access L2 tables and slices differently depending on whether an image has extended L2 entries or not. This patch replaces all l2_slice[] accesses with calls to get_l2_entry() and set_l2_entry(). Signed-off-by: Alberto Garcia Reviewed-by: Eric Blake Reviewed-by: Max Reitz --- block/qcow2-cluster.c | 65 ++++++++++++++++++++++-------------------- block/qcow2-refcount.c | 17 +++++------ block/qcow2.h | 12 ++++++++ 3 files changed, 55 insertions(+), 39 deletions(-) diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index 9387f15866..683c9569ad 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -379,12 +379,13 @@ fail: * cluster which may require a different handling) */ static int count_contiguous_clusters(BlockDriverState *bs, int nb_clusters, - int cluster_size, uint64_t *l2_slice, uint64_t stop_flags) + int cluster_size, uint64_t *l2_slice, int l2_index, uint64_t stop_flags) { + BDRVQcow2State *s = bs->opaque; int i; QCow2ClusterType first_cluster_type; uint64_t mask = stop_flags | L2E_OFFSET_MASK | QCOW_OFLAG_COMPRESSED; - uint64_t first_entry = be64_to_cpu(l2_slice[0]); + uint64_t first_entry = get_l2_entry(s, l2_slice, l2_index); uint64_t offset = first_entry & mask; first_cluster_type = qcow2_get_cluster_type(bs, first_entry); @@ -397,7 +398,7 @@ static int count_contiguous_clusters(BlockDriverState *bs, int nb_clusters, first_cluster_type == QCOW2_CLUSTER_ZERO_ALLOC); for (i = 0; i < nb_clusters; i++) { - uint64_t l2_entry = be64_to_cpu(l2_slice[i]) & mask; + uint64_t l2_entry = get_l2_entry(s, l2_slice, l2_index + i) & mask; if (offset + (uint64_t) i * cluster_size != l2_entry) { break; } @@ -413,14 +414,16 @@ static int count_contiguous_clusters(BlockDriverState *bs, int nb_clusters, static int count_contiguous_clusters_unallocated(BlockDriverState *bs, int nb_clusters, uint64_t *l2_slice, + int l2_index, QCow2ClusterType wanted_type) { + BDRVQcow2State *s = bs->opaque; int i; assert(wanted_type == QCOW2_CLUSTER_ZERO_PLAIN || wanted_type == QCOW2_CLUSTER_UNALLOCATED); for (i = 0; i < nb_clusters; i++) { - uint64_t entry = be64_to_cpu(l2_slice[i]); + uint64_t entry = get_l2_entry(s, l2_slice, l2_index + i); QCow2ClusterType type = qcow2_get_cluster_type(bs, entry); if (type != wanted_type) { @@ -566,7 +569,7 @@ int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, /* find the cluster offset for the given disk offset */ l2_index = offset_to_l2_slice_index(s, offset); - *cluster_offset = be64_to_cpu(l2_slice[l2_index]); + *cluster_offset = get_l2_entry(s, l2_slice, l2_index); nb_clusters = size_to_clusters(s, bytes_needed); /* bytes_needed <= *bytes + offset_in_cluster, both of which are unsigned @@ -601,14 +604,14 @@ int qcow2_get_cluster_offset(BlockDriverState *bs, uint64_t offset, case QCOW2_CLUSTER_UNALLOCATED: /* how many empty clusters ? */ c = count_contiguous_clusters_unallocated(bs, nb_clusters, - &l2_slice[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(bs, nb_clusters, s->cluster_size, - &l2_slice[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, @@ -761,7 +764,7 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, /* Compression can't overwrite anything. Fail if the cluster was already * allocated. */ - cluster_offset = be64_to_cpu(l2_slice[l2_index]); + cluster_offset = get_l2_entry(s, l2_slice, l2_index); if (cluster_offset & L2E_OFFSET_MASK) { qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice); return -EIO; @@ -786,7 +789,7 @@ int qcow2_alloc_compressed_cluster_offset(BlockDriverState *bs, BLKDBG_EVENT(bs->file, BLKDBG_L2_UPDATE_COMPRESSED); qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice); - l2_slice[l2_index] = cpu_to_be64(cluster_offset); + set_l2_entry(s, l2_slice, l2_index, cluster_offset); qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice); *host_offset = cluster_offset & s->cluster_offset_mask; @@ -978,12 +981,12 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) * cluster the second one has to do RMW (which is done above by * perform_cow()), update l2 table with its cluster pointer and free * old cluster. This is what this loop does */ - if (l2_slice[l2_index + i] != 0) { - old_cluster[j++] = l2_slice[l2_index + i]; + if (get_l2_entry(s, l2_slice, l2_index + i) != 0) { + old_cluster[j++] = get_l2_entry(s, l2_slice, l2_index + i); } - l2_slice[l2_index + i] = cpu_to_be64((cluster_offset + - (i << s->cluster_bits)) | QCOW_OFLAG_COPIED); + set_l2_entry(s, l2_slice, l2_index + i, QCOW_OFLAG_COPIED | + (cluster_offset + (i << s->cluster_bits))); } @@ -997,8 +1000,7 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, QCowL2Meta *m) */ if (!m->keep_old_clusters && j != 0) { for (i = 0; i < j; i++) { - qcow2_free_any_clusters(bs, be64_to_cpu(old_cluster[i]), 1, - QCOW2_DISCARD_NEVER); + qcow2_free_any_clusters(bs, old_cluster[i], 1, QCOW2_DISCARD_NEVER); } } @@ -1058,7 +1060,7 @@ static void calculate_l2_meta(BlockDriverState *bs, if (keep_old) { int i; for (i = 0; i < nb_clusters; i++) { - l2_entry = be64_to_cpu(l2_slice[l2_index + i]); + l2_entry = get_l2_entry(s, l2_slice, l2_index + i); if (qcow2_get_cluster_type(bs, l2_entry) != QCOW2_CLUSTER_NORMAL) { break; } @@ -1069,7 +1071,7 @@ static void calculate_l2_meta(BlockDriverState *bs, } /* Get the L2 entry from the first cluster */ - l2_entry = be64_to_cpu(l2_slice[l2_index]); + l2_entry = get_l2_entry(s, l2_slice, l2_index); type = qcow2_get_cluster_type(bs, l2_entry); if (type == QCOW2_CLUSTER_NORMAL && keep_old) { @@ -1079,7 +1081,7 @@ static void calculate_l2_meta(BlockDriverState *bs, } /* Get the L2 entry from the last cluster */ - l2_entry = be64_to_cpu(l2_slice[l2_index + nb_clusters - 1]); + l2_entry = get_l2_entry(s, l2_slice, l2_index + nb_clusters - 1); type = qcow2_get_cluster_type(bs, l2_entry); if (type == QCOW2_CLUSTER_NORMAL && keep_old) { @@ -1150,12 +1152,12 @@ static int count_single_write_clusters(BlockDriverState *bs, int nb_clusters, bool new_alloc) { BDRVQcow2State *s = bs->opaque; - uint64_t l2_entry = be64_to_cpu(l2_slice[l2_index]); + uint64_t l2_entry = get_l2_entry(s, l2_slice, l2_index); uint64_t expected_offset = l2_entry & L2E_OFFSET_MASK; int i; for (i = 0; i < nb_clusters; i++) { - l2_entry = be64_to_cpu(l2_slice[l2_index + i]); + l2_entry = get_l2_entry(s, l2_slice, l2_index + i); if (cluster_needs_new_alloc(bs, l2_entry) != new_alloc) { break; } @@ -1289,7 +1291,7 @@ static int handle_copied(BlockDriverState *bs, uint64_t guest_offset, return ret; } - cluster_offset = be64_to_cpu(l2_slice[l2_index]); + cluster_offset = get_l2_entry(s, l2_slice, l2_index); if (!cluster_needs_new_alloc(bs, cluster_offset)) { /* If a specific host_offset is required, check it */ @@ -1670,7 +1672,7 @@ static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset, for (i = 0; i < nb_clusters; i++) { uint64_t old_l2_entry; - old_l2_entry = be64_to_cpu(l2_slice[l2_index + i]); + old_l2_entry = get_l2_entry(s, l2_slice, l2_index + i); /* * If full_discard is false, make sure that a discarded area reads back @@ -1710,9 +1712,9 @@ static int discard_in_l2_slice(BlockDriverState *bs, uint64_t offset, /* First remove L2 entries */ qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice); if (!full_discard && s->qcow_version >= 3) { - l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO); + set_l2_entry(s, l2_slice, l2_index + i, QCOW_OFLAG_ZERO); } else { - l2_slice[l2_index + i] = cpu_to_be64(0); + set_l2_entry(s, l2_slice, l2_index + i, 0); } /* Then decrease the refcount */ @@ -1792,7 +1794,7 @@ static int zero_in_l2_slice(BlockDriverState *bs, uint64_t offset, uint64_t old_offset; QCow2ClusterType cluster_type; - old_offset = be64_to_cpu(l2_slice[l2_index + i]); + old_offset = get_l2_entry(s, l2_slice, l2_index + i); /* * Minimize L2 changes if the cluster already reads back as @@ -1806,10 +1808,11 @@ static int zero_in_l2_slice(BlockDriverState *bs, uint64_t offset, qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice); if (cluster_type == QCOW2_CLUSTER_COMPRESSED || unmap) { - l2_slice[l2_index + i] = cpu_to_be64(QCOW_OFLAG_ZERO); + set_l2_entry(s, l2_slice, l2_index + i, QCOW_OFLAG_ZERO); qcow2_free_any_clusters(bs, old_offset, 1, QCOW2_DISCARD_REQUEST); } else { - l2_slice[l2_index + i] |= cpu_to_be64(QCOW_OFLAG_ZERO); + uint64_t entry = get_l2_entry(s, l2_slice, l2_index + i); + set_l2_entry(s, l2_slice, l2_index + i, entry | QCOW_OFLAG_ZERO); } } @@ -1947,7 +1950,7 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, } for (j = 0; j < s->l2_slice_size; j++) { - uint64_t l2_entry = be64_to_cpu(l2_slice[j]); + uint64_t l2_entry = get_l2_entry(s, l2_slice, j); int64_t offset = l2_entry & L2E_OFFSET_MASK; QCow2ClusterType cluster_type = qcow2_get_cluster_type(bs, l2_entry); @@ -1961,7 +1964,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_slice[j] = 0; + set_l2_entry(s, l2_slice, j, 0); l2_dirty = true; continue; } @@ -2024,9 +2027,9 @@ static int expand_zero_clusters_in_l1(BlockDriverState *bs, uint64_t *l1_table, } if (l2_refcount == 1) { - l2_slice[j] = cpu_to_be64(offset | QCOW_OFLAG_COPIED); + set_l2_entry(s, l2_slice, j, offset | QCOW_OFLAG_COPIED); } else { - l2_slice[j] = cpu_to_be64(offset); + set_l2_entry(s, l2_slice, j, offset); } l2_dirty = true; } diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index f67ac6b2d8..223048569e 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1309,7 +1309,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, uint64_t cluster_index; uint64_t offset; - entry = be64_to_cpu(l2_slice[j]); + entry = get_l2_entry(s, l2_slice, j); old_entry = entry; entry &= ~QCOW_OFLAG_COPIED; offset = entry & L2E_OFFSET_MASK; @@ -1383,7 +1383,7 @@ int qcow2_update_snapshot_refcount(BlockDriverState *bs, qcow2_cache_set_dependency(bs, s->l2_table_cache, s->refcount_block_cache); } - l2_slice[j] = cpu_to_be64(entry); + set_l2_entry(s, l2_slice, j, entry); qcow2_cache_entry_mark_dirty(s->l2_table_cache, l2_slice); } @@ -1616,7 +1616,7 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, /* Do the actual checks */ for(i = 0; i < s->l2_size; i++) { - l2_entry = be64_to_cpu(l2_table[i]); + l2_entry = get_l2_entry(s, l2_table, i); switch (qcow2_get_cluster_type(bs, l2_entry)) { case QCOW2_CLUSTER_COMPRESSED: @@ -1685,7 +1685,7 @@ static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, QCOW2_OL_INACTIVE_L2; l2_entry = QCOW_OFLAG_ZERO; - l2_table[i] = cpu_to_be64(l2_entry); + set_l2_entry(s, l2_table, i, l2_entry); ret = qcow2_pre_write_overlap_check(bs, ign, l2e_offset, sizeof(uint64_t), false); if (ret < 0) { @@ -1913,7 +1913,7 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res, } for (j = 0; j < s->l2_size; j++) { - uint64_t l2_entry = be64_to_cpu(l2_table[j]); + uint64_t l2_entry = get_l2_entry(s, l2_table, j); uint64_t data_offset = l2_entry & L2E_OFFSET_MASK; QCow2ClusterType cluster_type = qcow2_get_cluster_type(bs, l2_entry); @@ -1936,9 +1936,10 @@ static int check_oflag_copied(BlockDriverState *bs, BdrvCheckResult *res, "l2_entry=%" PRIx64 " refcount=%" PRIu64 "\n", repair ? "Repairing" : "ERROR", l2_entry, refcount); if (repair) { - l2_table[j] = cpu_to_be64(refcount == 1 - ? l2_entry | QCOW_OFLAG_COPIED - : l2_entry & ~QCOW_OFLAG_COPIED); + set_l2_entry(s, l2_table, j, + refcount == 1 ? + l2_entry | QCOW_OFLAG_COPIED : + l2_entry & ~QCOW_OFLAG_COPIED); l2_dirty++; } } diff --git a/block/qcow2.h b/block/qcow2.h index 0942126232..6823d3f68f 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -495,6 +495,18 @@ typedef enum QCow2MetadataOverlap { #define INV_OFFSET (-1ULL) +static inline uint64_t get_l2_entry(BDRVQcow2State *s, uint64_t *l2_slice, + int idx) +{ + return be64_to_cpu(l2_slice[idx]); +} + +static inline void set_l2_entry(BDRVQcow2State *s, uint64_t *l2_slice, + int idx, uint64_t entry) +{ + l2_slice[idx] = cpu_to_be64(entry); +} + static inline bool has_data_file(BlockDriverState *bs) { BDRVQcow2State *s = bs->opaque;