Skip to contentSkip to conversation

btrfs: defrag related to corncer case handling

PATCH In review Qu Wenruo 2 participants 4 messages Nov 16 – Nov 17 Btrfs file system

lore
Share & tools
lore

Qu Wenruo

author
#0 lore raw

cover btrfs: defrag related to corncer case handling

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(-)
Signature · 1 line
2.56.0
author
#1 lore raw

PATCH 1/2 btrfs: defrag: break the loop early in defrag_get_extent()

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.

---
 fs/btrfs/defrag.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
fs/btrfs/defrag.c +9 -6
index cbc532b49c67..c3a9f1aff922 100644
@@ -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
Signature · 1 line
2.56.0
author
#2 lore raw

PATCH 2/2 btrfs: defrag: fix missed extent when the lookup lands on the next inode

In reply to Qu Wenruo

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.

---
 fs/btrfs/defrag.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
fs/btrfs/defrag.c +8 -2
index c3a9f1aff922..c6482aefa2a5 100644
@@ -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]--;
 	}
Signature · 1 line
2.56.0
Reviewed-by on the v1 series
#3 lore raw
In reply to Qu Wenruo
Quoted 4 lines · Qu Wenruo

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.

Quoted 8 lines

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

Looks good, thanks.

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

--
2.56.0