u-boot/fs/btrfs
Qu Wenruo 7c075433fe fs/btrfs: fix a bug that U-boot fs btrfs implementation doesn't handle NO_HOLE feature correctly
[BUG]
When passing a btrfs with NO_HOLE feature to U-boot, and if one file
contains holes, then the hash of the file is not correct in U-boot:

 # mkfs.btrfs -f test.img	# Since v5.15, mkfs defaults to NO_HOLES
 # mount test.img /mnt/btrfs
 # xfs_io -f -c "pwrite 0 4k" -c "pwrite 8k 4k" /mnt/btrfs/file
 # md5sum /mnt/btrfs/file
 277f3840b275c74d01e979ea9d75ac19  /mnt/btrfs/file
 # umount /mnt/btrfs
 # ./u-boot
 => host bind 0 /home/adam/test.img
 => ls host 0
 <   >      12288  Mon Dec 27 05:35:23 2021  file
 => load host 0 0x1000000 file
 12288 bytes read in 0 ms
 => md5sum 0x1000000 0x3000
 md5 for 01000000 ... 01002fff ==> 855ffdbe4d0ccc5acab92e1b5330e4c1

The md5sum doesn't match at all.

[CAUSE]
In U-boot btrfs implementation, the function btrfs_read_file() has the
following iteration for file extent iteration:

	/* Read the aligned part */
	while (cur < aligned_end) {
		ret = lookup_data_extent(root, &path, ino, cur, &next_offset);
		if (ret < 0)
			goto out;
		if (ret > 0) {
			/* No next, direct exit */
			if (!next_offset) {
				ret = 0;
				goto out;
			}
		}
		/* Read file extent */

But for NO_HOLES features, hole extents will not have any extent item
for it.
Thus if @cur is at a hole, lookup_data_extent() will just return >0, and
update @next_offset.

But we still believe there is some data to read for @cur for ret > 0
case, causing we read extent data from the next file extent.

This means, what we do for above NO_HOLES btrfs is:
- Read 4K data from disk to file offset [0, 4K)
  So far the data is still correct

- Read 4K data from disk to file offset [4K, 8K)
  We didn't skip the 4K hole, but read the data at file offset [8K, 12K)
  into file offset [4K, 8K).

  This causes the checksum mismatch.

[FIX]
Add extra check to skip to the next non-hole range after
lookup_data_extent().

Signed-off-by: Qu Wenruo <wqu@suse.com>
2022-01-18 08:31:02 -05: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 WS cleanup: remove SPACE(s) followed by TAB 2021-09-30 09:08:16 -04:00
btrfs.h fs: btrfs: Cleanup the old implementation 2020-09-07 21:00:36 -04:00
compat.h fs: btrfs: Introduce function to resolve path in one subvolume 2020-09-07 21:00:36 -04:00
compression.c btrfs: Use U-Boot API for decompression 2021-10-08 15:53:26 -04:00
conv-funcs.h SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
ctree.c fs: btrfs: Cleanup the old implementation 2020-09-07 21:00:36 -04:00
ctree.h fs: btrfs: Cleanup the old implementation 2020-09-07 21:00:36 -04:00
dev.c common: Drop part.h from common header 2020-05-18 17:33:33 -04:00
dir-item.c fs: btrfs: Cleanup the old implementation 2020-09-07 21:00:36 -04:00
disk-io.c fs/btrfs: add dependency on BLAKE2 hash 2022-01-18 08:31:02 -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 fs: btrfs: Cleanup the old implementation 2020-09-07 21:00:36 -04:00
extent-io.h fs: btrfs: Crossport extent-io.[ch] from btrfs-progs 2020-09-07 20:57:27 -04:00
inode.c fs/btrfs: fix a bug that U-boot fs btrfs implementation doesn't handle NO_HOLE feature correctly 2022-01-18 08:31:02 -05:00
Kconfig fs/btrfs: add dependency on BLAKE2 hash 2022-01-18 08:31:02 -05:00
Makefile fs: btrfs: Cleanup the old implementation 2020-09-07 21:00:36 -04:00
root-tree.c fs: btrfs: Crossport open_ctree_fs_info() from btrfs-progs 2020-09-07 20:57:27 -04:00
subvolume.c fs: btrfs: Cleanup the old implementation 2020-09-07 21:00:36 -04:00
volumes.c fs: btrfs: volumes: prevent overflow for multiplying 2021-01-20 14:01:08 -05:00
volumes.h fs: btrfs: Crossport btrfs_read_sys_array() and btrfs_read_chunk_tree() 2020-09-07 20:57:27 -04:00