diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h index f38fec3167fb..6825ffeaa62d 100644 --- a/fs/ext4/ext4.h +++ b/fs/ext4/ext4.h @@ -4127,12 +4127,19 @@ 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. 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_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..124000e8dd82 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); /* @@ -689,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);