Skip to contentSkip to conversation

ext2: fix ignored return value of generic_write_sync()

PATCH In review Danila Chernetsov 2 participants 3 messages May 30 – Jun 1 1 message not in the archive ext2 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ext2: fix ignored return value of generic_write_sync()

MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fix ext2_dio_write_iter() to propagate the error returned by generic_write_sync() instead of silently discarding it, which could cause write(2) to return success to userspace on O_SYNC/O_DSYNC files even when the sync failed.

The correct pattern, already used in ext2_dax_write_iter() in the same file and in ext4, xfs, f2fs among others, is:

    if (ret > 0)
        ret = generic_write_sync(iocb, ret);

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes fb5de4358e1a ("ext2: Move direct-io to use iomap")
---
 fs/ext2/file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext2/file.c +1 -1
index d9b1eb34694a..855a62e96c38 100644
@@ -272,7 +272,7 @@ static ssize_t ext2_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
 						 pos >> PAGE_SHIFT,
 						 endbyte >> PAGE_SHIFT);
 		if (ret > 0)
-			generic_write_sync(iocb, ret);
+			ret = generic_write_sync(iocb, ret);
 	}
 
 out_unlock:
Signature · 1 line
2.25.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#2 lore raw
Quoted 62 lines · On 30/05/2026 15:56, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] If the direct I/O phase partially succeeds but the buffered fallback fails, the successful partial write byte count is discarded and an error is returned.
- [High] For O_DIRECT writes without O_SYNC, I/O errors during the buffered fallback writeback phase are silently ignored, causing the write to report success even if data did not reach the disk. --

commit 219eb44f6ba1253d0149b02cde5191ac13546983
Author: Danila Chernetsov <[email protected]>

ext2: fix ignored return value of generic_write_sync()

Fix ext2_dio_write_iter() to propagate the error returned by generic_write_sync() instead of silently discarding it, which could cause write(2) to return success to userspace on O_SYNC/O_DSYNC files even when the sync failed.

fs/ext2/file.c static ssize_t ext2_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)

Quoted 5 lines
fs/ext2/file.c +0 -0
index d9b1eb34694a0..855a62e96c380 100644
@@ -272,7 +272,7 @@ static ssize_t ext2_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)

[Severity: High]
This isn't a bug introduced by this patch, but if the direct I/O phase partially succeeds while the buffered fallback fails, is the successful partial write byte count overwritten?

In ext2_dio_write_iter(), the error handling for generic_perform_write() looks like this:

		status = generic_perform_write(iocb, from);
		if (unlikely(status < 0)) {
			ret = status;
			goto out_unlock;
		}

If generic_perform_write() returns a negative error code (such as -ENOSPC) because no additional bytes can be written, ret is updated with this error. Could this hide the fact that data was successfully written and the file offset advanced, violating POSIX write semantics and causing userspace to corrupt the file by duplicating data on retry?

[Severity: High]
This is a pre-existing issue, but are I/O errors during the buffered fallback writeback phase silently ignored for O_DIRECT writes without O_SYNC?

Looking earlier in ext2_dio_write_iter():

		ret2 = filemap_write_and_wait_range(inode->i_mapping, pos,
						    endbyte);
		if (!ret2)
			invalidate_mapping_pages(inode->i_mapping,
						 pos >> PAGE_SHIFT,
						 endbyte >> PAGE_SHIFT);

If filemap_write_and_wait_range() fails and returns a negative error code in ret2, the error only prevents cache invalidation and is never propagated to the final return value. Will this cause the write to report success to userspace even if the data failed to reach the disk?

Quoted 9 lines

fs/ext2/file.c static ssize_t ext2_dio_write_iter(struct kiocb *iocb, struct iov_iter *from) · line 278

Quoted 8 lines
fs/ext2/file.c +1 -1
  						 pos >> PAGE_SHIFT,
  						 endbyte >> PAGE_SHIFT);
  		if (ret > 0)
-			generic_write_sync(iocb, ret);
+			ret = generic_write_sync(iocb, ret);
  	}
 
  out_unlock:

Thanks for the review. Both reported issues are pre-existing in the codebase and are completely unrelated to my changes. This patch does not introduce, touch, or modify this logic. I request the maintainers to review my patch as is.

Best regards,
Danila Chernetsov

#3 lore raw
In reply to Danila Chernetsov
Quoted 11 lines · Danila Chernetsov

MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Fix ext2_dio_write_iter() to propagate the error returned by generic_write_sync() instead of silently discarding it, which could cause write(2) to return success to userspace on O_SYNC/O_DSYNC files even when the sync failed.

The correct pattern, already used in ext2_dax_write_iter() in the same file and in ext4, xfs, f2fs among others, is:

Quoted 7 lines
    if (ret > 0)
        ret = generic_write_sync(iocb, ret);

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Fixes fb5de4358e1a ("ext2: Move direct-io to use iomap")

Thanks for the patch. I think it makes sense although I also think we should also reflect the return value of filemap_write_and_wait_range() (when it fails, return the error from ext2_dio_write_iter()) to be consistent with handling errors during flushing of direct IO fallback. I'll update the patch on commit.

Honza

Quoted 20 lines
---
 fs/ext2/file.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/ext2/file.c +1 -1
index d9b1eb34694a..855a62e96c38 100644
@@ -272,7 +272,7 @@ static ssize_t ext2_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
 						 pos >> PAGE_SHIFT,
 						 endbyte >> PAGE_SHIFT);
 		if (ret > 0)
-			generic_write_sync(iocb, ret);
+			ret = generic_write_sync(iocb, ret);
 	}
 
 out_unlock:
Signature · 1 line
2.25.1
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR