Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2476579lqp; Sun, 24 Mar 2024 22:31:07 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXaIYlhh+M06eqYXYE+lmR6EryuWOAo0raAaXOoSpkq+86TbiCzYLpsMJyaQOMtQDnbftR9yzqjVros9fP71xpz8op04TGsQcTcHwNZfQ== X-Google-Smtp-Source: AGHT+IGF2upVDfD0Qpydo33L7oNt/YU2SwUQ1gafarb/pdO73e20wDTUHT08QT1FEL2/d9oRrDtu X-Received: by 2002:a17:906:744:b0:a45:f9c5:3024 with SMTP id z4-20020a170906074400b00a45f9c53024mr4182225ejb.11.1711344667472; Sun, 24 Mar 2024 22:31:07 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711344667; cv=pass; d=google.com; s=arc-20160816; b=pRbpgJ4eRE7v9ZX64Fn3HhrnF5tQnqhwADJmFgZHxub/KviaxrSVypFwBdSOQnct11 b14ssbwmJORYnHf7NqEK7YxYdfGgqA2hYEzunYHoCr0wh/sB8iftyDCLzAf5hZ66WJR6 YoQjLbUYWmjmcyRolSBhVVOzI1Siou7Uili9QjK5prNYVZXkO4sIKAWEiyoSKUC2IMcP SAu0z/QVZ06r+IzlENQE2fx0gqMxw5Vnd1ZvDadSNT1XE9sG7I/lRM1LgAgM50OF7of7 zCyIKSG7ZQF5Xc8BgXuilGylFnZjX6QP4DcdNYyVZb2hmgOd6qHE4wITMrcf7dLWAlmN p21g== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=P2bhL2E9ZOO4WfpsI6sbc9Pju8WQfOXGnwO7CDbAZuQ=; fh=D9AhzmbXcqDDXLxhVCnaNszOGWm3Oi+lJeqXXiWZElA=; b=dBCTwwXqnowL6WwSv7v4z41PI6qRvtGVdxxtGrCBt43P7rxrJL9Uvnckofvr7BjXgh oL2Bj+zb3HJafTKnrQJyzsl8dWKht0x4ii7pbxiSMU9skdU8T3LT3xGOjh/GiMoCDk5s kJ4z7A6DsBlP8lwailiCRsxcDWu3++Zkjl5yzvXZ3DkFq3XSypF0NKwsMiW1jf8mNNu3 QJGe8T4GTzq9ZEOckk88GU3UnSZ/aAm93TNVUYYEJZBZANxF6QVKC1hmh2cujpknRK2W au+ZqkpyfRvhIBPF/7QFf5j4YXJJcVTPnbOPk9+I6zLd+hpVUXCx+u13j4TedxP2c+nK xk3Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="G/fCWzKK"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-113942-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-113942-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id y23-20020a1709063a9700b00a4340344dbdsi2183265ejd.900.2024.03.24.22.31.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 22:31:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-113942-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="G/fCWzKK"; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-113942-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-113942-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 361FD1F26946 for ; Mon, 25 Mar 2024 05:31:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E43EB1EFFC5; Sun, 24 Mar 2024 23:09:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="G/fCWzKK" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F7E01EF7F9; Sun, 24 Mar 2024 22:57:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711321041; cv=none; b=G8mamIYKFTKcu/8FjPYfQWSUMMoHioqaGpCQtIBhIFG6JjueR+I0ATL/Q/Yq9tiiKxrI7avjW2Tb6QiaChcJPdfabBAs1zbwDsnmV5GQF0U77DhdkesbnJ6xc1e4/fdZHPpp0ErcWvwu5VtdoScr+MiCxN0EcorNKCvTDQVb+XY= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711321041; c=relaxed/simple; bh=Dy6uae8FWce3WOWZYC2UBv4i5gNPJyNQKDnbKCQBSHo=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rT7zCYjXje6Wuh4zmNlzItRRxWG9CcPCCJ95UsqhzjHaiU/o/i1gdSAWkPEqkwIWtwHAl8CegFvhsYX2XJeF4XIxopUf7xzs3IIF8ORh8VD2rC5kmeoY0QAEB6uvMVRsLr9BUjk8l4bUeX4UFW/gshVxkt1o7sLyW1Hgb84X484= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=G/fCWzKK; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F606C43390; Sun, 24 Mar 2024 22:57:19 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711321040; bh=Dy6uae8FWce3WOWZYC2UBv4i5gNPJyNQKDnbKCQBSHo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=G/fCWzKKluiJ5eKIKIVn8zf7N1uHlgxPPjyQTe8ouwg2xrs/qwn3XGBR+qYUYHrLx sMvoK/BWkW46akHDURhKND532dOGenAFmeh0eAj/v2xJ79nqvx7U6xd/lJNGWfZE8o lxpMO3mcb2X0HVoYQm9Xa0jl4rjQZltnP4hWDj/t0LaDUMRhDD9AhmQWv3vtKmP9z3 GVLYP/2rPYuHVsTzKCGAhhBVzGQx5JrLw3DG+ew3M57EieZ4IyC7iHIFXaZHeEHRX6 c9NNAcIbDCZzVBSnJwuv1IP+H0j1+2UbRwXoHz8w9VaEDccbzHFuh1GOwzF2Aop2ln A/rrmwkaKde5g== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Xiuhong Wang , Zhiguo Niu , Chao Yu , Jaegeuk Kim , Sasha Levin Subject: [PATCH 6.7 603/713] f2fs: compress: fix reserve_cblocks counting error when out of space Date: Sun, 24 Mar 2024 18:45:29 -0400 Message-ID: <20240324224720.1345309-604-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324224720.1345309-1-sashal@kernel.org> References: <20240324224720.1345309-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit From: Xiuhong Wang [ Upstream commit 2f6d721e14b69d6e1251f69fa238b48e8374e25f ] When a file only needs one direct_node, performing the following operations will cause the file to be unrepairable: unisoc # ./f2fs_io compress test.apk unisoc #df -h | grep dm-48 /dev/block/dm-48 112G 112G 1.2M 100% /data unisoc # ./f2fs_io release_cblocks test.apk 924 unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 4.8M 100% /data unisoc # dd if=/dev/random of=file4 bs=1M count=3 3145728 bytes (3.0 M) copied, 0.025 s, 120 M/s unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 1.8M 100% /data unisoc # ./f2fs_io reserve_cblocks test.apk F2FS_IOC_RESERVE_COMPRESS_BLOCKS failed: No space left on device adb reboot unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 11M 100% /data unisoc # ./f2fs_io reserve_cblocks test.apk 0 This is because the file has only one direct_node. After returning to -ENOSPC, reserved_blocks += ret will not be executed. As a result, the reserved_blocks at this time is still 0, which is not the real number of reserved blocks. Therefore, fsck cannot be set to repair the file. After this patch, the fsck flag will be set to fix this problem. unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 1.8M 100% /data unisoc # ./f2fs_io reserve_cblocks test.apk F2FS_IOC_RESERVE_COMPRESS_BLOCKS failed: No space left on device adb reboot then fsck will be executed unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 11M 100% /data unisoc # ./f2fs_io reserve_cblocks test.apk 924 Fixes: c75488fb4d82 ("f2fs: introduce F2FS_IOC_RESERVE_COMPRESS_BLOCKS") Signed-off-by: Xiuhong Wang Signed-off-by: Zhiguo Niu Reviewed-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/file.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index f44c29a6bc24b..caeae900f797a 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -3583,10 +3583,10 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) return ret; } -static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) +static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count, + unsigned int *reserved_blocks) { struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); - unsigned int reserved_blocks = 0; int cluster_size = F2FS_I(dn->inode)->i_cluster_size; block_t blkaddr; int i; @@ -3650,12 +3650,12 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true); - reserved_blocks += reserved; + *reserved_blocks += reserved; next: count -= cluster_size; } - return reserved_blocks; + return 0; } static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) @@ -3716,7 +3716,7 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) count = min(end_offset - dn.ofs_in_node, last_idx - page_idx); count = round_up(count, F2FS_I(inode)->i_cluster_size); - ret = reserve_compress_blocks(&dn, count); + ret = reserve_compress_blocks(&dn, count, &reserved_blocks); f2fs_put_dnode(&dn); @@ -3724,13 +3724,12 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) break; page_idx += count; - reserved_blocks += ret; } filemap_invalidate_unlock(inode->i_mapping); f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); - if (ret >= 0) { + if (!ret) { clear_inode_flag(inode, FI_COMPRESS_RELEASED); inode_set_ctime_current(inode); f2fs_mark_inode_dirty_sync(inode, true); @@ -3739,7 +3738,7 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) inode_unlock(inode); mnt_drop_write_file(filp); - if (ret >= 0) { + if (!ret) { ret = put_user(reserved_blocks, (u64 __user *)arg); } else if (reserved_blocks && atomic_read(&F2FS_I(inode)->i_compr_blocks)) { -- 2.43.0