From nobody Sun Nov 24 18:31:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1720777646; cv=none; d=zohomail.com; s=zohoarc; b=FfHW2Gk9hiYiijTAhyf08BS7+Sn5xG4J5S7HGo5F3fc69lEhGZ/Q1qKayL7rNy8saUmRTKWvB3JkvQrAZ7UMFc47/6px0yrQEQVtAlg5C5+wCDxA5rWRpMh3lzYVMf0K/oZE7WK3+vTl8rSAYYrmMXJ6SMRdjMjjc+CcqrMGAEg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720777646; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=SxnnQcf/vCqlj8mFGAhTZWtk/V3Dt/KLf3GRTXyVbiM=; b=ZGfU8r4ExBpMpEZGhWrG2wJmaxi5FA5dJZ2HltC2lCrgt0ND6X4xrVitMcZrrr1MmRTWzBMu45p53Aks3C/Xwx9WBXXNmwvDtGa8kS9PwFs7brYKY+FEOUNs1WdBBvc36DHmymsAhb5xgHMzkrGIe5znAEbuYx/hPBNoSN8ow4Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1720777646539622.0624591587962; Fri, 12 Jul 2024 02:47:26 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sSCrU-0002c3-Uw; Fri, 12 Jul 2024 05:46:44 -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 1sSCrF-0002JP-5G; Fri, 12 Jul 2024 05:46:37 -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 1sSCrB-0001Y7-Iy; Fri, 12 Jul 2024 05:46:28 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1sSCqO-00Ceua-2b; Fri, 12 Jul 2024 11:46:17 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=SxnnQcf/vCqlj8mFGAhTZWtk/V3Dt/KLf3GRTXyVbiM=; b=ee++AjLfkr+e 7lB0d8+Op2frbeK6axigxz5f+LITkVCPmIUmUACjZE+Up1MHLrs9jI6ZT0vmrHlgQ+SBW8ncAsfJc ROxjgaS2AwPsLZncm7LruujMYI4oSaLJyR78XOZCFveR+Yuko0C0D6HsAVtmrXEcO/Zuf9U/4uXNL nMeu62lCPOjDXrlVqDwKVmarqXvYZt0Jj22+IN29S4qioREpA7B/yXgP1iS3hhkliUs9rt9Thirq/ zpNjoNjK8LdHGgV6yAK0++U8RxQRDuZzMquxHbsZo9X8Jmv7BEXpQ5oIHF/D3vMClIQgaMtsw0TzR dhalwBhd6ZYQbKa4SAyCfQ==; From: Andrey Drobyshev To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, hreitz@redhat.com, kwolf@redhat.com, vsementsov@yandex-team.ru, pbonzini@redhat.com, eesposit@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v2 1/2] block: zero data data corruption using prealloc-filter Date: Fri, 12 Jul 2024 12:46:16 +0300 Message-Id: <20240712094617.565237-2-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240712094617.565237-1-andrey.drobyshev@virtuozzo.com> References: <20240712094617.565237-1-andrey.drobyshev@virtuozzo.com> 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=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1720777648358116600 Content-Type: text/plain; charset="utf-8" From: "Denis V. Lunev" We have observed that some clusters in the QCOW2 files are zeroed while preallocation filter is used. We are able to trace down the following sequence when prealloc-filter is used: co=3D0x55e7cbed7680 qcow2_co_pwritev_task() co=3D0x55e7cbed7680 preallocate_co_pwritev_part() co=3D0x55e7cbed7680 handle_write() co=3D0x55e7cbed7680 bdrv_co_do_pwrite_zeroes() co=3D0x55e7cbed7680 raw_do_pwrite_zeroes() co=3D0x7f9edb7fe500 do_fallocate() Here coroutine 0x55e7cbed7680 is being blocked waiting while coroutine 0x7f9edb7fe500 will finish with fallocate of the file area. OK. It is time to handle next coroutine, which co=3D0x55e7cbee91b0 qcow2_co_pwritev_task() co=3D0x55e7cbee91b0 preallocate_co_pwritev_part() co=3D0x55e7cbee91b0 handle_write() co=3D0x55e7cbee91b0 bdrv_co_do_pwrite_zeroes() co=3D0x55e7cbee91b0 raw_do_pwrite_zeroes() co=3D0x7f9edb7deb00 do_fallocate() The trouble comes here. Coroutine 0x55e7cbed7680 has not advanced file_end yet and coroutine 0x55e7cbee91b0 will start fallocate() for the same area. This means that if (once fallocate is started inside 0x7f9edb7deb00) original fallocate could end and the real write will be executed. In that case write() request is handled at the same time as fallocate(). The patch moves s->file_lock assignment before fallocate and that is crucial. The idea is that all subsequent requests into the area being preallocation will be issued as just writes without fallocate to this area and they will not proceed thanks to overlapping requests mechanics. If preallocation will fail, we will just switch to the normal expand-by-write behavior and that is not a problem except performance. Signed-off-by: Denis V. Lunev Tested-by: Andrey Drobyshev --- block/preallocate.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/block/preallocate.c b/block/preallocate.c index d215bc5d6d..ecf0aa4baa 100644 --- a/block/preallocate.c +++ b/block/preallocate.c @@ -383,6 +383,13 @@ handle_write(BlockDriverState *bs, int64_t offset, int= 64_t bytes, =20 want_merge_zero =3D want_merge_zero && (prealloc_start <=3D offset); =20 + /* + * Assign file_end before making actual preallocation. This will ensure + * that next request performed while preallocation is in progress will + * be passed without preallocation. + */ + s->file_end =3D prealloc_end; + ret =3D bdrv_co_pwrite_zeroes( bs->file, prealloc_start, prealloc_end - prealloc_start, BDRV_REQ_NO_FALLBACK | BDRV_REQ_SERIALISING | BDRV_REQ_NO_WAIT= ); @@ -391,7 +398,6 @@ handle_write(BlockDriverState *bs, int64_t offset, int6= 4_t bytes, return false; } =20 - s->file_end =3D prealloc_end; return want_merge_zero; } =20 --=20 2.39.3 From nobody Sun Nov 24 18:31:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1720777645; cv=none; d=zohomail.com; s=zohoarc; b=logntP9+M+EnqTE/m6RrQQIFg14uXE2tlk+EmVRdzunA4dr6t+nIQA/Shcvih2XQBBidjh/U0MluHvIEBdQHvqG28a8mJ0LiGBlhOH9LHROR9sOkurRHfrcKhICDxOSfw+J3X64RgHu1FvTzamxoqp+1UR9ZvmufOMe8pbNVNNc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1720777645; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=wNiQ3C1oGxdSfTWhqpAGz0OkDU2T3/U7NR1jQ5Ua/cU=; b=P8P1g3jODpq7qWoXPtSQ8ofhioYATv5kUbSwc/ECXfcS57vWxpORWvIYu6ulnivVo89iwbTWvqlAV3F2c28atjLXTFKmr81OPVSc89FR0x4cbu+RMuRYju6Jkf5y85FoomT2DPbLHChoZf3pY5fsJp4ZZmaXBT4TbZ4fr8nGaAc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 172077764568224.17525107508004; Fri, 12 Jul 2024 02:47:25 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sSCrU-0002ZP-2A; Fri, 12 Jul 2024 05:46:44 -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 1sSCrD-0002JI-T2; Fri, 12 Jul 2024 05:46:36 -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 1sSCrB-0001Y8-Fz; Fri, 12 Jul 2024 05:46:27 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1sSCqO-00Ceua-2n; Fri, 12 Jul 2024 11:46:17 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=wNiQ3C1oGxdSfTWhqpAGz0OkDU2T3/U7NR1jQ5Ua/cU=; b=TZVOYJazAcC+ dZUJnpKeEs1+2aUSej4FmT3nh1BIxSVbx4S0Cv+ocCY1+H4UYoXKg6oQkko2Tc3mm8ypCECeWp3Tr sZy6KSFbIP1kDXnKjDrqhobxsrofGVV42GCkilQwCVD44DvYMG4C+YC/n7QTfDcdHBFTQZqRedxnK pRZn14XslOvlDloqe3PKX5p+czZ5/aOf0aO0fz0nSbQZQ/BzugwVFr3urCC4yzeT8j9IVVduQTOhu dEifB7gTYNKT5zl1wIld096MTBAhWPVQFM5yttMIF83KSt5H6imQcBy7+mQYn5mwsgPX3zCo0/ngx GPx/PVmgyIEwzXtG0KzJtg==; From: Andrey Drobyshev To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, hreitz@redhat.com, kwolf@redhat.com, vsementsov@yandex-team.ru, pbonzini@redhat.com, eesposit@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v2 2/2] iotests/298: add testcase for async writes with preallocation filter Date: Fri, 12 Jul 2024 12:46:17 +0300 Message-Id: <20240712094617.565237-3-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240712094617.565237-1-andrey.drobyshev@virtuozzo.com> References: <20240712094617.565237-1-andrey.drobyshev@virtuozzo.com> 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=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1720777646925116600 Content-Type: text/plain; charset="utf-8" The testcase simply creates a 64G image with 1M clusters, generates a list of 1M aligned offsets and feeds aio_write commands with those offsets to qemu-io run with '--aio native --nocache'. Then we check the data written at each of the offsets. Before the previous commit this could result into a race within the preallocation filter which would zeroize some clusters after actually writing data to them. Note: the test doesn't fail in 100% cases as there's a race involved, but the failures are pretty consistent so it should be good enough for detecting the problem. Signed-off-by: Andrey Drobyshev --- tests/qemu-iotests/298 | 34 ++++++++++++++++++++++++++++++++++ tests/qemu-iotests/298.out | 4 ++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/tests/qemu-iotests/298 b/tests/qemu-iotests/298 index 09c9290711..d1bf5ee0df 100755 --- a/tests/qemu-iotests/298 +++ b/tests/qemu-iotests/298 @@ -20,8 +20,10 @@ =20 import os import iotests +import random =20 MiB =3D 1024 * 1024 +GiB =3D MiB * 1024 disk =3D os.path.join(iotests.test_dir, 'disk') overlay =3D os.path.join(iotests.test_dir, 'overlay') refdisk =3D os.path.join(iotests.test_dir, 'refdisk') @@ -176,5 +178,37 @@ class TestTruncate(iotests.QMPTestCase): self.do_test('off', '150M') =20 =20 +class TestPreallocAsyncWrites(iotests.QMPTestCase): + def setUp(self): + # Make sure we get reproducible write patterns on each run + random.seed(42) + iotests.qemu_img_create('-f', iotests.imgfmt, disk, '-o', + f'cluster_size=3D{MiB},lazy_refcounts=3Don= ', + str(64 * GiB)) + + def tearDown(self): + os.remove(disk) + + def test_prealloc_async_writes(self): + requests =3D 1024 # Number of write/read requests to feed to qemu-= io + total_clusters =3D 64 * 1024 # 64G / 1M + + offsets =3D random.sample(range(0, total_clusters), requests) + aio_write_cmds =3D [f'aio_write -P 0xaa {off}M 1M' for off in offs= ets] + read_cmds =3D [f'read -P 0xaa {off}M 1M' for off in offsets] + + proc =3D iotests.QemuIoInteractive('--aio', 'native', '--nocache', + '--image-opts', drive_opts) + for cmd in aio_write_cmds: + proc.cmd(cmd) + proc.close() + + proc =3D iotests.QemuIoInteractive('-f', iotests.imgfmt, disk) + for cmd in read_cmds: + out =3D proc.cmd(cmd) + self.assertFalse('Pattern verification failed' in str(out)) + proc.close() + + if __name__ =3D=3D '__main__': iotests.main(supported_fmts=3D['qcow2'], required_fmts=3D['preallocate= ']) diff --git a/tests/qemu-iotests/298.out b/tests/qemu-iotests/298.out index fa16b5ccef..6323079e08 100644 --- a/tests/qemu-iotests/298.out +++ b/tests/qemu-iotests/298.out @@ -1,5 +1,5 @@ -............. +.............. ---------------------------------------------------------------------- -Ran 13 tests +Ran 14 tests =20 OK --=20 2.39.3 From nobody Sun Nov 24 18:31:01 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=quarantine dis=none) header.from=virtuozzo.com ARC-Seal: i=1; a=rsa-sha256; t=1721047060; cv=none; d=zohomail.com; s=zohoarc; b=luPhchkifSfFgYVl9tSH9NRuD/gffzMKd722KWnGKI9D6UbV51+5XMFZwlu5hv1+KuQl4FqGS/lG5uegCC9fl2togDrOCHmqw/3AvfMvIzcKrvMP785n9Y9si6hf4n/EeHavZ5RSJO5bPrTDs9tMyGxakNMaL9sNeTrDVllRnxY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1721047060; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=Sw82UZxyldCsryWuHDMLPvXTfZcc5DZM9+qg13v3L14=; b=aZ9NTsn817itK9yhnXb+mHmg7VVxPuUTG5wDPqnHJJkmGj4j6Vs5j0LBJBMZ7DdRE/4M92wP8oAf0vE6YUrKfGMlje8nEljJc5FZCeNjtRkTpgG1jjz9BMaRlIHKhNJ9O4VU9zu/+hSroSFq8zIDaCBr03LpEHK1Z/HCOrMo2yA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=quarantine dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1721047060759466.79999381945936; Mon, 15 Jul 2024 05:37:40 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sTKwk-0001ai-Ie; Mon, 15 Jul 2024 08:36:50 -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 1sTKwg-0001XC-Gr; Mon, 15 Jul 2024 08:36:46 -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 1sTKwd-0002X0-GJ; Mon, 15 Jul 2024 08:36:46 -0400 Received: from [130.117.225.1] (helo=dev005.ch-qa.vzint.dev) by relay.virtuozzo.com with esmtp (Exim 4.96) (envelope-from ) id 1sTKvk-00CsrT-1j; Mon, 15 Jul 2024 14:36:29 +0200 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=virtuozzo.com; s=relay; h=MIME-Version:Message-Id:Date:Subject:From: Content-Type; bh=Sw82UZxyldCsryWuHDMLPvXTfZcc5DZM9+qg13v3L14=; b=ktUA7jpDY/uB O89+tUGL6mOtPpdvIIsrgUVgQZalVpIvq9yIEyWpAwY2BT7XZkLGGCDJUfRY8VEmhCfbMWui+wl/S pywnZEW/JZpYLKx2ddi5ZZZBPPVzlkPkqv/SwDfHaKMI2699f9XIFHHont6eM56ORSq9cspjNjwnU ++qXMBlXf15lcsX+/XHXlDUzsl4OToEVBNHJ2OFOemAm/KRDLHbtne4Nw0KloXEPIYkYArbA7sRpg jd88qh3qNYGY/9n3wagq3UwRMFiwVwjrjyBLDio5YviEiTi2gqXZaIFGyYo2fiCLLUmPHBdk/KPi1 vuy4VIzcyyLDGyqIZvSTPQ==; From: Andrey Drobyshev To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, hreitz@redhat.com, kwolf@redhat.com, vsementsov@yandex-team.ru, pbonzini@redhat.com, eesposit@redhat.com, andrey.drobyshev@virtuozzo.com, den@virtuozzo.com Subject: [PATCH v2 3/2] scripts: add filev2p.py script for mapping virtual file offsets mapping Date: Mon, 15 Jul 2024 15:36:36 +0300 Message-Id: <20240715123636.619714-1-andrey.drobyshev@virtuozzo.com> X-Mailer: git-send-email 2.39.3 In-Reply-To: <20240712094617.565237-1-andrey.drobyshev@virtuozzo.com> References: <20240712094617.565237-1-andrey.drobyshev@virtuozzo.com> 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=andrey.drobyshev@virtuozzo.com; helo=relay.virtuozzo.com X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, 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-ZohoMail-DKIM: pass (identity @virtuozzo.com) X-ZM-MESSAGEID: 1721047062090116600 Content-Type: text/plain; charset="utf-8" The script is basically a wrapper around "filefrag" utility. This might be used to map virtual offsets within the file to the underlying block device offsets. In addition, a chunk size might be specified, in which case a list of such mappings will be obtained: $ scripts/filev2p.py -s 100M /sparsefile 1768M 1853882368..1895825407 (file) -> 16332619776..16374562815 (/dev/sda4) ->= 84492156928..84534099967 (/dev/sda) 1895825408..1958739967 (file) -> 17213591552..17276506111 (/dev/sda4) ->= 85373128704..85436043263 (/dev/sda) This could come in handy when we need to map a certain piece of data within a file inside VM to the same data within the image on the host (e.g. physical offset on VM's /dev/sda would be the virtual offset within QCOW2 image). Note: as of now the script only works with the files located on plain partitions, i.e. it doesn't work with partitions built on top of LVM. Partitions on LVM would require another level of mapping. Signed-off-by: Andrey Drobyshev --- scripts/filev2p.py | 311 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 311 insertions(+) create mode 100755 scripts/filev2p.py diff --git a/scripts/filev2p.py b/scripts/filev2p.py new file mode 100755 index 0000000000..3bd7d18b5e --- /dev/null +++ b/scripts/filev2p.py @@ -0,0 +1,311 @@ +#!/usr/bin/env python3 +# +# Map file virtual offset to the offset on the underlying block device. +# Works by parsing 'filefrag' output. +# +# Copyright (c) 2024 Virtuozzo International GmbH. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +import argparse +import os +import subprocess +import re +import sys + +from bisect import bisect_right +from collections import namedtuple +from dataclasses import dataclass +from shutil import which +from stat import S_ISBLK + + +Partition =3D namedtuple('Partition', ['partpath', 'diskpath', 'part_offt'= ]) + + +@dataclass +class Extent: + '''Class representing an individual file extent. + + This is basically a piece of data within the file which is located + consecutively (i.e. not sparsely) on the underlying block device. + ''' + + log_start: int + log_end: int + phys_start: int + phys_end: int + length: int + partition: Partition + + @property + def disk_start(self): + 'Number of the first byte of this extent on the whole disk (/dev/s= da)' + return self.partition.part_offt + self.phys_start + + @property + def disk_end(self): + 'Number of the last byte of this extent on the whole disk (/dev/sd= a)' + return self.partition.part_offt + self.phys_end + + def __str__(self): + ischunk =3D self.log_end > self.log_start + maybe_end =3D lambda s: f'..{s}' if ischunk else '' + return '%s%s (file) -> %s%s (%s) -> %s%s (%s)' % ( + self.log_start, maybe_end(self.log_end), + self.phys_start, maybe_end(self.phys_end), self.partition.part= path, + self.disk_start, maybe_end(self.disk_end), self.partition.disk= path + ) + + @classmethod + def ext_slice(cls, bigger_ext, start, end): + '''Constructor for the Extent class from a bigger extent. + + Return Extent instance which is a slice of @bigger_ext contained + within the range [start, end]. + ''' + + assert start >=3D bigger_ext.log_start + assert end <=3D bigger_ext.log_end + + if start =3D=3D bigger_ext.log_start and end =3D=3D bigger_ext.log= _end: + return bigger_ext + + phys_start =3D bigger_ext.phys_start + (start - bigger_ext.log_sta= rt) + phys_end =3D bigger_ext.phys_end - (bigger_ext.log_end - end) + length =3D end - start + 1 + + return cls(start, end, phys_start, phys_end, length, + bigger_ext.partition) + + +def run_cmd(cmd: str) -> str: + '''Wrapper around subprocess.run. + + Returns stdout in case of success, emits en error and exits in case + of failure. + ''' + + proc =3D subprocess.run(cmd, stdout=3Dsubprocess.PIPE, stderr=3Dsubpro= cess.PIPE, + check=3DFalse, shell=3DTrue) + if proc.stderr is not None: + stderr =3D f'\n{proc.stderr.decode().strip()}' + else: + stderr =3D '' + + if proc.returncode: + sys.exit(f'Error: Command "{cmd}" returned {proc.returncode}:{stde= rr}') + + return proc.stdout.decode().strip() + + +def parse_size(offset: str) -> int: + 'Convert human readable size to bytes' + + suffixes =3D { + **dict.fromkeys(['k', 'K', 'Kb', 'KB', 'KiB'], 2 ** 10), + **dict.fromkeys(['m', 'M', 'Mb', 'MB', 'MiB'], 2 ** 20), + **dict.fromkeys(['g', 'G', 'Gb', 'GB', 'GiB'], 2 ** 30), + **dict.fromkeys( ['T', 'Tb', 'TB', 'TiB'], 2 ** 40), + **dict.fromkeys([''], 1) + } + + sizematch =3D re.match(r'^([0-9]+)\s*([a-zA-Z]*)$', offset) + if not bool(sizematch): + sys.exit(f'Error: Couldn\'t parse size "{offset}". Pass offset ' + 'either in bytes or in format 1K, 2M, 3G') + + num, suff =3D sizematch.groups() + num =3D int(num) + + mult =3D suffixes.get(suff) + if mult is None: + sys.exit(f'Error: Couldn\'t parse size "{offset}": ' + f'unknown suffix {suff}') + + return num * mult + + +def fpath2part(filename: str) -> str: + 'Get partition on which @filename is located (i.e. /dev/sda1).' + + partpath =3D run_cmd(f'df --output=3Dsource {filename} | tail -n+2') + if not os.path.exists(partpath) or not S_ISBLK(os.stat(partpath).st_mo= de): + sys.exit(f'Error: file {filename} is located on {partpath} which ' + 'isn\'t a block device') + return partpath + + +def part2dev(partpath: str, filename: str) -> str: + 'Get block device on which @partpath is located (i.e. /dev/sda).' + dev =3D run_cmd(f'lsblk -no PKNAME {partpath}') + diskpath =3D f'/dev/{dev}' + if not os.path.exists(diskpath) or not S_ISBLK(os.stat(diskpath).st_mo= de): + sys.exit(f'Error: file {filename} is located on {diskpath} which ' + 'isn\'t a block device') + return diskpath + + +def part2disktype(partpath: str) -> str: + 'Parse /proc/devices and get block device type for @partpath' + + major =3D os.major(os.stat(partpath).st_rdev) + assert major + with open('/proc/devices', encoding=3D'utf-8') as devf: + for line in reversed(list(devf)): + # Our major cannot be absent among block devs + if line.startswith('Block'): + break + devmajor, devtype =3D line.strip().split() + if int(devmajor) =3D=3D major: + return devtype + + sys.exit('Error: We haven\'t found major {major} in /proc/devices, ' + 'and that can\'t be') + + +def get_part_offset(part: str, disk: str) -> int: + 'Get offset in bytes of the partition @part on the block device @disk.' + + lines =3D run_cmd(f'fdisk -l {disk} | egrep "^(Units|{part})"').splitl= ines() + + unitmatch =3D re.match('^.* =3D ([0-9]+) bytes$', lines[0]) + if not bool(unitmatch): + sys.exit(f'Error: Couldn\'t parse "fdisk -l" output:\n{lines[0]}') + secsize =3D int(unitmatch.group(1)) + + part_offt =3D int(lines[1].split()[1]) + return part_offt * secsize + + +def parse_frag_line(line: str, partition: Partition) -> Extent: + 'Construct Extent instance from a "filefrag" output line.' + + nums =3D [int(n) for n in re.findall(r'[0-9]+', line)] + + log_start =3D nums[1] + log_end =3D nums[2] + phys_start =3D nums[3] + phys_end =3D nums[4] + length =3D nums[5] + + assert log_start < log_end + assert phys_start < phys_end + assert (log_end - log_start + 1) =3D=3D (phys_end - phys_start + 1) = =3D=3D length + + return Extent(log_start, log_end, phys_start, phys_end, length, partit= ion) + + +def preliminary_checks(args: argparse.Namespace) -> None: + 'A bunch of checks to emit an error and exit at the earlier stage.' + + if which('filefrag') is None: + sys.exit('Error: Program "filefrag" doesn\'t exist') + + if not os.path.exists(args.filename): + sys.exit(f'Error: File {args.filename} doesn\'t exist') + + args.filesize =3D os.path.getsize(args.filename) + if args.offset >=3D args.filesize: + sys.exit(f'Error: Specified offset {args.offset} exceeds ' + f'file size {args.filesize}') + if args.size and (args.offset + args.size > args.filesize): + sys.exit(f'Error: Chunk of size {args.size} at offset ' + f'{args.offset} exceeds file size {args.filesize}') + + args.partpath =3D fpath2part(args.filename) + args.disktype =3D part2disktype(args.partpath) + if args.disktype not in ('sd', 'virtblk'): + sys.exit(f'Error: Cannot analyze files on {args.disktype} disks') + args.diskpath =3D part2dev(args.partpath, args.filename) + args.part_offt =3D get_part_offset(args.partpath, args.diskpath) + + +def get_extent_maps(args: argparse.Namespace) -> list[Extent]: + 'Run "filefrag", parse its output and return a list of Extent instance= s.' + + lines =3D run_cmd(f'filefrag -b1 -v {args.filename}').splitlines() + + ffinfo_re =3D re.compile('.* is ([0-9]+) .*of ([0-9]+) bytes') + ff_size, ff_block =3D re.match(ffinfo_re, lines[1]).groups() + + # Paranoia checks + if int(ff_size) !=3D args.filesize: + sys.exit('Error: filefrag and os.path.getsize() report different ' + f'sizes: {ff_size} and {args.filesize}') + if int(ff_block) !=3D 1: + sys.exit(f'Error: "filefrag -b1" invoked, but block size is {ff_bl= ock}') + + partition =3D Partition(args.partpath, args.diskpath, args.part_offt) + + # Fill extents list from the output + extents =3D [] + for line in lines: + if not re.match(r'^\s*[0-9]+:', line): + continue + extents +=3D [parse_frag_line(line, partition)] + + chunk_start =3D args.offset + chunk_end =3D args.offset + args.size - 1 + ext_offsets =3D [ext.log_start for ext in extents] + start_ind =3D bisect_right(ext_offsets, chunk_start) - 1 + end_ind =3D bisect_right(ext_offsets, chunk_end) - 1 + + res_extents =3D extents[start_ind : end_ind + 1] + for i, ext in enumerate(res_extents): + start =3D max(chunk_start, ext.log_start) + end =3D min(chunk_end, ext.log_end) + res_extents[i] =3D Extent.ext_slice(ext, start, end) + + return res_extents + + +def parse_args() -> argparse.Namespace: + 'Define program arguments and parse user input.' + + parser =3D argparse.ArgumentParser(description=3D''' +Map file offset to physical offset on the block device + +With --size provided get a list of mappings for the chunk''', + formatter_class=3Dargparse.RawTextHelpFormatter) + + parser.add_argument('filename', type=3Dstr, help=3D'filename to proces= s') + parser.add_argument('offset', type=3Dstr, + help=3D'logical offset inside the file') + parser.add_argument('-s', '--size', required=3DFalse, type=3Dstr, + help=3D'size of the file chunk to get offsets for') + args =3D parser.parse_args() + + args.offset =3D parse_size(args.offset) + if args.size: + args.size =3D parse_size(args.size) + else: + # When no chunk size is provided (only offset), it's equivalent to + # chunk size =3D=3D 1 + args.size =3D 1 + + return args + + +def main() -> int: + args =3D parse_args() + preliminary_checks(args) + extents =3D get_extent_maps(args) + for ext in extents: + print(ext) + + +if __name__ =3D=3D '__main__': + sys.exit(main()) --=20 2.39.3