* [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc
@ 2025-04-30 5:20 Ritesh Harjani (IBM)
2025-04-30 5:20 ` [RFC v2 1/2] ext4: Add multi-fsblock atomic write support with bigalloc Ritesh Harjani (IBM)
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Ritesh Harjani (IBM) @ 2025-04-30 5:20 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara
Cc: John Garry, djwong, Ojaswin Mujoo, linux-ext4,
Ritesh Harjani (IBM)
This is still an early preview (RFC v2) of multi-fsblock atomic write. Since the
core design of the feature looks ready, wanted to post this for some early
feedback. We will break this into more smaller and meaningful patches in later
revision. However to simplify the review of the core design changes, this
version is limited to just two patches. Individual patches might have more
details in the commit msg.
Note: This overall needs more careful review (other than the core design) which
I will be doing in parallel. However it would be helpful if one can provide any
feedback on the core design changes. Specially around ext4_iomap_alloc()
changes, ->end_io() changes and a new get block flag
EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS.
v1 -> v2:
==========
1. Handled review comments from Ojaswin to optimize the ext4_map_block() calls
in ext4_iomap_alloc().
2. Fixed the journal credits calculation for both:
- during block allocation in ext4_iomap_alloc()
- during dio completion in ->end_io callback.
Earlier we were starting multiple txns in ->end_io callback for unwritten to
written conversion. But since in case of atomic writes, we want a single jbd2
txn, hence made the necessary changes there.
TODOs:
======
1. although this survived quick tests and some custom fio tests for atomic
write, but I still think we need to add more tests for the corner cases.
Will work on adding such corner test cases to xfstests.
2. Many fsx related tests in quick group were failing with bigalloc. But those
were failing even without these patches. Need further investigation.
3. Finish more thorough testing of the series.
4. Refactoring + Cleanups and a more careful review of the overall patch series
before sending v3.
Ritesh Harjani (IBM) (2):
ext4: Add multi-fsblock atomic write support with bigalloc
ext4: Add support for EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS
fs/ext4/ext4.h | 9 ++
fs/ext4/extents.c | 69 ++++++++++++++
fs/ext4/file.c | 7 +-
fs/ext4/inode.c | 231 ++++++++++++++++++++++++++++++++++++++++++----
fs/ext4/super.c | 8 +-
5 files changed, 302 insertions(+), 22 deletions(-)
--
2.49.0
^ permalink raw reply [flat|nested] 6+ messages in thread
* [RFC v2 1/2] ext4: Add multi-fsblock atomic write support with bigalloc
2025-04-30 5:20 [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc Ritesh Harjani (IBM)
@ 2025-04-30 5:20 ` Ritesh Harjani (IBM)
2025-04-30 5:20 ` [RFC v2 2/2] ext4: Add support for EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS Ritesh Harjani (IBM)
2025-05-08 12:01 ` [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc John Garry
2 siblings, 0 replies; 6+ messages in thread
From: Ritesh Harjani (IBM) @ 2025-04-30 5:20 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara
Cc: John Garry, djwong, Ojaswin Mujoo, linux-ext4,
Ritesh Harjani (IBM)
EXT4 supports bigalloc feature which allows the FS to work in size of
clusters (group of blocks) rather than individual blocks. This patch
adds atomic write support for bigalloc so that systems with bs = ps can
also create FS using -
mkfs.ext4 -F -O bigalloc -b 4096 -C 16384 <dev>
With bigalloc ext4 can support multi-fsblock atomic writes. We will have to
adjust ext4's atomic write unit max value to cluster size. This can then support
atomic write of size anywhere between [blocksize, clustersize].
We first query the underlying region of the requested range by calling
ext4_map_blocks() call. Here are the various cases which we then handle
for block allocation depending upon the underlying mapping type:
1. If the underlying region for the entire requested range is a mapped extent,
then we don't call ext4_map_blocks() to allocate anything. We don't need to
even start the jbd2 txn in this case.
2. For an append write case, we create a mapped extent.
3. If the underlying region is entirely a hole, then we create an unwritten
extent for the requested range.
4. If the underlying region is a large unwritten extent, then we split the
extent into 2 unwritten extent of required size.
5. If the underlying region has any type of mixed mapping, then we call
ext4_map_blocks() in a loop to zero out the unwritten and the hole regions
within the requested range. This then provide a single mapped extent type
mapping for the requested range.
Note: We invoke ext4_map_blocks() in a loop with the EXT4_GET_BLOCKS_ZERO
flag only when the underlying extent mapping of the requested range is
not entirely a hole, an unwritten extent, or a fully mapped extent. That
is, if the underlying region contains a mix of hole(s), unwritten
extent(s), and mapped extent(s), we use this loop to ensure that all the
short mappings are zeroed out. This guarantees that the entire requested
range becomes a single, uniformly mapped extent. It is ok to do so
because we know this is being done on a bigalloc enabled filesystem
where the block bitmap represents the entire cluster unit.
Co-developed-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
---
fs/ext4/ext4.h | 3 +
fs/ext4/extents.c | 64 +++++++++++++++++++++
fs/ext4/file.c | 7 ++-
fs/ext4/inode.c | 142 +++++++++++++++++++++++++++++++++++++++++++---
fs/ext4/super.c | 8 ++-
5 files changed, 213 insertions(+), 11 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 5a20e9cd7184..589d51389327 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -3039,6 +3039,7 @@ extern void ext4_set_aops(struct inode *inode);
extern int ext4_writepage_trans_blocks(struct inode *);
extern int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode);
extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
+extern int ext4_meta_trans_blocks(struct inode *, int nrblocks, int extents);
extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
loff_t lstart, loff_t lend);
extern vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf);
@@ -3710,6 +3711,8 @@ extern long ext4_fallocate(struct file *file, int mode, loff_t offset,
loff_t len);
extern int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
loff_t offset, ssize_t len);
+extern int ext4_convert_unwritten_extents_atomic(handle_t *handle,
+ struct inode *inode, loff_t offset, ssize_t len);
extern int ext4_convert_unwritten_io_end_vec(handle_t *handle,
ext4_io_end_t *io_end);
extern int ext4_map_blocks(handle_t *handle, struct inode *inode,
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index c616a16a9f36..0e00b78b521c 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -4780,6 +4780,70 @@ long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
return ret;
}
+int ext4_convert_unwritten_extents_atomic(handle_t *handle, struct inode *inode,
+ loff_t offset, ssize_t len)
+{
+ unsigned int max_blocks;
+ int ret = 0, ret2 = 0, ret3 = 0;
+ struct ext4_map_blocks map;
+ unsigned int blkbits = inode->i_blkbits;
+ unsigned int credits = 0;
+ int flags = EXT4_GET_BLOCKS_IO_CONVERT_EXT;
+
+ map.m_lblk = offset >> blkbits;
+ max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
+
+ if (!handle) {
+ /*
+ * TODO: Should we query whether the extent is split across two
+ * leaf blocks. Or shall we just consider the worst case credits
+ * of inserting 2 extents into extent tree
+ */
+ credits = ext4_meta_trans_blocks(inode, max_blocks, 2);
+ }
+
+ if (credits) {
+ handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
+ credits);
+ if (IS_ERR(handle)) {
+ ret = PTR_ERR(handle);
+ return ret;
+ }
+ }
+
+ while (ret >= 0 && ret < max_blocks) {
+ map.m_lblk += ret;
+ map.m_len = (max_blocks -= ret);
+ ret = ext4_map_blocks(handle, inode, &map, flags);
+ if (ret != max_blocks)
+ ext4_warning(inode->i_sb,
+ "inode #%lu: block %u: len %u: "
+ "split block mapping found for atomic write,"
+ "ret = %d",
+ inode->i_ino, map.m_lblk,
+ map.m_len, ret);
+ if (ret <= 0)
+ break;
+ }
+
+ ret2 = ext4_mark_inode_dirty(handle, inode);
+
+ if (credits) {
+ ret3 = ext4_journal_stop(handle);
+ if (unlikely(ret3))
+ ret2 = ret3;
+ }
+
+ if (ret <= 0 || ret2)
+ ext4_warning(inode->i_sb,
+ "inode #%lu: block %u: len %u: "
+ "returned %d or %d",
+ inode->i_ino, map.m_lblk,
+ map.m_len, ret, ret2);
+
+ return ret > 0 ? ret2 : ret;
+}
+
/*
* This function convert a range of blocks to written extents
* The caller of this function will pass the start offset and the size.
diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index beb078ee4811..959328072c15 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -377,7 +377,12 @@ static int ext4_dio_write_end_io(struct kiocb *iocb, ssize_t size,
loff_t pos = iocb->ki_pos;
struct inode *inode = file_inode(iocb->ki_filp);
- if (!error && size && flags & IOMAP_DIO_UNWRITTEN)
+
+ if (!error && size && (flags & IOMAP_DIO_UNWRITTEN) &&
+ (iocb->ki_flags & IOCB_ATOMIC))
+ error = ext4_convert_unwritten_extents_atomic(NULL, inode, pos,
+ size);
+ else if (!error && size && flags & IOMAP_DIO_UNWRITTEN)
error = ext4_convert_unwritten_extents(NULL, inode, pos, size);
if (error)
return error;
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 94c7d2d828a6..27235a76c2d1 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -142,9 +142,6 @@ static inline int ext4_begin_ordered_truncate(struct inode *inode,
new_size);
}
-static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
- int pextents);
-
/*
* Test whether an inode is a fast symlink.
* A fast symlink has its symlink data stored in ext4_inode_info->i_data.
@@ -3340,6 +3337,91 @@ static void ext4_set_iomap(struct inode *inode, struct iomap *iomap,
}
}
+static int ext4_map_blocks_atomic_write_slow(handle_t *handle,
+ struct inode *inode, struct ext4_map_blocks *map)
+{
+ ext4_lblk_t m_lblk = map->m_lblk;
+ unsigned int m_len = map->m_len;
+ unsigned int mapped_len = 0, m_flags = 0;
+ int ret = 0;
+
+ /*
+ * This is a slow path in case of mixed mapping. We use
+ * EXTT4_GET_BLOCKS_CREATE_ZERO flag here to make sure we get a single
+ * contiguous mapping. This will ensure any unwritten or hole regions
+ * within the requested range is zeroed out and we return a single
+ * contiguous mapped extent.
+ */
+ m_flags = EXT4_GET_BLOCKS_CREATE_ZERO;
+
+ do {
+ ret = ext4_map_blocks(handle, inode, map, m_flags);
+ if (ret < 0)
+ goto out;
+ mapped_len += map->m_len;
+ map->m_lblk += map->m_len;
+ map->m_len = m_len - mapped_len;
+ } while (mapped_len < m_len);
+
+ /*
+ * We might have done some work in above loop, so we need to query the
+ * start of the physical extent, based on the origin m_lblk and m_len.
+ * Let's also ensure we were able to allocate the required range for
+ * mixed mapping case.
+ */
+ map->m_lblk = m_lblk;
+ map->m_len = m_len;
+
+ ret = ext4_map_blocks(handle, inode, map, 0);
+ if (ret != m_len) {
+ ext4_warning_inode(inode, "allocation failed for atomic write request pos:%u, len:%u\n",
+ m_lblk, m_len);
+ ret = -EINVAL;
+ }
+out:
+ return ret;
+
+}
+
+/*
+ * ext4_map_blocks_atomic: Helper routine to ensure the entire requested mapping
+ * [map.m_lblk, map.m_len] is one single contiguous extent with no mixed
+ * mappings. For the normal case we re-use the mappings passed to us by m_flags.
+ *
+ * We call EXT4_GET_BLOCKS_ZERO (in the slow path) only when the underlying
+ * physical extent for the requested range does not have a single mapping type
+ * (Hole, Mapped, or Unwritten) throughout. In that case we will loop over the
+ * requested range to allocate and zero out the unwritten / holes in between, to
+ * get a single mapped extent from [m_lblk, m_len]. This case is mostly
+ * non-performance critical path, so it should be ok to loop using
+ * ext4_map_blocks() with appropriate flags to allocate & zero the underlying
+ * short holes/unwritten extents within the requested range.
+ */
+static int ext4_map_blocks_atomic_write(handle_t *handle, struct inode *inode,
+ struct ext4_map_blocks *map, int m_flags)
+{
+ ext4_lblk_t m_lblk = map->m_lblk;
+ unsigned int m_len = map->m_len;
+ int ret = 0;
+
+ WARN_ON_ONCE(m_len > 1 && !ext4_has_feature_bigalloc(inode->i_sb));
+
+ ret = ext4_map_blocks(handle, inode, map, m_flags);
+ if (ret < 0 || ret == m_len)
+ goto out;
+ /*
+ * This is a mixed mapping case where we were not able to allocate
+ * a single contiguous extent. In that case let's reset requested
+ * mapping and call the slow path.
+ */
+ map->m_lblk = m_lblk;
+ map->m_len = m_len;
+
+ return ext4_map_blocks_atomic_write_slow(handle, inode, map);
+out:
+ return ret;
+}
+
static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
unsigned int flags)
{
@@ -3353,7 +3435,24 @@ static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
*/
if (map->m_len > DIO_MAX_BLOCKS)
map->m_len = DIO_MAX_BLOCKS;
- dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
+
+ if (flags & IOMAP_ATOMIC) {
+ unsigned int orig_mlen = map->m_len;
+
+ ret = ext4_map_blocks(NULL, inode, map, 0);
+ if (ret < 0)
+ return ret;
+ if (map->m_len < orig_mlen) {
+ map->m_len = orig_mlen;
+ dio_credits = ext4_meta_trans_blocks(inode, orig_mlen,
+ map->m_len / 2);
+ } else {
+ dio_credits = ext4_chunk_trans_blocks(inode,
+ map->m_len);
+ }
+ } else {
+ dio_credits = ext4_chunk_trans_blocks(inode, map->m_len);
+ }
retry:
/*
@@ -3384,7 +3483,10 @@ static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
else if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
m_flags = EXT4_GET_BLOCKS_IO_CREATE_EXT;
- ret = ext4_map_blocks(handle, inode, map, m_flags);
+ if (flags & IOMAP_ATOMIC)
+ ret = ext4_map_blocks_atomic_write(handle, inode, map, m_flags);
+ else
+ ret = ext4_map_blocks(handle, inode, map, m_flags);
/*
* We cannot fill holes in indirect tree based inodes as that could
@@ -3408,6 +3510,7 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
int ret;
struct ext4_map_blocks map;
u8 blkbits = inode->i_blkbits;
+ unsigned int m_len_orig;
if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
return -EINVAL;
@@ -3421,6 +3524,7 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
map.m_lblk = offset >> blkbits;
map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
+ m_len_orig = map.m_len;
if (flags & IOMAP_WRITE) {
/*
@@ -3431,11 +3535,23 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
*/
if (offset + length <= i_size_read(inode)) {
ret = ext4_map_blocks(NULL, inode, &map, 0);
- if (ret > 0 && (map.m_flags & EXT4_MAP_MAPPED))
- goto out;
+ /*
+ * For atomic writes the entire requested length should
+ * be mapped.
+ */
+ if (map.m_flags & EXT4_MAP_MAPPED) {
+ if ((!(flags & IOMAP_ATOMIC) && ret > 0) ||
+ (flags & IOMAP_ATOMIC && ret >= m_len_orig))
+ goto out;
+ }
+ map.m_len = m_len_orig;
}
ret = ext4_iomap_alloc(inode, &map, flags);
} else {
+ /*
+ * This can be called for overwrites path from
+ * ext4_iomap_overwrite_begin().
+ */
ret = ext4_map_blocks(NULL, inode, &map, 0);
}
@@ -3449,6 +3565,16 @@ static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
*/
map.m_len = fscrypt_limit_io_blocks(inode, map.m_lblk, map.m_len);
+ /*
+ * Before returning to iomap, let's ensure the allocated mapping
+ * covers the entire requested length for atomic writes.
+ */
+ if (flags & IOMAP_ATOMIC) {
+ if (map.m_len < (length >> blkbits)) {
+ WARN_ON(1);
+ return -EINVAL;
+ }
+ }
ext4_set_iomap(inode, iomap, &map, offset, length, flags);
return 0;
@@ -5773,7 +5899,7 @@ static int ext4_index_trans_blocks(struct inode *inode, int lblocks,
*
* Also account for superblock, inode, quota and xattr blocks
*/
-static int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
+int ext4_meta_trans_blocks(struct inode *inode, int lblocks,
int pextents)
{
ext4_group_t groups, ngroups = ext4_get_groups_count(inode->i_sb);
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 181934499624..5c319d08446f 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -4442,12 +4442,13 @@ static int ext4_handle_clustersize(struct super_block *sb)
/*
* ext4_atomic_write_init: Initializes filesystem min & max atomic write units.
* @sb: super block
- * TODO: Later add support for bigalloc
*/
static void ext4_atomic_write_init(struct super_block *sb)
{
struct ext4_sb_info *sbi = EXT4_SB(sb);
struct block_device *bdev = sb->s_bdev;
+ unsigned int blkbits = sb->s_blocksize_bits;
+ unsigned int clustersize = sb->s_blocksize;
if (!bdev_can_atomic_write(bdev))
return;
@@ -4455,9 +4456,12 @@ static void ext4_atomic_write_init(struct super_block *sb)
if (!ext4_has_feature_extents(sb))
return;
+ if (ext4_has_feature_bigalloc(sb))
+ clustersize = 1U << (sbi->s_cluster_bits + blkbits);
+
sbi->s_awu_min = max(sb->s_blocksize,
bdev_atomic_write_unit_min_bytes(bdev));
- sbi->s_awu_max = min(sb->s_blocksize,
+ sbi->s_awu_max = min(clustersize,
bdev_atomic_write_unit_max_bytes(bdev));
if (sbi->s_awu_min && sbi->s_awu_max &&
sbi->s_awu_min <= sbi->s_awu_max) {
--
2.49.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [RFC v2 2/2] ext4: Add support for EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS
2025-04-30 5:20 [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc Ritesh Harjani (IBM)
2025-04-30 5:20 ` [RFC v2 1/2] ext4: Add multi-fsblock atomic write support with bigalloc Ritesh Harjani (IBM)
@ 2025-04-30 5:20 ` Ritesh Harjani (IBM)
2025-05-08 12:01 ` [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc John Garry
2 siblings, 0 replies; 6+ messages in thread
From: Ritesh Harjani (IBM) @ 2025-04-30 5:20 UTC (permalink / raw)
To: Theodore Ts'o, Jan Kara
Cc: John Garry, djwong, Ojaswin Mujoo, linux-ext4,
Ritesh Harjani (IBM)
There can be a case where there are contiguous extents on the adjacent
leaf nodes of on-disk extent trees. So when someone tries to write to
this contiguous range, ext4_map_blocks() call will split by returning
1 extent at a time if this is not already cached in extent_status tree
cache (where if these extents when cached can get merged since they are
contiguous).
This is fine for a normal write however in case of atomic writes, it
can't afford to break the write into two. Now this is also something
that will only happen in the slow write case where we call
ext4_map_blocks() for each of these extents spread across different leaf
nodes. However, there is no guarantee that these extent status cache
cannot be reclaimed before the last call to ext4_map_blocks() in
ext4_map_blocks_atomic_write_slow().
Hence this patch adds support of EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS.
This flag checks if the requested range can be fully found in extent
status cache and return. If not, it looks up in on-disk extent
tree via ext4_map_query_blocks(). If the found extent is the last entry
in the leaf node, then it goes and queries the next lblk to see if there
is an adjacent contiguous extent in the adjacent leaf node of the
on-disk extent tree.
Even though there can be a case where there are multiple adjacent extent
entries spread across multiple leaf nodes. But we only read an adjacent
leaf block i.e. in total of 2 extent entries spread across 2 leaf nodes.
The reason for this is that we are mostly only going to support atomic
writes with upto 64KB or maybe max upto 1MB of atomic write support.
Co-developed-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Signed-off-by: Ojaswin Mujoo <ojaswin@linux.ibm.com>
Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@gmail.com>
---
fs/ext4/ext4.h | 6 ++++
fs/ext4/extents.c | 9 +++--
fs/ext4/inode.c | 91 ++++++++++++++++++++++++++++++++++++++++-------
3 files changed, 92 insertions(+), 14 deletions(-)
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 589d51389327..38f75d33d67f 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -256,6 +256,7 @@ struct ext4_allocation_request {
#define EXT4_MAP_UNWRITTEN BIT(BH_Unwritten)
#define EXT4_MAP_BOUNDARY BIT(BH_Boundary)
#define EXT4_MAP_DELAYED BIT(BH_Delay)
+#define EXT4_MAP_LAST_IN_LEAF BIT(BH_PrivateStart)
#define EXT4_MAP_FLAGS (EXT4_MAP_NEW | EXT4_MAP_MAPPED |\
EXT4_MAP_UNWRITTEN | EXT4_MAP_BOUNDARY |\
EXT4_MAP_DELAYED)
@@ -725,6 +726,11 @@ enum {
#define EXT4_GET_BLOCKS_IO_SUBMIT 0x0400
/* Caller is in the atomic contex, find extent if it has been cached */
#define EXT4_GET_BLOCKS_CACHED_NOWAIT 0x0800
+/*
+ * Atomic write caller needs this to query in the slow path of mixed mapping
+ * case, when a contiguous extent can be split across two adjacent leaf nodes.
+ */
+#define EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS 0x1000
/*
* The bit position of these flags must not overlap with any of the
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index 0e00b78b521c..12fae8d70f46 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -4433,6 +4433,10 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
allocated = map->m_len;
ext4_ext_show_leaf(inode, path);
out:
+ if (flags & EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS)
+ if (ex && (ex == EXT_LAST_EXTENT(path[depth].p_hdr)))
+ map->m_flags |= EXT4_MAP_LAST_IN_LEAF;
+
ext4_free_ext_path(path);
trace_ext4_ext_map_blocks_exit(inode, flags, map,
@@ -4788,7 +4792,8 @@ int ext4_convert_unwritten_extents_atomic(handle_t *handle, struct inode *inode,
struct ext4_map_blocks map;
unsigned int blkbits = inode->i_blkbits;
unsigned int credits = 0;
- int flags = EXT4_GET_BLOCKS_IO_CONVERT_EXT;
+ int flags = EXT4_GET_BLOCKS_IO_CONVERT_EXT |
+ EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS;
map.m_lblk = offset >> blkbits;
max_blocks = EXT4_MAX_BLOCKS(len, offset, blkbits);
@@ -4815,7 +4820,7 @@ int ext4_convert_unwritten_extents_atomic(handle_t *handle, struct inode *inode,
map.m_lblk += ret;
map.m_len = (max_blocks -= ret);
ret = ext4_map_blocks(handle, inode, &map, flags);
- if (ret != max_blocks)
+ if (!(map.m_flags & EXT4_MAP_LAST_IN_LEAF) && ret != max_blocks)
ext4_warning(inode->i_sb,
"inode #%lu: block %u: len %u: "
"split block mapping found for atomic write,"
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 27235a76c2d1..f5c8c4b8cd16 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -459,14 +459,69 @@ static void ext4_map_blocks_es_recheck(handle_t *handle,
}
#endif /* ES_AGGRESSIVE_TEST */
+static int ext4_map_query_last_in_leaf_blocks(handle_t *handle,
+ struct inode *inode, struct ext4_map_blocks *map,
+ unsigned int orig_mlen)
+{
+ struct ext4_map_blocks map2;
+ unsigned int status, status2;
+ int retval;
+
+ status = map->m_flags & EXT4_MAP_UNWRITTEN ?
+ EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
+
+ WARN_ON_ONCE(!(map->m_flags & EXT4_MAP_LAST_IN_LEAF));
+
+ map2.m_lblk = map->m_lblk + map->m_len;
+ map2.m_len = INT_MAX;
+ map2.m_flags = 0;
+ retval = ext4_ext_map_blocks(handle, inode, &map2, 0);
+
+ if (retval <= 0) {
+ ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
+ map->m_pblk, status, false);
+ return map->m_len;
+
+ }
+
+ if (unlikely(retval != map2.m_len)) {
+ ext4_warning(inode->i_sb,
+ "ES len assertion failed for inode "
+ "%lu: retval %d != map->m_len %d",
+ inode->i_ino, retval, map2.m_len);
+ WARN_ON(1);
+ }
+
+ status2 = map2.m_flags & EXT4_MAP_UNWRITTEN ?
+ EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
+
+ if (map->m_pblk + map->m_len == map2.m_pblk &&
+ status == status2) {
+ ext4_es_insert_extent(inode, map->m_lblk,
+ map->m_len + map2.m_len, map->m_pblk,
+ status, false);
+ if (in_range(orig_mlen, map->m_len,
+ map->m_len + map2.m_len + 1))
+ map->m_len = orig_mlen;
+ } else {
+ ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
+ map->m_pblk, status, false);
+ }
+
+ return map->m_len;
+}
+
static int ext4_map_query_blocks(handle_t *handle, struct inode *inode,
- struct ext4_map_blocks *map)
+ struct ext4_map_blocks *map, int flags)
{
unsigned int status;
int retval;
+ unsigned int orig_mlen = map->m_len;
+
+ flags = flags & EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS;
if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
- retval = ext4_ext_map_blocks(handle, inode, map, 0);
+ retval = ext4_ext_map_blocks(handle, inode, map, flags);
else
retval = ext4_ind_map_blocks(handle, inode, map, 0);
@@ -481,11 +536,16 @@ static int ext4_map_query_blocks(handle_t *handle, struct inode *inode,
WARN_ON(1);
}
- status = map->m_flags & EXT4_MAP_UNWRITTEN ?
- EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
- ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
- map->m_pblk, status, false);
- return retval;
+ if (!(map->m_flags & EXT4_MAP_LAST_IN_LEAF)) {
+ status = map->m_flags & EXT4_MAP_UNWRITTEN ?
+ EXTENT_STATUS_UNWRITTEN : EXTENT_STATUS_WRITTEN;
+ ext4_es_insert_extent(inode, map->m_lblk, map->m_len,
+ map->m_pblk, status, false);
+ return retval;
+ }
+
+ return ext4_map_query_last_in_leaf_blocks(handle, inode, map,
+ orig_mlen);
}
static int ext4_map_create_blocks(handle_t *handle, struct inode *inode,
@@ -599,6 +659,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
struct extent_status es;
int retval;
int ret = 0;
+ unsigned int orig_mlen = map->m_len;
#ifdef ES_AGGRESSIVE_TEST
struct ext4_map_blocks orig_map;
@@ -650,7 +711,12 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
ext4_map_blocks_es_recheck(handle, inode, map,
&orig_map, flags);
#endif
- goto found;
+ if (!(flags & EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS) ||
+ orig_mlen == map->m_len)
+ goto found;
+
+ if (flags & EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS)
+ map->m_len = orig_mlen;
}
/*
* In the query cache no-wait mode, nothing we can do more if we
@@ -664,7 +730,7 @@ int ext4_map_blocks(handle_t *handle, struct inode *inode,
* file system block.
*/
down_read(&EXT4_I(inode)->i_data_sem);
- retval = ext4_map_query_blocks(handle, inode, map);
+ retval = ext4_map_query_blocks(handle, inode, map, flags);
up_read((&EXT4_I(inode)->i_data_sem));
found:
@@ -1802,7 +1868,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
if (ext4_has_inline_data(inode))
retval = 0;
else
- retval = ext4_map_query_blocks(NULL, inode, map);
+ retval = ext4_map_query_blocks(NULL, inode, map, 0);
up_read(&EXT4_I(inode)->i_data_sem);
if (retval)
return retval < 0 ? retval : 0;
@@ -1825,7 +1891,7 @@ static int ext4_da_map_blocks(struct inode *inode, struct ext4_map_blocks *map)
goto found;
}
} else if (!ext4_has_inline_data(inode)) {
- retval = ext4_map_query_blocks(NULL, inode, map);
+ retval = ext4_map_query_blocks(NULL, inode, map, 0);
if (retval) {
up_write(&EXT4_I(inode)->i_data_sem);
return retval < 0 ? retval : 0;
@@ -3372,7 +3438,8 @@ static int ext4_map_blocks_atomic_write_slow(handle_t *handle,
map->m_lblk = m_lblk;
map->m_len = m_len;
- ret = ext4_map_blocks(handle, inode, map, 0);
+ ret = ext4_map_blocks(handle, inode, map,
+ EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS);
if (ret != m_len) {
ext4_warning_inode(inode, "allocation failed for atomic write request pos:%u, len:%u\n",
m_lblk, m_len);
--
2.49.0
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc
2025-04-30 5:20 [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc Ritesh Harjani (IBM)
2025-04-30 5:20 ` [RFC v2 1/2] ext4: Add multi-fsblock atomic write support with bigalloc Ritesh Harjani (IBM)
2025-04-30 5:20 ` [RFC v2 2/2] ext4: Add support for EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS Ritesh Harjani (IBM)
@ 2025-05-08 12:01 ` John Garry
2025-05-08 14:35 ` Ritesh Harjani
2 siblings, 1 reply; 6+ messages in thread
From: John Garry @ 2025-05-08 12:01 UTC (permalink / raw)
To: Ritesh Harjani (IBM), Theodore Ts'o, Jan Kara
Cc: djwong, Ojaswin Mujoo, linux-ext4
On 30/04/2025 06:20, Ritesh Harjani (IBM) wrote:
> This is still an early preview (RFC v2) of multi-fsblock atomic write. Since the
> core design of the feature looks ready, wanted to post this for some early
> feedback. We will break this into more smaller and meaningful patches in later
> revision. However to simplify the review of the core design changes, this
> version is limited to just two patches. Individual patches might have more
> details in the commit msg.
>
> Note: This overall needs more careful review (other than the core design) which
> I will be doing in parallel. However it would be helpful if one can provide any
> feedback on the core design changes. Specially around ext4_iomap_alloc()
> changes, ->end_io() changes and a new get block flag
> EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS.
I gave this a try and it looks ok, specifically atomic writing mixed
mappings.
I'll try to look closer that the implementation details. But I do note
that you use blkdev_issue_zeroout() to pre-zero any unwritten range
which is being atomically written.
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc
2025-05-08 12:01 ` [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc John Garry
@ 2025-05-08 14:35 ` Ritesh Harjani
2025-05-08 15:19 ` Darrick J. Wong
0 siblings, 1 reply; 6+ messages in thread
From: Ritesh Harjani @ 2025-05-08 14:35 UTC (permalink / raw)
To: John Garry, Theodore Ts'o, Jan Kara; +Cc: djwong, Ojaswin Mujoo, linux-ext4
John Garry <john.g.garry@oracle.com> writes:
> On 30/04/2025 06:20, Ritesh Harjani (IBM) wrote:
>> This is still an early preview (RFC v2) of multi-fsblock atomic write. Since the
>> core design of the feature looks ready, wanted to post this for some early
>> feedback. We will break this into more smaller and meaningful patches in later
>> revision. However to simplify the review of the core design changes, this
>> version is limited to just two patches. Individual patches might have more
>> details in the commit msg.
>>
>> Note: This overall needs more careful review (other than the core design) which
>> I will be doing in parallel. However it would be helpful if one can provide any
>> feedback on the core design changes. Specially around ext4_iomap_alloc()
>> changes, ->end_io() changes and a new get block flag
>> EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS.
>
> I gave this a try and it looks ok, specifically atomic writing mixed
> mappings.
>
Thanks John for taking a look.
> I'll try to look closer that the implementation details.
We are in the process of sending v3 (hopefully by tonight) which is an
improved version w.r.t error handling, journal credits and few other
changes. Although nothing has changed w.r.t the design aspect.
> But I do note
> that you use blkdev_issue_zeroout() to pre-zero any unwritten range
> which is being atomically written.
Yes, that is how internally ext4_map_blocks() with
EXT4_GET_BLOCKS_CREATE_ZERO will return us the allocated blocks. During
block allocation, on mixed mapping range, we ensure that the entire range
becomes a contiguous mapped extent before starting any data writes.
That means calling ext4_map_blocks() in a loop with
EXT4_GET_BLOCKS_CREATE_ZERO, so that it can zero out any unwritten
extents in the requested region.
I assume writing over a mixed mapping region is not a performance
critical path.
Do you forsee any problems with the approach (since you said "But I do note...")?
-ritesh
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc
2025-05-08 14:35 ` Ritesh Harjani
@ 2025-05-08 15:19 ` Darrick J. Wong
0 siblings, 0 replies; 6+ messages in thread
From: Darrick J. Wong @ 2025-05-08 15:19 UTC (permalink / raw)
To: Ritesh Harjani
Cc: John Garry, Theodore Ts'o, Jan Kara, Ojaswin Mujoo,
linux-ext4
On Thu, May 08, 2025 at 08:05:27PM +0530, Ritesh Harjani wrote:
> John Garry <john.g.garry@oracle.com> writes:
>
> > On 30/04/2025 06:20, Ritesh Harjani (IBM) wrote:
> >> This is still an early preview (RFC v2) of multi-fsblock atomic write. Since the
> >> core design of the feature looks ready, wanted to post this for some early
> >> feedback. We will break this into more smaller and meaningful patches in later
> >> revision. However to simplify the review of the core design changes, this
> >> version is limited to just two patches. Individual patches might have more
> >> details in the commit msg.
> >>
> >> Note: This overall needs more careful review (other than the core design) which
> >> I will be doing in parallel. However it would be helpful if one can provide any
> >> feedback on the core design changes. Specially around ext4_iomap_alloc()
> >> changes, ->end_io() changes and a new get block flag
> >> EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS.
> >
> > I gave this a try and it looks ok, specifically atomic writing mixed
> > mappings.
> >
>
> Thanks John for taking a look.
>
> > I'll try to look closer that the implementation details.
>
> We are in the process of sending v3 (hopefully by tonight) which is an
> improved version w.r.t error handling, journal credits and few other
> changes. Although nothing has changed w.r.t the design aspect.
>
> > But I do note
> > that you use blkdev_issue_zeroout() to pre-zero any unwritten range
> > which is being atomically written.
>
> Yes, that is how internally ext4_map_blocks() with
> EXT4_GET_BLOCKS_CREATE_ZERO will return us the allocated blocks. During
> block allocation, on mixed mapping range, we ensure that the entire range
> becomes a contiguous mapped extent before starting any data writes.
> That means calling ext4_map_blocks() in a loop with
> EXT4_GET_BLOCKS_CREATE_ZERO, so that it can zero out any unwritten
> extents in the requested region.
> I assume writing over a mixed mapping region is not a performance
> critical path.
>
> Do you forsee any problems with the approach (since you said "But I do note...")?
It's a little dumb to write zeroes just so you can atomicwrite a block.
However, ext4 lacks an out of place write handler, so I don't think
there's much else that can be done easily.
--D
> -ritesh
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2025-05-08 15:19 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-30 5:20 [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc Ritesh Harjani (IBM)
2025-04-30 5:20 ` [RFC v2 1/2] ext4: Add multi-fsblock atomic write support with bigalloc Ritesh Harjani (IBM)
2025-04-30 5:20 ` [RFC v2 2/2] ext4: Add support for EXT4_GET_BLOCKS_QUERY_LEAF_BLOCKS Ritesh Harjani (IBM)
2025-05-08 12:01 ` [RFC v2 0/2] ext4: Add multi-fsblock atomic write support using bigalloc John Garry
2025-05-08 14:35 ` Ritesh Harjani
2025-05-08 15:19 ` Darrick J. Wong
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox