From nobody Wed Nov 5 11:00:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534508854503892.5805869732524; Fri, 17 Aug 2018 05:27:34 -0700 (PDT) Received: from localhost ([::1]:33515 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdqh-0007vH-Tr for importer@patchew.org; Fri, 17 Aug 2018 08:27:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34806) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdlv-0004J0-TZ for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqdlq-0004Sn-Po for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:31 -0400 Received: from relay.sw.ru ([185.231.240.75]:52196) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqdlp-0004RA-Fu; Fri, 17 Aug 2018 08:22:26 -0400 Received: from [10.28.8.145] (helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fqdlm-0006lJ-Bh; Fri, 17 Aug 2018 15:22:23 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 17 Aug 2018 15:22:13 +0300 Message-Id: <20180817122219.16206-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180817122219.16206-1-vsementsov@virtuozzo.com> References: <20180817122219.16206-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 1/7] block/qcow2-refcount: fix check_oflag_copied X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Increase corruptions_fixed only after successful fix. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 3c539f02e5..615847eb09 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1816,7 +1816,7 @@ static int check_oflag_copied(BlockDriverState *bs, B= drvCheckResult *res, for (i =3D 0; i < s->l1_size; i++) { uint64_t l1_entry =3D s->l1_table[i]; uint64_t l2_offset =3D l1_entry & L1E_OFFSET_MASK; - bool l2_dirty =3D false; + int l2_fixed_entries =3D 0; =20 if (!l2_offset) { continue; @@ -1878,8 +1878,7 @@ static int check_oflag_copied(BlockDriverState *bs, B= drvCheckResult *res, l2_table[j] =3D cpu_to_be64(refcount =3D=3D 1 ? l2_entry | QCOW_OFLAG_COPIED : l2_entry & ~QCOW_OFLAG_COPIED); - l2_dirty =3D true; - res->corruptions_fixed++; + l2_fixed_entries++; } else { res->corruptions++; } @@ -1887,7 +1886,7 @@ static int check_oflag_copied(BlockDriverState *bs, B= drvCheckResult *res, } } =20 - if (l2_dirty) { + if (l2_fixed_entries > 0) { ret =3D qcow2_pre_write_overlap_check(bs, QCOW2_OL_ACTIVE_L2, l2_offset, s->cluster_size= ); if (ret < 0) { @@ -1905,6 +1904,7 @@ static int check_oflag_copied(BlockDriverState *bs, B= drvCheckResult *res, res->check_errors++; goto fail; } + res->corruptions_fixed +=3D l2_fixed_entries; } } =20 --=20 2.11.1 From nobody Wed Nov 5 11:00:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 153450868156823.14927820627952; Fri, 17 Aug 2018 05:24:41 -0700 (PDT) Received: from localhost ([::1]:33495 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdo0-0005UZ-A6 for importer@patchew.org; Fri, 17 Aug 2018 08:24:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34810) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdlw-0004J3-3E for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqdls-0004UF-Sq for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:31 -0400 Received: from relay.sw.ru ([185.231.240.75]:52202) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqdlr-0004S4-2u; Fri, 17 Aug 2018 08:22:28 -0400 Received: from [10.28.8.145] (helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fqdlo-0006lJ-A0; Fri, 17 Aug 2018 15:22:25 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 17 Aug 2018 15:22:14 +0300 Message-Id: <20180817122219.16206-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180817122219.16206-1-vsementsov@virtuozzo.com> References: <20180817122219.16206-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 2/7] block/qcow2-refcount: avoid eating RAM X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" qcow2_inc_refcounts_imrt() (through realloc_refcount_array()) can eat an unpredictable amount of memory on corrupted table entries, which are referencing regions far beyond the end of file. Prevent this, by skipping such regions from further processing. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 615847eb09..566c19fbfa 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1499,12 +1499,26 @@ int qcow2_inc_refcounts_imrt(BlockDriverState *bs, = BdrvCheckResult *res, { BDRVQcow2State *s =3D bs->opaque; uint64_t start, last, cluster_offset, k, refcount; + int64_t file_len; int ret; =20 if (size <=3D 0) { return 0; } =20 + file_len =3D bdrv_getlength(bs->file->bs); + if (file_len < 0) { + return file_len; + } + + if (offset + size - file_len > s->cluster_size) { + fprintf(stderr, "ERROR: counting reference for region exceeding th= e " + "end of the file by more than one cluster: offset 0x%" PRI= x64 + " size 0x%" PRIx64 "\n", offset, size); + res->corruptions++; + return 0; + } + start =3D start_of_cluster(s, offset); last =3D start_of_cluster(s, offset + size - 1); for(cluster_offset =3D start; cluster_offset <=3D last; --=20 2.11.1 From nobody Wed Nov 5 11:00:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534508684722455.9815121612195; Fri, 17 Aug 2018 05:24:44 -0700 (PDT) Received: from localhost ([::1]:33497 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdnz-0005VG-JN for importer@patchew.org; Fri, 17 Aug 2018 08:24:39 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34811) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdlw-0004J4-3G for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqdlt-0004UO-2f for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:31 -0400 Received: from relay.sw.ru ([185.231.240.75]:52206) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqdls-0004TQ-PY; Fri, 17 Aug 2018 08:22:28 -0400 Received: from [10.28.8.145] (helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fqdlp-0006lJ-OD; Fri, 17 Aug 2018 15:22:26 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 17 Aug 2018 15:22:15 +0300 Message-Id: <20180817122219.16206-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180817122219.16206-1-vsementsov@virtuozzo.com> References: <20180817122219.16206-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 3/7] block/qcow2-refcount: check_refcounts_l2: refactor compressed case X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Separate offset and size of compressed cluster. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 566c19fbfa..0ea01e3ee2 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1570,7 +1570,7 @@ static int check_refcounts_l2(BlockDriverState *bs, B= drvCheckResult *res, BDRVQcow2State *s =3D bs->opaque; uint64_t *l2_table, l2_entry; uint64_t next_contiguous_offset =3D 0; - int i, l2_size, nb_csectors, ret; + int i, l2_size, ret; =20 /* Read L2 table from disk */ l2_size =3D s->l2_size * sizeof(uint64_t); @@ -1589,6 +1589,9 @@ static int check_refcounts_l2(BlockDriverState *bs, B= drvCheckResult *res, =20 switch (qcow2_get_cluster_type(l2_entry)) { case QCOW2_CLUSTER_COMPRESSED: + { + int64_t csize, coffset; + /* Compressed clusters don't have QCOW_OFLAG_COPIED */ if (l2_entry & QCOW_OFLAG_COPIED) { fprintf(stderr, "ERROR: coffset=3D0x%" PRIx64 ": " @@ -1599,12 +1602,13 @@ static int check_refcounts_l2(BlockDriverState *bs,= BdrvCheckResult *res, } =20 /* Mark cluster as used */ - nb_csectors =3D ((l2_entry >> s->csize_shift) & - s->csize_mask) + 1; - l2_entry &=3D s->cluster_offset_mask; + csize =3D (((l2_entry >> s->csize_shift) & s->csize_mask) + 1)= * + BDRV_SECTOR_SIZE; + coffset =3D l2_entry & s->cluster_offset_mask & + ~(BDRV_SECTOR_SIZE - 1); ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_= size, - l2_entry & ~511, nb_csectors * = 512); + coffset, csize); if (ret < 0) { goto fail; } @@ -1621,6 +1625,7 @@ static int check_refcounts_l2(BlockDriverState *bs, B= drvCheckResult *res, res->bfi.fragmented_clusters++; } break; + } =20 case QCOW2_CLUSTER_ZERO_ALLOC: case QCOW2_CLUSTER_NORMAL: --=20 2.11.1 From nobody Wed Nov 5 11:00:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534508858641760.3474858121581; Fri, 17 Aug 2018 05:27:38 -0700 (PDT) Received: from localhost ([::1]:33516 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdqi-0007wx-KN for importer@patchew.org; Fri, 17 Aug 2018 08:27:28 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34809) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdlw-0004J2-37 for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqdlu-0004VF-15 for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:31 -0400 Received: from relay.sw.ru ([185.231.240.75]:52212) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqdlt-0004UC-N2; Fri, 17 Aug 2018 08:22:29 -0400 Received: from [10.28.8.145] (helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fqdlr-0006lJ-8j; Fri, 17 Aug 2018 15:22:27 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 17 Aug 2018 15:22:16 +0300 Message-Id: <20180817122219.16206-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180817122219.16206-1-vsementsov@virtuozzo.com> References: <20180817122219.16206-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 4/7] block/qcow2-refcount: check_refcounts_l2: reduce ignored overlaps X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Reduce number of structures ignored in overlap check: when checking active table ignore active tables, when checking inactive table ignore inactive ones. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Max Reitz --- block/qcow2-refcount.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 0ea01e3ee2..3b057b635d 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1565,7 +1565,7 @@ enum { static int check_refcounts_l2(BlockDriverState *bs, BdrvCheckResult *res, void **refcount_table, int64_t *refcount_table_size, int64_t l2_off= set, - int flags, BdrvCheckMode fix) + int flags, BdrvCheckMode fix, bool active) { BDRVQcow2State *s =3D bs->opaque; uint64_t *l2_table, l2_entry; @@ -1654,11 +1654,12 @@ static int check_refcounts_l2(BlockDriverState *bs,= BdrvCheckResult *res, if (fix & BDRV_FIX_ERRORS) { uint64_t l2e_offset =3D l2_offset + (uint64_t)i * sizeof(uint64_t); + int ign =3D active ? QCOW2_OL_ACTIVE_L2 : + QCOW2_OL_INACTIVE_L2; =20 l2_entry =3D QCOW_OFLAG_ZERO; l2_table[i] =3D cpu_to_be64(l2_entry); - ret =3D qcow2_pre_write_overlap_check(bs, - QCOW2_OL_ACTIVE_L2 | QCOW2_OL_INACTIVE_L2, + ret =3D qcow2_pre_write_overlap_check(bs, ign, l2e_offset, sizeof(uint64_t)); if (ret < 0) { fprintf(stderr, "ERROR: Overlap check failed\n= "); @@ -1732,7 +1733,7 @@ static int check_refcounts_l1(BlockDriverState *bs, void **refcount_table, int64_t *refcount_table_size, int64_t l1_table_offset, int l1_size, - int flags, BdrvCheckMode fix) + int flags, BdrvCheckMode fix, bool active) { BDRVQcow2State *s =3D bs->opaque; uint64_t *l1_table =3D NULL, l2_offset, l1_size2; @@ -1788,7 +1789,7 @@ static int check_refcounts_l1(BlockDriverState *bs, /* Process and check L2 entries */ ret =3D check_refcounts_l2(bs, res, refcount_table, refcount_table_size, l2_offset, flags, - fix); + fix, active); if (ret < 0) { goto fail; } @@ -2074,7 +2075,7 @@ static int calculate_refcounts(BlockDriverState *bs, = BdrvCheckResult *res, /* current L1 table */ ret =3D check_refcounts_l1(bs, res, refcount_table, nb_clusters, s->l1_table_offset, s->l1_size, CHECK_FRAG_IN= FO, - fix); + fix, true); if (ret < 0) { return ret; } @@ -2097,7 +2098,8 @@ static int calculate_refcounts(BlockDriverState *bs, = BdrvCheckResult *res, continue; } ret =3D check_refcounts_l1(bs, res, refcount_table, nb_clusters, - sn->l1_table_offset, sn->l1_size, 0, fix); + sn->l1_table_offset, sn->l1_size, 0, fix, + false); if (ret < 0) { return ret; } --=20 2.11.1 From nobody Wed Nov 5 11:00:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534508683479801.2160751602268; Fri, 17 Aug 2018 05:24:43 -0700 (PDT) Received: from localhost ([::1]:33498 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdo2-0005W1-4V for importer@patchew.org; Fri, 17 Aug 2018 08:24:42 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34837) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdlw-0004J9-JX for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqdlv-0004W1-LH for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:32 -0400 Received: from relay.sw.ru ([185.231.240.75]:52226) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqdlv-0004VU-9y; Fri, 17 Aug 2018 08:22:31 -0400 Received: from [10.28.8.145] (helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fqdls-0006lJ-I4; Fri, 17 Aug 2018 15:22:29 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 17 Aug 2018 15:22:17 +0300 Message-Id: <20180817122219.16206-6-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180817122219.16206-1-vsementsov@virtuozzo.com> References: <20180817122219.16206-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 5/7] block/qcow2-refcount: check_refcounts_l2: split fix_l2_entry_to_zero X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Split entry repairing to separate function, to be reused later. Note: entry in in-memory l2 table (local variable in check_refcounts_l2) is not updated after this patch. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 147 ++++++++++++++++++++++++++++++++++++---------= ---- 1 file changed, 109 insertions(+), 38 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 3b057b635d..d9c8cd666b 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1554,6 +1554,99 @@ enum { CHECK_FRAG_INFO =3D 0x2, /* update BlockFragInfo counters */ }; =20 +/* Update entry in L1 or L2 table + * + * Returns: -errno if overlap check failed + * 0 if write failed + * 1 on success + */ +static int write_table_entry(BlockDriverState *bs, const char *table_name, + uint64_t table_offset, int entry_index, + uint64_t new_val, int ign) +{ + int ret; + uint64_t entry_offset =3D + table_offset + (uint64_t)entry_index * sizeof(new_val); + + cpu_to_be64s(&new_val); + ret =3D qcow2_pre_write_overlap_check(bs, ign, entry_offset, sizeof(ne= w_val)); + if (ret < 0) { + fprintf(stderr, + "ERROR: Can't write %s table entry: overlap check failed: = %s\n", + table_name, strerror(-ret)); + return ret; + } + + ret =3D bdrv_pwrite_sync(bs->file, entry_offset, &new_val, sizeof(new_= val)); + if (ret < 0) { + fprintf(stderr, "ERROR: Failed to overwrite %s table entry: %s\n", + table_name, strerror(-ret)); + return 0; + } + + return 1; +} + +/* Try to fix (if allowed) entry in L1 or L2 table. Update @res correspond= ingly. + * + * Returns: -errno if overlap check failed + * 0 if entry was not updated for other reason + * (fixing disabled or write failed) + * 1 on success + */ +static int fix_table_entry(BlockDriverState *bs, BdrvCheckResult *res, + BdrvCheckMode fix, const char *table_name, + uint64_t table_offset, int entry_index, + uint64_t new_val, int ign, + const char *fmt, va_list args) +{ + int ret; + + fprintf(stderr, fix & BDRV_FIX_ERRORS ? "Repairing: " : "ERROR: "); + vfprintf(stderr, fmt, args); + fprintf(stderr, "\n"); + + if (!(fix & BDRV_FIX_ERRORS)) { + res->corruptions++; + return 0; + } + + ret =3D write_table_entry(bs, table_name, table_offset, entry_index, n= ew_val, + ign); + + if (ret =3D=3D 1) { + res->corruptions_fixed++; + } else { + res->check_errors++; + } + + return ret; +} + +/* Make L2 entry to be QCOW2_CLUSTER_ZERO_PLAIN + * + * Returns: -errno if overlap check failed + * 0 if write failed + * 1 on success + */ +static int fix_l2_entry_to_zero(BlockDriverState *bs, BdrvCheckResult *res, + BdrvCheckMode fix, int64_t l2_offset, + int l2_index, bool active, + const char *fmt, ...) +{ + int ret; + int ign =3D active ? QCOW2_OL_ACTIVE_L2 : QCOW2_OL_INACTIVE_L2; + uint64_t l2_entry =3D QCOW_OFLAG_ZERO; + va_list args; + + va_start(args, fmt); + ret =3D fix_table_entry(bs, res, fix, "L2", l2_offset, l2_index, l2_en= try, + ign, fmt, args); + va_end(args); + + return ret; +} + /* * Increases the refcount in the given refcount table for the all clusters * referenced in the L2 table. While doing so, performs some checks on L2 @@ -1646,46 +1739,24 @@ static int check_refcounts_l2(BlockDriverState *bs,= BdrvCheckResult *res, if (qcow2_get_cluster_type(l2_entry) =3D=3D QCOW2_CLUSTER_ZERO_ALLOC) { - fprintf(stderr, "%s offset=3D%" PRIx64 ": Preallocated= zero " - "cluster is not properly aligned; L2 entry " - "corrupted.\n", - fix & BDRV_FIX_ERRORS ? "Repairing" : "ERROR", + ret =3D fix_l2_entry_to_zero( + bs, res, fix, l2_offset, i, active, + "offset=3D%" PRIx64 ": Preallocated zero clust= er is " + "not properly aligned; L2 entry corrupted.", offset); - if (fix & BDRV_FIX_ERRORS) { - uint64_t l2e_offset =3D - l2_offset + (uint64_t)i * sizeof(uint64_t); - int ign =3D active ? QCOW2_OL_ACTIVE_L2 : - QCOW2_OL_INACTIVE_L2; - - l2_entry =3D QCOW_OFLAG_ZERO; - l2_table[i] =3D cpu_to_be64(l2_entry); - ret =3D qcow2_pre_write_overlap_check(bs, ign, - l2e_offset, sizeof(uint64_t)); - if (ret < 0) { - fprintf(stderr, "ERROR: Overlap check failed\n= "); - res->check_errors++; - /* Something is seriously wrong, so abort chec= king - * this L2 table */ - goto fail; - } - - ret =3D bdrv_pwrite_sync(bs->file, l2e_offset, - &l2_table[i], sizeof(uint64= _t)); - if (ret < 0) { - fprintf(stderr, "ERROR: Failed to overwrite L2= " - "table entry: %s\n", strerror(-ret)); - res->check_errors++; - /* Do not abort, continue checking the rest of= this - * L2 table's entries */ - } else { - res->corruptions_fixed++; - /* Skip marking the cluster as used - * (it is unused now) */ - continue; - } - } else { - res->corruptions++; + if (ret < 0) { + /* Something is seriously wrong, so abort checking + * this L2 table */ + goto fail; + } + if (ret =3D=3D 1) { + /* Skip marking the cluster as used + * (it is unused now) */ + continue; } + /* Entry was not updated, but do not abort, mark clust= er + * as used and continue checking the rest of this L2 + * table's entries */ } else { fprintf(stderr, "ERROR offset=3D%" PRIx64 ": Data clus= ter is " "not properly aligned; L2 entry corrupted.\n", off= set); --=20 2.11.1 From nobody Wed Nov 5 11:00:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534508871818456.2725731693881; Fri, 17 Aug 2018 05:27:51 -0700 (PDT) Received: from localhost ([::1]:33519 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdr4-0008I9-HJ for importer@patchew.org; Fri, 17 Aug 2018 08:27:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34908) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdly-0004Jx-EW for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqdlx-0004YF-L9 for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:34 -0400 Received: from relay.sw.ru ([185.231.240.75]:52234) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqdlx-0004Wy-A0; Fri, 17 Aug 2018 08:22:33 -0400 Received: from [10.28.8.145] (helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fqdlu-0006lJ-4Q; Fri, 17 Aug 2018 15:22:30 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 17 Aug 2018 15:22:18 +0300 Message-Id: <20180817122219.16206-7-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180817122219.16206-1-vsementsov@virtuozzo.com> References: <20180817122219.16206-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 6/7] block/qcow2-refcount: fix out-of-file L1 entries to be zero X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Zero out corrupted L1 table entry, which reference L2 table out of underlying file. Zero L1 table entry means that "the L2 table and all clusters described by this L2 table are unallocated." Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index d9c8cd666b..3c004e5bfe 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1647,6 +1647,29 @@ static int fix_l2_entry_to_zero(BlockDriverState *bs= , BdrvCheckResult *res, return ret; } =20 +/* Zero out L1 entry + * + * Returns: -errno if overlap check failed + * 0 if write failed + * 1 on success + */ +static int fix_l1_entry_to_zero(BlockDriverState *bs, BdrvCheckResult *res, + BdrvCheckMode fix, int64_t l1_offset, + int l1_index, bool active, + const char *fmt, ...) +{ + int ret; + int ign =3D active ? QCOW2_OL_ACTIVE_L2 : QCOW2_OL_INACTIVE_L2; + va_list args; + + va_start(args, fmt); + ret =3D fix_table_entry(bs, res, fix, "L1", l1_offset, l1_index, 0, ig= n, + fmt, args); + va_end(args); + + return ret; +} + /* * Increases the refcount in the given refcount table for the all clusters * referenced in the L2 table. While doing so, performs some checks on L2 @@ -1808,6 +1831,7 @@ static int check_refcounts_l1(BlockDriverState *bs, { BDRVQcow2State *s =3D bs->opaque; uint64_t *l1_table =3D NULL, l2_offset, l1_size2; + int64_t file_len; int i, ret; =20 l1_size2 =3D l1_size * sizeof(uint64_t); @@ -1837,12 +1861,32 @@ static int check_refcounts_l1(BlockDriverState *bs, be64_to_cpus(&l1_table[i]); } =20 + file_len =3D bdrv_getlength(bs->file->bs); + if (file_len < 0) { + ret =3D file_len; + goto fail; + } + /* Do the actual checks */ for(i =3D 0; i < l1_size; i++) { l2_offset =3D l1_table[i]; if (l2_offset) { /* Mark L2 table as used */ l2_offset &=3D L1E_OFFSET_MASK; + if (l2_offset >=3D file_len) { + ret =3D fix_l1_entry_to_zero( + bs, res, fix, l1_table_offset, i, active, + "l2 table offset out of file: offset 0x%" PRIx64, + l2_offset); + if (ret < 0) { + /* Something is seriously wrong, so abort checking + * this L1 table */ + goto fail; + } + + continue; + } + ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_= size, l2_offset, s->cluster_size); --=20 2.11.1 From nobody Wed Nov 5 11:00:04 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) client-ip=208.118.235.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of gnu.org designates 208.118.235.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=virtuozzo.com Return-Path: Received: from lists.gnu.org (208.118.235.17 [208.118.235.17]) by mx.zohomail.com with SMTPS id 1534508861507541.3962916197863; Fri, 17 Aug 2018 05:27:41 -0700 (PDT) Received: from localhost ([::1]:33517 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdqu-00088E-DX for importer@patchew.org; Fri, 17 Aug 2018 08:27:40 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:34933) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fqdm0-0004MG-3t for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:36 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fqdlz-0004a4-Dc for qemu-devel@nongnu.org; Fri, 17 Aug 2018 08:22:36 -0400 Received: from relay.sw.ru ([185.231.240.75]:52244) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fqdlz-0004Z7-68; Fri, 17 Aug 2018 08:22:35 -0400 Received: from [10.28.8.145] (helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fqdlw-0006lJ-6A; Fri, 17 Aug 2018 15:22:33 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-devel@nongnu.org, qemu-block@nongnu.org Date: Fri, 17 Aug 2018 15:22:19 +0300 Message-Id: <20180817122219.16206-8-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180817122219.16206-1-vsementsov@virtuozzo.com> References: <20180817122219.16206-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH v2 7/7] block/qcow2-refcount: fix out-of-file L2 entries to be read-as-zero X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, den@openvz.org, vsementsov@virtuozzo.com, mreitz@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Rewrite corrupted L2 table entry, which reference space out of underlying file. Make this L2 table entry read-as-all-zeros without any allocation. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index 3c004e5bfe..3de3768a3c 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1720,8 +1720,30 @@ static int check_refcounts_l2(BlockDriverState *bs, = BdrvCheckResult *res, /* Mark cluster as used */ csize =3D (((l2_entry >> s->csize_shift) & s->csize_mask) + 1)= * BDRV_SECTOR_SIZE; + if (csize > s->cluster_size) { + ret =3D fix_l2_entry_to_zero( + bs, res, fix, l2_offset, i, active, + "compressed cluster larger than cluster: size 0x%" + PRIx64, csize); + if (ret < 0) { + goto fail; + } + continue; + } + coffset =3D l2_entry & s->cluster_offset_mask & ~(BDRV_SECTOR_SIZE - 1); + if (coffset >=3D bdrv_getlength(bs->file->bs)) { + ret =3D fix_l2_entry_to_zero( + bs, res, fix, l2_offset, i, active, + "compressed cluster out of file: offset 0x%" PRIx6= 4, + coffset); + if (ret < 0) { + goto fail; + } + continue; + } + ret =3D qcow2_inc_refcounts_imrt(bs, res, refcount_table, refcount_table_= size, coffset, csize); @@ -1748,6 +1770,16 @@ static int check_refcounts_l2(BlockDriverState *bs, = BdrvCheckResult *res, { uint64_t offset =3D l2_entry & L2E_OFFSET_MASK; =20 + if (offset >=3D bdrv_getlength(bs->file->bs)) { + ret =3D fix_l2_entry_to_zero( + bs, res, fix, l2_offset, i, active, + "cluster out of file: offset 0x%" PRIx64, offset); + if (ret < 0) { + goto fail; + } + continue; + } + if (flags & CHECK_FRAG_INFO) { res->bfi.allocated_clusters++; if (next_contiguous_offset && --=20 2.11.1