Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp966567pxb; Wed, 6 Apr 2022 05:32:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyYt7gQ9ypm/HP3ko3ynpnbOoEbjUxZxXlZkVI+Lj7t+6zqp5vq9dHHay1/u7EJauu0HDSy X-Received: by 2002:a17:90b:4a82:b0:1c7:5837:ab5c with SMTP id lp2-20020a17090b4a8200b001c75837ab5cmr9624227pjb.55.1649248357872; Wed, 06 Apr 2022 05:32:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649248357; cv=none; d=google.com; s=arc-20160816; b=v9PvRTXyffNrU8WwIza6V8xjjWngdWI37j21h1jyPa9u30vYPTCOG6ZSl8VExxePA7 x6U3BRZxRZwa/IffEw+0rFOs9+0sdXi5GE4/kqkHzONJM0A0wgM/ABSdiygLp5rSggUg RJweKcHNNisuY/XCQDQCVH+aT7rDIxJuVu304MVHPGm0HDXtiFqD1txOh3HYGX5gpTpP sk6a+KJhPBXRyUvNUUiWbOGf7FZ0I6vYUrx1jO6etoM5Q8+beL6iVBNL8N8J25MmQKix QfYIqfbfbMZ2JCoUYGOXo74Gz555z/4LOb3zlt/9hzv/8gN0Eq8jjz9L3dnCdZR52NaH FwZQ== 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=ZtELXR81gVIYS4/IXm0qWYwo5PY5IiJr8e9FyLdXpbg=; b=UBpaAzoBfdNgdQvlS+jGiUib8A+DxSKue1N19MtY1q8k2MJftEsEJweR6uUUGSsRn1 XEdc5r7cC4Jkj4J/OMpAkY4dzxcGqsSAVYC4CAUnxfUqSKYXO3RjNA6Ll4MLqxBukSnG UqSUbnn3HOf5FL0PUmcoUINUJD5VN1vGG6lu4pZ44AqKYozbRe8LBT7yulQWwWFN7w8s s+MoRyxJyzYJYPKGCHCvHCInoz1ZN2ccDXxIV9f0/3vFVMVGh7Kw7rek/iwGRJSuuZwB ml0BelLk1BblMpBVlSGeVPtK0mWUSiiYdYLLGPcFq8BZhzCpY8d/Axu0mIrvbcZ9P9cD IwZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1VOVI7il; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id q13-20020aa7982d000000b004faa8ee4f05si16651895pfl.98.2022.04.06.05.32.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Apr 2022 05:32:37 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=1VOVI7il; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 F0C4C53D7D6; Wed, 6 Apr 2022 02:13:20 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1381391AbiDEU3J (ORCPT + 99 others); Tue, 5 Apr 2022 16:29:09 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41194 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1356703AbiDEKYt (ORCPT ); Tue, 5 Apr 2022 06:24:49 -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 980E9BF502; Tue, 5 Apr 2022 03:08:52 -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 3160AB81C8B; Tue, 5 Apr 2022 10:08:51 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 79387C385A6; Tue, 5 Apr 2022 10:08:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649153329; bh=2G8/AqG0+BbaHGBuCW1Wgi9beZzm2Mxp5cB/nqaBBfY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=1VOVI7ild3F6wRDaJmSopmKa4448/hYMG8TWNY5ch3FfiubAFTa0lLoZSqh6ebF9O iTBcs4K0/82hk8/PL23D++v7IlJ0ZTv40TVqK+yXJAFtNJk61XiQh8ySygHajWaMoH XL3mlSbm4z+/DLjwHkS32YqLN1PCVg6Kefg4cqCQ= 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.10 184/599] f2fs: fix compressed file start atomic write may cause data corruption Date: Tue, 5 Apr 2022 09:27:58 +0200 Message-Id: <20220405070304.316914552@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070258.802373272@linuxfoundation.org> References: <20220405070258.802373272@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 04e980c58319..b2016fd3a7ca 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -3461,7 +3461,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 1fbaab1f7aba..792f9059d897 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -2035,7 +2035,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