From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-18.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,MSGID_FROM_MTA_HEADER,SPF_HELO_NONE, SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91FB1C433B4 for ; Thu, 22 Apr 2021 16:50:37 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id CFB086124B for ; Thu, 22 Apr 2021 16:50:36 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org CFB086124B Authentication-Results: mail.kernel.org; dmarc=fail (p=quarantine dis=none) header.from=virtuozzo.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:39934 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lZcXD-0007Zk-MJ for qemu-devel@archiver.kernel.org; Thu, 22 Apr 2021 12:50:35 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55038) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lZcEe-0007Rc-B2; Thu, 22 Apr 2021 12:31:24 -0400 Received: from mail-eopbgr40129.outbound.protection.outlook.com ([40.107.4.129]:3204 helo=EUR03-DB5-obe.outbound.protection.outlook.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lZcEc-0003yB-GI; Thu, 22 Apr 2021 12:31:24 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=PBbBR3KDnQPnHraagRXonaEl5lPlIRcwwIfAtHPnmQdSPoQ5yjza8dMtVCPiU+Xa1NQyszJx5sQSNLJVe30DBM95CcwiM1mZdbRgXrECIqKY6XdDeXqfPr6ntWOU9KzsL1PL/GwTlGJzk270oETuZUd5YvpUO1iUyTzgTMmM7KmxJiQcRpVyCbI2L5Sz8to0x5b8HSc9Mu0fJuoU7US0SfIoEjAJsrjvz7EqcEu8JmHxC57D8A2qboHIX5YiBMglRiIfsxNsfrykBQtn7RAwVqW45InGG8ezCfaCbS53Q0nh3nAFCMRDPZhY0kJ8F3DFqRVvKf5n0yu7Yf/QBki2YA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JVPrbCXOLoGPkLQN1znGuGj0mBCvZjATpgjDjqHUy+I=; b=h1/4XYs+eWfgsFE4jjYS/8WZVCzemgyXV53Iz5tVWvo6zY1CWzx7BsVVijPGEo2h9bkLa5BTG061wap3DaFhGcd1ViIpKxIjMkskVJB2bI7/xQCqNE0oFfP0iWwP/b4kVtHSRKoTCDtsuQOBN+n/ED2eXqKAhO4z7Ynf3TrZJlHZYx1n1FYLeqZXzYl6PqceHwm6xxv+NqXVmL/mzcb7bbMKbTY3dUBnHJfp2tYrOR+TZmkE5Z1RdFsc9jZLmqPZnLnZdczMvUIYzSKDtQH/CsQWTBS25YhymD/0UQqGpQ3syaJbpSj99afJFWcm1+65eOQL3FUJzH5ex28gvTujqQ== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=virtuozzo.com; dmarc=pass action=none header.from=virtuozzo.com; dkim=pass header.d=virtuozzo.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=virtuozzo.com; s=selector2; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=JVPrbCXOLoGPkLQN1znGuGj0mBCvZjATpgjDjqHUy+I=; b=bhq1psvbMLDNBszJ43MTg+JLS5vhfcMkOzz0TLpVH5GKsWjCAIVq/AgmN4YlE8j6zPvh3Uo0phKEDjjf9hsGj5pYYBx4qB7+daAhzEFYzqIcIT9UtiQgUlnKa/3WNIWVOcWwb1h0aBHLHkyKodFyyP2mZokxTgNeaa56naqhtWI= Authentication-Results: nongnu.org; dkim=none (message not signed) header.d=none;nongnu.org; dmarc=none action=none header.from=virtuozzo.com; Received: from AM7PR08MB5494.eurprd08.prod.outlook.com (2603:10a6:20b:dc::15) by AM6PR08MB3861.eurprd08.prod.outlook.com (2603:10a6:20b:80::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4065.21; Thu, 22 Apr 2021 16:31:09 +0000 Received: from AM7PR08MB5494.eurprd08.prod.outlook.com ([fe80::b403:c1a9:6bb7:133]) by AM7PR08MB5494.eurprd08.prod.outlook.com ([fe80::b403:c1a9:6bb7:133%7]) with mapi id 15.20.4065.021; Thu, 22 Apr 2021 16:31:09 +0000 From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Cc: qemu-devel@nongnu.org, mreitz@redhat.com, kwolf@redhat.com, vsementsov@virtuozzo.com, den@openvz.org Subject: [PATCH v6 08/12] qcow2: introduce is_cluster_free() helper Date: Thu, 22 Apr 2021 19:30:42 +0300 Message-Id: <20210422163046.442932-9-vsementsov@virtuozzo.com> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210422163046.442932-1-vsementsov@virtuozzo.com> References: <20210422163046.442932-1-vsementsov@virtuozzo.com> Content-Transfer-Encoding: 8bit Content-Type: text/plain X-Originating-IP: [185.215.60.222] X-ClientProxiedBy: HE1PR0501CA0006.eurprd05.prod.outlook.com (2603:10a6:3:1a::16) To AM7PR08MB5494.eurprd08.prod.outlook.com (2603:10a6:20b:dc::15) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from localhost.localdomain (185.215.60.222) by HE1PR0501CA0006.eurprd05.prod.outlook.com (2603:10a6:3:1a::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4042.16 via Frontend Transport; Thu, 22 Apr 2021 16:31:08 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 31add05a-d114-4c52-1b7d-08d905ac08cb X-MS-TrafficTypeDiagnostic: AM6PR08MB3861: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:873; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: 4ZG3pDIbdFwGloimivqAD+Wzc2PW6EAJq4LjVZRWKYX5nJkh87WZq6tNqkj4WPv9bZkSjo/mOBMDPBFeOtuL/7pjESV7FAnFCfrgj8gQvWsVfGXjUlPT9SHQd+2NEwKCQo8ryl4Ly0gBVqJcH035OCfKn4WshSTcobkN0yComgqJuEtWoZoY74ShBPfK1CchHdkgYvMrtMkibM/i+lb8N+P7qZj3gSaEFFA+Li7QnVgB1LgvMzdWpeFzjFSpHPAHzLfDAOixjZRqvlE4WswzT4Gos9fdvRThTTh9e015HFusUmn/myILIHd5Th5zCXvmMuetzUmFi9OOmQdzbRN6TkkAGei4OsrRJRwOlvkyu9+MrHQ0fG9vP26Pwt8e67Q9Dz1UVchnVq/MSv2eE+QCDrBtFHQOpSAE1KnceNf7IhfDKeD2ldhby6DHGAB7SztZ0rmwZxAloUB3et2cqWU1h45TtrvfvLjbvhnTGnASC3BWnb+CJ/zgew2rhnriGF87S/FmatRxdgh5qaeQsjtu/PfufLZ9307Aj97f8KKpSN4ffM+R1bk+IlTJVmphzNOnAY5LTtqPTSsGifEW7EcDHXPWTvpo8lBXzcPF3ENHDHkz6vbfLn1IPLPW0mnJgKh8hGXAABfBN/EK6wtkBYqASiJLbNLTUjIzTavrQMj965QMudYuOsTO3dsUbbhvXVIm X-Forefront-Antispam-Report: CIP:255.255.255.255; CTRY:; LANG:en; SCL:1; SRV:; IPV:NLI; SFV:NSPM; H:AM7PR08MB5494.eurprd08.prod.outlook.com; PTR:; CAT:NONE; SFS:(4636009)(376002)(346002)(136003)(366004)(39840400004)(396003)(6666004)(4326008)(107886003)(26005)(6486002)(956004)(2906002)(83380400001)(2616005)(52116002)(316002)(478600001)(8936002)(66476007)(6916009)(66556008)(66946007)(38100700002)(1076003)(38350700002)(5660300002)(6506007)(8676002)(186003)(6512007)(16526019)(36756003)(86362001)(69590400013); DIR:OUT; SFP:1102; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?aKs4qWuFZ+CInrms6+kRI9PKL3oYm9nIrktAI32JZf+Puv1uBM+FdyIpk6Qf?= =?us-ascii?Q?15Vku7UBUPMKvwtx7cx2kVuJPHRvwne9Eu3AhXqKJcBVeKI+3lCEN0BtMqm9?= =?us-ascii?Q?EGGuwHd9cWoRAjNJlPO2TsHvwBv1Qfqhh8WsL38Q+SvBg3I3scckfbInFhBx?= =?us-ascii?Q?ohxIduyIj4fGgmD1c4emD1YBTbO+qWdVUkxcIQiTxQwiL2v+BEgTBL+dRTkr?= =?us-ascii?Q?EGQKfJhaHMhJAwiD3ywAqV+QCqg/cad6Tz4Qvu+NJtJWJBw0sNeVM71ZooQ1?= =?us-ascii?Q?FTDZN/2q08lOxLseS/WCUjytRTnNDMNF2d2Fy8zn6se8HnPTUq2jwQaMPKz6?= =?us-ascii?Q?dHqRPUZeqL+DC8dxjR749Ij8ZWdf86Q4yF26o8B2XhZSev4dLnbk1FKqp+rh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zyiCfkcWwCtSbNuicX0WQQ?= =?us-ascii?Q?Jvuodb/viSxvUdLc39DGBKu3?= X-OriginatorOrg: virtuozzo.com X-MS-Exchange-CrossTenant-Network-Message-Id: 31add05a-d114-4c52-1b7d-08d905ac08cb X-MS-Exchange-CrossTenant-AuthSource: AM7PR08MB5494.eurprd08.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 22 Apr 2021 16:31:09.1072 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 0bc7f26d-0264-416e-a6fc-8352af79c58f X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: DExefTgXcySTH4Dw4r9sr1xF97rK3EWUw52WBWUj6SUy862vivGDs4rgYRL82KV17jfTIydMWS+r6Q5BkbvIe9pAa1wN7wfrUloz54b7tK4= X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM6PR08MB3861 Received-SPF: pass client-ip=40.107.4.129; envelope-from=vsementsov@virtuozzo.com; helo=EUR03-DB5-obe.outbound.protection.outlook.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, MSGID_FROM_MTA_HEADER=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_PASS=-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.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" We are going to change the concept of "free host cluster", so let's clarify it now and add a helper, which we will modify later. Signed-off-by: Vladimir Sementsov-Ogievskiy --- block/qcow2-refcount.c | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/block/qcow2-refcount.c b/block/qcow2-refcount.c index ad021aab7a..72e6d1efd7 100644 --- a/block/qcow2-refcount.c +++ b/block/qcow2-refcount.c @@ -962,13 +962,32 @@ int qcow2_update_cluster_refcount(BlockDriverState *bs, /* cluster allocation functions */ +/* + * Cluster is free when its refcount is 0 + * + * Return < 0 if failed to get refcount + * 0 if cluster is not free + * 1 if cluster is free + */ +static int is_cluster_free(BlockDriverState *bs, int64_t cluster_index) +{ + int ret; + uint64_t refcount; + + ret = qcow2_get_refcount(bs, cluster_index, &refcount); + if (ret < 0) { + return ret; + } + + return refcount == 0; +} /* return < 0 if error */ static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size, uint64_t max) { BDRVQcow2State *s = bs->opaque; - uint64_t i, nb_clusters, refcount; + uint64_t i, nb_clusters; int ret; /* We can't allocate clusters if they may still be queued for discard. */ @@ -980,11 +999,11 @@ static int64_t alloc_clusters_noref(BlockDriverState *bs, uint64_t size, retry: for(i = 0; i < nb_clusters; i++) { uint64_t next_cluster_index = s->free_cluster_index++; - ret = qcow2_get_refcount(bs, next_cluster_index, &refcount); + ret = is_cluster_free(bs, next_cluster_index); if (ret < 0) { return ret; - } else if (refcount != 0) { + } else if (!ret) { goto retry; } } @@ -1031,7 +1050,7 @@ int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, int64_t nb_clusters) { BDRVQcow2State *s = bs->opaque; - uint64_t cluster_index, refcount; + uint64_t cluster_index; uint64_t i; int ret; @@ -1044,10 +1063,10 @@ int64_t qcow2_alloc_clusters_at(BlockDriverState *bs, uint64_t offset, /* Check how many clusters there are free */ cluster_index = offset >> s->cluster_bits; for(i = 0; i < nb_clusters; i++) { - ret = qcow2_get_refcount(bs, cluster_index++, &refcount); + ret = is_cluster_free(bs, cluster_index++); if (ret < 0) { return ret; - } else if (refcount != 0) { + } else if (!ret) { break; } } -- 2.29.2