From patchwork Mon Jul 1 05:12:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Qu Wenruo X-Patchwork-Id: 11024843 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id B37ED13B1 for ; Mon, 1 Jul 2019 05:28:38 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C54427F92 for ; Mon, 1 Jul 2019 05:28:38 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 90999283A5; Mon, 1 Jul 2019 05:28:38 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI,UNPARSEABLE_RELAY autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC5B827F92 for ; Mon, 1 Jul 2019 05:28:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727340AbfGAF2c (ORCPT ); Mon, 1 Jul 2019 01:28:32 -0400 Received: from m4a0039g.houston.softwaregrp.com ([15.124.2.85]:52010 "EHLO m4a0039g.houston.softwaregrp.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727298AbfGAF2c (ORCPT ); Mon, 1 Jul 2019 01:28:32 -0400 X-Greylist: delayed 935 seconds by postgrey-1.27 at vger.kernel.org; Mon, 01 Jul 2019 01:28:31 EDT Received: FROM m4a0039g.houston.softwaregrp.com (15.120.17.146) BY m4a0039g.houston.softwaregrp.com WITH ESMTP FOR linux-btrfs@vger.kernel.org; Mon, 1 Jul 2019 05:24:57 +0000 Received: from M4W0334.microfocus.com (2002:f78:1192::f78:1192) by M4W0334.microfocus.com (2002:f78:1192::f78:1192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10; Mon, 1 Jul 2019 05:12:48 +0000 Received: from NAM04-BN3-obe.outbound.protection.outlook.com (15.124.8.12) by M4W0334.microfocus.com (15.120.17.146) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.1591.10 via Frontend Transport; Mon, 1 Jul 2019 05:12:48 +0000 Received: from BY5PR18MB3266.namprd18.prod.outlook.com (10.255.163.207) by BY5PR18MB3155.namprd18.prod.outlook.com (10.255.136.224) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.2032.20; Mon, 1 Jul 2019 05:12:46 +0000 Received: from BY5PR18MB3266.namprd18.prod.outlook.com ([fe80::45a9:4750:5868:9bbe]) by BY5PR18MB3266.namprd18.prod.outlook.com ([fe80::45a9:4750:5868:9bbe%5]) with mapi id 15.20.2032.018; Mon, 1 Jul 2019 05:12:46 +0000 From: Qu Wenruo To: "linux-btrfs@vger.kernel.org" CC: James Harvey Subject: [PATCH v2] btrfs: inode: Don't compress if NODATASUM or NODATACOW set Thread-Topic: [PATCH v2] btrfs: inode: Don't compress if NODATASUM or NODATACOW set Thread-Index: AQHVL8ue7nuhfXyQVU+BGsD2wENnxQ== Date: Mon, 1 Jul 2019 05:12:46 +0000 Message-ID: <20190701051225.17957-1-wqu@suse.com> Accept-Language: zh-CN, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-clientproxiedby: SL2PR01CA0001.apcprd01.prod.exchangelabs.com (2603:1096:100:41::13) To BY5PR18MB3266.namprd18.prod.outlook.com (2603:10b6:a03:1a1::15) authentication-results: spf=none (sender IP is ) smtp.mailfrom=wqu@microfocus.com; x-ms-exchange-messagesentrepresentingtype: 1 x-mailer: git-send-email 2.22.0 x-originating-ip: [240e:3a1:c40:c630::aab] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 66bba85f-e422-4a1e-1c08-08d6fde2c075 x-microsoft-antispam: BCL:0;PCL:0;RULEID:(2390118)(7020095)(4652040)(8989299)(4534185)(4627221)(201703031133081)(201702281549075)(8990200)(5600148)(711020)(4605104)(1401327)(2017052603328)(7193020);SRVR:BY5PR18MB3155; x-ms-traffictypediagnostic: BY5PR18MB3155: x-ms-exchange-purlcount: 1 x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:8273; x-forefront-prvs: 00851CA28B x-forefront-antispam-report: SFV:NSPM;SFS:(10019020)(4636009)(396003)(346002)(136003)(376002)(39860400002)(366004)(54534003)(199004)(189003)(6506007)(386003)(52116002)(14454004)(7736002)(8936002)(81156014)(81166006)(8676002)(186003)(2351001)(2501003)(305945005)(102836004)(25786009)(1076003)(6916009)(2616005)(476003)(256004)(14444005)(486006)(2906002)(53936002)(6116002)(71190400001)(71200400001)(99286004)(42882007)(316002)(6512007)(6306002)(5660300002)(50226002)(68736007)(46003)(36756003)(66476007)(66556008)(64756008)(66446008)(66946007)(73956011)(6436002)(478600001)(6486002)(4326008)(5640700003);DIR:OUT;SFP:1102;SCL:1;SRVR:BY5PR18MB3155;H:BY5PR18MB3266.namprd18.prod.outlook.com;FPR:;SPF:None;LANG:en;PTR:InfoNoRecords;A:1;MX:1; received-spf: None (protection.outlook.com: microfocus.com does not designate permitted sender hosts) x-ms-exchange-senderadcheck: 1 x-microsoft-antispam-message-info: zq9jdUMb3ENMm/11C8eDGuiN80cMywEXmHXhk/OyXmlBBJZEuP2bD0nYaBSakHZwcBgF8LjSUlHt/Ft1793f4bkmyDxmTm2trTsJRck0w0EFzToCt77rVfz3JPbWBa9zNqp9D63/hM3MyH73RoQgZdLU5t5zYqjyrMSWau7BDVQaoMOGGQTAUq0hN3ly6HjqxBlqn/TO4yNCu74owKTu9TRrmrrQR8InQhUM7jeXJ5YIvHNb9A5GQ9U2KN69X/CIsOcwqr2SX24K01tYK6qv1DQC5F0v9XUhlI+hEV84GkFED/ufuEAQXRmMmxRWkeCdA3wwg+h9Pjh/CwXjL3xnT/9mBXX/KSPUoi/2xRm9L1Gs5+T/6LWLryPJP9Vg+C8w7avocG91sU1n2GI40bRxxxeeVcXB4/RwHF5qOKk8xA0= MIME-Version: 1.0 X-MS-Exchange-CrossTenant-Network-Message-Id: 66bba85f-e422-4a1e-1c08-08d6fde2c075 X-MS-Exchange-CrossTenant-originalarrivaltime: 01 Jul 2019 05:12:46.0776 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 856b813c-16e5-49a5-85ec-6f081e13b527 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: wqu@microfocus.com X-MS-Exchange-Transport-CrossTenantHeadersStamped: BY5PR18MB3155 X-OriginatorOrg: suse.com Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP As btrfs(5) specified: Note If nodatacow or nodatasum are enabled, compression is disabled. If NODATASUM or NODATACOW set, we should not compress the extent. Normally NODATACOW is detected properly in run_delalloc_range() so compression won't happen for NODATACOW. However for NODATASUM we don't have any check, and it can cause compressed extent without csum pretty easily, just by: mkfs.btrfs -f $dev mount $dev $mnt -o nodatasum touch $mnt/foobar mount -o remount,datasum,compress $mnt xfs_io -f -c "pwrite 0 128K" $mnt/foobar And in fact, we have a bug report about corrupted compressed extent without proper data checksum so even RAID1 can't recover the corruption. (https://bugzilla.kernel.org/show_bug.cgi?id=199707) Running compression without proper checksum could cause more damage when corruption happens, as compressed data could make the whole extent unreadable, so there is no need to allow compression for NODATACSUM. The fix will refactor the inode compression check into two parts: - inode_can_compress() As the hard requirement, checked at btrfs_run_delalloc_range(), so no compression will happen for NODATASUM inode at all. - inode_need_compress() As the soft requirement, checked at btrfs_run_delalloc_range() and compress_file_range(). Reported-by: James Harvey Signed-off-by: Qu Wenruo --- Changelog: v2: - Refactor inode_need_compress() into two functions - Refactor inode_need_compress() to return bool --- fs/btrfs/inode.c | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index a2aabdb85226..be1cabf35680 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -394,24 +394,49 @@ static noinline int add_async_extent(struct async_chunk *cow, return 0; } -static inline int inode_need_compress(struct inode *inode, u64 start, u64 end) +/* + * Check if the inode can accept compression. + * + * This checks for the hard requirement of compression, including CoW and + * checksum requirement. + */ +static inline bool inode_can_compress(struct inode *inode) +{ + if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW || + BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM) + return false; + return true; +} + +/* + * Check if the inode need compression. + * + * This checks for the soft requirement of compression. + */ +static inline bool inode_need_compress(struct inode *inode, u64 start, u64 end) { struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb); + if (!inode_can_compress(inode)) { + WARN(IS_ENABLED(CONFIG_BTRFS_DEBUG), + KERN_ERR "BTRFS: unexpected compression for ino %llu\n", + btrfs_ino(BTRFS_I(inode))); + return false; + } /* force compress */ if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) - return 1; + return true; /* defrag ioctl */ if (BTRFS_I(inode)->defrag_compress) - return 1; + return true; /* bad compression ratios */ if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) - return 0; + return false; if (btrfs_test_opt(fs_info, COMPRESS) || BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS || BTRFS_I(inode)->prop_compress) return btrfs_compress_heuristic(inode, start, end); - return 0; + return false; } static inline void inode_should_defrag(struct btrfs_inode *inode, @@ -1630,7 +1655,8 @@ int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page, } else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) { ret = run_delalloc_nocow(inode, locked_page, start, end, page_started, 0, nr_written); - } else if (!inode_need_compress(inode, start, end)) { + } else if (!inode_can_compress(inode) || + !inode_need_compress(inode, start, end)) { ret = cow_file_range(inode, locked_page, start, end, end, page_started, nr_written, 1, NULL); } else {