From: "Theodore Ts'o" <tytso@mit.edu>
To: Ext4 Developers List <linux-ext4@vger.kernel.org>
Cc: "Theodore Ts'o" <tytso@mit.edu>
Subject: [PATCH 23/23] ext4: don't stop EXT4_IOC_GET_ES_CACHE at uncached ranges
Date: Wed, 11 Nov 2026 23:47:12 -0500 [thread overview]
Message-ID: <20261112044712.1577296-24-tytso@mit.edu> (raw)
In-Reply-To: <20261112044712.1577296-1-tytso@mit.edu>
ext4_fill_es_cache_info() walks the extent status tree by calling
ext4_es_lookup_extent() with a next_lblk pointer and stops as soon as
the lookup misses. ext4_es_lookup_extent() only fills in *next_lblk on
a hit, so any block in the requested range that is not covered by an
extent status entry terminates the walk.
The extent status tree commonly has such gaps, since entries are only
added for ranges that have been mapped and can be reclaimed by the
shrinker. As a result EXT4_IOC_GET_ES_CACHE reports no extents at all
when the query starts in an uncached range, even if cached extents
follow, and a paginated query that resumes after an extent preceding
an uncached gap returns zero extents without FIEMAP_EXTENT_LAST, which
ends the iteration early and hides the rest of the cache.
Make ext4_es_lookup_extent() also report the start of the next cached
extent on a miss, derived from the last node visited during the tree
walk in the same way as __es_tree_search(), and have
ext4_fill_es_cache_info() skip ahead to it instead of giving up. The
returned block is always greater than the looked up one, so the walk
still terminates, and 0 still means there are no further extents.
Extend the KUnit tests to check next_lblk on lookup misses and
ext4_get_es_cache() on a tree with leading and interior gaps.
Fixes: bb5835edcdf8 ("ext4: add new ioctl EXT4_IOC_GET_ES_CACHE")
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
---
fs/ext4/extents-test.c | 30 ++++++++++++++++++++++++++++++
fs/ext4/extents.c | 9 +++++++--
fs/ext4/extents_status-test.c | 17 ++++++++++++++++-
fs/ext4/extents_status.c | 23 +++++++++++++++++++++++
4 files changed, 76 insertions(+), 3 deletions(-)
diff --git a/fs/ext4/extents-test.c b/fs/ext4/extents-test.c
index 4f1746be323f..1e6324a79cca 100644
--- a/fs/ext4/extents-test.c
+++ b/fs/ext4/extents-test.c
@@ -2946,6 +2946,36 @@ static void test_ext_fiemap_and_get_es_cache(struct kunit *test)
KUNIT_ASSERT_EQ(test, err, 0);
KUNIT_EXPECT_EQ(test, fieinfo.fi_flags & FIEMAP_FLAG_CACHE, 0U);
KUNIT_EXPECT_GE(test, fieinfo.fi_extents_mapped, 5U);
+
+ /*
+ * ES tree with uncached ranges [0, 10), [20, 50) and beyond 60:
+ * ext4_get_es_cache() must skip over them instead of stopping, both
+ * when the query starts in a leading uncached range and when a
+ * paginated query resumes inside an uncached gap.
+ */
+ down_write(&EXT4_I(inode)->i_data_sem);
+ ext4_es_remove_extent(inode, 0, EXT_MAX_BLOCKS);
+ ext4_es_insert_extent(inode, 10, 10, 400, EXTENT_STATUS_WRITTEN, 0);
+ ext4_es_insert_extent(inode, 50, 10, 500, EXTENT_STATUS_WRITTEN, 0);
+ up_write(&EXT4_I(inode)->i_data_sem);
+ inode->i_size = 100LL * 4096;
+ EXT4_I(inode)->i_disksize = 100LL * 4096;
+
+ memset(&fieinfo, 0, sizeof(fieinfo));
+ err = ext4_get_es_cache(inode, &fieinfo, 0, 100 * 4096);
+ KUNIT_EXPECT_EQ(test, err, 0);
+ KUNIT_EXPECT_EQ(test, fieinfo.fi_extents_mapped, 2U);
+
+ memset(&fieinfo, 0, sizeof(fieinfo));
+ err = ext4_get_es_cache(inode, &fieinfo, 20 * 4096, 80 * 4096);
+ KUNIT_EXPECT_EQ(test, err, 0);
+ KUNIT_EXPECT_EQ(test, fieinfo.fi_extents_mapped, 1U);
+
+ /* A query range entirely inside an uncached gap reports nothing */
+ memset(&fieinfo, 0, sizeof(fieinfo));
+ err = ext4_get_es_cache(inode, &fieinfo, 20 * 4096, 30 * 4096);
+ KUNIT_EXPECT_EQ(test, err, 0);
+ KUNIT_EXPECT_EQ(test, fieinfo.fi_extents_mapped, 0U);
}
static const struct kunit_ext_test_param test_split_convert_params[] = {
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 7872ddc21544..1925dd0d210e 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -2242,8 +2242,13 @@ static int ext4_fill_es_cache_info(struct inode *inode,
while (block <= end) {
next = 0;
flags = 0;
- if (!ext4_es_lookup_extent(inode, block, &next, &es, NULL))
- break;
+ if (!ext4_es_lookup_extent(inode, block, &next, &es, NULL)) {
+ /* Skip over an uncached range to the next extent */
+ if (next == 0)
+ break;
+ block = next;
+ continue;
+ }
if (ext4_es_is_unwritten(&es))
flags |= FIEMAP_EXTENT_UNWRITTEN;
if (ext4_es_is_delayed(&es))
diff --git a/fs/ext4/extents_status-test.c b/fs/ext4/extents_status-test.c
index 59eaac464bdc..a398dc97f30e 100644
--- a/fs/ext4/extents_status-test.c
+++ b/fs/ext4/extents_status-test.c
@@ -507,6 +507,7 @@ static void test_es_lookup_empty_tree_returns_miss(struct kunit *test)
&ctx->sbi->s_es_stats.es_stats_cache_misses);
ret = ext4_es_lookup_extent(inode, 10, &next_lblk, &es, NULL);
KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, next_lblk, 0U);
KUNIT_EXPECT_EQ(test, percpu_counter_sum_positive(
&ctx->sbi->s_es_stats.es_stats_cache_misses),
misses_before + 1);
@@ -579,9 +580,23 @@ test_es_lookup_hits_cache_and_rbtree_with_next_lblk(struct kunit *test)
&ctx->sbi->s_es_stats.es_stats_cache_hits),
hits_before + 4);
- /* Lookup in gap [15..19] returns 0 */
+ /* Lookup in gap [15..19] returns 0 and points next_lblk at [20, 5) */
+ next_lblk = 0xdead;
ret = ext4_es_lookup_extent(inode, 15, &next_lblk, &es, NULL);
KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, next_lblk, 20U);
+
+ /* Miss before the first extent points next_lblk at [10, 5) */
+ next_lblk = 0xdead;
+ ret = ext4_es_lookup_extent(inode, 5, &next_lblk, &es, NULL);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, next_lblk, 10U);
+
+ /* Miss after the last extent sets next_lblk to 0 */
+ next_lblk = 0xdead;
+ ret = ext4_es_lookup_extent(inode, 30, &next_lblk, &es, NULL);
+ KUNIT_EXPECT_EQ(test, ret, 0);
+ KUNIT_EXPECT_EQ(test, next_lblk, 0U);
}
static void test_es_fc_replay_bypasses_tree_operations(struct kunit *test)
diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
index 075b9efb6dd4..ce66a4b026c2 100644
--- a/fs/ext4/extents_status.c
+++ b/fs/ext4/extents_status.c
@@ -1087,6 +1087,10 @@ void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
*
* ext4_es_lookup_extent is called by ext4_map_blocks/ext4_da_map_blocks.
*
+ * If @next_lblk is not NULL, it is set to the starting block of the first
+ * extent after @lblk in the tree, or 0 if there is none, regardless of
+ * whether @lblk itself was found.
+ *
* Return: 1 on found, 0 on not
*/
int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
@@ -1157,6 +1161,25 @@ int ext4_es_lookup_extent(struct inode *inode, ext4_lblk_t lblk,
*pseq = EXT4_I(inode)->i_es_seq;
} else {
percpu_counter_inc(&stats->es_stats_cache_misses);
+ if (next_lblk) {
+ /*
+ * es1 is the last node visited by the tree walk, so
+ * it is either the successor or the predecessor of
+ * @lblk (see __es_tree_search()).
+ */
+ *next_lblk = 0;
+ if (es1 && lblk < es1->es_lblk) {
+ *next_lblk = es1->es_lblk;
+ } else if (es1) {
+ node = rb_next(&es1->rb_node);
+ if (node) {
+ es1 = rb_entry(node,
+ struct extent_status,
+ rb_node);
+ *next_lblk = es1->es_lblk;
+ }
+ }
+ }
}
read_unlock(&EXT4_I(inode)->i_es_lock);
--
2.56.0.542.g3ef89b7020-goog
prev parent reply other threads:[~2026-11-12 4:47 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 4:46 [PATCH 00/23] ext4: lots of misc bug fixes Theodore Ts'o
2026-11-12 4:46 ` [PATCH 01/23] ext4: only update s_mount_opts when EXT4_TUNE_FL_MOUNT_OPTS is set Theodore Ts'o
2026-11-12 4:46 ` [PATCH 02/23] ext4: return -EFSBADCRC when skipping a corrupt backup superblock Theodore Ts'o
2026-11-12 4:46 ` [PATCH 03/23] ext4: don't allow enabling casefold via EXT4_IOC_SET_TUNE_SB_PARAM Theodore Ts'o
2026-11-12 4:46 ` [PATCH 04/23] ext4: persist the htree hash seed when enabling dir_index online Theodore Ts'o
2026-11-12 4:46 ` [PATCH 05/23] ext4: enforce feature prerequisites when tuning a mounted fs Theodore Ts'o
2026-11-12 4:46 ` [PATCH 06/23] ext4: handle s_lastcheck_hi in the get/set tune superblock ioctls Theodore Ts'o
2026-11-12 4:46 ` [PATCH 07/23] ext4: pass missing ioctls through ext4_compat_ioctl Theodore Ts'o
2026-11-12 4:46 ` [PATCH 08/23] ext4: fix inverted err_report_sec check in ext4_update_super() Theodore Ts'o
2026-11-12 4:46 ` [PATCH 09/23] ext4: fix err_report_sec_store() timer and value handling Theodore Ts'o
2026-11-12 4:46 ` [PATCH 10/23] ext4: fix inode table accounting in count_overhead() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 11/23] ext4: handle flex groups straddling s_first_meta_bg in online resize Theodore Ts'o
2026-11-12 4:47 ` [PATCH 12/23] ext4: reject EXT4_IOC_GROUP_ADD inputs larger than a block group Theodore Ts'o
2026-11-12 4:47 ` [PATCH 13/23] ext4: propagate ext4_alloc_group_tables() errors from ext4_resize_fs() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 14/23] ext4: reject online shrink before probing the device size Theodore Ts'o
2026-11-12 4:47 ` [PATCH 15/23] ext4: cap migrated extents at EXT_INIT_MAX_LEN Theodore Ts'o
2026-11-12 4:47 ` [PATCH 16/23] ext4: don't treat a handle restart as an error in finish_range() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 17/23] ext4: don't clobber the migration result in ext4_ext_migrate() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 18/23] ext4: fix EOF clamping of the range in mext_check_adjust_range() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 19/23] ext4: bound the MMP update and check intervals Theodore Ts'o
2026-11-12 4:47 ` [PATCH 20/23] ext4: fix MMP claim of a cleanly unmounted filesystem Theodore Ts'o
2026-11-12 4:47 ` [PATCH 21/23] ext4: fix device comparison overflow in ext4_getfsmap_dev_compare() Theodore Ts'o
2026-11-12 4:47 ` [PATCH 22/23] ext4: don't allocate over indirect branches that failed to load Theodore Ts'o
2026-11-12 4:47 ` Theodore Ts'o [this message]
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=20261112044712.1577296-24-tytso@mit.edu \
--to=tytso@mit.edu \
--cc=linux-ext4@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