From nobody Tue Oct 22 22:33:05 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=openvz.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1694803478402688.8894239146482; Fri, 15 Sep 2023 11:44:38 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qhDn5-0000kU-Mp; Fri, 15 Sep 2023 14:43:43 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qhDmc-0008M0-57; Fri, 15 Sep 2023 14:43:15 -0400 Received: from relay.virtuozzo.com ([130.117.225.111]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qhDma-0003OS-Lq; Fri, 15 Sep 2023 14:43:13 -0400 Received: from ch-vpn.virtuozzo.com ([130.117.225.6] helo=iris.sw.ru) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1qhDhh-00Fs9Q-1z; Fri, 15 Sep 2023 20:41:34 +0200 From: "Denis V. Lunev" To: qemu-block@nongnu.org, qemu-devel@nongnu.org Cc: stefanha@redhat.com, alexander.ivanov@virtuozzo.com, mike.maslenkin@gmail.com, "Denis V. Lunev" Subject: [PATCH 11/21] parallels: collect bitmap of used clusters at open Date: Fri, 15 Sep 2023 20:41:20 +0200 Message-Id: <20230915184130.403366-14-den@openvz.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230915184130.403366-1-den@openvz.org> References: <20230915184130.403366-1-den@openvz.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=130.117.225.111; envelope-from=den@openvz.org; helo=relay.virtuozzo.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1694803480707100011 Content-Type: text/plain; charset="utf-8" If the operation is failed, we need to check image consistency if the problem is not about memory allocation. Bitmap adjustments in allocate_cluster are not performed yet. They worth to be separate. This was proven useful during debug of this series. Kept as is for future bissecting. It should be specifically noted that used bitmap must be recalculated if data_off has been fixed during image consistency check. Signed-off-by: Denis V. Lunev --- block/parallels.c | 73 +++++++++++++++++++++++++++++++++++++++++++++++ block/parallels.h | 3 ++ 2 files changed, 76 insertions(+) diff --git a/block/parallels.c b/block/parallels.c index 182ef98872..d677a1a253 100644 --- a/block/parallels.c +++ b/block/parallels.c @@ -193,6 +193,58 @@ static int mark_used(BlockDriverState *bs, return 0; } =20 +/* + * Collect used bitmap. The image can contain errors, we should fill the + * bitmap anyway, as much as we can. This information will be used for + * error resolution. + */ +static int parallels_fill_used_bitmap(BlockDriverState *bs) +{ + BDRVParallelsState *s =3D bs->opaque; + int64_t payload_bytes; + uint32_t i; + int err =3D 0; + + payload_bytes =3D bdrv_co_getlength(bs->file->bs); + if (payload_bytes < 0) { + return payload_bytes; + } + payload_bytes -=3D s->data_start * BDRV_SECTOR_SIZE; + if (payload_bytes < 0) { + return -EINVAL; + } + + s->used_bmap_size =3D DIV_ROUND_UP(payload_bytes, s->cluster_size); + if (s->used_bmap_size =3D=3D 0) { + return 0; + } + s->used_bmap =3D bitmap_try_new(s->used_bmap_size); + if (s->used_bmap =3D=3D NULL) { + return -ENOMEM; + } + + for (i =3D 0; i < s->bat_size; i++) { + int err2; + int64_t host_off =3D bat2sect(s, i) << BDRV_SECTOR_BITS; + if (host_off =3D=3D 0) { + continue; + } + + err2 =3D mark_used(bs, s->used_bmap, s->used_bmap_size, host_off); + if (err2 < 0 && err =3D=3D 0) { + err =3D err2; + } + } + return err; +} + +static void parallels_free_used_bitmap(BlockDriverState *bs) +{ + BDRVParallelsState *s =3D bs->opaque; + s->used_bmap_size =3D 0; + g_free(s->used_bmap); +} + static int64_t coroutine_fn GRAPH_RDLOCK allocate_clusters(BlockDriverState *bs, int64_t sector_num, int nb_sectors, int *pnum) @@ -530,8 +582,17 @@ parallels_check_data_off(BlockDriverState *bs, BdrvChe= ckResult *res, =20 res->corruptions++; if (fix & BDRV_FIX_ERRORS) { + int err; s->header->data_off =3D cpu_to_le32(data_off); s->data_start =3D data_off; + + parallels_free_used_bitmap(bs); + err =3D parallels_fill_used_bitmap(bs); + if (err =3D=3D -ENOMEM) { + res->check_errors++; + return err; + } + res->corruptions_fixed++; } =20 @@ -1214,6 +1275,14 @@ static int parallels_open(BlockDriverState *bs, QDic= t *options, int flags, } need_check =3D need_check || s->data_end > file_nb_sectors; =20 + if (!need_check) { + ret =3D parallels_fill_used_bitmap(bs); + if (ret =3D=3D -ENOMEM) { + goto fail; + } + need_check =3D need_check || ret < 0; /* These are correctable err= ors */ + } + /* * We don't repair the image here if it's opened for checks. Also we d= on't * want to change inactive images and can't change readonly images. @@ -1243,6 +1312,8 @@ fail: * "s" object was allocated by g_malloc0 so we can safely * try to free its fields even they were not allocated. */ + parallels_free_used_bitmap(bs); + error_free(s->migration_blocker); g_free(s->bat_dirty_bmap); qemu_vfree(s->header); @@ -1263,6 +1334,8 @@ static void parallels_close(BlockDriverState *bs) PREALLOC_MODE_OFF, 0, NULL); } =20 + parallels_free_used_bitmap(bs); + g_free(s->bat_dirty_bmap); qemu_vfree(s->header); =20 diff --git a/block/parallels.h b/block/parallels.h index 4e53e9572d..6b199443cf 100644 --- a/block/parallels.h +++ b/block/parallels.h @@ -72,6 +72,9 @@ typedef struct BDRVParallelsState { unsigned long *bat_dirty_bmap; unsigned int bat_dirty_block; =20 + unsigned long *used_bmap; + unsigned long used_bmap_size; + uint32_t *bat_bitmap; unsigned int bat_size; =20 --=20 2.34.1