Make sure dio write paths pass the EXT4_GET_BLOCKS_CONVERT_PENDING flag to track unwritten ranges that are about to be converted. This enables tracking such ranges in extent status. This avoid data corruption by allowing endio extent zeroout code to avoid zeroing over such ranges. Error note: Ideally, on an IO error we should clear the CONVERT_PENDING bits since they will not be converted, however, this is not straigh forward in dio. This is because nothing stops parallel overlapping dios to mark the same range as CONVERT_PENDING. If one of those fails, clearing CONVERT_PENDING would be wrong because the other dio is still depending on it. For simplicitly, we just don't clear it at all on dio errors because: 1. Clearing unconditionally exposes the range for zeroout corruption that CONVERT_PENDING was designed to solve. Not clearning it is much more forgiving because it just disables zeroout fallback on such ranges. 2. One sideeffect is that we would end up leaving these stale unreclaimable CONVERT_PENDING es entries but this would be rare and hopefully future IOs would consume the stale CONVERT_PENDING ranges if this happens. 3. Solving it would require much more intrusive changes to refcount track each CONVERT_PENDING extent status. Signed-off-by: Ojaswin Mujoo --- fs/ext4/ext4.h | 2 ++ fs/ext4/inode.c | 39 +++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index 8cce99d68978..72de06efb4b2 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -4221,6 +4221,8 @@ extern int ext4_map_blocks(handle_t *handle, struct inode *inode, struct ext4_map_blocks *map, int flags); extern int ext4_map_query_blocks(handle_t *handle, struct inode *inode, struct ext4_map_blocks *map, int flags); +extern void ext4_clear_convert_pending(struct inode *inode, ext4_lblk_t lblk, + ext4_lblk_t len); extern int ext4_map_create_blocks(handle_t *handle, struct inode *inode, struct ext4_map_blocks *map, int flags); extern int ext4_ext_calc_credits_for_single_extent(struct inode *inode, diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c index 3e4c0e1396ab..4aafd0aaea69 100644 --- a/fs/ext4/inode.c +++ b/fs/ext4/inode.c @@ -783,6 +783,37 @@ static bool ext4_map_mark_convert_pending(struct inode *inode, return ok; } +/* + * Drop the convert pending extents in the given range from the extent status + * tree. Convert pending is set when we map an unwritten extent which is + * expected to be converted to written for endio soon. If the IO fails, nobody + * will do that, so just drop the cache. + */ +void ext4_clear_convert_pending(struct inode *inode, ext4_lblk_t lblk, + ext4_lblk_t len) +{ + ext4_lblk_t end = lblk + len - 1; + struct extent_status es; + + if (!len) + return; + + while (lblk <= end) { + ext4_lblk_t start, stop; + + ext4_es_find_extent_range(inode, ext4_es_is_convert_pending, + lblk, end, &es); + if (!es.es_len || es.es_lblk > end) + break; + start = max(lblk, es.es_lblk); + stop = min(end, es.es_lblk + es.es_len - 1); + ext4_es_remove_extent(inode, start, stop - start + 1); + if (stop == end) + break; + lblk = stop + 1; + } +} + /* * The ext4_map_blocks() function tries to look up the requested blocks, * and returns if the blocks are already mapped. @@ -4012,7 +4043,8 @@ static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map, else if (EXT4_LBLK_TO_B(inode, map->m_lblk) >= i_size_read(inode)) m_flags = EXT4_GET_BLOCKS_CREATE; else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)) - m_flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT; + m_flags = EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT | + EXT4_GET_BLOCKS_CONVERT_PENDING; if (flags & IOMAP_ATOMIC) ret = ext4_map_blocks_atomic_write(handle, inode, map, m_flags, @@ -4082,7 +4114,10 @@ int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length, * especially in multi-threaded overwrite requests. */ if (offset + length <= i_size_read(inode)) { - ret = ext4_map_blocks(NULL, inode, &map, 0); + ret = ext4_map_blocks(NULL, inode, &map, + (flags & IOMAP_DAX) ? + 0 : + EXT4_GET_BLOCKS_CONVERT_PENDING); /* * For DAX we convert extents to initialized ones before * copying the data, otherwise we do it after I/O so -- 2.55.0