Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3802169ybi; Mon, 29 Jul 2019 12:55:46 -0700 (PDT) X-Google-Smtp-Source: APXvYqxKrXHRVI3bxoaQ1Q9hEFRY+nTH/WWwssemwy4KyGHZtEmTXQH3yvhM/IuAfHg9o8KaeEU4 X-Received: by 2002:a17:90a:cb18:: with SMTP id z24mr61686384pjt.108.1564430146108; Mon, 29 Jul 2019 12:55:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564430146; cv=none; d=google.com; s=arc-20160816; b=nC9e/kGzXjtS4o9bo0tyecPxoojJr8C9HJJeOI8kvSvajQz2xqLpgHWGFDK5zX6e7g HgRRIEQ68NATg5vT9LMTjtUwa0qrw6YCAWrO/W4knU1FvT/ny2xwoCBt+J50Hzk6IBqm qRpFA9wxw3HSXWtU4sNCzlNVg7LDiw4Z1Y0LF8gSqy8TXoeWUWQkt6v8u5U0lhxsJzkH YO6KU2CSULsFKfudP8glMG/8IJIBMYaguXuOiKahK2J7HUkBNpiEYf8LxMRgUBKLYPZt sN6MvnnxCjCmazhtdlXawy1tgQ8QaYoLnNuYMHwtGgPUEkBOvYGJYWMF+cD6MM/Zjq35 dW+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=35yVTK/rK/Dmi7ScCnqsfZa/vZ0KLsIOrZzs3sWkWwc=; b=DflVbYiK989W620BSZcPhyZmyMK2SapEd0JD3akyTLfSibT79vKAxzUmrdzL0kBPlo TFVMGwFVqlc6rv3uURz8EtW+OgsQJpsFM8pI39X2sZjLWpd15bD/Egnss4fy0xqI/b+x n5c7pTsom20Nnnnbo29NLa3jDg9ufA5GTwdGbZaA2d2bFNTdR+VatWgP6JeOPw7sOEsO zMpCFI+Fx9FX9loRT0PDug5SQ0A4StYGAPZwTF4L4tkdmT8+2W/1QD0PBO4pBXlJPAoK ihUXYMnfUnAeTJrFgIL/Kad8pBR8+cXjt6kc+yo+RaLPDDV13xPDg5rcKKk9RNc4HiFm rfvw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=J5gydGPD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id w62si27110382pgd.209.2019.07.29.12.55.30; Mon, 29 Jul 2019 12:55:46 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=J5gydGPD; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2391024AbfG2TyW (ORCPT + 99 others); Mon, 29 Jul 2019 15:54:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:46734 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2390821AbfG2TyI (ORCPT ); Mon, 29 Jul 2019 15:54:08 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 16CD021655; Mon, 29 Jul 2019 19:54:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564430047; bh=E45bOxYEs1GCke5wwWSaD5o7gMTEOtOqrjfmOPnbj38=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J5gydGPDegwRDmFnr83S49GvO7oRbsRCjXhXj1SC6NhzYS7JuHGo8nGGMLekdT1Fh im6SRu0x2Ifs06HGtMb5WndNSjPuKJN0SjWbzNEPE/uJmKF284sWgvidInkO9F4JAx wC6QFgAYtfSm8S8J/1CyCYof/yPZktikxrYpe3QA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, James Harvey , Qu Wenruo , David Sterba Subject: [PATCH 5.2 179/215] btrfs: inode: Dont compress if NODATASUM or NODATACOW set Date: Mon, 29 Jul 2019 21:22:55 +0200 Message-Id: <20190729190811.031475083@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190729190739.971253303@linuxfoundation.org> References: <20190729190739.971253303@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Qu Wenruo commit 42c16da6d684391db83788eb680accd84f6c2083 upstream. 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 CC: stable@vger.kernel.org # 4.4+ Signed-off-by: Qu Wenruo Reviewed-by: David Sterba Signed-off-by: David Sterba Signed-off-by: Greg Kroah-Hartman --- fs/btrfs/inode.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -394,10 +394,31 @@ static noinline int add_async_extent(str return 0; } +/* + * Check if the inode has flags compatible with compression + */ +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 needs to be submitted to compression, based on mount + * options, defragmentation, properties or heuristics. + */ static inline int 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 0; + } /* force compress */ if (btrfs_test_opt(fs_info, FORCE_COMPRESS)) return 1; @@ -1630,7 +1651,8 @@ int btrfs_run_delalloc_range(struct inod } 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 {