From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (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 E938A3FA5E5 for ; Mon, 29 Jun 2026 11:49:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782733770; cv=none; b=HiyMx8LAc7hj8fQoreVsT00LS84zjFIH3/2tilH13FxXTCuu/fhwVCuUoLjaVfsao+IfupnZETqNyfQXJ6lHPZiDn1y7cwj+wUE1d7J+443229Mbpg02Lo4r9T5jPlPdzJnfw+VQaQqXOD6ZMBgXJtZYFAs2CN+qUs0Kj1jgqck= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1782733770; c=relaxed/simple; bh=WAne//9Qofm60gg+KyKf1Uvkynk+ZSVX7G5H0jS5O90=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=Z8a7rd1YD8vosgeqZxuovSAoZHvh/+RxF6G9ubPzBtnNxyQj6F28VsZRxeL2bDpt0jj9dy3PqUBOMZHYP27PhQCbV2sEmHgGtvzM3HZEZm5gxpsu60n2Lsbnca4dLi8bgZJFUbrGngJ/ojRjDbFsI7pFMwDdGpXwM3BWi5LNGj4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=k2xCY9cA; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="k2xCY9cA" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5CC571F000E9; Mon, 29 Jun 2026 11:49:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1782733769; bh=+9+EvJ6TfUCv/YwSlzQGh1Hj5i02SV0UzKzpRkG+M38=; h=From:To:Cc:Subject:Date; b=k2xCY9cA/wBAEscNgoyvThzKrzwv+Cu2Xu8/4RuY44Kd47gKwJ/hYOUYAR/cP4BnY XHsSI9LE0MqAuh0lvvQrZXONuyO0I8Ygw7PXuwqCKWif5e1MRppbulSpu4MekvvnB7 5NmSOCNAU9N4D8qBdjX1tm7yMrHPJKlIJvoETLAuOZoUe/hohfyustUbxdgLcs1zSb f/Uo2zeNsjlXpIipa1GmXM6lt53Dm1y5N40ncrTEWskVmViO7RcA7e0FfFdkuGU4k3 RyOZPIi4jABBomJjN98c2h0rOF6wAS/cwZQBvKbA5GsET/q6QyTFPimb6dqlo4NaLg YYbOo/da1qkYg== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , stable@kernel.org, Daeho Jeong Subject: [PATCH] f2fs: fix to avoid potential section-unaligned pinfile Date: Mon, 29 Jun 2026 11:49:18 +0000 Message-ID: <20260629114918.224537-1-chao@kernel.org> X-Mailer: git-send-email 2.55.0.rc0.799.gd6f94ed593-goog Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Blocks of pinfile may not aligned to section size due to wrong use on pinfile, result in heavy overhead of GC, let avoid this by adding additional check condition in f2fs_setattr(). - truncate -s 8mb pinfile : random checkpoint may persist filesize w/ inode - fallocate -o 0 -l 8mb pinfile - f2fs_fallocate - f2fs_expand_inode_data - f2fs_allocate_pinning_section - f2fs_map_blocks - f2fs_map_lock - __allocate_data_block - file_need_truncate : w/ FADVISE_TRUNC_BIT, we can expect unaligned mapping can be truncated while open() if f2fs is not umount abnormally - f2fs_map_unlock : following f2fs checkpoint and sudden power-cut - mount - open pinfile - f2fs_file_open - finish_preallocate_blocks - truncate_setsize : filesize is 8mb - f2fs_truncate : can only truncate block outside filesize, rather than truncating unaligned blocks inside filesize Fixes: f5a53edcf01e ("f2fs: support aligned pinned file") Cc: stable@kernel.org Cc: Daeho Jeong Signed-off-by: Chao Yu --- fs/f2fs/file.c | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index f4facd409d9b..11cc8d79c235 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -1107,17 +1107,23 @@ int f2fs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, !IS_ALIGNED(attr->ia_size, F2FS_BLK_TO_BYTES(fi->i_cluster_size))) return -EINVAL; - /* - * To prevent scattered pin block generation, we don't allow - * smaller/equal size unaligned truncation for pinned file. - * We only support overwrite IO to pinned file, so don't - * care about larger size truncation. - */ - if (f2fs_is_pinned_file(inode) && - attr->ia_size <= i_size_read(inode) && - !IS_ALIGNED(attr->ia_size, - F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi)))) - return -EINVAL; + + if (f2fs_is_pinned_file(inode)) { + /* + * It may break section-aligned fallocate recovery + * mechanism, so do not allow larger size truncation. + */ + if (attr->ia_size > i_size_read(inode)) + return -EINVAL; + /* + * To prevent scattered pin block generation, we don't + * allow smaller/equal size unaligned truncation for + * pinned file. + */ + else if (!IS_ALIGNED(attr->ia_size, + F2FS_BLK_TO_BYTES(CAP_BLKS_PER_SEC(sbi)))) + return -EINVAL; + } } if (is_quota_modification(idmap, inode, attr)) { -- 2.49.0