Skip to contentSkip to conversation

ext4: Fix data corruption with direct IO read in dioread_nolock mode

PATCH In review Jan Kara 3 participants 6 messages Apr 8, 2013 – Apr 12, 2013 ext4 file system

lore
Share & tools
lore

Jan Kara

author reviewer
#0 lore raw

PATCH 1/1 ext4: Fix data corruption with direct IO read in dioread_nolock mode

When ext4 is mounted in dioread_nolock mode, it calls __blockdev_direct_IO() without DIO_LOCKING flag which also means mapping is not synced before direct IO starts. As a result consistency between buffered writes and following direct IO reads is broken. More importantly, if the file is truncated, and extented back, zeroing of the tail page is not visible to the following direct IO read which will return stale data.

Fix the problem by flushing dirty pages before issuing direct IO read.

---
 fs/ext4/indirect.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/ext4/indirect.c +4 -0
index b505a14..c18bf1c 100644
@@ -809,6 +809,10 @@ ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
 
 retry:
 	if (rw == READ && ext4_should_dioread_nolock(inode)) {
+		ret = filemap_write_and_wait_range(inode->i_mapping, offset,
+						   offset + count - 1);
+		if (ret)
+			goto out;
 		if (unlikely(atomic_read(&EXT4_I(inode)->i_unwritten))) {
 			mutex_lock(&inode->i_mutex);
 			ext4_flush_unwritten_io(inode);
Signature · 1 line
1.7.1
author reviewer
#1 lore raw
Quoted 11 lines · Jan Kara

When ext4 is mounted in dioread_nolock mode, it calls __blockdev_direct_IO() without DIO_LOCKING flag which also means mapping is not synced before direct IO starts. As a result consistency between buffered writes and following direct IO reads is broken. More importantly, if the file is truncated, and extented back, zeroing of the tail page is not visible to the following direct IO read which will return stale data.

Fix the problem by flushing dirty pages before issuing direct IO read.

Ted, didn't you miss this patch?

Honza

Quoted 26 lines
---
 fs/ext4/indirect.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/ext4/indirect.c +4 -0
index b505a14..c18bf1c 100644
@@ -809,6 +809,10 @@ ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
 
 retry:
 	if (rw == READ && ext4_should_dioread_nolock(inode)) {
+		ret = filemap_write_and_wait_range(inode->i_mapping, offset,
+						   offset + count - 1);
+		if (ret)
+			goto out;
 		if (unlikely(atomic_read(&EXT4_I(inode)->i_unwritten))) {
 			mutex_lock(&inode->i_mutex);
 			ext4_flush_unwritten_io(inode);
Signature · 6 lines
1.7.1

--
To unsubscribe from this list: send the line "unsubscribe linux-ext4" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#2 lore raw
Quoted 1 line · Jan Kara

Ted, didn't you miss this patch?

I didn't miss it, I was just dealing with the older patches first, and waiting to see if anyone would comment on it before I applied it into the tree. (That way I'm not the only person reviewing all of the patches. :-)

But thanks, I've added it to the ext4 patch queue.

- Ted

#3 lore raw
In reply to Jan Kara
Quoted 20 lines · Jan Kara

When ext4 is mounted in dioread_nolock mode, it calls __blockdev_direct_IO() without DIO_LOCKING flag which also means mapping is not synced before direct IO starts. As a result consistency between buffered writes and following direct IO reads is broken. More importantly, if the file is truncated, and extented back, zeroing of the tail page is not visible to the following direct IO read which will return stale data.

Fix the problem by flushing dirty pages before issuing direct IO read.

---
 fs/ext4/indirect.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/ext4/indirect.c +0 -0
index b505a14..c18bf1c 100644

fs/ext4/indirect.c ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, · line 818

Quoted 10 lines
fs/ext4/indirect.c +4 -0
@@ -809,6 +809,10 @@ ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
 
 retry:
 	if (rw == READ && ext4_should_dioread_nolock(inode)) {
+		ret = filemap_write_and_wait_range(inode->i_mapping, offset,
+						   offset + count - 1);
+		if (ret)
+			goto out;
 		if (unlikely(atomic_read(&EXT4_I(inode)->i_unwritten))) {
 			mutex_lock(&inode->i_mutex);
 			ext4_flush_unwritten_io(inode);

Hi Jan,

Sorry for the late reply. I have a question about this patch. In your patch, we flush dirty pages befoer issuing a dio read. But we have flushed dirty pages in generic_file_aio_read(). So is it really necessary to flush dirty pages again?

Regards,
- Zheng

author reviewer
#4 lore raw
Quoted 34 lines · Zheng Liu
Quoted 20 lines · Jan Kara

When ext4 is mounted in dioread_nolock mode, it calls __blockdev_direct_IO() without DIO_LOCKING flag which also means mapping is not synced before direct IO starts. As a result consistency between buffered writes and following direct IO reads is broken. More importantly, if the file is truncated, and extented back, zeroing of the tail page is not visible to the following direct IO read which will return stale data.

Fix the problem by flushing dirty pages before issuing direct IO read.

---
 fs/ext4/indirect.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/ext4/indirect.c +0 -0
index b505a14..c18bf1c 100644

fs/ext4/indirect.c ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb, · line 818

Quoted 10 lines
fs/ext4/indirect.c +4 -0
@@ -809,6 +809,10 @@ ssize_t ext4_ind_direct_IO(int rw, struct kiocb *iocb,
 
 retry:
 	if (rw == READ && ext4_should_dioread_nolock(inode)) {
+		ret = filemap_write_and_wait_range(inode->i_mapping, offset,
+						   offset + count - 1);
+		if (ret)
+			goto out;
 		if (unlikely(atomic_read(&EXT4_I(inode)->i_unwritten))) {
 			mutex_lock(&inode->i_mutex);
 			ext4_flush_unwritten_io(inode);

Hi Jan,

Quoted 4 lines

Sorry for the late reply. I have a question about this patch. In your patch, we flush dirty pages befoer issuing a dio read. But we have flushed dirty pages in generic_file_aio_read(). So is it really necessary to flush dirty pages again?

Doh, you are right. I didn't notice that. Thanks for correcting me. I was
seeing a failure in xfstest 091 (fsx using direct IO) with dioread_nolock and I thought the missing flush was the culprit (as fsx saw non-zeros in the tail of the page that was truncated). After adding the flush I didn't see it anymore. But now I'm not able to trigger the failure again regardless whether the patch is applied or not. So Ted, please drop the patch and I'll watch out if I ever see the failure again.

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer
#5 lore raw
Quoted 7 lines · Jan Kara

Doh, you are right. I didn't notice that. Thanks for correcting me. I was
seeing a failure in xfstest 091 (fsx using direct IO) with dioread_nolock and I thought the missing flush was the culprit (as fsx saw non-zeros in the tail of the page that was truncated). After adding the flush I didn't see it anymore. But now I'm not able to trigger the failure again regardless whether the patch is applied or not. So Ted, please drop the patch and I'll watch out if I ever see the failure again.

Dropped.

- Ted