From nobody Sat May 4 18:55:22 2024 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 1529433616184313.7289276431069; Tue, 19 Jun 2018 11:40:16 -0700 (PDT) Received: from localhost ([::1]:44408 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLY7-0003CP-F4 for importer@patchew.org; Tue, 19 Jun 2018 14:40:15 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47539) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLTC-0007jD-9s for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVLTA-0005aT-OX for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:10 -0400 Received: from relay.sw.ru ([185.231.240.75]:38916) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fVLT4-0005JE-K9; Tue, 19 Jun 2018 14:35:02 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fVLT0-0004dh-MX; Tue, 19 Jun 2018 21:34:58 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Tue, 19 Jun 2018 21:34:51 +0300 Message-Id: <20180619183457.371081-2-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180619183457.371081-1-vsementsov@virtuozzo.com> References: <20180619183457.371081-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 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: 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 18c729aa27..f9d095aa2d 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 Sat May 4 18:55:22 2024 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 1529433473147985.8229030961; Tue, 19 Jun 2018 11:37:53 -0700 (PDT) Received: from localhost ([::1]:44396 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLVm-0000rI-DH for importer@patchew.org; Tue, 19 Jun 2018 14:37:50 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47505) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLTB-0007i3-HQ for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVLTA-0005ZR-Em for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:09 -0400 Received: from relay.sw.ru ([185.231.240.75]:38926) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fVLT4-0005JF-HV; Tue, 19 Jun 2018 14:35:02 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fVLT0-0004dh-Tm; Tue, 19 Jun 2018 21:34:59 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Tue, 19 Jun 2018 21:34:52 +0300 Message-Id: <20180619183457.371081-3-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180619183457.371081-1-vsementsov@virtuozzo.com> References: <20180619183457.371081-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 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: 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 unpredicted 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 | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index f9d095aa2d..28d21bedc3 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1505,6 +1505,14 @@ int qcow2_inc_refcounts_imrt(BlockDriverState *bs, B= drvCheckResult *res, return 0; } =20 + if (offset + size - bdrv_getlength(bs->file->bs) > 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 Sat May 4 18:55:22 2024 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 1529433726079727.7345625254806; Tue, 19 Jun 2018 11:42:06 -0700 (PDT) Received: from localhost ([::1]:44422 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLZt-0004ia-CW for importer@patchew.org; Tue, 19 Jun 2018 14:42:05 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47510) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLTB-0007iO-NF for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVLTA-0005Yj-9D for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:09 -0400 Received: from relay.sw.ru ([185.231.240.75]:38924) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fVLT4-0005JI-Jw; Tue, 19 Jun 2018 14:35:02 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fVLT1-0004dh-6o; Tue, 19 Jun 2018 21:34:59 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Tue, 19 Jun 2018 21:34:53 +0300 Message-Id: <20180619183457.371081-4-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180619183457.371081-1-vsementsov@virtuozzo.com> References: <20180619183457.371081-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 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: 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 28d21bedc3..42167b7040 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1564,7 +1564,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); @@ -1583,6 +1583,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 ": " @@ -1593,12 +1596,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; } @@ -1615,6 +1619,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 Sat May 4 18:55:22 2024 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 1529433457301442.97955669569865; Tue, 19 Jun 2018 11:37:37 -0700 (PDT) Received: from localhost ([::1]:44395 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLVY-0000fx-IS for importer@patchew.org; Tue, 19 Jun 2018 14:37:36 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47520) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLTB-0007il-WB for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:11 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVLTA-0005Zk-Hh for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:10 -0400 Received: from relay.sw.ru ([185.231.240.75]:38920) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fVLT4-0005JH-H7; Tue, 19 Jun 2018 14:35:02 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fVLT1-0004dh-HU; Tue, 19 Jun 2018 21:34:59 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Tue, 19 Jun 2018 21:34:54 +0300 Message-Id: <20180619183457.371081-5-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180619183457.371081-1-vsementsov@virtuozzo.com> References: <20180619183457.371081-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 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: 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 --- 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 42167b7040..02583f260b 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1559,7 +1559,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; @@ -1648,11 +1648,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= "); @@ -1726,7 +1727,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; @@ -1782,7 +1783,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; } @@ -2068,7 +2069,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; } @@ -2091,7 +2092,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 Sat May 4 18:55:22 2024 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 1529433817320207.91724041675877; Tue, 19 Jun 2018 11:43:37 -0700 (PDT) Received: from localhost ([::1]:44427 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLbD-0005e5-MJ for importer@patchew.org; Tue, 19 Jun 2018 14:43:27 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47556) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLTC-0007jo-N7 for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:12 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVLTA-0005b9-VW for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:10 -0400 Received: from relay.sw.ru ([185.231.240.75]:38930) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fVLT4-0005JK-HI; Tue, 19 Jun 2018 14:35:02 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fVLT1-0004dh-Qi; Tue, 19 Jun 2018 21:34:59 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Tue, 19 Jun 2018 21:34:55 +0300 Message-Id: <20180619183457.371081-6-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180619183457.371081-1-vsementsov@virtuozzo.com> References: <20180619183457.371081-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 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: 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 02583f260b..d993252fb6 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1548,6 +1548,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 @@ -1640,46 +1733,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 Sat May 4 18:55:22 2024 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 152943350629041.26087034236764; Tue, 19 Jun 2018 11:38:26 -0700 (PDT) Received: from localhost ([::1]:44397 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLWL-0001JE-G3 for importer@patchew.org; Tue, 19 Jun 2018 14:38:25 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47514) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLTB-0007ib-QV for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVLTA-0005Zz-K5 for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:09 -0400 Received: from relay.sw.ru ([185.231.240.75]:38936) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fVLT4-0005Jk-Jx; Tue, 19 Jun 2018 14:35:02 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fVLT2-0004dh-2x; Tue, 19 Jun 2018 21:35:00 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Tue, 19 Jun 2018 21:34:56 +0300 Message-Id: <20180619183457.371081-7-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180619183457.371081-1-vsementsov@virtuozzo.com> References: <20180619183457.371081-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 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: 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 | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index d993252fb6..3c9e2da39e 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1641,6 +1641,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 @@ -1837,6 +1860,20 @@ static int check_refcounts_l1(BlockDriverState *bs, if (l2_offset) { /* Mark L2 table as used */ l2_offset &=3D L1E_OFFSET_MASK; + if (l2_offset >=3D bdrv_getlength(bs->file->bs)) { + 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 Sat May 4 18:55:22 2024 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 1529433609556763.8297442204017; Tue, 19 Jun 2018 11:40:09 -0700 (PDT) Received: from localhost ([::1]:44407 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLY0-00034c-Pm for importer@patchew.org; Tue, 19 Jun 2018 14:40:08 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:47497) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fVLTB-0007hn-Ch for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:10 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fVLTA-0005YI-5a for qemu-devel@nongnu.org; Tue, 19 Jun 2018 14:35:09 -0400 Received: from relay.sw.ru ([185.231.240.75]:38938) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fVLT4-0005K3-MB; Tue, 19 Jun 2018 14:35:02 -0400 Received: from msk-vpn.virtuozzo.com ([195.214.232.6] helo=kvm.sw.ru) by relay.sw.ru with esmtp (Exim 4.90_1) (envelope-from ) id 1fVLT2-0004dh-BB; Tue, 19 Jun 2018 21:35:00 +0300 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org, qemu-devel@nongnu.org Date: Tue, 19 Jun 2018 21:34:57 +0300 Message-Id: <20180619183457.371081-8-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20180619183457.371081-1-vsementsov@virtuozzo.com> References: <20180619183457.371081-1-vsementsov@virtuozzo.com> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 185.231.240.75 Subject: [Qemu-devel] [PATCH 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: 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 3c9e2da39e..cbad8355f3 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -1714,8 +1714,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); @@ -1742,6 +1764,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