Skip to content

Commit 75a36a7

Browse files
adam900710kdave
authored andcommitted
btrfs: avoid defragging extents whose next extents are not targets
[BUG] There is a report that autodefrag is defragging single sector, which is completely waste of IO, and no help for defragging: btrfs-cleaner-808 defrag_one_locked_range: root=256 ino=651122 start=0 len=4096 [CAUSE] In defrag_collect_targets(), we check if the current range (A) can be merged with next one (B). If mergeable, we will add range A into target for defrag. However there is a catch for autodefrag, when checking mergeability against range B, we intentionally pass 0 as @newer_than, hoping to get a higher chance to merge with the next extent. But in the next iteration, range B will looked up by defrag_lookup_extent(), with non-zero @newer_than. And if range B is not really newer, it will rejected directly, causing only range A being defragged, while we expect to defrag both range A and B. [FIX] Since the root cause is the difference in check condition of defrag_check_next_extent() and defrag_collect_targets(), we fix it by: 1. Pass @newer_than to defrag_check_next_extent() 2. Pass @extent_thresh to defrag_check_next_extent() This makes the check between defrag_collect_targets() and defrag_check_next_extent() more consistent. While there is still some minor difference, the remaining checks are focus on runtime flags like writeback/delalloc, which are mostly transient and safe to be checked only in defrag_collect_targets(). Link: btrfs#423 (comment) CC: [email protected] # 5.16+ Reviewed-by: Filipe Manana <[email protected]> Signed-off-by: Qu Wenruo <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 05fd956 commit 75a36a7

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

fs/btrfs/ioctl.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,7 @@ static u32 get_extent_max_capacity(const struct extent_map *em)
12391239
}
12401240

12411241
static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
1242-
bool locked)
1242+
u32 extent_thresh, u64 newer_than, bool locked)
12431243
{
12441244
struct extent_map *next;
12451245
bool ret = false;
@@ -1249,11 +1249,12 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
12491249
return false;
12501250

12511251
/*
1252-
* We want to check if the next extent can be merged with the current
1253-
* one, which can be an extent created in a past generation, so we pass
1254-
* a minimum generation of 0 to defrag_lookup_extent().
1252+
* Here we need to pass @newer_then when checking the next extent, or
1253+
* we will hit a case we mark current extent for defrag, but the next
1254+
* one will not be a target.
1255+
* This will just cause extra IO without really reducing the fragments.
12551256
*/
1256-
next = defrag_lookup_extent(inode, em->start + em->len, 0, locked);
1257+
next = defrag_lookup_extent(inode, em->start + em->len, newer_than, locked);
12571258
/* No more em or hole */
12581259
if (!next || next->block_start >= EXTENT_MAP_LAST_BYTE)
12591260
goto out;
@@ -1265,6 +1266,13 @@ static bool defrag_check_next_extent(struct inode *inode, struct extent_map *em,
12651266
*/
12661267
if (next->len >= get_extent_max_capacity(em))
12671268
goto out;
1269+
/* Skip older extent */
1270+
if (next->generation < newer_than)
1271+
goto out;
1272+
/* Also check extent size */
1273+
if (next->len >= extent_thresh)
1274+
goto out;
1275+
12681276
ret = true;
12691277
out:
12701278
free_extent_map(next);
@@ -1470,7 +1478,7 @@ static int defrag_collect_targets(struct btrfs_inode *inode,
14701478
goto next;
14711479

14721480
next_mergeable = defrag_check_next_extent(&inode->vfs_inode, em,
1473-
locked);
1481+
extent_thresh, newer_than, locked);
14741482
if (!next_mergeable) {
14751483
struct defrag_target_range *last;
14761484

0 commit comments

Comments
 (0)