From nobody Thu Mar 28 11:20:58 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=none dis=none) header.from=126.com ARC-Seal: i=1; a=rsa-sha256; t=1663770957; cv=none; d=zohomail.com; s=zohoarc; b=ivyxVFH2PsHVVW8q7gGBb7wNe2wQ3e1VyFcLD4zrFifjFdpzgIz8SFm3Ss/xB4PTfRk7hoG3vbgIx0PhpoeQNBLlLV4oSrlBRBr1SFGGU4+l0Ijy3zK3xIQuaVxENSn9VplHDFyOkEp4gIe5qTc5V9sltQIGBXbN9th26I916eA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1663770957; h=Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:Message-ID:Sender:Subject:To; bh=5kF/bmuqjE142pMlr1yOnrYSK7TVnzR7uQ0dqUNk6RA=; b=Fy89MuwRxMPNu0t//M1KzeT9jCEllOi8nA1qiUD7Wrg8UpTimf+a8EKDpLcSSArOu6wvRl0QJOl71YiwDMmG9hurQtKOXf9CC1PQ9n+g6TVekMUfJaYmh26gxw0npZwilmBo4yZ8T8DXVEKBI1QcZss23Nfbty83AtEL09yRMz8= 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=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1663770957917839.8303898571635; Wed, 21 Sep 2022 07:35:57 -0700 (PDT) Received: from localhost ([::1]:32826 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ob0pP-0007Kp-Uj for importer@patchew.org; Wed, 21 Sep 2022 10:35:55 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:38546) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oav5h-0003Xl-I9 for qemu-devel@nongnu.org; Wed, 21 Sep 2022 04:28:27 -0400 Received: from m15111.mail.126.com ([220.181.15.111]:29547) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oav5b-0006Wk-1p for qemu-devel@nongnu.org; Wed, 21 Sep 2022 04:28:18 -0400 Received: from master.localdomain (unknown [117.160.246.157]) by smtp1 (Coremail) with SMTP id C8mowAB3eIzrwypjMH8RCA--.64443S2; Wed, 21 Sep 2022 15:57:32 +0800 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=From:Subject:Date:Message-Id; bh=5kF/bmuqjE142pMlr1 yOnrYSK7TVnzR7uQ0dqUNk6RA=; b=a4g0oa7HO3wgf9Nvhg/1QYiMbkNsvxGRhj qt2o3fMnA/kSAGCXwzbxB7upA8j00jD1Zq36jVpiwCFUTbCPPX29oV2Gd4jUQ4JX U4m7CoOuGPZtCBVYezkX8FViM9kiWuA6qppzxpbOg0311YNM5CNfKlaXrEeQLafo U5RKX6WGU= From: songlinfeng To: qemu-devel@nongnu.org Cc: kwolf@redhat.com, songlinfeng Subject: [PATCH] try to find out which cluster allocated in qcow2 Date: Wed, 21 Sep 2022 15:57:30 +0800 Message-Id: <1663747050-122900-1-git-send-email-slf553@126.com> X-Mailer: git-send-email 1.8.3.1 X-CM-TRANSID: C8mowAB3eIzrwypjMH8RCA--.64443S2 X-Coremail-Antispam: 1Uf129KBjvJXoW7uw1UuFW3Zr4UCF1kZr18Xwb_yoW5Jry8pa ySkr13Jr4FqFy3WrsIkr1DXa1fGw4kCF1UG39rXr1jvFnxCr4xu34kKFZ0krWkWr17XFs0 qrs5tFW8uF1xWFJanT9S1TB71UUUUUUqnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x0pEs2-bUUUUU= X-Originating-IP: [117.160.246.157] X-CM-SenderInfo: 5voikkat6rjloofrz/1tbiLQ2DtVpD-lEK+wAAsR 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=220.181.15.111; envelope-from=slf553@126.com; helo=m15111.mail.126.com X-Spam_score_int: -17 X-Spam_score: -1.8 X-Spam_bar: - X-Spam_report: (-1.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, FREEMAIL_ENVFROM_END_DIGIT=0.25, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Wed, 21 Sep 2022 10:31:51 -0400 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" X-ZohoMail-DKIM: pass (identity @126.com) X-ZM-MESSAGEID: 1663770959963100001 Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" In our project,we want to full backup a disk only allocated area,but qmp bl= ock-dity-block-add can create a bitmap with all zero,so we can't find out w= hich cluster is allocated.in qcow2,I think l2_table can help me find out wh= ich cluster should be backup. Signed-off-by: songlinfeng --- block/qcow2.c | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ block/qcow2.h | 1 + 2 files changed, 50 insertions(+) diff --git a/block/qcow2.c b/block/qcow2.c index c6c6692..944cf4f 100644 --- a/block/qcow2.c +++ b/block/qcow2.c @@ -4194,6 +4194,55 @@ fail: return ret; } =20 +void qcow2_get_cluster(BlockDriverState *bs, uint64_t size) +{ + BDRVQcow2State *s =3D bs->opaque; + int l1_size =3D s->l1_size; + int cluster_size =3D s->cluster_size; + int i; + int j; + uint64_t *l2_table =3D (uint64_t *)malloc(cluster_size); + int l2_entries =3D cluster_size / sizeof(uint64_t); + int total =3D (size + cluster_size + 1) / cluster_size; + for (i =3D 0; i < l1_size; i++) { + uint64_t l1_entry =3D s->l1_table[i]; + uint64_t l2_offset =3D l1_entry & L1E_OFFSET_MASK; + if (l2_offset =3D=3D 0) { + if (l2_entries < total) { + char *buf =3D (char *)malloc(l2_entries * sizeof(char)); + memset(buf, '0', l2_entries); + printf("%s", buf); + free(buf); + total -=3D l2_entries; + } else { + char *buf =3D (char *)malloc(total * sizeof(char)); + memset(buf, '0', total); + printf("%s", buf); + free(buf); + total -=3D total; + } + continue; + } + int ret =3D bdrv_pread(bs->file, l2_offset, l2_table, cluster_size= ); + if (ret < 0) { + error_report("can't get l2_table"); + abort(); + } + for (j =3D 0; j < l2_entries; j++) { + if (total) { + if (l2_table[j] =3D=3D 0) { + printf("0"); + } else { + printf("1"); + } + total--; + } + } + } + free(l2_table); + printf("\n"); +} + static int coroutine_fn qcow2_co_truncate(BlockDriverState *bs, int64_t of= fset, bool exact, PreallocMode preallo= c, BdrvRequestFlags flags, Error **= errp) diff --git a/block/qcow2.h b/block/qcow2.h index ba436a8..7079916 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -998,6 +998,7 @@ int qcow2_co_remove_persistent_dirty_bitmap(BlockDriver= State *bs, const char *name, Error **errp); bool qcow2_supports_persistent_dirty_bitmap(BlockDriverState *bs); +void qcow2_get_cluster(BlockDriverState *bs, uint64_t size); uint64_t qcow2_get_persistent_dirty_bitmap_size(BlockDriverState *bs, uint32_t cluster_size); =20 --=20 1.8.3.1