2024-03-02 07:43:03

by Ritesh Harjani

[permalink] [raw]
Subject: [RFC 1/8] fs: Add FS_XFLAG_ATOMICWRITES flag

From: John Garry <[email protected]>

Add a flag indicating that a regular file is enabled for atomic writes.

Signed-off-by: John Garry <[email protected]>
Signed-off-by: Ritesh Harjani (IBM) <[email protected]>
---
include/uapi/linux/fs.h | 1 +
1 file changed, 1 insertion(+)

diff --git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index a0975ae81e64..b5b4e1db9576 100644
--- a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -140,6 +140,7 @@ struct fsxattr {
#define FS_XFLAG_FILESTREAM 0x00004000 /* use filestream allocator */
#define FS_XFLAG_DAX 0x00008000 /* use DAX for IO */
#define FS_XFLAG_COWEXTSIZE 0x00010000 /* CoW extent size allocator hint */
+#define FS_XFLAG_ATOMICWRITES 0x00020000 /* atomic writes enabled */
#define FS_XFLAG_HASATTR 0x80000000 /* no DIFLAG for this */

/* the read-only stuff doesn't really belong here, but any other place is
--
2.43.0



2024-03-02 07:44:29

by Ritesh Harjani

[permalink] [raw]
Subject: [RFC 5/8] ext4: Adds direct-io atomic writes checks

This patch adds ext4 specific checks for supporting atomic writes
using fsawu (filesystem atomic write unit). We can enable this support
with either -
1. bigalloc on a 4k pagesize system or
2. bs < ps system with -b <BS>
3. filesystems with LBS (large block size) support (future)

Let's use generic_atomic_write_valid() helper for alignment
restrictions checking.

Co-developed-by: Ojaswin Mujoo <[email protected]>
Signed-off-by: Ojaswin Mujoo <[email protected]>
Signed-off-by: Ritesh Harjani (IBM) <[email protected]>
---
fs/ext4/file.c | 34 +++++++++++++++++++++++++++++++---
1 file changed, 31 insertions(+), 3 deletions(-)

diff --git a/fs/ext4/file.c b/fs/ext4/file.c
index 54d6ff22585c..8e309a9a0bd6 100644
--- a/fs/ext4/file.c
+++ b/fs/ext4/file.c
@@ -400,6 +400,21 @@ static const struct iomap_dio_ops ext4_dio_write_ops = {
.end_io = ext4_dio_write_end_io,
};

+static bool ext4_dio_atomic_write_checks(struct kiocb *iocb,
+ struct iov_iter *from)
+{
+ struct super_block *sb = file_inode(iocb->ki_filp)->i_sb;
+ loff_t pos = iocb->ki_pos;
+ unsigned int fsawu_min, fsawu_max;
+
+ if (!ext4_can_atomic_write_fsawu(sb))
+ return false;
+
+ ext4_atomic_write_fsawu(sb, &fsawu_min, &fsawu_max);
+
+ return generic_atomic_write_valid(pos, from, fsawu_min, fsawu_max);
+}
+
/*
* The intention here is to start with shared lock acquired then see if any
* condition requires an exclusive inode lock. If yes, then we restart the
@@ -427,13 +442,19 @@ static ssize_t ext4_dio_write_checks(struct kiocb *iocb, struct iov_iter *from,
loff_t offset;
size_t count;
ssize_t ret;
- bool overwrite, unaligned_io;
+ bool overwrite, unaligned_io, atomic_write;

restart:
ret = ext4_generic_write_checks(iocb, from);
if (ret <= 0)
goto out;

+ atomic_write = iocb->ki_flags & IOCB_ATOMIC;
+ if (atomic_write && !ext4_dio_atomic_write_checks(iocb, from)) {
+ ret = -EINVAL;
+ goto out;
+ }
+
offset = iocb->ki_pos;
count = ret;

@@ -576,8 +597,15 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
iomap_ops = &ext4_iomap_overwrite_ops;
ret = iomap_dio_rw(iocb, from, iomap_ops, &ext4_dio_write_ops,
dio_flags, NULL, 0);
- if (ret == -ENOTBLK)
- ret = 0;
+
+ /* Fallback to buffered-io for non-atomic DIO */
+ if (ret == -ENOTBLK) {
+ if (iocb->ki_flags & IOCB_ATOMIC)
+ ret = -EIO;
+ else
+ ret = 0;
+ }
+
if (extend) {
/*
* We always perform extending DIO write synchronously so by
--
2.43.0