All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] btrfs: defrag related to corncer case handling
@ 2026-11-16 21:29 Qu Wenruo
  2026-11-16 21:29 ` [PATCH 1/2] btrfs: defrag: break the loop early in defrag_get_extent() Qu Wenruo
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-11-16 21:29 UTC (permalink / raw)
  To: linux-btrfs

The first one is a Sashiko report that we can step into the next inode
and continue looping unnecessarily. Fix it by re-ordering the checks.

The second one is another minor corner case where if the defrag range
starts inside the last extent, we may skip a valid defrag target.
Fix it by doing the backward search properly.

Qu Wenruo (2):
  btrfs: defrag: break the loop early in defrag_get_extent()
  btrfs: defrag: fix missed extent when the lookup lands on the next
    inode

 fs/btrfs/defrag.c | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)

-- 
2.56.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] btrfs: defrag: break the loop early in defrag_get_extent()
  2026-11-16 21:29 [PATCH 0/2] btrfs: defrag related to corncer case handling Qu Wenruo
@ 2026-11-16 21:29 ` Qu Wenruo
  2026-11-16 21:29 ` [PATCH 2/2] btrfs: defrag: fix missed extent when the lookup lands on the next inode Qu Wenruo
  2026-11-17 15:49 ` [PATCH 0/2] btrfs: defrag related to corncer case handling Filipe Manana
  2 siblings, 0 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-11-16 21:29 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Sashiko

Inside defrag_get_extent() we have the following checks determining
whether we should continue searching:

		if (WARN_ON(key.objectid < ino) || key.type < BTRFS_EXTENT_DATA_KEY)
			goto next;

		/* It's beyond our target range, definitely not extent found */
		if (key.objectid > ino || key.type > BTRFS_EXTENT_DATA_KEY)
			goto not_found;

However the above order is incorrect.

If we are already at the next inode, but the current key type is still
smaller than BTRFS_EXTENT_DATA_KEY, e.g. BTRFS_INODE_ITEM_KEY and
followed by BTRFS_DIR_ITEM_KEY, then we will continue iterating, even
it's definitely not necessary.

This is only a performance related bug, and requires the next inode to
be a very large directory to really cause observable performance
degradation.

Fix it by doing the not-found check first.

Reported-by: Sashiko <sashiko-bot@kernel.org>
Link: https://sashiko.dev/#/bug/linux-e166e8db-2bdf-4c02-8eb1-9701b3939ea4
Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/defrag.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/fs/btrfs/defrag.c b/fs/btrfs/defrag.c
index cbc532b49c67..c3a9f1aff922 100644
--- a/fs/btrfs/defrag.c
+++ b/fs/btrfs/defrag.c
@@ -674,18 +674,21 @@ static struct extent_map *defrag_get_extent(struct btrfs_inode *inode,
 
 		btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
 
-		/*
-		 * We may go one slot back to INODE_REF/XATTR item, then
-		 * need to go forward until we reach an EXTENT_DATA.
-		 * But we should still has the correct ino as key.objectid.
-		 */
-		if (WARN_ON(key.objectid < ino) || key.type < BTRFS_EXTENT_DATA_KEY)
+		if (WARN_ON(key.objectid < ino))
 			goto next;
 
 		/* It's beyond our target range, definitely not extent found */
 		if (key.objectid > ino || key.type > BTRFS_EXTENT_DATA_KEY)
 			goto not_found;
 
+		/*
+		 * We may go one slot back to INODE_REF/XATTR item, then
+		 * need to go forward until we reach an EXTENT_DATA.
+		 * But we should still has the correct ino as key.objectid.
+		 */
+		if (key.type < BTRFS_EXTENT_DATA_KEY)
+			goto next;
+
 		/*
 		 *	|	|<- File extent ->|
 		 *	\- start
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] btrfs: defrag: fix missed extent when the lookup lands on the next inode
  2026-11-16 21:29 [PATCH 0/2] btrfs: defrag related to corncer case handling Qu Wenruo
  2026-11-16 21:29 ` [PATCH 1/2] btrfs: defrag: break the loop early in defrag_get_extent() Qu Wenruo
@ 2026-11-16 21:29 ` Qu Wenruo
  2026-11-17 15:49 ` [PATCH 0/2] btrfs: defrag related to corncer case handling Filipe Manana
  2 siblings, 0 replies; 4+ messages in thread
From: Qu Wenruo @ 2026-11-16 21:29 UTC (permalink / raw)
  To: linux-btrfs

In defrag_get_extent(), if @start is inside the last extent of an inode
and the next inode's items are in the same leaf, btrfs_search_slot()
points to the next inode's item.

The code then checks the item at the current slot to decide whether to
go one slot back, which is never an EXTENT_DATA of our inode in this
case. So the extent covering @start is skipped and we return NULL.

Fix it by checking the previous item instead, which is the one that can
cover @start.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/defrag.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/fs/btrfs/defrag.c b/fs/btrfs/defrag.c
index c3a9f1aff922..c6482aefa2a5 100644
--- a/fs/btrfs/defrag.c
+++ b/fs/btrfs/defrag.c
@@ -657,9 +657,15 @@ static struct extent_map *defrag_get_extent(struct btrfs_inode *inode,
 	    key.offset == start)
 		goto iterate;
 
-	/* We didn't find a perfect match, needs to go one slot back */
+	/*
+	 * We didn't find a perfect match, go one slot back if the previous item
+	 * is an EXTENT_DATA of our inode, as it may cover @start.
+	 *
+	 * Note the item at the current slot can belong to the next inode, in
+	 * which case we still need to check the previous one.
+	 */
 	if (path.slots[0] > 0) {
-		btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
+		btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0] - 1);
 		if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY)
 			path.slots[0]--;
 	}
-- 
2.56.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] btrfs: defrag related to corncer case handling
  2026-11-16 21:29 [PATCH 0/2] btrfs: defrag related to corncer case handling Qu Wenruo
  2026-11-16 21:29 ` [PATCH 1/2] btrfs: defrag: break the loop early in defrag_get_extent() Qu Wenruo
  2026-11-16 21:29 ` [PATCH 2/2] btrfs: defrag: fix missed extent when the lookup lands on the next inode Qu Wenruo
@ 2026-11-17 15:49 ` Filipe Manana
  2 siblings, 0 replies; 4+ messages in thread
From: Filipe Manana @ 2026-11-17 15:49 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs

On Mon, Nov 16, 2026 at 9:33 PM Qu Wenruo <wqu@suse.com> wrote:
>
> The first one is a Sashiko report that we can step into the next inode
> and continue looping unnecessarily. Fix it by re-ordering the checks.
>
> The second one is another minor corner case where if the defrag range
> starts inside the last extent, we may skip a valid defrag target.
> Fix it by doing the backward search properly.
>
> Qu Wenruo (2):
>   btrfs: defrag: break the loop early in defrag_get_extent()
>   btrfs: defrag: fix missed extent when the lookup lands on the next
>     inode

Reviewed-by: Filipe Manana <fdmanana@suse.com>

Looks good, thanks.


>
>  fs/btrfs/defrag.c | 25 +++++++++++++++++--------
>  1 file changed, 17 insertions(+), 8 deletions(-)
>
> --
> 2.56.0
>
>

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-11-17 15:50 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-16 21:29 [PATCH 0/2] btrfs: defrag related to corncer case handling Qu Wenruo
2026-11-16 21:29 ` [PATCH 1/2] btrfs: defrag: break the loop early in defrag_get_extent() Qu Wenruo
2026-11-16 21:29 ` [PATCH 2/2] btrfs: defrag: fix missed extent when the lookup lands on the next inode Qu Wenruo
2026-11-17 15:49 ` [PATCH 0/2] btrfs: defrag related to corncer case handling Filipe Manana

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.