[PATCH v2 2/8] fs: btrfs: fix a bug which no data get read if the length is not 0
Qu Wenruo
wqu at suse.com
Tue Jul 26 15:22:10 AEST 2022
[BUG]
When testing with unaligned read, if a specific length is passed in,
btrfs driver will read out nothing:
=> load host 0 $kernel_addr_r 5k_file 0x1000 0
0 bytes read in 0 ms
But if no length is passed in, it works fine, even if we pass a non-zero
length:
=> load host 0 $kernel_addr_r 5k_file 0 0x1000
1024 bytes read in 0 ms
[CAUSE]
In btrfs_read() if we have a larger size than our file, we will try to
truncate it using the file size.
However the real file size is not initialized if @len is not zero, thus
we always truncate our length to 0, and cause the problem.
[FIX]
Fix it by just always do the file size check.
In fact btrfs_size() always follow soft link, thus it will return the
real file size correctly.
Signed-off-by: Qu Wenruo <wqu at suse.com>
---
fs/btrfs/btrfs.c | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/fs/btrfs/btrfs.c b/fs/btrfs/btrfs.c
index 741c6e20f533..9145727058d4 100644
--- a/fs/btrfs/btrfs.c
+++ b/fs/btrfs/btrfs.c
@@ -246,16 +246,17 @@ int btrfs_read(const char *file, void *buf, loff_t offset, loff_t len,
return -EINVAL;
}
- if (!len) {
- ret = btrfs_size(file, &real_size);
- if (ret < 0) {
- error("Failed to get inode size: %s", file);
- return ret;
- }
- len = real_size;
+ ret = btrfs_size(file, &real_size);
+ if (ret < 0) {
+ error("Failed to get inode size: %s", file);
+ return ret;
}
- if (len > real_size - offset)
+ /*
+ * If the length is 0 (meaning read the whole file) or the range is
+ * beyond file size, truncate it to the end of the file.
+ */
+ if (!len || len > real_size - offset)
len = real_size - offset;
ret = btrfs_file_read(root, ino, offset, len, buf);
--
2.37.0
More information about the Linux-erofs
mailing list