* Re: [PATCH] btrfs: add a test for defrag of contiguous file extents
2024-10-29 17:23 [PATCH] btrfs: add a test for defrag of contiguous file extents fdmanana
@ 2024-10-30 0:48 ` Anand Jain
2024-10-30 1:01 ` Anand Jain
2024-10-30 7:38 ` Filipe Manana
2024-10-30 7:50 ` [PATCH v2] " fdmanana
2024-11-04 11:41 ` [PATCH v3] " fdmanana
2 siblings, 2 replies; 8+ messages in thread
From: Anand Jain @ 2024-10-30 0:48 UTC (permalink / raw)
To: fdmanana, fstests; +Cc: linux-btrfs, Filipe Manana
On 30/10/24 01:23, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
>
> Test that defrag merges adjacent extents that are contiguous.
> This exercises a regression fixed by a patchset for the kernel that is
> comprissed of the following patches:
>
> btrfs: fix extent map merging not happening for adjacent extents
> btrfs: fix defrag not merging contiguous extents due to merged extent maps
>
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
> ---
> tests/btrfs/325 | 80 +++++++++++++++++++++++++++++++++++++++++++++
> tests/btrfs/325.out | 22 +++++++++++++
> 2 files changed, 102 insertions(+)
> create mode 100755 tests/btrfs/325
> create mode 100644 tests/btrfs/325.out
>
> diff --git a/tests/btrfs/325 b/tests/btrfs/325
> new file mode 100755
> index 00000000..0b1ab3c2
> --- /dev/null
> +++ b/tests/btrfs/325
> @@ -0,0 +1,80 @@
> +#! /bin/bash
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (C) 2024 SUSE Linux Products GmbH. All Rights Reserved.
> +#
> +# FS QA Test 325
> +#
> +# Test that defrag merges adjacent extents that are contiguous.
> +#
> +. ./common/preamble
> +_begin_fstest auto quick preallocrw defrag
> +
> +. ./common/filter
> +
> +_require_scratch
> +_require_btrfs_command inspect-internal dump-tree
> +_require_xfs_io_command "falloc"
> +
> +_fixed_by_kernel_commit xxxxxxxxxxxx \
> + "btrfs: fix extent map merging not happening for adjacent extents"
> +_fixed_by_kernel_commit xxxxxxxxxxxx \
> + "btrfs: fix defrag not merging contiguous extents due to merged extent maps"
> +
> +count_file_extent_items()
> +{
> + # We count file extent extent items through dump-tree instead of using
> + # fiemap because fiemap merges adjacent extent items when they are
> + # contiguous.
> + # We unmount because all metadata must be ondisk for dump-tree to see
> + # it and work correctly.
> + _scratch_unmount
> + $BTRFS_UTIL_PROG inspect-internal dump-tree -t 5 $SCRATCH_DEV | \
> + grep EXTENT_DATA | wc -l
> + _scratch_mount
> +}
> +
> +_scratch_mkfs >>$seqres.full 2>&1 || _fail "mkfs failed"
> +_scratch_mount
> +
> +# Create a file with a size of 256K and 4 written extents of 64K each.
> +# We fallocate to guarantee exact extent size, even if compression mount
> +# option is give, and write to them because defrag skips prealloc extents.
> +$XFS_IO_PROG -f -c "falloc 0 64K" \
> + -c "pwrite -S 0xab 0 64K" \
> + -c "falloc 64K 64K" \
> + -c "pwrite -S 0xcd 64K 64K" \
> + -c "falloc 128K 64K" \
> + -c "pwrite -S 0xef 128K 64K" \
> + -c "falloc 192K 64K" \
> + -c "pwrite -S 0x73 192K 64K" \
> + $SCRATCH_MNT/foo | _filter_xfs_io
> +
> +echo -n "Initial number of file extent items: "
> +count_file_extent_items
> +
> +# Read the whole file in order to load extent maps and merge them.
> +cat $SCRATCH_MNT/foo > /dev/null
> +
> +# Now defragment with a threshold of 128K. After this we expect to get a file
> +# with 1 file extent item - the treshold is 128K but since all the extents are
> +# contiguous, they should be merged into a single one of 256K.
> +$BTRFS_UTIL_PROG filesystem defragment -t 128K $SCRATCH_MNT/foo
> +echo -n "Number of file extent items after defrag with 128K treshold: "
Nit: s/treshold/threshold/g
> +count_file_extent_items
> +
> +# Read the whole file in order to load extent maps and merge them.
> +cat $SCRATCH_MNT/foo > /dev/null
> +
> +# Now defragment with a threshold of 256K. After this we expect to get a file
> +# with only 1 file extent item.
> +$BTRFS_UTIL_PROG filesystem defragment -t 256K $SCRATCH_MNT/foo
> +echo -n "Number of file extent items after defrag with 256K treshold: "
> +count_file_extent_items
> +
> +# Check that the file has the expected data, that defrag didn't cause any data
> +# loss or corruption.
> +echo "File data after defrag:"
> +_hexdump $SCRATCH_MNT/foo
> +
Nice.
Nit: This can be a generic test-case.
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Thx, Anand
> +status=0
> +exit
> diff --git a/tests/btrfs/325.out b/tests/btrfs/325.out
> new file mode 100644
> index 00000000..96053925
> --- /dev/null
> +++ b/tests/btrfs/325.out
> @@ -0,0 +1,22 @@
> +QA output created by 325
> +wrote 65536/65536 bytes at offset 0
> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +wrote 65536/65536 bytes at offset 65536
> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +wrote 65536/65536 bytes at offset 131072
> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +wrote 65536/65536 bytes at offset 196608
> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +Initial number of file extent items: 4
> +Number of file extent items after defrag with 128K treshold: 1
> +Number of file extent items after defrag with 256K treshold: 1
> +File data after defrag:
> +000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab >................<
> +*
> +010000 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd >................<
> +*
> +020000 ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef >................<
> +*
> +030000 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 >ssssssssssssssss<
> +*
> +040000
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] btrfs: add a test for defrag of contiguous file extents
2024-10-30 0:48 ` Anand Jain
@ 2024-10-30 1:01 ` Anand Jain
2024-10-30 7:38 ` Filipe Manana
1 sibling, 0 replies; 8+ messages in thread
From: Anand Jain @ 2024-10-30 1:01 UTC (permalink / raw)
To: fdmanana, fstests; +Cc: linux-btrfs, Filipe Manana
On 30/10/24 08:48, Anand Jain wrote:
> On 30/10/24 01:23, fdmanana@kernel.org wrote:
>> From: Filipe Manana <fdmanana@suse.com>
>>
>> Test that defrag merges adjacent extents that are contiguous.
>> This exercises a regression fixed by a patchset for the kernel that is
>> comprissed of the following patches:
>>
>> btrfs: fix extent map merging not happening for adjacent extents
>> btrfs: fix defrag not merging contiguous extents due to merged
>> extent maps
>>
>> Signed-off-by: Filipe Manana <fdmanana@suse.com>
>> ---
>> tests/btrfs/325 | 80 +++++++++++++++++++++++++++++++++++++++++++++
>> tests/btrfs/325.out | 22 +++++++++++++
>> 2 files changed, 102 insertions(+)
>> create mode 100755 tests/btrfs/325
>> create mode 100644 tests/btrfs/325.out
>>
>> diff --git a/tests/btrfs/325 b/tests/btrfs/325
>> new file mode 100755
>> index 00000000..0b1ab3c2
>> --- /dev/null
>> +++ b/tests/btrfs/325
>> @@ -0,0 +1,80 @@
>> +#! /bin/bash
>> +# SPDX-License-Identifier: GPL-2.0
>> +# Copyright (C) 2024 SUSE Linux Products GmbH. All Rights Reserved.
>> +#
>> +# FS QA Test 325
>> +#
>> +# Test that defrag merges adjacent extents that are contiguous.
>> +#
>> +. ./common/preamble
>> +_begin_fstest auto quick preallocrw defrag
>> +
>> +. ./common/filter
>> +
>> +_require_scratch
>> +_require_btrfs_command inspect-internal dump-tree
>> +_require_xfs_io_command "falloc"
>> +
>> +_fixed_by_kernel_commit xxxxxxxxxxxx \
>> + "btrfs: fix extent map merging not happening for adjacent extents"
>> +_fixed_by_kernel_commit xxxxxxxxxxxx \
>> + "btrfs: fix defrag not merging contiguous extents due to merged
>> extent maps"
>> +
>> +count_file_extent_items()
>> +{
>> + # We count file extent extent items through dump-tree instead of
>> using
>> + # fiemap because fiemap merges adjacent extent items when they are
>> + # contiguous.
>> + # We unmount because all metadata must be ondisk for dump-tree to
>> see
>> + # it and work correctly.
>> + _scratch_unmount
>> + $BTRFS_UTIL_PROG inspect-internal dump-tree -t 5 $SCRATCH_DEV | \
>> + grep EXTENT_DATA | wc -l
>> + _scratch_mount
>> +}
>> +
>> +_scratch_mkfs >>$seqres.full 2>&1 || _fail "mkfs failed"
>> +_scratch_mount
>> +
>> +# Create a file with a size of 256K and 4 written extents of 64K each.
>> +# We fallocate to guarantee exact extent size, even if compression mount
>> +# option is give, and write to them because defrag skips prealloc
>> extents.
>> +$XFS_IO_PROG -f -c "falloc 0 64K" \
>> + -c "pwrite -S 0xab 0 64K" \
>> + -c "falloc 64K 64K" \
>> + -c "pwrite -S 0xcd 64K 64K" \
>> + -c "falloc 128K 64K" \
>> + -c "pwrite -S 0xef 128K 64K" \
>> + -c "falloc 192K 64K" \
>> + -c "pwrite -S 0x73 192K 64K" \
>> + $SCRATCH_MNT/foo | _filter_xfs_io
>> +
With the fix applied, this test case fails when the compress option is used.
--------------
FSTYP -- btrfs
PLATFORM -- Linux/aarch64 dev2 6.12.0-rc4+ #36 SMP PREEMPT_DYNAMIC
Tue Oct 29 00:25:40 +08 2024
MKFS_OPTIONS -- /dev/sdb
MOUNT_OPTIONS -- -o compress -o context=system_u:object_r:root_t:s0
/dev/sdb /mnt/scratch
btrfs/325 1s ... - output mismatch (see
/Volumes/work/ws/fstests/results//btrfs/325.out.bad)
--- tests/btrfs/325.out 2024-10-30 08:07:48.507531048 +0800
+++ /Volumes/work/ws/fstests/results//btrfs/325.out.bad 2024-10-30
08:57:35.097186024 +0800
@@ -8,8 +8,8 @@
wrote 65536/65536 bytes at offset 196608
XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
Initial number of file extent items: 4
-Number of file extent items after defrag with 128K treshold: 1
-Number of file extent items after defrag with 256K treshold: 1
+Number of file extent items after defrag with 128K treshold: 2
+Number of file extent items after defrag with 256K treshold: 2
...
(Run 'diff -u /Volumes/work/ws/fstests/tests/btrfs/325.out
/Volumes/work/ws/fstests/results//btrfs/325.out.bad' to see the entire
diff)
--------------
One approach is to use random data, ensuring that compression remains
ineffective.
>> +echo -n "Initial number of file extent items: "
>> +count_file_extent_items
>> +
>> +# Read the whole file in order to load extent maps and merge them.
>> +cat $SCRATCH_MNT/foo > /dev/null
>> +
>> +# Now defragment with a threshold of 128K. After this we expect to
>> get a file
>
>> +# with 1 file extent item - the treshold is 128K but since all the
>> extents are
>
>> +# contiguous, they should be merged into a single one of 256K.
>> +$BTRFS_UTIL_PROG filesystem defragment -t 128K $SCRATCH_MNT/foo
>
>> +echo -n "Number of file extent items after defrag with 128K treshold: "
>
> Nit: s/treshold/threshold/g
>
>> +count_file_extent_items
>> +
>> +# Read the whole file in order to load extent maps and merge them.
>> +cat $SCRATCH_MNT/foo > /dev/null
>> +
>> +# Now defragment with a threshold of 256K. After this we expect to
>> get a file
>> +# with only 1 file extent item.
>> +$BTRFS_UTIL_PROG filesystem defragment -t 256K $SCRATCH_MNT/foo
>> +echo -n "Number of file extent items after defrag with 256K treshold: "
>> +count_file_extent_items
>> +
>> +# Check that the file has the expected data, that defrag didn't cause
>> any data
>> +# loss or corruption.
>> +echo "File data after defrag:"
>> +_hexdump $SCRATCH_MNT/foo
>> +
>
> Nice.
>
> Nit: This can be a generic test-case.
>
> Reviewed-by: Anand Jain <anand.jain@oracle.com>
Please ignore the rb for now.
Thanks, Anand
>
> Thx, Anand
>
>
>> +status=0
>> +exit
>> diff --git a/tests/btrfs/325.out b/tests/btrfs/325.out
>> new file mode 100644
>> index 00000000..96053925
>> --- /dev/null
>> +++ b/tests/btrfs/325.out
>> @@ -0,0 +1,22 @@
>> +QA output created by 325
>> +wrote 65536/65536 bytes at offset 0
>> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
>> +wrote 65536/65536 bytes at offset 65536
>> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
>> +wrote 65536/65536 bytes at offset 131072
>> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
>> +wrote 65536/65536 bytes at offset 196608
>> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
>> +Initial number of file extent items: 4
>> +Number of file extent items after defrag with 128K treshold: 1
>> +Number of file extent items after defrag with 256K treshold: 1
>> +File data after defrag:
>> +000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab
>> >................<
>> +*
>> +010000 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd
>> >................<
>> +*
>> +020000 ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef
>> >................<
>> +*
>> +030000 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73
>> >ssssssssssssssss<
>> +*
>> +040000
>
^ permalink raw reply [flat|nested] 8+ messages in thread* Re: [PATCH] btrfs: add a test for defrag of contiguous file extents
2024-10-30 0:48 ` Anand Jain
2024-10-30 1:01 ` Anand Jain
@ 2024-10-30 7:38 ` Filipe Manana
1 sibling, 0 replies; 8+ messages in thread
From: Filipe Manana @ 2024-10-30 7:38 UTC (permalink / raw)
To: Anand Jain; +Cc: fstests, linux-btrfs, Filipe Manana
On Wed, Oct 30, 2024 at 12:49 AM Anand Jain <anand.jain@oracle.com> wrote:
>
> On 30/10/24 01:23, fdmanana@kernel.org wrote:
> > From: Filipe Manana <fdmanana@suse.com>
> >
> > Test that defrag merges adjacent extents that are contiguous.
> > This exercises a regression fixed by a patchset for the kernel that is
> > comprissed of the following patches:
> >
> > btrfs: fix extent map merging not happening for adjacent extents
> > btrfs: fix defrag not merging contiguous extents due to merged extent maps
> >
> > Signed-off-by: Filipe Manana <fdmanana@suse.com>
> > ---
> > tests/btrfs/325 | 80 +++++++++++++++++++++++++++++++++++++++++++++
> > tests/btrfs/325.out | 22 +++++++++++++
> > 2 files changed, 102 insertions(+)
> > create mode 100755 tests/btrfs/325
> > create mode 100644 tests/btrfs/325.out
> >
> > diff --git a/tests/btrfs/325 b/tests/btrfs/325
> > new file mode 100755
> > index 00000000..0b1ab3c2
> > --- /dev/null
> > +++ b/tests/btrfs/325
> > @@ -0,0 +1,80 @@
> > +#! /bin/bash
> > +# SPDX-License-Identifier: GPL-2.0
> > +# Copyright (C) 2024 SUSE Linux Products GmbH. All Rights Reserved.
> > +#
> > +# FS QA Test 325
> > +#
> > +# Test that defrag merges adjacent extents that are contiguous.
> > +#
> > +. ./common/preamble
> > +_begin_fstest auto quick preallocrw defrag
> > +
> > +. ./common/filter
> > +
> > +_require_scratch
> > +_require_btrfs_command inspect-internal dump-tree
> > +_require_xfs_io_command "falloc"
> > +
> > +_fixed_by_kernel_commit xxxxxxxxxxxx \
> > + "btrfs: fix extent map merging not happening for adjacent extents"
> > +_fixed_by_kernel_commit xxxxxxxxxxxx \
> > + "btrfs: fix defrag not merging contiguous extents due to merged extent maps"
> > +
> > +count_file_extent_items()
> > +{
> > + # We count file extent extent items through dump-tree instead of using
> > + # fiemap because fiemap merges adjacent extent items when they are
> > + # contiguous.
> > + # We unmount because all metadata must be ondisk for dump-tree to see
> > + # it and work correctly.
> > + _scratch_unmount
> > + $BTRFS_UTIL_PROG inspect-internal dump-tree -t 5 $SCRATCH_DEV | \
> > + grep EXTENT_DATA | wc -l
> > + _scratch_mount
> > +}
> > +
> > +_scratch_mkfs >>$seqres.full 2>&1 || _fail "mkfs failed"
> > +_scratch_mount
> > +
> > +# Create a file with a size of 256K and 4 written extents of 64K each.
> > +# We fallocate to guarantee exact extent size, even if compression mount
> > +# option is give, and write to them because defrag skips prealloc extents.
> > +$XFS_IO_PROG -f -c "falloc 0 64K" \
> > + -c "pwrite -S 0xab 0 64K" \
> > + -c "falloc 64K 64K" \
> > + -c "pwrite -S 0xcd 64K 64K" \
> > + -c "falloc 128K 64K" \
> > + -c "pwrite -S 0xef 128K 64K" \
> > + -c "falloc 192K 64K" \
> > + -c "pwrite -S 0x73 192K 64K" \
> > + $SCRATCH_MNT/foo | _filter_xfs_io
> > +
> > +echo -n "Initial number of file extent items: "
> > +count_file_extent_items
> > +
> > +# Read the whole file in order to load extent maps and merge them.
> > +cat $SCRATCH_MNT/foo > /dev/null
> > +
> > +# Now defragment with a threshold of 128K. After this we expect to get a file
>
> > +# with 1 file extent item - the treshold is 128K but since all the extents are
>
> > +# contiguous, they should be merged into a single one of 256K.
> > +$BTRFS_UTIL_PROG filesystem defragment -t 128K $SCRATCH_MNT/foo
>
> > +echo -n "Number of file extent items after defrag with 128K treshold: "
>
> Nit: s/treshold/threshold/g
>
> > +count_file_extent_items
> > +
> > +# Read the whole file in order to load extent maps and merge them.
> > +cat $SCRATCH_MNT/foo > /dev/null
> > +
> > +# Now defragment with a threshold of 256K. After this we expect to get a file
> > +# with only 1 file extent item.
> > +$BTRFS_UTIL_PROG filesystem defragment -t 256K $SCRATCH_MNT/foo
> > +echo -n "Number of file extent items after defrag with 256K treshold: "
> > +count_file_extent_items
> > +
> > +# Check that the file has the expected data, that defrag didn't cause any data
> > +# loss or corruption.
> > +echo "File data after defrag:"
> > +_hexdump $SCRATCH_MNT/foo
> > +
>
> Nice.
>
> Nit: This can be a generic test-case.
No it can't, because:
1) Not all filesystems have a defrag functionality and with the
ability to specify a threshold;
2) We need to use dump-tree to count file extent items - both the tool
and metadata are btrfs specific.
>
> Reviewed-by: Anand Jain <anand.jain@oracle.com>
>
> Thx, Anand
>
>
> > +status=0
> > +exit
> > diff --git a/tests/btrfs/325.out b/tests/btrfs/325.out
> > new file mode 100644
> > index 00000000..96053925
> > --- /dev/null
> > +++ b/tests/btrfs/325.out
> > @@ -0,0 +1,22 @@
> > +QA output created by 325
> > +wrote 65536/65536 bytes at offset 0
> > +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> > +wrote 65536/65536 bytes at offset 65536
> > +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> > +wrote 65536/65536 bytes at offset 131072
> > +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> > +wrote 65536/65536 bytes at offset 196608
> > +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> > +Initial number of file extent items: 4
> > +Number of file extent items after defrag with 128K treshold: 1
> > +Number of file extent items after defrag with 256K treshold: 1
> > +File data after defrag:
> > +000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab >................<
> > +*
> > +010000 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd >................<
> > +*
> > +020000 ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef >................<
> > +*
> > +030000 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 >ssssssssssssssss<
> > +*
> > +040000
>
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v2] btrfs: add a test for defrag of contiguous file extents
2024-10-29 17:23 [PATCH] btrfs: add a test for defrag of contiguous file extents fdmanana
2024-10-30 0:48 ` Anand Jain
@ 2024-10-30 7:50 ` fdmanana
2024-10-30 8:20 ` Qu Wenruo
2024-11-04 11:41 ` [PATCH v3] " fdmanana
2 siblings, 1 reply; 8+ messages in thread
From: fdmanana @ 2024-10-30 7:50 UTC (permalink / raw)
To: fstests; +Cc: linux-btrfs, Filipe Manana
From: Filipe Manana <fdmanana@suse.com>
Test that defrag merges adjacent extents that are contiguous.
This exercises a regression fixed by a patchset for the kernel that is
comprissed of the following patches:
btrfs: fix extent map merging not happening for adjacent extents
btrfs: fix defrag not merging contiguous extents due to merged extent maps
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
V2: Fix typo (treshold -> threshold), make the test be skipped if compression
is enabled.
tests/btrfs/325 | 83 +++++++++++++++++++++++++++++++++++++++++++++
tests/btrfs/325.out | 22 ++++++++++++
2 files changed, 105 insertions(+)
create mode 100755 tests/btrfs/325
create mode 100644 tests/btrfs/325.out
diff --git a/tests/btrfs/325 b/tests/btrfs/325
new file mode 100755
index 00000000..48470509
--- /dev/null
+++ b/tests/btrfs/325
@@ -0,0 +1,83 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2024 SUSE Linux Products GmbH. All Rights Reserved.
+#
+# FS QA Test 325
+#
+# Test that defrag merges adjacent extents that are contiguous.
+#
+. ./common/preamble
+_begin_fstest auto quick preallocrw defrag
+
+. ./common/filter
+
+_require_scratch
+_require_btrfs_command inspect-internal dump-tree
+_require_xfs_io_command "falloc"
+# We want to test getting a 256K extent after defrag, so skip the test if
+# compression is enabled (with compression the maximum extent size is 128K).
+_require_no_compress
+
+_fixed_by_kernel_commit xxxxxxxxxxxx \
+ "btrfs: fix extent map merging not happening for adjacent extents"
+_fixed_by_kernel_commit xxxxxxxxxxxx \
+ "btrfs: fix defrag not merging contiguous extents due to merged extent maps"
+
+count_file_extent_items()
+{
+ # We count file extent extent items through dump-tree instead of using
+ # fiemap because fiemap merges adjacent extent items when they are
+ # contiguous.
+ # We unmount because all metadata must be ondisk for dump-tree to see
+ # it and work correctly.
+ _scratch_unmount
+ $BTRFS_UTIL_PROG inspect-internal dump-tree -t 5 $SCRATCH_DEV | \
+ grep EXTENT_DATA | wc -l
+ _scratch_mount
+}
+
+_scratch_mkfs >>$seqres.full 2>&1 || _fail "mkfs failed"
+_scratch_mount
+
+# Create a file with a size of 256K and 4 written extents of 64K each.
+# We fallocate to guarantee exact extent size, even if compression mount
+# option is give, and write to them because defrag skips prealloc extents.
+$XFS_IO_PROG -f -c "falloc 0 64K" \
+ -c "pwrite -S 0xab 0 64K" \
+ -c "falloc 64K 64K" \
+ -c "pwrite -S 0xcd 64K 64K" \
+ -c "falloc 128K 64K" \
+ -c "pwrite -S 0xef 128K 64K" \
+ -c "falloc 192K 64K" \
+ -c "pwrite -S 0x73 192K 64K" \
+ $SCRATCH_MNT/foo | _filter_xfs_io
+
+echo -n "Initial number of file extent items: "
+count_file_extent_items
+
+# Read the whole file in order to load extent maps and merge them.
+cat $SCRATCH_MNT/foo > /dev/null
+
+# Now defragment with a threshold of 128K. After this we expect to get a file
+# with 1 file extent item - the threshold is 128K but since all the extents are
+# contiguous, they should be merged into a single one of 256K.
+$BTRFS_UTIL_PROG filesystem defragment -t 128K $SCRATCH_MNT/foo
+echo -n "Number of file extent items after defrag with 128K threshold: "
+count_file_extent_items
+
+# Read the whole file in order to load extent maps and merge them.
+cat $SCRATCH_MNT/foo > /dev/null
+
+# Now defragment with a threshold of 256K. After this we expect to get a file
+# with only 1 file extent item.
+$BTRFS_UTIL_PROG filesystem defragment -t 256K $SCRATCH_MNT/foo
+echo -n "Number of file extent items after defrag with 256K threshold: "
+count_file_extent_items
+
+# Check that the file has the expected data, that defrag didn't cause any data
+# loss or corruption.
+echo "File data after defrag:"
+_hexdump $SCRATCH_MNT/foo
+
+status=0
+exit
diff --git a/tests/btrfs/325.out b/tests/btrfs/325.out
new file mode 100644
index 00000000..c0df8137
--- /dev/null
+++ b/tests/btrfs/325.out
@@ -0,0 +1,22 @@
+QA output created by 325
+wrote 65536/65536 bytes at offset 0
+XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 65536/65536 bytes at offset 65536
+XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 65536/65536 bytes at offset 131072
+XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 65536/65536 bytes at offset 196608
+XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+Initial number of file extent items: 4
+Number of file extent items after defrag with 128K threshold: 1
+Number of file extent items after defrag with 256K threshold: 1
+File data after defrag:
+000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab >................<
+*
+010000 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd >................<
+*
+020000 ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef >................<
+*
+030000 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 >ssssssssssssssss<
+*
+040000
--
2.45.2
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v2] btrfs: add a test for defrag of contiguous file extents
2024-10-30 7:50 ` [PATCH v2] " fdmanana
@ 2024-10-30 8:20 ` Qu Wenruo
0 siblings, 0 replies; 8+ messages in thread
From: Qu Wenruo @ 2024-10-30 8:20 UTC (permalink / raw)
To: fdmanana, fstests; +Cc: linux-btrfs, Filipe Manana
在 2024/10/30 18:20, fdmanana@kernel.org 写道:
> From: Filipe Manana <fdmanana@suse.com>
>
> Test that defrag merges adjacent extents that are contiguous.
> This exercises a regression fixed by a patchset for the kernel that is
> comprissed of the following patches:
>
> btrfs: fix extent map merging not happening for adjacent extents
> btrfs: fix defrag not merging contiguous extents due to merged extent maps
>
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Thanks,
Qu
> ---
>
> V2: Fix typo (treshold -> threshold), make the test be skipped if compression
> is enabled.
>
> tests/btrfs/325 | 83 +++++++++++++++++++++++++++++++++++++++++++++
> tests/btrfs/325.out | 22 ++++++++++++
> 2 files changed, 105 insertions(+)
> create mode 100755 tests/btrfs/325
> create mode 100644 tests/btrfs/325.out
>
> diff --git a/tests/btrfs/325 b/tests/btrfs/325
> new file mode 100755
> index 00000000..48470509
> --- /dev/null
> +++ b/tests/btrfs/325
> @@ -0,0 +1,83 @@
> +#! /bin/bash
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (C) 2024 SUSE Linux Products GmbH. All Rights Reserved.
> +#
> +# FS QA Test 325
> +#
> +# Test that defrag merges adjacent extents that are contiguous.
> +#
> +. ./common/preamble
> +_begin_fstest auto quick preallocrw defrag
> +
> +. ./common/filter
> +
> +_require_scratch
> +_require_btrfs_command inspect-internal dump-tree
> +_require_xfs_io_command "falloc"
> +# We want to test getting a 256K extent after defrag, so skip the test if
> +# compression is enabled (with compression the maximum extent size is 128K).
> +_require_no_compress
> +
> +_fixed_by_kernel_commit xxxxxxxxxxxx \
> + "btrfs: fix extent map merging not happening for adjacent extents"
> +_fixed_by_kernel_commit xxxxxxxxxxxx \
> + "btrfs: fix defrag not merging contiguous extents due to merged extent maps"
> +
> +count_file_extent_items()
> +{
> + # We count file extent extent items through dump-tree instead of using
> + # fiemap because fiemap merges adjacent extent items when they are
> + # contiguous.
> + # We unmount because all metadata must be ondisk for dump-tree to see
> + # it and work correctly.
> + _scratch_unmount
> + $BTRFS_UTIL_PROG inspect-internal dump-tree -t 5 $SCRATCH_DEV | \
> + grep EXTENT_DATA | wc -l
> + _scratch_mount
> +}
> +
> +_scratch_mkfs >>$seqres.full 2>&1 || _fail "mkfs failed"
> +_scratch_mount
> +
> +# Create a file with a size of 256K and 4 written extents of 64K each.
> +# We fallocate to guarantee exact extent size, even if compression mount
> +# option is give, and write to them because defrag skips prealloc extents.
> +$XFS_IO_PROG -f -c "falloc 0 64K" \
> + -c "pwrite -S 0xab 0 64K" \
> + -c "falloc 64K 64K" \
> + -c "pwrite -S 0xcd 64K 64K" \
> + -c "falloc 128K 64K" \
> + -c "pwrite -S 0xef 128K 64K" \
> + -c "falloc 192K 64K" \
> + -c "pwrite -S 0x73 192K 64K" \
> + $SCRATCH_MNT/foo | _filter_xfs_io
> +
> +echo -n "Initial number of file extent items: "
> +count_file_extent_items
> +
> +# Read the whole file in order to load extent maps and merge them.
> +cat $SCRATCH_MNT/foo > /dev/null
> +
> +# Now defragment with a threshold of 128K. After this we expect to get a file
> +# with 1 file extent item - the threshold is 128K but since all the extents are
> +# contiguous, they should be merged into a single one of 256K.
> +$BTRFS_UTIL_PROG filesystem defragment -t 128K $SCRATCH_MNT/foo
> +echo -n "Number of file extent items after defrag with 128K threshold: "
> +count_file_extent_items
> +
> +# Read the whole file in order to load extent maps and merge them.
> +cat $SCRATCH_MNT/foo > /dev/null
> +
> +# Now defragment with a threshold of 256K. After this we expect to get a file
> +# with only 1 file extent item.
> +$BTRFS_UTIL_PROG filesystem defragment -t 256K $SCRATCH_MNT/foo
> +echo -n "Number of file extent items after defrag with 256K threshold: "
> +count_file_extent_items
> +
> +# Check that the file has the expected data, that defrag didn't cause any data
> +# loss or corruption.
> +echo "File data after defrag:"
> +_hexdump $SCRATCH_MNT/foo
> +
> +status=0
> +exit
> diff --git a/tests/btrfs/325.out b/tests/btrfs/325.out
> new file mode 100644
> index 00000000..c0df8137
> --- /dev/null
> +++ b/tests/btrfs/325.out
> @@ -0,0 +1,22 @@
> +QA output created by 325
> +wrote 65536/65536 bytes at offset 0
> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +wrote 65536/65536 bytes at offset 65536
> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +wrote 65536/65536 bytes at offset 131072
> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +wrote 65536/65536 bytes at offset 196608
> +XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
> +Initial number of file extent items: 4
> +Number of file extent items after defrag with 128K threshold: 1
> +Number of file extent items after defrag with 256K threshold: 1
> +File data after defrag:
> +000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab >................<
> +*
> +010000 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd >................<
> +*
> +020000 ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef >................<
> +*
> +030000 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 >ssssssssssssssss<
> +*
> +040000
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH v3] btrfs: add a test for defrag of contiguous file extents
2024-10-29 17:23 [PATCH] btrfs: add a test for defrag of contiguous file extents fdmanana
2024-10-30 0:48 ` Anand Jain
2024-10-30 7:50 ` [PATCH v2] " fdmanana
@ 2024-11-04 11:41 ` fdmanana
2024-11-05 20:20 ` Anand Jain
2 siblings, 1 reply; 8+ messages in thread
From: fdmanana @ 2024-11-04 11:41 UTC (permalink / raw)
To: fstests; +Cc: linux-btrfs, Filipe Manana, Qu Wenruo
From: Filipe Manana <fdmanana@suse.com>
Test that defrag merges adjacent extents that are contiguous.
This exercises a regression fixed by a patchset for the kernel that is
comprissed of the following patches:
btrfs: fix extent map merging not happening for adjacent extents
btrfs: fix defrag not merging contiguous extents due to merged extent maps
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
V3: Add git commit IDs, the patches were merged into Linus' tree (6.12-rc6).
V2: Fix typo (treshold -> threshold), make the test be skipped if compression
is enabled.
tests/btrfs/325 | 83 +++++++++++++++++++++++++++++++++++++++++++++
tests/btrfs/325.out | 22 ++++++++++++
2 files changed, 105 insertions(+)
create mode 100755 tests/btrfs/325
create mode 100644 tests/btrfs/325.out
diff --git a/tests/btrfs/325 b/tests/btrfs/325
new file mode 100755
index 00000000..ad86bae5
--- /dev/null
+++ b/tests/btrfs/325
@@ -0,0 +1,83 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2024 SUSE Linux Products GmbH. All Rights Reserved.
+#
+# FS QA Test 325
+#
+# Test that defrag merges adjacent extents that are contiguous.
+#
+. ./common/preamble
+_begin_fstest auto quick preallocrw defrag
+
+. ./common/filter
+
+_require_scratch
+_require_btrfs_command inspect-internal dump-tree
+_require_xfs_io_command "falloc"
+# We want to test getting a 256K extent after defrag, so skip the test if
+# compression is enabled (with compression the maximum extent size is 128K).
+_require_no_compress
+
+_fixed_by_kernel_commit a0f062539085 \
+ "btrfs: fix extent map merging not happening for adjacent extents"
+_fixed_by_kernel_commit 77b0d113eec4 \
+ "btrfs: fix defrag not merging contiguous extents due to merged extent maps"
+
+count_file_extent_items()
+{
+ # We count file extent extent items through dump-tree instead of using
+ # fiemap because fiemap merges adjacent extent items when they are
+ # contiguous.
+ # We unmount because all metadata must be ondisk for dump-tree to see
+ # it and work correctly.
+ _scratch_unmount
+ $BTRFS_UTIL_PROG inspect-internal dump-tree -t 5 $SCRATCH_DEV | \
+ grep EXTENT_DATA | wc -l
+ _scratch_mount
+}
+
+_scratch_mkfs >>$seqres.full 2>&1 || _fail "mkfs failed"
+_scratch_mount
+
+# Create a file with a size of 256K and 4 written extents of 64K each.
+# We fallocate to guarantee exact extent size, even if compression mount
+# option is give, and write to them because defrag skips prealloc extents.
+$XFS_IO_PROG -f -c "falloc 0 64K" \
+ -c "pwrite -S 0xab 0 64K" \
+ -c "falloc 64K 64K" \
+ -c "pwrite -S 0xcd 64K 64K" \
+ -c "falloc 128K 64K" \
+ -c "pwrite -S 0xef 128K 64K" \
+ -c "falloc 192K 64K" \
+ -c "pwrite -S 0x73 192K 64K" \
+ $SCRATCH_MNT/foo | _filter_xfs_io
+
+echo -n "Initial number of file extent items: "
+count_file_extent_items
+
+# Read the whole file in order to load extent maps and merge them.
+cat $SCRATCH_MNT/foo > /dev/null
+
+# Now defragment with a threshold of 128K. After this we expect to get a file
+# with 1 file extent item - the threshold is 128K but since all the extents are
+# contiguous, they should be merged into a single one of 256K.
+$BTRFS_UTIL_PROG filesystem defragment -t 128K $SCRATCH_MNT/foo
+echo -n "Number of file extent items after defrag with 128K threshold: "
+count_file_extent_items
+
+# Read the whole file in order to load extent maps and merge them.
+cat $SCRATCH_MNT/foo > /dev/null
+
+# Now defragment with a threshold of 256K. After this we expect to get a file
+# with only 1 file extent item.
+$BTRFS_UTIL_PROG filesystem defragment -t 256K $SCRATCH_MNT/foo
+echo -n "Number of file extent items after defrag with 256K threshold: "
+count_file_extent_items
+
+# Check that the file has the expected data, that defrag didn't cause any data
+# loss or corruption.
+echo "File data after defrag:"
+_hexdump $SCRATCH_MNT/foo
+
+status=0
+exit
diff --git a/tests/btrfs/325.out b/tests/btrfs/325.out
new file mode 100644
index 00000000..c0df8137
--- /dev/null
+++ b/tests/btrfs/325.out
@@ -0,0 +1,22 @@
+QA output created by 325
+wrote 65536/65536 bytes at offset 0
+XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 65536/65536 bytes at offset 65536
+XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 65536/65536 bytes at offset 131072
+XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+wrote 65536/65536 bytes at offset 196608
+XXX Bytes, X ops; XX:XX:XX.X (XXX YYY/sec and XXX ops/sec)
+Initial number of file extent items: 4
+Number of file extent items after defrag with 128K threshold: 1
+Number of file extent items after defrag with 256K threshold: 1
+File data after defrag:
+000000 ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab ab >................<
+*
+010000 cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd cd >................<
+*
+020000 ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef ef >................<
+*
+030000 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 73 >ssssssssssssssss<
+*
+040000
--
2.45.2
^ permalink raw reply related [flat|nested] 8+ messages in thread* Re: [PATCH v3] btrfs: add a test for defrag of contiguous file extents
2024-11-04 11:41 ` [PATCH v3] " fdmanana
@ 2024-11-05 20:20 ` Anand Jain
0 siblings, 0 replies; 8+ messages in thread
From: Anand Jain @ 2024-11-05 20:20 UTC (permalink / raw)
To: fdmanana, fstests; +Cc: linux-btrfs, Filipe Manana, Qu Wenruo
On 4/11/24 19:41, fdmanana@kernel.org wrote:
> From: Filipe Manana <fdmanana@suse.com>
>
> Test that defrag merges adjacent extents that are contiguous.
> This exercises a regression fixed by a patchset for the kernel that is
> comprissed of the following patches:
>
> btrfs: fix extent map merging not happening for adjacent extents
> btrfs: fix defrag not merging contiguous extents due to merged extent maps
>
> Reviewed-by: Qu Wenruo <wqu@suse.com>
> Signed-off-by: Filipe Manana <fdmanana@suse.com>
Reviewed-by: Anand Jain <anand.jain@oracle.com>
Thx, Anand
^ permalink raw reply [flat|nested] 8+ messages in thread