]> www.infradead.org Git - users/hch/xfstests-dev.git/commitdiff
btrfs: defrag with regular and preallocated extents
authorQu Wenruo <wqu@suse.com>
Fri, 28 Jan 2022 00:26:59 +0000 (08:26 +0800)
committerEryu Guan <guaneryu@gmail.com>
Sun, 13 Feb 2022 17:15:38 +0000 (01:15 +0800)
Recent v5.16 has some regression around btrfs autodefrag mount
option, and the extra scrutiny around defrag code exposes some
questionable behavior from the old code.

One behavior is to defrag extents along with the next preallocated
extent.

This behavior will cause extra IO and convert all the preallocated
extent to regular zero filled extents, rendering the preallocated
extent useless.

The kernel fix is titled:

  btrfs: defrag: don't try to merge regular extents with preallocated extents

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Eryu Guan <guaneryu@gmail.com>
common/btrfs
common/rc
tests/btrfs/257 [new file with mode: 0755]
tests/btrfs/257.out [new file with mode: 0644]

index 4afe81eb2dfbce23af97a2d1dad5b4cae45bd647..5de926dd9bfaa5ae46833a411502c436ac833dd6 100644 (file)
@@ -480,3 +480,19 @@ _btrfs_no_v1_cache_opt()
        fi
        echo -n "-onospace_cache"
 }
+
+# Require certain sectorsize support
+_require_btrfs_support_sectorsize()
+{
+       local sectorsize=$1
+
+       # PAGE_SIZE as sectorsize is always supported
+       if [ $sectorsize -eq $(get_page_size) ]; then
+               return
+       fi
+
+       test -f /sys/fs/btrfs/features/supported_sectorsizes || \
+               _notrun "no subpage support found"
+       grep -wq $sectorsize /sys/fs/btrfs/features/supported_sectorsizes || \
+               _notrun "sectorsize $sectorsize is not supported"
+}
index a3b941251d389061959736febfd3ee8c5ccf0982..de60fb7b0677126c39e9abefdfcf173e93d4e566 100644 (file)
--- a/common/rc
+++ b/common/rc
@@ -3767,6 +3767,24 @@ _count_attr_extents()
        $XFS_IO_PROG -c "fiemap -a" $1 | tail -n +2 | grep -v hole | wc -l
 }
 
+# Get the sector number of the extent at @offset of @file
+_get_file_extent_sector()
+{
+       local file=$1
+       local offset=$2
+       local result
+
+       result=$($XFS_IO_PROG -c "fiemap $offset" "$file" | \
+                _filter_xfs_io_fiemap | head -n1 | $AWK_PROG '{print $3}')
+
+       # xfs_io fiemap will output nothing if there is only hole, so here
+       # to replace the empty string with "hole" instead
+       if [ -z "$result" ]; then
+               result="hole"
+       fi
+       echo "$result"
+}
+
 # arg 1 is dev to remove and is output of the below eg.
 # ls -l /sys/class/block/sdd | rev | cut -d "/" -f 3 | rev
 _devmgt_remove()
diff --git a/tests/btrfs/257 b/tests/btrfs/257
new file mode 100755 (executable)
index 0000000..5c5cdcc
--- /dev/null
@@ -0,0 +1,76 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (C) 2022 SUSE Linux Products GmbH. All Rights Reserved.
+#
+# FS QA Test 257
+#
+# Make sure btrfs doesn't defrag preallocated extents, nor lone extents
+# before preallocated extents.
+#
+
+. ./common/preamble
+_begin_fstest auto quick defrag prealloc
+
+# Override the default cleanup function.
+# _cleanup()
+# {
+#      cd /
+#      rm -r -f $tmp.*
+# }
+
+# Import common functions.
+. ./common/filter
+
+# real QA test starts here
+
+# Modify as appropriate.
+_supported_fs btrfs
+_require_scratch
+
+# Needs 4K sectorsize
+_require_btrfs_support_sectorsize 4096
+_require_xfs_io_command "falloc"
+
+_scratch_mkfs -s 4k >> $seqres.full 2>&1
+
+# Need datacow to make the defragged extents to have different bytenr
+_scratch_mount -o datacow
+
+# Create a file with the following layout:
+# 0       4K        8K        16K
+# |<- R ->|<-- Preallocated ->|
+# R is regular extents.
+#
+# In this case it makes no sense to defrag any extent.
+$XFS_IO_PROG -f -c "pwrite 0 4k" -c sync -c "falloc 4k 12k" \
+       "$SCRATCH_MNT/foobar" >> $seqres.full
+
+echo "=== Initial file extent layout ===" >> $seqres.full
+$XFS_IO_PROG -c "fiemap -v" "$SCRATCH_MNT/foobar" >> $seqres.full
+
+# Save the bytenr of both extents
+old_regular=$(_get_file_extent_sector "$SCRATCH_MNT/foobar" 0)
+old_prealloc=$(_get_file_extent_sector "$SCRATCH_MNT/foobar" 4096)
+
+# Now defrag and write the defragged range back to disk
+$BTRFS_UTIL_PROG filesystem defrag "$SCRATCH_MNT/foobar" >> $seqres.full
+sync
+
+echo "=== File extent layout after defrag ===" >> $seqres.full
+$XFS_IO_PROG -c "fiemap -v" "$SCRATCH_MNT/foobar" >> $seqres.full
+
+new_regular=$(_get_file_extent_sector "$SCRATCH_MNT/foobar" 0)
+new_prealloc=$(_get_file_extent_sector "$SCRATCH_MNT/foobar" 4096)
+
+if [ "$old_regular" -ne "$new_regular" ]; then
+       echo "the single lone sector got defragged"
+fi
+if [ "$old_prealloc" -ne "$new_prealloc" ]; then
+       echo "the preallocated extent got defragged"
+fi
+
+echo "Silence is golden"
+
+# success, all done
+status=0
+exit
diff --git a/tests/btrfs/257.out b/tests/btrfs/257.out
new file mode 100644 (file)
index 0000000..cc3693f
--- /dev/null
@@ -0,0 +1,2 @@
+QA output created by 257
+Silence is golden