ext2: drain in-flight DIO before buffered write fallback
Subsystems (3)
A message that is not in the archive (sent off-list or not yet received): [email protected]
PATCH 1/3 ext2: drain in-flight DIO before buffered write fallback
An asynchronous direct write can remain in flight after the inode lock is released. If another direct write falls back to buffered I/O while the first write is still pending, generic_perform_write() can dirty pages before the first write completes its post-I/O page cache invalidation. The invalidation then finds dirty pages, reports a page cache invalidation failure, and records -EIO in the mapping error sequence. A later fsync() therefore returns -EIO.
Commit 15cdefd0c0522f9d5e12d947fa04f4c11649b699 ("ext4: drain in-flight DIO before buffered write fallback") fixed the same race in ext4. Ext2 has an equivalent fallback after iomap_dio_rw() returns -ENOTBLK or a short write, but does not drain other in-flight DIO before dirtying the page cache.
Wait for in-flight DIO before calling generic_perform_write() in the fallback path.
A reproducer using concurrent AIO direct writes and buffered fallback triggered the following warning and made a subsequent fsync() return -EIO:
Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!
--- fs/ext2/file.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/ext2/file.c +7 -0
@@ -135,6 +135,13 @@ static ssize_t ext2_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)int ret2; iocb->ki_flags &= ~IOCB_DIRECT ; + +/* +* Prevent concurrent direct I/O and buffered I/O to the same file +* range. Wait for in-flight DIO to finish before dirtying pages. +*/ +inode_dio_wait (inode); + pos = iocb->ki_pos; status =generic_perform_write (iocb, from);if (unlikely (status <0 )) {
Signature · 1 line
2.34.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 21 lines
An asynchronous direct write can remain in flight after the inode lock is released. If another direct write falls back to buffered I/O while the first write is still pending,
generic_perform_write()can dirty pages before the first write completes its post-I/O page cache invalidation. The invalidation then finds dirty pages, reports a page cache invalidation failure, and records-EIOin the mapping error sequence. A laterfsync()therefore returns-EIO.Commit
15cdefd0c0522f9d5e12d947fa04f4c11649b699("ext4: drain in-flight DIO before buffered write fallback") fixed the same race in ext4. Ext2 has an equivalent fallback afteriomap_dio_rw()returns-ENOTBLKor a short write, but does not drain other in-flight DIO before dirtying the page cache.Wait for in-flight DIO before calling
generic_perform_write()in the fallback path.A reproducer using concurrent AIO direct writes and buffered fallback triggered the following warning and made a subsequent
fsync()return-EIO:
Quoted 6 lines
Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!
Fixes fb5de4358e1a ("ext2: Move direct-io to use iomap")Signed-off-by Jiale Yao <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Quoted 21 lines · Jiale Yao
An asynchronous direct write can remain in flight after the inode lock is released. If another direct write falls back to buffered I/O while the first write is still pending,
generic_perform_write()can dirty pages before the first write completes its post-I/O page cache invalidation. The invalidation then finds dirty pages, reports a page cache invalidation failure, and records-EIOin the mapping error sequence. A laterfsync()therefore returns-EIO.Commit
15cdefd0c0522f9d5e12d947fa04f4c11649b699("ext4: drain in-flight DIO before buffered write fallback") fixed the same race in ext4. Ext2 has an equivalent fallback afteriomap_dio_rw()returns-ENOTBLKor a short write, but does not drain other in-flight DIO before dirtying the page cache.Wait for in-flight DIO before calling
generic_perform_write()in the fallback path.A reproducer using concurrent AIO direct writes and buffered fallback triggered the following warning and made a subsequent
fsync()return-EIO:
Quoted 6 lines
Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!
Fixes fb5de4358e1a ("ext2: Move direct-io to use iomap")Signed-off-by Jiale Yao <[email protected]>
Thanks! Added to my tree.
Honza
Quoted 25 lines
--- fs/ext2/file.c | 7 +++++++ 1 file changed, 7 insertions(+)fs/ext2/file.c +7
-0@@ -135,6 +135,13 @@ static ssize_t ext2_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)int ret2; iocb->ki_flags &= ~IOCB_DIRECT ; + +/* +* Prevent concurrent direct I/O and buffered I/O to the same file +* range. Wait for in-flight DIO to finish before dirtying pages. +*/ +inode_dio_wait (inode); + pos = iocb->ki_pos; status =generic_perform_write (iocb, from);if (unlikely (status <0 )) {Signature · 1 line
2.34.1
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
PATCH 2/3 ntfs: drain in-flight DIO before buffered write fallback
In reply to a message not in the archiveAn asynchronous direct write can remain in flight after the inode lock is released. If another direct write falls back to buffered I/O while the first write is still pending, iomap_file_buffered_write() can dirty pages before the first write completes its post-I/O page cache invalidation. The invalidation then finds dirty pages, reports a page cache invalidation failure, and records -EIO in the mapping error sequence. A later fsync() therefore returns -EIO.
Commit 15cdefd0c0522f9d5e12d947fa04f4c11649b699 ("ext4: drain in-flight DIO before buffered write fallback") fixed the same race in ext4. NTFS has an equivalent fallback after iomap_dio_rw() returns -ENOTBLK or a short write, but does not drain other in-flight DIO before dirtying the page cache.
Wait for in-flight DIO before calling iomap_file_buffered_write() in the fallback path.
A reproducer using concurrent AIO direct writes and buffered fallback triggered the following warning and made a subsequent fsync() return -EIO:
Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!
--- fs/ntfs/file.c | 7 +++++++ 1 file changed, 7 insertions(+)
fs/ntfs/file.c +7 -0
@@ -527,6 +527,13 @@ static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from) offset = iocb->ki_pos; iocb->ki_flags &= ~IOCB_DIRECT ; + +/* +* Prevent concurrent direct I/O and buffered I/O to the same file +* range. Wait for in-flight DIO to finish before dirtying pages. +*/ +inode_dio_wait (file_inode (iocb->ki_filp)); + written =iomap_file_buffered_write (iocb, from, &ntfs_write_iomap_ops, &ntfs_iomap_folio_ops,NULL );
Signature · 1 line
2.34.1
PATCH 3/3 exfat: drain in-flight DIO before buffered write fallback
In reply to a message not in the archiveAn asynchronous direct write can remain in flight after the inode lock is released. If another direct write falls back to buffered I/O while the first write is still pending, iomap_file_buffered_write() can dirty pages before the first write completes its post-I/O page cache invalidation. The invalidation then finds dirty pages, reports a page cache invalidation failure, and records -EIO in the mapping error sequence. A later fsync() therefore returns -EIO.
Commit 15cdefd0c0522f9d5e12d947fa04f4c11649b699 ("ext4: drain in-flight DIO before buffered write fallback") fixed the same race in ext4. ExFAT has an equivalent fallback after iomap_dio_rw() returns -ENOTBLK or a short write, but does not drain other in-flight DIO before dirtying the page cache.
Wait for in-flight DIO before calling iomap_file_buffered_write() in the fallback path.
A reproducer using concurrent AIO direct writes and buffered fallback triggered the following warning and made a subsequent fsync() return -EIO:
Page cache invalidation failure on direct I/O. Possible data corruption due to collision with buffered I/O!
--- fs/exfat/file.c | 6 ++++++ 1 file changed, 6 insertions(+)
fs/exfat/file.c +6 -0
@@ -807,6 +807,12 @@ static ssize_t exfat_fallback_buffered_write(struct kiocb *iocb, iocb->ki_flags &= ~IOCB_DIRECT ; +/* +* Prevent concurrent direct I/O and buffered I/O to the same file +* range. Wait for in-flight DIO to finish before dirtying pages. +*/ +inode_dio_wait (file_inode (iocb->ki_filp)); + written =iomap_file_buffered_write (iocb, from, &exfat_write_iomap_ops,NULL ,NULL );if (written <0 )
Signature · 1 line
2.34.1