Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp749343pxb; Tue, 5 Apr 2022 21:38:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwn1T9JhisXqpmUGHdI9hD1Cb9NB41oKqKzNd2XJoHw0ky7xCPr8UaLUV2mki1eEGywmLXS X-Received: by 2002:a17:90b:3d0b:b0:1c9:b387:4372 with SMTP id pt11-20020a17090b3d0b00b001c9b3874372mr8045295pjb.74.1649219935681; Tue, 05 Apr 2022 21:38:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649219935; cv=none; d=google.com; s=arc-20160816; b=tnqoJEMApY2n4YYthD0zXGhO5SZtsfQVhR5TT/FY8JXBcaH4ggyTt9r+w3qtzhiMyz 5vT/H+Tx/R17caj6w80iTaz17QF4PlMRcDcHeKhy3Roc3p03df3o9qCeOBFqlOzex8MQ 9o9eKeTvVbsbNHj8WHFEmT8oMzhMgWiIqigXtyzaUoDEVvha51ByIVOopo+RlGaxGwLv Y01GKxsEqyIXPug0FlTvCOH7D4CeNqx13FcXVaQHZKHvuoO7W0s2M+VqsHUrB5zcROBM Tt8xyVPJCtNeeS6sUBzAnI3gdJyQzuiDNeyoQB91pUgBoj2St5HPVJm+cxUTM9wX6DMe vQ+A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=XV2BD5bXcH3c8MwvHDBBcrohO2G3tWEJUUa1Gie8ekA=; b=vSEyMfF1NJdqzsV1+8VE0ZNl4cvixWKb1xgGANdvYXzzhatHLmttvXXz3Thj6hmm6h rvXQTCfmczvxhRjPerccBGe5CdQa65vXPLi0maBU+Cu0JYRqUpWNlGRy0N2+AyvV2GkU NUB1RE67cAfMBioA9SPAKAjXDVCSlx0UAPhknU7EXGIr+W3kB4ESsGZEkPLNfZFoZncq NRUa8DN3P+cdfmyLMbc1d1a/NgVdq7yeGmirQbZcHjimtxmpYwrKEtDi4r1g7C0b9xvE lJBSQV/pJ+Dsph+2AjIwXp4qOM0S2i3qU4qFEO8/pflaL124qknQRYupIUpxqZ9Etjbp qZKw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DBUt5GWf; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id y15-20020a056a00190f00b004fa8f507068si15764081pfi.96.2022.04.05.21.38.55 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 21:38:55 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=DBUt5GWf; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 539E5AAC96; Tue, 5 Apr 2022 20:04:00 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S241939AbiDEIgJ (ORCPT + 99 others); Tue, 5 Apr 2022 04:36:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48340 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234029AbiDEH55 (ORCPT ); Tue, 5 Apr 2022 03:57:57 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BA3E95A37; Tue, 5 Apr 2022 00:51:48 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 49EEEB81BB2; Tue, 5 Apr 2022 07:51:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A5B01C340EE; Tue, 5 Apr 2022 07:51:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649145105; bh=/vR0csPgyK1a51us8gLjMxucFDCE+Ydgx3/OPfKJx48=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DBUt5GWfyvUtASSIpANBiVj8LyVgsqDBzZSMHeHh+tt7VkLSSztx4A68sQibGncij GgjKLPe8XWxDMNt+pZJdWbj7BTBrsTbg5HURrzhKuyLmITlDuHvvQY5aB2UKyXGzDp Ti3u/W4smXP0u+yEqJPFUe5Uh6VxVdxfjzXm97XI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, kernel test robot , Dan Carpenter , Fengnan Chang , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.17 0286/1126] f2fs: fix compressed file start atomic write may cause data corruption Date: Tue, 5 Apr 2022 09:17:13 +0200 Message-Id: <20220405070416.008596932@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070407.513532867@linuxfoundation.org> References: <20220405070407.513532867@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Fengnan Chang [ Upstream commit 9b56adcf525522e9ffa52471260298d91fc1d395 ] When compressed file has blocks, f2fs_ioc_start_atomic_write will succeed, but compressed flag will be remained in inode. If write partial compreseed cluster and commit atomic write will cause data corruption. This is the reproduction process: Step 1: create a compressed file ,write 64K data , call fsync(), then the blocks are write as compressed cluster. Step2: iotcl(F2FS_IOC_START_ATOMIC_WRITE) --- this should be fail, but not. write page 0 and page 3. iotcl(F2FS_IOC_COMMIT_ATOMIC_WRITE) -- page 0 and 3 write as normal file, Step3: drop cache. read page 0-4 -- Since page 0 has a valid block address, read as non-compressed cluster, page 1 and 2 will be filled with compressed data or zero. The root cause is, after commit 7eab7a696827 ("f2fs: compress: remove unneeded read when rewrite whole cluster"), in step 2, f2fs_write_begin() only set target page dirty, and in f2fs_commit_inmem_pages(), we will write partial raw pages into compressed cluster, result in corrupting compressed cluster layout. Fixes: 4c8ff7095bef ("f2fs: support data compression") Fixes: 7eab7a696827 ("f2fs: compress: remove unneeded read when rewrite whole cluster") Reported-by: kernel test robot Reported-by: Dan Carpenter Signed-off-by: Fengnan Chang Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/data.c | 2 +- fs/f2fs/file.c | 5 ++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index e1ef925be60c..bdfa8bed10b2 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -3357,7 +3357,7 @@ static int f2fs_write_begin(struct file *file, struct address_space *mapping, *fsdata = NULL; - if (len == PAGE_SIZE) + if (len == PAGE_SIZE && !(f2fs_is_atomic_file(inode))) goto repeat; ret = f2fs_prepare_compress_overwrite(inode, pagep, diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 3c98ef6af97d..b110c3a7db6a 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -2008,7 +2008,10 @@ static int f2fs_ioc_start_atomic_write(struct file *filp) inode_lock(inode); - f2fs_disable_compressed_file(inode); + if (!f2fs_disable_compressed_file(inode)) { + ret = -EINVAL; + goto out; + } if (f2fs_is_atomic_file(inode)) { if (is_inode_flag_set(inode, FI_ATOMIC_REVOKE_REQUEST)) -- 2.34.1