Skip to contentSkip to conversation

ext4: Fix data corruption caused by overlapping unaligned and aligned IO

PATCH In review Lukas Czerner 2 participants 2 messages May 6, 2019 – May 11, 2019 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext4: Fix data corruption caused by overlapping unaligned and aligned IO

Unaligned AIO must be serialized because the zeroing of partial blocks of unaligned AIO can result in data corruption in case it's overlapping another in flight IO.

Currently we wait for all unwritten extents before we submit unaligned AIO which protects data in case of unaligned AIO is following overlapping IO. However if a unaligned AIO is followed by overlapping aligned AIO we can still end up corrupting data.

To fix this, we must make sure that the unaligned AIO is the only IO in flight by waiting for unwritten extents conversion not just before the IO submission, but right after it as well.

This problem can be reproduced by xfstest generic/538

---
 fs/ext4/file.c | 7 +++++++
 1 file changed, 7 insertions(+)
fs/ext4/file.c +7 -0
index 98ec11f69cd4..2c5baa5e8291 100644
@@ -264,6 +264,13 @@ ext4_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	}
 
 	ret = __generic_file_write_iter(iocb, from);
+	/*
+	 * Unaligned direct AIO must be the only IO in flight. Otherwise
+	 * overlapping aligned IO after unaligned might result in data
+	 * corruption.
+	 */
+	if (ret == -EIOCBQUEUED && unaligned_aio)
+		ext4_unwritten_wait(inode);
 	inode_unlock(inode);
 
 	if (ret > 0)
Signature · 1 line
2.20.1
maintainer
#1 lore raw
Quoted 9 lines · Lukas Czerner

Unaligned AIO must be serialized because the zeroing of partial blocks of unaligned AIO can result in data corruption in case it's overlapping another in flight IO.

Currently we wait for all unwritten extents before we submit unaligned AIO which protects data in case of unaligned AIO is following overlapping IO. However if a unaligned AIO is followed by overlapping aligned AIO we can still end up corrupting data.

Quoted 7 lines

To fix this, we must make sure that the unaligned AIO is the only IO in flight by waiting for unwritten extents conversion not just before the IO submission, but right after it as well.

This problem can be reproduced by xfstest generic/538

Many thanks, applied.

- Ted