From ca0fdb5fcdd3eb699e2d924c57630f67a1487964 Mon Sep 17 00:00:00 2001 From: Tal Zussman Date: Mon, 12 Oct 2026 13:17:40 -0400 Subject: [PATCH 1/3] ext4: reject atomic writes on inodes that can't do direct I/O ext4_dio_write_iter() falls back to buffered I/O when the inode can't do direct I/O at all, i.e. journaled data, inline data or verity. That fallback runs before any atomic write handling, and ext4_buffered_write_iter() doesn't look at IOCB_ATOMIC, so an atomic write to such an inode is silently done as a plain buffered write with no torn-write protection. ext4_inode_can_atomic_write() makes it worse for journaled data by advertising atomic writes for those inodes: statx() reports atomic write limits and the file gets FMODE_CAN_ATOMIC_WRITE, so the write passes the VFS checks and reaches the fallback. This is reachable with data=journal, or with chattr +j on a nodelalloc mount. Inline data inodes don't have the extents flag and verity files can't be opened for writing, so those are refused before ext4 sees the write, but the fallback would still accept them. Reject IOCB_ATOMIC in ext4_buffered_write_iter(), next to the existing IOCB_NOWAIT rejection, so neither fallback into it can complete an atomic write non-atomically. Also require ext4_dio_alignment() to be nonzero in ext4_inode_can_atomic_write(), so statx() stops advertising atomic writes for these inodes and VFS refuses RWF_ATOMIC early. The runtime check is still needed, as the journal data flag can be changed on an open file. Fixes: b7987a7d69a4 ("ext4: Support setting FMODE_CAN_ATOMIC_WRITE") Reported-by: Sashiko Link: https://sashiko.dev/#/patchset/20260909-ext4-atomic-no-fallback-v1-1-5a6b9539c002%40columbia.edu Signed-off-by: Tal Zussman Reviewed-by: Jan Kara --- fs/ext4/ext4.h | 8 +++++++- fs/ext4/file.c | 8 ++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index f38fec3167fb..d761630bd869 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -4127,12 +4127,18 @@ static inline int ext4_buffer_uptodate(struct buffer_head *bh) return buffer_uptodate(bh); } +/* + * Atomic writes are only supported through direct I/O, so an inode that + * can't do direct I/O at all (journaled data, inline data, verity) can't do + * atomic writes either. + */ static inline bool ext4_inode_can_atomic_write(struct inode *inode) { return S_ISREG(inode->i_mode) && ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) && - EXT4_SB(inode->i_sb)->s_awu_min > 0; + EXT4_SB(inode->i_sb)->s_awu_min > 0 && + ext4_dio_alignment(inode) != 0; } extern int ext4_block_write_begin(handle_t *handle, struct folio *folio, diff --git a/fs/ext4/file.c b/fs/ext4/file.c index 50d3c92709c8..cb56d01ddd68 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -354,6 +354,14 @@ static ssize_t ext4_buffered_write_iter(struct kiocb *iocb, if (iocb->ki_flags & IOCB_NOWAIT) return -EOPNOTSUPP; + /* + * Buffered I/O provides no torn-write protection. The direct I/O path + * falls back here for inodes that can't do direct I/O at all, so an + * atomic write must be rejected rather than silently done non-atomically. + */ + if (iocb->ki_flags & IOCB_ATOMIC) + return -EOPNOTSUPP; + inode_lock(inode); /* From cf6f34611a861d334c103593f71f06057294ac89 Mon Sep 17 00:00:00 2001 From: Tal Zussman Date: Mon, 12 Oct 2026 13:17:41 -0400 Subject: [PATCH 2/3] ext4: never fall back to buffered I/O for atomic direct writes ext4_dio_write_iter() finishes a short direct write through the page cache. ext4_buffered_write_iter() now rejects IOCB_ATOMIC, so a short atomic write would warn and then fail with -EOPNOTSUPP, as if atomic writes were unsupported on the inode. This came out of a block device fix [1], where an IOCB_ATOMIC direct write could complete short when only part of the buffer could be pinned, with the rest going through the buffered fallback. Christoph Hellwig noted that ext4 has the same fallback and only warns in it [2], and John Garry agreed it should reject the write instead [3]. In contrast to the block device, nothing reaches the branch on ext4 today: iomap requires the mapping to cover the whole atomic write, a buffer that can't be pinned in full fails in iomap before the bio is submitted, and a failed page cache invalidation returns -EAGAIN rather than reaching the buffered fallback. A short atomic write is therefore a bug, and neither a short count nor -EOPNOTSUPP is a valid result for one. Fail the write with -EIO before the fallback, keeping the WARN() so any future bug is visible. Link: https://lore.kernel.org/linux-block/20260828-blkdev-fixes-v2-0-32f3f40cebed@columbia.edu/ [1] Link: https://lore.kernel.org/linux-block/20260907071217.GD934@lst.de/ [2] Link: https://lore.kernel.org/linux-block/a7752d1e-8ec1-44d1-a266-c287c315e8ab@oracle.com/ [3] Signed-off-by: Tal Zussman Reviewed-by: Jan Kara --- fs/ext4/file.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/fs/ext4/file.c b/fs/ext4/file.c index cb56d01ddd68..124000e8dd82 100644 --- a/fs/ext4/file.c +++ b/fs/ext4/file.c @@ -697,11 +697,12 @@ static ssize_t ext4_dio_write_iter(struct kiocb *iocb, struct iov_iter *from) loff_t endbyte; /* - * There is no support for atomic writes on buffered-io yet, - * we should never fallback to buffered-io for DIO atomic - * writes. + * Buffered I/O provides no torn-write protection, so never + * finish an atomic write through the page cache. iomap must + * not return a short atomic write, so treat one as a bug. */ - WARN_ON_ONCE(iocb->ki_flags & IOCB_ATOMIC); + if (WARN_ON_ONCE(iocb->ki_flags & IOCB_ATOMIC)) + return -EIO; offset = iocb->ki_pos; err = ext4_buffered_write_iter(iocb, from); From c253f6f550d8a506c17c003d30c42d9fb04f2563 Mon Sep 17 00:00:00 2001 From: Tal Zussman Date: Mon, 12 Oct 2026 13:17:42 -0400 Subject: [PATCH 3/3] ext4: don't advertise atomic writes for DAX inodes ext4_file_write_iter() hands DAX inodes to ext4_dax_write_iter() before any of the atomic write handling, and the DAX path neither checks IOCB_ATOMIC nor provides torn-write protection. ext4_inode_can_atomic_write() doesn't exclude DAX inodes, so on a device that supported both DAX and atomic writes, statx() would advertise atomic writes for such an inode and the VFS would let an RWF_ATOMIC write through to a plain DAX write. No DAX-capable block device advertises atomic write limits today, so this isn't reachable. When XFS gained its corresponding check, Ritesh Harjani suggested excluding DAX inodes in ext4 anyway [1], so that what is advertised matches what the write path can do. XFS does the same in xfs_inode_can_hw_atomic_write() and xfs_inode_can_sw_atomic_write(). Link: https://lore.kernel.org/linux-ext4/87a54e4cdn.fsf@gmail.com/ [1] Signed-off-by: Tal Zussman Reviewed-by: Jan Kara --- fs/ext4/ext4.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index d761630bd869..6825ffeaa62d 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -4130,12 +4130,13 @@ static inline int ext4_buffer_uptodate(struct buffer_head *bh) /* * Atomic writes are only supported through direct I/O, so an inode that * can't do direct I/O at all (journaled data, inline data, verity) can't do - * atomic writes either. + * atomic writes either. DAX inodes bypass the direct I/O path entirely. */ static inline bool ext4_inode_can_atomic_write(struct inode *inode) { return S_ISREG(inode->i_mode) && + !IS_DAX(inode) && ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) && EXT4_SB(inode->i_sb)->s_awu_min > 0 && ext4_dio_alignment(inode) != 0;