From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH] btrfs: fallback to buffered read if the inode has data checksum
Date: Fri, 26 Sep 2025 08:20:13 +0930 [thread overview]
Message-ID: <aa09b68f159fb0de4864de151eeba9250f2e34fd.1758840406.git.wqu@suse.com> (raw)
Commit 968f19c5b1b7 ("btrfs: always fallback to buffered write if the
inode requires checksum") makes direct writes to fallback to buffered
ones if the inode has data checksum.
That commit is to avoid unreliable user space modifying the write buffer
during writeback, which can lead to data checksum mismatch.
(As the checksum is calculated, then buffer is modified, and the
modified data is submitted)
On the other hand, it's also possible that the user space program can
modify the read buffer at any time.
If the content is just read from the disk, then during checksum
verification the user space program modified the read buffer, it will
cause false alerts about csum mismatch.
Despite the possibility of false alerts, we should also keep the
behavior between direct read and direct write consistent.
If direct writes are already falling back to buffered for inodes with
checksum, the direct reads should also follow the same behavior.
So here, add the same data checksum checks for direct reads, so that
those reads will also fallback to buffered reads.
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
This will make test case btrfs/267 fail, as the fallback to buffered
read will happen in a different context (workqueue), screwing up the pid
based read balance.
Furthermore true direct reads will require NODATASUM, it no longer makes
any sense to test direct IO read repair.
If this is merged, I'll send out a patch to remove btrfs/267.
---
fs/btrfs/direct-io.c | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/fs/btrfs/direct-io.c b/fs/btrfs/direct-io.c
index 6018d8c3e101..6f35fed5fa3f 100644
--- a/fs/btrfs/direct-io.c
+++ b/fs/btrfs/direct-io.c
@@ -1046,6 +1046,13 @@ ssize_t btrfs_direct_read(struct kiocb *iocb, struct iov_iter *to)
if (check_direct_read(inode_to_fs_info(inode), to, iocb->ki_pos))
return 0;
+ /*
+ * To keep the behavior consistent with direct write, fall back to
+ * buffered IO if the inode has data checksum.
+ */
+ if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM))
+ return 0;
+
btrfs_inode_lock(BTRFS_I(inode), BTRFS_ILOCK_SHARED);
again:
/*
--
2.50.1
next reply other threads:[~2025-09-25 22:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-09-25 22:50 Qu Wenruo [this message]
2025-11-01 21:36 ` [PATCH] btrfs: fallback to buffered read if the inode has data checksum Qu Wenruo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=aa09b68f159fb0de4864de151eeba9250f2e34fd.1758840406.git.wqu@suse.com \
--to=wqu@suse.com \
--cc=linux-btrfs@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).