mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Zhang Yi <yi.zhang@huaweicloud.com>
To: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Cc: Theodore Ts'o <tytso@mit.edu>,
	Andreas Dilger <adilger.kernel@dilger.ca>,
	Baokun Li <libaokun@linux.alibaba.com>, Jan Kara <jack@suse.cz>,
	"Ritesh Harjani (IBM)" <ritesh.list@gmail.com>,
	linux-ext4@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks()
Date: Wed, 2 Dec 2026 19:03:03 +0800	[thread overview]
Message-ID: <3e5119b7-38d0-4ed7-a3d6-64fd8fccc468@huaweicloud.com> (raw)
In-Reply-To: <20261127-ext4-endio-zero-bug-v2-5-4106968e4d15@linux.ibm.com>

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@linux.ibm.com>
> ---
>  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));





  reply	other threads:[~2026-12-02 11:03 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-27 15:10 [PATCH v2 00/13] ext4: fix endio zeroout corruption by tracking to-be-converted unwritten IOs Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 01/13] ext4: disable zeroout fallback in ext4_split_extent() Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 02/13] ext4: introduce CONVERT_PENDING extent status type Ojaswin Mujoo
2026-12-02  6:53   ` Zhang Yi
2026-11-27 15:10 ` [PATCH v2 03/13] ext4: preserve convert pending extents when adding unwrit extents to es cache Ojaswin Mujoo
2026-12-01 12:54   ` Zhang Yi
2026-11-27 15:10 ` [PATCH v2 04/13] ext4: add es helper to convert unwrit to convert pending without changing i_es_seq Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 05/13] ext4: add support to track convert pending ranges in ext4_map_blocks() Ojaswin Mujoo
2026-12-02 11:03   ` Zhang Yi [this message]
2026-11-27 15:10 ` [PATCH v2 06/13] ext4: add tracking for unwritten to-be-converted ranges in dio write path Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 07/13] ext4: add tracking for unwritten to-be-converted ranges in non iomap buffered path Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 08/13] ext4: add tracking for unwritten to-be-converted ranges in " Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 09/13] ext4: don't zeroout if conv pending ranges are present Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 10/13] ext4: add tracepoints for extent split and convert Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 11/13] ext4: update extent status cache when zeroing extents Ojaswin Mujoo
2026-11-27 15:10 ` [PATCH v2 12/13] ext4: don't drop the extents in case extent split fails Ojaswin Mujoo
2026-11-27 15:11 ` [PATCH v2 13/13] ext4: re-enable zeroout fallback Ojaswin Mujoo

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=3e5119b7-38d0-4ed7-a3d6-64fd8fccc468@huaweicloud.com \
    --to=yi.zhang@huaweicloud.com \
    --cc=adilger.kernel@dilger.ca \
    --cc=jack@suse.cz \
    --cc=libaokun@linux.alibaba.com \
    --cc=linux-ext4@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ojaswin@linux.ibm.com \
    --cc=ritesh.list@gmail.com \
    --cc=tytso@mit.edu \
    /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

all inboxes | Powered by JetHome®