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 --- 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) { diff --git a/include/trace/events/ext4.h b/include/trace/events/ext4.h index 4313694cd4e9..fb18fdcb3003 100644 --- a/include/trace/events/ext4.h +++ b/include/trace/events/ext4.h @@ -46,6 +46,7 @@ struct partial_cluster; { EXT4_GET_BLOCKS_CONVERT_UNWRITTEN, "CONVERT_UNWRITTEN" }, \ { EXT4_GET_BLOCKS_ZERO, "ZERO" }, \ { EXT4_GET_BLOCKS_IO_SUBMIT, "IO_SUBMIT" }, \ + { EXT4_GET_BLOCKS_CONVERT_PENDING, "CONV_PENDING" }, \ { EXT4_EX_NOCACHE, "EX_NOCACHE" }) /* -- 2.55.0