[PATCH v2 02/13] ext4: introduce CONVERT_PENDING extent status type

From: Ojaswin Mujoo

Date: Fri Nov 27 2026 - 10:11:41 EST


Add support to track ranges that are in an unwritten state but actaully
cover valid data. These are the unwritten ranges which have an in progress
disk IO, but are not yet converted to written via endio handling.

Upcoming patches will use this extent type to avoid zerooing out such
ranges during endio extent splitting, because they cover valid data.

The lifecycle of an extent undergoing write is now as follows:

1. Say we start with an extent (0,10,unwrit) which is also cached in es.

Ondisk: [0,10,unwrit]
es cache: [0,10,unwrit]

2. We perform a write to (0, 5):

Ondisk: [0,10,unwrit]
es cache: [0,5,conv_pending][5,10,unwrit]

3. IO is finished and endio handler converts (0,5) to written:

ondisk: [0,5,writ][5,10,unwrit]
es cache: [0,5,writ][5,10,unwrit]

An extent of CONVERT_PENDING type will behave similar to an UNWRITTEN
extent for most of the purposes like read. Just that we will take special
care in es cache to make sure the conv_pending range is not split or merged
so that we don't lose the status. Similarly, it also can't be evicted from
memory because we don't want to lost the status.

Signed-off-by: Ojaswin Mujoo <ojaswin@xxxxxxxxxxxxx>
---
fs/ext4/extents_status-test.c | 8 ++++----
fs/ext4/extents_status.c | 18 ++++++++++++++++++
fs/ext4/extents_status.h | 11 ++++++++++-
include/trace/events/ext4.h | 4 +++-
4 files changed, 35 insertions(+), 6 deletions(-)

diff --git a/fs/ext4/extents_status-test.c b/fs/ext4/extents_status-test.c
index 825d12513e4a..014475bae07c 100644
--- a/fs/ext4/extents_status-test.c
+++ b/fs/ext4/extents_status-test.c
@@ -452,13 +452,13 @@ static void test_es_bitfield_written_and_unwritten_encoding(struct kunit *test)
static void test_es_bitfield_delayed_and_hole_encoding(struct kunit *test)
{
struct extent_status es = {};
- /* 59-bit physical block field (64 - 5 status bits) all ones */
- const ext4_fsblk_t max_59bit_pblk = 0x07ffffffffffffffULL;
+ /* 58-bit physical block field (64 - 6 status bits) all ones */
+ const ext4_fsblk_t max_pblk = ~ES_MASK;

ext4_es_store_pblock_status(&es, ~0ULL, EXTENT_STATUS_DELAYED);
KUNIT_EXPECT_TRUE(test, ext4_es_is_delayed(&es));
KUNIT_EXPECT_FALSE(test, ext4_es_is_mapped(&es));
- KUNIT_EXPECT_EQ(test, ext4_es_pblock(&es), max_59bit_pblk);
+ KUNIT_EXPECT_EQ(test, ext4_es_pblock(&es), max_pblk);
KUNIT_EXPECT_EQ(test, ext4_es_show_pblock(&es), 0ULL);

ext4_es_store_pblock_status(&es, 0ULL, EXTENT_STATUS_HOLE);
@@ -472,7 +472,7 @@ test_es_bitfield_referenced_and_pblock_update_preserve_state(struct kunit *test)
{
struct extent_status es = {};
const ext4_fsblk_t initial_pblk = 0x123456789abcULL;
- const ext4_fsblk_t updated_pblk = 0x07ffffffffffffffULL;
+ const ext4_fsblk_t updated_pblk = ~ES_MASK;

ext4_es_store_pblock_status(&es, initial_pblk, EXTENT_STATUS_WRITTEN);

diff --git a/fs/ext4/extents_status.c b/fs/ext4/extents_status.c
index b7fb8502f9d4..28528f19be1d 100644
--- a/fs/ext4/extents_status.c
+++ b/fs/ext4/extents_status.c
@@ -529,6 +529,9 @@ static inline bool ext4_es_must_keep(struct extent_status *es)
if (ext4_es_is_delayed(es))
return true;

+ if (ext4_es_is_convert_pending(es))
+ return true;
+
return false;
}

@@ -594,6 +597,12 @@ static int ext4_es_can_be_merged(struct extent_status *es1,
if (ext4_es_type(es1) != ext4_es_type(es2))
return 0;

+ /*
+ * Don't merge block range that's about to get converted to written.
+ */
+ if (ext4_es_is_convert_pending(es1))
+ return 0;
+
if (((__u64) es1->es_len) + es2->es_len > EXT_MAX_BLOCKS) {
pr_warn("ES assertion failed when merging extents. "
"The sum of lengths of es1 (%d) and es2 (%d) "
@@ -1076,6 +1085,15 @@ void ext4_es_cache_extent(struct inode *inode, ext4_lblk_t lblk,
ext4_es_type(&chkes) == EXTENT_STATUS_DELAYED)
return;

+ /*
+ * An ondisk unwritten extent is allowed to conflict with
+ * convert_pending since the latter signifies an ondisk unwritten extent
+ * about to be converted to written due to endio.
+ */
+ if (status == EXTENT_STATUS_UNWRITTEN &&
+ ext4_es_type(&chkes) == EXTENT_STATUS_CONVERT_PENDING)
+ return;
+
ext4_warning_inode(inode,
"ES cache extent failed: add [%d,%d,%llu,0x%x] conflict with existing [%d,%d,%llu,0x%x]\n",
lblk, len, pblk, status, chkes.es_lblk, chkes.es_len,
diff --git a/fs/ext4/extents_status.h b/fs/ext4/extents_status.h
index 01cddcaaa68e..2247f811f6c3 100644
--- a/fs/ext4/extents_status.h
+++ b/fs/ext4/extents_status.h
@@ -36,6 +36,7 @@ enum {
ES_DELAYED_B,
ES_HOLE_B,
ES_REFERENCED_B,
+ ES_CONVERT_PENDING_B,
ES_FLAGS
};

@@ -51,10 +52,12 @@ enum {
#define EXTENT_STATUS_DELAYED (1 << ES_DELAYED_B)
#define EXTENT_STATUS_HOLE (1 << ES_HOLE_B)
#define EXTENT_STATUS_REFERENCED (1 << ES_REFERENCED_B)
+#define EXTENT_STATUS_CONVERT_PENDING (1 << ES_CONVERT_PENDING_B)

#define ES_TYPE_MASK ((ext4_fsblk_t)(EXTENT_STATUS_WRITTEN | \
EXTENT_STATUS_UNWRITTEN | \
EXTENT_STATUS_DELAYED | \
+ EXTENT_STATUS_CONVERT_PENDING | \
EXTENT_STATUS_HOLE))

#define ES_TYPE_VALID(type) ((type) && !((type) & ((type) - 1)))
@@ -173,7 +176,13 @@ static inline int ext4_es_is_written(struct extent_status *es)

static inline int ext4_es_is_unwritten(struct extent_status *es)
{
- return (ext4_es_type(es) & EXTENT_STATUS_UNWRITTEN) != 0;
+ return (ext4_es_type(es) &
+ (EXTENT_STATUS_UNWRITTEN | EXTENT_STATUS_CONVERT_PENDING)) != 0;
+}
+
+static inline int ext4_es_is_convert_pending(struct extent_status *es)
+{
+ return (ext4_es_type(es) & EXTENT_STATUS_CONVERT_PENDING) != 0;
}

static inline int ext4_es_is_delayed(struct extent_status *es)
diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h
index 92048da9dae4..4313694cd4e9 100644
--- a/include/trace/events/ext4.h
+++ b/include/trace/events/ext4.h
@@ -77,13 +77,15 @@ TRACE_DEFINE_ENUM(ES_UNWRITTEN_B);
TRACE_DEFINE_ENUM(ES_DELAYED_B);
TRACE_DEFINE_ENUM(ES_HOLE_B);
TRACE_DEFINE_ENUM(ES_REFERENCED_B);
+TRACE_DEFINE_ENUM(ES_CONVERT_PENDING_B);

#define show_extent_status(status) __print_flags(status, "", \
{ EXTENT_STATUS_WRITTEN, "W" }, \
{ EXTENT_STATUS_UNWRITTEN, "U" }, \
{ EXTENT_STATUS_DELAYED, "D" }, \
{ EXTENT_STATUS_HOLE, "H" }, \
- { EXTENT_STATUS_REFERENCED, "R" })
+ { EXTENT_STATUS_REFERENCED, "R" }, \
+ { EXTENT_STATUS_CONVERT_PENDING, "C" })

#define show_falloc_mode(mode) __print_flags(mode, "|", \
{ FALLOC_FL_KEEP_SIZE, "KEEP_SIZE"}, \

--
2.55.0