btrfs: mark dirty bit for out of bound prealloc extents
PATCH v2 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.
This is reproducible since the commit41a2ee75aab0 ("btrfs: introduce per-inode file extent tree"),
and became hidden since3d7db6e8bd22 ("btrfs: don't allocate file extent tree for non regular files") stops initializing file_extent_tree.
The following reproducer triggers the problem:
$ cat test.sh #!/bin/sh
MNT=/mnt/test
DEV=/dev/vdb
mkdir -p $MNT
mkfs.btrfs -f -O ^no-holes $DEV
mount $DEV $MNT
touch $MNT/file1
# Add a 2M extent at the file offset 1M without increasing i_size fallocate -n -o 1M -l 2M $MNT/file1
# Unmount and mount again to clear the in-memory extent tree umount $MNT
mount $DEV $MNT
# Use a length that doesn't trigger realloc of the 2M extent before len=$((1 * 1024 * 1024))
# fallocate on the prealloc extent to change i_size fallocate -o 1M -l $len $MNT/file1
# Check the file size, expecting 2M
du --bytes $MNT/file1
# Unmount and mount again the fs.
umount $MNT
mount $DEV $MNT
# The file size should be the same as before
du --bytes $MNT/file1
umount $MNT
Running the reproducer gives the following result:
$ ./test.sh (...) 2097152 /mnt/test/file1 1048576 /mnt/test/file1
The difference is exactly 1048576 as we assigned.
Fix by adding a call to btrfs_inode_set_file_extent_range() in btrfs_fallocate_update_isize().
---
Changelog:
v2: make a simpler reproducer and move the call of btrfs_inode_set_file_extent_range().
The snapshot in v1 is to make the extent tree empty for the inode, so "unmount then mount" can also trigger the issue.
We discovered that the unaligned length is only needed when the punch hole is present. It will drop and replace the extent to be aligned, and the later fallocate only marks the range on newly allocated 1M extent, which is also aligned. Once the punch hole is removed, the requirement become not to trigger new allocation on the 2M extent initially.
V2 also moves the call of btrfs_inode_set_file_extent_range() into btrfs_fallocate_update_isize() so that the range can be updated for all the extent operations that might affect i_size.
--- fs/btrfs/file.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
fs/btrfs/file.c +11 -0
@@ -2855,12 +2855,23 @@ 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);
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 10 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.This is reproducible since the commit
41a2ee75aab0("btrfs: introduce per-inode file extent tree"),
and became hidden since3d7db6e8bd22("btrfs: don't allocate file extent tree for non regular files") stops initializingfile_extent_tree.
So this is confusing because it misses mentioning that the issue became visible again after commit 8679d2687c35 ("btrfs: initialize inode::file_extent_tree after i_mode has been set").
I'll add that information when merging this to for-next.
Quoted 17 lines
The following reproducer triggers the problem:
$ cat test.sh #!/bin/shMNT=/mnt/test
DEV=/dev/vdbmkdir -p $MNT
mkfs.btrfs -f -O ^no-holes $DEV
mount $DEV $MNTtouch $MNT/file1
# Add a 2M extent at the file offset 1M without increasingi_sizefallocate -n -o 1M -l 2M $MNT/file1
Quoted 5 lines
# Unmount and mount again to clear the in-memory extent tree umount $MNT
mount $DEV $MNT# Use a length that doesn't trigger realloc of the 2M extent before
This comment is confusing.
What do you mean by "trigger realloc"?
Doing an fallocate against a range which already has allocated extents won't drop the extents and allocate new ones.
Perhaps what you want to say is that we want fallocate within the range of the previously allocated extent just to increase the i_size.
You don't need to send a new patch version for that. I can just replace it with:
"Allocate within the range of the previously allocated extent just to increase i_size."
Quoted 17 lines
len=$((1 * 1024 * 1024))
# fallocate on the prealloc extent to change i_size fallocate -o 1M -l $len $MNT/file1# Check the file size, expecting 2M
du --bytes $MNT/file1# Unmount and mount again the fs.
umount $MNT
mount $DEV $MNT# The file size should be the same as before
du --bytes $MNT/file1umount $MNT
Quoted 8 lines
Running the reproducer gives the following result:
$ ./test.sh (...) 2097152 /mnt/test/file1 1048576 /mnt/test/file1The difference is exactly 1048576 as we assigned.
See?
This is how a reproducer for a change log should be.
Easy to copy paste, modify as needed and run, with comments explaining steps. The previous one with snapshot and subvolume and magic numbers was not explained at all and more complex than it needed to be.
Thanks.
Quoted 61 lines
Fix by adding a call to
btrfs_inode_set_file_extent_range()inbtrfs_fallocate_update_isize().Fixes 41a2ee75aab0 ("btrfs: introduce per-inode file extent tree")Signed-off-by austinchang <[email protected]>---
Changelog:
v2: make a simpler reproducer and move the call ofbtrfs_inode_set_file_extent_range().The snapshot in v1 is to make the extent tree empty for the inode, so "unmount then mount" can also trigger the issue.
We discovered that the unaligned length is only needed when the punch hole is present. It will drop and replace the extent to be aligned, and the later fallocate only marks the range on newly allocated 1M extent, which is also aligned. Once the punch hole is removed, the requirement become not to trigger new allocation on the 2M extent initially.
V2 also moves the call of
btrfs_inode_set_file_extent_range()intobtrfs_fallocate_update_isize()so that the range can be updated for all the extent operations that might affecti_size.--- fs/btrfs/file.c | 11 +++++++++++ 1 file changed, 11 insertions(+)fs/btrfs/file.c +11
-1@@ -2855,12 +2855,23 @@ 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); --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.