Skip to contentSkip to conversation

fstests: btrfs: add test for fallocate on prealloc extents

PATCH In review austinchang 2 participants 2 messages Oct 31, 2025 Btrfs file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fstests: btrfs: add test for fallocate on prealloc extents

In subvolume without no_holes feature, following sequence can produce incorrect i_size for a file:

1. Allocate an extent at the offset larger than the i_size of the file.
2. Unmount then mount back the volume.
3. Perform fallocate on the preallocated range to increase the i_size.
4. The i_size won't reflect the fallocate in 3. after umount then mount.

The bug exists since the file_extent_tree has been introduced:
41a2ee75aab0 ("btrfs: introduce per-inode file extent tree")
And became hidden since
3d7db6e8bd22 ("btrfs: don't allocate file extent tree for non regular files") Then became visible again after
8679d2687c35 ("btrfs: initialize inode::file_extent_tree after i_mode has been set").

The test creates the file with the extents mentioned above and verify that the file size is consistent after unmount and mount.

---
 tests/btrfs/338     | 48 +++++++++++++++++++++++++++++++++++++++++++++
 tests/btrfs/338.out |  3 +++
 2 files changed, 51 insertions(+)
 create mode 100755 tests/btrfs/338
 create mode 100755 tests/btrfs/338.out
tests/btrfs/338 new file +48 -0
new file mode 100755
index 00000000..565a5ae2
@@ -0,0 +1,48 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2025 Synology Inc.  All Rights Reserved.
+#
+# FS QA Test 338
+#
+# Tests the file size when the fallocate is performed on prealloc extents
+# that start after i_size.
+#
+. ./common/preamble
+_begin_fstest auto quick prealloc
+
+_fixed_by_kernel_commit xxxxxxxxxxxx \
+	"btrfs: mark dirty bit for out of bound prealloc extents"
+
+. ./common/filter
+
+_require_btrfs_mkfs_feature "no-holes"
+
+_scratch_mkfs -O ^no-holes >>$seqres.full 2>&1
+_scratch_mount || _fail "mount failed"
+
+touch $SCRATCH_MNT/file1
+
+# Add a 2M extent at the file offset 1M without increasing i_size
+fallocate -n -o 1M -l 2M $SCRATCH_MNT/file1
+
+# Unmount and mount again to clear the in-memory extent tree
+_scratch_unmount
+_scratch_mount
+
+# 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 $SCRATCH_MNT/file1
+
+du --bytes $SCRATCH_MNT/file1| cut -f1
+
+_scratch_unmount
+_scratch_mount
+
+# The file size should be the same as before
+du --bytes $SCRATCH_MNT/file1| cut -f1
+
+_scratch_unmount
+
+_exit 0
tests/btrfs/338.out new file +3 -0
new file mode 100755
index 00000000..0edd2502
@@ -0,0 +1,3 @@
+QA output created by 338
+2097152
+2097152
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.
#1 lore raw
Quoted 38 lines · austinchang

In subvolume without no_holes feature, following sequence can produce incorrect i_size for a file:

1. Allocate an extent at the offset larger than the i_size of the file.
2. Unmount then mount back the volume.
3. Perform fallocate on the preallocated range to increase the i_size.
4. The i_size won't reflect the fallocate in 3. after umount then mount.

The bug exists since the file_extent_tree has been introduced:
41a2ee75aab0 ("btrfs: introduce per-inode file extent tree")
And became hidden since
3d7db6e8bd22 ("btrfs: don't allocate file extent tree for non regular files") Then became visible again after
8679d2687c35 ("btrfs: initialize inode::file_extent_tree after i_mode has been set").

The test creates the file with the extents mentioned above and verify that the file size is consistent after unmount and mount.

---
 tests/btrfs/338     | 48 +++++++++++++++++++++++++++++++++++++++++++++
 tests/btrfs/338.out |  3 +++
 2 files changed, 51 insertions(+)
 create mode 100755 tests/btrfs/338
 create mode 100755 tests/btrfs/338.out
tests/btrfs/338 new file +4 -0
new file mode 100755
index 00000000..565a5ae2
@@ -0,0 +1,48 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2025 Synology Inc.  All Rights Reserved.
+#

tests/btrfs/338 line 14

Quoted 10 lines
tests/btrfs/338 new file +10 -0
+# FS QA Test 338
+#
+# Tests the file size when the fallocate is performed on prealloc extents
+# that start after i_size.
+#
+. ./common/preamble
+_begin_fstest auto quick prealloc
+
+_fixed_by_kernel_commit xxxxxxxxxxxx \
+       "btrfs: mark dirty bit for out of bound prealloc extents"

The subject was changed when it was added to the for-next branch, so now it is:

"btrfs: mark dirty extent range for out of bound prealloc extents"

tests/btrfs/338 line 18

Quoted 4 lines
tests/btrfs/338 new file +4 -0
+
+. ./common/filter
+
+_require_btrfs_mkfs_feature "no-holes"

Missing kernel support check:

_require_btrfs_fs_feature "no_holes"

tests/btrfs/338 line 21

Quoted 3 lines
tests/btrfs/338 new file +3 -0
+
+_scratch_mkfs -O ^no-holes >>$seqres.full 2>&1
+_scratch_mount || _fail "mount failed"

If the mount fails we don't need to call _fail - the test fails automatically. However we're ignoring mkfs failures there, since stdout and stderr are redirected. There we do a _fail in more recent tests, so make sure the test runs on a new fs.

So this should be:

_scratch_mkfs -O ^no-holes >>$seqres.full 2>&1 || _fail "mkfs failed" _scratch_mount

tests/btrfs/338 line 30

Quoted 9 lines
tests/btrfs/338 new file +9 -0
+
+touch $SCRATCH_MNT/file1
+
+# Add a 2M extent at the file offset 1M without increasing i_size
+fallocate -n -o 1M -l 2M $SCRATCH_MNT/file1
+
+# Unmount and mount again to clear the in-memory extent tree
+_scratch_unmount
+_scratch_mount

_scratch_cycle_mount

tests/btrfs/338 line 32

Quoted 2 lines
tests/btrfs/338 new file +2 -0
+
+# Use a length that doesn't trigger realloc of the 2M extent before

Same comment I made for the kernel patch.
This is confusing, because any fallocate against a range that already has extents never reallocates.

I changed the kernel change log version to:

# Use a length that fits within the range of the previously allocated

# extent. We want the next fallocate to only increase the i_size.

tests/btrfs/338 line 33

Quoted 1 line
tests/btrfs/338 new file +1 -0
+len=$((1 * 1024 * 1024))

Btw, this can be simply

len=1M

tests/btrfs/338 line 38

Quoted 5 lines
tests/btrfs/338 new file +5 -0
+
+# fallocate on the prealloc extent to change i_size
+fallocate -o 1M -l $len $SCRATCH_MNT/file1
+
+du --bytes $SCRATCH_MNT/file1| cut -f1

Please add a space before the pipe too, to make it more readable.

tests/btrfs/338 line 30

Quoted 3 lines
tests/btrfs/338 new file +3 -0
+
+_scratch_unmount
+_scratch_mount

_scratch_cycle_mount

tests/btrfs/338 line 44

Quoted 3 lines
tests/btrfs/338 new file +3 -0
+
+# The file size should be the same as before
+du --bytes $SCRATCH_MNT/file1| cut -f1

Same as before, please add a space before the pipe too, to make it more readable.

tests/btrfs/338 line 46

Quoted 2 lines
tests/btrfs/338 new file +2 -0
+
+_scratch_unmount

No need for this. The fstests framework automatically unmounts.

Thanks.

Quoted 16 lines
tests/btrfs/338 new file +2 -0
+
+_exit 0
tests/btrfs/338.out new file +3 -1
new file mode 100755
index 00000000..0edd2502
@@ -0,0 +1,3 @@
+QA output created by 338
+2097152
+2097152
--

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.