Re: [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks()
From: Zhang Yi
Date: Wed Dec 02 2026 - 06:03:25 EST
On 11/27/2026 11:10 PM, Ojaswin Mujoo wrote:
> Introduce a new get block flag to indicate that the caller is about to
> issue IO over unwritten ranges and hence wants them to be marked as
> convert pending. This allows upcoming changes to query such ranges before
> trying to zero them out in endio.
>
> We need to be careful to always add the CONVERT_PENDING extent status entry
> under i_data_sem because the upcoming changes will use that sem to
> serialize with the endio zeroout path.
>
> Signed-off-by: Ojaswin Mujoo <ojaswin@xxxxxxxxxxxxx>
> ---
> fs/ext4/ext4.h | 9 ++++++++-
> fs/ext4/inode.c | 23 +++++++++++++++++++++--
> include/trace/events/ext4.h | 1 +
> 3 files changed, 30 insertions(+), 3 deletions(-)
>
> diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
> index 646c017dda1c..8cce99d68978 100644
> --- a/fs/ext4/ext4.h
> +++ b/fs/ext4/ext4.h
> @@ -748,6 +748,12 @@ struct ext4_new_flex_group_data {
> */
> #define EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF 0x1000
>
> +/*
> + * This range will get converted from unwritten to written soon due to endio
> + * conversion.
> + */
> +#define EXT4_GET_BLOCKS_CONVERT_PENDING 0x2000
> +
> /*
> * The bit position of these flags must not overlap with any of the
> * EXT4_GET_BLOCKS_*. They are used by ext4_find_extent(),
> @@ -766,7 +772,8 @@ struct ext4_new_flex_group_data {
> */
> #define EXT4_EX_QUERY_FILTER (EXT4_EX_NOCACHE | EXT4_EX_FORCE_CACHE |\
> EXT4_EX_NOFAIL |\
> - EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF)
> + EXT4_GET_BLOCKS_QUERY_LAST_IN_LEAF |\
> + EXT4_GET_BLOCKS_CONVERT_PENDING)
>
> /*
> * Flags used by ext4_free_blocks
> diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
> index f7fc5fb7e819..3e4c0e1396ab 100644
> --- a/fs/ext4/inode.c
> +++ b/fs/ext4/inode.c
> @@ -750,6 +750,9 @@ int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
>
> status = map->m_flags & EXT4_MAP_UNWRITTEN ?
> EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
> + if (status == EXTENT_STATUS_UNWRITTEN &&
> + flags & EXT4_GET_BLOCKS_CONVERT_PENDING)
> + status = EXTENT_STATUS_CONVERT_PENDING;
> ext4_es_insert_extent(inode, map->m_lblk, map->m_len, map->m_pblk,
> status, flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE);
> map->m_seq = READ_ONCE(EXT4_I(inode)->i_es_seq);
> @@ -911,8 +914,17 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
> }
>
> /* If it is only a block(s) look up */
> - if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
> + if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
> + if (map->m_flags & EXT4_MAP_UNWRITTEN &&
> + flags & EXT4_GET_BLOCKS_CONVERT_PENDING &&
> + !ext4_map_mark_convert_pending(inode, map)) {
> + map->m_flags = 0;
> + map->m_len = orig_mlen;
> + goto create_retry;
> + }
> +
> return retval;
> + }
>
> /*
> * Returns if the blocks have already allocated
> @@ -934,8 +946,15 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
> !(flags & EXT4_GET_BLOCKS_CONVERT)) {
> /* Contains EXT4_GET_BLOCKS_CREATE - mark mapped. */
> ret = check_block_validity(inode, map);
> - if (!ret)
> + if (!ret) {
> + if (flags & EXT4_GET_BLOCKS_CONVERT_PENDING &&
> + !ext4_map_mark_convert_pending(inode, map)) {
> + map->m_flags = 0;
> + map->m_len = orig_mlen;
> + goto create_retry;
> + }
> map->m_flags |= EXT4_MAP_MAPPED;
> + }
> goto out;
> }
> } else if (retval == 0) {
Hi Ojaswin,
Regarding the change in ext4_map_blocks(), maybe we can clean it up a
bit further. The current logic feels a little convoluted. For example,
something like the following, it would also let us drop the
ext4_map_mark_convert_pending() helper added in patch 4. What do you
think?
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 041b7d872604..5d0ba049dabe 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -796,7 +796,6 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
KUNIT_STATIC_STUB_REDIRECT(ext4_map_blocks, handle, inode, map, flags);
- map->m_flags = 0;
ext_debug(inode, "flag 0x%x, max_blocks %u, logical block %lu\n",
flags, map->m_len, (unsigned long) map->m_lblk);
@@ -821,7 +820,9 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
else
ext4_check_map_extents_env(inode);
-create_retry:
+retry:
+ map->m_flags = 0;
+ map->m_len = orig_mlen;
/* Lookup extent status tree firstly */
if (ext4_es_lookup_extent(inode, map->m_lblk, NULL, &es, &map->m_seq)) {
if (ext4_es_is_written(&es) || ext4_es_is_unwritten(&es)) {
@@ -882,36 +883,53 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
goto out;
}
- /* If it is only a block(s) look up */
- if ((flags & EXT4_GET_BLOCKS_CREATE) == 0)
+ /* It is only a block(s) look up */
+ if (!(flags & (EXT4_GET_BLOCKS_CREATE |
+ EXT4_GET_BLOCKS_CONVERT_PENDING)))
return retval;
- /*
- * Returns if the blocks have already allocated
- *
- * Note that if blocks have been preallocated
- * ext4_ext_map_blocks() returns with buffer head unmapped
- */
+ /* Got a written or unwritten extent. */
if (retval > 0) {
- /*
- * If we need to convert written extent to unwritten or
- * convert unwritten extent to written, continue and do
- * the actual work in ext4_ext_map_blocks().
- */
- if (map->m_flags & EXT4_MAP_MAPPED &&
- !(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
- goto out;
- if (map->m_flags & EXT4_MAP_UNWRITTEN &&
- (flags & EXT4_GET_BLOCKS_UNWRIT_EXT) &&
+ if (map->m_flags & EXT4_MAP_MAPPED) {
+ /* Return if we don't convert blocks to unwritten. */
+ if (!(flags & EXT4_GET_BLOCKS_CONVERT_UNWRITTEN))
+ goto out;
+ goto create;
+ }
+
+ /* The caller accepts the unwritten mapping as-is. */
+ if ((flags & (EXT4_GET_BLOCKS_UNWRIT_EXT |
+ EXT4_GET_BLOCKS_CONVERT_PENDING)) &&
!(flags & (EXT4_GET_BLOCKS_CONVERT |
EXT4_GET_BLOCKS_SPLIT_NOMERGE))) {
- /* Contains EXT4_GET_BLOCKS_CREATE - mark mapped. */
ret = check_block_validity(inode, map);
- if (!ret)
+ if (ret)
+ goto out;
+
+ /*
+ * If the tree changed since the lookup, the mapping
+ * is stale and must be looked up again.
+ */
+ if (flags & EXT4_GET_BLOCKS_CONVERT_PENDING) {
+ down_read(&EXT4_I(inode)->i_data_sem);
+ if (map->m_seq !=
+ READ_ONCE(EXT4_I(inode)->i_es_seq)) {
+ up_read(&EXT4_I(inode)->i_data_sem);
+ goto retry;
+ }
+ ext4_es_mark_convert_pending(inode, map->m_lblk,
+ map->m_len, map->m_pblk);
+ up_read(&EXT4_I(inode)->i_data_sem);
+ }
+
+ if (flags & EXT4_GET_BLOCKS_CREATE)
map->m_flags |= EXT4_MAP_MAPPED;
goto out;
}
+ /* Got a hole or delalloc extent. */
} else if (retval == 0) {
+ if (!(flags & EXT4_GET_BLOCKS_CREATE))
+ goto out;
/*
* Do not allocate blocks for holes in the context of
* data submission path.
@@ -920,6 +938,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
goto out;
}
+create:
if (!handle) {
handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
ext4_chunk_trans_blocks(inode, orig_mlen));
@@ -949,9 +968,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
*/
if (map->m_seq != READ_ONCE(EXT4_I(inode)->i_es_seq)) {
up_write(&EXT4_I(inode)->i_data_sem);
- map->m_flags = 0;
- map->m_len = orig_mlen;
- goto create_retry;
+ goto retry;
}
retval = ext4_map_create_blocks(handle, inode, map, flags);
up_write((&EXT4_I(inode)->i_data_sem));