Skip to content

Commit 979b25c

Browse files
adam900710kdave
authored andcommitted
btrfs: defrag: don't defrag extents which are already at max capacity
[BUG] For compressed extents, defrag ioctl will always try to defrag any compressed extents, wasting not only IO but also CPU time to compress/decompress: mkfs.btrfs -f $DEV mount -o compress $DEV $MNT xfs_io -f -c "pwrite -S 0xab 0 128K" $MNT/foobar sync xfs_io -f -c "pwrite -S 0xcd 128K 128K" $MNT/foobar sync echo "=== before ===" xfs_io -c "fiemap -v" $MNT/foobar btrfs filesystem defrag $MNT/foobar sync echo "=== after ===" xfs_io -c "fiemap -v" $MNT/foobar Then it shows the 2 128K extents just get COW for no extra benefit, with extra IO/CPU spent: === before === /mnt/btrfs/file1: EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS 0: [0..255]: 26624..26879 256 0x8 1: [256..511]: 26632..26887 256 0x9 === after === /mnt/btrfs/file1: EXT: FILE-OFFSET BLOCK-RANGE TOTAL FLAGS 0: [0..255]: 26640..26895 256 0x8 1: [256..511]: 26648..26903 256 0x9 This affects not only v5.16 (after the defrag rework), but also v5.15 (before the defrag rework). [CAUSE] From the very beginning, btrfs defrag never checks if one extent is already at its max capacity (128K for compressed extents, 128M otherwise). And the default extent size threshold is 256K, which is already beyond the compressed extent max size. This means, by default btrfs defrag ioctl will mark all compressed extent which is not adjacent to a hole/preallocated range for defrag. [FIX] Introduce a helper to grab the maximum extent size, and then in defrag_collect_targets() and defrag_check_next_extent(), reject extents which are already at their max capacity. Reported-by: Filipe Manana <fdmanana@suse.com> CC: stable@vger.kernel.org # 5.16 Reviewed-by: Filipe Manana <fdmanana@suse.com> Signed-off-by: Qu Wenruo <wqu@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent 7093f15 commit 979b25c

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

fs/btrfs/ioctl.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,13 @@ static struct extent_map *defrag_lookup_extent(struct inode *inode, u64 start,
10461046
return em;
10471047
}
10481048

1049+
static u32 get_extent_max_capacity(const struct extent_map *em)
1050+
{
1051+
if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags))
1052+
return BTRFS_MAX_COMPRESSED;
1053+
return BTRFS_MAX_EXTENT_SIZE;
1054+
}
1055+
10491056
static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
10501057
bool locked)
10511058
{
@@ -1062,6 +1069,12 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
10621069
goto out;
10631070
if (test_bit(EXTENT_FLAG_PREALLOC, &next->flags))
10641071
goto out;
1072+
/*
1073+
* If the next extent is at its max capacity, defragging current extent
1074+
* makes no sense, as the total number of extents won't change.
1075+
*/
1076+
if (next->len >= get_extent_max_capacity(em))
1077+
goto out;
10651078
/* Physically adjacent and large enough */
10661079
if ((em->block_start + em->block_len == next->block_start) &&
10671080
(em->block_len > SZ_128K && next->block_len > SZ_128K))
@@ -1262,6 +1275,13 @@ static int defrag_collect_targets(struct btrfs_inode *inode,
12621275
if (range_len >= extent_thresh)
12631276
goto next;
12641277

1278+
/*
1279+
* Skip extents already at its max capacity, this is mostly for
1280+
* compressed extents, which max cap is only 128K.
1281+
*/
1282+
if (em->len >= get_extent_max_capacity(em))
1283+
goto next;
1284+
12651285
next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em,
12661286
locked);
12671287
if (!next_mergeable) {

0 commit comments

Comments
 (0)