public inbox for linux-ext4@vger.kernel.org
 help / color / mirror / Atom feed
From: Wei Gao <wegao@suse.com>
To: Jan Kara <jack@suse.cz>
Cc: Wei Gao <wegao@suse.com>,
	linux-kernel@vger.kernel.org, linux-ext4@vger.kernel.org
Subject: [PATCH v2] ext2: Handle fiemap on empty files to prevent EINVAL
Date: Fri, 13 Jun 2025 11:18:38 -0400	[thread overview]
Message-ID: <20250613152402.3432135-1-wegao@suse.com> (raw)
In-Reply-To: <20250612142855.2678267-1-wegao@suse.com>

Previously, ext2_fiemap would unconditionally apply "len = min_t(u64, len,
i_size_read(inode));", When inode->i_size was 0 (for an empty file), this
would reduce the requested len to 0. Passing len = 0 to iomap_fiemap could
then result in an -EINVAL error, even for valid queries on empty files.

Link: https://github.com/linux-test-project/ltp/issues/1246
Signed-off-by: Wei Gao <wegao@suse.com>
---
 fs/ext2/inode.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/fs/ext2/inode.c b/fs/ext2/inode.c
index 30f8201c155f..591db2b4390a 100644
--- a/fs/ext2/inode.c
+++ b/fs/ext2/inode.c
@@ -895,9 +895,15 @@ int ext2_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
 		u64 start, u64 len)
 {
 	int ret;
+	u64 i_size;
 
 	inode_lock(inode);
-	len = min_t(u64, len, i_size_read(inode));
+
+	i_size = i_size_read(inode);
+
+	if (i_size > 0)
+		len = min_t(u64, len, i_size_read(inode));
+
 	ret = iomap_fiemap(inode, fieinfo, start, len, &ext2_iomap_ops);
 	inode_unlock(inode);
 
-- 
2.49.0


  parent reply	other threads:[~2025-06-13  3:25 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-12 14:28 [PATCH] ext2: Handle fiemap on empty files to prevent EINVAL Wei Gao
2025-06-12 10:29 ` Jan Kara
2025-06-12 11:06   ` Jan Kara
2025-06-13 15:18 ` Wei Gao [this message]
2025-06-13  9:42   ` [PATCH v2] " Jan Kara
2025-06-13 22:59     ` Wei Gao
2025-06-13 15:55       ` Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250613152402.3432135-1-wegao@suse.com \
    --to=wegao@suse.com \
    --cc=jack@suse.cz \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox