From nobody Sun May 5 14:34:40 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 ARC-Seal: i=1; a=rsa-sha256; t=1596595207; cv=none; d=zohomail.com; s=zohoarc; b=N8opU66zLbeX6OicEFADuJ5KPBdVE/BqaECZnCGj8A5vGtcKuxEOLKKEIAtnmg/E0gxcplpnuRKfgbhGm0NXA3riWoxUmKhhbAEfSnZOMOSJs9xYodS+gMXMQHSGh8AfeRMdC0V2LPf0Ez6r+4gdFCA7buqzbnUIV2LBO6lomL8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1596595207; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Sender:Subject:To; bh=AGVfURdzZhmBkjTKl2Z9mJN1FxBlzwGDdeHZtk+mFAM=; b=h720e9f+oEr5MazJZ/MdSzthjRV+Xgfx6Tvtz1G/D5E75LXnSPL/Y9vLMWEutCq+EaHQNup3+kWur2lIljY357FiXID+3+Z4nA6b+bmeQPqirh0N74LzYHc259yq4eHJpurtK6ehucoDjSYLYgU/QlLYUEPVyb/acy2dPj55phc= ARC-Authentication-Results: i=1; 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 Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1596595207366996.6346741642508; Tue, 4 Aug 2020 19:40:07 -0700 (PDT) Received: from localhost ([::1]:51422 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1k39LZ-0005dS-Nw for importer@patchew.org; Tue, 04 Aug 2020 22:40:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:40974) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k39Kx-00058M-BT; Tue, 04 Aug 2020 22:39:27 -0400 Received: from szxga07-in.huawei.com ([45.249.212.35]:38428 helo=huawei.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1k39Ku-0004Jl-2x; Tue, 04 Aug 2020 22:39:27 -0400 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 9D0F57869BB57C9B5EE2; Wed, 5 Aug 2020 10:39:09 +0800 (CST) Received: from localhost (10.174.185.104) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.487.0; Wed, 5 Aug 2020 10:38:59 +0800 From: Ying Fang To: , , Subject: [PATCH] qcow2: flush qcow2 l2 meta for new allocated clusters Date: Wed, 5 Aug 2020 10:38:26 +0800 Message-ID: <20200805023826.184-1-fangying1@huawei.com> X-Mailer: git-send-email 2.26.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.174.185.104] X-CFilter-Loop: Reflected 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=45.249.212.35; envelope-from=fangying1@huawei.com; helo=huawei.com X-detected-operating-system: by eggs.gnu.org: First seen = 2020/08/04 22:39:10 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -41 X-Spam_score: -4.2 X-Spam_bar: ---- X-Spam_report: (-4.2 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H4=-0.01, RCVD_IN_MSPIKE_WL=-0.01, 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: , Cc: alex.chen@huawei.com, fangying , zhang.zhanghailiang@huawei.com, qemu-block@nongnu.org Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" Content-Type: text/plain; charset="utf-8" From: fangying When qemu or qemu-nbd process uses a qcow2 image and configured with 'cache =3D none', it will write to the qcow2 image with a cache to cache L2 tables, however the process will not use L2 tables without explicitly calling the flush command or closing the mirror flash into the disk. Which may cause the disk data inconsistent with the written data for a long time. If an abnormal process exit occurs here, the issued written data will be lost. Therefore, in order to keep data consistency we need to flush the changes to the L2 entry to the disk in time for the newly allocated cluster. Signed-off-by: Ying Fang diff --git a/block/qcow2-cache.c b/block/qcow2-cache.c index 7444b9c..ab6e812 100644 --- a/block/qcow2-cache.c +++ b/block/qcow2-cache.c @@ -266,6 +266,22 @@ int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache= *c) return result; } =20 +#define L2_ENTRIES_PER_SECTOR 64 +int qcow2_cache_l2_write_entry(BlockDriverState *bs, Qcow2Cache *c, + void *table, int index, int num) +{ + int ret; + int i =3D qcow2_cache_get_table_idx(c, table); + int start_sector =3D index / L2_ENTRIES_PER_SECTOR; + int end_sector =3D (index + num - 1) / L2_ENTRIES_PER_SECTOR; + int nr_sectors =3D end_sector - start_sector + 1; + ret =3D bdrv_pwrite(bs->file, + c->entries[i].offset + start_sector * BDRV_SECTOR_SI= ZE, + table + start_sector * BDRV_SECTOR_SIZE, + nr_sectors * BDRV_SECTOR_SIZE); + return ret; +} + int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, Qcow2Cache *dependency) { diff --git a/block/qcow2-cluster.c b/block/qcow2-cluster.c index a677ba9..ae49a83 100644 --- a/block/qcow2-cluster.c +++ b/block/qcow2-cluster.c @@ -998,6 +998,9 @@ int qcow2_alloc_cluster_link_l2(BlockDriverState *bs, Q= CowL2Meta *m) } =20 =20 + ret =3D qcow2_cache_l2_write_entry(bs, s->l2_table_cache, l2_slice, + l2_index, m->nb_clusters); + qcow2_cache_put(s->l2_table_cache, (void **) &l2_slice); =20 /* diff --git a/block/qcow2.h b/block/qcow2.h index 7ce2c23..168ab59 100644 --- a/block/qcow2.h +++ b/block/qcow2.h @@ -748,6 +748,8 @@ int qcow2_cache_destroy(Qcow2Cache *c); void qcow2_cache_entry_mark_dirty(Qcow2Cache *c, void *table); int qcow2_cache_flush(BlockDriverState *bs, Qcow2Cache *c); int qcow2_cache_write(BlockDriverState *bs, Qcow2Cache *c); +int qcow2_cache_l2_write_entry(BlockDriverState *bs, Qcow2Cache *c, + void *table, int index, int num); int qcow2_cache_set_dependency(BlockDriverState *bs, Qcow2Cache *c, Qcow2Cache *dependency); void qcow2_cache_depends_on_flush(Qcow2Cache *c); --=20 1.8.3.1