[RFC 4/8] ext4: Add statx and other atomic write helper routines

From: Ritesh Harjani (IBM)
Date: Sat Mar 02 2024 - 02:44:05 EST


This patch adds the statx (STATX_WRITE_ATOMIC) support in ext4_getattr()
to query for atomic_write_unit_min(awu_min), awu_max and other
attributes for atomic writes.
This adds a new runtime mount flag (EXT4_MF_ATOMIC_WRITE_FSAWU),
for querying whether ext4 supports atomic write using fsawu
(filesystem atomic write unit).

Co-developed-by: Ojaswin Mujoo <ojaswin@xxxxxxxxxxxxx>
Signed-off-by: Ojaswin Mujoo <ojaswin@xxxxxxxxxxxxx>
Signed-off-by: Ritesh Harjani (IBM) <ritesh.list@xxxxxxxxx>
---
fs/ext4/ext4.h | 53 ++++++++++++++++++++++++++++++++++++++++++++++++-
fs/ext4/inode.c | 16 +++++++++++++++
2 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 023571f8dd1b..1d2bce26e616 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -1817,7 +1817,8 @@ static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino)
*/
enum {
EXT4_MF_MNTDIR_SAMPLED,
- EXT4_MF_FC_INELIGIBLE /* Fast commit ineligible */
+ EXT4_MF_FC_INELIGIBLE, /* Fast commit ineligible */
+ EXT4_MF_ATOMIC_WRITE_FSAWU /* Atomic write via FSAWU */
};

static inline void ext4_set_mount_flag(struct super_block *sb, int bit)
@@ -3839,6 +3840,56 @@ static inline int ext4_buffer_uptodate(struct buffer_head *bh)
return buffer_uptodate(bh);
}

+#define ext4_can_atomic_write_fsawu(sb) \
+ ext4_test_mount_flag(sb, EXT4_MF_ATOMIC_WRITE_FSAWU)
+
+/**
+ * ext4_atomic_write_fsawu Returns EXT4 filesystem atomic write unit.
+ * @sb super_block
+ * This returns the filesystem min|max atomic write units.
+ * For !bigalloc it is filesystem blocksize (fsawu_min)
+ * For bigalloc it should be either blocksize or multiple of blocksize
+ * (fsawu_min)
+ */
+static inline void ext4_atomic_write_fsawu(struct super_block *sb,
+ unsigned int *fsawu_min,
+ unsigned int *fsawu_max)
+{
+ u8 blkbits = sb->s_blocksize_bits;
+ unsigned int blocksize = 1U << blkbits;
+ unsigned int clustersize = blocksize;
+ struct block_device *bdev = sb->s_bdev;
+ unsigned int awu_min =
+ queue_atomic_write_unit_min_bytes(bdev->bd_queue);
+ unsigned int awu_max =
+ queue_atomic_write_unit_max_bytes(bdev->bd_queue);
+
+ if (ext4_has_feature_bigalloc(sb))
+ clustersize = 1U << (EXT4_SB(sb)->s_cluster_bits + blkbits);
+
+ /* fs min|max should respect awu_[min|max] units */
+ if (unlikely(awu_min > clustersize || awu_max < blocksize))
+ goto not_supported;
+
+ /* in case of !bigalloc fsawu_[min|max] should be same as blocksize */
+ if (!ext4_has_feature_bigalloc(sb)) {
+ *fsawu_min = blocksize;
+ *fsawu_max = blocksize;
+ return;
+ }
+
+ /* bigalloc can support write in blocksize units. So advertize it */
+ *fsawu_min = max(blocksize, awu_min);
+ *fsawu_max = min(clustersize, awu_max);
+
+ /* This should never happen, but let's keep a WARN_ON_ONCE */
+ WARN_ON_ONCE(!IS_ALIGNED(clustersize, *fsawu_min));
+ return;
+not_supported:
+ *fsawu_min = 0;
+ *fsawu_max = 0;
+}
+
#endif /* __KERNEL__ */

#define EFSBADCRC EBADMSG /* Bad CRC detected */
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index 2ccf3b5e3a7c..ea009ca9085d 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -5536,6 +5536,22 @@ int ext4_getattr(struct mnt_idmap *idmap, const struct path *path,
}
}

+ if (request_mask & STATX_WRITE_ATOMIC) {
+ unsigned int fsawu_min = 0, fsawu_max = 0;
+
+ /*
+ * Get fsawu_[min|max] value which we can advertise to userspace
+ * in statx call, if we support atomic writes using
+ * EXT4_MF_ATOMIC_WRITE_FSAWU.
+ */
+ if (ext4_can_atomic_write_fsawu(inode->i_sb)) {
+ ext4_atomic_write_fsawu(inode->i_sb, &fsawu_min,
+ &fsawu_max);
+ }
+
+ generic_fill_statx_atomic_writes(stat, fsawu_min, fsawu_max);
+ }
+
flags = ei->i_flags & EXT4_FL_USER_VISIBLE;
if (flags & EXT4_APPEND_FL)
stat->attributes |= STATX_ATTR_APPEND;
--
2.43.0