u-boot/fs/btrfs
Dominique Martinet b1d3013d02 btrfs: fix offset when reading compressed extents
btrfs_read_extent_reg correctly computed the extent offset in the
BTRFS_COMPRESS_NONE case, but did not account for the 'offset - key.offset'
part correctly in the compressed case, making the function read
incorrect data.

In the case I examined, the last 4k of a file was corrupted and
contained data from a few blocks prior, e.g. reading a 10k file with a
single extent:
btrfs_file_read()
 -> btrfs_read_extent_reg
    (aligned part loop, until 8k)
 -> read_and_truncate_page
   -> btrfs_read_extent_reg
      (re-reads the last extent from 8k to the end,
      incorrectly reading the first 2k of data)

This can be reproduced as follow:
$ truncate -s 200M btr
$ mount btr -o compress /mnt
$ pat() { dd if=/dev/zero bs=1M count=$1 iflag=count_bytes status=none | tr '\0' "\\$2"; }
$ { pat 4K 1; pat 4K 2; pat 2K 3; }  > /mnt/file
$ sync
$ filefrag -v /mnt/file
File size of /mnt/file is 10240 (3 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..       2:       3328..      3330:      3:             last,encoded,eof
$ umount /mnt

Then in u-boot:
=> load scsi 0 2000000 file
10240 bytes read in 3 ms (3.3 MiB/s)
=> md 2001ff0
02001ff0: 02020202 02020202 02020202 02020202  ................
02002000: 01010101 01010101 01010101 01010101  ................
02002010: 01010101 01010101 01010101 01010101  ................

(02002000 onwards should contain '03' pattern but went back to 01,
start of the extent)

After patch, data is read properly:
=> md 2001ff0
02001ff0: 02020202 02020202 02020202 02020202  ................
02002000: 03030303 03030303 03030303 03030303  ................
02002010: 03030303 03030303 03030303 03030303  ................

Note that the code previously (before commit e3427184f3 ("fs: btrfs:
Implement btrfs_file_read()")) did not split that read in two, so
this is a regression even if the previous code might not have been
handling offsets correctly either (something that booted now fails to
boot)

Fixes: a26a6bedaf ("fs: btrfs: Introduce btrfs_read_extent_inline() and btrfs_read_extent_reg()")
Signed-off-by: Dominique Martinet <dominique.martinet@atmark-techno.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
2023-05-08 09:23:53 -04:00
..
common fs: btrfs: Crossport rbtree-utils from btrfs-progs 2020-09-07 20:57:27 -04:00
crypto fs/btrfs: add dependency on BLAKE2 hash 2022-01-18 08:31:02 -05:00
kernel-shared WS cleanup: remove excessive empty lines 2021-09-30 08:08:56 -04:00
btrfs.c treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
btrfs.h treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
compat.h fs: btrfs: Introduce function to resolve path in one subvolume 2020-09-07 21:00:36 -04:00
compression.c treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
conv-funcs.h treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
ctree.c treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
ctree.h treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
dev.c treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
dir-item.c treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
disk-io.c fs/btrfs: handle data extents, which crosss stripe boundaries, correctly 2023-01-11 15:02:24 -05:00
disk-io.h fs: btrfs: Cleanup the old implementation 2020-09-07 21:00:36 -04:00
extent-cache.c fs: btrfs: Crossport extent-cache.[ch] from btrfs-progs 2020-09-07 20:57:27 -04:00
extent-cache.h fs: btrfs: Crossport extent-cache.[ch] from btrfs-progs 2020-09-07 20:57:27 -04:00
extent-io.c treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
extent-io.h fs: btrfs: Crossport extent-io.[ch] from btrfs-progs 2020-09-07 20:57:27 -04:00
inode.c btrfs: fix offset when reading compressed extents 2023-05-08 09:23:53 -04:00
Kconfig fs/btrfs: add dependency on BLAKE2 hash 2022-01-18 08:31:02 -05:00
Makefile treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
root-tree.c fs: btrfs: Crossport open_ctree_fs_info() from btrfs-progs 2020-09-07 20:57:27 -04:00
subvolume.c treewide: Fix Marek's name and change my e-mail address 2022-07-21 10:14:04 +02:00
volumes.c fs: btrfs: limit the mapped length to the original length 2023-02-23 13:29:19 -05:00
volumes.h fs: btrfs: Crossport btrfs_read_sys_array() and btrfs_read_chunk_tree() 2020-09-07 20:57:27 -04:00