btrfs: mark dirty bit for out of bound prealloc extents
PATCH 1/1 btrfs: mark dirty bit for out of bound prealloc extents
In btrfs_fallocate(), when the allocated range overlaps with a prealloc extent and the extent starts after i_size, the range doesn't get marked dirty in file_extent_tree.
It affects btrfs_find_contiguous_extent_bit() in btrfs_inode_safe_disk_i_size_write() procedure and can produce wrong on-disk i_size.
This would be reproducible since the commit
41a2ee7("btrfs: introduce per-inode file extent tree"), and became hidden since 3d7db6e("btrfs: don't allocate file extent tree for non regular files") stops initializing file_extent_tree. A quick example as follows:
btrfs sub cre sub1
touch sub1/file
fallocate -n -o 1359872 -l 540672 sub1/file
btrfs sub snap sub1 sub2
fallocate -n -p -o 1359872 -l 395859 sub2/file
fallocate -o 1359872 -l 395859 sub2/file
echo 3 > /proc/sys/vm/drop_caches
ls -l sub2/file
The correct size should be 1755731 instead of 1753088.
Fix by adding a call to btrfs_inode_set_file_extent_range() for such extents.
--- fs/btrfs/file.c | 5 +++++ 1 file changed, 5 insertions(+)
fs/btrfs/file.c +5 -0
@@ -3209,6 +3209,11 @@ static long btrfs_fallocate(struct file *file, int mode, } qgroup_reserved += range_len; data_space_needed += range_len; + }else { + ret =btrfs_inode_set_file_extent_range (BTRFS_I (inode), + em->start, em->len); +if (ret <0 ) +break ; }btrfs_free_extent_map (em); cur_offset = last_byte;
Signature · 4 lines
2.34.1 Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.
Quoted 5 lines · austinchang
In
btrfs_fallocate(), when the allocated range overlaps with a prealloc extent and the extent starts afteri_size, the range doesn't get marked dirty infile_extent_tree.
Quoted 7 lines
It affects
btrfs_find_contiguous_extent_bit()inbtrfs_inode_safe_disk_i_size_write()procedure and can produce wrong on-diski_size.This would be reproducible since the commit
41a2ee7("btrfs: introduce per-inode file extent tree"), and became hidden since 3d7db6e("btrfs: don't allocate file extent tree for non regular files")
The format to use in kernel change logs is:
<first 12 characters of commit> <single splace> ("btrfs: ...")
So for example:
41a2ee75aab0 ("btrfs: introduce per-inode file extent tree")
Quoted 11 lines
stops initializing
file_extent_tree. A quick example as follows:btrfs sub cre sub1
touch sub1/file
fallocate -n -o 1359872 -l 540672 sub1/file
btrfs sub snap sub1 sub2
fallocate -n -p -o 1359872 -l 395859 sub2/file
fallocate -o 1359872 -l 395859 sub2/file
echo 3 >/proc/sys/vm/drop_caches
ls -l sub2/file
Quoted 4 lines
The correct size should be 1755731 instead of 1753088.
Fix by adding a call to
btrfs_inode_set_file_extent_range()for such extents.
This is a too terse and incomplete explanation.
First, let the example be explicit that for this to trigger it needs to run without the no-holes feature enabled (which is a default for quite some time now).
Second, why do we need snapshots and subvolumes?
The first paragraph doesn't explain the whole picture, since without creating a RW snapshot and doing the second and third fallocates on the snapshot, the problem doesn't reproduce.
So snapshotting seems to be a necessary condition - but why?
The subvolume creation is an unnecessary step, we can just use the default subvolume and make the example more concise.
Also please make the reproducer easier to follow, with comments, with more friendly values, explanation for any magic values and without unnecessary steps.
For example, this:
"
The following reproducer triggers the problem:
$ cat test.sh #!/bin/bash
DEV=/dev/sdi
MNT=/mnt/sdi
mkfs.btrfs -f -O ^no-holes $DEV
mount $DEV $MNT
touch $MNT/file
# Add a 2M extent at file offset 1M without increasing i_size. fallocate -n -o 1M -l 2M $MNT/file
# Maybe add a comment here explaining why we need the snapshot. btrfs subvolume snapshot $MNT $MNT/snap
# Use a length not aligned to sector size - 1M + 123 bytes. len=$((1 * 1024 * 1024 + 123))
# Now punch a hole at the start of the new allocated extent for a
# length that is smaller than the extent's length and unaligned. fallocate -n -p -o 1M -l $len $MNT/snap/file
# Now allocate the range which we punched a hole for and allow
# fallocate to increase the file's i_size. fallocate -o 1M -l $len $MNT/snap/file
# Check the file size, should be 2M + 123 bytes (1M + $len). du --bytes $MNT/snap/file
# Unmount and mount again the fs.
umount $MNT
mount $DEV $MNT
# The file size should be the same as before.
du --bytes $MNT/snap/file
umount $MNT
Running reproducer gives the following result:
$ ./test.sh (...) Create snapshot of '/mnt/sdi' in '/mnt/sdi/snap' 2097275 /mnt/sdi/snap/file 2097152 /mnt/sdi/snap/file
The difference is exactly 123 bytes ...
"
This makes it a lot easier to understand without magic values and making it clear that an unaligned length is needed to trigger the problem.
But please explain why the snapshotting is needed to trigger the problem as well as the unaligned range.
Once that is clear and we have a minimal reproducer, we should also have a test for fstests to prevent regressions.
Quoted 2 lines
Signed-off-by austinchang <[email protected]>
Here missing a Fixes tag:
Quoted 9 lines
--- fs/btrfs/file.c | 5 +++++ 1 file changed, 5 insertions(+)fs/btrfs/file.c +0
-0
fs/btrfs/file.c static long btrfs_fallocate(struct file *file, int mode, · line 3216
Quoted 8 lines
fs/btrfs/file.c +5
-0@@ -3209,6 +3209,11 @@ static long btrfs_fallocate(struct file *file, int mode, } qgroup_reserved += range_len; data_space_needed += range_len; + }else { + ret =btrfs_inode_set_file_extent_range (BTRFS_I (inode), + em->start, em->len); +if (ret <0 ) +break ;
This is an odd place for this.
I'd rather have this placed in the most logical place which is btrfs_fallocate_update_isize(), since it's where we update i_size and this is closely related.
So:
fs/btrfs/file.c +0 -0
@@ -2855,12 +2855,22 @@ static int btrfs_fallocate_update_isize(struct
inode *inode,
{
struct btrfs_trans_handle *trans;
struct btrfs_root *root = BTRFS_I(inode)->root;
+ u64 range_start;
+ u64 range_end;
int ret;
int ret2;
if (mode & FALLOC_FL_KEEP_SIZE || end <= i_size_read(inode))
return 0;+ range_start = round_down(i_size_read(inode), root->fs_info->sectorsize);
+ range_end = round_up(end, root->fs_info->sectorsize);
+
+ ret = btrfs_inode_set_file_extent_range(BTRFS_I(inode), range_start,
+ range_end - range_start);
+ if (ret)
+ return ret;
+
trans = btrfs_start_transaction(root, 1);
if (IS_ERR(trans))
return PTR_ERR(trans);Thanks.
Quoted 9 lines
fs/btrfs/file.c +0
-1}btrfs_free_extent_map (em); cur_offset = last_byte; --2.34.1
Disclaimer: The contents of this e-mail message and any attachments are confidential and are intended solely for addressee. The information may also be legally privileged. This transmission is sent in trust, for the sole purpose of delivery to the intended recipient. If you have received this transmission in error, any use, reproduction or dissemination of this transmission is strictly prohibited. If you are not the intended recipient, please immediately notify the sender by reply e-mail or phone and delete this message and its attachments, if any.